VirtualBox

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

最後變更 在這個檔案從81161是 81161,由 vboxsync 提交於 5 年 前

SUP, VMM: bugref: 9562 Drop SUPR0GetRawModeUsability. No longer required with removal of raw-mode.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette