VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/PGMAll.cpp@ 23011

Last change on this file since 23011 was 22890, checked in by vboxsync, 15 years ago

VM::cCPUs -> VM::cCpus so it matches all the other cCpus and aCpus members.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 82.4 KB
Line 
1/* $Id: PGMAll.cpp 22890 2009-09-09 23:11:31Z vboxsync $ */
2/** @file
3 * PGM - Page Manager and Monitor - 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* Header Files *
24*******************************************************************************/
25#define LOG_GROUP LOG_GROUP_PGM
26#include <VBox/pgm.h>
27#include <VBox/cpum.h>
28#include <VBox/selm.h>
29#include <VBox/iom.h>
30#include <VBox/sup.h>
31#include <VBox/mm.h>
32#include <VBox/stam.h>
33#include <VBox/csam.h>
34#include <VBox/patm.h>
35#include <VBox/trpm.h>
36#include <VBox/rem.h>
37#include <VBox/em.h>
38#include <VBox/hwaccm.h>
39#include <VBox/hwacc_vmx.h>
40#include "PGMInternal.h"
41#include <VBox/vm.h>
42#include <iprt/assert.h>
43#include <iprt/asm.h>
44#include <iprt/string.h>
45#include <VBox/log.h>
46#include <VBox/param.h>
47#include <VBox/err.h>
48
49
50/*******************************************************************************
51* Structures and Typedefs *
52*******************************************************************************/
53/**
54 * Stated structure for PGM_GST_NAME(HandlerVirtualUpdate) that's
55 * passed to PGM_GST_NAME(VirtHandlerUpdateOne) during enumeration.
56 */
57typedef struct PGMHVUSTATE
58{
59 /** The VM handle. */
60 PVM pVM;
61 /** The VMCPU handle. */
62 PVMCPU pVCpu;
63 /** The todo flags. */
64 RTUINT fTodo;
65 /** The CR4 register value. */
66 uint32_t cr4;
67} PGMHVUSTATE, *PPGMHVUSTATE;
68
69
70/*******************************************************************************
71* Internal Functions *
72*******************************************************************************/
73DECLINLINE(int) pgmShwGetLongModePDPtr(PVMCPU pVCpu, RTGCPTR64 GCPtr, PX86PML4E *ppPml4e, PX86PDPT *ppPdpt, PX86PDPAE *ppPD);
74DECLINLINE(int) pgmShwGetPaePoolPagePD(PPGMCPU pPGM, RTGCPTR GCPtr, PPGMPOOLPAGE *ppShwPde);
75
76/*
77 * Shadow - 32-bit mode
78 */
79#define PGM_SHW_TYPE PGM_TYPE_32BIT
80#define PGM_SHW_NAME(name) PGM_SHW_NAME_32BIT(name)
81#include "PGMAllShw.h"
82
83/* Guest - real mode */
84#define PGM_GST_TYPE PGM_TYPE_REAL
85#define PGM_GST_NAME(name) PGM_GST_NAME_REAL(name)
86#define PGM_BTH_NAME(name) PGM_BTH_NAME_32BIT_REAL(name)
87#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_32BIT_PT_FOR_PHYS
88#define BTH_PGMPOOLKIND_ROOT PGMPOOLKIND_32BIT_PD_PHYS
89#include "PGMGstDefs.h"
90#include "PGMAllGst.h"
91#include "PGMAllBth.h"
92#undef BTH_PGMPOOLKIND_PT_FOR_PT
93#undef BTH_PGMPOOLKIND_ROOT
94#undef PGM_BTH_NAME
95#undef PGM_GST_TYPE
96#undef PGM_GST_NAME
97
98/* Guest - protected mode */
99#define PGM_GST_TYPE PGM_TYPE_PROT
100#define PGM_GST_NAME(name) PGM_GST_NAME_PROT(name)
101#define PGM_BTH_NAME(name) PGM_BTH_NAME_32BIT_PROT(name)
102#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_32BIT_PT_FOR_PHYS
103#define BTH_PGMPOOLKIND_ROOT PGMPOOLKIND_32BIT_PD_PHYS
104#include "PGMGstDefs.h"
105#include "PGMAllGst.h"
106#include "PGMAllBth.h"
107#undef BTH_PGMPOOLKIND_PT_FOR_PT
108#undef BTH_PGMPOOLKIND_ROOT
109#undef PGM_BTH_NAME
110#undef PGM_GST_TYPE
111#undef PGM_GST_NAME
112
113/* Guest - 32-bit mode */
114#define PGM_GST_TYPE PGM_TYPE_32BIT
115#define PGM_GST_NAME(name) PGM_GST_NAME_32BIT(name)
116#define PGM_BTH_NAME(name) PGM_BTH_NAME_32BIT_32BIT(name)
117#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT
118#define BTH_PGMPOOLKIND_PT_FOR_BIG PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB
119#define BTH_PGMPOOLKIND_ROOT PGMPOOLKIND_32BIT_PD
120#include "PGMGstDefs.h"
121#include "PGMAllGst.h"
122#include "PGMAllBth.h"
123#undef BTH_PGMPOOLKIND_PT_FOR_BIG
124#undef BTH_PGMPOOLKIND_PT_FOR_PT
125#undef BTH_PGMPOOLKIND_ROOT
126#undef PGM_BTH_NAME
127#undef PGM_GST_TYPE
128#undef PGM_GST_NAME
129
130#undef PGM_SHW_TYPE
131#undef PGM_SHW_NAME
132
133
134/*
135 * Shadow - PAE mode
136 */
137#define PGM_SHW_TYPE PGM_TYPE_PAE
138#define PGM_SHW_NAME(name) PGM_SHW_NAME_PAE(name)
139#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_REAL(name)
140#include "PGMAllShw.h"
141
142/* Guest - real mode */
143#define PGM_GST_TYPE PGM_TYPE_REAL
144#define PGM_GST_NAME(name) PGM_GST_NAME_REAL(name)
145#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_REAL(name)
146#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_PAE_PT_FOR_PHYS
147#define BTH_PGMPOOLKIND_ROOT PGMPOOLKIND_PAE_PDPT_PHYS
148#include "PGMGstDefs.h"
149#include "PGMAllBth.h"
150#undef BTH_PGMPOOLKIND_PT_FOR_PT
151#undef BTH_PGMPOOLKIND_ROOT
152#undef PGM_BTH_NAME
153#undef PGM_GST_TYPE
154#undef PGM_GST_NAME
155
156/* Guest - protected mode */
157#define PGM_GST_TYPE PGM_TYPE_PROT
158#define PGM_GST_NAME(name) PGM_GST_NAME_PROT(name)
159#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_PROT(name)
160#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_PAE_PT_FOR_PHYS
161#define BTH_PGMPOOLKIND_ROOT PGMPOOLKIND_PAE_PDPT_PHYS
162#include "PGMGstDefs.h"
163#include "PGMAllBth.h"
164#undef BTH_PGMPOOLKIND_PT_FOR_PT
165#undef BTH_PGMPOOLKIND_ROOT
166#undef PGM_BTH_NAME
167#undef PGM_GST_TYPE
168#undef PGM_GST_NAME
169
170/* Guest - 32-bit mode */
171#define PGM_GST_TYPE PGM_TYPE_32BIT
172#define PGM_GST_NAME(name) PGM_GST_NAME_32BIT(name)
173#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_32BIT(name)
174#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_PAE_PT_FOR_32BIT_PT
175#define BTH_PGMPOOLKIND_PT_FOR_BIG PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB
176#define BTH_PGMPOOLKIND_ROOT PGMPOOLKIND_PAE_PDPT_FOR_32BIT
177#include "PGMGstDefs.h"
178#include "PGMAllBth.h"
179#undef BTH_PGMPOOLKIND_PT_FOR_BIG
180#undef BTH_PGMPOOLKIND_PT_FOR_PT
181#undef BTH_PGMPOOLKIND_ROOT
182#undef PGM_BTH_NAME
183#undef PGM_GST_TYPE
184#undef PGM_GST_NAME
185
186
187/* Guest - PAE mode */
188#define PGM_GST_TYPE PGM_TYPE_PAE
189#define PGM_GST_NAME(name) PGM_GST_NAME_PAE(name)
190#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_PAE(name)
191#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_PAE_PT_FOR_PAE_PT
192#define BTH_PGMPOOLKIND_PT_FOR_BIG PGMPOOLKIND_PAE_PT_FOR_PAE_2MB
193#define BTH_PGMPOOLKIND_ROOT PGMPOOLKIND_PAE_PDPT
194#include "PGMGstDefs.h"
195#include "PGMAllGst.h"
196#include "PGMAllBth.h"
197#undef BTH_PGMPOOLKIND_PT_FOR_BIG
198#undef BTH_PGMPOOLKIND_PT_FOR_PT
199#undef BTH_PGMPOOLKIND_ROOT
200#undef PGM_BTH_NAME
201#undef PGM_GST_TYPE
202#undef PGM_GST_NAME
203
204#undef PGM_SHW_TYPE
205#undef PGM_SHW_NAME
206
207
208#ifndef IN_RC /* AMD64 implies VT-x/AMD-V */
209/*
210 * Shadow - AMD64 mode
211 */
212# define PGM_SHW_TYPE PGM_TYPE_AMD64
213# define PGM_SHW_NAME(name) PGM_SHW_NAME_AMD64(name)
214# include "PGMAllShw.h"
215
216/* Guest - protected mode (only used for AMD-V nested paging in 64 bits mode) */
217# define PGM_GST_TYPE PGM_TYPE_PROT
218# define PGM_GST_NAME(name) PGM_GST_NAME_PROT(name)
219# define PGM_BTH_NAME(name) PGM_BTH_NAME_AMD64_PROT(name)
220# define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_PAE_PT_FOR_PHYS
221# define BTH_PGMPOOLKIND_ROOT PGMPOOLKIND_PAE_PD_PHYS
222# include "PGMGstDefs.h"
223# include "PGMAllBth.h"
224# undef BTH_PGMPOOLKIND_PT_FOR_PT
225# undef BTH_PGMPOOLKIND_ROOT
226# undef PGM_BTH_NAME
227# undef PGM_GST_TYPE
228# undef PGM_GST_NAME
229
230# ifdef VBOX_WITH_64_BITS_GUESTS
231/* Guest - AMD64 mode */
232# define PGM_GST_TYPE PGM_TYPE_AMD64
233# define PGM_GST_NAME(name) PGM_GST_NAME_AMD64(name)
234# define PGM_BTH_NAME(name) PGM_BTH_NAME_AMD64_AMD64(name)
235# define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_PAE_PT_FOR_PAE_PT
236# define BTH_PGMPOOLKIND_PT_FOR_BIG PGMPOOLKIND_PAE_PT_FOR_PAE_2MB
237# define BTH_PGMPOOLKIND_ROOT PGMPOOLKIND_64BIT_PML4
238# include "PGMGstDefs.h"
239# include "PGMAllGst.h"
240# include "PGMAllBth.h"
241# undef BTH_PGMPOOLKIND_PT_FOR_BIG
242# undef BTH_PGMPOOLKIND_PT_FOR_PT
243# undef BTH_PGMPOOLKIND_ROOT
244# undef PGM_BTH_NAME
245# undef PGM_GST_TYPE
246# undef PGM_GST_NAME
247# endif /* VBOX_WITH_64_BITS_GUESTS */
248
249# undef PGM_SHW_TYPE
250# undef PGM_SHW_NAME
251
252
253/*
254 * Shadow - Nested paging mode
255 */
256# define PGM_SHW_TYPE PGM_TYPE_NESTED
257# define PGM_SHW_NAME(name) PGM_SHW_NAME_NESTED(name)
258# include "PGMAllShw.h"
259
260/* Guest - real mode */
261# define PGM_GST_TYPE PGM_TYPE_REAL
262# define PGM_GST_NAME(name) PGM_GST_NAME_REAL(name)
263# define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_REAL(name)
264# include "PGMGstDefs.h"
265# include "PGMAllBth.h"
266# undef PGM_BTH_NAME
267# undef PGM_GST_TYPE
268# undef PGM_GST_NAME
269
270/* Guest - protected mode */
271# define PGM_GST_TYPE PGM_TYPE_PROT
272# define PGM_GST_NAME(name) PGM_GST_NAME_PROT(name)
273# define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_PROT(name)
274# include "PGMGstDefs.h"
275# include "PGMAllBth.h"
276# undef PGM_BTH_NAME
277# undef PGM_GST_TYPE
278# undef PGM_GST_NAME
279
280/* Guest - 32-bit mode */
281# define PGM_GST_TYPE PGM_TYPE_32BIT
282# define PGM_GST_NAME(name) PGM_GST_NAME_32BIT(name)
283# define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_32BIT(name)
284# include "PGMGstDefs.h"
285# include "PGMAllBth.h"
286# undef PGM_BTH_NAME
287# undef PGM_GST_TYPE
288# undef PGM_GST_NAME
289
290/* Guest - PAE mode */
291# define PGM_GST_TYPE PGM_TYPE_PAE
292# define PGM_GST_NAME(name) PGM_GST_NAME_PAE(name)
293# define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_PAE(name)
294# include "PGMGstDefs.h"
295# include "PGMAllBth.h"
296# undef PGM_BTH_NAME
297# undef PGM_GST_TYPE
298# undef PGM_GST_NAME
299
300# ifdef VBOX_WITH_64_BITS_GUESTS
301/* Guest - AMD64 mode */
302# define PGM_GST_TYPE PGM_TYPE_AMD64
303# define PGM_GST_NAME(name) PGM_GST_NAME_AMD64(name)
304# define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_AMD64(name)
305# include "PGMGstDefs.h"
306# include "PGMAllBth.h"
307# undef PGM_BTH_NAME
308# undef PGM_GST_TYPE
309# undef PGM_GST_NAME
310# endif /* VBOX_WITH_64_BITS_GUESTS */
311
312# undef PGM_SHW_TYPE
313# undef PGM_SHW_NAME
314
315
316/*
317 * Shadow - EPT
318 */
319# define PGM_SHW_TYPE PGM_TYPE_EPT
320# define PGM_SHW_NAME(name) PGM_SHW_NAME_EPT(name)
321# include "PGMAllShw.h"
322
323/* Guest - real mode */
324# define PGM_GST_TYPE PGM_TYPE_REAL
325# define PGM_GST_NAME(name) PGM_GST_NAME_REAL(name)
326# define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_REAL(name)
327# define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_EPT_PT_FOR_PHYS
328# include "PGMGstDefs.h"
329# include "PGMAllBth.h"
330# undef BTH_PGMPOOLKIND_PT_FOR_PT
331# undef PGM_BTH_NAME
332# undef PGM_GST_TYPE
333# undef PGM_GST_NAME
334
335/* Guest - protected mode */
336# define PGM_GST_TYPE PGM_TYPE_PROT
337# define PGM_GST_NAME(name) PGM_GST_NAME_PROT(name)
338# define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_PROT(name)
339# define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_EPT_PT_FOR_PHYS
340# include "PGMGstDefs.h"
341# include "PGMAllBth.h"
342# undef BTH_PGMPOOLKIND_PT_FOR_PT
343# undef PGM_BTH_NAME
344# undef PGM_GST_TYPE
345# undef PGM_GST_NAME
346
347/* Guest - 32-bit mode */
348# define PGM_GST_TYPE PGM_TYPE_32BIT
349# define PGM_GST_NAME(name) PGM_GST_NAME_32BIT(name)
350# define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_32BIT(name)
351# define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_EPT_PT_FOR_PHYS
352# include "PGMGstDefs.h"
353# include "PGMAllBth.h"
354# undef BTH_PGMPOOLKIND_PT_FOR_PT
355# undef PGM_BTH_NAME
356# undef PGM_GST_TYPE
357# undef PGM_GST_NAME
358
359/* Guest - PAE mode */
360# define PGM_GST_TYPE PGM_TYPE_PAE
361# define PGM_GST_NAME(name) PGM_GST_NAME_PAE(name)
362# define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_PAE(name)
363# define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_EPT_PT_FOR_PHYS
364# include "PGMGstDefs.h"
365# include "PGMAllBth.h"
366# undef BTH_PGMPOOLKIND_PT_FOR_PT
367# undef PGM_BTH_NAME
368# undef PGM_GST_TYPE
369# undef PGM_GST_NAME
370
371# ifdef VBOX_WITH_64_BITS_GUESTS
372/* Guest - AMD64 mode */
373# define PGM_GST_TYPE PGM_TYPE_AMD64
374# define PGM_GST_NAME(name) PGM_GST_NAME_AMD64(name)
375# define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_AMD64(name)
376# define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_EPT_PT_FOR_PHYS
377# include "PGMGstDefs.h"
378# include "PGMAllBth.h"
379# undef BTH_PGMPOOLKIND_PT_FOR_PT
380# undef PGM_BTH_NAME
381# undef PGM_GST_TYPE
382# undef PGM_GST_NAME
383# endif /* VBOX_WITH_64_BITS_GUESTS */
384
385# undef PGM_SHW_TYPE
386# undef PGM_SHW_NAME
387
388#endif /* !IN_RC */
389
390
391#ifndef IN_RING3
392/**
393 * #PF Handler.
394 *
395 * @returns VBox status code (appropriate for trap handling and GC return).
396 * @param pVCpu VMCPU handle.
397 * @param uErr The trap error code.
398 * @param pRegFrame Trap register frame.
399 * @param pvFault The fault address.
400 */
401VMMDECL(int) PGMTrap0eHandler(PVMCPU pVCpu, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault)
402{
403 PVM pVM = pVCpu->CTX_SUFF(pVM);
404
405 Log(("PGMTrap0eHandler: uErr=%RGu pvFault=%RGv eip=%04x:%RGv\n", uErr, pvFault, pRegFrame->cs, (RTGCPTR)pRegFrame->rip));
406 STAM_PROFILE_START(&pVCpu->pgm.s.StatRZTrap0e, a);
407 STAM_STATS({ pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution) = NULL; } );
408
409
410#ifdef VBOX_WITH_STATISTICS
411 /*
412 * Error code stats.
413 */
414 if (uErr & X86_TRAP_PF_US)
415 {
416 if (!(uErr & X86_TRAP_PF_P))
417 {
418 if (uErr & X86_TRAP_PF_RW)
419 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSNotPresentWrite);
420 else
421 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSNotPresentRead);
422 }
423 else if (uErr & X86_TRAP_PF_RW)
424 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSWrite);
425 else if (uErr & X86_TRAP_PF_RSVD)
426 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSReserved);
427 else if (uErr & X86_TRAP_PF_ID)
428 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSNXE);
429 else
430 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSRead);
431 }
432 else
433 { /* Supervisor */
434 if (!(uErr & X86_TRAP_PF_P))
435 {
436 if (uErr & X86_TRAP_PF_RW)
437 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVNotPresentWrite);
438 else
439 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVNotPresentRead);
440 }
441 else if (uErr & X86_TRAP_PF_RW)
442 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVWrite);
443 else if (uErr & X86_TRAP_PF_ID)
444 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSNXE);
445 else if (uErr & X86_TRAP_PF_RSVD)
446 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVReserved);
447 }
448#endif /* VBOX_WITH_STATISTICS */
449
450 /*
451 * Call the worker.
452 */
453 pgmLock(pVM);
454 int rc = PGM_BTH_PFN(Trap0eHandler, pVCpu)(pVCpu, uErr, pRegFrame, pvFault);
455 Assert(PGMIsLockOwner(pVM));
456 pgmUnlock(pVM);
457 if (rc == VINF_PGM_SYNCPAGE_MODIFIED_PDE)
458 rc = VINF_SUCCESS;
459
460# ifdef IN_RING0
461 /* Note: hack alert for difficult to reproduce problem. */
462 if ( pVM->cCpus > 1
463 && rc == VERR_PAGE_TABLE_NOT_PRESENT)
464 {
465 Log(("WARNING: Unexpected VERR_PAGE_TABLE_NOT_PRESENT for page fault at %RGv error code %x (rip=%RGv)\n", pvFault, uErr, pRegFrame->rip));
466 rc = VINF_SUCCESS;
467 }
468# endif
469
470 STAM_STATS({ if (rc == VINF_EM_RAW_GUEST_TRAP) STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eGuestPF); });
471 STAM_STATS({ if (!pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution))
472 pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution) = &pVCpu->pgm.s.StatRZTrap0eTime2Misc; });
473 STAM_PROFILE_STOP_EX(&pVCpu->pgm.s.StatRZTrap0e, pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution), a);
474 return rc;
475}
476#endif /* !IN_RING3 */
477
478
479/**
480 * Prefetch a page
481 *
482 * Typically used to sync commonly used pages before entering raw mode
483 * after a CR3 reload.
484 *
485 * @returns VBox status code suitable for scheduling.
486 * @retval VINF_SUCCESS on success.
487 * @retval VINF_PGM_SYNC_CR3 if we're out of shadow pages or something like that.
488 * @param pVCpu VMCPU handle.
489 * @param GCPtrPage Page to invalidate.
490 */
491VMMDECL(int) PGMPrefetchPage(PVMCPU pVCpu, RTGCPTR GCPtrPage)
492{
493 STAM_PROFILE_START(&pVCpu->pgm.s.CTX_MID_Z(Stat,Prefetch), a);
494 int rc = PGM_BTH_PFN(PrefetchPage, pVCpu)(pVCpu, GCPtrPage);
495 STAM_PROFILE_STOP(&pVCpu->pgm.s.CTX_MID_Z(Stat,Prefetch), a);
496 AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 || RT_FAILURE(rc), ("rc=%Rrc\n", rc));
497 return rc;
498}
499
500
501/**
502 * Gets the mapping corresponding to the specified address (if any).
503 *
504 * @returns Pointer to the mapping.
505 * @returns NULL if not
506 *
507 * @param pVM The virtual machine.
508 * @param GCPtr The guest context pointer.
509 */
510PPGMMAPPING pgmGetMapping(PVM pVM, RTGCPTR GCPtr)
511{
512 PPGMMAPPING pMapping = pVM->pgm.s.CTX_SUFF(pMappings);
513 while (pMapping)
514 {
515 if ((uintptr_t)GCPtr < (uintptr_t)pMapping->GCPtr)
516 break;
517 if ((uintptr_t)GCPtr - (uintptr_t)pMapping->GCPtr < pMapping->cb)
518 return pMapping;
519 pMapping = pMapping->CTX_SUFF(pNext);
520 }
521 return NULL;
522}
523
524
525/**
526 * Verifies a range of pages for read or write access
527 *
528 * Only checks the guest's page tables
529 *
530 * @returns VBox status code.
531 * @param pVCpu VMCPU handle.
532 * @param Addr Guest virtual address to check
533 * @param cbSize Access size
534 * @param fAccess Access type (r/w, user/supervisor (X86_PTE_*))
535 * @remarks Current not in use.
536 */
537VMMDECL(int) PGMIsValidAccess(PVMCPU pVCpu, RTGCPTR Addr, uint32_t cbSize, uint32_t fAccess)
538{
539 /*
540 * Validate input.
541 */
542 if (fAccess & ~(X86_PTE_US | X86_PTE_RW))
543 {
544 AssertMsgFailed(("PGMIsValidAccess: invalid access type %08x\n", fAccess));
545 return VERR_INVALID_PARAMETER;
546 }
547
548 uint64_t fPage;
549 int rc = PGMGstGetPage(pVCpu, (RTGCPTR)Addr, &fPage, NULL);
550 if (RT_FAILURE(rc))
551 {
552 Log(("PGMIsValidAccess: access violation for %RGv rc=%d\n", Addr, rc));
553 return VINF_EM_RAW_GUEST_TRAP;
554 }
555
556 /*
557 * Check if the access would cause a page fault
558 *
559 * Note that hypervisor page directories are not present in the guest's tables, so this check
560 * is sufficient.
561 */
562 bool fWrite = !!(fAccess & X86_PTE_RW);
563 bool fUser = !!(fAccess & X86_PTE_US);
564 if ( !(fPage & X86_PTE_P)
565 || (fWrite && !(fPage & X86_PTE_RW))
566 || (fUser && !(fPage & X86_PTE_US)) )
567 {
568 Log(("PGMIsValidAccess: access violation for %RGv attr %#llx vs %d:%d\n", Addr, fPage, fWrite, fUser));
569 return VINF_EM_RAW_GUEST_TRAP;
570 }
571 if ( RT_SUCCESS(rc)
572 && PAGE_ADDRESS(Addr) != PAGE_ADDRESS(Addr + cbSize))
573 return PGMIsValidAccess(pVCpu, Addr + PAGE_SIZE, (cbSize > PAGE_SIZE) ? cbSize - PAGE_SIZE : 1, fAccess);
574 return rc;
575}
576
577
578/**
579 * Verifies a range of pages for read or write access
580 *
581 * Supports handling of pages marked for dirty bit tracking and CSAM
582 *
583 * @returns VBox status code.
584 * @param pVCpu VMCPU handle.
585 * @param Addr Guest virtual address to check
586 * @param cbSize Access size
587 * @param fAccess Access type (r/w, user/supervisor (X86_PTE_*))
588 */
589VMMDECL(int) PGMVerifyAccess(PVMCPU pVCpu, RTGCPTR Addr, uint32_t cbSize, uint32_t fAccess)
590{
591 PVM pVM = pVCpu->CTX_SUFF(pVM);
592
593 AssertMsg(!(fAccess & ~(X86_PTE_US | X86_PTE_RW)), ("PGMVerifyAccess: invalid access type %08x\n", fAccess));
594
595 /*
596 * Get going.
597 */
598 uint64_t fPageGst;
599 int rc = PGMGstGetPage(pVCpu, (RTGCPTR)Addr, &fPageGst, NULL);
600 if (RT_FAILURE(rc))
601 {
602 Log(("PGMVerifyAccess: access violation for %RGv rc=%d\n", Addr, rc));
603 return VINF_EM_RAW_GUEST_TRAP;
604 }
605
606 /*
607 * Check if the access would cause a page fault
608 *
609 * Note that hypervisor page directories are not present in the guest's tables, so this check
610 * is sufficient.
611 */
612 const bool fWrite = !!(fAccess & X86_PTE_RW);
613 const bool fUser = !!(fAccess & X86_PTE_US);
614 if ( !(fPageGst & X86_PTE_P)
615 || (fWrite && !(fPageGst & X86_PTE_RW))
616 || (fUser && !(fPageGst & X86_PTE_US)) )
617 {
618 Log(("PGMVerifyAccess: access violation for %RGv attr %#llx vs %d:%d\n", Addr, fPageGst, fWrite, fUser));
619 return VINF_EM_RAW_GUEST_TRAP;
620 }
621
622 if (!HWACCMIsNestedPagingActive(pVM))
623 {
624 /*
625 * Next step is to verify if we protected this page for dirty bit tracking or for CSAM scanning
626 */
627 rc = PGMShwGetPage(pVCpu, (RTGCPTR)Addr, NULL, NULL);
628 if ( rc == VERR_PAGE_NOT_PRESENT
629 || rc == VERR_PAGE_TABLE_NOT_PRESENT)
630 {
631 /*
632 * Page is not present in our page tables.
633 * Try to sync it!
634 */
635 Assert(X86_TRAP_PF_RW == X86_PTE_RW && X86_TRAP_PF_US == X86_PTE_US);
636 uint32_t uErr = fAccess & (X86_TRAP_PF_RW | X86_TRAP_PF_US);
637 rc = PGM_BTH_PFN(VerifyAccessSyncPage, pVCpu)(pVCpu, Addr, fPageGst, uErr);
638 if (rc != VINF_SUCCESS)
639 return rc;
640 }
641 else
642 AssertMsg(rc == VINF_SUCCESS, ("PGMShwGetPage %RGv failed with %Rrc\n", Addr, rc));
643 }
644
645#if 0 /* def VBOX_STRICT; triggers too often now */
646 /*
647 * This check is a bit paranoid, but useful.
648 */
649 /** @note this will assert when writing to monitored pages (a bit annoying actually) */
650 uint64_t fPageShw;
651 rc = PGMShwGetPage(pVCpu, (RTGCPTR)Addr, &fPageShw, NULL);
652 if ( (rc == VERR_PAGE_NOT_PRESENT || RT_FAILURE(rc))
653 || (fWrite && !(fPageShw & X86_PTE_RW))
654 || (fUser && !(fPageShw & X86_PTE_US)) )
655 {
656 AssertMsgFailed(("Unexpected access violation for %RGv! rc=%Rrc write=%d user=%d\n",
657 Addr, rc, fWrite && !(fPageShw & X86_PTE_RW), fUser && !(fPageShw & X86_PTE_US)));
658 return VINF_EM_RAW_GUEST_TRAP;
659 }
660#endif
661
662 if ( RT_SUCCESS(rc)
663 && ( PAGE_ADDRESS(Addr) != PAGE_ADDRESS(Addr + cbSize - 1)
664 || Addr + cbSize < Addr))
665 {
666 /* Don't recursively call PGMVerifyAccess as we might run out of stack. */
667 for (;;)
668 {
669 Addr += PAGE_SIZE;
670 if (cbSize > PAGE_SIZE)
671 cbSize -= PAGE_SIZE;
672 else
673 cbSize = 1;
674 rc = PGMVerifyAccess(pVCpu, Addr, 1, fAccess);
675 if (rc != VINF_SUCCESS)
676 break;
677 if (PAGE_ADDRESS(Addr) == PAGE_ADDRESS(Addr + cbSize - 1))
678 break;
679 }
680 }
681 return rc;
682}
683
684
685/**
686 * Emulation of the invlpg instruction (HC only actually).
687 *
688 * @returns VBox status code, special care required.
689 * @retval VINF_PGM_SYNC_CR3 - handled.
690 * @retval VINF_EM_RAW_EMULATE_INSTR - not handled (RC only).
691 * @retval VERR_REM_FLUSHED_PAGES_OVERFLOW - not handled.
692 *
693 * @param pVCpu VMCPU handle.
694 * @param GCPtrPage Page to invalidate.
695 *
696 * @remark ASSUMES the page table entry or page directory is valid. Fairly
697 * safe, but there could be edge cases!
698 *
699 * @todo Flush page or page directory only if necessary!
700 */
701VMMDECL(int) PGMInvalidatePage(PVMCPU pVCpu, RTGCPTR GCPtrPage)
702{
703 PVM pVM = pVCpu->CTX_SUFF(pVM);
704 int rc;
705 Log3(("PGMInvalidatePage: GCPtrPage=%RGv\n", GCPtrPage));
706
707#ifndef IN_RING3
708 /*
709 * Notify the recompiler so it can record this instruction.
710 * Failure happens when it's out of space. We'll return to HC in that case.
711 */
712 rc = REMNotifyInvalidatePage(pVM, GCPtrPage);
713 if (rc != VINF_SUCCESS)
714 return rc;
715#endif /* !IN_RING3 */
716
717
718#ifdef IN_RC
719 /*
720 * Check for conflicts and pending CR3 monitoring updates.
721 */
722 if (!pVM->pgm.s.fMappingsFixed)
723 {
724 if ( pgmGetMapping(pVM, GCPtrPage)
725 && PGMGstGetPage(pVCpu, GCPtrPage, NULL, NULL) != VERR_PAGE_TABLE_NOT_PRESENT)
726 {
727 LogFlow(("PGMGCInvalidatePage: Conflict!\n"));
728 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
729 STAM_COUNTER_INC(&pVM->pgm.s.StatRCInvlPgConflict);
730 return VINF_PGM_SYNC_CR3;
731 }
732
733 if (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_MONITOR_CR3)
734 {
735 LogFlow(("PGMGCInvalidatePage: PGM_SYNC_MONITOR_CR3 -> reinterpret instruction in R3\n"));
736 STAM_COUNTER_INC(&pVM->pgm.s.StatRCInvlPgSyncMonCR3);
737 return VINF_EM_RAW_EMULATE_INSTR;
738 }
739 }
740#endif /* IN_RC */
741
742 /*
743 * Call paging mode specific worker.
744 */
745 STAM_PROFILE_START(&pVCpu->pgm.s.CTX_MID_Z(Stat,InvalidatePage), a);
746 pgmLock(pVM);
747 rc = PGM_BTH_PFN(InvalidatePage, pVCpu)(pVCpu, GCPtrPage);
748 pgmUnlock(pVM);
749 STAM_PROFILE_STOP(&pVCpu->pgm.s.CTX_MID_Z(Stat,InvalidatePage), a);
750
751#ifdef IN_RING3
752 /*
753 * Check if we have a pending update of the CR3 monitoring.
754 */
755 if ( RT_SUCCESS(rc)
756 && (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_MONITOR_CR3))
757 {
758 pVCpu->pgm.s.fSyncFlags &= ~PGM_SYNC_MONITOR_CR3;
759 Assert(!pVM->pgm.s.fMappingsFixed);
760 }
761
762 /*
763 * Inform CSAM about the flush
764 *
765 * Note: This is to check if monitored pages have been changed; when we implement
766 * callbacks for virtual handlers, this is no longer required.
767 */
768 CSAMR3FlushPage(pVM, GCPtrPage);
769#endif /* IN_RING3 */
770 return rc;
771}
772
773
774/**
775 * Executes an instruction using the interpreter.
776 *
777 * @returns VBox status code (appropriate for trap handling and GC return).
778 * @param pVM VM handle.
779 * @param pVCpu VMCPU handle.
780 * @param pRegFrame Register frame.
781 * @param pvFault Fault address.
782 */
783VMMDECL(int) PGMInterpretInstruction(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault)
784{
785 uint32_t cb;
786 int rc = EMInterpretInstruction(pVM, pVCpu, pRegFrame, pvFault, &cb);
787 if (rc == VERR_EM_INTERPRETER)
788 rc = VINF_EM_RAW_EMULATE_INSTR;
789 if (rc != VINF_SUCCESS)
790 Log(("PGMInterpretInstruction: returns %Rrc (pvFault=%RGv)\n", rc, pvFault));
791 return rc;
792}
793
794
795/**
796 * Gets effective page information (from the VMM page directory).
797 *
798 * @returns VBox status.
799 * @param pVCpu VMCPU handle.
800 * @param GCPtr Guest Context virtual address of the page.
801 * @param pfFlags Where to store the flags. These are X86_PTE_*.
802 * @param pHCPhys Where to store the HC physical address of the page.
803 * This is page aligned.
804 * @remark You should use PGMMapGetPage() for pages in a mapping.
805 */
806VMMDECL(int) PGMShwGetPage(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys)
807{
808 pgmLock(pVCpu->CTX_SUFF(pVM));
809 int rc = PGM_SHW_PFN(GetPage, pVCpu)(pVCpu, GCPtr, pfFlags, pHCPhys);
810 pgmUnlock(pVCpu->CTX_SUFF(pVM));
811 return rc;
812}
813
814
815/**
816 * Sets (replaces) the page flags for a range of pages in the shadow context.
817 *
818 * @returns VBox status.
819 * @param pVCpu VMCPU handle.
820 * @param GCPtr The address of the first page.
821 * @param cb The size of the range in bytes.
822 * @param fFlags Page flags X86_PTE_*, excluding the page mask of course.
823 * @remark You must use PGMMapSetPage() for pages in a mapping.
824 */
825VMMDECL(int) PGMShwSetPage(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cb, uint64_t fFlags)
826{
827 return PGMShwModifyPage(pVCpu, GCPtr, cb, fFlags, 0);
828}
829
830
831/**
832 * Modify page flags for a range of pages in the shadow context.
833 *
834 * The existing flags are ANDed with the fMask and ORed with the fFlags.
835 *
836 * @returns VBox status code.
837 * @param pVCpu VMCPU handle.
838 * @param GCPtr Virtual address of the first page in the range.
839 * @param cb Size (in bytes) of the range to apply the modification to.
840 * @param fFlags The OR mask - page flags X86_PTE_*, excluding the page mask of course.
841 * @param fMask The AND mask - page flags X86_PTE_*.
842 * Be very CAREFUL when ~'ing constants which could be 32-bit!
843 * @remark You must use PGMMapModifyPage() for pages in a mapping.
844 */
845VMMDECL(int) PGMShwModifyPage(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cb, uint64_t fFlags, uint64_t fMask)
846{
847 AssertMsg(!(fFlags & X86_PTE_PAE_PG_MASK), ("fFlags=%#llx\n", fFlags));
848 Assert(cb);
849
850 /*
851 * Align the input.
852 */
853 cb += GCPtr & PAGE_OFFSET_MASK;
854 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
855 GCPtr = (GCPtr & PAGE_BASE_GC_MASK); /** @todo this ain't necessary, right... */
856
857 /*
858 * Call worker.
859 */
860 PVM pVM = pVCpu->CTX_SUFF(pVM);
861 pgmLock(pVM);
862 int rc = PGM_SHW_PFN(ModifyPage, pVCpu)(pVCpu, GCPtr, cb, fFlags, fMask);
863 pgmUnlock(pVM);
864 return rc;
865}
866
867/**
868 * Gets the shadow page directory for the specified address, PAE.
869 *
870 * @returns Pointer to the shadow PD.
871 * @param pVCpu The VMCPU handle.
872 * @param GCPtr The address.
873 * @param pGstPdpe Guest PDPT entry
874 * @param ppPD Receives address of page directory
875 */
876int pgmShwSyncPaePDPtr(PVMCPU pVCpu, RTGCPTR GCPtr, PX86PDPE pGstPdpe, PX86PDPAE *ppPD)
877{
878 const unsigned iPdPt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
879 PX86PDPT pPdpt = pgmShwGetPaePDPTPtr(&pVCpu->pgm.s);
880 PX86PDPE pPdpe = &pPdpt->a[iPdPt];
881 PVM pVM = pVCpu->CTX_SUFF(pVM);
882 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
883 PPGMPOOLPAGE pShwPage;
884 int rc;
885
886 Assert(PGMIsLockOwner(pVM));
887
888 /* Allocate page directory if not present. */
889 if ( !pPdpe->n.u1Present
890 && !(pPdpe->u & X86_PDPE_PG_MASK))
891 {
892 bool fNestedPaging = HWACCMIsNestedPagingActive(pVM);
893 bool fPaging = !!(CPUMGetGuestCR0(pVCpu) & X86_CR0_PG);
894 RTGCPTR64 GCPdPt;
895 PGMPOOLKIND enmKind;
896
897# if defined(IN_RC)
898 /* Make sure the dynamic pPdeDst mapping will not be reused during this function. */
899 PGMDynLockHCPage(pVM, (uint8_t *)pPdpe);
900# endif
901
902 if (fNestedPaging || !fPaging)
903 {
904 /* AMD-V nested paging or real/protected mode without paging */
905 GCPdPt = (RTGCPTR64)iPdPt << X86_PDPT_SHIFT;
906 enmKind = PGMPOOLKIND_PAE_PD_PHYS;
907 }
908 else
909 {
910 Assert(pGstPdpe);
911
912 if (CPUMGetGuestCR4(pVCpu) & X86_CR4_PAE)
913 {
914 if (!pGstPdpe->n.u1Present)
915 {
916 /* PD not present; guest must reload CR3 to change it.
917 * No need to monitor anything in this case.
918 */
919 Assert(!HWACCMIsEnabled(pVM));
920
921 GCPdPt = pGstPdpe->u & X86_PDPE_PG_MASK;
922 enmKind = PGMPOOLKIND_PAE_PD_PHYS;
923 pGstPdpe->n.u1Present = 1;
924 }
925 else
926 {
927 GCPdPt = pGstPdpe->u & X86_PDPE_PG_MASK;
928 enmKind = PGMPOOLKIND_PAE_PD_FOR_PAE_PD;
929 }
930 }
931 else
932 {
933 GCPdPt = CPUMGetGuestCR3(pVCpu);
934 enmKind = (PGMPOOLKIND)(PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD + iPdPt);
935 }
936 }
937
938 /* Create a reference back to the PDPT by using the index in its shadow page. */
939 rc = pgmPoolAlloc(pVM, GCPdPt, enmKind, pVCpu->pgm.s.CTX_SUFF(pShwPageCR3)->idx, iPdPt, &pShwPage);
940 AssertRCReturn(rc, rc);
941
942 /* The PD was cached or created; hook it up now. */
943 pPdpe->u |= pShwPage->Core.Key
944 | (pGstPdpe->u & ~(X86_PDPE_PG_MASK | X86_PDPE_AVL_MASK | X86_PDPE_PCD | X86_PDPE_PWT));
945
946# if defined(IN_RC)
947 /* In 32 bits PAE mode we *must* invalidate the TLB when changing a PDPT entry; the CPU fetches them only during cr3 load, so any
948 * non-present PDPT will continue to cause page faults.
949 */
950 ASMReloadCR3();
951 PGMDynUnlockHCPage(pVM, (uint8_t *)pPdpe);
952# endif
953 }
954 else
955 {
956 pShwPage = pgmPoolGetPage(pPool, pPdpe->u & X86_PDPE_PG_MASK);
957 AssertReturn(pShwPage, VERR_INTERNAL_ERROR);
958 Assert((pPdpe->u & X86_PDPE_PG_MASK) == pShwPage->Core.Key);
959
960 pgmPoolCacheUsed(pPool, pShwPage);
961 }
962 *ppPD = (PX86PDPAE)PGMPOOL_PAGE_2_PTR(pVM, pShwPage);
963 return VINF_SUCCESS;
964}
965
966
967/**
968 * Gets the pointer to the shadow page directory entry for an address, PAE.
969 *
970 * @returns Pointer to the PDE.
971 * @param pPGM Pointer to the PGMCPU instance data.
972 * @param GCPtr The address.
973 * @param ppShwPde Receives the address of the pgm pool page for the shadow page directory
974 */
975DECLINLINE(int) pgmShwGetPaePoolPagePD(PPGMCPU pPGM, RTGCPTR GCPtr, PPGMPOOLPAGE *ppShwPde)
976{
977 const unsigned iPdPt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
978 PX86PDPT pPdpt = pgmShwGetPaePDPTPtr(pPGM);
979
980 Assert(PGMIsLockOwner(PGMCPU2VM(pPGM)));
981
982 AssertReturn(pPdpt, VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT); /* can't happen */
983 if (!pPdpt->a[iPdPt].n.u1Present)
984 {
985 LogFlow(("pgmShwGetPaePoolPagePD: PD %d not present (%RX64)\n", iPdPt, pPdpt->a[iPdPt].u));
986 return VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT;
987 }
988 AssertMsg(pPdpt->a[iPdPt].u & X86_PDPE_PG_MASK, ("GCPtr=%RGv\n", GCPtr));
989
990 /* Fetch the pgm pool shadow descriptor. */
991 PPGMPOOLPAGE pShwPde = pgmPoolGetPage(PGMCPU2PGM(pPGM)->CTX_SUFF(pPool), pPdpt->a[iPdPt].u & X86_PDPE_PG_MASK);
992 AssertReturn(pShwPde, VERR_INTERNAL_ERROR);
993
994 *ppShwPde = pShwPde;
995 return VINF_SUCCESS;
996}
997
998#ifndef IN_RC
999
1000/**
1001 * Syncs the SHADOW page directory pointer for the specified address.
1002 *
1003 * Allocates backing pages in case the PDPT or PML4 entry is missing.
1004 *
1005 * The caller is responsible for making sure the guest has a valid PD before
1006 * calling this function.
1007 *
1008 * @returns VBox status.
1009 * @param pVCpu VMCPU handle.
1010 * @param GCPtr The address.
1011 * @param pGstPml4e Guest PML4 entry
1012 * @param pGstPdpe Guest PDPT entry
1013 * @param ppPD Receives address of page directory
1014 */
1015int pgmShwSyncLongModePDPtr(PVMCPU pVCpu, RTGCPTR64 GCPtr, PX86PML4E pGstPml4e, PX86PDPE pGstPdpe, PX86PDPAE *ppPD)
1016{
1017 PPGMCPU pPGM = &pVCpu->pgm.s;
1018 PVM pVM = pVCpu->CTX_SUFF(pVM);
1019 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
1020 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
1021 PX86PML4E pPml4e = pgmShwGetLongModePML4EPtr(pPGM, iPml4);
1022 bool fNestedPaging = HWACCMIsNestedPagingActive(pVM);
1023 bool fPaging = !!(CPUMGetGuestCR0(pVCpu) & X86_CR0_PG);
1024 PPGMPOOLPAGE pShwPage;
1025 int rc;
1026
1027 Assert(PGMIsLockOwner(pVM));
1028
1029 /* Allocate page directory pointer table if not present. */
1030 if ( !pPml4e->n.u1Present
1031 && !(pPml4e->u & X86_PML4E_PG_MASK))
1032 {
1033 RTGCPTR64 GCPml4;
1034 PGMPOOLKIND enmKind;
1035
1036 Assert(pVCpu->pgm.s.CTX_SUFF(pShwPageCR3));
1037
1038 if (fNestedPaging || !fPaging)
1039 {
1040 /* AMD-V nested paging or real/protected mode without paging */
1041 GCPml4 = (RTGCPTR64)iPml4 << X86_PML4_SHIFT;
1042 enmKind = PGMPOOLKIND_64BIT_PDPT_FOR_PHYS;
1043 }
1044 else
1045 {
1046 Assert(pGstPml4e && pGstPdpe);
1047
1048 GCPml4 = pGstPml4e->u & X86_PML4E_PG_MASK;
1049 enmKind = PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT;
1050 }
1051
1052 /* Create a reference back to the PDPT by using the index in its shadow page. */
1053 rc = pgmPoolAlloc(pVM, GCPml4, enmKind, pVCpu->pgm.s.CTX_SUFF(pShwPageCR3)->idx, iPml4, &pShwPage);
1054 AssertRCReturn(rc, rc);
1055 }
1056 else
1057 {
1058 pShwPage = pgmPoolGetPage(pPool, pPml4e->u & X86_PML4E_PG_MASK);
1059 AssertReturn(pShwPage, VERR_INTERNAL_ERROR);
1060
1061 pgmPoolCacheUsed(pPool, pShwPage);
1062 }
1063 /* The PDPT was cached or created; hook it up now. */
1064 pPml4e->u |= pShwPage->Core.Key
1065 | (pGstPml4e->u & ~(X86_PML4E_PG_MASK | X86_PML4E_AVL_MASK | X86_PML4E_PCD | X86_PML4E_PWT));
1066
1067 const unsigned iPdPt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
1068 PX86PDPT pPdpt = (PX86PDPT)PGMPOOL_PAGE_2_PTR(pVM, pShwPage);
1069 PX86PDPE pPdpe = &pPdpt->a[iPdPt];
1070
1071 /* Allocate page directory if not present. */
1072 if ( !pPdpe->n.u1Present
1073 && !(pPdpe->u & X86_PDPE_PG_MASK))
1074 {
1075 RTGCPTR64 GCPdPt;
1076 PGMPOOLKIND enmKind;
1077
1078 if (fNestedPaging || !fPaging)
1079 {
1080 /* AMD-V nested paging or real/protected mode without paging */
1081 GCPdPt = (RTGCPTR64)iPdPt << X86_PDPT_SHIFT;
1082 enmKind = PGMPOOLKIND_64BIT_PD_FOR_PHYS;
1083 }
1084 else
1085 {
1086 Assert(pGstPdpe);
1087
1088 GCPdPt = pGstPdpe->u & X86_PDPE_PG_MASK;
1089 enmKind = PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD;
1090 }
1091
1092 /* Create a reference back to the PDPT by using the index in its shadow page. */
1093 rc = pgmPoolAlloc(pVM, GCPdPt, enmKind, pShwPage->idx, iPdPt, &pShwPage);
1094 AssertRCReturn(rc, rc);
1095 }
1096 else
1097 {
1098 pShwPage = pgmPoolGetPage(pPool, pPdpe->u & X86_PDPE_PG_MASK);
1099 AssertReturn(pShwPage, VERR_INTERNAL_ERROR);
1100
1101 pgmPoolCacheUsed(pPool, pShwPage);
1102 }
1103 /* The PD was cached or created; hook it up now. */
1104 pPdpe->u |= pShwPage->Core.Key
1105 | (pGstPdpe->u & ~(X86_PDPE_PG_MASK | X86_PDPE_AVL_MASK | X86_PDPE_PCD | X86_PDPE_PWT));
1106
1107 *ppPD = (PX86PDPAE)PGMPOOL_PAGE_2_PTR(pVM, pShwPage);
1108 return VINF_SUCCESS;
1109}
1110
1111
1112/**
1113 * Gets the SHADOW page directory pointer for the specified address (long mode).
1114 *
1115 * @returns VBox status.
1116 * @param pVCpu VMCPU handle.
1117 * @param GCPtr The address.
1118 * @param ppPdpt Receives address of pdpt
1119 * @param ppPD Receives address of page directory
1120 */
1121DECLINLINE(int) pgmShwGetLongModePDPtr(PVMCPU pVCpu, RTGCPTR64 GCPtr, PX86PML4E *ppPml4e, PX86PDPT *ppPdpt, PX86PDPAE *ppPD)
1122{
1123 PPGMCPU pPGM = &pVCpu->pgm.s;
1124 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
1125 PCX86PML4E pPml4e = pgmShwGetLongModePML4EPtr(pPGM, iPml4);
1126
1127 Assert(PGMIsLockOwner(PGMCPU2VM(pPGM)));
1128
1129 AssertReturn(pPml4e, VERR_INTERNAL_ERROR);
1130 if (ppPml4e)
1131 *ppPml4e = (PX86PML4E)pPml4e;
1132
1133 Log4(("pgmShwGetLongModePDPtr %VGv (%VHv) %RX64\n", GCPtr, pPml4e, pPml4e->u));
1134
1135 if (!pPml4e->n.u1Present)
1136 return VERR_PAGE_MAP_LEVEL4_NOT_PRESENT;
1137
1138 PVM pVM = pVCpu->CTX_SUFF(pVM);
1139 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
1140 PPGMPOOLPAGE pShwPage = pgmPoolGetPage(pPool, pPml4e->u & X86_PML4E_PG_MASK);
1141 AssertReturn(pShwPage, VERR_INTERNAL_ERROR);
1142
1143 const unsigned iPdPt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
1144 PCX86PDPT pPdpt = *ppPdpt = (PX86PDPT)PGMPOOL_PAGE_2_PTR(pVM, pShwPage);
1145 if (!pPdpt->a[iPdPt].n.u1Present)
1146 return VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT;
1147
1148 pShwPage = pgmPoolGetPage(pPool, pPdpt->a[iPdPt].u & X86_PDPE_PG_MASK);
1149 AssertReturn(pShwPage, VERR_INTERNAL_ERROR);
1150
1151 *ppPD = (PX86PDPAE)PGMPOOL_PAGE_2_PTR(pVM, pShwPage);
1152 return VINF_SUCCESS;
1153}
1154
1155
1156/**
1157 * Syncs the SHADOW EPT page directory pointer for the specified address. Allocates
1158 * backing pages in case the PDPT or PML4 entry is missing.
1159 *
1160 * @returns VBox status.
1161 * @param pVCpu VMCPU handle.
1162 * @param GCPtr The address.
1163 * @param ppPdpt Receives address of pdpt
1164 * @param ppPD Receives address of page directory
1165 */
1166int pgmShwGetEPTPDPtr(PVMCPU pVCpu, RTGCPTR64 GCPtr, PEPTPDPT *ppPdpt, PEPTPD *ppPD)
1167{
1168 PPGMCPU pPGM = &pVCpu->pgm.s;
1169 PVM pVM = pVCpu->CTX_SUFF(pVM);
1170 const unsigned iPml4 = (GCPtr >> EPT_PML4_SHIFT) & EPT_PML4_MASK;
1171 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
1172 PEPTPML4 pPml4;
1173 PEPTPML4E pPml4e;
1174 PPGMPOOLPAGE pShwPage;
1175 int rc;
1176
1177 Assert(HWACCMIsNestedPagingActive(pVM));
1178 Assert(PGMIsLockOwner(pVM));
1179
1180 pPml4 = (PEPTPML4)PGMPOOL_PAGE_2_PTR_BY_PGMCPU(pPGM, pPGM->CTX_SUFF(pShwPageCR3));
1181 Assert(pPml4);
1182
1183 /* Allocate page directory pointer table if not present. */
1184 pPml4e = &pPml4->a[iPml4];
1185 if ( !pPml4e->n.u1Present
1186 && !(pPml4e->u & EPT_PML4E_PG_MASK))
1187 {
1188 Assert(!(pPml4e->u & EPT_PML4E_PG_MASK));
1189 RTGCPTR64 GCPml4 = (RTGCPTR64)iPml4 << EPT_PML4_SHIFT;
1190
1191 rc = pgmPoolAlloc(pVM, GCPml4, PGMPOOLKIND_EPT_PDPT_FOR_PHYS, PGMPOOL_IDX_NESTED_ROOT, iPml4, &pShwPage);
1192 AssertRCReturn(rc, rc);
1193 }
1194 else
1195 {
1196 pShwPage = pgmPoolGetPage(pPool, pPml4e->u & EPT_PML4E_PG_MASK);
1197 AssertReturn(pShwPage, VERR_INTERNAL_ERROR);
1198
1199 pgmPoolCacheUsed(pPool, pShwPage);
1200 }
1201 /* The PDPT was cached or created; hook it up now and fill with the default value. */
1202 pPml4e->u = pShwPage->Core.Key;
1203 pPml4e->n.u1Present = 1;
1204 pPml4e->n.u1Write = 1;
1205 pPml4e->n.u1Execute = 1;
1206
1207 const unsigned iPdPt = (GCPtr >> EPT_PDPT_SHIFT) & EPT_PDPT_MASK;
1208 PEPTPDPT pPdpt = (PEPTPDPT)PGMPOOL_PAGE_2_PTR(pVM, pShwPage);
1209 PEPTPDPTE pPdpe = &pPdpt->a[iPdPt];
1210
1211 if (ppPdpt)
1212 *ppPdpt = pPdpt;
1213
1214 /* Allocate page directory if not present. */
1215 if ( !pPdpe->n.u1Present
1216 && !(pPdpe->u & EPT_PDPTE_PG_MASK))
1217 {
1218 RTGCPTR64 GCPdPt = (RTGCPTR64)iPdPt << EPT_PDPT_SHIFT;
1219
1220 rc = pgmPoolAlloc(pVM, GCPdPt, PGMPOOLKIND_64BIT_PD_FOR_PHYS, pShwPage->idx, iPdPt, &pShwPage);
1221 AssertRCReturn(rc, rc);
1222 }
1223 else
1224 {
1225 pShwPage = pgmPoolGetPage(pPool, pPdpe->u & EPT_PDPTE_PG_MASK);
1226 AssertReturn(pShwPage, VERR_INTERNAL_ERROR);
1227
1228 pgmPoolCacheUsed(pPool, pShwPage);
1229 }
1230 /* The PD was cached or created; hook it up now and fill with the default value. */
1231 pPdpe->u = pShwPage->Core.Key;
1232 pPdpe->n.u1Present = 1;
1233 pPdpe->n.u1Write = 1;
1234 pPdpe->n.u1Execute = 1;
1235
1236 *ppPD = (PEPTPD)PGMPOOL_PAGE_2_PTR(pVM, pShwPage);
1237 return VINF_SUCCESS;
1238}
1239
1240#endif /* IN_RC */
1241
1242/**
1243 * Gets effective Guest OS page information.
1244 *
1245 * When GCPtr is in a big page, the function will return as if it was a normal
1246 * 4KB page. If the need for distinguishing between big and normal page becomes
1247 * necessary at a later point, a PGMGstGetPage() will be created for that
1248 * purpose.
1249 *
1250 * @returns VBox status.
1251 * @param pVCpu VMCPU handle.
1252 * @param GCPtr Guest Context virtual address of the page.
1253 * @param pfFlags Where to store the flags. These are X86_PTE_*, even for big pages.
1254 * @param pGCPhys Where to store the GC physical address of the page.
1255 * This is page aligned. The fact that the
1256 */
1257VMMDECL(int) PGMGstGetPage(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys)
1258{
1259 return PGM_GST_PFN(GetPage, pVCpu)(pVCpu, GCPtr, pfFlags, pGCPhys);
1260}
1261
1262
1263/**
1264 * Checks if the page is present.
1265 *
1266 * @returns true if the page is present.
1267 * @returns false if the page is not present.
1268 * @param pVCpu VMCPU handle.
1269 * @param GCPtr Address within the page.
1270 */
1271VMMDECL(bool) PGMGstIsPagePresent(PVMCPU pVCpu, RTGCPTR GCPtr)
1272{
1273 int rc = PGMGstGetPage(pVCpu, GCPtr, NULL, NULL);
1274 return RT_SUCCESS(rc);
1275}
1276
1277
1278/**
1279 * Sets (replaces) the page flags for a range of pages in the guest's tables.
1280 *
1281 * @returns VBox status.
1282 * @param pVCpu VMCPU handle.
1283 * @param GCPtr The address of the first page.
1284 * @param cb The size of the range in bytes.
1285 * @param fFlags Page flags X86_PTE_*, excluding the page mask of course.
1286 */
1287VMMDECL(int) PGMGstSetPage(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cb, uint64_t fFlags)
1288{
1289 return PGMGstModifyPage(pVCpu, GCPtr, cb, fFlags, 0);
1290}
1291
1292
1293/**
1294 * Modify page flags for a range of pages in the guest's tables
1295 *
1296 * The existing flags are ANDed with the fMask and ORed with the fFlags.
1297 *
1298 * @returns VBox status code.
1299 * @param pVCpu VMCPU handle.
1300 * @param GCPtr Virtual address of the first page in the range.
1301 * @param cb Size (in bytes) of the range to apply the modification to.
1302 * @param fFlags The OR mask - page flags X86_PTE_*, excluding the page mask of course.
1303 * @param fMask The AND mask - page flags X86_PTE_*, excluding the page mask of course.
1304 * Be very CAREFUL when ~'ing constants which could be 32-bit!
1305 */
1306VMMDECL(int) PGMGstModifyPage(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cb, uint64_t fFlags, uint64_t fMask)
1307{
1308 STAM_PROFILE_START(&pVCpu->pgm.s.CTX_MID_Z(Stat,GstModifyPage), a);
1309
1310 /*
1311 * Validate input.
1312 */
1313 AssertMsg(!(fFlags & X86_PTE_PAE_PG_MASK), ("fFlags=%#llx\n", fFlags));
1314 Assert(cb);
1315
1316 LogFlow(("PGMGstModifyPage %RGv %d bytes fFlags=%08llx fMask=%08llx\n", GCPtr, cb, fFlags, fMask));
1317
1318 /*
1319 * Adjust input.
1320 */
1321 cb += GCPtr & PAGE_OFFSET_MASK;
1322 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
1323 GCPtr = (GCPtr & PAGE_BASE_GC_MASK);
1324
1325 /*
1326 * Call worker.
1327 */
1328 int rc = PGM_GST_PFN(ModifyPage, pVCpu)(pVCpu, GCPtr, cb, fFlags, fMask);
1329
1330 STAM_PROFILE_STOP(&pVCpu->pgm.s.CTX_MID_Z(Stat,GstModifyPage), a);
1331 return rc;
1332}
1333
1334#ifdef IN_RING3
1335
1336/**
1337 * Performs the lazy mapping of the 32-bit guest PD.
1338 *
1339 * @returns Pointer to the mapping.
1340 * @param pPGM The PGM instance data.
1341 */
1342PX86PD pgmGstLazyMap32BitPD(PPGMCPU pPGM)
1343{
1344 Assert(!pPGM->CTX_SUFF(pGst32BitPd));
1345 PVM pVM = PGMCPU2VM(pPGM);
1346 pgmLock(pVM);
1347
1348 PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, pPGM->GCPhysCR3);
1349 AssertReturn(pPage, NULL);
1350
1351 RTHCPTR HCPtrGuestCR3;
1352 int rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, pPGM->GCPhysCR3 & X86_CR3_PAGE_MASK, (void **)&HCPtrGuestCR3);
1353 AssertRCReturn(rc, NULL);
1354
1355 pPGM->pGst32BitPdR3 = (R3PTRTYPE(PX86PD))HCPtrGuestCR3;
1356# ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
1357 pPGM->pGst32BitPdR0 = (R0PTRTYPE(PX86PD))HCPtrGuestCR3;
1358# endif
1359
1360 pgmUnlock(pVM);
1361 return pPGM->CTX_SUFF(pGst32BitPd);
1362}
1363
1364
1365/**
1366 * Performs the lazy mapping of the PAE guest PDPT.
1367 *
1368 * @returns Pointer to the mapping.
1369 * @param pPGM The PGM instance data.
1370 */
1371PX86PDPT pgmGstLazyMapPaePDPT(PPGMCPU pPGM)
1372{
1373 Assert(!pPGM->CTX_SUFF(pGstPaePdpt));
1374 PVM pVM = PGMCPU2VM(pPGM);
1375 pgmLock(pVM);
1376
1377 PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, pPGM->GCPhysCR3);
1378 AssertReturn(pPage, NULL);
1379
1380 RTHCPTR HCPtrGuestCR3;
1381 int rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, pPGM->GCPhysCR3 & X86_CR3_PAE_PAGE_MASK, (void **)&HCPtrGuestCR3); /** @todo r=bird: This GCPhysR3 masking isn't necessary. */
1382 AssertRCReturn(rc, NULL);
1383
1384 pPGM->pGstPaePdptR3 = (R3PTRTYPE(PX86PDPT))HCPtrGuestCR3;
1385# ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
1386 pPGM->pGstPaePdptR0 = (R0PTRTYPE(PX86PDPT))HCPtrGuestCR3;
1387# endif
1388
1389 pgmUnlock(pVM);
1390 return pPGM->CTX_SUFF(pGstPaePdpt);
1391}
1392
1393#endif /* IN_RING3 */
1394
1395#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
1396/**
1397 * Performs the lazy mapping / updating of a PAE guest PD.
1398 *
1399 * @returns Pointer to the mapping.
1400 * @param pPGM The PGM instance data.
1401 * @param iPdpt Which PD entry to map (0..3).
1402 */
1403PX86PDPAE pgmGstLazyMapPaePD(PPGMCPU pPGM, uint32_t iPdpt)
1404{
1405 PVM pVM = PGMCPU2VM(pPGM);
1406 pgmLock(pVM);
1407
1408 PX86PDPT pGuestPDPT = pPGM->CTX_SUFF(pGstPaePdpt);
1409 Assert(pGuestPDPT);
1410 Assert(pGuestPDPT->a[iPdpt].n.u1Present);
1411 RTGCPHYS GCPhys = pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK;
1412 bool const fChanged = pPGM->aGCPhysGstPaePDs[iPdpt] != GCPhys;
1413
1414 PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhys);
1415 if (RT_LIKELY(pPage))
1416 {
1417 int rc = VINF_SUCCESS;
1418 RTRCPTR RCPtr = NIL_RTRCPTR;
1419 RTHCPTR HCPtr = NIL_RTHCPTR;
1420#if !defined(IN_RC) && !defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
1421 rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &HCPtr);
1422 AssertRC(rc);
1423#endif
1424 if (RT_SUCCESS(rc) && fChanged)
1425 {
1426 RCPtr = (RTRCPTR)(RTRCUINTPTR)(pVM->pgm.s.GCPtrCR3Mapping + (1 + iPdpt) * PAGE_SIZE);
1427 rc = PGMMap(pVM, (RTRCUINTPTR)RCPtr, PGM_PAGE_GET_HCPHYS(pPage), PAGE_SIZE, 0);
1428 }
1429 if (RT_SUCCESS(rc))
1430 {
1431 pPGM->apGstPaePDsR3[iPdpt] = (R3PTRTYPE(PX86PDPAE))HCPtr;
1432# ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
1433 pPGM->apGstPaePDsR0[iPdpt] = (R0PTRTYPE(PX86PDPAE))HCPtr;
1434# endif
1435 if (fChanged)
1436 {
1437 pPGM->aGCPhysGstPaePDs[iPdpt] = GCPhys;
1438 pPGM->apGstPaePDsRC[iPdpt] = (RCPTRTYPE(PX86PDPAE))RCPtr;
1439 }
1440
1441 pgmUnlock(pVM);
1442 return pPGM->CTX_SUFF(apGstPaePDs)[iPdpt];
1443 }
1444 }
1445
1446 /* Invalid page or some failure, invalidate the entry. */
1447 pPGM->aGCPhysGstPaePDs[iPdpt] = NIL_RTGCPHYS;
1448 pPGM->apGstPaePDsR3[iPdpt] = 0;
1449# ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
1450 pPGM->apGstPaePDsR0[iPdpt] = 0;
1451# endif
1452 pPGM->apGstPaePDsRC[iPdpt] = 0;
1453
1454 pgmUnlock(pVM);
1455 return NULL;
1456}
1457#endif /* !VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
1458
1459
1460#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R3
1461/**
1462 * Performs the lazy mapping of the 32-bit guest PD.
1463 *
1464 * @returns Pointer to the mapping.
1465 * @param pPGM The PGM instance data.
1466 */
1467PX86PML4 pgmGstLazyMapPml4(PPGMCPU pPGM)
1468{
1469 Assert(!pPGM->CTX_SUFF(pGstAmd64Pml4));
1470 PVM pVM = PGMCPU2VM(pPGM);
1471 pgmLock(pVM);
1472
1473 PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, pPGM->GCPhysCR3);
1474 AssertReturn(pPage, NULL);
1475
1476 RTHCPTR HCPtrGuestCR3;
1477 int rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, pPGM->GCPhysCR3 & X86_CR3_AMD64_PAGE_MASK, (void **)&HCPtrGuestCR3); /** @todo r=bird: This GCPhysCR3 masking isn't necessary. */
1478 AssertRCReturn(rc, NULL);
1479
1480 pPGM->pGstAmd64Pml4R3 = (R3PTRTYPE(PX86PML4))HCPtrGuestCR3;
1481# ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
1482 pPGM->pGstAmd64Pml4R0 = (R0PTRTYPE(PX86PML4))HCPtrGuestCR3;
1483# endif
1484
1485 pgmUnlock(pVM);
1486 return pPGM->CTX_SUFF(pGstAmd64Pml4);
1487}
1488#endif /* VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R3 */
1489
1490
1491/**
1492 * Gets the specified page directory pointer table entry.
1493 *
1494 * @returns PDP entry
1495 * @param pVCpu VMCPU handle.
1496 * @param iPdpt PDPT index
1497 */
1498VMMDECL(X86PDPE) PGMGstGetPaePDPtr(PVMCPU pVCpu, unsigned iPdpt)
1499{
1500 Assert(iPdpt <= 3);
1501 return pgmGstGetPaePDPTPtr(&pVCpu->pgm.s)->a[iPdpt & 3];
1502}
1503
1504
1505/**
1506 * Gets the current CR3 register value for the shadow memory context.
1507 * @returns CR3 value.
1508 * @param pVCpu VMCPU handle.
1509 */
1510VMMDECL(RTHCPHYS) PGMGetHyperCR3(PVMCPU pVCpu)
1511{
1512 PPGMPOOLPAGE pPoolPage = pVCpu->pgm.s.CTX_SUFF(pShwPageCR3);
1513 AssertPtrReturn(pPoolPage, 0);
1514 return pPoolPage->Core.Key;
1515}
1516
1517
1518/**
1519 * Gets the current CR3 register value for the nested memory context.
1520 * @returns CR3 value.
1521 * @param pVCpu VMCPU handle.
1522 */
1523VMMDECL(RTHCPHYS) PGMGetNestedCR3(PVMCPU pVCpu, PGMMODE enmShadowMode)
1524{
1525 Assert(pVCpu->pgm.s.CTX_SUFF(pShwPageCR3));
1526 return pVCpu->pgm.s.CTX_SUFF(pShwPageCR3)->Core.Key;
1527}
1528
1529
1530/**
1531 * Gets the current CR3 register value for the HC intermediate memory context.
1532 * @returns CR3 value.
1533 * @param pVM The VM handle.
1534 */
1535VMMDECL(RTHCPHYS) PGMGetInterHCCR3(PVM pVM)
1536{
1537 switch (pVM->pgm.s.enmHostMode)
1538 {
1539 case SUPPAGINGMODE_32_BIT:
1540 case SUPPAGINGMODE_32_BIT_GLOBAL:
1541 return pVM->pgm.s.HCPhysInterPD;
1542
1543 case SUPPAGINGMODE_PAE:
1544 case SUPPAGINGMODE_PAE_GLOBAL:
1545 case SUPPAGINGMODE_PAE_NX:
1546 case SUPPAGINGMODE_PAE_GLOBAL_NX:
1547 return pVM->pgm.s.HCPhysInterPaePDPT;
1548
1549 case SUPPAGINGMODE_AMD64:
1550 case SUPPAGINGMODE_AMD64_GLOBAL:
1551 case SUPPAGINGMODE_AMD64_NX:
1552 case SUPPAGINGMODE_AMD64_GLOBAL_NX:
1553 return pVM->pgm.s.HCPhysInterPaePDPT;
1554
1555 default:
1556 AssertMsgFailed(("enmHostMode=%d\n", pVM->pgm.s.enmHostMode));
1557 return ~0;
1558 }
1559}
1560
1561
1562/**
1563 * Gets the current CR3 register value for the RC intermediate memory context.
1564 * @returns CR3 value.
1565 * @param pVM The VM handle.
1566 * @param pVCpu VMCPU handle.
1567 */
1568VMMDECL(RTHCPHYS) PGMGetInterRCCR3(PVM pVM, PVMCPU pVCpu)
1569{
1570 switch (pVCpu->pgm.s.enmShadowMode)
1571 {
1572 case PGMMODE_32_BIT:
1573 return pVM->pgm.s.HCPhysInterPD;
1574
1575 case PGMMODE_PAE:
1576 case PGMMODE_PAE_NX:
1577 return pVM->pgm.s.HCPhysInterPaePDPT;
1578
1579 case PGMMODE_AMD64:
1580 case PGMMODE_AMD64_NX:
1581 return pVM->pgm.s.HCPhysInterPaePML4;
1582
1583 case PGMMODE_EPT:
1584 case PGMMODE_NESTED:
1585 return 0; /* not relevant */
1586
1587 default:
1588 AssertMsgFailed(("enmShadowMode=%d\n", pVCpu->pgm.s.enmShadowMode));
1589 return ~0;
1590 }
1591}
1592
1593
1594/**
1595 * Gets the CR3 register value for the 32-Bit intermediate memory context.
1596 * @returns CR3 value.
1597 * @param pVM The VM handle.
1598 */
1599VMMDECL(RTHCPHYS) PGMGetInter32BitCR3(PVM pVM)
1600{
1601 return pVM->pgm.s.HCPhysInterPD;
1602}
1603
1604
1605/**
1606 * Gets the CR3 register value for the PAE intermediate memory context.
1607 * @returns CR3 value.
1608 * @param pVM The VM handle.
1609 */
1610VMMDECL(RTHCPHYS) PGMGetInterPaeCR3(PVM pVM)
1611{
1612 return pVM->pgm.s.HCPhysInterPaePDPT;
1613}
1614
1615
1616/**
1617 * Gets the CR3 register value for the AMD64 intermediate memory context.
1618 * @returns CR3 value.
1619 * @param pVM The VM handle.
1620 */
1621VMMDECL(RTHCPHYS) PGMGetInterAmd64CR3(PVM pVM)
1622{
1623 return pVM->pgm.s.HCPhysInterPaePML4;
1624}
1625
1626
1627/**
1628 * Performs and schedules necessary updates following a CR3 load or reload.
1629 *
1630 * This will normally involve mapping the guest PD or nPDPT
1631 *
1632 * @returns VBox status code.
1633 * @retval VINF_PGM_SYNC_CR3 if monitoring requires a CR3 sync. This can
1634 * safely be ignored and overridden since the FF will be set too then.
1635 * @param pVCpu VMCPU handle.
1636 * @param cr3 The new cr3.
1637 * @param fGlobal Indicates whether this is a global flush or not.
1638 */
1639VMMDECL(int) PGMFlushTLB(PVMCPU pVCpu, uint64_t cr3, bool fGlobal)
1640{
1641 PVM pVM = pVCpu->CTX_SUFF(pVM);
1642
1643 STAM_PROFILE_START(&pVCpu->pgm.s.CTX_MID_Z(Stat,FlushTLB), a);
1644
1645 /*
1646 * Always flag the necessary updates; necessary for hardware acceleration
1647 */
1648 /** @todo optimize this, it shouldn't always be necessary. */
1649 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL);
1650 if (fGlobal)
1651 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
1652 LogFlow(("PGMFlushTLB: cr3=%RX64 OldCr3=%RX64 fGlobal=%d\n", cr3, pVCpu->pgm.s.GCPhysCR3, fGlobal));
1653
1654 /*
1655 * Remap the CR3 content and adjust the monitoring if CR3 was actually changed.
1656 */
1657 int rc = VINF_SUCCESS;
1658 RTGCPHYS GCPhysCR3;
1659 switch (pVCpu->pgm.s.enmGuestMode)
1660 {
1661 case PGMMODE_PAE:
1662 case PGMMODE_PAE_NX:
1663 GCPhysCR3 = (RTGCPHYS)(cr3 & X86_CR3_PAE_PAGE_MASK);
1664 break;
1665 case PGMMODE_AMD64:
1666 case PGMMODE_AMD64_NX:
1667 GCPhysCR3 = (RTGCPHYS)(cr3 & X86_CR3_AMD64_PAGE_MASK);
1668 break;
1669 default:
1670 GCPhysCR3 = (RTGCPHYS)(cr3 & X86_CR3_PAGE_MASK);
1671 break;
1672 }
1673
1674 if (pVCpu->pgm.s.GCPhysCR3 != GCPhysCR3)
1675 {
1676 RTGCPHYS GCPhysOldCR3 = pVCpu->pgm.s.GCPhysCR3;
1677 pVCpu->pgm.s.GCPhysCR3 = GCPhysCR3;
1678 rc = PGM_BTH_PFN(MapCR3, pVCpu)(pVCpu, GCPhysCR3);
1679 if (RT_LIKELY(rc == VINF_SUCCESS))
1680 {
1681 if (!pVM->pgm.s.fMappingsFixed)
1682 {
1683 pVCpu->pgm.s.fSyncFlags &= ~PGM_SYNC_MONITOR_CR3;
1684 }
1685 }
1686 else
1687 {
1688 AssertMsg(rc == VINF_PGM_SYNC_CR3, ("%Rrc\n", rc));
1689 Assert(VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL | VMCPU_FF_PGM_SYNC_CR3));
1690 pVCpu->pgm.s.GCPhysCR3 = GCPhysOldCR3;
1691 pVCpu->pgm.s.fSyncFlags |= PGM_SYNC_MAP_CR3;
1692 if (!pVM->pgm.s.fMappingsFixed)
1693 pVCpu->pgm.s.fSyncFlags |= PGM_SYNC_MONITOR_CR3;
1694 }
1695
1696 if (fGlobal)
1697 STAM_COUNTER_INC(&pVCpu->pgm.s.CTX_MID_Z(Stat,FlushTLBNewCR3Global));
1698 else
1699 STAM_COUNTER_INC(&pVCpu->pgm.s.CTX_MID_Z(Stat,FlushTLBNewCR3));
1700 }
1701 else
1702 {
1703# ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
1704 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
1705 if (pPool->cDirtyPages)
1706 {
1707 pgmLock(pVM);
1708 pgmPoolResetDirtyPages(pVM);
1709 pgmUnlock(pVM);
1710 }
1711# endif
1712 /*
1713 * Check if we have a pending update of the CR3 monitoring.
1714 */
1715 if (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_MONITOR_CR3)
1716 {
1717 pVCpu->pgm.s.fSyncFlags &= ~PGM_SYNC_MONITOR_CR3;
1718 Assert(!pVM->pgm.s.fMappingsFixed);
1719 }
1720 if (fGlobal)
1721 STAM_COUNTER_INC(&pVCpu->pgm.s.CTX_MID_Z(Stat,FlushTLBSameCR3Global));
1722 else
1723 STAM_COUNTER_INC(&pVCpu->pgm.s.CTX_MID_Z(Stat,FlushTLBSameCR3));
1724 }
1725
1726 STAM_PROFILE_STOP(&pVCpu->pgm.s.CTX_MID_Z(Stat,FlushTLB), a);
1727 return rc;
1728}
1729
1730
1731/**
1732 * Performs and schedules necessary updates following a CR3 load or reload when
1733 * using nested or extended paging.
1734 *
1735 * This API is an alterantive to PDMFlushTLB that avoids actually flushing the
1736 * TLB and triggering a SyncCR3.
1737 *
1738 * This will normally involve mapping the guest PD or nPDPT
1739 *
1740 * @returns VBox status code.
1741 * @retval VINF_SUCCESS.
1742 * @retval (If applied when not in nested mode: VINF_PGM_SYNC_CR3 if monitoring
1743 * requires a CR3 sync. This can safely be ignored and overridden since
1744 * the FF will be set too then.)
1745 * @param pVCpu VMCPU handle.
1746 * @param cr3 The new cr3.
1747 */
1748VMMDECL(int) PGMUpdateCR3(PVMCPU pVCpu, uint64_t cr3)
1749{
1750 PVM pVM = pVCpu->CTX_SUFF(pVM);
1751
1752 LogFlow(("PGMUpdateCR3: cr3=%RX64 OldCr3=%RX64\n", cr3, pVCpu->pgm.s.GCPhysCR3));
1753
1754 /* We assume we're only called in nested paging mode. */
1755 Assert(pVM->pgm.s.fMappingsFixed);
1756 Assert(!(pVCpu->pgm.s.fSyncFlags & PGM_SYNC_MONITOR_CR3));
1757 Assert(HWACCMIsNestedPagingActive(pVM) || pVCpu->pgm.s.enmShadowMode == PGMMODE_EPT);
1758
1759 /*
1760 * Remap the CR3 content and adjust the monitoring if CR3 was actually changed.
1761 */
1762 int rc = VINF_SUCCESS;
1763 RTGCPHYS GCPhysCR3;
1764 switch (pVCpu->pgm.s.enmGuestMode)
1765 {
1766 case PGMMODE_PAE:
1767 case PGMMODE_PAE_NX:
1768 GCPhysCR3 = (RTGCPHYS)(cr3 & X86_CR3_PAE_PAGE_MASK);
1769 break;
1770 case PGMMODE_AMD64:
1771 case PGMMODE_AMD64_NX:
1772 GCPhysCR3 = (RTGCPHYS)(cr3 & X86_CR3_AMD64_PAGE_MASK);
1773 break;
1774 default:
1775 GCPhysCR3 = (RTGCPHYS)(cr3 & X86_CR3_PAGE_MASK);
1776 break;
1777 }
1778 if (pVCpu->pgm.s.GCPhysCR3 != GCPhysCR3)
1779 {
1780 pVCpu->pgm.s.GCPhysCR3 = GCPhysCR3;
1781 rc = PGM_BTH_PFN(MapCR3, pVCpu)(pVCpu, GCPhysCR3);
1782 AssertRCSuccess(rc); /* Assumes VINF_PGM_SYNC_CR3 doesn't apply to nested paging. */ /** @todo this isn't true for the mac, but we need hw to test/fix this. */
1783 }
1784 return rc;
1785}
1786
1787
1788/**
1789 * Synchronize the paging structures.
1790 *
1791 * This function is called in response to the VM_FF_PGM_SYNC_CR3 and
1792 * VM_FF_PGM_SYNC_CR3_NONGLOBAL. Those two force action flags are set
1793 * in several places, most importantly whenever the CR3 is loaded.
1794 *
1795 * @returns VBox status code.
1796 * @param pVCpu VMCPU handle.
1797 * @param cr0 Guest context CR0 register
1798 * @param cr3 Guest context CR3 register
1799 * @param cr4 Guest context CR4 register
1800 * @param fGlobal Including global page directories or not
1801 */
1802VMMDECL(int) PGMSyncCR3(PVMCPU pVCpu, uint64_t cr0, uint64_t cr3, uint64_t cr4, bool fGlobal)
1803{
1804 PVM pVM = pVCpu->CTX_SUFF(pVM);
1805 int rc;
1806
1807#ifdef PGMPOOL_WITH_MONITORING
1808 /*
1809 * The pool may have pending stuff and even require a return to ring-3 to
1810 * clear the whole thing.
1811 */
1812 rc = pgmPoolSyncCR3(pVCpu);
1813 if (rc != VINF_SUCCESS)
1814 return rc;
1815#endif
1816
1817 /*
1818 * We might be called when we shouldn't.
1819 *
1820 * The mode switching will ensure that the PD is resynced
1821 * after every mode switch. So, if we find ourselves here
1822 * when in protected or real mode we can safely disable the
1823 * FF and return immediately.
1824 */
1825 if (pVCpu->pgm.s.enmGuestMode <= PGMMODE_PROTECTED)
1826 {
1827 Assert((cr0 & (X86_CR0_PG | X86_CR0_PE)) != (X86_CR0_PG | X86_CR0_PE));
1828 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
1829 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL);
1830 return VINF_SUCCESS;
1831 }
1832
1833 /* If global pages are not supported, then all flushes are global. */
1834 if (!(cr4 & X86_CR4_PGE))
1835 fGlobal = true;
1836 LogFlow(("PGMSyncCR3: cr0=%RX64 cr3=%RX64 cr4=%RX64 fGlobal=%d[%d,%d]\n", cr0, cr3, cr4, fGlobal,
1837 VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3), VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL)));
1838
1839 /*
1840 * Check if we need to finish an aborted MapCR3 call (see PGMFlushTLB).
1841 * This should be done before SyncCR3.
1842 */
1843 if (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_MAP_CR3)
1844 {
1845 pVCpu->pgm.s.fSyncFlags &= ~PGM_SYNC_MAP_CR3;
1846
1847 RTGCPHYS GCPhysCR3Old = pVCpu->pgm.s.GCPhysCR3;
1848 RTGCPHYS GCPhysCR3;
1849 switch (pVCpu->pgm.s.enmGuestMode)
1850 {
1851 case PGMMODE_PAE:
1852 case PGMMODE_PAE_NX:
1853 GCPhysCR3 = (RTGCPHYS)(cr3 & X86_CR3_PAE_PAGE_MASK);
1854 break;
1855 case PGMMODE_AMD64:
1856 case PGMMODE_AMD64_NX:
1857 GCPhysCR3 = (RTGCPHYS)(cr3 & X86_CR3_AMD64_PAGE_MASK);
1858 break;
1859 default:
1860 GCPhysCR3 = (RTGCPHYS)(cr3 & X86_CR3_PAGE_MASK);
1861 break;
1862 }
1863
1864 if (pVCpu->pgm.s.GCPhysCR3 != GCPhysCR3)
1865 {
1866 pVCpu->pgm.s.GCPhysCR3 = GCPhysCR3;
1867 rc = PGM_BTH_PFN(MapCR3, pVCpu)(pVCpu, GCPhysCR3);
1868 }
1869#ifdef IN_RING3
1870 if (rc == VINF_PGM_SYNC_CR3)
1871 rc = pgmPoolSyncCR3(pVCpu);
1872#else
1873 if (rc == VINF_PGM_SYNC_CR3)
1874 {
1875 pVCpu->pgm.s.GCPhysCR3 = GCPhysCR3Old;
1876 return rc;
1877 }
1878#endif
1879 AssertRCReturn(rc, rc);
1880 AssertRCSuccessReturn(rc, VERR_INTERNAL_ERROR);
1881 }
1882
1883 /*
1884 * Let the 'Bth' function do the work and we'll just keep track of the flags.
1885 */
1886 STAM_PROFILE_START(&pVCpu->pgm.s.CTX_MID_Z(Stat,SyncCR3), a);
1887 rc = PGM_BTH_PFN(SyncCR3, pVCpu)(pVCpu, cr0, cr3, cr4, fGlobal);
1888 STAM_PROFILE_STOP(&pVCpu->pgm.s.CTX_MID_Z(Stat,SyncCR3), a);
1889 AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 || RT_FAILURE(rc), ("rc=%Rrc\n", rc));
1890 if (rc == VINF_SUCCESS)
1891 {
1892 if (!(pVCpu->pgm.s.fSyncFlags & PGM_SYNC_ALWAYS))
1893 {
1894 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
1895 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL);
1896 }
1897
1898 /*
1899 * Check if we have a pending update of the CR3 monitoring.
1900 */
1901 if (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_MONITOR_CR3)
1902 {
1903 pVCpu->pgm.s.fSyncFlags &= ~PGM_SYNC_MONITOR_CR3;
1904 Assert(!pVM->pgm.s.fMappingsFixed);
1905 }
1906 }
1907
1908 /*
1909 * Now flush the CR3 (guest context).
1910 */
1911 if (rc == VINF_SUCCESS)
1912 PGM_INVL_VCPU_TLBS(pVCpu);
1913 return rc;
1914}
1915
1916
1917/**
1918 * Called whenever CR0 or CR4 in a way which may change
1919 * the paging mode.
1920 *
1921 * @returns VBox status code, with the following informational code for
1922 * VM scheduling.
1923 * @retval VINF_SUCCESS if the was no change, or it was successfully dealt with.
1924 * @retval VINF_PGM_CHANGE_MODE if we're in RC or R0 and the mode changes.
1925 * (I.e. not in R3.)
1926 * @retval VINF_EM_SUSPEND or VINF_EM_OFF on a fatal runtime error. (R3 only)
1927 *
1928 * @param pVCpu VMCPU handle.
1929 * @param cr0 The new cr0.
1930 * @param cr4 The new cr4.
1931 * @param efer The new extended feature enable register.
1932 */
1933VMMDECL(int) PGMChangeMode(PVMCPU pVCpu, uint64_t cr0, uint64_t cr4, uint64_t efer)
1934{
1935 PVM pVM = pVCpu->CTX_SUFF(pVM);
1936 PGMMODE enmGuestMode;
1937
1938 /*
1939 * Calc the new guest mode.
1940 */
1941 if (!(cr0 & X86_CR0_PE))
1942 enmGuestMode = PGMMODE_REAL;
1943 else if (!(cr0 & X86_CR0_PG))
1944 enmGuestMode = PGMMODE_PROTECTED;
1945 else if (!(cr4 & X86_CR4_PAE))
1946 enmGuestMode = PGMMODE_32_BIT;
1947 else if (!(efer & MSR_K6_EFER_LME))
1948 {
1949 if (!(efer & MSR_K6_EFER_NXE))
1950 enmGuestMode = PGMMODE_PAE;
1951 else
1952 enmGuestMode = PGMMODE_PAE_NX;
1953 }
1954 else
1955 {
1956 if (!(efer & MSR_K6_EFER_NXE))
1957 enmGuestMode = PGMMODE_AMD64;
1958 else
1959 enmGuestMode = PGMMODE_AMD64_NX;
1960 }
1961
1962 /*
1963 * Did it change?
1964 */
1965 if (pVCpu->pgm.s.enmGuestMode == enmGuestMode)
1966 return VINF_SUCCESS;
1967
1968 /* Flush the TLB */
1969 PGM_INVL_VCPU_TLBS(pVCpu);
1970
1971#ifdef IN_RING3
1972 return PGMR3ChangeMode(pVM, pVCpu, enmGuestMode);
1973#else
1974 LogFlow(("PGMChangeMode: returns VINF_PGM_CHANGE_MODE.\n"));
1975 return VINF_PGM_CHANGE_MODE;
1976#endif
1977}
1978
1979
1980/**
1981 * Gets the current guest paging mode.
1982 *
1983 * If you just need the CPU mode (real/protected/long), use CPUMGetGuestMode().
1984 *
1985 * @returns The current paging mode.
1986 * @param pVCpu VMCPU handle.
1987 */
1988VMMDECL(PGMMODE) PGMGetGuestMode(PVMCPU pVCpu)
1989{
1990 return pVCpu->pgm.s.enmGuestMode;
1991}
1992
1993
1994/**
1995 * Gets the current shadow paging mode.
1996 *
1997 * @returns The current paging mode.
1998 * @param pVCpu VMCPU handle.
1999 */
2000VMMDECL(PGMMODE) PGMGetShadowMode(PVMCPU pVCpu)
2001{
2002 return pVCpu->pgm.s.enmShadowMode;
2003}
2004
2005/**
2006 * Gets the current host paging mode.
2007 *
2008 * @returns The current paging mode.
2009 * @param pVM The VM handle.
2010 */
2011VMMDECL(PGMMODE) PGMGetHostMode(PVM pVM)
2012{
2013 switch (pVM->pgm.s.enmHostMode)
2014 {
2015 case SUPPAGINGMODE_32_BIT:
2016 case SUPPAGINGMODE_32_BIT_GLOBAL:
2017 return PGMMODE_32_BIT;
2018
2019 case SUPPAGINGMODE_PAE:
2020 case SUPPAGINGMODE_PAE_GLOBAL:
2021 return PGMMODE_PAE;
2022
2023 case SUPPAGINGMODE_PAE_NX:
2024 case SUPPAGINGMODE_PAE_GLOBAL_NX:
2025 return PGMMODE_PAE_NX;
2026
2027 case SUPPAGINGMODE_AMD64:
2028 case SUPPAGINGMODE_AMD64_GLOBAL:
2029 return PGMMODE_AMD64;
2030
2031 case SUPPAGINGMODE_AMD64_NX:
2032 case SUPPAGINGMODE_AMD64_GLOBAL_NX:
2033 return PGMMODE_AMD64_NX;
2034
2035 default: AssertMsgFailed(("enmHostMode=%d\n", pVM->pgm.s.enmHostMode)); break;
2036 }
2037
2038 return PGMMODE_INVALID;
2039}
2040
2041
2042/**
2043 * Get mode name.
2044 *
2045 * @returns read-only name string.
2046 * @param enmMode The mode which name is desired.
2047 */
2048VMMDECL(const char *) PGMGetModeName(PGMMODE enmMode)
2049{
2050 switch (enmMode)
2051 {
2052 case PGMMODE_REAL: return "Real";
2053 case PGMMODE_PROTECTED: return "Protected";
2054 case PGMMODE_32_BIT: return "32-bit";
2055 case PGMMODE_PAE: return "PAE";
2056 case PGMMODE_PAE_NX: return "PAE+NX";
2057 case PGMMODE_AMD64: return "AMD64";
2058 case PGMMODE_AMD64_NX: return "AMD64+NX";
2059 case PGMMODE_NESTED: return "Nested";
2060 case PGMMODE_EPT: return "EPT";
2061 default: return "unknown mode value";
2062 }
2063}
2064
2065
2066/**
2067 * Check if the PGM lock is currently taken.
2068 *
2069 * @returns bool locked/not locked
2070 * @param pVM The VM to operate on.
2071 */
2072VMMDECL(bool) PGMIsLocked(PVM pVM)
2073{
2074 return PDMCritSectIsOwned(&pVM->pgm.s.CritSect);
2075}
2076
2077
2078/**
2079 * Check if this VCPU currently owns the PGM lock.
2080 *
2081 * @returns bool owner/not owner
2082 * @param pVM The VM to operate on.
2083 */
2084VMMDECL(bool) PGMIsLockOwner(PVM pVM)
2085{
2086 return PDMCritSectIsOwner(&pVM->pgm.s.CritSect);
2087}
2088
2089
2090/**
2091 * Acquire the PGM lock.
2092 *
2093 * @returns VBox status code
2094 * @param pVM The VM to operate on.
2095 */
2096int pgmLock(PVM pVM)
2097{
2098 int rc = PDMCritSectEnter(&pVM->pgm.s.CritSect, VERR_SEM_BUSY);
2099#if defined(IN_RC) || defined(IN_RING0)
2100 if (rc == VERR_SEM_BUSY)
2101 rc = VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_PGM_LOCK, 0);
2102#endif
2103 AssertMsg(rc == VINF_SUCCESS, ("%Rrc\n", rc));
2104 return rc;
2105}
2106
2107
2108/**
2109 * Release the PGM lock.
2110 *
2111 * @returns VBox status code
2112 * @param pVM The VM to operate on.
2113 */
2114void pgmUnlock(PVM pVM)
2115{
2116 PDMCritSectLeave(&pVM->pgm.s.CritSect);
2117}
2118
2119#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
2120
2121/**
2122 * Temporarily maps one guest page specified by GC physical address.
2123 * These pages must have a physical mapping in HC, i.e. they cannot be MMIO pages.
2124 *
2125 * Be WARNED that the dynamic page mapping area is small, 8 pages, thus the space is
2126 * reused after 8 mappings (or perhaps a few more if you score with the cache).
2127 *
2128 * @returns VBox status.
2129 * @param pVM VM handle.
2130 * @param GCPhys GC Physical address of the page.
2131 * @param ppv Where to store the address of the mapping.
2132 */
2133VMMDECL(int) PGMDynMapGCPage(PVM pVM, RTGCPHYS GCPhys, void **ppv)
2134{
2135 AssertMsg(!(GCPhys & PAGE_OFFSET_MASK), ("GCPhys=%RGp\n", GCPhys));
2136
2137 /*
2138 * Get the ram range.
2139 */
2140 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
2141 while (pRam && GCPhys - pRam->GCPhys >= pRam->cb)
2142 pRam = pRam->CTX_SUFF(pNext);
2143 if (!pRam)
2144 {
2145 AssertMsgFailed(("Invalid physical address %RGp!\n", GCPhys));
2146 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
2147 }
2148
2149 /*
2150 * Pass it on to PGMDynMapHCPage.
2151 */
2152 RTHCPHYS HCPhys = PGM_PAGE_GET_HCPHYS(&pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT]);
2153 //Log(("PGMDynMapGCPage: GCPhys=%RGp HCPhys=%RHp\n", GCPhys, HCPhys));
2154#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
2155 pgmR0DynMapHCPageInlined(&pVM->pgm.s, HCPhys, ppv);
2156#else
2157 PGMDynMapHCPage(pVM, HCPhys, ppv);
2158#endif
2159 return VINF_SUCCESS;
2160}
2161
2162
2163/**
2164 * Temporarily maps one guest page specified by unaligned GC physical address.
2165 * These pages must have a physical mapping in HC, i.e. they cannot be MMIO pages.
2166 *
2167 * Be WARNED that the dynamic page mapping area is small, 8 pages, thus the space is
2168 * reused after 8 mappings (or perhaps a few more if you score with the cache).
2169 *
2170 * The caller is aware that only the speicifed page is mapped and that really bad things
2171 * will happen if writing beyond the page!
2172 *
2173 * @returns VBox status.
2174 * @param pVM VM handle.
2175 * @param GCPhys GC Physical address within the page to be mapped.
2176 * @param ppv Where to store the address of the mapping address corresponding to GCPhys.
2177 */
2178VMMDECL(int) PGMDynMapGCPageOff(PVM pVM, RTGCPHYS GCPhys, void **ppv)
2179{
2180 /*
2181 * Get the ram range.
2182 */
2183 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
2184 while (pRam && GCPhys - pRam->GCPhys >= pRam->cb)
2185 pRam = pRam->CTX_SUFF(pNext);
2186 if (!pRam)
2187 {
2188 AssertMsgFailed(("Invalid physical address %RGp!\n", GCPhys));
2189 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
2190 }
2191
2192 /*
2193 * Pass it on to PGMDynMapHCPage.
2194 */
2195 RTHCPHYS HCPhys = PGM_PAGE_GET_HCPHYS(&pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT]);
2196#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
2197 pgmR0DynMapHCPageInlined(&pVM->pgm.s, HCPhys, ppv);
2198#else
2199 PGMDynMapHCPage(pVM, HCPhys, ppv);
2200#endif
2201 *ppv = (void *)((uintptr_t)*ppv | (GCPhys & PAGE_OFFSET_MASK));
2202 return VINF_SUCCESS;
2203}
2204
2205# ifdef IN_RC
2206
2207/**
2208 * Temporarily maps one host page specified by HC physical address.
2209 *
2210 * Be WARNED that the dynamic page mapping area is small, 16 pages, thus the space is
2211 * reused after 16 mappings (or perhaps a few more if you score with the cache).
2212 *
2213 * @returns VINF_SUCCESS, will bail out to ring-3 on failure.
2214 * @param pVM VM handle.
2215 * @param HCPhys HC Physical address of the page.
2216 * @param ppv Where to store the address of the mapping. This is the
2217 * address of the PAGE not the exact address corresponding
2218 * to HCPhys. Use PGMDynMapHCPageOff if you care for the
2219 * page offset.
2220 */
2221VMMDECL(int) PGMDynMapHCPage(PVM pVM, RTHCPHYS HCPhys, void **ppv)
2222{
2223 AssertMsg(!(HCPhys & PAGE_OFFSET_MASK), ("HCPhys=%RHp\n", HCPhys));
2224
2225 /*
2226 * Check the cache.
2227 */
2228 register unsigned iCache;
2229 for (iCache = 0;iCache < RT_ELEMENTS(pVM->pgm.s.aHCPhysDynPageMapCache);iCache++)
2230 {
2231 static const uint8_t au8Trans[MM_HYPER_DYNAMIC_SIZE >> PAGE_SHIFT][RT_ELEMENTS(pVM->pgm.s.aHCPhysDynPageMapCache)] =
2232 {
2233 { 0, 9, 10, 11, 12, 13, 14, 15},
2234 { 0, 1, 10, 11, 12, 13, 14, 15},
2235 { 0, 1, 2, 11, 12, 13, 14, 15},
2236 { 0, 1, 2, 3, 12, 13, 14, 15},
2237 { 0, 1, 2, 3, 4, 13, 14, 15},
2238 { 0, 1, 2, 3, 4, 5, 14, 15},
2239 { 0, 1, 2, 3, 4, 5, 6, 15},
2240 { 0, 1, 2, 3, 4, 5, 6, 7},
2241 { 8, 1, 2, 3, 4, 5, 6, 7},
2242 { 8, 9, 2, 3, 4, 5, 6, 7},
2243 { 8, 9, 10, 3, 4, 5, 6, 7},
2244 { 8, 9, 10, 11, 4, 5, 6, 7},
2245 { 8, 9, 10, 11, 12, 5, 6, 7},
2246 { 8, 9, 10, 11, 12, 13, 6, 7},
2247 { 8, 9, 10, 11, 12, 13, 14, 7},
2248 { 8, 9, 10, 11, 12, 13, 14, 15},
2249 };
2250 AssertCompile(RT_ELEMENTS(au8Trans) == 16);
2251 AssertCompile(RT_ELEMENTS(au8Trans[0]) == 8);
2252
2253 if (pVM->pgm.s.aHCPhysDynPageMapCache[iCache] == HCPhys)
2254 {
2255 int iPage = au8Trans[pVM->pgm.s.iDynPageMapLast][iCache];
2256
2257 /* The cache can get out of sync with locked entries. (10 locked, 2 overwrites its cache position, last = 11, lookup 2 -> page 10 instead of 2) */
2258 if ((pVM->pgm.s.paDynPageMap32BitPTEsGC[iPage].u & X86_PTE_PG_MASK) == HCPhys)
2259 {
2260 void *pv = pVM->pgm.s.pbDynPageMapBaseGC + (iPage << PAGE_SHIFT);
2261 *ppv = pv;
2262 STAM_COUNTER_INC(&pVM->pgm.s.StatRCDynMapCacheHits);
2263 Log4(("PGMGCDynMapHCPage: HCPhys=%RHp pv=%p iPage=%d iCache=%d\n", HCPhys, pv, iPage, iCache));
2264 return VINF_SUCCESS;
2265 }
2266 LogFlow(("Out of sync entry %d\n", iPage));
2267 }
2268 }
2269 AssertCompile(RT_ELEMENTS(pVM->pgm.s.aHCPhysDynPageMapCache) == 8);
2270 AssertCompile((MM_HYPER_DYNAMIC_SIZE >> PAGE_SHIFT) == 16);
2271 STAM_COUNTER_INC(&pVM->pgm.s.StatRCDynMapCacheMisses);
2272
2273 /*
2274 * Update the page tables.
2275 */
2276 unsigned iPage = pVM->pgm.s.iDynPageMapLast;
2277 unsigned i;
2278 for (i = 0; i < (MM_HYPER_DYNAMIC_SIZE >> PAGE_SHIFT); i++)
2279 {
2280 pVM->pgm.s.iDynPageMapLast = iPage = (iPage + 1) & ((MM_HYPER_DYNAMIC_SIZE >> PAGE_SHIFT) - 1);
2281 if (!pVM->pgm.s.aLockedDynPageMapCache[iPage])
2282 break;
2283 iPage++;
2284 }
2285 AssertRelease(i != (MM_HYPER_DYNAMIC_SIZE >> PAGE_SHIFT));
2286
2287 pVM->pgm.s.aHCPhysDynPageMapCache[iPage & (RT_ELEMENTS(pVM->pgm.s.aHCPhysDynPageMapCache) - 1)] = HCPhys;
2288 pVM->pgm.s.paDynPageMap32BitPTEsGC[iPage].u = (uint32_t)HCPhys | X86_PTE_P | X86_PTE_A | X86_PTE_D;
2289 pVM->pgm.s.paDynPageMapPaePTEsGC[iPage].u = HCPhys | X86_PTE_P | X86_PTE_A | X86_PTE_D;
2290 pVM->pgm.s.aLockedDynPageMapCache[iPage] = 0;
2291
2292 void *pv = pVM->pgm.s.pbDynPageMapBaseGC + (iPage << PAGE_SHIFT);
2293 *ppv = pv;
2294 ASMInvalidatePage(pv);
2295 Log4(("PGMGCDynMapHCPage: HCPhys=%RHp pv=%p iPage=%d\n", HCPhys, pv, iPage));
2296 return VINF_SUCCESS;
2297}
2298
2299
2300/**
2301 * Temporarily lock a dynamic page to prevent it from being reused.
2302 *
2303 * @param pVM VM handle.
2304 * @param GCPage GC address of page
2305 */
2306VMMDECL(void) PGMDynLockHCPage(PVM pVM, RCPTRTYPE(uint8_t *) GCPage)
2307{
2308 unsigned iPage;
2309
2310 Assert(GCPage >= pVM->pgm.s.pbDynPageMapBaseGC && GCPage < (pVM->pgm.s.pbDynPageMapBaseGC + MM_HYPER_DYNAMIC_SIZE));
2311 iPage = ((uintptr_t)(GCPage - pVM->pgm.s.pbDynPageMapBaseGC)) >> PAGE_SHIFT;
2312 ASMAtomicIncU32(&pVM->pgm.s.aLockedDynPageMapCache[iPage]);
2313 Log4(("PGMDynLockHCPage %RRv iPage=%d\n", GCPage, iPage));
2314}
2315
2316
2317/**
2318 * Unlock a dynamic page
2319 *
2320 * @param pVM VM handle.
2321 * @param GCPage GC address of page
2322 */
2323VMMDECL(void) PGMDynUnlockHCPage(PVM pVM, RCPTRTYPE(uint8_t *) GCPage)
2324{
2325 unsigned iPage;
2326
2327 AssertCompile(RT_ELEMENTS(pVM->pgm.s.aLockedDynPageMapCache) == 2* RT_ELEMENTS(pVM->pgm.s.aHCPhysDynPageMapCache));
2328 AssertCompileMemberSize(VM, pgm.s.aLockedDynPageMapCache, sizeof(uint32_t) * (MM_HYPER_DYNAMIC_SIZE >> (PAGE_SHIFT)));
2329
2330 Assert(GCPage >= pVM->pgm.s.pbDynPageMapBaseGC && GCPage < (pVM->pgm.s.pbDynPageMapBaseGC + MM_HYPER_DYNAMIC_SIZE));
2331 iPage = ((uintptr_t)(GCPage - pVM->pgm.s.pbDynPageMapBaseGC)) >> PAGE_SHIFT;
2332 Assert(pVM->pgm.s.aLockedDynPageMapCache[iPage]);
2333 ASMAtomicDecU32(&pVM->pgm.s.aLockedDynPageMapCache[iPage]);
2334 Log4(("PGMDynUnlockHCPage %RRv iPage=%d\n", GCPage, iPage));
2335}
2336
2337
2338# ifdef VBOX_STRICT
2339/**
2340 * Check for lock leaks.
2341 *
2342 * @param pVM VM handle.
2343 */
2344VMMDECL(void) PGMDynCheckLocks(PVM pVM)
2345{
2346 for (unsigned i=0;i<RT_ELEMENTS(pVM->pgm.s.aLockedDynPageMapCache);i++)
2347 Assert(!pVM->pgm.s.aLockedDynPageMapCache[i]);
2348}
2349# endif /* VBOX_STRICT */
2350
2351# endif /* IN_RC */
2352#endif /* IN_RC || VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
2353
2354#if !defined(IN_R0) || defined(LOG_ENABLED)
2355
2356/** Format handler for PGMPAGE.
2357 * @copydoc FNRTSTRFORMATTYPE */
2358static DECLCALLBACK(size_t) pgmFormatTypeHandlerPage(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
2359 const char *pszType, void const *pvValue,
2360 int cchWidth, int cchPrecision, unsigned fFlags,
2361 void *pvUser)
2362{
2363 size_t cch;
2364 PCPGMPAGE pPage = (PCPGMPAGE)pvValue;
2365 if (VALID_PTR(pPage))
2366 {
2367 char szTmp[64+80];
2368
2369 cch = 0;
2370
2371 /* The single char state stuff. */
2372 static const char s_achPageStates[4] = { 'Z', 'A', 'W', 'S' };
2373 szTmp[cch++] = s_achPageStates[PGM_PAGE_GET_STATE(pPage)];
2374
2375#define IS_PART_INCLUDED(lvl) ( !(fFlags & RTSTR_F_PRECISION) || cchPrecision == (lvl) || cchPrecision >= (lvl)+10 )
2376 if (IS_PART_INCLUDED(5))
2377 {
2378 static const char s_achHandlerStates[4] = { '-', 't', 'w', 'a' };
2379 szTmp[cch++] = s_achHandlerStates[PGM_PAGE_GET_HNDL_PHYS_STATE(pPage)];
2380 szTmp[cch++] = s_achHandlerStates[PGM_PAGE_GET_HNDL_VIRT_STATE(pPage)];
2381 }
2382
2383 /* The type. */
2384 if (IS_PART_INCLUDED(4))
2385 {
2386 szTmp[cch++] = ':';
2387 static const char s_achPageTypes[8][4] = { "INV", "RAM", "MI2", "M2A", "SHA", "ROM", "MIO", "BAD" };
2388 szTmp[cch++] = s_achPageTypes[PGM_PAGE_GET_TYPE(pPage)][0];
2389 szTmp[cch++] = s_achPageTypes[PGM_PAGE_GET_TYPE(pPage)][1];
2390 szTmp[cch++] = s_achPageTypes[PGM_PAGE_GET_TYPE(pPage)][2];
2391 }
2392
2393 /* The numbers. */
2394 if (IS_PART_INCLUDED(3))
2395 {
2396 szTmp[cch++] = ':';
2397 cch += RTStrFormatNumber(&szTmp[cch], PGM_PAGE_GET_HCPHYS(pPage), 16, 12, 0, RTSTR_F_ZEROPAD | RTSTR_F_64BIT);
2398 }
2399
2400 if (IS_PART_INCLUDED(2))
2401 {
2402 szTmp[cch++] = ':';
2403 cch += RTStrFormatNumber(&szTmp[cch], PGM_PAGE_GET_PAGEID(pPage), 16, 7, 0, RTSTR_F_ZEROPAD | RTSTR_F_32BIT);
2404 }
2405
2406 if (IS_PART_INCLUDED(6))
2407 {
2408 szTmp[cch++] = ':';
2409 static const char s_achRefs[4] = { '-', 'U', '!', 'L' };
2410 szTmp[cch++] = s_achRefs[PGM_PAGE_GET_TD_CREFS(pPage)];
2411 cch += RTStrFormatNumber(&szTmp[cch], PGM_PAGE_GET_TD_IDX(pPage), 16, 4, 0, RTSTR_F_ZEROPAD | RTSTR_F_16BIT);
2412 }
2413#undef IS_PART_INCLUDED
2414
2415 cch = pfnOutput(pvArgOutput, szTmp, cch);
2416 }
2417 else
2418 cch = pfnOutput(pvArgOutput, "<bad-pgmpage-ptr>", sizeof("<bad-pgmpage-ptr>") - 1);
2419 return cch;
2420}
2421
2422
2423/** Format handler for PGMRAMRANGE.
2424 * @copydoc FNRTSTRFORMATTYPE */
2425static DECLCALLBACK(size_t) pgmFormatTypeHandlerRamRange(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
2426 const char *pszType, void const *pvValue,
2427 int cchWidth, int cchPrecision, unsigned fFlags,
2428 void *pvUser)
2429{
2430 size_t cch;
2431 PGMRAMRANGE const *pRam = (PGMRAMRANGE const *)pvValue;
2432 if (VALID_PTR(pRam))
2433 {
2434 char szTmp[80];
2435 cch = RTStrPrintf(szTmp, sizeof(szTmp), "%RGp-%RGp", pRam->GCPhys, pRam->GCPhysLast);
2436 cch = pfnOutput(pvArgOutput, szTmp, cch);
2437 }
2438 else
2439 cch = pfnOutput(pvArgOutput, "<bad-pgmramrange-ptr>", sizeof("<bad-pgmramrange-ptr>") - 1);
2440 return cch;
2441}
2442
2443/** Format type andlers to be registered/deregistered. */
2444static const struct
2445{
2446 char szType[24];
2447 PFNRTSTRFORMATTYPE pfnHandler;
2448} g_aPgmFormatTypes[] =
2449{
2450 { "pgmpage", pgmFormatTypeHandlerPage },
2451 { "pgmramrange", pgmFormatTypeHandlerRamRange }
2452};
2453
2454#endif /* !IN_R0 || LOG_ENABLED */
2455
2456
2457/**
2458 * Registers the global string format types.
2459 *
2460 * This should be called at module load time or in some other manner that ensure
2461 * that it's called exactly one time.
2462 *
2463 * @returns IPRT status code on RTStrFormatTypeRegister failure.
2464 */
2465VMMDECL(int) PGMRegisterStringFormatTypes(void)
2466{
2467#if !defined(IN_R0) || defined(LOG_ENABLED)
2468 int rc = VINF_SUCCESS;
2469 unsigned i;
2470 for (i = 0; RT_SUCCESS(rc) && i < RT_ELEMENTS(g_aPgmFormatTypes); i++)
2471 {
2472 rc = RTStrFormatTypeRegister(g_aPgmFormatTypes[i].szType, g_aPgmFormatTypes[i].pfnHandler, NULL);
2473# ifdef IN_RING0
2474 if (rc == VERR_ALREADY_EXISTS)
2475 {
2476 /* in case of cleanup failure in ring-0 */
2477 RTStrFormatTypeDeregister(g_aPgmFormatTypes[i].szType);
2478 rc = RTStrFormatTypeRegister(g_aPgmFormatTypes[i].szType, g_aPgmFormatTypes[i].pfnHandler, NULL);
2479 }
2480# endif
2481 }
2482 if (RT_FAILURE(rc))
2483 while (i-- > 0)
2484 RTStrFormatTypeDeregister(g_aPgmFormatTypes[i].szType);
2485
2486 return rc;
2487#else
2488 return VINF_SUCCESS;
2489#endif
2490}
2491
2492
2493/**
2494 * Deregisters the global string format types.
2495 *
2496 * This should be called at module unload time or in some other manner that
2497 * ensure that it's called exactly one time.
2498 */
2499VMMDECL(void) PGMDeregisterStringFormatTypes(void)
2500{
2501#if !defined(IN_R0) || defined(LOG_ENABLED)
2502 for (unsigned i = 0; i < RT_ELEMENTS(g_aPgmFormatTypes); i++)
2503 RTStrFormatTypeDeregister(g_aPgmFormatTypes[i].szType);
2504#endif
2505}
2506
2507#ifdef VBOX_STRICT
2508
2509/**
2510 * Asserts that there are no mapping conflicts.
2511 *
2512 * @returns Number of conflicts.
2513 * @param pVM The VM Handle.
2514 */
2515VMMDECL(unsigned) PGMAssertNoMappingConflicts(PVM pVM)
2516{
2517 unsigned cErrors = 0;
2518
2519 /* Only applies to raw mode -> 1 VPCU */
2520 Assert(pVM->cCpus == 1);
2521 PVMCPU pVCpu = &pVM->aCpus[0];
2522
2523 /*
2524 * Check for mapping conflicts.
2525 */
2526 for (PPGMMAPPING pMapping = pVM->pgm.s.CTX_SUFF(pMappings);
2527 pMapping;
2528 pMapping = pMapping->CTX_SUFF(pNext))
2529 {
2530 /** @todo This is slow and should be optimized, but since it's just assertions I don't care now. */
2531 for (RTGCPTR GCPtr = pMapping->GCPtr;
2532 GCPtr <= pMapping->GCPtrLast;
2533 GCPtr += PAGE_SIZE)
2534 {
2535 int rc = PGMGstGetPage(pVCpu, (RTGCPTR)GCPtr, NULL, NULL);
2536 if (rc != VERR_PAGE_TABLE_NOT_PRESENT)
2537 {
2538 AssertMsgFailed(("Conflict at %RGv with %s\n", GCPtr, R3STRING(pMapping->pszDesc)));
2539 cErrors++;
2540 break;
2541 }
2542 }
2543 }
2544
2545 return cErrors;
2546}
2547
2548
2549/**
2550 * Asserts that everything related to the guest CR3 is correctly shadowed.
2551 *
2552 * This will call PGMAssertNoMappingConflicts() and PGMAssertHandlerAndFlagsInSync(),
2553 * and assert the correctness of the guest CR3 mapping before asserting that the
2554 * shadow page tables is in sync with the guest page tables.
2555 *
2556 * @returns Number of conflicts.
2557 * @param pVM The VM Handle.
2558 * @param pVCpu VMCPU handle.
2559 * @param cr3 The current guest CR3 register value.
2560 * @param cr4 The current guest CR4 register value.
2561 */
2562VMMDECL(unsigned) PGMAssertCR3(PVM pVM, PVMCPU pVCpu, uint64_t cr3, uint64_t cr4)
2563{
2564 STAM_PROFILE_START(&pVCpu->pgm.s.CTX_MID_Z(Stat,SyncCR3), a);
2565 pgmLock(pVM);
2566 unsigned cErrors = PGM_BTH_PFN(AssertCR3, pVCpu)(pVCpu, cr3, cr4, 0, ~(RTGCPTR)0);
2567 pgmUnlock(pVM);
2568 STAM_PROFILE_STOP(&pVCpu->pgm.s.CTX_MID_Z(Stat,SyncCR3), a);
2569 return cErrors;
2570}
2571
2572#endif /* VBOX_STRICT */
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