VirtualBox

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

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

VMM/TM: Rename TMMODE to TMTSCMODE and the corresponding enum and CFGM key. Fixed an endian issue in the assembly code.

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