VirtualBox

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

Last change on this file since 18976 was 18927, checked in by vboxsync, 16 years ago

Big step to separate VMM data structures for guest SMP. (pgm, em)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 12.8 KB
Line 
1/* $Id: PGMAllShw.h 18927 2009-04-16 11:41:38Z 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, PVMCPU pVCpu, RTGCUINTPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys);
124PGM_SHW_DECL(int, ModifyPage)(PVM pVM, PVMCPU pVCpu, 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 pVCpu The VMCPU handle.
135 * @param GCPtr Guest Context virtual address of the page.
136 * @param pfFlags Where to store the flags. These are X86_PTE_*.
137 * @param pHCPhys Where to store the HC physical address of the page.
138 * This is page aligned.
139 * @remark You should use PGMMapGetPage() for pages in a mapping.
140 */
141PGM_SHW_DECL(int, GetPage)(PVM pVM, PVMCPU pVCpu, RTGCUINTPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys)
142{
143#if PGM_SHW_TYPE == PGM_TYPE_NESTED
144 return VERR_PAGE_TABLE_NOT_PRESENT;
145
146#else /* PGM_SHW_TYPE != PGM_TYPE_NESTED && PGM_SHW_TYPE != PGM_TYPE_EPT */
147 /*
148 * Get the PDE.
149 */
150# if PGM_SHW_TYPE == PGM_TYPE_AMD64
151 bool fNoExecuteBitValid = !!(CPUMGetGuestEFER(pVCpu) & MSR_K6_EFER_NXE);
152 X86PDEPAE Pde;
153
154 /* PML4 */
155 X86PML4E Pml4e = pgmShwGetLongModePML4E(&pVCpu->pgm.s, GCPtr);
156 if (!Pml4e.n.u1Present)
157 return VERR_PAGE_TABLE_NOT_PRESENT;
158
159 /* PDPT */
160 PX86PDPT pPDPT;
161 int rc = PGM_HCPHYS_2_PTR(pVM, Pml4e.u & X86_PML4E_PG_MASK, &pPDPT);
162 if (RT_FAILURE(rc))
163 return rc;
164 const unsigned iPDPT = (GCPtr >> SHW_PDPT_SHIFT) & SHW_PDPT_MASK;
165 X86PDPE Pdpe = pPDPT->a[iPDPT];
166 if (!Pdpe.n.u1Present)
167 return VERR_PAGE_TABLE_NOT_PRESENT;
168
169 /* PD */
170 PX86PDPAE pPd;
171 rc = PGM_HCPHYS_2_PTR(pVM, Pdpe.u & X86_PDPE_PG_MASK, &pPd);
172 if (RT_FAILURE(rc))
173 return rc;
174 const unsigned iPd = (GCPtr >> SHW_PD_SHIFT) & SHW_PD_MASK;
175 Pde = pPd->a[iPd];
176
177 /* Merge accessed, write, user and no-execute bits into the PDE. */
178 Pde.n.u1Accessed &= Pml4e.n.u1Accessed & Pdpe.lm.u1Accessed;
179 Pde.n.u1Write &= Pml4e.n.u1Write & Pdpe.lm.u1Write;
180 Pde.n.u1User &= Pml4e.n.u1User & Pdpe.lm.u1User;
181 Pde.n.u1NoExecute &= Pml4e.n.u1NoExecute & Pdpe.lm.u1NoExecute;
182
183# elif PGM_SHW_TYPE == PGM_TYPE_PAE
184 bool fNoExecuteBitValid = !!(CPUMGetGuestEFER(pVCpu) & MSR_K6_EFER_NXE);
185 X86PDEPAE Pde = pgmShwGetPaePDE(&pVCpu->pgm.s, GCPtr);
186
187# elif PGM_SHW_TYPE == PGM_TYPE_EPT
188 const unsigned iPd = ((GCPtr >> SHW_PD_SHIFT) & SHW_PD_MASK);
189 PEPTPD pPDDst;
190 EPTPDE Pde;
191
192 int rc = pgmShwGetEPTPDPtr(pVM, pVCpu, GCPtr, NULL, &pPDDst);
193 if (rc != VINF_SUCCESS) /** @todo this function isn't expected to return informational status codes. Check callers / fix. */
194 {
195 AssertRC(rc);
196 return rc;
197 }
198 Assert(pPDDst);
199 Pde = pPDDst->a[iPd];
200
201# else /* PGM_TYPE_32BIT */
202 X86PDE Pde = pgmShwGet32BitPDE(&pVCpu->pgm.s, GCPtr);
203# endif
204 if (!Pde.n.u1Present)
205 return VERR_PAGE_TABLE_NOT_PRESENT;
206
207 Assert(!Pde.b.u1Size);
208
209 /*
210 * Get PT entry.
211 */
212 PSHWPT pPT;
213 if (!(Pde.u & PGM_PDFLAGS_MAPPING))
214 {
215 int rc = PGM_HCPHYS_2_PTR(pVM, Pde.u & SHW_PDE_PG_MASK, &pPT);
216 if (RT_FAILURE(rc))
217 return rc;
218 }
219 else /* mapping: */
220 {
221# if PGM_SHW_TYPE == PGM_TYPE_AMD64 \
222 || PGM_SHW_TYPE == PGM_TYPE_EPT
223 AssertFailed(); /* can't happen */
224# else
225 Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
226
227 PPGMMAPPING pMap = pgmGetMapping(pVM, (RTGCPTR)GCPtr);
228 AssertMsgReturn(pMap, ("GCPtr=%RGv\n", GCPtr), VERR_INTERNAL_ERROR);
229# if PGM_SHW_TYPE == PGM_TYPE_32BIT
230 pPT = pMap->aPTs[(GCPtr - pMap->GCPtr) >> X86_PD_SHIFT].CTX_SUFF(pPT);
231# else /* PAE */
232 pPT = pMap->aPTs[(GCPtr - pMap->GCPtr) >> X86_PD_SHIFT].CTX_SUFF(paPaePTs);
233# endif
234# endif
235 }
236 const unsigned iPt = (GCPtr >> SHW_PT_SHIFT) & SHW_PT_MASK;
237 SHWPTE Pte = pPT->a[iPt];
238 if (!Pte.n.u1Present)
239 return VERR_PAGE_NOT_PRESENT;
240
241 /*
242 * Store the results.
243 * RW and US flags depend on the entire page translation hierarchy - except for
244 * legacy PAE which has a simplified PDPE.
245 */
246 if (pfFlags)
247 {
248 *pfFlags = (Pte.u & ~SHW_PTE_PG_MASK)
249 & ((Pde.u & (X86_PTE_RW | X86_PTE_US)) | ~(uint64_t)(X86_PTE_RW | X86_PTE_US));
250# if PGM_WITH_NX(PGM_SHW_TYPE, PGM_SHW_TYPE)
251 /* The NX bit is determined by a bitwise OR between the PT and PD */
252 if (fNoExecuteBitValid)
253 *pfFlags |= (Pte.u & Pde.u & X86_PTE_PAE_NX);
254# endif
255 }
256
257 if (pHCPhys)
258 *pHCPhys = Pte.u & SHW_PTE_PG_MASK;
259
260 return VINF_SUCCESS;
261#endif /* PGM_SHW_TYPE != PGM_TYPE_NESTED */
262}
263
264
265/**
266 * Modify page flags for a range of pages in the shadow context.
267 *
268 * The existing flags are ANDed with the fMask and ORed with the fFlags.
269 *
270 * @returns VBox status code.
271 * @param pVM VM handle.
272 * @param pVCpu The VMCPU handle.
273 * @param GCPtr Virtual address of the first page in the range. Page aligned!
274 * @param cb Size (in bytes) of the range to apply the modification to. Page aligned!
275 * @param fFlags The OR mask - page flags X86_PTE_*, excluding the page mask of course.
276 * @param fMask The AND mask - page flags X86_PTE_*.
277 * Be extremely CAREFUL with ~'ing values because they can be 32-bit!
278 * @remark You must use PGMMapModifyPage() for pages in a mapping.
279 */
280PGM_SHW_DECL(int, ModifyPage)(PVM pVM, PVMCPU pVCpu, RTGCUINTPTR GCPtr, size_t cb, uint64_t fFlags, uint64_t fMask)
281{
282# if PGM_SHW_TYPE == PGM_TYPE_NESTED
283 return VERR_PAGE_TABLE_NOT_PRESENT;
284
285# else /* PGM_SHW_TYPE != PGM_TYPE_NESTED && PGM_SHW_TYPE != PGM_TYPE_EPT */
286 int rc;
287
288 /*
289 * Walk page tables and pages till we're done.
290 */
291 for (;;)
292 {
293 /*
294 * Get the PDE.
295 */
296# if PGM_SHW_TYPE == PGM_TYPE_AMD64
297 X86PDEPAE Pde;
298 /* PML4 */
299 X86PML4E Pml4e = pgmShwGetLongModePML4E(&pVCpu->pgm.s, GCPtr);
300 if (!Pml4e.n.u1Present)
301 return VERR_PAGE_TABLE_NOT_PRESENT;
302
303 /* PDPT */
304 PX86PDPT pPDPT;
305 rc = PGM_HCPHYS_2_PTR(pVM, Pml4e.u & X86_PML4E_PG_MASK, &pPDPT);
306 if (RT_FAILURE(rc))
307 return rc;
308 const unsigned iPDPT = (GCPtr >> SHW_PDPT_SHIFT) & SHW_PDPT_MASK;
309 X86PDPE Pdpe = pPDPT->a[iPDPT];
310 if (!Pdpe.n.u1Present)
311 return VERR_PAGE_TABLE_NOT_PRESENT;
312
313 /* PD */
314 PX86PDPAE pPd;
315 rc = PGM_HCPHYS_2_PTR(pVM, Pdpe.u & X86_PDPE_PG_MASK, &pPd);
316 if (RT_FAILURE(rc))
317 return rc;
318 const unsigned iPd = (GCPtr >> SHW_PD_SHIFT) & SHW_PD_MASK;
319 Pde = pPd->a[iPd];
320
321# elif PGM_SHW_TYPE == PGM_TYPE_PAE
322 X86PDEPAE Pde = pgmShwGetPaePDE(&pVCpu->pgm.s, GCPtr);
323
324# elif PGM_SHW_TYPE == PGM_TYPE_EPT
325 const unsigned iPd = ((GCPtr >> SHW_PD_SHIFT) & SHW_PD_MASK);
326 PEPTPD pPDDst;
327 EPTPDE Pde;
328
329 rc = pgmShwGetEPTPDPtr(pVM, pVCpu, GCPtr, NULL, &pPDDst);
330 if (rc != VINF_SUCCESS)
331 {
332 AssertRC(rc);
333 return rc;
334 }
335 Assert(pPDDst);
336 Pde = pPDDst->a[iPd];
337
338# else /* PGM_TYPE_32BIT */
339 X86PDE Pde = pgmShwGet32BitPDE(&pVCpu->pgm.s, GCPtr);
340# endif
341 if (!Pde.n.u1Present)
342 return VERR_PAGE_TABLE_NOT_PRESENT;
343
344 /*
345 * Map the page table.
346 */
347 PSHWPT pPT;
348 rc = PGM_HCPHYS_2_PTR(pVM, Pde.u & SHW_PDE_PG_MASK, &pPT);
349 if (RT_FAILURE(rc))
350 return rc;
351
352 unsigned iPTE = (GCPtr >> SHW_PT_SHIFT) & SHW_PT_MASK;
353 while (iPTE < RT_ELEMENTS(pPT->a))
354 {
355 if (pPT->a[iPTE].n.u1Present)
356 {
357 pPT->a[iPTE].u = (pPT->a[iPTE].u & (fMask | SHW_PTE_PG_MASK)) | (fFlags & ~SHW_PTE_PG_MASK);
358 Assert(pPT->a[iPTE].n.u1Present);
359# if PGM_SHW_TYPE == PGM_TYPE_EPT
360 HWACCMInvalidatePhysPage(pVM, (RTGCPHYS)GCPtr);
361# else
362 PGM_INVL_PG(GCPtr);
363# endif
364 }
365
366 /* next page */
367 cb -= PAGE_SIZE;
368 if (!cb)
369 return VINF_SUCCESS;
370 GCPtr += PAGE_SIZE;
371 iPTE++;
372 }
373 }
374# endif /* PGM_SHW_TYPE != PGM_TYPE_NESTED */
375}
376
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