1 | /* $Id: PGMAllBth.h 23 2007-01-15 14:08:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox - Page Manager, Shadow+Guest Paging Template - All context code.
|
---|
4 | *
|
---|
5 | * This file is a big challenge!
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License as published by the Free Software Foundation,
|
---|
15 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
16 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
17 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | *
|
---|
19 | * If you received this file as part of a commercial VirtualBox
|
---|
20 | * distribution, then only the terms of your commercial VirtualBox
|
---|
21 | * license agreement apply instead of the previous paragraph.
|
---|
22 | */
|
---|
23 |
|
---|
24 | /*******************************************************************************
|
---|
25 | * Internal Functions *
|
---|
26 | *******************************************************************************/
|
---|
27 | __BEGIN_DECLS
|
---|
28 | PGM_BTH_DECL(int, Trap0eHandler)(PVM pVM, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault);
|
---|
29 | PGM_BTH_DECL(int, InvalidatePage)(PVM pVM, RTGCUINTPTR GCPtrPage);
|
---|
30 | PGM_BTH_DECL(int, SyncPage)(PVM pVM, VBOXPDE PdeSrc, RTGCUINTPTR GCPtrPage, unsigned cPages, unsigned uErr);
|
---|
31 | PGM_BTH_DECL(int, CheckPageFault)(PVM pVM, uint32_t uErr, PSHWPDE pPdeDst, PVBOXPDE pPdeSrc, RTGCUINTPTR GCPtrPage);
|
---|
32 | PGM_BTH_DECL(int, SyncPT)(PVM pVM, unsigned iPD, PVBOXPD pPDSrc, RTGCUINTPTR GCPtrPage);
|
---|
33 | PGM_BTH_DECL(int, VerifyAccessSyncPage)(PVM pVM, RTGCUINTPTR Addr, unsigned fPage, unsigned uErr);
|
---|
34 | PGM_BTH_DECL(int, PrefetchPage)(PVM pVM, RTGCUINTPTR GCPtrPage);
|
---|
35 | PGM_BTH_DECL(int, SyncCR3)(PVM pVM, uint32_t cr0, uint32_t cr3, uint32_t cr4, bool fGlobal);
|
---|
36 | #ifdef VBOX_STRICT
|
---|
37 | PGM_BTH_DECL(unsigned, AssertCR3)(PVM pVM, uint32_t cr3, uint32_t cr4, RTGCUINTPTR GCPtr = 0, RTGCUINTPTR cb = ~(RTGCUINTPTR)0);
|
---|
38 | #endif
|
---|
39 | #ifdef PGMPOOL_WITH_USER_TRACKING
|
---|
40 | DECLINLINE(void) PGM_BTH_NAME(SyncPageWorkerTrackDeref)(PVM pVM, PPGMPOOLPAGE pShwPage, RTHCPHYS HCPhys);
|
---|
41 | #endif
|
---|
42 | __END_DECLS
|
---|
43 |
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * #PF Handler for raw-mode guest execution.
|
---|
47 | *
|
---|
48 | * @returns VBox status code (appropriate for trap handling and GC return).
|
---|
49 | * @param pVM VM Handle.
|
---|
50 | * @param uErr The trap error code.
|
---|
51 | * @param pRegFrame Trap register frame.
|
---|
52 | * @param pvFault The fault address.
|
---|
53 | */
|
---|
54 | PGM_BTH_DECL(int, Trap0eHandler)(PVM pVM, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault)
|
---|
55 | {
|
---|
56 | #if PGM_GST_TYPE == PGM_TYPE_32BIT
|
---|
57 | # if PGM_SHW_TYPE != PGM_TYPE_32BIT && PGM_SHW_TYPE != PGM_TYPE_PAE
|
---|
58 | # error "32-bit guest mode is only implemented for 32-bit and PAE shadow modes."
|
---|
59 | # endif
|
---|
60 |
|
---|
61 | # ifdef PGM_CACHE_VERY_STRICT
|
---|
62 | pgmCacheCheckPD(pVM, CPUMGetGuestCR0(pVM), CPUMGetGuestCR3(pVM), CPUMGetGuestCR4(pVM));
|
---|
63 | # endif
|
---|
64 |
|
---|
65 | # if PGM_SHW_TYPE == PGM_TYPE_PAE
|
---|
66 | /*
|
---|
67 | * Hide the instruction fetch trap indicator for now.
|
---|
68 | */
|
---|
69 | /** @todo NXE will change this and we must fix NXE in the switcher too! */
|
---|
70 | if (uErr & X86_TRAP_PF_ID)
|
---|
71 | {
|
---|
72 | uErr &= ~X86_TRAP_PF_ID;
|
---|
73 | TRPMSetErrorCode(pVM, uErr);
|
---|
74 | }
|
---|
75 | # endif
|
---|
76 |
|
---|
77 | /*
|
---|
78 | * Get PDs.
|
---|
79 | */
|
---|
80 | int rc;
|
---|
81 | PVBOXPD pPDSrc = CTXSUFF(pVM->pgm.s.pGuestPD);
|
---|
82 | const unsigned iPDSrc = (uintptr_t)pvFault >> GST_PD_SHIFT;
|
---|
83 | const unsigned iPDDst = (uintptr_t)pvFault >> SHW_PD_SHIFT;
|
---|
84 | # if PGM_SHW_TYPE == PGM_TYPE_32BIT
|
---|
85 | PX86PD pPDDst = pVM->pgm.s.CTXMID(p,32BitPD);
|
---|
86 | # else /* PAE */
|
---|
87 | PX86PDPAE pPDDst = pVM->pgm.s.CTXMID(ap,PaePDs)[0]; /* We treat this as a PD with 2048 entries. */
|
---|
88 | # endif
|
---|
89 |
|
---|
90 |
|
---|
91 | # ifdef PGM_SYNC_DIRTY_BIT
|
---|
92 | /*
|
---|
93 | * If we successfully correct the write protection fault due to dirty bit
|
---|
94 | * tracking, or this page fault is a genuine one, then return immediately.
|
---|
95 | */
|
---|
96 | STAM_PROFILE_START(&pVM->pgm.s.StatCheckPageFault, e);
|
---|
97 | rc = PGM_BTH_NAME(CheckPageFault)(pVM, uErr, &pPDDst->a[iPDDst], &pPDSrc->a[iPDSrc], (RTGCUINTPTR)pvFault);
|
---|
98 | STAM_PROFILE_STOP(&pVM->pgm.s.StatCheckPageFault, e);
|
---|
99 | if ( rc == VINF_PGM_HANDLED_DIRTY_BIT_FAULT
|
---|
100 | || rc == VINF_EM_RAW_GUEST_TRAP)
|
---|
101 | {
|
---|
102 | STAM_STATS({ pVM->pgm.s.CTXSUFF(pStatTrap0eAttribution)
|
---|
103 | = rc == VINF_PGM_HANDLED_DIRTY_BIT_FAULT ? &pVM->pgm.s.StatTrap0eDirtyAndAccessedBits : &pVM->pgm.s.StatTrap0eGuestTrap; });
|
---|
104 | LogBird(("Trap0eHandler: returns %s\n", rc == VINF_PGM_HANDLED_DIRTY_BIT_FAULT ? "VINF_SUCCESS" : "VINF_EM_RAW_GUEST_TRAP"));
|
---|
105 | return rc == VINF_PGM_HANDLED_DIRTY_BIT_FAULT ? VINF_SUCCESS : rc;
|
---|
106 | }
|
---|
107 | # endif
|
---|
108 | STAM_COUNTER_INC(&pVM->pgm.s.StatGCTrap0ePD[iPDSrc]);
|
---|
109 |
|
---|
110 | /*
|
---|
111 | * A common case is the not-present error caused by lazy page table syncing.
|
---|
112 | *
|
---|
113 | * It is IMPORTANT that we weed out any access to non-present shadow PDEs here
|
---|
114 | * so we can safely assume that the shadow PT is present when calling SyncPage later.
|
---|
115 | *
|
---|
116 | * On failure, we ASSUME that SyncPT is out of memory or detected some kind
|
---|
117 | * of mapping conflict and defer to SyncCR3 in R3.
|
---|
118 | * (Again, we do NOT support access handlers for non-present guest pages.)
|
---|
119 | *
|
---|
120 | */
|
---|
121 | VBOXPDE PdeSrc = pPDSrc->a[iPDSrc];
|
---|
122 | if ( !(uErr & X86_TRAP_PF_P) /* not set means page not present instead of page protection violation */
|
---|
123 | && !pPDDst->a[iPDDst].n.u1Present
|
---|
124 | && PdeSrc.n.u1Present)
|
---|
125 |
|
---|
126 | {
|
---|
127 | STAM_STATS({ pVM->pgm.s.CTXSUFF(pStatTrap0eAttribution) = &pVM->pgm.s.StatTrap0eSyncPT; });
|
---|
128 | STAM_PROFILE_START(&pVM->pgm.s.StatLazySyncPT, f);
|
---|
129 | LogFlow(("=>SyncPT %04x = %08x\n", iPDSrc, PdeSrc.au32[0]));
|
---|
130 | rc = PGM_BTH_NAME(SyncPT)(pVM, iPDSrc, pPDSrc, (RTGCUINTPTR)pvFault);
|
---|
131 | if (VBOX_SUCCESS(rc))
|
---|
132 | {
|
---|
133 | STAM_PROFILE_STOP(&pVM->pgm.s.StatLazySyncPT, f);
|
---|
134 | return rc;
|
---|
135 | }
|
---|
136 | Log(("SyncPT: %d failed!! rc=%d\n", iPDSrc, rc));
|
---|
137 | VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3); /** @todo no need to do global sync, right? */
|
---|
138 | STAM_PROFILE_STOP(&pVM->pgm.s.StatLazySyncPT, f);
|
---|
139 | return VINF_PGM_SYNC_CR3;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /*
|
---|
143 | * Check if this address is within any of our mappings.
|
---|
144 | *
|
---|
145 | * This is *very* fast and it's gonna save us a bit of effort below and prevent
|
---|
146 | * us from screwing ourself with MMIO2 pages which have a GC Mapping (VRam).
|
---|
147 | * (BTW, it's impossible to have physical access handlers in a mapping.)
|
---|
148 | */
|
---|
149 | if (pgmMapAreMappingsEnabled(&pVM->pgm.s))
|
---|
150 | {
|
---|
151 | STAM_PROFILE_START(&pVM->pgm.s.StatMapping, a);
|
---|
152 | PPGMMAPPING pMapping = CTXSUFF(pVM->pgm.s.pMappings);
|
---|
153 | for ( ; pMapping; pMapping = CTXSUFF(pMapping->pNext))
|
---|
154 | {
|
---|
155 | if ((uintptr_t)pvFault < (uintptr_t)pMapping->GCPtr)
|
---|
156 | break;
|
---|
157 | if ((uintptr_t)pvFault - (uintptr_t)pMapping->GCPtr < pMapping->cb)
|
---|
158 | {
|
---|
159 | /*
|
---|
160 | * The first thing we check is if we've got an undetected conflict.
|
---|
161 | */
|
---|
162 | if (!pVM->pgm.s.fMappingsFixed)
|
---|
163 | {
|
---|
164 | unsigned iPT = pMapping->cPTs;
|
---|
165 | while (iPT-- > 0)
|
---|
166 | if (pPDSrc->a[iPDSrc + iPT].n.u1Present)
|
---|
167 | {
|
---|
168 | STAM_COUNTER_INC(&pVM->pgm.s.StatGCTrap0eConflicts);
|
---|
169 | Log(("Trap0e: Detected Conflict %VGv-%VGv\n", pMapping->GCPtr, pMapping->GCPtrLast));
|
---|
170 | VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3); /** @todo no need to do global sync,right? */
|
---|
171 | STAM_PROFILE_STOP(&pVM->pgm.s.StatMapping, a);
|
---|
172 | return VINF_PGM_SYNC_CR3;
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | /*
|
---|
177 | * Check if the fault address is in a virtual page access handler range.
|
---|
178 | */
|
---|
179 | PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrRangeGet(&CTXSUFF(pVM->pgm.s.pTrees)->VirtHandlers, pvFault);
|
---|
180 | if ( pCur
|
---|
181 | && pCur->enmType != PGMVIRTHANDLERTYPE_EIP
|
---|
182 | && (uintptr_t)pvFault - (uintptr_t)pCur->GCPtr < pCur->cb
|
---|
183 | && ( uErr & X86_TRAP_PF_RW
|
---|
184 | || ( pCur->enmType != PGMVIRTHANDLERTYPE_WRITE
|
---|
185 | && pCur->enmType != PGMVIRTHANDLERTYPE_HYPERVISOR) ) ) /** r=bird: <- this is probably wrong. */
|
---|
186 | {
|
---|
187 | #ifdef IN_GC
|
---|
188 | STAM_PROFILE_START(&pCur->Stat, h);
|
---|
189 | rc = CTXSUFF(pCur->pfnHandler)(pVM, uErr, pRegFrame, pvFault, pCur->GCPtr, (uintptr_t)pvFault - (uintptr_t)pCur->GCPtr);
|
---|
190 | STAM_PROFILE_STOP(&pCur->Stat, h);
|
---|
191 | #else
|
---|
192 | AssertFailed();
|
---|
193 | rc = VINF_EM_RAW_EMULATE_INSTR; /* can't happen with VMX */
|
---|
194 | #endif
|
---|
195 | STAM_COUNTER_INC(&pVM->pgm.s.StatTrap0eMapHandler);
|
---|
196 | STAM_PROFILE_STOP(&pVM->pgm.s.StatMapping, a);
|
---|
197 | return rc;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /*
|
---|
201 | * Check if the EIP is in a virtual page access handler range.
|
---|
202 | */
|
---|
203 | if ((pRegFrame->ss & X86_SEL_RPL) == 1)
|
---|
204 | {
|
---|
205 | RTGCPTR pvEIP;
|
---|
206 | rc = SELMValidateAndConvertCSAddr(pVM, pRegFrame->ss, pRegFrame->cs, &pRegFrame->csHid, (RTGCPTR)pRegFrame->eip, &pvEIP);
|
---|
207 | if (VBOX_SUCCESS(rc))
|
---|
208 | {
|
---|
209 | PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrRangeGet(&CTXSUFF(pVM->pgm.s.pTrees)->VirtHandlers, pvEIP);
|
---|
210 | if ( pCur
|
---|
211 | && pCur->enmType == PGMVIRTHANDLERTYPE_EIP
|
---|
212 | && (uintptr_t)pvEIP - (uintptr_t)pCur->GCPtr < pCur->cb)
|
---|
213 | {
|
---|
214 | #ifdef IN_GC
|
---|
215 | STAM_PROFILE_START(&pCur->Stat, h);
|
---|
216 | rc = CTXSUFF(pCur->pfnHandler)(pVM, uErr, pRegFrame, pvFault, pCur->GCPtr, (uintptr_t)pvEIP - (uintptr_t)pCur->GCPtr);
|
---|
217 | STAM_PROFILE_STOP(&pCur->Stat, h);
|
---|
218 | #else
|
---|
219 | AssertFailed();
|
---|
220 | rc = VINF_EM_RAW_EMULATE_INSTR; /* can't happen with VMX */
|
---|
221 | #endif
|
---|
222 | STAM_COUNTER_INC(&pVM->pgm.s.StatTrap0eMapHandler);
|
---|
223 | STAM_PROFILE_STOP(&pVM->pgm.s.StatMapping, a);
|
---|
224 | return rc;
|
---|
225 | }
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | /*
|
---|
230 | * Pretend we're not here and let the guest handle the trap.
|
---|
231 | */
|
---|
232 | TRPMSetErrorCode(pVM, uErr & ~X86_TRAP_PF_P);
|
---|
233 | STAM_COUNTER_INC(&pVM->pgm.s.StatGCTrap0eMap);
|
---|
234 | LogFlow(("PGM: Mapping access -> route trap to recompiler!\n"));
|
---|
235 | STAM_PROFILE_STOP(&pVM->pgm.s.StatMapping, a);
|
---|
236 | return VINF_EM_RAW_GUEST_TRAP;
|
---|
237 | }
|
---|
238 | }
|
---|
239 | STAM_PROFILE_STOP(&pVM->pgm.s.StatMapping, a);
|
---|
240 | } /* pgmAreMappingsEnabled(&pVM->pgm.s) */
|
---|
241 |
|
---|
242 | /*
|
---|
243 | * Check if this fault address is flagged for special treatment,
|
---|
244 | * which means we'll have to figure out the physical address and
|
---|
245 | * check flags associated with it.
|
---|
246 | *
|
---|
247 | * ASSUME that we can limit any special access handling to pages
|
---|
248 | * in page tables which the guest believes to be present.
|
---|
249 | */
|
---|
250 | if (PdeSrc.n.u1Present)
|
---|
251 | {
|
---|
252 | RTGCPHYS GCPhys = ~0U;
|
---|
253 | uint32_t cr4 = CPUMGetGuestCR4(pVM);
|
---|
254 | if ( PdeSrc.b.u1Size
|
---|
255 | && (cr4 & X86_CR4_PSE))
|
---|
256 | GCPhys = (PdeSrc.u & X86_PDE4M_PG_MASK)
|
---|
257 | | ((RTGCPHYS)pvFault & (PAGE_OFFSET_MASK_BIG ^ PAGE_OFFSET_MASK));
|
---|
258 | else
|
---|
259 | {
|
---|
260 | PVBOXPT pPTSrc;
|
---|
261 | # ifdef IN_GC
|
---|
262 | rc = PGMGCDynMapGCPage(pVM, PdeSrc.u & X86_PDE_PG_MASK, (void **)&pPTSrc);
|
---|
263 | # else
|
---|
264 | pPTSrc = (PVBOXPT)MMPhysGCPhys2HCVirt(pVM, PdeSrc.u & X86_PDE_PG_MASK);
|
---|
265 | if (pPTSrc == 0)
|
---|
266 | rc = VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
|
---|
267 | # endif
|
---|
268 | if (VBOX_SUCCESS(rc))
|
---|
269 | {
|
---|
270 | unsigned iPTESrc = ((uintptr_t)pvFault >> PAGE_SHIFT) & PTE_MASK;
|
---|
271 | if (pPTSrc->a[iPTESrc].n.u1Present)
|
---|
272 | GCPhys = pPTSrc->a[iPTESrc].u & X86_PTE_PG_MASK;
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | /*
|
---|
277 | * If we have a GC address we'll check if it has any flags set.
|
---|
278 | */
|
---|
279 | if (GCPhys != ~0U)
|
---|
280 | {
|
---|
281 | STAM_PROFILE_START(&pVM->pgm.s.StatHandlers, b);
|
---|
282 |
|
---|
283 | RTHCPHYS HCPhys;
|
---|
284 | rc = PGMRamGCPhys2HCPhysWithFlags(&pVM->pgm.s, GCPhys, &HCPhys);
|
---|
285 | if (VBOX_SUCCESS(rc))
|
---|
286 | {
|
---|
287 | if (HCPhys & (MM_RAM_FLAGS_PHYSICAL_HANDLER | MM_RAM_FLAGS_VIRTUAL_HANDLER))
|
---|
288 | {
|
---|
289 | if (HCPhys & MM_RAM_FLAGS_PHYSICAL_HANDLER)
|
---|
290 | {
|
---|
291 | /*
|
---|
292 | * Physical page access handler.
|
---|
293 | */
|
---|
294 | const RTGCPHYS GCPhysFault = GCPhys | ((uintptr_t)pvFault & PAGE_OFFSET_MASK);
|
---|
295 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&CTXSUFF(pVM->pgm.s.pTrees)->PhysHandlers, GCPhysFault);
|
---|
296 | if (pCur)
|
---|
297 | {
|
---|
298 | # ifdef PGM_SYNC_N_PAGES
|
---|
299 | /*
|
---|
300 | * If the region is write protected and we got a page not present fault, then sync
|
---|
301 | * the pages. If the fault was caused by a read, then restart the instruction.
|
---|
302 | * In case of write access continue to the GC write handler.
|
---|
303 | *
|
---|
304 | * ASSUMES that there is only one handler per page or that they have similar write properties.
|
---|
305 | */
|
---|
306 | if ( pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_WRITE
|
---|
307 | && !(uErr & X86_TRAP_PF_P))
|
---|
308 | {
|
---|
309 | rc = PGM_BTH_NAME(SyncPage)(pVM, PdeSrc, (RTGCUINTPTR)pvFault, PGM_SYNC_NR_PAGES, uErr);
|
---|
310 | if ( VBOX_FAILURE(rc)
|
---|
311 | || !(uErr & X86_TRAP_PF_RW)
|
---|
312 | || rc == VINF_PGM_SYNCPAGE_MODIFIED_PDE)
|
---|
313 | {
|
---|
314 | AssertRC(rc);
|
---|
315 | STAM_COUNTER_INC(&pVM->pgm.s.StatHandlersOutOfSync);
|
---|
316 | STAM_PROFILE_STOP(&pVM->pgm.s.StatHandlers, b);
|
---|
317 | STAM_STATS({ pVM->pgm.s.CTXSUFF(pStatTrap0eAttribution) = &pVM->pgm.s.StatTrap0eOutOfSyncHndPhys; });
|
---|
318 | return rc;
|
---|
319 | }
|
---|
320 | }
|
---|
321 | # endif
|
---|
322 |
|
---|
323 | AssertMsg( pCur->enmType != PGMPHYSHANDLERTYPE_PHYSICAL_WRITE
|
---|
324 | || (pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_WRITE && (uErr & X86_TRAP_PF_RW)),
|
---|
325 | ("Unexpected trap for physical handler: %08X (phys=%08x) HCPhys=%X uErr=%X, enum=%d\n", pvFault, GCPhys, HCPhys, uErr, pCur->enmType));
|
---|
326 |
|
---|
327 | #ifdef IN_GC
|
---|
328 | Assert(CTXSUFF(pCur->pfnHandler));
|
---|
329 | STAM_PROFILE_START(&pCur->Stat, h);
|
---|
330 | rc = pCur->CTXSUFF(pfnHandler)(pVM, uErr, pRegFrame, pvFault, GCPhysFault, CTXSUFF(pCur->pvUser));
|
---|
331 | STAM_PROFILE_STOP(&pCur->Stat, h);
|
---|
332 | #elif IN_RING0
|
---|
333 | if (CTXALLSUFF(pCur->pfnHandler))
|
---|
334 | {
|
---|
335 | STAM_PROFILE_START(&pCur->Stat, h);
|
---|
336 | rc = pCur->CTXALLSUFF(pfnHandler)(pVM, uErr, pRegFrame, pvFault, GCPhysFault, CTXALLSUFF(pCur->pvUser));
|
---|
337 | STAM_PROFILE_STOP(&pCur->Stat, h);
|
---|
338 | }
|
---|
339 | else
|
---|
340 | rc = VINF_EM_RAW_EMULATE_INSTR;
|
---|
341 | #else
|
---|
342 | rc = VINF_EM_RAW_EMULATE_INSTR;
|
---|
343 | #endif
|
---|
344 | STAM_COUNTER_INC(&pVM->pgm.s.StatHandlersPhysical);
|
---|
345 | STAM_PROFILE_STOP(&pVM->pgm.s.StatHandlers, b);
|
---|
346 | STAM_STATS({ pVM->pgm.s.CTXSUFF(pStatTrap0eAttribution) = &pVM->pgm.s.StatTrap0eHndPhys; });
|
---|
347 | return rc;
|
---|
348 | }
|
---|
349 | }
|
---|
350 | else
|
---|
351 | {
|
---|
352 | # ifdef PGM_SYNC_N_PAGES
|
---|
353 | /*
|
---|
354 | * If the region is write protected and we got a page not present fault, then sync
|
---|
355 | * the pages. If the fault was caused by a read, then restart the instruction.
|
---|
356 | * In case of write access continue to the GC write handler.
|
---|
357 | */
|
---|
358 | if ( (HCPhys & (MM_RAM_FLAGS_VIRTUAL_WRITE | MM_RAM_FLAGS_VIRTUAL_ALL)) == MM_RAM_FLAGS_VIRTUAL_WRITE
|
---|
359 | && !(uErr & X86_TRAP_PF_P))
|
---|
360 | {
|
---|
361 | rc = PGM_BTH_NAME(SyncPage)(pVM, PdeSrc, (RTGCUINTPTR)pvFault, PGM_SYNC_NR_PAGES, uErr);
|
---|
362 | if ( VBOX_FAILURE(rc)
|
---|
363 | || rc == VINF_PGM_SYNCPAGE_MODIFIED_PDE
|
---|
364 | || !(uErr & X86_TRAP_PF_RW))
|
---|
365 | {
|
---|
366 | AssertRC(rc);
|
---|
367 | STAM_COUNTER_INC(&pVM->pgm.s.StatHandlersOutOfSync);
|
---|
368 | STAM_PROFILE_STOP(&pVM->pgm.s.StatHandlers, b);
|
---|
369 | STAM_STATS({ pVM->pgm.s.CTXSUFF(pStatTrap0eAttribution) = &pVM->pgm.s.StatTrap0eOutOfSyncHndVirt; });
|
---|
370 | return rc;
|
---|
371 | }
|
---|
372 | }
|
---|
373 | # endif
|
---|
374 | /*
|
---|
375 | * Ok, it's an virtual page access handler.
|
---|
376 | *
|
---|
377 | * Since it's faster to search by address, we'll do that first
|
---|
378 | * and then retry by GCPhys if that fails.
|
---|
379 | */
|
---|
380 | /** @todo r=bird: perhaps we should consider looking up by physical address directly now? */
|
---|
381 | /** @note r=svl: true, but lookup on virtual address should remain as a fallback as phys & virt trees might be out of sync, because the
|
---|
382 | * page was changed without us noticing it (not-present -> present without invlpg or mov cr3, xxx)
|
---|
383 | */
|
---|
384 | PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrRangeGet(&CTXSUFF(pVM->pgm.s.pTrees)->VirtHandlers, pvFault);
|
---|
385 | if (pCur)
|
---|
386 | {
|
---|
387 | AssertMsg(!((uintptr_t)pvFault - (uintptr_t)pCur->GCPtr < pCur->cb)
|
---|
388 | || ( pCur->enmType != PGMVIRTHANDLERTYPE_WRITE
|
---|
389 | || !(uErr & X86_TRAP_PF_P)
|
---|
390 | || (pCur->enmType == PGMVIRTHANDLERTYPE_WRITE && (uErr & X86_TRAP_PF_RW))),
|
---|
391 | ("Unexpected trap for virtual handler: %VGv (phys=%VGp) HCPhys=%HGp uErr=%X, enum=%d\n", pvFault, GCPhys, HCPhys, uErr, pCur->enmType));
|
---|
392 |
|
---|
393 | if ( pCur->enmType != PGMVIRTHANDLERTYPE_EIP
|
---|
394 | && (uintptr_t)pvFault - (uintptr_t)pCur->GCPtr < pCur->cb
|
---|
395 | && ( uErr & X86_TRAP_PF_RW
|
---|
396 | || ( pCur->enmType != PGMVIRTHANDLERTYPE_WRITE
|
---|
397 | && pCur->enmType != PGMVIRTHANDLERTYPE_HYPERVISOR) ) ) /** @todo r=bird: _HYPERVISOR is impossible here because of mapping check. */
|
---|
398 | {
|
---|
399 | #ifdef IN_GC
|
---|
400 | STAM_PROFILE_START(&pCur->Stat, h);
|
---|
401 | rc = CTXSUFF(pCur->pfnHandler)(pVM, uErr, pRegFrame, pvFault, pCur->GCPtr, (uintptr_t)pvFault - (uintptr_t)pCur->GCPtr);
|
---|
402 | STAM_PROFILE_STOP(&pCur->Stat, h);
|
---|
403 | #else
|
---|
404 | rc = VINF_EM_RAW_EMULATE_INSTR; /** @todo for VMX */
|
---|
405 | #endif
|
---|
406 | STAM_COUNTER_INC(&pVM->pgm.s.StatHandlersVirtual);
|
---|
407 | STAM_PROFILE_STOP(&pVM->pgm.s.StatHandlers, b);
|
---|
408 | STAM_STATS({ pVM->pgm.s.CTXSUFF(pStatTrap0eAttribution) = &pVM->pgm.s.StatTrap0eHndVirt; });
|
---|
409 | return rc;
|
---|
410 | }
|
---|
411 | /* Unhandled part of a monitored page */
|
---|
412 | }
|
---|
413 | else
|
---|
414 | {
|
---|
415 | /* Check by physical address. */
|
---|
416 | PPGMVIRTHANDLER pCur;
|
---|
417 | unsigned iPage;
|
---|
418 | rc = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys + ((uintptr_t)pvFault & PAGE_OFFSET_MASK),
|
---|
419 | &pCur, &iPage);
|
---|
420 | Assert(VBOX_SUCCESS(rc) || !pCur);
|
---|
421 | if ( pCur
|
---|
422 | && pCur->enmType != PGMVIRTHANDLERTYPE_EIP
|
---|
423 | && ( uErr & X86_TRAP_PF_RW
|
---|
424 | || ( pCur->enmType != PGMVIRTHANDLERTYPE_WRITE
|
---|
425 | && pCur->enmType != PGMVIRTHANDLERTYPE_HYPERVISOR) ) )
|
---|
426 | {
|
---|
427 | Assert((pCur->aPhysToVirt[iPage].Core.Key & X86_PTE_PAE_PG_MASK) == GCPhys);
|
---|
428 | #ifdef IN_GC
|
---|
429 | uintptr_t off = (iPage << PAGE_SHIFT) + ((uintptr_t)pvFault & PAGE_OFFSET_MASK) - ((uintptr_t)pCur->GCPtr & PAGE_OFFSET_MASK);
|
---|
430 | Assert(off < pCur->cb);
|
---|
431 | STAM_PROFILE_START(&pCur->Stat, h);
|
---|
432 | rc = CTXSUFF(pCur->pfnHandler)(pVM, uErr, pRegFrame, pvFault, pCur->GCPtr, off);
|
---|
433 | STAM_PROFILE_STOP(&pCur->Stat, h);
|
---|
434 | #else
|
---|
435 | rc = VINF_EM_RAW_EMULATE_INSTR; /** @todo for VMX */
|
---|
436 | #endif
|
---|
437 | STAM_COUNTER_INC(&pVM->pgm.s.StatHandlersVirtualByPhys);
|
---|
438 | STAM_PROFILE_STOP(&pVM->pgm.s.StatHandlers, b);
|
---|
439 | STAM_STATS({ pVM->pgm.s.CTXSUFF(pStatTrap0eAttribution) = &pVM->pgm.s.StatTrap0eHndVirt; });
|
---|
440 | return rc;
|
---|
441 | }
|
---|
442 | }
|
---|
443 | }
|
---|
444 |
|
---|
445 | /*
|
---|
446 | * There is a handled area of the page, but this fault doesn't belong to it.
|
---|
447 | * We must emulate the instruction.
|
---|
448 | *
|
---|
449 | * To avoid crashing (non-fatal) in the interpreter and go back to the recompiler
|
---|
450 | * we first check if this was a page-not-present fault for a page with only
|
---|
451 | * write access handlers. Restart the instruction if it wasn't a write access.
|
---|
452 | */
|
---|
453 | STAM_COUNTER_INC(&pVM->pgm.s.StatHandlersUnhandled);
|
---|
454 |
|
---|
455 | if ( !(HCPhys & (MM_RAM_FLAGS_PHYSICAL_ALL | MM_RAM_FLAGS_VIRTUAL_ALL))
|
---|
456 | && !(uErr & X86_TRAP_PF_P))
|
---|
457 | {
|
---|
458 | rc = PGM_BTH_NAME(SyncPage)(pVM, PdeSrc, (RTGCUINTPTR)pvFault, PGM_SYNC_NR_PAGES, uErr);
|
---|
459 | if ( VBOX_FAILURE(rc)
|
---|
460 | || rc == VINF_PGM_SYNCPAGE_MODIFIED_PDE
|
---|
461 | || !(uErr & X86_TRAP_PF_RW))
|
---|
462 | {
|
---|
463 | AssertRC(rc);
|
---|
464 | STAM_COUNTER_INC(&pVM->pgm.s.StatHandlersOutOfSync);
|
---|
465 | STAM_PROFILE_STOP(&pVM->pgm.s.StatHandlers, b);
|
---|
466 | STAM_STATS({ pVM->pgm.s.CTXSUFF(pStatTrap0eAttribution) = &pVM->pgm.s.StatTrap0eOutOfSyncHndPhys; });
|
---|
467 | return rc;
|
---|
468 | }
|
---|
469 | }
|
---|
470 |
|
---|
471 | /** @todo This particular case can cause quite a lot of overhead. E.g. early stage of kernel booting in Ubuntu 6.06
|
---|
472 | * It's writing to an unhandled part of the LDT page several million times.
|
---|
473 | */
|
---|
474 | rc = PGMInterpretInstruction(pVM, pRegFrame, pvFault);
|
---|
475 | LogFlow(("PGM: PGMInterpretInstruction -> rc=%d HCPhys=%VHp%s%s\n",
|
---|
476 | rc, HCPhys, HCPhys & MM_RAM_FLAGS_PHYSICAL_HANDLER ? " phys" : "",
|
---|
477 | HCPhys & MM_RAM_FLAGS_VIRTUAL_HANDLER ? " virt" : ""));
|
---|
478 | STAM_PROFILE_STOP(&pVM->pgm.s.StatHandlers, b);
|
---|
479 | STAM_STATS({ pVM->pgm.s.CTXSUFF(pStatTrap0eAttribution) = &pVM->pgm.s.StatTrap0eHndUnhandled; });
|
---|
480 | return rc;
|
---|
481 | } /* if any kind of handler */
|
---|
482 |
|
---|
483 | if (uErr & X86_TRAP_PF_P)
|
---|
484 | {
|
---|
485 | /*
|
---|
486 | * The page isn't marked, but it might still be monitored by a virtual page access handler.
|
---|
487 | * (ASSUMES no temporary disabling of virtual handlers.)
|
---|
488 | */
|
---|
489 | /** @todo r=bird: Since the purpose is to catch out of sync pages with virtual handler(s) here,
|
---|
490 | * we should correct both the shadow page table and physical memory flags, and not only check for
|
---|
491 | * accesses within the handler region but for access to pages with virtual handlers. */
|
---|
492 | PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrRangeGet(&CTXSUFF(pVM->pgm.s.pTrees)->VirtHandlers, pvFault);
|
---|
493 | if (pCur)
|
---|
494 | {
|
---|
495 | AssertMsg( !((uintptr_t)pvFault - (uintptr_t)pCur->GCPtr < pCur->cb)
|
---|
496 | || ( pCur->enmType != PGMVIRTHANDLERTYPE_WRITE
|
---|
497 | || !(uErr & X86_TRAP_PF_P)
|
---|
498 | || (pCur->enmType == PGMVIRTHANDLERTYPE_WRITE && (uErr & X86_TRAP_PF_RW))),
|
---|
499 | ("Unexpected trap for virtual handler: %08X (phys=%08x) HCPhys=%X uErr=%X, enum=%d\n", pvFault, GCPhys, HCPhys, uErr, pCur->enmType));
|
---|
500 |
|
---|
501 | if ( pCur->enmType != PGMVIRTHANDLERTYPE_EIP
|
---|
502 | && (uintptr_t)pvFault - (uintptr_t)pCur->GCPtr < pCur->cb
|
---|
503 | && ( uErr & X86_TRAP_PF_RW
|
---|
504 | || ( pCur->enmType != PGMVIRTHANDLERTYPE_WRITE
|
---|
505 | && pCur->enmType != PGMVIRTHANDLERTYPE_HYPERVISOR) ) ) /** @todo r=bird: _HYPERVISOR is impossible here because of mapping check. */
|
---|
506 | {
|
---|
507 | #ifdef IN_GC
|
---|
508 | STAM_PROFILE_START(&pCur->Stat, h);
|
---|
509 | rc = CTXSUFF(pCur->pfnHandler)(pVM, uErr, pRegFrame, pvFault, pCur->GCPtr, (uintptr_t)pvFault - (uintptr_t)pCur->GCPtr);
|
---|
510 | STAM_PROFILE_STOP(&pCur->Stat, h);
|
---|
511 | #else
|
---|
512 | rc = VINF_EM_RAW_EMULATE_INSTR; /** @todo for VMX */
|
---|
513 | #endif
|
---|
514 | STAM_COUNTER_INC(&pVM->pgm.s.StatHandlersVirtualUnmarked);
|
---|
515 | STAM_PROFILE_STOP(&pVM->pgm.s.StatHandlers, b);
|
---|
516 | STAM_STATS({ pVM->pgm.s.CTXSUFF(pStatTrap0eAttribution) = &pVM->pgm.s.StatTrap0eHndVirt; });
|
---|
517 | return rc;
|
---|
518 | }
|
---|
519 | }
|
---|
520 | }
|
---|
521 | }
|
---|
522 | STAM_PROFILE_STOP(&pVM->pgm.s.StatHandlers, b);
|
---|
523 |
|
---|
524 | # ifdef PGM_OUT_OF_SYNC_IN_GC
|
---|
525 | /*
|
---|
526 | * We are here only if page is present in Guest page tables and trap is not handled
|
---|
527 | * by our handlers.
|
---|
528 | * Check it for page out-of-sync situation.
|
---|
529 | */
|
---|
530 | STAM_PROFILE_START(&pVM->pgm.s.StatOutOfSync, c);
|
---|
531 |
|
---|
532 | if (!(uErr & X86_TRAP_PF_P))
|
---|
533 | {
|
---|
534 | /*
|
---|
535 | * Page is not present in our page tables.
|
---|
536 | * Try to sync it!
|
---|
537 | * BTW, fPageShw is invalid in this branch!
|
---|
538 | */
|
---|
539 | if (uErr & X86_TRAP_PF_US)
|
---|
540 | STAM_COUNTER_INC(&pVM->pgm.s.StatGCPageOutOfSyncUser);
|
---|
541 | else /* supervisor */
|
---|
542 | STAM_COUNTER_INC(&pVM->pgm.s.StatGCPageOutOfSyncSupervisor);
|
---|
543 |
|
---|
544 | # ifdef LOG_ENABLED
|
---|
545 | RTGCPHYS GCPhys;
|
---|
546 | uint64_t fPageGst;
|
---|
547 | PGMGstGetPage(pVM, pvFault, &fPageGst, &GCPhys);
|
---|
548 | LogFlow(("Page out of sync: %p eip=%08x PdeSrc.n.u1User=%d fPageGst=%08llx GCPhys=%VGp\n",
|
---|
549 | pvFault, pRegFrame->eip, PdeSrc.n.u1User, fPageGst, GCPhys));
|
---|
550 | # endif /* LOG_ENABLED */
|
---|
551 |
|
---|
552 | # ifdef IN_RING0
|
---|
553 | Assert((pRegFrame->ss & X86_SEL_RPL) == 0 || (pRegFrame->ss & X86_SEL_RPL) == 3);
|
---|
554 | # else
|
---|
555 | Assert((pRegFrame->ss & X86_SEL_RPL) == 1 || (pRegFrame->ss & X86_SEL_RPL) == 3);
|
---|
556 | if (CSAMIsEnabled(pVM) && (pRegFrame->ss & X86_SEL_RPL) == 1)
|
---|
557 | {
|
---|
558 | uint64_t fPageGst;
|
---|
559 | rc = PGMGstGetPage(pVM, pvFault, &fPageGst, NULL);
|
---|
560 | if ( VBOX_SUCCESS(rc)
|
---|
561 | && !(fPageGst & X86_PTE_US))
|
---|
562 | {
|
---|
563 | if (pvFault == (RTGCPTR)pRegFrame->eip)
|
---|
564 | {
|
---|
565 | LogFlow(("CSAMExecFault %VGv\n", pvFault));
|
---|
566 | rc = CSAMExecFault(pVM, pvFault);
|
---|
567 | if (rc != VINF_SUCCESS)
|
---|
568 | {
|
---|
569 | /*
|
---|
570 | * CSAM needs to perform a job in ring 3.
|
---|
571 | *
|
---|
572 | * Sync the page before going to the host context; otherwise we'll end up in a loop if
|
---|
573 | * CSAM fails (e.g. instruction crosses a page boundary and the next page is not present)
|
---|
574 | */
|
---|
575 | LogFlow(("CSAM ring 3 job\n"));
|
---|
576 | int rc2 = PGM_BTH_NAME(SyncPage)(pVM, PdeSrc, (RTGCUINTPTR)pvFault, 1, uErr);
|
---|
577 | AssertRC(rc2);
|
---|
578 |
|
---|
579 | STAM_PROFILE_STOP(&pVM->pgm.s.StatOutOfSync, c);
|
---|
580 | STAM_STATS({ pVM->pgm.s.CTXSUFF(pStatTrap0eAttribution) = &pVM->pgm.s.StatTrap0eCSAM; });
|
---|
581 | return rc;
|
---|
582 | }
|
---|
583 | }
|
---|
584 |
|
---|
585 | /*
|
---|
586 | * Mark this page as safe.
|
---|
587 | */
|
---|
588 | /** @todo not correct for pages that contain both code and data!! */
|
---|
589 | Log2(("CSAMMarkPage %p; scanned=%d\n", pvFault, true));
|
---|
590 | CSAMMarkPage(pVM, (RTGCPTR)pvFault, true);
|
---|
591 | }
|
---|
592 | }
|
---|
593 | # endif
|
---|
594 | rc = PGM_BTH_NAME(SyncPage)(pVM, PdeSrc, (RTGCUINTPTR)pvFault, PGM_SYNC_NR_PAGES, uErr);
|
---|
595 | if (VBOX_SUCCESS(rc))
|
---|
596 | {
|
---|
597 | /* The page was successfully synced, return to the guest. */
|
---|
598 | STAM_PROFILE_STOP(&pVM->pgm.s.StatOutOfSync, c);
|
---|
599 | STAM_STATS({ pVM->pgm.s.CTXSUFF(pStatTrap0eAttribution) = &pVM->pgm.s.StatTrap0eOutOfSync; });
|
---|
600 | return VINF_SUCCESS;
|
---|
601 | }
|
---|
602 | }
|
---|
603 | else
|
---|
604 | {
|
---|
605 | /*
|
---|
606 | * A side effect of not flushing global PDEs are out of sync pages due
|
---|
607 | * to physical monitored regions, that are no longer valid.
|
---|
608 | * Assume for now it only applies to the read/write flag
|
---|
609 | */
|
---|
610 | if (VBOX_SUCCESS(rc) && (uErr & X86_TRAP_PF_RW))
|
---|
611 | {
|
---|
612 | if (uErr & X86_TRAP_PF_US)
|
---|
613 | STAM_COUNTER_INC(&pVM->pgm.s.StatGCPageOutOfSyncUser);
|
---|
614 | else /* supervisor */
|
---|
615 | STAM_COUNTER_INC(&pVM->pgm.s.StatGCPageOutOfSyncSupervisor);
|
---|
616 |
|
---|
617 |
|
---|
618 | /*
|
---|
619 | * Note: Do NOT use PGM_SYNC_NR_PAGES here. That only works if the page is not present, which is not true in this case.
|
---|
620 | */
|
---|
621 | rc = PGM_BTH_NAME(SyncPage)(pVM, PdeSrc, (RTGCUINTPTR)pvFault, 1, uErr);
|
---|
622 | if (VBOX_SUCCESS(rc))
|
---|
623 | {
|
---|
624 | /*
|
---|
625 | * Page was successfully synced, return to guest.
|
---|
626 | */
|
---|
627 | # ifdef VBOX_STRICT
|
---|
628 | RTGCPHYS GCPhys;
|
---|
629 | uint64_t fPageGst;
|
---|
630 | rc = PGMGstGetPage(pVM, pvFault, &fPageGst, &GCPhys);
|
---|
631 | Assert(VBOX_SUCCESS(rc) && fPageGst & X86_PTE_RW);
|
---|
632 | LogFlow(("Obsolete physical monitor page out of sync %VGv - phys %VGp flags=%08llx\n", pvFault, GCPhys, (uint64_t)fPageGst));
|
---|
633 |
|
---|
634 | uint64_t fPageShw;
|
---|
635 | rc = PGMShwGetPage(pVM, pvFault, &fPageShw, NULL);
|
---|
636 | Assert(VBOX_SUCCESS(rc) && fPageShw & X86_PTE_RW);
|
---|
637 | # endif /* VBOX_STRICT */
|
---|
638 | STAM_PROFILE_STOP(&pVM->pgm.s.StatOutOfSync, c);
|
---|
639 | STAM_STATS({ pVM->pgm.s.CTXSUFF(pStatTrap0eAttribution) = &pVM->pgm.s.StatTrap0eOutOfSyncObsHnd; });
|
---|
640 | return VINF_SUCCESS;
|
---|
641 | }
|
---|
642 | }
|
---|
643 |
|
---|
644 | # ifdef VBOX_STRICT
|
---|
645 | /*
|
---|
646 | * Check for VMM page flags vs. Guest page flags consistency.
|
---|
647 | * Currently only for debug purposes.
|
---|
648 | */
|
---|
649 | if (VBOX_SUCCESS(rc))
|
---|
650 | {
|
---|
651 | /* Get guest page flags. */
|
---|
652 | uint64_t fPageGst;
|
---|
653 | rc = PGMGstGetPage(pVM, pvFault, &fPageGst, NULL);
|
---|
654 | if (VBOX_SUCCESS(rc))
|
---|
655 | {
|
---|
656 | uint64_t fPageShw;
|
---|
657 | rc = PGMShwGetPage(pVM, pvFault, &fPageShw, NULL);
|
---|
658 |
|
---|
659 | /*
|
---|
660 | * Compare page flags.
|
---|
661 | * Note: we have AVL, A, D bits desynched.
|
---|
662 | */
|
---|
663 | AssertMsg((fPageShw & ~(X86_PTE_A | X86_PTE_D | X86_PTE_AVL_MASK)) == (fPageGst & ~(X86_PTE_A | X86_PTE_D | X86_PTE_AVL_MASK)),
|
---|
664 | ("Page flags mismatch! pvFault=%p GCPhys=%VGp fPageShw=%08llx fPageGst=%08llx\n", pvFault, GCPhys, fPageShw, fPageGst));
|
---|
665 | }
|
---|
666 | else
|
---|
667 | AssertMsgFailed(("PGMGstGetPage rc=%Vrc\n", rc));
|
---|
668 | }
|
---|
669 | else
|
---|
670 | AssertMsgFailed(("PGMGCGetPage rc=%Vrc\n", rc));
|
---|
671 | # endif /* VBOX_STRICT */
|
---|
672 | }
|
---|
673 | STAM_PROFILE_STOP(&pVM->pgm.s.StatOutOfSync, c);
|
---|
674 | # endif /* PGM_OUT_OF_SYNC_IN_GC */
|
---|
675 | }
|
---|
676 | else
|
---|
677 | {
|
---|
678 | /*
|
---|
679 | * Page not present in Guest OS or invalid page table address.
|
---|
680 | * This is potential virtual page access handler food.
|
---|
681 | *
|
---|
682 | * For the present we'll say that our access handlers don't
|
---|
683 | * work for this case - we've already discarded the page table
|
---|
684 | * not present case which is identical to this.
|
---|
685 | *
|
---|
686 | * When we perchance find we need this, we will probably have AVL
|
---|
687 | * trees (offset based) to operate on and we can measure their speed
|
---|
688 | * agains mapping a page table and probably rearrange this handling
|
---|
689 | * a bit. (Like, searching virtual ranges before checking the
|
---|
690 | * physical address.)
|
---|
691 | */
|
---|
692 | }
|
---|
693 | }
|
---|
694 |
|
---|
695 |
|
---|
696 | /*
|
---|
697 | * Check if it's in a EIP based virtual page access handler range.
|
---|
698 | * This is only used for supervisor pages in flat mode.
|
---|
699 | */
|
---|
700 | /** @todo this stuff is completely broken by the out-of-sync stuff. since we don't use this stuff, that's not really a problem yet. */
|
---|
701 | STAM_PROFILE_START(&pVM->pgm.s.StatEIPHandlers, d);
|
---|
702 | if ((pRegFrame->ss & X86_SEL_RPL) == 1)
|
---|
703 | {
|
---|
704 | RTGCPTR pvEIP;
|
---|
705 | rc = SELMValidateAndConvertCSAddr(pVM, pRegFrame->ss, pRegFrame->cs, &pRegFrame->csHid, (RTGCPTR)pRegFrame->eip, &pvEIP);
|
---|
706 | if ( VBOX_SUCCESS(rc)
|
---|
707 | && pvEIP == (RTGCPTR)pRegFrame->eip)
|
---|
708 | {
|
---|
709 | PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrRangeGet(&CTXSUFF(pVM->pgm.s.pTrees)->VirtHandlers, pvEIP);
|
---|
710 | if ( pCur
|
---|
711 | && pCur->enmType == PGMVIRTHANDLERTYPE_EIP
|
---|
712 | && (uintptr_t)pvEIP - (uintptr_t)pCur->GCPtr < pCur->cb)
|
---|
713 | {
|
---|
714 | LogFlow(("EIP handler\n"));
|
---|
715 | #ifdef IN_GC
|
---|
716 | STAM_PROFILE_START(&pCur->Stat, h);
|
---|
717 | rc = CTXSUFF(pCur->pfnHandler)(pVM, uErr, pRegFrame, pvFault, pCur->GCPtr, (uintptr_t)pvEIP - (uintptr_t)pCur->GCPtr);
|
---|
718 | STAM_PROFILE_STOP(&pCur->Stat, h);
|
---|
719 | #else
|
---|
720 | rc = VINF_EM_RAW_EMULATE_INSTR; /** @todo for VMX */
|
---|
721 | #endif
|
---|
722 | STAM_PROFILE_STOP(&pVM->pgm.s.StatEIPHandlers, d);
|
---|
723 | return rc;
|
---|
724 | }
|
---|
725 | }
|
---|
726 | }
|
---|
727 | STAM_PROFILE_STOP(&pVM->pgm.s.StatEIPHandlers, d);
|
---|
728 |
|
---|
729 | /*
|
---|
730 | * Conclusion, this is a guest trap.
|
---|
731 | */
|
---|
732 | LogFlow(("PGM: Unhandled #PF -> route trap to recompiler!\n"));
|
---|
733 | STAM_COUNTER_INC(&pVM->pgm.s.StatGCTrap0eUnhandled);
|
---|
734 | return VINF_EM_RAW_GUEST_TRAP;
|
---|
735 |
|
---|
736 | #else /* PGM_GST_TYPE != PGM_TYPE_32BIT */
|
---|
737 |
|
---|
738 | AssertReleaseMsgFailed(("Shw=%d Gst=%d is not implemented!\n", PGM_GST_TYPE, PGM_SHW_TYPE));
|
---|
739 | return VERR_INTERNAL_ERROR;
|
---|
740 | #endif /* PGM_GST_TYPE != PGM_TYPE_32BIT */
|
---|
741 | }
|
---|
742 |
|
---|
743 |
|
---|
744 | /**
|
---|
745 | * Emulation of the invlpg instruction.
|
---|
746 | *
|
---|
747 | *
|
---|
748 | * @returns VBox status code.
|
---|
749 | *
|
---|
750 | * @param pVM VM handle.
|
---|
751 | * @param GCPtrPage Page to invalidate.
|
---|
752 | *
|
---|
753 | * @remark ASSUMES that the guest is updating before invalidating. This order
|
---|
754 | * isn't required by the CPU, so this is speculative and could cause
|
---|
755 | * trouble.
|
---|
756 | *
|
---|
757 | * @todo Flush page or page directory only if necessary!
|
---|
758 | * @todo Add a #define for simply invalidating the page.
|
---|
759 | */
|
---|
760 | PGM_BTH_DECL(int, InvalidatePage)(PVM pVM, RTGCUINTPTR GCPtrPage)
|
---|
761 | {
|
---|
762 | #if PGM_GST_TYPE == PGM_TYPE_32BIT
|
---|
763 |
|
---|
764 | LogFlow(("InvalidatePage %x\n", GCPtrPage));
|
---|
765 | # if PGM_SHW_TYPE == PGM_TYPE_32BIT || PGM_SHW_TYPE == PGM_TYPE_PAE
|
---|
766 | /*
|
---|
767 | * Get the shadow PD entry and skip out if this PD isn't present.
|
---|
768 | * (Guessing that it is frequent for a shadow PDE to not be present, do this first.)
|
---|
769 | */
|
---|
770 | const unsigned iPDDst = GCPtrPage >> SHW_PD_SHIFT;
|
---|
771 | # if PGM_SHW_TYPE == PGM_TYPE_32BIT
|
---|
772 | PX86PDE pPdeDst = &pVM->pgm.s.CTXMID(p,32BitPD)->a[iPDDst];
|
---|
773 | # else
|
---|
774 | PX86PDEPAE pPdeDst = &pVM->pgm.s.CTXMID(ap,PaePDs[0])->a[iPDDst];
|
---|
775 | # endif
|
---|
776 | const SHWPDE PdeDst = *pPdeDst;
|
---|
777 | if (!PdeDst.n.u1Present)
|
---|
778 | {
|
---|
779 | STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,InvalidatePageSkipped));
|
---|
780 | return VINF_SUCCESS;
|
---|
781 | }
|
---|
782 |
|
---|
783 | /*
|
---|
784 | * Get the guest PD entry and calc big page.
|
---|
785 | */
|
---|
786 | PVBOXPD pPDSrc = CTXSUFF(pVM->pgm.s.pGuestPD);
|
---|
787 | const unsigned iPDSrc = GCPtrPage >> GST_PD_SHIFT;
|
---|
788 | VBOXPDE PdeSrc = pPDSrc->a[iPDSrc];
|
---|
789 | const uint32_t cr4 = CPUMGetGuestCR4(pVM);
|
---|
790 | const bool fIsBigPage = PdeSrc.b.u1Size && (cr4 & X86_CR4_PSE);
|
---|
791 |
|
---|
792 | # ifdef IN_RING3
|
---|
793 | /*
|
---|
794 | * If a CR3 Sync is pending we may ignore the invalidate page operation
|
---|
795 | * depending on the kind of sync and if it's a global page or not.
|
---|
796 | * This doesn't make sense in GC/R0 so we'll skip it entirely there.
|
---|
797 | */
|
---|
798 | # ifdef PGM_SKIP_GLOBAL_PAGEDIRS_ON_NONGLOBAL_FLUSH
|
---|
799 | if ( VM_FF_ISSET(pVM, VM_FF_PGM_SYNC_CR3)
|
---|
800 | || ( VM_FF_ISSET(pVM, VM_FF_PGM_SYNC_CR3_NON_GLOBAL)
|
---|
801 | && fIsBigPage
|
---|
802 | && PdeSrc.b.u1Global
|
---|
803 | && (cr4 & X86_CR4_PGE)
|
---|
804 | )
|
---|
805 | )
|
---|
806 | # else
|
---|
807 | if (VM_FF_ISPENDING(pVM, VM_FF_PGM_SYNC_CR3 | VM_FF_PGM_SYNC_CR3_NON_GLOBAL) )
|
---|
808 | # endif
|
---|
809 | {
|
---|
810 | STAM_COUNTER_INC(&pVM->pgm.s.StatHCInvalidatePageSkipped);
|
---|
811 | return VINF_SUCCESS;
|
---|
812 | }
|
---|
813 | # endif /* IN_RING3 */
|
---|
814 |
|
---|
815 |
|
---|
816 | /*
|
---|
817 | * Deal with the Guest PDE.
|
---|
818 | */
|
---|
819 | int rc = VINF_SUCCESS;
|
---|
820 | if (PdeSrc.n.u1Present)
|
---|
821 | {
|
---|
822 | if (PdeDst.u & PGM_PDFLAGS_MAPPING)
|
---|
823 | {
|
---|
824 | /*
|
---|
825 | * Conflict - Let SyncPT deal with it to avoid duplicate code.
|
---|
826 | */
|
---|
827 | Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
|
---|
828 | rc = PGM_BTH_NAME(SyncPT)(pVM, iPDSrc, pPDSrc, GCPtrPage);
|
---|
829 | }
|
---|
830 | else if ( PdeSrc.n.u1User != PdeDst.n.u1User
|
---|
831 | || (!PdeSrc.n.u1Write && PdeDst.n.u1Write))
|
---|
832 | {
|
---|
833 | /*
|
---|
834 | * Mark not present so we can resync the PDE when it's used.
|
---|
835 | */
|
---|
836 | LogFlow(("InvalidatePage: Out-of-sync at %VGp PdeSrc=%RX64 PdeDst=%RX64\n",
|
---|
837 | GCPtrPage, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
|
---|
838 | pgmPoolFree(pVM, PdeDst.u & SHW_PDE_PG_MASK, SHW_POOL_ROOT_IDX, iPDDst);
|
---|
839 | pPdeDst->u = 0;
|
---|
840 | STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,InvalidatePagePDOutOfSync));
|
---|
841 | PGM_INVL_GUEST_TLBS();
|
---|
842 | }
|
---|
843 | # ifdef PGM_SYNC_ACCESSED_BIT
|
---|
844 | else if (!PdeSrc.n.u1Accessed)
|
---|
845 | {
|
---|
846 | /*
|
---|
847 | * Mark not present so we can set the accessed bit.
|
---|
848 | */
|
---|
849 | pgmPoolFree(pVM, PdeDst.u & SHW_PDE_PG_MASK, SHW_POOL_ROOT_IDX, iPDDst);
|
---|
850 | pPdeDst->u = 0;
|
---|
851 | STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,InvalidatePagePDNAs));
|
---|
852 | PGM_INVL_GUEST_TLBS();
|
---|
853 | }
|
---|
854 | # endif
|
---|
855 | else if (!fIsBigPage)
|
---|
856 | {
|
---|
857 | /*
|
---|
858 | * 4KB - page.
|
---|
859 | */
|
---|
860 | PPGMPOOLPAGE pShwPage = pgmPoolGetPageByHCPhys(pVM, PdeDst.u & SHW_PDE_PG_MASK);
|
---|
861 | RTGCPHYS GCPhys = PdeSrc.u & GST_PDE_PG_MASK;
|
---|
862 | # if PGM_SHW_TYPE != PGM_TYPE_32BIT
|
---|
863 | GCPhys |= (iPDDst & 1) * (PAGE_SIZE/2);
|
---|
864 | # endif
|
---|
865 | if (pShwPage->GCPhys == GCPhys)
|
---|
866 | {
|
---|
867 | #if 0 /* debug build + flash + xp (=1000Hz timer?) => bad invalidation + sync loops. */
|
---|
868 | const unsigned iPTEDst = (GCPtrPage >> SHW_PT_SHIFT) & SHW_PT_MASK;
|
---|
869 | PSHWPT pPT = (PSHWPT)PGMPOOL_PAGE_2_PTR(pVM, pShwPage);
|
---|
870 | if (pPT->a[iPTEDst].n.u1Present)
|
---|
871 | {
|
---|
872 | # ifdef PGMPOOL_WITH_USER_TRACKING
|
---|
873 | /* This is very unlikely with caching/monitoring enabled. */
|
---|
874 | PGM_BTH_NAME(SyncPageWorkerTrackDeref)(pVM, pShwPage, pPT->a[iPTEDst].u & SHW_PTE_PG_MASK);
|
---|
875 | # endif
|
---|
876 | pPT->a[iPTEDst].u = 0;
|
---|
877 | }
|
---|
878 | #else /* Syncing it here isn't 100% safe and it's probably not worth spending time syncing it. */
|
---|
879 | rc = PGM_BTH_NAME(SyncPage)(pVM, PdeSrc, GCPtrPage, 1, 0);
|
---|
880 | if (VBOX_SUCCESS(rc))
|
---|
881 | rc = VINF_SUCCESS;
|
---|
882 | #endif
|
---|
883 | STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,InvalidatePage4KBPages));
|
---|
884 | PGM_INVL_PG(GCPtrPage);
|
---|
885 | }
|
---|
886 | else
|
---|
887 | {
|
---|
888 | /*
|
---|
889 | * The page table address changed.
|
---|
890 | */
|
---|
891 | LogFlow(("InvalidatePage: Out-of-sync at %VGp PdeSrc=%RX64 PdeDst=%RX64 ShwGCPhys=%VGp iPDDst=%#x\n",
|
---|
892 | GCPtrPage, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u, pShwPage->GCPhys, iPDDst));
|
---|
893 | pgmPoolFree(pVM, PdeDst.u & SHW_PDE_PG_MASK, SHW_POOL_ROOT_IDX, iPDDst);
|
---|
894 | pPdeDst->u = 0;
|
---|
895 | STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,InvalidatePagePDOutOfSync));
|
---|
896 | PGM_INVL_GUEST_TLBS();
|
---|
897 | }
|
---|
898 | }
|
---|
899 | else
|
---|
900 | {
|
---|
901 | /*
|
---|
902 | * 4MB - page.
|
---|
903 | */
|
---|
904 | /* Before freeing the page, check if anything really changed. */
|
---|
905 | PPGMPOOLPAGE pShwPage = pgmPoolGetPageByHCPhys(pVM, PdeDst.u & SHW_PDE_PG_MASK);
|
---|
906 | RTGCPHYS GCPhys = PdeSrc.u & X86_PDE4M_PG_MASK;
|
---|
907 | # if PGM_SHW_TYPE != PGM_TYPE_32BIT
|
---|
908 | GCPhys |= GCPtrPage & (1 << X86_PD_PAE_SHIFT);
|
---|
909 | # endif
|
---|
910 | if ( pShwPage->GCPhys == GCPhys
|
---|
911 | && pShwPage->enmKind == BTH_PGMPOOLKIND_PT_FOR_BIG)
|
---|
912 | {
|
---|
913 | /* ASSUMES a the given bits are identical for 4M and normal PDEs */
|
---|
914 | /** @todo PAT */
|
---|
915 | # ifdef PGM_SYNC_DIRTY_BIT
|
---|
916 | if ( (PdeSrc.u & (X86_PDE_P | X86_PDE_RW | X86_PDE_US | X86_PDE_PWT | X86_PDE_PCD))
|
---|
917 | == (PdeDst.u & (X86_PDE_P | X86_PDE_RW | X86_PDE_US | X86_PDE_PWT | X86_PDE_PCD))
|
---|
918 | && ( PdeSrc.b.u1Dirty /** @todo rainy day: What about read-only 4M pages? not very common, but still... */
|
---|
919 | || (PdeDst.u & PGM_PDFLAGS_TRACK_DIRTY)))
|
---|
920 | # else
|
---|
921 | if ( (PdeSrc.u & (X86_PDE_P | X86_PDE_RW | X86_PDE_US | X86_PDE_PWT | X86_PDE_PCD))
|
---|
922 | == (PdeDst.u & (X86_PDE_P | X86_PDE_RW | X86_PDE_US | X86_PDE_PWT | X86_PDE_PCD)))
|
---|
923 | # endif
|
---|
924 | {
|
---|
925 | LogFlow(("Skipping flush for big page containing %VGv (PD=%X)-> nothing has changed!\n", GCPtrPage, iPDSrc));
|
---|
926 | STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,InvalidatePage4MBPagesSkip));
|
---|
927 | return VINF_SUCCESS;
|
---|
928 | }
|
---|
929 | }
|
---|
930 |
|
---|
931 | /*
|
---|
932 | * Ok, the page table is present and it's been changed in the guest.
|
---|
933 | * If we're in host context, we'll just mark it as not present taking the lazy approach.
|
---|
934 | * We could do this for some flushes in GC too, but we need an algorithm for
|
---|
935 | * deciding which 4MB pages containing code likely to be executed very soon.
|
---|
936 | */
|
---|
937 | pgmPoolFree(pVM, PdeDst.u & SHW_PDE_PG_MASK, SHW_POOL_ROOT_IDX, iPDDst);
|
---|
938 | pPdeDst->u = 0;
|
---|
939 | STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,InvalidatePage4MBPages));
|
---|
940 | DUMP_PDE_BIG("PGMInvalidatePage", iPDSrc, PdeSrc);
|
---|
941 | PGM_INVL_BIG_PG(GCPtrPage);
|
---|
942 | }
|
---|
943 | }
|
---|
944 | else
|
---|
945 | {
|
---|
946 | /*
|
---|
947 | * Page directory is not present, mark shadow PDE not present.
|
---|
948 | */
|
---|
949 | if (!(PdeDst.u & PGM_PDFLAGS_MAPPING))
|
---|
950 | {
|
---|
951 | pgmPoolFree(pVM, PdeDst.u & SHW_PDE_PG_MASK, SHW_POOL_ROOT_IDX, iPDDst);
|
---|
952 | pPdeDst->u = 0;
|
---|
953 | STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,InvalidatePagePDNPs));
|
---|
954 | PGM_INVL_PG(GCPtrPage);
|
---|
955 | }
|
---|
956 | else
|
---|
957 | {
|
---|
958 | Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
|
---|
959 | STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,InvalidatePagePDMappings));
|
---|
960 | }
|
---|
961 | }
|
---|
962 |
|
---|
963 | return rc;
|
---|
964 |
|
---|
965 | # elif PGM_SHW_TYPE == PGM_TYPE_AMD64
|
---|
966 | # error "Guest 32-bit mode and shadow AMD64 mode doesn't add up!"
|
---|
967 | # endif
|
---|
968 | return VINF_SUCCESS;
|
---|
969 |
|
---|
970 | #elif PGM_GST_TYPE == PGM_TYPE_PAE
|
---|
971 | # if PGM_SHW_TYPE == PGM_TYPE_PAE
|
---|
972 | //# error not implemented
|
---|
973 | return VERR_INTERNAL_ERROR;
|
---|
974 |
|
---|
975 | # else /* PGM_SHW_TYPE != PGM_TYPE_AMD64 */
|
---|
976 | # error "Guest PAE mode, but not the shadow mode ; 32bit - maybe, but amd64 no."
|
---|
977 | # endif /* PGM_SHW_TYPE != PGM_TYPE_AMD64 */
|
---|
978 |
|
---|
979 | #elif PGM_GST_TYPE == PGM_TYPE_AMD64
|
---|
980 | # if PGM_SHW_TYPE == PGM_TYPE_AMD64
|
---|
981 | //# error not implemented
|
---|
982 | return VERR_INTERNAL_ERROR;
|
---|
983 |
|
---|
984 | # else /* PGM_SHW_TYPE != PGM_TYPE_AMD64 */
|
---|
985 | # error "Guest AMD64 mode, but not the shadow mode - that can't be right!"
|
---|
986 | # endif /* PGM_SHW_TYPE != PGM_TYPE_AMD64 */
|
---|
987 |
|
---|
988 | #else /* guest real and protected mode */
|
---|
989 |
|
---|
990 | return VINF_SUCCESS;
|
---|
991 | #endif
|
---|
992 | }
|
---|
993 |
|
---|
994 |
|
---|
995 | #ifdef PGMPOOL_WITH_USER_TRACKING
|
---|
996 | /**
|
---|
997 | * Update the tracking of shadowed pages.
|
---|
998 | *
|
---|
999 | * @param pVM The VM handle.
|
---|
1000 | * @param pShwPage The shadow page.
|
---|
1001 | * @param HCPhys The physical page we is being dereferenced.
|
---|
1002 | */
|
---|
1003 | DECLINLINE(void) PGM_BTH_NAME(SyncPageWorkerTrackDeref)(PVM pVM, PPGMPOOLPAGE pShwPage, RTHCPHYS HCPhys)
|
---|
1004 | {
|
---|
1005 | # ifdef PGMPOOL_WITH_GCPHYS_TRACKING
|
---|
1006 | STAM_PROFILE_START(&pVM->pgm.s.StatTrackDeref, a);
|
---|
1007 | LogFlow(("SyncPageWorkerTrackDeref: Damn HCPhys=%VHp pShwPage->idx=%#x!!!\n", HCPhys, pShwPage->idx));
|
---|
1008 |
|
---|
1009 | /** @todo If this turns out to be a bottle neck (*very* likely) two things can be done:
|
---|
1010 | * 1. have a medium sized HCPhys -> GCPhys cache (hash?)
|
---|
1011 | * 2. write protect all shadowed pages. I.e. implement caching.
|
---|
1012 | */
|
---|
1013 | /*
|
---|
1014 | * Find the guest address.
|
---|
1015 | */
|
---|
1016 | for (PPGMRAMRANGE pRam = pVM->pgm.s.CTXSUFF(pRamRanges);
|
---|
1017 | pRam;
|
---|
1018 | pRam = pRam->CTXSUFF(pNext))
|
---|
1019 | {
|
---|
1020 | unsigned iPage = pRam->cb >> PAGE_SHIFT;
|
---|
1021 | while (iPage-- > 0)
|
---|
1022 | {
|
---|
1023 | if ((pRam->aHCPhys[iPage] & X86_PTE_PAE_PG_MASK) == HCPhys)
|
---|
1024 | {
|
---|
1025 | PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
|
---|
1026 | pgmTrackDerefGCPhys(pPool, pShwPage, &pRam->aHCPhys[iPage]);
|
---|
1027 | pShwPage->cPresent--;
|
---|
1028 | pPool->cPresent--;
|
---|
1029 | STAM_PROFILE_STOP(&pVM->pgm.s.StatTrackDeref, a);
|
---|
1030 | return;
|
---|
1031 | }
|
---|
1032 | }
|
---|
1033 | }
|
---|
1034 |
|
---|
1035 | for (;;)
|
---|
1036 | AssertReleaseMsgFailed(("HCPhys=%VHp wasn't found!\n", HCPhys));
|
---|
1037 | # else /* !PGMPOOL_WITH_GCPHYS_TRACKING */
|
---|
1038 | pShwPage->cPresent--;
|
---|
1039 | pVM->pgm.s.CTXSUFF(pPool)->cPresent--;
|
---|
1040 | # endif /* !PGMPOOL_WITH_GCPHYS_TRACKING */
|
---|
1041 | }
|
---|
1042 |
|
---|
1043 |
|
---|
1044 | /**
|
---|
1045 | * Update the tracking of shadowed pages.
|
---|
1046 | *
|
---|
1047 | * @param pVM The VM handle.
|
---|
1048 | * @param pShwPage The shadow page.
|
---|
1049 | * @param u16 The top 16-bit of the *pHCPhys.
|
---|
1050 | * @param pHCPhys Pointer to the ram range physical page entry.
|
---|
1051 | * @param iPTDst The index into the shadow table.
|
---|
1052 | */
|
---|
1053 | DECLINLINE(void) PGM_BTH_NAME(SyncPageWorkerTrackAddref)(PVM pVM, PPGMPOOLPAGE pShwPage, uint16_t u16, PRTHCPHYS pHCPhys, const unsigned iPTDst)
|
---|
1054 | {
|
---|
1055 | # ifdef PGMPOOL_WITH_GCPHYS_TRACKING
|
---|
1056 | /*
|
---|
1057 | * We're making certain assumptions about the placement of cRef and idx.
|
---|
1058 | */
|
---|
1059 | Assert(MM_RAM_FLAGS_IDX_SHIFT == 48);
|
---|
1060 | Assert(MM_RAM_FLAGS_CREFS_SHIFT > MM_RAM_FLAGS_IDX_SHIFT);
|
---|
1061 |
|
---|
1062 | /*
|
---|
1063 | * Just deal with the simple first time here.
|
---|
1064 | */
|
---|
1065 | if (!u16)
|
---|
1066 | {
|
---|
1067 | STAM_COUNTER_INC(&pVM->pgm.s.StatTrackVirgin);
|
---|
1068 | u16 = (1 << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT)) | pShwPage->idx;
|
---|
1069 | }
|
---|
1070 | else
|
---|
1071 | u16 = pgmPoolTrackPhysExtAddref(pVM, u16, pShwPage->idx);
|
---|
1072 |
|
---|
1073 | /* write back, trying to be clever... */
|
---|
1074 | Log2(("SyncPageWorkerTrackAddRef: u16=%#x *pHCPhys=%VHp->%VHp iPTDst=%#x\n",
|
---|
1075 | u16, *pHCPhys, (*pHCPhys & MM_RAM_FLAGS_NO_REFS_MASK) | ((uint64_t)u16 << MM_RAM_FLAGS_CREFS_SHIFT), iPTDst));
|
---|
1076 | *((uint16_t *)pHCPhys + 3) = u16;
|
---|
1077 | # endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
|
---|
1078 |
|
---|
1079 | /* update statistics. */
|
---|
1080 | pVM->pgm.s.CTXSUFF(pPool)->cPresent++;
|
---|
1081 | pShwPage->cPresent++;
|
---|
1082 | if (pShwPage->iFirstPresent > iPTDst)
|
---|
1083 | pShwPage->iFirstPresent = iPTDst;
|
---|
1084 | }
|
---|
1085 | #endif /* PGMPOOL_WITH_USER_TRACKING */
|
---|
1086 |
|
---|
1087 |
|
---|
1088 | /**
|
---|
1089 | * Creates a 4K shadow page for a guest page.
|
---|
1090 | *
|
---|
1091 | * For 4M pages the caller must convert the PDE4M to a PTE, this includes adjusting the
|
---|
1092 | * physical address. The PdeSrc argument only the flags are used. No page structured
|
---|
1093 | * will be mapped in this function.
|
---|
1094 | *
|
---|
1095 | * @param pVM VM handle.
|
---|
1096 | * @param pPteDst Destination page table entry.
|
---|
1097 | * @param PdeSrc Source page directory entry (i.e. Guest OS page directory entry).
|
---|
1098 | * Can safely assume that only the flags are being used.
|
---|
1099 | * @param PteSrc Source page table entry (i.e. Guest OS page table entry).
|
---|
1100 | * @param pShwPage Pointer to the shadow page.
|
---|
1101 | * @param iPTDst The index into the shadow table.
|
---|
1102 | *
|
---|
1103 | * @remark Not used for 2/4MB pages!
|
---|
1104 | */
|
---|
1105 | DECLINLINE(void) PGM_BTH_NAME(SyncPageWorker)(PVM pVM, PSHWPTE pPteDst, VBOXPDE PdeSrc, VBOXPTE PteSrc, PPGMPOOLPAGE pShwPage, unsigned iPTDst)
|
---|
1106 | {
|
---|
1107 | if (PteSrc.n.u1Present)
|
---|
1108 | {
|
---|
1109 | /*
|
---|
1110 | * Find the ram range.
|
---|
1111 | */
|
---|
1112 | PRTHCPHYS pHCPhys;
|
---|
1113 | int rc = PGMRamGCPhys2PagePtr(&pVM->pgm.s, PteSrc.u & X86_PTE_PG_MASK, &pHCPhys);
|
---|
1114 | if (VBOX_SUCCESS(rc))
|
---|
1115 | {
|
---|
1116 | /** @todo investiage PWT, PCD and PAT. */
|
---|
1117 | /*
|
---|
1118 | * Make page table entry.
|
---|
1119 | */
|
---|
1120 | const RTHCPHYS HCPhys = *pHCPhys;
|
---|
1121 | SHWPTE PteDst;
|
---|
1122 | if (HCPhys & (MM_RAM_FLAGS_PHYSICAL_ALL | MM_RAM_FLAGS_VIRTUAL_ALL | MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_VIRTUAL_WRITE))
|
---|
1123 | {
|
---|
1124 | /** @todo r=bird: Are we actually handling dirty and access bits for pages with access handlers correctly? No. */
|
---|
1125 | if (!(HCPhys & (MM_RAM_FLAGS_PHYSICAL_ALL | MM_RAM_FLAGS_VIRTUAL_ALL)))
|
---|
1126 | PteDst.u = (PteSrc.u & ~(X86_PTE_PAE_PG_MASK | X86_PTE_AVL_MASK | X86_PTE_PAT | X86_PTE_PCD | X86_PTE_PWT | X86_PTE_RW))
|
---|
1127 | | (HCPhys & X86_PTE_PAE_PG_MASK);
|
---|
1128 | else
|
---|
1129 | PteDst.u = 0;
|
---|
1130 | /** @todo count these two kinds. */
|
---|
1131 | }
|
---|
1132 | else
|
---|
1133 | {
|
---|
1134 | #ifdef PGM_SYNC_DIRTY_BIT
|
---|
1135 | # ifdef PGM_SYNC_ACCESSED_BIT
|
---|
1136 | /*
|
---|
1137 | * If the page or page directory entry is not marked accessed,
|
---|
1138 | * we mark the page not present.
|
---|
1139 | */
|
---|
1140 | if (!PteSrc.n.u1Accessed || !PdeSrc.n.u1Accessed)
|
---|
1141 | {
|
---|
1142 | STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,AccessedPage));
|
---|
1143 | PteDst.u = 0;
|
---|
1144 | }
|
---|
1145 | else
|
---|
1146 | # endif
|
---|
1147 | /*
|
---|
1148 | * If the page is not flagged as dirty and is writable, then make it read-only, so we can set the dirty bit
|
---|
1149 | * when the page is modified.
|
---|
1150 | */
|
---|
1151 | if (!PteSrc.n.u1Dirty && (PdeSrc.n.u1Write & PteSrc.n.u1Write))
|
---|
1152 | {
|
---|
1153 | STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,DirtyPage));
|
---|
1154 | PteDst.u = (PteSrc.u & ~(X86_PTE_PAE_PG_MASK | X86_PTE_AVL_MASK | X86_PTE_PAT | X86_PTE_PCD | X86_PTE_PWT | X86_PTE_RW))
|
---|
1155 | | (HCPhys & X86_PTE_PAE_PG_MASK)
|
---|
1156 | | PGM_PTFLAGS_TRACK_DIRTY;
|
---|
1157 | }
|
---|
1158 | else
|
---|
1159 | {
|
---|
1160 | STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,DirtyPageSkipped));
|
---|
1161 | PteDst.u = (PteSrc.u & ~(X86_PTE_PAE_PG_MASK | X86_PTE_AVL_MASK | X86_PTE_PAT | X86_PTE_PCD | X86_PTE_PWT))
|
---|
1162 | | (HCPhys & X86_PTE_PAE_PG_MASK);
|
---|
1163 | }
|
---|
1164 | #endif
|
---|
1165 | }
|
---|
1166 |
|
---|
1167 | #ifdef PGMPOOL_WITH_USER_TRACKING
|
---|
1168 | /*
|
---|
1169 | * Keep user track up to date.
|
---|
1170 | */
|
---|
1171 | if (PteDst.n.u1Present)
|
---|
1172 | {
|
---|
1173 | if (!pPteDst->n.u1Present)
|
---|
1174 | PGM_BTH_NAME(SyncPageWorkerTrackAddref)(pVM, pShwPage, HCPhys >> MM_RAM_FLAGS_IDX_SHIFT, pHCPhys, iPTDst);
|
---|
1175 | else if ((pPteDst->u & SHW_PTE_PG_MASK) != (PteDst.u & SHW_PTE_PG_MASK))
|
---|
1176 | {
|
---|
1177 | Log2(("SyncPageWorker: deref! *pPteDst=%RX64 PteDst=%RX64\n", (uint64_t)pPteDst->u, (uint64_t)PteDst.u));
|
---|
1178 | PGM_BTH_NAME(SyncPageWorkerTrackDeref)(pVM, pShwPage, pPteDst->u & SHW_PTE_PG_MASK);
|
---|
1179 | PGM_BTH_NAME(SyncPageWorkerTrackAddref)(pVM, pShwPage, HCPhys >> MM_RAM_FLAGS_IDX_SHIFT, pHCPhys, iPTDst);
|
---|
1180 | }
|
---|
1181 | }
|
---|
1182 | else if (pPteDst->n.u1Present)
|
---|
1183 | {
|
---|
1184 | Log2(("SyncPageWorker: deref! *pPteDst=%RX64\n", (uint64_t)pPteDst->u));
|
---|
1185 | PGM_BTH_NAME(SyncPageWorkerTrackDeref)(pVM, pShwPage, pPteDst->u & SHW_PTE_PG_MASK);
|
---|
1186 | }
|
---|
1187 | #endif /* PGMPOOL_WITH_USER_TRACKING */
|
---|
1188 |
|
---|
1189 | /*
|
---|
1190 | * Update statistics and commit the entry.
|
---|
1191 | */
|
---|
1192 | if (!PteSrc.n.u1Global)
|
---|
1193 | pShwPage->fSeenNonGlobal = true;
|
---|
1194 | *pPteDst = PteDst;
|
---|
1195 | }
|
---|
1196 | /* else MMIO or invalid page, we must handle them manually in the #PF handler. */
|
---|
1197 | /** @todo count these. */
|
---|
1198 | }
|
---|
1199 | else
|
---|
1200 | {
|
---|
1201 | /*
|
---|
1202 | * Page not-present.
|
---|
1203 | */
|
---|
1204 | #ifdef PGMPOOL_WITH_USER_TRACKING
|
---|
1205 | /* Keep user track up to date. */
|
---|
1206 | if (pPteDst->n.u1Present)
|
---|
1207 | {
|
---|
1208 | Log2(("SyncPageWorker: deref! *pPteDst=%RX64\n", (uint64_t)pPteDst->u));
|
---|
1209 | PGM_BTH_NAME(SyncPageWorkerTrackDeref)(pVM, pShwPage, pPteDst->u & SHW_PTE_PG_MASK);
|
---|
1210 | }
|
---|
1211 | #endif /* PGMPOOL_WITH_USER_TRACKING */
|
---|
1212 | pPteDst->u = 0;
|
---|
1213 | /** @todo count these. */
|
---|
1214 | }
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 |
|
---|
1218 | /**
|
---|
1219 | * Syncs a guest OS page.
|
---|
1220 | *
|
---|
1221 | * There are no conflicts at this point, neither is there any need for
|
---|
1222 | * page table allocations.
|
---|
1223 | *
|
---|
1224 | * @returns VBox status code.
|
---|
1225 | * @returns VINF_PGM_SYNCPAGE_MODIFIED_PDE if it modifies the PDE in any way.
|
---|
1226 | * @param pVM VM handle.
|
---|
1227 | * @param PdeSrc Page directory entry of the guest.
|
---|
1228 | * @param GCPtrPage Guest context page address.
|
---|
1229 | * @param cPages Number of pages to sync (PGM_SYNC_N_PAGES) (default=1).
|
---|
1230 | * @param uErr Fault error (X86_TRAP_PF_*).
|
---|
1231 | */
|
---|
1232 | PGM_BTH_DECL(int, SyncPage)(PVM pVM, VBOXPDE PdeSrc, RTGCUINTPTR GCPtrPage, unsigned cPages, unsigned uErr)
|
---|
1233 | {
|
---|
1234 | LogFlow(("SyncPage: GCPtrPage=%VGv cPages=%d uErr=%#x\n", GCPtrPage, cPages, uErr));
|
---|
1235 |
|
---|
1236 | #if PGM_GST_TYPE == PGM_TYPE_32BIT
|
---|
1237 |
|
---|
1238 | # if PGM_SHW_TYPE != PGM_TYPE_32BIT && PGM_SHW_TYPE != PGM_TYPE_PAE
|
---|
1239 | # error "Invalid shadow mode for 32-bit guest mode!"
|
---|
1240 | # endif
|
---|
1241 |
|
---|
1242 | /*
|
---|
1243 | * Assert preconditions.
|
---|
1244 | */
|
---|
1245 | # if GC_ARCH_BITS != 32
|
---|
1246 | Assert(GCPtrPage < _4G); //???
|
---|
1247 | # endif
|
---|
1248 | STAM_COUNTER_INC(&pVM->pgm.s.StatGCSyncPagePD[(GCPtrPage >> X86_PD_SHIFT) & X86_PD_MASK]);
|
---|
1249 | Assert(PdeSrc.n.u1Present);
|
---|
1250 | Assert(cPages);
|
---|
1251 |
|
---|
1252 | /*
|
---|
1253 | * Get the shadow PDE, find the shadow page table in the pool.
|
---|
1254 | */
|
---|
1255 | const unsigned iPDDst = GCPtrPage >> SHW_PD_SHIFT;
|
---|
1256 | # if PGM_SHW_TYPE == PGM_TYPE_32BIT
|
---|
1257 | X86PDE PdeDst = pVM->pgm.s.CTXMID(p,32BitPD)->a[iPDDst];
|
---|
1258 | # else /* PAE */
|
---|
1259 | X86PDEPAE PdeDst = pVM->pgm.s.CTXMID(ap,PaePDs)[0]->a[iPDDst];
|
---|
1260 | # endif
|
---|
1261 | Assert(PdeDst.n.u1Present);
|
---|
1262 | PPGMPOOLPAGE pShwPage = pgmPoolGetPageByHCPhys(pVM, PdeDst.u & SHW_PDE_PG_MASK);
|
---|
1263 |
|
---|
1264 | /*
|
---|
1265 | * Check that the page is present and that the shadow PDE isn't out of sync.
|
---|
1266 | */
|
---|
1267 | const bool fBigPage = PdeSrc.b.u1Size && (CPUMGetGuestCR4(pVM) & X86_CR4_PSE);
|
---|
1268 | RTGCPHYS GCPhys;
|
---|
1269 | if (!fBigPage)
|
---|
1270 | {
|
---|
1271 | GCPhys = PdeSrc.u & GST_PDE_PG_MASK;
|
---|
1272 | # if PGM_SHW_TYPE != PGM_TYPE_32BIT
|
---|
1273 | GCPhys |= (iPDDst & 1) * (PAGE_SIZE/2);
|
---|
1274 | # endif
|
---|
1275 | }
|
---|
1276 | else
|
---|
1277 | {
|
---|
1278 | GCPhys = PdeSrc.u & GST_PDE4M_PG_MASK;
|
---|
1279 | # if PGM_SHW_TYPE != PGM_TYPE_32BIT
|
---|
1280 | GCPhys |= GCPtrPage & X86_PAGE_2M_SIZE;
|
---|
1281 | # endif
|
---|
1282 | }
|
---|
1283 | if ( pShwPage->GCPhys == GCPhys
|
---|
1284 | && PdeSrc.n.u1Present
|
---|
1285 | && (PdeSrc.n.u1User == PdeDst.n.u1User)
|
---|
1286 | && (PdeSrc.n.u1Write == PdeDst.n.u1Write || !PdeDst.n.u1Write)
|
---|
1287 | )
|
---|
1288 | {
|
---|
1289 | # ifdef PGM_SYNC_ACCESSED_BIT
|
---|
1290 | /*
|
---|
1291 | * Check that the PDE is marked accessed already.
|
---|
1292 | * Since we set the accessed bit *before* getting here on a #PF, this
|
---|
1293 | * check is only meant for dealing with non-#PF'ing paths.
|
---|
1294 | */
|
---|
1295 | if (PdeSrc.n.u1Accessed)
|
---|
1296 | # endif
|
---|
1297 | {
|
---|
1298 | PSHWPT pPTDst = (PSHWPT)PGMPOOL_PAGE_2_PTR(pVM, pShwPage);
|
---|
1299 | if (!fBigPage)
|
---|
1300 | {
|
---|
1301 | /*
|
---|
1302 | * 4KB Page - Map the guest page table.
|
---|
1303 | */
|
---|
1304 | PVBOXPT pPTSrc;
|
---|
1305 | int rc = PGM_GCPHYS_2_PTR(pVM, PdeSrc.u & X86_PDE_PG_MASK, &pPTSrc);
|
---|
1306 | if (VBOX_SUCCESS(rc))
|
---|
1307 | {
|
---|
1308 | # ifdef PGM_SYNC_N_PAGES
|
---|
1309 | Assert(cPages == 1 || !(uErr & X86_TRAP_PF_P));
|
---|
1310 | if (cPages > 1 && !(uErr & X86_TRAP_PF_P))
|
---|
1311 | {
|
---|
1312 | /*
|
---|
1313 | * This code path is currently only taken when the caller is PGMTrap0eHandler
|
---|
1314 | * for non-present pages!
|
---|
1315 | *
|
---|
1316 | * We're setting PGM_SYNC_NR_PAGES pages around the faulting page to sync it and
|
---|
1317 | * deal with locality.
|
---|
1318 | */
|
---|
1319 | unsigned iPTDst = (GCPtrPage >> SHW_PT_SHIFT) & SHW_PT_MASK;
|
---|
1320 | # if PGM_SHW_TYPE == PGM_TYPE_32BIT
|
---|
1321 | const unsigned offPTSrc = 0;
|
---|
1322 | # else
|
---|
1323 | const unsigned offPTSrc = ((GCPtrPage >> SHW_PD_SHIFT) & 1) * 512;
|
---|
1324 | # endif
|
---|
1325 | const unsigned iPTDstEnd = RT_MIN(iPTDst + PGM_SYNC_NR_PAGES / 2, ELEMENTS(pPTDst->a));
|
---|
1326 | if (iPTDst < PGM_SYNC_NR_PAGES / 2)
|
---|
1327 | iPTDst = 0;
|
---|
1328 | else
|
---|
1329 | iPTDst -= PGM_SYNC_NR_PAGES / 2;
|
---|
1330 | for (; iPTDst < iPTDstEnd; iPTDst++)
|
---|
1331 | {
|
---|
1332 | if (!pPTDst->a[iPTDst].n.u1Present)
|
---|
1333 | {
|
---|
1334 | VBOXPTE PteSrc = pPTSrc->a[offPTSrc + iPTDst];
|
---|
1335 | PGM_BTH_NAME(SyncPageWorker)(pVM, &pPTDst->a[iPTDst], PdeSrc, PteSrc, pShwPage, iPTDst);
|
---|
1336 | Log2(("SyncPage: 4K+ %VGv PteSrc:{P=%d RW=%d U=%d raw=%08llx}%s\n",
|
---|
1337 | (GCPtrPage & ~(RTGCUINTPTR)(X86_PT_MASK << X86_PT_SHIFT)) | ((offPTSrc + iPTDst) << PAGE_SHIFT),
|
---|
1338 | PteSrc.n.u1Present, PteSrc.n.u1Write, PteSrc.n.u1User, (uint64_t)PteSrc.u,
|
---|
1339 | pPTDst->a[iPTDst].u & PGM_PTFLAGS_TRACK_DIRTY ? " Track-Dirty" : ""));
|
---|
1340 | }
|
---|
1341 | }
|
---|
1342 | }
|
---|
1343 | else
|
---|
1344 | # endif /* PGM_SYNC_N_PAGES */
|
---|
1345 | {
|
---|
1346 | const unsigned iPTSrc = (GCPtrPage >> X86_PT_SHIFT) & X86_PT_MASK;
|
---|
1347 | VBOXPTE PteSrc = pPTSrc->a[iPTSrc];
|
---|
1348 | const unsigned iPTDst = (GCPtrPage >> SHW_PT_SHIFT) & SHW_PT_MASK;
|
---|
1349 | PGM_BTH_NAME(SyncPageWorker)(pVM, &pPTDst->a[iPTDst], PdeSrc, PteSrc, pShwPage, iPTDst);
|
---|
1350 | Log2(("SyncPage: 4K %VGv PteSrc:{P=%d RW=%d U=%d raw=%08llx}%s\n",
|
---|
1351 | GCPtrPage, PteSrc.n.u1Present, PteSrc.n.u1Write, PteSrc.n.u1User, (uint64_t)PteSrc.u,
|
---|
1352 | pPTDst->a[iPTDst].u & PGM_PTFLAGS_TRACK_DIRTY ? " Track-Dirty" : ""));
|
---|
1353 | }
|
---|
1354 | }
|
---|
1355 | else /* MMIO or invalid page: emulated in #PF handler. */
|
---|
1356 | Assert(!pPTDst->a[(GCPtrPage >> SHW_PT_SHIFT) & SHW_PT_MASK].n.u1Present);
|
---|
1357 | }
|
---|
1358 | else
|
---|
1359 | {
|
---|
1360 | /*
|
---|
1361 | * 4/2MB page - lazy syncing shadow 4K pages.
|
---|
1362 | * (There are many causes of getting here, it's no longer only CSAM.)
|
---|
1363 | */
|
---|
1364 | /* Calculate the GC physical address of this 4KB shadow page. */
|
---|
1365 | RTGCPHYS GCPhys = (PdeSrc.u & X86_PDE4M_PAE_PG_MASK) | ((RTGCUINTPTR)GCPtrPage & PAGE_OFFSET_MASK_BIG);
|
---|
1366 | /* Find ram range. */
|
---|
1367 | PRTHCPHYS pHCPhys;
|
---|
1368 | int rc = PGMRamGCPhys2PagePtr(&pVM->pgm.s, GCPhys, &pHCPhys);
|
---|
1369 | if (VBOX_SUCCESS(rc))
|
---|
1370 | {
|
---|
1371 | /*
|
---|
1372 | * Make shadow PTE entry.
|
---|
1373 | */
|
---|
1374 | RTHCPHYS HCPhys = *pHCPhys;
|
---|
1375 | SHWPTE PteDst;
|
---|
1376 | PteDst.u = (PdeSrc.u & ~(X86_PTE_PAE_PG_MASK | X86_PTE_AVL_MASK | X86_PTE_PAT | X86_PTE_PCD | X86_PTE_PWT))
|
---|
1377 | | (HCPhys & X86_PTE_PAE_PG_MASK);
|
---|
1378 | if (HCPhys & (MM_RAM_FLAGS_PHYSICAL_ALL | MM_RAM_FLAGS_VIRTUAL_ALL | MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_VIRTUAL_WRITE))
|
---|
1379 | {
|
---|
1380 | if (!(HCPhys & (MM_RAM_FLAGS_PHYSICAL_ALL | MM_RAM_FLAGS_VIRTUAL_ALL)))
|
---|
1381 | PteDst.n.u1Write = 0;
|
---|
1382 | else
|
---|
1383 | PteDst.u = 0;
|
---|
1384 | }
|
---|
1385 | const unsigned iPTDst = (GCPtrPage >> SHW_PT_SHIFT) & SHW_PT_MASK;
|
---|
1386 | # ifdef PGMPOOL_WITH_USER_TRACKING
|
---|
1387 | if (PteDst.n.u1Present && !pPTDst->a[iPTDst].n.u1Present)
|
---|
1388 | PGM_BTH_NAME(SyncPageWorkerTrackAddref)(pVM, pShwPage, HCPhys >> MM_RAM_FLAGS_IDX_SHIFT, pHCPhys, iPTDst);
|
---|
1389 | # endif
|
---|
1390 | pPTDst->a[iPTDst] = PteDst;
|
---|
1391 |
|
---|
1392 |
|
---|
1393 | # ifdef PGM_SYNC_DIRTY_BIT
|
---|
1394 | /*
|
---|
1395 | * If the page is not flagged as dirty and is writable, then make it read-only
|
---|
1396 | * at PD level, so we can set the dirty bit when the page is modified.
|
---|
1397 | *
|
---|
1398 | * ASSUMES that page access handlers are implemented on page table entry level.
|
---|
1399 | * Thus we will first catch the dirty access and set PDE.D and restart. If
|
---|
1400 | * there is an access handler, we'll trap again and let it work on the problem.
|
---|
1401 | */
|
---|
1402 | /** @todo r=bird: figure out why we need this here, SyncPT should've taken care of this already.
|
---|
1403 | * As for invlpg, it simply frees the whole shadow PT.
|
---|
1404 | * ...It's possibly because the guest clears it and the guest doesn't really tell us... */
|
---|
1405 | if (!PdeSrc.b.u1Dirty && PdeSrc.b.u1Write)
|
---|
1406 | {
|
---|
1407 | STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,DirtyPageBig));
|
---|
1408 | PdeDst.u |= PGM_PDFLAGS_TRACK_DIRTY;
|
---|
1409 | PdeDst.n.u1Write = 0;
|
---|
1410 | }
|
---|
1411 | else
|
---|
1412 | {
|
---|
1413 | PdeDst.au32[0] &= ~PGM_PDFLAGS_TRACK_DIRTY;
|
---|
1414 | PdeDst.n.u1Write = PdeSrc.n.u1Write;
|
---|
1415 | }
|
---|
1416 | # if PGM_SHW_TYPE == PGM_TYPE_32BIT
|
---|
1417 | pVM->pgm.s.CTXMID(p,32BitPD)->a[iPDDst] = PdeDst;
|
---|
1418 | # else /* PAE */
|
---|
1419 | pVM->pgm.s.CTXMID(ap,PaePDs)[0]->a[iPDDst] = PdeDst;
|
---|
1420 | # endif
|
---|
1421 | # endif /* PGM_SYNC_DIRTY_BIT */
|
---|
1422 | Log2(("SyncPage: BIG %VGv PdeSrc:{P=%d RW=%d U=%d raw=%08llx} GCPhys=%VGp%s\n",
|
---|
1423 | GCPtrPage, PdeSrc.n.u1Present, PdeSrc.n.u1Write, PdeSrc.n.u1User, (uint64_t)PdeSrc.u, GCPhys,
|
---|
1424 | PdeDst.u & PGM_PDFLAGS_TRACK_DIRTY ? " Track-Dirty" : ""));
|
---|
1425 | }
|
---|
1426 | }
|
---|
1427 | return VINF_SUCCESS;
|
---|
1428 | }
|
---|
1429 | # ifdef PGM_SYNC_ACCESSED_BIT
|
---|
1430 | STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,SyncPagePDNAs));
|
---|
1431 | #endif
|
---|
1432 | }
|
---|
1433 | else
|
---|
1434 | {
|
---|
1435 | STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,SyncPagePDOutOfSync));
|
---|
1436 | Log2(("SyncPage: Out-Of-Sync PDE at %VGp PdeSrc=%RX64 PdeDst=%RX64\n",
|
---|
1437 | GCPtrPage, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
|
---|
1438 | }
|
---|
1439 |
|
---|
1440 | /*
|
---|
1441 | * Mark the PDE not present. Restart the instruction and let #PF call SyncPT.
|
---|
1442 | * Yea, I'm lazy.
|
---|
1443 | */
|
---|
1444 | pgmPoolFree(pVM, PdeDst.u & SHW_PDE_PG_MASK, SHW_POOL_ROOT_IDX, iPDDst);
|
---|
1445 | # if PGM_SHW_TYPE == PGM_TYPE_32BIT
|
---|
1446 | pVM->pgm.s.CTXMID(p,32BitPD)->a[iPDDst].u = 0;
|
---|
1447 | # else /* PAE */
|
---|
1448 | pVM->pgm.s.CTXMID(ap,PaePDs)[0]->a[iPDDst].u = 0;
|
---|
1449 | # endif
|
---|
1450 | PGM_INVL_GUEST_TLBS();
|
---|
1451 | return VINF_PGM_SYNCPAGE_MODIFIED_PDE;
|
---|
1452 |
|
---|
1453 | #else /* PGM_GST_TYPE != PGM_TYPE_32BIT */
|
---|
1454 |
|
---|
1455 | AssertReleaseMsgFailed(("Shw=%d Gst=%d is not implemented!\n", PGM_GST_TYPE, PGM_SHW_TYPE));
|
---|
1456 | return VERR_INTERNAL_ERROR;
|
---|
1457 | #endif /* PGM_GST_TYPE != PGM_TYPE_32BIT */
|
---|
1458 | }
|
---|
1459 |
|
---|
1460 |
|
---|
1461 |
|
---|
1462 | #ifdef PGM_SYNC_DIRTY_BIT
|
---|
1463 |
|
---|
1464 | /**
|
---|
1465 | * Investigate page fault and handle write protection page faults caused by
|
---|
1466 | * dirty bit tracking.
|
---|
1467 | *
|
---|
1468 | * @returns VBox status code.
|
---|
1469 | * @param pVM VM handle.
|
---|
1470 | * @param uErr Page fault error code.
|
---|
1471 | * @param pPdeDst Shadow page directory entry.
|
---|
1472 | * @param pPdeSrc Guest page directory entry.
|
---|
1473 | * @param GCPtrPage Guest context page address.
|
---|
1474 | */
|
---|
1475 | PGM_BTH_DECL(int, CheckPageFault)(PVM pVM, uint32_t uErr, PSHWPDE pPdeDst, PVBOXPDE pPdeSrc, RTGCUINTPTR GCPtrPage)
|
---|
1476 | {
|
---|
1477 | STAM_PROFILE_START(&pVM->pgm.s.CTXMID(Stat, DirtyBitTracking), a);
|
---|
1478 | LogFlow(("CheckPageFault: GCPtrPage=%VGv uErr=%#x PdeSrc=%08x\n", GCPtrPage, uErr, pPdeSrc->u));
|
---|
1479 |
|
---|
1480 | /*
|
---|
1481 | * Real page fault?
|
---|
1482 | */
|
---|
1483 | if ( (uErr & X86_TRAP_PF_RSVD)
|
---|
1484 | || !pPdeSrc->n.u1Present
|
---|
1485 | || ((uErr & X86_TRAP_PF_RW) && !pPdeSrc->n.u1Write)
|
---|
1486 | || ((uErr & X86_TRAP_PF_US) && !pPdeSrc->n.u1User) )
|
---|
1487 | {
|
---|
1488 | #ifdef IN_GC
|
---|
1489 | STAM_COUNTER_INC(&pVM->pgm.s.StatGCDirtyTrackRealPF);
|
---|
1490 | #endif
|
---|
1491 | STAM_PROFILE_STOP(&pVM->pgm.s.CTXMID(Stat, DirtyBitTracking), a);
|
---|
1492 | LogFlow(("CheckPageFault: real page fault at %VGv (1)\n", GCPtrPage));
|
---|
1493 |
|
---|
1494 | if (pPdeSrc->n.u1Present)
|
---|
1495 | {
|
---|
1496 | /* Check the present bit as the shadow tables can cause different error codes by being out of sync.
|
---|
1497 | * See the 2nd case below as well.
|
---|
1498 | */
|
---|
1499 | if (pPdeSrc->b.u1Size && (CPUMGetGuestCR4(pVM) & X86_CR4_PSE))
|
---|
1500 | {
|
---|
1501 | TRPMSetErrorCode(pVM, uErr | X86_TRAP_PF_P); /* page-level protection violation */
|
---|
1502 | }
|
---|
1503 | else
|
---|
1504 | {
|
---|
1505 | /*
|
---|
1506 | * Map the guest page table.
|
---|
1507 | */
|
---|
1508 | PVBOXPT pPTSrc;
|
---|
1509 | int rc = PGM_GCPHYS_2_PTR(pVM, pPdeSrc->u & X86_PDE_PG_MASK, &pPTSrc);
|
---|
1510 | if (VBOX_SUCCESS(rc))
|
---|
1511 | {
|
---|
1512 | PVBOXPTE pPteSrc = &pPTSrc->a[(GCPtrPage >> PAGE_SHIFT) & PTE_MASK];
|
---|
1513 | const VBOXPTE PteSrc = *pPteSrc;
|
---|
1514 | if (pPteSrc->n.u1Present)
|
---|
1515 | TRPMSetErrorCode(pVM, uErr | X86_TRAP_PF_P); /* page-level protection violation */
|
---|
1516 | }
|
---|
1517 | AssertRC(rc);
|
---|
1518 | }
|
---|
1519 | }
|
---|
1520 | return VINF_EM_RAW_GUEST_TRAP;
|
---|
1521 | }
|
---|
1522 |
|
---|
1523 | /*
|
---|
1524 | * First check the easy case where the page directory has been marked read-only to track
|
---|
1525 | * the dirty bit of an emulated BIG page
|
---|
1526 | */
|
---|
1527 | if (pPdeSrc->b.u1Size && (CPUMGetGuestCR4(pVM) & X86_CR4_PSE))
|
---|
1528 | {
|
---|
1529 | /* Mark guest page directory as accessed */
|
---|
1530 | pPdeSrc->b.u1Accessed = 1;
|
---|
1531 |
|
---|
1532 | /*
|
---|
1533 | * Only write protection page faults are relevant here.
|
---|
1534 | */
|
---|
1535 | if (uErr & X86_TRAP_PF_RW)
|
---|
1536 | {
|
---|
1537 | /* Mark guest page directory as dirty (BIG page only). */
|
---|
1538 | pPdeSrc->b.u1Dirty = 1;
|
---|
1539 |
|
---|
1540 | if (pPdeDst->n.u1Present && (pPdeDst->u & PGM_PDFLAGS_TRACK_DIRTY))
|
---|
1541 | {
|
---|
1542 | STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,DirtyPageTrap));
|
---|
1543 |
|
---|
1544 | Assert(pPdeSrc->b.u1Write);
|
---|
1545 |
|
---|
1546 | pPdeDst->n.u1Write = 1;
|
---|
1547 | pPdeDst->n.u1Accessed = 1;
|
---|
1548 | pPdeDst->au32[0] &= ~PGM_PDFLAGS_TRACK_DIRTY;
|
---|
1549 | PGM_INVL_BIG_PG(GCPtrPage);
|
---|
1550 | STAM_PROFILE_STOP(&pVM->pgm.s.CTXMID(Stat,DirtyBitTracking), a);
|
---|
1551 | return VINF_PGM_HANDLED_DIRTY_BIT_FAULT;
|
---|
1552 | }
|
---|
1553 | }
|
---|
1554 | STAM_PROFILE_STOP(&pVM->pgm.s.CTXMID(Stat,DirtyBitTracking), a);
|
---|
1555 | return VINF_PGM_NO_DIRTY_BIT_TRACKING;
|
---|
1556 | }
|
---|
1557 | /* else: 4KB page table */
|
---|
1558 |
|
---|
1559 | /*
|
---|
1560 | * Map the guest page table.
|
---|
1561 | */
|
---|
1562 | PVBOXPT pPTSrc;
|
---|
1563 | int rc = PGM_GCPHYS_2_PTR(pVM, pPdeSrc->u & X86_PDE_PG_MASK, &pPTSrc);
|
---|
1564 | if (VBOX_SUCCESS(rc))
|
---|
1565 | {
|
---|
1566 | /*
|
---|
1567 | * Real page fault?
|
---|
1568 | */
|
---|
1569 | PVBOXPTE pPteSrc = &pPTSrc->a[(GCPtrPage >> PAGE_SHIFT) & PTE_MASK];
|
---|
1570 | const VBOXPTE PteSrc = *pPteSrc;
|
---|
1571 | if ( !PteSrc.n.u1Present
|
---|
1572 | || ((uErr & X86_TRAP_PF_RW) && !PteSrc.n.u1Write)
|
---|
1573 | || ((uErr & X86_TRAP_PF_US) && !PteSrc.n.u1User)
|
---|
1574 | )
|
---|
1575 | {
|
---|
1576 | #ifdef IN_GC
|
---|
1577 | STAM_COUNTER_INC(&pVM->pgm.s.StatGCDirtyTrackRealPF);
|
---|
1578 | #endif
|
---|
1579 | STAM_PROFILE_STOP(&pVM->pgm.s.CTXMID(Stat,DirtyBitTracking), a);
|
---|
1580 | LogFlow(("CheckPageFault: real page fault at %VGv PteSrc.u=%08x (2)\n", GCPtrPage, PteSrc.u));
|
---|
1581 |
|
---|
1582 | /* Check the present bit as the shadow tables can cause different error codes by being out of sync.
|
---|
1583 | * See the 2nd case above as well.
|
---|
1584 | */
|
---|
1585 | if (pPdeSrc->n.u1Present && pPteSrc->n.u1Present)
|
---|
1586 | TRPMSetErrorCode(pVM, uErr | X86_TRAP_PF_P); /* page-level protection violation */
|
---|
1587 |
|
---|
1588 | return VINF_EM_RAW_GUEST_TRAP;
|
---|
1589 | }
|
---|
1590 | LogFlow(("CheckPageFault: page fault at %VGv PteSrc.u=%08x\n", GCPtrPage, PteSrc.u));
|
---|
1591 |
|
---|
1592 | /*
|
---|
1593 | * Set the accessed bits in the page directory and the page table.
|
---|
1594 | */
|
---|
1595 | pPdeSrc->n.u1Accessed = 1;
|
---|
1596 | pPteSrc->n.u1Accessed = 1;
|
---|
1597 |
|
---|
1598 | /*
|
---|
1599 | * Only write protection page faults are relevant here.
|
---|
1600 | */
|
---|
1601 | if (uErr & X86_TRAP_PF_RW)
|
---|
1602 | {
|
---|
1603 | /* Write access, so mark guest entry as dirty. */
|
---|
1604 | #if defined(IN_GC) && defined(VBOX_WITH_STATISTICS)
|
---|
1605 | if (!pPteSrc->n.u1Dirty)
|
---|
1606 | STAM_COUNTER_INC(&pVM->pgm.s.StatGCDirtiedPage);
|
---|
1607 | else
|
---|
1608 | STAM_COUNTER_INC(&pVM->pgm.s.StatGCPageAlreadyDirty);
|
---|
1609 | #endif
|
---|
1610 | pPteSrc->n.u1Dirty = 1;
|
---|
1611 |
|
---|
1612 | if (pPdeDst->n.u1Present)
|
---|
1613 | {
|
---|
1614 | /*
|
---|
1615 | * Map shadow page table.
|
---|
1616 | */
|
---|
1617 | PPGMPOOLPAGE pShwPage = pgmPoolGetPageByHCPhys(pVM, pPdeDst->u & SHW_PDE_PG_MASK);
|
---|
1618 | PSHWPT pPTDst = (PSHWPT)PGMPOOL_PAGE_2_PTR(pVM, pShwPage);
|
---|
1619 | PSHWPTE pPteDst = &pPTDst->a[(GCPtrPage >> SHW_PT_SHIFT) & SHW_PT_MASK];
|
---|
1620 | if ( pPteDst->n.u1Present /** @todo Optimize accessed bit emulation? */
|
---|
1621 | && (pPteDst->u & PGM_PTFLAGS_TRACK_DIRTY))
|
---|
1622 | {
|
---|
1623 | LogFlow(("DIRTY page trap addr=%VGv\n", GCPtrPage));
|
---|
1624 | #ifdef VBOX_STRICT
|
---|
1625 | RTHCPHYS HCPhys;
|
---|
1626 | rc = PGMRamGCPhys2HCPhysWithFlags(&pVM->pgm.s, pPteSrc->u & X86_PTE_PG_MASK, &HCPhys);
|
---|
1627 | if (VBOX_SUCCESS(rc))
|
---|
1628 | AssertMsg(!(HCPhys & (MM_RAM_FLAGS_PHYSICAL_ALL | MM_RAM_FLAGS_VIRTUAL_ALL | MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_VIRTUAL_WRITE)),
|
---|
1629 | ("Unexpected dirty bit tracking on monitored page %VGv (phys %VGp)!!!!!!\n", GCPtrPage, pPteSrc->u & X86_PTE_PAE_PG_MASK));
|
---|
1630 | #endif
|
---|
1631 | STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,DirtyPageTrap));
|
---|
1632 |
|
---|
1633 | Assert(pPteSrc->n.u1Write);
|
---|
1634 |
|
---|
1635 | pPteDst->n.u1Write = 1;
|
---|
1636 | pPteDst->n.u1Dirty = 1;
|
---|
1637 | pPteDst->n.u1Accessed = 1;
|
---|
1638 | pPteDst->au32[0] &= ~PGM_PTFLAGS_TRACK_DIRTY;
|
---|
1639 | PGM_INVL_PG(GCPtrPage);
|
---|
1640 |
|
---|
1641 | STAM_PROFILE_STOP(&pVM->pgm.s.CTXMID(Stat,DirtyBitTracking), a);
|
---|
1642 | return VINF_PGM_HANDLED_DIRTY_BIT_FAULT;
|
---|
1643 | }
|
---|
1644 | }
|
---|
1645 | }
|
---|
1646 | /** @todo Optimize accessed bit emulation? */
|
---|
1647 | #ifdef VBOX_STRICT
|
---|
1648 | /*
|
---|
1649 | * Sanity check.
|
---|
1650 | */
|
---|
1651 | else if ( !pPteSrc->n.u1Dirty
|
---|
1652 | && (pPdeSrc->n.u1Write & pPteSrc->n.u1Write)
|
---|
1653 | && pPdeDst->n.u1Present)
|
---|
1654 | {
|
---|
1655 | PPGMPOOLPAGE pShwPage = pgmPoolGetPageByHCPhys(pVM, pPdeDst->u & SHW_PDE_PG_MASK);
|
---|
1656 | PSHWPT pPTDst = (PSHWPT)PGMPOOL_PAGE_2_PTR(pVM, pShwPage);
|
---|
1657 | PSHWPTE pPteDst = &pPTDst->a[(GCPtrPage >> SHW_PT_SHIFT) & SHW_PT_MASK];
|
---|
1658 | if ( pPteDst->n.u1Present
|
---|
1659 | && pPteDst->n.u1Write)
|
---|
1660 | LogFlow(("Writable present page %VGv not marked for dirty bit tracking!!!\n", GCPtrPage));
|
---|
1661 | }
|
---|
1662 | #endif /* VBOX_STRICT */
|
---|
1663 | STAM_PROFILE_STOP(&pVM->pgm.s.CTXMID(Stat,DirtyBitTracking), a);
|
---|
1664 | return VINF_PGM_NO_DIRTY_BIT_TRACKING;
|
---|
1665 | }
|
---|
1666 | AssertRC(rc);
|
---|
1667 | STAM_PROFILE_STOP(&pVM->pgm.s.CTXMID(Stat,DirtyBitTracking), a);
|
---|
1668 | return rc;
|
---|
1669 | }
|
---|
1670 |
|
---|
1671 | #endif
|
---|
1672 |
|
---|
1673 |
|
---|
1674 | /**
|
---|
1675 | * Sync a shadow page table.
|
---|
1676 | *
|
---|
1677 | * The shadow page table is not present. This includes the case where
|
---|
1678 | * there is a conflict with a mapping.
|
---|
1679 | *
|
---|
1680 | * @returns VBox status code.
|
---|
1681 | * @param pVM VM handle.
|
---|
1682 | * @param iPD Page directory index.
|
---|
1683 | * @param pPDSrc Source page directory (i.e. Guest OS page directory).
|
---|
1684 | * Assume this is a temporary mapping.
|
---|
1685 | * @param GCPtrPage GC Pointer of the page that caused the fault
|
---|
1686 | */
|
---|
1687 | PGM_BTH_DECL(int, SyncPT)(PVM pVM, unsigned iPDSrc, PVBOXPD pPDSrc, RTGCUINTPTR GCPtrPage)
|
---|
1688 | {
|
---|
1689 | STAM_PROFILE_START(&pVM->pgm.s.CTXMID(Stat,SyncPT), a);
|
---|
1690 | STAM_COUNTER_INC(&pVM->pgm.s.StatGCSyncPtPD[iPDSrc]);
|
---|
1691 | LogFlow(("SyncPT: GCPtrPage=%VGv\n", GCPtrPage));
|
---|
1692 |
|
---|
1693 | #if PGM_GST_TYPE == PGM_TYPE_32BIT
|
---|
1694 |
|
---|
1695 | # if PGM_SHW_TYPE != PGM_TYPE_32BIT && PGM_SHW_TYPE != PGM_TYPE_PAE
|
---|
1696 | # error "Invalid shadow mode for 32-bit guest mode!"
|
---|
1697 | # endif
|
---|
1698 |
|
---|
1699 | /*
|
---|
1700 | * Validate input a little bit.
|
---|
1701 | */
|
---|
1702 | Assert(iPDSrc == (GCPtrPage >> GST_PD_SHIFT));
|
---|
1703 | # if PGM_SHW_TYPE == PGM_TYPE_32BIT
|
---|
1704 | PX86PD pPDDst = pVM->pgm.s.CTXMID(p,32BitPD);
|
---|
1705 | # else
|
---|
1706 | PX86PDPAE pPDDst = pVM->pgm.s.CTXMID(ap,PaePDs)[0];
|
---|
1707 | # endif
|
---|
1708 | const unsigned iPDDst = GCPtrPage >> SHW_PD_SHIFT;
|
---|
1709 | PSHWPDE pPdeDst = &pPDDst->a[iPDDst];
|
---|
1710 | SHWPDE PdeDst = *pPdeDst;
|
---|
1711 |
|
---|
1712 | /*
|
---|
1713 | * Check for conflicts.
|
---|
1714 | * GC: In case of a conflict we'll go to Ring-3 and do a full SyncCR3.
|
---|
1715 | * HC: Simply resolve the conflict.
|
---|
1716 | */
|
---|
1717 | if (PdeDst.u & PGM_PDFLAGS_MAPPING)
|
---|
1718 | {
|
---|
1719 | Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
|
---|
1720 | # ifndef IN_RING3
|
---|
1721 | Log(("SyncPT: Conflict at %VGv\n", GCPtrPage));
|
---|
1722 | STAM_PROFILE_STOP(&pVM->pgm.s.CTXMID(Stat,SyncPT), a);
|
---|
1723 | return VERR_ADDRESS_CONFLICT;
|
---|
1724 | # else
|
---|
1725 | PPGMMAPPING pMapping = pgmGetMapping(pVM, (RTGCPTR)GCPtrPage);
|
---|
1726 | Assert(pMapping);
|
---|
1727 | int rc = pgmR3SyncPTResolveConflict(pVM, pMapping, pPDSrc, iPDSrc);
|
---|
1728 | if (VBOX_FAILURE(rc))
|
---|
1729 | {
|
---|
1730 | STAM_PROFILE_STOP(&pVM->pgm.s.CTXMID(Stat,SyncPT), a);
|
---|
1731 | return rc;
|
---|
1732 | }
|
---|
1733 | PdeDst = *pPdeDst;
|
---|
1734 | # endif
|
---|
1735 | }
|
---|
1736 | Assert(!PdeDst.n.u1Present); /* We're only supposed to call SyncPT on PDE!P and conflicts.*/
|
---|
1737 |
|
---|
1738 | /*
|
---|
1739 | * Sync page directory entry.
|
---|
1740 | */
|
---|
1741 | int rc = VINF_SUCCESS;
|
---|
1742 | GSTPDE PdeSrc = pPDSrc->a[iPDSrc];
|
---|
1743 | if (PdeSrc.n.u1Present)
|
---|
1744 | {
|
---|
1745 | /*
|
---|
1746 | * Allocate & map the page table.
|
---|
1747 | */
|
---|
1748 | PSHWPT pPTDst;
|
---|
1749 | const bool fPageTable = !PdeSrc.b.u1Size || !(CPUMGetGuestCR4(pVM) & X86_CR4_PSE);
|
---|
1750 | PPGMPOOLPAGE pShwPage;
|
---|
1751 | RTGCPHYS GCPhys;
|
---|
1752 | if (fPageTable)
|
---|
1753 | {
|
---|
1754 | GCPhys = PdeSrc.u & GST_PDE_PG_MASK;
|
---|
1755 | # if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
|
---|
1756 | GCPhys |= (iPDDst & 1) * (PAGE_SIZE / 2);
|
---|
1757 | # endif
|
---|
1758 | rc = pgmPoolAlloc(pVM, GCPhys, BTH_PGMPOOLKIND_PT_FOR_PT, SHW_POOL_ROOT_IDX, iPDDst, &pShwPage);
|
---|
1759 | }
|
---|
1760 | else
|
---|
1761 | {
|
---|
1762 | GCPhys = PdeSrc.u & GST_PDE4M_PG_MASK;
|
---|
1763 | # if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
|
---|
1764 | GCPhys |= GCPtrPage & BIT(X86_PAGE_2M_SHIFT);
|
---|
1765 | # endif
|
---|
1766 | rc = pgmPoolAlloc(pVM, GCPhys, BTH_PGMPOOLKIND_PT_FOR_BIG, SHW_POOL_ROOT_IDX, iPDDst, &pShwPage);
|
---|
1767 | }
|
---|
1768 | if (rc == VINF_SUCCESS)
|
---|
1769 | pPTDst = (PSHWPT)PGMPOOL_PAGE_2_PTR(pVM, pShwPage);
|
---|
1770 | else if (rc == VINF_PGM_CACHED_PAGE)
|
---|
1771 | {
|
---|
1772 | /*
|
---|
1773 | * The PT was cached, just hook it up.
|
---|
1774 | */
|
---|
1775 | if (fPageTable)
|
---|
1776 | PdeDst.u = pShwPage->Core.Key
|
---|
1777 | | (PdeSrc.u & ~(X86_PDE_PAE_PG_MASK | X86_PDE_AVL_MASK | X86_PDE_PCD | X86_PDE_PWT | X86_PDE_PS | X86_PDE4M_G | X86_PDE4M_D));
|
---|
1778 | else
|
---|
1779 | {
|
---|
1780 | PdeDst.u = pShwPage->Core.Key
|
---|
1781 | | (PdeSrc.u & ~(X86_PDE_PAE_PG_MASK | X86_PDE_AVL_MASK | X86_PDE_PCD | X86_PDE_PWT | X86_PDE_PS | X86_PDE4M_G | X86_PDE4M_D));
|
---|
1782 | # ifdef PGM_SYNC_DIRTY_BIT /* (see explanation and assumtions further down.) */
|
---|
1783 | if (!PdeSrc.b.u1Dirty && PdeSrc.b.u1Write)
|
---|
1784 | {
|
---|
1785 | STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,DirtyPageBig));
|
---|
1786 | PdeDst.u |= PGM_PDFLAGS_TRACK_DIRTY;
|
---|
1787 | PdeDst.b.u1Write = 0;
|
---|
1788 | }
|
---|
1789 | # endif
|
---|
1790 | }
|
---|
1791 | *pPdeDst = PdeDst;
|
---|
1792 | return VINF_SUCCESS;
|
---|
1793 | }
|
---|
1794 | else if (rc == VERR_PGM_POOL_FLUSHED)
|
---|
1795 | return VINF_PGM_SYNC_CR3;
|
---|
1796 | else
|
---|
1797 | AssertMsgFailedReturn(("rc=%Vrc\n", rc), VERR_INTERNAL_ERROR);
|
---|
1798 | PdeDst.u &= X86_PDE_AVL_MASK;
|
---|
1799 | PdeDst.u |= pShwPage->Core.Key;
|
---|
1800 |
|
---|
1801 | # ifdef PGM_SYNC_DIRTY_BIT
|
---|
1802 | /*
|
---|
1803 | * Page directory has been accessed (this is a fault situation, remember).
|
---|
1804 | */
|
---|
1805 | pPDSrc->a[iPDSrc].n.u1Accessed = 1;
|
---|
1806 | # endif
|
---|
1807 |
|
---|
1808 | if (fPageTable)
|
---|
1809 | {
|
---|
1810 | /*
|
---|
1811 | * Page table - 4KB.
|
---|
1812 | *
|
---|
1813 | * Sync all or just a few entries depending on PGM_SYNC_N_PAGES.
|
---|
1814 | */
|
---|
1815 | Log2(("SyncPT: 4K %VGv PdeSrc:{P=%d RW=%d U=%d raw=%08llx}\n",
|
---|
1816 | GCPtrPage, PdeSrc.b.u1Present, PdeSrc.b.u1Write, PdeSrc.b.u1User, (uint64_t)PdeSrc.u));
|
---|
1817 | PGSTPT pPTSrc;
|
---|
1818 | rc = PGM_GCPHYS_2_PTR(pVM, PdeSrc.u & GST_PDE_PG_MASK, &pPTSrc);
|
---|
1819 | if (VBOX_SUCCESS(rc))
|
---|
1820 | {
|
---|
1821 | /*
|
---|
1822 | * Start by syncing the page directory entry so CSAM's TLB trick works.
|
---|
1823 | */
|
---|
1824 | PdeDst.u = (PdeDst.u & (X86_PDE_PAE_PG_MASK | X86_PDE_AVL_MASK))
|
---|
1825 | | (PdeSrc.u & ~(X86_PDE_PAE_PG_MASK | X86_PDE_AVL_MASK | X86_PDE_PCD | X86_PDE_PWT | X86_PDE_PS | X86_PDE4M_G | X86_PDE4M_D));
|
---|
1826 | *pPdeDst = PdeDst;
|
---|
1827 |
|
---|
1828 | /*
|
---|
1829 | * Directory/page user or supervisor privilege: (same goes for read/write)
|
---|
1830 | *
|
---|
1831 | * Directory Page Combined
|
---|
1832 | * U/S U/S U/S
|
---|
1833 | * 0 0 0
|
---|
1834 | * 0 1 0
|
---|
1835 | * 1 0 0
|
---|
1836 | * 1 1 1
|
---|
1837 | *
|
---|
1838 | * Simple AND operation. Table listed for completeness.
|
---|
1839 | *
|
---|
1840 | */
|
---|
1841 | STAM_COUNTER_INC(CTXSUFF(&pVM->pgm.s.StatSynPT4k));
|
---|
1842 | # ifdef PGM_SYNC_N_PAGES
|
---|
1843 | unsigned iPTBase = (GCPtrPage >> SHW_PT_SHIFT) & SHW_PT_MASK;
|
---|
1844 | unsigned iPTDst = iPTBase;
|
---|
1845 | const unsigned iPTDstEnd = RT_MIN(iPTDst + PGM_SYNC_NR_PAGES / 2, ELEMENTS(pPTDst->a));
|
---|
1846 | if (iPTDst <= PGM_SYNC_NR_PAGES / 2)
|
---|
1847 | iPTDst = 0;
|
---|
1848 | else
|
---|
1849 | iPTDst -= PGM_SYNC_NR_PAGES / 2;
|
---|
1850 | # else /* !PGM_SYNC_N_PAGES */
|
---|
1851 | unsigned iPTDst = 0;
|
---|
1852 | const unsigned iPTDstEnd = ELEMENTS(pPTDst->a);
|
---|
1853 | # endif /* !PGM_SYNC_N_PAGES */
|
---|
1854 | # if PGM_SHW_TYPE == PGM_TYPE_32BIT
|
---|
1855 | const unsigned offPTSrc = 0;
|
---|
1856 | # else
|
---|
1857 | const unsigned offPTSrc = ((GCPtrPage >> SHW_PD_SHIFT) & 1) * 512;
|
---|
1858 | # endif
|
---|
1859 | for (; iPTDst < iPTDstEnd; iPTDst++)
|
---|
1860 | {
|
---|
1861 | const unsigned iPTSrc = iPTDst + offPTSrc;
|
---|
1862 | const GSTPTE PteSrc = pPTSrc->a[iPTSrc];
|
---|
1863 |
|
---|
1864 | if (PteSrc.n.u1Present) /* we've already cleared it above */
|
---|
1865 | {
|
---|
1866 | #ifndef IN_RING0
|
---|
1867 | /*
|
---|
1868 | * Assuming kernel code will be marked as supervisor - and not as user level
|
---|
1869 | * and executed using a conforming code selector - And marked as readonly.
|
---|
1870 | * Also assume that if we're monitoring a page, it's of no interest to CSAM.
|
---|
1871 | */
|
---|
1872 | if ( ((PdeSrc.u & pPTSrc->a[iPTSrc].u) & (X86_PTE_RW | X86_PTE_US))
|
---|
1873 | || !CSAMIsEnabled(pVM)
|
---|
1874 | || !CSAMDoesPageNeedScanning(pVM, (RTGCPTR)((iPDSrc << GST_PD_SHIFT) | (iPTSrc << PAGE_SHIFT)))
|
---|
1875 | || PGMRamTestFlags(&pVM->pgm.s, PteSrc.u & GST_PTE_PG_MASK,
|
---|
1876 | MM_RAM_FLAGS_PHYSICAL_ALL | MM_RAM_FLAGS_VIRTUAL_ALL | MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_VIRTUAL_WRITE)
|
---|
1877 | )
|
---|
1878 | #endif
|
---|
1879 | PGM_BTH_NAME(SyncPageWorker)(pVM, &pPTDst->a[iPTDst], PdeSrc, PteSrc, pShwPage, iPTDst);
|
---|
1880 | Log2(("SyncPT: 4K+ %VGv PteSrc:{P=%d RW=%d U=%d raw=%08llx}%s dst.raw=%08llx iPTSrc=%x PdeSrc.u=%x physpte=%VGp\n",
|
---|
1881 | (RTGCPTR)((iPDSrc << GST_PD_SHIFT) | (iPTSrc << PAGE_SHIFT)),
|
---|
1882 | PteSrc.n.u1Present, PteSrc.n.u1Write, PteSrc.n.u1User, (uint64_t)PteSrc.u,
|
---|
1883 | pPTDst->a[iPTDst].u & PGM_PTFLAGS_TRACK_DIRTY ? " Track-Dirty" : "", pPTDst->a[iPTDst].u, iPTSrc, PdeSrc.au32[0],
|
---|
1884 | (PdeSrc.u & GST_PDE_PG_MASK) + iPTSrc*sizeof(PteSrc)));
|
---|
1885 | }
|
---|
1886 | } /* for PTEs */
|
---|
1887 | }
|
---|
1888 | }
|
---|
1889 | else
|
---|
1890 | {
|
---|
1891 | /*
|
---|
1892 | * Big page - 2/4MB.
|
---|
1893 | *
|
---|
1894 | * We'll walk the ram range list in parallel and optimize lookups.
|
---|
1895 | * We will only sync on shadow page table at a time.
|
---|
1896 | */
|
---|
1897 | STAM_COUNTER_INC(CTXSUFF(&pVM->pgm.s.StatSynPT4M));
|
---|
1898 |
|
---|
1899 | /**
|
---|
1900 | * @todo It might be more efficient to sync only a part of the 4MB page (similar to what we do for 4kb PDs).
|
---|
1901 | */
|
---|
1902 |
|
---|
1903 | /*
|
---|
1904 | * Start by syncing the page directory entry.
|
---|
1905 | */
|
---|
1906 | PdeDst.u = (PdeDst.u & (X86_PDE_PAE_PG_MASK | (X86_PDE_AVL_MASK & ~PGM_PDFLAGS_TRACK_DIRTY)))
|
---|
1907 | | (PdeSrc.u & ~(X86_PDE_PAE_PG_MASK | X86_PDE_AVL_MASK | X86_PDE_PCD | X86_PDE_PWT | X86_PDE_PS | X86_PDE4M_G | X86_PDE4M_D));
|
---|
1908 |
|
---|
1909 | # ifdef PGM_SYNC_DIRTY_BIT
|
---|
1910 | /*
|
---|
1911 | * If the page is not flagged as dirty and is writable, then make it read-only
|
---|
1912 | * at PD level, so we can set the dirty bit when the page is modified.
|
---|
1913 | *
|
---|
1914 | * ASSUMES that page access handlers are implemented on page table entry level.
|
---|
1915 | * Thus we will first catch the dirty access and set PDE.D and restart. If
|
---|
1916 | * there is an access handler, we'll trap again and let it work on the problem.
|
---|
1917 | */
|
---|
1918 | /** @todo move the above stuff to a section in the PGM documentation. */
|
---|
1919 | Assert(!(PdeDst.u & PGM_PDFLAGS_TRACK_DIRTY));
|
---|
1920 | if (!PdeSrc.b.u1Dirty && PdeSrc.b.u1Write)
|
---|
1921 | {
|
---|
1922 | STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,DirtyPageBig));
|
---|
1923 | PdeDst.u |= PGM_PDFLAGS_TRACK_DIRTY;
|
---|
1924 | PdeDst.b.u1Write = 0;
|
---|
1925 | }
|
---|
1926 | # endif /* PGM_SYNC_DIRTY_BIT */
|
---|
1927 | *pPdeDst = PdeDst;
|
---|
1928 |
|
---|
1929 | /*
|
---|
1930 | * Fill the shadow page table.
|
---|
1931 | */
|
---|
1932 | /* Get address and flags from the source PDE. */
|
---|
1933 | SHWPTE PteDstBase;
|
---|
1934 | PteDstBase.u = PdeSrc.u & ~(X86_PTE_PAE_PG_MASK | X86_PTE_AVL_MASK | X86_PTE_PAT | X86_PTE_PCD | X86_PTE_PWT);
|
---|
1935 |
|
---|
1936 | /* Loop thru the entries in the shadow PT. */
|
---|
1937 | const RTGCUINTPTR GCPtr = (GCPtrPage >> SHW_PD_SHIFT) << SHW_PD_SHIFT; NOREF(GCPtr);
|
---|
1938 | Log2(("SyncPT: BIG %VGv PdeSrc:{P=%d RW=%d U=%d raw=%08llx} Shw=%VGv GCPhys=%VGp %s\n",
|
---|
1939 | GCPtrPage, PdeSrc.b.u1Present, PdeSrc.b.u1Write, PdeSrc.b.u1User, (uint64_t)PdeSrc.u, GCPtr,
|
---|
1940 | GCPhys, PdeDst.u & PGM_PDFLAGS_TRACK_DIRTY ? " Track-Dirty" : ""));
|
---|
1941 | PPGMRAMRANGE pRam = CTXSUFF(pVM->pgm.s.pRamRanges);
|
---|
1942 | unsigned iPTDst = 0;
|
---|
1943 | while (iPTDst < ELEMENTS(pPTDst->a))
|
---|
1944 | {
|
---|
1945 | /* Advance ram range list. */
|
---|
1946 | while (pRam && GCPhys > pRam->GCPhysLast)
|
---|
1947 | pRam = CTXSUFF(pRam->pNext);
|
---|
1948 | if (pRam && GCPhys >= pRam->GCPhys)
|
---|
1949 | {
|
---|
1950 | unsigned iHCPage = (GCPhys - pRam->GCPhys) >> PAGE_SHIFT;
|
---|
1951 | do
|
---|
1952 | {
|
---|
1953 | /* Make shadow PTE. */
|
---|
1954 | RTHCPHYS HCPhys = pRam->aHCPhys[iHCPage];
|
---|
1955 | SHWPTE PteDst;
|
---|
1956 | if (HCPhys & (MM_RAM_FLAGS_PHYSICAL_ALL | MM_RAM_FLAGS_VIRTUAL_ALL | MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_VIRTUAL_WRITE))
|
---|
1957 | {
|
---|
1958 | if (!(HCPhys & (MM_RAM_FLAGS_PHYSICAL_ALL | MM_RAM_FLAGS_VIRTUAL_ALL)))
|
---|
1959 | {
|
---|
1960 | PteDst.u = (HCPhys & X86_PTE_PAE_PG_MASK) | PteDstBase.u;
|
---|
1961 | PteDst.n.u1Write = 0;
|
---|
1962 | }
|
---|
1963 | else
|
---|
1964 | PteDst.u = 0;
|
---|
1965 | }
|
---|
1966 | #ifndef IN_RING0
|
---|
1967 | /*
|
---|
1968 | * Assuming kernel code will be marked as supervisor and not as user level and executed
|
---|
1969 | * using a conforming code selector. Don't check for readonly, as that implies the whole
|
---|
1970 | * 4MB can be code or readonly data. Linux enables write access for its large pages.
|
---|
1971 | */
|
---|
1972 | else if ( !PdeSrc.n.u1User
|
---|
1973 | && CSAMIsEnabled(pVM)
|
---|
1974 | && CSAMDoesPageNeedScanning(pVM, (RTGCPTR)(GCPtr | (iPTDst << SHW_PT_SHIFT))))
|
---|
1975 | PteDst.u = 0;
|
---|
1976 | #endif
|
---|
1977 | else
|
---|
1978 | PteDst.u = (HCPhys & X86_PTE_PAE_PG_MASK) | PteDstBase.u;
|
---|
1979 | # ifdef PGMPOOL_WITH_USER_TRACKING
|
---|
1980 | if (PteDst.n.u1Present)
|
---|
1981 | PGM_BTH_NAME(SyncPageWorkerTrackAddref)(pVM, pShwPage, HCPhys >> MM_RAM_FLAGS_IDX_SHIFT, &pRam->aHCPhys[iHCPage], iPTDst);
|
---|
1982 | # endif
|
---|
1983 | /* commit it */
|
---|
1984 | pPTDst->a[iPTDst] = PteDst;
|
---|
1985 | Log4(("SyncPT: BIG %VGv PteDst:{P=%d RW=%d U=%d raw=%08llx}%s\n",
|
---|
1986 | (RTGCPTR)(GCPtr | (iPTDst << SHW_PT_SHIFT)), PteDst.n.u1Present, PteDst.n.u1Write, PteDst.n.u1User, (uint64_t)PteDst.u,
|
---|
1987 | PteDst.u & PGM_PTFLAGS_TRACK_DIRTY ? " Track-Dirty" : ""));
|
---|
1988 |
|
---|
1989 | /* advance */
|
---|
1990 | GCPhys += PAGE_SIZE;
|
---|
1991 | iHCPage++;
|
---|
1992 | iPTDst++;
|
---|
1993 | } while ( iPTDst < ELEMENTS(pPTDst->a)
|
---|
1994 | && GCPhys <= pRam->GCPhysLast);
|
---|
1995 | }
|
---|
1996 | else if (pRam)
|
---|
1997 | {
|
---|
1998 | Log(("Invalid pages at %VGp\n", GCPhys));
|
---|
1999 | do
|
---|
2000 | {
|
---|
2001 | pPTDst->a[iPTDst].u = 0; /* MMIO or invalid page, we must handle them manually. */
|
---|
2002 | GCPhys += PAGE_SIZE;
|
---|
2003 | iPTDst++;
|
---|
2004 | } while ( iPTDst < ELEMENTS(pPTDst->a)
|
---|
2005 | && GCPhys < pRam->GCPhys);
|
---|
2006 | }
|
---|
2007 | else
|
---|
2008 | {
|
---|
2009 | Log(("Invalid pages at %VGp (2)\n", GCPhys));
|
---|
2010 | for ( ; iPTDst < ELEMENTS(pPTDst->a); iPTDst++)
|
---|
2011 | pPTDst->a[iPTDst].u = 0; /* MMIO or invalid page, we must handle them manually. */
|
---|
2012 | }
|
---|
2013 | } /* while more PTEs */
|
---|
2014 | } /* 4KB / 4MB */
|
---|
2015 | }
|
---|
2016 | else
|
---|
2017 | AssertRelease(!PdeDst.n.u1Present);
|
---|
2018 |
|
---|
2019 | STAM_PROFILE_STOP(&pVM->pgm.s.CTXMID(Stat,SyncPT), a);
|
---|
2020 | # ifdef IN_GC
|
---|
2021 | if (VBOX_FAILURE(rc))
|
---|
2022 | STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,SyncPTFailed));
|
---|
2023 | # endif
|
---|
2024 | return rc;
|
---|
2025 |
|
---|
2026 | #else /* PGM_GST_TYPE != PGM_TYPE_32BIT */
|
---|
2027 |
|
---|
2028 | AssertReleaseMsgFailed(("Shw=%d Gst=%d is not implemented!\n", PGM_GST_TYPE, PGM_SHW_TYPE));
|
---|
2029 | STAM_PROFILE_STOP(&pVM->pgm.s.CTXMID(Stat,SyncPT), a);
|
---|
2030 | return VERR_INTERNAL_ERROR;
|
---|
2031 | #endif /* PGM_GST_TYPE != PGM_TYPE_32BIT */
|
---|
2032 | }
|
---|
2033 |
|
---|
2034 |
|
---|
2035 |
|
---|
2036 | /**
|
---|
2037 | * Prefetch a page/set of pages.
|
---|
2038 | *
|
---|
2039 | * Typically used to sync commonly used pages before entering raw mode
|
---|
2040 | * after a CR3 reload.
|
---|
2041 | *
|
---|
2042 | * @returns VBox status code.
|
---|
2043 | * @param pVM VM handle.
|
---|
2044 | * @param GCPtrPage Page to invalidate.
|
---|
2045 | */
|
---|
2046 | PGM_BTH_DECL(int, PrefetchPage)(PVM pVM, RTGCUINTPTR GCPtrPage)
|
---|
2047 | {
|
---|
2048 | #if PGM_GST_TYPE == PGM_TYPE_32BIT
|
---|
2049 |
|
---|
2050 | # if PGM_SHW_TYPE != PGM_TYPE_32BIT && PGM_SHW_TYPE != PGM_TYPE_PAE
|
---|
2051 | # error "Invalid shadow mode for 32-bit guest mode!"
|
---|
2052 | # endif
|
---|
2053 |
|
---|
2054 | /*
|
---|
2055 | * Check that all Guest levels thru the PDE are present, getting the
|
---|
2056 | * PD and PDE in the processes.
|
---|
2057 | */
|
---|
2058 | int rc = VINF_SUCCESS;
|
---|
2059 | PVBOXPD pPDSrc = CTXSUFF(pVM->pgm.s.pGuestPD);
|
---|
2060 | const unsigned iPDSrc = GCPtrPage >> GST_PD_SHIFT;
|
---|
2061 | const VBOXPDE PdeSrc = pPDSrc->a[iPDSrc];
|
---|
2062 | # ifdef PGM_SYNC_ACCESSED_BIT
|
---|
2063 | if (PdeSrc.n.u1Present && PdeSrc.n.u1Accessed)
|
---|
2064 | # else
|
---|
2065 | if (PdeSrc.n.u1Present)
|
---|
2066 | # endif
|
---|
2067 | {
|
---|
2068 | # if PGM_SHW_TYPE == PGM_TYPE_32BIT
|
---|
2069 | const X86PDE PdeDst = pVM->pgm.s.CTXMID(p,32BitPD)->a[GCPtrPage >> X86_PD_SHIFT];
|
---|
2070 | # else
|
---|
2071 | const X86PDEPAE PdeDst = pVM->pgm.s.CTXMID(ap,PaePDs)[0]->a[GCPtrPage >> X86_PD_PAE_SHIFT];
|
---|
2072 | # endif
|
---|
2073 | if (!(PdeDst.u & PGM_PDFLAGS_MAPPING))
|
---|
2074 | {
|
---|
2075 | if (!PdeDst.n.u1Present)
|
---|
2076 | /** r=bird: This guy will set the A bit on the PDE, probably harmless. */
|
---|
2077 | rc = PGM_BTH_NAME(SyncPT)(pVM, iPDSrc, pPDSrc, GCPtrPage);
|
---|
2078 | else
|
---|
2079 | {
|
---|
2080 | /** @note We used to sync PGM_SYNC_NR_PAGES pages, which triggered assertions in CSAM, because
|
---|
2081 | * R/W attributes of nearby pages were reset. Not sure how that could happen. Anyway, it
|
---|
2082 | * makes no sense to prefetch more than one page.
|
---|
2083 | */
|
---|
2084 | rc = PGM_BTH_NAME(SyncPage)(pVM, PdeSrc, GCPtrPage, 1, 0);
|
---|
2085 | if (VBOX_SUCCESS(rc))
|
---|
2086 | rc = VINF_SUCCESS;
|
---|
2087 | }
|
---|
2088 | }
|
---|
2089 | }
|
---|
2090 | return rc;
|
---|
2091 |
|
---|
2092 | #else /* PGM_GST_TYPE != PGM_TYPE_32BIT */
|
---|
2093 |
|
---|
2094 | AssertReleaseMsgFailed(("Shw=%d Gst=%d is not implemented!\n", PGM_SHW_TYPE, PGM_GST_TYPE));
|
---|
2095 | return VERR_INTERNAL_ERROR;
|
---|
2096 | #endif /* PGM_GST_TYPE != PGM_TYPE_32BIT */
|
---|
2097 | }
|
---|
2098 |
|
---|
2099 |
|
---|
2100 |
|
---|
2101 |
|
---|
2102 | /**
|
---|
2103 | * Syncs a page during a PGMVerifyAccess() call.
|
---|
2104 | *
|
---|
2105 | * @returns VBox status code (informational included).
|
---|
2106 | * @param GCPtrPage The address of the page to sync.
|
---|
2107 | * @param fPage The effective guest page flags.
|
---|
2108 | * @param uErr The trap error code.
|
---|
2109 | */
|
---|
2110 | PGM_BTH_DECL(int, VerifyAccessSyncPage)(PVM pVM, RTGCUINTPTR GCPtrPage, unsigned fPage, unsigned uErr)
|
---|
2111 | {
|
---|
2112 | LogFlow(("VerifyAccessSyncPage: GCPtrPage=%VGv fPage=%#x uErr=%#x\n", GCPtrPage, fPage, uErr));
|
---|
2113 |
|
---|
2114 | #if PGM_GST_TYPE == PGM_TYPE_32BIT
|
---|
2115 |
|
---|
2116 | # if PGM_SHW_TYPE != PGM_TYPE_32BIT && PGM_SHW_TYPE != PGM_TYPE_PAE
|
---|
2117 | # error "Invalid shadow mode for 32-bit guest mode!"
|
---|
2118 | # endif
|
---|
2119 |
|
---|
2120 | #ifndef IN_RING0
|
---|
2121 | if (!(fPage & X86_PTE_US))
|
---|
2122 | {
|
---|
2123 | /*
|
---|
2124 | * Mark this page as safe.
|
---|
2125 | */
|
---|
2126 | /** @todo not correct for pages that contain both code and data!! */
|
---|
2127 | Log(("CSAMMarkPage %VGv; scanned=%d\n", GCPtrPage, true));
|
---|
2128 | CSAMMarkPage(pVM, (RTGCPTR)GCPtrPage, true);
|
---|
2129 | }
|
---|
2130 | #endif
|
---|
2131 | /*
|
---|
2132 | * Get guest PD and index.
|
---|
2133 | */
|
---|
2134 | unsigned iPDSrc = GCPtrPage >> GST_PD_SHIFT;
|
---|
2135 | PVBOXPD pPDSrc = CTXSUFF(pVM->pgm.s.pGuestPD);
|
---|
2136 | int rc = VINF_SUCCESS;
|
---|
2137 |
|
---|
2138 | # ifdef PGM_SYNC_DIRTY_BIT
|
---|
2139 | /*
|
---|
2140 | * First check if the page fault was caused by dirty bit tracking
|
---|
2141 | */
|
---|
2142 | # if PGM_SHW_TYPE == PGM_TYPE_32BIT
|
---|
2143 | PX86PDE pPdeDst = &pVM->pgm.s.CTXMID(p,32BitPD)->a[GCPtrPage >> X86_PD_SHIFT];
|
---|
2144 | # else
|
---|
2145 | PX86PDEPAE pPdeDst = &pVM->pgm.s.CTXMID(ap,PaePDs)[0]->a[GCPtrPage >> X86_PD_PAE_SHIFT];
|
---|
2146 | # endif
|
---|
2147 | rc = PGM_BTH_NAME(CheckPageFault)(pVM, uErr, pPdeDst, &pPDSrc->a[iPDSrc], GCPtrPage);
|
---|
2148 | if (rc == VINF_PGM_HANDLED_DIRTY_BIT_FAULT)
|
---|
2149 | Log(("PGMVerifyAccess: success (dirty)\n"));
|
---|
2150 | else
|
---|
2151 | # endif /* PGM_SYNC_DIRTY_BIT */
|
---|
2152 | {
|
---|
2153 | Assert(rc != VINF_EM_RAW_GUEST_TRAP);
|
---|
2154 | if (uErr & X86_TRAP_PF_US)
|
---|
2155 | STAM_COUNTER_INC(&pVM->pgm.s.StatGCPageOutOfSyncUser);
|
---|
2156 | else /* supervisor */
|
---|
2157 | STAM_COUNTER_INC(&pVM->pgm.s.StatGCPageOutOfSyncSupervisor);
|
---|
2158 |
|
---|
2159 | rc = PGM_BTH_NAME(SyncPage)(pVM, pPDSrc->a[iPDSrc], GCPtrPage, 1, 0);
|
---|
2160 | if (VBOX_SUCCESS(rc))
|
---|
2161 | {
|
---|
2162 | /* Page was successfully synced */
|
---|
2163 | Log(("PGMVerifyAccess: success (sync)\n"));
|
---|
2164 | rc = VINF_SUCCESS;
|
---|
2165 | }
|
---|
2166 | else
|
---|
2167 | {
|
---|
2168 | Log(("PGMVerifyAccess: access violation for %VGv rc=%d\n", GCPtrPage, rc));
|
---|
2169 | return VINF_EM_RAW_GUEST_TRAP;
|
---|
2170 | }
|
---|
2171 | }
|
---|
2172 | return rc;
|
---|
2173 |
|
---|
2174 | #else /* PGM_GST_TYPE != PGM_TYPE_32BIT */
|
---|
2175 |
|
---|
2176 | AssertReleaseMsgFailed(("Shw=%d Gst=%d is not implemented!\n", PGM_GST_TYPE, PGM_SHW_TYPE));
|
---|
2177 | return VERR_INTERNAL_ERROR;
|
---|
2178 | #endif /* PGM_GST_TYPE != PGM_TYPE_32BIT */
|
---|
2179 | }
|
---|
2180 |
|
---|
2181 |
|
---|
2182 | #if PGM_GST_TYPE == PGM_TYPE_32BIT
|
---|
2183 | # if PGM_SHW_TYPE == PGM_TYPE_32BIT || PGM_SHW_TYPE == PGM_TYPE_PAE
|
---|
2184 | /**
|
---|
2185 | * Figures out which kind of shadow page this guest PDE warrants.
|
---|
2186 | *
|
---|
2187 | * @returns Shadow page kind.
|
---|
2188 | * @param pPdeSrc The guest PDE in question.
|
---|
2189 | * @param cr4 The current guest cr4 value.
|
---|
2190 | */
|
---|
2191 | DECLINLINE(PGMPOOLKIND) PGM_BTH_NAME(CalcPageKind)(const VBOXPDE *pPdeSrc, uint32_t cr4)
|
---|
2192 | {
|
---|
2193 | if (!pPdeSrc->n.u1Size || !(cr4 & X86_CR4_PSE))
|
---|
2194 | return BTH_PGMPOOLKIND_PT_FOR_PT;
|
---|
2195 | //switch (pPdeSrc->u & (X86_PDE4M_RW | X86_PDE4M_US /*| X86_PDE4M_PAE_NX*/))
|
---|
2196 | //{
|
---|
2197 | // case 0:
|
---|
2198 | // return BTH_PGMPOOLKIND_PT_FOR_BIG_RO;
|
---|
2199 | // case X86_PDE4M_RW:
|
---|
2200 | // return BTH_PGMPOOLKIND_PT_FOR_BIG_RW;
|
---|
2201 | // case X86_PDE4M_US:
|
---|
2202 | // return BTH_PGMPOOLKIND_PT_FOR_BIG_US;
|
---|
2203 | // case X86_PDE4M_RW | X86_PDE4M_US:
|
---|
2204 | // return BTH_PGMPOOLKIND_PT_FOR_BIG_RW_US;
|
---|
2205 | # if 0
|
---|
2206 | // case X86_PDE4M_PAE_NX:
|
---|
2207 | // return BTH_PGMPOOLKIND_PT_FOR_BIG_NX;
|
---|
2208 | // case X86_PDE4M_RW | X86_PDE4M_PAE_NX:
|
---|
2209 | // return BTH_PGMPOOLKIND_PT_FOR_BIG_RW_NX;
|
---|
2210 | // case X86_PDE4M_US | X86_PDE4M_PAE_NX:
|
---|
2211 | // return BTH_PGMPOOLKIND_PT_FOR_BIG_US_NX;
|
---|
2212 | // case X86_PDE4M_RW | X86_PDE4M_US | X86_PDE4M_PAE_NX:
|
---|
2213 | // return BTH_PGMPOOLKIND_PT_FOR_BIG_RW_US_NX;
|
---|
2214 | # endif
|
---|
2215 | return BTH_PGMPOOLKIND_PT_FOR_BIG;
|
---|
2216 | //}
|
---|
2217 | }
|
---|
2218 | # endif
|
---|
2219 | #endif
|
---|
2220 |
|
---|
2221 | #undef MY_STAM_COUNTER_INC
|
---|
2222 | #define MY_STAM_COUNTER_INC(a) do { } while (0)
|
---|
2223 |
|
---|
2224 |
|
---|
2225 | /**
|
---|
2226 | * Syncs the paging hierarchy starting at CR3.
|
---|
2227 | *
|
---|
2228 | * @returns VBox status code, no specials.
|
---|
2229 | * @param pVM The virtual machine.
|
---|
2230 | * @param cr0 Guest context CR0 register
|
---|
2231 | * @param cr3 Guest context CR3 register
|
---|
2232 | * @param cr4 Guest context CR4 register
|
---|
2233 | * @param fGlobal Including global page directories or not
|
---|
2234 | */
|
---|
2235 | PGM_BTH_DECL(int, SyncCR3)(PVM pVM, uint32_t cr0, uint32_t cr3, uint32_t cr4, bool fGlobal)
|
---|
2236 | {
|
---|
2237 | #if PGM_GST_TYPE == PGM_TYPE_32BIT
|
---|
2238 | # if PGM_SHW_TYPE == PGM_TYPE_32BIT || PGM_SHW_TYPE == PGM_TYPE_PAE
|
---|
2239 | /*
|
---|
2240 | * Inform the PGM PD Cache Manager about the pending sync.
|
---|
2241 | */
|
---|
2242 | if (fGlobal || VM_FF_ISSET(pVM, VM_FF_PGM_SYNC_CR3))
|
---|
2243 | {
|
---|
2244 | # if 0 /** @todo what the heck is this about? */
|
---|
2245 | /* Don't cause an additional global CR3 reload the next time (the flag is cleared in PGMSyncCR3). */
|
---|
2246 | VM_FF_CLEAR(pVM, VM_FF_PGM_SYNC_CR3);
|
---|
2247 | # endif
|
---|
2248 |
|
---|
2249 | /* Change this CR3 reload to be a global one. */
|
---|
2250 | fGlobal = true;
|
---|
2251 | }
|
---|
2252 | # endif
|
---|
2253 | #endif
|
---|
2254 |
|
---|
2255 | /*
|
---|
2256 | * Update page access handlers.
|
---|
2257 | * The virtual are always flushed, while the physical are only on demand.
|
---|
2258 | * WARNING: We are incorrectly not doing global flushing on Virtual Handler updates. We'll
|
---|
2259 | * have to look into that later because it will have a bad influence on the performance.
|
---|
2260 | * @note SvL: There's no need for that. Just invalidate the virtual range(s).
|
---|
2261 | * bird: Yes, but that won't work for aliases.
|
---|
2262 | */
|
---|
2263 | /** @todo this MUST go away. See #1557. */
|
---|
2264 | STAM_PROFILE_START(&pVM->pgm.s.CTXMID(Stat,SyncCR3Handlers), h);
|
---|
2265 | PGM_GST_NAME(HandlerVirtualUpdate)(pVM, cr4);
|
---|
2266 | STAM_PROFILE_STOP(&pVM->pgm.s.CTXMID(Stat,SyncCR3Handlers), h);
|
---|
2267 |
|
---|
2268 | #ifdef PGMPOOL_WITH_MONITORING
|
---|
2269 | /*
|
---|
2270 | * When monitoring shadowed pages, we reset the modification counters on CR3 sync.
|
---|
2271 | * Occationally we will have to clear all the shadow page tables because we wanted
|
---|
2272 | * to monitor a page which was mapped by too many shadowed page tables. This operation
|
---|
2273 | * sometimes refered to as a 'lightweight flush'.
|
---|
2274 | */
|
---|
2275 | if (!(pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL))
|
---|
2276 | pgmPoolMonitorModifiedClearAll(pVM);
|
---|
2277 | else
|
---|
2278 | {
|
---|
2279 | # ifdef IN_RING3
|
---|
2280 | pVM->pgm.s.fSyncFlags &= ~PGM_SYNC_CLEAR_PGM_POOL;
|
---|
2281 | pgmPoolClearAll(pVM);
|
---|
2282 | # else
|
---|
2283 | LogFlow(("SyncCR3: PGM_SYNC_CLEAR_PGM_POOL is set -> VINF_PGM_SYNC_CR3\n"));
|
---|
2284 | return VINF_PGM_SYNC_CR3;
|
---|
2285 | # endif
|
---|
2286 | }
|
---|
2287 | #endif
|
---|
2288 |
|
---|
2289 | Assert(fGlobal || (cr4 & X86_CR4_PGE));
|
---|
2290 | MY_STAM_COUNTER_INC(fGlobal ? &pVM->pgm.s.CTXMID(Stat,SyncCR3Global) : &pVM->pgm.s.CTXMID(Stat,SyncCR3NotGlobal));
|
---|
2291 |
|
---|
2292 | #if PGM_GST_TYPE == PGM_TYPE_32BIT
|
---|
2293 | # if PGM_SHW_TYPE == PGM_TYPE_32BIT || PGM_SHW_TYPE == PGM_TYPE_PAE
|
---|
2294 | /*
|
---|
2295 | * Get page directory addresses.
|
---|
2296 | */
|
---|
2297 | # if PGM_SHW_TYPE == PGM_TYPE_32BIT
|
---|
2298 | PX86PDE pPDEDst = &pVM->pgm.s.CTXMID(p,32BitPD)->a[0];
|
---|
2299 | # else
|
---|
2300 | PX86PDEPAE pPDEDst = &pVM->pgm.s.CTXMID(ap,PaePDs)[0]->a[0];
|
---|
2301 | # endif
|
---|
2302 | PVBOXPD pPDSrc = pVM->pgm.s.CTXSUFF(pGuestPD);
|
---|
2303 |
|
---|
2304 | Assert(pPDSrc);
|
---|
2305 | #ifndef IN_GC
|
---|
2306 | Assert(MMPhysGCPhys2HCVirt(pVM, (RTGCPHYS)(cr3 & X86_CR3_PAGE_MASK)) == pPDSrc);
|
---|
2307 | #endif
|
---|
2308 |
|
---|
2309 | /*
|
---|
2310 | * Iterate the page directory.
|
---|
2311 | */
|
---|
2312 | PPGMMAPPING pMapping;
|
---|
2313 | unsigned iPdNoMapping;
|
---|
2314 | const bool fRawR0Enabled = EMIsRawRing0Enabled(pVM);
|
---|
2315 | PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
|
---|
2316 |
|
---|
2317 | /* Only check mappings if they are supposed to be put into the shadow page table. */
|
---|
2318 | if (pgmMapAreMappingsEnabled(&pVM->pgm.s))
|
---|
2319 | {
|
---|
2320 | pMapping = pVM->pgm.s.CTXSUFF(pMappings);
|
---|
2321 | iPdNoMapping = (pMapping) ? pMapping->GCPtr >> PGDIR_SHIFT : ~0U;
|
---|
2322 | }
|
---|
2323 | else
|
---|
2324 | {
|
---|
2325 | pMapping = 0;
|
---|
2326 | iPdNoMapping = ~0U;
|
---|
2327 | }
|
---|
2328 |
|
---|
2329 | for (unsigned iPD = 0; iPD < ELEMENTS(pPDSrc->a); iPD++)
|
---|
2330 | {
|
---|
2331 | # if PGM_SHW_TYPE == PGM_TYPE_32BIT
|
---|
2332 | Assert(&pVM->pgm.s.CTXMID(p,32BitPD)->a[iPD] == pPDEDst);
|
---|
2333 | # else
|
---|
2334 | Assert(&pVM->pgm.s.CTXMID(ap,PaePDs)[iPD * 2 / 512]->a[iPD * 2 % 512] == pPDEDst);
|
---|
2335 | # endif
|
---|
2336 | register VBOXPDE PdeSrc = pPDSrc->a[iPD];
|
---|
2337 | if ( PdeSrc.n.u1Present
|
---|
2338 | && (PdeSrc.n.u1User || fRawR0Enabled))
|
---|
2339 | {
|
---|
2340 | /*
|
---|
2341 | * Check for conflicts with GC mappings.
|
---|
2342 | */
|
---|
2343 | if (iPD == iPdNoMapping)
|
---|
2344 | {
|
---|
2345 | if (pVM->pgm.s.fMappingsFixed)
|
---|
2346 | {
|
---|
2347 | /* It's fixed, just skip the mapping. */
|
---|
2348 | const unsigned cPTs = pMapping->cPTs;
|
---|
2349 | iPD += cPTs - 1;
|
---|
2350 | pPDEDst += cPTs + (PGM_SHW_TYPE != PGM_TYPE_32BIT) * cPTs;
|
---|
2351 | pMapping = pMapping->CTXSUFF(pNext);
|
---|
2352 | iPdNoMapping = pMapping ? pMapping->GCPtr >> PGDIR_SHIFT : ~0U;
|
---|
2353 | continue;
|
---|
2354 | }
|
---|
2355 |
|
---|
2356 | #ifdef IN_RING3
|
---|
2357 | int rc = pgmR3SyncPTResolveConflict(pVM, pMapping, pPDSrc, iPD);
|
---|
2358 | if (VBOX_FAILURE(rc))
|
---|
2359 | return rc;
|
---|
2360 |
|
---|
2361 | /*
|
---|
2362 | * Update iPdNoMapping and pMapping.
|
---|
2363 | */
|
---|
2364 | pMapping = pVM->pgm.s.pMappingsHC;
|
---|
2365 | while (pMapping && pMapping->GCPtr < (iPD << PGDIR_SHIFT))
|
---|
2366 | pMapping = pMapping->pNextHC;
|
---|
2367 | iPdNoMapping = pMapping ? pMapping->GCPtr >> PGDIR_SHIFT : ~0U;
|
---|
2368 | #else
|
---|
2369 | LogFlow(("SyncCR3: detected conflict -> VINF_PGM_SYNC_CR3\n"));
|
---|
2370 | return VINF_PGM_SYNC_CR3;
|
---|
2371 | #endif
|
---|
2372 | }
|
---|
2373 |
|
---|
2374 | /*
|
---|
2375 | * Sync page directory entry.
|
---|
2376 | *
|
---|
2377 | * The current approach is to allocated the page table but to set
|
---|
2378 | * the entry to not-present and postpone the page table synching till
|
---|
2379 | * it's actually used.
|
---|
2380 | */
|
---|
2381 | # if PGM_SHW_TYPE == PGM_TYPE_32BIT
|
---|
2382 | const unsigned iPdShw = iPD; NOREF(iPdShw);
|
---|
2383 | # else
|
---|
2384 | for (unsigned i = 0, iPdShw = iPD * 2; i < 2; i++, iPdShw++) /* pray that the compiler unrolls this */
|
---|
2385 | # endif
|
---|
2386 | {
|
---|
2387 | SHWPDE PdeDst = *pPDEDst;
|
---|
2388 | if (PdeDst.n.u1Present)
|
---|
2389 | {
|
---|
2390 | PPGMPOOLPAGE pShwPage = pgmPoolGetPage(pPool, PdeDst.u & SHW_PDE_PG_MASK);
|
---|
2391 | RTGCPHYS GCPhys;
|
---|
2392 | if ( !PdeSrc.b.u1Size
|
---|
2393 | || !(cr4 & X86_CR4_PSE))
|
---|
2394 | {
|
---|
2395 | GCPhys = PdeSrc.u & GST_PDE_PG_MASK;
|
---|
2396 | # if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
|
---|
2397 | GCPhys |= i * (PAGE_SIZE / 2);
|
---|
2398 | # endif
|
---|
2399 | }
|
---|
2400 | else
|
---|
2401 | {
|
---|
2402 | GCPhys = PdeSrc.u & GST_PDE4M_PG_MASK;
|
---|
2403 | # if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
|
---|
2404 | GCPhys |= i * X86_PAGE_2M_SIZE;
|
---|
2405 | # endif
|
---|
2406 | }
|
---|
2407 |
|
---|
2408 | if ( pShwPage->GCPhys == GCPhys
|
---|
2409 | && pShwPage->enmKind == PGM_BTH_NAME(CalcPageKind)(&PdeSrc, cr4)
|
---|
2410 | && ( pShwPage->fCached
|
---|
2411 | || ( !fGlobal
|
---|
2412 | && ( false
|
---|
2413 | # ifdef PGM_SKIP_GLOBAL_PAGEDIRS_ON_NONGLOBAL_FLUSH
|
---|
2414 | || ( (PdeSrc.u & (X86_PDE4M_PS | X86_PDE4M_G)) == (X86_PDE4M_PS | X86_PDE4M_G)
|
---|
2415 | && (cr4 & (X86_CR4_PGE | X86_CR4_PSE)) == (X86_CR4_PGE | X86_CR4_PSE)) /* global 2/4MB page. */
|
---|
2416 | || ( !pShwPage->fSeenNonGlobal
|
---|
2417 | && (cr4 & X86_CR4_PGE))
|
---|
2418 | # endif
|
---|
2419 | )
|
---|
2420 | )
|
---|
2421 | )
|
---|
2422 | && ( (PdeSrc.u & (X86_PDE_US | X86_PDE_RW)) == (PdeDst.u & (X86_PDE_US | X86_PDE_RW))
|
---|
2423 | || ( (cr4 & X86_CR4_PSE)
|
---|
2424 | && ((PdeSrc.u & (X86_PDE_US | X86_PDE4M_PS | X86_PDE4M_D)) | PGM_PDFLAGS_TRACK_DIRTY)
|
---|
2425 | == ((PdeDst.u & (X86_PDE_US | X86_PDE_RW | PGM_PDFLAGS_TRACK_DIRTY)) | X86_PDE4M_PS))
|
---|
2426 | )
|
---|
2427 | )
|
---|
2428 | {
|
---|
2429 | # ifdef VBOX_WITH_STATISTICS
|
---|
2430 | if ( !fGlobal
|
---|
2431 | && (PdeSrc.u & (X86_PDE4M_PS | X86_PDE4M_G)) == (X86_PDE4M_PS | X86_PDE4M_G)
|
---|
2432 | && (cr4 & (X86_CR4_PGE | X86_CR4_PSE)) == (X86_CR4_PGE | X86_CR4_PSE))
|
---|
2433 | MY_STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,SyncCR3DstSkippedGlobalPD));
|
---|
2434 | else if (!fGlobal && !pShwPage->fSeenNonGlobal && (cr4 & X86_CR4_PGE))
|
---|
2435 | MY_STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,SyncCR3DstSkippedGlobalPT));
|
---|
2436 | else
|
---|
2437 | MY_STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,SyncCR3DstCacheHit));
|
---|
2438 | # endif /* VBOX_WITH_STATISTICS */
|
---|
2439 | /** @todo a replacement strategy isn't really needed unless we're using a very small pool < 512 pages.
|
---|
2440 | * The whole ageing stuff should be put in yet another set of #ifdefs. For now, let's just skip it. */
|
---|
2441 | //# ifdef PGMPOOL_WITH_CACHE
|
---|
2442 | // pgmPoolCacheUsed(pPool, pShwPage);
|
---|
2443 | //# endif
|
---|
2444 | }
|
---|
2445 | else
|
---|
2446 | {
|
---|
2447 | pgmPoolFreeByPage(pPool, pShwPage, SHW_POOL_ROOT_IDX, iPdShw);
|
---|
2448 | pPDEDst->u = 0;
|
---|
2449 | MY_STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,SyncCR3DstFreed));
|
---|
2450 | }
|
---|
2451 | }
|
---|
2452 | else
|
---|
2453 | MY_STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,SyncCR3DstNotPresent));
|
---|
2454 | pPDEDst++;
|
---|
2455 | }
|
---|
2456 | }
|
---|
2457 | else if (iPD != iPdNoMapping)
|
---|
2458 | {
|
---|
2459 | /*
|
---|
2460 | * Check if there is any page directory to mark not present here.
|
---|
2461 | */
|
---|
2462 | # if PGM_SHW_TYPE == PGM_TYPE_32BIT
|
---|
2463 | const unsigned iPdShw = iPD; NOREF(iPdShw);
|
---|
2464 | # else
|
---|
2465 | for (unsigned i = 0, iPdShw = iPD * 2; i < 2; i++, iPdShw++) /* pray that the compiler unrolls this */
|
---|
2466 | # endif
|
---|
2467 | {
|
---|
2468 | if (pPDEDst->n.u1Present)
|
---|
2469 | {
|
---|
2470 | pgmPoolFreeByPage(pPool, pgmPoolGetPage(pPool, pPDEDst->u & SHW_PDE_PG_MASK), SHW_POOL_ROOT_IDX, iPdShw);
|
---|
2471 | pPDEDst->u = 0;
|
---|
2472 | MY_STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,SyncCR3DstFreedSrcNP));
|
---|
2473 | }
|
---|
2474 | pPDEDst++;
|
---|
2475 | }
|
---|
2476 | }
|
---|
2477 | else
|
---|
2478 | {
|
---|
2479 | Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
|
---|
2480 | const unsigned cPTs = pMapping->cPTs;
|
---|
2481 | if (pVM->pgm.s.fMappingsFixed)
|
---|
2482 | {
|
---|
2483 | /* It's fixed, just skip the mapping. */
|
---|
2484 | pMapping = pMapping->CTXSUFF(pNext);
|
---|
2485 | iPdNoMapping = pMapping ? pMapping->GCPtr >> PGDIR_SHIFT : ~0U;
|
---|
2486 | }
|
---|
2487 | else
|
---|
2488 | {
|
---|
2489 | /*
|
---|
2490 | * Check for conflicts for subsequent pagetables
|
---|
2491 | * and advance to the next mapping.
|
---|
2492 | */
|
---|
2493 | iPdNoMapping = ~0U;
|
---|
2494 | unsigned iPT = cPTs;
|
---|
2495 | while (iPT-- > 1)
|
---|
2496 | {
|
---|
2497 | if ( pPDSrc->a[iPD + iPT].n.u1Present
|
---|
2498 | && (pPDSrc->a[iPD + iPT].n.u1User || fRawR0Enabled))
|
---|
2499 | {
|
---|
2500 | # ifdef IN_RING3
|
---|
2501 | int rc = pgmR3SyncPTResolveConflict(pVM, pMapping, pPDSrc, iPD);
|
---|
2502 | if (VBOX_FAILURE(rc))
|
---|
2503 | return rc;
|
---|
2504 |
|
---|
2505 | /*
|
---|
2506 | * Update iPdNoMapping and pMapping.
|
---|
2507 | */
|
---|
2508 | pMapping = pVM->pgm.s.CTXSUFF(pMappings);
|
---|
2509 | while (pMapping && pMapping->GCPtr < (iPD << PGDIR_SHIFT))
|
---|
2510 | pMapping = pMapping->CTXSUFF(pNext);
|
---|
2511 | iPdNoMapping = pMapping ? pMapping->GCPtr >> PGDIR_SHIFT : ~0U;
|
---|
2512 | break;
|
---|
2513 | # else
|
---|
2514 | LogFlow(("SyncCR3: detected conflict -> VINF_PGM_SYNC_CR3\n"));
|
---|
2515 | return VINF_PGM_SYNC_CR3;
|
---|
2516 | # endif
|
---|
2517 | }
|
---|
2518 | }
|
---|
2519 | if (iPdNoMapping == ~0U && pMapping)
|
---|
2520 | {
|
---|
2521 | pMapping = pMapping->CTXSUFF(pNext);
|
---|
2522 | if (pMapping)
|
---|
2523 | iPdNoMapping = pMapping->GCPtr >> PGDIR_SHIFT;
|
---|
2524 | }
|
---|
2525 | }
|
---|
2526 | /* advance. */
|
---|
2527 | iPD += cPTs - 1;
|
---|
2528 | pPDEDst += cPTs + (PGM_SHW_TYPE != PGM_TYPE_32BIT) * cPTs;
|
---|
2529 | }
|
---|
2530 |
|
---|
2531 | } /* for iPD */
|
---|
2532 | # elif PGM_SHW_TYPE == PGM_TYPE_AMD64
|
---|
2533 | # error "Guest 32-bit mode and shadow AMD64 mode doesn't add up!"
|
---|
2534 | # endif
|
---|
2535 |
|
---|
2536 | return VINF_SUCCESS;
|
---|
2537 |
|
---|
2538 | #elif PGM_GST_TYPE == PGM_TYPE_PAE
|
---|
2539 | # if PGM_SHW_TYPE == PGM_TYPE_PAE
|
---|
2540 | //# error not implemented
|
---|
2541 | return VERR_INTERNAL_ERROR;
|
---|
2542 |
|
---|
2543 | # else /* PGM_SHW_TYPE != PGM_TYPE_AMD64 */
|
---|
2544 | # error "Guest PAE mode, but not the shadow mode ; 32bit - maybe, but amd64 no."
|
---|
2545 | # endif /* PGM_SHW_TYPE != PGM_TYPE_AMD64 */
|
---|
2546 |
|
---|
2547 | #elif PGM_GST_TYPE == PGM_TYPE_AMD64
|
---|
2548 | # if PGM_SHW_TYPE == PGM_TYPE_AMD64
|
---|
2549 | //# error not implemented
|
---|
2550 | return VERR_INTERNAL_ERROR;
|
---|
2551 |
|
---|
2552 | # else /* PGM_SHW_TYPE != PGM_TYPE_AMD64 */
|
---|
2553 | # error "Guest AMD64 mode, but not the shadow mode - that can't be right!"
|
---|
2554 | # endif /* PGM_SHW_TYPE != PGM_TYPE_AMD64 */
|
---|
2555 |
|
---|
2556 | #else /* guest real and protected mode */
|
---|
2557 |
|
---|
2558 | return VINF_SUCCESS;
|
---|
2559 | #endif
|
---|
2560 | }
|
---|
2561 |
|
---|
2562 |
|
---|
2563 |
|
---|
2564 |
|
---|
2565 | #ifdef VBOX_STRICT
|
---|
2566 | #ifdef IN_GC
|
---|
2567 | # undef AssertMsgFailed
|
---|
2568 | # define AssertMsgFailed Log
|
---|
2569 | #endif
|
---|
2570 | #ifdef IN_RING3
|
---|
2571 | # include <VBox/dbgf.h>
|
---|
2572 |
|
---|
2573 | /**
|
---|
2574 | * Dumps a page table hierarchy use only physical addresses and cr4/lm flags.
|
---|
2575 | *
|
---|
2576 | * @returns VBox status code (VINF_SUCCESS).
|
---|
2577 | * @param pVM The VM handle.
|
---|
2578 | * @param cr3 The root of the hierarchy.
|
---|
2579 | * @param crr The cr4, only PAE and PSE is currently used.
|
---|
2580 | * @param fLongMode Set if long mode, false if not long mode.
|
---|
2581 | * @param cMaxDepth Number of levels to dump.
|
---|
2582 | * @param pHlp Pointer to the output functions.
|
---|
2583 | */
|
---|
2584 | __BEGIN_DECLS
|
---|
2585 | PGMR3DECL(int) PGMR3DumpHierarchyHC(PVM pVM, uint32_t cr3, uint32_t cr4, bool fLongMode, unsigned cMaxDepth, PCDBGFINFOHLP pHlp);
|
---|
2586 | __END_DECLS
|
---|
2587 |
|
---|
2588 | #endif
|
---|
2589 |
|
---|
2590 | /**
|
---|
2591 | * Checks that the shadow page table is in sync with the guest one.
|
---|
2592 | *
|
---|
2593 | * @returns The number of errors.
|
---|
2594 | * @param pVM The virtual machine.
|
---|
2595 | * @param cr3 Guest context CR3 register
|
---|
2596 | * @param cr4 Guest context CR4 register
|
---|
2597 | * @param GCPtr Where to start. Defaults to 0.
|
---|
2598 | * @param cb How much to check. Defaults to everything.
|
---|
2599 | */
|
---|
2600 | PGM_BTH_DECL(unsigned, AssertCR3)(PVM pVM, uint32_t cr3, uint32_t cr4, RTGCUINTPTR GCPtr, RTGCUINTPTR cb)
|
---|
2601 | {
|
---|
2602 | unsigned cErrors = 0;
|
---|
2603 |
|
---|
2604 | #if PGM_GST_TYPE == PGM_TYPE_32BIT
|
---|
2605 |
|
---|
2606 | # if PGM_SHW_TYPE != PGM_TYPE_32BIT && PGM_SHW_TYPE != PGM_TYPE_PAE
|
---|
2607 | # error "Invalid shadow mode for 32-bit guest paging."
|
---|
2608 | # endif
|
---|
2609 |
|
---|
2610 | PPGM pPGM = &pVM->pgm.s;
|
---|
2611 | RTHCPHYS HCPhysShw; /* page address derived from the shadow page tables. */
|
---|
2612 | RTGCPHYS GCPhysGst; /* page address derived from the guest page tables. */
|
---|
2613 | RTHCPHYS HCPhys; /* general usage. */
|
---|
2614 | int rc;
|
---|
2615 |
|
---|
2616 | /*
|
---|
2617 | * Check that the Guest CR3 and all it's mappings are correct.
|
---|
2618 | */
|
---|
2619 | AssertMsgReturn(pPGM->GCPhysCR3 == (cr3 & X86_CR3_PAGE_MASK),
|
---|
2620 | ("Invalid GCPhysCR3=%VGp cr3=%VGp\n", pPGM->GCPhysCR3, (RTGCPHYS)cr3),
|
---|
2621 | false);
|
---|
2622 | rc = PGMShwGetPage(pVM, pPGM->pGuestPDGC, NULL, &HCPhysShw);
|
---|
2623 | AssertRCReturn(rc, 1);
|
---|
2624 | rc = PGMRamGCPhys2HCPhys(pPGM, cr3 & X86_CR3_PAGE_MASK, &HCPhys);
|
---|
2625 | AssertMsgReturn(HCPhys == HCPhysShw, ("HCPhys=%VHp HCPhyswShw=%VHp (cr3)\n", HCPhys, HCPhysShw), false);
|
---|
2626 | # ifndef IN_GC
|
---|
2627 | RTGCPHYS GCPhys;
|
---|
2628 | rc = PGMPhysHCPtr2GCPhys(pVM, pPGM->pGuestPDHC, &GCPhys);
|
---|
2629 | AssertRCReturn(rc, 1);
|
---|
2630 | AssertMsgReturn((cr3 & X86_CR3_PAGE_MASK) == GCPhys, ("GCPhys=%VGp cr3=%VGp\n", GCPhys, (RTGCPHYS)cr3), false);
|
---|
2631 | # endif
|
---|
2632 | const X86PD *pPDSrc = CTXSUFF(pPGM->pGuestPD);
|
---|
2633 |
|
---|
2634 | /*
|
---|
2635 | * Get and check the Shadow CR3.
|
---|
2636 | */
|
---|
2637 | # if PGM_SHW_TYPE == PGM_TYPE_32BIT
|
---|
2638 | const X86PD *pPDDst = pPGM->CTXMID(p,32BitPD);
|
---|
2639 | unsigned cPDEs = ELEMENTS(pPDDst->a);
|
---|
2640 | # else
|
---|
2641 | const X86PDPAE *pPDDst = pPGM->CTXMID(ap,PaePDs[0]); /* use it as a 2048 entry PD */
|
---|
2642 | unsigned cPDEs = ELEMENTS(pPDDst->a) * ELEMENTS(pPGM->apHCPaePDs);
|
---|
2643 | # endif
|
---|
2644 | if (cb != ~(RTGCUINTPTR)0)
|
---|
2645 | cPDEs = RT_MIN(cb >> SHW_PD_SHIFT, 1);
|
---|
2646 |
|
---|
2647 | /** @todo call the other two PGMAssert*() functions. */
|
---|
2648 |
|
---|
2649 | /*
|
---|
2650 | * Iterate the shadow page directory.
|
---|
2651 | */
|
---|
2652 | GCPtr = (GCPtr >> SHW_PD_SHIFT) << SHW_PD_SHIFT;
|
---|
2653 | unsigned iPDDst = GCPtr >> SHW_PD_SHIFT;
|
---|
2654 | cPDEs += iPDDst;
|
---|
2655 | for (;
|
---|
2656 | iPDDst < cPDEs;
|
---|
2657 | iPDDst++, GCPtr += _4G / cPDEs)
|
---|
2658 | {
|
---|
2659 | const SHWPDE PdeDst = pPDDst->a[iPDDst];
|
---|
2660 | if (PdeDst.u & PGM_PDFLAGS_MAPPING)
|
---|
2661 | {
|
---|
2662 | Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
|
---|
2663 | if ((PdeDst.u & X86_PDE_AVL_MASK) != PGM_PDFLAGS_MAPPING)
|
---|
2664 | {
|
---|
2665 | AssertMsgFailed(("Mapping shall only have PGM_PDFLAGS_MAPPING set! PdeDst.u=%#RX64\n", (uint64_t)PdeDst.u));
|
---|
2666 | cErrors++;
|
---|
2667 | continue;
|
---|
2668 | }
|
---|
2669 | }
|
---|
2670 | else if ( (PdeDst.u & X86_PDE_P)
|
---|
2671 | || ((PdeDst.u & (X86_PDE_P | PGM_PDFLAGS_TRACK_DIRTY)) == (X86_PDE_P | PGM_PDFLAGS_TRACK_DIRTY))
|
---|
2672 | )
|
---|
2673 | {
|
---|
2674 | HCPhysShw = PdeDst.u & SHW_PDE_PG_MASK;
|
---|
2675 | PPGMPOOLPAGE pPage = pgmPoolGetPageByHCPhys(pVM, HCPhysShw);
|
---|
2676 | if (!pPage)
|
---|
2677 | {
|
---|
2678 | AssertMsgFailed(("Invalid page table address %VGp at %VGv! PdeDst=%#RX64\n",
|
---|
2679 | HCPhysShw, GCPtr, (uint64_t)PdeDst.u));
|
---|
2680 | cErrors++;
|
---|
2681 | continue;
|
---|
2682 | }
|
---|
2683 | const SHWPT *pPTDst = (const SHWPT *)PGMPOOL_PAGE_2_PTR(pVM, pPage);
|
---|
2684 |
|
---|
2685 | if (PdeDst.u & (X86_PDE4M_PWT | X86_PDE4M_PCD))
|
---|
2686 | {
|
---|
2687 | AssertMsgFailed(("PDE flags PWT and/or PCD is set at %VGv! These flags are not virtualized! PdeDst=%#RX64\n",
|
---|
2688 | GCPtr, (uint64_t)PdeDst.u));
|
---|
2689 | cErrors++;
|
---|
2690 | }
|
---|
2691 |
|
---|
2692 | if (PdeDst.u & (X86_PDE4M_G | X86_PDE4M_D))
|
---|
2693 | {
|
---|
2694 | AssertMsgFailed(("4K PDE reserved flags at %VGv! PdeDst=%#RX64\n",
|
---|
2695 | GCPtr, (uint64_t)PdeDst.u));
|
---|
2696 | cErrors++;
|
---|
2697 | }
|
---|
2698 |
|
---|
2699 | const X86PDE PdeSrc = pPDSrc->a[iPDDst >> (GST_PD_SHIFT - SHW_PD_SHIFT)];
|
---|
2700 | if (!PdeSrc.n.u1Present)
|
---|
2701 | {
|
---|
2702 | AssertMsgFailed(("Guest PDE at %VGv is not present! PdeDst=%#RX64 PdeSrc=%#RX64\n",
|
---|
2703 | GCPtr, (uint64_t)PdeDst.u, (uint64_t)PdeSrc.u));
|
---|
2704 | cErrors++;
|
---|
2705 | continue;
|
---|
2706 | }
|
---|
2707 |
|
---|
2708 | if ( !PdeSrc.b.u1Size
|
---|
2709 | || !(cr4 & X86_CR4_PSE))
|
---|
2710 | {
|
---|
2711 | GCPhysGst = PdeSrc.u & GST_PDE_PG_MASK;
|
---|
2712 | # if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
|
---|
2713 | GCPhysGst |= (iPDDst & 1) * (PAGE_SIZE / 2);
|
---|
2714 | # endif
|
---|
2715 | }
|
---|
2716 | else
|
---|
2717 | {
|
---|
2718 | if (PdeSrc.u & X86_PDE4M_PG_HIGH_MASK)
|
---|
2719 | {
|
---|
2720 | AssertMsgFailed(("Guest PDE at %VGv is using PSE36 or similar! PdeSrc=%#RX64\n",
|
---|
2721 | GCPtr, (uint64_t)PdeSrc.u));
|
---|
2722 | cErrors++;
|
---|
2723 | continue;
|
---|
2724 | }
|
---|
2725 | GCPhysGst = PdeSrc.u & GST_PDE4M_PG_MASK;
|
---|
2726 | # if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
|
---|
2727 | GCPhysGst |= GCPtr & BIT(X86_PAGE_2M_SHIFT);
|
---|
2728 | # endif
|
---|
2729 | }
|
---|
2730 |
|
---|
2731 | if ( pPage->enmKind
|
---|
2732 | != (!PdeSrc.b.u1Size || !(cr4 & X86_CR4_PSE) ? BTH_PGMPOOLKIND_PT_FOR_PT : BTH_PGMPOOLKIND_PT_FOR_BIG))
|
---|
2733 | {
|
---|
2734 | AssertMsgFailed(("Invalid shadow page table kind %d at %VGv! PdeSrc=%#RX64\n",
|
---|
2735 | pPage->enmKind, GCPtr, (uint64_t)PdeSrc.u));
|
---|
2736 | cErrors++;
|
---|
2737 | }
|
---|
2738 |
|
---|
2739 | rc = PGMRamGCPhys2HCPhysWithFlags(pPGM, GCPhysGst, &HCPhys);
|
---|
2740 | if (VBOX_FAILURE(rc))
|
---|
2741 | {
|
---|
2742 | AssertMsgFailed(("Cannot find guest physical address %VGp in the PDE at %VGv! PdeSrc=%#RX64\n",
|
---|
2743 | GCPhysGst, GCPtr, (uint64_t)PdeSrc.u));
|
---|
2744 | cErrors++;
|
---|
2745 | continue;
|
---|
2746 | }
|
---|
2747 |
|
---|
2748 | if (GCPhysGst != pPage->GCPhys)
|
---|
2749 | {
|
---|
2750 | AssertMsgFailed(("GCPhysGst=%VGp != pPage->GCPhys=%VGp at %VGv\n",
|
---|
2751 | GCPhysGst, pPage->GCPhys, GCPtr));
|
---|
2752 | cErrors++;
|
---|
2753 | continue;
|
---|
2754 | }
|
---|
2755 |
|
---|
2756 | if ( !PdeSrc.b.u1Size
|
---|
2757 | || !(cr4 & X86_CR4_PSE))
|
---|
2758 | {
|
---|
2759 | /*
|
---|
2760 | * Page Table.
|
---|
2761 | */
|
---|
2762 | const GSTPT *pPTSrc;
|
---|
2763 | rc = PGM_GCPHYS_2_PTR(pVM, GCPhysGst & ~(RTGCPHYS)(PAGE_SIZE - 1), &pPTSrc);
|
---|
2764 | if (VBOX_FAILURE(rc))
|
---|
2765 | {
|
---|
2766 | AssertMsgFailed(("Cannot map/convert guest physical address %VGp in the PDE at %VGv! PdeSrc=%#RX64\n",
|
---|
2767 | GCPhysGst, GCPtr, (uint64_t)PdeSrc.u));
|
---|
2768 | cErrors++;
|
---|
2769 | continue;
|
---|
2770 | }
|
---|
2771 | if ( (PdeSrc.u & (X86_PDE_P | X86_PDE_US | X86_PDE_RW/* | X86_PDE_A*/))
|
---|
2772 | != (PdeDst.u & (X86_PDE_P | X86_PDE_US | X86_PDE_RW/* | X86_PDE_A*/)))
|
---|
2773 | {
|
---|
2774 | /// @todo We get here a lot on out-of-sync CR3 entries. The access handler should zap them to avoid false alarms here!
|
---|
2775 | // (This problem will go away when/if we shadow multiple CR3s.)
|
---|
2776 | AssertMsgFailed(("4K PDE flags mismatch at %VGv! PdeSrc=%#RX64 PdeDst=%#RX64\n",
|
---|
2777 | GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
|
---|
2778 | cErrors++;
|
---|
2779 | continue;
|
---|
2780 | }
|
---|
2781 | if (PdeDst.u & PGM_PDFLAGS_TRACK_DIRTY)
|
---|
2782 | {
|
---|
2783 | AssertMsgFailed(("4K PDEs cannot have PGM_PDFLAGS_TRACK_DIRTY set! GCPtr=%VGv PdeDst=%#RX64\n",
|
---|
2784 | GCPtr, (uint64_t)PdeDst.u));
|
---|
2785 | cErrors++;
|
---|
2786 | continue;
|
---|
2787 | }
|
---|
2788 |
|
---|
2789 | /* iterate the page table. */
|
---|
2790 | # if PGM_SHW_TYPE == PGM_TYPE_32BIT
|
---|
2791 | const unsigned offPTSrc = 0;
|
---|
2792 | # else
|
---|
2793 | const unsigned offPTSrc = ((GCPtr >> SHW_PD_SHIFT) & 1) * 512;
|
---|
2794 | # endif
|
---|
2795 | for (unsigned iPT = 0, off = 0;
|
---|
2796 | iPT < ELEMENTS(pPTDst->a);
|
---|
2797 | iPT++, off += PAGE_SIZE)
|
---|
2798 | {
|
---|
2799 | const SHWPTE PteDst = pPTDst->a[iPT];
|
---|
2800 |
|
---|
2801 | /* skip not-present entries. */
|
---|
2802 | if (!(PteDst.u & (X86_PTE_P | PGM_PTFLAGS_TRACK_DIRTY))) /** @todo deal with ALL handlers and CSAM !P pages! */
|
---|
2803 | continue;
|
---|
2804 | Assert(PteDst.n.u1Present);
|
---|
2805 |
|
---|
2806 | const GSTPTE PteSrc = pPTSrc->a[iPT + offPTSrc];
|
---|
2807 | if (!PteSrc.n.u1Present)
|
---|
2808 | {
|
---|
2809 | #ifdef IN_RING3
|
---|
2810 | PGMAssertHandlerAndFlagsInSync(pVM);
|
---|
2811 | PGMR3DumpHierarchyGC(pVM, cr3, cr4, (PdeSrc.u & GST_PDE_PG_MASK));
|
---|
2812 | #endif
|
---|
2813 | AssertMsgFailed(("Out of sync (!P) PTE at %VGv! PteSrc=%#RX64 PteDst=%#RX64 pPTSrc=%VGv iPTSrc=%x PdeSrc=%x physpte=%VGp\n",
|
---|
2814 | GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u, pPTSrc, iPT + offPTSrc, PdeSrc.au32[0],
|
---|
2815 | (PdeSrc.u & GST_PDE_PG_MASK) + (iPT + offPTSrc)*sizeof(PteSrc)));
|
---|
2816 | cErrors++;
|
---|
2817 | continue;
|
---|
2818 | }
|
---|
2819 |
|
---|
2820 | uint64_t fIgnoreFlags = GST_PTE_PG_MASK | X86_PTE_AVL_MASK | X86_PTE_G | X86_PTE_D | X86_PTE_PWT | X86_PTE_PCD | X86_PTE_PAT;
|
---|
2821 | # if 1 /** @todo sync accessed bit properly... */
|
---|
2822 | fIgnoreFlags |= X86_PTE_A;
|
---|
2823 | # endif
|
---|
2824 |
|
---|
2825 | /* match the physical addresses */
|
---|
2826 | HCPhysShw = PteDst.u & SHW_PTE_PG_MASK;
|
---|
2827 | GCPhysGst = PteSrc.u & GST_PTE_PG_MASK;
|
---|
2828 |
|
---|
2829 | # ifdef IN_RING3
|
---|
2830 | rc = PGMPhysGCPhys2HCPhys(pVM, GCPhysGst, &HCPhys);
|
---|
2831 | if (VBOX_FAILURE(rc))
|
---|
2832 | {
|
---|
2833 | if (HCPhysShw != MMR3PageDummyHCPhys(pVM))
|
---|
2834 | {
|
---|
2835 | AssertMsgFailed(("Cannot find guest physical address %VGp at %VGv! PteSrc=%#RX64 PteDst=%#RX64\n",
|
---|
2836 | GCPhysGst, GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
|
---|
2837 | cErrors++;
|
---|
2838 | continue;
|
---|
2839 | }
|
---|
2840 | }
|
---|
2841 | else if (HCPhysShw != (HCPhys & SHW_PTE_PG_MASK))
|
---|
2842 | {
|
---|
2843 | AssertMsgFailed(("Out of sync (phys) at %VGv! HCPhysShw=%VHp HCPhys=%VHp GCPhysGst=%VGp PteSrc=%#RX64 PteDst=%#RX64\n",
|
---|
2844 | GCPtr + off, HCPhysShw, HCPhys, GCPhysGst, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
|
---|
2845 | cErrors++;
|
---|
2846 | continue;
|
---|
2847 | }
|
---|
2848 | # endif
|
---|
2849 |
|
---|
2850 | rc = PGMRamGCPhys2HCPhysWithFlags(pPGM, GCPhysGst, &HCPhys);
|
---|
2851 | if (VBOX_FAILURE(rc))
|
---|
2852 | {
|
---|
2853 | # ifdef IN_RING3 /** @todo make MMR3PageDummyHCPhys an 'All' function! */
|
---|
2854 | if (HCPhysShw != MMR3PageDummyHCPhys(pVM))
|
---|
2855 | {
|
---|
2856 | AssertMsgFailed(("Cannot find guest physical address %VGp at %VGv! PteSrc=%#RX64 PteDst=%#RX64\n",
|
---|
2857 | GCPhysGst, GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
|
---|
2858 | cErrors++;
|
---|
2859 | continue;
|
---|
2860 | }
|
---|
2861 | # endif
|
---|
2862 | if (PteDst.n.u1Write)
|
---|
2863 | {
|
---|
2864 | AssertMsgFailed(("Invalid guest page at %VGv is writable! GCPhysGst=%VGp PteSrc=%#RX64 PteDst=%#RX64\n",
|
---|
2865 | GCPtr + off, GCPhysGst, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
|
---|
2866 | cErrors++;
|
---|
2867 | }
|
---|
2868 | fIgnoreFlags |= X86_PTE_RW;
|
---|
2869 | }
|
---|
2870 | else if (HCPhysShw != (HCPhys & SHW_PTE_PG_MASK))
|
---|
2871 | {
|
---|
2872 | AssertMsgFailed(("Out of sync (phys) at %VGv! HCPhysShw=%VHp HCPhys=%VHp GCPhysGst=%VGp PteSrc=%#RX64 PteDst=%#RX64\n",
|
---|
2873 | GCPtr + off, HCPhysShw, HCPhys, GCPhysGst, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
|
---|
2874 | cErrors++;
|
---|
2875 | continue;
|
---|
2876 | }
|
---|
2877 |
|
---|
2878 | /* flags */
|
---|
2879 | if (HCPhys & (MM_RAM_FLAGS_PHYSICAL_ALL | MM_RAM_FLAGS_VIRTUAL_ALL | MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_VIRTUAL_WRITE))
|
---|
2880 | {
|
---|
2881 | if (HCPhys & (MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_VIRTUAL_WRITE))
|
---|
2882 | {
|
---|
2883 | if (PteDst.n.u1Write)
|
---|
2884 | {
|
---|
2885 | AssertMsgFailed(("WRITE access flagged at %VGv but the page is writable! HCPhys=%VGv PteSrc=%#RX64 PteDst=%#RX64\n",
|
---|
2886 | GCPtr + off, HCPhys, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
|
---|
2887 | cErrors++;
|
---|
2888 | continue;
|
---|
2889 | }
|
---|
2890 | fIgnoreFlags |= X86_PTE_RW;
|
---|
2891 | }
|
---|
2892 | else
|
---|
2893 | {
|
---|
2894 | if (PteDst.n.u1Present)
|
---|
2895 | {
|
---|
2896 | AssertMsgFailed(("ALL access flagged at %VGv but the page is present! HCPhys=%VHp PteSrc=%#RX64 PteDst=%#RX64\n",
|
---|
2897 | GCPtr + off, HCPhys, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
|
---|
2898 | cErrors++;
|
---|
2899 | continue;
|
---|
2900 | }
|
---|
2901 | fIgnoreFlags |= X86_PTE_P;
|
---|
2902 | }
|
---|
2903 | }
|
---|
2904 | else
|
---|
2905 | {
|
---|
2906 | if (!PteSrc.n.u1Dirty && PteSrc.n.u1Write)
|
---|
2907 | {
|
---|
2908 | if (PteDst.n.u1Write)
|
---|
2909 | {
|
---|
2910 | AssertMsgFailed(("!DIRTY page at %VGv is writable! PteSrc=%#RX64 PteDst=%#RX64\n",
|
---|
2911 | GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
|
---|
2912 | cErrors++;
|
---|
2913 | continue;
|
---|
2914 | }
|
---|
2915 | if (!(PteDst.u & PGM_PTFLAGS_TRACK_DIRTY))
|
---|
2916 | {
|
---|
2917 | AssertMsgFailed(("!DIRTY page at %VGv is not marked TRACK_DIRTY! PteSrc=%#RX64 PteDst=%#RX64\n",
|
---|
2918 | GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
|
---|
2919 | cErrors++;
|
---|
2920 | continue;
|
---|
2921 | }
|
---|
2922 | if (PteDst.n.u1Dirty)
|
---|
2923 | {
|
---|
2924 | AssertMsgFailed(("!DIRTY page at %VGv is marked DIRTY! PteSrc=%#RX64 PteDst=%#RX64\n",
|
---|
2925 | GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
|
---|
2926 | cErrors++;
|
---|
2927 | }
|
---|
2928 | # if 0 /** @todo sync access bit properly... */
|
---|
2929 | if (PteDst.n.u1Accessed != PteSrc.n.u1Accessed)
|
---|
2930 | {
|
---|
2931 | AssertMsgFailed(("!DIRTY page at %VGv is has mismatching accessed bit! PteSrc=%#RX64 PteDst=%#RX64\n",
|
---|
2932 | GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
|
---|
2933 | cErrors++;
|
---|
2934 | }
|
---|
2935 | fIgnoreFlags |= X86_PTE_RW;
|
---|
2936 | # else
|
---|
2937 | fIgnoreFlags |= X86_PTE_RW | X86_PTE_A;
|
---|
2938 | # endif
|
---|
2939 | }
|
---|
2940 | else if (PteDst.u & PGM_PTFLAGS_TRACK_DIRTY)
|
---|
2941 | {
|
---|
2942 | /* access bit emulation (not implemented). */
|
---|
2943 | if (PteSrc.n.u1Accessed || PteDst.n.u1Present)
|
---|
2944 | {
|
---|
2945 | AssertMsgFailed(("PGM_PTFLAGS_TRACK_DIRTY set at %VGv but no accessed bit emulation! PteSrc=%#RX64 PteDst=%#RX64\n",
|
---|
2946 | GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
|
---|
2947 | cErrors++;
|
---|
2948 | continue;
|
---|
2949 | }
|
---|
2950 | if (!PteDst.n.u1Accessed)
|
---|
2951 | {
|
---|
2952 | AssertMsgFailed(("!ACCESSED page at %VGv is has the accessed bit set! PteSrc=%#RX64 PteDst=%#RX64\n",
|
---|
2953 | GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
|
---|
2954 | cErrors++;
|
---|
2955 | }
|
---|
2956 | fIgnoreFlags |= X86_PTE_P;
|
---|
2957 | }
|
---|
2958 | # ifdef DEBUG_sandervl
|
---|
2959 | fIgnoreFlags |= X86_PTE_D | X86_PTE_A;
|
---|
2960 | # endif
|
---|
2961 | }
|
---|
2962 |
|
---|
2963 | if ( (PteSrc.u & ~fIgnoreFlags) != (PteDst.u & ~fIgnoreFlags)
|
---|
2964 | && (PteSrc.u & ~(fIgnoreFlags | X86_PTE_RW)) != (PteDst.u & ~fIgnoreFlags)
|
---|
2965 | )
|
---|
2966 | {
|
---|
2967 | AssertMsgFailed(("Flags mismatch at %VGv! %#RX64 != %#RX64 fIgnoreFlags=%#RX64 PteSrc=%#RX64 PteDst=%#RX64\n",
|
---|
2968 | GCPtr + off, (uint64_t)PteSrc.u & ~fIgnoreFlags, (uint64_t)PteDst.u & ~fIgnoreFlags,
|
---|
2969 | fIgnoreFlags, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
|
---|
2970 | cErrors++;
|
---|
2971 | continue;
|
---|
2972 | }
|
---|
2973 | } /* foreach PTE */
|
---|
2974 | }
|
---|
2975 | else
|
---|
2976 | {
|
---|
2977 | /*
|
---|
2978 | * Big Page.
|
---|
2979 | */
|
---|
2980 | uint64_t fIgnoreFlags = X86_PDE_AVL_MASK | X86_PDE_PAE_PG_MASK | X86_PDE4M_G | X86_PDE4M_D | X86_PDE4M_PS | X86_PDE4M_PWT | X86_PDE4M_PCD;
|
---|
2981 | if (!PdeSrc.b.u1Dirty && PdeSrc.b.u1Write)
|
---|
2982 | {
|
---|
2983 | if (PdeDst.n.u1Write)
|
---|
2984 | {
|
---|
2985 | AssertMsgFailed(("!DIRTY page at %VGv is writable! PdeSrc=%#RX64 PdeDst=%#RX64\n",
|
---|
2986 | GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
|
---|
2987 | cErrors++;
|
---|
2988 | continue;
|
---|
2989 | }
|
---|
2990 | if (!(PdeDst.u & PGM_PDFLAGS_TRACK_DIRTY))
|
---|
2991 | {
|
---|
2992 | AssertMsgFailed(("!DIRTY page at %VGv is not marked TRACK_DIRTY! PteSrc=%#RX64 PteDst=%#RX64\n",
|
---|
2993 | GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
|
---|
2994 | cErrors++;
|
---|
2995 | continue;
|
---|
2996 | }
|
---|
2997 | # if 0 /** @todo sync access bit properly... */
|
---|
2998 | if (PdeDst.n.u1Accessed != PdeSrc.b.u1Accessed)
|
---|
2999 | {
|
---|
3000 | AssertMsgFailed(("!DIRTY page at %VGv is has mismatching accessed bit! PteSrc=%#RX64 PteDst=%#RX64\n",
|
---|
3001 | GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
|
---|
3002 | cErrors++;
|
---|
3003 | }
|
---|
3004 | fIgnoreFlags |= X86_PTE_RW;
|
---|
3005 | # else
|
---|
3006 | fIgnoreFlags |= X86_PTE_RW | X86_PTE_A;
|
---|
3007 | # endif
|
---|
3008 | }
|
---|
3009 | else if (PdeDst.u & PGM_PDFLAGS_TRACK_DIRTY)
|
---|
3010 | {
|
---|
3011 | /* access bit emulation (not implemented). */
|
---|
3012 | if (PdeSrc.b.u1Accessed || PdeDst.n.u1Present)
|
---|
3013 | {
|
---|
3014 | AssertMsgFailed(("PGM_PDFLAGS_TRACK_DIRTY set at %VGv but no accessed bit emulation! PdeSrc=%#RX64 PdeDst=%#RX64\n",
|
---|
3015 | GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
|
---|
3016 | cErrors++;
|
---|
3017 | continue;
|
---|
3018 | }
|
---|
3019 | if (!PdeDst.n.u1Accessed)
|
---|
3020 | {
|
---|
3021 | AssertMsgFailed(("!ACCESSED page at %VGv is has the accessed bit set! PdeSrc=%#RX64 PdeDst=%#RX64\n",
|
---|
3022 | GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
|
---|
3023 | cErrors++;
|
---|
3024 | }
|
---|
3025 | fIgnoreFlags |= X86_PTE_P;
|
---|
3026 | }
|
---|
3027 |
|
---|
3028 | if ((PdeSrc.u & ~fIgnoreFlags) != (PdeDst.u & ~fIgnoreFlags))
|
---|
3029 | {
|
---|
3030 | AssertMsgFailed(("Flags mismatch (B) at %VGv! %#RX64 != %#RX64 fIgnoreFlags=%#RX64 PdeSrc=%#RX64 PdeDst=%#RX64\n",
|
---|
3031 | GCPtr, (uint64_t)PdeSrc.u & ~fIgnoreFlags, (uint64_t)PdeDst.u & ~fIgnoreFlags,
|
---|
3032 | fIgnoreFlags, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
|
---|
3033 | cErrors++;
|
---|
3034 | }
|
---|
3035 |
|
---|
3036 | /* iterate the page table. */
|
---|
3037 | for (unsigned iPT = 0, off = 0;
|
---|
3038 | iPT < ELEMENTS(pPTDst->a);
|
---|
3039 | iPT++, off += PAGE_SIZE, GCPhysGst += PAGE_SIZE)
|
---|
3040 | {
|
---|
3041 | const SHWPTE PteDst = pPTDst->a[iPT];
|
---|
3042 |
|
---|
3043 | if (PteDst.u & PGM_PTFLAGS_TRACK_DIRTY)
|
---|
3044 | {
|
---|
3045 | AssertMsgFailed(("The PTE at %VGv emulating a 2/4M page is marked TRACK_DIRTY! PdeSrc=%#RX64 PteDst=%#RX64\n",
|
---|
3046 | GCPtr + off, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
|
---|
3047 | cErrors++;
|
---|
3048 | }
|
---|
3049 |
|
---|
3050 | /* skip not-present entries. */
|
---|
3051 | if (!PteDst.n.u1Present) /** @todo deal with ALL handlers and CSAM !P pages! */
|
---|
3052 | continue;
|
---|
3053 |
|
---|
3054 | fIgnoreFlags = X86_PTE_PAE_PG_MASK | X86_PTE_AVL_MASK | X86_PTE_PWT | X86_PTE_PCD | X86_PTE_PAT;
|
---|
3055 |
|
---|
3056 | /* match the physical addresses */
|
---|
3057 | HCPhysShw = PteDst.u & X86_PTE_PAE_PG_MASK;
|
---|
3058 |
|
---|
3059 | # ifdef IN_RING3
|
---|
3060 | rc = PGMPhysGCPhys2HCPhys(pVM, GCPhysGst, &HCPhys);
|
---|
3061 | if (VBOX_FAILURE(rc))
|
---|
3062 | {
|
---|
3063 | if (HCPhysShw != MMR3PageDummyHCPhys(pVM))
|
---|
3064 | {
|
---|
3065 | AssertMsgFailed(("Cannot find guest physical address %VGp at %VGv! PdeSrc=%#RX64 PteDst=%#RX64\n",
|
---|
3066 | GCPhysGst, GCPtr + off, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
|
---|
3067 | cErrors++;
|
---|
3068 | }
|
---|
3069 | }
|
---|
3070 | else if (HCPhysShw != (HCPhys & X86_PTE_PAE_PG_MASK))
|
---|
3071 | {
|
---|
3072 | AssertMsgFailed(("Out of sync (phys) at %VGv! HCPhysShw=%VHp HCPhys=%VHp GCPhysGst=%VGp PdeSrc=%#RX64 PteDst=%#RX64\n",
|
---|
3073 | GCPtr + off, HCPhysShw, HCPhys, GCPhysGst, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
|
---|
3074 | cErrors++;
|
---|
3075 | continue;
|
---|
3076 | }
|
---|
3077 | # endif
|
---|
3078 |
|
---|
3079 | rc = PGMRamGCPhys2HCPhysWithFlags(pPGM, GCPhysGst, &HCPhys);
|
---|
3080 | if (VBOX_FAILURE(rc))
|
---|
3081 | {
|
---|
3082 | # ifdef IN_RING3 /** @todo make MMR3PageDummyHCPhys an 'All' function! */
|
---|
3083 | if (HCPhysShw != MMR3PageDummyHCPhys(pVM))
|
---|
3084 | {
|
---|
3085 | AssertMsgFailed(("Cannot find guest physical address %VGp at %VGv! PdeSrc=%#RX64 PteDst=%#RX64\n",
|
---|
3086 | GCPhysGst, GCPtr + off, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
|
---|
3087 | cErrors++;
|
---|
3088 | continue;
|
---|
3089 | }
|
---|
3090 | # endif
|
---|
3091 | if (PteDst.n.u1Write)
|
---|
3092 | {
|
---|
3093 | AssertMsgFailed(("Invalid guest page at %VGv is writable! GCPhysGst=%VGp PdeSrc=%#RX64 PteDst=%#RX64\n",
|
---|
3094 | GCPtr + off, GCPhysGst, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
|
---|
3095 | cErrors++;
|
---|
3096 | }
|
---|
3097 | fIgnoreFlags |= X86_PTE_RW;
|
---|
3098 | }
|
---|
3099 | else if (HCPhysShw != (HCPhys & X86_PTE_PAE_PG_MASK))
|
---|
3100 | {
|
---|
3101 | AssertMsgFailed(("Out of sync (phys) at %VGv! HCPhysShw=%VHp HCPhys=%VHp GCPhysGst=%VGp PdeSrc=%#RX64 PteDst=%#RX64\n",
|
---|
3102 | GCPtr + off, HCPhysShw, HCPhys, GCPhysGst, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
|
---|
3103 | cErrors++;
|
---|
3104 | continue;
|
---|
3105 | }
|
---|
3106 |
|
---|
3107 | /* flags */
|
---|
3108 | if (HCPhys & (MM_RAM_FLAGS_PHYSICAL_ALL | MM_RAM_FLAGS_VIRTUAL_ALL | MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_VIRTUAL_WRITE))
|
---|
3109 | {
|
---|
3110 | if (HCPhys & (MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_VIRTUAL_WRITE))
|
---|
3111 | {
|
---|
3112 | if (!(HCPhys & MM_RAM_FLAGS_PHYSICAL_TEMP_OFF))
|
---|
3113 | {
|
---|
3114 | if (PteDst.n.u1Write)
|
---|
3115 | {
|
---|
3116 | AssertMsgFailed(("WRITE access flagged at %VGv but the page is writable! HCPhys=%VGv PdeSrc=%#RX64 PteDst=%#RX64\n",
|
---|
3117 | GCPtr + off, HCPhys, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
|
---|
3118 | cErrors++;
|
---|
3119 | continue;
|
---|
3120 | }
|
---|
3121 | fIgnoreFlags |= X86_PTE_RW;
|
---|
3122 | }
|
---|
3123 | }
|
---|
3124 | else
|
---|
3125 | {
|
---|
3126 | if (PteDst.n.u1Present)
|
---|
3127 | {
|
---|
3128 | AssertMsgFailed(("ALL access flagged at %VGv but the page is present! HCPhys=%VGv PdeSrc=%#RX64 PteDst=%#RX64\n",
|
---|
3129 | GCPtr + off, HCPhys, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
|
---|
3130 | cErrors++;
|
---|
3131 | continue;
|
---|
3132 | }
|
---|
3133 | fIgnoreFlags |= X86_PTE_P;
|
---|
3134 | }
|
---|
3135 | }
|
---|
3136 |
|
---|
3137 | if ( (PdeSrc.u & ~fIgnoreFlags) != (PteDst.u & ~fIgnoreFlags)
|
---|
3138 | && (PdeSrc.u & ~(fIgnoreFlags | X86_PTE_RW)) != (PteDst.u & ~fIgnoreFlags) /* lazy phys handler dereg. */
|
---|
3139 | )
|
---|
3140 | {
|
---|
3141 | AssertMsgFailed(("Flags mismatch (BT) at %VGv! %#RX64 != %#RX64 fIgnoreFlags=%#RX64 PdeSrc=%#RX64 PteDst=%#RX64\n",
|
---|
3142 | GCPtr + off, (uint64_t)PdeSrc.u & ~fIgnoreFlags, (uint64_t)PteDst.u & ~fIgnoreFlags,
|
---|
3143 | fIgnoreFlags, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
|
---|
3144 | cErrors++;
|
---|
3145 | continue;
|
---|
3146 | }
|
---|
3147 | } /* foreach PTE */
|
---|
3148 | }
|
---|
3149 | }
|
---|
3150 | /* not present */
|
---|
3151 |
|
---|
3152 | } /* forearch PDE */
|
---|
3153 |
|
---|
3154 | # ifdef DEBUG
|
---|
3155 | if (cErrors)
|
---|
3156 | LogFlow(("AssertCR3: cErrors=%d\n", cErrors));
|
---|
3157 | # endif
|
---|
3158 |
|
---|
3159 | #elif PGM_GST_TYPE == PGM_TYPE_PAE
|
---|
3160 | //# error not implemented
|
---|
3161 |
|
---|
3162 |
|
---|
3163 | #elif PGM_GST_TYPE == PGM_TYPE_AMD64
|
---|
3164 | //# error not implemented
|
---|
3165 |
|
---|
3166 | /*#else: guest real and protected mode */
|
---|
3167 | #endif
|
---|
3168 | return cErrors;
|
---|
3169 | }
|
---|
3170 | #endif /* VBOX_STRICT */
|
---|
3171 |
|
---|