VirtualBox

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

Last change on this file since 85972 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 53.7 KB
Line 
1/* $Id: MMHyper.cpp 82968 2020-02-04 10:35:17Z 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->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#endif /* !PGM_WITHOUT_MAPPINGS */
414
415
416/**
417 * Service a VMMCALLRING3_MMHYPER_LOCK call.
418 *
419 * @returns VBox status code.
420 * @param pVM The cross context VM structure.
421 */
422VMMR3DECL(int) MMR3LockCall(PVM pVM)
423{
424 PMMHYPERHEAP pHeap = pVM->mm.s.CTX_SUFF(pHyperHeap);
425
426 int rc = PDMR3CritSectEnterEx(&pHeap->Lock, true /* fHostCall */);
427 AssertRC(rc);
428 return rc;
429}
430
431
432#ifndef PGM_WITHOUT_MAPPINGS
433
434/**
435 * Maps contiguous HC physical memory into the hypervisor region in the GC.
436 *
437 * @return VBox status code.
438 *
439 * @param pVM The cross context VM structure.
440 * @param pvR3 Ring-3 address of the memory. Must be page aligned!
441 * @param pvR0 Optional ring-0 address of the memory.
442 * @param HCPhys Host context physical address of the memory to be
443 * mapped. Must be page aligned!
444 * @param cb Size of the memory. Will be rounded up to nearest page.
445 * @param pszDesc Description.
446 * @param pGCPtr Where to store the GC address.
447 */
448VMMR3DECL(int) MMR3HyperMapHCPhys(PVM pVM, void *pvR3, RTR0PTR pvR0, RTHCPHYS HCPhys, size_t cb,
449 const char *pszDesc, PRTGCPTR pGCPtr)
450{
451 LogFlow(("MMR3HyperMapHCPhys: pvR3=%p pvR0=%p HCPhys=%RHp cb=%d pszDesc=%p:{%s} pGCPtr=%p\n",
452 pvR3, pvR0, HCPhys, (int)cb, pszDesc, pszDesc, pGCPtr));
453
454 /*
455 * Validate input.
456 */
457 AssertReturn(RT_ALIGN_P(pvR3, PAGE_SIZE) == pvR3, VERR_INVALID_PARAMETER);
458 AssertReturn(RT_ALIGN_T(pvR0, PAGE_SIZE, RTR0PTR) == pvR0, VERR_INVALID_PARAMETER);
459 AssertReturn(RT_ALIGN_T(HCPhys, PAGE_SIZE, RTHCPHYS) == HCPhys, VERR_INVALID_PARAMETER);
460 AssertReturn(pszDesc && *pszDesc, VERR_INVALID_PARAMETER);
461
462 /*
463 * Add the memory to the hypervisor area.
464 */
465 uint32_t cbAligned = RT_ALIGN_32(cb, PAGE_SIZE);
466 AssertReturn(cbAligned >= cb, VERR_INVALID_PARAMETER);
467 RTGCPTR GCPtr;
468 PMMLOOKUPHYPER pLookup;
469 int rc = mmR3HyperMap(pVM, cbAligned, pszDesc, &GCPtr, &pLookup);
470 if (RT_SUCCESS(rc))
471 {
472 pLookup->enmType = MMLOOKUPHYPERTYPE_HCPHYS;
473 pLookup->u.HCPhys.pvR3 = pvR3;
474 pLookup->u.HCPhys.pvR0 = pvR0;
475 pLookup->u.HCPhys.HCPhys = HCPhys;
476
477 /*
478 * Update the page table.
479 */
480 if (pVM->mm.s.fPGMInitialized)
481 rc = PGMMap(pVM, GCPtr, HCPhys, cbAligned, 0);
482 if (RT_SUCCESS(rc))
483 *pGCPtr = GCPtr;
484 }
485 return rc;
486}
487
488
489/**
490 * Maps contiguous GC physical memory into the hypervisor region in the GC.
491 *
492 * @return VBox status code.
493 *
494 * @param pVM The cross context VM structure.
495 * @param GCPhys Guest context physical address of the memory to be mapped. Must be page aligned!
496 * @param cb Size of the memory. Will be rounded up to nearest page.
497 * @param pszDesc Mapping description.
498 * @param pGCPtr Where to store the GC address.
499 */
500VMMR3DECL(int) MMR3HyperMapGCPhys(PVM pVM, RTGCPHYS GCPhys, size_t cb, const char *pszDesc, PRTGCPTR pGCPtr)
501{
502 LogFlow(("MMR3HyperMapGCPhys: GCPhys=%RGp cb=%d pszDesc=%p:{%s} pGCPtr=%p\n", GCPhys, (int)cb, pszDesc, pszDesc, pGCPtr));
503
504 /*
505 * Validate input.
506 */
507 AssertReturn(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys, VERR_INVALID_PARAMETER);
508 AssertReturn(pszDesc && *pszDesc, VERR_INVALID_PARAMETER);
509
510 /*
511 * Add the memory to the hypervisor area.
512 */
513 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
514 RTGCPTR GCPtr;
515 PMMLOOKUPHYPER pLookup;
516 int rc = mmR3HyperMap(pVM, cb, pszDesc, &GCPtr, &pLookup);
517 if (RT_SUCCESS(rc))
518 {
519 pLookup->enmType = MMLOOKUPHYPERTYPE_GCPHYS;
520 pLookup->u.GCPhys.GCPhys = GCPhys;
521
522 /*
523 * Update the page table.
524 */
525 for (unsigned off = 0; off < cb; off += PAGE_SIZE)
526 {
527 RTHCPHYS HCPhys;
528 rc = PGMPhysGCPhys2HCPhys(pVM, GCPhys + off, &HCPhys);
529 AssertRC(rc);
530 if (RT_FAILURE(rc))
531 {
532 AssertMsgFailed(("rc=%Rrc GCPhys=%RGp off=%#x %s\n", rc, GCPhys, off, pszDesc));
533 break;
534 }
535 if (pVM->mm.s.fPGMInitialized)
536 {
537 rc = PGMMap(pVM, GCPtr + off, HCPhys, PAGE_SIZE, 0);
538 AssertRC(rc);
539 if (RT_FAILURE(rc))
540 {
541 AssertMsgFailed(("rc=%Rrc GCPhys=%RGp off=%#x %s\n", rc, GCPhys, off, pszDesc));
542 break;
543 }
544 }
545 }
546
547 if (RT_SUCCESS(rc) && pGCPtr)
548 *pGCPtr = GCPtr;
549 }
550 return rc;
551}
552
553
554/**
555 * Maps a portion of an MMIO2 region into the hypervisor region.
556 *
557 * Callers of this API must never deregister the MMIO2 region before the
558 * VM is powered off. If this becomes a requirement MMR3HyperUnmapMMIO2
559 * API will be needed to perform cleanups.
560 *
561 * @return VBox status code.
562 *
563 * @param pVM The cross context VM structure.
564 * @param pDevIns The device owning the MMIO2 memory.
565 * @param iSubDev The sub-device number.
566 * @param iRegion The region.
567 * @param off The offset into the region. Will be rounded down to closest page boundary.
568 * @param cb The number of bytes to map. Will be rounded up to the closest page boundary.
569 * @param pszDesc Mapping description.
570 * @param pRCPtr Where to store the RC address.
571 */
572VMMR3DECL(int) MMR3HyperMapMMIO2(PVM pVM, PPDMDEVINS pDevIns, uint32_t iSubDev, uint32_t iRegion, RTGCPHYS off, RTGCPHYS cb,
573 const char *pszDesc, PRTRCPTR pRCPtr)
574{
575 LogFlow(("MMR3HyperMapMMIO2: pDevIns=%p iSubDev=%#x iRegion=%#x off=%RGp cb=%RGp pszDesc=%p:{%s} pRCPtr=%p\n",
576 pDevIns, iSubDev, iRegion, off, cb, pszDesc, pszDesc, pRCPtr));
577 int rc;
578
579 /*
580 * Validate input.
581 */
582 AssertReturn(pszDesc && *pszDesc, VERR_INVALID_PARAMETER);
583 AssertReturn(off + cb > off, VERR_INVALID_PARAMETER);
584 uint32_t const offPage = off & PAGE_OFFSET_MASK;
585 off &= ~(RTGCPHYS)PAGE_OFFSET_MASK;
586 cb += offPage;
587 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
588 const RTGCPHYS offEnd = off + cb;
589 AssertReturn(offEnd > off, VERR_INVALID_PARAMETER);
590 for (RTGCPHYS offCur = off; offCur < offEnd; offCur += PAGE_SIZE)
591 {
592 RTHCPHYS HCPhys;
593 rc = PGMR3PhysMMIO2GetHCPhys(pVM, pDevIns, iSubDev, iRegion, offCur, &HCPhys);
594 AssertMsgRCReturn(rc, ("rc=%Rrc - iSubDev=%#x iRegion=%#x off=%RGp\n", rc, iSubDev, iRegion, off), rc);
595 }
596
597 /*
598 * Add the memory to the hypervisor area.
599 */
600 RTGCPTR GCPtr;
601 PMMLOOKUPHYPER pLookup;
602 rc = mmR3HyperMap(pVM, cb, pszDesc, &GCPtr, &pLookup);
603 if (RT_SUCCESS(rc))
604 {
605 pLookup->enmType = MMLOOKUPHYPERTYPE_MMIO2;
606 pLookup->u.MMIO2.pDevIns = pDevIns;
607 pLookup->u.MMIO2.iSubDev = iSubDev;
608 pLookup->u.MMIO2.iRegion = iRegion;
609 pLookup->u.MMIO2.off = off;
610
611 /*
612 * Update the page table.
613 */
614 if (pVM->mm.s.fPGMInitialized)
615 {
616 for (RTGCPHYS offCur = off; offCur < offEnd; offCur += PAGE_SIZE)
617 {
618 RTHCPHYS HCPhys;
619 rc = PGMR3PhysMMIO2GetHCPhys(pVM, pDevIns, iSubDev, iRegion, offCur, &HCPhys);
620 AssertRCReturn(rc, rc);
621 rc = PGMMap(pVM, GCPtr + (offCur - off), HCPhys, PAGE_SIZE, 0);
622 if (RT_FAILURE(rc))
623 {
624 AssertMsgFailed(("rc=%Rrc offCur=%RGp %s\n", rc, offCur, pszDesc));
625 break;
626 }
627 }
628 }
629
630 if (RT_SUCCESS(rc))
631 {
632 GCPtr |= offPage;
633 *pRCPtr = GCPtr;
634 AssertLogRelReturn(*pRCPtr == GCPtr, VERR_INTERNAL_ERROR);
635 }
636 }
637 return rc;
638}
639
640#endif /* !PGM_WITHOUT_MAPPINGS */
641
642/**
643 * Maps locked R3 virtual memory into the hypervisor region in the GC.
644 *
645 * @return VBox status code.
646 *
647 * @param pVM The cross context VM structure.
648 * @param pvR3 The ring-3 address of the memory, must be page aligned.
649 * @param pvR0 The ring-0 address of the memory, must be page aligned. (optional)
650 * @param cPages The number of pages.
651 * @param paPages The page descriptors.
652 * @param pszDesc Mapping description.
653 * @param pGCPtr Where to store the GC address corresponding to pvR3.
654 */
655VMMR3DECL(int) MMR3HyperMapPages(PVM pVM, void *pvR3, RTR0PTR pvR0, size_t cPages, PCSUPPAGE paPages,
656 const char *pszDesc, PRTGCPTR pGCPtr)
657{
658 LogFlow(("MMR3HyperMapPages: pvR3=%p pvR0=%p cPages=%zu paPages=%p pszDesc=%p:{%s} pGCPtr=%p\n",
659 pvR3, pvR0, cPages, paPages, pszDesc, pszDesc, pGCPtr));
660
661 /*
662 * Validate input.
663 */
664 AssertPtrReturn(pvR3, VERR_INVALID_POINTER);
665 AssertPtrReturn(paPages, VERR_INVALID_POINTER);
666 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
667 AssertReturn(cPages <= VBOX_MAX_ALLOC_PAGE_COUNT, VERR_PAGE_COUNT_OUT_OF_RANGE);
668 AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
669 AssertReturn(*pszDesc, VERR_INVALID_PARAMETER);
670 AssertPtrReturn(pGCPtr, VERR_INVALID_PARAMETER);
671
672 /*
673 * Add the memory to the hypervisor area.
674 */
675 RTGCPTR GCPtr;
676 PMMLOOKUPHYPER pLookup;
677 int rc = mmR3HyperMap(pVM, cPages << PAGE_SHIFT, pszDesc, &GCPtr, &pLookup);
678 if (RT_SUCCESS(rc))
679 {
680 /*
681 * Copy the physical page addresses and tell PGM about them.
682 */
683 PRTHCPHYS paHCPhysPages = (PRTHCPHYS)MMR3HeapAlloc(pVM, MM_TAG_MM, sizeof(RTHCPHYS) * cPages);
684 if (paHCPhysPages)
685 {
686 for (size_t i = 0; i < cPages; i++)
687 {
688 AssertReleaseMsgReturn( paPages[i].Phys != 0
689 && paPages[i].Phys != NIL_RTHCPHYS
690 && !(paPages[i].Phys & PAGE_OFFSET_MASK),
691 ("i=%#zx Phys=%RHp %s\n", i, paPages[i].Phys, pszDesc),
692 VERR_INTERNAL_ERROR);
693 paHCPhysPages[i] = paPages[i].Phys;
694 }
695
696#ifndef PGM_WITHOUT_MAPPINGS
697 if (pVM->mm.s.fPGMInitialized)
698 {
699 for (size_t i = 0; i < cPages; i++)
700 {
701 rc = PGMMap(pVM, GCPtr + (i << PAGE_SHIFT), paHCPhysPages[i], PAGE_SIZE, 0);
702 AssertRCBreak(rc);
703 }
704 }
705#endif
706 if (RT_SUCCESS(rc))
707 {
708 pLookup->enmType = MMLOOKUPHYPERTYPE_LOCKED;
709 pLookup->u.Locked.pvR3 = pvR3;
710 pLookup->u.Locked.pvR0 = pvR0;
711 pLookup->u.Locked.paHCPhysPages = paHCPhysPages;
712
713 /* done. */
714 *pGCPtr = GCPtr;
715 return rc;
716 }
717 /* Don't care about failure clean, we're screwed if this fails anyway. */
718 }
719 }
720
721 return rc;
722}
723
724
725#ifndef PGM_WITHOUT_MAPPINGS
726/**
727 * Reserves a hypervisor memory area.
728 * Most frequent usage is fence pages and dynamically mappings like the guest PD and PDPT.
729 *
730 * @return VBox status code.
731 *
732 * @param pVM The cross context VM structure.
733 * @param cb Size of the memory. Will be rounded up to nearest page.
734 * @param pszDesc Mapping description.
735 * @param pGCPtr Where to store the assigned GC address. Optional.
736 */
737VMMR3DECL(int) MMR3HyperReserve(PVM pVM, unsigned cb, const char *pszDesc, PRTGCPTR pGCPtr)
738{
739 LogFlow(("MMR3HyperMapHCRam: cb=%d pszDesc=%p:{%s} pGCPtr=%p\n", (int)cb, pszDesc, pszDesc, pGCPtr));
740
741 /*
742 * Validate input.
743 */
744 if ( cb <= 0
745 || !pszDesc
746 || !*pszDesc)
747 {
748 AssertMsgFailed(("Invalid parameter\n"));
749 return VERR_INVALID_PARAMETER;
750 }
751
752 /*
753 * Add the memory to the hypervisor area.
754 */
755 RTGCPTR GCPtr;
756 PMMLOOKUPHYPER pLookup;
757 int rc = mmR3HyperMap(pVM, cb, pszDesc, &GCPtr, &pLookup);
758 if (RT_SUCCESS(rc))
759 {
760 pLookup->enmType = MMLOOKUPHYPERTYPE_DYNAMIC;
761 if (pGCPtr)
762 *pGCPtr = GCPtr;
763 return VINF_SUCCESS;
764 }
765 return rc;
766}
767#endif /* !PGM_WITHOUT_MAPPINGS */
768
769
770/**
771 * Reserves an electric fence page.
772 *
773 * @returns VBox status code.
774 * @param pVM The cross context VM structure.
775 */
776VMMR3DECL(int) MMR3HyperReserveFence(PVM pVM)
777{
778#ifndef PGM_WITHOUT_MAPPINGS
779 return MMR3HyperReserve(pVM, cb, "fence", NULL);
780#else
781 RT_NOREF(pVM);
782 return VINF_SUCCESS;
783#endif
784}
785
786
787/**
788 * Adds memory to the hypervisor memory arena.
789 *
790 * @return VBox status code.
791 * @param pVM The cross context VM structure.
792 * @param cb Size of the memory. Will be rounded up to nearest page.
793 * @param pszDesc The description of the memory.
794 * @param pGCPtr Where to store the GC address.
795 * @param ppLookup Where to store the pointer to the lookup record.
796 * @remark We assume the threading structure of VBox imposes natural
797 * serialization of most functions, this one included.
798 */
799static int mmR3HyperMap(PVM pVM, const size_t cb, const char *pszDesc, PRTGCPTR pGCPtr, PMMLOOKUPHYPER *ppLookup)
800{
801 /*
802 * Validate input.
803 */
804 const uint32_t cbAligned = RT_ALIGN_32(cb, PAGE_SIZE);
805 AssertReturn(cbAligned >= cb, VERR_INVALID_PARAMETER);
806 if (pVM->mm.s.offHyperNextStatic + cbAligned >= pVM->mm.s.cbHyperArea) /* don't use the last page, it's a fence. */
807 {
808 AssertMsgFailed(("Out of static mapping space in the HMA! offHyperAreaGC=%x cbAligned=%x cbHyperArea=%x\n",
809 pVM->mm.s.offHyperNextStatic, cbAligned, pVM->mm.s.cbHyperArea));
810 return VERR_NO_MEMORY;
811 }
812
813 /*
814 * Allocate lookup record.
815 */
816 PMMLOOKUPHYPER pLookup;
817 int rc = MMHyperAlloc(pVM, sizeof(*pLookup), 1, MM_TAG_MM, (void **)&pLookup);
818 if (RT_SUCCESS(rc))
819 {
820 /*
821 * Initialize it and insert it.
822 */
823 pLookup->offNext = pVM->mm.s.offLookupHyper;
824 pLookup->cb = cbAligned;
825 pLookup->off = pVM->mm.s.offHyperNextStatic;
826 pVM->mm.s.offLookupHyper = (uint8_t *)pLookup - (uint8_t *)pVM->mm.s.pHyperHeapR3;
827 if (pLookup->offNext != (int32_t)NIL_OFFSET)
828 pLookup->offNext -= pVM->mm.s.offLookupHyper;
829 pLookup->enmType = MMLOOKUPHYPERTYPE_INVALID;
830 memset(&pLookup->u, 0xff, sizeof(pLookup->u));
831 pLookup->pszDesc = pszDesc;
832
833 /* Mapping. */
834 *pGCPtr = pVM->mm.s.pvHyperAreaGC + pVM->mm.s.offHyperNextStatic;
835 pVM->mm.s.offHyperNextStatic += cbAligned;
836
837 /* Return pointer. */
838 *ppLookup = pLookup;
839 }
840
841 AssertRC(rc);
842 LogFlow(("mmR3HyperMap: returns %Rrc *pGCPtr=%RGv\n", rc, *pGCPtr));
843 return rc;
844}
845
846
847/**
848 * Allocates a new heap.
849 *
850 * @returns VBox status code.
851 * @param pVM The cross context VM structure.
852 * @param cb The size of the new heap.
853 * @param ppHeap Where to store the heap pointer on successful return.
854 * @param pR0PtrHeap Where to store the ring-0 address of the heap on
855 * success.
856 */
857static int mmR3HyperHeapCreate(PVM pVM, const size_t cb, PMMHYPERHEAP *ppHeap, PRTR0PTR pR0PtrHeap)
858{
859 /*
860 * Allocate the hypervisor heap.
861 */
862 const uint32_t cbAligned = RT_ALIGN_32(cb, PAGE_SIZE);
863 AssertReturn(cbAligned >= cb, VERR_INVALID_PARAMETER);
864 uint32_t const cPages = cbAligned >> PAGE_SHIFT;
865 PSUPPAGE paPages = (PSUPPAGE)MMR3HeapAlloc(pVM, MM_TAG_MM, cPages * sizeof(paPages[0]));
866 if (!paPages)
867 return VERR_NO_MEMORY;
868 void *pv;
869 RTR0PTR pvR0 = NIL_RTR0PTR;
870 int rc = SUPR3PageAllocEx(cPages,
871 0 /*fFlags*/,
872 &pv,
873 &pvR0,
874 paPages);
875 if (RT_SUCCESS(rc))
876 {
877 Assert(pvR0 != NIL_RTR0PTR && !(PAGE_OFFSET_MASK & pvR0));
878 memset(pv, 0, cbAligned);
879
880 /*
881 * Initialize the heap and first free chunk.
882 */
883 PMMHYPERHEAP pHeap = (PMMHYPERHEAP)pv;
884 pHeap->u32Magic = MMHYPERHEAP_MAGIC;
885 pHeap->pbHeapR3 = (uint8_t *)pHeap + MMYPERHEAP_HDR_SIZE;
886 pHeap->pbHeapR0 = pvR0 + MMYPERHEAP_HDR_SIZE;
887 //pHeap->pbHeapRC = 0; // set by mmR3HyperHeapMap()
888 pHeap->pVMR3 = pVM;
889 pHeap->pVMR0 = pVM->pVMR0ForCall;
890 pHeap->pVMRC = pVM->pVMRC;
891 pHeap->cbHeap = cbAligned - MMYPERHEAP_HDR_SIZE;
892 pHeap->cbFree = pHeap->cbHeap - sizeof(MMHYPERCHUNK);
893 //pHeap->offFreeHead = 0;
894 //pHeap->offFreeTail = 0;
895 pHeap->offPageAligned = pHeap->cbHeap;
896 //pHeap->HyperHeapStatTree = 0;
897 pHeap->paPages = paPages;
898
899 PMMHYPERCHUNKFREE pFree = (PMMHYPERCHUNKFREE)pHeap->pbHeapR3;
900 pFree->cb = pHeap->cbFree;
901 //pFree->core.offNext = 0;
902 MMHYPERCHUNK_SET_TYPE(&pFree->core, MMHYPERCHUNK_FLAGS_FREE);
903 pFree->core.offHeap = -(int32_t)MMYPERHEAP_HDR_SIZE;
904 //pFree->offNext = 0;
905 //pFree->offPrev = 0;
906
907 STAMR3Register(pVM, &pHeap->cbHeap, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, "/MM/HyperHeap/cbHeap", STAMUNIT_BYTES, "The heap size.");
908 STAMR3Register(pVM, &pHeap->cbFree, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, "/MM/HyperHeap/cbFree", STAMUNIT_BYTES, "The free space.");
909
910 *ppHeap = pHeap;
911 *pR0PtrHeap = pvR0;
912 return VINF_SUCCESS;
913 }
914 AssertMsgFailed(("SUPR3PageAllocEx(%d,,,,) -> %Rrc\n", cbAligned >> PAGE_SHIFT, rc));
915
916 *ppHeap = NULL;
917 return rc;
918}
919
920
921/**
922 * Allocates a new heap.
923 */
924static int mmR3HyperHeapMap(PVM pVM, PMMHYPERHEAP pHeap, PRTGCPTR ppHeapGC)
925{
926 Assert(RT_ALIGN_Z(pHeap->cbHeap + MMYPERHEAP_HDR_SIZE, PAGE_SIZE) == pHeap->cbHeap + MMYPERHEAP_HDR_SIZE);
927 Assert(pHeap->pbHeapR0);
928 Assert(pHeap->paPages);
929 int rc = MMR3HyperMapPages(pVM,
930 pHeap,
931 pHeap->pbHeapR0 - MMYPERHEAP_HDR_SIZE,
932 (pHeap->cbHeap + MMYPERHEAP_HDR_SIZE) >> PAGE_SHIFT,
933 pHeap->paPages,
934 "Heap", ppHeapGC);
935 if (RT_SUCCESS(rc))
936 {
937 pHeap->pVMRC = pVM->pVMRC;
938 pHeap->pbHeapRC = *ppHeapGC + MMYPERHEAP_HDR_SIZE;
939 /* Reserve a page for fencing. */
940 MMR3HyperReserveFence(pVM);
941
942 /* We won't need these any more. */
943 MMR3HeapFree(pHeap->paPages);
944 pHeap->paPages = NULL;
945 }
946 return rc;
947}
948
949
950/**
951 * Allocates memory in the Hypervisor (GC VMM) area which never will
952 * be freed and doesn't have any offset based relation to other heap blocks.
953 *
954 * The latter means that two blocks allocated by this API will not have the
955 * same relative position to each other in GC and HC. In short, never use
956 * this API for allocating nodes for an offset based AVL tree!
957 *
958 * The returned memory is of course zeroed.
959 *
960 * @returns VBox status code.
961 * @param pVM The cross context VM structure.
962 * @param cb Number of bytes to allocate.
963 * @param uAlignment Required memory alignment in bytes.
964 * Values are 0,8,16,32 and PAGE_SIZE.
965 * 0 -> default alignment, i.e. 8 bytes.
966 * @param enmTag The statistics tag.
967 * @param ppv Where to store the address to the allocated
968 * memory.
969 * @remark This is assumed not to be used at times when serialization is required.
970 */
971VMMR3DECL(int) MMR3HyperAllocOnceNoRel(PVM pVM, size_t cb, unsigned uAlignment, MMTAG enmTag, void **ppv)
972{
973 return MMR3HyperAllocOnceNoRelEx(pVM, cb, uAlignment, enmTag, 0/*fFlags*/, ppv);
974}
975
976
977/**
978 * Allocates memory in the Hypervisor (GC VMM) area which never will
979 * be freed and doesn't have any offset based relation to other heap blocks.
980 *
981 * The latter means that two blocks allocated by this API will not have the
982 * same relative position to each other in GC and HC. In short, never use
983 * this API for allocating nodes for an offset based AVL tree!
984 *
985 * The returned memory is of course zeroed.
986 *
987 * @returns VBox status code.
988 * @param pVM The cross context VM structure.
989 * @param cb Number of bytes to allocate.
990 * @param uAlignment Required memory alignment in bytes.
991 * Values are 0,8,16,32 and PAGE_SIZE.
992 * 0 -> default alignment, i.e. 8 bytes.
993 * @param enmTag The statistics tag.
994 * @param fFlags Flags, see MMHYPER_AONR_FLAGS_KERNEL_MAPPING.
995 * @param ppv Where to store the address to the allocated memory.
996 * @remark This is assumed not to be used at times when serialization is required.
997 */
998VMMR3DECL(int) MMR3HyperAllocOnceNoRelEx(PVM pVM, size_t cb, unsigned uAlignment, MMTAG enmTag, uint32_t fFlags, void **ppv)
999{
1000 AssertMsg(cb >= 8, ("Hey! Do you really mean to allocate less than 8 bytes?! cb=%d\n", cb));
1001 Assert(!(fFlags & ~(MMHYPER_AONR_FLAGS_KERNEL_MAPPING)));
1002
1003 /*
1004 * Choose between allocating a new chunk of HMA memory
1005 * and the heap. We will only do BIG allocations from HMA and
1006 * only at creation time.
1007 */
1008 if ( ( cb < _64K
1009 && ( uAlignment != PAGE_SIZE
1010 || cb < 48*_1K)
1011 && !(fFlags & MMHYPER_AONR_FLAGS_KERNEL_MAPPING)
1012 )
1013 || VMR3GetState(pVM) != VMSTATE_CREATING
1014 )
1015 {
1016 Assert(!(fFlags & MMHYPER_AONR_FLAGS_KERNEL_MAPPING));
1017 int rc = MMHyperAlloc(pVM, cb, uAlignment, enmTag, ppv);
1018 if ( rc != VERR_MM_HYPER_NO_MEMORY
1019 || cb <= 8*_1K)
1020 {
1021 Log2(("MMR3HyperAllocOnceNoRel: cb=%#zx uAlignment=%#x returns %Rrc and *ppv=%p\n",
1022 cb, uAlignment, rc, *ppv));
1023 return rc;
1024 }
1025 }
1026
1027#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1028 /*
1029 * Set MMHYPER_AONR_FLAGS_KERNEL_MAPPING if we're in going to execute in ring-0.
1030 */
1031 if (VM_IS_HM_OR_NEM_ENABLED(pVM))
1032 fFlags |= MMHYPER_AONR_FLAGS_KERNEL_MAPPING;
1033#endif
1034
1035 /*
1036 * Validate alignment.
1037 */
1038 switch (uAlignment)
1039 {
1040 case 0:
1041 case 8:
1042 case 16:
1043 case 32:
1044 case PAGE_SIZE:
1045 break;
1046 default:
1047 AssertMsgFailed(("Invalid alignment %u\n", uAlignment));
1048 return VERR_INVALID_PARAMETER;
1049 }
1050
1051 /*
1052 * Allocate the pages and map them into HMA space.
1053 */
1054 uint32_t const cbAligned = RT_ALIGN_32(cb, PAGE_SIZE);
1055 AssertReturn(cbAligned >= cb, VERR_INVALID_PARAMETER);
1056 uint32_t const cPages = cbAligned >> PAGE_SHIFT;
1057 PSUPPAGE paPages = (PSUPPAGE)RTMemTmpAlloc(cPages * sizeof(paPages[0]));
1058 if (!paPages)
1059 return VERR_NO_TMP_MEMORY;
1060 void *pvPages;
1061 RTR0PTR pvR0 = NIL_RTR0PTR;
1062 int rc = SUPR3PageAllocEx(cPages,
1063 0 /*fFlags*/,
1064 &pvPages,
1065 &pvR0,
1066 paPages);
1067 if (RT_SUCCESS(rc))
1068 {
1069 Assert(pvR0 != NIL_RTR0PTR);
1070 memset(pvPages, 0, cbAligned);
1071
1072 RTGCPTR GCPtr;
1073 rc = MMR3HyperMapPages(pVM,
1074 pvPages,
1075 pvR0,
1076 cPages,
1077 paPages,
1078 MMR3HeapAPrintf(pVM, MM_TAG_MM, "alloc once (%s)", mmGetTagName(enmTag)),
1079 &GCPtr);
1080 /* not needed anymore */
1081 RTMemTmpFree(paPages);
1082 if (RT_SUCCESS(rc))
1083 {
1084 *ppv = pvPages;
1085 Log2(("MMR3HyperAllocOnceNoRel: cbAligned=%#x uAlignment=%#x returns VINF_SUCCESS and *ppv=%p\n",
1086 cbAligned, uAlignment, *ppv));
1087 MMR3HyperReserveFence(pVM);
1088 return rc;
1089 }
1090 AssertMsgFailed(("Failed to allocate %zd bytes! %Rrc\n", cbAligned, rc));
1091 SUPR3PageFreeEx(pvPages, cPages);
1092
1093
1094 /*
1095 * HACK ALERT! Try allocate it off the heap so that we don't freak
1096 * out during vga/vmmdev mmio2 allocation with certain ram sizes.
1097 */
1098 /** @todo make a proper fix for this so we will never end up in this kind of situation! */
1099 Log(("MMR3HyperAllocOnceNoRel: MMR3HyperMapHCRam failed with rc=%Rrc, try MMHyperAlloc(,%#x,,) instead\n", rc, cb));
1100 int rc2 = MMHyperAlloc(pVM, cb, uAlignment, enmTag, ppv);
1101 if (RT_SUCCESS(rc2))
1102 {
1103 Log2(("MMR3HyperAllocOnceNoRel: cb=%#x uAlignment=%#x returns %Rrc and *ppv=%p\n",
1104 cb, uAlignment, rc, *ppv));
1105 return rc;
1106 }
1107 }
1108 else
1109 AssertMsgFailed(("Failed to allocate %zd bytes! %Rrc\n", cbAligned, rc));
1110
1111 if (rc == VERR_NO_MEMORY)
1112 rc = VERR_MM_HYPER_NO_MEMORY;
1113 LogRel(("MMR3HyperAllocOnceNoRel: cb=%#zx uAlignment=%#x returns %Rrc\n", cb, uAlignment, rc));
1114 return rc;
1115}
1116
1117
1118/**
1119 * Lookus up a ring-3 pointer to HMA.
1120 *
1121 * @returns The lookup record on success, NULL on failure.
1122 * @param pVM The cross context VM structure.
1123 * @param pvR3 The ring-3 address to look up.
1124 */
1125DECLINLINE(PMMLOOKUPHYPER) mmR3HyperLookupR3(PVM pVM, void *pvR3)
1126{
1127 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
1128 for (;;)
1129 {
1130 switch (pLookup->enmType)
1131 {
1132 case MMLOOKUPHYPERTYPE_LOCKED:
1133 {
1134 unsigned off = (uint8_t *)pvR3 - (uint8_t *)pLookup->u.Locked.pvR3;
1135 if (off < pLookup->cb)
1136 return pLookup;
1137 break;
1138 }
1139
1140 case MMLOOKUPHYPERTYPE_HCPHYS:
1141 {
1142 unsigned off = (uint8_t *)pvR3 - (uint8_t *)pLookup->u.HCPhys.pvR3;
1143 if (off < pLookup->cb)
1144 return pLookup;
1145 break;
1146 }
1147
1148 case MMLOOKUPHYPERTYPE_GCPHYS:
1149 case MMLOOKUPHYPERTYPE_MMIO2:
1150 case MMLOOKUPHYPERTYPE_DYNAMIC:
1151 /** @todo ? */
1152 break;
1153
1154 default:
1155 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
1156 return NULL;
1157 }
1158
1159 /* next */
1160 if ((unsigned)pLookup->offNext == NIL_OFFSET)
1161 return NULL;
1162 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
1163 }
1164}
1165
1166
1167/**
1168 * Set / unset guard status on one or more hyper heap pages.
1169 *
1170 * @returns VBox status code (first failure).
1171 * @param pVM The cross context VM structure.
1172 * @param pvStart The hyper heap page address. Must be page
1173 * aligned.
1174 * @param cb The number of bytes. Must be page aligned.
1175 * @param fSet Whether to set or unset guard page status.
1176 */
1177VMMR3DECL(int) MMR3HyperSetGuard(PVM pVM, void *pvStart, size_t cb, bool fSet)
1178{
1179 /*
1180 * Validate input.
1181 */
1182 AssertReturn(!((uintptr_t)pvStart & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
1183 AssertReturn(!(cb & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
1184 AssertReturn(cb <= UINT32_MAX, VERR_INVALID_PARAMETER);
1185 PMMLOOKUPHYPER pLookup = mmR3HyperLookupR3(pVM, pvStart);
1186 AssertReturn(pLookup, VERR_INVALID_PARAMETER);
1187 AssertReturn(pLookup->enmType == MMLOOKUPHYPERTYPE_LOCKED, VERR_INVALID_PARAMETER);
1188
1189 /*
1190 * Get down to business.
1191 * Note! We quietly ignore errors from the support library since the
1192 * protection stuff isn't possible to implement on all platforms.
1193 */
1194 uint8_t *pbR3 = (uint8_t *)pLookup->u.Locked.pvR3;
1195 RTR0PTR R0Ptr = pLookup->u.Locked.pvR0 != (uintptr_t)pLookup->u.Locked.pvR3
1196 ? pLookup->u.Locked.pvR0
1197 : NIL_RTR0PTR;
1198 uint32_t off = (uint32_t)((uint8_t *)pvStart - pbR3);
1199 int rc;
1200 if (fSet)
1201 {
1202#ifndef PGM_WITHOUT_MAPPINGS
1203 rc = PGMMapSetPage(pVM, MMHyperR3ToRC(pVM, pvStart), cb, 0);
1204#else
1205 rc = VINF_SUCCESS;
1206#endif
1207 SUPR3PageProtect(pbR3, R0Ptr, off, (uint32_t)cb, RTMEM_PROT_NONE);
1208 }
1209 else
1210 {
1211#ifndef PGM_WITHOUT_MAPPINGS
1212 rc = PGMMapSetPage(pVM, MMHyperR3ToRC(pVM, pvStart), cb, X86_PTE_P | X86_PTE_A | X86_PTE_D | X86_PTE_RW);
1213#else
1214 rc = VINF_SUCCESS;
1215#endif
1216 SUPR3PageProtect(pbR3, R0Ptr, off, (uint32_t)cb, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
1217 }
1218 return rc;
1219}
1220
1221
1222/**
1223 * Convert hypervisor HC virtual address to HC physical address.
1224 *
1225 * @returns HC physical address.
1226 * @param pVM The cross context VM structure.
1227 * @param pvR3 Host context virtual address.
1228 */
1229VMMR3DECL(RTHCPHYS) MMR3HyperHCVirt2HCPhys(PVM pVM, void *pvR3)
1230{
1231 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
1232 for (;;)
1233 {
1234 switch (pLookup->enmType)
1235 {
1236 case MMLOOKUPHYPERTYPE_LOCKED:
1237 {
1238 unsigned off = (uint8_t *)pvR3 - (uint8_t *)pLookup->u.Locked.pvR3;
1239 if (off < pLookup->cb)
1240 return pLookup->u.Locked.paHCPhysPages[off >> PAGE_SHIFT] | (off & PAGE_OFFSET_MASK);
1241 break;
1242 }
1243
1244 case MMLOOKUPHYPERTYPE_HCPHYS:
1245 {
1246 unsigned off = (uint8_t *)pvR3 - (uint8_t *)pLookup->u.HCPhys.pvR3;
1247 if (off < pLookup->cb)
1248 return pLookup->u.HCPhys.HCPhys + off;
1249 break;
1250 }
1251
1252 case MMLOOKUPHYPERTYPE_GCPHYS:
1253 case MMLOOKUPHYPERTYPE_MMIO2:
1254 case MMLOOKUPHYPERTYPE_DYNAMIC:
1255 /* can (or don't want to) convert these kind of records. */
1256 break;
1257
1258 default:
1259 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
1260 break;
1261 }
1262
1263 /* next */
1264 if ((unsigned)pLookup->offNext == NIL_OFFSET)
1265 break;
1266 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
1267 }
1268
1269 AssertMsgFailed(("pvR3=%p is not inside the hypervisor memory area!\n", pvR3));
1270 return NIL_RTHCPHYS;
1271}
1272
1273#ifndef PGM_WITHOUT_MAPPINGS
1274
1275/**
1276 * Implements the hcphys-not-found return case of MMR3HyperQueryInfoFromHCPhys.
1277 *
1278 * @returns VINF_SUCCESS, VINF_BUFFER_OVERFLOW.
1279 * @param pVM The cross context VM structure.
1280 * @param HCPhys The host physical address to look for.
1281 * @param pLookup The HMA lookup entry corresponding to HCPhys.
1282 * @param pszWhat Where to return the description.
1283 * @param cbWhat Size of the return buffer.
1284 * @param pcbAlloc Where to return the size of whatever it is.
1285 */
1286static int mmR3HyperQueryInfoFromHCPhysFound(PVM pVM, RTHCPHYS HCPhys, PMMLOOKUPHYPER pLookup,
1287 char *pszWhat, size_t cbWhat, uint32_t *pcbAlloc)
1288{
1289 NOREF(pVM); NOREF(HCPhys);
1290 *pcbAlloc = pLookup->cb;
1291 int rc = RTStrCopy(pszWhat, cbWhat, pLookup->pszDesc);
1292 return rc == VERR_BUFFER_OVERFLOW ? VINF_BUFFER_OVERFLOW : rc;
1293}
1294
1295
1296/**
1297 * Scans the HMA for the physical page and reports back a description if found.
1298 *
1299 * @returns VINF_SUCCESS, VINF_BUFFER_OVERFLOW, VERR_NOT_FOUND.
1300 * @param pVM The cross context VM structure.
1301 * @param HCPhys The host physical address to look for.
1302 * @param pszWhat Where to return the description.
1303 * @param cbWhat Size of the return buffer.
1304 * @param pcbAlloc Where to return the size of whatever it is.
1305 */
1306VMMR3_INT_DECL(int) MMR3HyperQueryInfoFromHCPhys(PVM pVM, RTHCPHYS HCPhys, char *pszWhat, size_t cbWhat, uint32_t *pcbAlloc)
1307{
1308 RTHCPHYS HCPhysPage = HCPhys & ~(RTHCPHYS)PAGE_OFFSET_MASK;
1309 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
1310 for (;;)
1311 {
1312 switch (pLookup->enmType)
1313 {
1314 case MMLOOKUPHYPERTYPE_LOCKED:
1315 {
1316 uint32_t i = pLookup->cb >> PAGE_SHIFT;
1317 while (i-- > 0)
1318 if (pLookup->u.Locked.paHCPhysPages[i] == HCPhysPage)
1319 return mmR3HyperQueryInfoFromHCPhysFound(pVM, HCPhys, pLookup, pszWhat, cbWhat, pcbAlloc);
1320 break;
1321 }
1322
1323 case MMLOOKUPHYPERTYPE_HCPHYS:
1324 {
1325 if (pLookup->u.HCPhys.HCPhys - HCPhysPage < pLookup->cb)
1326 return mmR3HyperQueryInfoFromHCPhysFound(pVM, HCPhys, pLookup, pszWhat, cbWhat, pcbAlloc);
1327 break;
1328 }
1329
1330 case MMLOOKUPHYPERTYPE_MMIO2:
1331 case MMLOOKUPHYPERTYPE_GCPHYS:
1332 case MMLOOKUPHYPERTYPE_DYNAMIC:
1333 {
1334 /* brute force. */
1335 uint32_t i = pLookup->cb >> PAGE_SHIFT;
1336 while (i-- > 0)
1337 {
1338 RTGCPTR GCPtr = pLookup->off + pVM->mm.s.pvHyperAreaGC;
1339 RTHCPHYS HCPhysCur;
1340 int rc = PGMMapGetPage(pVM, GCPtr, NULL, &HCPhysCur);
1341 if (RT_SUCCESS(rc) && HCPhysCur == HCPhysPage)
1342 return mmR3HyperQueryInfoFromHCPhysFound(pVM, HCPhys, pLookup, pszWhat, cbWhat, pcbAlloc);
1343 }
1344 break;
1345 }
1346 default:
1347 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
1348 break;
1349 }
1350
1351 /* next */
1352 if ((unsigned)pLookup->offNext == NIL_OFFSET)
1353 break;
1354 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
1355 }
1356 return VERR_NOT_FOUND;
1357}
1358
1359
1360/**
1361 * Read hypervisor memory from GC virtual address.
1362 *
1363 * @returns VBox status code.
1364 * @param pVM The cross context VM structure.
1365 * @param pvDst Destination address (HC of course).
1366 * @param GCPtr GC virtual address.
1367 * @param cb Number of bytes to read.
1368 *
1369 * @remarks For DBGF only.
1370 */
1371VMMR3DECL(int) MMR3HyperReadGCVirt(PVM pVM, void *pvDst, RTGCPTR GCPtr, size_t cb)
1372{
1373 if (GCPtr - pVM->mm.s.pvHyperAreaGC >= pVM->mm.s.cbHyperArea)
1374 return VERR_INVALID_POINTER;
1375 return PGMR3MapRead(pVM, pvDst, GCPtr, cb);
1376}
1377
1378#endif /* !PGM_WITHOUT_MAPPINGS */
1379
1380/**
1381 * Info handler for 'hma', it dumps the list of lookup records for the hypervisor memory area.
1382 *
1383 * @param pVM The cross context VM structure.
1384 * @param pHlp Callback functions for doing output.
1385 * @param pszArgs Argument string. Optional and specific to the handler.
1386 */
1387static DECLCALLBACK(void) mmR3HyperInfoHma(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
1388{
1389 NOREF(pszArgs);
1390
1391 pHlp->pfnPrintf(pHlp, "Hypervisor Memory Area (HMA) Layout: Base %RGv, 0x%08x bytes\n",
1392 pVM->mm.s.pvHyperAreaGC, pVM->mm.s.cbHyperArea);
1393
1394 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
1395 for (;;)
1396 {
1397 switch (pLookup->enmType)
1398 {
1399 case MMLOOKUPHYPERTYPE_LOCKED:
1400 pHlp->pfnPrintf(pHlp, "%RGv-%RGv %RHv %RHv LOCKED %-*s %s\n",
1401 pLookup->off + pVM->mm.s.pvHyperAreaGC,
1402 pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
1403 pLookup->u.Locked.pvR3,
1404 pLookup->u.Locked.pvR0,
1405 sizeof(RTHCPTR) * 2, "",
1406 pLookup->pszDesc);
1407 break;
1408
1409 case MMLOOKUPHYPERTYPE_HCPHYS:
1410 pHlp->pfnPrintf(pHlp, "%RGv-%RGv %RHv %RHv HCPHYS %RHp %s\n",
1411 pLookup->off + pVM->mm.s.pvHyperAreaGC,
1412 pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
1413 pLookup->u.HCPhys.pvR3,
1414 pLookup->u.HCPhys.pvR0,
1415 pLookup->u.HCPhys.HCPhys,
1416 pLookup->pszDesc);
1417 break;
1418
1419 case MMLOOKUPHYPERTYPE_GCPHYS:
1420 pHlp->pfnPrintf(pHlp, "%RGv-%RGv %*s GCPHYS %RGp%*s %s\n",
1421 pLookup->off + pVM->mm.s.pvHyperAreaGC,
1422 pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
1423 sizeof(RTHCPTR) * 2 * 2 + 1, "",
1424 pLookup->u.GCPhys.GCPhys, RT_ABS((int)(sizeof(RTHCPHYS) - sizeof(RTGCPHYS))) * 2, "",
1425 pLookup->pszDesc);
1426 break;
1427
1428 case MMLOOKUPHYPERTYPE_MMIO2:
1429 pHlp->pfnPrintf(pHlp, "%RGv-%RGv %*s MMIO2 %RGp%*s %s\n",
1430 pLookup->off + pVM->mm.s.pvHyperAreaGC,
1431 pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
1432 sizeof(RTHCPTR) * 2 * 2 + 1, "",
1433 pLookup->u.MMIO2.off, RT_ABS((int)(sizeof(RTHCPHYS) - sizeof(RTGCPHYS))) * 2, "",
1434 pLookup->pszDesc);
1435 break;
1436
1437 case MMLOOKUPHYPERTYPE_DYNAMIC:
1438 pHlp->pfnPrintf(pHlp, "%RGv-%RGv %*s DYNAMIC %*s %s\n",
1439 pLookup->off + pVM->mm.s.pvHyperAreaGC,
1440 pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
1441 sizeof(RTHCPTR) * 2 * 2 + 1, "",
1442 sizeof(RTHCPTR) * 2, "",
1443 pLookup->pszDesc);
1444 break;
1445
1446 default:
1447 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
1448 break;
1449 }
1450
1451 /* next */
1452 if ((unsigned)pLookup->offNext == NIL_OFFSET)
1453 break;
1454 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
1455 }
1456}
1457
1458
1459/**
1460 * Re-allocates memory from the hyper heap.
1461 *
1462 * @returns VBox status code.
1463 * @param pVM The cross context VM structure.
1464 * @param pvOld The existing block of memory in the hyper heap to
1465 * re-allocate (can be NULL).
1466 * @param cbOld Size of the existing block.
1467 * @param uAlignmentNew Required memory alignment in bytes. Values are
1468 * 0,8,16,32 and PAGE_SIZE. 0 -> default alignment,
1469 * i.e. 8 bytes.
1470 * @param enmTagNew The statistics tag.
1471 * @param cbNew The required size of the new block.
1472 * @param ppv Where to store the address to the re-allocated
1473 * block.
1474 *
1475 * @remarks This does not work like normal realloc() on failure, the memory
1476 * pointed to by @a pvOld is lost if there isn't sufficient space on
1477 * the hyper heap for the re-allocation to succeed.
1478*/
1479VMMR3DECL(int) MMR3HyperRealloc(PVM pVM, void *pvOld, size_t cbOld, unsigned uAlignmentNew, MMTAG enmTagNew, size_t cbNew,
1480 void **ppv)
1481{
1482 if (!pvOld)
1483 return MMHyperAlloc(pVM, cbNew, uAlignmentNew, enmTagNew, ppv);
1484
1485 if (!cbNew && pvOld)
1486 return MMHyperFree(pVM, pvOld);
1487
1488 if (cbOld == cbNew)
1489 return VINF_SUCCESS;
1490
1491 size_t cbData = RT_MIN(cbNew, cbOld);
1492 void *pvTmp = RTMemTmpAlloc(cbData);
1493 if (RT_UNLIKELY(!pvTmp))
1494 {
1495 MMHyperFree(pVM, pvOld);
1496 return VERR_NO_TMP_MEMORY;
1497 }
1498 memcpy(pvTmp, pvOld, cbData);
1499
1500 int rc = MMHyperFree(pVM, pvOld);
1501 if (RT_SUCCESS(rc))
1502 {
1503 rc = MMHyperAlloc(pVM, cbNew, uAlignmentNew, enmTagNew, ppv);
1504 if (RT_SUCCESS(rc))
1505 {
1506 Assert(cbData <= cbNew);
1507 memcpy(*ppv, pvTmp, cbData);
1508 }
1509 }
1510 else
1511 AssertMsgFailed(("Failed to free hyper heap block pvOld=%p cbOld=%u\n", pvOld, cbOld));
1512
1513 RTMemTmpFree(pvTmp);
1514 return rc;
1515}
1516
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