VirtualBox

source: vbox/trunk/include/VBox/sup.h@ 54214

Last change on this file since 54214 was 54214, checked in by vboxsync, 10 years ago

HostDrivers/Support, testcase, TM: Add fTscDeltasRoughlyInSync.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 78.0 KB
Line 
1/** @file
2 * SUP - Support Library. (HDrv)
3 */
4
5/*
6 * Copyright (C) 2006-2015 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___VBox_sup_h
27#define ___VBox_sup_h
28
29#include <VBox/cdefs.h>
30#include <VBox/types.h>
31#include <VBox/err.h>
32#include <iprt/assert.h>
33#include <iprt/stdarg.h>
34#include <iprt/cpuset.h>
35#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
36# include <iprt/asm-amd64-x86.h>
37#endif
38
39RT_C_DECLS_BEGIN
40
41struct VTGOBJHDR;
42struct VTGPROBELOC;
43
44
45/** @defgroup grp_sup The Support Library API
46 * @{
47 */
48
49/**
50 * Physical page descriptor.
51 */
52#pragma pack(4) /* space is more important. */
53typedef struct SUPPAGE
54{
55 /** Physical memory address. */
56 RTHCPHYS Phys;
57 /** Reserved entry for internal use by the caller. */
58 RTHCUINTPTR uReserved;
59} SUPPAGE;
60#pragma pack()
61/** Pointer to a page descriptor. */
62typedef SUPPAGE *PSUPPAGE;
63/** Pointer to a const page descriptor. */
64typedef const SUPPAGE *PCSUPPAGE;
65
66/**
67 * The paging mode.
68 *
69 * @remarks Users are making assumptions about the order here!
70 */
71typedef enum SUPPAGINGMODE
72{
73 /** The usual invalid entry.
74 * This is returned by SUPR3GetPagingMode() */
75 SUPPAGINGMODE_INVALID = 0,
76 /** Normal 32-bit paging, no global pages */
77 SUPPAGINGMODE_32_BIT,
78 /** Normal 32-bit paging with global pages. */
79 SUPPAGINGMODE_32_BIT_GLOBAL,
80 /** PAE mode, no global pages, no NX. */
81 SUPPAGINGMODE_PAE,
82 /** PAE mode with global pages. */
83 SUPPAGINGMODE_PAE_GLOBAL,
84 /** PAE mode with NX, no global pages. */
85 SUPPAGINGMODE_PAE_NX,
86 /** PAE mode with global pages and NX. */
87 SUPPAGINGMODE_PAE_GLOBAL_NX,
88 /** AMD64 mode, no global pages. */
89 SUPPAGINGMODE_AMD64,
90 /** AMD64 mode with global pages, no NX. */
91 SUPPAGINGMODE_AMD64_GLOBAL,
92 /** AMD64 mode with NX, no global pages. */
93 SUPPAGINGMODE_AMD64_NX,
94 /** AMD64 mode with global pages and NX. */
95 SUPPAGINGMODE_AMD64_GLOBAL_NX
96} SUPPAGINGMODE;
97
98
99/** @name Flags returned by SUPR0GetKernelFeatures().
100 * @{
101 */
102/** GDT is read-only. */
103#define SUPKERNELFEATURES_GDT_READ_ONLY RT_BIT(0)
104/** @} */
105
106
107/**
108 * Usermode probe context information.
109 */
110typedef struct SUPDRVTRACERUSRCTX
111{
112 /** The probe ID from the VTG location record. */
113 uint32_t idProbe;
114 /** 32 if X86, 64 if AMD64. */
115 uint8_t cBits;
116 /** Reserved padding. */
117 uint8_t abReserved[3];
118 /** Data which format is dictated by the cBits member. */
119 union
120 {
121 /** X86 context info. */
122 struct
123 {
124 uint32_t uVtgProbeLoc; /**< Location record address. */
125 uint32_t aArgs[20]; /**< Raw arguments. */
126 uint32_t eip;
127 uint32_t eflags;
128 uint32_t eax;
129 uint32_t ecx;
130 uint32_t edx;
131 uint32_t ebx;
132 uint32_t esp;
133 uint32_t ebp;
134 uint32_t esi;
135 uint32_t edi;
136 uint16_t cs;
137 uint16_t ss;
138 uint16_t ds;
139 uint16_t es;
140 uint16_t fs;
141 uint16_t gs;
142 } X86;
143
144 /** AMD64 context info. */
145 struct
146 {
147 uint64_t uVtgProbeLoc; /**< Location record address. */
148 uint64_t aArgs[10]; /**< Raw arguments. */
149 uint64_t rip;
150 uint64_t rflags;
151 uint64_t rax;
152 uint64_t rcx;
153 uint64_t rdx;
154 uint64_t rbx;
155 uint64_t rsp;
156 uint64_t rbp;
157 uint64_t rsi;
158 uint64_t rdi;
159 uint64_t r8;
160 uint64_t r9;
161 uint64_t r10;
162 uint64_t r11;
163 uint64_t r12;
164 uint64_t r13;
165 uint64_t r14;
166 uint64_t r15;
167 } Amd64;
168 } u;
169} SUPDRVTRACERUSRCTX;
170/** Pointer to the usermode probe context information. */
171typedef SUPDRVTRACERUSRCTX *PSUPDRVTRACERUSRCTX;
172/** Pointer to the const usermode probe context information. */
173typedef SUPDRVTRACERUSRCTX const *PCSUPDRVTRACERUSRCTX;
174
175/**
176 * The result of a modification operation (SUPMSRPROBEROP_MODIFY or
177 * SUPMSRPROBEROP_MODIFY_FASTER).
178 */
179typedef struct SUPMSRPROBERMODIFYRESULT
180{
181 /** The MSR value prior to the modifications. Valid if fBeforeGp is false */
182 uint64_t uBefore;
183 /** The value that was written. Valid if fBeforeGp is false */
184 uint64_t uWritten;
185 /** The MSR value after the modifications. Valid if AfterGp is false. */
186 uint64_t uAfter;
187 /** Set if we GPed reading the MSR before the modification. */
188 bool fBeforeGp;
189 /** Set if we GPed while trying to write the modified value.
190 * This is set when fBeforeGp is true. */
191 bool fModifyGp;
192 /** Set if we GPed while trying to read the MSR after the modification.
193 * This is set when fBeforeGp is true. */
194 bool fAfterGp;
195 /** Set if we GPed while trying to restore the MSR after the modification.
196 * This is set when fBeforeGp is true. */
197 bool fRestoreGp;
198 /** Structure size alignment padding. */
199 bool afReserved[4];
200} SUPMSRPROBERMODIFYRESULT, *PSUPMSRPROBERMODIFYRESULT;
201
202
203/**
204 * The CPU state.
205 */
206typedef enum SUPGIPCPUSTATE
207{
208 /** Invalid CPU state / unused CPU entry. */
209 SUPGIPCPUSTATE_INVALID = 0,
210 /** The CPU is not present. */
211 SUPGIPCPUSTATE_ABSENT,
212 /** The CPU is offline. */
213 SUPGIPCPUSTATE_OFFLINE,
214 /** The CPU is online. */
215 SUPGIPCPUSTATE_ONLINE,
216 /** Force 32-bit enum type. */
217 SUPGIPCPUSTATE_32_BIT_HACK = 0x7fffffff
218} SUPGIPCPUSTATE;
219
220/**
221 * Per CPU data.
222 */
223typedef struct SUPGIPCPU
224{
225 /** Update transaction number.
226 * This number is incremented at the start and end of each update. It follows
227 * thusly that odd numbers indicates update in progress, while even numbers
228 * indicate stable data. Use this to make sure that the data items you fetch
229 * are consistent. */
230 volatile uint32_t u32TransactionId;
231 /** The interval in TSC ticks between two NanoTS updates.
232 * This is the average interval over the last 2, 4 or 8 updates + a little slack.
233 * The slack makes the time go a tiny tiny bit slower and extends the interval enough
234 * to avoid ending up with too many 1ns increments. */
235 volatile uint32_t u32UpdateIntervalTSC;
236 /** Current nanosecond timestamp. */
237 volatile uint64_t u64NanoTS;
238 /** The TSC at the time of u64NanoTS. */
239 volatile uint64_t u64TSC;
240 /** Current CPU Frequency. */
241 volatile uint64_t u64CpuHz;
242 /** The TSC delta with reference to the master TSC. */
243 volatile int64_t i64TSCDelta;
244 /** Number of errors during updating.
245 * Typical errors are under/overflows. */
246 volatile uint32_t cErrors;
247 /** Index of the head item in au32TSCHistory. */
248 volatile uint32_t iTSCHistoryHead;
249 /** Array of recent TSC interval deltas.
250 * The most recent item is at index iTSCHistoryHead.
251 * This history is used to calculate u32UpdateIntervalTSC.
252 */
253 volatile uint32_t au32TSCHistory[8];
254 /** The interval between the last two NanoTS updates. (experiment for now) */
255 volatile uint32_t u32PrevUpdateIntervalNS;
256
257 /** Reserved for future per processor data. */
258 volatile uint32_t au32Reserved0[5];
259
260 /** The TSC value read while doing TSC delta measurements across CPUs. */
261 volatile uint64_t u64TSCSample;
262
263 /** Reserved for future per processor data. */
264 volatile uint32_t au32Reserved1[1];
265
266 /** @todo Add topology/NUMA info. */
267 /** The CPU state. */
268 SUPGIPCPUSTATE volatile enmState;
269 /** The host CPU ID of this CPU (the SUPGIPCPU is indexed by APIC ID). */
270 RTCPUID idCpu;
271 /** The CPU set index of this CPU. */
272 int16_t iCpuSet;
273 /** The APIC ID of this CPU. */
274 uint16_t idApic;
275} SUPGIPCPU;
276AssertCompileSize(RTCPUID, 4);
277AssertCompileSize(SUPGIPCPU, 128);
278AssertCompileMemberAlignment(SUPGIPCPU, u64NanoTS, 8);
279AssertCompileMemberAlignment(SUPGIPCPU, u64TSC, 8);
280
281/** Pointer to per cpu data.
282 * @remark there is no const version of this typedef, see g_pSUPGlobalInfoPage for details. */
283typedef SUPGIPCPU *PSUPGIPCPU;
284
285
286
287/**
288 * Global Information Page.
289 *
290 * This page contains useful information and can be mapped into any
291 * process or VM. It can be accessed thru the g_pSUPGlobalInfoPage
292 * pointer when a session is open.
293 */
294typedef struct SUPGLOBALINFOPAGE
295{
296 /** Magic (SUPGLOBALINFOPAGE_MAGIC). */
297 uint32_t u32Magic;
298 /** The GIP version. */
299 uint32_t u32Version;
300
301 /** The GIP update mode, see SUPGIPMODE. */
302 uint32_t u32Mode;
303 /** The number of entries in the CPU table.
304 * (This can work as RTMpGetArraySize().) */
305 uint16_t cCpus;
306 /** The size of the GIP in pages. */
307 uint16_t cPages;
308 /** The update frequency of the of the NanoTS. */
309 volatile uint32_t u32UpdateHz;
310 /** The update interval in nanoseconds. (10^9 / u32UpdateHz) */
311 volatile uint32_t u32UpdateIntervalNS;
312 /** The timestamp of the last time we update the update frequency. */
313 volatile uint64_t u64NanoTSLastUpdateHz;
314 /** The TSC frequency of the system. */
315 uint64_t u64CpuHz;
316 /** The set of online CPUs. */
317 RTCPUSET OnlineCpuSet;
318 /** The set of present CPUs. */
319 RTCPUSET PresentCpuSet;
320 /** The set of possible CPUs. */
321 RTCPUSET PossibleCpuSet;
322 /** The number of CPUs that are online. */
323 volatile uint16_t cOnlineCpus;
324 /** The number of CPUs present in the system. */
325 volatile uint16_t cPresentCpus;
326 /** The highest number of CPUs possible. */
327 uint16_t cPossibleCpus;
328 uint16_t u16Padding0;
329 /** The max CPU ID (RTMpGetMaxCpuId). */
330 RTCPUID idCpuMax;
331 /** Whether the host OS has already normalized the hardware TSC deltas across
332 * CPUs. */
333 bool fOsTscDeltasInSync;
334 /** Whether the TSC deltas are small enough to not bother applying. */
335 bool fTscDeltasRoughlyInSync;
336 uint8_t au8Padding0[2];
337
338 /** Padding / reserved space for future data. */
339 uint32_t au32Padding1[26];
340
341 /** Table indexed by the CPU APIC ID to get the CPU table index. */
342 uint16_t aiCpuFromApicId[256];
343 /** CPU set index to CPU table index. */
344 uint16_t aiCpuFromCpuSetIdx[RTCPUSET_MAX_CPUS];
345
346 /** Array of per-cpu data.
347 * This is index by ApicId via the aiCpuFromApicId table.
348 *
349 * The clock and frequency information is updated for all CPUs if @c u32Mode
350 * is SUPGIPMODE_ASYNC_TSC. If @c u32Mode is SUPGIPMODE_SYNC_TSC only the first
351 * entry is updated. If @c u32Mode is SUPGIPMODE_SYNC_TSC the TSC frequency in
352 * @c u64CpuHz is copied to all CPUs. */
353 SUPGIPCPU aCPUs[1];
354} SUPGLOBALINFOPAGE;
355AssertCompileMemberAlignment(SUPGLOBALINFOPAGE, u64NanoTSLastUpdateHz, 8);
356#if defined(RT_ARCH_SPARC) || defined(RT_ARCH_SPARC64)
357AssertCompileMemberAlignment(SUPGLOBALINFOPAGE, aCPUs, 32);
358#else
359AssertCompileMemberAlignment(SUPGLOBALINFOPAGE, aCPUs, 256);
360#endif
361
362/** Pointer to the global info page.
363 * @remark there is no const version of this typedef, see g_pSUPGlobalInfoPage for details. */
364typedef SUPGLOBALINFOPAGE *PSUPGLOBALINFOPAGE;
365
366
367/** The value of the SUPGLOBALINFOPAGE::u32Magic field. (Soryo Fuyumi) */
368#define SUPGLOBALINFOPAGE_MAGIC 0x19590106
369/** The GIP version.
370 * Upper 16 bits is the major version. Major version is only changed with
371 * incompatible changes in the GIP. */
372#define SUPGLOBALINFOPAGE_VERSION 0x00050000
373
374/**
375 * SUPGLOBALINFOPAGE::u32Mode values.
376 */
377typedef enum SUPGIPMODE
378{
379 /** The usual invalid null entry. */
380 SUPGIPMODE_INVALID = 0,
381 /** The TSC of the cores and cpus in the system is in sync. */
382 SUPGIPMODE_SYNC_TSC,
383 /** Each core has it's own TSC. */
384 SUPGIPMODE_ASYNC_TSC,
385 /** The TSC of the cores are non-stop and have a constant frequency. */
386 SUPGIPMODE_INVARIANT_TSC,
387 /** The usual 32-bit hack. */
388 SUPGIPMODE_32BIT_HACK = 0x7fffffff
389} SUPGIPMODE;
390
391/** Pointer to the Global Information Page.
392 *
393 * This pointer is valid as long as SUPLib has a open session. Anyone using
394 * the page must treat this pointer as highly volatile and not trust it beyond
395 * one transaction.
396 *
397 * @remark The GIP page is read-only to everyone but the support driver and
398 * is actually mapped read only everywhere but in ring-0. However
399 * it is not marked 'const' as this might confuse compilers into
400 * thinking that values doesn't change even if members are marked
401 * as volatile. Thus, there is no PCSUPGLOBALINFOPAGE type.
402 */
403#if defined(IN_SUP_R0) || defined(IN_SUP_R3) || defined(IN_SUP_RC)
404extern DECLEXPORT(PSUPGLOBALINFOPAGE) g_pSUPGlobalInfoPage;
405
406#elif !defined(IN_RING0) || defined(RT_OS_WINDOWS) || defined(RT_OS_SOLARIS)
407extern DECLIMPORT(PSUPGLOBALINFOPAGE) g_pSUPGlobalInfoPage;
408
409#else /* IN_RING0 && !RT_OS_WINDOWS */
410# if !defined(__GNUC__) || defined(RT_OS_DARWIN) || !defined(RT_ARCH_AMD64)
411# define g_pSUPGlobalInfoPage (&g_SUPGlobalInfoPage)
412# else
413# define g_pSUPGlobalInfoPage (SUPGetGIPHlp())
414/** Workaround for ELF+GCC problem on 64-bit hosts.
415 * (GCC emits a mov with a R_X86_64_32 reloc, we need R_X86_64_64.) */
416DECLINLINE(PSUPGLOBALINFOPAGE) SUPGetGIPHlp(void)
417{
418 PSUPGLOBALINFOPAGE pGIP;
419 __asm__ __volatile__ ("movabs $g_SUPGlobalInfoPage,%0\n\t"
420 : "=a" (pGIP));
421 return pGIP;
422}
423# endif
424/** The GIP.
425 * We save a level of indirection by exporting the GIP instead of a variable
426 * pointing to it. */
427extern DECLIMPORT(SUPGLOBALINFOPAGE) g_SUPGlobalInfoPage;
428#endif
429
430/**
431 * Gets the GIP pointer.
432 *
433 * @returns Pointer to the GIP or NULL.
434 */
435SUPDECL(PSUPGLOBALINFOPAGE) SUPGetGIP(void);
436
437/** Whether the application of TSC-deltas is required. */
438#define GIP_ARE_TSC_DELTAS_APPLICABLE(a_pGip) \
439 ((a_pGip)->u32Mode == SUPGIPMODE_INVARIANT_TSC && !((a_pGip)->fOsTscDeltasInSync))
440
441#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
442/**
443 * Gets the TSC frequency of the calling CPU.
444 *
445 * @returns TSC frequency, UINT64_MAX on failure.
446 * @param pGip The GIP pointer.
447 */
448DECLINLINE(uint64_t) SUPGetCpuHzFromGIP(PSUPGLOBALINFOPAGE pGip)
449{
450 unsigned iCpu;
451
452 if (RT_UNLIKELY(!pGip || pGip->u32Magic != SUPGLOBALINFOPAGE_MAGIC || !pGip->u64CpuHz))
453 return UINT64_MAX;
454
455 if ( pGip->u32Mode == SUPGIPMODE_INVARIANT_TSC
456 || pGip->u32Mode == SUPGIPMODE_SYNC_TSC)
457 iCpu = 0;
458 else
459 {
460 Assert(pGip->u32Mode == SUPGIPMODE_ASYNC_TSC);
461 iCpu = pGip->aiCpuFromApicId[ASMGetApicId()];
462 if (RT_UNLIKELY(iCpu >= pGip->cCpus))
463 return UINT64_MAX;
464 }
465
466 return pGip->aCPUs[iCpu].u64CpuHz;
467}
468#endif /* X86 || AMD64 */
469
470/**
471 * Request for generic VMMR0Entry calls.
472 */
473typedef struct SUPVMMR0REQHDR
474{
475 /** The magic. (SUPVMMR0REQHDR_MAGIC) */
476 uint32_t u32Magic;
477 /** The size of the request. */
478 uint32_t cbReq;
479} SUPVMMR0REQHDR;
480/** Pointer to a ring-0 request header. */
481typedef SUPVMMR0REQHDR *PSUPVMMR0REQHDR;
482/** the SUPVMMR0REQHDR::u32Magic value (Ethan Iverson - The Bad Plus). */
483#define SUPVMMR0REQHDR_MAGIC UINT32_C(0x19730211)
484
485
486/** For the fast ioctl path.
487 * @{
488 */
489/** @see VMMR0_DO_RAW_RUN. */
490#define SUP_VMMR0_DO_RAW_RUN 0
491/** @see VMMR0_DO_HM_RUN. */
492#define SUP_VMMR0_DO_HM_RUN 1
493/** @see VMMR0_DO_NOP */
494#define SUP_VMMR0_DO_NOP 2
495/** @} */
496
497/** SUPR3QueryVTCaps capability flags
498 * @{
499 */
500#define SUPVTCAPS_AMD_V RT_BIT(0)
501#define SUPVTCAPS_VT_X RT_BIT(1)
502#define SUPVTCAPS_NESTED_PAGING RT_BIT(2)
503/** @} */
504
505/**
506 * Request for generic FNSUPR0SERVICEREQHANDLER calls.
507 */
508typedef struct SUPR0SERVICEREQHDR
509{
510 /** The magic. (SUPR0SERVICEREQHDR_MAGIC) */
511 uint32_t u32Magic;
512 /** The size of the request. */
513 uint32_t cbReq;
514} SUPR0SERVICEREQHDR;
515/** Pointer to a ring-0 service request header. */
516typedef SUPR0SERVICEREQHDR *PSUPR0SERVICEREQHDR;
517/** the SUPVMMR0REQHDR::u32Magic value (Esbjoern Svensson - E.S.P.). */
518#define SUPR0SERVICEREQHDR_MAGIC UINT32_C(0x19640416)
519
520
521/** Event semaphore handle. Ring-0 / ring-3. */
522typedef R0PTRTYPE(struct SUPSEMEVENTHANDLE *) SUPSEMEVENT;
523/** Pointer to an event semaphore handle. */
524typedef SUPSEMEVENT *PSUPSEMEVENT;
525/** Nil event semaphore handle. */
526#define NIL_SUPSEMEVENT ((SUPSEMEVENT)0)
527
528/**
529 * Creates a single release event semaphore.
530 *
531 * @returns VBox status code.
532 * @param pSession The session handle of the caller.
533 * @param phEvent Where to return the handle to the event semaphore.
534 */
535SUPDECL(int) SUPSemEventCreate(PSUPDRVSESSION pSession, PSUPSEMEVENT phEvent);
536
537/**
538 * Closes a single release event semaphore handle.
539 *
540 * @returns VBox status code.
541 * @retval VINF_OBJECT_DESTROYED if the semaphore was destroyed.
542 * @retval VINF_SUCCESS if the handle was successfully closed but the semaphore
543 * object remained alive because of other references.
544 *
545 * @param pSession The session handle of the caller.
546 * @param hEvent The handle. Nil is quietly ignored.
547 */
548SUPDECL(int) SUPSemEventClose(PSUPDRVSESSION pSession, SUPSEMEVENT hEvent);
549
550/**
551 * Signals a single release event semaphore.
552 *
553 * @returns VBox status code.
554 * @param pSession The session handle of the caller.
555 * @param hEvent The semaphore handle.
556 */
557SUPDECL(int) SUPSemEventSignal(PSUPDRVSESSION pSession, SUPSEMEVENT hEvent);
558
559#ifdef IN_RING0
560/**
561 * Waits on a single release event semaphore, not interruptible.
562 *
563 * @returns VBox status code.
564 * @param pSession The session handle of the caller.
565 * @param hEvent The semaphore handle.
566 * @param cMillies The number of milliseconds to wait.
567 * @remarks Not available in ring-3.
568 */
569SUPDECL(int) SUPSemEventWait(PSUPDRVSESSION pSession, SUPSEMEVENT hEvent, uint32_t cMillies);
570#endif
571
572/**
573 * Waits on a single release event semaphore, interruptible.
574 *
575 * @returns VBox status code.
576 * @param pSession The session handle of the caller.
577 * @param hEvent The semaphore handle.
578 * @param cMillies The number of milliseconds to wait.
579 */
580SUPDECL(int) SUPSemEventWaitNoResume(PSUPDRVSESSION pSession, SUPSEMEVENT hEvent, uint32_t cMillies);
581
582/**
583 * Waits on a single release event semaphore, interruptible.
584 *
585 * @returns VBox status code.
586 * @param pSession The session handle of the caller.
587 * @param hEvent The semaphore handle.
588 * @param uNsTimeout The deadline given on the RTTimeNanoTS() clock.
589 */
590SUPDECL(int) SUPSemEventWaitNsAbsIntr(PSUPDRVSESSION pSession, SUPSEMEVENT hEvent, uint64_t uNsTimeout);
591
592/**
593 * Waits on a single release event semaphore, interruptible.
594 *
595 * @returns VBox status code.
596 * @param pSession The session handle of the caller.
597 * @param hEvent The semaphore handle.
598 * @param cNsTimeout The number of nanoseconds to wait.
599 */
600SUPDECL(int) SUPSemEventWaitNsRelIntr(PSUPDRVSESSION pSession, SUPSEMEVENT hEvent, uint64_t cNsTimeout);
601
602/**
603 * Gets the best timeout resolution that SUPSemEventWaitNsAbsIntr and
604 * SUPSemEventWaitNsAbsIntr can do.
605 *
606 * @returns The resolution in nanoseconds.
607 * @param pSession The session handle of the caller.
608 */
609SUPDECL(uint32_t) SUPSemEventGetResolution(PSUPDRVSESSION pSession);
610
611
612/** Multiple release event semaphore handle. Ring-0 / ring-3. */
613typedef R0PTRTYPE(struct SUPSEMEVENTMULTIHANDLE *) SUPSEMEVENTMULTI;
614/** Pointer to an multiple release event semaphore handle. */
615typedef SUPSEMEVENTMULTI *PSUPSEMEVENTMULTI;
616/** Nil multiple release event semaphore handle. */
617#define NIL_SUPSEMEVENTMULTI ((SUPSEMEVENTMULTI)0)
618
619/**
620 * Creates a multiple release event semaphore.
621 *
622 * @returns VBox status code.
623 * @param pSession The session handle of the caller.
624 * @param phEventMulti Where to return the handle to the event semaphore.
625 */
626SUPDECL(int) SUPSemEventMultiCreate(PSUPDRVSESSION pSession, PSUPSEMEVENTMULTI phEventMulti);
627
628/**
629 * Closes a multiple release event semaphore handle.
630 *
631 * @returns VBox status code.
632 * @retval VINF_OBJECT_DESTROYED if the semaphore was destroyed.
633 * @retval VINF_SUCCESS if the handle was successfully closed but the semaphore
634 * object remained alive because of other references.
635 *
636 * @param pSession The session handle of the caller.
637 * @param hEventMulti The handle. Nil is quietly ignored.
638 */
639SUPDECL(int) SUPSemEventMultiClose(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti);
640
641/**
642 * Signals a multiple release event semaphore.
643 *
644 * @returns VBox status code.
645 * @param pSession The session handle of the caller.
646 * @param hEventMulti The semaphore handle.
647 */
648SUPDECL(int) SUPSemEventMultiSignal(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti);
649
650/**
651 * Resets a multiple release event semaphore.
652 *
653 * @returns VBox status code.
654 * @param pSession The session handle of the caller.
655 * @param hEventMulti The semaphore handle.
656 */
657SUPDECL(int) SUPSemEventMultiReset(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti);
658
659#ifdef IN_RING0
660/**
661 * Waits on a multiple release event semaphore, not interruptible.
662 *
663 * @returns VBox status code.
664 * @param pSession The session handle of the caller.
665 * @param hEventMulti The semaphore handle.
666 * @param cMillies The number of milliseconds to wait.
667 * @remarks Not available in ring-3.
668 */
669SUPDECL(int) SUPSemEventMultiWait(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti, uint32_t cMillies);
670#endif
671
672/**
673 * Waits on a multiple release event semaphore, interruptible.
674 *
675 * @returns VBox status code.
676 * @param pSession The session handle of the caller.
677 * @param hEventMulti The semaphore handle.
678 * @param cMillies The number of milliseconds to wait.
679 */
680SUPDECL(int) SUPSemEventMultiWaitNoResume(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti, uint32_t cMillies);
681
682/**
683 * Waits on a multiple release event semaphore, interruptible.
684 *
685 * @returns VBox status code.
686 * @param pSession The session handle of the caller.
687 * @param hEventMulti The semaphore handle.
688 * @param uNsTimeout The deadline given on the RTTimeNanoTS() clock.
689 */
690SUPDECL(int) SUPSemEventMultiWaitNsAbsIntr(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti, uint64_t uNsTimeout);
691
692/**
693 * Waits on a multiple release event semaphore, interruptible.
694 *
695 * @returns VBox status code.
696 * @param pSession The session handle of the caller.
697 * @param hEventMulti The semaphore handle.
698 * @param cNsTimeout The number of nanoseconds to wait.
699 */
700SUPDECL(int) SUPSemEventMultiWaitNsRelIntr(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti, uint64_t cNsTimeout);
701
702/**
703 * Gets the best timeout resolution that SUPSemEventMultiWaitNsAbsIntr and
704 * SUPSemEventMultiWaitNsRelIntr can do.
705 *
706 * @returns The resolution in nanoseconds.
707 * @param pSession The session handle of the caller.
708 */
709SUPDECL(uint32_t) SUPSemEventMultiGetResolution(PSUPDRVSESSION pSession);
710
711
712#ifdef IN_RING3
713
714/** @defgroup grp_sup_r3 SUP Host Context Ring-3 API
715 * @{
716 */
717
718/**
719 * Installs the support library.
720 *
721 * @returns VBox status code.
722 */
723SUPR3DECL(int) SUPR3Install(void);
724
725/**
726 * Uninstalls the support library.
727 *
728 * @returns VBox status code.
729 */
730SUPR3DECL(int) SUPR3Uninstall(void);
731
732/**
733 * Trusted main entry point.
734 *
735 * This is exported as "TrustedMain" by the dynamic libraries which contains the
736 * "real" application binary for which the hardened stub is built. The entry
737 * point is invoked upon successful initialization of the support library and
738 * runtime.
739 *
740 * @returns main kind of exit code.
741 * @param argc The argument count.
742 * @param argv The argument vector.
743 * @param envp The environment vector.
744 */
745typedef DECLCALLBACK(int) FNSUPTRUSTEDMAIN(int argc, char **argv, char **envp);
746/** Pointer to FNSUPTRUSTEDMAIN(). */
747typedef FNSUPTRUSTEDMAIN *PFNSUPTRUSTEDMAIN;
748
749/** Which operation failed. */
750typedef enum SUPINITOP
751{
752 /** Invalid. */
753 kSupInitOp_Invalid = 0,
754 /** Installation integrity error. */
755 kSupInitOp_Integrity,
756 /** Setuid related. */
757 kSupInitOp_RootCheck,
758 /** Driver related. */
759 kSupInitOp_Driver,
760 /** IPRT init related. */
761 kSupInitOp_IPRT,
762 /** Miscellaneous. */
763 kSupInitOp_Misc,
764 /** Place holder. */
765 kSupInitOp_End
766} SUPINITOP;
767
768/**
769 * Trusted error entry point, optional.
770 *
771 * This is exported as "TrustedError" by the dynamic libraries which contains
772 * the "real" application binary for which the hardened stub is built. The
773 * hardened main() must specify SUPSECMAIN_FLAGS_TRUSTED_ERROR when calling
774 * SUPR3HardenedMain.
775 *
776 * @param pszWhere Where the error occurred (function name).
777 * @param enmWhat Which operation went wrong.
778 * @param rc The status code.
779 * @param pszMsgFmt Error message format string.
780 * @param va The message format arguments.
781 */
782typedef DECLCALLBACK(void) FNSUPTRUSTEDERROR(const char *pszWhere, SUPINITOP enmWhat, int rc, const char *pszMsgFmt, va_list va);
783/** Pointer to FNSUPTRUSTEDERROR. */
784typedef FNSUPTRUSTEDERROR *PFNSUPTRUSTEDERROR;
785
786/**
787 * Secure main.
788 *
789 * This is used for the set-user-ID-on-execute binaries on unixy systems
790 * and when using the open-vboxdrv-via-root-service setup on Windows.
791 *
792 * This function will perform the integrity checks of the VirtualBox
793 * installation, open the support driver, open the root service (later),
794 * and load the DLL corresponding to \a pszProgName and execute its main
795 * function.
796 *
797 * @returns Return code appropriate for main().
798 *
799 * @param pszProgName The program name. This will be used to figure out which
800 * DLL/SO/DYLIB to load and execute.
801 * @param fFlags Flags.
802 * @param argc The argument count.
803 * @param argv The argument vector.
804 * @param envp The environment vector.
805 */
806DECLHIDDEN(int) SUPR3HardenedMain(const char *pszProgName, uint32_t fFlags, int argc, char **argv, char **envp);
807
808/** @name SUPR3HardenedMain flags.
809 * @{ */
810/** Don't open the device. (Intended for VirtualBox without -startvm.) */
811#define SUPSECMAIN_FLAGS_DONT_OPEN_DEV RT_BIT_32(0)
812/** The hardened DLL has a "TrustedError" function (see FNSUPTRUSTEDERROR). */
813#define SUPSECMAIN_FLAGS_TRUSTED_ERROR RT_BIT_32(1)
814/** @} */
815
816/**
817 * Initializes the support library.
818 *
819 * Each successful call to SUPR3Init() or SUPR3InitEx must be countered by a
820 * call to SUPR3Term(false).
821 *
822 * @returns VBox status code.
823 * @param ppSession Where to store the session handle. Defaults to NULL.
824 */
825SUPR3DECL(int) SUPR3Init(PSUPDRVSESSION *ppSession);
826
827
828/**
829 * Initializes the support library, extended version.
830 *
831 * Each successful call to SUPR3Init() or SUPR3InitEx must be countered by a
832 * call to SUPR3Term(false).
833 *
834 * @returns VBox status code.
835 * @param fUnrestricted The desired access.
836 * @param ppSession Where to store the session handle. Defaults to NULL.
837 */
838SUPR3DECL(int) SUPR3InitEx(bool fUnrestricted, PSUPDRVSESSION *ppSession);
839
840/**
841 * Terminates the support library.
842 *
843 * @returns VBox status code.
844 * @param fForced Forced termination. This means to ignore the
845 * init call count and just terminated.
846 */
847#ifdef __cplusplus
848SUPR3DECL(int) SUPR3Term(bool fForced = false);
849#else
850SUPR3DECL(int) SUPR3Term(int fForced);
851#endif
852
853/**
854 * Sets the ring-0 VM handle for use with fast IOCtls.
855 *
856 * @returns VBox status code.
857 * @param pVMR0 The ring-0 VM handle.
858 * NIL_RTR0PTR can be used to unset the handle when the
859 * VM is about to be destroyed.
860 */
861SUPR3DECL(int) SUPR3SetVMForFastIOCtl(PVMR0 pVMR0);
862
863/**
864 * Calls the HC R0 VMM entry point.
865 * See VMMR0Entry() for more details.
866 *
867 * @returns error code specific to uFunction.
868 * @param pVMR0 Pointer to the Ring-0 (Host Context) mapping of the VM structure.
869 * @param idCpu The virtual CPU ID.
870 * @param uOperation Operation to execute.
871 * @param pvArg Argument.
872 */
873SUPR3DECL(int) SUPR3CallVMMR0(PVMR0 pVMR0, VMCPUID idCpu, unsigned uOperation, void *pvArg);
874
875/**
876 * Variant of SUPR3CallVMMR0, except that this takes the fast ioclt path
877 * regardsless of compile-time defaults.
878 *
879 * @returns VBox status code.
880 * @param pVMR0 The ring-0 VM handle.
881 * @param uOperation The operation; only the SUP_VMMR0_DO_* ones are valid.
882 * @param idCpu The virtual CPU ID.
883 */
884SUPR3DECL(int) SUPR3CallVMMR0Fast(PVMR0 pVMR0, unsigned uOperation, VMCPUID idCpu);
885
886/**
887 * Calls the HC R0 VMM entry point, in a safer but slower manner than
888 * SUPR3CallVMMR0. When entering using this call the R0 components can call
889 * into the host kernel (i.e. use the SUPR0 and RT APIs).
890 *
891 * See VMMR0Entry() for more details.
892 *
893 * @returns error code specific to uFunction.
894 * @param pVMR0 Pointer to the Ring-0 (Host Context) mapping of the VM structure.
895 * @param idCpu The virtual CPU ID.
896 * @param uOperation Operation to execute.
897 * @param u64Arg Constant argument.
898 * @param pReqHdr Pointer to a request header. Optional.
899 * This will be copied in and out of kernel space. There currently is a size
900 * limit on this, just below 4KB.
901 */
902SUPR3DECL(int) SUPR3CallVMMR0Ex(PVMR0 pVMR0, VMCPUID idCpu, unsigned uOperation, uint64_t u64Arg, PSUPVMMR0REQHDR pReqHdr);
903
904/**
905 * Calls a ring-0 service.
906 *
907 * The operation and the request packet is specific to the service.
908 *
909 * @returns error code specific to uFunction.
910 * @param pszService The service name.
911 * @param cchService The length of the service name.
912 * @param uReq The request number.
913 * @param u64Arg Constant argument.
914 * @param pReqHdr Pointer to a request header. Optional.
915 * This will be copied in and out of kernel space. There currently is a size
916 * limit on this, just below 4KB.
917 */
918SUPR3DECL(int) SUPR3CallR0Service(const char *pszService, size_t cchService, uint32_t uOperation, uint64_t u64Arg, PSUPR0SERVICEREQHDR pReqHdr);
919
920/** Which logger. */
921typedef enum SUPLOGGER
922{
923 SUPLOGGER_DEBUG = 1,
924 SUPLOGGER_RELEASE
925} SUPLOGGER;
926
927/**
928 * Changes the settings of the specified ring-0 logger.
929 *
930 * @returns VBox status code.
931 * @param enmWhich Which logger.
932 * @param pszFlags The flags settings.
933 * @param pszGroups The groups settings.
934 * @param pszDest The destination specificier.
935 */
936SUPR3DECL(int) SUPR3LoggerSettings(SUPLOGGER enmWhich, const char *pszFlags, const char *pszGroups, const char *pszDest);
937
938/**
939 * Creates a ring-0 logger instance.
940 *
941 * @returns VBox status code.
942 * @param enmWhich Which logger to create.
943 * @param pszFlags The flags settings.
944 * @param pszGroups The groups settings.
945 * @param pszDest The destination specificier.
946 */
947SUPR3DECL(int) SUPR3LoggerCreate(SUPLOGGER enmWhich, const char *pszFlags, const char *pszGroups, const char *pszDest);
948
949/**
950 * Destroys a ring-0 logger instance.
951 *
952 * @returns VBox status code.
953 * @param enmWhich Which logger.
954 */
955SUPR3DECL(int) SUPR3LoggerDestroy(SUPLOGGER enmWhich);
956
957/**
958 * Queries the paging mode of the host OS.
959 *
960 * @returns The paging mode.
961 */
962SUPR3DECL(SUPPAGINGMODE) SUPR3GetPagingMode(void);
963
964/**
965 * Allocate zero-filled pages.
966 *
967 * Use this to allocate a number of pages suitable for seeding / locking.
968 * Call SUPR3PageFree() to free the pages once done with them.
969 *
970 * @returns VBox status.
971 * @param cPages Number of pages to allocate.
972 * @param ppvPages Where to store the base pointer to the allocated pages.
973 */
974SUPR3DECL(int) SUPR3PageAlloc(size_t cPages, void **ppvPages);
975
976/**
977 * Frees pages allocated with SUPR3PageAlloc().
978 *
979 * @returns VBox status.
980 * @param pvPages Pointer returned by SUPR3PageAlloc().
981 * @param cPages Number of pages that was allocated.
982 */
983SUPR3DECL(int) SUPR3PageFree(void *pvPages, size_t cPages);
984
985/**
986 * Allocate non-zeroed, locked, pages with user and, optionally, kernel
987 * mappings.
988 *
989 * Use SUPR3PageFreeEx() to free memory allocated with this function.
990 *
991 * @returns VBox status code.
992 * @param cPages The number of pages to allocate.
993 * @param fFlags Flags, reserved. Must be zero.
994 * @param ppvPages Where to store the address of the user mapping.
995 * @param pR0Ptr Where to store the address of the kernel mapping.
996 * NULL if no kernel mapping is desired.
997 * @param paPages Where to store the physical addresses of each page.
998 * Optional.
999 */
1000SUPR3DECL(int) SUPR3PageAllocEx(size_t cPages, uint32_t fFlags, void **ppvPages, PRTR0PTR pR0Ptr, PSUPPAGE paPages);
1001
1002/**
1003 * Maps a portion of a ring-3 only allocation into kernel space.
1004 *
1005 * @returns VBox status code.
1006 *
1007 * @param pvR3 The address SUPR3PageAllocEx return.
1008 * @param off Offset to start mapping at. Must be page aligned.
1009 * @param cb Number of bytes to map. Must be page aligned.
1010 * @param fFlags Flags, must be zero.
1011 * @param pR0Ptr Where to store the address on success.
1012 *
1013 */
1014SUPR3DECL(int) SUPR3PageMapKernel(void *pvR3, uint32_t off, uint32_t cb, uint32_t fFlags, PRTR0PTR pR0Ptr);
1015
1016/**
1017 * Changes the protection of
1018 *
1019 * @returns VBox status code.
1020 * @retval VERR_NOT_SUPPORTED if the OS doesn't allow us to change page level
1021 * protection. See also RTR0MemObjProtect.
1022 *
1023 * @param pvR3 The ring-3 address SUPR3PageAllocEx returned.
1024 * @param R0Ptr The ring-0 address SUPR3PageAllocEx returned if it
1025 * is desired that the corresponding ring-0 page
1026 * mappings should change protection as well. Pass
1027 * NIL_RTR0PTR if the ring-0 pages should remain
1028 * unaffected.
1029 * @param off Offset to start at which to start chagning the page
1030 * level protection. Must be page aligned.
1031 * @param cb Number of bytes to change. Must be page aligned.
1032 * @param fProt The new page level protection, either a combination
1033 * of RTMEM_PROT_READ, RTMEM_PROT_WRITE and
1034 * RTMEM_PROT_EXEC, or just RTMEM_PROT_NONE.
1035 */
1036SUPR3DECL(int) SUPR3PageProtect(void *pvR3, RTR0PTR R0Ptr, uint32_t off, uint32_t cb, uint32_t fProt);
1037
1038/**
1039 * Free pages allocated by SUPR3PageAllocEx.
1040 *
1041 * @returns VBox status code.
1042 * @param pvPages The address of the user mapping.
1043 * @param cPages The number of pages.
1044 */
1045SUPR3DECL(int) SUPR3PageFreeEx(void *pvPages, size_t cPages);
1046
1047/**
1048 * Allocated memory with page aligned memory with a contiguous and locked physical
1049 * memory backing below 4GB.
1050 *
1051 * @returns Pointer to the allocated memory (virtual address).
1052 * *pHCPhys is set to the physical address of the memory.
1053 * If ppvR0 isn't NULL, *ppvR0 is set to the ring-0 mapping.
1054 * The returned memory must be freed using SUPR3ContFree().
1055 * @returns NULL on failure.
1056 * @param cPages Number of pages to allocate.
1057 * @param pR0Ptr Where to store the ring-0 mapping of the allocation. (optional)
1058 * @param pHCPhys Where to store the physical address of the memory block.
1059 *
1060 * @remark This 2nd version of this API exists because we're not able to map the
1061 * ring-3 mapping executable on WIN64. This is a serious problem in regard to
1062 * the world switchers.
1063 */
1064SUPR3DECL(void *) SUPR3ContAlloc(size_t cPages, PRTR0PTR pR0Ptr, PRTHCPHYS pHCPhys);
1065
1066/**
1067 * Frees memory allocated with SUPR3ContAlloc().
1068 *
1069 * @returns VBox status code.
1070 * @param pv Pointer to the memory block which should be freed.
1071 * @param cPages Number of pages to be freed.
1072 */
1073SUPR3DECL(int) SUPR3ContFree(void *pv, size_t cPages);
1074
1075/**
1076 * Allocated non contiguous physical memory below 4GB.
1077 *
1078 * The memory isn't zeroed.
1079 *
1080 * @returns VBox status code.
1081 * @returns NULL on failure.
1082 * @param cPages Number of pages to allocate.
1083 * @param ppvPages Where to store the pointer to the allocated memory.
1084 * The pointer stored here on success must be passed to
1085 * SUPR3LowFree when the memory should be released.
1086 * @param ppvPagesR0 Where to store the ring-0 pointer to the allocated memory. optional.
1087 * @param paPages Where to store the physical addresses of the individual pages.
1088 */
1089SUPR3DECL(int) SUPR3LowAlloc(size_t cPages, void **ppvPages, PRTR0PTR ppvPagesR0, PSUPPAGE paPages);
1090
1091/**
1092 * Frees memory allocated with SUPR3LowAlloc().
1093 *
1094 * @returns VBox status code.
1095 * @param pv Pointer to the memory block which should be freed.
1096 * @param cPages Number of pages that was allocated.
1097 */
1098SUPR3DECL(int) SUPR3LowFree(void *pv, size_t cPages);
1099
1100/**
1101 * Load a module into R0 HC.
1102 *
1103 * This will verify the file integrity in a similar manner as
1104 * SUPR3HardenedVerifyFile before loading it.
1105 *
1106 * @returns VBox status code.
1107 * @param pszFilename The path to the image file.
1108 * @param pszModule The module name. Max 32 bytes.
1109 * @param ppvImageBase Where to store the image address.
1110 * @param pErrInfo Where to return extended error information.
1111 * Optional.
1112 */
1113SUPR3DECL(int) SUPR3LoadModule(const char *pszFilename, const char *pszModule, void **ppvImageBase, PRTERRINFO pErrInfo);
1114
1115/**
1116 * Load a module into R0 HC.
1117 *
1118 * This will verify the file integrity in a similar manner as
1119 * SUPR3HardenedVerifyFile before loading it.
1120 *
1121 * @returns VBox status code.
1122 * @param pszFilename The path to the image file.
1123 * @param pszModule The module name. Max 32 bytes.
1124 * @param pszSrvReqHandler The name of the service request handler entry
1125 * point. See FNSUPR0SERVICEREQHANDLER.
1126 * @param ppvImageBase Where to store the image address.
1127 */
1128SUPR3DECL(int) SUPR3LoadServiceModule(const char *pszFilename, const char *pszModule,
1129 const char *pszSrvReqHandler, void **ppvImageBase);
1130
1131/**
1132 * Frees a R0 HC module.
1133 *
1134 * @returns VBox status code.
1135 * @param pszModule The module to free.
1136 * @remark This will not actually 'free' the module, there are of course usage counting.
1137 */
1138SUPR3DECL(int) SUPR3FreeModule(void *pvImageBase);
1139
1140/**
1141 * Lock down the module loader interface.
1142 *
1143 * This will lock down the module loader interface. No new modules can be
1144 * loaded and all loaded modules can no longer be freed.
1145 *
1146 * @returns VBox status code.
1147 * @param pErrInfo Where to return extended error information.
1148 * Optional.
1149 */
1150SUPR3DECL(int) SUPR3LockDownLoader(PRTERRINFO pErrInfo);
1151
1152/**
1153 * Get the address of a symbol in a ring-0 module.
1154 *
1155 * @returns VBox status code.
1156 * @param pszModule The module name.
1157 * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
1158 * ordinal value rather than a string pointer.
1159 * @param ppvValue Where to store the symbol value.
1160 */
1161SUPR3DECL(int) SUPR3GetSymbolR0(void *pvImageBase, const char *pszSymbol, void **ppvValue);
1162
1163/**
1164 * Load R0 HC VMM code.
1165 *
1166 * @returns VBox status code.
1167 * @deprecated Use SUPR3LoadModule(pszFilename, "VMMR0.r0", &pvImageBase)
1168 */
1169SUPR3DECL(int) SUPR3LoadVMM(const char *pszFilename);
1170
1171/**
1172 * Unloads R0 HC VMM code.
1173 *
1174 * @returns VBox status code.
1175 * @deprecated Use SUPR3FreeModule().
1176 */
1177SUPR3DECL(int) SUPR3UnloadVMM(void);
1178
1179/**
1180 * Get the physical address of the GIP.
1181 *
1182 * @returns VBox status code.
1183 * @param pHCPhys Where to store the physical address of the GIP.
1184 */
1185SUPR3DECL(int) SUPR3GipGetPhys(PRTHCPHYS pHCPhys);
1186
1187/**
1188 * Initializes only the bits relevant for the SUPR3HardenedVerify* APIs.
1189 *
1190 * This is for users that don't necessarily need to initialize the whole of
1191 * SUPLib. There is no harm in calling this one more time.
1192 *
1193 * @returns VBox status code.
1194 * @remarks Currently not counted, so only call once.
1195 */
1196SUPR3DECL(int) SUPR3HardenedVerifyInit(void);
1197
1198/**
1199 * Reverses the effect of SUPR3HardenedVerifyInit if SUPR3InitEx hasn't been
1200 * called.
1201 *
1202 * Ignored if the support library was initialized using SUPR3Init or
1203 * SUPR3InitEx.
1204 *
1205 * @returns VBox status code.
1206 */
1207SUPR3DECL(int) SUPR3HardenedVerifyTerm(void);
1208
1209/**
1210 * Verifies the integrity of a file, and optionally opens it.
1211 *
1212 * The integrity check is for whether the file is suitable for loading into
1213 * the hypervisor or VM process. The integrity check may include verifying
1214 * the authenticode/elfsign/whatever signature of the file, which can take
1215 * a little while.
1216 *
1217 * @returns VBox status code. On failure it will have printed a LogRel message.
1218 *
1219 * @param pszFilename The file.
1220 * @param pszWhat For the LogRel on failure.
1221 * @param phFile Where to store the handle to the opened file. This is optional, pass NULL
1222 * if the file should not be opened.
1223 * @deprecated Write a new one.
1224 */
1225SUPR3DECL(int) SUPR3HardenedVerifyFile(const char *pszFilename, const char *pszWhat, PRTFILE phFile);
1226
1227/**
1228 * Verifies the integrity of a the current process, including the image
1229 * location and that the invocation was absolute.
1230 *
1231 * This must currently be called after initializing the runtime. The intended
1232 * audience is set-uid-to-root applications, root services and similar.
1233 *
1234 * @returns VBox status code. On failure
1235 * message.
1236 * @param pszArgv0 The first argument to main().
1237 * @param fInternal Set this to @c true if this is an internal
1238 * VirtualBox application. Otherwise pass @c false.
1239 * @param pErrInfo Where to return extended error information.
1240 */
1241SUPR3DECL(int) SUPR3HardenedVerifySelf(const char *pszArgv0, bool fInternal, PRTERRINFO pErrInfo);
1242
1243/**
1244 * Verifies the integrity of an installation directory.
1245 *
1246 * The integrity check verifies that the directory cannot be tampered with by
1247 * normal users on the system. On Unix this translates to root ownership and
1248 * no symbolic linking.
1249 *
1250 * @returns VBox status code. On failure a message will be stored in @a pszErr.
1251 *
1252 * @param pszDirPath The directory path.
1253 * @param fRecursive Whether the check should be recursive or
1254 * not. When set, all sub-directores will be checked,
1255 * including files (@a fCheckFiles is ignored).
1256 * @param fCheckFiles Whether to apply the same basic integrity check to
1257 * the files in the directory as the directory itself.
1258 * @param pErrInfo Where to return extended error information.
1259 * Optional.
1260 */
1261SUPR3DECL(int) SUPR3HardenedVerifyDir(const char *pszDirPath, bool fRecursive, bool fCheckFiles, PRTERRINFO pErrInfo);
1262
1263/**
1264 * Verifies the integrity of a plug-in module.
1265 *
1266 * This is similar to SUPR3HardenedLdrLoad, except it does not load the module
1267 * and that the module does not have to be shipped with VirtualBox.
1268 *
1269 * @returns VBox status code. On failure a message will be stored in @a pszErr.
1270 *
1271 * @param pszFilename The filename of the plug-in module (nothing can be
1272 * omitted here).
1273 * @param pErrInfo Where to return extended error information.
1274 * Optional.
1275 */
1276SUPR3DECL(int) SUPR3HardenedVerifyPlugIn(const char *pszFilename, PRTERRINFO pErrInfo);
1277
1278/**
1279 * Same as RTLdrLoad() but will verify the files it loads (hardened builds).
1280 *
1281 * Will add dll suffix if missing and try load the file.
1282 *
1283 * @returns iprt status code.
1284 * @param pszFilename Image filename. This must have a path.
1285 * @param phLdrMod Where to store the handle to the loaded module.
1286 * @param fFlags See RTLDRLOAD_FLAGS_XXX.
1287 * @param pErrInfo Where to return extended error information.
1288 * Optional.
1289 */
1290SUPR3DECL(int) SUPR3HardenedLdrLoad(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo);
1291
1292/**
1293 * Same as RTLdrLoadAppPriv() but it will verify the files it loads (hardened
1294 * builds).
1295 *
1296 * Will add dll suffix to the file if missing, then look for it in the
1297 * architecture dependent application directory.
1298 *
1299 * @returns iprt status code.
1300 * @param pszFilename Image filename.
1301 * @param phLdrMod Where to store the handle to the loaded module.
1302 * @param fFlags See RTLDRLOAD_FLAGS_XXX.
1303 * @param pErrInfo Where to return extended error information.
1304 * Optional.
1305 */
1306SUPR3DECL(int) SUPR3HardenedLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo);
1307
1308/**
1309 * Same as RTLdrLoad() but will verify the files it loads (hardened builds).
1310 *
1311 * This differs from SUPR3HardenedLdrLoad() in that it can load modules from
1312 * extension packs and anything else safely installed on the system, provided
1313 * they pass the hardening tests.
1314 *
1315 * @returns iprt status code.
1316 * @param pszFilename The full path to the module, with extension.
1317 * @param phLdrMod Where to store the handle to the loaded module.
1318 * @param pErrInfo Where to return extended error information.
1319 * Optional.
1320 */
1321SUPR3DECL(int) SUPR3HardenedLdrLoadPlugIn(const char *pszFilename, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo);
1322
1323/**
1324 * Check if the host kernel can run in VMX root mode.
1325 *
1326 * @returns VINF_SUCCESS if supported, error code indicating why if not.
1327 */
1328SUPR3DECL(int) SUPR3QueryVTxSupported(void);
1329
1330/**
1331 * Return VT-x/AMD-V capabilities.
1332 *
1333 * @returns VINF_SUCCESS if supported, error code indicating why if not.
1334 * @param pfCaps Pointer to capability dword (out).
1335 * @todo Intended for main, which means we need to relax the privilege requires
1336 * when accessing certain vboxdrv functions.
1337 */
1338SUPR3DECL(int) SUPR3QueryVTCaps(uint32_t *pfCaps);
1339
1340/**
1341 * Open the tracer.
1342 *
1343 * @returns VBox status code.
1344 * @param uCookie Cookie identifying the tracer we expect to talk to.
1345 * @param uArg Tracer specific open argument.
1346 */
1347SUPR3DECL(int) SUPR3TracerOpen(uint32_t uCookie, uintptr_t uArg);
1348
1349/**
1350 * Closes the tracer.
1351 *
1352 * @returns VBox status code.
1353 */
1354SUPR3DECL(int) SUPR3TracerClose(void);
1355
1356/**
1357 * Perform an I/O request on the tracer.
1358 *
1359 * @returns VBox status.
1360 * @param uCmd The tracer command.
1361 * @param uArg The argument.
1362 * @param piRetVal Where to store the tracer return value.
1363 */
1364SUPR3DECL(int) SUPR3TracerIoCtl(uintptr_t uCmd, uintptr_t uArg, int32_t *piRetVal);
1365
1366/**
1367 * Registers the user module with the tracer.
1368 *
1369 * @returns VBox status code.
1370 * @param hModNative Native module handle. Pass ~(uintptr_t)0 if not
1371 * at hand.
1372 * @param pszModule The module name.
1373 * @param pVtgHdr The VTG header.
1374 * @param uVtgHdrAddr The address to which the VTG header is loaded
1375 * in the relevant execution context.
1376 * @param fFlags See SUP_TRACER_UMOD_FLAGS_XXX
1377 */
1378SUPR3DECL(int) SUPR3TracerRegisterModule(uintptr_t hModNative, const char *pszModule, struct VTGOBJHDR *pVtgHdr,
1379 RTUINTPTR uVtgHdrAddr, uint32_t fFlags);
1380
1381/**
1382 * Deregisters the user module.
1383 *
1384 * @returns VBox status code.
1385 * @param pVtgHdr The VTG header.
1386 */
1387SUPR3DECL(int) SUPR3TracerDeregisterModule(struct VTGOBJHDR *pVtgHdr);
1388
1389/**
1390 * Fire the probe.
1391 *
1392 * @param pVtgProbeLoc The probe location record.
1393 * @param uArg0 Raw probe argument 0.
1394 * @param uArg1 Raw probe argument 1.
1395 * @param uArg2 Raw probe argument 2.
1396 * @param uArg3 Raw probe argument 3.
1397 * @param uArg4 Raw probe argument 4.
1398 */
1399SUPDECL(void) SUPTracerFireProbe(struct VTGPROBELOC *pVtgProbeLoc, uintptr_t uArg0, uintptr_t uArg1, uintptr_t uArg2,
1400 uintptr_t uArg3, uintptr_t uArg4);
1401
1402
1403/**
1404 * Attempts to read the value of an MSR.
1405 *
1406 * @returns VBox status code.
1407 * @param uMsr The MSR to read.
1408 * @param idCpu The CPU to read it on, NIL_RTCPUID if it doesn't
1409 * matter which CPU.
1410 * @param puValue Where to return the value.
1411 * @param pfGp Where to store the \#GP indicator for the read
1412 * operation.
1413 */
1414SUPR3DECL(int) SUPR3MsrProberRead(uint32_t uMsr, RTCPUID idCpu, uint64_t *puValue, bool *pfGp);
1415
1416/**
1417 * Attempts to write to an MSR.
1418 *
1419 * @returns VBox status code.
1420 * @param uMsr The MSR to write to.
1421 * @param idCpu The CPU to wrtie it on, NIL_RTCPUID if it
1422 * doesn't matter which CPU.
1423 * @param uValue The value to write.
1424 * @param pfGp Where to store the \#GP indicator for the write
1425 * operation.
1426 */
1427SUPR3DECL(int) SUPR3MsrProberWrite(uint32_t uMsr, RTCPUID idCpu, uint64_t uValue, bool *pfGp);
1428
1429/**
1430 * Attempts to modify the value of an MSR.
1431 *
1432 * @returns VBox status code.
1433 * @param uMsr The MSR to modify.
1434 * @param idCpu The CPU to modify it on, NIL_RTCPUID if it
1435 * doesn't matter which CPU.
1436 * @param fAndMask The bits to keep in the current MSR value.
1437 * @param fOrMask The bits to set before writing.
1438 * @param pResult The result buffer.
1439 */
1440SUPR3DECL(int) SUPR3MsrProberModify(uint32_t uMsr, RTCPUID idCpu, uint64_t fAndMask, uint64_t fOrMask,
1441 PSUPMSRPROBERMODIFYRESULT pResult);
1442
1443/**
1444 * Attempts to modify the value of an MSR, extended version.
1445 *
1446 * @returns VBox status code.
1447 * @param uMsr The MSR to modify.
1448 * @param idCpu The CPU to modify it on, NIL_RTCPUID if it
1449 * doesn't matter which CPU.
1450 * @param fAndMask The bits to keep in the current MSR value.
1451 * @param fOrMask The bits to set before writing.
1452 * @param fFaster If set to @c true some cache/tlb invalidation is
1453 * skipped, otherwise behave like
1454 * SUPR3MsrProberModify.
1455 * @param pResult The result buffer.
1456 */
1457SUPR3DECL(int) SUPR3MsrProberModifyEx(uint32_t uMsr, RTCPUID idCpu, uint64_t fAndMask, uint64_t fOrMask, bool fFaster,
1458 PSUPMSRPROBERMODIFYRESULT pResult);
1459
1460/**
1461 * Resume built-in keyboard on MacBook Air and Pro hosts.
1462 *
1463 * @returns VBox status code.
1464 */
1465SUPR3DECL(int) SUPR3ResumeSuspendedKeyboards(void);
1466
1467
1468/**
1469 * Measure the TSC-delta for the specified CPU.
1470 *
1471 * @returns VBox status code.
1472 * @param idCpu The CPU to measure the TSC-delta for.
1473 * @param fAsync Whether the measurement is asynchronous, returns
1474 * immediately after signalling a measurement
1475 * request.
1476 * @param fForce Whether to perform a measurement even if the
1477 * specified CPU has a (possibly) valid TSC delta.
1478 * @param cRetries Number of times to retry failed delta
1479 * measurements.
1480 */
1481SUPR3DECL(int) SUPR3TscDeltaMeasure(RTCPUID idCpu, bool fAsync, bool fForce, uint8_t cRetries);
1482
1483/**
1484 * Reads the delta-adjust TSC value.
1485 *
1486 * @returns VBox status code.
1487 * @param puTsc Where to store the read TSC value.
1488 * @param pidApic Where to store the APIC ID of the CPU where the TSC
1489 * was read (optional, can be NULL).
1490 */
1491SUPR3DECL(int) SUPR3ReadTsc(uint64_t *puTsc, uint16_t *pidApic);
1492
1493/** @} */
1494#endif /* IN_RING3 */
1495
1496
1497/**
1498 * Gets the descriptive GIP mode name.
1499 *
1500 * @returns The name.
1501 * @param pGip Pointer to the GIP.
1502 */
1503DECLINLINE(const char *) SUPGetGIPModeName(PSUPGLOBALINFOPAGE pGip)
1504{
1505 AssertReturn(pGip, NULL);
1506 switch (pGip->u32Mode)
1507 {
1508 case SUPGIPMODE_INVARIANT_TSC: return "Invariant";
1509 case SUPGIPMODE_SYNC_TSC: return "Synchronous";
1510 case SUPGIPMODE_ASYNC_TSC: return "Asynchronous";
1511 case SUPGIPMODE_INVALID: return "Invalid";
1512 default: return "???";
1513 }
1514}
1515
1516
1517/**
1518 * Checks if the provided TSC frequency is close enough to the computed TSC
1519 * frequency of the host.
1520 *
1521 * @returns true if it's compatible, false otherwise.
1522 */
1523DECLINLINE(bool) SUPIsTscFreqCompatible(uint64_t u64CpuHz)
1524{
1525 PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
1526 if ( pGip
1527 && pGip->u32Mode == SUPGIPMODE_INVARIANT_TSC)
1528 {
1529 if (pGip->u64CpuHz != u64CpuHz)
1530 {
1531 /* Arbitrary tolerance threshold, tweak later if required, perhaps
1532 more tolerance on lower frequencies and less tolerance on higher. */
1533 uint64_t uLo = (pGip->u64CpuHz << 10) / 1025;
1534 uint64_t uHi = pGip->u64CpuHz + (pGip->u64CpuHz - uLo);
1535 if ( u64CpuHz < uLo
1536 || u64CpuHz > uHi)
1537 return false;
1538 }
1539 return true;
1540 }
1541 return false;
1542}
1543
1544#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
1545
1546/**
1547 * Applies the TSC delta to the supplied raw TSC value.
1548 *
1549 * @returns VBox status code.
1550 * @param pGip Pointer to the GIP.
1551 * @param puTsc Pointer to a valid TSC value before the TSC delta has been applied.
1552 * @param idApic The APIC ID of the CPU @c puTsc corresponds to.
1553 * @param fDeltaApplied Where to store whether the TSC delta was succesfully
1554 * applied or not (optional, can be NULL).
1555 *
1556 * @remarks Maybe called with interrupts disabled in ring-0!
1557 *
1558 * @note If you change the delta calculation made here, make sure to update
1559 * the assembly version in sup.mac! Also update supdrvGipMpEvent()
1560 * while re-adjusting deltas while choosing a new GIP master.
1561 */
1562DECLINLINE(int) SUPTscDeltaApply(PSUPGLOBALINFOPAGE pGip, uint64_t *puTsc, uint16_t idApic, bool *pfDeltaApplied)
1563{
1564 PSUPGIPCPU pGipCpu;
1565 uint16_t iCpu;
1566
1567 /* Validate. */
1568 Assert(puTsc);
1569 Assert(pGip);
1570 Assert(GIP_ARE_TSC_DELTAS_APPLICABLE(pGip));
1571
1572 /** @todo convert these to AssertMsg() after sufficient testing before public
1573 * release. */
1574 AssertMsgReturn(idApic < RT_ELEMENTS(pGip->aiCpuFromApicId), ("idApic=%u\n", idApic), VERR_INVALID_CPU_ID);
1575 iCpu = pGip->aiCpuFromApicId[idApic];
1576 AssertMsgReturn(iCpu < pGip->cCpus, ("iCpu=%u cCpus=%u\n", iCpu, pGip->cCpus), VERR_INVALID_CPU_INDEX);
1577 pGipCpu = &pGip->aCPUs[iCpu];
1578 Assert(pGipCpu);
1579
1580 if (RT_LIKELY(pGipCpu->i64TSCDelta != INT64_MAX))
1581 {
1582 *puTsc -= pGipCpu->i64TSCDelta;
1583 if (pfDeltaApplied)
1584 *pfDeltaApplied = true;
1585 }
1586 else if (pfDeltaApplied)
1587 *pfDeltaApplied = false;
1588
1589 return VINF_SUCCESS;
1590}
1591
1592
1593/**
1594 * Gets the delta-adjusted TSC.
1595 *
1596 * Must only be called when GIP mode is invariant (i.e. when TSC deltas are
1597 * likely to be computed and available). In other GIP modes, like async, we
1598 * don't bother with computing TSC deltas and therefore it is meaningless to
1599 * call this function, use SUPReadTSC() instead.
1600 *
1601 * @returns VBox status code.
1602 * @param puTsc Where to store the normalized TSC value.
1603 * @param pidApic Where to store the APIC ID of the CPU where the TSC
1604 * was read (optional, can be NULL). This is updated
1605 * even if the function fails with
1606 * VERR_SUPDRV_TSC_READ_FAILED. Not guaranteed to be
1607 * updated for other failures.
1608 *
1609 * @remarks May be called with interrupts disabled in ring-0!
1610 */
1611DECLINLINE(int) SUPGetTsc(uint64_t *puTsc, uint16_t *pidApic)
1612{
1613# ifdef IN_RING3
1614 Assert(GIP_ARE_TSC_DELTAS_APPLICABLE(g_pSUPGlobalInfoPage));
1615 return SUPR3ReadTsc(puTsc, pidApic);
1616# else
1617 RTCCUINTREG uFlags;
1618 uint16_t idApic;
1619 bool fDeltaApplied;
1620 int rc;
1621
1622 /* Validate. */
1623 Assert(puTsc);
1624
1625 uFlags = ASMIntDisableFlags();
1626 idApic = ASMGetApicId(); /* Doubles as serialization. */
1627 *puTsc = ASMReadTSC();
1628 ASMSetFlags(uFlags);
1629
1630 if (pidApic)
1631 *pidApic = idApic;
1632
1633 rc = SUPTscDeltaApply(g_pSUPGlobalInfoPage, puTsc, idApic, &fDeltaApplied);
1634 AssertRC(rc);
1635 return fDeltaApplied ? VINF_SUCCESS : VERR_SUPDRV_TSC_READ_FAILED;
1636# endif
1637}
1638
1639
1640/**
1641 * Reads the host TSC value.
1642 *
1643 * If applicable, normalizes the host TSC value with intercpu TSC deltas.
1644 *
1645 * @returns the TSC value.
1646 *
1647 * @remarks Requires GIP to be initialized.
1648 */
1649DECLINLINE(uint64_t) SUPReadTsc(void)
1650{
1651 if (GIP_ARE_TSC_DELTAS_APPLICABLE(g_pSUPGlobalInfoPage))
1652 {
1653 uint64_t u64Tsc = UINT64_MAX;
1654 int rc = SUPGetTsc(&u64Tsc, NULL /* pidApic */);
1655 NOREF(rc);
1656#ifdef DEBUG_ramshankar
1657 AssertRC(rc);
1658#endif
1659 return u64Tsc;
1660 }
1661 return ASMReadTSC();
1662}
1663
1664#endif /* X86 || AMD64 */
1665
1666
1667/** @name User mode module flags (SUPR3TracerRegisterModule & SUP_IOCTL_TRACER_UMOD_REG).
1668 * @{ */
1669/** Executable image. */
1670#define SUP_TRACER_UMOD_FLAGS_EXE UINT32_C(1)
1671/** Shared library (DLL, DYLIB, SO, etc). */
1672#define SUP_TRACER_UMOD_FLAGS_SHARED UINT32_C(2)
1673/** Image type mask. */
1674#define SUP_TRACER_UMOD_FLAGS_TYPE_MASK UINT32_C(3)
1675/** @} */
1676
1677
1678#ifdef IN_RING0
1679/** @defgroup grp_sup_r0 SUP Host Context Ring-0 API
1680 * @{
1681 */
1682
1683/**
1684 * Security objectype.
1685 */
1686typedef enum SUPDRVOBJTYPE
1687{
1688 /** The usual invalid object. */
1689 SUPDRVOBJTYPE_INVALID = 0,
1690 /** A Virtual Machine instance. */
1691 SUPDRVOBJTYPE_VM,
1692 /** Internal network. */
1693 SUPDRVOBJTYPE_INTERNAL_NETWORK,
1694 /** Internal network interface. */
1695 SUPDRVOBJTYPE_INTERNAL_NETWORK_INTERFACE,
1696 /** Single release event semaphore. */
1697 SUPDRVOBJTYPE_SEM_EVENT,
1698 /** Multiple release event semaphore. */
1699 SUPDRVOBJTYPE_SEM_EVENT_MULTI,
1700 /** Raw PCI device. */
1701 SUPDRVOBJTYPE_RAW_PCI_DEVICE,
1702 /** The first invalid object type in this end. */
1703 SUPDRVOBJTYPE_END,
1704 /** The usual 32-bit type size hack. */
1705 SUPDRVOBJTYPE_32_BIT_HACK = 0x7ffffff
1706} SUPDRVOBJTYPE;
1707
1708/**
1709 * Object destructor callback.
1710 * This is called for reference counted objectes when the count reaches 0.
1711 *
1712 * @param pvObj The object pointer.
1713 * @param pvUser1 The first user argument.
1714 * @param pvUser2 The second user argument.
1715 */
1716typedef DECLCALLBACK(void) FNSUPDRVDESTRUCTOR(void *pvObj, void *pvUser1, void *pvUser2);
1717/** Pointer to a FNSUPDRVDESTRUCTOR(). */
1718typedef FNSUPDRVDESTRUCTOR *PFNSUPDRVDESTRUCTOR;
1719
1720SUPR0DECL(void *) SUPR0ObjRegister(PSUPDRVSESSION pSession, SUPDRVOBJTYPE enmType, PFNSUPDRVDESTRUCTOR pfnDestructor, void *pvUser1, void *pvUser2);
1721SUPR0DECL(int) SUPR0ObjAddRef(void *pvObj, PSUPDRVSESSION pSession);
1722SUPR0DECL(int) SUPR0ObjAddRefEx(void *pvObj, PSUPDRVSESSION pSession, bool fNoBlocking);
1723SUPR0DECL(int) SUPR0ObjRelease(void *pvObj, PSUPDRVSESSION pSession);
1724SUPR0DECL(int) SUPR0ObjVerifyAccess(void *pvObj, PSUPDRVSESSION pSession, const char *pszObjName);
1725
1726SUPR0DECL(int) SUPR0LockMem(PSUPDRVSESSION pSession, RTR3PTR pvR3, uint32_t cPages, PRTHCPHYS paPages);
1727SUPR0DECL(int) SUPR0UnlockMem(PSUPDRVSESSION pSession, RTR3PTR pvR3);
1728SUPR0DECL(int) SUPR0ContAlloc(PSUPDRVSESSION pSession, uint32_t cPages, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PRTHCPHYS pHCPhys);
1729SUPR0DECL(int) SUPR0ContFree(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr);
1730SUPR0DECL(int) SUPR0LowAlloc(PSUPDRVSESSION pSession, uint32_t cPages, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PRTHCPHYS paPages);
1731SUPR0DECL(int) SUPR0LowFree(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr);
1732SUPR0DECL(int) SUPR0MemAlloc(PSUPDRVSESSION pSession, uint32_t cb, PRTR0PTR ppvR0, PRTR3PTR ppvR3);
1733SUPR0DECL(int) SUPR0MemGetPhys(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr, PSUPPAGE paPages);
1734SUPR0DECL(int) SUPR0MemFree(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr);
1735SUPR0DECL(int) SUPR0PageAllocEx(PSUPDRVSESSION pSession, uint32_t cPages, uint32_t fFlags, PRTR3PTR ppvR3, PRTR0PTR ppvR0, PRTHCPHYS paPages);
1736SUPR0DECL(int) SUPR0PageMapKernel(PSUPDRVSESSION pSession, RTR3PTR pvR3, uint32_t offSub, uint32_t cbSub, uint32_t fFlags, PRTR0PTR ppvR0);
1737SUPR0DECL(int) SUPR0PageProtect(PSUPDRVSESSION pSession, RTR3PTR pvR3, RTR0PTR pvR0, uint32_t offSub, uint32_t cbSub, uint32_t fProt);
1738SUPR0DECL(int) SUPR0PageFree(PSUPDRVSESSION pSession, RTR3PTR pvR3);
1739SUPR0DECL(int) SUPR0GipMap(PSUPDRVSESSION pSession, PRTR3PTR ppGipR3, PRTHCPHYS pHCPhysGip);
1740SUPR0DECL(int) SUPR0GetSvmUsability(bool fInitSvm);
1741SUPR0DECL(int) SUPR0GetVmxUsability(bool *pfIsSmxModeAmbiguous);
1742SUPR0DECL(int) SUPR0QueryVTCaps(PSUPDRVSESSION pSession, uint32_t *pfCaps);
1743SUPR0DECL(int) SUPR0GipUnmap(PSUPDRVSESSION pSession);
1744SUPR0DECL(int) SUPR0Printf(const char *pszFormat, ...);
1745SUPR0DECL(SUPPAGINGMODE) SUPR0GetPagingMode(void);
1746SUPR0DECL(uint32_t) SUPR0GetKernelFeatures(void);
1747SUPR0DECL(int) SUPR0EnableVTx(bool fEnable);
1748SUPR0DECL(bool) SUPR0SuspendVTxOnCpu(void);
1749SUPR0DECL(void) SUPR0ResumeVTxOnCpu(bool fSuspended);
1750
1751/** @name Absolute symbols
1752 * Take the address of these, don't try call them.
1753 * @{ */
1754SUPR0DECL(void) SUPR0AbsIs64bit(void);
1755SUPR0DECL(void) SUPR0Abs64bitKernelCS(void);
1756SUPR0DECL(void) SUPR0Abs64bitKernelSS(void);
1757SUPR0DECL(void) SUPR0Abs64bitKernelDS(void);
1758SUPR0DECL(void) SUPR0AbsKernelCS(void);
1759SUPR0DECL(void) SUPR0AbsKernelSS(void);
1760SUPR0DECL(void) SUPR0AbsKernelDS(void);
1761SUPR0DECL(void) SUPR0AbsKernelES(void);
1762SUPR0DECL(void) SUPR0AbsKernelFS(void);
1763SUPR0DECL(void) SUPR0AbsKernelGS(void);
1764/** @} */
1765
1766/**
1767 * Support driver component factory.
1768 *
1769 * Component factories are registered by drivers that provides services
1770 * such as the host network interface filtering and access to the host
1771 * TCP/IP stack.
1772 *
1773 * @remark Module dependencies and making sure that a component doesn't
1774 * get unloaded while in use, is the sole responsibility of the
1775 * driver/kext/whatever implementing the component.
1776 */
1777typedef struct SUPDRVFACTORY
1778{
1779 /** The (unique) name of the component factory. */
1780 char szName[56];
1781 /**
1782 * Queries a factory interface.
1783 *
1784 * The factory interface is specific to each component and will be be
1785 * found in the header(s) for the component alongside its UUID.
1786 *
1787 * @returns Pointer to the factory interfaces on success, NULL on failure.
1788 *
1789 * @param pSupDrvFactory Pointer to this structure.
1790 * @param pSession The SUPDRV session making the query.
1791 * @param pszInterfaceUuid The UUID of the factory interface.
1792 */
1793 DECLR0CALLBACKMEMBER(void *, pfnQueryFactoryInterface,(struct SUPDRVFACTORY const *pSupDrvFactory, PSUPDRVSESSION pSession, const char *pszInterfaceUuid));
1794} SUPDRVFACTORY;
1795/** Pointer to a support driver factory. */
1796typedef SUPDRVFACTORY *PSUPDRVFACTORY;
1797/** Pointer to a const support driver factory. */
1798typedef SUPDRVFACTORY const *PCSUPDRVFACTORY;
1799
1800SUPR0DECL(int) SUPR0ComponentRegisterFactory(PSUPDRVSESSION pSession, PCSUPDRVFACTORY pFactory);
1801SUPR0DECL(int) SUPR0ComponentDeregisterFactory(PSUPDRVSESSION pSession, PCSUPDRVFACTORY pFactory);
1802SUPR0DECL(int) SUPR0ComponentQueryFactory(PSUPDRVSESSION pSession, const char *pszName, const char *pszInterfaceUuid, void **ppvFactoryIf);
1803
1804
1805/** @name Tracing
1806 * @{ */
1807
1808/**
1809 * Tracer data associated with a provider.
1810 */
1811typedef union SUPDRVTRACERDATA
1812{
1813 /** Generic */
1814 uint64_t au64[2];
1815
1816 /** DTrace data. */
1817 struct
1818 {
1819 /** Provider ID. */
1820 uintptr_t idProvider;
1821 /** The number of trace points provided. */
1822 uint32_t volatile cProvidedProbes;
1823 /** Whether we've invalidated this bugger. */
1824 bool fZombie;
1825 } DTrace;
1826} SUPDRVTRACERDATA;
1827/** Pointer to the tracer data associated with a provider. */
1828typedef SUPDRVTRACERDATA *PSUPDRVTRACERDATA;
1829
1830/**
1831 * Probe location info for ring-0.
1832 *
1833 * Since we cannot trust user tracepoint modules, we need to duplicate the probe
1834 * ID and enabled flag in ring-0.
1835 */
1836typedef struct SUPDRVPROBELOC
1837{
1838 /** The probe ID. */
1839 uint32_t idProbe;
1840 /** Whether it's enabled or not. */
1841 bool fEnabled;
1842} SUPDRVPROBELOC;
1843/** Pointer to a ring-0 probe location record. */
1844typedef SUPDRVPROBELOC *PSUPDRVPROBELOC;
1845
1846/**
1847 * Probe info for ring-0.
1848 *
1849 * Since we cannot trust user tracepoint modules, we need to duplicate the
1850 * probe enable count.
1851 */
1852typedef struct SUPDRVPROBEINFO
1853{
1854 /** The number of times this probe has been enabled. */
1855 uint32_t volatile cEnabled;
1856} SUPDRVPROBEINFO;
1857/** Pointer to a ring-0 probe info record. */
1858typedef SUPDRVPROBEINFO *PSUPDRVPROBEINFO;
1859
1860/**
1861 * Support driver tracepoint provider core.
1862 */
1863typedef struct SUPDRVVDTPROVIDERCORE
1864{
1865 /** The tracer data member. */
1866 SUPDRVTRACERDATA TracerData;
1867 /** Pointer to the provider name (a copy that's always available). */
1868 const char *pszName;
1869 /** Pointer to the module name (a copy that's always available). */
1870 const char *pszModName;
1871
1872 /** The provider descriptor. */
1873 struct VTGDESCPROVIDER *pDesc;
1874 /** The VTG header. */
1875 struct VTGOBJHDR *pHdr;
1876
1877 /** The size of the entries in the pvProbeLocsEn table. */
1878 uint8_t cbProbeLocsEn;
1879 /** The actual module bit count (corresponds to cbProbeLocsEn). */
1880 uint8_t cBits;
1881 /** Set if this is a Umod, otherwise clear.. */
1882 bool fUmod;
1883 /** Explicit alignment padding (paranoia). */
1884 uint8_t abAlignment[ARCH_BITS == 32 ? 1 : 5];
1885
1886 /** The probe locations used for descriptive purposes. */
1887 struct VTGPROBELOC const *paProbeLocsRO;
1888 /** Pointer to the probe location array where the enable flag needs
1889 * flipping. For kernel providers, this will always be SUPDRVPROBELOC,
1890 * while user providers can either be 32-bit or 64-bit. Use
1891 * cbProbeLocsEn to calculate the address of an entry. */
1892 void *pvProbeLocsEn;
1893 /** Pointer to the probe array containing the enabled counts. */
1894 uint32_t *pacProbeEnabled;
1895
1896 /** The ring-0 probe location info for user tracepoint modules.
1897 * This is NULL if fUmod is false. */
1898 PSUPDRVPROBELOC paR0ProbeLocs;
1899 /** The ring-0 probe info for user tracepoint modules.
1900 * This is NULL if fUmod is false. */
1901 PSUPDRVPROBEINFO paR0Probes;
1902
1903} SUPDRVVDTPROVIDERCORE;
1904/** Pointer to a tracepoint provider core structure. */
1905typedef SUPDRVVDTPROVIDERCORE *PSUPDRVVDTPROVIDERCORE;
1906
1907/** Pointer to a tracer registration record. */
1908typedef struct SUPDRVTRACERREG const *PCSUPDRVTRACERREG;
1909/**
1910 * Support driver tracer registration record.
1911 */
1912typedef struct SUPDRVTRACERREG
1913{
1914 /** Magic value (SUPDRVTRACERREG_MAGIC). */
1915 uint32_t u32Magic;
1916 /** Version (SUPDRVTRACERREG_VERSION). */
1917 uint32_t u32Version;
1918
1919 /**
1920 * Fire off a kernel probe.
1921 *
1922 * @param pVtgProbeLoc The probe location record.
1923 * @param uArg0 The first raw probe argument.
1924 * @param uArg1 The second raw probe argument.
1925 * @param uArg2 The third raw probe argument.
1926 * @param uArg3 The fourth raw probe argument.
1927 * @param uArg4 The fifth raw probe argument.
1928 *
1929 * @remarks SUPR0TracerFireProbe will do a tail jump thru this member, so
1930 * no extra stack frames will be added.
1931 * @remarks This does not take a 'this' pointer argument because it doesn't map
1932 * well onto VTG or DTrace.
1933 *
1934 */
1935 DECLR0CALLBACKMEMBER(void, pfnProbeFireKernel, (struct VTGPROBELOC *pVtgProbeLoc, uintptr_t uArg0, uintptr_t uArg1, uintptr_t uArg2,
1936 uintptr_t uArg3, uintptr_t uArg4));
1937
1938 /**
1939 * Fire off a user-mode probe.
1940 *
1941 * @param pThis Pointer to the registration record.
1942 *
1943 * @param pVtgProbeLoc The probe location record.
1944 * @param pSession The user session.
1945 * @param pCtx The usermode context info.
1946 * @param pVtgHdr The VTG header (read-only).
1947 * @param pProbeLocRO The read-only probe location record .
1948 */
1949 DECLR0CALLBACKMEMBER(void, pfnProbeFireUser, (PCSUPDRVTRACERREG pThis, PSUPDRVSESSION pSession, PCSUPDRVTRACERUSRCTX pCtx,
1950 struct VTGOBJHDR const *pVtgHdr, struct VTGPROBELOC const *pProbeLocRO));
1951
1952 /**
1953 * Opens up the tracer.
1954 *
1955 * @returns VBox status code.
1956 * @param pThis Pointer to the registration record.
1957 * @param pSession The session doing the opening.
1958 * @param uCookie A cookie (magic) unique to the tracer, so it can
1959 * fend off incompatible clients.
1960 * @param uArg Tracer specific argument.
1961 * @param puSessionData Pointer to the session data variable. This must be
1962 * set to a non-zero value on success.
1963 */
1964 DECLR0CALLBACKMEMBER(int, pfnTracerOpen, (PCSUPDRVTRACERREG pThis, PSUPDRVSESSION pSession, uint32_t uCookie, uintptr_t uArg,
1965 uintptr_t *puSessionData));
1966
1967 /**
1968 * I/O control style tracer communication method.
1969 *
1970 *
1971 * @returns VBox status code.
1972 * @param pThis Pointer to the registration record.
1973 * @param pSession The session.
1974 * @param uSessionData The session data value.
1975 * @param uCmd The tracer specific command.
1976 * @param uArg The tracer command specific argument.
1977 * @param piRetVal The tracer specific return value.
1978 */
1979 DECLR0CALLBACKMEMBER(int, pfnTracerIoCtl, (PCSUPDRVTRACERREG pThis, PSUPDRVSESSION pSession, uintptr_t uSessionData,
1980 uintptr_t uCmd, uintptr_t uArg, int32_t *piRetVal));
1981
1982 /**
1983 * Cleans up data the tracer has associated with a session.
1984 *
1985 * @param pThis Pointer to the registration record.
1986 * @param pSession The session handle.
1987 * @param uSessionData The data assoicated with the session.
1988 */
1989 DECLR0CALLBACKMEMBER(void, pfnTracerClose, (PCSUPDRVTRACERREG pThis, PSUPDRVSESSION pSession, uintptr_t uSessionData));
1990
1991 /**
1992 * Registers a provider.
1993 *
1994 * @returns VBox status code.
1995 * @param pThis Pointer to the registration record.
1996 * @param pCore The provider core data.
1997 *
1998 * @todo Kernel vs. Userland providers.
1999 */
2000 DECLR0CALLBACKMEMBER(int, pfnProviderRegister, (PCSUPDRVTRACERREG pThis, PSUPDRVVDTPROVIDERCORE pCore));
2001
2002 /**
2003 * Attempts to deregisters a provider.
2004 *
2005 * @returns VINF_SUCCESS or VERR_TRY_AGAIN. If the latter, the provider
2006 * should be made as harmless as possible before returning as the
2007 * VTG object and associated code will be unloaded upon return.
2008 *
2009 * @param pThis Pointer to the registration record.
2010 * @param pCore The provider core data.
2011 */
2012 DECLR0CALLBACKMEMBER(int, pfnProviderDeregister, (PCSUPDRVTRACERREG pThis, PSUPDRVVDTPROVIDERCORE pCore));
2013
2014 /**
2015 * Make another attempt at unregister a busy provider.
2016 *
2017 * @returns VINF_SUCCESS or VERR_TRY_AGAIN.
2018 * @param pThis Pointer to the registration record.
2019 * @param pCore The provider core data.
2020 */
2021 DECLR0CALLBACKMEMBER(int, pfnProviderDeregisterZombie, (PCSUPDRVTRACERREG pThis, PSUPDRVVDTPROVIDERCORE pCore));
2022
2023 /** End marker (SUPDRVTRACERREG_MAGIC). */
2024 uintptr_t uEndMagic;
2025} SUPDRVTRACERREG;
2026
2027/** Tracer magic (Kenny Garrett). */
2028#define SUPDRVTRACERREG_MAGIC UINT32_C(0x19601009)
2029/** Tracer registration structure version. */
2030#define SUPDRVTRACERREG_VERSION RT_MAKE_U32(0, 1)
2031
2032/** Pointer to a trace helper structure. */
2033typedef struct SUPDRVTRACERHLP const *PCSUPDRVTRACERHLP;
2034/**
2035 * Helper structure.
2036 */
2037typedef struct SUPDRVTRACERHLP
2038{
2039 /** The structure version (SUPDRVTRACERHLP_VERSION). */
2040 uintptr_t uVersion;
2041
2042 /** @todo ... */
2043
2044 /** End marker (SUPDRVTRACERHLP_VERSION) */
2045 uintptr_t uEndVersion;
2046} SUPDRVTRACERHLP;
2047/** Tracer helper structure version. */
2048#define SUPDRVTRACERHLP_VERSION RT_MAKE_U32(0, 1)
2049
2050SUPR0DECL(int) SUPR0TracerRegisterImpl(void *hMod, PSUPDRVSESSION pSession, PCSUPDRVTRACERREG pReg, PCSUPDRVTRACERHLP *ppHlp);
2051SUPR0DECL(int) SUPR0TracerDeregisterImpl(void *hMod, PSUPDRVSESSION pSession);
2052SUPR0DECL(int) SUPR0TracerRegisterDrv(PSUPDRVSESSION pSession, struct VTGOBJHDR *pVtgHdr, const char *pszName);
2053SUPR0DECL(void) SUPR0TracerDeregisterDrv(PSUPDRVSESSION pSession);
2054SUPR0DECL(int) SUPR0TracerRegisterModule(void *hMod, struct VTGOBJHDR *pVtgHdr);
2055SUPR0DECL(void) SUPR0TracerFireProbe(struct VTGPROBELOC *pVtgProbeLoc, uintptr_t uArg0, uintptr_t uArg1, uintptr_t uArg2,
2056 uintptr_t uArg3, uintptr_t uArg4);
2057SUPR0DECL(void) SUPR0TracerUmodProbeFire(PSUPDRVSESSION pSession, PSUPDRVTRACERUSRCTX pCtx);
2058/** @} */
2059
2060
2061/**
2062 * Service request callback function.
2063 *
2064 * @returns VBox status code.
2065 * @param pSession The caller's session.
2066 * @param u64Arg 64-bit integer argument.
2067 * @param pReqHdr The request header. Input / Output. Optional.
2068 */
2069typedef DECLCALLBACK(int) FNSUPR0SERVICEREQHANDLER(PSUPDRVSESSION pSession, uint32_t uOperation,
2070 uint64_t u64Arg, PSUPR0SERVICEREQHDR pReqHdr);
2071/** Pointer to a FNR0SERVICEREQHANDLER(). */
2072typedef R0PTRTYPE(FNSUPR0SERVICEREQHANDLER *) PFNSUPR0SERVICEREQHANDLER;
2073
2074
2075/** @defgroup grp_sup_r0_idc The IDC Interface
2076 * @{
2077 */
2078
2079/** The current SUPDRV IDC version.
2080 * This follows the usual high word / low word rules, i.e. high word is the
2081 * major number and it signifies incompatible interface changes. */
2082#define SUPDRV_IDC_VERSION UINT32_C(0x00010000)
2083
2084/**
2085 * Inter-Driver Communication Handle.
2086 */
2087typedef union SUPDRVIDCHANDLE
2088{
2089 /** Padding for opaque usage.
2090 * Must be greater or equal in size than the private struct. */
2091 void *apvPadding[4];
2092#ifdef SUPDRVIDCHANDLEPRIVATE_DECLARED
2093 /** The private view. */
2094 struct SUPDRVIDCHANDLEPRIVATE s;
2095#endif
2096} SUPDRVIDCHANDLE;
2097/** Pointer to a handle. */
2098typedef SUPDRVIDCHANDLE *PSUPDRVIDCHANDLE;
2099
2100SUPR0DECL(int) SUPR0IdcOpen(PSUPDRVIDCHANDLE pHandle, uint32_t uReqVersion, uint32_t uMinVersion,
2101 uint32_t *puSessionVersion, uint32_t *puDriverVersion, uint32_t *puDriverRevision);
2102SUPR0DECL(int) SUPR0IdcCall(PSUPDRVIDCHANDLE pHandle, uint32_t iReq, void *pvReq, uint32_t cbReq);
2103SUPR0DECL(int) SUPR0IdcClose(PSUPDRVIDCHANDLE pHandle);
2104SUPR0DECL(PSUPDRVSESSION) SUPR0IdcGetSession(PSUPDRVIDCHANDLE pHandle);
2105SUPR0DECL(int) SUPR0IdcComponentRegisterFactory(PSUPDRVIDCHANDLE pHandle, PCSUPDRVFACTORY pFactory);
2106SUPR0DECL(int) SUPR0IdcComponentDeregisterFactory(PSUPDRVIDCHANDLE pHandle, PCSUPDRVFACTORY pFactory);
2107
2108/** @} */
2109
2110/** @name Ring-0 module entry points.
2111 *
2112 * These can be exported by ring-0 modules SUP are told to load.
2113 *
2114 * @{ */
2115DECLEXPORT(int) ModuleInit(void *hMod);
2116DECLEXPORT(void) ModuleTerm(void *hMod);
2117/** @} */
2118
2119
2120/** @} */
2121#endif
2122
2123
2124/** @name Trust Anchors and Certificates
2125 * @{ */
2126
2127/**
2128 * Trust anchor table entry (in generated Certificates.cpp).
2129 */
2130typedef struct SUPTAENTRY
2131{
2132 /** Pointer to the raw bytes. */
2133 const unsigned char *pch;
2134 /** Number of bytes. */
2135 unsigned cb;
2136} SUPTAENTRY;
2137/** Pointer to a trust anchor table entry. */
2138typedef SUPTAENTRY const *PCSUPTAENTRY;
2139
2140/** Macro for simplifying generating the trust anchor tables. */
2141#define SUPTAENTRY_GEN(a_abTA) { &a_abTA[0], sizeof(a_abTA) }
2142
2143/** All certificates we know. */
2144extern SUPTAENTRY const g_aSUPAllTAs[];
2145/** Number of entries in g_aSUPAllTAs. */
2146extern unsigned const g_cSUPAllTAs;
2147
2148/** Software publisher certificate roots (Authenticode). */
2149extern SUPTAENTRY const g_aSUPSpcRootTAs[];
2150/** Number of entries in g_aSUPSpcRootTAs. */
2151extern unsigned const g_cSUPSpcRootTAs;
2152
2153/** Kernel root certificates used by Windows. */
2154extern SUPTAENTRY const g_aSUPNtKernelRootTAs[];
2155/** Number of entries in g_aSUPNtKernelRootTAs. */
2156extern unsigned const g_cSUPNtKernelRootTAs;
2157
2158/** Timestamp root certificates trusted by Windows. */
2159extern SUPTAENTRY const g_aSUPTimestampTAs[];
2160/** Number of entries in g_aSUPTimestampTAs. */
2161extern unsigned const g_cSUPTimestampTAs;
2162
2163/** TAs we trust (the build certificate, Oracle VirtualBox). */
2164extern SUPTAENTRY const g_aSUPTrustedTAs[];
2165/** Number of entries in g_aSUPTrustedTAs. */
2166extern unsigned const g_cSUPTrustedTAs;
2167
2168/** Supplemental certificates, like cross signing certificates. */
2169extern SUPTAENTRY const g_aSUPSupplementalTAs[];
2170/** Number of entries in g_aSUPTrustedTAs. */
2171extern unsigned const g_cSUPSupplementalTAs;
2172
2173/** The build certificate. */
2174extern const unsigned char g_abSUPBuildCert[];
2175/** The size of the build certificate. */
2176extern const unsigned g_cbSUPBuildCert;
2177
2178/** @} */
2179
2180
2181/** @} */
2182
2183RT_C_DECLS_END
2184
2185#endif
2186
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