VirtualBox

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

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

Missing unlocks in error cases

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

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