1 /////////////////////////////////////////////////////////////////////////
2 // $Id: segment_ctrl.cc 13729 2019-12-27 13:02:30Z sshwarts $
3 /////////////////////////////////////////////////////////////////////////
4 //
5 //  Copyright (C) 2001-2018  The Bochs Project
6 //
7 //  This library is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU Lesser General Public
9 //  License as published by the Free Software Foundation; either
10 //  version 2 of the License, or (at your option) any later version.
11 //
12 //  This library is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 //  Lesser General Public License for more details.
16 //
17 //  You should have received a copy of the GNU Lesser General Public
18 //  License along with this library; if not, write to the Free Software
19 //  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA B 02110-1301 USA
20 //
21 /////////////////////////////////////////////////////////////////////////
22 
23 #define NEED_CPU_REG_SHORTCUTS 1
24 #include "bochs.h"
25 #include "cpu.h"
26 #define LOG_THIS BX_CPU_THIS_PTR
27 
28 const char *segname[] = { "ES", "CS", "SS", "DS", "FS", "GS" };
29 
load_segw(bxInstruction_c * i,unsigned seg)30 void BX_CPP_AttrRegparmN(2) BX_CPU_C::load_segw(bxInstruction_c *i, unsigned seg)
31 {
32   bx_address eaddr = BX_CPU_RESOLVE_ADDR(i);
33 
34   Bit16u segsel = read_virtual_word(i->seg(), (eaddr + 2) & i->asize_mask());
35   Bit16u reg_16 = read_virtual_word(i->seg(),  eaddr);
36 
37   load_seg_reg(&BX_CPU_THIS_PTR sregs[seg], segsel);
38 
39   BX_WRITE_16BIT_REG(i->dst(), reg_16);
40 }
41 
load_segd(bxInstruction_c * i,unsigned seg)42 void BX_CPP_AttrRegparmN(2) BX_CPU_C::load_segd(bxInstruction_c *i, unsigned seg)
43 {
44   bx_address eaddr = BX_CPU_RESOLVE_ADDR(i);
45 
46   Bit16u segsel = read_virtual_word(i->seg(), (eaddr + 4) & i->asize_mask());
47   Bit32u reg_32 = read_virtual_dword(i->seg(), eaddr);
48 
49   load_seg_reg(&BX_CPU_THIS_PTR sregs[seg], segsel);
50 
51   BX_WRITE_32BIT_REGZ(i->dst(), reg_32);
52 }
53 
54 #if BX_SUPPORT_X86_64
load_segq(bxInstruction_c * i,unsigned seg)55 void BX_CPP_AttrRegparmN(2) BX_CPU_C::load_segq(bxInstruction_c *i, unsigned seg)
56 {
57   bx_address eaddr = BX_CPU_RESOLVE_ADDR_64(i);
58 
59   Bit16u segsel = read_linear_word(i->seg(), get_laddr64(i->seg(), (eaddr + 8) & i->asize_mask()));
60   Bit64u reg_64 = read_linear_qword(i->seg(), get_laddr64(i->seg(), eaddr));
61 
62   load_seg_reg(&BX_CPU_THIS_PTR sregs[seg], segsel);
63 
64   BX_WRITE_64BIT_REG(i->dst(), reg_64);
65 }
66 #endif
67 
68 // LES/LDS can't be called from long64 mode
LES_GwMp(bxInstruction_c * i)69 void BX_CPP_AttrRegparmN(1) BX_CPU_C::LES_GwMp(bxInstruction_c *i)
70 {
71   load_segw(i, BX_SEG_REG_ES);
72 
73   BX_NEXT_INSTR(i);
74 }
75 
76 // LES/LDS can't be called from long64 mode
LES_GdMp(bxInstruction_c * i)77 void BX_CPP_AttrRegparmN(1) BX_CPU_C::LES_GdMp(bxInstruction_c *i)
78 {
79   load_segd(i, BX_SEG_REG_ES);
80 
81   BX_NEXT_INSTR(i);
82 }
83 
84 // LES/LDS can't be called from long64 mode
LDS_GwMp(bxInstruction_c * i)85 void BX_CPP_AttrRegparmN(1) BX_CPU_C::LDS_GwMp(bxInstruction_c *i)
86 {
87   load_segw(i, BX_SEG_REG_DS);
88 
89   BX_NEXT_INSTR(i);
90 }
91 
92 // LES/LDS can't be called from long64 mode
LDS_GdMp(bxInstruction_c * i)93 void BX_CPP_AttrRegparmN(1) BX_CPU_C::LDS_GdMp(bxInstruction_c *i)
94 {
95   load_segd(i, BX_SEG_REG_DS);
96 
97   BX_NEXT_INSTR(i);
98 }
99 
LFS_GwMp(bxInstruction_c * i)100 void BX_CPP_AttrRegparmN(1) BX_CPU_C::LFS_GwMp(bxInstruction_c *i)
101 {
102   load_segw(i, BX_SEG_REG_FS);
103 
104   BX_NEXT_INSTR(i);
105 }
106 
LFS_GdMp(bxInstruction_c * i)107 void BX_CPP_AttrRegparmN(1) BX_CPU_C::LFS_GdMp(bxInstruction_c *i)
108 {
109   load_segd(i, BX_SEG_REG_FS);
110 
111   BX_NEXT_INSTR(i);
112 }
113 
114 #if BX_SUPPORT_X86_64
LFS_GqMp(bxInstruction_c * i)115 void BX_CPP_AttrRegparmN(1) BX_CPU_C::LFS_GqMp(bxInstruction_c *i)
116 {
117   load_segq(i, BX_SEG_REG_FS);
118 
119   BX_NEXT_INSTR(i);
120 }
121 #endif
122 
LGS_GwMp(bxInstruction_c * i)123 void BX_CPP_AttrRegparmN(1) BX_CPU_C::LGS_GwMp(bxInstruction_c *i)
124 {
125   load_segw(i, BX_SEG_REG_GS);
126 
127   BX_NEXT_INSTR(i);
128 }
129 
LGS_GdMp(bxInstruction_c * i)130 void BX_CPP_AttrRegparmN(1) BX_CPU_C::LGS_GdMp(bxInstruction_c *i)
131 {
132   load_segd(i, BX_SEG_REG_GS);
133 
134   BX_NEXT_INSTR(i);
135 }
136 
137 #if BX_SUPPORT_X86_64
LGS_GqMp(bxInstruction_c * i)138 void BX_CPP_AttrRegparmN(1) BX_CPU_C::LGS_GqMp(bxInstruction_c *i)
139 {
140   load_segq(i, BX_SEG_REG_GS);
141 
142   BX_NEXT_INSTR(i);
143 }
144 #endif
145 
LSS_GwMp(bxInstruction_c * i)146 void BX_CPP_AttrRegparmN(1) BX_CPU_C::LSS_GwMp(bxInstruction_c *i)
147 {
148   load_segw(i, BX_SEG_REG_SS);
149 
150   BX_NEXT_INSTR(i);
151 }
152 
LSS_GdMp(bxInstruction_c * i)153 void BX_CPP_AttrRegparmN(1) BX_CPU_C::LSS_GdMp(bxInstruction_c *i)
154 {
155   load_segd(i, BX_SEG_REG_SS);
156 
157   BX_NEXT_INSTR(i);
158 }
159 
160 #if BX_SUPPORT_X86_64
LSS_GqMp(bxInstruction_c * i)161 void BX_CPP_AttrRegparmN(1) BX_CPU_C::LSS_GqMp(bxInstruction_c *i)
162 {
163   load_segq(i, BX_SEG_REG_SS);
164 
165   BX_NEXT_INSTR(i);
166 }
167 #endif
168