VirtualBox

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

Last change on this file since 76461 was 76461, checked in by vboxsync, 6 years ago

SUPDrv: Added an ioctl for fetching hardware-virtualization MSRs.

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