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