VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/PGMAllShw.h@ 18330

Last change on this file since 18330 was 17586, checked in by vboxsync, 16 years ago

Removed all dead non-VBOX_WITH_PGMPOOL_PAGING_ONLY code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 12.7 KB
Line 
1/* $Id: PGMAllShw.h 17586 2009-03-09 15:28:25Z vboxsync $ */
2/** @file
3 * VBox - Page Manager, Shadow Paging Template - 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* Defined Constants And Macros *
24*******************************************************************************/
25#undef SHWPT
26#undef PSHWPT
27#undef SHWPTE
28#undef PSHWPTE
29#undef SHWPD
30#undef PSHWPD
31#undef SHWPDE
32#undef PSHWPDE
33#undef SHW_PDE_PG_MASK
34#undef SHW_PD_SHIFT
35#undef SHW_PD_MASK
36#undef SHW_PTE_PG_MASK
37#undef SHW_PT_SHIFT
38#undef SHW_PT_MASK
39#undef SHW_TOTAL_PD_ENTRIES
40#undef SHW_PDPT_SHIFT
41#undef SHW_PDPT_MASK
42#undef SHW_PDPE_PG_MASK
43#undef SHW_POOL_ROOT_IDX
44
45#if PGM_SHW_TYPE == PGM_TYPE_32BIT
46# define SHWPT X86PT
47# define PSHWPT PX86PT
48# define SHWPTE X86PTE
49# define PSHWPTE PX86PTE
50# define SHWPD X86PD
51# define PSHWPD PX86PD
52# define SHWPDE X86PDE
53# define PSHWPDE PX86PDE
54# define SHW_PDE_PG_MASK X86_PDE_PG_MASK
55# define SHW_PD_SHIFT X86_PD_SHIFT
56# define SHW_PD_MASK X86_PD_MASK
57# define SHW_TOTAL_PD_ENTRIES X86_PG_ENTRIES
58# define SHW_PTE_PG_MASK X86_PTE_PG_MASK
59# define SHW_PT_SHIFT X86_PT_SHIFT
60# define SHW_PT_MASK X86_PT_MASK
61# define SHW_POOL_ROOT_IDX PGMPOOL_IDX_PD
62
63#elif PGM_SHW_TYPE == PGM_TYPE_EPT
64# define SHWPT EPTPT
65# define PSHWPT PEPTPT
66# define SHWPTE EPTPTE
67# define PSHWPTE PEPTPTE
68# define SHWPD EPTPD
69# define PSHWPD PEPTPD
70# define SHWPDE EPTPDE
71# define PSHWPDE PEPTPDE
72# define SHW_PDE_PG_MASK EPT_PDE_PG_MASK
73# define SHW_PD_SHIFT EPT_PD_SHIFT
74# define SHW_PD_MASK EPT_PD_MASK
75# define SHW_PTE_PG_MASK EPT_PTE_PG_MASK
76# define SHW_PT_SHIFT EPT_PT_SHIFT
77# define SHW_PT_MASK EPT_PT_MASK
78# define SHW_PDPT_SHIFT EPT_PDPT_SHIFT
79# define SHW_PDPT_MASK EPT_PDPT_MASK
80# define SHW_PDPE_PG_MASK EPT_PDPE_PG_MASK
81# define SHW_TOTAL_PD_ENTRIES (EPT_PG_AMD64_ENTRIES*EPT_PG_AMD64_PDPE_ENTRIES)
82# define SHW_POOL_ROOT_IDX PGMPOOL_IDX_NESTED_ROOT /* do not use! exception is real mode & protected mode without paging. */
83
84#else
85# define SHWPT X86PTPAE
86# define PSHWPT PX86PTPAE
87# define SHWPTE X86PTEPAE
88# define PSHWPTE PX86PTEPAE
89# define SHWPD X86PDPAE
90# define PSHWPD PX86PDPAE
91# define SHWPDE X86PDEPAE
92# define PSHWPDE PX86PDEPAE
93# define SHW_PDE_PG_MASK X86_PDE_PAE_PG_MASK
94# define SHW_PD_SHIFT X86_PD_PAE_SHIFT
95# define SHW_PD_MASK X86_PD_PAE_MASK
96# define SHW_PTE_PG_MASK X86_PTE_PAE_PG_MASK
97# define SHW_PT_SHIFT X86_PT_PAE_SHIFT
98# define SHW_PT_MASK X86_PT_PAE_MASK
99
100# if PGM_SHW_TYPE == PGM_TYPE_AMD64
101# define SHW_PDPT_SHIFT X86_PDPT_SHIFT
102# define SHW_PDPT_MASK X86_PDPT_MASK_AMD64
103# define SHW_PDPE_PG_MASK X86_PDPE_PG_MASK
104# define SHW_TOTAL_PD_ENTRIES (X86_PG_AMD64_ENTRIES*X86_PG_AMD64_PDPE_ENTRIES)
105# define SHW_POOL_ROOT_IDX PGMPOOL_IDX_AMD64_CR3
106
107# else /* 32 bits PAE mode */
108# define SHW_PDPT_SHIFT X86_PDPT_SHIFT
109# define SHW_PDPT_MASK X86_PDPT_MASK_PAE
110# define SHW_PDPE_PG_MASK X86_PDPE_PG_MASK
111# define SHW_TOTAL_PD_ENTRIES (X86_PG_PAE_ENTRIES*X86_PG_PAE_PDPE_ENTRIES)
112# define SHW_POOL_ROOT_IDX PGMPOOL_IDX_PDPT
113
114# endif
115#endif
116
117
118
119/*******************************************************************************
120* Internal Functions *
121*******************************************************************************/
122__BEGIN_DECLS
123PGM_SHW_DECL(int, GetPage)(PVM pVM, RTGCUINTPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys);
124PGM_SHW_DECL(int, ModifyPage)(PVM pVM, RTGCUINTPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask);
125__END_DECLS
126
127
128
129/**
130 * Gets effective page information (from the VMM page directory).
131 *
132 * @returns VBox status.
133 * @param pVM VM Handle.
134 * @param GCPtr Guest Context virtual address of the page.
135 * @param pfFlags Where to store the flags. These are X86_PTE_*.
136 * @param pHCPhys Where to store the HC physical address of the page.
137 * This is page aligned.
138 * @remark You should use PGMMapGetPage() for pages in a mapping.
139 */
140PGM_SHW_DECL(int, GetPage)(PVM pVM, RTGCUINTPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys)
141{
142#if PGM_SHW_TYPE == PGM_TYPE_NESTED
143 return VERR_PAGE_TABLE_NOT_PRESENT;
144
145#else /* PGM_SHW_TYPE != PGM_TYPE_NESTED && PGM_SHW_TYPE != PGM_TYPE_EPT */
146 /*
147 * Get the PDE.
148 */
149# if PGM_SHW_TYPE == PGM_TYPE_AMD64
150 bool fNoExecuteBitValid = !!(CPUMGetGuestEFER(pVM) & MSR_K6_EFER_NXE);
151 X86PDEPAE Pde;
152
153 /* PML4 */
154 X86PML4E Pml4e = pgmShwGetLongModePML4E(&pVM->pgm.s, GCPtr);
155 if (!Pml4e.n.u1Present)
156 return VERR_PAGE_TABLE_NOT_PRESENT;
157
158 /* PDPT */
159 PX86PDPT pPDPT;
160 int rc = PGM_HCPHYS_2_PTR(pVM, Pml4e.u & X86_PML4E_PG_MASK, &pPDPT);
161 if (RT_FAILURE(rc))
162 return rc;
163 const unsigned iPDPT = (GCPtr >> SHW_PDPT_SHIFT) & SHW_PDPT_MASK;
164 X86PDPE Pdpe = pPDPT->a[iPDPT];
165 if (!Pdpe.n.u1Present)
166 return VERR_PAGE_TABLE_NOT_PRESENT;
167
168 /* PD */
169 PX86PDPAE pPd;
170 rc = PGM_HCPHYS_2_PTR(pVM, Pdpe.u & X86_PDPE_PG_MASK, &pPd);
171 if (RT_FAILURE(rc))
172 return rc;
173 const unsigned iPd = (GCPtr >> SHW_PD_SHIFT) & SHW_PD_MASK;
174 Pde = pPd->a[iPd];
175
176 /* Merge accessed, write, user and no-execute bits into the PDE. */
177 Pde.n.u1Accessed &= Pml4e.n.u1Accessed & Pdpe.lm.u1Accessed;
178 Pde.n.u1Write &= Pml4e.n.u1Write & Pdpe.lm.u1Write;
179 Pde.n.u1User &= Pml4e.n.u1User & Pdpe.lm.u1User;
180 Pde.n.u1NoExecute &= Pml4e.n.u1NoExecute & Pdpe.lm.u1NoExecute;
181
182# elif PGM_SHW_TYPE == PGM_TYPE_PAE
183 bool fNoExecuteBitValid = !!(CPUMGetGuestEFER(pVM) & MSR_K6_EFER_NXE);
184 X86PDEPAE Pde = pgmShwGetPaePDE(&pVM->pgm.s, GCPtr);
185
186# elif PGM_SHW_TYPE == PGM_TYPE_EPT
187 const unsigned iPd = ((GCPtr >> SHW_PD_SHIFT) & SHW_PD_MASK);
188 PEPTPD pPDDst;
189 EPTPDE Pde;
190
191 int rc = pgmShwGetEPTPDPtr(pVM, GCPtr, NULL, &pPDDst);
192 if (rc != VINF_SUCCESS) /** @todo this function isn't expected to return informational status codes. Check callers / fix. */
193 {
194 AssertRC(rc);
195 return rc;
196 }
197 Assert(pPDDst);
198 Pde = pPDDst->a[iPd];
199
200# else /* PGM_TYPE_32BIT */
201 X86PDE Pde = pgmShwGet32BitPDE(&pVM->pgm.s, GCPtr);
202# endif
203 if (!Pde.n.u1Present)
204 return VERR_PAGE_TABLE_NOT_PRESENT;
205
206 Assert(!Pde.b.u1Size);
207
208 /*
209 * Get PT entry.
210 */
211 PSHWPT pPT;
212 if (!(Pde.u & PGM_PDFLAGS_MAPPING))
213 {
214 int rc = PGM_HCPHYS_2_PTR(pVM, Pde.u & SHW_PDE_PG_MASK, &pPT);
215 if (RT_FAILURE(rc))
216 return rc;
217 }
218 else /* mapping: */
219 {
220# if PGM_SHW_TYPE == PGM_TYPE_AMD64 \
221 || PGM_SHW_TYPE == PGM_TYPE_EPT
222 AssertFailed(); /* can't happen */
223# else
224 Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
225
226 PPGMMAPPING pMap = pgmGetMapping(pVM, (RTGCPTR)GCPtr);
227 AssertMsgReturn(pMap, ("GCPtr=%RGv\n", GCPtr), VERR_INTERNAL_ERROR);
228# if PGM_SHW_TYPE == PGM_TYPE_32BIT
229 pPT = pMap->aPTs[(GCPtr - pMap->GCPtr) >> X86_PD_SHIFT].CTX_SUFF(pPT);
230# else /* PAE */
231 pPT = pMap->aPTs[(GCPtr - pMap->GCPtr) >> X86_PD_SHIFT].CTX_SUFF(paPaePTs);
232# endif
233# endif
234 }
235 const unsigned iPt = (GCPtr >> SHW_PT_SHIFT) & SHW_PT_MASK;
236 SHWPTE Pte = pPT->a[iPt];
237 if (!Pte.n.u1Present)
238 return VERR_PAGE_NOT_PRESENT;
239
240 /*
241 * Store the results.
242 * RW and US flags depend on the entire page translation hierarchy - except for
243 * legacy PAE which has a simplified PDPE.
244 */
245 if (pfFlags)
246 {
247 *pfFlags = (Pte.u & ~SHW_PTE_PG_MASK)
248 & ((Pde.u & (X86_PTE_RW | X86_PTE_US)) | ~(uint64_t)(X86_PTE_RW | X86_PTE_US));
249# if PGM_WITH_NX(PGM_SHW_TYPE, PGM_SHW_TYPE)
250 /* The NX bit is determined by a bitwise OR between the PT and PD */
251 if (fNoExecuteBitValid)
252 *pfFlags |= (Pte.u & Pde.u & X86_PTE_PAE_NX);
253# endif
254 }
255
256 if (pHCPhys)
257 *pHCPhys = Pte.u & SHW_PTE_PG_MASK;
258
259 return VINF_SUCCESS;
260#endif /* PGM_SHW_TYPE != PGM_TYPE_NESTED */
261}
262
263
264/**
265 * Modify page flags for a range of pages in the shadow context.
266 *
267 * The existing flags are ANDed with the fMask and ORed with the fFlags.
268 *
269 * @returns VBox status code.
270 * @param pVM VM handle.
271 * @param GCPtr Virtual address of the first page in the range. Page aligned!
272 * @param cb Size (in bytes) of the range to apply the modification to. Page aligned!
273 * @param fFlags The OR mask - page flags X86_PTE_*, excluding the page mask of course.
274 * @param fMask The AND mask - page flags X86_PTE_*.
275 * Be extremely CAREFUL with ~'ing values because they can be 32-bit!
276 * @remark You must use PGMMapModifyPage() for pages in a mapping.
277 */
278PGM_SHW_DECL(int, ModifyPage)(PVM pVM, RTGCUINTPTR GCPtr, size_t cb, uint64_t fFlags, uint64_t fMask)
279{
280# if PGM_SHW_TYPE == PGM_TYPE_NESTED
281 return VERR_PAGE_TABLE_NOT_PRESENT;
282
283# else /* PGM_SHW_TYPE != PGM_TYPE_NESTED && PGM_SHW_TYPE != PGM_TYPE_EPT */
284 int rc;
285
286 /*
287 * Walk page tables and pages till we're done.
288 */
289 for (;;)
290 {
291 /*
292 * Get the PDE.
293 */
294# if PGM_SHW_TYPE == PGM_TYPE_AMD64
295 X86PDEPAE Pde;
296 /* PML4 */
297 X86PML4E Pml4e = pgmShwGetLongModePML4E(&pVM->pgm.s, GCPtr);
298 if (!Pml4e.n.u1Present)
299 return VERR_PAGE_TABLE_NOT_PRESENT;
300
301 /* PDPT */
302 PX86PDPT pPDPT;
303 rc = PGM_HCPHYS_2_PTR(pVM, Pml4e.u & X86_PML4E_PG_MASK, &pPDPT);
304 if (RT_FAILURE(rc))
305 return rc;
306 const unsigned iPDPT = (GCPtr >> SHW_PDPT_SHIFT) & SHW_PDPT_MASK;
307 X86PDPE Pdpe = pPDPT->a[iPDPT];
308 if (!Pdpe.n.u1Present)
309 return VERR_PAGE_TABLE_NOT_PRESENT;
310
311 /* PD */
312 PX86PDPAE pPd;
313 rc = PGM_HCPHYS_2_PTR(pVM, Pdpe.u & X86_PDPE_PG_MASK, &pPd);
314 if (RT_FAILURE(rc))
315 return rc;
316 const unsigned iPd = (GCPtr >> SHW_PD_SHIFT) & SHW_PD_MASK;
317 Pde = pPd->a[iPd];
318
319# elif PGM_SHW_TYPE == PGM_TYPE_PAE
320 X86PDEPAE Pde = pgmShwGetPaePDE(&pVM->pgm.s, GCPtr);
321
322# elif PGM_SHW_TYPE == PGM_TYPE_EPT
323 const unsigned iPd = ((GCPtr >> SHW_PD_SHIFT) & SHW_PD_MASK);
324 PEPTPD pPDDst;
325 EPTPDE Pde;
326
327 rc = pgmShwGetEPTPDPtr(pVM, GCPtr, NULL, &pPDDst);
328 if (rc != VINF_SUCCESS)
329 {
330 AssertRC(rc);
331 return rc;
332 }
333 Assert(pPDDst);
334 Pde = pPDDst->a[iPd];
335
336# else /* PGM_TYPE_32BIT */
337 X86PDE Pde = pgmShwGet32BitPDE(&pVM->pgm.s, GCPtr);
338# endif
339 if (!Pde.n.u1Present)
340 return VERR_PAGE_TABLE_NOT_PRESENT;
341
342 /*
343 * Map the page table.
344 */
345 PSHWPT pPT;
346 rc = PGM_HCPHYS_2_PTR(pVM, Pde.u & SHW_PDE_PG_MASK, &pPT);
347 if (RT_FAILURE(rc))
348 return rc;
349
350 unsigned iPTE = (GCPtr >> SHW_PT_SHIFT) & SHW_PT_MASK;
351 while (iPTE < RT_ELEMENTS(pPT->a))
352 {
353 if (pPT->a[iPTE].n.u1Present)
354 {
355 pPT->a[iPTE].u = (pPT->a[iPTE].u & (fMask | SHW_PTE_PG_MASK)) | (fFlags & ~SHW_PTE_PG_MASK);
356 Assert(pPT->a[iPTE].n.u1Present);
357# if PGM_SHW_TYPE == PGM_TYPE_EPT
358 HWACCMInvalidatePhysPage(pVM, (RTGCPHYS)GCPtr);
359# else
360 PGM_INVL_PG(GCPtr);
361# endif
362 }
363
364 /* next page */
365 cb -= PAGE_SIZE;
366 if (!cb)
367 return VINF_SUCCESS;
368 GCPtr += PAGE_SIZE;
369 iPTE++;
370 }
371 }
372# endif /* PGM_SHW_TYPE != PGM_TYPE_NESTED */
373}
374
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