VirtualBox

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

Last change on this file since 74125 was 71198, checked in by vboxsync, 7 years ago

SUPDrv,VMMR0: Prepped for extending the fast I/O control interface a bit for NEM; SUPDRV version increment. bugref:9044

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