VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR0/PGMR0SharedPage.cpp@ 30060

Last change on this file since 30060 was 30027, checked in by vboxsync, 14 years ago

Deal with different load addresses of shared modules

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 6.8 KB
Line 
1/* $Id: PGMR0SharedPage.cpp 30027 2010-06-04 11:06:02Z vboxsync $ */
2/** @file
3 * PGM - Page Manager and Monitor, Ring-0.
4 */
5
6/*
7 * Copyright (C) 2007 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21#define LOG_GROUP LOG_GROUP_PGM_SHARED
22#include <VBox/pgm.h>
23#include <VBox/gmm.h>
24#include "../PGMInternal.h"
25#include <VBox/vm.h>
26#include "../PGMInline.h"
27#include <VBox/log.h>
28#include <VBox/err.h>
29#include <iprt/assert.h>
30#include <iprt/mem.h>
31
32
33#ifdef VBOX_WITH_PAGE_SHARING
34/**
35 * Check a registered module for shared page changes
36 *
37 * @returns The following VBox status codes.
38 *
39 * @param pVM The VM handle.
40 * @param idCpu VCPU id
41 * @param pModule Module description
42 * @param cRegions Number of regions
43 * @param pRegions Region array
44 * @param pGVM Pointer to the GVM instance data.
45 */
46VMMR0DECL(int) PGMR0SharedModuleCheck(PVM pVM, PGVM pGVM, VMCPUID idCpu, PGMMSHAREDMODULE pModule, uint32_t cRegions, PGMMSHAREDREGIONDESC pRegions)
47{
48 int rc = VINF_SUCCESS;
49 PGMMSHAREDPAGEDESC paPageDesc = NULL;
50 uint32_t cbPreviousRegion = 0;
51 bool fFlushTLBs = false;
52 PVMCPU pVCpu = &pVM->aCpus[idCpu];
53
54 Log(("PGMR0SharedModuleCheck: check %s %s base=%RGv size=%x\n", pModule->szName, pModule->szVersion, pModule->Core.Key, pModule->cbModule));
55
56 pgmLock(pVM);
57
58 /* Check every region of the shared module. */
59 for (unsigned idxRegion = 0; idxRegion < cRegions; idxRegion++)
60 {
61 Assert((pRegions[idxRegion].cbRegion & 0xfff) == 0);
62 Assert((pRegions[idxRegion].GCRegionAddr & 0xfff) == 0);
63
64 RTGCPTR GCRegion = pRegions[idxRegion].GCRegionAddr;
65 unsigned cbRegion = pRegions[idxRegion].cbRegion & ~0xfff;
66 unsigned idxPage = 0;
67 bool fValidChanges = false;
68
69 if (cbPreviousRegion < cbRegion)
70 {
71 if (paPageDesc)
72 RTMemFree(paPageDesc);
73
74 paPageDesc = (PGMMSHAREDPAGEDESC)RTMemAlloc((cbRegion >> PAGE_SHIFT) * sizeof(*paPageDesc));
75 if (!paPageDesc)
76 {
77 AssertFailed();
78 rc = VERR_NO_MEMORY;
79 goto end;
80 }
81 cbPreviousRegion = cbRegion;
82 }
83
84 while (cbRegion)
85 {
86 RTGCPHYS GCPhys;
87 uint64_t fFlags;
88
89 rc = PGMGstGetPage(pVCpu, GCRegion, &fFlags, &GCPhys);
90 if ( rc == VINF_SUCCESS
91 && !(fFlags & X86_PTE_RW)) /* important as we make assumptions about this below! */
92 {
93 PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhys);
94 if ( pPage
95 && !PGM_PAGE_IS_SHARED(pPage))
96 {
97 fValidChanges = true;
98 paPageDesc[idxPage].uHCPhysPageId = PGM_PAGE_GET_PAGEID(pPage);
99 paPageDesc[idxPage].HCPhys = PGM_PAGE_GET_HCPHYS(pPage);
100 paPageDesc[idxPage].GCPhys = GCPhys;
101 }
102 else
103 paPageDesc[idxPage].uHCPhysPageId = NIL_GMM_PAGEID;
104 }
105 else
106 paPageDesc[idxPage].uHCPhysPageId = NIL_GMM_PAGEID;
107
108 idxPage++;
109 GCRegion += PAGE_SIZE;
110 cbRegion -= PAGE_SIZE;
111 }
112
113 if (fValidChanges)
114 {
115 rc = GMMR0SharedModuleCheckRange(pGVM, pModule, idxRegion, idxPage, paPageDesc);
116 AssertRC(rc);
117 if (RT_FAILURE(rc))
118 break;
119
120 for (unsigned i = 0; i < idxPage; i++)
121 {
122 /* Any change for this page? */
123 if (paPageDesc[i].uHCPhysPageId != NIL_GMM_PAGEID)
124 {
125 /** todo: maybe cache these to prevent the nth lookup. */
126 PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, paPageDesc[i].GCPhys);
127 if (!pPage)
128 {
129 /* Should never happen. */
130 AssertFailed();
131 rc = VERR_PGM_PHYS_INVALID_PAGE_ID;
132 goto end;
133 }
134 Assert(!PGM_PAGE_IS_SHARED(pPage));
135
136 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));
137 if (paPageDesc[i].HCPhys != PGM_PAGE_GET_HCPHYS(pPage))
138 {
139 bool fFlush = false;
140
141 /* Page was replaced by an existing shared version of it; clear all references first. */
142 rc = pgmPoolTrackUpdateGCPhys(pVM, paPageDesc[i].GCPhys, pPage, true /* clear the entries */, &fFlush);
143 if (RT_FAILURE(rc))
144 {
145 AssertRC(rc);
146 goto end;
147 }
148 Assert(rc == VINF_SUCCESS || (VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3) && (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)));
149 if (rc == VINF_SUCCESS)
150 fFlushTLBs |= fFlush;
151
152 /* Update the physical address and page id now. */
153 PGM_PAGE_SET_HCPHYS(pPage, paPageDesc[i].HCPhys);
154 PGM_PAGE_SET_PAGEID(pPage, paPageDesc[i].uHCPhysPageId);
155
156 /* Invalidate page map TLB entry for this page too. */
157 PGMPhysInvalidatePageMapTLBEntry(pVM, paPageDesc[i].GCPhys);
158 pVM->pgm.s.cReusedSharedPages++;
159 }
160 /* else nothing changed (== this page is now a shared page), so no need to flush anything. */
161
162 pVM->pgm.s.cSharedPages++;
163 pVM->pgm.s.cPrivatePages--;
164 PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_SHARED);
165 }
166 }
167 }
168 else
169 rc = VINF_SUCCESS; /* nothing to do. */
170 }
171
172end:
173 pgmUnlock(pVM);
174 if (fFlushTLBs)
175 PGM_INVL_ALL_VCPU_TLBS(pVM);
176
177 if (paPageDesc)
178 RTMemFree(paPageDesc);
179
180 return rc;
181}
182#endif
183
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette