VirtualBox

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

Last change on this file since 6817 was 6817, checked in by vboxsync, 17 years ago

Fixed to reservation update code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 25.1 KB
Line 
1/* $Id: MM.cpp 6817 2008-02-05 21:51:06Z vboxsync $ */
2/** @file
3 * MM - Memory Monitor(/Manager).
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 Monitor/Manager
20 *
21 * WARNING: THIS IS SOMEWHAT OUTDATED!
22 *
23 * It seems like this is going to be the entity taking care of memory allocations
24 * and the locking of physical memory for a VM. MM will track these allocations and
25 * pinnings so pointer conversions, memory read and write, and correct clean up can
26 * be done.
27 *
28 * Memory types:
29 * - Hypervisor Memory Area (HMA).
30 * - Page tables.
31 * - Physical pages.
32 *
33 * The first two types are not accessible using the generic conversion functions
34 * for GC memory, there are special functions for these.
35 *
36 *
37 * A decent structure for this component need to be eveloped as we see usage. One
38 * or two rewrites is probabaly needed to get it right...
39 *
40 *
41 *
42 * @section Hypervisor Memory Area
43 *
44 * The hypervisor is give 4MB of space inside the guest, we assume that we can
45 * steal an page directory entry from the guest OS without cause trouble. In
46 * addition to these 4MB we'll be mapping memory for the graphics emulation,
47 * but that will be an independant mapping.
48 *
49 * The 4MBs are divided into two main parts:
50 * -# The static code and data
51 * -# The shortlived page mappings.
52 *
53 * The first part is used for the VM structure, the core code (VMMSwitch),
54 * GC modules, and the alloc-only-heap. The size will be determined at a
55 * later point but initially we'll say 2MB of locked memory, most of which
56 * is non contiguous physically.
57 *
58 * The second part is used for mapping pages to the hypervisor. We'll be using
59 * a simple round robin when doing these mappings. This means that no-one can
60 * assume that a mapping hangs around for very long, while the managing of the
61 * pages are very simple.
62 *
63 *
64 *
65 * @section Page Pool
66 *
67 * The MM manages a per VM page pool from which other components can allocate
68 * locked, page aligned and page granular memory objects. The pool provides
69 * facilities to convert back and forth between physical and virtual addresses
70 * (within the pool of course). Several specialized interfaces are provided
71 * for the most common alloctions and convertions to save the caller from
72 * bothersome casting and extra parameter passing.
73 *
74 *
75 */
76
77
78
79/*******************************************************************************
80* Header Files *
81*******************************************************************************/
82#define LOG_GROUP LOG_GROUP_MM
83#include <VBox/mm.h>
84#include <VBox/pgm.h>
85#include <VBox/cfgm.h>
86#include <VBox/ssm.h>
87#include <VBox/gmm.h>
88#include "MMInternal.h"
89#include <VBox/vm.h>
90#include <VBox/uvm.h>
91#include <VBox/err.h>
92#include <VBox/param.h>
93
94#include <VBox/log.h>
95#include <iprt/alloc.h>
96#include <iprt/assert.h>
97#include <iprt/string.h>
98
99
100/*******************************************************************************
101* Defined Constants And Macros *
102*******************************************************************************/
103/** The current saved state versino of MM. */
104#define MM_SAVED_STATE_VERSION 2
105
106
107/*******************************************************************************
108* Internal Functions *
109*******************************************************************************/
110static DECLCALLBACK(int) mmR3Save(PVM pVM, PSSMHANDLE pSSM);
111static DECLCALLBACK(int) mmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version);
112
113
114/**
115 * Initializes the MM members of the UVM.
116 *
117 * This is currently only the ring-3 heap.
118 *
119 * @returns VBox status code.
120 * @param pUVM Pointer to the user mode VM structure.
121 */
122MMR3DECL(int) MMR3InitUVM(PUVM pUVM)
123{
124 /*
125 * Assert sizes and order.
126 */
127 AssertCompile(sizeof(pUVM->mm.s) <= sizeof(pUVM->mm.padding));
128 AssertRelease(sizeof(pUVM->mm.s) <= sizeof(pUVM->mm.padding));
129 Assert(!pUVM->mm.s.pHeap);
130
131 /*
132 * Init the heap.
133 */
134 return mmR3HeapCreateU(pUVM, &pUVM->mm.s.pHeap);
135}
136
137
138/**
139 * Initializes the MM.
140 *
141 * MM is managing the virtual address space (among other things) and
142 * setup the hypvervisor memory area mapping in the VM structure and
143 * the hypvervisor alloc-only-heap. Assuming the current init order
144 * and components the hypvervisor memory area looks like this:
145 * -# VM Structure.
146 * -# Hypervisor alloc only heap (also call Hypervisor memory region).
147 * -# Core code.
148 *
149 * MM determins the virtual address of the hypvervisor memory area by
150 * checking for location at previous run. If that property isn't available
151 * it will choose a default starting location, currently 0xe0000000.
152 *
153 * @returns VBox status code.
154 * @param pVM The VM to operate on.
155 */
156MMR3DECL(int) MMR3Init(PVM pVM)
157{
158 LogFlow(("MMR3Init\n"));
159
160 /*
161 * Assert alignment, sizes and order.
162 */
163 AssertRelease(!(RT_OFFSETOF(VM, mm.s) & 31));
164 AssertRelease(sizeof(pVM->mm.s) <= sizeof(pVM->mm.padding));
165 AssertMsg(pVM->mm.s.offVM == 0, ("Already initialized!\n"));
166
167 /*
168 * Init the structure.
169 */
170 pVM->mm.s.offVM = RT_OFFSETOF(VM, mm);
171 pVM->mm.s.offLookupHyper = NIL_OFFSET;
172
173 /*
174 * Init the page pool.
175 */
176 int rc = mmR3PagePoolInit(pVM);
177 if (VBOX_SUCCESS(rc))
178 {
179 /*
180 * Init the hypervisor related stuff.
181 */
182 rc = mmR3HyperInit(pVM);
183 if (VBOX_SUCCESS(rc))
184 {
185 /*
186 * Register the saved state data unit.
187 */
188 rc = SSMR3RegisterInternal(pVM, "mm", 1, MM_SAVED_STATE_VERSION, sizeof(uint32_t) * 2,
189 NULL, mmR3Save, NULL,
190 NULL, mmR3Load, NULL);
191 if (VBOX_SUCCESS(rc))
192 return rc;
193
194 /* .... failure .... */
195 }
196 }
197 MMR3Term(pVM);
198 return rc;
199}
200
201
202/**
203 * Initializes the MM parts which depends on PGM being initialized.
204 *
205 * @returns VBox status code.
206 * @param pVM The VM to operate on.
207 * @remark No cleanup necessary since MMR3Term() will be called on failure.
208 */
209MMR3DECL(int) MMR3InitPaging(PVM pVM)
210{
211 LogFlow(("MMR3InitPaging:\n"));
212
213 /*
214 * Query the CFGM values.
215 */
216 int rc;
217 PCFGMNODE pMMCfg = CFGMR3GetChild(CFGMR3GetRoot(pVM), "MM");
218 if (pMMCfg)
219 {
220 rc = CFGMR3InsertNode(CFGMR3GetRoot(pVM), "MM", &pMMCfg);
221 AssertRCReturn(rc, rc);
222 }
223
224 /** @cfgm{RamPreAlloc, boolean, false}
225 * Indicates whether the base RAM should all be allocated before starting
226 * the VM (default), or if it should be allocated when first written to.
227 */
228 bool fPreAlloc;
229 rc = CFGMR3QueryBool(CFGMR3GetRoot(pVM), "RamPreAlloc", &fPreAlloc);
230 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
231 fPreAlloc = false;
232 else
233 AssertMsgRCReturn(rc, ("Configuration error: Failed to query integer \"RamPreAlloc\", rc=%Vrc.\n", rc), rc);
234
235 /** @cfgm{RamSize, uint64_t, 0, 0, UINT64_MAX}
236 * Specifies the size of the base RAM that is to be set up during
237 * VM initialization.
238 */
239 uint64_t cbRam;
240 rc = CFGMR3QueryU64(CFGMR3GetRoot(pVM), "RamSize", &cbRam);
241 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
242 cbRam = 0;
243 else
244 AssertMsgRCReturn(rc, ("Configuration error: Failed to query integer \"RamSize\", rc=%Vrc.\n", rc), rc);
245
246 cbRam &= X86_PTE_PAE_PG_MASK;
247 pVM->mm.s.cbRamBase = cbRam; /* Warning: don't move this code to MMR3Init without fixing REMR3Init. */
248 Log(("MM: %RU64 bytes of RAM%s\n", cbRam, fPreAlloc ? " (PreAlloc)" : ""));
249
250 /** @cfgm{MM/Policy, string, no overcommitment}
251 * Specifies the policy to use when reserving memory for this VM. The recognized
252 * value is 'no overcommitment' (default). See GMMPOLICY.
253 */
254 GMMOCPOLICY enmPolicy;
255 char sz[64];
256 rc = CFGMR3QueryString(CFGMR3GetRoot(pVM), "Policy", sz, sizeof(sz));
257 if (RT_SUCCESS(rc))
258 {
259 if ( !RTStrICmp(sz, "no_oc")
260 || !RTStrICmp(sz, "no overcommitment"))
261 enmPolicy = GMMOCPOLICY_NO_OC;
262 else
263 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, "Unknown \"MM/Policy\" value \"%s\"", sz);
264 }
265 else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
266 enmPolicy = GMMOCPOLICY_NO_OC;
267 else
268 AssertMsgRCReturn(rc, ("Configuration error: Failed to query string \"MM/Policy\", rc=%Vrc.\n", rc), rc);
269
270 /** @cfgm{MM/Priority, string, normal}
271 * Specifies the memory priority of this VM. The priority comes into play when the
272 * system is overcommitted and the VMs needs to be milked for memory. The recognized
273 * values are 'low', 'normal' (default) and 'high'. See GMMPRIORITY.
274 */
275 GMMPRIORITY enmPriority;
276 rc = CFGMR3QueryString(CFGMR3GetRoot(pVM), "Priority", sz, sizeof(sz));
277 if (RT_SUCCESS(rc))
278 {
279 if (!RTStrICmp(sz, "low"))
280 enmPriority = GMMPRIORITY_LOW;
281 else if (!RTStrICmp(sz, "normal"))
282 enmPriority = GMMPRIORITY_NORMAL;
283 else if (!RTStrICmp(sz, "high"))
284 enmPriority = GMMPRIORITY_HIGH;
285 else
286 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, "Unknown \"MM/Priority\" value \"%s\"", sz);
287 }
288 else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
289 enmPriority = GMMPRIORITY_NORMAL;
290 else
291 AssertMsgRCReturn(rc, ("Configuration error: Failed to query string \"MM/Priority\", rc=%Vrc.\n", rc), rc);
292
293 /*
294 * Make the initial memory reservation with GMM.
295 */
296 rc = GMMR3InitialReservation(pVM, cbRam >> PAGE_SHIFT, 1, 1, enmPolicy, enmPriority);
297 if (RT_FAILURE(rc))
298 {
299 if (rc == VERR_GMM_MEMORY_RESERVATION_DECLINED)
300 return VMSetError(pVM, rc, RT_SRC_POS,
301 N_("Insufficient free memory to start the VM (cbRam=%#RX64 enmPolicy=%d enmPriority=%d)"),
302 cbRam, enmPolicy, enmPriority);
303 return VMSetError(pVM, rc, RT_SRC_POS, "GMMR3InitialReservation(,%#RX64,0,0,%d,%d)",
304 cbRam >> PAGE_SHIFT, enmPolicy, enmPriority);
305 }
306
307 /*
308 * If RamSize is 0 we're done now.
309 */
310 if (cbRam < PAGE_SIZE)
311 {
312 Log(("MM: No RAM configured\n"));
313 return VINF_SUCCESS;
314 }
315
316#ifdef VBOX_WITH_NEW_PHYS_CODE
317 /*
318 * Setup the base ram (PGM).
319 */
320 rc = PGMR3PhysRegisterRam(pVM, 0, cbRam, "Base RAM");
321 if (RT_SUCCESS(rc) && fPreAlloc)
322 {
323 /** @todo implement RamPreAlloc if it is *really* needed - in PGM preferably. (lazy bird) */
324 return VM_SET_ERROR(pVM, VERR_NOT_IMPLEMENTED, "TODO: RamPreAlloc");
325 }
326
327#else
328 rc = MMR3PhysRegisterEx(pVM, NULL, 0, cbRam, MM_RAM_FLAGS_DYNAMIC_ALLOC, MM_PHYS_TYPE_NORMAL, "Main Memory");
329 if (RT_SUCCESS(rc))
330 {
331 /*
332 * Allocate the first chunk, as we'll map ROM ranges there.
333 * If requested, allocated the rest too.
334 */
335 rc = PGM3PhysGrowRange(pVM, (RTGCPHYS)0);
336 if (RT_SUCCESS(rc) && fPreAlloc)
337 for (RTGCPHYS GCPhys = PGM_DYNAMIC_CHUNK_SIZE;
338 GCPhys < cbRam && RT_SUCCESS(rc);
339 GCPhys += PGM_DYNAMIC_CHUNK_SIZE)
340 rc = PGM3PhysGrowRange(pVM, GCPhys);
341 }
342#endif
343
344 LogFlow(("MMR3InitPaging: returns %Vrc\n", rc));
345 return rc;
346}
347
348
349/**
350 * Terminates the MM.
351 *
352 * Termination means cleaning up and freeing all resources,
353 * the VM it self is at this point powered off or suspended.
354 *
355 * @returns VBox status code.
356 * @param pVM The VM to operate on.
357 */
358MMR3DECL(int) MMR3Term(PVM pVM)
359{
360 /*
361 * Destroy the page pool. (first as it used the hyper heap)
362 */
363 mmR3PagePoolTerm(pVM);
364
365 /*
366 * Release locked memory.
367 * (Associated record are released by the heap.)
368 */
369 PMMLOCKEDMEM pLockedMem = pVM->mm.s.pLockedMem;
370 while (pLockedMem)
371 {
372 int rc = SUPPageUnlock(pLockedMem->pv);
373 AssertMsgRC(rc, ("SUPPageUnlock(%p) -> rc=%d\n", pLockedMem->pv, rc));
374 switch (pLockedMem->eType)
375 {
376 case MM_LOCKED_TYPE_HYPER:
377 rc = SUPPageFree(pLockedMem->pv, pLockedMem->cb >> PAGE_SHIFT);
378 AssertMsgRC(rc, ("SUPPageFree(%p) -> rc=%d\n", pLockedMem->pv, rc));
379 break;
380 case MM_LOCKED_TYPE_HYPER_NOFREE:
381 case MM_LOCKED_TYPE_HYPER_PAGES:
382 case MM_LOCKED_TYPE_PHYS:
383 /* nothing to do. */
384 break;
385 }
386 /* next */
387 pLockedMem = pLockedMem->pNext;
388 }
389
390 /*
391 * Zero stuff to detect after termination use of the MM interface
392 */
393 pVM->mm.s.offLookupHyper = NIL_OFFSET;
394 pVM->mm.s.pLockedMem = NULL;
395 pVM->mm.s.pHyperHeapHC = NULL; /* freed above. */
396 pVM->mm.s.pHyperHeapGC = 0; /* freed above. */
397 pVM->mm.s.offVM = 0; /* init assertion on this */
398
399 return 0;
400}
401
402
403/**
404 * Terminates the UVM part of MM.
405 *
406 * Termination means cleaning up and freeing all resources,
407 * the VM it self is at this point powered off or suspended.
408 *
409 * @returns VBox status code.
410 * @param pUVM Pointer to the user mode VM structure.
411 */
412MMR3DECL(void) MMR3TermUVM(PUVM pUVM)
413{
414 /*
415 * Destroy the heap.
416 */
417 mmR3HeapDestroy(pUVM->mm.s.pHeap);
418 pUVM->mm.s.pHeap = NULL;
419}
420
421
422/**
423 * Reset notification.
424 *
425 * MM will reload shadow ROMs into RAM at this point and make
426 * the ROM writable.
427 *
428 * @param pVM The VM handle.
429 */
430MMR3DECL(void) MMR3Reset(PVM pVM)
431{
432 mmR3PhysRomReset(pVM);
433}
434
435
436/**
437 * Execute state save operation.
438 *
439 * @returns VBox status code.
440 * @param pVM VM Handle.
441 * @param pSSM SSM operation handle.
442 */
443static DECLCALLBACK(int) mmR3Save(PVM pVM, PSSMHANDLE pSSM)
444{
445 LogFlow(("mmR3Save:\n"));
446
447 /* (PGM saves the physical memory.) */
448 SSMR3PutU64(pSSM, pVM->mm.s.cBasePages);
449 return SSMR3PutU64(pSSM, pVM->mm.s.cbRamBase);
450}
451
452
453/**
454 * Execute state load operation.
455 *
456 * @returns VBox status code.
457 * @param pVM VM Handle.
458 * @param pSSM SSM operation handle.
459 * @param u32Version Data layout version.
460 */
461static DECLCALLBACK(int) mmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version)
462{
463 LogFlow(("mmR3Load:\n"));
464
465 /*
466 * Validate version.
467 */
468 if ( SSM_VERSION_MAJOR_CHANGED(u32Version, MM_SAVED_STATE_VERSION)
469 || !u32Version)
470 {
471 Log(("mmR3Load: Invalid version u32Version=%d!\n", u32Version));
472 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
473 }
474
475 /*
476 * Check the cBasePages and cbRamBase values.
477 */
478 int rc;
479 RTUINT cb1;
480
481 /* cBasePages */
482 uint64_t cPages;
483 if (u32Version != 1)
484 rc = SSMR3GetU64(pSSM, &cPages);
485 else
486 {
487 rc = SSMR3GetUInt(pSSM, &cb1);
488 cPages = cb1 >> PAGE_SHIFT;
489 }
490 if (VBOX_FAILURE(rc))
491 return rc;
492 if (cPages != pVM->mm.s.cBasePages)
493 {
494 Log(("mmR3Load: Memory configuration has changed. cPages=%#RX64 saved=%#RX64\n", pVM->mm.s.cBasePages, cPages));
495 return VERR_SSM_LOAD_MEMORY_SIZE_MISMATCH;
496 }
497
498 /* cbRamBase */
499 uint64_t cb;
500 if (u32Version != 1)
501 rc = SSMR3GetU64(pSSM, &cb);
502 else
503 {
504 rc = SSMR3GetUInt(pSSM, &cb1);
505 cb = cb1;
506 }
507 if (VBOX_FAILURE(rc))
508 return rc;
509 if (cb != pVM->mm.s.cbRamBase)
510 {
511 Log(("mmR3Load: Memory configuration has changed. cbRamBase=%#RX64 save=%#RX64\n", pVM->mm.s.cbRamBase, cb));
512 return VERR_SSM_LOAD_MEMORY_SIZE_MISMATCH;
513 }
514
515 /* (PGM restores the physical memory.) */
516 return rc;
517}
518
519
520/**
521 * Updates GMM with memory reservation changes.
522 *
523 * Called when MM::cbRamRegistered, MM::cShadowPages or MM::cFixedPages changes.
524 *
525 * @returns VBox status code - see GMMR0UpdateReservation.
526 * @param pVM The shared VM structure.
527 */
528int mmR3UpdateReservation(PVM pVM)
529{
530 if (pVM->mm.s.fDoneMMR3InitPaging)
531 return GMMR3UpdateReservation(pVM,
532 RT_MAX(pVM->mm.s.cBasePages, 1),
533 RT_MAX(pVM->mm.s.cShadowPages, 1),
534 RT_MAX(pVM->mm.s.cFixedPages, 1));
535 return VINF_SUCCESS;
536}
537
538
539/**
540 * Interface for PGM to increase the reservation of RAM and ROM pages.
541 *
542 * This can be called before MMR3InitPaging.
543 *
544 * @returns VBox status code.
545 * @param pVM The shared VM structure.
546 * @param cAddBasePages The number of pages to add.
547 */
548MMR3DECL(int) MMR3IncreaseBaseReservation(PVM pVM, uint64_t cAddBasePages)
549{
550 uint64_t cOld = pVM->mm.s.cBasePages;
551 pVM->mm.s.cBasePages += cAddBasePages;
552 LogFlow(("MMR3IncreaseBaseReservation: +%RU64 (%RU64 -> %RU64\n", cAddBasePages, cOld, pVM->mm.s.cBasePages));
553 int rc = mmR3UpdateReservation(pVM);
554 if (RT_FAILURE(rc))
555 {
556 VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserved physical memory for the RAM (%#RX64 -> %#RX64)"), cOld, pVM->mm.s.cBasePages);
557 pVM->mm.s.cBasePages = cOld;
558 }
559 return rc;
560}
561
562
563/**
564 * Interface for PGM to increase the reservation of fixed pages.
565 *
566 * This can be called before MMR3InitPaging.
567 *
568 * @returns VBox status code.
569 * @param pVM The shared VM structure.
570 * @param cAddFixedPages The number of pages to add.
571 */
572MMR3DECL(int) MMR3IncreaseFixedReservation(PVM pVM, uint32_t cAddFixedPages)
573{
574 const uint32_t cOld = pVM->mm.s.cFixedPages;
575 pVM->mm.s.cFixedPages += cAddFixedPages;
576 LogFlow(("MMR3AddFixedReservation: +%u (%u -> %u)\n", cAddFixedPages, cOld, pVM->mm.s.cFixedPages));
577 int rc = mmR3UpdateReservation(pVM);
578 if (RT_FAILURE(rc))
579 {
580 VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserve physical memory (%#x -> %#x)"), cOld, pVM->mm.s.cFixedPages);
581 pVM->mm.s.cFixedPages = cOld;
582 }
583 return rc;
584}
585
586
587/**
588 * Interface for PGM to update the reservation of shadow pages.
589 *
590 * This can be called before MMR3InitPaging.
591 *
592 * @returns VBox status code.
593 * @param pVM The shared VM structure.
594 * @param cShadowPages The new page count.
595 */
596MMR3DECL(int) MMR3UpdateShadowReservation(PVM pVM, uint32_t cShadowPages)
597{
598 const uint32_t cOld = pVM->mm.s.cShadowPages;
599 pVM->mm.s.cShadowPages = cShadowPages;
600 LogFlow(("MMR3UpdateShadowReservation: %u -> %u\n", cOld, pVM->mm.s.cShadowPages));
601 int rc = mmR3UpdateReservation(pVM);
602 if (RT_FAILURE(rc))
603 {
604 VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserve physical memory for shadow page tables (%#x -> %#x)"), cOld, pVM->mm.s.cShadowPages);
605 pVM->mm.s.cShadowPages = cOld;
606 }
607 return rc;
608}
609
610
611/**
612 * Locks physical memory which backs a virtual memory range (HC) adding
613 * the required records to the pLockedMem list.
614 *
615 * @returns VBox status code.
616 * @param pVM The VM handle.
617 * @param pv Pointer to memory range which shall be locked down.
618 * This pointer is page aligned.
619 * @param cb Size of memory range (in bytes). This size is page aligned.
620 * @param eType Memory type.
621 * @param ppLockedMem Where to store the pointer to the created locked memory record.
622 * This is optional, pass NULL if not used.
623 * @param fSilentFailure Don't raise an error when unsuccessful. Upper layer with deal with it.
624 */
625int mmR3LockMem(PVM pVM, void *pv, size_t cb, MMLOCKEDTYPE eType, PMMLOCKEDMEM *ppLockedMem, bool fSilentFailure)
626{
627 Assert(RT_ALIGN_P(pv, PAGE_SIZE) == pv);
628 Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb);
629
630 if (ppLockedMem)
631 *ppLockedMem = NULL;
632
633 /*
634 * Allocate locked mem structure.
635 */
636 unsigned cPages = cb >> PAGE_SHIFT;
637 AssertReturn(cPages == (cb >> PAGE_SHIFT), VERR_OUT_OF_RANGE);
638 PMMLOCKEDMEM pLockedMem = (PMMLOCKEDMEM)MMR3HeapAlloc(pVM, MM_TAG_MM, RT_OFFSETOF(MMLOCKEDMEM, aPhysPages[cPages]));
639 if (!pLockedMem)
640 return VERR_NO_MEMORY;
641 pLockedMem->pv = pv;
642 pLockedMem->cb = cb;
643 pLockedMem->eType = eType;
644 memset(&pLockedMem->u, 0, sizeof(pLockedMem->u));
645
646 /*
647 * Lock the memory.
648 */
649 int rc = SUPPageLock(pv, cPages, &pLockedMem->aPhysPages[0]);
650 if (VBOX_SUCCESS(rc))
651 {
652 /*
653 * Setup the reserved field.
654 */
655 PSUPPAGE pPhysPage = &pLockedMem->aPhysPages[0];
656 for (unsigned c = cPages; c > 0; c--, pPhysPage++)
657 pPhysPage->uReserved = (RTHCUINTPTR)pLockedMem;
658
659 /*
660 * Insert into the list.
661 *
662 * ASSUME no protected needed here as only one thread in the system can possibly
663 * be doing this. No other threads will walk this list either we assume.
664 */
665 pLockedMem->pNext = pVM->mm.s.pLockedMem;
666 pVM->mm.s.pLockedMem = pLockedMem;
667 /* Set return value. */
668 if (ppLockedMem)
669 *ppLockedMem = pLockedMem;
670 }
671 else
672 {
673 AssertMsgFailed(("SUPPageLock failed with rc=%d\n", rc));
674 MMR3HeapFree(pLockedMem);
675 if (!fSilentFailure)
676 rc = VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to lock %d bytes of host memory (out of memory)"), cb);
677 }
678
679 return rc;
680}
681
682
683/**
684 * Maps a part of or an entire locked memory region into the guest context.
685 *
686 * @returns VBox status.
687 * God knows what happens if we fail...
688 * @param pVM VM handle.
689 * @param pLockedMem Locked memory structure.
690 * @param Addr GC Address where to start the mapping.
691 * @param iPage Page number in the locked memory region.
692 * @param cPages Number of pages to map.
693 * @param fFlags See the fFlags argument of PGR3Map().
694 */
695int mmR3MapLocked(PVM pVM, PMMLOCKEDMEM pLockedMem, RTGCPTR Addr, unsigned iPage, size_t cPages, unsigned fFlags)
696{
697 /*
698 * Adjust ~0 argument
699 */
700 if (cPages == ~(size_t)0)
701 cPages = (pLockedMem->cb >> PAGE_SHIFT) - iPage;
702 Assert(cPages != ~0U);
703 /* no incorrect arguments are accepted */
704 Assert(RT_ALIGN_GCPT(Addr, PAGE_SIZE, RTGCPTR) == Addr);
705 AssertMsg(iPage < (pLockedMem->cb >> PAGE_SHIFT), ("never even think about giving me a bad iPage(=%d)\n", iPage));
706 AssertMsg(iPage + cPages <= (pLockedMem->cb >> PAGE_SHIFT), ("never even think about giving me a bad cPages(=%d)\n", cPages));
707
708 /*
709 * Map the the pages.
710 */
711 PSUPPAGE pPhysPage = &pLockedMem->aPhysPages[iPage];
712 while (cPages)
713 {
714 RTHCPHYS HCPhys = pPhysPage->Phys;
715 int rc = PGMMap(pVM, Addr, HCPhys, PAGE_SIZE, fFlags);
716 if (VBOX_FAILURE(rc))
717 {
718 /** @todo how the hell can we do a proper bailout here. */
719 return rc;
720 }
721
722 /* next */
723 cPages--;
724 iPage++;
725 pPhysPage++;
726 Addr += PAGE_SIZE;
727 }
728
729 return VINF_SUCCESS;
730}
731
732
733/**
734 * Convert HC Physical address to HC Virtual address.
735 *
736 * @returns VBox status.
737 * @param pVM VM handle.
738 * @param HCPhys The host context virtual address.
739 * @param ppv Where to store the resulting address.
740 * @thread The Emulation Thread.
741 */
742MMR3DECL(int) MMR3HCPhys2HCVirt(PVM pVM, RTHCPHYS HCPhys, void **ppv)
743{
744 /*
745 * Try page tables.
746 */
747 int rc = MMPagePhys2PageTry(pVM, HCPhys, ppv);
748 if (VBOX_SUCCESS(rc))
749 return rc;
750
751 /*
752 * Iterate the locked memory - very slow.
753 */
754 uint32_t off = HCPhys & PAGE_OFFSET_MASK;
755 HCPhys &= X86_PTE_PAE_PG_MASK;
756 for (PMMLOCKEDMEM pCur = pVM->mm.s.pLockedMem; pCur; pCur = pCur->pNext)
757 {
758 size_t iPage = pCur->cb >> PAGE_SHIFT;
759 while (iPage-- > 0)
760 if ((pCur->aPhysPages[iPage].Phys & X86_PTE_PAE_PG_MASK) == HCPhys)
761 {
762 *ppv = (char *)pCur->pv + (iPage << PAGE_SHIFT) + off;
763 return VINF_SUCCESS;
764 }
765 }
766 /* give up */
767 return VERR_INVALID_POINTER;
768}
769
770
771/**
772 * Read memory from GC virtual address using the current guest CR3.
773 *
774 * @returns VBox status.
775 * @param pVM VM handle.
776 * @param pvDst Destination address (HC of course).
777 * @param GCPtr GC virtual address.
778 * @param cb Number of bytes to read.
779 */
780MMR3DECL(int) MMR3ReadGCVirt(PVM pVM, void *pvDst, RTGCPTR GCPtr, size_t cb)
781{
782 if (GCPtr - pVM->mm.s.pvHyperAreaGC < pVM->mm.s.cbHyperArea)
783 return MMR3HyperReadGCVirt(pVM, pvDst, GCPtr, cb);
784 return PGMPhysReadGCPtr(pVM, pvDst, GCPtr, cb);
785}
786
787
788/**
789 * Write to memory at GC virtual address translated using the current guest CR3.
790 *
791 * @returns VBox status.
792 * @param pVM VM handle.
793 * @param GCPtrDst GC virtual address.
794 * @param pvSrc The source address (HC of course).
795 * @param cb Number of bytes to read.
796 */
797MMR3DECL(int) MMR3WriteGCVirt(PVM pVM, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
798{
799 if (GCPtrDst - pVM->mm.s.pvHyperAreaGC < pVM->mm.s.cbHyperArea)
800 return VERR_ACCESS_DENIED;
801 return PGMPhysWriteGCPtr(pVM, GCPtrDst, pvSrc, cb);
802}
803
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