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