VirtualBox

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

Last change on this file since 72300 was 70977, checked in by vboxsync, 7 years ago

NEM: Working on PGM notifications. bugref:9044

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