VirtualBox

source: vbox/trunk/src/VBox/VMM/MM.cpp@ 22480

Last change on this file since 22480 was 22480, checked in by vboxsync, 15 years ago

SSM,VMM,Devices,Main,VBoxBFE: Live snapshot/migration SSM API adjustments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 27.9 KB
Line 
1/* $Id: MM.cpp 22480 2009-08-26 17:14:13Z vboxsync $ */
2/** @file
3 * MM - Memory Manager.
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
23/** @page pg_mm MM - The Memory Manager
24 *
25 * The memory manager is in charge of the following memory:
26 * - Hypervisor Memory Area (HMA) - Address space management.
27 * - Hypervisor Heap - A memory heap that lives in all contexts.
28 * - Tagged ring-3 heap.
29 * - Page pools - Primarily used by PGM for shadow page tables.
30 * - Locked process memory - Guest RAM and other. (reduce/obsolete this)
31 * - Physical guest memory (RAM & ROM) - Moving to PGM. (obsolete this)
32 *
33 * The global memory manager (GMM) is the global counter part / partner of MM.
34 * MM will provide therefore ring-3 callable interfaces for some of the GMM APIs
35 * related to resource tracking (PGM is the user).
36 *
37 * @see grp_mm
38 *
39 *
40 * @section sec_mm_hma Hypervisor Memory Area
41 *
42 * The HMA is used when executing in raw-mode. We borrow, with the help of
43 * PGMMap, some unused space (one or more page directory entries to be precise)
44 * in the guest's virtual memory context. PGM will monitor the guest's virtual
45 * address space for changes and relocate the HMA when required.
46 *
47 * To give some idea what's in the HMA, study the 'info hma' output:
48 * @verbatim
49VBoxDbg> info hma
50Hypervisor Memory Area (HMA) Layout: Base 00000000a0000000, 0x00800000 bytes
5100000000a05cc000-00000000a05cd000 DYNAMIC fence
5200000000a05c4000-00000000a05cc000 DYNAMIC Dynamic mapping
5300000000a05c3000-00000000a05c4000 DYNAMIC fence
5400000000a05b8000-00000000a05c3000 DYNAMIC Paging
5500000000a05b6000-00000000a05b8000 MMIO2 0000000000000000 PCNetShMem
5600000000a0536000-00000000a05b6000 MMIO2 0000000000000000 VGA VRam
5700000000a0523000-00000000a0536000 00002aaab3d0c000 LOCKED autofree alloc once (PDM_DEVICE)
5800000000a0522000-00000000a0523000 DYNAMIC fence
5900000000a051e000-00000000a0522000 00002aaab36f5000 LOCKED autofree VBoxDD2GC.gc
6000000000a051d000-00000000a051e000 DYNAMIC fence
6100000000a04eb000-00000000a051d000 00002aaab36c3000 LOCKED autofree VBoxDDGC.gc
6200000000a04ea000-00000000a04eb000 DYNAMIC fence
6300000000a04e9000-00000000a04ea000 00002aaab36c2000 LOCKED autofree ram range (High ROM Region)
6400000000a04e8000-00000000a04e9000 DYNAMIC fence
6500000000a040e000-00000000a04e8000 00002aaab2e6d000 LOCKED autofree VMMGC.gc
6600000000a0208000-00000000a040e000 00002aaab2c67000 LOCKED autofree alloc once (PATM)
6700000000a01f7000-00000000a0208000 00002aaaab92d000 LOCKED autofree alloc once (SELM)
6800000000a01e7000-00000000a01f7000 00002aaaab5e8000 LOCKED autofree alloc once (SELM)
6900000000a01e6000-00000000a01e7000 DYNAMIC fence
7000000000a01e5000-00000000a01e6000 00002aaaab5e7000 HCPHYS 00000000c363c000 Core Code
7100000000a01e4000-00000000a01e5000 DYNAMIC fence
7200000000a01e3000-00000000a01e4000 00002aaaaab26000 HCPHYS 00000000619cf000 GIP
7300000000a01a2000-00000000a01e3000 00002aaaabf32000 LOCKED autofree alloc once (PGM_PHYS)
7400000000a016b000-00000000a01a2000 00002aaab233f000 LOCKED autofree alloc once (PGM_POOL)
7500000000a016a000-00000000a016b000 DYNAMIC fence
7600000000a0165000-00000000a016a000 DYNAMIC CR3 mapping
7700000000a0164000-00000000a0165000 DYNAMIC fence
7800000000a0024000-00000000a0164000 00002aaab215f000 LOCKED autofree Heap
7900000000a0023000-00000000a0024000 DYNAMIC fence
8000000000a0001000-00000000a0023000 00002aaab1d24000 LOCKED pages VM
8100000000a0000000-00000000a0001000 DYNAMIC fence
82 @endverbatim
83 *
84 *
85 * @section sec_mm_hyperheap Hypervisor Heap
86 *
87 * The heap is accessible from ring-3, ring-0 and the raw-mode context. That
88 * said, it's not necessarily mapped into ring-0 on if that's possible since we
89 * don't wish to waste kernel address space without a good reason.
90 *
91 * Allocations within the heap are always in the same relative position in all
92 * contexts, so, it's possible to use offset based linking. In fact, the heap is
93 * internally using offset based linked lists tracking heap blocks. We use
94 * offset linked AVL trees and lists in a lot of places where share structures
95 * between RC, R3 and R0, so this is a strict requirement of the heap. However
96 * this means that we cannot easily extend the heap since the extension won't
97 * necessarily be in the continuation of the current heap memory in all (or any)
98 * context.
99 *
100 * All allocations are tagged. Per tag allocation statistics will be maintaing
101 * and exposed thru STAM when VBOX_WITH_STATISTICS is defined.
102 *
103 *
104 * @section sec_mm_r3heap Tagged Ring-3 Heap
105 *
106 * The ring-3 heap is a wrapper around the RTMem API adding allocation
107 * statistics and automatic cleanup on VM destruction.
108 *
109 * Per tag allocation statistics will be maintaing and exposed thru STAM when
110 * VBOX_WITH_STATISTICS is defined.
111 *
112 *
113 * @section sec_mm_page Page Pool
114 *
115 * The MM manages a page pool from which other components can allocate locked,
116 * page aligned and page sized memory objects. The pool provides facilities to
117 * convert back and forth between (host) physical and virtual addresses (within
118 * the pool of course). Several specialized interfaces are provided for the most
119 * common alloctions and convertions to save the caller from bothersome casting
120 * and extra parameter passing.
121 *
122 *
123 * @section sec_mm_locked Locked Process Memory
124 *
125 * MM manages the locked process memory. This is used for a bunch of things
126 * (count the LOCKED entries in the'info hma' output found in @ref sec_mm_hma),
127 * but the main consumer of memory is currently for guest RAM. There is an
128 * ongoing rewrite that will move all the guest RAM allocation to PGM and
129 * GMM.
130 *
131 * The locking of memory is something doing in cooperation with the VirtualBox
132 * support driver, SUPDrv (aka. VBoxDrv), thru the support library API,
133 * SUPR3 (aka. SUPLib).
134 *
135 *
136 * @section sec_mm_phys Physical Guest Memory
137 *
138 * MM is currently managing the physical memory for the guest. It relies heavily
139 * on PGM for this. There is an ongoing rewrite that will move this to PGM. (The
140 * rewrite is driven by the need for more flexible guest ram allocation, but
141 * also motivated by the fact that MMPhys is just adding stupid bureaucracy and
142 * that MMR3PhysReserve is a totally weird artifact that must go away.)
143 *
144 */
145
146
147/*******************************************************************************
148* Header Files *
149*******************************************************************************/
150#define LOG_GROUP LOG_GROUP_MM
151#include <VBox/mm.h>
152#include <VBox/pgm.h>
153#include <VBox/cfgm.h>
154#include <VBox/ssm.h>
155#include <VBox/gmm.h>
156#include "MMInternal.h"
157#include <VBox/vm.h>
158#include <VBox/uvm.h>
159#include <VBox/err.h>
160#include <VBox/param.h>
161
162#include <VBox/log.h>
163#include <iprt/alloc.h>
164#include <iprt/assert.h>
165#include <iprt/string.h>
166
167
168/*******************************************************************************
169* Defined Constants And Macros *
170*******************************************************************************/
171/** The current saved state versino of MM. */
172#define MM_SAVED_STATE_VERSION 2
173
174
175/*******************************************************************************
176* Internal Functions *
177*******************************************************************************/
178static DECLCALLBACK(int) mmR3Save(PVM pVM, PSSMHANDLE pSSM);
179static DECLCALLBACK(int) mmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPhase);
180
181
182
183
184/**
185 * Initializes the MM members of the UVM.
186 *
187 * This is currently only the ring-3 heap.
188 *
189 * @returns VBox status code.
190 * @param pUVM Pointer to the user mode VM structure.
191 */
192VMMR3DECL(int) MMR3InitUVM(PUVM pUVM)
193{
194 /*
195 * Assert sizes and order.
196 */
197 AssertCompile(sizeof(pUVM->mm.s) <= sizeof(pUVM->mm.padding));
198 AssertRelease(sizeof(pUVM->mm.s) <= sizeof(pUVM->mm.padding));
199 Assert(!pUVM->mm.s.pHeap);
200
201 /*
202 * Init the heap.
203 */
204 int rc = mmR3HeapCreateU(pUVM, &pUVM->mm.s.pHeap);
205 if (RT_SUCCESS(rc))
206 {
207 rc = mmR3UkHeapCreateU(pUVM, &pUVM->mm.s.pUkHeap);
208 if (RT_SUCCESS(rc))
209 return VINF_SUCCESS;
210 mmR3HeapDestroy(pUVM->mm.s.pHeap);
211 pUVM->mm.s.pHeap = NULL;
212 }
213 return rc;
214}
215
216
217/**
218 * Initializes the MM.
219 *
220 * MM is managing the virtual address space (among other things) and
221 * setup the hypvervisor memory area mapping in the VM structure and
222 * the hypvervisor alloc-only-heap. Assuming the current init order
223 * and components the hypvervisor memory area looks like this:
224 * -# VM Structure.
225 * -# Hypervisor alloc only heap (also call Hypervisor memory region).
226 * -# Core code.
227 *
228 * MM determins the virtual address of the hypvervisor memory area by
229 * checking for location at previous run. If that property isn't available
230 * it will choose a default starting location, currently 0xa0000000.
231 *
232 * @returns VBox status code.
233 * @param pVM The VM to operate on.
234 */
235VMMR3DECL(int) MMR3Init(PVM pVM)
236{
237 LogFlow(("MMR3Init\n"));
238
239 /*
240 * Assert alignment, sizes and order.
241 */
242 AssertRelease(!(RT_OFFSETOF(VM, mm.s) & 31));
243 AssertRelease(sizeof(pVM->mm.s) <= sizeof(pVM->mm.padding));
244 AssertMsg(pVM->mm.s.offVM == 0, ("Already initialized!\n"));
245
246 /*
247 * Init the structure.
248 */
249 pVM->mm.s.offVM = RT_OFFSETOF(VM, mm);
250 pVM->mm.s.offLookupHyper = NIL_OFFSET;
251
252 /*
253 * Init the page pool.
254 */
255 int rc = mmR3PagePoolInit(pVM);
256 if (RT_SUCCESS(rc))
257 {
258 /*
259 * Init the hypervisor related stuff.
260 */
261 rc = mmR3HyperInit(pVM);
262 if (RT_SUCCESS(rc))
263 {
264 /*
265 * Register the saved state data unit.
266 */
267 rc = SSMR3RegisterInternal(pVM, "mm", 1, MM_SAVED_STATE_VERSION, sizeof(uint32_t) * 2,
268 NULL, NULL, NULL,
269 NULL, mmR3Save, NULL,
270 NULL, mmR3Load, NULL);
271 if (RT_SUCCESS(rc))
272 return rc;
273
274 /* .... failure .... */
275 }
276 }
277 MMR3Term(pVM);
278 return rc;
279}
280
281
282/**
283 * Initializes the MM parts which depends on PGM being initialized.
284 *
285 * @returns VBox status code.
286 * @param pVM The VM to operate on.
287 * @remark No cleanup necessary since MMR3Term() will be called on failure.
288 */
289VMMR3DECL(int) MMR3InitPaging(PVM pVM)
290{
291 LogFlow(("MMR3InitPaging:\n"));
292
293 /*
294 * Query the CFGM values.
295 */
296 int rc;
297 PCFGMNODE pMMCfg = CFGMR3GetChild(CFGMR3GetRoot(pVM), "MM");
298 if (!pMMCfg)
299 {
300 rc = CFGMR3InsertNode(CFGMR3GetRoot(pVM), "MM", &pMMCfg);
301 AssertRCReturn(rc, rc);
302 }
303
304 /** @cfgm{RamSize, uint64_t, 0, 16TB, 0}
305 * Specifies the size of the base RAM that is to be set up during
306 * VM initialization.
307 */
308 uint64_t cbRam;
309 rc = CFGMR3QueryU64(CFGMR3GetRoot(pVM), "RamSize", &cbRam);
310 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
311 cbRam = 0;
312 else
313 AssertMsgRCReturn(rc, ("Configuration error: Failed to query integer \"RamSize\", rc=%Rrc.\n", rc), rc);
314 AssertLogRelMsg(!(cbRam & ~X86_PTE_PAE_PG_MASK), ("%RGp X86_PTE_PAE_PG_MASK=%RX64\n", cbRam, X86_PTE_PAE_PG_MASK));
315 AssertLogRelMsgReturn(cbRam <= GMM_GCPHYS_LAST, ("cbRam=%RGp GMM_GCPHYS_LAST=%RX64\n", cbRam, GMM_GCPHYS_LAST), VERR_OUT_OF_RANGE);
316 cbRam &= X86_PTE_PAE_PG_MASK;
317 pVM->mm.s.cbRamBase = cbRam;
318
319 /** @cfgm{RamHoleSize, uint32_t, 0, 4032MB, 512MB}
320 * Specifies the size of the memory hole. The memory hole is used
321 * to avoid mapping RAM to the range normally used for PCI memory regions.
322 * Must be aligned on a 4MB boundrary. */
323 uint32_t cbRamHole;
324 rc = CFGMR3QueryU32Def(CFGMR3GetRoot(pVM), "RamHoleSize", &cbRamHole, MM_RAM_HOLE_SIZE_DEFAULT);
325 AssertLogRelMsgRCReturn(rc, ("Configuration error: Failed to query integer \"RamHoleSize\", rc=%Rrc.\n", rc), rc);
326 AssertLogRelMsgReturn(cbRamHole <= 4032U * _1M,
327 ("Configuration error: \"RamHoleSize\"=%#RX32 is too large.\n", cbRamHole), VERR_OUT_OF_RANGE);
328 AssertLogRelMsgReturn(cbRamHole > 16 * _1M,
329 ("Configuration error: \"RamHoleSize\"=%#RX32 is too large.\n", cbRamHole), VERR_OUT_OF_RANGE);
330 AssertLogRelMsgReturn(!(cbRamHole & (_4M - 1)),
331 ("Configuration error: \"RamHoleSize\"=%#RX32 is misaligned.\n", cbRamHole), VERR_OUT_OF_RANGE);
332 uint64_t const offRamHole = _4G - cbRamHole;
333 if (cbRam < offRamHole)
334 Log(("MM: %RU64 bytes of RAM\n", cbRam));
335 else
336 Log(("MM: %RU64 bytes of RAM with a hole at %RU64 up to 4GB.\n", cbRam, offRamHole));
337
338 /** @cfgm{MM/Policy, string, no overcommitment}
339 * Specifies the policy to use when reserving memory for this VM. The recognized
340 * value is 'no overcommitment' (default). See GMMPOLICY.
341 */
342 GMMOCPOLICY enmOcPolicy;
343 char sz[64];
344 rc = CFGMR3QueryString(CFGMR3GetRoot(pVM), "Policy", sz, sizeof(sz));
345 if (RT_SUCCESS(rc))
346 {
347 if ( !RTStrICmp(sz, "no_oc")
348 || !RTStrICmp(sz, "no overcommitment"))
349 enmOcPolicy = GMMOCPOLICY_NO_OC;
350 else
351 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, "Unknown \"MM/Policy\" value \"%s\"", sz);
352 }
353 else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
354 enmOcPolicy = GMMOCPOLICY_NO_OC;
355 else
356 AssertMsgFailedReturn(("Configuration error: Failed to query string \"MM/Policy\", rc=%Rrc.\n", rc), rc);
357
358 /** @cfgm{MM/Priority, string, normal}
359 * Specifies the memory priority of this VM. The priority comes into play when the
360 * system is overcommitted and the VMs needs to be milked for memory. The recognized
361 * values are 'low', 'normal' (default) and 'high'. See GMMPRIORITY.
362 */
363 GMMPRIORITY enmPriority;
364 rc = CFGMR3QueryString(CFGMR3GetRoot(pVM), "Priority", sz, sizeof(sz));
365 if (RT_SUCCESS(rc))
366 {
367 if (!RTStrICmp(sz, "low"))
368 enmPriority = GMMPRIORITY_LOW;
369 else if (!RTStrICmp(sz, "normal"))
370 enmPriority = GMMPRIORITY_NORMAL;
371 else if (!RTStrICmp(sz, "high"))
372 enmPriority = GMMPRIORITY_HIGH;
373 else
374 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, "Unknown \"MM/Priority\" value \"%s\"", sz);
375 }
376 else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
377 enmPriority = GMMPRIORITY_NORMAL;
378 else
379 AssertMsgFailedReturn(("Configuration error: Failed to query string \"MM/Priority\", rc=%Rrc.\n", rc), rc);
380
381 /*
382 * Make the initial memory reservation with GMM.
383 */
384 uint64_t cBasePages = (cbRam >> PAGE_SHIFT) + pVM->mm.s.cBasePages;
385 rc = GMMR3InitialReservation(pVM,
386 RT_MAX(cBasePages + pVM->mm.s.cHandyPages, 1),
387 RT_MAX(pVM->mm.s.cShadowPages, 1),
388 RT_MAX(pVM->mm.s.cFixedPages, 1),
389 enmOcPolicy,
390 enmPriority);
391 if (RT_FAILURE(rc))
392 {
393 if (rc == VERR_GMM_MEMORY_RESERVATION_DECLINED)
394 return VMSetError(pVM, rc, RT_SRC_POS,
395 N_("Insufficient free memory to start the VM (cbRam=%#RX64 enmOcPolicy=%d enmPriority=%d)"),
396 cbRam, enmOcPolicy, enmPriority);
397 return VMSetError(pVM, rc, RT_SRC_POS, "GMMR3InitialReservation(,%#RX64,0,0,%d,%d)",
398 cbRam >> PAGE_SHIFT, enmOcPolicy, enmPriority);
399 }
400
401 /*
402 * If RamSize is 0 we're done now.
403 */
404 if (cbRam < PAGE_SIZE)
405 {
406 Log(("MM: No RAM configured\n"));
407 return VINF_SUCCESS;
408 }
409
410 /*
411 * Setup the base ram (PGM).
412 */
413 if (cbRam > offRamHole)
414 {
415 rc = PGMR3PhysRegisterRam(pVM, 0, offRamHole, "Base RAM");
416 if (RT_SUCCESS(rc))
417 rc = PGMR3PhysRegisterRam(pVM, _4G, cbRam - offRamHole, "Above 4GB Base RAM");
418 }
419 else
420 rc = PGMR3PhysRegisterRam(pVM, 0, RT_MIN(cbRam, offRamHole), "Base RAM");
421
422 /*
423 * Enabled mmR3UpdateReservation here since we don't want the
424 * PGMR3PhysRegisterRam calls above mess things up.
425 */
426 pVM->mm.s.fDoneMMR3InitPaging = true;
427 AssertMsg(pVM->mm.s.cBasePages == cBasePages || RT_FAILURE(rc), ("%RX64 != %RX64\n", pVM->mm.s.cBasePages, cBasePages));
428
429 LogFlow(("MMR3InitPaging: returns %Rrc\n", rc));
430 return rc;
431}
432
433
434/**
435 * Terminates the MM.
436 *
437 * Termination means cleaning up and freeing all resources,
438 * the VM it self is at this point powered off or suspended.
439 *
440 * @returns VBox status code.
441 * @param pVM The VM to operate on.
442 */
443VMMR3DECL(int) MMR3Term(PVM pVM)
444{
445 /*
446 * Destroy the page pool. (first as it used the hyper heap)
447 */
448 mmR3PagePoolTerm(pVM);
449
450 /* Clean up the hypervisor heap. */
451 mmR3HyperTerm(pVM);
452
453 /*
454 * Zero stuff to detect after termination use of the MM interface
455 */
456 pVM->mm.s.offLookupHyper = NIL_OFFSET;
457 pVM->mm.s.pHyperHeapR3 = NULL; /* freed above. */
458 pVM->mm.s.pHyperHeapR0 = NIL_RTR0PTR; /* freed above. */
459 pVM->mm.s.pHyperHeapRC = NIL_RTRCPTR; /* freed above. */
460 pVM->mm.s.offVM = 0; /* init assertion on this */
461
462 /*
463 * Destroy the User-kernel heap here since the support driver session
464 * may have been terminated by the time we get to MMR3TermUVM.
465 */
466 mmR3UkHeapDestroy(pVM->pUVM->mm.s.pUkHeap);
467 pVM->pUVM->mm.s.pUkHeap = NULL;
468
469 return VINF_SUCCESS;
470}
471
472
473/**
474 * Terminates the UVM part of MM.
475 *
476 * Termination means cleaning up and freeing all resources,
477 * the VM it self is at this point powered off or suspended.
478 *
479 * @returns VBox status code.
480 * @param pUVM Pointer to the user mode VM structure.
481 */
482VMMR3DECL(void) MMR3TermUVM(PUVM pUVM)
483{
484 /*
485 * Destroy the heaps.
486 */
487 if (pUVM->mm.s.pUkHeap)
488 {
489 mmR3UkHeapDestroy(pUVM->mm.s.pUkHeap);
490 pUVM->mm.s.pUkHeap = NULL;
491 }
492 mmR3HeapDestroy(pUVM->mm.s.pHeap);
493 pUVM->mm.s.pHeap = NULL;
494}
495
496
497/**
498 * Reset notification.
499 *
500 * @param pVM The VM handle.
501 */
502VMMR3DECL(void) MMR3Reset(PVM pVM)
503{
504 /* nothing to do anylonger. */
505}
506
507
508/**
509 * Execute state save operation.
510 *
511 * @returns VBox status code.
512 * @param pVM VM Handle.
513 * @param pSSM SSM operation handle.
514 */
515static DECLCALLBACK(int) mmR3Save(PVM pVM, PSSMHANDLE pSSM)
516{
517 LogFlow(("mmR3Save:\n"));
518
519 /* (PGM saves the physical memory.) */
520 SSMR3PutU64(pSSM, pVM->mm.s.cBasePages);
521 return SSMR3PutU64(pSSM, pVM->mm.s.cbRamBase);
522}
523
524
525/**
526 * Execute state load operation.
527 *
528 * @returns VBox status code.
529 * @param pVM VM Handle.
530 * @param pSSM SSM operation handle.
531 * @param uVersion Data layout version.
532 * @param uPhase The data phase.
533 */
534static DECLCALLBACK(int) mmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPhase)
535{
536 LogFlow(("mmR3Load:\n"));
537 Assert(uPhase == SSM_PHASE_FINAL); NOREF(uPhase);
538
539 /*
540 * Validate version.
541 */
542 if ( SSM_VERSION_MAJOR_CHANGED(uVersion, MM_SAVED_STATE_VERSION)
543 || !uVersion)
544 {
545 AssertMsgFailed(("mmR3Load: Invalid version uVersion=%d!\n", uVersion));
546 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
547 }
548
549 /*
550 * Check the cBasePages and cbRamBase values.
551 */
552 int rc;
553 RTUINT cb1;
554
555 /* cBasePages (ignored) */
556 uint64_t cPages;
557 if (uVersion >= 2)
558 rc = SSMR3GetU64(pSSM, &cPages);
559 else
560 {
561 rc = SSMR3GetUInt(pSSM, &cb1);
562 cPages = cb1 >> PAGE_SHIFT;
563 }
564 if (RT_FAILURE(rc))
565 return rc;
566
567 /* cbRamBase */
568 uint64_t cb;
569 if (uVersion != 1)
570 rc = SSMR3GetU64(pSSM, &cb);
571 else
572 {
573 rc = SSMR3GetUInt(pSSM, &cb1);
574 cb = cb1;
575 }
576 if (RT_FAILURE(rc))
577 return rc;
578 AssertLogRelMsgReturn(cb == pVM->mm.s.cbRamBase,
579 ("Memory configuration has changed. cbRamBase=%#RX64 save=%#RX64\n", pVM->mm.s.cbRamBase, cb),
580 VERR_SSM_LOAD_MEMORY_SIZE_MISMATCH);
581
582 /* (PGM restores the physical memory.) */
583 return rc;
584}
585
586
587/**
588 * Updates GMM with memory reservation changes.
589 *
590 * Called when MM::cbRamRegistered, MM::cShadowPages or MM::cFixedPages changes.
591 *
592 * @returns VBox status code - see GMMR0UpdateReservation.
593 * @param pVM The shared VM structure.
594 */
595int mmR3UpdateReservation(PVM pVM)
596{
597 VM_ASSERT_EMT(pVM);
598 if (pVM->mm.s.fDoneMMR3InitPaging)
599 return GMMR3UpdateReservation(pVM,
600 RT_MAX(pVM->mm.s.cBasePages + pVM->mm.s.cHandyPages, 1),
601 RT_MAX(pVM->mm.s.cShadowPages, 1),
602 RT_MAX(pVM->mm.s.cFixedPages, 1));
603 return VINF_SUCCESS;
604}
605
606
607/**
608 * Interface for PGM to increase the reservation of RAM and ROM pages.
609 *
610 * This can be called before MMR3InitPaging.
611 *
612 * @returns VBox status code. Will set VM error on failure.
613 * @param pVM The shared VM structure.
614 * @param cAddBasePages The number of pages to add.
615 */
616VMMR3DECL(int) MMR3IncreaseBaseReservation(PVM pVM, uint64_t cAddBasePages)
617{
618 uint64_t cOld = pVM->mm.s.cBasePages;
619 pVM->mm.s.cBasePages += cAddBasePages;
620 LogFlow(("MMR3IncreaseBaseReservation: +%RU64 (%RU64 -> %RU64\n", cAddBasePages, cOld, pVM->mm.s.cBasePages));
621 int rc = mmR3UpdateReservation(pVM);
622 if (RT_FAILURE(rc))
623 {
624 VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserved physical memory for the RAM (%#RX64 -> %#RX64 + %#RX32)"),
625 cOld, pVM->mm.s.cBasePages, pVM->mm.s.cHandyPages);
626 pVM->mm.s.cBasePages = cOld;
627 }
628 return rc;
629}
630
631
632/**
633 * Interface for PGM to make reservations for handy pages in addition to the
634 * base memory.
635 *
636 * This can be called before MMR3InitPaging.
637 *
638 * @returns VBox status code. Will set VM error on failure.
639 * @param pVM The shared VM structure.
640 * @param cHandyPages The number of handy pages.
641 */
642VMMR3DECL(int) MMR3ReserveHandyPages(PVM pVM, uint32_t cHandyPages)
643{
644 AssertReturn(!pVM->mm.s.cHandyPages, VERR_WRONG_ORDER);
645
646 pVM->mm.s.cHandyPages = cHandyPages;
647 LogFlow(("MMR3ReserveHandyPages: %RU32 (base %RU64)\n", pVM->mm.s.cHandyPages, pVM->mm.s.cBasePages));
648 int rc = mmR3UpdateReservation(pVM);
649 if (RT_FAILURE(rc))
650 {
651 VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserved physical memory for the RAM (%#RX64 + %#RX32)"),
652 pVM->mm.s.cBasePages, pVM->mm.s.cHandyPages);
653 pVM->mm.s.cHandyPages = 0;
654 }
655 return rc;
656}
657
658
659/**
660 * Interface for PGM to adjust the reservation of fixed pages.
661 *
662 * This can be called before MMR3InitPaging.
663 *
664 * @returns VBox status code. Will set VM error on failure.
665 * @param pVM The shared VM structure.
666 * @param cDeltaFixedPages The number of pages to add (positive) or subtract (negative).
667 * @param pszDesc Some description associated with the reservation.
668 */
669VMMR3DECL(int) MMR3AdjustFixedReservation(PVM pVM, int32_t cDeltaFixedPages, const char *pszDesc)
670{
671 const uint32_t cOld = pVM->mm.s.cFixedPages;
672 pVM->mm.s.cFixedPages += cDeltaFixedPages;
673 LogFlow(("MMR3AdjustFixedReservation: %d (%u -> %u)\n", cDeltaFixedPages, cOld, pVM->mm.s.cFixedPages));
674 int rc = mmR3UpdateReservation(pVM);
675 if (RT_FAILURE(rc))
676 {
677 VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserve physical memory (%#x -> %#x; %s)"),
678 cOld, pVM->mm.s.cFixedPages, pszDesc);
679 pVM->mm.s.cFixedPages = cOld;
680 }
681 return rc;
682}
683
684
685/**
686 * Interface for PGM to update the reservation of shadow pages.
687 *
688 * This can be called before MMR3InitPaging.
689 *
690 * @returns VBox status code. Will set VM error on failure.
691 * @param pVM The shared VM structure.
692 * @param cShadowPages The new page count.
693 */
694VMMR3DECL(int) MMR3UpdateShadowReservation(PVM pVM, uint32_t cShadowPages)
695{
696 const uint32_t cOld = pVM->mm.s.cShadowPages;
697 pVM->mm.s.cShadowPages = cShadowPages;
698 LogFlow(("MMR3UpdateShadowReservation: %u -> %u\n", cOld, pVM->mm.s.cShadowPages));
699 int rc = mmR3UpdateReservation(pVM);
700 if (RT_FAILURE(rc))
701 {
702 VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserve physical memory for shadow page tables (%#x -> %#x)"), cOld, pVM->mm.s.cShadowPages);
703 pVM->mm.s.cShadowPages = cOld;
704 }
705 return rc;
706}
707
708
709/**
710 * Convert HC Physical address to HC Virtual address.
711 *
712 * @returns VBox status.
713 * @param pVM VM handle.
714 * @param HCPhys The host context virtual address.
715 * @param ppv Where to store the resulting address.
716 * @thread The Emulation Thread.
717 *
718 * @remarks Avoid whenever possible.
719 * Intended for the debugger facility only.
720 * @todo Rename to indicate the special usage.
721 */
722VMMR3DECL(int) MMR3HCPhys2HCVirt(PVM pVM, RTHCPHYS HCPhys, void **ppv)
723{
724 /*
725 * Try page tables.
726 */
727 int rc = MMPagePhys2PageTry(pVM, HCPhys, ppv);
728 if (RT_SUCCESS(rc))
729 return rc;
730
731 /*
732 * Iterate thru the lookup records for HMA.
733 */
734 uint32_t off = HCPhys & PAGE_OFFSET_MASK;
735 HCPhys &= X86_PTE_PAE_PG_MASK;
736 PMMLOOKUPHYPER pCur = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.CTX_SUFF(pHyperHeap) + pVM->mm.s.offLookupHyper);
737 for (;;)
738 {
739 switch (pCur->enmType)
740 {
741 case MMLOOKUPHYPERTYPE_LOCKED:
742 {
743 PCRTHCPHYS paHCPhysPages = pCur->u.Locked.paHCPhysPages;
744 size_t iPage = pCur->cb >> PAGE_SHIFT;
745 while (iPage-- > 0)
746 if (paHCPhysPages[iPage] == HCPhys)
747 {
748 *ppv = (char *)pCur->u.Locked.pvR3 + (iPage << PAGE_SHIFT) + off;
749 return VINF_SUCCESS;
750 }
751 break;
752 }
753
754 case MMLOOKUPHYPERTYPE_HCPHYS:
755 if (pCur->u.HCPhys.HCPhys - HCPhys < pCur->cb)
756 {
757 *ppv = (uint8_t *)pCur->u.HCPhys.pvR3 + pCur->u.HCPhys.HCPhys - HCPhys + off;
758 return VINF_SUCCESS;
759 }
760 break;
761
762 case MMLOOKUPHYPERTYPE_GCPHYS: /* (for now we'll not allow these kind of conversions) */
763 case MMLOOKUPHYPERTYPE_MMIO2:
764 case MMLOOKUPHYPERTYPE_DYNAMIC:
765 break;
766
767 default:
768 AssertMsgFailed(("enmType=%d\n", pCur->enmType));
769 break;
770 }
771
772 /* next */
773 if (pCur->offNext == (int32_t)NIL_OFFSET)
774 break;
775 pCur = (PMMLOOKUPHYPER)((uint8_t *)pCur + pCur->offNext);
776 }
777 /* give up */
778 return VERR_INVALID_POINTER;
779}
780
781
782
783/**
784 * Get the size of the base RAM.
785 * This usually means the size of the first contigous block of physical memory.
786 *
787 * @returns The guest base RAM size.
788 * @param pVM The VM handle.
789 * @thread Any.
790 *
791 * @deprecated
792 */
793VMMR3DECL(uint64_t) MMR3PhysGetRamSize(PVM pVM)
794{
795 return pVM->mm.s.cbRamBase;
796}
797
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette