VirtualBox

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

Last change on this file since 63549 was 62664, checked in by vboxsync, 8 years ago

SUPDrv: warnings

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