VirtualBox

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

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

Support: added SUPR3LockDownLoader()

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