VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/MMHyper.cpp@ 90991

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

VMM: Eliminated VMMCALLRING3_MMHYPER_LOCK. bugref:6695

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 53.3 KB
Line 
1/* $Id: MMHyper.cpp 90991 2021-08-30 09:49:20Z vboxsync $ */
2/** @file
3 * MM - Memory Manager - Hypervisor Memory Area.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_MM_HYPER
23#include <VBox/vmm/pgm.h>
24#include <VBox/vmm/mm.h>
25#include <VBox/vmm/hm.h>
26#include <VBox/vmm/dbgf.h>
27#include "MMInternal.h"
28#include <VBox/vmm/vm.h>
29#include <VBox/vmm/gvm.h>
30#include <VBox/err.h>
31#include <VBox/param.h>
32#include <VBox/log.h>
33#include <iprt/alloc.h>
34#include <iprt/assert.h>
35#include <iprt/string.h>
36
37
38/*********************************************************************************************************************************
39* Internal Functions *
40*********************************************************************************************************************************/
41#ifndef PGM_WITHOUT_MAPPINGS
42static DECLCALLBACK(bool) mmR3HyperRelocateCallback(PVM pVM, RTGCPTR GCPtrOld, RTGCPTR GCPtrNew, PGMRELOCATECALL enmMode,
43 void *pvUser);
44#endif
45static int mmR3HyperMap(PVM pVM, const size_t cb, const char *pszDesc, PRTGCPTR pGCPtr, PMMLOOKUPHYPER *ppLookup);
46static int mmR3HyperHeapCreate(PVM pVM, const size_t cb, PMMHYPERHEAP *ppHeap, PRTR0PTR pR0PtrHeap);
47static int mmR3HyperHeapMap(PVM pVM, PMMHYPERHEAP pHeap, PRTGCPTR ppHeapGC);
48static DECLCALLBACK(void) mmR3HyperInfoHma(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
49
50
51/**
52 * Determin the default heap size.
53 *
54 * @returns The heap size in bytes.
55 * @param pVM The cross context VM structure.
56 */
57static uint32_t mmR3HyperComputeHeapSize(PVM pVM)
58{
59 /** @todo Redo after moving allocations off the hyper heap. */
60
61 /*
62 * Gather parameters.
63 */
64 bool fCanUseLargerHeap = true;
65 //bool fCanUseLargerHeap;
66 //int rc = CFGMR3QueryBoolDef(CFGMR3GetChild(CFGMR3GetRoot(pVM), "MM"), "CanUseLargerHeap", &fCanUseLargerHeap, false);
67 //AssertStmt(RT_SUCCESS(rc), fCanUseLargerHeap = false);
68
69 uint64_t cbRam;
70 int rc = CFGMR3QueryU64(CFGMR3GetRoot(pVM), "RamSize", &cbRam);
71 AssertStmt(RT_SUCCESS(rc), cbRam = _1G);
72
73 /*
74 * We need to keep saved state compatibility if raw-mode is an option,
75 * so lets filter out that case first.
76 */
77 if ( !fCanUseLargerHeap
78 && VM_IS_RAW_MODE_ENABLED(pVM)
79 && cbRam < 16*_1G64)
80 return 1280 * _1K;
81
82 /*
83 * Calculate the heap size.
84 */
85 uint32_t cbHeap = _1M;
86
87 /* The newer chipset may have more devices attached, putting additional
88 pressure on the heap. */
89 if (fCanUseLargerHeap)
90 cbHeap += _1M;
91
92 /* More CPUs means some extra memory usage. */
93 if (pVM->cCpus > 1)
94 cbHeap += pVM->cCpus * _64K;
95
96 /* Lots of memory means extra memory consumption as well (pool). */
97 if (cbRam > 16*_1G64)
98 cbHeap += _2M; /** @todo figure out extactly how much */
99
100 return RT_ALIGN(cbHeap, _256K);
101}
102
103
104/**
105 * Initializes the hypervisor related MM stuff without
106 * calling down to PGM.
107 *
108 * PGM is not initialized at this point, PGM relies on
109 * the heap to initialize.
110 *
111 * @returns VBox status code.
112 */
113int mmR3HyperInit(PVM pVM)
114{
115 LogFlow(("mmR3HyperInit:\n"));
116
117 /*
118 * Decide Hypervisor mapping in the guest context
119 * And setup various hypervisor area and heap parameters.
120 */
121 pVM->mm.s.pvHyperAreaGC = (RTGCPTR)MM_HYPER_AREA_ADDRESS;
122 pVM->mm.s.cbHyperArea = MM_HYPER_AREA_MAX_SIZE;
123 AssertRelease(RT_ALIGN_T(pVM->mm.s.pvHyperAreaGC, 1 << X86_PD_SHIFT, RTGCPTR) == pVM->mm.s.pvHyperAreaGC);
124 Assert(pVM->mm.s.pvHyperAreaGC < 0xff000000);
125
126 /** @todo @bugref{1865}, @bugref{3202}: Change the cbHyperHeap default
127 * depending on whether VT-x/AMD-V is enabled or not! Don't waste
128 * precious kernel space on heap for the PATM.
129 */
130 PCFGMNODE pMM = CFGMR3GetChild(CFGMR3GetRoot(pVM), "MM");
131 uint32_t cbHyperHeap;
132 int rc = CFGMR3QueryU32Def(pMM, "cbHyperHeap", &cbHyperHeap, mmR3HyperComputeHeapSize(pVM));
133 AssertLogRelRCReturn(rc, rc);
134
135 cbHyperHeap = RT_ALIGN_32(cbHyperHeap, PAGE_SIZE);
136 LogRel(("MM: cbHyperHeap=%#x (%u)\n", cbHyperHeap, cbHyperHeap));
137
138 /*
139 * Allocate the hypervisor heap.
140 *
141 * (This must be done before we start adding memory to the
142 * hypervisor static area because lookup records are allocated from it.)
143 */
144 rc = mmR3HyperHeapCreate(pVM, cbHyperHeap, &pVM->mm.s.pHyperHeapR3, &pVM->mm.s.pHyperHeapR0);
145 if (RT_SUCCESS(rc))
146 {
147 /*
148 * Make a small head fence to fend of accidental sequential access.
149 */
150 MMR3HyperReserveFence(pVM);
151
152 /*
153 * Map the VM structure into the hypervisor space.
154 * Note! Keeping the mappings here for now in case someone is using
155 * MMHyperR3ToR0 or similar.
156 */
157 AssertCompileSizeAlignment(VM, PAGE_SIZE);
158 AssertCompileSizeAlignment(VMCPU, PAGE_SIZE);
159 AssertCompileSizeAlignment(GVM, PAGE_SIZE);
160 AssertCompileSizeAlignment(GVMCPU, PAGE_SIZE);
161 AssertRelease(pVM->cbSelf == sizeof(VM));
162 AssertRelease(pVM->cbVCpu == sizeof(VMCPU));
163/** @todo get rid of this */
164 RTGCPTR GCPtr;
165 rc = MMR3HyperMapPages(pVM, pVM, pVM->pVMR0ForCall, sizeof(VM) >> PAGE_SHIFT, pVM->paVMPagesR3, "VM", &GCPtr);
166 uint32_t offPages = RT_UOFFSETOF_DYN(GVM, aCpus) >> PAGE_SHIFT; /* (Using the _DYN variant avoids -Winvalid-offset) */
167 for (uint32_t idCpu = 0; idCpu < pVM->cCpus && RT_SUCCESS(rc); idCpu++, offPages += sizeof(GVMCPU) >> PAGE_SHIFT)
168 {
169 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
170 RTGCPTR GCPtrIgn;
171 rc = MMR3HyperMapPages(pVM, pVCpu, pVM->pVMR0ForCall + offPages * PAGE_SIZE,
172 sizeof(VMCPU) >> PAGE_SHIFT, &pVM->paVMPagesR3[offPages], "VMCPU", &GCPtrIgn);
173 }
174 if (RT_SUCCESS(rc))
175 {
176 pVM->pVMRC = (RTRCPTR)GCPtr;
177 for (VMCPUID i = 0; i < pVM->cCpus; i++)
178 pVM->apCpusR3[i]->pVMRC = pVM->pVMRC;
179
180 /* Reserve a page for fencing. */
181 MMR3HyperReserveFence(pVM);
182
183 /*
184 * Map the heap into the hypervisor space.
185 */
186 rc = mmR3HyperHeapMap(pVM, pVM->mm.s.pHyperHeapR3, &GCPtr);
187 if (RT_SUCCESS(rc))
188 {
189 pVM->mm.s.pHyperHeapRC = (RTRCPTR)GCPtr;
190 Assert(pVM->mm.s.pHyperHeapRC == GCPtr);
191
192 /*
193 * Register info handlers.
194 */
195 DBGFR3InfoRegisterInternal(pVM, "hma", "Show the layout of the Hypervisor Memory Area.", mmR3HyperInfoHma);
196
197 LogFlow(("mmR3HyperInit: returns VINF_SUCCESS\n"));
198 return VINF_SUCCESS;
199 }
200 /* Caller will do proper cleanup. */
201 }
202 }
203
204 LogFlow(("mmR3HyperInit: returns %Rrc\n", rc));
205 return rc;
206}
207
208
209/**
210 * Cleans up the hypervisor heap.
211 *
212 * @returns VBox status code.
213 */
214int mmR3HyperTerm(PVM pVM)
215{
216 if (pVM->mm.s.pHyperHeapR3)
217 PDMR3CritSectDelete(pVM, &pVM->mm.s.pHyperHeapR3->Lock);
218
219 return VINF_SUCCESS;
220}
221
222
223/**
224 * Finalizes the HMA mapping.
225 *
226 * This is called later during init, most (all) HMA allocations should be done
227 * by the time this function is called.
228 *
229 * @returns VBox status code.
230 */
231VMMR3DECL(int) MMR3HyperInitFinalize(PVM pVM)
232{
233 LogFlow(("MMR3HyperInitFinalize:\n"));
234
235 /*
236 * Initialize the hyper heap critical section.
237 */
238 int rc = PDMR3CritSectInit(pVM, &pVM->mm.s.pHyperHeapR3->Lock, RT_SRC_POS, "MM-HYPER");
239 AssertRC(rc);
240
241#ifndef PGM_WITHOUT_MAPPINGS
242 /*
243 * Adjust and create the HMA mapping.
244 */
245 while ((RTINT)pVM->mm.s.offHyperNextStatic + 64*_1K < (RTINT)pVM->mm.s.cbHyperArea - _4M)
246 pVM->mm.s.cbHyperArea -= _4M;
247 rc = PGMR3MapPT(pVM, pVM->mm.s.pvHyperAreaGC, pVM->mm.s.cbHyperArea, 0 /*fFlags*/,
248 mmR3HyperRelocateCallback, NULL, "Hypervisor Memory Area");
249 if (RT_FAILURE(rc))
250 return rc;
251#endif
252 pVM->mm.s.fPGMInitialized = true;
253
254#ifndef PGM_WITHOUT_MAPPINGS
255 /*
256 * Do all the delayed mappings.
257 */
258 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uintptr_t)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
259 for (;;)
260 {
261 RTGCPTR GCPtr = pVM->mm.s.pvHyperAreaGC + pLookup->off;
262 uint32_t cPages = pLookup->cb >> PAGE_SHIFT;
263 switch (pLookup->enmType)
264 {
265 case MMLOOKUPHYPERTYPE_LOCKED:
266 {
267 PCRTHCPHYS paHCPhysPages = pLookup->u.Locked.paHCPhysPages;
268 for (uint32_t i = 0; i < cPages; i++)
269 {
270 rc = PGMMap(pVM, GCPtr + (i << PAGE_SHIFT), paHCPhysPages[i], PAGE_SIZE, 0);
271 AssertRCReturn(rc, rc);
272 }
273 break;
274 }
275
276 case MMLOOKUPHYPERTYPE_HCPHYS:
277 rc = PGMMap(pVM, GCPtr, pLookup->u.HCPhys.HCPhys, pLookup->cb, 0);
278 break;
279
280 case MMLOOKUPHYPERTYPE_GCPHYS:
281 {
282 const RTGCPHYS GCPhys = pLookup->u.GCPhys.GCPhys;
283 const uint32_t cb = pLookup->cb;
284 for (uint32_t off = 0; off < cb; off += PAGE_SIZE)
285 {
286 RTHCPHYS HCPhys;
287 rc = PGMPhysGCPhys2HCPhys(pVM, GCPhys + off, &HCPhys);
288 if (RT_FAILURE(rc))
289 break;
290 rc = PGMMap(pVM, GCPtr + off, HCPhys, PAGE_SIZE, 0);
291 if (RT_FAILURE(rc))
292 break;
293 }
294 break;
295 }
296
297 case MMLOOKUPHYPERTYPE_MMIO2:
298 {
299 const RTGCPHYS offEnd = pLookup->u.MMIO2.off + pLookup->cb;
300 for (RTGCPHYS offCur = pLookup->u.MMIO2.off; offCur < offEnd; offCur += PAGE_SIZE)
301 {
302 RTHCPHYS HCPhys;
303 rc = PGMR3PhysMMIO2GetHCPhys(pVM, pLookup->u.MMIO2.pDevIns, pLookup->u.MMIO2.iSubDev,
304 pLookup->u.MMIO2.iRegion, offCur, &HCPhys);
305 if (RT_FAILURE(rc))
306 break;
307 rc = PGMMap(pVM, GCPtr + (offCur - pLookup->u.MMIO2.off), HCPhys, PAGE_SIZE, 0);
308 if (RT_FAILURE(rc))
309 break;
310 }
311 break;
312 }
313
314 case MMLOOKUPHYPERTYPE_DYNAMIC:
315 /* do nothing here since these are either fences or managed by someone else using PGM. */
316 break;
317
318 default:
319 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
320 break;
321 }
322
323 if (RT_FAILURE(rc))
324 {
325 AssertMsgFailed(("rc=%Rrc cb=%d off=%#RX32 enmType=%d pszDesc=%s\n",
326 rc, pLookup->cb, pLookup->off, pLookup->enmType, pLookup->pszDesc));
327 return rc;
328 }
329
330 /* next */
331 if (pLookup->offNext == (int32_t)NIL_OFFSET)
332 break;
333 pLookup = (PMMLOOKUPHYPER)((uintptr_t)pLookup + pLookup->offNext);
334 }
335#endif /* !PGM_WITHOUT_MAPPINGS */
336
337 LogFlow(("MMR3HyperInitFinalize: returns VINF_SUCCESS\n"));
338 return VINF_SUCCESS;
339}
340
341
342#ifndef PGM_WITHOUT_MAPPINGS
343/**
344 * Callback function which will be called when PGM is trying to find a new
345 * location for the mapping.
346 *
347 * The callback is called in two modes, 1) the check mode and 2) the relocate mode.
348 * In 1) the callback should say if it objects to a suggested new location. If it
349 * accepts the new location, it is called again for doing it's relocation.
350 *
351 *
352 * @returns true if the location is ok.
353 * @returns false if another location should be found.
354 * @param pVM The cross context VM structure.
355 * @param GCPtrOld The old virtual address.
356 * @param GCPtrNew The new virtual address.
357 * @param enmMode Used to indicate the callback mode.
358 * @param pvUser User argument. Ignored.
359 * @remark The return value is no a failure indicator, it's an acceptance
360 * indicator. Relocation can not fail!
361 */
362static DECLCALLBACK(bool) mmR3HyperRelocateCallback(PVM pVM, RTGCPTR GCPtrOld, RTGCPTR GCPtrNew,
363 PGMRELOCATECALL enmMode, void *pvUser)
364{
365 NOREF(pvUser);
366 switch (enmMode)
367 {
368 /*
369 * Verify location - all locations are good for us.
370 */
371 case PGMRELOCATECALL_SUGGEST:
372 return true;
373
374 /*
375 * Execute the relocation.
376 */
377 case PGMRELOCATECALL_RELOCATE:
378 {
379 /*
380 * Accepted!
381 */
382 AssertMsg(GCPtrOld == pVM->mm.s.pvHyperAreaGC,
383 ("GCPtrOld=%RGv pVM->mm.s.pvHyperAreaGC=%RGv\n", GCPtrOld, pVM->mm.s.pvHyperAreaGC));
384 Log(("Relocating the hypervisor from %RGv to %RGv\n", GCPtrOld, GCPtrNew));
385
386 /*
387 * Relocate the VM structure and ourselves.
388 */
389 RTGCINTPTR offDelta = GCPtrNew - GCPtrOld;
390 pVM->pVMRC += offDelta;
391 for (VMCPUID i = 0; i < pVM->cCpus; i++)
392 pVM->aCpus[i].pVMRC = pVM->pVMRC;
393
394 pVM->mm.s.pvHyperAreaGC += offDelta;
395 Assert(pVM->mm.s.pvHyperAreaGC < _4G);
396 pVM->mm.s.pHyperHeapRC += offDelta;
397 pVM->mm.s.pHyperHeapR3->pbHeapRC += offDelta;
398 pVM->mm.s.pHyperHeapR3->pVMRC = pVM->pVMRC;
399
400 /*
401 * Relocate the rest.
402 */
403 VMR3Relocate(pVM, offDelta);
404 return true;
405 }
406
407 default:
408 AssertMsgFailed(("Invalid relocation mode %d\n", enmMode));
409 }
410
411 return false;
412}
413
414
415/**
416 * Maps contiguous HC physical memory into the hypervisor region in the GC.
417 *
418 * @return VBox status code.
419 *
420 * @param pVM The cross context VM structure.
421 * @param pvR3 Ring-3 address of the memory. Must be page aligned!
422 * @param pvR0 Optional ring-0 address of the memory.
423 * @param HCPhys Host context physical address of the memory to be
424 * mapped. Must be page aligned!
425 * @param cb Size of the memory. Will be rounded up to nearest page.
426 * @param pszDesc Description.
427 * @param pGCPtr Where to store the GC address.
428 */
429VMMR3DECL(int) MMR3HyperMapHCPhys(PVM pVM, void *pvR3, RTR0PTR pvR0, RTHCPHYS HCPhys, size_t cb,
430 const char *pszDesc, PRTGCPTR pGCPtr)
431{
432 LogFlow(("MMR3HyperMapHCPhys: pvR3=%p pvR0=%p HCPhys=%RHp cb=%d pszDesc=%p:{%s} pGCPtr=%p\n",
433 pvR3, pvR0, HCPhys, (int)cb, pszDesc, pszDesc, pGCPtr));
434
435 /*
436 * Validate input.
437 */
438 AssertReturn(RT_ALIGN_P(pvR3, PAGE_SIZE) == pvR3, VERR_INVALID_PARAMETER);
439 AssertReturn(RT_ALIGN_T(pvR0, PAGE_SIZE, RTR0PTR) == pvR0, VERR_INVALID_PARAMETER);
440 AssertReturn(RT_ALIGN_T(HCPhys, PAGE_SIZE, RTHCPHYS) == HCPhys, VERR_INVALID_PARAMETER);
441 AssertReturn(pszDesc && *pszDesc, VERR_INVALID_PARAMETER);
442
443 /*
444 * Add the memory to the hypervisor area.
445 */
446 uint32_t cbAligned = RT_ALIGN_32(cb, PAGE_SIZE);
447 AssertReturn(cbAligned >= cb, VERR_INVALID_PARAMETER);
448 RTGCPTR GCPtr;
449 PMMLOOKUPHYPER pLookup;
450 int rc = mmR3HyperMap(pVM, cbAligned, pszDesc, &GCPtr, &pLookup);
451 if (RT_SUCCESS(rc))
452 {
453 pLookup->enmType = MMLOOKUPHYPERTYPE_HCPHYS;
454 pLookup->u.HCPhys.pvR3 = pvR3;
455 pLookup->u.HCPhys.pvR0 = pvR0;
456 pLookup->u.HCPhys.HCPhys = HCPhys;
457
458 /*
459 * Update the page table.
460 */
461 if (pVM->mm.s.fPGMInitialized)
462 rc = PGMMap(pVM, GCPtr, HCPhys, cbAligned, 0);
463 if (RT_SUCCESS(rc))
464 *pGCPtr = GCPtr;
465 }
466 return rc;
467}
468
469
470/**
471 * Maps contiguous GC physical memory into the hypervisor region in the GC.
472 *
473 * @return VBox status code.
474 *
475 * @param pVM The cross context VM structure.
476 * @param GCPhys Guest context physical address of the memory to be mapped. Must be page aligned!
477 * @param cb Size of the memory. Will be rounded up to nearest page.
478 * @param pszDesc Mapping description.
479 * @param pGCPtr Where to store the GC address.
480 */
481VMMR3DECL(int) MMR3HyperMapGCPhys(PVM pVM, RTGCPHYS GCPhys, size_t cb, const char *pszDesc, PRTGCPTR pGCPtr)
482{
483 LogFlow(("MMR3HyperMapGCPhys: GCPhys=%RGp cb=%d pszDesc=%p:{%s} pGCPtr=%p\n", GCPhys, (int)cb, pszDesc, pszDesc, pGCPtr));
484
485 /*
486 * Validate input.
487 */
488 AssertReturn(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys, VERR_INVALID_PARAMETER);
489 AssertReturn(pszDesc && *pszDesc, VERR_INVALID_PARAMETER);
490
491 /*
492 * Add the memory to the hypervisor area.
493 */
494 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
495 RTGCPTR GCPtr;
496 PMMLOOKUPHYPER pLookup;
497 int rc = mmR3HyperMap(pVM, cb, pszDesc, &GCPtr, &pLookup);
498 if (RT_SUCCESS(rc))
499 {
500 pLookup->enmType = MMLOOKUPHYPERTYPE_GCPHYS;
501 pLookup->u.GCPhys.GCPhys = GCPhys;
502
503 /*
504 * Update the page table.
505 */
506 for (unsigned off = 0; off < cb; off += PAGE_SIZE)
507 {
508 RTHCPHYS HCPhys;
509 rc = PGMPhysGCPhys2HCPhys(pVM, GCPhys + off, &HCPhys);
510 AssertRC(rc);
511 if (RT_FAILURE(rc))
512 {
513 AssertMsgFailed(("rc=%Rrc GCPhys=%RGp off=%#x %s\n", rc, GCPhys, off, pszDesc));
514 break;
515 }
516 if (pVM->mm.s.fPGMInitialized)
517 {
518 rc = PGMMap(pVM, GCPtr + off, HCPhys, PAGE_SIZE, 0);
519 AssertRC(rc);
520 if (RT_FAILURE(rc))
521 {
522 AssertMsgFailed(("rc=%Rrc GCPhys=%RGp off=%#x %s\n", rc, GCPhys, off, pszDesc));
523 break;
524 }
525 }
526 }
527
528 if (RT_SUCCESS(rc) && pGCPtr)
529 *pGCPtr = GCPtr;
530 }
531 return rc;
532}
533
534
535/**
536 * Maps a portion of an MMIO2 region into the hypervisor region.
537 *
538 * Callers of this API must never deregister the MMIO2 region before the
539 * VM is powered off. If this becomes a requirement MMR3HyperUnmapMMIO2
540 * API will be needed to perform cleanups.
541 *
542 * @return VBox status code.
543 *
544 * @param pVM The cross context VM structure.
545 * @param pDevIns The device owning the MMIO2 memory.
546 * @param iSubDev The sub-device number.
547 * @param iRegion The region.
548 * @param off The offset into the region. Will be rounded down to closest page boundary.
549 * @param cb The number of bytes to map. Will be rounded up to the closest page boundary.
550 * @param pszDesc Mapping description.
551 * @param pRCPtr Where to store the RC address.
552 */
553VMMR3DECL(int) MMR3HyperMapMMIO2(PVM pVM, PPDMDEVINS pDevIns, uint32_t iSubDev, uint32_t iRegion, RTGCPHYS off, RTGCPHYS cb,
554 const char *pszDesc, PRTRCPTR pRCPtr)
555{
556 LogFlow(("MMR3HyperMapMMIO2: pDevIns=%p iSubDev=%#x iRegion=%#x off=%RGp cb=%RGp pszDesc=%p:{%s} pRCPtr=%p\n",
557 pDevIns, iSubDev, iRegion, off, cb, pszDesc, pszDesc, pRCPtr));
558 int rc;
559
560 /*
561 * Validate input.
562 */
563 AssertReturn(pszDesc && *pszDesc, VERR_INVALID_PARAMETER);
564 AssertReturn(off + cb > off, VERR_INVALID_PARAMETER);
565 uint32_t const offPage = off & PAGE_OFFSET_MASK;
566 off &= ~(RTGCPHYS)PAGE_OFFSET_MASK;
567 cb += offPage;
568 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
569 const RTGCPHYS offEnd = off + cb;
570 AssertReturn(offEnd > off, VERR_INVALID_PARAMETER);
571 for (RTGCPHYS offCur = off; offCur < offEnd; offCur += PAGE_SIZE)
572 {
573 RTHCPHYS HCPhys;
574 rc = PGMR3PhysMMIO2GetHCPhys(pVM, pDevIns, iSubDev, iRegion, offCur, &HCPhys);
575 AssertMsgRCReturn(rc, ("rc=%Rrc - iSubDev=%#x iRegion=%#x off=%RGp\n", rc, iSubDev, iRegion, off), rc);
576 }
577
578 /*
579 * Add the memory to the hypervisor area.
580 */
581 RTGCPTR GCPtr;
582 PMMLOOKUPHYPER pLookup;
583 rc = mmR3HyperMap(pVM, cb, pszDesc, &GCPtr, &pLookup);
584 if (RT_SUCCESS(rc))
585 {
586 pLookup->enmType = MMLOOKUPHYPERTYPE_MMIO2;
587 pLookup->u.MMIO2.pDevIns = pDevIns;
588 pLookup->u.MMIO2.iSubDev = iSubDev;
589 pLookup->u.MMIO2.iRegion = iRegion;
590 pLookup->u.MMIO2.off = off;
591
592 /*
593 * Update the page table.
594 */
595 if (pVM->mm.s.fPGMInitialized)
596 {
597 for (RTGCPHYS offCur = off; offCur < offEnd; offCur += PAGE_SIZE)
598 {
599 RTHCPHYS HCPhys;
600 rc = PGMR3PhysMMIO2GetHCPhys(pVM, pDevIns, iSubDev, iRegion, offCur, &HCPhys);
601 AssertRCReturn(rc, rc);
602 rc = PGMMap(pVM, GCPtr + (offCur - off), HCPhys, PAGE_SIZE, 0);
603 if (RT_FAILURE(rc))
604 {
605 AssertMsgFailed(("rc=%Rrc offCur=%RGp %s\n", rc, offCur, pszDesc));
606 break;
607 }
608 }
609 }
610
611 if (RT_SUCCESS(rc))
612 {
613 GCPtr |= offPage;
614 *pRCPtr = GCPtr;
615 AssertLogRelReturn(*pRCPtr == GCPtr, VERR_INTERNAL_ERROR);
616 }
617 }
618 return rc;
619}
620
621#endif /* !PGM_WITHOUT_MAPPINGS */
622
623/**
624 * Maps locked R3 virtual memory into the hypervisor region in the GC.
625 *
626 * @return VBox status code.
627 *
628 * @param pVM The cross context VM structure.
629 * @param pvR3 The ring-3 address of the memory, must be page aligned.
630 * @param pvR0 The ring-0 address of the memory, must be page aligned. (optional)
631 * @param cPages The number of pages.
632 * @param paPages The page descriptors.
633 * @param pszDesc Mapping description.
634 * @param pGCPtr Where to store the GC address corresponding to pvR3.
635 */
636VMMR3DECL(int) MMR3HyperMapPages(PVM pVM, void *pvR3, RTR0PTR pvR0, size_t cPages, PCSUPPAGE paPages,
637 const char *pszDesc, PRTGCPTR pGCPtr)
638{
639 LogFlow(("MMR3HyperMapPages: pvR3=%p pvR0=%p cPages=%zu paPages=%p pszDesc=%p:{%s} pGCPtr=%p\n",
640 pvR3, pvR0, cPages, paPages, pszDesc, pszDesc, pGCPtr));
641
642 /*
643 * Validate input.
644 */
645 AssertPtrReturn(pvR3, VERR_INVALID_POINTER);
646 AssertPtrReturn(paPages, VERR_INVALID_POINTER);
647 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
648 AssertReturn(cPages <= VBOX_MAX_ALLOC_PAGE_COUNT, VERR_PAGE_COUNT_OUT_OF_RANGE);
649 AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
650 AssertReturn(*pszDesc, VERR_INVALID_PARAMETER);
651 AssertPtrReturn(pGCPtr, VERR_INVALID_PARAMETER);
652
653 /*
654 * Add the memory to the hypervisor area.
655 */
656 RTGCPTR GCPtr;
657 PMMLOOKUPHYPER pLookup;
658 int rc = mmR3HyperMap(pVM, cPages << PAGE_SHIFT, pszDesc, &GCPtr, &pLookup);
659 if (RT_SUCCESS(rc))
660 {
661 /*
662 * Copy the physical page addresses and tell PGM about them.
663 */
664 PRTHCPHYS paHCPhysPages = (PRTHCPHYS)MMR3HeapAlloc(pVM, MM_TAG_MM, sizeof(RTHCPHYS) * cPages);
665 if (paHCPhysPages)
666 {
667 for (size_t i = 0; i < cPages; i++)
668 {
669 AssertReleaseMsgReturn( paPages[i].Phys != 0
670 && paPages[i].Phys != NIL_RTHCPHYS
671 && !(paPages[i].Phys & PAGE_OFFSET_MASK),
672 ("i=%#zx Phys=%RHp %s\n", i, paPages[i].Phys, pszDesc),
673 VERR_INTERNAL_ERROR);
674 paHCPhysPages[i] = paPages[i].Phys;
675 }
676
677#ifndef PGM_WITHOUT_MAPPINGS
678 if (pVM->mm.s.fPGMInitialized)
679 {
680 for (size_t i = 0; i < cPages; i++)
681 {
682 rc = PGMMap(pVM, GCPtr + (i << PAGE_SHIFT), paHCPhysPages[i], PAGE_SIZE, 0);
683 AssertRCBreak(rc);
684 }
685 }
686#endif
687 if (RT_SUCCESS(rc))
688 {
689 pLookup->enmType = MMLOOKUPHYPERTYPE_LOCKED;
690 pLookup->u.Locked.pvR3 = pvR3;
691 pLookup->u.Locked.pvR0 = pvR0;
692 pLookup->u.Locked.paHCPhysPages = paHCPhysPages;
693
694 /* done. */
695 *pGCPtr = GCPtr;
696 return rc;
697 }
698 /* Don't care about failure clean, we're screwed if this fails anyway. */
699 }
700 }
701
702 return rc;
703}
704
705
706#ifndef PGM_WITHOUT_MAPPINGS
707/**
708 * Reserves a hypervisor memory area.
709 * Most frequent usage is fence pages and dynamically mappings like the guest PD and PDPT.
710 *
711 * @return VBox status code.
712 *
713 * @param pVM The cross context VM structure.
714 * @param cb Size of the memory. Will be rounded up to nearest page.
715 * @param pszDesc Mapping description.
716 * @param pGCPtr Where to store the assigned GC address. Optional.
717 */
718VMMR3DECL(int) MMR3HyperReserve(PVM pVM, unsigned cb, const char *pszDesc, PRTGCPTR pGCPtr)
719{
720 LogFlow(("MMR3HyperMapHCRam: cb=%d pszDesc=%p:{%s} pGCPtr=%p\n", (int)cb, pszDesc, pszDesc, pGCPtr));
721
722 /*
723 * Validate input.
724 */
725 if ( cb <= 0
726 || !pszDesc
727 || !*pszDesc)
728 {
729 AssertMsgFailed(("Invalid parameter\n"));
730 return VERR_INVALID_PARAMETER;
731 }
732
733 /*
734 * Add the memory to the hypervisor area.
735 */
736 RTGCPTR GCPtr;
737 PMMLOOKUPHYPER pLookup;
738 int rc = mmR3HyperMap(pVM, cb, pszDesc, &GCPtr, &pLookup);
739 if (RT_SUCCESS(rc))
740 {
741 pLookup->enmType = MMLOOKUPHYPERTYPE_DYNAMIC;
742 if (pGCPtr)
743 *pGCPtr = GCPtr;
744 return VINF_SUCCESS;
745 }
746 return rc;
747}
748#endif /* !PGM_WITHOUT_MAPPINGS */
749
750
751/**
752 * Reserves an electric fence page.
753 *
754 * @returns VBox status code.
755 * @param pVM The cross context VM structure.
756 */
757VMMR3DECL(int) MMR3HyperReserveFence(PVM pVM)
758{
759#ifndef PGM_WITHOUT_MAPPINGS
760 return MMR3HyperReserve(pVM, cb, "fence", NULL);
761#else
762 RT_NOREF(pVM);
763 return VINF_SUCCESS;
764#endif
765}
766
767
768/**
769 * Adds memory to the hypervisor memory arena.
770 *
771 * @return VBox status code.
772 * @param pVM The cross context VM structure.
773 * @param cb Size of the memory. Will be rounded up to nearest page.
774 * @param pszDesc The description of the memory.
775 * @param pGCPtr Where to store the GC address.
776 * @param ppLookup Where to store the pointer to the lookup record.
777 * @remark We assume the threading structure of VBox imposes natural
778 * serialization of most functions, this one included.
779 */
780static int mmR3HyperMap(PVM pVM, const size_t cb, const char *pszDesc, PRTGCPTR pGCPtr, PMMLOOKUPHYPER *ppLookup)
781{
782 /*
783 * Validate input.
784 */
785 const uint32_t cbAligned = RT_ALIGN_32(cb, PAGE_SIZE);
786 AssertReturn(cbAligned >= cb, VERR_INVALID_PARAMETER);
787 if (pVM->mm.s.offHyperNextStatic + cbAligned >= pVM->mm.s.cbHyperArea) /* don't use the last page, it's a fence. */
788 {
789 AssertMsgFailed(("Out of static mapping space in the HMA! offHyperAreaGC=%x cbAligned=%x cbHyperArea=%x\n",
790 pVM->mm.s.offHyperNextStatic, cbAligned, pVM->mm.s.cbHyperArea));
791 return VERR_NO_MEMORY;
792 }
793
794 /*
795 * Allocate lookup record.
796 */
797 PMMLOOKUPHYPER pLookup;
798 int rc = MMHyperAlloc(pVM, sizeof(*pLookup), 1, MM_TAG_MM, (void **)&pLookup);
799 if (RT_SUCCESS(rc))
800 {
801 /*
802 * Initialize it and insert it.
803 */
804 pLookup->offNext = pVM->mm.s.offLookupHyper;
805 pLookup->cb = cbAligned;
806 pLookup->off = pVM->mm.s.offHyperNextStatic;
807 pVM->mm.s.offLookupHyper = (uint8_t *)pLookup - (uint8_t *)pVM->mm.s.pHyperHeapR3;
808 if (pLookup->offNext != (int32_t)NIL_OFFSET)
809 pLookup->offNext -= pVM->mm.s.offLookupHyper;
810 pLookup->enmType = MMLOOKUPHYPERTYPE_INVALID;
811 memset(&pLookup->u, 0xff, sizeof(pLookup->u));
812 pLookup->pszDesc = pszDesc;
813
814 /* Mapping. */
815 *pGCPtr = pVM->mm.s.pvHyperAreaGC + pVM->mm.s.offHyperNextStatic;
816 pVM->mm.s.offHyperNextStatic += cbAligned;
817
818 /* Return pointer. */
819 *ppLookup = pLookup;
820 }
821
822 AssertRC(rc);
823 LogFlow(("mmR3HyperMap: returns %Rrc *pGCPtr=%RGv\n", rc, *pGCPtr));
824 return rc;
825}
826
827
828/**
829 * Allocates a new heap.
830 *
831 * @returns VBox status code.
832 * @param pVM The cross context VM structure.
833 * @param cb The size of the new heap.
834 * @param ppHeap Where to store the heap pointer on successful return.
835 * @param pR0PtrHeap Where to store the ring-0 address of the heap on
836 * success.
837 */
838static int mmR3HyperHeapCreate(PVM pVM, const size_t cb, PMMHYPERHEAP *ppHeap, PRTR0PTR pR0PtrHeap)
839{
840 /*
841 * Allocate the hypervisor heap.
842 */
843 const uint32_t cbAligned = RT_ALIGN_32(cb, PAGE_SIZE);
844 AssertReturn(cbAligned >= cb, VERR_INVALID_PARAMETER);
845 uint32_t const cPages = cbAligned >> PAGE_SHIFT;
846 PSUPPAGE paPages = (PSUPPAGE)MMR3HeapAlloc(pVM, MM_TAG_MM, cPages * sizeof(paPages[0]));
847 if (!paPages)
848 return VERR_NO_MEMORY;
849 void *pv;
850 RTR0PTR pvR0 = NIL_RTR0PTR;
851 int rc = SUPR3PageAllocEx(cPages,
852 0 /*fFlags*/,
853 &pv,
854 &pvR0,
855 paPages);
856 if (RT_SUCCESS(rc))
857 {
858 Assert(pvR0 != NIL_RTR0PTR && !(PAGE_OFFSET_MASK & pvR0));
859 memset(pv, 0, cbAligned);
860
861 /*
862 * Initialize the heap and first free chunk.
863 */
864 PMMHYPERHEAP pHeap = (PMMHYPERHEAP)pv;
865 pHeap->u32Magic = MMHYPERHEAP_MAGIC;
866 pHeap->pbHeapR3 = (uint8_t *)pHeap + MMYPERHEAP_HDR_SIZE;
867 pHeap->pbHeapR0 = pvR0 + MMYPERHEAP_HDR_SIZE;
868 //pHeap->pbHeapRC = 0; // set by mmR3HyperHeapMap()
869 pHeap->pVMR3 = pVM;
870 pHeap->pVMR0 = pVM->pVMR0ForCall;
871 pHeap->pVMRC = pVM->pVMRC;
872 pHeap->cbHeap = cbAligned - MMYPERHEAP_HDR_SIZE;
873 pHeap->cbFree = pHeap->cbHeap - sizeof(MMHYPERCHUNK);
874 //pHeap->offFreeHead = 0;
875 //pHeap->offFreeTail = 0;
876 pHeap->offPageAligned = pHeap->cbHeap;
877 //pHeap->HyperHeapStatTree = 0;
878 pHeap->paPages = paPages;
879
880 PMMHYPERCHUNKFREE pFree = (PMMHYPERCHUNKFREE)pHeap->pbHeapR3;
881 pFree->cb = pHeap->cbFree;
882 //pFree->core.offNext = 0;
883 MMHYPERCHUNK_SET_TYPE(&pFree->core, MMHYPERCHUNK_FLAGS_FREE);
884 pFree->core.offHeap = -(int32_t)MMYPERHEAP_HDR_SIZE;
885 //pFree->offNext = 0;
886 //pFree->offPrev = 0;
887
888 STAMR3Register(pVM, &pHeap->cbHeap, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, "/MM/HyperHeap/cbHeap", STAMUNIT_BYTES, "The heap size.");
889 STAMR3Register(pVM, &pHeap->cbFree, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, "/MM/HyperHeap/cbFree", STAMUNIT_BYTES, "The free space.");
890
891 *ppHeap = pHeap;
892 *pR0PtrHeap = pvR0;
893 return VINF_SUCCESS;
894 }
895 AssertMsgFailed(("SUPR3PageAllocEx(%d,,,,) -> %Rrc\n", cbAligned >> PAGE_SHIFT, rc));
896
897 *ppHeap = NULL;
898 return rc;
899}
900
901
902/**
903 * Allocates a new heap.
904 */
905static int mmR3HyperHeapMap(PVM pVM, PMMHYPERHEAP pHeap, PRTGCPTR ppHeapGC)
906{
907 Assert(RT_ALIGN_Z(pHeap->cbHeap + MMYPERHEAP_HDR_SIZE, PAGE_SIZE) == pHeap->cbHeap + MMYPERHEAP_HDR_SIZE);
908 Assert(pHeap->pbHeapR0);
909 Assert(pHeap->paPages);
910 int rc = MMR3HyperMapPages(pVM,
911 pHeap,
912 pHeap->pbHeapR0 - MMYPERHEAP_HDR_SIZE,
913 (pHeap->cbHeap + MMYPERHEAP_HDR_SIZE) >> PAGE_SHIFT,
914 pHeap->paPages,
915 "Heap", ppHeapGC);
916 if (RT_SUCCESS(rc))
917 {
918 pHeap->pVMRC = pVM->pVMRC;
919 pHeap->pbHeapRC = *ppHeapGC + MMYPERHEAP_HDR_SIZE;
920 /* Reserve a page for fencing. */
921 MMR3HyperReserveFence(pVM);
922
923 /* We won't need these any more. */
924 MMR3HeapFree(pHeap->paPages);
925 pHeap->paPages = NULL;
926 }
927 return rc;
928}
929
930
931/**
932 * Allocates memory in the Hypervisor (GC VMM) area which never will
933 * be freed and doesn't have any offset based relation to other heap blocks.
934 *
935 * The latter means that two blocks allocated by this API will not have the
936 * same relative position to each other in GC and HC. In short, never use
937 * this API for allocating nodes for an offset based AVL tree!
938 *
939 * The returned memory is of course zeroed.
940 *
941 * @returns VBox status code.
942 * @param pVM The cross context VM structure.
943 * @param cb Number of bytes to allocate.
944 * @param uAlignment Required memory alignment in bytes.
945 * Values are 0,8,16,32 and PAGE_SIZE.
946 * 0 -> default alignment, i.e. 8 bytes.
947 * @param enmTag The statistics tag.
948 * @param ppv Where to store the address to the allocated
949 * memory.
950 * @remark This is assumed not to be used at times when serialization is required.
951 */
952VMMR3DECL(int) MMR3HyperAllocOnceNoRel(PVM pVM, size_t cb, unsigned uAlignment, MMTAG enmTag, void **ppv)
953{
954 return MMR3HyperAllocOnceNoRelEx(pVM, cb, uAlignment, enmTag, 0/*fFlags*/, ppv);
955}
956
957
958/**
959 * Allocates memory in the Hypervisor (GC VMM) area which never will
960 * be freed and doesn't have any offset based relation to other heap blocks.
961 *
962 * The latter means that two blocks allocated by this API will not have the
963 * same relative position to each other in GC and HC. In short, never use
964 * this API for allocating nodes for an offset based AVL tree!
965 *
966 * The returned memory is of course zeroed.
967 *
968 * @returns VBox status code.
969 * @param pVM The cross context VM structure.
970 * @param cb Number of bytes to allocate.
971 * @param uAlignment Required memory alignment in bytes.
972 * Values are 0,8,16,32 and PAGE_SIZE.
973 * 0 -> default alignment, i.e. 8 bytes.
974 * @param enmTag The statistics tag.
975 * @param fFlags Flags, see MMHYPER_AONR_FLAGS_KERNEL_MAPPING.
976 * @param ppv Where to store the address to the allocated memory.
977 * @remark This is assumed not to be used at times when serialization is required.
978 */
979VMMR3DECL(int) MMR3HyperAllocOnceNoRelEx(PVM pVM, size_t cb, unsigned uAlignment, MMTAG enmTag, uint32_t fFlags, void **ppv)
980{
981 AssertMsg(cb >= 8, ("Hey! Do you really mean to allocate less than 8 bytes?! cb=%d\n", cb));
982 Assert(!(fFlags & ~(MMHYPER_AONR_FLAGS_KERNEL_MAPPING)));
983
984 /*
985 * Choose between allocating a new chunk of HMA memory
986 * and the heap. We will only do BIG allocations from HMA and
987 * only at creation time.
988 */
989 if ( ( cb < _64K
990 && ( uAlignment != PAGE_SIZE
991 || cb < 48*_1K)
992 && !(fFlags & MMHYPER_AONR_FLAGS_KERNEL_MAPPING)
993 )
994 || VMR3GetState(pVM) != VMSTATE_CREATING
995 )
996 {
997 Assert(!(fFlags & MMHYPER_AONR_FLAGS_KERNEL_MAPPING));
998 int rc = MMHyperAlloc(pVM, cb, uAlignment, enmTag, ppv);
999 if ( rc != VERR_MM_HYPER_NO_MEMORY
1000 || cb <= 8*_1K)
1001 {
1002 Log2(("MMR3HyperAllocOnceNoRel: cb=%#zx uAlignment=%#x returns %Rrc and *ppv=%p\n",
1003 cb, uAlignment, rc, *ppv));
1004 return rc;
1005 }
1006 }
1007
1008#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1009 /*
1010 * Set MMHYPER_AONR_FLAGS_KERNEL_MAPPING if we're in going to execute in ring-0.
1011 */
1012 if (VM_IS_HM_OR_NEM_ENABLED(pVM))
1013 fFlags |= MMHYPER_AONR_FLAGS_KERNEL_MAPPING;
1014#endif
1015
1016 /*
1017 * Validate alignment.
1018 */
1019 switch (uAlignment)
1020 {
1021 case 0:
1022 case 8:
1023 case 16:
1024 case 32:
1025 case PAGE_SIZE:
1026 break;
1027 default:
1028 AssertMsgFailed(("Invalid alignment %u\n", uAlignment));
1029 return VERR_INVALID_PARAMETER;
1030 }
1031
1032 /*
1033 * Allocate the pages and map them into HMA space.
1034 */
1035 uint32_t const cbAligned = RT_ALIGN_32(cb, PAGE_SIZE);
1036 AssertReturn(cbAligned >= cb, VERR_INVALID_PARAMETER);
1037 uint32_t const cPages = cbAligned >> PAGE_SHIFT;
1038 PSUPPAGE paPages = (PSUPPAGE)RTMemTmpAlloc(cPages * sizeof(paPages[0]));
1039 if (!paPages)
1040 return VERR_NO_TMP_MEMORY;
1041 void *pvPages;
1042 RTR0PTR pvR0 = NIL_RTR0PTR;
1043 int rc = SUPR3PageAllocEx(cPages,
1044 0 /*fFlags*/,
1045 &pvPages,
1046 &pvR0,
1047 paPages);
1048 if (RT_SUCCESS(rc))
1049 {
1050 Assert(pvR0 != NIL_RTR0PTR);
1051 memset(pvPages, 0, cbAligned);
1052
1053 RTGCPTR GCPtr;
1054 rc = MMR3HyperMapPages(pVM,
1055 pvPages,
1056 pvR0,
1057 cPages,
1058 paPages,
1059 MMR3HeapAPrintf(pVM, MM_TAG_MM, "alloc once (%s)", mmGetTagName(enmTag)),
1060 &GCPtr);
1061 /* not needed anymore */
1062 RTMemTmpFree(paPages);
1063 if (RT_SUCCESS(rc))
1064 {
1065 *ppv = pvPages;
1066 Log2(("MMR3HyperAllocOnceNoRel: cbAligned=%#x uAlignment=%#x returns VINF_SUCCESS and *ppv=%p\n",
1067 cbAligned, uAlignment, *ppv));
1068 MMR3HyperReserveFence(pVM);
1069 return rc;
1070 }
1071 AssertMsgFailed(("Failed to allocate %zd bytes! %Rrc\n", cbAligned, rc));
1072 SUPR3PageFreeEx(pvPages, cPages);
1073
1074
1075 /*
1076 * HACK ALERT! Try allocate it off the heap so that we don't freak
1077 * out during vga/vmmdev mmio2 allocation with certain ram sizes.
1078 */
1079 /** @todo make a proper fix for this so we will never end up in this kind of situation! */
1080 Log(("MMR3HyperAllocOnceNoRel: MMR3HyperMapHCRam failed with rc=%Rrc, try MMHyperAlloc(,%#x,,) instead\n", rc, cb));
1081 int rc2 = MMHyperAlloc(pVM, cb, uAlignment, enmTag, ppv);
1082 if (RT_SUCCESS(rc2))
1083 {
1084 Log2(("MMR3HyperAllocOnceNoRel: cb=%#x uAlignment=%#x returns %Rrc and *ppv=%p\n",
1085 cb, uAlignment, rc, *ppv));
1086 return rc;
1087 }
1088 }
1089 else
1090 AssertMsgFailed(("Failed to allocate %zd bytes! %Rrc\n", cbAligned, rc));
1091
1092 if (rc == VERR_NO_MEMORY)
1093 rc = VERR_MM_HYPER_NO_MEMORY;
1094 LogRel(("MMR3HyperAllocOnceNoRel: cb=%#zx uAlignment=%#x returns %Rrc\n", cb, uAlignment, rc));
1095 return rc;
1096}
1097
1098
1099/**
1100 * Lookus up a ring-3 pointer to HMA.
1101 *
1102 * @returns The lookup record on success, NULL on failure.
1103 * @param pVM The cross context VM structure.
1104 * @param pvR3 The ring-3 address to look up.
1105 */
1106DECLINLINE(PMMLOOKUPHYPER) mmR3HyperLookupR3(PVM pVM, void *pvR3)
1107{
1108 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
1109 for (;;)
1110 {
1111 switch (pLookup->enmType)
1112 {
1113 case MMLOOKUPHYPERTYPE_LOCKED:
1114 {
1115 unsigned off = (uint8_t *)pvR3 - (uint8_t *)pLookup->u.Locked.pvR3;
1116 if (off < pLookup->cb)
1117 return pLookup;
1118 break;
1119 }
1120
1121 case MMLOOKUPHYPERTYPE_HCPHYS:
1122 {
1123 unsigned off = (uint8_t *)pvR3 - (uint8_t *)pLookup->u.HCPhys.pvR3;
1124 if (off < pLookup->cb)
1125 return pLookup;
1126 break;
1127 }
1128
1129 case MMLOOKUPHYPERTYPE_GCPHYS:
1130 case MMLOOKUPHYPERTYPE_MMIO2:
1131 case MMLOOKUPHYPERTYPE_DYNAMIC:
1132 /** @todo ? */
1133 break;
1134
1135 default:
1136 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
1137 return NULL;
1138 }
1139
1140 /* next */
1141 if ((unsigned)pLookup->offNext == NIL_OFFSET)
1142 return NULL;
1143 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
1144 }
1145}
1146
1147
1148/**
1149 * Set / unset guard status on one or more hyper heap pages.
1150 *
1151 * @returns VBox status code (first failure).
1152 * @param pVM The cross context VM structure.
1153 * @param pvStart The hyper heap page address. Must be page
1154 * aligned.
1155 * @param cb The number of bytes. Must be page aligned.
1156 * @param fSet Whether to set or unset guard page status.
1157 */
1158VMMR3DECL(int) MMR3HyperSetGuard(PVM pVM, void *pvStart, size_t cb, bool fSet)
1159{
1160 /*
1161 * Validate input.
1162 */
1163 AssertReturn(!((uintptr_t)pvStart & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
1164 AssertReturn(!(cb & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
1165 AssertReturn(cb <= UINT32_MAX, VERR_INVALID_PARAMETER);
1166 PMMLOOKUPHYPER pLookup = mmR3HyperLookupR3(pVM, pvStart);
1167 AssertReturn(pLookup, VERR_INVALID_PARAMETER);
1168 AssertReturn(pLookup->enmType == MMLOOKUPHYPERTYPE_LOCKED, VERR_INVALID_PARAMETER);
1169
1170 /*
1171 * Get down to business.
1172 * Note! We quietly ignore errors from the support library since the
1173 * protection stuff isn't possible to implement on all platforms.
1174 */
1175 uint8_t *pbR3 = (uint8_t *)pLookup->u.Locked.pvR3;
1176 RTR0PTR R0Ptr = pLookup->u.Locked.pvR0 != (uintptr_t)pLookup->u.Locked.pvR3
1177 ? pLookup->u.Locked.pvR0
1178 : NIL_RTR0PTR;
1179 uint32_t off = (uint32_t)((uint8_t *)pvStart - pbR3);
1180 int rc;
1181 if (fSet)
1182 {
1183#ifndef PGM_WITHOUT_MAPPINGS
1184 rc = PGMMapSetPage(pVM, MMHyperR3ToRC(pVM, pvStart), cb, 0);
1185#else
1186 rc = VINF_SUCCESS;
1187#endif
1188 SUPR3PageProtect(pbR3, R0Ptr, off, (uint32_t)cb, RTMEM_PROT_NONE);
1189 }
1190 else
1191 {
1192#ifndef PGM_WITHOUT_MAPPINGS
1193 rc = PGMMapSetPage(pVM, MMHyperR3ToRC(pVM, pvStart), cb, X86_PTE_P | X86_PTE_A | X86_PTE_D | X86_PTE_RW);
1194#else
1195 rc = VINF_SUCCESS;
1196#endif
1197 SUPR3PageProtect(pbR3, R0Ptr, off, (uint32_t)cb, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
1198 }
1199 return rc;
1200}
1201
1202
1203/**
1204 * Convert hypervisor HC virtual address to HC physical address.
1205 *
1206 * @returns HC physical address.
1207 * @param pVM The cross context VM structure.
1208 * @param pvR3 Host context virtual address.
1209 */
1210VMMR3DECL(RTHCPHYS) MMR3HyperHCVirt2HCPhys(PVM pVM, void *pvR3)
1211{
1212 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
1213 for (;;)
1214 {
1215 switch (pLookup->enmType)
1216 {
1217 case MMLOOKUPHYPERTYPE_LOCKED:
1218 {
1219 unsigned off = (uint8_t *)pvR3 - (uint8_t *)pLookup->u.Locked.pvR3;
1220 if (off < pLookup->cb)
1221 return pLookup->u.Locked.paHCPhysPages[off >> PAGE_SHIFT] | (off & PAGE_OFFSET_MASK);
1222 break;
1223 }
1224
1225 case MMLOOKUPHYPERTYPE_HCPHYS:
1226 {
1227 unsigned off = (uint8_t *)pvR3 - (uint8_t *)pLookup->u.HCPhys.pvR3;
1228 if (off < pLookup->cb)
1229 return pLookup->u.HCPhys.HCPhys + off;
1230 break;
1231 }
1232
1233 case MMLOOKUPHYPERTYPE_GCPHYS:
1234 case MMLOOKUPHYPERTYPE_MMIO2:
1235 case MMLOOKUPHYPERTYPE_DYNAMIC:
1236 /* can (or don't want to) convert these kind of records. */
1237 break;
1238
1239 default:
1240 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
1241 break;
1242 }
1243
1244 /* next */
1245 if ((unsigned)pLookup->offNext == NIL_OFFSET)
1246 break;
1247 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
1248 }
1249
1250 AssertMsgFailed(("pvR3=%p is not inside the hypervisor memory area!\n", pvR3));
1251 return NIL_RTHCPHYS;
1252}
1253
1254#ifndef PGM_WITHOUT_MAPPINGS
1255
1256/**
1257 * Implements the hcphys-not-found return case of MMR3HyperQueryInfoFromHCPhys.
1258 *
1259 * @returns VINF_SUCCESS, VINF_BUFFER_OVERFLOW.
1260 * @param pVM The cross context VM structure.
1261 * @param HCPhys The host physical address to look for.
1262 * @param pLookup The HMA lookup entry corresponding to HCPhys.
1263 * @param pszWhat Where to return the description.
1264 * @param cbWhat Size of the return buffer.
1265 * @param pcbAlloc Where to return the size of whatever it is.
1266 */
1267static int mmR3HyperQueryInfoFromHCPhysFound(PVM pVM, RTHCPHYS HCPhys, PMMLOOKUPHYPER pLookup,
1268 char *pszWhat, size_t cbWhat, uint32_t *pcbAlloc)
1269{
1270 NOREF(pVM); NOREF(HCPhys);
1271 *pcbAlloc = pLookup->cb;
1272 int rc = RTStrCopy(pszWhat, cbWhat, pLookup->pszDesc);
1273 return rc == VERR_BUFFER_OVERFLOW ? VINF_BUFFER_OVERFLOW : rc;
1274}
1275
1276
1277/**
1278 * Scans the HMA for the physical page and reports back a description if found.
1279 *
1280 * @returns VINF_SUCCESS, VINF_BUFFER_OVERFLOW, VERR_NOT_FOUND.
1281 * @param pVM The cross context VM structure.
1282 * @param HCPhys The host physical address to look for.
1283 * @param pszWhat Where to return the description.
1284 * @param cbWhat Size of the return buffer.
1285 * @param pcbAlloc Where to return the size of whatever it is.
1286 */
1287VMMR3_INT_DECL(int) MMR3HyperQueryInfoFromHCPhys(PVM pVM, RTHCPHYS HCPhys, char *pszWhat, size_t cbWhat, uint32_t *pcbAlloc)
1288{
1289 RTHCPHYS HCPhysPage = HCPhys & ~(RTHCPHYS)PAGE_OFFSET_MASK;
1290 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
1291 for (;;)
1292 {
1293 switch (pLookup->enmType)
1294 {
1295 case MMLOOKUPHYPERTYPE_LOCKED:
1296 {
1297 uint32_t i = pLookup->cb >> PAGE_SHIFT;
1298 while (i-- > 0)
1299 if (pLookup->u.Locked.paHCPhysPages[i] == HCPhysPage)
1300 return mmR3HyperQueryInfoFromHCPhysFound(pVM, HCPhys, pLookup, pszWhat, cbWhat, pcbAlloc);
1301 break;
1302 }
1303
1304 case MMLOOKUPHYPERTYPE_HCPHYS:
1305 {
1306 if (pLookup->u.HCPhys.HCPhys - HCPhysPage < pLookup->cb)
1307 return mmR3HyperQueryInfoFromHCPhysFound(pVM, HCPhys, pLookup, pszWhat, cbWhat, pcbAlloc);
1308 break;
1309 }
1310
1311 case MMLOOKUPHYPERTYPE_MMIO2:
1312 case MMLOOKUPHYPERTYPE_GCPHYS:
1313 case MMLOOKUPHYPERTYPE_DYNAMIC:
1314 {
1315 /* brute force. */
1316 uint32_t i = pLookup->cb >> PAGE_SHIFT;
1317 while (i-- > 0)
1318 {
1319 RTGCPTR GCPtr = pLookup->off + pVM->mm.s.pvHyperAreaGC;
1320 RTHCPHYS HCPhysCur;
1321 int rc = PGMMapGetPage(pVM, GCPtr, NULL, &HCPhysCur);
1322 if (RT_SUCCESS(rc) && HCPhysCur == HCPhysPage)
1323 return mmR3HyperQueryInfoFromHCPhysFound(pVM, HCPhys, pLookup, pszWhat, cbWhat, pcbAlloc);
1324 }
1325 break;
1326 }
1327 default:
1328 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
1329 break;
1330 }
1331
1332 /* next */
1333 if ((unsigned)pLookup->offNext == NIL_OFFSET)
1334 break;
1335 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
1336 }
1337 return VERR_NOT_FOUND;
1338}
1339
1340
1341/**
1342 * Read hypervisor memory from GC virtual address.
1343 *
1344 * @returns VBox status code.
1345 * @param pVM The cross context VM structure.
1346 * @param pvDst Destination address (HC of course).
1347 * @param GCPtr GC virtual address.
1348 * @param cb Number of bytes to read.
1349 *
1350 * @remarks For DBGF only.
1351 */
1352VMMR3DECL(int) MMR3HyperReadGCVirt(PVM pVM, void *pvDst, RTGCPTR GCPtr, size_t cb)
1353{
1354 if (GCPtr - pVM->mm.s.pvHyperAreaGC >= pVM->mm.s.cbHyperArea)
1355 return VERR_INVALID_POINTER;
1356 return PGMR3MapRead(pVM, pvDst, GCPtr, cb);
1357}
1358
1359#endif /* !PGM_WITHOUT_MAPPINGS */
1360
1361/**
1362 * Info handler for 'hma', it dumps the list of lookup records for the hypervisor memory area.
1363 *
1364 * @param pVM The cross context VM structure.
1365 * @param pHlp Callback functions for doing output.
1366 * @param pszArgs Argument string. Optional and specific to the handler.
1367 */
1368static DECLCALLBACK(void) mmR3HyperInfoHma(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
1369{
1370 NOREF(pszArgs);
1371
1372 pHlp->pfnPrintf(pHlp, "Hypervisor Memory Area (HMA) Layout: Base %RGv, 0x%08x bytes\n",
1373 pVM->mm.s.pvHyperAreaGC, pVM->mm.s.cbHyperArea);
1374
1375 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
1376 for (;;)
1377 {
1378 switch (pLookup->enmType)
1379 {
1380 case MMLOOKUPHYPERTYPE_LOCKED:
1381 pHlp->pfnPrintf(pHlp, "%RGv-%RGv %RHv %RHv LOCKED %-*s %s\n",
1382 pLookup->off + pVM->mm.s.pvHyperAreaGC,
1383 pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
1384 pLookup->u.Locked.pvR3,
1385 pLookup->u.Locked.pvR0,
1386 sizeof(RTHCPTR) * 2, "",
1387 pLookup->pszDesc);
1388 break;
1389
1390 case MMLOOKUPHYPERTYPE_HCPHYS:
1391 pHlp->pfnPrintf(pHlp, "%RGv-%RGv %RHv %RHv HCPHYS %RHp %s\n",
1392 pLookup->off + pVM->mm.s.pvHyperAreaGC,
1393 pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
1394 pLookup->u.HCPhys.pvR3,
1395 pLookup->u.HCPhys.pvR0,
1396 pLookup->u.HCPhys.HCPhys,
1397 pLookup->pszDesc);
1398 break;
1399
1400 case MMLOOKUPHYPERTYPE_GCPHYS:
1401 pHlp->pfnPrintf(pHlp, "%RGv-%RGv %*s GCPHYS %RGp%*s %s\n",
1402 pLookup->off + pVM->mm.s.pvHyperAreaGC,
1403 pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
1404 sizeof(RTHCPTR) * 2 * 2 + 1, "",
1405 pLookup->u.GCPhys.GCPhys, RT_ABS((int)(sizeof(RTHCPHYS) - sizeof(RTGCPHYS))) * 2, "",
1406 pLookup->pszDesc);
1407 break;
1408
1409 case MMLOOKUPHYPERTYPE_MMIO2:
1410 pHlp->pfnPrintf(pHlp, "%RGv-%RGv %*s MMIO2 %RGp%*s %s\n",
1411 pLookup->off + pVM->mm.s.pvHyperAreaGC,
1412 pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
1413 sizeof(RTHCPTR) * 2 * 2 + 1, "",
1414 pLookup->u.MMIO2.off, RT_ABS((int)(sizeof(RTHCPHYS) - sizeof(RTGCPHYS))) * 2, "",
1415 pLookup->pszDesc);
1416 break;
1417
1418 case MMLOOKUPHYPERTYPE_DYNAMIC:
1419 pHlp->pfnPrintf(pHlp, "%RGv-%RGv %*s DYNAMIC %*s %s\n",
1420 pLookup->off + pVM->mm.s.pvHyperAreaGC,
1421 pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
1422 sizeof(RTHCPTR) * 2 * 2 + 1, "",
1423 sizeof(RTHCPTR) * 2, "",
1424 pLookup->pszDesc);
1425 break;
1426
1427 default:
1428 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
1429 break;
1430 }
1431
1432 /* next */
1433 if ((unsigned)pLookup->offNext == NIL_OFFSET)
1434 break;
1435 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
1436 }
1437}
1438
1439
1440/**
1441 * Re-allocates memory from the hyper heap.
1442 *
1443 * @returns VBox status code.
1444 * @param pVM The cross context VM structure.
1445 * @param pvOld The existing block of memory in the hyper heap to
1446 * re-allocate (can be NULL).
1447 * @param cbOld Size of the existing block.
1448 * @param uAlignmentNew Required memory alignment in bytes. Values are
1449 * 0,8,16,32 and PAGE_SIZE. 0 -> default alignment,
1450 * i.e. 8 bytes.
1451 * @param enmTagNew The statistics tag.
1452 * @param cbNew The required size of the new block.
1453 * @param ppv Where to store the address to the re-allocated
1454 * block.
1455 *
1456 * @remarks This does not work like normal realloc() on failure, the memory
1457 * pointed to by @a pvOld is lost if there isn't sufficient space on
1458 * the hyper heap for the re-allocation to succeed.
1459*/
1460VMMR3DECL(int) MMR3HyperRealloc(PVM pVM, void *pvOld, size_t cbOld, unsigned uAlignmentNew, MMTAG enmTagNew, size_t cbNew,
1461 void **ppv)
1462{
1463 if (!pvOld)
1464 return MMHyperAlloc(pVM, cbNew, uAlignmentNew, enmTagNew, ppv);
1465
1466 if (!cbNew && pvOld)
1467 return MMHyperFree(pVM, pvOld);
1468
1469 if (cbOld == cbNew)
1470 return VINF_SUCCESS;
1471
1472 size_t cbData = RT_MIN(cbNew, cbOld);
1473 void *pvTmp = RTMemTmpAlloc(cbData);
1474 if (RT_UNLIKELY(!pvTmp))
1475 {
1476 MMHyperFree(pVM, pvOld);
1477 return VERR_NO_TMP_MEMORY;
1478 }
1479 memcpy(pvTmp, pvOld, cbData);
1480
1481 int rc = MMHyperFree(pVM, pvOld);
1482 if (RT_SUCCESS(rc))
1483 {
1484 rc = MMHyperAlloc(pVM, cbNew, uAlignmentNew, enmTagNew, ppv);
1485 if (RT_SUCCESS(rc))
1486 {
1487 Assert(cbData <= cbNew);
1488 memcpy(*ppv, pvTmp, cbData);
1489 }
1490 }
1491 else
1492 AssertMsgFailed(("Failed to free hyper heap block pvOld=%p cbOld=%u\n", pvOld, cbOld));
1493
1494 RTMemTmpFree(pvTmp);
1495 return rc;
1496}
1497
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