1 | /* $Id: bs3-cmn-RegCtxConvertToRingX.c 60527 2016-04-18 09:11:04Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * BS3Kit - Bs3RegCtxConvertToRingX
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2016 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 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /*********************************************************************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *********************************************************************************************************************************/
|
---|
30 | #include "bs3kit-template-header.h"
|
---|
31 |
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Transforms a real mode segment into a protected mode selector.
|
---|
35 | *
|
---|
36 | * @returns Protected mode selector.
|
---|
37 | * @param uSeg The real mode segment.
|
---|
38 | * @param bRing The target ring.
|
---|
39 | */
|
---|
40 | static uint16_t bs3RegCtxConvertRealSegToRingX(uint16_t uSeg, uint8_t bRing)
|
---|
41 | {
|
---|
42 | uint16_t uSel;
|
---|
43 | if ( uSeg == 0
|
---|
44 | || uSeg == BS3_SEL_R0_SS16)
|
---|
45 | uSel = BS3_SEL_R0_SS16 + ((uint16_t)bRing << BS3_SEL_RING_SHIFT);
|
---|
46 | else if ( uSeg == (BS3_ADDR_BS3TEXT16 >> 4)
|
---|
47 | || uSeg == BS3_SEL_R0_CS16)
|
---|
48 | uSel = BS3_SEL_R0_CS16 + ((uint16_t)bRing << BS3_SEL_RING_SHIFT);
|
---|
49 | else if ( uSeg == (BS3_ADDR_BS3DATA16 >> 4)
|
---|
50 | || uSeg == BS3_SEL_R0_DS16)
|
---|
51 | uSel = BS3_SEL_R0_DS16 + ((uint16_t)bRing << BS3_SEL_RING_SHIFT);
|
---|
52 | else if (uSeg == (BS3_ADDR_BS3SYSTEM16 >> 4))
|
---|
53 | uSel = BS3_SEL_SYSTEM16;
|
---|
54 | else if (!(uSeg & 0xfff))
|
---|
55 | uSel = (uSeg >> (12 - X86_SEL_SHIFT)) + BS3_SEL_TILED;
|
---|
56 | else if (uSeg == BS3_SEL_R0_DS16)
|
---|
57 | uSel = (uSeg >> (12 - X86_SEL_SHIFT)) + BS3_SEL_TILED;
|
---|
58 | else
|
---|
59 | {
|
---|
60 | Bs3Printf("uSeg=%#x\n", uSeg);
|
---|
61 | BS3_ASSERT(0);
|
---|
62 | return 0;
|
---|
63 | }
|
---|
64 | uSel |= bRing;
|
---|
65 | return uSel;
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Transforms a protected mode selector to a different ring.
|
---|
71 | *
|
---|
72 | * @returns Adjusted protected mode selector.
|
---|
73 | * @param uSeg The current selector value.
|
---|
74 | * @param bRing The target ring.
|
---|
75 | * @param iReg Register index.
|
---|
76 | */
|
---|
77 | static uint16_t bs3RegCtxConvertProtSelToRingX(uint16_t uSel, uint8_t bRing, uint8_t iReg)
|
---|
78 | {
|
---|
79 | if ( uSel > X86_SEL_RPL
|
---|
80 | && !(uSel & X86_SEL_LDT) )
|
---|
81 | {
|
---|
82 | if (uSel >= BS3_SEL_R0_FIRST && uSel < BS3_SEL_R0_FIRST + (5 << BS3_SEL_RING_SHIFT))
|
---|
83 | {
|
---|
84 | /* Convert BS3_SEL_R*_XXX to the target ring. */
|
---|
85 | uSel &= BS3_SEL_RING_SUB_MASK;
|
---|
86 | uSel |= bRing;
|
---|
87 | uSel += BS3_SEL_R0_FIRST;
|
---|
88 | uSel += (uint16_t)bRing << BS3_SEL_RING_SHIFT;
|
---|
89 | }
|
---|
90 | else
|
---|
91 | {
|
---|
92 | /* Convert TEXT16 and DATA16 to BS3_SEL_R*_XXX. */
|
---|
93 | uint16_t const uSelRaw = uSel & X86_SEL_MASK_OFF_RPL;
|
---|
94 | if (uSelRaw == BS3_SEL_TEXT16)
|
---|
95 | uSel = (BS3_SEL_R0_CS16 | bRing) + ((uint16_t)bRing << BS3_SEL_RING_SHIFT);
|
---|
96 | else if (uSelRaw == BS3_SEL_DATA16)
|
---|
97 | uSel = (BS3_SEL_R0_DS16 | bRing) + ((uint16_t)bRing << BS3_SEL_RING_SHIFT);
|
---|
98 | /* CS and SS must have CPL == DPL. So, convert to standard selectors as we're
|
---|
99 | usually here because Bs3SwitchToRing0 was called to get out of a test situation. */
|
---|
100 | else if (iReg == X86_SREG_CS || iReg == X86_SREG_SS)
|
---|
101 | {
|
---|
102 | if ( Bs3Gdt[uSel >> X86_SEL_SHIFT].Gen.u1Long
|
---|
103 | && BS3_MODE_IS_64BIT_SYS(g_bBs3CurrentMode) )
|
---|
104 | uSel = iReg == X86_SREG_CS ? BS3_SEL_R0_CS64 : BS3_SEL_R0_DS64;
|
---|
105 | else
|
---|
106 | {
|
---|
107 | uint32_t uFlat = Bs3SelFar32ToFlat32(0, uSel);
|
---|
108 | bool fDefBig = Bs3Gdt[uSel >> X86_SEL_SHIFT].Gen.u1DefBig;
|
---|
109 | if (!fDefBig && uFlat == BS3_ADDR_BS3TEXT16 && iReg == X86_SREG_CS)
|
---|
110 | uSel = BS3_SEL_R0_CS16;
|
---|
111 | else if (!fDefBig && uFlat == 0 && iReg == X86_SREG_SS)
|
---|
112 | uSel = BS3_SEL_R0_SS16;
|
---|
113 | else if (fDefBig && uFlat == 0)
|
---|
114 | uSel = iReg == X86_SREG_CS ? BS3_SEL_R0_CS32 : BS3_SEL_R0_SS32;
|
---|
115 | else
|
---|
116 | {
|
---|
117 | Bs3Printf("uSel=%#x iReg=%d\n", uSel, iReg);
|
---|
118 | BS3_ASSERT(0);
|
---|
119 | return uSel;
|
---|
120 | }
|
---|
121 | uSel |= bRing;
|
---|
122 | uSel += (uint16_t)bRing << BS3_SEL_RING_SHIFT;
|
---|
123 | }
|
---|
124 | }
|
---|
125 | /* Adjust the RPL on tiled and MMIO selectors. */
|
---|
126 | else if ( uSelRaw == BS3_SEL_VMMDEV_MMIO16
|
---|
127 | || uSelRaw >= BS3_SEL_TILED)
|
---|
128 | uSel = uSelRaw | bRing;
|
---|
129 | }
|
---|
130 | }
|
---|
131 | return uSel;
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Transforms a register context to a different ring.
|
---|
137 | *
|
---|
138 | * @param pRegCtx The register context.
|
---|
139 | * @param bRing The target ring (0..3).
|
---|
140 | */
|
---|
141 | #undef Bs3RegCtxConvertToRingX
|
---|
142 | BS3_CMN_DEF(void, Bs3RegCtxConvertToRingX,(PBS3REGCTX pRegCtx, uint8_t bRing))
|
---|
143 | {
|
---|
144 | if ( (pRegCtx->rflags.u32 & X86_EFL_VM)
|
---|
145 | || pRegCtx->bMode == BS3_MODE_RM)
|
---|
146 | {
|
---|
147 | pRegCtx->rflags.u32 &= ~X86_EFL_VM;
|
---|
148 | pRegCtx->bMode &= ~BS3_MODE_CODE_MASK;
|
---|
149 | pRegCtx->bMode |= BS3_MODE_CODE_16;
|
---|
150 | pRegCtx->cs = bs3RegCtxConvertRealSegToRingX(pRegCtx->cs, bRing);
|
---|
151 | pRegCtx->ss = bs3RegCtxConvertRealSegToRingX(pRegCtx->ss, bRing);
|
---|
152 | pRegCtx->ds = bs3RegCtxConvertRealSegToRingX(pRegCtx->ds, bRing);
|
---|
153 | pRegCtx->es = bs3RegCtxConvertRealSegToRingX(pRegCtx->es, bRing);
|
---|
154 | pRegCtx->fs = bs3RegCtxConvertRealSegToRingX(pRegCtx->fs, bRing);
|
---|
155 | pRegCtx->gs = bs3RegCtxConvertRealSegToRingX(pRegCtx->gs, bRing);
|
---|
156 | }
|
---|
157 | else
|
---|
158 | {
|
---|
159 | pRegCtx->cs = bs3RegCtxConvertProtSelToRingX(pRegCtx->cs, bRing, X86_SREG_CS);
|
---|
160 | pRegCtx->ss = bs3RegCtxConvertProtSelToRingX(pRegCtx->ss, bRing, X86_SREG_SS);
|
---|
161 | pRegCtx->ds = bs3RegCtxConvertProtSelToRingX(pRegCtx->ds, bRing, X86_SREG_DS);
|
---|
162 | pRegCtx->es = bs3RegCtxConvertProtSelToRingX(pRegCtx->es, bRing, X86_SREG_ES);
|
---|
163 | pRegCtx->fs = bs3RegCtxConvertProtSelToRingX(pRegCtx->fs, bRing, X86_SREG_FS);
|
---|
164 | pRegCtx->gs = bs3RegCtxConvertProtSelToRingX(pRegCtx->gs, bRing, X86_SREG_GS);
|
---|
165 | }
|
---|
166 | pRegCtx->bCpl = bRing;
|
---|
167 | }
|
---|
168 |
|
---|