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