PGMPhys.cpp revision 30868e719f5a45ec4689ecb2616767cb1fd02c28
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * PGM - Page Manager and Monitor, Physical Memory Addressing.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * available from http://www.virtualbox.org. This file is free software;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * additional information or have any questions.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync/*******************************************************************************
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync* Header Files *
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync*******************************************************************************/
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync/*******************************************************************************
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync* Defined Constants And Macros *
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync*******************************************************************************/
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync/** The number of pages to free in one batch. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync/*******************************************************************************
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync* Internal Functions *
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync*******************************************************************************/
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic DECLCALLBACK(int) pgmR3PhysRomWriteHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic int pgmPhysFreePage(PVM pVM, PGMMFREEPAGESREQ pReq, uint32_t *pcPendingPages, PPGMPAGE pPage, RTGCPHYS GCPhys);
17c6e5e8177d068d1bc6af875d1610718efcfdb4vboxsync * PGMR3PhysReadU8-64
17c6e5e8177d068d1bc6af875d1610718efcfdb4vboxsync * PGMR3PhysWriteU8-64
2a08e12d5dcc1bb5057a9620e87ad361d41a1c1fvboxsync * EMT worker for PGMR3PhysReadExternal.
6c83eb6b98d1dd1b1d9795c16801ee2f53d2cc31vboxsyncstatic DECLCALLBACK(int) pgmR3PhysReadExternalEMT(PVM pVM, PRTGCPHYS pGCPhys, void *pvBuf, size_t cbRead)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Write to physical memory, external users.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_SUCCESS.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM VM Handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param GCPhys Physical address to write to.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pvBuf What to write.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param cbWrite How many bytes to write.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @thread Any but EMTs.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncVMMR3DECL(int) PGMR3PhysReadExternal(PVM pVM, RTGCPHYS GCPhys, void *pvBuf, size_t cbRead)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertMsgReturn(cbRead > 0, ("don't even think about reading zero bytes!\n"), VINF_SUCCESS);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("PGMR3PhysReadExternal: %RGp %d\n", GCPhys, cbRead));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Copy loop on ram ranges.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Find range. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Inside range or not? */
5eca6b757429b1f1d768e16fba65c485af34319dvboxsync * Must work our way thru this page by page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * If the page has an ALL access handler, we'll have to
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * delegate the job to EMT.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)pgmR3PhysReadExternalEMT, 4,
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Simple stuff, go ahead.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync const void *pvSrc;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = pgmPhysGCPhys2CCPtrInternalReadOnly(pVM, pPage, pRam->GCPhys + off, &pvSrc);
6565c2fb4f10f47bcb5436630d2dc35e5a4f28e2vboxsync AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternalReadOnly failed on %RGp / %R[pgmpage] -> %Rrc\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* next page */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync } /* walk pages in ram range. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("PGMPhysRead: Unassigned %RGp size=%u\n", GCPhys, cbRead));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Unassigned address space.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync } /* Ram range walk */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * EMT worker for PGMR3PhysWriteExternal.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncstatic DECLCALLBACK(int) pgmR3PhysWriteExternalEMT(PVM pVM, PRTGCPHYS pGCPhys, const void *pvBuf, size_t cbWrite)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /** @todo VERR_EM_NO_MEMORY */
38b70b2dcb1783801f7580cba797a0c8af4b5326vboxsync * Write to physical memory, external users.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_SUCCESS.
38b70b2dcb1783801f7580cba797a0c8af4b5326vboxsync * @retval VERR_EM_NO_MEMORY.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM VM Handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param GCPhys Physical address to write to.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pvBuf What to write.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param cbWrite How many bytes to write.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pszWho Who is writing. For tracking down who is writing
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * after we've saved the state.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @thread Any but EMTs.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncVMMDECL(int) PGMR3PhysWriteExternal(PVM pVM, RTGCPHYS GCPhys, const void *pvBuf, size_t cbWrite, const char *pszWho)
dee2201f96a012bfb966c8de4ab006c2c90a0eefvboxsync ("Calling PGMR3PhysWriteExternal after pgmR3Save()! GCPhys=%RGp cbWrite=%#x pszWho=%s\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertMsgReturn(cbWrite > 0, ("don't even think about writing zero bytes!\n"), VINF_SUCCESS);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("PGMR3PhysWriteExternal: %RGp %d\n", GCPhys, cbWrite));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Copy loop on ram ranges, stop when we hit something difficult.
b1c3cdef473df2fbc621d5da81acc82dbfb8a11avboxsync PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
fa92c704624def98d3c4aca86d65182effb98e04vboxsync /* Find range. */
fa92c704624def98d3c4aca86d65182effb98e04vboxsync /* Inside range or not? */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Must work our way thru this page by page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Is the page problematic, we have to do the work on the EMT.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Allocating writable pages and access handlers are
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * problematic, write monitored pages are simple and can be
fa92c704624def98d3c4aca86d65182effb98e04vboxsync * dealth with here.
fa92c704624def98d3c4aca86d65182effb98e04vboxsync || PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED)
17c6e5e8177d068d1bc6af875d1610718efcfdb4vboxsync if ( PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_WRITE_MONITORED
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)pgmR3PhysWriteExternalEMT, 4,
dee2201f96a012bfb966c8de4ab006c2c90a0eefvboxsync * Simple stuff, go ahead.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, pRam->GCPhys + off, &pvDst);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
dee2201f96a012bfb966c8de4ab006c2c90a0eefvboxsync /* next page */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync } /* walk pages in ram range */
5eca6b757429b1f1d768e16fba65c485af34319dvboxsync * Unassigned address space, skip it.
5eca6b757429b1f1d768e16fba65c485af34319dvboxsync } /* Ram range walk */
5eca6b757429b1f1d768e16fba65c485af34319dvboxsync * VMR3ReqCall worker for PGMR3PhysGCPhys2CCPtrExternal to make pages writable.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns see PGMR3PhysGCPhys2CCPtrExternal
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pGCPhys Pointer to the guest physical address.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param ppv Where to store the mapping address.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pLock Where to store the lock.
ada08ea58e7613c10d4c40669fd4fb955324bfdfvboxsyncstatic DECLCALLBACK(int) pgmR3PhysGCPhys2CCPtrDelegated(PVM pVM, PRTGCPHYS pGCPhys, void **ppv, PPGMPAGEMAPLOCK pLock)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Just hand it to PGMPhysGCPhys2CCPtr and check that it's not a page with
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * an access handler after it succeeds.
2a08e12d5dcc1bb5057a9620e87ad361d41a1c1fvboxsync rc = PGMPhysGCPhys2CCPtr(pVM, *pGCPhys, ppv, pLock);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc2 = pgmPhysPageQueryTlbe(&pVM->pgm.s, *pGCPhys, &pTlbe);
f20f327b65009074292a4b9ad44a02b6bfb2de8avboxsync /* We *must* flush any corresponding pgm pool page here, otherwise we'll
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * not be informed about writes and keep bogus gst->shw mappings around.
dee2201f96a012bfb966c8de4ab006c2c90a0eefvboxsync /** @todo r=bird: return VERR_PGM_PHYS_PAGE_RESERVED here if it still has
607961fb76ee6b24ca8d3b53ece9d70412f73b1bvboxsync * active handlers, see the PGMR3PhysGCPhys2CCPtrExternal docs. */
dee2201f96a012bfb966c8de4ab006c2c90a0eefvboxsync * Requests the mapping of a guest page into ring-3, external threads.
dee2201f96a012bfb966c8de4ab006c2c90a0eefvboxsync * When you're done with the page, call PGMPhysReleasePageMappingLock() ASAP to
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * release it.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This API will assume your intention is to write to the page, and will
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * therefore replace shared and zero pages. If you do not intend to modify the
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * page, use the PGMR3PhysGCPhys2CCPtrReadOnlyExternal() API.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code.
dee2201f96a012bfb966c8de4ab006c2c90a0eefvboxsync * @retval VINF_SUCCESS on success.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * backing or if the page has any active access handlers. The caller
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * must fall back on using PGMR3PhysWriteExternal.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param GCPhys The guest physical address of the page that should be mapped.
dee2201f96a012bfb966c8de4ab006c2c90a0eefvboxsync * @param ppv Where to store the address corresponding to GCPhys.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @remark Avoid calling this API from within critical sections (other than the
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * PGM one) because of the deadlock risk when we have to delegating the
f20f327b65009074292a4b9ad44a02b6bfb2de8avboxsync * task to an EMT.
10eaaac806009b8336cc5d746fe5072f6c9f58c0vboxsync * @thread Any.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncVMMR3DECL(int) PGMR3PhysGCPhys2CCPtrExternal(PVM pVM, RTGCPHYS GCPhys, void **ppv, PPGMPAGEMAPLOCK pLock)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Query the Physical TLB entry for the page (may fail).
dee2201f96a012bfb966c8de4ab006c2c90a0eefvboxsync rc = pgmPhysPageQueryTlbe(&pVM->pgm.s, GCPhys, &pTlbe);
c50100d1513854735d4e3593b3b385c007f6d8b6vboxsync * If the page is shared, the zero page, or being write monitored
c50100d1513854735d4e3593b3b385c007f6d8b6vboxsync * it must be converted to an page that's writable if possible.
b0d29fd0a868929a608ff72658aac997cc95319avboxsync * We can only deal with write monitored pages here, the rest have
c50100d1513854735d4e3593b3b385c007f6d8b6vboxsync * to be on an EMT.
b0d29fd0a868929a608ff72658aac997cc95319avboxsync || PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED
b0d29fd0a868929a608ff72658aac997cc95319avboxsync if ( PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_WRITE_MONITORED
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)pgmR3PhysGCPhys2CCPtrDelegated, 4,
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Now, just perform the locking and calculate the return address.
6565c2fb4f10f47bcb5436630d2dc35e5a4f28e2vboxsync else if (cLocks != PGM_PAGE_GET_WRITE_LOCKS(pPage))
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertMsgFailed(("%RGp / %R[pgmpage] is entering permanent write locked state!\n", GCPhys, pPage));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pMap->cRefs++; /* Extra ref to prevent it from going away. */
dee2201f96a012bfb966c8de4ab006c2c90a0eefvboxsync *ppv = (void *)((uintptr_t)pTlbe->pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pLock->uPageAndType = (uintptr_t)pPage | PGMPAGEMAPLOCK_TYPE_WRITE;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Requests the mapping of a guest page into ring-3, external threads.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * When you're done with the page, call PGMPhysReleasePageMappingLock() ASAP to
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * release it.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_SUCCESS on success.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * backing or if the page as an active ALL access handler. The caller
dee2201f96a012bfb966c8de4ab006c2c90a0eefvboxsync * must fall back on using PGMPhysRead.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param GCPhys The guest physical address of the page that should be mapped.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param ppv Where to store the address corresponding to GCPhys.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @remark Avoid calling this API from within critical sections (other than
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * the PGM one) because of the deadlock risk.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @thread Any.
dee2201f96a012bfb966c8de4ab006c2c90a0eefvboxsyncVMMR3DECL(int) PGMR3PhysGCPhys2CCPtrReadOnlyExternal(PVM pVM, RTGCPHYS GCPhys, void const **ppv, PPGMPAGEMAPLOCK pLock)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Query the Physical TLB entry for the page (may fail).
dee2201f96a012bfb966c8de4ab006c2c90a0eefvboxsync rc = pgmPhysPageQueryTlbe(&pVM->pgm.s, GCPhys, &pTlbe);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* MMIO pages doesn't have any readable backing. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Now, just perform the locking and calculate the return address.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertMsgFailed(("%RGp / %R[pgmpage] is entering permanent readonly locked state!\n", GCPhys, pPage));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pMap->cRefs++; /* Extra ref to prevent it from going away. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync *ppv = (void *)((uintptr_t)pTlbe->pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync pLock->uPageAndType = (uintptr_t)pPage | PGMPAGEMAPLOCK_TYPE_READ;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Relinks the RAM ranges using the pSelfRC and pSelfR0 pointers.
38b70b2dcb1783801f7580cba797a0c8af4b5326vboxsync * Called when anything was relocated.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM Pointer to the shared VM structure.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (pCur = pVM->pgm.s.pRamRangesR3; pCur; pCur = pCur->pNextR3)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert((pCur->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING) || pCur->pSelfR0 == MMHyperCCToR0(pVM, pCur));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert((pCur->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING) || pCur->pSelfRC == MMHyperCCToRC(pVM, pCur));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert((pCur->GCPhysLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(pCur->cb == pCur->GCPhysLast - pCur->GCPhys + 1);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (PPGMRAMRANGE pCur2 = pVM->pgm.s.pRamRangesR3; pCur2; pCur2 = pCur2->pNextR3)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync || strcmp(pCur2->pszDesc, pCur->pszDesc)); /** @todo fix MMIO ranges!! */
if (pPrev)
if (pPrev)
static int pgmR3PhysFreePageRange(PVM pVM, PPGMRAMRANGE pRam, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast, uint8_t uType)
while (cPagesLeft-- > 0)
pPageDst++;
if (cPendingPages)
return rc;
static DECLCALLBACK(VBOXSTRICTRC) pgmR3PhysFreeRamPagesRendezvous(PVM pVM, PVMCPU pVCpu, void *pvUser)
return rc;
for (unsigned i = 0; i < cPages; i++)
Log(("PGMR3PhysFreePageRange: invalid physical page %RGp pPage->u3Type=%d\n", paPhysPage[i], (pPage) ? pPage->uTypeY : 0));
return rc;
if (cPendingPages)
return rc;
return rc;
int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, pgmR3PhysFreeRamPagesRendezvous, (void *)paUser);
return rc;
static void pgmR3PhysInitAndLinkRamRange(PVM pVM, PPGMRAMRANGE pNew, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast,
while (iPage-- > 0)
static DECLCALLBACK(bool) pgmR3PhysRamRangeRelocate(PVM pVM, RTGCPTR GCPtrOld, RTGCPTR GCPtrNew, PGMRELOCATECALL enmMode, void *pvUser)
switch (enmMode)
case PGMRELOCATECALL_SUGGEST:
case PGMRELOCATECALL_RELOCATE:
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_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:
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_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)
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_ZERO:
case PGM_PAGE_STATE_SHARED:
Log6(("PGMR3PhysTlbGCPhys2Ptr: GCPhys=%RGp rc=%Rrc pPage=%R[pgmpage] *ppv=%p\n", GCPhys, rc, pPage, *ppv));
return rc;