PGMAllPool.cpp revision 0e907bfa48d3b7f7ab8c2ec437eb9871fcc5c104
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync/* $Id$ */
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync/** @file
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * PGM Shadow Page Pool.
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync */
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync/*
c58f1213e628a545081c70e26c6b67a841cff880vboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync *
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * available from http://www.virtualbox.org. This file is free software;
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * you can redistribute it and/or modify it under the terms of the GNU
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * General Public License (GPL) as published by the Free Software
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync *
0d12c7f9423f2745f8e282523d0930f91bff03b3vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
0d12c7f9423f2745f8e282523d0930f91bff03b3vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
0d12c7f9423f2745f8e282523d0930f91bff03b3vboxsync * additional information or have any questions.
0d12c7f9423f2745f8e282523d0930f91bff03b3vboxsync */
0d12c7f9423f2745f8e282523d0930f91bff03b3vboxsync
0d12c7f9423f2745f8e282523d0930f91bff03b3vboxsync
0d12c7f9423f2745f8e282523d0930f91bff03b3vboxsync/*******************************************************************************
0d12c7f9423f2745f8e282523d0930f91bff03b3vboxsync* Header Files *
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync*******************************************************************************/
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync#define LOG_GROUP LOG_GROUP_PGM_POOL
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync#include <VBox/pgm.h>
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync#include <VBox/mm.h>
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync#include <VBox/em.h>
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync#include <VBox/cpum.h>
032a52c5b2984e26e84c2961f8f7f98a3954c8f2vboxsync#ifdef IN_GC
6ba706e9f431401d425d16817fdcd6316f83b584vboxsync# include <VBox/patm.h>
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync#endif
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsync#include "PGMInternal.h"
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync#include <VBox/vm.h>
45c3e41d012100c5f4a3f77e391e4c6da8f5b97avboxsync#include <VBox/disopcode.h>
45c3e41d012100c5f4a3f77e391e4c6da8f5b97avboxsync
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync#include <VBox/log.h>
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync#include <VBox/err.h>
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync#include <iprt/asm.h>
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync/*******************************************************************************
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync* Internal Functions *
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync*******************************************************************************/
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync__BEGIN_DECLS
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsyncstatic void pgmPoolFlushAllInt(PPGMPOOL pPool);
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync#ifdef PGMPOOL_WITH_USER_TRACKING
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsyncDECLINLINE(unsigned) pgmPoolTrackGetShadowEntrySize(PGMPOOLKIND enmKind);
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsyncDECLINLINE(unsigned) pgmPoolTrackGetGuestEntrySize(PGMPOOLKIND enmKind);
c2046db2cc346cc299f0cd9b2d1e160179159cfcvboxsyncstatic void pgmPoolTrackDeref(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync#endif
c2046db2cc346cc299f0cd9b2d1e160179159cfcvboxsync#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsyncstatic void pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint);
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync#endif
c2046db2cc346cc299f0cd9b2d1e160179159cfcvboxsync#ifdef PGMPOOL_WITH_CACHE
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsyncstatic int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable);
c2046db2cc346cc299f0cd9b2d1e160179159cfcvboxsync#endif
c2046db2cc346cc299f0cd9b2d1e160179159cfcvboxsync#ifdef PGMPOOL_WITH_MONITORING
c2046db2cc346cc299f0cd9b2d1e160179159cfcvboxsyncstatic void pgmPoolMonitorModifiedRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
c2046db2cc346cc299f0cd9b2d1e160179159cfcvboxsync#endif
c2046db2cc346cc299f0cd9b2d1e160179159cfcvboxsync#ifndef IN_RING3
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsyncDECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser);
044af0d1e6474076366759db86f101778c5f20ccvboxsync#endif
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync__END_DECLS
2d8870843ff566fee9bd3a6a5942414254106479vboxsync
2d8870843ff566fee9bd3a6a5942414254106479vboxsync
2d8870843ff566fee9bd3a6a5942414254106479vboxsync/**
2d8870843ff566fee9bd3a6a5942414254106479vboxsync * Checks if the specified page pool kind is for a 4MB or 2MB guest page.
2d8870843ff566fee9bd3a6a5942414254106479vboxsync *
2d8870843ff566fee9bd3a6a5942414254106479vboxsync * @returns true if it's the shadow of a 4MB or 2MB guest page, otherwise false.
2d8870843ff566fee9bd3a6a5942414254106479vboxsync * @param enmKind The page kind.
2d8870843ff566fee9bd3a6a5942414254106479vboxsync */
2d8870843ff566fee9bd3a6a5942414254106479vboxsyncDECLINLINE(bool) pgmPoolIsBigPage(PGMPOOLKIND enmKind)
2d8870843ff566fee9bd3a6a5942414254106479vboxsync{
2d8870843ff566fee9bd3a6a5942414254106479vboxsync switch (enmKind)
2d8870843ff566fee9bd3a6a5942414254106479vboxsync {
044af0d1e6474076366759db86f101778c5f20ccvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
044af0d1e6474076366759db86f101778c5f20ccvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
044af0d1e6474076366759db86f101778c5f20ccvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
044af0d1e6474076366759db86f101778c5f20ccvboxsync return true;
044af0d1e6474076366759db86f101778c5f20ccvboxsync default:
044af0d1e6474076366759db86f101778c5f20ccvboxsync return false;
044af0d1e6474076366759db86f101778c5f20ccvboxsync }
044af0d1e6474076366759db86f101778c5f20ccvboxsync}
044af0d1e6474076366759db86f101778c5f20ccvboxsync
044af0d1e6474076366759db86f101778c5f20ccvboxsync
044af0d1e6474076366759db86f101778c5f20ccvboxsync#ifdef IN_GC
044af0d1e6474076366759db86f101778c5f20ccvboxsync/**
044af0d1e6474076366759db86f101778c5f20ccvboxsync * Maps a pool page into the current context.
044af0d1e6474076366759db86f101778c5f20ccvboxsync *
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @returns Pointer to the mapping.
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @param pVM The VM handle.
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @param pPage The page to map.
044af0d1e6474076366759db86f101778c5f20ccvboxsync */
044af0d1e6474076366759db86f101778c5f20ccvboxsyncvoid *pgmGCPoolMapPage(PVM pVM, PPGMPOOLPAGE pPage)
044af0d1e6474076366759db86f101778c5f20ccvboxsync{
044af0d1e6474076366759db86f101778c5f20ccvboxsync /* general pages. */
044af0d1e6474076366759db86f101778c5f20ccvboxsync if (pPage->idx >= PGMPOOL_IDX_FIRST)
044af0d1e6474076366759db86f101778c5f20ccvboxsync {
044af0d1e6474076366759db86f101778c5f20ccvboxsync Assert(pPage->idx < pVM->pgm.s.pPoolGC->cCurPages);
044af0d1e6474076366759db86f101778c5f20ccvboxsync void *pv;
044af0d1e6474076366759db86f101778c5f20ccvboxsync int rc = PGMGCDynMapHCPage(pVM, pPage->Core.Key, &pv);
044af0d1e6474076366759db86f101778c5f20ccvboxsync AssertReleaseRC(rc);
044af0d1e6474076366759db86f101778c5f20ccvboxsync return pv;
044af0d1e6474076366759db86f101778c5f20ccvboxsync }
044af0d1e6474076366759db86f101778c5f20ccvboxsync
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync /* special pages. */
044af0d1e6474076366759db86f101778c5f20ccvboxsync switch (pPage->idx)
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync {
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync case PGMPOOL_IDX_PD:
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync return pVM->pgm.s.pGC32BitPD;
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync case PGMPOOL_IDX_PAE_PD:
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync case PGMPOOL_IDX_PAE_PD_0:
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync return pVM->pgm.s.apGCPaePDs[0];
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync case PGMPOOL_IDX_PAE_PD_1:
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync return pVM->pgm.s.apGCPaePDs[1];
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync case PGMPOOL_IDX_PAE_PD_2:
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync return pVM->pgm.s.apGCPaePDs[2];
044af0d1e6474076366759db86f101778c5f20ccvboxsync case PGMPOOL_IDX_PAE_PD_3:
044af0d1e6474076366759db86f101778c5f20ccvboxsync return pVM->pgm.s.apGCPaePDs[3];
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync case PGMPOOL_IDX_PDPT:
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync return pVM->pgm.s.pGCPaePDPT;
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync default:
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync AssertReleaseMsgFailed(("Invalid index %d\n", pPage->idx));
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync return NULL;
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync }
044af0d1e6474076366759db86f101778c5f20ccvboxsync}
044af0d1e6474076366759db86f101778c5f20ccvboxsync#endif /* IN_GC */
044af0d1e6474076366759db86f101778c5f20ccvboxsync
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync#ifdef PGMPOOL_WITH_MONITORING
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync/**
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * Determin the size of a write instruction.
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * @returns number of bytes written.
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * @param pDis The disassembler state.
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync */
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsyncstatic unsigned pgmPoolDisasWriteSize(PDISCPUSTATE pDis)
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync{
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync /*
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync * This is very crude and possibly wrong for some opcodes,
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync * but since it's not really supposed to be called we can
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync * probably live with that.
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync */
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync return DISGetParamSize(pDis, &pDis->param1);
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync}
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync/**
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync * Flushes a chain of pages sharing the same access monitor.
a1df400bbe9d64aad400442e56eb637019300a5evboxsync *
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync * @returns VBox status code suitable for scheduling.
a1df400bbe9d64aad400442e56eb637019300a5evboxsync * @param pPool The pool.
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync * @param pPage A page in the chain.
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync */
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsyncint pgmPoolMonitorChainFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync{
a1df400bbe9d64aad400442e56eb637019300a5evboxsync LogFlow(("pgmPoolMonitorChainFlush: Flush page %VGp type=%d\n", pPage->GCPhys, pPage->enmKind));
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync
a1df400bbe9d64aad400442e56eb637019300a5evboxsync /*
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync * Find the list head.
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync */
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync uint16_t idx = pPage->idx;
044af0d1e6474076366759db86f101778c5f20ccvboxsync if (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync {
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync while (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync {
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync idx = pPage->iMonitoredPrev;
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync Assert(idx != pPage->idx);
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync pPage = &pPool->aPages[idx];
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync }
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync }
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync /*
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * Iterate the list flushing each shadow page.
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync */
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync int rc = VINF_SUCCESS;
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync for (;;)
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync {
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync idx = pPage->iMonitoredNext;
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync Assert(idx != pPage->idx);
044af0d1e6474076366759db86f101778c5f20ccvboxsync if (pPage->idx >= PGMPOOL_IDX_FIRST)
044af0d1e6474076366759db86f101778c5f20ccvboxsync {
044af0d1e6474076366759db86f101778c5f20ccvboxsync int rc2 = pgmPoolFlushPage(pPool, pPage);
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync if (rc2 == VERR_PGM_POOL_CLEARED && rc == VINF_SUCCESS)
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync rc = VINF_PGM_SYNC_CR3;
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync }
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync /* next */
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync if (idx == NIL_PGMPOOL_IDX)
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync break;
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync pPage = &pPool->aPages[idx];
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync }
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync return rc;
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync}
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync/**
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync * Wrapper for getting the current context pointer to the entry being modified.
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync *
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync * @returns Pointer to the current context mapping of the entry.
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync * @param pPool The pool.
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync * @param pvFault The fault virtual address.
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync * @param GCPhysFault The fault physical address.
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync * @param cbEntry The entry size.
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync */
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync#ifdef IN_RING3
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsyncDECLINLINE(const void *) pgmPoolMonitorGCPtr2CCPtr(PPGMPOOL pPool, RTHCPTR pvFault, RTGCPHYS GCPhysFault, const unsigned cbEntry)
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync#else
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsyncDECLINLINE(const void *) pgmPoolMonitorGCPtr2CCPtr(PPGMPOOL pPool, RTGCPTR pvFault, RTGCPHYS GCPhysFault, const unsigned cbEntry)
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync#endif
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync{
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync#ifdef IN_GC
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync return (const void *)((RTGCUINTPTR)pvFault & ~(RTGCUINTPTR)(cbEntry - 1));
7d80dfbe5d66fc4c6de6fe109ce96a081496dcd4vboxsync
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync#elif defined(IN_RING0)
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync void *pvRet;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync int rc = pgmRamGCPhys2HCPtr(&pPool->pVMHC->pgm.s, GCPhysFault & ~(RTGCPHYS)(cbEntry - 1), &pvRet);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync AssertFatalRCSuccess(rc);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync return pvRet;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync#elif defined(IN_RING3)
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync return (RTHCPTR)((uintptr_t)pvFault & ~(RTHCUINTPTR)(cbEntry - 1));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync#else
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync# error "huh?"
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync#endif
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync}
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync/**
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * Process shadow entries before they are changed by the guest.
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync *
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * For PT entries we will clear them. For PD entries, we'll simply check
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * for mapping conflicts and set the SyncCR3 FF if found.
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync *
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * @param pPool The pool.
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * @param pPage The head page.
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * @param GCPhysFault The guest physical fault address.
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * @param uAddress In R0 and GC this is the guest context fault address (flat).
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * In R3 this is the host context 'fault' address.
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * @param pCpu The disassembler state for figuring out the write size.
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * This need not be specified if the caller knows we won't do cross entry accesses.
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync */
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync#ifdef IN_RING3
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsyncvoid pgmPoolMonitorChainChanging(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, RTHCPTR pvAddress, PDISCPUSTATE pCpu)
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync#else
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsyncvoid pgmPoolMonitorChainChanging(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, RTGCPTR pvAddress, PDISCPUSTATE pCpu)
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync#endif
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync{
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync const unsigned off = GCPhysFault & PAGE_OFFSET_MASK;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync const unsigned cbWrite = (pCpu) ? pgmPoolDisasWriteSize(pCpu) : 0;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync LogFlow(("pgmPoolMonitorChainChanging: %VGv phys=%VGp kind=%d cbWrite=%d\n", pvAddress, GCPhysFault, pPage->enmKind, cbWrite));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync for (;;)
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync {
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync union
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync {
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync void *pv;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync PX86PT pPT;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync PX86PTPAE pPTPae;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync PX86PD pPD;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync PX86PDPAE pPDPae;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync PX86PDPT pPDPT;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync PX86PML4 pPML4;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync } uShw;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTXSUFF(pVM), pPage);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync switch (pPage->enmKind)
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync {
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync {
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync const unsigned iShw = off / sizeof(X86PTE);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync if (uShw.pPT->a[iShw].n.u1Present)
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync {
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync PCX86PTE pGstPte = (PCX86PTE)pgmPoolMonitorGCPtr2CCPtr(pPool, pvAddress, GCPhysFault, sizeof(*pGstPte));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync Log4(("pgmPoolMonitorChainChanging 32_32: deref %VHp GCPhys %VGp\n", uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK, pGstPte->u & X86_PTE_PG_MASK));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage,
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK,
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync pGstPte->u & X86_PTE_PG_MASK);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync# endif
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync uShw.pPT->a[iShw].u = 0;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync }
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync break;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync }
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync /* page/2 sized */
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync if (!((off ^ pPage->GCPhys) & (PAGE_SIZE / 2)))
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync {
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync const unsigned iShw = (off / sizeof(X86PTE)) & (X86_PG_PAE_ENTRIES - 1);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync if (uShw.pPTPae->a[iShw].n.u1Present)
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync {
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync PCX86PTE pGstPte = (PCX86PTE)pgmPoolMonitorGCPtr2CCPtr(pPool, pvAddress, GCPhysFault, sizeof(*pGstPte));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync Log4(("pgmPoolMonitorChainChanging pae_32: deref %VHp GCPhys %VGp\n", uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK, pGstPte->u & X86_PTE_PG_MASK));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage,
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync uShw.pPTPae->a[iShw].u & X86_PTE_PAE_PG_MASK,
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync pGstPte->u & X86_PTE_PG_MASK);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync# endif
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync uShw.pPTPae->a[iShw].u = 0;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync }
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync }
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync break;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync {
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync const unsigned iShw = off / sizeof(X86PTEPAE);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync if (uShw.pPTPae->a[iShw].n.u1Present)
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync {
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync PCX86PTEPAE pGstPte = (PCX86PTEPAE)pgmPoolMonitorGCPtr2CCPtr(pPool, pvAddress, GCPhysFault, sizeof(*pGstPte));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync Log4(("pgmPoolMonitorChainChanging pae: deref %VHp GCPhys %VGp\n", uShw.pPTPae->a[iShw].u & X86_PTE_PAE_PG_MASK, pGstPte->u & X86_PTE_PAE_PG_MASK));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage,
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync uShw.pPTPae->a[iShw].u & X86_PTE_PAE_PG_MASK,
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync pGstPte->u & X86_PTE_PAE_PG_MASK);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync# endif
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync uShw.pPTPae->a[iShw].u = 0;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync }
0109a2240391a89f6556b1545e6cc57f9efab060vboxsync
0109a2240391a89f6556b1545e6cc57f9efab060vboxsync /* paranoia / a bit assumptive. */
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync if ( pCpu
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync && (off & 7)
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync && (off & 7) + cbWrite > sizeof(X86PTEPAE))
0109a2240391a89f6556b1545e6cc57f9efab060vboxsync {
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTEPAE);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync AssertReturnVoid(iShw2 < ELEMENTS(uShw.pPTPae->a));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync if (uShw.pPTPae->a[iShw2].n.u1Present)
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync {
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync PCX86PTEPAE pGstPte = (PCX86PTEPAE)pgmPoolMonitorGCPtr2CCPtr(pPool, pvAddress, GCPhysFault, sizeof(*pGstPte));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync Log4(("pgmPoolMonitorChainChanging pae: deref %VHp GCPhys %VGp\n", uShw.pPTPae->a[iShw2].u & X86_PTE_PAE_PG_MASK, pGstPte->u & X86_PTE_PAE_PG_MASK));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync pgmPoolTracDerefGCPhysHint(pPool, pPage,
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync uShw.pPTPae->a[iShw2].u & X86_PTE_PAE_PG_MASK,
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync pGstPte->u & X86_PTE_PAE_PG_MASK);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync# endif
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync uShw.pPTPae->a[iShw2].u = 0;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync }
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync }
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync break;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync }
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync case PGMPOOLKIND_ROOT_32BIT_PD:
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync {
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync const unsigned iShw = off / sizeof(X86PTE); // ASSUMING 32-bit guest paging!
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync if (uShw.pPD->a[iShw].u & PGM_PDFLAGS_MAPPING)
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync {
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync }
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync /* paranoia / a bit assumptive. */
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync else if ( pCpu
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync && (off & 3)
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync && (off & 3) + cbWrite > sizeof(X86PTE))
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync {
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTE);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync if ( iShw2 != iShw
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync && iShw2 < ELEMENTS(uShw.pPD->a)
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync && uShw.pPD->a[iShw2].u & PGM_PDFLAGS_MAPPING)
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync {
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync }
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync }
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync#if 0 /* useful when running PGMAssertCR3(), a bit too troublesome for general use (TLBs). */
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync if ( uShw.pPD->a[iShw].n.u1Present
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync && !VM_FF_ISSET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3))
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync {
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync LogFlow(("pgmPoolMonitorChainChanging: iShw=%#x: %RX32 -> freeing it!\n", iShw, uShw.pPD->a[iShw].u));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync# ifdef IN_GC /* TLB load - we're pushing things a bit... */
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync ASMProbeReadByte(pvAddress);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync# endif
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync pgmPoolFree(pPool->CTXSUFF(pVM), uShw.pPD->a[iShw].u & X86_PDE_PG_MASK, pPage->idx, iShw);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync uShw.pPD->a[iShw].u = 0;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync }
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync#endif
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync break;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync }
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync case PGMPOOLKIND_ROOT_PAE_PD:
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync {
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync unsigned iShw = (off / sizeof(X86PTE)) * 2; // ASSUMING 32-bit guest paging!
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync for (unsigned i = 0; i < 2; i++, iShw++)
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync {
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync if ((uShw.pPDPae->a[iShw].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync {
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync }
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync /* paranoia / a bit assumptive. */
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync else if ( pCpu
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync && (off & 3)
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync && (off & 3) + cbWrite > 4)
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync {
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync const unsigned iShw2 = iShw + 2;
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync if ( iShw2 < ELEMENTS(uShw.pPDPae->a)
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync && (uShw.pPDPae->a[iShw2].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync {
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync }
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync }
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync#if 0 /* useful when running PGMAssertCR3(), a bit too troublesome for general use (TLBs). */
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync if ( uShw.pPDPae->a[iShw].n.u1Present
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync && !VM_FF_ISSET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3))
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync {
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync LogFlow(("pgmPoolMonitorChainChanging: iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync# ifdef IN_GC /* TLB load - we're pushing things a bit... */
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync ASMProbeReadByte(pvAddress);
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync# endif
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync pgmPoolFree(pPool->CTXSUFF(pVM), uShw.pPDPae->a[iShw].u & X86_PDE_PAE_PG_MASK, pPage->idx, iShw);
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync uShw.pPDPae->a[iShw].u = 0;
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync }
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync#endif
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync }
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync break;
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync }
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync {
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync const unsigned iShw = off / sizeof(X86PDEPAE);
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync if (uShw.pPDPae->a[iShw].u & PGM_PDFLAGS_MAPPING)
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync {
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync }
9af7167fa39f1f139899c989da973e59b9cccc3fvboxsync#ifdef PGMPOOL_INVALIDATE_UPPER_SHADOW_TABLE_ENTRIES
9af7167fa39f1f139899c989da973e59b9cccc3fvboxsync /* causes trouble when the guest uses a PDE to refer to the whole page table level structure. (invalidate here; faults later on when it tries
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync * to change the page table entries
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync * -> recheck; probably only applies to the GC case
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync */
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync else
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync {
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync if (uShw.pPDPae->a[iShw].n.u1Present)
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync {
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync pgmPoolFree(pPool->CTXSUFF(pVM),
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync uShw.pPDPae->a[iShw].u & X86_PDE_PAE_PG_MASK,
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync /* Note: hardcoded PAE implementation dependency */
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync (pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD) ? PGMPOOL_IDX_PAE_PD : pPage->idx,
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync (pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD) ? iShw + (pPage->idx - PGMPOOL_IDX_PAE_PD_0) * X86_PG_PAE_ENTRIES : iShw);
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync uShw.pPDPae->a[iShw].u = 0;
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync }
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync }
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync#endif
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync /* paranoia / a bit assumptive. */
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync if ( pCpu
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync && (off & 7)
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync && (off & 7) + cbWrite > sizeof(X86PDEPAE))
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync {
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync AssertReturnVoid(iShw2 < ELEMENTS(uShw.pPDPae->a));
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync
6ba706e9f431401d425d16817fdcd6316f83b584vboxsync if ( iShw2 != iShw
6ba706e9f431401d425d16817fdcd6316f83b584vboxsync && uShw.pPDPae->a[iShw2].u & PGM_PDFLAGS_MAPPING)
6ba706e9f431401d425d16817fdcd6316f83b584vboxsync {
d6f8b76ab3b2ec0c270c96f9db6e2568fc41b5fevboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync }
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync#ifdef PGMPOOL_INVALIDATE_UPPER_SHADOW_TABLE_ENTRIES
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync else
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync if (uShw.pPDPae->a[iShw2].n.u1Present)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync pgmPoolFree(pPool->CTXSUFF(pVM),
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync uShw.pPDPae->a[iShw2].u & X86_PDE_PAE_PG_MASK,
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync /* Note: hardcoded PAE implementation dependency */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync (pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD) ? PGMPOOL_IDX_PAE_PD : pPage->idx,
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync (pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD) ? iShw2 + (pPage->idx - PGMPOOL_IDX_PAE_PD_0) * X86_PG_PAE_ENTRIES : iShw2);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync uShw.pPDPae->a[iShw2].u = 0;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync#endif
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync break;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync case PGMPOOLKIND_ROOT_PDPT:
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync /* Hopefully this doesn't happen very often:
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * - touching unused parts of the page
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * - messing with the bits of pd pointers without changing the physical address
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync const unsigned iShw = off / sizeof(X86PDPE);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync if (iShw < X86_PG_PAE_PDPE_ENTRIES) /* don't use ELEMENTS(uShw.pPDPT->a), because that's for long mode only */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync if (uShw.pPDPT->a[iShw].u & PGM_PLXFLAGS_MAPPING)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync /* paranoia / a bit assumptive. */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync else if ( pCpu
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync && (off & 7)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync && (off & 7) + cbWrite > sizeof(X86PDPE))
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDPE);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync if ( iShw2 != iShw
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync && iShw2 < X86_PG_PAE_PDPE_ENTRIES
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync && uShw.pPDPT->a[iShw2].u & PGM_PLXFLAGS_MAPPING)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync }
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync }
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync }
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync break;
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync }
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync#ifndef IN_GC
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync {
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync Assert(pPage->enmKind == PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD);
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync const unsigned iShw = off / sizeof(X86PDEPAE);
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync if (uShw.pPDPae->a[iShw].u & PGM_PDFLAGS_MAPPING)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync else
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync {
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync if (uShw.pPDPae->a[iShw].n.u1Present)
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync pgmPoolFree(pPool->CTXSUFF(pVM),
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync uShw.pPDPae->a[iShw].u & X86_PDE_PAE_PG_MASK,
31771163041e3661403a806eb3382d2a165c003bvboxsync pPage->idx,
31771163041e3661403a806eb3382d2a165c003bvboxsync iShw);
31771163041e3661403a806eb3382d2a165c003bvboxsync uShw.pPDPae->a[iShw].u = 0;
31771163041e3661403a806eb3382d2a165c003bvboxsync }
31771163041e3661403a806eb3382d2a165c003bvboxsync }
31771163041e3661403a806eb3382d2a165c003bvboxsync /* paranoia / a bit assumptive. */
31771163041e3661403a806eb3382d2a165c003bvboxsync if ( pCpu
31771163041e3661403a806eb3382d2a165c003bvboxsync && (off & 7)
31771163041e3661403a806eb3382d2a165c003bvboxsync && (off & 7) + cbWrite > sizeof(X86PDEPAE))
31771163041e3661403a806eb3382d2a165c003bvboxsync {
31771163041e3661403a806eb3382d2a165c003bvboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
31771163041e3661403a806eb3382d2a165c003bvboxsync AssertReturnVoid(iShw2 < ELEMENTS(uShw.pPDPae->a));
31771163041e3661403a806eb3382d2a165c003bvboxsync
31771163041e3661403a806eb3382d2a165c003bvboxsync if ( iShw2 != iShw
31771163041e3661403a806eb3382d2a165c003bvboxsync && uShw.pPDPae->a[iShw2].u & PGM_PDFLAGS_MAPPING)
31771163041e3661403a806eb3382d2a165c003bvboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync Assert(pgmMapAreMappingsEnabled(&pPool->CTXSUFF(pVM)->pgm.s));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync else
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync if (uShw.pPDPae->a[iShw2].n.u1Present)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync {
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync pgmPoolFree(pPool->CTXSUFF(pVM),
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync uShw.pPDPae->a[iShw2].u & X86_PDE_PAE_PG_MASK,
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync pPage->idx,
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync iShw2);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync uShw.pPDPae->a[iShw2].u = 0;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync break;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync {
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync /* Hopefully this doesn't happen very often:
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * - messing with the bits of pd pointers without changing the physical address
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync if (!VM_FF_ISSET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3))
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync const unsigned iShw = off / sizeof(X86PDPE);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync if (uShw.pPDPT->a[iShw].n.u1Present)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPT->a[iShw].u));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync pgmPoolFree(pPool->CTXSUFF(pVM), uShw.pPDPT->a[iShw].u & X86_PDPE_PG_MASK, pPage->idx, iShw);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync uShw.pPDPT->a[iShw].u = 0;
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync }
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync /* paranoia / a bit assumptive. */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync if ( pCpu
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync && (off & 7)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync && (off & 7) + cbWrite > sizeof(X86PDPE))
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDPE);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync if (uShw.pPDPT->a[iShw2].n.u1Present)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPT->a[iShw2].u));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync pgmPoolFree(pPool->CTXSUFF(pVM), uShw.pPDPT->a[iShw2].u & X86_PDPE_PG_MASK, pPage->idx, iShw2);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync uShw.pPDPT->a[iShw2].u = 0;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync }
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync break;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync {
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync /* Hopefully this doesn't happen very often:
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync * - messing with the bits of pd pointers without changing the physical address
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync */
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync if (!VM_FF_ISSET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3))
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync {
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync const unsigned iShw = off / sizeof(X86PDPE);
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync if (uShw.pPML4->a[iShw].n.u1Present)
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync {
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPML4->a[iShw].u));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync pgmPoolFree(pPool->CTXSUFF(pVM), uShw.pPML4->a[iShw].u & X86_PML4E_PG_MASK, pPage->idx, iShw);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync uShw.pPML4->a[iShw].u = 0;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync /* paranoia / a bit assumptive. */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync if ( pCpu
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync && (off & 7)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync && (off & 7) + cbWrite > sizeof(X86PDPE))
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PML4E);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync if (uShw.pPML4->a[iShw2].n.u1Present)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPML4->a[iShw2].u));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync pgmPoolFree(pPool->CTXSUFF(pVM), uShw.pPML4->a[iShw2].u & X86_PML4E_PG_MASK, pPage->idx, iShw2);
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync uShw.pPML4->a[iShw2].u = 0;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync }
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync break;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync#endif /* IN_RING0 */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync default:
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync AssertFatalMsgFailed(("enmKind=%d\n", pPage->enmKind));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync /* next */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync if (pPage->iMonitoredNext == NIL_PGMPOOL_IDX)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync return;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync pPage = &pPool->aPages[pPage->iMonitoredNext];
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync}
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync# ifndef IN_RING3
9939c713bffcfc4305d99d994552aa2ad9bce097vboxsync/**
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * Checks if a access could be a fork operation in progress.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync *
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * Meaning, that the guest is setuping up the parent process for Copy-On-Write.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync *
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @returns true if it's likly that we're forking, otherwise false.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param pPool The pool.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param pCpu The disassembled instruction.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param offFault The access offset.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsyncDECLINLINE(bool) pgmPoolMonitorIsForking(PPGMPOOL pPool, PDISCPUSTATE pCpu, unsigned offFault)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync{
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync /*
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * i386 linux is using btr to clear X86_PTE_RW.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * The functions involved are (2.6.16 source inspection):
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * clear_bit
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * ptep_set_wrprotect
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * copy_one_pte
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * copy_pte_range
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * copy_pmd_range
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * copy_pud_range
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * copy_page_range
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * dup_mmap
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * dup_mm
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * copy_mm
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * copy_process
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * do_fork
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync if ( pCpu->pCurInstr->opcode == OP_BTR
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync && !(offFault & 4)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync /** @todo Validate that the bit index is X86_PTE_RW. */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync )
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync STAM_COUNTER_INC(&pPool->CTXMID(StatMonitor,Fork));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync return true;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync return false;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync}
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync/**
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * Determine whether the page is likely to have been reused.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync *
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @returns true if we consider the page as being reused for a different purpose.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @returns false if we consider it to still be a paging page.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param pPage The page in question.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param pRegFrame Trap register frame.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param pCpu The disassembly info for the faulting insturction.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param pvFault The fault address.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync *
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @remark The REP prefix check is left to the caller because of STOSD/W.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsyncDECLINLINE(bool) pgmPoolMonitorIsReused(PPGMPOOLPAGE pPage, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu, RTGCPTR pvFault)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync{
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync switch (pCpu->pCurInstr->opcode)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync case OP_PUSH:
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync Log4(("pgmPoolMonitorIsReused: PUSH\n"));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync return true;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync case OP_PUSHF:
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync Log4(("pgmPoolMonitorIsReused: PUSHF\n"));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync return true;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync case OP_PUSHA:
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync Log4(("pgmPoolMonitorIsReused: PUSHA\n"));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync return true;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync case OP_FXSAVE:
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync Log4(("pgmPoolMonitorIsReused: FXSAVE\n"));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync return true;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync case OP_MOVNTI: /* solaris - block_zero_no_xmm */
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync Log4(("pgmPoolMonitorIsReused: MOVNTI\n"));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync return true;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync case OP_MOVNTDQ: /* solaris - hwblkclr & hwblkpagecopy */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync Log4(("pgmPoolMonitorIsReused: MOVNTDQ\n"));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync return true;
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync case OP_MOVSWD:
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync case OP_STOSWD:
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync if ( pRegFrame
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync && pCpu->prefix == (PREFIX_REP|PREFIX_REX)
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync && pRegFrame->rcx == 0x200
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync )
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync {
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync Assert(pCpu->mode == CPUMODE_64BIT);
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync Log4(("pgmPoolMonitorIsReused: OP_STOSQ\n"));
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync return true;
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync }
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync return false;
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync }
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync if ( (pCpu->param1.flags & USE_REG_GEN32)
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync && (pCpu->param1.base.reg_gen == USE_REG_ESP))
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync {
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync Log4(("pgmPoolMonitorIsReused: ESP\n"));
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync return true;
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync }
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync //if (pPage->fCR3Mix)
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync // return false;
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync return false;
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync}
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync/**
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync * Flushes the page being accessed.
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync *
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync * @returns VBox status code suitable for scheduling.
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync * @param pVM The VM handle.
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync * @param pPool The pool.
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync * @param pPage The pool page (head).
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync * @param pCpu The disassembly of the write instruction.
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync * @param pRegFrame The trap register frame.
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync * @param GCPhysFault The fault address as guest physical address.
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync * @param pvFault The fault address.
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsync */
9b19ad593b379ebfcc8273f85b90763b14b1da63vboxsyncstatic int pgmPoolAccessHandlerFlush(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync{
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync /*
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * First, do the flushing.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync int rc = pgmPoolMonitorChainFlush(pPool, pPage);
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync /*
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * Emulate the instruction (xp/w2k problem, requires pc/cr2/sp detection).
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync uint32_t cbWritten;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync int rc2 = EMInterpretInstructionCPU(pVM, pCpu, pRegFrame, pvFault, &cbWritten);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync if (VBOX_SUCCESS(rc2))
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync pRegFrame->rip += pCpu->opsize;
044af0d1e6474076366759db86f101778c5f20ccvboxsync else if (rc2 == VERR_EM_INTERPRETER)
044af0d1e6474076366759db86f101778c5f20ccvboxsync {
044af0d1e6474076366759db86f101778c5f20ccvboxsync#ifdef IN_GC
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync if (PATMIsPatchGCAddr(pVM, (RTRCPTR)pRegFrame->eip))
044af0d1e6474076366759db86f101778c5f20ccvboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for patch code %04x:%RGv, ignoring.\n",
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync pRegFrame->cs, (RTGCPTR)pRegFrame->eip));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync rc = VINF_SUCCESS;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync STAM_COUNTER_INC(&pPool->StatMonitorGCIntrFailPatch2);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync else
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync#endif
2d8870843ff566fee9bd3a6a5942414254106479vboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync rc = VINF_EM_RAW_EMULATE_INSTR;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync STAM_COUNTER_INC(&pPool->CTXMID(StatMonitor,EmulateInstr));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
2d8870843ff566fee9bd3a6a5942414254106479vboxsync }
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync else
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync rc = rc2;
88acfa6629a7976c0583c1712d2b5b22a87a5121vboxsync
9d020a0622f95aec3aabaff436a495e88dbbd71avboxsync /* See use in pgmPoolAccessHandlerSimple(). */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync PGM_INVL_GUEST_TLBS();
2d8870843ff566fee9bd3a6a5942414254106479vboxsync
2d8870843ff566fee9bd3a6a5942414254106479vboxsync LogFlow(("pgmPoolAccessHandlerPT: returns %Vrc (flushed)\n", rc));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync return rc;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync}
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync/**
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * Handles the STOSD write accesses.
2d8870843ff566fee9bd3a6a5942414254106479vboxsync *
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @returns VBox status code suitable for scheduling.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param pVM The VM handle.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param pPool The pool.
2d8870843ff566fee9bd3a6a5942414254106479vboxsync * @param pPage The pool page (head).
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param pCpu The disassembly of the write instruction.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * @param pRegFrame The trap register frame.
a1df400bbe9d64aad400442e56eb637019300a5evboxsync * @param GCPhysFault The fault address as guest physical address.
a1df400bbe9d64aad400442e56eb637019300a5evboxsync * @param pvFault The fault address.
9d020a0622f95aec3aabaff436a495e88dbbd71avboxsync */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsyncDECLINLINE(int) pgmPoolAccessHandlerSTOSD(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
2d8870843ff566fee9bd3a6a5942414254106479vboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
2d8870843ff566fee9bd3a6a5942414254106479vboxsync{
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync /*
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * Increment the modification counter and insert it into the list
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * of modified pages the first time.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync if (!pPage->cModifications++)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync pgmPoolMonitorModifiedInsert(pPool, pPage);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync /*
9d020a0622f95aec3aabaff436a495e88dbbd71avboxsync * Execute REP STOSD.
9d020a0622f95aec3aabaff436a495e88dbbd71avboxsync *
9d020a0622f95aec3aabaff436a495e88dbbd71avboxsync * This ASSUMES that we're not invoked by Trap0e on in a out-of-sync
9d020a0622f95aec3aabaff436a495e88dbbd71avboxsync * write situation, meaning that it's safe to write here.
9d020a0622f95aec3aabaff436a495e88dbbd71avboxsync */
9d020a0622f95aec3aabaff436a495e88dbbd71avboxsync RTGCUINTPTR pu32 = (RTGCUINTPTR)pvFault;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync while (pRegFrame->ecx)
9d020a0622f95aec3aabaff436a495e88dbbd71avboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync pgmPoolMonitorChainChanging(pPool, pPage, GCPhysFault, (RTGCPTR)pu32, NULL);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync#ifdef IN_GC
9d020a0622f95aec3aabaff436a495e88dbbd71avboxsync *(uint32_t *)pu32 = pRegFrame->eax;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync#else
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync PGMPhysWriteGCPhys(pVM, GCPhysFault, &pRegFrame->eax, 4);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync#endif
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync pu32 += 4;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync GCPhysFault += 4;
9d020a0622f95aec3aabaff436a495e88dbbd71avboxsync pRegFrame->edi += 4;
9d020a0622f95aec3aabaff436a495e88dbbd71avboxsync pRegFrame->ecx--;
9d020a0622f95aec3aabaff436a495e88dbbd71avboxsync }
9d020a0622f95aec3aabaff436a495e88dbbd71avboxsync pRegFrame->rip += pCpu->opsize;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync
9d020a0622f95aec3aabaff436a495e88dbbd71avboxsync /* See use in pgmPoolAccessHandlerSimple(). */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync PGM_INVL_GUEST_TLBS();
044af0d1e6474076366759db86f101778c5f20ccvboxsync
044af0d1e6474076366759db86f101778c5f20ccvboxsync LogFlow(("pgmPoolAccessHandlerSTOSD: returns\n"));
044af0d1e6474076366759db86f101778c5f20ccvboxsync return VINF_SUCCESS;
682342827b0e80c493c820603508e79e76c42658vboxsync}
044af0d1e6474076366759db86f101778c5f20ccvboxsync
044af0d1e6474076366759db86f101778c5f20ccvboxsync
044af0d1e6474076366759db86f101778c5f20ccvboxsync/**
044af0d1e6474076366759db86f101778c5f20ccvboxsync * Handles the simple write accesses.
044af0d1e6474076366759db86f101778c5f20ccvboxsync *
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @returns VBox status code suitable for scheduling.
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @param pVM The VM handle.
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @param pPool The pool.
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @param pPage The pool page (head).
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @param pCpu The disassembly of the write instruction.
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @param pRegFrame The trap register frame.
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @param GCPhysFault The fault address as guest physical address.
044af0d1e6474076366759db86f101778c5f20ccvboxsync * @param pvFault The fault address.
044af0d1e6474076366759db86f101778c5f20ccvboxsync */
044af0d1e6474076366759db86f101778c5f20ccvboxsyncDECLINLINE(int) pgmPoolAccessHandlerSimple(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync{
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync /*
682342827b0e80c493c820603508e79e76c42658vboxsync * Increment the modification counter and insert it into the list
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync * of modified pages the first time.
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync */
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync if (!pPage->cModifications++)
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync pgmPoolMonitorModifiedInsert(pPool, pPage);
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync /*
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync * Clear all the pages. ASSUMES that pvFault is readable.
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync */
682342827b0e80c493c820603508e79e76c42658vboxsync pgmPoolMonitorChainChanging(pPool, pPage, GCPhysFault, pvFault, pCpu);
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync
682342827b0e80c493c820603508e79e76c42658vboxsync /*
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync * Interpret the instruction.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync uint32_t cb;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync int rc = EMInterpretInstructionCPU(pVM, pCpu, pRegFrame, pvFault, &cb);
682342827b0e80c493c820603508e79e76c42658vboxsync if (VBOX_SUCCESS(rc))
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync pRegFrame->rip += pCpu->opsize;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync else if (rc == VERR_EM_INTERPRETER)
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for patch code %04x:%RGv - opcode=%d\n",
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync pRegFrame->cs, (RTGCPTR)pRegFrame->rip, pCpu->pCurInstr->opcode));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync rc = VINF_EM_RAW_EMULATE_INSTR;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync STAM_COUNTER_INC(&pPool->CTXMID(StatMonitor,EmulateInstr));
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync
682342827b0e80c493c820603508e79e76c42658vboxsync /*
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * Quick hack, with logging enabled we're getting stale
682342827b0e80c493c820603508e79e76c42658vboxsync * code TLBs but no data TLB for EIP and crash in EMInterpretDisasOne.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * Flushing here is BAD and expensive, I think EMInterpretDisasOne will
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * have to be fixed to support this. But that'll have to wait till next week.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync *
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * An alternative is to keep track of the changed PTEs together with the
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * GCPhys from the guest PT. This may proove expensive though.
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync *
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync * At the moment, it's VITAL that it's done AFTER the instruction interpreting
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * because we need the stale TLBs in some cases (XP boot). This MUST be fixed properly!
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync */
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync PGM_INVL_GUEST_TLBS();
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync LogFlow(("pgmPoolAccessHandlerSimple: returns %Vrc cb=%d\n", rc, cb));
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync return rc;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync}
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync/**
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * \#PF Handler callback for PT write accesses.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync *
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @returns VBox status code (appropriate for GC return).
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @param pVM VM Handle.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @param uErrorCode CPU Error code.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @param pRegFrame Trap register frame.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * NULL on DMA and other non CPU access.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @param pvFault The fault address (cr2).
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @param GCPhysFault The GC physical address corresponding to pvFault.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @param pvUser User argument.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync */
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsyncDECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser)
6ba706e9f431401d425d16817fdcd6316f83b584vboxsync{
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync STAM_PROFILE_START(&pVM->pgm.s.CTXSUFF(pPool)->CTXSUFF(StatMonitor), a);
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
830a019ad79a45e6bf7a5419efd5a729a36e599evboxsync PPGMPOOLPAGE pPage = (PPGMPOOLPAGE)pvUser;
830a019ad79a45e6bf7a5419efd5a729a36e599evboxsync LogFlow(("pgmPoolAccessHandler: pvFault=%VGv pPage=%p:{.idx=%d} GCPhysFault=%VGp\n", pvFault, pPage, pPage->idx, GCPhysFault));
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync /*
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync * We should ALWAYS have the list head as user parameter. This
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync * is because we use that page to record the changes.
b7a07b07543924f45c1fffd2f90de582038b8ba6vboxsync */
daa94352f51be2329ac8660f70396e03a7cb983bvboxsync Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync /*
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * Disassemble the faulting instruction.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync */
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync DISCPUSTATE Cpu;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync int rc = EMInterpretDisasOne(pVM, pRegFrame, &Cpu, NULL);
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync AssertRCReturn(rc, rc);
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync /*
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * Check if it's worth dealing with.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync */
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync bool fReused = false;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync if ( ( pPage->cModifications < 48 /** @todo #define */ /** @todo need to check that it's not mapping EIP. */ /** @todo adjust this! */
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync || pPage->fCR3Mix)
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync && !(fReused = pgmPoolMonitorIsReused(pPage, pRegFrame, &Cpu, pvFault))
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync && !pgmPoolMonitorIsForking(pPool, &Cpu, GCPhysFault & PAGE_OFFSET_MASK))
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync {
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync /*
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * Simple instructions, no REP prefix.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync */
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync if (!(Cpu.prefix & (PREFIX_REP | PREFIX_REPNE)))
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync {
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync rc = pgmPoolAccessHandlerSimple(pVM, pPool, pPage, &Cpu, pRegFrame, GCPhysFault, pvFault);
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTXSUFF(pPool)->CTXSUFF(StatMonitor), &pPool->CTXMID(StatMonitor,Handled), a);
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync return rc;
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync }
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync
31771163041e3661403a806eb3382d2a165c003bvboxsync /*
31771163041e3661403a806eb3382d2a165c003bvboxsync * Windows is frequently doing small memset() operations (netio test 4k+).
31771163041e3661403a806eb3382d2a165c003bvboxsync * We have to deal with these or we'll kill the cache and performance.
31771163041e3661403a806eb3382d2a165c003bvboxsync */
31771163041e3661403a806eb3382d2a165c003bvboxsync if ( Cpu.pCurInstr->opcode == OP_STOSWD
31771163041e3661403a806eb3382d2a165c003bvboxsync && CPUMGetGuestCPL(pVM, pRegFrame) == 0
31771163041e3661403a806eb3382d2a165c003bvboxsync && pRegFrame->ecx <= 0x20
31771163041e3661403a806eb3382d2a165c003bvboxsync && pRegFrame->ecx * 4 <= PAGE_SIZE - ((uintptr_t)pvFault & PAGE_OFFSET_MASK)
31771163041e3661403a806eb3382d2a165c003bvboxsync && !((uintptr_t)pvFault & 3)
31771163041e3661403a806eb3382d2a165c003bvboxsync && (pRegFrame->eax == 0 || pRegFrame->eax == 0x80) /* the two values observed. */
31771163041e3661403a806eb3382d2a165c003bvboxsync && Cpu.mode == CPUMODE_32BIT
31771163041e3661403a806eb3382d2a165c003bvboxsync && Cpu.opmode == CPUMODE_32BIT
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync && Cpu.addrmode == CPUMODE_32BIT
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync && Cpu.prefix == PREFIX_REP
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync && !pRegFrame->eflags.Bits.u1DF
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync )
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync {
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync rc = pgmPoolAccessHandlerSTOSD(pVM, pPool, pPage, &Cpu, pRegFrame, GCPhysFault, pvFault);
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTXSUFF(pPool)->CTXSUFF(StatMonitor), &pPool->CTXMID(StatMonitor,RepStosd), a);
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync return rc;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync }
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync /* REP prefix, don't bother. */
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync STAM_COUNTER_INC(&pPool->CTXMID(StatMonitor,RepPrefix));
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync Log4(("pgmPoolAccessHandler: eax=%#x ecx=%#x edi=%#x esi=%#x eip=%VGv opcode=%d prefix=%#x\n",
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync pRegFrame->eax, pRegFrame->ecx, pRegFrame->edi, pRegFrame->esi, pRegFrame->rip, Cpu.pCurInstr->opcode, Cpu.prefix));
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync }
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync /*
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync * Not worth it, so flush it.
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync *
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync * If we considered it to be reused, don't to back to ring-3
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync * to emulate failed instructions since we usually cannot
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync * interpret then. This may be a bit risky, in which case
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync * the reuse detection must be fixed.
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync */
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync rc = pgmPoolAccessHandlerFlush(pVM, pPool, pPage, &Cpu, pRegFrame, GCPhysFault, pvFault);
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync if (rc == VINF_EM_RAW_EMULATE_INSTR && fReused)
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync rc = VINF_SUCCESS;
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTXSUFF(pPool)->CTXSUFF(StatMonitor), &pPool->CTXMID(StatMonitor,FlushPage), a);
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync return rc;
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync}
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync# endif /* !IN_RING3 */
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync#endif /* PGMPOOL_WITH_MONITORING */
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync#ifdef PGMPOOL_WITH_CACHE
e9525bea57dc13d82fd3392913aebb33d2cb79e3vboxsync/**
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsync * Inserts a page into the GCPhys hash table.
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsync *
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsync * @param pPool The pool.
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsync * @param pPage The page.
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsync */
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsyncDECLINLINE(void) pgmPoolHashInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsync{
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsync Log3(("pgmPoolHashInsert: %VGp\n", pPage->GCPhys));
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsync Assert(pPage->GCPhys != NIL_RTGCPHYS); Assert(pPage->iNext == NIL_PGMPOOL_IDX);
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsync uint16_t iHash = PGMPOOL_HASH(pPage->GCPhys);
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsync pPage->iNext = pPool->aiHash[iHash];
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsync pPool->aiHash[iHash] = pPage->idx;
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsync}
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsync
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsync
d90eec53c9dcaa0f3d1054e8734ed46875b9093avboxsync/**
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * Removes a page from the GCPhys hash table.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync *
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @param pPool The pool.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @param pPage The page.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync */
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsyncDECLINLINE(void) pgmPoolHashRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync{
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync Log3(("pgmPoolHashRemove: %VGp\n", pPage->GCPhys));
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync uint16_t iHash = PGMPOOL_HASH(pPage->GCPhys);
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync if (pPool->aiHash[iHash] == pPage->idx)
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync pPool->aiHash[iHash] = pPage->iNext;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync else
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync {
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync uint16_t iPrev = pPool->aiHash[iHash];
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync for (;;)
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync {
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync const int16_t i = pPool->aPages[iPrev].iNext;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync if (i == pPage->idx)
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync {
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync pPool->aPages[iPrev].iNext = pPage->iNext;
a1df400bbe9d64aad400442e56eb637019300a5evboxsync break;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync }
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync if (i == NIL_PGMPOOL_IDX)
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync {
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync AssertReleaseMsgFailed(("GCPhys=%VGp idx=%#x\n", pPage->GCPhys, pPage->idx));
044af0d1e6474076366759db86f101778c5f20ccvboxsync break;
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync }
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync iPrev = i;
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync }
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync }
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync pPage->iNext = NIL_PGMPOOL_IDX;
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync}
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync/**
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync * Frees up one cache page.
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync *
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync * @returns VBox status code.
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync * @retval VINF_SUCCESS on success.
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync * @retval VERR_PGM_POOL_CLEARED if the deregistration of a physical handler will cause a light weight pool flush.
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync * @param pPool The pool.
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync * @param iUser The user index.
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync */
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsyncstatic int pgmPoolCacheFreeOne(PPGMPOOL pPool, uint16_t iUser)
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync{
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync#ifndef IN_GC
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync const PVM pVM = pPool->CTXSUFF(pVM);
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync#endif
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync Assert(pPool->iAgeHead != pPool->iAgeTail); /* We shouldn't be here if there < 2 cached entries! */
bbb4c0bfd5ea55e99591d8811771257a437053eevboxsync STAM_COUNTER_INC(&pPool->StatCacheFreeUpOne);
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync /*
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * Select one page from the tail of the age list.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync */
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync uint16_t iToFree = pPool->iAgeTail;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync if (iToFree == iUser)
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync iToFree = pPool->aPages[iToFree].iAgePrev;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync/* This is the alternative to the SyncCR3 pgmPoolCacheUsed calls.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync if (pPool->aPages[iToFree].iUserHead != NIL_PGMPOOL_USER_INDEX)
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync {
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync uint16_t i = pPool->aPages[iToFree].iAgePrev;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync for (unsigned j = 0; j < 10 && i != NIL_PGMPOOL_USER_INDEX; j++, i = pPool->aPages[i].iAgePrev)
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync {
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync if (pPool->aPages[iToFree].iUserHead == NIL_PGMPOOL_USER_INDEX)
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync continue;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync iToFree = i;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync break;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync }
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync }
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync*/
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync Assert(iToFree != iUser);
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync AssertRelease(iToFree != NIL_PGMPOOL_IDX);
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync int rc = pgmPoolFlushPage(pPool, &pPool->aPages[iToFree]);
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync if (rc == VINF_SUCCESS)
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync PGM_INVL_GUEST_TLBS(); /* see PT handler. */
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync return rc;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync}
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync/**
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * Checks if a kind mismatch is really a page being reused
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * or if it's just normal remappings.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync *
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @returns true if reused and the cached page (enmKind1) should be flushed
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @returns false if not reused.
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * @param enmKind1 The kind of the cached page.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * @param enmKind2 The kind of the requested page.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync */
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsyncstatic bool pgmPoolCacheReusedByKind(PGMPOOLKIND enmKind1, PGMPOOLKIND enmKind2)
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync{
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync switch (enmKind1)
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync {
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync /*
044af0d1e6474076366759db86f101778c5f20ccvboxsync * Never reuse them. There is no remapping in non-paging mode.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync */
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync return true;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync /*
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync */
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync switch (enmKind2)
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync {
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
044af0d1e6474076366759db86f101778c5f20ccvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
a1df400bbe9d64aad400442e56eb637019300a5evboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync return true;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync default:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync return false;
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync }
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync /*
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
4a1654dd5b9f0ae4e149d909843a3ab07b8bec33vboxsync */
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync switch (enmKind2)
a1df400bbe9d64aad400442e56eb637019300a5evboxsync {
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
c0e27f622f9bd6d9e77d2d959aab71d69dabf0d3vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
ca3da10d05961c339b5180fbd40a54587d6bad35vboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync return true;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync default:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync return false;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync }
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync /*
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * These cannot be flushed, and it's common to reuse the PDs as PTs.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_ROOT_32BIT_PD:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_ROOT_PAE_PD:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_ROOT_PDPT:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_ROOT_NESTED:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync return false;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync default:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync AssertFatalMsgFailed(("enmKind1=%d\n", enmKind1));
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync }
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync}
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync/**
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Attempts to satisfy a pgmPoolAlloc request from the cache.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync *
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @returns VBox status code.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @retval VINF_PGM_CACHED_PAGE on success.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @retval VERR_FILE_NOT_FOUND if not found.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param pPool The pool.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param GCPhys The GC physical address of the page we're gonna shadow.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param enmKind The kind of mapping.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param iUser The shadow page pool index of the user table.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param iUserTable The index into the user table (shadowed).
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param ppPage Where to store the pointer to the page.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsyncstatic int pgmPoolCacheAlloc(PPGMPOOL pPool, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync{
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync#ifndef IN_GC
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync const PVM pVM = pPool->CTXSUFF(pVM);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync#endif
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync /*
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Look up the GCPhys in the hash.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync unsigned i = pPool->aiHash[PGMPOOL_HASH(GCPhys)];
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync Log3(("pgmPoolCacheAlloc: %VGp kind %d iUser=%d iUserTable=%x SLOT=%d\n", GCPhys, enmKind, iUser, iUserTable, i));
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync if (i != NIL_PGMPOOL_IDX)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync {
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync do
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync {
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync PPGMPOOLPAGE pPage = &pPool->aPages[i];
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync Log3(("pgmPoolCacheAlloc: slot %d found page %VGp\n", i, pPage->GCPhys));
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync if (pPage->GCPhys == GCPhys)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync {
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync if ((PGMPOOLKIND)pPage->enmKind == enmKind)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync {
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync int rc = pgmPoolTrackAddUser(pPool, pPage, iUser, iUserTable);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync if (VBOX_SUCCESS(rc))
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync {
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync *ppPage = pPage;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync STAM_COUNTER_INC(&pPool->StatCacheHits);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync return VINF_PGM_CACHED_PAGE;
a1df400bbe9d64aad400442e56eb637019300a5evboxsync }
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync return rc;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync }
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync /*
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * The kind is different. In some cases we should now flush the page
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * as it has been reused, but in most cases this is normal remapping
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * of PDs as PT or big pages using the GCPhys field in a slightly
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * different way than the other kinds.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync if (pgmPoolCacheReusedByKind((PGMPOOLKIND)pPage->enmKind, enmKind))
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync {
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync STAM_COUNTER_INC(&pPool->StatCacheKindMismatches);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pgmPoolFlushPage(pPool, pPage); /* ASSUMES that VERR_PGM_POOL_CLEARED will be returned by pgmPoolTracInsert. */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync PGM_INVL_GUEST_TLBS(); /* see PT handler. */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync break;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync }
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync }
a1df400bbe9d64aad400442e56eb637019300a5evboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync /* next */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync i = pPage->iNext;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync } while (i != NIL_PGMPOOL_IDX);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync }
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync Log3(("pgmPoolCacheAlloc: Missed GCPhys=%RGp enmKind=%d\n", GCPhys, enmKind));
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync STAM_COUNTER_INC(&pPool->StatCacheMisses);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync return VERR_FILE_NOT_FOUND;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync}
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync/**
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Inserts a page into the cache.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync *
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param pPool The pool.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param pPage The cached page.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param fCanBeCached Set if the page is fit for caching from the caller's point of view.
2d8870843ff566fee9bd3a6a5942414254106479vboxsync */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsyncstatic void pgmPoolCacheInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, bool fCanBeCached)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync{
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync /*
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Insert into the GCPhys hash if the page is fit for that.
2d8870843ff566fee9bd3a6a5942414254106479vboxsync */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync Assert(!pPage->fCached);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync if (fCanBeCached)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync {
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPage->fCached = true;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pgmPoolHashInsert(pPool, pPage);
2d8870843ff566fee9bd3a6a5942414254106479vboxsync Log3(("pgmPoolCacheInsert: Caching %p:{.Core=%RHp, .idx=%d, .enmKind=%d, GCPhys=%RGp}\n",
2d8870843ff566fee9bd3a6a5942414254106479vboxsync pPage, pPage->Core.Key, pPage->idx, pPage->enmKind, pPage->GCPhys));
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync STAM_COUNTER_INC(&pPool->StatCacheCacheable);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync }
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync else
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync {
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync Log3(("pgmPoolCacheInsert: Not caching %p:{.Core=%RHp, .idx=%d, .enmKind=%d, GCPhys=%RGp}\n",
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPage, pPage->Core.Key, pPage->idx, pPage->enmKind, pPage->GCPhys));
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync STAM_COUNTER_INC(&pPool->StatCacheUncacheable);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync }
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync /*
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Insert at the head of the age list.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPage->iAgePrev = NIL_PGMPOOL_IDX;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPage->iAgeNext = pPool->iAgeHead;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync if (pPool->iAgeHead != NIL_PGMPOOL_IDX)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPool->aPages[pPool->iAgeHead].iAgePrev = pPage->idx;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync else
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPool->iAgeTail = pPage->idx;
2d8870843ff566fee9bd3a6a5942414254106479vboxsync pPool->iAgeHead = pPage->idx;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync}
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync/**
2d8870843ff566fee9bd3a6a5942414254106479vboxsync * Flushes a cached page.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync *
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param pPool The pool.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param pPage The cached page.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsyncstatic void pgmPoolCacheFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync{
2d8870843ff566fee9bd3a6a5942414254106479vboxsync Log3(("pgmPoolCacheFlushPage: %VGp\n", pPage->GCPhys));
2d8870843ff566fee9bd3a6a5942414254106479vboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync /*
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Remove the page from the hash.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync if (pPage->fCached)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync {
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPage->fCached = false;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pgmPoolHashRemove(pPool, pPage);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync }
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync else
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync Assert(pPage->iNext == NIL_PGMPOOL_IDX);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync /*
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Remove it from the age list.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync */
a1df400bbe9d64aad400442e56eb637019300a5evboxsync if (pPage->iAgeNext != NIL_PGMPOOL_IDX)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPool->aPages[pPage->iAgeNext].iAgePrev = pPage->iAgePrev;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync else
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPool->iAgeTail = pPage->iAgePrev;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync if (pPage->iAgePrev != NIL_PGMPOOL_IDX)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPool->aPages[pPage->iAgePrev].iAgeNext = pPage->iAgeNext;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync else
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPool->iAgeHead = pPage->iAgeNext;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPage->iAgeNext = NIL_PGMPOOL_IDX;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPage->iAgePrev = NIL_PGMPOOL_IDX;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync}
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync#endif /* PGMPOOL_WITH_CACHE */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync#ifdef PGMPOOL_WITH_MONITORING
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync/**
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Looks for pages sharing the monitor.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync *
a1df400bbe9d64aad400442e56eb637019300a5evboxsync * @returns Pointer to the head page.
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync * @returns NULL if not found.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param pPool The Pool
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param pNewPage The page which is going to be monitored.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsyncstatic PPGMPOOLPAGE pgmPoolMonitorGetPageByGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pNewPage)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync{
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync#ifdef PGMPOOL_WITH_CACHE
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync /*
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Look up the GCPhys in the hash.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync RTGCPHYS GCPhys = pNewPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync unsigned i = pPool->aiHash[PGMPOOL_HASH(GCPhys)];
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync if (i == NIL_PGMPOOL_IDX)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync return NULL;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync do
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync {
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync PPGMPOOLPAGE pPage = &pPool->aPages[i];
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync if ( pPage->GCPhys - GCPhys < PAGE_SIZE
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync && pPage != pNewPage)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync {
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync switch (pPage->enmKind)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync {
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_ROOT_32BIT_PD:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_ROOT_PAE_PD:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_ROOT_PDPT:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync {
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync /* find the head */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync while (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync {
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync Assert(pPage->iMonitoredPrev != pPage->idx);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPage = &pPool->aPages[pPage->iMonitoredPrev];
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync }
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync return pPage;
a1df400bbe9d64aad400442e56eb637019300a5evboxsync }
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync /* ignore, no monitoring. */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_ROOT_NESTED:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync break;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync default:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync AssertFatalMsgFailed(("enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync }
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync }
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync /* next */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync i = pPage->iNext;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync } while (i != NIL_PGMPOOL_IDX);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync#endif
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync return NULL;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync}
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync/**
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Enabled write monitoring of a guest page.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync *
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @returns VBox status code.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @retval VINF_SUCCESS on success.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @retval VERR_PGM_POOL_CLEARED if the registration of the physical handler will cause a light weight pool flush.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param pPool The pool.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * @param pPage The cached page.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsyncstatic int pgmPoolMonitorInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync{
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync LogFlow(("pgmPoolMonitorInsert %VGp\n", pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1)));
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync /*
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Filter out the relevant kinds.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync switch (pPage->enmKind)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync {
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_ROOT_PDPT:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync break;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_ROOT_NESTED:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync /* Nothing to monitor here. */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync return VINF_SUCCESS;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_ROOT_32BIT_PD:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_ROOT_PAE_PD:
88acfa6629a7976c0583c1712d2b5b22a87a5121vboxsync#ifdef PGMPOOL_WITH_MIXED_PT_CR3
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync break;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync#endif
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync default:
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync }
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync /*
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync * Install handler.
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync */
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync int rc;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync PPGMPOOLPAGE pPageHead = pgmPoolMonitorGetPageByGCPhys(pPool, pPage);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync if (pPageHead)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync {
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync Assert(pPageHead != pPage); Assert(pPageHead->iMonitoredNext != pPage->idx);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync Assert(pPageHead->iMonitoredPrev != pPage->idx);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPage->iMonitoredPrev = pPageHead->idx;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPage->iMonitoredNext = pPageHead->iMonitoredNext;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync if (pPageHead->iMonitoredNext != NIL_PGMPOOL_IDX)
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPool->aPages[pPageHead->iMonitoredNext].iMonitoredPrev = pPage->idx;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPageHead->iMonitoredNext = pPage->idx;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync rc = VINF_SUCCESS;
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync }
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync else
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync {
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync Assert(pPage->iMonitoredNext == NIL_PGMPOOL_IDX); Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync PVM pVM = pPool->CTXSUFF(pVM);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync const RTGCPHYS GCPhysPage = pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync rc = PGMHandlerPhysicalRegisterEx(pVM, PGMPHYSHANDLERTYPE_PHYSICAL_WRITE,
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync GCPhysPage, GCPhysPage + (PAGE_SIZE - 1),
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pPage),
df8e6a449f00e1884fbf4a1fc67143614d7d528dvboxsync pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pPage),
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync pPool->pfnAccessHandlerGC, MMHyperCCToGC(pVM, pPage),
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync pPool->pszAccessHandler);
45c3e41d012100c5f4a3f77e391e4c6da8f5b97avboxsync /** @todo we should probably deal with out-of-memory conditions here, but for now increasing
45c3e41d012100c5f4a3f77e391e4c6da8f5b97avboxsync * the heap size should suffice. */
26645f447bb6a819f33492c84e03e364092ec600vboxsync AssertFatalRC(rc);
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync if (pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync rc = VERR_PGM_POOL_CLEARED;
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync }
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync pPage->fMonitored = true;
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync return rc;
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync}
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync
26645f447bb6a819f33492c84e03e364092ec600vboxsync
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync/**
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync * Disables write monitoring of a guest page.
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync *
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync * @returns VBox status code.
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync * @retval VINF_SUCCESS on success.
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync * @retval VERR_PGM_POOL_CLEARED if the deregistration of the physical handler will cause a light weight pool flush.
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync * @param pPool The pool.
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync * @param pPage The cached page.
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync */
4dcf37df51439573b55cc16f53590c7b68cb1051vboxsyncstatic int pgmPoolMonitorFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync{
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync /*
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync * Filter out the relevant kinds.
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync */
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync switch (pPage->enmKind)
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync {
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
4dcf37df51439573b55cc16f53590c7b68cb1051vboxsync case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync case PGMPOOLKIND_ROOT_PDPT:
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync break;
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
4dcf37df51439573b55cc16f53590c7b68cb1051vboxsync case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync case PGMPOOLKIND_PAE_PT_FOR_PHYS:
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync case PGMPOOLKIND_ROOT_NESTED:
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync /* Nothing to monitor here. */
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync return VINF_SUCCESS;
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync case PGMPOOLKIND_ROOT_32BIT_PD:
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync case PGMPOOLKIND_ROOT_PAE_PD:
4dcf37df51439573b55cc16f53590c7b68cb1051vboxsync#ifdef PGMPOOL_WITH_MIXED_PT_CR3
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync break;
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync#endif
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync default:
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync }
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync /*
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync * Remove the page from the monitored list or uninstall it if last.
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync */
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync const PVM pVM = pPool->CTXSUFF(pVM);
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync int rc;
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync if ( pPage->iMonitoredNext != NIL_PGMPOOL_IDX
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync || pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync {
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync if (pPage->iMonitoredPrev == NIL_PGMPOOL_IDX)
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync {
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync PPGMPOOLPAGE pNewHead = &pPool->aPages[pPage->iMonitoredNext];
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync pNewHead->iMonitoredPrev = NIL_PGMPOOL_IDX;
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync pNewHead->fCR3Mix = pPage->fCR3Mix;
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync rc = PGMHandlerPhysicalChangeCallbacks(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1),
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pNewHead),
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pNewHead),
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync pPool->pfnAccessHandlerGC, MMHyperCCToGC(pVM, pNewHead),
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync pPool->pszAccessHandler);
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync AssertFatalRCSuccess(rc);
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync }
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync else
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync {
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync pPool->aPages[pPage->iMonitoredPrev].iMonitoredNext = pPage->iMonitoredNext;
560a1042a968cf73161ac78555eddc8d16e8bebdvboxsync if (pPage->iMonitoredNext != NIL_PGMPOOL_IDX)
560a1042a968cf73161ac78555eddc8d16e8bebdvboxsync {
560a1042a968cf73161ac78555eddc8d16e8bebdvboxsync pPool->aPages[pPage->iMonitoredNext].iMonitoredPrev = pPage->iMonitoredPrev;
560a1042a968cf73161ac78555eddc8d16e8bebdvboxsync pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync }
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync rc = VINF_SUCCESS;
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync }
feffb6492a600891a421e4c1bf0a2bcd2213568dvboxsync }
26645f447bb6a819f33492c84e03e364092ec600vboxsync else
26645f447bb6a819f33492c84e03e364092ec600vboxsync {
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync rc = PGMHandlerPhysicalDeregister(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1));
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync AssertFatalRC(rc);
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsync if (pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync rc = VERR_PGM_POOL_CLEARED;
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync }
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync pPage->fMonitored = false;
/*
* Remove it from the list of modified pages (if in it).
*/
pgmPoolMonitorModifiedRemove(pPool, pPage);
return rc;
}
#ifdef PGMPOOL_WITH_MIXED_PT_CR3
/**
* Set or clear the fCR3Mix attribute in a chain of monitored pages.
*
* @param pPool The Pool.
* @param pPage A page in the chain.
* @param fCR3Mix The new fCR3Mix value.
*/
static void pgmPoolMonitorChainChangeCR3Mix(PPGMPOOL pPool, PPGMPOOLPAGE pPage, bool fCR3Mix)
{
/* current */
pPage->fCR3Mix = fCR3Mix;
/* before */
int16_t idx = pPage->iMonitoredPrev;
while (idx != NIL_PGMPOOL_IDX)
{
pPool->aPages[idx].fCR3Mix = fCR3Mix;
idx = pPool->aPages[idx].iMonitoredPrev;
}
/* after */
idx = pPage->iMonitoredNext;
while (idx != NIL_PGMPOOL_IDX)
{
pPool->aPages[idx].fCR3Mix = fCR3Mix;
idx = pPool->aPages[idx].iMonitoredNext;
}
}
/**
* Installs or modifies monitoring of a CR3 page (special).
*
* We're pretending the CR3 page is shadowed by the pool so we can use the
* generic mechanisms in detecting chained monitoring. (This also gives us a
* tast of what code changes are required to really pool CR3 shadow pages.)
*
* @returns VBox status code.
* @param pPool The pool.
* @param idxRoot The CR3 (root) page index.
* @param GCPhysCR3 The (new) CR3 value.
*/
int pgmPoolMonitorMonitorCR3(PPGMPOOL pPool, uint16_t idxRoot, RTGCPHYS GCPhysCR3)
{
Assert(idxRoot != NIL_PGMPOOL_IDX && idxRoot < PGMPOOL_IDX_FIRST);
PPGMPOOLPAGE pPage = &pPool->aPages[idxRoot];
LogFlow(("pgmPoolMonitorMonitorCR3: idxRoot=%d pPage=%p:{.GCPhys=%VGp, .fMonitored=%d} GCPhysCR3=%VGp\n",
idxRoot, pPage, pPage->GCPhys, pPage->fMonitored, GCPhysCR3));
/*
* The unlikely case where it already matches.
*/
if (pPage->GCPhys == GCPhysCR3)
{
Assert(pPage->fMonitored);
return VINF_SUCCESS;
}
/*
* Flush the current monitoring and remove it from the hash.
*/
int rc = VINF_SUCCESS;
if (pPage->fMonitored)
{
pgmPoolMonitorChainChangeCR3Mix(pPool, pPage, false);
rc = pgmPoolMonitorFlush(pPool, pPage);
if (rc == VERR_PGM_POOL_CLEARED)
rc = VINF_SUCCESS;
else
AssertFatalRC(rc);
pgmPoolHashRemove(pPool, pPage);
}
/*
* Monitor the page at the new location and insert it into the hash.
*/
pPage->GCPhys = GCPhysCR3;
int rc2 = pgmPoolMonitorInsert(pPool, pPage);
if (rc2 != VERR_PGM_POOL_CLEARED)
{
AssertFatalRC(rc2);
if (rc2 != VINF_SUCCESS && rc == VINF_SUCCESS)
rc = rc2;
}
pgmPoolHashInsert(pPool, pPage);
pgmPoolMonitorChainChangeCR3Mix(pPool, pPage, true);
return rc;
}
/**
* Removes the monitoring of a CR3 page (special).
*
* @returns VBox status code.
* @param pPool The pool.
* @param idxRoot The CR3 (root) page index.
*/
int pgmPoolMonitorUnmonitorCR3(PPGMPOOL pPool, uint16_t idxRoot)
{
Assert(idxRoot != NIL_PGMPOOL_IDX && idxRoot < PGMPOOL_IDX_FIRST);
PPGMPOOLPAGE pPage = &pPool->aPages[idxRoot];
LogFlow(("pgmPoolMonitorUnmonitorCR3: idxRoot=%d pPage=%p:{.GCPhys=%VGp, .fMonitored=%d}\n",
idxRoot, pPage, pPage->GCPhys, pPage->fMonitored));
if (!pPage->fMonitored)
return VINF_SUCCESS;
pgmPoolMonitorChainChangeCR3Mix(pPool, pPage, false);
int rc = pgmPoolMonitorFlush(pPool, pPage);
if (rc != VERR_PGM_POOL_CLEARED)
AssertFatalRC(rc);
else
rc = VINF_SUCCESS;
pgmPoolHashRemove(pPool, pPage);
Assert(!pPage->fMonitored);
pPage->GCPhys = NIL_RTGCPHYS;
return rc;
}
#endif /* PGMPOOL_WITH_MIXED_PT_CR3 */
/**
* Inserts the page into the list of modified pages.
*
* @param pPool The pool.
* @param pPage The page.
*/
void pgmPoolMonitorModifiedInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
{
Log3(("pgmPoolMonitorModifiedInsert: idx=%d\n", pPage->idx));
AssertMsg( pPage->iModifiedNext == NIL_PGMPOOL_IDX
&& pPage->iModifiedPrev == NIL_PGMPOOL_IDX
&& pPool->iModifiedHead != pPage->idx,
("Next=%d Prev=%d idx=%d cModifications=%d Head=%d cModifiedPages=%d\n",
pPage->iModifiedNext, pPage->iModifiedPrev, pPage->idx, pPage->cModifications,
pPool->iModifiedHead, pPool->cModifiedPages));
pPage->iModifiedNext = pPool->iModifiedHead;
if (pPool->iModifiedHead != NIL_PGMPOOL_IDX)
pPool->aPages[pPool->iModifiedHead].iModifiedPrev = pPage->idx;
pPool->iModifiedHead = pPage->idx;
pPool->cModifiedPages++;
#ifdef VBOX_WITH_STATISTICS
if (pPool->cModifiedPages > pPool->cModifiedPagesHigh)
pPool->cModifiedPagesHigh = pPool->cModifiedPages;
#endif
}
/**
* Removes the page from the list of modified pages and resets the
* moficiation counter.
*
* @param pPool The pool.
* @param pPage The page which is believed to be in the list of modified pages.
*/
static void pgmPoolMonitorModifiedRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
{
Log3(("pgmPoolMonitorModifiedRemove: idx=%d cModifications=%d\n", pPage->idx, pPage->cModifications));
if (pPool->iModifiedHead == pPage->idx)
{
Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX);
pPool->iModifiedHead = pPage->iModifiedNext;
if (pPage->iModifiedNext != NIL_PGMPOOL_IDX)
{
pPool->aPages[pPage->iModifiedNext].iModifiedPrev = NIL_PGMPOOL_IDX;
pPage->iModifiedNext = NIL_PGMPOOL_IDX;
}
pPool->cModifiedPages--;
}
else if (pPage->iModifiedPrev != NIL_PGMPOOL_IDX)
{
pPool->aPages[pPage->iModifiedPrev].iModifiedNext = pPage->iModifiedNext;
if (pPage->iModifiedNext != NIL_PGMPOOL_IDX)
{
pPool->aPages[pPage->iModifiedNext].iModifiedPrev = pPage->iModifiedPrev;
pPage->iModifiedNext = NIL_PGMPOOL_IDX;
}
pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
pPool->cModifiedPages--;
}
else
Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX);
pPage->cModifications = 0;
}
/**
* Zaps the list of modified pages, resetting their modification counters in the process.
*
* @param pVM The VM handle.
*/
void pgmPoolMonitorModifiedClearAll(PVM pVM)
{
PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
LogFlow(("pgmPoolMonitorModifiedClearAll: cModifiedPages=%d\n", pPool->cModifiedPages));
unsigned cPages = 0; NOREF(cPages);
uint16_t idx = pPool->iModifiedHead;
pPool->iModifiedHead = NIL_PGMPOOL_IDX;
while (idx != NIL_PGMPOOL_IDX)
{
PPGMPOOLPAGE pPage = &pPool->aPages[idx];
idx = pPage->iModifiedNext;
pPage->iModifiedNext = NIL_PGMPOOL_IDX;
pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
pPage->cModifications = 0;
Assert(++cPages);
}
AssertMsg(cPages == pPool->cModifiedPages, ("%d != %d\n", cPages, pPool->cModifiedPages));
pPool->cModifiedPages = 0;
}
/**
* Clear all shadow pages and clear all modification counters.
*
* @param pVM The VM handle.
* @remark Should only be used when monitoring is available, thus placed in
* the PGMPOOL_WITH_MONITORING #ifdef.
*/
void pgmPoolClearAll(PVM pVM)
{
PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
STAM_PROFILE_START(&pPool->StatClearAll, c);
LogFlow(("pgmPoolClearAll: cUsedPages=%d\n", pPool->cUsedPages));
/*
* Iterate all the pages until we've encountered all that in use.
* This is simple but not quite optimal solution.
*/
unsigned cModifiedPages = 0; NOREF(cModifiedPages);
unsigned cLeft = pPool->cUsedPages;
unsigned iPage = pPool->cCurPages;
while (--iPage >= PGMPOOL_IDX_FIRST)
{
PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
if (pPage->GCPhys != NIL_RTGCPHYS)
{
switch (pPage->enmKind)
{
/*
* We only care about shadow page tables.
*/
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
{
#ifdef PGMPOOL_WITH_USER_TRACKING
if (pPage->cPresent)
#endif
{
void *pvShw = PGMPOOL_PAGE_2_PTR(pPool->CTXSUFF(pVM), pPage);
STAM_PROFILE_START(&pPool->StatZeroPage, z);
ASMMemZeroPage(pvShw);
STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
#ifdef PGMPOOL_WITH_USER_TRACKING
pPage->cPresent = 0;
pPage->iFirstPresent = ~0;
#endif
}
}
/* fall thru */
default:
Assert(!pPage->cModifications || ++cModifiedPages);
Assert(pPage->iModifiedNext == NIL_PGMPOOL_IDX || pPage->cModifications);
Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX || pPage->cModifications);
pPage->iModifiedNext = NIL_PGMPOOL_IDX;
pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
pPage->cModifications = 0;
break;
}
if (!--cLeft)
break;
}
}
/* swipe the special pages too. */
for (iPage = PGMPOOL_IDX_FIRST_SPECIAL; iPage < PGMPOOL_IDX_FIRST; iPage++)
{
PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
if (pPage->GCPhys != NIL_RTGCPHYS)
{
Assert(!pPage->cModifications || ++cModifiedPages);
Assert(pPage->iModifiedNext == NIL_PGMPOOL_IDX || pPage->cModifications);
Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX || pPage->cModifications);
pPage->iModifiedNext = NIL_PGMPOOL_IDX;
pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
pPage->cModifications = 0;
}
}
#ifndef DEBUG_michael
AssertMsg(cModifiedPages == pPool->cModifiedPages, ("%d != %d\n", cModifiedPages, pPool->cModifiedPages));
#endif
pPool->iModifiedHead = NIL_PGMPOOL_IDX;
pPool->cModifiedPages = 0;
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
/*
* Clear all the GCPhys links and rebuild the phys ext free list.
*/
for (PPGMRAMRANGE pRam = pPool->CTXSUFF(pVM)->pgm.s.CTXALLSUFF(pRamRanges);
pRam;
pRam = CTXALLSUFF(pRam->pNext))
{
unsigned iPage = pRam->cb >> PAGE_SHIFT;
while (iPage-- > 0)
pRam->aPages[iPage].HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
}
pPool->iPhysExtFreeHead = 0;
PPGMPOOLPHYSEXT paPhysExts = pPool->CTXSUFF(paPhysExts);
const unsigned cMaxPhysExts = pPool->cMaxPhysExts;
for (unsigned i = 0; i < cMaxPhysExts; i++)
{
paPhysExts[i].iNext = i + 1;
paPhysExts[i].aidx[0] = NIL_PGMPOOL_IDX;
paPhysExts[i].aidx[1] = NIL_PGMPOOL_IDX;
paPhysExts[i].aidx[2] = NIL_PGMPOOL_IDX;
}
paPhysExts[cMaxPhysExts - 1].iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
#endif
pPool->cPresent = 0;
STAM_PROFILE_STOP(&pPool->StatClearAll, c);
}
/**
* Handle SyncCR3 pool tasks
*
* @returns VBox status code.
* @retval VINF_SUCCESS if successfully added.
* @retval VINF_PGM_SYNC_CR3 is it needs to be deferred to ring 3 (GC only)
* @param pVM The VM handle.
* @remark Should only be used when monitoring is available, thus placed in
* the PGMPOOL_WITH_MONITORING #ifdef.
*/
int pgmPoolSyncCR3(PVM pVM)
{
/*
* When monitoring shadowed pages, we reset the modification counters on CR3 sync.
* Occasionally we will have to clear all the shadow page tables because we wanted
* to monitor a page which was mapped by too many shadowed page tables. This operation
* sometimes refered to as a 'lightweight flush'.
*/
if (!(pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL))
pgmPoolMonitorModifiedClearAll(pVM);
else
{
# ifndef IN_GC
pVM->pgm.s.fSyncFlags &= ~PGM_SYNC_CLEAR_PGM_POOL;
pgmPoolClearAll(pVM);
# else
LogFlow(("SyncCR3: PGM_SYNC_CLEAR_PGM_POOL is set -> VINF_PGM_SYNC_CR3\n"));
VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3); /** @todo no need to do global sync, right? */
return VINF_PGM_SYNC_CR3;
# endif
}
return VINF_SUCCESS;
}
#endif /* PGMPOOL_WITH_MONITORING */
#ifdef PGMPOOL_WITH_USER_TRACKING
/**
* Frees up at least one user entry.
*
* @returns VBox status code.
* @retval VINF_SUCCESS if successfully added.
* @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
* @param pPool The pool.
* @param iUser The user index.
*/
static int pgmPoolTrackFreeOneUser(PPGMPOOL pPool, uint16_t iUser)
{
STAM_COUNTER_INC(&pPool->StatTrackFreeUpOneUser);
#ifdef PGMPOOL_WITH_CACHE
/*
* Just free cached pages in a braindead fashion.
*/
/** @todo walk the age list backwards and free the first with usage. */
int rc = VINF_SUCCESS;
do
{
int rc2 = pgmPoolCacheFreeOne(pPool, iUser);
if (VBOX_FAILURE(rc2) && rc == VINF_SUCCESS)
rc = rc2;
} while (pPool->iUserFreeHead == NIL_PGMPOOL_USER_INDEX);
return rc;
#else
/*
* Lazy approach.
*/
pgmPoolFlushAllInt(pPool);
return VERR_PGM_POOL_FLUSHED;
#endif
}
/**
* Inserts a page into the cache.
*
* This will create user node for the page, insert it into the GCPhys
* hash, and insert it into the age list.
*
* @returns VBox status code.
* @retval VINF_SUCCESS if successfully added.
* @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
* @retval VERR_PGM_POOL_CLEARED if the deregistration of the physical handler will cause a light weight pool flush.
* @param pPool The pool.
* @param pPage The cached page.
* @param GCPhys The GC physical address of the page we're gonna shadow.
* @param iUser The user index.
* @param iUserTable The user table index.
*/
DECLINLINE(int) pgmPoolTrackInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhys, uint16_t iUser, uint32_t iUserTable)
{
int rc = VINF_SUCCESS;
PPGMPOOLUSER pUser = pPool->CTXSUFF(paUsers);
LogFlow(("pgmPoolTrackInsert iUser %d iUserTable %d\n", iUser, iUserTable));
/*
* Find free a user node.
*/
uint16_t i = pPool->iUserFreeHead;
if (i == NIL_PGMPOOL_USER_INDEX)
{
int rc = pgmPoolTrackFreeOneUser(pPool, iUser);
if (VBOX_FAILURE(rc))
return rc;
i = pPool->iUserFreeHead;
}
/*
* Unlink the user node from the free list,
* initialize and insert it into the user list.
*/
pPool->iUserFreeHead = pUser[i].iNext;
pUser[i].iNext = NIL_PGMPOOL_USER_INDEX;
pUser[i].iUser = iUser;
pUser[i].iUserTable = iUserTable;
pPage->iUserHead = i;
/*
* Insert into cache and enable monitoring of the guest page if enabled.
*
* Until we implement caching of all levels, including the CR3 one, we'll
* have to make sure we don't try monitor & cache any recursive reuse of
* a monitored CR3 page. Because all windows versions are doing this we'll
* have to be able to do combined access monitoring, CR3 + PT and
* PD + PT (guest PAE).
*
* Update:
* We're now cooperating with the CR3 monitor if an uncachable page is found.
*/
#if defined(PGMPOOL_WITH_MONITORING) || defined(PGMPOOL_WITH_CACHE)
# ifdef PGMPOOL_WITH_MIXED_PT_CR3
const bool fCanBeMonitored = true;
# else
bool fCanBeMonitored = pPool->CTXSUFF(pVM)->pgm.s.GCPhysGstCR3Monitored == NIL_RTGCPHYS
|| (GCPhys & X86_PTE_PAE_PG_MASK) != (pPool->CTXSUFF(pVM)->pgm.s.GCPhysGstCR3Monitored & X86_PTE_PAE_PG_MASK)
|| pgmPoolIsBigPage((PGMPOOLKIND)pPage->enmKind);
# endif
# ifdef PGMPOOL_WITH_CACHE
pgmPoolCacheInsert(pPool, pPage, fCanBeMonitored); /* This can be expanded. */
# endif
if (fCanBeMonitored)
{
# ifdef PGMPOOL_WITH_MONITORING
rc = pgmPoolMonitorInsert(pPool, pPage);
if (rc == VERR_PGM_POOL_CLEARED)
{
/* 'Failed' - free the usage, and keep it in the cache (if enabled). */
# ifndef PGMPOOL_WITH_CACHE
pgmPoolMonitorFlush(pPool, pPage);
rc = VERR_PGM_POOL_FLUSHED;
# endif
pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
pUser[i].iNext = pPool->iUserFreeHead;
pUser[i].iUser = NIL_PGMPOOL_IDX;
pPool->iUserFreeHead = i;
}
}
# endif
#endif /* PGMPOOL_WITH_MONITORING */
return rc;
}
# ifdef PGMPOOL_WITH_CACHE /* (only used when the cache is enabled.) */
/**
* Adds a user reference to a page.
*
* This will
* This will move the page to the head of the
*
* @returns VBox status code.
* @retval VINF_SUCCESS if successfully added.
* @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
* @param pPool The pool.
* @param pPage The cached page.
* @param iUser The user index.
* @param iUserTable The user table.
*/
static int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
{
PPGMPOOLUSER paUsers = pPool->CTXSUFF(paUsers);
LogFlow(("pgmPoolTrackAddUser iUser %d iUserTable %d\n", iUser, iUserTable));
# ifdef VBOX_STRICT
/*
* Check that the entry doesn't already exists.
*/
if (pPage->iUserHead != NIL_PGMPOOL_USER_INDEX)
{
uint16_t i = pPage->iUserHead;
do
{
Assert(i < pPool->cMaxUsers);
AssertMsg(paUsers[i].iUser != iUser || paUsers[i].iUserTable != iUserTable, ("%x %x vs new %x %x\n", paUsers[i].iUser, paUsers[i].iUserTable, iUser, iUserTable));
i = paUsers[i].iNext;
} while (i != NIL_PGMPOOL_USER_INDEX);
}
# endif
/*
* Allocate a user node.
*/
uint16_t i = pPool->iUserFreeHead;
if (i == NIL_PGMPOOL_USER_INDEX)
{
int rc = pgmPoolTrackFreeOneUser(pPool, iUser);
if (VBOX_FAILURE(rc))
return rc;
i = pPool->iUserFreeHead;
}
pPool->iUserFreeHead = paUsers[i].iNext;
/*
* Initialize the user node and insert it.
*/
paUsers[i].iNext = pPage->iUserHead;
paUsers[i].iUser = iUser;
paUsers[i].iUserTable = iUserTable;
pPage->iUserHead = i;
# ifdef PGMPOOL_WITH_CACHE
/*
* Tell the cache to update its replacement stats for this page.
*/
pgmPoolCacheUsed(pPool, pPage);
# endif
return VINF_SUCCESS;
}
# endif /* PGMPOOL_WITH_CACHE */
/**
* Frees a user record associated with a page.
*
* This does not clear the entry in the user table, it simply replaces the
* user record to the chain of free records.
*
* @param pPool The pool.
* @param HCPhys The HC physical address of the shadow page.
* @param iUser The shadow page pool index of the user table.
* @param iUserTable The index into the user table (shadowed).
*/
static void pgmPoolTrackFreeUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
{
/*
* Unlink and free the specified user entry.
*/
PPGMPOOLUSER paUsers = pPool->CTXSUFF(paUsers);
/* Special: For PAE and 32-bit paging, there is usually no more than one user. */
uint16_t i = pPage->iUserHead;
if ( i != NIL_PGMPOOL_USER_INDEX
&& paUsers[i].iUser == iUser
&& paUsers[i].iUserTable == iUserTable)
{
pPage->iUserHead = paUsers[i].iNext;
paUsers[i].iUser = NIL_PGMPOOL_IDX;
paUsers[i].iNext = pPool->iUserFreeHead;
pPool->iUserFreeHead = i;
return;
}
/* General: Linear search. */
uint16_t iPrev = NIL_PGMPOOL_USER_INDEX;
while (i != NIL_PGMPOOL_USER_INDEX)
{
if ( paUsers[i].iUser == iUser
&& paUsers[i].iUserTable == iUserTable)
{
if (iPrev != NIL_PGMPOOL_USER_INDEX)
paUsers[iPrev].iNext = paUsers[i].iNext;
else
pPage->iUserHead = paUsers[i].iNext;
paUsers[i].iUser = NIL_PGMPOOL_IDX;
paUsers[i].iNext = pPool->iUserFreeHead;
pPool->iUserFreeHead = i;
return;
}
iPrev = i;
i = paUsers[i].iNext;
}
/* Fatal: didn't find it */
AssertFatalMsgFailed(("Didn't find the user entry! iUser=%#x iUserTable=%#x GCPhys=%VGp\n",
iUser, iUserTable, pPage->GCPhys));
}
/**
* Gets the entry size of a shadow table.
*
* @param enmKind The kind of page.
*
* @returns The size of the entry in bytes. That is, 4 or 8.
* @returns If the kind is not for a table, an assertion is raised and 0 is
* returned.
*/
DECLINLINE(unsigned) pgmPoolTrackGetShadowEntrySize(PGMPOOLKIND enmKind)
{
switch (enmKind)
{
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_ROOT_32BIT_PD:
return 4;
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
case PGMPOOLKIND_ROOT_PAE_PD:
case PGMPOOLKIND_ROOT_PDPT:
case PGMPOOLKIND_ROOT_NESTED:
case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
return 8;
default:
AssertFatalMsgFailed(("enmKind=%d\n", enmKind));
}
}
/**
* Gets the entry size of a guest table.
*
* @param enmKind The kind of page.
*
* @returns The size of the entry in bytes. That is, 0, 4 or 8.
* @returns If the kind is not for a table, an assertion is raised and 0 is
* returned.
*/
DECLINLINE(unsigned) pgmPoolTrackGetGuestEntrySize(PGMPOOLKIND enmKind)
{
switch (enmKind)
{
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_ROOT_32BIT_PD:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
return 4;
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
case PGMPOOLKIND_ROOT_PAE_PD:
case PGMPOOLKIND_ROOT_PDPT:
return 8;
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
case PGMPOOLKIND_ROOT_NESTED:
/** @todo can we return 0? (nobody is calling this...) */
AssertFailed();
return 0;
default:
AssertFatalMsgFailed(("enmKind=%d\n", enmKind));
}
}
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
/**
* Scans one shadow page table for mappings of a physical page.
*
* @param pVM The VM handle.
* @param pPhysPage The guest page in question.
* @param iShw The shadow page table.
* @param cRefs The number of references made in that PT.
*/
static void pgmPoolTrackFlushGCPhysPTInt(PVM pVM, PCPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs)
{
LogFlow(("pgmPoolTrackFlushGCPhysPT: HCPhys=%RHp iShw=%d cRefs=%d\n", pPhysPage->HCPhys, iShw, cRefs));
PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
/*
* Assert sanity.
*/
Assert(cRefs == 1);
AssertFatalMsg(iShw < pPool->cCurPages && iShw != NIL_PGMPOOL_IDX, ("iShw=%d\n", iShw));
PPGMPOOLPAGE pPage = &pPool->aPages[iShw];
/*
* Then, clear the actual mappings to the page in the shadow PT.
*/
switch (pPage->enmKind)
{
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
{
const uint32_t u32 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
PX86PT pPT = (PX86PT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
for (unsigned i = pPage->iFirstPresent; i < ELEMENTS(pPT->a); i++)
if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
{
Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX32 cRefs=%#x\n", i, pPT->a[i], cRefs));
pPT->a[i].u = 0;
cRefs--;
if (!cRefs)
return;
}
#if defined(DEBUG) && !defined(IN_RING0) ///@todo RTLogPrintf is missing in R0.
RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
for (unsigned i = 0; i < ELEMENTS(pPT->a); i++)
if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
{
RTLogPrintf("i=%d cRefs=%d\n", i, cRefs--);
pPT->a[i].u = 0;
}
#endif
AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
break;
}
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
{
const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
PX86PTPAE pPT = (PX86PTPAE)PGMPOOL_PAGE_2_PTR(pVM, pPage);
for (unsigned i = pPage->iFirstPresent; i < ELEMENTS(pPT->a); i++)
if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
{
Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX64 cRefs=%#x\n", i, pPT->a[i], cRefs));
pPT->a[i].u = 0;
cRefs--;
if (!cRefs)
return;
}
#if defined(DEBUG) && !defined(IN_RING0) ///@todo RTLogPrintf is missing in R0.
RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
for (unsigned i = 0; i < ELEMENTS(pPT->a); i++)
if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
{
RTLogPrintf("i=%d cRefs=%d\n", i, cRefs--);
pPT->a[i].u = 0;
}
#endif
AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
break;
}
default:
AssertFatalMsgFailed(("enmKind=%d iShw=%d\n", pPage->enmKind, iShw));
}
}
/**
* Scans one shadow page table for mappings of a physical page.
*
* @param pVM The VM handle.
* @param pPhysPage The guest page in question.
* @param iShw The shadow page table.
* @param cRefs The number of references made in that PT.
*/
void pgmPoolTrackFlushGCPhysPT(PVM pVM, PPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs)
{
PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool); NOREF(pPool);
LogFlow(("pgmPoolTrackFlushGCPhysPT: HCPhys=%RHp iShw=%d cRefs=%d\n", pPhysPage->HCPhys, iShw, cRefs));
STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPT, f);
pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, iShw, cRefs);
pPhysPage->HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPT, f);
}
/**
* Flushes a list of shadow page tables mapping the same physical page.
*
* @param pVM The VM handle.
* @param pPhysPage The guest page in question.
* @param iPhysExt The physical cross reference extent list to flush.
*/
void pgmPoolTrackFlushGCPhysPTs(PVM pVM, PPGMPAGE pPhysPage, uint16_t iPhysExt)
{
PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTs, f);
LogFlow(("pgmPoolTrackFlushGCPhysPTs: HCPhys=%RHp iPhysExt\n", pPhysPage->HCPhys, iPhysExt));
const uint16_t iPhysExtStart = iPhysExt;
PPGMPOOLPHYSEXT pPhysExt;
do
{
Assert(iPhysExt < pPool->cMaxPhysExts);
pPhysExt = &pPool->CTXSUFF(paPhysExts)[iPhysExt];
for (unsigned i = 0; i < ELEMENTS(pPhysExt->aidx); i++)
if (pPhysExt->aidx[i] != NIL_PGMPOOL_IDX)
{
pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, pPhysExt->aidx[i], 1);
pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
}
/* next */
iPhysExt = pPhysExt->iNext;
} while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
/* insert the list into the free list and clear the ram range entry. */
pPhysExt->iNext = pPool->iPhysExtFreeHead;
pPool->iPhysExtFreeHead = iPhysExtStart;
pPhysPage->HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTs, f);
}
#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
/**
* Scans all shadow page tables for mappings of a physical page.
*
* This may be slow, but it's most likely more efficient than cleaning
* out the entire page pool / cache.
*
* @returns VBox status code.
* @retval VINF_SUCCESS if all references has been successfully cleared.
* @retval VINF_PGM_GCPHYS_ALIASED if we're better off with a CR3 sync and
* a page pool cleaning.
*
* @param pVM The VM handle.
* @param pPhysPage The guest page in question.
*/
int pgmPoolTrackFlushGCPhysPTsSlow(PVM pVM, PPGMPAGE pPhysPage)
{
PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTsSlow, s);
LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: cUsedPages=%d cPresent=%d HCPhys=%RHp\n",
pPool->cUsedPages, pPool->cPresent, pPhysPage->HCPhys));
#if 1
/*
* There is a limit to what makes sense.
*/
if (pPool->cPresent > 1024)
{
LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: giving up... (cPresent=%d)\n", pPool->cPresent));
STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTsSlow, s);
return VINF_PGM_GCPHYS_ALIASED;
}
#endif
/*
* Iterate all the pages until we've encountered all that in use.
* This is simple but not quite optimal solution.
*/
const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
const uint32_t u32 = u64;
unsigned cLeft = pPool->cUsedPages;
unsigned iPage = pPool->cCurPages;
while (--iPage >= PGMPOOL_IDX_FIRST)
{
PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
if (pPage->GCPhys != NIL_RTGCPHYS)
{
switch (pPage->enmKind)
{
/*
* We only care about shadow page tables.
*/
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
{
unsigned cPresent = pPage->cPresent;
PX86PT pPT = (PX86PT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
for (unsigned i = pPage->iFirstPresent; i < ELEMENTS(pPT->a); i++)
if (pPT->a[i].n.u1Present)
{
if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
{
//Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX32\n", iPage, i, pPT->a[i]));
pPT->a[i].u = 0;
}
if (!--cPresent)
break;
}
break;
}
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
{
unsigned cPresent = pPage->cPresent;
PX86PTPAE pPT = (PX86PTPAE)PGMPOOL_PAGE_2_PTR(pVM, pPage);
for (unsigned i = pPage->iFirstPresent; i < ELEMENTS(pPT->a); i++)
if (pPT->a[i].n.u1Present)
{
if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
{
//Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX64\n", iPage, i, pPT->a[i]));
pPT->a[i].u = 0;
}
if (!--cPresent)
break;
}
break;
}
}
if (!--cLeft)
break;
}
}
pPhysPage->HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTsSlow, s);
return VINF_SUCCESS;
}
/**
* Clears the user entry in a user table.
*
* This is used to remove all references to a page when flushing it.
*/
static void pgmPoolTrackClearPageUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PCPGMPOOLUSER pUser)
{
Assert(pUser->iUser != NIL_PGMPOOL_IDX);
Assert(pUser->iUser < pPool->cCurPages);
/*
* Map the user page.
*/
PPGMPOOLPAGE pUserPage = &pPool->aPages[pUser->iUser];
union
{
uint64_t *pau64;
uint32_t *pau32;
} u;
u.pau64 = (uint64_t *)PGMPOOL_PAGE_2_PTR(pPool->CTXSUFF(pVM), pUserPage);
/* Safety precaution in case we change the paging for other modes too in the future. */
Assert(PGMGetHyperCR3(CTXSUFF(pPool->pVM)) != pPage->Core.Key);
#ifdef VBOX_STRICT
/*
* Some sanity checks.
*/
switch (pUserPage->enmKind)
{
case PGMPOOLKIND_ROOT_32BIT_PD:
Assert(pUser->iUserTable < X86_PG_ENTRIES);
Assert(!(u.pau32[pUser->iUserTable] & PGM_PDFLAGS_MAPPING));
break;
case PGMPOOLKIND_ROOT_PAE_PD:
Assert(pUser->iUserTable < 2048 && pUser->iUser == PGMPOOL_IDX_PAE_PD);
Assert(!(u.pau64[pUser->iUserTable] & PGM_PDFLAGS_MAPPING));
break;
case PGMPOOLKIND_ROOT_PDPT:
Assert(pUser->iUserTable < 4);
Assert(!(u.pau64[pUser->iUserTable] & PGM_PLXFLAGS_PERMANENT));
break;
case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
Assert(pUser->iUserTable < X86_PG_PAE_ENTRIES);
break;
case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
Assert(pUser->iUserTable < X86_PG_PAE_ENTRIES);
Assert(!(u.pau64[pUser->iUserTable] & PGM_PDFLAGS_MAPPING));
break;
case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
Assert(pUser->iUserTable < X86_PG_PAE_ENTRIES);
Assert(!(u.pau64[pUser->iUserTable] & PGM_PLXFLAGS_PERMANENT));
break;
case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
Assert(!(u.pau64[pUser->iUserTable] & PGM_PLXFLAGS_PERMANENT));
/* GCPhys >> PAGE_SHIFT is the index here */
break;
case PGMPOOLKIND_ROOT_NESTED:
Assert(pUser->iUserTable < X86_PG_PAE_ENTRIES);
break;
default:
AssertMsgFailed(("enmKind=%d\n", pUserPage->enmKind));
break;
}
#endif /* VBOX_STRICT */
/*
* Clear the entry in the user page.
*/
switch (pUserPage->enmKind)
{
/* 32-bit entries */
case PGMPOOLKIND_ROOT_32BIT_PD:
u.pau32[pUser->iUserTable] = 0;
break;
/* 64-bit entries */
case PGMPOOLKIND_ROOT_PAE_PD:
case PGMPOOLKIND_ROOT_PDPT:
case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
case PGMPOOLKIND_ROOT_NESTED:
u.pau64[pUser->iUserTable] = 0;
break;
default:
AssertFatalMsgFailed(("enmKind=%d iUser=%#x iUserTable=%#x\n", pUserPage->enmKind, pUser->iUser, pUser->iUserTable));
}
}
/**
* Clears all users of a page.
*/
static void pgmPoolTrackClearPageUsers(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
{
/*
* Free all the user records.
*/
PPGMPOOLUSER paUsers = pPool->CTXSUFF(paUsers);
uint16_t i = pPage->iUserHead;
while (i != NIL_PGMPOOL_USER_INDEX)
{
/* Clear enter in user table. */
pgmPoolTrackClearPageUser(pPool, pPage, &paUsers[i]);
/* Free it. */
const uint16_t iNext = paUsers[i].iNext;
paUsers[i].iUser = NIL_PGMPOOL_IDX;
paUsers[i].iNext = pPool->iUserFreeHead;
pPool->iUserFreeHead = i;
/* Next. */
i = iNext;
}
pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
}
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
/**
* Allocates a new physical cross reference extent.
*
* @returns Pointer to the allocated extent on success. NULL if we're out of them.
* @param pVM The VM handle.
* @param piPhysExt Where to store the phys ext index.
*/
PPGMPOOLPHYSEXT pgmPoolTrackPhysExtAlloc(PVM pVM, uint16_t *piPhysExt)
{
PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
uint16_t iPhysExt = pPool->iPhysExtFreeHead;
if (iPhysExt == NIL_PGMPOOL_PHYSEXT_INDEX)
{
STAM_COUNTER_INC(&pPool->StamTrackPhysExtAllocFailures);
return NULL;
}
PPGMPOOLPHYSEXT pPhysExt = &pPool->CTXSUFF(paPhysExts)[iPhysExt];
pPool->iPhysExtFreeHead = pPhysExt->iNext;
pPhysExt->iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
*piPhysExt = iPhysExt;
return pPhysExt;
}
/**
* Frees a physical cross reference extent.
*
* @param pVM The VM handle.
* @param iPhysExt The extent to free.
*/
void pgmPoolTrackPhysExtFree(PVM pVM, uint16_t iPhysExt)
{
PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
Assert(iPhysExt < pPool->cMaxPhysExts);
PPGMPOOLPHYSEXT pPhysExt = &pPool->CTXSUFF(paPhysExts)[iPhysExt];
for (unsigned i = 0; i < ELEMENTS(pPhysExt->aidx); i++)
pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
pPhysExt->iNext = pPool->iPhysExtFreeHead;
pPool->iPhysExtFreeHead = iPhysExt;
}
/**
* Frees a physical cross reference extent.
*
* @param pVM The VM handle.
* @param iPhysExt The extent to free.
*/
void pgmPoolTrackPhysExtFreeList(PVM pVM, uint16_t iPhysExt)
{
PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
const uint16_t iPhysExtStart = iPhysExt;
PPGMPOOLPHYSEXT pPhysExt;
do
{
Assert(iPhysExt < pPool->cMaxPhysExts);
pPhysExt = &pPool->CTXSUFF(paPhysExts)[iPhysExt];
for (unsigned i = 0; i < ELEMENTS(pPhysExt->aidx); i++)
pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
/* next */
iPhysExt = pPhysExt->iNext;
} while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
pPhysExt->iNext = pPool->iPhysExtFreeHead;
pPool->iPhysExtFreeHead = iPhysExtStart;
}
/**
* Insert a reference into a list of physical cross reference extents.
*
* @returns The new ram range flags (top 16-bits).
*
* @param pVM The VM handle.
* @param iPhysExt The physical extent index of the list head.
* @param iShwPT The shadow page table index.
*
*/
static uint16_t pgmPoolTrackPhysExtInsert(PVM pVM, uint16_t iPhysExt, uint16_t iShwPT)
{
PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
PPGMPOOLPHYSEXT paPhysExts = pPool->CTXSUFF(paPhysExts);
/* special common case. */
if (paPhysExts[iPhysExt].aidx[2] == NIL_PGMPOOL_IDX)
{
paPhysExts[iPhysExt].aidx[2] = iShwPT;
STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedMany);
LogFlow(("pgmPoolTrackPhysExtAddref: %d:{,,%d}\n", iPhysExt, iShwPT));
return iPhysExt | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
}
/* general treatment. */
const uint16_t iPhysExtStart = iPhysExt;
unsigned cMax = 15;
for (;;)
{
Assert(iPhysExt < pPool->cMaxPhysExts);
for (unsigned i = 0; i < ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
if (paPhysExts[iPhysExt].aidx[i] == NIL_PGMPOOL_IDX)
{
paPhysExts[iPhysExt].aidx[i] = iShwPT;
STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedMany);
LogFlow(("pgmPoolTrackPhysExtAddref: %d:{%d} i=%d cMax=%d\n", iPhysExt, iShwPT, i, cMax));
return iPhysExtStart | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
}
if (!--cMax)
{
STAM_COUNTER_INC(&pVM->pgm.s.StatTrackOverflows);
pgmPoolTrackPhysExtFreeList(pVM, iPhysExtStart);
LogFlow(("pgmPoolTrackPhysExtAddref: overflow (1) iShwPT=%d\n", iShwPT));
return MM_RAM_FLAGS_IDX_OVERFLOWED | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
}
}
/* add another extent to the list. */
PPGMPOOLPHYSEXT pNew = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
if (!pNew)
{
STAM_COUNTER_INC(&pVM->pgm.s.StatTrackOverflows);
pgmPoolTrackPhysExtFreeList(pVM, iPhysExtStart);
return MM_RAM_FLAGS_IDX_OVERFLOWED | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
}
pNew->iNext = iPhysExtStart;
pNew->aidx[0] = iShwPT;
LogFlow(("pgmPoolTrackPhysExtAddref: added new extent %d:{%d}->%d\n", iPhysExt, iShwPT, iPhysExtStart));
return iPhysExt | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
}
/**
* Add a reference to guest physical page where extents are in use.
*
* @returns The new ram range flags (top 16-bits).
*
* @param pVM The VM handle.
* @param u16 The ram range flags (top 16-bits).
* @param iShwPT The shadow page table index.
*/
uint16_t pgmPoolTrackPhysExtAddref(PVM pVM, uint16_t u16, uint16_t iShwPT)
{
if ((u16 >> (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT)) != MM_RAM_FLAGS_CREFS_PHYSEXT)
{
/*
* Convert to extent list.
*/
Assert((u16 >> (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT)) == 1);
uint16_t iPhysExt;
PPGMPOOLPHYSEXT pPhysExt = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
if (pPhysExt)
{
LogFlow(("pgmPoolTrackPhysExtAddref: new extent: %d:{%d, %d}\n", iPhysExt, u16 & MM_RAM_FLAGS_IDX_MASK, iShwPT));
STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliased);
pPhysExt->aidx[0] = u16 & MM_RAM_FLAGS_IDX_MASK;
pPhysExt->aidx[1] = iShwPT;
u16 = iPhysExt | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
}
else
u16 = MM_RAM_FLAGS_IDX_OVERFLOWED | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
}
else if (u16 != (MM_RAM_FLAGS_IDX_OVERFLOWED | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT))))
{
/*
* Insert into the extent list.
*/
u16 = pgmPoolTrackPhysExtInsert(pVM, u16 & MM_RAM_FLAGS_IDX_MASK, iShwPT);
}
else
STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedLots);
return u16;
}
/**
* Clear references to guest physical memory.
*
* @param pPool The pool.
* @param pPage The page.
* @param pPhysPage Pointer to the aPages entry in the ram range.
*/
void pgmPoolTrackPhysExtDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PPGMPAGE pPhysPage)
{
const unsigned cRefs = pPhysPage->HCPhys >> MM_RAM_FLAGS_CREFS_SHIFT; /** @todo PAGE FLAGS */
AssertFatalMsg(cRefs == MM_RAM_FLAGS_CREFS_PHYSEXT, ("cRefs=%d HCPhys=%RHp pPage=%p:{.idx=%d}\n", cRefs, pPhysPage->HCPhys, pPage, pPage->idx));
uint16_t iPhysExt = (pPhysPage->HCPhys >> MM_RAM_FLAGS_IDX_SHIFT) & MM_RAM_FLAGS_IDX_MASK;
if (iPhysExt != MM_RAM_FLAGS_IDX_OVERFLOWED)
{
uint16_t iPhysExtPrev = NIL_PGMPOOL_PHYSEXT_INDEX;
PPGMPOOLPHYSEXT paPhysExts = pPool->CTXSUFF(paPhysExts);
do
{
Assert(iPhysExt < pPool->cMaxPhysExts);
/*
* Look for the shadow page and check if it's all freed.
*/
for (unsigned i = 0; i < ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
{
if (paPhysExts[iPhysExt].aidx[i] == pPage->idx)
{
paPhysExts[iPhysExt].aidx[i] = NIL_PGMPOOL_IDX;
for (i = 0; i < ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
if (paPhysExts[iPhysExt].aidx[i] != NIL_PGMPOOL_IDX)
{
LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64 idx=%d\n", pPhysPage->HCPhys, pPage->idx));
return;
}
/* we can free the node. */
PVM pVM = pPool->CTXSUFF(pVM);
const uint16_t iPhysExtNext = paPhysExts[iPhysExt].iNext;
if ( iPhysExtPrev == NIL_PGMPOOL_PHYSEXT_INDEX
&& iPhysExtNext == NIL_PGMPOOL_PHYSEXT_INDEX)
{
/* lonely node */
pgmPoolTrackPhysExtFree(pVM, iPhysExt);
LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64 idx=%d lonely\n", pPhysPage->HCPhys, pPage->idx));
pPhysPage->HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
}
else if (iPhysExtPrev == NIL_PGMPOOL_PHYSEXT_INDEX)
{
/* head */
LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64 idx=%d head\n", pPhysPage->HCPhys, pPage->idx));
pPhysPage->HCPhys = (pPhysPage->HCPhys & MM_RAM_FLAGS_NO_REFS_MASK) /** @todo PAGE FLAGS */
| ((uint64_t)MM_RAM_FLAGS_CREFS_PHYSEXT << MM_RAM_FLAGS_CREFS_SHIFT)
| ((uint64_t)iPhysExtNext << MM_RAM_FLAGS_IDX_SHIFT);
pgmPoolTrackPhysExtFree(pVM, iPhysExt);
}
else
{
/* in list */
LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64 idx=%d\n", pPhysPage->HCPhys, pPage->idx));
paPhysExts[iPhysExtPrev].iNext = iPhysExtNext;
pgmPoolTrackPhysExtFree(pVM, iPhysExt);
}
iPhysExt = iPhysExtNext;
return;
}
}
/* next */
iPhysExtPrev = iPhysExt;
iPhysExt = paPhysExts[iPhysExt].iNext;
} while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
AssertFatalMsgFailed(("not-found! cRefs=%d HCPhys=%RHp pPage=%p:{.idx=%d}\n", cRefs, pPhysPage->HCPhys, pPage, pPage->idx));
}
else /* nothing to do */
LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64\n", pPhysPage->HCPhys));
}
/**
* Clear references to guest physical memory.
*
* This is the same as pgmPoolTracDerefGCPhys except that the guest physical address
* is assumed to be correct, so the linear search can be skipped and we can assert
* at an earlier point.
*
* @param pPool The pool.
* @param pPage The page.
* @param HCPhys The host physical address corresponding to the guest page.
* @param GCPhys The guest physical address corresponding to HCPhys.
*/
static void pgmPoolTracDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhys)
{
/*
* Walk range list.
*/
PPGMRAMRANGE pRam = pPool->CTXSUFF(pVM)->pgm.s.CTXALLSUFF(pRamRanges);
while (pRam)
{
RTGCPHYS off = GCPhys - pRam->GCPhys;
if (off < pRam->cb)
{
/* does it match? */
const unsigned iPage = off >> PAGE_SHIFT;
Assert(PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]));
RTHCPHYS HCPhysPage = PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]);
Log(("pgmPoolTracDerefGCPhys %VHp vs %VHp\n", HCPhysPage, HCPhys));
if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
{
pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
return;
}
break;
}
pRam = CTXALLSUFF(pRam->pNext);
}
AssertFatalMsgFailed(("HCPhys=%VHp GCPhys=%VGp\n", HCPhys, GCPhys));
}
/**
* Clear references to guest physical memory.
*
* @param pPool The pool.
* @param pPage The page.
* @param HCPhys The host physical address corresponding to the guest page.
* @param GCPhysHint The guest physical address which may corresponding to HCPhys.
*/
static void pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint)
{
/*
* Walk range list.
*/
PPGMRAMRANGE pRam = pPool->CTXSUFF(pVM)->pgm.s.CTXALLSUFF(pRamRanges);
while (pRam)
{
RTGCPHYS off = GCPhysHint - pRam->GCPhys;
if (off < pRam->cb)
{
/* does it match? */
const unsigned iPage = off >> PAGE_SHIFT;
Assert(PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]));
if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
{
pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
return;
}
break;
}
pRam = CTXALLSUFF(pRam->pNext);
}
/*
* Damn, the hint didn't work. We'll have to do an expensive linear search.
*/
STAM_COUNTER_INC(&pPool->StatTrackLinearRamSearches);
pRam = pPool->CTXSUFF(pVM)->pgm.s.CTXALLSUFF(pRamRanges);
while (pRam)
{
unsigned iPage = pRam->cb >> PAGE_SHIFT;
while (iPage-- > 0)
{
if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
{
Log4(("pgmPoolTracDerefGCPhysHint: Linear HCPhys=%VHp GCPhysHint=%VGp GCPhysReal=%VGp\n",
HCPhys, GCPhysHint, pRam->GCPhys + (iPage << PAGE_SHIFT)));
pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
return;
}
}
pRam = CTXALLSUFF(pRam->pNext);
}
AssertFatalMsgFailed(("HCPhys=%VHp GCPhysHint=%VGp\n", HCPhys, GCPhysHint));
}
/**
* Clear references to guest physical memory in a 32-bit / 32-bit page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
* @param pGstPT The guest page table.
*/
DECLINLINE(void) pgmPoolTrackDerefPT32Bit32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT, PCX86PT pGstPT)
{
for (unsigned i = pPage->iFirstPresent; i < ELEMENTS(pShwPT->a); i++)
if (pShwPT->a[i].n.u1Present)
{
Log4(("pgmPoolTrackDerefPT32Bit32Bit: i=%d pte=%RX32 hint=%RX32\n",
i, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK));
pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
if (!--pPage->cPresent)
break;
}
}
/**
* Clear references to guest physical memory in a PAE / 32-bit page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
* @param pGstPT The guest page table (just a half one).
*/
DECLINLINE(void) pgmPoolTrackDerefPTPae32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PT pGstPT)
{
for (unsigned i = 0; i < ELEMENTS(pShwPT->a); i++)
if (pShwPT->a[i].n.u1Present)
{
Log4(("pgmPoolTrackDerefPTPae32Bit: i=%d pte=%RX32 hint=%RX32\n",
i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK));
pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
}
}
/**
* Clear references to guest physical memory in a PAE / PAE page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
* @param pGstPT The guest page table.
*/
DECLINLINE(void) pgmPoolTrackDerefPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PTPAE pGstPT)
{
for (unsigned i = 0; i < ELEMENTS(pShwPT->a); i++)
if (pShwPT->a[i].n.u1Present)
{
Log4(("pgmPoolTrackDerefPTPaePae: i=%d pte=%RX32 hint=%RX32\n",
i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK));
pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK);
}
}
/**
* Clear references to guest physical memory in a 32-bit / 4MB page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPT32Bit4MB(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT)
{
RTGCPHYS GCPhys = pPage->GCPhys;
for (unsigned i = 0; i < ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
if (pShwPT->a[i].n.u1Present)
{
Log4(("pgmPoolTrackDerefPT32Bit4MB: i=%d pte=%RX32 GCPhys=%RGp\n",
i, pShwPT->a[i].u & X86_PTE_PG_MASK, GCPhys));
pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, GCPhys);
}
}
/**
* Clear references to guest physical memory in a PAE / 2/4MB page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPTPaeBig(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT)
{
RTGCPHYS GCPhys = pPage->GCPhys;
for (unsigned i = 0; i < ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
if (pShwPT->a[i].n.u1Present)
{
Log4(("pgmPoolTrackDerefPTPaeBig: i=%d pte=%RX64 hint=%VGp\n",
i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, GCPhys));
pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, GCPhys);
}
}
#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
/**
* Clear references to shadowed pages in a PAE (legacy or 64 bits) page directory.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPD The shadow page directory (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPDPae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPAE pShwPD)
{
for (unsigned i = 0; i < ELEMENTS(pShwPD->a); i++)
{
if (pShwPD->a[i].n.u1Present)
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PAE_PG_MASK);
if (pSubPage)
pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
else
AssertFatalMsgFailed(("%RX64\n", pShwPD->a[i].u & X86_PDE_PAE_PG_MASK));
/** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
}
}
}
/**
* Clear references to shadowed pages in a 64-bit page directory pointer table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPDPT The shadow page directory pointer table (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPDPT64Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPT pShwPDPT)
{
for (unsigned i = 0; i < ELEMENTS(pShwPDPT->a); i++)
{
if (pShwPDPT->a[i].n.u1Present)
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
if (pSubPage)
pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
else
AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & X86_PDPE_PG_MASK));
/** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
}
}
}
/**
* Clear references to shadowed pages in a 64-bit level 4 page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPML4 The shadow page directory pointer table (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPML464Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PML4 pShwPML4)
{
for (unsigned i = 0; i < ELEMENTS(pShwPML4->a); i++)
{
if (pShwPML4->a[i].n.u1Present)
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPML4->a[i].u & X86_PDPE_PG_MASK);
if (pSubPage)
pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
else
AssertFatalMsgFailed(("%RX64\n", pShwPML4->a[i].u & X86_PML4E_PG_MASK));
/** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
}
}
}
/**
* Clears all references made by this page.
*
* This includes other shadow pages and GC physical addresses.
*
* @param pPool The pool.
* @param pPage The page.
*/
static void pgmPoolTrackDeref(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
{
/*
* Map the shadow page and take action according to the page kind.
*/
void *pvShw = PGMPOOL_PAGE_2_PTR(pPool->CTXSUFF(pVM), pPage);
switch (pPage->enmKind)
{
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
{
STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
void *pvGst;
int rc = PGM_GCPHYS_2_PTR(pPool->CTXSUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
pgmPoolTrackDerefPT32Bit32Bit(pPool, pPage, (PX86PT)pvShw, (PCX86PT)pvGst);
STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
break;
}
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
{
STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
void *pvGst;
int rc = PGM_GCPHYS_2_PTR_EX(pPool->CTXSUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
pgmPoolTrackDerefPTPae32Bit(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PT)pvGst);
STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
break;
}
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
{
STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
void *pvGst;
int rc = PGM_GCPHYS_2_PTR(pPool->CTXSUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
pgmPoolTrackDerefPTPaePae(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PTPAE)pvGst);
STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
break;
}
case PGMPOOLKIND_32BIT_PT_FOR_PHYS: /* treat it like a 4 MB page */
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
{
STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
pgmPoolTrackDerefPT32Bit4MB(pPool, pPage, (PX86PT)pvShw);
STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
break;
}
case PGMPOOLKIND_PAE_PT_FOR_PHYS: /* treat it like a 4 MB page */
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
{
STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
pgmPoolTrackDerefPTPaeBig(pPool, pPage, (PX86PTPAE)pvShw);
STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
break;
}
#else /* !PGMPOOL_WITH_GCPHYS_TRACKING */
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
break;
#endif /* !PGMPOOL_WITH_GCPHYS_TRACKING */
case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
pgmPoolTrackDerefPDPae(pPool, pPage, (PX86PDPAE)pvShw);
break;
case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
pgmPoolTrackDerefPDPT64Bit(pPool, pPage, (PX86PDPT)pvShw);
break;
case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
pgmPoolTrackDerefPML464Bit(pPool, pPage, (PX86PML4)pvShw);
break;
default:
AssertFatalMsgFailed(("enmKind=%d\n", pPage->enmKind));
}
/* paranoia, clear the shadow page. Remove this laser (i.e. let Alloc and ClearAll do it). */
STAM_PROFILE_START(&pPool->StatZeroPage, z);
ASMMemZeroPage(pvShw);
STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
pPage->fZeroed = true;
}
#endif /* PGMPOOL_WITH_USER_TRACKING */
/**
* Flushes all the special root pages as part of a pgmPoolFlushAllInt operation.
*
* @param pPool The pool.
*/
static void pgmPoolFlushAllSpecialRoots(PPGMPOOL pPool)
{
/*
* These special pages are all mapped into the indexes 1..PGMPOOL_IDX_FIRST.
*/
Assert(NIL_PGMPOOL_IDX == 0);
for (unsigned i = 1; i < PGMPOOL_IDX_FIRST; i++)
{
/*
* Get the page address.
*/
PPGMPOOLPAGE pPage = &pPool->aPages[i];
union
{
uint64_t *pau64;
uint32_t *pau32;
} u;
u.pau64 = (uint64_t *)PGMPOOL_PAGE_2_PTR(pPool->CTXSUFF(pVM), pPage);
/*
* Mark stuff not present.
*/
switch (pPage->enmKind)
{
case PGMPOOLKIND_ROOT_32BIT_PD:
for (unsigned iPage = 0; iPage < X86_PG_ENTRIES; iPage++)
if ((u.pau32[iPage] & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == X86_PDE_P)
u.pau32[iPage] = 0;
break;
case PGMPOOLKIND_ROOT_PAE_PD:
for (unsigned iPage = 0; iPage < X86_PG_PAE_ENTRIES * X86_PG_PAE_PDPE_ENTRIES; iPage++)
if ((u.pau64[iPage] & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == X86_PDE_P)
u.pau64[iPage] = 0;
break;
case PGMPOOLKIND_ROOT_PDPT:
/* Not root of shadowed pages currently, ignore it. */
break;
case PGMPOOLKIND_ROOT_NESTED:
ASMMemZero32(u.pau64, PAGE_SIZE);
break;
}
}
/*
* Paranoia (to be removed), flag a global CR3 sync.
*/
VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
}
/**
* Flushes the entire cache.
*
* It will assert a global CR3 flush (FF) and assumes the caller is aware of this
* and execute this CR3 flush.
*
* @param pPool The pool.
*/
static void pgmPoolFlushAllInt(PPGMPOOL pPool)
{
STAM_PROFILE_START(&pPool->StatFlushAllInt, a);
LogFlow(("pgmPoolFlushAllInt:\n"));
/*
* If there are no pages in the pool, there is nothing to do.
*/
if (pPool->cCurPages <= PGMPOOL_IDX_FIRST)
{
STAM_PROFILE_STOP(&pPool->StatFlushAllInt, a);
return;
}
/*
* Nuke the free list and reinsert all pages into it.
*/
for (unsigned i = pPool->cCurPages - 1; i >= PGMPOOL_IDX_FIRST; i--)
{
PPGMPOOLPAGE pPage = &pPool->aPages[i];
#ifdef IN_RING3
Assert(pPage->Core.Key == MMPage2Phys(pPool->pVMHC, pPage->pvPageHC));
#endif
#ifdef PGMPOOL_WITH_MONITORING
if (pPage->fMonitored)
pgmPoolMonitorFlush(pPool, pPage);
pPage->iModifiedNext = NIL_PGMPOOL_IDX;
pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
pPage->cModifications = 0;
#endif
pPage->GCPhys = NIL_RTGCPHYS;
pPage->enmKind = PGMPOOLKIND_FREE;
Assert(pPage->idx == i);
pPage->iNext = i + 1;
pPage->fZeroed = false; /* This could probably be optimized, but better safe than sorry. */
pPage->fSeenNonGlobal = false;
pPage->fMonitored= false;
pPage->fCached = false;
pPage->fReusedFlushPending = false;
pPage->fCR3Mix = false;
#ifdef PGMPOOL_WITH_USER_TRACKING
pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
#endif
#ifdef PGMPOOL_WITH_CACHE
pPage->iAgeNext = NIL_PGMPOOL_IDX;
pPage->iAgePrev = NIL_PGMPOOL_IDX;
#endif
}
pPool->aPages[pPool->cCurPages - 1].iNext = NIL_PGMPOOL_IDX;
pPool->iFreeHead = PGMPOOL_IDX_FIRST;
pPool->cUsedPages = 0;
#ifdef PGMPOOL_WITH_USER_TRACKING
/*
* Zap and reinitialize the user records.
*/
pPool->cPresent = 0;
pPool->iUserFreeHead = 0;
PPGMPOOLUSER paUsers = pPool->CTXSUFF(paUsers);
const unsigned cMaxUsers = pPool->cMaxUsers;
for (unsigned i = 0; i < cMaxUsers; i++)
{
paUsers[i].iNext = i + 1;
paUsers[i].iUser = NIL_PGMPOOL_IDX;
paUsers[i].iUserTable = 0xfffffffe;
}
paUsers[cMaxUsers - 1].iNext = NIL_PGMPOOL_USER_INDEX;
#endif
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
/*
* Clear all the GCPhys links and rebuild the phys ext free list.
*/
for (PPGMRAMRANGE pRam = pPool->CTXSUFF(pVM)->pgm.s.CTXALLSUFF(pRamRanges);
pRam;
pRam = CTXALLSUFF(pRam->pNext))
{
unsigned iPage = pRam->cb >> PAGE_SHIFT;
while (iPage-- > 0)
pRam->aPages[iPage].HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
}
pPool->iPhysExtFreeHead = 0;
PPGMPOOLPHYSEXT paPhysExts = pPool->CTXSUFF(paPhysExts);
const unsigned cMaxPhysExts = pPool->cMaxPhysExts;
for (unsigned i = 0; i < cMaxPhysExts; i++)
{
paPhysExts[i].iNext = i + 1;
paPhysExts[i].aidx[0] = NIL_PGMPOOL_IDX;
paPhysExts[i].aidx[1] = NIL_PGMPOOL_IDX;
paPhysExts[i].aidx[2] = NIL_PGMPOOL_IDX;
}
paPhysExts[cMaxPhysExts - 1].iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
#endif
#ifdef PGMPOOL_WITH_MONITORING
/*
* Just zap the modified list.
*/
pPool->cModifiedPages = 0;
pPool->iModifiedHead = NIL_PGMPOOL_IDX;
#endif
#ifdef PGMPOOL_WITH_CACHE
/*
* Clear the GCPhys hash and the age list.
*/
for (unsigned i = 0; i < ELEMENTS(pPool->aiHash); i++)
pPool->aiHash[i] = NIL_PGMPOOL_IDX;
pPool->iAgeHead = NIL_PGMPOOL_IDX;
pPool->iAgeTail = NIL_PGMPOOL_IDX;
#endif
/*
* Flush all the special root pages.
* Reinsert active pages into the hash and ensure monitoring chains are correct.
*/
pgmPoolFlushAllSpecialRoots(pPool);
for (unsigned i = PGMPOOL_IDX_FIRST_SPECIAL; i < PGMPOOL_IDX_FIRST; i++)
{
PPGMPOOLPAGE pPage = &pPool->aPages[i];
pPage->iNext = NIL_PGMPOOL_IDX;
#ifdef PGMPOOL_WITH_MONITORING
pPage->iModifiedNext = NIL_PGMPOOL_IDX;
pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
pPage->cModifications = 0;
/* ASSUMES that we're not sharing with any of the other special pages (safe for now). */
pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
if (pPage->fMonitored)
{
PVM pVM = pPool->CTXSUFF(pVM);
int rc = PGMHandlerPhysicalChangeCallbacks(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1),
pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pPage),
pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pPage),
pPool->pfnAccessHandlerGC, MMHyperCCToGC(pVM, pPage),
pPool->pszAccessHandler);
AssertFatalRCSuccess(rc);
# ifdef PGMPOOL_WITH_CACHE
pgmPoolHashInsert(pPool, pPage);
# endif
}
#endif
#ifdef PGMPOOL_WITH_USER_TRACKING
Assert(pPage->iUserHead == NIL_PGMPOOL_USER_INDEX); /* for now */
#endif
#ifdef PGMPOOL_WITH_CACHE
Assert(pPage->iAgeNext == NIL_PGMPOOL_IDX);
Assert(pPage->iAgePrev == NIL_PGMPOOL_IDX);
#endif
}
STAM_PROFILE_STOP(&pPool->StatFlushAllInt, a);
}
/**
* Flushes a pool page.
*
* This moves the page to the free list after removing all user references to it.
* In GC this will cause a CR3 reload if the page is traced back to an active root page.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @retval VERR_PGM_POOL_CLEARED if the deregistration of the physical handler will cause a light weight pool flush.
* @param pPool The pool.
* @param HCPhys The HC physical address of the shadow page.
*/
int pgmPoolFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
{
int rc = VINF_SUCCESS;
STAM_PROFILE_START(&pPool->StatFlushPage, f);
LogFlow(("pgmPoolFlushPage: pPage=%p:{.Key=%VHp, .idx=%d, .enmKind=%d, .GCPhys=%VGp}\n",
pPage, pPage->Core.Key, pPage->idx, pPage->enmKind, pPage->GCPhys));
/*
* Quietly reject any attempts at flushing any of the special root pages.
*/
if (pPage->idx < PGMPOOL_IDX_FIRST)
{
Log(("pgmPoolFlushPage: special root page, rejected. enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
return VINF_SUCCESS;
}
/*
* Quietly reject any attempts at flushing the currently active shadow CR3 mapping
*/
if ( pPage->enmKind == PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4
&& PGMGetHyperCR3(CTXSUFF(pPool->pVM)) == pPage->Core.Key)
{
Log(("pgmPoolFlushPage: current active shadow CR3, rejected. enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
return VINF_SUCCESS;
}
/* Safety precaution in case we change the paging for other modes too in the future. */
AssertFatal(PGMGetHyperCR3(CTXSUFF(pPool->pVM)) != pPage->Core.Key);
/*
* Mark the page as being in need of a ASMMemZeroPage().
*/
pPage->fZeroed = false;
#ifdef PGMPOOL_WITH_USER_TRACKING
/*
* Clear the page.
*/
pgmPoolTrackClearPageUsers(pPool, pPage);
STAM_PROFILE_START(&pPool->StatTrackDeref,a);
pgmPoolTrackDeref(pPool, pPage);
STAM_PROFILE_STOP(&pPool->StatTrackDeref,a);
#endif
#ifdef PGMPOOL_WITH_CACHE
/*
* Flush it from the cache.
*/
pgmPoolCacheFlushPage(pPool, pPage);
#endif /* PGMPOOL_WITH_CACHE */
#ifdef PGMPOOL_WITH_MONITORING
/*
* Deregistering the monitoring.
*/
if (pPage->fMonitored)
rc = pgmPoolMonitorFlush(pPool, pPage);
#endif
/*
* Free the page.
*/
Assert(pPage->iNext == NIL_PGMPOOL_IDX);
pPage->iNext = pPool->iFreeHead;
pPool->iFreeHead = pPage->idx;
pPage->enmKind = PGMPOOLKIND_FREE;
pPage->GCPhys = NIL_RTGCPHYS;
pPage->fReusedFlushPending = false;
pPool->cUsedPages--;
STAM_PROFILE_STOP(&pPool->StatFlushPage, f);
return rc;
}
/**
* Frees a usage of a pool page.
*
* The caller is responsible to updating the user table so that it no longer
* references the shadow page.
*
* @param pPool The pool.
* @param HCPhys The HC physical address of the shadow page.
* @param iUser The shadow page pool index of the user table.
* @param iUserTable The index into the user table (shadowed).
*/
void pgmPoolFreeByPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
{
STAM_PROFILE_START(&pPool->StatFree, a);
LogFlow(("pgmPoolFreeByPage: pPage=%p:{.Key=%VHp, .idx=%d, enmKind=%d} iUser=%#x iUserTable=%#x\n",
pPage, pPage->Core.Key, pPage->idx, pPage->enmKind, iUser, iUserTable));
Assert(pPage->idx >= PGMPOOL_IDX_FIRST);
#ifdef PGMPOOL_WITH_USER_TRACKING
pgmPoolTrackFreeUser(pPool, pPage, iUser, iUserTable);
#endif
#ifdef PGMPOOL_WITH_CACHE
if (!pPage->fCached)
#endif
pgmPoolFlushPage(pPool, pPage); /* ASSUMES that VERR_PGM_POOL_CLEARED can be ignored here. */
STAM_PROFILE_STOP(&pPool->StatFree, a);
}
/**
* Makes one or more free page free.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
*
* @param pPool The pool.
* @param iUser The user of the page.
*/
static int pgmPoolMakeMoreFreePages(PPGMPOOL pPool, uint16_t iUser)
{
LogFlow(("pgmPoolMakeMoreFreePages: iUser=%#x\n", iUser));
/*
* If the pool isn't full grown yet, expand it.
*/
if (pPool->cCurPages < pPool->cMaxPages)
{
STAM_PROFILE_ADV_SUSPEND(&pPool->StatAlloc, a);
#ifdef IN_RING3
int rc = PGMR3PoolGrow(pPool->pVMHC);
#else
int rc = CTXALLMID(VMM, CallHost)(pPool->CTXSUFF(pVM), VMMCALLHOST_PGM_POOL_GROW, 0);
#endif
if (VBOX_FAILURE(rc))
return rc;
STAM_PROFILE_ADV_RESUME(&pPool->StatAlloc, a);
if (pPool->iFreeHead != NIL_PGMPOOL_IDX)
return VINF_SUCCESS;
}
#ifdef PGMPOOL_WITH_CACHE
/*
* Free one cached page.
*/
return pgmPoolCacheFreeOne(pPool, iUser);
#else
/*
* Flush the pool.
* If we have tracking enabled, it should be possible to come up with
* a cheap replacement strategy...
*/
pgmPoolFlushAllInt(pPool);
return VERR_PGM_POOL_FLUSHED;
#endif
}
/**
* Allocates a page from the pool.
*
* This page may actually be a cached page and not in need of any processing
* on the callers part.
*
* @returns VBox status code.
* @retval VINF_SUCCESS if a NEW page was allocated.
* @retval VINF_PGM_CACHED_PAGE if a CACHED page was returned.
* @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
* @param pVM The VM handle.
* @param GCPhys The GC physical address of the page we're gonna shadow.
* For 4MB and 2MB PD entries, it's the first address the
* shadow PT is covering.
* @param enmKind The kind of mapping.
* @param iUser The shadow page pool index of the user table.
* @param iUserTable The index into the user table (shadowed).
* @param ppPage Where to store the pointer to the page. NULL is stored here on failure.
*/
int pgmPoolAlloc(PVM pVM, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
{
PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
STAM_PROFILE_ADV_START(&pPool->StatAlloc, a);
LogFlow(("pgmPoolAlloc: GCPhys=%VGp enmKind=%d iUser=%#x iUserTable=%#x\n", GCPhys, enmKind, iUser, iUserTable));
*ppPage = NULL;
#ifdef PGMPOOL_WITH_CACHE
if (pPool->fCacheEnabled)
{
int rc2 = pgmPoolCacheAlloc(pPool, GCPhys, enmKind, iUser, iUserTable, ppPage);
if (VBOX_SUCCESS(rc2))
{
STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
LogFlow(("pgmPoolAlloc: cached returns %Vrc *ppPage=%p:{.Key=%VHp, .idx=%d}\n", rc2, *ppPage, (*ppPage)->Core.Key, (*ppPage)->idx));
return rc2;
}
}
#endif
/*
* Allocate a new one.
*/
int rc = VINF_SUCCESS;
uint16_t iNew = pPool->iFreeHead;
if (iNew == NIL_PGMPOOL_IDX)
{
rc = pgmPoolMakeMoreFreePages(pPool, iUser);
if (VBOX_FAILURE(rc))
{
if (rc != VERR_PGM_POOL_CLEARED)
{
Log(("pgmPoolAlloc: returns %Vrc (Free)\n", rc));
STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
return rc;
}
Log(("pgmPoolMakeMoreFreePages failed with %Vrc -> return VERR_PGM_POOL_FLUSHED\n", rc));
rc = VERR_PGM_POOL_FLUSHED;
}
iNew = pPool->iFreeHead;
AssertReleaseReturn(iNew != NIL_PGMPOOL_IDX, VERR_INTERNAL_ERROR);
}
/* unlink the free head */
PPGMPOOLPAGE pPage = &pPool->aPages[iNew];
pPool->iFreeHead = pPage->iNext;
pPage->iNext = NIL_PGMPOOL_IDX;
/*
* Initialize it.
*/
pPool->cUsedPages++; /* physical handler registration / pgmPoolTrackFlushGCPhysPTsSlow requirement. */
pPage->enmKind = enmKind;
pPage->GCPhys = GCPhys;
pPage->fSeenNonGlobal = false; /* Set this to 'true' to disable this feature. */
pPage->fMonitored = false;
pPage->fCached = false;
pPage->fReusedFlushPending = false;
pPage->fCR3Mix = false;
#ifdef PGMPOOL_WITH_MONITORING
pPage->cModifications = 0;
pPage->iModifiedNext = NIL_PGMPOOL_IDX;
pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
#endif
#ifdef PGMPOOL_WITH_USER_TRACKING
pPage->cPresent = 0;
pPage->iFirstPresent = ~0;
/*
* Insert into the tracking and cache. If this fails, free the page.
*/
int rc3 = pgmPoolTrackInsert(pPool, pPage, GCPhys, iUser, iUserTable);
if (VBOX_FAILURE(rc3))
{
if (rc3 != VERR_PGM_POOL_CLEARED)
{
pPool->cUsedPages--;
pPage->enmKind = PGMPOOLKIND_FREE;
pPage->GCPhys = NIL_RTGCPHYS;
pPage->iNext = pPool->iFreeHead;
pPool->iFreeHead = pPage->idx;
STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
Log(("pgmPoolAlloc: returns %Vrc (Insert)\n", rc3));
return rc3;
}
Log(("pgmPoolTrackInsert failed with %Vrc -> return VERR_PGM_POOL_FLUSHED\n", rc3));
rc = VERR_PGM_POOL_FLUSHED;
}
#endif /* PGMPOOL_WITH_USER_TRACKING */
/*
* Commit the allocation, clear the page and return.
*/
#ifdef VBOX_WITH_STATISTICS
if (pPool->cUsedPages > pPool->cUsedPagesHigh)
pPool->cUsedPagesHigh = pPool->cUsedPages;
#endif
if (!pPage->fZeroed)
{
STAM_PROFILE_START(&pPool->StatZeroPage, z);
void *pv = PGMPOOL_PAGE_2_PTR(pVM, pPage);
ASMMemZeroPage(pv);
STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
}
*ppPage = pPage;
LogFlow(("pgmPoolAlloc: returns %Vrc *ppPage=%p:{.Key=%VHp, .idx=%d, .fCached=%RTbool, .fMonitored=%RTbool}\n",
rc, pPage, pPage->Core.Key, pPage->idx, pPage->fCached, pPage->fMonitored));
STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
return rc;
}
/**
* Frees a usage of a pool page.
*
* @param pVM The VM handle.
* @param HCPhys The HC physical address of the shadow page.
* @param iUser The shadow page pool index of the user table.
* @param iUserTable The index into the user table (shadowed).
*/
void pgmPoolFree(PVM pVM, RTHCPHYS HCPhys, uint16_t iUser, uint32_t iUserTable)
{
LogFlow(("pgmPoolFree: HCPhys=%VHp iUser=%#x iUserTable=%#x\n", HCPhys, iUser, iUserTable));
PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
pgmPoolFreeByPage(pPool, pgmPoolGetPage(pPool, HCPhys), iUser, iUserTable);
}
/**
* Gets a in-use page in the pool by it's physical address.
*
* @returns Pointer to the page.
* @param pVM The VM handle.
* @param HCPhys The HC physical address of the shadow page.
* @remark This function will NEVER return NULL. It will assert if HCPhys is invalid.
*/
PPGMPOOLPAGE pgmPoolGetPageByHCPhys(PVM pVM, RTHCPHYS HCPhys)
{
/** @todo profile this! */
PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
PPGMPOOLPAGE pPage = pgmPoolGetPage(pPool, HCPhys);
Log3(("pgmPoolGetPageByHCPhys: HCPhys=%VHp -> %p:{.idx=%d .GCPhys=%VGp .enmKind=%d}\n",
HCPhys, pPage, pPage->idx, pPage->GCPhys, pPage->enmKind));
return pPage;
}
/**
* Flushes the entire cache.
*
* It will assert a global CR3 flush (FF) and assumes the caller is aware of this
* and execute this CR3 flush.
*
* @param pPool The pool.
*/
void pgmPoolFlushAll(PVM pVM)
{
LogFlow(("pgmPoolFlushAll:\n"));
pgmPoolFlushAllInt(pVM->pgm.s.CTXSUFF(pPool));
}