VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-RegCtxConvertToRingX.c@ 60112

Last change on this file since 60112 was 60009, checked in by vboxsync, 9 years ago

bs3kit: Updates

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 KB
Line 
1/* $Id: bs3-cmn-RegCtxConvertToRingX.c 60009 2016-03-13 16:34:32Z 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 */
40static 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 */
76static uint16_t bs3RegCtxConvertProtSelToRingX(uint16_t uSel, uint8_t bRing)
77{
78 if ( uSel > X86_SEL_RPL
79 && !(uSel & X86_SEL_LDT) )
80 {
81 if (uSel >= BS3_SEL_R0_FIRST)
82 {
83 /* Convert BS3_SEL_R*_XXX to the target ring. */
84 uSel &= BS3_SEL_RING_SUB_MASK;
85 uSel |= bRing;
86 uSel += BS3_SEL_R0_FIRST;
87 uSel |= (uint16_t)bRing << BS3_SEL_RING_SHIFT;
88 }
89 else
90 {
91 /* Convert TEXT16 and DATA16 to BS3_SEL_R*_XXX. */
92 uint16_t const uSelRaw = uSel & X86_SEL_MASK_OFF_RPL;
93 if (uSelRaw == BS3_SEL_TEXT16)
94 uSel = (BS3_SEL_R0_CS16 | bRing) + ((uint16_t)bRing << BS3_SEL_RING_SHIFT);
95 else if (uSelRaw == BS3_SEL_DATA16)
96 uSel = (BS3_SEL_R0_DS16 | bRing) + ((uint16_t)bRing << BS3_SEL_RING_SHIFT);
97 /* Adjust the RPL on tiled and MMIO selectors. */
98 else if ( uSelRaw == BS3_SEL_VMMDEV_MMIO16
99 || uSelRaw >= BS3_SEL_TILED)
100 uSel = uSelRaw | bRing;
101 }
102 }
103 return uSel;
104}
105
106
107/**
108 * Transforms a register context to a different ring.
109 *
110 * @param pRegCtx The register context.
111 * @param bRing The target ring (0..3).
112 */
113BS3_DECL(void) Bs3RegCtxConvertToRingX(PBS3REGCTX pRegCtx, uint8_t bRing)
114{
115 if ( (pRegCtx->rflags.u32 & X86_EFL_VM)
116 || pRegCtx->bMode == BS3_MODE_RM)
117 {
118 pRegCtx->rflags.u32 &= ~X86_EFL_VM;
119 pRegCtx->bMode &= ~BS3_MODE_CODE_MASK;
120 pRegCtx->bMode |= BS3_MODE_CODE_16;
121 pRegCtx->cs = bs3RegCtxConvertRealSegToRingX(pRegCtx->cs, bRing);
122 pRegCtx->ss = bs3RegCtxConvertRealSegToRingX(pRegCtx->ss, bRing);
123 pRegCtx->ds = bs3RegCtxConvertRealSegToRingX(pRegCtx->ds, bRing);
124 pRegCtx->es = bs3RegCtxConvertRealSegToRingX(pRegCtx->es, bRing);
125 pRegCtx->fs = bs3RegCtxConvertRealSegToRingX(pRegCtx->fs, bRing);
126 pRegCtx->gs = bs3RegCtxConvertRealSegToRingX(pRegCtx->gs, bRing);
127 }
128 else
129 {
130 pRegCtx->cs = bs3RegCtxConvertProtSelToRingX(pRegCtx->cs, bRing);
131 pRegCtx->ss = bs3RegCtxConvertProtSelToRingX(pRegCtx->ss, bRing);
132 pRegCtx->ds = bs3RegCtxConvertProtSelToRingX(pRegCtx->ds, bRing);
133 pRegCtx->es = bs3RegCtxConvertProtSelToRingX(pRegCtx->es, bRing);
134 pRegCtx->fs = bs3RegCtxConvertProtSelToRingX(pRegCtx->fs, bRing);
135 pRegCtx->gs = bs3RegCtxConvertProtSelToRingX(pRegCtx->gs, bRing);
136 }
137 pRegCtx->bCpl = bRing;
138}
139
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