VirtualBox

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

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

Backed out 32892-32894

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