1 | /* $Id: MM.cpp 6830 2008-02-06 14:30:13Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MM - Memory Monitor(/Manager).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /** @page pg_mm MM - The Memory Monitor/Manager
|
---|
20 | *
|
---|
21 | * WARNING: THIS IS SOMEWHAT OUTDATED!
|
---|
22 | *
|
---|
23 | * It seems like this is going to be the entity taking care of memory allocations
|
---|
24 | * and the locking of physical memory for a VM. MM will track these allocations and
|
---|
25 | * pinnings so pointer conversions, memory read and write, and correct clean up can
|
---|
26 | * be done.
|
---|
27 | *
|
---|
28 | * Memory types:
|
---|
29 | * - Hypervisor Memory Area (HMA).
|
---|
30 | * - Page tables.
|
---|
31 | * - Physical pages.
|
---|
32 | *
|
---|
33 | * The first two types are not accessible using the generic conversion functions
|
---|
34 | * for GC memory, there are special functions for these.
|
---|
35 | *
|
---|
36 | *
|
---|
37 | * A decent structure for this component need to be eveloped as we see usage. One
|
---|
38 | * or two rewrites is probabaly needed to get it right...
|
---|
39 | *
|
---|
40 | *
|
---|
41 | *
|
---|
42 | * @section Hypervisor Memory Area
|
---|
43 | *
|
---|
44 | * The hypervisor is give 4MB of space inside the guest, we assume that we can
|
---|
45 | * steal an page directory entry from the guest OS without cause trouble. In
|
---|
46 | * addition to these 4MB we'll be mapping memory for the graphics emulation,
|
---|
47 | * but that will be an independant mapping.
|
---|
48 | *
|
---|
49 | * The 4MBs are divided into two main parts:
|
---|
50 | * -# The static code and data
|
---|
51 | * -# The shortlived page mappings.
|
---|
52 | *
|
---|
53 | * The first part is used for the VM structure, the core code (VMMSwitch),
|
---|
54 | * GC modules, and the alloc-only-heap. The size will be determined at a
|
---|
55 | * later point but initially we'll say 2MB of locked memory, most of which
|
---|
56 | * is non contiguous physically.
|
---|
57 | *
|
---|
58 | * The second part is used for mapping pages to the hypervisor. We'll be using
|
---|
59 | * a simple round robin when doing these mappings. This means that no-one can
|
---|
60 | * assume that a mapping hangs around for very long, while the managing of the
|
---|
61 | * pages are very simple.
|
---|
62 | *
|
---|
63 | *
|
---|
64 | *
|
---|
65 | * @section Page Pool
|
---|
66 | *
|
---|
67 | * The MM manages a per VM page pool from which other components can allocate
|
---|
68 | * locked, page aligned and page granular memory objects. The pool provides
|
---|
69 | * facilities to convert back and forth between physical and virtual addresses
|
---|
70 | * (within the pool of course). Several specialized interfaces are provided
|
---|
71 | * for the most common alloctions and convertions to save the caller from
|
---|
72 | * bothersome casting and extra parameter passing.
|
---|
73 | *
|
---|
74 | *
|
---|
75 | */
|
---|
76 |
|
---|
77 |
|
---|
78 |
|
---|
79 | /*******************************************************************************
|
---|
80 | * Header Files *
|
---|
81 | *******************************************************************************/
|
---|
82 | #define LOG_GROUP LOG_GROUP_MM
|
---|
83 | #include <VBox/mm.h>
|
---|
84 | #include <VBox/pgm.h>
|
---|
85 | #include <VBox/cfgm.h>
|
---|
86 | #include <VBox/ssm.h>
|
---|
87 | #include <VBox/gmm.h>
|
---|
88 | #include "MMInternal.h"
|
---|
89 | #include <VBox/vm.h>
|
---|
90 | #include <VBox/uvm.h>
|
---|
91 | #include <VBox/err.h>
|
---|
92 | #include <VBox/param.h>
|
---|
93 |
|
---|
94 | #include <VBox/log.h>
|
---|
95 | #include <iprt/alloc.h>
|
---|
96 | #include <iprt/assert.h>
|
---|
97 | #include <iprt/string.h>
|
---|
98 |
|
---|
99 |
|
---|
100 | /*******************************************************************************
|
---|
101 | * Defined Constants And Macros *
|
---|
102 | *******************************************************************************/
|
---|
103 | /** The current saved state versino of MM. */
|
---|
104 | #define MM_SAVED_STATE_VERSION 2
|
---|
105 |
|
---|
106 |
|
---|
107 | /*******************************************************************************
|
---|
108 | * Internal Functions *
|
---|
109 | *******************************************************************************/
|
---|
110 | static DECLCALLBACK(int) mmR3Save(PVM pVM, PSSMHANDLE pSSM);
|
---|
111 | static DECLCALLBACK(int) mmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version);
|
---|
112 |
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Initializes the MM members of the UVM.
|
---|
116 | *
|
---|
117 | * This is currently only the ring-3 heap.
|
---|
118 | *
|
---|
119 | * @returns VBox status code.
|
---|
120 | * @param pUVM Pointer to the user mode VM structure.
|
---|
121 | */
|
---|
122 | MMR3DECL(int) MMR3InitUVM(PUVM pUVM)
|
---|
123 | {
|
---|
124 | /*
|
---|
125 | * Assert sizes and order.
|
---|
126 | */
|
---|
127 | AssertCompile(sizeof(pUVM->mm.s) <= sizeof(pUVM->mm.padding));
|
---|
128 | AssertRelease(sizeof(pUVM->mm.s) <= sizeof(pUVM->mm.padding));
|
---|
129 | Assert(!pUVM->mm.s.pHeap);
|
---|
130 |
|
---|
131 | /*
|
---|
132 | * Init the heap.
|
---|
133 | */
|
---|
134 | return mmR3HeapCreateU(pUVM, &pUVM->mm.s.pHeap);
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Initializes the MM.
|
---|
140 | *
|
---|
141 | * MM is managing the virtual address space (among other things) and
|
---|
142 | * setup the hypvervisor memory area mapping in the VM structure and
|
---|
143 | * the hypvervisor alloc-only-heap. Assuming the current init order
|
---|
144 | * and components the hypvervisor memory area looks like this:
|
---|
145 | * -# VM Structure.
|
---|
146 | * -# Hypervisor alloc only heap (also call Hypervisor memory region).
|
---|
147 | * -# Core code.
|
---|
148 | *
|
---|
149 | * MM determins the virtual address of the hypvervisor memory area by
|
---|
150 | * checking for location at previous run. If that property isn't available
|
---|
151 | * it will choose a default starting location, currently 0xe0000000.
|
---|
152 | *
|
---|
153 | * @returns VBox status code.
|
---|
154 | * @param pVM The VM to operate on.
|
---|
155 | */
|
---|
156 | MMR3DECL(int) MMR3Init(PVM pVM)
|
---|
157 | {
|
---|
158 | LogFlow(("MMR3Init\n"));
|
---|
159 |
|
---|
160 | /*
|
---|
161 | * Assert alignment, sizes and order.
|
---|
162 | */
|
---|
163 | AssertRelease(!(RT_OFFSETOF(VM, mm.s) & 31));
|
---|
164 | AssertRelease(sizeof(pVM->mm.s) <= sizeof(pVM->mm.padding));
|
---|
165 | AssertMsg(pVM->mm.s.offVM == 0, ("Already initialized!\n"));
|
---|
166 |
|
---|
167 | /*
|
---|
168 | * Init the structure.
|
---|
169 | */
|
---|
170 | pVM->mm.s.offVM = RT_OFFSETOF(VM, mm);
|
---|
171 | pVM->mm.s.offLookupHyper = NIL_OFFSET;
|
---|
172 |
|
---|
173 | /*
|
---|
174 | * Init the page pool.
|
---|
175 | */
|
---|
176 | int rc = mmR3PagePoolInit(pVM);
|
---|
177 | if (VBOX_SUCCESS(rc))
|
---|
178 | {
|
---|
179 | /*
|
---|
180 | * Init the hypervisor related stuff.
|
---|
181 | */
|
---|
182 | rc = mmR3HyperInit(pVM);
|
---|
183 | if (VBOX_SUCCESS(rc))
|
---|
184 | {
|
---|
185 | /*
|
---|
186 | * Register the saved state data unit.
|
---|
187 | */
|
---|
188 | rc = SSMR3RegisterInternal(pVM, "mm", 1, MM_SAVED_STATE_VERSION, sizeof(uint32_t) * 2,
|
---|
189 | NULL, mmR3Save, NULL,
|
---|
190 | NULL, mmR3Load, NULL);
|
---|
191 | if (VBOX_SUCCESS(rc))
|
---|
192 | return rc;
|
---|
193 |
|
---|
194 | /* .... failure .... */
|
---|
195 | }
|
---|
196 | }
|
---|
197 | MMR3Term(pVM);
|
---|
198 | return rc;
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Initializes the MM parts which depends on PGM being initialized.
|
---|
204 | *
|
---|
205 | * @returns VBox status code.
|
---|
206 | * @param pVM The VM to operate on.
|
---|
207 | * @remark No cleanup necessary since MMR3Term() will be called on failure.
|
---|
208 | */
|
---|
209 | MMR3DECL(int) MMR3InitPaging(PVM pVM)
|
---|
210 | {
|
---|
211 | LogFlow(("MMR3InitPaging:\n"));
|
---|
212 |
|
---|
213 | /*
|
---|
214 | * Query the CFGM values.
|
---|
215 | */
|
---|
216 | int rc;
|
---|
217 | PCFGMNODE pMMCfg = CFGMR3GetChild(CFGMR3GetRoot(pVM), "MM");
|
---|
218 | if (pMMCfg)
|
---|
219 | {
|
---|
220 | rc = CFGMR3InsertNode(CFGMR3GetRoot(pVM), "MM", &pMMCfg);
|
---|
221 | AssertRCReturn(rc, rc);
|
---|
222 | }
|
---|
223 |
|
---|
224 | /** @cfgm{RamPreAlloc, boolean, false}
|
---|
225 | * Indicates whether the base RAM should all be allocated before starting
|
---|
226 | * the VM (default), or if it should be allocated when first written to.
|
---|
227 | */
|
---|
228 | bool fPreAlloc;
|
---|
229 | rc = CFGMR3QueryBool(CFGMR3GetRoot(pVM), "RamPreAlloc", &fPreAlloc);
|
---|
230 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
231 | fPreAlloc = false;
|
---|
232 | else
|
---|
233 | AssertMsgRCReturn(rc, ("Configuration error: Failed to query integer \"RamPreAlloc\", rc=%Vrc.\n", rc), rc);
|
---|
234 |
|
---|
235 | /** @cfgm{RamSize, uint64_t, 0, 0, UINT64_MAX}
|
---|
236 | * Specifies the size of the base RAM that is to be set up during
|
---|
237 | * VM initialization.
|
---|
238 | */
|
---|
239 | uint64_t cbRam;
|
---|
240 | rc = CFGMR3QueryU64(CFGMR3GetRoot(pVM), "RamSize", &cbRam);
|
---|
241 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
242 | cbRam = 0;
|
---|
243 | else
|
---|
244 | AssertMsgRCReturn(rc, ("Configuration error: Failed to query integer \"RamSize\", rc=%Vrc.\n", rc), rc);
|
---|
245 |
|
---|
246 | cbRam &= X86_PTE_PAE_PG_MASK;
|
---|
247 | pVM->mm.s.cbRamBase = cbRam; /* Warning: don't move this code to MMR3Init without fixing REMR3Init. */
|
---|
248 | Log(("MM: %RU64 bytes of RAM%s\n", cbRam, fPreAlloc ? " (PreAlloc)" : ""));
|
---|
249 |
|
---|
250 | /** @cfgm{MM/Policy, string, no overcommitment}
|
---|
251 | * Specifies the policy to use when reserving memory for this VM. The recognized
|
---|
252 | * value is 'no overcommitment' (default). See GMMPOLICY.
|
---|
253 | */
|
---|
254 | GMMOCPOLICY enmPolicy;
|
---|
255 | char sz[64];
|
---|
256 | rc = CFGMR3QueryString(CFGMR3GetRoot(pVM), "Policy", sz, sizeof(sz));
|
---|
257 | if (RT_SUCCESS(rc))
|
---|
258 | {
|
---|
259 | if ( !RTStrICmp(sz, "no_oc")
|
---|
260 | || !RTStrICmp(sz, "no overcommitment"))
|
---|
261 | enmPolicy = GMMOCPOLICY_NO_OC;
|
---|
262 | else
|
---|
263 | return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, "Unknown \"MM/Policy\" value \"%s\"", sz);
|
---|
264 | }
|
---|
265 | else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
266 | enmPolicy = GMMOCPOLICY_NO_OC;
|
---|
267 | else
|
---|
268 | AssertMsgRCReturn(rc, ("Configuration error: Failed to query string \"MM/Policy\", rc=%Vrc.\n", rc), rc);
|
---|
269 |
|
---|
270 | /** @cfgm{MM/Priority, string, normal}
|
---|
271 | * Specifies the memory priority of this VM. The priority comes into play when the
|
---|
272 | * system is overcommitted and the VMs needs to be milked for memory. The recognized
|
---|
273 | * values are 'low', 'normal' (default) and 'high'. See GMMPRIORITY.
|
---|
274 | */
|
---|
275 | GMMPRIORITY enmPriority;
|
---|
276 | rc = CFGMR3QueryString(CFGMR3GetRoot(pVM), "Priority", sz, sizeof(sz));
|
---|
277 | if (RT_SUCCESS(rc))
|
---|
278 | {
|
---|
279 | if (!RTStrICmp(sz, "low"))
|
---|
280 | enmPriority = GMMPRIORITY_LOW;
|
---|
281 | else if (!RTStrICmp(sz, "normal"))
|
---|
282 | enmPriority = GMMPRIORITY_NORMAL;
|
---|
283 | else if (!RTStrICmp(sz, "high"))
|
---|
284 | enmPriority = GMMPRIORITY_HIGH;
|
---|
285 | else
|
---|
286 | return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, "Unknown \"MM/Priority\" value \"%s\"", sz);
|
---|
287 | }
|
---|
288 | else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
289 | enmPriority = GMMPRIORITY_NORMAL;
|
---|
290 | else
|
---|
291 | AssertMsgRCReturn(rc, ("Configuration error: Failed to query string \"MM/Priority\", rc=%Vrc.\n", rc), rc);
|
---|
292 |
|
---|
293 | /*
|
---|
294 | * Make the initial memory reservation with GMM.
|
---|
295 | */
|
---|
296 | rc = GMMR3InitialReservation(pVM, cbRam >> PAGE_SHIFT, 1, 1, enmPolicy, enmPriority);
|
---|
297 | if (RT_FAILURE(rc))
|
---|
298 | {
|
---|
299 | if (rc == VERR_GMM_MEMORY_RESERVATION_DECLINED)
|
---|
300 | return VMSetError(pVM, rc, RT_SRC_POS,
|
---|
301 | N_("Insufficient free memory to start the VM (cbRam=%#RX64 enmPolicy=%d enmPriority=%d)"),
|
---|
302 | cbRam, enmPolicy, enmPriority);
|
---|
303 | return VMSetError(pVM, rc, RT_SRC_POS, "GMMR3InitialReservation(,%#RX64,0,0,%d,%d)",
|
---|
304 | cbRam >> PAGE_SHIFT, enmPolicy, enmPriority);
|
---|
305 | }
|
---|
306 |
|
---|
307 | /*
|
---|
308 | * If RamSize is 0 we're done now.
|
---|
309 | */
|
---|
310 | if (cbRam < PAGE_SIZE)
|
---|
311 | {
|
---|
312 | Log(("MM: No RAM configured\n"));
|
---|
313 | return VINF_SUCCESS;
|
---|
314 | }
|
---|
315 |
|
---|
316 | /*
|
---|
317 | * Setup the base ram (PGM).
|
---|
318 | */
|
---|
319 | rc = PGMR3PhysRegisterRam(pVM, 0, cbRam, "Base RAM");
|
---|
320 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
321 | if (RT_SUCCESS(rc) && fPreAlloc)
|
---|
322 | {
|
---|
323 | /** @todo RamPreAlloc should be handled at the very end of the VM creation. (lazy bird) */
|
---|
324 | return VM_SET_ERROR(pVM, VERR_NOT_IMPLEMENTED, "TODO: RamPreAlloc");
|
---|
325 | }
|
---|
326 | #else
|
---|
327 | if (RT_SUCCESS(rc))
|
---|
328 | {
|
---|
329 | /*
|
---|
330 | * Allocate the first chunk, as we'll map ROM ranges there.
|
---|
331 | * If requested, allocated the rest too.
|
---|
332 | */
|
---|
333 | rc = PGM3PhysGrowRange(pVM, (RTGCPHYS)0);
|
---|
334 | if (RT_SUCCESS(rc) && fPreAlloc)
|
---|
335 | for (RTGCPHYS GCPhys = PGM_DYNAMIC_CHUNK_SIZE;
|
---|
336 | GCPhys < cbRam && RT_SUCCESS(rc);
|
---|
337 | GCPhys += PGM_DYNAMIC_CHUNK_SIZE)
|
---|
338 | rc = PGM3PhysGrowRange(pVM, GCPhys);
|
---|
339 | }
|
---|
340 | #endif
|
---|
341 |
|
---|
342 | LogFlow(("MMR3InitPaging: returns %Vrc\n", rc));
|
---|
343 | return rc;
|
---|
344 | }
|
---|
345 |
|
---|
346 |
|
---|
347 | /**
|
---|
348 | * Terminates the MM.
|
---|
349 | *
|
---|
350 | * Termination means cleaning up and freeing all resources,
|
---|
351 | * the VM it self is at this point powered off or suspended.
|
---|
352 | *
|
---|
353 | * @returns VBox status code.
|
---|
354 | * @param pVM The VM to operate on.
|
---|
355 | */
|
---|
356 | MMR3DECL(int) MMR3Term(PVM pVM)
|
---|
357 | {
|
---|
358 | /*
|
---|
359 | * Destroy the page pool. (first as it used the hyper heap)
|
---|
360 | */
|
---|
361 | mmR3PagePoolTerm(pVM);
|
---|
362 |
|
---|
363 | /*
|
---|
364 | * Release locked memory.
|
---|
365 | * (Associated record are released by the heap.)
|
---|
366 | */
|
---|
367 | PMMLOCKEDMEM pLockedMem = pVM->mm.s.pLockedMem;
|
---|
368 | while (pLockedMem)
|
---|
369 | {
|
---|
370 | int rc = SUPPageUnlock(pLockedMem->pv);
|
---|
371 | AssertMsgRC(rc, ("SUPPageUnlock(%p) -> rc=%d\n", pLockedMem->pv, rc));
|
---|
372 | switch (pLockedMem->eType)
|
---|
373 | {
|
---|
374 | case MM_LOCKED_TYPE_HYPER:
|
---|
375 | rc = SUPPageFree(pLockedMem->pv, pLockedMem->cb >> PAGE_SHIFT);
|
---|
376 | AssertMsgRC(rc, ("SUPPageFree(%p) -> rc=%d\n", pLockedMem->pv, rc));
|
---|
377 | break;
|
---|
378 | case MM_LOCKED_TYPE_HYPER_NOFREE:
|
---|
379 | case MM_LOCKED_TYPE_HYPER_PAGES:
|
---|
380 | case MM_LOCKED_TYPE_PHYS:
|
---|
381 | /* nothing to do. */
|
---|
382 | break;
|
---|
383 | }
|
---|
384 | /* next */
|
---|
385 | pLockedMem = pLockedMem->pNext;
|
---|
386 | }
|
---|
387 |
|
---|
388 | /*
|
---|
389 | * Zero stuff to detect after termination use of the MM interface
|
---|
390 | */
|
---|
391 | pVM->mm.s.offLookupHyper = NIL_OFFSET;
|
---|
392 | pVM->mm.s.pLockedMem = NULL;
|
---|
393 | pVM->mm.s.pHyperHeapHC = NULL; /* freed above. */
|
---|
394 | pVM->mm.s.pHyperHeapGC = 0; /* freed above. */
|
---|
395 | pVM->mm.s.offVM = 0; /* init assertion on this */
|
---|
396 |
|
---|
397 | return 0;
|
---|
398 | }
|
---|
399 |
|
---|
400 |
|
---|
401 | /**
|
---|
402 | * Terminates the UVM part of MM.
|
---|
403 | *
|
---|
404 | * Termination means cleaning up and freeing all resources,
|
---|
405 | * the VM it self is at this point powered off or suspended.
|
---|
406 | *
|
---|
407 | * @returns VBox status code.
|
---|
408 | * @param pUVM Pointer to the user mode VM structure.
|
---|
409 | */
|
---|
410 | MMR3DECL(void) MMR3TermUVM(PUVM pUVM)
|
---|
411 | {
|
---|
412 | /*
|
---|
413 | * Destroy the heap.
|
---|
414 | */
|
---|
415 | mmR3HeapDestroy(pUVM->mm.s.pHeap);
|
---|
416 | pUVM->mm.s.pHeap = NULL;
|
---|
417 | }
|
---|
418 |
|
---|
419 |
|
---|
420 | /**
|
---|
421 | * Reset notification.
|
---|
422 | *
|
---|
423 | * MM will reload shadow ROMs into RAM at this point and make
|
---|
424 | * the ROM writable.
|
---|
425 | *
|
---|
426 | * @param pVM The VM handle.
|
---|
427 | */
|
---|
428 | MMR3DECL(void) MMR3Reset(PVM pVM)
|
---|
429 | {
|
---|
430 | mmR3PhysRomReset(pVM);
|
---|
431 | }
|
---|
432 |
|
---|
433 |
|
---|
434 | /**
|
---|
435 | * Execute state save operation.
|
---|
436 | *
|
---|
437 | * @returns VBox status code.
|
---|
438 | * @param pVM VM Handle.
|
---|
439 | * @param pSSM SSM operation handle.
|
---|
440 | */
|
---|
441 | static DECLCALLBACK(int) mmR3Save(PVM pVM, PSSMHANDLE pSSM)
|
---|
442 | {
|
---|
443 | LogFlow(("mmR3Save:\n"));
|
---|
444 |
|
---|
445 | /* (PGM saves the physical memory.) */
|
---|
446 | SSMR3PutU64(pSSM, pVM->mm.s.cBasePages);
|
---|
447 | return SSMR3PutU64(pSSM, pVM->mm.s.cbRamBase);
|
---|
448 | }
|
---|
449 |
|
---|
450 |
|
---|
451 | /**
|
---|
452 | * Execute state load operation.
|
---|
453 | *
|
---|
454 | * @returns VBox status code.
|
---|
455 | * @param pVM VM Handle.
|
---|
456 | * @param pSSM SSM operation handle.
|
---|
457 | * @param u32Version Data layout version.
|
---|
458 | */
|
---|
459 | static DECLCALLBACK(int) mmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version)
|
---|
460 | {
|
---|
461 | LogFlow(("mmR3Load:\n"));
|
---|
462 |
|
---|
463 | /*
|
---|
464 | * Validate version.
|
---|
465 | */
|
---|
466 | if ( SSM_VERSION_MAJOR_CHANGED(u32Version, MM_SAVED_STATE_VERSION)
|
---|
467 | || !u32Version)
|
---|
468 | {
|
---|
469 | Log(("mmR3Load: Invalid version u32Version=%d!\n", u32Version));
|
---|
470 | return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
|
---|
471 | }
|
---|
472 |
|
---|
473 | /*
|
---|
474 | * Check the cBasePages and cbRamBase values.
|
---|
475 | */
|
---|
476 | int rc;
|
---|
477 | RTUINT cb1;
|
---|
478 |
|
---|
479 | /* cBasePages */
|
---|
480 | uint64_t cPages;
|
---|
481 | if (u32Version != 1)
|
---|
482 | rc = SSMR3GetU64(pSSM, &cPages);
|
---|
483 | else
|
---|
484 | {
|
---|
485 | rc = SSMR3GetUInt(pSSM, &cb1);
|
---|
486 | cPages = cb1 >> PAGE_SHIFT;
|
---|
487 | }
|
---|
488 | if (VBOX_FAILURE(rc))
|
---|
489 | return rc;
|
---|
490 | if (cPages != pVM->mm.s.cBasePages)
|
---|
491 | {
|
---|
492 | Log(("mmR3Load: Memory configuration has changed. cPages=%#RX64 saved=%#RX64\n", pVM->mm.s.cBasePages, cPages));
|
---|
493 | return VERR_SSM_LOAD_MEMORY_SIZE_MISMATCH;
|
---|
494 | }
|
---|
495 |
|
---|
496 | /* cbRamBase */
|
---|
497 | uint64_t cb;
|
---|
498 | if (u32Version != 1)
|
---|
499 | rc = SSMR3GetU64(pSSM, &cb);
|
---|
500 | else
|
---|
501 | {
|
---|
502 | rc = SSMR3GetUInt(pSSM, &cb1);
|
---|
503 | cb = cb1;
|
---|
504 | }
|
---|
505 | if (VBOX_FAILURE(rc))
|
---|
506 | return rc;
|
---|
507 | if (cb != pVM->mm.s.cbRamBase)
|
---|
508 | {
|
---|
509 | Log(("mmR3Load: Memory configuration has changed. cbRamBase=%#RX64 save=%#RX64\n", pVM->mm.s.cbRamBase, cb));
|
---|
510 | return VERR_SSM_LOAD_MEMORY_SIZE_MISMATCH;
|
---|
511 | }
|
---|
512 |
|
---|
513 | /* (PGM restores the physical memory.) */
|
---|
514 | return rc;
|
---|
515 | }
|
---|
516 |
|
---|
517 |
|
---|
518 | /**
|
---|
519 | * Updates GMM with memory reservation changes.
|
---|
520 | *
|
---|
521 | * Called when MM::cbRamRegistered, MM::cShadowPages or MM::cFixedPages changes.
|
---|
522 | *
|
---|
523 | * @returns VBox status code - see GMMR0UpdateReservation.
|
---|
524 | * @param pVM The shared VM structure.
|
---|
525 | */
|
---|
526 | int mmR3UpdateReservation(PVM pVM)
|
---|
527 | {
|
---|
528 | if (pVM->mm.s.fDoneMMR3InitPaging)
|
---|
529 | return GMMR3UpdateReservation(pVM,
|
---|
530 | RT_MAX(pVM->mm.s.cBasePages, 1),
|
---|
531 | RT_MAX(pVM->mm.s.cShadowPages, 1),
|
---|
532 | RT_MAX(pVM->mm.s.cFixedPages, 1));
|
---|
533 | return VINF_SUCCESS;
|
---|
534 | }
|
---|
535 |
|
---|
536 |
|
---|
537 | /**
|
---|
538 | * Interface for PGM to increase the reservation of RAM and ROM pages.
|
---|
539 | *
|
---|
540 | * This can be called before MMR3InitPaging.
|
---|
541 | *
|
---|
542 | * @returns VBox status code.
|
---|
543 | * @param pVM The shared VM structure.
|
---|
544 | * @param cAddBasePages The number of pages to add.
|
---|
545 | */
|
---|
546 | MMR3DECL(int) MMR3IncreaseBaseReservation(PVM pVM, uint64_t cAddBasePages)
|
---|
547 | {
|
---|
548 | uint64_t cOld = pVM->mm.s.cBasePages;
|
---|
549 | pVM->mm.s.cBasePages += cAddBasePages;
|
---|
550 | LogFlow(("MMR3IncreaseBaseReservation: +%RU64 (%RU64 -> %RU64\n", cAddBasePages, cOld, pVM->mm.s.cBasePages));
|
---|
551 | int rc = mmR3UpdateReservation(pVM);
|
---|
552 | if (RT_FAILURE(rc))
|
---|
553 | {
|
---|
554 | VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserved physical memory for the RAM (%#RX64 -> %#RX64)"), cOld, pVM->mm.s.cBasePages);
|
---|
555 | pVM->mm.s.cBasePages = cOld;
|
---|
556 | }
|
---|
557 | return rc;
|
---|
558 | }
|
---|
559 |
|
---|
560 |
|
---|
561 | /**
|
---|
562 | * Interface for PGM to increase the reservation of fixed pages.
|
---|
563 | *
|
---|
564 | * This can be called before MMR3InitPaging.
|
---|
565 | *
|
---|
566 | * @returns VBox status code.
|
---|
567 | * @param pVM The shared VM structure.
|
---|
568 | * @param cAddFixedPages The number of pages to add.
|
---|
569 | */
|
---|
570 | MMR3DECL(int) MMR3IncreaseFixedReservation(PVM pVM, uint32_t cAddFixedPages)
|
---|
571 | {
|
---|
572 | const uint32_t cOld = pVM->mm.s.cFixedPages;
|
---|
573 | pVM->mm.s.cFixedPages += cAddFixedPages;
|
---|
574 | LogFlow(("MMR3AddFixedReservation: +%u (%u -> %u)\n", cAddFixedPages, cOld, pVM->mm.s.cFixedPages));
|
---|
575 | int rc = mmR3UpdateReservation(pVM);
|
---|
576 | if (RT_FAILURE(rc))
|
---|
577 | {
|
---|
578 | VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserve physical memory (%#x -> %#x)"), cOld, pVM->mm.s.cFixedPages);
|
---|
579 | pVM->mm.s.cFixedPages = cOld;
|
---|
580 | }
|
---|
581 | return rc;
|
---|
582 | }
|
---|
583 |
|
---|
584 |
|
---|
585 | /**
|
---|
586 | * Interface for PGM to update the reservation of shadow pages.
|
---|
587 | *
|
---|
588 | * This can be called before MMR3InitPaging.
|
---|
589 | *
|
---|
590 | * @returns VBox status code.
|
---|
591 | * @param pVM The shared VM structure.
|
---|
592 | * @param cShadowPages The new page count.
|
---|
593 | */
|
---|
594 | MMR3DECL(int) MMR3UpdateShadowReservation(PVM pVM, uint32_t cShadowPages)
|
---|
595 | {
|
---|
596 | const uint32_t cOld = pVM->mm.s.cShadowPages;
|
---|
597 | pVM->mm.s.cShadowPages = cShadowPages;
|
---|
598 | LogFlow(("MMR3UpdateShadowReservation: %u -> %u\n", cOld, pVM->mm.s.cShadowPages));
|
---|
599 | int rc = mmR3UpdateReservation(pVM);
|
---|
600 | if (RT_FAILURE(rc))
|
---|
601 | {
|
---|
602 | VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserve physical memory for shadow page tables (%#x -> %#x)"), cOld, pVM->mm.s.cShadowPages);
|
---|
603 | pVM->mm.s.cShadowPages = cOld;
|
---|
604 | }
|
---|
605 | return rc;
|
---|
606 | }
|
---|
607 |
|
---|
608 |
|
---|
609 | /**
|
---|
610 | * Locks physical memory which backs a virtual memory range (HC) adding
|
---|
611 | * the required records to the pLockedMem list.
|
---|
612 | *
|
---|
613 | * @returns VBox status code.
|
---|
614 | * @param pVM The VM handle.
|
---|
615 | * @param pv Pointer to memory range which shall be locked down.
|
---|
616 | * This pointer is page aligned.
|
---|
617 | * @param cb Size of memory range (in bytes). This size is page aligned.
|
---|
618 | * @param eType Memory type.
|
---|
619 | * @param ppLockedMem Where to store the pointer to the created locked memory record.
|
---|
620 | * This is optional, pass NULL if not used.
|
---|
621 | * @param fSilentFailure Don't raise an error when unsuccessful. Upper layer with deal with it.
|
---|
622 | */
|
---|
623 | int mmR3LockMem(PVM pVM, void *pv, size_t cb, MMLOCKEDTYPE eType, PMMLOCKEDMEM *ppLockedMem, bool fSilentFailure)
|
---|
624 | {
|
---|
625 | Assert(RT_ALIGN_P(pv, PAGE_SIZE) == pv);
|
---|
626 | Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb);
|
---|
627 |
|
---|
628 | if (ppLockedMem)
|
---|
629 | *ppLockedMem = NULL;
|
---|
630 |
|
---|
631 | /*
|
---|
632 | * Allocate locked mem structure.
|
---|
633 | */
|
---|
634 | unsigned cPages = cb >> PAGE_SHIFT;
|
---|
635 | AssertReturn(cPages == (cb >> PAGE_SHIFT), VERR_OUT_OF_RANGE);
|
---|
636 | PMMLOCKEDMEM pLockedMem = (PMMLOCKEDMEM)MMR3HeapAlloc(pVM, MM_TAG_MM, RT_OFFSETOF(MMLOCKEDMEM, aPhysPages[cPages]));
|
---|
637 | if (!pLockedMem)
|
---|
638 | return VERR_NO_MEMORY;
|
---|
639 | pLockedMem->pv = pv;
|
---|
640 | pLockedMem->cb = cb;
|
---|
641 | pLockedMem->eType = eType;
|
---|
642 | memset(&pLockedMem->u, 0, sizeof(pLockedMem->u));
|
---|
643 |
|
---|
644 | /*
|
---|
645 | * Lock the memory.
|
---|
646 | */
|
---|
647 | int rc = SUPPageLock(pv, cPages, &pLockedMem->aPhysPages[0]);
|
---|
648 | if (VBOX_SUCCESS(rc))
|
---|
649 | {
|
---|
650 | /*
|
---|
651 | * Setup the reserved field.
|
---|
652 | */
|
---|
653 | PSUPPAGE pPhysPage = &pLockedMem->aPhysPages[0];
|
---|
654 | for (unsigned c = cPages; c > 0; c--, pPhysPage++)
|
---|
655 | pPhysPage->uReserved = (RTHCUINTPTR)pLockedMem;
|
---|
656 |
|
---|
657 | /*
|
---|
658 | * Insert into the list.
|
---|
659 | *
|
---|
660 | * ASSUME no protected needed here as only one thread in the system can possibly
|
---|
661 | * be doing this. No other threads will walk this list either we assume.
|
---|
662 | */
|
---|
663 | pLockedMem->pNext = pVM->mm.s.pLockedMem;
|
---|
664 | pVM->mm.s.pLockedMem = pLockedMem;
|
---|
665 | /* Set return value. */
|
---|
666 | if (ppLockedMem)
|
---|
667 | *ppLockedMem = pLockedMem;
|
---|
668 | }
|
---|
669 | else
|
---|
670 | {
|
---|
671 | AssertMsgFailed(("SUPPageLock failed with rc=%d\n", rc));
|
---|
672 | MMR3HeapFree(pLockedMem);
|
---|
673 | if (!fSilentFailure)
|
---|
674 | rc = VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to lock %d bytes of host memory (out of memory)"), cb);
|
---|
675 | }
|
---|
676 |
|
---|
677 | return rc;
|
---|
678 | }
|
---|
679 |
|
---|
680 |
|
---|
681 | /**
|
---|
682 | * Maps a part of or an entire locked memory region into the guest context.
|
---|
683 | *
|
---|
684 | * @returns VBox status.
|
---|
685 | * God knows what happens if we fail...
|
---|
686 | * @param pVM VM handle.
|
---|
687 | * @param pLockedMem Locked memory structure.
|
---|
688 | * @param Addr GC Address where to start the mapping.
|
---|
689 | * @param iPage Page number in the locked memory region.
|
---|
690 | * @param cPages Number of pages to map.
|
---|
691 | * @param fFlags See the fFlags argument of PGR3Map().
|
---|
692 | */
|
---|
693 | int mmR3MapLocked(PVM pVM, PMMLOCKEDMEM pLockedMem, RTGCPTR Addr, unsigned iPage, size_t cPages, unsigned fFlags)
|
---|
694 | {
|
---|
695 | /*
|
---|
696 | * Adjust ~0 argument
|
---|
697 | */
|
---|
698 | if (cPages == ~(size_t)0)
|
---|
699 | cPages = (pLockedMem->cb >> PAGE_SHIFT) - iPage;
|
---|
700 | Assert(cPages != ~0U);
|
---|
701 | /* no incorrect arguments are accepted */
|
---|
702 | Assert(RT_ALIGN_GCPT(Addr, PAGE_SIZE, RTGCPTR) == Addr);
|
---|
703 | AssertMsg(iPage < (pLockedMem->cb >> PAGE_SHIFT), ("never even think about giving me a bad iPage(=%d)\n", iPage));
|
---|
704 | AssertMsg(iPage + cPages <= (pLockedMem->cb >> PAGE_SHIFT), ("never even think about giving me a bad cPages(=%d)\n", cPages));
|
---|
705 |
|
---|
706 | /*
|
---|
707 | * Map the the pages.
|
---|
708 | */
|
---|
709 | PSUPPAGE pPhysPage = &pLockedMem->aPhysPages[iPage];
|
---|
710 | while (cPages)
|
---|
711 | {
|
---|
712 | RTHCPHYS HCPhys = pPhysPage->Phys;
|
---|
713 | int rc = PGMMap(pVM, Addr, HCPhys, PAGE_SIZE, fFlags);
|
---|
714 | if (VBOX_FAILURE(rc))
|
---|
715 | {
|
---|
716 | /** @todo how the hell can we do a proper bailout here. */
|
---|
717 | return rc;
|
---|
718 | }
|
---|
719 |
|
---|
720 | /* next */
|
---|
721 | cPages--;
|
---|
722 | iPage++;
|
---|
723 | pPhysPage++;
|
---|
724 | Addr += PAGE_SIZE;
|
---|
725 | }
|
---|
726 |
|
---|
727 | return VINF_SUCCESS;
|
---|
728 | }
|
---|
729 |
|
---|
730 |
|
---|
731 | /**
|
---|
732 | * Convert HC Physical address to HC Virtual address.
|
---|
733 | *
|
---|
734 | * @returns VBox status.
|
---|
735 | * @param pVM VM handle.
|
---|
736 | * @param HCPhys The host context virtual address.
|
---|
737 | * @param ppv Where to store the resulting address.
|
---|
738 | * @thread The Emulation Thread.
|
---|
739 | */
|
---|
740 | MMR3DECL(int) MMR3HCPhys2HCVirt(PVM pVM, RTHCPHYS HCPhys, void **ppv)
|
---|
741 | {
|
---|
742 | /*
|
---|
743 | * Try page tables.
|
---|
744 | */
|
---|
745 | int rc = MMPagePhys2PageTry(pVM, HCPhys, ppv);
|
---|
746 | if (VBOX_SUCCESS(rc))
|
---|
747 | return rc;
|
---|
748 |
|
---|
749 | /*
|
---|
750 | * Iterate the locked memory - very slow.
|
---|
751 | */
|
---|
752 | uint32_t off = HCPhys & PAGE_OFFSET_MASK;
|
---|
753 | HCPhys &= X86_PTE_PAE_PG_MASK;
|
---|
754 | for (PMMLOCKEDMEM pCur = pVM->mm.s.pLockedMem; pCur; pCur = pCur->pNext)
|
---|
755 | {
|
---|
756 | size_t iPage = pCur->cb >> PAGE_SHIFT;
|
---|
757 | while (iPage-- > 0)
|
---|
758 | if ((pCur->aPhysPages[iPage].Phys & X86_PTE_PAE_PG_MASK) == HCPhys)
|
---|
759 | {
|
---|
760 | *ppv = (char *)pCur->pv + (iPage << PAGE_SHIFT) + off;
|
---|
761 | return VINF_SUCCESS;
|
---|
762 | }
|
---|
763 | }
|
---|
764 | /* give up */
|
---|
765 | return VERR_INVALID_POINTER;
|
---|
766 | }
|
---|
767 |
|
---|
768 |
|
---|
769 | /**
|
---|
770 | * Read memory from GC virtual address using the current guest CR3.
|
---|
771 | *
|
---|
772 | * @returns VBox status.
|
---|
773 | * @param pVM VM handle.
|
---|
774 | * @param pvDst Destination address (HC of course).
|
---|
775 | * @param GCPtr GC virtual address.
|
---|
776 | * @param cb Number of bytes to read.
|
---|
777 | */
|
---|
778 | MMR3DECL(int) MMR3ReadGCVirt(PVM pVM, void *pvDst, RTGCPTR GCPtr, size_t cb)
|
---|
779 | {
|
---|
780 | if (GCPtr - pVM->mm.s.pvHyperAreaGC < pVM->mm.s.cbHyperArea)
|
---|
781 | return MMR3HyperReadGCVirt(pVM, pvDst, GCPtr, cb);
|
---|
782 | return PGMPhysReadGCPtr(pVM, pvDst, GCPtr, cb);
|
---|
783 | }
|
---|
784 |
|
---|
785 |
|
---|
786 | /**
|
---|
787 | * Write to memory at GC virtual address translated using the current guest CR3.
|
---|
788 | *
|
---|
789 | * @returns VBox status.
|
---|
790 | * @param pVM VM handle.
|
---|
791 | * @param GCPtrDst GC virtual address.
|
---|
792 | * @param pvSrc The source address (HC of course).
|
---|
793 | * @param cb Number of bytes to read.
|
---|
794 | */
|
---|
795 | MMR3DECL(int) MMR3WriteGCVirt(PVM pVM, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
|
---|
796 | {
|
---|
797 | if (GCPtrDst - pVM->mm.s.pvHyperAreaGC < pVM->mm.s.cbHyperArea)
|
---|
798 | return VERR_ACCESS_DENIED;
|
---|
799 | return PGMPhysWriteGCPtr(pVM, GCPtrDst, pvSrc, cb);
|
---|
800 | }
|
---|
801 |
|
---|