xref: /openbmc/qemu/target/i386/hvf/x86_flags.c (revision dc5bd18f)
1 /////////////////////////////////////////////////////////////////////////
2 //
3 //  Copyright (C) 2001-2012  The Bochs Project
4 //  Copyright (C) 2017 Google Inc.
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2 of the License, or (at your option) any later version.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA B 02110-1301 USA
19 /////////////////////////////////////////////////////////////////////////
20 /*
21  * flags functions
22  */
23 
24 #include "qemu/osdep.h"
25 
26 #include "qemu-common.h"
27 #include "panic.h"
28 #include "cpu.h"
29 #include "x86_flags.h"
30 #include "x86.h"
31 
32 
33 /* this is basically bocsh code */
34 
35 #define LF_SIGN_BIT     31
36 
37 #define LF_BIT_SD      (0)          /* lazy Sign Flag Delta            */
38 #define LF_BIT_AF      (3)          /* lazy Adjust flag                */
39 #define LF_BIT_PDB     (8)          /* lazy Parity Delta Byte (8 bits) */
40 #define LF_BIT_CF      (31)         /* lazy Carry Flag                 */
41 #define LF_BIT_PO      (30)         /* lazy Partial Overflow = CF ^ OF */
42 
43 #define LF_MASK_SD     (0x01 << LF_BIT_SD)
44 #define LF_MASK_AF     (0x01 << LF_BIT_AF)
45 #define LF_MASK_PDB    (0xFF << LF_BIT_PDB)
46 #define LF_MASK_CF     (0x01 << LF_BIT_CF)
47 #define LF_MASK_PO     (0x01 << LF_BIT_PO)
48 
49 #define ADD_COUT_VEC(op1, op2, result) \
50    (((op1) & (op2)) | (((op1) | (op2)) & (~(result))))
51 
52 #define SUB_COUT_VEC(op1, op2, result) \
53    (((~(op1)) & (op2)) | (((~(op1)) ^ (op2)) & (result)))
54 
55 #define GET_ADD_OVERFLOW(op1, op2, result, mask) \
56    ((((op1) ^ (result)) & ((op2) ^ (result))) & (mask))
57 
58 /* ******************* */
59 /* OSZAPC */
60 /* ******************* */
61 
62 /* size, carries, result */
63 #define SET_FLAGS_OSZAPC_SIZE(size, lf_carries, lf_result) { \
64     target_ulong temp = ((lf_carries) & (LF_MASK_AF)) | \
65     (((lf_carries) >> (size - 2)) << LF_BIT_PO); \
66     env->hvf_emul->lflags.result = (target_ulong)(int##size##_t)(lf_result); \
67     if ((size) == 32) { \
68         temp = ((lf_carries) & ~(LF_MASK_PDB | LF_MASK_SD)); \
69     } else if ((size) == 16) { \
70         temp = ((lf_carries) & (LF_MASK_AF)) | ((lf_carries) << 16); \
71     } else if ((size) == 8)  { \
72         temp = ((lf_carries) & (LF_MASK_AF)) | ((lf_carries) << 24); \
73     } else { \
74         VM_PANIC("unimplemented");  \
75     } \
76     env->hvf_emul->lflags.auxbits = (target_ulong)(uint32_t)temp; \
77 }
78 
79 /* carries, result */
80 #define SET_FLAGS_OSZAPC_8(carries, result) \
81     SET_FLAGS_OSZAPC_SIZE(8, carries, result)
82 #define SET_FLAGS_OSZAPC_16(carries, result) \
83     SET_FLAGS_OSZAPC_SIZE(16, carries, result)
84 #define SET_FLAGS_OSZAPC_32(carries, result) \
85     SET_FLAGS_OSZAPC_SIZE(32, carries, result)
86 
87 /* ******************* */
88 /* OSZAP */
89 /* ******************* */
90 /* size, carries, result */
91 #define SET_FLAGS_OSZAP_SIZE(size, lf_carries, lf_result) { \
92     target_ulong temp = ((lf_carries) & (LF_MASK_AF)) | \
93     (((lf_carries) >> (size - 2)) << LF_BIT_PO); \
94     if ((size) == 32) { \
95         temp = ((lf_carries) & ~(LF_MASK_PDB | LF_MASK_SD)); \
96     } else if ((size) == 16) { \
97         temp = ((lf_carries) & (LF_MASK_AF)) | ((lf_carries) << 16); \
98     } else if ((size) == 8) { \
99         temp = ((lf_carries) & (LF_MASK_AF)) | ((lf_carries) << 24); \
100     } else { \
101         VM_PANIC("unimplemented");      \
102     } \
103     env->hvf_emul->lflags.result = (target_ulong)(int##size##_t)(lf_result); \
104     target_ulong delta_c = (env->hvf_emul->lflags.auxbits ^ temp) & LF_MASK_CF; \
105     delta_c ^= (delta_c >> 1); \
106     env->hvf_emul->lflags.auxbits = (target_ulong)(uint32_t)(temp ^ delta_c); \
107 }
108 
109 /* carries, result */
110 #define SET_FLAGS_OSZAP_8(carries, result) \
111     SET_FLAGS_OSZAP_SIZE(8, carries, result)
112 #define SET_FLAGS_OSZAP_16(carries, result) \
113     SET_FLAGS_OSZAP_SIZE(16, carries, result)
114 #define SET_FLAGS_OSZAP_32(carries, result) \
115     SET_FLAGS_OSZAP_SIZE(32, carries, result)
116 
117 void SET_FLAGS_OxxxxC(CPUX86State *env, uint32_t new_of, uint32_t new_cf)
118 {
119     uint32_t temp_po = new_of ^ new_cf;
120     env->hvf_emul->lflags.auxbits &= ~(LF_MASK_PO | LF_MASK_CF);
121     env->hvf_emul->lflags.auxbits |= (temp_po << LF_BIT_PO) |
122                                      (new_cf << LF_BIT_CF);
123 }
124 
125 void SET_FLAGS_OSZAPC_SUB32(CPUX86State *env, uint32_t v1, uint32_t v2,
126                             uint32_t diff)
127 {
128     SET_FLAGS_OSZAPC_32(SUB_COUT_VEC(v1, v2, diff), diff);
129 }
130 
131 void SET_FLAGS_OSZAPC_SUB16(CPUX86State *env, uint16_t v1, uint16_t v2,
132                             uint16_t diff)
133 {
134     SET_FLAGS_OSZAPC_16(SUB_COUT_VEC(v1, v2, diff), diff);
135 }
136 
137 void SET_FLAGS_OSZAPC_SUB8(CPUX86State *env, uint8_t v1, uint8_t v2,
138                             uint8_t diff)
139 {
140     SET_FLAGS_OSZAPC_8(SUB_COUT_VEC(v1, v2, diff), diff);
141 }
142 
143 void SET_FLAGS_OSZAPC_ADD32(CPUX86State *env, uint32_t v1, uint32_t v2,
144                             uint32_t diff)
145 {
146     SET_FLAGS_OSZAPC_32(ADD_COUT_VEC(v1, v2, diff), diff);
147 }
148 
149 void SET_FLAGS_OSZAPC_ADD16(CPUX86State *env, uint16_t v1, uint16_t v2,
150                             uint16_t diff)
151 {
152     SET_FLAGS_OSZAPC_16(ADD_COUT_VEC(v1, v2, diff), diff);
153 }
154 
155 void SET_FLAGS_OSZAPC_ADD8(CPUX86State *env, uint8_t v1, uint8_t v2,
156                             uint8_t diff)
157 {
158     SET_FLAGS_OSZAPC_8(ADD_COUT_VEC(v1, v2, diff), diff);
159 }
160 
161 void SET_FLAGS_OSZAP_SUB32(CPUX86State *env, uint32_t v1, uint32_t v2,
162                             uint32_t diff)
163 {
164     SET_FLAGS_OSZAP_32(SUB_COUT_VEC(v1, v2, diff), diff);
165 }
166 
167 void SET_FLAGS_OSZAP_SUB16(CPUX86State *env, uint16_t v1, uint16_t v2,
168                             uint16_t diff)
169 {
170     SET_FLAGS_OSZAP_16(SUB_COUT_VEC(v1, v2, diff), diff);
171 }
172 
173 void SET_FLAGS_OSZAP_SUB8(CPUX86State *env, uint8_t v1, uint8_t v2,
174                             uint8_t diff)
175 {
176     SET_FLAGS_OSZAP_8(SUB_COUT_VEC(v1, v2, diff), diff);
177 }
178 
179 void SET_FLAGS_OSZAP_ADD32(CPUX86State *env, uint32_t v1, uint32_t v2,
180                             uint32_t diff)
181 {
182     SET_FLAGS_OSZAP_32(ADD_COUT_VEC(v1, v2, diff), diff);
183 }
184 
185 void SET_FLAGS_OSZAP_ADD16(CPUX86State *env, uint16_t v1, uint16_t v2,
186                             uint16_t diff)
187 {
188     SET_FLAGS_OSZAP_16(ADD_COUT_VEC(v1, v2, diff), diff);
189 }
190 
191 void SET_FLAGS_OSZAP_ADD8(CPUX86State *env, uint8_t v1, uint8_t v2,
192                             uint8_t diff)
193 {
194     SET_FLAGS_OSZAP_8(ADD_COUT_VEC(v1, v2, diff), diff);
195 }
196 
197 
198 void SET_FLAGS_OSZAPC_LOGIC32(CPUX86State *env, uint32_t v1, uint32_t v2,
199                               uint32_t diff)
200 {
201     SET_FLAGS_OSZAPC_32(0, diff);
202 }
203 
204 void SET_FLAGS_OSZAPC_LOGIC16(CPUX86State *env, uint16_t v1, uint16_t v2,
205                               uint16_t diff)
206 {
207     SET_FLAGS_OSZAPC_16(0, diff);
208 }
209 
210 void SET_FLAGS_OSZAPC_LOGIC8(CPUX86State *env, uint8_t v1, uint8_t v2,
211                              uint8_t diff)
212 {
213     SET_FLAGS_OSZAPC_8(0, diff);
214 }
215 
216 bool get_PF(CPUX86State *env)
217 {
218     uint32_t temp = (255 & env->hvf_emul->lflags.result);
219     temp = temp ^ (255 & (env->hvf_emul->lflags.auxbits >> LF_BIT_PDB));
220     temp = (temp ^ (temp >> 4)) & 0x0F;
221     return (0x9669U >> temp) & 1;
222 }
223 
224 void set_PF(CPUX86State *env, bool val)
225 {
226     uint32_t temp = (255 & env->hvf_emul->lflags.result) ^ (!val);
227     env->hvf_emul->lflags.auxbits &= ~(LF_MASK_PDB);
228     env->hvf_emul->lflags.auxbits |= (temp << LF_BIT_PDB);
229 }
230 
231 bool get_OF(CPUX86State *env)
232 {
233     return ((env->hvf_emul->lflags.auxbits + (1U << LF_BIT_PO)) >> LF_BIT_CF) & 1;
234 }
235 
236 bool get_CF(CPUX86State *env)
237 {
238     return (env->hvf_emul->lflags.auxbits >> LF_BIT_CF) & 1;
239 }
240 
241 void set_OF(CPUX86State *env, bool val)
242 {
243     bool old_cf = get_CF(env);
244     SET_FLAGS_OxxxxC(env, val, old_cf);
245 }
246 
247 void set_CF(CPUX86State *env, bool val)
248 {
249     bool old_of = get_OF(env);
250     SET_FLAGS_OxxxxC(env, old_of, val);
251 }
252 
253 bool get_AF(CPUX86State *env)
254 {
255     return (env->hvf_emul->lflags.auxbits >> LF_BIT_AF) & 1;
256 }
257 
258 void set_AF(CPUX86State *env, bool val)
259 {
260     env->hvf_emul->lflags.auxbits &= ~(LF_MASK_AF);
261     env->hvf_emul->lflags.auxbits |= val << LF_BIT_AF;
262 }
263 
264 bool get_ZF(CPUX86State *env)
265 {
266     return !env->hvf_emul->lflags.result;
267 }
268 
269 void set_ZF(CPUX86State *env, bool val)
270 {
271     if (val) {
272         env->hvf_emul->lflags.auxbits ^=
273          (((env->hvf_emul->lflags.result >> LF_SIGN_BIT) & 1) << LF_BIT_SD);
274         /* merge the parity bits into the Parity Delta Byte */
275         uint32_t temp_pdb = (255 & env->hvf_emul->lflags.result);
276         env->hvf_emul->lflags.auxbits ^= (temp_pdb << LF_BIT_PDB);
277         /* now zero the .result value */
278         env->hvf_emul->lflags.result = 0;
279     } else {
280         env->hvf_emul->lflags.result |= (1 << 8);
281     }
282 }
283 
284 bool get_SF(CPUX86State *env)
285 {
286     return ((env->hvf_emul->lflags.result >> LF_SIGN_BIT) ^
287             (env->hvf_emul->lflags.auxbits >> LF_BIT_SD)) & 1;
288 }
289 
290 void set_SF(CPUX86State *env, bool val)
291 {
292     bool temp_sf = get_SF(env);
293     env->hvf_emul->lflags.auxbits ^= (temp_sf ^ val) << LF_BIT_SD;
294 }
295 
296 void lflags_to_rflags(CPUX86State *env)
297 {
298     env->hvf_emul->rflags.cf = get_CF(env);
299     env->hvf_emul->rflags.pf = get_PF(env);
300     env->hvf_emul->rflags.af = get_AF(env);
301     env->hvf_emul->rflags.zf = get_ZF(env);
302     env->hvf_emul->rflags.sf = get_SF(env);
303     env->hvf_emul->rflags.of = get_OF(env);
304 }
305 
306 void rflags_to_lflags(CPUX86State *env)
307 {
308     env->hvf_emul->lflags.auxbits = env->hvf_emul->lflags.result = 0;
309     set_OF(env, env->hvf_emul->rflags.of);
310     set_SF(env, env->hvf_emul->rflags.sf);
311     set_ZF(env, env->hvf_emul->rflags.zf);
312     set_AF(env, env->hvf_emul->rflags.af);
313     set_PF(env, env->hvf_emul->rflags.pf);
314     set_CF(env, env->hvf_emul->rflags.cf);
315 }
316