VirtualBox

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

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

#1865: Converted 4 PGM*2HC* conversion functions to RTR3PTR.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 187.8 KB
Line 
1/* $Id: PGMAllBth.h 14755 2008-11-28 02:58:01Z 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#ifdef PGMPOOL_WITH_MONITORING
3123 int rc = pgmPoolSyncCR3(pVM);
3124 if (rc != VINF_SUCCESS)
3125 return rc;
3126#endif
3127
3128#if PGM_SHW_TYPE == PGM_TYPE_NESTED || PGM_SHW_TYPE == PGM_TYPE_EPT
3129 /*
3130 * Nested / EPT - almost no work.
3131 */
3132 /** @todo check if this is really necessary */
3133 HWACCMFlushTLB(pVM);
3134 return VINF_SUCCESS;
3135
3136#elif PGM_SHW_TYPE == PGM_TYPE_AMD64 || defined(VBOX_WITH_PGMPOOL_PAGING_ONLY)
3137 /*
3138 * AMD64 (Shw & Gst) - No need to check all paging levels; we zero
3139 * out the shadow parts when the guest modifies its tables.
3140 */
3141 return VINF_SUCCESS;
3142
3143#else /* PGM_SHW_TYPE != PGM_TYPE_NESTED && PGM_SHW_TYPE != PGM_TYPE_EPT && PGM_SHW_TYPE != PGM_TYPE_AMD64 */
3144 /*
3145 * PAE and 32-bit legacy mode (shadow).
3146 * (Guest PAE, 32-bit legacy, protected and real modes.)
3147 */
3148 Assert(fGlobal || (cr4 & X86_CR4_PGE));
3149 MY_STAM_COUNTER_INC(fGlobal ? &pVM->pgm.s.CTX_MID_Z(Stat,SyncCR3Global) : &pVM->pgm.s.CTX_MID_Z(Stat,SyncCR3NotGlobal));
3150
3151# if PGM_GST_TYPE == PGM_TYPE_32BIT || PGM_GST_TYPE == PGM_TYPE_PAE
3152 bool const fBigPagesSupported = !!(CPUMGetGuestCR4(pVM) & X86_CR4_PSE);
3153
3154 /*
3155 * Get page directory addresses.
3156 */
3157# if PGM_SHW_TYPE == PGM_TYPE_32BIT
3158 PX86PDE pPDEDst = pgmShwGet32BitPDEPtr(&pVM->pgm.s, 0);
3159# else /* PGM_SHW_TYPE == PGM_TYPE_PAE */
3160# if PGM_GST_TYPE == PGM_TYPE_32BIT
3161 PX86PDEPAE pPDEDst = NULL;
3162# endif
3163# endif
3164
3165# if PGM_GST_TYPE == PGM_TYPE_32BIT
3166 PGSTPD pPDSrc = pgmGstGet32bitPDPtr(&pVM->pgm.s);
3167 Assert(pPDSrc);
3168# if !defined(IN_RC) && !defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
3169 Assert(PGMPhysGCPhys2R3PtrAssert(pVM, (RTGCPHYS)(cr3 & GST_CR3_PAGE_MASK), sizeof(*pPDSrc)) == (RTR3PTR)pPDSrc);
3170# endif
3171# endif /* PGM_GST_TYPE == PGM_TYPE_32BIT */
3172
3173 /*
3174 * Iterate the the CR3 page.
3175 */
3176 PPGMMAPPING pMapping;
3177 unsigned iPdNoMapping;
3178 const bool fRawR0Enabled = EMIsRawRing0Enabled(pVM);
3179 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
3180
3181 /* Only check mappings if they are supposed to be put into the shadow page table. */
3182 if (pgmMapAreMappingsEnabled(&pVM->pgm.s))
3183 {
3184 pMapping = pVM->pgm.s.CTX_SUFF(pMappings);
3185 iPdNoMapping = (pMapping) ? (pMapping->GCPtr >> GST_PD_SHIFT) : ~0U;
3186 }
3187 else
3188 {
3189 pMapping = 0;
3190 iPdNoMapping = ~0U;
3191 }
3192
3193# if PGM_GST_TYPE == PGM_TYPE_PAE
3194 for (uint64_t iPdpt = 0; iPdpt < GST_PDPE_ENTRIES; iPdpt++)
3195 {
3196 unsigned iPDSrc;
3197 X86PDPE PdpeSrc;
3198 PGSTPD pPDSrc = pgmGstGetPaePDPtr(&pVM->pgm.s, iPdpt << X86_PDPT_SHIFT, &iPDSrc, &PdpeSrc);
3199 PX86PDEPAE pPDEDst = pgmShwGetPaePDEPtr(&pVM->pgm.s, iPdpt << X86_PDPT_SHIFT);
3200 PX86PDPT pPdptDst = pgmShwGetPaePDPTPtr(&pVM->pgm.s);
3201
3202 if (pPDSrc == NULL)
3203 {
3204 /* PDPE not present */
3205 if (pPdptDst->a[iPdpt].n.u1Present)
3206 {
3207 LogFlow(("SyncCR3: guest PDPE %lld not present; clear shw pdpe\n", iPdpt));
3208 /* for each page directory entry */
3209 for (unsigned iPD = 0; iPD < RT_ELEMENTS(pPDSrc->a); iPD++)
3210 {
3211 if ( pPDEDst[iPD].n.u1Present
3212 && !(pPDEDst[iPD].u & PGM_PDFLAGS_MAPPING))
3213 {
3214 pgmPoolFree(pVM, pPDEDst[iPD].u & SHW_PDE_PG_MASK, SHW_POOL_ROOT_IDX, iPdpt * X86_PG_PAE_ENTRIES + iPD);
3215 pPDEDst[iPD].u = 0;
3216 }
3217 }
3218 }
3219 if (!(pPdptDst->a[iPdpt].u & PGM_PLXFLAGS_MAPPING))
3220 pPdptDst->a[iPdpt].n.u1Present = 0;
3221 continue;
3222 }
3223# else /* PGM_GST_TYPE != PGM_TYPE_PAE */
3224 {
3225# endif /* PGM_GST_TYPE != PGM_TYPE_PAE */
3226 for (unsigned iPD = 0; iPD < RT_ELEMENTS(pPDSrc->a); iPD++)
3227 {
3228# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
3229 if ((iPD & 255) == 0) /* Start of new PD. */
3230 pPDEDst = pgmShwGetPaePDEPtr(&pVM->pgm.s, (uint32_t)iPD << GST_PD_SHIFT);
3231# endif
3232# if PGM_SHW_TYPE == PGM_TYPE_32BIT
3233 Assert(pgmShwGet32BitPDEPtr(&pVM->pgm.s, (uint32_t)iPD << SHW_PD_SHIFT) == pPDEDst);
3234# elif PGM_SHW_TYPE == PGM_TYPE_PAE
3235# ifdef VBOX_STRICT
3236 RTGCPTR GCPtrStrict = (uint32_t)iPD << GST_PD_SHIFT;
3237# if PGM_GST_TYPE == PGM_TYPE_PAE
3238 GCPtrStrict |= iPdpt << X86_PDPT_SHIFT;
3239# endif
3240 AssertMsg(pgmShwGetPaePDEPtr(&pVM->pgm.s, GCPtrStrict) == pPDEDst, ("%p vs %p (%RGv)\n", pgmShwGetPaePDEPtr(&pVM->pgm.s, GCPtrStrict), pPDEDst, GCPtrStrict));
3241# endif /* VBOX_STRICT */
3242# endif
3243 GSTPDE PdeSrc = pPDSrc->a[iPD];
3244 if ( PdeSrc.n.u1Present
3245 && (PdeSrc.n.u1User || fRawR0Enabled))
3246 {
3247# if ( PGM_GST_TYPE == PGM_TYPE_32BIT \
3248 || PGM_GST_TYPE == PGM_TYPE_PAE) \
3249 && !defined(PGM_WITHOUT_MAPPINGS)
3250
3251 /*
3252 * Check for conflicts with GC mappings.
3253 */
3254# if PGM_GST_TYPE == PGM_TYPE_PAE
3255 if (iPD + iPdpt * X86_PG_PAE_ENTRIES == iPdNoMapping)
3256# else
3257 if (iPD == iPdNoMapping)
3258# endif
3259 {
3260 if (pVM->pgm.s.fMappingsFixed)
3261 {
3262 /* It's fixed, just skip the mapping. */
3263 const unsigned cPTs = pMapping->cb >> GST_PD_SHIFT;
3264 Assert(PGM_GST_TYPE == PGM_TYPE_32BIT || (iPD + cPTs - 1) / X86_PG_PAE_ENTRIES == iPD / X86_PG_PAE_ENTRIES);
3265 iPD += cPTs - 1;
3266# if PGM_SHW_TYPE != PGM_GST_TYPE /* SHW==PAE && GST==32BIT */
3267 pPDEDst = pgmShwGetPaePDEPtr(&pVM->pgm.s, (uint32_t)(iPD + 1) << GST_PD_SHIFT);
3268# else
3269 pPDEDst += cPTs;
3270# endif
3271 pMapping = pMapping->CTX_SUFF(pNext);
3272 iPdNoMapping = pMapping ? pMapping->GCPtr >> GST_PD_SHIFT : ~0U;
3273 continue;
3274 }
3275# ifdef IN_RING3
3276# if PGM_GST_TYPE == PGM_TYPE_32BIT
3277 int rc = pgmR3SyncPTResolveConflict(pVM, pMapping, pPDSrc, iPD << GST_PD_SHIFT);
3278# elif PGM_GST_TYPE == PGM_TYPE_PAE
3279 int rc = pgmR3SyncPTResolveConflictPAE(pVM, pMapping, (iPdpt << GST_PDPT_SHIFT) + (iPD << GST_PD_SHIFT));
3280# endif
3281 if (RT_FAILURE(rc))
3282 return rc;
3283
3284 /*
3285 * Update iPdNoMapping and pMapping.
3286 */
3287 pMapping = pVM->pgm.s.pMappingsR3;
3288 while (pMapping && pMapping->GCPtr < (iPD << GST_PD_SHIFT))
3289 pMapping = pMapping->pNextR3;
3290 iPdNoMapping = pMapping ? pMapping->GCPtr >> GST_PD_SHIFT : ~0U;
3291# else /* !IN_RING3 */
3292 LogFlow(("SyncCR3: detected conflict -> VINF_PGM_SYNC_CR3\n"));
3293 return VINF_PGM_SYNC_CR3;
3294# endif /* !IN_RING3 */
3295 }
3296# else /* (PGM_GST_TYPE != PGM_TYPE_32BIT && PGM_GST_TYPE != PGM_TYPE_PAE) || PGM_WITHOUT_MAPPINGS */
3297 Assert(!pgmMapAreMappingsEnabled(&pVM->pgm.s));
3298# endif /* (PGM_GST_TYPE != PGM_TYPE_32BIT && PGM_GST_TYPE != PGM_TYPE_PAE) || PGM_WITHOUT_MAPPINGS */
3299
3300 /*
3301 * Sync page directory entry.
3302 *
3303 * The current approach is to allocated the page table but to set
3304 * the entry to not-present and postpone the page table synching till
3305 * it's actually used.
3306 */
3307# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
3308 for (unsigned i = 0, iPdShw = iPD * 2; i < 2; i++, iPdShw++) /* pray that the compiler unrolls this */
3309# elif PGM_GST_TYPE == PGM_TYPE_PAE
3310 const unsigned iPdShw = iPD + iPdpt * X86_PG_PAE_ENTRIES; NOREF(iPdShw);
3311# else
3312 const unsigned iPdShw = iPD; NOREF(iPdShw);
3313# endif
3314 {
3315 SHWPDE PdeDst = *pPDEDst;
3316 if (PdeDst.n.u1Present)
3317 {
3318 PPGMPOOLPAGE pShwPage = pgmPoolGetPage(pPool, PdeDst.u & SHW_PDE_PG_MASK);
3319 RTGCPHYS GCPhys;
3320 if ( !PdeSrc.b.u1Size
3321 || !fBigPagesSupported)
3322 {
3323 GCPhys = PdeSrc.u & GST_PDE_PG_MASK;
3324# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
3325 /* Select the right PDE as we're emulating a 4kb page table with 2 shadow page tables. */
3326 GCPhys |= i * (PAGE_SIZE / 2);
3327# endif
3328 }
3329 else
3330 {
3331 GCPhys = GST_GET_PDE_BIG_PG_GCPHYS(PdeSrc);
3332# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
3333 /* Select the right PDE as we're emulating a 4MB page directory with two 2 MB shadow PDEs.*/
3334 GCPhys |= i * X86_PAGE_2M_SIZE;
3335# endif
3336 }
3337
3338 if ( pShwPage->GCPhys == GCPhys
3339 && pShwPage->enmKind == PGM_BTH_NAME(CalcPageKind)(&PdeSrc, cr4)
3340 && ( pShwPage->fCached
3341 || ( !fGlobal
3342 && ( false
3343# ifdef PGM_SKIP_GLOBAL_PAGEDIRS_ON_NONGLOBAL_FLUSH
3344 || ( (PdeSrc.u & (X86_PDE4M_PS | X86_PDE4M_G)) == (X86_PDE4M_PS | X86_PDE4M_G)
3345 && (cr4 & (X86_CR4_PGE | X86_CR4_PSE)) == (X86_CR4_PGE | X86_CR4_PSE)) /* global 2/4MB page. */
3346 || ( !pShwPage->fSeenNonGlobal
3347 && (cr4 & X86_CR4_PGE))
3348# endif
3349 )
3350 )
3351 )
3352 && ( (PdeSrc.u & (X86_PDE_US | X86_PDE_RW)) == (PdeDst.u & (X86_PDE_US | X86_PDE_RW))
3353 || ( fBigPagesSupported
3354 && ((PdeSrc.u & (X86_PDE_US | X86_PDE4M_PS | X86_PDE4M_D)) | PGM_PDFLAGS_TRACK_DIRTY)
3355 == ((PdeDst.u & (X86_PDE_US | X86_PDE_RW | PGM_PDFLAGS_TRACK_DIRTY)) | X86_PDE4M_PS))
3356 )
3357 )
3358 {
3359# ifdef VBOX_WITH_STATISTICS
3360 if ( !fGlobal
3361 && (PdeSrc.u & (X86_PDE4M_PS | X86_PDE4M_G)) == (X86_PDE4M_PS | X86_PDE4M_G)
3362 && (cr4 & (X86_CR4_PGE | X86_CR4_PSE)) == (X86_CR4_PGE | X86_CR4_PSE))
3363 MY_STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,SyncCR3DstSkippedGlobalPD));
3364 else if (!fGlobal && !pShwPage->fSeenNonGlobal && (cr4 & X86_CR4_PGE))
3365 MY_STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,SyncCR3DstSkippedGlobalPT));
3366 else
3367 MY_STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,SyncCR3DstCacheHit));
3368# endif /* VBOX_WITH_STATISTICS */
3369 /** @todo a replacement strategy isn't really needed unless we're using a very small pool < 512 pages.
3370 * The whole ageing stuff should be put in yet another set of #ifdefs. For now, let's just skip it. */
3371 //# ifdef PGMPOOL_WITH_CACHE
3372 // pgmPoolCacheUsed(pPool, pShwPage);
3373 //# endif
3374 }
3375 else
3376 {
3377 pgmPoolFreeByPage(pPool, pShwPage, SHW_POOL_ROOT_IDX, iPdShw);
3378 pPDEDst->u = 0;
3379 MY_STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,SyncCR3DstFreed));
3380 }
3381 }
3382 else
3383 MY_STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,SyncCR3DstNotPresent));
3384
3385 /* advance */
3386 pPDEDst++;
3387 } /* foreach 2MB PAE PDE in 4MB guest PDE */
3388 }
3389# if PGM_GST_TYPE == PGM_TYPE_PAE
3390 else if (iPD + iPdpt * X86_PG_PAE_ENTRIES != iPdNoMapping)
3391# else
3392 else if (iPD != iPdNoMapping)
3393# endif
3394 {
3395 /*
3396 * Check if there is any page directory to mark not present here.
3397 */
3398# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
3399 for (unsigned i = 0, iPdShw = iPD * 2; i < 2; i++, iPdShw++) /* pray that the compiler unrolls this */
3400# elif PGM_GST_TYPE == PGM_TYPE_PAE
3401 const unsigned iPdShw = iPD + iPdpt * X86_PG_PAE_ENTRIES;
3402# else
3403 const unsigned iPdShw = iPD;
3404# endif
3405 {
3406 if (pPDEDst->n.u1Present)
3407 {
3408 pgmPoolFree(pVM, pPDEDst->u & SHW_PDE_PG_MASK, SHW_POOL_ROOT_IDX, iPdShw);
3409 pPDEDst->u = 0;
3410 MY_STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,SyncCR3DstFreedSrcNP));
3411 }
3412 pPDEDst++;
3413 }
3414 }
3415 else
3416 {
3417# if ( PGM_GST_TYPE == PGM_TYPE_32BIT \
3418 || PGM_GST_TYPE == PGM_TYPE_PAE) \
3419 && !defined(PGM_WITHOUT_MAPPINGS)
3420
3421 const unsigned cPTs = pMapping->cb >> GST_PD_SHIFT;
3422
3423 Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
3424 if (pVM->pgm.s.fMappingsFixed)
3425 {
3426 /* It's fixed, just skip the mapping. */
3427 pMapping = pMapping->CTX_SUFF(pNext);
3428 iPdNoMapping = pMapping ? pMapping->GCPtr >> GST_PD_SHIFT : ~0U;
3429 }
3430 else
3431 {
3432 /*
3433 * Check for conflicts for subsequent pagetables
3434 * and advance to the next mapping.
3435 */
3436 iPdNoMapping = ~0U;
3437 unsigned iPT = cPTs;
3438 while (iPT-- > 1)
3439 {
3440 if ( pPDSrc->a[iPD + iPT].n.u1Present
3441 && (pPDSrc->a[iPD + iPT].n.u1User || fRawR0Enabled))
3442 {
3443# ifdef IN_RING3
3444# if PGM_GST_TYPE == PGM_TYPE_32BIT
3445 int rc = pgmR3SyncPTResolveConflict(pVM, pMapping, pPDSrc, iPD << GST_PD_SHIFT);
3446# elif PGM_GST_TYPE == PGM_TYPE_PAE
3447 int rc = pgmR3SyncPTResolveConflictPAE(pVM, pMapping, (iPdpt << GST_PDPT_SHIFT) + (iPD << GST_PD_SHIFT));
3448# endif
3449 if (RT_FAILURE(rc))
3450 return rc;
3451
3452 /*
3453 * Update iPdNoMapping and pMapping.
3454 */
3455 pMapping = pVM->pgm.s.CTX_SUFF(pMappings);
3456 while (pMapping && pMapping->GCPtr < (iPD << GST_PD_SHIFT))
3457 pMapping = pMapping->CTX_SUFF(pNext);
3458 iPdNoMapping = pMapping ? pMapping->GCPtr >> GST_PD_SHIFT : ~0U;
3459 break;
3460# else
3461 LogFlow(("SyncCR3: detected conflict -> VINF_PGM_SYNC_CR3\n"));
3462 return VINF_PGM_SYNC_CR3;
3463# endif
3464 }
3465 }
3466 if (iPdNoMapping == ~0U && pMapping)
3467 {
3468 pMapping = pMapping->CTX_SUFF(pNext);
3469 if (pMapping)
3470 iPdNoMapping = pMapping->GCPtr >> GST_PD_SHIFT;
3471 }
3472 }
3473
3474 /* advance. */
3475 Assert(PGM_GST_TYPE == PGM_TYPE_32BIT || (iPD + cPTs - 1) / X86_PG_PAE_ENTRIES == iPD / X86_PG_PAE_ENTRIES);
3476 iPD += cPTs - 1;
3477# if PGM_SHW_TYPE != PGM_GST_TYPE /* SHW==PAE && GST==32BIT */
3478 pPDEDst = pgmShwGetPaePDEPtr(&pVM->pgm.s, (uint32_t)(iPD + 1) << GST_PD_SHIFT);
3479# else
3480 pPDEDst += cPTs;
3481# endif
3482# if PGM_GST_TYPE != PGM_SHW_TYPE
3483 AssertCompile(PGM_GST_TYPE == PGM_TYPE_32BIT && PGM_SHW_TYPE == PGM_TYPE_PAE);
3484# endif
3485# else /* (PGM_GST_TYPE != PGM_TYPE_32BIT && PGM_GST_TYPE != PGM_TYPE_PAE) || PGM_WITHOUT_MAPPINGS */
3486 Assert(!pgmMapAreMappingsEnabled(&pVM->pgm.s));
3487# endif /* (PGM_GST_TYPE != PGM_TYPE_32BIT && PGM_GST_TYPE != PGM_TYPE_PAE) || PGM_WITHOUT_MAPPINGS */
3488 }
3489
3490 } /* for iPD */
3491 } /* for each PDPTE (PAE) */
3492 return VINF_SUCCESS;
3493
3494# else /* guest real and protected mode */
3495 return VINF_SUCCESS;
3496# endif
3497#endif /* PGM_SHW_TYPE != PGM_TYPE_NESTED && PGM_SHW_TYPE != PGM_TYPE_EPT && PGM_SHW_TYPE != PGM_TYPE_AMD64 */
3498}
3499
3500
3501
3502
3503#ifdef VBOX_STRICT
3504#ifdef IN_RC
3505# undef AssertMsgFailed
3506# define AssertMsgFailed Log
3507#endif
3508#ifdef IN_RING3
3509# include <VBox/dbgf.h>
3510
3511/**
3512 * Dumps a page table hierarchy use only physical addresses and cr4/lm flags.
3513 *
3514 * @returns VBox status code (VINF_SUCCESS).
3515 * @param pVM The VM handle.
3516 * @param cr3 The root of the hierarchy.
3517 * @param crr The cr4, only PAE and PSE is currently used.
3518 * @param fLongMode Set if long mode, false if not long mode.
3519 * @param cMaxDepth Number of levels to dump.
3520 * @param pHlp Pointer to the output functions.
3521 */
3522__BEGIN_DECLS
3523VMMR3DECL(int) PGMR3DumpHierarchyHC(PVM pVM, uint32_t cr3, uint32_t cr4, bool fLongMode, unsigned cMaxDepth, PCDBGFINFOHLP pHlp);
3524__END_DECLS
3525
3526#endif
3527
3528/**
3529 * Checks that the shadow page table is in sync with the guest one.
3530 *
3531 * @returns The number of errors.
3532 * @param pVM The virtual machine.
3533 * @param cr3 Guest context CR3 register
3534 * @param cr4 Guest context CR4 register
3535 * @param GCPtr Where to start. Defaults to 0.
3536 * @param cb How much to check. Defaults to everything.
3537 */
3538PGM_BTH_DECL(unsigned, AssertCR3)(PVM pVM, uint64_t cr3, uint64_t cr4, RTGCPTR GCPtr, RTGCPTR cb)
3539{
3540#if PGM_SHW_TYPE == PGM_TYPE_NESTED || PGM_SHW_TYPE == PGM_TYPE_EPT
3541 return 0;
3542#else
3543 unsigned cErrors = 0;
3544
3545#if PGM_GST_TYPE == PGM_TYPE_PAE
3546 /** @todo currently broken; crashes below somewhere */
3547 AssertFailed();
3548#endif
3549
3550#if PGM_GST_TYPE == PGM_TYPE_32BIT \
3551 || PGM_GST_TYPE == PGM_TYPE_PAE \
3552 || PGM_GST_TYPE == PGM_TYPE_AMD64
3553
3554# if PGM_GST_TYPE == PGM_TYPE_AMD64
3555 bool fBigPagesSupported = true;
3556# else
3557 bool fBigPagesSupported = !!(CPUMGetGuestCR4(pVM) & X86_CR4_PSE);
3558# endif
3559 PPGM pPGM = &pVM->pgm.s;
3560 RTGCPHYS GCPhysGst; /* page address derived from the guest page tables. */
3561 RTHCPHYS HCPhysShw; /* page address derived from the shadow page tables. */
3562# ifndef IN_RING0
3563 RTHCPHYS HCPhys; /* general usage. */
3564# endif
3565 int rc;
3566
3567 /*
3568 * Check that the Guest CR3 and all its mappings are correct.
3569 */
3570 AssertMsgReturn(pPGM->GCPhysCR3 == (cr3 & GST_CR3_PAGE_MASK),
3571 ("Invalid GCPhysCR3=%RGp cr3=%RGp\n", pPGM->GCPhysCR3, (RTGCPHYS)cr3),
3572 false);
3573# if !defined(IN_RING0) && PGM_GST_TYPE != PGM_TYPE_AMD64
3574# if PGM_GST_TYPE == PGM_TYPE_32BIT
3575 rc = PGMShwGetPage(pVM, (RTGCPTR)pPGM->pGst32BitPdRC, NULL, &HCPhysShw);
3576# else
3577 rc = PGMShwGetPage(pVM, (RTGCPTR)pPGM->pGstPaePdptRC, NULL, &HCPhysShw);
3578# endif
3579 AssertRCReturn(rc, 1);
3580 HCPhys = NIL_RTHCPHYS;
3581 rc = pgmRamGCPhys2HCPhys(pPGM, cr3 & GST_CR3_PAGE_MASK, &HCPhys);
3582 AssertMsgReturn(HCPhys == HCPhysShw, ("HCPhys=%RHp HCPhyswShw=%RHp (cr3)\n", HCPhys, HCPhysShw), false);
3583# if PGM_GST_TYPE == PGM_TYPE_32BIT && defined(IN_RING3)
3584 RTGCPHYS GCPhys;
3585 rc = PGMR3DbgR3Ptr2GCPhys(pVM, pPGM->pGst32BitPdR3, &GCPhys);
3586 AssertRCReturn(rc, 1);
3587 AssertMsgReturn((cr3 & GST_CR3_PAGE_MASK) == GCPhys, ("GCPhys=%RGp cr3=%RGp\n", GCPhys, (RTGCPHYS)cr3), false);
3588# endif
3589# endif /* !IN_RING0 */
3590
3591 /*
3592 * Get and check the Shadow CR3.
3593 */
3594# if PGM_SHW_TYPE == PGM_TYPE_32BIT
3595 unsigned cPDEs = X86_PG_ENTRIES;
3596 unsigned cIncrement = X86_PG_ENTRIES * PAGE_SIZE;
3597# elif PGM_SHW_TYPE == PGM_TYPE_PAE
3598# if PGM_GST_TYPE == PGM_TYPE_32BIT
3599 unsigned cPDEs = X86_PG_PAE_ENTRIES * 4; /* treat it as a 2048 entry table. */
3600# else
3601 unsigned cPDEs = X86_PG_PAE_ENTRIES;
3602# endif
3603 unsigned cIncrement = X86_PG_PAE_ENTRIES * PAGE_SIZE;
3604# elif PGM_SHW_TYPE == PGM_TYPE_AMD64
3605 unsigned cPDEs = X86_PG_PAE_ENTRIES;
3606 unsigned cIncrement = X86_PG_PAE_ENTRIES * PAGE_SIZE;
3607# endif
3608 if (cb != ~(RTGCPTR)0)
3609 cPDEs = RT_MIN(cb >> SHW_PD_SHIFT, 1);
3610
3611/** @todo call the other two PGMAssert*() functions. */
3612
3613# if PGM_GST_TYPE == PGM_TYPE_AMD64 || PGM_GST_TYPE == PGM_TYPE_PAE
3614 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
3615# endif
3616
3617# if PGM_GST_TYPE == PGM_TYPE_AMD64
3618 unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
3619
3620 for (; iPml4 < X86_PG_PAE_ENTRIES; iPml4++)
3621 {
3622 PPGMPOOLPAGE pShwPdpt = NULL;
3623 PX86PML4E pPml4eSrc;
3624 PX86PML4E pPml4eDst;
3625 RTGCPHYS GCPhysPdptSrc;
3626
3627 pPml4eSrc = pgmGstGetLongModePML4EPtr(&pVM->pgm.s, iPml4);
3628 pPml4eDst = pgmShwGetLongModePML4EPtr(&pVM->pgm.s, iPml4);
3629
3630 /* Fetch the pgm pool shadow descriptor if the shadow pml4e is present. */
3631 if (!pPml4eDst->n.u1Present)
3632 {
3633 GCPtr += _2M * UINT64_C(512) * UINT64_C(512);
3634 continue;
3635 }
3636
3637 pShwPdpt = pgmPoolGetPage(pPool, pPml4eDst->u & X86_PML4E_PG_MASK);
3638 GCPhysPdptSrc = pPml4eSrc->u & X86_PML4E_PG_MASK_FULL;
3639
3640 if (pPml4eSrc->n.u1Present != pPml4eDst->n.u1Present)
3641 {
3642 AssertMsgFailed(("Present bit doesn't match! pPml4eDst.u=%#RX64 pPml4eSrc.u=%RX64\n", pPml4eDst->u, pPml4eSrc->u));
3643 GCPtr += _2M * UINT64_C(512) * UINT64_C(512);
3644 cErrors++;
3645 continue;
3646 }
3647
3648 if (GCPhysPdptSrc != pShwPdpt->GCPhys)
3649 {
3650 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));
3651 GCPtr += _2M * UINT64_C(512) * UINT64_C(512);
3652 cErrors++;
3653 continue;
3654 }
3655
3656 if ( pPml4eDst->n.u1User != pPml4eSrc->n.u1User
3657 || pPml4eDst->n.u1Write != pPml4eSrc->n.u1Write
3658 || pPml4eDst->n.u1NoExecute != pPml4eSrc->n.u1NoExecute)
3659 {
3660 AssertMsgFailed(("User/Write/NoExec bits don't match! pPml4eDst.u=%#RX64 pPml4eSrc.u=%RX64\n", pPml4eDst->u, pPml4eSrc->u));
3661 GCPtr += _2M * UINT64_C(512) * UINT64_C(512);
3662 cErrors++;
3663 continue;
3664 }
3665# else /* PGM_GST_TYPE != PGM_TYPE_AMD64 */
3666 {
3667# endif /* PGM_GST_TYPE != PGM_TYPE_AMD64 */
3668
3669# if PGM_GST_TYPE == PGM_TYPE_AMD64 || PGM_GST_TYPE == PGM_TYPE_PAE
3670 /*
3671 * Check the PDPTEs too.
3672 */
3673 unsigned iPdpt = (GCPtr >> SHW_PDPT_SHIFT) & SHW_PDPT_MASK;
3674
3675 for (;iPdpt <= SHW_PDPT_MASK; iPdpt++)
3676 {
3677 unsigned iPDSrc;
3678 PPGMPOOLPAGE pShwPde = NULL;
3679 PX86PDPE pPdpeDst;
3680 RTGCPHYS GCPhysPdeSrc;
3681# if PGM_GST_TYPE == PGM_TYPE_PAE
3682 X86PDPE PdpeSrc;
3683 PGSTPD pPDSrc = pgmGstGetPaePDPtr(&pVM->pgm.s, GCPtr, &iPDSrc, &PdpeSrc);
3684 PX86PDPT pPdptDst = pgmShwGetPaePDPTPtr(&pVM->pgm.s);
3685# else
3686 PX86PML4E pPml4eSrc;
3687 X86PDPE PdpeSrc;
3688 PX86PDPT pPdptDst;
3689 PX86PDPAE pPDDst;
3690 PGSTPD pPDSrc = pgmGstGetLongModePDPtr(&pVM->pgm.s, GCPtr, &pPml4eSrc, &PdpeSrc, &iPDSrc);
3691
3692 rc = pgmShwGetLongModePDPtr(pVM, GCPtr, NULL, &pPdptDst, &pPDDst);
3693 if (rc != VINF_SUCCESS)
3694 {
3695 AssertMsg(rc == VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT, ("Unexpected rc=%Rrc\n", rc));
3696 GCPtr += 512 * _2M;
3697 continue; /* next PDPTE */
3698 }
3699 Assert(pPDDst);
3700# endif
3701 Assert(iPDSrc == 0);
3702
3703 pPdpeDst = &pPdptDst->a[iPdpt];
3704
3705 if (!pPdpeDst->n.u1Present)
3706 {
3707 GCPtr += 512 * _2M;
3708 continue; /* next PDPTE */
3709 }
3710
3711 pShwPde = pgmPoolGetPage(pPool, pPdpeDst->u & X86_PDPE_PG_MASK);
3712 GCPhysPdeSrc = PdpeSrc.u & X86_PDPE_PG_MASK;
3713
3714 if (pPdpeDst->n.u1Present != PdpeSrc.n.u1Present)
3715 {
3716 AssertMsgFailed(("Present bit doesn't match! pPdpeDst.u=%#RX64 pPdpeSrc.u=%RX64\n", pPdpeDst->u, PdpeSrc.u));
3717 GCPtr += 512 * _2M;
3718 cErrors++;
3719 continue;
3720 }
3721
3722 if (GCPhysPdeSrc != pShwPde->GCPhys)
3723 {
3724# if PGM_GST_TYPE == PGM_TYPE_AMD64
3725 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));
3726# else
3727 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));
3728# endif
3729 GCPtr += 512 * _2M;
3730 cErrors++;
3731 continue;
3732 }
3733
3734# if PGM_GST_TYPE == PGM_TYPE_AMD64
3735 if ( pPdpeDst->lm.u1User != PdpeSrc.lm.u1User
3736 || pPdpeDst->lm.u1Write != PdpeSrc.lm.u1Write
3737 || pPdpeDst->lm.u1NoExecute != PdpeSrc.lm.u1NoExecute)
3738 {
3739 AssertMsgFailed(("User/Write/NoExec bits don't match! pPdpeDst.u=%#RX64 pPdpeSrc.u=%RX64\n", pPdpeDst->u, PdpeSrc.u));
3740 GCPtr += 512 * _2M;
3741 cErrors++;
3742 continue;
3743 }
3744# endif
3745
3746# else /* PGM_GST_TYPE != PGM_TYPE_AMD64 && PGM_GST_TYPE != PGM_TYPE_PAE */
3747 {
3748# endif /* PGM_GST_TYPE != PGM_TYPE_AMD64 && PGM_GST_TYPE != PGM_TYPE_PAE */
3749# if PGM_GST_TYPE == PGM_TYPE_32BIT
3750 GSTPD const *pPDSrc = pgmGstGet32bitPDPtr(&pVM->pgm.s);
3751# if PGM_SHW_TYPE == PGM_TYPE_32BIT
3752 PCX86PD pPDDst = pgmShwGet32BitPDPtr(&pVM->pgm.s);
3753# endif
3754# endif /* PGM_GST_TYPE == PGM_TYPE_32BIT */
3755 /*
3756 * Iterate the shadow page directory.
3757 */
3758 GCPtr = (GCPtr >> SHW_PD_SHIFT) << SHW_PD_SHIFT;
3759 unsigned iPDDst = (GCPtr >> SHW_PD_SHIFT) & SHW_PD_MASK;
3760
3761 for (;
3762 iPDDst < cPDEs;
3763 iPDDst++, GCPtr += cIncrement)
3764 {
3765# if PGM_SHW_TYPE == PGM_TYPE_PAE
3766 const SHWPDE PdeDst = *pgmShwGetPaePDEPtr(pPGM, GCPtr);
3767# else
3768 const SHWPDE PdeDst = pPDDst->a[iPDDst];
3769# endif
3770 if (PdeDst.u & PGM_PDFLAGS_MAPPING)
3771 {
3772 Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
3773 if ((PdeDst.u & X86_PDE_AVL_MASK) != PGM_PDFLAGS_MAPPING)
3774 {
3775 AssertMsgFailed(("Mapping shall only have PGM_PDFLAGS_MAPPING set! PdeDst.u=%#RX64\n", (uint64_t)PdeDst.u));
3776 cErrors++;
3777 continue;
3778 }
3779 }
3780 else if ( (PdeDst.u & X86_PDE_P)
3781 || ((PdeDst.u & (X86_PDE_P | PGM_PDFLAGS_TRACK_DIRTY)) == (X86_PDE_P | PGM_PDFLAGS_TRACK_DIRTY))
3782 )
3783 {
3784 HCPhysShw = PdeDst.u & SHW_PDE_PG_MASK;
3785 PPGMPOOLPAGE pPoolPage = pgmPoolGetPageByHCPhys(pVM, HCPhysShw);
3786 if (!pPoolPage)
3787 {
3788 AssertMsgFailed(("Invalid page table address %RHp at %RGv! PdeDst=%#RX64\n",
3789 HCPhysShw, GCPtr, (uint64_t)PdeDst.u));
3790 cErrors++;
3791 continue;
3792 }
3793 const SHWPT *pPTDst = (const SHWPT *)PGMPOOL_PAGE_2_PTR(pVM, pPoolPage);
3794
3795 if (PdeDst.u & (X86_PDE4M_PWT | X86_PDE4M_PCD))
3796 {
3797 AssertMsgFailed(("PDE flags PWT and/or PCD is set at %RGv! These flags are not virtualized! PdeDst=%#RX64\n",
3798 GCPtr, (uint64_t)PdeDst.u));
3799 cErrors++;
3800 }
3801
3802 if (PdeDst.u & (X86_PDE4M_G | X86_PDE4M_D))
3803 {
3804 AssertMsgFailed(("4K PDE reserved flags at %RGv! PdeDst=%#RX64\n",
3805 GCPtr, (uint64_t)PdeDst.u));
3806 cErrors++;
3807 }
3808
3809 const GSTPDE PdeSrc = pPDSrc->a[(iPDDst >> (GST_PD_SHIFT - SHW_PD_SHIFT)) & GST_PD_MASK];
3810 if (!PdeSrc.n.u1Present)
3811 {
3812 AssertMsgFailed(("Guest PDE at %RGv is not present! PdeDst=%#RX64 PdeSrc=%#RX64\n",
3813 GCPtr, (uint64_t)PdeDst.u, (uint64_t)PdeSrc.u));
3814 cErrors++;
3815 continue;
3816 }
3817
3818 if ( !PdeSrc.b.u1Size
3819 || !fBigPagesSupported)
3820 {
3821 GCPhysGst = PdeSrc.u & GST_PDE_PG_MASK;
3822# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
3823 GCPhysGst |= (iPDDst & 1) * (PAGE_SIZE / 2);
3824# endif
3825 }
3826 else
3827 {
3828# if PGM_GST_TYPE == PGM_TYPE_32BIT
3829 if (PdeSrc.u & X86_PDE4M_PG_HIGH_MASK)
3830 {
3831 AssertMsgFailed(("Guest PDE at %RGv is using PSE36 or similar! PdeSrc=%#RX64\n",
3832 GCPtr, (uint64_t)PdeSrc.u));
3833 cErrors++;
3834 continue;
3835 }
3836# endif
3837 GCPhysGst = GST_GET_PDE_BIG_PG_GCPHYS(PdeSrc);
3838# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
3839 GCPhysGst |= GCPtr & RT_BIT(X86_PAGE_2M_SHIFT);
3840# endif
3841 }
3842
3843 if ( pPoolPage->enmKind
3844 != (!PdeSrc.b.u1Size || !fBigPagesSupported ? BTH_PGMPOOLKIND_PT_FOR_PT : BTH_PGMPOOLKIND_PT_FOR_BIG))
3845 {
3846 AssertMsgFailed(("Invalid shadow page table kind %d at %RGv! PdeSrc=%#RX64\n",
3847 pPoolPage->enmKind, GCPtr, (uint64_t)PdeSrc.u));
3848 cErrors++;
3849 }
3850
3851 PPGMPAGE pPhysPage = pgmPhysGetPage(pPGM, GCPhysGst);
3852 if (!pPhysPage)
3853 {
3854 AssertMsgFailed(("Cannot find guest physical address %RGp in the PDE at %RGv! PdeSrc=%#RX64\n",
3855 GCPhysGst, GCPtr, (uint64_t)PdeSrc.u));
3856 cErrors++;
3857 continue;
3858 }
3859
3860 if (GCPhysGst != pPoolPage->GCPhys)
3861 {
3862 AssertMsgFailed(("GCPhysGst=%RGp != pPage->GCPhys=%RGp at %RGv\n",
3863 GCPhysGst, pPoolPage->GCPhys, GCPtr));
3864 cErrors++;
3865 continue;
3866 }
3867
3868 if ( !PdeSrc.b.u1Size
3869 || !fBigPagesSupported)
3870 {
3871 /*
3872 * Page Table.
3873 */
3874 const GSTPT *pPTSrc;
3875 rc = PGM_GCPHYS_2_PTR(pVM, GCPhysGst & ~(RTGCPHYS)(PAGE_SIZE - 1), &pPTSrc);
3876 if (RT_FAILURE(rc))
3877 {
3878 AssertMsgFailed(("Cannot map/convert guest physical address %RGp in the PDE at %RGv! PdeSrc=%#RX64\n",
3879 GCPhysGst, GCPtr, (uint64_t)PdeSrc.u));
3880 cErrors++;
3881 continue;
3882 }
3883 if ( (PdeSrc.u & (X86_PDE_P | X86_PDE_US | X86_PDE_RW/* | X86_PDE_A*/))
3884 != (PdeDst.u & (X86_PDE_P | X86_PDE_US | X86_PDE_RW/* | X86_PDE_A*/)))
3885 {
3886 /// @todo We get here a lot on out-of-sync CR3 entries. The access handler should zap them to avoid false alarms here!
3887 // (This problem will go away when/if we shadow multiple CR3s.)
3888 AssertMsgFailed(("4K PDE flags mismatch at %RGv! PdeSrc=%#RX64 PdeDst=%#RX64\n",
3889 GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
3890 cErrors++;
3891 continue;
3892 }
3893 if (PdeDst.u & PGM_PDFLAGS_TRACK_DIRTY)
3894 {
3895 AssertMsgFailed(("4K PDEs cannot have PGM_PDFLAGS_TRACK_DIRTY set! GCPtr=%RGv PdeDst=%#RX64\n",
3896 GCPtr, (uint64_t)PdeDst.u));
3897 cErrors++;
3898 continue;
3899 }
3900
3901 /* iterate the page table. */
3902# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
3903 /* Select the right PDE as we're emulating a 4kb page table with 2 shadow page tables. */
3904 const unsigned offPTSrc = ((GCPtr >> SHW_PD_SHIFT) & 1) * 512;
3905# else
3906 const unsigned offPTSrc = 0;
3907# endif
3908 for (unsigned iPT = 0, off = 0;
3909 iPT < RT_ELEMENTS(pPTDst->a);
3910 iPT++, off += PAGE_SIZE)
3911 {
3912 const SHWPTE PteDst = pPTDst->a[iPT];
3913
3914 /* skip not-present entries. */
3915 if (!(PteDst.u & (X86_PTE_P | PGM_PTFLAGS_TRACK_DIRTY))) /** @todo deal with ALL handlers and CSAM !P pages! */
3916 continue;
3917 Assert(PteDst.n.u1Present);
3918
3919 const GSTPTE PteSrc = pPTSrc->a[iPT + offPTSrc];
3920 if (!PteSrc.n.u1Present)
3921 {
3922# ifdef IN_RING3
3923 PGMAssertHandlerAndFlagsInSync(pVM);
3924 PGMR3DumpHierarchyGC(pVM, cr3, cr4, (PdeSrc.u & GST_PDE_PG_MASK));
3925# endif
3926 AssertMsgFailed(("Out of sync (!P) PTE at %RGv! PteSrc=%#RX64 PteDst=%#RX64 pPTSrc=%RGv iPTSrc=%x PdeSrc=%x physpte=%RGp\n",
3927 GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u, pPTSrc, iPT + offPTSrc, PdeSrc.au32[0],
3928 (PdeSrc.u & GST_PDE_PG_MASK) + (iPT + offPTSrc)*sizeof(PteSrc)));
3929 cErrors++;
3930 continue;
3931 }
3932
3933 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;
3934# if 1 /** @todo sync accessed bit properly... */
3935 fIgnoreFlags |= X86_PTE_A;
3936# endif
3937
3938 /* match the physical addresses */
3939 HCPhysShw = PteDst.u & SHW_PTE_PG_MASK;
3940 GCPhysGst = PteSrc.u & GST_PTE_PG_MASK;
3941
3942# ifdef IN_RING3
3943 rc = PGMPhysGCPhys2HCPhys(pVM, GCPhysGst, &HCPhys);
3944 if (RT_FAILURE(rc))
3945 {
3946 if (HCPhysShw != MMR3PageDummyHCPhys(pVM))
3947 {
3948 AssertMsgFailed(("Cannot find guest physical address %RGp at %RGv! PteSrc=%#RX64 PteDst=%#RX64\n",
3949 GCPhysGst, GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
3950 cErrors++;
3951 continue;
3952 }
3953 }
3954 else if (HCPhysShw != (HCPhys & SHW_PTE_PG_MASK))
3955 {
3956 AssertMsgFailed(("Out of sync (phys) at %RGv! HCPhysShw=%RHp HCPhys=%RHp GCPhysGst=%RGp PteSrc=%#RX64 PteDst=%#RX64\n",
3957 GCPtr + off, HCPhysShw, HCPhys, GCPhysGst, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
3958 cErrors++;
3959 continue;
3960 }
3961# endif
3962
3963 pPhysPage = pgmPhysGetPage(pPGM, GCPhysGst);
3964 if (!pPhysPage)
3965 {
3966# ifdef IN_RING3 /** @todo make MMR3PageDummyHCPhys an 'All' function! */
3967 if (HCPhysShw != MMR3PageDummyHCPhys(pVM))
3968 {
3969 AssertMsgFailed(("Cannot find guest physical address %RGp at %RGv! PteSrc=%#RX64 PteDst=%#RX64\n",
3970 GCPhysGst, GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
3971 cErrors++;
3972 continue;
3973 }
3974# endif
3975 if (PteDst.n.u1Write)
3976 {
3977 AssertMsgFailed(("Invalid guest page at %RGv is writable! GCPhysGst=%RGp PteSrc=%#RX64 PteDst=%#RX64\n",
3978 GCPtr + off, GCPhysGst, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
3979 cErrors++;
3980 }
3981 fIgnoreFlags |= X86_PTE_RW;
3982 }
3983 else if (HCPhysShw != (PGM_PAGE_GET_HCPHYS(pPhysPage) & SHW_PTE_PG_MASK))
3984 {
3985 AssertMsgFailed(("Out of sync (phys) at %RGv! HCPhysShw=%RHp HCPhys=%RHp GCPhysGst=%RGp PteSrc=%#RX64 PteDst=%#RX64\n",
3986 GCPtr + off, HCPhysShw, pPhysPage->HCPhys, GCPhysGst, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
3987 cErrors++;
3988 continue;
3989 }
3990
3991 /* flags */
3992 if (PGM_PAGE_HAS_ACTIVE_HANDLERS(pPhysPage))
3993 {
3994 if (!PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPhysPage))
3995 {
3996 if (PteDst.n.u1Write)
3997 {
3998 AssertMsgFailed(("WRITE access flagged at %RGv but the page is writable! HCPhys=%RHp PteSrc=%#RX64 PteDst=%#RX64\n",
3999 GCPtr + off, pPhysPage->HCPhys, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
4000 cErrors++;
4001 continue;
4002 }
4003 fIgnoreFlags |= X86_PTE_RW;
4004 }
4005 else
4006 {
4007 if (PteDst.n.u1Present)
4008 {
4009 AssertMsgFailed(("ALL access flagged at %RGv but the page is present! HCPhys=%RHp PteSrc=%#RX64 PteDst=%#RX64\n",
4010 GCPtr + off, pPhysPage->HCPhys, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
4011 cErrors++;
4012 continue;
4013 }
4014 fIgnoreFlags |= X86_PTE_P;
4015 }
4016 }
4017 else
4018 {
4019 if (!PteSrc.n.u1Dirty && PteSrc.n.u1Write)
4020 {
4021 if (PteDst.n.u1Write)
4022 {
4023 AssertMsgFailed(("!DIRTY page at %RGv is writable! PteSrc=%#RX64 PteDst=%#RX64\n",
4024 GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
4025 cErrors++;
4026 continue;
4027 }
4028 if (!(PteDst.u & PGM_PTFLAGS_TRACK_DIRTY))
4029 {
4030 AssertMsgFailed(("!DIRTY page at %RGv is not marked TRACK_DIRTY! PteSrc=%#RX64 PteDst=%#RX64\n",
4031 GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
4032 cErrors++;
4033 continue;
4034 }
4035 if (PteDst.n.u1Dirty)
4036 {
4037 AssertMsgFailed(("!DIRTY page at %RGv is marked DIRTY! PteSrc=%#RX64 PteDst=%#RX64\n",
4038 GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
4039 cErrors++;
4040 }
4041# if 0 /** @todo sync access bit properly... */
4042 if (PteDst.n.u1Accessed != PteSrc.n.u1Accessed)
4043 {
4044 AssertMsgFailed(("!DIRTY page at %RGv is has mismatching accessed bit! PteSrc=%#RX64 PteDst=%#RX64\n",
4045 GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
4046 cErrors++;
4047 }
4048 fIgnoreFlags |= X86_PTE_RW;
4049# else
4050 fIgnoreFlags |= X86_PTE_RW | X86_PTE_A;
4051# endif
4052 }
4053 else if (PteDst.u & PGM_PTFLAGS_TRACK_DIRTY)
4054 {
4055 /* access bit emulation (not implemented). */
4056 if (PteSrc.n.u1Accessed || PteDst.n.u1Present)
4057 {
4058 AssertMsgFailed(("PGM_PTFLAGS_TRACK_DIRTY set at %RGv but no accessed bit emulation! PteSrc=%#RX64 PteDst=%#RX64\n",
4059 GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
4060 cErrors++;
4061 continue;
4062 }
4063 if (!PteDst.n.u1Accessed)
4064 {
4065 AssertMsgFailed(("!ACCESSED page at %RGv is has the accessed bit set! PteSrc=%#RX64 PteDst=%#RX64\n",
4066 GCPtr + off, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
4067 cErrors++;
4068 }
4069 fIgnoreFlags |= X86_PTE_P;
4070 }
4071# ifdef DEBUG_sandervl
4072 fIgnoreFlags |= X86_PTE_D | X86_PTE_A;
4073# endif
4074 }
4075
4076 if ( (PteSrc.u & ~fIgnoreFlags) != (PteDst.u & ~fIgnoreFlags)
4077 && (PteSrc.u & ~(fIgnoreFlags | X86_PTE_RW)) != (PteDst.u & ~fIgnoreFlags)
4078 )
4079 {
4080 AssertMsgFailed(("Flags mismatch at %RGv! %#RX64 != %#RX64 fIgnoreFlags=%#RX64 PteSrc=%#RX64 PteDst=%#RX64\n",
4081 GCPtr + off, (uint64_t)PteSrc.u & ~fIgnoreFlags, (uint64_t)PteDst.u & ~fIgnoreFlags,
4082 fIgnoreFlags, (uint64_t)PteSrc.u, (uint64_t)PteDst.u));
4083 cErrors++;
4084 continue;
4085 }
4086 } /* foreach PTE */
4087 }
4088 else
4089 {
4090 /*
4091 * Big Page.
4092 */
4093 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;
4094 if (!PdeSrc.b.u1Dirty && PdeSrc.b.u1Write)
4095 {
4096 if (PdeDst.n.u1Write)
4097 {
4098 AssertMsgFailed(("!DIRTY page at %RGv is writable! PdeSrc=%#RX64 PdeDst=%#RX64\n",
4099 GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
4100 cErrors++;
4101 continue;
4102 }
4103 if (!(PdeDst.u & PGM_PDFLAGS_TRACK_DIRTY))
4104 {
4105 AssertMsgFailed(("!DIRTY page at %RGv is not marked TRACK_DIRTY! PteSrc=%#RX64 PteDst=%#RX64\n",
4106 GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
4107 cErrors++;
4108 continue;
4109 }
4110# if 0 /** @todo sync access bit properly... */
4111 if (PdeDst.n.u1Accessed != PdeSrc.b.u1Accessed)
4112 {
4113 AssertMsgFailed(("!DIRTY page at %RGv is has mismatching accessed bit! PteSrc=%#RX64 PteDst=%#RX64\n",
4114 GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
4115 cErrors++;
4116 }
4117 fIgnoreFlags |= X86_PTE_RW;
4118# else
4119 fIgnoreFlags |= X86_PTE_RW | X86_PTE_A;
4120# endif
4121 }
4122 else if (PdeDst.u & PGM_PDFLAGS_TRACK_DIRTY)
4123 {
4124 /* access bit emulation (not implemented). */
4125 if (PdeSrc.b.u1Accessed || PdeDst.n.u1Present)
4126 {
4127 AssertMsgFailed(("PGM_PDFLAGS_TRACK_DIRTY set at %RGv but no accessed bit emulation! PdeSrc=%#RX64 PdeDst=%#RX64\n",
4128 GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
4129 cErrors++;
4130 continue;
4131 }
4132 if (!PdeDst.n.u1Accessed)
4133 {
4134 AssertMsgFailed(("!ACCESSED page at %RGv is has the accessed bit set! PdeSrc=%#RX64 PdeDst=%#RX64\n",
4135 GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
4136 cErrors++;
4137 }
4138 fIgnoreFlags |= X86_PTE_P;
4139 }
4140
4141 if ((PdeSrc.u & ~fIgnoreFlags) != (PdeDst.u & ~fIgnoreFlags))
4142 {
4143 AssertMsgFailed(("Flags mismatch (B) at %RGv! %#RX64 != %#RX64 fIgnoreFlags=%#RX64 PdeSrc=%#RX64 PdeDst=%#RX64\n",
4144 GCPtr, (uint64_t)PdeSrc.u & ~fIgnoreFlags, (uint64_t)PdeDst.u & ~fIgnoreFlags,
4145 fIgnoreFlags, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
4146 cErrors++;
4147 }
4148
4149 /* iterate the page table. */
4150 for (unsigned iPT = 0, off = 0;
4151 iPT < RT_ELEMENTS(pPTDst->a);
4152 iPT++, off += PAGE_SIZE, GCPhysGst += PAGE_SIZE)
4153 {
4154 const SHWPTE PteDst = pPTDst->a[iPT];
4155
4156 if (PteDst.u & PGM_PTFLAGS_TRACK_DIRTY)
4157 {
4158 AssertMsgFailed(("The PTE at %RGv emulating a 2/4M page is marked TRACK_DIRTY! PdeSrc=%#RX64 PteDst=%#RX64\n",
4159 GCPtr + off, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
4160 cErrors++;
4161 }
4162
4163 /* skip not-present entries. */
4164 if (!PteDst.n.u1Present) /** @todo deal with ALL handlers and CSAM !P pages! */
4165 continue;
4166
4167 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;
4168
4169 /* match the physical addresses */
4170 HCPhysShw = PteDst.u & X86_PTE_PAE_PG_MASK;
4171
4172# ifdef IN_RING3
4173 rc = PGMPhysGCPhys2HCPhys(pVM, GCPhysGst, &HCPhys);
4174 if (RT_FAILURE(rc))
4175 {
4176 if (HCPhysShw != MMR3PageDummyHCPhys(pVM))
4177 {
4178 AssertMsgFailed(("Cannot find guest physical address %RGp at %RGv! PdeSrc=%#RX64 PteDst=%#RX64\n",
4179 GCPhysGst, GCPtr + off, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
4180 cErrors++;
4181 }
4182 }
4183 else if (HCPhysShw != (HCPhys & X86_PTE_PAE_PG_MASK))
4184 {
4185 AssertMsgFailed(("Out of sync (phys) at %RGv! HCPhysShw=%RHp HCPhys=%RHp GCPhysGst=%RGp PdeSrc=%#RX64 PteDst=%#RX64\n",
4186 GCPtr + off, HCPhysShw, HCPhys, GCPhysGst, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
4187 cErrors++;
4188 continue;
4189 }
4190# endif
4191 pPhysPage = pgmPhysGetPage(pPGM, GCPhysGst);
4192 if (!pPhysPage)
4193 {
4194# ifdef IN_RING3 /** @todo make MMR3PageDummyHCPhys an 'All' function! */
4195 if (HCPhysShw != MMR3PageDummyHCPhys(pVM))
4196 {
4197 AssertMsgFailed(("Cannot find guest physical address %RGp at %RGv! PdeSrc=%#RX64 PteDst=%#RX64\n",
4198 GCPhysGst, GCPtr + off, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
4199 cErrors++;
4200 continue;
4201 }
4202# endif
4203 if (PteDst.n.u1Write)
4204 {
4205 AssertMsgFailed(("Invalid guest page at %RGv is writable! GCPhysGst=%RGp PdeSrc=%#RX64 PteDst=%#RX64\n",
4206 GCPtr + off, GCPhysGst, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
4207 cErrors++;
4208 }
4209 fIgnoreFlags |= X86_PTE_RW;
4210 }
4211 else if (HCPhysShw != (pPhysPage->HCPhys & X86_PTE_PAE_PG_MASK))
4212 {
4213 AssertMsgFailed(("Out of sync (phys) at %RGv! HCPhysShw=%RHp HCPhys=%RHp GCPhysGst=%RGp PdeSrc=%#RX64 PteDst=%#RX64\n",
4214 GCPtr + off, HCPhysShw, pPhysPage->HCPhys, GCPhysGst, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
4215 cErrors++;
4216 continue;
4217 }
4218
4219 /* flags */
4220 if (PGM_PAGE_HAS_ACTIVE_HANDLERS(pPhysPage))
4221 {
4222 if (!PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPhysPage))
4223 {
4224 if (PGM_PAGE_GET_HNDL_PHYS_STATE(pPhysPage) != PGM_PAGE_HNDL_PHYS_STATE_DISABLED)
4225 {
4226 if (PteDst.n.u1Write)
4227 {
4228 AssertMsgFailed(("WRITE access flagged at %RGv but the page is writable! HCPhys=%RHp PdeSrc=%#RX64 PteDst=%#RX64\n",
4229 GCPtr + off, pPhysPage->HCPhys, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
4230 cErrors++;
4231 continue;
4232 }
4233 fIgnoreFlags |= X86_PTE_RW;
4234 }
4235 }
4236 else
4237 {
4238 if (PteDst.n.u1Present)
4239 {
4240 AssertMsgFailed(("ALL access flagged at %RGv but the page is present! HCPhys=%RHp PdeSrc=%#RX64 PteDst=%#RX64\n",
4241 GCPtr + off, pPhysPage->HCPhys, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
4242 cErrors++;
4243 continue;
4244 }
4245 fIgnoreFlags |= X86_PTE_P;
4246 }
4247 }
4248
4249 if ( (PdeSrc.u & ~fIgnoreFlags) != (PteDst.u & ~fIgnoreFlags)
4250 && (PdeSrc.u & ~(fIgnoreFlags | X86_PTE_RW)) != (PteDst.u & ~fIgnoreFlags) /* lazy phys handler dereg. */
4251 )
4252 {
4253 AssertMsgFailed(("Flags mismatch (BT) at %RGv! %#RX64 != %#RX64 fIgnoreFlags=%#RX64 PdeSrc=%#RX64 PteDst=%#RX64\n",
4254 GCPtr + off, (uint64_t)PdeSrc.u & ~fIgnoreFlags, (uint64_t)PteDst.u & ~fIgnoreFlags,
4255 fIgnoreFlags, (uint64_t)PdeSrc.u, (uint64_t)PteDst.u));
4256 cErrors++;
4257 continue;
4258 }
4259 } /* for each PTE */
4260 }
4261 }
4262 /* not present */
4263
4264 } /* for each PDE */
4265
4266 } /* for each PDPTE */
4267
4268 } /* for each PML4E */
4269
4270# ifdef DEBUG
4271 if (cErrors)
4272 LogFlow(("AssertCR3: cErrors=%d\n", cErrors));
4273# endif
4274
4275#endif /* GST == 32BIT, PAE or AMD64 */
4276 return cErrors;
4277
4278#endif /* PGM_SHW_TYPE != PGM_TYPE_NESTED && PGM_SHW_TYPE != PGM_TYPE_EPT */
4279}
4280#endif /* VBOX_STRICT */
4281
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