VirtualBox

source: vbox/trunk/src/VBox/VMM/include/SELMInline.h@ 77807

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

*: scm --fix-header-guard-endif

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 10.7 KB
Line 
1/* $Id: SELMInline.h 76585 2019-01-01 06:31:29Z vboxsync $ */
2/** @file
3 * SELM - Internal header file.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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#ifndef VMM_INCLUDED_SRC_include_SELMInline_h
19#define VMM_INCLUDED_SRC_include_SELMInline_h
20#ifndef RT_WITHOUT_PRAGMA_ONCE
21# pragma once
22#endif
23
24#ifdef VBOX_WITH_RAW_MODE_NOT_R0
25
26/**
27 * Checks if a shadow descriptor table entry is good for the given segment
28 * register.
29 *
30 * @returns @c true if good, @c false if not.
31 * @param pSReg The segment register.
32 * @param pShwDesc The shadow descriptor table entry.
33 * @param iSReg The segment register index (X86_SREG_XXX).
34 * @param uCpl The CPL.
35 */
36DECLINLINE(bool) selmIsShwDescGoodForSReg(PCCPUMSELREG pSReg, PCX86DESC pShwDesc, uint32_t iSReg, uint32_t uCpl)
37{
38 /*
39 * See iemMiscValidateNewSS, iemCImpl_LoadSReg and intel+amd manuals.
40 */
41
42 if (!pShwDesc->Gen.u1Present)
43 {
44 Log(("selmIsShwDescGoodForSReg: Not present\n"));
45 return false;
46 }
47
48 if (!pShwDesc->Gen.u1DescType)
49 {
50 Log(("selmIsShwDescGoodForSReg: System descriptor\n"));
51 return false;
52 }
53
54 if (iSReg == X86_SREG_SS)
55 {
56 if ((pShwDesc->Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_WRITE)) != X86_SEL_TYPE_WRITE)
57 {
58 Log(("selmIsShwDescGoodForSReg: Stack must be writable\n"));
59 return false;
60 }
61 if (uCpl > (unsigned)pShwDesc->Gen.u2Dpl - pShwDesc->Gen.u1Available)
62 {
63 Log(("selmIsShwDescGoodForSReg: CPL(%d) > DPL(%d)\n", uCpl, pShwDesc->Gen.u2Dpl - pShwDesc->Gen.u1Available));
64 return false;
65 }
66 }
67 else
68 {
69 if (iSReg == X86_SREG_CS)
70 {
71 if (!(pShwDesc->Gen.u4Type & X86_SEL_TYPE_CODE))
72 {
73 Log(("selmIsShwDescGoodForSReg: CS needs code segment\n"));
74 return false;
75 }
76 }
77 else if ((pShwDesc->Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_READ)) == X86_SEL_TYPE_CODE)
78 {
79 Log(("selmIsShwDescGoodForSReg: iSReg=%u execute only\n", iSReg));
80 return false;
81 }
82
83 if ( (pShwDesc->Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
84 != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF)
85 && ( ( (pSReg->Sel & X86_SEL_RPL) > (unsigned)pShwDesc->Gen.u2Dpl - pShwDesc->Gen.u1Available
86 && (pSReg->Sel & X86_SEL_RPL) != pShwDesc->Gen.u1Available )
87 || uCpl > (unsigned)pShwDesc->Gen.u2Dpl - pShwDesc->Gen.u1Available ) )
88 {
89 Log(("selmIsShwDescGoodForSReg: iSReg=%u DPL=%u CPL=%u RPL=%u\n", iSReg,
90 pShwDesc->Gen.u2Dpl - pShwDesc->Gen.u1Available, uCpl, pSReg->Sel & X86_SEL_RPL));
91 return false;
92 }
93 }
94
95 return true;
96}
97
98
99/**
100 * Checks if a guest descriptor table entry is good for the given segment
101 * register.
102 *
103 * @returns @c true if good, @c false if not.
104 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
105 * @param pSReg The segment register.
106 * @param pGstDesc The guest descriptor table entry.
107 * @param iSReg The segment register index (X86_SREG_XXX).
108 * @param uCpl The CPL.
109 */
110DECLINLINE(bool) selmIsGstDescGoodForSReg(PVMCPU pVCpu, PCCPUMSELREG pSReg, PCX86DESC pGstDesc, uint32_t iSReg, uint32_t uCpl)
111{
112 /*
113 * See iemMiscValidateNewSS, iemCImpl_LoadSReg and intel+amd manuals.
114 */
115
116 if (!pGstDesc->Gen.u1Present)
117 {
118 Log(("selmIsGstDescGoodForSReg: Not present\n"));
119 return false;
120 }
121
122 if (!pGstDesc->Gen.u1DescType)
123 {
124 Log(("selmIsGstDescGoodForSReg: System descriptor\n"));
125 return false;
126 }
127
128 if (iSReg == X86_SREG_SS)
129 {
130 if ((pGstDesc->Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_WRITE)) != X86_SEL_TYPE_WRITE)
131 {
132 Log(("selmIsGstDescGoodForSReg: Stack must be writable\n"));
133 return false;
134 }
135 if (uCpl > pGstDesc->Gen.u2Dpl)
136 {
137 Log(("selmIsGstDescGoodForSReg: CPL(%d) > DPL(%d)\n", uCpl, pGstDesc->Gen.u2Dpl));
138 return false;
139 }
140 }
141 else
142 {
143 if (iSReg == X86_SREG_CS)
144 {
145 if (!(pGstDesc->Gen.u4Type & X86_SEL_TYPE_CODE))
146 {
147 Log(("selmIsGstDescGoodForSReg: CS needs code segment\n"));
148 return false;
149 }
150 }
151 else if ((pGstDesc->Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_READ)) == X86_SEL_TYPE_CODE)
152 {
153 Log(("selmIsGstDescGoodForSReg: iSReg=%u execute only\n", iSReg));
154 return false;
155 }
156
157 if ( (pGstDesc->Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
158 != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF)
159 && ( ( (pSReg->Sel & X86_SEL_RPL) > pGstDesc->Gen.u2Dpl
160 && ( (pSReg->Sel & X86_SEL_RPL) != 1
161 || !CPUMIsGuestInRawMode(pVCpu) ) )
162 || uCpl > (unsigned)pGstDesc->Gen.u2Dpl
163 )
164 )
165 {
166 Log(("selmIsGstDescGoodForSReg: iSReg=%u DPL=%u CPL=%u RPL=%u InRawMode=%u\n", iSReg,
167 pGstDesc->Gen.u2Dpl, uCpl, pSReg->Sel & X86_SEL_RPL, CPUMIsGuestInRawMode(pVCpu)));
168 return false;
169 }
170 }
171
172 return true;
173}
174
175
176/**
177 * Converts a guest GDT or LDT entry to a shadow table entry.
178 *
179 * @param pVM The cross context VM structure.
180 * @param pDesc Guest entry on input, shadow entry on return.
181 */
182DECL_FORCE_INLINE(void) selmGuestToShadowDesc(PVM pVM, PX86DESC pDesc)
183{
184 /*
185 * Code and data selectors are generally 1:1, with the
186 * 'little' adjustment we do for DPL 0 selectors.
187 */
188 if (pDesc->Gen.u1DescType)
189 {
190 /*
191 * Hack for A-bit against Trap E on read-only GDT.
192 */
193 /** @todo Fix this by loading ds and cs before turning off WP. */
194 pDesc->Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
195
196 /*
197 * All DPL 0 code and data segments are squeezed into DPL 1.
198 *
199 * We're skipping conforming segments here because those
200 * cannot give us any trouble.
201 */
202 if ( pDesc->Gen.u2Dpl == 0
203 && (pDesc->Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
204 != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF) )
205 {
206 pDesc->Gen.u2Dpl = 1;
207 pDesc->Gen.u1Available = 1;
208 }
209# ifdef VBOX_WITH_RAW_RING1
210 else if ( pDesc->Gen.u2Dpl == 1
211 && EMIsRawRing1Enabled(pVM)
212 && (pDesc->Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
213 != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF) )
214 {
215 pDesc->Gen.u2Dpl = 2;
216 pDesc->Gen.u1Available = 1;
217 }
218# endif
219 else
220 pDesc->Gen.u1Available = 0;
221 }
222 else
223 {
224 /*
225 * System type selectors are marked not present.
226 * Recompiler or special handling is required for these.
227 */
228 /** @todo what about interrupt gates and rawr0? */
229 pDesc->Gen.u1Present = 0;
230 }
231}
232
233
234/**
235 * Checks if a segment register is stale given the shadow descriptor table
236 * entry.
237 *
238 * @returns @c true if stale, @c false if not.
239 * @param pSReg The segment register.
240 * @param pShwDesc The shadow descriptor entry.
241 * @param iSReg The segment register number (X86_SREG_XXX).
242 */
243DECLINLINE(bool) selmIsSRegStale32(PCCPUMSELREG pSReg, PCX86DESC pShwDesc, uint32_t iSReg)
244{
245 if ( pSReg->Attr.n.u1Present != pShwDesc->Gen.u1Present
246 || pSReg->Attr.n.u4Type != pShwDesc->Gen.u4Type
247 || pSReg->Attr.n.u1DescType != pShwDesc->Gen.u1DescType
248 || pSReg->Attr.n.u1DefBig != pShwDesc->Gen.u1DefBig
249 || pSReg->Attr.n.u1Granularity != pShwDesc->Gen.u1Granularity
250 || pSReg->Attr.n.u2Dpl != pShwDesc->Gen.u2Dpl - pShwDesc->Gen.u1Available)
251 {
252 Log(("selmIsSRegStale32: Attributes changed (%#x -> %#x) for %u\n", pSReg->Attr.u, X86DESC_GET_HID_ATTR(pShwDesc), iSReg));
253 return true;
254 }
255
256 if (pSReg->u64Base != X86DESC_BASE(pShwDesc))
257 {
258 Log(("selmIsSRegStale32: base changed (%#llx -> %#x) for %u\n", pSReg->u64Base, X86DESC_BASE(pShwDesc), iSReg));
259 return true;
260 }
261
262 if (pSReg->u32Limit != X86DESC_LIMIT_G(pShwDesc))
263 {
264 Log(("selmIsSRegStale32: limit changed (%#x -> %#x) for %u\n", pSReg->u32Limit, X86DESC_LIMIT_G(pShwDesc), iSReg));
265 return true;
266 }
267
268 RT_NOREF_PV(iSReg);
269 return false;
270}
271
272
273/**
274 * Loads the hidden bits of a selector register from a shadow descriptor table
275 * entry.
276 *
277 * @param pSReg The segment register in question.
278 * @param pShwDesc The shadow descriptor table entry.
279 */
280DECLINLINE(void) selmLoadHiddenSRegFromShadowDesc(PCPUMSELREG pSReg, PCX86DESC pShwDesc)
281{
282 pSReg->Attr.u = X86DESC_GET_HID_ATTR(pShwDesc);
283 pSReg->Attr.n.u2Dpl -= pSReg->Attr.n.u1Available;
284 Assert(pSReg->Attr.n.u4Type & X86_SEL_TYPE_ACCESSED);
285 pSReg->u32Limit = X86DESC_LIMIT_G(pShwDesc);
286 pSReg->u64Base = X86DESC_BASE(pShwDesc);
287 pSReg->ValidSel = pSReg->Sel;
288/** @todo VBOX_WITH_RAW_RING1 */
289 if (pSReg->Attr.n.u1Available)
290 pSReg->ValidSel &= ~(RTSEL)1;
291 pSReg->fFlags = CPUMSELREG_FLAGS_VALID;
292}
293
294
295/**
296 * Loads the hidden bits of a selector register from a guest descriptor table
297 * entry.
298 *
299 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
300 * @param pSReg The segment register in question.
301 * @param pGstDesc The guest descriptor table entry.
302 */
303DECLINLINE(void) selmLoadHiddenSRegFromGuestDesc(PVMCPU pVCpu, PCPUMSELREG pSReg, PCX86DESC pGstDesc)
304{
305 pSReg->Attr.u = X86DESC_GET_HID_ATTR(pGstDesc);
306 pSReg->Attr.n.u4Type |= X86_SEL_TYPE_ACCESSED;
307 pSReg->u32Limit = X86DESC_LIMIT_G(pGstDesc);
308 pSReg->u64Base = X86DESC_BASE(pGstDesc);
309 pSReg->ValidSel = pSReg->Sel;
310/** @todo VBOX_WITH_RAW_RING1 */
311 if ((pSReg->ValidSel & 1) && CPUMIsGuestInRawMode(pVCpu))
312 pSReg->ValidSel &= ~(RTSEL)1;
313 pSReg->fFlags = CPUMSELREG_FLAGS_VALID;
314}
315
316#endif /* VBOX_WITH_RAW_MODE_NOT_R0 */
317
318/** @} */
319
320#endif /* !VMM_INCLUDED_SRC_include_SELMInline_h */
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