[23] | 1 | /* $Id: MMHyper.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
[12967] | 3 | * MM - Memory Manager - Hypervisor Memory Area.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[82968] | 7 | * Copyright (C) 2006-2020 Oracle Corporation
|
---|
[1] | 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
|
---|
[5999] | 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.
|
---|
[1] | 16 | */
|
---|
| 17 |
|
---|
| 18 |
|
---|
[57358] | 19 | /*********************************************************************************************************************************
|
---|
| 20 | * Header Files *
|
---|
| 21 | *********************************************************************************************************************************/
|
---|
[1] | 22 | #define LOG_GROUP LOG_GROUP_MM_HYPER
|
---|
[35346] | 23 | #include <VBox/vmm/pgm.h>
|
---|
| 24 | #include <VBox/vmm/mm.h>
|
---|
[45618] | 25 | #include <VBox/vmm/hm.h>
|
---|
[35346] | 26 | #include <VBox/vmm/dbgf.h>
|
---|
[1] | 27 | #include "MMInternal.h"
|
---|
[35346] | 28 | #include <VBox/vmm/vm.h>
|
---|
[80281] | 29 | #include <VBox/vmm/gvm.h>
|
---|
[1] | 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 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Internal Functions *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[80118] | 41 | #ifndef PGM_WITHOUT_MAPPINGS
|
---|
[50112] | 42 | static DECLCALLBACK(bool) mmR3HyperRelocateCallback(PVM pVM, RTGCPTR GCPtrOld, RTGCPTR GCPtrNew, PGMRELOCATECALL enmMode,
|
---|
| 43 | void *pvUser);
|
---|
[80118] | 44 | #endif
|
---|
[1] | 45 | static int mmR3HyperMap(PVM pVM, const size_t cb, const char *pszDesc, PRTGCPTR pGCPtr, PMMLOOKUPHYPER *ppLookup);
|
---|
[14601] | 46 | static int mmR3HyperHeapCreate(PVM pVM, const size_t cb, PMMHYPERHEAP *ppHeap, PRTR0PTR pR0PtrHeap);
|
---|
[1] | 47 | static int mmR3HyperHeapMap(PVM pVM, PMMHYPERHEAP pHeap, PRTGCPTR ppHeapGC);
|
---|
| 48 | static DECLCALLBACK(void) mmR3HyperInfoHma(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
|
---|
| 49 |
|
---|
| 50 |
|
---|
[36960] | 51 | /**
|
---|
| 52 | * Determin the default heap size.
|
---|
| 53 | *
|
---|
| 54 | * @returns The heap size in bytes.
|
---|
[58122] | 55 | * @param pVM The cross context VM structure.
|
---|
[36960] | 56 | */
|
---|
[50111] | 57 | static uint32_t mmR3HyperComputeHeapSize(PVM pVM)
|
---|
[32910] | 58 | {
|
---|
[80118] | 59 | /** @todo Redo after moving allocations off the hyper heap. */
|
---|
| 60 |
|
---|
[36960] | 61 | /*
|
---|
| 62 | * Gather parameters.
|
---|
| 63 | */
|
---|
[80118] | 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);
|
---|
[36960] | 68 |
|
---|
| 69 | uint64_t cbRam;
|
---|
[80118] | 70 | int rc = CFGMR3QueryU64(CFGMR3GetRoot(pVM), "RamSize", &cbRam);
|
---|
[36960] | 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
|
---|
[70948] | 78 | && VM_IS_RAW_MODE_ENABLED(pVM)
|
---|
[36960] | 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. */
|
---|
[33998] | 89 | if (fCanUseLargerHeap)
|
---|
[36960] | 90 | cbHeap += _1M;
|
---|
[33998] | 91 |
|
---|
[36960] | 92 | /* More CPUs means some extra memory usage. */
|
---|
[33192] | 93 | if (pVM->cCpus > 1)
|
---|
[36960] | 94 | cbHeap += pVM->cCpus * _64K;
|
---|
[33192] | 95 |
|
---|
[36960] | 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 */
|
---|
[33193] | 99 |
|
---|
[36960] | 100 | return RT_ALIGN(cbHeap, _256K);
|
---|
[32910] | 101 | }
|
---|
[12968] | 102 |
|
---|
[32910] | 103 |
|
---|
[1] | 104 | /**
|
---|
[33540] | 105 | * Initializes the hypervisor related MM stuff without
|
---|
[1] | 106 | * calling down to PGM.
|
---|
| 107 | *
|
---|
| 108 | * PGM is not initialized at this point, PGM relies on
|
---|
| 109 | * the heap to initialize.
|
---|
| 110 | *
|
---|
[58170] | 111 | * @returns VBox status code.
|
---|
[1] | 112 | */
|
---|
[6529] | 113 | int mmR3HyperInit(PVM pVM)
|
---|
[1] | 114 | {
|
---|
[6529] | 115 | LogFlow(("mmR3HyperInit:\n"));
|
---|
[1] | 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 |
|
---|
[12814] | 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
|
---|
[20531] | 128 | * precious kernel space on heap for the PATM.
|
---|
[19663] | 129 | */
|
---|
[32910] | 130 | PCFGMNODE pMM = CFGMR3GetChild(CFGMR3GetRoot(pVM), "MM");
|
---|
[36960] | 131 | uint32_t cbHyperHeap;
|
---|
[50111] | 132 | int rc = CFGMR3QueryU32Def(pMM, "cbHyperHeap", &cbHyperHeap, mmR3HyperComputeHeapSize(pVM));
|
---|
[32908] | 133 | AssertLogRelRCReturn(rc, rc);
|
---|
[32910] | 134 |
|
---|
[1] | 135 | cbHyperHeap = RT_ALIGN_32(cbHyperHeap, PAGE_SIZE);
|
---|
[18354] | 136 | LogRel(("MM: cbHyperHeap=%#x (%u)\n", cbHyperHeap, cbHyperHeap));
|
---|
[1] | 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 | */
|
---|
[14601] | 144 | rc = mmR3HyperHeapCreate(pVM, cbHyperHeap, &pVM->mm.s.pHyperHeapR3, &pVM->mm.s.pHyperHeapR0);
|
---|
[13816] | 145 | if (RT_SUCCESS(rc))
|
---|
[1] | 146 | {
|
---|
| 147 | /*
|
---|
| 148 | * Make a small head fence to fend of accidental sequential access.
|
---|
| 149 | */
|
---|
[80118] | 150 | MMR3HyperReserveFence(pVM);
|
---|
[1] | 151 |
|
---|
| 152 | /*
|
---|
| 153 | * Map the VM structure into the hypervisor space.
|
---|
[80281] | 154 | * Note! Keeping the mappings here for now in case someone is using
|
---|
| 155 | * MMHyperR3ToR0 or similar.
|
---|
[1] | 156 | */
|
---|
[80281] | 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));
|
---|
[80334] | 163 | /** @todo get rid of this */
|
---|
[80281] | 164 | RTGCPTR GCPtr;
|
---|
| 165 | rc = MMR3HyperMapPages(pVM, pVM, pVM->pVMR0ForCall, sizeof(VM) >> PAGE_SHIFT, pVM->paVMPagesR3, "VM", &GCPtr);
|
---|
[80319] | 166 | uint32_t offPages = RT_UOFFSETOF_DYN(GVM, aCpus) >> PAGE_SHIFT; /* (Using the _DYN variant avoids -Winvalid-offset) */
|
---|
[80281] | 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 | }
|
---|
[13816] | 174 | if (RT_SUCCESS(rc))
|
---|
[1] | 175 | {
|
---|
[13815] | 176 | pVM->pVMRC = (RTRCPTR)GCPtr;
|
---|
[22890] | 177 | for (VMCPUID i = 0; i < pVM->cCpus; i++)
|
---|
[80191] | 178 | pVM->apCpusR3[i]->pVMRC = pVM->pVMRC;
|
---|
[9148] | 179 |
|
---|
[1] | 180 | /* Reserve a page for fencing. */
|
---|
[80118] | 181 | MMR3HyperReserveFence(pVM);
|
---|
[1] | 182 |
|
---|
| 183 | /*
|
---|
| 184 | * Map the heap into the hypervisor space.
|
---|
| 185 | */
|
---|
[12814] | 186 | rc = mmR3HyperHeapMap(pVM, pVM->mm.s.pHyperHeapR3, &GCPtr);
|
---|
[13816] | 187 | if (RT_SUCCESS(rc))
|
---|
[1] | 188 | {
|
---|
[12814] | 189 | pVM->mm.s.pHyperHeapRC = (RTRCPTR)GCPtr;
|
---|
| 190 | Assert(pVM->mm.s.pHyperHeapRC == GCPtr);
|
---|
[9148] | 191 |
|
---|
[1] | 192 | /*
|
---|
| 193 | * Register info handlers.
|
---|
| 194 | */
|
---|
| 195 | DBGFR3InfoRegisterInternal(pVM, "hma", "Show the layout of the Hypervisor Memory Area.", mmR3HyperInfoHma);
|
---|
| 196 |
|
---|
[6529] | 197 | LogFlow(("mmR3HyperInit: returns VINF_SUCCESS\n"));
|
---|
[1] | 198 | return VINF_SUCCESS;
|
---|
| 199 | }
|
---|
| 200 | /* Caller will do proper cleanup. */
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 |
|
---|
[13818] | 204 | LogFlow(("mmR3HyperInit: returns %Rrc\n", rc));
|
---|
[1] | 205 | return rc;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 |
|
---|
| 209 | /**
|
---|
[19663] | 210 | * Cleans up the hypervisor heap.
|
---|
| 211 | *
|
---|
[58170] | 212 | * @returns VBox status code.
|
---|
[19663] | 213 | */
|
---|
| 214 | int 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 | /**
|
---|
[1] | 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 | *
|
---|
[58170] | 229 | * @returns VBox status code.
|
---|
[1] | 230 | */
|
---|
[12989] | 231 | VMMR3DECL(int) MMR3HyperInitFinalize(PVM pVM)
|
---|
[1] | 232 | {
|
---|
| 233 | LogFlow(("MMR3HyperInitFinalize:\n"));
|
---|
| 234 |
|
---|
| 235 | /*
|
---|
[19663] | 236 | * Initialize the hyper heap critical section.
|
---|
| 237 | */
|
---|
[25732] | 238 | int rc = PDMR3CritSectInit(pVM, &pVM->mm.s.pHyperHeapR3->Lock, RT_SRC_POS, "MM-HYPER");
|
---|
[19663] | 239 | AssertRC(rc);
|
---|
| 240 |
|
---|
[80118] | 241 | #ifndef PGM_WITHOUT_MAPPINGS
|
---|
[19663] | 242 | /*
|
---|
[1] | 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;
|
---|
[19663] | 247 | rc = PGMR3MapPT(pVM, pVM->mm.s.pvHyperAreaGC, pVM->mm.s.cbHyperArea, 0 /*fFlags*/,
|
---|
| 248 | mmR3HyperRelocateCallback, NULL, "Hypervisor Memory Area");
|
---|
[13816] | 249 | if (RT_FAILURE(rc))
|
---|
[1] | 250 | return rc;
|
---|
[80118] | 251 | #endif
|
---|
[1] | 252 | pVM->mm.s.fPGMInitialized = true;
|
---|
| 253 |
|
---|
[80118] | 254 | #ifndef PGM_WITHOUT_MAPPINGS
|
---|
[1] | 255 | /*
|
---|
| 256 | * Do all the delayed mappings.
|
---|
| 257 | */
|
---|
[12814] | 258 | PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uintptr_t)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
|
---|
[1] | 259 | for (;;)
|
---|
| 260 | {
|
---|
| 261 | RTGCPTR GCPtr = pVM->mm.s.pvHyperAreaGC + pLookup->off;
|
---|
[18718] | 262 | uint32_t cPages = pLookup->cb >> PAGE_SHIFT;
|
---|
[1] | 263 | switch (pLookup->enmType)
|
---|
| 264 | {
|
---|
| 265 | case MMLOOKUPHYPERTYPE_LOCKED:
|
---|
[18718] | 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 | }
|
---|
[1] | 273 | break;
|
---|
[18718] | 274 | }
|
---|
[1] | 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;
|
---|
[18718] | 283 | const uint32_t cb = pLookup->cb;
|
---|
| 284 | for (uint32_t off = 0; off < cb; off += PAGE_SIZE)
|
---|
[1] | 285 | {
|
---|
| 286 | RTHCPHYS HCPhys;
|
---|
| 287 | rc = PGMPhysGCPhys2HCPhys(pVM, GCPhys + off, &HCPhys);
|
---|
[13816] | 288 | if (RT_FAILURE(rc))
|
---|
[1] | 289 | break;
|
---|
| 290 | rc = PGMMap(pVM, GCPtr + off, HCPhys, PAGE_SIZE, 0);
|
---|
[13816] | 291 | if (RT_FAILURE(rc))
|
---|
[1] | 292 | break;
|
---|
| 293 | }
|
---|
| 294 | break;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
[7635] | 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;
|
---|
[64373] | 303 | rc = PGMR3PhysMMIO2GetHCPhys(pVM, pLookup->u.MMIO2.pDevIns, pLookup->u.MMIO2.iSubDev,
|
---|
| 304 | pLookup->u.MMIO2.iRegion, offCur, &HCPhys);
|
---|
[7635] | 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 |
|
---|
[1] | 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 |
|
---|
[13816] | 323 | if (RT_FAILURE(rc))
|
---|
[1] | 324 | {
|
---|
[13820] | 325 | AssertMsgFailed(("rc=%Rrc cb=%d off=%#RX32 enmType=%d pszDesc=%s\n",
|
---|
| 326 | rc, pLookup->cb, pLookup->off, pLookup->enmType, pLookup->pszDesc));
|
---|
[1] | 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 | }
|
---|
[80118] | 335 | #endif /* !PGM_WITHOUT_MAPPINGS */
|
---|
[1] | 336 |
|
---|
| 337 | LogFlow(("MMR3HyperInitFinalize: returns VINF_SUCCESS\n"));
|
---|
| 338 | return VINF_SUCCESS;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 |
|
---|
[80118] | 342 | #ifndef PGM_WITHOUT_MAPPINGS
|
---|
[1] | 343 | /**
|
---|
[39078] | 344 | * Callback function which will be called when PGM is trying to find a new
|
---|
| 345 | * location for the mapping.
|
---|
[1] | 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.
|
---|
[58122] | 354 | * @param pVM The cross context VM structure.
|
---|
[1] | 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 | */
|
---|
[39078] | 362 | static DECLCALLBACK(bool) mmR3HyperRelocateCallback(PVM pVM, RTGCPTR GCPtrOld, RTGCPTR GCPtrNew,
|
---|
| 363 | PGMRELOCATECALL enmMode, void *pvUser)
|
---|
[1] | 364 | {
|
---|
[39078] | 365 | NOREF(pvUser);
|
---|
[1] | 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 | */
|
---|
[50111] | 382 | AssertMsg(GCPtrOld == pVM->mm.s.pvHyperAreaGC,
|
---|
| 383 | ("GCPtrOld=%RGv pVM->mm.s.pvHyperAreaGC=%RGv\n", GCPtrOld, pVM->mm.s.pvHyperAreaGC));
|
---|
[13823] | 384 | Log(("Relocating the hypervisor from %RGv to %RGv\n", GCPtrOld, GCPtrNew));
|
---|
[1] | 385 |
|
---|
[12792] | 386 | /*
|
---|
| 387 | * Relocate the VM structure and ourselves.
|
---|
| 388 | */
|
---|
[13831] | 389 | RTGCINTPTR offDelta = GCPtrNew - GCPtrOld;
|
---|
[12794] | 390 | pVM->pVMRC += offDelta;
|
---|
[22890] | 391 | for (VMCPUID i = 0; i < pVM->cCpus; i++)
|
---|
[13830] | 392 | pVM->aCpus[i].pVMRC = pVM->pVMRC;
|
---|
[1] | 393 |
|
---|
[12792] | 394 | pVM->mm.s.pvHyperAreaGC += offDelta;
|
---|
[12814] | 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;
|
---|
[12792] | 399 |
|
---|
| 400 | /*
|
---|
| 401 | * Relocate the rest.
|
---|
| 402 | */
|
---|
[1] | 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 | }
|
---|
[80118] | 413 | #endif /* !PGM_WITHOUT_MAPPINGS */
|
---|
[1] | 414 |
|
---|
[80118] | 415 |
|
---|
[19666] | 416 | /**
|
---|
[20874] | 417 | * Service a VMMCALLRING3_MMHYPER_LOCK call.
|
---|
[19666] | 418 | *
|
---|
| 419 | * @returns VBox status code.
|
---|
[58122] | 420 | * @param pVM The cross context VM structure.
|
---|
[19666] | 421 | */
|
---|
| 422 | VMMR3DECL(int) MMR3LockCall(PVM pVM)
|
---|
| 423 | {
|
---|
| 424 | PMMHYPERHEAP pHeap = pVM->mm.s.CTX_SUFF(pHyperHeap);
|
---|
[1] | 425 |
|
---|
[19666] | 426 | int rc = PDMR3CritSectEnterEx(&pHeap->Lock, true /* fHostCall */);
|
---|
| 427 | AssertRC(rc);
|
---|
| 428 | return rc;
|
---|
| 429 | }
|
---|
| 430 |
|
---|
[80118] | 431 |
|
---|
| 432 | #ifndef PGM_WITHOUT_MAPPINGS
|
---|
| 433 |
|
---|
[1] | 434 | /**
|
---|
| 435 | * Maps contiguous HC physical memory into the hypervisor region in the GC.
|
---|
| 436 | *
|
---|
| 437 | * @return VBox status code.
|
---|
| 438 | *
|
---|
[58122] | 439 | * @param pVM The cross context VM structure.
|
---|
[14597] | 440 | * @param pvR3 Ring-3 address of the memory. Must be page aligned!
|
---|
| 441 | * @param pvR0 Optional ring-0 address of the memory.
|
---|
[12814] | 442 | * @param HCPhys Host context physical address of the memory to be
|
---|
| 443 | * mapped. Must be page aligned!
|
---|
[1] | 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 | */
|
---|
[50111] | 448 | VMMR3DECL(int) MMR3HyperMapHCPhys(PVM pVM, void *pvR3, RTR0PTR pvR0, RTHCPHYS HCPhys, size_t cb,
|
---|
| 449 | const char *pszDesc, PRTGCPTR pGCPtr)
|
---|
[1] | 450 | {
|
---|
[50111] | 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));
|
---|
[1] | 453 |
|
---|
| 454 | /*
|
---|
| 455 | * Validate input.
|
---|
| 456 | */
|
---|
[12814] | 457 | AssertReturn(RT_ALIGN_P(pvR3, PAGE_SIZE) == pvR3, VERR_INVALID_PARAMETER);
|
---|
[14597] | 458 | AssertReturn(RT_ALIGN_T(pvR0, PAGE_SIZE, RTR0PTR) == pvR0, VERR_INVALID_PARAMETER);
|
---|
[1] | 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);
|
---|
[13816] | 470 | if (RT_SUCCESS(rc))
|
---|
[1] | 471 | {
|
---|
| 472 | pLookup->enmType = MMLOOKUPHYPERTYPE_HCPHYS;
|
---|
[12814] | 473 | pLookup->u.HCPhys.pvR3 = pvR3;
|
---|
[14597] | 474 | pLookup->u.HCPhys.pvR0 = pvR0;
|
---|
[1] | 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);
|
---|
[13816] | 482 | if (RT_SUCCESS(rc))
|
---|
[1] | 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 | *
|
---|
[58122] | 494 | * @param pVM The cross context VM structure.
|
---|
[1] | 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 | */
|
---|
[12989] | 500 | VMMR3DECL(int) MMR3HyperMapGCPhys(PVM pVM, RTGCPHYS GCPhys, size_t cb, const char *pszDesc, PRTGCPTR pGCPtr)
|
---|
[1] | 501 | {
|
---|
[13824] | 502 | LogFlow(("MMR3HyperMapGCPhys: GCPhys=%RGp cb=%d pszDesc=%p:{%s} pGCPtr=%p\n", GCPhys, (int)cb, pszDesc, pszDesc, pGCPtr));
|
---|
[1] | 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);
|
---|
[13816] | 517 | if (RT_SUCCESS(rc))
|
---|
[1] | 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);
|
---|
[13816] | 530 | if (RT_FAILURE(rc))
|
---|
[1] | 531 | {
|
---|
[13824] | 532 | AssertMsgFailed(("rc=%Rrc GCPhys=%RGp off=%#x %s\n", rc, GCPhys, off, pszDesc));
|
---|
[1] | 533 | break;
|
---|
| 534 | }
|
---|
| 535 | if (pVM->mm.s.fPGMInitialized)
|
---|
| 536 | {
|
---|
| 537 | rc = PGMMap(pVM, GCPtr + off, HCPhys, PAGE_SIZE, 0);
|
---|
| 538 | AssertRC(rc);
|
---|
[13816] | 539 | if (RT_FAILURE(rc))
|
---|
[1] | 540 | {
|
---|
[13824] | 541 | AssertMsgFailed(("rc=%Rrc GCPhys=%RGp off=%#x %s\n", rc, GCPhys, off, pszDesc));
|
---|
[1] | 542 | break;
|
---|
| 543 | }
|
---|
| 544 | }
|
---|
| 545 | }
|
---|
| 546 |
|
---|
[13816] | 547 | if (RT_SUCCESS(rc) && pGCPtr)
|
---|
[1] | 548 | *pGCPtr = GCPtr;
|
---|
| 549 | }
|
---|
| 550 | return rc;
|
---|
| 551 | }
|
---|
| 552 |
|
---|
| 553 |
|
---|
| 554 | /**
|
---|
[7635] | 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
|
---|
[64373] | 558 | * VM is powered off. If this becomes a requirement MMR3HyperUnmapMMIO2
|
---|
[7635] | 559 | * API will be needed to perform cleanups.
|
---|
| 560 | *
|
---|
| 561 | * @return VBox status code.
|
---|
| 562 | *
|
---|
[58122] | 563 | * @param pVM The cross context VM structure.
|
---|
[7635] | 564 | * @param pDevIns The device owning the MMIO2 memory.
|
---|
[64373] | 565 | * @param iSubDev The sub-device number.
|
---|
[7635] | 566 | * @param iRegion The region.
|
---|
[33540] | 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.
|
---|
[7635] | 569 | * @param pszDesc Mapping description.
|
---|
[11164] | 570 | * @param pRCPtr Where to store the RC address.
|
---|
[7635] | 571 | */
|
---|
[64373] | 572 | VMMR3DECL(int) MMR3HyperMapMMIO2(PVM pVM, PPDMDEVINS pDevIns, uint32_t iSubDev, uint32_t iRegion, RTGCPHYS off, RTGCPHYS cb,
|
---|
[11164] | 573 | const char *pszDesc, PRTRCPTR pRCPtr)
|
---|
[7635] | 574 | {
|
---|
[64373] | 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));
|
---|
[7635] | 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;
|
---|
[64373] | 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);
|
---|
[7635] | 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);
|
---|
[13816] | 603 | if (RT_SUCCESS(rc))
|
---|
[7635] | 604 | {
|
---|
| 605 | pLookup->enmType = MMLOOKUPHYPERTYPE_MMIO2;
|
---|
| 606 | pLookup->u.MMIO2.pDevIns = pDevIns;
|
---|
[64373] | 607 | pLookup->u.MMIO2.iSubDev = iSubDev;
|
---|
[7635] | 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;
|
---|
[64373] | 619 | rc = PGMR3PhysMMIO2GetHCPhys(pVM, pDevIns, iSubDev, iRegion, offCur, &HCPhys);
|
---|
[39402] | 620 | AssertRCReturn(rc, rc);
|
---|
[7635] | 621 | rc = PGMMap(pVM, GCPtr + (offCur - off), HCPhys, PAGE_SIZE, 0);
|
---|
[13816] | 622 | if (RT_FAILURE(rc))
|
---|
[7635] | 623 | {
|
---|
[13818] | 624 | AssertMsgFailed(("rc=%Rrc offCur=%RGp %s\n", rc, offCur, pszDesc));
|
---|
[7635] | 625 | break;
|
---|
| 626 | }
|
---|
| 627 | }
|
---|
| 628 | }
|
---|
| 629 |
|
---|
[13816] | 630 | if (RT_SUCCESS(rc))
|
---|
[11164] | 631 | {
|
---|
| 632 | GCPtr |= offPage;
|
---|
| 633 | *pRCPtr = GCPtr;
|
---|
| 634 | AssertLogRelReturn(*pRCPtr == GCPtr, VERR_INTERNAL_ERROR);
|
---|
| 635 | }
|
---|
[7635] | 636 | }
|
---|
| 637 | return rc;
|
---|
| 638 | }
|
---|
| 639 |
|
---|
[80118] | 640 | #endif /* !PGM_WITHOUT_MAPPINGS */
|
---|
[7635] | 641 |
|
---|
| 642 | /**
|
---|
[1480] | 643 | * Maps locked R3 virtual memory into the hypervisor region in the GC.
|
---|
| 644 | *
|
---|
| 645 | * @return VBox status code.
|
---|
| 646 | *
|
---|
[58122] | 647 | * @param pVM The cross context VM structure.
|
---|
[1480] | 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.
|
---|
[12814] | 653 | * @param pGCPtr Where to store the GC address corresponding to pvR3.
|
---|
[1480] | 654 | */
|
---|
[50111] | 655 | VMMR3DECL(int) MMR3HyperMapPages(PVM pVM, void *pvR3, RTR0PTR pvR0, size_t cPages, PCSUPPAGE paPages,
|
---|
| 656 | const char *pszDesc, PRTGCPTR pGCPtr)
|
---|
[1480] | 657 | {
|
---|
[6529] | 658 | LogFlow(("MMR3HyperMapPages: pvR3=%p pvR0=%p cPages=%zu paPages=%p pszDesc=%p:{%s} pGCPtr=%p\n",
|
---|
[1480] | 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);
|
---|
[14645] | 666 | AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
|
---|
| 667 | AssertReturn(cPages <= VBOX_MAX_ALLOC_PAGE_COUNT, VERR_PAGE_COUNT_OUT_OF_RANGE);
|
---|
[1480] | 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);
|
---|
[13816] | 678 | if (RT_SUCCESS(rc))
|
---|
[1480] | 679 | {
|
---|
| 680 | /*
|
---|
[18718] | 681 | * Copy the physical page addresses and tell PGM about them.
|
---|
[1480] | 682 | */
|
---|
[18718] | 683 | PRTHCPHYS paHCPhysPages = (PRTHCPHYS)MMR3HeapAlloc(pVM, MM_TAG_MM, sizeof(RTHCPHYS) * cPages);
|
---|
| 684 | if (paHCPhysPages)
|
---|
[1480] | 685 | {
|
---|
| 686 | for (size_t i = 0; i < cPages; i++)
|
---|
| 687 | {
|
---|
[50111] | 688 | AssertReleaseMsgReturn( paPages[i].Phys != 0
|
---|
| 689 | && paPages[i].Phys != NIL_RTHCPHYS
|
---|
| 690 | && !(paPages[i].Phys & PAGE_OFFSET_MASK),
|
---|
[19608] | 691 | ("i=%#zx Phys=%RHp %s\n", i, paPages[i].Phys, pszDesc),
|
---|
| 692 | VERR_INTERNAL_ERROR);
|
---|
[18718] | 693 | paHCPhysPages[i] = paPages[i].Phys;
|
---|
[1480] | 694 | }
|
---|
| 695 |
|
---|
[80118] | 696 | #ifndef PGM_WITHOUT_MAPPINGS
|
---|
[1480] | 697 | if (pVM->mm.s.fPGMInitialized)
|
---|
[18718] | 698 | {
|
---|
| 699 | for (size_t i = 0; i < cPages; i++)
|
---|
| 700 | {
|
---|
| 701 | rc = PGMMap(pVM, GCPtr + (i << PAGE_SHIFT), paHCPhysPages[i], PAGE_SIZE, 0);
|
---|
[18719] | 702 | AssertRCBreak(rc);
|
---|
[18718] | 703 | }
|
---|
| 704 | }
|
---|
[80118] | 705 | #endif
|
---|
[13816] | 706 | if (RT_SUCCESS(rc))
|
---|
[1480] | 707 | {
|
---|
| 708 | pLookup->enmType = MMLOOKUPHYPERTYPE_LOCKED;
|
---|
[18718] | 709 | pLookup->u.Locked.pvR3 = pvR3;
|
---|
| 710 | pLookup->u.Locked.pvR0 = pvR0;
|
---|
| 711 | pLookup->u.Locked.paHCPhysPages = paHCPhysPages;
|
---|
[1480] | 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 |
|
---|
[80118] | 725 | #ifndef PGM_WITHOUT_MAPPINGS
|
---|
[1480] | 726 | /**
|
---|
[1] | 727 | * Reserves a hypervisor memory area.
|
---|
[7715] | 728 | * Most frequent usage is fence pages and dynamically mappings like the guest PD and PDPT.
|
---|
[1] | 729 | *
|
---|
| 730 | * @return VBox status code.
|
---|
| 731 | *
|
---|
[58122] | 732 | * @param pVM The cross context VM structure.
|
---|
[1] | 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 | */
|
---|
[12989] | 737 | VMMR3DECL(int) MMR3HyperReserve(PVM pVM, unsigned cb, const char *pszDesc, PRTGCPTR pGCPtr)
|
---|
[1] | 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);
|
---|
[13816] | 758 | if (RT_SUCCESS(rc))
|
---|
[1] | 759 | {
|
---|
| 760 | pLookup->enmType = MMLOOKUPHYPERTYPE_DYNAMIC;
|
---|
| 761 | if (pGCPtr)
|
---|
| 762 | *pGCPtr = GCPtr;
|
---|
| 763 | return VINF_SUCCESS;
|
---|
| 764 | }
|
---|
| 765 | return rc;
|
---|
| 766 | }
|
---|
[80118] | 767 | #endif /* !PGM_WITHOUT_MAPPINGS */
|
---|
[1] | 768 |
|
---|
| 769 |
|
---|
| 770 | /**
|
---|
[80118] | 771 | * Reserves an electric fence page.
|
---|
| 772 | *
|
---|
| 773 | * @returns VBox status code.
|
---|
| 774 | * @param pVM The cross context VM structure.
|
---|
| 775 | */
|
---|
| 776 | VMMR3DECL(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 | /**
|
---|
[1] | 788 | * Adds memory to the hypervisor memory arena.
|
---|
| 789 | *
|
---|
| 790 | * @return VBox status code.
|
---|
[58122] | 791 | * @param pVM The cross context VM structure.
|
---|
[33540] | 792 | * @param cb Size of the memory. Will be rounded up to nearest page.
|
---|
[1] | 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 | */
|
---|
| 799 | static int mmR3HyperMap(PVM pVM, const size_t cb, const char *pszDesc, PRTGCPTR pGCPtr, PMMLOOKUPHYPER *ppLookup)
|
---|
| 800 | {
|
---|
| 801 | /*
|
---|
| 802 | * Validate input.
|
---|
| 803 | */
|
---|
[14071] | 804 | const uint32_t cbAligned = RT_ALIGN_32(cb, PAGE_SIZE);
|
---|
[1] | 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 | {
|
---|
[17536] | 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));
|
---|
[1] | 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);
|
---|
[13816] | 818 | if (RT_SUCCESS(rc))
|
---|
[1] | 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;
|
---|
[12814] | 826 | pVM->mm.s.offLookupHyper = (uint8_t *)pLookup - (uint8_t *)pVM->mm.s.pHyperHeapR3;
|
---|
[1] | 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);
|
---|
[13823] | 842 | LogFlow(("mmR3HyperMap: returns %Rrc *pGCPtr=%RGv\n", rc, *pGCPtr));
|
---|
[1] | 843 | return rc;
|
---|
| 844 | }
|
---|
| 845 |
|
---|
| 846 |
|
---|
| 847 | /**
|
---|
| 848 | * Allocates a new heap.
|
---|
| 849 | *
|
---|
| 850 | * @returns VBox status code.
|
---|
[58122] | 851 | * @param pVM The cross context VM structure.
|
---|
[14601] | 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.
|
---|
[1] | 856 | */
|
---|
[14601] | 857 | static int mmR3HyperHeapCreate(PVM pVM, const size_t cb, PMMHYPERHEAP *ppHeap, PRTR0PTR pR0PtrHeap)
|
---|
[1] | 858 | {
|
---|
| 859 | /*
|
---|
| 860 | * Allocate the hypervisor heap.
|
---|
| 861 | */
|
---|
[14543] | 862 | const uint32_t cbAligned = RT_ALIGN_32(cb, PAGE_SIZE);
|
---|
[1] | 863 | AssertReturn(cbAligned >= cb, VERR_INVALID_PARAMETER);
|
---|
[18429] | 864 | uint32_t const cPages = cbAligned >> PAGE_SHIFT;
|
---|
[14589] | 865 | PSUPPAGE paPages = (PSUPPAGE)MMR3HeapAlloc(pVM, MM_TAG_MM, cPages * sizeof(paPages[0]));
|
---|
| 866 | if (!paPages)
|
---|
| 867 | return VERR_NO_MEMORY;
|
---|
[14590] | 868 | void *pv;
|
---|
| 869 | RTR0PTR pvR0 = NIL_RTR0PTR;
|
---|
| 870 | int rc = SUPR3PageAllocEx(cPages,
|
---|
| 871 | 0 /*fFlags*/,
|
---|
| 872 | &pv,
|
---|
[50113] | 873 | &pvR0,
|
---|
[14590] | 874 | paPages);
|
---|
[13816] | 875 | if (RT_SUCCESS(rc))
|
---|
[1] | 876 | {
|
---|
[80118] | 877 | Assert(pvR0 != NIL_RTR0PTR && !(PAGE_OFFSET_MASK & pvR0));
|
---|
[14589] | 878 | memset(pv, 0, cbAligned);
|
---|
| 879 |
|
---|
[1] | 880 | /*
|
---|
| 881 | * Initialize the heap and first free chunk.
|
---|
| 882 | */
|
---|
| 883 | PMMHYPERHEAP pHeap = (PMMHYPERHEAP)pv;
|
---|
| 884 | pHeap->u32Magic = MMHYPERHEAP_MAGIC;
|
---|
[12792] | 885 | pHeap->pbHeapR3 = (uint8_t *)pHeap + MMYPERHEAP_HDR_SIZE;
|
---|
[80118] | 886 | pHeap->pbHeapR0 = pvR0 + MMYPERHEAP_HDR_SIZE;
|
---|
[14589] | 887 | //pHeap->pbHeapRC = 0; // set by mmR3HyperHeapMap()
|
---|
[12792] | 888 | pHeap->pVMR3 = pVM;
|
---|
[80281] | 889 | pHeap->pVMR0 = pVM->pVMR0ForCall;
|
---|
[12794] | 890 | pHeap->pVMRC = pVM->pVMRC;
|
---|
[1] | 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;
|
---|
[14589] | 897 | pHeap->paPages = paPages;
|
---|
[1] | 898 |
|
---|
[12792] | 899 | PMMHYPERCHUNKFREE pFree = (PMMHYPERCHUNKFREE)pHeap->pbHeapR3;
|
---|
[1] | 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;
|
---|
[14601] | 911 | *pR0PtrHeap = pvR0;
|
---|
[1] | 912 | return VINF_SUCCESS;
|
---|
| 913 | }
|
---|
[14590] | 914 | AssertMsgFailed(("SUPR3PageAllocEx(%d,,,,) -> %Rrc\n", cbAligned >> PAGE_SHIFT, rc));
|
---|
[1] | 915 |
|
---|
| 916 | *ppHeap = NULL;
|
---|
| 917 | return rc;
|
---|
| 918 | }
|
---|
| 919 |
|
---|
[80118] | 920 |
|
---|
[1] | 921 | /**
|
---|
| 922 | * Allocates a new heap.
|
---|
| 923 | */
|
---|
| 924 | static int mmR3HyperHeapMap(PVM pVM, PMMHYPERHEAP pHeap, PRTGCPTR ppHeapGC)
|
---|
| 925 | {
|
---|
[14589] | 926 | Assert(RT_ALIGN_Z(pHeap->cbHeap + MMYPERHEAP_HDR_SIZE, PAGE_SIZE) == pHeap->cbHeap + MMYPERHEAP_HDR_SIZE);
|
---|
[80118] | 927 | Assert(pHeap->pbHeapR0);
|
---|
[14589] | 928 | Assert(pHeap->paPages);
|
---|
| 929 | int rc = MMR3HyperMapPages(pVM,
|
---|
| 930 | pHeap,
|
---|
[80118] | 931 | pHeap->pbHeapR0 - MMYPERHEAP_HDR_SIZE,
|
---|
[14589] | 932 | (pHeap->cbHeap + MMYPERHEAP_HDR_SIZE) >> PAGE_SHIFT,
|
---|
| 933 | pHeap->paPages,
|
---|
| 934 | "Heap", ppHeapGC);
|
---|
[13816] | 935 | if (RT_SUCCESS(rc))
|
---|
[1] | 936 | {
|
---|
[12794] | 937 | pHeap->pVMRC = pVM->pVMRC;
|
---|
[12792] | 938 | pHeap->pbHeapRC = *ppHeapGC + MMYPERHEAP_HDR_SIZE;
|
---|
[1] | 939 | /* Reserve a page for fencing. */
|
---|
[80118] | 940 | MMR3HyperReserveFence(pVM);
|
---|
[14589] | 941 |
|
---|
| 942 | /* We won't need these any more. */
|
---|
| 943 | MMR3HeapFree(pHeap->paPages);
|
---|
| 944 | pHeap->paPages = NULL;
|
---|
[1] | 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.
|
---|
[58122] | 961 | * @param pVM The cross context VM structure.
|
---|
[1] | 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 | */
|
---|
[20531] | 971 | VMMR3DECL(int) MMR3HyperAllocOnceNoRel(PVM pVM, size_t cb, unsigned uAlignment, MMTAG enmTag, void **ppv)
|
---|
[1] | 972 | {
|
---|
[21094] | 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.
|
---|
[58122] | 988 | * @param pVM The cross context VM structure.
|
---|
[21094] | 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 | */
|
---|
| 998 | VMMR3DECL(int) MMR3HyperAllocOnceNoRelEx(PVM pVM, size_t cb, unsigned uAlignment, MMTAG enmTag, uint32_t fFlags, void **ppv)
|
---|
| 999 | {
|
---|
[1] | 1000 | AssertMsg(cb >= 8, ("Hey! Do you really mean to allocate less than 8 bytes?! cb=%d\n", cb));
|
---|
[21094] | 1001 | Assert(!(fFlags & ~(MMHYPER_AONR_FLAGS_KERNEL_MAPPING)));
|
---|
[1] | 1002 |
|
---|
| 1003 | /*
|
---|
| 1004 | * Choose between allocating a new chunk of HMA memory
|
---|
[15508] | 1005 | * and the heap. We will only do BIG allocations from HMA and
|
---|
| 1006 | * only at creation time.
|
---|
[1] | 1007 | */
|
---|
[15508] | 1008 | if ( ( cb < _64K
|
---|
| 1009 | && ( uAlignment != PAGE_SIZE
|
---|
[21094] | 1010 | || cb < 48*_1K)
|
---|
| 1011 | && !(fFlags & MMHYPER_AONR_FLAGS_KERNEL_MAPPING)
|
---|
| 1012 | )
|
---|
| 1013 | || VMR3GetState(pVM) != VMSTATE_CREATING
|
---|
| 1014 | )
|
---|
[1] | 1015 | {
|
---|
[21094] | 1016 | Assert(!(fFlags & MMHYPER_AONR_FLAGS_KERNEL_MAPPING));
|
---|
[1] | 1017 | int rc = MMHyperAlloc(pVM, cb, uAlignment, enmTag, ppv);
|
---|
| 1018 | if ( rc != VERR_MM_HYPER_NO_MEMORY
|
---|
| 1019 | || cb <= 8*_1K)
|
---|
| 1020 | {
|
---|
[6816] | 1021 | Log2(("MMR3HyperAllocOnceNoRel: cb=%#zx uAlignment=%#x returns %Rrc and *ppv=%p\n",
|
---|
[1] | 1022 | cb, uAlignment, rc, *ppv));
|
---|
| 1023 | return rc;
|
---|
| 1024 | }
|
---|
| 1025 | }
|
---|
| 1026 |
|
---|
[21094] | 1027 | #ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
[1] | 1028 | /*
|
---|
[21094] | 1029 | * Set MMHYPER_AONR_FLAGS_KERNEL_MAPPING if we're in going to execute in ring-0.
|
---|
| 1030 | */
|
---|
[70948] | 1031 | if (VM_IS_HM_OR_NEM_ENABLED(pVM))
|
---|
[21094] | 1032 | fFlags |= MMHYPER_AONR_FLAGS_KERNEL_MAPPING;
|
---|
| 1033 | #endif
|
---|
| 1034 |
|
---|
| 1035 | /*
|
---|
[1] | 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 | /*
|
---|
[14590] | 1052 | * Allocate the pages and map them into HMA space.
|
---|
[1] | 1053 | */
|
---|
[18430] | 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]));
|
---|
[14590] | 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,
|
---|
[54823] | 1065 | &pvR0,
|
---|
[14590] | 1066 | paPages);
|
---|
[13816] | 1067 | if (RT_SUCCESS(rc))
|
---|
[1] | 1068 | {
|
---|
[54823] | 1069 | Assert(pvR0 != NIL_RTR0PTR);
|
---|
[18430] | 1070 | memset(pvPages, 0, cbAligned);
|
---|
[14590] | 1071 |
|
---|
[1] | 1072 | RTGCPTR GCPtr;
|
---|
[14590] | 1073 | rc = MMR3HyperMapPages(pVM,
|
---|
| 1074 | pvPages,
|
---|
| 1075 | pvR0,
|
---|
| 1076 | cPages,
|
---|
| 1077 | paPages,
|
---|
[20775] | 1078 | MMR3HeapAPrintf(pVM, MM_TAG_MM, "alloc once (%s)", mmGetTagName(enmTag)),
|
---|
[12967] | 1079 | &GCPtr);
|
---|
[65721] | 1080 | /* not needed anymore */
|
---|
| 1081 | RTMemTmpFree(paPages);
|
---|
[13816] | 1082 | if (RT_SUCCESS(rc))
|
---|
[1] | 1083 | {
|
---|
| 1084 | *ppv = pvPages;
|
---|
[18430] | 1085 | Log2(("MMR3HyperAllocOnceNoRel: cbAligned=%#x uAlignment=%#x returns VINF_SUCCESS and *ppv=%p\n",
|
---|
| 1086 | cbAligned, uAlignment, *ppv));
|
---|
[80118] | 1087 | MMR3HyperReserveFence(pVM);
|
---|
[1] | 1088 | return rc;
|
---|
| 1089 | }
|
---|
[18430] | 1090 | AssertMsgFailed(("Failed to allocate %zd bytes! %Rrc\n", cbAligned, rc));
|
---|
[14590] | 1091 | SUPR3PageFreeEx(pvPages, cPages);
|
---|
[6816] | 1092 |
|
---|
[14590] | 1093 |
|
---|
[6816] | 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! */
|
---|
[18430] | 1099 | Log(("MMR3HyperAllocOnceNoRel: MMR3HyperMapHCRam failed with rc=%Rrc, try MMHyperAlloc(,%#x,,) instead\n", rc, cb));
|
---|
[6816] | 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 | }
|
---|
[1] | 1107 | }
|
---|
[6816] | 1108 | else
|
---|
[18430] | 1109 | AssertMsgFailed(("Failed to allocate %zd bytes! %Rrc\n", cbAligned, rc));
|
---|
[6816] | 1110 |
|
---|
[1] | 1111 | if (rc == VERR_NO_MEMORY)
|
---|
| 1112 | rc = VERR_MM_HYPER_NO_MEMORY;
|
---|
[6816] | 1113 | LogRel(("MMR3HyperAllocOnceNoRel: cb=%#zx uAlignment=%#x returns %Rrc\n", cb, uAlignment, rc));
|
---|
[1] | 1114 | return rc;
|
---|
| 1115 | }
|
---|
| 1116 |
|
---|
| 1117 |
|
---|
| 1118 | /**
|
---|
[20531] | 1119 | * Lookus up a ring-3 pointer to HMA.
|
---|
| 1120 | *
|
---|
| 1121 | * @returns The lookup record on success, NULL on failure.
|
---|
[58122] | 1122 | * @param pVM The cross context VM structure.
|
---|
[20531] | 1123 | * @param pvR3 The ring-3 address to look up.
|
---|
| 1124 | */
|
---|
| 1125 | DECLINLINE(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).
|
---|
[58122] | 1171 | * @param pVM The cross context VM structure.
|
---|
[20531] | 1172 | * @param pvStart The hyper heap page address. Must be page
|
---|
| 1173 | * aligned.
|
---|
| 1174 | * @param cb The number of bytes. Must be page aligned.
|
---|
[33540] | 1175 | * @param fSet Whether to set or unset guard page status.
|
---|
[20531] | 1176 | */
|
---|
| 1177 | VMMR3DECL(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);
|
---|
[26522] | 1184 | AssertReturn(cb <= UINT32_MAX, VERR_INVALID_PARAMETER);
|
---|
[20531] | 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;
|
---|
[21992] | 1195 | RTR0PTR R0Ptr = pLookup->u.Locked.pvR0 != (uintptr_t)pLookup->u.Locked.pvR3
|
---|
| 1196 | ? pLookup->u.Locked.pvR0
|
---|
| 1197 | : NIL_RTR0PTR;
|
---|
[20531] | 1198 | uint32_t off = (uint32_t)((uint8_t *)pvStart - pbR3);
|
---|
| 1199 | int rc;
|
---|
| 1200 | if (fSet)
|
---|
| 1201 | {
|
---|
[80118] | 1202 | #ifndef PGM_WITHOUT_MAPPINGS
|
---|
[20531] | 1203 | rc = PGMMapSetPage(pVM, MMHyperR3ToRC(pVM, pvStart), cb, 0);
|
---|
[80118] | 1204 | #else
|
---|
| 1205 | rc = VINF_SUCCESS;
|
---|
| 1206 | #endif
|
---|
[26522] | 1207 | SUPR3PageProtect(pbR3, R0Ptr, off, (uint32_t)cb, RTMEM_PROT_NONE);
|
---|
[20531] | 1208 | }
|
---|
| 1209 | else
|
---|
| 1210 | {
|
---|
[80118] | 1211 | #ifndef PGM_WITHOUT_MAPPINGS
|
---|
[20531] | 1212 | rc = PGMMapSetPage(pVM, MMHyperR3ToRC(pVM, pvStart), cb, X86_PTE_P | X86_PTE_A | X86_PTE_D | X86_PTE_RW);
|
---|
[80118] | 1213 | #else
|
---|
| 1214 | rc = VINF_SUCCESS;
|
---|
| 1215 | #endif
|
---|
[26522] | 1216 | SUPR3PageProtect(pbR3, R0Ptr, off, (uint32_t)cb, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
|
---|
[20531] | 1217 | }
|
---|
| 1218 | return rc;
|
---|
| 1219 | }
|
---|
| 1220 |
|
---|
| 1221 |
|
---|
| 1222 | /**
|
---|
[1] | 1223 | * Convert hypervisor HC virtual address to HC physical address.
|
---|
| 1224 | *
|
---|
| 1225 | * @returns HC physical address.
|
---|
[58122] | 1226 | * @param pVM The cross context VM structure.
|
---|
[12814] | 1227 | * @param pvR3 Host context virtual address.
|
---|
[1] | 1228 | */
|
---|
[12989] | 1229 | VMMR3DECL(RTHCPHYS) MMR3HyperHCVirt2HCPhys(PVM pVM, void *pvR3)
|
---|
[1] | 1230 | {
|
---|
[12814] | 1231 | PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
|
---|
[1] | 1232 | for (;;)
|
---|
| 1233 | {
|
---|
| 1234 | switch (pLookup->enmType)
|
---|
| 1235 | {
|
---|
| 1236 | case MMLOOKUPHYPERTYPE_LOCKED:
|
---|
| 1237 | {
|
---|
[12814] | 1238 | unsigned off = (uint8_t *)pvR3 - (uint8_t *)pLookup->u.Locked.pvR3;
|
---|
[1] | 1239 | if (off < pLookup->cb)
|
---|
[18718] | 1240 | return pLookup->u.Locked.paHCPhysPages[off >> PAGE_SHIFT] | (off & PAGE_OFFSET_MASK);
|
---|
[1] | 1241 | break;
|
---|
| 1242 | }
|
---|
| 1243 |
|
---|
| 1244 | case MMLOOKUPHYPERTYPE_HCPHYS:
|
---|
| 1245 | {
|
---|
[12814] | 1246 | unsigned off = (uint8_t *)pvR3 - (uint8_t *)pLookup->u.HCPhys.pvR3;
|
---|
[1] | 1247 | if (off < pLookup->cb)
|
---|
| 1248 | return pLookup->u.HCPhys.HCPhys + off;
|
---|
| 1249 | break;
|
---|
| 1250 | }
|
---|
| 1251 |
|
---|
| 1252 | case MMLOOKUPHYPERTYPE_GCPHYS:
|
---|
[7635] | 1253 | case MMLOOKUPHYPERTYPE_MMIO2:
|
---|
[1] | 1254 | case MMLOOKUPHYPERTYPE_DYNAMIC:
|
---|
[7635] | 1255 | /* can (or don't want to) convert these kind of records. */
|
---|
[1] | 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;
|
---|
[12814] | 1266 | pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
|
---|
[1] | 1267 | }
|
---|
| 1268 |
|
---|
[12814] | 1269 | AssertMsgFailed(("pvR3=%p is not inside the hypervisor memory area!\n", pvR3));
|
---|
[1] | 1270 | return NIL_RTHCPHYS;
|
---|
| 1271 | }
|
---|
| 1272 |
|
---|
[80118] | 1273 | #ifndef PGM_WITHOUT_MAPPINGS
|
---|
[1] | 1274 |
|
---|
[31978] | 1275 | /**
|
---|
[39078] | 1276 | * Implements the hcphys-not-found return case of MMR3HyperQueryInfoFromHCPhys.
|
---|
[31978] | 1277 | *
|
---|
| 1278 | * @returns VINF_SUCCESS, VINF_BUFFER_OVERFLOW.
|
---|
[58122] | 1279 | * @param pVM The cross context VM structure.
|
---|
[31978] | 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 | */
|
---|
| 1286 | static int mmR3HyperQueryInfoFromHCPhysFound(PVM pVM, RTHCPHYS HCPhys, PMMLOOKUPHYPER pLookup,
|
---|
| 1287 | char *pszWhat, size_t cbWhat, uint32_t *pcbAlloc)
|
---|
| 1288 | {
|
---|
[39078] | 1289 | NOREF(pVM); NOREF(HCPhys);
|
---|
[31978] | 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.
|
---|
[58122] | 1300 | * @param pVM The cross context VM structure.
|
---|
[31978] | 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 | */
|
---|
| 1306 | VMMR3_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 |
|
---|
[1] | 1360 | /**
|
---|
| 1361 | * Read hypervisor memory from GC virtual address.
|
---|
| 1362 | *
|
---|
[58170] | 1363 | * @returns VBox status code.
|
---|
[58122] | 1364 | * @param pVM The cross context VM structure.
|
---|
[1] | 1365 | * @param pvDst Destination address (HC of course).
|
---|
| 1366 | * @param GCPtr GC virtual address.
|
---|
| 1367 | * @param cb Number of bytes to read.
|
---|
[12968] | 1368 | *
|
---|
| 1369 | * @remarks For DBGF only.
|
---|
[1] | 1370 | */
|
---|
[12989] | 1371 | VMMR3DECL(int) MMR3HyperReadGCVirt(PVM pVM, void *pvDst, RTGCPTR GCPtr, size_t cb)
|
---|
[1] | 1372 | {
|
---|
| 1373 | if (GCPtr - pVM->mm.s.pvHyperAreaGC >= pVM->mm.s.cbHyperArea)
|
---|
[20868] | 1374 | return VERR_INVALID_POINTER;
|
---|
[1] | 1375 | return PGMR3MapRead(pVM, pvDst, GCPtr, cb);
|
---|
| 1376 | }
|
---|
| 1377 |
|
---|
[80118] | 1378 | #endif /* !PGM_WITHOUT_MAPPINGS */
|
---|
[1] | 1379 |
|
---|
| 1380 | /**
|
---|
| 1381 | * Info handler for 'hma', it dumps the list of lookup records for the hypervisor memory area.
|
---|
| 1382 | *
|
---|
[58122] | 1383 | * @param pVM The cross context VM structure.
|
---|
[1] | 1384 | * @param pHlp Callback functions for doing output.
|
---|
| 1385 | * @param pszArgs Argument string. Optional and specific to the handler.
|
---|
| 1386 | */
|
---|
| 1387 | static DECLCALLBACK(void) mmR3HyperInfoHma(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
|
---|
| 1388 | {
|
---|
[39078] | 1389 | NOREF(pszArgs);
|
---|
| 1390 |
|
---|
[13823] | 1391 | pHlp->pfnPrintf(pHlp, "Hypervisor Memory Area (HMA) Layout: Base %RGv, 0x%08x bytes\n",
|
---|
[1] | 1392 | pVM->mm.s.pvHyperAreaGC, pVM->mm.s.cbHyperArea);
|
---|
| 1393 |
|
---|
[12814] | 1394 | PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
|
---|
[1] | 1395 | for (;;)
|
---|
| 1396 | {
|
---|
| 1397 | switch (pLookup->enmType)
|
---|
| 1398 | {
|
---|
| 1399 | case MMLOOKUPHYPERTYPE_LOCKED:
|
---|
[14597] | 1400 | pHlp->pfnPrintf(pHlp, "%RGv-%RGv %RHv %RHv LOCKED %-*s %s\n",
|
---|
[1] | 1401 | pLookup->off + pVM->mm.s.pvHyperAreaGC,
|
---|
| 1402 | pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
|
---|
[12814] | 1403 | pLookup->u.Locked.pvR3,
|
---|
[14597] | 1404 | pLookup->u.Locked.pvR0,
|
---|
[18718] | 1405 | sizeof(RTHCPTR) * 2, "",
|
---|
[1] | 1406 | pLookup->pszDesc);
|
---|
| 1407 | break;
|
---|
| 1408 |
|
---|
| 1409 | case MMLOOKUPHYPERTYPE_HCPHYS:
|
---|
[14597] | 1410 | pHlp->pfnPrintf(pHlp, "%RGv-%RGv %RHv %RHv HCPHYS %RHp %s\n",
|
---|
[1] | 1411 | pLookup->off + pVM->mm.s.pvHyperAreaGC,
|
---|
| 1412 | pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
|
---|
[14597] | 1413 | pLookup->u.HCPhys.pvR3,
|
---|
| 1414 | pLookup->u.HCPhys.pvR0,
|
---|
| 1415 | pLookup->u.HCPhys.HCPhys,
|
---|
[1] | 1416 | pLookup->pszDesc);
|
---|
| 1417 | break;
|
---|
| 1418 |
|
---|
| 1419 | case MMLOOKUPHYPERTYPE_GCPHYS:
|
---|
[13824] | 1420 | pHlp->pfnPrintf(pHlp, "%RGv-%RGv %*s GCPHYS %RGp%*s %s\n",
|
---|
[1] | 1421 | pLookup->off + pVM->mm.s.pvHyperAreaGC,
|
---|
| 1422 | pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
|
---|
[14597] | 1423 | sizeof(RTHCPTR) * 2 * 2 + 1, "",
|
---|
[12940] | 1424 | pLookup->u.GCPhys.GCPhys, RT_ABS((int)(sizeof(RTHCPHYS) - sizeof(RTGCPHYS))) * 2, "",
|
---|
[1] | 1425 | pLookup->pszDesc);
|
---|
| 1426 | break;
|
---|
| 1427 |
|
---|
[7635] | 1428 | case MMLOOKUPHYPERTYPE_MMIO2:
|
---|
[13824] | 1429 | pHlp->pfnPrintf(pHlp, "%RGv-%RGv %*s MMIO2 %RGp%*s %s\n",
|
---|
[7635] | 1430 | pLookup->off + pVM->mm.s.pvHyperAreaGC,
|
---|
| 1431 | pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
|
---|
[14597] | 1432 | sizeof(RTHCPTR) * 2 * 2 + 1, "",
|
---|
[12940] | 1433 | pLookup->u.MMIO2.off, RT_ABS((int)(sizeof(RTHCPHYS) - sizeof(RTGCPHYS))) * 2, "",
|
---|
[7635] | 1434 | pLookup->pszDesc);
|
---|
| 1435 | break;
|
---|
| 1436 |
|
---|
[1] | 1437 | case MMLOOKUPHYPERTYPE_DYNAMIC:
|
---|
[13823] | 1438 | pHlp->pfnPrintf(pHlp, "%RGv-%RGv %*s DYNAMIC %*s %s\n",
|
---|
[1] | 1439 | pLookup->off + pVM->mm.s.pvHyperAreaGC,
|
---|
| 1440 | pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
|
---|
[14597] | 1441 | sizeof(RTHCPTR) * 2 * 2 + 1, "",
|
---|
[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;
|
---|
[12814] | 1454 | pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
|
---|
[1] | 1455 | }
|
---|
| 1456 | }
|
---|
[50111] | 1457 |
|
---|
[51271] | 1458 |
|
---|
| 1459 | /**
|
---|
| 1460 | * Re-allocates memory from the hyper heap.
|
---|
| 1461 | *
|
---|
| 1462 | * @returns VBox status code.
|
---|
[58122] | 1463 | * @param pVM The cross context VM structure.
|
---|
[54737] | 1464 | * @param pvOld The existing block of memory in the hyper heap to
|
---|
| 1465 | * re-allocate (can be NULL).
|
---|
[51271] | 1466 | * @param cbOld Size of the existing block.
|
---|
[54737] | 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.
|
---|
[51271] | 1470 | * @param enmTagNew The statistics tag.
|
---|
| 1471 | * @param cbNew The required size of the new block.
|
---|
[54737] | 1472 | * @param ppv Where to store the address to the re-allocated
|
---|
| 1473 | * block.
|
---|
[51271] | 1474 | *
|
---|
[54737] | 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.
|
---|
[51271] | 1478 | */
|
---|
| 1479 | VMMR3DECL(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 |
|
---|