VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/PGMAllGst.h@ 10342

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

Clear old cr3 values before attempting to allocate a new page from our pool.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 44.4 KB
Line 
1/* $Id: PGMAllGst.h 10342 2008-07-07 17:22:16Z vboxsync $ */
2/** @file
3 * VBox - Page Manager, Guest Paging Template - All context code.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Defined Constants And Macros *
25*******************************************************************************/
26#undef GSTPT
27#undef PGSTPT
28#undef GSTPTE
29#undef PGSTPTE
30#undef GSTPD
31#undef PGSTPD
32#undef GSTPDE
33#undef PGSTPDE
34#undef GST_BIG_PAGE_SIZE
35#undef GST_BIG_PAGE_OFFSET_MASK
36#undef GST_PDE_PG_MASK
37#undef GST_PDE_BIG_PG_MASK
38#undef GST_PD_SHIFT
39#undef GST_PD_MASK
40#undef GST_PTE_PG_MASK
41#undef GST_PT_SHIFT
42#undef GST_PT_MASK
43#undef GST_TOTAL_PD_ENTRIES
44#undef GST_CR3_PAGE_MASK
45#undef GST_PDPE_ENTRIES
46#undef GST_PDPT_SHIFT
47#undef GST_PDPT_MASK
48#undef GST_PDPE_PG_MASK
49
50#if PGM_GST_TYPE == PGM_TYPE_REAL \
51 || PGM_GST_TYPE == PGM_TYPE_PROT
52# define GSTPT SHWPT
53# define PGSTPT PSHWPT
54# define GSTPTE SHWPTE
55# define PGSTPTE PSHWPTE
56# define GSTPD SHWPD
57# define PGSTPD PSHWPD
58# define GSTPDE SHWPDE
59# define PGSTPDE PSHWPDE
60# define GST_PTE_PG_MASK SHW_PTE_PG_MASK
61#elif PGM_GST_TYPE == PGM_TYPE_32BIT
62# define GSTPT X86PT
63# define PGSTPT PX86PT
64# define GSTPTE X86PTE
65# define PGSTPTE PX86PTE
66# define GSTPD X86PD
67# define PGSTPD PX86PD
68# define GSTPDE X86PDE
69# define PGSTPDE PX86PDE
70# define GST_BIG_PAGE_SIZE X86_PAGE_4M_SIZE
71# define GST_BIG_PAGE_OFFSET_MASK X86_PAGE_4M_OFFSET_MASK
72# define GST_PDE_PG_MASK X86_PDE_PG_MASK
73# define GST_PDE_BIG_PG_MASK X86_PDE4M_PG_MASK
74# define GST_PD_SHIFT X86_PD_SHIFT
75# define GST_PD_MASK X86_PD_MASK
76# define GST_TOTAL_PD_ENTRIES X86_PG_ENTRIES
77# define GST_PTE_PG_MASK X86_PTE_PG_MASK
78# define GST_PT_SHIFT X86_PT_SHIFT
79# define GST_PT_MASK X86_PT_MASK
80# define GST_CR3_PAGE_MASK X86_CR3_PAGE_MASK
81#elif PGM_GST_TYPE == PGM_TYPE_PAE \
82 || PGM_GST_TYPE == PGM_TYPE_AMD64
83# define GSTPT X86PTPAE
84# define PGSTPT PX86PTPAE
85# define GSTPTE X86PTEPAE
86# define PGSTPTE PX86PTEPAE
87# define GSTPD X86PDPAE
88# define PGSTPD PX86PDPAE
89# define GSTPDE X86PDEPAE
90# define PGSTPDE PX86PDEPAE
91# define GST_BIG_PAGE_SIZE X86_PAGE_2M_SIZE
92# define GST_BIG_PAGE_OFFSET_MASK X86_PAGE_2M_OFFSET_MASK
93# define GST_PDE_PG_MASK X86_PDE_PAE_PG_MASK_FULL
94# define GST_PDE_BIG_PG_MASK X86_PDE2M_PAE_PG_MASK
95# define GST_PD_SHIFT X86_PD_PAE_SHIFT
96# define GST_PD_MASK X86_PD_PAE_MASK
97# if PGM_GST_TYPE == PGM_TYPE_PAE
98# define GST_TOTAL_PD_ENTRIES (X86_PG_PAE_ENTRIES * X86_PG_PAE_PDPE_ENTRIES)
99# define GST_PDPE_ENTRIES X86_PG_PAE_PDPE_ENTRIES
100# define GST_PDPE_PG_MASK X86_PDPE_PG_MASK_FULL
101# define GST_PDPT_SHIFT X86_PDPT_SHIFT
102# define GST_PDPT_MASK X86_PDPT_MASK_PAE
103# define GST_PTE_PG_MASK X86_PTE_PAE_PG_MASK
104# define GST_CR3_PAGE_MASK X86_CR3_PAE_PAGE_MASK
105# else
106# define GST_TOTAL_PD_ENTRIES (X86_PG_AMD64_ENTRIES * X86_PG_AMD64_PDPE_ENTRIES)
107# define GST_PDPE_ENTRIES X86_PG_AMD64_PDPE_ENTRIES
108# define GST_PDPT_SHIFT X86_PDPT_SHIFT
109# define GST_PDPE_PG_MASK X86_PDPE_PG_MASK_FULL
110# define GST_PDPT_MASK X86_PDPT_MASK_AMD64
111# define GST_PTE_PG_MASK X86_PTE_PAE_PG_MASK_FULL
112# define GST_CR3_PAGE_MASK X86_CR3_AMD64_PAGE_MASK
113# endif
114# define GST_PT_SHIFT X86_PT_PAE_SHIFT
115# define GST_PT_MASK X86_PT_PAE_MASK
116#endif
117
118
119/*******************************************************************************
120* Internal Functions *
121*******************************************************************************/
122__BEGIN_DECLS
123PGM_GST_DECL(int, GetPage)(PVM pVM, RTGCUINTPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys);
124PGM_GST_DECL(int, ModifyPage)(PVM pVM, RTGCUINTPTR GCPtr, size_t cb, uint64_t fFlags, uint64_t fMask);
125PGM_GST_DECL(int, GetPDE)(PVM pVM, RTGCUINTPTR GCPtr, PX86PDEPAE pPDE);
126PGM_GST_DECL(int, MapCR3)(PVM pVM, RTGCPHYS GCPhysCR3);
127PGM_GST_DECL(int, UnmapCR3)(PVM pVM);
128PGM_GST_DECL(int, MonitorCR3)(PVM pVM, RTGCPHYS GCPhysCR3);
129PGM_GST_DECL(int, UnmonitorCR3)(PVM pVM);
130PGM_GST_DECL(bool, HandlerVirtualUpdate)(PVM pVM, uint32_t cr4);
131#ifndef IN_RING3
132PGM_GST_DECL(int, WriteHandlerCR3)(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser);
133# if PGM_GST_TYPE == PGM_TYPE_PAE \
134 || PGM_GST_TYPE == PGM_TYPE_AMD64
135PGM_GST_DECL(int, PAEWriteHandlerPD)(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser);
136# endif
137#endif
138__END_DECLS
139
140
141
142/**
143 * Gets effective Guest OS page information.
144 *
145 * When GCPtr is in a big page, the function will return as if it was a normal
146 * 4KB page. If the need for distinguishing between big and normal page becomes
147 * necessary at a later point, a PGMGstGetPage Ex() will be created for that
148 * purpose.
149 *
150 * @returns VBox status.
151 * @param pVM VM Handle.
152 * @param GCPtr Guest Context virtual address of the page. Page aligned!
153 * @param pfFlags Where to store the flags. These are X86_PTE_*, even for big pages.
154 * @param pGCPhys Where to store the GC physical address of the page.
155 * This is page aligned. The fact that the
156 */
157PGM_GST_DECL(int, GetPage)(PVM pVM, RTGCUINTPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys)
158{
159#if PGM_GST_TYPE == PGM_TYPE_REAL \
160 || PGM_GST_TYPE == PGM_TYPE_PROT
161 /*
162 * Fake it.
163 */
164 if (pfFlags)
165 *pfFlags = X86_PTE_P | X86_PTE_RW | X86_PTE_US;
166 if (pGCPhys)
167 *pGCPhys = GCPtr & PAGE_BASE_GC_MASK;
168 return VINF_SUCCESS;
169
170#elif PGM_GST_TYPE == PGM_TYPE_32BIT || PGM_GST_TYPE == PGM_TYPE_PAE || PGM_GST_TYPE == PGM_TYPE_AMD64
171
172 /*
173 * Get the PDE.
174 */
175# if PGM_GST_TYPE == PGM_TYPE_32BIT
176 const X86PDE Pde = CTXSUFF(pVM->pgm.s.pGuestPD)->a[GCPtr >> X86_PD_SHIFT];
177#elif PGM_GST_TYPE == PGM_TYPE_PAE
178 X86PDEPAE Pde;
179 bool fNoExecuteBitValid = !!(CPUMGetGuestEFER(pVM) & MSR_K6_EFER_NXE);
180
181 /* pgmGstGetPaePDE will return 0 if the PDPTE is marked as not present
182 * All the other bits in the PDPTE are only valid in long mode (r/w, u/s, nx)
183 */
184 Pde.u = pgmGstGetPaePDE(&pVM->pgm.s, GCPtr);
185#elif PGM_GST_TYPE == PGM_TYPE_AMD64
186 PX86PML4E pPml4e;
187 X86PDPE Pdpe;
188 X86PDEPAE Pde;
189 bool fNoExecuteBitValid = !!(CPUMGetGuestEFER(pVM) & MSR_K6_EFER_NXE);
190
191 Pde.u = pgmGstGetLongModePDE(&pVM->pgm.s, GCPtr, &pPml4e, &Pdpe);
192 Assert(pPml4e);
193 if (!(pPml4e->n.u1Present & Pdpe.n.u1Present))
194 return VERR_PAGE_TABLE_NOT_PRESENT;
195
196 /* Merge accessed, write, user and no-execute bits into the PDE. */
197 Pde.n.u1Accessed &= pPml4e->n.u1Accessed & Pdpe.lm.u1Accessed;
198 Pde.n.u1Write &= pPml4e->n.u1Write & Pdpe.lm.u1Write;
199 Pde.n.u1User &= pPml4e->n.u1User & Pdpe.lm.u1User;
200 Pde.n.u1NoExecute &= pPml4e->n.u1NoExecute & Pdpe.lm.u1NoExecute;
201# endif
202
203 /*
204 * Lookup the page.
205 */
206 if (!Pde.n.u1Present)
207 return VERR_PAGE_TABLE_NOT_PRESENT;
208
209 if ( !Pde.b.u1Size
210# if PGM_GST_TYPE != PGM_TYPE_AMD64
211 || !(CPUMGetGuestCR4(pVM) & X86_CR4_PSE)
212# endif
213 )
214 {
215 PGSTPT pPT;
216 int rc = PGM_GCPHYS_2_PTR(pVM, Pde.u & GST_PDE_PG_MASK, &pPT);
217 if (VBOX_FAILURE(rc))
218 return rc;
219
220 /*
221 * Get PT entry and check presence.
222 */
223 const GSTPTE Pte = pPT->a[(GCPtr >> GST_PT_SHIFT) & GST_PT_MASK];
224 if (!Pte.n.u1Present)
225 return VERR_PAGE_NOT_PRESENT;
226
227 /*
228 * Store the result.
229 * RW and US flags depend on all levels (bitwise AND) - except for legacy PAE
230 * where the PDPE is simplified.
231 */
232 if (pfFlags)
233 {
234 *pfFlags = (Pte.u & ~GST_PTE_PG_MASK)
235 & ((Pde.u & (X86_PTE_RW | X86_PTE_US)) | ~(uint64_t)(X86_PTE_RW | X86_PTE_US));
236# if PGM_WITH_NX(PGM_GST_TYPE)
237 /* The NX bit is determined by a bitwise OR between the PT and PD */
238 if (fNoExecuteBitValid)
239 *pfFlags |= (Pte.u & Pde.u & X86_PTE_PAE_NX);
240# endif
241 }
242 if (pGCPhys)
243 *pGCPhys = Pte.u & GST_PTE_PG_MASK;
244 }
245 else
246 {
247 /*
248 * Map big to 4k PTE and store the result
249 */
250 if (pfFlags)
251 {
252 *pfFlags = (Pde.u & ~(GST_PTE_PG_MASK | X86_PTE_PAT))
253 | ((Pde.u & X86_PDE4M_PAT) >> X86_PDE4M_PAT_SHIFT);
254# if PGM_WITH_NX(PGM_GST_TYPE)
255 /* The NX bit is determined by a bitwise OR between the PT and PD */
256 if (fNoExecuteBitValid)
257 *pfFlags |= (Pde.u & X86_PTE_PAE_NX);
258# endif
259 }
260 if (pGCPhys)
261 *pGCPhys = (Pde.u & GST_PDE_BIG_PG_MASK) | (GCPtr & (~GST_PDE_BIG_PG_MASK ^ ~GST_PTE_PG_MASK)); /** @todo pse36 */
262 }
263 return VINF_SUCCESS;
264#else
265# error "shouldn't be here!"
266 /* something else... */
267 return VERR_NOT_SUPPORTED;
268#endif
269}
270
271
272/**
273 * Modify page flags for a range of pages in the guest's tables
274 *
275 * The existing flags are ANDed with the fMask and ORed with the fFlags.
276 *
277 * @returns VBox status code.
278 * @param pVM VM handle.
279 * @param GCPtr Virtual address of the first page in the range. Page aligned!
280 * @param cb Size (in bytes) of the page range to apply the modification to. Page aligned!
281 * @param fFlags The OR mask - page flags X86_PTE_*, excluding the page mask of course.
282 * @param fMask The AND mask - page flags X86_PTE_*.
283 */
284PGM_GST_DECL(int, ModifyPage)(PVM pVM, RTGCUINTPTR GCPtr, size_t cb, uint64_t fFlags, uint64_t fMask)
285{
286#if PGM_GST_TYPE == PGM_TYPE_32BIT \
287 || PGM_GST_TYPE == PGM_TYPE_PAE \
288 || PGM_GST_TYPE == PGM_TYPE_AMD64
289
290 for (;;)
291 {
292 /*
293 * Get the PD entry.
294 */
295# if PGM_GST_TYPE == PGM_TYPE_32BIT
296 PX86PDE pPde = &CTXSUFF(pVM->pgm.s.pGuestPD)->a[GCPtr >> X86_PD_SHIFT];
297# elif PGM_GST_TYPE == PGM_TYPE_PAE
298 /* pgmGstGetPaePDEPtr will return 0 if the PDPTE is marked as not present
299 * All the other bits in the PDPTE are only valid in long mode (r/w, u/s, nx)
300 */
301 PX86PDEPAE pPde = pgmGstGetPaePDEPtr(&pVM->pgm.s, GCPtr);
302 Assert(pPde);
303 if (!pPde)
304 return VERR_PAGE_TABLE_NOT_PRESENT;
305# elif PGM_GST_TYPE == PGM_TYPE_AMD64
306 /** @todo Setting the r/w, u/s & nx bits might have no effect depending on the pdpte & pml4 values */
307 PX86PDEPAE pPde = pgmGstGetLongModePDEPtr(&pVM->pgm.s, GCPtr);
308 Assert(pPde);
309 if (!pPde)
310 return VERR_PAGE_TABLE_NOT_PRESENT;
311# endif
312 GSTPDE Pde = *pPde;
313 Assert(Pde.n.u1Present);
314 if (!Pde.n.u1Present)
315 return VERR_PAGE_TABLE_NOT_PRESENT;
316
317 if ( !Pde.b.u1Size
318# if PGM_GST_TYPE != PGM_TYPE_AMD64
319 || !(CPUMGetGuestCR4(pVM) & X86_CR4_PSE)
320# endif
321 )
322 {
323 /*
324 * 4KB Page table
325 *
326 * Walk page tables and pages till we're done.
327 */
328 PGSTPT pPT;
329 int rc = PGM_GCPHYS_2_PTR(pVM, Pde.u & GST_PDE_PG_MASK, &pPT);
330 if (VBOX_FAILURE(rc))
331 return rc;
332
333 unsigned iPTE = (GCPtr >> GST_PT_SHIFT) & GST_PT_MASK;
334 while (iPTE < RT_ELEMENTS(pPT->a))
335 {
336 GSTPTE Pte = pPT->a[iPTE];
337 Pte.u = (Pte.u & (fMask | X86_PTE_PAE_PG_MASK))
338 | (fFlags & ~GST_PTE_PG_MASK);
339 pPT->a[iPTE] = Pte;
340
341 /* next page */
342 cb -= PAGE_SIZE;
343 if (!cb)
344 return VINF_SUCCESS;
345 GCPtr += PAGE_SIZE;
346 iPTE++;
347 }
348 }
349 else
350 {
351 /*
352 * 4MB Page table
353 */
354 Pde.u = (Pde.u & (fMask | ((fMask & X86_PTE_PAT) << X86_PDE4M_PAT_SHIFT) | GST_PDE_BIG_PG_MASK | X86_PDE4M_PS)) /** @todo pse36 */
355 | (fFlags & ~GST_PTE_PG_MASK)
356 | ((fFlags & X86_PTE_PAT) << X86_PDE4M_PAT_SHIFT);
357 *pPde = Pde;
358
359 /* advance */
360 const unsigned cbDone = GST_BIG_PAGE_SIZE - (GCPtr & GST_BIG_PAGE_OFFSET_MASK);
361 if (cbDone >= cb)
362 return VINF_SUCCESS;
363 cb -= cbDone;
364 GCPtr += cbDone;
365 }
366 }
367
368#else
369 /* real / protected mode: ignore. */
370 return VINF_SUCCESS;
371#endif
372}
373
374
375/**
376 * Retrieve guest PDE information
377 *
378 * @returns VBox status code.
379 * @param pVM The virtual machine.
380 * @param GCPtr Guest context pointer
381 * @param pPDE Pointer to guest PDE structure
382 */
383PGM_GST_DECL(int, GetPDE)(PVM pVM, RTGCUINTPTR GCPtr, PX86PDEPAE pPDE)
384{
385#if PGM_GST_TYPE == PGM_TYPE_32BIT \
386 || PGM_GST_TYPE == PGM_TYPE_PAE \
387 || PGM_GST_TYPE == PGM_TYPE_AMD64
388
389# if PGM_GST_TYPE == PGM_TYPE_32BIT
390 X86PDE Pde;
391 Pde = CTXSUFF(pVM->pgm.s.pGuestPD)->a[GCPtr >> GST_PD_SHIFT];
392# elif PGM_GST_TYPE == PGM_TYPE_PAE
393 X86PDEPAE Pde;
394 Pde.u = pgmGstGetPaePDE(&pVM->pgm.s, GCPtr);
395# elif PGM_GST_TYPE == PGM_TYPE_AMD64
396 X86PDEPAE Pde;
397 Pde.u = pgmGstGetLongModePDE(&pVM->pgm.s, GCPtr);
398# endif
399
400 pPDE->u = (X86PGPAEUINT)Pde.u;
401 return VINF_SUCCESS;
402#else
403 AssertFailed();
404 return VERR_NOT_IMPLEMENTED;
405#endif
406}
407
408
409
410/**
411 * Maps the CR3 into HMA in GC and locate it in HC.
412 *
413 * Note that a MapCR3 call is usually not followed by an UnmapCR3 call; whenever
414 * CR3 is updated we simply call MapCR3 again.
415 *
416 * @returns VBox status, no specials.
417 * @param pVM VM handle.
418 * @param GCPhysCR3 The physical address in the CR3 register.
419 */
420PGM_GST_DECL(int, MapCR3)(PVM pVM, RTGCPHYS GCPhysCR3)
421{
422#if PGM_GST_TYPE == PGM_TYPE_32BIT \
423 || PGM_GST_TYPE == PGM_TYPE_PAE \
424 || PGM_GST_TYPE == PGM_TYPE_AMD64
425
426 LogFlow(("MapCR3: %VGp\n", GCPhysCR3));
427
428 /*
429 * Map the page CR3 points at.
430 */
431 RTHCPHYS HCPhysGuestCR3;
432 RTHCPTR HCPtrGuestCR3;
433 int rc = pgmRamGCPhys2HCPtrAndHCPhysWithFlags(&pVM->pgm.s, GCPhysCR3 & GST_CR3_PAGE_MASK, &HCPtrGuestCR3, &HCPhysGuestCR3);
434 if (VBOX_SUCCESS(rc))
435 {
436 rc = PGMMap(pVM, (RTGCUINTPTR)pVM->pgm.s.GCPtrCR3Mapping, HCPhysGuestCR3, PAGE_SIZE, 0);
437 if (VBOX_SUCCESS(rc))
438 {
439 PGM_INVL_PG(pVM->pgm.s.GCPtrCR3Mapping);
440# if PGM_GST_TYPE == PGM_TYPE_32BIT
441 pVM->pgm.s.pGuestPDHC = (R3R0PTRTYPE(PX86PD))HCPtrGuestCR3;
442 pVM->pgm.s.pGuestPDGC = (RCPTRTYPE(PX86PD))pVM->pgm.s.GCPtrCR3Mapping;
443
444# elif PGM_GST_TYPE == PGM_TYPE_PAE
445 unsigned offset = GCPhysCR3 & GST_CR3_PAGE_MASK & PAGE_OFFSET_MASK;
446 pVM->pgm.s.pGstPaePDPTHC = (R3R0PTRTYPE(PX86PDPT)) HCPtrGuestCR3;
447 pVM->pgm.s.pGstPaePDPTGC = (RCPTRTYPE(PX86PDPT)) ((RCPTRTYPE(uint8_t *))pVM->pgm.s.GCPtrCR3Mapping + offset);
448 Log(("Cached mapping %VGv\n", pVM->pgm.s.pGstPaePDPTGC));
449
450 /*
451 * Map the 4 PDs too.
452 */
453 RTGCUINTPTR GCPtr = (RTGCUINTPTR)pVM->pgm.s.GCPtrCR3Mapping + PAGE_SIZE;
454 for (unsigned i = 0; i < X86_PG_PAE_PDPE_ENTRIES; i++, GCPtr += PAGE_SIZE)
455 {
456 if (pVM->pgm.s.CTXSUFF(pGstPaePDPT)->a[i].n.u1Present)
457 {
458 RTHCPTR HCPtr;
459 RTHCPHYS HCPhys;
460 RTGCPHYS GCPhys = pVM->pgm.s.CTXSUFF(pGstPaePDPT)->a[i].u & X86_PDPE_PG_MASK;
461 int rc2 = pgmRamGCPhys2HCPtrAndHCPhysWithFlags(&pVM->pgm.s, GCPhys, &HCPtr, &HCPhys);
462 if (VBOX_SUCCESS(rc2))
463 {
464 rc = PGMMap(pVM, GCPtr, HCPhys & X86_PTE_PAE_PG_MASK, PAGE_SIZE, 0);
465 AssertRCReturn(rc, rc);
466 pVM->pgm.s.apGstPaePDsHC[i] = (R3R0PTRTYPE(PX86PDPAE))HCPtr;
467 pVM->pgm.s.apGstPaePDsGC[i] = (RCPTRTYPE(PX86PDPAE))GCPtr;
468 pVM->pgm.s.aGCPhysGstPaePDs[i] = GCPhys;
469 PGM_INVL_PG(GCPtr);
470 continue;
471 }
472 AssertMsgFailed(("pgmR3Gst32BitMapCR3: rc2=%d GCPhys=%RGp i=%d\n", rc2, GCPhys, i));
473 }
474
475 pVM->pgm.s.apGstPaePDsHC[i] = 0;
476 pVM->pgm.s.apGstPaePDsGC[i] = 0;
477 pVM->pgm.s.aGCPhysGstPaePDs[i] = NIL_RTGCPHYS;
478 PGM_INVL_PG(GCPtr);
479 }
480# elif PGM_GST_TYPE == PGM_TYPE_AMD64
481 PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
482
483 pVM->pgm.s.pGstPaePML4HC = (R3R0PTRTYPE(PX86PML4))HCPtrGuestCR3;
484
485 if (!HWACCMIsNestedPagingActive(pVM))
486 {
487 if (pVM->pgm.s.pHCShwAmd64CR3)
488 {
489 pgmPoolFreeByPage(pPool, pVM->pgm.s.pHCShwAmd64CR3, PGMPOOL_IDX_AMD64_CR3, pVM->pgm.s.pHCShwAmd64CR3->GCPhys >> PAGE_SHIFT);
490 pVM->pgm.s.pHCShwAmd64CR3 = 0;
491 pVM->pgm.s.pHCPaePML4 = 0;
492 pVM->pgm.s.HCPhysPaePML4 = 0;
493 }
494
495 Assert(!(GCPhysCR3 >> (PAGE_SHIFT + 32)));
496try_again:
497 rc = pgmPoolAlloc(pVM, GCPhysCR3, PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4, PGMPOOL_IDX_AMD64_CR3, GCPhysCR3 >> PAGE_SHIFT, &pVM->pgm.s.pHCShwAmd64CR3);
498 if (rc == VERR_PGM_POOL_FLUSHED)
499 {
500 Assert(pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL);
501 rc = pgmPoolSyncCR3(pVM);
502 AssertRC(rc);
503 goto try_again;
504 }
505 pVM->pgm.s.pHCPaePML4 = (PX86PML4)PGMPOOL_PAGE_2_PTR(pPool->CTXSUFF(pVM), pVM->pgm.s.pHCShwAmd64CR3);
506 pVM->pgm.s.HCPhysPaePML4 = pVM->pgm.s.pHCShwAmd64CR3->Core.Key;
507 }
508# endif
509 }
510 else
511 AssertMsgFailed(("rc=%Vrc GCPhysGuestPD=%VGp\n", rc, GCPhysCR3));
512 }
513 else
514 AssertMsgFailed(("rc=%Vrc GCPhysGuestPD=%VGp\n", rc, GCPhysCR3));
515
516#else /* prot/real stub */
517 int rc = VINF_SUCCESS;
518#endif
519 return rc;
520}
521
522
523/**
524 * Unmaps the CR3.
525 *
526 * @returns VBox status, no specials.
527 * @param pVM VM handle.
528 */
529PGM_GST_DECL(int, UnmapCR3)(PVM pVM)
530{
531 LogFlow(("UnmapCR3\n"));
532
533 int rc = VINF_SUCCESS;
534
535#if PGM_GST_TYPE == PGM_TYPE_32BIT
536 pVM->pgm.s.pGuestPDHC = 0;
537 pVM->pgm.s.pGuestPDGC = 0;
538
539#elif PGM_GST_TYPE == PGM_TYPE_PAE
540 pVM->pgm.s.pGstPaePDPTHC = 0;
541 pVM->pgm.s.pGstPaePDPTGC = 0;
542 for (unsigned i=0; i < X86_PG_PAE_PDPE_ENTRIES; i++)
543 {
544 pVM->pgm.s.apGstPaePDsHC[i] = 0;
545 pVM->pgm.s.apGstPaePDsGC[i] = 0;
546 pVM->pgm.s.aGCPhysGstPaePDs[i] = NIL_RTGCPHYS;
547 }
548
549#elif PGM_GST_TYPE == PGM_TYPE_AMD64
550 pVM->pgm.s.pGstPaePML4HC = 0;
551 if (!HWACCMIsNestedPagingActive(pVM))
552 {
553 pVM->pgm.s.pHCPaePML4 = 0;
554 if (pVM->pgm.s.pHCShwAmd64CR3)
555 {
556 PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
557 pgmPoolFreeByPage(pPool, pVM->pgm.s.pHCShwAmd64CR3, PGMPOOL_IDX_AMD64_CR3, pVM->pgm.s.pHCShwAmd64CR3->GCPhys >> PAGE_SHIFT);
558 pVM->pgm.s.pHCShwAmd64CR3 = NULL;
559 }
560 }
561
562#else /* prot/real mode stub */
563 /* nothing to do */
564#endif
565 return rc;
566}
567
568
569#undef LOG_GROUP
570#define LOG_GROUP LOG_GROUP_PGM_POOL
571
572/**
573 * Registers physical page monitors for the necessary paging
574 * structures to detect conflicts with our guest mappings.
575 *
576 * This is always called after mapping CR3.
577 * This is never called with fixed mappings.
578 *
579 * @returns VBox status, no specials.
580 * @param pVM VM handle.
581 * @param GCPhysCR3 The physical address in the CR3 register.
582 */
583PGM_GST_DECL(int, MonitorCR3)(PVM pVM, RTGCPHYS GCPhysCR3)
584{
585 Assert(!pVM->pgm.s.fMappingsFixed);
586 int rc = VINF_SUCCESS;
587
588 /*
589 * Register/Modify write phys handler for guest's CR3 if it changed.
590 */
591#if PGM_GST_TYPE == PGM_TYPE_32BIT
592
593 if (pVM->pgm.s.GCPhysGstCR3Monitored != GCPhysCR3)
594 {
595# ifndef PGMPOOL_WITH_MIXED_PT_CR3
596 const unsigned cbCR3Stuff = PGM_GST_TYPE == PGM_TYPE_PAE ? 32 : PAGE_SIZE;
597 if (pVM->pgm.s.GCPhysGstCR3Monitored != NIL_RTGCPHYS)
598 rc = PGMHandlerPhysicalModify(pVM, pVM->pgm.s.GCPhysGstCR3Monitored, GCPhysCR3, GCPhysCR3 + cbCR3Stuff - 1);
599 else
600 rc = PGMHandlerPhysicalRegisterEx(pVM, PGMPHYSHANDLERTYPE_PHYSICAL_WRITE, GCPhysCR3, GCPhysCR3 + cbCR3Stuff - 1,
601 pVM->pgm.s.pfnR3GstWriteHandlerCR3, 0,
602 pVM->pgm.s.pfnR0GstWriteHandlerCR3, 0,
603 pVM->pgm.s.pfnGCGstWriteHandlerCR3, 0,
604 pVM->pgm.s.pszR3GstWriteHandlerCR3);
605# else /* PGMPOOL_WITH_MIXED_PT_CR3 */
606 rc = pgmPoolMonitorMonitorCR3(pVM->pgm.s.CTXSUFF(pPool),
607 pVM->pgm.s.enmShadowMode == PGMMODE_PAE
608 || pVM->pgm.s.enmShadowMode == PGMMODE_PAE_NX
609 ? PGMPOOL_IDX_PAE_PD
610 : PGMPOOL_IDX_PD,
611 GCPhysCR3);
612# endif /* PGMPOOL_WITH_MIXED_PT_CR3 */
613 if (VBOX_FAILURE(rc))
614 {
615 AssertMsgFailed(("PGMHandlerPhysicalModify/PGMR3HandlerPhysicalRegister failed, rc=%Rrc GCPhysGstCR3Monitored=%RGp GCPhysCR3=%RGp\n",
616 rc, pVM->pgm.s.GCPhysGstCR3Monitored, GCPhysCR3));
617 return rc;
618 }
619 pVM->pgm.s.GCPhysGstCR3Monitored = GCPhysCR3;
620 }
621
622#elif PGM_GST_TYPE == PGM_TYPE_PAE
623 /* Monitor the PDPT page */
624 /*
625 * Register/Modify write phys handler for guest's CR3 if it changed.
626 */
627# ifndef PGMPOOL_WITH_MIXED_PT_CR3
628 AssertFailed();
629# endif
630 if (pVM->pgm.s.GCPhysGstCR3Monitored != GCPhysCR3)
631 {
632 rc = pgmPoolMonitorMonitorCR3(pVM->pgm.s.CTXSUFF(pPool), PGMPOOL_IDX_PDPT, GCPhysCR3);
633 if (VBOX_FAILURE(rc))
634 {
635 AssertMsgFailed(("PGMHandlerPhysicalModify/PGMR3HandlerPhysicalRegister failed, rc=%Rrc GCPhysGstCR3Monitored=%RGp GCPhysCR3=%RGp\n",
636 rc, pVM->pgm.s.GCPhysGstCR3Monitored, GCPhysCR3));
637 return rc;
638 }
639 pVM->pgm.s.GCPhysGstCR3Monitored = GCPhysCR3;
640 }
641 /*
642 * Do the 4 PDs.
643 */
644 for (unsigned i = 0; i < X86_PG_PAE_PDPE_ENTRIES; i++)
645 {
646 if (CTXSUFF(pVM->pgm.s.pGstPaePDPT)->a[i].n.u1Present)
647 {
648 RTGCPHYS GCPhys = CTXSUFF(pVM->pgm.s.pGstPaePDPT)->a[i].u & X86_PDPE_PG_MASK;
649 if (pVM->pgm.s.aGCPhysGstPaePDsMonitored[i] != GCPhys)
650 {
651 Assert(pVM->pgm.s.enmShadowMode == PGMMODE_PAE || pVM->pgm.s.enmShadowMode == PGMMODE_PAE_NX);
652
653 rc = pgmPoolMonitorMonitorCR3(pVM->pgm.s.CTXSUFF(pPool), PGMPOOL_IDX_PAE_PD_0 + i, GCPhys);
654 }
655
656 if (VBOX_FAILURE(rc))
657 {
658 AssertMsgFailed(("PGMHandlerPhysicalModify/PGMR3HandlerPhysicalRegister failed, rc=%Rrc GCPhysGstCR3Monitored=%RGp GCPhysCR3=%RGp\n",
659 rc, pVM->pgm.s.aGCPhysGstPaePDsMonitored[i], GCPhys));
660 return rc;
661 }
662 pVM->pgm.s.aGCPhysGstPaePDsMonitored[i] = GCPhys;
663 }
664 else if (pVM->pgm.s.aGCPhysGstPaePDsMonitored[i] != NIL_RTGCPHYS)
665 {
666 rc = pgmPoolMonitorUnmonitorCR3(pVM->pgm.s.CTXSUFF(pPool), PGMPOOL_IDX_PAE_PD_0 + i);
667 AssertRC(rc);
668 pVM->pgm.s.aGCPhysGstPaePDsMonitored[i] = NIL_RTGCPHYS;
669 }
670 }
671
672#else
673 /* prot/real/amd64 mode stub */
674
675#endif
676 return rc;
677}
678
679/**
680 * Deregisters any physical page monitors installed by MonitorCR3.
681 *
682 * @returns VBox status code, no specials.
683 * @param pVM The VM handle.
684 */
685PGM_GST_DECL(int, UnmonitorCR3)(PVM pVM)
686{
687 int rc = VINF_SUCCESS;
688
689 /*
690 * Deregister the access handlers.
691 *
692 * PGMSyncCR3 will reinstall it if required and PGMSyncCR3 will be executed
693 * before we enter GC again.
694 */
695#if PGM_GST_TYPE == PGM_TYPE_32BIT
696 if (pVM->pgm.s.GCPhysGstCR3Monitored != NIL_RTGCPHYS)
697 {
698# ifndef PGMPOOL_WITH_MIXED_PT_CR3
699 rc = PGMHandlerPhysicalDeregister(pVM, pVM->pgm.s.GCPhysGstCR3Monitored);
700 AssertRCReturn(rc, rc);
701# else /* PGMPOOL_WITH_MIXED_PT_CR3 */
702 rc = pgmPoolMonitorUnmonitorCR3(pVM->pgm.s.CTXSUFF(pPool),
703 pVM->pgm.s.enmShadowMode == PGMMODE_PAE
704 || pVM->pgm.s.enmShadowMode == PGMMODE_PAE_NX
705 ? PGMPOOL_IDX_PAE_PD
706 : PGMPOOL_IDX_PD);
707 AssertRCReturn(rc, rc);
708# endif /* PGMPOOL_WITH_MIXED_PT_CR3 */
709 pVM->pgm.s.GCPhysGstCR3Monitored = NIL_RTGCPHYS;
710 }
711
712#elif PGM_GST_TYPE == PGM_TYPE_PAE
713 /* The PDPT page */
714# ifndef PGMPOOL_WITH_MIXED_PT_CR3
715 AssertFailed();
716# endif
717
718 if (pVM->pgm.s.GCPhysGstCR3Monitored != NIL_RTGCPHYS)
719 {
720 rc = pgmPoolMonitorUnmonitorCR3(pVM->pgm.s.CTXSUFF(pPool), PGMPOOL_IDX_PDPT);
721 AssertRC(rc);
722 }
723
724 /* The 4 PDs. */
725 for (unsigned i = 0; i < X86_PG_PAE_PDPE_ENTRIES; i++)
726 {
727 if (pVM->pgm.s.aGCPhysGstPaePDsMonitored[i] != NIL_RTGCPHYS)
728 {
729 Assert(pVM->pgm.s.enmShadowMode == PGMMODE_PAE || pVM->pgm.s.enmShadowMode == PGMMODE_PAE_NX);
730 int rc2 = pgmPoolMonitorUnmonitorCR3(pVM->pgm.s.CTXSUFF(pPool), PGMPOOL_IDX_PAE_PD_0 + i);
731 AssertRC(rc2);
732 if (VBOX_FAILURE(rc2))
733 rc = rc2;
734 pVM->pgm.s.aGCPhysGstPaePDsMonitored[i] = NIL_RTGCPHYS;
735 }
736 }
737#else
738 /* prot/real/amd64 mode stub */
739#endif
740 return rc;
741
742}
743
744#undef LOG_GROUP
745#define LOG_GROUP LOG_GROUP_PGM
746
747
748#if PGM_GST_TYPE == PGM_TYPE_32BIT \
749 || PGM_GST_TYPE == PGM_TYPE_PAE \
750 || PGM_GST_TYPE == PGM_TYPE_AMD64
751/**
752 * Updates one virtual handler range.
753 *
754 * @returns 0
755 * @param pNode Pointer to a PGMVIRTHANDLER.
756 * @param pvUser Pointer to a PGMVHUARGS structure (see PGM.cpp).
757 */
758static DECLCALLBACK(int) PGM_GST_NAME(VirtHandlerUpdateOne)(PAVLROGCPTRNODECORE pNode, void *pvUser)
759{
760 PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)pNode;
761 PPGMHVUSTATE pState = (PPGMHVUSTATE)pvUser;
762 Assert(pCur->enmType != PGMVIRTHANDLERTYPE_HYPERVISOR);
763
764#if PGM_GST_TYPE == PGM_TYPE_32BIT
765 PX86PD pPDSrc = pState->pVM->pgm.s.CTXSUFF(pGuestPD);
766#endif
767
768 RTGCUINTPTR GCPtr = (RTUINTPTR)pCur->GCPtr;
769#if PGM_GST_MODE != PGM_MODE_AMD64
770 /* skip all stuff above 4GB if not AMD64 mode. */
771 if (GCPtr >= _4GB)
772 return 0;
773#endif
774
775 unsigned offPage = GCPtr & PAGE_OFFSET_MASK;
776 unsigned iPage = 0;
777 while (iPage < pCur->cPages)
778 {
779#if PGM_GST_TYPE == PGM_TYPE_32BIT
780 X86PDE Pde = pPDSrc->a[GCPtr >> X86_PD_SHIFT];
781#elif PGM_GST_TYPE == PGM_TYPE_PAE
782 X86PDEPAE Pde;
783 Pde.u = pgmGstGetPaePDE(&pState->pVM->pgm.s, GCPtr);
784#elif PGM_GST_TYPE == PGM_TYPE_AMD64
785 X86PDEPAE Pde;
786 Pde.u = pgmGstGetLongModePDE(&pState->pVM->pgm.s, GCPtr);
787#endif
788 if (Pde.n.u1Present)
789 {
790 if ( !Pde.b.u1Size
791# if PGM_GST_TYPE != PGM_TYPE_AMD64
792 || !(pState->cr4 & X86_CR4_PSE)
793# endif
794 )
795 {
796 /*
797 * Normal page table.
798 */
799 PGSTPT pPT;
800 int rc = PGM_GCPHYS_2_PTR(pState->pVM, Pde.u & GST_PDE_PG_MASK, &pPT);
801 if (VBOX_SUCCESS(rc))
802 {
803 for (unsigned iPTE = (GCPtr >> GST_PT_SHIFT) & GST_PT_MASK;
804 iPTE < RT_ELEMENTS(pPT->a) && iPage < pCur->cPages;
805 iPTE++, iPage++, GCPtr += PAGE_SIZE, offPage = 0)
806 {
807 GSTPTE Pte = pPT->a[iPTE];
808 RTGCPHYS GCPhysNew;
809 if (Pte.n.u1Present)
810 GCPhysNew = (RTGCPHYS)(pPT->a[iPTE].u & GST_PTE_PG_MASK) + offPage;
811 else
812 GCPhysNew = NIL_RTGCPHYS;
813 if (pCur->aPhysToVirt[iPage].Core.Key != GCPhysNew)
814 {
815 if (pCur->aPhysToVirt[iPage].Core.Key != NIL_RTGCPHYS)
816 pgmHandlerVirtualClearPage(&pState->pVM->pgm.s, pCur, iPage);
817#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
818 AssertReleaseMsg(!pCur->aPhysToVirt[iPage].offNextAlias,
819 ("{.Core.Key=%VGp, .Core.KeyLast=%VGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32} GCPhysNew=%VGp\n",
820 pCur->aPhysToVirt[iPage].Core.Key, pCur->aPhysToVirt[iPage].Core.KeyLast,
821 pCur->aPhysToVirt[iPage].offVirtHandler, pCur->aPhysToVirt[iPage].offNextAlias, GCPhysNew));
822#endif
823 pCur->aPhysToVirt[iPage].Core.Key = GCPhysNew;
824 pState->fTodo |= PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL;
825 }
826 }
827 }
828 else
829 {
830 /* not-present. */
831 offPage = 0;
832 AssertRC(rc);
833 for (unsigned iPTE = (GCPtr >> GST_PT_SHIFT) & GST_PT_MASK;
834 iPTE < RT_ELEMENTS(pPT->a) && iPage < pCur->cPages;
835 iPTE++, iPage++, GCPtr += PAGE_SIZE)
836 {
837 if (pCur->aPhysToVirt[iPage].Core.Key != NIL_RTGCPHYS)
838 {
839 pgmHandlerVirtualClearPage(&pState->pVM->pgm.s, pCur, iPage);
840#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
841 AssertReleaseMsg(!pCur->aPhysToVirt[iPage].offNextAlias,
842 ("{.Core.Key=%VGp, .Core.KeyLast=%VGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
843 pCur->aPhysToVirt[iPage].Core.Key, pCur->aPhysToVirt[iPage].Core.KeyLast,
844 pCur->aPhysToVirt[iPage].offVirtHandler, pCur->aPhysToVirt[iPage].offNextAlias));
845#endif
846 pCur->aPhysToVirt[iPage].Core.Key = NIL_RTGCPHYS;
847 pState->fTodo |= PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL;
848 }
849 }
850 }
851 }
852 else
853 {
854 /*
855 * 2/4MB page.
856 */
857 RTGCPHYS GCPhys = (RTGCPHYS)(Pde.u & GST_PDE_PG_MASK);
858 for (unsigned i4KB = (GCPtr >> GST_PT_SHIFT) & GST_PT_MASK;
859 i4KB < PAGE_SIZE / sizeof(GSTPDE) && iPage < pCur->cPages;
860 i4KB++, iPage++, GCPtr += PAGE_SIZE, offPage = 0)
861 {
862 RTGCPHYS GCPhysNew = GCPhys + (i4KB << PAGE_SHIFT) + offPage;
863 if (pCur->aPhysToVirt[iPage].Core.Key != GCPhysNew)
864 {
865 if (pCur->aPhysToVirt[iPage].Core.Key != NIL_RTGCPHYS)
866 pgmHandlerVirtualClearPage(&pState->pVM->pgm.s, pCur, iPage);
867#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
868 AssertReleaseMsg(!pCur->aPhysToVirt[iPage].offNextAlias,
869 ("{.Core.Key=%VGp, .Core.KeyLast=%VGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32} GCPhysNew=%VGp\n",
870 pCur->aPhysToVirt[iPage].Core.Key, pCur->aPhysToVirt[iPage].Core.KeyLast,
871 pCur->aPhysToVirt[iPage].offVirtHandler, pCur->aPhysToVirt[iPage].offNextAlias, GCPhysNew));
872#endif
873 pCur->aPhysToVirt[iPage].Core.Key = GCPhysNew;
874 pState->fTodo |= PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL;
875 }
876 }
877 } /* pde type */
878 }
879 else
880 {
881 /* not-present. */
882 for (unsigned cPages = (GST_PT_MASK + 1) - ((GCPtr >> GST_PT_SHIFT) & GST_PT_MASK);
883 cPages && iPage < pCur->cPages;
884 iPage++, GCPtr += PAGE_SIZE)
885 {
886 if (pCur->aPhysToVirt[iPage].Core.Key != NIL_RTGCPHYS)
887 {
888 pgmHandlerVirtualClearPage(&pState->pVM->pgm.s, pCur, iPage);
889 pCur->aPhysToVirt[iPage].Core.Key = NIL_RTGCPHYS;
890 pState->fTodo |= PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL;
891 }
892 }
893 offPage = 0;
894 }
895 } /* for pages in virtual mapping. */
896
897 return 0;
898}
899#endif /* 32BIT, PAE and AMD64 */
900
901
902/**
903 * Updates the virtual page access handlers.
904 *
905 * @returns true if bits were flushed.
906 * @returns false if bits weren't flushed.
907 * @param pVM VM handle.
908 * @param pPDSrc The page directory.
909 * @param cr4 The cr4 register value.
910 */
911PGM_GST_DECL(bool, HandlerVirtualUpdate)(PVM pVM, uint32_t cr4)
912{
913#if PGM_GST_TYPE == PGM_TYPE_32BIT \
914 || PGM_GST_TYPE == PGM_TYPE_PAE \
915 || PGM_GST_TYPE == PGM_TYPE_AMD64
916
917 /** @todo
918 * In theory this is not sufficient: the guest can change a single page in a range with invlpg
919 */
920
921 /*
922 * Resolve any virtual address based access handlers to GC physical addresses.
923 * This should be fairly quick.
924 */
925 PGMHVUSTATE State;
926
927 pgmLock(pVM);
928 STAM_PROFILE_START(&pVM->pgm.s.CTXMID(Stat,SyncCR3HandlerVirtualUpdate), a);
929 State.pVM = pVM;
930 State.fTodo = pVM->pgm.s.fSyncFlags;
931 State.cr4 = cr4;
932 RTAvlroGCPtrDoWithAll(&pVM->pgm.s.CTXSUFF(pTrees)->VirtHandlers, true, PGM_GST_NAME(VirtHandlerUpdateOne), &State);
933 STAM_PROFILE_STOP(&pVM->pgm.s.CTXMID(Stat,SyncCR3HandlerVirtualUpdate), a);
934
935
936 /*
937 * Set / reset bits?
938 */
939 if (State.fTodo & PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL)
940 {
941 STAM_PROFILE_START(&pVM->pgm.s.CTXMID(Stat,SyncCR3HandlerVirtualReset), b);
942 Log(("pgmR3VirtualHandlersUpdate: resets bits\n"));
943 RTAvlroGCPtrDoWithAll(&pVM->pgm.s.CTXSUFF(pTrees)->VirtHandlers, true, pgmHandlerVirtualResetOne, pVM);
944 pVM->pgm.s.fSyncFlags &= ~PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL;
945 STAM_PROFILE_STOP(&pVM->pgm.s.CTXMID(Stat,SyncCR3HandlerVirtualReset), b);
946 }
947 pgmUnlock(pVM);
948
949 return !!(State.fTodo & PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL);
950
951#else /* real / protected */
952 return false;
953#endif
954}
955
956
957#if PGM_GST_TYPE == PGM_TYPE_32BIT && !defined(IN_RING3)
958
959/**
960 * Write access handler for the Guest CR3 page in 32-bit mode.
961 *
962 * This will try interpret the instruction, if failure fail back to the recompiler.
963 * Check if the changed PDEs are marked present and conflicts with our
964 * mappings. If conflict, we'll switch to the host context and resolve it there
965 *
966 * @returns VBox status code (appropritate for trap handling and GC return).
967 * @param pVM VM Handle.
968 * @param uErrorCode CPU Error code.
969 * @param pRegFrame Trap register frame.
970 * @param pvFault The fault address (cr2).
971 * @param GCPhysFault The GC physical address corresponding to pvFault.
972 * @param pvUser User argument.
973 */
974PGM_GST_DECL(int, WriteHandlerCR3)(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser)
975{
976 AssertMsg(!pVM->pgm.s.fMappingsFixed, ("Shouldn't be registered when mappings are fixed!\n"));
977
978 /*
979 * Try interpret the instruction.
980 */
981 uint32_t cb;
982 int rc = EMInterpretInstruction(pVM, pRegFrame, pvFault, &cb);
983 if (VBOX_SUCCESS(rc) && cb)
984 {
985 /*
986 * Check if the modified PDEs are present and mappings.
987 */
988 const RTGCUINTPTR offPD = GCPhysFault & PAGE_OFFSET_MASK;
989 const unsigned iPD1 = offPD / sizeof(X86PDE);
990 const unsigned iPD2 = (offPD + cb - 1) / sizeof(X86PDE);
991
992 Assert(cb > 0 && cb <= 8);
993 Assert(iPD1 < RT_ELEMENTS(pVM->pgm.s.CTXSUFF(pGuestPD)->a)); /// @todo R3/R0 separation.
994 Assert(iPD2 < RT_ELEMENTS(pVM->pgm.s.CTXSUFF(pGuestPD)->a));
995
996#ifdef DEBUG
997 Log(("pgmXXGst32BitWriteHandlerCR3: emulated change to PD %#x addr=%VGv\n", iPD1, iPD1 << X86_PD_SHIFT));
998 if (iPD1 != iPD2)
999 Log(("pgmXXGst32BitWriteHandlerCR3: emulated change to PD %#x addr=%VGv\n", iPD2, iPD2 << X86_PD_SHIFT));
1000#endif
1001
1002 if (!pVM->pgm.s.fMappingsFixed)
1003 {
1004 PX86PD pPDSrc = CTXSUFF(pVM->pgm.s.pGuestPD);
1005 if ( ( pPDSrc->a[iPD1].n.u1Present
1006 && pgmGetMapping(pVM, (RTGCPTR)(iPD1 << X86_PD_SHIFT)) )
1007 || ( iPD1 != iPD2
1008 && pPDSrc->a[iPD2].n.u1Present
1009 && pgmGetMapping(pVM, (RTGCPTR)(iPD2 << X86_PD_SHIFT)) )
1010 )
1011 {
1012 STAM_COUNTER_INC(&pVM->pgm.s.StatGCGuestCR3WriteConflict);
1013 VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3);
1014 if (rc == VINF_SUCCESS)
1015 rc = VINF_PGM_SYNC_CR3;
1016 Log(("pgmXXGst32BitWriteHandlerCR3: detected conflict iPD1=%#x iPD2=%#x - returns %Rrc\n", iPD1, iPD2, rc));
1017 return rc;
1018 }
1019 }
1020
1021 STAM_COUNTER_INC(&pVM->pgm.s.StatGCGuestCR3WriteHandled);
1022 }
1023 else
1024 {
1025 Assert(VBOX_FAILURE(rc));
1026 if (rc == VERR_EM_INTERPRETER)
1027 rc = VINF_EM_RAW_EMULATE_INSTR_PD_FAULT;
1028 Log(("pgmXXGst32BitWriteHandlerCR3: returns %Rrc\n", rc));
1029 STAM_COUNTER_INC(&pVM->pgm.s.StatGCGuestCR3WriteUnhandled);
1030 }
1031 return rc;
1032}
1033
1034#endif /* PGM_TYPE_32BIT && !IN_RING3 */
1035
1036
1037#if PGM_GST_TYPE == PGM_TYPE_PAE && !defined(IN_RING3)
1038
1039/**
1040 * Write access handler for the Guest CR3 page in PAE mode.
1041 *
1042 * This will try interpret the instruction, if failure fail back to the recompiler.
1043 * Check if the changed PDEs are marked present and conflicts with our
1044 * mappings. If conflict, we'll switch to the host context and resolve it there
1045 *
1046 * @returns VBox status code (appropritate for trap handling and GC return).
1047 * @param pVM VM Handle.
1048 * @param uErrorCode CPU Error code.
1049 * @param pRegFrame Trap register frame.
1050 * @param pvFault The fault address (cr2).
1051 * @param GCPhysFault The GC physical address corresponding to pvFault.
1052 * @param pvUser User argument.
1053 */
1054PGM_GST_DECL(int, WriteHandlerCR3)(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser)
1055{
1056 AssertMsg(!pVM->pgm.s.fMappingsFixed, ("Shouldn't be registered when mappings are fixed!\n"));
1057
1058 /*
1059 * Try interpret the instruction.
1060 */
1061 uint32_t cb;
1062 int rc = EMInterpretInstruction(pVM, pRegFrame, pvFault, &cb);
1063 if (VBOX_SUCCESS(rc) && cb)
1064 {
1065 /*
1066 * Check if any of the PDs have changed.
1067 * We'll simply check all of them instead of figuring out which one/two to check.
1068 */
1069 for (unsigned i = 0; i < X86_PG_PAE_PDPE_ENTRIES; i++)
1070 {
1071 if ( CTXSUFF(pVM->pgm.s.pGstPaePDPT)->a[i].n.u1Present
1072 && ( CTXSUFF(pVM->pgm.s.pGstPaePDPT)->a[i].u & X86_PDPE_PG_MASK)
1073 != pVM->pgm.s.aGCPhysGstPaePDsMonitored[i])
1074 {
1075 /*
1076 * The PDPE has changed.
1077 * We will schedule a monitoring update for the next TLB Flush,
1078 * InvalidatePage or SyncCR3.
1079 *
1080 * This isn't perfect, because a lazy page sync might be dealing with an half
1081 * updated PDPE. However, we assume that the guest OS is disabling interrupts
1082 * and being extremely careful (cmpxchg8b) when updating a PDPE where it's
1083 * executing.
1084 */
1085 pVM->pgm.s.fSyncFlags |= PGM_SYNC_MONITOR_CR3;
1086 Log(("pgmXXGstPaeWriteHandlerCR3: detected updated PDPE; [%d] = %#llx, Old GCPhys=%VGp\n",
1087 i, CTXSUFF(pVM->pgm.s.pGstPaePDPT)->a[i].u, pVM->pgm.s.aGCPhysGstPaePDsMonitored[i]));
1088 }
1089 }
1090
1091 STAM_COUNTER_INC(&pVM->pgm.s.StatGCGuestCR3WriteHandled);
1092 }
1093 else
1094 {
1095 Assert(VBOX_FAILURE(rc));
1096 STAM_COUNTER_INC(&pVM->pgm.s.StatGCGuestCR3WriteUnhandled);
1097 if (rc == VERR_EM_INTERPRETER)
1098 rc = VINF_EM_RAW_EMULATE_INSTR_PD_FAULT;
1099 }
1100 Log(("pgmXXGstPaeWriteHandlerCR3: returns %Rrc\n", rc));
1101 return rc;
1102}
1103
1104
1105/**
1106 * Write access handler for the Guest PDs in PAE mode.
1107 *
1108 * This will try interpret the instruction, if failure fail back to the recompiler.
1109 * Check if the changed PDEs are marked present and conflicts with our
1110 * mappings. If conflict, we'll switch to the host context and resolve it there
1111 *
1112 * @returns VBox status code (appropritate for trap handling and GC return).
1113 * @param pVM VM Handle.
1114 * @param uErrorCode CPU Error code.
1115 * @param pRegFrame Trap register frame.
1116 * @param pvFault The fault address (cr2).
1117 * @param GCPhysFault The GC physical address corresponding to pvFault.
1118 * @param pvUser User argument.
1119 */
1120PGM_GST_DECL(int, WriteHandlerPD)(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser)
1121{
1122 AssertMsg(!pVM->pgm.s.fMappingsFixed, ("Shouldn't be registered when mappings are fixed!\n"));
1123
1124 /*
1125 * Try interpret the instruction.
1126 */
1127 uint32_t cb;
1128 int rc = EMInterpretInstruction(pVM, pRegFrame, pvFault, &cb);
1129 if (VBOX_SUCCESS(rc) && cb)
1130 {
1131 /*
1132 * Figure out which of the 4 PDs this is.
1133 */
1134 RTGCUINTPTR i;
1135 for (i = 0; i < X86_PG_PAE_PDPE_ENTRIES; i++)
1136 if (CTXSUFF(pVM->pgm.s.pGstPaePDPT)->a[i].u == (GCPhysFault & X86_PTE_PAE_PG_MASK))
1137 {
1138 PX86PDPAE pPDSrc = pgmGstGetPaePD(&pVM->pgm.s, i << X86_PDPT_SHIFT);
1139 const RTGCUINTPTR offPD = GCPhysFault & PAGE_OFFSET_MASK;
1140 const unsigned iPD1 = offPD / sizeof(X86PDEPAE);
1141 const unsigned iPD2 = (offPD + cb - 1) / sizeof(X86PDEPAE);
1142
1143 Assert(cb > 0 && cb <= 8);
1144 Assert(iPD1 < X86_PG_PAE_ENTRIES);
1145 Assert(iPD2 < X86_PG_PAE_ENTRIES);
1146
1147#ifdef DEBUG
1148 Log(("pgmXXGstPaeWriteHandlerPD: emulated change to i=%d iPD1=%#05x (%VGv)\n",
1149 i, iPD1, (i << X86_PDPT_SHIFT) | (iPD1 << X86_PD_PAE_SHIFT)));
1150 if (iPD1 != iPD2)
1151 Log(("pgmXXGstPaeWriteHandlerPD: emulated change to i=%d iPD2=%#05x (%VGv)\n",
1152 i, iPD2, (i << X86_PDPT_SHIFT) | (iPD2 << X86_PD_PAE_SHIFT)));
1153#endif
1154
1155 if (!pVM->pgm.s.fMappingsFixed)
1156 {
1157 if ( ( pPDSrc->a[iPD1].n.u1Present
1158 && pgmGetMapping(pVM, (RTGCPTR)((i << X86_PDPT_SHIFT) | (iPD1 << X86_PD_PAE_SHIFT))) )
1159 || ( iPD1 != iPD2
1160 && pPDSrc->a[iPD2].n.u1Present
1161 && pgmGetMapping(pVM, (RTGCPTR)((i << X86_PDPT_SHIFT) | (iPD2 << X86_PD_PAE_SHIFT))) )
1162 )
1163 {
1164 Log(("pgmXXGstPaeWriteHandlerPD: detected conflict iPD1=%#x iPD2=%#x\n", iPD1, iPD2));
1165 STAM_COUNTER_INC(&pVM->pgm.s.StatGCGuestCR3WriteConflict);
1166 VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3);
1167 return VINF_PGM_SYNC_CR3;
1168 }
1169 }
1170 break; /* ASSUMES no duplicate entries... */
1171 }
1172 Assert(i < 4);
1173
1174 STAM_COUNTER_INC(&pVM->pgm.s.StatGCGuestCR3WriteHandled);
1175 }
1176 else
1177 {
1178 Assert(VBOX_FAILURE(rc));
1179 if (rc == VERR_EM_INTERPRETER)
1180 rc = VINF_EM_RAW_EMULATE_INSTR_PD_FAULT;
1181 else
1182 Log(("pgmXXGst32BitWriteHandlerCR3: returns %Rrc\n", rc));
1183 STAM_COUNTER_INC(&pVM->pgm.s.StatGCGuestCR3WriteUnhandled);
1184 }
1185 return rc;
1186}
1187
1188#endif /* PGM_TYPE_PAE && !IN_RING3 */
1189
Note: See TracBrowser for help on using the repository browser.

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