VirtualBox

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

Last change on this file since 76553 was 76553, checked in by vboxsync, 5 years ago

scm --update-copyright-year

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

© 2023 Oracle
ContactPrivacy policyTerms of Use