VirtualBox

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

Last change on this file since 85450 was 85450, checked in by vboxsync, 5 years ago

HostDrivers/Support: Added reading the SMM address/mask MSRs while reading the rest of the AMD-V MSRs from the host.

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

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