VirtualBox

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

Last change on this file since 70344 was 70223, checked in by vboxsync, 7 years ago

VBoxGuest: Split up VGDrvCommonInitDevExt to accommodate plug-and-play on windows. This should fix some error cleanup path issues on windows.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.3 KB
Line 
1/* $Id: VBoxGuestInternal.h 70223 2017-12-19 17:10:59Z vboxsync $ */
2/** @file
3 * VBoxGuest - Guest Additions Driver, Internal Header.
4 */
5
6/*
7 * Copyright (C) 2010-2017 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_DEFERRED_WAKE_UP
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/** @def VBOXGUEST_MOUSE_NOTIFY_CAN_PREEMPT
46 * The mouse notification callback can cause preemption and must not be invoked
47 * while holding a high-level spinlock.
48 */
49#if defined(RT_OS_SOLARIS) || defined(RT_OS_WINDOWS) || defined(DOXYGEN_RUNNING)
50# define VBOXGUEST_MOUSE_NOTIFY_CAN_PREEMPT
51#endif
52
53/** Pointer to the VBoxGuest per session data. */
54typedef struct VBOXGUESTSESSION *PVBOXGUESTSESSION;
55
56/** Pointer to a wait-for-event entry. */
57typedef struct VBOXGUESTWAIT *PVBOXGUESTWAIT;
58
59/**
60 * VBox guest wait for event entry.
61 *
62 * Each waiting thread allocates one of these items and adds
63 * it to the wait list before going to sleep on the event sem.
64 */
65typedef struct VBOXGUESTWAIT
66{
67 /** The list node. */
68 RTLISTNODE ListNode;
69 /** The events we are waiting on. */
70 uint32_t fReqEvents;
71 /** The events we received. */
72 uint32_t volatile fResEvents;
73#ifdef VBOXGUEST_USE_DEFERRED_WAKE_UP
74 /** Set by VGDrvCommonWaitDoWakeUps before leaving the spinlock to call
75 * RTSemEventMultiSignal. */
76 bool volatile fPendingWakeUp;
77 /** Set by the requestor thread if it got the spinlock before the
78 * signaller. Deals with the race in VGDrvCommonWaitDoWakeUps. */
79 bool volatile fFreeMe;
80#endif
81 /** The event semaphore. */
82 RTSEMEVENTMULTI Event;
83 /** The session that's waiting. */
84 PVBOXGUESTSESSION pSession;
85#ifdef VBOX_WITH_HGCM
86 /** The HGCM request we're waiting for to complete. */
87 VMMDevHGCMRequestHeader volatile *pHGCMReq;
88#endif
89} VBOXGUESTWAIT;
90
91
92/**
93 * VBox guest memory balloon.
94 */
95typedef struct VBOXGUESTMEMBALLOON
96{
97 /** Mutex protecting the members below from concurrent access. */
98 RTSEMFASTMUTEX hMtx;
99 /** The current number of chunks in the balloon. */
100 uint32_t cChunks;
101 /** The maximum number of chunks in the balloon (typically the amount of guest
102 * memory / chunksize). */
103 uint32_t cMaxChunks;
104 /** This is true if we are using RTR0MemObjAllocPhysNC() / RTR0MemObjGetPagePhysAddr()
105 * and false otherwise. */
106 bool fUseKernelAPI;
107 /** The current owner of the balloon.
108 * This is automatically assigned to the first session using the ballooning
109 * API and first released when the session closes. */
110 PVBOXGUESTSESSION pOwner;
111 /** The pointer to the array of memory objects holding the chunks of the
112 * balloon. This array is cMaxChunks in size when present. */
113 PRTR0MEMOBJ paMemObj;
114} VBOXGUESTMEMBALLOON;
115/** Pointer to a memory balloon. */
116typedef VBOXGUESTMEMBALLOON *PVBOXGUESTMEMBALLOON;
117
118
119/**
120 * Per bit usage tracker for a uint32_t mask.
121 *
122 * Used for optimal handling of guest properties, mouse status and event filter.
123 */
124typedef struct VBOXGUESTBITUSAGETRACER
125{
126 /** Per bit usage counters. */
127 uint32_t acPerBitUsage[32];
128 /** The current mask according to acPerBitUsage. */
129 uint32_t fMask;
130} VBOXGUESTBITUSAGETRACER;
131/** Pointer to a per bit usage tracker. */
132typedef VBOXGUESTBITUSAGETRACER *PVBOXGUESTBITUSAGETRACER;
133/** Pointer to a const per bit usage tracker. */
134typedef VBOXGUESTBITUSAGETRACER const *PCVBOXGUESTBITUSAGETRACER;
135
136
137/**
138 * VBox guest device (data) extension.
139 */
140typedef struct VBOXGUESTDEVEXT
141{
142 /** VBOXGUESTDEVEXT_INIT_STATE_XXX. */
143 uint32_t uInitState;
144 /** The base of the adapter I/O ports. */
145 RTIOPORT IOPortBase;
146 /** Pointer to the mapping of the VMMDev adapter memory. */
147 VMMDevMemory volatile *pVMMDevMemory;
148 /** The memory object reserving space for the guest mappings. */
149 RTR0MEMOBJ hGuestMappings;
150 /** Spinlock protecting the signaling and resetting of the wait-for-event
151 * semaphores as well as the event acking in the ISR. */
152 RTSPINLOCK EventSpinlock;
153 /** Preallocated VMMDevEvents for the IRQ handler. */
154 VMMDevEvents *pIrqAckEvents;
155 /** The physical address of pIrqAckEvents. */
156 RTCCPHYS PhysIrqAckEvents;
157 /** Wait-for-event list for threads waiting for multiple events
158 * (VBOXGUESTWAIT). */
159 RTLISTANCHOR WaitList;
160#ifdef VBOX_WITH_HGCM
161 /** Wait-for-event list for threads waiting on HGCM async completion
162 * (VBOXGUESTWAIT).
163 *
164 * The entire list is evaluated upon the arrival of an HGCM event, unlike
165 * the other lists which are only evaluated till the first thread has
166 * been woken up. */
167 RTLISTANCHOR HGCMWaitList;
168#endif
169#ifdef VBOXGUEST_USE_DEFERRED_WAKE_UP
170 /** List of wait-for-event entries that needs waking up
171 * (VBOXGUESTWAIT). */
172 RTLISTANCHOR WakeUpList;
173#endif
174 /** List of wait-for-event entries that has been woken up
175 * (VBOXGUESTWAIT). */
176 RTLISTANCHOR WokenUpList;
177 /** List of free wait-for-event entries (VBOXGUESTWAIT). */
178 RTLISTANCHOR FreeList;
179 /** Mask of pending events. */
180 uint32_t volatile f32PendingEvents;
181 /** Current VMMDEV_EVENT_MOUSE_POSITION_CHANGED sequence number.
182 * Used to implement polling. */
183 uint32_t volatile u32MousePosChangedSeq;
184
185 /** Spinlock various items in the VBOXGUESTSESSION. */
186 RTSPINLOCK SessionSpinlock;
187 /** List of guest sessions (VBOXGUESTSESSION). We currently traverse this
188 * but do not search it, so a list data type should be fine. Use under the
189 * #SessionSpinlock lock. */
190 RTLISTANCHOR SessionList;
191 /** Number of session. */
192 uint32_t cSessions;
193 /** Flag indicating whether logging to the release log
194 * is enabled. */
195 bool fLoggingEnabled;
196 /** Memory balloon information for RTR0MemObjAllocPhysNC(). */
197 VBOXGUESTMEMBALLOON MemBalloon;
198 /** Mouse notification callback function. */
199 PFNVBOXGUESTMOUSENOTIFY pfnMouseNotifyCallback;
200 /** The callback argument for the mouse ntofication callback. */
201 void *pvMouseNotifyCallbackArg;
202
203 /** @name Host Event Filtering
204 * @{ */
205 /** Events we won't permit anyone to filter out. */
206 uint32_t fFixedEvents;
207 /** Usage counters for the host events. (Fixed events are not included.) */
208 VBOXGUESTBITUSAGETRACER EventFilterTracker;
209 /** The event filter last reported to the host (UINT32_MAX on failure). */
210 uint32_t fEventFilterHost;
211 /** @} */
212
213 /** @name Mouse Status
214 * @{ */
215 /** Usage counters for the mouse statuses (VMMDEV_MOUSE_XXX). */
216 VBOXGUESTBITUSAGETRACER MouseStatusTracker;
217 /** The mouse status last reported to the host (UINT32_MAX on failure). */
218 uint32_t fMouseStatusHost;
219 /** @} */
220
221 /** @name Guest Capabilities
222 * @{ */
223 /** Guest capabilities which have been set to "acquire" mode. This means
224 * that only one session can use them at a time, and that they will be
225 * automatically cleaned up if that session exits without doing so.
226 *
227 * Protected by VBOXGUESTDEVEXT::SessionSpinlock, but is unfortunately read
228 * without holding the lock in a couple of places. */
229 uint32_t volatile fAcquireModeGuestCaps;
230 /** Guest capabilities which have been set to "set" mode. This just means
231 * that they have been blocked from ever being set to "acquire" mode. */
232 uint32_t fSetModeGuestCaps;
233 /** Mask of all capabilities which are currently acquired by some session
234 * and as such reported to the host. */
235 uint32_t fAcquiredGuestCaps;
236 /** Usage counters for guest capabilities in "set" mode. Indexed by
237 * capability bit number, one count per session using a capability. */
238 VBOXGUESTBITUSAGETRACER SetGuestCapsTracker;
239 /** The guest capabilities last reported to the host (UINT32_MAX on failure). */
240 uint32_t fGuestCapsHost;
241 /** @} */
242
243 /** Heartbeat timer which fires with interval
244 * cNsHearbeatInterval and its handler sends
245 * VMMDevReq_GuestHeartbeat to VMMDev. */
246 PRTTIMER pHeartbeatTimer;
247 /** Heartbeat timer interval in nanoseconds. */
248 uint64_t cNsHeartbeatInterval;
249 /** Preallocated VMMDevReq_GuestHeartbeat request. */
250 VMMDevRequestHeader *pReqGuestHeartbeat;
251} VBOXGUESTDEVEXT;
252/** Pointer to the VBoxGuest driver data. */
253typedef VBOXGUESTDEVEXT *PVBOXGUESTDEVEXT;
254
255/** @name VBOXGUESTDEVEXT_INIT_STATE_XXX - magic values for validating init
256 * state of the device extension structur.
257 * @{ */
258#define VBOXGUESTDEVEXT_INIT_STATE_FUNDAMENT UINT32_C(0x0badcafe)
259#define VBOXGUESTDEVEXT_INIT_STATE_RESOURCES UINT32_C(0xcafebabe)
260#define VBOXGUESTDEVEXT_INIT_STATE_DELETED UINT32_C(0xdeadd0d0)
261/** @} */
262
263/**
264 * The VBoxGuest per session data.
265 */
266typedef struct VBOXGUESTSESSION
267{
268 /** The list node. */
269 RTLISTNODE ListNode;
270#if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD) || defined(RT_OS_OS2) || defined(RT_OS_SOLARIS)
271 /** Pointer to the next session with the same hash. */
272 PVBOXGUESTSESSION pNextHash;
273#endif
274#if defined(RT_OS_OS2)
275 /** The system file number of this session. */
276 uint16_t sfn;
277 uint16_t Alignment; /**< Alignment */
278#endif
279 /** The process (id) of the session.
280 * This is NIL if it's a kernel session. */
281 RTPROCESS Process;
282 /** Which process this session is associated with.
283 * This is NIL if it's a kernel session. */
284 RTR0PROCESS R0Process;
285 /** Pointer to the device extension. */
286 PVBOXGUESTDEVEXT pDevExt;
287
288#ifdef VBOX_WITH_HGCM
289 /** Array containing HGCM client IDs associated with this session.
290 * This will be automatically disconnected when the session is closed. */
291 uint32_t volatile aHGCMClientIds[64];
292#endif
293 /** The last consumed VMMDEV_EVENT_MOUSE_POSITION_CHANGED sequence number.
294 * Used to implement polling. */
295 uint32_t volatile u32MousePosChangedSeq;
296 /** Host events requested by the session.
297 * An event type requested in any guest session will be added to the host
298 * filter. Protected by VBOXGUESTDEVEXT::SessionSpinlock. */
299 uint32_t fEventFilter;
300 /** Guest capabilities held in "acquired" by this session.
301 * Protected by VBOXGUESTDEVEXT::SessionSpinlock, but is unfortunately read
302 * without holding the lock in a couple of places. */
303 uint32_t volatile fAcquiredGuestCaps;
304 /** Guest capabilities in "set" mode for this session.
305 * These accumulated for sessions via VBOXGUESTDEVEXT::acGuestCapsSet and
306 * reported to the host. Protected by VBOXGUESTDEVEXT::SessionSpinlock. */
307 uint32_t fCapabilities;
308 /** Mouse features supported. A feature enabled in any guest session will
309 * be enabled for the host.
310 * @note We invert the VMMDEV_MOUSE_GUEST_NEEDS_HOST_CURSOR feature in this
311 * bitmap. The logic of this is that the real feature is when the host
312 * cursor is not needed, and we tell the host it is not needed if any
313 * session explicitly fails to assert it. Storing it inverted simplifies
314 * the checks.
315 * Use under the VBOXGUESTDEVEXT#SessionSpinlock lock. */
316 uint32_t fMouseStatus;
317#ifdef RT_OS_DARWIN
318 /** Pointer to the associated org_virtualbox_VBoxGuestClient object. */
319 void *pvVBoxGuestClient;
320 /** Whether this session has been opened or not. */
321 bool fOpened;
322#endif
323 /** Whether a CANCEL_ALL_WAITEVENTS is pending. This happens when
324 * CANCEL_ALL_WAITEVENTS is called, but no call to WAITEVENT is in process
325 * in the current session. In that case the next call will be interrupted
326 * at once. */
327 bool volatile fPendingCancelWaitEvents;
328 /** Does this session belong to a root process or a user one? */
329 bool fUserSession;
330} VBOXGUESTSESSION;
331
332RT_C_DECLS_BEGIN
333
334int VGDrvCommonInitDevExt(PVBOXGUESTDEVEXT pDevExt, uint16_t IOPortBase, void *pvMMIOBase, uint32_t cbMMIO,
335 VBOXOSTYPE enmOSType, uint32_t fEvents);
336void VGDrvCommonDeleteDevExt(PVBOXGUESTDEVEXT pDevExt);
337
338int VGDrvCommonInitLoggers(void);
339void VGDrvCommonDestroyLoggers(void);
340int VGDrvCommonInitDevExtFundament(PVBOXGUESTDEVEXT pDevExt);
341void VGDrvCommonDeleteDevExtFundament(PVBOXGUESTDEVEXT pDevExt);
342int VGDrvCommonInitDevExtResources(PVBOXGUESTDEVEXT pDevExt, uint16_t IOPortBase,
343 void *pvMMIOBase, uint32_t cbMMIO, VBOXOSTYPE enmOSType, uint32_t fFixedEvents);
344void VGDrvCommonDeleteDevExtResources(PVBOXGUESTDEVEXT pDevExt);
345int VGDrvCommonReinitDevExtAfterHibernation(PVBOXGUESTDEVEXT pDevExt, VBOXOSTYPE enmOSType);
346
347bool VBDrvCommonIsOptionValueTrue(const char *pszValue);
348void VGDrvCommonProcessOption(PVBOXGUESTDEVEXT pDevExt, const char *pszName, const char *pszValue);
349void VGDrvCommonProcessOptionsFromHost(PVBOXGUESTDEVEXT pDevExt);
350bool VGDrvCommonIsOurIRQ(PVBOXGUESTDEVEXT pDevExt);
351bool VGDrvCommonISR(PVBOXGUESTDEVEXT pDevExt);
352
353#ifdef VBOXGUEST_USE_DEFERRED_WAKE_UP
354void VGDrvCommonWaitDoWakeUps(PVBOXGUESTDEVEXT pDevExt);
355#endif
356
357int VGDrvCommonCreateUserSession(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION *ppSession);
358int VGDrvCommonCreateKernelSession(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION *ppSession);
359void VGDrvCommonCloseSession(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession);
360
361int VGDrvCommonIoCtlFast(uintptr_t iFunction, PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession);
362int VGDrvCommonIoCtl(uintptr_t iFunction, PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession,
363 PVBGLREQHDR pReqHdr, size_t cbReq);
364
365/**
366 * ISR callback for notifying threads polling for mouse events.
367 *
368 * This is called at the end of the ISR, after leaving the event spinlock, if
369 * VMMDEV_EVENT_MOUSE_POSITION_CHANGED was raised by the host.
370 *
371 * @param pDevExt The device extension.
372 */
373void VGDrvNativeISRMousePollEvent(PVBOXGUESTDEVEXT pDevExt);
374
375/**
376 * Hook for handling OS specfic options from the host.
377 *
378 * @returns true if handled, false if not.
379 * @param pDevExt The device extension.
380 * @param pszName The option name.
381 * @param pszValue The option value.
382 */
383bool VGDrvNativeProcessOption(PVBOXGUESTDEVEXT pDevExt, const char *pszName, const char *pszValue);
384
385
386#ifdef VBOX_WITH_DPC_LATENCY_CHECKER
387int VGDrvNtIOCtl_DpcLatencyChecker(void);
388#endif
389
390#ifdef VBOXGUEST_MOUSE_NOTIFY_CAN_PREEMPT
391int VGDrvNativeSetMouseNotifyCallback(PVBOXGUESTDEVEXT pDevExt, PVBGLIOCSETMOUSENOTIFYCALLBACK pNotify);
392#endif
393
394RT_C_DECLS_END
395
396#endif
397
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