VirtualBox

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

Last change on this file since 8042 was 8042, checked in by vboxsync, 17 years ago

corrected #if/#else/#endif comments.

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