VirtualBox

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

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

fixes to new (disabled) code.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette