VirtualBox

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

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

doxygen fixes.

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