VirtualBox

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

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

SUP: Added a PRTERRINFO parameter to SUPR3LoadVMM. [doxyfix] bugref:9801

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 94.2 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 * @param pszFilename Full path to the VMMR0.r0 file (silly).
1559 * @param pErrInfo Where to return extended error information.
1560 * Optional.
1561 */
1562SUPR3DECL(int) SUPR3LoadVMM(const char *pszFilename, PRTERRINFO pErrInfo);
1563
1564/**
1565 * Unloads R0 HC VMM code.
1566 *
1567 * @returns VBox status code.
1568 * @deprecated Use SUPR3FreeModule().
1569 */
1570SUPR3DECL(int) SUPR3UnloadVMM(void);
1571
1572/**
1573 * Get the physical address of the GIP.
1574 *
1575 * @returns VBox status code.
1576 * @param pHCPhys Where to store the physical address of the GIP.
1577 */
1578SUPR3DECL(int) SUPR3GipGetPhys(PRTHCPHYS pHCPhys);
1579
1580/**
1581 * Initializes only the bits relevant for the SUPR3HardenedVerify* APIs.
1582 *
1583 * This is for users that don't necessarily need to initialize the whole of
1584 * SUPLib. There is no harm in calling this one more time.
1585 *
1586 * @returns VBox status code.
1587 * @remarks Currently not counted, so only call once.
1588 */
1589SUPR3DECL(int) SUPR3HardenedVerifyInit(void);
1590
1591/**
1592 * Reverses the effect of SUPR3HardenedVerifyInit if SUPR3InitEx hasn't been
1593 * called.
1594 *
1595 * Ignored if the support library was initialized using SUPR3Init or
1596 * SUPR3InitEx.
1597 *
1598 * @returns VBox status code.
1599 */
1600SUPR3DECL(int) SUPR3HardenedVerifyTerm(void);
1601
1602/**
1603 * Verifies the integrity of a file, and optionally opens it.
1604 *
1605 * The integrity check is for whether the file is suitable for loading into
1606 * the hypervisor or VM process. The integrity check may include verifying
1607 * the authenticode/elfsign/whatever signature of the file, which can take
1608 * a little while.
1609 *
1610 * @returns VBox status code. On failure it will have printed a LogRel message.
1611 *
1612 * @param pszFilename The file.
1613 * @param pszWhat For the LogRel on failure.
1614 * @param phFile Where to store the handle to the opened file. This is optional, pass NULL
1615 * if the file should not be opened.
1616 * @deprecated Write a new one.
1617 */
1618SUPR3DECL(int) SUPR3HardenedVerifyFile(const char *pszFilename, const char *pszWhat, PRTFILE phFile);
1619
1620/**
1621 * Verifies the integrity of a the current process, including the image
1622 * location and that the invocation was absolute.
1623 *
1624 * This must currently be called after initializing the runtime. The intended
1625 * audience is set-uid-to-root applications, root services and similar.
1626 *
1627 * @returns VBox status code. On failure
1628 * message.
1629 * @param pszArgv0 The first argument to main().
1630 * @param fInternal Set this to @c true if this is an internal
1631 * VirtualBox application. Otherwise pass @c false.
1632 * @param pErrInfo Where to return extended error information.
1633 */
1634SUPR3DECL(int) SUPR3HardenedVerifySelf(const char *pszArgv0, bool fInternal, PRTERRINFO pErrInfo);
1635
1636/**
1637 * Verifies the integrity of an installation directory.
1638 *
1639 * The integrity check verifies that the directory cannot be tampered with by
1640 * normal users on the system. On Unix this translates to root ownership and
1641 * no symbolic linking.
1642 *
1643 * @returns VBox status code. On failure a message will be stored in @a pszErr.
1644 *
1645 * @param pszDirPath The directory path.
1646 * @param fRecursive Whether the check should be recursive or
1647 * not. When set, all sub-directores will be checked,
1648 * including files (@a fCheckFiles is ignored).
1649 * @param fCheckFiles Whether to apply the same basic integrity check to
1650 * the files in the directory as the directory itself.
1651 * @param pErrInfo Where to return extended error information.
1652 * Optional.
1653 */
1654SUPR3DECL(int) SUPR3HardenedVerifyDir(const char *pszDirPath, bool fRecursive, bool fCheckFiles, PRTERRINFO pErrInfo);
1655
1656/**
1657 * Verifies the integrity of a plug-in module.
1658 *
1659 * This is similar to SUPR3HardenedLdrLoad, except it does not load the module
1660 * and that the module does not have to be shipped with VirtualBox.
1661 *
1662 * @returns VBox status code. On failure a message will be stored in @a pszErr.
1663 *
1664 * @param pszFilename The filename of the plug-in module (nothing can be
1665 * omitted here).
1666 * @param pErrInfo Where to return extended error information.
1667 * Optional.
1668 */
1669SUPR3DECL(int) SUPR3HardenedVerifyPlugIn(const char *pszFilename, PRTERRINFO pErrInfo);
1670
1671/**
1672 * Same as RTLdrLoad() but will verify the files it loads (hardened builds).
1673 *
1674 * Will add dll suffix if missing and try load the file.
1675 *
1676 * @returns iprt status code.
1677 * @param pszFilename Image filename. This must have a path.
1678 * @param phLdrMod Where to store the handle to the loaded module.
1679 * @param fFlags See RTLDRLOAD_FLAGS_XXX.
1680 * @param pErrInfo Where to return extended error information.
1681 * Optional.
1682 */
1683SUPR3DECL(int) SUPR3HardenedLdrLoad(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo);
1684
1685/**
1686 * Same as RTLdrLoadAppPriv() but it will verify the files it loads (hardened
1687 * builds).
1688 *
1689 * Will add dll suffix to the file if missing, then look for it in the
1690 * architecture dependent application directory.
1691 *
1692 * @returns iprt status code.
1693 * @param pszFilename Image filename.
1694 * @param phLdrMod Where to store the handle to the loaded module.
1695 * @param fFlags See RTLDRLOAD_FLAGS_XXX.
1696 * @param pErrInfo Where to return extended error information.
1697 * Optional.
1698 */
1699SUPR3DECL(int) SUPR3HardenedLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo);
1700
1701/**
1702 * Same as RTLdrLoad() but will verify the files it loads (hardened builds).
1703 *
1704 * This differs from SUPR3HardenedLdrLoad() in that it can load modules from
1705 * extension packs and anything else safely installed on the system, provided
1706 * they pass the hardening tests.
1707 *
1708 * @returns iprt status code.
1709 * @param pszFilename The full path to the module, with extension.
1710 * @param phLdrMod Where to store the handle to the loaded module.
1711 * @param pErrInfo Where to return extended error information.
1712 * Optional.
1713 */
1714SUPR3DECL(int) SUPR3HardenedLdrLoadPlugIn(const char *pszFilename, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo);
1715
1716/**
1717 * Check if the host kernel can run in VMX root mode.
1718 *
1719 * @returns VINF_SUCCESS if supported, error code indicating why if not.
1720 * @param ppszWhy Where to return an explanatory message on failure.
1721 */
1722SUPR3DECL(int) SUPR3QueryVTxSupported(const char **ppszWhy);
1723
1724/**
1725 * Return VT-x/AMD-V capabilities.
1726 *
1727 * @returns VINF_SUCCESS if supported, error code indicating why if not.
1728 * @param pfCaps Pointer to capability dword (out).
1729 * @todo Intended for main, which means we need to relax the privilege requires
1730 * when accessing certain vboxdrv functions.
1731 */
1732SUPR3DECL(int) SUPR3QueryVTCaps(uint32_t *pfCaps);
1733
1734/**
1735 * Check if NEM is supported when no VT-x/AMD-V is indicated by the CPU.
1736 *
1737 * This is really only for the windows case where we're running in a root
1738 * partition and isn't allowed to use the hardware directly.
1739 *
1740 * @returns True if NEM API support, false if not.
1741 */
1742SUPR3DECL(bool) SUPR3IsNemSupportedWhenNoVtxOrAmdV(void);
1743
1744/**
1745 * Open the tracer.
1746 *
1747 * @returns VBox status code.
1748 * @param uCookie Cookie identifying the tracer we expect to talk to.
1749 * @param uArg Tracer specific open argument.
1750 */
1751SUPR3DECL(int) SUPR3TracerOpen(uint32_t uCookie, uintptr_t uArg);
1752
1753/**
1754 * Closes the tracer.
1755 *
1756 * @returns VBox status code.
1757 */
1758SUPR3DECL(int) SUPR3TracerClose(void);
1759
1760/**
1761 * Perform an I/O request on the tracer.
1762 *
1763 * @returns VBox status.
1764 * @param uCmd The tracer command.
1765 * @param uArg The argument.
1766 * @param piRetVal Where to store the tracer return value.
1767 */
1768SUPR3DECL(int) SUPR3TracerIoCtl(uintptr_t uCmd, uintptr_t uArg, int32_t *piRetVal);
1769
1770/**
1771 * Registers the user module with the tracer.
1772 *
1773 * @returns VBox status code.
1774 * @param hModNative Native module handle. Pass ~(uintptr_t)0 if not
1775 * at hand.
1776 * @param pszModule The module name.
1777 * @param pVtgHdr The VTG header.
1778 * @param uVtgHdrAddr The address to which the VTG header is loaded
1779 * in the relevant execution context.
1780 * @param fFlags See SUP_TRACER_UMOD_FLAGS_XXX
1781 */
1782SUPR3DECL(int) SUPR3TracerRegisterModule(uintptr_t hModNative, const char *pszModule, struct VTGOBJHDR *pVtgHdr,
1783 RTUINTPTR uVtgHdrAddr, uint32_t fFlags);
1784
1785/**
1786 * Deregisters the user module.
1787 *
1788 * @returns VBox status code.
1789 * @param pVtgHdr The VTG header.
1790 */
1791SUPR3DECL(int) SUPR3TracerDeregisterModule(struct VTGOBJHDR *pVtgHdr);
1792
1793/**
1794 * Fire the probe.
1795 *
1796 * @param pVtgProbeLoc The probe location record.
1797 * @param uArg0 Raw probe argument 0.
1798 * @param uArg1 Raw probe argument 1.
1799 * @param uArg2 Raw probe argument 2.
1800 * @param uArg3 Raw probe argument 3.
1801 * @param uArg4 Raw probe argument 4.
1802 */
1803SUPDECL(void) SUPTracerFireProbe(struct VTGPROBELOC *pVtgProbeLoc, uintptr_t uArg0, uintptr_t uArg1, uintptr_t uArg2,
1804 uintptr_t uArg3, uintptr_t uArg4);
1805
1806/**
1807 * Attempts to read the value of an MSR.
1808 *
1809 * @returns VBox status code.
1810 * @param uMsr The MSR to read.
1811 * @param idCpu The CPU to read it on, NIL_RTCPUID if it doesn't
1812 * matter which CPU.
1813 * @param puValue Where to return the value.
1814 * @param pfGp Where to store the \#GP indicator for the read
1815 * operation.
1816 */
1817SUPR3DECL(int) SUPR3MsrProberRead(uint32_t uMsr, RTCPUID idCpu, uint64_t *puValue, bool *pfGp);
1818
1819/**
1820 * Attempts to write to an MSR.
1821 *
1822 * @returns VBox status code.
1823 * @param uMsr The MSR to write to.
1824 * @param idCpu The CPU to wrtie it on, NIL_RTCPUID if it
1825 * doesn't matter which CPU.
1826 * @param uValue The value to write.
1827 * @param pfGp Where to store the \#GP indicator for the write
1828 * operation.
1829 */
1830SUPR3DECL(int) SUPR3MsrProberWrite(uint32_t uMsr, RTCPUID idCpu, uint64_t uValue, bool *pfGp);
1831
1832/**
1833 * Attempts to modify the value of an MSR.
1834 *
1835 * @returns VBox status code.
1836 * @param uMsr The MSR to modify.
1837 * @param idCpu The CPU to modify it on, NIL_RTCPUID if it
1838 * doesn't matter which CPU.
1839 * @param fAndMask The bits to keep in the current MSR value.
1840 * @param fOrMask The bits to set before writing.
1841 * @param pResult The result buffer.
1842 */
1843SUPR3DECL(int) SUPR3MsrProberModify(uint32_t uMsr, RTCPUID idCpu, uint64_t fAndMask, uint64_t fOrMask,
1844 PSUPMSRPROBERMODIFYRESULT pResult);
1845
1846/**
1847 * Attempts to modify the value of an MSR, extended version.
1848 *
1849 * @returns VBox status code.
1850 * @param uMsr The MSR to modify.
1851 * @param idCpu The CPU to modify it on, NIL_RTCPUID if it
1852 * doesn't matter which CPU.
1853 * @param fAndMask The bits to keep in the current MSR value.
1854 * @param fOrMask The bits to set before writing.
1855 * @param fFaster If set to @c true some cache/tlb invalidation is
1856 * skipped, otherwise behave like
1857 * SUPR3MsrProberModify.
1858 * @param pResult The result buffer.
1859 */
1860SUPR3DECL(int) SUPR3MsrProberModifyEx(uint32_t uMsr, RTCPUID idCpu, uint64_t fAndMask, uint64_t fOrMask, bool fFaster,
1861 PSUPMSRPROBERMODIFYRESULT pResult);
1862
1863/**
1864 * Resume built-in keyboard on MacBook Air and Pro hosts.
1865 *
1866 * @returns VBox status code.
1867 */
1868SUPR3DECL(int) SUPR3ResumeSuspendedKeyboards(void);
1869
1870/**
1871 * Measure the TSC-delta for the specified CPU.
1872 *
1873 * @returns VBox status code.
1874 * @param idCpu The CPU to measure the TSC-delta for.
1875 * @param fAsync Whether the measurement is asynchronous, returns
1876 * immediately after signalling a measurement
1877 * request.
1878 * @param fForce Whether to perform a measurement even if the
1879 * specified CPU has a (possibly) valid TSC delta.
1880 * @param cRetries Number of times to retry failed delta
1881 * measurements.
1882 * @param cMsWaitRetry Number of milliseconds to wait between retries.
1883 */
1884SUPR3DECL(int) SUPR3TscDeltaMeasure(RTCPUID idCpu, bool fAsync, bool fForce, uint8_t cRetries, uint8_t cMsWaitRetry);
1885
1886/**
1887 * Reads the delta-adjust TSC value.
1888 *
1889 * @returns VBox status code.
1890 * @param puTsc Where to store the read TSC value.
1891 * @param pidApic Where to store the APIC ID of the CPU where the TSC
1892 * was read (optional, can be NULL).
1893 */
1894SUPR3DECL(int) SUPR3ReadTsc(uint64_t *puTsc, uint16_t *pidApic);
1895
1896/**
1897 * Modifies the GIP flags.
1898 *
1899 * @returns VBox status code.
1900 * @param fOrMask The OR mask of the GIP flags, see SUPGIP_FLAGS_XXX.
1901 * @param fAndMask The AND mask of the GIP flags, see SUPGIP_FLAGS_XXX.
1902 */
1903SUPR3DECL(int) SUPR3GipSetFlags(uint32_t fOrMask, uint32_t fAndMask);
1904
1905/**
1906 * Return processor microcode revision, if applicable.
1907 *
1908 * @returns VINF_SUCCESS if supported, error code indicating why if not.
1909 * @param puMicrocodeRev Pointer to microcode revision dword (out).
1910 */
1911SUPR3DECL(int) SUPR3QueryMicrocodeRev(uint32_t *puMicrocodeRev);
1912
1913/**
1914 * Gets hardware-virtualization MSRs of the CPU, if available.
1915 *
1916 * @returns VINF_SUCCESS if available, error code indicating why if not.
1917 * @param pHwvirtMsrs Where to store the hardware-virtualization MSRs.
1918 * @param fForceRequery Whether to force complete re-querying of MSRs (rather
1919 * than fetching cached values when available).
1920 */
1921SUPR3DECL(int) SUPR3GetHwvirtMsrs(PSUPHWVIRTMSRS pHwvirtMsrs, bool fForceRequery);
1922
1923/** @} */
1924#endif /* IN_RING3 */
1925
1926
1927/** @name User mode module flags (SUPR3TracerRegisterModule & SUP_IOCTL_TRACER_UMOD_REG).
1928 * @{ */
1929/** Executable image. */
1930#define SUP_TRACER_UMOD_FLAGS_EXE UINT32_C(1)
1931/** Shared library (DLL, DYLIB, SO, etc). */
1932#define SUP_TRACER_UMOD_FLAGS_SHARED UINT32_C(2)
1933/** Image type mask. */
1934#define SUP_TRACER_UMOD_FLAGS_TYPE_MASK UINT32_C(3)
1935/** @} */
1936
1937
1938#ifdef IN_RING0
1939/** @defgroup grp_sup_r0 SUP Host Context Ring-0 API
1940 * @{
1941 */
1942
1943/**
1944 * Security objectype.
1945 */
1946typedef enum SUPDRVOBJTYPE
1947{
1948 /** The usual invalid object. */
1949 SUPDRVOBJTYPE_INVALID = 0,
1950 /** A Virtual Machine instance. */
1951 SUPDRVOBJTYPE_VM,
1952 /** Internal network. */
1953 SUPDRVOBJTYPE_INTERNAL_NETWORK,
1954 /** Internal network interface. */
1955 SUPDRVOBJTYPE_INTERNAL_NETWORK_INTERFACE,
1956 /** Single release event semaphore. */
1957 SUPDRVOBJTYPE_SEM_EVENT,
1958 /** Multiple release event semaphore. */
1959 SUPDRVOBJTYPE_SEM_EVENT_MULTI,
1960 /** Raw PCI device. */
1961 SUPDRVOBJTYPE_RAW_PCI_DEVICE,
1962 /** The first invalid object type in this end. */
1963 SUPDRVOBJTYPE_END,
1964 /** The usual 32-bit type size hack. */
1965 SUPDRVOBJTYPE_32_BIT_HACK = 0x7ffffff
1966} SUPDRVOBJTYPE;
1967
1968/**
1969 * Object destructor callback.
1970 * This is called for reference counted objectes when the count reaches 0.
1971 *
1972 * @param pvObj The object pointer.
1973 * @param pvUser1 The first user argument.
1974 * @param pvUser2 The second user argument.
1975 */
1976typedef DECLCALLBACKTYPE(void, FNSUPDRVDESTRUCTOR,(void *pvObj, void *pvUser1, void *pvUser2));
1977/** Pointer to a FNSUPDRVDESTRUCTOR(). */
1978typedef FNSUPDRVDESTRUCTOR *PFNSUPDRVDESTRUCTOR;
1979
1980SUPR0DECL(void *) SUPR0ObjRegister(PSUPDRVSESSION pSession, SUPDRVOBJTYPE enmType, PFNSUPDRVDESTRUCTOR pfnDestructor, void *pvUser1, void *pvUser2);
1981SUPR0DECL(int) SUPR0ObjAddRef(void *pvObj, PSUPDRVSESSION pSession);
1982SUPR0DECL(int) SUPR0ObjAddRefEx(void *pvObj, PSUPDRVSESSION pSession, bool fNoBlocking);
1983SUPR0DECL(int) SUPR0ObjRelease(void *pvObj, PSUPDRVSESSION pSession);
1984SUPR0DECL(int) SUPR0ObjVerifyAccess(void *pvObj, PSUPDRVSESSION pSession, const char *pszObjName);
1985
1986SUPR0DECL(PVM) SUPR0GetSessionVM(PSUPDRVSESSION pSession);
1987SUPR0DECL(PGVM) SUPR0GetSessionGVM(PSUPDRVSESSION pSession);
1988SUPR0DECL(int) SUPR0SetSessionVM(PSUPDRVSESSION pSession, PGVM pGVM, PVM pVM);
1989
1990SUPR0DECL(int) SUPR0LockMem(PSUPDRVSESSION pSession, RTR3PTR pvR3, uint32_t cPages, PRTHCPHYS paPages);
1991SUPR0DECL(int) SUPR0UnlockMem(PSUPDRVSESSION pSession, RTR3PTR pvR3);
1992SUPR0DECL(int) SUPR0ContAlloc(PSUPDRVSESSION pSession, uint32_t cPages, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PRTHCPHYS pHCPhys);
1993SUPR0DECL(int) SUPR0ContFree(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr);
1994SUPR0DECL(int) SUPR0LowAlloc(PSUPDRVSESSION pSession, uint32_t cPages, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PRTHCPHYS paPages);
1995SUPR0DECL(int) SUPR0LowFree(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr);
1996SUPR0DECL(int) SUPR0MemAlloc(PSUPDRVSESSION pSession, uint32_t cb, PRTR0PTR ppvR0, PRTR3PTR ppvR3);
1997SUPR0DECL(int) SUPR0MemGetPhys(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr, PSUPPAGE paPages);
1998SUPR0DECL(int) SUPR0MemFree(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr);
1999SUPR0DECL(int) SUPR0PageAllocEx(PSUPDRVSESSION pSession, uint32_t cPages, uint32_t fFlags, PRTR3PTR ppvR3, PRTR0PTR ppvR0, PRTHCPHYS paPages);
2000SUPR0DECL(int) SUPR0PageMapKernel(PSUPDRVSESSION pSession, RTR3PTR pvR3, uint32_t offSub, uint32_t cbSub, uint32_t fFlags, PRTR0PTR ppvR0);
2001SUPR0DECL(int) SUPR0PageProtect(PSUPDRVSESSION pSession, RTR3PTR pvR3, RTR0PTR pvR0, uint32_t offSub, uint32_t cbSub, uint32_t fProt);
2002SUPR0DECL(int) SUPR0PageFree(PSUPDRVSESSION pSession, RTR3PTR pvR3);
2003SUPR0DECL(int) SUPR0GipMap(PSUPDRVSESSION pSession, PRTR3PTR ppGipR3, PRTHCPHYS pHCPhysGip);
2004SUPR0DECL(int) SUPR0LdrLock(PSUPDRVSESSION pSession);
2005SUPR0DECL(int) SUPR0LdrUnlock(PSUPDRVSESSION pSession);
2006SUPR0DECL(bool) SUPR0LdrIsLockOwnerByMod(void *hMod, bool fWantToHear);
2007SUPR0DECL(int) SUPR0LdrModByName(PSUPDRVSESSION pSession, const char *pszName, void **phMod);
2008SUPR0DECL(int) SUPR0LdrModRetain(PSUPDRVSESSION pSession, void *hMod);
2009SUPR0DECL(int) SUPR0LdrModRelease(PSUPDRVSESSION pSession, void *hMod);
2010SUPR0DECL(int) SUPR0GetVTSupport(uint32_t *pfCaps);
2011SUPR0DECL(int) SUPR0GetHwvirtMsrs(PSUPHWVIRTMSRS pMsrs, uint32_t fCaps, bool fForce);
2012SUPR0DECL(int) SUPR0GetSvmUsability(bool fInitSvm);
2013SUPR0DECL(int) SUPR0GetVmxUsability(bool *pfIsSmxModeAmbiguous);
2014SUPR0DECL(int) SUPR0GetCurrentGdtRw(RTHCUINTPTR *pGdtRw);
2015SUPR0DECL(int) SUPR0QueryVTCaps(PSUPDRVSESSION pSession, uint32_t *pfCaps);
2016SUPR0DECL(int) SUPR0GipUnmap(PSUPDRVSESSION pSession);
2017SUPR0DECL(int) SUPR0QueryUcodeRev(PSUPDRVSESSION pSession, uint32_t *puMicrocodeRev);
2018SUPR0DECL(SUPPAGINGMODE) SUPR0GetPagingMode(void);
2019SUPR0DECL(RTCCUINTREG) SUPR0ChangeCR4(RTCCUINTREG fOrMask, RTCCUINTREG fAndMask);
2020SUPR0DECL(int) SUPR0EnableVTx(bool fEnable);
2021SUPR0DECL(bool) SUPR0SuspendVTxOnCpu(void);
2022SUPR0DECL(void) SUPR0ResumeVTxOnCpu(bool fSuspended);
2023#define SUP_TSCDELTA_MEASURE_F_FORCE RT_BIT_32(0)
2024#define SUP_TSCDELTA_MEASURE_F_ASYNC RT_BIT_32(1)
2025#define SUP_TSCDELTA_MEASURE_F_VALID_MASK UINT32_C(0x00000003)
2026SUPR0DECL(int) SUPR0TscDeltaMeasureBySetIndex(PSUPDRVSESSION pSession, uint32_t iCpuSet, uint32_t fFlags,
2027 RTMSINTERVAL cMsWaitRetry, RTMSINTERVAL cMsWaitThread, uint32_t cTries);
2028
2029SUPR0DECL(void) SUPR0BadContext(PSUPDRVSESSION pSession, const char *pszFile, uint32_t uLine, const char *pszExpr);
2030
2031#if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
2032/**
2033 * Translates a physical address to a virtual mapping (valid up to end of page).
2034 * @returns VBox status code.
2035 * @param HCPhys The physical address, must be page aligned.
2036 * @param ppv Where to store the mapping address on success.
2037 */
2038SUPR0DECL(int) SUPR0HCPhysToVirt(RTHCPHYS HCPhys, void **ppv);
2039#endif
2040
2041/** Context structure returned by SUPR0IoCtlSetup for use with
2042 * SUPR0IoCtlPerform and cleaned up by SUPR0IoCtlCleanup. */
2043typedef struct SUPR0IOCTLCTX *PSUPR0IOCTLCTX;
2044
2045/**
2046 * Sets up a I/O control context for the given handle.
2047 *
2048 * @returns VBox status code.
2049 * @param pSession The support driver session.
2050 * @param hHandle The handle.
2051 * @param fFlags Flag, MBZ.
2052 * @param ppCtx Where the context is returned.
2053 */
2054SUPR0DECL(int) SUPR0IoCtlSetupForHandle(PSUPDRVSESSION pSession, intptr_t hHandle, uint32_t fFlags, PSUPR0IOCTLCTX *ppCtx);
2055
2056/**
2057 * Cleans up the I/O control context when done.
2058 *
2059 * This won't close the handle passed to SUPR0IoCtlSetupForHandle.
2060 *
2061 * @returns VBox status code.
2062 * @param pCtx The I/O control context to cleanup.
2063 */
2064SUPR0DECL(int) SUPR0IoCtlCleanup(PSUPR0IOCTLCTX pCtx);
2065
2066/**
2067 * Performs an I/O control operation.
2068 *
2069 * @returns VBox status code.
2070 * @param pCtx The I/O control context returned by
2071 * SUPR0IoCtlSetupForHandle.
2072 * @param uFunction The I/O control function to perform.
2073 * @param pvInput Pointer to input buffer (ring-0).
2074 * @param pvInputUser Ring-3 pointer corresponding to @a pvInput.
2075 * @param cbInput The amount of input. If zero, both input pointers
2076 * are expected to be NULL.
2077 * @param pvOutput Pointer to output buffer (ring-0).
2078 * @param pvOutputUser Ring-3 pointer corresponding to @a pvInput.
2079 * @param cbOutput The amount of input. If zero, both input pointers
2080 * are expected to be NULL.
2081 * @param piNativeRc Where to return the native return code. When
2082 * specified the VBox status code will typically be
2083 * VINF_SUCCESS and the caller have to consult this for
2084 * the actual result of the operation. (This saves
2085 * pointless status code conversion.) Optional.
2086 *
2087 * @note On unix systems where there is only one set of buffers possible,
2088 * pass the same pointers as input and output.
2089 */
2090SUPR0DECL(int) SUPR0IoCtlPerform(PSUPR0IOCTLCTX pCtx, uintptr_t uFunction,
2091 void *pvInput, RTR3PTR pvInputUser, size_t cbInput,
2092 void *pvOutput, RTR3PTR pvOutputUser, size_t cbOutput,
2093 int32_t *piNativeRc);
2094
2095/**
2096 * Writes to the debugger and/or kernel log.
2097 *
2098 * The length of the formatted message is somewhat limited, so keep things short
2099 * and to the point.
2100 *
2101 * @returns Number of bytes written, mabye.
2102 * @param pszFormat IPRT format string.
2103 * @param ... Arguments referenced by the format string.
2104 */
2105SUPR0DECL(int) SUPR0Printf(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
2106
2107/**
2108 * Returns configuration flags of the host kernel.
2109 *
2110 * @returns Combination of SUPKERNELFEATURES_XXX flags.
2111 */
2112SUPR0DECL(uint32_t) SUPR0GetKernelFeatures(void);
2113
2114/** @copydoc RTLogGetDefaultInstanceEx
2115 * @remarks To allow overriding RTLogGetDefaultInstanceEx locally. */
2116SUPR0DECL(struct RTLOGGER *) SUPR0GetDefaultLogInstanceEx(uint32_t fFlagsAndGroup);
2117/** @copydoc RTLogRelGetDefaultInstanceEx
2118 * @remarks To allow overriding RTLogRelGetDefaultInstanceEx locally. */
2119SUPR0DECL(struct RTLOGGER *) SUPR0GetDefaultLogRelInstanceEx(uint32_t fFlagsAndGroup);
2120
2121
2122/** @name Absolute symbols
2123 * Take the address of these, don't try call them.
2124 * @{ */
2125SUPR0DECL(void) SUPR0AbsIs64bit(void);
2126SUPR0DECL(void) SUPR0Abs64bitKernelCS(void);
2127SUPR0DECL(void) SUPR0Abs64bitKernelSS(void);
2128SUPR0DECL(void) SUPR0Abs64bitKernelDS(void);
2129SUPR0DECL(void) SUPR0AbsKernelCS(void);
2130SUPR0DECL(void) SUPR0AbsKernelSS(void);
2131SUPR0DECL(void) SUPR0AbsKernelDS(void);
2132SUPR0DECL(void) SUPR0AbsKernelES(void);
2133SUPR0DECL(void) SUPR0AbsKernelFS(void);
2134SUPR0DECL(void) SUPR0AbsKernelGS(void);
2135/** @} */
2136
2137/**
2138 * Support driver component factory.
2139 *
2140 * Component factories are registered by drivers that provides services
2141 * such as the host network interface filtering and access to the host
2142 * TCP/IP stack.
2143 *
2144 * @remark Module dependencies and making sure that a component doesn't
2145 * get unloaded while in use, is the sole responsibility of the
2146 * driver/kext/whatever implementing the component.
2147 */
2148typedef struct SUPDRVFACTORY
2149{
2150 /** The (unique) name of the component factory. */
2151 char szName[56];
2152 /**
2153 * Queries a factory interface.
2154 *
2155 * The factory interface is specific to each component and will be be
2156 * found in the header(s) for the component alongside its UUID.
2157 *
2158 * @returns Pointer to the factory interfaces on success, NULL on failure.
2159 *
2160 * @param pSupDrvFactory Pointer to this structure.
2161 * @param pSession The SUPDRV session making the query.
2162 * @param pszInterfaceUuid The UUID of the factory interface.
2163 */
2164 DECLR0CALLBACKMEMBER(void *, pfnQueryFactoryInterface,(struct SUPDRVFACTORY const *pSupDrvFactory, PSUPDRVSESSION pSession, const char *pszInterfaceUuid));
2165} SUPDRVFACTORY;
2166/** Pointer to a support driver factory. */
2167typedef SUPDRVFACTORY *PSUPDRVFACTORY;
2168/** Pointer to a const support driver factory. */
2169typedef SUPDRVFACTORY const *PCSUPDRVFACTORY;
2170
2171SUPR0DECL(int) SUPR0ComponentRegisterFactory(PSUPDRVSESSION pSession, PCSUPDRVFACTORY pFactory);
2172SUPR0DECL(int) SUPR0ComponentDeregisterFactory(PSUPDRVSESSION pSession, PCSUPDRVFACTORY pFactory);
2173SUPR0DECL(int) SUPR0ComponentQueryFactory(PSUPDRVSESSION pSession, const char *pszName, const char *pszInterfaceUuid, void **ppvFactoryIf);
2174
2175
2176/** @name Tracing
2177 * @{ */
2178
2179/**
2180 * Tracer data associated with a provider.
2181 */
2182typedef union SUPDRVTRACERDATA
2183{
2184 /** Generic */
2185 uint64_t au64[2];
2186
2187 /** DTrace data. */
2188 struct
2189 {
2190 /** Provider ID. */
2191 uintptr_t idProvider;
2192 /** The number of trace points provided. */
2193 uint32_t volatile cProvidedProbes;
2194 /** Whether we've invalidated this bugger. */
2195 bool fZombie;
2196 } DTrace;
2197} SUPDRVTRACERDATA;
2198/** Pointer to the tracer data associated with a provider. */
2199typedef SUPDRVTRACERDATA *PSUPDRVTRACERDATA;
2200
2201/**
2202 * Probe location info for ring-0.
2203 *
2204 * Since we cannot trust user tracepoint modules, we need to duplicate the probe
2205 * ID and enabled flag in ring-0.
2206 */
2207typedef struct SUPDRVPROBELOC
2208{
2209 /** The probe ID. */
2210 uint32_t idProbe;
2211 /** Whether it's enabled or not. */
2212 bool fEnabled;
2213} SUPDRVPROBELOC;
2214/** Pointer to a ring-0 probe location record. */
2215typedef SUPDRVPROBELOC *PSUPDRVPROBELOC;
2216
2217/**
2218 * Probe info for ring-0.
2219 *
2220 * Since we cannot trust user tracepoint modules, we need to duplicate the
2221 * probe enable count.
2222 */
2223typedef struct SUPDRVPROBEINFO
2224{
2225 /** The number of times this probe has been enabled. */
2226 uint32_t volatile cEnabled;
2227} SUPDRVPROBEINFO;
2228/** Pointer to a ring-0 probe info record. */
2229typedef SUPDRVPROBEINFO *PSUPDRVPROBEINFO;
2230
2231/**
2232 * Support driver tracepoint provider core.
2233 */
2234typedef struct SUPDRVVDTPROVIDERCORE
2235{
2236 /** The tracer data member. */
2237 SUPDRVTRACERDATA TracerData;
2238 /** Pointer to the provider name (a copy that's always available). */
2239 const char *pszName;
2240 /** Pointer to the module name (a copy that's always available). */
2241 const char *pszModName;
2242
2243 /** The provider descriptor. */
2244 struct VTGDESCPROVIDER *pDesc;
2245 /** The VTG header. */
2246 struct VTGOBJHDR *pHdr;
2247
2248 /** The size of the entries in the pvProbeLocsEn table. */
2249 uint8_t cbProbeLocsEn;
2250 /** The actual module bit count (corresponds to cbProbeLocsEn). */
2251 uint8_t cBits;
2252 /** Set if this is a Umod, otherwise clear. */
2253 bool fUmod;
2254 /** Explicit alignment padding (paranoia). */
2255 uint8_t abAlignment[ARCH_BITS == 32 ? 1 : 5];
2256
2257 /** The probe locations used for descriptive purposes. */
2258 struct VTGPROBELOC const *paProbeLocsRO;
2259 /** Pointer to the probe location array where the enable flag needs
2260 * flipping. For kernel providers, this will always be SUPDRVPROBELOC,
2261 * while user providers can either be 32-bit or 64-bit. Use
2262 * cbProbeLocsEn to calculate the address of an entry. */
2263 void *pvProbeLocsEn;
2264 /** Pointer to the probe array containing the enabled counts. */
2265 uint32_t *pacProbeEnabled;
2266
2267 /** The ring-0 probe location info for user tracepoint modules.
2268 * This is NULL if fUmod is false. */
2269 PSUPDRVPROBELOC paR0ProbeLocs;
2270 /** The ring-0 probe info for user tracepoint modules.
2271 * This is NULL if fUmod is false. */
2272 PSUPDRVPROBEINFO paR0Probes;
2273
2274} SUPDRVVDTPROVIDERCORE;
2275/** Pointer to a tracepoint provider core structure. */
2276typedef SUPDRVVDTPROVIDERCORE *PSUPDRVVDTPROVIDERCORE;
2277
2278/** Pointer to a tracer registration record. */
2279typedef struct SUPDRVTRACERREG const *PCSUPDRVTRACERREG;
2280/**
2281 * Support driver tracer registration record.
2282 */
2283typedef struct SUPDRVTRACERREG
2284{
2285 /** Magic value (SUPDRVTRACERREG_MAGIC). */
2286 uint32_t u32Magic;
2287 /** Version (SUPDRVTRACERREG_VERSION). */
2288 uint32_t u32Version;
2289
2290 /**
2291 * Fire off a kernel probe.
2292 *
2293 * @param pVtgProbeLoc The probe location record.
2294 * @param uArg0 The first raw probe argument.
2295 * @param uArg1 The second raw probe argument.
2296 * @param uArg2 The third raw probe argument.
2297 * @param uArg3 The fourth raw probe argument.
2298 * @param uArg4 The fifth raw probe argument.
2299 *
2300 * @remarks SUPR0TracerFireProbe will do a tail jump thru this member, so
2301 * no extra stack frames will be added.
2302 * @remarks This does not take a 'this' pointer argument because it doesn't map
2303 * well onto VTG or DTrace.
2304 *
2305 */
2306 DECLR0CALLBACKMEMBER(void, pfnProbeFireKernel, (struct VTGPROBELOC *pVtgProbeLoc, uintptr_t uArg0, uintptr_t uArg1, uintptr_t uArg2,
2307 uintptr_t uArg3, uintptr_t uArg4));
2308
2309 /**
2310 * Fire off a user-mode probe.
2311 *
2312 * @param pThis Pointer to the registration record.
2313 *
2314 * @param pVtgProbeLoc The probe location record.
2315 * @param pSession The user session.
2316 * @param pCtx The usermode context info.
2317 * @param pVtgHdr The VTG header (read-only).
2318 * @param pProbeLocRO The read-only probe location record .
2319 */
2320 DECLR0CALLBACKMEMBER(void, pfnProbeFireUser, (PCSUPDRVTRACERREG pThis, PSUPDRVSESSION pSession, PCSUPDRVTRACERUSRCTX pCtx,
2321 struct VTGOBJHDR const *pVtgHdr, struct VTGPROBELOC const *pProbeLocRO));
2322
2323 /**
2324 * Opens up the tracer.
2325 *
2326 * @returns VBox status code.
2327 * @param pThis Pointer to the registration record.
2328 * @param pSession The session doing the opening.
2329 * @param uCookie A cookie (magic) unique to the tracer, so it can
2330 * fend off incompatible clients.
2331 * @param uArg Tracer specific argument.
2332 * @param puSessionData Pointer to the session data variable. This must be
2333 * set to a non-zero value on success.
2334 */
2335 DECLR0CALLBACKMEMBER(int, pfnTracerOpen, (PCSUPDRVTRACERREG pThis, PSUPDRVSESSION pSession, uint32_t uCookie, uintptr_t uArg,
2336 uintptr_t *puSessionData));
2337
2338 /**
2339 * I/O control style tracer communication method.
2340 *
2341 *
2342 * @returns VBox status code.
2343 * @param pThis Pointer to the registration record.
2344 * @param pSession The session.
2345 * @param uSessionData The session data value.
2346 * @param uCmd The tracer specific command.
2347 * @param uArg The tracer command specific argument.
2348 * @param piRetVal The tracer specific return value.
2349 */
2350 DECLR0CALLBACKMEMBER(int, pfnTracerIoCtl, (PCSUPDRVTRACERREG pThis, PSUPDRVSESSION pSession, uintptr_t uSessionData,
2351 uintptr_t uCmd, uintptr_t uArg, int32_t *piRetVal));
2352
2353 /**
2354 * Cleans up data the tracer has associated with a session.
2355 *
2356 * @param pThis Pointer to the registration record.
2357 * @param pSession The session handle.
2358 * @param uSessionData The data assoicated with the session.
2359 */
2360 DECLR0CALLBACKMEMBER(void, pfnTracerClose, (PCSUPDRVTRACERREG pThis, PSUPDRVSESSION pSession, uintptr_t uSessionData));
2361
2362 /**
2363 * Registers a provider.
2364 *
2365 * @returns VBox status code.
2366 * @param pThis Pointer to the registration record.
2367 * @param pCore The provider core data.
2368 *
2369 * @todo Kernel vs. Userland providers.
2370 */
2371 DECLR0CALLBACKMEMBER(int, pfnProviderRegister, (PCSUPDRVTRACERREG pThis, PSUPDRVVDTPROVIDERCORE pCore));
2372
2373 /**
2374 * Attempts to deregisters a provider.
2375 *
2376 * @returns VINF_SUCCESS or VERR_TRY_AGAIN. If the latter, the provider
2377 * should be made as harmless as possible before returning as the
2378 * VTG object and associated code will be unloaded upon return.
2379 *
2380 * @param pThis Pointer to the registration record.
2381 * @param pCore The provider core data.
2382 */
2383 DECLR0CALLBACKMEMBER(int, pfnProviderDeregister, (PCSUPDRVTRACERREG pThis, PSUPDRVVDTPROVIDERCORE pCore));
2384
2385 /**
2386 * Make another attempt at unregister a busy provider.
2387 *
2388 * @returns VINF_SUCCESS or VERR_TRY_AGAIN.
2389 * @param pThis Pointer to the registration record.
2390 * @param pCore The provider core data.
2391 */
2392 DECLR0CALLBACKMEMBER(int, pfnProviderDeregisterZombie, (PCSUPDRVTRACERREG pThis, PSUPDRVVDTPROVIDERCORE pCore));
2393
2394 /** End marker (SUPDRVTRACERREG_MAGIC). */
2395 uintptr_t uEndMagic;
2396} SUPDRVTRACERREG;
2397
2398/** Tracer magic (Kenny Garrett). */
2399#define SUPDRVTRACERREG_MAGIC UINT32_C(0x19601009)
2400/** Tracer registration structure version. */
2401#define SUPDRVTRACERREG_VERSION RT_MAKE_U32(0, 1)
2402
2403/** Pointer to a trace helper structure. */
2404typedef struct SUPDRVTRACERHLP const *PCSUPDRVTRACERHLP;
2405/**
2406 * Helper structure.
2407 */
2408typedef struct SUPDRVTRACERHLP
2409{
2410 /** The structure version (SUPDRVTRACERHLP_VERSION). */
2411 uintptr_t uVersion;
2412
2413 /** @todo ... */
2414
2415 /** End marker (SUPDRVTRACERHLP_VERSION) */
2416 uintptr_t uEndVersion;
2417} SUPDRVTRACERHLP;
2418/** Tracer helper structure version. */
2419#define SUPDRVTRACERHLP_VERSION RT_MAKE_U32(0, 1)
2420
2421SUPR0DECL(int) SUPR0TracerRegisterImpl(void *hMod, PSUPDRVSESSION pSession, PCSUPDRVTRACERREG pReg, PCSUPDRVTRACERHLP *ppHlp);
2422SUPR0DECL(int) SUPR0TracerDeregisterImpl(void *hMod, PSUPDRVSESSION pSession);
2423SUPR0DECL(int) SUPR0TracerRegisterDrv(PSUPDRVSESSION pSession, struct VTGOBJHDR *pVtgHdr, const char *pszName);
2424SUPR0DECL(void) SUPR0TracerDeregisterDrv(PSUPDRVSESSION pSession);
2425SUPR0DECL(int) SUPR0TracerRegisterModule(void *hMod, struct VTGOBJHDR *pVtgHdr);
2426SUPR0DECL(void) SUPR0TracerFireProbe(struct VTGPROBELOC *pVtgProbeLoc, uintptr_t uArg0, uintptr_t uArg1, uintptr_t uArg2,
2427 uintptr_t uArg3, uintptr_t uArg4);
2428SUPR0DECL(void) SUPR0TracerUmodProbeFire(PSUPDRVSESSION pSession, PSUPDRVTRACERUSRCTX pCtx);
2429/** @} */
2430
2431
2432/**
2433 * Service request callback function.
2434 *
2435 * @returns VBox status code.
2436 * @param pSession The caller's session.
2437 * @param uOperation The operation identifier.
2438 * @param u64Arg 64-bit integer argument.
2439 * @param pReqHdr The request header. Input / Output. Optional.
2440 */
2441typedef DECLCALLBACKTYPE(int, FNSUPR0SERVICEREQHANDLER,(PSUPDRVSESSION pSession, uint32_t uOperation,
2442 uint64_t u64Arg, PSUPR0SERVICEREQHDR pReqHdr));
2443/** Pointer to a FNR0SERVICEREQHANDLER(). */
2444typedef R0PTRTYPE(FNSUPR0SERVICEREQHANDLER *) PFNSUPR0SERVICEREQHANDLER;
2445
2446
2447/** @defgroup grp_sup_r0_idc The IDC Interface
2448 * @{
2449 */
2450
2451/** The current SUPDRV IDC version.
2452 * This follows the usual high word / low word rules, i.e. high word is the
2453 * major number and it signifies incompatible interface changes. */
2454#define SUPDRV_IDC_VERSION UINT32_C(0x00010000)
2455
2456/**
2457 * Inter-Driver Communication Handle.
2458 */
2459typedef union SUPDRVIDCHANDLE
2460{
2461 /** Padding for opaque usage.
2462 * Must be greater or equal in size than the private struct. */
2463 void *apvPadding[4];
2464#ifdef SUPDRVIDCHANDLEPRIVATE_DECLARED
2465 /** The private view. */
2466 struct SUPDRVIDCHANDLEPRIVATE s;
2467#endif
2468} SUPDRVIDCHANDLE;
2469/** Pointer to a handle. */
2470typedef SUPDRVIDCHANDLE *PSUPDRVIDCHANDLE;
2471
2472SUPR0DECL(int) SUPR0IdcOpen(PSUPDRVIDCHANDLE pHandle, uint32_t uReqVersion, uint32_t uMinVersion,
2473 uint32_t *puSessionVersion, uint32_t *puDriverVersion, uint32_t *puDriverRevision);
2474SUPR0DECL(int) SUPR0IdcCall(PSUPDRVIDCHANDLE pHandle, uint32_t iReq, void *pvReq, uint32_t cbReq);
2475SUPR0DECL(int) SUPR0IdcClose(PSUPDRVIDCHANDLE pHandle);
2476SUPR0DECL(PSUPDRVSESSION) SUPR0IdcGetSession(PSUPDRVIDCHANDLE pHandle);
2477SUPR0DECL(int) SUPR0IdcComponentRegisterFactory(PSUPDRVIDCHANDLE pHandle, PCSUPDRVFACTORY pFactory);
2478SUPR0DECL(int) SUPR0IdcComponentDeregisterFactory(PSUPDRVIDCHANDLE pHandle, PCSUPDRVFACTORY pFactory);
2479
2480/** @} */
2481
2482/** @name Ring-0 module entry points.
2483 *
2484 * These can be exported by ring-0 modules SUP are told to load.
2485 *
2486 * @{ */
2487DECLEXPORT(int) ModuleInit(void *hMod);
2488DECLEXPORT(void) ModuleTerm(void *hMod);
2489/** @} */
2490
2491
2492/** @} */
2493#endif
2494
2495
2496/** @name Trust Anchors and Certificates
2497 * @{ */
2498
2499/**
2500 * Trust anchor table entry (in generated Certificates.cpp).
2501 */
2502typedef struct SUPTAENTRY
2503{
2504 /** Pointer to the raw bytes. */
2505 const unsigned char *pch;
2506 /** Number of bytes. */
2507 unsigned cb;
2508} SUPTAENTRY;
2509/** Pointer to a trust anchor table entry. */
2510typedef SUPTAENTRY const *PCSUPTAENTRY;
2511
2512/** Macro for simplifying generating the trust anchor tables. */
2513#define SUPTAENTRY_GEN(a_abTA) { &a_abTA[0], sizeof(a_abTA) }
2514
2515/** All certificates we know. */
2516extern SUPTAENTRY const g_aSUPAllTAs[];
2517/** Number of entries in g_aSUPAllTAs. */
2518extern unsigned const g_cSUPAllTAs;
2519
2520/** Software publisher certificate roots (Authenticode). */
2521extern SUPTAENTRY const g_aSUPSpcRootTAs[];
2522/** Number of entries in g_aSUPSpcRootTAs. */
2523extern unsigned const g_cSUPSpcRootTAs;
2524
2525/** Kernel root certificates used by Windows. */
2526extern SUPTAENTRY const g_aSUPNtKernelRootTAs[];
2527/** Number of entries in g_aSUPNtKernelRootTAs. */
2528extern unsigned const g_cSUPNtKernelRootTAs;
2529
2530/** Timestamp root certificates trusted by Windows. */
2531extern SUPTAENTRY const g_aSUPTimestampTAs[];
2532/** Number of entries in g_aSUPTimestampTAs. */
2533extern unsigned const g_cSUPTimestampTAs;
2534
2535/** Root certificates trusted by Apple code signing. */
2536extern SUPTAENTRY const g_aSUPAppleRootTAs[];
2537/** Number of entries in g_cSUPAppleRootTAs. */
2538extern unsigned const g_cSUPAppleRootTAs;
2539
2540/** TAs we trust (the build certificate, Oracle VirtualBox). */
2541extern SUPTAENTRY const g_aSUPTrustedTAs[];
2542/** Number of entries in g_aSUPTrustedTAs. */
2543extern unsigned const g_cSUPTrustedTAs;
2544
2545/** Supplemental certificates, like cross signing certificates. */
2546extern SUPTAENTRY const g_aSUPSupplementalTAs[];
2547/** Number of entries in g_aSUPTrustedTAs. */
2548extern unsigned const g_cSUPSupplementalTAs;
2549
2550/** The build certificate. */
2551extern const unsigned char g_abSUPBuildCert[];
2552/** The size of the build certificate. */
2553extern const unsigned g_cbSUPBuildCert;
2554
2555/** @} */
2556
2557
2558/** @} */
2559
2560RT_C_DECLS_END
2561
2562#endif /* !VBOX_INCLUDED_sup_h */
2563
Note: See TracBrowser for help on using the repository browser.

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