VirtualBox

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

Last change on this file since 3234 was 2981, checked in by vboxsync, 18 years ago

InnoTek -> innotek: all the headers and comments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.0 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 * If you received this file as part of a commercial VirtualBox
17 * distribution, then only the terms of your commercial VirtualBox
18 * license agreement apply instead of the previous paragraph.
19 */
20
21#ifndef __VBox_sup_h__
22#define __VBox_sup_h__
23
24#include <VBox/cdefs.h>
25#include <VBox/types.h>
26#include <iprt/assert.h>
27#include <iprt/asm.h>
28
29__BEGIN_DECLS
30
31/** @defgroup grp_sup The Support Library API
32 * @{
33 */
34
35/**
36 * Physical page descriptor.
37 */
38#pragma pack(4) /* space is more important. */
39typedef struct SUPPAGE
40{
41 /** Physical memory address. */
42 RTHCPHYS Phys;
43 /** Reserved entry for internal use by the caller. */
44 RTHCUINTPTR uReserved;
45} SUPPAGE;
46#pragma pack()
47/** Pointer to a page descriptor. */
48typedef SUPPAGE *PSUPPAGE;
49/** Pointer to a const page descriptor. */
50typedef const SUPPAGE *PCSUPPAGE;
51
52/**
53 * The paging mode.
54 */
55typedef enum SUPPAGINGMODE
56{
57 /** The usual invalid entry.
58 * This is returned by SUPGetPagingMode() */
59 SUPPAGINGMODE_INVALID = 0,
60 /** Normal 32-bit paging, no global pages */
61 SUPPAGINGMODE_32_BIT,
62 /** Normal 32-bit paging with global pages. */
63 SUPPAGINGMODE_32_BIT_GLOBAL,
64 /** PAE mode, no global pages, no NX. */
65 SUPPAGINGMODE_PAE,
66 /** PAE mode with global pages. */
67 SUPPAGINGMODE_PAE_GLOBAL,
68 /** PAE mode with NX, no global pages. */
69 SUPPAGINGMODE_PAE_NX,
70 /** PAE mode with global pages and NX. */
71 SUPPAGINGMODE_PAE_GLOBAL_NX,
72 /** AMD64 mode, no global pages. */
73 SUPPAGINGMODE_AMD64,
74 /** AMD64 mode with global pages, no NX. */
75 SUPPAGINGMODE_AMD64_GLOBAL,
76 /** AMD64 mode with NX, no global pages. */
77 SUPPAGINGMODE_AMD64_NX,
78 /** AMD64 mode with global pages and NX. */
79 SUPPAGINGMODE_AMD64_GLOBAL_NX
80} SUPPAGINGMODE;
81
82
83#pragma pack(1) /* paranoia */
84
85/**
86 * Per CPU data.
87 * This is only used when
88 */
89typedef struct SUPGIPCPU
90{
91 /** Update transaction number.
92 * This number is incremented at the start and end of each update. It follows
93 * thusly that odd numbers indicates update in progress, while even numbers
94 * indicate stable data. Use this to make sure that the data items you fetch
95 * are consistent. */
96 volatile uint32_t u32TransactionId;
97 /** The interval in TSC ticks between two NanoTS updates.
98 * This is the average interval over the last 2, 4 or 8 updates + a little slack.
99 * The slack makes the time go a tiny tiny bit slower and extends the interval enough
100 * to avoid ending up with too many 1ns increments. */
101 volatile uint32_t u32UpdateIntervalTSC;
102 /** Current nanosecond timestamp. */
103 volatile uint64_t u64NanoTS;
104 /** The TSC at the time of u64NanoTS. */
105 volatile uint64_t u64TSC;
106 /** Current CPU Frequency. */
107 volatile uint64_t u64CpuHz;
108 /** Number of errors during updating.
109 * Typical errors are under/overflows. */
110 volatile uint32_t cErrors;
111 /** Index of the head item in au32TSCHistory. */
112 volatile uint32_t iTSCHistoryHead;
113 /** Array of recent TSC interval deltas.
114 * The most recent item is at index iTSCHistoryHead.
115 * This history is used to calculate u32UpdateIntervalTSC.
116 */
117 volatile uint32_t au32TSCHistory[8];
118 /** Reserved for future per processor data. */
119 volatile uint32_t au32Reserved[6];
120} SUPGIPCPU;
121AssertCompileSize(SUPGIPCPU, 96);
122/*AssertCompileMemberAlignment(SUPGIPCPU, u64TSC, 8); -fixme */
123
124/** Pointer to per cpu data. */
125typedef SUPGIPCPU *PSUPGIPCPU;
126/** Pointer to const per cpu data. */
127typedef const SUPGIPCPU *PCSUPGIPCPU;
128
129/**
130 * Global Information Page.
131 *
132 * This page contains useful information and can be mapped into any
133 * process or VM. It can be accessed thru the g_pSUPGlobalInfoPage
134 * pointer when a session is open.
135 */
136typedef struct SUPGLOBALINFOPAGE
137{
138 /** Magic (SUPGLOBALINFOPAGE_MAGIC). */
139 uint32_t u32Magic;
140 /** The GIP version. */
141 uint32_t u32Version;
142
143 /** The GIP update mode, see SUPGIPMODE. */
144 uint32_t u32Mode;
145 /** Reserved / padding. */
146 uint32_t u32Padding0;
147 /** The update frequency of the of the NanoTS. */
148 volatile uint32_t u32UpdateHz;
149 /** The update interval in nanoseconds. (10^9 / u32UpdateHz) */
150 volatile uint32_t u32UpdateIntervalNS;
151 /** The timestamp of the last time we update the update frequency. */
152 volatile uint64_t u64NanoTSLastUpdateHz;
153
154 /** Padding / reserved space for future data. */
155 uint32_t au32Padding1[56];
156
157 /** Array of per-cpu data.
158 * If u32Mode == SUPGIPMODE_SYNC_TSC then only the first entry is used.
159 * If u32Mode == SUPGIPMODE_ASYNC_TSC then the CPU ACPI ID is used as an
160 * index into the array. */
161 SUPGIPCPU aCPUs[32];
162} SUPGLOBALINFOPAGE;
163AssertCompile(sizeof(SUPGLOBALINFOPAGE) <= 0x1000);
164/* AssertCompileMemberAlignment(SUPGLOBALINFOPAGE, aCPU, 32); - fixme */
165
166/** Pointer to the global info page. */
167typedef SUPGLOBALINFOPAGE *PSUPGLOBALINFOPAGE;
168/** Const pointer to the global info page. */
169typedef const SUPGLOBALINFOPAGE *PCSUPGLOBALINFOPAGE;
170
171#pragma pack() /* end of paranoia */
172
173/** The value of the SUPGLOBALINFOPAGE::u32Magic field. (Soryo Fuyumi) */
174#define SUPGLOBALINFOPAGE_MAGIC 0x19590106
175/** The GIP version.
176 * Upper 16 bits is the major version. Major version is only changed with
177 * incompatible changes in the GIP. */
178#define SUPGLOBALINFOPAGE_VERSION 0x00020000
179
180/**
181 * SUPGLOBALINFOPAGE::u32Mode values.
182 */
183typedef enum SUPGIPMODE
184{
185 /** The usual invalid null entry. */
186 SUPGIPMODE_INVALID = 0,
187 /** The TSC of the cores and cpus in the system is in sync. */
188 SUPGIPMODE_SYNC_TSC,
189 /** Each core has it's own TSC. */
190 SUPGIPMODE_ASYNC_TSC,
191 /** The usual 32-bit hack. */
192 SUPGIPMODE_32BIT_HACK = 0x7fffffff
193} SUPGIPMODE;
194
195/** Pointer to the Global Information Page.
196 *
197 * This pointer is valid as long as SUPLib has a open session. Anyone using
198 * the page must treat this pointer as higly volatile and not trust it beyond
199 * one transaction.
200 */
201#if defined(IN_SUP_R0) || defined(IN_SUP_R3) || defined(IN_SUP_GC)
202extern DECLEXPORT(PCSUPGLOBALINFOPAGE) g_pSUPGlobalInfoPage;
203#elif defined(IN_RING0)
204extern DECLIMPORT(const SUPGLOBALINFOPAGE) g_SUPGlobalInfoPage;
205# if defined(__GNUC__) && !defined(__DARWIN__) && defined(__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(PCSUPGLOBALINFOPAGE) SUPGetGIP(void)
209{
210 PCSUPGLOBALINFOPAGE 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(PCSUPGLOBALINFOPAGE) 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(PCSUPGLOBALINFOPAGE 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
574#endif
575
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