VirtualBox

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

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

#1865: PGM - and another one.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 66.2 KB
Line 
1/* $Id: PGMAll.cpp 14038 2008-11-10 18:23:15Z 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 todo flags. */
62 RTUINT fTodo;
63 /** The CR4 register value. */
64 uint32_t cr4;
65} PGMHVUSTATE, *PPGMHVUSTATE;
66
67
68/*******************************************************************************
69* Internal Functions *
70*******************************************************************************/
71DECLINLINE(int) pgmShwGetLongModePDPtr(PVM pVM, RTGCPTR64 GCPtr, PX86PML4E *ppPml4e, PX86PDPT *ppPdpt, PX86PDPAE *ppPD);
72DECLINLINE(int) pgmShwSyncLongModePDPtr(PVM pVM, RTGCPTR64 GCPtr, PX86PML4E pGstPml4e, PX86PDPE pGstPdpe, PX86PDPAE *ppPD);
73DECLINLINE(int) pgmShwGetEPTPDPtr(PVM pVM, RTGCPTR64 GCPtr, PEPTPDPT *ppPdpt, PEPTPD *ppPD);
74DECLINLINE(int) pgmShwSyncPAEPDPtr(PVM pVM, RTGCPTR GCPtr, PX86PDPE pGstPdpe, PX86PDPAE *ppPD);
75DECLINLINE(int) pgmShwGetPAEPDPtr(PVM pVM, RTGCPTR GCPtr, PX86PDPT *ppPdpt, PX86PDPAE *ppPD);
76
77
78/*
79 * Shadow - 32-bit mode
80 */
81#define PGM_SHW_TYPE PGM_TYPE_32BIT
82#define PGM_SHW_NAME(name) PGM_SHW_NAME_32BIT(name)
83#include "PGMAllShw.h"
84
85/* Guest - real mode */
86#define PGM_GST_TYPE PGM_TYPE_REAL
87#define PGM_GST_NAME(name) PGM_GST_NAME_REAL(name)
88#define PGM_BTH_NAME(name) PGM_BTH_NAME_32BIT_REAL(name)
89#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_32BIT_PT_FOR_PHYS
90#include "PGMAllGst.h"
91#include "PGMAllBth.h"
92#undef BTH_PGMPOOLKIND_PT_FOR_PT
93#undef PGM_BTH_NAME
94#undef PGM_GST_TYPE
95#undef PGM_GST_NAME
96
97/* Guest - protected mode */
98#define PGM_GST_TYPE PGM_TYPE_PROT
99#define PGM_GST_NAME(name) PGM_GST_NAME_PROT(name)
100#define PGM_BTH_NAME(name) PGM_BTH_NAME_32BIT_PROT(name)
101#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_32BIT_PT_FOR_PHYS
102#include "PGMAllGst.h"
103#include "PGMAllBth.h"
104#undef BTH_PGMPOOLKIND_PT_FOR_PT
105#undef PGM_BTH_NAME
106#undef PGM_GST_TYPE
107#undef PGM_GST_NAME
108
109/* Guest - 32-bit mode */
110#define PGM_GST_TYPE PGM_TYPE_32BIT
111#define PGM_GST_NAME(name) PGM_GST_NAME_32BIT(name)
112#define PGM_BTH_NAME(name) PGM_BTH_NAME_32BIT_32BIT(name)
113#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT
114#define BTH_PGMPOOLKIND_PT_FOR_BIG PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB
115#include "PGMAllGst.h"
116#include "PGMAllBth.h"
117#undef BTH_PGMPOOLKIND_PT_FOR_BIG
118#undef BTH_PGMPOOLKIND_PT_FOR_PT
119#undef PGM_BTH_NAME
120#undef PGM_GST_TYPE
121#undef PGM_GST_NAME
122
123#undef PGM_SHW_TYPE
124#undef PGM_SHW_NAME
125
126
127/*
128 * Shadow - PAE mode
129 */
130#define PGM_SHW_TYPE PGM_TYPE_PAE
131#define PGM_SHW_NAME(name) PGM_SHW_NAME_PAE(name)
132#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_REAL(name)
133#include "PGMAllShw.h"
134
135/* Guest - real mode */
136#define PGM_GST_TYPE PGM_TYPE_REAL
137#define PGM_GST_NAME(name) PGM_GST_NAME_REAL(name)
138#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_REAL(name)
139#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_PAE_PT_FOR_PHYS
140#include "PGMAllBth.h"
141#undef BTH_PGMPOOLKIND_PT_FOR_PT
142#undef PGM_BTH_NAME
143#undef PGM_GST_TYPE
144#undef PGM_GST_NAME
145
146/* Guest - protected mode */
147#define PGM_GST_TYPE PGM_TYPE_PROT
148#define PGM_GST_NAME(name) PGM_GST_NAME_PROT(name)
149#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_PROT(name)
150#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_PAE_PT_FOR_PHYS
151#include "PGMAllBth.h"
152#undef BTH_PGMPOOLKIND_PT_FOR_PT
153#undef PGM_BTH_NAME
154#undef PGM_GST_TYPE
155#undef PGM_GST_NAME
156
157/* Guest - 32-bit mode */
158#define PGM_GST_TYPE PGM_TYPE_32BIT
159#define PGM_GST_NAME(name) PGM_GST_NAME_32BIT(name)
160#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_32BIT(name)
161#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_PAE_PT_FOR_32BIT_PT
162#define BTH_PGMPOOLKIND_PT_FOR_BIG PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB
163#include "PGMAllBth.h"
164#undef BTH_PGMPOOLKIND_PT_FOR_BIG
165#undef BTH_PGMPOOLKIND_PT_FOR_PT
166#undef PGM_BTH_NAME
167#undef PGM_GST_TYPE
168#undef PGM_GST_NAME
169
170
171/* Guest - PAE mode */
172#define PGM_GST_TYPE PGM_TYPE_PAE
173#define PGM_GST_NAME(name) PGM_GST_NAME_PAE(name)
174#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_PAE(name)
175#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_PAE_PT_FOR_PAE_PT
176#define BTH_PGMPOOLKIND_PT_FOR_BIG PGMPOOLKIND_PAE_PT_FOR_PAE_2MB
177#include "PGMAllGst.h"
178#include "PGMAllBth.h"
179#undef BTH_PGMPOOLKIND_PT_FOR_BIG
180#undef BTH_PGMPOOLKIND_PT_FOR_PT
181#undef PGM_BTH_NAME
182#undef PGM_GST_TYPE
183#undef PGM_GST_NAME
184
185#undef PGM_SHW_TYPE
186#undef PGM_SHW_NAME
187
188
189#ifndef IN_RC /* AMD64 implies VT-x/AMD-V */
190/*
191 * Shadow - AMD64 mode
192 */
193# define PGM_SHW_TYPE PGM_TYPE_AMD64
194# define PGM_SHW_NAME(name) PGM_SHW_NAME_AMD64(name)
195# include "PGMAllShw.h"
196
197/* Guest - protected mode */
198# define PGM_GST_TYPE PGM_TYPE_PROT
199# define PGM_GST_NAME(name) PGM_GST_NAME_PROT(name)
200# define PGM_BTH_NAME(name) PGM_BTH_NAME_AMD64_PROT(name)
201# define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_PAE_PT_FOR_PHYS
202# include "PGMAllBth.h"
203# undef BTH_PGMPOOLKIND_PT_FOR_PT
204# undef PGM_BTH_NAME
205# undef PGM_GST_TYPE
206# undef PGM_GST_NAME
207
208# ifdef VBOX_WITH_64_BITS_GUESTS
209/* Guest - AMD64 mode */
210# define PGM_GST_TYPE PGM_TYPE_AMD64
211# define PGM_GST_NAME(name) PGM_GST_NAME_AMD64(name)
212# define PGM_BTH_NAME(name) PGM_BTH_NAME_AMD64_AMD64(name)
213# define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_PAE_PT_FOR_PAE_PT
214# define BTH_PGMPOOLKIND_PT_FOR_BIG PGMPOOLKIND_PAE_PT_FOR_PAE_2MB
215# include "PGMAllGst.h"
216# include "PGMAllBth.h"
217# undef BTH_PGMPOOLKIND_PT_FOR_BIG
218# undef BTH_PGMPOOLKIND_PT_FOR_PT
219# undef PGM_BTH_NAME
220# undef PGM_GST_TYPE
221# undef PGM_GST_NAME
222# endif /* VBOX_WITH_64_BITS_GUESTS */
223
224# undef PGM_SHW_TYPE
225# undef PGM_SHW_NAME
226
227
228/*
229 * Shadow - Nested paging mode
230 */
231# define PGM_SHW_TYPE PGM_TYPE_NESTED
232# define PGM_SHW_NAME(name) PGM_SHW_NAME_NESTED(name)
233# include "PGMAllShw.h"
234
235/* Guest - real mode */
236# define PGM_GST_TYPE PGM_TYPE_REAL
237# define PGM_GST_NAME(name) PGM_GST_NAME_REAL(name)
238# define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_REAL(name)
239# include "PGMAllBth.h"
240# undef PGM_BTH_NAME
241# undef PGM_GST_TYPE
242# undef PGM_GST_NAME
243
244/* Guest - protected mode */
245# define PGM_GST_TYPE PGM_TYPE_PROT
246# define PGM_GST_NAME(name) PGM_GST_NAME_PROT(name)
247# define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_PROT(name)
248# include "PGMAllBth.h"
249# undef PGM_BTH_NAME
250# undef PGM_GST_TYPE
251# undef PGM_GST_NAME
252
253/* Guest - 32-bit mode */
254# define PGM_GST_TYPE PGM_TYPE_32BIT
255# define PGM_GST_NAME(name) PGM_GST_NAME_32BIT(name)
256# define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_32BIT(name)
257# include "PGMAllBth.h"
258# undef PGM_BTH_NAME
259# undef PGM_GST_TYPE
260# undef PGM_GST_NAME
261
262/* Guest - PAE mode */
263# define PGM_GST_TYPE PGM_TYPE_PAE
264# define PGM_GST_NAME(name) PGM_GST_NAME_PAE(name)
265# define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_PAE(name)
266# include "PGMAllBth.h"
267# undef PGM_BTH_NAME
268# undef PGM_GST_TYPE
269# undef PGM_GST_NAME
270
271# ifdef VBOX_WITH_64_BITS_GUESTS
272/* Guest - AMD64 mode */
273# define PGM_GST_TYPE PGM_TYPE_AMD64
274# define PGM_GST_NAME(name) PGM_GST_NAME_AMD64(name)
275# define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_AMD64(name)
276# include "PGMAllBth.h"
277# undef PGM_BTH_NAME
278# undef PGM_GST_TYPE
279# undef PGM_GST_NAME
280# endif /* VBOX_WITH_64_BITS_GUESTS */
281
282# undef PGM_SHW_TYPE
283# undef PGM_SHW_NAME
284
285
286/*
287 * Shadow - EPT
288 */
289# define PGM_SHW_TYPE PGM_TYPE_EPT
290# define PGM_SHW_NAME(name) PGM_SHW_NAME_EPT(name)
291# include "PGMAllShw.h"
292
293/* Guest - real mode */
294# define PGM_GST_TYPE PGM_TYPE_REAL
295# define PGM_GST_NAME(name) PGM_GST_NAME_REAL(name)
296# define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_REAL(name)
297# define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_EPT_PT_FOR_PHYS
298# include "PGMAllBth.h"
299# undef BTH_PGMPOOLKIND_PT_FOR_PT
300# undef PGM_BTH_NAME
301# undef PGM_GST_TYPE
302# undef PGM_GST_NAME
303
304/* Guest - protected mode */
305# define PGM_GST_TYPE PGM_TYPE_PROT
306# define PGM_GST_NAME(name) PGM_GST_NAME_PROT(name)
307# define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_PROT(name)
308# define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_EPT_PT_FOR_PHYS
309# include "PGMAllBth.h"
310# undef BTH_PGMPOOLKIND_PT_FOR_PT
311# undef PGM_BTH_NAME
312# undef PGM_GST_TYPE
313# undef PGM_GST_NAME
314
315/* Guest - 32-bit mode */
316# define PGM_GST_TYPE PGM_TYPE_32BIT
317# define PGM_GST_NAME(name) PGM_GST_NAME_32BIT(name)
318# define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_32BIT(name)
319# define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_EPT_PT_FOR_PHYS
320# include "PGMAllBth.h"
321# undef BTH_PGMPOOLKIND_PT_FOR_PT
322# undef PGM_BTH_NAME
323# undef PGM_GST_TYPE
324# undef PGM_GST_NAME
325
326/* Guest - PAE mode */
327# define PGM_GST_TYPE PGM_TYPE_PAE
328# define PGM_GST_NAME(name) PGM_GST_NAME_PAE(name)
329# define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_PAE(name)
330# define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_EPT_PT_FOR_PHYS
331# include "PGMAllBth.h"
332# undef BTH_PGMPOOLKIND_PT_FOR_PT
333# undef PGM_BTH_NAME
334# undef PGM_GST_TYPE
335# undef PGM_GST_NAME
336
337# ifdef VBOX_WITH_64_BITS_GUESTS
338/* Guest - AMD64 mode */
339# define PGM_GST_TYPE PGM_TYPE_AMD64
340# define PGM_GST_NAME(name) PGM_GST_NAME_AMD64(name)
341# define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_AMD64(name)
342# define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_EPT_PT_FOR_PHYS
343# include "PGMAllBth.h"
344# undef BTH_PGMPOOLKIND_PT_FOR_PT
345# undef PGM_BTH_NAME
346# undef PGM_GST_TYPE
347# undef PGM_GST_NAME
348# endif /* VBOX_WITH_64_BITS_GUESTS */
349
350# undef PGM_SHW_TYPE
351# undef PGM_SHW_NAME
352
353#endif /* !IN_RC */
354
355
356#ifndef IN_RING3
357/**
358 * #PF Handler.
359 *
360 * @returns VBox status code (appropriate for trap handling and GC return).
361 * @param pVM VM Handle.
362 * @param uErr The trap error code.
363 * @param pRegFrame Trap register frame.
364 * @param pvFault The fault address.
365 */
366VMMDECL(int) PGMTrap0eHandler(PVM pVM, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault)
367{
368 LogFlow(("PGMTrap0eHandler: uErr=%RGu pvFault=%RGv eip=%RGv\n", uErr, pvFault, (RTGCPTR)pRegFrame->rip));
369 STAM_PROFILE_START(&pVM->pgm.s.StatRZTrap0e, a);
370 STAM_STATS({ pVM->pgm.s.CTX_SUFF(pStatTrap0eAttribution) = NULL; } );
371
372
373#ifdef VBOX_WITH_STATISTICS
374 /*
375 * Error code stats.
376 */
377 if (uErr & X86_TRAP_PF_US)
378 {
379 if (!(uErr & X86_TRAP_PF_P))
380 {
381 if (uErr & X86_TRAP_PF_RW)
382 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eUSNotPresentWrite);
383 else
384 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eUSNotPresentRead);
385 }
386 else if (uErr & X86_TRAP_PF_RW)
387 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eUSWrite);
388 else if (uErr & X86_TRAP_PF_RSVD)
389 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eUSReserved);
390 else if (uErr & X86_TRAP_PF_ID)
391 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eUSNXE);
392 else
393 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eUSRead);
394 }
395 else
396 { /* Supervisor */
397 if (!(uErr & X86_TRAP_PF_P))
398 {
399 if (uErr & X86_TRAP_PF_RW)
400 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eSVNotPresentWrite);
401 else
402 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eSVNotPresentRead);
403 }
404 else if (uErr & X86_TRAP_PF_RW)
405 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eSVWrite);
406 else if (uErr & X86_TRAP_PF_ID)
407 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eSNXE);
408 else if (uErr & X86_TRAP_PF_RSVD)
409 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eSVReserved);
410 }
411#endif /* VBOX_WITH_STATISTICS */
412
413 /*
414 * Call the worker.
415 */
416 int rc = PGM_BTH_PFN(Trap0eHandler, pVM)(pVM, uErr, pRegFrame, pvFault);
417 if (rc == VINF_PGM_SYNCPAGE_MODIFIED_PDE)
418 rc = VINF_SUCCESS;
419 STAM_STATS({ if (rc == VINF_EM_RAW_GUEST_TRAP) STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eGuestPF); });
420 STAM_STATS({ if (!pVM->pgm.s.CTX_SUFF(pStatTrap0eAttribution))
421 pVM->pgm.s.CTX_SUFF(pStatTrap0eAttribution) = &pVM->pgm.s.StatRZTrap0eTime2Misc; });
422 STAM_PROFILE_STOP_EX(&pVM->pgm.s.StatRZTrap0e, pVM->pgm.s.CTX_SUFF(pStatTrap0eAttribution), a);
423 return rc;
424}
425#endif /* !IN_RING3 */
426
427
428/**
429 * Prefetch a page
430 *
431 * Typically used to sync commonly used pages before entering raw mode
432 * after a CR3 reload.
433 *
434 * @returns VBox status code suitable for scheduling.
435 * @retval VINF_SUCCESS on success.
436 * @retval VINF_PGM_SYNC_CR3 if we're out of shadow pages or something like that.
437 * @param pVM VM handle.
438 * @param GCPtrPage Page to invalidate.
439 */
440VMMDECL(int) PGMPrefetchPage(PVM pVM, RTGCPTR GCPtrPage)
441{
442 STAM_PROFILE_START(&pVM->pgm.s.CTX_MID_Z(Stat,Prefetch), a);
443 int rc = PGM_BTH_PFN(PrefetchPage, pVM)(pVM, GCPtrPage);
444 STAM_PROFILE_STOP(&pVM->pgm.s.CTX_MID_Z(Stat,Prefetch), a);
445 AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 || RT_FAILURE(rc), ("rc=%Rrc\n", rc));
446 return rc;
447}
448
449
450/**
451 * Gets the mapping corresponding to the specified address (if any).
452 *
453 * @returns Pointer to the mapping.
454 * @returns NULL if not
455 *
456 * @param pVM The virtual machine.
457 * @param GCPtr The guest context pointer.
458 */
459PPGMMAPPING pgmGetMapping(PVM pVM, RTGCPTR GCPtr)
460{
461 PPGMMAPPING pMapping = pVM->pgm.s.CTX_SUFF(pMappings);
462 while (pMapping)
463 {
464 if ((uintptr_t)GCPtr < (uintptr_t)pMapping->GCPtr)
465 break;
466 if ((uintptr_t)GCPtr - (uintptr_t)pMapping->GCPtr < pMapping->cb)
467 return pMapping;
468 pMapping = pMapping->CTX_SUFF(pNext);
469 }
470 return NULL;
471}
472
473
474/**
475 * Verifies a range of pages for read or write access
476 *
477 * Only checks the guest's page tables
478 *
479 * @returns VBox status code.
480 * @param pVM VM handle.
481 * @param Addr Guest virtual address to check
482 * @param cbSize Access size
483 * @param fAccess Access type (r/w, user/supervisor (X86_PTE_*))
484 * @remarks Current not in use.
485 */
486VMMDECL(int) PGMIsValidAccess(PVM pVM, RTGCPTR Addr, uint32_t cbSize, uint32_t fAccess)
487{
488 /*
489 * Validate input.
490 */
491 if (fAccess & ~(X86_PTE_US | X86_PTE_RW))
492 {
493 AssertMsgFailed(("PGMIsValidAccess: invalid access type %08x\n", fAccess));
494 return VERR_INVALID_PARAMETER;
495 }
496
497 uint64_t fPage;
498 int rc = PGMGstGetPage(pVM, (RTGCPTR)Addr, &fPage, NULL);
499 if (RT_FAILURE(rc))
500 {
501 Log(("PGMIsValidAccess: access violation for %RGv rc=%d\n", Addr, rc));
502 return VINF_EM_RAW_GUEST_TRAP;
503 }
504
505 /*
506 * Check if the access would cause a page fault
507 *
508 * Note that hypervisor page directories are not present in the guest's tables, so this check
509 * is sufficient.
510 */
511 bool fWrite = !!(fAccess & X86_PTE_RW);
512 bool fUser = !!(fAccess & X86_PTE_US);
513 if ( !(fPage & X86_PTE_P)
514 || (fWrite && !(fPage & X86_PTE_RW))
515 || (fUser && !(fPage & X86_PTE_US)) )
516 {
517 Log(("PGMIsValidAccess: access violation for %RGv attr %#llx vs %d:%d\n", Addr, fPage, fWrite, fUser));
518 return VINF_EM_RAW_GUEST_TRAP;
519 }
520 if ( RT_SUCCESS(rc)
521 && PAGE_ADDRESS(Addr) != PAGE_ADDRESS(Addr + cbSize))
522 return PGMIsValidAccess(pVM, Addr + PAGE_SIZE, (cbSize > PAGE_SIZE) ? cbSize - PAGE_SIZE : 1, fAccess);
523 return rc;
524}
525
526
527/**
528 * Verifies a range of pages for read or write access
529 *
530 * Supports handling of pages marked for dirty bit tracking and CSAM
531 *
532 * @returns VBox status code.
533 * @param pVM VM handle.
534 * @param Addr Guest virtual address to check
535 * @param cbSize Access size
536 * @param fAccess Access type (r/w, user/supervisor (X86_PTE_*))
537 */
538VMMDECL(int) PGMVerifyAccess(PVM pVM, RTGCPTR Addr, uint32_t cbSize, uint32_t fAccess)
539{
540 AssertMsg(!(fAccess & ~(X86_PTE_US | X86_PTE_RW)), ("PGMVerifyAccess: invalid access type %08x\n", fAccess));
541
542 /*
543 * Get going.
544 */
545 uint64_t fPageGst;
546 int rc = PGMGstGetPage(pVM, (RTGCPTR)Addr, &fPageGst, NULL);
547 if (RT_FAILURE(rc))
548 {
549 Log(("PGMVerifyAccess: access violation for %RGv rc=%d\n", Addr, rc));
550 return VINF_EM_RAW_GUEST_TRAP;
551 }
552
553 /*
554 * Check if the access would cause a page fault
555 *
556 * Note that hypervisor page directories are not present in the guest's tables, so this check
557 * is sufficient.
558 */
559 const bool fWrite = !!(fAccess & X86_PTE_RW);
560 const bool fUser = !!(fAccess & X86_PTE_US);
561 if ( !(fPageGst & X86_PTE_P)
562 || (fWrite && !(fPageGst & X86_PTE_RW))
563 || (fUser && !(fPageGst & X86_PTE_US)) )
564 {
565 Log(("PGMVerifyAccess: access violation for %RGv attr %#llx vs %d:%d\n", Addr, fPageGst, fWrite, fUser));
566 return VINF_EM_RAW_GUEST_TRAP;
567 }
568
569 if (!HWACCMIsNestedPagingActive(pVM))
570 {
571 /*
572 * Next step is to verify if we protected this page for dirty bit tracking or for CSAM scanning
573 */
574 rc = PGMShwGetPage(pVM, (RTGCPTR)Addr, NULL, NULL);
575 if ( rc == VERR_PAGE_NOT_PRESENT
576 || rc == VERR_PAGE_TABLE_NOT_PRESENT)
577 {
578 /*
579 * Page is not present in our page tables.
580 * Try to sync it!
581 */
582 Assert(X86_TRAP_PF_RW == X86_PTE_RW && X86_TRAP_PF_US == X86_PTE_US);
583 uint32_t uErr = fAccess & (X86_TRAP_PF_RW | X86_TRAP_PF_US);
584 rc = PGM_BTH_PFN(VerifyAccessSyncPage, pVM)(pVM, Addr, fPageGst, uErr);
585 if (rc != VINF_SUCCESS)
586 return rc;
587 }
588 else
589 AssertMsg(rc == VINF_SUCCESS, ("PGMShwGetPage %RGv failed with %Rrc\n", Addr, rc));
590 }
591
592#if 0 /* def VBOX_STRICT; triggers too often now */
593 /*
594 * This check is a bit paranoid, but useful.
595 */
596 /** @note this will assert when writing to monitored pages (a bit annoying actually) */
597 uint64_t fPageShw;
598 rc = PGMShwGetPage(pVM, (RTGCPTR)Addr, &fPageShw, NULL);
599 if ( (rc == VERR_PAGE_NOT_PRESENT || RT_FAILURE(rc))
600 || (fWrite && !(fPageShw & X86_PTE_RW))
601 || (fUser && !(fPageShw & X86_PTE_US)) )
602 {
603 AssertMsgFailed(("Unexpected access violation for %RGv! rc=%Rrc write=%d user=%d\n",
604 Addr, rc, fWrite && !(fPageShw & X86_PTE_RW), fUser && !(fPageShw & X86_PTE_US)));
605 return VINF_EM_RAW_GUEST_TRAP;
606 }
607#endif
608
609 if ( RT_SUCCESS(rc)
610 && ( PAGE_ADDRESS(Addr) != PAGE_ADDRESS(Addr + cbSize - 1)
611 || Addr + cbSize < Addr))
612 {
613 /* Don't recursively call PGMVerifyAccess as we might run out of stack. */
614 for (;;)
615 {
616 Addr += PAGE_SIZE;
617 if (cbSize > PAGE_SIZE)
618 cbSize -= PAGE_SIZE;
619 else
620 cbSize = 1;
621 rc = PGMVerifyAccess(pVM, Addr, 1, fAccess);
622 if (rc != VINF_SUCCESS)
623 break;
624 if (PAGE_ADDRESS(Addr) == PAGE_ADDRESS(Addr + cbSize - 1))
625 break;
626 }
627 }
628 return rc;
629}
630
631
632/**
633 * Emulation of the invlpg instruction (HC only actually).
634 *
635 * @returns VBox status code, special care required.
636 * @retval VINF_PGM_SYNC_CR3 - handled.
637 * @retval VINF_EM_RAW_EMULATE_INSTR - not handled (RC only).
638 * @retval VERR_REM_FLUSHED_PAGES_OVERFLOW - not handled.
639 *
640 * @param pVM VM handle.
641 * @param GCPtrPage Page to invalidate.
642 *
643 * @remark ASSUMES the page table entry or page directory is valid. Fairly
644 * safe, but there could be edge cases!
645 *
646 * @todo Flush page or page directory only if necessary!
647 */
648VMMDECL(int) PGMInvalidatePage(PVM pVM, RTGCPTR GCPtrPage)
649{
650 int rc;
651 Log3(("PGMInvalidatePage: GCPtrPage=%RGv\n", GCPtrPage));
652
653#ifndef IN_RING3
654 /*
655 * Notify the recompiler so it can record this instruction.
656 * Failure happens when it's out of space. We'll return to HC in that case.
657 */
658 rc = REMNotifyInvalidatePage(pVM, GCPtrPage);
659 if (rc != VINF_SUCCESS)
660 return rc;
661#endif /* !IN_RING3 */
662
663
664#ifdef IN_RC
665 /*
666 * Check for conflicts and pending CR3 monitoring updates.
667 */
668 if (!pVM->pgm.s.fMappingsFixed)
669 {
670 if ( pgmGetMapping(pVM, GCPtrPage)
671 && PGMGstGetPage(pVM, GCPtrPage, NULL, NULL) != VERR_PAGE_TABLE_NOT_PRESENT)
672 {
673 LogFlow(("PGMGCInvalidatePage: Conflict!\n"));
674 VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3);
675 STAM_COUNTER_INC(&pVM->pgm.s.StatRCInvlPgConflict);
676 return VINF_PGM_SYNC_CR3;
677 }
678
679 if (pVM->pgm.s.fSyncFlags & PGM_SYNC_MONITOR_CR3)
680 {
681 LogFlow(("PGMGCInvalidatePage: PGM_SYNC_MONITOR_CR3 -> reinterpret instruction in R3\n"));
682 STAM_COUNTER_INC(&pVM->pgm.s.StatRCInvlPgSyncMonCR3);
683 return VINF_EM_RAW_EMULATE_INSTR;
684 }
685 }
686#endif /* IN_RC */
687
688 /*
689 * Call paging mode specific worker.
690 */
691 STAM_PROFILE_START(&pVM->pgm.s.CTX_MID_Z(Stat,InvalidatePage), a);
692 rc = PGM_BTH_PFN(InvalidatePage, pVM)(pVM, GCPtrPage);
693 STAM_PROFILE_STOP(&pVM->pgm.s.CTX_MID_Z(Stat,InvalidatePage), a);
694
695#ifdef IN_RING3
696 /*
697 * Check if we have a pending update of the CR3 monitoring.
698 */
699 if ( RT_SUCCESS(rc)
700 && (pVM->pgm.s.fSyncFlags & PGM_SYNC_MONITOR_CR3))
701 {
702 pVM->pgm.s.fSyncFlags &= ~PGM_SYNC_MONITOR_CR3;
703 Assert(!pVM->pgm.s.fMappingsFixed);
704 Assert(pVM->pgm.s.GCPhysCR3 == pVM->pgm.s.GCPhysGstCR3Monitored);
705 rc = PGM_GST_PFN(MonitorCR3, pVM)(pVM, pVM->pgm.s.GCPhysCR3);
706 }
707
708 /*
709 * Inform CSAM about the flush
710 *
711 * Note: This is to check if monitored pages have been changed; when we implement
712 * callbacks for virtual handlers, this is no longer required.
713 */
714 CSAMR3FlushPage(pVM, GCPtrPage);
715#endif /* IN_RING3 */
716 return rc;
717}
718
719
720/**
721 * Executes an instruction using the interpreter.
722 *
723 * @returns VBox status code (appropriate for trap handling and GC return).
724 * @param pVM VM handle.
725 * @param pRegFrame Register frame.
726 * @param pvFault Fault address.
727 */
728VMMDECL(int) PGMInterpretInstruction(PVM pVM, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault)
729{
730 uint32_t cb;
731 int rc = EMInterpretInstruction(pVM, pRegFrame, pvFault, &cb);
732 if (rc == VERR_EM_INTERPRETER)
733 rc = VINF_EM_RAW_EMULATE_INSTR;
734 if (rc != VINF_SUCCESS)
735 Log(("PGMInterpretInstruction: returns %Rrc (pvFault=%RGv)\n", rc, pvFault));
736 return rc;
737}
738
739
740/**
741 * Gets effective page information (from the VMM page directory).
742 *
743 * @returns VBox status.
744 * @param pVM VM Handle.
745 * @param GCPtr Guest Context virtual address of the page.
746 * @param pfFlags Where to store the flags. These are X86_PTE_*.
747 * @param pHCPhys Where to store the HC physical address of the page.
748 * This is page aligned.
749 * @remark You should use PGMMapGetPage() for pages in a mapping.
750 */
751VMMDECL(int) PGMShwGetPage(PVM pVM, RTGCPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys)
752{
753 return PGM_SHW_PFN(GetPage,pVM)(pVM, GCPtr, pfFlags, pHCPhys);
754}
755
756
757/**
758 * Sets (replaces) the page flags for a range of pages in the shadow context.
759 *
760 * @returns VBox status.
761 * @param pVM VM handle.
762 * @param GCPtr The address of the first page.
763 * @param cb The size of the range in bytes.
764 * @param fFlags Page flags X86_PTE_*, excluding the page mask of course.
765 * @remark You must use PGMMapSetPage() for pages in a mapping.
766 */
767VMMDECL(int) PGMShwSetPage(PVM pVM, RTGCPTR GCPtr, size_t cb, uint64_t fFlags)
768{
769 return PGMShwModifyPage(pVM, GCPtr, cb, fFlags, 0);
770}
771
772
773/**
774 * Modify page flags for a range of pages in the shadow context.
775 *
776 * The existing flags are ANDed with the fMask and ORed with the fFlags.
777 *
778 * @returns VBox status code.
779 * @param pVM VM handle.
780 * @param GCPtr Virtual address of the first page in the range.
781 * @param cb Size (in bytes) of the range to apply the modification to.
782 * @param fFlags The OR mask - page flags X86_PTE_*, excluding the page mask of course.
783 * @param fMask The AND mask - page flags X86_PTE_*.
784 * Be very CAREFUL when ~'ing constants which could be 32-bit!
785 * @remark You must use PGMMapModifyPage() for pages in a mapping.
786 */
787VMMDECL(int) PGMShwModifyPage(PVM pVM, RTGCPTR GCPtr, size_t cb, uint64_t fFlags, uint64_t fMask)
788{
789 AssertMsg(!(fFlags & X86_PTE_PAE_PG_MASK), ("fFlags=%#llx\n", fFlags));
790 Assert(cb);
791
792 /*
793 * Align the input.
794 */
795 cb += GCPtr & PAGE_OFFSET_MASK;
796 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
797 GCPtr = (GCPtr & PAGE_BASE_GC_MASK); /** @todo this ain't necessary, right... */
798
799 /*
800 * Call worker.
801 */
802 return PGM_SHW_PFN(ModifyPage, pVM)(pVM, GCPtr, cb, fFlags, fMask);
803}
804
805
806/**
807 * Syncs the SHADOW page directory pointer for the specified address.
808 *
809 * Allocates backing pages in case the PDPT entry is missing.
810 *
811 * @returns VBox status.
812 * @param pVM VM handle.
813 * @param GCPtr The address.
814 * @param pGstPdpe Guest PDPT entry
815 * @param ppPD Receives address of page directory
816 * @remarks Unused.
817 */
818DECLINLINE(int) pgmShwSyncPAEPDPtr(PVM pVM, RTGCPTR GCPtr, PX86PDPE pGstPdpe, PX86PDPAE *ppPD)
819{
820 PPGM pPGM = &pVM->pgm.s;
821 PPGMPOOL pPool = pPGM->CTX_SUFF(pPool);
822 PPGMPOOLPAGE pShwPage;
823 int rc;
824
825 Assert(!HWACCMIsNestedPagingActive(pVM));
826
827 const unsigned iPdPt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
828 PX86PDPT pPdpt = pgmShwGetPaePDPTPtr(&pVM->pgm.s);
829 PX86PDPE pPdpe = &pPdpt->a[iPdPt];
830
831 /* Allocate page directory if not present. */
832 if ( !pPdpe->n.u1Present
833 && !(pPdpe->u & X86_PDPE_PG_MASK))
834 {
835 PX86PDPE pPdptGst = pgmGstGetPaePDPEPtr(pPGM, GCPtr);
836
837 Assert(!(pPdpe->u & X86_PDPE_PG_MASK));
838 /* Create a reference back to the PDPT by using the index in its shadow page. */
839 rc = pgmPoolAlloc(pVM, pPdptGst->u & X86_PDPE_PG_MASK, PGMPOOLKIND_PAE_PD_FOR_PAE_PD, PGMPOOL_IDX_PDPT, iPdPt, &pShwPage);
840 if (rc == VERR_PGM_POOL_FLUSHED)
841 {
842 Assert(pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL);
843 VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3);
844 return VINF_PGM_SYNC_CR3;
845 }
846 AssertRCReturn(rc, rc);
847 }
848 else
849 {
850 pShwPage = pgmPoolGetPage(pPool, pPdpe->u & X86_PDPE_PG_MASK);
851 AssertReturn(pShwPage, VERR_INTERNAL_ERROR);
852 }
853 /* The PD was cached or created; hook it up now. */
854 pPdpe->u |= pShwPage->Core.Key
855 | (pGstPdpe->u & ~(X86_PDPE_PG_MASK | X86_PDPE_AVL_MASK | X86_PDPE_PCD | X86_PDPE_PWT));
856
857 *ppPD = (PX86PDPAE)PGMPOOL_PAGE_2_PTR(pVM, pShwPage);
858 return VINF_SUCCESS;
859}
860
861
862/**
863 * Gets the SHADOW page directory pointer for the specified address.
864 *
865 * @returns VBox status.
866 * @param pVM VM handle.
867 * @param GCPtr The address.
868 * @param ppPdpt Receives address of pdpt
869 * @param ppPD Receives address of page directory
870 * @remarks Unused.
871 */
872DECLINLINE(int) pgmShwGetPAEPDPtr(PVM pVM, RTGCPTR GCPtr, PX86PDPT *ppPdpt, PX86PDPAE *ppPD)
873{
874 PPGM pPGM = &pVM->pgm.s;
875 PPGMPOOL pPool = pPGM->CTX_SUFF(pPool);
876 PPGMPOOLPAGE pShwPage;
877
878 Assert(!HWACCMIsNestedPagingActive(pVM));
879
880 const unsigned iPdPt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
881 PX86PDPT pPdpt = pgmShwGetPaePDPTPtr(&pVM->pgm.s);
882 PX86PDPE pPdpe = &pPdpt->a[iPdPt];
883
884 *ppPdpt = pPdpt;
885 if (!pPdpe->n.u1Present)
886 return VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT;
887
888 pShwPage = pgmPoolGetPage(pPool, pPdpe->u & X86_PDPE_PG_MASK);
889 AssertReturn(pShwPage, VERR_INTERNAL_ERROR);
890
891 *ppPD = (PX86PDPAE)PGMPOOL_PAGE_2_PTR(pVM, pShwPage);
892 return VINF_SUCCESS;
893}
894
895#ifndef IN_RC
896
897/**
898 * Syncs the SHADOW page directory pointer for the specified address.
899 *
900 * Allocates backing pages in case the PDPT or PML4 entry is missing.
901 *
902 * The caller is responsible for making sure the guest has a valid PD before
903 * calling this function.
904 *
905 * @returns VBox status.
906 * @param pVM VM handle.
907 * @param GCPtr The address.
908 * @param pGstPml4e Guest PML4 entry
909 * @param pGstPdpe Guest PDPT entry
910 * @param ppPD Receives address of page directory
911 */
912DECLINLINE(int) pgmShwSyncLongModePDPtr(PVM pVM, RTGCPTR64 GCPtr, PX86PML4E pGstPml4e, PX86PDPE pGstPdpe, PX86PDPAE *ppPD)
913{
914 PPGM pPGM = &pVM->pgm.s;
915 PPGMPOOL pPool = pPGM->CTX_SUFF(pPool);
916 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
917 PX86PML4E pPml4e = pgmShwGetLongModePML4EPtr(pPGM, iPml4);
918 bool fNestedPaging = HWACCMIsNestedPagingActive(pVM);
919 PPGMPOOLPAGE pShwPage;
920 X86PML4E Pml4eGst;
921 int rc;
922
923 /* Allocate page directory pointer table if not present. */
924 if ( !pPml4e->n.u1Present
925 && !(pPml4e->u & X86_PML4E_PG_MASK))
926 {
927 Assert(!(pPml4e->u & X86_PML4E_PG_MASK));
928 if (!fNestedPaging)
929 {
930 /** @todo why are we looking up the guest PML4E here? Isn't pGstPml4e
931 * trustworthy? (Remove pgmGstGetLongModePML4E if pGstPml4e and pGstPdpe
932 * are fine.) */
933 Assert(pVM->pgm.s.CTX_SUFF(pShwAmd64CR3));
934 Pml4eGst.u = pgmGstGetLongModePML4E(&pVM->pgm.s, iPml4);
935
936 rc = pgmPoolAlloc(pVM, Pml4eGst.u & X86_PML4E_PG_MASK,
937 PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT, pVM->pgm.s.CTX_SUFF(pShwAmd64CR3)->idx, iPml4, &pShwPage);
938 }
939 else
940 rc = pgmPoolAlloc(pVM, GCPtr + RT_BIT_64(63) /* hack: make the address unique */,
941 PGMPOOLKIND_64BIT_PDPT_FOR_PHYS, PGMPOOL_IDX_NESTED_ROOT, iPml4, &pShwPage);
942
943 if (rc == VERR_PGM_POOL_FLUSHED)
944 {
945 Log(("PGMShwSyncLongModePDPtr: PGM pool flushed (1) -> signal sync cr3\n"));
946 Assert(pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL);
947 VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3);
948 return VINF_PGM_SYNC_CR3;
949 }
950 AssertRCReturn(rc, rc);
951 }
952 else
953 {
954 pShwPage = pgmPoolGetPage(pPool, pPml4e->u & X86_PML4E_PG_MASK);
955 AssertReturn(pShwPage, VERR_INTERNAL_ERROR);
956 }
957 /* The PDPT was cached or created; hook it up now. */
958 pPml4e->u |= pShwPage->Core.Key
959 | (pGstPml4e->u & ~(X86_PML4E_PG_MASK | X86_PML4E_AVL_MASK | X86_PML4E_PCD | X86_PML4E_PWT));
960
961 const unsigned iPdPt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
962 PX86PDPT pPdpt = (PX86PDPT)PGMPOOL_PAGE_2_PTR(pVM, pShwPage);
963 PX86PDPE pPdpe = &pPdpt->a[iPdPt];
964
965 /* Allocate page directory if not present. */
966 if ( !pPdpe->n.u1Present
967 && !(pPdpe->u & X86_PDPE_PG_MASK))
968 {
969 if (!fNestedPaging)
970 {
971 /** @todo why are we looking up the guest PDPTE here? Isn't pGstPdpe
972 * trustworthy? */
973 Pml4eGst.u = pgmGstGetLongModePML4E(&pVM->pgm.s, iPml4);
974 PX86PDPT pPdptGst;
975 rc = PGM_GCPHYS_2_PTR(pVM, Pml4eGst.u & X86_PML4E_PG_MASK, &pPdptGst);
976 AssertRCReturn(rc, rc);
977
978 Assert(!(pPdpe->u & X86_PDPE_PG_MASK));
979 /* Create a reference back to the PDPT by using the index in its shadow page. */
980 rc = pgmPoolAlloc(pVM, pPdptGst->a[iPdPt].u & X86_PDPE_PG_MASK, PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD, pShwPage->idx, iPdPt, &pShwPage);
981 }
982 else
983 rc = pgmPoolAlloc(pVM, GCPtr + RT_BIT_64(62) /* hack: make the address unique */, PGMPOOLKIND_64BIT_PD_FOR_PHYS, pShwPage->idx, iPdPt, &pShwPage);
984
985 if (rc == VERR_PGM_POOL_FLUSHED)
986 {
987 Log(("PGMShwSyncLongModePDPtr: PGM pool flushed (2) -> signal sync cr3\n"));
988 Assert(pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL);
989 VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3);
990 return VINF_PGM_SYNC_CR3;
991 }
992 AssertRCReturn(rc, rc);
993 }
994 else
995 {
996 pShwPage = pgmPoolGetPage(pPool, pPdpe->u & X86_PDPE_PG_MASK);
997 AssertReturn(pShwPage, VERR_INTERNAL_ERROR);
998 }
999 /* The PD was cached or created; hook it up now. */
1000 pPdpe->u |= pShwPage->Core.Key
1001 | (pGstPdpe->u & ~(X86_PDPE_PG_MASK | X86_PDPE_AVL_MASK | X86_PDPE_PCD | X86_PDPE_PWT));
1002
1003 *ppPD = (PX86PDPAE)PGMPOOL_PAGE_2_PTR(pVM, pShwPage);
1004 return VINF_SUCCESS;
1005}
1006
1007
1008/**
1009 * Gets the SHADOW page directory pointer for the specified address (long mode).
1010 *
1011 * @returns VBox status.
1012 * @param pVM VM handle.
1013 * @param GCPtr The address.
1014 * @param ppPdpt Receives address of pdpt
1015 * @param ppPD Receives address of page directory
1016 */
1017DECLINLINE(int) pgmShwGetLongModePDPtr(PVM pVM, RTGCPTR64 GCPtr, PX86PML4E *ppPml4e, PX86PDPT *ppPdpt, PX86PDPAE *ppPD)
1018{
1019 PPGM pPGM = &pVM->pgm.s;
1020 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
1021 PCX86PML4E pPml4e = pgmShwGetLongModePML4EPtr(pPGM, iPml4);
1022 AssertReturn(pPml4e, VERR_INTERNAL_ERROR);
1023 if (ppPml4e)
1024 *ppPml4e = (PX86PML4E)pPml4e;
1025 if (!pPml4e->n.u1Present)
1026 return VERR_PAGE_MAP_LEVEL4_NOT_PRESENT;
1027
1028 PPGMPOOL pPool = pPGM->CTX_SUFF(pPool);
1029 PPGMPOOLPAGE pShwPage = pgmPoolGetPage(pPool, pPml4e->u & X86_PML4E_PG_MASK);
1030 AssertReturn(pShwPage, VERR_INTERNAL_ERROR);
1031
1032 const unsigned iPdPt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
1033 PCX86PDPT pPdpt = *ppPdpt = (PX86PDPT)PGMPOOL_PAGE_2_PTR(pVM, pShwPage);
1034 if (!pPdpt->a[iPdPt].n.u1Present)
1035 return VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT;
1036
1037 pShwPage = pgmPoolGetPage(pPool, pPdpt->a[iPdPt].u & X86_PDPE_PG_MASK);
1038 AssertReturn(pShwPage, VERR_INTERNAL_ERROR);
1039
1040 *ppPD = (PX86PDPAE)PGMPOOL_PAGE_2_PTR(pVM, pShwPage);
1041 return VINF_SUCCESS;
1042}
1043
1044
1045/**
1046 * Syncs the SHADOW EPT page directory pointer for the specified address. Allocates
1047 * backing pages in case the PDPT or PML4 entry is missing.
1048 *
1049 * @returns VBox status.
1050 * @param pVM VM handle.
1051 * @param GCPtr The address.
1052 * @param ppPdpt Receives address of pdpt
1053 * @param ppPD Receives address of page directory
1054 */
1055DECLINLINE(int) pgmShwGetEPTPDPtr(PVM pVM, RTGCPTR64 GCPtr, PEPTPDPT *ppPdpt, PEPTPD *ppPD)
1056{
1057 PPGM pPGM = &pVM->pgm.s;
1058 const unsigned iPml4 = (GCPtr >> EPT_PML4_SHIFT) & EPT_PML4_MASK;
1059 PPGMPOOL pPool = pPGM->CTX_SUFF(pPool);
1060 PEPTPML4 pPml4;
1061 PEPTPML4E pPml4e;
1062 PPGMPOOLPAGE pShwPage;
1063 int rc;
1064
1065 Assert(HWACCMIsNestedPagingActive(pVM));
1066
1067# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
1068 rc = PGM_HCPHYS_2_PTR(pVM, pPGM->HCPhysNestedRoot, &pPml4);
1069 AssertRCReturn(rc, rc);
1070# else
1071 pPml4 = (PEPTPML4)pPGM->CTX_SUFF(pShwNestedRoot);
1072# endif
1073 Assert(pPml4);
1074
1075 /* Allocate page directory pointer table if not present. */
1076 pPml4e = &pPml4->a[iPml4];
1077 if ( !pPml4e->n.u1Present
1078 && !(pPml4e->u & EPT_PML4E_PG_MASK))
1079 {
1080 Assert(!(pPml4e->u & EPT_PML4E_PG_MASK));
1081
1082 rc = pgmPoolAlloc(pVM, (GCPtr & EPT_PML4E_PG_MASK) + RT_BIT_64(63) /* hack: make the address unique */, PGMPOOLKIND_EPT_PDPT_FOR_PHYS, PGMPOOL_IDX_NESTED_ROOT, iPml4, &pShwPage);
1083 if (rc == VERR_PGM_POOL_FLUSHED)
1084 {
1085 Log(("PGMShwSyncEPTPDPtr: PGM pool flushed (1) -> signal sync cr3\n"));
1086 Assert(pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL);
1087 VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3);
1088 return VINF_PGM_SYNC_CR3;
1089 }
1090 AssertRCReturn(rc, rc);
1091 }
1092 else
1093 {
1094 pShwPage = pgmPoolGetPage(pPool, pPml4e->u & EPT_PML4E_PG_MASK);
1095 AssertReturn(pShwPage, VERR_INTERNAL_ERROR);
1096 }
1097 /* The PDPT was cached or created; hook it up now and fill with the default value. */
1098 pPml4e->u = pShwPage->Core.Key;
1099 pPml4e->n.u1Present = 1;
1100 pPml4e->n.u1Write = 1;
1101 pPml4e->n.u1Execute = 1;
1102
1103 const unsigned iPdPt = (GCPtr >> EPT_PDPT_SHIFT) & EPT_PDPT_MASK;
1104 PEPTPDPT pPdpt = (PEPTPDPT)PGMPOOL_PAGE_2_PTR(pVM, pShwPage);
1105 PEPTPDPTE pPdpe = &pPdpt->a[iPdPt];
1106
1107 if (ppPdpt)
1108 *ppPdpt = pPdpt;
1109
1110 /* Allocate page directory if not present. */
1111 if ( !pPdpe->n.u1Present
1112 && !(pPdpe->u & EPT_PDPTE_PG_MASK))
1113 {
1114 rc = pgmPoolAlloc(pVM, (GCPtr & EPT_PDPTE_PG_MASK) + RT_BIT_64(62) /* hack: make the address unique */, PGMPOOLKIND_64BIT_PD_FOR_PHYS, pShwPage->idx, iPdPt, &pShwPage);
1115 if (rc == VERR_PGM_POOL_FLUSHED)
1116 {
1117 Log(("PGMShwSyncEPTPDPtr: PGM pool flushed (2) -> signal sync cr3\n"));
1118 Assert(pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL);
1119 VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3);
1120 return VINF_PGM_SYNC_CR3;
1121 }
1122 AssertRCReturn(rc, rc);
1123 }
1124 else
1125 {
1126 pShwPage = pgmPoolGetPage(pPool, pPdpe->u & EPT_PDPTE_PG_MASK);
1127 AssertReturn(pShwPage, VERR_INTERNAL_ERROR);
1128 }
1129 /* The PD was cached or created; hook it up now and fill with the default value. */
1130 pPdpe->u = pShwPage->Core.Key;
1131 pPdpe->n.u1Present = 1;
1132 pPdpe->n.u1Write = 1;
1133 pPdpe->n.u1Execute = 1;
1134
1135 *ppPD = (PEPTPD)PGMPOOL_PAGE_2_PTR(pVM, pShwPage);
1136 return VINF_SUCCESS;
1137}
1138
1139#endif /* IN_RC */
1140
1141/**
1142 * Gets effective Guest OS page information.
1143 *
1144 * When GCPtr is in a big page, the function will return as if it was a normal
1145 * 4KB page. If the need for distinguishing between big and normal page becomes
1146 * necessary at a later point, a PGMGstGetPage() will be created for that
1147 * purpose.
1148 *
1149 * @returns VBox status.
1150 * @param pVM VM Handle.
1151 * @param GCPtr Guest Context virtual address of the page.
1152 * @param pfFlags Where to store the flags. These are X86_PTE_*, even for big pages.
1153 * @param pGCPhys Where to store the GC physical address of the page.
1154 * This is page aligned. The fact that the
1155 */
1156VMMDECL(int) PGMGstGetPage(PVM pVM, RTGCPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys)
1157{
1158 return PGM_GST_PFN(GetPage,pVM)(pVM, GCPtr, pfFlags, pGCPhys);
1159}
1160
1161
1162/**
1163 * Checks if the page is present.
1164 *
1165 * @returns true if the page is present.
1166 * @returns false if the page is not present.
1167 * @param pVM The VM handle.
1168 * @param GCPtr Address within the page.
1169 */
1170VMMDECL(bool) PGMGstIsPagePresent(PVM pVM, RTGCPTR GCPtr)
1171{
1172 int rc = PGMGstGetPage(pVM, GCPtr, NULL, NULL);
1173 return RT_SUCCESS(rc);
1174}
1175
1176
1177/**
1178 * Sets (replaces) the page flags for a range of pages in the guest's tables.
1179 *
1180 * @returns VBox status.
1181 * @param pVM VM handle.
1182 * @param GCPtr The address of the first page.
1183 * @param cb The size of the range in bytes.
1184 * @param fFlags Page flags X86_PTE_*, excluding the page mask of course.
1185 */
1186VMMDECL(int) PGMGstSetPage(PVM pVM, RTGCPTR GCPtr, size_t cb, uint64_t fFlags)
1187{
1188 return PGMGstModifyPage(pVM, GCPtr, cb, fFlags, 0);
1189}
1190
1191
1192/**
1193 * Modify page flags for a range of pages in the guest's tables
1194 *
1195 * The existing flags are ANDed with the fMask and ORed with the fFlags.
1196 *
1197 * @returns VBox status code.
1198 * @param pVM VM handle.
1199 * @param GCPtr Virtual address of the first page in the range.
1200 * @param cb Size (in bytes) of the range to apply the modification to.
1201 * @param fFlags The OR mask - page flags X86_PTE_*, excluding the page mask of course.
1202 * @param fMask The AND mask - page flags X86_PTE_*, excluding the page mask of course.
1203 * Be very CAREFUL when ~'ing constants which could be 32-bit!
1204 */
1205VMMDECL(int) PGMGstModifyPage(PVM pVM, RTGCPTR GCPtr, size_t cb, uint64_t fFlags, uint64_t fMask)
1206{
1207 STAM_PROFILE_START(&pVM->pgm.s.CTX_MID_Z(Stat,GstModifyPage), a);
1208
1209 /*
1210 * Validate input.
1211 */
1212 AssertMsg(!(fFlags & X86_PTE_PAE_PG_MASK), ("fFlags=%#llx\n", fFlags));
1213 Assert(cb);
1214
1215 LogFlow(("PGMGstModifyPage %RGv %d bytes fFlags=%08llx fMask=%08llx\n", GCPtr, cb, fFlags, fMask));
1216
1217 /*
1218 * Adjust input.
1219 */
1220 cb += GCPtr & PAGE_OFFSET_MASK;
1221 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
1222 GCPtr = (GCPtr & PAGE_BASE_GC_MASK);
1223
1224 /*
1225 * Call worker.
1226 */
1227 int rc = PGM_GST_PFN(ModifyPage, pVM)(pVM, GCPtr, cb, fFlags, fMask);
1228
1229 STAM_PROFILE_STOP(&pVM->pgm.s.CTX_MID_Z(Stat,GstModifyPage), a);
1230 return rc;
1231}
1232
1233
1234/**
1235 * Gets the specified page directory pointer table entry.
1236 *
1237 * @returns PDP entry
1238 * @param pPGM Pointer to the PGM instance data.
1239 * @param iPdpt PDPT index
1240 */
1241VMMDECL(X86PDPE) PGMGstGetPaePDPtr(PVM pVM, unsigned iPdpt)
1242{
1243 Assert(iPdpt <= 3);
1244 return pgmGstGetPaePDPTPtr(&pVM->pgm.s)->a[iPdpt & 3];
1245}
1246
1247
1248/**
1249 * Gets the current CR3 register value for the shadow memory context.
1250 * @returns CR3 value.
1251 * @param pVM The VM handle.
1252 */
1253VMMDECL(RTHCPHYS) PGMGetHyperCR3(PVM pVM)
1254{
1255 PGMMODE enmShadowMode = pVM->pgm.s.enmShadowMode;
1256 switch (enmShadowMode)
1257 {
1258 case PGMMODE_32_BIT:
1259 return pVM->pgm.s.HCPhys32BitPD;
1260
1261 case PGMMODE_PAE:
1262 case PGMMODE_PAE_NX:
1263 return pVM->pgm.s.HCPhysPaePDPT;
1264
1265 case PGMMODE_AMD64:
1266 case PGMMODE_AMD64_NX:
1267 return pVM->pgm.s.HCPhysPaePML4;
1268
1269 case PGMMODE_EPT:
1270 return pVM->pgm.s.HCPhysNestedRoot;
1271
1272 case PGMMODE_NESTED:
1273 return PGMGetNestedCR3(pVM, PGMGetHostMode(pVM));
1274
1275 default:
1276 AssertMsgFailed(("enmShadowMode=%d\n", enmShadowMode));
1277 return ~0;
1278 }
1279}
1280
1281
1282/**
1283 * Gets the current CR3 register value for the nested memory context.
1284 * @returns CR3 value.
1285 * @param pVM The VM handle.
1286 */
1287VMMDECL(RTHCPHYS) PGMGetNestedCR3(PVM pVM, PGMMODE enmShadowMode)
1288{
1289 switch (enmShadowMode)
1290 {
1291 case PGMMODE_32_BIT:
1292 return pVM->pgm.s.HCPhys32BitPD;
1293
1294 case PGMMODE_PAE:
1295 case PGMMODE_PAE_NX:
1296 return pVM->pgm.s.HCPhysPaePDPT;
1297
1298 case PGMMODE_AMD64:
1299 case PGMMODE_AMD64_NX:
1300 return pVM->pgm.s.HCPhysPaePML4;
1301
1302 default:
1303 AssertMsgFailed(("enmShadowMode=%d\n", enmShadowMode));
1304 return ~0;
1305 }
1306}
1307
1308
1309/**
1310 * Gets the current CR3 register value for the EPT paging memory context.
1311 * @returns CR3 value.
1312 * @param pVM The VM handle.
1313 */
1314VMMDECL(RTHCPHYS) PGMGetEPTCR3(PVM pVM)
1315{
1316 return pVM->pgm.s.HCPhysNestedRoot;
1317}
1318
1319
1320/**
1321 * Gets the CR3 register value for the 32-Bit shadow memory context.
1322 * @returns CR3 value.
1323 * @param pVM The VM handle.
1324 */
1325VMMDECL(RTHCPHYS) PGMGetHyper32BitCR3(PVM pVM)
1326{
1327 return pVM->pgm.s.HCPhys32BitPD;
1328}
1329
1330
1331/**
1332 * Gets the CR3 register value for the PAE shadow memory context.
1333 * @returns CR3 value.
1334 * @param pVM The VM handle.
1335 */
1336VMMDECL(RTHCPHYS) PGMGetHyperPaeCR3(PVM pVM)
1337{
1338 return pVM->pgm.s.HCPhysPaePDPT;
1339}
1340
1341
1342/**
1343 * Gets the CR3 register value for the AMD64 shadow memory context.
1344 * @returns CR3 value.
1345 * @param pVM The VM handle.
1346 */
1347VMMDECL(RTHCPHYS) PGMGetHyperAmd64CR3(PVM pVM)
1348{
1349 return pVM->pgm.s.HCPhysPaePML4;
1350}
1351
1352
1353/**
1354 * Gets the current CR3 register value for the HC intermediate memory context.
1355 * @returns CR3 value.
1356 * @param pVM The VM handle.
1357 */
1358VMMDECL(RTHCPHYS) PGMGetInterHCCR3(PVM pVM)
1359{
1360 switch (pVM->pgm.s.enmHostMode)
1361 {
1362 case SUPPAGINGMODE_32_BIT:
1363 case SUPPAGINGMODE_32_BIT_GLOBAL:
1364 return pVM->pgm.s.HCPhysInterPD;
1365
1366 case SUPPAGINGMODE_PAE:
1367 case SUPPAGINGMODE_PAE_GLOBAL:
1368 case SUPPAGINGMODE_PAE_NX:
1369 case SUPPAGINGMODE_PAE_GLOBAL_NX:
1370 return pVM->pgm.s.HCPhysInterPaePDPT;
1371
1372 case SUPPAGINGMODE_AMD64:
1373 case SUPPAGINGMODE_AMD64_GLOBAL:
1374 case SUPPAGINGMODE_AMD64_NX:
1375 case SUPPAGINGMODE_AMD64_GLOBAL_NX:
1376 return pVM->pgm.s.HCPhysInterPaePDPT;
1377
1378 default:
1379 AssertMsgFailed(("enmHostMode=%d\n", pVM->pgm.s.enmHostMode));
1380 return ~0;
1381 }
1382}
1383
1384
1385/**
1386 * Gets the current CR3 register value for the RC intermediate memory context.
1387 * @returns CR3 value.
1388 * @param pVM The VM handle.
1389 */
1390VMMDECL(RTHCPHYS) PGMGetInterRCCR3(PVM pVM)
1391{
1392 switch (pVM->pgm.s.enmShadowMode)
1393 {
1394 case PGMMODE_32_BIT:
1395 return pVM->pgm.s.HCPhysInterPD;
1396
1397 case PGMMODE_PAE:
1398 case PGMMODE_PAE_NX:
1399 return pVM->pgm.s.HCPhysInterPaePDPT;
1400
1401 case PGMMODE_AMD64:
1402 case PGMMODE_AMD64_NX:
1403 return pVM->pgm.s.HCPhysInterPaePML4;
1404
1405 case PGMMODE_EPT:
1406 case PGMMODE_NESTED:
1407 return 0; /* not relevant */
1408
1409 default:
1410 AssertMsgFailed(("enmShadowMode=%d\n", pVM->pgm.s.enmShadowMode));
1411 return ~0;
1412 }
1413}
1414
1415
1416/**
1417 * Gets the CR3 register value for the 32-Bit intermediate memory context.
1418 * @returns CR3 value.
1419 * @param pVM The VM handle.
1420 */
1421VMMDECL(RTHCPHYS) PGMGetInter32BitCR3(PVM pVM)
1422{
1423 return pVM->pgm.s.HCPhysInterPD;
1424}
1425
1426
1427/**
1428 * Gets the CR3 register value for the PAE intermediate memory context.
1429 * @returns CR3 value.
1430 * @param pVM The VM handle.
1431 */
1432VMMDECL(RTHCPHYS) PGMGetInterPaeCR3(PVM pVM)
1433{
1434 return pVM->pgm.s.HCPhysInterPaePDPT;
1435}
1436
1437
1438/**
1439 * Gets the CR3 register value for the AMD64 intermediate memory context.
1440 * @returns CR3 value.
1441 * @param pVM The VM handle.
1442 */
1443VMMDECL(RTHCPHYS) PGMGetInterAmd64CR3(PVM pVM)
1444{
1445 return pVM->pgm.s.HCPhysInterPaePML4;
1446}
1447
1448
1449/**
1450 * Performs and schedules necessary updates following a CR3 load or reload.
1451 *
1452 * This will normally involve mapping the guest PD or nPDPT
1453 *
1454 * @returns VBox status code.
1455 * @retval VINF_PGM_SYNC_CR3 if monitoring requires a CR3 sync. This can
1456 * safely be ignored and overridden since the FF will be set too then.
1457 * @param pVM VM handle.
1458 * @param cr3 The new cr3.
1459 * @param fGlobal Indicates whether this is a global flush or not.
1460 */
1461VMMDECL(int) PGMFlushTLB(PVM pVM, uint64_t cr3, bool fGlobal)
1462{
1463 STAM_PROFILE_START(&pVM->pgm.s.CTX_MID_Z(Stat,FlushTLB), a);
1464
1465 /*
1466 * Always flag the necessary updates; necessary for hardware acceleration
1467 */
1468 VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3_NON_GLOBAL);
1469 if (fGlobal)
1470 VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3);
1471 LogFlow(("PGMFlushTLB: cr3=%RX64 OldCr3=%RX64 fGlobal=%d\n", cr3, pVM->pgm.s.GCPhysCR3, fGlobal));
1472
1473 /*
1474 * Remap the CR3 content and adjust the monitoring if CR3 was actually changed.
1475 */
1476 int rc = VINF_SUCCESS;
1477 RTGCPHYS GCPhysCR3;
1478 if ( pVM->pgm.s.enmGuestMode == PGMMODE_PAE
1479 || pVM->pgm.s.enmGuestMode == PGMMODE_PAE_NX
1480 || pVM->pgm.s.enmGuestMode == PGMMODE_AMD64
1481 || pVM->pgm.s.enmGuestMode == PGMMODE_AMD64_NX)
1482 GCPhysCR3 = (RTGCPHYS)(cr3 & X86_CR3_PAE_PAGE_MASK);
1483 else
1484 GCPhysCR3 = (RTGCPHYS)(cr3 & X86_CR3_PAGE_MASK);
1485 if (pVM->pgm.s.GCPhysCR3 != GCPhysCR3)
1486 {
1487 pVM->pgm.s.GCPhysCR3 = GCPhysCR3;
1488 rc = PGM_GST_PFN(MapCR3, pVM)(pVM, GCPhysCR3);
1489 if (RT_SUCCESS(rc) && !pVM->pgm.s.fMappingsFixed)
1490 {
1491 pVM->pgm.s.fSyncFlags &= ~PGM_SYNC_MONITOR_CR3;
1492 rc = PGM_GST_PFN(MonitorCR3, pVM)(pVM, GCPhysCR3);
1493 }
1494 if (fGlobal)
1495 STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,FlushTLBNewCR3Global));
1496 else
1497 STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,FlushTLBNewCR3));
1498 }
1499 else
1500 {
1501 /*
1502 * Check if we have a pending update of the CR3 monitoring.
1503 */
1504 if (pVM->pgm.s.fSyncFlags & PGM_SYNC_MONITOR_CR3)
1505 {
1506 pVM->pgm.s.fSyncFlags &= ~PGM_SYNC_MONITOR_CR3;
1507 Assert(!pVM->pgm.s.fMappingsFixed);
1508 rc = PGM_GST_PFN(MonitorCR3, pVM)(pVM, GCPhysCR3);
1509 }
1510 if (fGlobal)
1511 STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,FlushTLBSameCR3Global));
1512 else
1513 STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,FlushTLBSameCR3));
1514 }
1515
1516 STAM_PROFILE_STOP(&pVM->pgm.s.CTX_MID_Z(Stat,FlushTLB), a);
1517 return rc;
1518}
1519
1520
1521/**
1522 * Performs and schedules necessary updates following a CR3 load or reload when
1523 * using nested or extended paging.
1524 *
1525 * This API is an alterantive to PDMFlushTLB that avoids actually flushing the
1526 * TLB and triggering a SyncCR3.
1527 *
1528 * This will normally involve mapping the guest PD or nPDPT
1529 *
1530 * @returns VBox status code.
1531 * @retval VINF_PGM_SYNC_CR3 if monitoring requires a CR3 sync. This can
1532 * safely be ignored and overridden since the FF will be set too then.
1533 * @param pVM VM handle.
1534 * @param cr3 The new cr3.
1535 */
1536VMMDECL(int) PGMUpdateCR3(PVM pVM, uint64_t cr3)
1537{
1538 LogFlow(("PGMUpdateCR3: cr3=%RX64 OldCr3=%RX64\n", cr3, pVM->pgm.s.GCPhysCR3));
1539
1540 /* We assume we're only called in nested paging mode. */
1541 Assert(pVM->pgm.s.fMappingsFixed);
1542 Assert(!(pVM->pgm.s.fSyncFlags & PGM_SYNC_MONITOR_CR3));
1543 Assert(pVM->pgm.s.enmShadowMode == PGMMODE_NESTED || pVM->pgm.s.enmShadowMode == PGMMODE_EPT);
1544
1545 /*
1546 * Remap the CR3 content and adjust the monitoring if CR3 was actually changed.
1547 */
1548 int rc = VINF_SUCCESS;
1549 RTGCPHYS GCPhysCR3;
1550 if ( pVM->pgm.s.enmGuestMode == PGMMODE_PAE
1551 || pVM->pgm.s.enmGuestMode == PGMMODE_PAE_NX
1552 || pVM->pgm.s.enmGuestMode == PGMMODE_AMD64
1553 || pVM->pgm.s.enmGuestMode == PGMMODE_AMD64_NX)
1554 GCPhysCR3 = (RTGCPHYS)(cr3 & X86_CR3_PAE_PAGE_MASK);
1555 else
1556 GCPhysCR3 = (RTGCPHYS)(cr3 & X86_CR3_PAGE_MASK);
1557 if (pVM->pgm.s.GCPhysCR3 != GCPhysCR3)
1558 {
1559 pVM->pgm.s.GCPhysCR3 = GCPhysCR3;
1560 rc = PGM_GST_PFN(MapCR3, pVM)(pVM, GCPhysCR3);
1561 }
1562 AssertRC(rc);
1563 return rc;
1564}
1565
1566
1567/**
1568 * Synchronize the paging structures.
1569 *
1570 * This function is called in response to the VM_FF_PGM_SYNC_CR3 and
1571 * VM_FF_PGM_SYNC_CR3_NONGLOBAL. Those two force action flags are set
1572 * in several places, most importantly whenever the CR3 is loaded.
1573 *
1574 * @returns VBox status code.
1575 * @param pVM The virtual machine.
1576 * @param cr0 Guest context CR0 register
1577 * @param cr3 Guest context CR3 register
1578 * @param cr4 Guest context CR4 register
1579 * @param fGlobal Including global page directories or not
1580 */
1581VMMDECL(int) PGMSyncCR3(PVM pVM, uint64_t cr0, uint64_t cr3, uint64_t cr4, bool fGlobal)
1582{
1583 /*
1584 * We might be called when we shouldn't.
1585 *
1586 * The mode switching will ensure that the PD is resynced
1587 * after every mode switch. So, if we find ourselves here
1588 * when in protected or real mode we can safely disable the
1589 * FF and return immediately.
1590 */
1591 if (pVM->pgm.s.enmGuestMode <= PGMMODE_PROTECTED)
1592 {
1593 Assert((cr0 & (X86_CR0_PG | X86_CR0_PE)) != (X86_CR0_PG | X86_CR0_PE));
1594 VM_FF_CLEAR(pVM, VM_FF_PGM_SYNC_CR3);
1595 VM_FF_CLEAR(pVM, VM_FF_PGM_SYNC_CR3_NON_GLOBAL);
1596 return VINF_SUCCESS;
1597 }
1598
1599 /* If global pages are not supported, then all flushes are global */
1600 if (!(cr4 & X86_CR4_PGE))
1601 fGlobal = true;
1602 LogFlow(("PGMSyncCR3: cr0=%RX64 cr3=%RX64 cr4=%RX64 fGlobal=%d[%d,%d]\n", cr0, cr3, cr4, fGlobal,
1603 VM_FF_ISSET(pVM, VM_FF_PGM_SYNC_CR3), VM_FF_ISSET(pVM, VM_FF_PGM_SYNC_CR3_NON_GLOBAL)));
1604
1605 /*
1606 * Let the 'Bth' function do the work and we'll just keep track of the flags.
1607 */
1608 STAM_PROFILE_START(&pVM->pgm.s.CTX_MID_Z(Stat,SyncCR3), a);
1609 int rc = PGM_BTH_PFN(SyncCR3, pVM)(pVM, cr0, cr3, cr4, fGlobal);
1610 STAM_PROFILE_STOP(&pVM->pgm.s.CTX_MID_Z(Stat,SyncCR3), a);
1611 AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 || RT_FAILURE(rc), ("rc=%Rrc\n", rc));
1612 if (rc == VINF_SUCCESS)
1613 {
1614 if (!(pVM->pgm.s.fSyncFlags & PGM_SYNC_ALWAYS))
1615 {
1616 VM_FF_CLEAR(pVM, VM_FF_PGM_SYNC_CR3);
1617 VM_FF_CLEAR(pVM, VM_FF_PGM_SYNC_CR3_NON_GLOBAL);
1618 }
1619
1620 /*
1621 * Check if we have a pending update of the CR3 monitoring.
1622 */
1623 if (pVM->pgm.s.fSyncFlags & PGM_SYNC_MONITOR_CR3)
1624 {
1625 pVM->pgm.s.fSyncFlags &= ~PGM_SYNC_MONITOR_CR3;
1626 Assert(!pVM->pgm.s.fMappingsFixed);
1627 Assert(pVM->pgm.s.GCPhysCR3 == pVM->pgm.s.GCPhysGstCR3Monitored);
1628 rc = PGM_GST_PFN(MonitorCR3, pVM)(pVM, pVM->pgm.s.GCPhysCR3);
1629 }
1630 }
1631
1632 /*
1633 * Now flush the CR3 (guest context).
1634 */
1635 if (rc == VINF_SUCCESS)
1636 PGM_INVL_GUEST_TLBS();
1637 return rc;
1638}
1639
1640
1641/**
1642 * Called whenever CR0 or CR4 in a way which may change
1643 * the paging mode.
1644 *
1645 * @returns VBox status code fit for scheduling in GC and R0.
1646 * @retval VINF_SUCCESS if the was no change, or it was successfully dealt with.
1647 * @retval VINF_PGM_CHANGE_MODE if we're in GC or R0 and the mode changes.
1648 * @param pVM VM handle.
1649 * @param cr0 The new cr0.
1650 * @param cr4 The new cr4.
1651 * @param efer The new extended feature enable register.
1652 */
1653VMMDECL(int) PGMChangeMode(PVM pVM, uint64_t cr0, uint64_t cr4, uint64_t efer)
1654{
1655 PGMMODE enmGuestMode;
1656
1657 /*
1658 * Calc the new guest mode.
1659 */
1660 if (!(cr0 & X86_CR0_PE))
1661 enmGuestMode = PGMMODE_REAL;
1662 else if (!(cr0 & X86_CR0_PG))
1663 enmGuestMode = PGMMODE_PROTECTED;
1664 else if (!(cr4 & X86_CR4_PAE))
1665 enmGuestMode = PGMMODE_32_BIT;
1666 else if (!(efer & MSR_K6_EFER_LME))
1667 {
1668 if (!(efer & MSR_K6_EFER_NXE))
1669 enmGuestMode = PGMMODE_PAE;
1670 else
1671 enmGuestMode = PGMMODE_PAE_NX;
1672 }
1673 else
1674 {
1675 if (!(efer & MSR_K6_EFER_NXE))
1676 enmGuestMode = PGMMODE_AMD64;
1677 else
1678 enmGuestMode = PGMMODE_AMD64_NX;
1679 }
1680
1681 /*
1682 * Did it change?
1683 */
1684 if (pVM->pgm.s.enmGuestMode == enmGuestMode)
1685 return VINF_SUCCESS;
1686
1687 /* Flush the TLB */
1688 PGM_INVL_GUEST_TLBS();
1689
1690#ifdef IN_RING3
1691 return PGMR3ChangeMode(pVM, enmGuestMode);
1692#else
1693 LogFlow(("PGMChangeMode: returns VINF_PGM_CHANGE_MODE.\n"));
1694 return VINF_PGM_CHANGE_MODE;
1695#endif
1696}
1697
1698
1699/**
1700 * Gets the current guest paging mode.
1701 *
1702 * If you just need the CPU mode (real/protected/long), use CPUMGetGuestMode().
1703 *
1704 * @returns The current paging mode.
1705 * @param pVM The VM handle.
1706 */
1707VMMDECL(PGMMODE) PGMGetGuestMode(PVM pVM)
1708{
1709 return pVM->pgm.s.enmGuestMode;
1710}
1711
1712
1713/**
1714 * Gets the current shadow paging mode.
1715 *
1716 * @returns The current paging mode.
1717 * @param pVM The VM handle.
1718 */
1719VMMDECL(PGMMODE) PGMGetShadowMode(PVM pVM)
1720{
1721 return pVM->pgm.s.enmShadowMode;
1722}
1723
1724/**
1725 * Gets the current host paging mode.
1726 *
1727 * @returns The current paging mode.
1728 * @param pVM The VM handle.
1729 */
1730VMMDECL(PGMMODE) PGMGetHostMode(PVM pVM)
1731{
1732 switch (pVM->pgm.s.enmHostMode)
1733 {
1734 case SUPPAGINGMODE_32_BIT:
1735 case SUPPAGINGMODE_32_BIT_GLOBAL:
1736 return PGMMODE_32_BIT;
1737
1738 case SUPPAGINGMODE_PAE:
1739 case SUPPAGINGMODE_PAE_GLOBAL:
1740 return PGMMODE_PAE;
1741
1742 case SUPPAGINGMODE_PAE_NX:
1743 case SUPPAGINGMODE_PAE_GLOBAL_NX:
1744 return PGMMODE_PAE_NX;
1745
1746 case SUPPAGINGMODE_AMD64:
1747 case SUPPAGINGMODE_AMD64_GLOBAL:
1748 return PGMMODE_AMD64;
1749
1750 case SUPPAGINGMODE_AMD64_NX:
1751 case SUPPAGINGMODE_AMD64_GLOBAL_NX:
1752 return PGMMODE_AMD64_NX;
1753
1754 default: AssertMsgFailed(("enmHostMode=%d\n", pVM->pgm.s.enmHostMode)); break;
1755 }
1756
1757 return PGMMODE_INVALID;
1758}
1759
1760
1761/**
1762 * Get mode name.
1763 *
1764 * @returns read-only name string.
1765 * @param enmMode The mode which name is desired.
1766 */
1767VMMDECL(const char *) PGMGetModeName(PGMMODE enmMode)
1768{
1769 switch (enmMode)
1770 {
1771 case PGMMODE_REAL: return "Real";
1772 case PGMMODE_PROTECTED: return "Protected";
1773 case PGMMODE_32_BIT: return "32-bit";
1774 case PGMMODE_PAE: return "PAE";
1775 case PGMMODE_PAE_NX: return "PAE+NX";
1776 case PGMMODE_AMD64: return "AMD64";
1777 case PGMMODE_AMD64_NX: return "AMD64+NX";
1778 case PGMMODE_NESTED: return "Nested";
1779 case PGMMODE_EPT: return "EPT";
1780 default: return "unknown mode value";
1781 }
1782}
1783
1784
1785/**
1786 * Acquire the PGM lock.
1787 *
1788 * @returns VBox status code
1789 * @param pVM The VM to operate on.
1790 */
1791int pgmLock(PVM pVM)
1792{
1793 int rc = PDMCritSectEnter(&pVM->pgm.s.CritSect, VERR_SEM_BUSY);
1794#ifdef IN_RC
1795 if (rc == VERR_SEM_BUSY)
1796 rc = VMMGCCallHost(pVM, VMMCALLHOST_PGM_LOCK, 0);
1797#elif defined(IN_RING0)
1798 if (rc == VERR_SEM_BUSY)
1799 rc = VMMR0CallHost(pVM, VMMCALLHOST_PGM_LOCK, 0);
1800#endif
1801 AssertRC(rc);
1802 return rc;
1803}
1804
1805
1806/**
1807 * Release the PGM lock.
1808 *
1809 * @returns VBox status code
1810 * @param pVM The VM to operate on.
1811 */
1812void pgmUnlock(PVM pVM)
1813{
1814 PDMCritSectLeave(&pVM->pgm.s.CritSect);
1815}
1816
1817#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
1818
1819/**
1820 * Temporarily maps one guest page specified by GC physical address.
1821 * These pages must have a physical mapping in HC, i.e. they cannot be MMIO pages.
1822 *
1823 * Be WARNED that the dynamic page mapping area is small, 8 pages, thus the space is
1824 * reused after 8 mappings (or perhaps a few more if you score with the cache).
1825 *
1826 * @returns VBox status.
1827 * @param pVM VM handle.
1828 * @param GCPhys GC Physical address of the page.
1829 * @param ppv Where to store the address of the mapping.
1830 */
1831VMMDECL(int) PGMDynMapGCPage(PVM pVM, RTGCPHYS GCPhys, void **ppv)
1832{
1833 AssertMsg(!(GCPhys & PAGE_OFFSET_MASK), ("GCPhys=%RGp\n", GCPhys));
1834
1835 /*
1836 * Get the ram range.
1837 */
1838 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
1839 while (pRam && GCPhys - pRam->GCPhys >= pRam->cb)
1840 pRam = pRam->CTX_SUFF(pNext);
1841 if (!pRam)
1842 {
1843 AssertMsgFailed(("Invalid physical address %RGp!\n", GCPhys));
1844 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
1845 }
1846
1847 /*
1848 * Pass it on to PGMDynMapHCPage.
1849 */
1850 RTHCPHYS HCPhys = PGM_PAGE_GET_HCPHYS(&pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT]);
1851 //Log(("PGMDynMapGCPage: GCPhys=%RGp HCPhys=%RHp\n", GCPhys, HCPhys));
1852 return PGMDynMapHCPage(pVM, HCPhys, ppv);
1853}
1854
1855
1856/**
1857 * Temporarily maps one guest page specified by unaligned GC physical address.
1858 * These pages must have a physical mapping in HC, i.e. they cannot be MMIO pages.
1859 *
1860 * Be WARNED that the dynamic page mapping area is small, 8 pages, thus the space is
1861 * reused after 8 mappings (or perhaps a few more if you score with the cache).
1862 *
1863 * The caller is aware that only the speicifed page is mapped and that really bad things
1864 * will happen if writing beyond the page!
1865 *
1866 * @returns VBox status.
1867 * @param pVM VM handle.
1868 * @param GCPhys GC Physical address within the page to be mapped.
1869 * @param ppv Where to store the address of the mapping address corresponding to GCPhys.
1870 */
1871VMMDECL(int) PGMDynMapGCPageOff(PVM pVM, RTGCPHYS GCPhys, void **ppv)
1872{
1873 /*
1874 * Get the ram range.
1875 */
1876 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
1877 while (pRam && GCPhys - pRam->GCPhys >= pRam->cb)
1878 pRam = pRam->CTX_SUFF(pNext);
1879 if (!pRam)
1880 {
1881 AssertMsgFailed(("Invalid physical address %RGp!\n", GCPhys));
1882 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
1883 }
1884
1885 /*
1886 * Pass it on to PGMDynMapHCPage.
1887 */
1888 RTHCPHYS HCPhys = PGM_PAGE_GET_HCPHYS(&pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT]);
1889 int rc = PGMDynMapHCPage(pVM, HCPhys, ppv);
1890 if (RT_SUCCESS(rc))
1891 *ppv = (void *)((uintptr_t)*ppv | (GCPhys & PAGE_OFFSET_MASK));
1892 return rc;
1893}
1894
1895
1896/**
1897 * Temporarily maps one host page specified by HC physical address.
1898 *
1899 * Be WARNED that the dynamic page mapping area is small, 8 pages, thus the space is
1900 * reused after 8 mappings (or perhaps a few more if you score with the cache).
1901 *
1902 * @returns VBox status.
1903 * @param pVM VM handle.
1904 * @param HCPhys HC Physical address of the page.
1905 * @param ppv Where to store the address of the mapping. This is the
1906 * address of the PAGE not the exact address corresponding
1907 * to HCPhys. Use PGMDynMapHCPageOff if you care for the
1908 * page offset.
1909 */
1910VMMDECL(int) PGMDynMapHCPage(PVM pVM, RTHCPHYS HCPhys, void **ppv)
1911{
1912 AssertMsg(!(HCPhys & PAGE_OFFSET_MASK), ("HCPhys=%RHp\n", HCPhys));
1913# ifdef IN_RC
1914
1915 /*
1916 * Check the cache.
1917 */
1918 register unsigned iCache;
1919 if ( pVM->pgm.s.aHCPhysDynPageMapCache[iCache = 0] == HCPhys
1920 || pVM->pgm.s.aHCPhysDynPageMapCache[iCache = 1] == HCPhys
1921 || pVM->pgm.s.aHCPhysDynPageMapCache[iCache = 2] == HCPhys
1922 || pVM->pgm.s.aHCPhysDynPageMapCache[iCache = 3] == HCPhys)
1923 {
1924 static const uint8_t au8Trans[MM_HYPER_DYNAMIC_SIZE >> PAGE_SHIFT][RT_ELEMENTS(pVM->pgm.s.aHCPhysDynPageMapCache)] =
1925 {
1926 { 0, 5, 6, 7 },
1927 { 0, 1, 6, 7 },
1928 { 0, 1, 2, 7 },
1929 { 0, 1, 2, 3 },
1930 { 4, 1, 2, 3 },
1931 { 4, 5, 2, 3 },
1932 { 4, 5, 6, 3 },
1933 { 4, 5, 6, 7 },
1934 };
1935 Assert(RT_ELEMENTS(au8Trans) == 8);
1936 Assert(RT_ELEMENTS(au8Trans[0]) == 4);
1937 int iPage = au8Trans[pVM->pgm.s.iDynPageMapLast][iCache];
1938 void *pv = pVM->pgm.s.pbDynPageMapBaseGC + (iPage << PAGE_SHIFT);
1939 *ppv = pv;
1940 STAM_COUNTER_INC(&pVM->pgm.s.StatRCDynMapCacheHits);
1941 //Log(("PGMGCDynMapHCPage: HCPhys=%RHp pv=%p iPage=%d iCache=%d\n", HCPhys, pv, iPage, iCache));
1942 return VINF_SUCCESS;
1943 }
1944 Assert(RT_ELEMENTS(pVM->pgm.s.aHCPhysDynPageMapCache) == 4);
1945 STAM_COUNTER_INC(&pVM->pgm.s.StatRCDynMapCacheMisses);
1946
1947 /*
1948 * Update the page tables.
1949 */
1950 register unsigned iPage = pVM->pgm.s.iDynPageMapLast;
1951 pVM->pgm.s.iDynPageMapLast = iPage = (iPage + 1) & ((MM_HYPER_DYNAMIC_SIZE >> PAGE_SHIFT) - 1);
1952 Assert((MM_HYPER_DYNAMIC_SIZE >> PAGE_SHIFT) == 8);
1953
1954 pVM->pgm.s.aHCPhysDynPageMapCache[iPage & (RT_ELEMENTS(pVM->pgm.s.aHCPhysDynPageMapCache) - 1)] = HCPhys;
1955 pVM->pgm.s.paDynPageMap32BitPTEsGC[iPage].u = (uint32_t)HCPhys | X86_PTE_P | X86_PTE_A | X86_PTE_D;
1956 pVM->pgm.s.paDynPageMapPaePTEsGC[iPage].u = HCPhys | X86_PTE_P | X86_PTE_A | X86_PTE_D;
1957
1958 void *pv = pVM->pgm.s.pbDynPageMapBaseGC + (iPage << PAGE_SHIFT);
1959 *ppv = pv;
1960 ASMInvalidatePage(pv);
1961 Log4(("PGMGCDynMapHCPage: HCPhys=%RHp pv=%p iPage=%d\n", HCPhys, pv, iPage));
1962 return VINF_SUCCESS;
1963
1964#else /* VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
1965 /** @todo @bugref{3202}: Implement ring-0 mapping cache similar to the one in
1966 * RC. To begin with, a simple but expensive one based on
1967 * RTR0MemObjEnterPhys can be used to get things started. Later a
1968 * global cache with mappings per CPU (to avoid shootdown) should be
1969 * employed. */
1970 AssertFailed();
1971 return VERR_NOT_IMPLEMENTED;
1972#endif /* VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
1973}
1974
1975
1976/**
1977 * Temporarily maps one host page specified by HC physical address, returning
1978 * pointer within the page.
1979 *
1980 * Be WARNED that the dynamic page mapping area is small, 8 pages, thus the space is
1981 * reused after 8 mappings (or perhaps a few more if you score with the cache).
1982 *
1983 * @returns VBox status.
1984 * @param pVM VM handle.
1985 * @param HCPhys HC Physical address of the page.
1986 * @param ppv Where to store the address corresponding to HCPhys.
1987 */
1988VMMDECL(int) PGMDynMapHCPageOff(PVM pVM, RTHCPHYS HCPhys, void **ppv)
1989{
1990 int rc = PGMDynMapHCPage(pVM, HCPhys & ~(RTHCPHYS)PAGE_OFFSET_MASK, ppv);
1991 if (RT_SUCCESS(rc))
1992 *ppv = (void *)((uintptr_t)*ppv | (HCPhys & PAGE_OFFSET_MASK));
1993 return rc;
1994}
1995
1996#endif /* IN_RC || VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
1997#ifdef VBOX_STRICT
1998
1999/**
2000 * Asserts that there are no mapping conflicts.
2001 *
2002 * @returns Number of conflicts.
2003 * @param pVM The VM Handle.
2004 */
2005VMMDECL(unsigned) PGMAssertNoMappingConflicts(PVM pVM)
2006{
2007 unsigned cErrors = 0;
2008
2009 /*
2010 * Check for mapping conflicts.
2011 */
2012 for (PPGMMAPPING pMapping = pVM->pgm.s.CTX_SUFF(pMappings);
2013 pMapping;
2014 pMapping = pMapping->CTX_SUFF(pNext))
2015 {
2016 /** @todo This is slow and should be optimized, but since it's just assertions I don't care now. */
2017 for (RTGCPTR GCPtr = pMapping->GCPtr;
2018 GCPtr <= pMapping->GCPtrLast;
2019 GCPtr += PAGE_SIZE)
2020 {
2021 int rc = PGMGstGetPage(pVM, (RTGCPTR)GCPtr, NULL, NULL);
2022 if (rc != VERR_PAGE_TABLE_NOT_PRESENT)
2023 {
2024 AssertMsgFailed(("Conflict at %RGv with %s\n", GCPtr, R3STRING(pMapping->pszDesc)));
2025 cErrors++;
2026 break;
2027 }
2028 }
2029 }
2030
2031 return cErrors;
2032}
2033
2034
2035/**
2036 * Asserts that everything related to the guest CR3 is correctly shadowed.
2037 *
2038 * This will call PGMAssertNoMappingConflicts() and PGMAssertHandlerAndFlagsInSync(),
2039 * and assert the correctness of the guest CR3 mapping before asserting that the
2040 * shadow page tables is in sync with the guest page tables.
2041 *
2042 * @returns Number of conflicts.
2043 * @param pVM The VM Handle.
2044 * @param cr3 The current guest CR3 register value.
2045 * @param cr4 The current guest CR4 register value.
2046 */
2047VMMDECL(unsigned) PGMAssertCR3(PVM pVM, uint64_t cr3, uint64_t cr4)
2048{
2049 STAM_PROFILE_START(&pVM->pgm.s.CTX_MID_Z(Stat,SyncCR3), a);
2050 unsigned cErrors = PGM_BTH_PFN(AssertCR3, pVM)(pVM, cr3, cr4, 0, ~(RTGCPTR)0);
2051 STAM_PROFILE_STOP(&pVM->pgm.s.CTX_MID_Z(Stat,SyncCR3), a);
2052 return cErrors;
2053 return 0;
2054}
2055
2056#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