PGMPhys.cpp revision 4fa9fd7328a2a320f51953c974907c53f36e5469
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * PGM - Page Manager and Monitor, Physical Memory Addressing.
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * available from http://www.virtualbox.org. This file is free software;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * you can redistribute it and/or modify it under the terms of the GNU
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * General Public License (GPL) as published by the Free Software
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * additional information or have any questions.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync/*******************************************************************************
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync* Header Files *
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync*******************************************************************************/
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync/*******************************************************************************
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync* Defined Constants And Macros *
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync*******************************************************************************/
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync/** The number of pages to free in one batch. */
822e11c896dd36c9dc3609dff676059576b7d3devboxsync/*******************************************************************************
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync* Internal Functions *
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync*******************************************************************************/
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncstatic DECLCALLBACK(int) pgmR3PhysRomWriteHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncstatic int pgmPhysFreePage(PVM pVM, PGMMFREEPAGESREQ pReq, uint32_t *pcPendingPages, PPGMPAGE pPage, RTGCPHYS GCPhys);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * PGMR3PhysReadU8-64
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * PGMR3PhysWriteU8-64
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * EMT worker for PGMR3PhysReadExternal.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncstatic DECLCALLBACK(int) pgmR3PhysReadExternalEMT(PVM pVM, PRTGCPHYS pGCPhys, void *pvBuf, size_t cbRead)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Write to physical memory, external users.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @returns VBox status code.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @retval VINF_SUCCESS.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pVM VM Handle.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param GCPhys Physical address to write to.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pvBuf What to write.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param cbWrite How many bytes to write.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @thread Any but EMTs.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncVMMR3DECL(int) PGMR3PhysReadExternal(PVM pVM, RTGCPHYS GCPhys, void *pvBuf, size_t cbRead)
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync AssertMsgReturn(cbRead > 0, ("don't even think about reading zero bytes!\n"), VINF_SUCCESS);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync LogFlow(("PGMR3PhysReadExternal: %RGp %d\n", GCPhys, cbRead));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Copy loop on ram ranges.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* Find range. */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* Inside range or not? */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Must work our way thru this page by page.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * If the page has an ALL access handler, we'll have to
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * delegate the job to EMT.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync return VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)pgmR3PhysReadExternalEMT, 4,
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Simple stuff, go ahead.
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync const void *pvSrc;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync int rc = pgmPhysGCPhys2CCPtrInternalReadOnly(pVM, pPage, pRam->GCPhys + off, &pvSrc);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternalReadOnly failed on %RGp / %R[pgmpage] -> %Rrc\n",
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* next page */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync } /* walk pages in ram range. */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync LogFlow(("PGMPhysRead: Unassigned %RGp size=%u\n", GCPhys, cbRead));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Unassigned address space.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync } /* Ram range walk */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * EMT worker for PGMR3PhysWriteExternal.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncstatic DECLCALLBACK(int) pgmR3PhysWriteExternalEMT(PVM pVM, PRTGCPHYS pGCPhys, const void *pvBuf, size_t cbWrite)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /** @todo VERR_EM_NO_MEMORY */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Write to physical memory, external users.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @returns VBox status code.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @retval VINF_SUCCESS.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @retval VERR_EM_NO_MEMORY.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pVM VM Handle.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param GCPhys Physical address to write to.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pvBuf What to write.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param cbWrite How many bytes to write.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pszWho Who is writing. For tracking down who is writing
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * after we've saved the state.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @thread Any but EMTs.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncVMMDECL(int) PGMR3PhysWriteExternal(PVM pVM, RTGCPHYS GCPhys, const void *pvBuf, size_t cbWrite, const char *pszWho)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync ("Calling PGMR3PhysWriteExternal after pgmR3Save()! GCPhys=%RGp cbWrite=%#x pszWho=%s\n",
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync AssertMsgReturn(cbWrite > 0, ("don't even think about writing zero bytes!\n"), VINF_SUCCESS);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync LogFlow(("PGMR3PhysWriteExternal: %RGp %d\n", GCPhys, cbWrite));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Copy loop on ram ranges, stop when we hit something difficult.
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* Find range. */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* Inside range or not? */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Must work our way thru this page by page.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Is the page problematic, we have to do the work on the EMT.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Allocating writable pages and access handlers are
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * problematic, write monitored pages are simple and can be
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * dealth with here.
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync || PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if ( PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_WRITE_MONITORED
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync return VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)pgmR3PhysWriteExternalEMT, 4,
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Simple stuff, go ahead.
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync int rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, pRam->GCPhys + off, &pvDst);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* next page */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync } /* walk pages in ram range */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Unassigned address space, skip it.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync } /* Ram range walk */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * VMR3ReqCall worker for PGMR3PhysGCPhys2CCPtrExternal to make pages writable.
822e11c896dd36c9dc3609dff676059576b7d3devboxsync * @returns see PGMR3PhysGCPhys2CCPtrExternal
822e11c896dd36c9dc3609dff676059576b7d3devboxsync * @param pVM The VM handle.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pGCPhys Pointer to the guest physical address.
822e11c896dd36c9dc3609dff676059576b7d3devboxsync * @param ppv Where to store the mapping address.
822e11c896dd36c9dc3609dff676059576b7d3devboxsync * @param pLock Where to store the lock.
822e11c896dd36c9dc3609dff676059576b7d3devboxsyncstatic DECLCALLBACK(int) pgmR3PhysGCPhys2CCPtrDelegated(PVM pVM, PRTGCPHYS pGCPhys, void **ppv, PPGMPAGEMAPLOCK pLock)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Just hand it to PGMPhysGCPhys2CCPtr and check that it's not a page with
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * an access handler after it succeeds.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync rc = PGMPhysGCPhys2CCPtr(pVM, *pGCPhys, ppv, pLock);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync int rc2 = pgmPhysPageQueryTlbe(&pVM->pgm.s, *pGCPhys, &pTlbe);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* We *must* flush any corresponding pgm pool page here, otherwise we'll
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * not be informed about writes and keep bogus gst->shw mappings around.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /** @todo r=bird: return VERR_PGM_PHYS_PAGE_RESERVED here if it still has
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * active handlers, see the PGMR3PhysGCPhys2CCPtrExternal docs. */
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync * Requests the mapping of a guest page into ring-3, external threads.
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync * When you're done with the page, call PGMPhysReleasePageMappingLock() ASAP to
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync * release it.
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync * This API will assume your intention is to write to the page, and will
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync * therefore replace shared and zero pages. If you do not intend to modify the
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync * page, use the PGMR3PhysGCPhys2CCPtrReadOnlyExternal() API.
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync * @returns VBox status code.
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync * @retval VINF_SUCCESS on success.
822e11c896dd36c9dc3609dff676059576b7d3devboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync * backing or if the page has any active access handlers. The caller
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync * must fall back on using PGMR3PhysWriteExternal.
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync * @param pVM The VM handle.
822e11c896dd36c9dc3609dff676059576b7d3devboxsync * @param GCPhys The guest physical address of the page that should be mapped.
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync * @param ppv Where to store the address corresponding to GCPhys.
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync * @remark Avoid calling this API from within critical sections (other than the
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync * PGM one) because of the deadlock risk when we have to delegating the
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * task to an EMT.
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync * @thread Any.
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsyncVMMR3DECL(int) PGMR3PhysGCPhys2CCPtrExternal(PVM pVM, RTGCPHYS GCPhys, void **ppv, PPGMPAGEMAPLOCK pLock)
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync * Query the Physical TLB entry for the page (may fail).
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync rc = pgmPhysPageQueryTlbe(&pVM->pgm.s, GCPhys, &pTlbe);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * If the page is shared, the zero page, or being write monitored
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync * it must be converted to an page that's writable if possible.
822e11c896dd36c9dc3609dff676059576b7d3devboxsync * We can only deal with write monitored pages here, the rest have
822e11c896dd36c9dc3609dff676059576b7d3devboxsync * to be on an EMT.
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync || PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync if ( PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_WRITE_MONITORED
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync return VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)pgmR3PhysGCPhys2CCPtrDelegated, 4,
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync * Now, just perform the locking and calculate the return address.
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync else if (cLocks != PGM_PAGE_GET_WRITE_LOCKS(pPage))
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync AssertMsgFailed(("%RGp / %R[pgmpage] is entering permanent write locked state!\n", GCPhys, pPage));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync pMap->cRefs++; /* Extra ref to prevent it from going away. */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync *ppv = (void *)((uintptr_t)pTlbe->pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync pLock->uPageAndType = (uintptr_t)pPage | PGMPAGEMAPLOCK_TYPE_WRITE;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Requests the mapping of a guest page into ring-3, external threads.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * When you're done with the page, call PGMPhysReleasePageMappingLock() ASAP to
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * release it.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @returns VBox status code.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @retval VINF_SUCCESS on success.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * backing or if the page as an active ALL access handler. The caller
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * must fall back on using PGMPhysRead.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync * @param pVM The VM handle.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param GCPhys The guest physical address of the page that should be mapped.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param ppv Where to store the address corresponding to GCPhys.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @remark Avoid calling this API from within critical sections (other than
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * the PGM one) because of the deadlock risk.
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync * @thread Any.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncVMMR3DECL(int) PGMR3PhysGCPhys2CCPtrReadOnlyExternal(PVM pVM, RTGCPHYS GCPhys, void const **ppv, PPGMPAGEMAPLOCK pLock)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Query the Physical TLB entry for the page (may fail).
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync rc = pgmPhysPageQueryTlbe(&pVM->pgm.s, GCPhys, &pTlbe);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* MMIO pages doesn't have any readable backing. */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Now, just perform the locking and calculate the return address.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync AssertMsgFailed(("%RGp / %R[pgmpage] is entering permanent readonly locked state!\n", GCPhys, pPage));
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync pMap->cRefs++; /* Extra ref to prevent it from going away. */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync *ppv = (void *)((uintptr_t)pTlbe->pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync pLock->uPageAndType = (uintptr_t)pPage | PGMPAGEMAPLOCK_TYPE_READ;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Relinks the RAM ranges using the pSelfRC and pSelfR0 pointers.
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync * Called when anything was relocated.
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync * @param pVM Pointer to the shared VM structure.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync for (pCur = pVM->pgm.s.pRamRangesR3; pCur; pCur = pCur->pNextR3)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync Assert((pCur->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING) || pCur->pSelfR0 == MMHyperCCToR0(pVM, pCur));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync Assert((pCur->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING) || pCur->pSelfRC == MMHyperCCToRC(pVM, pCur));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync Assert((pCur->GCPhysLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync Assert(pCur->cb == pCur->GCPhysLast - pCur->GCPhys + 1);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync for (PPGMRAMRANGE pCur2 = pVM->pgm.s.pRamRangesR3; pCur2; pCur2 = pCur2->pNextR3)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync || strcmp(pCur2->pszDesc, pCur->pszDesc)); /** @todo fix MMIO ranges!! */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Links a new RAM range into the list.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pVM Pointer to the shared VM structure.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pNew Pointer to the new list entry.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pPrev Pointer to the previous list entry. If NULL, insert as head.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncstatic void pgmR3PhysLinkRamRange(PVM pVM, PPGMRAMRANGE pNew, PPGMRAMRANGE pPrev)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync AssertMsg(pNew->pszDesc, ("%RGp-%RGp\n", pNew->GCPhys, pNew->GCPhysLast));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync Assert((pNew->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING) || pNew->pSelfR0 == MMHyperCCToR0(pVM, pNew));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync Assert((pNew->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING) || pNew->pSelfRC == MMHyperCCToRC(pVM, pNew));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync PPGMRAMRANGE pRam = pPrev ? pPrev->pNextR3 : pVM->pgm.s.pRamRangesR3;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync pNew->pNextR0 = pRam ? pRam->pSelfR0 : NIL_RTR0PTR;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync pNew->pNextRC = pRam ? pRam->pSelfRC : NIL_RTRCPTR;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Unlink an existing RAM range from the list.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pVM Pointer to the shared VM structure.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pRam Pointer to the new list entry.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pPrev Pointer to the previous list entry. If NULL, insert as head.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncstatic void pgmR3PhysUnlinkRamRange2(PVM pVM, PPGMRAMRANGE pRam, PPGMRAMRANGE pPrev)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync Assert(pPrev ? pPrev->pNextR3 == pRam : pVM->pgm.s.pRamRangesR3 == pRam);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync Assert((pRam->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING) || pRam->pSelfR0 == MMHyperCCToR0(pVM, pRam));
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync Assert((pRam->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING) || pRam->pSelfRC == MMHyperCCToRC(pVM, pRam));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync pPrev->pNextR0 = pNext ? pNext->pSelfR0 : NIL_RTR0PTR;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync pPrev->pNextRC = pNext ? pNext->pSelfRC : NIL_RTRCPTR;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync pVM->pgm.s.pRamRangesR0 = pNext ? pNext->pSelfR0 : NIL_RTR0PTR;
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync pVM->pgm.s.pRamRangesRC = pNext ? pNext->pSelfRC : NIL_RTRCPTR;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Unlink an existing RAM range from the list.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pVM Pointer to the shared VM structure.
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync * @param pRam Pointer to the new list entry.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncstatic void pgmR3PhysUnlinkRamRange(PVM pVM, PPGMRAMRANGE pRam)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* find prev. */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Frees a range of pages, replacing them with ZERO pages of the specified type.
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync * @returns VBox status code.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pVM The VM handle.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pRam The RAM range in which the pages resides.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param GCPhys The address of the first page.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param GCPhysLast The address of the last page.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param uType The page type to replace then with.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncstatic int pgmR3PhysFreePageRange(PVM pVM, PPGMRAMRANGE pRam, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast, uint8_t uType)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync int rc = GMMR3FreePagesPrepare(pVM, &pReq, PGMPHYS_FREE_PAGE_BATCH_SIZE, GMMACCOUNT_BASE);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* Iterate the pages. */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync PPGMPAGE pPageDst = &pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync uint32_t cPagesLeft = ((GCPhysLast - GCPhys) >> PAGE_SHIFT) + 1;
d1e57a56f56520aa667d719ae7a7a0e1171c2926vboxsync while (cPagesLeft-- > 0)
822e11c896dd36c9dc3609dff676059576b7d3devboxsync rc = pgmPhysFreePage(pVM, pReq, &cPendingPages, pPageDst, GCPhys);
822e11c896dd36c9dc3609dff676059576b7d3devboxsync AssertLogRelRCReturn(rc, rc); /* We're done for if this goes wrong. */
822e11c896dd36c9dc3609dff676059576b7d3devboxsync rc = GMMR3FreePagesPerform(pVM, pReq, cPendingPages);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Rendezvous callback used by PGMR3ChangeMemBalloon that changes the memory balloon size
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * This is only called on one of the EMTs while the other ones are waiting for
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * it to complete this function.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @returns VINF_SUCCESS (VBox strict status code).
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pVM The VM handle.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pVCpu The VMCPU for the EMT we're being called on. Unused.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pvUser User parameter
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncstatic DECLCALLBACK(VBOXSTRICTRC) pgmR3PhysChangeMemBalloonRendezvous(PVM pVM, PVMCPU pVCpu, void *pvUser)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync Log(("pgmR3PhysChangeMemBalloonRendezvous: %s %x pages\n", (fInflate) ? "inflate" : "deflate", cPages));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* Replace pages with ZERO pages. */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync rc = GMMR3FreePagesPrepare(pVM, &pReq, PGMPHYS_FREE_PAGE_BATCH_SIZE, GMMACCOUNT_BASE);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* Iterate the pages. */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync for (unsigned i = 0; i < cPages; i++)
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, paPhysPage[i]);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync Log(("pgmR3PhysChangeMemBalloonRendezvous: invalid physical page %RGp pPage->u3Type=%d\n", paPhysPage[i], (pPage) ? pPage->uTypeY : 0));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* Flush the shadow PT if this page was previously used as a guest page table. */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync rc = pgmPhysFreePage(pVM, pReq, &cPendingPages, pPage, paPhysPage[i]);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_BALLOONED);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync rc = GMMR3FreePagesPerform(pVM, pReq, cPendingPages);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* Flush the PGM pool cache as we might have stale references to pages that we just freed. */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* Iterate the pages. */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync for (unsigned i = 0; i < cPages; i++)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, paPhysPage[i]);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync AssertBreak(pPage && pPage->uTypeY == PGMPAGETYPE_RAM);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync LogFlow(("Free ballooned page: %RGp\n", paPhysPage[i]));
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync /* Change back to zero page. */
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync /* Note that we currently do not map any ballooned pages in our shadow page tables, so no need to flush the pgm pool. */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* Notify GMM about the balloon change. */
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync rc = GMMR3BalloonedPages(pVM, (fInflate) ? GMMBALLOONACTION_INFLATE : GMMBALLOONACTION_DEFLATE, cPages);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync /* Flush the recompiler's TLB as well. */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync CPUMSetChangedFlags(&pVM->aCpus[i], CPUM_CHANGED_GLOBAL_TLB_FLUSH);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Frees a range of ram pages, replacing them with ZERO pages; helper for PGMR3PhysFreeRamPages
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @returns VBox status code.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pVM The VM handle.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param fInflate Inflate or deflate memory balloon
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param cPages Number of pages to free
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param paPhysPage Array of guest physical addresses
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsyncstatic DECLCALLBACK(void) pgmR3PhysChangeMemBalloonHelper(PVM pVM, bool fInflate, unsigned cPages, RTGCPHYS *paPhysPage)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, pgmR3PhysChangeMemBalloonRendezvous, (void *)paUser);
a3011b448b38c39a7222f2f1eb40c8349023f650vboxsync /* Made a copy in PGMR3PhysFreeRamPages; free it here. */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Inflate or deflate a memory balloon
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @returns VBox status code.
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync * @param pVM The VM handle.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param fInflate Inflate or deflate memory balloon
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param cPages Number of pages to free
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param paPhysPage Array of guest physical addresses
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsyncVMMR3DECL(int) PGMR3PhysChangeMemBalloon(PVM pVM, bool fInflate, unsigned cPages, RTGCPHYS *paPhysPage)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* We own the IOM lock here and could cause a deadlock by waiting for another VCPU that is blocking on the IOM lock.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * In the SMP case we post a request packet to postpone the job.
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync unsigned cbPhysPage = cPages * sizeof(paPhysPage[0]);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync RTGCPHYS *paPhysPageCopy = (RTGCPHYS *)RTMemAlloc(cbPhysPage);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync rc = VMR3ReqCallNoWait(pVM, VMCPUID_ANY_QUEUE, (PFNRT)pgmR3PhysChangeMemBalloonHelper, 4, pVM, fInflate, cPages, paPhysPageCopy);
a3011b448b38c39a7222f2f1eb40c8349023f650vboxsync rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, pgmR3PhysChangeMemBalloonRendezvous, (void *)paUser);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Query the VM and host balloon sizes
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync * @returns VBox status code.
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync * @param pVM The VM handle.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param puBalloonVM Pointer to VM balloon size
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync * @param puBalloonAllVMs Pointer to total balloon size of all VMs
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncVMMR3DECL(int) PGMR3QueryBalloonSize(PVM pVM, uint64_t *puBalloonVM, uint64_t *puBalloonAllVMs)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync rc = GMMR3QueryTotalBalloonSize(pVM, puBalloonAllVMs);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync * PGMR3PhysRegisterRam worker that initializes and links a RAM range.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pVM The VM handle.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pNew The new RAM range.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param GCPhys The address of the RAM range.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param GCPhysLast The last address of the RAM range.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param RCPtrNew The RC address if the range is floating. NIL_RTRCPTR
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * if in HMA.
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync * @param R0PtrNew Ditto for R0.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pszDesc The description.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @param pPrev The previous RAM range (for linking).
a3011b448b38c39a7222f2f1eb40c8349023f650vboxsyncstatic void pgmR3PhysInitAndLinkRamRange(PVM pVM, PPGMRAMRANGE pNew, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast,
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync RTRCPTR RCPtrNew, RTR0PTR R0PtrNew, const char *pszDesc, PPGMRAMRANGE pPrev)
a3011b448b38c39a7222f2f1eb40c8349023f650vboxsync * Initialize the range.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync pNew->pSelfR0 = R0PtrNew != NIL_RTR0PTR ? R0PtrNew : MMHyperCCToR0(pVM, pNew);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync pNew->pSelfRC = RCPtrNew != NIL_RTRCPTR ? RCPtrNew : MMHyperCCToRC(pVM, pNew);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync pNew->fFlags = RCPtrNew != NIL_RTRCPTR ? PGM_RAM_RANGE_FLAGS_FLOATING : 0;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync while (iPage-- > 0)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync PGM_PAGE_INIT_ZERO(&pNew->aPages[iPage], pVM, PGMPAGETYPE_RAM);
a3011b448b38c39a7222f2f1eb40c8349023f650vboxsync /* Update the page count stats. */
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync * Relocate a floating RAM range.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * @copydoc FNPGMRELOCATE.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncstatic DECLCALLBACK(bool) pgmR3PhysRamRangeRelocate(PVM pVM, RTGCPTR GCPtrOld, RTGCPTR GCPtrNew, PGMRELOCATECALL enmMode, void *pvUser)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync Assert(pRam->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING);
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync return true;
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync /* Update myself and then relink all the ranges. */
AssertFailedReturn(false);
? pszDesc
size_t const cChunkPages = RT_ALIGN_Z(RT_UOFFSETOF(PGMRAMRANGE, aPages[cRamPages]), PAGE_SIZE) >> PAGE_SHIFT;
#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
NULL,
#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
rc = PGMR3MapPT(pVM, GCPtrChunkMap, cbChunk, 0 /*fFlags*/, pgmR3PhysRamRangeRelocate, pNew, pszDescChunk);
return rc;
AssertMsgReturn(GCPhysLast > GCPhys, ("The range wraps! GCPhys=%RGp cb=%RGp\n", GCPhys, cb), VERR_INVALID_PARAMETER);
return rc;
while (cPagesLeft > 0)
rc = pgmR3PhysRegisterHighRamChunk(pVM, GCPhysChunk, cPagesInChunk, cbChunk, iChunk, pszDesc, &pPrev);
iChunk++;
pgmR3PhysInitAndLinkRamRange(pVM, pNew, GCPhys, GCPhysLast, NIL_RTRCPTR, NIL_RTR0PTR, pszDesc, pPrev);
return VINF_SUCCESS;
while (cLeft-- > 0)
case PGM_PAGE_STATE_ZERO:
LogRel(("PGM: RAM Pre-allocation failed at %RGp (in %s) with rc=%Rrc\n", GCPhys, pRam->pszDesc, rc));
return rc;
cPages++;
case PGM_PAGE_STATE_BALLOONED:
case PGM_PAGE_STATE_ALLOCATED:
case PGM_PAGE_STATE_SHARED:
pPage++;
return VINF_SUCCESS;
AssertMsg(((RTGCPHYS)iPage << PAGE_SHIFT) == pRam->cb, ("%RGp %RGp\n", (RTGCPHYS)iPage << PAGE_SHIFT, pRam->cb));
while (iPage-- > 0)
case PGMPAGETYPE_RAM:
void *pvPage;
rc = pgmPhysFreePage(pVM, pReq, &cPendingPages, pPage, pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT));
case PGMPAGETYPE_MMIO2:
case PGMPAGETYPE_ROM:
case PGMPAGETYPE_MMIO:
AssertFailed();
while (iPage-- > 0)
case PGMPAGETYPE_RAM:
case PGM_PAGE_STATE_ZERO:
case PGM_PAGE_STATE_BALLOONED:
case PGM_PAGE_STATE_SHARED:
case PGM_PAGE_STATE_ALLOCATED:
void *pvPage;
case PGMPAGETYPE_MMIO2:
case PGMPAGETYPE_ROM_SHADOW:
case PGMPAGETYPE_ROM:
case PGMPAGETYPE_MMIO:
AssertFailed();
if (cPendingPages)
return VINF_SUCCESS;
int rc;
bool fRamExists = false;
while (cLeft-- > 0)
pPage++;
fRamExists = true;
if (fRamExists)
Log(("PGMR3PhysMMIORegister: Adding ad hoc MMIO range for %RGp-%RGp %s\n", GCPhys, GCPhysLast, pszDesc));
rc = MMHyperAlloc(pVM, RT_OFFSETOF(PGMRAMRANGE, aPages[cPages]), 16, MM_TAG_PGM_PHYS, (void **)&pNew);
while (iPage-- > 0)
&& !fRamExists)
return rc;
bool fAllMMIO = true;
while (cLeft-- > 0)
fAllMMIO = false;
pPage++;
if (fAllMMIO)
* Range match? It will all be within one range (see PGMAllHandler.cpp).
while (cLeft--)
AssertMsg(PGM_PAGE_IS_MMIO(pPage), ("%RGp %R[pgmpage]\n", pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT), pPage));
AssertMsg(PGM_PAGE_IS_ZERO(pPage), ("%RGp %R[pgmpage]\n", pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT), pPage));
return rc;
return pCur;
return NULL;
VMMR3DECL(int) PGMR3PhysMMIO2Register(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion, RTGCPHYS cb, uint32_t fFlags, void **ppv, const char *pszDesc)
if (!pszDesc)
return VERR_NO_MEMORY;
void *pvPages;
while (iPage-- > 0)
return VINF_SUCCESS;
return rc;
unsigned cFound = 0;
while (pCur)
cFound++;
if (pPrev)
#ifdef VBOX_STRICT
bool fRamExists = false;
fRamExists = true;
if (fRamExists)
while (cPagesLeft-- > 0)
pPage++;
if (fRamExists)
while (cPagesLeft-- > 0)
pPageSrc++;
pPageDst++;
if (cPendingPages)
return VINF_SUCCESS;
bool fInformREM;
while (cPagesLeft-- > 0)
pPageDst++;
fInformREM = false;
fInformREM = true;
if (fInformREM)
return VINF_SUCCESS;
VMMR3DECL(int) PGMR3PhysMMIO2GetHCPhys(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion, RTGCPHYS off, PRTHCPHYS pHCPhys)
return VINF_SUCCESS;
VMMR3DECL(int) PGMR3PhysMMIO2MapKernel(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion, RTGCPHYS off, RTGCPHYS cb,
return rc;
Log(("PGMR3PhysRomRegister: pDevIns=%p GCPhys=%RGp(-%RGp) cb=%RGp pvBinary=%p fFlags=%#x pszDesc=%s\n",
AssertReturn(!(fFlags & ~(PGMPHYS_ROM_FLAGS_SHADOWED | PGMPHYS_ROM_FLAGS_PERMANENT_BINARY)), VERR_INVALID_PARAMETER);
bool fRamExists = false;
fRamExists = true;
if (fRamExists)
while (cPagesLeft-- > 0)
pPage++;
if (cExtraBaseCost)
return rc;
return rc;
rc = MMHyperAlloc(pVM, RT_OFFSETOF(PGMROMRANGE, aPages[cPages]), 0, MM_TAG_PGM_PHYS, (void **)&pRomNew);
if (!fRamExists)
rc = MMHyperAlloc(pVM, RT_OFFSETOF(PGMRAMRANGE, aPages[cPages]), sizeof(PGMPAGE), MM_TAG_PGM_PHYS, (void **)&pRamNew);
if (!fRamExists)
void *pvDstPage;
if (pRomPrev)
return VINF_SUCCESS;
if (!fRamExists)
return rc;
static DECLCALLBACK(int) pgmR3PhysRomWriteHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser)
Log5(("pgmR3PhysRomWriteHandler: %d %c %#08RGp %#04zx\n", pRomPage->enmProt, enmAccessType == PGMACCESSTYPE_READ ? 'R' : 'W', GCPhys, cbBuf));
return VINF_PGM_HANDLER_DO_DEFAULT;
return VINF_SUCCESS;
void *pvDstPage;
return rc;
rc = pgmPhysFreePage(pVM, pReq, &cPendingPages, &pRom->aPages[iPage].Shadow, pRom->GCPhys + (iPage << PAGE_SHIFT));
if (cPendingPages)
Assert(!PGM_PAGE_IS_ZERO(&pRom->aPages[iPage].Shadow) && !PGM_PAGE_IS_BALLOONED(&pRom->aPages[iPage].Shadow));
void *pvDstPage;
#ifdef VBOX_STRICT
void const *pvDstPage;
return VINF_SUCCESS;
if (!cb)
return VINF_SUCCESS;
bool fFlushTLB = false;
bool fChanges = false;
iPage++)
fChanges = true;
if (fChanges)
return rc2;
if (fFlushTLB)
return rc;
RTAvlU32DoWithAll(&pVM->pgm.s.ChunkR3Map.pTree, true /*fFromLeft*/, pgmR3PhysChunkAgeingRolloverCallback, pVM);
RTAvlU32DoWithAll(&pVM->pgm.s.ChunkR3Map.pTree, true /*fFromLeft*/, pgmR3PhysChunkAgeingCallback, pVM);
typedef struct PGMR3PHYSCHUNKUNMAPCB
if (pChunk)
if (pChunk)
/* next with the same age - this version of the AVL API doesn't enumerate the list, so we have to do it. */
} while (pNode);
if (RTAvllU32DoWithAll(&pVM->pgm.s.ChunkR3Map.pAgeTree, true /*fFromLeft*/, pgmR3PhysChunkUnmapCandidateCallback, pVM))
return INT32_MAX;
int rc;
#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)MMR3HeapAlloc(pVM, MM_TAG_PGM_CHUNK_MAPPING, sizeof(*pChunk));
PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)MMR3UkHeapAlloc(pVM, MM_TAG_PGM_CHUNK_MAPPING, sizeof(*pChunk), NULL);
PPGMCHUNKR3MAP pUnmappedChunk = (PPGMCHUNKR3MAP)RTAvlU32Remove(&pVM->pgm.s.ChunkR3Map.pTree, Req.idChunkUnmap);
#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
return rc;
int rc;
return rc;
void *pv;
idPage++;
return rc;
* in EM.cpp and shouldn't be propagated outside TRPM, HWACCM, EM and
AssertMsgReturn(iClear <= RT_ELEMENTS(pVM->pgm.s.aHandyPages), ("%d", iClear), VERR_INTERNAL_ERROR);
void *pvChunk;
void *pv;
AssertLogRelMsgBreak(RT_SUCCESS(rc), ("idPage=%#x HCPhysGCPhys=%RHp rc=%Rrc", pPage->idPage, pPage->HCPhysGCPhys, rc));
iClear++;
Log3(("PGMR3PhysAllocateHandyPages: idPage=%#x HCPhys=%RGp\n", pPage->idPage, pPage->HCPhysGCPhys));
pRam;
return rc;
static int pgmPhysFreePage(PVM pVM, PGMMFREEPAGESREQ pReq, uint32_t *pcPendingPages, PPGMPAGE pPage, RTGCPHYS GCPhys)
return VMSetError(pVM, VERR_PGM_PHYS_NOT_RAM, RT_SRC_POS, "GCPhys=%RGp type=%d", GCPhys, PGM_PAGE_GET_TYPE(pPage));
return VINF_SUCCESS;
return VMSetError(pVM, VERR_PGM_PHYS_INVALID_PAGE_ID, RT_SRC_POS, "GCPhys=%RGp idPage=%#x", GCPhys, pPage);
return VINF_SUCCESS;
*pcPendingPages = 0;
return rc;
/** @todo Handle TLB loads of virtual handlers so ./test.sh can be made to work
if (fWritable)
int rc2;
case PGM_PAGE_STATE_ALLOCATED:
case PGM_PAGE_STATE_BALLOONED:
AssertFailed();
case PGM_PAGE_STATE_ZERO:
case PGM_PAGE_STATE_SHARED:
Log6(("PGMR3PhysTlbGCPhys2Ptr: GCPhys=%RGp rc=%Rrc pPage=%R[pgmpage] *ppv=%p\n", GCPhys, rc, pPage, *ppv));
return rc;