VirtualBox

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

Last change on this file since 41456 was 41456, checked in by vboxsync, 13 years ago

PGMPool: PGMPOOLPAGE adjustments and cleanups, no real code changes intended.

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