PGMR0.cpp revision 7c9a5eca233baf6ede345ace077a00bd0b7af1ef
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/* $Id$ */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/** @file
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * PGM - Page Manager and Monitor, Ring-0.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/*
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Copyright (C) 2007-2010 Oracle Corporation
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
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
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/*******************************************************************************
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync* Header Files *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync*******************************************************************************/
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#define LOG_GROUP LOG_GROUP_PGM
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#include <VBox/pgm.h>
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync#include <VBox/gmm.h>
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#include "../PGMInternal.h"
7b9f0c34e9ea328981c99e97054bdf8684d9d620vboxsync#include <VBox/vm.h>
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#include "../PGMInline.h"
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#include <VBox/log.h>
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#include <VBox/err.h>
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#include <iprt/assert.h>
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync#include <iprt/mem.h>
7862f4bd000f1eb6c86289f5ac2849e9cf943ca9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
f9ce005e61f0fbb51a2cabc53d58c3485151faa9vboxsync/*
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Instantiate the ring-0 header/code templates.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#define PGM_BTH_NAME(name) PGM_BTH_NAME_32BIT_PROT(name)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#include "PGMR0Bth.h"
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#undef PGM_BTH_NAME
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_PROT(name)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#include "PGMR0Bth.h"
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#undef PGM_BTH_NAME
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#define PGM_BTH_NAME(name) PGM_BTH_NAME_AMD64_PROT(name)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#include "PGMR0Bth.h"
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#undef PGM_BTH_NAME
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_PROT(name)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#include "PGMR0Bth.h"
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#undef PGM_BTH_NAME
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/**
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Worker function for PGMR3PhysAllocateHandyPages and pgmPhysEnsureHandyPage.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
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.
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pVM The VM handle.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync * @param pVCpu The VMCPU handle.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync *
c142e0f462e6c921a73d84ee50f41396b3a99db5vboxsync * @remarks Must be called from within the PGM critical section. The caller
c142e0f462e6c921a73d84ee50f41396b3a99db5vboxsync * must clear the new pages.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncVMMR0DECL(int) PGMR0PhysAllocateHandyPages(PVM pVM, PVMCPU pVCpu)
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync{
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(PDMCritSectIsOwnerEx(&pVM->pgm.s.CritSect, pVCpu->idCpu));
907b6adfa052386a0666d5557bee9bdbc100c2e5vboxsync
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync /*
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync * Check for error injection.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync if (RT_UNLIKELY(pVM->pgm.s.fErrInjHandyPages))
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync return VERR_NO_MEMORY;
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /*
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Try allocate a full set of handy pages.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync uint32_t iFirst = pVM->pgm.s.cHandyPages;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync AssertReturn(iFirst <= RT_ELEMENTS(pVM->pgm.s.aHandyPages), VERR_INTERNAL_ERROR);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync uint32_t cPages = RT_ELEMENTS(pVM->pgm.s.aHandyPages) - iFirst;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync if (!cPages)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync return VINF_SUCCESS;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync int rc = GMMR0AllocateHandyPages(pVM, pVCpu->idCpu, cPages, cPages, &pVM->pgm.s.aHandyPages[iFirst]);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (RT_SUCCESS(rc))
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync for (uint32_t i = 0; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
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 }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pVM->pgm.s.cHandyPages = RT_ELEMENTS(pVM->pgm.s.aHandyPages);
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync }
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync else if (rc != VERR_GMM_SEED_ME)
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync {
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync if ( ( rc == VERR_GMM_HIT_GLOBAL_LIMIT
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync || rc == VERR_GMM_HIT_VM_ACCOUNT_LIMIT)
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync && iFirst < PGM_HANDY_PAGES_MIN)
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync {
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync#ifdef VBOX_STRICT
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync /* We're ASSUMING that GMM has updated all the entires before failing us. */
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync uint32_t i;
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync for (i = iFirst; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync {
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);
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync }
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync#endif
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync /*
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync * Reduce the number of pages until we hit the minimum limit.
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync */
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync do
ed9d3db07648c7e3a979105c15ad752ee9ea18devboxsync {
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync cPages >>= 2;
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync if (cPages + iFirst < PGM_HANDY_PAGES_MIN)
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync cPages = PGM_HANDY_PAGES_MIN - iFirst;
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync rc = GMMR0AllocateHandyPages(pVM, pVCpu->idCpu, cPages, cPages, &pVM->pgm.s.aHandyPages[iFirst]);
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync } while ( ( rc == VERR_GMM_HIT_GLOBAL_LIMIT
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync || rc == VERR_GMM_HIT_VM_ACCOUNT_LIMIT)
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync && cPages + iFirst > PGM_HANDY_PAGES_MIN);
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync if (RT_SUCCESS(rc))
d4a9d525e6f2111d462d2d96462dced6b9ec00efvboxsync {
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync#ifdef VBOX_STRICT
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync i = iFirst + cPages;
9523921c89c66f4bececdbd5ac95aed0039eda1bvboxsync while (i-- > 0)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
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 }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync for (i = cPages + iFirst; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
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 }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync#endif
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pVM->pgm.s.cHandyPages = iFirst + cPages;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (RT_FAILURE(rc) && rc != VERR_GMM_SEED_ME)
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogRel(("PGMR0PhysAllocateHandyPages: rc=%Rrc iFirst=%d cPages=%d\n", rc, iFirst, cPages));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync VM_FF_SET(pVM, VM_FF_PGM_NO_MEMORY);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("PGMR0PhysAllocateHandyPages: cPages=%d rc=%Rrc\n", cPages, rc));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync return rc;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync}
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/**
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Worker function for PGMR3PhysAllocateLargeHandyPage
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
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.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
5f2b03bf7695dabd71222dba123532a3f76828c1vboxsync * @param pVM The VM handle.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @param pVCpu The VMCPU handle.
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * @remarks Must be called from within the PGM critical section. The caller
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * must clear the new pages.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncVMMR0DECL(int) PGMR0PhysAllocateLargeHandyPage(PVM pVM, PVMCPU pVCpu)
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync{
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(PDMCritSectIsOwnerEx(&pVM->pgm.s.CritSect, pVCpu->idCpu));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync Assert(!pVM->pgm.s.cLargeHandyPages);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync int rc = GMMR0AllocateLargePage(pVM, pVCpu->idCpu, _2M, &pVM->pgm.s.aLargeHandyPage[0].idPage, &pVM->pgm.s.aLargeHandyPage[0].HCPhysGCPhys);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync if (RT_SUCCESS(rc))
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync pVM->pgm.s.cLargeHandyPages = 1;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync return rc;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync}
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/**
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * #PF Handler for nested paging.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync *
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.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncVMMR0DECL(int) PGMR0Trap0eHandlerNestedPaging(PVM pVM, PVMCPU pVCpu, PGMMODE enmShwPagingMode, RTGCUINT uErr,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS pvFault)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync{
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync int rc;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync LogFlow(("PGMTrap0eHandler: uErr=%RGx pvFault=%RGp eip=%RGv\n", uErr, pvFault, (RTGCPTR)pRegFrame->rip));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_PROFILE_START(&pVCpu->pgm.s.StatRZTrap0e, a);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_STATS({ pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution) = NULL; } );
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
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 ("enmShwPagingMode=%d\n", enmShwPagingMode));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync#ifdef VBOX_WITH_STATISTICS
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /*
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Error code stats.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (uErr & X86_TRAP_PF_US)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (!(uErr & X86_TRAP_PF_P))
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (uErr & X86_TRAP_PF_RW)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSNotPresentWrite);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync else
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSNotPresentRead);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync else if (uErr & X86_TRAP_PF_RW)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSWrite);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync else if (uErr & X86_TRAP_PF_RSVD)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSReserved);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync else if (uErr & X86_TRAP_PF_ID)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSNXE);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync else
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSRead);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync else
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync { /* Supervisor */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (!(uErr & X86_TRAP_PF_P))
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
7e8ef90d3160234df0f254131b87af4243d79476vboxsync if (uErr & X86_TRAP_PF_RW)
7e8ef90d3160234df0f254131b87af4243d79476vboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVNotPresentWrite);
7e8ef90d3160234df0f254131b87af4243d79476vboxsync else
7e8ef90d3160234df0f254131b87af4243d79476vboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVNotPresentRead);
7e8ef90d3160234df0f254131b87af4243d79476vboxsync }
7e8ef90d3160234df0f254131b87af4243d79476vboxsync else if (uErr & X86_TRAP_PF_RW)
7e8ef90d3160234df0f254131b87af4243d79476vboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVWrite);
7e8ef90d3160234df0f254131b87af4243d79476vboxsync else if (uErr & X86_TRAP_PF_ID)
7e8ef90d3160234df0f254131b87af4243d79476vboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSNXE);
7e8ef90d3160234df0f254131b87af4243d79476vboxsync else if (uErr & X86_TRAP_PF_RSVD)
7e8ef90d3160234df0f254131b87af4243d79476vboxsync STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVReserved);
7e8ef90d3160234df0f254131b87af4243d79476vboxsync }
7e8ef90d3160234df0f254131b87af4243d79476vboxsync#endif
7e8ef90d3160234df0f254131b87af4243d79476vboxsync
7e8ef90d3160234df0f254131b87af4243d79476vboxsync /*
7e8ef90d3160234df0f254131b87af4243d79476vboxsync * Call the worker.
7e8ef90d3160234df0f254131b87af4243d79476vboxsync *
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.
06ea6bcf23874b662d499b3f130024c98b2dd7a6vboxsync */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync bool fLockTaken = false;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync switch(enmShwPagingMode)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync {
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMMODE_32_BIT:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync rc = PGM_BTH_NAME_32BIT_PROT(Trap0eHandler)(pVCpu, uErr, pRegFrame, pvFault, &fLockTaken);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMMODE_PAE:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMMODE_PAE_NX:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync rc = PGM_BTH_NAME_PAE_PROT(Trap0eHandler)(pVCpu, uErr, pRegFrame, pvFault, &fLockTaken);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMMODE_AMD64:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync case PGMMODE_AMD64_NX:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync rc = PGM_BTH_NAME_AMD64_PROT(Trap0eHandler)(pVCpu, uErr, pRegFrame, pvFault, &fLockTaken);
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync break;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync case PGMMODE_EPT:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync rc = PGM_BTH_NAME_EPT_PROT(Trap0eHandler)(pVCpu, uErr, pRegFrame, pvFault, &fLockTaken);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync break;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync default:
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync AssertFailed();
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync rc = VERR_INVALID_PARAMETER;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync break;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync }
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync if (fLockTaken)
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync {
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync Assert(PGMIsLockOwner(pVM));
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pgmUnlock(pVM);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (rc == VINF_PGM_SYNCPAGE_MODIFIED_PDE)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync rc = VINF_SUCCESS;
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 */
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync {
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 rc = VINF_SUCCESS;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync }
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_STATS({ if (!pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution))
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution) = &pVCpu->pgm.s.StatRZTrap0eTime2Misc; });
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync STAM_PROFILE_STOP_EX(&pVCpu->pgm.s.StatRZTrap0e, pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution), a);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync return rc;
702a8ee2dc1de96f2f77e97135015d3e243186fdvboxsync}
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync