VirtualBox

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

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

Initial GIP change. Missing detection of SMP systems with TSC drift.

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