VirtualBox

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

Last change on this file since 74795 was 73097, checked in by vboxsync, 6 years ago

*: Made RT_UOFFSETOF, RT_OFFSETOF, RT_UOFFSETOF_ADD and RT_OFFSETOF_ADD work like builtin_offsetof() and require compile time resolvable requests, adding RT_UOFFSETOF_DYN for the dynamic questions that can only be answered at runtime.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use