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 "panic.h" 27 #include "cpu.h" 28 #include "x86_flags.h" 29 #include "x86.h" 30 31 32 /* 33 * The algorithms here are similar to those in Bochs. After an ALU 34 * operation, CC_DST can be used to compute ZF, SF and PF, whereas 35 * CC_SRC is used to compute AF, CF and OF. In reality, SF and PF are the 36 * XOR of the value computed from CC_DST and the value found in bits 7 and 2 37 * of CC_SRC; this way the same logic can be used to compute the flags 38 * both before and after an ALU operation. 39 * 40 * Compared to the TCG CC_OP codes, this avoids conditionals when converting 41 * to and from the RFLAGS representation. 42 */ 43 44 #define LF_SIGN_BIT (TARGET_LONG_BITS - 1) 45 46 #define LF_BIT_PD (2) /* lazy Parity Delta, same bit as PF */ 47 #define LF_BIT_AF (3) /* lazy Adjust flag */ 48 #define LF_BIT_SD (7) /* lazy Sign Flag Delta, same bit as SF */ 49 #define LF_BIT_CF (TARGET_LONG_BITS - 1) /* lazy Carry Flag */ 50 #define LF_BIT_PO (TARGET_LONG_BITS - 2) /* lazy Partial Overflow = CF ^ OF */ 51 52 #define LF_MASK_PD ((target_ulong)0x01 << LF_BIT_PD) 53 #define LF_MASK_AF ((target_ulong)0x01 << LF_BIT_AF) 54 #define LF_MASK_SD ((target_ulong)0x01 << LF_BIT_SD) 55 #define LF_MASK_CF ((target_ulong)0x01 << LF_BIT_CF) 56 #define LF_MASK_PO ((target_ulong)0x01 << LF_BIT_PO) 57 58 /* ******************* */ 59 /* OSZAPC */ 60 /* ******************* */ 61 62 /* use carries to fill in AF, PO and CF, while ensuring PD and SD are clear. 63 * for full-word operations just clear PD and SD; for smaller operand 64 * sizes only keep AF in the low byte and shift the carries left to 65 * place PO and CF in the top two bits. 66 */ 67 #define SET_FLAGS_OSZAPC_SIZE(size, lf_carries, lf_result) { \ 68 env->cc_dst = (target_ulong)(int##size##_t)(lf_result); \ 69 target_ulong temp = (lf_carries); \ 70 if ((size) == TARGET_LONG_BITS) { \ 71 temp = temp & ~(LF_MASK_PD | LF_MASK_SD); \ 72 } else { \ 73 temp = (temp & LF_MASK_AF) | (temp << (TARGET_LONG_BITS - (size))); \ 74 } \ 75 env->cc_src = temp; \ 76 } 77 78 /* carries, result */ 79 #define SET_FLAGS_OSZAPC_8(carries, result) \ 80 SET_FLAGS_OSZAPC_SIZE(8, carries, result) 81 #define SET_FLAGS_OSZAPC_16(carries, result) \ 82 SET_FLAGS_OSZAPC_SIZE(16, carries, result) 83 #define SET_FLAGS_OSZAPC_32(carries, result) \ 84 SET_FLAGS_OSZAPC_SIZE(32, carries, result) 85 86 /* ******************* */ 87 /* OSZAP */ 88 /* ******************* */ 89 /* same as setting OSZAPC, but preserve CF and flip PO if the old value of CF 90 * did not match the high bit of lf_carries. */ 91 #define SET_FLAGS_OSZAP_SIZE(size, lf_carries, lf_result) { \ 92 env->cc_dst = (target_ulong)(int##size##_t)(lf_result); \ 93 target_ulong temp = (lf_carries); \ 94 if ((size) == TARGET_LONG_BITS) { \ 95 temp = (temp & ~(LF_MASK_PD | LF_MASK_SD)); \ 96 } else { \ 97 temp = (temp & LF_MASK_AF) | (temp << (TARGET_LONG_BITS - (size))); \ 98 } \ 99 target_ulong cf_changed = ((target_long)(env->cc_src ^ temp)) < 0; \ 100 env->cc_src = temp ^ (cf_changed * (LF_MASK_PO | LF_MASK_CF)); \ 101 } 102 103 /* carries, result */ 104 #define SET_FLAGS_OSZAP_8(carries, result) \ 105 SET_FLAGS_OSZAP_SIZE(8, carries, result) 106 #define SET_FLAGS_OSZAP_16(carries, result) \ 107 SET_FLAGS_OSZAP_SIZE(16, carries, result) 108 #define SET_FLAGS_OSZAP_32(carries, result) \ 109 SET_FLAGS_OSZAP_SIZE(32, carries, result) 110 111 void SET_FLAGS_OxxxxC(CPUX86State *env, bool new_of, bool new_cf) 112 { 113 env->cc_src &= ~(LF_MASK_PO | LF_MASK_CF); 114 env->cc_src |= (-(target_ulong)new_cf << LF_BIT_PO); 115 env->cc_src ^= ((target_ulong)new_of << LF_BIT_PO); 116 } 117 118 void SET_FLAGS_OSZAPC_SUB32(CPUX86State *env, uint32_t v1, uint32_t v2, 119 uint32_t diff) 120 { 121 SET_FLAGS_OSZAPC_32(SUB_COUT_VEC(v1, v2, diff), diff); 122 } 123 124 void SET_FLAGS_OSZAPC_SUB16(CPUX86State *env, uint16_t v1, uint16_t v2, 125 uint16_t diff) 126 { 127 SET_FLAGS_OSZAPC_16(SUB_COUT_VEC(v1, v2, diff), diff); 128 } 129 130 void SET_FLAGS_OSZAPC_SUB8(CPUX86State *env, uint8_t v1, uint8_t v2, 131 uint8_t diff) 132 { 133 SET_FLAGS_OSZAPC_8(SUB_COUT_VEC(v1, v2, diff), diff); 134 } 135 136 void SET_FLAGS_OSZAPC_ADD32(CPUX86State *env, uint32_t v1, uint32_t v2, 137 uint32_t diff) 138 { 139 SET_FLAGS_OSZAPC_32(ADD_COUT_VEC(v1, v2, diff), diff); 140 } 141 142 void SET_FLAGS_OSZAPC_ADD16(CPUX86State *env, uint16_t v1, uint16_t v2, 143 uint16_t diff) 144 { 145 SET_FLAGS_OSZAPC_16(ADD_COUT_VEC(v1, v2, diff), diff); 146 } 147 148 void SET_FLAGS_OSZAPC_ADD8(CPUX86State *env, uint8_t v1, uint8_t v2, 149 uint8_t diff) 150 { 151 SET_FLAGS_OSZAPC_8(ADD_COUT_VEC(v1, v2, diff), diff); 152 } 153 154 void SET_FLAGS_OSZAP_SUB32(CPUX86State *env, uint32_t v1, uint32_t v2, 155 uint32_t diff) 156 { 157 SET_FLAGS_OSZAP_32(SUB_COUT_VEC(v1, v2, diff), diff); 158 } 159 160 void SET_FLAGS_OSZAP_SUB16(CPUX86State *env, uint16_t v1, uint16_t v2, 161 uint16_t diff) 162 { 163 SET_FLAGS_OSZAP_16(SUB_COUT_VEC(v1, v2, diff), diff); 164 } 165 166 void SET_FLAGS_OSZAP_SUB8(CPUX86State *env, uint8_t v1, uint8_t v2, 167 uint8_t diff) 168 { 169 SET_FLAGS_OSZAP_8(SUB_COUT_VEC(v1, v2, diff), diff); 170 } 171 172 void SET_FLAGS_OSZAP_ADD32(CPUX86State *env, uint32_t v1, uint32_t v2, 173 uint32_t diff) 174 { 175 SET_FLAGS_OSZAP_32(ADD_COUT_VEC(v1, v2, diff), diff); 176 } 177 178 void SET_FLAGS_OSZAP_ADD16(CPUX86State *env, uint16_t v1, uint16_t v2, 179 uint16_t diff) 180 { 181 SET_FLAGS_OSZAP_16(ADD_COUT_VEC(v1, v2, diff), diff); 182 } 183 184 void SET_FLAGS_OSZAP_ADD8(CPUX86State *env, uint8_t v1, uint8_t v2, 185 uint8_t diff) 186 { 187 SET_FLAGS_OSZAP_8(ADD_COUT_VEC(v1, v2, diff), diff); 188 } 189 190 191 void SET_FLAGS_OSZAPC_LOGIC32(CPUX86State *env, uint32_t v1, uint32_t v2, 192 uint32_t diff) 193 { 194 SET_FLAGS_OSZAPC_32(0, diff); 195 } 196 197 void SET_FLAGS_OSZAPC_LOGIC16(CPUX86State *env, uint16_t v1, uint16_t v2, 198 uint16_t diff) 199 { 200 SET_FLAGS_OSZAPC_16(0, diff); 201 } 202 203 void SET_FLAGS_OSZAPC_LOGIC8(CPUX86State *env, uint8_t v1, uint8_t v2, 204 uint8_t diff) 205 { 206 SET_FLAGS_OSZAPC_8(0, diff); 207 } 208 209 static inline uint32_t get_PF(CPUX86State *env) 210 { 211 return ((parity8(env->cc_dst) - 1) ^ env->cc_src) & CC_P; 212 } 213 214 static inline uint32_t get_OF(CPUX86State *env) 215 { 216 return ((env->cc_src >> (LF_BIT_CF - 11)) + CC_O / 2) & CC_O; 217 } 218 219 bool get_CF(CPUX86State *env) 220 { 221 return ((target_long)env->cc_src) < 0; 222 } 223 224 void set_CF(CPUX86State *env, bool val) 225 { 226 /* If CF changes, flip PO and CF */ 227 target_ulong temp = -(target_ulong)val; 228 target_ulong cf_changed = ((target_long)(env->cc_src ^ temp)) < 0; 229 env->cc_src ^= cf_changed * (LF_MASK_PO | LF_MASK_CF); 230 } 231 232 static inline uint32_t get_ZF(CPUX86State *env) 233 { 234 return env->cc_dst ? 0 : CC_Z; 235 } 236 237 static inline uint32_t get_SF(CPUX86State *env) 238 { 239 return ((env->cc_dst >> (LF_SIGN_BIT - LF_BIT_SD)) ^ 240 env->cc_src) & CC_S; 241 } 242 243 void lflags_to_rflags(CPUX86State *env) 244 { 245 env->eflags &= ~(CC_C|CC_P|CC_A|CC_Z|CC_S|CC_O); 246 /* rotate left by one to move carry-out bits into CF and AF */ 247 env->eflags |= ( 248 (env->cc_src << 1) | 249 (env->cc_src >> (TARGET_LONG_BITS - 1))) & (CC_C | CC_A); 250 env->eflags |= get_SF(env); 251 env->eflags |= get_PF(env); 252 env->eflags |= get_ZF(env); 253 env->eflags |= get_OF(env); 254 } 255 256 void rflags_to_lflags(CPUX86State *env) 257 { 258 target_ulong cf_af, cf_xor_of; 259 260 /* Leave the low byte zero so that parity is always even... */ 261 env->cc_dst = !(env->eflags & CC_Z) << 8; 262 263 /* ... and therefore cc_src always uses opposite polarity. */ 264 env->cc_src = CC_P; 265 env->cc_src ^= env->eflags & (CC_S | CC_P); 266 267 /* rotate right by one to move CF and AF into the carry-out positions */ 268 cf_af = env->eflags & (CC_C | CC_A); 269 env->cc_src |= ((cf_af >> 1) | (cf_af << (TARGET_LONG_BITS - 1))); 270 271 cf_xor_of = ((env->eflags & (CC_C | CC_O)) + (CC_O - CC_C)) & CC_O; 272 env->cc_src |= -cf_xor_of & LF_MASK_PO; 273 } 274