VirtualBox

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

Last change on this file since 75049 was 75049, checked in by vboxsync, 6 years ago

IPRT,SUPDrv: Added cbImage parameter to FNRTLDRRDRMEMDTOR; sketched the basics for darwin module verification. bugref:9232

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