VirtualBox

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

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

backed out r118835 as it incorrectly updated the 'This file is based on' file headers.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 40.4 KB
Line 
1/* $Id: SUPDrvInternal.h 69498 2017-10-28 15:07:25Z vboxsync $ */
2/** @file
3 * VirtualBox Support Driver - Internal header.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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 * Per session data.
461 * This is mainly for memory tracking.
462 */
463typedef struct SUPDRVSESSION
464{
465 /** Pointer to the device extension. */
466 PSUPDRVDEVEXT pDevExt;
467 /** Session Cookie. */
468 uint32_t u32Cookie;
469 /** Set if is an unrestricted session, clear if restricted. */
470 bool fUnrestricted;
471
472 /** Set if we're in the hash table, clear if not. Protected by the hash
473 * table spinlock. */
474 bool fInHashTable;
475 /** Reference counter. */
476 uint32_t volatile cRefs;
477 /** Pointer to the next session with the same hash (common hash table).
478 * Protected by the hash table spinlock. */
479 PSUPDRVSESSION pCommonNextHash;
480 /** Pointer to the OS specific session pointer, if available and in use.
481 * This is atomically set and cleared as the session is inserted and removed
482 * from the hash table (protected by the session hash table spinlock). */
483 PSUPDRVSESSION *ppOsSessionPtr;
484 /** The process (id) of the session. */
485 RTPROCESS Process;
486 /** Which process this session is associated with.
487 * This is NIL_RTR0PROCESS for kernel sessions and valid for user ones. */
488 RTR0PROCESS R0Process;
489
490 /** The GVM associated with the session.
491 * This is set by VMMR0. */
492 PGVM pSessionGVM;
493 /** The VM associated with the session.
494 * This is set by VMMR0. */
495 PVM pSessionVM;
496 /** Set to pSessionVM if fast I/O controlls are enabled. */
497 PVM pFastIoCtrlVM;
498 /** Handle table for IPRT semaphore wrapper APIs.
499 * This takes care of its own locking in an IRQ safe manner. */
500 RTHANDLETABLE hHandleTable;
501 /** Load usage records. (protected by SUPDRVDEVEXT::mtxLdr) */
502 PSUPDRVLDRUSAGE volatile pLdrUsage;
503
504 /** Spinlock protecting the bundles, the GIP members and the
505 * fProcessCleanupDone flag. It continues to be valid until the last
506 * reference to the session is released. */
507 RTSPINLOCK Spinlock;
508 /** The ring-3 mapping of the GIP (readonly). */
509 RTR0MEMOBJ GipMapObjR3;
510 /** Set if the session is using the GIP. */
511 uint32_t fGipReferenced;
512 /** Bundle of locked memory objects. */
513 SUPDRVBUNDLE Bundle;
514 /** List of generic usage records. (protected by SUPDRVDEVEXT::SpinLock) */
515 PSUPDRVUSAGE volatile pUsage;
516
517 /** The user id of the session. (Set by the OS part.) */
518 RTUID Uid;
519 /** The group id of the session. (Set by the OS part.) */
520 RTGID Gid;
521 /** Per session tracer specfic data. */
522 uintptr_t uTracerData;
523 /** The thread currently actively talking to the tracer. (One at the time!) */
524 RTNATIVETHREAD hTracerCaller;
525 /** List of tracepoint providers associated with the session
526 * (SUPDRVTPPROVIDER). */
527 RTLISTANCHOR TpProviders;
528 /** The number of providers in TpProviders. */
529 uint32_t cTpProviders;
530 /** The number of threads active in supdrvIOCtl_TracerUmodProbeFire or
531 * SUPR0TracerUmodProbeFire. */
532 uint32_t volatile cTpProbesFiring;
533 /** User tracepoint modules (PSUPDRVTRACKERUMOD). */
534 RTLISTANCHOR TpUmods;
535 /** The user tracepoint module lookup table. */
536 struct SUPDRVTRACERUMOD *apTpLookupTable[32];
537 /** Whether this is a GIP test-mode client session or not. */
538 bool fGipTestMode;
539#ifndef SUPDRV_AGNOSTIC
540# if defined(RT_OS_DARWIN)
541 /** Pointer to the associated org_virtualbox_SupDrvClient object. */
542 void *pvSupDrvClient;
543 /** Whether this session has been opened or not. */
544 bool fOpened;
545# endif
546# if defined(RT_OS_OS2)
547 /** The system file number of this session. */
548 uint16_t sfn;
549 uint16_t Alignment; /**< Alignment */
550# endif
551# if defined(RT_OS_DARWIN) || defined(RT_OS_OS2) || defined(RT_OS_SOLARIS)
552 /** Pointer to the next session with the same hash. */
553 PSUPDRVSESSION pNextHash;
554# endif
555# if defined(RT_OS_WINDOWS) && defined(VBOX_WITH_HARDENING)
556 /** Pointer to the process protection structure for this session. */
557 struct SUPDRVNTPROTECT *pNtProtect;
558# endif
559#endif /* !SUPDRV_AGNOSTIC */
560} SUPDRVSESSION;
561
562
563/**
564 * Device extension.
565 */
566typedef struct SUPDRVDEVEXT
567{
568 /** Global cookie. */
569 uint32_t u32Cookie;
570 /** The actual size of SUPDRVSESSION. (SUPDRV_AGNOSTIC) */
571 uint32_t cbSession;
572
573 /** Spinlock to serialize the initialization, usage counting and objects.
574 * This is IRQ safe because we want to be able signal semaphores from the
575 * special HM context (and later maybe interrupt handlers), so we must be able
576 * to reference and dereference handles when IRQs are disabled. */
577 RTSPINLOCK Spinlock;
578
579 /** List of registered objects. Protected by the spinlock. */
580 PSUPDRVOBJ volatile pObjs;
581 /** List of free object usage records. */
582 PSUPDRVUSAGE volatile pUsageFree;
583
584 /** Loader mutex.
585 * This protects pvVMMR0, pvVMMR0Entry, pImages and SUPDRVSESSION::pLdrUsage. */
586#ifdef SUPDRV_USE_MUTEX_FOR_LDR
587 RTSEMMUTEX mtxLdr;
588#else
589 RTSEMFASTMUTEX mtxLdr;
590#endif
591
592 /** VMM Module 'handle'.
593 * 0 if the code VMM isn't loaded and Idt are nops. */
594 void * volatile pvVMMR0;
595 /** VMMR0EntryFast() pointer. */
596 DECLR0CALLBACKMEMBER(void, pfnVMMR0EntryFast, (PGVM pGVM, PVM pVM, VMCPUID idCpu, uint32_t uOperation));
597 /** VMMR0EntryEx() pointer. */
598 DECLR0CALLBACKMEMBER(int, pfnVMMR0EntryEx, (PGVM pGVM, PVM pVM, VMCPUID idCpu, uint32_t uOperation,
599 PSUPVMMR0REQHDR pReq, uint64_t u64Arg, PSUPDRVSESSION pSession));
600
601 /** Linked list of loaded code. */
602 PSUPDRVLDRIMAGE volatile pLdrImages;
603 /** Set if the image loading interface got disabled after loading all needed images */
604 bool fLdrLockedDown;
605
606 /** @name These members for detecting whether an API caller is in ModuleInit.
607 * Certain APIs are only permitted from ModuleInit, like for instance tracepoint
608 * registration.
609 * @{ */
610 /** The image currently executing its ModuleInit. */
611 PSUPDRVLDRIMAGE volatile pLdrInitImage;
612 /** The thread currently executing a ModuleInit function. */
613 RTNATIVETHREAD volatile hLdrInitThread;
614 /** @} */
615
616 /** Number of times someone reported bad execution context via SUPR0BadContext.
617 * (This is times EFLAGS.AC is zero when we expected it to be 1.) */
618 uint32_t volatile cBadContextCalls;
619
620 /** GIP mutex.
621 * Any changes to any of the GIP members requires ownership of this mutex,
622 * except on driver init and termination. */
623#ifdef SUPDRV_USE_MUTEX_FOR_GIP
624 RTSEMMUTEX mtxGip;
625#else
626 RTSEMFASTMUTEX mtxGip;
627#endif
628 /** GIP spinlock protecting GIP members during Mp events.
629 * This is IRQ safe since be may get MP callbacks in contexts where IRQs are
630 * disabled (on some platforms). */
631 RTSPINLOCK hGipSpinlock;
632 /** Pointer to the Global Info Page (GIP). */
633 PSUPGLOBALINFOPAGE pGip;
634 /** The physical address of the GIP. */
635 RTHCPHYS HCPhysGip;
636 /** Number of processes using the GIP.
637 * (The updates are suspend while cGipUsers is 0.)*/
638 uint32_t volatile cGipUsers;
639 /** The ring-0 memory object handle for the GIP page. */
640 RTR0MEMOBJ GipMemObj;
641 /** The GIP timer handle. */
642 PRTTIMER pGipTimer;
643 /** If non-zero we've successfully called RTTimerRequestSystemGranularity(). */
644 uint32_t u32SystemTimerGranularityGrant;
645 /** The CPU id of the GIP master.
646 * This CPU is responsible for the updating the common GIP data and it is
647 * the one used to calculate TSC deltas relative to.
648 * (The initial master will have a 0 zero value, but it it goes offline the
649 * new master may have a non-zero value.) */
650 RTCPUID volatile idGipMaster;
651
652 /** Component factory mutex.
653 * This protects pComponentFactoryHead and component factory querying. */
654 RTSEMFASTMUTEX mtxComponentFactory;
655 /** The head of the list of registered component factories. */
656 PSUPDRVFACTORYREG pComponentFactoryHead;
657
658 /** Lock protecting The tracer members. */
659 RTSEMFASTMUTEX mtxTracer;
660 /** List of tracer providers (SUPDRVTPPROVIDER). */
661 RTLISTANCHOR TracerProviderList;
662 /** List of zombie tracer providers (SUPDRVTPPROVIDER). */
663 RTLISTANCHOR TracerProviderZombieList;
664 /** Pointer to the tracer registration record. */
665 PCSUPDRVTRACERREG pTracerOps;
666 /** The ring-0 session of a native tracer provider. */
667 PSUPDRVSESSION pTracerSession;
668 /** The image containing the tracer. */
669 PSUPDRVLDRIMAGE pTracerImage;
670 /** The tracer helpers. */
671 SUPDRVTRACERHLP TracerHlp;
672 /** The number of session having opened the tracer currently. */
673 uint32_t cTracerOpens;
674 /** The number of threads currently calling into the tracer. */
675 uint32_t volatile cTracerCallers;
676 /** Set if the tracer is being unloaded. */
677 bool fTracerUnloading;
678 /** Hash table for user tracer modules (SUPDRVVTGCOPY). */
679 RTLISTANCHOR aTrackerUmodHash[128];
680
681 /** @name Session Handle Table.
682 * @{ */
683 /** Spinlock protecting apSessionHashTab, cSessions,
684 * SUPDRVSESSION::ppOsSessionPtr, SUPDRVSESSION::pCommonNextHash, and possibly
685 * others depending on the OS. */
686 RTSPINLOCK hSessionHashTabSpinlock;
687 /** Session hash table hash table. The size of this table must make sense in
688 * comparison to GVMM_MAX_HANDLES. */
689 PSUPDRVSESSION apSessionHashTab[HC_ARCH_BITS == 64 ? 8191 : 127];
690 /** The number of open sessions. */
691 int32_t cSessions;
692 /** @} */
693
694 /** @name Invariant TSC frequency refinement.
695 * @{ */
696 /** Nanosecond timestamp at the start of the TSC frequency refinement phase. */
697 uint64_t nsStartInvarTscRefine;
698 /** TSC reading at the start of the TSC frequency refinement phase. */
699 uint64_t uTscStartInvarTscRefine;
700 /** The CPU id of the CPU that u64TscAnchor was measured on. */
701 RTCPUID idCpuInvarTscRefine;
702 /** Pointer to the timer used to refine the TSC frequency. */
703 PRTTIMER pInvarTscRefineTimer;
704 /** Stop the timer on the next tick because we saw a power event. */
705 bool volatile fInvTscRefinePowerEvent;
706 /** @} */
707
708 /** @name TSC-delta measurement.
709 * @{ */
710 /** Number of online/offline events, incremented each time a CPU goes online
711 * or offline. */
712 uint32_t volatile cMpOnOffEvents;
713 /** TSC-delta measurement mutext.
714 * At the moment, we don't want to have more than one measurement going on at
715 * any one time. We might be using broadcast IPIs which are heavy and could
716 * perhaps get in each others way. */
717#ifdef SUPDRV_USE_MUTEX_FOR_GIP
718 RTSEMMUTEX mtxTscDelta;
719#else
720 RTSEMFASTMUTEX mtxTscDelta;
721#endif
722 /** The set of CPUs we need to take measurements for. */
723 RTCPUSET TscDeltaCpuSet;
724 /** The set of CPUs we have completed taken measurements for. */
725 RTCPUSET TscDeltaObtainedCpuSet;
726 /** @} */
727
728#ifdef SUPDRV_USE_TSC_DELTA_THREAD
729 /** @name TSC-delta measurement thread.
730 * @{ */
731 /** Spinlock protecting enmTscDeltaThreadState. */
732 RTSPINLOCK hTscDeltaSpinlock;
733 /** TSC-delta measurement thread. */
734 RTTHREAD hTscDeltaThread;
735 /** The event signalled during state changes to the TSC-delta thread. */
736 RTSEMEVENT hTscDeltaEvent;
737 /** The state of the TSC-delta measurement thread. */
738 SUPDRVTSCDELTATHREADSTATE enmTscDeltaThreadState;
739 /** Thread timeout time before rechecking state in ms. */
740 RTMSINTERVAL cMsTscDeltaTimeout;
741 /** Whether the TSC-delta measurement was successful. */
742 int32_t volatile rcTscDelta;
743 /** Tell the thread we want TSC-deltas for all CPUs with retries. */
744 bool fTscThreadRecomputeAllDeltas;
745 /** @} */
746#endif
747
748 /** @name GIP test mode.
749 * @{ */
750 /** Reference counter for GIP test-mode sessions. */
751 uint32_t cGipTestModeRefs;
752 /** Cache of TSC frequency before enabling test-mode on invariant GIP systems. */
753 uint64_t uGipTestModeInvariantCpuHz;
754 /** @} */
755
756 /*
757 * Note! The non-agnostic bits must be at the very end of the structure!
758 */
759#ifndef SUPDRV_AGNOSTIC
760# ifdef RT_OS_WINDOWS
761 /** Callback object returned by ExCreateCallback. */
762 PCALLBACK_OBJECT pObjPowerCallback;
763 /** Callback handle returned by ExRegisterCallback. */
764 PVOID hPowerCallback;
765# endif
766#endif
767} SUPDRVDEVEXT;
768
769/** Calculates the index into g_apSessionHashTab.*/
770#define SUPDRV_SESSION_HASH(a_pid) ( (a_pid) % RT_ELEMENTS(((SUPDRVDEVEXT *)NULL)->apSessionHashTab) )
771
772
773RT_C_DECLS_BEGIN
774
775/*******************************************************************************
776* OS Specific Functions *
777*******************************************************************************/
778/**
779 * Called to clean up the session structure before it's freed.
780 *
781 * @param pDevExt The device globals.
782 * @param pSession The session that's being cleaned up.
783 */
784void VBOXCALL supdrvOSCleanupSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession);
785
786/**
787 * Called to let the OS specfic code perform additional insertion work while
788 * still under the protection of the hash table spinlock.
789 *
790 * @param pDevExt The device globals.
791 * @param pSession The session that was inserted.
792 * @param pvUser User context specified to the insert call.
793 */
794void VBOXCALL supdrvOSSessionHashTabInserted(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, void *pvUser);
795
796/**
797 * Called to let the OS specfic code perform additional removal work while still
798 * under the protection of the hash table spinlock.
799 *
800 * @param pDevExt The device globals.
801 * @param pSession The session that was removed.
802 * @param pvUser User context specified to the remove call.
803 */
804void VBOXCALL supdrvOSSessionHashTabRemoved(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, void *pvUser);
805
806/**
807 * Called during GIP initializtion to calc the CPU group table size.
808 *
809 * This is currently only implemented on windows [lazy bird].
810 *
811 * @returns Number of bytes needed for SUPGIPCPUGROUP structures.
812 * @param pDevExt The device globals.
813 */
814size_t VBOXCALL supdrvOSGipGetGroupTableSize(PSUPDRVDEVEXT pDevExt);
815
816/**
817 * Called during GIP initialization to set up the group table and group count.
818 *
819 * This is currently only implemented on windows [lazy bird].
820 *
821 * @param pDevExt The device globals.
822 * @param pGip The GIP which group table needs initialization.
823 * It's only partially initialized at this point.
824 * @param cbGipCpuGroups What supdrvOSGipGetGroupTableSize returned.
825 */
826int VBOXCALL supdrvOSInitGipGroupTable(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE pGip, size_t cbGipCpuGroups);
827
828/**
829 * Initializes the group related members when a CPU is added to the GIP.
830 *
831 * This is called both during GIP initalization and during an CPU online event.
832 *
833 * This is currently only implemented on windows [lazy bird].
834 *
835 * @returns CPU group number.
836 * @param pDevExt The device globals.
837 * @param pGip The GIP.
838 * @param pGipCpu The GIP CPU structure being initialized.
839 */
840void VBOXCALL supdrvOSGipInitGroupBitsForCpu(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE pGip, PSUPGIPCPU pGipCpu);
841
842void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession);
843bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc);
844bool VBOXCALL supdrvOSGetForcedAsyncTscMode(PSUPDRVDEVEXT pDevExt);
845bool VBOXCALL supdrvOSAreCpusOfflinedOnSuspend(void);
846bool VBOXCALL supdrvOSAreTscDeltasInSync(void);
847int VBOXCALL supdrvOSEnableVTx(bool fEnabled);
848RTCCUINTREG VBOXCALL supdrvOSChangeCR4(RTCCUINTREG fOrMask, RTCCUINTREG fAndMask);
849bool VBOXCALL supdrvOSSuspendVTxOnCpu(void);
850void VBOXCALL supdrvOSResumeVTxOnCpu(bool fSuspended);
851int VBOXCALL supdrvOSGetCurrentGdtRw(RTHCUINTPTR *pGdtRw);
852
853/**
854 * Try open the image using the native loader.
855 *
856 * @returns IPRT status code.
857 * @retval VERR_NOT_SUPPORTED if native loading isn't supported.
858 *
859 * @param pDevExt The device globals.
860 * @param pImage The image handle. pvImage should be set on
861 * success, pvImageAlloc can also be set if
862 * appropriate.
863 * @param pszFilename The file name - UTF-8, may containing UNIX
864 * slashes on non-UNIX systems.
865 */
866int VBOXCALL supdrvOSLdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const char *pszFilename);
867
868/**
869 * Notification call indicating that a image is being opened for the first time.
870 *
871 * Called for both native and non-native images (after supdrvOSLdrOpen). Can be
872 * used to log the load address of the image or inform the kernel about the
873 * alien image.
874 *
875 * @param pDevExt The device globals.
876 * @param pImage The image handle.
877 * @param pszFilename The file name - UTF-8, may containing UNIX
878 * slashes on non-UNIX systems.
879 */
880void VBOXCALL supdrvOSLdrNotifyOpened(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const char *pszFilename);
881
882/**
883 * Validates an entry point address.
884 *
885 * Called before supdrvOSLdrLoad.
886 *
887 * @returns IPRT status code.
888 * @param pDevExt The device globals.
889 * @param pImage The image data (still in the open state).
890 * @param pv The address within the image.
891 * @param pbImageBits The image bits as loaded by ring-3.
892 */
893int VBOXCALL supdrvOSLdrValidatePointer(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage,
894 void *pv, const uint8_t *pbImageBits);
895
896/**
897 * Load the image.
898 *
899 * @returns IPRT status code.
900 * @param pDevExt The device globals.
901 * @param pImage The image data (up to date). Adjust entrypoints
902 * and exports if necessary.
903 * @param pbImageBits The image bits as loaded by ring-3.
904 * @param pReq Pointer to the request packet so that the VMMR0
905 * entry points can be adjusted.
906 */
907int VBOXCALL supdrvOSLdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const uint8_t *pbImageBits, PSUPLDRLOAD pReq);
908
909
910/**
911 * Unload the image (only called if supdrvOSLdrOpen returned success).
912 *
913 * @param pDevExt The device globals.
914 * @param pImage The image data (mostly still valid).
915 */
916void VBOXCALL supdrvOSLdrUnload(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage);
917
918/**
919 * Notification call indicating that a image is being unloaded.
920 *
921 * Called for both native and non-native images. In the former case, it's
922 * called after supdrvOSLdrUnload.
923 *
924 * @param pDevExt The device globals.
925 * @param pImage The image handle.
926 */
927void VBOXCALL supdrvOSLdrNotifyUnloaded(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage);
928
929
930#ifdef SUPDRV_WITH_MSR_PROBER
931
932/**
933 * Tries to read an MSR.
934 *
935 * @returns One of the listed VBox status codes.
936 * @retval VINF_SUCCESS if read successfully, value in *puValue.
937 * @retval VERR_ACCESS_DENIED if we couldn't read it (GP).
938 * @retval VERR_NOT_SUPPORTED if not supported.
939 *
940 * @param uMsr The MSR to read from.
941 * @param idCpu The CPU to read the MSR on. NIL_RTCPUID
942 * indicates any suitable CPU.
943 * @param puValue Where to return the value.
944 */
945int VBOXCALL supdrvOSMsrProberRead(uint32_t uMsr, RTCPUID idCpu, uint64_t *puValue);
946
947/**
948 * Tries to write an MSR.
949 *
950 * @returns One of the listed VBox status codes.
951 * @retval VINF_SUCCESS if written successfully.
952 * @retval VERR_ACCESS_DENIED if we couldn't write the value to it (GP).
953 * @retval VERR_NOT_SUPPORTED if not supported.
954 *
955 * @param uMsr The MSR to write to.
956 * @param idCpu The CPU to write the MSR on. NIL_RTCPUID
957 * indicates any suitable CPU.
958 * @param uValue The value to write.
959 */
960int VBOXCALL supdrvOSMsrProberWrite(uint32_t uMsr, RTCPUID idCpu, uint64_t uValue);
961
962/**
963 * Tries to modify an MSR value.
964 *
965 * @returns One of the listed VBox status codes.
966 * @retval VINF_SUCCESS if succeeded.
967 * @retval VERR_NOT_SUPPORTED if not supported.
968 *
969 * @param idCpu The CPU to modify the MSR on. NIL_RTCPUID
970 * indicates any suitable CPU.
971 * @param pReq The request packet with input arguments and
972 * where to store the results.
973 */
974int VBOXCALL supdrvOSMsrProberModify(RTCPUID idCpu, PSUPMSRPROBER pReq);
975
976#endif /* SUPDRV_WITH_MSR_PROBER */
977
978#if defined(RT_OS_DARWIN)
979int VBOXCALL supdrvDarwinResumeSuspendedKbds(void);
980#endif
981
982/*******************************************************************************
983* Shared Functions *
984*******************************************************************************/
985/* SUPDrv.c */
986int VBOXCALL supdrvIOCtl(uintptr_t uIOCtl, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPREQHDR pReqHdr, size_t cbReq);
987int VBOXCALL supdrvIOCtlFast(uintptr_t uIOCtl, VMCPUID idCpu, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession);
988int VBOXCALL supdrvIDC(uintptr_t uIOCtl, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVIDCREQHDR pReqHdr);
989int VBOXCALL supdrvInitDevExt(PSUPDRVDEVEXT pDevExt, size_t cbSession);
990void VBOXCALL supdrvDeleteDevExt(PSUPDRVDEVEXT pDevExt);
991int VBOXCALL supdrvCreateSession(PSUPDRVDEVEXT pDevExt, bool fUser, bool fUnrestricted, PSUPDRVSESSION *ppSession);
992int VBOXCALL supdrvSessionHashTabInsert(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVSESSION *ppOsSessionPtr, void *pvUser);
993int VBOXCALL supdrvSessionHashTabRemove(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, void *pvUser);
994PSUPDRVSESSION VBOXCALL supdrvSessionHashTabLookup(PSUPDRVDEVEXT pDevExt, RTPROCESS Process, RTR0PROCESS R0Process,
995 PSUPDRVSESSION *ppOsSessionPtr);
996uint32_t VBOXCALL supdrvSessionRetain(PSUPDRVSESSION pSession);
997uint32_t VBOXCALL supdrvSessionRelease(PSUPDRVSESSION pSession);
998void VBOXCALL supdrvBadContext(PSUPDRVDEVEXT pDevExt, const char *pszFile, uint32_t uLine, const char *pszExtra);
999int VBOXCALL supdrvQueryVTCapsInternal(uint32_t *pfCaps);
1000int VBOXCALL supdrvLdrLoadError(int rc, PSUPLDRLOAD pReq, const char *pszFormat, ...);
1001
1002/* SUPDrvGip.cpp */
1003int VBOXCALL supdrvGipCreate(PSUPDRVDEVEXT pDevExt);
1004void VBOXCALL supdrvGipDestroy(PSUPDRVDEVEXT pDevExt);
1005int VBOXCALL supdrvIOCtl_TscDeltaMeasure(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPTSCDELTAMEASURE pReq);
1006int VBOXCALL supdrvIOCtl_TscRead(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPTSCREAD pReq);
1007int VBOXCALL supdrvIOCtl_GipSetFlags(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, uint32_t fOrMask, uint32_t fAndMask);
1008
1009
1010/* SUPDrvTracer.cpp */
1011int VBOXCALL supdrvTracerInit(PSUPDRVDEVEXT pDevExt);
1012void VBOXCALL supdrvTracerTerm(PSUPDRVDEVEXT pDevExt);
1013void VBOXCALL supdrvTracerModuleUnloading(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage);
1014void VBOXCALL supdrvTracerCleanupSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession);
1015int VBOXCALL supdrvIOCtl_TracerUmodRegister(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession,
1016 RTR3PTR R3PtrVtgHdr, RTUINTPTR uVtgHdrAddr,
1017 RTR3PTR R3PtrStrTab, uint32_t cbStrTab,
1018 const char *pszModName, uint32_t fFlags);
1019int VBOXCALL supdrvIOCtl_TracerUmodDeregister(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, RTR3PTR R3PtrVtgHdr);
1020void VBOXCALL supdrvIOCtl_TracerUmodProbeFire(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVTRACERUSRCTX pCtx);
1021int VBOXCALL supdrvIOCtl_TracerOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, uint32_t uCookie, uintptr_t uArg);
1022int VBOXCALL supdrvIOCtl_TracerClose(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession);
1023int VBOXCALL supdrvIOCtl_TracerIOCtl(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, uintptr_t uCmd, uintptr_t uArg, int32_t *piRetVal);
1024extern PFNRT g_pfnSupdrvProbeFireKernel;
1025DECLASM(void) supdrvTracerProbeFireStub(void);
1026
1027#ifdef VBOX_WITH_NATIVE_DTRACE
1028const SUPDRVTRACERREG * VBOXCALL supdrvDTraceInit(void);
1029#endif
1030
1031RT_C_DECLS_END
1032
1033#endif
1034
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