VirtualBox

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

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

HostDrivers/Support, VMM: bugref:8864: On Linux 4.12 the GDT is mapped read-only. The writable-mapped GDT is available and is used for clearing the TSS BUSY descriptor bit and for LTR.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use