PGMAllPhys.cpp revision 6ed48beeb7f2ef925ae5bd091e1e475eae18f939
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * PGM - Page Manager and Monitor, Physical Memory Addressing.
3f1e0eea71cabeb90529e546f16eb7aee513fde9vboxsync * 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.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * additional information or have any questions.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync/** @def PGM_IGNORE_RAM_FLAGS_RESERVED
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Don't respect the MM_RAM_FLAGS_RESERVED flag when converting to HC addresses.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Since this flag is currently incorrectly kept set for ROM regions we will
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * have to ignore it for now so we don't break stuff.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @todo this has been fixed now I believe, remove this hack.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync/*******************************************************************************
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync* Header Files *
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync*******************************************************************************/
2634ec5cbf8f1fa0a968cd4664ead6df1fed730dvboxsync * \#PF Handler callback for Guest ROM range write access.
614cbe11a7e5588dc8d369e223174b1441a09359vboxsync * We simply ignore the writes or fall back to the recompiler if we don't support the instruction.
7666082b743c5e146a8cee6cc794ff4bc3fd0ffdvboxsync * @returns VBox status code (appropritate for trap handling and GC return).
7666082b743c5e146a8cee6cc794ff4bc3fd0ffdvboxsync * @param pVM VM Handle.
7666082b743c5e146a8cee6cc794ff4bc3fd0ffdvboxsync * @param uErrorCode CPU Error code.
7666082b743c5e146a8cee6cc794ff4bc3fd0ffdvboxsync * @param pRegFrame Trap register frame.
7666082b743c5e146a8cee6cc794ff4bc3fd0ffdvboxsync * @param pvFault The fault address (cr2).
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsync * @param GCPhysFault The GC physical address corresponding to pvFault.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pvUser User argument. Pointer to the ROM range structure.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncVMMDECL(int) pgmPhysRomWriteHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, void *pvFault, RTGCPHYS GCPhysFault, void *pvUser)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * If it's a simple instruction which doesn't change the cpu state
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * we will simply skip it. Otherwise we'll have to defer it to REM.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = EMInterpretDisasOne(pVM, pRegFrame, &Cpu, &cbOp);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync && Cpu.mode == CPUMODE_32BIT /* @todo why does this matter? */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync && !(Cpu.prefix & (PREFIX_REPNE | PREFIX_REP | PREFIX_SEG)))
f351fab06d1e1507b72f5c574df71193061fd393vboxsync /** @todo Find other instructions we can safely skip, possibly
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * adding this kind of detection to DIS or EM. */
b1c3cdef473df2fbc621d5da81acc82dbfb8a11avboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatGCGuestROMWriteHandled);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = PGMHandlerPhysicalPageTempOff(pVM, pRom->GCPhys, GCPhysFault & X86_PTE_PG_MASK);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Handle it in ring-3 because it's *way* easier there. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertMsgFailedReturn(("enmProt=%d iPage=%d GCPhysFault=%RGp\n",
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatGCGuestROMWriteUnhandled);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#endif /* IN_RING3 */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Checks if Address Gate 20 is enabled or not.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns true if enabled.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns false if disabled.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM VM handle.
aaeb2e2f6ed5b164f1dec9a16a7adeb84f64cf31vboxsync LogFlow(("PGMPhysIsA20Enabled %d\n", pVM->pgm.s.fA20Enabled));
223cf005b18af2c21352a70693ebaf0582f68ebcvboxsync return !!pVM->pgm.s.fA20Enabled ; /* stupid MS compiler doesn't trust me. */
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync * Validates a GC physical address.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns true if valid.
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync * @returns false if invalid.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
576d4214137bce409cdcf01e8df4a0bca5e0b2d1vboxsync * @param GCPhys The physical address to validate.
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsyncVMMDECL(bool) PGMPhysIsGCPhysValid(PVM pVM, RTGCPHYS GCPhys)
72ef2b9fc5ffc01d0dabd5052d6e8baa3a952773vboxsync PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhys);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Checks if a GC physical address is a normal page,
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * i.e. not ROM, MMIO or reserved.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns true if normal.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns false if invalid, ROM, MMIO or reserved page.
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync * @param pVM The VM handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param GCPhys The physical address to check.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncVMMDECL(bool) PGMPhysIsGCPhysNormal(PVM pVM, RTGCPHYS GCPhys)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhys);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync && !(pPage->HCPhys & (MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO2));
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * Converts a GC physical address to a HC physical address.
69deddbc68802f1cf1c3a404a9f816b8accb3385vboxsync * @returns VINF_SUCCESS on success.
3ecd8008b81f02a04220705ae0033142af363280vboxsync * @returns VERR_PGM_PHYS_PAGE_RESERVED it it's a valid GC physical
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * page but has no physical backing.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid
576d4214137bce409cdcf01e8df4a0bca5e0b2d1vboxsync * GC physical address.
2634ec5cbf8f1fa0a968cd4664ead6df1fed730dvboxsync * @param pVM The VM handle.
2634ec5cbf8f1fa0a968cd4664ead6df1fed730dvboxsync * @param GCPhys The GC physical address to convert.
2634ec5cbf8f1fa0a968cd4664ead6df1fed730dvboxsync * @param pHCPhys Where to store the HC physical address on success.
2634ec5cbf8f1fa0a968cd4664ead6df1fed730dvboxsyncVMMDECL(int) PGMPhysGCPhys2HCPhys(PVM pVM, RTGCPHYS GCPhys, PRTHCPHYS pHCPhys)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhys, &pPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (RT_UNLIKELY(pPage->HCPhys & MM_RAM_FLAGS_RESERVED)) /** @todo PAGE FLAGS */
b978e5849454446957177fd47ee98609ab0457a6vboxsync *pHCPhys = PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK);
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync * Invalidates the GC page mapping TLB.
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * @param pVM The VM handle.
b40179b44fea65b72b2f226f62af1ed7bd3c48fcvboxsyncVMMDECL(void) PGMPhysInvalidatePageGCMapTLB(PVM pVM)
b40179b44fea65b72b2f226f62af1ed7bd3c48fcvboxsync /* later */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Invalidates the ring-0 page mapping TLB.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
533ffcb943c4af2c5fe6385d816d0ba3eda9383bvboxsyncVMMDECL(void) PGMPhysInvalidatePageR0MapTLB(PVM pVM)
2634ec5cbf8f1fa0a968cd4664ead6df1fed730dvboxsync * Invalidates the ring-3 page mapping TLB.
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync * @param pVM The VM handle.
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsyncVMMDECL(void) PGMPhysInvalidatePageR3MapTLB(PVM pVM)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.PhysTlbHC.aEntries); i++)
82e90599291da476b2de7c8db33cfb0f2cbac774vboxsync pVM->pgm.s.PhysTlbHC.aEntries[i].GCPhys = NIL_RTGCPHYS;
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync * Frees the specified RAM page.
533ffcb943c4af2c5fe6385d816d0ba3eda9383bvboxsync * This is used by ballooning and remapping MMIO2.
533ffcb943c4af2c5fe6385d816d0ba3eda9383bvboxsync * @param pVM Pointer to the shared VM structure.
533ffcb943c4af2c5fe6385d816d0ba3eda9383bvboxsync * @param pPage Pointer to the page structure.
9984bae4ef25be8afd95d184308adcfef6e2ec0dvboxsync * @param GCPhys The guest physical address of the page, if applicable.
e50404712a2b5234c42bdf9740bddab5729ba188vboxsyncvoid pgmPhysFreePage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
57399ab65e2825c324fb9dcb4642d4ae2c232509vboxsync AssertFatal(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM);
57399ab65e2825c324fb9dcb4642d4ae2c232509vboxsync /** @todo implement this... */
1843553dbdf4e46417158b4c6348c503adf10740vboxsync * Makes sure that there is at least one handy page ready for use.
1843553dbdf4e46417158b4c6348c503adf10740vboxsync * This will also take the appropriate actions when reaching water-marks.
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync * @returns The following VBox status codes.
1843553dbdf4e46417158b4c6348c503adf10740vboxsync * @retval VINF_SUCCESS on success.
1843553dbdf4e46417158b4c6348c503adf10740vboxsync * @retval VERR_EM_NO_MEMORY if we're really out of memory.
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * @param pVM The VM handle.
1843553dbdf4e46417158b4c6348c503adf10740vboxsync * @remarks Must be called from within the PGM critical section. It may
1843553dbdf4e46417158b4c6348c503adf10740vboxsync * nip back to ring-3/0 in some cases.
1843553dbdf4e46417158b4c6348c503adf10740vboxsync /** @remarks
9e4166cf5ed4940f506bc718ea6c89bf7ed252c8vboxsync * low-water mark logic for R0 & GC:
9e4166cf5ed4940f506bc718ea6c89bf7ed252c8vboxsync * - 75%: Set FF.
533ffcb943c4af2c5fe6385d816d0ba3eda9383bvboxsync * - 50%: Force return to ring-3 ASAP.
1843553dbdf4e46417158b4c6348c503adf10740vboxsync * For ring-3 there is a little problem wrt to the recompiler, so:
ebbb1f6c7e8bae363a4efda4b35b58c8467d24bcvboxsync * - 75%: Set FF.
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync * - 50%: Try allocate pages; on failure we'll force REM to quite ASAP.
1843553dbdf4e46417158b4c6348c503adf10740vboxsync * The basic idea is that we should be able to get out of any situation with
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * only 50% of handy pages remaining.
1843553dbdf4e46417158b4c6348c503adf10740vboxsync * At the moment we'll not adjust the number of handy pages relative to the
806d0b554daa555364af5f87bc96eccbe760db7avboxsync * actual VM RAM committment, that's too much work for now.
1843553dbdf4e46417158b4c6348c503adf10740vboxsync Assert(pVM->pgm.s.cHandyPages <= RT_ELEMENTS(pVM->pgm.s.aHandyPages));
1843553dbdf4e46417158b4c6348c503adf10740vboxsync || pVM->pgm.s.cHandyPages - 1 <= RT_ELEMENTS(pVM->pgm.s.aHandyPages) / 2 /* 50% */
1843553dbdf4e46417158b4c6348c503adf10740vboxsync Log(("PGM: cHandyPages=%u out of %u -> allocate more\n", pVM->pgm.s.cHandyPages - 1 <= RT_ELEMENTS(pVM->pgm.s.aHandyPages)));
4b8fcf8e4d11536879a1f6e66279ba0c363a7eaevboxsync int rc = SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES, 0, NULL);
ebbb1f6c7e8bae363a4efda4b35b58c8467d24bcvboxsync /** @todo call PGMR0PhysAllocateHandyPages directly - need to make sure we can call kernel code first and deal with the seeding fallback. */
533ffcb943c4af2c5fe6385d816d0ba3eda9383bvboxsync int rc = VMMR0CallHost(pVM, VMMCALLHOST_PGM_ALLOCATE_HANDY_PAGES, 0);
533ffcb943c4af2c5fe6385d816d0ba3eda9383bvboxsync int rc = VMMGCCallHost(pVM, VMMCALLHOST_PGM_ALLOCATE_HANDY_PAGES, 0);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Assert(VM_FF_ISSET(pVM, VM_FF_PGM_NEED_HANDY_PAGES));
2634ec5cbf8f1fa0a968cd4664ead6df1fed730dvboxsync Assert(pVM->pgm.s.cHandyPages <= RT_ELEMENTS(pVM->pgm.s.aHandyPages));
82e90599291da476b2de7c8db33cfb0f2cbac774vboxsync else if (pVM->pgm.s.cHandyPages - 1 <= (RT_ELEMENTS(pVM->pgm.s.aHandyPages) / 4) * 3) /* 75% */
0e77737b0ba913683e614db11463b31ca67aacbevboxsync if (pVM->pgm.s.cHandyPages - 1 <= RT_ELEMENTS(pVM->pgm.s.aHandyPages) / 2)
9e4166cf5ed4940f506bc718ea6c89bf7ed252c8vboxsync Log(("PGM: VM_FF_TO_R3 - cHandyPages=%u out of %u\n", pVM->pgm.s.cHandyPages - 1 <= RT_ELEMENTS(pVM->pgm.s.aHandyPages)));
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * Replace a zero or shared page with new page that we can write to.
b74ca013e5f201a2dd371e6c438433ceac12af30vboxsync * @returns The following VBox status codes.
b40179b44fea65b72b2f226f62af1ed7bd3c48fcvboxsync * @retval VINF_SUCCESS on success, pPage is modified.
2d53f6e472561965d363674e17f48d3bdffc24d3vboxsync * @retval VERR_EM_NO_MEMORY if we're totally out of memory.
b40179b44fea65b72b2f226f62af1ed7bd3c48fcvboxsync * @todo Propagate VERR_EM_NO_MEMORY up the call tree.
2634ec5cbf8f1fa0a968cd4664ead6df1fed730dvboxsync * @param pVM The VM address.
2d53f6e472561965d363674e17f48d3bdffc24d3vboxsync * @param pPage The physical page tracking structure. This will
0e77737b0ba913683e614db11463b31ca67aacbevboxsync * be modified on success.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param GCPhys The address of the page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @remarks Must be called from within the PGM critical section. It may
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync * nip back to ring-3/0 in some cases.
9984bae4ef25be8afd95d184308adcfef6e2ec0dvboxsync * @remarks This function shouldn't really fail, however if it does
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * it probably means we've screwed up the size of the amount
8e972b677df5ee27b99211fc7e456a5aa50f3e68vboxsync * and/or the low-water mark of handy pages. Or, that some
8e972b677df5ee27b99211fc7e456a5aa50f3e68vboxsync * device I/O is causing a lot of pages to be allocated while
b978e5849454446957177fd47ee98609ab0457a6vboxsync * while the host is in a low-memory condition.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncint pgmPhysAllocPage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
e0b9d3c357adf9b7d05f55540e86f22943fc4b23vboxsync * Ensure that we've got a page handy, take it and use it.
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync AssertMsg(PGM_PAGE_IS_ZERO(pPage) || PGM_PAGE_IS_SHARED(pPage), ("%d %RGp\n", PGM_PAGE_GET_STATE(pPage), GCPhys));
22e281e75ed636601178296c6daebda8f1d17c59vboxsync Assert(iHandyPage < RT_ELEMENTS(pVM->pgm.s.aHandyPages));
34e34d3656484dde89a9c7b57fcbf7048d2fdb13vboxsync Assert(pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys != NIL_RTHCPHYS);
806d0b554daa555364af5f87bc96eccbe760db7avboxsync Assert(!(pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys & ~X86_PTE_PAE_PG_MASK));
806d0b554daa555364af5f87bc96eccbe760db7avboxsync Assert(pVM->pgm.s.aHandyPages[iHandyPage].idPage != NIL_GMM_PAGEID);
4b8fcf8e4d11536879a1f6e66279ba0c363a7eaevboxsync Assert(pVM->pgm.s.aHandyPages[iHandyPage].idSharedPage == NIL_GMM_PAGEID);
d98e61ba075ed7d0b567a5d884bc85d643fe3de7vboxsync * There are one or two action to be taken the next time we allocate handy pages:
9e4166cf5ed4940f506bc718ea6c89bf7ed252c8vboxsync * - Tell the GMM (global memory manager) what the page is being used for.
9e4166cf5ed4940f506bc718ea6c89bf7ed252c8vboxsync * (Speeds up replacement operations - sharing and defragmenting.)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * - If the current backing is shared, it must be freed.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync const RTHCPHYS HCPhys = pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys;
806d0b554daa555364af5f87bc96eccbe760db7avboxsync pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys = GCPhys;
806d0b554daa555364af5f87bc96eccbe760db7avboxsync pVM->pgm.s.aHandyPages[iHandyPage].idSharedPage = PGM_PAGE_GET_PAGEID(pPage);
22e281e75ed636601178296c6daebda8f1d17c59vboxsync Assert(PGM_PAGE_GET_PAGEID(pPage) != NIL_GMM_PAGEID);
806d0b554daa555364af5f87bc96eccbe760db7avboxsync Log2(("PGM: Replaced shared page %#x at %RGp with %#x / %RHp\n", PGM_PAGE_GET_PAGEID(pPage),
e149c362e69e5f0bbd82da11fd8163b2d29c3a72vboxsync GCPhys, pVM->pgm.s.aHandyPages[iHandyPage].idPage, HCPhys));
806d0b554daa555364af5f87bc96eccbe760db7avboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatPageReplaceShared);
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync/** @todo err.. what about copying the page content? */
806d0b554daa555364af5f87bc96eccbe760db7avboxsync Log2(("PGM: Replaced zero page %RGp with %#x / %RHp\n", GCPhys, pVM->pgm.s.aHandyPages[iHandyPage].idPage, HCPhys));
22e281e75ed636601178296c6daebda8f1d17c59vboxsync/** @todo verify that the handy page is zero! */
4b8fcf8e4d11536879a1f6e66279ba0c363a7eaevboxsync * Do the PGMPAGE modifications.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PGM_PAGE_SET_PAGEID(pPage, pVM->pgm.s.aHandyPages[iHandyPage].idPage);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ALLOCATED);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Deal with pages that are not writable, i.e. not in the ALLOCATED state.
4b8fcf8e4d11536879a1f6e66279ba0c363a7eaevboxsync * @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 backing.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM address.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pPage The physical page tracking structure.
79b24ef0ab7cd4a03a3571b3954c52ab8b573137vboxsync * @param GCPhys The address of the page.
9984bae4ef25be8afd95d184308adcfef6e2ec0dvboxsync * @remarks Called from within the PGM critical section.
b978e5849454446957177fd47ee98609ab0457a6vboxsyncint pgmPhysPageMakeWritable(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
1986f56777969a25707ab214f8dd070804be666cvboxsync PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ALLOCATED);
79b24ef0ab7cd4a03a3571b3954c52ab8b573137vboxsync /* fall thru */
806d0b554daa555364af5f87bc96eccbe760db7avboxsync default: /* to shut up GCC */
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync * Zero pages can be dummy pages for MMIO or reserved memory,
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync * so we need to check the flags before joining cause with
03319aeaef07ef63a404237f2cb56199131f4a03vboxsync * shared page replacement.
5e797edc29f96c8367de4fbf5874171c24a89ba7vboxsync /* fall thru */
3dde2f85d4cf477621a3128887a2c08a8bca7c01vboxsync * Maps a page into the current virtual address space so it can be accessed.
9e4166cf5ed4940f506bc718ea6c89bf7ed252c8vboxsync * @returns VBox status code.
9e4166cf5ed4940f506bc718ea6c89bf7ed252c8vboxsync * @retval VINF_SUCCESS on success.
79b24ef0ab7cd4a03a3571b3954c52ab8b573137vboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
22e281e75ed636601178296c6daebda8f1d17c59vboxsync * @param pVM The VM address.
5e797edc29f96c8367de4fbf5874171c24a89ba7vboxsync * @param pPage The physical page tracking structure.
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync * @param GCPhys The address of the page.
79b24ef0ab7cd4a03a3571b3954c52ab8b573137vboxsync * @param ppMap Where to store the address of the mapping tracking structure.
79b24ef0ab7cd4a03a3571b3954c52ab8b573137vboxsync * @param ppv Where to store the mapping address of the page. The page
79b24ef0ab7cd4a03a3571b3954c52ab8b573137vboxsync * offset is masked off!
e0b9d3c357adf9b7d05f55540e86f22943fc4b23vboxsync * @remarks Called from within the PGM critical section.
79b24ef0ab7cd4a03a3571b3954c52ab8b573137vboxsyncint pgmPhysPageMap(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, PPPGMPAGEMAP ppMap, void **ppv)
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync * Just some sketchy GC code.
34e34d3656484dde89a9c7b57fcbf7048d2fdb13vboxsync#else /* IN_RING3 || IN_RING0 */
4b8fcf8e4d11536879a1f6e66279ba0c363a7eaevboxsync * Find/make Chunk TLB entry for the mapping chunk.
79b24ef0ab7cd4a03a3571b3954c52ab8b573137vboxsync const uint32_t idChunk = PGM_PAGE_GET_CHUNKID(pPage);
79b24ef0ab7cd4a03a3571b3954c52ab8b573137vboxsync PPGMCHUNKR3MAPTLBE pTlbe = &pVM->pgm.s.ChunkR3Map.Tlb.aEntries[PGM_CHUNKR3MAPTLB_IDX(idChunk)];
f9147fe1eaa4e35287f8f39282c7f92f0d7de0b7vboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatChunkR3MapTlbHits);
7082d29724f6c3788977a51591b0379fd3acbf72vboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatChunkR3MapTlbMisses);
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync * Find the chunk, map it if necessary.
9984bae4ef25be8afd95d184308adcfef6e2ec0dvboxsync pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync int rc = VMMR0CallHost(pVM, VMMCALLHOST_PGM_MAP_CHUNK, idChunk);
f9147fe1eaa4e35287f8f39282c7f92f0d7de0b7vboxsync pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync * Enter it into the Chunk TLB.
f9147fe1eaa4e35287f8f39282c7f92f0d7de0b7vboxsync *ppv = (uint8_t *)pMap->pv + (PGM_PAGE_GET_PAGE_IN_CHUNK(pPage) << PAGE_SHIFT);
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync#endif /* IN_RING3 */
1986f56777969a25707ab214f8dd070804be666cvboxsync * Load a guest page into the ring-3 physical TLB.
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync * @returns VBox status code.
e149c362e69e5f0bbd82da11fd8163b2d29c3a72vboxsync * @retval VINF_SUCCESS on success
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync * @param pPGM The PGM instance pointer.
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync * @param GCPhys The guest physical address in question.
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsyncint pgmPhysPageLoadIntoTlb(PPGM pPGM, RTGCPHYS GCPhys)
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync STAM_COUNTER_INC(&pPGM->CTXMID(StatPage,MapTlbMisses));
9f9a20823b87e89c1b5cb45eb9b5699b29bfefebvboxsync * Find the ram range.
6f0193f5a9287559d34a75f438c2682d8fb08038vboxsync * 99.8% of requests are expected to be in the first range.
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync * Map the page.
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync * Make a special case for the zero page as it is kind of special.
22e281e75ed636601178296c6daebda8f1d17c59vboxsync PPGMPAGEMAPTLBE pTlbe = &pPGM->CTXSUFF(PhysTlb).aEntries[PGM_PAGEMAPTLB_IDX(GCPhys)];
65b61798a61dd4c32cce448db1dac70bba8d5cf5vboxsync int rc = pgmPhysPageMap(PGM2VM(pPGM), pPage, GCPhys, &pMap, &pv);
13ba5527caaa9b8c4fee29f22e374fa67c4c6f72vboxsync Assert(PGM_PAGE_GET_HCPHYS(pPage) == pPGM->HCPhysZeroPg);
65b61798a61dd4c32cce448db1dac70bba8d5cf5vboxsync#endif /* !IN_GC */
65b61798a61dd4c32cce448db1dac70bba8d5cf5vboxsync * Requests the mapping of a guest page into the current context.
65b61798a61dd4c32cce448db1dac70bba8d5cf5vboxsync * This API should only be used for very short term, as it will consume
46df4404c8dbbf3672e7aae8cd0b2770356e5b73vboxsync * scarse resources (R0 and GC) in the mapping cache. When you're done
46df4404c8dbbf3672e7aae8cd0b2770356e5b73vboxsync * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync * This API will assume your intention is to write to the page, and will
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync * therefore replace shared and zero pages. If you do not intend to modify
46df4404c8dbbf3672e7aae8cd0b2770356e5b73vboxsync * the page, use the PGMPhysGCPhys2CCPtrReadOnly() API.
9984bae4ef25be8afd95d184308adcfef6e2ec0dvboxsync * @returns VBox status code.
ce2d2eef2525963979102e63c2419d1c69d767ccvboxsync * @retval VINF_SUCCESS on success.
ce2d2eef2525963979102e63c2419d1c69d767ccvboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
46df4404c8dbbf3672e7aae8cd0b2770356e5b73vboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
9f9a20823b87e89c1b5cb45eb9b5699b29bfefebvboxsync * @param pVM The VM handle.
4b8fcf8e4d11536879a1f6e66279ba0c363a7eaevboxsync * @param GCPhys The guest physical address of the page that should be mapped.
ce2d2eef2525963979102e63c2419d1c69d767ccvboxsync * @param ppv Where to store the address corresponding to GCPhys.
ce2d2eef2525963979102e63c2419d1c69d767ccvboxsync * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
9e4166cf5ed4940f506bc718ea6c89bf7ed252c8vboxsync * @remark Avoid calling this API from within critical sections (other than
ce2d2eef2525963979102e63c2419d1c69d767ccvboxsync * the PGM one) because of the deadlock risk.
ce2d2eef2525963979102e63c2419d1c69d767ccvboxsync * @thread Any thread.
3dde2f85d4cf477621a3128887a2c08a8bca7c01vboxsyncVMMDECL(int) PGMPhysGCPhys2CCPtr(PVM pVM, RTGCPHYS GCPhys, void **ppv, PPGMPAGEMAPLOCK pLock)
3dde2f85d4cf477621a3128887a2c08a8bca7c01vboxsync /* Until a physical TLB is implemented for GC, let PGMGCDynMapGCPageEx handle it. */
b978e5849454446957177fd47ee98609ab0457a6vboxsync * Query the Physical TLB entry for the page (may fail).
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsync int rc = pgmPhysPageQueryTlbe(&pVM->pgm.s, GCPhys, &pTlbe);
ce2d2eef2525963979102e63c2419d1c69d767ccvboxsync * If the page is shared, the zero page, or being write monitored
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsync * it must be converted to an page that's writable if possible.
4b8fcf8e4d11536879a1f6e66279ba0c363a7eaevboxsync if (RT_UNLIKELY(PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED))
9e4166cf5ed4940f506bc718ea6c89bf7ed252c8vboxsync /** @todo stuff is missing here! */
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsync * Now, just perform the locking and calculate the return address.
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsync if (RT_LIKELY(pPage->cLocks != PGM_PAGE_MAX_LOCKS))
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsync if (RT_UNLIKELY(++pPage->cLocks == PGM_PAGE_MAX_LOCKS))
585f64d6f624f9e683321dabeb21b0eb2e6aa473vboxsync AssertMsgFailed(("%VGp is entering permanent locked state!\n", GCPhys));
65b61798a61dd4c32cce448db1dac70bba8d5cf5vboxsync pMap->cRefs++; /* Extra ref to prevent it from going away. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync *ppv = (void *)((uintptr_t)pTlbe->pv | (GCPhys & PAGE_OFFSET_MASK));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#endif /* IN_RING3 || IN_RING0 */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Temporary fallback code.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Requests the mapping of a guest page into the current context.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This API should only be used for very short term, as it will consume
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * scarse resources (R0 and GC) in the mapping cache. When you're done
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * with the page, call PGMPhysReleasePageMappingLock() ASAP to 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 backing.
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 thread.
5b6e2c9a765c3c72295acc15791af8a700746956vboxsyncVMMDECL(int) PGMPhysGCPhys2CCPtrReadOnly(PVM pVM, RTGCPHYS GCPhys, void const **ppv, PPGMPAGEMAPLOCK pLock)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /** @todo implement this */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return PGMPhysGCPhys2CCPtr(pVM, GCPhys, (void **)ppv, pLock);
e9a217d585085a6a6d129d27ca0d96a1b8e6d0eevboxsync * Requests the mapping of a guest page given by virtual address into the current context.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This API should only be used for very short term, as it will consume
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * scarse resources (R0 and GC) in the mapping cache. When you're done
e17bd6c32a8dd64f2d42838f9028216465e2caf0vboxsync * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
e9a217d585085a6a6d129d27ca0d96a1b8e6d0eevboxsync * This API will assume your intention is to write to the page, and will
3cbb4f9a6a320e58ed398ef7aaa004cc8727abc5vboxsync * therefore replace shared and zero pages. If you do not intend to modify
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * the page, use the PGMPhysGCPtr2CCPtrReadOnly() API.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_SUCCESS on success.
5ace91141404400247438502a84a418fba00c8cfvboxsync * @retval VERR_PAGE_TABLE_NOT_PRESENT if the page directory for the virtual address isn't present.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VERR_PAGE_NOT_PRESENT if the page at the virtual address isn't present.
7e960d3a0a8a3a84d7aba2cca45d72b1c31cc97bvboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
e17bd6c32a8dd64f2d42838f9028216465e2caf0vboxsync * @param pVM The VM handle.
e17bd6c32a8dd64f2d42838f9028216465e2caf0vboxsync * @param GCPhys The guest physical address of the page that should be mapped.
e17bd6c32a8dd64f2d42838f9028216465e2caf0vboxsync * @param ppv Where to store the address corresponding to GCPhys.
08bc90fc2848c80bf8270bedc883745b8398e186vboxsync * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
e17bd6c32a8dd64f2d42838f9028216465e2caf0vboxsync * @remark Avoid calling this API from within critical sections (other than
e17bd6c32a8dd64f2d42838f9028216465e2caf0vboxsync * the PGM one) because of the deadlock risk.
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync * @thread EMT
e17bd6c32a8dd64f2d42838f9028216465e2caf0vboxsyncVMMDECL(int) PGMPhysGCPtr2CCPtr(PVM pVM, RTGCPTR GCPtr, void **ppv, PPGMPAGEMAPLOCK pLock)
49748bb305bd71f672cd083af208f4bb08c5d6abvboxsync * Requests the mapping of a guest page given by virtual address into the current context.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This API should only be used for very short term, as it will consume
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * scarse resources (R0 and GC) in the mapping cache. When you're done
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VINF_SUCCESS on success.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VERR_PAGE_TABLE_NOT_PRESENT if the page directory for the virtual address isn't present.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VERR_PAGE_NOT_PRESENT if the page at the virtual address isn't present.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
b9ca93dd1ad44cb8b27679dc5624be2f7b7f7af5vboxsync * @param GCPhys The guest physical address of the page that should be mapped.
b9ca93dd1ad44cb8b27679dc5624be2f7b7f7af5vboxsync * @param ppv Where to store the address corresponding to GCPhys.
b9ca93dd1ad44cb8b27679dc5624be2f7b7f7af5vboxsync * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
b9ca93dd1ad44cb8b27679dc5624be2f7b7f7af5vboxsync * @remark Avoid calling this API from within critical sections (other than
b9ca93dd1ad44cb8b27679dc5624be2f7b7f7af5vboxsync * the PGM one) because of the deadlock risk.
7c3417bbf525c03163d54d151a277a981d5d61b6vboxsync * @thread EMT
3cbb4f9a6a320e58ed398ef7aaa004cc8727abc5vboxsyncVMMDECL(int) PGMPhysGCPtr2CCPtrReadOnly(PVM pVM, RTGCPTR GCPtr, void const **ppv, PPGMPAGEMAPLOCK pLock)
51d4024e2984de499ecd878341898f71f55cf9e0vboxsync rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys, ppv, pLock);
e17f587595bd5d3a7be56a892e3fd3a0ef83d268vboxsync * Release the mapping of a guest page.
3cbb4f9a6a320e58ed398ef7aaa004cc8727abc5vboxsync * This is the counter part of PGMPhysGCPhys2CCPtr, PGMPhysGCPhys2CCPtrReadOnly
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * PGMPhysGCPtr2CCPtr and PGMPhysGCPtr2CCPtrReadOnly.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pLock The lock structure initialized by the mapping function.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncVMMDECL(void) PGMPhysReleasePageMappingLock(PVM pVM, PPGMPAGEMAPLOCK pLock)
ac6ddb9d00c1da301dfa25b0961dbd58b5f2f6e8vboxsync /* currently nothing to do here. */
ac6ddb9d00c1da301dfa25b0961dbd58b5f2f6e8vboxsync/* --- postponed
ac6ddb9d00c1da301dfa25b0961dbd58b5f2f6e8vboxsync#elif defined(IN_RING0)
ac6ddb9d00c1da301dfa25b0961dbd58b5f2f6e8vboxsync#else /* IN_RING3 */
ac6ddb9d00c1da301dfa25b0961dbd58b5f2f6e8vboxsync PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)pLock->pvChunk;
ac6ddb9d00c1da301dfa25b0961dbd58b5f2f6e8vboxsync#endif /* IN_RING3 */
f351fab06d1e1507b72f5c574df71193061fd393vboxsync * Converts a GC physical address to a HC pointer.
f351fab06d1e1507b72f5c574df71193061fd393vboxsync * @returns VINF_SUCCESS on success.
f351fab06d1e1507b72f5c574df71193061fd393vboxsync * @returns VERR_PGM_PHYS_PAGE_RESERVED it it's a valid GC physical
ac6ddb9d00c1da301dfa25b0961dbd58b5f2f6e8vboxsync * page but has no physical backing.
ac6ddb9d00c1da301dfa25b0961dbd58b5f2f6e8vboxsync * @returns VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid
ac6ddb9d00c1da301dfa25b0961dbd58b5f2f6e8vboxsync * GC physical address.
1f19ba1642702523c0d2684aa2542d051f15275bvboxsync * @returns VERR_PGM_GCPHYS_RANGE_CROSSES_BOUNDARY if the range crosses
ac6ddb9d00c1da301dfa25b0961dbd58b5f2f6e8vboxsync * a dynamic ram chunk boundary
ac6ddb9d00c1da301dfa25b0961dbd58b5f2f6e8vboxsync * @param pVM The VM handle.
ac6ddb9d00c1da301dfa25b0961dbd58b5f2f6e8vboxsync * @param GCPhys The GC physical address to convert.
ac6ddb9d00c1da301dfa25b0961dbd58b5f2f6e8vboxsync * @param cbRange Physical range
ac6ddb9d00c1da301dfa25b0961dbd58b5f2f6e8vboxsync * @param pHCPtr Where to store the HC pointer on success.
ac6ddb9d00c1da301dfa25b0961dbd58b5f2f6e8vboxsyncVMMDECL(int) PGMPhysGCPhys2HCPtr(PVM pVM, RTGCPHYS GCPhys, RTUINT cbRange, PRTHCPTR pHCPtr) /** @todo @bugref{1865}: HCPtr -> R3Ptr */
ac6ddb9d00c1da301dfa25b0961dbd58b5f2f6e8vboxsync VM_ASSERT_EMT(pVM); /* no longer safe for use outside the EMT thread! */
ac6ddb9d00c1da301dfa25b0961dbd58b5f2f6e8vboxsync if ((GCPhys & PGM_DYNAMIC_CHUNK_BASE_MASK) != ((GCPhys+cbRange-1) & PGM_DYNAMIC_CHUNK_BASE_MASK))
f351fab06d1e1507b72f5c574df71193061fd393vboxsync AssertMsgFailed(("%VGp - %VGp crosses a chunk boundary!!\n", GCPhys, GCPhys+cbRange));
ac6ddb9d00c1da301dfa25b0961dbd58b5f2f6e8vboxsync LogRel(("PGMPhysGCPhys2HCPtr %VGp - %VGp crosses a chunk boundary!!\n", GCPhys, GCPhys+cbRange));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = pgmPhysGetPageAndRangeEx(&pVM->pgm.s, GCPhys, &pPage, &pRam);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertMsgFailed(("%VGp - %VGp crosses a chunk boundary!!\n", GCPhys, GCPhys + cbRange));
09e2f92b8f54aa362088e216007b53ecdb42e283vboxsync unsigned iChunk = (off >> PGM_DYNAMIC_CHUNK_SHIFT);
5ace91141404400247438502a84a418fba00c8cfvboxsync#if defined(IN_GC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0) /* ASSUMES this is a rare occurence */
5ace91141404400247438502a84a418fba00c8cfvboxsync PRTR3UINTPTR paChunkR3Ptrs = (PRTR3UINTPTR)MMHyperR3ToCC(pVM, pRam->paChunkR3Ptrs);
5ace91141404400247438502a84a418fba00c8cfvboxsync *pHCPtr = (RTHCPTR)(paChunkR3Ptrs[iChunk] + (off & PGM_DYNAMIC_CHUNK_OFFSET_MASK));
5ace91141404400247438502a84a418fba00c8cfvboxsync *pHCPtr = (RTHCPTR)(pRam->paChunkR3Ptrs[iChunk] + (off & PGM_DYNAMIC_CHUNK_OFFSET_MASK));
247b55faa8d054157f2481e68caca36f4dc9542cvboxsync *pHCPtr = (RTHCPTR)((RTR3UINTPTR)pRam->pvR3 + off);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * PGMPhysGCPhys2HCPtr convenience for use with assertions.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns The HCPtr, NIL_RTHCPTR on failure.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM The VM handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param GCPhys The GC Physical addresss.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param cbRange Physical range.
78a205e3fc6719d59e8c561b3d287d3a4f879852vboxsyncVMMDECL(RTHCPTR) PGMPhysGCPhys2HCPtrAssert(PVM pVM, RTGCPHYS GCPhys, RTUINT cbRange) /** @todo @bugref{1865}: HCPtr -> R3Ptr */
9984bae4ef25be8afd95d184308adcfef6e2ec0dvboxsync int rc = PGMPhysGCPhys2HCPtr(pVM, GCPhys, cbRange, &HCPtr);
d07ea3c3faead54c0de83ab93f2b03dedfb819e2vboxsync * Converts a guest pointer to a GC physical address.
09e2f92b8f54aa362088e216007b53ecdb42e283vboxsync * This uses the current CR3/CR0/CR4 of the guest.
09e2f92b8f54aa362088e216007b53ecdb42e283vboxsync * @returns VBox status code.
09e2f92b8f54aa362088e216007b53ecdb42e283vboxsync * @param pVM The VM Handle
09e2f92b8f54aa362088e216007b53ecdb42e283vboxsync * @param GCPtr The guest pointer to convert.
09e2f92b8f54aa362088e216007b53ecdb42e283vboxsync * @param pGCPhys Where to store the GC physical address.
3cbb4f9a6a320e58ed398ef7aaa004cc8727abc5vboxsyncVMMDECL(int) PGMPhysGCPtr2GCPhys(PVM pVM, RTGCPTR GCPtr, PRTGCPHYS pGCPhys)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtr, NULL, pGCPhys);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Converts a guest pointer to a HC physical address.
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync * This uses the current CR3/CR0/CR4 of the guest.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code.
3cbb4f9a6a320e58ed398ef7aaa004cc8727abc5vboxsync * @param pVM The VM Handle
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param GCPtr The guest pointer to convert.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pHCPhys Where to store the HC physical address.
1fb9c510656583ba12872e082125263a58d9bc6bvboxsyncVMMDECL(int) PGMPhysGCPtr2HCPhys(PVM pVM, RTGCPTR GCPtr, PRTHCPHYS pHCPhys)
1fb9c510656583ba12872e082125263a58d9bc6bvboxsync int rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtr, NULL, &GCPhys);
247b55faa8d054157f2481e68caca36f4dc9542cvboxsync rc = PGMPhysGCPhys2HCPhys(pVM, GCPhys | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK), pHCPhys);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Converts a guest pointer to a HC pointer.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This uses the current CR3/CR0/CR4 of the guest.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status code.
9984bae4ef25be8afd95d184308adcfef6e2ec0dvboxsync * @param pVM The VM Handle
9984bae4ef25be8afd95d184308adcfef6e2ec0dvboxsync * @param GCPtr The guest pointer to convert.
9e4166cf5ed4940f506bc718ea6c89bf7ed252c8vboxsync * @param pHCPtr Where to store the HC virtual address.
0975ae0a0fb615c945150c48e4a73187c1f4f84dvboxsyncVMMDECL(int) PGMPhysGCPtr2HCPtr(PVM pVM, RTGCPTR GCPtr, PRTHCPTR pHCPtr) /** @todo @bugref{1865}: HCPtr -> R3Ptr */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync VM_ASSERT_EMT(pVM); /* no longer safe for use outside the EMT thread! */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtr, NULL, &GCPhys);
1a25adaca81841abf5e6cdfed02eaff64941357dvboxsync rc = PGMPhysGCPhys2HCPtr(pVM, GCPhys | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK), 1 /* we always stay within one page */, pHCPtr);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Converts a guest virtual address to a HC pointer by specfied CR3 and flags.
3cbb4f9a6a320e58ed398ef7aaa004cc8727abc5vboxsync * @returns VBox status code.
a11c569636fa6838bd423f4631a9660a5a84204bvboxsync * @param pVM The VM Handle
5b6e2c9a765c3c72295acc15791af8a700746956vboxsync * @param GCPtr The guest pointer to convert.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param cr3 The guest CR3.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param fFlags Flags used for interpreting the PD correctly: X86_CR4_PSE and X86_CR4_PAE
036d626c5d4722da925dc8292f9248a5e09b4588vboxsync * @param pHCPtr Where to store the HC pointer.
1fb9c510656583ba12872e082125263a58d9bc6bvboxsync * @remark This function is used by the REM at a time where PGM could
1fb9c510656583ba12872e082125263a58d9bc6bvboxsync * potentially not be in sync. It could also be used by a
1fb9c510656583ba12872e082125263a58d9bc6bvboxsync * future DBGF API to cpu state independent conversions.
1fb9c510656583ba12872e082125263a58d9bc6bvboxsyncVMMDECL(int) PGMPhysGCPtr2HCPtrByGstCR3(PVM pVM, RTGCPTR GCPtr, uint64_t cr3, unsigned fFlags, PRTHCPTR pHCPtr) /** @todo @bugref{1865}: HCPtr -> R3Ptr */
1fb9c510656583ba12872e082125263a58d9bc6bvboxsync VM_ASSERT_EMT(pVM); /* no longer safe for use outside the EMT thread! */
1fb9c510656583ba12872e082125263a58d9bc6bvboxsync * PAE or 32-bit?
1fb9c510656583ba12872e082125263a58d9bc6bvboxsync rc = PGM_GCPHYS_2_PTR(pVM, cr3 & X86_CR3_PAGE_MASK, &pPD);
1fb9c510656583ba12872e082125263a58d9bc6bvboxsync X86PDE Pde = pPD->a[(RTGCUINTPTR)GCPtr >> X86_PD_SHIFT];
1fb9c510656583ba12872e082125263a58d9bc6bvboxsync { /* (big page) */
036d626c5d4722da925dc8292f9248a5e09b4588vboxsync rc = PGMPhysGCPhys2HCPtr(pVM, pgmGstGet4MBPhysPage(&pVM->pgm.s, Pde) | ((RTGCUINTPTR)GCPtr & X86_PAGE_4M_OFFSET_MASK), 1 /* we always stay within one page */, pHCPtr);
f351fab06d1e1507b72f5c574df71193061fd393vboxsync { /* (normal page) */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = PGM_GCPHYS_2_PTR(pVM, Pde.u & X86_PDE_PG_MASK, &pPT);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync X86PTE Pte = pPT->a[((RTGCUINTPTR)GCPtr >> X86_PT_SHIFT) & X86_PT_MASK];
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return PGMPhysGCPhys2HCPtr(pVM, (Pte.u & X86_PTE_PG_MASK) | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK), 1 /* we always stay within one page */, pHCPtr);
50df3da42ff6589b0ecc4f50f2288811bc370186vboxsync /** @todo long mode! */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync rc = PGM_GCPHYS_2_PTR(pVM, cr3 & X86_CR3_PAE_PAGE_MASK, &pPdpt);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync X86PDPE Pdpe = pPdpt->a[((RTGCUINTPTR)GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE];
6778b34cb96bef0fef23ebc461eb6a429d2907c5vboxsync rc = PGM_GCPHYS_2_PTR(pVM, Pdpe.u & X86_PDPE_PG_MASK, &pPD);
6778b34cb96bef0fef23ebc461eb6a429d2907c5vboxsync X86PDEPAE Pde = pPD->a[((RTGCUINTPTR)GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK];
614cbe11a7e5588dc8d369e223174b1441a09359vboxsync { /* (big page) */
614cbe11a7e5588dc8d369e223174b1441a09359vboxsync rc = PGMPhysGCPhys2HCPtr(pVM, (Pde.u & X86_PDE2M_PAE_PG_MASK) | ((RTGCUINTPTR)GCPtr & X86_PAGE_2M_OFFSET_MASK), 1 /* we always stay within one page */, pHCPtr);
614cbe11a7e5588dc8d369e223174b1441a09359vboxsync { /* (normal page) */
614cbe11a7e5588dc8d369e223174b1441a09359vboxsync rc = PGM_GCPHYS_2_PTR(pVM, (Pde.u & X86_PDE_PAE_PG_MASK), &pPT);
614cbe11a7e5588dc8d369e223174b1441a09359vboxsync X86PTEPAE Pte = pPT->a[((RTGCUINTPTR)GCPtr >> X86_PT_PAE_SHIFT) & X86_PT_PAE_MASK];
9984bae4ef25be8afd95d184308adcfef6e2ec0dvboxsync return PGMPhysGCPhys2HCPtr(pVM, (Pte.u & X86_PTE_PAE_PG_MASK) | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK), 1 /* we always stay within one page */, pHCPtr);
fd69ca9bd8b533bfa9ade45c1c2ff3116854e84avboxsync * Cache PGMPhys memory access
fd69ca9bd8b533bfa9ade45c1c2ff3116854e84avboxsync * @param pVM VM Handle.
fd69ca9bd8b533bfa9ade45c1c2ff3116854e84avboxsync * @param pCache Cache structure pointer
fd69ca9bd8b533bfa9ade45c1c2ff3116854e84avboxsync * @param GCPhys GC physical address
fd69ca9bd8b533bfa9ade45c1c2ff3116854e84avboxsync * @param pbHC HC pointer corresponding to physical page
b978e5849454446957177fd47ee98609ab0457a6vboxsync * @thread EMT.
b978e5849454446957177fd47ee98609ab0457a6vboxsyncstatic void pgmPhysCacheAdd(PVM pVM, PGMPHYSCACHE *pCache, RTGCPHYS GCPhys, uint8_t *pbHC)
5ace91141404400247438502a84a418fba00c8cfvboxsync iCacheIndex = ((GCPhys >> PAGE_SHIFT) & PGM_MAX_PHYSCACHE_ENTRIES_MASK);
5a12b9772d9cf396a0ba7f54db399817ba7a65bavboxsync * Read physical memory.
5ace91141404400247438502a84a418fba00c8cfvboxsync * This API respects access handlers and MMIO. Use PGMPhysReadGCPhys() if you
2b5a4cf3d77ab62dcbd882115b6d497547b20d29vboxsync * want to ignore those.
5ace91141404400247438502a84a418fba00c8cfvboxsync * @param pVM VM Handle.
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync * @param GCPhys Physical address start reading from.
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync * @param pvBuf Where to put the read bits.
5ace91141404400247438502a84a418fba00c8cfvboxsync * @param cbRead How many bytes to read.
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsyncVMMDECL(void) PGMPhysRead(PVM pVM, RTGCPHYS GCPhys, void *pvBuf, size_t cbRead)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync bool fGrabbedLock = false;
0f5d1b2abd9e82c7ee46f1327287c44856604bcbvboxsync AssertMsg(cbRead > 0, ("don't even think about reading zero bytes!\n"));
20a793635760c71752932fde640ba328cab84348vboxsync LogFlow(("PGMPhysRead: %VGp %d\n", GCPhys, cbRead));
1fb9c510656583ba12872e082125263a58d9bc6bvboxsync * Copy loop on ram ranges.
1fb9c510656583ba12872e082125263a58d9bc6bvboxsync PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
1fb9c510656583ba12872e082125263a58d9bc6bvboxsync /* Find range. */
1fb9c510656583ba12872e082125263a58d9bc6bvboxsync /* Inside range or not? */
1fb9c510656583ba12872e082125263a58d9bc6bvboxsync * Must work our way thru this page by page.
1fb9c510656583ba12872e082125263a58d9bc6bvboxsync /* Physical chunk in dynamically allocated range not present? */
1fb9c510656583ba12872e082125263a58d9bc6bvboxsync /* Treat it as reserved; return zeros */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* temp hacks, will be reorganized. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Physical handler.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync else if ( RT_UNLIKELY(PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) >= PGM_PAGE_HNDL_PHYS_STATE_ALL)
3cbb4f9a6a320e58ed398ef7aaa004cc8727abc5vboxsync && !(pPage->HCPhys & MM_RAM_FLAGS_MMIO)) /// @todo PAGE FLAGS
09e2f92b8f54aa362088e216007b53ecdb42e283vboxsync#ifdef IN_RING3 /** @todo deal with this in GC and R0! */
09e2f92b8f54aa362088e216007b53ecdb42e283vboxsync /* find and call the handler */
09e2f92b8f54aa362088e216007b53ecdb42e283vboxsync PPGMPHYSHANDLER pNode = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.pTreesHC->PhysHandlers, GCPhys);
09e2f92b8f54aa362088e216007b53ecdb42e283vboxsync /** @note Dangerous assumption that HC handlers don't do anything that really requires an EMT lock! */
09e2f92b8f54aa362088e216007b53ecdb42e283vboxsync rc = pNode->pfnHandlerR3(pVM, GCPhys, pvSrc, pvBuf, cb, PGMACCESSTYPE_READ, pNode->pvUserR3);
09e2f92b8f54aa362088e216007b53ecdb42e283vboxsync#endif /* IN_RING3 */
09e2f92b8f54aa362088e216007b53ecdb42e283vboxsync PGMGCDynMapHCPage(pVM, PGM_PAGE_GET_HCPHYS(pPage), &pvSrc);
2b5a4cf3d77ab62dcbd882115b6d497547b20d29vboxsync * Virtual handlers.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync else if ( RT_UNLIKELY(PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) >= PGM_PAGE_HNDL_VIRT_STATE_ALL)
b978e5849454446957177fd47ee98609ab0457a6vboxsync && !(pPage->HCPhys & MM_RAM_FLAGS_MMIO)) /// @todo PAGE FLAGS
2b5a4cf3d77ab62dcbd882115b6d497547b20d29vboxsync#ifdef IN_RING3 /** @todo deal with this in GC and R0! */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync /* Search the whole tree for matching physical addresses (rather expensive!) */
2b5a4cf3d77ab62dcbd882115b6d497547b20d29vboxsync int rc2 = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pNode, &iPage);
74735ec6edd6640eebac8885fbb2dadc86b89cf5vboxsync RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pNode->Core.Key & PAGE_BASE_GC_MASK)
74735ec6edd6640eebac8885fbb2dadc86b89cf5vboxsync + (iPage << PAGE_SHIFT) + (off & PAGE_OFFSET_MASK);
74735ec6edd6640eebac8885fbb2dadc86b89cf5vboxsync /* Note: Dangerous assumption that HC handlers don't do anything that really requires an EMT lock! */
74735ec6edd6640eebac8885fbb2dadc86b89cf5vboxsync rc = pNode->pfnHandlerR3(pVM, (RTGCPTR)GCPtr, pvSrc, pvBuf, cb, PGMACCESSTYPE_READ, 0);
74735ec6edd6640eebac8885fbb2dadc86b89cf5vboxsync#endif /* IN_RING3 */
74735ec6edd6640eebac8885fbb2dadc86b89cf5vboxsync PGMGCDynMapHCPage(pVM, PGM_PAGE_GET_HCPHYS(pPage), &pvSrc);
2b5a4cf3d77ab62dcbd882115b6d497547b20d29vboxsync switch (pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_ROM)) /** @todo PAGE FLAGS */
1171e4fb031146163c9a5a66bd9cbf3f2a5acdb6vboxsync * Normal memory or ROM.
2b5a4cf3d77ab62dcbd882115b6d497547b20d29vboxsync //case MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO2: /* = shadow */ - //MMIO2 isn't in the mask.
f3e9d2446012445807621d8dd95afb2c8cad2789vboxsync case MM_RAM_FLAGS_MMIO2: // MMIO2 isn't in the mask.
e17f587595bd5d3a7be56a892e3fd3a0ef83d268vboxsync PGMGCDynMapHCPage(pVM, PGM_PAGE_GET_HCPHYS(pPage), &pvSrc);
2b5a4cf3d77ab62dcbd882115b6d497547b20d29vboxsync#if defined(IN_RING3) && defined(PGM_PHYSMEMACCESS_CACHING)
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync pgmPhysCacheAdd(pVM, &pVM->pgm.s.pgmphysreadcache, GCPhys, (uint8_t*)pvSrc);
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync#endif /* IN_RING3 && PGM_PHYSMEMACCESS_CACHING */
ac6ddb9d00c1da301dfa25b0961dbd58b5f2f6e8vboxsync * All reserved, nothing there.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * The rest needs to be taken more carefully.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#if 1 /** @todo r=bird: Can you do this properly please. */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync /** @todo Try MMIO; quick hack */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync if (cbRead <= 8 && IOMMMIORead(pVM, GCPhys, (uint32_t *)pvBuf, cbRead) == VINF_SUCCESS)
2b5a4cf3d77ab62dcbd882115b6d497547b20d29vboxsync /** @todo fix me later. */
2b5a4cf3d77ab62dcbd882115b6d497547b20d29vboxsync AssertReleaseMsgFailed(("Unknown read at %VGp size %d implement the complex physical reading case %x\n",
2b5a4cf3d77ab62dcbd882115b6d497547b20d29vboxsync pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_ROM))); /** @todo PAGE FLAGS */
2634ec5cbf8f1fa0a968cd4664ead6df1fed730dvboxsync LogFlow(("PGMPhysRead: Unassigned %VGp size=%d\n", GCPhys, cbRead));
2b5a4cf3d77ab62dcbd882115b6d497547b20d29vboxsync * Unassigned address space.
13b9db9ae2c12b6c4e00eda5c79772d57a0d29e1vboxsync * Write to physical memory.
13b9db9ae2c12b6c4e00eda5c79772d57a0d29e1vboxsync * This API respects access handlers and MMIO. Use PGMPhysReadGCPhys() if you
2634ec5cbf8f1fa0a968cd4664ead6df1fed730dvboxsync * want to ignore those.
13b9db9ae2c12b6c4e00eda5c79772d57a0d29e1vboxsync * @param pVM VM Handle.
2634ec5cbf8f1fa0a968cd4664ead6df1fed730dvboxsync * @param GCPhys Physical address to write to.
13b9db9ae2c12b6c4e00eda5c79772d57a0d29e1vboxsync * @param pvBuf What to write.
13b9db9ae2c12b6c4e00eda5c79772d57a0d29e1vboxsync * @param cbWrite How many bytes to write.
b0065cd95da56e4208839eb0d3963472c688a8d4vboxsyncVMMDECL(void) PGMPhysWrite(PVM pVM, RTGCPHYS GCPhys, const void *pvBuf, size_t cbWrite)
13b9db9ae2c12b6c4e00eda5c79772d57a0d29e1vboxsync bool fGrabbedLock = false;
2634ec5cbf8f1fa0a968cd4664ead6df1fed730dvboxsync AssertMsg(!pVM->pgm.s.fNoMorePhysWrites, ("Calling PGMPhysWrite after pgmR3Save()!\n"));
13b9db9ae2c12b6c4e00eda5c79772d57a0d29e1vboxsync AssertMsg(cbWrite > 0, ("don't even think about writing zero bytes!\n"));
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync LogFlow(("PGMPhysWrite: %VGp %d\n", GCPhys, cbWrite));
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync * Copy loop on ram ranges.
b0065cd95da56e4208839eb0d3963472c688a8d4vboxsync PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync /* Find range. */
2634ec5cbf8f1fa0a968cd4664ead6df1fed730dvboxsync /* Inside range or not? */
4584ef6026a823006bea5acbeb13dc4efe50da69vboxsync * Must work our way thru this page by page.
b40179b44fea65b72b2f226f62af1ed7bd3c48fcvboxsync /* Physical chunk in dynamically allocated range not present? */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync PGMPhysWrite(pVM, GCPhys, pvBuf, cbWrite); /* try again; can't assume pRam is still valid (paranoia) */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync rc = CTXALLMID(VMM, CallHost)(pVM, VMMCALLHOST_PGM_RAM_GROW_RANGE, GCPhys);
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync /* temporary hack, will reogranize is later. */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync * Virtual handlers
c39952b427f31961ee5281dcdd492ad847ca74bbvboxsync && !(pPage->HCPhys & MM_RAM_FLAGS_MMIO)) /// @todo PAGE FLAGS
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync * Physical write handler + virtual write handler.
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync * Consider this a quick workaround for the CSAM + shadow caching problem.
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync * We hand it to the shadow caching first since it requires the unchanged
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync * data. CSAM will have to put up with it already being changed.
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync#ifdef IN_RING3 /** @todo deal with this in GC and R0! */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync /* 1. The physical handler */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync PPGMPHYSHANDLER pPhysNode = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.pTreesHC->PhysHandlers, GCPhys);
01cc79d6798a7ad8b8041ddb2b67fc8b37bf0b37vboxsync size_t cbRange = pPhysNode->Core.KeyLast - GCPhys + 1;
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync /* Note! Dangerous assumption that R3 handlers don't do anything that really requires an EMT lock! */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync rc = pPhysNode->pfnHandlerR3(pVM, GCPhys, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, pPhysNode->pvUserR3);
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync /* 2. The virtual handler (will see incorrect data) */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync int rc2 = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pVirtNode, &iPage);
9984bae4ef25be8afd95d184308adcfef6e2ec0dvboxsync size_t cbRange = pVirtNode->Core.KeyLast - GCPhys + 1;
4b02cd4133ae0d8e4732c57274a59358e418349cvboxsync RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirtNode->Core.Key & PAGE_BASE_GC_MASK)
4b02cd4133ae0d8e4732c57274a59358e418349cvboxsync + (iPage << PAGE_SHIFT) + (off & PAGE_OFFSET_MASK);
1171e4fb031146163c9a5a66bd9cbf3f2a5acdb6vboxsync /* Note! Dangerous assumption that R3 handlers don't do anything that really requires an EMT lock! */
1171e4fb031146163c9a5a66bd9cbf3f2a5acdb6vboxsync rc2 = pVirtNode->pfnHandlerR3(pVM, (RTGCPTR)GCPtr, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, 0);
1171e4fb031146163c9a5a66bd9cbf3f2a5acdb6vboxsync#endif /* IN_RING3 */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync PGMGCDynMapHCPage(pVM, PGM_PAGE_GET_HCPHYS(pPage), &pvDst);
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync/** @todo deal with this in GC and R0! */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync /* Search the whole tree for matching physical addresses (rather expensive!) */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync int rc2 = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pNode, &iPage);
9b726ba798aabd1a27863e6f5cfeef1393bd198dvboxsync RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pNode->Core.Key & PAGE_BASE_GC_MASK)
9b726ba798aabd1a27863e6f5cfeef1393bd198dvboxsync + (iPage << PAGE_SHIFT) + (off & PAGE_OFFSET_MASK);
9984bae4ef25be8afd95d184308adcfef6e2ec0dvboxsync /* Note! Dangerous assumption that R3 handlers don't do anything that really requires an EMT lock! */
8ddd175fa7cb232e090f3b8934a411c167aadb27vboxsync rc = pNode->pfnHandlerR3(pVM, (RTGCPTR)GCPtr, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, 0);
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync#endif /* IN_RING3 */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync PGMGCDynMapHCPage(pVM, PGM_PAGE_GET_HCPHYS(pPage), &pvDst);
e8f172f2032e21b4be9a8f3df20e8ef689c6a6favboxsync * Physical handler.
01cc79d6798a7ad8b8041ddb2b67fc8b37bf0b37vboxsync else if ( RT_UNLIKELY(PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) >= PGM_PAGE_HNDL_PHYS_STATE_WRITE)
e8f172f2032e21b4be9a8f3df20e8ef689c6a6favboxsync && !(pPage->HCPhys & MM_RAM_FLAGS_MMIO)) /// @todo PAGE FLAGS
2b5a4cf3d77ab62dcbd882115b6d497547b20d29vboxsync#ifdef IN_RING3 /** @todo deal with this in GC and R0! */
2b5a4cf3d77ab62dcbd882115b6d497547b20d29vboxsync /* find and call the handler */
b0065cd95da56e4208839eb0d3963472c688a8d4vboxsync PPGMPHYSHANDLER pNode = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.pTreesHC->PhysHandlers, GCPhys);
b0065cd95da56e4208839eb0d3963472c688a8d4vboxsync /** @todo Dangerous assumption that HC handlers don't do anything that really requires an EMT lock! */
2cd06fc737773d015b5268b9e4dfba5997915957vboxsync rc = pNode->pfnHandlerR3(pVM, GCPhys, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, pNode->pvUserR3);
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync#endif /* IN_RING3 */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync PGMGCDynMapHCPage(pVM, PGM_PAGE_GET_HCPHYS(pPage), &pvDst);
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync /** @todo r=bird: missing MM_RAM_FLAGS_ROM here, we shall not allow anyone to overwrite the ROM! */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync switch (pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2)) /** @todo PAGE FLAGS */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync * Normal memory, MMIO2 or writable shadow ROM.
d41220dff1068effe66bb6a11f444811ba58de40vboxsync case MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO2: /* shadow rom */
d41220dff1068effe66bb6a11f444811ba58de40vboxsync PGMGCDynMapHCPage(pVM, PGM_PAGE_GET_HCPHYS(pPage), &pvDst);
d41220dff1068effe66bb6a11f444811ba58de40vboxsync#if defined(IN_RING3) && defined(PGM_PHYSMEMACCESS_CACHING)
d184d98f6e75700295aaa08ff14d0478dd6cc94avboxsync pgmPhysCacheAdd(pVM, &pVM->pgm.s.pgmphyswritecache, GCPhys, (uint8_t*)pvDst);
d41220dff1068effe66bb6a11f444811ba58de40vboxsync#endif /* IN_RING3 && PGM_PHYSMEMACCESS_CACHING */
d41220dff1068effe66bb6a11f444811ba58de40vboxsync * All reserved, nothing there.
01cc79d6798a7ad8b8041ddb2b67fc8b37bf0b37vboxsync * The rest needs to be taken more carefully.
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync#if 1 /** @todo r=bird: Can you do this properly please. */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync /** @todo Try MMIO; quick hack */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync if (cbWrite <= 8 && IOMMMIOWrite(pVM, GCPhys, *(uint32_t *)pvBuf, cbWrite) == VINF_SUCCESS)
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync /** @todo fix me later. */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync AssertReleaseMsgFailed(("Unknown write at %VGp size %d implement the complex physical writing case %x\n",
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync (pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2)))); /** @todo PAGE FLAGS */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync /* skip the write */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync * Unassigned address space.
a5c11842b92303b7b5d0086ee5011a969e321b50vboxsync#ifndef IN_GC /* Ring 0 & 3 only */ /** @todo @bugref{1865,3202}: this'll be fun! */
dea1368099e5337861dd52906d4c683c447a33c4vboxsync * Read from guest physical memory by GC physical address, bypassing
dea1368099e5337861dd52906d4c683c447a33c4vboxsync * MMIO and access handlers.
dea1368099e5337861dd52906d4c683c447a33c4vboxsync * @returns VBox status.
dea1368099e5337861dd52906d4c683c447a33c4vboxsync * @param pVM VM handle.
dea1368099e5337861dd52906d4c683c447a33c4vboxsync * @param pvDst The destination address.
dea1368099e5337861dd52906d4c683c447a33c4vboxsync * @param GCPhysSrc The source address (GC physical address).
dea1368099e5337861dd52906d4c683c447a33c4vboxsync * @param cb The number of bytes to read.
dea1368099e5337861dd52906d4c683c447a33c4vboxsyncVMMDECL(int) PGMPhysReadGCPhys(PVM pVM, void *pvDst, RTGCPHYS GCPhysSrc, size_t cb)
dea1368099e5337861dd52906d4c683c447a33c4vboxsync * Anything to be done?
dea1368099e5337861dd52906d4c683c447a33c4vboxsync * Loop ram ranges.
dea1368099e5337861dd52906d4c683c447a33c4vboxsync for (PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
dea1368099e5337861dd52906d4c683c447a33c4vboxsync /* Copy page by page as we're not dealing with a linear HC range. */
dea1368099e5337861dd52906d4c683c447a33c4vboxsync /* convert */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync int rc = pgmRamGCPhys2HCPtrWithRange(pVM, pRam, GCPhysSrc, &pvSrc);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync size_t cbRead = PAGE_SIZE - ((RTGCUINTPTR)GCPhysSrc & PAGE_OFFSET_MASK);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync memcpy(pvDst, (uint8_t *)pRam->pvR3 + off, cbRead);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Write to guest physical memory referenced by GC pointer.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Write memory to GC physical address in guest physical memory.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This will bypass MMIO and access handlers.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM VM handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param GCPhysDst The GC physical address of the destination.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pvSrc The source buffer.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param cb The number of bytes to write.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncVMMDECL(int) PGMPhysWriteGCPhys(PVM pVM, RTGCPHYS GCPhysDst, const void *pvSrc, size_t cb)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Anything to be done?
b45d66c0e496e2fd861479202f3d43aad592bd14vboxsync LogFlow(("PGMPhysWriteGCPhys: %VGp %d\n", GCPhysDst, cb));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Loop ram ranges.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync for (PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
1ce20e84ae29a712029d1e213740388d7e158d5cvboxsync/** @todo PGMRamGCPhys2HCPtrWithRange. */
1ce20e84ae29a712029d1e213740388d7e158d5cvboxsync /* Copy page by page as we're not dealing with a linear HC range. */
1ce20e84ae29a712029d1e213740388d7e158d5cvboxsync /* convert */
1ce20e84ae29a712029d1e213740388d7e158d5cvboxsync int rc = pgmRamGCPhys2HCPtrWithRange(pVM, pRam, GCPhysDst, &pvDst);
1ce20e84ae29a712029d1e213740388d7e158d5cvboxsync size_t cbWrite = PAGE_SIZE - ((RTGCUINTPTR)GCPhysDst & PAGE_OFFSET_MASK);
c995102a21cae5b5ffeedadbfaddcc105809e196vboxsync /* write */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync memcpy((uint8_t *)pRam->pvR3 + off, pvSrc, cbWrite);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Read from guest physical memory referenced by GC pointer.
e85d76a7e5a047db3cdc8576ff5f412c7b73bbabvboxsync * This function uses the current CR3/CR0/CR4 of the guest and will
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * bypass access handlers and not set any accessed bits.
e85d76a7e5a047db3cdc8576ff5f412c7b73bbabvboxsync * @returns VBox status.
9b45880674da6f82ca27cc28b0272de3dd3cc7dfvboxsync * @param pVM VM handle.
9b45880674da6f82ca27cc28b0272de3dd3cc7dfvboxsync * @param pvDst The destination address.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * @param GCPtrSrc The source address (GC pointer).
e85d76a7e5a047db3cdc8576ff5f412c7b73bbabvboxsync * @param cb The number of bytes to read.
b55721e255f55546462b62c2d09259bb7bf8ef90vboxsyncVMMDECL(int) PGMPhysReadGCPtr(PVM pVM, void *pvDst, RTGCPTR GCPtrSrc, size_t cb)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Anything to do?
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Optimize reads within a single page.
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync if (((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
8cdd041284cfe47b639146efec02846e20b67a0cvboxsync int rc = PGMPhysGCPtr2HCPtr(pVM, GCPtrSrc, &pvSrc);
5db1d52ffbcaa46c3d944c6c2d9c552306817d9avboxsync * Page by page.
9b45880674da6f82ca27cc28b0272de3dd3cc7dfvboxsync /* convert */
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync int rc = PGMPhysGCPtr2HCPtr(pVM, GCPtrSrc, &pvSrc);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync size_t cbRead = PAGE_SIZE - ((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Write to guest physical memory referenced by GC pointer.
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync * This function uses the current CR3/CR0/CR4 of the guest and will
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync * bypass access handlers and not set dirty or accessed bits.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM VM handle.
9b45880674da6f82ca27cc28b0272de3dd3cc7dfvboxsync * @param GCPtrDst The destination address (GC pointer).
9b45880674da6f82ca27cc28b0272de3dd3cc7dfvboxsync * @param pvSrc The source address.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * @param cb The number of bytes to write.
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsyncVMMDECL(int) PGMPhysWriteGCPtr(PVM pVM, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Anything to do?
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("PGMPhysWriteGCPtr: %VGv %d\n", GCPtrDst, cb));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Optimize writes within a single page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = PGMPhysGCPtr2HCPtr(pVM, GCPtrDst, &pvDst);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Page by page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* convert */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = PGMPhysGCPtr2HCPtr(pVM, GCPtrDst, &pvDst);
6b022885f2cb6a55167609edecd89570cd80001dvboxsync size_t cbWrite = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
74991d7531692858fd22acf371a7ee941567977cvboxsync * Read from guest physical memory referenced by GC pointer.
57399ab65e2825c324fb9dcb4642d4ae2c232509vboxsync * This function uses the current CR3/CR0/CR4 of the guest and will
3a21cbe769e7500039a2d17c794a911f7acb2dadvboxsync * respect access handlers and set accessed bits.
3a21cbe769e7500039a2d17c794a911f7acb2dadvboxsync * @returns VBox status.
3a21cbe769e7500039a2d17c794a911f7acb2dadvboxsync * @param pVM VM handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pvDst The destination address.
1a25adaca81841abf5e6cdfed02eaff64941357dvboxsync * @param GCPtrSrc The source address (GC pointer).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param cb The number of bytes to read.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync/** @todo use the PGMPhysReadGCPtr name and rename the unsafe one to something appropriate */
6863baa4d9788245537c986d9caf02fb1893d2c6vboxsyncVMMDECL(int) PGMPhysReadGCPtrSafe(PVM pVM, void *pvDst, RTGCPTR GCPtrSrc, size_t cb)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Anything to do?
74991d7531692858fd22acf371a7ee941567977cvboxsync LogFlow(("PGMPhysReadGCPtrSafe: %VGv %d\n", GCPtrSrc, cb));
74991d7531692858fd22acf371a7ee941567977cvboxsync * Optimize reads within a single page.
74991d7531692858fd22acf371a7ee941567977cvboxsync if (((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
74991d7531692858fd22acf371a7ee941567977cvboxsync /* Convert virtual to physical address */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* mark the guest page as accessed. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = PGMGstModifyPage(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)(X86_PTE_A));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Page by page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Convert virtual to physical address */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* mark the guest page as accessed. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = PGMGstModifyPage(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)(X86_PTE_A));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync size_t cbRead = PAGE_SIZE - ((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Write to guest physical memory referenced by GC pointer.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This function uses the current CR3/CR0/CR4 of the guest and will
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * respect access handlers and set dirty and accessed bits.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM VM handle.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param GCPtrDst The destination address (GC pointer).
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pvSrc The source address.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param cb The number of bytes to write.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync/** @todo use the PGMPhysWriteGCPtr name and rename the unsafe one to something appropriate */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncVMMDECL(int) PGMPhysWriteGCPtrSafe(PVM pVM, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Anything to do?
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync LogFlow(("PGMPhysWriteGCPtrSafe: %VGv %d\n", GCPtrDst, cb));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Optimize writes within a single page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Convert virtual to physical address */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* mark the guest page as accessed and dirty. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Page by page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Convert virtual to physical address */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* mark the guest page as accessed and dirty. */
3cac8f8c6923a3a89ecfccda5e89ad75f48658e0vboxsync rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync size_t cbWrite = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Write to guest physical memory referenced by GC pointer and update the PTE.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This function uses the current CR3/CR0/CR4 of the guest and will
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * bypass access handlers and set any dirty and accessed bits in the PTE.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * If you don't want to set the dirty bit, use PGMPhysWriteGCPtr().
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @returns VBox status.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param pVM VM handle.
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync * @param GCPtrDst The destination address (GC pointer).
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync * @param pvSrc The source address.
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync * @param cb The number of bytes to write.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncVMMDECL(int) PGMPhysWriteGCPtrDirty(PVM pVM, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
5db1d52ffbcaa46c3d944c6c2d9c552306817d9avboxsync * Anything to do?
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Optimize writes within a single page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync if (((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync int rc = PGMPhysGCPtr2HCPtr(pVM, GCPtrDst, &pvDst);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = PGMGstModifyPage(pVM, GCPtrDst, cb, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * Page by page.
75fca099c974cbbd48f132af1378c60232614760vboxsync /* convert */
75fca099c974cbbd48f132af1378c60232614760vboxsync int rc = PGMPhysGCPtr2HCPtr(pVM, GCPtrDst, &pvDst);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* mark the guest page as accessed and dirty. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync size_t cbWrite = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync#endif /* !IN_GC */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Performs a read of guest virtual memory for instruction emulation.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * This will check permissions, raise exceptions and update the access bits.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * The current implementation will bypass all access handlers. It may later be
a27fbcbb29ffc2196c2ebd0f2dad92f40c7ec65dvboxsync * changed to at least respect MMIO.
315f443a509c31db47b8f5cb94d26e54c3d5c497vboxsync * @returns VBox status code suitable to scheduling.
5db1d52ffbcaa46c3d944c6c2d9c552306817d9avboxsync * @retval VINF_SUCCESS if the read was performed successfully.
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
00599f6d39cc25ca39845c2433cd75de7b9f6971vboxsync * @param pVM The VM handle.
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync * @param pCtxCore The context core.
3080f6c0871099df43a4e91b31894d9c2b1369a8vboxsync * @param pvDst Where to put the bytes we've read.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param GCPtrSrc The source address.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @param cb The number of bytes to read. Not more than a page.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * @remark This function will dynamically map physical pages in GC. This may unmap
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * mappings done by the caller. Be careful!
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsyncVMMDECL(int) PGMPhysInterpretedRead(PVM pVM, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCUINTPTR GCPtrSrc, size_t cb)
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync/** @todo r=bird: This isn't perfect!
61fa69e2bc9fc9e7490feed1c020273f3ddb238dvboxsync * -# It's not checking for reserved bits being 1.
ec588a4ac8429a8b6c744544818b3ce3b2c75690vboxsync * -# It's not correctly dealing with the access bit.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * -# It's not respecting MMIO memory or any other access handlers.
75fca099c974cbbd48f132af1378c60232614760vboxsync * 1. Translate virtual to physical. This may fault.
75fca099c974cbbd48f132af1378c60232614760vboxsync * 2. Map the physical address.
75fca099c974cbbd48f132af1378c60232614760vboxsync * 3. Do the read operation.
57f41f24b7a290eba73c63449f44017d7917561dvboxsync * 4. Set access bits if required.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync unsigned cb1 = PAGE_SIZE - (GCPtrSrc & PAGE_OFFSET_MASK);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Not crossing pages.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrSrc, &fFlags, &GCPhys);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /** @todo we should check reserved bits ... */
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync Log(("PGMPhysInterpretedRead: pvDst=%p pvSrc=%p cb=%d\n", pvDst, (uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb));
fdc5224bd8d9a60af82da5809e3d6729c9bc69cbvboxsync memcpy(pvDst, (uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /** @todo access bit emulation isn't 100% correct. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
32e2c6f724d4daf2665f2ffa5cbc2333073d42cfvboxsync * Crosses pages.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrSrc, &fFlags1, &GCPhys1);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrSrc + cb1, &fFlags2, &GCPhys2);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /** @todo we should check reserved bits ... */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertMsgFailed(("cb=%d cb1=%d cb2=%d GCPtrSrc=%VGv\n", cb, cb1, cb2, GCPtrSrc));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync memcpy(pvDst, (uint8_t *)pvSrc1 + (GCPtrSrc & PAGE_OFFSET_MASK), cb1);
9b45880674da6f82ca27cc28b0272de3dd3cc7dfvboxsync rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
75fca099c974cbbd48f132af1378c60232614760vboxsync rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrSrc + cb1, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync * Raise a #PF.
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync /* Get the current privilege level. */
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync AssertMsgFailed(("rc=%Vrc GCPtrSrc=%VGv cb=%#x\n", rc, GCPtrSrc, cb));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync Log(("PGMPhysInterpretedRead: GCPtrSrc=%VGv cb=%#x -> #PF(%#x)\n", GCPtrSrc, cb, uErr));
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync return TRPMRaiseXcptErrCR2(pVM, pCtxCore, X86_XCPT_PF, uErr, GCPtrSrc);
d408b82da0773c7e8cd4b3a01cb8a065a2c73a2dvboxsync/// @todo VMMDECL(int) PGMPhysInterpretedWrite(PVM pVM, PCPUMCTXCORE pCtxCore, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)