VirtualBox

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

Last change on this file since 102075 was 102075, checked in by vboxsync, 7 months ago

scm

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 236.0 KB
Line 
1/* $Id: PGMAllBth.h 102075 2023-11-13 04:00:10Z 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, SLATPTE GstSlatPte);
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# ifndef VBOX_WITH_NEW_LAZY_PAGE_ALLOC
782 Assert(!PGM_PAGE_IS_ZERO(pPage));
783# endif
784 AssertFatalMsg(!PGM_PAGE_IS_BALLOONED(pPage), ("Unexpected ballooned page at %RGp\n", GCPhys));
785# ifdef PGM_WITH_PAGE_ZEROING_DETECTION
786 if ( PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ZERO
787 && (pvFault & X86_PAGE_OFFSET_MASK) == 0
788 && pgmHandlePageZeroingCode(pVCpu, pCtx))
789 {
790 STAM_STATS({ pVCpu->pgmr0.s.pStatTrap0eAttributionR0 = &pVCpu->pgm.s.Stats.StatRZTrap0eTime2PageZeroing; });
791 return VINF_SUCCESS;
792 }
793# endif
794 STAM_STATS({ pVCpu->pgmr0.s.pStatTrap0eAttributionR0 = &pVCpu->pgm.s.Stats.StatRZTrap0eTime2MakeWritable; });
795
796 rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
797 if (rc != VINF_SUCCESS)
798 {
799 AssertMsg(rc == VINF_PGM_SYNC_CR3 || RT_FAILURE(rc), ("%Rrc\n", rc));
800 return rc;
801 }
802 if (RT_UNLIKELY(VM_FF_IS_SET(pVM, VM_FF_PGM_NO_MEMORY)))
803 return VINF_EM_NO_MEMORY;
804 }
805
806# if PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE)
807 /*
808 * Check to see if we need to emulate the instruction if CR0.WP=0.
809 */
810 if ( !(Walk.fEffective & PGM_PTATTRS_W_MASK)
811 && (CPUMGetGuestCR0(pVCpu) & (X86_CR0_WP | X86_CR0_PG)) == X86_CR0_PG
812 && CPUMGetGuestCPL(pVCpu) < 3)
813 {
814 Assert((uErr & (X86_TRAP_PF_RW | X86_TRAP_PF_P)) == (X86_TRAP_PF_RW | X86_TRAP_PF_P));
815
816 /*
817 * The Netware WP0+RO+US hack.
818 *
819 * Netware sometimes(/always?) runs with WP0. It has been observed doing
820 * excessive write accesses to pages which are mapped with US=1 and RW=0
821 * while WP=0. This causes a lot of exits and extremely slow execution.
822 * To avoid trapping and emulating every write here, we change the shadow
823 * page table entry to map it as US=0 and RW=1 until user mode tries to
824 * access it again (see further below). We count these shadow page table
825 * changes so we can avoid having to clear the page pool every time the WP
826 * bit changes to 1 (see PGMCr0WpEnabled()).
827 */
828# if (PGM_GST_TYPE == PGM_TYPE_32BIT || PGM_GST_TYPE == PGM_TYPE_PAE) && 1
829 if ( (Walk.fEffective & (PGM_PTATTRS_W_MASK | PGM_PTATTRS_US_MASK)) == PGM_PTATTRS_US_MASK
830 && (Walk.fBigPage || (GstWalk.Pde.u & X86_PDE_RW))
831 && pVM->cCpus == 1 /* Sorry, no go on SMP. Add CFGM option? */)
832 {
833 Log(("PGM #PF: Netware WP0+RO+US hack: pvFault=%RGp uErr=%#x (big=%d)\n", pvFault, uErr, Walk.fBigPage));
834 rc = pgmShwMakePageSupervisorAndWritable(pVCpu, pvFault, Walk.fBigPage, PGM_MK_PG_IS_WRITE_FAULT);
835 if (rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3)
836 {
837 PGM_INVL_PG(pVCpu, pvFault);
838 pVCpu->pgm.s.cNetwareWp0Hacks++;
839 STAM_STATS({ pVCpu->pgmr0.s.pStatTrap0eAttributionR0 = &pVCpu->pgm.s.Stats.StatRZTrap0eTime2Wp0RoUsHack; });
840 return rc;
841 }
842 AssertMsg(RT_FAILURE_NP(rc), ("%Rrc\n", rc));
843 Log(("pgmShwMakePageSupervisorAndWritable(%RGv) failed with rc=%Rrc - ignored\n", pvFault, rc));
844 }
845# endif
846
847 /* Interpret the access. */
848 rc = VBOXSTRICTRC_TODO(PGMInterpretInstruction(pVCpu, pvFault));
849 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)));
850 if (RT_SUCCESS(rc))
851 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.StatRZTrap0eWPEmulInRZ);
852 else
853 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.StatRZTrap0eWPEmulToR3);
854 STAM_STATS({ pVCpu->pgmr0.s.pStatTrap0eAttributionR0 = &pVCpu->pgm.s.Stats.StatRZTrap0eTime2WPEmulation; });
855 return rc;
856 }
857# endif
858 /// @todo count the above case; else
859 if (uErr & X86_TRAP_PF_US)
860 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,PageOutOfSyncUserWrite));
861 else /* supervisor */
862 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,PageOutOfSyncSupervisorWrite));
863
864 /*
865 * Sync the page.
866 *
867 * Note: Do NOT use PGM_SYNC_NR_PAGES here. That only works if the
868 * page is not present, which is not true in this case.
869 */
870# if PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE)
871 rc = PGM_BTH_NAME(SyncPage)(pVCpu, GstWalk.Pde, pvFault, 1, uErr);
872# else
873 rc = PGM_BTH_NAME(SyncPage)(pVCpu, PdeSrcDummy, pvFault, 1, uErr);
874# endif
875 if (RT_SUCCESS(rc))
876 {
877 /*
878 * Page was successfully synced, return to guest but invalidate
879 * the TLB first as the page is very likely to be in it.
880 */
881# if PGM_SHW_TYPE == PGM_TYPE_EPT
882 HMInvalidatePhysPage(pVM, (RTGCPHYS)pvFault);
883# else
884 PGM_INVL_PG(pVCpu, pvFault);
885# endif
886# ifdef VBOX_STRICT
887 PGMPTWALK GstPageWalk;
888 GstPageWalk.GCPhys = RTGCPHYS_MAX;
889 if (!pVM->pgm.s.fNestedPaging)
890 {
891 rc = PGMGstGetPage(pVCpu, pvFault, &GstPageWalk);
892 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));
893 LogFlow(("Obsolete physical monitor page out of sync %RGv - phys %RGp flags=%08llx\n", pvFault, GstPageWalk.GCPhys, GstPageWalk.fEffective));
894 }
895# if 0 /* Bogus! Triggers incorrectly with w7-64 and later for the SyncPage case: "Pde at %RGv changed behind our back?" */
896 uint64_t fPageShw = 0;
897 rc = PGMShwGetPage(pVCpu, pvFault, &fPageShw, NULL);
898 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 */,
899 ("rc=%Rrc fPageShw=%RX64 GCPhys2=%RGp fPageGst=%RX64 pvFault=%RGv\n", rc, fPageShw, GstPageWalk.GCPhys, fPageGst, pvFault));
900# endif
901# endif /* VBOX_STRICT */
902 STAM_STATS({ pVCpu->pgmr0.s.pStatTrap0eAttributionR0 = &pVCpu->pgm.s.Stats.StatRZTrap0eTime2OutOfSyncHndObs; });
903 return VINF_SUCCESS;
904 }
905 }
906# if PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE)
907 /*
908 * Check for Netware WP0+RO+US hack from above and undo it when user
909 * mode accesses the page again.
910 */
911 else if ( (Walk.fEffective & (PGM_PTATTRS_W_MASK | PGM_PTATTRS_US_MASK)) == PGM_PTATTRS_US_MASK
912 && (Walk.fBigPage || (GstWalk.Pde.u & X86_PDE_RW))
913 && pVCpu->pgm.s.cNetwareWp0Hacks > 0
914 && (CPUMGetGuestCR0(pVCpu) & (X86_CR0_WP | X86_CR0_PG)) == X86_CR0_PG
915 && CPUMGetGuestCPL(pVCpu) == 3
916 && pVM->cCpus == 1
917 )
918 {
919 Log(("PGM #PF: Undo netware WP0+RO+US hack: pvFault=%RGp uErr=%#x\n", pvFault, uErr));
920 rc = PGM_BTH_NAME(SyncPage)(pVCpu, GstWalk.Pde, pvFault, 1, uErr);
921 if (RT_SUCCESS(rc))
922 {
923 PGM_INVL_PG(pVCpu, pvFault);
924 pVCpu->pgm.s.cNetwareWp0Hacks--;
925 STAM_STATS({ pVCpu->pgmr0.s.pStatTrap0eAttributionR0 = &pVCpu->pgm.s.Stats.StatRZTrap0eTime2Wp0RoUsUnhack; });
926 return VINF_SUCCESS;
927 }
928 }
929# endif /* PGM_WITH_PAGING */
930
931 /** @todo else: why are we here? */
932
933# if PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE) && defined(VBOX_STRICT)
934 /*
935 * Check for VMM page flags vs. Guest page flags consistency.
936 * Currently only for debug purposes.
937 */
938 if (RT_SUCCESS(rc))
939 {
940 /* Get guest page flags. */
941 PGMPTWALK GstPageWalk;
942 int rc2 = PGMGstGetPage(pVCpu, pvFault, &GstPageWalk);
943 if (RT_SUCCESS(rc2))
944 {
945 uint64_t fPageShw = 0;
946 rc2 = PGMShwGetPage(pVCpu, pvFault, &fPageShw, NULL);
947
948#if 0
949 /*
950 * Compare page flags.
951 * Note: we have AVL, A, D bits desynced.
952 */
953 AssertMsg( (fPageShw & ~(X86_PTE_A | X86_PTE_D | X86_PTE_AVL_MASK))
954 == (fPageGst & ~(X86_PTE_A | X86_PTE_D | X86_PTE_AVL_MASK))
955 || ( pVCpu->pgm.s.cNetwareWp0Hacks > 0
956 && (fPageShw & ~(X86_PTE_A | X86_PTE_D | X86_PTE_AVL_MASK | X86_PTE_RW | X86_PTE_US))
957 == (fPageGst & ~(X86_PTE_A | X86_PTE_D | X86_PTE_AVL_MASK | X86_PTE_RW | X86_PTE_US))
958 && (fPageShw & (X86_PTE_RW | X86_PTE_US)) == X86_PTE_RW
959 && (fPageGst & (X86_PTE_RW | X86_PTE_US)) == X86_PTE_US),
960 ("Page flags mismatch! pvFault=%RGv uErr=%x GCPhys=%RGp fPageShw=%RX64 fPageGst=%RX64 rc=%d\n",
961 pvFault, (uint32_t)uErr, GCPhys, fPageShw, fPageGst, rc));
96201: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)
96301: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
964
96501: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)
96601:01:15.625516 00:08:43.268051 Location :
967e:\vbox\svn\trunk\srcPage flags mismatch!
968pvFault=fffff801b0d7b000
969 uErr=11 X86_TRAP_PF_ID | X86_TRAP_PF_P
970GCPhys=0000000019b52000
971fPageShw=0
972fPageGst=77b0000000000121
973rc=0
974#endif
975
976 }
977 else
978 AssertMsgFailed(("PGMGstGetPage rc=%Rrc\n", rc));
979 }
980 else
981 AssertMsgFailed(("PGMGCGetPage rc=%Rrc\n", rc));
982# endif /* PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE) && VBOX_STRICT */
983 }
984
985
986 /*
987 * If we get here it is because something failed above, i.e. most like guru
988 * meditiation time.
989 */
990 LogRel(("%s: returns rc=%Rrc pvFault=%RGv uErr=%RX64 cs:rip=%04x:%08RX64\n",
991 __PRETTY_FUNCTION__, rc, pvFault, (uint64_t)uErr, pCtx->cs.Sel, pCtx->rip));
992 return rc;
993
994# else /* Nested paging, EPT except PGM_GST_TYPE = PROT, NONE. */
995 NOREF(uErr); NOREF(pCtx); NOREF(pvFault);
996 AssertReleaseMsgFailed(("Shw=%d Gst=%d is not implemented!\n", PGM_SHW_TYPE, PGM_GST_TYPE));
997 return VERR_PGM_NOT_USED_IN_MODE;
998# endif
999}
1000
1001
1002# if defined(VBOX_WITH_NESTED_HWVIRT_VMX_EPT)
1003/**
1004 * Deals with a nested-guest \#PF fault for a guest-physical page with a handler.
1005 *
1006 * @returns Strict VBox status code.
1007 * @param pVCpu The cross context virtual CPU structure.
1008 * @param uErr The error code.
1009 * @param pCtx Pointer to the register context for the CPU.
1010 * @param GCPhysNestedFault The nested-guest physical address of the fault.
1011 * @param pPage The guest page at @a GCPhysNestedFault.
1012 * @param GCPhysFault The guest-physical address of the fault.
1013 * @param pGstWalkAll The guest page walk result.
1014 * @param pfLockTaken Where to store whether the PGM is still held when
1015 * this function completes.
1016 *
1017 * @note The caller has taken the PGM lock.
1018 */
1019static VBOXSTRICTRC PGM_BTH_NAME(NestedTrap0eHandlerDoAccessHandlers)(PVMCPUCC pVCpu, RTGCUINT uErr, PCPUMCTX pCtx,
1020 RTGCPHYS GCPhysNestedFault, PPGMPAGE pPage,
1021 RTGCPHYS GCPhysFault, PPGMPTWALKGST pGstWalkAll,
1022 bool *pfLockTaken)
1023{
1024# if PGM_GST_TYPE == PGM_TYPE_PROT \
1025 && PGM_SHW_TYPE == PGM_TYPE_EPT
1026
1027 /** @todo Assert uErr isn't X86_TRAP_PF_RSVD and remove release checks. */
1028 PGM_A20_ASSERT_MASKED(pVCpu, GCPhysFault);
1029 AssertMsgReturn(PGM_PAGE_HAS_ANY_PHYSICAL_HANDLERS(pPage), ("%RGp %RGp uErr=%u\n", GCPhysNestedFault, GCPhysFault, uErr),
1030 VERR_PGM_HANDLER_IPE_1);
1031
1032 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
1033 RTGCPHYS const GCPhysNestedPage = GCPhysNestedFault & ~(RTGCPHYS)GUEST_PAGE_OFFSET_MASK;
1034 RTGCPHYS const GCPhysPage = GCPhysFault & ~(RTGCPHYS)GUEST_PAGE_OFFSET_MASK;
1035
1036 /*
1037 * Physical page access handler.
1038 */
1039 PPGMPHYSHANDLER pCur;
1040 VBOXSTRICTRC rcStrict = pgmHandlerPhysicalLookup(pVM, GCPhysPage, &pCur);
1041 AssertRCReturn(VBOXSTRICTRC_VAL(rcStrict), rcStrict);
1042
1043 PCPGMPHYSHANDLERTYPEINT const pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
1044 Assert(pCurType);
1045
1046 /*
1047 * If the region is write protected and we got a page not present fault, then sync
1048 * the pages. If the fault was caused by a read, then restart the instruction.
1049 * In case of write access continue to the GC write handler.
1050 */
1051 if ( !(uErr & X86_TRAP_PF_P)
1052 && pCurType->enmKind == PGMPHYSHANDLERKIND_WRITE)
1053 {
1054 Log7Func(("Syncing Monitored: GCPhysNestedPage=%RGp GCPhysPage=%RGp uErr=%#x\n", GCPhysNestedPage, GCPhysPage, uErr));
1055 rcStrict = PGM_BTH_NAME(NestedSyncPage)(pVCpu, GCPhysNestedPage, GCPhysPage, 1 /*cPages*/, uErr, pGstWalkAll);
1056 Assert(rcStrict != VINF_PGM_SYNCPAGE_MODIFIED_PDE);
1057 if ( RT_FAILURE(rcStrict)
1058 || !(uErr & X86_TRAP_PF_RW))
1059 {
1060 AssertMsgRC(rcStrict, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
1061 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.StatRZTrap0eHandlersOutOfSync);
1062 STAM_STATS({ pVCpu->pgmr0.s.pStatTrap0eAttributionR0 = &pVCpu->pgm.s.Stats.StatRZTrap0eTime2OutOfSyncHndPhys; });
1063 return rcStrict;
1064 }
1065 }
1066 else if ( !(uErr & X86_TRAP_PF_RSVD)
1067 && pCurType->enmKind != PGMPHYSHANDLERKIND_WRITE)
1068 {
1069 /*
1070 * If the access was NOT through an EPT misconfig (i.e. RSVD), sync the page.
1071 * This can happen for the VMX APIC-access page.
1072 */
1073 Log7Func(("Syncing MMIO: GCPhysNestedPage=%RGp GCPhysPage=%RGp\n", GCPhysNestedPage, GCPhysPage));
1074 rcStrict = PGM_BTH_NAME(NestedSyncPage)(pVCpu, GCPhysNestedPage, GCPhysPage, 1 /*cPages*/, uErr, pGstWalkAll);
1075 Assert(rcStrict != VINF_PGM_SYNCPAGE_MODIFIED_PDE);
1076 if (RT_FAILURE(rcStrict))
1077 {
1078 AssertMsgRC(rcStrict, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
1079 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.StatRZTrap0eHandlersOutOfSync);
1080 STAM_STATS({ pVCpu->pgmr0.s.pStatTrap0eAttributionR0 = &pVCpu->pgm.s.Stats.StatRZTrap0eTime2OutOfSyncHndPhys; });
1081 return rcStrict;
1082 }
1083 }
1084
1085 AssertMsg( pCurType->enmKind != PGMPHYSHANDLERKIND_WRITE
1086 || (pCurType->enmKind == PGMPHYSHANDLERKIND_WRITE && (uErr & X86_TRAP_PF_RW)),
1087 ("Unexpected trap for physical handler: %08X (phys=%08x) pPage=%R[pgmpage] uErr=%X, enmKind=%d\n",
1088 GCPhysNestedFault, GCPhysFault, pPage, uErr, pCurType->enmKind));
1089 if (pCurType->enmKind == PGMPHYSHANDLERKIND_WRITE)
1090 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.StatRZTrap0eHandlersPhysWrite);
1091 else
1092 {
1093 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.StatRZTrap0eHandlersPhysAll);
1094 if (uErr & X86_TRAP_PF_RSVD)
1095 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.StatRZTrap0eHandlersPhysAllOpt);
1096 }
1097
1098 if (pCurType->pfnPfHandler)
1099 {
1100 STAM_PROFILE_START(&pCur->Stat, h);
1101 uint64_t const uUser = !pCurType->fRing0DevInsIdx ? pCur->uUser
1102 : (uintptr_t)PDMDeviceRing0IdxToInstance(pVM, pCur->uUser);
1103
1104 if (pCurType->fKeepPgmLock)
1105 {
1106 rcStrict = pCurType->pfnPfHandler(pVM, pVCpu, uErr, pCtx, GCPhysNestedFault, GCPhysFault, uUser);
1107 STAM_PROFILE_STOP(&pCur->Stat, h);
1108 }
1109 else
1110 {
1111 PGM_UNLOCK(pVM);
1112 *pfLockTaken = false;
1113 rcStrict = pCurType->pfnPfHandler(pVM, pVCpu, uErr, pCtx, GCPhysNestedFault, GCPhysFault, uUser);
1114 STAM_PROFILE_STOP(&pCur->Stat, h); /* no locking needed, entry is unlikely reused before we get here. */
1115 }
1116 }
1117 else
1118 {
1119 AssertMsgFailed(("What's going on here!? Fault falls outside handler range!?\n"));
1120 rcStrict = VINF_EM_RAW_EMULATE_INSTR;
1121 }
1122
1123 STAM_STATS({ pVCpu->pgmr0.s.pStatTrap0eAttributionR0 = &pVCpu->pgm.s.Stats.StatRZTrap0eTime2HndPhys; });
1124 return rcStrict;
1125
1126# else
1127 RT_NOREF8(pVCpu, uErr, pCtx, GCPhysNestedFault, pPage, GCPhysFault, pGstWalkAll, pfLockTaken);
1128 AssertReleaseMsgFailed(("Shw=%d Gst=%d is not implemented!\n", PGM_SHW_TYPE, PGM_GST_TYPE));
1129 return VERR_PGM_NOT_USED_IN_MODE;
1130# endif
1131}
1132# endif /* VBOX_WITH_NESTED_HWVIRT_VMX_EPT */
1133
1134
1135/**
1136 * Nested \#PF handler for nested-guest hardware-assisted execution using nested
1137 * paging.
1138 *
1139 * @returns VBox status code (appropriate for trap handling and GC return).
1140 * @param pVCpu The cross context virtual CPU structure.
1141 * @param uErr The fault error (X86_TRAP_PF_*).
1142 * @param pCtx Pointer to the register context for the CPU.
1143 * @param GCPhysNestedFault The nested-guest physical address of the fault.
1144 * @param fIsLinearAddrValid Whether translation of a nested-guest linear address
1145 * caused this fault. If @c false, GCPtrNestedFault
1146 * must be 0.
1147 * @param GCPtrNestedFault The nested-guest linear address of this fault.
1148 * @param pWalk The guest page table walk result.
1149 * @param pfLockTaken Where to store whether the PGM lock is still held
1150 * when this function completes.
1151 */
1152PGM_BTH_DECL(int, NestedTrap0eHandler)(PVMCPUCC pVCpu, RTGCUINT uErr, PCPUMCTX pCtx, RTGCPHYS GCPhysNestedFault,
1153 bool fIsLinearAddrValid, RTGCPTR GCPtrNestedFault, PPGMPTWALK pWalk, bool *pfLockTaken)
1154{
1155 *pfLockTaken = false;
1156# if defined(VBOX_WITH_NESTED_HWVIRT_VMX_EPT) \
1157 && PGM_GST_TYPE == PGM_TYPE_PROT \
1158 && PGM_SHW_TYPE == PGM_TYPE_EPT
1159
1160 Assert(CPUMIsGuestVmxEptPagingEnabled(pVCpu));
1161 Assert(PGM_A20_IS_ENABLED(pVCpu));
1162
1163 /* We don't support mode-based execute control for EPT yet. */
1164 Assert(!pVCpu->CTX_SUFF(pVM)->cpum.ro.GuestFeatures.fVmxModeBasedExecuteEpt);
1165 Assert(!(uErr & X86_TRAP_PF_US));
1166
1167 /* Take the big lock now. */
1168 *pfLockTaken = true;
1169 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
1170 PGM_LOCK_VOID(pVM);
1171
1172 /*
1173 * Walk the guest EPT tables and check if it's an EPT violation or misconfiguration.
1174 */
1175 if (fIsLinearAddrValid)
1176 Log7Func(("cs:rip=%04x:%#08RX64 GCPhysNestedFault=%RGp uErr=%#x GCPtrNestedFault=%RGv\n",
1177 pCtx->cs.Sel, pCtx->rip, GCPhysNestedFault, uErr, GCPtrNestedFault));
1178 else
1179 Log7Func(("cs:rip=%04x:%#08RX64 GCPhysNestedFault=%RGp uErr=%#x\n",
1180 pCtx->cs.Sel, pCtx->rip, GCPhysNestedFault, uErr));
1181 PGMPTWALKGST GstWalkAll;
1182 int rc = pgmGstSlatWalk(pVCpu, GCPhysNestedFault, fIsLinearAddrValid, GCPtrNestedFault, pWalk, &GstWalkAll);
1183 if (RT_FAILURE(rc))
1184 return rc;
1185
1186 Assert(GstWalkAll.enmType == PGMPTWALKGSTTYPE_EPT);
1187 Assert(pWalk->fSucceeded);
1188 Assert(pWalk->fEffective & (PGM_PTATTRS_EPT_R_MASK | PGM_PTATTRS_EPT_W_MASK | PGM_PTATTRS_EPT_X_SUPER_MASK));
1189 Assert(pWalk->fIsSlat);
1190
1191# ifdef DEBUG_ramshankar
1192 /* Paranoia. */
1193 Assert(RT_BOOL(pWalk->fEffective & PGM_PTATTRS_R_MASK) == RT_BOOL(pWalk->fEffective & PGM_PTATTRS_EPT_R_MASK));
1194 Assert(RT_BOOL(pWalk->fEffective & PGM_PTATTRS_W_MASK) == RT_BOOL(pWalk->fEffective & PGM_PTATTRS_EPT_W_MASK));
1195 Assert(RT_BOOL(pWalk->fEffective & PGM_PTATTRS_NX_MASK) == !RT_BOOL(pWalk->fEffective & PGM_PTATTRS_EPT_X_SUPER_MASK));
1196# endif
1197
1198 Log7Func(("SLAT: GCPhysNestedFault=%RGp -> GCPhys=%#RGp\n", GCPhysNestedFault, pWalk->GCPhys));
1199
1200 /*
1201 * Check page-access permissions.
1202 */
1203 if ( ((uErr & X86_TRAP_PF_RW) && !(pWalk->fEffective & PGM_PTATTRS_W_MASK))
1204 || ((uErr & X86_TRAP_PF_ID) && (pWalk->fEffective & PGM_PTATTRS_NX_MASK)))
1205 {
1206 Log7Func(("Permission failed! GCPtrNested=%RGv GCPhysNested=%RGp uErr=%#x fEffective=%#RX64\n", GCPtrNestedFault,
1207 GCPhysNestedFault, uErr, pWalk->fEffective));
1208 pWalk->fFailed = PGM_WALKFAIL_EPT_VIOLATION;
1209 return VERR_ACCESS_DENIED;
1210 }
1211
1212 PGM_A20_ASSERT_MASKED(pVCpu, pWalk->GCPhys);
1213 RTGCPHYS const GCPhysPage = pWalk->GCPhys & ~(RTGCPHYS)GUEST_PAGE_OFFSET_MASK;
1214 RTGCPHYS const GCPhysNestedPage = GCPhysNestedFault & ~(RTGCPHYS)GUEST_PAGE_OFFSET_MASK;
1215
1216 /*
1217 * If we were called via an EPT misconfig, it should've already resulted in a nested-guest VM-exit.
1218 */
1219 AssertMsgReturn(!(uErr & X86_TRAP_PF_RSVD),
1220 ("Unexpected EPT misconfig VM-exit. GCPhysPage=%RGp GCPhysNestedPage=%RGp\n", GCPhysPage, GCPhysNestedPage),
1221 VERR_PGM_MAPPING_IPE);
1222
1223 /*
1224 * Fetch and sync the nested-guest EPT page directory pointer.
1225 */
1226 PEPTPD pEptPd;
1227 rc = pgmShwGetNestedEPTPDPtr(pVCpu, GCPhysNestedPage, NULL /*ppPdpt*/, &pEptPd, &GstWalkAll);
1228 AssertRCReturn(rc, rc);
1229 Assert(pEptPd);
1230
1231 /*
1232 * A common case is the not-present error caused by lazy page table syncing.
1233 *
1234 * It is IMPORTANT that we weed out any access to non-present shadow PDEs
1235 * here so we can safely assume that the shadow PT is present when calling
1236 * NestedSyncPage later.
1237 *
1238 * NOTE: It's possible we will be syncing the VMX APIC-access page here.
1239 * In that case, we would sync the page but will NOT go ahead with emulating
1240 * the APIC-access VM-exit through IEM. However, once the page is mapped in
1241 * the shadow tables, subsequent APIC-access VM-exits for the nested-guest
1242 * will be triggered by hardware. Maybe calling the IEM #PF handler can be
1243 * considered as an optimization later.
1244 */
1245 unsigned const iPde = (GCPhysNestedPage >> SHW_PD_SHIFT) & SHW_PD_MASK;
1246 if ( !(uErr & X86_TRAP_PF_P)
1247 && !(pEptPd->a[iPde].u & EPT_PRESENT_MASK))
1248 {
1249 STAM_STATS({ pVCpu->pgmr0.s.pStatTrap0eAttributionR0 = &pVCpu->pgm.s.Stats.StatRZTrap0eTime2SyncPT; });
1250 Log7Func(("NestedSyncPT: Lazy. GCPhysNestedPage=%RGp GCPhysPage=%RGp\n", GCPhysNestedPage, GCPhysPage));
1251 rc = PGM_BTH_NAME(NestedSyncPT)(pVCpu, GCPhysNestedPage, GCPhysPage, &GstWalkAll);
1252 if (RT_SUCCESS(rc))
1253 return rc;
1254 AssertMsgFailedReturn(("NestedSyncPT: %RGv failed! rc=%Rrc\n", GCPhysNestedPage, rc), VERR_PGM_MAPPING_IPE);
1255 }
1256
1257 /*
1258 * Check if this fault address is flagged for special treatment.
1259 * This handles faults on an MMIO or write-monitored page.
1260 *
1261 * If this happens to be the VMX APIC-access page, we don't treat is as MMIO
1262 * but rather sync it further below (as a regular guest page) which lets
1263 * hardware-assisted execution trigger the APIC-access VM-exits of the
1264 * nested-guest directly.
1265 */
1266 PPGMPAGE pPage;
1267 rc = pgmPhysGetPageEx(pVM, GCPhysPage, &pPage);
1268 if (RT_FAILURE(rc))
1269 {
1270 /*
1271 * We failed to get the physical page which means it's a reserved/invalid
1272 * page address (not MMIO even). This can typically be observed with
1273 * Microsoft Hyper-V enabled Windows guests. We must fall back to emulating
1274 * the instruction, see @bugref{10318#c7}.
1275 */
1276 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.StatRZTrap0eHandlersInvalid);
1277 STAM_STATS({ pVCpu->pgmr0.s.pStatTrap0eAttributionR0 = &pVCpu->pgm.s.Stats.StatRZTrap0eTime2InvalidPhys; });
1278 return VINF_EM_RAW_EMULATE_INSTR;
1279 }
1280 /* Check if this is an MMIO page and NOT the VMX APIC-access page. */
1281 if (PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage) && !PGM_PAGE_IS_HNDL_PHYS_NOT_IN_HM(pPage))
1282 {
1283 Log7Func(("MMIO: Calling NestedTrap0eHandlerDoAccessHandlers for GCPhys %RGp\n", GCPhysPage));
1284 return VBOXSTRICTRC_TODO(PGM_BTH_NAME(NestedTrap0eHandlerDoAccessHandlers)(pVCpu, uErr, pCtx, GCPhysNestedFault,
1285 pPage, pWalk->GCPhys, &GstWalkAll,
1286 pfLockTaken));
1287 }
1288
1289 /*
1290 * We are here only if page is present in nested-guest page tables but the
1291 * trap is not handled by our handlers. Check for page out-of-sync situation.
1292 */
1293 if (!(uErr & X86_TRAP_PF_P))
1294 {
1295 Assert(!PGM_PAGE_IS_BALLOONED(pPage));
1296 Assert(!(uErr & X86_TRAP_PF_US)); /* Mode-based execute not supported yet. */
1297 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,PageOutOfSyncSupervisor));
1298
1299 Log7Func(("SyncPage: Not-Present: GCPhysNestedPage=%RGp GCPhysPage=%RGp\n", GCPhysNestedFault, GCPhysPage));
1300 rc = PGM_BTH_NAME(NestedSyncPage)(pVCpu, GCPhysNestedPage, GCPhysPage, PGM_SYNC_NR_PAGES, uErr, &GstWalkAll);
1301 if (RT_SUCCESS(rc))
1302 {
1303 STAM_STATS({ pVCpu->pgmr0.s.pStatTrap0eAttributionR0 = &pVCpu->pgm.s.Stats.StatRZTrap0eTime2OutOfSync; });
1304 return VINF_SUCCESS;
1305 }
1306 }
1307 else if (uErr & X86_TRAP_PF_RW)
1308 {
1309 /*
1310 * Write protected pages are made writable when the guest makes the
1311 * first write to it. This happens for pages that are shared, write
1312 * monitored or not yet allocated.
1313 *
1314 * We may also end up here when CR0.WP=0 in the guest.
1315 *
1316 * Also, a side effect of not flushing global PDEs are out of sync
1317 * pages due to physical monitored regions, that are no longer valid.
1318 * Assume for now it only applies to the read/write flag.
1319 */
1320 if (PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED)
1321 {
1322 /* This is a read-only page. */
1323 AssertFatalMsg(!PGM_PAGE_IS_BALLOONED(pPage), ("Unexpected ballooned page at %RGp\n", GCPhysPage));
1324#ifdef PGM_WITH_PAGE_ZEROING_DETECTION
1325 if ( PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ZERO
1326 && (GCPhysNestedFault & X86_PAGE_OFFSET_MASK) == 0
1327 && pgmHandlePageZeroingCode(pVCpu, pCtx))
1328 {
1329 STAM_STATS({ pVCpu->pgmr0.s.pStatTrap0eAttributionR0 = &pVCpu->pgm.s.Stats.StatRZTrap0eTime2PageZeroing; });
1330 return VINF_SUCCESS;
1331 }
1332#endif
1333 STAM_STATS({ pVCpu->pgmr0.s.pStatTrap0eAttributionR0 = &pVCpu->pgm.s.Stats.StatRZTrap0eTime2MakeWritable; });
1334
1335 Log7Func(("Calling pgmPhysPageMakeWritable for GCPhysPage=%RGp\n", GCPhysPage));
1336 rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhysPage);
1337 if (rc != VINF_SUCCESS)
1338 {
1339 AssertMsg(rc == VINF_PGM_SYNC_CR3 || RT_FAILURE(rc), ("%Rrc\n", rc));
1340 return rc;
1341 }
1342 if (RT_UNLIKELY(VM_FF_IS_SET(pVM, VM_FF_PGM_NO_MEMORY)))
1343 return VINF_EM_NO_MEMORY;
1344 }
1345
1346 Assert(!(uErr & X86_TRAP_PF_US)); /* Mode-based execute not supported yet. */
1347 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,PageOutOfSyncSupervisorWrite));
1348
1349 /*
1350 * Sync the write-protected page.
1351 * Note: Do NOT use PGM_SYNC_NR_PAGES here. That only works if the
1352 * page is not present, which is not true in this case.
1353 */
1354 Log7Func(("SyncPage: RW: cs:rip=%04x:%#RX64 GCPhysNestedPage=%RGp uErr=%#RX32 GCPhysPage=%RGp WalkGCPhys=%RGp\n",
1355 pCtx->cs.Sel, pCtx->rip, GCPhysNestedPage, (uint32_t)uErr, GCPhysPage, pWalk->GCPhys));
1356 rc = PGM_BTH_NAME(NestedSyncPage)(pVCpu, GCPhysNestedPage, GCPhysPage, 1 /* cPages */, uErr, &GstWalkAll);
1357 if (RT_SUCCESS(rc))
1358 {
1359 HMInvalidatePhysPage(pVM, GCPhysPage);
1360 STAM_STATS({ pVCpu->pgmr0.s.pStatTrap0eAttributionR0 = &pVCpu->pgm.s.Stats.StatRZTrap0eTime2OutOfSyncHndObs; });
1361 return VINF_SUCCESS;
1362 }
1363 }
1364
1365 /*
1366 * If we get here it is because something failed above => guru meditation time?
1367 */
1368 LogRelMaxFunc(32, ("rc=%Rrc GCPhysNestedFault=%#RGp (%#RGp) uErr=%#RX32 cs:rip=%04x:%08RX64\n",
1369 rc, GCPhysNestedFault, GCPhysPage, (uint32_t)uErr, pCtx->cs.Sel, pCtx->rip));
1370 return VERR_PGM_MAPPING_IPE;
1371
1372# else /* !VBOX_WITH_NESTED_HWVIRT_VMX_EPT || PGM_GST_TYPE != PGM_TYPE_PROT || PGM_SHW_TYPE != PGM_TYPE_EPT */
1373 RT_NOREF7(pVCpu, uErr, pCtx, GCPhysNestedFault, fIsLinearAddrValid, GCPtrNestedFault, pWalk);
1374 AssertReleaseMsgFailed(("Shw=%d Gst=%d is not implemented!\n", PGM_SHW_TYPE, PGM_GST_TYPE));
1375 return VERR_PGM_NOT_USED_IN_MODE;
1376# endif
1377}
1378
1379#endif /* !IN_RING3 */
1380
1381
1382/**
1383 * Emulation of the invlpg instruction.
1384 *
1385 *
1386 * @returns VBox status code.
1387 *
1388 * @param pVCpu The cross context virtual CPU structure.
1389 * @param GCPtrPage Page to invalidate.
1390 *
1391 * @remark ASSUMES that the guest is updating before invalidating. This order
1392 * isn't required by the CPU, so this is speculative and could cause
1393 * trouble.
1394 * @remark No TLB shootdown is done on any other VCPU as we assume that
1395 * invlpg emulation is the *only* reason for calling this function.
1396 * (The guest has to shoot down TLB entries on other CPUs itself)
1397 * Currently true, but keep in mind!
1398 *
1399 * @todo Clean this up! Most of it is (or should be) no longer necessary as we catch all page table accesses.
1400 * Should only be required when PGMPOOL_WITH_OPTIMIZED_DIRTY_PT is active (PAE or AMD64 (for now))
1401 */
1402PGM_BTH_DECL(int, InvalidatePage)(PVMCPUCC pVCpu, RTGCPTR GCPtrPage)
1403{
1404#if PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE) \
1405 && !PGM_TYPE_IS_NESTED_OR_EPT(PGM_SHW_TYPE) \
1406 && PGM_SHW_TYPE != PGM_TYPE_NONE
1407 int rc;
1408 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
1409 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
1410
1411 PGM_LOCK_ASSERT_OWNER(pVM);
1412
1413 LogFlow(("InvalidatePage %RGv\n", GCPtrPage));
1414
1415 /*
1416 * Get the shadow PD entry and skip out if this PD isn't present.
1417 * (Guessing that it is frequent for a shadow PDE to not be present, do this first.)
1418 */
1419# if PGM_SHW_TYPE == PGM_TYPE_32BIT
1420 const unsigned iPDDst = (uint32_t)GCPtrPage >> SHW_PD_SHIFT;
1421 PX86PDE pPdeDst = pgmShwGet32BitPDEPtr(pVCpu, GCPtrPage);
1422
1423 /* Fetch the pgm pool shadow descriptor. */
1424 PPGMPOOLPAGE pShwPde = pVCpu->pgm.s.CTX_SUFF(pShwPageCR3);
1425# ifdef IN_RING3 /* Possible we didn't resync yet when called from REM. */
1426 if (!pShwPde)
1427 {
1428 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,InvalidatePageSkipped));
1429 return VINF_SUCCESS;
1430 }
1431# else
1432 Assert(pShwPde);
1433# endif
1434
1435# elif PGM_SHW_TYPE == PGM_TYPE_PAE
1436 const unsigned iPdpt = (uint32_t)GCPtrPage >> X86_PDPT_SHIFT;
1437 PX86PDPT pPdptDst = pgmShwGetPaePDPTPtr(pVCpu);
1438
1439 /* If the shadow PDPE isn't present, then skip the invalidate. */
1440# ifdef IN_RING3 /* Possible we didn't resync yet when called from REM. */
1441 if (!pPdptDst || !(pPdptDst->a[iPdpt].u & X86_PDPE_P))
1442# else
1443 if (!(pPdptDst->a[iPdpt].u & X86_PDPE_P))
1444# endif
1445 {
1446 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,InvalidatePageSkipped));
1447 PGM_INVL_PG(pVCpu, GCPtrPage);
1448 return VINF_SUCCESS;
1449 }
1450
1451 /* Fetch the pgm pool shadow descriptor. */
1452 PPGMPOOLPAGE pShwPde = pgmPoolGetPage(pPool, pPdptDst->a[iPdpt].u & X86_PDPE_PG_MASK);
1453 AssertReturn(pShwPde, VERR_PGM_POOL_GET_PAGE_FAILED);
1454
1455 PX86PDPAE pPDDst = (PX86PDPAE)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPde);
1456 const unsigned iPDDst = (GCPtrPage >> SHW_PD_SHIFT) & SHW_PD_MASK;
1457 PX86PDEPAE pPdeDst = &pPDDst->a[iPDDst];
1458
1459# else /* PGM_SHW_TYPE == PGM_TYPE_AMD64 */
1460 /* PML4 */
1461 /*const unsigned iPml4 = (GCPtrPage >> X86_PML4_SHIFT) & X86_PML4_MASK;*/
1462 const unsigned iPdpt = (GCPtrPage >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
1463 const unsigned iPDDst = (GCPtrPage >> SHW_PD_SHIFT) & SHW_PD_MASK;
1464 PX86PDPAE pPDDst;
1465 PX86PDPT pPdptDst;
1466 PX86PML4E pPml4eDst;
1467 rc = pgmShwGetLongModePDPtr(pVCpu, GCPtrPage, &pPml4eDst, &pPdptDst, &pPDDst);
1468 if (rc != VINF_SUCCESS)
1469 {
1470 AssertMsg(rc == VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT || rc == VERR_PAGE_MAP_LEVEL4_NOT_PRESENT, ("Unexpected rc=%Rrc\n", rc));
1471 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,InvalidatePageSkipped));
1472 PGM_INVL_PG(pVCpu, GCPtrPage);
1473 return VINF_SUCCESS;
1474 }
1475 PX86PDEPAE pPdeDst = &pPDDst->a[iPDDst];
1476 Assert(pPDDst);
1477 Assert(pPdptDst->a[iPdpt].u & X86_PDPE_P);
1478
1479 /* Fetch the pgm pool shadow descriptor. */
1480 PPGMPOOLPAGE pShwPde = pgmPoolGetPage(pPool, pPdptDst->a[iPdpt].u & SHW_PDPE_PG_MASK);
1481 Assert(pShwPde);
1482
1483# endif /* PGM_SHW_TYPE == PGM_TYPE_AMD64 */
1484
1485 const SHWPDE PdeDst = *pPdeDst;
1486 if (!(PdeDst.u & X86_PDE_P))
1487 {
1488 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,InvalidatePageSkipped));
1489 PGM_INVL_PG(pVCpu, GCPtrPage);
1490 return VINF_SUCCESS;
1491 }
1492
1493 /*
1494 * Get the guest PD entry and calc big page.
1495 */
1496# if PGM_GST_TYPE == PGM_TYPE_32BIT
1497 PGSTPD pPDSrc = pgmGstGet32bitPDPtr(pVCpu);
1498 const unsigned iPDSrc = (uint32_t)GCPtrPage >> GST_PD_SHIFT;
1499 GSTPDE PdeSrc = pPDSrc->a[iPDSrc];
1500# else /* PGM_GST_TYPE != PGM_TYPE_32BIT */
1501 unsigned iPDSrc = 0;
1502# if PGM_GST_TYPE == PGM_TYPE_PAE
1503 X86PDPE PdpeSrcIgn;
1504 PX86PDPAE pPDSrc = pgmGstGetPaePDPtr(pVCpu, GCPtrPage, &iPDSrc, &PdpeSrcIgn);
1505# else /* AMD64 */
1506 PX86PML4E pPml4eSrcIgn;
1507 X86PDPE PdpeSrcIgn;
1508 PX86PDPAE pPDSrc = pgmGstGetLongModePDPtr(pVCpu, GCPtrPage, &pPml4eSrcIgn, &PdpeSrcIgn, &iPDSrc);
1509# endif
1510 GSTPDE PdeSrc;
1511
1512 if (pPDSrc)
1513 PdeSrc = pPDSrc->a[iPDSrc];
1514 else
1515 PdeSrc.u = 0;
1516# endif /* PGM_GST_TYPE != PGM_TYPE_32BIT */
1517 const bool fWasBigPage = RT_BOOL(PdeDst.u & PGM_PDFLAGS_BIG_PAGE);
1518 const bool fIsBigPage = (PdeSrc.u & X86_PDE_PS) && GST_IS_PSE_ACTIVE(pVCpu);
1519 if (fWasBigPage != fIsBigPage)
1520 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,InvalidatePageSkipped));
1521
1522# ifdef IN_RING3
1523 /*
1524 * If a CR3 Sync is pending we may ignore the invalidate page operation
1525 * depending on the kind of sync and if it's a global page or not.
1526 * This doesn't make sense in GC/R0 so we'll skip it entirely there.
1527 */
1528# ifdef PGM_SKIP_GLOBAL_PAGEDIRS_ON_NONGLOBAL_FLUSH
1529 if ( VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3)
1530 || ( VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL)
1531 && fIsBigPage
1532 && (PdeSrc.u & X86_PDE4M_G)
1533 )
1534 )
1535# else
1536 if (VM_FF_IS_ANY_SET(pVM, VM_FF_PGM_SYNC_CR3 | VM_FF_PGM_SYNC_CR3_NON_GLOBAL) )
1537# endif
1538 {
1539 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,InvalidatePageSkipped));
1540 return VINF_SUCCESS;
1541 }
1542# endif /* IN_RING3 */
1543
1544 /*
1545 * Deal with the Guest PDE.
1546 */
1547 rc = VINF_SUCCESS;
1548 if (PdeSrc.u & X86_PDE_P)
1549 {
1550 Assert( (PdeSrc.u & X86_PDE_US) == (PdeDst.u & X86_PDE_US)
1551 && ((PdeSrc.u & X86_PDE_RW) || !(PdeDst.u & X86_PDE_RW) || pVCpu->pgm.s.cNetwareWp0Hacks > 0));
1552 if (!fIsBigPage)
1553 {
1554 /*
1555 * 4KB - page.
1556 */
1557 PPGMPOOLPAGE pShwPage = pgmPoolGetPage(pPool, PdeDst.u & SHW_PDE_PG_MASK);
1558 RTGCPHYS GCPhys = GST_GET_PDE_GCPHYS(PdeSrc);
1559
1560# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
1561 /* Select the right PDE as we're emulating a 4kb page table with 2 shadow page tables. */
1562 GCPhys = PGM_A20_APPLY(pVCpu, GCPhys | ((iPDDst & 1) * (GUEST_PAGE_SIZE / 2)));
1563# endif
1564 if (pShwPage->GCPhys == GCPhys)
1565 {
1566 /* Syncing it here isn't 100% safe and it's probably not worth spending time syncing it. */
1567 PSHWPT pPTDst = (PSHWPT)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPage);
1568
1569 PGSTPT pPTSrc;
1570 rc = PGM_GCPHYS_2_PTR_V2(pVM, pVCpu, GST_GET_PDE_GCPHYS(PdeSrc), &pPTSrc);
1571 if (RT_SUCCESS(rc))
1572 {
1573 const unsigned iPTSrc = (GCPtrPage >> GST_PT_SHIFT) & GST_PT_MASK;
1574 GSTPTE PteSrc = pPTSrc->a[iPTSrc];
1575 const unsigned iPTDst = (GCPtrPage >> SHW_PT_SHIFT) & SHW_PT_MASK;
1576 PGM_BTH_NAME(SyncPageWorker)(pVCpu, &pPTDst->a[iPTDst], PdeSrc, PteSrc, pShwPage, iPTDst);
1577 Log2(("SyncPage: 4K %RGv PteSrc:{P=%d RW=%d U=%d raw=%08llx} PteDst=%08llx %s\n",
1578 GCPtrPage, PteSrc.u & X86_PTE_P,
1579 (PteSrc.u & PdeSrc.u & X86_PTE_RW),
1580 (PteSrc.u & PdeSrc.u & X86_PTE_US),
1581 (uint64_t)PteSrc.u,
1582 SHW_PTE_LOG64(pPTDst->a[iPTDst]),
1583 SHW_PTE_IS_TRACK_DIRTY(pPTDst->a[iPTDst]) ? " Track-Dirty" : ""));
1584 }
1585 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,InvalidatePage4KBPages));
1586 PGM_INVL_PG(pVCpu, GCPtrPage);
1587 }
1588 else
1589 {
1590 /*
1591 * The page table address changed.
1592 */
1593 LogFlow(("InvalidatePage: Out-of-sync at %RGp PdeSrc=%RX64 PdeDst=%RX64 ShwGCPhys=%RGp iPDDst=%#x\n",
1594 GCPtrPage, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u, pShwPage->GCPhys, iPDDst));
1595 pgmPoolFree(pVM, PdeDst.u & SHW_PDE_PG_MASK, pShwPde->idx, iPDDst);
1596 SHW_PDE_ATOMIC_SET(*pPdeDst, 0);
1597 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,InvalidatePagePDOutOfSync));
1598 PGM_INVL_VCPU_TLBS(pVCpu);
1599 }
1600 }
1601 else
1602 {
1603 /*
1604 * 2/4MB - page.
1605 */
1606 /* Before freeing the page, check if anything really changed. */
1607 PPGMPOOLPAGE pShwPage = pgmPoolGetPage(pPool, PdeDst.u & SHW_PDE_PG_MASK);
1608 RTGCPHYS GCPhys = GST_GET_BIG_PDE_GCPHYS(pVM, PdeSrc);
1609# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
1610 /* Select the right PDE as we're emulating a 4MB page directory with two 2 MB shadow PDEs.*/
1611 GCPhys = PGM_A20_APPLY(pVCpu, GCPhys | (GCPtrPage & (1 << X86_PD_PAE_SHIFT)));
1612# endif
1613 if ( pShwPage->GCPhys == GCPhys
1614 && pShwPage->enmKind == BTH_PGMPOOLKIND_PT_FOR_BIG)
1615 {
1616 /* ASSUMES a the given bits are identical for 4M and normal PDEs */
1617 /** @todo This test is wrong as it cannot check the G bit!
1618 * FIXME */
1619 if ( (PdeSrc.u & (X86_PDE_P | X86_PDE_RW | X86_PDE_US))
1620 == (PdeDst.u & (X86_PDE_P | X86_PDE_RW | X86_PDE_US))
1621 && ( (PdeSrc.u & X86_PDE4M_D) /** @todo rainy day: What about read-only 4M pages? not very common, but still... */
1622 || (PdeDst.u & PGM_PDFLAGS_TRACK_DIRTY)))
1623 {
1624 LogFlow(("Skipping flush for big page containing %RGv (PD=%X .u=%RX64)-> nothing has changed!\n", GCPtrPage, iPDSrc, PdeSrc.u));
1625 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,InvalidatePage4MBPagesSkip));
1626 return VINF_SUCCESS;
1627 }
1628 }
1629
1630 /*
1631 * Ok, the page table is present and it's been changed in the guest.
1632 * If we're in host context, we'll just mark it as not present taking the lazy approach.
1633 * We could do this for some flushes in GC too, but we need an algorithm for
1634 * deciding which 4MB pages containing code likely to be executed very soon.
1635 */
1636 LogFlow(("InvalidatePage: Out-of-sync PD at %RGp PdeSrc=%RX64 PdeDst=%RX64\n",
1637 GCPtrPage, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
1638 pgmPoolFree(pVM, PdeDst.u & SHW_PDE_PG_MASK, pShwPde->idx, iPDDst);
1639 SHW_PDE_ATOMIC_SET(*pPdeDst, 0);
1640 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,InvalidatePage4MBPages));
1641 PGM_INVL_BIG_PG(pVCpu, GCPtrPage);
1642 }
1643 }
1644 else
1645 {
1646 /*
1647 * Page directory is not present, mark shadow PDE not present.
1648 */
1649 pgmPoolFree(pVM, PdeDst.u & SHW_PDE_PG_MASK, pShwPde->idx, iPDDst);
1650 SHW_PDE_ATOMIC_SET(*pPdeDst, 0);
1651 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,InvalidatePagePDNPs));
1652 PGM_INVL_PG(pVCpu, GCPtrPage);
1653 }
1654 return rc;
1655
1656#else /* guest real and protected mode, nested + ept, none. */
1657 /* There's no such thing as InvalidatePage when paging is disabled, so just ignore. */
1658 NOREF(pVCpu); NOREF(GCPtrPage);
1659 return VINF_SUCCESS;
1660#endif
1661}
1662
1663#if PGM_SHW_TYPE != PGM_TYPE_NONE
1664
1665/**
1666 * Update the tracking of shadowed pages.
1667 *
1668 * @param pVCpu The cross context virtual CPU structure.
1669 * @param pShwPage The shadow page.
1670 * @param HCPhys The physical page we is being dereferenced.
1671 * @param iPte Shadow PTE index
1672 * @param GCPhysPage Guest physical address (only valid if pShwPage->fDirty is set)
1673 */
1674DECLINLINE(void) PGM_BTH_NAME(SyncPageWorkerTrackDeref)(PVMCPUCC pVCpu, PPGMPOOLPAGE pShwPage, RTHCPHYS HCPhys, uint16_t iPte,
1675 RTGCPHYS GCPhysPage)
1676{
1677 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
1678
1679# if defined(PGMPOOL_WITH_OPTIMIZED_DIRTY_PT) \
1680 && PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE) \
1681 && (PGM_GST_TYPE == PGM_TYPE_PAE || PGM_GST_TYPE == PGM_TYPE_AMD64 || PGM_SHW_TYPE == PGM_TYPE_PAE /* pae/32bit combo */)
1682
1683 /* Use the hint we retrieved from the cached guest PT. */
1684 if (pShwPage->fDirty)
1685 {
1686 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
1687
1688 Assert(pShwPage->cPresent);
1689 Assert(pPool->cPresent);
1690 pShwPage->cPresent--;
1691 pPool->cPresent--;
1692
1693 PPGMPAGE pPhysPage = pgmPhysGetPage(pVM, GCPhysPage);
1694 AssertRelease(pPhysPage);
1695 pgmTrackDerefGCPhys(pPool, pShwPage, pPhysPage, iPte);
1696 return;
1697 }
1698# else
1699 NOREF(GCPhysPage);
1700# endif
1701
1702 /** @todo If this turns out to be a bottle neck (*very* likely) two things can be done:
1703 * 1. have a medium sized HCPhys -> GCPhys TLB (hash?)
1704 * 2. write protect all shadowed pages. I.e. implement caching.
1705 *
1706 * 2023-08-24 bird: If we allow the ZeroPg to enter the shadow page tables,
1707 * this becomes a common occurence and we screw up. A better to the above would
1708 * be to have a parallel table that records the guest physical addresses of the
1709 * pages mapped by the shadow page table... For nested page tables,
1710 * we can easily correleate a table entry to a page entry, so it won't be
1711 * needed for those.
1712 */
1713# if PGM_TYPE_IS_NESTED_OR_EPT(PGM_SHW_TYPE) || !PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE)
1714 /*
1715 * For non-paged guest tables, EPT and nested tables we can figure out the
1716 * physical page corresponding to the entry and dereference it.
1717 * (This ASSUMES that shadow PTs won't be used ever be used out of place.)
1718 */
1719 if ( pShwPage->enmKind == PGMPOOLKIND_EPT_PT_FOR_PHYS
1720 || pShwPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_PHYS
1721 || pShwPage->enmKind == PGMPOOLKIND_32BIT_PT_FOR_PHYS)
1722 {
1723 RTGCPHYS GCPhysNestedEntry = pShwPage->GCPhys + ((uint32_t)iPte << X86_PAGE_SHIFT);
1724 if (!pShwPage->fA20Enabled)
1725 GCPhysNestedEntry &= ~(uint64_t)RT_BIT_64(20);
1726 PPGMPAGE const pPhysPage = pgmPhysGetPage(pVM, GCPhysNestedEntry);
1727 AssertRelease(pPhysPage);
1728 pgmTrackDerefGCPhys(pVM->pgm.s.CTX_SUFF(pPool), pShwPage, pPhysPage, iPte);
1729 }
1730 else
1731 AssertMsgFailed(("enmKind=%d GCPhys=%RGp\n", pShwPage->enmKind, pShwPage->GCPhys));
1732# endif
1733
1734 /** @todo duplicated in the 2nd half of pgmPoolTracDerefGCPhysHint */
1735
1736 /*
1737 * Find the guest address.
1738 */
1739 STAM_PROFILE_START(&pVM->pgm.s.Stats.StatTrackDeref, a);
1740 LogFlow(("SyncPageWorkerTrackDeref(%d,%d): Damn HCPhys=%RHp pShwPage->idx=%#x!!!\n",
1741 PGM_SHW_TYPE, PGM_GST_TYPE, HCPhys, pShwPage->idx));
1742 for (PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRangesX);
1743 pRam;
1744 pRam = pRam->CTX_SUFF(pNext))
1745 {
1746 unsigned iPage = pRam->cb >> GUEST_PAGE_SHIFT;
1747 while (iPage-- > 0)
1748 {
1749 if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
1750 {
1751 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
1752
1753 Assert(pShwPage->cPresent);
1754 Assert(pPool->cPresent);
1755 pShwPage->cPresent--;
1756 pPool->cPresent--;
1757
1758 pgmTrackDerefGCPhys(pPool, pShwPage, &pRam->aPages[iPage], iPte);
1759 STAM_PROFILE_STOP(&pVM->pgm.s.Stats.StatTrackDeref, a);
1760 return;
1761 }
1762 }
1763 }
1764
1765 for (;;)
1766 AssertReleaseMsgFailed(("HCPhys=%RHp wasn't found!\n", HCPhys));
1767}
1768
1769
1770/**
1771 * Update the tracking of shadowed pages.
1772 *
1773 * @param pVCpu The cross context virtual CPU structure.
1774 * @param pShwPage The shadow page.
1775 * @param u16 The top 16-bit of the pPage->HCPhys.
1776 * @param pPage Pointer to the guest page. this will be modified.
1777 * @param iPTDst The index into the shadow table.
1778 */
1779DECLINLINE(void) PGM_BTH_NAME(SyncPageWorkerTrackAddref)(PVMCPUCC pVCpu, PPGMPOOLPAGE pShwPage, uint16_t u16,
1780 PPGMPAGE pPage, const unsigned iPTDst)
1781{
1782 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
1783
1784 /*
1785 * Just deal with the simple first time here.
1786 */
1787 if (!u16)
1788 {
1789 STAM_COUNTER_INC(&pVM->pgm.s.Stats.StatTrackVirgin);
1790 u16 = PGMPOOL_TD_MAKE(1, pShwPage->idx);
1791 /* Save the page table index. */
1792 PGM_PAGE_SET_PTE_INDEX(pVM, pPage, iPTDst);
1793 }
1794 else
1795 u16 = pgmPoolTrackPhysExtAddref(pVM, pPage, u16, pShwPage->idx, iPTDst);
1796
1797 /* write back */
1798 Log2(("SyncPageWorkerTrackAddRef: u16=%#x->%#x iPTDst=%#x pPage=%p\n", u16, PGM_PAGE_GET_TRACKING(pPage), iPTDst, pPage));
1799 PGM_PAGE_SET_TRACKING(pVM, pPage, u16);
1800
1801 /* update statistics. */
1802 pVM->pgm.s.CTX_SUFF(pPool)->cPresent++;
1803 pShwPage->cPresent++;
1804 if (pShwPage->iFirstPresent > iPTDst)
1805 pShwPage->iFirstPresent = iPTDst;
1806}
1807
1808
1809/**
1810 * Modifies a shadow PTE to account for access handlers.
1811 *
1812 * @param pVM The cross context VM structure.
1813 * @param pVCpu The cross context virtual CPU structure.
1814 * @param pPage The page in question.
1815 * @param GCPhysPage The guest-physical address of the page.
1816 * @param fPteSrc The shadowed flags of the source PTE. Must include the
1817 * A (accessed) bit so it can be emulated correctly.
1818 * @param pPteDst The shadow PTE (output). This is temporary storage and
1819 * does not need to be set atomically.
1820 */
1821DECLINLINE(void) PGM_BTH_NAME(SyncHandlerPte)(PVMCC pVM, PVMCPUCC pVCpu, PCPGMPAGE pPage, RTGCPHYS GCPhysPage, uint64_t fPteSrc,
1822 PSHWPTE pPteDst)
1823{
1824 RT_NOREF_PV(pVM); RT_NOREF_PV(fPteSrc); RT_NOREF_PV(pVCpu); RT_NOREF_PV(GCPhysPage);
1825
1826 /** @todo r=bird: Are we actually handling dirty and access bits for pages with access handlers correctly? No.
1827 * Update: \#PF should deal with this before or after calling the handlers. It has all the info to do the job efficiently. */
1828 if (!PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage))
1829 {
1830 LogFlow(("SyncHandlerPte: monitored page (%R[pgmpage]) -> mark read-only\n", pPage));
1831# if PGM_SHW_TYPE == PGM_TYPE_EPT
1832 pPteDst->u = PGM_PAGE_GET_HCPHYS(pPage) | EPT_E_READ | EPT_E_EXECUTE | EPT_E_MEMTYPE_WB | EPT_E_IGNORE_PAT;
1833# else
1834 if (fPteSrc & X86_PTE_A)
1835 {
1836 SHW_PTE_SET(*pPteDst, fPteSrc | PGM_PAGE_GET_HCPHYS(pPage));
1837 SHW_PTE_SET_RO(*pPteDst);
1838 }
1839 else
1840 SHW_PTE_SET(*pPteDst, 0);
1841# endif
1842 }
1843# ifdef PGM_WITH_MMIO_OPTIMIZATIONS
1844# if PGM_SHW_TYPE == PGM_TYPE_EPT || PGM_SHW_TYPE == PGM_TYPE_PAE || PGM_SHW_TYPE == PGM_TYPE_AMD64
1845 else if ( PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage)
1846 && ( BTH_IS_NP_ACTIVE(pVM)
1847 || (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. */
1848# if PGM_SHW_TYPE == PGM_TYPE_AMD64
1849 && pVM->pgm.s.fLessThan52PhysicalAddressBits
1850# endif
1851 )
1852 {
1853 LogFlow(("SyncHandlerPte: MMIO page -> invalid \n"));
1854# if PGM_SHW_TYPE == PGM_TYPE_EPT
1855 /* 25.2.3.1: Reserved physical address bit -> EPT Misconfiguration (exit 49) */
1856 pPteDst->u = pVM->pgm.s.HCPhysInvMmioPg
1857 /* 25.2.3.1: bits 2:0 = 010b -> EPT Misconfiguration (exit 49) */
1858 | EPT_E_WRITE
1859 /* 25.2.3.1: leaf && 2:0 != 0 && u3Emt in {2, 3, 7} -> EPT Misconfiguration */
1860 | EPT_E_MEMTYPE_INVALID_3;
1861# else
1862 /* Set high page frame bits that MBZ (bankers on PAE, CPU dependent on AMD64). */
1863 SHW_PTE_SET(*pPteDst, pVM->pgm.s.HCPhysInvMmioPg | X86_PTE_PAE_MBZ_MASK_NO_NX | X86_PTE_P);
1864# endif
1865 }
1866# endif
1867# endif /* PGM_WITH_MMIO_OPTIMIZATIONS */
1868 else
1869 {
1870 LogFlow(("SyncHandlerPte: monitored page (%R[pgmpage]) -> mark not present\n", pPage));
1871 SHW_PTE_SET(*pPteDst, 0);
1872 }
1873 /** @todo count these kinds of entries. */
1874}
1875
1876
1877/**
1878 * Creates a 4K shadow page for a guest page.
1879 *
1880 * For 4M pages the caller must convert the PDE4M to a PTE, this includes adjusting the
1881 * physical address. The PdeSrc argument only the flags are used. No page
1882 * structured will be mapped in this function.
1883 *
1884 * @param pVCpu The cross context virtual CPU structure.
1885 * @param pPteDst Destination page table entry.
1886 * @param PdeSrc Source page directory entry (i.e. Guest OS page directory entry).
1887 * Can safely assume that only the flags are being used.
1888 * @param PteSrc Source page table entry (i.e. Guest OS page table entry).
1889 * @param pShwPage Pointer to the shadow page.
1890 * @param iPTDst The index into the shadow table.
1891 *
1892 * @remark Not used for 2/4MB pages!
1893 */
1894# if PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE) || defined(DOXYGEN_RUNNING)
1895static void PGM_BTH_NAME(SyncPageWorker)(PVMCPUCC pVCpu, PSHWPTE pPteDst, GSTPDE PdeSrc, GSTPTE PteSrc,
1896 PPGMPOOLPAGE pShwPage, unsigned iPTDst)
1897# else
1898static void PGM_BTH_NAME(SyncPageWorker)(PVMCPUCC pVCpu, PSHWPTE pPteDst, RTGCPHYS GCPhysPage,
1899 PPGMPOOLPAGE pShwPage, unsigned iPTDst)
1900# endif
1901{
1902 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
1903 RTGCPHYS GCPhysOldPage = NIL_RTGCPHYS;
1904
1905# if defined(PGMPOOL_WITH_OPTIMIZED_DIRTY_PT) \
1906 && PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE) \
1907 && (PGM_GST_TYPE == PGM_TYPE_PAE || PGM_GST_TYPE == PGM_TYPE_AMD64 || PGM_SHW_TYPE == PGM_TYPE_PAE /* pae/32bit combo */)
1908
1909 if (pShwPage->fDirty)
1910 {
1911 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
1912 PGSTPT pGstPT;
1913
1914 /* 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. */
1915 pGstPT = (PGSTPT)&pPool->aDirtyPages[pShwPage->idxDirtyEntry].aPage[0];
1916 GCPhysOldPage = GST_GET_PTE_GCPHYS(pGstPT->a[iPTDst]);
1917 pGstPT->a[iPTDst].u = PteSrc.u;
1918 }
1919# else
1920 Assert(!pShwPage->fDirty);
1921# endif
1922
1923# if PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE)
1924 if ( (PteSrc.u & X86_PTE_P)
1925 && GST_IS_PTE_VALID(pVCpu, PteSrc))
1926# endif
1927 {
1928# if PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE)
1929 RTGCPHYS GCPhysPage = GST_GET_PTE_GCPHYS(PteSrc);
1930# endif
1931 PGM_A20_ASSERT_MASKED(pVCpu, GCPhysPage);
1932
1933 /*
1934 * Find the ram range.
1935 */
1936 PPGMPAGE pPage;
1937 int rc = pgmPhysGetPageEx(pVM, GCPhysPage, &pPage);
1938 if (RT_SUCCESS(rc))
1939 {
1940 /* Ignore ballooned pages.
1941 Don't return errors or use a fatal assert here as part of a
1942 shadow sync range might included ballooned pages. */
1943 if (PGM_PAGE_IS_BALLOONED(pPage))
1944 {
1945 Assert(!SHW_PTE_IS_P(*pPteDst)); /** @todo user tracking needs updating if this triggers. */
1946 return;
1947 }
1948
1949# ifndef VBOX_WITH_NEW_LAZY_PAGE_ALLOC
1950 /* Make the page writable if necessary. */
1951 if ( PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM
1952 && ( PGM_PAGE_IS_ZERO(pPage)
1953# if PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE)
1954 || ( (PteSrc.u & X86_PTE_RW)
1955# else
1956 || ( 1
1957# endif
1958 && PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED
1959# ifdef VBOX_WITH_REAL_WRITE_MONITORED_PAGES
1960 && PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_WRITE_MONITORED
1961# endif
1962# ifdef VBOX_WITH_PAGE_SHARING
1963 && PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_SHARED
1964# endif
1965 )
1966 )
1967 )
1968 {
1969 rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhysPage);
1970 AssertRC(rc);
1971 }
1972# endif
1973
1974 /*
1975 * Make page table entry.
1976 */
1977 SHWPTE PteDst;
1978# if PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE)
1979 uint64_t fGstShwPteFlags = GST_GET_PTE_SHW_FLAGS(pVCpu, PteSrc);
1980# else
1981 uint64_t fGstShwPteFlags = X86_PTE_P | X86_PTE_RW | X86_PTE_US | X86_PTE_A | X86_PTE_D;
1982# endif
1983 if (!PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage) || PGM_PAGE_IS_HNDL_PHYS_NOT_IN_HM(pPage))
1984 {
1985# if PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE)
1986 /*
1987 * If the page or page directory entry is not marked accessed,
1988 * we mark the page not present.
1989 */
1990 if (!(PteSrc.u & X86_PTE_A) || !(PdeSrc.u & X86_PDE_A))
1991 {
1992 LogFlow(("SyncPageWorker: page and or page directory not accessed -> mark not present\n"));
1993 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,AccessedPage));
1994 SHW_PTE_SET(PteDst, 0);
1995 }
1996 /*
1997 * If the page is not flagged as dirty and is writable, then make it read-only, so we can set the dirty bit
1998 * when the page is modified.
1999 */
2000 else if (!(PteSrc.u & X86_PTE_D) && (PdeSrc.u & PteSrc.u & X86_PTE_RW))
2001 {
2002 AssertCompile(X86_PTE_RW == X86_PDE_RW);
2003 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,DirtyPage));
2004 SHW_PTE_SET(PteDst,
2005 fGstShwPteFlags
2006 | PGM_PAGE_GET_HCPHYS(pPage)
2007 | PGM_PTFLAGS_TRACK_DIRTY);
2008 SHW_PTE_SET_RO(PteDst);
2009 }
2010 else
2011# endif
2012 {
2013 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,DirtyPageSkipped));
2014# if PGM_SHW_TYPE == PGM_TYPE_EPT
2015 PteDst.u = PGM_PAGE_GET_HCPHYS(pPage)
2016 | EPT_E_READ | EPT_E_WRITE | EPT_E_EXECUTE | EPT_E_MEMTYPE_WB | EPT_E_IGNORE_PAT;
2017# else
2018 SHW_PTE_SET(PteDst, fGstShwPteFlags | PGM_PAGE_GET_HCPHYS(pPage));
2019# endif
2020 }
2021
2022 /*
2023 * Make sure only allocated pages are mapped writable.
2024 */
2025 if ( SHW_PTE_IS_P_RW(PteDst)
2026 && PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED)
2027 {
2028# ifndef VBOX_WITH_NEW_LAZY_PAGE_ALLOC
2029 /* Still applies to shared pages. */
2030 Assert(!PGM_PAGE_IS_ZERO(pPage));
2031# endif
2032 SHW_PTE_SET_RO(PteDst); /** @todo this isn't quite working yet. Why, isn't it? */
2033 Log3(("SyncPageWorker: write-protecting %RGp pPage=%R[pgmpage]at iPTDst=%d\n", GCPhysPage, pPage, iPTDst));
2034 }
2035 }
2036 else
2037 PGM_BTH_NAME(SyncHandlerPte)(pVM, pVCpu, pPage, GCPhysPage, fGstShwPteFlags, &PteDst);
2038
2039 /*
2040 * Keep user track up to date.
2041 */
2042 if (SHW_PTE_IS_P(PteDst))
2043 {
2044 if (!SHW_PTE_IS_P(*pPteDst))
2045 PGM_BTH_NAME(SyncPageWorkerTrackAddref)(pVCpu, pShwPage, PGM_PAGE_GET_TRACKING(pPage), pPage, iPTDst);
2046 else if (SHW_PTE_GET_HCPHYS(*pPteDst) != SHW_PTE_GET_HCPHYS(PteDst))
2047 {
2048 Log2(("SyncPageWorker: deref! *pPteDst=%RX64 PteDst=%RX64\n", SHW_PTE_LOG64(*pPteDst), SHW_PTE_LOG64(PteDst)));
2049 PGM_BTH_NAME(SyncPageWorkerTrackDeref)(pVCpu, pShwPage, SHW_PTE_GET_HCPHYS(*pPteDst), iPTDst, GCPhysOldPage);
2050 PGM_BTH_NAME(SyncPageWorkerTrackAddref)(pVCpu, pShwPage, PGM_PAGE_GET_TRACKING(pPage), pPage, iPTDst);
2051 }
2052 }
2053 else if (SHW_PTE_IS_P(*pPteDst))
2054 {
2055 Log2(("SyncPageWorker: deref! *pPteDst=%RX64\n", SHW_PTE_LOG64(*pPteDst)));
2056 PGM_BTH_NAME(SyncPageWorkerTrackDeref)(pVCpu, pShwPage, SHW_PTE_GET_HCPHYS(*pPteDst), iPTDst, GCPhysOldPage);
2057 }
2058
2059 /*
2060 * Update statistics and commit the entry.
2061 */
2062# if PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE)
2063 if (!(PteSrc.u & X86_PTE_G))
2064 pShwPage->fSeenNonGlobal = true;
2065# endif
2066 SHW_PTE_ATOMIC_SET2(*pPteDst, PteDst);
2067 return;
2068 }
2069
2070/** @todo count these three different kinds. */
2071 Log2(("SyncPageWorker: invalid address in Pte\n"));
2072 }
2073# if PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE)
2074 else if (!(PteSrc.u & X86_PTE_P))
2075 Log2(("SyncPageWorker: page not present in Pte\n"));
2076 else
2077 Log2(("SyncPageWorker: invalid Pte\n"));
2078# endif
2079
2080 /*
2081 * The page is not present or the PTE is bad. Replace the shadow PTE by
2082 * an empty entry, making sure to keep the user tracking up to date.
2083 */
2084 if (SHW_PTE_IS_P(*pPteDst))
2085 {
2086 Log2(("SyncPageWorker: deref! *pPteDst=%RX64\n", SHW_PTE_LOG64(*pPteDst)));
2087 PGM_BTH_NAME(SyncPageWorkerTrackDeref)(pVCpu, pShwPage, SHW_PTE_GET_HCPHYS(*pPteDst), iPTDst, GCPhysOldPage);
2088 }
2089 SHW_PTE_ATOMIC_SET(*pPteDst, 0);
2090}
2091
2092
2093/**
2094 * Syncs a guest OS page.
2095 *
2096 * There are no conflicts at this point, neither is there any need for
2097 * page table allocations.
2098 *
2099 * When called in PAE or AMD64 guest mode, the guest PDPE shall be valid.
2100 * When called in AMD64 guest mode, the guest PML4E shall be valid.
2101 *
2102 * @returns VBox status code.
2103 * @returns VINF_PGM_SYNCPAGE_MODIFIED_PDE if it modifies the PDE in any way.
2104 * @param pVCpu The cross context virtual CPU structure.
2105 * @param PdeSrc Page directory entry of the guest.
2106 * @param GCPtrPage Guest context page address.
2107 * @param cPages Number of pages to sync (PGM_SYNC_N_PAGES) (default=1).
2108 * @param uErr Fault error (X86_TRAP_PF_*).
2109 */
2110static int PGM_BTH_NAME(SyncPage)(PVMCPUCC pVCpu, GSTPDE PdeSrc, RTGCPTR GCPtrPage, unsigned cPages, unsigned uErr)
2111{
2112 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
2113 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool); NOREF(pPool);
2114 LogFlow(("SyncPage: GCPtrPage=%RGv cPages=%u uErr=%#x\n", GCPtrPage, cPages, uErr));
2115 RT_NOREF_PV(uErr); RT_NOREF_PV(cPages); RT_NOREF_PV(GCPtrPage);
2116
2117 PGM_LOCK_ASSERT_OWNER(pVM);
2118
2119# if ( PGM_GST_TYPE == PGM_TYPE_32BIT \
2120 || PGM_GST_TYPE == PGM_TYPE_PAE \
2121 || PGM_GST_TYPE == PGM_TYPE_AMD64) \
2122 && !PGM_TYPE_IS_NESTED_OR_EPT(PGM_SHW_TYPE)
2123
2124 /*
2125 * Assert preconditions.
2126 */
2127 Assert(PdeSrc.u & X86_PDE_P);
2128 Assert(cPages);
2129# if 0 /* rarely useful; leave for debugging. */
2130 STAM_COUNTER_INC(&pVCpu->pgm.s.StatSyncPagePD[(GCPtrPage >> GST_PD_SHIFT) & GST_PD_MASK]);
2131# endif
2132
2133 /*
2134 * Get the shadow PDE, find the shadow page table in the pool.
2135 */
2136# if PGM_SHW_TYPE == PGM_TYPE_32BIT
2137 const unsigned iPDDst = (GCPtrPage >> SHW_PD_SHIFT) & SHW_PD_MASK;
2138 PX86PDE pPdeDst = pgmShwGet32BitPDEPtr(pVCpu, GCPtrPage);
2139
2140 /* Fetch the pgm pool shadow descriptor. */
2141 PPGMPOOLPAGE pShwPde = pVCpu->pgm.s.CTX_SUFF(pShwPageCR3);
2142 Assert(pShwPde);
2143
2144# elif PGM_SHW_TYPE == PGM_TYPE_PAE
2145 const unsigned iPDDst = (GCPtrPage >> SHW_PD_SHIFT) & SHW_PD_MASK;
2146 PPGMPOOLPAGE pShwPde = NULL;
2147 PX86PDPAE pPDDst;
2148
2149 /* Fetch the pgm pool shadow descriptor. */
2150 int rc2 = pgmShwGetPaePoolPagePD(pVCpu, GCPtrPage, &pShwPde);
2151 AssertRCSuccessReturn(rc2, rc2);
2152 Assert(pShwPde);
2153
2154 pPDDst = (PX86PDPAE)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPde);
2155 PX86PDEPAE pPdeDst = &pPDDst->a[iPDDst];
2156
2157# elif PGM_SHW_TYPE == PGM_TYPE_AMD64
2158 const unsigned iPDDst = (GCPtrPage >> SHW_PD_SHIFT) & SHW_PD_MASK;
2159 const unsigned iPdpt = (GCPtrPage >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
2160 PX86PDPAE pPDDst = NULL; /* initialized to shut up gcc */
2161 PX86PDPT pPdptDst = NULL; /* initialized to shut up gcc */
2162
2163 int rc2 = pgmShwGetLongModePDPtr(pVCpu, GCPtrPage, NULL, &pPdptDst, &pPDDst);
2164 AssertRCSuccessReturn(rc2, rc2);
2165 Assert(pPDDst && pPdptDst);
2166 PX86PDEPAE pPdeDst = &pPDDst->a[iPDDst];
2167# endif
2168 SHWPDE PdeDst = *pPdeDst;
2169
2170 /*
2171 * - In the guest SMP case we could have blocked while another VCPU reused
2172 * this page table.
2173 * - With W7-64 we may also take this path when the A bit is cleared on
2174 * higher level tables (PDPE/PML4E). The guest does not invalidate the
2175 * relevant TLB entries. If we're write monitoring any page mapped by
2176 * the modified entry, we may end up here with a "stale" TLB entry.
2177 */
2178 if (!(PdeDst.u & X86_PDE_P))
2179 {
2180 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));
2181 AssertMsg(pVM->cCpus > 1 || (uErr & (X86_TRAP_PF_P | X86_TRAP_PF_RW)) == (X86_TRAP_PF_P | X86_TRAP_PF_RW),
2182 ("Unexpected missing PDE p=%p/%RX64 uErr=%#x\n", pPdeDst, (uint64_t)PdeDst.u, (uint32_t)uErr));
2183 if (uErr & X86_TRAP_PF_P)
2184 PGM_INVL_PG(pVCpu, GCPtrPage);
2185 return VINF_SUCCESS; /* force the instruction to be executed again. */
2186 }
2187
2188 PPGMPOOLPAGE pShwPage = pgmPoolGetPage(pPool, PdeDst.u & SHW_PDE_PG_MASK);
2189 Assert(pShwPage);
2190
2191# if PGM_GST_TYPE == PGM_TYPE_AMD64
2192 /* Fetch the pgm pool shadow descriptor. */
2193 PPGMPOOLPAGE pShwPde = pgmPoolGetPage(pPool, pPdptDst->a[iPdpt].u & X86_PDPE_PG_MASK);
2194 Assert(pShwPde);
2195# endif
2196
2197 /*
2198 * Check that the page is present and that the shadow PDE isn't out of sync.
2199 */
2200 const bool fBigPage = (PdeSrc.u & X86_PDE_PS) && GST_IS_PSE_ACTIVE(pVCpu);
2201 const bool fPdeValid = !fBigPage ? GST_IS_PDE_VALID(pVCpu, PdeSrc) : GST_IS_BIG_PDE_VALID(pVCpu, PdeSrc);
2202 RTGCPHYS GCPhys;
2203 if (!fBigPage)
2204 {
2205 GCPhys = GST_GET_PDE_GCPHYS(PdeSrc);
2206# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
2207 /* Select the right PDE as we're emulating a 4kb page table with 2 shadow page tables. */
2208 GCPhys = PGM_A20_APPLY(pVCpu, GCPhys | ((iPDDst & 1) * (GUEST_PAGE_SIZE / 2)));
2209# endif
2210 }
2211 else
2212 {
2213 GCPhys = GST_GET_BIG_PDE_GCPHYS(pVM, PdeSrc);
2214# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
2215 /* Select the right PDE as we're emulating a 4MB page directory with two 2 MB shadow PDEs.*/
2216 GCPhys = PGM_A20_APPLY(pVCpu, GCPhys | (GCPtrPage & (1 << X86_PD_PAE_SHIFT)));
2217# endif
2218 }
2219 /** @todo This doesn't check the G bit of 2/4MB pages. FIXME */
2220 if ( fPdeValid
2221 && pShwPage->GCPhys == GCPhys
2222 && (PdeSrc.u & X86_PDE_P)
2223 && (PdeSrc.u & X86_PDE_US) == (PdeDst.u & X86_PDE_US)
2224 && ((PdeSrc.u & X86_PDE_RW) == (PdeDst.u & X86_PDE_RW) || !(PdeDst.u & X86_PDE_RW))
2225# if PGM_WITH_NX(PGM_GST_TYPE, PGM_SHW_TYPE)
2226 && ((PdeSrc.u & X86_PDE_PAE_NX) == (PdeDst.u & X86_PDE_PAE_NX) || !GST_IS_NX_ACTIVE(pVCpu))
2227# endif
2228 )
2229 {
2230 /*
2231 * Check that the PDE is marked accessed already.
2232 * Since we set the accessed bit *before* getting here on a #PF, this
2233 * check is only meant for dealing with non-#PF'ing paths.
2234 */
2235 if (PdeSrc.u & X86_PDE_A)
2236 {
2237 PSHWPT pPTDst = (PSHWPT)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPage);
2238 if (!fBigPage)
2239 {
2240 /*
2241 * 4KB Page - Map the guest page table.
2242 */
2243 PGSTPT pPTSrc;
2244 int rc = PGM_GCPHYS_2_PTR_V2(pVM, pVCpu, GST_GET_PDE_GCPHYS(PdeSrc), &pPTSrc);
2245 if (RT_SUCCESS(rc))
2246 {
2247# ifdef PGM_SYNC_N_PAGES
2248 Assert(cPages == 1 || !(uErr & X86_TRAP_PF_P));
2249 if ( cPages > 1
2250 && !(uErr & X86_TRAP_PF_P)
2251 && !VM_FF_IS_SET(pVM, VM_FF_PGM_NO_MEMORY))
2252 {
2253 /*
2254 * This code path is currently only taken when the caller is PGMTrap0eHandler
2255 * for non-present pages!
2256 *
2257 * We're setting PGM_SYNC_NR_PAGES pages around the faulting page to sync it and
2258 * deal with locality.
2259 */
2260 unsigned iPTDst = (GCPtrPage >> SHW_PT_SHIFT) & SHW_PT_MASK;
2261# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
2262 /* Select the right PDE as we're emulating a 4kb page table with 2 shadow page tables. */
2263 const unsigned offPTSrc = ((GCPtrPage >> SHW_PD_SHIFT) & 1) * 512;
2264# else
2265 const unsigned offPTSrc = 0;
2266# endif
2267 const unsigned iPTDstEnd = RT_MIN(iPTDst + PGM_SYNC_NR_PAGES / 2, RT_ELEMENTS(pPTDst->a));
2268 if (iPTDst < PGM_SYNC_NR_PAGES / 2)
2269 iPTDst = 0;
2270 else
2271 iPTDst -= PGM_SYNC_NR_PAGES / 2;
2272
2273 for (; iPTDst < iPTDstEnd; iPTDst++)
2274 {
2275 const PGSTPTE pPteSrc = &pPTSrc->a[offPTSrc + iPTDst];
2276
2277 if ( (pPteSrc->u & X86_PTE_P)
2278 && !SHW_PTE_IS_P(pPTDst->a[iPTDst]))
2279 {
2280 RTGCPTR GCPtrCurPage = (GCPtrPage & ~(RTGCPTR)(GST_PT_MASK << GST_PT_SHIFT))
2281 | ((offPTSrc + iPTDst) << GUEST_PAGE_SHIFT);
2282 NOREF(GCPtrCurPage);
2283 PGM_BTH_NAME(SyncPageWorker)(pVCpu, &pPTDst->a[iPTDst], PdeSrc, *pPteSrc, pShwPage, iPTDst);
2284 Log2(("SyncPage: 4K+ %RGv PteSrc:{P=%d RW=%d U=%d raw=%08llx} PteDst=%08llx%s\n",
2285 GCPtrCurPage, pPteSrc->u & X86_PTE_P,
2286 !!(pPteSrc->u & PdeSrc.u & X86_PTE_RW),
2287 !!(pPteSrc->u & PdeSrc.u & X86_PTE_US),
2288 (uint64_t)pPteSrc->u,
2289 SHW_PTE_LOG64(pPTDst->a[iPTDst]),
2290 SHW_PTE_IS_TRACK_DIRTY(pPTDst->a[iPTDst]) ? " Track-Dirty" : ""));
2291 }
2292 }
2293 }
2294 else
2295# endif /* PGM_SYNC_N_PAGES */
2296 {
2297 const unsigned iPTSrc = (GCPtrPage >> GST_PT_SHIFT) & GST_PT_MASK;
2298 GSTPTE PteSrc = pPTSrc->a[iPTSrc];
2299 const unsigned iPTDst = (GCPtrPage >> SHW_PT_SHIFT) & SHW_PT_MASK;
2300 PGM_BTH_NAME(SyncPageWorker)(pVCpu, &pPTDst->a[iPTDst], PdeSrc, PteSrc, pShwPage, iPTDst);
2301 Log2(("SyncPage: 4K %RGv PteSrc:{P=%d RW=%d U=%d raw=%08llx} PteDst=%08llx %s\n",
2302 GCPtrPage, PteSrc.u & X86_PTE_P,
2303 !!(PteSrc.u & PdeSrc.u & X86_PTE_RW),
2304 !!(PteSrc.u & PdeSrc.u & X86_PTE_US),
2305 (uint64_t)PteSrc.u,
2306 SHW_PTE_LOG64(pPTDst->a[iPTDst]),
2307 SHW_PTE_IS_TRACK_DIRTY(pPTDst->a[iPTDst]) ? " Track-Dirty" : ""));
2308 }
2309 }
2310 else /* MMIO or invalid page: emulated in #PF handler. */
2311 {
2312 LogFlow(("PGM_GCPHYS_2_PTR %RGp failed with %Rrc\n", GCPhys, rc));
2313 Assert(!SHW_PTE_IS_P(pPTDst->a[(GCPtrPage >> SHW_PT_SHIFT) & SHW_PT_MASK]));
2314 }
2315 }
2316 else
2317 {
2318 /*
2319 * 4/2MB page - lazy syncing shadow 4K pages.
2320 * (There are many causes of getting here, it's no longer only CSAM.)
2321 */
2322 /* Calculate the GC physical address of this 4KB shadow page. */
2323 GCPhys = PGM_A20_APPLY(pVCpu, GST_GET_BIG_PDE_GCPHYS(pVM, PdeSrc) | (GCPtrPage & GST_BIG_PAGE_OFFSET_MASK));
2324 /* Find ram range. */
2325 PPGMPAGE pPage;
2326 int rc = pgmPhysGetPageEx(pVM, GCPhys, &pPage);
2327 if (RT_SUCCESS(rc))
2328 {
2329 AssertFatalMsg(!PGM_PAGE_IS_BALLOONED(pPage), ("Unexpected ballooned page at %RGp\n", GCPhys));
2330
2331# ifndef VBOX_WITH_NEW_LAZY_PAGE_ALLOC
2332 /* Try to make the page writable if necessary. */
2333 if ( PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM
2334 && ( PGM_PAGE_IS_ZERO(pPage)
2335 || ( (PdeSrc.u & X86_PDE_RW)
2336 && PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED
2337# ifdef VBOX_WITH_REAL_WRITE_MONITORED_PAGES
2338 && PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_WRITE_MONITORED
2339# endif
2340# ifdef VBOX_WITH_PAGE_SHARING
2341 && PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_SHARED
2342# endif
2343 )
2344 )
2345 )
2346 {
2347 rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
2348 AssertRC(rc);
2349 }
2350# endif
2351
2352 /*
2353 * Make shadow PTE entry.
2354 */
2355 SHWPTE PteDst;
2356 if (!PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage) || PGM_PAGE_IS_HNDL_PHYS_NOT_IN_HM(pPage))
2357 SHW_PTE_SET(PteDst, GST_GET_BIG_PDE_SHW_FLAGS_4_PTE(pVCpu, PdeSrc) | PGM_PAGE_GET_HCPHYS(pPage));
2358 else
2359 PGM_BTH_NAME(SyncHandlerPte)(pVM, pVCpu, pPage, GCPhys, GST_GET_BIG_PDE_SHW_FLAGS_4_PTE(pVCpu, PdeSrc), &PteDst);
2360
2361 const unsigned iPTDst = (GCPtrPage >> SHW_PT_SHIFT) & SHW_PT_MASK;
2362 if ( SHW_PTE_IS_P(PteDst)
2363 && !SHW_PTE_IS_P(pPTDst->a[iPTDst]))
2364 PGM_BTH_NAME(SyncPageWorkerTrackAddref)(pVCpu, pShwPage, PGM_PAGE_GET_TRACKING(pPage), pPage, iPTDst);
2365
2366 /* Make sure only allocated pages are mapped writable. */
2367 if ( SHW_PTE_IS_P_RW(PteDst)
2368 && PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED)
2369 {
2370# ifndef VBOX_WITH_NEW_LAZY_PAGE_ALLOC
2371 /* Still applies to shared pages. */
2372 Assert(!PGM_PAGE_IS_ZERO(pPage));
2373# endif
2374 SHW_PTE_SET_RO(PteDst); /** @todo this isn't quite working yet... */
2375 Log3(("SyncPage: write-protecting %RGp pPage=%R[pgmpage] at %RGv\n", GCPhys, pPage, GCPtrPage));
2376 }
2377
2378 SHW_PTE_ATOMIC_SET2(pPTDst->a[iPTDst], PteDst);
2379
2380 /*
2381 * If the page is not flagged as dirty and is writable, then make it read-only
2382 * at PD level, so we can set the dirty bit when the page is modified.
2383 *
2384 * ASSUMES that page access handlers are implemented on page table entry level.
2385 * Thus we will first catch the dirty access and set PDE.D and restart. If
2386 * there is an access handler, we'll trap again and let it work on the problem.
2387 */
2388 /** @todo r=bird: figure out why we need this here, SyncPT should've taken care of this already.
2389 * As for invlpg, it simply frees the whole shadow PT.
2390 * ...It's possibly because the guest clears it and the guest doesn't really tell us... */
2391 if ((PdeSrc.u & (X86_PDE4M_D | X86_PDE_RW)) == X86_PDE_RW)
2392 {
2393 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,DirtyPageBig));
2394 PdeDst.u |= PGM_PDFLAGS_TRACK_DIRTY;
2395 PdeDst.u &= ~(SHWUINT)X86_PDE_RW;
2396 }
2397 else
2398 {
2399 PdeDst.u &= ~(SHWUINT)(PGM_PDFLAGS_TRACK_DIRTY | X86_PDE_RW);
2400 PdeDst.u |= PdeSrc.u & X86_PDE_RW;
2401 }
2402 SHW_PDE_ATOMIC_SET2(*pPdeDst, PdeDst);
2403 Log2(("SyncPage: BIG %RGv PdeSrc:{P=%d RW=%d U=%d raw=%08llx} GCPhys=%RGp%s\n",
2404 GCPtrPage, PdeSrc.u & X86_PDE_P, !!(PdeSrc.u & X86_PDE_RW), !!(PdeSrc.u & X86_PDE_US),
2405 (uint64_t)PdeSrc.u, GCPhys, PdeDst.u & PGM_PDFLAGS_TRACK_DIRTY ? " Track-Dirty" : ""));
2406 }
2407 else
2408 {
2409 LogFlow(("PGM_GCPHYS_2_PTR %RGp (big) failed with %Rrc\n", GCPhys, rc));
2410 /** @todo must wipe the shadow page table entry in this
2411 * case. */
2412 }
2413 }
2414 PGM_DYNMAP_UNUSED_HINT(pVCpu, pPdeDst);
2415 return VINF_SUCCESS;
2416 }
2417
2418 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncPagePDNAs));
2419 }
2420 else if (fPdeValid)
2421 {
2422 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncPagePDOutOfSync));
2423 Log2(("SyncPage: Out-Of-Sync PDE at %RGp PdeSrc=%RX64 PdeDst=%RX64 (GCPhys %RGp vs %RGp)\n",
2424 GCPtrPage, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u, pShwPage->GCPhys, GCPhys));
2425 }
2426 else
2427 {
2428/// @todo STAM_COUNTER_INC(&pVCpu->pgm.s.CTX_MID_Z(Stat,SyncPagePDOutOfSyncAndInvalid));
2429 Log2(("SyncPage: Bad PDE at %RGp PdeSrc=%RX64 PdeDst=%RX64 (GCPhys %RGp vs %RGp)\n",
2430 GCPtrPage, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u, pShwPage->GCPhys, GCPhys));
2431 }
2432
2433 /*
2434 * Mark the PDE not present. Restart the instruction and let #PF call SyncPT.
2435 * Yea, I'm lazy.
2436 */
2437 pgmPoolFreeByPage(pPool, pShwPage, pShwPde->idx, iPDDst);
2438 SHW_PDE_ATOMIC_SET(*pPdeDst, 0);
2439
2440 PGM_DYNMAP_UNUSED_HINT(pVCpu, pPdeDst);
2441 PGM_INVL_VCPU_TLBS(pVCpu);
2442 return VINF_PGM_SYNCPAGE_MODIFIED_PDE;
2443
2444
2445# elif (PGM_GST_TYPE == PGM_TYPE_REAL || PGM_GST_TYPE == PGM_TYPE_PROT) \
2446 && !PGM_TYPE_IS_NESTED(PGM_SHW_TYPE) \
2447 && (PGM_SHW_TYPE != PGM_TYPE_EPT || PGM_GST_TYPE == PGM_TYPE_PROT)
2448 NOREF(PdeSrc);
2449
2450# ifdef PGM_SYNC_N_PAGES
2451 /*
2452 * Get the shadow PDE, find the shadow page table in the pool.
2453 */
2454# if PGM_SHW_TYPE == PGM_TYPE_32BIT
2455 X86PDE PdeDst = pgmShwGet32BitPDE(pVCpu, GCPtrPage);
2456
2457# elif PGM_SHW_TYPE == PGM_TYPE_PAE
2458 X86PDEPAE PdeDst = pgmShwGetPaePDE(pVCpu, GCPtrPage);
2459
2460# elif PGM_SHW_TYPE == PGM_TYPE_AMD64
2461 const unsigned iPDDst = ((GCPtrPage >> SHW_PD_SHIFT) & SHW_PD_MASK);
2462 const unsigned iPdpt = (GCPtrPage >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64; NOREF(iPdpt);
2463 PX86PDPAE pPDDst = NULL; /* initialized to shut up gcc */
2464 X86PDEPAE PdeDst;
2465 PX86PDPT pPdptDst = NULL; /* initialized to shut up gcc */
2466
2467 int rc = pgmShwGetLongModePDPtr(pVCpu, GCPtrPage, NULL, &pPdptDst, &pPDDst);
2468 AssertRCSuccessReturn(rc, rc);
2469 Assert(pPDDst && pPdptDst);
2470 PdeDst = pPDDst->a[iPDDst];
2471
2472# elif PGM_SHW_TYPE == PGM_TYPE_EPT
2473 const unsigned iPDDst = ((GCPtrPage >> SHW_PD_SHIFT) & SHW_PD_MASK);
2474 PEPTPD pPDDst;
2475 EPTPDE PdeDst;
2476
2477 int rc = pgmShwGetEPTPDPtr(pVCpu, GCPtrPage, NULL, &pPDDst);
2478 if (rc != VINF_SUCCESS)
2479 {
2480 AssertRC(rc);
2481 return rc;
2482 }
2483 Assert(pPDDst);
2484 PdeDst = pPDDst->a[iPDDst];
2485# endif
2486 /* In the guest SMP case we could have blocked while another VCPU reused this page table. */
2487 if (!SHW_PDE_IS_P(PdeDst))
2488 {
2489 AssertMsg(pVM->cCpus > 1, ("Unexpected missing PDE %RX64\n", (uint64_t)PdeDst.u));
2490 Log(("CPU%d: SyncPage: Pde at %RGv changed behind our back!\n", pVCpu->idCpu, GCPtrPage));
2491 return VINF_SUCCESS; /* force the instruction to be executed again. */
2492 }
2493
2494 /* Can happen in the guest SMP case; other VCPU activated this PDE while we were blocking to handle the page fault. */
2495 if (SHW_PDE_IS_BIG(PdeDst))
2496 {
2497 Assert(pVM->pgm.s.fNestedPaging);
2498 Log(("CPU%d: SyncPage: Pde (big:%RX64) at %RGv changed behind our back!\n", pVCpu->idCpu, PdeDst.u, GCPtrPage));
2499 return VINF_SUCCESS;
2500 }
2501
2502 /* Mask away the page offset. */
2503 GCPtrPage &= ~((RTGCPTR)0xfff);
2504
2505 PPGMPOOLPAGE pShwPage = pgmPoolGetPage(pPool, PdeDst.u & SHW_PDE_PG_MASK);
2506 PSHWPT pPTDst = (PSHWPT)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPage);
2507
2508 Assert(cPages == 1 || !(uErr & X86_TRAP_PF_P));
2509 if ( cPages > 1
2510 && !(uErr & X86_TRAP_PF_P)
2511 && !VM_FF_IS_SET(pVM, VM_FF_PGM_NO_MEMORY))
2512 {
2513 /*
2514 * This code path is currently only taken when the caller is PGMTrap0eHandler
2515 * for non-present pages!
2516 *
2517 * We're setting PGM_SYNC_NR_PAGES pages around the faulting page to sync it and
2518 * deal with locality.
2519 */
2520 unsigned iPTDst = (GCPtrPage >> SHW_PT_SHIFT) & SHW_PT_MASK;
2521 const unsigned iPTDstEnd = RT_MIN(iPTDst + PGM_SYNC_NR_PAGES / 2, RT_ELEMENTS(pPTDst->a));
2522 if (iPTDst < PGM_SYNC_NR_PAGES / 2)
2523 iPTDst = 0;
2524 else
2525 iPTDst -= PGM_SYNC_NR_PAGES / 2;
2526 for (; iPTDst < iPTDstEnd; iPTDst++)
2527 {
2528 if (!SHW_PTE_IS_P(pPTDst->a[iPTDst]))
2529 {
2530 RTGCPTR GCPtrCurPage = PGM_A20_APPLY(pVCpu, (GCPtrPage & ~(RTGCPTR)(SHW_PT_MASK << SHW_PT_SHIFT))
2531 | (iPTDst << GUEST_PAGE_SHIFT));
2532
2533 PGM_BTH_NAME(SyncPageWorker)(pVCpu, &pPTDst->a[iPTDst], GCPtrCurPage, pShwPage, iPTDst);
2534 Log2(("SyncPage: 4K+ %RGv PteSrc:{P=1 RW=1 U=1} PteDst=%08llx%s\n",
2535 GCPtrCurPage,
2536 SHW_PTE_LOG64(pPTDst->a[iPTDst]),
2537 SHW_PTE_IS_TRACK_DIRTY(pPTDst->a[iPTDst]) ? " Track-Dirty" : ""));
2538
2539 if (RT_UNLIKELY(VM_FF_IS_SET(pVM, VM_FF_PGM_NO_MEMORY)))
2540 break;
2541 }
2542 else
2543 Log4(("%RGv iPTDst=%x pPTDst->a[iPTDst] %RX64\n",
2544 (GCPtrPage & ~(RTGCPTR)(SHW_PT_MASK << SHW_PT_SHIFT)) | (iPTDst << GUEST_PAGE_SHIFT), iPTDst, SHW_PTE_LOG64(pPTDst->a[iPTDst]) ));
2545 }
2546 }
2547 else
2548# endif /* PGM_SYNC_N_PAGES */
2549 {
2550 const unsigned iPTDst = (GCPtrPage >> SHW_PT_SHIFT) & SHW_PT_MASK;
2551 RTGCPTR GCPtrCurPage = PGM_A20_APPLY(pVCpu, (GCPtrPage & ~(RTGCPTR)(SHW_PT_MASK << SHW_PT_SHIFT))
2552 | (iPTDst << GUEST_PAGE_SHIFT));
2553
2554 PGM_BTH_NAME(SyncPageWorker)(pVCpu, &pPTDst->a[iPTDst], GCPtrCurPage, pShwPage, iPTDst);
2555
2556 Log2(("SyncPage: 4K %RGv PteSrc:{P=1 RW=1 U=1}PteDst=%08llx%s\n",
2557 GCPtrPage,
2558 SHW_PTE_LOG64(pPTDst->a[iPTDst]),
2559 SHW_PTE_IS_TRACK_DIRTY(pPTDst->a[iPTDst]) ? " Track-Dirty" : ""));
2560 }
2561 return VINF_SUCCESS;
2562
2563# else
2564 NOREF(PdeSrc);
2565 AssertReleaseMsgFailed(("Shw=%d Gst=%d is not implemented!\n", PGM_GST_TYPE, PGM_SHW_TYPE));
2566 return VERR_PGM_NOT_USED_IN_MODE;
2567# endif
2568}
2569
2570#endif /* PGM_SHW_TYPE != PGM_TYPE_NONE */
2571
2572#if !defined(IN_RING3) && defined(VBOX_WITH_NESTED_HWVIRT_VMX_EPT) && PGM_SHW_TYPE == PGM_TYPE_EPT
2573
2574/**
2575 * Sync a shadow page for a nested-guest page.
2576 *
2577 * @param pVCpu The cross context virtual CPU structure.
2578 * @param pPte The shadow page table entry.
2579 * @param GCPhysPage The guest-physical address of the page.
2580 * @param pShwPage The shadow page of the page table.
2581 * @param iPte The index of the page table entry.
2582 * @param pGstSlatPte The guest SLAT page table entry.
2583 *
2584 * @note Not to be used for 2/4MB pages!
2585 */
2586static void PGM_BTH_NAME(NestedSyncPageWorker)(PVMCPUCC pVCpu, PSHWPTE pPte, RTGCPHYS GCPhysPage, PPGMPOOLPAGE pShwPage,
2587 unsigned iPte, SLATPTE GstSlatPte)
2588{
2589 PGM_A20_ASSERT_MASKED(pVCpu, GCPhysPage);
2590 Assert(PGMPOOL_PAGE_IS_NESTED(pShwPage));
2591 Assert(!pShwPage->fDirty);
2592 Assert(pVCpu->pgm.s.enmGuestSlatMode == PGMSLAT_EPT);
2593 AssertMsg(!(GstSlatPte.u & EPT_E_LEAF), ("Large page unexpected: %RX64\n", GstSlatPte.u));
2594 AssertMsg((GstSlatPte.u & EPT_PTE_PG_MASK) == GCPhysPage,
2595 ("PTE address mismatch. GCPhysPage=%RGp Pte=%RX64\n", GCPhysPage, GstSlatPte.u & EPT_PTE_PG_MASK));
2596
2597 /*
2598 * Find the ram range.
2599 */
2600 PPGMPAGE pPage;
2601 int rc = pgmPhysGetPageEx(pVCpu->CTX_SUFF(pVM), GCPhysPage, &pPage);
2602 if (RT_SUCCESS(rc))
2603 { /* likely */ }
2604 else
2605 {
2606 /*
2607 * This is a RAM hole/invalid/reserved address (not MMIO).
2608 * Nested Microsoft Hyper-V maps addresses like 0xf0220000 as RW WB memory.
2609 * Shadow a not-present page similar to MMIO, see @bugref{10318#c7}.
2610 */
2611 Assert(rc == VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS);
2612 if (SHW_PTE_IS_P(*pPte))
2613 {
2614 Log2(("NestedSyncPageWorker: deref! *pPte=%RX64\n", SHW_PTE_LOG64(*pPte)));
2615 PGM_BTH_NAME(SyncPageWorkerTrackDeref)(pVCpu, pShwPage, SHW_PTE_GET_HCPHYS(*pPte), iPte, NIL_RTGCPHYS);
2616 }
2617 Log7Func(("RAM hole/reserved %RGp -> ShwPte=0\n", GCPhysPage));
2618 SHW_PTE_ATOMIC_SET(*pPte, 0);
2619 return;
2620 }
2621
2622 Assert(!PGM_PAGE_IS_BALLOONED(pPage));
2623
2624 /*
2625 * Make page table entry.
2626 */
2627 SHWPTE Pte;
2628 uint64_t const fGstShwPteFlags = (GstSlatPte.u & pVCpu->pgm.s.fGstEptShadowedPteMask)
2629 | EPT_E_MEMTYPE_WB | EPT_E_IGNORE_PAT;
2630 if (!PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage) || PGM_PAGE_IS_HNDL_PHYS_NOT_IN_HM(pPage))
2631 {
2632# ifndef VBOX_WITH_NEW_LAZY_PAGE_ALLOC
2633 /* If it's the zero page or write to an unallocated page, allocate it to make it writable. */
2634 if ( PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM
2635 && ( PGM_PAGE_IS_ZERO(pPage)
2636 || ( (GstSlatPte.u & EPT_E_WRITE)
2637 && PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED
2638# ifdef VBOX_WITH_REAL_WRITE_MONITORED_PAGES
2639 && PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_WRITE_MONITORED
2640# endif
2641# ifdef VBOX_WITH_PAGE_SHARING
2642 && PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_SHARED
2643# endif
2644 && PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_BALLOONED
2645 )
2646 )
2647 )
2648 {
2649 rc = pgmPhysPageMakeWritable(pVCpu->CTX_SUFF(pVM), pPage, GCPhysPage);
2650 AssertRC(rc);
2651 Log7Func(("made writable (%R[pgmpage]) at %RGp\n", pPage, GCPhysPage));
2652 }
2653# endif
2654 /** @todo access bit. */
2655 Pte.u = PGM_PAGE_GET_HCPHYS(pPage) | fGstShwPteFlags;
2656 Log7Func(("regular page (%R[pgmpage]) at %RGp -> %RX64\n", pPage, GCPhysPage, Pte.u));
2657
2658 /* Make sure only allocated pages are mapped writable. */
2659 if ( (fGstShwPteFlags & EPT_E_WRITE)
2660 && PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED)
2661 {
2662 Pte.u &= ~EPT_E_WRITE;
2663 Log7Func(("write-protecting page (%R[pgmpage]) at %RGp -> %RX64\n", pPage, GCPhysPage, Pte.u));
2664 }
2665 }
2666 else if (!PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage))
2667 {
2668 /** @todo access bit. */
2669 Pte.u = PGM_PAGE_GET_HCPHYS(pPage) | (fGstShwPteFlags & ~EPT_E_WRITE);
2670 Log7Func(("monitored page (%R[pgmpage]) at %RGp -> %RX64\n", pPage, GCPhysPage, Pte.u));
2671 }
2672 else
2673 {
2674 /** @todo Do MMIO optimizations here too? */
2675 Log7Func(("mmio/all page (%R[pgmpage]) at %RGp -> 0\n", pPage, GCPhysPage));
2676 Pte.u = 0;
2677 }
2678
2679 /* Make sure only allocated pages are mapped writable. */
2680 Assert(!SHW_PTE_IS_P_RW(Pte) || PGM_PAGE_IS_ALLOCATED(pPage));
2681
2682 /*
2683 * Keep user track up to date.
2684 */
2685 if (SHW_PTE_IS_P(Pte))
2686 {
2687 if (!SHW_PTE_IS_P(*pPte))
2688 PGM_BTH_NAME(SyncPageWorkerTrackAddref)(pVCpu, pShwPage, PGM_PAGE_GET_TRACKING(pPage), pPage, iPte);
2689 else if (SHW_PTE_GET_HCPHYS(*pPte) != SHW_PTE_GET_HCPHYS(Pte))
2690 {
2691 Log2(("NestedSyncPageWorker: deref! *pPte=%RX64 Pte=%RX64\n", SHW_PTE_LOG64(*pPte), SHW_PTE_LOG64(Pte)));
2692 PGM_BTH_NAME(SyncPageWorkerTrackDeref)(pVCpu, pShwPage, SHW_PTE_GET_HCPHYS(*pPte), iPte, NIL_RTGCPHYS);
2693 PGM_BTH_NAME(SyncPageWorkerTrackAddref)(pVCpu, pShwPage, PGM_PAGE_GET_TRACKING(pPage), pPage, iPte);
2694 }
2695 }
2696 else if (SHW_PTE_IS_P(*pPte))
2697 {
2698 Log2(("NestedSyncPageWorker: deref! *pPte=%RX64\n", SHW_PTE_LOG64(*pPte)));
2699 PGM_BTH_NAME(SyncPageWorkerTrackDeref)(pVCpu, pShwPage, SHW_PTE_GET_HCPHYS(*pPte), iPte, NIL_RTGCPHYS);
2700 }
2701
2702 /*
2703 * Commit the entry.
2704 */
2705 SHW_PTE_ATOMIC_SET2(*pPte, Pte);
2706 return;
2707}
2708
2709
2710/**
2711 * Syncs a nested-guest page.
2712 *
2713 * There are no conflicts at this point, neither is there any need for
2714 * page table allocations.
2715 *
2716 * @returns VBox status code.
2717 * @param pVCpu The cross context virtual CPU structure.
2718 * @param GCPhysNestedPage The nested-guest physical address of the page being
2719 * synced.
2720 * @param GCPhysPage The guest-physical address of the page being synced.
2721 * @param cPages Number of pages to sync (PGM_SYNC_N_PAGES) (default=1).
2722 * @param uErr The page fault error (X86_TRAP_PF_XXX).
2723 * @param pGstWalkAll The guest page table walk result.
2724 */
2725static int PGM_BTH_NAME(NestedSyncPage)(PVMCPUCC pVCpu, RTGCPHYS GCPhysNestedPage, RTGCPHYS GCPhysPage, unsigned cPages,
2726 uint32_t uErr, PPGMPTWALKGST pGstWalkAll)
2727{
2728 PGM_A20_ASSERT_MASKED(pVCpu, GCPhysPage);
2729 Assert(!(GCPhysNestedPage & GUEST_PAGE_OFFSET_MASK));
2730 Assert(!(GCPhysPage & GUEST_PAGE_OFFSET_MASK));
2731
2732 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
2733 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool); NOREF(pPool);
2734 Log7Func(("GCPhysNestedPage=%RGv GCPhysPage=%RGp cPages=%u uErr=%#x\n", GCPhysNestedPage, GCPhysPage, cPages, uErr));
2735 RT_NOREF_PV(uErr); RT_NOREF_PV(cPages);
2736
2737 PGM_LOCK_ASSERT_OWNER(pVM);
2738
2739 /*
2740 * Get the shadow PDE, find the shadow page table in the pool.
2741 */
2742 unsigned const iPde = ((GCPhysNestedPage >> EPT_PD_SHIFT) & EPT_PD_MASK);
2743 PEPTPD pPd;
2744 int rc = pgmShwGetNestedEPTPDPtr(pVCpu, GCPhysNestedPage, NULL, &pPd, pGstWalkAll);
2745 if (RT_SUCCESS(rc))
2746 { /* likely */ }
2747 else
2748 {
2749 Log(("Failed to fetch EPT PD for %RGp (%RGp) rc=%Rrc\n", GCPhysNestedPage, GCPhysPage, rc));
2750 return rc;
2751 }
2752 Assert(pPd);
2753 EPTPDE Pde = pPd->a[iPde];
2754
2755 /* In the guest SMP case we could have blocked while another VCPU reused this page table. */
2756 if (!SHW_PDE_IS_P(Pde))
2757 {
2758 AssertMsg(pVM->cCpus > 1, ("Unexpected missing PDE %RX64\n", (uint64_t)Pde.u));
2759 Log7Func(("CPU%d: SyncPage: Pde at %RGp changed behind our back!\n", pVCpu->idCpu, GCPhysNestedPage));
2760 return VINF_SUCCESS; /* force the instruction to be executed again. */
2761 }
2762
2763 /* Can happen in the guest SMP case; other VCPU activated this PDE while we were blocking to handle the page fault. */
2764 if (SHW_PDE_IS_BIG(Pde))
2765 {
2766 Log7Func(("CPU%d: SyncPage: %RGp changed behind our back!\n", pVCpu->idCpu, GCPhysNestedPage));
2767 return VINF_SUCCESS;
2768 }
2769
2770 PPGMPOOLPAGE pShwPage = pgmPoolGetPage(pPool, Pde.u & EPT_PDE_PG_MASK);
2771 PEPTPT pPt = (PEPTPT)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPage);
2772
2773 /*
2774 * If we've shadowed a guest EPT PDE that maps a 2M page using a 4K table,
2775 * then sync the 4K sub-page in the 2M range.
2776 */
2777 if (pGstWalkAll->u.Ept.Pde.u & EPT_E_LEAF)
2778 {
2779 Assert(!SHW_PDE_IS_BIG(Pde));
2780
2781 Assert(pGstWalkAll->u.Ept.Pte.u == 0);
2782 Assert((Pde.u & EPT_PRESENT_MASK) == (pGstWalkAll->u.Ept.Pde.u & EPT_PRESENT_MASK));
2783 Assert(pShwPage->GCPhys == (pGstWalkAll->u.Ept.Pde.u & EPT_PDE2M_PG_MASK));
2784
2785#if defined(VBOX_STRICT) && defined(DEBUG_ramshankar)
2786 PPGMPAGE pPage;
2787 rc = pgmPhysGetPageEx(pVM, GCPhysPage, &pPage); AssertRC(rc);
2788 Assert(PGM_PAGE_GET_PDE_TYPE(pPage) != PGM_PAGE_PDE_TYPE_PDE);
2789 Assert(pShwPage->enmKind == PGMPOOLKIND_EPT_PT_FOR_EPT_2MB);
2790#endif
2791 uint64_t const fGstShwPteFlags = (pGstWalkAll->u.Ept.Pde.u & pVCpu->pgm.s.fGstEptShadowedBigPdeMask & ~EPT_E_LEAF)
2792 | EPT_E_MEMTYPE_WB | EPT_E_IGNORE_PAT;
2793 SLATPTE GstSlatPte;
2794 GstSlatPte.u = GCPhysPage | fGstShwPteFlags;
2795
2796 unsigned const iPte = (GCPhysNestedPage >> SHW_PT_SHIFT) & SHW_PT_MASK;
2797 PGM_BTH_NAME(NestedSyncPageWorker)(pVCpu, &pPt->a[iPte], GCPhysPage, pShwPage, iPte, GstSlatPte);
2798 Log7Func(("4K: GCPhysPage=%RGp iPte=%u ShwPte=%08llx\n", GCPhysPage, iPte, SHW_PTE_LOG64(pPt->a[iPte])));
2799 return VINF_SUCCESS;
2800 }
2801
2802 Assert(cPages == 1 || !(uErr & X86_TRAP_PF_P));
2803# ifdef PGM_SYNC_N_PAGES
2804 if ( cPages > 1
2805 && !(uErr & X86_TRAP_PF_P)
2806 && !VM_FF_IS_SET(pVM, VM_FF_PGM_NO_MEMORY))
2807 {
2808 /*
2809 * This code path is currently only taken for non-present pages!
2810 *
2811 * We're setting PGM_SYNC_NR_PAGES pages around the faulting page to sync it and
2812 * deal with locality.
2813 */
2814 unsigned iPte = (GCPhysNestedPage >> SHW_PT_SHIFT) & SHW_PT_MASK;
2815 unsigned const iPteEnd = RT_MIN(iPte + PGM_SYNC_NR_PAGES / 2, RT_ELEMENTS(pPt->a));
2816 if (iPte < PGM_SYNC_NR_PAGES / 2)
2817 iPte = 0;
2818 else
2819 iPte -= PGM_SYNC_NR_PAGES / 2;
2820 for (; iPte < iPteEnd; iPte++)
2821 {
2822 if (!SHW_PTE_IS_P(pPt->a[iPte]))
2823 {
2824 PGMPTWALKGST GstWalkPt;
2825 PGMPTWALK WalkPt;
2826 GCPhysNestedPage &= ~(SHW_PT_MASK << SHW_PT_SHIFT);
2827 GCPhysNestedPage |= (iPte << GUEST_PAGE_SHIFT);
2828 rc = pgmGstSlatWalk(pVCpu, GCPhysNestedPage, false /*fIsLinearAddrValid*/, 0 /*GCPtrNested*/, &WalkPt,
2829 &GstWalkPt);
2830 if (RT_SUCCESS(rc))
2831 PGM_BTH_NAME(NestedSyncPageWorker)(pVCpu, &pPt->a[iPte], WalkPt.GCPhys, pShwPage, iPte, GstWalkPt.u.Ept.Pte);
2832 else
2833 {
2834 /*
2835 * This could be MMIO pages reserved by the nested-hypevisor or genuinely not-present pages.
2836 * Ensure the shadow tables entry is not-present.
2837 */
2838 /** @todo Potential room for optimization (explained in NestedSyncPT). */
2839 AssertMsg(!pPt->a[iPte].u, ("%RX64\n", pPt->a[iPte].u));
2840 }
2841 Log7Func(("Many: %RGp iPte=%u ShwPte=%RX64\n", GCPhysNestedPage, iPte, SHW_PTE_LOG64(pPt->a[iPte])));
2842 if (RT_UNLIKELY(VM_FF_IS_SET(pVM, VM_FF_PGM_NO_MEMORY)))
2843 break;
2844 }
2845 else
2846 {
2847# ifdef VBOX_STRICT
2848 /* Paranoia - Verify address of the page is what it should be. */
2849 PGMPTWALKGST GstWalkPt;
2850 PGMPTWALK WalkPt;
2851 GCPhysNestedPage &= ~(SHW_PT_MASK << SHW_PT_SHIFT);
2852 GCPhysNestedPage |= (iPte << GUEST_PAGE_SHIFT);
2853 rc = pgmGstSlatWalk(pVCpu, GCPhysNestedPage, false /*fIsLinearAddrValid*/, 0 /*GCPtrNested*/, &WalkPt, &GstWalkPt);
2854 AssertRC(rc);
2855 PPGMPAGE pPage;
2856 rc = pgmPhysGetPageEx(pVM, WalkPt.GCPhys, &pPage);
2857 AssertRC(rc);
2858 AssertMsg(PGM_PAGE_GET_HCPHYS(pPage) == SHW_PTE_GET_HCPHYS(pPt->a[iPte]),
2859 ("PGM page and shadow PTE address conflict. GCPhysNestedPage=%RGp GCPhysPage=%RGp HCPhys=%RHp Shw=%RHp\n",
2860 GCPhysNestedPage, WalkPt.GCPhys, PGM_PAGE_GET_HCPHYS(pPage), SHW_PTE_GET_HCPHYS(pPt->a[iPte])));
2861# endif
2862 Log7Func(("Many3: %RGp iPte=%u ShwPte=%RX64\n", GCPhysNestedPage, iPte, SHW_PTE_LOG64(pPt->a[iPte])));
2863 }
2864 }
2865 }
2866 else
2867# endif /* PGM_SYNC_N_PAGES */
2868 {
2869 unsigned const iPte = (GCPhysNestedPage >> SHW_PT_SHIFT) & SHW_PT_MASK;
2870 PGM_BTH_NAME(NestedSyncPageWorker)(pVCpu, &pPt->a[iPte], GCPhysPage, pShwPage, iPte, pGstWalkAll->u.Ept.Pte);
2871 Log7Func(("4K: GCPhysPage=%RGp iPte=%u ShwPte=%08llx\n", GCPhysPage, iPte, SHW_PTE_LOG64(pPt->a[iPte])));
2872 }
2873
2874 return VINF_SUCCESS;
2875}
2876
2877
2878/**
2879 * Sync a shadow page table for a nested-guest page table.
2880 *
2881 * The shadow page table is not present in the shadow PDE.
2882 *
2883 * Handles mapping conflicts.
2884 *
2885 * A precondition for this method is that the shadow PDE is not present. The
2886 * caller must take the PGM lock before checking this and continue to hold it
2887 * when calling this method.
2888 *
2889 * @returns VBox status code.
2890 * @param pVCpu The cross context virtual CPU structure.
2891 * @param GCPhysNestedPage The nested-guest physical page address of the page
2892 * being synced.
2893 * @param GCPhysPage The guest-physical address of the page being synced.
2894 * @param pGstWalkAll The guest page table walk result.
2895 */
2896static int PGM_BTH_NAME(NestedSyncPT)(PVMCPUCC pVCpu, RTGCPHYS GCPhysNestedPage, RTGCPHYS GCPhysPage, PPGMPTWALKGST pGstWalkAll)
2897{
2898 PGM_A20_ASSERT_MASKED(pVCpu, GCPhysPage);
2899 Assert(!(GCPhysNestedPage & GUEST_PAGE_OFFSET_MASK));
2900 Assert(!(GCPhysPage & GUEST_PAGE_OFFSET_MASK));
2901
2902 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
2903 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
2904
2905 Log7Func(("GCPhysNestedPage=%RGp GCPhysPage=%RGp\n", GCPhysNestedPage, GCPhysPage));
2906
2907 PGM_LOCK_ASSERT_OWNER(pVM);
2908 STAM_PROFILE_START(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncPT), a);
2909
2910 PEPTPD pPd;
2911 PEPTPDPT pPdpt;
2912 unsigned const iPde = (GCPhysNestedPage >> EPT_PD_SHIFT) & EPT_PD_MASK;
2913 int rc = pgmShwGetNestedEPTPDPtr(pVCpu, GCPhysNestedPage, &pPdpt, &pPd, pGstWalkAll);
2914 if (RT_SUCCESS(rc))
2915 { /* likely */ }
2916 else
2917 {
2918 STAM_PROFILE_STOP(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncPT), a);
2919 AssertRC(rc);
2920 return rc;
2921 }
2922 Assert(pPd);
2923 PSHWPDE pPde = &pPd->a[iPde];
2924
2925 unsigned const iPdpt = (GCPhysNestedPage >> EPT_PDPT_SHIFT) & EPT_PDPT_MASK;
2926 PPGMPOOLPAGE pShwPde = pgmPoolGetPage(pPool, pPdpt->a[iPdpt].u & EPT_PDPTE_PG_MASK);
2927 Assert(pShwPde->enmKind == PGMPOOLKIND_EPT_PD_FOR_EPT_PD);
2928
2929 SHWPDE Pde = *pPde;
2930 Assert(!SHW_PDE_IS_P(Pde)); /* We're only supposed to call SyncPT on PDE!P and conflicts. */
2931
2932# ifdef PGM_WITH_LARGE_PAGES
2933 Assert(BTH_IS_NP_ACTIVE(pVM));
2934
2935 /*
2936 * Check if the guest is mapping a 2M page.
2937 */
2938 if (pGstWalkAll->u.Ept.Pde.u & EPT_E_LEAF)
2939 {
2940 PPGMPAGE pPage;
2941 rc = pgmPhysGetPageEx(pVM, GCPhysPage & X86_PDE2M_PAE_PG_MASK, &pPage);
2942 AssertRCReturn(rc, rc);
2943
2944 /* A20 is always enabled in VMX root and non-root operation. */
2945 Assert(PGM_A20_IS_ENABLED(pVCpu));
2946
2947 /*
2948 * Check if we have or can get a 2M backing page here.
2949 */
2950 RTHCPHYS HCPhys = NIL_RTHCPHYS;
2951 if (PGM_PAGE_GET_PDE_TYPE(pPage) == PGM_PAGE_PDE_TYPE_PDE)
2952 {
2953 STAM_REL_COUNTER_INC(&pVM->pgm.s.StatLargePageReused);
2954 AssertRelease(PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ALLOCATED);
2955 HCPhys = PGM_PAGE_GET_HCPHYS(pPage);
2956 }
2957 else if (PGM_PAGE_GET_PDE_TYPE(pPage) == PGM_PAGE_PDE_TYPE_PDE_DISABLED)
2958 {
2959 /* Recheck the entire 2 MB range to see if we can use it again as a large page. */
2960 rc = pgmPhysRecheckLargePage(pVM, GCPhysPage, pPage);
2961 if (RT_SUCCESS(rc))
2962 {
2963 Assert(PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ALLOCATED);
2964 Assert(PGM_PAGE_GET_PDE_TYPE(pPage) == PGM_PAGE_PDE_TYPE_PDE);
2965 HCPhys = PGM_PAGE_GET_HCPHYS(pPage);
2966 }
2967 }
2968 else if (PGMIsUsingLargePages(pVM))
2969 {
2970 rc = pgmPhysAllocLargePage(pVM, GCPhysPage);
2971 if (RT_SUCCESS(rc))
2972 {
2973 Assert(PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ALLOCATED);
2974 Assert(PGM_PAGE_GET_PDE_TYPE(pPage) == PGM_PAGE_PDE_TYPE_PDE);
2975 HCPhys = PGM_PAGE_GET_HCPHYS(pPage);
2976 }
2977 }
2978
2979 /*
2980 * If we have a 2M backing page, we can map the guest's 2M page right away.
2981 */
2982 uint64_t const fGstShwBigPdeFlags = (pGstWalkAll->u.Ept.Pde.u & pVCpu->pgm.s.fGstEptShadowedBigPdeMask)
2983 | EPT_E_MEMTYPE_WB | EPT_E_IGNORE_PAT;
2984 if (HCPhys != NIL_RTHCPHYS)
2985 {
2986 Pde.u = HCPhys | fGstShwBigPdeFlags;
2987 Assert(!(Pde.u & pVCpu->pgm.s.fGstEptMbzBigPdeMask));
2988 Assert(Pde.u & EPT_E_LEAF);
2989 SHW_PDE_ATOMIC_SET2(*pPde, Pde);
2990
2991 /* Add a reference to the first page only. */
2992 PGM_BTH_NAME(SyncPageWorkerTrackAddref)(pVCpu, pShwPde, PGM_PAGE_GET_TRACKING(pPage), pPage, iPde);
2993
2994 Assert(PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_WRITE_MONITORED);
2995
2996 STAM_PROFILE_STOP(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncPT), a);
2997 Log7Func(("GstPde=%RGp ShwPde=%RX64 [2M]\n", pGstWalkAll->u.Ept.Pde.u, Pde.u));
2998 return VINF_SUCCESS;
2999 }
3000
3001 /*
3002 * We didn't get a perfect 2M fit. Split the 2M page into 4K pages.
3003 * The page ought not to be marked as a big (2M) page at this point.
3004 */
3005 Assert(PGM_PAGE_GET_PDE_TYPE(pPage) != PGM_PAGE_PDE_TYPE_PDE);
3006
3007 /* Determine the right kind of large page to avoid incorrect cached entry reuse. */
3008 PGMPOOLACCESS enmAccess;
3009 {
3010 /*
3011 * Mode-based execute control for EPT not supported.
3012 *
3013 * However, Windows 10 with Hyper-V enabled sets the EPT_E_USER_EXECUTE bit but does
3014 * not enable "mode-based execute control for EPT" in the VT-x secondary VM-execution
3015 * controls. The CPU ignores this bit when the control isn't set. Hence, the assertion
3016 * below is commented out.
3017 */
3018 /* Assert(!(pGstWalkAll->u.Ept.Pde.u & EPT_E_USER_EXECUTE)); */
3019 Assert(!pVCpu->CTX_SUFF(pVM)->cpum.ro.GuestFeatures.fVmxModeBasedExecuteEpt);
3020 bool const fNoExecute = !(pGstWalkAll->u.Ept.Pde.u & EPT_E_EXECUTE);
3021 if (pGstWalkAll->u.Ept.Pde.u & EPT_E_WRITE)
3022 enmAccess = fNoExecute ? PGMPOOLACCESS_SUPERVISOR_RW_NX : PGMPOOLACCESS_SUPERVISOR_RW;
3023 else
3024 enmAccess = fNoExecute ? PGMPOOLACCESS_SUPERVISOR_R_NX : PGMPOOLACCESS_SUPERVISOR_R;
3025 }
3026
3027 /*
3028 * Allocate & map a 4K shadow table to cover the 2M guest page.
3029 */
3030 PPGMPOOLPAGE pShwPage;
3031 RTGCPHYS const GCPhysPt = pGstWalkAll->u.Ept.Pde.u & EPT_PDE2M_PG_MASK;
3032 rc = pgmPoolAlloc(pVM, GCPhysPt, PGMPOOLKIND_EPT_PT_FOR_EPT_2MB, enmAccess, PGM_A20_IS_ENABLED(pVCpu),
3033 pShwPde->idx, iPde, false /*fLockPage*/, &pShwPage);
3034 if ( rc == VINF_SUCCESS
3035 || rc == VINF_PGM_CACHED_PAGE)
3036 { /* likely */ }
3037 else
3038 {
3039 STAM_PROFILE_STOP(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncPT), a);
3040 AssertMsgFailedReturn(("rc=%Rrc\n", rc), RT_FAILURE_NP(rc) ? rc : VERR_IPE_UNEXPECTED_INFO_STATUS);
3041 }
3042
3043 PSHWPT pPt = (PSHWPT)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPage);
3044 Assert(pPt);
3045 Assert(PGMPOOL_PAGE_IS_NESTED(pShwPage));
3046 if (rc == VINF_SUCCESS)
3047 {
3048 /* The 4K PTEs shall inherit the flags of the 2M PDE page sans the leaf bit. */
3049 uint64_t const fGstShwPteFlags = fGstShwBigPdeFlags & ~EPT_E_LEAF;
3050
3051 /* Sync each 4K pages in the 2M range. */
3052 for (unsigned iPte = 0; iPte < RT_ELEMENTS(pPt->a); iPte++)
3053 {
3054 RTGCPHYS const GCPhysSubPage = GCPhysPt | (iPte << GUEST_PAGE_SHIFT);
3055 SLATPTE GstSlatPte;
3056 GstSlatPte.u = GCPhysSubPage | fGstShwPteFlags;
3057 Assert(!(GstSlatPte.u & pVCpu->pgm.s.fGstEptMbzPteMask));
3058 PGM_BTH_NAME(NestedSyncPageWorker)(pVCpu, &pPt->a[iPte], GCPhysSubPage, pShwPage, iPte, GstSlatPte);
3059 Log7Func(("GstPte=%RGp ShwPte=%RX64 iPte=%u [2M->4K]\n", pGstWalkAll->u.Ept.Pte, pPt->a[iPte].u, iPte));
3060 if (RT_UNLIKELY(VM_FF_IS_SET(pVM, VM_FF_PGM_NO_MEMORY)))
3061 break;
3062 }
3063 }
3064 else
3065 {
3066 Assert(rc == VINF_PGM_CACHED_PAGE);
3067# if defined(VBOX_STRICT) && defined(DEBUG_ramshankar)
3068 /* Paranoia - Verify address of each of the subpages are what they should be. */
3069 RTGCPHYS GCPhysSubPage = GCPhysPt;
3070 for (unsigned iPte = 0; iPte < RT_ELEMENTS(pPt->a); iPte++, GCPhysSubPage += GUEST_PAGE_SIZE)
3071 {
3072 PPGMPAGE pSubPage;
3073 rc = pgmPhysGetPageEx(pVM, GCPhysSubPage, &pSubPage);
3074 AssertRC(rc);
3075 AssertMsg( PGM_PAGE_GET_HCPHYS(pSubPage) == SHW_PTE_GET_HCPHYS(pPt->a[iPte])
3076 || !SHW_PTE_IS_P(pPt->a[iPte]),
3077 ("PGM 2M page and shadow PTE conflict. GCPhysSubPage=%RGp Page=%RHp Shw=%RHp\n",
3078 GCPhysSubPage, PGM_PAGE_GET_HCPHYS(pSubPage), SHW_PTE_GET_HCPHYS(pPt->a[iPte])));
3079 }
3080# endif
3081 rc = VINF_SUCCESS; /* Cached entry; assume it's still fully valid. */
3082 }
3083
3084 /* Save the new PDE. */
3085 uint64_t const fShwPdeFlags = pGstWalkAll->u.Ept.Pde.u & pVCpu->pgm.s.fGstEptShadowedPdeMask;
3086 Pde.u = pShwPage->Core.Key | fShwPdeFlags;
3087 Assert(!(Pde.u & EPT_E_LEAF));
3088 Assert(!(Pde.u & pVCpu->pgm.s.fGstEptMbzPdeMask));
3089 SHW_PDE_ATOMIC_SET2(*pPde, Pde);
3090 STAM_PROFILE_STOP(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncPT), a);
3091 Log7Func(("GstPde=%RGp ShwPde=%RX64 iPde=%u\n", pGstWalkAll->u.Ept.Pde.u, pPde->u, iPde));
3092 return rc;
3093 }
3094# endif /* PGM_WITH_LARGE_PAGES */
3095
3096 /*
3097 * Allocate & map the shadow page table.
3098 */
3099 PSHWPT pPt;
3100 PPGMPOOLPAGE pShwPage;
3101
3102 RTGCPHYS const GCPhysPt = pGstWalkAll->u.Ept.Pde.u & EPT_PDE_PG_MASK;
3103 rc = pgmPoolAlloc(pVM, GCPhysPt, PGMPOOLKIND_EPT_PT_FOR_EPT_PT, PGMPOOLACCESS_DONTCARE,
3104 PGM_A20_IS_ENABLED(pVCpu), pShwPde->idx, iPde, false /*fLockPage*/, &pShwPage);
3105 if ( rc == VINF_SUCCESS
3106 || rc == VINF_PGM_CACHED_PAGE)
3107 { /* likely */ }
3108 else
3109 {
3110 STAM_PROFILE_STOP(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncPT), a);
3111 AssertMsgFailedReturn(("rc=%Rrc\n", rc), RT_FAILURE_NP(rc) ? rc : VERR_IPE_UNEXPECTED_INFO_STATUS);
3112 }
3113
3114 pPt = (PSHWPT)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPage);
3115 Assert(pPt);
3116 Assert(PGMPOOL_PAGE_IS_NESTED(pShwPage));
3117
3118 if (rc == VINF_SUCCESS)
3119 {
3120 /* Sync the page we've already translated through SLAT. */
3121 const unsigned iPte = (GCPhysNestedPage >> SHW_PT_SHIFT) & SHW_PT_MASK;
3122 PGM_BTH_NAME(NestedSyncPageWorker)(pVCpu, &pPt->a[iPte], GCPhysPage, pShwPage, iPte, pGstWalkAll->u.Ept.Pte);
3123 Log7Func(("GstPte=%RGp ShwPte=%RX64 iPte=%u\n", pGstWalkAll->u.Ept.Pte.u, pPt->a[iPte].u, iPte));
3124
3125 /* Sync the rest of page table (expensive but might be cheaper than nested-guest VM-exits in hardware). */
3126 for (unsigned iPteCur = 0; iPteCur < RT_ELEMENTS(pPt->a); iPteCur++)
3127 {
3128 if (iPteCur != iPte)
3129 {
3130 PGMPTWALKGST GstWalkPt;
3131 PGMPTWALK WalkPt;
3132 GCPhysNestedPage &= ~(SHW_PT_MASK << SHW_PT_SHIFT);
3133 GCPhysNestedPage |= (iPteCur << GUEST_PAGE_SHIFT);
3134 int const rc2 = pgmGstSlatWalk(pVCpu, GCPhysNestedPage, false /*fIsLinearAddrValid*/, 0 /*GCPtrNested*/,
3135 &WalkPt, &GstWalkPt);
3136 if (RT_SUCCESS(rc2))
3137 {
3138 PGM_BTH_NAME(NestedSyncPageWorker)(pVCpu, &pPt->a[iPteCur], WalkPt.GCPhys, pShwPage, iPteCur,
3139 GstWalkPt.u.Ept.Pte);
3140 Log7Func(("GstPte=%RGp ShwPte=%RX64 iPte=%u\n", GstWalkPt.u.Ept.Pte.u, pPt->a[iPteCur].u, iPteCur));
3141 }
3142 else
3143 {
3144 /*
3145 * This could be MMIO pages reserved by the nested-hypevisor or genuinely not-present pages.
3146 * Ensure the shadow tables entry is not-present.
3147 */
3148 /** @todo We currently don't configure these to cause EPT misconfigs but rather trap
3149 * them using EPT violations and walk the guest EPT tables to determine
3150 * whether they are EPT misconfigs VM-exits for the nested-hypervisor. We
3151 * could optimize this by using a specific combination of reserved bits
3152 * which we could immediately identify as EPT misconfigs of the
3153 * nested-hypervisor without having to walk its EPT tables. However, tracking
3154 * non-present entries might be tricky...
3155 */
3156 AssertMsg(!pPt->a[iPteCur].u, ("%RX64\n", pPt->a[iPteCur].u));
3157 }
3158 if (RT_UNLIKELY(VM_FF_IS_SET(pVM, VM_FF_PGM_NO_MEMORY)))
3159 break;
3160 }
3161 }
3162 }
3163 else
3164 {
3165 Assert(rc == VINF_PGM_CACHED_PAGE);
3166# if defined(VBOX_STRICT) && defined(DEBUG_ramshankar)
3167 /* Paranoia - Verify address of the page is what it should be. */
3168 PPGMPAGE pPage;
3169 rc = pgmPhysGetPageEx(pVM, GCPhysPage, &pPage);
3170 AssertRC(rc);
3171 const unsigned iPte = (GCPhysNestedPage >> SHW_PT_SHIFT) & SHW_PT_MASK;
3172 AssertMsg(PGM_PAGE_GET_HCPHYS(pPage) == SHW_PTE_GET_HCPHYS(pPt->a[iPte]) || !SHW_PTE_IS_P(pPt->a[iPte]),
3173 ("PGM page and shadow PTE address conflict. GCPhysNestedPage=%RGp GCPhysPage=%RGp Page=%RHp Shw=%RHp\n",
3174 GCPhysNestedPage, GCPhysPage, PGM_PAGE_GET_HCPHYS(pPage), SHW_PTE_GET_HCPHYS(pPt->a[iPte])));
3175 Log7Func(("GstPte=%RGp ShwPte=%RX64 iPte=%u [cache]\n", pGstWalkAll->u.Ept.Pte.u, pPt->a[iPte].u, iPte));
3176# endif
3177 rc = VINF_SUCCESS; /* Cached entry; assume it's still fully valid. */
3178 }
3179
3180 /* Save the new PDE. */
3181 uint64_t const fShwPdeFlags = pGstWalkAll->u.Ept.Pde.u & pVCpu->pgm.s.fGstEptShadowedPdeMask;
3182 Assert(!(pGstWalkAll->u.Ept.Pde.u & EPT_E_LEAF));
3183 Assert(!(pGstWalkAll->u.Ept.Pde.u & pVCpu->pgm.s.fGstEptMbzPdeMask));
3184 Pde.u = pShwPage->Core.Key | fShwPdeFlags;
3185 SHW_PDE_ATOMIC_SET2(*pPde, Pde);
3186 Log7Func(("GstPde=%RGp ShwPde=%RX64 iPde=%u\n", pGstWalkAll->u.Ept.Pde.u, pPde->u, iPde));
3187
3188 STAM_PROFILE_STOP(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncPT), a);
3189 return rc;
3190}
3191
3192#endif /* !IN_RING3 && VBOX_WITH_NESTED_HWVIRT_VMX_EPT && PGM_SHW_TYPE == PGM_TYPE_EPT*/
3193#if PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE) && PGM_SHW_TYPE != PGM_TYPE_NONE
3194
3195/**
3196 * Handle dirty bit tracking faults.
3197 *
3198 * @returns VBox status code.
3199 * @param pVCpu The cross context virtual CPU structure.
3200 * @param uErr Page fault error code.
3201 * @param pPdeSrc Guest page directory entry.
3202 * @param pPdeDst Shadow page directory entry.
3203 * @param GCPtrPage Guest context page address.
3204 */
3205static int PGM_BTH_NAME(CheckDirtyPageFault)(PVMCPUCC pVCpu, uint32_t uErr, PSHWPDE pPdeDst, GSTPDE const *pPdeSrc,
3206 RTGCPTR GCPtrPage)
3207{
3208 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
3209 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
3210 NOREF(uErr);
3211
3212 PGM_LOCK_ASSERT_OWNER(pVM);
3213
3214 /*
3215 * Handle big page.
3216 */
3217 if ((pPdeSrc->u & X86_PDE_PS) && GST_IS_PSE_ACTIVE(pVCpu))
3218 {
3219 if ((pPdeDst->u & (X86_PDE_P | PGM_PDFLAGS_TRACK_DIRTY)) == (X86_PDE_P | PGM_PDFLAGS_TRACK_DIRTY))
3220 {
3221 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,DirtyPageTrap));
3222 Assert(pPdeSrc->u & X86_PDE_RW);
3223
3224 /* Note: No need to invalidate this entry on other VCPUs as a stale TLB entry will not harm; write access will simply
3225 * fault again and take this path to only invalidate the entry (see below). */
3226 SHWPDE PdeDst = *pPdeDst;
3227 PdeDst.u &= ~(SHWUINT)PGM_PDFLAGS_TRACK_DIRTY;
3228 PdeDst.u |= X86_PDE_RW | X86_PDE_A;
3229 SHW_PDE_ATOMIC_SET2(*pPdeDst, PdeDst);
3230 PGM_INVL_BIG_PG(pVCpu, GCPtrPage);
3231 return VINF_PGM_HANDLED_DIRTY_BIT_FAULT; /* restarts the instruction. */
3232 }
3233
3234# ifdef IN_RING0
3235 /* Check for stale TLB entry; only applies to the SMP guest case. */
3236 if ( pVM->cCpus > 1
3237 && (pPdeDst->u & (X86_PDE_P | X86_PDE_RW | X86_PDE_A)) == (X86_PDE_P | X86_PDE_RW | X86_PDE_A))
3238 {
3239 PPGMPOOLPAGE pShwPage = pgmPoolGetPage(pPool, pPdeDst->u & SHW_PDE_PG_MASK);
3240 if (pShwPage)
3241 {
3242 PSHWPT pPTDst = (PSHWPT)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPage);
3243 PSHWPTE pPteDst = &pPTDst->a[(GCPtrPage >> SHW_PT_SHIFT) & SHW_PT_MASK];
3244 if (SHW_PTE_IS_P_RW(*pPteDst))
3245 {
3246 /* Stale TLB entry. */
3247 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,DirtyPageStale));
3248 PGM_INVL_PG(pVCpu, GCPtrPage);
3249 return VINF_PGM_HANDLED_DIRTY_BIT_FAULT; /* restarts the instruction. */
3250 }
3251 }
3252 }
3253# endif /* IN_RING0 */
3254 return VINF_PGM_NO_DIRTY_BIT_TRACKING;
3255 }
3256
3257 /*
3258 * Map the guest page table.
3259 */
3260 PGSTPT pPTSrc;
3261 int rc = PGM_GCPHYS_2_PTR_V2(pVM, pVCpu, GST_GET_PDE_GCPHYS(*pPdeSrc), &pPTSrc);
3262 AssertRCReturn(rc, rc);
3263
3264 if (SHW_PDE_IS_P(*pPdeDst))
3265 {
3266 GSTPTE const *pPteSrc = &pPTSrc->a[(GCPtrPage >> GST_PT_SHIFT) & GST_PT_MASK];
3267 const GSTPTE PteSrc = *pPteSrc;
3268
3269 /*
3270 * Map shadow page table.
3271 */
3272 PPGMPOOLPAGE pShwPage = pgmPoolGetPage(pPool, pPdeDst->u & SHW_PDE_PG_MASK);
3273 if (pShwPage)
3274 {
3275 PSHWPT pPTDst = (PSHWPT)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPage);
3276 PSHWPTE pPteDst = &pPTDst->a[(GCPtrPage >> SHW_PT_SHIFT) & SHW_PT_MASK];
3277 if (SHW_PTE_IS_P(*pPteDst)) /** @todo Optimize accessed bit emulation? */
3278 {
3279 if (SHW_PTE_IS_TRACK_DIRTY(*pPteDst))
3280 {
3281 PPGMPAGE pPage = pgmPhysGetPage(pVM, GST_GET_PTE_GCPHYS(PteSrc));
3282 SHWPTE PteDst = *pPteDst;
3283
3284 LogFlow(("DIRTY page trap addr=%RGv\n", GCPtrPage));
3285 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,DirtyPageTrap));
3286
3287 Assert(PteSrc.u & X86_PTE_RW);
3288
3289 /* Note: No need to invalidate this entry on other VCPUs as a stale TLB
3290 * entry will not harm; write access will simply fault again and
3291 * take this path to only invalidate the entry.
3292 */
3293 if (RT_LIKELY(pPage))
3294 {
3295 if (PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage) && !PGM_PAGE_IS_HNDL_PHYS_NOT_IN_HM(pPage))
3296 {
3297 //AssertMsgFailed(("%R[pgmpage] - we don't set PGM_PTFLAGS_TRACK_DIRTY for these pages\n", pPage));
3298 Assert(!PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage));
3299 /* Assuming write handlers here as the PTE is present (otherwise we wouldn't be here). */
3300 SHW_PTE_SET_RO(PteDst);
3301 }
3302 else
3303 {
3304 if ( PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_WRITE_MONITORED
3305 && PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM)
3306 {
3307 rc = pgmPhysPageMakeWritable(pVM, pPage, GST_GET_PTE_GCPHYS(PteSrc));
3308 AssertRC(rc);
3309 }
3310 if (PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ALLOCATED)
3311 SHW_PTE_SET_RW(PteDst);
3312 else
3313 {
3314 /* Still applies to shared pages. */
3315 Assert(!PGM_PAGE_IS_ZERO(pPage));
3316 SHW_PTE_SET_RO(PteDst);
3317 }
3318 }
3319 }
3320 else
3321 SHW_PTE_SET_RW(PteDst); /** @todo r=bird: This doesn't make sense to me. */
3322
3323 SHW_PTE_SET(PteDst, (SHW_PTE_GET_U(PteDst) | X86_PTE_D | X86_PTE_A) & ~(uint64_t)PGM_PTFLAGS_TRACK_DIRTY);
3324 SHW_PTE_ATOMIC_SET2(*pPteDst, PteDst);
3325 PGM_INVL_PG(pVCpu, GCPtrPage);
3326 return VINF_PGM_HANDLED_DIRTY_BIT_FAULT; /* restarts the instruction. */
3327 }
3328
3329# ifdef IN_RING0
3330 /* Check for stale TLB entry; only applies to the SMP guest case. */
3331 if ( pVM->cCpus > 1
3332 && SHW_PTE_IS_RW(*pPteDst)
3333 && SHW_PTE_IS_A(*pPteDst))
3334 {
3335 /* Stale TLB entry. */
3336 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,DirtyPageStale));
3337 PGM_INVL_PG(pVCpu, GCPtrPage);
3338 return VINF_PGM_HANDLED_DIRTY_BIT_FAULT; /* restarts the instruction. */
3339 }
3340# endif
3341 }
3342 }
3343 else
3344 AssertMsgFailed(("pgmPoolGetPageByHCPhys %RGp failed!\n", pPdeDst->u & SHW_PDE_PG_MASK));
3345 }
3346
3347 return VINF_PGM_NO_DIRTY_BIT_TRACKING;
3348}
3349
3350#endif /* PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE) && PGM_SHW_TYPE != PGM_TYPE_NONE */
3351
3352/**
3353 * Sync a shadow page table.
3354 *
3355 * The shadow page table is not present in the shadow PDE.
3356 *
3357 * Handles mapping conflicts.
3358 *
3359 * This is called by VerifyAccessSyncPage, PrefetchPage, InvalidatePage (on
3360 * conflict), and Trap0eHandler.
3361 *
3362 * A precondition for this method is that the shadow PDE is not present. The
3363 * caller must take the PGM lock before checking this and continue to hold it
3364 * when calling this method.
3365 *
3366 * @returns VBox status code.
3367 * @param pVCpu The cross context virtual CPU structure.
3368 * @param iPDSrc Page directory index.
3369 * @param pPDSrc Source page directory (i.e. Guest OS page directory).
3370 * Assume this is a temporary mapping.
3371 * @param GCPtrPage GC Pointer of the page that caused the fault
3372 */
3373static int PGM_BTH_NAME(SyncPT)(PVMCPUCC pVCpu, unsigned iPDSrc, PGSTPD pPDSrc, RTGCPTR GCPtrPage)
3374{
3375 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
3376 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool); NOREF(pPool);
3377
3378#if 0 /* rarely useful; leave for debugging. */
3379 STAM_COUNTER_INC(&pVCpu->pgm.s.StatSyncPtPD[iPDSrc]);
3380#endif
3381 LogFlow(("SyncPT: GCPtrPage=%RGv\n", GCPtrPage)); RT_NOREF_PV(GCPtrPage);
3382
3383 PGM_LOCK_ASSERT_OWNER(pVM);
3384
3385#if ( PGM_GST_TYPE == PGM_TYPE_32BIT \
3386 || PGM_GST_TYPE == PGM_TYPE_PAE \
3387 || PGM_GST_TYPE == PGM_TYPE_AMD64) \
3388 && !PGM_TYPE_IS_NESTED_OR_EPT(PGM_SHW_TYPE) \
3389 && PGM_SHW_TYPE != PGM_TYPE_NONE
3390 int rc = VINF_SUCCESS;
3391
3392 STAM_PROFILE_START(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncPT), a);
3393
3394 /*
3395 * Some input validation first.
3396 */
3397 AssertMsg(iPDSrc == ((GCPtrPage >> GST_PD_SHIFT) & GST_PD_MASK), ("iPDSrc=%x GCPtrPage=%RGv\n", iPDSrc, GCPtrPage));
3398
3399 /*
3400 * Get the relevant shadow PDE entry.
3401 */
3402# if PGM_SHW_TYPE == PGM_TYPE_32BIT
3403 const unsigned iPDDst = GCPtrPage >> SHW_PD_SHIFT;
3404 PSHWPDE pPdeDst = pgmShwGet32BitPDEPtr(pVCpu, GCPtrPage);
3405
3406 /* Fetch the pgm pool shadow descriptor. */
3407 PPGMPOOLPAGE pShwPde = pVCpu->pgm.s.CTX_SUFF(pShwPageCR3);
3408 Assert(pShwPde);
3409
3410# elif PGM_SHW_TYPE == PGM_TYPE_PAE
3411 const unsigned iPDDst = (GCPtrPage >> SHW_PD_SHIFT) & SHW_PD_MASK;
3412 PPGMPOOLPAGE pShwPde = NULL;
3413 PX86PDPAE pPDDst;
3414 PSHWPDE pPdeDst;
3415
3416 /* Fetch the pgm pool shadow descriptor. */
3417 rc = pgmShwGetPaePoolPagePD(pVCpu, GCPtrPage, &pShwPde);
3418 AssertRCSuccessReturn(rc, rc);
3419 Assert(pShwPde);
3420
3421 pPDDst = (PX86PDPAE)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPde);
3422 pPdeDst = &pPDDst->a[iPDDst];
3423
3424# elif PGM_SHW_TYPE == PGM_TYPE_AMD64
3425 const unsigned iPdpt = (GCPtrPage >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
3426 const unsigned iPDDst = (GCPtrPage >> SHW_PD_SHIFT) & SHW_PD_MASK;
3427 PX86PDPAE pPDDst = NULL; /* initialized to shut up gcc */
3428 PX86PDPT pPdptDst = NULL; /* initialized to shut up gcc */
3429 rc = pgmShwGetLongModePDPtr(pVCpu, GCPtrPage, NULL, &pPdptDst, &pPDDst);
3430 AssertRCSuccessReturn(rc, rc);
3431 Assert(pPDDst);
3432 PSHWPDE pPdeDst = &pPDDst->a[iPDDst];
3433
3434# endif
3435 SHWPDE PdeDst = *pPdeDst;
3436
3437# if PGM_GST_TYPE == PGM_TYPE_AMD64
3438 /* Fetch the pgm pool shadow descriptor. */
3439 PPGMPOOLPAGE pShwPde = pgmPoolGetPage(pPool, pPdptDst->a[iPdpt].u & X86_PDPE_PG_MASK);
3440 Assert(pShwPde);
3441# endif
3442
3443 Assert(!SHW_PDE_IS_P(PdeDst)); /* We're only supposed to call SyncPT on PDE!P.*/
3444
3445 /*
3446 * Sync the page directory entry.
3447 */
3448 GSTPDE PdeSrc = pPDSrc->a[iPDSrc];
3449 const bool fPageTable = !(PdeSrc.u & X86_PDE_PS) || !GST_IS_PSE_ACTIVE(pVCpu);
3450 if ( (PdeSrc.u & X86_PDE_P)
3451 && (fPageTable ? GST_IS_PDE_VALID(pVCpu, PdeSrc) : GST_IS_BIG_PDE_VALID(pVCpu, PdeSrc)) )
3452 {
3453 /*
3454 * Allocate & map the page table.
3455 */
3456 PSHWPT pPTDst;
3457 PPGMPOOLPAGE pShwPage;
3458 RTGCPHYS GCPhys;
3459 if (fPageTable)
3460 {
3461 GCPhys = GST_GET_PDE_GCPHYS(PdeSrc);
3462# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
3463 /* Select the right PDE as we're emulating a 4kb page table with 2 shadow page tables. */
3464 GCPhys = PGM_A20_APPLY(pVCpu, GCPhys | ((iPDDst & 1) * (GUEST_PAGE_SIZE / 2)));
3465# endif
3466 rc = pgmPoolAlloc(pVM, GCPhys, BTH_PGMPOOLKIND_PT_FOR_PT, PGMPOOLACCESS_DONTCARE, PGM_A20_IS_ENABLED(pVCpu),
3467 pShwPde->idx, iPDDst, false /*fLockPage*/,
3468 &pShwPage);
3469 }
3470 else
3471 {
3472 PGMPOOLACCESS enmAccess;
3473# if PGM_WITH_NX(PGM_GST_TYPE, PGM_SHW_TYPE)
3474 const bool fNoExecute = (PdeSrc.u & X86_PDE_PAE_NX) && GST_IS_NX_ACTIVE(pVCpu);
3475# else
3476 const bool fNoExecute = false;
3477# endif
3478
3479 GCPhys = GST_GET_BIG_PDE_GCPHYS(pVM, PdeSrc);
3480# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
3481 /* Select the right PDE as we're emulating a 4MB page directory with two 2 MB shadow PDEs.*/
3482 GCPhys = PGM_A20_APPLY(pVCpu, GCPhys | (GCPtrPage & (1 << X86_PD_PAE_SHIFT)));
3483# endif
3484 /* Determine the right kind of large page to avoid incorrect cached entry reuse. */
3485 if (PdeSrc.u & X86_PDE_US)
3486 {
3487 if (PdeSrc.u & X86_PDE_RW)
3488 enmAccess = (fNoExecute) ? PGMPOOLACCESS_USER_RW_NX : PGMPOOLACCESS_USER_RW;
3489 else
3490 enmAccess = (fNoExecute) ? PGMPOOLACCESS_USER_R_NX : PGMPOOLACCESS_USER_R;
3491 }
3492 else
3493 {
3494 if (PdeSrc.u & X86_PDE_RW)
3495 enmAccess = (fNoExecute) ? PGMPOOLACCESS_SUPERVISOR_RW_NX : PGMPOOLACCESS_SUPERVISOR_RW;
3496 else
3497 enmAccess = (fNoExecute) ? PGMPOOLACCESS_SUPERVISOR_R_NX : PGMPOOLACCESS_SUPERVISOR_R;
3498 }
3499 rc = pgmPoolAlloc(pVM, GCPhys, BTH_PGMPOOLKIND_PT_FOR_BIG, enmAccess, PGM_A20_IS_ENABLED(pVCpu),
3500 pShwPde->idx, iPDDst, false /*fLockPage*/,
3501 &pShwPage);
3502 }
3503 if (rc == VINF_SUCCESS)
3504 pPTDst = (PSHWPT)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPage);
3505 else if (rc == VINF_PGM_CACHED_PAGE)
3506 {
3507 /*
3508 * The PT was cached, just hook it up.
3509 */
3510 if (fPageTable)
3511 PdeDst.u = pShwPage->Core.Key | GST_GET_PDE_SHW_FLAGS(pVCpu, PdeSrc);
3512 else
3513 {
3514 PdeDst.u = pShwPage->Core.Key | GST_GET_BIG_PDE_SHW_FLAGS(pVCpu, PdeSrc);
3515 /* (see explanation and assumptions further down.) */
3516 if ((PdeSrc.u & (X86_PDE_RW | X86_PDE4M_D)) == X86_PDE_RW)
3517 {
3518 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,DirtyPageBig));
3519 PdeDst.u |= PGM_PDFLAGS_TRACK_DIRTY;
3520 PdeDst.u &= ~(SHWUINT)X86_PDE_RW;
3521 }
3522 }
3523 SHW_PDE_ATOMIC_SET2(*pPdeDst, PdeDst);
3524 PGM_DYNMAP_UNUSED_HINT(pVCpu, pPdeDst);
3525 return VINF_SUCCESS;
3526 }
3527 else
3528 AssertMsgFailedReturn(("rc=%Rrc\n", rc), RT_FAILURE_NP(rc) ? rc : VERR_IPE_UNEXPECTED_INFO_STATUS);
3529 /** @todo Why do we bother preserving X86_PDE_AVL_MASK here?
3530 * Both PGM_PDFLAGS_MAPPING and PGM_PDFLAGS_TRACK_DIRTY should be
3531 * irrelevant at this point. */
3532 PdeDst.u &= X86_PDE_AVL_MASK;
3533 PdeDst.u |= pShwPage->Core.Key;
3534
3535 /*
3536 * Page directory has been accessed (this is a fault situation, remember).
3537 */
3538 /** @todo
3539 * Well, when the caller is PrefetchPage or InvalidatePage is isn't a
3540 * fault situation. What's more, the Trap0eHandler has already set the
3541 * accessed bit. So, it's actually just VerifyAccessSyncPage which
3542 * might need setting the accessed flag.
3543 *
3544 * The best idea is to leave this change to the caller and add an
3545 * assertion that it's set already. */
3546 pPDSrc->a[iPDSrc].u |= X86_PDE_A;
3547 if (fPageTable)
3548 {
3549 /*
3550 * Page table - 4KB.
3551 *
3552 * Sync all or just a few entries depending on PGM_SYNC_N_PAGES.
3553 */
3554 Log2(("SyncPT: 4K %RGv PdeSrc:{P=%d RW=%d U=%d raw=%08llx}\n",
3555 GCPtrPage, PdeSrc.u & X86_PTE_P, !!(PdeSrc.u & X86_PTE_RW), !!(PdeSrc.u & X86_PDE_US), (uint64_t)PdeSrc.u));
3556 PGSTPT pPTSrc;
3557 rc = PGM_GCPHYS_2_PTR(pVM, GST_GET_PDE_GCPHYS(PdeSrc), &pPTSrc);
3558 if (RT_SUCCESS(rc))
3559 {
3560 /*
3561 * Start by syncing the page directory entry so CSAM's TLB trick works.
3562 */
3563 PdeDst.u = (PdeDst.u & (SHW_PDE_PG_MASK | X86_PDE_AVL_MASK))
3564 | GST_GET_PDE_SHW_FLAGS(pVCpu, PdeSrc);
3565 SHW_PDE_ATOMIC_SET2(*pPdeDst, PdeDst);
3566 PGM_DYNMAP_UNUSED_HINT(pVCpu, pPdeDst);
3567
3568 /*
3569 * Directory/page user or supervisor privilege: (same goes for read/write)
3570 *
3571 * Directory Page Combined
3572 * U/S U/S U/S
3573 * 0 0 0
3574 * 0 1 0
3575 * 1 0 0
3576 * 1 1 1
3577 *
3578 * Simple AND operation. Table listed for completeness.
3579 *
3580 */
3581 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncPT4K));
3582# ifdef PGM_SYNC_N_PAGES
3583 unsigned iPTBase = (GCPtrPage >> SHW_PT_SHIFT) & SHW_PT_MASK;
3584 unsigned iPTDst = iPTBase;
3585 const unsigned iPTDstEnd = RT_MIN(iPTDst + PGM_SYNC_NR_PAGES / 2, RT_ELEMENTS(pPTDst->a));
3586 if (iPTDst <= PGM_SYNC_NR_PAGES / 2)
3587 iPTDst = 0;
3588 else
3589 iPTDst -= PGM_SYNC_NR_PAGES / 2;
3590# else /* !PGM_SYNC_N_PAGES */
3591 unsigned iPTDst = 0;
3592 const unsigned iPTDstEnd = RT_ELEMENTS(pPTDst->a);
3593# endif /* !PGM_SYNC_N_PAGES */
3594 RTGCPTR GCPtrCur = (GCPtrPage & ~(RTGCPTR)((1 << SHW_PD_SHIFT) - 1))
3595 | ((RTGCPTR)iPTDst << GUEST_PAGE_SHIFT);
3596# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
3597 /* Select the right PDE as we're emulating a 4kb page table with 2 shadow page tables. */
3598 const unsigned offPTSrc = ((GCPtrPage >> SHW_PD_SHIFT) & 1) * 512;
3599# else
3600 const unsigned offPTSrc = 0;
3601# endif
3602 for (; iPTDst < iPTDstEnd; iPTDst++, GCPtrCur += GUEST_PAGE_SIZE)
3603 {
3604 const unsigned iPTSrc = iPTDst + offPTSrc;
3605 const GSTPTE PteSrc = pPTSrc->a[iPTSrc];
3606 if (PteSrc.u & X86_PTE_P)
3607 {
3608 PGM_BTH_NAME(SyncPageWorker)(pVCpu, &pPTDst->a[iPTDst], PdeSrc, PteSrc, pShwPage, iPTDst);
3609 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",
3610 GCPtrCur,
3611 PteSrc.u & X86_PTE_P,
3612 !!(PteSrc.u & PdeSrc.u & X86_PTE_RW),
3613 !!(PteSrc.u & PdeSrc.u & X86_PTE_US),
3614 (uint64_t)PteSrc.u,
3615 SHW_PTE_IS_TRACK_DIRTY(pPTDst->a[iPTDst]) ? " Track-Dirty" : "", SHW_PTE_LOG64(pPTDst->a[iPTDst]), iPTSrc, PdeSrc.au32[0],
3616 (RTGCPHYS)(GST_GET_PDE_GCPHYS(PdeSrc) + iPTSrc*sizeof(PteSrc)) ));
3617 }
3618 /* else: the page table was cleared by the pool */
3619 } /* for PTEs */
3620 }
3621 }
3622 else
3623 {
3624 /*
3625 * Big page - 2/4MB.
3626 *
3627 * We'll walk the ram range list in parallel and optimize lookups.
3628 * We will only sync one shadow page table at a time.
3629 */
3630 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncPT4M));
3631
3632 /**
3633 * @todo It might be more efficient to sync only a part of the 4MB
3634 * page (similar to what we do for 4KB PDs).
3635 */
3636
3637 /*
3638 * Start by syncing the page directory entry.
3639 */
3640 PdeDst.u = (PdeDst.u & (SHW_PDE_PG_MASK | (X86_PDE_AVL_MASK & ~PGM_PDFLAGS_TRACK_DIRTY)))
3641 | GST_GET_BIG_PDE_SHW_FLAGS(pVCpu, PdeSrc);
3642
3643 /*
3644 * If the page is not flagged as dirty and is writable, then make it read-only
3645 * at PD level, so we can set the dirty bit when the page is modified.
3646 *
3647 * ASSUMES that page access handlers are implemented on page table entry level.
3648 * Thus we will first catch the dirty access and set PDE.D and restart. If
3649 * there is an access handler, we'll trap again and let it work on the problem.
3650 */
3651 /** @todo move the above stuff to a section in the PGM documentation. */
3652 Assert(!(PdeDst.u & PGM_PDFLAGS_TRACK_DIRTY));
3653 if ((PdeSrc.u & (X86_PDE_RW | X86_PDE4M_D)) == X86_PDE_RW)
3654 {
3655 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,DirtyPageBig));
3656 PdeDst.u |= PGM_PDFLAGS_TRACK_DIRTY;
3657 PdeDst.u &= ~(SHWUINT)X86_PDE_RW;
3658 }
3659 SHW_PDE_ATOMIC_SET2(*pPdeDst, PdeDst);
3660 PGM_DYNMAP_UNUSED_HINT(pVCpu, pPdeDst);
3661
3662 /*
3663 * Fill the shadow page table.
3664 */
3665 /* Get address and flags from the source PDE. */
3666 SHWPTE PteDstBase;
3667 SHW_PTE_SET(PteDstBase, GST_GET_BIG_PDE_SHW_FLAGS_4_PTE(pVCpu, PdeSrc));
3668
3669 /* Loop thru the entries in the shadow PT. */
3670 const RTGCPTR GCPtr = (GCPtrPage >> SHW_PD_SHIFT) << SHW_PD_SHIFT; NOREF(GCPtr);
3671 Log2(("SyncPT: BIG %RGv PdeSrc:{P=%d RW=%d U=%d raw=%08llx} Shw=%RGv GCPhys=%RGp %s\n",
3672 GCPtrPage, PdeSrc.u & X86_PDE_P, !!(PdeSrc.u & X86_PDE_RW), !!(PdeSrc.u & X86_PDE_US), (uint64_t)PdeSrc.u, GCPtr,
3673 GCPhys, PdeDst.u & PGM_PDFLAGS_TRACK_DIRTY ? " Track-Dirty" : ""));
3674 PPGMRAMRANGE pRam = pgmPhysGetRangeAtOrAbove(pVM, GCPhys);
3675 unsigned iPTDst = 0;
3676 while ( iPTDst < RT_ELEMENTS(pPTDst->a)
3677 && !VM_FF_IS_SET(pVM, VM_FF_PGM_NO_MEMORY))
3678 {
3679 if (pRam && GCPhys >= pRam->GCPhys)
3680 {
3681# ifndef PGM_WITH_A20
3682 unsigned iHCPage = (GCPhys - pRam->GCPhys) >> GUEST_PAGE_SHIFT;
3683# endif
3684 do
3685 {
3686 /* Make shadow PTE. */
3687# ifdef PGM_WITH_A20
3688 PPGMPAGE pPage = &pRam->aPages[(GCPhys - pRam->GCPhys) >> GUEST_PAGE_SHIFT];
3689# else
3690 PPGMPAGE pPage = &pRam->aPages[iHCPage];
3691# endif
3692 SHWPTE PteDst;
3693
3694# ifndef VBOX_WITH_NEW_LAZY_PAGE_ALLOC
3695 /* Try to make the page writable if necessary. */
3696 if ( PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM
3697 && ( PGM_PAGE_IS_ZERO(pPage)
3698 || ( SHW_PTE_IS_RW(PteDstBase)
3699 && PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED
3700# ifdef VBOX_WITH_REAL_WRITE_MONITORED_PAGES
3701 && PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_WRITE_MONITORED
3702# endif
3703# ifdef VBOX_WITH_PAGE_SHARING
3704 && PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_SHARED
3705# endif
3706 && !PGM_PAGE_IS_BALLOONED(pPage))
3707 )
3708 )
3709 {
3710 rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
3711 AssertRCReturn(rc, rc);
3712 if (VM_FF_IS_SET(pVM, VM_FF_PGM_NO_MEMORY))
3713 break;
3714 }
3715# endif
3716
3717 if (PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage) && !PGM_PAGE_IS_HNDL_PHYS_NOT_IN_HM(pPage))
3718 PGM_BTH_NAME(SyncHandlerPte)(pVM, pVCpu, pPage, GCPhys, SHW_PTE_GET_U(PteDstBase), &PteDst);
3719 else if (PGM_PAGE_IS_BALLOONED(pPage))
3720 SHW_PTE_SET(PteDst, 0); /* Handle ballooned pages at #PF time. */
3721 else
3722 SHW_PTE_SET(PteDst, PGM_PAGE_GET_HCPHYS(pPage) | SHW_PTE_GET_U(PteDstBase));
3723
3724 /* Only map writable pages writable. */
3725 if ( SHW_PTE_IS_P_RW(PteDst)
3726 && PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED)
3727 {
3728# ifndef VBOX_WITH_NEW_LAZY_PAGE_ALLOC
3729 /* Still applies to shared pages. */
3730 Assert(!PGM_PAGE_IS_ZERO(pPage));
3731# endif
3732 SHW_PTE_SET_RO(PteDst); /** @todo this isn't quite working yet... */
3733 Log3(("SyncPT: write-protecting %RGp pPage=%R[pgmpage] at %RGv\n", GCPhys, pPage, (RTGCPTR)(GCPtr | (iPTDst << SHW_PT_SHIFT))));
3734 }
3735
3736 if (SHW_PTE_IS_P(PteDst))
3737 PGM_BTH_NAME(SyncPageWorkerTrackAddref)(pVCpu, pShwPage, PGM_PAGE_GET_TRACKING(pPage), pPage, iPTDst);
3738
3739 /* commit it (not atomic, new table) */
3740 pPTDst->a[iPTDst] = PteDst;
3741 Log4(("SyncPT: BIG %RGv PteDst:{P=%d RW=%d U=%d raw=%08llx}%s\n",
3742 (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),
3743 SHW_PTE_IS_TRACK_DIRTY(PteDst) ? " Track-Dirty" : ""));
3744
3745 /* advance */
3746 GCPhys += GUEST_PAGE_SIZE;
3747 PGM_A20_APPLY_TO_VAR(pVCpu, GCPhys);
3748# ifndef PGM_WITH_A20
3749 iHCPage++;
3750# endif
3751 iPTDst++;
3752 } while ( iPTDst < RT_ELEMENTS(pPTDst->a)
3753 && GCPhys <= pRam->GCPhysLast);
3754
3755 /* Advance ram range list. */
3756 while (pRam && GCPhys > pRam->GCPhysLast)
3757 pRam = pRam->CTX_SUFF(pNext);
3758 }
3759 else if (pRam)
3760 {
3761 Log(("Invalid pages at %RGp\n", GCPhys));
3762 do
3763 {
3764 SHW_PTE_SET(pPTDst->a[iPTDst], 0); /* Invalid page, we must handle them manually. */
3765 GCPhys += GUEST_PAGE_SIZE;
3766 iPTDst++;
3767 } while ( iPTDst < RT_ELEMENTS(pPTDst->a)
3768 && GCPhys < pRam->GCPhys);
3769 PGM_A20_APPLY_TO_VAR(pVCpu,GCPhys);
3770 }
3771 else
3772 {
3773 Log(("Invalid pages at %RGp (2)\n", GCPhys));
3774 for ( ; iPTDst < RT_ELEMENTS(pPTDst->a); iPTDst++)
3775 SHW_PTE_SET(pPTDst->a[iPTDst], 0); /* Invalid page, we must handle them manually. */
3776 }
3777 } /* while more PTEs */
3778 } /* 4KB / 4MB */
3779 }
3780 else
3781 AssertRelease(!SHW_PDE_IS_P(PdeDst));
3782
3783 STAM_PROFILE_STOP(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncPT), a);
3784 if (RT_FAILURE(rc))
3785 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncPTFailed));
3786 return rc;
3787
3788#elif (PGM_GST_TYPE == PGM_TYPE_REAL || PGM_GST_TYPE == PGM_TYPE_PROT) \
3789 && !PGM_TYPE_IS_NESTED(PGM_SHW_TYPE) \
3790 && (PGM_SHW_TYPE != PGM_TYPE_EPT || PGM_GST_TYPE == PGM_TYPE_PROT) \
3791 && PGM_SHW_TYPE != PGM_TYPE_NONE
3792 NOREF(iPDSrc); NOREF(pPDSrc);
3793
3794 STAM_PROFILE_START(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncPT), a);
3795
3796 /*
3797 * Validate input a little bit.
3798 */
3799 int rc = VINF_SUCCESS;
3800# if PGM_SHW_TYPE == PGM_TYPE_32BIT
3801 const unsigned iPDDst = (GCPtrPage >> SHW_PD_SHIFT) & SHW_PD_MASK;
3802 PSHWPDE pPdeDst = pgmShwGet32BitPDEPtr(pVCpu, GCPtrPage);
3803
3804 /* Fetch the pgm pool shadow descriptor. */
3805 PPGMPOOLPAGE pShwPde = pVCpu->pgm.s.CTX_SUFF(pShwPageCR3);
3806 Assert(pShwPde);
3807
3808# elif PGM_SHW_TYPE == PGM_TYPE_PAE
3809 const unsigned iPDDst = (GCPtrPage >> SHW_PD_SHIFT) & SHW_PD_MASK;
3810 PPGMPOOLPAGE pShwPde = NULL; /* initialized to shut up gcc */
3811 PX86PDPAE pPDDst;
3812 PSHWPDE pPdeDst;
3813
3814 /* Fetch the pgm pool shadow descriptor. */
3815 rc = pgmShwGetPaePoolPagePD(pVCpu, GCPtrPage, &pShwPde);
3816 AssertRCSuccessReturn(rc, rc);
3817 Assert(pShwPde);
3818
3819 pPDDst = (PX86PDPAE)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPde);
3820 pPdeDst = &pPDDst->a[iPDDst];
3821
3822# elif PGM_SHW_TYPE == PGM_TYPE_AMD64
3823 const unsigned iPdpt = (GCPtrPage >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
3824 const unsigned iPDDst = (GCPtrPage >> SHW_PD_SHIFT) & SHW_PD_MASK;
3825 PX86PDPAE pPDDst = NULL; /* initialized to shut up gcc */
3826 PX86PDPT pPdptDst= NULL; /* initialized to shut up gcc */
3827 rc = pgmShwGetLongModePDPtr(pVCpu, GCPtrPage, NULL, &pPdptDst, &pPDDst);
3828 AssertRCSuccessReturn(rc, rc);
3829 Assert(pPDDst);
3830 PSHWPDE pPdeDst = &pPDDst->a[iPDDst];
3831
3832 /* Fetch the pgm pool shadow descriptor. */
3833 PPGMPOOLPAGE pShwPde = pgmPoolGetPage(pPool, pPdptDst->a[iPdpt].u & X86_PDPE_PG_MASK);
3834 Assert(pShwPde);
3835
3836# elif PGM_SHW_TYPE == PGM_TYPE_EPT
3837 const unsigned iPdpt = (GCPtrPage >> EPT_PDPT_SHIFT) & EPT_PDPT_MASK;
3838 const unsigned iPDDst = ((GCPtrPage >> SHW_PD_SHIFT) & SHW_PD_MASK);
3839 PEPTPD pPDDst;
3840 PEPTPDPT pPdptDst;
3841
3842 rc = pgmShwGetEPTPDPtr(pVCpu, GCPtrPage, &pPdptDst, &pPDDst);
3843 if (rc != VINF_SUCCESS)
3844 {
3845 STAM_PROFILE_STOP(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncPT), a);
3846 AssertRC(rc);
3847 return rc;
3848 }
3849 Assert(pPDDst);
3850 PSHWPDE pPdeDst = &pPDDst->a[iPDDst];
3851
3852 /* Fetch the pgm pool shadow descriptor. */
3853 /** @todo r=bird: didn't pgmShwGetEPTPDPtr just do this lookup already? */
3854 PPGMPOOLPAGE pShwPde = pgmPoolGetPage(pPool, pPdptDst->a[iPdpt].u & EPT_PDPTE_PG_MASK);
3855 Assert(pShwPde);
3856# endif
3857 SHWPDE PdeDst = *pPdeDst;
3858
3859 Assert(!SHW_PDE_IS_P(PdeDst)); /* We're only supposed to call SyncPT on PDE!P and conflicts.*/
3860
3861# if defined(PGM_WITH_LARGE_PAGES) && PGM_SHW_TYPE != PGM_TYPE_32BIT && PGM_SHW_TYPE != PGM_TYPE_PAE
3862 if (BTH_IS_NP_ACTIVE(pVM))
3863 {
3864 Assert(!VM_IS_NEM_ENABLED(pVM));
3865
3866 /* Check if we allocated a big page before for this 2 MB range. */
3867 PPGMPAGE pPage;
3868 rc = pgmPhysGetPageEx(pVM, PGM_A20_APPLY(pVCpu, GCPtrPage & X86_PDE2M_PAE_PG_MASK), &pPage);
3869 if (RT_SUCCESS(rc))
3870 {
3871 RTHCPHYS HCPhys = NIL_RTHCPHYS;
3872 if (PGM_PAGE_GET_PDE_TYPE(pPage) == PGM_PAGE_PDE_TYPE_PDE)
3873 {
3874 if (PGM_A20_IS_ENABLED(pVCpu))
3875 {
3876 STAM_REL_COUNTER_INC(&pVM->pgm.s.StatLargePageReused);
3877 AssertRelease(PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ALLOCATED);
3878 HCPhys = PGM_PAGE_GET_HCPHYS(pPage);
3879 }
3880 else
3881 {
3882 PGM_PAGE_SET_PDE_TYPE(pVM, pPage, PGM_PAGE_PDE_TYPE_PDE_DISABLED);
3883 pVM->pgm.s.cLargePagesDisabled++;
3884 }
3885 }
3886 else if ( PGM_PAGE_GET_PDE_TYPE(pPage) == PGM_PAGE_PDE_TYPE_PDE_DISABLED
3887 && PGM_A20_IS_ENABLED(pVCpu))
3888 {
3889 /* Recheck the entire 2 MB range to see if we can use it again as a large page. */
3890 rc = pgmPhysRecheckLargePage(pVM, GCPtrPage, pPage);
3891 if (RT_SUCCESS(rc))
3892 {
3893 Assert(PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ALLOCATED);
3894 Assert(PGM_PAGE_GET_PDE_TYPE(pPage) == PGM_PAGE_PDE_TYPE_PDE);
3895 HCPhys = PGM_PAGE_GET_HCPHYS(pPage);
3896 }
3897 }
3898# if !defined(VBOX_WITH_NEW_LAZY_PAGE_ALLOC) && !defined(PGM_WITH_PAGE_ZEROING_DETECTION) /* This code is too aggresive! */
3899 else if ( PGMIsUsingLargePages(pVM)
3900 && PGM_A20_IS_ENABLED(pVCpu))
3901 {
3902 rc = pgmPhysAllocLargePage(pVM, GCPtrPage);
3903 if (RT_SUCCESS(rc))
3904 {
3905 Assert(PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ALLOCATED);
3906 Assert(PGM_PAGE_GET_PDE_TYPE(pPage) == PGM_PAGE_PDE_TYPE_PDE);
3907 HCPhys = PGM_PAGE_GET_HCPHYS(pPage);
3908 }
3909 else
3910 LogFlow(("pgmPhysAllocLargePage failed with %Rrc\n", rc));
3911 }
3912# endif
3913
3914 if (HCPhys != NIL_RTHCPHYS)
3915 {
3916# if PGM_SHW_TYPE == PGM_TYPE_EPT
3917 PdeDst.u = HCPhys | EPT_E_READ | EPT_E_WRITE | EPT_E_EXECUTE | EPT_E_LEAF | EPT_E_IGNORE_PAT | EPT_E_MEMTYPE_WB
3918 | (PdeDst.u & X86_PDE_AVL_MASK) /** @todo do we need this? */;
3919# else
3920 PdeDst.u = HCPhys | X86_PDE_P | X86_PDE_RW | X86_PDE_US | X86_PDE_PS
3921 | (PdeDst.u & X86_PDE_AVL_MASK) /** @todo PGM_PD_FLAGS? */;
3922# endif
3923 SHW_PDE_ATOMIC_SET2(*pPdeDst, PdeDst);
3924
3925 Log(("SyncPT: Use large page at %RGp PDE=%RX64\n", GCPtrPage, PdeDst.u));
3926 /* Add a reference to the first page only. */
3927 PGM_BTH_NAME(SyncPageWorkerTrackAddref)(pVCpu, pShwPde, PGM_PAGE_GET_TRACKING(pPage), pPage, iPDDst);
3928
3929 STAM_PROFILE_STOP(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncPT), a);
3930 return VINF_SUCCESS;
3931 }
3932 }
3933 }
3934# endif /* defined(PGM_WITH_LARGE_PAGES) && PGM_SHW_TYPE != PGM_TYPE_32BIT && PGM_SHW_TYPE != PGM_TYPE_PAE */
3935
3936 /*
3937 * Allocate & map the page table.
3938 */
3939 PSHWPT pPTDst;
3940 PPGMPOOLPAGE pShwPage;
3941 RTGCPHYS GCPhys;
3942
3943 /* Virtual address = physical address */
3944 GCPhys = PGM_A20_APPLY(pVCpu, GCPtrPage & X86_PAGE_4K_BASE_MASK);
3945 rc = pgmPoolAlloc(pVM, GCPhys & ~(RT_BIT_64(SHW_PD_SHIFT) - 1), BTH_PGMPOOLKIND_PT_FOR_PT, PGMPOOLACCESS_DONTCARE,
3946 PGM_A20_IS_ENABLED(pVCpu), pShwPde->idx, iPDDst, false /*fLockPage*/,
3947 &pShwPage);
3948 if ( rc == VINF_SUCCESS
3949 || rc == VINF_PGM_CACHED_PAGE)
3950 pPTDst = (PSHWPT)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPage);
3951 else
3952 {
3953 STAM_PROFILE_STOP(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncPT), a);
3954 AssertMsgFailedReturn(("rc=%Rrc\n", rc), RT_FAILURE_NP(rc) ? rc : VERR_IPE_UNEXPECTED_INFO_STATUS);
3955 }
3956
3957 if (rc == VINF_SUCCESS)
3958 {
3959 /* New page table; fully set it up. */
3960 Assert(pPTDst);
3961
3962 /* Mask away the page offset. */
3963 GCPtrPage &= ~(RTGCPTR)GUEST_PAGE_OFFSET_MASK;
3964
3965 for (unsigned iPTDst = 0; iPTDst < RT_ELEMENTS(pPTDst->a); iPTDst++)
3966 {
3967 RTGCPTR GCPtrCurPage = PGM_A20_APPLY(pVCpu, (GCPtrPage & ~(RTGCPTR)(SHW_PT_MASK << SHW_PT_SHIFT))
3968 | (iPTDst << GUEST_PAGE_SHIFT));
3969
3970 PGM_BTH_NAME(SyncPageWorker)(pVCpu, &pPTDst->a[iPTDst], GCPtrCurPage, pShwPage, iPTDst);
3971 Log2(("SyncPage: 4K+ %RGv PteSrc:{P=1 RW=1 U=1} PteDst=%08llx%s\n",
3972 GCPtrCurPage,
3973 SHW_PTE_LOG64(pPTDst->a[iPTDst]),
3974 SHW_PTE_IS_TRACK_DIRTY(pPTDst->a[iPTDst]) ? " Track-Dirty" : ""));
3975
3976 if (RT_UNLIKELY(VM_FF_IS_SET(pVM, VM_FF_PGM_NO_MEMORY)))
3977 break;
3978 }
3979 }
3980 else
3981 rc = VINF_SUCCESS; /* Cached entry; assume it's still fully valid. */
3982
3983 /* Save the new PDE. */
3984# if PGM_SHW_TYPE == PGM_TYPE_EPT
3985 PdeDst.u = pShwPage->Core.Key | EPT_E_READ | EPT_E_WRITE | EPT_E_EXECUTE
3986 | (PdeDst.u & X86_PDE_AVL_MASK /** @todo do we really need this? */);
3987# else
3988 PdeDst.u = pShwPage->Core.Key | X86_PDE_P | X86_PDE_RW | X86_PDE_US | X86_PDE_A
3989 | (PdeDst.u & X86_PDE_AVL_MASK /** @todo use a PGM_PD_FLAGS define */);
3990# endif
3991 SHW_PDE_ATOMIC_SET2(*pPdeDst, PdeDst);
3992
3993 STAM_PROFILE_STOP(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncPT), a);
3994 if (RT_FAILURE(rc))
3995 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncPTFailed));
3996 return rc;
3997
3998#else
3999 NOREF(iPDSrc); NOREF(pPDSrc);
4000 AssertReleaseMsgFailed(("Shw=%d Gst=%d is not implemented!\n", PGM_SHW_TYPE, PGM_GST_TYPE));
4001 return VERR_PGM_NOT_USED_IN_MODE;
4002#endif
4003}
4004
4005
4006
4007/**
4008 * Prefetch a page/set of pages.
4009 *
4010 * Typically used to sync commonly used pages before entering raw mode
4011 * after a CR3 reload.
4012 *
4013 * @returns VBox status code.
4014 * @param pVCpu The cross context virtual CPU structure.
4015 * @param GCPtrPage Page to invalidate.
4016 */
4017PGM_BTH_DECL(int, PrefetchPage)(PVMCPUCC pVCpu, RTGCPTR GCPtrPage)
4018{
4019#if ( PGM_GST_TYPE == PGM_TYPE_32BIT \
4020 || PGM_GST_TYPE == PGM_TYPE_REAL \
4021 || PGM_GST_TYPE == PGM_TYPE_PROT \
4022 || PGM_GST_TYPE == PGM_TYPE_PAE \
4023 || PGM_GST_TYPE == PGM_TYPE_AMD64 ) \
4024 && !PGM_TYPE_IS_NESTED_OR_EPT(PGM_SHW_TYPE) \
4025 && PGM_SHW_TYPE != PGM_TYPE_NONE
4026 /*
4027 * Check that all Guest levels thru the PDE are present, getting the
4028 * PD and PDE in the processes.
4029 */
4030 int rc = VINF_SUCCESS;
4031# if PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE)
4032# if PGM_GST_TYPE == PGM_TYPE_32BIT
4033 const unsigned iPDSrc = (uint32_t)GCPtrPage >> GST_PD_SHIFT;
4034 PGSTPD pPDSrc = pgmGstGet32bitPDPtr(pVCpu);
4035# elif PGM_GST_TYPE == PGM_TYPE_PAE
4036 unsigned iPDSrc;
4037 X86PDPE PdpeSrc;
4038 PGSTPD pPDSrc = pgmGstGetPaePDPtr(pVCpu, GCPtrPage, &iPDSrc, &PdpeSrc);
4039 if (!pPDSrc)
4040 return VINF_SUCCESS; /* not present */
4041# elif PGM_GST_TYPE == PGM_TYPE_AMD64
4042 unsigned iPDSrc;
4043 PX86PML4E pPml4eSrc;
4044 X86PDPE PdpeSrc;
4045 PGSTPD pPDSrc = pgmGstGetLongModePDPtr(pVCpu, GCPtrPage, &pPml4eSrc, &PdpeSrc, &iPDSrc);
4046 if (!pPDSrc)
4047 return VINF_SUCCESS; /* not present */
4048# endif
4049 const GSTPDE PdeSrc = pPDSrc->a[iPDSrc];
4050# else
4051 PGSTPD pPDSrc = NULL;
4052 const unsigned iPDSrc = 0;
4053 GSTPDE const PdeSrc = { X86_PDE_P | X86_PDE_RW | X86_PDE_US | X86_PDE_A }; /* faked so we don't have to #ifdef everything */
4054# endif
4055
4056 if ((PdeSrc.u & (X86_PDE_P | X86_PDE_A)) == (X86_PDE_P | X86_PDE_A))
4057 {
4058 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
4059 PGM_LOCK_VOID(pVM);
4060
4061# if PGM_SHW_TYPE == PGM_TYPE_32BIT
4062 const X86PDE PdeDst = pgmShwGet32BitPDE(pVCpu, GCPtrPage);
4063# elif PGM_SHW_TYPE == PGM_TYPE_PAE
4064 const unsigned iPDDst = ((GCPtrPage >> SHW_PD_SHIFT) & SHW_PD_MASK);
4065 PX86PDPAE pPDDst;
4066 X86PDEPAE PdeDst;
4067# if PGM_GST_TYPE != PGM_TYPE_PAE
4068 X86PDPE PdpeSrc;
4069
4070 /* Fake PDPT entry; access control handled on the page table level, so allow everything. */
4071 PdpeSrc.u = X86_PDPE_P; /* rw/us are reserved for PAE pdpte's; accessed bit causes invalid VT-x guest state errors */
4072# endif
4073 rc = pgmShwSyncPaePDPtr(pVCpu, GCPtrPage, PdpeSrc.u, &pPDDst);
4074 if (rc != VINF_SUCCESS)
4075 {
4076 PGM_UNLOCK(pVM);
4077 AssertRC(rc);
4078 return rc;
4079 }
4080 Assert(pPDDst);
4081 PdeDst = pPDDst->a[iPDDst];
4082
4083# elif PGM_SHW_TYPE == PGM_TYPE_AMD64
4084 const unsigned iPDDst = ((GCPtrPage >> SHW_PD_SHIFT) & SHW_PD_MASK);
4085 PX86PDPAE pPDDst;
4086 X86PDEPAE PdeDst;
4087
4088# if PGM_GST_TYPE == PGM_TYPE_PROT
4089 /* AMD-V nested paging */
4090 X86PML4E Pml4eSrc;
4091 X86PDPE PdpeSrc;
4092 PX86PML4E pPml4eSrc = &Pml4eSrc;
4093
4094 /* Fake PML4 & PDPT entry; access control handled on the page table level, so allow everything. */
4095 Pml4eSrc.u = X86_PML4E_P | X86_PML4E_RW | X86_PML4E_US | X86_PML4E_A;
4096 PdpeSrc.u = X86_PDPE_P | X86_PDPE_RW | X86_PDPE_US | X86_PDPE_A;
4097# endif
4098
4099 rc = pgmShwSyncLongModePDPtr(pVCpu, GCPtrPage, pPml4eSrc->u, PdpeSrc.u, &pPDDst);
4100 if (rc != VINF_SUCCESS)
4101 {
4102 PGM_UNLOCK(pVM);
4103 AssertRC(rc);
4104 return rc;
4105 }
4106 Assert(pPDDst);
4107 PdeDst = pPDDst->a[iPDDst];
4108# endif
4109 if (!(PdeDst.u & X86_PDE_P))
4110 {
4111 /** @todo r=bird: This guy will set the A bit on the PDE,
4112 * probably harmless. */
4113 rc = PGM_BTH_NAME(SyncPT)(pVCpu, iPDSrc, pPDSrc, GCPtrPage);
4114 }
4115 else
4116 {
4117 /* Note! We used to sync PGM_SYNC_NR_PAGES pages, which triggered assertions in CSAM, because
4118 * R/W attributes of nearby pages were reset. Not sure how that could happen. Anyway, it
4119 * makes no sense to prefetch more than one page.
4120 */
4121 rc = PGM_BTH_NAME(SyncPage)(pVCpu, PdeSrc, GCPtrPage, 1, 0);
4122 if (RT_SUCCESS(rc))
4123 rc = VINF_SUCCESS;
4124 }
4125 PGM_UNLOCK(pVM);
4126 }
4127 return rc;
4128
4129#elif PGM_TYPE_IS_NESTED_OR_EPT(PGM_SHW_TYPE) || PGM_SHW_TYPE == PGM_TYPE_NONE
4130 NOREF(pVCpu); NOREF(GCPtrPage);
4131 return VINF_SUCCESS; /* ignore */
4132#else
4133 AssertCompile(0);
4134#endif
4135}
4136
4137
4138
4139
4140/**
4141 * Syncs a page during a PGMVerifyAccess() call.
4142 *
4143 * @returns VBox status code (informational included).
4144 * @param pVCpu The cross context virtual CPU structure.
4145 * @param GCPtrPage The address of the page to sync.
4146 * @param fPage The effective guest page flags.
4147 * @param uErr The trap error code.
4148 * @remarks This will normally never be called on invalid guest page
4149 * translation entries.
4150 */
4151PGM_BTH_DECL(int, VerifyAccessSyncPage)(PVMCPUCC pVCpu, RTGCPTR GCPtrPage, unsigned fPage, unsigned uErr)
4152{
4153 PVMCC pVM = pVCpu->CTX_SUFF(pVM); NOREF(pVM);
4154
4155 LogFlow(("VerifyAccessSyncPage: GCPtrPage=%RGv fPage=%#x uErr=%#x\n", GCPtrPage, fPage, uErr));
4156 RT_NOREF_PV(GCPtrPage); RT_NOREF_PV(fPage); RT_NOREF_PV(uErr);
4157
4158 Assert(!pVM->pgm.s.fNestedPaging);
4159#if ( PGM_GST_TYPE == PGM_TYPE_32BIT \
4160 || PGM_GST_TYPE == PGM_TYPE_REAL \
4161 || PGM_GST_TYPE == PGM_TYPE_PROT \
4162 || PGM_GST_TYPE == PGM_TYPE_PAE \
4163 || PGM_GST_TYPE == PGM_TYPE_AMD64 ) \
4164 && !PGM_TYPE_IS_NESTED_OR_EPT(PGM_SHW_TYPE) \
4165 && PGM_SHW_TYPE != PGM_TYPE_NONE
4166
4167 /*
4168 * Get guest PD and index.
4169 */
4170 /** @todo Performance: We've done all this a jiffy ago in the
4171 * PGMGstGetPage call. */
4172# if PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE)
4173# if PGM_GST_TYPE == PGM_TYPE_32BIT
4174 const unsigned iPDSrc = (uint32_t)GCPtrPage >> GST_PD_SHIFT;
4175 PGSTPD pPDSrc = pgmGstGet32bitPDPtr(pVCpu);
4176
4177# elif PGM_GST_TYPE == PGM_TYPE_PAE
4178 unsigned iPDSrc = 0;
4179 X86PDPE PdpeSrc;
4180 PGSTPD pPDSrc = pgmGstGetPaePDPtr(pVCpu, GCPtrPage, &iPDSrc, &PdpeSrc);
4181 if (RT_UNLIKELY(!pPDSrc))
4182 {
4183 Log(("PGMVerifyAccess: access violation for %RGv due to non-present PDPTR\n", GCPtrPage));
4184 return VINF_EM_RAW_GUEST_TRAP;
4185 }
4186
4187# elif PGM_GST_TYPE == PGM_TYPE_AMD64
4188 unsigned iPDSrc = 0; /* shut up gcc */
4189 PX86PML4E pPml4eSrc = NULL; /* ditto */
4190 X86PDPE PdpeSrc;
4191 PGSTPD pPDSrc = pgmGstGetLongModePDPtr(pVCpu, GCPtrPage, &pPml4eSrc, &PdpeSrc, &iPDSrc);
4192 if (RT_UNLIKELY(!pPDSrc))
4193 {
4194 Log(("PGMVerifyAccess: access violation for %RGv due to non-present PDPTR\n", GCPtrPage));
4195 return VINF_EM_RAW_GUEST_TRAP;
4196 }
4197# endif
4198
4199# else /* !PGM_WITH_PAGING */
4200 PGSTPD pPDSrc = NULL;
4201 const unsigned iPDSrc = 0;
4202# endif /* !PGM_WITH_PAGING */
4203 int rc = VINF_SUCCESS;
4204
4205 PGM_LOCK_VOID(pVM);
4206
4207 /*
4208 * First check if the shadow pd is present.
4209 */
4210# if PGM_SHW_TYPE == PGM_TYPE_32BIT
4211 PX86PDE pPdeDst = pgmShwGet32BitPDEPtr(pVCpu, GCPtrPage);
4212
4213# elif PGM_SHW_TYPE == PGM_TYPE_PAE
4214 PX86PDEPAE pPdeDst;
4215 const unsigned iPDDst = ((GCPtrPage >> SHW_PD_SHIFT) & SHW_PD_MASK);
4216 PX86PDPAE pPDDst;
4217# if PGM_GST_TYPE != PGM_TYPE_PAE
4218 /* Fake PDPT entry; access control handled on the page table level, so allow everything. */
4219 X86PDPE PdpeSrc;
4220 PdpeSrc.u = X86_PDPE_P; /* rw/us are reserved for PAE pdpte's; accessed bit causes invalid VT-x guest state errors */
4221# endif
4222 rc = pgmShwSyncPaePDPtr(pVCpu, GCPtrPage, PdpeSrc.u, &pPDDst);
4223 if (rc != VINF_SUCCESS)
4224 {
4225 PGM_UNLOCK(pVM);
4226 AssertRC(rc);
4227 return rc;
4228 }
4229 Assert(pPDDst);
4230 pPdeDst = &pPDDst->a[iPDDst];
4231
4232# elif PGM_SHW_TYPE == PGM_TYPE_AMD64
4233 const unsigned iPDDst = ((GCPtrPage >> SHW_PD_SHIFT) & SHW_PD_MASK);
4234 PX86PDPAE pPDDst;
4235 PX86PDEPAE pPdeDst;
4236
4237# if PGM_GST_TYPE == PGM_TYPE_PROT
4238 /* AMD-V nested paging: Fake PML4 & PDPT entry; access control handled on the page table level, so allow everything. */
4239 X86PML4E Pml4eSrc;
4240 X86PDPE PdpeSrc;
4241 PX86PML4E pPml4eSrc = &Pml4eSrc;
4242 Pml4eSrc.u = X86_PML4E_P | X86_PML4E_RW | X86_PML4E_US | X86_PML4E_A;
4243 PdpeSrc.u = X86_PDPE_P | X86_PDPE_RW | X86_PDPE_US | X86_PDPE_A;
4244# endif
4245
4246 rc = pgmShwSyncLongModePDPtr(pVCpu, GCPtrPage, pPml4eSrc->u, PdpeSrc.u, &pPDDst);
4247 if (rc != VINF_SUCCESS)
4248 {
4249 PGM_UNLOCK(pVM);
4250 AssertRC(rc);
4251 return rc;
4252 }
4253 Assert(pPDDst);
4254 pPdeDst = &pPDDst->a[iPDDst];
4255# endif
4256
4257 if (!(pPdeDst->u & X86_PDE_P))
4258 {
4259 rc = PGM_BTH_NAME(SyncPT)(pVCpu, iPDSrc, pPDSrc, GCPtrPage);
4260 if (rc != VINF_SUCCESS)
4261 {
4262 PGM_DYNMAP_UNUSED_HINT(pVCpu, pPdeDst);
4263 PGM_UNLOCK(pVM);
4264 AssertRC(rc);
4265 return rc;
4266 }
4267 }
4268
4269# if PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE)
4270 /* Check for dirty bit fault */
4271 rc = PGM_BTH_NAME(CheckDirtyPageFault)(pVCpu, uErr, pPdeDst, &pPDSrc->a[iPDSrc], GCPtrPage);
4272 if (rc == VINF_PGM_HANDLED_DIRTY_BIT_FAULT)
4273 Log(("PGMVerifyAccess: success (dirty)\n"));
4274 else
4275# endif
4276 {
4277# if PGM_WITH_PAGING(PGM_GST_TYPE, PGM_SHW_TYPE)
4278 GSTPDE PdeSrc = pPDSrc->a[iPDSrc];
4279# else
4280 GSTPDE const PdeSrc = { X86_PDE_P | X86_PDE_RW | X86_PDE_US | X86_PDE_A }; /* faked so we don't have to #ifdef everything */
4281# endif
4282
4283 Assert(rc != VINF_EM_RAW_GUEST_TRAP);
4284 if (uErr & X86_TRAP_PF_US)
4285 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,PageOutOfSyncUser));
4286 else /* supervisor */
4287 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,PageOutOfSyncSupervisor));
4288
4289 rc = PGM_BTH_NAME(SyncPage)(pVCpu, PdeSrc, GCPtrPage, 1, 0);
4290 if (RT_SUCCESS(rc))
4291 {
4292 /* Page was successfully synced */
4293 Log2(("PGMVerifyAccess: success (sync)\n"));
4294 rc = VINF_SUCCESS;
4295 }
4296 else
4297 {
4298 Log(("PGMVerifyAccess: access violation for %RGv rc=%Rrc\n", GCPtrPage, rc));
4299 rc = VINF_EM_RAW_GUEST_TRAP;
4300 }
4301 }
4302 PGM_DYNMAP_UNUSED_HINT(pVCpu, pPdeDst);
4303 PGM_UNLOCK(pVM);
4304 return rc;
4305
4306#else /* PGM_TYPE_IS_NESTED_OR_EPT(PGM_SHW_TYPE) */
4307
4308 AssertLogRelMsgFailed(("Shw=%d Gst=%d is not implemented!\n", PGM_GST_TYPE, PGM_SHW_TYPE));
4309 return VERR_PGM_NOT_USED_IN_MODE;
4310#endif /* PGM_TYPE_IS_NESTED_OR_EPT(PGM_SHW_TYPE) */
4311}
4312
4313
4314/**
4315 * Syncs the paging hierarchy starting at CR3.
4316 *
4317 * @returns VBox status code, R0/RC may return VINF_PGM_SYNC_CR3, no other
4318 * informational status codes.
4319 * @retval VERR_PGM_NO_HYPERVISOR_ADDRESS in raw-mode when we're unable to map
4320 * the VMM into guest context.
4321 * @param pVCpu The cross context virtual CPU structure.
4322 * @param cr0 Guest context CR0 register.
4323 * @param cr3 Guest context CR3 register. Not subjected to the A20
4324 * mask.
4325 * @param cr4 Guest context CR4 register.
4326 * @param fGlobal Including global page directories or not
4327 */
4328PGM_BTH_DECL(int, SyncCR3)(PVMCPUCC pVCpu, uint64_t cr0, uint64_t cr3, uint64_t cr4, bool fGlobal)
4329{
4330 PVMCC pVM = pVCpu->CTX_SUFF(pVM); NOREF(pVM);
4331 NOREF(cr0); NOREF(cr3); NOREF(cr4); NOREF(fGlobal);
4332
4333 LogFlow(("SyncCR3 FF=%d fGlobal=%d\n", !!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3), fGlobal));
4334
4335#if !PGM_TYPE_IS_NESTED_OR_EPT(PGM_SHW_TYPE) && PGM_SHW_TYPE != PGM_TYPE_NONE
4336# ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
4337 PGM_LOCK_VOID(pVM);
4338 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
4339 if (pPool->cDirtyPages)
4340 pgmPoolResetDirtyPages(pVM);
4341 PGM_UNLOCK(pVM);
4342# endif
4343#endif /* !NESTED && !EPT */
4344
4345#if PGM_TYPE_IS_NESTED_OR_EPT(PGM_SHW_TYPE) || PGM_SHW_TYPE == PGM_TYPE_NONE
4346 /*
4347 * Nested / EPT / None - No work.
4348 */
4349 return VINF_SUCCESS;
4350
4351#elif PGM_SHW_TYPE == PGM_TYPE_AMD64
4352 /*
4353 * AMD64 (Shw & Gst) - No need to check all paging levels; we zero
4354 * out the shadow parts when the guest modifies its tables.
4355 */
4356 return VINF_SUCCESS;
4357
4358#else /* !PGM_TYPE_IS_NESTED_OR_EPT(PGM_SHW_TYPE) && PGM_SHW_TYPE != PGM_TYPE_AMD64 */
4359
4360 return VINF_SUCCESS;
4361#endif /* !PGM_TYPE_IS_NESTED_OR_EPT(PGM_SHW_TYPE) && PGM_SHW_TYPE != PGM_TYPE_AMD64 */
4362}
4363
4364
4365
4366
4367#ifdef VBOX_STRICT
4368
4369/**
4370 * Checks that the shadow page table is in sync with the guest one.
4371 *
4372 * @returns The number of errors.
4373 * @param pVCpu The cross context virtual CPU structure.
4374 * @param cr3 Guest context CR3 register.
4375 * @param cr4 Guest context CR4 register.
4376 * @param GCPtr Where to start. Defaults to 0.
4377 * @param cb How much to check. Defaults to everything.
4378 */
4379PGM_BTH_DECL(unsigned, AssertCR3)(PVMCPUCC pVCpu, uint64_t cr3, uint64_t cr4, RTGCPTR GCPtr, RTGCPTR cb)
4380{
4381 NOREF(pVCpu); NOREF(cr3); NOREF(cr4); NOREF(GCPtr); NOREF(cb);
4382#if PGM_TYPE_IS_NESTED_OR_EPT(PGM_SHW_TYPE) || PGM_SHW_TYPE == PGM_TYPE_NONE
4383 return 0;
4384#else
4385 unsigned cErrors = 0;
4386 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
4387 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool); NOREF(pPool);
4388
4389# if PGM_GST_TYPE == PGM_TYPE_PAE
4390 /** @todo currently broken; crashes below somewhere */
4391 AssertFailed();
4392# endif
4393
4394# if PGM_GST_TYPE == PGM_TYPE_32BIT \
4395 || PGM_GST_TYPE == PGM_TYPE_PAE \
4396 || PGM_GST_TYPE == PGM_TYPE_AMD64
4397
4398 bool fBigPagesSupported = GST_IS_PSE_ACTIVE(pVCpu);
4399 PPGMCPU pPGM = &pVCpu->pgm.s;
4400 RTGCPHYS GCPhysGst; /* page address derived from the guest page tables. */
4401 RTHCPHYS HCPhysShw; /* page address derived from the shadow page tables. */
4402# ifndef IN_RING0
4403 RTHCPHYS HCPhys; /* general usage. */
4404# endif
4405 int rc;
4406
4407 /*
4408 * Check that the Guest CR3 and all its mappings are correct.
4409 */
4410 AssertMsgReturn(pPGM->GCPhysCR3 == PGM_A20_APPLY(pVCpu, cr3 & GST_CR3_PAGE_MASK),
4411 ("Invalid GCPhysCR3=%RGp cr3=%RGp\n", pPGM->GCPhysCR3, (RTGCPHYS)cr3),
4412 false);
4413# if !defined(IN_RING0) && PGM_GST_TYPE != PGM_TYPE_AMD64
4414# if 0
4415# if PGM_GST_TYPE == PGM_TYPE_32BIT
4416 rc = PGMShwGetPage(pVCpu, (RTRCUINTPTR)pPGM->pGst32BitPdRC, NULL, &HCPhysShw);
4417# else
4418 rc = PGMShwGetPage(pVCpu, (RTRCUINTPTR)pPGM->pGstPaePdptRC, NULL, &HCPhysShw);
4419# endif
4420 AssertRCReturn(rc, 1);
4421 HCPhys = NIL_RTHCPHYS;
4422 rc = pgmRamGCPhys2HCPhys(pVM, PGM_A20_APPLY(pVCpu, cr3 & GST_CR3_PAGE_MASK), &HCPhys);
4423 AssertMsgReturn(HCPhys == HCPhysShw, ("HCPhys=%RHp HCPhyswShw=%RHp (cr3)\n", HCPhys, HCPhysShw), false);
4424# endif
4425# if PGM_GST_TYPE == PGM_TYPE_32BIT && defined(IN_RING3)
4426 pgmGstGet32bitPDPtr(pVCpu);
4427 RTGCPHYS GCPhys;
4428 rc = PGMR3DbgR3Ptr2GCPhys(pVM->pUVM, pPGM->pGst32BitPdR3, &GCPhys);
4429 AssertRCReturn(rc, 1);
4430 AssertMsgReturn(PGM_A20_APPLY(pVCpu, cr3 & GST_CR3_PAGE_MASK) == GCPhys, ("GCPhys=%RGp cr3=%RGp\n", GCPhys, (RTGCPHYS)cr3), false);
4431# endif
4432# endif /* !IN_RING0 */
4433
4434 /*
4435 * Get and check the Shadow CR3.
4436 */
4437# if PGM_SHW_TYPE == PGM_TYPE_32BIT
4438 unsigned cPDEs = X86_PG_ENTRIES;
4439 unsigned cIncrement = X86_PG_ENTRIES * GUEST_PAGE_SIZE;
4440# elif PGM_SHW_TYPE == PGM_TYPE_PAE
4441# if PGM_GST_TYPE == PGM_TYPE_32BIT
4442 unsigned cPDEs = X86_PG_PAE_ENTRIES * 4; /* treat it as a 2048 entry table. */
4443# else
4444 unsigned cPDEs = X86_PG_PAE_ENTRIES;
4445# endif
4446 unsigned cIncrement = X86_PG_PAE_ENTRIES * GUEST_PAGE_SIZE;
4447# elif PGM_SHW_TYPE == PGM_TYPE_AMD64
4448 unsigned cPDEs = X86_PG_PAE_ENTRIES;
4449 unsigned cIncrement = X86_PG_PAE_ENTRIES * GUEST_PAGE_SIZE;
4450# endif
4451 if (cb != ~(RTGCPTR)0)
4452 cPDEs = RT_MIN(cb >> SHW_PD_SHIFT, 1);
4453
4454/** @todo call the other two PGMAssert*() functions. */
4455
4456# if PGM_GST_TYPE == PGM_TYPE_AMD64
4457 unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
4458
4459 for (; iPml4 < X86_PG_PAE_ENTRIES; iPml4++)
4460 {
4461 PPGMPOOLPAGE pShwPdpt = NULL;
4462 PX86PML4E pPml4eSrc;
4463 PX86PML4E pPml4eDst;
4464 RTGCPHYS GCPhysPdptSrc;
4465
4466 pPml4eSrc = pgmGstGetLongModePML4EPtr(pVCpu, iPml4);
4467 pPml4eDst = pgmShwGetLongModePML4EPtr(pVCpu, iPml4);
4468
4469 /* Fetch the pgm pool shadow descriptor if the shadow pml4e is present. */
4470 if (!(pPml4eDst->u & X86_PML4E_P))
4471 {
4472 GCPtr += _2M * UINT64_C(512) * UINT64_C(512);
4473 continue;
4474 }
4475
4476 pShwPdpt = pgmPoolGetPage(pPool, pPml4eDst->u & X86_PML4E_PG_MASK);
4477 GCPhysPdptSrc = PGM_A20_APPLY(pVCpu, pPml4eSrc->u & X86_PML4E_PG_MASK);
4478
4479 if ((pPml4eSrc->u & X86_PML4E_P) != (pPml4eDst->u & X86_PML4E_P))
4480 {
4481 AssertMsgFailed(("Present bit doesn't match! pPml4eDst.u=%#RX64 pPml4eSrc.u=%RX64\n", pPml4eDst->u, pPml4eSrc->u));
4482 GCPtr += _2M * UINT64_C(512) * UINT64_C(512);
4483 cErrors++;
4484 continue;
4485 }
4486
4487 if (GCPhysPdptSrc != pShwPdpt->GCPhys)
4488 {
4489 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));
4490 GCPtr += _2M * UINT64_C(512) * UINT64_C(512);
4491 cErrors++;
4492 continue;
4493 }
4494
4495 if ( (pPml4eDst->u & (X86_PML4E_US | X86_PML4E_RW | X86_PML4E_NX))
4496 != (pPml4eSrc->u & (X86_PML4E_US | X86_PML4E_RW | X86_PML4E_NX)))
4497 {
4498 AssertMsgFailed(("User/Write/NoExec bits don't match! pPml4eDst.u=%#RX64 pPml4eSrc.u=%RX64\n", pPml4eDst->u, pPml4eSrc->u));
4499 GCPtr += _2M * UINT64_C(512) * UINT64_C(512);
4500 cErrors++;
4501 continue;
4502 }
4503# else /* PGM_GST_TYPE != PGM_TYPE_AMD64 */
4504 {
4505# endif /* PGM_GST_TYPE != PGM_TYPE_AMD64 */
4506
4507# if PGM_GST_TYPE == PGM_TYPE_AMD64 || PGM_GST_TYPE == PGM_TYPE_PAE
4508 /*
4509 * Check the PDPTEs too.
4510 */
4511 unsigned iPdpt = (GCPtr >> SHW_PDPT_SHIFT) & SHW_PDPT_MASK;
4512
4513 for (;iPdpt <= SHW_PDPT_MASK; iPdpt++)
4514 {
4515 unsigned iPDSrc = 0; /* initialized to shut up gcc */
4516 PPGMPOOLPAGE pShwPde = NULL;
4517 PX86PDPE pPdpeDst;
4518 RTGCPHYS GCPhysPdeSrc;
4519 X86PDPE PdpeSrc;
4520 PdpeSrc.u = 0; /* initialized to shut up gcc 4.5 */
4521# if PGM_GST_TYPE == PGM_TYPE_PAE
4522 PGSTPD pPDSrc = pgmGstGetPaePDPtr(pVCpu, GCPtr, &iPDSrc, &PdpeSrc);
4523 PX86PDPT pPdptDst = pgmShwGetPaePDPTPtr(pVCpu);
4524# else
4525 PX86PML4E pPml4eSrcIgn;
4526 PX86PDPT pPdptDst;
4527 PX86PDPAE pPDDst;
4528 PGSTPD pPDSrc = pgmGstGetLongModePDPtr(pVCpu, GCPtr, &pPml4eSrcIgn, &PdpeSrc, &iPDSrc);
4529
4530 rc = pgmShwGetLongModePDPtr(pVCpu, GCPtr, NULL, &pPdptDst, &pPDDst);
4531 if (rc != VINF_SUCCESS)
4532 {
4533 AssertMsg(rc == VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT, ("Unexpected rc=%Rrc\n", rc));
4534 GCPtr += 512 * _2M;
4535 continue; /* next PDPTE */
4536 }
4537 Assert(pPDDst);
4538# endif
4539 Assert(iPDSrc == 0);
4540
4541 pPdpeDst = &pPdptDst->a[iPdpt];
4542
4543 if (!(pPdpeDst->u & X86_PDPE_P))
4544 {
4545 GCPtr += 512 * _2M;
4546 continue; /* next PDPTE */
4547 }
4548
4549 pShwPde = pgmPoolGetPage(pPool, pPdpeDst->u & X86_PDPE_PG_MASK);
4550 GCPhysPdeSrc = PGM_A20_APPLY(pVCpu, PdpeSrc.u & X86_PDPE_PG_MASK);
4551
4552 if ((pPdpeDst->u & X86_PDPE_P) != (PdpeSrc.u & X86_PDPE_P))
4553 {
4554 AssertMsgFailed(("Present bit doesn't match! pPdpeDst.u=%#RX64 pPdpeSrc.u=%RX64\n", pPdpeDst->u, PdpeSrc.u));
4555 GCPtr += 512 * _2M;
4556 cErrors++;
4557 continue;
4558 }
4559
4560 if (GCPhysPdeSrc != pShwPde->GCPhys)
4561 {
4562# if PGM_GST_TYPE == PGM_TYPE_AMD64
4563 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));
4564# else
4565 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));
4566# endif
4567 GCPtr += 512 * _2M;
4568 cErrors++;
4569 continue;
4570 }
4571
4572# if PGM_GST_TYPE == PGM_TYPE_AMD64
4573 if ( (pPdpeDst->u & (X86_PDPE_US | X86_PDPE_RW | X86_PDPE_LM_NX))
4574 != (PdpeSrc.u & (X86_PDPE_US | X86_PDPE_RW | X86_PDPE_LM_NX)))
4575 {
4576 AssertMsgFailed(("User/Write/NoExec bits don't match! pPdpeDst.u=%#RX64 pPdpeSrc.u=%RX64\n", pPdpeDst->u, PdpeSrc.u));
4577 GCPtr += 512 * _2M;
4578 cErrors++;
4579 continue;
4580 }
4581# endif
4582
4583# else /* PGM_GST_TYPE != PGM_TYPE_AMD64 && PGM_GST_TYPE != PGM_TYPE_PAE */
4584 {
4585# endif /* PGM_GST_TYPE != PGM_TYPE_AMD64 && PGM_GST_TYPE != PGM_TYPE_PAE */
4586# if PGM_GST_TYPE == PGM_TYPE_32BIT
4587 GSTPD const *pPDSrc = pgmGstGet32bitPDPtr(pVCpu);
4588# if PGM_SHW_TYPE == PGM_TYPE_32BIT
4589 PCX86PD pPDDst = pgmShwGet32BitPDPtr(pVCpu);
4590# endif
4591# endif /* PGM_GST_TYPE == PGM_TYPE_32BIT */
4592 /*
4593 * Iterate the shadow page directory.
4594 */
4595 GCPtr = (GCPtr >> SHW_PD_SHIFT) << SHW_PD_SHIFT;
4596 unsigned iPDDst = (GCPtr >> SHW_PD_SHIFT) & SHW_PD_MASK;
4597
4598 for (;
4599 iPDDst < cPDEs;
4600 iPDDst++, GCPtr += cIncrement)
4601 {
4602# if PGM_SHW_TYPE == PGM_TYPE_PAE
4603 const SHWPDE PdeDst = *pgmShwGetPaePDEPtr(pVCpu, GCPtr);
4604# else
4605 const SHWPDE PdeDst = pPDDst->a[iPDDst];
4606# endif
4607 if ( (PdeDst.u & X86_PDE_P)
4608 || ((PdeDst.u & (X86_PDE_P | PGM_PDFLAGS_TRACK_DIRTY)) == (X86_PDE_P | PGM_PDFLAGS_TRACK_DIRTY)) )
4609 {
4610 HCPhysShw = PdeDst.u & SHW_PDE_PG_MASK;
4611 PPGMPOOLPAGE pPoolPage = pgmPoolGetPage(pPool, HCPhysShw);
4612 if (!pPoolPage)
4613 {
4614 AssertMsgFailed(("Invalid page table address %RHp at %RGv! PdeDst=%#RX64\n",
4615 HCPhysShw, GCPtr, (uint64_t)PdeDst.u));
4616 cErrors++;
4617 continue;
4618 }
4619 const SHWPT *pPTDst = (const SHWPT *)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pPoolPage);
4620
4621 if (PdeDst.u & (X86_PDE4M_PWT | X86_PDE4M_PCD))
4622 {
4623 AssertMsgFailed(("PDE flags PWT and/or PCD is set at %RGv! These flags are not virtualized! PdeDst=%#RX64\n",
4624 GCPtr, (uint64_t)PdeDst.u));
4625 cErrors++;
4626 }
4627
4628 if (PdeDst.u & (X86_PDE4M_G | X86_PDE4M_D))
4629 {
4630 AssertMsgFailed(("4K PDE reserved flags at %RGv! PdeDst=%#RX64\n",
4631 GCPtr, (uint64_t)PdeDst.u));
4632 cErrors++;
4633 }
4634
4635 const GSTPDE PdeSrc = pPDSrc->a[(iPDDst >> (GST_PD_SHIFT - SHW_PD_SHIFT)) & GST_PD_MASK];
4636 if (!(PdeSrc.u & X86_PDE_P))
4637 {
4638 AssertMsgFailed(("Guest PDE at %RGv is not present! PdeDst=%#RX64 PdeSrc=%#RX64\n",
4639 GCPtr, (uint64_t)PdeDst.u, (uint64_t)PdeSrc.u));
4640 cErrors++;
4641 continue;
4642 }
4643
4644 if ( !(PdeSrc.u & X86_PDE_PS)
4645 || !fBigPagesSupported)
4646 {
4647 GCPhysGst = GST_GET_PDE_GCPHYS(PdeSrc);
4648# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
4649 GCPhysGst = PGM_A20_APPLY(pVCpu, GCPhysGst | ((iPDDst & 1) * (GUEST_PAGE_SIZE / 2)));
4650# endif
4651 }
4652 else
4653 {
4654# if PGM_GST_TYPE == PGM_TYPE_32BIT
4655 if (PdeSrc.u & X86_PDE4M_PG_HIGH_MASK)
4656 {
4657 AssertMsgFailed(("Guest PDE at %RGv is using PSE36 or similar! PdeSrc=%#RX64\n",
4658 GCPtr, (uint64_t)PdeSrc.u));
4659 cErrors++;
4660 continue;
4661 }
4662# endif
4663 GCPhysGst = GST_GET_BIG_PDE_GCPHYS(pVM, PdeSrc);
4664# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
4665 GCPhysGst = PGM_A20_APPLY(pVCpu, GCPhysGst | (GCPtr & RT_BIT(X86_PAGE_2M_SHIFT)));
4666# endif
4667 }
4668
4669 if ( pPoolPage->enmKind
4670 != (!(PdeSrc.u & X86_PDE_PS) || !fBigPagesSupported ? BTH_PGMPOOLKIND_PT_FOR_PT : BTH_PGMPOOLKIND_PT_FOR_BIG))
4671 {
4672 AssertMsgFailed(("Invalid shadow page table kind %d at %RGv! PdeSrc=%#RX64\n",
4673 pPoolPage->enmKind, GCPtr, (uint64_t)PdeSrc.u));
4674 cErrors++;
4675 }
4676
4677 PPGMPAGE pPhysPage = pgmPhysGetPage(pVM, GCPhysGst);
4678 if (!pPhysPage)
4679 {
4680 AssertMsgFailed(("Cannot find guest physical address %RGp in the PDE at %RGv! PdeSrc=%#RX64\n",
4681 GCPhysGst, GCPtr, (uint64_t)PdeSrc.u));
4682 cErrors++;
4683 continue;
4684 }
4685
4686 if (GCPhysGst != pPoolPage->GCPhys)
4687 {
4688 AssertMsgFailed(("GCPhysGst=%RGp != pPage->GCPhys=%RGp at %RGv\n",
4689 GCPhysGst, pPoolPage->GCPhys, GCPtr));
4690 cErrors++;
4691 continue;
4692 }
4693
4694 if ( !(PdeSrc.u & X86_PDE_PS)
4695 || !fBigPagesSupported)
4696 {
4697 /*
4698 * Page Table.
4699 */
4700 const GSTPT *pPTSrc;
4701 rc = PGM_GCPHYS_2_PTR_V2(pVM, pVCpu, PGM_A20_APPLY(pVCpu, GCPhysGst & ~(RTGCPHYS)(GUEST_PAGE_SIZE - 1)),
4702 &pPTSrc);
4703 if (RT_FAILURE(rc))
4704 {
4705 AssertMsgFailed(("Cannot map/convert guest physical address %RGp in the PDE at %RGv! PdeSrc=%#RX64\n",
4706 GCPhysGst, GCPtr, (uint64_t)PdeSrc.u));
4707 cErrors++;
4708 continue;
4709 }
4710 if ( (PdeSrc.u & (X86_PDE_P | X86_PDE_US | X86_PDE_RW/* | X86_PDE_A*/))
4711 != (PdeDst.u & (X86_PDE_P | X86_PDE_US | X86_PDE_RW/* | X86_PDE_A*/)))
4712 {
4713 /// @todo We get here a lot on out-of-sync CR3 entries. The access handler should zap them to avoid false alarms here!
4714 // (This problem will go away when/if we shadow multiple CR3s.)
4715 AssertMsgFailed(("4K PDE flags mismatch at %RGv! PdeSrc=%#RX64 PdeDst=%#RX64\n",
4716 GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
4717 cErrors++;
4718 continue;
4719 }
4720 if (PdeDst.u & PGM_PDFLAGS_TRACK_DIRTY)
4721 {
4722 AssertMsgFailed(("4K PDEs cannot have PGM_PDFLAGS_TRACK_DIRTY set! GCPtr=%RGv PdeDst=%#RX64\n",
4723 GCPtr, (uint64_t)PdeDst.u));
4724 cErrors++;
4725 continue;
4726 }
4727
4728 /* iterate the page table. */
4729# if PGM_SHW_TYPE == PGM_TYPE_PAE && PGM_GST_TYPE == PGM_TYPE_32BIT
4730 /* Select the right PDE as we're emulating a 4kb page table with 2 shadow page tables. */
4731 const unsigned offPTSrc = ((GCPtr >> SHW_PD_SHIFT) & 1) * 512;
4732# else
4733 const unsigned offPTSrc = 0;
4734# endif
4735 for (unsigned iPT = 0, off = 0;
4736 iPT < RT_ELEMENTS(pPTDst->a);
4737 iPT++, off += GUEST_PAGE_SIZE)
4738 {
4739 const SHWPTE PteDst = pPTDst->a[iPT];
4740
4741 /* skip not-present and dirty tracked entries. */
4742 if (!(SHW_PTE_GET_U(PteDst) & (X86_PTE_P | PGM_PTFLAGS_TRACK_DIRTY))) /** @todo deal with ALL handlers and CSAM !P pages! */
4743 continue;
4744 Assert(SHW_PTE_IS_P(PteDst));
4745
4746 const GSTPTE PteSrc = pPTSrc->a[iPT + offPTSrc];
4747 if (!(PteSrc.u & X86_PTE_P))
4748 {
4749# ifdef IN_RING3
4750 PGMAssertHandlerAndFlagsInSync(pVM);
4751 DBGFR3PagingDumpEx(pVM->pUVM, pVCpu->idCpu, DBGFPGDMP_FLAGS_CURRENT_CR3 | DBGFPGDMP_FLAGS_CURRENT_MODE
4752 | DBGFPGDMP_FLAGS_GUEST | DBGFPGDMP_FLAGS_HEADER | DBGFPGDMP_FLAGS_PRINT_CR3,
4753 0, 0, UINT64_MAX, 99, NULL);
4754# endif
4755 AssertMsgFailed(("Out of sync (!P) PTE at %RGv! PteSrc=%#RX64 PteDst=%#RX64 pPTSrc=%RGv iPTSrc=%x PdeSrc=%x physpte=%RGp\n",
4756 GCPtr + off, (uint64_t)PteSrc.u, SHW_PTE_LOG64(PteDst), pPTSrc, iPT + offPTSrc, PdeSrc.au32[0],
4757 (uint64_t)GST_GET_PDE_GCPHYS(PdeSrc) + (iPT + offPTSrc) * sizeof(PteSrc)));
4758 cErrors++;
4759 continue;
4760 }
4761
4762 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;
4763# if 1 /** @todo sync accessed bit properly... */
4764 fIgnoreFlags |= X86_PTE_A;
4765# endif
4766
4767 /* match the physical addresses */
4768 HCPhysShw = SHW_PTE_GET_HCPHYS(PteDst);
4769 GCPhysGst = GST_GET_PTE_GCPHYS(PteSrc);
4770
4771# ifdef IN_RING3
4772 rc = PGMPhysGCPhys2HCPhys(pVM, GCPhysGst, &HCPhys);
4773 if (RT_FAILURE(rc))
4774 {
4775# if 0
4776 if (HCPhysShw != MMR3PageDummyHCPhys(pVM)) /** @todo this is wrong. */
4777 {
4778 AssertMsgFailed(("Cannot find guest physical address %RGp at %RGv! PteSrc=%#RX64 PteDst=%#RX64\n",
4779 GCPhysGst, GCPtr + off, (uint64_t)PteSrc.u, SHW_PTE_LOG64(PteDst)));
4780 cErrors++;
4781 continue;
4782 }
4783# endif
4784 }
4785 else if (HCPhysShw != (HCPhys & SHW_PTE_PG_MASK))
4786 {
4787 AssertMsgFailed(("Out of sync (phys) at %RGv! HCPhysShw=%RHp HCPhys=%RHp GCPhysGst=%RGp PteSrc=%#RX64 PteDst=%#RX64\n",
4788 GCPtr + off, HCPhysShw, HCPhys, GCPhysGst, (uint64_t)PteSrc.u, SHW_PTE_LOG64(PteDst)));
4789 cErrors++;
4790 continue;
4791 }
4792# endif
4793
4794 pPhysPage = pgmPhysGetPage(pVM, GCPhysGst);
4795 if (!pPhysPage)
4796 {
4797# if 0
4798 if (HCPhysShw != MMR3PageDummyHCPhys(pVM)) /** @todo this is wrong. */
4799 {
4800 AssertMsgFailed(("Cannot find guest physical address %RGp at %RGv! PteSrc=%#RX64 PteDst=%#RX64\n",
4801 GCPhysGst, GCPtr + off, (uint64_t)PteSrc.u, SHW_PTE_LOG64(PteDst)));
4802 cErrors++;
4803 continue;
4804 }
4805# endif
4806 if (SHW_PTE_IS_RW(PteDst))
4807 {
4808 AssertMsgFailed(("Invalid guest page at %RGv is writable! GCPhysGst=%RGp PteSrc=%#RX64 PteDst=%#RX64\n",
4809 GCPtr + off, GCPhysGst, (uint64_t)PteSrc.u, SHW_PTE_LOG64(PteDst)));
4810 cErrors++;
4811 }
4812 fIgnoreFlags |= X86_PTE_RW;
4813 }
4814 else if (HCPhysShw != PGM_PAGE_GET_HCPHYS(pPhysPage))
4815 {
4816 AssertMsgFailed(("Out of sync (phys) at %RGv! HCPhysShw=%RHp pPhysPage:%R[pgmpage] GCPhysGst=%RGp PteSrc=%#RX64 PteDst=%#RX64\n",
4817 GCPtr + off, HCPhysShw, pPhysPage, GCPhysGst, (uint64_t)PteSrc.u, SHW_PTE_LOG64(PteDst)));
4818 cErrors++;
4819 continue;
4820 }
4821
4822 /* flags */
4823 if (PGM_PAGE_HAS_ACTIVE_HANDLERS(pPhysPage) && !PGM_PAGE_IS_HNDL_PHYS_NOT_IN_HM(pPhysPage))
4824 {
4825 if (!PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPhysPage))
4826 {
4827 if (SHW_PTE_IS_RW(PteDst))
4828 {
4829 AssertMsgFailed(("WRITE access flagged at %RGv but the page is writable! pPhysPage=%R[pgmpage] PteSrc=%#RX64 PteDst=%#RX64\n",
4830 GCPtr + off, pPhysPage, (uint64_t)PteSrc.u, SHW_PTE_LOG64(PteDst)));
4831 cErrors++;
4832 continue;
4833 }
4834 fIgnoreFlags |= X86_PTE_RW;
4835 }
4836 else
4837 {
4838 if ( SHW_PTE_IS_P(PteDst)
4839# if PGM_SHW_TYPE == PGM_TYPE_EPT || PGM_SHW_TYPE == PGM_TYPE_PAE || PGM_SHW_TYPE == PGM_TYPE_AMD64
4840 && !PGM_PAGE_IS_MMIO(pPhysPage)
4841# endif
4842 )
4843 {
4844 AssertMsgFailed(("ALL access flagged at %RGv but the page is present! pPhysPage=%R[pgmpage] PteSrc=%#RX64 PteDst=%#RX64\n",
4845 GCPtr + off, pPhysPage, (uint64_t)PteSrc.u, SHW_PTE_LOG64(PteDst)));
4846 cErrors++;
4847 continue;
4848 }
4849 fIgnoreFlags |= X86_PTE_P;
4850 }
4851 }
4852 else
4853 {
4854 if ((PteSrc.u & (X86_PTE_RW | X86_PTE_D)) == X86_PTE_RW)
4855 {
4856 if (SHW_PTE_IS_RW(PteDst))
4857 {
4858 AssertMsgFailed(("!DIRTY page at %RGv is writable! PteSrc=%#RX64 PteDst=%#RX64\n",
4859 GCPtr + off, (uint64_t)PteSrc.u, SHW_PTE_LOG64(PteDst)));
4860 cErrors++;
4861 continue;
4862 }
4863 if (!SHW_PTE_IS_TRACK_DIRTY(PteDst))
4864 {
4865 AssertMsgFailed(("!DIRTY page at %RGv is not marked TRACK_DIRTY! PteSrc=%#RX64 PteDst=%#RX64\n",
4866 GCPtr + off, (uint64_t)PteSrc.u, SHW_PTE_LOG64(PteDst)));
4867 cErrors++;
4868 continue;
4869 }
4870 if (SHW_PTE_IS_D(PteDst))
4871 {
4872 AssertMsgFailed(("!DIRTY page at %RGv is marked DIRTY! PteSrc=%#RX64 PteDst=%#RX64\n",
4873 GCPtr + off, (uint64_t)PteSrc.u, SHW_PTE_LOG64(PteDst)));
4874 cErrors++;
4875 }
4876# if 0 /** @todo sync access bit properly... */
4877 if (PteDst.n.u1Accessed != PteSrc.n.u1Accessed)
4878 {
4879 AssertMsgFailed(("!DIRTY page at %RGv is has mismatching accessed bit! PteSrc=%#RX64 PteDst=%#RX64\n",
4880 GCPtr + off, (uint64_t)PteSrc.u, SHW_PTE_LOG64(PteDst)));
4881 cErrors++;
4882 }
4883 fIgnoreFlags |= X86_PTE_RW;
4884# else
4885 fIgnoreFlags |= X86_PTE_RW | X86_PTE_A;
4886# endif
4887 }
4888 else if (SHW_PTE_IS_TRACK_DIRTY(PteDst))
4889 {
4890 /* access bit emulation (not implemented). */
4891 if ((PteSrc.u & X86_PTE_A) || SHW_PTE_IS_P(PteDst))
4892 {
4893 AssertMsgFailed(("PGM_PTFLAGS_TRACK_DIRTY set at %RGv but no accessed bit emulation! PteSrc=%#RX64 PteDst=%#RX64\n",
4894 GCPtr + off, (uint64_t)PteSrc.u, SHW_PTE_LOG64(PteDst)));
4895 cErrors++;
4896 continue;
4897 }
4898 if (!SHW_PTE_IS_A(PteDst))
4899 {
4900 AssertMsgFailed(("!ACCESSED page at %RGv is has the accessed bit set! PteSrc=%#RX64 PteDst=%#RX64\n",
4901 GCPtr + off, (uint64_t)PteSrc.u, SHW_PTE_LOG64(PteDst)));
4902 cErrors++;
4903 }
4904 fIgnoreFlags |= X86_PTE_P;
4905 }
4906# ifdef DEBUG_sandervl
4907 fIgnoreFlags |= X86_PTE_D | X86_PTE_A;
4908# endif
4909 }
4910
4911 if ( (PteSrc.u & ~fIgnoreFlags) != (SHW_PTE_GET_U(PteDst) & ~fIgnoreFlags)
4912 && (PteSrc.u & ~(fIgnoreFlags | X86_PTE_RW)) != (SHW_PTE_GET_U(PteDst) & ~fIgnoreFlags)
4913 )
4914 {
4915 AssertMsgFailed(("Flags mismatch at %RGv! %#RX64 != %#RX64 fIgnoreFlags=%#RX64 PteSrc=%#RX64 PteDst=%#RX64\n",
4916 GCPtr + off, (uint64_t)PteSrc.u & ~fIgnoreFlags, SHW_PTE_LOG64(PteDst) & ~fIgnoreFlags,
4917 fIgnoreFlags, (uint64_t)PteSrc.u, SHW_PTE_LOG64(PteDst)));
4918 cErrors++;
4919 continue;
4920 }
4921 } /* foreach PTE */
4922 }
4923 else
4924 {
4925 /*
4926 * Big Page.
4927 */
4928 uint64_t fIgnoreFlags = X86_PDE_AVL_MASK | GST_PDE_PG_MASK | X86_PDE4M_G | X86_PDE4M_D | X86_PDE4M_PS | X86_PDE4M_PWT | X86_PDE4M_PCD;
4929 if ((PdeSrc.u & (X86_PDE_RW | X86_PDE4M_D)) == X86_PDE_RW)
4930 {
4931 if (PdeDst.u & X86_PDE_RW)
4932 {
4933 AssertMsgFailed(("!DIRTY page at %RGv is writable! PdeSrc=%#RX64 PdeDst=%#RX64\n",
4934 GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
4935 cErrors++;
4936 continue;
4937 }
4938 if (!(PdeDst.u & PGM_PDFLAGS_TRACK_DIRTY))
4939 {
4940 AssertMsgFailed(("!DIRTY page at %RGv is not marked TRACK_DIRTY! PteSrc=%#RX64 PteDst=%#RX64\n",
4941 GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
4942 cErrors++;
4943 continue;
4944 }
4945# if 0 /** @todo sync access bit properly... */
4946 if (PdeDst.n.u1Accessed != PdeSrc.b.u1Accessed)
4947 {
4948 AssertMsgFailed(("!DIRTY page at %RGv is has mismatching accessed bit! PteSrc=%#RX64 PteDst=%#RX64\n",
4949 GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
4950 cErrors++;
4951 }
4952 fIgnoreFlags |= X86_PTE_RW;
4953# else
4954 fIgnoreFlags |= X86_PTE_RW | X86_PTE_A;
4955# endif
4956 }
4957 else if (PdeDst.u & PGM_PDFLAGS_TRACK_DIRTY)
4958 {
4959 /* access bit emulation (not implemented). */
4960 if ((PdeSrc.u & X86_PDE_A) || SHW_PDE_IS_P(PdeDst))
4961 {
4962 AssertMsgFailed(("PGM_PDFLAGS_TRACK_DIRTY set at %RGv but no accessed bit emulation! PdeSrc=%#RX64 PdeDst=%#RX64\n",
4963 GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
4964 cErrors++;
4965 continue;
4966 }
4967 if (!SHW_PDE_IS_A(PdeDst))
4968 {
4969 AssertMsgFailed(("!ACCESSED page at %RGv is has the accessed bit set! PdeSrc=%#RX64 PdeDst=%#RX64\n",
4970 GCPtr, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
4971 cErrors++;
4972 }
4973 fIgnoreFlags |= X86_PTE_P;
4974 }
4975
4976 if ((PdeSrc.u & ~fIgnoreFlags) != (PdeDst.u & ~fIgnoreFlags))
4977 {
4978 AssertMsgFailed(("Flags mismatch (B) at %RGv! %#RX64 != %#RX64 fIgnoreFlags=%#RX64 PdeSrc=%#RX64 PdeDst=%#RX64\n",
4979 GCPtr, (uint64_t)PdeSrc.u & ~fIgnoreFlags, (uint64_t)PdeDst.u & ~fIgnoreFlags,
4980 fIgnoreFlags, (uint64_t)PdeSrc.u, (uint64_t)PdeDst.u));
4981 cErrors++;
4982 }
4983
4984 /* iterate the page table. */
4985 for (unsigned iPT = 0, off = 0;
4986 iPT < RT_ELEMENTS(pPTDst->a);
4987 iPT++, off += GUEST_PAGE_SIZE, GCPhysGst = PGM_A20_APPLY(pVCpu, GCPhysGst + GUEST_PAGE_SIZE))
4988 {
4989 const SHWPTE PteDst = pPTDst->a[iPT];
4990
4991 if (SHW_PTE_IS_TRACK_DIRTY(PteDst))
4992 {
4993 AssertMsgFailed(("The PTE at %RGv emulating a 2/4M page is marked TRACK_DIRTY! PdeSrc=%#RX64 PteDst=%#RX64\n",
4994 GCPtr + off, (uint64_t)PdeSrc.u, SHW_PTE_LOG64(PteDst)));
4995 cErrors++;
4996 }
4997
4998 /* skip not-present entries. */
4999 if (!SHW_PTE_IS_P(PteDst)) /** @todo deal with ALL handlers and CSAM !P pages! */
5000 continue;
5001
5002 fIgnoreFlags = X86_PTE_PAE_PG_MASK | X86_PTE_AVL_MASK | X86_PTE_PWT | X86_PTE_PCD | X86_PTE_PAT | X86_PTE_D | X86_PTE_A | X86_PTE_G | X86_PTE_PAE_NX;
5003
5004 /* match the physical addresses */
5005 HCPhysShw = SHW_PTE_GET_HCPHYS(PteDst);
5006
5007# ifdef IN_RING3
5008 rc = PGMPhysGCPhys2HCPhys(pVM, GCPhysGst, &HCPhys);
5009 if (RT_FAILURE(rc))
5010 {
5011# if 0
5012 if (HCPhysShw != MMR3PageDummyHCPhys(pVM)) /** @todo this is wrong. */
5013 {
5014 AssertMsgFailed(("Cannot find guest physical address %RGp at %RGv! PdeSrc=%#RX64 PteDst=%#RX64\n",
5015 GCPhysGst, GCPtr + off, (uint64_t)PdeSrc.u, SHW_PTE_LOG64(PteDst)));
5016 cErrors++;
5017 }
5018# endif
5019 }
5020 else if (HCPhysShw != (HCPhys & X86_PTE_PAE_PG_MASK))
5021 {
5022 AssertMsgFailed(("Out of sync (phys) at %RGv! HCPhysShw=%RHp HCPhys=%RHp GCPhysGst=%RGp PdeSrc=%#RX64 PteDst=%#RX64\n",
5023 GCPtr + off, HCPhysShw, HCPhys, GCPhysGst, (uint64_t)PdeSrc.u, SHW_PTE_LOG64(PteDst)));
5024 cErrors++;
5025 continue;
5026 }
5027# endif
5028 pPhysPage = pgmPhysGetPage(pVM, GCPhysGst);
5029 if (!pPhysPage)
5030 {
5031# if 0 /** @todo make MMR3PageDummyHCPhys an 'All' function! */
5032 if (HCPhysShw != MMR3PageDummyHCPhys(pVM)) /** @todo this is wrong. */
5033 {
5034 AssertMsgFailed(("Cannot find guest physical address %RGp at %RGv! PdeSrc=%#RX64 PteDst=%#RX64\n",
5035 GCPhysGst, GCPtr + off, (uint64_t)PdeSrc.u, SHW_PTE_LOG64(PteDst)));
5036 cErrors++;
5037 continue;
5038 }
5039# endif
5040 if (SHW_PTE_IS_RW(PteDst))
5041 {
5042 AssertMsgFailed(("Invalid guest page at %RGv is writable! GCPhysGst=%RGp PdeSrc=%#RX64 PteDst=%#RX64\n",
5043 GCPtr + off, GCPhysGst, (uint64_t)PdeSrc.u, SHW_PTE_LOG64(PteDst)));
5044 cErrors++;
5045 }
5046 fIgnoreFlags |= X86_PTE_RW;
5047 }
5048 else if (HCPhysShw != PGM_PAGE_GET_HCPHYS(pPhysPage))
5049 {
5050 AssertMsgFailed(("Out of sync (phys) at %RGv! HCPhysShw=%RHp pPhysPage=%R[pgmpage] GCPhysGst=%RGp PdeSrc=%#RX64 PteDst=%#RX64\n",
5051 GCPtr + off, HCPhysShw, pPhysPage, GCPhysGst, (uint64_t)PdeSrc.u, SHW_PTE_LOG64(PteDst)));
5052 cErrors++;
5053 continue;
5054 }
5055
5056 /* flags */
5057 if (PGM_PAGE_HAS_ACTIVE_HANDLERS(pPhysPage))
5058 {
5059 if (!PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPhysPage))
5060 {
5061 if (PGM_PAGE_GET_HNDL_PHYS_STATE(pPhysPage) != PGM_PAGE_HNDL_PHYS_STATE_DISABLED)
5062 {
5063 if ( SHW_PTE_IS_RW(PteDst)
5064 && !PGM_PAGE_IS_HNDL_PHYS_NOT_IN_HM(pPhysPage))
5065 {
5066 AssertMsgFailed(("WRITE access flagged at %RGv but the page is writable! pPhysPage=%R[pgmpage] PdeSrc=%#RX64 PteDst=%#RX64\n",
5067 GCPtr + off, pPhysPage, (uint64_t)PdeSrc.u, SHW_PTE_LOG64(PteDst)));
5068 cErrors++;
5069 continue;
5070 }
5071 fIgnoreFlags |= X86_PTE_RW;
5072 }
5073 }
5074 else
5075 {
5076 if ( SHW_PTE_IS_P(PteDst)
5077 && !PGM_PAGE_IS_HNDL_PHYS_NOT_IN_HM(pPhysPage)
5078# if PGM_SHW_TYPE == PGM_TYPE_EPT || PGM_SHW_TYPE == PGM_TYPE_PAE || PGM_SHW_TYPE == PGM_TYPE_AMD64
5079 && !PGM_PAGE_IS_MMIO(pPhysPage)
5080# endif
5081 )
5082 {
5083 AssertMsgFailed(("ALL access flagged at %RGv but the page is present! pPhysPage=%R[pgmpage] PdeSrc=%#RX64 PteDst=%#RX64\n",
5084 GCPtr + off, pPhysPage, (uint64_t)PdeSrc.u, SHW_PTE_LOG64(PteDst)));
5085 cErrors++;
5086 continue;
5087 }
5088 fIgnoreFlags |= X86_PTE_P;
5089 }
5090 }
5091
5092 if ( (PdeSrc.u & ~fIgnoreFlags) != (SHW_PTE_GET_U(PteDst) & ~fIgnoreFlags)
5093 && (PdeSrc.u & ~(fIgnoreFlags | X86_PTE_RW)) != (SHW_PTE_GET_U(PteDst) & ~fIgnoreFlags) /* lazy phys handler dereg. */
5094 )
5095 {
5096 AssertMsgFailed(("Flags mismatch (BT) at %RGv! %#RX64 != %#RX64 fIgnoreFlags=%#RX64 PdeSrc=%#RX64 PteDst=%#RX64\n",
5097 GCPtr + off, (uint64_t)PdeSrc.u & ~fIgnoreFlags, SHW_PTE_LOG64(PteDst) & ~fIgnoreFlags,
5098 fIgnoreFlags, (uint64_t)PdeSrc.u, SHW_PTE_LOG64(PteDst)));
5099 cErrors++;
5100 continue;
5101 }
5102 } /* for each PTE */
5103 }
5104 }
5105 /* not present */
5106
5107 } /* for each PDE */
5108
5109 } /* for each PDPTE */
5110
5111 } /* for each PML4E */
5112
5113# ifdef DEBUG
5114 if (cErrors)
5115 LogFlow(("AssertCR3: cErrors=%d\n", cErrors));
5116# endif
5117# endif /* GST is in {32BIT, PAE, AMD64} */
5118 return cErrors;
5119#endif /* !PGM_TYPE_IS_NESTED_OR_EPT(PGM_SHW_TYPE) && PGM_SHW_TYPE != PGM_TYPE_NONE */
5120}
5121#endif /* VBOX_STRICT */
5122
5123
5124/**
5125 * Sets up the CR3 for shadow paging
5126 *
5127 * @returns Strict VBox status code.
5128 * @retval VINF_SUCCESS.
5129 *
5130 * @param pVCpu The cross context virtual CPU structure.
5131 * @param GCPhysCR3 The physical address in the CR3 register. (A20 mask
5132 * already applied.)
5133 */
5134PGM_BTH_DECL(int, MapCR3)(PVMCPUCC pVCpu, RTGCPHYS GCPhysCR3)
5135{
5136 PVMCC pVM = pVCpu->CTX_SUFF(pVM); NOREF(pVM);
5137 int rc = VINF_SUCCESS;
5138
5139 /* Update guest paging info. */
5140#if PGM_GST_TYPE == PGM_TYPE_32BIT \
5141 || PGM_GST_TYPE == PGM_TYPE_PAE \
5142 || PGM_GST_TYPE == PGM_TYPE_AMD64
5143
5144 LogFlow(("MapCR3: %RGp\n", GCPhysCR3));
5145 PGM_A20_ASSERT_MASKED(pVCpu, GCPhysCR3);
5146
5147# if PGM_GST_TYPE == PGM_TYPE_PAE
5148 if ( !pVCpu->pgm.s.CTX_SUFF(fPaePdpesAndCr3Mapped)
5149 || pVCpu->pgm.s.GCPhysPaeCR3 != GCPhysCR3)
5150# endif
5151 {
5152 /*
5153 * Map the page CR3 points at.
5154 */
5155 RTHCPTR HCPtrGuestCR3;
5156 rc = pgmGstMapCr3(pVCpu, GCPhysCR3, &HCPtrGuestCR3);
5157 if (RT_SUCCESS(rc))
5158 {
5159# if PGM_GST_TYPE == PGM_TYPE_32BIT
5160# ifdef IN_RING3
5161 pVCpu->pgm.s.pGst32BitPdR3 = (PX86PD)HCPtrGuestCR3;
5162 pVCpu->pgm.s.pGst32BitPdR0 = NIL_RTR0PTR;
5163# else
5164 pVCpu->pgm.s.pGst32BitPdR3 = NIL_RTR3PTR;
5165 pVCpu->pgm.s.pGst32BitPdR0 = (PX86PD)HCPtrGuestCR3;
5166# endif
5167
5168# elif PGM_GST_TYPE == PGM_TYPE_PAE
5169# ifdef IN_RING3
5170 pVCpu->pgm.s.pGstPaePdptR3 = (PX86PDPT)HCPtrGuestCR3;
5171 pVCpu->pgm.s.pGstPaePdptR0 = NIL_RTR0PTR;
5172# else
5173 pVCpu->pgm.s.pGstPaePdptR3 = NIL_RTR3PTR;
5174 pVCpu->pgm.s.pGstPaePdptR0 = (PX86PDPT)HCPtrGuestCR3;
5175# endif
5176
5177 X86PDPE aGstPaePdpes[X86_PG_PAE_PDPE_ENTRIES];
5178#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
5179 /*
5180 * When EPT is enabled by the nested-hypervisor and the nested-guest is in PAE mode,
5181 * the guest-CPU context would've already been updated with the 4 PAE PDPEs specified
5182 * in the virtual VMCS. The PDPEs can differ from those in guest memory referenced by
5183 * the translated nested-guest CR3. We -MUST- use the PDPEs provided in the virtual VMCS
5184 * rather than those in guest memory.
5185 *
5186 * See Intel spec. 26.3.2.4 "Loading Page-Directory-Pointer-Table Entries".
5187 */
5188 if (pVCpu->pgm.s.enmGuestSlatMode == PGMSLAT_EPT)
5189 CPUMGetGuestPaePdpes(pVCpu, &aGstPaePdpes[0]);
5190 else
5191#endif
5192 {
5193 /* Update CPUM with the PAE PDPEs referenced by CR3. */
5194 memcpy(&aGstPaePdpes, HCPtrGuestCR3, sizeof(aGstPaePdpes));
5195 CPUMSetGuestPaePdpes(pVCpu, &aGstPaePdpes[0]);
5196 }
5197
5198 /*
5199 * Map the 4 PAE PDPEs.
5200 */
5201 rc = PGMGstMapPaePdpes(pVCpu, &aGstPaePdpes[0]);
5202 if (RT_SUCCESS(rc))
5203 {
5204# ifdef IN_RING3
5205 pVCpu->pgm.s.fPaePdpesAndCr3MappedR3 = true;
5206 pVCpu->pgm.s.fPaePdpesAndCr3MappedR0 = false;
5207# else
5208 pVCpu->pgm.s.fPaePdpesAndCr3MappedR3 = false;
5209 pVCpu->pgm.s.fPaePdpesAndCr3MappedR0 = true;
5210# endif
5211 pVCpu->pgm.s.GCPhysPaeCR3 = GCPhysCR3;
5212 }
5213
5214# elif PGM_GST_TYPE == PGM_TYPE_AMD64
5215# ifdef IN_RING3
5216 pVCpu->pgm.s.pGstAmd64Pml4R3 = (PX86PML4)HCPtrGuestCR3;
5217 pVCpu->pgm.s.pGstAmd64Pml4R0 = NIL_RTR0PTR;
5218# else
5219 pVCpu->pgm.s.pGstAmd64Pml4R3 = NIL_RTR3PTR;
5220 pVCpu->pgm.s.pGstAmd64Pml4R0 = (PX86PML4)HCPtrGuestCR3;
5221# endif
5222# endif
5223 }
5224 else
5225 AssertMsgFailed(("rc=%Rrc GCPhysGuestPD=%RGp\n", rc, GCPhysCR3));
5226 }
5227#endif
5228
5229 /*
5230 * Update shadow paging info for guest modes with paging (32-bit, PAE, AMD64).
5231 */
5232# if ( ( PGM_SHW_TYPE == PGM_TYPE_32BIT \
5233 || PGM_SHW_TYPE == PGM_TYPE_PAE \
5234 || PGM_SHW_TYPE == PGM_TYPE_AMD64) \
5235 && ( PGM_GST_TYPE != PGM_TYPE_REAL \
5236 && PGM_GST_TYPE != PGM_TYPE_PROT))
5237
5238 Assert(!pVM->pgm.s.fNestedPaging);
5239 PGM_A20_ASSERT_MASKED(pVCpu, GCPhysCR3);
5240
5241 /*
5242 * Update the shadow root page as well since that's not fixed.
5243 */
5244 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
5245 PPGMPOOLPAGE pOldShwPageCR3 = pVCpu->pgm.s.CTX_SUFF(pShwPageCR3);
5246 PPGMPOOLPAGE pNewShwPageCR3;
5247
5248 PGM_LOCK_VOID(pVM);
5249
5250# ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
5251 if (pPool->cDirtyPages)
5252 pgmPoolResetDirtyPages(pVM);
5253# endif
5254
5255 Assert(!(GCPhysCR3 >> (GUEST_PAGE_SHIFT + 32))); /** @todo what is this for? */
5256 int const rc2 = pgmPoolAlloc(pVM, GCPhysCR3 & GST_CR3_PAGE_MASK, BTH_PGMPOOLKIND_ROOT, PGMPOOLACCESS_DONTCARE,
5257 PGM_A20_IS_ENABLED(pVCpu), NIL_PGMPOOL_IDX, UINT32_MAX, true /*fLockPage*/, &pNewShwPageCR3);
5258 AssertFatalRC(rc2);
5259
5260 pVCpu->pgm.s.pShwPageCR3R3 = pgmPoolConvertPageToR3(pPool, pNewShwPageCR3);
5261 pVCpu->pgm.s.pShwPageCR3R0 = pgmPoolConvertPageToR0(pPool, pNewShwPageCR3);
5262
5263 /* Set the current hypervisor CR3. */
5264 CPUMSetHyperCR3(pVCpu, PGMGetHyperCR3(pVCpu));
5265
5266 /* Clean up the old CR3 root. */
5267 if ( pOldShwPageCR3
5268 && pOldShwPageCR3 != pNewShwPageCR3 /* @todo can happen due to incorrect syncing between REM & PGM; find the real cause */)
5269 {
5270 Assert(pOldShwPageCR3->enmKind != PGMPOOLKIND_FREE);
5271
5272 /* Mark the page as unlocked; allow flushing again. */
5273 pgmPoolUnlockPage(pPool, pOldShwPageCR3);
5274
5275 pgmPoolFreeByPage(pPool, pOldShwPageCR3, NIL_PGMPOOL_IDX, UINT32_MAX);
5276 }
5277 PGM_UNLOCK(pVM);
5278# else
5279 NOREF(GCPhysCR3);
5280# endif
5281
5282 return rc;
5283}
5284
5285/**
5286 * Unmaps the shadow CR3.
5287 *
5288 * @returns VBox status, no specials.
5289 * @param pVCpu The cross context virtual CPU structure.
5290 */
5291PGM_BTH_DECL(int, UnmapCR3)(PVMCPUCC pVCpu)
5292{
5293 LogFlow(("UnmapCR3\n"));
5294
5295 int rc = VINF_SUCCESS;
5296 PVMCC pVM = pVCpu->CTX_SUFF(pVM); NOREF(pVM);
5297
5298 /*
5299 * Update guest paging info.
5300 */
5301#if PGM_GST_TYPE == PGM_TYPE_32BIT
5302 pVCpu->pgm.s.pGst32BitPdR3 = 0;
5303 pVCpu->pgm.s.pGst32BitPdR0 = 0;
5304
5305#elif PGM_GST_TYPE == PGM_TYPE_PAE
5306 pVCpu->pgm.s.pGstPaePdptR3 = 0;
5307 pVCpu->pgm.s.pGstPaePdptR0 = 0;
5308 for (unsigned i = 0; i < X86_PG_PAE_PDPE_ENTRIES; i++)
5309 {
5310 pVCpu->pgm.s.apGstPaePDsR3[i] = 0;
5311 pVCpu->pgm.s.apGstPaePDsR0[i] = 0;
5312 pVCpu->pgm.s.aGCPhysGstPaePDs[i] = NIL_RTGCPHYS;
5313 }
5314
5315#elif PGM_GST_TYPE == PGM_TYPE_AMD64
5316 pVCpu->pgm.s.pGstAmd64Pml4R3 = 0;
5317 pVCpu->pgm.s.pGstAmd64Pml4R0 = 0;
5318
5319#else /* prot/real mode stub */
5320 /* nothing to do */
5321#endif
5322
5323 /*
5324 * PAE PDPEs (and CR3) might have been mapped via PGMGstMapPaePdpesAtCr3()
5325 * prior to switching to PAE in pfnMapCr3(), so we need to clear them here.
5326 */
5327 pVCpu->pgm.s.fPaePdpesAndCr3MappedR3 = false;
5328 pVCpu->pgm.s.fPaePdpesAndCr3MappedR0 = false;
5329 pVCpu->pgm.s.GCPhysPaeCR3 = NIL_RTGCPHYS;
5330
5331 /*
5332 * Update shadow paging info.
5333 */
5334#if ( ( PGM_SHW_TYPE == PGM_TYPE_32BIT \
5335 || PGM_SHW_TYPE == PGM_TYPE_PAE \
5336 || PGM_SHW_TYPE == PGM_TYPE_AMD64))
5337# if PGM_GST_TYPE != PGM_TYPE_REAL
5338 Assert(!pVM->pgm.s.fNestedPaging);
5339# endif
5340 PGM_LOCK_VOID(pVM);
5341
5342 if (pVCpu->pgm.s.CTX_SUFF(pShwPageCR3))
5343 {
5344 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
5345
5346# ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
5347 if (pPool->cDirtyPages)
5348 pgmPoolResetDirtyPages(pVM);
5349# endif
5350
5351 /* Mark the page as unlocked; allow flushing again. */
5352 pgmPoolUnlockPage(pPool, pVCpu->pgm.s.CTX_SUFF(pShwPageCR3));
5353
5354 pgmPoolFreeByPage(pPool, pVCpu->pgm.s.CTX_SUFF(pShwPageCR3), NIL_PGMPOOL_IDX, UINT32_MAX);
5355 pVCpu->pgm.s.pShwPageCR3R3 = 0;
5356 pVCpu->pgm.s.pShwPageCR3R0 = 0;
5357 }
5358
5359 PGM_UNLOCK(pVM);
5360#endif
5361
5362 return rc;
5363}
5364
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use