VirtualBox

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

Last change on this file since 100594 was 100243, checked in by vboxsync, 18 months ago

VMM/PGM: Nested VMX: bugref:10474 Prevent log flooding just in case.

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