PGMR0SharedPage.cpp revision 46ae097c942b4a2d5038d9593e312856238da75f
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync/* $Id$ */
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync/** @file
00fa3bc989ffe84474e828c8b90b24284dcfdf0cvboxsync * PGM - Page Manager and Monitor, Page Sharing, Ring-0.
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync */
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync/*
5f4259f329a2073f6644399b6922077adca01529vboxsync * Copyright (C) 2010-2012 Oracle Corporation
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync *
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * available from http://www.virtualbox.org. This file is free software;
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * you can redistribute it and/or modify it under the terms of the GNU
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * General Public License (GPL) as published by the Free Software
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync */
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync/*******************************************************************************
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync* Header Files *
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync*******************************************************************************/
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync#define LOG_GROUP LOG_GROUP_PGM_SHARED
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync#include <VBox/vmm/pgm.h>
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync#include <VBox/vmm/gmm.h>
0c437bb10c61b229407a7517efde04dfe3b1e4a1vboxsync#include "PGMInternal.h"
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync#include <VBox/vmm/vm.h>
0c437bb10c61b229407a7517efde04dfe3b1e4a1vboxsync#include "PGMInline.h"
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync#include <VBox/log.h>
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync#include <VBox/err.h>
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync#include <iprt/assert.h>
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync#include <iprt/mem.h>
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync#ifdef VBOX_WITH_PAGE_SHARING
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync/**
5f4259f329a2073f6644399b6922077adca01529vboxsync * Check a registered module for shared page changes.
5f4259f329a2073f6644399b6922077adca01529vboxsync *
5f4259f329a2073f6644399b6922077adca01529vboxsync * The PGM lock shall be taken prior to calling this method.
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync *
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * @returns The following VBox status codes.
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync *
4bfa7b58e362a1bca0628643c352c137900bf01avboxsync * @param pVM Pointer to the VM.
5f4259f329a2073f6644399b6922077adca01529vboxsync * @param pGVM Pointer to the GVM instance data.
5f4259f329a2073f6644399b6922077adca01529vboxsync * @param idCpu The ID of the calling virtual CPU.
67b4b089c50d0ab8ad847dddd8c0e0812fdadc9evboxsync * @param pModule Global module description.
5f4259f329a2073f6644399b6922077adca01529vboxsync * @param paRegionsGCPtrs Array parallel to pModules->aRegions with the
5f4259f329a2073f6644399b6922077adca01529vboxsync * addresses of the regions in the calling
5f4259f329a2073f6644399b6922077adca01529vboxsync * process.
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync */
5f4259f329a2073f6644399b6922077adca01529vboxsyncVMMR0DECL(int) PGMR0SharedModuleCheck(PVM pVM, PGVM pGVM, VMCPUID idCpu, PGMMSHAREDMODULE pModule, PCRTGCPTR64 paRegionsGCPtrs)
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync{
5f4259f329a2073f6644399b6922077adca01529vboxsync PVMCPU pVCpu = &pVM->aCpus[idCpu];
5f4259f329a2073f6644399b6922077adca01529vboxsync int rc = VINF_SUCCESS;
5f4259f329a2073f6644399b6922077adca01529vboxsync bool fFlushTLBs = false;
5f4259f329a2073f6644399b6922077adca01529vboxsync bool fFlushRemTLBs = false;
5f4259f329a2073f6644399b6922077adca01529vboxsync GMMSHAREDPAGEDESC PageDesc;
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync Log(("PGMR0SharedModuleCheck: check %s %s base=%RGv size=%x\n", pModule->szName, pModule->szVersion, pModule->Core.Key, pModule->cbModule));
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync
addc480d0d7650db6323467bbdab6c21836a2928vboxsync PGM_LOCK_ASSERT_OWNER(pVM); /* This cannot fail as we grab the lock in pgmR3SharedModuleRegRendezvous before calling into ring-0. */
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync
5f4259f329a2073f6644399b6922077adca01529vboxsync /*
5f4259f329a2073f6644399b6922077adca01529vboxsync * Check every region of the shared module.
5f4259f329a2073f6644399b6922077adca01529vboxsync */
5f4259f329a2073f6644399b6922077adca01529vboxsync for (uint32_t idxRegion = 0; idxRegion < pModule->cRegions; idxRegion++)
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync {
5f4259f329a2073f6644399b6922077adca01529vboxsync RTGCPTR GCPtrPage = paRegionsGCPtrs[idxRegion] & ~(RTGCPTR)PAGE_OFFSET_MASK;
5f4259f329a2073f6644399b6922077adca01529vboxsync uint32_t cbLeft = pModule->aRegions[idxRegion].cb; Assert(!(cbLeft & PAGE_OFFSET_MASK));
5f4259f329a2073f6644399b6922077adca01529vboxsync uint32_t idxPage = 0;
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync
5f4259f329a2073f6644399b6922077adca01529vboxsync while (cbLeft)
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync {
5f4259f329a2073f6644399b6922077adca01529vboxsync /** @todo inefficient to fetch each guest page like this... */
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync RTGCPHYS GCPhys;
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync uint64_t fFlags;
5f4259f329a2073f6644399b6922077adca01529vboxsync rc = PGMGstGetPage(pVCpu, GCPtrPage, &fFlags, &GCPhys);
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync if ( rc == VINF_SUCCESS
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync && !(fFlags & X86_PTE_RW)) /* important as we make assumptions about this below! */
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync {
1999ae03c34840fa4d712fd2e020120b2cb7182avboxsync PPGMPAGE pPage = pgmPhysGetPage(pVM, GCPhys);
2fb42335a74fe26c4e2bccbf16f077015e42a5e1vboxsync Assert(!pPage || !PGM_PAGE_IS_BALLOONED(pPage));
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync if ( pPage
5f4259f329a2073f6644399b6922077adca01529vboxsync && PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ALLOCATED
5f4259f329a2073f6644399b6922077adca01529vboxsync && PGM_PAGE_GET_READ_LOCKS(pPage) == 0
5f4259f329a2073f6644399b6922077adca01529vboxsync && PGM_PAGE_GET_WRITE_LOCKS(pPage) == 0 )
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync {
61ec6a59a936b2f248a67be4c4f8f9325f85b702vboxsync PageDesc.idPage = PGM_PAGE_GET_PAGEID(pPage);
61ec6a59a936b2f248a67be4c4f8f9325f85b702vboxsync PageDesc.HCPhys = PGM_PAGE_GET_HCPHYS(pPage);
61ec6a59a936b2f248a67be4c4f8f9325f85b702vboxsync PageDesc.GCPhys = GCPhys;
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync rc = GMMR0SharedModuleCheckPage(pGVM, pModule, idxRegion, idxPage, &PageDesc);
5f4259f329a2073f6644399b6922077adca01529vboxsync if (RT_FAILURE(rc))
5f4259f329a2073f6644399b6922077adca01529vboxsync break;
5f4259f329a2073f6644399b6922077adca01529vboxsync
5f4259f329a2073f6644399b6922077adca01529vboxsync /*
5f4259f329a2073f6644399b6922077adca01529vboxsync * Any change for this page?
5f4259f329a2073f6644399b6922077adca01529vboxsync */
5f4259f329a2073f6644399b6922077adca01529vboxsync if (PageDesc.idPage != NIL_GMM_PAGEID)
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync {
5f4259f329a2073f6644399b6922077adca01529vboxsync Assert(PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ALLOCATED);
5f4259f329a2073f6644399b6922077adca01529vboxsync
5f4259f329a2073f6644399b6922077adca01529vboxsync Log(("PGMR0SharedModuleCheck: shared page gst virt=%RGv phys=%RGp host %RHp->%RHp\n",
5f4259f329a2073f6644399b6922077adca01529vboxsync GCPtrPage, PageDesc.GCPhys, PGM_PAGE_GET_HCPHYS(pPage), PageDesc.HCPhys));
5f4259f329a2073f6644399b6922077adca01529vboxsync
12c8c9f6f737f5ec482fce4d07f44537451c6478vboxsync /* Page was either replaced by an existing shared
12c8c9f6f737f5ec482fce4d07f44537451c6478vboxsync version of it or converted into a read-only shared
12c8c9f6f737f5ec482fce4d07f44537451c6478vboxsync page, so, clear all references. */
12c8c9f6f737f5ec482fce4d07f44537451c6478vboxsync bool fFlush = false;
12c8c9f6f737f5ec482fce4d07f44537451c6478vboxsync rc = pgmPoolTrackUpdateGCPhys(pVM, PageDesc.GCPhys, pPage, true /* clear the entries */, &fFlush);
12c8c9f6f737f5ec482fce4d07f44537451c6478vboxsync Assert( rc == VINF_SUCCESS
46ae097c942b4a2d5038d9593e312856238da75fvboxsync || ( VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3)
12c8c9f6f737f5ec482fce4d07f44537451c6478vboxsync && (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)));
12c8c9f6f737f5ec482fce4d07f44537451c6478vboxsync if (rc == VINF_SUCCESS)
12c8c9f6f737f5ec482fce4d07f44537451c6478vboxsync fFlushTLBs |= fFlush;
12c8c9f6f737f5ec482fce4d07f44537451c6478vboxsync fFlushRemTLBs = true;
12c8c9f6f737f5ec482fce4d07f44537451c6478vboxsync
5f4259f329a2073f6644399b6922077adca01529vboxsync if (PageDesc.HCPhys != PGM_PAGE_GET_HCPHYS(pPage))
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync {
5f4259f329a2073f6644399b6922077adca01529vboxsync /* Update the physical address and page id now. */
5f4259f329a2073f6644399b6922077adca01529vboxsync PGM_PAGE_SET_HCPHYS(pVM, pPage, PageDesc.HCPhys);
5f4259f329a2073f6644399b6922077adca01529vboxsync PGM_PAGE_SET_PAGEID(pVM, pPage, PageDesc.idPage);
5f4259f329a2073f6644399b6922077adca01529vboxsync
5f4259f329a2073f6644399b6922077adca01529vboxsync /* Invalidate page map TLB entry for this page too. */
5f4259f329a2073f6644399b6922077adca01529vboxsync pgmPhysInvalidatePageMapTLBEntry(pVM, PageDesc.GCPhys);
5f4259f329a2073f6644399b6922077adca01529vboxsync pVM->pgm.s.cReusedSharedPages++;
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync }
5f4259f329a2073f6644399b6922077adca01529vboxsync /* else: nothing changed (== this page is now a shared
5f4259f329a2073f6644399b6922077adca01529vboxsync page), so no need to flush anything. */
5f4259f329a2073f6644399b6922077adca01529vboxsync
5f4259f329a2073f6644399b6922077adca01529vboxsync pVM->pgm.s.cSharedPages++;
5f4259f329a2073f6644399b6922077adca01529vboxsync pVM->pgm.s.cPrivatePages--;
5f4259f329a2073f6644399b6922077adca01529vboxsync PGM_PAGE_SET_STATE(pVM, pPage, PGM_PAGE_STATE_SHARED);
05aea370c2b0e20fb84a962ed30a8c22c16d51ccvboxsync
05aea370c2b0e20fb84a962ed30a8c22c16d51ccvboxsync# ifdef VBOX_STRICT /* check sum hack */
05aea370c2b0e20fb84a962ed30a8c22c16d51ccvboxsync pPage->s.u2Unused0 = PageDesc.u32StrictChecksum & 3;
05aea370c2b0e20fb84a962ed30a8c22c16d51ccvboxsync pPage->s.u2Unused1 = (PageDesc.u32StrictChecksum >> 8) & 3;
05aea370c2b0e20fb84a962ed30a8c22c16d51ccvboxsync# endif
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync }
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync }
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync }
8de98485a60eb742b1d720a8cb9747dc03e821a8vboxsync else
8de98485a60eb742b1d720a8cb9747dc03e821a8vboxsync {
faa31dfcb46e5e2fb9c2bf224d113a0ca136ddecvboxsync Assert( rc == VINF_SUCCESS
8de98485a60eb742b1d720a8cb9747dc03e821a8vboxsync || rc == VERR_PAGE_NOT_PRESENT
8de98485a60eb742b1d720a8cb9747dc03e821a8vboxsync || rc == VERR_PAGE_MAP_LEVEL4_NOT_PRESENT
8de98485a60eb742b1d720a8cb9747dc03e821a8vboxsync || rc == VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT
faa31dfcb46e5e2fb9c2bf224d113a0ca136ddecvboxsync || rc == VERR_PAGE_TABLE_NOT_PRESENT);
8de98485a60eb742b1d720a8cb9747dc03e821a8vboxsync rc = VINF_SUCCESS; /* ignore error */
8de98485a60eb742b1d720a8cb9747dc03e821a8vboxsync }
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync idxPage++;
5f4259f329a2073f6644399b6922077adca01529vboxsync GCPtrPage += PAGE_SIZE;
5f4259f329a2073f6644399b6922077adca01529vboxsync cbLeft -= PAGE_SIZE;
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync }
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync }
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync
5f4259f329a2073f6644399b6922077adca01529vboxsync /*
5f4259f329a2073f6644399b6922077adca01529vboxsync * Do TLB flushing if necessary.
5f4259f329a2073f6644399b6922077adca01529vboxsync */
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync if (fFlushTLBs)
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync PGM_INVL_ALL_VCPU_TLBS(pVM);
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync
5f4259f329a2073f6644399b6922077adca01529vboxsync if (fFlushRemTLBs)
5f4259f329a2073f6644399b6922077adca01529vboxsync for (VMCPUID idCurCpu = 0; idCurCpu < pVM->cCpus; idCurCpu++)
5f4259f329a2073f6644399b6922077adca01529vboxsync CPUMSetChangedFlags(&pVM->aCpus[idCurCpu], CPUM_CHANGED_GLOBAL_TLB_FLUSH);
5f4259f329a2073f6644399b6922077adca01529vboxsync
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync return rc;
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync}
4ace9bce00c6d792391a0e064131716659e3a922vboxsync#endif /* VBOX_WITH_PAGE_SHARING */
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync