1 | /* $Id: PGMR0SharedPage.cpp 30202 2010-06-15 14:52:31Z 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 | */
|
---|
46 | VMMR0DECL(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 | Assert(!pPage || !PGM_PAGE_IS_BALLOONED(pPage));
|
---|
95 | if ( pPage
|
---|
96 | && pPage->uStateY == PGM_PAGE_STATE_ALLOCATED)
|
---|
97 | {
|
---|
98 | fValidChanges = true;
|
---|
99 | paPageDesc[idxPage].uHCPhysPageId = PGM_PAGE_GET_PAGEID(pPage);
|
---|
100 | paPageDesc[idxPage].HCPhys = PGM_PAGE_GET_HCPHYS(pPage);
|
---|
101 | paPageDesc[idxPage].GCPhys = GCPhys;
|
---|
102 | }
|
---|
103 | else
|
---|
104 | paPageDesc[idxPage].uHCPhysPageId = NIL_GMM_PAGEID;
|
---|
105 | }
|
---|
106 | else
|
---|
107 | paPageDesc[idxPage].uHCPhysPageId = NIL_GMM_PAGEID;
|
---|
108 |
|
---|
109 | idxPage++;
|
---|
110 | GCRegion += PAGE_SIZE;
|
---|
111 | cbRegion -= PAGE_SIZE;
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (fValidChanges)
|
---|
115 | {
|
---|
116 | rc = GMMR0SharedModuleCheckRange(pGVM, pModule, idxRegion, idxPage, paPageDesc);
|
---|
117 | AssertRC(rc);
|
---|
118 | if (RT_FAILURE(rc))
|
---|
119 | break;
|
---|
120 |
|
---|
121 | for (unsigned i = 0; i < idxPage; i++)
|
---|
122 | {
|
---|
123 | /* Any change for this page? */
|
---|
124 | if (paPageDesc[i].uHCPhysPageId != NIL_GMM_PAGEID)
|
---|
125 | {
|
---|
126 | /** todo: maybe cache these to prevent the nth lookup. */
|
---|
127 | PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, paPageDesc[i].GCPhys);
|
---|
128 | if (!pPage)
|
---|
129 | {
|
---|
130 | /* Should never happen. */
|
---|
131 | AssertFailed();
|
---|
132 | rc = VERR_PGM_PHYS_INVALID_PAGE_ID;
|
---|
133 | goto end;
|
---|
134 | }
|
---|
135 | Assert(pPage->uStateY == PGM_PAGE_STATE_ALLOCATED);
|
---|
136 |
|
---|
137 | 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));
|
---|
138 | if (paPageDesc[i].HCPhys != PGM_PAGE_GET_HCPHYS(pPage))
|
---|
139 | {
|
---|
140 | bool fFlush = false;
|
---|
141 |
|
---|
142 | /* Page was replaced by an existing shared version of it; clear all references first. */
|
---|
143 | rc = pgmPoolTrackUpdateGCPhys(pVM, paPageDesc[i].GCPhys, pPage, true /* clear the entries */, &fFlush);
|
---|
144 | if (RT_FAILURE(rc))
|
---|
145 | {
|
---|
146 | AssertRC(rc);
|
---|
147 | goto end;
|
---|
148 | }
|
---|
149 | Assert(rc == VINF_SUCCESS || (VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3) && (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)));
|
---|
150 | if (rc == VINF_SUCCESS)
|
---|
151 | fFlushTLBs |= fFlush;
|
---|
152 |
|
---|
153 | /* Update the physical address and page id now. */
|
---|
154 | PGM_PAGE_SET_HCPHYS(pPage, paPageDesc[i].HCPhys);
|
---|
155 | PGM_PAGE_SET_PAGEID(pPage, paPageDesc[i].uHCPhysPageId);
|
---|
156 |
|
---|
157 | /* Invalidate page map TLB entry for this page too. */
|
---|
158 | PGMPhysInvalidatePageMapTLBEntry(pVM, paPageDesc[i].GCPhys);
|
---|
159 | pVM->pgm.s.cReusedSharedPages++;
|
---|
160 | }
|
---|
161 | /* else nothing changed (== this page is now a shared page), so no need to flush anything. */
|
---|
162 |
|
---|
163 | pVM->pgm.s.cSharedPages++;
|
---|
164 | pVM->pgm.s.cPrivatePages--;
|
---|
165 | PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_SHARED);
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|
169 | else
|
---|
170 | rc = VINF_SUCCESS; /* nothing to do. */
|
---|
171 | }
|
---|
172 |
|
---|
173 | end:
|
---|
174 | pgmUnlock(pVM);
|
---|
175 | if (fFlushTLBs)
|
---|
176 | PGM_INVL_ALL_VCPU_TLBS(pVM);
|
---|
177 |
|
---|
178 | if (paPageDesc)
|
---|
179 | RTMemFree(paPageDesc);
|
---|
180 |
|
---|
181 | return rc;
|
---|
182 | }
|
---|
183 | #endif
|
---|
184 |
|
---|