1 | /* $Id: MMInternal.h 14155 2008-11-12 23:55:08Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MM - Internal header file.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #ifndef ___MMInternal_h
|
---|
23 | #define ___MMInternal_h
|
---|
24 |
|
---|
25 | #include <VBox/cdefs.h>
|
---|
26 | #include <VBox/types.h>
|
---|
27 | #include <VBox/sup.h>
|
---|
28 | #include <VBox/stam.h>
|
---|
29 | #include <iprt/avl.h>
|
---|
30 | #include <iprt/critsect.h>
|
---|
31 |
|
---|
32 |
|
---|
33 |
|
---|
34 | /** @defgroup grp_mm_int Internals
|
---|
35 | * @internal
|
---|
36 | * @ingroup grp_mm
|
---|
37 | * @{
|
---|
38 | */
|
---|
39 |
|
---|
40 | /** @name VM Ring-3 Heap Internals
|
---|
41 | * @{
|
---|
42 | */
|
---|
43 |
|
---|
44 | /** @def MMR3HEAP_WITH_STATISTICS
|
---|
45 | * Enable MMR3Heap statistics.
|
---|
46 | */
|
---|
47 | #if !defined(MMR3HEAP_WITH_STATISTICS) && defined(VBOX_WITH_STATISTICS)
|
---|
48 | # define MMR3HEAP_WITH_STATISTICS
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | /** @def MMR3HEAP_SIZE_ALIGNMENT
|
---|
52 | * The allocation size alignment of the MMR3Heap.
|
---|
53 | */
|
---|
54 | #define MMR3HEAP_SIZE_ALIGNMENT 16
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Heap statistics record.
|
---|
58 | * There is one global and one per allocation tag.
|
---|
59 | */
|
---|
60 | typedef struct MMHEAPSTAT
|
---|
61 | {
|
---|
62 | /** Core avl node, key is the tag. */
|
---|
63 | AVLULNODECORE Core;
|
---|
64 | /** Pointer to the heap the memory belongs to. */
|
---|
65 | struct MMHEAP *pHeap;
|
---|
66 | #ifdef MMR3HEAP_WITH_STATISTICS
|
---|
67 | /** Number of allocation. */
|
---|
68 | uint64_t cAllocations;
|
---|
69 | /** Number of reallocations. */
|
---|
70 | uint64_t cReallocations;
|
---|
71 | /** Number of frees. */
|
---|
72 | uint64_t cFrees;
|
---|
73 | /** Failures. */
|
---|
74 | uint64_t cFailures;
|
---|
75 | /** Number of bytes allocated (sum). */
|
---|
76 | uint64_t cbAllocated;
|
---|
77 | /** Number of bytes freed. */
|
---|
78 | uint64_t cbFreed;
|
---|
79 | /** Number of bytes currently allocated. */
|
---|
80 | size_t cbCurAllocated;
|
---|
81 | #endif
|
---|
82 | } MMHEAPSTAT;
|
---|
83 | /** Pointer to heap statistics record. */
|
---|
84 | typedef MMHEAPSTAT *PMMHEAPSTAT;
|
---|
85 |
|
---|
86 |
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Additional heap block header for relating allocations to the VM.
|
---|
90 | */
|
---|
91 | typedef struct MMHEAPHDR
|
---|
92 | {
|
---|
93 | /** Pointer to the next record. */
|
---|
94 | struct MMHEAPHDR *pNext;
|
---|
95 | /** Pointer to the previous record. */
|
---|
96 | struct MMHEAPHDR *pPrev;
|
---|
97 | /** Pointer to the heap statistics record.
|
---|
98 | * (Where the a PVM can be found.) */
|
---|
99 | PMMHEAPSTAT pStat;
|
---|
100 | /** Size of the allocation (including this header). */
|
---|
101 | size_t cbSize;
|
---|
102 | } MMHEAPHDR;
|
---|
103 | /** Pointer to MM heap header. */
|
---|
104 | typedef MMHEAPHDR *PMMHEAPHDR;
|
---|
105 |
|
---|
106 |
|
---|
107 | /** MM Heap structure. */
|
---|
108 | typedef struct MMHEAP
|
---|
109 | {
|
---|
110 | /** Lock protecting the heap. */
|
---|
111 | RTCRITSECT Lock;
|
---|
112 | /** Heap block list head. */
|
---|
113 | PMMHEAPHDR pHead;
|
---|
114 | /** Heap block list tail. */
|
---|
115 | PMMHEAPHDR pTail;
|
---|
116 | /** Heap per tag statistics tree. */
|
---|
117 | PAVLULNODECORE pStatTree;
|
---|
118 | /** The VM handle. */
|
---|
119 | PUVM pUVM;
|
---|
120 | /** Heap global statistics. */
|
---|
121 | MMHEAPSTAT Stat;
|
---|
122 | } MMHEAP;
|
---|
123 | /** Pointer to MM Heap structure. */
|
---|
124 | typedef MMHEAP *PMMHEAP;
|
---|
125 |
|
---|
126 | /** @} */
|
---|
127 |
|
---|
128 |
|
---|
129 |
|
---|
130 | /** @name Hypervisor Heap Internals
|
---|
131 | * @{
|
---|
132 | */
|
---|
133 |
|
---|
134 | /** @def MMHYPER_HEAP_FREE_DELAY
|
---|
135 | * If defined, it indicates the number of frees that should be delayed.
|
---|
136 | */
|
---|
137 | #if defined(DOXYGEN_RUNNING)
|
---|
138 | # define MMHYPER_HEAP_FREE_DELAY 64
|
---|
139 | #endif
|
---|
140 |
|
---|
141 | /** @def MMHYPER_HEAP_FREE_POISON
|
---|
142 | * If defined, it indicates that freed memory should be poisoned
|
---|
143 | * with the value it has.
|
---|
144 | */
|
---|
145 | #if defined(VBOX_STRICT) || defined(DOXYGEN_RUNNING)
|
---|
146 | # define MMHYPER_HEAP_FREE_POISON 0xcb
|
---|
147 | #endif
|
---|
148 |
|
---|
149 | /** @def MMHYPER_HEAP_STRICT
|
---|
150 | * Enables a bunch of assertions in the heap code. */
|
---|
151 | #if defined(VBOX_STRICT) || defined(DOXYGEN_RUNNING)
|
---|
152 | # define MMHYPER_HEAP_STRICT 1
|
---|
153 | # if 0 || defined(DOXYGEN_RUNNING)
|
---|
154 | /** @def MMHYPER_HEAP_STRICT_FENCE
|
---|
155 | * Enables tail fence. */
|
---|
156 | # define MMHYPER_HEAP_STRICT_FENCE
|
---|
157 | /** @def MMHYPER_HEAP_STRICT_FENCE_SIZE
|
---|
158 | * The fence size in bytes. */
|
---|
159 | # define MMHYPER_HEAP_STRICT_FENCE_SIZE 256
|
---|
160 | /** @def MMHYPER_HEAP_STRICT_FENCE_U32
|
---|
161 | * The fence filler. */
|
---|
162 | # define MMHYPER_HEAP_STRICT_FENCE_U32 UINT32_C(0xdeadbeef)
|
---|
163 | # endif
|
---|
164 | #endif
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Hypervisor heap statistics record.
|
---|
168 | * There is one global and one per allocation tag.
|
---|
169 | */
|
---|
170 | typedef struct MMHYPERSTAT
|
---|
171 | {
|
---|
172 | /** Core avl node, key is the tag.
|
---|
173 | * @todo The type is wrong! Get your lazy a$$ over and create that offsetted uint32_t version we need here! */
|
---|
174 | AVLOGCPHYSNODECORE Core;
|
---|
175 | /** Aligning the 64-bit fields on a 64-bit line. */
|
---|
176 | uint32_t u32Padding0;
|
---|
177 | /** Indicator for whether these statistics are registered with STAM or not. */
|
---|
178 | bool fRegistered;
|
---|
179 | /** Number of allocation. */
|
---|
180 | uint64_t cAllocations;
|
---|
181 | /** Number of frees. */
|
---|
182 | uint64_t cFrees;
|
---|
183 | /** Failures. */
|
---|
184 | uint64_t cFailures;
|
---|
185 | /** Number of bytes allocated (sum). */
|
---|
186 | uint64_t cbAllocated;
|
---|
187 | /** Number of bytes freed (sum). */
|
---|
188 | uint64_t cbFreed;
|
---|
189 | /** Number of bytes currently allocated. */
|
---|
190 | uint32_t cbCurAllocated;
|
---|
191 | /** Max number of bytes allocated. */
|
---|
192 | uint32_t cbMaxAllocated;
|
---|
193 | } MMHYPERSTAT;
|
---|
194 | /** Pointer to hypervisor heap statistics record. */
|
---|
195 | typedef MMHYPERSTAT *PMMHYPERSTAT;
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Hypervisor heap chunk.
|
---|
199 | */
|
---|
200 | typedef struct MMHYPERCHUNK
|
---|
201 | {
|
---|
202 | /** Previous block in the list of all blocks.
|
---|
203 | * This is relative to the start of the heap. */
|
---|
204 | uint32_t offNext;
|
---|
205 | /** Offset to the previous block relative to this one. */
|
---|
206 | int32_t offPrev;
|
---|
207 | /** The statistics record this allocation belongs to (self relative). */
|
---|
208 | int32_t offStat;
|
---|
209 | /** Offset to the heap block (self relative). */
|
---|
210 | int32_t offHeap;
|
---|
211 | } MMHYPERCHUNK;
|
---|
212 | /** Pointer to a hypervisor heap chunk. */
|
---|
213 | typedef MMHYPERCHUNK *PMMHYPERCHUNK;
|
---|
214 |
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Hypervisor heap chunk.
|
---|
218 | */
|
---|
219 | typedef struct MMHYPERCHUNKFREE
|
---|
220 | {
|
---|
221 | /** Main list. */
|
---|
222 | MMHYPERCHUNK core;
|
---|
223 | /** Offset of the next chunk in the list of free nodes. */
|
---|
224 | uint32_t offNext;
|
---|
225 | /** Offset of the previous chunk in the list of free nodes. */
|
---|
226 | int32_t offPrev;
|
---|
227 | /** Size of the block. */
|
---|
228 | uint32_t cb;
|
---|
229 | } MMHYPERCHUNKFREE;
|
---|
230 | /** Pointer to a free hypervisor heap chunk. */
|
---|
231 | typedef MMHYPERCHUNKFREE *PMMHYPERCHUNKFREE;
|
---|
232 |
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * The hypervisor heap.
|
---|
236 | */
|
---|
237 | typedef struct MMHYPERHEAP
|
---|
238 | {
|
---|
239 | /** The typical magic (MMHYPERHEAP_MAGIC). */
|
---|
240 | uint32_t u32Magic;
|
---|
241 | /** The heap size. (This structure is not included!) */
|
---|
242 | uint32_t cbHeap;
|
---|
243 | /** The HC ring-3 address of the heap. */
|
---|
244 | R3PTRTYPE(uint8_t *) pbHeapR3;
|
---|
245 | /** The HC ring-3 address of the shared VM strcture. */
|
---|
246 | PVMR3 pVMR3;
|
---|
247 | /** The HC ring-0 address of the heap. */
|
---|
248 | R0PTRTYPE(uint8_t *) pbHeapR0;
|
---|
249 | /** The HC ring-0 address of the shared VM strcture. */
|
---|
250 | PVMR0 pVMR0;
|
---|
251 | /** The RC address of the heap. */
|
---|
252 | RCPTRTYPE(uint8_t *) pbHeapRC;
|
---|
253 | /** The RC address of the shared VM strcture. */
|
---|
254 | PVMRC pVMRC;
|
---|
255 | /** The amount of free memory in the heap. */
|
---|
256 | uint32_t cbFree;
|
---|
257 | /** Offset of the first free chunk in the heap.
|
---|
258 | * The offset is relative to the start of the heap. */
|
---|
259 | uint32_t offFreeHead;
|
---|
260 | /** Offset of the last free chunk in the heap.
|
---|
261 | * The offset is relative to the start of the heap. */
|
---|
262 | uint32_t offFreeTail;
|
---|
263 | /** Offset of the first page aligned block in the heap.
|
---|
264 | * The offset is equal to cbHeap initially. */
|
---|
265 | uint32_t offPageAligned;
|
---|
266 | /** Tree of hypervisor heap statistics. */
|
---|
267 | AVLOGCPHYSTREE HyperHeapStatTree;
|
---|
268 | #ifdef MMHYPER_HEAP_FREE_DELAY
|
---|
269 | /** Where to insert the next free. */
|
---|
270 | uint32_t iDelayedFree;
|
---|
271 | /** Array of delayed frees. Circular. Offsets relative to this structure. */
|
---|
272 | struct
|
---|
273 | {
|
---|
274 | /** The free caller address. */
|
---|
275 | RTUINTPTR uCaller;
|
---|
276 | /** The offset of the freed chunk. */
|
---|
277 | uint32_t offChunk;
|
---|
278 | } aDelayedFrees[MMHYPER_HEAP_FREE_DELAY];
|
---|
279 | #else
|
---|
280 | /** Padding the structure to a 64-bit aligned size. */
|
---|
281 | uint32_t u32Padding0;
|
---|
282 | #endif
|
---|
283 | } MMHYPERHEAP;
|
---|
284 | /** Pointer to the hypervisor heap. */
|
---|
285 | typedef MMHYPERHEAP *PMMHYPERHEAP;
|
---|
286 |
|
---|
287 | /** Magic value for MMHYPERHEAP. (C. S. Lewis) */
|
---|
288 | #define MMHYPERHEAP_MAGIC UINT32_C(0x18981129)
|
---|
289 |
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * Hypervisor heap minimum alignment (16 bytes).
|
---|
293 | */
|
---|
294 | #define MMHYPER_HEAP_ALIGN_MIN 16
|
---|
295 |
|
---|
296 | /**
|
---|
297 | * The aligned size of the the MMHYPERHEAP structure.
|
---|
298 | */
|
---|
299 | #define MMYPERHEAP_HDR_SIZE RT_ALIGN_Z(sizeof(MMHYPERHEAP), MMHYPER_HEAP_ALIGN_MIN * 4)
|
---|
300 |
|
---|
301 | /** @name Hypervisor heap chunk flags.
|
---|
302 | * The flags are put in the first bits of the MMHYPERCHUNK::offPrev member.
|
---|
303 | * These bits aren't used anyway because of the chunk minimal alignment (16 bytes).
|
---|
304 | * @{ */
|
---|
305 | /** The chunk is free. (The code ASSUMES this is 0!) */
|
---|
306 | #define MMHYPERCHUNK_FLAGS_FREE 0x0
|
---|
307 | /** The chunk is in use. */
|
---|
308 | #define MMHYPERCHUNK_FLAGS_USED 0x1
|
---|
309 | /** The type mask. */
|
---|
310 | #define MMHYPERCHUNK_FLAGS_TYPE_MASK 0x1
|
---|
311 | /** The flag mask */
|
---|
312 | #define MMHYPERCHUNK_FLAGS_MASK 0x1
|
---|
313 |
|
---|
314 | /** Checks if the chunk is free. */
|
---|
315 | #define MMHYPERCHUNK_ISFREE(pChunk) ( (((pChunk)->offPrev) & MMHYPERCHUNK_FLAGS_TYPE_MASK) == MMHYPERCHUNK_FLAGS_FREE )
|
---|
316 | /** Checks if the chunk is used. */
|
---|
317 | #define MMHYPERCHUNK_ISUSED(pChunk) ( (((pChunk)->offPrev) & MMHYPERCHUNK_FLAGS_TYPE_MASK) == MMHYPERCHUNK_FLAGS_USED )
|
---|
318 | /** Toggles FREE/USED flag of a chunk. */
|
---|
319 | #define MMHYPERCHUNK_SET_TYPE(pChunk, type) do { (pChunk)->offPrev = ((pChunk)->offPrev & ~MMHYPERCHUNK_FLAGS_TYPE_MASK) | ((type) & MMHYPERCHUNK_FLAGS_TYPE_MASK); } while (0)
|
---|
320 |
|
---|
321 | /** Gets the prev offset without the flags. */
|
---|
322 | #define MMHYPERCHUNK_GET_OFFPREV(pChunk) ((int32_t)((pChunk)->offPrev & ~MMHYPERCHUNK_FLAGS_MASK))
|
---|
323 | /** Sets the prev offset without changing the flags. */
|
---|
324 | #define MMHYPERCHUNK_SET_OFFPREV(pChunk, off) do { (pChunk)->offPrev = (off) | ((pChunk)->offPrev & MMHYPERCHUNK_FLAGS_MASK); } while (0)
|
---|
325 | #if 0
|
---|
326 | /** Clears one or more flags. */
|
---|
327 | #define MMHYPERCHUNK_FLAGS_OP_CLEAR(pChunk, fFlags) do { ((pChunk)->offPrev) &= ~((fFlags) & MMHYPERCHUNK_FLAGS_MASK); } while (0)
|
---|
328 | /** Sets one or more flags. */
|
---|
329 | #define MMHYPERCHUNK_FLAGS_OP_SET(pChunk, fFlags) do { ((pChunk)->offPrev) |= ((fFlags) & MMHYPERCHUNK_FLAGS_MASK); } while (0)
|
---|
330 | /** Checks if one is set. */
|
---|
331 | #define MMHYPERCHUNK_FLAGS_OP_ISSET(pChunk, fFlag) (!!(((pChunk)->offPrev) & ((fFlag) & MMHYPERCHUNK_FLAGS_MASK)))
|
---|
332 | #endif
|
---|
333 | /** @} */
|
---|
334 |
|
---|
335 | /** @} */
|
---|
336 |
|
---|
337 |
|
---|
338 | /** @name Page Pool Internals
|
---|
339 | * @{
|
---|
340 | */
|
---|
341 |
|
---|
342 | /**
|
---|
343 | * Page sub pool
|
---|
344 | *
|
---|
345 | * About the allocation of this structure. To keep the number of heap blocks,
|
---|
346 | * the number of heap calls, and fragmentation low we allocate all the data
|
---|
347 | * related to a MMPAGESUBPOOL node in one chunk. That means that after the
|
---|
348 | * bitmap (which is of variable size) comes the SUPPAGE records and then
|
---|
349 | * follows the lookup tree nodes. (The heap in question is the hyper heap.)
|
---|
350 | */
|
---|
351 | typedef struct MMPAGESUBPOOL
|
---|
352 | {
|
---|
353 | /** Pointer to next sub pool. */
|
---|
354 | #ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
355 | R3PTRTYPE(struct MMPAGESUBPOOL *) pNext;
|
---|
356 | #else
|
---|
357 | R3R0PTRTYPE(struct MMPAGESUBPOOL *) pNext;
|
---|
358 | #endif
|
---|
359 | /** Pointer to next sub pool in the free chain.
|
---|
360 | * This is NULL if we're not in the free chain or at the end of it. */
|
---|
361 | #ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
362 | R3PTRTYPE(struct MMPAGESUBPOOL *) pNextFree;
|
---|
363 | #else
|
---|
364 | R3R0PTRTYPE(struct MMPAGESUBPOOL *) pNextFree;
|
---|
365 | #endif
|
---|
366 | /** Pointer to array of lock ranges.
|
---|
367 | * This is allocated together with the MMPAGESUBPOOL and thus needs no freeing.
|
---|
368 | * It follows immediately after the bitmap.
|
---|
369 | * The reserved field is a pointer to this structure.
|
---|
370 | */
|
---|
371 | #ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
372 | R3PTRTYPE(PSUPPAGE) paPhysPages;
|
---|
373 | #else
|
---|
374 | R3R0PTRTYPE(PSUPPAGE) paPhysPages;
|
---|
375 | #endif
|
---|
376 | /** Pointer to the first page. */
|
---|
377 | #ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
378 | R3PTRTYPE(void *) pvPages;
|
---|
379 | #else
|
---|
380 | R3R0PTRTYPE(void *) pvPages;
|
---|
381 | #endif
|
---|
382 | /** Size of the subpool. */
|
---|
383 | uint32_t cPages;
|
---|
384 | /** Number of free pages. */
|
---|
385 | uint32_t cPagesFree;
|
---|
386 | /** The allocation bitmap.
|
---|
387 | * This may extend beyond the end of the defined array size.
|
---|
388 | */
|
---|
389 | uint32_t auBitmap[1];
|
---|
390 | /* ... SUPPAGE aRanges[1]; */
|
---|
391 | } MMPAGESUBPOOL;
|
---|
392 | /** Pointer to page sub pool. */
|
---|
393 | typedef MMPAGESUBPOOL *PMMPAGESUBPOOL;
|
---|
394 |
|
---|
395 | /**
|
---|
396 | * Page pool.
|
---|
397 | */
|
---|
398 | typedef struct MMPAGEPOOL
|
---|
399 | {
|
---|
400 | /** List of subpools. */
|
---|
401 | #ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
402 | R3PTRTYPE(PMMPAGESUBPOOL) pHead;
|
---|
403 | #else
|
---|
404 | R3R0PTRTYPE(PMMPAGESUBPOOL) pHead;
|
---|
405 | #endif
|
---|
406 | /** Head of subpools with free pages. */
|
---|
407 | #ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
408 | R3PTRTYPE(PMMPAGESUBPOOL) pHeadFree;
|
---|
409 | #else
|
---|
410 | R3R0PTRTYPE(PMMPAGESUBPOOL) pHeadFree;
|
---|
411 | #endif
|
---|
412 | /** AVLPV tree for looking up HC virtual addresses.
|
---|
413 | * The tree contains MMLOOKUPVIRTPP records.
|
---|
414 | */
|
---|
415 | #ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
416 | R3PTRTYPE(PAVLPVNODECORE) pLookupVirt;
|
---|
417 | #else
|
---|
418 | R3R0PTRTYPE(PAVLPVNODECORE) pLookupVirt;
|
---|
419 | #endif
|
---|
420 | /** Tree for looking up HC physical addresses.
|
---|
421 | * The tree contains MMLOOKUPPHYSHC records.
|
---|
422 | */
|
---|
423 | #ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
424 | R3PTRTYPE(AVLHCPHYSTREE) pLookupPhys;
|
---|
425 | #else
|
---|
426 | R3R0PTRTYPE(AVLHCPHYSTREE) pLookupPhys;
|
---|
427 | #endif
|
---|
428 | /** Pointer to the VM this pool belongs. */
|
---|
429 | #ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
430 | PVMR3 pVM;
|
---|
431 | #else
|
---|
432 | R3R0PTRTYPE(PVM) pVM;
|
---|
433 | #endif
|
---|
434 | /** Flag indicating the allocation method.
|
---|
435 | * Set: SUPLowAlloc().
|
---|
436 | * Clear: SUPPageAlloc() + SUPPageLock(). */
|
---|
437 | bool fLow;
|
---|
438 | /** Number of subpools. */
|
---|
439 | uint32_t cSubPools;
|
---|
440 | /** Number of pages in pool. */
|
---|
441 | uint32_t cPages;
|
---|
442 | #ifdef VBOX_WITH_STATISTICS
|
---|
443 | /** Number of free pages in pool. */
|
---|
444 | uint32_t cFreePages;
|
---|
445 | /** Number of alloc calls. */
|
---|
446 | STAMCOUNTER cAllocCalls;
|
---|
447 | /** Number of free calls. */
|
---|
448 | STAMCOUNTER cFreeCalls;
|
---|
449 | /** Number of to phys conversions. */
|
---|
450 | STAMCOUNTER cToPhysCalls;
|
---|
451 | /** Number of to virtual conversions. */
|
---|
452 | STAMCOUNTER cToVirtCalls;
|
---|
453 | /** Number of real errors. */
|
---|
454 | STAMCOUNTER cErrors;
|
---|
455 | #endif
|
---|
456 | } MMPAGEPOOL;
|
---|
457 | /** Pointer to page pool. */
|
---|
458 | typedef MMPAGEPOOL *PMMPAGEPOOL;
|
---|
459 |
|
---|
460 | /**
|
---|
461 | * Lookup record for HC virtual memory in the page pool.
|
---|
462 | */
|
---|
463 | typedef struct MMPPLOOKUPHCPTR
|
---|
464 | {
|
---|
465 | /** The key is virtual address. */
|
---|
466 | AVLPVNODECORE Core;
|
---|
467 | /** Pointer to subpool if lookup record for a pool. */
|
---|
468 | struct MMPAGESUBPOOL *pSubPool;
|
---|
469 | } MMPPLOOKUPHCPTR;
|
---|
470 | /** Pointer to virtual memory lookup record. */
|
---|
471 | typedef MMPPLOOKUPHCPTR *PMMPPLOOKUPHCPTR;
|
---|
472 |
|
---|
473 | /**
|
---|
474 | * Lookup record for HC physical memory.
|
---|
475 | */
|
---|
476 | typedef struct MMPPLOOKUPHCPHYS
|
---|
477 | {
|
---|
478 | /** The key is physical address. */
|
---|
479 | AVLHCPHYSNODECORE Core;
|
---|
480 | /** Pointer to SUPPAGE record for this physical address. */
|
---|
481 | PSUPPAGE pPhysPage;
|
---|
482 | } MMPPLOOKUPHCPHYS;
|
---|
483 | /** Pointer to physical memory lookup record. */
|
---|
484 | typedef MMPPLOOKUPHCPHYS *PMMPPLOOKUPHCPHYS;
|
---|
485 |
|
---|
486 | /** @} */
|
---|
487 |
|
---|
488 |
|
---|
489 |
|
---|
490 | /**
|
---|
491 | * Type of memory that's locked.
|
---|
492 | */
|
---|
493 | typedef enum MMLOCKEDTYPE
|
---|
494 | {
|
---|
495 | /** Hypervisor: Ring-3 memory locked by MM. */
|
---|
496 | MM_LOCKED_TYPE_HYPER,
|
---|
497 | /** Hypervisor: Ring-3 memory locked by MM that shouldn't be freed up. */
|
---|
498 | MM_LOCKED_TYPE_HYPER_NOFREE,
|
---|
499 | /** Hypervisor: Pre-locked ring-3 pages. */
|
---|
500 | MM_LOCKED_TYPE_HYPER_PAGES,
|
---|
501 | /** Guest: Physical VM memory (RAM & MMIO2). */
|
---|
502 | MM_LOCKED_TYPE_PHYS
|
---|
503 | } MMLOCKEDTYPE;
|
---|
504 | /** Pointer to memory type. */
|
---|
505 | typedef MMLOCKEDTYPE *PMMLOCKEDTYPE;
|
---|
506 |
|
---|
507 |
|
---|
508 | /**
|
---|
509 | * Converts a SUPPAGE pointer to a MMLOCKEDMEM pointer.
|
---|
510 | * @returns Pointer to the MMLOCKEDMEM record the range is associated with.
|
---|
511 | * @param pSupPage Pointer to SUPPAGE structure managed by MM.
|
---|
512 | */
|
---|
513 | #define MM_SUPRANGE_TO_MMLOCKEDMEM(pSupPage) ((PMMLOCKEDMEM)pSupPage->uReserved)
|
---|
514 |
|
---|
515 |
|
---|
516 | /**
|
---|
517 | * Locked memory record.
|
---|
518 | */
|
---|
519 | typedef struct MMLOCKEDMEM
|
---|
520 | {
|
---|
521 | /** Address (host mapping). */
|
---|
522 | void *pv;
|
---|
523 | /** Size. */
|
---|
524 | size_t cb;
|
---|
525 | /** Next record. */
|
---|
526 | struct MMLOCKEDMEM *pNext;
|
---|
527 | /** Record type. */
|
---|
528 | MMLOCKEDTYPE eType;
|
---|
529 | /** Type specific data. */
|
---|
530 | union
|
---|
531 | {
|
---|
532 | /** Data for MM_LOCKED_TYPE_HYPER, MM_LOCKED_TYPE_HYPER_NOFREE and MM_LOCKED_TYPE_HYPER_PAGES. */
|
---|
533 | struct
|
---|
534 | {
|
---|
535 | unsigned uNothing;
|
---|
536 | } hyper;
|
---|
537 |
|
---|
538 | /** Data for MM_LOCKED_TYPE_PHYS. */
|
---|
539 | struct
|
---|
540 | {
|
---|
541 | /** The GC physical address.
|
---|
542 | * (Assuming that this is a linear range of GC physical pages.)
|
---|
543 | */
|
---|
544 | RTGCPHYS GCPhys;
|
---|
545 | } phys;
|
---|
546 | } u;
|
---|
547 |
|
---|
548 | /** Physical Page Array. (Variable length.)
|
---|
549 | * The uReserved field contains pointer to the MMLOCKMEM record.
|
---|
550 | * Use the macro MM_SUPPAGE_TO_MMLOCKEDMEM() to convert.
|
---|
551 | *
|
---|
552 | * For MM_LOCKED_TYPE_PHYS the low 12 bits of the pvPhys member
|
---|
553 | * are bits (MM_RAM_FLAGS_*) and not part of the physical address.
|
---|
554 | */
|
---|
555 | SUPPAGE aPhysPages[1];
|
---|
556 | } MMLOCKEDMEM;
|
---|
557 | /** Pointer to locked memory. */
|
---|
558 | typedef MMLOCKEDMEM *PMMLOCKEDMEM;
|
---|
559 |
|
---|
560 |
|
---|
561 | /**
|
---|
562 | * A registered Rom range.
|
---|
563 | *
|
---|
564 | * This is used to track ROM registrations both for debug reasons
|
---|
565 | * and for resetting shadow ROM at reset.
|
---|
566 | *
|
---|
567 | * This is allocated of the MMR3Heap and thus only accessibel from ring-3.
|
---|
568 | */
|
---|
569 | typedef struct MMROMRANGE
|
---|
570 | {
|
---|
571 | /** Pointer to the next */
|
---|
572 | struct MMROMRANGE *pNext;
|
---|
573 | /** Address of the range. */
|
---|
574 | RTGCPHYS GCPhys;
|
---|
575 | /** Size of the range. */
|
---|
576 | uint32_t cbRange;
|
---|
577 | /** Shadow ROM? */
|
---|
578 | bool fShadow;
|
---|
579 | /** Is the shadow ROM currently wriable? */
|
---|
580 | bool fWritable;
|
---|
581 | /** The address of the virgin ROM image for shadow ROM. */
|
---|
582 | const void *pvBinary;
|
---|
583 | /** The address of the guest RAM that's shadowing the ROM. (lazy bird) */
|
---|
584 | void *pvCopy;
|
---|
585 | /** The ROM description. */
|
---|
586 | const char *pszDesc;
|
---|
587 | } MMROMRANGE;
|
---|
588 | /** Pointer to a ROM range. */
|
---|
589 | typedef MMROMRANGE *PMMROMRANGE;
|
---|
590 |
|
---|
591 |
|
---|
592 | /**
|
---|
593 | * Hypervisor memory mapping type.
|
---|
594 | */
|
---|
595 | typedef enum MMLOOKUPHYPERTYPE
|
---|
596 | {
|
---|
597 | /** Invalid record. This is used for record which are incomplete. */
|
---|
598 | MMLOOKUPHYPERTYPE_INVALID = 0,
|
---|
599 | /** Mapping of locked memory. */
|
---|
600 | MMLOOKUPHYPERTYPE_LOCKED,
|
---|
601 | /** Mapping of contiguous HC physical memory. */
|
---|
602 | MMLOOKUPHYPERTYPE_HCPHYS,
|
---|
603 | /** Mapping of contiguous GC physical memory. */
|
---|
604 | MMLOOKUPHYPERTYPE_GCPHYS,
|
---|
605 | /** Mapping of MMIO2 memory. */
|
---|
606 | MMLOOKUPHYPERTYPE_MMIO2,
|
---|
607 | /** Dynamic mapping area (MMR3HyperReserve).
|
---|
608 | * A conversion will require to check what's in the page table for the pages. */
|
---|
609 | MMLOOKUPHYPERTYPE_DYNAMIC
|
---|
610 | } MMLOOKUPHYPERTYPE;
|
---|
611 |
|
---|
612 | /**
|
---|
613 | * Lookup record for the hypervisor memory area.
|
---|
614 | */
|
---|
615 | typedef struct MMLOOKUPHYPER
|
---|
616 | {
|
---|
617 | /** Byte offset from the start of this record to the next.
|
---|
618 | * If the value is NIL_OFFSET the chain is terminated. */
|
---|
619 | int32_t offNext;
|
---|
620 | /** Offset into the hypvervisor memory area. */
|
---|
621 | uint32_t off;
|
---|
622 | /** Size of this part. */
|
---|
623 | uint32_t cb;
|
---|
624 | /** Locking type. */
|
---|
625 | MMLOOKUPHYPERTYPE enmType;
|
---|
626 | /** Type specific data */
|
---|
627 | union
|
---|
628 | {
|
---|
629 | /** Locked memory. */
|
---|
630 | struct
|
---|
631 | {
|
---|
632 | /** Host context pointer. */
|
---|
633 | R3PTRTYPE(void *) pvR3;
|
---|
634 | /** Host context ring-0 pointer. */
|
---|
635 | /** @todo #1865: Check if this actually works (doubt it) */
|
---|
636 | RTR0PTR pvR0;
|
---|
637 | /** Pointer to the locked mem record. */
|
---|
638 | R3PTRTYPE(PMMLOCKEDMEM) pLockedMem;
|
---|
639 | } Locked;
|
---|
640 |
|
---|
641 | /** Contiguous physical memory. */
|
---|
642 | struct
|
---|
643 | {
|
---|
644 | /** Host context pointer. */
|
---|
645 | R3PTRTYPE(void *) pvR3;
|
---|
646 | /** @todo #1865: Add a pvR0 here! */
|
---|
647 | /** HC physical address corresponding to pvR3. */
|
---|
648 | RTHCPHYS HCPhys;
|
---|
649 | } HCPhys;
|
---|
650 |
|
---|
651 | /** Contiguous guest physical memory. */
|
---|
652 | struct
|
---|
653 | {
|
---|
654 | /** The memory address (Guest Context). */
|
---|
655 | RTGCPHYS GCPhys;
|
---|
656 | } GCPhys;
|
---|
657 |
|
---|
658 | /** MMIO2 memory. */
|
---|
659 | struct
|
---|
660 | {
|
---|
661 | /** The device instance owning the MMIO2 region. */
|
---|
662 | PPDMDEVINSR3 pDevIns;
|
---|
663 | /** The region number. */
|
---|
664 | uint32_t iRegion;
|
---|
665 | /** The offset into the MMIO2 region. */
|
---|
666 | RTGCPHYS off;
|
---|
667 | } MMIO2;
|
---|
668 | } u;
|
---|
669 | /** Description. */
|
---|
670 | R3PTRTYPE(const char *) pszDesc;
|
---|
671 | } MMLOOKUPHYPER;
|
---|
672 | /** Pointer to a hypervisor memory lookup record. */
|
---|
673 | typedef MMLOOKUPHYPER *PMMLOOKUPHYPER;
|
---|
674 |
|
---|
675 |
|
---|
676 | /**
|
---|
677 | * Converts a MM pointer into a VM pointer.
|
---|
678 | * @returns Pointer to the VM structure the MM is part of.
|
---|
679 | * @param pMM Pointer to MM instance data.
|
---|
680 | */
|
---|
681 | #define MM2VM(pMM) ( (PVM)((uint8_t *)pMM - pMM->offVM) )
|
---|
682 |
|
---|
683 |
|
---|
684 | /**
|
---|
685 | * MM Data (part of VM)
|
---|
686 | */
|
---|
687 | typedef struct MM
|
---|
688 | {
|
---|
689 | /** Offset to the VM structure.
|
---|
690 | * See MM2VM(). */
|
---|
691 | RTINT offVM;
|
---|
692 |
|
---|
693 | /** Set if MMR3InitPaging has been called. */
|
---|
694 | bool fDoneMMR3InitPaging;
|
---|
695 | /** Set if PGM has been initialized and we can safely call PGMR3Map(). */
|
---|
696 | bool fPGMInitialized;
|
---|
697 | #if GC_ARCH_BITS == 64 || HC_ARCH_BITS == 64
|
---|
698 | uint32_t u32Padding1; /**< alignment padding. */
|
---|
699 | #endif
|
---|
700 |
|
---|
701 | /** Lookup list for the Hypervisor Memory Area.
|
---|
702 | * The offset is relative to the start of the heap.
|
---|
703 | * Use pHyperHeapR3, pHyperHeapR0 or pHypeRHeapRC to calculate the address.
|
---|
704 | */
|
---|
705 | RTUINT offLookupHyper;
|
---|
706 |
|
---|
707 | /** The offset of the next static mapping in the Hypervisor Memory Area. */
|
---|
708 | RTUINT offHyperNextStatic;
|
---|
709 | /** The size of the HMA.
|
---|
710 | * Starts at 12MB and will be fixed late in the init process. */
|
---|
711 | RTUINT cbHyperArea;
|
---|
712 |
|
---|
713 | /** Guest address of the Hypervisor Memory Area.
|
---|
714 | * @remarks It's still a bit open whether this should be change to RTRCPTR or
|
---|
715 | * remain a RTGCPTR. */
|
---|
716 | RTGCPTR pvHyperAreaGC;
|
---|
717 |
|
---|
718 | /** The hypervisor heap (GC Ptr). */
|
---|
719 | RCPTRTYPE(PMMHYPERHEAP) pHyperHeapRC;
|
---|
720 | #if HC_ARCH_BITS == 64 && GC_ARCH_BITS == 64
|
---|
721 | uint32_t u32Padding2;
|
---|
722 | #endif
|
---|
723 |
|
---|
724 | /** The hypervisor heap (R0 Ptr). */
|
---|
725 | R0PTRTYPE(PMMHYPERHEAP) pHyperHeapR0;
|
---|
726 | #ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
727 | /** Page pool - R0 Ptr. */
|
---|
728 | R0PTRTYPE(PMMPAGEPOOL) pPagePoolR0;
|
---|
729 | /** Page pool pages in low memory R0 Ptr. */
|
---|
730 | R0PTRTYPE(PMMPAGEPOOL) pPagePoolLowR0;
|
---|
731 | #endif /* !VBOX_WITH_2X_4GB_ADDR_SPACE */
|
---|
732 |
|
---|
733 | /** The hypervisor heap (R3 Ptr). */
|
---|
734 | R3PTRTYPE(PMMHYPERHEAP) pHyperHeapR3;
|
---|
735 | /** Page pool - R3 Ptr. */
|
---|
736 | R3PTRTYPE(PMMPAGEPOOL) pPagePoolR3;
|
---|
737 | /** Page pool pages in low memory R3 Ptr. */
|
---|
738 | R3PTRTYPE(PMMPAGEPOOL) pPagePoolLowR3;
|
---|
739 | /** List of memory locks. (HC only) */
|
---|
740 | R3PTRTYPE(PMMLOCKEDMEM) pLockedMem;
|
---|
741 |
|
---|
742 | /** Pointer to the dummy page.
|
---|
743 | * The dummy page is a paranoia thingy used for instance for pure MMIO RAM ranges
|
---|
744 | * to make sure any bugs will not harm whatever the system stores in the first
|
---|
745 | * physical page. */
|
---|
746 | R3PTRTYPE(void *) pvDummyPage;
|
---|
747 | /** Physical address of the dummy page. */
|
---|
748 | RTHCPHYS HCPhysDummyPage;
|
---|
749 |
|
---|
750 | /** Size of the base RAM in bytes. (The CFGM RamSize value.) */
|
---|
751 | uint64_t cbRamBase;
|
---|
752 | /** The number of base RAM pages that PGM has reserved (GMM).
|
---|
753 | * @remarks Shadow ROMs will be counted twice (RAM+ROM), so it won't be 1:1 with
|
---|
754 | * what the guest sees. */
|
---|
755 | uint64_t cBasePages;
|
---|
756 | /** The number of shadow pages PGM has reserved (GMM). */
|
---|
757 | uint32_t cShadowPages;
|
---|
758 | /** The number of fixed pages we've reserved (GMM). */
|
---|
759 | uint32_t cFixedPages;
|
---|
760 |
|
---|
761 | /** The head of the ROM ranges. */
|
---|
762 | R3PTRTYPE(PMMROMRANGE) pRomHead;
|
---|
763 | } MM;
|
---|
764 | /** Pointer to MM Data (part of VM). */
|
---|
765 | typedef MM *PMM;
|
---|
766 |
|
---|
767 |
|
---|
768 | /**
|
---|
769 | * MM data kept in the UVM.
|
---|
770 | */
|
---|
771 | typedef struct MMUSERPERVM
|
---|
772 | {
|
---|
773 | /** Pointer to the MM R3 Heap. */
|
---|
774 | R3PTRTYPE(PMMHEAP) pHeap;
|
---|
775 | } MMUSERPERVM;
|
---|
776 | /** Pointer to the MM data kept in the UVM. */
|
---|
777 | typedef MMUSERPERVM *PMMUSERPERVM;
|
---|
778 |
|
---|
779 |
|
---|
780 | __BEGIN_DECLS
|
---|
781 |
|
---|
782 |
|
---|
783 | int mmR3UpdateReservation(PVM pVM);
|
---|
784 |
|
---|
785 | int mmR3PagePoolInit(PVM pVM);
|
---|
786 | void mmR3PagePoolTerm(PVM pVM);
|
---|
787 |
|
---|
788 | int mmR3HeapCreateU(PUVM pUVM, PMMHEAP *ppHeap);
|
---|
789 | void mmR3HeapDestroy(PMMHEAP pHeap);
|
---|
790 |
|
---|
791 | int mmR3HyperInit(PVM pVM);
|
---|
792 | int mmR3HyperInitPaging(PVM pVM);
|
---|
793 |
|
---|
794 | int mmR3LockMem(PVM pVM, void *pv, size_t cb, MMLOCKEDTYPE eType, PMMLOCKEDMEM *ppLockedMem, bool fSilentFailure);
|
---|
795 | int mmR3MapLocked(PVM pVM, PMMLOCKEDMEM pLockedMem, RTGCPTR Addr, unsigned iPage, size_t cPages, unsigned fFlags);
|
---|
796 |
|
---|
797 | const char *mmR3GetTagName(MMTAG enmTag);
|
---|
798 |
|
---|
799 | void mmR3PhysRomReset(PVM pVM);
|
---|
800 |
|
---|
801 | /**
|
---|
802 | * Converts a pool address to a physical address.
|
---|
803 | * The specified allocation type must match with the address.
|
---|
804 | *
|
---|
805 | * @returns Physical address.
|
---|
806 | * @returns NIL_RTHCPHYS if not found or eType is not matching.
|
---|
807 | * @param pPool Pointer to the page pool.
|
---|
808 | * @param pv The address to convert.
|
---|
809 | * @thread The Emulation Thread.
|
---|
810 | */
|
---|
811 | RTHCPHYS mmPagePoolPtr2Phys(PMMPAGEPOOL pPool, void *pv);
|
---|
812 |
|
---|
813 | /**
|
---|
814 | * Converts a pool physical address to a linear address.
|
---|
815 | * The specified allocation type must match with the address.
|
---|
816 | *
|
---|
817 | * @returns Physical address.
|
---|
818 | * @returns NULL if not found or eType is not matching.
|
---|
819 | * @param pPool Pointer to the page pool.
|
---|
820 | * @param HCPhys The address to convert.
|
---|
821 | * @thread The Emulation Thread.
|
---|
822 | */
|
---|
823 | void *mmPagePoolPhys2Ptr(PMMPAGEPOOL pPool, RTHCPHYS HCPhys);
|
---|
824 |
|
---|
825 | __END_DECLS
|
---|
826 |
|
---|
827 | /** @} */
|
---|
828 |
|
---|
829 | #endif
|
---|
830 |
|
---|