VirtualBox

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

Last change on this file since 1507 was 23, checked in by vboxsync, 18 years ago

string.h & stdio.h + header cleanups.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 28.8 KB
Line 
1/* $Id: PGMAllGst.h 23 2007-01-15 14:08:28Z vboxsync $ */
2/** @file
3 * VBox - Page Manager, Guest Paging Template - All context code.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung 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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
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_PDE4M_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
45#if PGM_GST_TYPE == PGM_TYPE_32BIT
46# define GSTPT X86PT
47# define PGSTPT PX86PT
48# define GSTPTE X86PTE
49# define PGSTPTE PX86PTE
50# define GSTPD X86PD
51# define PGSTPD PX86PD
52# define GSTPDE X86PDE
53# define PGSTPDE PX86PDE
54# define GST_BIG_PAGE_SIZE X86_PAGE_4M_SIZE
55# define GST_BIG_PAGE_OFFSET_MASK X86_PAGE_4M_OFFSET_MASK
56# define GST_PDE_PG_MASK X86_PDE_PG_MASK
57# define GST_PDE4M_PG_MASK X86_PDE4M_PG_MASK
58# define GST_PD_SHIFT X86_PD_SHIFT
59# define GST_PD_MASK X86_PD_MASK
60# define GST_TOTAL_PD_ENTRIES X86_PG_ENTRIES
61# define GST_PTE_PG_MASK X86_PTE_PG_MASK
62# define GST_PT_SHIFT X86_PT_SHIFT
63# define GST_PT_MASK X86_PT_MASK
64#else
65# define GSTPT X86PTPAE
66# define PGSTPT PX86PTPAE
67# define GSTPTE X86PTEPAE
68# define PGSTPTE PX86PTEPAE
69# define GSTPD X86PDPAE
70# define PGSTPD PX86PDPAE
71# define GSTPDE X86PDEPAE
72# define PGSTPDE PX86PDEPAE
73# define GST_BIG_PAGE_SIZE X86_PAGE_2M_SIZE
74# define GST_BIG_PAGE_OFFSET_MASK X86_PAGE_2M_OFFSET_MASK
75# define GST_PDE_PG_MASK X86_PDE_PAE_PG_MASK
76# define GST_PDE4M_PG_MASK X86_PDE4M_PAE_PG_MASK
77# define GST_PD_SHIFT X86_PD_PAE_SHIFT
78# define GST_PD_MASK X86_PD_PAE_MASK
79# define GST_TOTAL_PD_ENTRIES (X86_PG_PAE_ENTRIES*4)
80# define GST_PTE_PG_MASK X86_PTE_PAE_PG_MASK
81# define GST_PT_SHIFT X86_PT_PAE_SHIFT
82# define GST_PT_MASK X86_PT_PAE_MASK
83#endif
84
85
86/*******************************************************************************
87* Internal Functions *
88*******************************************************************************/
89__BEGIN_DECLS
90PGM_GST_DECL(int, GetPage)(PVM pVM, RTGCUINTPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys);
91PGM_GST_DECL(int, ModifyPage)(PVM pVM, RTGCUINTPTR GCPtr, size_t cb, uint64_t fFlags, uint64_t fMask);
92PGM_GST_DECL(int, GetPDE)(PVM pVM, RTGCUINTPTR GCPtr, PX86PDEPAE pPDE);
93PGM_GST_DECL(int, MapCR3)(PVM pVM, RTGCPHYS GCPhysCR3);
94PGM_GST_DECL(int, UnmapCR3)(PVM pVM);
95PGM_GST_DECL(int, MonitorCR3)(PVM pVM, RTGCPHYS GCPhysCR3);
96PGM_GST_DECL(int, UnmonitorCR3)(PVM pVM);
97PGM_GST_DECL(bool, HandlerVirtualUpdate)(PVM pVM, uint32_t cr4);
98__END_DECLS
99
100
101
102/**
103 * Gets effective Guest OS page information.
104 *
105 * When GCPtr is in a big page, the function will return as if it was a normal
106 * 4KB page. If the need for distinguishing between big and normal page becomes
107 * necessary at a later point, a PGMGstGetPage Ex() will be created for that
108 * purpose.
109 *
110 * @returns VBox status.
111 * @param pVM VM Handle.
112 * @param GCPtr Guest Context virtual address of the page. Page aligned!
113 * @param pfFlags Where to store the flags. These are X86_PTE_*, even for big pages.
114 * @param pGCPhys Where to store the GC physical address of the page.
115 * This is page aligned. The fact that the
116 */
117PGM_GST_DECL(int, GetPage)(PVM pVM, RTGCUINTPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys)
118{
119#if PGM_GST_TYPE == PGM_TYPE_REAL \
120 || PGM_GST_TYPE == PGM_TYPE_PROT
121 /*
122 * Fake it.
123 */
124 if (pfFlags)
125 *pfFlags = X86_PTE_P | X86_PTE_RW | X86_PTE_US;
126 if (pGCPhys)
127 *pGCPhys = GCPtr & PAGE_BASE_GC_MASK;
128 return VINF_SUCCESS;
129
130#elif PGM_GST_TYPE == PGM_TYPE_32BIT \
131 || PGM_GST_TYPE == PGM_TYPE_PAE \
132 || PGM_GST_TYPE == PGM_TYPE_AMD64
133
134#if PGM_GST_TYPE == PGM_TYPE_AMD64
135 /* later */
136 AssertFailed();
137 return VERR_NOT_IMPLEMENTED;
138#endif
139
140
141 /*
142 * Get the PDE.
143 */
144#if PGM_GST_TYPE == PGM_TYPE_32BIT
145 const X86PDE Pde = CTXSUFF(pVM->pgm.s.pGuestPD)->a[GCPtr >> X86_PD_SHIFT];
146#else /* PAE */
147 X86PDEPAE Pde;
148 Pde.u = pgmGstGetPaePDE(&pVM->pgm.s, GCPtr);
149#endif
150
151 /*
152 * Lookup the page.
153 */
154 if (!Pde.n.u1Present)
155 return VERR_PAGE_TABLE_NOT_PRESENT;
156
157 if ( !Pde.b.u1Size
158 || !(CPUMGetGuestCR4(pVM) & X86_CR4_PSE))
159 {
160 PGSTPT pPT;
161 int rc = PGM_GCPHYS_2_PTR(pVM, Pde.u & GST_PDE_PG_MASK, &pPT);
162 if (VBOX_FAILURE(rc))
163 return rc;
164
165 /*
166 * Get PT entry and check presentness.
167 */
168 const GSTPTE Pte = pPT->a[(GCPtr >> GST_PT_SHIFT) & GST_PT_MASK];
169 if (!Pte.n.u1Present)
170 return VERR_PAGE_NOT_PRESENT;
171
172 /*
173 * Store the result.
174 * RW and US flags depend on all levels (bitwise AND) - except for legacy PAE
175 * where the PDPE is simplified.
176 */
177 if (pfFlags)
178 *pfFlags = (Pte.u & ~GST_PTE_PG_MASK)
179 & ((Pde.u & (X86_PTE_RW | X86_PTE_US)) | ~(uint64_t)(X86_PTE_RW | X86_PTE_US));
180 if (pGCPhys)
181 *pGCPhys = Pte.u & GST_PTE_PG_MASK;
182 }
183 else
184 {
185 /*
186 * Map big to 4k PTE and store the result
187 */
188 if (pfFlags)
189 *pfFlags = (Pde.u & ~(GST_PTE_PG_MASK | X86_PTE_PAT))
190 | ((Pde.u & X86_PDE4M_PAT) >> X86_PDE4M_PAT_SHIFT);
191 if (pGCPhys)
192 *pGCPhys = (Pde.u & GST_PDE4M_PG_MASK) | (GCPtr & (~GST_PDE4M_PG_MASK ^ ~GST_PTE_PG_MASK)); /** @todo pse36 */
193 }
194 return VINF_SUCCESS;
195#else
196 /* something else... */
197 return VERR_NOT_SUPPORTED;
198#endif
199}
200
201
202/**
203 * Modify page flags for a range of pages in the guest's tables
204 *
205 * The existing flags are ANDed with the fMask and ORed with the fFlags.
206 *
207 * @returns VBox status code.
208 * @param pVM VM handle.
209 * @param GCPtr Virtual address of the first page in the range. Page aligned!
210 * @param cb Size (in bytes) of the page range to apply the modification to. Page aligned!
211 * @param fFlags The OR mask - page flags X86_PTE_*, excluding the page mask of course.
212 * @param fMask The AND mask - page flags X86_PTE_*.
213 */
214PGM_GST_DECL(int, ModifyPage)(PVM pVM, RTGCUINTPTR GCPtr, size_t cb, uint64_t fFlags, uint64_t fMask)
215{
216#if PGM_GST_TYPE == PGM_TYPE_32BIT \
217 || PGM_GST_TYPE == PGM_TYPE_PAE \
218 || PGM_GST_TYPE == PGM_TYPE_AMD64
219
220#if PGM_GST_TYPE == PGM_TYPE_AMD64
221 /* later */
222 AssertFailed();
223 return VERR_NOT_IMPLEMENTED;
224#endif
225
226 for (;;)
227 {
228 /*
229 * Get the PD entry.
230 */
231#if PGM_GST_TYPE == PGM_TYPE_32BIT
232 PX86PDE pPde = &CTXSUFF(pVM->pgm.s.pGuestPD)->a[GCPtr >> X86_PD_SHIFT];
233#else /* PAE */
234 PX86PDEPAE pPde = pgmGstGetPaePDEPtr(&pVM->pgm.s, GCPtr);
235 Assert(pPde);
236 if (!pPde)
237 return VERR_PAGE_TABLE_NOT_PRESENT;
238#endif
239 GSTPDE Pde = *pPde;
240 Assert(Pde.n.u1Present);
241 if (!Pde.n.u1Present)
242 return VERR_PAGE_TABLE_NOT_PRESENT;
243
244 if ( !Pde.b.u1Size
245 || !(CPUMGetGuestCR4(pVM) & X86_CR4_PSE))
246 {
247 /*
248 * 4KB Page table
249 *
250 * Walk page tables and pages till we're done.
251 */
252 PGSTPT pPT;
253 int rc = PGM_GCPHYS_2_PTR(pVM, Pde.u & GST_PDE_PG_MASK, &pPT);
254 if (VBOX_FAILURE(rc))
255 return rc;
256
257 unsigned iPTE = (GCPtr >> GST_PT_SHIFT) & GST_PT_MASK;
258 while (iPTE < ELEMENTS(pPT->a))
259 {
260 GSTPTE Pte = pPT->a[iPTE];
261 Pte.u = (Pte.u & (fMask | X86_PTE_PAE_PG_MASK))
262 | (fFlags & ~GST_PTE_PG_MASK);
263 pPT->a[iPTE] = Pte;
264
265 /* next page */
266 cb -= PAGE_SIZE;
267 if (!cb)
268 return VINF_SUCCESS;
269 GCPtr += PAGE_SIZE;
270 iPTE++;
271 }
272 }
273 else
274 {
275 /*
276 * 4MB Page table
277 */
278 Pde.u = (Pde.u & (fMask | ((fMask & X86_PTE_PAT) << X86_PDE4M_PAT_SHIFT) | X86_PDE4M_PAE_PG_MASK | X86_PDE4M_PS)) /** @todo pse36 */
279 | (fFlags & ~GST_PTE_PG_MASK)
280 | ((fFlags & X86_PTE_PAT) << X86_PDE4M_PAT_SHIFT);
281 *pPde = Pde;
282
283 /* advance */
284 const unsigned cbDone = GST_BIG_PAGE_SIZE - (GCPtr & GST_BIG_PAGE_OFFSET_MASK);
285 if (cbDone >= cb)
286 return VINF_SUCCESS;
287 cb -= cbDone;
288 GCPtr += cbDone;
289 }
290 }
291
292#else
293 /* real / protected mode. */
294 AssertFailed();
295 return VERR_NOT_SUPPORTED;
296#endif
297}
298
299
300/**
301 * Retrieve guest PDE information
302 *
303 * @returns VBox status code.
304 * @param pVM The virtual machine.
305 * @param GCPtr Guest context pointer
306 * @param pPDE Pointer to guest PDE structure
307 */
308PGM_GST_DECL(int, GetPDE)(PVM pVM, RTGCUINTPTR GCPtr, PX86PDEPAE pPDE)
309{
310#if PGM_GST_TYPE == PGM_TYPE_32BIT \
311 || PGM_GST_TYPE == PGM_TYPE_PAE \
312 || PGM_GST_TYPE == PGM_TYPE_AMD64
313
314#if PGM_GST_TYPE == PGM_TYPE_AMD64
315 /* later */
316 AssertFailed();
317 return VERR_NOT_IMPLEMENTED;
318#endif
319
320# if PGM_GST_TYPE == PGM_TYPE_32BIT
321 X86PDE Pde;
322 Pde = CTXSUFF(pVM->pgm.s.pGuestPD)->a[GCPtr >> GST_PD_SHIFT];
323# else
324 X86PDEPAE Pde;
325 Pde.u = pgmGstGetPaePDE(&pVM->pgm.s, GCPtr);
326# endif
327
328 pPDE->u = (X86PGPAEUINT)Pde.u;
329 return VINF_SUCCESS;
330#else
331 AssertFailed();
332 return VERR_NOT_IMPLEMENTED;
333#endif
334}
335
336
337
338/**
339 * Maps the CR3 into HMA in GC and locate it in HC.
340 *
341 * @returns VBox status, no specials.
342 * @param pVM VM handle.
343 * @param GCPhysCR3 The physical address in the CR3 register.
344 */
345PGM_GST_DECL(int, MapCR3)(PVM pVM, RTGCPHYS GCPhysCR3)
346{
347#if PGM_GST_TYPE == PGM_TYPE_32BIT \
348 || PGM_GST_TYPE == PGM_TYPE_PAE \
349 || PGM_GST_TYPE == PGM_TYPE_AMD64
350 /*
351 * Map the page CR3 points at.
352 */
353 RTHCPHYS HCPhysGuestCR3;
354 RTHCPTR HCPtrGuestCR3;
355 int rc = PGMRamGCPhys2HCPtrAndHCPhysWithFlags(&pVM->pgm.s, GCPhysCR3, &HCPtrGuestCR3, &HCPhysGuestCR3);
356 if (VBOX_SUCCESS(rc))
357 {
358 rc = PGMMap(pVM, (RTGCUINTPTR)pVM->pgm.s.GCPtrCR3Mapping, HCPhysGuestCR3 & X86_PTE_PAE_PG_MASK, PAGE_SIZE, 0);
359 if (VBOX_SUCCESS(rc))
360 {
361 PGM_INVL_PG(pVM->pgm.s.GCPtrCR3Mapping);
362#if PGM_GST_TYPE == PGM_TYPE_32BIT
363 pVM->pgm.s.pGuestPDHC = (HCPTRTYPE(PX86PD))HCPtrGuestCR3;
364 pVM->pgm.s.pGuestPDGC = (GCPTRTYPE(PX86PD))pVM->pgm.s.GCPtrCR3Mapping;
365
366#elif PGM_GST_TYPE == PGM_TYPE_PAE
367 const unsigned off = GCPhysCR3 & X86_CR3_PAE_PAGE_MASK;
368 pVM->pgm.s.pGstPaePDPTRHC = (HCPTRTYPE(PX86PDPTR))((RTHCUINTPTR)HCPtrGuestCR3 | off);
369 pVM->pgm.s.pGstPaePDPTRGC = (GCPTRTYPE(PX86PDPTR))((RTGCUINTPTR)pVM->pgm.s.GCPtrCR3Mapping | off);
370
371 /*
372 * Map the 4 PDs too.
373 */
374 RTGCUINTPTR GCPtr = (RTGCUINTPTR)pVM->pgm.s.GCPtrCR3Mapping + PAGE_SIZE;
375 for (unsigned i = 0; i < 4; i++, GCPtr += PAGE_SIZE)
376 {
377 if (pVM->pgm.s.CTXSUFF(pGstPaePDPTR)->a[i].n.u1Present)
378 {
379 RTHCPTR HCPtr;
380 RTHCPHYS HCPhys;
381 RTGCPHYS GCPhys = pVM->pgm.s.CTXSUFF(pGstPaePDPTR)->a[i].u & X86_PDPE_PG_MASK;
382 int rc2 = PGMRamGCPhys2HCPtrAndHCPhysWithFlags(&pVM->pgm.s, GCPhys, &HCPtr, &HCPhys);
383 if (VBOX_SUCCESS(rc2))
384 {
385 rc = PGMMap(pVM, GCPtr, HCPhys & X86_PTE_PAE_PG_MASK, PAGE_SIZE, 0);
386 AssertRCReturn(rc, rc);
387 pVM->pgm.s.apGstPaePDsHC[i] = (HCPTRTYPE(PX86PDPAE))HCPtr;
388 pVM->pgm.s.apGstPaePDsGC[i] = (GCPTRTYPE(PX86PDPAE))GCPtr;
389 pVM->pgm.s.aGCPhysGstPaePDs[i] = GCPhys;
390 PGM_INVL_PG(GCPtr);
391 continue;
392 }
393 AssertMsgFailed(("pgmR3Gst32BitMapCR3: rc2=%d GCPhys=%RGp i=%d\n", rc2, GCPhys, i));
394 }
395
396 pVM->pgm.s.apGstPaePDsHC[i] = 0;
397 pVM->pgm.s.apGstPaePDsGC[i] = 0;
398 pVM->pgm.s.aGCPhysGstPaePDs[i] = NIL_RTGCPHYS;
399 PGM_INVL_PG(GCPtr);
400 }
401
402#else /* PGM_GST_TYPE == PGM_TYPE_AMD64 */
403 rc = VERR_NOT_IMPLEMENTED;
404#endif
405 }
406 }
407 else
408 AssertMsgFailed(("rc=%Vrc GCPhysGuestPD=%VGp\n", rc, GCPhysCR3));
409
410#else /* prot/real mode stub */
411 int rc = VINF_SUCCESS;
412#endif
413 return rc;
414}
415
416
417/**
418 * Unmaps the CR3.
419 *
420 * @returns VBox status, no specials.
421 * @param pVM VM handle.
422 * @param GCPhysCR3 The physical address in the CR3 register.
423 */
424PGM_GST_DECL(int, UnmapCR3)(PVM pVM)
425{
426 int rc = VINF_SUCCESS;
427#if PGM_GST_TYPE == PGM_TYPE_32BIT
428 pVM->pgm.s.pGuestPDHC = 0;
429 pVM->pgm.s.pGuestPDGC = 0;
430
431#elif PGM_GST_TYPE == PGM_TYPE_PAE
432 pVM->pgm.s.pGstPaePDPTRHC = 0;
433 pVM->pgm.s.pGstPaePDPTRGC = 0;
434
435#elif PGM_GST_TYPE == PGM_TYPE_AMD64
436//#error not implemented
437 rc = VERR_NOT_IMPLEMENTED;
438
439#else /* prot/real mode stub */
440 /* nothing to do */
441#endif
442 return rc;
443}
444
445
446#undef LOG_GROUP
447#define LOG_GROUP LOG_GROUP_PGM_POOL
448
449/**
450 * Registers physical page monitors for the necessary paging
451 * structures to detect conflicts with our guest mappings.
452 *
453 * This is always called after mapping CR3.
454 * This is never called with fixed mappings.
455 *
456 * @returns VBox status, no specials.
457 * @param pVM VM handle.
458 * @param GCPhysCR3 The physical address in the CR3 register.
459 */
460PGM_GST_DECL(int, MonitorCR3)(PVM pVM, RTGCPHYS GCPhysCR3)
461{
462 Assert(!pVM->pgm.s.fMappingsFixed);
463 int rc = VINF_SUCCESS;
464
465#if PGM_GST_TYPE == PGM_TYPE_32BIT \
466 || PGM_GST_TYPE == PGM_TYPE_PAE \
467 || PGM_GST_TYPE == PGM_TYPE_AMD64
468
469 /*
470 * Register/Modify write phys handler for guest's CR3 if it changed.
471 */
472 if (pVM->pgm.s.GCPhysGstCR3Monitored != GCPhysCR3)
473 {
474# ifndef PGMPOOL_WITH_MIXED_PT_CR3
475 const unsigned cbCR3Stuff = PGM_GST_TYPE == PGM_TYPE_PAE ? 32 : PAGE_SIZE;
476 if (pVM->pgm.s.GCPhysGstCR3Monitored != NIL_RTGCPHYS)
477 rc = PGMHandlerPhysicalModify(pVM, pVM->pgm.s.GCPhysGstCR3Monitored, GCPhysCR3, GCPhysCR3 + cbCR3Stuff - 1);
478 else
479 rc = PGMHandlerPhysicalRegisterEx(pVM, PGMPHYSHANDLERTYPE_PHYSICAL_WRITE, GCPhysCR3, GCPhysCR3 + cbCR3Stuff - 1,
480 pVM->pgm.s.pfnHCGstWriteHandlerCR3, 0,
481 pVM->pgm.s.pfnR0GstWriteHandlerCR3, 0,
482 pVM->pgm.s.pfnGCGstWriteHandlerCR3, 0,
483 pVM->pgm.s.pszHCGstWriteHandlerCR3);
484# else /* PGMPOOL_WITH_MIXED_PT_CR3 */
485 rc = pgmPoolMonitorMonitorCR3(pVM->pgm.s.CTXSUFF(pPool),
486 pVM->pgm.s.enmShadowMode == PGMMODE_PAE
487 || pVM->pgm.s.enmShadowMode == PGMMODE_PAE_NX
488 ? PGMPOOL_IDX_PAE_PD
489 : PGMPOOL_IDX_PD,
490 GCPhysCR3);
491# endif /* PGMPOOL_WITH_MIXED_PT_CR3 */
492 if (VBOX_FAILURE(rc))
493 {
494 AssertMsgFailed(("PGMHandlerPhysicalModify/PGMR3HandlerPhysicalRegister failed, rc=%Rrc GCPhysGstCR3Monitored=%RGp GCPhysCR3=%RGp\n",
495 rc, pVM->pgm.s.GCPhysGstCR3Monitored, GCPhysCR3));
496 return rc;
497 }
498 pVM->pgm.s.GCPhysGstCR3Monitored = GCPhysCR3;
499 }
500
501#if PGM_GST_TYPE == PGM_TYPE_PAE
502 AssertFatalFailed();
503# if 0 /* later */
504 /*
505 * Do the 4 PDs.
506 */
507 for (unsigned i = 0; i < 4; i++)
508 {
509 if (pVM->pgm.s.pGstPaePDPTRHC->a[i].n.u1Present)
510 {
511 RTGCPHYS GCPhys = pVM->pgm.s.pGstPaePDPTRHC->a[i].u & X86_PDPE_PG_MASK;
512 if (pVM->pgm.s.aGCPhysGstPaePDsMonitored[i] != GCPhys)
513 {
514 if (pVM->pgm.s.aGCPhysGstPaePDsMonitored[i] != NIL_RTGCPHYS)
515 rc = PGMHandlerPhysicalModify(pVM, pVM->pgm.s.aGCPhysGstPaePDsMonitored[i], GCPhys, GCPhys + PAGE_SIZE - 1);
516 else
517 rc = PGMR3HandlerPhysicalRegister(pVM, PGMPHYSHANDLERTYPE_PHYSICAL_WRITE, GCPhys, GCPhys + PAGE_SIZE - 1,
518 pgmR3GstPaePDWriteHandler, NULL,
519 NULL, "pgmGCGstPaePDWriteHandler", 0,
520 "Guest PD write access handler");
521 if (VBOX_SUCCESS(rc))
522 pVM->pgm.s.aGCPhysGstPaePDsMonitored[i] = GCPhys;
523 }
524 }
525 else if (pVM->pgm.s.aGCPhysGstPaePDsMonitored[i] != NIL_RTGCPHYS)
526 {
527 rc = PGMHandlerPhysicalDeregister(pVM, pVM->pgm.s.aGCPhysGstPaePDsMonitored[i]);
528 AssertRC(rc);
529 pVM->pgm.s.aGCPhysGstPaePDsMonitored[i] = NIL_RTGCPHYS;
530 }
531 }
532# endif
533#endif /* PGM_GST_TYPE == PGM_TYPE_PAE */
534
535#else
536 /* prot/real mode stub */
537
538#endif
539 return rc;
540}
541
542/**
543 * Deregisters any physical page monitors installed by MonitorCR3.
544 *
545 * @returns VBox status code, no specials.
546 * @param pVM The VM handle.
547 */
548PGM_GST_DECL(int, UnmonitorCR3)(PVM pVM)
549{
550 int rc = VINF_SUCCESS;
551
552#if PGM_GST_TYPE == PGM_TYPE_32BIT \
553 || PGM_GST_TYPE == PGM_TYPE_PAE \
554 || PGM_GST_TYPE == PGM_TYPE_AMD64
555
556 /*
557 * Deregister the access handlers.
558 *
559 * PGMSyncCR3 will reinstall it if required and PGMSyncCR3 will be executed
560 * before we enter GC again.
561 */
562 if (pVM->pgm.s.GCPhysGstCR3Monitored != NIL_RTGCPHYS)
563 {
564# ifndef PGMPOOL_WITH_MIXED_PT_CR3
565 rc = PGMHandlerPhysicalDeregister(pVM, pVM->pgm.s.GCPhysGstCR3Monitored);
566 AssertRCReturn(rc, rc);
567# else /* PGMPOOL_WITH_MIXED_PT_CR3 */
568 rc = pgmPoolMonitorUnmonitorCR3(pVM->pgm.s.CTXSUFF(pPool),
569 pVM->pgm.s.enmShadowMode == PGMMODE_PAE
570 || pVM->pgm.s.enmShadowMode == PGMMODE_PAE_NX
571 ? PGMPOOL_IDX_PAE_PD
572 : PGMPOOL_IDX_PD);
573 AssertRCReturn(rc, rc);
574# endif /* PGMPOOL_WITH_MIXED_PT_CR3 */
575 pVM->pgm.s.GCPhysGstCR3Monitored = NIL_RTGCPHYS;
576 }
577
578# if PGM_GST_TYPE == PGM_TYPE_PAE
579 /* The 4 PDs. */
580 for (unsigned i = 0; i < 4; i++)
581 if (pVM->pgm.s.aGCPhysGstPaePDsMonitored[i] != NIL_RTGCPHYS)
582 {
583 int rc2 = PGMHandlerPhysicalDeregister(pVM, pVM->pgm.s.aGCPhysGstPaePDsMonitored[i]);
584 AssertRC(rc2);
585 if (VBOX_FAILURE(rc2))
586 rc = rc2;
587 pVM->pgm.s.aGCPhysGstPaePDsMonitored[i] = NIL_RTGCPHYS;
588 }
589# endif
590
591#else
592 /* prot/real mode stub */
593#endif
594 return rc;
595
596}
597
598#undef LOG_GROUP
599#define LOG_GROUP LOG_GROUP_PGM
600
601
602#if PGM_GST_TYPE == PGM_TYPE_32BIT \
603 || PGM_GST_TYPE == PGM_TYPE_PAE \
604 || PGM_GST_TYPE == PGM_TYPE_AMD64
605/**
606 * Updates one virtual handler range.
607 *
608 * @returns 0
609 * @param pNode Pointer to a PGMVIRTHANDLER.
610 * @param pvUser Pointer to a PGMVHUARGS structure (see PGM.cpp).
611 */
612static DECLCALLBACK(int) PGM_GST_NAME(VirtHandlerUpdateOne)(PAVLROGCPTRNODECORE pNode, void *pvUser)
613{
614 PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)pNode;
615 PPGMHVUSTATE pState = (PPGMHVUSTATE)pvUser;
616
617#if PGM_GST_TYPE == PGM_TYPE_32BIT
618 PX86PD pPDSrc = pState->pVM->pgm.s.CTXSUFF(pGuestPD);
619#endif
620
621 RTGCUINTPTR GCPtr = (RTUINTPTR)pCur->GCPtr;
622#if PGM_GST_MODE != PGM_MODE_AMD64
623 /* skip all stuff above 4GB if not AMD64 mode. */
624 if (GCPtr >= _4GB)
625 return 0;
626#endif
627
628 unsigned fFlags;
629 switch (pCur->enmType)
630 {
631 case PGMVIRTHANDLERTYPE_EIP:
632 case PGMVIRTHANDLERTYPE_NORMAL: fFlags = MM_RAM_FLAGS_VIRTUAL_HANDLER; break;
633 case PGMVIRTHANDLERTYPE_WRITE: fFlags = MM_RAM_FLAGS_VIRTUAL_HANDLER | MM_RAM_FLAGS_VIRTUAL_WRITE; break;
634 case PGMVIRTHANDLERTYPE_ALL: fFlags = MM_RAM_FLAGS_VIRTUAL_HANDLER | MM_RAM_FLAGS_VIRTUAL_ALL; break;
635 /* hypervisor handlers need no flags and wouldn't have nowhere to put them in any case. */
636 case PGMVIRTHANDLERTYPE_HYPERVISOR:
637 return 0;
638 }
639
640 unsigned offPage = GCPtr & PAGE_OFFSET_MASK;
641 unsigned iPage = 0;
642 while (iPage < pCur->cPages)
643 {
644#if PGM_GST_TYPE == PGM_TYPE_32BIT
645 X86PDE Pde = pPDSrc->a[GCPtr >> X86_PD_SHIFT];
646#else
647 X86PDEPAE Pde;
648 Pde.u = pgmGstGetPaePDE(&pState->pVM->pgm.s, GCPtr);
649#endif
650 if (Pde.n.u1Present)
651 {
652 if (!Pde.b.u1Size || !(pState->cr4 & X86_CR4_PSE))
653 {
654 /*
655 * Normal page table.
656 */
657 PGSTPT pPT;
658 int rc = PGM_GCPHYS_2_PTR(pState->pVM, Pde.u & GST_PDE_PG_MASK, &pPT);
659 if (VBOX_SUCCESS(rc))
660 {
661 for (unsigned iPTE = (GCPtr >> GST_PT_SHIFT) & GST_PT_MASK;
662 iPTE < ELEMENTS(pPT->a) && iPage < pCur->cPages;
663 iPTE++, iPage++, GCPtr += PAGE_SIZE, offPage = 0)
664 {
665 GSTPTE Pte = pPT->a[iPTE];
666 RTGCPHYS GCPhysNew;
667 if (Pte.n.u1Present)
668 GCPhysNew = (RTGCPHYS)(pPT->a[iPTE].u & GST_PTE_PG_MASK) + offPage;
669 else
670 GCPhysNew = NIL_RTGCPHYS;
671 if (pCur->aPhysToVirt[iPage].Core.Key != GCPhysNew)
672 {
673 if (pCur->aPhysToVirt[iPage].Core.Key != NIL_RTGCPHYS)
674 pgmHandlerVirtualClearPage(&pState->pVM->pgm.s, pCur, iPage);
675#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
676 AssertReleaseMsg(!pCur->aPhysToVirt[iPage].offNextAlias,
677 ("{.Core.Key=%VGp, .Core.KeyLast=%VGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32} GCPhysNew=%VGp\n",
678 pCur->aPhysToVirt[iPage].Core.Key, pCur->aPhysToVirt[iPage].Core.KeyLast,
679 pCur->aPhysToVirt[iPage].offVirtHandler, pCur->aPhysToVirt[iPage].offNextAlias, GCPhysNew));
680#endif
681 pCur->aPhysToVirt[iPage].Core.Key = GCPhysNew;
682 pState->fTodo |= PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL;
683 }
684 }
685 }
686 else
687 {
688 /* not-present. */
689 offPage = 0;
690 AssertRC(rc);
691 for (unsigned iPTE = (GCPtr >> GST_PT_SHIFT) & GST_PT_MASK;
692 iPTE < ELEMENTS(pPT->a) && iPage < pCur->cPages;
693 iPTE++, iPage++, GCPtr += PAGE_SIZE)
694 {
695 if (pCur->aPhysToVirt[iPage].Core.Key != NIL_RTGCPHYS)
696 {
697 pgmHandlerVirtualClearPage(&pState->pVM->pgm.s, pCur, iPage);
698#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
699 AssertReleaseMsg(!pCur->aPhysToVirt[iPage].offNextAlias,
700 ("{.Core.Key=%VGp, .Core.KeyLast=%VGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
701 pCur->aPhysToVirt[iPage].Core.Key, pCur->aPhysToVirt[iPage].Core.KeyLast,
702 pCur->aPhysToVirt[iPage].offVirtHandler, pCur->aPhysToVirt[iPage].offNextAlias));
703#endif
704 pCur->aPhysToVirt[iPage].Core.Key = NIL_RTGCPHYS;
705 pState->fTodo |= PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL;
706 }
707 }
708 }
709 }
710 else
711 {
712 /*
713 * 2/4MB page.
714 */
715 RTGCPHYS GCPhys = (RTGCPHYS)(Pde.u & GST_PDE_PG_MASK);
716 for (unsigned i4KB = (GCPtr >> GST_PT_SHIFT) & GST_PT_MASK;
717 i4KB < PAGE_SIZE / sizeof(GSTPDE) && iPage < pCur->cPages;
718 i4KB++, iPage++, GCPtr += PAGE_SIZE, offPage = 0)
719 {
720 RTGCPHYS GCPhysNew = GCPhys + (i4KB << PAGE_SHIFT) + offPage;
721 if (pCur->aPhysToVirt[iPage].Core.Key != GCPhysNew)
722 {
723 if (pCur->aPhysToVirt[iPage].Core.Key != NIL_RTGCPHYS)
724 pgmHandlerVirtualClearPage(&pState->pVM->pgm.s, pCur, iPage);
725#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
726 AssertReleaseMsg(!pCur->aPhysToVirt[iPage].offNextAlias,
727 ("{.Core.Key=%VGp, .Core.KeyLast=%VGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32} GCPhysNew=%VGp\n",
728 pCur->aPhysToVirt[iPage].Core.Key, pCur->aPhysToVirt[iPage].Core.KeyLast,
729 pCur->aPhysToVirt[iPage].offVirtHandler, pCur->aPhysToVirt[iPage].offNextAlias, GCPhysNew));
730#endif
731 pCur->aPhysToVirt[iPage].Core.Key = GCPhysNew;
732 pState->fTodo |= PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL;
733 }
734 }
735 } /* pde type */
736 }
737 else
738 {
739 /* not-present. */
740 for (unsigned cPages = (GST_PT_MASK + 1) - ((GCPtr >> GST_PT_SHIFT) & GST_PT_MASK);
741 cPages && iPage < pCur->cPages;
742 iPage++, GCPtr += PAGE_SIZE)
743 {
744 if (pCur->aPhysToVirt[iPage].Core.Key != NIL_RTGCPHYS)
745 {
746 pgmHandlerVirtualClearPage(&pState->pVM->pgm.s, pCur, iPage);
747 pCur->aPhysToVirt[iPage].Core.Key = NIL_RTGCPHYS;
748 pState->fTodo |= PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL;
749 }
750 }
751 offPage = 0;
752 }
753 } /* for pages in virtual mapping. */
754
755 return 0;
756}
757#endif /* 32BIT, PAE and AMD64 */
758
759
760/**
761 * Updates the virtual page access handlers.
762 *
763 * @returns true if bits were flushed.
764 * @returns false if bits weren't flushed.
765 * @param pVM VM handle.
766 * @param pPDSrc The page directory.
767 * @param cr4 The cr4 register value.
768 */
769PGM_GST_DECL(bool, HandlerVirtualUpdate)(PVM pVM, uint32_t cr4)
770{
771#if PGM_GST_TYPE == PGM_TYPE_32BIT \
772 || PGM_GST_TYPE == PGM_TYPE_PAE \
773 || PGM_GST_TYPE == PGM_TYPE_AMD64
774#if PGM_GST_TYPE == PGM_TYPE_AMD64
775 AssertFailed();
776#endif
777
778 /** @todo
779 * In theory this is not sufficient: the guest can change a single page in a range with invlpg
780 */
781
782 /*
783 * Resolve any virtual address based access handlers to GC physical addresses.
784 * This should be fairly quick.
785 */
786 PGMHVUSTATE State;
787
788 pgmLock(pVM);
789 STAM_PROFILE_START(&pVM->pgm.s.CTXMID(Stat,SyncCR3HandlerVirtualUpdate), a);
790 State.pVM = pVM;
791 State.fTodo = pVM->pgm.s.fSyncFlags;
792 State.cr4 = cr4;
793 RTAvlroGCPtrDoWithAll(&pVM->pgm.s.CTXSUFF(pTrees)->VirtHandlers, true, PGM_GST_NAME(VirtHandlerUpdateOne), &State);
794 STAM_PROFILE_STOP(&pVM->pgm.s.CTXMID(Stat,SyncCR3HandlerVirtualUpdate), a);
795
796
797 /*
798 * Set / reset bits?
799 */
800 if (State.fTodo & PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL)
801 {
802 STAM_PROFILE_START(&pVM->pgm.s.CTXMID(Stat,SyncCR3HandlerVirtualReset), b);
803 Log(("pgmR3VirtualHandlersUpdate: resets bits\n"));
804 RTAvlroGCPtrDoWithAll(&pVM->pgm.s.CTXSUFF(pTrees)->VirtHandlers, true, pgmHandlerVirtualResetOne, pVM);
805 pVM->pgm.s.fSyncFlags &= ~PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL;
806 STAM_PROFILE_STOP(&pVM->pgm.s.CTXMID(Stat,SyncCR3HandlerVirtualReset), b);
807 }
808 pgmUnlock(pVM);
809
810 return !!(State.fTodo & PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL);
811
812#else /* real / protected */
813 return false;
814#endif
815}
816
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