PGMR0SharedPage.cpp revision d89d64f261b0fecda692038bc6351dedf996c563
f9fc8c7343143818644af6caf51a2b2520b8b7a9vboxsync/* $Id$ */
2291faee92ebb5cc9722cd3f22e499900a5a411fvboxsync/** @file
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync * PGM - Page Manager and Monitor, Ring-0.
f9fc8c7343143818644af6caf51a2b2520b8b7a9vboxsync */
c58f1213e628a545081c70e26c6b67a841cff880vboxsync
f9fc8c7343143818644af6caf51a2b2520b8b7a9vboxsync/*
f9fc8c7343143818644af6caf51a2b2520b8b7a9vboxsync * Copyright (C) 2007 Oracle Corporation
f9fc8c7343143818644af6caf51a2b2520b8b7a9vboxsync *
f9fc8c7343143818644af6caf51a2b2520b8b7a9vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
f9fc8c7343143818644af6caf51a2b2520b8b7a9vboxsync * available from http://www.virtualbox.org. This file is free software;
f9fc8c7343143818644af6caf51a2b2520b8b7a9vboxsync * you can redistribute it and/or modify it under the terms of the GNU
f9fc8c7343143818644af6caf51a2b2520b8b7a9vboxsync * General Public License (GPL) as published by the Free Software
f9fc8c7343143818644af6caf51a2b2520b8b7a9vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
f9fc8c7343143818644af6caf51a2b2520b8b7a9vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
f9fc8c7343143818644af6caf51a2b2520b8b7a9vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
546cd3313d635980a2cc168a79a58b4f0858d784vboxsync */
546cd3313d635980a2cc168a79a58b4f0858d784vboxsync
546cd3313d635980a2cc168a79a58b4f0858d784vboxsync/*******************************************************************************
546cd3313d635980a2cc168a79a58b4f0858d784vboxsync* Header Files *
546cd3313d635980a2cc168a79a58b4f0858d784vboxsync*******************************************************************************/
546cd3313d635980a2cc168a79a58b4f0858d784vboxsync#define LOG_GROUP LOG_GROUP_PGM_SHARED
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync#include <VBox/pgm.h>
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync#include <VBox/gmm.h>
f9fc8c7343143818644af6caf51a2b2520b8b7a9vboxsync#include "../PGMInternal.h"
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync#include <VBox/vm.h>
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync#include "../PGMInline.h"
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync#include <VBox/log.h>
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync#include <VBox/err.h>
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync#include <iprt/assert.h>
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync#include <iprt/mem.h>
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync#ifdef VBOX_WITH_PAGE_SHARING
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync/**
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync * Check a registered module for shared page changes
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync *
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync * @returns The following VBox status codes.
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync *
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync * @param pVM The VM handle.
395d92a7732aad3c0b9baecedfabba5113b84485vboxsync * @param idCpu VCPU id
395d92a7732aad3c0b9baecedfabba5113b84485vboxsync * @param pModule Module description
395d92a7732aad3c0b9baecedfabba5113b84485vboxsync * @param cRegions Number of regions
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync * @param pRegions Region array
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync * @param pGVM Pointer to the GVM instance data.
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync */
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsyncVMMR0DECL(int) PGMR0SharedModuleCheck(PVM pVM, PGVM pGVM, VMCPUID idCpu, PGMMSHAREDMODULE pModule, uint32_t cRegions, PGMMSHAREDREGIONDESC pRegions)
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync{
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync int rc = VINF_SUCCESS;
a3f342a86f026f46fed32aae20ca045db066d141vboxsync PGMMSHAREDPAGEDESC paPageDesc = NULL;
a3f342a86f026f46fed32aae20ca045db066d141vboxsync uint32_t cbPreviousRegion = 0;
a3f342a86f026f46fed32aae20ca045db066d141vboxsync bool fFlushTLBs = false;
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync PVMCPU pVCpu = &pVM->aCpus[idCpu];
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync Log(("PGMR0SharedModuleCheck: check %s %s base=%RGv size=%x\n", pModule->szName, pModule->szVersion, pModule->Core.Key, pModule->cbModule));
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync
f9fc8c7343143818644af6caf51a2b2520b8b7a9vboxsync pgmLock(pVM);
15617cf4cb5fa181f4d6f55f987a883cf298cce6vboxsync
a3f342a86f026f46fed32aae20ca045db066d141vboxsync /* Check every region of the shared module. */
for (unsigned idxRegion = 0; idxRegion < cRegions; idxRegion++)
{
Assert((pRegions[idxRegion].cbRegion & 0xfff) == 0);
Assert((pRegions[idxRegion].GCRegionAddr & 0xfff) == 0);
RTGCPTR GCRegion = pRegions[idxRegion].GCRegionAddr;
unsigned cbRegion = pRegions[idxRegion].cbRegion & ~0xfff;
unsigned idxPage = 0;
bool fValidChanges = false;
if (cbPreviousRegion < cbRegion)
{
if (paPageDesc)
RTMemFree(paPageDesc);
paPageDesc = (PGMMSHAREDPAGEDESC)RTMemAlloc((cbRegion >> PAGE_SHIFT) * sizeof(*paPageDesc));
if (!paPageDesc)
{
AssertFailed();
rc = VERR_NO_MEMORY;
goto end;
}
cbPreviousRegion = cbRegion;
}
while (cbRegion)
{
RTGCPHYS GCPhys;
uint64_t fFlags;
/** todo: inefficient to fetch each guest page like this... */
rc = PGMGstGetPage(pVCpu, GCRegion, &fFlags, &GCPhys);
if ( rc == VINF_SUCCESS
&& !(fFlags & X86_PTE_RW)) /* important as we make assumptions about this below! */
{
PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhys);
Assert(!pPage || !PGM_PAGE_IS_BALLOONED(pPage));
if ( pPage
&& PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ALLOCATED)
{
fValidChanges = true;
paPageDesc[idxPage].uHCPhysPageId = PGM_PAGE_GET_PAGEID(pPage);
paPageDesc[idxPage].HCPhys = PGM_PAGE_GET_HCPHYS(pPage);
paPageDesc[idxPage].GCPhys = GCPhys;
}
else
paPageDesc[idxPage].uHCPhysPageId = NIL_GMM_PAGEID;
}
else
paPageDesc[idxPage].uHCPhysPageId = NIL_GMM_PAGEID;
idxPage++;
GCRegion += PAGE_SIZE;
cbRegion -= PAGE_SIZE;
}
if (fValidChanges)
{
rc = GMMR0SharedModuleCheckRange(pGVM, pModule, idxRegion, idxPage, paPageDesc);
AssertRC(rc);
if (RT_FAILURE(rc))
break;
for (unsigned i = 0; i < idxPage; i++)
{
/* Any change for this page? */
if (paPageDesc[i].uHCPhysPageId != NIL_GMM_PAGEID)
{
/** todo: maybe cache these to prevent the nth lookup. */
PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, paPageDesc[i].GCPhys);
if (!pPage)
{
/* Should never happen. */
AssertFailed();
rc = VERR_PGM_PHYS_INVALID_PAGE_ID;
goto end;
}
Assert(PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ALLOCATED);
Log(("PGMR0SharedModuleCheck: shared page gc virt=%RGv phys %RGp host %RHp->%RHp\n", pRegions[idxRegion].GCRegionAddr + i * PAGE_SIZE, paPageDesc[i].GCPhys, PGM_PAGE_GET_HCPHYS(pPage), paPageDesc[i].HCPhys));
if (paPageDesc[i].HCPhys != PGM_PAGE_GET_HCPHYS(pPage))
{
bool fFlush = false;
/* Page was replaced by an existing shared version of it; clear all references first. */
rc = pgmPoolTrackUpdateGCPhys(pVM, paPageDesc[i].GCPhys, pPage, true /* clear the entries */, &fFlush);
if (RT_FAILURE(rc))
{
AssertRC(rc);
goto end;
}
Assert(rc == VINF_SUCCESS || (VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3) && (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)));
if (rc == VINF_SUCCESS)
fFlushTLBs |= fFlush;
/* Update the physical address and page id now. */
PGM_PAGE_SET_HCPHYS(pPage, paPageDesc[i].HCPhys);
PGM_PAGE_SET_PAGEID(pPage, paPageDesc[i].uHCPhysPageId);
/* Invalidate page map TLB entry for this page too. */
PGMPhysInvalidatePageMapTLBEntry(pVM, paPageDesc[i].GCPhys);
pVM->pgm.s.cReusedSharedPages++;
}
/* else nothing changed (== this page is now a shared page), so no need to flush anything. */
pVM->pgm.s.cSharedPages++;
pVM->pgm.s.cPrivatePages--;
PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_SHARED);
}
}
}
else
rc = VINF_SUCCESS; /* nothing to do. */
}
end:
pgmUnlock(pVM);
if (fFlushTLBs)
PGM_INVL_ALL_VCPU_TLBS(pVM);
if (paPageDesc)
RTMemFree(paPageDesc);
return rc;
}
#endif