VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/SUPDrvInternal.h@ 53442

最後變更 在這個檔案從53442是 53430,由 vboxsync 提交於 10 年 前

VMM/TM: First step in introducing the invariant TM mode.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 35.0 KB
 
1/* $Id: SUPDrvInternal.h 53430 2014-12-03 13:18:41Z vboxsync $ */
2/** @file
3 * VirtualBox Support Driver - Internal header.
4 */
5
6/*
7 * Copyright (C) 2006-2014 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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 ___SUPDrvInternal_h
28#define ___SUPDrvInternal_h
29
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <VBox/cdefs.h>
35#include <VBox/types.h>
36#include <VBox/sup.h>
37
38#include <iprt/assert.h>
39#include <iprt/list.h>
40#include <iprt/memobj.h>
41#include <iprt/time.h>
42#include <iprt/timer.h>
43#include <iprt/string.h>
44#include <iprt/err.h>
45
46#ifdef SUPDRV_AGNOSTIC
47/* do nothing */
48
49#elif defined(RT_OS_WINDOWS)
50 RT_C_DECLS_BEGIN
51# if (_MSC_VER >= 1400) && !defined(VBOX_WITH_PATCHED_DDK)
52# define _InterlockedExchange _InterlockedExchange_StupidDDKVsCompilerCrap
53# define _InterlockedExchangeAdd _InterlockedExchangeAdd_StupidDDKVsCompilerCrap
54# define _InterlockedCompareExchange _InterlockedCompareExchange_StupidDDKVsCompilerCrap
55# define _InterlockedAddLargeStatistic _InterlockedAddLargeStatistic_StupidDDKVsCompilerCrap
56# define _interlockedbittestandset _interlockedbittestandset_StupidDDKVsCompilerCrap
57# define _interlockedbittestandreset _interlockedbittestandreset_StupidDDKVsCompilerCrap
58# define _interlockedbittestandset64 _interlockedbittestandset64_StupidDDKVsCompilerCrap
59# define _interlockedbittestandreset64 _interlockedbittestandreset64_StupidDDKVsCompilerCrap
60# pragma warning(disable : 4163)
61# include <iprt/nt/nt.h>
62# pragma warning(default : 4163)
63# undef _InterlockedExchange
64# undef _InterlockedExchangeAdd
65# undef _InterlockedCompareExchange
66# undef _InterlockedAddLargeStatistic
67# undef _interlockedbittestandset
68# undef _interlockedbittestandreset
69# undef _interlockedbittestandset64
70# undef _interlockedbittestandreset64
71# else
72# include <iprt/nt/nt.h>
73# endif
74# include <memory.h>
75 RT_C_DECLS_END
76
77#elif defined(RT_OS_LINUX)
78# include <linux/version.h>
79# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33)
80# include <generated/autoconf.h>
81# else
82# ifndef AUTOCONF_INCLUDED
83# include <linux/autoconf.h>
84# endif
85# endif
86# if defined(CONFIG_MODVERSIONS) && !defined(MODVERSIONS)
87# define MODVERSIONS
88# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 71)
89# include <linux/modversions.h>
90# endif
91# endif
92# if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 0)
93# undef ALIGN
94# endif
95# ifndef KBUILD_STR
96# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)
97# define KBUILD_STR(s) s
98# else
99# define KBUILD_STR(s) #s
100# endif
101# endif
102# include <linux/string.h>
103# include <linux/spinlock.h>
104# include <linux/slab.h>
105# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
106# include <linux/semaphore.h>
107# else /* older kernels */
108# include <asm/semaphore.h>
109# endif /* older kernels */
110# include <linux/timer.h>
111
112#elif defined(RT_OS_DARWIN)
113# include <libkern/libkern.h>
114# include <iprt/string.h>
115
116#elif defined(RT_OS_OS2)
117
118#elif defined(RT_OS_FREEBSD)
119# define memset libkern_memset /** @todo these are just hacks to get it compiling, check out later. */
120# define memcmp libkern_memcmp
121# define strchr libkern_strchr
122# define strrchr libkern_strrchr
123# define ffsl libkern_ffsl
124# define fls libkern_fls
125# define flsl libkern_flsl
126# include <sys/libkern.h>
127# undef memset
128# undef memcmp
129# undef strchr
130# undef strrchr
131# undef ffs
132# undef ffsl
133# undef fls
134# undef flsl
135# include <iprt/string.h>
136
137#elif defined(RT_OS_SOLARIS)
138# include <sys/cmn_err.h>
139# include <iprt/string.h>
140
141#else
142# error "unsupported OS."
143#endif
144
145#include "SUPDrvIOC.h"
146#include "SUPDrvIDC.h"
147
148
149
150/*******************************************************************************
151* Defined Constants And Macros *
152*******************************************************************************/
153/*
154 * Hardcoded cookies.
155 */
156#define BIRD 0x64726962 /* 'bird' */
157#define BIRD_INV 0x62697264 /* 'drib' */
158
159
160#ifdef RT_OS_WINDOWS
161/** Use a normal mutex for the loader so we remain at the same IRQL after
162 * taking it.
163 * @todo fix the mutex implementation on linux and make this the default. */
164# define SUPDRV_USE_MUTEX_FOR_LDR
165
166/** Use a normal mutex for the GIP so we remain at the same IRQL after
167 * taking it.
168 * @todo fix the mutex implementation on linux and make this the default. */
169# define SUPDRV_USE_MUTEX_FOR_GIP
170#endif
171
172
173/**
174 * OS debug print macro.
175 */
176#define OSDBGPRINT(a) SUPR0Printf a
177
178/** Debug printf macro shared with the ring-3 part. */
179#ifdef DEBUG_bird
180# define SUP_DPRINTF(a) SUPR0Printf a
181#else
182# define SUP_DPRINTF(a) do { } while (0)
183#endif
184
185
186/** @name Context values for the per-session handle tables.
187 * The context value is used to distinguish between the different kinds of
188 * handles, making the handle table API do all the work.
189 * @{ */
190/** Handle context value for single release event handles. */
191#define SUPDRV_HANDLE_CTX_EVENT ((void *)(uintptr_t)(SUPDRVOBJTYPE_SEM_EVENT))
192/** Handle context value for multiple release event handles. */
193#define SUPDRV_HANDLE_CTX_EVENT_MULTI ((void *)(uintptr_t)(SUPDRVOBJTYPE_SEM_EVENT_MULTI))
194/** @} */
195
196
197/**
198 * Validates a session pointer.
199 *
200 * @returns true/false accordingly.
201 * @param pSession The session.
202 */
203#define SUP_IS_SESSION_VALID(pSession) \
204 ( VALID_PTR(pSession) \
205 && pSession->u32Cookie == BIRD_INV)
206
207/**
208 * Validates a device extension pointer.
209 *
210 * @returns true/false accordingly.
211 * @param pDevExt The device extension.
212 */
213#define SUP_IS_DEVEXT_VALID(pDevExt) \
214 ( VALID_PTR(pDevExt)\
215 && pDevExt->u32Cookie == BIRD)
216
217
218/** @def SUPDRV_WITH_MSR_PROBER
219 * Enables the SUP_IOCTL_MSR_PROBER function.
220 * By default, only enabled in DEBUG builds as it's a sensitive feature.
221 */
222#if defined(DEBUG) && !defined(SUPDRV_WITH_MSR_PROBER) && !defined(SUPDRV_WITHOUT_MSR_PROBER)
223# define SUPDRV_WITH_MSR_PROBER
224#endif
225
226/** @def SUPDRV_WITHOUT_MSR_PROBER
227 * Executive overide for disabling the SUP_IOCTL_MSR_PROBER function.
228 */
229#ifdef SUPDRV_WITHOUT_MSR_PROBER
230# undef SUPDRV_WITH_MSR_PROBER
231#endif
232
233#if 0
234/** Use a dedicated kernel thread to service TSC-delta measurement requests.
235 * @todo Test on servers with many CPUs and sockets. */
236#define SUPDRV_USE_TSC_DELTA_THREAD
237#endif
238
239/*******************************************************************************
240* Structures and Typedefs *
241*******************************************************************************/
242/** Pointer to the device extension. */
243typedef struct SUPDRVDEVEXT *PSUPDRVDEVEXT;
244
245#ifdef SUPDRV_USE_TSC_DELTA_THREAD
246/**
247 * TSC-delta measurement thread state machine.
248 */
249typedef enum SUPDRVTSCDELTASTATE
250{
251 /** Uninitialized/invalid value. */
252 kSupDrvTscDeltaState_Invalid = 0,
253 /** The thread is being created. */
254 kSupDrvTscDeltaState_Creating,
255 /** The thread is listening for events. */
256 kSupDrvTscDeltaState_Listening,
257 /** The thread is sleeping before starting a measurement. */
258 kSupDrvTscDeltaState_WaitAndMeasure,
259 /** The thread is currently servicing a measurement request. */
260 kSupDrvTscDeltaState_Measuring,
261 /** The thread is terminating. */
262 kSupDrvTscDeltaState_Terminating,
263 /** The thread is butchered due to an unexpected error. */
264 kSupDrvTscDeltaState_Butchered,
265 /** The thread is destroyed. */
266 kSupDrvTscDeltaState_Destroyed,
267 /** The usual 32-bit blowup hack. */
268 kSupDrvTscDeltaState_32BitHack = 0x7fffffff
269} SUPDRVTSCDELTASTATE;
270#endif
271
272/**
273 * Memory reference types.
274 */
275typedef enum
276{
277 /** Unused entry */
278 MEMREF_TYPE_UNUSED = 0,
279 /** Locked memory (r3 mapping only). */
280 MEMREF_TYPE_LOCKED,
281 /** Continuous memory block (r3 and r0 mapping). */
282 MEMREF_TYPE_CONT,
283 /** Low memory block (r3 and r0 mapping). */
284 MEMREF_TYPE_LOW,
285 /** Memory block (r3 and r0 mapping). */
286 MEMREF_TYPE_MEM,
287 /** Locked memory (r3 mapping only) allocated by the support driver. */
288 MEMREF_TYPE_PAGE,
289 /** Blow the type up to 32-bit and mark the end. */
290 MEMREF_TYPE_32BIT_HACK = 0x7fffffff
291} SUPDRVMEMREFTYPE, *PSUPDRVMEMREFTYPE;
292
293
294/**
295 * Structure used for tracking memory a session
296 * references in one way or another.
297 */
298typedef struct SUPDRVMEMREF
299{
300 /** The memory object handle. */
301 RTR0MEMOBJ MemObj;
302 /** The ring-3 mapping memory object handle. */
303 RTR0MEMOBJ MapObjR3;
304 /** Type of memory. */
305 SUPDRVMEMREFTYPE eType;
306} SUPDRVMEMREF, *PSUPDRVMEMREF;
307
308
309/**
310 * Bundle of locked memory ranges.
311 */
312typedef struct SUPDRVBUNDLE
313{
314 /** Pointer to the next bundle. */
315 struct SUPDRVBUNDLE * volatile pNext;
316 /** Referenced memory. */
317 SUPDRVMEMREF aMem[64];
318 /** Number of entries used. */
319 uint32_t volatile cUsed;
320} SUPDRVBUNDLE, *PSUPDRVBUNDLE;
321
322
323/**
324 * Loaded image.
325 */
326typedef struct SUPDRVLDRIMAGE
327{
328 /** Next in chain. */
329 struct SUPDRVLDRIMAGE * volatile pNext;
330 /** Pointer to the image. */
331 void *pvImage;
332 /** Pointer to the allocated image buffer.
333 * pvImage is 32-byte aligned or it may governed by the native loader (this
334 * member is NULL then). */
335 void *pvImageAlloc;
336 /** Size of the image including the tables. This is mainly for verification
337 * of the load request. */
338 uint32_t cbImageWithTabs;
339 /** Size of the image. */
340 uint32_t cbImageBits;
341 /** The number of entries in the symbol table. */
342 uint32_t cSymbols;
343 /** Pointer to the symbol table. */
344 PSUPLDRSYM paSymbols;
345 /** The offset of the string table. */
346 char *pachStrTab;
347 /** Size of the string table. */
348 uint32_t cbStrTab;
349 /** Pointer to the optional module initialization callback. */
350 PFNR0MODULEINIT pfnModuleInit;
351 /** Pointer to the optional module termination callback. */
352 PFNR0MODULETERM pfnModuleTerm;
353 /** Service request handler. This is NULL for non-service modules. */
354 PFNSUPR0SERVICEREQHANDLER pfnServiceReqHandler;
355 /** The ldr image state. (IOCtl code of last operation.) */
356 uint32_t uState;
357 /** Usage count. */
358 uint32_t volatile cUsage;
359 /** Pointer to the device extension. */
360 struct SUPDRVDEVEXT *pDevExt;
361#ifdef RT_OS_WINDOWS
362 /** The section object for the loaded image (fNative=true). */
363 void *pvNtSectionObj;
364 /** Lock object. */
365 RTR0MEMOBJ hMemLock;
366#endif
367#if defined(RT_OS_SOLARIS) && defined(VBOX_WITH_NATIVE_SOLARIS_LOADING)
368 /** The Solaris module ID. */
369 int idSolMod;
370 /** Pointer to the module control structure. */
371 struct modctl *pSolModCtl;
372#endif
373 /** Whether it's loaded by the native loader or not. */
374 bool fNative;
375 /** Image name. */
376 char szName[32];
377} SUPDRVLDRIMAGE, *PSUPDRVLDRIMAGE;
378
379
380/** Image usage record. */
381typedef struct SUPDRVLDRUSAGE
382{
383 /** Next in chain. */
384 struct SUPDRVLDRUSAGE * volatile pNext;
385 /** The image. */
386 PSUPDRVLDRIMAGE pImage;
387 /** Load count. */
388 uint32_t volatile cUsage;
389} SUPDRVLDRUSAGE, *PSUPDRVLDRUSAGE;
390
391
392/**
393 * Component factory registration record.
394 */
395typedef struct SUPDRVFACTORYREG
396{
397 /** Pointer to the next registration. */
398 struct SUPDRVFACTORYREG *pNext;
399 /** Pointer to the registered factory. */
400 PCSUPDRVFACTORY pFactory;
401 /** The session owning the factory.
402 * Used for deregistration and session cleanup. */
403 PSUPDRVSESSION pSession;
404 /** Length of the name. */
405 size_t cchName;
406} SUPDRVFACTORYREG;
407/** Pointer to a component factory registration record. */
408typedef SUPDRVFACTORYREG *PSUPDRVFACTORYREG;
409/** Pointer to a const component factory registration record. */
410typedef SUPDRVFACTORYREG const *PCSUPDRVFACTORYREG;
411
412
413/**
414 * Registered object.
415 * This takes care of reference counting and tracking data for access checks.
416 */
417typedef struct SUPDRVOBJ
418{
419 /** Magic value (SUPDRVOBJ_MAGIC). */
420 uint32_t u32Magic;
421 /** The object type. */
422 SUPDRVOBJTYPE enmType;
423 /** Pointer to the next in the global list. */
424 struct SUPDRVOBJ * volatile pNext;
425 /** Pointer to the object destructor.
426 * This may be set to NULL if the image containing the destructor get unloaded. */
427 PFNSUPDRVDESTRUCTOR pfnDestructor;
428 /** User argument 1. */
429 void *pvUser1;
430 /** User argument 2. */
431 void *pvUser2;
432 /** The total sum of all per-session usage. */
433 uint32_t volatile cUsage;
434 /** The creator user id. */
435 RTUID CreatorUid;
436 /** The creator group id. */
437 RTGID CreatorGid;
438 /** The creator process id. */
439 RTPROCESS CreatorProcess;
440} SUPDRVOBJ, *PSUPDRVOBJ;
441
442/** Magic number for SUPDRVOBJ::u32Magic. (Dame Agatha Mary Clarissa Christie). */
443#define SUPDRVOBJ_MAGIC UINT32_C(0x18900915)
444/** Dead number magic for SUPDRVOBJ::u32Magic. */
445#define SUPDRVOBJ_MAGIC_DEAD UINT32_C(0x19760112)
446
447/**
448 * The per-session object usage record.
449 */
450typedef struct SUPDRVUSAGE
451{
452 /** Pointer to the next in the list. */
453 struct SUPDRVUSAGE * volatile pNext;
454 /** Pointer to the object we're recording usage for. */
455 PSUPDRVOBJ pObj;
456 /** The usage count. */
457 uint32_t volatile cUsage;
458} SUPDRVUSAGE, *PSUPDRVUSAGE;
459
460
461/**
462 * Per session data.
463 * This is mainly for memory tracking.
464 */
465typedef struct SUPDRVSESSION
466{
467 /** Pointer to the device extension. */
468 PSUPDRVDEVEXT pDevExt;
469 /** Session Cookie. */
470 uint32_t u32Cookie;
471 /** Set if is an unrestricted session, clear if restricted. */
472 bool fUnrestricted;
473
474 /** Set if we're in the hash table, clear if not. Protected by the hash
475 * table spinlock. */
476 bool fInHashTable;
477 /** Reference counter. */
478 uint32_t volatile cRefs;
479 /** Pointer to the next session with the same hash (common hash table).
480 * Protected by the hash table spinlock. */
481 PSUPDRVSESSION pCommonNextHash;
482 /** Pointer to the OS specific session pointer, if available and in use.
483 * This is atomically set and cleared as the session is inserted and removed
484 * from the hash table (protected by the session hash table spinlock). */
485 PSUPDRVSESSION *ppOsSessionPtr;
486 /** The process (id) of the session. */
487 RTPROCESS Process;
488 /** Which process this session is associated with.
489 * This is NIL_RTR0PROCESS for kernel sessions and valid for user ones. */
490 RTR0PROCESS R0Process;
491
492 /** The VM associated with the session. */
493 PVM pVM;
494 /** Handle table for IPRT semaphore wrapper APIs.
495 * This takes care of its own locking in an IRQ safe manner. */
496 RTHANDLETABLE hHandleTable;
497 /** Load usage records. (protected by SUPDRVDEVEXT::mtxLdr) */
498 PSUPDRVLDRUSAGE volatile pLdrUsage;
499
500 /** Spinlock protecting the bundles, the GIP members and the
501 * fProcessCleanupDone flag. It continues to be valid until the last
502 * reference to the session is released. */
503 RTSPINLOCK Spinlock;
504 /** The ring-3 mapping of the GIP (readonly). */
505 RTR0MEMOBJ GipMapObjR3;
506 /** Set if the session is using the GIP. */
507 uint32_t fGipReferenced;
508 /** Bundle of locked memory objects. */
509 SUPDRVBUNDLE Bundle;
510 /** List of generic usage records. (protected by SUPDRVDEVEXT::SpinLock) */
511 PSUPDRVUSAGE volatile pUsage;
512
513 /** The user id of the session. (Set by the OS part.) */
514 RTUID Uid;
515 /** The group id of the session. (Set by the OS part.) */
516 RTGID Gid;
517 /** Per session tracer specfic data. */
518 uintptr_t uTracerData;
519 /** The thread currently actively talking to the tracer. (One at the time!) */
520 RTNATIVETHREAD hTracerCaller;
521 /** List of tracepoint providers associated with the session
522 * (SUPDRVTPPROVIDER). */
523 RTLISTANCHOR TpProviders;
524 /** The number of providers in TpProviders. */
525 uint32_t cTpProviders;
526 /** The number of threads active in supdrvIOCtl_TracerUmodProbeFire or
527 * SUPR0TracerUmodProbeFire. */
528 uint32_t volatile cTpProbesFiring;
529 /** User tracepoint modules (PSUPDRVTRACKERUMOD). */
530 RTLISTANCHOR TpUmods;
531 /** The user tracepoint module lookup table. */
532 struct SUPDRVTRACERUMOD *apTpLookupTable[32];
533#ifndef SUPDRV_AGNOSTIC
534# if defined(RT_OS_DARWIN)
535 /** Pointer to the associated org_virtualbox_SupDrvClient object. */
536 void *pvSupDrvClient;
537 /** Whether this session has been opened or not. */
538 bool fOpened;
539# endif
540# if defined(RT_OS_OS2)
541 /** The system file number of this session. */
542 uint16_t sfn;
543 uint16_t Alignment; /**< Alignment */
544# endif
545# if defined(RT_OS_DARWIN) || defined(RT_OS_OS2) || defined(RT_OS_SOLARIS)
546 /** Pointer to the next session with the same hash. */
547 PSUPDRVSESSION pNextHash;
548# endif
549# if defined(RT_OS_WINDOWS) && defined(VBOX_WITH_HARDENING)
550 /** Pointer to the process protection structure for this session. */
551 struct SUPDRVNTPROTECT *pNtProtect;
552# endif
553#endif /* !SUPDRV_AGNOSTIC */
554} SUPDRVSESSION;
555
556
557/**
558 * Device extension.
559 */
560typedef struct SUPDRVDEVEXT
561{
562 /** Global cookie. */
563 uint32_t u32Cookie;
564 /** The actual size of SUPDRVSESSION. (SUPDRV_AGNOSTIC) */
565 uint32_t cbSession;
566
567 /** Spinlock to serialize the initialization, usage counting and objects.
568 * This is IRQ safe because we want to be able signal semaphores from the
569 * special HM context (and later maybe interrupt handlers), so we must be able
570 * to reference and dereference handles when IRQs are disabled. */
571 RTSPINLOCK Spinlock;
572
573 /** List of registered objects. Protected by the spinlock. */
574 PSUPDRVOBJ volatile pObjs;
575 /** List of free object usage records. */
576 PSUPDRVUSAGE volatile pUsageFree;
577
578 /** Loader mutex.
579 * This protects pvVMMR0, pvVMMR0Entry, pImages and SUPDRVSESSION::pLdrUsage. */
580#ifdef SUPDRV_USE_MUTEX_FOR_LDR
581 RTSEMMUTEX mtxLdr;
582#else
583 RTSEMFASTMUTEX mtxLdr;
584#endif
585
586 /** VMM Module 'handle'.
587 * 0 if the code VMM isn't loaded and Idt are nops. */
588 void * volatile pvVMMR0;
589 /** VMMR0EntryInt() pointer. */
590 DECLR0CALLBACKMEMBER(int, pfnVMMR0EntryInt, (PVM pVM, unsigned uOperation, void *pvArg));
591 /** VMMR0EntryFast() pointer. */
592 DECLR0CALLBACKMEMBER(void, pfnVMMR0EntryFast, (PVM pVM, VMCPUID idCpu, unsigned uOperation));
593 /** VMMR0EntryEx() pointer. */
594 DECLR0CALLBACKMEMBER(int, pfnVMMR0EntryEx, (PVM pVM, VMCPUID idCpu, unsigned uOperation, PSUPVMMR0REQHDR pReq, uint64_t u64Arg, PSUPDRVSESSION pSession));
595
596 /** Linked list of loaded code. */
597 PSUPDRVLDRIMAGE volatile pLdrImages;
598
599 /** @name These members for detecting whether an API caller is in ModuleInit.
600 * Certain APIs are only permitted from ModuleInit, like for instance tracepoint
601 * registration.
602 * @{ */
603 /** The image currently executing its ModuleInit. */
604 PSUPDRVLDRIMAGE volatile pLdrInitImage;
605 /** The thread currently executing a ModuleInit function. */
606 RTNATIVETHREAD volatile hLdrInitThread;
607 /** @} */
608
609
610 /** GIP mutex.
611 * Any changes to any of the GIP members requires ownership of this mutex,
612 * except on driver init and termination. */
613#ifdef SUPDRV_USE_MUTEX_FOR_GIP
614 RTSEMMUTEX mtxGip;
615#else
616 RTSEMFASTMUTEX mtxGip;
617#endif
618 /** GIP spinlock protecting GIP members during Mp events.
619 * This is IRQ safe since be may get MP callbacks in contexts where IRQs are
620 * disabled (on some platforms). */
621 RTSPINLOCK hGipSpinlock;
622 /** Pointer to the Global Info Page (GIP). */
623 PSUPGLOBALINFOPAGE pGip;
624 /** The physical address of the GIP. */
625 RTHCPHYS HCPhysGip;
626 /** Number of processes using the GIP.
627 * (The updates are suspend while cGipUsers is 0.)*/
628 uint32_t volatile cGipUsers;
629 /** The ring-0 memory object handle for the GIP page. */
630 RTR0MEMOBJ GipMemObj;
631 /** The GIP timer handle. */
632 PRTTIMER pGipTimer;
633 /** If non-zero we've successfully called RTTimerRequestSystemGranularity(). */
634 uint32_t u32SystemTimerGranularityGrant;
635 /** The CPU id of the GIP master.
636 * This CPU is responsible for the updating the common GIP data. */
637 RTCPUID volatile idGipMaster;
638
639 /** Component factory mutex.
640 * This protects pComponentFactoryHead and component factory querying. */
641 RTSEMFASTMUTEX mtxComponentFactory;
642 /** The head of the list of registered component factories. */
643 PSUPDRVFACTORYREG pComponentFactoryHead;
644
645 /** Lock protecting The tracer members. */
646 RTSEMFASTMUTEX mtxTracer;
647 /** List of tracer providers (SUPDRVTPPROVIDER). */
648 RTLISTANCHOR TracerProviderList;
649 /** List of zombie tracer providers (SUPDRVTPPROVIDER). */
650 RTLISTANCHOR TracerProviderZombieList;
651 /** Pointer to the tracer registration record. */
652 PCSUPDRVTRACERREG pTracerOps;
653 /** The ring-0 session of a native tracer provider. */
654 PSUPDRVSESSION pTracerSession;
655 /** The image containing the tracer. */
656 PSUPDRVLDRIMAGE pTracerImage;
657 /** The tracer helpers. */
658 SUPDRVTRACERHLP TracerHlp;
659 /** The number of session having opened the tracer currently. */
660 uint32_t cTracerOpens;
661 /** The number of threads currently calling into the tracer. */
662 uint32_t volatile cTracerCallers;
663 /** Set if the tracer is being unloaded. */
664 bool fTracerUnloading;
665 /** Hash table for user tracer modules (SUPDRVVTGCOPY). */
666 RTLISTANCHOR aTrackerUmodHash[128];
667
668 /** @name Session Handle Table.
669 * @{ */
670 /** Spinlock protecting apSessionHashTab, cSessions,
671 * SUPDRVSESSION::ppOsSessionPtr, SUPDRVSESSION::pCommonNextHash, and possibly
672 * others depending on the OS. */
673 RTSPINLOCK hSessionHashTabSpinlock;
674 /** Session hash table hash table. The size of this table must make sense in
675 * comparison to GVMM_MAX_HANDLES. */
676 PSUPDRVSESSION apSessionHashTab[HC_ARCH_BITS == 64 ? 8191 : 127];
677 /** The number of open sessions. */
678 int32_t cSessions;
679 /** @} */
680
681#ifdef SUPDRV_USE_TSC_DELTA_THREAD
682 /** @name TSC-delta measurement.
683 * @{ */
684 /** Spinlock protecting enmTscDeltaState. */
685 RTSPINLOCK hTscDeltaSpinlock;
686 /** TSC-delta measurement thread. */
687 RTTHREAD hTscDeltaThread;
688 /** The event signalled during state changes to the TSC-delta thread. */
689 RTSEMEVENT hTscDeltaEvent;
690 /** The state of the TSC-delta measurement thread. */
691 SUPDRVTSCDELTASTATE enmTscDeltaState;
692 /** Thread timeout time before rechecking state in ms. */
693 RTMSINTERVAL cMsTscDeltaTimeout;
694 /** The set of CPUs we need to take measurements for. */
695 RTCPUSET TscDeltaCpuSet;
696 /** The set of CPUs we have completed taken measurements for. */
697 RTCPUSET TscDeltaObtainedCpuSet;
698 /** Whether the TSC-delta measurement was successful. */
699 int rcTscDelta;
700 /** @} */
701#endif
702
703 /*
704 * Note! The non-agnostic bits must be at the very end of the structure!
705 */
706#ifndef SUPDRV_AGNOSTIC
707# ifdef RT_OS_WINDOWS
708 /** Callback object returned by ExCreateCallback. */
709 PCALLBACK_OBJECT pObjPowerCallback;
710 /** Callback handle returned by ExRegisterCallback. */
711 PVOID hPowerCallback;
712# endif
713#endif
714} SUPDRVDEVEXT;
715
716/** Calculates the index into g_apSessionHashTab.*/
717#define SUPDRV_SESSION_HASH(a_pid) ( (a_pid) % RT_ELEMENTS(((SUPDRVDEVEXT *)NULL)->apSessionHashTab) )
718
719
720RT_C_DECLS_BEGIN
721
722/*******************************************************************************
723* OS Specific Functions *
724*******************************************************************************/
725/**
726 * Called to clean up the session structure before it's freed.
727 *
728 * @param pDevExt The device globals.
729 * @param pSession The session that's being cleaned up.
730 */
731void VBOXCALL supdrvOSCleanupSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession);
732
733/**
734 * Called to let the OS specfic code perform additional insertion work while
735 * still under the protection of the hash table spinlock.
736 *
737 * @param pDevExt The device globals.
738 * @param pSession The session that was inserted.
739 * @param pvUser User context specified to the insert call.
740 */
741void VBOXCALL supdrvOSSessionHashTabInserted(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, void *pvUser);
742
743/**
744 * Called to let the OS specfic code perform additional removal work while still
745 * under the protection of the hash table spinlock.
746 *
747 * @param pDevExt The device globals.
748 * @param pSession The session that was removed.
749 * @param pvUser User context specified to the remove call.
750 */
751void VBOXCALL supdrvOSSessionHashTabRemoved(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, void *pvUser);
752
753void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession);
754bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc);
755bool VBOXCALL supdrvOSGetForcedAsyncTscMode(PSUPDRVDEVEXT pDevExt);
756bool VBOXCALL supdrvOSAreTscDeltasInSync(void);
757int VBOXCALL supdrvOSEnableVTx(bool fEnabled);
758bool VBOXCALL supdrvOSSuspendVTxOnCpu(void);
759void VBOXCALL supdrvOSResumeVTxOnCpu(bool fSuspended);
760
761/**
762 * Try open the image using the native loader.
763 *
764 * @returns IPRT status code.
765 * @retval VERR_NOT_SUPPORTED if native loading isn't supported.
766 *
767 * @param pDevExt The device globals.
768 * @param pImage The image handle. pvImage should be set on
769 * success, pvImageAlloc can also be set if
770 * appropriate.
771 * @param pszFilename The file name - UTF-8, may containing UNIX
772 * slashes on non-UNIX systems.
773 */
774int VBOXCALL supdrvOSLdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const char *pszFilename);
775
776/**
777 * Notification call indicating that a image is being opened for the first time.
778 *
779 * Can be used to log the load address of the image.
780 *
781 * @param pDevExt The device globals.
782 * @param pImage The image handle.
783 */
784void VBOXCALL supdrvOSLdrNotifyOpened(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage);
785
786/**
787 * Validates an entry point address.
788 *
789 * Called before supdrvOSLdrLoad.
790 *
791 * @returns IPRT status code.
792 * @param pDevExt The device globals.
793 * @param pImage The image data (still in the open state).
794 * @param pv The address within the image.
795 * @param pbImageBits The image bits as loaded by ring-3.
796 */
797int VBOXCALL supdrvOSLdrValidatePointer(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage,
798 void *pv, const uint8_t *pbImageBits);
799
800/**
801 * Load the image.
802 *
803 * @returns IPRT status code.
804 * @param pDevExt The device globals.
805 * @param pImage The image data (up to date). Adjust entrypoints
806 * and exports if necessary.
807 * @param pbImageBits The image bits as loaded by ring-3.
808 * @param pReq Pointer to the request packet so that the VMMR0
809 * entry points can be adjusted.
810 */
811int VBOXCALL supdrvOSLdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const uint8_t *pbImageBits, PSUPLDRLOAD pReq);
812
813
814/**
815 * Unload the image.
816 *
817 * @param pDevExt The device globals.
818 * @param pImage The image data (mostly still valid).
819 */
820void VBOXCALL supdrvOSLdrUnload(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage);
821
822
823#ifdef SUPDRV_WITH_MSR_PROBER
824
825/**
826 * Tries to read an MSR.
827 *
828 * @returns One of the listed VBox status codes.
829 * @retval VINF_SUCCESS if read successfully, value in *puValue.
830 * @retval VERR_ACCESS_DENIED if we couldn't read it (GP).
831 * @retval VERR_NOT_SUPPORTED if not supported.
832 *
833 * @param uMsr The MSR to read from.
834 * @param idCpu The CPU to read the MSR on. NIL_RTCPUID
835 * indicates any suitable CPU.
836 * @param puValue Where to return the value.
837 */
838int VBOXCALL supdrvOSMsrProberRead(uint32_t uMsr, RTCPUID idCpu, uint64_t *puValue);
839
840/**
841 * Tries to write an MSR.
842 *
843 * @returns One of the listed VBox status codes.
844 * @retval VINF_SUCCESS if written successfully.
845 * @retval VERR_ACCESS_DENIED if we couldn't write the value to it (GP).
846 * @retval VERR_NOT_SUPPORTED if not supported.
847 *
848 * @param uMsr The MSR to write to.
849 * @param idCpu The CPU to write the MSR on. NIL_RTCPUID
850 * indicates any suitable CPU.
851 * @param uValue The value to write.
852 */
853int VBOXCALL supdrvOSMsrProberWrite(uint32_t uMsr, RTCPUID idCpu, uint64_t uValue);
854
855/**
856 * Tries to modify an MSR value.
857 *
858 * @returns One of the listed VBox status codes.
859 * @retval VINF_SUCCESS if succeeded.
860 * @retval VERR_NOT_SUPPORTED if not supported.
861 *
862 * @param idCpu The CPU to modify the MSR on. NIL_RTCPUID
863 * indicates any suitable CPU.
864 * @param pReq The request packet with input arguments and
865 * where to store the results.
866 */
867int VBOXCALL supdrvOSMsrProberModify(RTCPUID idCpu, PSUPMSRPROBER pReq);
868
869#endif /* SUPDRV_WITH_MSR_PROBER */
870
871#if defined(RT_OS_DARWIN)
872int VBOXCALL supdrvDarwinResumeSuspendedKbds(void);
873#endif
874
875/*******************************************************************************
876* Shared Functions *
877*******************************************************************************/
878/* SUPDrv.c */
879int VBOXCALL supdrvIOCtl(uintptr_t uIOCtl, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPREQHDR pReqHdr, size_t cbReq);
880int VBOXCALL supdrvIOCtlFast(uintptr_t uIOCtl, VMCPUID idCpu, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession);
881int VBOXCALL supdrvIDC(uintptr_t uIOCtl, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVIDCREQHDR pReqHdr);
882int VBOXCALL supdrvInitDevExt(PSUPDRVDEVEXT pDevExt, size_t cbSession);
883void VBOXCALL supdrvDeleteDevExt(PSUPDRVDEVEXT pDevExt);
884int VBOXCALL supdrvCreateSession(PSUPDRVDEVEXT pDevExt, bool fUser, bool fUnrestricted, PSUPDRVSESSION *ppSession);
885int VBOXCALL supdrvSessionHashTabInsert(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVSESSION *ppOsSessionPtr, void *pvUser);
886int VBOXCALL supdrvSessionHashTabRemove(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, void *pvUser);
887PSUPDRVSESSION VBOXCALL supdrvSessionHashTabLookup(PSUPDRVDEVEXT pDevExt, RTPROCESS Process, RTR0PROCESS R0Process,
888 PSUPDRVSESSION *ppOsSessionPtr);
889uint32_t VBOXCALL supdrvSessionRetain(PSUPDRVSESSION pSession);
890uint32_t VBOXCALL supdrvSessionRelease(PSUPDRVSESSION pSession);
891
892int VBOXCALL supdrvTracerInit(PSUPDRVDEVEXT pDevExt);
893void VBOXCALL supdrvTracerTerm(PSUPDRVDEVEXT pDevExt);
894void VBOXCALL supdrvTracerModuleUnloading(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage);
895void VBOXCALL supdrvTracerCleanupSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession);
896int VBOXCALL supdrvIOCtl_TracerUmodRegister(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession,
897 RTR3PTR R3PtrVtgHdr, RTUINTPTR uVtgHdrAddr,
898 RTR3PTR R3PtrStrTab, uint32_t cbStrTab,
899 const char *pszModName, uint32_t fFlags);
900int VBOXCALL supdrvIOCtl_TracerUmodDeregister(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, RTR3PTR R3PtrVtgHdr);
901void VBOXCALL supdrvIOCtl_TracerUmodProbeFire(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVTRACERUSRCTX pCtx);
902int VBOXCALL supdrvIOCtl_TracerOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, uint32_t uCookie, uintptr_t uArg);
903int VBOXCALL supdrvIOCtl_TracerClose(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession);
904int VBOXCALL supdrvIOCtl_TracerIOCtl(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, uintptr_t uCmd, uintptr_t uArg, int32_t *piRetVal);
905extern PFNRT g_pfnSupdrvProbeFireKernel;
906DECLASM(void) supdrvTracerProbeFireStub(void);
907
908#ifdef VBOX_WITH_NATIVE_DTRACE
909const SUPDRVTRACERREG * VBOXCALL supdrvDTraceInit(void);
910#endif
911
912RT_C_DECLS_END
913
914#endif
915
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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