1 | /* $Id: bs3-cmn-RegCtxConvertToRingX.c 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * BS3Kit - Bs3RegCtxConvertToRingX
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "bs3kit-template-header.h"
|
---|
42 |
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Transforms a real mode segment into a protected mode selector.
|
---|
46 | *
|
---|
47 | * @returns Protected mode selector.
|
---|
48 | * @param uSeg The real mode segment.
|
---|
49 | * @param bRing The target ring.
|
---|
50 | */
|
---|
51 | static uint16_t bs3RegCtxConvertRealSegToRingX(uint16_t uSeg, uint8_t bRing)
|
---|
52 | {
|
---|
53 | uint16_t uSel;
|
---|
54 | if ( uSeg == 0
|
---|
55 | || uSeg == BS3_SEL_R0_SS16)
|
---|
56 | uSel = BS3_SEL_R0_SS16 + ((uint16_t)bRing << BS3_SEL_RING_SHIFT);
|
---|
57 | else if ( uSeg == (BS3_ADDR_BS3TEXT16 >> 4)
|
---|
58 | || uSeg == BS3_SEL_R0_CS16)
|
---|
59 | uSel = BS3_SEL_R0_CS16 + ((uint16_t)bRing << BS3_SEL_RING_SHIFT);
|
---|
60 | else if ( uSeg == (BS3_ADDR_BS3DATA16 >> 4)
|
---|
61 | || uSeg == BS3_SEL_R0_DS16)
|
---|
62 | uSel = BS3_SEL_R0_DS16 + ((uint16_t)bRing << BS3_SEL_RING_SHIFT);
|
---|
63 | else if (uSeg == (BS3_ADDR_BS3SYSTEM16 >> 4))
|
---|
64 | uSel = BS3_SEL_SYSTEM16;
|
---|
65 | else if (!(uSeg & 0xfff))
|
---|
66 | uSel = (uSeg >> (12 - X86_SEL_SHIFT)) + BS3_SEL_TILED;
|
---|
67 | else if (uSeg == BS3_SEL_R0_DS16)
|
---|
68 | uSel = (uSeg >> (12 - X86_SEL_SHIFT)) + BS3_SEL_TILED;
|
---|
69 | else
|
---|
70 | {
|
---|
71 | Bs3Printf("uSeg=%#x\n", uSeg);
|
---|
72 | BS3_ASSERT(0);
|
---|
73 | return 0;
|
---|
74 | }
|
---|
75 | uSel |= bRing;
|
---|
76 | return uSel;
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Transforms a protected mode selector to a different ring.
|
---|
82 | *
|
---|
83 | * @returns Adjusted protected mode selector.
|
---|
84 | * @param uSel The current selector value.
|
---|
85 | * @param bRing The target ring.
|
---|
86 | * @param iReg Register index.
|
---|
87 | */
|
---|
88 | static uint16_t bs3RegCtxConvertProtSelToRingX(uint16_t uSel, uint8_t bRing, uint8_t iReg)
|
---|
89 | {
|
---|
90 | if ( uSel > X86_SEL_RPL
|
---|
91 | && !(uSel & X86_SEL_LDT) )
|
---|
92 | {
|
---|
93 | if (uSel >= BS3_SEL_R0_FIRST && uSel < BS3_SEL_R0_FIRST + (5 << BS3_SEL_RING_SHIFT))
|
---|
94 | {
|
---|
95 | /* Convert BS3_SEL_R*_XXX to the target ring. */
|
---|
96 | uSel &= BS3_SEL_RING_SUB_MASK;
|
---|
97 | uSel |= bRing;
|
---|
98 | uSel += BS3_SEL_R0_FIRST;
|
---|
99 | uSel += (uint16_t)bRing << BS3_SEL_RING_SHIFT;
|
---|
100 | }
|
---|
101 | else
|
---|
102 | {
|
---|
103 | /* Convert TEXT16 and DATA16 to BS3_SEL_R*_XXX. */
|
---|
104 | uint16_t const uSelRaw = uSel & X86_SEL_MASK_OFF_RPL;
|
---|
105 | if (uSelRaw == BS3_SEL_TEXT16)
|
---|
106 | uSel = (BS3_SEL_R0_CS16 | bRing) + ((uint16_t)bRing << BS3_SEL_RING_SHIFT);
|
---|
107 | else if (uSelRaw == BS3_SEL_DATA16)
|
---|
108 | uSel = (BS3_SEL_R0_DS16 | bRing) + ((uint16_t)bRing << BS3_SEL_RING_SHIFT);
|
---|
109 | /* CS and SS must have CPL == DPL. So, convert to standard selectors as we're
|
---|
110 | usually here because Bs3SwitchToRing0 was called to get out of a test situation. */
|
---|
111 | else if (iReg == X86_SREG_CS || iReg == X86_SREG_SS)
|
---|
112 | {
|
---|
113 | if ( Bs3Gdt[uSel >> X86_SEL_SHIFT].Gen.u1Long
|
---|
114 | && BS3_MODE_IS_64BIT_SYS(g_bBs3CurrentMode) )
|
---|
115 | uSel = iReg == X86_SREG_CS ? BS3_SEL_R0_CS64 : BS3_SEL_R0_DS64;
|
---|
116 | else
|
---|
117 | {
|
---|
118 | uint32_t uFlat = Bs3SelFar32ToFlat32(0, uSel);
|
---|
119 | bool fDefBig = Bs3Gdt[uSel >> X86_SEL_SHIFT].Gen.u1DefBig;
|
---|
120 | if (!fDefBig && uFlat == BS3_ADDR_BS3TEXT16 && iReg == X86_SREG_CS)
|
---|
121 | uSel = BS3_SEL_R0_CS16;
|
---|
122 | else if (!fDefBig && uFlat == 0 && iReg == X86_SREG_SS)
|
---|
123 | uSel = BS3_SEL_R0_SS16;
|
---|
124 | else if (fDefBig && uFlat == 0)
|
---|
125 | uSel = iReg == X86_SREG_CS ? BS3_SEL_R0_CS32 : BS3_SEL_R0_SS32;
|
---|
126 | else
|
---|
127 | {
|
---|
128 | Bs3Printf("uSel=%#x iReg=%d\n", uSel, iReg);
|
---|
129 | BS3_ASSERT(0);
|
---|
130 | return uSel;
|
---|
131 | }
|
---|
132 | uSel |= bRing;
|
---|
133 | uSel += (uint16_t)bRing << BS3_SEL_RING_SHIFT;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | /* Adjust the RPL on tiled and MMIO selectors. */
|
---|
137 | else if ( uSelRaw == BS3_SEL_VMMDEV_MMIO16
|
---|
138 | || uSelRaw >= BS3_SEL_TILED)
|
---|
139 | uSel = uSelRaw | bRing;
|
---|
140 | }
|
---|
141 | }
|
---|
142 | return uSel;
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Transforms a register context to a different ring.
|
---|
148 | *
|
---|
149 | * @param pRegCtx The register context.
|
---|
150 | * @param bRing The target ring (0..3).
|
---|
151 | *
|
---|
152 | * @note Do _NOT_ call this for creating real mode or v8086 contexts, because
|
---|
153 | * it will always output a protected mode context!
|
---|
154 | */
|
---|
155 | #undef Bs3RegCtxConvertToRingX
|
---|
156 | BS3_CMN_DEF(void, Bs3RegCtxConvertToRingX,(PBS3REGCTX pRegCtx, uint8_t bRing))
|
---|
157 | {
|
---|
158 | if ( (pRegCtx->rflags.u32 & X86_EFL_VM)
|
---|
159 | || pRegCtx->bMode == BS3_MODE_RM)
|
---|
160 | {
|
---|
161 | pRegCtx->rflags.u32 &= ~X86_EFL_VM;
|
---|
162 | pRegCtx->bMode &= ~BS3_MODE_CODE_MASK;
|
---|
163 | pRegCtx->bMode |= BS3_MODE_CODE_16;
|
---|
164 | pRegCtx->cs = bs3RegCtxConvertRealSegToRingX(pRegCtx->cs, bRing);
|
---|
165 | pRegCtx->ss = bs3RegCtxConvertRealSegToRingX(pRegCtx->ss, bRing);
|
---|
166 | pRegCtx->ds = bs3RegCtxConvertRealSegToRingX(pRegCtx->ds, bRing);
|
---|
167 | pRegCtx->es = bs3RegCtxConvertRealSegToRingX(pRegCtx->es, bRing);
|
---|
168 | pRegCtx->fs = bs3RegCtxConvertRealSegToRingX(pRegCtx->fs, bRing);
|
---|
169 | pRegCtx->gs = bs3RegCtxConvertRealSegToRingX(pRegCtx->gs, bRing);
|
---|
170 | }
|
---|
171 | else
|
---|
172 | {
|
---|
173 | pRegCtx->cs = bs3RegCtxConvertProtSelToRingX(pRegCtx->cs, bRing, X86_SREG_CS);
|
---|
174 | pRegCtx->ss = bs3RegCtxConvertProtSelToRingX(pRegCtx->ss, bRing, X86_SREG_SS);
|
---|
175 | pRegCtx->ds = bs3RegCtxConvertProtSelToRingX(pRegCtx->ds, bRing, X86_SREG_DS);
|
---|
176 | pRegCtx->es = bs3RegCtxConvertProtSelToRingX(pRegCtx->es, bRing, X86_SREG_ES);
|
---|
177 | pRegCtx->fs = bs3RegCtxConvertProtSelToRingX(pRegCtx->fs, bRing, X86_SREG_FS);
|
---|
178 | pRegCtx->gs = bs3RegCtxConvertProtSelToRingX(pRegCtx->gs, bRing, X86_SREG_GS);
|
---|
179 | }
|
---|
180 | pRegCtx->bCpl = bRing;
|
---|
181 | }
|
---|
182 |
|
---|