VirtualBox

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

Last change on this file since 72352 was 70873, checked in by vboxsync, 7 years ago

VMMDev,VBoxGuest: Classify who is calling the host (part 1). bugref:9105

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.5 KB
Line 
1/* $Id: VBoxGuestInternal.h 70873 2018-02-05 18:13:55Z 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 requestor information to pass to the host for this session.
280 * @sa VMMDevRequestHeader::fRequestor */
281 uint32_t fRequestor;
282 /** The process (id) of the session.
283 * This is NIL if it's a kernel session. */
284 RTPROCESS Process;
285 /** Which process this session is associated with.
286 * This is NIL if it's a kernel session. */
287 RTR0PROCESS R0Process;
288 /** Pointer to the device extension. */
289 PVBOXGUESTDEVEXT pDevExt;
290
291#ifdef VBOX_WITH_HGCM
292 /** Array containing HGCM client IDs associated with this session.
293 * This will be automatically disconnected when the session is closed. */
294 uint32_t volatile aHGCMClientIds[64];
295#endif
296 /** The last consumed VMMDEV_EVENT_MOUSE_POSITION_CHANGED sequence number.
297 * Used to implement polling. */
298 uint32_t volatile u32MousePosChangedSeq;
299 /** Host events requested by the session.
300 * An event type requested in any guest session will be added to the host
301 * filter. Protected by VBOXGUESTDEVEXT::SessionSpinlock. */
302 uint32_t fEventFilter;
303 /** Guest capabilities held in "acquired" by this session.
304 * Protected by VBOXGUESTDEVEXT::SessionSpinlock, but is unfortunately read
305 * without holding the lock in a couple of places. */
306 uint32_t volatile fAcquiredGuestCaps;
307 /** Guest capabilities in "set" mode for this session.
308 * These accumulated for sessions via VBOXGUESTDEVEXT::acGuestCapsSet and
309 * reported to the host. Protected by VBOXGUESTDEVEXT::SessionSpinlock. */
310 uint32_t fCapabilities;
311 /** Mouse features supported. A feature enabled in any guest session will
312 * be enabled for the host.
313 * @note We invert the VMMDEV_MOUSE_GUEST_NEEDS_HOST_CURSOR feature in this
314 * bitmap. The logic of this is that the real feature is when the host
315 * cursor is not needed, and we tell the host it is not needed if any
316 * session explicitly fails to assert it. Storing it inverted simplifies
317 * the checks.
318 * Use under the VBOXGUESTDEVEXT#SessionSpinlock lock. */
319 uint32_t fMouseStatus;
320#ifdef RT_OS_DARWIN
321 /** Pointer to the associated org_virtualbox_VBoxGuestClient object. */
322 void *pvVBoxGuestClient;
323 /** Whether this session has been opened or not. */
324 bool fOpened;
325#endif
326 /** Whether a CANCEL_ALL_WAITEVENTS is pending. This happens when
327 * CANCEL_ALL_WAITEVENTS is called, but no call to WAITEVENT is in process
328 * in the current session. In that case the next call will be interrupted
329 * at once. */
330 bool volatile fPendingCancelWaitEvents;
331 /** Does this session belong to a root process or a user one? */
332 bool fUserSession;
333} VBOXGUESTSESSION;
334
335RT_C_DECLS_BEGIN
336
337int VGDrvCommonInitDevExt(PVBOXGUESTDEVEXT pDevExt, uint16_t IOPortBase, void *pvMMIOBase, uint32_t cbMMIO,
338 VBOXOSTYPE enmOSType, uint32_t fEvents);
339void VGDrvCommonDeleteDevExt(PVBOXGUESTDEVEXT pDevExt);
340
341int VGDrvCommonInitLoggers(void);
342void VGDrvCommonDestroyLoggers(void);
343int VGDrvCommonInitDevExtFundament(PVBOXGUESTDEVEXT pDevExt);
344void VGDrvCommonDeleteDevExtFundament(PVBOXGUESTDEVEXT pDevExt);
345int VGDrvCommonInitDevExtResources(PVBOXGUESTDEVEXT pDevExt, uint16_t IOPortBase,
346 void *pvMMIOBase, uint32_t cbMMIO, VBOXOSTYPE enmOSType, uint32_t fFixedEvents);
347void VGDrvCommonDeleteDevExtResources(PVBOXGUESTDEVEXT pDevExt);
348int VGDrvCommonReinitDevExtAfterHibernation(PVBOXGUESTDEVEXT pDevExt, VBOXOSTYPE enmOSType);
349
350bool VBDrvCommonIsOptionValueTrue(const char *pszValue);
351void VGDrvCommonProcessOption(PVBOXGUESTDEVEXT pDevExt, const char *pszName, const char *pszValue);
352void VGDrvCommonProcessOptionsFromHost(PVBOXGUESTDEVEXT pDevExt);
353bool VGDrvCommonIsOurIRQ(PVBOXGUESTDEVEXT pDevExt);
354bool VGDrvCommonISR(PVBOXGUESTDEVEXT pDevExt);
355
356#ifdef VBOXGUEST_USE_DEFERRED_WAKE_UP
357void VGDrvCommonWaitDoWakeUps(PVBOXGUESTDEVEXT pDevExt);
358#endif
359
360int VGDrvCommonCreateUserSession(PVBOXGUESTDEVEXT pDevExt, uint32_t fRequestor, PVBOXGUESTSESSION *ppSession);
361int VGDrvCommonCreateKernelSession(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION *ppSession);
362void VGDrvCommonCloseSession(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession);
363
364int VGDrvCommonIoCtlFast(uintptr_t iFunction, PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession);
365int VGDrvCommonIoCtl(uintptr_t iFunction, PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession,
366 PVBGLREQHDR pReqHdr, size_t cbReq);
367
368/**
369 * ISR callback for notifying threads polling for mouse events.
370 *
371 * This is called at the end of the ISR, after leaving the event spinlock, if
372 * VMMDEV_EVENT_MOUSE_POSITION_CHANGED was raised by the host.
373 *
374 * @param pDevExt The device extension.
375 */
376void VGDrvNativeISRMousePollEvent(PVBOXGUESTDEVEXT pDevExt);
377
378/**
379 * Hook for handling OS specfic options from the host.
380 *
381 * @returns true if handled, false if not.
382 * @param pDevExt The device extension.
383 * @param pszName The option name.
384 * @param pszValue The option value.
385 */
386bool VGDrvNativeProcessOption(PVBOXGUESTDEVEXT pDevExt, const char *pszName, const char *pszValue);
387
388
389#ifdef VBOX_WITH_DPC_LATENCY_CHECKER
390int VGDrvNtIOCtl_DpcLatencyChecker(void);
391#endif
392
393#ifdef VBOXGUEST_MOUSE_NOTIFY_CAN_PREEMPT
394int VGDrvNativeSetMouseNotifyCallback(PVBOXGUESTDEVEXT pDevExt, PVBGLIOCSETMOUSENOTIFYCALLBACK pNotify);
395#endif
396
397RT_C_DECLS_END
398
399#endif
400
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