VirtualBox

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

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

VMM/PGM,++: Kicked out VBOX_WITH_2X_4GB_ADDR_SPACE and the DynMap code used by it and raw-mode. Kept this around in case we wanted to reuse it for SMAP workarounds, but that's no longer needed. bugref:9517 bugref:9627

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 53.1 KB
Line 
1/* $Id: MMHyper.cpp 91016 2021-08-31 01:23:53Z 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 /*
1009 * Validate alignment.
1010 */
1011 switch (uAlignment)
1012 {
1013 case 0:
1014 case 8:
1015 case 16:
1016 case 32:
1017 case PAGE_SIZE:
1018 break;
1019 default:
1020 AssertMsgFailed(("Invalid alignment %u\n", uAlignment));
1021 return VERR_INVALID_PARAMETER;
1022 }
1023
1024 /*
1025 * Allocate the pages and map them into HMA space.
1026 */
1027 uint32_t const cbAligned = RT_ALIGN_32(cb, PAGE_SIZE);
1028 AssertReturn(cbAligned >= cb, VERR_INVALID_PARAMETER);
1029 uint32_t const cPages = cbAligned >> PAGE_SHIFT;
1030 PSUPPAGE paPages = (PSUPPAGE)RTMemTmpAlloc(cPages * sizeof(paPages[0]));
1031 if (!paPages)
1032 return VERR_NO_TMP_MEMORY;
1033 void *pvPages;
1034 RTR0PTR pvR0 = NIL_RTR0PTR;
1035 int rc = SUPR3PageAllocEx(cPages,
1036 0 /*fFlags*/,
1037 &pvPages,
1038 &pvR0,
1039 paPages);
1040 if (RT_SUCCESS(rc))
1041 {
1042 Assert(pvR0 != NIL_RTR0PTR);
1043 memset(pvPages, 0, cbAligned);
1044
1045 RTGCPTR GCPtr;
1046 rc = MMR3HyperMapPages(pVM,
1047 pvPages,
1048 pvR0,
1049 cPages,
1050 paPages,
1051 MMR3HeapAPrintf(pVM, MM_TAG_MM, "alloc once (%s)", mmGetTagName(enmTag)),
1052 &GCPtr);
1053 /* not needed anymore */
1054 RTMemTmpFree(paPages);
1055 if (RT_SUCCESS(rc))
1056 {
1057 *ppv = pvPages;
1058 Log2(("MMR3HyperAllocOnceNoRel: cbAligned=%#x uAlignment=%#x returns VINF_SUCCESS and *ppv=%p\n",
1059 cbAligned, uAlignment, *ppv));
1060 MMR3HyperReserveFence(pVM);
1061 return rc;
1062 }
1063 AssertMsgFailed(("Failed to allocate %zd bytes! %Rrc\n", cbAligned, rc));
1064 SUPR3PageFreeEx(pvPages, cPages);
1065
1066
1067 /*
1068 * HACK ALERT! Try allocate it off the heap so that we don't freak
1069 * out during vga/vmmdev mmio2 allocation with certain ram sizes.
1070 */
1071 /** @todo make a proper fix for this so we will never end up in this kind of situation! */
1072 Log(("MMR3HyperAllocOnceNoRel: MMR3HyperMapHCRam failed with rc=%Rrc, try MMHyperAlloc(,%#x,,) instead\n", rc, cb));
1073 int rc2 = MMHyperAlloc(pVM, cb, uAlignment, enmTag, ppv);
1074 if (RT_SUCCESS(rc2))
1075 {
1076 Log2(("MMR3HyperAllocOnceNoRel: cb=%#x uAlignment=%#x returns %Rrc and *ppv=%p\n",
1077 cb, uAlignment, rc, *ppv));
1078 return rc;
1079 }
1080 }
1081 else
1082 AssertMsgFailed(("Failed to allocate %zd bytes! %Rrc\n", cbAligned, rc));
1083
1084 if (rc == VERR_NO_MEMORY)
1085 rc = VERR_MM_HYPER_NO_MEMORY;
1086 LogRel(("MMR3HyperAllocOnceNoRel: cb=%#zx uAlignment=%#x returns %Rrc\n", cb, uAlignment, rc));
1087 return rc;
1088}
1089
1090
1091/**
1092 * Lookus up a ring-3 pointer to HMA.
1093 *
1094 * @returns The lookup record on success, NULL on failure.
1095 * @param pVM The cross context VM structure.
1096 * @param pvR3 The ring-3 address to look up.
1097 */
1098DECLINLINE(PMMLOOKUPHYPER) mmR3HyperLookupR3(PVM pVM, void *pvR3)
1099{
1100 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
1101 for (;;)
1102 {
1103 switch (pLookup->enmType)
1104 {
1105 case MMLOOKUPHYPERTYPE_LOCKED:
1106 {
1107 unsigned off = (uint8_t *)pvR3 - (uint8_t *)pLookup->u.Locked.pvR3;
1108 if (off < pLookup->cb)
1109 return pLookup;
1110 break;
1111 }
1112
1113 case MMLOOKUPHYPERTYPE_HCPHYS:
1114 {
1115 unsigned off = (uint8_t *)pvR3 - (uint8_t *)pLookup->u.HCPhys.pvR3;
1116 if (off < pLookup->cb)
1117 return pLookup;
1118 break;
1119 }
1120
1121 case MMLOOKUPHYPERTYPE_GCPHYS:
1122 case MMLOOKUPHYPERTYPE_MMIO2:
1123 case MMLOOKUPHYPERTYPE_DYNAMIC:
1124 /** @todo ? */
1125 break;
1126
1127 default:
1128 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
1129 return NULL;
1130 }
1131
1132 /* next */
1133 if ((unsigned)pLookup->offNext == NIL_OFFSET)
1134 return NULL;
1135 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
1136 }
1137}
1138
1139
1140/**
1141 * Set / unset guard status on one or more hyper heap pages.
1142 *
1143 * @returns VBox status code (first failure).
1144 * @param pVM The cross context VM structure.
1145 * @param pvStart The hyper heap page address. Must be page
1146 * aligned.
1147 * @param cb The number of bytes. Must be page aligned.
1148 * @param fSet Whether to set or unset guard page status.
1149 */
1150VMMR3DECL(int) MMR3HyperSetGuard(PVM pVM, void *pvStart, size_t cb, bool fSet)
1151{
1152 /*
1153 * Validate input.
1154 */
1155 AssertReturn(!((uintptr_t)pvStart & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
1156 AssertReturn(!(cb & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
1157 AssertReturn(cb <= UINT32_MAX, VERR_INVALID_PARAMETER);
1158 PMMLOOKUPHYPER pLookup = mmR3HyperLookupR3(pVM, pvStart);
1159 AssertReturn(pLookup, VERR_INVALID_PARAMETER);
1160 AssertReturn(pLookup->enmType == MMLOOKUPHYPERTYPE_LOCKED, VERR_INVALID_PARAMETER);
1161
1162 /*
1163 * Get down to business.
1164 * Note! We quietly ignore errors from the support library since the
1165 * protection stuff isn't possible to implement on all platforms.
1166 */
1167 uint8_t *pbR3 = (uint8_t *)pLookup->u.Locked.pvR3;
1168 RTR0PTR R0Ptr = pLookup->u.Locked.pvR0 != (uintptr_t)pLookup->u.Locked.pvR3
1169 ? pLookup->u.Locked.pvR0
1170 : NIL_RTR0PTR;
1171 uint32_t off = (uint32_t)((uint8_t *)pvStart - pbR3);
1172 int rc;
1173 if (fSet)
1174 {
1175#ifndef PGM_WITHOUT_MAPPINGS
1176 rc = PGMMapSetPage(pVM, MMHyperR3ToRC(pVM, pvStart), cb, 0);
1177#else
1178 rc = VINF_SUCCESS;
1179#endif
1180 SUPR3PageProtect(pbR3, R0Ptr, off, (uint32_t)cb, RTMEM_PROT_NONE);
1181 }
1182 else
1183 {
1184#ifndef PGM_WITHOUT_MAPPINGS
1185 rc = PGMMapSetPage(pVM, MMHyperR3ToRC(pVM, pvStart), cb, X86_PTE_P | X86_PTE_A | X86_PTE_D | X86_PTE_RW);
1186#else
1187 rc = VINF_SUCCESS;
1188#endif
1189 SUPR3PageProtect(pbR3, R0Ptr, off, (uint32_t)cb, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
1190 }
1191 return rc;
1192}
1193
1194
1195/**
1196 * Convert hypervisor HC virtual address to HC physical address.
1197 *
1198 * @returns HC physical address.
1199 * @param pVM The cross context VM structure.
1200 * @param pvR3 Host context virtual address.
1201 */
1202VMMR3DECL(RTHCPHYS) MMR3HyperHCVirt2HCPhys(PVM pVM, void *pvR3)
1203{
1204 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
1205 for (;;)
1206 {
1207 switch (pLookup->enmType)
1208 {
1209 case MMLOOKUPHYPERTYPE_LOCKED:
1210 {
1211 unsigned off = (uint8_t *)pvR3 - (uint8_t *)pLookup->u.Locked.pvR3;
1212 if (off < pLookup->cb)
1213 return pLookup->u.Locked.paHCPhysPages[off >> PAGE_SHIFT] | (off & PAGE_OFFSET_MASK);
1214 break;
1215 }
1216
1217 case MMLOOKUPHYPERTYPE_HCPHYS:
1218 {
1219 unsigned off = (uint8_t *)pvR3 - (uint8_t *)pLookup->u.HCPhys.pvR3;
1220 if (off < pLookup->cb)
1221 return pLookup->u.HCPhys.HCPhys + off;
1222 break;
1223 }
1224
1225 case MMLOOKUPHYPERTYPE_GCPHYS:
1226 case MMLOOKUPHYPERTYPE_MMIO2:
1227 case MMLOOKUPHYPERTYPE_DYNAMIC:
1228 /* can (or don't want to) convert these kind of records. */
1229 break;
1230
1231 default:
1232 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
1233 break;
1234 }
1235
1236 /* next */
1237 if ((unsigned)pLookup->offNext == NIL_OFFSET)
1238 break;
1239 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
1240 }
1241
1242 AssertMsgFailed(("pvR3=%p is not inside the hypervisor memory area!\n", pvR3));
1243 return NIL_RTHCPHYS;
1244}
1245
1246#ifndef PGM_WITHOUT_MAPPINGS
1247
1248/**
1249 * Implements the hcphys-not-found return case of MMR3HyperQueryInfoFromHCPhys.
1250 *
1251 * @returns VINF_SUCCESS, VINF_BUFFER_OVERFLOW.
1252 * @param pVM The cross context VM structure.
1253 * @param HCPhys The host physical address to look for.
1254 * @param pLookup The HMA lookup entry corresponding to HCPhys.
1255 * @param pszWhat Where to return the description.
1256 * @param cbWhat Size of the return buffer.
1257 * @param pcbAlloc Where to return the size of whatever it is.
1258 */
1259static int mmR3HyperQueryInfoFromHCPhysFound(PVM pVM, RTHCPHYS HCPhys, PMMLOOKUPHYPER pLookup,
1260 char *pszWhat, size_t cbWhat, uint32_t *pcbAlloc)
1261{
1262 NOREF(pVM); NOREF(HCPhys);
1263 *pcbAlloc = pLookup->cb;
1264 int rc = RTStrCopy(pszWhat, cbWhat, pLookup->pszDesc);
1265 return rc == VERR_BUFFER_OVERFLOW ? VINF_BUFFER_OVERFLOW : rc;
1266}
1267
1268
1269/**
1270 * Scans the HMA for the physical page and reports back a description if found.
1271 *
1272 * @returns VINF_SUCCESS, VINF_BUFFER_OVERFLOW, VERR_NOT_FOUND.
1273 * @param pVM The cross context VM structure.
1274 * @param HCPhys The host physical address to look for.
1275 * @param pszWhat Where to return the description.
1276 * @param cbWhat Size of the return buffer.
1277 * @param pcbAlloc Where to return the size of whatever it is.
1278 */
1279VMMR3_INT_DECL(int) MMR3HyperQueryInfoFromHCPhys(PVM pVM, RTHCPHYS HCPhys, char *pszWhat, size_t cbWhat, uint32_t *pcbAlloc)
1280{
1281 RTHCPHYS HCPhysPage = HCPhys & ~(RTHCPHYS)PAGE_OFFSET_MASK;
1282 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
1283 for (;;)
1284 {
1285 switch (pLookup->enmType)
1286 {
1287 case MMLOOKUPHYPERTYPE_LOCKED:
1288 {
1289 uint32_t i = pLookup->cb >> PAGE_SHIFT;
1290 while (i-- > 0)
1291 if (pLookup->u.Locked.paHCPhysPages[i] == HCPhysPage)
1292 return mmR3HyperQueryInfoFromHCPhysFound(pVM, HCPhys, pLookup, pszWhat, cbWhat, pcbAlloc);
1293 break;
1294 }
1295
1296 case MMLOOKUPHYPERTYPE_HCPHYS:
1297 {
1298 if (pLookup->u.HCPhys.HCPhys - HCPhysPage < pLookup->cb)
1299 return mmR3HyperQueryInfoFromHCPhysFound(pVM, HCPhys, pLookup, pszWhat, cbWhat, pcbAlloc);
1300 break;
1301 }
1302
1303 case MMLOOKUPHYPERTYPE_MMIO2:
1304 case MMLOOKUPHYPERTYPE_GCPHYS:
1305 case MMLOOKUPHYPERTYPE_DYNAMIC:
1306 {
1307 /* brute force. */
1308 uint32_t i = pLookup->cb >> PAGE_SHIFT;
1309 while (i-- > 0)
1310 {
1311 RTGCPTR GCPtr = pLookup->off + pVM->mm.s.pvHyperAreaGC;
1312 RTHCPHYS HCPhysCur;
1313 int rc = PGMMapGetPage(pVM, GCPtr, NULL, &HCPhysCur);
1314 if (RT_SUCCESS(rc) && HCPhysCur == HCPhysPage)
1315 return mmR3HyperQueryInfoFromHCPhysFound(pVM, HCPhys, pLookup, pszWhat, cbWhat, pcbAlloc);
1316 }
1317 break;
1318 }
1319 default:
1320 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
1321 break;
1322 }
1323
1324 /* next */
1325 if ((unsigned)pLookup->offNext == NIL_OFFSET)
1326 break;
1327 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
1328 }
1329 return VERR_NOT_FOUND;
1330}
1331
1332
1333/**
1334 * Read hypervisor memory from GC virtual address.
1335 *
1336 * @returns VBox status code.
1337 * @param pVM The cross context VM structure.
1338 * @param pvDst Destination address (HC of course).
1339 * @param GCPtr GC virtual address.
1340 * @param cb Number of bytes to read.
1341 *
1342 * @remarks For DBGF only.
1343 */
1344VMMR3DECL(int) MMR3HyperReadGCVirt(PVM pVM, void *pvDst, RTGCPTR GCPtr, size_t cb)
1345{
1346 if (GCPtr - pVM->mm.s.pvHyperAreaGC >= pVM->mm.s.cbHyperArea)
1347 return VERR_INVALID_POINTER;
1348 return PGMR3MapRead(pVM, pvDst, GCPtr, cb);
1349}
1350
1351#endif /* !PGM_WITHOUT_MAPPINGS */
1352
1353/**
1354 * Info handler for 'hma', it dumps the list of lookup records for the hypervisor memory area.
1355 *
1356 * @param pVM The cross context VM structure.
1357 * @param pHlp Callback functions for doing output.
1358 * @param pszArgs Argument string. Optional and specific to the handler.
1359 */
1360static DECLCALLBACK(void) mmR3HyperInfoHma(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
1361{
1362 NOREF(pszArgs);
1363
1364 pHlp->pfnPrintf(pHlp, "Hypervisor Memory Area (HMA) Layout: Base %RGv, 0x%08x bytes\n",
1365 pVM->mm.s.pvHyperAreaGC, pVM->mm.s.cbHyperArea);
1366
1367 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
1368 for (;;)
1369 {
1370 switch (pLookup->enmType)
1371 {
1372 case MMLOOKUPHYPERTYPE_LOCKED:
1373 pHlp->pfnPrintf(pHlp, "%RGv-%RGv %RHv %RHv LOCKED %-*s %s\n",
1374 pLookup->off + pVM->mm.s.pvHyperAreaGC,
1375 pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
1376 pLookup->u.Locked.pvR3,
1377 pLookup->u.Locked.pvR0,
1378 sizeof(RTHCPTR) * 2, "",
1379 pLookup->pszDesc);
1380 break;
1381
1382 case MMLOOKUPHYPERTYPE_HCPHYS:
1383 pHlp->pfnPrintf(pHlp, "%RGv-%RGv %RHv %RHv HCPHYS %RHp %s\n",
1384 pLookup->off + pVM->mm.s.pvHyperAreaGC,
1385 pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
1386 pLookup->u.HCPhys.pvR3,
1387 pLookup->u.HCPhys.pvR0,
1388 pLookup->u.HCPhys.HCPhys,
1389 pLookup->pszDesc);
1390 break;
1391
1392 case MMLOOKUPHYPERTYPE_GCPHYS:
1393 pHlp->pfnPrintf(pHlp, "%RGv-%RGv %*s GCPHYS %RGp%*s %s\n",
1394 pLookup->off + pVM->mm.s.pvHyperAreaGC,
1395 pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
1396 sizeof(RTHCPTR) * 2 * 2 + 1, "",
1397 pLookup->u.GCPhys.GCPhys, RT_ABS((int)(sizeof(RTHCPHYS) - sizeof(RTGCPHYS))) * 2, "",
1398 pLookup->pszDesc);
1399 break;
1400
1401 case MMLOOKUPHYPERTYPE_MMIO2:
1402 pHlp->pfnPrintf(pHlp, "%RGv-%RGv %*s MMIO2 %RGp%*s %s\n",
1403 pLookup->off + pVM->mm.s.pvHyperAreaGC,
1404 pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
1405 sizeof(RTHCPTR) * 2 * 2 + 1, "",
1406 pLookup->u.MMIO2.off, RT_ABS((int)(sizeof(RTHCPHYS) - sizeof(RTGCPHYS))) * 2, "",
1407 pLookup->pszDesc);
1408 break;
1409
1410 case MMLOOKUPHYPERTYPE_DYNAMIC:
1411 pHlp->pfnPrintf(pHlp, "%RGv-%RGv %*s DYNAMIC %*s %s\n",
1412 pLookup->off + pVM->mm.s.pvHyperAreaGC,
1413 pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
1414 sizeof(RTHCPTR) * 2 * 2 + 1, "",
1415 sizeof(RTHCPTR) * 2, "",
1416 pLookup->pszDesc);
1417 break;
1418
1419 default:
1420 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
1421 break;
1422 }
1423
1424 /* next */
1425 if ((unsigned)pLookup->offNext == NIL_OFFSET)
1426 break;
1427 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
1428 }
1429}
1430
1431
1432/**
1433 * Re-allocates memory from the hyper heap.
1434 *
1435 * @returns VBox status code.
1436 * @param pVM The cross context VM structure.
1437 * @param pvOld The existing block of memory in the hyper heap to
1438 * re-allocate (can be NULL).
1439 * @param cbOld Size of the existing block.
1440 * @param uAlignmentNew Required memory alignment in bytes. Values are
1441 * 0,8,16,32 and PAGE_SIZE. 0 -> default alignment,
1442 * i.e. 8 bytes.
1443 * @param enmTagNew The statistics tag.
1444 * @param cbNew The required size of the new block.
1445 * @param ppv Where to store the address to the re-allocated
1446 * block.
1447 *
1448 * @remarks This does not work like normal realloc() on failure, the memory
1449 * pointed to by @a pvOld is lost if there isn't sufficient space on
1450 * the hyper heap for the re-allocation to succeed.
1451*/
1452VMMR3DECL(int) MMR3HyperRealloc(PVM pVM, void *pvOld, size_t cbOld, unsigned uAlignmentNew, MMTAG enmTagNew, size_t cbNew,
1453 void **ppv)
1454{
1455 if (!pvOld)
1456 return MMHyperAlloc(pVM, cbNew, uAlignmentNew, enmTagNew, ppv);
1457
1458 if (!cbNew && pvOld)
1459 return MMHyperFree(pVM, pvOld);
1460
1461 if (cbOld == cbNew)
1462 return VINF_SUCCESS;
1463
1464 size_t cbData = RT_MIN(cbNew, cbOld);
1465 void *pvTmp = RTMemTmpAlloc(cbData);
1466 if (RT_UNLIKELY(!pvTmp))
1467 {
1468 MMHyperFree(pVM, pvOld);
1469 return VERR_NO_TMP_MEMORY;
1470 }
1471 memcpy(pvTmp, pvOld, cbData);
1472
1473 int rc = MMHyperFree(pVM, pvOld);
1474 if (RT_SUCCESS(rc))
1475 {
1476 rc = MMHyperAlloc(pVM, cbNew, uAlignmentNew, enmTagNew, ppv);
1477 if (RT_SUCCESS(rc))
1478 {
1479 Assert(cbData <= cbNew);
1480 memcpy(*ppv, pvTmp, cbData);
1481 }
1482 }
1483 else
1484 AssertMsgFailed(("Failed to free hyper heap block pvOld=%p cbOld=%u\n", pvOld, cbOld));
1485
1486 RTMemTmpFree(pvTmp);
1487 return rc;
1488}
1489
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