VirtualBox

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

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

#1865: shadow pae, the forth bit. (dead code checks)

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