VirtualBox

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

Last change on this file since 85123 was 85121, checked in by vboxsync, 4 years ago

iprt/cdefs.h: Refactored the typedef use of DECLCALLBACK as well as DECLCALLBACKMEMBER to wrap the whole expression, similar to the DECLR?CALLBACKMEMBER macros. This allows adding a throw() at the end when compiling with the VC++ compiler to indicate that the callbacks won't throw anything, so we can stop supressing the C5039 warning about passing functions that can potential throw C++ exceptions to extern C code that can't necessarily cope with such (unwind,++). Introduced a few _EX variations that allows specifying different/no calling convention too, as that's handy when dynamically resolving host APIs. Fixed numerous places missing DECLCALLBACK and such. Left two angry @todos regarding use of CreateThread. bugref:9794

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 93.9 KB
Line 
1/** @file
2 * SUP - Support Library. (HDrv)
3 */
4
5/*
6 * Copyright (C) 2006-2020 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef VBOX_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/** VT-x: VMCS shadowing is supported. */
887#define SUPVTCAPS_VTX_VMCS_SHADOWING RT_BIT(4)
888/** @} */
889
890/**
891 * Request for generic FNSUPR0SERVICEREQHANDLER calls.
892 */
893typedef struct SUPR0SERVICEREQHDR
894{
895 /** The magic. (SUPR0SERVICEREQHDR_MAGIC) */
896 uint32_t u32Magic;
897 /** The size of the request. */
898 uint32_t cbReq;
899} SUPR0SERVICEREQHDR;
900/** Pointer to a ring-0 service request header. */
901typedef SUPR0SERVICEREQHDR *PSUPR0SERVICEREQHDR;
902/** the SUPVMMR0REQHDR::u32Magic value (Esbjoern Svensson - E.S.P.). */
903#define SUPR0SERVICEREQHDR_MAGIC UINT32_C(0x19640416)
904
905
906/**
907 * Creates a single release event semaphore.
908 *
909 * @returns VBox status code.
910 * @param pSession The session handle of the caller.
911 * @param phEvent Where to return the handle to the event semaphore.
912 */
913SUPDECL(int) SUPSemEventCreate(PSUPDRVSESSION pSession, PSUPSEMEVENT phEvent);
914
915/**
916 * Closes a single release event semaphore handle.
917 *
918 * @returns VBox status code.
919 * @retval VINF_OBJECT_DESTROYED if the semaphore was destroyed.
920 * @retval VINF_SUCCESS if the handle was successfully closed but the semaphore
921 * object remained alive because of other references.
922 *
923 * @param pSession The session handle of the caller.
924 * @param hEvent The handle. Nil is quietly ignored.
925 */
926SUPDECL(int) SUPSemEventClose(PSUPDRVSESSION pSession, SUPSEMEVENT hEvent);
927
928/**
929 * Signals a single release event semaphore.
930 *
931 * @returns VBox status code.
932 * @param pSession The session handle of the caller.
933 * @param hEvent The semaphore handle.
934 */
935SUPDECL(int) SUPSemEventSignal(PSUPDRVSESSION pSession, SUPSEMEVENT hEvent);
936
937#ifdef IN_RING0
938/**
939 * Waits on a single release event semaphore, not interruptible.
940 *
941 * @returns VBox status code.
942 * @param pSession The session handle of the caller.
943 * @param hEvent The semaphore handle.
944 * @param cMillies The number of milliseconds to wait.
945 * @remarks Not available in ring-3.
946 */
947SUPDECL(int) SUPSemEventWait(PSUPDRVSESSION pSession, SUPSEMEVENT hEvent, uint32_t cMillies);
948#endif
949
950/**
951 * Waits on a single release event semaphore, interruptible.
952 *
953 * @returns VBox status code.
954 * @param pSession The session handle of the caller.
955 * @param hEvent The semaphore handle.
956 * @param cMillies The number of milliseconds to wait.
957 */
958SUPDECL(int) SUPSemEventWaitNoResume(PSUPDRVSESSION pSession, SUPSEMEVENT hEvent, uint32_t cMillies);
959
960/**
961 * Waits on a single release event semaphore, interruptible.
962 *
963 * @returns VBox status code.
964 * @param pSession The session handle of the caller.
965 * @param hEvent The semaphore handle.
966 * @param uNsTimeout The deadline given on the RTTimeNanoTS() clock.
967 */
968SUPDECL(int) SUPSemEventWaitNsAbsIntr(PSUPDRVSESSION pSession, SUPSEMEVENT hEvent, uint64_t uNsTimeout);
969
970/**
971 * Waits on a single release event semaphore, interruptible.
972 *
973 * @returns VBox status code.
974 * @param pSession The session handle of the caller.
975 * @param hEvent The semaphore handle.
976 * @param cNsTimeout The number of nanoseconds to wait.
977 */
978SUPDECL(int) SUPSemEventWaitNsRelIntr(PSUPDRVSESSION pSession, SUPSEMEVENT hEvent, uint64_t cNsTimeout);
979
980/**
981 * Gets the best timeout resolution that SUPSemEventWaitNsAbsIntr and
982 * SUPSemEventWaitNsAbsIntr can do.
983 *
984 * @returns The resolution in nanoseconds.
985 * @param pSession The session handle of the caller.
986 */
987SUPDECL(uint32_t) SUPSemEventGetResolution(PSUPDRVSESSION pSession);
988
989
990/**
991 * Creates a multiple release event semaphore.
992 *
993 * @returns VBox status code.
994 * @param pSession The session handle of the caller.
995 * @param phEventMulti Where to return the handle to the event semaphore.
996 */
997SUPDECL(int) SUPSemEventMultiCreate(PSUPDRVSESSION pSession, PSUPSEMEVENTMULTI phEventMulti);
998
999/**
1000 * Closes a multiple release event semaphore handle.
1001 *
1002 * @returns VBox status code.
1003 * @retval VINF_OBJECT_DESTROYED if the semaphore was destroyed.
1004 * @retval VINF_SUCCESS if the handle was successfully closed but the semaphore
1005 * object remained alive because of other references.
1006 *
1007 * @param pSession The session handle of the caller.
1008 * @param hEventMulti The handle. Nil is quietly ignored.
1009 */
1010SUPDECL(int) SUPSemEventMultiClose(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti);
1011
1012/**
1013 * Signals a multiple release event semaphore.
1014 *
1015 * @returns VBox status code.
1016 * @param pSession The session handle of the caller.
1017 * @param hEventMulti The semaphore handle.
1018 */
1019SUPDECL(int) SUPSemEventMultiSignal(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti);
1020
1021/**
1022 * Resets a multiple release event semaphore.
1023 *
1024 * @returns VBox status code.
1025 * @param pSession The session handle of the caller.
1026 * @param hEventMulti The semaphore handle.
1027 */
1028SUPDECL(int) SUPSemEventMultiReset(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti);
1029
1030#ifdef IN_RING0
1031/**
1032 * Waits on a multiple release event semaphore, not interruptible.
1033 *
1034 * @returns VBox status code.
1035 * @param pSession The session handle of the caller.
1036 * @param hEventMulti The semaphore handle.
1037 * @param cMillies The number of milliseconds to wait.
1038 * @remarks Not available in ring-3.
1039 */
1040SUPDECL(int) SUPSemEventMultiWait(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti, uint32_t cMillies);
1041#endif
1042
1043/**
1044 * Waits on a multiple release event semaphore, interruptible.
1045 *
1046 * @returns VBox status code.
1047 * @param pSession The session handle of the caller.
1048 * @param hEventMulti The semaphore handle.
1049 * @param cMillies The number of milliseconds to wait.
1050 */
1051SUPDECL(int) SUPSemEventMultiWaitNoResume(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti, uint32_t cMillies);
1052
1053/**
1054 * Waits on a multiple release event semaphore, interruptible.
1055 *
1056 * @returns VBox status code.
1057 * @param pSession The session handle of the caller.
1058 * @param hEventMulti The semaphore handle.
1059 * @param uNsTimeout The deadline given on the RTTimeNanoTS() clock.
1060 */
1061SUPDECL(int) SUPSemEventMultiWaitNsAbsIntr(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti, uint64_t uNsTimeout);
1062
1063/**
1064 * Waits on a multiple release event semaphore, interruptible.
1065 *
1066 * @returns VBox status code.
1067 * @param pSession The session handle of the caller.
1068 * @param hEventMulti The semaphore handle.
1069 * @param cNsTimeout The number of nanoseconds to wait.
1070 */
1071SUPDECL(int) SUPSemEventMultiWaitNsRelIntr(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti, uint64_t cNsTimeout);
1072
1073/**
1074 * Gets the best timeout resolution that SUPSemEventMultiWaitNsAbsIntr and
1075 * SUPSemEventMultiWaitNsRelIntr can do.
1076 *
1077 * @returns The resolution in nanoseconds.
1078 * @param pSession The session handle of the caller.
1079 */
1080SUPDECL(uint32_t) SUPSemEventMultiGetResolution(PSUPDRVSESSION pSession);
1081
1082
1083#ifdef IN_RING3
1084
1085/** @defgroup grp_sup_r3 SUP Host Context Ring-3 API
1086 * @{
1087 */
1088
1089/**
1090 * Installs the support library.
1091 *
1092 * @returns VBox status code.
1093 */
1094SUPR3DECL(int) SUPR3Install(void);
1095
1096/**
1097 * Uninstalls the support library.
1098 *
1099 * @returns VBox status code.
1100 */
1101SUPR3DECL(int) SUPR3Uninstall(void);
1102
1103/**
1104 * Trusted main entry point.
1105 *
1106 * This is exported as "TrustedMain" by the dynamic libraries which contains the
1107 * "real" application binary for which the hardened stub is built. The entry
1108 * point is invoked upon successful initialization of the support library and
1109 * runtime.
1110 *
1111 * @returns main kind of exit code.
1112 * @param argc The argument count.
1113 * @param argv The argument vector.
1114 * @param envp The environment vector.
1115 */
1116typedef DECLCALLBACKTYPE(int, FNSUPTRUSTEDMAIN,(int argc, char **argv, char **envp));
1117/** Pointer to FNSUPTRUSTEDMAIN(). */
1118typedef FNSUPTRUSTEDMAIN *PFNSUPTRUSTEDMAIN;
1119
1120/** Which operation failed. */
1121typedef enum SUPINITOP
1122{
1123 /** Invalid. */
1124 kSupInitOp_Invalid = 0,
1125 /** Installation integrity error. */
1126 kSupInitOp_Integrity,
1127 /** Setuid related. */
1128 kSupInitOp_RootCheck,
1129 /** Driver related. */
1130 kSupInitOp_Driver,
1131 /** IPRT init related. */
1132 kSupInitOp_IPRT,
1133 /** Miscellaneous. */
1134 kSupInitOp_Misc,
1135 /** Place holder. */
1136 kSupInitOp_End
1137} SUPINITOP;
1138
1139/**
1140 * Trusted error entry point, optional.
1141 *
1142 * This is exported as "TrustedError" by the dynamic libraries which contains
1143 * the "real" application binary for which the hardened stub is built. The
1144 * hardened main() must specify SUPSECMAIN_FLAGS_TRUSTED_ERROR when calling
1145 * SUPR3HardenedMain.
1146 *
1147 * @param pszWhere Where the error occurred (function name).
1148 * @param enmWhat Which operation went wrong.
1149 * @param rc The status code.
1150 * @param pszMsgFmt Error message format string.
1151 * @param va The message format arguments.
1152 */
1153typedef DECLCALLBACKTYPE(void, FNSUPTRUSTEDERROR,(const char *pszWhere, SUPINITOP enmWhat, int rc,
1154 const char *pszMsgFmt, va_list va)) RT_IPRT_FORMAT_ATTR(4, 0);
1155/** Pointer to FNSUPTRUSTEDERROR. */
1156typedef FNSUPTRUSTEDERROR *PFNSUPTRUSTEDERROR;
1157
1158/**
1159 * Secure main.
1160 *
1161 * This is used for the set-user-ID-on-execute binaries on unixy systems
1162 * and when using the open-vboxdrv-via-root-service setup on Windows.
1163 *
1164 * This function will perform the integrity checks of the VirtualBox
1165 * installation, open the support driver, open the root service (later),
1166 * and load the DLL corresponding to \a pszProgName and execute its main
1167 * function.
1168 *
1169 * @returns Return code appropriate for main().
1170 *
1171 * @param pszProgName The program name. This will be used to figure out which
1172 * DLL/SO/DYLIB to load and execute.
1173 * @param fFlags Flags.
1174 * @param argc The argument count.
1175 * @param argv The argument vector.
1176 * @param envp The environment vector.
1177 */
1178DECL_HIDDEN_NOTHROW(int) SUPR3HardenedMain(const char *pszProgName, uint32_t fFlags, int argc, char **argv, char **envp);
1179
1180/** @name SUPR3HardenedMain flags.
1181 * @{ */
1182/** Don't open the device. (Intended for VirtualBox without -startvm.) */
1183#define SUPSECMAIN_FLAGS_DONT_OPEN_DEV RT_BIT_32(0)
1184/** The hardened DLL has a "TrustedError" function (see FNSUPTRUSTEDERROR). */
1185#define SUPSECMAIN_FLAGS_TRUSTED_ERROR RT_BIT_32(1)
1186/** Hack for making VirtualBoxVM use VirtualBox.dylib on Mac OS X.
1187 * @note Not used since 6.0 */
1188#define SUPSECMAIN_FLAGS_OSX_VM_APP RT_BIT_32(2)
1189/** Program binary location mask. */
1190#define SUPSECMAIN_FLAGS_LOC_MASK UINT32_C(0x00000030)
1191/** Default binary location is the application binary directory. Does
1192 * not need to be given explicitly (it's 0). */
1193#define SUPSECMAIN_FLAGS_LOC_APP_BIN UINT32_C(0x00000000)
1194/** The binary is located in the testcase directory instead of the
1195 * default application binary directory. */
1196#define SUPSECMAIN_FLAGS_LOC_TESTCASE UINT32_C(0x00000010)
1197/** The binary is located in a nested application bundle under Resources/ in the
1198 * main Mac OS X application (think Resources/VirtualBoxVM.app). */
1199#define SUPSECMAIN_FLAGS_LOC_OSX_HLP_APP UINT32_C(0x00000020)
1200/** The first process.
1201 * @internal */
1202#define SUPSECMAIN_FLAGS_FIRST_PROCESS UINT32_C(0x00000100)
1203/** @} */
1204
1205/**
1206 * Initializes the support library.
1207 *
1208 * Each successful call to SUPR3Init() or SUPR3InitEx must be countered by a
1209 * call to SUPR3Term(false).
1210 *
1211 * @returns VBox status code.
1212 * @param ppSession Where to store the session handle. Defaults to NULL.
1213 */
1214SUPR3DECL(int) SUPR3Init(PSUPDRVSESSION *ppSession);
1215
1216/**
1217 * Initializes the support library, extended version.
1218 *
1219 * Each successful call to SUPR3Init() or SUPR3InitEx must be countered by a
1220 * call to SUPR3Term(false).
1221 *
1222 * @returns VBox status code.
1223 * @param fUnrestricted The desired access.
1224 * @param ppSession Where to store the session handle. Defaults to NULL.
1225 */
1226SUPR3DECL(int) SUPR3InitEx(bool fUnrestricted, PSUPDRVSESSION *ppSession);
1227
1228/**
1229 * Terminates the support library.
1230 *
1231 * @returns VBox status code.
1232 * @param fForced Forced termination. This means to ignore the
1233 * init call count and just terminated.
1234 */
1235#ifdef __cplusplus
1236SUPR3DECL(int) SUPR3Term(bool fForced = false);
1237#else
1238SUPR3DECL(int) SUPR3Term(int fForced);
1239#endif
1240
1241/**
1242 * Sets the ring-0 VM handle for use with fast IOCtls.
1243 *
1244 * @returns VBox status code.
1245 * @param pVMR0 The ring-0 VM handle.
1246 * NIL_RTR0PTR can be used to unset the handle when the
1247 * VM is about to be destroyed.
1248 */
1249SUPR3DECL(int) SUPR3SetVMForFastIOCtl(PVMR0 pVMR0);
1250
1251/**
1252 * Calls the HC R0 VMM entry point.
1253 * See VMMR0Entry() for more details.
1254 *
1255 * @returns error code specific to uFunction.
1256 * @param pVMR0 Pointer to the Ring-0 (Host Context) mapping of the VM structure.
1257 * @param idCpu The virtual CPU ID.
1258 * @param uOperation Operation to execute.
1259 * @param pvArg Argument.
1260 */
1261SUPR3DECL(int) SUPR3CallVMMR0(PVMR0 pVMR0, VMCPUID idCpu, unsigned uOperation, void *pvArg);
1262
1263/**
1264 * Variant of SUPR3CallVMMR0, except that this takes the fast ioclt path
1265 * regardsless of compile-time defaults.
1266 *
1267 * @returns VBox status code.
1268 * @param pVMR0 The ring-0 VM handle.
1269 * @param uOperation The operation; only the SUP_VMMR0_DO_* ones are valid.
1270 * @param idCpu The virtual CPU ID.
1271 */
1272SUPR3DECL(int) SUPR3CallVMMR0Fast(PVMR0 pVMR0, unsigned uOperation, VMCPUID idCpu);
1273
1274/**
1275 * Calls the HC R0 VMM entry point, in a safer but slower manner than
1276 * SUPR3CallVMMR0. When entering using this call the R0 components can call
1277 * into the host kernel (i.e. use the SUPR0 and RT APIs).
1278 *
1279 * See VMMR0Entry() for more details.
1280 *
1281 * @returns error code specific to uFunction.
1282 * @param pVMR0 Pointer to the Ring-0 (Host Context) mapping of the VM structure.
1283 * @param idCpu The virtual CPU ID.
1284 * @param uOperation Operation to execute.
1285 * @param u64Arg Constant argument.
1286 * @param pReqHdr Pointer to a request header. Optional.
1287 * This will be copied in and out of kernel space. There currently is a size
1288 * limit on this, just below 4KB.
1289 */
1290SUPR3DECL(int) SUPR3CallVMMR0Ex(PVMR0 pVMR0, VMCPUID idCpu, unsigned uOperation, uint64_t u64Arg, PSUPVMMR0REQHDR pReqHdr);
1291
1292/**
1293 * Calls a ring-0 service.
1294 *
1295 * The operation and the request packet is specific to the service.
1296 *
1297 * @returns error code specific to uFunction.
1298 * @param pszService The service name.
1299 * @param cchService The length of the service name.
1300 * @param uOperation The request number.
1301 * @param u64Arg Constant argument.
1302 * @param pReqHdr Pointer to a request header. Optional.
1303 * This will be copied in and out of kernel space. There currently is a size
1304 * limit on this, just below 4KB.
1305 */
1306SUPR3DECL(int) SUPR3CallR0Service(const char *pszService, size_t cchService, uint32_t uOperation, uint64_t u64Arg, PSUPR0SERVICEREQHDR pReqHdr);
1307
1308/** Which logger. */
1309typedef enum SUPLOGGER
1310{
1311 SUPLOGGER_DEBUG = 1,
1312 SUPLOGGER_RELEASE
1313} SUPLOGGER;
1314
1315/**
1316 * Changes the settings of the specified ring-0 logger.
1317 *
1318 * @returns VBox status code.
1319 * @param enmWhich Which logger.
1320 * @param pszFlags The flags settings.
1321 * @param pszGroups The groups settings.
1322 * @param pszDest The destination specificier.
1323 */
1324SUPR3DECL(int) SUPR3LoggerSettings(SUPLOGGER enmWhich, const char *pszFlags, const char *pszGroups, const char *pszDest);
1325
1326/**
1327 * Creates a ring-0 logger instance.
1328 *
1329 * @returns VBox status code.
1330 * @param enmWhich Which logger to create.
1331 * @param pszFlags The flags settings.
1332 * @param pszGroups The groups settings.
1333 * @param pszDest The destination specificier.
1334 */
1335SUPR3DECL(int) SUPR3LoggerCreate(SUPLOGGER enmWhich, const char *pszFlags, const char *pszGroups, const char *pszDest);
1336
1337/**
1338 * Destroys a ring-0 logger instance.
1339 *
1340 * @returns VBox status code.
1341 * @param enmWhich Which logger.
1342 */
1343SUPR3DECL(int) SUPR3LoggerDestroy(SUPLOGGER enmWhich);
1344
1345/**
1346 * Queries the paging mode of the host OS.
1347 *
1348 * @returns The paging mode.
1349 */
1350SUPR3DECL(SUPPAGINGMODE) SUPR3GetPagingMode(void);
1351
1352/**
1353 * Allocate zero-filled pages.
1354 *
1355 * Use this to allocate a number of pages suitable for seeding / locking.
1356 * Call SUPR3PageFree() to free the pages once done with them.
1357 *
1358 * @returns VBox status.
1359 * @param cPages Number of pages to allocate.
1360 * @param ppvPages Where to store the base pointer to the allocated pages.
1361 */
1362SUPR3DECL(int) SUPR3PageAlloc(size_t cPages, void **ppvPages);
1363
1364/**
1365 * Frees pages allocated with SUPR3PageAlloc().
1366 *
1367 * @returns VBox status.
1368 * @param pvPages Pointer returned by SUPR3PageAlloc().
1369 * @param cPages Number of pages that was allocated.
1370 */
1371SUPR3DECL(int) SUPR3PageFree(void *pvPages, size_t cPages);
1372
1373/**
1374 * Allocate non-zeroed, locked, pages with user and, optionally, kernel
1375 * mappings.
1376 *
1377 * Use SUPR3PageFreeEx() to free memory allocated with this function.
1378 *
1379 * @returns VBox status code.
1380 * @param cPages The number of pages to allocate.
1381 * @param fFlags Flags, reserved. Must be zero.
1382 * @param ppvPages Where to store the address of the user mapping.
1383 * @param pR0Ptr Where to store the address of the kernel mapping.
1384 * NULL if no kernel mapping is desired.
1385 * @param paPages Where to store the physical addresses of each page.
1386 * Optional.
1387 */
1388SUPR3DECL(int) SUPR3PageAllocEx(size_t cPages, uint32_t fFlags, void **ppvPages, PRTR0PTR pR0Ptr, PSUPPAGE paPages);
1389
1390/**
1391 * Maps a portion of a ring-3 only allocation into kernel space.
1392 *
1393 * @returns VBox status code.
1394 *
1395 * @param pvR3 The address SUPR3PageAllocEx return.
1396 * @param off Offset to start mapping at. Must be page aligned.
1397 * @param cb Number of bytes to map. Must be page aligned.
1398 * @param fFlags Flags, must be zero.
1399 * @param pR0Ptr Where to store the address on success.
1400 *
1401 */
1402SUPR3DECL(int) SUPR3PageMapKernel(void *pvR3, uint32_t off, uint32_t cb, uint32_t fFlags, PRTR0PTR pR0Ptr);
1403
1404/**
1405 * Changes the protection of
1406 *
1407 * @returns VBox status code.
1408 * @retval VERR_NOT_SUPPORTED if the OS doesn't allow us to change page level
1409 * protection. See also RTR0MemObjProtect.
1410 *
1411 * @param pvR3 The ring-3 address SUPR3PageAllocEx returned.
1412 * @param R0Ptr The ring-0 address SUPR3PageAllocEx returned if it
1413 * is desired that the corresponding ring-0 page
1414 * mappings should change protection as well. Pass
1415 * NIL_RTR0PTR if the ring-0 pages should remain
1416 * unaffected.
1417 * @param off Offset to start at which to start chagning the page
1418 * level protection. Must be page aligned.
1419 * @param cb Number of bytes to change. Must be page aligned.
1420 * @param fProt The new page level protection, either a combination
1421 * of RTMEM_PROT_READ, RTMEM_PROT_WRITE and
1422 * RTMEM_PROT_EXEC, or just RTMEM_PROT_NONE.
1423 */
1424SUPR3DECL(int) SUPR3PageProtect(void *pvR3, RTR0PTR R0Ptr, uint32_t off, uint32_t cb, uint32_t fProt);
1425
1426/**
1427 * Free pages allocated by SUPR3PageAllocEx.
1428 *
1429 * @returns VBox status code.
1430 * @param pvPages The address of the user mapping.
1431 * @param cPages The number of pages.
1432 */
1433SUPR3DECL(int) SUPR3PageFreeEx(void *pvPages, size_t cPages);
1434
1435/**
1436 * Allocated memory with page aligned memory with a contiguous and locked physical
1437 * memory backing below 4GB.
1438 *
1439 * @returns Pointer to the allocated memory (virtual address).
1440 * *pHCPhys is set to the physical address of the memory.
1441 * If ppvR0 isn't NULL, *ppvR0 is set to the ring-0 mapping.
1442 * The returned memory must be freed using SUPR3ContFree().
1443 * @returns NULL on failure.
1444 * @param cPages Number of pages to allocate.
1445 * @param pR0Ptr Where to store the ring-0 mapping of the allocation. (optional)
1446 * @param pHCPhys Where to store the physical address of the memory block.
1447 *
1448 * @remark This 2nd version of this API exists because we're not able to map the
1449 * ring-3 mapping executable on WIN64. This is a serious problem in regard to
1450 * the world switchers.
1451 */
1452SUPR3DECL(void *) SUPR3ContAlloc(size_t cPages, PRTR0PTR pR0Ptr, PRTHCPHYS pHCPhys);
1453
1454/**
1455 * Frees memory allocated with SUPR3ContAlloc().
1456 *
1457 * @returns VBox status code.
1458 * @param pv Pointer to the memory block which should be freed.
1459 * @param cPages Number of pages to be freed.
1460 */
1461SUPR3DECL(int) SUPR3ContFree(void *pv, size_t cPages);
1462
1463/**
1464 * Allocated non contiguous physical memory below 4GB.
1465 *
1466 * The memory isn't zeroed.
1467 *
1468 * @returns VBox status code.
1469 * @returns NULL on failure.
1470 * @param cPages Number of pages to allocate.
1471 * @param ppvPages Where to store the pointer to the allocated memory.
1472 * The pointer stored here on success must be passed to
1473 * SUPR3LowFree when the memory should be released.
1474 * @param ppvPagesR0 Where to store the ring-0 pointer to the allocated memory. optional.
1475 * @param paPages Where to store the physical addresses of the individual pages.
1476 */
1477SUPR3DECL(int) SUPR3LowAlloc(size_t cPages, void **ppvPages, PRTR0PTR ppvPagesR0, PSUPPAGE paPages);
1478
1479/**
1480 * Frees memory allocated with SUPR3LowAlloc().
1481 *
1482 * @returns VBox status code.
1483 * @param pv Pointer to the memory block which should be freed.
1484 * @param cPages Number of pages that was allocated.
1485 */
1486SUPR3DECL(int) SUPR3LowFree(void *pv, size_t cPages);
1487
1488/**
1489 * Load a module into R0 HC.
1490 *
1491 * This will verify the file integrity in a similar manner as
1492 * SUPR3HardenedVerifyFile before loading it.
1493 *
1494 * @returns VBox status code.
1495 * @param pszFilename The path to the image file.
1496 * @param pszModule The module name. Max 32 bytes.
1497 * @param ppvImageBase Where to store the image address.
1498 * @param pErrInfo Where to return extended error information.
1499 * Optional.
1500 */
1501SUPR3DECL(int) SUPR3LoadModule(const char *pszFilename, const char *pszModule, void **ppvImageBase, PRTERRINFO pErrInfo);
1502
1503/**
1504 * Load a module into R0 HC.
1505 *
1506 * This will verify the file integrity in a similar manner as
1507 * SUPR3HardenedVerifyFile before loading it.
1508 *
1509 * @returns VBox status code.
1510 * @param pszFilename The path to the image file.
1511 * @param pszModule The module name. Max 32 bytes.
1512 * @param pszSrvReqHandler The name of the service request handler entry
1513 * point. See FNSUPR0SERVICEREQHANDLER.
1514 * @param ppvImageBase Where to store the image address.
1515 */
1516SUPR3DECL(int) SUPR3LoadServiceModule(const char *pszFilename, const char *pszModule,
1517 const char *pszSrvReqHandler, void **ppvImageBase);
1518
1519/**
1520 * Frees a R0 HC module.
1521 *
1522 * @returns VBox status code.
1523 * @param pvImageBase The base address of the image to free.
1524 * @remark This will not actually 'free' the module, there are of course usage counting.
1525 */
1526SUPR3DECL(int) SUPR3FreeModule(void *pvImageBase);
1527
1528/**
1529 * Lock down the module loader interface.
1530 *
1531 * This will lock down the module loader interface. No new modules can be
1532 * loaded and all loaded modules can no longer be freed.
1533 *
1534 * @returns VBox status code.
1535 * @param pErrInfo Where to return extended error information.
1536 * Optional.
1537 */
1538SUPR3DECL(int) SUPR3LockDownLoader(PRTERRINFO pErrInfo);
1539
1540/**
1541 * Get the address of a symbol in a ring-0 module.
1542 *
1543 * @returns VBox status code.
1544 * @param pvImageBase The base address of the image to search.
1545 * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
1546 * ordinal value rather than a string pointer.
1547 * @param ppvValue Where to store the symbol value.
1548 */
1549SUPR3DECL(int) SUPR3GetSymbolR0(void *pvImageBase, const char *pszSymbol, void **ppvValue);
1550
1551/**
1552 * Load R0 HC VMM code.
1553 *
1554 * @returns VBox status code.
1555 * @deprecated Use SUPR3LoadModule(pszFilename, "VMMR0.r0", &pvImageBase)
1556 */
1557SUPR3DECL(int) SUPR3LoadVMM(const char *pszFilename);
1558
1559/**
1560 * Unloads R0 HC VMM code.
1561 *
1562 * @returns VBox status code.
1563 * @deprecated Use SUPR3FreeModule().
1564 */
1565SUPR3DECL(int) SUPR3UnloadVMM(void);
1566
1567/**
1568 * Get the physical address of the GIP.
1569 *
1570 * @returns VBox status code.
1571 * @param pHCPhys Where to store the physical address of the GIP.
1572 */
1573SUPR3DECL(int) SUPR3GipGetPhys(PRTHCPHYS pHCPhys);
1574
1575/**
1576 * Initializes only the bits relevant for the SUPR3HardenedVerify* APIs.
1577 *
1578 * This is for users that don't necessarily need to initialize the whole of
1579 * SUPLib. There is no harm in calling this one more time.
1580 *
1581 * @returns VBox status code.
1582 * @remarks Currently not counted, so only call once.
1583 */
1584SUPR3DECL(int) SUPR3HardenedVerifyInit(void);
1585
1586/**
1587 * Reverses the effect of SUPR3HardenedVerifyInit if SUPR3InitEx hasn't been
1588 * called.
1589 *
1590 * Ignored if the support library was initialized using SUPR3Init or
1591 * SUPR3InitEx.
1592 *
1593 * @returns VBox status code.
1594 */
1595SUPR3DECL(int) SUPR3HardenedVerifyTerm(void);
1596
1597/**
1598 * Verifies the integrity of a file, and optionally opens it.
1599 *
1600 * The integrity check is for whether the file is suitable for loading into
1601 * the hypervisor or VM process. The integrity check may include verifying
1602 * the authenticode/elfsign/whatever signature of the file, which can take
1603 * a little while.
1604 *
1605 * @returns VBox status code. On failure it will have printed a LogRel message.
1606 *
1607 * @param pszFilename The file.
1608 * @param pszWhat For the LogRel on failure.
1609 * @param phFile Where to store the handle to the opened file. This is optional, pass NULL
1610 * if the file should not be opened.
1611 * @deprecated Write a new one.
1612 */
1613SUPR3DECL(int) SUPR3HardenedVerifyFile(const char *pszFilename, const char *pszWhat, PRTFILE phFile);
1614
1615/**
1616 * Verifies the integrity of a the current process, including the image
1617 * location and that the invocation was absolute.
1618 *
1619 * This must currently be called after initializing the runtime. The intended
1620 * audience is set-uid-to-root applications, root services and similar.
1621 *
1622 * @returns VBox status code. On failure
1623 * message.
1624 * @param pszArgv0 The first argument to main().
1625 * @param fInternal Set this to @c true if this is an internal
1626 * VirtualBox application. Otherwise pass @c false.
1627 * @param pErrInfo Where to return extended error information.
1628 */
1629SUPR3DECL(int) SUPR3HardenedVerifySelf(const char *pszArgv0, bool fInternal, PRTERRINFO pErrInfo);
1630
1631/**
1632 * Verifies the integrity of an installation directory.
1633 *
1634 * The integrity check verifies that the directory cannot be tampered with by
1635 * normal users on the system. On Unix this translates to root ownership and
1636 * no symbolic linking.
1637 *
1638 * @returns VBox status code. On failure a message will be stored in @a pszErr.
1639 *
1640 * @param pszDirPath The directory path.
1641 * @param fRecursive Whether the check should be recursive or
1642 * not. When set, all sub-directores will be checked,
1643 * including files (@a fCheckFiles is ignored).
1644 * @param fCheckFiles Whether to apply the same basic integrity check to
1645 * the files in the directory as the directory itself.
1646 * @param pErrInfo Where to return extended error information.
1647 * Optional.
1648 */
1649SUPR3DECL(int) SUPR3HardenedVerifyDir(const char *pszDirPath, bool fRecursive, bool fCheckFiles, PRTERRINFO pErrInfo);
1650
1651/**
1652 * Verifies the integrity of a plug-in module.
1653 *
1654 * This is similar to SUPR3HardenedLdrLoad, except it does not load the module
1655 * and that the module does not have to be shipped with VirtualBox.
1656 *
1657 * @returns VBox status code. On failure a message will be stored in @a pszErr.
1658 *
1659 * @param pszFilename The filename of the plug-in module (nothing can be
1660 * omitted here).
1661 * @param pErrInfo Where to return extended error information.
1662 * Optional.
1663 */
1664SUPR3DECL(int) SUPR3HardenedVerifyPlugIn(const char *pszFilename, PRTERRINFO pErrInfo);
1665
1666/**
1667 * Same as RTLdrLoad() but will verify the files it loads (hardened builds).
1668 *
1669 * Will add dll suffix if missing and try load the file.
1670 *
1671 * @returns iprt status code.
1672 * @param pszFilename Image filename. This must have a path.
1673 * @param phLdrMod Where to store the handle to the loaded module.
1674 * @param fFlags See RTLDRLOAD_FLAGS_XXX.
1675 * @param pErrInfo Where to return extended error information.
1676 * Optional.
1677 */
1678SUPR3DECL(int) SUPR3HardenedLdrLoad(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo);
1679
1680/**
1681 * Same as RTLdrLoadAppPriv() but it will verify the files it loads (hardened
1682 * builds).
1683 *
1684 * Will add dll suffix to the file if missing, then look for it in the
1685 * architecture dependent application directory.
1686 *
1687 * @returns iprt status code.
1688 * @param pszFilename Image filename.
1689 * @param phLdrMod Where to store the handle to the loaded module.
1690 * @param fFlags See RTLDRLOAD_FLAGS_XXX.
1691 * @param pErrInfo Where to return extended error information.
1692 * Optional.
1693 */
1694SUPR3DECL(int) SUPR3HardenedLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo);
1695
1696/**
1697 * Same as RTLdrLoad() but will verify the files it loads (hardened builds).
1698 *
1699 * This differs from SUPR3HardenedLdrLoad() in that it can load modules from
1700 * extension packs and anything else safely installed on the system, provided
1701 * they pass the hardening tests.
1702 *
1703 * @returns iprt status code.
1704 * @param pszFilename The full path to the module, with extension.
1705 * @param phLdrMod Where to store the handle to the loaded module.
1706 * @param pErrInfo Where to return extended error information.
1707 * Optional.
1708 */
1709SUPR3DECL(int) SUPR3HardenedLdrLoadPlugIn(const char *pszFilename, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo);
1710
1711/**
1712 * Check if the host kernel can run in VMX root mode.
1713 *
1714 * @returns VINF_SUCCESS if supported, error code indicating why if not.
1715 * @param ppszWhy Where to return an explanatory message on failure.
1716 */
1717SUPR3DECL(int) SUPR3QueryVTxSupported(const char **ppszWhy);
1718
1719/**
1720 * Return VT-x/AMD-V capabilities.
1721 *
1722 * @returns VINF_SUCCESS if supported, error code indicating why if not.
1723 * @param pfCaps Pointer to capability dword (out).
1724 * @todo Intended for main, which means we need to relax the privilege requires
1725 * when accessing certain vboxdrv functions.
1726 */
1727SUPR3DECL(int) SUPR3QueryVTCaps(uint32_t *pfCaps);
1728
1729/**
1730 * Check if NEM is supported when no VT-x/AMD-V is indicated by the CPU.
1731 *
1732 * This is really only for the windows case where we're running in a root
1733 * partition and isn't allowed to use the hardware directly.
1734 *
1735 * @returns True if NEM API support, false if not.
1736 */
1737SUPR3DECL(bool) SUPR3IsNemSupportedWhenNoVtxOrAmdV(void);
1738
1739/**
1740 * Open the tracer.
1741 *
1742 * @returns VBox status code.
1743 * @param uCookie Cookie identifying the tracer we expect to talk to.
1744 * @param uArg Tracer specific open argument.
1745 */
1746SUPR3DECL(int) SUPR3TracerOpen(uint32_t uCookie, uintptr_t uArg);
1747
1748/**
1749 * Closes the tracer.
1750 *
1751 * @returns VBox status code.
1752 */
1753SUPR3DECL(int) SUPR3TracerClose(void);
1754
1755/**
1756 * Perform an I/O request on the tracer.
1757 *
1758 * @returns VBox status.
1759 * @param uCmd The tracer command.
1760 * @param uArg The argument.
1761 * @param piRetVal Where to store the tracer return value.
1762 */
1763SUPR3DECL(int) SUPR3TracerIoCtl(uintptr_t uCmd, uintptr_t uArg, int32_t *piRetVal);
1764
1765/**
1766 * Registers the user module with the tracer.
1767 *
1768 * @returns VBox status code.
1769 * @param hModNative Native module handle. Pass ~(uintptr_t)0 if not
1770 * at hand.
1771 * @param pszModule The module name.
1772 * @param pVtgHdr The VTG header.
1773 * @param uVtgHdrAddr The address to which the VTG header is loaded
1774 * in the relevant execution context.
1775 * @param fFlags See SUP_TRACER_UMOD_FLAGS_XXX
1776 */
1777SUPR3DECL(int) SUPR3TracerRegisterModule(uintptr_t hModNative, const char *pszModule, struct VTGOBJHDR *pVtgHdr,
1778 RTUINTPTR uVtgHdrAddr, uint32_t fFlags);
1779
1780/**
1781 * Deregisters the user module.
1782 *
1783 * @returns VBox status code.
1784 * @param pVtgHdr The VTG header.
1785 */
1786SUPR3DECL(int) SUPR3TracerDeregisterModule(struct VTGOBJHDR *pVtgHdr);
1787
1788/**
1789 * Fire the probe.
1790 *
1791 * @param pVtgProbeLoc The probe location record.
1792 * @param uArg0 Raw probe argument 0.
1793 * @param uArg1 Raw probe argument 1.
1794 * @param uArg2 Raw probe argument 2.
1795 * @param uArg3 Raw probe argument 3.
1796 * @param uArg4 Raw probe argument 4.
1797 */
1798SUPDECL(void) SUPTracerFireProbe(struct VTGPROBELOC *pVtgProbeLoc, uintptr_t uArg0, uintptr_t uArg1, uintptr_t uArg2,
1799 uintptr_t uArg3, uintptr_t uArg4);
1800
1801/**
1802 * Attempts to read the value of an MSR.
1803 *
1804 * @returns VBox status code.
1805 * @param uMsr The MSR to read.
1806 * @param idCpu The CPU to read it on, NIL_RTCPUID if it doesn't
1807 * matter which CPU.
1808 * @param puValue Where to return the value.
1809 * @param pfGp Where to store the \#GP indicator for the read
1810 * operation.
1811 */
1812SUPR3DECL(int) SUPR3MsrProberRead(uint32_t uMsr, RTCPUID idCpu, uint64_t *puValue, bool *pfGp);
1813
1814/**
1815 * Attempts to write to an MSR.
1816 *
1817 * @returns VBox status code.
1818 * @param uMsr The MSR to write to.
1819 * @param idCpu The CPU to wrtie it on, NIL_RTCPUID if it
1820 * doesn't matter which CPU.
1821 * @param uValue The value to write.
1822 * @param pfGp Where to store the \#GP indicator for the write
1823 * operation.
1824 */
1825SUPR3DECL(int) SUPR3MsrProberWrite(uint32_t uMsr, RTCPUID idCpu, uint64_t uValue, bool *pfGp);
1826
1827/**
1828 * Attempts to modify the value of an MSR.
1829 *
1830 * @returns VBox status code.
1831 * @param uMsr The MSR to modify.
1832 * @param idCpu The CPU to modify it on, NIL_RTCPUID if it
1833 * doesn't matter which CPU.
1834 * @param fAndMask The bits to keep in the current MSR value.
1835 * @param fOrMask The bits to set before writing.
1836 * @param pResult The result buffer.
1837 */
1838SUPR3DECL(int) SUPR3MsrProberModify(uint32_t uMsr, RTCPUID idCpu, uint64_t fAndMask, uint64_t fOrMask,
1839 PSUPMSRPROBERMODIFYRESULT pResult);
1840
1841/**
1842 * Attempts to modify the value of an MSR, extended version.
1843 *
1844 * @returns VBox status code.
1845 * @param uMsr The MSR to modify.
1846 * @param idCpu The CPU to modify it on, NIL_RTCPUID if it
1847 * doesn't matter which CPU.
1848 * @param fAndMask The bits to keep in the current MSR value.
1849 * @param fOrMask The bits to set before writing.
1850 * @param fFaster If set to @c true some cache/tlb invalidation is
1851 * skipped, otherwise behave like
1852 * SUPR3MsrProberModify.
1853 * @param pResult The result buffer.
1854 */
1855SUPR3DECL(int) SUPR3MsrProberModifyEx(uint32_t uMsr, RTCPUID idCpu, uint64_t fAndMask, uint64_t fOrMask, bool fFaster,
1856 PSUPMSRPROBERMODIFYRESULT pResult);
1857
1858/**
1859 * Resume built-in keyboard on MacBook Air and Pro hosts.
1860 *
1861 * @returns VBox status code.
1862 */
1863SUPR3DECL(int) SUPR3ResumeSuspendedKeyboards(void);
1864
1865/**
1866 * Measure the TSC-delta for the specified CPU.
1867 *
1868 * @returns VBox status code.
1869 * @param idCpu The CPU to measure the TSC-delta for.
1870 * @param fAsync Whether the measurement is asynchronous, returns
1871 * immediately after signalling a measurement
1872 * request.
1873 * @param fForce Whether to perform a measurement even if the
1874 * specified CPU has a (possibly) valid TSC delta.
1875 * @param cRetries Number of times to retry failed delta
1876 * measurements.
1877 * @param cMsWaitRetry Number of milliseconds to wait between retries.
1878 */
1879SUPR3DECL(int) SUPR3TscDeltaMeasure(RTCPUID idCpu, bool fAsync, bool fForce, uint8_t cRetries, uint8_t cMsWaitRetry);
1880
1881/**
1882 * Reads the delta-adjust TSC value.
1883 *
1884 * @returns VBox status code.
1885 * @param puTsc Where to store the read TSC value.
1886 * @param pidApic Where to store the APIC ID of the CPU where the TSC
1887 * was read (optional, can be NULL).
1888 */
1889SUPR3DECL(int) SUPR3ReadTsc(uint64_t *puTsc, uint16_t *pidApic);
1890
1891/**
1892 * Modifies the GIP flags.
1893 *
1894 * @returns VBox status code.
1895 * @param fOrMask The OR mask of the GIP flags, see SUPGIP_FLAGS_XXX.
1896 * @param fAndMask The AND mask of the GIP flags, see SUPGIP_FLAGS_XXX.
1897 */
1898SUPR3DECL(int) SUPR3GipSetFlags(uint32_t fOrMask, uint32_t fAndMask);
1899
1900/**
1901 * Return processor microcode revision, if applicable.
1902 *
1903 * @returns VINF_SUCCESS if supported, error code indicating why if not.
1904 * @param puMicrocodeRev Pointer to microcode revision dword (out).
1905 */
1906SUPR3DECL(int) SUPR3QueryMicrocodeRev(uint32_t *puMicrocodeRev);
1907
1908/**
1909 * Gets hardware-virtualization MSRs of the CPU, if available.
1910 *
1911 * @returns VINF_SUCCESS if available, error code indicating why if not.
1912 * @param pHwvirtMsrs Where to store the hardware-virtualization MSRs.
1913 * @param fForceRequery Whether to force complete re-querying of MSRs (rather
1914 * than fetching cached values when available).
1915 */
1916SUPR3DECL(int) SUPR3GetHwvirtMsrs(PSUPHWVIRTMSRS pHwvirtMsrs, bool fForceRequery);
1917
1918/** @} */
1919#endif /* IN_RING3 */
1920
1921
1922/** @name User mode module flags (SUPR3TracerRegisterModule & SUP_IOCTL_TRACER_UMOD_REG).
1923 * @{ */
1924/** Executable image. */
1925#define SUP_TRACER_UMOD_FLAGS_EXE UINT32_C(1)
1926/** Shared library (DLL, DYLIB, SO, etc). */
1927#define SUP_TRACER_UMOD_FLAGS_SHARED UINT32_C(2)
1928/** Image type mask. */
1929#define SUP_TRACER_UMOD_FLAGS_TYPE_MASK UINT32_C(3)
1930/** @} */
1931
1932
1933#ifdef IN_RING0
1934/** @defgroup grp_sup_r0 SUP Host Context Ring-0 API
1935 * @{
1936 */
1937
1938/**
1939 * Security objectype.
1940 */
1941typedef enum SUPDRVOBJTYPE
1942{
1943 /** The usual invalid object. */
1944 SUPDRVOBJTYPE_INVALID = 0,
1945 /** A Virtual Machine instance. */
1946 SUPDRVOBJTYPE_VM,
1947 /** Internal network. */
1948 SUPDRVOBJTYPE_INTERNAL_NETWORK,
1949 /** Internal network interface. */
1950 SUPDRVOBJTYPE_INTERNAL_NETWORK_INTERFACE,
1951 /** Single release event semaphore. */
1952 SUPDRVOBJTYPE_SEM_EVENT,
1953 /** Multiple release event semaphore. */
1954 SUPDRVOBJTYPE_SEM_EVENT_MULTI,
1955 /** Raw PCI device. */
1956 SUPDRVOBJTYPE_RAW_PCI_DEVICE,
1957 /** The first invalid object type in this end. */
1958 SUPDRVOBJTYPE_END,
1959 /** The usual 32-bit type size hack. */
1960 SUPDRVOBJTYPE_32_BIT_HACK = 0x7ffffff
1961} SUPDRVOBJTYPE;
1962
1963/**
1964 * Object destructor callback.
1965 * This is called for reference counted objectes when the count reaches 0.
1966 *
1967 * @param pvObj The object pointer.
1968 * @param pvUser1 The first user argument.
1969 * @param pvUser2 The second user argument.
1970 */
1971typedef DECLCALLBACKTYPE(void, FNSUPDRVDESTRUCTOR,(void *pvObj, void *pvUser1, void *pvUser2));
1972/** Pointer to a FNSUPDRVDESTRUCTOR(). */
1973typedef FNSUPDRVDESTRUCTOR *PFNSUPDRVDESTRUCTOR;
1974
1975SUPR0DECL(void *) SUPR0ObjRegister(PSUPDRVSESSION pSession, SUPDRVOBJTYPE enmType, PFNSUPDRVDESTRUCTOR pfnDestructor, void *pvUser1, void *pvUser2);
1976SUPR0DECL(int) SUPR0ObjAddRef(void *pvObj, PSUPDRVSESSION pSession);
1977SUPR0DECL(int) SUPR0ObjAddRefEx(void *pvObj, PSUPDRVSESSION pSession, bool fNoBlocking);
1978SUPR0DECL(int) SUPR0ObjRelease(void *pvObj, PSUPDRVSESSION pSession);
1979SUPR0DECL(int) SUPR0ObjVerifyAccess(void *pvObj, PSUPDRVSESSION pSession, const char *pszObjName);
1980
1981SUPR0DECL(PVM) SUPR0GetSessionVM(PSUPDRVSESSION pSession);
1982SUPR0DECL(PGVM) SUPR0GetSessionGVM(PSUPDRVSESSION pSession);
1983SUPR0DECL(int) SUPR0SetSessionVM(PSUPDRVSESSION pSession, PGVM pGVM, PVM pVM);
1984
1985SUPR0DECL(int) SUPR0LockMem(PSUPDRVSESSION pSession, RTR3PTR pvR3, uint32_t cPages, PRTHCPHYS paPages);
1986SUPR0DECL(int) SUPR0UnlockMem(PSUPDRVSESSION pSession, RTR3PTR pvR3);
1987SUPR0DECL(int) SUPR0ContAlloc(PSUPDRVSESSION pSession, uint32_t cPages, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PRTHCPHYS pHCPhys);
1988SUPR0DECL(int) SUPR0ContFree(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr);
1989SUPR0DECL(int) SUPR0LowAlloc(PSUPDRVSESSION pSession, uint32_t cPages, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PRTHCPHYS paPages);
1990SUPR0DECL(int) SUPR0LowFree(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr);
1991SUPR0DECL(int) SUPR0MemAlloc(PSUPDRVSESSION pSession, uint32_t cb, PRTR0PTR ppvR0, PRTR3PTR ppvR3);
1992SUPR0DECL(int) SUPR0MemGetPhys(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr, PSUPPAGE paPages);
1993SUPR0DECL(int) SUPR0MemFree(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr);
1994SUPR0DECL(int) SUPR0PageAllocEx(PSUPDRVSESSION pSession, uint32_t cPages, uint32_t fFlags, PRTR3PTR ppvR3, PRTR0PTR ppvR0, PRTHCPHYS paPages);
1995SUPR0DECL(int) SUPR0PageMapKernel(PSUPDRVSESSION pSession, RTR3PTR pvR3, uint32_t offSub, uint32_t cbSub, uint32_t fFlags, PRTR0PTR ppvR0);
1996SUPR0DECL(int) SUPR0PageProtect(PSUPDRVSESSION pSession, RTR3PTR pvR3, RTR0PTR pvR0, uint32_t offSub, uint32_t cbSub, uint32_t fProt);
1997SUPR0DECL(int) SUPR0PageFree(PSUPDRVSESSION pSession, RTR3PTR pvR3);
1998SUPR0DECL(int) SUPR0GipMap(PSUPDRVSESSION pSession, PRTR3PTR ppGipR3, PRTHCPHYS pHCPhysGip);
1999SUPR0DECL(int) SUPR0LdrLock(PSUPDRVSESSION pSession);
2000SUPR0DECL(int) SUPR0LdrUnlock(PSUPDRVSESSION pSession);
2001SUPR0DECL(bool) SUPR0LdrIsLockOwnerByMod(void *hMod, bool fWantToHear);
2002SUPR0DECL(int) SUPR0LdrModByName(PSUPDRVSESSION pSession, const char *pszName, void **phMod);
2003SUPR0DECL(int) SUPR0LdrModRetain(PSUPDRVSESSION pSession, void *hMod);
2004SUPR0DECL(int) SUPR0LdrModRelease(PSUPDRVSESSION pSession, void *hMod);
2005SUPR0DECL(int) SUPR0GetVTSupport(uint32_t *pfCaps);
2006SUPR0DECL(int) SUPR0GetHwvirtMsrs(PSUPHWVIRTMSRS pMsrs, uint32_t fCaps, bool fForce);
2007SUPR0DECL(int) SUPR0GetSvmUsability(bool fInitSvm);
2008SUPR0DECL(int) SUPR0GetVmxUsability(bool *pfIsSmxModeAmbiguous);
2009SUPR0DECL(int) SUPR0GetCurrentGdtRw(RTHCUINTPTR *pGdtRw);
2010SUPR0DECL(int) SUPR0QueryVTCaps(PSUPDRVSESSION pSession, uint32_t *pfCaps);
2011SUPR0DECL(int) SUPR0GipUnmap(PSUPDRVSESSION pSession);
2012SUPR0DECL(int) SUPR0QueryUcodeRev(PSUPDRVSESSION pSession, uint32_t *puMicrocodeRev);
2013SUPR0DECL(SUPPAGINGMODE) SUPR0GetPagingMode(void);
2014SUPR0DECL(RTCCUINTREG) SUPR0ChangeCR4(RTCCUINTREG fOrMask, RTCCUINTREG fAndMask);
2015SUPR0DECL(int) SUPR0EnableVTx(bool fEnable);
2016SUPR0DECL(bool) SUPR0SuspendVTxOnCpu(void);
2017SUPR0DECL(void) SUPR0ResumeVTxOnCpu(bool fSuspended);
2018#define SUP_TSCDELTA_MEASURE_F_FORCE RT_BIT_32(0)
2019#define SUP_TSCDELTA_MEASURE_F_ASYNC RT_BIT_32(1)
2020#define SUP_TSCDELTA_MEASURE_F_VALID_MASK UINT32_C(0x00000003)
2021SUPR0DECL(int) SUPR0TscDeltaMeasureBySetIndex(PSUPDRVSESSION pSession, uint32_t iCpuSet, uint32_t fFlags,
2022 RTMSINTERVAL cMsWaitRetry, RTMSINTERVAL cMsWaitThread, uint32_t cTries);
2023
2024SUPR0DECL(void) SUPR0BadContext(PSUPDRVSESSION pSession, const char *pszFile, uint32_t uLine, const char *pszExpr);
2025
2026#if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
2027/**
2028 * Translates a physical address to a virtual mapping (valid up to end of page).
2029 * @returns VBox status code.
2030 * @param HCPhys The physical address, must be page aligned.
2031 * @param ppv Where to store the mapping address on success.
2032 */
2033SUPR0DECL(int) SUPR0HCPhysToVirt(RTHCPHYS HCPhys, void **ppv);
2034#endif
2035
2036/** Context structure returned by SUPR0IoCtlSetup for use with
2037 * SUPR0IoCtlPerform and cleaned up by SUPR0IoCtlCleanup. */
2038typedef struct SUPR0IOCTLCTX *PSUPR0IOCTLCTX;
2039
2040/**
2041 * Sets up a I/O control context for the given handle.
2042 *
2043 * @returns VBox status code.
2044 * @param pSession The support driver session.
2045 * @param hHandle The handle.
2046 * @param fFlags Flag, MBZ.
2047 * @param ppCtx Where the context is returned.
2048 */
2049SUPR0DECL(int) SUPR0IoCtlSetupForHandle(PSUPDRVSESSION pSession, intptr_t hHandle, uint32_t fFlags, PSUPR0IOCTLCTX *ppCtx);
2050
2051/**
2052 * Cleans up the I/O control context when done.
2053 *
2054 * This won't close the handle passed to SUPR0IoCtlSetupForHandle.
2055 *
2056 * @returns VBox status code.
2057 * @param pCtx The I/O control context to cleanup.
2058 */
2059SUPR0DECL(int) SUPR0IoCtlCleanup(PSUPR0IOCTLCTX pCtx);
2060
2061/**
2062 * Performs an I/O control operation.
2063 *
2064 * @returns VBox status code.
2065 * @param pCtx The I/O control context returned by
2066 * SUPR0IoCtlSetupForHandle.
2067 * @param uFunction The I/O control function to perform.
2068 * @param pvInput Pointer to input buffer (ring-0).
2069 * @param pvInputUser Ring-3 pointer corresponding to @a pvInput.
2070 * @param cbInput The amount of input. If zero, both input pointers
2071 * are expected to be NULL.
2072 * @param pvOutput Pointer to output buffer (ring-0).
2073 * @param pvOutputUser Ring-3 pointer corresponding to @a pvInput.
2074 * @param cbOutput The amount of input. If zero, both input pointers
2075 * are expected to be NULL.
2076 * @param piNativeRc Where to return the native return code. When
2077 * specified the VBox status code will typically be
2078 * VINF_SUCCESS and the caller have to consult this for
2079 * the actual result of the operation. (This saves
2080 * pointless status code conversion.) Optional.
2081 *
2082 * @note On unix systems where there is only one set of buffers possible,
2083 * pass the same pointers as input and output.
2084 */
2085SUPR0DECL(int) SUPR0IoCtlPerform(PSUPR0IOCTLCTX pCtx, uintptr_t uFunction,
2086 void *pvInput, RTR3PTR pvInputUser, size_t cbInput,
2087 void *pvOutput, RTR3PTR pvOutputUser, size_t cbOutput,
2088 int32_t *piNativeRc);
2089
2090/**
2091 * Writes to the debugger and/or kernel log.
2092 *
2093 * The length of the formatted message is somewhat limited, so keep things short
2094 * and to the point.
2095 *
2096 * @returns Number of bytes written, mabye.
2097 * @param pszFormat IPRT format string.
2098 * @param ... Arguments referenced by the format string.
2099 */
2100SUPR0DECL(int) SUPR0Printf(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
2101
2102/**
2103 * Returns configuration flags of the host kernel.
2104 *
2105 * @returns Combination of SUPKERNELFEATURES_XXX flags.
2106 */
2107SUPR0DECL(uint32_t) SUPR0GetKernelFeatures(void);
2108
2109/** @copydoc RTLogGetDefaultInstanceEx
2110 * @remarks To allow overriding RTLogGetDefaultInstanceEx locally. */
2111SUPR0DECL(struct RTLOGGER *) SUPR0GetDefaultLogInstanceEx(uint32_t fFlagsAndGroup);
2112/** @copydoc RTLogRelGetDefaultInstanceEx
2113 * @remarks To allow overriding RTLogRelGetDefaultInstanceEx locally. */
2114SUPR0DECL(struct RTLOGGER *) SUPR0GetDefaultLogRelInstanceEx(uint32_t fFlagsAndGroup);
2115
2116
2117/** @name Absolute symbols
2118 * Take the address of these, don't try call them.
2119 * @{ */
2120SUPR0DECL(void) SUPR0AbsIs64bit(void);
2121SUPR0DECL(void) SUPR0Abs64bitKernelCS(void);
2122SUPR0DECL(void) SUPR0Abs64bitKernelSS(void);
2123SUPR0DECL(void) SUPR0Abs64bitKernelDS(void);
2124SUPR0DECL(void) SUPR0AbsKernelCS(void);
2125SUPR0DECL(void) SUPR0AbsKernelSS(void);
2126SUPR0DECL(void) SUPR0AbsKernelDS(void);
2127SUPR0DECL(void) SUPR0AbsKernelES(void);
2128SUPR0DECL(void) SUPR0AbsKernelFS(void);
2129SUPR0DECL(void) SUPR0AbsKernelGS(void);
2130/** @} */
2131
2132/**
2133 * Support driver component factory.
2134 *
2135 * Component factories are registered by drivers that provides services
2136 * such as the host network interface filtering and access to the host
2137 * TCP/IP stack.
2138 *
2139 * @remark Module dependencies and making sure that a component doesn't
2140 * get unloaded while in use, is the sole responsibility of the
2141 * driver/kext/whatever implementing the component.
2142 */
2143typedef struct SUPDRVFACTORY
2144{
2145 /** The (unique) name of the component factory. */
2146 char szName[56];
2147 /**
2148 * Queries a factory interface.
2149 *
2150 * The factory interface is specific to each component and will be be
2151 * found in the header(s) for the component alongside its UUID.
2152 *
2153 * @returns Pointer to the factory interfaces on success, NULL on failure.
2154 *
2155 * @param pSupDrvFactory Pointer to this structure.
2156 * @param pSession The SUPDRV session making the query.
2157 * @param pszInterfaceUuid The UUID of the factory interface.
2158 */
2159 DECLR0CALLBACKMEMBER(void *, pfnQueryFactoryInterface,(struct SUPDRVFACTORY const *pSupDrvFactory, PSUPDRVSESSION pSession, const char *pszInterfaceUuid));
2160} SUPDRVFACTORY;
2161/** Pointer to a support driver factory. */
2162typedef SUPDRVFACTORY *PSUPDRVFACTORY;
2163/** Pointer to a const support driver factory. */
2164typedef SUPDRVFACTORY const *PCSUPDRVFACTORY;
2165
2166SUPR0DECL(int) SUPR0ComponentRegisterFactory(PSUPDRVSESSION pSession, PCSUPDRVFACTORY pFactory);
2167SUPR0DECL(int) SUPR0ComponentDeregisterFactory(PSUPDRVSESSION pSession, PCSUPDRVFACTORY pFactory);
2168SUPR0DECL(int) SUPR0ComponentQueryFactory(PSUPDRVSESSION pSession, const char *pszName, const char *pszInterfaceUuid, void **ppvFactoryIf);
2169
2170
2171/** @name Tracing
2172 * @{ */
2173
2174/**
2175 * Tracer data associated with a provider.
2176 */
2177typedef union SUPDRVTRACERDATA
2178{
2179 /** Generic */
2180 uint64_t au64[2];
2181
2182 /** DTrace data. */
2183 struct
2184 {
2185 /** Provider ID. */
2186 uintptr_t idProvider;
2187 /** The number of trace points provided. */
2188 uint32_t volatile cProvidedProbes;
2189 /** Whether we've invalidated this bugger. */
2190 bool fZombie;
2191 } DTrace;
2192} SUPDRVTRACERDATA;
2193/** Pointer to the tracer data associated with a provider. */
2194typedef SUPDRVTRACERDATA *PSUPDRVTRACERDATA;
2195
2196/**
2197 * Probe location info for ring-0.
2198 *
2199 * Since we cannot trust user tracepoint modules, we need to duplicate the probe
2200 * ID and enabled flag in ring-0.
2201 */
2202typedef struct SUPDRVPROBELOC
2203{
2204 /** The probe ID. */
2205 uint32_t idProbe;
2206 /** Whether it's enabled or not. */
2207 bool fEnabled;
2208} SUPDRVPROBELOC;
2209/** Pointer to a ring-0 probe location record. */
2210typedef SUPDRVPROBELOC *PSUPDRVPROBELOC;
2211
2212/**
2213 * Probe info for ring-0.
2214 *
2215 * Since we cannot trust user tracepoint modules, we need to duplicate the
2216 * probe enable count.
2217 */
2218typedef struct SUPDRVPROBEINFO
2219{
2220 /** The number of times this probe has been enabled. */
2221 uint32_t volatile cEnabled;
2222} SUPDRVPROBEINFO;
2223/** Pointer to a ring-0 probe info record. */
2224typedef SUPDRVPROBEINFO *PSUPDRVPROBEINFO;
2225
2226/**
2227 * Support driver tracepoint provider core.
2228 */
2229typedef struct SUPDRVVDTPROVIDERCORE
2230{
2231 /** The tracer data member. */
2232 SUPDRVTRACERDATA TracerData;
2233 /** Pointer to the provider name (a copy that's always available). */
2234 const char *pszName;
2235 /** Pointer to the module name (a copy that's always available). */
2236 const char *pszModName;
2237
2238 /** The provider descriptor. */
2239 struct VTGDESCPROVIDER *pDesc;
2240 /** The VTG header. */
2241 struct VTGOBJHDR *pHdr;
2242
2243 /** The size of the entries in the pvProbeLocsEn table. */
2244 uint8_t cbProbeLocsEn;
2245 /** The actual module bit count (corresponds to cbProbeLocsEn). */
2246 uint8_t cBits;
2247 /** Set if this is a Umod, otherwise clear. */
2248 bool fUmod;
2249 /** Explicit alignment padding (paranoia). */
2250 uint8_t abAlignment[ARCH_BITS == 32 ? 1 : 5];
2251
2252 /** The probe locations used for descriptive purposes. */
2253 struct VTGPROBELOC const *paProbeLocsRO;
2254 /** Pointer to the probe location array where the enable flag needs
2255 * flipping. For kernel providers, this will always be SUPDRVPROBELOC,
2256 * while user providers can either be 32-bit or 64-bit. Use
2257 * cbProbeLocsEn to calculate the address of an entry. */
2258 void *pvProbeLocsEn;
2259 /** Pointer to the probe array containing the enabled counts. */
2260 uint32_t *pacProbeEnabled;
2261
2262 /** The ring-0 probe location info for user tracepoint modules.
2263 * This is NULL if fUmod is false. */
2264 PSUPDRVPROBELOC paR0ProbeLocs;
2265 /** The ring-0 probe info for user tracepoint modules.
2266 * This is NULL if fUmod is false. */
2267 PSUPDRVPROBEINFO paR0Probes;
2268
2269} SUPDRVVDTPROVIDERCORE;
2270/** Pointer to a tracepoint provider core structure. */
2271typedef SUPDRVVDTPROVIDERCORE *PSUPDRVVDTPROVIDERCORE;
2272
2273/** Pointer to a tracer registration record. */
2274typedef struct SUPDRVTRACERREG const *PCSUPDRVTRACERREG;
2275/**
2276 * Support driver tracer registration record.
2277 */
2278typedef struct SUPDRVTRACERREG
2279{
2280 /** Magic value (SUPDRVTRACERREG_MAGIC). */
2281 uint32_t u32Magic;
2282 /** Version (SUPDRVTRACERREG_VERSION). */
2283 uint32_t u32Version;
2284
2285 /**
2286 * Fire off a kernel probe.
2287 *
2288 * @param pVtgProbeLoc The probe location record.
2289 * @param uArg0 The first raw probe argument.
2290 * @param uArg1 The second raw probe argument.
2291 * @param uArg2 The third raw probe argument.
2292 * @param uArg3 The fourth raw probe argument.
2293 * @param uArg4 The fifth raw probe argument.
2294 *
2295 * @remarks SUPR0TracerFireProbe will do a tail jump thru this member, so
2296 * no extra stack frames will be added.
2297 * @remarks This does not take a 'this' pointer argument because it doesn't map
2298 * well onto VTG or DTrace.
2299 *
2300 */
2301 DECLR0CALLBACKMEMBER(void, pfnProbeFireKernel, (struct VTGPROBELOC *pVtgProbeLoc, uintptr_t uArg0, uintptr_t uArg1, uintptr_t uArg2,
2302 uintptr_t uArg3, uintptr_t uArg4));
2303
2304 /**
2305 * Fire off a user-mode probe.
2306 *
2307 * @param pThis Pointer to the registration record.
2308 *
2309 * @param pVtgProbeLoc The probe location record.
2310 * @param pSession The user session.
2311 * @param pCtx The usermode context info.
2312 * @param pVtgHdr The VTG header (read-only).
2313 * @param pProbeLocRO The read-only probe location record .
2314 */
2315 DECLR0CALLBACKMEMBER(void, pfnProbeFireUser, (PCSUPDRVTRACERREG pThis, PSUPDRVSESSION pSession, PCSUPDRVTRACERUSRCTX pCtx,
2316 struct VTGOBJHDR const *pVtgHdr, struct VTGPROBELOC const *pProbeLocRO));
2317
2318 /**
2319 * Opens up the tracer.
2320 *
2321 * @returns VBox status code.
2322 * @param pThis Pointer to the registration record.
2323 * @param pSession The session doing the opening.
2324 * @param uCookie A cookie (magic) unique to the tracer, so it can
2325 * fend off incompatible clients.
2326 * @param uArg Tracer specific argument.
2327 * @param puSessionData Pointer to the session data variable. This must be
2328 * set to a non-zero value on success.
2329 */
2330 DECLR0CALLBACKMEMBER(int, pfnTracerOpen, (PCSUPDRVTRACERREG pThis, PSUPDRVSESSION pSession, uint32_t uCookie, uintptr_t uArg,
2331 uintptr_t *puSessionData));
2332
2333 /**
2334 * I/O control style tracer communication method.
2335 *
2336 *
2337 * @returns VBox status code.
2338 * @param pThis Pointer to the registration record.
2339 * @param pSession The session.
2340 * @param uSessionData The session data value.
2341 * @param uCmd The tracer specific command.
2342 * @param uArg The tracer command specific argument.
2343 * @param piRetVal The tracer specific return value.
2344 */
2345 DECLR0CALLBACKMEMBER(int, pfnTracerIoCtl, (PCSUPDRVTRACERREG pThis, PSUPDRVSESSION pSession, uintptr_t uSessionData,
2346 uintptr_t uCmd, uintptr_t uArg, int32_t *piRetVal));
2347
2348 /**
2349 * Cleans up data the tracer has associated with a session.
2350 *
2351 * @param pThis Pointer to the registration record.
2352 * @param pSession The session handle.
2353 * @param uSessionData The data assoicated with the session.
2354 */
2355 DECLR0CALLBACKMEMBER(void, pfnTracerClose, (PCSUPDRVTRACERREG pThis, PSUPDRVSESSION pSession, uintptr_t uSessionData));
2356
2357 /**
2358 * Registers a provider.
2359 *
2360 * @returns VBox status code.
2361 * @param pThis Pointer to the registration record.
2362 * @param pCore The provider core data.
2363 *
2364 * @todo Kernel vs. Userland providers.
2365 */
2366 DECLR0CALLBACKMEMBER(int, pfnProviderRegister, (PCSUPDRVTRACERREG pThis, PSUPDRVVDTPROVIDERCORE pCore));
2367
2368 /**
2369 * Attempts to deregisters a provider.
2370 *
2371 * @returns VINF_SUCCESS or VERR_TRY_AGAIN. If the latter, the provider
2372 * should be made as harmless as possible before returning as the
2373 * VTG object and associated code will be unloaded upon return.
2374 *
2375 * @param pThis Pointer to the registration record.
2376 * @param pCore The provider core data.
2377 */
2378 DECLR0CALLBACKMEMBER(int, pfnProviderDeregister, (PCSUPDRVTRACERREG pThis, PSUPDRVVDTPROVIDERCORE pCore));
2379
2380 /**
2381 * Make another attempt at unregister a busy provider.
2382 *
2383 * @returns VINF_SUCCESS or VERR_TRY_AGAIN.
2384 * @param pThis Pointer to the registration record.
2385 * @param pCore The provider core data.
2386 */
2387 DECLR0CALLBACKMEMBER(int, pfnProviderDeregisterZombie, (PCSUPDRVTRACERREG pThis, PSUPDRVVDTPROVIDERCORE pCore));
2388
2389 /** End marker (SUPDRVTRACERREG_MAGIC). */
2390 uintptr_t uEndMagic;
2391} SUPDRVTRACERREG;
2392
2393/** Tracer magic (Kenny Garrett). */
2394#define SUPDRVTRACERREG_MAGIC UINT32_C(0x19601009)
2395/** Tracer registration structure version. */
2396#define SUPDRVTRACERREG_VERSION RT_MAKE_U32(0, 1)
2397
2398/** Pointer to a trace helper structure. */
2399typedef struct SUPDRVTRACERHLP const *PCSUPDRVTRACERHLP;
2400/**
2401 * Helper structure.
2402 */
2403typedef struct SUPDRVTRACERHLP
2404{
2405 /** The structure version (SUPDRVTRACERHLP_VERSION). */
2406 uintptr_t uVersion;
2407
2408 /** @todo ... */
2409
2410 /** End marker (SUPDRVTRACERHLP_VERSION) */
2411 uintptr_t uEndVersion;
2412} SUPDRVTRACERHLP;
2413/** Tracer helper structure version. */
2414#define SUPDRVTRACERHLP_VERSION RT_MAKE_U32(0, 1)
2415
2416SUPR0DECL(int) SUPR0TracerRegisterImpl(void *hMod, PSUPDRVSESSION pSession, PCSUPDRVTRACERREG pReg, PCSUPDRVTRACERHLP *ppHlp);
2417SUPR0DECL(int) SUPR0TracerDeregisterImpl(void *hMod, PSUPDRVSESSION pSession);
2418SUPR0DECL(int) SUPR0TracerRegisterDrv(PSUPDRVSESSION pSession, struct VTGOBJHDR *pVtgHdr, const char *pszName);
2419SUPR0DECL(void) SUPR0TracerDeregisterDrv(PSUPDRVSESSION pSession);
2420SUPR0DECL(int) SUPR0TracerRegisterModule(void *hMod, struct VTGOBJHDR *pVtgHdr);
2421SUPR0DECL(void) SUPR0TracerFireProbe(struct VTGPROBELOC *pVtgProbeLoc, uintptr_t uArg0, uintptr_t uArg1, uintptr_t uArg2,
2422 uintptr_t uArg3, uintptr_t uArg4);
2423SUPR0DECL(void) SUPR0TracerUmodProbeFire(PSUPDRVSESSION pSession, PSUPDRVTRACERUSRCTX pCtx);
2424/** @} */
2425
2426
2427/**
2428 * Service request callback function.
2429 *
2430 * @returns VBox status code.
2431 * @param pSession The caller's session.
2432 * @param uOperation The operation identifier.
2433 * @param u64Arg 64-bit integer argument.
2434 * @param pReqHdr The request header. Input / Output. Optional.
2435 */
2436typedef DECLCALLBACKTYPE(int, FNSUPR0SERVICEREQHANDLER,(PSUPDRVSESSION pSession, uint32_t uOperation,
2437 uint64_t u64Arg, PSUPR0SERVICEREQHDR pReqHdr));
2438/** Pointer to a FNR0SERVICEREQHANDLER(). */
2439typedef R0PTRTYPE(FNSUPR0SERVICEREQHANDLER *) PFNSUPR0SERVICEREQHANDLER;
2440
2441
2442/** @defgroup grp_sup_r0_idc The IDC Interface
2443 * @{
2444 */
2445
2446/** The current SUPDRV IDC version.
2447 * This follows the usual high word / low word rules, i.e. high word is the
2448 * major number and it signifies incompatible interface changes. */
2449#define SUPDRV_IDC_VERSION UINT32_C(0x00010000)
2450
2451/**
2452 * Inter-Driver Communication Handle.
2453 */
2454typedef union SUPDRVIDCHANDLE
2455{
2456 /** Padding for opaque usage.
2457 * Must be greater or equal in size than the private struct. */
2458 void *apvPadding[4];
2459#ifdef SUPDRVIDCHANDLEPRIVATE_DECLARED
2460 /** The private view. */
2461 struct SUPDRVIDCHANDLEPRIVATE s;
2462#endif
2463} SUPDRVIDCHANDLE;
2464/** Pointer to a handle. */
2465typedef SUPDRVIDCHANDLE *PSUPDRVIDCHANDLE;
2466
2467SUPR0DECL(int) SUPR0IdcOpen(PSUPDRVIDCHANDLE pHandle, uint32_t uReqVersion, uint32_t uMinVersion,
2468 uint32_t *puSessionVersion, uint32_t *puDriverVersion, uint32_t *puDriverRevision);
2469SUPR0DECL(int) SUPR0IdcCall(PSUPDRVIDCHANDLE pHandle, uint32_t iReq, void *pvReq, uint32_t cbReq);
2470SUPR0DECL(int) SUPR0IdcClose(PSUPDRVIDCHANDLE pHandle);
2471SUPR0DECL(PSUPDRVSESSION) SUPR0IdcGetSession(PSUPDRVIDCHANDLE pHandle);
2472SUPR0DECL(int) SUPR0IdcComponentRegisterFactory(PSUPDRVIDCHANDLE pHandle, PCSUPDRVFACTORY pFactory);
2473SUPR0DECL(int) SUPR0IdcComponentDeregisterFactory(PSUPDRVIDCHANDLE pHandle, PCSUPDRVFACTORY pFactory);
2474
2475/** @} */
2476
2477/** @name Ring-0 module entry points.
2478 *
2479 * These can be exported by ring-0 modules SUP are told to load.
2480 *
2481 * @{ */
2482DECLEXPORT(int) ModuleInit(void *hMod);
2483DECLEXPORT(void) ModuleTerm(void *hMod);
2484/** @} */
2485
2486
2487/** @} */
2488#endif
2489
2490
2491/** @name Trust Anchors and Certificates
2492 * @{ */
2493
2494/**
2495 * Trust anchor table entry (in generated Certificates.cpp).
2496 */
2497typedef struct SUPTAENTRY
2498{
2499 /** Pointer to the raw bytes. */
2500 const unsigned char *pch;
2501 /** Number of bytes. */
2502 unsigned cb;
2503} SUPTAENTRY;
2504/** Pointer to a trust anchor table entry. */
2505typedef SUPTAENTRY const *PCSUPTAENTRY;
2506
2507/** Macro for simplifying generating the trust anchor tables. */
2508#define SUPTAENTRY_GEN(a_abTA) { &a_abTA[0], sizeof(a_abTA) }
2509
2510/** All certificates we know. */
2511extern SUPTAENTRY const g_aSUPAllTAs[];
2512/** Number of entries in g_aSUPAllTAs. */
2513extern unsigned const g_cSUPAllTAs;
2514
2515/** Software publisher certificate roots (Authenticode). */
2516extern SUPTAENTRY const g_aSUPSpcRootTAs[];
2517/** Number of entries in g_aSUPSpcRootTAs. */
2518extern unsigned const g_cSUPSpcRootTAs;
2519
2520/** Kernel root certificates used by Windows. */
2521extern SUPTAENTRY const g_aSUPNtKernelRootTAs[];
2522/** Number of entries in g_aSUPNtKernelRootTAs. */
2523extern unsigned const g_cSUPNtKernelRootTAs;
2524
2525/** Timestamp root certificates trusted by Windows. */
2526extern SUPTAENTRY const g_aSUPTimestampTAs[];
2527/** Number of entries in g_aSUPTimestampTAs. */
2528extern unsigned const g_cSUPTimestampTAs;
2529
2530/** Root certificates trusted by Apple code signing. */
2531extern SUPTAENTRY const g_aSUPAppleRootTAs[];
2532/** Number of entries in g_cSUPAppleRootTAs. */
2533extern unsigned const g_cSUPAppleRootTAs;
2534
2535/** TAs we trust (the build certificate, Oracle VirtualBox). */
2536extern SUPTAENTRY const g_aSUPTrustedTAs[];
2537/** Number of entries in g_aSUPTrustedTAs. */
2538extern unsigned const g_cSUPTrustedTAs;
2539
2540/** Supplemental certificates, like cross signing certificates. */
2541extern SUPTAENTRY const g_aSUPSupplementalTAs[];
2542/** Number of entries in g_aSUPTrustedTAs. */
2543extern unsigned const g_cSUPSupplementalTAs;
2544
2545/** The build certificate. */
2546extern const unsigned char g_abSUPBuildCert[];
2547/** The size of the build certificate. */
2548extern const unsigned g_cbSUPBuildCert;
2549
2550/** @} */
2551
2552
2553/** @} */
2554
2555RT_C_DECLS_END
2556
2557#endif /* !VBOX_INCLUDED_sup_h */
2558
Note: See TracBrowser for help on using the repository browser.

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