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