VirtualBox

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

Last change on this file since 31463 was 31447, checked in by vboxsync, 14 years ago

InvalidatePage: Drop the unused hint here as we'll be releaseing the AutoSet shortly after returning anyway.

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