VirtualBox

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

Last change on this file since 17522 was 17513, checked in by vboxsync, 16 years ago

MM,PGM: Fixed page reservation, include a full set of handy pages for the time being. Fixed page counting statistics.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 32.3 KB
Line 
1/* $Id: MM.cpp 17513 2009-03-07 05:44:48Z vboxsync $ */
2/** @file
3 * MM - Memory Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/** @page pg_mm MM - The Memory Manager
24 *
25 * The memory manager is in charge of the following memory:
26 * - Hypervisor Memory Area (HMA) - Address space management.
27 * - Hypervisor Heap - A memory heap that lives in all contexts.
28 * - Tagged ring-3 heap.
29 * - Page pools - Primarily used by PGM for shadow page tables.
30 * - Locked process memory - Guest RAM and other. (reduce/obsolete this)
31 * - Physical guest memory (RAM & ROM) - Moving to PGM. (obsolete this)
32 *
33 * The global memory manager (GMM) is the global counter part / partner of MM.
34 * MM will provide therefore ring-3 callable interfaces for some of the GMM APIs
35 * related to resource tracking (PGM is the user).
36 *
37 * @see grp_mm
38 *
39 *
40 * @section sec_mm_hma Hypervisor Memory Area
41 *
42 * The HMA is used when executing in raw-mode. We borrow, with the help of
43 * PGMMap, some unused space (one or more page directory entries to be precise)
44 * in the guest's virtual memory context. PGM will monitor the guest's virtual
45 * address space for changes and relocate the HMA when required.
46 *
47 * To give some idea what's in the HMA, study the 'info hma' output:
48 * @verbatim
49VBoxDbg> info hma
50Hypervisor Memory Area (HMA) Layout: Base 00000000a0000000, 0x00800000 bytes
5100000000a05cc000-00000000a05cd000 DYNAMIC fence
5200000000a05c4000-00000000a05cc000 DYNAMIC Dynamic mapping
5300000000a05c3000-00000000a05c4000 DYNAMIC fence
5400000000a05b8000-00000000a05c3000 DYNAMIC Paging
5500000000a05b6000-00000000a05b8000 MMIO2 0000000000000000 PCNetShMem
5600000000a0536000-00000000a05b6000 MMIO2 0000000000000000 VGA VRam
5700000000a0523000-00000000a0536000 00002aaab3d0c000 LOCKED autofree alloc once (PDM_DEVICE)
5800000000a0522000-00000000a0523000 DYNAMIC fence
5900000000a051e000-00000000a0522000 00002aaab36f5000 LOCKED autofree VBoxDD2GC.gc
6000000000a051d000-00000000a051e000 DYNAMIC fence
6100000000a04eb000-00000000a051d000 00002aaab36c3000 LOCKED autofree VBoxDDGC.gc
6200000000a04ea000-00000000a04eb000 DYNAMIC fence
6300000000a04e9000-00000000a04ea000 00002aaab36c2000 LOCKED autofree ram range (High ROM Region)
6400000000a04e8000-00000000a04e9000 DYNAMIC fence
6500000000a040e000-00000000a04e8000 00002aaab2e6d000 LOCKED autofree VMMGC.gc
6600000000a0208000-00000000a040e000 00002aaab2c67000 LOCKED autofree alloc once (PATM)
6700000000a01f7000-00000000a0208000 00002aaaab92d000 LOCKED autofree alloc once (SELM)
6800000000a01e7000-00000000a01f7000 00002aaaab5e8000 LOCKED autofree alloc once (SELM)
6900000000a01e6000-00000000a01e7000 DYNAMIC fence
7000000000a01e5000-00000000a01e6000 00002aaaab5e7000 HCPHYS 00000000c363c000 Core Code
7100000000a01e4000-00000000a01e5000 DYNAMIC fence
7200000000a01e3000-00000000a01e4000 00002aaaaab26000 HCPHYS 00000000619cf000 GIP
7300000000a01a2000-00000000a01e3000 00002aaaabf32000 LOCKED autofree alloc once (PGM_PHYS)
7400000000a016b000-00000000a01a2000 00002aaab233f000 LOCKED autofree alloc once (PGM_POOL)
7500000000a016a000-00000000a016b000 DYNAMIC fence
7600000000a0165000-00000000a016a000 DYNAMIC CR3 mapping
7700000000a0164000-00000000a0165000 DYNAMIC fence
7800000000a0024000-00000000a0164000 00002aaab215f000 LOCKED autofree Heap
7900000000a0023000-00000000a0024000 DYNAMIC fence
8000000000a0001000-00000000a0023000 00002aaab1d24000 LOCKED pages VM
8100000000a0000000-00000000a0001000 DYNAMIC fence
82 @endverbatim
83 *
84 *
85 * @section sec_mm_hyperheap Hypervisor Heap
86 *
87 * The heap is accessible from ring-3, ring-0 and the raw-mode context. That
88 * said, it's not necessarily mapped into ring-0 on if that's possible since we
89 * don't wish to waste kernel address space without a good reason.
90 *
91 * Allocations within the heap are always in the same relative position in all
92 * contexts, so, it's possible to use offset based linking. In fact, the heap is
93 * internally using offset based linked lists tracking heap blocks. We use
94 * offset linked AVL trees and lists in a lot of places where share structures
95 * between RC, R3 and R0, so this is a strict requirement of the heap. However
96 * this means that we cannot easily extend the heap since the extension won't
97 * necessarily be in the continuation of the current heap memory in all (or any)
98 * context.
99 *
100 * All allocations are tagged. Per tag allocation statistics will be maintaing
101 * and exposed thru STAM when VBOX_WITH_STATISTICS is defined.
102 *
103 *
104 * @section sec_mm_r3heap Tagged Ring-3 Heap
105 *
106 * The ring-3 heap is a wrapper around the RTMem API adding allocation
107 * statistics and automatic cleanup on VM destruction.
108 *
109 * Per tag allocation statistics will be maintaing and exposed thru STAM when
110 * VBOX_WITH_STATISTICS is defined.
111 *
112 *
113 * @section sec_mm_page Page Pool
114 *
115 * The MM manages a page pool from which other components can allocate locked,
116 * page aligned and page sized memory objects. The pool provides facilities to
117 * convert back and forth between (host) physical and virtual addresses (within
118 * the pool of course). Several specialized interfaces are provided for the most
119 * common alloctions and convertions to save the caller from bothersome casting
120 * and extra parameter passing.
121 *
122 *
123 * @section sec_mm_locked Locked Process Memory
124 *
125 * MM manages the locked process memory. This is used for a bunch of things
126 * (count the LOCKED entries in the'info hma' output found in @ref sec_mm_hma),
127 * but the main consumer of memory is currently for guest RAM. There is an
128 * ongoing rewrite that will move all the guest RAM allocation to PGM and
129 * GMM.
130 *
131 * The locking of memory is something doing in cooperation with the VirtualBox
132 * support driver, SUPDrv (aka. VBoxDrv), thru the support library API,
133 * SUPR3 (aka. SUPLib).
134 *
135 *
136 * @section sec_mm_phys Physical Guest Memory
137 *
138 * MM is currently managing the physical memory for the guest. It relies heavily
139 * on PGM for this. There is an ongoing rewrite that will move this to PGM. (The
140 * rewrite is driven by the need for more flexible guest ram allocation, but
141 * also motivated by the fact that MMPhys is just adding stupid bureaucracy and
142 * that MMR3PhysReserve is a totally weird artifact that must go away.)
143 *
144 */
145
146
147/*******************************************************************************
148* Header Files *
149*******************************************************************************/
150#define LOG_GROUP LOG_GROUP_MM
151#include <VBox/mm.h>
152#include <VBox/pgm.h>
153#include <VBox/cfgm.h>
154#include <VBox/ssm.h>
155#include <VBox/gmm.h>
156#include "MMInternal.h"
157#include <VBox/vm.h>
158#include <VBox/uvm.h>
159#include <VBox/err.h>
160#include <VBox/param.h>
161
162#include <VBox/log.h>
163#include <iprt/alloc.h>
164#include <iprt/assert.h>
165#include <iprt/string.h>
166
167
168/*******************************************************************************
169* Defined Constants And Macros *
170*******************************************************************************/
171/** The current saved state versino of MM. */
172#define MM_SAVED_STATE_VERSION 2
173
174
175/*******************************************************************************
176* Internal Functions *
177*******************************************************************************/
178static DECLCALLBACK(int) mmR3Save(PVM pVM, PSSMHANDLE pSSM);
179static DECLCALLBACK(int) mmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version);
180
181
182
183
184/**
185 * Initializes the MM members of the UVM.
186 *
187 * This is currently only the ring-3 heap.
188 *
189 * @returns VBox status code.
190 * @param pUVM Pointer to the user mode VM structure.
191 */
192VMMR3DECL(int) MMR3InitUVM(PUVM pUVM)
193{
194 /*
195 * Assert sizes and order.
196 */
197 AssertCompile(sizeof(pUVM->mm.s) <= sizeof(pUVM->mm.padding));
198 AssertRelease(sizeof(pUVM->mm.s) <= sizeof(pUVM->mm.padding));
199 Assert(!pUVM->mm.s.pHeap);
200
201 /*
202 * Init the heap.
203 */
204 return mmR3HeapCreateU(pUVM, &pUVM->mm.s.pHeap);
205}
206
207
208/**
209 * Initializes the MM.
210 *
211 * MM is managing the virtual address space (among other things) and
212 * setup the hypvervisor memory area mapping in the VM structure and
213 * the hypvervisor alloc-only-heap. Assuming the current init order
214 * and components the hypvervisor memory area looks like this:
215 * -# VM Structure.
216 * -# Hypervisor alloc only heap (also call Hypervisor memory region).
217 * -# Core code.
218 *
219 * MM determins the virtual address of the hypvervisor 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 VM to operate on.
225 */
226VMMR3DECL(int) MMR3Init(PVM pVM)
227{
228 LogFlow(("MMR3Init\n"));
229
230 /*
231 * Assert alignment, sizes and order.
232 */
233 AssertRelease(!(RT_OFFSETOF(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_OFFSETOF(VM, mm);
241 pVM->mm.s.offLookupHyper = NIL_OFFSET;
242
243 /*
244 * Init the page pool.
245 */
246 int rc = mmR3PagePoolInit(pVM);
247 if (RT_SUCCESS(rc))
248 {
249 /*
250 * Init the hypervisor related stuff.
251 */
252 rc = mmR3HyperInit(pVM);
253 if (RT_SUCCESS(rc))
254 {
255 /*
256 * Register the saved state data unit.
257 */
258 rc = SSMR3RegisterInternal(pVM, "mm", 1, MM_SAVED_STATE_VERSION, sizeof(uint32_t) * 2,
259 NULL, mmR3Save, NULL,
260 NULL, mmR3Load, NULL);
261 if (RT_SUCCESS(rc))
262 return rc;
263
264 /* .... failure .... */
265 }
266 }
267 MMR3Term(pVM);
268 return rc;
269}
270
271
272/**
273 * Initializes the MM parts which depends on PGM being initialized.
274 *
275 * @returns VBox status code.
276 * @param pVM The VM to operate on.
277 * @remark No cleanup necessary since MMR3Term() will be called on failure.
278 */
279VMMR3DECL(int) MMR3InitPaging(PVM pVM)
280{
281 LogFlow(("MMR3InitPaging:\n"));
282
283 /*
284 * Query the CFGM values.
285 */
286 int rc;
287 PCFGMNODE pMMCfg = CFGMR3GetChild(CFGMR3GetRoot(pVM), "MM");
288 if (!pMMCfg)
289 {
290 rc = CFGMR3InsertNode(CFGMR3GetRoot(pVM), "MM", &pMMCfg);
291 AssertRCReturn(rc, rc);
292 }
293
294 /** @cfgm{RamPreAlloc, boolean, false}
295 * Indicates whether the base RAM should all be allocated before starting
296 * the VM (default), or if it should be allocated when first written to.
297 */
298 bool fPreAlloc;
299 rc = CFGMR3QueryBool(CFGMR3GetRoot(pVM), "RamPreAlloc", &fPreAlloc);
300 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
301 fPreAlloc = false;
302 else
303 AssertMsgRCReturn(rc, ("Configuration error: Failed to query integer \"RamPreAlloc\", rc=%Rrc.\n", rc), rc);
304
305 /** @cfgm{RamSize, uint64_t, 0, 0, UINT64_MAX}
306 * Specifies the size of the base RAM that is to be set up during
307 * VM initialization.
308 */
309 uint64_t cbRam;
310 rc = CFGMR3QueryU64(CFGMR3GetRoot(pVM), "RamSize", &cbRam);
311 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
312 cbRam = 0;
313 else
314 AssertMsgRCReturn(rc, ("Configuration error: Failed to query integer \"RamSize\", rc=%Rrc.\n", rc), rc);
315
316 cbRam &= X86_PTE_PAE_PG_MASK;
317 pVM->mm.s.cbRamBase = cbRam; /* Warning: don't move this code to MMR3Init without fixing REMR3Init. */
318 Log(("MM: %RU64 bytes of RAM%s\n", cbRam, fPreAlloc ? " (PreAlloc)" : ""));
319
320 /** @cfgm{MM/Policy, string, no overcommitment}
321 * Specifies the policy to use when reserving memory for this VM. The recognized
322 * value is 'no overcommitment' (default). See GMMPOLICY.
323 */
324 GMMOCPOLICY enmOcPolicy;
325 char sz[64];
326 rc = CFGMR3QueryString(CFGMR3GetRoot(pVM), "Policy", sz, sizeof(sz));
327 if (RT_SUCCESS(rc))
328 {
329 if ( !RTStrICmp(sz, "no_oc")
330 || !RTStrICmp(sz, "no overcommitment"))
331 enmOcPolicy = GMMOCPOLICY_NO_OC;
332 else
333 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, "Unknown \"MM/Policy\" value \"%s\"", sz);
334 }
335 else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
336 enmOcPolicy = GMMOCPOLICY_NO_OC;
337 else
338 AssertMsgRCReturn(rc, ("Configuration error: Failed to query string \"MM/Policy\", rc=%Rrc.\n", rc), rc);
339
340 /** @cfgm{MM/Priority, string, normal}
341 * Specifies the memory priority of this VM. The priority comes into play when the
342 * system is overcommitted and the VMs needs to be milked for memory. The recognized
343 * values are 'low', 'normal' (default) and 'high'. See GMMPRIORITY.
344 */
345 GMMPRIORITY enmPriority;
346 rc = CFGMR3QueryString(CFGMR3GetRoot(pVM), "Priority", sz, sizeof(sz));
347 if (RT_SUCCESS(rc))
348 {
349 if (!RTStrICmp(sz, "low"))
350 enmPriority = GMMPRIORITY_LOW;
351 else if (!RTStrICmp(sz, "normal"))
352 enmPriority = GMMPRIORITY_NORMAL;
353 else if (!RTStrICmp(sz, "high"))
354 enmPriority = GMMPRIORITY_HIGH;
355 else
356 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, "Unknown \"MM/Priority\" value \"%s\"", sz);
357 }
358 else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
359 enmPriority = GMMPRIORITY_NORMAL;
360 else
361 AssertMsgRCReturn(rc, ("Configuration error: Failed to query string \"MM/Priority\", rc=%Rrc.\n", rc), rc);
362
363 /*
364 * Make the initial memory reservation with GMM.
365 */
366 uint64_t cBasePages = (cbRam >> PAGE_SHIFT) + pVM->mm.s.cBasePages;
367 rc = GMMR3InitialReservation(pVM,
368 RT_MAX(cBasePages + pVM->mm.s.cHandyPages, 1),
369 RT_MAX(pVM->mm.s.cShadowPages, 1),
370 RT_MAX(pVM->mm.s.cFixedPages, 1),
371 enmOcPolicy,
372 enmPriority);
373 if (RT_FAILURE(rc))
374 {
375 if (rc == VERR_GMM_MEMORY_RESERVATION_DECLINED)
376 return VMSetError(pVM, rc, RT_SRC_POS,
377 N_("Insufficient free memory to start the VM (cbRam=%#RX64 enmOcPolicy=%d enmPriority=%d)"),
378 cbRam, enmOcPolicy, enmPriority);
379 return VMSetError(pVM, rc, RT_SRC_POS, "GMMR3InitialReservation(,%#RX64,0,0,%d,%d)",
380 cbRam >> PAGE_SHIFT, enmOcPolicy, enmPriority);
381 }
382
383 /*
384 * If RamSize is 0 we're done now.
385 */
386 if (cbRam < PAGE_SIZE)
387 {
388 Log(("MM: No RAM configured\n"));
389 return VINF_SUCCESS;
390 }
391
392 /*
393 * Setup the base ram (PGM).
394 */
395 rc = PGMR3PhysRegisterRam(pVM, 0, cbRam, "Base RAM");
396#ifdef VBOX_WITH_NEW_PHYS_CODE
397 if (RT_SUCCESS(rc) && fPreAlloc)
398 {
399 /** @todo RamPreAlloc should be handled at the very end of the VM creation. (lazy bird) */
400 return VM_SET_ERROR(pVM, VERR_NOT_IMPLEMENTED, "TODO: RamPreAlloc");
401 }
402#else
403 if (RT_SUCCESS(rc))
404 {
405 /*
406 * Allocate the first chunk, as we'll map ROM ranges there.
407 * If requested, allocated the rest too.
408 */
409 RTGCPHYS GCPhys = (RTGCPHYS)0;
410 rc = PGM3PhysGrowRange(pVM, &GCPhys);
411 if (RT_SUCCESS(rc) && fPreAlloc)
412 for (GCPhys = PGM_DYNAMIC_CHUNK_SIZE;
413 GCPhys < cbRam && RT_SUCCESS(rc);
414 GCPhys += PGM_DYNAMIC_CHUNK_SIZE)
415 rc = PGM3PhysGrowRange(pVM, &GCPhys);
416 }
417#endif
418
419#ifdef VBOX_WITH_NEW_PHYS_CODE
420 /*
421 * Enabled mmR3UpdateReservation here since we don't want the
422 * PGMR3PhysRegisterRam calls above mess things up.
423 */
424 pVM->mm.s.fDoneMMR3InitPaging = true;
425 AssertMsg(pVM->mm.s.cBasePages == cBasePages, ("%RX64 != %RX64\n", pVM->mm.s.cBasePages, cBasePages));
426#endif
427
428 LogFlow(("MMR3InitPaging: returns %Rrc\n", rc));
429 return rc;
430}
431
432
433/**
434 * Terminates the MM.
435 *
436 * Termination means cleaning up and freeing all resources,
437 * the VM it self is at this point powered off or suspended.
438 *
439 * @returns VBox status code.
440 * @param pVM The VM to operate on.
441 */
442VMMR3DECL(int) MMR3Term(PVM pVM)
443{
444 /*
445 * Destroy the page pool. (first as it used the hyper heap)
446 */
447 mmR3PagePoolTerm(pVM);
448
449 /*
450 * Release locked memory.
451 * (Associated record are released by the heap.)
452 */
453 PMMLOCKEDMEM pLockedMem = pVM->mm.s.pLockedMem;
454 while (pLockedMem)
455 {
456 int rc = SUPPageUnlock(pLockedMem->pv);
457 AssertMsgRC(rc, ("SUPPageUnlock(%p) -> rc=%d\n", pLockedMem->pv, rc));
458 switch (pLockedMem->eType)
459 {
460 case MM_LOCKED_TYPE_HYPER:
461 rc = SUPPageFree(pLockedMem->pv, pLockedMem->cb >> PAGE_SHIFT);
462 AssertMsgRC(rc, ("SUPPageFree(%p) -> rc=%d\n", pLockedMem->pv, rc));
463 break;
464 case MM_LOCKED_TYPE_HYPER_NOFREE:
465 case MM_LOCKED_TYPE_HYPER_PAGES:
466 case MM_LOCKED_TYPE_PHYS:
467 /* nothing to do. */
468 break;
469 }
470 /* next */
471 pLockedMem = pLockedMem->pNext;
472 }
473
474 /*
475 * Zero stuff to detect after termination use of the MM interface
476 */
477 pVM->mm.s.offLookupHyper = NIL_OFFSET;
478 pVM->mm.s.pLockedMem = NULL;
479 pVM->mm.s.pHyperHeapR3 = NULL; /* freed above. */
480 pVM->mm.s.pHyperHeapR0 = NIL_RTR0PTR; /* freed above. */
481 pVM->mm.s.pHyperHeapRC = NIL_RTRCPTR; /* freed above. */
482 pVM->mm.s.offVM = 0; /* init assertion on this */
483
484 return VINF_SUCCESS;
485}
486
487
488/**
489 * Terminates the UVM part of MM.
490 *
491 * Termination means cleaning up and freeing all resources,
492 * the VM it self is at this point powered off or suspended.
493 *
494 * @returns VBox status code.
495 * @param pUVM Pointer to the user mode VM structure.
496 */
497VMMR3DECL(void) MMR3TermUVM(PUVM pUVM)
498{
499 /*
500 * Destroy the heap.
501 */
502 mmR3HeapDestroy(pUVM->mm.s.pHeap);
503 pUVM->mm.s.pHeap = NULL;
504}
505
506
507/**
508 * Reset notification.
509 *
510 * MM will reload shadow ROMs into RAM at this point and make
511 * the ROM writable.
512 *
513 * @param pVM The VM handle.
514 */
515VMMR3DECL(void) MMR3Reset(PVM pVM)
516{
517#ifndef VBOX_WITH_NEW_PHYS_CODE
518 mmR3PhysRomReset(pVM);
519#endif
520}
521
522
523/**
524 * Execute state save operation.
525 *
526 * @returns VBox status code.
527 * @param pVM VM Handle.
528 * @param pSSM SSM operation handle.
529 */
530static DECLCALLBACK(int) mmR3Save(PVM pVM, PSSMHANDLE pSSM)
531{
532 LogFlow(("mmR3Save:\n"));
533
534 /* (PGM saves the physical memory.) */
535 SSMR3PutU64(pSSM, pVM->mm.s.cBasePages);
536 return SSMR3PutU64(pSSM, pVM->mm.s.cbRamBase);
537}
538
539
540/**
541 * Execute state load operation.
542 *
543 * @returns VBox status code.
544 * @param pVM VM Handle.
545 * @param pSSM SSM operation handle.
546 * @param u32Version Data layout version.
547 */
548static DECLCALLBACK(int) mmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version)
549{
550 LogFlow(("mmR3Load:\n"));
551
552 /*
553 * Validate version.
554 */
555 if ( SSM_VERSION_MAJOR_CHANGED(u32Version, MM_SAVED_STATE_VERSION)
556 || !u32Version)
557 {
558 AssertMsgFailed(("mmR3Load: Invalid version u32Version=%d!\n", u32Version));
559 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
560 }
561
562 /*
563 * Check the cBasePages and cbRamBase values.
564 */
565 int rc;
566 RTUINT cb1;
567
568 /* cBasePages */
569 uint64_t cPages;
570 if (u32Version != 1)
571 rc = SSMR3GetU64(pSSM, &cPages);
572 else
573 {
574 rc = SSMR3GetUInt(pSSM, &cb1);
575 cPages = cb1 >> PAGE_SHIFT;
576 }
577 if (RT_FAILURE(rc))
578 return rc;
579 if (cPages != pVM->mm.s.cBasePages)
580 {
581 LogRel(("mmR3Load: Memory configuration has changed. cPages=%#RX64 saved=%#RX64\n", pVM->mm.s.cBasePages, cPages));
582 return VERR_SSM_LOAD_MEMORY_SIZE_MISMATCH;
583 }
584
585 /* cbRamBase */
586 uint64_t cb;
587 if (u32Version != 1)
588 rc = SSMR3GetU64(pSSM, &cb);
589 else
590 {
591 rc = SSMR3GetUInt(pSSM, &cb1);
592 cb = cb1;
593 }
594 if (RT_FAILURE(rc))
595 return rc;
596 if (cb != pVM->mm.s.cbRamBase)
597 {
598 LogRel(("mmR3Load: Memory configuration has changed. cbRamBase=%#RX64 save=%#RX64\n", pVM->mm.s.cbRamBase, cb));
599 return VERR_SSM_LOAD_MEMORY_SIZE_MISMATCH;
600 }
601
602 /* (PGM restores the physical memory.) */
603 return rc;
604}
605
606
607/**
608 * Updates GMM with memory reservation changes.
609 *
610 * Called when MM::cbRamRegistered, MM::cShadowPages or MM::cFixedPages changes.
611 *
612 * @returns VBox status code - see GMMR0UpdateReservation.
613 * @param pVM The shared VM structure.
614 */
615int mmR3UpdateReservation(PVM pVM)
616{
617 VM_ASSERT_EMT(pVM);
618 if (pVM->mm.s.fDoneMMR3InitPaging)
619 return GMMR3UpdateReservation(pVM,
620 RT_MAX(pVM->mm.s.cBasePages + pVM->mm.s.cHandyPages, 1),
621 RT_MAX(pVM->mm.s.cShadowPages, 1),
622 RT_MAX(pVM->mm.s.cFixedPages, 1));
623 return VINF_SUCCESS;
624}
625
626
627/**
628 * Interface for PGM to increase the reservation of RAM and ROM pages.
629 *
630 * This can be called before MMR3InitPaging.
631 *
632 * @returns VBox status code. Will set VM error on failure.
633 * @param pVM The shared VM structure.
634 * @param cAddBasePages The number of pages to add.
635 */
636VMMR3DECL(int) MMR3IncreaseBaseReservation(PVM pVM, uint64_t cAddBasePages)
637{
638 uint64_t cOld = pVM->mm.s.cBasePages;
639 pVM->mm.s.cBasePages += cAddBasePages;
640 LogFlow(("MMR3IncreaseBaseReservation: +%RU64 (%RU64 -> %RU64\n", cAddBasePages, cOld, pVM->mm.s.cBasePages));
641 int rc = mmR3UpdateReservation(pVM);
642 if (RT_FAILURE(rc))
643 {
644 VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserved physical memory for the RAM (%#RX64 -> %#RX64 + %#RX32)"),
645 cOld, pVM->mm.s.cBasePages, pVM->mm.s.cHandyPages);
646 pVM->mm.s.cBasePages = cOld;
647 }
648 return rc;
649}
650
651
652/**
653 * Interface for PGM to make reservations for handy pages in addition to the
654 * base memory.
655 *
656 * This can be called before MMR3InitPaging.
657 *
658 * @returns VBox status code. Will set VM error on failure.
659 * @param pVM The shared VM structure.
660 * @param cHandyPages The number of handy pages.
661 */
662VMMR3DECL(int) MMR3ReserveHandyPages(PVM pVM, uint32_t cHandyPages)
663{
664 AssertReturn(!pVM->mm.s.cHandyPages, VERR_WRONG_ORDER);
665
666 pVM->mm.s.cHandyPages = cHandyPages;
667 LogFlow(("MMR3ReserveHandyPages: %RU32 (base %RU64)\n", pVM->mm.s.cHandyPages, pVM->mm.s.cBasePages));
668 int rc = mmR3UpdateReservation(pVM);
669 if (RT_FAILURE(rc))
670 {
671 VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserved physical memory for the RAM (%#RX64 + %#RX32)"),
672 pVM->mm.s.cBasePages, pVM->mm.s.cHandyPages);
673 pVM->mm.s.cHandyPages = 0;
674 }
675 return rc;
676}
677
678
679/**
680 * Interface for PGM to adjust the reservation of fixed pages.
681 *
682 * This can be called before MMR3InitPaging.
683 *
684 * @returns VBox status code. Will set VM error on failure.
685 * @param pVM The shared VM structure.
686 * @param cDeltaFixedPages The number of pages to add (positive) or subtract (negative).
687 * @param pszDesc Some description associated with the reservation.
688 */
689VMMR3DECL(int) MMR3AdjustFixedReservation(PVM pVM, int32_t cDeltaFixedPages, const char *pszDesc)
690{
691 const uint32_t cOld = pVM->mm.s.cFixedPages;
692 pVM->mm.s.cFixedPages += cDeltaFixedPages;
693 LogFlow(("MMR3AdjustFixedReservation: %d (%u -> %u)\n", cDeltaFixedPages, cOld, pVM->mm.s.cFixedPages));
694 int rc = mmR3UpdateReservation(pVM);
695 if (RT_FAILURE(rc))
696 {
697 VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserve physical memory (%#x -> %#x; %s)"),
698 cOld, pVM->mm.s.cFixedPages, pszDesc);
699 pVM->mm.s.cFixedPages = cOld;
700 }
701 return rc;
702}
703
704
705/**
706 * Interface for PGM to update the reservation of shadow pages.
707 *
708 * This can be called before MMR3InitPaging.
709 *
710 * @returns VBox status code. Will set VM error on failure.
711 * @param pVM The shared VM structure.
712 * @param cShadowPages The new page count.
713 */
714VMMR3DECL(int) MMR3UpdateShadowReservation(PVM pVM, uint32_t cShadowPages)
715{
716 const uint32_t cOld = pVM->mm.s.cShadowPages;
717 pVM->mm.s.cShadowPages = cShadowPages;
718 LogFlow(("MMR3UpdateShadowReservation: %u -> %u\n", cOld, pVM->mm.s.cShadowPages));
719 int rc = mmR3UpdateReservation(pVM);
720 if (RT_FAILURE(rc))
721 {
722 VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserve physical memory for shadow page tables (%#x -> %#x)"), cOld, pVM->mm.s.cShadowPages);
723 pVM->mm.s.cShadowPages = cOld;
724 }
725 return rc;
726}
727
728
729/**
730 * Locks physical memory which backs a virtual memory range (HC) adding
731 * the required records to the pLockedMem list.
732 *
733 * @returns VBox status code.
734 * @param pVM The VM handle.
735 * @param pv Pointer to memory range which shall be locked down.
736 * This pointer is page aligned.
737 * @param cb Size of memory range (in bytes). This size is page aligned.
738 * @param eType Memory type.
739 * @param ppLockedMem Where to store the pointer to the created locked memory record.
740 * This is optional, pass NULL if not used.
741 * @param fSilentFailure Don't raise an error when unsuccessful. Upper layer with deal with it.
742 */
743int mmR3LockMem(PVM pVM, void *pv, size_t cb, MMLOCKEDTYPE eType, PMMLOCKEDMEM *ppLockedMem, bool fSilentFailure)
744{
745 Assert(RT_ALIGN_P(pv, PAGE_SIZE) == pv);
746 Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb);
747
748 if (ppLockedMem)
749 *ppLockedMem = NULL;
750
751 /*
752 * Allocate locked mem structure.
753 */
754 unsigned cPages = (unsigned)(cb >> PAGE_SHIFT);
755 AssertReturn(cPages == (cb >> PAGE_SHIFT), VERR_OUT_OF_RANGE);
756 PMMLOCKEDMEM pLockedMem = (PMMLOCKEDMEM)MMR3HeapAlloc(pVM, MM_TAG_MM, RT_OFFSETOF(MMLOCKEDMEM, aPhysPages[cPages]));
757 if (!pLockedMem)
758 return VERR_NO_MEMORY;
759 pLockedMem->pv = pv;
760 pLockedMem->cb = cb;
761 pLockedMem->eType = eType;
762 memset(&pLockedMem->u, 0, sizeof(pLockedMem->u));
763
764 /*
765 * Lock the memory.
766 */
767 int rc = SUPPageLock(pv, cPages, &pLockedMem->aPhysPages[0]);
768 if (RT_SUCCESS(rc))
769 {
770 /*
771 * Setup the reserved field.
772 */
773 PSUPPAGE pPhysPage = &pLockedMem->aPhysPages[0];
774 for (unsigned c = cPages; c > 0; c--, pPhysPage++)
775 pPhysPage->uReserved = (RTHCUINTPTR)pLockedMem;
776
777 /*
778 * Insert into the list.
779 *
780 * ASSUME no protected needed here as only one thread in the system can possibly
781 * be doing this. No other threads will walk this list either we assume.
782 */
783 pLockedMem->pNext = pVM->mm.s.pLockedMem;
784 pVM->mm.s.pLockedMem = pLockedMem;
785 /* Set return value. */
786 if (ppLockedMem)
787 *ppLockedMem = pLockedMem;
788 }
789 else
790 {
791 AssertMsgFailed(("SUPPageLock failed with rc=%d\n", rc));
792 MMR3HeapFree(pLockedMem);
793 if (!fSilentFailure)
794 rc = VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to lock %d bytes of host memory (out of memory)"), cb);
795 }
796
797 return rc;
798}
799
800
801/**
802 * Maps a part of or an entire locked memory region into the guest context.
803 *
804 * @returns VBox status.
805 * God knows what happens if we fail...
806 * @param pVM VM handle.
807 * @param pLockedMem Locked memory structure.
808 * @param Addr GC Address where to start the mapping.
809 * @param iPage Page number in the locked memory region.
810 * @param cPages Number of pages to map.
811 * @param fFlags See the fFlags argument of PGR3Map().
812 */
813int mmR3MapLocked(PVM pVM, PMMLOCKEDMEM pLockedMem, RTGCPTR Addr, unsigned iPage, size_t cPages, unsigned fFlags)
814{
815 /*
816 * Adjust ~0 argument
817 */
818 if (cPages == ~(size_t)0)
819 cPages = (pLockedMem->cb >> PAGE_SHIFT) - iPage;
820 Assert(cPages != ~0U);
821 /* no incorrect arguments are accepted */
822 Assert(RT_ALIGN_GCPT(Addr, PAGE_SIZE, RTGCPTR) == Addr);
823 AssertMsg(iPage < (pLockedMem->cb >> PAGE_SHIFT), ("never even think about giving me a bad iPage(=%d)\n", iPage));
824 AssertMsg(iPage + cPages <= (pLockedMem->cb >> PAGE_SHIFT), ("never even think about giving me a bad cPages(=%d)\n", cPages));
825
826 /*
827 * Map the pages.
828 */
829 PSUPPAGE pPhysPage = &pLockedMem->aPhysPages[iPage];
830 while (cPages)
831 {
832 RTHCPHYS HCPhys = pPhysPage->Phys;
833 int rc = PGMMap(pVM, Addr, HCPhys, PAGE_SIZE, fFlags);
834 if (RT_FAILURE(rc))
835 {
836 /** @todo how the hell can we do a proper bailout here. */
837 return rc;
838 }
839
840 /* next */
841 cPages--;
842 iPage++;
843 pPhysPage++;
844 Addr += PAGE_SIZE;
845 }
846
847 return VINF_SUCCESS;
848}
849
850
851/**
852 * Convert HC Physical address to HC Virtual address.
853 *
854 * @returns VBox status.
855 * @param pVM VM handle.
856 * @param HCPhys The host context virtual address.
857 * @param ppv Where to store the resulting address.
858 * @thread The Emulation Thread.
859 *
860 * @remarks Avoid whenever possible.
861 * Intended for the debugger facility only.
862 * @todo Rename to indicate the special usage.
863 */
864VMMR3DECL(int) MMR3HCPhys2HCVirt(PVM pVM, RTHCPHYS HCPhys, void **ppv)
865{
866 /*
867 * Try page tables.
868 */
869 int rc = MMPagePhys2PageTry(pVM, HCPhys, ppv);
870 if (RT_SUCCESS(rc))
871 return rc;
872
873 /*
874 * Iterate the locked memory - very slow.
875 */
876 uint32_t off = HCPhys & PAGE_OFFSET_MASK;
877 HCPhys &= X86_PTE_PAE_PG_MASK;
878 for (PMMLOCKEDMEM pCur = pVM->mm.s.pLockedMem; pCur; pCur = pCur->pNext)
879 {
880 size_t iPage = pCur->cb >> PAGE_SHIFT;
881 while (iPage-- > 0)
882 if ((pCur->aPhysPages[iPage].Phys & X86_PTE_PAE_PG_MASK) == HCPhys)
883 {
884 *ppv = (char *)pCur->pv + (iPage << PAGE_SHIFT) + off;
885 return VINF_SUCCESS;
886 }
887 }
888 /* give up */
889 return VERR_INVALID_POINTER;
890}
891
892
893/**
894 * Read memory from GC virtual address using the current guest CR3.
895 *
896 * @returns VBox status.
897 * @param pVM VM handle.
898 * @param pvDst Destination address (HC of course).
899 * @param GCPtr GC virtual address.
900 * @param cb Number of bytes to read.
901 *
902 * @remarks Intended for the debugger facility only.
903 * @todo Move to DBGF, it's only selecting which functions to use!
904 */
905VMMR3DECL(int) MMR3ReadGCVirt(PVM pVM, void *pvDst, RTGCPTR GCPtr, size_t cb)
906{
907 if (GCPtr - pVM->mm.s.pvHyperAreaGC < pVM->mm.s.cbHyperArea)
908 return MMR3HyperReadGCVirt(pVM, pvDst, GCPtr, cb);
909 return PGMPhysSimpleReadGCPtr(pVM, pvDst, GCPtr, cb);
910}
911
912
913/**
914 * Write to memory at GC virtual address translated using the current guest CR3.
915 *
916 * @returns VBox status.
917 * @param pVM VM handle.
918 * @param GCPtrDst GC virtual address.
919 * @param pvSrc The source address (HC of course).
920 * @param cb Number of bytes to read.
921 *
922 * @remarks Intended for the debugger facility only.
923 * @todo Move to DBGF, it's only selecting which functions to use!
924 */
925VMMR3DECL(int) MMR3WriteGCVirt(PVM pVM, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
926{
927 if (GCPtrDst - pVM->mm.s.pvHyperAreaGC < pVM->mm.s.cbHyperArea)
928 return VERR_ACCESS_DENIED;
929 return PGMPhysSimpleWriteGCPtr(pVM, GCPtrDst, pvSrc, cb);
930}
931
932
933/**
934 * Get the size of the base RAM.
935 * This usually means the size of the first contigous block of physical memory.
936 *
937 * @returns The guest base RAM size.
938 * @param pVM The VM handle.
939 * @thread Any.
940 *
941 * @deprecated
942 */
943VMMR3DECL(uint64_t) MMR3PhysGetRamSize(PVM pVM)
944{
945 return pVM->mm.s.cbRamBase;
946}
947
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