VirtualBox

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

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

Long mode paging updates

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