1 | /* $Id: bs3-cmn-RegCtxConvertToRingX.c 59975 2016-03-09 23:00:02Z 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 | uSel = BS3_SEL_R0_SS16 + ((uint16_t)bRing << BS3_SEL_RING_SHIFT);
|
---|
45 | else if (uSeg == (BS3_ADDR_BS3TEXT16 >> 4))
|
---|
46 | uSel = BS3_SEL_R0_CS16 + ((uint16_t)bRing << BS3_SEL_RING_SHIFT);
|
---|
47 | else if (uSeg == (BS3_ADDR_BS3DATA16 >> 4))
|
---|
48 | uSel = BS3_SEL_R0_DS16 + ((uint16_t)bRing << BS3_SEL_RING_SHIFT);
|
---|
49 | else if (uSeg == (BS3_ADDR_BS3SYSTEM16 >> 4))
|
---|
50 | uSel = BS3_SEL_SYSTEM16;
|
---|
51 | else if (!(uSeg & 0xfff))
|
---|
52 | uSel = (uSeg >> (12 - X86_SEL_SHIFT)) + BS3_SEL_TILED;
|
---|
53 | else
|
---|
54 | {
|
---|
55 | BS3_ASSERT(0);
|
---|
56 | return 0;
|
---|
57 | }
|
---|
58 | uSel |= bRing;
|
---|
59 | return uSel;
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Transforms a protected mode selector to a different ring.
|
---|
65 | *
|
---|
66 | * @returns Adjusted protected mode selector.
|
---|
67 | * @param uSeg The current selector value.
|
---|
68 | * @param bRing The target ring.
|
---|
69 | */
|
---|
70 | static uint16_t bs3RegCtxConvertProtSelToRingX(uint16_t uSel, uint8_t bRing)
|
---|
71 | {
|
---|
72 | if ( uSel > X86_SEL_RPL
|
---|
73 | && !(uSel & X86_SEL_LDT) )
|
---|
74 | {
|
---|
75 | if (uSel >= BS3_SEL_R0_FIRST)
|
---|
76 | {
|
---|
77 | /* Convert BS3_SEL_R*_XXX to the target ring. */
|
---|
78 | uSel &= BS3_SEL_RING_SUB_MASK;
|
---|
79 | uSel |= bRing;
|
---|
80 | uSel += BS3_SEL_R0_FIRST;
|
---|
81 | uSel |= (uint16_t)bRing << BS3_SEL_RING_SHIFT;
|
---|
82 | }
|
---|
83 | else
|
---|
84 | {
|
---|
85 | /* Convert TEXT16 and DATA16 to BS3_SEL_R*_XXX. */
|
---|
86 | uint16_t const uSelRaw = uSel & X86_SEL_MASK_OFF_RPL;
|
---|
87 | if (uSelRaw == BS3_SEL_TEXT16)
|
---|
88 | uSel = (BS3_SEL_R0_CS16 | bRing) + ((uint16_t)bRing << BS3_SEL_RING_SHIFT);
|
---|
89 | else if (uSelRaw == BS3_SEL_DATA16)
|
---|
90 | uSel = (BS3_SEL_R0_DS16 | bRing) + ((uint16_t)bRing << BS3_SEL_RING_SHIFT);
|
---|
91 | /* Adjust the RPL on tiled and MMIO selectors. */
|
---|
92 | else if ( uSelRaw == BS3_SEL_VMMDEV_MMIO16
|
---|
93 | || uSelRaw >= BS3_SEL_TILED)
|
---|
94 | uSel = uSelRaw | bRing;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | return uSel;
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Transforms a register context to a different ring.
|
---|
103 | *
|
---|
104 | * @param pRegCtx The register context.
|
---|
105 | * @param bRing The target ring (0..3).
|
---|
106 | */
|
---|
107 | BS3_DECL(void) Bs3RegCtxConvertToRingX(PBS3REGCTX pRegCtx, uint8_t bRing)
|
---|
108 | {
|
---|
109 | if ( (pRegCtx->rflags.u32 & X86_EFL_VM)
|
---|
110 | || pRegCtx->bMode == BS3_MODE_RM)
|
---|
111 | {
|
---|
112 | pRegCtx->cs = bs3RegCtxConvertRealSegToRingX(pRegCtx->cs, bRing);
|
---|
113 | pRegCtx->ss = bs3RegCtxConvertRealSegToRingX(pRegCtx->ss, bRing);
|
---|
114 | pRegCtx->ds = bs3RegCtxConvertRealSegToRingX(pRegCtx->ds, bRing);
|
---|
115 | pRegCtx->es = bs3RegCtxConvertRealSegToRingX(pRegCtx->es, bRing);
|
---|
116 | pRegCtx->fs = bs3RegCtxConvertRealSegToRingX(pRegCtx->fs, bRing);
|
---|
117 | pRegCtx->gs = bs3RegCtxConvertRealSegToRingX(pRegCtx->gs, bRing);
|
---|
118 | }
|
---|
119 | else
|
---|
120 | {
|
---|
121 | pRegCtx->cs = bs3RegCtxConvertProtSelToRingX(pRegCtx->cs, bRing);
|
---|
122 | pRegCtx->ss = bs3RegCtxConvertProtSelToRingX(pRegCtx->ss, bRing);
|
---|
123 | pRegCtx->ds = bs3RegCtxConvertProtSelToRingX(pRegCtx->ds, bRing);
|
---|
124 | pRegCtx->es = bs3RegCtxConvertProtSelToRingX(pRegCtx->es, bRing);
|
---|
125 | pRegCtx->fs = bs3RegCtxConvertProtSelToRingX(pRegCtx->fs, bRing);
|
---|
126 | pRegCtx->gs = bs3RegCtxConvertProtSelToRingX(pRegCtx->gs, bRing);
|
---|
127 | }
|
---|
128 | pRegCtx->bCpl = bRing;
|
---|
129 | }
|
---|
130 |
|
---|