MMHyper.cpp revision 4b9dfb569f4f1db12f2edcab743d6b251c0a65c1
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * MM - Memory Manager - Hypervisor Memory Area.
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * available from http://www.virtualbox.org. This file is free software;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * you can redistribute it and/or modify it under the terms of the GNU
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * General Public License (GPL) as published by the Free Software
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * additional information or have any questions.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/*******************************************************************************
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync* Header Files *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync*******************************************************************************/
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/*******************************************************************************
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync* Internal Functions *
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync*******************************************************************************/
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsyncstatic DECLCALLBACK(bool) mmR3HyperRelocateCallback(PVM pVM, RTGCPTR GCPtrOld, RTGCPTR GCPtrNew, PGMRELOCATECALL enmMode, void *pvUser);
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsyncstatic int mmR3HyperMap(PVM pVM, const size_t cb, const char *pszDesc, PRTGCPTR pGCPtr, PMMLOOKUPHYPER *ppLookup);
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsyncstatic int mmR3HyperHeapCreate(PVM pVM, const size_t cb, PMMHYPERHEAP *ppHeap);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic int mmR3HyperHeapMap(PVM pVM, PMMHYPERHEAP pHeap, PRTGCPTR ppHeapGC);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic DECLCALLBACK(void) mmR3HyperInfoHma(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Initializes the hypvervisor related MM stuff without
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * calling down to PGM.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * PGM is not initialized at this point, PGM relies on
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * the heap to initialize.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @returns VBox status.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Decide Hypervisor mapping in the guest context
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * And setup various hypervisor area and heap parameters.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pVM->mm.s.pvHyperAreaGC = (RTGCPTR)MM_HYPER_AREA_ADDRESS;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertRelease(RT_ALIGN_T(pVM->mm.s.pvHyperAreaGC, 1 << X86_PD_SHIFT, RTGCPTR) == pVM->mm.s.pvHyperAreaGC);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** @todo @bugref{1865}, @bugref{3202}: Change the cbHyperHeap default
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * depending on whether VT-x/AMD-V is enabled or not! Don't waste
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * precious kernel space on heap for the PATM. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = CFGMR3QueryU32(CFGMR3GetChild(CFGMR3GetRoot(pVM), "MM"), "cbHyperHeap", &cbHyperHeap);
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync if (rc == VERR_CFGM_NO_PARENT || rc == VERR_CFGM_VALUE_NOT_FOUND)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Allocate the hypervisor heap.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * (This must be done before we start adding memory to the
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * hypervisor static area because lookup records are allocated from it.)
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync rc = mmR3HyperHeapCreate(pVM, cbHyperHeap, &pVM->mm.s.pHyperHeapR3);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync pVM->mm.s.pHyperHeapR0 = (uintptr_t)pVM->mm.s.pHyperHeapR3; /** @todo #1865: map into ring-0 / whatever. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Make a small head fence to fend of accidental sequential access.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Map the VM structure into the hypervisor space.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertRelease(pVM->cbSelf == RT_OFFSETOF(VM, aCpus[pVM->cCPUs]));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = MMR3HyperMapPages(pVM, pVM, pVM->pVMR0, RT_ALIGN_Z(pVM->cbSelf, PAGE_SIZE) >> PAGE_SHIFT, pVM->paVMPagesR3, "VM", &GCPtr);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* Reserve a page for fencing. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * Map the heap into the hypervisor space.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = mmR3HyperHeapMap(pVM, pVM->mm.s.pHyperHeapR3, &GCPtr);
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync * Register info handlers.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync DBGFR3InfoRegisterInternal(pVM, "hma", "Show the layout of the Hypervisor Memory Area.", mmR3HyperInfoHma);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync LogFlow(("mmR3HyperInit: returns VINF_SUCCESS\n"));
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* Caller will do proper cleanup. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * Finalizes the HMA mapping.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * This is called later during init, most (all) HMA allocations should be done
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * by the time this function is called.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @returns VBox status.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * Adjust and create the HMA mapping.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync while ((RTINT)pVM->mm.s.offHyperNextStatic + 64*_1K < (RTINT)pVM->mm.s.cbHyperArea - _4M)
93f91841f87620d1cb6d0238b3d0d5e52cd3b9a4vboxsync int rc = PGMR3MapPT(pVM, pVM->mm.s.pvHyperAreaGC, pVM->mm.s.cbHyperArea,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync mmR3HyperRelocateCallback, NULL, "Hypervisor Memory Area");
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Do all the delayed mappings.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uintptr_t)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync RTGCPTR GCPtr = pVM->mm.s.pvHyperAreaGC + pLookup->off;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = mmR3MapLocked(pVM, pLookup->u.Locked.pLockedMem, GCPtr, 0, cPages, 0);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = PGMMap(pVM, GCPtr, pLookup->u.HCPhys.HCPhys, pLookup->cb, 0);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = PGMPhysGCPhys2HCPhys(pVM, GCPhys + off, &HCPhys);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = PGMMap(pVM, GCPtr + off, HCPhys, PAGE_SIZE, 0);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync const RTGCPHYS offEnd = pLookup->u.MMIO2.off + pLookup->cb;
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync for (RTGCPHYS offCur = pLookup->u.MMIO2.off; offCur < offEnd; offCur += PAGE_SIZE)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = PGMR3PhysMMIO2GetHCPhys(pVM, pLookup->u.MMIO2.pDevIns, pLookup->u.MMIO2.iRegion, offCur, &HCPhys);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = PGMMap(pVM, GCPtr + (offCur - pLookup->u.MMIO2.off), HCPhys, PAGE_SIZE, 0);
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync /* do nothing here since these are either fences or managed by someone else using PGM. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertMsgFailed(("rc=%Vrc cb=%d GCPtr=%VGv enmType=%d pszDesc=%s\n",
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc, pLookup->cb, pLookup->enmType, pLookup->pszDesc));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pLookup = (PMMLOOKUPHYPER)((uintptr_t)pLookup + pLookup->offNext);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync LogFlow(("MMR3HyperInitFinalize: returns VINF_SUCCESS\n"));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Callback function which will be called when PGM is trying to find
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * a new location for the mapping.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * The callback is called in two modes, 1) the check mode and 2) the relocate mode.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * In 1) the callback should say if it objects to a suggested new location. If it
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * accepts the new location, it is called again for doing it's relocation.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @returns true if the location is ok.
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync * @returns false if another location should be found.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param pVM The VM handle.
d5ea45cc92d7f1d3ade8189944531f665bfe8ed5vboxsync * @param GCPtrOld The old virtual address.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param GCPtrNew The new virtual address.
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync * @param enmMode Used to indicate the callback mode.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param pvUser User argument. Ignored.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @remark The return value is no a failure indicator, it's an acceptance
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * indicator. Relocation can not fail!
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsyncstatic DECLCALLBACK(bool) mmR3HyperRelocateCallback(PVM pVM, RTGCPTR GCPtrOld, RTGCPTR GCPtrNew, PGMRELOCATECALL enmMode, void *pvUser)
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync * Verify location - all locations are good for us.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return true;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * Execute the relocation.
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync * Accepted!
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync AssertMsg(GCPtrOld == pVM->mm.s.pvHyperAreaGC, ("GCPtrOld=%VGv pVM->mm.s.pvHyperAreaGC=%VGv\n", GCPtrOld, pVM->mm.s.pvHyperAreaGC));
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync Log(("Relocating the hypervisor from %VGv to %VGv\n", GCPtrOld, GCPtrNew));
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync * Relocate the VM structure and ourselves.
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync * Relocate the rest.
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync return true;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertMsgFailed(("Invalid relocation mode %d\n", enmMode));
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return false;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Maps contiguous HC physical memory into the hypervisor region in the GC.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @return VBox status code.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param pVM VM handle.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param pvR3 Host context address of the memory. Must be page
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param HCPhys Host context physical address of the memory to be
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * mapped. Must be page aligned!
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param cb Size of the memory. Will be rounded up to nearest page.
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync * @param pszDesc Description.
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * @param pGCPtr Where to store the GC address.
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsyncVMMR3DECL(int) MMR3HyperMapHCPhys(PVM pVM, void *pvR3, RTHCPHYS HCPhys, size_t cb, const char *pszDesc, PRTGCPTR pGCPtr)
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync LogFlow(("MMR3HyperMapHCPhys: pvR3=%p HCPhys=%RHp cb=%d pszDesc=%p:{%s} pGCPtr=%p\n", pvR3, HCPhys, (int)cb, pszDesc, pszDesc, pGCPtr));
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync * Validate input.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync AssertReturn(RT_ALIGN_P(pvR3, PAGE_SIZE) == pvR3, VERR_INVALID_PARAMETER);
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync AssertReturn(RT_ALIGN_T(HCPhys, PAGE_SIZE, RTHCPHYS) == HCPhys, VERR_INVALID_PARAMETER);
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync AssertReturn(pszDesc && *pszDesc, VERR_INVALID_PARAMETER);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Add the memory to the hypervisor area.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertReturn(cbAligned >= cb, VERR_INVALID_PARAMETER);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = mmR3HyperMap(pVM, cbAligned, pszDesc, &GCPtr, &pLookup);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Update the page table.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Maps contiguous GC physical memory into the hypervisor region in the GC.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @return VBox status code.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param pVM VM handle.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param GCPhys Guest context physical address of the memory to be mapped. Must be page aligned!
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param cb Size of the memory. Will be rounded up to nearest page.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param pszDesc Mapping description.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param pGCPtr Where to store the GC address.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncVMMR3DECL(int) MMR3HyperMapGCPhys(PVM pVM, RTGCPHYS GCPhys, size_t cb, const char *pszDesc, PRTGCPTR pGCPtr)
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync LogFlow(("MMR3HyperMapGCPhys: GCPhys=%VGp cb=%d pszDesc=%p:{%s} pGCPtr=%p\n", GCPhys, (int)cb, pszDesc, pszDesc, pGCPtr));
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * Validate input.
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync AssertReturn(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys, VERR_INVALID_PARAMETER);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync AssertReturn(pszDesc && *pszDesc, VERR_INVALID_PARAMETER);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * Add the memory to the hypervisor area.
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync int rc = mmR3HyperMap(pVM, cb, pszDesc, &GCPtr, &pLookup);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Update the page table.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = PGMPhysGCPhys2HCPhys(pVM, GCPhys + off, &HCPhys);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync AssertMsgFailed(("rc=%Vrc GCPhys=%VGv off=%#x %s\n", rc, GCPhys, off, pszDesc));
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync rc = PGMMap(pVM, GCPtr + off, HCPhys, PAGE_SIZE, 0);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync AssertMsgFailed(("rc=%Vrc GCPhys=%VGv off=%#x %s\n", rc, GCPhys, off, pszDesc));
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * Maps a portion of an MMIO2 region into the hypervisor region.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * Callers of this API must never deregister the MMIO2 region before the
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * VM is powered off. If this becomes a requirement MMR3HyperUnmapMMIO2
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * API will be needed to perform cleanups.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @return VBox status code.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param pVM Pointer to the shared VM structure.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param pDevIns The device owning the MMIO2 memory.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param iRegion The region.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param off The offset into the region. Will be rounded down to closest page boundrary.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param cb The number of bytes to map. Will be rounded up to the closest page boundrary.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param pszDesc Mapping description.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param pRCPtr Where to store the RC address.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncVMMR3DECL(int) MMR3HyperMapMMIO2(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion, RTGCPHYS off, RTGCPHYS cb,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync LogFlow(("MMR3HyperMapMMIO2: pDevIns=%p iRegion=%#x off=%VGp cb=%VGp pszDesc=%p:{%s} pRCPtr=%p\n",
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pDevIns, iRegion, off, cb, pszDesc, pszDesc, pRCPtr));
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * Validate input.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertReturn(pszDesc && *pszDesc, VERR_INVALID_PARAMETER);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertReturn(off + cb > off, VERR_INVALID_PARAMETER);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertReturn(offEnd > off, VERR_INVALID_PARAMETER);
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync for (RTGCPHYS offCur = off; offCur < offEnd; offCur += PAGE_SIZE)
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync rc = PGMR3PhysMMIO2GetHCPhys(pVM, pDevIns, iRegion, offCur, &HCPhys);
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync AssertMsgRCReturn(rc, ("rc=%Rrc - iRegion=%d off=%RGp\n", rc, iRegion, off), rc);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * Add the memory to the hypervisor area.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync rc = mmR3HyperMap(pVM, cb, pszDesc, &GCPtr, &pLookup);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * Update the page table.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync for (RTGCPHYS offCur = off; offCur < offEnd; offCur += PAGE_SIZE)
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync rc = PGMR3PhysMMIO2GetHCPhys(pVM, pDevIns, iRegion, offCur, &HCPhys);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync rc = PGMMap(pVM, GCPtr + (offCur - off), HCPhys, PAGE_SIZE, 0);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync AssertMsgFailed(("rc=%Vrc offCur=%RGp %s\n", rc, offCur, pszDesc));
0c4004948fca34f2db87e7b38013137e9472c306vboxsync AssertLogRelReturn(*pRCPtr == GCPtr, VERR_INTERNAL_ERROR);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * Locks and Maps HC virtual memory into the hypervisor region in the GC.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * @return VBox status code.
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * @param pVM VM handle.
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync * @param pvR3 Host context address of the memory (may be not page
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync * aligned).
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync * @param cb Size of the memory. Will be rounded up to nearest page.
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync * @param fFree Set this if MM is responsible for freeing the memory
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync * using SUPPageFree.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * @param pszDesc Mapping description.
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync * @param pGCPtr Where to store the GC address corresponding to pvR3.
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsyncVMMR3DECL(int) MMR3HyperMapHCRam(PVM pVM, void *pvR3, size_t cb, bool fFree, const char *pszDesc, PRTGCPTR pGCPtr)
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync LogFlow(("MMR3HyperMapHCRam: pvR3=%p cb=%d fFree=%d pszDesc=%p:{%s} pGCPtr=%p\n", pvR3, (int)cb, fFree, pszDesc, pszDesc, pGCPtr));
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * Validate input.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * Page align address and size.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync void *pvR3Page = (void *)((uintptr_t)pvR3 & PAGE_BASE_HC_MASK);
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync * Add the memory to the hypervisor area.
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync int rc = mmR3HyperMap(pVM, cb, pszDesc, &GCPtr, &pLookup);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Lock the heap memory and tell PGM about the locked pages.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = mmR3LockMem(pVM, pvR3Page, cb, fFree ? MM_LOCKED_TYPE_HYPER : MM_LOCKED_TYPE_HYPER_NOFREE, &pLockedMem, false /* fSilentFailure */);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* map the stuff into guest address space. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = mmR3MapLocked(pVM, pLockedMem, GCPtr, 0, ~(size_t)0, 0);
8cd2f2e64725096acb682f34a5568b7fb816eda7vboxsync /* done. */
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync /* Don't care about failure clean, we're screwed if this fails anyway. */
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync * Maps locked R3 virtual memory into the hypervisor region in the GC.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @return VBox status code.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param pVM VM handle.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param pvR3 The ring-3 address of the memory, must be page aligned.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param pvR0 The ring-0 address of the memory, must be page aligned. (optional)
cba6719bd64ec749967bbe931230452664109857vboxsync * @param cPages The number of pages.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param paPages The page descriptors.
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync * @param pszDesc Mapping description.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param pGCPtr Where to store the GC address corresponding to pvR3.
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsyncVMMR3DECL(int) MMR3HyperMapPages(PVM pVM, void *pvR3, RTR0PTR pvR0, size_t cPages, PCSUPPAGE paPages, const char *pszDesc, PRTGCPTR pGCPtr)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync LogFlow(("MMR3HyperMapPages: pvR3=%p pvR0=%p cPages=%zu paPages=%p pszDesc=%p:{%s} pGCPtr=%p\n",
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pvR3, pvR0, cPages, paPages, pszDesc, pszDesc, pGCPtr));
a8139954a84d6e9090dd3a8371aa788351d45bc3vboxsync * Validate input.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertReturn(cPages < 1024, VERR_INVALID_PARAMETER);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Add the memory to the hypervisor area.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = mmR3HyperMap(pVM, cPages << PAGE_SHIFT, pszDesc, &GCPtr, &pLookup);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Create a locked memory record and tell PGM about this.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PMMLOCKEDMEM pLockedMem = (PMMLOCKEDMEM)MMR3HeapAlloc(pVM, MM_TAG_MM, RT_OFFSETOF(MMLOCKEDMEM, aPhysPages[cPages]));
0db6a029780d9f9b347500e117320a8d5661efe5vboxsync AssertReleaseReturn(paPages[i].Phys != 0 && paPages[i].Phys != NIL_RTHCPHYS && !(paPages[i].Phys & PAGE_OFFSET_MASK), VERR_INTERNAL_ERROR);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pLockedMem->aPhysPages[i].uReserved = (RTHCUINTPTR)pLockedMem;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* map the stuff into guest address space. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = mmR3MapLocked(pVM, pLockedMem, GCPtr, 0, ~(size_t)0, 0);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* done. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Don't care about failure clean, we're screwed if this fails anyway. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * Reserves a hypervisor memory area.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * Most frequent usage is fence pages and dynamically mappings like the guest PD and PDPT.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @return VBox status code.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param pVM VM handle.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param cb Size of the memory. Will be rounded up to nearest page.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param pszDesc Mapping description.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param pGCPtr Where to store the assigned GC address. Optional.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncVMMR3DECL(int) MMR3HyperReserve(PVM pVM, unsigned cb, const char *pszDesc, PRTGCPTR pGCPtr)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync LogFlow(("MMR3HyperMapHCRam: cb=%d pszDesc=%p:{%s} pGCPtr=%p\n", (int)cb, pszDesc, pszDesc, pGCPtr));
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync * Validate input.
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync * Add the memory to the hypervisor area.
fbf08fabb4c4b383d6aa2830c2bd5b943a26f10cvboxsync int rc = mmR3HyperMap(pVM, cb, pszDesc, &GCPtr, &pLookup);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Adds memory to the hypervisor memory arena.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @return VBox status code.
ad77e3ec3cde24263bc7537575f5cae442bee3b1vboxsync * @param pVM The VM handle.
ad77e3ec3cde24263bc7537575f5cae442bee3b1vboxsync * @param cb Size of the memory. Will be rounded up to neares page.
ad77e3ec3cde24263bc7537575f5cae442bee3b1vboxsync * @param pszDesc The description of the memory.
ad77e3ec3cde24263bc7537575f5cae442bee3b1vboxsync * @param pGCPtr Where to store the GC address.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param ppLookup Where to store the pointer to the lookup record.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @remark We assume the threading structure of VBox imposes natural
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * serialization of most functions, this one included.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic int mmR3HyperMap(PVM pVM, const size_t cb, const char *pszDesc, PRTGCPTR pGCPtr, PMMLOOKUPHYPER *ppLookup)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Validate input.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync const uint32_t cbAligned = RT_ALIGN(cb, PAGE_SIZE);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertReturn(cbAligned >= cb, VERR_INVALID_PARAMETER);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (pVM->mm.s.offHyperNextStatic + cbAligned >= pVM->mm.s.cbHyperArea) /* don't use the last page, it's a fence. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertMsgFailed(("Out of static mapping space in the HMA! offHyperAreaGC=%x cbAligned=%x\n",
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Allocate lookup record.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = MMHyperAlloc(pVM, sizeof(*pLookup), 1, MM_TAG_MM, (void **)&pLookup);
cba6719bd64ec749967bbe931230452664109857vboxsync * Initialize it and insert it.
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync pVM->mm.s.offLookupHyper = (uint8_t *)pLookup - (uint8_t *)pVM->mm.s.pHyperHeapR3;
return rc;
void *pv;
int rc = SUPPageAlloc(cbAligned >> PAGE_SHIFT, &pv); /** @todo #1865: heap allocation must be changed for osx (only). */
STAMR3Register(pVM, &pHeap->cbHeap, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, "/MM/HyperHeap/cbHeap", STAMUNIT_BYTES, "The heap size.");
STAMR3Register(pVM, &pHeap->cbFree, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, "/MM/HyperHeap/cbFree", STAMUNIT_BYTES, "The free space.");
return VINF_SUCCESS;
return rc;
int rc = MMR3HyperMapHCRam(pVM, pHeap, pHeap->cbHeap + MMYPERHEAP_HDR_SIZE, true, "Heap", ppHeapGC);
return rc;
VMMDECL(int) MMR3HyperAllocOnceNoRel(PVM pVM, size_t cb, unsigned uAlignment, MMTAG enmTag, void **ppv)
return rc;
switch (uAlignment)
case PAGE_SIZE:
return VERR_INVALID_PARAMETER;
void *pvPages;
&GCPtr);
return rc;
Log(("MMR3HyperAllocOnceNoRel: MMR3HyperMapHCRam failed with rc=%Rrc, try MMHyperAlloc(,%#d,,) instead\n", rc, cb));
return rc;
return rc;
PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
case MMLOOKUPHYPERTYPE_LOCKED:
return (pLookup->u.Locked.pLockedMem->aPhysPages[off >> PAGE_SHIFT].Phys & X86_PTE_PAE_PG_MASK) | (off & PAGE_OFFSET_MASK);
case MMLOOKUPHYPERTYPE_HCPHYS:
case MMLOOKUPHYPERTYPE_GCPHYS:
case MMLOOKUPHYPERTYPE_MMIO2:
return NIL_RTHCPHYS;
void *pv;
return pv;
return NULL;
return VERR_INVALID_POINTER;
return VERR_INVALID_PARAMETER;
PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
case MMLOOKUPHYPERTYPE_LOCKED:
case MMLOOKUPHYPERTYPE_HCPHYS:
case MMLOOKUPHYPERTYPE_GCPHYS:
case MMLOOKUPHYPERTYPE_MMIO2: