VirtualBox

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

Last change on this file since 13919 was 13919, checked in by vboxsync, 16 years ago

VMM: 3 R3R0PTRTYPE members down (a bunch left to go).

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