PGMAllPhys.cpp revision 436b5c616e019c5e62053657c52d3ab5562ecbbf
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * PGM - Page Manager and Monitor, Physical Memory Addressing.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * available from http://www.virtualbox.org. This file is free software;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * you can redistribute it and/or modify it under the terms of the GNU
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * General Public License (GPL) as published by the Free Software
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * additional information or have any questions.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/*******************************************************************************
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync* Defined Constants And Macros *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync*******************************************************************************/
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/** @def PGM_IGNORE_RAM_FLAGS_RESERVED
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Don't respect the MM_RAM_FLAGS_RESERVED flag when converting to HC addresses.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Since this flag is currently incorrectly kept set for ROM regions we will
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * have to ignore it for now so we don't break stuff.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @todo this has been fixed now I believe, remove this hack.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync/*******************************************************************************
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync* Header Files *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync*******************************************************************************/
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * \#PF Handler callback for Guest ROM range write access.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * We simply ignore the writes or fall back to the recompiler if we don't support the instruction.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns VBox status code (appropritate for trap handling and GC return).
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM VM Handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param uErrorCode CPU Error code.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pRegFrame Trap register frame.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pvFault The fault address (cr2).
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhysFault The GC physical address corresponding to pvFault.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pvUser User argument. Pointer to the ROM range structure.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncVMMDECL(int) pgmPhysRomWriteHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, void *pvFault, RTGCPHYS GCPhysFault, void *pvUser)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * If it's a simple instruction which doesn't change the cpu state
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * we will simply skip it. Otherwise we'll have to defer it to REM.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = EMInterpretDisasOne(pVM, pRegFrame, &Cpu, &cbOp);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync && Cpu.mode == CPUMODE_32BIT /** @todo why does this matter? */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync && !(Cpu.prefix & (PREFIX_REPNE | PREFIX_REP | PREFIX_SEG)))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** @todo Find other instructions we can safely skip, possibly
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * adding this kind of detection to DIS or EM. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatRZGuestROMWriteHandled);
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync rc = PGMHandlerPhysicalPageTempOff(pVM, pRom->GCPhys, GCPhysFault & X86_PTE_PG_MASK);
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync break; /** @todo Must restart the instruction, not use the interpreter! */
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync /* Handle it in ring-3 because it's *way* easier there. */
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync AssertMsgFailedReturn(("enmProt=%d iPage=%d GCPhysFault=%RGp\n",
956230513f1e35e9d694c219c7009034b71bb836vboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatRZGuestROMWriteUnhandled);
956230513f1e35e9d694c219c7009034b71bb836vboxsync#endif /* IN_RING3 */
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Checks if Address Gate 20 is enabled or not.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @returns true if enabled.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @returns false if disabled.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param pVM VM handle.
956230513f1e35e9d694c219c7009034b71bb836vboxsync LogFlow(("PGMPhysIsA20Enabled %d\n", pVM->pgm.s.fA20Enabled));
956230513f1e35e9d694c219c7009034b71bb836vboxsync return !!pVM->pgm.s.fA20Enabled ; /* stupid MS compiler doesn't trust me. */
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Validates a GC physical address.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @returns true if valid.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @returns false if invalid.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param pVM The VM handle.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param GCPhys The physical address to validate.
956230513f1e35e9d694c219c7009034b71bb836vboxsyncVMMDECL(bool) PGMPhysIsGCPhysValid(PVM pVM, RTGCPHYS GCPhys)
956230513f1e35e9d694c219c7009034b71bb836vboxsync PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhys);
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Checks if a GC physical address is a normal page,
956230513f1e35e9d694c219c7009034b71bb836vboxsync * i.e. not ROM, MMIO or reserved.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns true if normal.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns false if invalid, ROM, MMIO or reserved page.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhys The physical address to check.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncVMMDECL(bool) PGMPhysIsGCPhysNormal(PVM pVM, RTGCPHYS GCPhys)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhys);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync && !(pPage->HCPhys & (MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO2));
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Converts a GC physical address to a HC physical address.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @returns VINF_SUCCESS on success.
7cd65b78ef52a960d2237ca56abb05385a12217cvboxsync * @returns VERR_PGM_PHYS_PAGE_RESERVED it it's a valid GC physical
7cd65b78ef52a960d2237ca56abb05385a12217cvboxsync * page but has no physical backing.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @returns VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid
956230513f1e35e9d694c219c7009034b71bb836vboxsync * GC physical address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhys The GC physical address to convert.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pHCPhys Where to store the HC physical address on success.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncVMMDECL(int) PGMPhysGCPhys2HCPhys(PVM pVM, RTGCPHYS GCPhys, PRTHCPHYS pHCPhys)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhys, &pPage);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_UNLIKELY(pPage->HCPhys & MM_RAM_FLAGS_RESERVED)) /** @todo PAGE FLAGS */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *pHCPhys = PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Invalidates the GC page mapping TLB.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncVMMDECL(void) PGMPhysInvalidatePageGCMapTLB(PVM pVM)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /* later */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Invalidates the ring-0 page mapping TLB.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncVMMDECL(void) PGMPhysInvalidatePageR0MapTLB(PVM pVM)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Invalidates the ring-3 page mapping TLB.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncVMMDECL(void) PGMPhysInvalidatePageR3MapTLB(PVM pVM)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.PhysTlbHC.aEntries); i++)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pVM->pgm.s.PhysTlbHC.aEntries[i].GCPhys = NIL_RTGCPHYS;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Frees the specified RAM page.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * This is used by ballooning and remapping MMIO2.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM Pointer to the shared VM structure.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pPage Pointer to the page structure.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param GCPhys The guest physical address of the page, if applicable.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncvoid pgmPhysFreePage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync AssertFatal(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** @todo implement this... */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Makes sure that there is at least one handy page ready for use.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * This will also take the appropriate actions when reaching water-marks.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns The following VBox status codes.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VINF_SUCCESS on success.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_EM_NO_MEMORY if we're really out of memory.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @remarks Must be called from within the PGM critical section. It may
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * nip back to ring-3/0 in some cases.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** @remarks
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * low-water mark logic for R0 & GC:
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * - 75%: Set FF.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * - 50%: Force return to ring-3 ASAP.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * For ring-3 there is a little problem wrt to the recompiler, so:
956230513f1e35e9d694c219c7009034b71bb836vboxsync * - 75%: Set FF.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * - 50%: Try allocate pages; on failure we'll force REM to quite ASAP.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * The basic idea is that we should be able to get out of any situation with
bd7c18002f48884a132bb0967408b6111dec326evboxsync * only 50% of handy pages remaining.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * At the moment we'll not adjust the number of handy pages relative to the
956230513f1e35e9d694c219c7009034b71bb836vboxsync * actual VM RAM committment, that's too much work for now.
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(pVM->pgm.s.cHandyPages <= RT_ELEMENTS(pVM->pgm.s.aHandyPages));
956230513f1e35e9d694c219c7009034b71bb836vboxsync || pVM->pgm.s.cHandyPages - 1 <= RT_ELEMENTS(pVM->pgm.s.aHandyPages) / 2 /* 50% */
bd7c18002f48884a132bb0967408b6111dec326evboxsync Log(("PGM: cHandyPages=%u out of %u -> allocate more\n", pVM->pgm.s.cHandyPages, RT_ELEMENTS(pVM->pgm.s.aHandyPages)));
956230513f1e35e9d694c219c7009034b71bb836vboxsync int rc = VMMR0CallHost(pVM, VMMCALLHOST_PGM_ALLOCATE_HANDY_PAGES, 0);
956230513f1e35e9d694c219c7009034b71bb836vboxsync int rc = VMMGCCallHost(pVM, VMMCALLHOST_PGM_ALLOCATE_HANDY_PAGES, 0);
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(VM_FF_ISSET(pVM, VM_FF_PGM_NEED_HANDY_PAGES));
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(pVM->pgm.s.cHandyPages <= RT_ELEMENTS(pVM->pgm.s.aHandyPages));
956230513f1e35e9d694c219c7009034b71bb836vboxsync else if (pVM->pgm.s.cHandyPages - 1 <= (RT_ELEMENTS(pVM->pgm.s.aHandyPages) / 4) * 3) /* 75% */
956230513f1e35e9d694c219c7009034b71bb836vboxsync if (pVM->pgm.s.cHandyPages - 1 <= RT_ELEMENTS(pVM->pgm.s.aHandyPages) / 2)
956230513f1e35e9d694c219c7009034b71bb836vboxsync Log(("PGM: VM_FF_TO_R3 - cHandyPages=%u out of %u\n", pVM->pgm.s.cHandyPages - 1, RT_ELEMENTS(pVM->pgm.s.aHandyPages)));
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Replace a zero or shared page with new page that we can write to.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @returns The following VBox status codes.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @retval VINF_SUCCESS on success, pPage is modified.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @retval VERR_EM_NO_MEMORY if we're totally out of memory.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @todo Propagate VERR_EM_NO_MEMORY up the call tree.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param pVM The VM address.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param pPage The physical page tracking structure. This will
956230513f1e35e9d694c219c7009034b71bb836vboxsync * be modified on success.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param GCPhys The address of the page.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @remarks Must be called from within the PGM critical section. It may
956230513f1e35e9d694c219c7009034b71bb836vboxsync * nip back to ring-3/0 in some cases.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @remarks This function shouldn't really fail, however if it does
956230513f1e35e9d694c219c7009034b71bb836vboxsync * it probably means we've screwed up the size of the amount
956230513f1e35e9d694c219c7009034b71bb836vboxsync * and/or the low-water mark of handy pages. Or, that some
956230513f1e35e9d694c219c7009034b71bb836vboxsync * device I/O is causing a lot of pages to be allocated while
956230513f1e35e9d694c219c7009034b71bb836vboxsync * while the host is in a low-memory condition.
956230513f1e35e9d694c219c7009034b71bb836vboxsyncint pgmPhysAllocPage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Ensure that we've got a page handy, take it and use it.
956230513f1e35e9d694c219c7009034b71bb836vboxsync AssertMsg(PGM_PAGE_IS_ZERO(pPage) || PGM_PAGE_IS_SHARED(pPage), ("%d %RGp\n", PGM_PAGE_GET_STATE(pPage), GCPhys));
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(iHandyPage < RT_ELEMENTS(pVM->pgm.s.aHandyPages));
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys != NIL_RTHCPHYS);
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(!(pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys & ~X86_PTE_PAE_PG_MASK));
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(pVM->pgm.s.aHandyPages[iHandyPage].idPage != NIL_GMM_PAGEID);
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(pVM->pgm.s.aHandyPages[iHandyPage].idSharedPage == NIL_GMM_PAGEID);
956230513f1e35e9d694c219c7009034b71bb836vboxsync * There are one or two action to be taken the next time we allocate handy pages:
956230513f1e35e9d694c219c7009034b71bb836vboxsync * - Tell the GMM (global memory manager) what the page is being used for.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * (Speeds up replacement operations - sharing and defragmenting.)
956230513f1e35e9d694c219c7009034b71bb836vboxsync * - If the current backing is shared, it must be freed.
956230513f1e35e9d694c219c7009034b71bb836vboxsync const RTHCPHYS HCPhys = pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys;
956230513f1e35e9d694c219c7009034b71bb836vboxsync pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys = GCPhys;
956230513f1e35e9d694c219c7009034b71bb836vboxsync pVM->pgm.s.aHandyPages[iHandyPage].idSharedPage = PGM_PAGE_GET_PAGEID(pPage);
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(PGM_PAGE_GET_PAGEID(pPage) != NIL_GMM_PAGEID);
956230513f1e35e9d694c219c7009034b71bb836vboxsync Log2(("PGM: Replaced shared page %#x at %RGp with %#x / %RHp\n", PGM_PAGE_GET_PAGEID(pPage),
956230513f1e35e9d694c219c7009034b71bb836vboxsync GCPhys, pVM->pgm.s.aHandyPages[iHandyPage].idPage, HCPhys));
956230513f1e35e9d694c219c7009034b71bb836vboxsync STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,PageReplaceShared));
956230513f1e35e9d694c219c7009034b71bb836vboxsync AssertMsgFailed(("TODO: copy shared page content")); /** @todo err.. what about copying the page content? */
956230513f1e35e9d694c219c7009034b71bb836vboxsync Log2(("PGM: Replaced zero page %RGp with %#x / %RHp\n", GCPhys, pVM->pgm.s.aHandyPages[iHandyPage].idPage, HCPhys));
956230513f1e35e9d694c219c7009034b71bb836vboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatRZPageReplaceZero);
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(pVM->pgm.s.aHandyPages[iHandyPage].idSharedPage == NIL_GMM_PAGEID);
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Do the PGMPAGE modifications.
956230513f1e35e9d694c219c7009034b71bb836vboxsync PGM_PAGE_SET_PAGEID(pPage, pVM->pgm.s.aHandyPages[iHandyPage].idPage);
956230513f1e35e9d694c219c7009034b71bb836vboxsync PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ALLOCATED);
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Deal with pages that are not writable, i.e. not in the ALLOCATED state.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @returns VBox status code.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @retval VINF_SUCCESS on success.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param pVM The VM address.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param pPage The physical page tracking structure.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param GCPhys The address of the page.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @remarks Called from within the PGM critical section.
956230513f1e35e9d694c219c7009034b71bb836vboxsyncint pgmPhysPageMakeWritable(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
956230513f1e35e9d694c219c7009034b71bb836vboxsync PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ALLOCATED);
956230513f1e35e9d694c219c7009034b71bb836vboxsync /* fall thru */
956230513f1e35e9d694c219c7009034b71bb836vboxsync default: /* to shut up GCC */
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Zero pages can be dummy pages for MMIO or reserved memory,
956230513f1e35e9d694c219c7009034b71bb836vboxsync * so we need to check the flags before joining cause with
956230513f1e35e9d694c219c7009034b71bb836vboxsync * shared page replacement.
956230513f1e35e9d694c219c7009034b71bb836vboxsync /* fall thru */
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Internal usage: Map the page specified by its GMM ID.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * This is similar to pgmPhysPageMap
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @returns VBox status code.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param pVM The VM handle.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param idPage The Page ID.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param HCPhys The physical address (for RC).
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param ppv Where to store the mapping address.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @remarks Called from within the PGM critical section.
956230513f1e35e9d694c219c7009034b71bb836vboxsyncint pgmPhysPageMapByPageID(PVM pVM, uint32_t idPage, RTHCPHYS HCPhys, void **ppv)
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Validation.
956230513f1e35e9d694c219c7009034b71bb836vboxsync AssertReturn(HCPhys && !(HCPhys & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
956230513f1e35e9d694c219c7009034b71bb836vboxsync const uint32_t idChunk = idPage >> GMM_CHUNKID_SHIFT;
956230513f1e35e9d694c219c7009034b71bb836vboxsync AssertReturn(idChunk != NIL_GMM_CHUNKID, VERR_INVALID_PARAMETER);
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Map it by HCPhys.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Map it by HCPhys.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Find/make Chunk TLB entry for the mapping chunk.
956230513f1e35e9d694c219c7009034b71bb836vboxsync PPGMCHUNKR3MAPTLBE pTlbe = &pVM->pgm.s.ChunkR3Map.Tlb.aEntries[PGM_CHUNKR3MAPTLB_IDX(idChunk)];
956230513f1e35e9d694c219c7009034b71bb836vboxsync STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,ChunkR3MapTlbHits));
956230513f1e35e9d694c219c7009034b71bb836vboxsync STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,ChunkR3MapTlbMisses));
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Find the chunk, map it if necessary.
956230513f1e35e9d694c219c7009034b71bb836vboxsync pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
956230513f1e35e9d694c219c7009034b71bb836vboxsync int rc = VMMR0CallHost(pVM, VMMCALLHOST_PGM_MAP_CHUNK, idChunk);
956230513f1e35e9d694c219c7009034b71bb836vboxsync pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Enter it into the Chunk TLB.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *ppv = (uint8_t *)pMap->pv + ((idPage &GMM_PAGEID_IDX_MASK) << PAGE_SHIFT);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Maps a page into the current virtual address space so it can be accessed.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns VBox status code.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VINF_SUCCESS on success.
db22a92f701bfcc17823963d6c8b745f68d44e30vboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pPage The physical page tracking structure.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhys The address of the page.
db22a92f701bfcc17823963d6c8b745f68d44e30vboxsync * @param ppMap Where to store the address of the mapping tracking structure.
db22a92f701bfcc17823963d6c8b745f68d44e30vboxsync * @param ppv Where to store the mapping address of the page. The page
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * offset is masked off!
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @remarks Called from within the PGM critical section.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncint pgmPhysPageMap(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, PPPGMPAGEMAP ppMap, void **ppv)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Just some sketchy GC/R0-darwin code.
956230513f1e35e9d694c219c7009034b71bb836vboxsync pgmR0DynMapHCPageInlined(&pVM->pgm.s, HCPhys, ppv);
956230513f1e35e9d694c219c7009034b71bb836vboxsync#else /* IN_RING3 || IN_RING0 */
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Special case: ZERO and MMIO2 pages.
956230513f1e35e9d694c219c7009034b71bb836vboxsync const uint32_t idChunk = PGM_PAGE_GET_CHUNKID(pPage);
956230513f1e35e9d694c219c7009034b71bb836vboxsync AssertMsgReturn(PGM_PAGE_GET_PAGEID(pPage) == NIL_GMM_PAGEID, ("pPage=%R[pgmpage]\n", pPage), VERR_INTERNAL_ERROR);
956230513f1e35e9d694c219c7009034b71bb836vboxsync /* Lookup the MMIO2 range and use pvR3 to calc the address. */
956230513f1e35e9d694c219c7009034b71bb836vboxsync PPGMRAMRANGE pRam = pgmPhysGetRange(&pVM->pgm.s, GCPhys);
956230513f1e35e9d694c219c7009034b71bb836vboxsync AssertMsgReturn(pRam || !pRam->pvR3, ("pRam=%p pPage=%R[pgmpage]\n", pRam, pPage), VERR_INTERNAL_ERROR);
956230513f1e35e9d694c219c7009034b71bb836vboxsync *ppv = (void *)((uintptr_t)pRam->pvR3 + (GCPhys - pRam->GCPhys));
956230513f1e35e9d694c219c7009034b71bb836vboxsync else if (PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO2_ALIAS_MMIO)
956230513f1e35e9d694c219c7009034b71bb836vboxsync /** @todo deal with aliased MMIO2 pages somehow...
956230513f1e35e9d694c219c7009034b71bb836vboxsync * One solution would be to seed MMIO2 pages to GMM and get unique Page IDs for
956230513f1e35e9d694c219c7009034b71bb836vboxsync * them, that would also avoid this mess. It would actually be kind of
956230513f1e35e9d694c219c7009034b71bb836vboxsync * elegant... */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** @todo handle MMIO2 */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync AssertMsgReturn(PGM_PAGE_IS_ZERO(pPage), ("pPage=%R[pgmpage]\n", pPage), VERR_INTERNAL_ERROR);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync AssertMsgReturn(PGM_PAGE_GET_HCPHYS(pPage) == pVM->pgm.s.HCPhysZeroPg,
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Find/make Chunk TLB entry for the mapping chunk.
956230513f1e35e9d694c219c7009034b71bb836vboxsync PPGMCHUNKR3MAPTLBE pTlbe = &pVM->pgm.s.ChunkR3Map.Tlb.aEntries[PGM_CHUNKR3MAPTLB_IDX(idChunk)];
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,ChunkR3MapTlbHits));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,ChunkR3MapTlbMisses));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Find the chunk, map it if necessary.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync int rc = VMMR0CallHost(pVM, VMMCALLHOST_PGM_MAP_CHUNK, idChunk);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Enter it into the Chunk TLB.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *ppv = (uint8_t *)pMap->pv + (PGM_PAGE_GET_PAGE_IN_CHUNK(pPage) << PAGE_SHIFT);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#endif /* IN_RING3 */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#if !defined(IN_RC) && !defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Load a guest page into the ring-3 physical TLB.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns VBox status code.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VINF_SUCCESS on success
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pPGM The PGM instance pointer.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhys The guest physical address in question.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncint pgmPhysPageLoadIntoTlb(PPGM pPGM, RTGCPHYS GCPhys)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync STAM_COUNTER_INC(&pPGM->CTX_MID_Z(Stat,PageMapTlbMisses));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Find the ram range.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * 99.8% of requests are expected to be in the first range.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Map the page.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Make a special case for the zero page as it is kind of special.
956230513f1e35e9d694c219c7009034b71bb836vboxsync PPGMPAGEMAPTLBE pTlbe = &pPGM->CTXSUFF(PhysTlb).aEntries[PGM_PAGEMAPTLB_IDX(GCPhys)];
956230513f1e35e9d694c219c7009034b71bb836vboxsync int rc = pgmPhysPageMap(PGM2VM(pPGM), pPage, GCPhys, &pMap, &pv);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync Assert(PGM_PAGE_GET_HCPHYS(pPage) == pPGM->HCPhysZeroPg);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Load a guest page into the ring-3 physical TLB.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns VBox status code.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VINF_SUCCESS on success
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pPGM The PGM instance pointer.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param pPage Pointer to the PGMPAGE structure corresponding to
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhys The guest physical address in question.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncint pgmPhysPageLoadIntoTlbWithPage(PPGM pPGM, PPGMPAGE pPage, RTGCPHYS GCPhys)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync STAM_COUNTER_INC(&pPGM->CTX_MID_Z(Stat,PageMapTlbMisses));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Map the page.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Make a special case for the zero page as it is kind of special.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PPGMPAGEMAPTLBE pTlbe = &pPGM->CTXSUFF(PhysTlb).aEntries[PGM_PAGEMAPTLB_IDX(GCPhys)];
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync int rc = pgmPhysPageMap(PGM2VM(pPGM), pPage, GCPhys, &pMap, &pv);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync Assert(PGM_PAGE_GET_HCPHYS(pPage) == pPGM->HCPhysZeroPg);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#endif /* !IN_RC && !VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Internal version of PGMPhysGCPhys2CCPtr that expects the caller to
956230513f1e35e9d694c219c7009034b71bb836vboxsync * own the PGM lock and therefore not need to lock the mapped page.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns VBox status code.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @retval VINF_SUCCESS on success.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhys The guest physical address of the page that should be mapped.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pPage Pointer to the PGMPAGE structure for the page.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param ppv Where to store the address corresponding to GCPhys.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @internal
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncint pgmPhysGCPhys2CCPtrInternal(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void **ppv)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync Assert(PDMCritSectIsOwner(&pVM->pgm.s.CritSect) || VM_IS_EMT(pVM));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Make sure the page is writable.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_UNLIKELY(PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Get the mapping address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *ppv = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = pgmPhysPageQueryTlbeWithPage(&pVM->pgm.s, pPage, GCPhys, &pTlbe);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *ppv = (void *)((uintptr_t)pTlbe->pv | (GCPhys & PAGE_OFFSET_MASK));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Internal version of PGMPhysGCPhys2CCPtrReadOnly that expects the caller to
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * own the PGM lock and therefore not need to lock the mapped page.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns VBox status code.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VINF_SUCCESS on success.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhys The guest physical address of the page that should be mapped.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pPage Pointer to the PGMPAGE structure for the page.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param ppv Where to store the address corresponding to GCPhys.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @internal
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncint pgmPhysGCPhys2CCPtrInternalReadOnly(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, const void **ppv)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync Assert(PDMCritSectIsOwner(&pVM->pgm.s.CritSect) || VM_IS_EMT(pVM));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Get the mapping address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *ppv = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK)); /** @todo add a read only flag? */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = pgmPhysPageQueryTlbeWithPage(&pVM->pgm.s, pPage, GCPhys, &pTlbe);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *ppv = (void *)((uintptr_t)pTlbe->pv | (GCPhys & PAGE_OFFSET_MASK));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Requests the mapping of a guest page into the current context.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * This API should only be used for very short term, as it will consume
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * scarse resources (R0 and GC) in the mapping cache. When you're done
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * This API will assume your intention is to write to the page, and will
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * therefore replace shared and zero pages. If you do not intend to modify
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * the page, use the PGMPhysGCPhys2CCPtrReadOnly() API.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns VBox status code.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VINF_SUCCESS on success.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhys The guest physical address of the page that should be mapped.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param ppv Where to store the address corresponding to GCPhys.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
8f827447008ecf94b4c101b6ac2b795b760d802dvboxsync * @remark Avoid calling this API from within critical sections (other than
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * the PGM one) because of the deadlock risk.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @thread Any thread.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncVMMDECL(int) PGMPhysGCPhys2CCPtr(PVM pVM, RTGCPHYS GCPhys, void **ppv, PPGMPAGEMAPLOCK pLock)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync# if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Find the page and make sure it's writable.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhys, &pPage);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_UNLIKELY(PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *ppv = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK)); /** @todo add a read only flag? */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Query the Physical TLB entry for the page (may fail).
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = pgmPhysPageQueryTlbe(&pVM->pgm.s, GCPhys, &pTlbe);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * If the page is shared, the zero page, or being write monitored
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * it must be converted to an page that's writable if possible.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_UNLIKELY(PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = pgmPhysPageQueryTlbeWithPage(&pVM->pgm.s, pPage, GCPhys, &pTlbe);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Now, just perform the locking and calculate the return address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#if 0 /** @Todo implement locking properly */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_LIKELY(pPage->cLocks != PGM_PAGE_MAX_LOCKS))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_UNLIKELY(++pPage->cLocks == PGM_PAGE_MAX_LOCKS))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync AssertMsgFailed(("%RGp is entering permanent locked state!\n", GCPhys));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pMap->cRefs++; /* Extra ref to prevent it from going away. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *ppv = (void *)((uintptr_t)pTlbe->pv | (GCPhys & PAGE_OFFSET_MASK));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#endif /* IN_RING3 || IN_RING0 */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Temporary fallback code.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync# if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/** @todo @bugref{3202}: check up this path. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return PGMPhysGCPhys2R3Ptr(pVM, GCPhys, 1, (PRTR3PTR)ppv);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Requests the mapping of a guest page into the current context.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * This API should only be used for very short term, as it will consume
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * scarse resources (R0 and GC) in the mapping cache. When you're done
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns VBox status code.
8f827447008ecf94b4c101b6ac2b795b760d802dvboxsync * @retval VINF_SUCCESS on success.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhys The guest physical address of the page that should be mapped.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param ppv Where to store the address corresponding to GCPhys.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @remark Avoid calling this API from within critical sections (other than
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * the PGM one) because of the deadlock risk.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @thread Any thread.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncVMMDECL(int) PGMPhysGCPhys2CCPtrReadOnly(PVM pVM, RTGCPHYS GCPhys, void const **ppv, PPGMPAGEMAPLOCK pLock)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** @todo implement this */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return PGMPhysGCPhys2CCPtr(pVM, GCPhys, (void **)ppv, pLock);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Requests the mapping of a guest page given by virtual address into the current context.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * This API should only be used for very short term, as it will consume
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * scarse resources (R0 and GC) in the mapping cache. When you're done
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * This API will assume your intention is to write to the page, and will
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * therefore replace shared and zero pages. If you do not intend to modify
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * the page, use the PGMPhysGCPtr2CCPtrReadOnly() API.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns VBox status code.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VINF_SUCCESS on success.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PAGE_TABLE_NOT_PRESENT if the page directory for the virtual address isn't present.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PAGE_NOT_PRESENT if the page at the virtual address isn't present.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhys The guest physical address of the page that should be mapped.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param ppv Where to store the address corresponding to GCPhys.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @remark Avoid calling this API from within critical sections (other than
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * the PGM one) because of the deadlock risk.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @thread EMT
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncVMMDECL(int) PGMPhysGCPtr2CCPtr(PVM pVM, RTGCPTR GCPtr, void **ppv, PPGMPAGEMAPLOCK pLock)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Requests the mapping of a guest page given by virtual address into the current context.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * This API should only be used for very short term, as it will consume
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * scarse resources (R0 and GC) in the mapping cache. When you're done
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @returns VBox status code.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @retval VINF_SUCCESS on success.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @retval VERR_PAGE_TABLE_NOT_PRESENT if the page directory for the virtual address isn't present.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @retval VERR_PAGE_NOT_PRESENT if the page at the virtual address isn't present.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @param pVM The VM handle.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @param GCPhys The guest physical address of the page that should be mapped.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @param ppv Where to store the address corresponding to GCPhys.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @remark Avoid calling this API from within critical sections (other than
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * the PGM one) because of the deadlock risk.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @thread EMT
35d0bfb0c568c691325cade7618555e3d3df918avboxsyncVMMDECL(int) PGMPhysGCPtr2CCPtrReadOnly(PVM pVM, RTGCPTR GCPtr, void const **ppv, PPGMPAGEMAPLOCK pLock)
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys, ppv, pLock);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * Release the mapping of a guest page.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * This is the counter part of PGMPhysGCPhys2CCPtr, PGMPhysGCPhys2CCPtrReadOnly
d012a89a724ba60c4fb5e74ce51f8b404fda4a8cvboxsync * PGMPhysGCPtr2CCPtr and PGMPhysGCPtr2CCPtrReadOnly.
d012a89a724ba60c4fb5e74ce51f8b404fda4a8cvboxsync * @param pVM The VM handle.
d012a89a724ba60c4fb5e74ce51f8b404fda4a8cvboxsync * @param pLock The lock structure initialized by the mapping function.
d012a89a724ba60c4fb5e74ce51f8b404fda4a8cvboxsyncVMMDECL(void) PGMPhysReleasePageMappingLock(PVM pVM, PPGMPAGEMAPLOCK pLock)
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
d012a89a724ba60c4fb5e74ce51f8b404fda4a8cvboxsync /* currently nothing to do here. */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#else /* IN_RING3 */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync /* The ZERO page and MMIO2 ends up here. */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync# if 0 /** @todo implement page locking */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#endif /* IN_RING3 */
d012a89a724ba60c4fb5e74ce51f8b404fda4a8cvboxsync * Converts a GC physical address to a HC ring-3 pointer.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @returns VINF_SUCCESS on success.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @returns VERR_PGM_PHYS_PAGE_RESERVED it it's a valid GC physical
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * page but has no physical backing.
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * @returns VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * GC physical address.
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * @returns VERR_PGM_GCPHYS_RANGE_CROSSES_BOUNDARY if the range crosses
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * a dynamic ram chunk boundary
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * @param pVM The VM handle.
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * @param GCPhys The GC physical address to convert.
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * @param cbRange Physical range
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param pR3Ptr Where to store the R3 pointer on success.
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsyncVMMDECL(int) PGMPhysGCPhys2R3Ptr(PVM pVM, RTGCPHYS GCPhys, RTUINT cbRange, PRTR3PTR pR3Ptr)
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync VM_ASSERT_EMT(pVM); /* no longer safe for use outside the EMT thread! */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync LogAlways(("PGMPhysGCPhys2R3Ptr(,%RGp,%#x,): dont use this API!\n", GCPhys, cbRange)); /** @todo eliminate this API! */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync# if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync int rc = pgmPhysGetPageAndRangeEx(&pVM->pgm.s, GCPhys, &pPage, &pRam);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pv);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#else /* !VBOX_WITH_NEW_PHYS_CODE */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync if ((GCPhys & PGM_DYNAMIC_CHUNK_BASE_MASK) != ((GCPhys+cbRange-1) & PGM_DYNAMIC_CHUNK_BASE_MASK))
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync AssertMsgFailed(("%RGp - %RGp crosses a chunk boundary!!\n", GCPhys, GCPhys+cbRange));
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync LogRel(("PGMPhysGCPhys2HCPtr %RGp - %RGp crosses a chunk boundary!!\n", GCPhys, GCPhys+cbRange));
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync int rc = pgmPhysGetPageAndRangeEx(&pVM->pgm.s, GCPhys, &pPage, &pRam);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync AssertMsgFailed(("%RGp - %RGp crosses a chunk boundary!!\n", GCPhys, GCPhys + cbRange));
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync unsigned iChunk = (off >> PGM_DYNAMIC_CHUNK_SHIFT);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0) /* ASSUMES this is a rare occurence */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync PRTR3UINTPTR paChunkR3Ptrs = (PRTR3UINTPTR)MMHyperR3ToCC(pVM, pRam->paChunkR3Ptrs);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync *pR3Ptr = (RTR3PTR)(paChunkR3Ptrs[iChunk] + (off & PGM_DYNAMIC_CHUNK_OFFSET_MASK));
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync *pR3Ptr = (RTR3PTR)(pRam->paChunkR3Ptrs[iChunk] + (off & PGM_DYNAMIC_CHUNK_OFFSET_MASK));
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync *pR3Ptr = (RTR3PTR)((RTR3UINTPTR)pRam->pvR3 + off);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#endif /* !VBOX_WITH_NEW_PHYS_CODE */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * PGMPhysGCPhys2R3Ptr convenience for use with assertions.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @returns The R3Ptr, NIL_RTR3PTR on failure.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param pVM The VM handle.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param GCPhys The GC Physical addresss.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param cbRange Physical range.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsyncVMMDECL(RTR3PTR) PGMPhysGCPhys2R3PtrAssert(PVM pVM, RTGCPHYS GCPhys, RTUINT cbRange)
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync int rc = PGMPhysGCPhys2R3Ptr(pVM, GCPhys, cbRange, &R3Ptr);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * Converts a guest pointer to a GC physical address.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * This uses the current CR3/CR0/CR4 of the guest.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @returns VBox status code.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param pVM The VM Handle
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param GCPtr The guest pointer to convert.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param pGCPhys Where to store the GC physical address.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsyncVMMDECL(int) PGMPhysGCPtr2GCPhys(PVM pVM, RTGCPTR GCPtr, PRTGCPHYS pGCPhys)
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync int rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtr, NULL, pGCPhys);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * Converts a guest pointer to a HC physical address.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * This uses the current CR3/CR0/CR4 of the guest.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @returns VBox status code.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param pVM The VM Handle
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param GCPtr The guest pointer to convert.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param pHCPhys Where to store the HC physical address.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsyncVMMDECL(int) PGMPhysGCPtr2HCPhys(PVM pVM, RTGCPTR GCPtr, PRTHCPHYS pHCPhys)
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync int rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtr, NULL, &GCPhys);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync rc = PGMPhysGCPhys2HCPhys(pVM, GCPhys | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK), pHCPhys);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * Converts a guest pointer to a R3 pointer.
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync * This uses the current CR3/CR0/CR4 of the guest.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @returns VBox status code.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param pVM The VM Handle
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param GCPtr The guest pointer to convert.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param pR3Ptr Where to store the R3 virtual address.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsyncVMMDECL(int) PGMPhysGCPtr2R3Ptr(PVM pVM, RTGCPTR GCPtr, PRTR3PTR pR3Ptr)
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync VM_ASSERT_EMT(pVM); /* no longer safe for use outside the EMT thread! */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync int rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtr, NULL, &GCPhys);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync rc = PGMPhysGCPhys2R3Ptr(pVM, GCPhys | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK), 1 /* we always stay within one page */, pR3Ptr);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * Converts a guest virtual address to a HC ring-3 pointer by specfied CR3 and
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @returns VBox status code.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM Handle
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPtr The guest pointer to convert.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param cr3 The guest CR3.
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * @param fFlags Flags used for interpreting the PD correctly: X86_CR4_PSE and X86_CR4_PAE
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * @param pR3Ptr Where to store the R3 pointer.
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * @remark This function is used by the REM at a time where PGM could
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * potentially not be in sync. It could also be used by a
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * future DBGF API to cpu state independent conversions.
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsyncVMMDECL(int) PGMPhysGCPtr2R3PtrByGstCR3(PVM pVM, RTGCPTR GCPtr, uint64_t cr3, unsigned fFlags, PRTR3PTR pR3Ptr)
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync VM_ASSERT_EMT(pVM); /* no longer safe for use outside the EMT thread! */
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * PAE or 32-bit?
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync rc = PGM_GCPHYS_2_PTR(pVM, cr3 & X86_CR3_PAGE_MASK, &pPD);
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync X86PDE Pde = pPD->a[(RTGCUINTPTR)GCPtr >> X86_PD_SHIFT];
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync { /* (big page) */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = PGMPhysGCPhys2R3Ptr(pVM, pgmGstGet4MBPhysPage(&pVM->pgm.s, Pde) | ((RTGCUINTPTR)GCPtr & X86_PAGE_4M_OFFSET_MASK),
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync { /* (normal page) */
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync rc = PGM_GCPHYS_2_PTR(pVM, Pde.u & X86_PDE_PG_MASK, &pPT);
956230513f1e35e9d694c219c7009034b71bb836vboxsync X86PTE Pte = pPT->a[((RTGCUINTPTR)GCPtr >> X86_PT_SHIFT) & X86_PT_MASK];
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync return PGMPhysGCPhys2R3Ptr(pVM, (Pte.u & X86_PTE_PG_MASK) | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK),
rc = PGMPhysGCPhys2R3Ptr(pVM, (Pde.u & X86_PDE2M_PAE_PG_MASK) | ((RTGCUINTPTR)GCPtr & X86_PAGE_2M_OFFSET_MASK),
return PGMPhysGCPhys2R3Ptr(pVM, (Pte.u & X86_PTE_PAE_PG_MASK) | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK),
return rc;
#ifdef IN_RING3
#ifdef VBOX_WITH_NEW_PHYS_CODE
static void pgmPhysReadHandler(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void *pvBuf, size_t cbRead)
AssertFailed();
const void *pvSrc;
AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternalReadOnly failed on %RGp / %R[pgmpage] -> %Rrc\n",
if (!pRam)
#ifdef IN_RING3
bool fGrabbedLock = false;
if (cbRead == 0)
#ifdef IN_RING3
fGrabbedLock = true;
goto l_End;
PPGMPHYSHANDLER pNode = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.pTreesR3->PhysHandlers, GCPhys);
/* Note! Dangerous assumption that HC handlers don't do anything that really requires an EMT lock! */
void *pvSrc = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
goto l_End;
goto l_End;
unsigned iPage;
/* Note! Dangerous assumption that HC handlers don't do anything that really requires an EMT lock! */
void *pvSrc = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
goto l_End;
goto l_End;
switch (pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_ROM)) /** @todo PAGE FLAGS */
case MM_RAM_FLAGS_ROM:
void *pvSrc = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
goto l_End;
case MM_RAM_FLAGS_RESERVED:
goto l_End;
goto l_End;
AssertReleaseMsgFailed(("Unknown read at %RGp size %u implement the complex physical reading case %RHp\n",
pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_ROM))); /** @todo PAGE FLAGS */
if ( !pRam
goto l_End;
#ifdef IN_RING3
if (fGrabbedLock)
#ifdef VBOX_WITH_NEW_PHYS_CODE
static void pgmPhysWriteHandler(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void const *pvBuf, size_t cbWrite)
AssertFailed();
void *pvDst;
if (!pRam)
#ifdef IN_RING3
bool fGrabbedLock = false;
if (cbWrite == 0)
#ifdef IN_RING3
fGrabbedLock = true;
int rc;
#ifdef IN_RING3
if (fGrabbedLock)
PGMPhysWrite(pVM, GCPhys, pvBuf, cbWrite); /* try again; can't assume pRam is still valid (paranoia) */
goto l_End;
PPGMPHYSHANDLER pPhysNode = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.pTreesR3->PhysHandlers, GCPhys);
/* Note! Dangerous assumption that R3 handlers don't do anything that really requires an EMT lock! */
rc = pPhysNode->pfnHandlerR3(pVM, GCPhys, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, pPhysNode->pvUserR3);
unsigned iPage;
/* Note! Dangerous assumption that R3 handlers don't do anything that really requires an EMT lock! */
rc2 = pVirtNode->pfnHandlerR3(pVM, (RTGCPTR)GCPtr, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, 0);
void *pvDst = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
goto l_End;
goto l_End;
#ifdef IN_RING3
unsigned iPage;
/* Note! Dangerous assumption that R3 handlers don't do anything that really requires an EMT lock! */
void *pvDst = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
goto l_End;
goto l_End;
PPGMPHYSHANDLER pNode = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.pTreesR3->PhysHandlers, GCPhys);
/** @todo Dangerous assumption that HC handlers don't do anything that really requires an EMT lock! */
rc = pNode->pfnHandlerR3(pVM, GCPhys, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, pNode->pvUserR3);
void *pvDst = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
goto l_End;
goto l_End;
switch (pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2)) /** @todo PAGE FLAGS */
case MM_RAM_FLAGS_MMIO2:
void *pvDst = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
goto l_End;
case MM_RAM_FLAGS_RESERVED:
goto l_End;
goto l_End;
AssertReleaseMsgFailed(("Unknown write at %RGp size %u implement the complex physical writing case %RHp\n",
(pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2)))); /** @todo PAGE FLAGS */
if ( !pRam
goto l_End;
#ifdef IN_RING3
if (fGrabbedLock)
if (!cb)
return VINF_SUCCESS;
void const *pvSrc;
return rc;
return VINF_SUCCESS;
return rc;
return VINF_SUCCESS;
if (!cb)
return VINF_SUCCESS;
void *pvDst;
return rc;
return VINF_SUCCESS;
return rc;
return VINF_SUCCESS;
if (!cb)
return VINF_SUCCESS;
void const *pvSrc;
return rc;
return VINF_SUCCESS;
return rc;
return VINF_SUCCESS;
if (!cb)
return VINF_SUCCESS;
void *pvDst;
return rc;
return VINF_SUCCESS;
return rc;
return VINF_SUCCESS;
if (!cb)
return VINF_SUCCESS;
void *pvDst;
return rc;
rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
return VINF_SUCCESS;
rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
return rc;
rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
return VINF_SUCCESS;
rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
int rc;
if (!cb)
return VINF_SUCCESS;
return VINF_SUCCESS;
return VINF_SUCCESS;
int rc;
if (!cb)
return VINF_SUCCESS;
return VINF_SUCCESS;
return VINF_SUCCESS;
VMMDECL(int) PGMPhysInterpretedRead(PVM pVM, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCUINTPTR GCPtrSrc, size_t cb)
int rc;
void *pvSrc;
switch (rc)
case VINF_SUCCESS:
Log(("PGMPhysInterpretedRead: pvDst=%p pvSrc=%p cb=%d\n", pvDst, (uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb));
return rc;
return VINF_SUCCESS;
void *pvSrc1;
switch (rc)
case VINF_SUCCESS:
return rc;
void *pvSrc2;
switch (rc)
case VINF_SUCCESS:
return rc;
return VINF_SUCCESS;
switch (rc)
case VINF_SUCCESS:
case VERR_PAGE_NOT_PRESENT:
return rc;