VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/PGMAllGstSlatEpt.cpp.h@ 95173

Last change on this file since 95173 was 95173, checked in by vboxsync, 3 years ago

VMM: Nested VMX: bugref:10092 Guest SLAT walk nits.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.5 KB
Line 
1/* $Id: PGMAllGstSlatEpt.cpp.h 95173 2022-06-02 14:45:19Z vboxsync $ */
2/** @file
3 * VBox - Page Manager, Guest EPT SLAT - All context code.
4 */
5
6/*
7 * Copyright (C) 2021-2022 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#if PGM_GST_TYPE == PGM_TYPE_EPT
19DECLINLINE(bool) PGM_GST_SLAT_NAME_EPT(WalkIsPermValid)(PCVMCPUCC pVCpu, uint64_t uEntry)
20{
21 if (!(uEntry & EPT_E_READ))
22 {
23 Assert(!pVCpu->CTX_SUFF(pVM)->cpum.ro.GuestFeatures.fVmxModeBasedExecuteEpt);
24 Assert(!RT_BF_GET(pVCpu->pgm.s.uEptVpidCapMsr, VMX_BF_EPT_VPID_CAP_EXEC_ONLY));
25 NOREF(pVCpu);
26 if (uEntry & (EPT_E_WRITE | EPT_E_EXECUTE))
27 return false;
28 }
29 return true;
30}
31
32
33DECLINLINE(bool) PGM_GST_SLAT_NAME_EPT(WalkIsMemTypeValid)(uint64_t uEntry, uint8_t uLevel)
34{
35 Assert(uLevel <= 3 && uLevel >= 1); NOREF(uLevel);
36 uint8_t const fEptMemTypeMask = uEntry & VMX_BF_EPT_PT_MEMTYPE_MASK;
37 switch (fEptMemTypeMask)
38 {
39 case EPT_E_MEMTYPE_WB:
40 case EPT_E_MEMTYPE_UC:
41 case EPT_E_MEMTYPE_WP:
42 case EPT_E_MEMTYPE_WT:
43 case EPT_E_MEMTYPE_WC:
44 return true;
45 }
46 return false;
47}
48
49
50DECLINLINE(int) PGM_GST_SLAT_NAME_EPT(WalkReturnNotPresent)(PCVMCPUCC pVCpu, PPGMPTWALK pWalk, uint64_t uEntry, uint8_t uLevel)
51{
52 static PGMWALKFAIL const s_afEptViolations[] = { PGM_WALKFAIL_EPT_VIOLATION, PGM_WALKFAIL_EPT_VIOLATION_CONVERTIBLE };
53 uint8_t const fEptVeSupported = pVCpu->CTX_SUFF(pVM)->cpum.ro.GuestFeatures.fVmxEptXcptVe;
54 uint8_t const fConvertible = RT_BOOL(uLevel == 1 || (uEntry & EPT_E_BIT_LEAF));
55 uint8_t const idxViolationType = fEptVeSupported & fConvertible & !RT_BF_GET(uEntry, VMX_BF_EPT_PT_SUPPRESS_VE);
56
57 pWalk->fNotPresent = true;
58 pWalk->uLevel = uLevel;
59 pWalk->fFailed = s_afEptViolations[idxViolationType];
60 return VERR_PAGE_TABLE_NOT_PRESENT;
61}
62
63
64DECLINLINE(int) PGM_GST_SLAT_NAME_EPT(WalkReturnBadPhysAddr)(PCVMCPUCC pVCpu, PPGMPTWALK pWalk, uint8_t uLevel, int rc)
65{
66 AssertMsg(rc == VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS, ("%Rrc\n", rc)); NOREF(rc); NOREF(pVCpu);
67 pWalk->fBadPhysAddr = true;
68 pWalk->uLevel = uLevel;
69 pWalk->fFailed = PGM_WALKFAIL_EPT_VIOLATION;
70 return VERR_PAGE_TABLE_NOT_PRESENT;
71}
72
73
74DECLINLINE(int) PGM_GST_SLAT_NAME_EPT(WalkReturnRsvdError)(PVMCPUCC pVCpu, PPGMPTWALK pWalk, uint8_t uLevel)
75{
76 NOREF(pVCpu);
77 pWalk->fRsvdError = true;
78 pWalk->uLevel = uLevel;
79 pWalk->fFailed = PGM_WALKFAIL_EPT_MISCONFIG;
80 return VERR_PAGE_TABLE_NOT_PRESENT;
81}
82
83
84/**
85 * Performs an EPT walk (second-level address translation).
86 *
87 * @returns VBox status code.
88 * @retval VINF_SUCCESS on success.
89 * @retval VERR_PAGE_TABLE_NOT_PRESENT on failure. Check pWalk for details.
90 *
91 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
92 * @param GCPhysNested The nested-guest physical address to walk.
93 * @param fIsLinearAddrValid Whether the linear-address in @c GCPtrNested caused
94 * this page walk.
95 * @param GCPtrNested The nested-guest linear address that caused this
96 * page walk. If @c fIsLinearAddrValid is false, pass
97 * 0.
98 * @param pWalk The page walk info.
99 * @param pGstWalk The guest mode specific page walk info.
100 */
101DECLINLINE(int) PGM_GST_SLAT_NAME_EPT(Walk)(PVMCPUCC pVCpu, RTGCPHYS GCPhysNested, bool fIsLinearAddrValid, RTGCPTR GCPtrNested,
102 PPGMPTWALK pWalk, PGSTPTWALK pGstWalk)
103{
104 Assert(fIsLinearAddrValid || GCPtrNested == 0);
105
106 /*
107 * Init walk structures.
108 */
109 RT_ZERO(*pWalk);
110 RT_ZERO(*pGstWalk);
111
112 pWalk->GCPtr = GCPtrNested;
113 pWalk->GCPhysNested = GCPhysNested;
114 pWalk->fIsLinearAddrValid = fIsLinearAddrValid;
115 pWalk->fIsSlat = true;
116
117 /*
118 * Figure out EPT attributes that are cumulative (logical-AND) across page walks.
119 * - R, W, X_SUPER are unconditionally cumulative.
120 * See Intel spec. Table 26-7 "Exit Qualification for EPT Violations".
121 *
122 * - X_USER is cumulative but relevant only when mode-based execute control for EPT
123 * which we currently don't support it (asserted below).
124 *
125 * - MEMTYPE is not cumulative and only applicable to the final paging entry.
126 *
127 * - A, D EPT bits map to the regular page-table bit positions. Thus, they're not
128 * included in the mask below and handled separately. Accessed bits are
129 * cumulative but dirty bits are not cumulative as they're only applicable to
130 * the final paging entry.
131 */
132 Assert(!pVCpu->CTX_SUFF(pVM)->cpum.ro.GuestFeatures.fVmxModeBasedExecuteEpt);
133 uint64_t const fCumulativeEpt = ( PGM_PTATTRS_EPT_R_MASK
134 | PGM_PTATTRS_EPT_W_MASK
135 | PGM_PTATTRS_EPT_X_SUPER_MASK) & PGM_PTATTRS_EPT_MASK;
136
137 /*
138 * Do the walk.
139 */
140 uint64_t fEffective;
141 {
142 /*
143 * EPTP.
144 *
145 * We currently only support 4-level EPT paging.
146 * EPT 5-level paging was documented at some point (bit 7 of MSR_IA32_VMX_EPT_VPID_CAP)
147 * but for some reason seems to have been removed from subsequent specs.
148 */
149 int const rc = pgmGstGetEptPML4PtrEx(pVCpu, &pGstWalk->pPml4);
150 if (RT_SUCCESS(rc))
151 { /* likely */ }
152 else
153 return PGM_GST_SLAT_NAME_EPT(WalkReturnBadPhysAddr)(pVCpu, pWalk, 4, rc);
154 }
155 {
156 /*
157 * PML4E.
158 */
159 PEPTPML4E pPml4e;
160 pGstWalk->pPml4e = pPml4e = &pGstWalk->pPml4->a[(GCPhysNested >> EPT_PML4_SHIFT) & EPT_PML4_MASK];
161 EPTPML4E Pml4e;
162 pGstWalk->Pml4e.u = Pml4e.u = pPml4e->u;
163
164 if (GST_IS_PGENTRY_PRESENT(pVCpu, Pml4e)) { /* probable */ }
165 else return PGM_GST_SLAT_NAME_EPT(WalkReturnNotPresent)(pVCpu, pWalk, Pml4e.u, 4);
166
167 if (RT_LIKELY( GST_IS_PML4E_VALID(pVCpu, Pml4e)
168 && PGM_GST_SLAT_NAME_EPT(WalkIsPermValid)(pVCpu, Pml4e.u)))
169 { /* likely */ }
170 else return PGM_GST_SLAT_NAME_EPT(WalkReturnRsvdError)(pVCpu, pWalk, 4);
171
172 uint64_t const fEptAttrs = Pml4e.u & EPT_PML4E_ATTR_MASK;
173 uint8_t const fRead = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_READ);
174 uint8_t const fWrite = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_WRITE);
175 uint8_t const fExecute = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_EXECUTE);
176 uint8_t const fAccessed = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_ACCESSED);
177 uint64_t const fEffectiveEpt = (fEptAttrs << PGM_PTATTRS_EPT_SHIFT) & fCumulativeEpt;
178 fEffective = RT_BF_MAKE(PGM_PTATTRS_R, fRead)
179 | RT_BF_MAKE(PGM_PTATTRS_W, fWrite)
180 | RT_BF_MAKE(PGM_PTATTRS_NX, !fExecute)
181 | RT_BF_MAKE(PGM_PTATTRS_A, fAccessed)
182 | fEffectiveEpt;
183 pWalk->fEffective = fEffective;
184
185 int const rc = PGM_GCPHYS_2_PTR_BY_VMCPU(pVCpu, Pml4e.u & EPT_PML4E_PG_MASK, &pGstWalk->pPdpt);
186 if (RT_SUCCESS(rc)) { /* probable */ }
187 else return PGM_GST_SLAT_NAME_EPT(WalkReturnBadPhysAddr)(pVCpu, pWalk, 3, rc);
188 }
189 {
190 /*
191 * PDPTE.
192 */
193 PEPTPDPTE pPdpte;
194 pGstWalk->pPdpte = pPdpte = &pGstWalk->pPdpt->a[(GCPhysNested >> GST_PDPT_SHIFT) & GST_PDPT_MASK];
195 EPTPDPTE Pdpte;
196 pGstWalk->Pdpte.u = Pdpte.u = pPdpte->u;
197
198 if (GST_IS_PGENTRY_PRESENT(pVCpu, Pdpte)) { /* probable */ }
199 else return PGM_GST_SLAT_NAME_EPT(WalkReturnNotPresent)(pVCpu, pWalk, Pdpte.u, 3);
200
201 /* The order of the following "if" and "else if" statements matter. */
202 if ( GST_IS_PDPE_VALID(pVCpu, Pdpte)
203 && PGM_GST_SLAT_NAME_EPT(WalkIsPermValid)(pVCpu, Pdpte.u))
204 {
205 uint64_t const fEptAttrs = Pdpte.u & EPT_PDPTE_ATTR_MASK;
206 uint8_t const fRead = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_READ);
207 uint8_t const fWrite = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_WRITE);
208 uint8_t const fExecute = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_EXECUTE);
209 uint8_t const fAccessed = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_ACCESSED);
210 uint64_t const fEffectiveEpt = (fEptAttrs << PGM_PTATTRS_EPT_SHIFT) & fCumulativeEpt;
211 fEffective &= RT_BF_MAKE(PGM_PTATTRS_R, fRead)
212 | RT_BF_MAKE(PGM_PTATTRS_W, fWrite)
213 | RT_BF_MAKE(PGM_PTATTRS_NX, !fExecute)
214 | RT_BF_MAKE(PGM_PTATTRS_A, fAccessed)
215 | fEffectiveEpt;
216 pWalk->fEffective = fEffective;
217 }
218 else if ( GST_IS_BIG_PDPE_VALID(pVCpu, Pdpte)
219 && PGM_GST_SLAT_NAME_EPT(WalkIsPermValid)(pVCpu, Pdpte.u)
220 && PGM_GST_SLAT_NAME_EPT(WalkIsMemTypeValid)(Pdpte.u, 3))
221 {
222 uint64_t const fEptAttrs = Pdpte.u & EPT_PDPTE1G_ATTR_MASK;
223 uint8_t const fRead = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_READ);
224 uint8_t const fWrite = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_WRITE);
225 uint8_t const fExecute = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_EXECUTE);
226 uint8_t const fAccessed = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_ACCESSED);
227 uint8_t const fDirty = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_DIRTY);
228 uint8_t const fMemType = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_MEMTYPE);
229 uint64_t const fEffectiveEpt = (fEptAttrs << PGM_PTATTRS_EPT_SHIFT) & fCumulativeEpt;
230 fEffective &= RT_BF_MAKE(PGM_PTATTRS_R, fRead)
231 | RT_BF_MAKE(PGM_PTATTRS_W, fWrite)
232 | RT_BF_MAKE(PGM_PTATTRS_NX, !fExecute)
233 | RT_BF_MAKE(PGM_PTATTRS_A, fAccessed)
234 | fEffectiveEpt;
235 fEffective |= RT_BF_MAKE(PGM_PTATTRS_D, fDirty)
236 | RT_BF_MAKE(PGM_PTATTRS_EPT_MEMTYPE, fMemType);
237 pWalk->fEffective = fEffective;
238
239 pWalk->fGigantPage = true;
240 pWalk->fSucceeded = true;
241 pWalk->GCPhys = GST_GET_BIG_PDPE_GCPHYS(pVCpu->CTX_SUFF(pVM), Pdpte)
242 | (GCPhysNested & GST_GIGANT_PAGE_OFFSET_MASK);
243 PGM_A20_APPLY_TO_VAR(pVCpu, pWalk->GCPhys);
244 return VINF_SUCCESS;
245 }
246 else return PGM_GST_SLAT_NAME_EPT(WalkReturnRsvdError)(pVCpu, pWalk, 3);
247
248 int const rc = PGM_GCPHYS_2_PTR_BY_VMCPU(pVCpu, Pdpte.u & EPT_PDPTE_PG_MASK, &pGstWalk->pPd);
249 if (RT_SUCCESS(rc)) { /* probable */ }
250 else return PGM_GST_SLAT_NAME_EPT(WalkReturnBadPhysAddr)(pVCpu, pWalk, 3, rc);
251 }
252 {
253 /*
254 * PDE.
255 */
256 PGSTPDE pPde;
257 pGstWalk->pPde = pPde = &pGstWalk->pPd->a[(GCPhysNested >> GST_PD_SHIFT) & GST_PD_MASK];
258 GSTPDE Pde;
259 pGstWalk->Pde.u = Pde.u = pPde->u;
260
261 if (GST_IS_PGENTRY_PRESENT(pVCpu, Pde)) { /* probable */ }
262 else return PGM_GST_SLAT_NAME_EPT(WalkReturnNotPresent)(pVCpu, pWalk, Pde.u, 2);
263
264 /* The order of the following "if" and "else if" statements matter. */
265 if ( GST_IS_PDE_VALID(pVCpu, Pde)
266 && PGM_GST_SLAT_NAME_EPT(WalkIsPermValid)(pVCpu, Pde.u))
267 {
268 uint64_t const fEptAttrs = Pde.u & EPT_PDE_ATTR_MASK;
269 uint8_t const fRead = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_READ);
270 uint8_t const fWrite = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_WRITE);
271 uint8_t const fExecute = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_EXECUTE);
272 uint8_t const fAccessed = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_ACCESSED);
273 uint64_t const fEffectiveEpt = (fEptAttrs << PGM_PTATTRS_EPT_SHIFT) & fCumulativeEpt;
274 fEffective &= RT_BF_MAKE(PGM_PTATTRS_R, fRead)
275 | RT_BF_MAKE(PGM_PTATTRS_W, fWrite)
276 | RT_BF_MAKE(PGM_PTATTRS_NX, !fExecute)
277 | RT_BF_MAKE(PGM_PTATTRS_A, fAccessed)
278 | fEffectiveEpt;
279 pWalk->fEffective = fEffective;
280 }
281 else if ( GST_IS_BIG_PDE_VALID(pVCpu, Pde)
282 && PGM_GST_SLAT_NAME_EPT(WalkIsPermValid)(pVCpu, Pde.u)
283 && PGM_GST_SLAT_NAME_EPT(WalkIsMemTypeValid)(Pde.u, 2))
284 {
285 uint64_t const fEptAttrs = Pde.u & EPT_PDE2M_ATTR_MASK;
286 uint8_t const fRead = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_READ);
287 uint8_t const fWrite = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_WRITE);
288 uint8_t const fExecute = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_EXECUTE);
289 uint8_t const fAccessed = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_ACCESSED);
290 uint8_t const fDirty = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_DIRTY);
291 uint8_t const fMemType = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_MEMTYPE);
292 uint64_t const fEffectiveEpt = (fEptAttrs << PGM_PTATTRS_EPT_SHIFT) & fCumulativeEpt;
293 fEffective &= RT_BF_MAKE(PGM_PTATTRS_R, fRead)
294 | RT_BF_MAKE(PGM_PTATTRS_W, fWrite)
295 | RT_BF_MAKE(PGM_PTATTRS_NX, !fExecute)
296 | RT_BF_MAKE(PGM_PTATTRS_A, fAccessed)
297 | fEffectiveEpt;
298 fEffective |= RT_BF_MAKE(PGM_PTATTRS_D, fDirty)
299 | RT_BF_MAKE(PGM_PTATTRS_EPT_MEMTYPE, fMemType);
300 pWalk->fEffective = fEffective;
301
302 pWalk->fBigPage = true;
303 pWalk->fSucceeded = true;
304 pWalk->GCPhys = GST_GET_BIG_PDE_GCPHYS(pVCpu->CTX_SUFF(pVM), Pde)
305 | (GCPhysNested & GST_BIG_PAGE_OFFSET_MASK);
306 PGM_A20_APPLY_TO_VAR(pVCpu, pWalk->GCPhys);
307 return VINF_SUCCESS;
308 }
309 else return PGM_GST_SLAT_NAME_EPT(WalkReturnRsvdError)(pVCpu, pWalk, 2);
310
311 int const rc = PGM_GCPHYS_2_PTR_BY_VMCPU(pVCpu, GST_GET_PDE_GCPHYS(Pde), &pGstWalk->pPt);
312 if (RT_SUCCESS(rc)) { /* probable */ }
313 else return PGM_GST_SLAT_NAME_EPT(WalkReturnBadPhysAddr)(pVCpu, pWalk, 1, rc);
314 }
315 {
316 /*
317 * PTE.
318 */
319 PGSTPTE pPte;
320 pGstWalk->pPte = pPte = &pGstWalk->pPt->a[(GCPhysNested >> GST_PT_SHIFT) & GST_PT_MASK];
321 GSTPTE Pte;
322 pGstWalk->Pte.u = Pte.u = pPte->u;
323
324 if (GST_IS_PGENTRY_PRESENT(pVCpu, Pte)) { /* probable */ }
325 else return PGM_GST_SLAT_NAME_EPT(WalkReturnNotPresent)(pVCpu, pWalk, Pte.u, 1);
326
327 if ( GST_IS_PTE_VALID(pVCpu, Pte)
328 && PGM_GST_SLAT_NAME_EPT(WalkIsPermValid)(pVCpu, Pte.u)
329 && PGM_GST_SLAT_NAME_EPT(WalkIsMemTypeValid)(Pte.u, 1))
330 { /* likely*/ }
331 else
332 return PGM_GST_SLAT_NAME_EPT(WalkReturnRsvdError)(pVCpu, pWalk, 1);
333
334 uint64_t const fEptAttrs = Pte.u & EPT_PTE_ATTR_MASK;
335 uint8_t const fRead = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_READ);
336 uint8_t const fWrite = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_WRITE);
337 uint8_t const fExecute = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_EXECUTE);
338 uint8_t const fAccessed = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_ACCESSED);
339 uint8_t const fDirty = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_DIRTY);
340 uint8_t const fMemType = RT_BF_GET(fEptAttrs, VMX_BF_EPT_PT_MEMTYPE);
341 uint64_t const fEffectiveEpt = (fEptAttrs << PGM_PTATTRS_EPT_SHIFT) & fCumulativeEpt;
342 fEffective &= RT_BF_MAKE(PGM_PTATTRS_R, fRead)
343 | RT_BF_MAKE(PGM_PTATTRS_W, fWrite)
344 | RT_BF_MAKE(PGM_PTATTRS_NX, !fExecute)
345 | RT_BF_MAKE(PGM_PTATTRS_A, fAccessed)
346 | fEffectiveEpt;
347 fEffective |= RT_BF_MAKE(PGM_PTATTRS_D, fDirty)
348 | RT_BF_MAKE(PGM_PTATTRS_EPT_MEMTYPE, fMemType);
349 pWalk->fEffective = fEffective;
350
351 pWalk->fSucceeded = true;
352 pWalk->GCPhys = GST_GET_PTE_GCPHYS(Pte) | (GCPhysNested & GUEST_PAGE_OFFSET_MASK);
353 return VINF_SUCCESS;
354 }
355}
356#else
357# error "Guest paging type must be EPT."
358#endif
359
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