VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/SELMAll.cpp@ 5662

Last change on this file since 5662 was 5396, checked in by vboxsync, 17 years ago

Shaved off a few ticks on RDTSC emulation to speed up Solaris guest.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 27.6 KB
Line 
1/* $Id: SELMAll.cpp 5396 2007-10-21 03:57:23Z vboxsync $ */
2/** @file
3 * SELM All contexts.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * 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
35
36
37/**
38 * Converts a GC selector based address to a flat address.
39 *
40 * No limit checks are done. Use the SELMToFlat*() or SELMValidate*() functions
41 * for that.
42 *
43 * @returns Flat address.
44 * @param pVM VM Handle.
45 * @param Sel Selector part.
46 * @param Addr Address part.
47 */
48static RTGCPTR selmToFlat(PVM pVM, RTSEL Sel, RTGCPTR Addr)
49{
50 Assert(!CPUMAreHiddenSelRegsValid(pVM));
51
52 /** @todo check the limit. */
53 VBOXDESC Desc;
54 if (!(Sel & X86_SEL_LDT))
55 Desc = pVM->selm.s.CTXSUFF(paGdt)[Sel >> X86_SEL_SHIFT];
56 else
57 {
58 /** @todo handle LDT pages not present! */
59 #ifdef IN_GC
60 PVBOXDESC paLDT = (PVBOXDESC)((char *)pVM->selm.s.GCPtrLdt + pVM->selm.s.offLdtHyper);
61 #else
62 PVBOXDESC paLDT = (PVBOXDESC)((char *)pVM->selm.s.HCPtrLdt + pVM->selm.s.offLdtHyper);
63 #endif
64 Desc = paLDT[Sel >> X86_SEL_SHIFT];
65 }
66
67 return (RTGCPTR)( (RTGCUINTPTR)Addr
68 + ( (Desc.Gen.u8BaseHigh2 << 24)
69 | (Desc.Gen.u8BaseHigh1 << 16)
70 | Desc.Gen.u16BaseLow));
71}
72
73
74/**
75 * Converts a GC selector based address to a flat address.
76 *
77 * No limit checks are done. Use the SELMToFlat*() or SELMValidate*() functions
78 * for that.
79 *
80 * @returns Flat address.
81 * @param pVM VM Handle.
82 * @param eflags Current eflags
83 * @param Sel Selector part.
84 * @param pHiddenSel Hidden selector register
85 * @param Addr Address part.
86 */
87SELMDECL(RTGCPTR) SELMToFlat(PVM pVM, X86EFLAGS eflags, RTSEL Sel, CPUMSELREGHID *pHiddenSel, RTGCPTR Addr)
88{
89 Assert(pHiddenSel || !CPUMAreHiddenSelRegsValid(pVM));
90
91 /*
92 * Deal with real & v86 mode first.
93 */
94 if ( CPUMIsGuestInRealMode(pVM)
95 || eflags.Bits.u1VM)
96 {
97 RTGCUINTPTR uFlat = (RTGCUINTPTR)Addr & 0xffff;
98
99 if (CPUMAreHiddenSelRegsValid(pVM))
100 uFlat += pHiddenSel->u32Base;
101 else
102 uFlat += ((RTGCUINTPTR)Sel << 4);
103 return (RTGCPTR)uFlat;
104 }
105
106 /** @todo when we're in 16 bits mode, we should cut off the address as well.. */
107 if (!CPUMAreHiddenSelRegsValid(pVM))
108 return selmToFlat(pVM, Sel, Addr);
109 return (RTGCPTR)(pHiddenSel->u32Base + (RTGCUINTPTR)Addr);
110}
111
112
113/**
114 * Converts a GC selector based address to a flat address.
115 *
116 * Some basic checking is done, but not all kinds yet.
117 *
118 * @returns VBox status
119 * @param pVM VM Handle.
120 * @param eflags Current eflags
121 * @param Sel Selector part.
122 * @param Addr Address part.
123 * @param pHiddenSel Hidden selector register (can be NULL)
124 * @param fFlags SELMTOFLAT_FLAGS_*
125 * GDT entires are valid.
126 * @param ppvGC Where to store the GC flat address.
127 * @param pcb Where to store the bytes from *ppvGC which can be accessed according to
128 * the selector. NULL is allowed.
129 */
130SELMDECL(int) SELMToFlatEx(PVM pVM, X86EFLAGS eflags, RTSEL Sel, RTGCPTR Addr, CPUMSELREGHID *pHiddenSel, unsigned fFlags, PRTGCPTR ppvGC, uint32_t *pcb)
131{
132 /*
133 * Deal with real & v86 mode first.
134 */
135 if ( CPUMIsGuestInRealMode(pVM)
136 || eflags.Bits.u1VM)
137 {
138 RTGCUINTPTR uFlat = (RTGCUINTPTR)Addr & 0xffff;
139 if (ppvGC)
140 {
141 if ( pHiddenSel
142 && CPUMAreHiddenSelRegsValid(pVM))
143 *ppvGC = (RTGCPTR)(pHiddenSel->u32Base + uFlat);
144 else
145 *ppvGC = (RTGCPTR)(((RTGCUINTPTR)Sel << 4) + uFlat);
146 }
147 if (pcb)
148 *pcb = 0x10000 - uFlat;
149 return VINF_SUCCESS;
150 }
151
152
153 uint32_t u32Limit;
154 RTGCPTR pvFlat;
155 uint32_t u1Present, u1DescType, u1Granularity, u4Type;
156
157 /** @todo when we're in 16 bits mode, we should cut off the address as well.. */
158 if ( pHiddenSel
159 && CPUMAreHiddenSelRegsValid(pVM))
160 {
161 u1Present = pHiddenSel->Attr.n.u1Present;
162 u1Granularity = pHiddenSel->Attr.n.u1Granularity;
163 u1DescType = pHiddenSel->Attr.n.u1DescType;
164 u4Type = pHiddenSel->Attr.n.u4Type;
165
166 u32Limit = pHiddenSel->u32Limit;
167 pvFlat = (RTGCPTR)(pHiddenSel->u32Base + (RTGCUINTPTR)Addr);
168 }
169 else
170 {
171 VBOXDESC Desc;
172
173 if (!(Sel & X86_SEL_LDT))
174 {
175 if ( !(fFlags & SELMTOFLAT_FLAGS_HYPER)
176 && (unsigned)(Sel & X86_SEL_MASK) >= pVM->selm.s.GuestGdtr.cbGdt)
177 return VERR_INVALID_SELECTOR;
178 Desc = pVM->selm.s.CTXSUFF(paGdt)[Sel >> X86_SEL_SHIFT];
179 }
180 else
181 {
182 if ((unsigned)(Sel & X86_SEL_MASK) >= pVM->selm.s.cbLdtLimit)
183 return VERR_INVALID_SELECTOR;
184
185 /** @todo handle LDT page(s) not present! */
186#ifdef IN_GC
187 PVBOXDESC paLDT = (PVBOXDESC)((char *)pVM->selm.s.GCPtrLdt + pVM->selm.s.offLdtHyper);
188#else
189 PVBOXDESC paLDT = (PVBOXDESC)((char *)pVM->selm.s.HCPtrLdt + pVM->selm.s.offLdtHyper);
190#endif
191 Desc = paLDT[Sel >> X86_SEL_SHIFT];
192 }
193
194 /* calc limit. */
195 u32Limit = Desc.Gen.u4LimitHigh << 16 | Desc.Gen.u16LimitLow;
196 if (Desc.Gen.u1Granularity)
197 u32Limit = (u32Limit << PAGE_SHIFT) | PAGE_OFFSET_MASK;
198
199 /* calc address assuming straight stuff. */
200 pvFlat = (RTGCPTR)( (RTGCUINTPTR)Addr
201 + ( (Desc.Gen.u8BaseHigh2 << 24)
202 | (Desc.Gen.u8BaseHigh1 << 16)
203 | Desc.Gen.u16BaseLow )
204 );
205
206 u1Present = Desc.Gen.u1Present;
207 u1Granularity = Desc.Gen.u1Granularity;
208 u1DescType = Desc.Gen.u1DescType;
209 u4Type = Desc.Gen.u4Type;
210 }
211
212 /*
213 * Check if present.
214 */
215 if (u1Present)
216 {
217 /*
218 * Type check.
219 */
220#define BOTH(a, b) ((a << 16) | b)
221 switch (BOTH(u1DescType, u4Type))
222 {
223
224 /** Read only selector type. */
225 case BOTH(1,X86_SEL_TYPE_RO):
226 case BOTH(1,X86_SEL_TYPE_RO_ACC):
227 case BOTH(1,X86_SEL_TYPE_RW):
228 case BOTH(1,X86_SEL_TYPE_RW_ACC):
229 case BOTH(1,X86_SEL_TYPE_EO):
230 case BOTH(1,X86_SEL_TYPE_EO_ACC):
231 case BOTH(1,X86_SEL_TYPE_ER):
232 case BOTH(1,X86_SEL_TYPE_ER_ACC):
233 if (!(fFlags & SELMTOFLAT_FLAGS_NO_PL))
234 {
235 /** @todo fix this mess */
236 }
237 /* check limit. */
238 if ((RTGCUINTPTR)Addr > u32Limit)
239 return VERR_OUT_OF_SELECTOR_BOUNDS;
240 /* ok */
241 if (ppvGC)
242 *ppvGC = pvFlat;
243 if (pcb)
244 *pcb = u32Limit - (uint32_t)Addr + 1;
245 return VINF_SUCCESS;
246
247 case BOTH(1,X86_SEL_TYPE_EO_CONF):
248 case BOTH(1,X86_SEL_TYPE_EO_CONF_ACC):
249 case BOTH(1,X86_SEL_TYPE_ER_CONF):
250 case BOTH(1,X86_SEL_TYPE_ER_CONF_ACC):
251 if (!(fFlags & SELMTOFLAT_FLAGS_NO_PL))
252 {
253 /** @todo fix this mess */
254 }
255 /* check limit. */
256 if ((RTGCUINTPTR)Addr > u32Limit)
257 return VERR_OUT_OF_SELECTOR_BOUNDS;
258 /* ok */
259 if (ppvGC)
260 *ppvGC = pvFlat;
261 if (pcb)
262 *pcb = u32Limit - (uint32_t)Addr + 1;
263 return VINF_SUCCESS;
264
265 case BOTH(1,X86_SEL_TYPE_RO_DOWN):
266 case BOTH(1,X86_SEL_TYPE_RO_DOWN_ACC):
267 case BOTH(1,X86_SEL_TYPE_RW_DOWN):
268 case BOTH(1,X86_SEL_TYPE_RW_DOWN_ACC):
269 if (!(fFlags & SELMTOFLAT_FLAGS_NO_PL))
270 {
271 /** @todo fix this mess */
272 }
273 /* check limit. */
274 if (!u1Granularity && (RTGCUINTPTR)Addr > (RTGCUINTPTR)0xffff)
275 return VERR_OUT_OF_SELECTOR_BOUNDS;
276 if ((RTGCUINTPTR)Addr <= u32Limit)
277 return VERR_OUT_OF_SELECTOR_BOUNDS;
278
279 /* ok */
280 if (ppvGC)
281 *ppvGC = pvFlat;
282 if (pcb)
283 *pcb = (RTGCUINTPTR)(u1Granularity ? 0xffffffff : 0xffff) - (RTGCUINTPTR)Addr + 1;
284 return VINF_SUCCESS;
285
286 case BOTH(0,X86_SEL_TYPE_SYS_286_TSS_AVAIL):
287 case BOTH(0,X86_SEL_TYPE_SYS_LDT):
288 case BOTH(0,X86_SEL_TYPE_SYS_286_TSS_BUSY):
289 case BOTH(0,X86_SEL_TYPE_SYS_286_CALL_GATE):
290 case BOTH(0,X86_SEL_TYPE_SYS_TASK_GATE):
291 case BOTH(0,X86_SEL_TYPE_SYS_286_INT_GATE):
292 case BOTH(0,X86_SEL_TYPE_SYS_286_TRAP_GATE):
293 case BOTH(0,X86_SEL_TYPE_SYS_386_TSS_AVAIL):
294 case BOTH(0,X86_SEL_TYPE_SYS_386_TSS_BUSY):
295 case BOTH(0,X86_SEL_TYPE_SYS_386_CALL_GATE):
296 case BOTH(0,X86_SEL_TYPE_SYS_386_INT_GATE):
297 case BOTH(0,X86_SEL_TYPE_SYS_386_TRAP_GATE):
298 if (!(fFlags & SELMTOFLAT_FLAGS_NO_PL))
299 {
300 /** @todo fix this mess */
301 }
302 /* check limit. */
303 if ((RTGCUINTPTR)Addr > u32Limit)
304 return VERR_OUT_OF_SELECTOR_BOUNDS;
305 /* ok */
306 if (ppvGC)
307 *ppvGC = pvFlat;
308 if (pcb)
309 *pcb = 0xffffffff - (RTGCUINTPTR)pvFlat + 1; /* Depends on the type.. fixme if we care. */
310 return VINF_SUCCESS;
311
312 default:
313 return VERR_INVALID_SELECTOR;
314
315 }
316#undef BOTH
317 }
318 return VERR_SELECTOR_NOT_PRESENT;
319}
320
321
322/**
323 * Validates and converts a GC selector based code address to a flat
324 * address when in real or v8086 mode.
325 *
326 * @returns VINF_SUCCESS.
327 * @param pVM VM Handle.
328 * @param SelCS Selector part.
329 * @param pHidCS The hidden CS register part. Optional.
330 * @param Addr Address part.
331 * @param ppvFlat Where to store the flat address.
332 */
333DECLINLINE(int) selmValidateAndConvertCSAddrRealMode(PVM pVM, RTSEL SelCS, PCPUMSELREGHID pHidCS, RTGCPTR Addr, PRTGCPTR ppvFlat)
334{
335 RTGCUINTPTR uFlat = (RTGCUINTPTR)Addr & 0xffff;
336 if (!pHidCS || !CPUMAreHiddenSelRegsValid(pVM))
337 uFlat += ((RTGCUINTPTR)SelCS << 4);
338 else
339 uFlat += pHidCS->u32Base;
340 *ppvFlat = (RTGCPTR)uFlat;
341 return VINF_SUCCESS;
342}
343
344
345/**
346 * Validates and converts a GC selector based code address to a flat
347 * address when in protected/long mode using the standard algorithm.
348 *
349 * @returns VBox status code.
350 * @param pVM VM Handle.
351 * @param SelCPL Current privilege level. Get this from SS - CS might be conforming!
352 * A full selector can be passed, we'll only use the RPL part.
353 * @param SelCS Selector part.
354 * @param Addr Address part.
355 * @param ppvFlat Where to store the flat address.
356 * @param pcBits Where to store the segment bitness (16/32/64). Optional.
357 */
358DECLINLINE(int) selmValidateAndConvertCSAddrStd(PVM pVM, RTSEL SelCPL, RTSEL SelCS, RTGCPTR Addr, PRTGCPTR ppvFlat, uint32_t *pcBits)
359{
360 Assert(!CPUMAreHiddenSelRegsValid(pVM));
361
362 /** @todo validate limit! */
363 VBOXDESC Desc;
364 if (!(SelCS & X86_SEL_LDT))
365 Desc = pVM->selm.s.CTXSUFF(paGdt)[SelCS >> X86_SEL_SHIFT];
366 else
367 {
368 /** @todo handle LDT page(s) not present! */
369#ifdef IN_GC
370 PVBOXDESC paLDT = (PVBOXDESC)((char *)pVM->selm.s.GCPtrLdt + pVM->selm.s.offLdtHyper);
371#else
372 PVBOXDESC paLDT = (PVBOXDESC)((char *)pVM->selm.s.HCPtrLdt + pVM->selm.s.offLdtHyper);
373#endif
374 Desc = paLDT[SelCS >> X86_SEL_SHIFT];
375 }
376
377 /*
378 * Check if present.
379 */
380 if (Desc.Gen.u1Present)
381 {
382 /*
383 * Type check.
384 */
385 if ( Desc.Gen.u1DescType == 1
386 && (Desc.Gen.u4Type & X86_SEL_TYPE_CODE))
387 {
388 /*
389 * Check level.
390 */
391 unsigned uLevel = RT_MAX(SelCPL & X86_SEL_RPL, SelCS & X86_SEL_RPL);
392 if ( !(Desc.Gen.u4Type & X86_SEL_TYPE_CONF)
393 ? uLevel <= Desc.Gen.u2Dpl
394 : uLevel >= Desc.Gen.u2Dpl /* hope I got this right now... */
395 )
396 {
397 /*
398 * Limit check.
399 */
400 uint32_t u32Limit = Desc.Gen.u4LimitHigh << 16 | Desc.Gen.u16LimitLow;
401 if (Desc.Gen.u1Granularity)
402 u32Limit = (u32Limit << PAGE_SHIFT) | PAGE_OFFSET_MASK;
403 if ((RTGCUINTPTR)Addr <= u32Limit)
404 {
405 *ppvFlat = (RTGCPTR)( (RTGCUINTPTR)Addr
406 + ( (Desc.Gen.u8BaseHigh2 << 24)
407 | (Desc.Gen.u8BaseHigh1 << 16)
408 | Desc.Gen.u16BaseLow)
409 );
410 if (pcBits)
411 *pcBits = Desc.Gen.u1DefBig ? 32 : 16; /** @todo GUEST64 */
412 return VINF_SUCCESS;
413 }
414 return VERR_OUT_OF_SELECTOR_BOUNDS;
415 }
416 return VERR_INVALID_RPL;
417 }
418 return VERR_NOT_CODE_SELECTOR;
419 }
420 return VERR_SELECTOR_NOT_PRESENT;
421}
422
423
424/**
425 * Validates and converts a GC selector based code address to a flat
426 * address when in protected/long mode using the standard algorithm.
427 *
428 * @returns VBox status code.
429 * @param pVM VM Handle.
430 * @param SelCPL Current privilege level. Get this from SS - CS might be conforming!
431 * A full selector can be passed, we'll only use the RPL part.
432 * @param SelCS Selector part.
433 * @param Addr Address part.
434 * @param ppvFlat Where to store the flat address.
435 * @param pcBits Where to store the segment bitness (16/32/64). Optional.
436 */
437DECLINLINE(int) selmValidateAndConvertCSAddrHidden(PVM pVM, RTSEL SelCPL, RTSEL SelCS, PCPUMSELREGHID pHidCS, RTGCPTR Addr, PRTGCPTR ppvFlat)
438{
439 /*
440 * Check if present.
441 */
442 if (pHidCS->Attr.n.u1Present)
443 {
444 /*
445 * Type check.
446 */
447 if ( pHidCS->Attr.n.u1DescType == 1
448 && (pHidCS->Attr.n.u4Type & X86_SEL_TYPE_CODE))
449 {
450 /*
451 * Check level.
452 */
453 unsigned uLevel = RT_MAX(SelCPL & X86_SEL_RPL, SelCS & X86_SEL_RPL);
454 if ( !(pHidCS->Attr.n.u4Type & X86_SEL_TYPE_CONF)
455 ? uLevel <= pHidCS->Attr.n.u2Dpl
456 : uLevel >= pHidCS->Attr.n.u2Dpl /* hope I got this right now... */
457 )
458 {
459 /*
460 * Limit check.
461 */
462 uint32_t u32Limit = pHidCS->u32Limit;
463 /** @todo correct with hidden limit value?? */
464 if (pHidCS->Attr.n.u1Granularity)
465 u32Limit = (u32Limit << PAGE_SHIFT) | PAGE_OFFSET_MASK;
466 if ((RTGCUINTPTR)Addr <= u32Limit)
467 {
468 *ppvFlat = (RTGCPTR)( (RTGCUINTPTR)Addr + pHidCS->u32Base );
469 return VINF_SUCCESS;
470 }
471 return VERR_OUT_OF_SELECTOR_BOUNDS;
472 }
473 return VERR_INVALID_RPL;
474 }
475 return VERR_NOT_CODE_SELECTOR;
476 }
477 return VERR_SELECTOR_NOT_PRESENT;
478}
479
480
481/**
482 * Validates and converts a GC selector based code address to a flat address.
483 *
484 * This is like SELMValidateAndConvertCSAddr + SELMIsSelector32Bit but with
485 * invalid hidden CS data. It's customized for dealing efficiently with CS
486 * at GC trap time.
487 *
488 * @returns VBox status code.
489 * @param pVM VM Handle.
490 * @param eflags Current eflags
491 * @param SelCPL Current privilege level. Get this from SS - CS might be conforming!
492 * A full selector can be passed, we'll only use the RPL part.
493 * @param SelCS Selector part.
494 * @param Addr Address part.
495 * @param ppvFlat Where to store the flat address.
496 * @param pcBits Where to store the 64-bit/32-bit/16-bit indicator.
497 */
498SELMDECL(int) SELMValidateAndConvertCSAddrGCTrap(PVM pVM, X86EFLAGS eflags, RTSEL SelCPL, RTSEL SelCS, RTGCPTR Addr, PRTGCPTR ppvFlat, uint32_t *pcBits)
499{
500 if ( CPUMIsGuestInRealMode(pVM)
501 || eflags.Bits.u1VM)
502 {
503 *pcBits = 16;
504 return selmValidateAndConvertCSAddrRealMode(pVM, SelCS, NULL, Addr, ppvFlat);
505 }
506 return selmValidateAndConvertCSAddrStd(pVM, SelCPL, SelCS, Addr, ppvFlat, pcBits);
507}
508
509
510/**
511 * Validates and converts a GC selector based code address to a flat address.
512 *
513 * @returns VBox status code.
514 * @param pVM VM Handle.
515 * @param eflags Current eflags
516 * @param SelCPL Current privilege level. Get this from SS - CS might be conforming!
517 * A full selector can be passed, we'll only use the RPL part.
518 * @param SelCS Selector part.
519 * @param pHiddenSel The hidden CS selector register.
520 * @param Addr Address part.
521 * @param ppvFlat Where to store the flat address.
522 */
523SELMDECL(int) SELMValidateAndConvertCSAddr(PVM pVM, X86EFLAGS eflags, RTSEL SelCPL, RTSEL SelCS, CPUMSELREGHID *pHiddenCSSel, RTGCPTR Addr, PRTGCPTR ppvFlat)
524{
525 if ( CPUMIsGuestInRealMode(pVM)
526 || eflags.Bits.u1VM)
527 return selmValidateAndConvertCSAddrRealMode(pVM, SelCS, pHiddenCSSel, Addr, ppvFlat);
528
529 /** @todo when we're in 16 bits mode, we should cut off the address as well? (like in selmValidateAndConvertCSAddrRealMode) */
530 if (!CPUMAreHiddenSelRegsValid(pVM))
531 return selmValidateAndConvertCSAddrStd(pVM, SelCPL, SelCS, Addr, ppvFlat, NULL);
532 return selmValidateAndConvertCSAddrHidden(pVM, SelCPL, SelCS, pHiddenCSSel, Addr, ppvFlat);
533}
534
535
536/**
537 * Checks if a selector is 32-bit or 16-bit.
538 *
539 * @returns True if it is 32-bit.
540 * @returns False if it is 16-bit.
541 * @param pVM VM Handle.
542 * @param Sel The selector.
543 */
544static bool selmIsSelector32Bit(PVM pVM, RTSEL Sel)
545{
546 Assert(!CPUMAreHiddenSelRegsValid(pVM));
547
548 /** @todo validate limit! */
549 VBOXDESC Desc;
550 if (!(Sel & X86_SEL_LDT))
551 Desc = pVM->selm.s.CTXSUFF(paGdt)[Sel >> X86_SEL_SHIFT];
552 else
553 {
554 /** @todo handle LDT page(s) not present! */
555 PVBOXDESC paLDT = (PVBOXDESC)((char *)pVM->selm.s.CTXMID(,PtrLdt) + pVM->selm.s.offLdtHyper);
556 Desc = paLDT[Sel >> X86_SEL_SHIFT];
557 }
558 return Desc.Gen.u1DefBig;
559}
560
561
562/**
563 * Checks if a selector is 32-bit or 16-bit.
564 *
565 * @returns True if it is 32-bit.
566 * @returns False if it is 16-bit.
567 * @param pVM VM Handle.
568 * @param eflags Current eflags register
569 * @param Sel The selector.
570 * @param pHiddenSel The hidden selector register.
571 */
572SELMDECL(bool) SELMIsSelector32Bit(PVM pVM, X86EFLAGS eflags, RTSEL Sel, CPUMSELREGHID *pHiddenSel)
573{
574 if (!CPUMAreHiddenSelRegsValid(pVM))
575 {
576 /*
577 * Deal with real & v86 mode first.
578 */
579 if ( CPUMIsGuestInRealMode(pVM)
580 || eflags.Bits.u1VM)
581 return false;
582
583 return selmIsSelector32Bit(pVM, Sel);
584 }
585 return pHiddenSel->Attr.n.u1DefBig;
586}
587
588
589/**
590 * Returns Hypervisor's Trap 08 (\#DF) selector.
591 *
592 * @returns Hypervisor's Trap 08 (\#DF) selector.
593 * @param pVM VM Handle.
594 */
595SELMDECL(RTSEL) SELMGetTrap8Selector(PVM pVM)
596{
597 return pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08];
598}
599
600
601/**
602 * Sets EIP of Hypervisor's Trap 08 (\#DF) TSS.
603 *
604 * @param pVM VM Handle.
605 * @param u32EIP EIP of Trap 08 handler.
606 */
607SELMDECL(void) SELMSetTrap8EIP(PVM pVM, uint32_t u32EIP)
608{
609 pVM->selm.s.TssTrap08.eip = u32EIP;
610}
611
612
613/**
614 * Sets ss:esp for ring1 in main Hypervisor's TSS.
615 *
616 * @param pVM VM Handle.
617 * @param ss Ring1 SS register value.
618 * @param esp Ring1 ESP register value.
619 */
620SELMDECL(void) SELMSetRing1Stack(PVM pVM, uint32_t ss, uint32_t esp)
621{
622 pVM->selm.s.Tss.ss1 = ss;
623 pVM->selm.s.Tss.esp1 = esp;
624}
625
626
627/**
628 * Gets ss:esp for ring1 in main Hypervisor's TSS.
629 *
630 * @returns VBox status code.
631 * @param pVM VM Handle.
632 * @param pSS Ring1 SS register value.
633 * @param pEsp Ring1 ESP register value.
634 */
635SELMDECL(int) SELMGetRing1Stack(PVM pVM, uint32_t *pSS, uint32_t *pEsp)
636{
637 if (pVM->selm.s.fSyncTSSRing0Stack)
638 {
639 GCPTRTYPE(uint8_t *) GCPtrTss = (GCPTRTYPE(uint8_t *))pVM->selm.s.GCPtrGuestTss;
640 int rc;
641 VBOXTSS tss;
642
643 Assert(pVM->selm.s.GCPtrGuestTss && pVM->selm.s.cbMonitoredGuestTss);
644
645#ifdef IN_GC
646 bool fTriedAlready = false;
647
648l_tryagain:
649 rc = MMGCRamRead(pVM, &tss.ss0, GCPtrTss + RT_OFFSETOF(VBOXTSS, ss0), sizeof(tss.ss0));
650 rc |= MMGCRamRead(pVM, &tss.esp0, GCPtrTss + RT_OFFSETOF(VBOXTSS, esp0), sizeof(tss.esp0));
651 #ifdef DEBUG
652 rc |= MMGCRamRead(pVM, &tss.offIoBitmap, GCPtrTss + RT_OFFSETOF(VBOXTSS, offIoBitmap), sizeof(tss.offIoBitmap));
653 #endif
654
655 if (VBOX_FAILURE(rc))
656 {
657 if (!fTriedAlready)
658 {
659 /* Shadow page might be out of sync. Sync and try again */
660 /** @todo might cross page boundary */
661 fTriedAlready = true;
662 rc = PGMPrefetchPage(pVM, GCPtrTss);
663 if (rc != VINF_SUCCESS)
664 return rc;
665 goto l_tryagain;
666 }
667 AssertMsgFailed(("Unable to read TSS structure at %08X\n", GCPtrTss));
668 return rc;
669 }
670
671#else /* !IN_GC */
672 /* Reading too much. Could be cheaper than two seperate calls though. */
673 rc = PGMPhysReadGCPtr(pVM, &tss, GCPtrTss, sizeof(VBOXTSS));
674 if (VBOX_FAILURE(rc))
675 {
676 AssertReleaseMsgFailed(("Unable to read TSS structure at %08X\n", GCPtrTss));
677 return rc;
678 }
679#endif /* !IN_GC */
680
681#ifdef LOG_ENABLED
682 uint32_t ssr0 = pVM->selm.s.Tss.ss1;
683 uint32_t espr0 = pVM->selm.s.Tss.esp1;
684 ssr0 &= ~1;
685
686 if (ssr0 != tss.ss0 || espr0 != tss.esp0)
687 Log(("SELMGetRing1Stack: Updating TSS ring 0 stack to %04X:%08X\n", tss.ss0, tss.esp0));
688
689 Log(("offIoBitmap=%#x\n", tss.offIoBitmap));
690#endif
691 /* Update our TSS structure for the guest's ring 1 stack */
692 SELMSetRing1Stack(pVM, tss.ss0 | 1, tss.esp0);
693 pVM->selm.s.fSyncTSSRing0Stack = false;
694 }
695
696 *pSS = pVM->selm.s.Tss.ss1;
697 *pEsp = pVM->selm.s.Tss.esp1;
698
699 return VINF_SUCCESS;
700}
701
702
703/**
704 * Returns Guest TSS pointer
705 *
706 * @param pVM VM Handle.
707 */
708SELMDECL(RTGCPTR) SELMGetGuestTSS(PVM pVM)
709{
710 return (RTGCPTR)pVM->selm.s.GCPtrGuestTss;
711}
712
713
714/**
715 * Validates a CS selector.
716 *
717 * @returns VBox status code.
718 * @param pSelInfo Pointer to the selector information for the CS selector.
719 * @param SelCPL The selector defining the CPL (SS).
720 */
721SELMDECL(int) SELMSelInfoValidateCS(PCSELMSELINFO pSelInfo, RTSEL SelCPL)
722{
723 /*
724 * Check if present.
725 */
726 if (pSelInfo->Raw.Gen.u1Present)
727 {
728 /*
729 * Type check.
730 */
731 if ( pSelInfo->Raw.Gen.u1DescType == 1
732 && (pSelInfo->Raw.Gen.u4Type & X86_SEL_TYPE_CODE))
733 {
734 /*
735 * Check level.
736 */
737 unsigned uLevel = RT_MAX(SelCPL & X86_SEL_RPL, pSelInfo->Sel & X86_SEL_RPL);
738 if ( !(pSelInfo->Raw.Gen.u4Type & X86_SEL_TYPE_CONF)
739 ? uLevel <= pSelInfo->Raw.Gen.u2Dpl
740 : uLevel >= pSelInfo->Raw.Gen.u2Dpl /* hope I got this right now... */
741 )
742 return VINF_SUCCESS;
743 return VERR_INVALID_RPL;
744 }
745 return VERR_NOT_CODE_SELECTOR;
746 }
747 return VERR_SELECTOR_NOT_PRESENT;
748}
749
750#ifndef IN_RING0
751/**
752 * Gets the hypervisor code selector (CS).
753 * @returns CS selector.
754 * @param pVM The VM handle.
755 */
756SELMDECL(RTSEL) SELMGetHyperCS(PVM pVM)
757{
758 return pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS];
759}
760
761
762/**
763 * Gets the 64-mode hypervisor code selector (CS64).
764 * @returns CS selector.
765 * @param pVM The VM handle.
766 */
767SELMDECL(RTSEL) SELMGetHyperCS64(PVM pVM)
768{
769 return pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS64];
770}
771
772
773/**
774 * Gets the hypervisor data selector (DS).
775 * @returns DS selector.
776 * @param pVM The VM handle.
777 */
778SELMDECL(RTSEL) SELMGetHyperDS(PVM pVM)
779{
780 return pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS];
781}
782
783
784/**
785 * Gets the hypervisor TSS selector.
786 * @returns TSS selector.
787 * @param pVM The VM handle.
788 */
789SELMDECL(RTSEL) SELMGetHyperTSS(PVM pVM)
790{
791 return pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS];
792}
793
794
795/**
796 * Gets the hypervisor TSS Trap 8 selector.
797 * @returns TSS Trap 8 selector.
798 * @param pVM The VM handle.
799 */
800SELMDECL(RTSEL) SELMGetHyperTSSTrap08(PVM pVM)
801{
802 return pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08];
803}
804
805/**
806 * Gets the address for the hypervisor GDT.
807 *
808 * @returns The GDT address.
809 * @param pVM The VM handle.
810 * @remark This is intended only for very special use, like in the world
811 * switchers. Don't exploit this API!
812 */
813SELMDECL(RTGCPTR) SELMGetHyperGDT(PVM pVM)
814{
815 /*
816 * Always convert this from the HC pointer since. We're can be
817 * called before the first relocation and have to work correctly
818 * without having dependencies on the relocation order.
819 */
820 return MMHyperHC2GC(pVM, pVM->selm.s.paGdtHC);
821}
822#endif /* IN_RING0 */
823
824/**
825 * Gets info about the current TSS.
826 *
827 * @returns VBox status code.
828 * @retval VINF_SUCCESS if we've got a TSS loaded.
829 * @retval VERR_SELM_NO_TSS if we haven't got a TSS (rather unlikely).
830 *
831 * @param pVM The VM handle.
832 * @param pGCPtrTss Where to store the TSS address.
833 * @param pcbTss Where to store the TSS size limit.
834 * @param pfCanHaveIOBitmap Where to store the can-have-I/O-bitmap indicator. (optional)
835 */
836SELMDECL(int) SELMGetTSSInfo(PVM pVM, PRTGCUINTPTR pGCPtrTss, PRTGCUINTPTR pcbTss, bool *pfCanHaveIOBitmap)
837{
838 if (!CPUMAreHiddenSelRegsValid(pVM))
839 {
840 /*
841 * Do we have a valid TSS?
842 */
843 if ( pVM->selm.s.GCSelTss == (RTSEL)~0
844 || !pVM->selm.s.fGuestTss32Bit)
845 return VERR_SELM_NO_TSS;
846
847 /*
848 * Fill in return values.
849 */
850 *pGCPtrTss = (RTGCUINTPTR)pVM->selm.s.GCPtrGuestTss;
851 *pcbTss = pVM->selm.s.cbGuestTss;
852 if (pfCanHaveIOBitmap)
853 *pfCanHaveIOBitmap = pVM->selm.s.fGuestTss32Bit;
854 }
855 else
856 {
857 CPUMSELREGHID *pHiddenTRReg;
858
859 pHiddenTRReg = CPUMGetGuestTRHid(pVM);
860
861 *pGCPtrTss = pHiddenTRReg->u32Base;
862 *pcbTss = pHiddenTRReg->u32Limit;
863
864 if (pfCanHaveIOBitmap)
865 *pfCanHaveIOBitmap = pHiddenTRReg->Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_AVAIL
866 || pHiddenTRReg->Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_BUSY;
867 }
868 return VINF_SUCCESS;
869}
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