PGMR0.cpp revision 6f516ad9911d9037a18778742caa955fe362f8ff
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync/* $Id$ */
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync/** @file
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * PGM - Page Manager and Monitor, Ring-0.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync */
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync/*
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Copyright (C) 2007 Sun Microsystems, Inc.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync *
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 *
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
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync/*******************************************************************************
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync* Header Files *
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync*******************************************************************************/
f84cd77241a1c4b9106a92280611c659243e10d1vboxsync#define LOG_GROUP LOG_GROUP_PGM
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#include <VBox/pgm.h>
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#include "PGMInternal.h"
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#include <VBox/vm.h>
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#include <VBox/log.h>
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#include <VBox/err.h>
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#include <iprt/assert.h>
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync__BEGIN_DECLS
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#define PGM_BTH_NAME(name) PGM_BTH_NAME_32BIT_PROT(name)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#include "PGMR0Bth.h"
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync#undef PGM_BTH_NAME
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_PROT(name)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#include "PGMR0Bth.h"
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync#undef PGM_BTH_NAME
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#define PGM_BTH_NAME(name) PGM_BTH_NAME_AMD64_PROT(name)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#include "PGMR0Bth.h"
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync#undef PGM_BTH_NAME
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync__END_DECLS
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync/**
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * Worker function for PGMR3PhysAllocateHandyPages and pgmPhysEnsureHandyPage.
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync *
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.
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync *
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * @param pVM The VM handle.
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync *
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * @remarks Must be called from within the PGM critical section.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync */
9ad5e3912962c3dbccc1afc4e7d62890fe906814vboxsyncVMMR0DECL(int) PGMR0PhysAllocateHandyPages(PVM pVM)
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync{
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync return VERR_NOT_IMPLEMENTED;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync}
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync/**
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * #PF Handler for nested paging.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync *
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @returns VBox status code (appropriate for trap handling and GC return).
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @param pVM VM 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.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync */
9ad5e3912962c3dbccc1afc4e7d62890fe906814vboxsyncVMMR0DECL(int) PGMR0Trap0eHandlerNestedPaging(PVM pVM, PGMMODE enmShwPagingMode, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPHYS pvFault)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync{
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync int rc;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
d32c860c64e340970271b4113a6a67cad64460b4vboxsync LogFlow(("PGMTrap0eHandler: uErr=%#x pvFault=%VGp eip=%VGv\n", uErr, pvFault, pRegFrame->rip));
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_PROFILE_START(&pVM->pgm.s.StatGCTrap0e, a);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_STATS({ pVM->pgm.s.CTXSUFF(pStatTrap0eAttribution) = NULL; } );
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync /* AMD uses the host's paging mode; Intel's version is on the todo list */
28d7c24dda3ad9c1c47a3f77454193b1a48da852vboxsync AssertMsg(enmShwPagingMode == PGMMODE_32_BIT || enmShwPagingMode == PGMMODE_PAE || enmShwPagingMode == PGMMODE_PAE_NX || enmShwPagingMode == PGMMODE_AMD64 || enmShwPagingMode == PGMMODE_AMD64_NX, ("enmShwPagingMode=%d\n", enmShwPagingMode));
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#ifdef VBOX_WITH_STATISTICS
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync /*
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * Error code stats.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync */
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync if (uErr & X86_TRAP_PF_US)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync {
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync if (!(uErr & X86_TRAP_PF_P))
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync {
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync if (uErr & X86_TRAP_PF_RW)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatGCTrap0eUSNotPresentWrite);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync else
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatGCTrap0eUSNotPresentRead);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync }
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync else if (uErr & X86_TRAP_PF_RW)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatGCTrap0eUSWrite);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync else if (uErr & X86_TRAP_PF_RSVD)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatGCTrap0eUSReserved);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync else if (uErr & X86_TRAP_PF_ID)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatGCTrap0eUSNXE);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync else
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatGCTrap0eUSRead);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync }
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync else
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync { /* Supervisor */
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync if (!(uErr & X86_TRAP_PF_P))
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync {
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync if (uErr & X86_TRAP_PF_RW)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatGCTrap0eSVNotPresentWrite);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync else
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatGCTrap0eSVNotPresentRead);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync }
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync else if (uErr & X86_TRAP_PF_RW)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatGCTrap0eSVWrite);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync else if (uErr & X86_TRAP_PF_ID)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatGCTrap0eSNXE);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync else if (uErr & X86_TRAP_PF_RSVD)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatGCTrap0eSVReserved);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync }
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#endif
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync /*
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * Call the worker.
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync *
9ad5e3912962c3dbccc1afc4e7d62890fe906814vboxsync * We pretend the guest is in protected mode without paging, so we can use existing code to build the
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync * nested page tables.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync */
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync switch(enmShwPagingMode)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync {
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync case PGMMODE_32_BIT:
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync rc = PGM_BTH_NAME_32BIT_PROT(Trap0eHandler)(pVM, uErr, pRegFrame, pvFault);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync break;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync case PGMMODE_PAE:
50fdc90dae026b2086f85b0f028aa63dd6bbe14evboxsync case PGMMODE_PAE_NX:
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync rc = PGM_BTH_NAME_PAE_PROT(Trap0eHandler)(pVM, uErr, pRegFrame, pvFault);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync break;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync case PGMMODE_AMD64:
50fdc90dae026b2086f85b0f028aa63dd6bbe14evboxsync case PGMMODE_AMD64_NX:
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync rc = PGM_BTH_NAME_AMD64_PROT(Trap0eHandler)(pVM, uErr, pRegFrame, pvFault);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync break;
50fdc90dae026b2086f85b0f028aa63dd6bbe14evboxsync default:
50fdc90dae026b2086f85b0f028aa63dd6bbe14evboxsync AssertFailed();
e8ac7dce6d625856c57792a6af738e2fe2667264vboxsync rc = VERR_INVALID_PARAMETER;
50fdc90dae026b2086f85b0f028aa63dd6bbe14evboxsync break;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync }
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync if (rc == VINF_PGM_SYNCPAGE_MODIFIED_PDE)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync rc = VINF_SUCCESS;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_STATS({ if (!pVM->pgm.s.CTXSUFF(pStatTrap0eAttribution))
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync pVM->pgm.s.CTXSUFF(pStatTrap0eAttribution) = &pVM->pgm.s.StatTrap0eMisc; });
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.StatGCTrap0e, pVM->pgm.s.CTXSUFF(pStatTrap0eAttribution), a);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync return rc;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync}
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync/** darwin stub */
6f516ad9911d9037a18778742caa955fe362f8ffvboxsyncVMMR0DECL(int) PGMR0DynMapGCPage(PVM pVM, RTGCPHYS GCPhys, void **ppv)
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync{
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync Assert(!(GCPhys & PAGE_OFFSET));
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync return VERR_NOT_IMPLEMENTED;
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync}
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync/** darwin stub */
6f516ad9911d9037a18778742caa955fe362f8ffvboxsyncVMMR0DECL(int) PGMR0DynMapGCPageEx(PVM pVM, RTGCPHYS GCPhys, void **ppv)
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync{
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync return VERR_NOT_IMPLEMENTED;
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync}
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync/** darwin stub */
6f516ad9911d9037a18778742caa955fe362f8ffvboxsyncVMMR0DECL(int) PGMR0DynMapHCPage(PVM pVM, RTHCPHYS HCPhys, void **ppv)
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync{
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync return VERR_NOT_IMPLEMENTED;
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync}
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync#endif /* VBOX_WITH_2X_4GB_ADDR_SPACE */
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync