VirtualBox

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

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

SUPDrv: Added flag for detecting Virtualized VMSAVE/VMLOAD feature on AMD CPUs.

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