VirtualBox

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

Last change on this file since 81071 was 81071, checked in by vboxsync, 6 years ago

SUPDrv,IPRT,VMM: Support host APIC ID above 256 in GIP. (Only tested on 4 core intel.) bugref:9501

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

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