VirtualBox

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

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

Cleanup

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