VirtualBox

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

Last change on this file since 17505 was 17505, checked in by vboxsync, 16 years ago

PGM: MapCR3 hack for the new code, fixing PGMFlushTLB status propagation error in emUpdateCRx.

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