VirtualBox

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

Last change on this file since 54252 was 54252, checked in by vboxsync, 10 years ago

SUPDrv,GIP,TM: Combined fOsTscDeltasInSync and fTscDeltasRoughlyInSync into enmUseTscDelta and added fGetGipCpu. Bumped GIP and support driver versions.

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