VirtualBox

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

Last change on this file since 4337 was 4071, checked in by vboxsync, 17 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.2 KB
Line 
1/** @file
2 * SUP - Support Library.
3 */
4
5/*
6 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
12 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
13 * distribution. VirtualBox OSE is distributed in the hope that it will
14 * be useful, but WITHOUT ANY WARRANTY of any kind.
15 */
16
17#ifndef ___VBox_sup_h
18#define ___VBox_sup_h
19
20#include <VBox/cdefs.h>
21#include <VBox/types.h>
22#include <iprt/assert.h>
23#include <iprt/asm.h>
24
25__BEGIN_DECLS
26
27/** @defgroup grp_sup The Support Library API
28 * @{
29 */
30
31/**
32 * Physical page descriptor.
33 */
34#pragma pack(4) /* space is more important. */
35typedef struct SUPPAGE
36{
37 /** Physical memory address. */
38 RTHCPHYS Phys;
39 /** Reserved entry for internal use by the caller. */
40 RTHCUINTPTR uReserved;
41} SUPPAGE;
42#pragma pack()
43/** Pointer to a page descriptor. */
44typedef SUPPAGE *PSUPPAGE;
45/** Pointer to a const page descriptor. */
46typedef const SUPPAGE *PCSUPPAGE;
47
48/**
49 * The paging mode.
50 */
51typedef enum SUPPAGINGMODE
52{
53 /** The usual invalid entry.
54 * This is returned by SUPGetPagingMode() */
55 SUPPAGINGMODE_INVALID = 0,
56 /** Normal 32-bit paging, no global pages */
57 SUPPAGINGMODE_32_BIT,
58 /** Normal 32-bit paging with global pages. */
59 SUPPAGINGMODE_32_BIT_GLOBAL,
60 /** PAE mode, no global pages, no NX. */
61 SUPPAGINGMODE_PAE,
62 /** PAE mode with global pages. */
63 SUPPAGINGMODE_PAE_GLOBAL,
64 /** PAE mode with NX, no global pages. */
65 SUPPAGINGMODE_PAE_NX,
66 /** PAE mode with global pages and NX. */
67 SUPPAGINGMODE_PAE_GLOBAL_NX,
68 /** AMD64 mode, no global pages. */
69 SUPPAGINGMODE_AMD64,
70 /** AMD64 mode with global pages, no NX. */
71 SUPPAGINGMODE_AMD64_GLOBAL,
72 /** AMD64 mode with NX, no global pages. */
73 SUPPAGINGMODE_AMD64_NX,
74 /** AMD64 mode with global pages and NX. */
75 SUPPAGINGMODE_AMD64_GLOBAL_NX
76} SUPPAGINGMODE;
77
78
79#pragma pack(1) /* paranoia */
80
81/**
82 * Per CPU data.
83 * This is only used when
84 */
85typedef struct SUPGIPCPU
86{
87 /** Update transaction number.
88 * This number is incremented at the start and end of each update. It follows
89 * thusly that odd numbers indicates update in progress, while even numbers
90 * indicate stable data. Use this to make sure that the data items you fetch
91 * are consistent. */
92 volatile uint32_t u32TransactionId;
93 /** The interval in TSC ticks between two NanoTS updates.
94 * This is the average interval over the last 2, 4 or 8 updates + a little slack.
95 * The slack makes the time go a tiny tiny bit slower and extends the interval enough
96 * to avoid ending up with too many 1ns increments. */
97 volatile uint32_t u32UpdateIntervalTSC;
98 /** Current nanosecond timestamp. */
99 volatile uint64_t u64NanoTS;
100 /** The TSC at the time of u64NanoTS. */
101 volatile uint64_t u64TSC;
102 /** Current CPU Frequency. */
103 volatile uint64_t u64CpuHz;
104 /** Number of errors during updating.
105 * Typical errors are under/overflows. */
106 volatile uint32_t cErrors;
107 /** Index of the head item in au32TSCHistory. */
108 volatile uint32_t iTSCHistoryHead;
109 /** Array of recent TSC interval deltas.
110 * The most recent item is at index iTSCHistoryHead.
111 * This history is used to calculate u32UpdateIntervalTSC.
112 */
113 volatile uint32_t au32TSCHistory[8];
114 /** Reserved for future per processor data. */
115 volatile uint32_t au32Reserved[6];
116} SUPGIPCPU;
117AssertCompileSize(SUPGIPCPU, 96);
118/*AssertCompileMemberAlignment(SUPGIPCPU, u64TSC, 8); -fixme */
119
120/** Pointer to per cpu data.
121 * @remark there is no const version of this typedef, see g_pSUPGlobalInfoPage for details. */
122typedef SUPGIPCPU *PSUPGIPCPU;
123
124/**
125 * Global Information Page.
126 *
127 * This page contains useful information and can be mapped into any
128 * process or VM. It can be accessed thru the g_pSUPGlobalInfoPage
129 * pointer when a session is open.
130 */
131typedef struct SUPGLOBALINFOPAGE
132{
133 /** Magic (SUPGLOBALINFOPAGE_MAGIC). */
134 uint32_t u32Magic;
135 /** The GIP version. */
136 uint32_t u32Version;
137
138 /** The GIP update mode, see SUPGIPMODE. */
139 uint32_t u32Mode;
140 /** Reserved / padding. */
141 uint32_t u32Padding0;
142 /** The update frequency of the of the NanoTS. */
143 volatile uint32_t u32UpdateHz;
144 /** The update interval in nanoseconds. (10^9 / u32UpdateHz) */
145 volatile uint32_t u32UpdateIntervalNS;
146 /** The timestamp of the last time we update the update frequency. */
147 volatile uint64_t u64NanoTSLastUpdateHz;
148
149 /** Padding / reserved space for future data. */
150 uint32_t au32Padding1[56];
151
152 /** Array of per-cpu data.
153 * If u32Mode == SUPGIPMODE_SYNC_TSC then only the first entry is used.
154 * If u32Mode == SUPGIPMODE_ASYNC_TSC then the CPU ACPI ID is used as an
155 * index into the array. */
156 SUPGIPCPU aCPUs[32];
157} SUPGLOBALINFOPAGE;
158AssertCompile(sizeof(SUPGLOBALINFOPAGE) <= 0x1000);
159/* AssertCompileMemberAlignment(SUPGLOBALINFOPAGE, aCPU, 32); - fixme */
160
161/** Pointer to the global info page.
162 * @remark there is no const version of this typedef, see g_pSUPGlobalInfoPage for details. */
163typedef SUPGLOBALINFOPAGE *PSUPGLOBALINFOPAGE;
164
165#pragma pack() /* end of paranoia */
166
167/** The value of the SUPGLOBALINFOPAGE::u32Magic field. (Soryo Fuyumi) */
168#define SUPGLOBALINFOPAGE_MAGIC 0x19590106
169/** The GIP version.
170 * Upper 16 bits is the major version. Major version is only changed with
171 * incompatible changes in the GIP. */
172#define SUPGLOBALINFOPAGE_VERSION 0x00020000
173
174/**
175 * SUPGLOBALINFOPAGE::u32Mode values.
176 */
177typedef enum SUPGIPMODE
178{
179 /** The usual invalid null entry. */
180 SUPGIPMODE_INVALID = 0,
181 /** The TSC of the cores and cpus in the system is in sync. */
182 SUPGIPMODE_SYNC_TSC,
183 /** Each core has it's own TSC. */
184 SUPGIPMODE_ASYNC_TSC,
185 /** The usual 32-bit hack. */
186 SUPGIPMODE_32BIT_HACK = 0x7fffffff
187} SUPGIPMODE;
188
189/** Pointer to the Global Information Page.
190 *
191 * This pointer is valid as long as SUPLib has a open session. Anyone using
192 * the page must treat this pointer as higly volatile and not trust it beyond
193 * one transaction.
194 *
195 * @remark The GIP page is read-only to everyone but the support driver and
196 * is actually mapped read only everywhere but in ring-0. However
197 * it is not marked 'const' as this might confuse compilers into
198 * thinking that values doesn't change even if members are marked
199 * as volatile. Thus, there is no PCSUPGLOBALINFOPAGE type.
200 */
201#if defined(IN_SUP_R0) || defined(IN_SUP_R3) || defined(IN_SUP_GC)
202extern DECLEXPORT(PSUPGLOBALINFOPAGE) g_pSUPGlobalInfoPage;
203#elif defined(IN_RING0)
204extern DECLIMPORT(SUPGLOBALINFOPAGE) g_SUPGlobalInfoPage;
205# if defined(__GNUC__) && !defined(RT_OS_DARWIN) && defined(RT_ARCH_AMD64)
206/** Workaround for ELF+GCC problem on 64-bit hosts.
207 * (GCC emits a mov with a R_X86_64_32 reloc, we need R_X86_64_64.) */
208DECLINLINE(PSUPGLOBALINFOPAGE) SUPGetGIP(void)
209{
210 PSUPGLOBALINFOPAGE pGIP;
211 __asm__ __volatile__ ("movabs $g_SUPGlobalInfoPage,%0\n\t"
212 : "=a" (pGIP));
213 return pGIP;
214}
215# define g_pSUPGlobalInfoPage (SUPGetGIP())
216# else
217# define g_pSUPGlobalInfoPage (&g_SUPGlobalInfoPage)
218# endif
219#else
220extern DECLIMPORT(PSUPGLOBALINFOPAGE) g_pSUPGlobalInfoPage;
221#endif
222
223
224/**
225 * Gets the TSC frequency of the calling CPU.
226 *
227 * @returns TSC frequency.
228 * @param pGip The GIP pointer.
229 */
230DECLINLINE(uint64_t) SUPGetCpuHzFromGIP(PSUPGLOBALINFOPAGE pGip)
231{
232 unsigned iCpu;
233
234 if (RT_UNLIKELY(!pGip || pGip->u32Magic != SUPGLOBALINFOPAGE_MAGIC))
235 return ~(uint64_t)0;
236
237 if (pGip->u32Mode != SUPGIPMODE_ASYNC_TSC)
238 iCpu = 0;
239 else
240 {
241 iCpu = ASMGetApicId();
242 if (RT_UNLIKELY(iCpu >= RT_ELEMENTS(pGip->aCPUs)))
243 return ~(uint64_t)0;
244 }
245
246 return pGip->aCPUs[iCpu].u64CpuHz;
247}
248
249
250#ifdef IN_RING3
251
252/** @defgroup grp_sup_r3 SUP Host Context Ring 3 API
253 * @ingroup grp_sup
254 * @{
255 */
256
257/**
258 * Installs the support library.
259 *
260 * @returns VBox status code.
261 */
262SUPR3DECL(int) SUPInstall(void);
263
264/**
265 * Uninstalls the support library.
266 *
267 * @returns VBox status code.
268 */
269SUPR3DECL(int) SUPUninstall(void);
270
271/**
272 * Initializes the support library.
273 * Each succesful call to SUPInit() must be countered by a
274 * call to SUPTerm(false).
275 *
276 * @returns VBox status code.
277 * @param ppSession Where to store the session handle. Defaults to NULL.
278 * @param cbReserve The number of bytes of contiguous memory that should be reserved by
279 * the runtime / support library.
280 * Set this to 0 if no reservation is required. (default)
281 * Set this to ~0 if the maximum amount supported by the VM is to be
282 * attempted reserved, or the maximum available.
283 */
284#ifdef __cplusplus
285SUPR3DECL(int) SUPInit(PSUPDRVSESSION *ppSession = NULL, size_t cbReserve = 0);
286#else
287SUPR3DECL(int) SUPInit(PSUPDRVSESSION *ppSession, size_t cbReserve);
288#endif
289
290/**
291 * Terminates the support library.
292 *
293 * @returns VBox status code.
294 * @param fForced Forced termination. This means to ignore the
295 * init call count and just terminated.
296 */
297#ifdef __cplusplus
298SUPR3DECL(int) SUPTerm(bool fForced = false);
299#else
300SUPR3DECL(int) SUPTerm(int fForced);
301#endif
302
303/**
304 * Sets the ring-0 VM handle for use with fast IOCtls.
305 *
306 * @returns VBox status code.
307 * @param pVMR0 The ring-0 VM handle.
308 * NIL_RTR0PTR can be used to unset the handle when the
309 * VM is about to be destroyed.
310 */
311SUPR3DECL(int) SUPSetVMForFastIOCtl(PVMR0 pVMR0);
312
313/**
314 * Calls the HC R0 VMM entry point.
315 * See VMMR0Entry() for more details.
316 *
317 * @returns error code specific to uFunction.
318 * @param pVMR0 Pointer to the Ring-0 (Host Context) mapping of the VM structure.
319 * @param uOperation Operation to execute.
320 * @param pvArg Argument.
321 */
322SUPR3DECL(int) SUPCallVMMR0(PVMR0 pVMR0, unsigned uOperation, void *pvArg);
323
324/**
325 * Calls the HC R0 VMM entry point, in a safer but slower manner than SUPCallVMMR0.
326 * When entering using this call the R0 components can call into the host kernel
327 * (i.e. use the SUPR0 and RT APIs).
328 *
329 * See VMMR0Entry() for more details.
330 *
331 * @returns error code specific to uFunction.
332 * @param pVMR0 Pointer to the Ring-0 (Host Context) mapping of the VM structure.
333 * @param uOperation Operation to execute.
334 * @param pvArg Pointer to argument structure or if cbArg is 0 just an value.
335 * @param cbArg The size of the argument. This is used to copy whatever the argument
336 * points at into a kernel buffer to avoid problems like the user page
337 * being invalidated while we're executing the call.
338 */
339SUPR3DECL(int) SUPCallVMMR0Ex(PVMR0 pVMR0, unsigned uOperation, void *pvArg, unsigned cbArg);
340
341/**
342 * Queries the paging mode of the host OS.
343 *
344 * @returns The paging mode.
345 */
346SUPR3DECL(SUPPAGINGMODE) SUPGetPagingMode(void);
347
348/**
349 * Allocate zero-filled pages.
350 *
351 * Use this to allocate a number of pages rather than using RTMem*() and mess with
352 * alignment. The returned address is of course page aligned. Call SUPPageFree()
353 * to free the pages once done with them.
354 *
355 * @returns VBox status.
356 * @param cPages Number of pages to allocate.
357 * @param ppvPages Where to store the base pointer to the allocated pages.
358 */
359SUPR3DECL(int) SUPPageAlloc(size_t cPages, void **ppvPages);
360
361/**
362 * Frees pages allocated with SUPPageAlloc().
363 *
364 * @returns VBox status.
365 * @param pvPages Pointer returned by SUPPageAlloc().
366 * @param cPages Number of pages that was allocated.
367 */
368SUPR3DECL(int) SUPPageFree(void *pvPages, size_t cPages);
369
370/**
371 * Locks down the physical memory backing a virtual memory
372 * range in the current process.
373 *
374 * @returns VBox status code.
375 * @param pvStart Start of virtual memory range.
376 * Must be page aligned.
377 * @param cPages Number of pages.
378 * @param paPages Where to store the physical page addresses returned.
379 * On entry this will point to an array of with cbMemory >> PAGE_SHIFT entries.
380 */
381SUPR3DECL(int) SUPPageLock(void *pvStart, size_t cPages, PSUPPAGE paPages);
382
383/**
384 * Releases locked down pages.
385 *
386 * @returns VBox status code.
387 * @param pvStart Start of virtual memory range previously locked
388 * down by SUPPageLock().
389 */
390SUPR3DECL(int) SUPPageUnlock(void *pvStart);
391
392/**
393 * Allocated memory with page aligned memory with a contiguous and locked physical
394 * memory backing below 4GB.
395 *
396 * @returns Pointer to the allocated memory (virtual address).
397 * *pHCPhys is set to the physical address of the memory.
398 * The returned memory must be freed using SUPContFree().
399 * @returns NULL on failure.
400 * @param cPages Number of pages to allocate.
401 * @param pHCPhys Where to store the physical address of the memory block.
402 */
403SUPR3DECL(void *) SUPContAlloc(size_t cPages, PRTHCPHYS pHCPhys);
404
405/**
406 * Allocated memory with page aligned memory with a contiguous and locked physical
407 * memory backing below 4GB.
408 *
409 * @returns Pointer to the allocated memory (virtual address).
410 * *pHCPhys is set to the physical address of the memory.
411 * If ppvR0 isn't NULL, *ppvR0 is set to the ring-0 mapping.
412 * The returned memory must be freed using SUPContFree().
413 * @returns NULL on failure.
414 * @param cPages Number of pages to allocate.
415 * @param pR0Ptr Where to store the ring-0 mapping of the allocation. (optional)
416 * @param pHCPhys Where to store the physical address of the memory block.
417 *
418 * @remark This 2nd version of this API exists because we're not able to map the
419 * ring-3 mapping executable on WIN64. This is a serious problem in regard to
420 * the world switchers.
421 */
422SUPR3DECL(void *) SUPContAlloc2(size_t cPages, PRTR0PTR pR0Ptr, PRTHCPHYS pHCPhys);
423
424/**
425 * Frees memory allocated with SUPContAlloc().
426 *
427 * @returns VBox status code.
428 * @param pv Pointer to the memory block which should be freed.
429 * @param cPages Number of pages to be freed.
430 */
431SUPR3DECL(int) SUPContFree(void *pv, size_t cPages);
432
433/**
434 * Allocated non contiguous physical memory below 4GB.
435 *
436 * @returns VBox status code.
437 * @returns NULL on failure.
438 * @param cPages Number of pages to allocate.
439 * @param ppvPages Where to store the pointer to the allocated memory.
440 * The pointer stored here on success must be passed to SUPLowFree when
441 * the memory should be released.
442 * @param ppvPagesR0 Where to store the ring-0 pointer to the allocated memory. optional.
443 * @param paPages Where to store the physical addresses of the individual pages.
444 */
445SUPR3DECL(int) SUPLowAlloc(size_t cPages, void **ppvPages, PRTR0PTR ppvPagesR0, PSUPPAGE paPages);
446
447/**
448 * Frees memory allocated with SUPLowAlloc().
449 *
450 * @returns VBox status code.
451 * @param pv Pointer to the memory block which should be freed.
452 * @param cPages Number of pages that was allocated.
453 */
454SUPR3DECL(int) SUPLowFree(void *pv, size_t cPages);
455
456/**
457 * Load a module into R0 HC.
458 *
459 * @returns VBox status code.
460 * @param pszFilename The path to the image file.
461 * @param pszModule The module name. Max 32 bytes.
462 */
463SUPR3DECL(int) SUPLoadModule(const char *pszFilename, const char *pszModule, void **ppvImageBase);
464
465/**
466 * Frees a R0 HC module.
467 *
468 * @returns VBox status code.
469 * @param pszModule The module to free.
470 * @remark This will not actually 'free' the module, there are of course usage counting.
471 */
472SUPR3DECL(int) SUPFreeModule(void *pvImageBase);
473
474/**
475 * Get the address of a symbol in a ring-0 module.
476 *
477 * @returns VBox status code.
478 * @param pszModule The module name.
479 * @param pszSymbol Symbol name. If it's value is less than 64k it's treated like a
480 * ordinal value rather than a string pointer.
481 * @param ppvValue Where to store the symbol value.
482 */
483SUPR3DECL(int) SUPGetSymbolR0(void *pvImageBase, const char *pszSymbol, void **ppvValue);
484
485/**
486 * Load R0 HC VMM code.
487 *
488 * @returns VBox status code.
489 * @deprecated Use SUPLoadModule(pszFilename, "VMMR0.r0", &pvImageBase)
490 */
491SUPR3DECL(int) SUPLoadVMM(const char *pszFilename);
492
493/**
494 * Unloads R0 HC VMM code.
495 *
496 * @returns VBox status code.
497 * @deprecated Use SUPFreeModule().
498 */
499SUPR3DECL(int) SUPUnloadVMM(void);
500
501/**
502 * Get the physical address of the GIP.
503 *
504 * @returns VBox status code.
505 * @param pHCPhys Where to store the physical address of the GIP.
506 */
507SUPR3DECL(int) SUPGipGetPhys(PRTHCPHYS pHCPhys);
508
509/** @} */
510#endif /* IN_RING3 */
511
512
513#ifdef IN_RING0
514/** @defgroup grp_sup_r0 SUP Host Context Ring 0 API
515 * @ingroup grp_sup
516 * @{
517 */
518
519/**
520 * Security objectype.
521 */
522typedef enum SUPDRVOBJTYPE
523{
524 /** The usual invalid object. */
525 SUPDRVOBJTYPE_INVALID = 0,
526 /** Internal network. */
527 SUPDRVOBJTYPE_INTERNAL_NETWORK,
528 /** Internal network interface. */
529 SUPDRVOBJTYPE_INTERNAL_NETWORK_INTERFACE,
530 /** The first invalid object type in this end. */
531 SUPDRVOBJTYPE_END,
532 /** The usual 32-bit type size hack. */
533 SUPDRVOBJTYPE_32_BIT_HACK = 0x7ffffff
534} SUPDRVOBJTYPE;
535
536/**
537 * Object destructor callback.
538 * This is called for reference counted objectes when the count reaches 0.
539 *
540 * @param pvObj The object pointer.
541 * @param pvUser1 The first user argument.
542 * @param pvUser2 The second user argument.
543 */
544typedef DECLCALLBACK(void) FNSUPDRVDESTRUCTOR(void *pvObj, void *pvUser1, void *pvUser2);
545/** Pointer to a FNSUPDRVDESTRUCTOR(). */
546typedef FNSUPDRVDESTRUCTOR *PFNSUPDRVDESTRUCTOR;
547
548SUPR0DECL(void *) SUPR0ObjRegister(PSUPDRVSESSION pSession, SUPDRVOBJTYPE enmType, PFNSUPDRVDESTRUCTOR pfnDestructor, void *pvUser1, void *pvUser2);
549SUPR0DECL(int) SUPR0ObjAddRef(void *pvObj, PSUPDRVSESSION pSession);
550SUPR0DECL(int) SUPR0ObjRelease(void *pvObj, PSUPDRVSESSION pSession);
551SUPR0DECL(int) SUPR0ObjVerifyAccess(void *pvObj, PSUPDRVSESSION pSession, const char *pszObjName);
552
553SUPR0DECL(int) SUPR0LockMem(PSUPDRVSESSION pSession, RTR3PTR pvR3, uint32_t cPages, PSUPPAGE paPages);
554SUPR0DECL(int) SUPR0UnlockMem(PSUPDRVSESSION pSession, RTR3PTR pvR3);
555SUPR0DECL(int) SUPR0ContAlloc(PSUPDRVSESSION pSession, uint32_t cPages, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PRTHCPHYS pHCPhys);
556SUPR0DECL(int) SUPR0ContFree(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr);
557SUPR0DECL(int) SUPR0LowAlloc(PSUPDRVSESSION pSession, uint32_t cPages, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PSUPPAGE paPages);
558SUPR0DECL(int) SUPR0LowFree(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr);
559SUPR0DECL(int) SUPR0MemAlloc(PSUPDRVSESSION pSession, uint32_t cb, PRTR0PTR ppvR0, PRTR3PTR ppvR3);
560SUPR0DECL(int) SUPR0MemGetPhys(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr, PSUPPAGE paPages);
561SUPR0DECL(int) SUPR0MemFree(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr);
562SUPR0DECL(int) SUPR0GipMap(PSUPDRVSESSION pSession, PRTR3PTR ppGipR3, PRTHCPHYS pHCPhysGid);
563SUPR0DECL(int) SUPR0GipUnmap(PSUPDRVSESSION pSession);
564SUPR0DECL(int) SUPR0Printf(const char *pszFormat, ...);
565
566/** @} */
567#endif
568
569/** @} */
570
571__END_DECLS
572
573#endif
574
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