VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/PGMShw.h@ 73246

Last change on this file since 73246 was 73246, checked in by vboxsync, 6 years ago

PGM: Working on eliminating PGMMODEDATA and the corresponding PGMCPU section so we can do mode switching in ring-0. This second part deals with shadow paging pointers and expands PGM_TYPE_NESTED & PGMMODE_NESTED into 32BIT, PAE and AMD64 variants to better map to reality at the expense of a little bit of more code. bugref:9044

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 7.7 KB
Line 
1/* $Id: PGMShw.h 73246 2018-07-19 15:51:20Z vboxsync $ */
2/** @file
3 * VBox - Page Manager / Monitor, Shadow Paging Template.
4 */
5
6/*
7 * Copyright (C) 2006-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*******************************************************************************
19* Defined Constants And Macros *
20*******************************************************************************/
21#undef SHWPT
22#undef PSHWPT
23#undef SHWPTE
24#undef PSHWPTE
25#undef SHWPD
26#undef PSHWPD
27#undef SHWPDE
28#undef PSHWPDE
29#undef SHW_PDE_PG_MASK
30#undef SHW_PD_SHIFT
31#undef SHW_PD_MASK
32#undef SHW_PTE_PG_MASK
33#undef SHW_PT_SHIFT
34#undef SHW_PT_MASK
35#undef SHW_TOTAL_PD_ENTRIES
36#undef SHW_PDPT_SHIFT
37#undef SHW_PDPT_MASK
38#undef SHW_PDPE_PG_MASK
39
40#if PGM_SHW_TYPE == PGM_TYPE_32BIT
41# define SHWPT X86PT
42# define PSHWPT PX86PT
43# define SHWPTE X86PTE
44# define PSHWPTE PX86PTE
45# define SHWPD X86PD
46# define PSHWPD PX86PD
47# define SHWPDE X86PDE
48# define PSHWPDE PX86PDE
49# define SHW_PDE_PG_MASK X86_PDE_PG_MASK
50# define SHW_PD_SHIFT X86_PD_SHIFT
51# define SHW_PD_MASK X86_PD_MASK
52# define SHW_TOTAL_PD_ENTRIES X86_PG_ENTRIES
53# define SHW_PTE_PG_MASK X86_PTE_PG_MASK
54# define SHW_PT_SHIFT X86_PT_SHIFT
55# define SHW_PT_MASK X86_PT_MASK
56
57#elif PGM_SHW_TYPE == PGM_TYPE_EPT
58# define SHWPT EPTPT
59# define PSHWPT PEPTPT
60# define SHWPTE EPTPTE
61# define PSHWPTE PEPTPTE
62# define SHWPD EPTPD
63# define PSHWPD PEPTPD
64# define SHWPDE EPTPDE
65# define PSHWPDE PEPTPDE
66# define SHW_PDE_PG_MASK EPT_PDE_PG_MASK
67# define SHW_PD_SHIFT EPT_PD_SHIFT
68# define SHW_PD_MASK EPT_PD_MASK
69# define SHW_PTE_PG_MASK EPT_PTE_PG_MASK
70# define SHW_PT_SHIFT EPT_PT_SHIFT
71# define SHW_PT_MASK EPT_PT_MASK
72# define SHW_PDPT_SHIFT EPT_PDPT_SHIFT
73# define SHW_PDPT_MASK EPT_PDPT_MASK
74# define SHW_PDPE_PG_MASK EPT_PDPE_PG_MASK
75# define SHW_TOTAL_PD_ENTRIES (EPT_PG_AMD64_ENTRIES*EPT_PG_AMD64_PDPE_ENTRIES)
76
77#else
78# define SHWPT PGMSHWPTPAE
79# define PSHWPT PPGMSHWPTPAE
80# define SHWPTE PGMSHWPTEPAE
81# define PSHWPTE PPGMSHWPTEPAE
82# define SHWPD X86PDPAE
83# define PSHWPD PX86PDPAE
84# define SHWPDE X86PDEPAE
85# define PSHWPDE PX86PDEPAE
86# define SHW_PDE_PG_MASK X86_PDE_PAE_PG_MASK
87# define SHW_PD_SHIFT X86_PD_PAE_SHIFT
88# define SHW_PD_MASK X86_PD_PAE_MASK
89# define SHW_PTE_PG_MASK X86_PTE_PAE_PG_MASK
90# define SHW_PT_SHIFT X86_PT_PAE_SHIFT
91# define SHW_PT_MASK X86_PT_PAE_MASK
92
93# if PGM_SHW_TYPE == PGM_TYPE_AMD64
94# define SHW_PDPT_SHIFT X86_PDPT_SHIFT
95# define SHW_PDPT_MASK X86_PDPT_MASK_AMD64
96# define SHW_PDPE_PG_MASK X86_PDPE_PG_MASK
97# define SHW_TOTAL_PD_ENTRIES (X86_PG_AMD64_ENTRIES*X86_PG_AMD64_PDPE_ENTRIES)
98
99# else /* 32 bits PAE mode */
100# define SHW_PDPT_SHIFT X86_PDPT_SHIFT
101# define SHW_PDPT_MASK X86_PDPT_MASK_PAE
102# define SHW_PDPE_PG_MASK X86_PDPE_PG_MASK
103# define SHW_TOTAL_PD_ENTRIES (X86_PG_PAE_ENTRIES*X86_PG_PAE_PDPE_ENTRIES)
104# endif
105#endif
106
107
108/*******************************************************************************
109* Internal Functions *
110*******************************************************************************/
111RT_C_DECLS_BEGIN
112/* r3 */
113PGM_SHW_DECL(int, Enter)(PVMCPU pVCpu, bool fIs64BitsPagingMode);
114PGM_SHW_DECL(int, Relocate)(PVMCPU pVCpu, RTGCPTR offDelta);
115PGM_SHW_DECL(int, Exit)(PVMCPU pVCpu);
116
117/* all */
118PGM_SHW_DECL(int, GetPage)(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys);
119PGM_SHW_DECL(int, ModifyPage)(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cb, uint64_t fFlags, uint64_t fMask, uint32_t fOpFlags);
120RT_C_DECLS_END
121
122
123/**
124 * Enters the shadow mode.
125 *
126 * @returns VBox status code.
127 * @param pVCpu The cross context virtual CPU structure.
128 * @param fIs64BitsPagingMode New shadow paging mode is for 64 bits? (only relevant for 64 bits guests on a 32 bits AMD-V nested paging host)
129 */
130PGM_SHW_DECL(int, Enter)(PVMCPU pVCpu, bool fIs64BitsPagingMode)
131{
132#if PGM_TYPE_IS_NESTED_OR_EPT(PGM_SHW_TYPE)
133
134# if PGM_TYPE_IS_NESTED(PGM_SHW_TYPE) && HC_ARCH_BITS == 32
135 /* Must distinguish between 32 and 64 bits guest paging modes as we'll use
136 a different shadow paging root/mode in both cases. */
137 RTGCPHYS GCPhysCR3 = (fIs64BitsPagingMode) ? RT_BIT_64(63) : RT_BIT_64(62);
138# else
139 RTGCPHYS GCPhysCR3 = RT_BIT_64(63); NOREF(fIs64BitsPagingMode);
140# endif
141 PPGMPOOLPAGE pNewShwPageCR3;
142 PVM pVM = pVCpu->pVMR3;
143
144 Assert((HMIsNestedPagingActive(pVM) || VM_IS_NEM_ENABLED(pVM)) == pVM->pgm.s.fNestedPaging);
145 Assert(pVM->pgm.s.fNestedPaging);
146 Assert(!pVCpu->pgm.s.pShwPageCR3R3);
147
148 pgmLock(pVM);
149
150 int rc = pgmPoolAlloc(pVM, GCPhysCR3, PGMPOOLKIND_ROOT_NESTED, PGMPOOLACCESS_DONTCARE, PGM_A20_IS_ENABLED(pVCpu),
151 NIL_PGMPOOL_IDX, UINT32_MAX, true /*fLockPage*/,
152 &pNewShwPageCR3);
153 AssertFatalRC(rc);
154
155 pVCpu->pgm.s.pShwPageCR3R3 = pNewShwPageCR3;
156
157 pVCpu->pgm.s.pShwPageCR3RC = MMHyperCCToRC(pVM, pVCpu->pgm.s.pShwPageCR3R3);
158 pVCpu->pgm.s.pShwPageCR3R0 = MMHyperCCToR0(pVM, pVCpu->pgm.s.pShwPageCR3R3);
159
160 pgmUnlock(pVM);
161
162 Log(("Enter nested shadow paging mode: root %RHv phys %RHp\n", pVCpu->pgm.s.pShwPageCR3R3, pVCpu->pgm.s.CTX_SUFF(pShwPageCR3)->Core.Key));
163#else
164 NOREF(pVCpu); NOREF(fIs64BitsPagingMode);
165#endif
166 return VINF_SUCCESS;
167}
168
169
170/**
171 * Relocate any GC pointers related to shadow mode paging.
172 *
173 * @returns VBox status code.
174 * @param pVCpu The cross context virtual CPU structure.
175 * @param offDelta The relocation offset.
176 */
177PGM_SHW_DECL(int, Relocate)(PVMCPU pVCpu, RTGCPTR offDelta)
178{
179 pVCpu->pgm.s.pShwPageCR3RC += offDelta;
180 return VINF_SUCCESS;
181}
182
183
184/**
185 * Exits the shadow mode.
186 *
187 * @returns VBox status code.
188 * @param pVCpu The cross context virtual CPU structure.
189 */
190PGM_SHW_DECL(int, Exit)(PVMCPU pVCpu)
191{
192#if PGM_TYPE_IS_NESTED_OR_EPT(PGM_SHW_TYPE)
193 PVM pVM = pVCpu->pVMR3;
194 if (pVCpu->pgm.s.CTX_SUFF(pShwPageCR3))
195 {
196 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
197
198 pgmLock(pVM);
199
200 /* Do *not* unlock this page as we have two of them floating around in the 32-bit host & 64-bit guest case.
201 * We currently assert when you try to free one of them; don't bother to really allow this.
202 *
203 * Note that this is two nested paging root pages max. This isn't a leak. They are reused.
204 */
205 /* pgmPoolUnlockPage(pPool, pVCpu->pgm.s.CTX_SUFF(pShwPageCR3)); */
206
207 pgmPoolFreeByPage(pPool, pVCpu->pgm.s.CTX_SUFF(pShwPageCR3), NIL_PGMPOOL_IDX, UINT32_MAX);
208 pVCpu->pgm.s.pShwPageCR3R3 = 0;
209 pVCpu->pgm.s.pShwPageCR3R0 = 0;
210 pVCpu->pgm.s.pShwPageCR3RC = 0;
211
212 pgmUnlock(pVM);
213
214 Log(("Leave nested shadow paging mode\n"));
215 }
216#else
217 RT_NOREF_PV(pVCpu);
218#endif
219 return VINF_SUCCESS;
220}
221
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