VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/VBoxGuestInternal.h@ 57358

Last change on this file since 57358 was 55275, checked in by vboxsync, 10 years ago

heartbeat service: document it, sanity check and fixed logging

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.2 KB
Line 
1/* $Id: VBoxGuestInternal.h 55275 2015-04-15 10:57:04Z vboxsync $ */
2/** @file
3 * VBoxGuest - Guest Additions Driver.
4 */
5
6/*
7 * Copyright (C) 2010-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27#ifndef ___VBoxGuestInternal_h
28#define ___VBoxGuestInternal_h
29
30#include <iprt/types.h>
31#include <iprt/list.h>
32#include <iprt/semaphore.h>
33#include <iprt/spinlock.h>
34#include <iprt/timer.h>
35#include <VBox/VMMDev.h>
36#include <VBox/VBoxGuest.h>
37#include <VBox/VBoxGuestLib.h>
38
39/** @def VBOXGUEST_USE_WAKE_UP_LIST
40 * Defer wake-up of waiting thread when defined. */
41#if defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS) || defined(RT_OS_WINDOWS) || defined(DOXYGEN_RUNNING)
42# define VBOXGUEST_USE_DEFERRED_WAKE_UP
43#endif
44
45
46/** Pointer to the VBoxGuest per session data. */
47typedef struct VBOXGUESTSESSION *PVBOXGUESTSESSION;
48
49/** Pointer to a wait-for-event entry. */
50typedef struct VBOXGUESTWAIT *PVBOXGUESTWAIT;
51
52/**
53 * VBox guest wait for event entry.
54 *
55 * Each waiting thread allocates one of these items and adds
56 * it to the wait list before going to sleep on the event sem.
57 */
58typedef struct VBOXGUESTWAIT
59{
60 /** The list node. */
61 RTLISTNODE ListNode;
62 /** The events we are waiting on. */
63 uint32_t fReqEvents;
64 /** The events we received. */
65 uint32_t volatile fResEvents;
66#ifdef VBOXGUEST_USE_DEFERRED_WAKE_UP
67 /** Set by VbgdCommonWaitDoWakeUps before leaving the spinlock to call
68 * RTSemEventMultiSignal. */
69 bool volatile fPendingWakeUp;
70 /** Set by the requestor thread if it got the spinlock before the
71 * signaller. Deals with the race in VbgdCommonWaitDoWakeUps. */
72 bool volatile fFreeMe;
73#endif
74 /** The event semaphore. */
75 RTSEMEVENTMULTI Event;
76 /** The session that's waiting. */
77 PVBOXGUESTSESSION pSession;
78#ifdef VBOX_WITH_HGCM
79 /** The HGCM request we're waiting for to complete. */
80 VMMDevHGCMRequestHeader volatile *pHGCMReq;
81#endif
82} VBOXGUESTWAIT;
83
84
85/**
86 * VBox guest memory balloon.
87 */
88typedef struct VBOXGUESTMEMBALLOON
89{
90 /** Mutex protecting the members below from concurrent access.. */
91 RTSEMFASTMUTEX hMtx;
92 /** The current number of chunks in the balloon. */
93 uint32_t cChunks;
94 /** The maximum number of chunks in the balloon (typically the amount of guest
95 * memory / chunksize). */
96 uint32_t cMaxChunks;
97 /** This is true if we are using RTR0MemObjAllocPhysNC() / RTR0MemObjGetPagePhysAddr()
98 * and false otherwise. */
99 bool fUseKernelAPI;
100 /** The current owner of the balloon.
101 * This is automatically assigned to the first session using the ballooning
102 * API and first released when the session closes. */
103 PVBOXGUESTSESSION pOwner;
104 /** The pointer to the array of memory objects holding the chunks of the
105 * balloon. This array is cMaxChunks in size when present. */
106 PRTR0MEMOBJ paMemObj;
107} VBOXGUESTMEMBALLOON;
108/** Pointer to a memory balloon. */
109typedef VBOXGUESTMEMBALLOON *PVBOXGUESTMEMBALLOON;
110
111
112/**
113 * Per bit usage tracker for a uint32_t mask.
114 *
115 * Used for optimal handling of guest properties, mouse status and event filter.
116 */
117typedef struct VBOXGUESTBITUSAGETRACER
118{
119 /** Per bit usage counters. */
120 uint32_t acPerBitUsage[32];
121 /** The current mask according to acPerBitUsage. */
122 uint32_t fMask;
123} VBOXGUESTBITUSAGETRACER;
124/** Pointer to a per bit usage tracker. */
125typedef VBOXGUESTBITUSAGETRACER *PVBOXGUESTBITUSAGETRACER;
126/** Pointer to a const per bit usage tracker. */
127typedef VBOXGUESTBITUSAGETRACER const *PCVBOXGUESTBITUSAGETRACER;
128
129
130/**
131 * VBox guest device (data) extension.
132 */
133typedef struct VBOXGUESTDEVEXT
134{
135 /** The base of the adapter I/O ports. */
136 RTIOPORT IOPortBase;
137 /** Pointer to the mapping of the VMMDev adapter memory. */
138 VMMDevMemory volatile *pVMMDevMemory;
139 /** The memory object reserving space for the guest mappings. */
140 RTR0MEMOBJ hGuestMappings;
141 /** Spinlock protecting the signaling and resetting of the wait-for-event
142 * semaphores as well as the event acking in the ISR. */
143 RTSPINLOCK EventSpinlock;
144 /** Preallocated VMMDevEvents for the IRQ handler. */
145 VMMDevEvents *pIrqAckEvents;
146 /** The physical address of pIrqAckEvents. */
147 RTCCPHYS PhysIrqAckEvents;
148 /** Wait-for-event list for threads waiting for multiple events
149 * (VBOXGUESTWAIT). */
150 RTLISTANCHOR WaitList;
151#ifdef VBOX_WITH_HGCM
152 /** Wait-for-event list for threads waiting on HGCM async completion
153 * (VBOXGUESTWAIT).
154 *
155 * The entire list is evaluated upon the arrival of an HGCM event, unlike
156 * the other lists which are only evaluated till the first thread has
157 * been woken up. */
158 RTLISTANCHOR HGCMWaitList;
159#endif
160#ifdef VBOXGUEST_USE_DEFERRED_WAKE_UP
161 /** List of wait-for-event entries that needs waking up
162 * (VBOXGUESTWAIT). */
163 RTLISTANCHOR WakeUpList;
164#endif
165 /** List of wait-for-event entries that has been woken up
166 * (VBOXGUESTWAIT). */
167 RTLISTANCHOR WokenUpList;
168 /** List of free wait-for-event entries (VBOXGUESTWAIT). */
169 RTLISTANCHOR FreeList;
170 /** Mask of pending events. */
171 uint32_t volatile f32PendingEvents;
172 /** Current VMMDEV_EVENT_MOUSE_POSITION_CHANGED sequence number.
173 * Used to implement polling. */
174 uint32_t volatile u32MousePosChangedSeq;
175
176 /** Spinlock various items in the VBOXGUESTSESSION. */
177 RTSPINLOCK SessionSpinlock;
178 /** List of guest sessions (VBOXGUESTSESSION). We currently traverse this
179 * but do not search it, so a list data type should be fine. Use under the
180 * #SessionSpinlock lock. */
181 RTLISTANCHOR SessionList;
182 /** Number of session. */
183 uint32_t cSessions;
184 /** Flag indicating whether logging to the release log
185 * is enabled. */
186 bool fLoggingEnabled;
187 /** Memory balloon information for RTR0MemObjAllocPhysNC(). */
188 VBOXGUESTMEMBALLOON MemBalloon;
189 /** Callback and user data for a kernel mouse handler. */
190 VBoxGuestMouseSetNotifyCallback MouseNotifyCallback;
191
192 /** @name Host Event Filtering
193 * @{ */
194 /** Events we won't permit anyone to filter out. */
195 uint32_t fFixedEvents;
196 /** Usage counters for the host events. (Fixed events are not included.) */
197 VBOXGUESTBITUSAGETRACER EventFilterTracker;
198 /** The event filter last reported to the host (UINT32_MAX on failure). */
199 uint32_t fEventFilterHost;
200 /** @} */
201
202 /** @name Mouse Status
203 * @{ */
204 /** Usage counters for the mouse statuses (VMMDEV_MOUSE_XXX). */
205 VBOXGUESTBITUSAGETRACER MouseStatusTracker;
206 /** The mouse status last reported to the host (UINT32_MAX on failure). */
207 uint32_t fMouseStatusHost;
208 /** @} */
209
210 /** @name Guest Capabilities
211 * @{ */
212 /** Guest capabilities which have been set to "acquire" mode. This means
213 * that only one session can use them at a time, and that they will be
214 * automatically cleaned up if that session exits without doing so.
215 *
216 * Protected by VBOXGUESTDEVEXT::SessionSpinlock, but is unfortunately read
217 * without holding the lock in a couple of places. */
218 uint32_t volatile fAcquireModeGuestCaps;
219 /** Guest capabilities which have been set to "set" mode. This just means
220 * that they have been blocked from ever being set to "acquire" mode. */
221 uint32_t fSetModeGuestCaps;
222 /** Mask of all capabilities which are currently acquired by some session
223 * and as such reported to the host. */
224 uint32_t fAcquiredGuestCaps;
225 /** Usage counters for guest capabilities in "set" mode. Indexed by
226 * capability bit number, one count per session using a capability. */
227 VBOXGUESTBITUSAGETRACER SetGuestCapsTracker;
228 /** The guest capabilities last reported to the host (UINT32_MAX on failure). */
229 uint32_t fGuestCapsHost;
230 /** @} */
231
232 /** Heartbeat timer which fires with interval
233 * cNsHearbeatInterval and its handler sends
234 * VMMDevReq_GuestHeartbeat to VMMDev. */
235 PRTTIMER pHeartbeatTimer;
236 /** Heartbeat timer interval in nanoseconds. */
237 uint64_t cNsHeartbeatInterval;
238 /** Preallocated VMMDevReq_GuestHeartbeat request. */
239 VMMDevRequestHeader *pReqGuestHeartbeat;
240} VBOXGUESTDEVEXT;
241/** Pointer to the VBoxGuest driver data. */
242typedef VBOXGUESTDEVEXT *PVBOXGUESTDEVEXT;
243
244
245/**
246 * The VBoxGuest per session data.
247 */
248typedef struct VBOXGUESTSESSION
249{
250 /** The list node. */
251 RTLISTNODE ListNode;
252#if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD) || defined(RT_OS_OS2) || defined(RT_OS_SOLARIS)
253 /** Pointer to the next session with the same hash. */
254 PVBOXGUESTSESSION pNextHash;
255#endif
256#if defined(RT_OS_OS2)
257 /** The system file number of this session. */
258 uint16_t sfn;
259 uint16_t Alignment; /**< Alignment */
260#endif
261 /** The process (id) of the session.
262 * This is NIL if it's a kernel session. */
263 RTPROCESS Process;
264 /** Which process this session is associated with.
265 * This is NIL if it's a kernel session. */
266 RTR0PROCESS R0Process;
267 /** Pointer to the device extension. */
268 PVBOXGUESTDEVEXT pDevExt;
269
270#ifdef VBOX_WITH_HGCM
271 /** Array containing HGCM client IDs associated with this session.
272 * This will be automatically disconnected when the session is closed. */
273 uint32_t volatile aHGCMClientIds[64];
274#endif
275 /** The last consumed VMMDEV_EVENT_MOUSE_POSITION_CHANGED sequence number.
276 * Used to implement polling. */
277 uint32_t volatile u32MousePosChangedSeq;
278 /** Host events requested by the session.
279 * An event type requested in any guest session will be added to the host
280 * filter. Protected by VBOXGUESTDEVEXT::SessionSpinlock. */
281 uint32_t fEventFilter;
282 /** Guest capabilities held in "acquired" by this session.
283 * Protected by VBOXGUESTDEVEXT::SessionSpinlock, but is unfortunately read
284 * without holding the lock in a couple of places. */
285 uint32_t volatile fAcquiredGuestCaps;
286 /** Guest capabilities in "set" mode for this session.
287 * These accumulated for sessions via VBOXGUESTDEVEXT::acGuestCapsSet and
288 * reported to the host. Protected by VBOXGUESTDEVEXT::SessionSpinlock. */
289 uint32_t fCapabilities;
290 /** Mouse features supported. A feature enabled in any guest session will
291 * be enabled for the host.
292 * @note We invert the VMMDEV_MOUSE_GUEST_NEEDS_HOST_CURSOR feature in this
293 * bitmap. The logic of this is that the real feature is when the host
294 * cursor is not needed, and we tell the host it is not needed if any
295 * session explicitly fails to assert it. Storing it inverted simplifies
296 * the checks.
297 * Use under the VBOXGUESTDEVEXT#SessionSpinlock lock. */
298 uint32_t fMouseStatus;
299#ifdef RT_OS_DARWIN
300 /** Pointer to the associated org_virtualbox_VBoxGuestClient object. */
301 void *pvVBoxGuestClient;
302 /** Whether this session has been opened or not. */
303 bool fOpened;
304#endif
305 /** Whether a CANCEL_ALL_WAITEVENTS is pending. This happens when
306 * CANCEL_ALL_WAITEVENTS is called, but no call to WAITEVENT is in process
307 * in the current session. In that case the next call will be interrupted
308 * at once. */
309 bool volatile fPendingCancelWaitEvents;
310 /** Does this session belong to a root process or a user one? */
311 bool fUserSession;
312} VBOXGUESTSESSION;
313
314RT_C_DECLS_BEGIN
315
316int VbgdCommonInitDevExt(PVBOXGUESTDEVEXT pDevExt, uint16_t IOPortBase, void *pvMMIOBase, uint32_t cbMMIO,
317 VBOXOSTYPE enmOSType, uint32_t fEvents);
318bool VbgdCommonISR(PVBOXGUESTDEVEXT pDevExt);
319void VbgdCommonDeleteDevExt(PVBOXGUESTDEVEXT pDevExt);
320int VbgdCommonReinitDevExtAfterHibernation(PVBOXGUESTDEVEXT pDevExt, VBOXOSTYPE enmOSType);
321#ifdef VBOXGUEST_USE_DEFERRED_WAKE_UP
322void VbgdCommonWaitDoWakeUps(PVBOXGUESTDEVEXT pDevExt);
323#endif
324
325int VbgdCommonCreateUserSession(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION *ppSession);
326int VbgdCommonCreateKernelSession(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION *ppSession);
327void VbgdCommonCloseSession(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession);
328
329int VbgdCommonIoCtlFast(unsigned iFunction, PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession);
330int VbgdCommonIoCtl(unsigned iFunction, PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession,
331 void *pvData, size_t cbData, size_t *pcbDataReturned);
332
333/**
334 * ISR callback for notifying threads polling for mouse events.
335 *
336 * This is called at the end of the ISR, after leaving the event spinlock, if
337 * VMMDEV_EVENT_MOUSE_POSITION_CHANGED was raised by the host.
338 *
339 * @param pDevExt The device extension.
340 */
341void VbgdNativeISRMousePollEvent(PVBOXGUESTDEVEXT pDevExt);
342
343
344#ifdef VBOX_WITH_DPC_LATENCY_CHECKER
345int VbgdNtIOCtl_DpcLatencyChecker(void);
346#endif
347
348RT_C_DECLS_END
349
350#endif
351
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette