VirtualBox

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

Last change on this file since 97405 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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