PGMR0.cpp revision f94f82d66536c7332c347dd9a3a9f0f8c79247f4
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * PGM - Page Manager and Monitor, Ring-0.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Copyright (C) 2007 Sun Microsystems, Inc.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * available from http://www.virtualbox.org. This file is free software;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * additional information or have any questions.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync/*******************************************************************************
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync* Header Files *
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync*******************************************************************************/
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#define PGM_BTH_NAME(name) PGM_BTH_NAME_32BIT_PROT(name)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_PROT(name)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#define PGM_BTH_NAME(name) PGM_BTH_NAME_AMD64_PROT(name)
289060a0c3cb1d509f2cb01fca060796212376f6vboxsync#define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_PROT(name)
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * Worker function for PGMR3PhysAllocateHandyPages and pgmPhysEnsureHandyPage.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * @returns The following VBox status codes.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * @retval VINF_SUCCESS on success. FF cleared.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * @retval VINF_EM_NO_MEMORY if we're out of memory. The FF is set in this case.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * @param pVM The VM handle.
faf968cea88f2ab4bcc3325b17bc8b095a8e3642vboxsync * @param pVCpu The VMCPU handle.
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync * @remarks Must be called from within the PGM critical section. The caller
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync * must clear the new pages.
faf968cea88f2ab4bcc3325b17bc8b095a8e3642vboxsyncVMMR0DECL(int) PGMR0PhysAllocateHandyPages(PVM pVM, PVMCPU pVCpu)
faf968cea88f2ab4bcc3325b17bc8b095a8e3642vboxsync Assert(PDMCritSectIsOwnerEx(&pVM->pgm.s.CritSect, pVCpu->idCpu));
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * Check for error injection.
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * Try allocate a full set of handy pages.
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync AssertReturn(iFirst <= RT_ELEMENTS(pVM->pgm.s.aHandyPages), VERR_INTERNAL_ERROR);
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync uint32_t cPages = RT_ELEMENTS(pVM->pgm.s.aHandyPages) - iFirst;
f94f82d66536c7332c347dd9a3a9f0f8c79247f4vboxsync int rc = GMMR0AllocateHandyPages(pVM, pVCpu->idCpu, cPages, cPages, &pVM->pgm.s.aHandyPages[iFirst]);
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync for (uint32_t i = 0; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync Assert(pVM->pgm.s.aHandyPages[i].idPage != NIL_GMM_PAGEID);
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync Assert(pVM->pgm.s.aHandyPages[i].idPage <= GMM_PAGEID_LAST);
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync Assert(pVM->pgm.s.aHandyPages[i].idSharedPage == NIL_GMM_PAGEID);
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync Assert(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys != NIL_RTHCPHYS);
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync Assert(!(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys & ~X86_PTE_PAE_PG_MASK));
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync pVM->pgm.s.cHandyPages = RT_ELEMENTS(pVM->pgm.s.aHandyPages);
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync /* We're ASSUMING that GMM has updated all the entires before failing us. */
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync for (i = iFirst; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync Assert(pVM->pgm.s.aHandyPages[i].idPage == NIL_GMM_PAGEID);
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync Assert(pVM->pgm.s.aHandyPages[i].idSharedPage == NIL_GMM_PAGEID);
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync Assert(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys == NIL_RTHCPHYS);
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * Reduce the number of pages until we hit the minimum limit.
f94f82d66536c7332c347dd9a3a9f0f8c79247f4vboxsync rc = GMMR0AllocateHandyPages(pVM, pVCpu->idCpu, cPages, cPages, &pVM->pgm.s.aHandyPages[iFirst]);
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync while (i-- > 0)
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync Assert(pVM->pgm.s.aHandyPages[i].idPage != NIL_GMM_PAGEID);
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync Assert(pVM->pgm.s.aHandyPages[i].idPage <= GMM_PAGEID_LAST);
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync Assert(pVM->pgm.s.aHandyPages[i].idSharedPage == NIL_GMM_PAGEID);
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync Assert(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys != NIL_RTHCPHYS);
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync Assert(!(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys & ~X86_PTE_PAE_PG_MASK));
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync for (i = cPages + iFirst; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync Assert(pVM->pgm.s.aHandyPages[i].idPage == NIL_GMM_PAGEID);
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync Assert(pVM->pgm.s.aHandyPages[i].idSharedPage == NIL_GMM_PAGEID);
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync Assert(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys == NIL_RTHCPHYS);
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync LogRel(("PGMR0PhysAllocateHandyPages: rc=%Rrc iFirst=%d cPages=%d\n", rc, iFirst, cPages));
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync LogFlow(("PGMR0PhysAllocateHandyPages: cPages=%d rc=%Rrc\n", cPages, rc));
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * #PF Handler for nested paging.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @returns VBox status code (appropriate for trap handling and GC return).
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @param pVM VM Handle.
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync * @param pVCpu VMCPU Handle.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @param enmShwPagingMode Paging mode for the nested page tables
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @param uErr The trap error code.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @param pRegFrame Trap register frame.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @param pvFault The fault address.
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsyncVMMR0DECL(int) PGMR0Trap0eHandlerNestedPaging(PVM pVM, PVMCPU pVCpu, PGMMODE enmShwPagingMode, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPHYS pvFault)
b1c3cdef473df2fbc621d5da81acc82dbfb8a11avboxsync LogFlow(("PGMTrap0eHandler: uErr=%#x pvFault=%RGp eip=%RGv\n", uErr, pvFault, (RTGCPTR)pRegFrame->rip));
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync STAM_STATS({ pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution) = NULL; } );
289060a0c3cb1d509f2cb01fca060796212376f6vboxsync /* AMD uses the host's paging mode; Intel has a single mode (EPT). */
289060a0c3cb1d509f2cb01fca060796212376f6vboxsync AssertMsg(enmShwPagingMode == PGMMODE_32_BIT || enmShwPagingMode == PGMMODE_PAE || enmShwPagingMode == PGMMODE_PAE_NX || enmShwPagingMode == PGMMODE_AMD64 || enmShwPagingMode == PGMMODE_AMD64_NX || enmShwPagingMode == PGMMODE_EPT, ("enmShwPagingMode=%d\n", enmShwPagingMode));
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * Error code stats.
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSNotPresentWrite);
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSNotPresentRead);
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSWrite);
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSReserved);
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSRead);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync { /* Supervisor */
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVNotPresentWrite);
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVNotPresentRead);
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVWrite);
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVReserved);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * Call the worker.
9ad5e3912962c3dbccc1afc4e7d62890fe906814vboxsync * We pretend the guest is in protected mode without paging, so we can use existing code to build the
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync * nested page tables.
d45f7f7fe0c28b500b45b2dc88d7a04f4c0be6b8vboxsync rc = PGM_BTH_NAME_32BIT_PROT(Trap0eHandler)(pVCpu, uErr, pRegFrame, pvFault);
d45f7f7fe0c28b500b45b2dc88d7a04f4c0be6b8vboxsync rc = PGM_BTH_NAME_PAE_PROT(Trap0eHandler)(pVCpu, uErr, pRegFrame, pvFault);
d45f7f7fe0c28b500b45b2dc88d7a04f4c0be6b8vboxsync rc = PGM_BTH_NAME_AMD64_PROT(Trap0eHandler)(pVCpu, uErr, pRegFrame, pvFault);
d45f7f7fe0c28b500b45b2dc88d7a04f4c0be6b8vboxsync rc = PGM_BTH_NAME_EPT_PROT(Trap0eHandler)(pVCpu, uErr, pRegFrame, pvFault);
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync STAM_STATS({ if (!pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution))
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution) = &pVCpu->pgm.s.StatRZTrap0eTime2Misc; });