VirtualBox

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

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

PGM: Deal with pgmPoolAlloc failure in MapCR3 without relying on having to clear the pool. The MapCR3 action will be postponed to SyncCR3.

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