1 | /* $Id: MMInternal.h 323 2007-01-25 17:25:01Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MM - Internal header file.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006 InnoTek Systemberatung 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 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
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 | #if !defined(IN_MM_R3) && !defined(IN_MM_R0) && !defined(IN_MM_GC)
|
---|
34 | # error "Not in MM! This is an internal header!"
|
---|
35 | #endif
|
---|
36 |
|
---|
37 |
|
---|
38 | /** @defgroup grp_mm_int Internals
|
---|
39 | * @internal
|
---|
40 | * @ingroup grp_mm
|
---|
41 | * @{
|
---|
42 | */
|
---|
43 |
|
---|
44 | /** @name VM Ring-3 Heap Internals
|
---|
45 | * @{
|
---|
46 | */
|
---|
47 |
|
---|
48 | /** @def MMR3HEAP_WITH_STATISTICS
|
---|
49 | * Enable MMR3Heap statistics.
|
---|
50 | */
|
---|
51 | #if !defined(MMR3HEAP_WITH_STATISTICS) && defined(VBOX_WITH_STATISTICS)
|
---|
52 | # define MMR3HEAP_WITH_STATISTICS
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | /** @def MMR3HEAP_SIZE_ALIGNMENT
|
---|
56 | * The allocation size alignment of the MMR3Heap.
|
---|
57 | */
|
---|
58 | #define MMR3HEAP_SIZE_ALIGNMENT 16
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Heap statistics record.
|
---|
62 | * There is one global and one per allocation tag.
|
---|
63 | */
|
---|
64 | typedef struct MMHEAPSTAT
|
---|
65 | {
|
---|
66 | /** Core avl node, key is the tag. */
|
---|
67 | AVLULNODECORE Core;
|
---|
68 | /** Pointer to the heap the memory belongs to. */
|
---|
69 | struct MMHEAP *pHeap;
|
---|
70 | #ifdef MMR3HEAP_WITH_STATISTICS
|
---|
71 | /** Number of allocation. */
|
---|
72 | uint64_t cAllocations;
|
---|
73 | /** Number of reallocations. */
|
---|
74 | uint64_t cReallocations;
|
---|
75 | /** Number of frees. */
|
---|
76 | uint64_t cFrees;
|
---|
77 | /** Failures. */
|
---|
78 | uint64_t cFailures;
|
---|
79 | /** Number of bytes allocated (sum). */
|
---|
80 | uint64_t cbAllocated;
|
---|
81 | /** Number of bytes freed. */
|
---|
82 | uint64_t cbFreed;
|
---|
83 | /** Number of bytes currently allocated. */
|
---|
84 | size_t cbCurAllocated;
|
---|
85 | #endif
|
---|
86 | } MMHEAPSTAT;
|
---|
87 | /** Pointer to heap statistics record. */
|
---|
88 | typedef MMHEAPSTAT *PMMHEAPSTAT;
|
---|
89 |
|
---|
90 |
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Additional heap block header for relating allocations to the VM.
|
---|
94 | */
|
---|
95 | typedef struct MMHEAPHDR
|
---|
96 | {
|
---|
97 | /** Pointer to the next record. */
|
---|
98 | struct MMHEAPHDR *pNext;
|
---|
99 | /** Pointer to the previous record. */
|
---|
100 | struct MMHEAPHDR *pPrev;
|
---|
101 | /** Pointer to the heap statistics record.
|
---|
102 | * (Where the a PVM can be found.) */
|
---|
103 | PMMHEAPSTAT pStat;
|
---|
104 | /** Size of the allocation (including this header). */
|
---|
105 | size_t cbSize;
|
---|
106 | } MMHEAPHDR;
|
---|
107 | /** Pointer to MM heap header. */
|
---|
108 | typedef MMHEAPHDR *PMMHEAPHDR;
|
---|
109 |
|
---|
110 |
|
---|
111 | /** MM Heap structure. */
|
---|
112 | typedef struct MMHEAP
|
---|
113 | {
|
---|
114 | /** Lock protecting the heap. */
|
---|
115 | RTCRITSECT Lock;
|
---|
116 | /** Heap block list head. */
|
---|
117 | PMMHEAPHDR pHead;
|
---|
118 | /** Heap block list tail. */
|
---|
119 | PMMHEAPHDR pTail;
|
---|
120 | /** Heap per tag statistics tree. */
|
---|
121 | PAVLULNODECORE pStatTree;
|
---|
122 | /** The VM handle. */
|
---|
123 | PVM pVM;
|
---|
124 | /** Heap global statistics. */
|
---|
125 | MMHEAPSTAT Stat;
|
---|
126 | } MMHEAP;
|
---|
127 | /** Pointer to MM Heap structure. */
|
---|
128 | typedef MMHEAP *PMMHEAP;
|
---|
129 |
|
---|
130 | /** @} */
|
---|
131 |
|
---|
132 |
|
---|
133 |
|
---|
134 | /** @name Hypervisor Heap Internals
|
---|
135 | * @{
|
---|
136 | */
|
---|
137 |
|
---|
138 | /** @def MMHYPER_HEAP_FREE_DELAY
|
---|
139 | * If defined, it indicates the number of frees that should be delayed.
|
---|
140 | */
|
---|
141 | #if defined(__DOXYGEN__)
|
---|
142 | # define MMHYPER_HEAP_FREE_DELAY 64
|
---|
143 | #endif
|
---|
144 |
|
---|
145 | /** @def MMHYPER_HEAP_FREE_POISON
|
---|
146 | * If defined, it indicates that freed memory should be poisoned
|
---|
147 | * with the value it has.
|
---|
148 | */
|
---|
149 | #if defined(VBOX_STRICT) || defined(__DOXYGEN__)
|
---|
150 | # define MMHYPER_HEAP_FREE_POISON 0xCB
|
---|
151 | #endif
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Hypervisor heap statistics record.
|
---|
155 | * There is one global and one per allocation tag.
|
---|
156 | */
|
---|
157 | typedef struct MMHYPERSTAT
|
---|
158 | {
|
---|
159 | /** Core avl node, key is the tag.
|
---|
160 | * @todo The type is wrong! Get your lazy a$$ over and create that offsetted uint32_t version we need here! */
|
---|
161 | AVLOGCPHYSNODECORE Core;
|
---|
162 | /** Aligning the 64-bit fields on a 64-bit line. */
|
---|
163 | uint32_t u32Padding0;
|
---|
164 | /** Indicator for whether these statistics are registered with STAM or not. */
|
---|
165 | bool fRegistered;
|
---|
166 | /** Number of allocation. */
|
---|
167 | uint64_t cAllocations;
|
---|
168 | /** Number of frees. */
|
---|
169 | uint64_t cFrees;
|
---|
170 | /** Failures. */
|
---|
171 | uint64_t cFailures;
|
---|
172 | /** Number of bytes allocated (sum). */
|
---|
173 | uint64_t cbAllocated;
|
---|
174 | /** Number of bytes freed (sum). */
|
---|
175 | uint64_t cbFreed;
|
---|
176 | /** Number of bytes currently allocated. */
|
---|
177 | uint32_t cbCurAllocated;
|
---|
178 | /** Max number of bytes allocated. */
|
---|
179 | uint32_t cbMaxAllocated;
|
---|
180 | } MMHYPERSTAT;
|
---|
181 | /** Pointer to hypervisor heap statistics record. */
|
---|
182 | typedef MMHYPERSTAT *PMMHYPERSTAT;
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Hypervisor heap chunk.
|
---|
186 | */
|
---|
187 | typedef struct MMHYPERCHUNK
|
---|
188 | {
|
---|
189 | /** Previous block in the list of all blocks.
|
---|
190 | * This is relative to the start of the heap. */
|
---|
191 | uint32_t offNext;
|
---|
192 | /** Offset to the previous block relative to this one. */
|
---|
193 | int32_t offPrev;
|
---|
194 | /** The statistics record this allocation belongs to (self relative). */
|
---|
195 | int32_t offStat;
|
---|
196 | /** Offset to the heap block (self relative). */
|
---|
197 | int32_t offHeap;
|
---|
198 | } MMHYPERCHUNK;
|
---|
199 | /** Pointer to a hypervisor heap chunk. */
|
---|
200 | typedef MMHYPERCHUNK *PMMHYPERCHUNK;
|
---|
201 |
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Hypervisor heap chunk.
|
---|
205 | */
|
---|
206 | typedef struct MMHYPERCHUNKFREE
|
---|
207 | {
|
---|
208 | /** Main list. */
|
---|
209 | MMHYPERCHUNK core;
|
---|
210 | /** Offset of the next chunk in the list of free nodes. */
|
---|
211 | uint32_t offNext;
|
---|
212 | /** Offset of the previous chunk in the list of free nodes. */
|
---|
213 | int32_t offPrev;
|
---|
214 | /** Size of the block. */
|
---|
215 | uint32_t cb;
|
---|
216 | } MMHYPERCHUNKFREE;
|
---|
217 | /** Pointer to a free hypervisor heap chunk. */
|
---|
218 | typedef MMHYPERCHUNKFREE *PMMHYPERCHUNKFREE;
|
---|
219 |
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * The hypervisor heap.
|
---|
223 | */
|
---|
224 | typedef struct MMHYPERHEAP
|
---|
225 | {
|
---|
226 | /** The typical magic (MMHYPERHEAP_MAGIC). */
|
---|
227 | uint32_t u32Magic;
|
---|
228 | /** The heap size. (This structure is not included!) */
|
---|
229 | uint32_t cbHeap;
|
---|
230 | /** The HC Ring-3 address of the VM. */
|
---|
231 | HCPTRTYPE(PVM) pVMHC;
|
---|
232 | /** The HC Ring-3 address of the heap. */
|
---|
233 | HCPTRTYPE(uint8_t *) pbHeapHC;
|
---|
234 | /** The GC address of the heap. */
|
---|
235 | GCPTRTYPE(uint8_t *) pbHeapGC;
|
---|
236 | /** The GC address of the VM. */
|
---|
237 | GCPTRTYPE(PVM) pVMGC;
|
---|
238 | /** The amount of free memory in the heap. */
|
---|
239 | uint32_t cbFree;
|
---|
240 | /** Offset of the first free chunk in the heap.
|
---|
241 | * The offset is relative to the start of the heap. */
|
---|
242 | uint32_t offFreeHead;
|
---|
243 | /** Offset of the last free chunk in the heap.
|
---|
244 | * The offset is relative to the start of the heap. */
|
---|
245 | uint32_t offFreeTail;
|
---|
246 | /** Offset of the first page aligned block in the heap.
|
---|
247 | * The offset is equal to cbHeap initially. */
|
---|
248 | uint32_t offPageAligned;
|
---|
249 | /** Tree of hypervisor heap statistics. */
|
---|
250 | AVLOGCPHYSTREE HyperHeapStatTree;
|
---|
251 | #ifdef MMHYPER_HEAP_FREE_DELAY
|
---|
252 | /** Where to insert the next free. */
|
---|
253 | uint32_t iDelayedFree;
|
---|
254 | /** Array of delayed frees. Circular. Offsets relative to this structure. */
|
---|
255 | struct
|
---|
256 | {
|
---|
257 | /** The free caller address. */
|
---|
258 | RTUINTPTR uCaller;
|
---|
259 | /** The offset of the freed chunk. */
|
---|
260 | uint32_t offChunk;
|
---|
261 | } aDelayedFrees[MMHYPER_HEAP_FREE_DELAY];
|
---|
262 | #else
|
---|
263 | /** Padding the structure to a 64-bit aligned size. */
|
---|
264 | uint32_t u32Padding0;
|
---|
265 | #endif
|
---|
266 | } MMHYPERHEAP;
|
---|
267 | /** Pointer to the hypervisor heap. */
|
---|
268 | typedef MMHYPERHEAP *PMMHYPERHEAP;
|
---|
269 |
|
---|
270 | /** Magic value for MMHYPERHEAP. (C. S. Lewis) */
|
---|
271 | #define MMHYPERHEAP_MAGIC 0x18981129
|
---|
272 |
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Hypervisor heap minimum alignment (16 bytes).
|
---|
276 | */
|
---|
277 | #define MMHYPER_HEAP_ALIGN_MIN 16
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * The aligned size of the the MMHYPERHEAP structure.
|
---|
281 | */
|
---|
282 | #define MMYPERHEAP_HDR_SIZE RT_ALIGN_Z(sizeof(MMHYPERHEAP), MMHYPER_HEAP_ALIGN_MIN * 4)
|
---|
283 |
|
---|
284 | /** @name Hypervisor heap chunk flags.
|
---|
285 | * The flags are put in the first bits of the MMHYPERCHUNK::offPrev member.
|
---|
286 | * These bits aren't used anyway because of the chunk minimal alignment (16 bytes).
|
---|
287 | * @{ */
|
---|
288 | /** The chunk is free. (The code ASSUMES this is 0!) */
|
---|
289 | #define MMHYPERCHUNK_FLAGS_FREE 0x0
|
---|
290 | /** The chunk is in use. */
|
---|
291 | #define MMHYPERCHUNK_FLAGS_USED 0x1
|
---|
292 | /** The type mask. */
|
---|
293 | #define MMHYPERCHUNK_FLAGS_TYPE_MASK 0x1
|
---|
294 | /** The flag mask */
|
---|
295 | #define MMHYPERCHUNK_FLAGS_MASK 0x1
|
---|
296 |
|
---|
297 | /** Checks if the chunk is free. */
|
---|
298 | #define MMHYPERCHUNK_ISFREE(pChunk) ( (((pChunk)->offPrev) & MMHYPERCHUNK_FLAGS_TYPE_MASK) == MMHYPERCHUNK_FLAGS_FREE )
|
---|
299 | /** Checks if the chunk is used. */
|
---|
300 | #define MMHYPERCHUNK_ISUSED(pChunk) ( (((pChunk)->offPrev) & MMHYPERCHUNK_FLAGS_TYPE_MASK) == MMHYPERCHUNK_FLAGS_USED )
|
---|
301 | /** Toggles FREE/USED flag of a chunk. */
|
---|
302 | #define MMHYPERCHUNK_SET_TYPE(pChunk, type) do { (pChunk)->offPrev = ((pChunk)->offPrev & ~MMHYPERCHUNK_FLAGS_TYPE_MASK) | ((type) & MMHYPERCHUNK_FLAGS_TYPE_MASK); } while (0)
|
---|
303 |
|
---|
304 | /** Gets the prev offset without the flags. */
|
---|
305 | #define MMHYPERCHUNK_GET_OFFPREV(pChunk) ((int32_t)((pChunk)->offPrev & ~MMHYPERCHUNK_FLAGS_MASK))
|
---|
306 | /** Sets the prev offset without changing the flags. */
|
---|
307 | #define MMHYPERCHUNK_SET_OFFPREV(pChunk, off) do { (pChunk)->offPrev = (off) | ((pChunk)->offPrev & MMHYPERCHUNK_FLAGS_MASK); } while (0)
|
---|
308 | #if 0
|
---|
309 | /** Clears one or more flags. */
|
---|
310 | #define MMHYPERCHUNK_FLAGS_OP_CLEAR(pChunk, fFlags) do { ((pChunk)->offPrev) &= ~((fFlags) & MMHYPERCHUNK_FLAGS_MASK); } while (0)
|
---|
311 | /** Sets one or more flags. */
|
---|
312 | #define MMHYPERCHUNK_FLAGS_OP_SET(pChunk, fFlags) do { ((pChunk)->offPrev) |= ((fFlags) & MMHYPERCHUNK_FLAGS_MASK); } while (0)
|
---|
313 | /** Checks if one is set. */
|
---|
314 | #define MMHYPERCHUNK_FLAGS_OP_ISSET(pChunk, fFlag) (!!(((pChunk)->offPrev) & ((fFlag) & MMHYPERCHUNK_FLAGS_MASK)))
|
---|
315 | #endif
|
---|
316 | /** @} */
|
---|
317 |
|
---|
318 | /** @} */
|
---|
319 |
|
---|
320 |
|
---|
321 | /** @name Page Pool Internals
|
---|
322 | * @{
|
---|
323 | */
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * Page sub pool
|
---|
327 | *
|
---|
328 | * About the allocation of this structrue. To keep the number of heap blocks,
|
---|
329 | * the number of heap calls, and fragmentation low we allocate all the data
|
---|
330 | * related to a MMPAGESUBPOOL node in one chunk. That means that after the
|
---|
331 | * bitmap (which is of variable size) comes the SUPPAGE records and then
|
---|
332 | * follows the lookup tree nodes.
|
---|
333 | */
|
---|
334 | typedef struct MMPAGESUBPOOL
|
---|
335 | {
|
---|
336 | /** Pointer to next sub pool. */
|
---|
337 | struct MMPAGESUBPOOL *pNext;
|
---|
338 | /** Pointer to next sub pool in the free chain.
|
---|
339 | * This is NULL if we're not in the free chain or at the end of it. */
|
---|
340 | struct MMPAGESUBPOOL *pNextFree;
|
---|
341 | /** Pointer to array of lock ranges.
|
---|
342 | * This is allocated together with the MMPAGESUBPOOL and thus needs no freeing.
|
---|
343 | * It follows immediately after the bitmap.
|
---|
344 | * The reserved field is a pointer to this structure.
|
---|
345 | */
|
---|
346 | PSUPPAGE paPhysPages;
|
---|
347 | /** Pointer to the first page. */
|
---|
348 | void *pvPages;
|
---|
349 | /** Size of the subpool. */
|
---|
350 | unsigned cPages;
|
---|
351 | /** Number of free pages. */
|
---|
352 | unsigned cPagesFree;
|
---|
353 | /** The allocation bitmap.
|
---|
354 | * This may extend beyond the end of the defined array size.
|
---|
355 | */
|
---|
356 | unsigned auBitmap[1];
|
---|
357 | /* ... SUPPAGE aRanges[1]; */
|
---|
358 | } MMPAGESUBPOOL;
|
---|
359 | /** Pointer to page sub pool. */
|
---|
360 | typedef MMPAGESUBPOOL *PMMPAGESUBPOOL;
|
---|
361 |
|
---|
362 | /**
|
---|
363 | * Page pool.
|
---|
364 | */
|
---|
365 | typedef struct MMPAGEPOOL
|
---|
366 | {
|
---|
367 | /** List of subpools. */
|
---|
368 | PMMPAGESUBPOOL pHead;
|
---|
369 | /** Head of subpools with free pages. */
|
---|
370 | PMMPAGESUBPOOL pHeadFree;
|
---|
371 | /** AVLPV tree for looking up HC virtual addresses.
|
---|
372 | * The tree contains MMLOOKUPVIRTPP records.
|
---|
373 | */
|
---|
374 | PAVLPVNODECORE pLookupVirt;
|
---|
375 | /** Tree for looking up HC physical addresses.
|
---|
376 | * The tree contains MMLOOKUPPHYSHC records.
|
---|
377 | */
|
---|
378 | AVLHCPHYSTREE pLookupPhys;
|
---|
379 | /** Pointer to the VM this pool belongs. */
|
---|
380 | PVM pVM;
|
---|
381 | /** Flag indicating the allocation method.
|
---|
382 | * Set: SUPLowAlloc().
|
---|
383 | * Clear: SUPPageAlloc() + SUPPageLock(). */
|
---|
384 | bool fLow;
|
---|
385 | /** Number of subpools. */
|
---|
386 | uint32_t cSubPools;
|
---|
387 | /** Number of pages in pool. */
|
---|
388 | uint32_t cPages;
|
---|
389 | #ifdef VBOX_WITH_STATISTICS
|
---|
390 | /** Number of free pages in pool. */
|
---|
391 | uint32_t cFreePages;
|
---|
392 | /** Number of alloc calls. */
|
---|
393 | STAMCOUNTER cAllocCalls;
|
---|
394 | /** Number of free calls. */
|
---|
395 | STAMCOUNTER cFreeCalls;
|
---|
396 | /** Number of to phys conversions. */
|
---|
397 | STAMCOUNTER cToPhysCalls;
|
---|
398 | /** Number of to virtual conversions. */
|
---|
399 | STAMCOUNTER cToVirtCalls;
|
---|
400 | /** Number of real errors. */
|
---|
401 | STAMCOUNTER cErrors;
|
---|
402 | #endif
|
---|
403 | } MMPAGEPOOL;
|
---|
404 | /** Pointer to page pool. */
|
---|
405 | typedef MMPAGEPOOL *PMMPAGEPOOL;
|
---|
406 |
|
---|
407 | /**
|
---|
408 | * Lookup record for HC virtual memory in the page pool.
|
---|
409 | */
|
---|
410 | typedef struct MMPPLOOKUPHCPTR
|
---|
411 | {
|
---|
412 | /** The key is virtual address. */
|
---|
413 | AVLPVNODECORE Core;
|
---|
414 | /** Pointer to subpool if lookup record for a pool. */
|
---|
415 | struct MMPAGESUBPOOL *pSubPool;
|
---|
416 | } MMPPLOOKUPHCPTR;
|
---|
417 | /** Pointer to virtual memory lookup record. */
|
---|
418 | typedef MMPPLOOKUPHCPTR *PMMPPLOOKUPHCPTR;
|
---|
419 |
|
---|
420 | /**
|
---|
421 | * Lookup record for HC physical memory.
|
---|
422 | */
|
---|
423 | typedef struct MMPPLOOKUPHCPHYS
|
---|
424 | {
|
---|
425 | /** The key is physical address. */
|
---|
426 | AVLHCPHYSNODECORE Core;
|
---|
427 | /** Pointer to SUPPAGE record for this physical address. */
|
---|
428 | PSUPPAGE pPhysPage;
|
---|
429 | } MMPPLOOKUPHCPHYS;
|
---|
430 | /** Pointer to physical memory lookup record. */
|
---|
431 | typedef MMPPLOOKUPHCPHYS *PMMPPLOOKUPHCPHYS;
|
---|
432 |
|
---|
433 | /** @} */
|
---|
434 |
|
---|
435 |
|
---|
436 |
|
---|
437 | /**
|
---|
438 | * Type of memory that's locked.
|
---|
439 | */
|
---|
440 | typedef enum MMLOCKEDTYPE
|
---|
441 | {
|
---|
442 | /** Hypervisor memory (VMM GC). */
|
---|
443 | MM_LOCKED_TYPE_HYPER,
|
---|
444 | /** Hypervisor memory to be mapped from HC. */
|
---|
445 | MM_LOCKED_TYPE_HYPER_NOFREE,
|
---|
446 | /** Physical VM memory (RAM & MMIO2). */
|
---|
447 | MM_LOCKED_TYPE_PHYS
|
---|
448 | } MMLOCKEDTYPE;
|
---|
449 | /** Pointer to memory type. */
|
---|
450 | typedef MMLOCKEDTYPE *PMMLOCKEDTYPE;
|
---|
451 |
|
---|
452 |
|
---|
453 | /**
|
---|
454 | * Converts a SUPPAGE pointer to a MMLOCKEDMEM pointer.
|
---|
455 | * @returns Pointer to the MMLOCKEDMEM record the range is associated with.
|
---|
456 | * @param pSupPage Pointer to SUPPAGE structure managed by MM.
|
---|
457 | */
|
---|
458 | #define MM_SUPRANGE_TO_MMLOCKEDMEM(pSupPage) ((PMMLOCKEDMEM)pSupPage->uReserved)
|
---|
459 |
|
---|
460 |
|
---|
461 | /**
|
---|
462 | * Locked memory record.
|
---|
463 | */
|
---|
464 | typedef struct MMLOCKEDMEM
|
---|
465 | {
|
---|
466 | /** Address (host mapping). */
|
---|
467 | void *pv;
|
---|
468 | /** Size. */
|
---|
469 | size_t cb;
|
---|
470 | /** Next record. */
|
---|
471 | struct MMLOCKEDMEM *pNext;
|
---|
472 | /** Record type. */
|
---|
473 | MMLOCKEDTYPE eType;
|
---|
474 | /** Type specific data. */
|
---|
475 | union
|
---|
476 | {
|
---|
477 | /** Data for MM_LOCKED_TYPE_HYPER. */
|
---|
478 | struct
|
---|
479 | {
|
---|
480 | unsigned uNothing;
|
---|
481 | } hyper;
|
---|
482 |
|
---|
483 | /** Data for MM_LOCKED_TYPE_PHYS. */
|
---|
484 | struct
|
---|
485 | {
|
---|
486 | /** The GC physical address.
|
---|
487 | * (Assuming that this is a linear range of GC physical pages.)
|
---|
488 | */
|
---|
489 | RTGCPHYS GCPhys;
|
---|
490 | } phys;
|
---|
491 | } u;
|
---|
492 |
|
---|
493 | /** Physical Page Array. (Variable length.)
|
---|
494 | * The uReserved field contains pointer to the MMLOCKMEM record.
|
---|
495 | * Use the macro MM_SUPPAGE_TO_MMLOCKEDMEM() to convert.
|
---|
496 | *
|
---|
497 | * For MM_LOCKED_TYPE_PHYS the low 12 bits of the pvPhys member
|
---|
498 | * are bits (MM_RAM_FLAGS_*) and not part of the physical address.
|
---|
499 | */
|
---|
500 | SUPPAGE aPhysPages[1];
|
---|
501 | } MMLOCKEDMEM;
|
---|
502 | /** Pointer to locked memory. */
|
---|
503 | typedef MMLOCKEDMEM *PMMLOCKEDMEM;
|
---|
504 |
|
---|
505 |
|
---|
506 | /**
|
---|
507 | * Hypervisor memory mapping type.
|
---|
508 | */
|
---|
509 | typedef enum MMLOOKUPHYPERTYPE
|
---|
510 | {
|
---|
511 | /** Invalid record. This is used for record which are incomplete. */
|
---|
512 | MMLOOKUPHYPERTYPE_INVALID = 0,
|
---|
513 | /** Mapping of locked memory. */
|
---|
514 | MMLOOKUPHYPERTYPE_LOCKED,
|
---|
515 | /** Mapping of contiguous HC physical memory. */
|
---|
516 | MMLOOKUPHYPERTYPE_HCPHYS,
|
---|
517 | /** Mapping of contiguous GC physical memory. */
|
---|
518 | MMLOOKUPHYPERTYPE_GCPHYS,
|
---|
519 | /** Dynamic mapping area (MMR3HyperReserve).
|
---|
520 | * A conversion will require to check what's in the page table for the pages. */
|
---|
521 | MMLOOKUPHYPERTYPE_DYNAMIC
|
---|
522 | };
|
---|
523 |
|
---|
524 | /**
|
---|
525 | * Lookup record for the hypervisor memory area.
|
---|
526 | */
|
---|
527 | typedef struct MMLOOKUPHYPER
|
---|
528 | {
|
---|
529 | /** Byte offset from the start of this record to the next.
|
---|
530 | * If the value is NIL_OFFSET the chain is terminated. */
|
---|
531 | int32_t offNext;
|
---|
532 | /** Offset into the hypvervisor memory area. */
|
---|
533 | uint32_t off;
|
---|
534 | /** Size of this part. */
|
---|
535 | uint32_t cb;
|
---|
536 | /** Locking type. */
|
---|
537 | MMLOOKUPHYPERTYPE enmType;
|
---|
538 | /** Type specific data */
|
---|
539 | union
|
---|
540 | {
|
---|
541 | /** Locked memory. */
|
---|
542 | struct
|
---|
543 | {
|
---|
544 | /** Host context pointer. */
|
---|
545 | HCPTRTYPE(void *) pvHC;
|
---|
546 | /** Pointer to the locked mem record. */
|
---|
547 | HCPTRTYPE(PMMLOCKEDMEM) pLockedMem;
|
---|
548 | } Locked;
|
---|
549 |
|
---|
550 | /** Contiguous physical memory. */
|
---|
551 | struct
|
---|
552 | {
|
---|
553 | /** Host context pointer. */
|
---|
554 | HCPTRTYPE(void *) pvHC;
|
---|
555 | /** HC physical address corresponding to pvHC. */
|
---|
556 | RTHCPHYS HCPhys;
|
---|
557 | } HCPhys;
|
---|
558 | /** Contiguous guest physical memory. */
|
---|
559 | struct
|
---|
560 | {
|
---|
561 | /** HC physical address corresponding to pvHC. */
|
---|
562 | RTGCPHYS GCPhys;
|
---|
563 | } GCPhys;
|
---|
564 | } u;
|
---|
565 | /** Description. */
|
---|
566 | HCPTRTYPE(const char *) pszDesc;
|
---|
567 | } MMLOOKUPHYPER;
|
---|
568 | /** Pointer to a hypervisor memory lookup record. */
|
---|
569 | typedef MMLOOKUPHYPER *PMMLOOKUPHYPER;
|
---|
570 |
|
---|
571 |
|
---|
572 | /**
|
---|
573 | * Converts a MM pointer into a VM pointer.
|
---|
574 | * @returns Pointer to the VM structure the MM is part of.
|
---|
575 | * @param pMM Pointer to MM instance data.
|
---|
576 | */
|
---|
577 | #define MM2VM(pMM) ( (PVM)((char*)pMM - pMM->offVM) )
|
---|
578 |
|
---|
579 |
|
---|
580 | /**
|
---|
581 | * MM Data (part of VM)
|
---|
582 | */
|
---|
583 | typedef struct MM
|
---|
584 | {
|
---|
585 | /** Offset to the VM structure.
|
---|
586 | * See MM2VM(). */
|
---|
587 | RTINT offVM;
|
---|
588 |
|
---|
589 | /** Set if PGM has been initialized and we can safely call PGMR3Map(). */
|
---|
590 | bool fPGMInitialized;
|
---|
591 | #if GC_ARCH_BITS == 64 || HC_ARCH_BITS == 64
|
---|
592 | uint32_t u32Padding1; /**< alignment padding. */
|
---|
593 | #endif
|
---|
594 |
|
---|
595 | /** Lookup list for the Hypervisor Memory Area.
|
---|
596 | * The offset is relative to the start of the heap.
|
---|
597 | * Use pHyperHeapHC or pHyperHeapGC to calculate the address.
|
---|
598 | */
|
---|
599 | RTUINT offLookupHyper;
|
---|
600 |
|
---|
601 | /** The offset of the next static mapping in the Hypervisor Memory Area. */
|
---|
602 | RTUINT offHyperNextStatic;
|
---|
603 | /** The size of the HMA.
|
---|
604 | * Starts at 12MB and will be fixed late in the init process. */
|
---|
605 | RTUINT cbHyperArea;
|
---|
606 |
|
---|
607 | /** Guest address of the Hypervisor Memory Area. */
|
---|
608 | RTGCPTR pvHyperAreaGC;
|
---|
609 |
|
---|
610 | /** The hypervisor heap (GC Ptr). */
|
---|
611 | GCPTRTYPE(PMMHYPERHEAP) pHyperHeapGC;
|
---|
612 | /** The hypervisor heap (HC Ptr). */
|
---|
613 | HCPTRTYPE(PMMHYPERHEAP) pHyperHeapHC;
|
---|
614 |
|
---|
615 | /** List of memory locks. (HC only) */
|
---|
616 | HCPTRTYPE(PMMLOCKEDMEM) pLockedMem;
|
---|
617 |
|
---|
618 | /** Page pool. (HC only) */
|
---|
619 | HCPTRTYPE(PMMPAGEPOOL) pPagePool;
|
---|
620 | /** Page pool pages in low memory. (HC only) */
|
---|
621 | HCPTRTYPE(PMMPAGEPOOL) pPagePoolLow;
|
---|
622 |
|
---|
623 | /** Pointer to the dummy page.
|
---|
624 | * The dummy page is a paranoia thingy used for instance for pure MMIO RAM ranges
|
---|
625 | * to make sure any bugs will not harm whatever the system stores in the first
|
---|
626 | * physical page. */
|
---|
627 | HCPTRTYPE(void *) pvDummyPage;
|
---|
628 | /** Physical address of the dummy page. */
|
---|
629 | RTHCPHYS HCPhysDummyPage;
|
---|
630 |
|
---|
631 | /** Size of the currently allocated guest RAM.
|
---|
632 | * Mark that this is the actual size, not the end address. */
|
---|
633 | RTUINT cbRAMSize;
|
---|
634 | /** Size of the base RAM in bytes. */
|
---|
635 | RTUINT cbRamBase;
|
---|
636 | /** Pointer to the base RAM. */
|
---|
637 | HCPTRTYPE(void *) pvRamBaseHC;
|
---|
638 |
|
---|
639 | /** Pointer to the MM R3 Heap. */
|
---|
640 | HCPTRTYPE(PMMHEAP) pHeap;
|
---|
641 |
|
---|
642 | } MM;
|
---|
643 | /** Pointer to MM Data (part of VM). */
|
---|
644 | typedef MM *PMM;
|
---|
645 |
|
---|
646 | __BEGIN_DECLS
|
---|
647 |
|
---|
648 |
|
---|
649 | int mmr3PagePoolInit(PVM pVM);
|
---|
650 | void mmr3PagePoolTerm(PVM pVM);
|
---|
651 |
|
---|
652 | int mmr3HeapCreate(PVM pVM, PMMHEAP *ppHeap);
|
---|
653 | void mmr3HeapDestroy(PMMHEAP pHeap);
|
---|
654 |
|
---|
655 | int mmr3HyperInit(PVM pVM);
|
---|
656 | int mmR3HyperInitPaging(PVM pVM);
|
---|
657 |
|
---|
658 | int mmr3LockMem(PVM pVM, void *pv, size_t cb, MMLOCKEDTYPE eType, PMMLOCKEDMEM *ppLockedMem, bool fSilentFailure);
|
---|
659 | int mmr3MapLocked(PVM pVM, PMMLOCKEDMEM pLockedMem, RTGCPTR Addr, unsigned iPage, size_t cPages, unsigned fFlags);
|
---|
660 |
|
---|
661 | const char *mmR3GetTagName(MMTAG enmTag);
|
---|
662 |
|
---|
663 | /**
|
---|
664 | * Converts a pool address to a physical address.
|
---|
665 | * The specified allocation type must match with the address.
|
---|
666 | *
|
---|
667 | * @returns Physical address.
|
---|
668 | * @returns NIL_RTHCPHYS if not found or eType is not matching.
|
---|
669 | * @param pPool Pointer to the page pool.
|
---|
670 | * @param pv The address to convert.
|
---|
671 | * @thread The Emulation Thread.
|
---|
672 | */
|
---|
673 | MMDECL(RTHCPHYS) mmPagePoolPtr2Phys(PMMPAGEPOOL pPool, void *pv);
|
---|
674 |
|
---|
675 | /**
|
---|
676 | * Converts a pool physical address to a linear address.
|
---|
677 | * The specified allocation type must match with the address.
|
---|
678 | *
|
---|
679 | * @returns Physical address.
|
---|
680 | * @returns NULL if not found or eType is not matching.
|
---|
681 | * @param pPool Pointer to the page pool.
|
---|
682 | * @param HCPhys The address to convert.
|
---|
683 | * @thread The Emulation Thread.
|
---|
684 | */
|
---|
685 | MMDECL(void *) mmPagePoolPhys2Ptr(PMMPAGEPOOL pPool, RTHCPHYS HCPhys);
|
---|
686 |
|
---|
687 | __END_DECLS
|
---|
688 |
|
---|
689 | /** @} */
|
---|
690 |
|
---|
691 | #endif
|
---|