VirtualBox

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

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

IPRT,TM: Implemented the get-cpu-number optimizations for the RTTimeNanoTS code.

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