VirtualBox

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

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

*: scm --fix-header-guard-endif

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

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