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