VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/PGMAllBth.h@ 1136

Last change on this file since 1136 was 1090, checked in by vboxsync, 18 years ago

Corrected assertions for v86 mode

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 137.5 KB
Line 
1/* $Id: PGMAllBth.h 1090 2007-02-28 08:44:17Z 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
28PGM_BTH_DECL(int, Trap0eHandler)(PVM pVM, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault);
29PGM_BTH_DECL(int, InvalidatePage)(PVM pVM, RTGCUINTPTR GCPtrPage);
30PGM_BTH_DECL(int, SyncPage)(PVM pVM, VBOXPDE PdeSrc, RTGCUINTPTR GCPtrPage, unsigned cPages, unsigned uErr);
31PGM_BTH_DECL(int, CheckPageFault)(PVM pVM, uint32_t uErr, PSHWPDE pPdeDst, PVBOXPDE pPdeSrc, RTGCUINTPTR GCPtrPage);
32PGM_BTH_DECL(int, SyncPT)(PVM pVM, unsigned iPD, PVBOXPD pPDSrc, RTGCUINTPTR GCPtrPage);
33PGM_BTH_DECL(int, VerifyAccessSyncPage)(PVM pVM, RTGCUINTPTR Addr, unsigned fPage, unsigned uErr);
34PGM_BTH_DECL(int, PrefetchPage)(PVM pVM, RTGCUINTPTR GCPtrPage);
35PGM_BTH_DECL(int, SyncCR3)(PVM pVM, uint32_t cr0, uint32_t cr3, uint32_t cr4, bool fGlobal);
36#ifdef VBOX_STRICT
37PGM_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
40DECLINLINE(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 */
54PGM_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, sizeof(*pPTSrc));
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 || pRegFrame->eflags.Bits.u1VM);
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 */
760PGM_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 */
1003DECLINLINE(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 */
1053DECLINLINE(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 */
1105DECLINLINE(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 */
1232PGM_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 */
1475PGM_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 */
1687PGM_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
1957 /* Make sure the RAM has already been allocated. */
1958 if (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC)
1959 {
1960 if (RT_UNLIKELY(!(pRam->aHCPhys[iHCPage] & X86_PTE_PAE_PG_MASK)))
1961 {
1962#ifdef IN_RING3
1963 int rc = pgmr3PhysGrowRange(pVM, GCPhys);
1964#else
1965 int rc = CTXALLMID(VMM, CallHost)(pVM, VMMCALLHOST_PGM_RAM_GROW_RANGE, GCPhys);
1966#endif
1967 if (rc != VINF_SUCCESS)
1968 return rc;
1969
1970 HCPhys = pRam->aHCPhys[iHCPage];
1971 }
1972 }
1973
1974 if (HCPhys & (MM_RAM_FLAGS_PHYSICAL_ALL | MM_RAM_FLAGS_VIRTUAL_ALL | MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_VIRTUAL_WRITE))
1975 {
1976 if (!(HCPhys & (MM_RAM_FLAGS_PHYSICAL_ALL | MM_RAM_FLAGS_VIRTUAL_ALL)))
1977 {
1978 PteDst.u = (HCPhys & X86_PTE_PAE_PG_MASK) | PteDstBase.u;
1979 PteDst.n.u1Write = 0;
1980 }
1981 else
1982 PteDst.u = 0;
1983 }
1984#ifndef IN_RING0
1985 /*
1986 * Assuming kernel code will be marked as supervisor and not as user level and executed
1987 * using a conforming code selector. Don't check for readonly, as that implies the whole
1988 * 4MB can be code or readonly data. Linux enables write access for its large pages.
1989 */
1990 else if ( !PdeSrc.n.u1User
1991 && CSAMIsEnabled(pVM)
1992 && CSAMDoesPageNeedScanning(pVM, (RTGCPTR)(GCPtr | (iPTDst << SHW_PT_SHIFT))))
1993 PteDst.u = 0;
1994#endif
1995 else
1996 PteDst.u = (HCPhys & X86_PTE_PAE_PG_MASK) | PteDstBase.u;
1997# ifdef PGMPOOL_WITH_USER_TRACKING
1998 if (PteDst.n.u1Present)
1999 PGM_BTH_NAME(SyncPageWorkerTrackAddref)(pVM, pShwPage, HCPhys >> MM_RAM_FLAGS_IDX_SHIFT, &pRam->aHCPhys[iHCPage], iPTDst);
2000# endif
2001 /* commit it */
2002 pPTDst->a[iPTDst] = PteDst;
2003 Log4(("SyncPT: BIG %VGv PteDst:{P=%d RW=%d U=%d raw=%08llx}%s\n",
2004 (RTGCPTR)(GCPtr | (iPTDst << SHW_PT_SHIFT)), PteDst.n.u1Present, PteDst.n.u1Write, PteDst.n.u1User, (uint64_t)PteDst.u,
2005 PteDst.u & PGM_PTFLAGS_TRACK_DIRTY ? " Track-Dirty" : ""));
2006
2007 /* advance */
2008 GCPhys += PAGE_SIZE;
2009 iHCPage++;
2010 iPTDst++;
2011 } while ( iPTDst < ELEMENTS(pPTDst->a)
2012 && GCPhys <= pRam->GCPhysLast);
2013 }
2014 else if (pRam)
2015 {
2016 Log(("Invalid pages at %VGp\n", GCPhys));
2017 do
2018 {
2019 pPTDst->a[iPTDst].u = 0; /* MMIO or invalid page, we must handle them manually. */
2020 GCPhys += PAGE_SIZE;
2021 iPTDst++;
2022 } while ( iPTDst < ELEMENTS(pPTDst->a)
2023 && GCPhys < pRam->GCPhys);
2024 }
2025 else
2026 {
2027 Log(("Invalid pages at %VGp (2)\n", GCPhys));
2028 for ( ; iPTDst < ELEMENTS(pPTDst->a); iPTDst++)
2029 pPTDst->a[iPTDst].u = 0; /* MMIO or invalid page, we must handle them manually. */
2030 }
2031 } /* while more PTEs */
2032 } /* 4KB / 4MB */
2033 }
2034 else
2035 AssertRelease(!PdeDst.n.u1Present);
2036
2037 STAM_PROFILE_STOP(&pVM->pgm.s.CTXMID(Stat,SyncPT), a);
2038# ifdef IN_GC
2039 if (VBOX_FAILURE(rc))
2040 STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,SyncPTFailed));
2041# endif
2042 return rc;
2043
2044#else /* PGM_GST_TYPE != PGM_TYPE_32BIT */
2045
2046 AssertReleaseMsgFailed(("Shw=%d Gst=%d is not implemented!\n", PGM_GST_TYPE, PGM_SHW_TYPE));
2047 STAM_PROFILE_STOP(&pVM->pgm.s.CTXMID(Stat,SyncPT), a);
2048 return VERR_INTERNAL_ERROR;
2049#endif /* PGM_GST_TYPE != PGM_TYPE_32BIT */
2050}
2051
2052
2053
2054/**
2055 * Prefetch a page/set of pages.
2056 *
2057 * Typically used to sync commonly used pages before entering raw mode
2058 * after a CR3 reload.
2059 *
2060 * @returns VBox status code.
2061 * @param pVM VM handle.
2062 * @param GCPtrPage Page to invalidate.
2063 */
2064PGM_BTH_DECL(int, PrefetchPage)(PVM pVM, RTGCUINTPTR GCPtrPage)
2065{
2066#if PGM_GST_TYPE == PGM_TYPE_32BIT
2067
2068# if PGM_SHW_TYPE != PGM_TYPE_32BIT && PGM_SHW_TYPE != PGM_TYPE_PAE
2069# error "Invalid shadow mode for 32-bit guest mode!"
2070# endif
2071
2072 /*
2073 * Check that all Guest levels thru the PDE are present, getting the
2074 * PD and PDE in the processes.
2075 */
2076 int rc = VINF_SUCCESS;
2077 PVBOXPD pPDSrc = CTXSUFF(pVM->pgm.s.pGuestPD);
2078 const unsigned iPDSrc = GCPtrPage >> GST_PD_SHIFT;
2079 const VBOXPDE PdeSrc = pPDSrc->a[iPDSrc];
2080# ifdef PGM_SYNC_ACCESSED_BIT
2081 if (PdeSrc.n.u1Present && PdeSrc.n.u1Accessed)
2082# else
2083 if (PdeSrc.n.u1Present)
2084# endif
2085 {
2086# if PGM_SHW_TYPE == PGM_TYPE_32BIT
2087 const X86PDE PdeDst = pVM->pgm.s.CTXMID(p,32BitPD)->a[GCPtrPage >> X86_PD_SHIFT];
2088# else
2089 const X86PDEPAE PdeDst = pVM->pgm.s.CTXMID(ap,PaePDs)[0]->a[GCPtrPage >> X86_PD_PAE_SHIFT];
2090# endif
2091 if (!(PdeDst.u & PGM_PDFLAGS_MAPPING))
2092 {
2093 if (!PdeDst.n.u1Present)
2094 /** r=bird: This guy will set the A bit on the PDE, probably harmless. */
2095 rc = PGM_BTH_NAME(SyncPT)(pVM, iPDSrc, pPDSrc, GCPtrPage);
2096 else
2097 {
2098 /** @note We used to sync PGM_SYNC_NR_PAGES pages, which triggered assertions in CSAM, because
2099 * R/W attributes of nearby pages were reset. Not sure how that could happen. Anyway, it
2100 * makes no sense to prefetch more than one page.
2101 */
2102 rc = PGM_BTH_NAME(SyncPage)(pVM, PdeSrc, GCPtrPage, 1, 0);
2103 if (VBOX_SUCCESS(rc))
2104 rc = VINF_SUCCESS;
2105 }
2106 }
2107 }
2108 return rc;
2109
2110#else /* PGM_GST_TYPE != PGM_TYPE_32BIT */
2111
2112 AssertReleaseMsgFailed(("Shw=%d Gst=%d is not implemented!\n", PGM_SHW_TYPE, PGM_GST_TYPE));
2113 return VERR_INTERNAL_ERROR;
2114#endif /* PGM_GST_TYPE != PGM_TYPE_32BIT */
2115}
2116
2117
2118
2119
2120/**
2121 * Syncs a page during a PGMVerifyAccess() call.
2122 *
2123 * @returns VBox status code (informational included).
2124 * @param GCPtrPage The address of the page to sync.
2125 * @param fPage The effective guest page flags.
2126 * @param uErr The trap error code.
2127 */
2128PGM_BTH_DECL(int, VerifyAccessSyncPage)(PVM pVM, RTGCUINTPTR GCPtrPage, unsigned fPage, unsigned uErr)
2129{
2130 LogFlow(("VerifyAccessSyncPage: GCPtrPage=%VGv fPage=%#x uErr=%#x\n", GCPtrPage, fPage, uErr));
2131
2132#if PGM_GST_TYPE == PGM_TYPE_32BIT
2133
2134# if PGM_SHW_TYPE != PGM_TYPE_32BIT && PGM_SHW_TYPE != PGM_TYPE_PAE
2135# error "Invalid shadow mode for 32-bit guest mode!"
2136# endif
2137
2138#ifndef IN_RING0
2139 if (!(fPage & X86_PTE_US))
2140 {
2141 /*
2142 * Mark this page as safe.
2143 */
2144 /** @todo not correct for pages that contain both code and data!! */
2145 Log(("CSAMMarkPage %VGv; scanned=%d\n", GCPtrPage, true));
2146 CSAMMarkPage(pVM, (RTGCPTR)GCPtrPage, true);
2147 }
2148#endif
2149 /*
2150 * Get guest PD and index.
2151 */
2152 unsigned iPDSrc = GCPtrPage >> GST_PD_SHIFT;
2153 PVBOXPD pPDSrc = CTXSUFF(pVM->pgm.s.pGuestPD);
2154 int rc = VINF_SUCCESS;
2155
2156# ifdef PGM_SYNC_DIRTY_BIT
2157 /*
2158 * First check if the page fault was caused by dirty bit tracking
2159 */
2160# if PGM_SHW_TYPE == PGM_TYPE_32BIT
2161 PX86PDE pPdeDst = &pVM->pgm.s.CTXMID(p,32BitPD)->a[GCPtrPage >> X86_PD_SHIFT];
2162# else
2163 PX86PDEPAE pPdeDst = &pVM->pgm.s.CTXMID(ap,PaePDs)[0]->a[GCPtrPage >> X86_PD_PAE_SHIFT];
2164# endif
2165 rc = PGM_BTH_NAME(CheckPageFault)(pVM, uErr, pPdeDst, &pPDSrc->a[iPDSrc], GCPtrPage);
2166 if (rc == VINF_PGM_HANDLED_DIRTY_BIT_FAULT)
2167 Log(("PGMVerifyAccess: success (dirty)\n"));
2168 else
2169# endif /* PGM_SYNC_DIRTY_BIT */
2170 {
2171 Assert(rc != VINF_EM_RAW_GUEST_TRAP);
2172 if (uErr & X86_TRAP_PF_US)
2173 STAM_COUNTER_INC(&pVM->pgm.s.StatGCPageOutOfSyncUser);
2174 else /* supervisor */
2175 STAM_COUNTER_INC(&pVM->pgm.s.StatGCPageOutOfSyncSupervisor);
2176
2177 rc = PGM_BTH_NAME(SyncPage)(pVM, pPDSrc->a[iPDSrc], GCPtrPage, 1, 0);
2178 if (VBOX_SUCCESS(rc))
2179 {
2180 /* Page was successfully synced */
2181 Log(("PGMVerifyAccess: success (sync)\n"));
2182 rc = VINF_SUCCESS;
2183 }
2184 else
2185 {
2186 Log(("PGMVerifyAccess: access violation for %VGv rc=%d\n", GCPtrPage, rc));
2187 return VINF_EM_RAW_GUEST_TRAP;
2188 }
2189 }
2190 return rc;
2191
2192#else /* PGM_GST_TYPE != PGM_TYPE_32BIT */
2193
2194 AssertReleaseMsgFailed(("Shw=%d Gst=%d is not implemented!\n", PGM_GST_TYPE, PGM_SHW_TYPE));
2195 return VERR_INTERNAL_ERROR;
2196#endif /* PGM_GST_TYPE != PGM_TYPE_32BIT */
2197}
2198
2199
2200#if PGM_GST_TYPE == PGM_TYPE_32BIT
2201# if PGM_SHW_TYPE == PGM_TYPE_32BIT || PGM_SHW_TYPE == PGM_TYPE_PAE
2202/**
2203 * Figures out which kind of shadow page this guest PDE warrants.
2204 *
2205 * @returns Shadow page kind.
2206 * @param pPdeSrc The guest PDE in question.
2207 * @param cr4 The current guest cr4 value.
2208 */
2209DECLINLINE(PGMPOOLKIND) PGM_BTH_NAME(CalcPageKind)(const VBOXPDE *pPdeSrc, uint32_t cr4)
2210{
2211 if (!pPdeSrc->n.u1Size || !(cr4 & X86_CR4_PSE))
2212 return BTH_PGMPOOLKIND_PT_FOR_PT;
2213 //switch (pPdeSrc->u & (X86_PDE4M_RW | X86_PDE4M_US /*| X86_PDE4M_PAE_NX*/))
2214 //{
2215 // case 0:
2216 // return BTH_PGMPOOLKIND_PT_FOR_BIG_RO;
2217 // case X86_PDE4M_RW:
2218 // return BTH_PGMPOOLKIND_PT_FOR_BIG_RW;
2219 // case X86_PDE4M_US:
2220 // return BTH_PGMPOOLKIND_PT_FOR_BIG_US;
2221 // case X86_PDE4M_RW | X86_PDE4M_US:
2222 // return BTH_PGMPOOLKIND_PT_FOR_BIG_RW_US;
2223# if 0
2224 // case X86_PDE4M_PAE_NX:
2225 // return BTH_PGMPOOLKIND_PT_FOR_BIG_NX;
2226 // case X86_PDE4M_RW | X86_PDE4M_PAE_NX:
2227 // return BTH_PGMPOOLKIND_PT_FOR_BIG_RW_NX;
2228 // case X86_PDE4M_US | X86_PDE4M_PAE_NX:
2229 // return BTH_PGMPOOLKIND_PT_FOR_BIG_US_NX;
2230 // case X86_PDE4M_RW | X86_PDE4M_US | X86_PDE4M_PAE_NX:
2231 // return BTH_PGMPOOLKIND_PT_FOR_BIG_RW_US_NX;
2232# endif
2233 return BTH_PGMPOOLKIND_PT_FOR_BIG;
2234 //}
2235}
2236# endif
2237#endif
2238
2239#undef MY_STAM_COUNTER_INC
2240#define MY_STAM_COUNTER_INC(a) do { } while (0)
2241
2242
2243/**
2244 * Syncs the paging hierarchy starting at CR3.
2245 *
2246 * @returns VBox status code, no specials.
2247 * @param pVM The virtual machine.
2248 * @param cr0 Guest context CR0 register
2249 * @param cr3 Guest context CR3 register
2250 * @param cr4 Guest context CR4 register
2251 * @param fGlobal Including global page directories or not
2252 */
2253PGM_BTH_DECL(int, SyncCR3)(PVM pVM, uint32_t cr0, uint32_t cr3, uint32_t cr4, bool fGlobal)
2254{
2255#if PGM_GST_TYPE == PGM_TYPE_32BIT
2256# if PGM_SHW_TYPE == PGM_TYPE_32BIT || PGM_SHW_TYPE == PGM_TYPE_PAE
2257 /*
2258 * Inform the PGM PD Cache Manager about the pending sync.
2259 */
2260 if (fGlobal || VM_FF_ISSET(pVM, VM_FF_PGM_SYNC_CR3))
2261 {
2262# if 0 /** @todo what the heck is this about? */
2263 /* Don't cause an additional global CR3 reload the next time (the flag is cleared in PGMSyncCR3). */
2264 VM_FF_CLEAR(pVM, VM_FF_PGM_SYNC_CR3);
2265# endif
2266
2267 /* Change this CR3 reload to be a global one. */
2268 fGlobal = true;
2269 }
2270# endif
2271#endif
2272
2273 /*
2274 * Update page access handlers.
2275 * The virtual are always flushed, while the physical are only on demand.
2276 * WARNING: We are incorrectly not doing global flushing on Virtual Handler updates. We'll
2277 * have to look into that later because it will have a bad influence on the performance.
2278 * @note SvL: There's no need for that. Just invalidate the virtual range(s).
2279 * bird: Yes, but that won't work for aliases.
2280 */
2281 /** @todo this MUST go away. See #1557. */
2282 STAM_PROFILE_START(&pVM->pgm.s.CTXMID(Stat,SyncCR3Handlers), h);
2283 PGM_GST_NAME(HandlerVirtualUpdate)(pVM, cr4);
2284 STAM_PROFILE_STOP(&pVM->pgm.s.CTXMID(Stat,SyncCR3Handlers), h);
2285
2286#ifdef PGMPOOL_WITH_MONITORING
2287 /*
2288 * When monitoring shadowed pages, we reset the modification counters on CR3 sync.
2289 * Occationally we will have to clear all the shadow page tables because we wanted
2290 * to monitor a page which was mapped by too many shadowed page tables. This operation
2291 * sometimes refered to as a 'lightweight flush'.
2292 */
2293 if (!(pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL))
2294 pgmPoolMonitorModifiedClearAll(pVM);
2295 else
2296 {
2297# ifdef IN_RING3
2298 pVM->pgm.s.fSyncFlags &= ~PGM_SYNC_CLEAR_PGM_POOL;
2299 pgmPoolClearAll(pVM);
2300# else
2301 LogFlow(("SyncCR3: PGM_SYNC_CLEAR_PGM_POOL is set -> VINF_PGM_SYNC_CR3\n"));
2302 return VINF_PGM_SYNC_CR3;
2303# endif
2304 }
2305#endif
2306
2307 Assert(fGlobal || (cr4 & X86_CR4_PGE));
2308 MY_STAM_COUNTER_INC(fGlobal ? &pVM->pgm.s.CTXMID(Stat,SyncCR3Global) : &pVM->pgm.s.CTXMID(Stat,SyncCR3NotGlobal));
2309
2310#if PGM_GST_TYPE == PGM_TYPE_32BIT
2311# if PGM_SHW_TYPE == PGM_TYPE_32BIT || PGM_SHW_TYPE == PGM_TYPE_PAE
2312 /*
2313 * Get page directory addresses.
2314 */
2315# if PGM_SHW_TYPE == PGM_TYPE_32BIT
2316 PX86PDE pPDEDst = &pVM->pgm.s.CTXMID(p,32BitPD)->a[0];
2317# else
2318 PX86PDEPAE pPDEDst = &pVM->pgm.s.CTXMID(ap,PaePDs)[0]->a[0];
2319# endif
2320 PVBOXPD pPDSrc = pVM->pgm.s.CTXSUFF(pGuestPD);
2321
2322 Assert(pPDSrc);
2323#ifndef IN_GC
2324 Assert(MMPhysGCPhys2HCVirt(pVM, (RTGCPHYS)(cr3 & X86_CR3_PAGE_MASK), sizeof(*pPDSrc)) == pPDSrc);
2325#endif
2326
2327 /*
2328 * Iterate the page directory.
2329 */
2330 PPGMMAPPING pMapping;
2331 unsigned iPdNoMapping;
2332 const bool fRawR0Enabled = EMIsRawRing0Enabled(pVM);
2333 PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
2334
2335 /* Only check mappings if they are supposed to be put into the shadow page table. */
2336 if (pgmMapAreMappingsEnabled(&pVM->pgm.s))
2337 {
2338 pMapping = pVM->pgm.s.CTXSUFF(pMappings);
2339 iPdNoMapping = (pMapping) ? pMapping->GCPtr >> PGDIR_SHIFT : ~0U;
2340 }
2341 else
2342 {
2343 pMapping = 0;
2344 iPdNoMapping = ~0U;
2345 }
2346
2347 for (unsigned iPD = 0; iPD < ELEMENTS(pPDSrc->a); iPD++)
2348 {
2349# if PGM_SHW_TYPE == PGM_TYPE_32BIT
2350 Assert(&pVM->pgm.s.CTXMID(p,32BitPD)->a[iPD] == pPDEDst);
2351# else
2352 Assert(&pVM->pgm.s.CTXMID(ap,PaePDs)[iPD * 2 / 512]->a[iPD * 2 % 512] == pPDEDst);
2353# endif
2354 register VBOXPDE PdeSrc = pPDSrc->a[iPD];
2355 if ( PdeSrc.n.u1Present
2356 && (PdeSrc.n.u1User || fRawR0Enabled))
2357 {
2358 /*
2359 * Check for conflicts with GC mappings.
2360 */
2361 if (iPD == iPdNoMapping)
2362 {
2363 if (pVM->pgm.s.fMappingsFixed)
2364 {
2365 /* It's fixed, just skip the mapping. */
2366 const unsigned cPTs = pMapping->cPTs;
2367 iPD += cPTs - 1;
2368 pPDEDst += cPTs + (PGM_SHW_TYPE != PGM_TYPE_32BIT) * cPTs;
2369 pMapping = pMapping->CTXSUFF(pNext);
2370 iPdNoMapping = pMapping ? pMapping->GCPtr >> PGDIR_SHIFT : ~0U;
2371 continue;
2372 }
2373
2374#ifdef IN_RING3
2375 int rc = pgmR3SyncPTResolveConflict(pVM, pMapping, pPDSrc, iPD);
2376 if (VBOX_FAILURE(rc))
2377 return rc;
2378
2379 /*
2380 * Update iPdNoMapping and pMapping.
2381 */
2382 pMapping = pVM->pgm.s.pMappingsHC;
2383 while (pMapping && pMapping->GCPtr < (iPD << PGDIR_SHIFT))
2384 pMapping = pMapping->pNextHC;
2385 iPdNoMapping = pMapping ? pMapping->GCPtr >> PGDIR_SHIFT : ~0U;
2386#else
2387 LogFlow(("SyncCR3: detected conflict -> VINF_PGM_SYNC_CR3\n"));
2388 return VINF_PGM_SYNC_CR3;
2389#endif
2390 }
2391
2392 /*
2393 * Sync page directory entry.
2394 *
2395 * The current approach is to allocated the page table but to set
2396 * the entry to not-present and postpone the page table synching till
2397 * it's actually used.
2398 */
2399# if PGM_SHW_TYPE == PGM_TYPE_32BIT
2400 const unsigned iPdShw = iPD; NOREF(iPdShw);
2401# else
2402 for (unsigned i = 0, iPdShw = iPD * 2; i < 2; i++, iPdShw++) /* pray that the compiler unrolls this */
2403# endif
2404 {
2405 SHWPDE PdeDst = *pPDEDst;
2406 if (PdeDst.n.u1Present)
2407 {
2408 PPGMPOOLPAGE pShwPage = pgmPoolGetPage(pPool, PdeDst.u & SHW_PDE_PG_MASK);
2409 RTGCPHYS GCPhys;
2410 if ( !PdeSrc.b.u1Size
2411 || !(cr4 & X86_CR4_PSE))
2412 {
2413 GCPhys = PdeSrc.u & GST_PDE_PG_MASK;
2414# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
2415 GCPhys |= i * (PAGE_SIZE / 2);
2416# endif
2417 }
2418 else
2419 {
2420 GCPhys = PdeSrc.u & GST_PDE4M_PG_MASK;
2421# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
2422 GCPhys |= i * X86_PAGE_2M_SIZE;
2423# endif
2424 }
2425
2426 if ( pShwPage->GCPhys == GCPhys
2427 && pShwPage->enmKind == PGM_BTH_NAME(CalcPageKind)(&PdeSrc, cr4)
2428 && ( pShwPage->fCached
2429 || ( !fGlobal
2430 && ( false
2431# ifdef PGM_SKIP_GLOBAL_PAGEDIRS_ON_NONGLOBAL_FLUSH
2432 || ( (PdeSrc.u & (X86_PDE4M_PS | X86_PDE4M_G)) == (X86_PDE4M_PS | X86_PDE4M_G)
2433 && (cr4 & (X86_CR4_PGE | X86_CR4_PSE)) == (X86_CR4_PGE | X86_CR4_PSE)) /* global 2/4MB page. */
2434 || ( !pShwPage->fSeenNonGlobal
2435 && (cr4 & X86_CR4_PGE))
2436# endif
2437 )
2438 )
2439 )
2440 && ( (PdeSrc.u & (X86_PDE_US | X86_PDE_RW)) == (PdeDst.u & (X86_PDE_US | X86_PDE_RW))
2441 || ( (cr4 & X86_CR4_PSE)
2442 && ((PdeSrc.u & (X86_PDE_US | X86_PDE4M_PS | X86_PDE4M_D)) | PGM_PDFLAGS_TRACK_DIRTY)
2443 == ((PdeDst.u & (X86_PDE_US | X86_PDE_RW | PGM_PDFLAGS_TRACK_DIRTY)) | X86_PDE4M_PS))
2444 )
2445 )
2446 {
2447# ifdef VBOX_WITH_STATISTICS
2448 if ( !fGlobal
2449 && (PdeSrc.u & (X86_PDE4M_PS | X86_PDE4M_G)) == (X86_PDE4M_PS | X86_PDE4M_G)
2450 && (cr4 & (X86_CR4_PGE | X86_CR4_PSE)) == (X86_CR4_PGE | X86_CR4_PSE))
2451 MY_STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,SyncCR3DstSkippedGlobalPD));
2452 else if (!fGlobal && !pShwPage->fSeenNonGlobal && (cr4 & X86_CR4_PGE))
2453 MY_STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,SyncCR3DstSkippedGlobalPT));
2454 else
2455 MY_STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,SyncCR3DstCacheHit));
2456# endif /* VBOX_WITH_STATISTICS */
2457/** @todo a replacement strategy isn't really needed unless we're using a very small pool < 512 pages.
2458 * The whole ageing stuff should be put in yet another set of #ifdefs. For now, let's just skip it. */
2459//# ifdef PGMPOOL_WITH_CACHE
2460// pgmPoolCacheUsed(pPool, pShwPage);
2461//# endif
2462 }
2463 else
2464 {
2465 pgmPoolFreeByPage(pPool, pShwPage, SHW_POOL_ROOT_IDX, iPdShw);
2466 pPDEDst->u = 0;
2467 MY_STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,SyncCR3DstFreed));
2468 }
2469 }
2470 else
2471 MY_STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,SyncCR3DstNotPresent));
2472 pPDEDst++;
2473 }
2474 }
2475 else if (iPD != iPdNoMapping)
2476 {
2477 /*
2478 * Check if there is any page directory to mark not present here.
2479 */
2480# if PGM_SHW_TYPE == PGM_TYPE_32BIT
2481 const unsigned iPdShw = iPD; NOREF(iPdShw);
2482# else
2483 for (unsigned i = 0, iPdShw = iPD * 2; i < 2; i++, iPdShw++) /* pray that the compiler unrolls this */
2484# endif
2485 {
2486 if (pPDEDst->n.u1Present)
2487 {
2488 pgmPoolFreeByPage(pPool, pgmPoolGetPage(pPool, pPDEDst->u & SHW_PDE_PG_MASK), SHW_POOL_ROOT_IDX, iPdShw);
2489 pPDEDst->u = 0;
2490 MY_STAM_COUNTER_INC(&pVM->pgm.s.CTXMID(Stat,SyncCR3DstFreedSrcNP));
2491 }
2492 pPDEDst++;
2493 }
2494 }
2495 else
2496 {
2497 Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
2498 const unsigned cPTs = pMapping->cPTs;
2499 if (pVM->pgm.s.fMappingsFixed)
2500 {
2501 /* It's fixed, just skip the mapping. */
2502 pMapping = pMapping->CTXSUFF(pNext);
2503 iPdNoMapping = pMapping ? pMapping->GCPtr >> PGDIR_SHIFT : ~0U;
2504 }
2505 else
2506 {
2507 /*
2508 * Check for conflicts for subsequent pagetables
2509 * and advance to the next mapping.
2510 */
2511 iPdNoMapping = ~0U;
2512 unsigned iPT = cPTs;
2513 while (iPT-- > 1)
2514 {
2515 if ( pPDSrc->a[iPD + iPT].n.u1Present
2516 && (pPDSrc->a[iPD + iPT].n.u1User || fRawR0Enabled))
2517 {
2518# ifdef IN_RING3
2519 int rc = pgmR3SyncPTResolveConflict(pVM, pMapping, pPDSrc, iPD);
2520 if (VBOX_FAILURE(rc))
2521 return rc;
2522
2523 /*
2524 * Update iPdNoMapping and pMapping.
2525 */
2526 pMapping = pVM->pgm.s.CTXSUFF(pMappings);
2527 while (pMapping && pMapping->GCPtr < (iPD << PGDIR_SHIFT))
2528 pMapping = pMapping->CTXSUFF(pNext);
2529 iPdNoMapping = pMapping ? pMapping->GCPtr >> PGDIR_SHIFT : ~0U;
2530 break;
2531# else
2532 LogFlow(("SyncCR3: detected conflict -> VINF_PGM_SYNC_CR3\n"));
2533 return VINF_PGM_SYNC_CR3;
2534# endif
2535 }
2536 }
2537 if (iPdNoMapping == ~0U && pMapping)
2538 {
2539 pMapping = pMapping->CTXSUFF(pNext);
2540 if (pMapping)
2541 iPdNoMapping = pMapping->GCPtr >> PGDIR_SHIFT;
2542 }
2543 }
2544 /* advance. */
2545 iPD += cPTs - 1;
2546 pPDEDst += cPTs + (PGM_SHW_TYPE != PGM_TYPE_32BIT) * cPTs;
2547 }
2548
2549 } /* for iPD */
2550# elif PGM_SHW_TYPE == PGM_TYPE_AMD64
2551# error "Guest 32-bit mode and shadow AMD64 mode doesn't add up!"
2552# endif
2553
2554 return VINF_SUCCESS;
2555
2556#elif PGM_GST_TYPE == PGM_TYPE_PAE
2557# if PGM_SHW_TYPE == PGM_TYPE_PAE
2558//# error not implemented
2559 return VERR_INTERNAL_ERROR;
2560
2561# else /* PGM_SHW_TYPE != PGM_TYPE_AMD64 */
2562# error "Guest PAE mode, but not the shadow mode ; 32bit - maybe, but amd64 no."
2563# endif /* PGM_SHW_TYPE != PGM_TYPE_AMD64 */
2564
2565#elif PGM_GST_TYPE == PGM_TYPE_AMD64
2566# if PGM_SHW_TYPE == PGM_TYPE_AMD64
2567//# error not implemented
2568 return VERR_INTERNAL_ERROR;
2569
2570# else /* PGM_SHW_TYPE != PGM_TYPE_AMD64 */
2571# error "Guest AMD64 mode, but not the shadow mode - that can't be right!"
2572# endif /* PGM_SHW_TYPE != PGM_TYPE_AMD64 */
2573
2574#else /* guest real and protected mode */
2575
2576 return VINF_SUCCESS;
2577#endif
2578}
2579
2580
2581
2582
2583#ifdef VBOX_STRICT
2584#ifdef IN_GC
2585# undef AssertMsgFailed
2586# define AssertMsgFailed Log
2587#endif
2588#ifdef IN_RING3
2589# include <VBox/dbgf.h>
2590
2591/**
2592 * Dumps a page table hierarchy use only physical addresses and cr4/lm flags.
2593 *
2594 * @returns VBox status code (VINF_SUCCESS).
2595 * @param pVM The VM handle.
2596 * @param cr3 The root of the hierarchy.
2597 * @param crr The cr4, only PAE and PSE is currently used.
2598 * @param fLongMode Set if long mode, false if not long mode.
2599 * @param cMaxDepth Number of levels to dump.
2600 * @param pHlp Pointer to the output functions.
2601 */
2602__BEGIN_DECLS
2603PGMR3DECL(int) PGMR3DumpHierarchyHC(PVM pVM, uint32_t cr3, uint32_t cr4, bool fLongMode, unsigned cMaxDepth, PCDBGFINFOHLP pHlp);
2604__END_DECLS
2605
2606#endif
2607
2608/**
2609 * Checks that the shadow page table is in sync with the guest one.
2610 *
2611 * @returns The number of errors.
2612 * @param pVM The virtual machine.
2613 * @param cr3 Guest context CR3 register
2614 * @param cr4 Guest context CR4 register
2615 * @param GCPtr Where to start. Defaults to 0.
2616 * @param cb How much to check. Defaults to everything.
2617 */
2618PGM_BTH_DECL(unsigned, AssertCR3)(PVM pVM, uint32_t cr3, uint32_t cr4, RTGCUINTPTR GCPtr, RTGCUINTPTR cb)
2619{
2620 unsigned cErrors = 0;
2621
2622#if PGM_GST_TYPE == PGM_TYPE_32BIT
2623
2624# if PGM_SHW_TYPE != PGM_TYPE_32BIT && PGM_SHW_TYPE != PGM_TYPE_PAE
2625# error "Invalid shadow mode for 32-bit guest paging."
2626# endif
2627
2628 PPGM pPGM = &pVM->pgm.s;
2629 RTHCPHYS HCPhysShw; /* page address derived from the shadow page tables. */
2630 RTGCPHYS GCPhysGst; /* page address derived from the guest page tables. */
2631 RTHCPHYS HCPhys; /* general usage. */
2632 int rc;
2633
2634 /*
2635 * Check that the Guest CR3 and all it's mappings are correct.
2636 */
2637 AssertMsgReturn(pPGM->GCPhysCR3 == (cr3 & X86_CR3_PAGE_MASK),
2638 ("Invalid GCPhysCR3=%VGp cr3=%VGp\n", pPGM->GCPhysCR3, (RTGCPHYS)cr3),
2639 false);
2640 rc = PGMShwGetPage(pVM, pPGM->pGuestPDGC, NULL, &HCPhysShw);
2641 AssertRCReturn(rc, 1);
2642 rc = PGMRamGCPhys2HCPhys(pPGM, cr3 & X86_CR3_PAGE_MASK, &HCPhys);
2643 AssertMsgReturn(HCPhys == HCPhysShw, ("HCPhys=%VHp HCPhyswShw=%VHp (cr3)\n", HCPhys, HCPhysShw), false);
2644# ifndef IN_GC
2645 RTGCPHYS GCPhys;
2646 rc = PGMPhysHCPtr2GCPhys(pVM, pPGM->pGuestPDHC, &GCPhys);
2647 AssertRCReturn(rc, 1);
2648 AssertMsgReturn((cr3 & X86_CR3_PAGE_MASK) == GCPhys, ("GCPhys=%VGp cr3=%VGp\n", GCPhys, (RTGCPHYS)cr3), false);
2649# endif
2650 const X86PD *pPDSrc = CTXSUFF(pPGM->pGuestPD);
2651
2652 /*
2653 * Get and check the Shadow CR3.
2654 */
2655# if PGM_SHW_TYPE == PGM_TYPE_32BIT
2656 const X86PD *pPDDst = pPGM->CTXMID(p,32BitPD);
2657 unsigned cPDEs = ELEMENTS(pPDDst->a);
2658# else
2659 const X86PDPAE *pPDDst = pPGM->CTXMID(ap,PaePDs[0]); /* use it as a 2048 entry PD */
2660 unsigned cPDEs = ELEMENTS(pPDDst->a) * ELEMENTS(pPGM->apHCPaePDs);
2661# endif
2662 if (cb != ~(RTGCUINTPTR)0)
2663 cPDEs = RT_MIN(cb >> SHW_PD_SHIFT, 1);
2664
2665/** @todo call the other two PGMAssert*() functions. */
2666
2667 /*
2668 * Iterate the shadow page directory.
2669 */
2670 GCPtr = (GCPtr >> SHW_PD_SHIFT) << SHW_PD_SHIFT;
2671 unsigned iPDDst = GCPtr >> SHW_PD_SHIFT;
2672 cPDEs += iPDDst;
2673 for (;
2674 iPDDst < cPDEs;
2675 iPDDst++, GCPtr += _4G / cPDEs)
2676 {
2677 const SHWPDE PdeDst = pPDDst->a[iPDDst];
2678 if (PdeDst.u & PGM_PDFLAGS_MAPPING)
2679 {
2680 Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
2681 if ((PdeDst.u & X86_PDE_AVL_MASK) != PGM_PDFLAGS_MAPPING)
2682 {
2683 AssertMsgFailed(("Mapping shall only have PGM_PDFLAGS_MAPPING set! PdeDst.u=%#RX64\n", (uint64_t)PdeDst.u));
2684 cErrors++;
2685 continue;
2686 }
2687 }
2688 else if ( (PdeDst.u & X86_PDE_P)
2689 || ((PdeDst.u & (X86_PDE_P | PGM_PDFLAGS_TRACK_DIRTY)) == (X86_PDE_P | PGM_PDFLAGS_TRACK_DIRTY))
2690 )
2691 {
2692 HCPhysShw = PdeDst.u & SHW_PDE_PG_MASK;
2693 PPGMPOOLPAGE pPage = pgmPoolGetPageByHCPhys(pVM, HCPhysShw);
2694 if (!pPage)
2695 {
2696 AssertMsgFailed(("Invalid page table address %VGp at %VGv! PdeDst=%#RX64\n",
2697 HCPhysShw, GCPtr, (uint64_t)PdeDst.u));
2698 cErrors++;
2699 continue;
2700 }
2701 const SHWPT *pPTDst = (const SHWPT *)PGMPOOL_PAGE_2_PTR(pVM, pPage);
2702
2703 if (PdeDst.u & (X86_PDE4M_PWT | X86_PDE4M_PCD))
2704 {
2705 AssertMsgFailed(("PDE flags PWT and/or PCD is set at %VGv! These flags are not virtualized! PdeDst=%#RX64\n",
2706 GCPtr, (uint64_t)PdeDst.u));
2707 cErrors++;
2708 }
2709
2710 if (PdeDst.u & (X86_PDE4M_G | X86_PDE4M_D))
2711 {
2712 AssertMsgFailed(("4K PDE reserved flags at %VGv! PdeDst=%#RX64\n",
2713 GCPtr, (uint64_t)PdeDst.u));
2714 cErrors++;
2715 }
2716
2717 const X86PDE PdeSrc = pPDSrc->a[iPDDst >> (GST_PD_SHIFT - SHW_PD_SHIFT)];
2718 if (!PdeSrc.n.u1Present)
2719 {
2720 AssertMsgFailed(("Guest PDE at %VGv is not present! PdeDst=%#RX64 PdeSrc=%#RX64\n",
2721 GCPtr, (uint64_t)PdeDst.u, (uint64_t)PdeSrc.u));
2722 cErrors++;
2723 continue;
2724 }
2725
2726 if ( !PdeSrc.b.u1Size
2727 || !(cr4 & X86_CR4_PSE))
2728 {
2729 GCPhysGst = PdeSrc.u & GST_PDE_PG_MASK;
2730# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
2731 GCPhysGst |= (iPDDst & 1) * (PAGE_SIZE / 2);
2732# endif
2733 }
2734 else
2735 {
2736 if (PdeSrc.u & X86_PDE4M_PG_HIGH_MASK)
2737 {
2738 AssertMsgFailed(("Guest PDE at %VGv is using PSE36 or similar! PdeSrc=%#RX64\n",
2739 GCPtr, (uint64_t)PdeSrc.u));
2740 cErrors++;
2741 continue;
2742 }
2743 GCPhysGst = PdeSrc.u & GST_PDE4M_PG_MASK;
2744# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
2745 GCPhysGst |= GCPtr & BIT(X86_PAGE_2M_SHIFT);
2746# endif
2747 }
2748
2749 if ( pPage->enmKind
2750 != (!PdeSrc.b.u1Size || !(cr4 & X86_CR4_PSE) ? BTH_PGMPOOLKIND_PT_FOR_PT : BTH_PGMPOOLKIND_PT_FOR_BIG))
2751 {
2752 AssertMsgFailed(("Invalid shadow page table kind %d at %VGv! PdeSrc=%#RX64\n",
2753 pPage->enmKind, GCPtr, (uint64_t)PdeSrc.u));
2754 cErrors++;
2755 }
2756
2757 rc = PGMRamGCPhys2HCPhysWithFlags(pPGM, GCPhysGst, &HCPhys);
2758 if (VBOX_FAILURE(rc))
2759 {
2760 AssertMsgFailed(("Cannot find guest physical address %VGp in the PDE at %VGv! PdeSrc=%#RX64\n",
2761 GCPhysGst, GCPtr, (uint64_t)PdeSrc.u));
2762 cErrors++;
2763 continue;
2764 }
2765
2766 if (GCPhysGst != pPage->GCPhys)
2767 {
2768 AssertMsgFailed(("GCPhysGst=%VGp != pPage->GCPhys=%VGp at %VGv\n",
2769 GCPhysGst, pPage->GCPhys, GCPtr));
2770 cErrors++;
2771 continue;
2772 }
2773
2774 if ( !PdeSrc.b.u1Size
2775 || !(cr4 & X86_CR4_PSE))
2776 {
2777 /*
2778 * Page Table.
2779 */
2780 const GSTPT *pPTSrc;
2781 rc = PGM_GCPHYS_2_PTR(pVM, GCPhysGst & ~(RTGCPHYS)(PAGE_SIZE - 1), &pPTSrc);
2782 if (VBOX_FAILURE(rc))
2783 {
2784 AssertMsgFailed(("Cannot map/convert guest physical address %VGp in the PDE at %VGv! PdeSrc=%#RX64\n",
2785 GCPhysGst, GCPtr, (uint64_t)PdeSrc.u));
2786 cErrors++;
2787 continue;
2788 }
2789 if ( (PdeSrc.u & (X86_PDE_P | X86_PDE_US | X86_PDE_RW/* | X86_PDE_A*/))
2790 != (PdeDst.u & (X86_PDE_P | X86_PDE_US | X86_PDE_RW/* | X86_PDE_A*/)))
2791 {
2792 /// @todo We get here a lot on out-of-sync CR3 entries. The access handler should zap them to avoid false alarms here!
2793 // (This problem will go away when/if we shadow multiple CR3s.)
2794 AssertMsgFailed(("4K PDE flags mismatch at %VGv! PdeSrc=%#RX64 PdeDst=%#RX64\n",
2795 GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
2796 cErrors++;
2797 continue;
2798 }
2799 if (PdeDst.u & PGM_PDFLAGS_TRACK_DIRTY)
2800 {
2801 AssertMsgFailed(("4K PDEs cannot have PGM_PDFLAGS_TRACK_DIRTY set! GCPtr=%VGv PdeDst=%#RX64\n",
2802 GCPtr, (uint64_t)PdeDst.u));
2803 cErrors++;
2804 continue;
2805 }
2806
2807 /* iterate the page table. */
2808# if PGM_SHW_TYPE == PGM_TYPE_32BIT
2809 const unsigned offPTSrc = 0;
2810# else
2811 const unsigned offPTSrc = ((GCPtr >> SHW_PD_SHIFT) & 1) * 512;
2812# endif
2813 for (unsigned iPT = 0, off = 0;
2814 iPT < ELEMENTS(pPTDst->a);
2815 iPT++, off += PAGE_SIZE)
2816 {
2817 const SHWPTE PteDst = pPTDst->a[iPT];
2818
2819 /* skip not-present entries. */
2820 if (!(PteDst.u & (X86_PTE_P | PGM_PTFLAGS_TRACK_DIRTY))) /** @todo deal with ALL handlers and CSAM !P pages! */
2821 continue;
2822 Assert(PteDst.n.u1Present);
2823
2824 const GSTPTE PteSrc = pPTSrc->a[iPT + offPTSrc];
2825 if (!PteSrc.n.u1Present)
2826 {
2827#ifdef IN_RING3
2828 PGMAssertHandlerAndFlagsInSync(pVM);
2829 PGMR3DumpHierarchyGC(pVM, cr3, cr4, (PdeSrc.u & GST_PDE_PG_MASK));
2830#endif
2831 AssertMsgFailed(("Out of sync (!P) PTE at %VGv! PteSrc=%#RX64 PteDst=%#RX64 pPTSrc=%VGv iPTSrc=%x PdeSrc=%x physpte=%VGp\n",
2832 GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u, pPTSrc, iPT + offPTSrc, PdeSrc.au32[0],
2833 (PdeSrc.u & GST_PDE_PG_MASK) + (iPT + offPTSrc)*sizeof(PteSrc)));
2834 cErrors++;
2835 continue;
2836 }
2837
2838 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;
2839# if 1 /** @todo sync accessed bit properly... */
2840 fIgnoreFlags |= X86_PTE_A;
2841# endif
2842
2843 /* match the physical addresses */
2844 HCPhysShw = PteDst.u & SHW_PTE_PG_MASK;
2845 GCPhysGst = PteSrc.u & GST_PTE_PG_MASK;
2846
2847# ifdef IN_RING3
2848 rc = PGMPhysGCPhys2HCPhys(pVM, GCPhysGst, &HCPhys);
2849 if (VBOX_FAILURE(rc))
2850 {
2851 if (HCPhysShw != MMR3PageDummyHCPhys(pVM))
2852 {
2853 AssertMsgFailed(("Cannot find guest physical address %VGp at %VGv! PteSrc=%#RX64 PteDst=%#RX64\n",
2854 GCPhysGst, GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
2855 cErrors++;
2856 continue;
2857 }
2858 }
2859 else if (HCPhysShw != (HCPhys & SHW_PTE_PG_MASK))
2860 {
2861 AssertMsgFailed(("Out of sync (phys) at %VGv! HCPhysShw=%VHp HCPhys=%VHp GCPhysGst=%VGp PteSrc=%#RX64 PteDst=%#RX64\n",
2862 GCPtr + off, HCPhysShw, HCPhys, GCPhysGst, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
2863 cErrors++;
2864 continue;
2865 }
2866# endif
2867
2868 rc = PGMRamGCPhys2HCPhysWithFlags(pPGM, GCPhysGst, &HCPhys);
2869 if (VBOX_FAILURE(rc))
2870 {
2871# ifdef IN_RING3 /** @todo make MMR3PageDummyHCPhys an 'All' function! */
2872 if (HCPhysShw != MMR3PageDummyHCPhys(pVM))
2873 {
2874 AssertMsgFailed(("Cannot find guest physical address %VGp at %VGv! PteSrc=%#RX64 PteDst=%#RX64\n",
2875 GCPhysGst, GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
2876 cErrors++;
2877 continue;
2878 }
2879# endif
2880 if (PteDst.n.u1Write)
2881 {
2882 AssertMsgFailed(("Invalid guest page at %VGv is writable! GCPhysGst=%VGp PteSrc=%#RX64 PteDst=%#RX64\n",
2883 GCPtr + off, GCPhysGst, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
2884 cErrors++;
2885 }
2886 fIgnoreFlags |= X86_PTE_RW;
2887 }
2888 else if (HCPhysShw != (HCPhys & SHW_PTE_PG_MASK))
2889 {
2890 AssertMsgFailed(("Out of sync (phys) at %VGv! HCPhysShw=%VHp HCPhys=%VHp GCPhysGst=%VGp PteSrc=%#RX64 PteDst=%#RX64\n",
2891 GCPtr + off, HCPhysShw, HCPhys, GCPhysGst, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
2892 cErrors++;
2893 continue;
2894 }
2895
2896 /* flags */
2897 if (HCPhys & (MM_RAM_FLAGS_PHYSICAL_ALL | MM_RAM_FLAGS_VIRTUAL_ALL | MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_VIRTUAL_WRITE))
2898 {
2899 if (HCPhys & (MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_VIRTUAL_WRITE))
2900 {
2901 if (PteDst.n.u1Write)
2902 {
2903 AssertMsgFailed(("WRITE access flagged at %VGv but the page is writable! HCPhys=%VGv PteSrc=%#RX64 PteDst=%#RX64\n",
2904 GCPtr + off, HCPhys, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
2905 cErrors++;
2906 continue;
2907 }
2908 fIgnoreFlags |= X86_PTE_RW;
2909 }
2910 else
2911 {
2912 if (PteDst.n.u1Present)
2913 {
2914 AssertMsgFailed(("ALL access flagged at %VGv but the page is present! HCPhys=%VHp PteSrc=%#RX64 PteDst=%#RX64\n",
2915 GCPtr + off, HCPhys, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
2916 cErrors++;
2917 continue;
2918 }
2919 fIgnoreFlags |= X86_PTE_P;
2920 }
2921 }
2922 else
2923 {
2924 if (!PteSrc.n.u1Dirty && PteSrc.n.u1Write)
2925 {
2926 if (PteDst.n.u1Write)
2927 {
2928 AssertMsgFailed(("!DIRTY page at %VGv is writable! PteSrc=%#RX64 PteDst=%#RX64\n",
2929 GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
2930 cErrors++;
2931 continue;
2932 }
2933 if (!(PteDst.u & PGM_PTFLAGS_TRACK_DIRTY))
2934 {
2935 AssertMsgFailed(("!DIRTY page at %VGv is not marked TRACK_DIRTY! PteSrc=%#RX64 PteDst=%#RX64\n",
2936 GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
2937 cErrors++;
2938 continue;
2939 }
2940 if (PteDst.n.u1Dirty)
2941 {
2942 AssertMsgFailed(("!DIRTY page at %VGv is marked DIRTY! PteSrc=%#RX64 PteDst=%#RX64\n",
2943 GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
2944 cErrors++;
2945 }
2946# if 0 /** @todo sync access bit properly... */
2947 if (PteDst.n.u1Accessed != PteSrc.n.u1Accessed)
2948 {
2949 AssertMsgFailed(("!DIRTY page at %VGv is has mismatching accessed bit! PteSrc=%#RX64 PteDst=%#RX64\n",
2950 GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
2951 cErrors++;
2952 }
2953 fIgnoreFlags |= X86_PTE_RW;
2954# else
2955 fIgnoreFlags |= X86_PTE_RW | X86_PTE_A;
2956# endif
2957 }
2958 else if (PteDst.u & PGM_PTFLAGS_TRACK_DIRTY)
2959 {
2960 /* access bit emulation (not implemented). */
2961 if (PteSrc.n.u1Accessed || PteDst.n.u1Present)
2962 {
2963 AssertMsgFailed(("PGM_PTFLAGS_TRACK_DIRTY set at %VGv but no accessed bit emulation! PteSrc=%#RX64 PteDst=%#RX64\n",
2964 GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
2965 cErrors++;
2966 continue;
2967 }
2968 if (!PteDst.n.u1Accessed)
2969 {
2970 AssertMsgFailed(("!ACCESSED page at %VGv is has the accessed bit set! PteSrc=%#RX64 PteDst=%#RX64\n",
2971 GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
2972 cErrors++;
2973 }
2974 fIgnoreFlags |= X86_PTE_P;
2975 }
2976# ifdef DEBUG_sandervl
2977 fIgnoreFlags |= X86_PTE_D | X86_PTE_A;
2978# endif
2979 }
2980
2981 if ( (PteSrc.u & ~fIgnoreFlags) != (PteDst.u & ~fIgnoreFlags)
2982 && (PteSrc.u & ~(fIgnoreFlags | X86_PTE_RW)) != (PteDst.u & ~fIgnoreFlags)
2983 )
2984 {
2985 AssertMsgFailed(("Flags mismatch at %VGv! %#RX64 != %#RX64 fIgnoreFlags=%#RX64 PteSrc=%#RX64 PteDst=%#RX64\n",
2986 GCPtr + off, (uint64_t)PteSrc.u & ~fIgnoreFlags, (uint64_t)PteDst.u & ~fIgnoreFlags,
2987 fIgnoreFlags, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
2988 cErrors++;
2989 continue;
2990 }
2991 } /* foreach PTE */
2992 }
2993 else
2994 {
2995 /*
2996 * Big Page.
2997 */
2998 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;
2999 if (!PdeSrc.b.u1Dirty && PdeSrc.b.u1Write)
3000 {
3001 if (PdeDst.n.u1Write)
3002 {
3003 AssertMsgFailed(("!DIRTY page at %VGv is writable! PdeSrc=%#RX64 PdeDst=%#RX64\n",
3004 GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
3005 cErrors++;
3006 continue;
3007 }
3008 if (!(PdeDst.u & PGM_PDFLAGS_TRACK_DIRTY))
3009 {
3010 AssertMsgFailed(("!DIRTY page at %VGv is not marked TRACK_DIRTY! PteSrc=%#RX64 PteDst=%#RX64\n",
3011 GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
3012 cErrors++;
3013 continue;
3014 }
3015# if 0 /** @todo sync access bit properly... */
3016 if (PdeDst.n.u1Accessed != PdeSrc.b.u1Accessed)
3017 {
3018 AssertMsgFailed(("!DIRTY page at %VGv is has mismatching accessed bit! PteSrc=%#RX64 PteDst=%#RX64\n",
3019 GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
3020 cErrors++;
3021 }
3022 fIgnoreFlags |= X86_PTE_RW;
3023# else
3024 fIgnoreFlags |= X86_PTE_RW | X86_PTE_A;
3025# endif
3026 }
3027 else if (PdeDst.u & PGM_PDFLAGS_TRACK_DIRTY)
3028 {
3029 /* access bit emulation (not implemented). */
3030 if (PdeSrc.b.u1Accessed || PdeDst.n.u1Present)
3031 {
3032 AssertMsgFailed(("PGM_PDFLAGS_TRACK_DIRTY set at %VGv but no accessed bit emulation! PdeSrc=%#RX64 PdeDst=%#RX64\n",
3033 GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
3034 cErrors++;
3035 continue;
3036 }
3037 if (!PdeDst.n.u1Accessed)
3038 {
3039 AssertMsgFailed(("!ACCESSED page at %VGv is has the accessed bit set! PdeSrc=%#RX64 PdeDst=%#RX64\n",
3040 GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
3041 cErrors++;
3042 }
3043 fIgnoreFlags |= X86_PTE_P;
3044 }
3045
3046 if ((PdeSrc.u & ~fIgnoreFlags) != (PdeDst.u & ~fIgnoreFlags))
3047 {
3048 AssertMsgFailed(("Flags mismatch (B) at %VGv! %#RX64 != %#RX64 fIgnoreFlags=%#RX64 PdeSrc=%#RX64 PdeDst=%#RX64\n",
3049 GCPtr, (uint64_t)PdeSrc.u & ~fIgnoreFlags, (uint64_t)PdeDst.u & ~fIgnoreFlags,
3050 fIgnoreFlags, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
3051 cErrors++;
3052 }
3053
3054 /* iterate the page table. */
3055 for (unsigned iPT = 0, off = 0;
3056 iPT < ELEMENTS(pPTDst->a);
3057 iPT++, off += PAGE_SIZE, GCPhysGst += PAGE_SIZE)
3058 {
3059 const SHWPTE PteDst = pPTDst->a[iPT];
3060
3061 if (PteDst.u & PGM_PTFLAGS_TRACK_DIRTY)
3062 {
3063 AssertMsgFailed(("The PTE at %VGv emulating a 2/4M page is marked TRACK_DIRTY! PdeSrc=%#RX64 PteDst=%#RX64\n",
3064 GCPtr + off, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
3065 cErrors++;
3066 }
3067
3068 /* skip not-present entries. */
3069 if (!PteDst.n.u1Present) /** @todo deal with ALL handlers and CSAM !P pages! */
3070 continue;
3071
3072 fIgnoreFlags = X86_PTE_PAE_PG_MASK | X86_PTE_AVL_MASK | X86_PTE_PWT | X86_PTE_PCD | X86_PTE_PAT;
3073
3074 /* match the physical addresses */
3075 HCPhysShw = PteDst.u & X86_PTE_PAE_PG_MASK;
3076
3077# ifdef IN_RING3
3078 rc = PGMPhysGCPhys2HCPhys(pVM, GCPhysGst, &HCPhys);
3079 if (VBOX_FAILURE(rc))
3080 {
3081 if (HCPhysShw != MMR3PageDummyHCPhys(pVM))
3082 {
3083 AssertMsgFailed(("Cannot find guest physical address %VGp at %VGv! PdeSrc=%#RX64 PteDst=%#RX64\n",
3084 GCPhysGst, GCPtr + off, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
3085 cErrors++;
3086 }
3087 }
3088 else if (HCPhysShw != (HCPhys & X86_PTE_PAE_PG_MASK))
3089 {
3090 AssertMsgFailed(("Out of sync (phys) at %VGv! HCPhysShw=%VHp HCPhys=%VHp GCPhysGst=%VGp PdeSrc=%#RX64 PteDst=%#RX64\n",
3091 GCPtr + off, HCPhysShw, HCPhys, GCPhysGst, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
3092 cErrors++;
3093 continue;
3094 }
3095# endif
3096
3097 rc = PGMRamGCPhys2HCPhysWithFlags(pPGM, GCPhysGst, &HCPhys);
3098 if (VBOX_FAILURE(rc))
3099 {
3100# ifdef IN_RING3 /** @todo make MMR3PageDummyHCPhys an 'All' function! */
3101 if (HCPhysShw != MMR3PageDummyHCPhys(pVM))
3102 {
3103 AssertMsgFailed(("Cannot find guest physical address %VGp at %VGv! PdeSrc=%#RX64 PteDst=%#RX64\n",
3104 GCPhysGst, GCPtr + off, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
3105 cErrors++;
3106 continue;
3107 }
3108# endif
3109 if (PteDst.n.u1Write)
3110 {
3111 AssertMsgFailed(("Invalid guest page at %VGv is writable! GCPhysGst=%VGp PdeSrc=%#RX64 PteDst=%#RX64\n",
3112 GCPtr + off, GCPhysGst, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
3113 cErrors++;
3114 }
3115 fIgnoreFlags |= X86_PTE_RW;
3116 }
3117 else if (HCPhysShw != (HCPhys & X86_PTE_PAE_PG_MASK))
3118 {
3119 AssertMsgFailed(("Out of sync (phys) at %VGv! HCPhysShw=%VHp HCPhys=%VHp GCPhysGst=%VGp PdeSrc=%#RX64 PteDst=%#RX64\n",
3120 GCPtr + off, HCPhysShw, HCPhys, GCPhysGst, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
3121 cErrors++;
3122 continue;
3123 }
3124
3125 /* flags */
3126 if (HCPhys & (MM_RAM_FLAGS_PHYSICAL_ALL | MM_RAM_FLAGS_VIRTUAL_ALL | MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_VIRTUAL_WRITE))
3127 {
3128 if (HCPhys & (MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_VIRTUAL_WRITE))
3129 {
3130 if (!(HCPhys & MM_RAM_FLAGS_PHYSICAL_TEMP_OFF))
3131 {
3132 if (PteDst.n.u1Write)
3133 {
3134 AssertMsgFailed(("WRITE access flagged at %VGv but the page is writable! HCPhys=%VGv PdeSrc=%#RX64 PteDst=%#RX64\n",
3135 GCPtr + off, HCPhys, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
3136 cErrors++;
3137 continue;
3138 }
3139 fIgnoreFlags |= X86_PTE_RW;
3140 }
3141 }
3142 else
3143 {
3144 if (PteDst.n.u1Present)
3145 {
3146 AssertMsgFailed(("ALL access flagged at %VGv but the page is present! HCPhys=%VGv PdeSrc=%#RX64 PteDst=%#RX64\n",
3147 GCPtr + off, HCPhys, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
3148 cErrors++;
3149 continue;
3150 }
3151 fIgnoreFlags |= X86_PTE_P;
3152 }
3153 }
3154
3155 if ( (PdeSrc.u & ~fIgnoreFlags) != (PteDst.u & ~fIgnoreFlags)
3156 && (PdeSrc.u & ~(fIgnoreFlags | X86_PTE_RW)) != (PteDst.u & ~fIgnoreFlags) /* lazy phys handler dereg. */
3157 )
3158 {
3159 AssertMsgFailed(("Flags mismatch (BT) at %VGv! %#RX64 != %#RX64 fIgnoreFlags=%#RX64 PdeSrc=%#RX64 PteDst=%#RX64\n",
3160 GCPtr + off, (uint64_t)PdeSrc.u & ~fIgnoreFlags, (uint64_t)PteDst.u & ~fIgnoreFlags,
3161 fIgnoreFlags, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
3162 cErrors++;
3163 continue;
3164 }
3165 } /* foreach PTE */
3166 }
3167 }
3168 /* not present */
3169
3170 } /* forearch PDE */
3171
3172# ifdef DEBUG
3173 if (cErrors)
3174 LogFlow(("AssertCR3: cErrors=%d\n", cErrors));
3175# endif
3176
3177#elif PGM_GST_TYPE == PGM_TYPE_PAE
3178//# error not implemented
3179
3180
3181#elif PGM_GST_TYPE == PGM_TYPE_AMD64
3182//# error not implemented
3183
3184/*#else: guest real and protected mode */
3185#endif
3186 return cErrors;
3187}
3188#endif /* VBOX_STRICT */
3189
Note: See TracBrowser for help on using the repository browser.

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