1 | /* $Id: SELMAll.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * SELM All contexts.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_SELM
|
---|
23 | #include <VBox/selm.h>
|
---|
24 | #include <VBox/stam.h>
|
---|
25 | #include <VBox/mm.h>
|
---|
26 | #include <VBox/pgm.h>
|
---|
27 | #include "SELMInternal.h"
|
---|
28 | #include <VBox/vm.h>
|
---|
29 | #include <VBox/x86.h>
|
---|
30 | #include <VBox/err.h>
|
---|
31 | #include <VBox/param.h>
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <VBox/log.h>
|
---|
34 | #include <VBox/vmm.h>
|
---|
35 |
|
---|
36 |
|
---|
37 |
|
---|
38 | #ifndef IN_RING0
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Converts a GC selector based address to a flat address.
|
---|
42 | *
|
---|
43 | * No limit checks are done. Use the SELMToFlat*() or SELMValidate*() functions
|
---|
44 | * for that.
|
---|
45 | *
|
---|
46 | * @returns Flat address.
|
---|
47 | * @param pVM VM Handle.
|
---|
48 | * @param Sel Selector part.
|
---|
49 | * @param Addr Address part.
|
---|
50 | * @remarks Don't use when in long mode.
|
---|
51 | */
|
---|
52 | VMMDECL(RTGCPTR) SELMToFlatBySel(PVM pVM, RTSEL Sel, RTGCPTR Addr)
|
---|
53 | {
|
---|
54 | Assert(pVM->cCpus == 1 && !CPUMIsGuestInLongMode(VMMGetCpu(pVM))); /* DON'T USE! */
|
---|
55 |
|
---|
56 | /** @todo check the limit. */
|
---|
57 | X86DESC Desc;
|
---|
58 | if (!(Sel & X86_SEL_LDT))
|
---|
59 | Desc = pVM->selm.s.CTX_SUFF(paGdt)[Sel >> X86_SEL_SHIFT];
|
---|
60 | else
|
---|
61 | {
|
---|
62 | /** @todo handle LDT pages not present! */
|
---|
63 | PX86DESC paLDT = (PX86DESC)((char *)pVM->selm.s.CTX_SUFF(pvLdt) + pVM->selm.s.offLdtHyper);
|
---|
64 | Desc = paLDT[Sel >> X86_SEL_SHIFT];
|
---|
65 | }
|
---|
66 |
|
---|
67 | return (RTGCPTR)(((RTGCUINTPTR)Addr + X86DESC_BASE(Desc)) & 0xffffffff);
|
---|
68 | }
|
---|
69 | #endif /* !IN_RING0 */
|
---|
70 |
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Converts a GC selector based address to a flat address.
|
---|
74 | *
|
---|
75 | * No limit checks are done. Use the SELMToFlat*() or SELMValidate*() functions
|
---|
76 | * for that.
|
---|
77 | *
|
---|
78 | * @returns Flat address.
|
---|
79 | * @param pVM VM Handle.
|
---|
80 | * @param SelReg Selector register
|
---|
81 | * @param pCtxCore CPU context
|
---|
82 | * @param Addr Address part.
|
---|
83 | */
|
---|
84 | VMMDECL(RTGCPTR) SELMToFlat(PVM pVM, DIS_SELREG SelReg, PCPUMCTXCORE pCtxCore, RTGCPTR Addr)
|
---|
85 | {
|
---|
86 | PCPUMSELREGHID pHiddenSel;
|
---|
87 | RTSEL Sel;
|
---|
88 | int rc;
|
---|
89 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
90 |
|
---|
91 | rc = DISFetchRegSegEx(pCtxCore, SelReg, &Sel, &pHiddenSel); AssertRC(rc);
|
---|
92 |
|
---|
93 | /*
|
---|
94 | * Deal with real & v86 mode first.
|
---|
95 | */
|
---|
96 | if ( pCtxCore->eflags.Bits.u1VM
|
---|
97 | || CPUMIsGuestInRealMode(pVCpu))
|
---|
98 | {
|
---|
99 | RTGCUINTPTR uFlat = (RTGCUINTPTR)Addr & 0xffff;
|
---|
100 | if (CPUMAreHiddenSelRegsValid(pVM))
|
---|
101 | uFlat += pHiddenSel->u64Base;
|
---|
102 | else
|
---|
103 | uFlat += ((RTGCUINTPTR)Sel << 4);
|
---|
104 | return (RTGCPTR)uFlat;
|
---|
105 | }
|
---|
106 |
|
---|
107 | #ifdef IN_RING0
|
---|
108 | Assert(CPUMAreHiddenSelRegsValid(pVM));
|
---|
109 | #else
|
---|
110 | /** @todo when we're in 16 bits mode, we should cut off the address as well.. */
|
---|
111 | if (!CPUMAreHiddenSelRegsValid(pVM))
|
---|
112 | return SELMToFlatBySel(pVM, Sel, Addr);
|
---|
113 | #endif
|
---|
114 |
|
---|
115 | /* 64 bits mode: CS, DS, ES and SS are treated as if each segment base is 0 (Intel® 64 and IA-32 Architectures Software Developer's Manual: 3.4.2.1). */
|
---|
116 | if ( pCtxCore->csHid.Attr.n.u1Long
|
---|
117 | && CPUMIsGuestInLongMode(pVCpu))
|
---|
118 | {
|
---|
119 | switch (SelReg)
|
---|
120 | {
|
---|
121 | case DIS_SELREG_FS:
|
---|
122 | case DIS_SELREG_GS:
|
---|
123 | return (RTGCPTR)(pHiddenSel->u64Base + Addr);
|
---|
124 |
|
---|
125 | default:
|
---|
126 | return Addr; /* base 0 */
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | /* AMD64 manual: compatibility mode ignores the high 32 bits when calculating an effective address. */
|
---|
131 | Assert(pHiddenSel->u64Base <= 0xffffffff);
|
---|
132 | return ((pHiddenSel->u64Base + (RTGCUINTPTR)Addr) & 0xffffffff);
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Converts a GC selector based address to a flat address.
|
---|
138 | *
|
---|
139 | * Some basic checking is done, but not all kinds yet.
|
---|
140 | *
|
---|
141 | * @returns VBox status
|
---|
142 | * @param pVM VM Handle.
|
---|
143 | * @param SelReg Selector register
|
---|
144 | * @param pCtxCore CPU context
|
---|
145 | * @param Addr Address part.
|
---|
146 | * @param fFlags SELMTOFLAT_FLAGS_*
|
---|
147 | * GDT entires are valid.
|
---|
148 | * @param ppvGC Where to store the GC flat address.
|
---|
149 | */
|
---|
150 | VMMDECL(int) SELMToFlatEx(PVM pVM, DIS_SELREG SelReg, PCCPUMCTXCORE pCtxCore, RTGCPTR Addr, unsigned fFlags, PRTGCPTR ppvGC)
|
---|
151 | {
|
---|
152 | /*
|
---|
153 | * Fetch the selector first.
|
---|
154 | */
|
---|
155 | PCPUMSELREGHID pHiddenSel;
|
---|
156 | RTSEL Sel;
|
---|
157 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
158 |
|
---|
159 | int rc = DISFetchRegSegEx(pCtxCore, SelReg, &Sel, &pHiddenSel);
|
---|
160 | AssertRC(rc);
|
---|
161 |
|
---|
162 | /*
|
---|
163 | * Deal with real & v86 mode first.
|
---|
164 | */
|
---|
165 | if ( pCtxCore->eflags.Bits.u1VM
|
---|
166 | || CPUMIsGuestInRealMode(pVCpu))
|
---|
167 | {
|
---|
168 | RTGCUINTPTR uFlat = (RTGCUINTPTR)Addr & 0xffff;
|
---|
169 | if (ppvGC)
|
---|
170 | {
|
---|
171 | if ( pHiddenSel
|
---|
172 | && CPUMAreHiddenSelRegsValid(pVM))
|
---|
173 | *ppvGC = (RTGCPTR)(pHiddenSel->u64Base + uFlat);
|
---|
174 | else
|
---|
175 | *ppvGC = (RTGCPTR)(((RTGCUINTPTR)Sel << 4) + uFlat);
|
---|
176 | }
|
---|
177 | return VINF_SUCCESS;
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | uint32_t u32Limit;
|
---|
182 | RTGCPTR pvFlat;
|
---|
183 | uint32_t u1Present, u1DescType, u1Granularity, u4Type;
|
---|
184 |
|
---|
185 | /** @todo when we're in 16 bits mode, we should cut off the address as well.. */
|
---|
186 | #ifndef IN_RC
|
---|
187 | if ( pHiddenSel
|
---|
188 | && CPUMAreHiddenSelRegsValid(pVM))
|
---|
189 | {
|
---|
190 | bool fCheckLimit = true;
|
---|
191 |
|
---|
192 | u1Present = pHiddenSel->Attr.n.u1Present;
|
---|
193 | u1Granularity = pHiddenSel->Attr.n.u1Granularity;
|
---|
194 | u1DescType = pHiddenSel->Attr.n.u1DescType;
|
---|
195 | u4Type = pHiddenSel->Attr.n.u4Type;
|
---|
196 | u32Limit = pHiddenSel->u32Limit;
|
---|
197 |
|
---|
198 | /* 64 bits mode: CS, DS, ES and SS are treated as if each segment base is 0 (Intel® 64 and IA-32 Architectures Software Developer's Manual: 3.4.2.1). */
|
---|
199 | if ( pCtxCore->csHid.Attr.n.u1Long
|
---|
200 | && CPUMIsGuestInLongMode(pVCpu))
|
---|
201 | {
|
---|
202 | fCheckLimit = false;
|
---|
203 | switch (SelReg)
|
---|
204 | {
|
---|
205 | case DIS_SELREG_FS:
|
---|
206 | case DIS_SELREG_GS:
|
---|
207 | pvFlat = (pHiddenSel->u64Base + Addr);
|
---|
208 | break;
|
---|
209 |
|
---|
210 | default:
|
---|
211 | pvFlat = Addr;
|
---|
212 | break;
|
---|
213 | }
|
---|
214 | }
|
---|
215 | else
|
---|
216 | {
|
---|
217 | /* AMD64 manual: compatibility mode ignores the high 32 bits when calculating an effective address. */
|
---|
218 | Assert(pHiddenSel->u64Base <= 0xffffffff);
|
---|
219 | pvFlat = (RTGCPTR)((pHiddenSel->u64Base + (RTGCUINTPTR)Addr) & 0xffffffff);
|
---|
220 | }
|
---|
221 |
|
---|
222 | /*
|
---|
223 | * Check if present.
|
---|
224 | */
|
---|
225 | if (u1Present)
|
---|
226 | {
|
---|
227 | /*
|
---|
228 | * Type check.
|
---|
229 | */
|
---|
230 | switch (u4Type)
|
---|
231 | {
|
---|
232 |
|
---|
233 | /** Read only selector type. */
|
---|
234 | case X86_SEL_TYPE_RO:
|
---|
235 | case X86_SEL_TYPE_RO_ACC:
|
---|
236 | case X86_SEL_TYPE_RW:
|
---|
237 | case X86_SEL_TYPE_RW_ACC:
|
---|
238 | case X86_SEL_TYPE_EO:
|
---|
239 | case X86_SEL_TYPE_EO_ACC:
|
---|
240 | case X86_SEL_TYPE_ER:
|
---|
241 | case X86_SEL_TYPE_ER_ACC:
|
---|
242 | if (!(fFlags & SELMTOFLAT_FLAGS_NO_PL))
|
---|
243 | {
|
---|
244 | /** @todo fix this mess */
|
---|
245 | }
|
---|
246 | /* check limit. */
|
---|
247 | if (fCheckLimit && (RTGCUINTPTR)Addr > u32Limit)
|
---|
248 | return VERR_OUT_OF_SELECTOR_BOUNDS;
|
---|
249 | /* ok */
|
---|
250 | if (ppvGC)
|
---|
251 | *ppvGC = pvFlat;
|
---|
252 | return VINF_SUCCESS;
|
---|
253 |
|
---|
254 | case X86_SEL_TYPE_EO_CONF:
|
---|
255 | case X86_SEL_TYPE_EO_CONF_ACC:
|
---|
256 | case X86_SEL_TYPE_ER_CONF:
|
---|
257 | case X86_SEL_TYPE_ER_CONF_ACC:
|
---|
258 | if (!(fFlags & SELMTOFLAT_FLAGS_NO_PL))
|
---|
259 | {
|
---|
260 | /** @todo fix this mess */
|
---|
261 | }
|
---|
262 | /* check limit. */
|
---|
263 | if (fCheckLimit && (RTGCUINTPTR)Addr > u32Limit)
|
---|
264 | return VERR_OUT_OF_SELECTOR_BOUNDS;
|
---|
265 | /* ok */
|
---|
266 | if (ppvGC)
|
---|
267 | *ppvGC = pvFlat;
|
---|
268 | return VINF_SUCCESS;
|
---|
269 |
|
---|
270 | case X86_SEL_TYPE_RO_DOWN:
|
---|
271 | case X86_SEL_TYPE_RO_DOWN_ACC:
|
---|
272 | case X86_SEL_TYPE_RW_DOWN:
|
---|
273 | case X86_SEL_TYPE_RW_DOWN_ACC:
|
---|
274 | if (!(fFlags & SELMTOFLAT_FLAGS_NO_PL))
|
---|
275 | {
|
---|
276 | /** @todo fix this mess */
|
---|
277 | }
|
---|
278 | /* check limit. */
|
---|
279 | if (fCheckLimit)
|
---|
280 | {
|
---|
281 | if (!u1Granularity && (RTGCUINTPTR)Addr > (RTGCUINTPTR)0xffff)
|
---|
282 | return VERR_OUT_OF_SELECTOR_BOUNDS;
|
---|
283 | if ((RTGCUINTPTR)Addr <= u32Limit)
|
---|
284 | return VERR_OUT_OF_SELECTOR_BOUNDS;
|
---|
285 | }
|
---|
286 | /* ok */
|
---|
287 | if (ppvGC)
|
---|
288 | *ppvGC = pvFlat;
|
---|
289 | return VINF_SUCCESS;
|
---|
290 |
|
---|
291 | default:
|
---|
292 | return VERR_INVALID_SELECTOR;
|
---|
293 |
|
---|
294 | }
|
---|
295 | }
|
---|
296 | }
|
---|
297 | # ifndef IN_RING0
|
---|
298 | else
|
---|
299 | # endif
|
---|
300 | #endif /* !IN_RC */
|
---|
301 | #ifndef IN_RING0
|
---|
302 | {
|
---|
303 | X86DESC Desc;
|
---|
304 |
|
---|
305 | if (!(Sel & X86_SEL_LDT))
|
---|
306 | {
|
---|
307 | if ( !(fFlags & SELMTOFLAT_FLAGS_HYPER)
|
---|
308 | && (unsigned)(Sel & X86_SEL_MASK) >= pVM->selm.s.GuestGdtr.cbGdt)
|
---|
309 | return VERR_INVALID_SELECTOR;
|
---|
310 | Desc = pVM->selm.s.CTX_SUFF(paGdt)[Sel >> X86_SEL_SHIFT];
|
---|
311 | }
|
---|
312 | else
|
---|
313 | {
|
---|
314 | if ((unsigned)(Sel & X86_SEL_MASK) >= pVM->selm.s.cbLdtLimit)
|
---|
315 | return VERR_INVALID_SELECTOR;
|
---|
316 |
|
---|
317 | /** @todo handle LDT page(s) not present! */
|
---|
318 | PX86DESC paLDT = (PX86DESC)((char *)pVM->selm.s.CTX_SUFF(pvLdt) + pVM->selm.s.offLdtHyper);
|
---|
319 | Desc = paLDT[Sel >> X86_SEL_SHIFT];
|
---|
320 | }
|
---|
321 |
|
---|
322 | /* calc limit. */
|
---|
323 | u32Limit = X86DESC_LIMIT(Desc);
|
---|
324 | if (Desc.Gen.u1Granularity)
|
---|
325 | u32Limit = (u32Limit << PAGE_SHIFT) | PAGE_OFFSET_MASK;
|
---|
326 |
|
---|
327 | /* calc address assuming straight stuff. */
|
---|
328 | pvFlat = (RTGCPTR)((RTGCUINTPTR)Addr + X86DESC_BASE(Desc));
|
---|
329 |
|
---|
330 | /* Cut the address to 32 bits. */
|
---|
331 | Assert(!CPUMIsGuestInLongMode(pVCpu));
|
---|
332 | pvFlat &= 0xffffffff;
|
---|
333 |
|
---|
334 | u1Present = Desc.Gen.u1Present;
|
---|
335 | u1Granularity = Desc.Gen.u1Granularity;
|
---|
336 | u1DescType = Desc.Gen.u1DescType;
|
---|
337 | u4Type = Desc.Gen.u4Type;
|
---|
338 |
|
---|
339 | /*
|
---|
340 | * Check if present.
|
---|
341 | */
|
---|
342 | if (u1Present)
|
---|
343 | {
|
---|
344 | /*
|
---|
345 | * Type check.
|
---|
346 | */
|
---|
347 | # define BOTH(a, b) ((a << 16) | b)
|
---|
348 | switch (BOTH(u1DescType, u4Type))
|
---|
349 | {
|
---|
350 |
|
---|
351 | /** Read only selector type. */
|
---|
352 | case BOTH(1,X86_SEL_TYPE_RO):
|
---|
353 | case BOTH(1,X86_SEL_TYPE_RO_ACC):
|
---|
354 | case BOTH(1,X86_SEL_TYPE_RW):
|
---|
355 | case BOTH(1,X86_SEL_TYPE_RW_ACC):
|
---|
356 | case BOTH(1,X86_SEL_TYPE_EO):
|
---|
357 | case BOTH(1,X86_SEL_TYPE_EO_ACC):
|
---|
358 | case BOTH(1,X86_SEL_TYPE_ER):
|
---|
359 | case BOTH(1,X86_SEL_TYPE_ER_ACC):
|
---|
360 | if (!(fFlags & SELMTOFLAT_FLAGS_NO_PL))
|
---|
361 | {
|
---|
362 | /** @todo fix this mess */
|
---|
363 | }
|
---|
364 | /* check limit. */
|
---|
365 | if ((RTGCUINTPTR)Addr > u32Limit)
|
---|
366 | return VERR_OUT_OF_SELECTOR_BOUNDS;
|
---|
367 | /* ok */
|
---|
368 | if (ppvGC)
|
---|
369 | *ppvGC = pvFlat;
|
---|
370 | return VINF_SUCCESS;
|
---|
371 |
|
---|
372 | case BOTH(1,X86_SEL_TYPE_EO_CONF):
|
---|
373 | case BOTH(1,X86_SEL_TYPE_EO_CONF_ACC):
|
---|
374 | case BOTH(1,X86_SEL_TYPE_ER_CONF):
|
---|
375 | case BOTH(1,X86_SEL_TYPE_ER_CONF_ACC):
|
---|
376 | if (!(fFlags & SELMTOFLAT_FLAGS_NO_PL))
|
---|
377 | {
|
---|
378 | /** @todo fix this mess */
|
---|
379 | }
|
---|
380 | /* check limit. */
|
---|
381 | if ((RTGCUINTPTR)Addr > u32Limit)
|
---|
382 | return VERR_OUT_OF_SELECTOR_BOUNDS;
|
---|
383 | /* ok */
|
---|
384 | if (ppvGC)
|
---|
385 | *ppvGC = pvFlat;
|
---|
386 | return VINF_SUCCESS;
|
---|
387 |
|
---|
388 | case BOTH(1,X86_SEL_TYPE_RO_DOWN):
|
---|
389 | case BOTH(1,X86_SEL_TYPE_RO_DOWN_ACC):
|
---|
390 | case BOTH(1,X86_SEL_TYPE_RW_DOWN):
|
---|
391 | case BOTH(1,X86_SEL_TYPE_RW_DOWN_ACC):
|
---|
392 | if (!(fFlags & SELMTOFLAT_FLAGS_NO_PL))
|
---|
393 | {
|
---|
394 | /** @todo fix this mess */
|
---|
395 | }
|
---|
396 | /* check limit. */
|
---|
397 | if (!u1Granularity && (RTGCUINTPTR)Addr > (RTGCUINTPTR)0xffff)
|
---|
398 | return VERR_OUT_OF_SELECTOR_BOUNDS;
|
---|
399 | if ((RTGCUINTPTR)Addr <= u32Limit)
|
---|
400 | return VERR_OUT_OF_SELECTOR_BOUNDS;
|
---|
401 |
|
---|
402 | /* ok */
|
---|
403 | if (ppvGC)
|
---|
404 | *ppvGC = pvFlat;
|
---|
405 | return VINF_SUCCESS;
|
---|
406 |
|
---|
407 | case BOTH(0,X86_SEL_TYPE_SYS_286_TSS_AVAIL):
|
---|
408 | case BOTH(0,X86_SEL_TYPE_SYS_LDT):
|
---|
409 | case BOTH(0,X86_SEL_TYPE_SYS_286_TSS_BUSY):
|
---|
410 | case BOTH(0,X86_SEL_TYPE_SYS_286_CALL_GATE):
|
---|
411 | case BOTH(0,X86_SEL_TYPE_SYS_TASK_GATE):
|
---|
412 | case BOTH(0,X86_SEL_TYPE_SYS_286_INT_GATE):
|
---|
413 | case BOTH(0,X86_SEL_TYPE_SYS_286_TRAP_GATE):
|
---|
414 | case BOTH(0,X86_SEL_TYPE_SYS_386_TSS_AVAIL):
|
---|
415 | case BOTH(0,X86_SEL_TYPE_SYS_386_TSS_BUSY):
|
---|
416 | case BOTH(0,X86_SEL_TYPE_SYS_386_CALL_GATE):
|
---|
417 | case BOTH(0,X86_SEL_TYPE_SYS_386_INT_GATE):
|
---|
418 | case BOTH(0,X86_SEL_TYPE_SYS_386_TRAP_GATE):
|
---|
419 | if (!(fFlags & SELMTOFLAT_FLAGS_NO_PL))
|
---|
420 | {
|
---|
421 | /** @todo fix this mess */
|
---|
422 | }
|
---|
423 | /* check limit. */
|
---|
424 | if ((RTGCUINTPTR)Addr > u32Limit)
|
---|
425 | return VERR_OUT_OF_SELECTOR_BOUNDS;
|
---|
426 | /* ok */
|
---|
427 | if (ppvGC)
|
---|
428 | *ppvGC = pvFlat;
|
---|
429 | return VINF_SUCCESS;
|
---|
430 |
|
---|
431 | default:
|
---|
432 | return VERR_INVALID_SELECTOR;
|
---|
433 |
|
---|
434 | }
|
---|
435 | # undef BOTH
|
---|
436 | }
|
---|
437 | }
|
---|
438 | #endif /* !IN_RING0 */
|
---|
439 | return VERR_SELECTOR_NOT_PRESENT;
|
---|
440 | }
|
---|
441 |
|
---|
442 |
|
---|
443 | #ifndef IN_RING0
|
---|
444 | /**
|
---|
445 | * Converts a GC selector based address to a flat address.
|
---|
446 | *
|
---|
447 | * Some basic checking is done, but not all kinds yet.
|
---|
448 | *
|
---|
449 | * @returns VBox status
|
---|
450 | * @param pVM VM Handle.
|
---|
451 | * @param eflags Current eflags
|
---|
452 | * @param Sel Selector part.
|
---|
453 | * @param Addr Address part.
|
---|
454 | * @param pHiddenSel Hidden selector register (can be NULL)
|
---|
455 | * @param fFlags SELMTOFLAT_FLAGS_*
|
---|
456 | * GDT entires are valid.
|
---|
457 | * @param ppvGC Where to store the GC flat address.
|
---|
458 | * @param pcb Where to store the bytes from *ppvGC which can be accessed according to
|
---|
459 | * the selector. NULL is allowed.
|
---|
460 | * @remarks Don't use when in long mode.
|
---|
461 | */
|
---|
462 | VMMDECL(int) SELMToFlatBySelEx(PVM pVM, X86EFLAGS eflags, RTSEL Sel, RTGCPTR Addr, CPUMSELREGHID *pHiddenSel, unsigned fFlags, PRTGCPTR ppvGC, uint32_t *pcb)
|
---|
463 | {
|
---|
464 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
465 |
|
---|
466 | Assert(!CPUMIsGuestInLongMode(pVCpu)); /* DON'T USE! */
|
---|
467 |
|
---|
468 | /*
|
---|
469 | * Deal with real & v86 mode first.
|
---|
470 | */
|
---|
471 | if ( eflags.Bits.u1VM
|
---|
472 | || CPUMIsGuestInRealMode(pVCpu))
|
---|
473 | {
|
---|
474 | RTGCUINTPTR uFlat = (RTGCUINTPTR)Addr & 0xffff;
|
---|
475 | if (ppvGC)
|
---|
476 | {
|
---|
477 | if ( pHiddenSel
|
---|
478 | && CPUMAreHiddenSelRegsValid(pVM))
|
---|
479 | *ppvGC = (RTGCPTR)(pHiddenSel->u64Base + uFlat);
|
---|
480 | else
|
---|
481 | *ppvGC = (RTGCPTR)(((RTGCUINTPTR)Sel << 4) + uFlat);
|
---|
482 | }
|
---|
483 | if (pcb)
|
---|
484 | *pcb = 0x10000 - uFlat;
|
---|
485 | return VINF_SUCCESS;
|
---|
486 | }
|
---|
487 |
|
---|
488 |
|
---|
489 | uint32_t u32Limit;
|
---|
490 | RTGCPTR pvFlat;
|
---|
491 | uint32_t u1Present, u1DescType, u1Granularity, u4Type;
|
---|
492 |
|
---|
493 | /** @todo when we're in 16 bits mode, we should cut off the address as well.. */
|
---|
494 | if ( pHiddenSel
|
---|
495 | && CPUMAreHiddenSelRegsValid(pVM))
|
---|
496 | {
|
---|
497 | u1Present = pHiddenSel->Attr.n.u1Present;
|
---|
498 | u1Granularity = pHiddenSel->Attr.n.u1Granularity;
|
---|
499 | u1DescType = pHiddenSel->Attr.n.u1DescType;
|
---|
500 | u4Type = pHiddenSel->Attr.n.u4Type;
|
---|
501 |
|
---|
502 | u32Limit = pHiddenSel->u32Limit;
|
---|
503 | pvFlat = (RTGCPTR)(pHiddenSel->u64Base + (RTGCUINTPTR)Addr);
|
---|
504 |
|
---|
505 | if ( !pHiddenSel->Attr.n.u1Long
|
---|
506 | || !CPUMIsGuestInLongMode(pVCpu))
|
---|
507 | {
|
---|
508 | /* AMD64 manual: compatibility mode ignores the high 32 bits when calculating an effective address. */
|
---|
509 | pvFlat &= 0xffffffff;
|
---|
510 | }
|
---|
511 | }
|
---|
512 | else
|
---|
513 | {
|
---|
514 | X86DESC Desc;
|
---|
515 |
|
---|
516 | if (!(Sel & X86_SEL_LDT))
|
---|
517 | {
|
---|
518 | if ( !(fFlags & SELMTOFLAT_FLAGS_HYPER)
|
---|
519 | && (unsigned)(Sel & X86_SEL_MASK) >= pVM->selm.s.GuestGdtr.cbGdt)
|
---|
520 | return VERR_INVALID_SELECTOR;
|
---|
521 | Desc = pVM->selm.s.CTX_SUFF(paGdt)[Sel >> X86_SEL_SHIFT];
|
---|
522 | }
|
---|
523 | else
|
---|
524 | {
|
---|
525 | if ((unsigned)(Sel & X86_SEL_MASK) >= pVM->selm.s.cbLdtLimit)
|
---|
526 | return VERR_INVALID_SELECTOR;
|
---|
527 |
|
---|
528 | /** @todo handle LDT page(s) not present! */
|
---|
529 | PX86DESC paLDT = (PX86DESC)((char *)pVM->selm.s.CTX_SUFF(pvLdt) + pVM->selm.s.offLdtHyper);
|
---|
530 | Desc = paLDT[Sel >> X86_SEL_SHIFT];
|
---|
531 | }
|
---|
532 |
|
---|
533 | /* calc limit. */
|
---|
534 | u32Limit = X86DESC_LIMIT(Desc);
|
---|
535 | if (Desc.Gen.u1Granularity)
|
---|
536 | u32Limit = (u32Limit << PAGE_SHIFT) | PAGE_OFFSET_MASK;
|
---|
537 |
|
---|
538 | /* calc address assuming straight stuff. */
|
---|
539 | pvFlat = (RTGCPTR)((RTGCUINTPTR)Addr + X86DESC_BASE(Desc));
|
---|
540 |
|
---|
541 | /* Cut the address to 32 bits. */
|
---|
542 | Assert(!CPUMIsGuestInLongMode(pVCpu));
|
---|
543 | pvFlat &= 0xffffffff;
|
---|
544 |
|
---|
545 | u1Present = Desc.Gen.u1Present;
|
---|
546 | u1Granularity = Desc.Gen.u1Granularity;
|
---|
547 | u1DescType = Desc.Gen.u1DescType;
|
---|
548 | u4Type = Desc.Gen.u4Type;
|
---|
549 | }
|
---|
550 |
|
---|
551 | /*
|
---|
552 | * Check if present.
|
---|
553 | */
|
---|
554 | if (u1Present)
|
---|
555 | {
|
---|
556 | /*
|
---|
557 | * Type check.
|
---|
558 | */
|
---|
559 | #define BOTH(a, b) ((a << 16) | b)
|
---|
560 | switch (BOTH(u1DescType, u4Type))
|
---|
561 | {
|
---|
562 |
|
---|
563 | /** Read only selector type. */
|
---|
564 | case BOTH(1,X86_SEL_TYPE_RO):
|
---|
565 | case BOTH(1,X86_SEL_TYPE_RO_ACC):
|
---|
566 | case BOTH(1,X86_SEL_TYPE_RW):
|
---|
567 | case BOTH(1,X86_SEL_TYPE_RW_ACC):
|
---|
568 | case BOTH(1,X86_SEL_TYPE_EO):
|
---|
569 | case BOTH(1,X86_SEL_TYPE_EO_ACC):
|
---|
570 | case BOTH(1,X86_SEL_TYPE_ER):
|
---|
571 | case BOTH(1,X86_SEL_TYPE_ER_ACC):
|
---|
572 | if (!(fFlags & SELMTOFLAT_FLAGS_NO_PL))
|
---|
573 | {
|
---|
574 | /** @todo fix this mess */
|
---|
575 | }
|
---|
576 | /* check limit. */
|
---|
577 | if ((RTGCUINTPTR)Addr > u32Limit)
|
---|
578 | return VERR_OUT_OF_SELECTOR_BOUNDS;
|
---|
579 | /* ok */
|
---|
580 | if (ppvGC)
|
---|
581 | *ppvGC = pvFlat;
|
---|
582 | if (pcb)
|
---|
583 | *pcb = u32Limit - (uint32_t)Addr + 1;
|
---|
584 | return VINF_SUCCESS;
|
---|
585 |
|
---|
586 | case BOTH(1,X86_SEL_TYPE_EO_CONF):
|
---|
587 | case BOTH(1,X86_SEL_TYPE_EO_CONF_ACC):
|
---|
588 | case BOTH(1,X86_SEL_TYPE_ER_CONF):
|
---|
589 | case BOTH(1,X86_SEL_TYPE_ER_CONF_ACC):
|
---|
590 | if (!(fFlags & SELMTOFLAT_FLAGS_NO_PL))
|
---|
591 | {
|
---|
592 | /** @todo fix this mess */
|
---|
593 | }
|
---|
594 | /* check limit. */
|
---|
595 | if ((RTGCUINTPTR)Addr > u32Limit)
|
---|
596 | return VERR_OUT_OF_SELECTOR_BOUNDS;
|
---|
597 | /* ok */
|
---|
598 | if (ppvGC)
|
---|
599 | *ppvGC = pvFlat;
|
---|
600 | if (pcb)
|
---|
601 | *pcb = u32Limit - (uint32_t)Addr + 1;
|
---|
602 | return VINF_SUCCESS;
|
---|
603 |
|
---|
604 | case BOTH(1,X86_SEL_TYPE_RO_DOWN):
|
---|
605 | case BOTH(1,X86_SEL_TYPE_RO_DOWN_ACC):
|
---|
606 | case BOTH(1,X86_SEL_TYPE_RW_DOWN):
|
---|
607 | case BOTH(1,X86_SEL_TYPE_RW_DOWN_ACC):
|
---|
608 | if (!(fFlags & SELMTOFLAT_FLAGS_NO_PL))
|
---|
609 | {
|
---|
610 | /** @todo fix this mess */
|
---|
611 | }
|
---|
612 | /* check limit. */
|
---|
613 | if (!u1Granularity && (RTGCUINTPTR)Addr > (RTGCUINTPTR)0xffff)
|
---|
614 | return VERR_OUT_OF_SELECTOR_BOUNDS;
|
---|
615 | if ((RTGCUINTPTR)Addr <= u32Limit)
|
---|
616 | return VERR_OUT_OF_SELECTOR_BOUNDS;
|
---|
617 |
|
---|
618 | /* ok */
|
---|
619 | if (ppvGC)
|
---|
620 | *ppvGC = pvFlat;
|
---|
621 | if (pcb)
|
---|
622 | *pcb = (RTGCUINTPTR)(u1Granularity ? 0xffffffff : 0xffff) - (RTGCUINTPTR)Addr + 1;
|
---|
623 | return VINF_SUCCESS;
|
---|
624 |
|
---|
625 | case BOTH(0,X86_SEL_TYPE_SYS_286_TSS_AVAIL):
|
---|
626 | case BOTH(0,X86_SEL_TYPE_SYS_LDT):
|
---|
627 | case BOTH(0,X86_SEL_TYPE_SYS_286_TSS_BUSY):
|
---|
628 | case BOTH(0,X86_SEL_TYPE_SYS_286_CALL_GATE):
|
---|
629 | case BOTH(0,X86_SEL_TYPE_SYS_TASK_GATE):
|
---|
630 | case BOTH(0,X86_SEL_TYPE_SYS_286_INT_GATE):
|
---|
631 | case BOTH(0,X86_SEL_TYPE_SYS_286_TRAP_GATE):
|
---|
632 | case BOTH(0,X86_SEL_TYPE_SYS_386_TSS_AVAIL):
|
---|
633 | case BOTH(0,X86_SEL_TYPE_SYS_386_TSS_BUSY):
|
---|
634 | case BOTH(0,X86_SEL_TYPE_SYS_386_CALL_GATE):
|
---|
635 | case BOTH(0,X86_SEL_TYPE_SYS_386_INT_GATE):
|
---|
636 | case BOTH(0,X86_SEL_TYPE_SYS_386_TRAP_GATE):
|
---|
637 | if (!(fFlags & SELMTOFLAT_FLAGS_NO_PL))
|
---|
638 | {
|
---|
639 | /** @todo fix this mess */
|
---|
640 | }
|
---|
641 | /* check limit. */
|
---|
642 | if ((RTGCUINTPTR)Addr > u32Limit)
|
---|
643 | return VERR_OUT_OF_SELECTOR_BOUNDS;
|
---|
644 | /* ok */
|
---|
645 | if (ppvGC)
|
---|
646 | *ppvGC = pvFlat;
|
---|
647 | if (pcb)
|
---|
648 | *pcb = 0xffffffff - (RTGCUINTPTR)pvFlat + 1; /* Depends on the type.. fixme if we care. */
|
---|
649 | return VINF_SUCCESS;
|
---|
650 |
|
---|
651 | default:
|
---|
652 | return VERR_INVALID_SELECTOR;
|
---|
653 |
|
---|
654 | }
|
---|
655 | #undef BOTH
|
---|
656 | }
|
---|
657 | return VERR_SELECTOR_NOT_PRESENT;
|
---|
658 | }
|
---|
659 | #endif /* !IN_RING0 */
|
---|
660 |
|
---|
661 |
|
---|
662 | /**
|
---|
663 | * Validates and converts a GC selector based code address to a flat
|
---|
664 | * address when in real or v8086 mode.
|
---|
665 | *
|
---|
666 | * @returns VINF_SUCCESS.
|
---|
667 | * @param pVM VM Handle.
|
---|
668 | * @param SelCS Selector part.
|
---|
669 | * @param pHidCS The hidden CS register part. Optional.
|
---|
670 | * @param Addr Address part.
|
---|
671 | * @param ppvFlat Where to store the flat address.
|
---|
672 | */
|
---|
673 | DECLINLINE(int) selmValidateAndConvertCSAddrRealMode(PVM pVM, RTSEL SelCS, PCPUMSELREGHID pHidCS, RTGCPTR Addr, PRTGCPTR ppvFlat)
|
---|
674 | {
|
---|
675 | RTGCUINTPTR uFlat = (RTGCUINTPTR)Addr & 0xffff;
|
---|
676 | if (!pHidCS || !CPUMAreHiddenSelRegsValid(pVM))
|
---|
677 | uFlat += ((RTGCUINTPTR)SelCS << 4);
|
---|
678 | else
|
---|
679 | uFlat += pHidCS->u64Base;
|
---|
680 | *ppvFlat = (RTGCPTR)uFlat;
|
---|
681 | return VINF_SUCCESS;
|
---|
682 | }
|
---|
683 |
|
---|
684 |
|
---|
685 | #ifndef IN_RING0
|
---|
686 | /**
|
---|
687 | * Validates and converts a GC selector based code address to a flat
|
---|
688 | * address when in protected/long mode using the standard algorithm.
|
---|
689 | *
|
---|
690 | * @returns VBox status code.
|
---|
691 | * @param pVM VM Handle.
|
---|
692 | * @param SelCPL Current privilege level. Get this from SS - CS might be conforming!
|
---|
693 | * A full selector can be passed, we'll only use the RPL part.
|
---|
694 | * @param SelCS Selector part.
|
---|
695 | * @param Addr Address part.
|
---|
696 | * @param ppvFlat Where to store the flat address.
|
---|
697 | * @param pcBits Where to store the segment bitness (16/32/64). Optional.
|
---|
698 | */
|
---|
699 | DECLINLINE(int) selmValidateAndConvertCSAddrStd(PVM pVM, RTSEL SelCPL, RTSEL SelCS, RTGCPTR Addr, PRTGCPTR ppvFlat, uint32_t *pcBits)
|
---|
700 | {
|
---|
701 | Assert(!CPUMAreHiddenSelRegsValid(pVM));
|
---|
702 |
|
---|
703 | /** @todo validate limit! */
|
---|
704 | X86DESC Desc;
|
---|
705 | if (!(SelCS & X86_SEL_LDT))
|
---|
706 | Desc = pVM->selm.s.CTX_SUFF(paGdt)[SelCS >> X86_SEL_SHIFT];
|
---|
707 | else
|
---|
708 | {
|
---|
709 | /** @todo handle LDT page(s) not present! */
|
---|
710 | PX86DESC paLDT = (PX86DESC)((char *)pVM->selm.s.CTX_SUFF(pvLdt) + pVM->selm.s.offLdtHyper);
|
---|
711 | Desc = paLDT[SelCS >> X86_SEL_SHIFT];
|
---|
712 | }
|
---|
713 |
|
---|
714 | /*
|
---|
715 | * Check if present.
|
---|
716 | */
|
---|
717 | if (Desc.Gen.u1Present)
|
---|
718 | {
|
---|
719 | /*
|
---|
720 | * Type check.
|
---|
721 | */
|
---|
722 | if ( Desc.Gen.u1DescType == 1
|
---|
723 | && (Desc.Gen.u4Type & X86_SEL_TYPE_CODE))
|
---|
724 | {
|
---|
725 | /*
|
---|
726 | * Check level.
|
---|
727 | */
|
---|
728 | unsigned uLevel = RT_MAX(SelCPL & X86_SEL_RPL, SelCS & X86_SEL_RPL);
|
---|
729 | if ( !(Desc.Gen.u4Type & X86_SEL_TYPE_CONF)
|
---|
730 | ? uLevel <= Desc.Gen.u2Dpl
|
---|
731 | : uLevel >= Desc.Gen.u2Dpl /* hope I got this right now... */
|
---|
732 | )
|
---|
733 | {
|
---|
734 | /*
|
---|
735 | * Limit check.
|
---|
736 | */
|
---|
737 | uint32_t u32Limit = X86DESC_LIMIT(Desc);
|
---|
738 | if (Desc.Gen.u1Granularity)
|
---|
739 | u32Limit = (u32Limit << PAGE_SHIFT) | PAGE_OFFSET_MASK;
|
---|
740 | if ((RTGCUINTPTR)Addr <= u32Limit)
|
---|
741 | {
|
---|
742 | *ppvFlat = (RTGCPTR)((RTGCUINTPTR)Addr + X86DESC_BASE(Desc));
|
---|
743 | /* Cut the address to 32 bits. */
|
---|
744 | *ppvFlat &= 0xffffffff;
|
---|
745 |
|
---|
746 | if (pcBits)
|
---|
747 | *pcBits = Desc.Gen.u1DefBig ? 32 : 16; /** @todo GUEST64 */
|
---|
748 | return VINF_SUCCESS;
|
---|
749 | }
|
---|
750 | return VERR_OUT_OF_SELECTOR_BOUNDS;
|
---|
751 | }
|
---|
752 | return VERR_INVALID_RPL;
|
---|
753 | }
|
---|
754 | return VERR_NOT_CODE_SELECTOR;
|
---|
755 | }
|
---|
756 | return VERR_SELECTOR_NOT_PRESENT;
|
---|
757 | }
|
---|
758 | #endif /* !IN_RING0 */
|
---|
759 |
|
---|
760 |
|
---|
761 | /**
|
---|
762 | * Validates and converts a GC selector based code address to a flat
|
---|
763 | * address when in protected/long mode using the standard algorithm.
|
---|
764 | *
|
---|
765 | * @returns VBox status code.
|
---|
766 | * @param pVCpu VMCPU Handle.
|
---|
767 | * @param SelCPL Current privilege level. Get this from SS - CS might be conforming!
|
---|
768 | * A full selector can be passed, we'll only use the RPL part.
|
---|
769 | * @param SelCS Selector part.
|
---|
770 | * @param Addr Address part.
|
---|
771 | * @param ppvFlat Where to store the flat address.
|
---|
772 | */
|
---|
773 | DECLINLINE(int) selmValidateAndConvertCSAddrHidden(PVMCPU pVCpu, RTSEL SelCPL, RTSEL SelCS, PCPUMSELREGHID pHidCS, RTGCPTR Addr, PRTGCPTR ppvFlat)
|
---|
774 | {
|
---|
775 | /*
|
---|
776 | * Check if present.
|
---|
777 | */
|
---|
778 | if (pHidCS->Attr.n.u1Present)
|
---|
779 | {
|
---|
780 | /*
|
---|
781 | * Type check.
|
---|
782 | */
|
---|
783 | if ( pHidCS->Attr.n.u1DescType == 1
|
---|
784 | && (pHidCS->Attr.n.u4Type & X86_SEL_TYPE_CODE))
|
---|
785 | {
|
---|
786 | /*
|
---|
787 | * Check level.
|
---|
788 | */
|
---|
789 | unsigned uLevel = RT_MAX(SelCPL & X86_SEL_RPL, SelCS & X86_SEL_RPL);
|
---|
790 | if ( !(pHidCS->Attr.n.u4Type & X86_SEL_TYPE_CONF)
|
---|
791 | ? uLevel <= pHidCS->Attr.n.u2Dpl
|
---|
792 | : uLevel >= pHidCS->Attr.n.u2Dpl /* hope I got this right now... */
|
---|
793 | )
|
---|
794 | {
|
---|
795 | /* 64 bits mode: CS, DS, ES and SS are treated as if each segment base is 0 (Intel® 64 and IA-32 Architectures Software Developer's Manual: 3.4.2.1). */
|
---|
796 | if ( pHidCS->Attr.n.u1Long
|
---|
797 | && CPUMIsGuestInLongMode(pVCpu))
|
---|
798 | {
|
---|
799 | *ppvFlat = Addr;
|
---|
800 | return VINF_SUCCESS;
|
---|
801 | }
|
---|
802 |
|
---|
803 | /*
|
---|
804 | * Limit check. Note that the limit in the hidden register is the
|
---|
805 | * final value. The granularity bit was included in its calculation.
|
---|
806 | */
|
---|
807 | uint32_t u32Limit = pHidCS->u32Limit;
|
---|
808 | if ((RTGCUINTPTR)Addr <= u32Limit)
|
---|
809 | {
|
---|
810 | *ppvFlat = (RTGCPTR)( (RTGCUINTPTR)Addr + pHidCS->u64Base );
|
---|
811 | return VINF_SUCCESS;
|
---|
812 | }
|
---|
813 | return VERR_OUT_OF_SELECTOR_BOUNDS;
|
---|
814 | }
|
---|
815 | Log(("Invalid RPL Attr.n.u4Type=%x cpl=%x dpl=%x\n", pHidCS->Attr.n.u4Type, uLevel, pHidCS->Attr.n.u2Dpl));
|
---|
816 | return VERR_INVALID_RPL;
|
---|
817 | }
|
---|
818 | return VERR_NOT_CODE_SELECTOR;
|
---|
819 | }
|
---|
820 | return VERR_SELECTOR_NOT_PRESENT;
|
---|
821 | }
|
---|
822 |
|
---|
823 |
|
---|
824 | #ifdef IN_RC
|
---|
825 | /**
|
---|
826 | * Validates and converts a GC selector based code address to a flat address.
|
---|
827 | *
|
---|
828 | * This is like SELMValidateAndConvertCSAddr + SELMIsSelector32Bit but with
|
---|
829 | * invalid hidden CS data. It's customized for dealing efficiently with CS
|
---|
830 | * at GC trap time.
|
---|
831 | *
|
---|
832 | * @returns VBox status code.
|
---|
833 | * @param pVM VM Handle.
|
---|
834 | * @param eflags Current eflags
|
---|
835 | * @param SelCPL Current privilege level. Get this from SS - CS might be conforming!
|
---|
836 | * A full selector can be passed, we'll only use the RPL part.
|
---|
837 | * @param SelCS Selector part.
|
---|
838 | * @param Addr Address part.
|
---|
839 | * @param ppvFlat Where to store the flat address.
|
---|
840 | * @param pcBits Where to store the 64-bit/32-bit/16-bit indicator.
|
---|
841 | */
|
---|
842 | VMMDECL(int) SELMValidateAndConvertCSAddrGCTrap(PVM pVM, X86EFLAGS eflags, RTSEL SelCPL, RTSEL SelCS, RTGCPTR Addr, PRTGCPTR ppvFlat, uint32_t *pcBits)
|
---|
843 | {
|
---|
844 | Assert(pVM->cCpus == 1);
|
---|
845 | PVMCPU pVCpu = &pVM->aCpus[0];
|
---|
846 |
|
---|
847 | if ( eflags.Bits.u1VM
|
---|
848 | || CPUMIsGuestInRealMode(pVCpu))
|
---|
849 | {
|
---|
850 | *pcBits = 16;
|
---|
851 | return selmValidateAndConvertCSAddrRealMode(pVM, SelCS, NULL, Addr, ppvFlat);
|
---|
852 | }
|
---|
853 | return selmValidateAndConvertCSAddrStd(pVM, SelCPL, SelCS, Addr, ppvFlat, pcBits);
|
---|
854 | }
|
---|
855 | #endif /* IN_RC */
|
---|
856 |
|
---|
857 |
|
---|
858 | /**
|
---|
859 | * Validates and converts a GC selector based code address to a flat address.
|
---|
860 | *
|
---|
861 | * @returns VBox status code.
|
---|
862 | * @param pVM VM Handle.
|
---|
863 | * @param eflags Current eflags
|
---|
864 | * @param SelCPL Current privilege level. Get this from SS - CS might be conforming!
|
---|
865 | * A full selector can be passed, we'll only use the RPL part.
|
---|
866 | * @param SelCS Selector part.
|
---|
867 | * @param pHiddenSel The hidden CS selector register.
|
---|
868 | * @param Addr Address part.
|
---|
869 | * @param ppvFlat Where to store the flat address.
|
---|
870 | */
|
---|
871 | VMMDECL(int) SELMValidateAndConvertCSAddr(PVM pVM, X86EFLAGS eflags, RTSEL SelCPL, RTSEL SelCS, CPUMSELREGHID *pHiddenCSSel, RTGCPTR Addr, PRTGCPTR ppvFlat)
|
---|
872 | {
|
---|
873 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
874 |
|
---|
875 | if ( eflags.Bits.u1VM
|
---|
876 | || CPUMIsGuestInRealMode(pVCpu))
|
---|
877 | return selmValidateAndConvertCSAddrRealMode(pVM, SelCS, pHiddenCSSel, Addr, ppvFlat);
|
---|
878 |
|
---|
879 | #ifdef IN_RING0
|
---|
880 | Assert(CPUMAreHiddenSelRegsValid(pVM));
|
---|
881 | #else
|
---|
882 | /** @todo when we're in 16 bits mode, we should cut off the address as well? (like in selmValidateAndConvertCSAddrRealMode) */
|
---|
883 | if (!CPUMAreHiddenSelRegsValid(pVM))
|
---|
884 | return selmValidateAndConvertCSAddrStd(pVM, SelCPL, SelCS, Addr, ppvFlat, NULL);
|
---|
885 | #endif
|
---|
886 | return selmValidateAndConvertCSAddrHidden(pVCpu, SelCPL, SelCS, pHiddenCSSel, Addr, ppvFlat);
|
---|
887 | }
|
---|
888 |
|
---|
889 |
|
---|
890 | #ifndef IN_RING0
|
---|
891 | /**
|
---|
892 | * Return the cpu mode corresponding to the (CS) selector
|
---|
893 | *
|
---|
894 | * @returns DISCPUMODE according to the selector type (16, 32 or 64 bits)
|
---|
895 | * @param pVM VM Handle.
|
---|
896 | * @param Sel The selector.
|
---|
897 | */
|
---|
898 | static DISCPUMODE selmGetCpuModeFromSelector(PVM pVM, RTSEL Sel)
|
---|
899 | {
|
---|
900 | Assert(!CPUMAreHiddenSelRegsValid(pVM));
|
---|
901 |
|
---|
902 | /** @todo validate limit! */
|
---|
903 | X86DESC Desc;
|
---|
904 | if (!(Sel & X86_SEL_LDT))
|
---|
905 | Desc = pVM->selm.s.CTX_SUFF(paGdt)[Sel >> X86_SEL_SHIFT];
|
---|
906 | else
|
---|
907 | {
|
---|
908 | /** @todo handle LDT page(s) not present! */
|
---|
909 | PX86DESC paLDT = (PX86DESC)((char *)pVM->selm.s.CTX_SUFF(pvLdt) + pVM->selm.s.offLdtHyper);
|
---|
910 | Desc = paLDT[Sel >> X86_SEL_SHIFT];
|
---|
911 | }
|
---|
912 | return (Desc.Gen.u1DefBig) ? CPUMODE_32BIT : CPUMODE_16BIT;
|
---|
913 | }
|
---|
914 | #endif /* !IN_RING0 */
|
---|
915 |
|
---|
916 |
|
---|
917 | /**
|
---|
918 | * Return the cpu mode corresponding to the (CS) selector
|
---|
919 | *
|
---|
920 | * @returns DISCPUMODE according to the selector type (16, 32 or 64 bits)
|
---|
921 | * @param pVM VM Handle.
|
---|
922 | * @param eflags Current eflags register
|
---|
923 | * @param Sel The selector.
|
---|
924 | * @param pHiddenSel The hidden selector register.
|
---|
925 | */
|
---|
926 | VMMDECL(DISCPUMODE) SELMGetCpuModeFromSelector(PVM pVM, X86EFLAGS eflags, RTSEL Sel, CPUMSELREGHID *pHiddenSel)
|
---|
927 | {
|
---|
928 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
929 | #ifdef IN_RING0
|
---|
930 | Assert(CPUMAreHiddenSelRegsValid(pVM));
|
---|
931 | #else /* !IN_RING0 */
|
---|
932 | if (!CPUMAreHiddenSelRegsValid(pVM))
|
---|
933 | {
|
---|
934 | /*
|
---|
935 | * Deal with real & v86 mode first.
|
---|
936 | */
|
---|
937 | if ( eflags.Bits.u1VM
|
---|
938 | || CPUMIsGuestInRealMode(pVCpu))
|
---|
939 | return CPUMODE_16BIT;
|
---|
940 |
|
---|
941 | return selmGetCpuModeFromSelector(pVM, Sel);
|
---|
942 | }
|
---|
943 | #endif /* !IN_RING0 */
|
---|
944 | if ( pHiddenSel->Attr.n.u1Long
|
---|
945 | && CPUMIsGuestInLongMode(pVCpu))
|
---|
946 | return CPUMODE_64BIT;
|
---|
947 |
|
---|
948 | /* Else compatibility or 32 bits mode. */
|
---|
949 | return (pHiddenSel->Attr.n.u1DefBig) ? CPUMODE_32BIT : CPUMODE_16BIT;
|
---|
950 | }
|
---|
951 |
|
---|
952 |
|
---|
953 | /**
|
---|
954 | * Returns Hypervisor's Trap 08 (\#DF) selector.
|
---|
955 | *
|
---|
956 | * @returns Hypervisor's Trap 08 (\#DF) selector.
|
---|
957 | * @param pVM VM Handle.
|
---|
958 | */
|
---|
959 | VMMDECL(RTSEL) SELMGetTrap8Selector(PVM pVM)
|
---|
960 | {
|
---|
961 | return pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08];
|
---|
962 | }
|
---|
963 |
|
---|
964 |
|
---|
965 | /**
|
---|
966 | * Sets EIP of Hypervisor's Trap 08 (\#DF) TSS.
|
---|
967 | *
|
---|
968 | * @param pVM VM Handle.
|
---|
969 | * @param u32EIP EIP of Trap 08 handler.
|
---|
970 | */
|
---|
971 | VMMDECL(void) SELMSetTrap8EIP(PVM pVM, uint32_t u32EIP)
|
---|
972 | {
|
---|
973 | pVM->selm.s.TssTrap08.eip = u32EIP;
|
---|
974 | }
|
---|
975 |
|
---|
976 |
|
---|
977 | /**
|
---|
978 | * Sets ss:esp for ring1 in main Hypervisor's TSS.
|
---|
979 | *
|
---|
980 | * @param pVM VM Handle.
|
---|
981 | * @param ss Ring1 SS register value. Pass 0 if invalid.
|
---|
982 | * @param esp Ring1 ESP register value.
|
---|
983 | */
|
---|
984 | void selmSetRing1Stack(PVM pVM, uint32_t ss, RTGCPTR32 esp)
|
---|
985 | {
|
---|
986 | Assert((ss & 1) || esp == 0);
|
---|
987 | pVM->selm.s.Tss.ss1 = ss;
|
---|
988 | pVM->selm.s.Tss.esp1 = (uint32_t)esp;
|
---|
989 | }
|
---|
990 |
|
---|
991 |
|
---|
992 | #ifndef IN_RING0
|
---|
993 | /**
|
---|
994 | * Gets ss:esp for ring1 in main Hypervisor's TSS.
|
---|
995 | *
|
---|
996 | * Returns SS=0 if the ring-1 stack isn't valid.
|
---|
997 | *
|
---|
998 | * @returns VBox status code.
|
---|
999 | * @param pVM VM Handle.
|
---|
1000 | * @param pSS Ring1 SS register value.
|
---|
1001 | * @param pEsp Ring1 ESP register value.
|
---|
1002 | */
|
---|
1003 | VMMDECL(int) SELMGetRing1Stack(PVM pVM, uint32_t *pSS, PRTGCPTR32 pEsp)
|
---|
1004 | {
|
---|
1005 | Assert(pVM->cCpus == 1);
|
---|
1006 | PVMCPU pVCpu = &pVM->aCpus[0];
|
---|
1007 |
|
---|
1008 | if (pVM->selm.s.fSyncTSSRing0Stack)
|
---|
1009 | {
|
---|
1010 | RTGCPTR GCPtrTss = pVM->selm.s.GCPtrGuestTss;
|
---|
1011 | int rc;
|
---|
1012 | VBOXTSS tss;
|
---|
1013 |
|
---|
1014 | Assert(pVM->selm.s.GCPtrGuestTss && pVM->selm.s.cbMonitoredGuestTss);
|
---|
1015 |
|
---|
1016 | # ifdef IN_RC
|
---|
1017 | bool fTriedAlready = false;
|
---|
1018 |
|
---|
1019 | l_tryagain:
|
---|
1020 | PVBOXTSS pTss = (PVBOXTSS)(uintptr_t)GCPtrTss;
|
---|
1021 | rc = MMGCRamRead(pVM, &tss.ss0, &pTss->ss0, sizeof(tss.ss0));
|
---|
1022 | rc |= MMGCRamRead(pVM, &tss.esp0, &pTss->esp0, sizeof(tss.esp0));
|
---|
1023 | # ifdef DEBUG
|
---|
1024 | rc |= MMGCRamRead(pVM, &tss.offIoBitmap, &pTss->offIoBitmap, sizeof(tss.offIoBitmap));
|
---|
1025 | # endif
|
---|
1026 |
|
---|
1027 | if (RT_FAILURE(rc))
|
---|
1028 | {
|
---|
1029 | if (!fTriedAlready)
|
---|
1030 | {
|
---|
1031 | /* Shadow page might be out of sync. Sync and try again */
|
---|
1032 | /** @todo might cross page boundary */
|
---|
1033 | fTriedAlready = true;
|
---|
1034 | rc = PGMPrefetchPage(pVCpu, (RTGCPTR)GCPtrTss);
|
---|
1035 | if (rc != VINF_SUCCESS)
|
---|
1036 | return rc;
|
---|
1037 | goto l_tryagain;
|
---|
1038 | }
|
---|
1039 | AssertMsgFailed(("Unable to read TSS structure at %08X\n", GCPtrTss));
|
---|
1040 | return rc;
|
---|
1041 | }
|
---|
1042 |
|
---|
1043 | # else /* !IN_RC */
|
---|
1044 | /* Reading too much. Could be cheaper than two seperate calls though. */
|
---|
1045 | rc = PGMPhysSimpleReadGCPtr(pVCpu, &tss, GCPtrTss, sizeof(VBOXTSS));
|
---|
1046 | if (RT_FAILURE(rc))
|
---|
1047 | {
|
---|
1048 | AssertReleaseMsgFailed(("Unable to read TSS structure at %08X\n", GCPtrTss));
|
---|
1049 | return rc;
|
---|
1050 | }
|
---|
1051 | # endif /* !IN_RC */
|
---|
1052 |
|
---|
1053 | # ifdef LOG_ENABLED
|
---|
1054 | uint32_t ssr0 = pVM->selm.s.Tss.ss1;
|
---|
1055 | uint32_t espr0 = pVM->selm.s.Tss.esp1;
|
---|
1056 | ssr0 &= ~1;
|
---|
1057 |
|
---|
1058 | if (ssr0 != tss.ss0 || espr0 != tss.esp0)
|
---|
1059 | Log(("SELMGetRing1Stack: Updating TSS ring 0 stack to %04X:%08X\n", tss.ss0, tss.esp0));
|
---|
1060 |
|
---|
1061 | Log(("offIoBitmap=%#x\n", tss.offIoBitmap));
|
---|
1062 | # endif
|
---|
1063 | /* Update our TSS structure for the guest's ring 1 stack */
|
---|
1064 | selmSetRing1Stack(pVM, tss.ss0 | 1, (RTGCPTR32)tss.esp0);
|
---|
1065 | pVM->selm.s.fSyncTSSRing0Stack = false;
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | *pSS = pVM->selm.s.Tss.ss1;
|
---|
1069 | *pEsp = (RTGCPTR32)pVM->selm.s.Tss.esp1;
|
---|
1070 |
|
---|
1071 | return VINF_SUCCESS;
|
---|
1072 | }
|
---|
1073 | #endif /* !IN_RING0 */
|
---|
1074 |
|
---|
1075 |
|
---|
1076 | /**
|
---|
1077 | * Returns Guest TSS pointer
|
---|
1078 | *
|
---|
1079 | * @returns Pointer to the guest TSS, RTRCPTR_MAX if not being monitored.
|
---|
1080 | * @param pVM VM Handle.
|
---|
1081 | */
|
---|
1082 | VMMDECL(RTGCPTR) SELMGetGuestTSS(PVM pVM)
|
---|
1083 | {
|
---|
1084 | return (RTGCPTR)pVM->selm.s.GCPtrGuestTss;
|
---|
1085 | }
|
---|
1086 |
|
---|
1087 |
|
---|
1088 | #ifndef IN_RING0
|
---|
1089 |
|
---|
1090 | /**
|
---|
1091 | * Gets the hypervisor code selector (CS).
|
---|
1092 | * @returns CS selector.
|
---|
1093 | * @param pVM The VM handle.
|
---|
1094 | */
|
---|
1095 | VMMDECL(RTSEL) SELMGetHyperCS(PVM pVM)
|
---|
1096 | {
|
---|
1097 | return pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS];
|
---|
1098 | }
|
---|
1099 |
|
---|
1100 |
|
---|
1101 | /**
|
---|
1102 | * Gets the 64-mode hypervisor code selector (CS64).
|
---|
1103 | * @returns CS selector.
|
---|
1104 | * @param pVM The VM handle.
|
---|
1105 | */
|
---|
1106 | VMMDECL(RTSEL) SELMGetHyperCS64(PVM pVM)
|
---|
1107 | {
|
---|
1108 | return pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS64];
|
---|
1109 | }
|
---|
1110 |
|
---|
1111 |
|
---|
1112 | /**
|
---|
1113 | * Gets the hypervisor data selector (DS).
|
---|
1114 | * @returns DS selector.
|
---|
1115 | * @param pVM The VM handle.
|
---|
1116 | */
|
---|
1117 | VMMDECL(RTSEL) SELMGetHyperDS(PVM pVM)
|
---|
1118 | {
|
---|
1119 | return pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS];
|
---|
1120 | }
|
---|
1121 |
|
---|
1122 |
|
---|
1123 | /**
|
---|
1124 | * Gets the hypervisor TSS selector.
|
---|
1125 | * @returns TSS selector.
|
---|
1126 | * @param pVM The VM handle.
|
---|
1127 | */
|
---|
1128 | VMMDECL(RTSEL) SELMGetHyperTSS(PVM pVM)
|
---|
1129 | {
|
---|
1130 | return pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS];
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 |
|
---|
1134 | /**
|
---|
1135 | * Gets the hypervisor TSS Trap 8 selector.
|
---|
1136 | * @returns TSS Trap 8 selector.
|
---|
1137 | * @param pVM The VM handle.
|
---|
1138 | */
|
---|
1139 | VMMDECL(RTSEL) SELMGetHyperTSSTrap08(PVM pVM)
|
---|
1140 | {
|
---|
1141 | return pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08];
|
---|
1142 | }
|
---|
1143 |
|
---|
1144 | /**
|
---|
1145 | * Gets the address for the hypervisor GDT.
|
---|
1146 | *
|
---|
1147 | * @returns The GDT address.
|
---|
1148 | * @param pVM The VM handle.
|
---|
1149 | * @remark This is intended only for very special use, like in the world
|
---|
1150 | * switchers. Don't exploit this API!
|
---|
1151 | */
|
---|
1152 | VMMDECL(RTRCPTR) SELMGetHyperGDT(PVM pVM)
|
---|
1153 | {
|
---|
1154 | /*
|
---|
1155 | * Always convert this from the HC pointer since we can be
|
---|
1156 | * called before the first relocation and have to work correctly
|
---|
1157 | * without having dependencies on the relocation order.
|
---|
1158 | */
|
---|
1159 | return (RTRCPTR)MMHyperR3ToRC(pVM, pVM->selm.s.paGdtR3);
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | #endif /* !IN_RING0 */
|
---|
1163 |
|
---|
1164 | /**
|
---|
1165 | * Gets info about the current TSS.
|
---|
1166 | *
|
---|
1167 | * @returns VBox status code.
|
---|
1168 | * @retval VINF_SUCCESS if we've got a TSS loaded.
|
---|
1169 | * @retval VERR_SELM_NO_TSS if we haven't got a TSS (rather unlikely).
|
---|
1170 | *
|
---|
1171 | * @param pVM The VM handle.
|
---|
1172 | * @param pVCpu VMCPU Handle.
|
---|
1173 | * @param pGCPtrTss Where to store the TSS address.
|
---|
1174 | * @param pcbTss Where to store the TSS size limit.
|
---|
1175 | * @param pfCanHaveIOBitmap Where to store the can-have-I/O-bitmap indicator. (optional)
|
---|
1176 | */
|
---|
1177 | VMMDECL(int) SELMGetTSSInfo(PVM pVM, PVMCPU pVCpu, PRTGCUINTPTR pGCPtrTss, PRTGCUINTPTR pcbTss, bool *pfCanHaveIOBitmap)
|
---|
1178 | {
|
---|
1179 | /*
|
---|
1180 | * The TR hidden register is always valid.
|
---|
1181 | */
|
---|
1182 | CPUMSELREGHID trHid;
|
---|
1183 | RTSEL tr = CPUMGetGuestTR(pVCpu, &trHid);
|
---|
1184 | if (!(tr & X86_SEL_MASK))
|
---|
1185 | return VERR_SELM_NO_TSS;
|
---|
1186 |
|
---|
1187 | *pGCPtrTss = trHid.u64Base;
|
---|
1188 | *pcbTss = trHid.u32Limit + (trHid.u32Limit != UINT32_MAX); /* be careful. */
|
---|
1189 | if (pfCanHaveIOBitmap)
|
---|
1190 | *pfCanHaveIOBitmap = trHid.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_AVAIL
|
---|
1191 | || trHid.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_BUSY;
|
---|
1192 | return VINF_SUCCESS;
|
---|
1193 | }
|
---|
1194 |
|
---|
1195 |
|
---|
1196 |
|
---|
1197 | /**
|
---|
1198 | * Notification callback which is called whenever there is a chance that a CR3
|
---|
1199 | * value might have changed.
|
---|
1200 | * This is called by PGM.
|
---|
1201 | *
|
---|
1202 | * @param pVM The VM handle
|
---|
1203 | * @param pVCpu The VMCPU handle
|
---|
1204 | */
|
---|
1205 | VMMDECL(void) SELMShadowCR3Changed(PVM pVM, PVMCPU pVCpu)
|
---|
1206 | {
|
---|
1207 | /** @todo SMP support!! */
|
---|
1208 | pVM->selm.s.Tss.cr3 = PGMGetHyperCR3(pVCpu);
|
---|
1209 | pVM->selm.s.TssTrap08.cr3 = PGMGetInterRCCR3(pVM, pVCpu);
|
---|
1210 | }
|
---|