1 | /* $Revision: 4833 $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Support Driver - Internal header.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef __SUPDRV_h__
|
---|
19 | #define __SUPDRV_h__
|
---|
20 |
|
---|
21 |
|
---|
22 | /*******************************************************************************
|
---|
23 | * Header Files *
|
---|
24 | *******************************************************************************/
|
---|
25 | #include <VBox/cdefs.h>
|
---|
26 | #include <VBox/types.h>
|
---|
27 | #include <iprt/assert.h>
|
---|
28 | #include <iprt/asm.h>
|
---|
29 | #include <VBox/sup.h>
|
---|
30 | #ifdef USE_NEW_OS_INTERFACE
|
---|
31 | # define USE_NEW_OS_INTERFACE_FOR_MM
|
---|
32 | # define USE_NEW_OS_INTERFACE_FOR_GIP
|
---|
33 | # undef USE_NEW_OS_INTERFACE_FOR_LOW
|
---|
34 | #endif
|
---|
35 | #if defined(USE_NEW_OS_INTERFACE) || defined(USE_NEW_OS_INTERFACE_FOR_LOW) || defined(USE_NEW_OS_INTERFACE_FOR_MM) || defined(USE_NEW_OS_INTERFACE_FOR_GIP)
|
---|
36 | # include <iprt/memobj.h>
|
---|
37 | # include <iprt/time.h>
|
---|
38 | # include <iprt/timer.h>
|
---|
39 | # include <iprt/string.h>
|
---|
40 | # include <iprt/err.h>
|
---|
41 | #endif
|
---|
42 |
|
---|
43 |
|
---|
44 | #if defined(RT_OS_WINDOWS)
|
---|
45 | __BEGIN_DECLS
|
---|
46 | # if (_MSC_VER >= 1400) && !defined(VBOX_WITH_PATCHED_DDK)
|
---|
47 | # define _InterlockedExchange _InterlockedExchange_StupidDDKVsCompilerCrap
|
---|
48 | # define _InterlockedExchangeAdd _InterlockedExchangeAdd_StupidDDKVsCompilerCrap
|
---|
49 | # define _InterlockedCompareExchange _InterlockedCompareExchange_StupidDDKVsCompilerCrap
|
---|
50 | # define _InterlockedAddLargeStatistic _InterlockedAddLargeStatistic_StupidDDKVsCompilerCrap
|
---|
51 | # include <ntddk.h>
|
---|
52 | # undef _InterlockedExchange
|
---|
53 | # undef _InterlockedExchangeAdd
|
---|
54 | # undef _InterlockedCompareExchange
|
---|
55 | # undef _InterlockedAddLargeStatistic
|
---|
56 | # else
|
---|
57 | # include <ntddk.h>
|
---|
58 | # endif
|
---|
59 | # include <memory.h>
|
---|
60 | # define memcmp(a,b,c) mymemcmp(a,b,c)
|
---|
61 | int VBOXCALL mymemcmp(const void *, const void *, size_t);
|
---|
62 | __END_DECLS
|
---|
63 |
|
---|
64 | #elif defined(RT_OS_LINUX)
|
---|
65 | # include <linux/autoconf.h>
|
---|
66 | # include <linux/version.h>
|
---|
67 | # if defined(CONFIG_MODVERSIONS) && !defined(MODVERSIONS)
|
---|
68 | # define MODVERSIONS
|
---|
69 | # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 71)
|
---|
70 | # include <linux/modversions.h>
|
---|
71 | # endif
|
---|
72 | # endif
|
---|
73 | # if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 0)
|
---|
74 | # undef ALIGN
|
---|
75 | # endif
|
---|
76 | # ifndef KBUILD_STR
|
---|
77 | # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)
|
---|
78 | # define KBUILD_STR(s) s
|
---|
79 | # else
|
---|
80 | # define KBUILD_STR(s) #s
|
---|
81 | # endif
|
---|
82 | # endif
|
---|
83 | # include <linux/string.h>
|
---|
84 | # include <linux/spinlock.h>
|
---|
85 | # include <linux/slab.h>
|
---|
86 | # include <asm/semaphore.h>
|
---|
87 | # include <linux/timer.h>
|
---|
88 |
|
---|
89 | #elif defined(RT_OS_DARWIN)
|
---|
90 | # include <libkern/libkern.h>
|
---|
91 | # include <iprt/string.h>
|
---|
92 |
|
---|
93 | #elif defined(RT_OS_OS2)
|
---|
94 |
|
---|
95 | #elif defined(RT_OS_FREEBSD)
|
---|
96 | # include <sys/libkern.h>
|
---|
97 | # include <iprt/string.h>
|
---|
98 |
|
---|
99 | #elif defined(RT_OS_SOLARIS)
|
---|
100 | # include <sys/cmn_err.h>
|
---|
101 | # include <iprt/string.h>
|
---|
102 |
|
---|
103 | #else
|
---|
104 | # error "unsupported OS."
|
---|
105 | #endif
|
---|
106 |
|
---|
107 | #include "SUPDRVIOC.h"
|
---|
108 |
|
---|
109 |
|
---|
110 |
|
---|
111 | /*******************************************************************************
|
---|
112 | * Defined Constants And Macros *
|
---|
113 | *******************************************************************************/
|
---|
114 | /*
|
---|
115 | * Hardcoded cookies.
|
---|
116 | */
|
---|
117 | #define BIRD 0x64726962 /* 'bird' */
|
---|
118 | #define BIRD_INV 0x62697264 /* 'drib' */
|
---|
119 |
|
---|
120 |
|
---|
121 | /*
|
---|
122 | * Win32
|
---|
123 | */
|
---|
124 | #if defined(RT_OS_WINDOWS)
|
---|
125 |
|
---|
126 | /* debug printf */
|
---|
127 | # define OSDBGPRINT(a) DbgPrint a
|
---|
128 |
|
---|
129 | /** Maximum number of bytes we try to lock down in one go.
|
---|
130 | * This is supposed to have a limit right below 256MB, but this appears
|
---|
131 | * to actually be much lower. The values here have been determined experimentally.
|
---|
132 | */
|
---|
133 | #ifdef RT_ARCH_X86
|
---|
134 | # define MAX_LOCK_MEM_SIZE (32*1024*1024) /* 32mb */
|
---|
135 | #endif
|
---|
136 | #ifdef RT_ARCH_AMD64
|
---|
137 | # define MAX_LOCK_MEM_SIZE (24*1024*1024) /* 24mb */
|
---|
138 | #endif
|
---|
139 |
|
---|
140 |
|
---|
141 | /*
|
---|
142 | * Linux
|
---|
143 | */
|
---|
144 | #elif defined(RT_OS_LINUX)
|
---|
145 |
|
---|
146 | /* check kernel version */
|
---|
147 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 0)
|
---|
148 | # error Unsupported kernel version!
|
---|
149 | #endif
|
---|
150 |
|
---|
151 | __BEGIN_DECLS
|
---|
152 | int linux_dprintf(const char *format, ...);
|
---|
153 | __END_DECLS
|
---|
154 |
|
---|
155 | /* debug printf */
|
---|
156 | # define OSDBGPRINT(a) printk a
|
---|
157 |
|
---|
158 |
|
---|
159 | /*
|
---|
160 | * Darwin
|
---|
161 | */
|
---|
162 | #elif defined(RT_OS_DARWIN)
|
---|
163 |
|
---|
164 | /* debug printf */
|
---|
165 | # define OSDBGPRINT(a) printf a
|
---|
166 |
|
---|
167 |
|
---|
168 | /*
|
---|
169 | * OS/2
|
---|
170 | */
|
---|
171 | #elif defined(RT_OS_OS2)
|
---|
172 |
|
---|
173 | /* No log API in OS/2 only COM port. */
|
---|
174 | # define OSDBGPRINT(a) SUPR0Printf a
|
---|
175 |
|
---|
176 |
|
---|
177 | /*
|
---|
178 | * FreeBSD
|
---|
179 | */
|
---|
180 | #elif defined(RT_OS_FREEBSD)
|
---|
181 |
|
---|
182 | /* No log API in OS/2 only COM port. */
|
---|
183 | # define OSDBGPRINT(a) printf a
|
---|
184 |
|
---|
185 |
|
---|
186 | /*
|
---|
187 | * Solaris
|
---|
188 | */
|
---|
189 | #elif defined(RT_OS_SOLARIS)
|
---|
190 | # define OSDBGPRINT(a) SUPR0Printf a
|
---|
191 |
|
---|
192 |
|
---|
193 | #else
|
---|
194 | /** @todo other os'es */
|
---|
195 | # error "OS interface defines is not done for this OS!"
|
---|
196 | #endif
|
---|
197 |
|
---|
198 |
|
---|
199 | /* dprintf */
|
---|
200 | #if (defined(DEBUG) && !defined(NO_LOGGING)) || defined(RT_OS_FREEBSD)
|
---|
201 | # ifdef LOG_TO_COM
|
---|
202 | # include <VBox/log.h>
|
---|
203 | # define dprintf(a) RTLogComPrintf a
|
---|
204 | # else
|
---|
205 | # define dprintf(a) OSDBGPRINT(a)
|
---|
206 | # endif
|
---|
207 | #else
|
---|
208 | # define dprintf(a) do {} while (0)
|
---|
209 | #endif
|
---|
210 |
|
---|
211 | /* dprintf2 - extended logging. */
|
---|
212 | #if defined(RT_OS_DARWIN) || defined(RT_OS_OS2) || defined(RT_OS_FREEBSD)
|
---|
213 | # define dprintf2 dprintf
|
---|
214 | #else
|
---|
215 | # define dprintf2(a) do { } while (0)
|
---|
216 | #endif
|
---|
217 |
|
---|
218 |
|
---|
219 | /*
|
---|
220 | * Error codes.
|
---|
221 | */
|
---|
222 | /** Invalid parameter. */
|
---|
223 | #define SUPDRV_ERR_GENERAL_FAILURE (-1)
|
---|
224 | /** Invalid parameter. */
|
---|
225 | #define SUPDRV_ERR_INVALID_PARAM (-2)
|
---|
226 | /** Invalid magic or cookie. */
|
---|
227 | #define SUPDRV_ERR_INVALID_MAGIC (-3)
|
---|
228 | /** Invalid loader handle. */
|
---|
229 | #define SUPDRV_ERR_INVALID_HANDLE (-4)
|
---|
230 | /** Failed to lock the address range. */
|
---|
231 | #define SUPDRV_ERR_LOCK_FAILED (-5)
|
---|
232 | /** Invalid memory pointer. */
|
---|
233 | #define SUPDRV_ERR_INVALID_POINTER (-6)
|
---|
234 | /** Failed to patch the IDT. */
|
---|
235 | #define SUPDRV_ERR_IDT_FAILED (-7)
|
---|
236 | /** Memory allocation failed. */
|
---|
237 | #define SUPDRV_ERR_NO_MEMORY (-8)
|
---|
238 | /** Already loaded. */
|
---|
239 | #define SUPDRV_ERR_ALREADY_LOADED (-9)
|
---|
240 | /** Permission denied. */
|
---|
241 | #define SUPDRV_ERR_PERMISSION_DENIED (-10)
|
---|
242 | /** Version mismatch. */
|
---|
243 | #define SUPDRV_ERR_VERSION_MISMATCH (-11)
|
---|
244 |
|
---|
245 |
|
---|
246 |
|
---|
247 | /*******************************************************************************
|
---|
248 | * Structures and Typedefs *
|
---|
249 | *******************************************************************************/
|
---|
250 | /** Pointer to the device extension. */
|
---|
251 | typedef struct SUPDRVDEVEXT *PSUPDRVDEVEXT;
|
---|
252 |
|
---|
253 | #ifdef VBOX_WITH_IDT_PATCHING
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * An IDT Entry.
|
---|
257 | */
|
---|
258 | typedef struct SUPDRVIDTE
|
---|
259 | {
|
---|
260 | /** Low offset word. */
|
---|
261 | uint32_t u16OffsetLow : 16;
|
---|
262 | /** Segment Selector. */
|
---|
263 | uint32_t u16SegSel : 16;
|
---|
264 | #ifdef RT_ARCH_AMD64
|
---|
265 | /** Interrupt Stack Table index. */
|
---|
266 | uint32_t u3IST : 3;
|
---|
267 | /** Reserved, ignored. */
|
---|
268 | uint32_t u5Reserved : 5;
|
---|
269 | #else
|
---|
270 | /** Reserved. */
|
---|
271 | uint32_t u5Reserved : 5;
|
---|
272 | /** IDT Type part one (not used for task gate). */
|
---|
273 | uint32_t u3Type1 : 3;
|
---|
274 | #endif
|
---|
275 | /** IDT Type part two. */
|
---|
276 | uint32_t u5Type2 : 5;
|
---|
277 | /** Descriptor Privilege level. */
|
---|
278 | uint32_t u2DPL : 2;
|
---|
279 | /** Present flag. */
|
---|
280 | uint32_t u1Present : 1;
|
---|
281 | /** High offset word. */
|
---|
282 | uint32_t u16OffsetHigh : 16;
|
---|
283 | #ifdef RT_ARCH_AMD64
|
---|
284 | /** The upper top part of the address. */
|
---|
285 | uint32_t u32OffsetTop;
|
---|
286 | /** Reserved dword for qword (aligning the struct), ignored. */
|
---|
287 | uint32_t u32Reserved;
|
---|
288 | #endif
|
---|
289 | } SUPDRVIDTE, *PSUPDRVIDTE;
|
---|
290 |
|
---|
291 | /** The u5Type2 value for an interrupt gate. */
|
---|
292 | #define SUPDRV_IDTE_TYPE2_INTERRUPT_GATE 0x0e
|
---|
293 |
|
---|
294 |
|
---|
295 | /**
|
---|
296 | * Patch code.
|
---|
297 | */
|
---|
298 | typedef struct SUPDRVPATCH
|
---|
299 | {
|
---|
300 | #define SUPDRV_PATCH_CODE_SIZE 0x50
|
---|
301 | /** Patch code. */
|
---|
302 | uint8_t auCode[SUPDRV_PATCH_CODE_SIZE];
|
---|
303 | /** Changed IDT entry (for parnoid UnpatchIdt()). */
|
---|
304 | SUPDRVIDTE ChangedIdt;
|
---|
305 | /** Saved IDT entry. */
|
---|
306 | SUPDRVIDTE SavedIdt;
|
---|
307 | /** Pointer to the IDT.
|
---|
308 | * We ASSUME the IDT is not re(al)located after bootup and use this as key
|
---|
309 | * for the patches rather than processor number. This prevents some
|
---|
310 | * stupid nesting stuff from happening in case of processors sharing the
|
---|
311 | * IDT.
|
---|
312 | * We're fucked if the processors have different physical mapping for
|
---|
313 | * the(se) page(s), but we'll find that out soon enough in VBOX_STRICT mode.
|
---|
314 | */
|
---|
315 | void *pvIdt;
|
---|
316 | /** Pointer to the IDT entry. */
|
---|
317 | SUPDRVIDTE volatile *pIdtEntry;
|
---|
318 | /** Usage counter. */
|
---|
319 | uint32_t volatile cUsage;
|
---|
320 | /** The offset into auCode of the VMMR0Entry fixup. */
|
---|
321 | uint16_t offVMMR0EntryFixup;
|
---|
322 | /** The offset into auCode of the stub function. */
|
---|
323 | uint16_t offStub;
|
---|
324 | /** Pointer to the next patch. */
|
---|
325 | struct SUPDRVPATCH * volatile pNext;
|
---|
326 | } SUPDRVPATCH, *PSUPDRVPATCH;
|
---|
327 |
|
---|
328 | /**
|
---|
329 | * Usage record for a patch.
|
---|
330 | */
|
---|
331 | typedef struct SUPDRVPATCHUSAGE
|
---|
332 | {
|
---|
333 | /** Next in the chain. */
|
---|
334 | struct SUPDRVPATCHUSAGE * volatile pNext;
|
---|
335 | /** The patch this usage applies to. */
|
---|
336 | PSUPDRVPATCH pPatch;
|
---|
337 | /** Usage count. */
|
---|
338 | uint32_t volatile cUsage;
|
---|
339 | } SUPDRVPATCHUSAGE, *PSUPDRVPATCHUSAGE;
|
---|
340 |
|
---|
341 | #endif /* VBOX_WITH_IDT_PATCHING */
|
---|
342 |
|
---|
343 |
|
---|
344 | /**
|
---|
345 | * Memory reference types.
|
---|
346 | */
|
---|
347 | typedef enum
|
---|
348 | {
|
---|
349 | /** Unused entry */
|
---|
350 | MEMREF_TYPE_UNUSED = 0,
|
---|
351 | /** Locked memory (r3 mapping only). */
|
---|
352 | MEMREF_TYPE_LOCKED,
|
---|
353 | /** Continous memory block (r3 and r0 mapping). */
|
---|
354 | MEMREF_TYPE_CONT,
|
---|
355 | /** Low memory block (r3 and r0 mapping). */
|
---|
356 | MEMREF_TYPE_LOW,
|
---|
357 | /** Memory block (r3 and r0 mapping). */
|
---|
358 | MEMREF_TYPE_MEM,
|
---|
359 | /** Locked memory (r3 mapping only) allocated by the support driver. */
|
---|
360 | MEMREF_TYPE_LOCKED_SUP,
|
---|
361 | /** Blow the type up to 32-bit and mark the end. */
|
---|
362 | MEMREG_TYPE_32BIT_HACK = 0x7fffffff
|
---|
363 | } SUPDRVMEMREFTYPE, *PSUPDRVMEMREFTYPE;
|
---|
364 |
|
---|
365 |
|
---|
366 | /**
|
---|
367 | * Structure used for tracking memory a session
|
---|
368 | * references in one way or another.
|
---|
369 | */
|
---|
370 | typedef struct SUPDRVMEMREF
|
---|
371 | {
|
---|
372 | #ifdef USE_NEW_OS_INTERFACE_FOR_MM
|
---|
373 | /** The memory object handle. */
|
---|
374 | RTR0MEMOBJ MemObj;
|
---|
375 | /** The ring-3 mapping memory object handle. */
|
---|
376 | RTR0MEMOBJ MapObjR3;
|
---|
377 | /** Type of memory. */
|
---|
378 | SUPDRVMEMREFTYPE eType;
|
---|
379 |
|
---|
380 | #else /* !USE_NEW_OS_INTERFACE_FOR_MM */
|
---|
381 | /** Pointer to the R0 mapping of the memory.
|
---|
382 | * Set to NULL if N/A. */
|
---|
383 | void *pvR0;
|
---|
384 | /** Pointer to the R3 mapping of the memory.
|
---|
385 | * Set to NULL if N/A. */
|
---|
386 | RTR3PTR pvR3;
|
---|
387 | /** Size of the locked memory. */
|
---|
388 | unsigned cb;
|
---|
389 | /** Type of memory. */
|
---|
390 | SUPDRVMEMREFTYPE eType;
|
---|
391 |
|
---|
392 | /** memory type specific information. */
|
---|
393 | union
|
---|
394 | {
|
---|
395 | struct
|
---|
396 | {
|
---|
397 | #if defined(RT_OS_WINDOWS)
|
---|
398 | /** Pointer to memory descriptor list (MDL). */
|
---|
399 | PMDL *papMdl;
|
---|
400 | unsigned cMdls;
|
---|
401 | #elif defined(RT_OS_LINUX)
|
---|
402 | struct page **papPages;
|
---|
403 | unsigned cPages;
|
---|
404 | #else
|
---|
405 | # error "Either no target was defined or we haven't ported the driver to the target yet."
|
---|
406 | #endif
|
---|
407 | } locked;
|
---|
408 | struct
|
---|
409 | {
|
---|
410 | #if defined(RT_OS_WINDOWS)
|
---|
411 | /** Pointer to memory descriptor list (MDL). */
|
---|
412 | PMDL pMdl;
|
---|
413 | #elif defined(RT_OS_LINUX)
|
---|
414 | struct page *paPages;
|
---|
415 | unsigned cPages;
|
---|
416 | #else
|
---|
417 | # error "Either no target was defined or we haven't ported the driver to the target yet."
|
---|
418 | #endif
|
---|
419 | } cont;
|
---|
420 | struct
|
---|
421 | {
|
---|
422 | #if defined(RT_OS_WINDOWS)
|
---|
423 | /** Pointer to memory descriptor list (MDL). */
|
---|
424 | PMDL pMdl;
|
---|
425 | #elif defined(RT_OS_LINUX)
|
---|
426 | /** Pointer to the array of page pointers. */
|
---|
427 | struct page **papPages;
|
---|
428 | /** Number of pages in papPages. */
|
---|
429 | unsigned cPages;
|
---|
430 | #else
|
---|
431 | # error "Either no target was defined or we haven't ported the driver to the target yet."
|
---|
432 | #endif
|
---|
433 | } mem;
|
---|
434 | #if defined(USE_NEW_OS_INTERFACE_FOR_LOW)
|
---|
435 | struct
|
---|
436 | {
|
---|
437 | /** The memory object handle. */
|
---|
438 | RTR0MEMOBJ MemObj;
|
---|
439 | /** The ring-3 mapping memory object handle. */
|
---|
440 | RTR0MEMOBJ MapObjR3;
|
---|
441 | } iprt;
|
---|
442 | #endif
|
---|
443 | } u;
|
---|
444 | #endif /* !USE_NEW_OS_INTERFACE_FOR_MM */
|
---|
445 | } SUPDRVMEMREF, *PSUPDRVMEMREF;
|
---|
446 |
|
---|
447 |
|
---|
448 | /**
|
---|
449 | * Bundle of locked memory ranges.
|
---|
450 | */
|
---|
451 | typedef struct SUPDRVBUNDLE
|
---|
452 | {
|
---|
453 | /** Pointer to the next bundle. */
|
---|
454 | struct SUPDRVBUNDLE * volatile pNext;
|
---|
455 | /** Referenced memory. */
|
---|
456 | SUPDRVMEMREF aMem[64];
|
---|
457 | /** Number of entries used. */
|
---|
458 | uint32_t volatile cUsed;
|
---|
459 | } SUPDRVBUNDLE, *PSUPDRVBUNDLE;
|
---|
460 |
|
---|
461 |
|
---|
462 | /**
|
---|
463 | * Loaded image.
|
---|
464 | */
|
---|
465 | typedef struct SUPDRVLDRIMAGE
|
---|
466 | {
|
---|
467 | /** Next in chain. */
|
---|
468 | struct SUPDRVLDRIMAGE * volatile pNext;
|
---|
469 | /** Pointer to the image. */
|
---|
470 | void *pvImage;
|
---|
471 | /** Pointer to the optional module initialization callback. */
|
---|
472 | PFNR0MODULEINIT pfnModuleInit;
|
---|
473 | /** Pointer to the optional module termination callback. */
|
---|
474 | PFNR0MODULETERM pfnModuleTerm;
|
---|
475 | /** Size of the image. */
|
---|
476 | uint32_t cbImage;
|
---|
477 | /** The offset of the symbol table. */
|
---|
478 | uint32_t offSymbols;
|
---|
479 | /** The number of entries in the symbol table. */
|
---|
480 | uint32_t cSymbols;
|
---|
481 | /** The offset of the string table. */
|
---|
482 | uint32_t offStrTab;
|
---|
483 | /** Size of the string table. */
|
---|
484 | uint32_t cbStrTab;
|
---|
485 | /** The ldr image state. (IOCtl code of last opration.) */
|
---|
486 | uint32_t uState;
|
---|
487 | /** Usage count. */
|
---|
488 | uint32_t volatile cUsage;
|
---|
489 | /** Image name. */
|
---|
490 | char szName[32];
|
---|
491 | } SUPDRVLDRIMAGE, *PSUPDRVLDRIMAGE;
|
---|
492 |
|
---|
493 |
|
---|
494 | /** Image usage record. */
|
---|
495 | typedef struct SUPDRVLDRUSAGE
|
---|
496 | {
|
---|
497 | /** Next in chain. */
|
---|
498 | struct SUPDRVLDRUSAGE * volatile pNext;
|
---|
499 | /** The image. */
|
---|
500 | PSUPDRVLDRIMAGE pImage;
|
---|
501 | /** Load count. */
|
---|
502 | uint32_t volatile cUsage;
|
---|
503 | } SUPDRVLDRUSAGE, *PSUPDRVLDRUSAGE;
|
---|
504 |
|
---|
505 |
|
---|
506 | /**
|
---|
507 | * Registered object.
|
---|
508 | * This takes care of reference counting and tracking data for access checks.
|
---|
509 | */
|
---|
510 | typedef struct SUPDRVOBJ
|
---|
511 | {
|
---|
512 | /** Magic value (SUPDRVOBJ_MAGIC). */
|
---|
513 | uint32_t u32Magic;
|
---|
514 | /** The object type. */
|
---|
515 | SUPDRVOBJTYPE enmType;
|
---|
516 | /** Pointer to the next in the global list. */
|
---|
517 | struct SUPDRVOBJ * volatile pNext;
|
---|
518 | /** Pointer to the object destructor. */
|
---|
519 | PFNSUPDRVDESTRUCTOR pfnDestructor;
|
---|
520 | /** User argument 1. */
|
---|
521 | void *pvUser1;
|
---|
522 | /** User argument 2. */
|
---|
523 | void *pvUser2;
|
---|
524 | /** The total sum of all per-session usage. */
|
---|
525 | uint32_t volatile cUsage;
|
---|
526 | /** The creator user id. */
|
---|
527 | RTUID CreatorUid;
|
---|
528 | /** The creator group id. */
|
---|
529 | RTGID CreatorGid;
|
---|
530 | /** The creator process id. */
|
---|
531 | RTPROCESS CreatorProcess;
|
---|
532 | } SUPDRVOBJ, *PSUPDRVOBJ;
|
---|
533 |
|
---|
534 | /** Magic number for SUPDRVOBJ::u32Magic. (Dame Agatha Mary Clarissa Christie). */
|
---|
535 | #define SUPDRVOBJ_MAGIC 0x18900915
|
---|
536 |
|
---|
537 | /**
|
---|
538 | * The per-session object usage record.
|
---|
539 | */
|
---|
540 | typedef struct SUPDRVUSAGE
|
---|
541 | {
|
---|
542 | /** Pointer to the next in the list. */
|
---|
543 | struct SUPDRVUSAGE * volatile pNext;
|
---|
544 | /** Pointer to the object we're recording usage for. */
|
---|
545 | PSUPDRVOBJ pObj;
|
---|
546 | /** The usage count. */
|
---|
547 | uint32_t volatile cUsage;
|
---|
548 | } SUPDRVUSAGE, *PSUPDRVUSAGE;
|
---|
549 |
|
---|
550 |
|
---|
551 | /**
|
---|
552 | * Per session data.
|
---|
553 | * This is mainly for memory tracking.
|
---|
554 | */
|
---|
555 | typedef struct SUPDRVSESSION
|
---|
556 | {
|
---|
557 | /** Pointer to the device extension. */
|
---|
558 | PSUPDRVDEVEXT pDevExt;
|
---|
559 | /** Session Cookie. */
|
---|
560 | uint32_t u32Cookie;
|
---|
561 |
|
---|
562 | /** Load usage records. (protected by SUPDRVDEVEXT::mtxLdr) */
|
---|
563 | PSUPDRVLDRUSAGE volatile pLdrUsage;
|
---|
564 | #ifdef VBOX_WITH_IDT_PATCHING
|
---|
565 | /** Patch usage records. (protected by SUPDRVDEVEXT::SpinLock) */
|
---|
566 | PSUPDRVPATCHUSAGE volatile pPatchUsage;
|
---|
567 | #endif
|
---|
568 | /** The VM associated with the session. */
|
---|
569 | PVM pVM;
|
---|
570 | /** List of generic usage records. (protected by SUPDRVDEVEXT::SpinLock) */
|
---|
571 | PSUPDRVUSAGE volatile pUsage;
|
---|
572 |
|
---|
573 | /** Spinlock protecting the bundles and the GIP members. */
|
---|
574 | RTSPINLOCK Spinlock;
|
---|
575 | #ifdef USE_NEW_OS_INTERFACE_FOR_GIP
|
---|
576 | /** The ring-3 mapping of the GIP (readonly). */
|
---|
577 | RTR0MEMOBJ GipMapObjR3;
|
---|
578 | #else
|
---|
579 | /** The read-only usermode mapping address of the GID.
|
---|
580 | * This is NULL if the GIP hasn't been mapped. */
|
---|
581 | PSUPGLOBALINFOPAGE pGip;
|
---|
582 | #endif
|
---|
583 | /** Set if the session is using the GIP. */
|
---|
584 | uint32_t fGipReferenced;
|
---|
585 | /** Bundle of locked memory objects. */
|
---|
586 | SUPDRVBUNDLE Bundle;
|
---|
587 |
|
---|
588 | /** The user id of the session. (Set by the OS part.) */
|
---|
589 | RTUID Uid;
|
---|
590 | /** The group id of the session. (Set by the OS part.) */
|
---|
591 | RTGID Gid;
|
---|
592 | /** The process (id) of the session. (Set by the OS part.) */
|
---|
593 | RTPROCESS Process;
|
---|
594 | /** Which process this session is associated with. */
|
---|
595 | RTR0PROCESS R0Process;
|
---|
596 | #if defined(RT_OS_OS2)
|
---|
597 | /** The system file number of this session. */
|
---|
598 | uint16_t sfn;
|
---|
599 | uint16_t Alignment; /**< Alignment */
|
---|
600 | #endif
|
---|
601 | #if defined(RT_OS_DARWIN) || defined(RT_OS_OS2) || defined(RT_OS_SOLARIS)
|
---|
602 | /** Pointer to the next session with the same hash. */
|
---|
603 | PSUPDRVSESSION pNextHash;
|
---|
604 | #endif
|
---|
605 | } SUPDRVSESSION;
|
---|
606 |
|
---|
607 |
|
---|
608 | /**
|
---|
609 | * Device extension.
|
---|
610 | */
|
---|
611 | typedef struct SUPDRVDEVEXT
|
---|
612 | {
|
---|
613 | /** Spinlock to serialize the initialization,
|
---|
614 | * usage counting and destruction of the IDT entry override. */
|
---|
615 | RTSPINLOCK Spinlock;
|
---|
616 |
|
---|
617 | #ifdef VBOX_WITH_IDT_PATCHING
|
---|
618 | /** List of patches. */
|
---|
619 | PSUPDRVPATCH volatile pIdtPatches;
|
---|
620 | /** List of patches Free. */
|
---|
621 | PSUPDRVPATCH volatile pIdtPatchesFree;
|
---|
622 | #endif
|
---|
623 |
|
---|
624 | /** List of registered objects. */
|
---|
625 | PSUPDRVOBJ volatile pObjs;
|
---|
626 | /** List of free object usage records. */
|
---|
627 | PSUPDRVUSAGE volatile pUsageFree;
|
---|
628 |
|
---|
629 | /** Global cookie. */
|
---|
630 | uint32_t u32Cookie;
|
---|
631 |
|
---|
632 | /** The IDT entry number.
|
---|
633 | * Only valid if pIdtPatches is set. */
|
---|
634 | uint8_t volatile u8Idt;
|
---|
635 |
|
---|
636 | /** Loader mutex.
|
---|
637 | * This protects pvVMMR0, pvVMMR0Entry, pImages and SUPDRVSESSION::pLdrUsage. */
|
---|
638 | RTSEMFASTMUTEX mtxLdr;
|
---|
639 |
|
---|
640 | /** VMM Module 'handle'.
|
---|
641 | * 0 if the code VMM isn't loaded and Idt are nops. */
|
---|
642 | void * volatile pvVMMR0;
|
---|
643 | /** VMMR0EntryInt() pointer. */
|
---|
644 | DECLR0CALLBACKMEMBER(int, pfnVMMR0EntryInt, (PVM pVM, unsigned uOperation, void *pvArg));
|
---|
645 | /** VMMR0EntryFast() pointer. */
|
---|
646 | DECLR0CALLBACKMEMBER(int, pfnVMMR0EntryFast, (PVM pVM, unsigned uOperation));
|
---|
647 | /** VMMR0EntryEx() pointer. */
|
---|
648 | DECLR0CALLBACKMEMBER(int, pfnVMMR0EntryEx, (PVM pVM, unsigned uOperation, PSUPVMMR0REQHDR pReq, uint64_t u64Arg));
|
---|
649 |
|
---|
650 | /** Linked list of loaded code. */
|
---|
651 | PSUPDRVLDRIMAGE volatile pLdrImages;
|
---|
652 |
|
---|
653 | /** GIP mutex.
|
---|
654 | * Any changes to any of the GIP members requires ownership of this mutex,
|
---|
655 | * except on driver init and termination. */
|
---|
656 | RTSEMFASTMUTEX mtxGip;
|
---|
657 | /** Pointer to the Global Info Page (GIP). */
|
---|
658 | PSUPGLOBALINFOPAGE pGip;
|
---|
659 | /** The physical address of the GIP. */
|
---|
660 | RTHCPHYS HCPhysGip;
|
---|
661 | /** Number of processes using the GIP.
|
---|
662 | * (The updates are suspend while cGipUsers is 0.)*/
|
---|
663 | uint32_t volatile cGipUsers;
|
---|
664 | #ifdef USE_NEW_OS_INTERFACE_FOR_GIP
|
---|
665 | /** The ring-0 memory object handle for the GIP page. */
|
---|
666 | RTR0MEMOBJ GipMemObj;
|
---|
667 | /** The GIP timer handle. */
|
---|
668 | PRTTIMER pGipTimer;
|
---|
669 | /** If non-zero we've successfully called RTTimerRequestSystemGranularity(). */
|
---|
670 | uint32_t u32SystemTimerGranularityGrant;
|
---|
671 | #endif
|
---|
672 | #ifdef RT_OS_WINDOWS
|
---|
673 | /** The GIP timer object. */
|
---|
674 | KTIMER GipTimer;
|
---|
675 | /** The GIP DPC object associated with GipTimer. */
|
---|
676 | KDPC GipDpc;
|
---|
677 | /** The GIP DPC objects for updating per-cpu data. */
|
---|
678 | KDPC aGipCpuDpcs[32];
|
---|
679 | /** Pointer to the MDL for the pGip page. */
|
---|
680 | PMDL pGipMdl;
|
---|
681 | /** GIP timer interval (ms). */
|
---|
682 | ULONG ulGipTimerInterval;
|
---|
683 | #endif
|
---|
684 | #ifdef RT_OS_LINUX
|
---|
685 | /** The last jiffies. */
|
---|
686 | unsigned long ulLastJiffies;
|
---|
687 | /** The last mono time stamp. */
|
---|
688 | uint64_t volatile u64LastMonotime;
|
---|
689 | /** Set when GIP is suspended to prevent the timers from re-registering themselves). */
|
---|
690 | uint8_t volatile fGIPSuspended;
|
---|
691 | # ifdef CONFIG_SMP
|
---|
692 | /** Array of per CPU data for SUPGIPMODE_ASYNC_TSC. */
|
---|
693 | struct LINUXCPU
|
---|
694 | {
|
---|
695 | /** The last mono time stamp. */
|
---|
696 | uint64_t volatile u64LastMonotime;
|
---|
697 | /** The last jiffies. */
|
---|
698 | unsigned long ulLastJiffies;
|
---|
699 | /** The Linux Process ID. */
|
---|
700 | unsigned iSmpProcessorId;
|
---|
701 | /** The per cpu timer. */
|
---|
702 | struct timer_list Timer;
|
---|
703 | } aCPUs[256];
|
---|
704 | # endif
|
---|
705 | #endif
|
---|
706 | } SUPDRVDEVEXT;
|
---|
707 |
|
---|
708 |
|
---|
709 | __BEGIN_DECLS
|
---|
710 |
|
---|
711 | /*******************************************************************************
|
---|
712 | * OS Specific Functions *
|
---|
713 | *******************************************************************************/
|
---|
714 | void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession);
|
---|
715 | bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc);
|
---|
716 | #ifndef USE_NEW_OS_INTERFACE_FOR_MM
|
---|
717 | int VBOXCALL supdrvOSLockMemOne(PSUPDRVMEMREF pMem, PSUPPAGE paPages);
|
---|
718 | void VBOXCALL supdrvOSUnlockMemOne(PSUPDRVMEMREF pMem);
|
---|
719 | int VBOXCALL supdrvOSContAllocOne(PSUPDRVMEMREF pMem, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PRTHCPHYS pHCPhys);
|
---|
720 | void VBOXCALL supdrvOSContFreeOne(PSUPDRVMEMREF pMem);
|
---|
721 | int VBOXCALL supdrvOSLowAllocOne(PSUPDRVMEMREF pMem, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PSUPPAGE paPages);
|
---|
722 | void VBOXCALL supdrvOSLowFreeOne(PSUPDRVMEMREF pMem);
|
---|
723 | int VBOXCALL supdrvOSMemAllocOne(PSUPDRVMEMREF pMem, PRTR0PTR ppvR0, PRTR3PTR ppvR3);
|
---|
724 | void VBOXCALL supdrvOSMemGetPages(PSUPDRVMEMREF pMem, PSUPPAGE paPages);
|
---|
725 | void VBOXCALL supdrvOSMemFreeOne(PSUPDRVMEMREF pMem);
|
---|
726 | #endif
|
---|
727 | #ifndef USE_NEW_OS_INTERFACE_FOR_GIP
|
---|
728 | int VBOXCALL supdrvOSGipMap(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE *ppGip);
|
---|
729 | int VBOXCALL supdrvOSGipUnmap(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE pGip);
|
---|
730 | void VBOXCALL supdrvOSGipResume(PSUPDRVDEVEXT pDevExt);
|
---|
731 | void VBOXCALL supdrvOSGipSuspend(PSUPDRVDEVEXT pDevExt);
|
---|
732 | unsigned VBOXCALL supdrvOSGetCPUCount(void);
|
---|
733 | bool VBOXCALL supdrvOSGetForcedAsyncTscMode(void);
|
---|
734 | #endif
|
---|
735 |
|
---|
736 |
|
---|
737 | /*******************************************************************************
|
---|
738 | * Shared Functions *
|
---|
739 | *******************************************************************************/
|
---|
740 | int VBOXCALL supdrvIOCtl(uintptr_t uIOCtl, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPREQHDR pReqHdr);
|
---|
741 | int VBOXCALL supdrvIOCtlFast(uintptr_t uIOCtl, PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession);
|
---|
742 | int VBOXCALL supdrvInitDevExt(PSUPDRVDEVEXT pDevExt);
|
---|
743 | void VBOXCALL supdrvDeleteDevExt(PSUPDRVDEVEXT pDevExt);
|
---|
744 | int VBOXCALL supdrvCreateSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION *ppSession);
|
---|
745 | void VBOXCALL supdrvCloseSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession);
|
---|
746 | void VBOXCALL supdrvCleanupSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession);
|
---|
747 | int VBOXCALL supdrvGipInit(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE pGip, RTHCPHYS HCPhys, uint64_t u64NanoTS, unsigned uUpdateHz);
|
---|
748 | void VBOXCALL supdrvGipTerm(PSUPGLOBALINFOPAGE pGip);
|
---|
749 | void VBOXCALL supdrvGipUpdate(PSUPGLOBALINFOPAGE pGip, uint64_t u64NanoTS);
|
---|
750 | void VBOXCALL supdrvGipUpdatePerCpu(PSUPGLOBALINFOPAGE pGip, uint64_t u64NanoTS, unsigned iCpu);
|
---|
751 |
|
---|
752 | __END_DECLS
|
---|
753 |
|
---|
754 | #endif
|
---|
755 |
|
---|