VirtualBox

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

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

SUPDrv: Added reporting of VT-x's VMCS shadowing CPU feature as part of SUPR0QueryVTCaps.

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