PGMR0.cpp revision 7c9a5eca233baf6ede345ace077a00bd0b7af1ef
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * PGM - Page Manager and Monitor, Ring-0.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Copyright (C) 2007-2010 Oracle Corporation
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * available from http://www.virtualbox.org. This file is free software;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * you can redistribute it and/or modify it under the terms of the GNU
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * General Public License (GPL) as published by the Free Software
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/*******************************************************************************
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync* Header Files *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync*******************************************************************************/
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Instantiate the ring-0 header/code templates.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#define PGM_BTH_NAME(name) PGM_BTH_NAME_32BIT_PROT(name)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_PROT(name)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#define PGM_BTH_NAME(name) PGM_BTH_NAME_AMD64_PROT(name)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_PROT(name)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Worker function for PGMR3PhysAllocateHandyPages and pgmPhysEnsureHandyPage.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @returns The following VBox status codes.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @retval VINF_SUCCESS on success. FF cleared.
687794577e2e35c3cae67e692a7f2130d1262a82vboxsync * @retval VINF_EM_NO_MEMORY if we're out of memory. The FF is set in this case.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pVM The VM handle.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * @param pVCpu The VMCPU handle.
c142e0f462e6c921a73d84ee50f41396b3a99db5vboxsync * @remarks Must be called from within the PGM critical section. The caller
c142e0f462e6c921a73d84ee50f41396b3a99db5vboxsync * must clear the new pages.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncVMMR0DECL(int) PGMR0PhysAllocateHandyPages(PVM pVM, PVMCPU pVCpu)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(PDMCritSectIsOwnerEx(&pVM->pgm.s.CritSect, pVCpu->idCpu));
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync * Check for error injection.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Try allocate a full set of handy pages.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync AssertReturn(iFirst <= RT_ELEMENTS(pVM->pgm.s.aHandyPages), VERR_INTERNAL_ERROR);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync uint32_t cPages = RT_ELEMENTS(pVM->pgm.s.aHandyPages) - iFirst;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync int rc = GMMR0AllocateHandyPages(pVM, pVCpu->idCpu, cPages, cPages, &pVM->pgm.s.aHandyPages[iFirst]);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync for (uint32_t i = 0; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(pVM->pgm.s.aHandyPages[i].idPage != NIL_GMM_PAGEID);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(pVM->pgm.s.aHandyPages[i].idPage <= GMM_PAGEID_LAST);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync Assert(pVM->pgm.s.aHandyPages[i].idSharedPage == NIL_GMM_PAGEID);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys != NIL_RTHCPHYS);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(!(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys & ~X86_PTE_PAE_PG_MASK));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pVM->pgm.s.cHandyPages = RT_ELEMENTS(pVM->pgm.s.aHandyPages);
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync /* We're ASSUMING that GMM has updated all the entires before failing us. */
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync for (i = iFirst; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync Assert(pVM->pgm.s.aHandyPages[i].idPage == NIL_GMM_PAGEID);
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync Assert(pVM->pgm.s.aHandyPages[i].idSharedPage == NIL_GMM_PAGEID);
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync Assert(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys == NIL_RTHCPHYS);
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync * Reduce the number of pages until we hit the minimum limit.
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync rc = GMMR0AllocateHandyPages(pVM, pVCpu->idCpu, cPages, cPages, &pVM->pgm.s.aHandyPages[iFirst]);
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync while (i-- > 0)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(pVM->pgm.s.aHandyPages[i].idPage != NIL_GMM_PAGEID);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(pVM->pgm.s.aHandyPages[i].idPage <= GMM_PAGEID_LAST);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(pVM->pgm.s.aHandyPages[i].idSharedPage == NIL_GMM_PAGEID);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys != NIL_RTHCPHYS);
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync Assert(!(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys & ~X86_PTE_PAE_PG_MASK));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync for (i = cPages + iFirst; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync Assert(pVM->pgm.s.aHandyPages[i].idPage == NIL_GMM_PAGEID);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync Assert(pVM->pgm.s.aHandyPages[i].idSharedPage == NIL_GMM_PAGEID);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys == NIL_RTHCPHYS);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogRel(("PGMR0PhysAllocateHandyPages: rc=%Rrc iFirst=%d cPages=%d\n", rc, iFirst, cPages));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("PGMR0PhysAllocateHandyPages: cPages=%d rc=%Rrc\n", cPages, rc));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Worker function for PGMR3PhysAllocateLargeHandyPage
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @returns The following VBox status codes.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @retval VINF_SUCCESS on success.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @retval VINF_EM_NO_MEMORY if we're out of memory.
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync * @param pVM The VM handle.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pVCpu The VMCPU handle.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @remarks Must be called from within the PGM critical section. The caller
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * must clear the new pages.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncVMMR0DECL(int) PGMR0PhysAllocateLargeHandyPage(PVM pVM, PVMCPU pVCpu)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(PDMCritSectIsOwnerEx(&pVM->pgm.s.CritSect, pVCpu->idCpu));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync int rc = GMMR0AllocateLargePage(pVM, pVCpu->idCpu, _2M, &pVM->pgm.s.aLargeHandyPage[0].idPage, &pVM->pgm.s.aLargeHandyPage[0].HCPhysGCPhys);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * #PF Handler for nested paging.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @returns VBox status code (appropriate for trap handling and GC return).
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * @param pVM VM Handle.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pVCpu VMCPU Handle.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param enmShwPagingMode Paging mode for the nested page tables
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param uErr The trap error code.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pRegFrame Trap register frame.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pvFault The fault address.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncVMMR0DECL(int) PGMR0Trap0eHandlerNestedPaging(PVM pVM, PVMCPU pVCpu, PGMMODE enmShwPagingMode, RTGCUINT uErr,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("PGMTrap0eHandler: uErr=%RGx pvFault=%RGp eip=%RGv\n", uErr, pvFault, (RTGCPTR)pRegFrame->rip));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_STATS({ pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution) = NULL; } );
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /* AMD uses the host's paging mode; Intel has a single mode (EPT). */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync AssertMsg( enmShwPagingMode == PGMMODE_32_BIT || enmShwPagingMode == PGMMODE_PAE || enmShwPagingMode == PGMMODE_PAE_NX
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync || enmShwPagingMode == PGMMODE_AMD64 || enmShwPagingMode == PGMMODE_AMD64_NX || enmShwPagingMode == PGMMODE_EPT,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Error code stats.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSNotPresentWrite);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSNotPresentRead);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSWrite);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSReserved);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSRead);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync { /* Supervisor */
7e8ef90d3160234df0f254131b87af4243d79476vboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVNotPresentWrite);
7e8ef90d3160234df0f254131b87af4243d79476vboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVNotPresentRead);
7e8ef90d3160234df0f254131b87af4243d79476vboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVWrite);
7e8ef90d3160234df0f254131b87af4243d79476vboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVReserved);
7e8ef90d3160234df0f254131b87af4243d79476vboxsync * Call the worker.
7e8ef90d3160234df0f254131b87af4243d79476vboxsync * Note! We pretend the guest is in protected mode without paging, so we
7e8ef90d3160234df0f254131b87af4243d79476vboxsync * can use existing code to build the nested page tables.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync bool fLockTaken = false;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync rc = PGM_BTH_NAME_32BIT_PROT(Trap0eHandler)(pVCpu, uErr, pRegFrame, pvFault, &fLockTaken);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync rc = PGM_BTH_NAME_PAE_PROT(Trap0eHandler)(pVCpu, uErr, pRegFrame, pvFault, &fLockTaken);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync rc = PGM_BTH_NAME_AMD64_PROT(Trap0eHandler)(pVCpu, uErr, pRegFrame, pvFault, &fLockTaken);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync rc = PGM_BTH_NAME_EPT_PROT(Trap0eHandler)(pVCpu, uErr, pRegFrame, pvFault, &fLockTaken);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /* Note: hack alert for difficult to reproduce problem. */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync else if ( rc == VERR_PAGE_NOT_PRESENT /* SMP only ; disassembly might fail. */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync || rc == VERR_PAGE_TABLE_NOT_PRESENT /* seen with UNI & SMP */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync || rc == VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT /* seen with SMP */
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync || rc == VERR_PAGE_MAP_LEVEL4_NOT_PRESENT) /* precaution */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Log(("WARNING: Unexpected VERR_PAGE_TABLE_NOT_PRESENT (%d) for page fault at %RGp error code %x (rip=%RGv)\n", rc, pvFault, uErr, pRegFrame->rip));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /* Some kind of inconsistency in the SMP case; it's safe to just execute the instruction again; not sure about
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync single VCPU VMs though. */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_STATS({ if (!pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution))
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution) = &pVCpu->pgm.s.StatRZTrap0eTime2Misc; });