xref: /openbmc/qemu/target/i386/hvf/x86_flags.c (revision 3539d84d)
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.1 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_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_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_lflags.result = (target_ulong)(int##size##_t)(lf_result); \
104     target_ulong delta_c = (env->hvf_lflags.auxbits ^ temp) & LF_MASK_CF; \
105     delta_c ^= (delta_c >> 1); \
106     env->hvf_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_lflags.auxbits &= ~(LF_MASK_PO | LF_MASK_CF);
121     env->hvf_lflags.auxbits |= (temp_po << LF_BIT_PO) | (new_cf << LF_BIT_CF);
122 }
123 
124 void SET_FLAGS_OSZAPC_SUB32(CPUX86State *env, uint32_t v1, uint32_t v2,
125                             uint32_t diff)
126 {
127     SET_FLAGS_OSZAPC_32(SUB_COUT_VEC(v1, v2, diff), diff);
128 }
129 
130 void SET_FLAGS_OSZAPC_SUB16(CPUX86State *env, uint16_t v1, uint16_t v2,
131                             uint16_t diff)
132 {
133     SET_FLAGS_OSZAPC_16(SUB_COUT_VEC(v1, v2, diff), diff);
134 }
135 
136 void SET_FLAGS_OSZAPC_SUB8(CPUX86State *env, uint8_t v1, uint8_t v2,
137                             uint8_t diff)
138 {
139     SET_FLAGS_OSZAPC_8(SUB_COUT_VEC(v1, v2, diff), diff);
140 }
141 
142 void SET_FLAGS_OSZAPC_ADD32(CPUX86State *env, uint32_t v1, uint32_t v2,
143                             uint32_t diff)
144 {
145     SET_FLAGS_OSZAPC_32(ADD_COUT_VEC(v1, v2, diff), diff);
146 }
147 
148 void SET_FLAGS_OSZAPC_ADD16(CPUX86State *env, uint16_t v1, uint16_t v2,
149                             uint16_t diff)
150 {
151     SET_FLAGS_OSZAPC_16(ADD_COUT_VEC(v1, v2, diff), diff);
152 }
153 
154 void SET_FLAGS_OSZAPC_ADD8(CPUX86State *env, uint8_t v1, uint8_t v2,
155                             uint8_t diff)
156 {
157     SET_FLAGS_OSZAPC_8(ADD_COUT_VEC(v1, v2, diff), diff);
158 }
159 
160 void SET_FLAGS_OSZAP_SUB32(CPUX86State *env, uint32_t v1, uint32_t v2,
161                             uint32_t diff)
162 {
163     SET_FLAGS_OSZAP_32(SUB_COUT_VEC(v1, v2, diff), diff);
164 }
165 
166 void SET_FLAGS_OSZAP_SUB16(CPUX86State *env, uint16_t v1, uint16_t v2,
167                             uint16_t diff)
168 {
169     SET_FLAGS_OSZAP_16(SUB_COUT_VEC(v1, v2, diff), diff);
170 }
171 
172 void SET_FLAGS_OSZAP_SUB8(CPUX86State *env, uint8_t v1, uint8_t v2,
173                             uint8_t diff)
174 {
175     SET_FLAGS_OSZAP_8(SUB_COUT_VEC(v1, v2, diff), diff);
176 }
177 
178 void SET_FLAGS_OSZAP_ADD32(CPUX86State *env, uint32_t v1, uint32_t v2,
179                             uint32_t diff)
180 {
181     SET_FLAGS_OSZAP_32(ADD_COUT_VEC(v1, v2, diff), diff);
182 }
183 
184 void SET_FLAGS_OSZAP_ADD16(CPUX86State *env, uint16_t v1, uint16_t v2,
185                             uint16_t diff)
186 {
187     SET_FLAGS_OSZAP_16(ADD_COUT_VEC(v1, v2, diff), diff);
188 }
189 
190 void SET_FLAGS_OSZAP_ADD8(CPUX86State *env, uint8_t v1, uint8_t v2,
191                             uint8_t diff)
192 {
193     SET_FLAGS_OSZAP_8(ADD_COUT_VEC(v1, v2, diff), diff);
194 }
195 
196 
197 void SET_FLAGS_OSZAPC_LOGIC32(CPUX86State *env, uint32_t v1, uint32_t v2,
198                               uint32_t diff)
199 {
200     SET_FLAGS_OSZAPC_32(0, diff);
201 }
202 
203 void SET_FLAGS_OSZAPC_LOGIC16(CPUX86State *env, uint16_t v1, uint16_t v2,
204                               uint16_t diff)
205 {
206     SET_FLAGS_OSZAPC_16(0, diff);
207 }
208 
209 void SET_FLAGS_OSZAPC_LOGIC8(CPUX86State *env, uint8_t v1, uint8_t v2,
210                              uint8_t diff)
211 {
212     SET_FLAGS_OSZAPC_8(0, diff);
213 }
214 
215 bool get_PF(CPUX86State *env)
216 {
217     uint32_t temp = (255 & env->hvf_lflags.result);
218     temp = temp ^ (255 & (env->hvf_lflags.auxbits >> LF_BIT_PDB));
219     temp = (temp ^ (temp >> 4)) & 0x0F;
220     return (0x9669U >> temp) & 1;
221 }
222 
223 void set_PF(CPUX86State *env, bool val)
224 {
225     uint32_t temp = (255 & env->hvf_lflags.result) ^ (!val);
226     env->hvf_lflags.auxbits &= ~(LF_MASK_PDB);
227     env->hvf_lflags.auxbits |= (temp << LF_BIT_PDB);
228 }
229 
230 bool get_OF(CPUX86State *env)
231 {
232     return ((env->hvf_lflags.auxbits + (1U << LF_BIT_PO)) >> LF_BIT_CF) & 1;
233 }
234 
235 bool get_CF(CPUX86State *env)
236 {
237     return (env->hvf_lflags.auxbits >> LF_BIT_CF) & 1;
238 }
239 
240 void set_OF(CPUX86State *env, bool val)
241 {
242     bool old_cf = get_CF(env);
243     SET_FLAGS_OxxxxC(env, val, old_cf);
244 }
245 
246 void set_CF(CPUX86State *env, bool val)
247 {
248     bool old_of = get_OF(env);
249     SET_FLAGS_OxxxxC(env, old_of, val);
250 }
251 
252 bool get_AF(CPUX86State *env)
253 {
254     return (env->hvf_lflags.auxbits >> LF_BIT_AF) & 1;
255 }
256 
257 void set_AF(CPUX86State *env, bool val)
258 {
259     env->hvf_lflags.auxbits &= ~(LF_MASK_AF);
260     env->hvf_lflags.auxbits |= val << LF_BIT_AF;
261 }
262 
263 bool get_ZF(CPUX86State *env)
264 {
265     return !env->hvf_lflags.result;
266 }
267 
268 void set_ZF(CPUX86State *env, bool val)
269 {
270     if (val) {
271         env->hvf_lflags.auxbits ^=
272          (((env->hvf_lflags.result >> LF_SIGN_BIT) & 1) << LF_BIT_SD);
273         /* merge the parity bits into the Parity Delta Byte */
274         uint32_t temp_pdb = (255 & env->hvf_lflags.result);
275         env->hvf_lflags.auxbits ^= (temp_pdb << LF_BIT_PDB);
276         /* now zero the .result value */
277         env->hvf_lflags.result = 0;
278     } else {
279         env->hvf_lflags.result |= (1 << 8);
280     }
281 }
282 
283 bool get_SF(CPUX86State *env)
284 {
285     return ((env->hvf_lflags.result >> LF_SIGN_BIT) ^
286             (env->hvf_lflags.auxbits >> LF_BIT_SD)) & 1;
287 }
288 
289 void set_SF(CPUX86State *env, bool val)
290 {
291     bool temp_sf = get_SF(env);
292     env->hvf_lflags.auxbits ^= (temp_sf ^ val) << LF_BIT_SD;
293 }
294 
295 void lflags_to_rflags(CPUX86State *env)
296 {
297     env->eflags |= get_CF(env) ? CC_C : 0;
298     env->eflags |= get_PF(env) ? CC_P : 0;
299     env->eflags |= get_AF(env) ? CC_A : 0;
300     env->eflags |= get_ZF(env) ? CC_Z : 0;
301     env->eflags |= get_SF(env) ? CC_S : 0;
302     env->eflags |= get_OF(env) ? CC_O : 0;
303 }
304 
305 void rflags_to_lflags(CPUX86State *env)
306 {
307     env->hvf_lflags.auxbits = env->hvf_lflags.result = 0;
308     set_OF(env, env->eflags & CC_O);
309     set_SF(env, env->eflags & CC_S);
310     set_ZF(env, env->eflags & CC_Z);
311     set_AF(env, env->eflags & CC_A);
312     set_PF(env, env->eflags & CC_P);
313     set_CF(env, env->eflags & CC_C);
314 }
315