1 | /* $Id: PGMR0.cpp 61539 2010-05-12 15:11:09Z sandervl $ */
|
---|
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 pGVM Pointer to the GVM instance data.
|
---|
43 | */
|
---|
44 | VMMR0DECL(int) PGMR0SharedModuleCheckRegion(PVM pVM, VMCPUID idCpu, PGMMSHAREDMODULE pModule, PGVM pGVM)
|
---|
45 | {
|
---|
46 | int rc = VINF_SUCCESS;
|
---|
47 | PGMMSHAREDPAGEDESC paPageDesc = NULL;
|
---|
48 | uint32_t cbPreviousRegion = 0;
|
---|
49 | bool fFlushTLBs = false;
|
---|
50 | PVMCPU pVCpu = &pVM->aCpus[idCpu];
|
---|
51 |
|
---|
52 | Log(("PGMR0SharedModuleCheck: check %s %s base=%RGv size=%x\n", pModule->szName, pModule->szVersion, pModule->Core.Key, pModule->cbModule));
|
---|
53 |
|
---|
54 | pgmLock(pVM);
|
---|
55 |
|
---|
56 | /* Check every region of the shared module. */
|
---|
57 | for (unsigned idxModule = 0; idxModule < pModule->cRegions; idxModule++)
|
---|
58 | {
|
---|
59 | Assert((pModule->aRegions[idxModule].cbRegion & 0xfff) == 0);
|
---|
60 | Assert((pModule->aRegions[idxModule].GCRegionAddr & 0xfff) == 0);
|
---|
61 |
|
---|
62 | RTGCPTR GCRegion = pModule->aRegions[idxModule].GCRegionAddr;
|
---|
63 | unsigned cbRegion = pModule->aRegions[idxModule].cbRegion & ~0xfff;
|
---|
64 | unsigned idxPage = 0;
|
---|
65 | bool fValidChanges = false;
|
---|
66 |
|
---|
67 | if (cbPreviousRegion < cbRegion)
|
---|
68 | {
|
---|
69 | if (paPageDesc)
|
---|
70 | RTMemFree(paPageDesc);
|
---|
71 |
|
---|
72 | paPageDesc = (PGMMSHAREDPAGEDESC)RTMemAlloc((cbRegion >> PAGE_SHIFT) * sizeof(*paPageDesc));
|
---|
73 | if (!paPageDesc)
|
---|
74 | {
|
---|
75 | AssertFailed();
|
---|
76 | rc = VERR_NO_MEMORY;
|
---|
77 | goto end;
|
---|
78 | }
|
---|
79 | cbPreviousRegion = cbRegion;
|
---|
80 | }
|
---|
81 |
|
---|
82 | while (cbRegion)
|
---|
83 | {
|
---|
84 | RTGCPHYS GCPhys;
|
---|
85 | uint64_t fFlags;
|
---|
86 |
|
---|
87 | rc = PGMGstGetPage(pVCpu, GCRegion, &fFlags, &GCPhys);
|
---|
88 | if ( rc == VINF_SUCCESS
|
---|
89 | && !(fFlags & X86_PTE_RW)) /* important as we make assumptions about this below! */
|
---|
90 | {
|
---|
91 | PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhys);
|
---|
92 | if ( pPage
|
---|
93 | && !PGM_PAGE_IS_SHARED(pPage))
|
---|
94 | {
|
---|
95 | fValidChanges = true;
|
---|
96 | paPageDesc[idxPage].uHCPhysPageId = PGM_PAGE_GET_PAGEID(pPage);
|
---|
97 | paPageDesc[idxPage].HCPhys = PGM_PAGE_GET_HCPHYS(pPage);
|
---|
98 | paPageDesc[idxPage].GCPhys = GCPhys;
|
---|
99 | }
|
---|
100 | else
|
---|
101 | paPageDesc[idxPage].uHCPhysPageId = NIL_GMM_PAGEID;
|
---|
102 | }
|
---|
103 | else
|
---|
104 | paPageDesc[idxPage].uHCPhysPageId = NIL_GMM_PAGEID;
|
---|
105 |
|
---|
106 | idxPage++;
|
---|
107 | GCRegion += PAGE_SIZE;
|
---|
108 | cbRegion -= PAGE_SIZE;
|
---|
109 | }
|
---|
110 |
|
---|
111 | if (fValidChanges)
|
---|
112 | {
|
---|
113 | rc = GMMR0SharedModuleCheckRange(pGVM, pModule, idxModule, idxPage, paPageDesc);
|
---|
114 | AssertRC(rc);
|
---|
115 | if (RT_FAILURE(rc))
|
---|
116 | break;
|
---|
117 |
|
---|
118 | for (unsigned i = 0; i < idxPage; i++)
|
---|
119 | {
|
---|
120 | /* Any change for this page? */
|
---|
121 | if (paPageDesc[i].uHCPhysPageId != NIL_GMM_PAGEID)
|
---|
122 | {
|
---|
123 | /** todo: maybe cache these to prevent the nth lookup. */
|
---|
124 | PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, paPageDesc[i].GCPhys);
|
---|
125 | if (!pPage)
|
---|
126 | {
|
---|
127 | /* Should never happen. */
|
---|
128 | AssertFailed();
|
---|
129 | rc = VERR_PGM_PHYS_INVALID_PAGE_ID;
|
---|
130 | goto end;
|
---|
131 | }
|
---|
132 | Assert(!PGM_PAGE_IS_SHARED(pPage));
|
---|
133 |
|
---|
134 | Log(("PGMR0SharedModuleCheck: shared page gc virt=%RGv phys %RGp host %RHp->%RHp\n", pModule->aRegions[idxModule].GCRegionAddr + i * PAGE_SIZE, paPageDesc[i].GCPhys, PGM_PAGE_GET_HCPHYS(pPage), paPageDesc[i].HCPhys));
|
---|
135 | if (paPageDesc[i].HCPhys != PGM_PAGE_GET_HCPHYS(pPage))
|
---|
136 | {
|
---|
137 | bool fFlush = false;
|
---|
138 |
|
---|
139 | /* Page was replaced by an existing shared version of it; clear all references first. */
|
---|
140 | rc = pgmPoolTrackUpdateGCPhys(pVM, paPageDesc[i].GCPhys, pPage, true /* clear the entries */, &fFlush);
|
---|
141 | if (RT_FAILURE(rc))
|
---|
142 | {
|
---|
143 | AssertRC(rc);
|
---|
144 | goto end;
|
---|
145 | }
|
---|
146 | Assert(rc == VINF_SUCCESS || (VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3) && (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)));
|
---|
147 | if (rc == VINF_SUCCESS)
|
---|
148 | fFlushTLBs |= fFlush;
|
---|
149 |
|
---|
150 | /* Update the physical address and page id now. */
|
---|
151 | PGM_PAGE_SET_HCPHYS(pPage, paPageDesc[i].HCPhys);
|
---|
152 | PGM_PAGE_SET_PAGEID(pPage, paPageDesc[i].uHCPhysPageId);
|
---|
153 |
|
---|
154 | /* Invalidate page map TLB entry for this page too. */
|
---|
155 | PGMPhysInvalidatePageMapTLBEntry(pVM, paPageDesc[i].GCPhys);
|
---|
156 | pVM->pgm.s.cReusedSharedPages++;
|
---|
157 | }
|
---|
158 | /* else nothing changed (== this page is now a shared page), so no need to flush anything. */
|
---|
159 |
|
---|
160 | pVM->pgm.s.cSharedPages++;
|
---|
161 | pVM->pgm.s.cPrivatePages--;
|
---|
162 | PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_SHARED);
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|
166 | else
|
---|
167 | rc = VINF_SUCCESS; /* nothing to do. */
|
---|
168 | }
|
---|
169 |
|
---|
170 | end:
|
---|
171 | pgmUnlock(pVM);
|
---|
172 | if (fFlushTLBs)
|
---|
173 | PGM_INVL_ALL_VCPU_TLBS(pVM);
|
---|
174 |
|
---|
175 | if (paPageDesc)
|
---|
176 | RTMemFree(paPageDesc);
|
---|
177 |
|
---|
178 | return rc;
|
---|
179 | }
|
---|
180 | #endif
|
---|
181 |
|
---|