VirtualBox

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

Last change on this file since 6912 was 6912, checked in by vboxsync, 17 years ago

Removed the normal (PGMVIRTHANDLERTYPE_NORMAL) kind of virtual access handlers. This type have never been used and is just complicating the code. It can easily be re-added later if found to be useful.

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