VirtualBox

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

Last change on this file since 92192 was 92192, checked in by vboxsync, 3 years ago

VMM/MM,DevPcArch: Split the base RAM allocation into conventional (< 640KB) and extended (>= 1MB) ranges, leaving the UMA w/o RAM range backing. This will fit better with NEM, esp. on linux. bugref:10122

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 30.3 KB
Line 
1/* $Id: MM.cpp 92192 2021-11-03 14:44:45Z vboxsync $ */
2/** @file
3 * MM - Memory Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
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 Manager
20 *
21 * The memory manager is in charge of the following memory:
22 * - Hypervisor Memory Area (HMA) - Address space management (obsolete in 6.1).
23 * - Hypervisor Heap - A memory heap that lives in all contexts.
24 * - User-Kernel Heap - A memory heap lives in both host context.
25 * - Tagged ring-3 heap.
26 * - Page pools - Primarily used by PGM for shadow page tables.
27 * - Locked process memory - Guest RAM and other. (reduce/obsolete this)
28 * - Physical guest memory (RAM & ROM) - Moving to PGM. (obsolete this)
29 *
30 * The global memory manager (GMM) is the global counter part / partner of MM.
31 * MM will provide therefore ring-3 callable interfaces for some of the GMM APIs
32 * related to resource tracking (PGM is the user).
33 *
34 * @see grp_mm
35 *
36 *
37 * @section sec_mm_hma Hypervisor Memory Area - Obsolete in 6.1
38 *
39 * The HMA is used when executing in raw-mode. We borrow, with the help of
40 * PGMMap, some unused space (one or more page directory entries to be precise)
41 * in the guest's virtual memory context. PGM will monitor the guest's virtual
42 * address space for changes and relocate the HMA when required.
43 *
44 * To give some idea what's in the HMA, study the 'info hma' output:
45 * @verbatim
46VBoxDbg> info hma
47Hypervisor Memory Area (HMA) Layout: Base 00000000a0000000, 0x00800000 bytes
4800000000a05cc000-00000000a05cd000 DYNAMIC fence
4900000000a05c4000-00000000a05cc000 DYNAMIC Dynamic mapping
5000000000a05c3000-00000000a05c4000 DYNAMIC fence
5100000000a05b8000-00000000a05c3000 DYNAMIC Paging
5200000000a05b6000-00000000a05b8000 MMIO2 0000000000000000 PCNetShMem
5300000000a0536000-00000000a05b6000 MMIO2 0000000000000000 VGA VRam
5400000000a0523000-00000000a0536000 00002aaab3d0c000 LOCKED autofree alloc once (PDM_DEVICE)
5500000000a0522000-00000000a0523000 DYNAMIC fence
5600000000a051e000-00000000a0522000 00002aaab36f5000 LOCKED autofree VBoxDD2RC.rc
5700000000a051d000-00000000a051e000 DYNAMIC fence
5800000000a04eb000-00000000a051d000 00002aaab36c3000 LOCKED autofree VBoxDDRC.rc
5900000000a04ea000-00000000a04eb000 DYNAMIC fence
6000000000a04e9000-00000000a04ea000 00002aaab36c2000 LOCKED autofree ram range (High ROM Region)
6100000000a04e8000-00000000a04e9000 DYNAMIC fence
6200000000a040e000-00000000a04e8000 00002aaab2e6d000 LOCKED autofree VMMRC.rc
6300000000a0208000-00000000a040e000 00002aaab2c67000 LOCKED autofree alloc once (PATM)
6400000000a01f7000-00000000a0208000 00002aaaab92d000 LOCKED autofree alloc once (SELM)
6500000000a01e7000-00000000a01f7000 00002aaaab5e8000 LOCKED autofree alloc once (SELM)
6600000000a01e6000-00000000a01e7000 DYNAMIC fence
6700000000a01e5000-00000000a01e6000 00002aaaab5e7000 HCPHYS 00000000c363c000 Core Code
6800000000a01e4000-00000000a01e5000 DYNAMIC fence
6900000000a01e3000-00000000a01e4000 00002aaaaab26000 HCPHYS 00000000619cf000 GIP
7000000000a01a2000-00000000a01e3000 00002aaaabf32000 LOCKED autofree alloc once (PGM_PHYS)
7100000000a016b000-00000000a01a2000 00002aaab233f000 LOCKED autofree alloc once (PGM_POOL)
7200000000a016a000-00000000a016b000 DYNAMIC fence
7300000000a0165000-00000000a016a000 DYNAMIC CR3 mapping
7400000000a0164000-00000000a0165000 DYNAMIC fence
7500000000a0024000-00000000a0164000 00002aaab215f000 LOCKED autofree Heap
7600000000a0023000-00000000a0024000 DYNAMIC fence
7700000000a0001000-00000000a0023000 00002aaab1d24000 LOCKED pages VM
7800000000a0000000-00000000a0001000 DYNAMIC fence
79 @endverbatim
80 *
81 *
82 * @section sec_mm_hyperheap Hypervisor Heap
83 *
84 * The heap is accessible from ring-3, ring-0 and the raw-mode context. That
85 * said, it's not necessarily mapped into ring-0 on if that's possible since we
86 * don't wish to waste kernel address space without a good reason.
87 *
88 * Allocations within the heap are always in the same relative position in all
89 * contexts, so, it's possible to use offset based linking. In fact, the heap is
90 * internally using offset based linked lists tracking heap blocks. We use
91 * offset linked AVL trees and lists in a lot of places where share structures
92 * between RC, R3 and R0, so this is a strict requirement of the heap. However
93 * this means that we cannot easily extend the heap since the extension won't
94 * necessarily be in the continuation of the current heap memory in all (or any)
95 * context.
96 *
97 * All allocations are tagged. Per tag allocation statistics will be maintaining
98 * and exposed thru STAM when VBOX_WITH_STATISTICS is defined.
99 *
100 *
101 * @section sec_mm_r3heap Tagged Ring-3 Heap
102 *
103 * The ring-3 heap is a wrapper around the RTMem API adding allocation
104 * statistics and automatic cleanup on VM destruction.
105 *
106 * Per tag allocation statistics will be maintaining and exposed thru STAM when
107 * VBOX_WITH_STATISTICS is defined.
108 *
109 *
110 * @section sec_mm_page Page Pool
111 *
112 * The MM manages a page pool from which other components can allocate locked,
113 * page aligned and page sized memory objects. The pool provides facilities to
114 * convert back and forth between (host) physical and virtual addresses (within
115 * the pool of course). Several specialized interfaces are provided for the most
116 * common allocations and conversions to save the caller from bothersome casting
117 * and extra parameter passing.
118 *
119 *
120 * @section sec_mm_locked Locked Process Memory
121 *
122 * MM manages the locked process memory. This is used for a bunch of things
123 * (count the LOCKED entries in the 'info hma' output found in @ref sec_mm_hma),
124 * but the main consumer of memory is currently for guest RAM. There is an
125 * ongoing rewrite that will move all the guest RAM allocation to PGM and
126 * GMM.
127 *
128 * The locking of memory is something doing in cooperation with the VirtualBox
129 * support driver, SUPDrv (aka. VBoxDrv), thru the support library API,
130 * SUPR3 (aka. SUPLib).
131 *
132 *
133 * @section sec_mm_phys Physical Guest Memory
134 *
135 * MM is currently managing the physical memory for the guest. It relies heavily
136 * on PGM for this. There is an ongoing rewrite that will move this to PGM. (The
137 * rewrite is driven by the need for more flexible guest ram allocation, but
138 * also motivated by the fact that MMPhys is just adding stupid bureaucracy and
139 * that MMR3PhysReserve is a totally weird artifact that must go away.)
140 *
141 */
142
143
144/*********************************************************************************************************************************
145* Header Files *
146*********************************************************************************************************************************/
147#define LOG_GROUP LOG_GROUP_MM
148#include <VBox/vmm/mm.h>
149#include <VBox/vmm/pgm.h>
150#include <VBox/vmm/cfgm.h>
151#include <VBox/vmm/ssm.h>
152#include <VBox/vmm/gmm.h>
153#include "MMInternal.h"
154#include <VBox/vmm/vm.h>
155#include <VBox/vmm/uvm.h>
156#include <VBox/err.h>
157#include <VBox/param.h>
158
159#include <VBox/log.h>
160#include <iprt/alloc.h>
161#include <iprt/assert.h>
162#include <iprt/string.h>
163
164
165/*********************************************************************************************************************************
166* Defined Constants And Macros *
167*********************************************************************************************************************************/
168/** The current saved state version of MM. */
169#define MM_SAVED_STATE_VERSION 2
170
171
172/*********************************************************************************************************************************
173* Internal Functions *
174*********************************************************************************************************************************/
175static DECLCALLBACK(int) mmR3Save(PVM pVM, PSSMHANDLE pSSM);
176static DECLCALLBACK(int) mmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
177
178
179
180
181/**
182 * Initializes the MM members of the UVM.
183 *
184 * This is currently only the ring-3 heap.
185 *
186 * @returns VBox status code.
187 * @param pUVM Pointer to the user mode VM structure.
188 */
189VMMR3DECL(int) MMR3InitUVM(PUVM pUVM)
190{
191 /*
192 * Assert sizes and order.
193 */
194 AssertCompile(sizeof(pUVM->mm.s) <= sizeof(pUVM->mm.padding));
195 AssertRelease(sizeof(pUVM->mm.s) <= sizeof(pUVM->mm.padding));
196 Assert(!pUVM->mm.s.pHeap);
197
198 /*
199 * Init the heap.
200 */
201 int rc = mmR3HeapCreateU(pUVM, &pUVM->mm.s.pHeap);
202 if (RT_SUCCESS(rc))
203 return VINF_SUCCESS;
204 return rc;
205}
206
207
208/**
209 * Initializes the MM.
210 *
211 * MM is managing the virtual address space (among other things) and
212 * setup the hypervisor memory area mapping in the VM structure and
213 * the hypervisor alloc-only-heap. Assuming the current init order
214 * and components the hypervisor memory area looks like this:
215 * -# VM Structure.
216 * -# Hypervisor alloc only heap (also call Hypervisor memory region).
217 * -# Core code.
218 *
219 * MM determines the virtual address of the hypervisor memory area by
220 * checking for location at previous run. If that property isn't available
221 * it will choose a default starting location, currently 0xa0000000.
222 *
223 * @returns VBox status code.
224 * @param pVM The cross context VM structure.
225 */
226VMMR3DECL(int) MMR3Init(PVM pVM)
227{
228 LogFlow(("MMR3Init\n"));
229
230 /*
231 * Assert alignment, sizes and order.
232 */
233 AssertRelease(!(RT_UOFFSETOF(VM, mm.s) & 31));
234 AssertRelease(sizeof(pVM->mm.s) <= sizeof(pVM->mm.padding));
235 AssertMsg(pVM->mm.s.offVM == 0, ("Already initialized!\n"));
236
237 /*
238 * Init the structure.
239 */
240 pVM->mm.s.offVM = RT_UOFFSETOF(VM, mm);
241 pVM->mm.s.offLookupHyper = NIL_OFFSET;
242
243 /*
244 * Init the hypervisor related stuff.
245 */
246 int rc = mmR3HyperInit(pVM);
247 if (RT_SUCCESS(rc))
248 {
249 /*
250 * Register the saved state data unit.
251 */
252 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 /* .... failure .... */
271 }
272 MMR3Term(pVM);
273 return rc;
274}
275
276
277/**
278 * Initializes the MM parts which depends on PGM being initialized.
279 *
280 * @returns VBox status code.
281 * @param pVM The cross context VM structure.
282 * @remark No cleanup necessary since MMR3Term() will be called on failure.
283 */
284VMMR3DECL(int) MMR3InitPaging(PVM pVM)
285{
286 LogFlow(("MMR3InitPaging:\n"));
287
288 /*
289 * Query the CFGM values.
290 */
291 int rc;
292 PCFGMNODE pMMCfg = CFGMR3GetChild(CFGMR3GetRoot(pVM), "MM");
293 if (!pMMCfg)
294 {
295 rc = CFGMR3InsertNode(CFGMR3GetRoot(pVM), "MM", &pMMCfg);
296 AssertRCReturn(rc, rc);
297 }
298
299 /** @cfgm{/RamSize, uint64_t, 0, 16TB, 0}
300 * Specifies the size of the base RAM that is to be set up during
301 * VM initialization.
302 */
303 uint64_t cbRam;
304 rc = CFGMR3QueryU64(CFGMR3GetRoot(pVM), "RamSize", &cbRam);
305 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
306 cbRam = 0;
307 else
308 AssertMsgRCReturn(rc, ("Configuration error: Failed to query integer \"RamSize\", rc=%Rrc.\n", rc), rc);
309 AssertLogRelMsg(!(cbRam & ~X86_PTE_PAE_PG_MASK), ("%RGp X86_PTE_PAE_PG_MASK=%RX64\n", cbRam, X86_PTE_PAE_PG_MASK));
310 AssertLogRelMsgReturn(cbRam <= GMM_GCPHYS_LAST, ("cbRam=%RGp GMM_GCPHYS_LAST=%RX64\n", cbRam, GMM_GCPHYS_LAST), VERR_OUT_OF_RANGE);
311 cbRam &= X86_PTE_PAE_PG_MASK;
312 pVM->mm.s.cbRamBase = cbRam;
313
314 /** @cfgm{/RamHoleSize, uint32_t, 0, 4032MB, 512MB}
315 * Specifies the size of the memory hole. The memory hole is used
316 * to avoid mapping RAM to the range normally used for PCI memory regions.
317 * Must be aligned on a 4MB boundary. */
318 uint32_t cbRamHole;
319 rc = CFGMR3QueryU32Def(CFGMR3GetRoot(pVM), "RamHoleSize", &cbRamHole, MM_RAM_HOLE_SIZE_DEFAULT);
320 AssertLogRelMsgRCReturn(rc, ("Configuration error: Failed to query integer \"RamHoleSize\", rc=%Rrc.\n", rc), rc);
321 AssertLogRelMsgReturn(cbRamHole <= 4032U * _1M,
322 ("Configuration error: \"RamHoleSize\"=%#RX32 is too large.\n", cbRamHole), VERR_OUT_OF_RANGE);
323 AssertLogRelMsgReturn(cbRamHole > 16 * _1M,
324 ("Configuration error: \"RamHoleSize\"=%#RX32 is too large.\n", cbRamHole), VERR_OUT_OF_RANGE);
325 AssertLogRelMsgReturn(!(cbRamHole & (_4M - 1)),
326 ("Configuration error: \"RamHoleSize\"=%#RX32 is misaligned.\n", cbRamHole), VERR_OUT_OF_RANGE);
327 uint64_t const offRamHole = _4G - cbRamHole;
328 if (cbRam < offRamHole)
329 Log(("MM: %RU64 bytes of RAM\n", cbRam));
330 else
331 Log(("MM: %RU64 bytes of RAM with a hole at %RU64 up to 4GB.\n", cbRam, offRamHole));
332
333 /** @cfgm{/MM/Policy, string, no overcommitment}
334 * Specifies the policy to use when reserving memory for this VM. The recognized
335 * value is 'no overcommitment' (default). See GMMPOLICY.
336 */
337 GMMOCPOLICY enmOcPolicy;
338 char sz[64];
339 rc = CFGMR3QueryString(CFGMR3GetRoot(pVM), "Policy", sz, sizeof(sz));
340 if (RT_SUCCESS(rc))
341 {
342 if ( !RTStrICmp(sz, "no_oc")
343 || !RTStrICmp(sz, "no overcommitment"))
344 enmOcPolicy = GMMOCPOLICY_NO_OC;
345 else
346 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, "Unknown \"MM/Policy\" value \"%s\"", sz);
347 }
348 else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
349 enmOcPolicy = GMMOCPOLICY_NO_OC;
350 else
351 AssertMsgFailedReturn(("Configuration error: Failed to query string \"MM/Policy\", rc=%Rrc.\n", rc), rc);
352
353 /** @cfgm{/MM/Priority, string, normal}
354 * Specifies the memory priority of this VM. The priority comes into play when the
355 * system is overcommitted and the VMs needs to be milked for memory. The recognized
356 * values are 'low', 'normal' (default) and 'high'. See GMMPRIORITY.
357 */
358 GMMPRIORITY enmPriority;
359 rc = CFGMR3QueryString(CFGMR3GetRoot(pVM), "Priority", sz, sizeof(sz));
360 if (RT_SUCCESS(rc))
361 {
362 if (!RTStrICmp(sz, "low"))
363 enmPriority = GMMPRIORITY_LOW;
364 else if (!RTStrICmp(sz, "normal"))
365 enmPriority = GMMPRIORITY_NORMAL;
366 else if (!RTStrICmp(sz, "high"))
367 enmPriority = GMMPRIORITY_HIGH;
368 else
369 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, "Unknown \"MM/Priority\" value \"%s\"", sz);
370 }
371 else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
372 enmPriority = GMMPRIORITY_NORMAL;
373 else
374 AssertMsgFailedReturn(("Configuration error: Failed to query string \"MM/Priority\", rc=%Rrc.\n", rc), rc);
375
376 /*
377 * Make the initial memory reservation with GMM.
378 */
379 uint32_t const cbUma = _1M - 640*_1K;
380 uint64_t cBasePages = ((cbRam - cbUma) >> PAGE_SHIFT) + pVM->mm.s.cBasePages;
381 rc = GMMR3InitialReservation(pVM,
382 RT_MAX(cBasePages + pVM->mm.s.cHandyPages, 1),
383 RT_MAX(pVM->mm.s.cShadowPages, 1),
384 RT_MAX(pVM->mm.s.cFixedPages, 1),
385 enmOcPolicy,
386 enmPriority);
387 if (RT_FAILURE(rc))
388 {
389 if (rc == VERR_GMM_MEMORY_RESERVATION_DECLINED)
390 return VMSetError(pVM, rc, RT_SRC_POS,
391 N_("Insufficient free memory to start the VM (cbRam=%#RX64 enmOcPolicy=%d enmPriority=%d)"),
392 cbRam, enmOcPolicy, enmPriority);
393 return VMSetError(pVM, rc, RT_SRC_POS, "GMMR3InitialReservation(,%#RX64,0,0,%d,%d)",
394 cbRam >> PAGE_SHIFT, enmOcPolicy, enmPriority);
395 }
396
397 /*
398 * If RamSize is 0 we're done now.
399 */
400 if (cbRam < PAGE_SIZE)
401 {
402 Log(("MM: No RAM configured\n"));
403 return VINF_SUCCESS;
404 }
405
406 /*
407 * Setup the base ram (PGM).
408 */
409 pVM->mm.s.cbRamHole = cbRamHole;
410
411 /* First the conventional memory: */
412 rc = PGMR3PhysRegisterRam(pVM, 0, RT_MIN(cbRam, 640*_1K), "Conventional RAM");
413 if (RT_SUCCESS(rc))
414 {
415 /* The extended memory from 1MiB up to 4GiB: */
416 if (cbRam > offRamHole)
417 {
418 pVM->mm.s.cbRamBelow4GB = offRamHole;
419 rc = PGMR3PhysRegisterRam(pVM, _1M, offRamHole - _1M, "Extended RAM");
420 if (RT_SUCCESS(rc))
421 {
422 /* Then all the memory above 4GiB: */
423 pVM->mm.s.cbRamAbove4GB = cbRam - offRamHole;
424 rc = PGMR3PhysRegisterRam(pVM, _4G, cbRam - offRamHole, "Above 4GB Base RAM");
425 }
426 }
427 else
428 {
429 pVM->mm.s.cbRamBelow4GB = cbRam;
430 pVM->mm.s.cbRamAbove4GB = 0;
431 if (cbRam > _1M)
432 rc = PGMR3PhysRegisterRam(pVM, _1M, cbRam - _1M, "Extended RAM");
433 }
434 }
435
436 /*
437 * Enabled mmR3UpdateReservation here since we don't want the
438 * PGMR3PhysRegisterRam calls above mess things up.
439 */
440 pVM->mm.s.fDoneMMR3InitPaging = true;
441 AssertMsg(pVM->mm.s.cBasePages == cBasePages || RT_FAILURE(rc), ("%RX64 != %RX64\n", pVM->mm.s.cBasePages, cBasePages));
442
443 LogFlow(("MMR3InitPaging: returns %Rrc\n", rc));
444 return rc;
445}
446
447
448/**
449 * Terminates the MM.
450 *
451 * Termination means cleaning up and freeing all resources,
452 * the VM it self is at this point powered off or suspended.
453 *
454 * @returns VBox status code.
455 * @param pVM The cross context VM structure.
456 */
457VMMR3DECL(int) MMR3Term(PVM pVM)
458{
459 /*
460 * Clean up the hypervisor heap.
461 */
462 mmR3HyperTerm(pVM);
463
464 /*
465 * Zero stuff to detect after termination use of the MM interface
466 */
467 pVM->mm.s.offLookupHyper = NIL_OFFSET;
468 pVM->mm.s.pHyperHeapR3 = NULL; /* freed above. */
469 pVM->mm.s.pHyperHeapR0 = NIL_RTR0PTR; /* freed above. */
470 pVM->mm.s.pHyperHeapRC = NIL_RTRCPTR; /* freed above. */
471 pVM->mm.s.offVM = 0; /* init assertion on this */
472
473 return VINF_SUCCESS;
474}
475
476
477/**
478 * Terminates the UVM part of MM.
479 *
480 * Termination means cleaning up and freeing all resources,
481 * the VM it self is at this point powered off or suspended.
482 *
483 * @returns VBox status code.
484 * @param pUVM Pointer to the user mode VM structure.
485 */
486VMMR3DECL(void) MMR3TermUVM(PUVM pUVM)
487{
488 /*
489 * Destroy the heap.
490 */
491 mmR3HeapDestroy(pUVM->mm.s.pHeap);
492 pUVM->mm.s.pHeap = NULL;
493}
494
495
496/**
497 * Checks if the both VM and UVM parts of MM have been initialized.
498 *
499 * @returns true if initialized, false if not.
500 * @param pVM The cross context VM structure.
501 */
502VMMR3_INT_DECL(bool) MMR3IsInitialized(PVM pVM)
503{
504 return pVM->mm.s.pHyperHeapR3 != NULL;
505}
506
507
508/**
509 * Execute state save operation.
510 *
511 * @returns VBox status code.
512 * @param pVM The cross context VM structure.
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 The cross context VM structure.
530 * @param pSSM SSM operation handle.
531 * @param uVersion Data layout version.
532 * @param uPass The data pass.
533 */
534static DECLCALLBACK(int) mmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
535{
536 LogFlow(("mmR3Load:\n"));
537 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
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 cross context 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 cross context 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 cross context 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 cross context 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 cross context 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 code.
713 * @param pVM The cross context VM structure.
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#if 0
725 /*
726 * Try page tables.
727 */
728 int rc = MMPagePhys2PageTry(pVM, HCPhys, ppv);
729 if (RT_SUCCESS(rc))
730 return rc;
731#endif
732
733 /*
734 * Iterate thru the lookup records for HMA.
735 */
736 uint32_t off = HCPhys & PAGE_OFFSET_MASK;
737 HCPhys &= X86_PTE_PAE_PG_MASK;
738 PMMLOOKUPHYPER pCur = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.CTX_SUFF(pHyperHeap) + pVM->mm.s.offLookupHyper);
739 for (;;)
740 {
741 switch (pCur->enmType)
742 {
743 case MMLOOKUPHYPERTYPE_LOCKED:
744 {
745 PCRTHCPHYS paHCPhysPages = pCur->u.Locked.paHCPhysPages;
746 size_t iPage = pCur->cb >> PAGE_SHIFT;
747 while (iPage-- > 0)
748 if (paHCPhysPages[iPage] == HCPhys)
749 {
750 *ppv = (char *)pCur->u.Locked.pvR3 + (iPage << PAGE_SHIFT) + off;
751 return VINF_SUCCESS;
752 }
753 break;
754 }
755
756 case MMLOOKUPHYPERTYPE_HCPHYS:
757 if (pCur->u.HCPhys.HCPhys - HCPhys < pCur->cb)
758 {
759 *ppv = (uint8_t *)pCur->u.HCPhys.pvR3 + pCur->u.HCPhys.HCPhys - HCPhys + off;
760 return VINF_SUCCESS;
761 }
762 break;
763
764 case MMLOOKUPHYPERTYPE_GCPHYS: /* (for now we'll not allow these kind of conversions) */
765 case MMLOOKUPHYPERTYPE_MMIO2:
766 case MMLOOKUPHYPERTYPE_DYNAMIC:
767 break;
768
769 default:
770 AssertMsgFailed(("enmType=%d\n", pCur->enmType));
771 break;
772 }
773
774 /* next */
775 if (pCur->offNext == (int32_t)NIL_OFFSET)
776 break;
777 pCur = (PMMLOOKUPHYPER)((uint8_t *)pCur + pCur->offNext);
778 }
779 /* give up */
780 return VERR_INVALID_POINTER;
781}
782
783
784
785/**
786 * Get the size of the base RAM.
787 * This usually means the size of the first contiguous block of physical memory.
788 *
789 * @returns The guest base RAM size.
790 * @param pVM The cross context VM structure.
791 * @thread Any.
792 *
793 * @deprecated
794 */
795VMMR3DECL(uint64_t) MMR3PhysGetRamSize(PVM pVM)
796{
797 return pVM->mm.s.cbRamBase;
798}
799
800
801/**
802 * Get the size of RAM below 4GB (starts at address 0x00000000).
803 *
804 * @returns The amount of RAM below 4GB in bytes.
805 * @param pVM The cross context VM structure.
806 * @thread Any.
807 */
808VMMR3DECL(uint32_t) MMR3PhysGetRamSizeBelow4GB(PVM pVM)
809{
810 VM_ASSERT_VALID_EXT_RETURN(pVM, UINT32_MAX);
811 return pVM->mm.s.cbRamBelow4GB;
812}
813
814
815/**
816 * Get the size of RAM above 4GB (starts at address 0x000100000000).
817 *
818 * @returns The amount of RAM above 4GB in bytes.
819 * @param pVM The cross context VM structure.
820 * @thread Any.
821 */
822VMMR3DECL(uint64_t) MMR3PhysGetRamSizeAbove4GB(PVM pVM)
823{
824 VM_ASSERT_VALID_EXT_RETURN(pVM, UINT64_MAX);
825 return pVM->mm.s.cbRamAbove4GB;
826}
827
828
829/**
830 * Get the size of the RAM hole below 4GB.
831 *
832 * @returns Size in bytes.
833 * @param pVM The cross context VM structure.
834 * @thread Any.
835 */
836VMMR3DECL(uint32_t) MMR3PhysGet4GBRamHoleSize(PVM pVM)
837{
838 VM_ASSERT_VALID_EXT_RETURN(pVM, UINT32_MAX);
839 return pVM->mm.s.cbRamHole;
840}
841
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