VirtualBox

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

Last change on this file since 96945 was 96879, checked in by vboxsync, 2 years ago

VMM/PGM: Nested VMX: bugref:10092 Nested EPT shadow page-pool handling.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette