VirtualBox

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

Last change on this file since 107663 was 107227, checked in by vboxsync, 2 months ago

VMM: Cleaning up ARMv8 / x86 split. jiraref:VBP-1470

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 31.8 KB
Line 
1/* $Id: MM.cpp 107227 2024-12-04 15:20:14Z vboxsync $ */
2/** @file
3 * MM - Memory Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2024 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#ifdef VBOX_VMM_TARGET_ARMV8
174# include <iprt/file.h>
175#endif
176
177
178/*********************************************************************************************************************************
179* Defined Constants And Macros *
180*********************************************************************************************************************************/
181/** The current saved state version of MM. */
182#define MM_SAVED_STATE_VERSION 2
183
184
185/*********************************************************************************************************************************
186* Internal Functions *
187*********************************************************************************************************************************/
188static DECLCALLBACK(int) mmR3Save(PVM pVM, PSSMHANDLE pSSM);
189static DECLCALLBACK(int) mmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
190
191
192
193
194/**
195 * Initializes the MM members of the UVM.
196 *
197 * This is currently only the ring-3 heap.
198 *
199 * @returns VBox status code.
200 * @param pUVM Pointer to the user mode VM structure.
201 */
202VMMR3DECL(int) MMR3InitUVM(PUVM pUVM)
203{
204 /*
205 * Assert sizes and order.
206 */
207 AssertCompile(sizeof(pUVM->mm.s) <= sizeof(pUVM->mm.padding));
208 AssertRelease(sizeof(pUVM->mm.s) <= sizeof(pUVM->mm.padding));
209 Assert(!pUVM->mm.s.pHeap);
210
211 /*
212 * Init the heap.
213 */
214 int rc = mmR3HeapCreateU(pUVM, &pUVM->mm.s.pHeap);
215 if (RT_SUCCESS(rc))
216 return VINF_SUCCESS;
217 return rc;
218}
219
220
221/**
222 * Initializes the MM.
223 *
224 * MM is managing the virtual address space (among other things) and
225 * setup the hypervisor memory area mapping in the VM structure and
226 * the hypervisor alloc-only-heap. Assuming the current init order
227 * and components the hypervisor memory area looks like this:
228 * -# VM Structure.
229 * -# Hypervisor alloc only heap (also call Hypervisor memory region).
230 * -# Core code.
231 *
232 * MM determines the virtual address of the hypervisor memory area by
233 * checking for location at previous run. If that property isn't available
234 * it will choose a default starting location, currently 0xa0000000.
235 *
236 * @returns VBox status code.
237 * @param pVM The cross context VM structure.
238 */
239VMMR3DECL(int) MMR3Init(PVM pVM)
240{
241 LogFlow(("MMR3Init\n"));
242
243 /*
244 * Assert alignment, sizes and order.
245 */
246 AssertRelease(!(RT_UOFFSETOF(VM, mm.s) & 31));
247 AssertRelease(sizeof(pVM->mm.s) <= sizeof(pVM->mm.padding));
248
249 /*
250 * Register the saved state data unit.
251 */
252 int rc = SSMR3RegisterInternal(pVM, "mm", 1, MM_SAVED_STATE_VERSION, sizeof(uint32_t) * 2,
253 NULL, NULL, NULL,
254 NULL, mmR3Save, NULL,
255 NULL, mmR3Load, NULL);
256 if (RT_SUCCESS(rc))
257 {
258 /*
259 * Statistics.
260 */
261 STAM_REG(pVM, &pVM->mm.s.cBasePages, STAMTYPE_U64, "/MM/Reserved/cBasePages", STAMUNIT_PAGES, "Reserved number of base pages, ROM and Shadow ROM included.");
262 STAM_REG(pVM, &pVM->mm.s.cHandyPages, STAMTYPE_U32, "/MM/Reserved/cHandyPages", STAMUNIT_PAGES, "Reserved number of handy pages.");
263 STAM_REG(pVM, &pVM->mm.s.cShadowPages, STAMTYPE_U32, "/MM/Reserved/cShadowPages", STAMUNIT_PAGES, "Reserved number of shadow paging pages.");
264 STAM_REG(pVM, &pVM->mm.s.cFixedPages, STAMTYPE_U32, "/MM/Reserved/cFixedPages", STAMUNIT_PAGES, "Reserved number of fixed pages (MMIO2).");
265 STAM_REG(pVM, &pVM->mm.s.cbRamBase, STAMTYPE_U64, "/MM/cbRamBase", STAMUNIT_BYTES, "Size of the base RAM.");
266
267 return rc;
268 }
269
270 return rc;
271}
272
273#ifdef VBOX_VMM_TARGET_ARMV8
274
275/**
276 * Initializes the given RAM range with data from the given file.
277 *
278 * @returns VBox status code.
279 * @param pVM The cross context VM structure.
280 * @param GCPhysStart Where to start putting the file content.
281 * @param pszFilename The file to read the data from.
282 */
283static int mmR3RamRegionInitFromFile(PVM pVM, RTGCPHYS GCPhysStart, const char *pszFilename)
284{
285 RTFILE hFile = NIL_RTFILE;
286 int rc = RTFileOpen(&hFile, pszFilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
287 if (RT_SUCCESS(rc))
288 {
289 RTGCPHYS GCPhys = GCPhysStart;
290
291 for (;;)
292 {
293 uint8_t abRead[GUEST_PAGE_SIZE];
294 size_t cbThisRead = 0;
295 rc = RTFileRead(hFile, &abRead[0], sizeof(abRead), &cbThisRead);
296 if (RT_FAILURE(rc))
297 break;
298
299 rc = PGMPhysSimpleWriteGCPhys(pVM, GCPhys, &abRead[0], cbThisRead);
300 if (RT_FAILURE(rc))
301 break;
302
303 GCPhys += cbThisRead;
304 if (cbThisRead < sizeof(abRead))
305 break;
306 }
307
308 RTFileClose(hFile);
309 }
310
311 if (RT_FAILURE(rc))
312 LogRel(("RAM#%RGp: Loading file %s failed -> %Rrc\n", GCPhysStart, pszFilename, rc));
313
314 return rc;
315}
316
317
318/**
319 * This sets up the RAM ranges from the VM config.
320 *
321 * @returns VBox status code.
322 * @param pVM The cross context VM structure.
323 * @param pMMCfg Pointer to the CFGM node holding the RAM config.
324 *
325 * @note On ARM there is no "standard" way to handle RAM like on x86.
326 * Every SoC can have multiple RAM regions scattered across the whole
327 * address space so we have to be much more flexible here.
328 */
329static int mmR3InitRamArmV8(PVM pVM, PCFGMNODE pMMCfg)
330{
331 int rc = VINF_SUCCESS;
332 PCFGMNODE pMemRegions = CFGMR3GetChild(pMMCfg, "MemRegions");
333
334 pVM->mm.s.cbRamBase = 0;
335 pVM->mm.s.cbRamHole = 0;
336 pVM->mm.s.cbRamBelow4GB = 0;
337 pVM->mm.s.cbRamAbove4GB = 0;
338
339 for (PCFGMNODE pCur = CFGMR3GetFirstChild(pMemRegions); pCur; pCur = CFGMR3GetNextChild(pCur))
340 {
341 char szMemRegion[512]; RT_ZERO(szMemRegion);
342 rc = CFGMR3GetName(pCur, &szMemRegion[0], sizeof(szMemRegion));
343 if (RT_FAILURE(rc))
344 {
345 LogRel(("Failed to query memory region name -> %Rrc\n", rc));
346 break;
347 }
348
349 uint64_t u64GCPhysStart = 0;
350 rc = CFGMR3QueryU64(pCur, "GCPhysStart", &u64GCPhysStart);
351 if (RT_FAILURE(rc))
352 {
353 LogRel(("Failed to query \"GCPhysStart\" for memory region %s -> %Rrc\n", szMemRegion, rc));
354 break;
355 }
356
357 uint64_t u64MemSize = 0;
358 rc = CFGMR3QueryU64(pCur, "Size", &u64MemSize);
359 if (RT_FAILURE(rc))
360 {
361 LogRel(("Failed to query \"Size\" for memory region %s -> %Rrc\n", szMemRegion, rc));
362 break;
363 }
364
365 rc = PGMR3PhysRegisterRam(pVM, u64GCPhysStart, u64MemSize, "Conventional RAM");
366 if (RT_FAILURE(rc))
367 {
368 LogRel(("Failed to register memory region '%s' GCPhysStart=%RGp Size=%#RX64 -> %Rrc\n",
369 szMemRegion, u64GCPhysStart, u64MemSize));
370 break;
371 }
372
373 char *pszFilename = NULL;
374 rc = CFGMR3QueryStringAlloc(pCur, "PrepopulateFromFile", &pszFilename);
375 if (RT_SUCCESS(rc))
376 {
377 rc = mmR3RamRegionInitFromFile(pVM, u64GCPhysStart, pszFilename);
378 MMR3HeapFree(pszFilename);
379 if (RT_FAILURE(rc))
380 break;
381 }
382 else if (rc != VERR_CFGM_VALUE_NOT_FOUND)
383 {
384 LogRel(("Failed to query \"PrepopulateFromFile\" for memory region %s -> %Rrc\n", szMemRegion, rc));
385 break;
386 }
387 else
388 rc = VINF_SUCCESS;
389
390 pVM->mm.s.cbRamBase += u64MemSize;
391 if (u64GCPhysStart >= _4G)
392 pVM->mm.s.cbRamAbove4GB += u64MemSize;
393 else if (u64GCPhysStart + u64MemSize <= _4G)
394 pVM->mm.s.cbRamBelow4GB += (uint32_t)u64MemSize;
395 else
396 {
397 uint64_t const cbRamAbove4GB = u64GCPhysStart + u64MemSize - _4G;
398 pVM->mm.s.cbRamAbove4GB += cbRamAbove4GB;
399 pVM->mm.s.cbRamBelow4GB += u64MemSize - cbRamAbove4GB;
400 }
401 }
402
403 return rc;
404}
405
406#elif defined(VBOX_VMM_TARGET_X86)
407
408/**
409 * RAM setup function for X86.
410 */
411static int mmR3InitRamX86(PVM pVM, PCFGMNODE pMMCfg)
412{
413 RT_NOREF(pMMCfg);
414
415 /** @cfgm{/RamSize, uint64_t, 0, 16TB, 0}
416 * Specifies the size of the base RAM that is to be set up during
417 * VM initialization.
418 */
419 uint64_t cbRam = 0;
420 int rc = CFGMR3QueryU64(CFGMR3GetRoot(pVM), "RamSize", &cbRam);
421 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
422 cbRam = 0;
423 else
424 AssertMsgRCReturn(rc, ("Configuration error: Failed to query integer \"RamSize\", rc=%Rrc.\n", rc), rc);
425 AssertLogRelMsg(!(cbRam & ~X86_PTE_PAE_PG_MASK), ("%RGp X86_PTE_PAE_PG_MASK=%RX64\n", cbRam, X86_PTE_PAE_PG_MASK));
426 AssertLogRelMsgReturn(cbRam <= GMM_GCPHYS_LAST, ("cbRam=%RGp GMM_GCPHYS_LAST=%RX64\n", cbRam, GMM_GCPHYS_LAST), VERR_OUT_OF_RANGE);
427 cbRam &= X86_PTE_PAE_PG_MASK;
428 pVM->mm.s.cbRamBase = cbRam;
429
430 /** @cfgm{/RamHoleSize, uint32_t, 0, 4032MB, 512MB}
431 * Specifies the size of the memory hole. The memory hole is used
432 * to avoid mapping RAM to the range normally used for PCI memory regions.
433 * Must be aligned on a 4MB boundary. */
434 uint32_t cbRamHole = 0;
435 rc = CFGMR3QueryU32Def(CFGMR3GetRoot(pVM), "RamHoleSize", &cbRamHole, MM_RAM_HOLE_SIZE_DEFAULT);
436 AssertLogRelMsgRCReturn(rc, ("Configuration error: Failed to query integer \"RamHoleSize\", rc=%Rrc.\n", rc), rc);
437 AssertLogRelMsgReturn(cbRamHole <= 4032U * _1M,
438 ("Configuration error: \"RamHoleSize\"=%#RX32 is too large.\n", cbRamHole), VERR_OUT_OF_RANGE);
439 AssertLogRelMsgReturn(cbRamHole > 16 * _1M,
440 ("Configuration error: \"RamHoleSize\"=%#RX32 is too small.\n", cbRamHole), VERR_OUT_OF_RANGE);
441 AssertLogRelMsgReturn(!(cbRamHole & (_4M - 1)),
442 ("Configuration error: \"RamHoleSize\"=%#RX32 is misaligned.\n", cbRamHole), VERR_OUT_OF_RANGE);
443 uint64_t const offRamHole = _4G - cbRamHole;
444 if (cbRam < offRamHole)
445 Log(("MM: %RU64 bytes of RAM\n", cbRam));
446 else
447 Log(("MM: %RU64 bytes of RAM with a hole at %RU64 up to 4GB.\n", cbRam, offRamHole));
448
449 /** @cfgm{/MM/Policy, string, no overcommitment}
450 * Specifies the policy to use when reserving memory for this VM. The recognized
451 * value is 'no overcommitment' (default). See GMMPOLICY.
452 */
453 GMMOCPOLICY enmOcPolicy;
454 char sz[64];
455 rc = CFGMR3QueryString(CFGMR3GetRoot(pVM), "Policy", sz, sizeof(sz));
456 if (RT_SUCCESS(rc))
457 {
458 if ( !RTStrICmp(sz, "no_oc")
459 || !RTStrICmp(sz, "no overcommitment"))
460 enmOcPolicy = GMMOCPOLICY_NO_OC;
461 else
462 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, "Unknown \"MM/Policy\" value \"%s\"", sz);
463 }
464 else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
465 enmOcPolicy = GMMOCPOLICY_NO_OC;
466 else
467 AssertMsgFailedReturn(("Configuration error: Failed to query string \"MM/Policy\", rc=%Rrc.\n", rc), rc);
468
469 /** @cfgm{/MM/Priority, string, normal}
470 * Specifies the memory priority of this VM. The priority comes into play when the
471 * system is overcommitted and the VMs needs to be milked for memory. The recognized
472 * values are 'low', 'normal' (default) and 'high'. See GMMPRIORITY.
473 */
474 GMMPRIORITY enmPriority;
475 rc = CFGMR3QueryString(CFGMR3GetRoot(pVM), "Priority", sz, sizeof(sz));
476 if (RT_SUCCESS(rc))
477 {
478 if (!RTStrICmp(sz, "low"))
479 enmPriority = GMMPRIORITY_LOW;
480 else if (!RTStrICmp(sz, "normal"))
481 enmPriority = GMMPRIORITY_NORMAL;
482 else if (!RTStrICmp(sz, "high"))
483 enmPriority = GMMPRIORITY_HIGH;
484 else
485 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, "Unknown \"MM/Priority\" value \"%s\"", sz);
486 }
487 else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
488 enmPriority = GMMPRIORITY_NORMAL;
489 else
490 AssertMsgFailedReturn(("Configuration error: Failed to query string \"MM/Priority\", rc=%Rrc.\n", rc), rc);
491
492 /*
493 * Make the initial memory reservation with GMM.
494 */
495 uint32_t const cbUma = _1M - 640*_1K;
496 uint64_t cBasePages = ((cbRam - cbUma) >> GUEST_PAGE_SHIFT) + pVM->mm.s.cBasePages;
497 rc = GMMR3InitialReservation(pVM,
498 RT_MAX(cBasePages + pVM->mm.s.cHandyPages, 1),
499 RT_MAX(pVM->mm.s.cShadowPages, 1),
500 RT_MAX(pVM->mm.s.cFixedPages, 1),
501 enmOcPolicy,
502 enmPriority);
503 if (RT_FAILURE(rc))
504 {
505 if (rc == VERR_GMM_MEMORY_RESERVATION_DECLINED)
506 return VMSetError(pVM, rc, RT_SRC_POS,
507 N_("Insufficient free memory to start the VM (cbRam=%#RX64 enmOcPolicy=%d enmPriority=%d)"),
508 cbRam, enmOcPolicy, enmPriority);
509 return VMSetError(pVM, rc, RT_SRC_POS, "GMMR3InitialReservation(,%#RX64,0,0,%d,%d)",
510 cbRam >> GUEST_PAGE_SHIFT, enmOcPolicy, enmPriority);
511 }
512
513 /*
514 * If RamSize is 0 we're done now.
515 */
516 if (cbRam < GUEST_PAGE_SIZE)
517 {
518 Log(("MM: No RAM configured\n"));
519 return VINF_SUCCESS;
520 }
521
522 /*
523 * Setup the base ram (PGM).
524 */
525 pVM->mm.s.cbRamHole = cbRamHole;
526 pVM->mm.s.cbRamBelow4GB = cbRam > offRamHole ? offRamHole : cbRam;
527 pVM->mm.s.cbRamAbove4GB = cbRam > offRamHole ? cbRam - offRamHole : 0;
528
529 /* First the conventional memory: */
530 rc = PGMR3PhysRegisterRam(pVM, 0, RT_MIN(cbRam, 640*_1K), "Conventional RAM");
531 if (RT_SUCCESS(rc) && cbRam >= _1M)
532 {
533 /* The extended memory from 1MiB to 2MiB to align better with large pages in NEM mode: */
534 rc = PGMR3PhysRegisterRam(pVM, _1M, RT_MIN(_1M, cbRam - _1M), "Extended RAM, 1-2MB");
535 if (cbRam > _2M)
536 {
537 /* The extended memory from 2MiB up to 4GiB: */
538 rc = PGMR3PhysRegisterRam(pVM, _2M, pVM->mm.s.cbRamBelow4GB - _2M, "Extended RAM, >2MB");
539
540 /* Then all the memory above 4GiB: */
541 if (RT_SUCCESS(rc) && pVM->mm.s.cbRamAbove4GB > 0)
542 rc = PGMR3PhysRegisterRam(pVM, _4G, cbRam - offRamHole, "Above 4GB Base RAM");
543 }
544 }
545
546 AssertMsg(pVM->mm.s.cBasePages == cBasePages || RT_FAILURE(rc), ("%RX64 != %RX64\n", pVM->mm.s.cBasePages, cBasePages));
547 return rc;
548}
549
550#endif /* VBOX_VMM_TARGET_X86 */
551
552/**
553 * Initializes the MM parts which depends on PGM being initialized.
554 *
555 * @returns VBox status code.
556 * @param pVM The cross context VM structure.
557 * @remark No cleanup necessary since MMR3Term() will be called on failure.
558 */
559VMMR3DECL(int) MMR3InitPaging(PVM pVM)
560{
561 LogFlow(("MMR3InitPaging:\n"));
562
563 /*
564 * Query the CFGM values.
565 */
566 int rc;
567 PCFGMNODE pMMCfg = CFGMR3GetChild(CFGMR3GetRoot(pVM), "MM");
568 if (!pMMCfg)
569 {
570 rc = CFGMR3InsertNode(CFGMR3GetRoot(pVM), "MM", &pMMCfg);
571 AssertRCReturn(rc, rc);
572 }
573
574#ifdef VBOX_VMM_TARGET_ARMV8
575 rc = mmR3InitRamArmV8(pVM, pMMCfg);
576#elif defined(VBOX_VMM_TARGET_X86)
577 rc = mmR3InitRamX86(pVM, pMMCfg);
578#else
579# error "port me"
580#endif
581
582 /*
583 * Enabled mmR3UpdateReservation here since we don't want the
584 * PGMR3PhysRegisterRam calls above mess things up.
585 */
586 pVM->mm.s.fDoneMMR3InitPaging = true;
587
588 LogFlow(("MMR3InitPaging: returns %Rrc\n", rc));
589 return rc;
590}
591
592
593/**
594 * Terminates the MM.
595 *
596 * Termination means cleaning up and freeing all resources,
597 * the VM it self is at this point powered off or suspended.
598 *
599 * @returns VBox status code.
600 * @param pVM The cross context VM structure.
601 */
602VMMR3DECL(int) MMR3Term(PVM pVM)
603{
604 RT_NOREF(pVM);
605 return VINF_SUCCESS;
606}
607
608
609/**
610 * Terminates the UVM part of MM.
611 *
612 * Termination means cleaning up and freeing all resources,
613 * the VM it self is at this point powered off or suspended.
614 *
615 * @param pUVM Pointer to the user mode VM structure.
616 */
617VMMR3DECL(void) MMR3TermUVM(PUVM pUVM)
618{
619 /*
620 * Destroy the heap.
621 */
622 mmR3HeapDestroy(pUVM->mm.s.pHeap);
623 pUVM->mm.s.pHeap = NULL;
624}
625
626
627/**
628 * Execute state save operation.
629 *
630 * @returns VBox status code.
631 * @param pVM The cross context VM structure.
632 * @param pSSM SSM operation handle.
633 */
634static DECLCALLBACK(int) mmR3Save(PVM pVM, PSSMHANDLE pSSM)
635{
636 LogFlow(("mmR3Save:\n"));
637
638 /* (PGM saves the physical memory.) */
639 SSMR3PutU64(pSSM, pVM->mm.s.cBasePages);
640 return SSMR3PutU64(pSSM, pVM->mm.s.cbRamBase);
641}
642
643
644/**
645 * Execute state load operation.
646 *
647 * @returns VBox status code.
648 * @param pVM The cross context VM structure.
649 * @param pSSM SSM operation handle.
650 * @param uVersion Data layout version.
651 * @param uPass The data pass.
652 */
653static DECLCALLBACK(int) mmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
654{
655 LogFlow(("mmR3Load:\n"));
656 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
657
658 /*
659 * Validate version.
660 */
661 if ( SSM_VERSION_MAJOR_CHANGED(uVersion, MM_SAVED_STATE_VERSION)
662 || !uVersion)
663 {
664 AssertMsgFailed(("mmR3Load: Invalid version uVersion=%d!\n", uVersion));
665 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
666 }
667
668 /*
669 * Check the cBasePages and cbRamBase values.
670 */
671 int rc;
672 RTUINT cb1;
673
674 /* cBasePages (ignored) */
675 uint64_t cGuestPages;
676 if (uVersion >= 2)
677 rc = SSMR3GetU64(pSSM, &cGuestPages);
678 else
679 {
680 rc = SSMR3GetUInt(pSSM, &cb1);
681 cGuestPages = cb1 >> GUEST_PAGE_SHIFT;
682 }
683 if (RT_FAILURE(rc))
684 return rc;
685
686 /* cbRamBase */
687 uint64_t cb;
688 if (uVersion != 1)
689 rc = SSMR3GetU64(pSSM, &cb);
690 else
691 {
692 rc = SSMR3GetUInt(pSSM, &cb1);
693 cb = cb1;
694 }
695 if (RT_FAILURE(rc))
696 return rc;
697 AssertLogRelMsgReturn(cb == pVM->mm.s.cbRamBase,
698 ("Memory configuration has changed. cbRamBase=%#RX64 save=%#RX64\n", pVM->mm.s.cbRamBase, cb),
699 VERR_SSM_LOAD_MEMORY_SIZE_MISMATCH);
700
701 /* (PGM restores the physical memory.) */
702 return rc;
703}
704
705
706/**
707 * Updates GMM with memory reservation changes.
708 *
709 * Called when MM::cbRamRegistered, MM::cShadowPages or MM::cFixedPages changes.
710 *
711 * @returns VBox status code - see GMMR0UpdateReservation.
712 * @param pVM The cross context VM structure.
713 */
714int mmR3UpdateReservation(PVM pVM)
715{
716 VM_ASSERT_EMT(pVM);
717 if (pVM->mm.s.fDoneMMR3InitPaging)
718 return GMMR3UpdateReservation(pVM,
719 RT_MAX(pVM->mm.s.cBasePages + pVM->mm.s.cHandyPages, 1),
720 RT_MAX(pVM->mm.s.cShadowPages, 1),
721 RT_MAX(pVM->mm.s.cFixedPages, 1));
722 return VINF_SUCCESS;
723}
724
725
726/**
727 * Interface for PGM to increase the reservation of RAM and ROM pages.
728 *
729 * This can be called before MMR3InitPaging.
730 *
731 * @returns VBox status code. Will set VM error on failure.
732 * @param pVM The cross context VM structure.
733 * @param cAddBasePages The number of pages to add.
734 */
735VMMR3DECL(int) MMR3IncreaseBaseReservation(PVM pVM, uint64_t cAddBasePages)
736{
737 uint64_t cOld = pVM->mm.s.cBasePages;
738 pVM->mm.s.cBasePages += cAddBasePages;
739 LogFlow(("MMR3IncreaseBaseReservation: +%RU64 (%RU64 -> %RU64)\n", cAddBasePages, cOld, pVM->mm.s.cBasePages));
740 int rc = mmR3UpdateReservation(pVM);
741 if (RT_FAILURE(rc))
742 {
743 VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserved physical memory for the RAM (%#RX64 -> %#RX64 + %#RX32)"),
744 cOld, pVM->mm.s.cBasePages, pVM->mm.s.cHandyPages);
745 pVM->mm.s.cBasePages = cOld;
746 }
747 return rc;
748}
749
750
751/**
752 * Interface for PGM to make reservations for handy pages in addition to the
753 * base memory.
754 *
755 * This can be called before MMR3InitPaging.
756 *
757 * @returns VBox status code. Will set VM error on failure.
758 * @param pVM The cross context VM structure.
759 * @param cHandyPages The number of handy pages.
760 */
761VMMR3DECL(int) MMR3ReserveHandyPages(PVM pVM, uint32_t cHandyPages)
762{
763 AssertReturn(!pVM->mm.s.cHandyPages, VERR_WRONG_ORDER);
764
765 pVM->mm.s.cHandyPages = cHandyPages;
766 LogFlow(("MMR3ReserveHandyPages: %RU32 (base %RU64)\n", pVM->mm.s.cHandyPages, pVM->mm.s.cBasePages));
767 int rc = mmR3UpdateReservation(pVM);
768 if (RT_FAILURE(rc))
769 {
770 VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserved physical memory for the RAM (%#RX64 + %#RX32)"),
771 pVM->mm.s.cBasePages, pVM->mm.s.cHandyPages);
772 pVM->mm.s.cHandyPages = 0;
773 }
774 return rc;
775}
776
777
778/**
779 * Interface for PGM to adjust the reservation of fixed pages.
780 *
781 * This can be called before MMR3InitPaging.
782 *
783 * @returns VBox status code. Will set VM error on failure.
784 * @param pVM The cross context VM structure.
785 * @param cDeltaFixedPages The number of guest pages to add (positive) or
786 * subtract (negative).
787 * @param pszDesc Some description associated with the reservation.
788 */
789VMMR3DECL(int) MMR3AdjustFixedReservation(PVM pVM, int32_t cDeltaFixedPages, const char *pszDesc)
790{
791 const uint32_t cOld = pVM->mm.s.cFixedPages;
792 pVM->mm.s.cFixedPages += cDeltaFixedPages;
793 LogFlow(("MMR3AdjustFixedReservation: %d (%u -> %u)\n", cDeltaFixedPages, cOld, pVM->mm.s.cFixedPages));
794 int rc = mmR3UpdateReservation(pVM);
795 if (RT_FAILURE(rc))
796 {
797 VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserve physical memory (%#x -> %#x; %s)"),
798 cOld, pVM->mm.s.cFixedPages, pszDesc);
799 pVM->mm.s.cFixedPages = cOld;
800 }
801 return rc;
802}
803
804
805/**
806 * Interface for PGM to update the reservation of shadow pages.
807 *
808 * This can be called before MMR3InitPaging.
809 *
810 * @returns VBox status code. Will set VM error on failure.
811 * @param pVM The cross context VM structure.
812 * @param cShadowPages The new page count.
813 */
814VMMR3DECL(int) MMR3UpdateShadowReservation(PVM pVM, uint32_t cShadowPages)
815{
816 const uint32_t cOld = pVM->mm.s.cShadowPages;
817 pVM->mm.s.cShadowPages = cShadowPages;
818 LogFlow(("MMR3UpdateShadowReservation: %u -> %u\n", cOld, pVM->mm.s.cShadowPages));
819 int rc = mmR3UpdateReservation(pVM);
820 if (RT_FAILURE(rc))
821 {
822 VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserve physical memory for shadow page tables (%#x -> %#x)"), cOld, pVM->mm.s.cShadowPages);
823 pVM->mm.s.cShadowPages = cOld;
824 }
825 return rc;
826}
827
828
829/**
830 * Get the size of the base RAM.
831 * This usually means the size of the first contiguous block of physical memory.
832 *
833 * @returns The guest base RAM size.
834 * @param pVM The cross context VM structure.
835 * @thread Any.
836 *
837 * @deprecated
838 */
839VMMR3DECL(uint64_t) MMR3PhysGetRamSize(PVM pVM)
840{
841 return pVM->mm.s.cbRamBase;
842}
843
844
845/**
846 * Get the size of RAM below 4GB (starts at address 0x00000000).
847 *
848 * @returns The amount of RAM below 4GB in bytes.
849 * @param pVM The cross context VM structure.
850 * @thread Any.
851 */
852VMMR3DECL(uint32_t) MMR3PhysGetRamSizeBelow4GB(PVM pVM)
853{
854 VM_ASSERT_VALID_EXT_RETURN(pVM, UINT32_MAX);
855 return pVM->mm.s.cbRamBelow4GB;
856}
857
858
859/**
860 * Get the size of RAM above 4GB (starts at address 0x000100000000).
861 *
862 * @returns The amount of RAM above 4GB in bytes.
863 * @param pVM The cross context VM structure.
864 * @thread Any.
865 */
866VMMR3DECL(uint64_t) MMR3PhysGetRamSizeAbove4GB(PVM pVM)
867{
868 VM_ASSERT_VALID_EXT_RETURN(pVM, UINT64_MAX);
869 return pVM->mm.s.cbRamAbove4GB;
870}
871
872
873/**
874 * Get the size of the RAM hole below 4GB.
875 *
876 * @returns Size in bytes.
877 * @param pVM The cross context VM structure.
878 * @thread Any.
879 */
880VMMR3DECL(uint32_t) MMR3PhysGet4GBRamHoleSize(PVM pVM)
881{
882 VM_ASSERT_VALID_EXT_RETURN(pVM, UINT32_MAX);
883 return pVM->mm.s.cbRamHole;
884}
885
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