Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * x86 integer helpers |
| 3 | * |
| 4 | * Copyright (c) 2003 Fabrice Bellard |
| 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 |
Chetan Pant | d9ff33a | 2020-10-23 12:28:01 +0000 | [diff] [blame] | 9 | * version 2.1 of the License, or (at your option) any later version. |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 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, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
Peter Maydell | b6a0aa0 | 2016-01-26 18:17:03 +0000 | [diff] [blame] | 20 | #include "qemu/osdep.h" |
Philippe Mathieu-Daudé | cd61748 | 2022-02-07 09:27:56 +0100 | [diff] [blame] | 21 | #include "qemu/log.h" |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 22 | #include "cpu.h" |
Paolo Bonzini | 63c9155 | 2016-03-15 13:18:37 +0100 | [diff] [blame] | 23 | #include "exec/exec-all.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 24 | #include "qemu/host-utils.h" |
Richard Henderson | 2ef6175 | 2014-04-07 22:31:41 -0700 | [diff] [blame] | 25 | #include "exec/helper-proto.h" |
Richard Henderson | 369fd5c | 2019-03-14 20:01:42 -0700 | [diff] [blame] | 26 | #include "qapi/error.h" |
| 27 | #include "qemu/guest-random.h" |
Claudio Fontana | ed69e83 | 2020-12-12 16:55:14 +0100 | [diff] [blame] | 28 | #include "helper-tcg.h" |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 29 | |
| 30 | //#define DEBUG_MULDIV |
| 31 | |
| 32 | /* modulo 9 table */ |
| 33 | static const uint8_t rclb_table[32] = { |
| 34 | 0, 1, 2, 3, 4, 5, 6, 7, |
| 35 | 8, 0, 1, 2, 3, 4, 5, 6, |
| 36 | 7, 8, 0, 1, 2, 3, 4, 5, |
| 37 | 6, 7, 8, 0, 1, 2, 3, 4, |
| 38 | }; |
| 39 | |
| 40 | /* modulo 17 table */ |
| 41 | static const uint8_t rclw_table[32] = { |
| 42 | 0, 1, 2, 3, 4, 5, 6, 7, |
| 43 | 8, 9, 10, 11, 12, 13, 14, 15, |
| 44 | 16, 0, 1, 2, 3, 4, 5, 6, |
| 45 | 7, 8, 9, 10, 11, 12, 13, 14, |
| 46 | }; |
| 47 | |
| 48 | /* division, flags are undefined */ |
| 49 | |
Blue Swirl | 7923057 | 2012-04-29 14:11:56 +0000 | [diff] [blame] | 50 | void helper_divb_AL(CPUX86State *env, target_ulong t0) |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 51 | { |
| 52 | unsigned int num, den, q, r; |
| 53 | |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 54 | num = (env->regs[R_EAX] & 0xffff); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 55 | den = (t0 & 0xff); |
| 56 | if (den == 0) { |
Pavel Dovgalyuk | cc33c5d | 2015-07-10 12:57:25 +0300 | [diff] [blame] | 57 | raise_exception_ra(env, EXCP00_DIVZ, GETPC()); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 58 | } |
| 59 | q = (num / den); |
| 60 | if (q > 0xff) { |
Pavel Dovgalyuk | cc33c5d | 2015-07-10 12:57:25 +0300 | [diff] [blame] | 61 | raise_exception_ra(env, EXCP00_DIVZ, GETPC()); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 62 | } |
| 63 | q &= 0xff; |
| 64 | r = (num % den) & 0xff; |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 65 | env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | (r << 8) | q; |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Blue Swirl | 7923057 | 2012-04-29 14:11:56 +0000 | [diff] [blame] | 68 | void helper_idivb_AL(CPUX86State *env, target_ulong t0) |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 69 | { |
| 70 | int num, den, q, r; |
| 71 | |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 72 | num = (int16_t)env->regs[R_EAX]; |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 73 | den = (int8_t)t0; |
| 74 | if (den == 0) { |
Pavel Dovgalyuk | cc33c5d | 2015-07-10 12:57:25 +0300 | [diff] [blame] | 75 | raise_exception_ra(env, EXCP00_DIVZ, GETPC()); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 76 | } |
| 77 | q = (num / den); |
| 78 | if (q != (int8_t)q) { |
Pavel Dovgalyuk | cc33c5d | 2015-07-10 12:57:25 +0300 | [diff] [blame] | 79 | raise_exception_ra(env, EXCP00_DIVZ, GETPC()); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 80 | } |
| 81 | q &= 0xff; |
| 82 | r = (num % den) & 0xff; |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 83 | env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | (r << 8) | q; |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Blue Swirl | 7923057 | 2012-04-29 14:11:56 +0000 | [diff] [blame] | 86 | void helper_divw_AX(CPUX86State *env, target_ulong t0) |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 87 | { |
| 88 | unsigned int num, den, q, r; |
| 89 | |
liguang | 00f5e6f | 2013-05-28 16:21:02 +0800 | [diff] [blame] | 90 | num = (env->regs[R_EAX] & 0xffff) | ((env->regs[R_EDX] & 0xffff) << 16); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 91 | den = (t0 & 0xffff); |
| 92 | if (den == 0) { |
Pavel Dovgalyuk | cc33c5d | 2015-07-10 12:57:25 +0300 | [diff] [blame] | 93 | raise_exception_ra(env, EXCP00_DIVZ, GETPC()); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 94 | } |
| 95 | q = (num / den); |
| 96 | if (q > 0xffff) { |
Pavel Dovgalyuk | cc33c5d | 2015-07-10 12:57:25 +0300 | [diff] [blame] | 97 | raise_exception_ra(env, EXCP00_DIVZ, GETPC()); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 98 | } |
| 99 | q &= 0xffff; |
| 100 | r = (num % den) & 0xffff; |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 101 | env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | q; |
liguang | 00f5e6f | 2013-05-28 16:21:02 +0800 | [diff] [blame] | 102 | env->regs[R_EDX] = (env->regs[R_EDX] & ~0xffff) | r; |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Blue Swirl | 7923057 | 2012-04-29 14:11:56 +0000 | [diff] [blame] | 105 | void helper_idivw_AX(CPUX86State *env, target_ulong t0) |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 106 | { |
| 107 | int num, den, q, r; |
| 108 | |
liguang | 00f5e6f | 2013-05-28 16:21:02 +0800 | [diff] [blame] | 109 | num = (env->regs[R_EAX] & 0xffff) | ((env->regs[R_EDX] & 0xffff) << 16); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 110 | den = (int16_t)t0; |
| 111 | if (den == 0) { |
Pavel Dovgalyuk | cc33c5d | 2015-07-10 12:57:25 +0300 | [diff] [blame] | 112 | raise_exception_ra(env, EXCP00_DIVZ, GETPC()); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 113 | } |
| 114 | q = (num / den); |
| 115 | if (q != (int16_t)q) { |
Pavel Dovgalyuk | cc33c5d | 2015-07-10 12:57:25 +0300 | [diff] [blame] | 116 | raise_exception_ra(env, EXCP00_DIVZ, GETPC()); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 117 | } |
| 118 | q &= 0xffff; |
| 119 | r = (num % den) & 0xffff; |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 120 | env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | q; |
liguang | 00f5e6f | 2013-05-28 16:21:02 +0800 | [diff] [blame] | 121 | env->regs[R_EDX] = (env->regs[R_EDX] & ~0xffff) | r; |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Blue Swirl | 7923057 | 2012-04-29 14:11:56 +0000 | [diff] [blame] | 124 | void helper_divl_EAX(CPUX86State *env, target_ulong t0) |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 125 | { |
| 126 | unsigned int den, r; |
| 127 | uint64_t num, q; |
| 128 | |
liguang | 00f5e6f | 2013-05-28 16:21:02 +0800 | [diff] [blame] | 129 | num = ((uint32_t)env->regs[R_EAX]) | ((uint64_t)((uint32_t)env->regs[R_EDX]) << 32); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 130 | den = t0; |
| 131 | if (den == 0) { |
Pavel Dovgalyuk | cc33c5d | 2015-07-10 12:57:25 +0300 | [diff] [blame] | 132 | raise_exception_ra(env, EXCP00_DIVZ, GETPC()); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 133 | } |
| 134 | q = (num / den); |
| 135 | r = (num % den); |
| 136 | if (q > 0xffffffff) { |
Pavel Dovgalyuk | cc33c5d | 2015-07-10 12:57:25 +0300 | [diff] [blame] | 137 | raise_exception_ra(env, EXCP00_DIVZ, GETPC()); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 138 | } |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 139 | env->regs[R_EAX] = (uint32_t)q; |
liguang | 00f5e6f | 2013-05-28 16:21:02 +0800 | [diff] [blame] | 140 | env->regs[R_EDX] = (uint32_t)r; |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Blue Swirl | 7923057 | 2012-04-29 14:11:56 +0000 | [diff] [blame] | 143 | void helper_idivl_EAX(CPUX86State *env, target_ulong t0) |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 144 | { |
| 145 | int den, r; |
| 146 | int64_t num, q; |
| 147 | |
liguang | 00f5e6f | 2013-05-28 16:21:02 +0800 | [diff] [blame] | 148 | num = ((uint32_t)env->regs[R_EAX]) | ((uint64_t)((uint32_t)env->regs[R_EDX]) << 32); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 149 | den = t0; |
| 150 | if (den == 0) { |
Pavel Dovgalyuk | cc33c5d | 2015-07-10 12:57:25 +0300 | [diff] [blame] | 151 | raise_exception_ra(env, EXCP00_DIVZ, GETPC()); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 152 | } |
| 153 | q = (num / den); |
| 154 | r = (num % den); |
| 155 | if (q != (int32_t)q) { |
Pavel Dovgalyuk | cc33c5d | 2015-07-10 12:57:25 +0300 | [diff] [blame] | 156 | raise_exception_ra(env, EXCP00_DIVZ, GETPC()); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 157 | } |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 158 | env->regs[R_EAX] = (uint32_t)q; |
liguang | 00f5e6f | 2013-05-28 16:21:02 +0800 | [diff] [blame] | 159 | env->regs[R_EDX] = (uint32_t)r; |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* bcd */ |
| 163 | |
| 164 | /* XXX: exception */ |
Blue Swirl | 7923057 | 2012-04-29 14:11:56 +0000 | [diff] [blame] | 165 | void helper_aam(CPUX86State *env, int base) |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 166 | { |
| 167 | int al, ah; |
| 168 | |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 169 | al = env->regs[R_EAX] & 0xff; |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 170 | ah = al / base; |
| 171 | al = al % base; |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 172 | env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | al | (ah << 8); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 173 | CC_DST = al; |
| 174 | } |
| 175 | |
Blue Swirl | 7923057 | 2012-04-29 14:11:56 +0000 | [diff] [blame] | 176 | void helper_aad(CPUX86State *env, int base) |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 177 | { |
| 178 | int al, ah; |
| 179 | |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 180 | al = env->regs[R_EAX] & 0xff; |
| 181 | ah = (env->regs[R_EAX] >> 8) & 0xff; |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 182 | al = ((ah * base) + al) & 0xff; |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 183 | env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | al; |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 184 | CC_DST = al; |
| 185 | } |
| 186 | |
Blue Swirl | 7923057 | 2012-04-29 14:11:56 +0000 | [diff] [blame] | 187 | void helper_aaa(CPUX86State *env) |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 188 | { |
| 189 | int icarry; |
| 190 | int al, ah, af; |
| 191 | int eflags; |
| 192 | |
Paolo Bonzini | 2455e9c | 2023-10-26 00:24:46 +0200 | [diff] [blame] | 193 | eflags = cpu_cc_compute_all(env); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 194 | af = eflags & CC_A; |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 195 | al = env->regs[R_EAX] & 0xff; |
| 196 | ah = (env->regs[R_EAX] >> 8) & 0xff; |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 197 | |
| 198 | icarry = (al > 0xf9); |
| 199 | if (((al & 0x0f) > 9) || af) { |
| 200 | al = (al + 6) & 0x0f; |
| 201 | ah = (ah + 1 + icarry) & 0xff; |
| 202 | eflags |= CC_C | CC_A; |
| 203 | } else { |
| 204 | eflags &= ~(CC_C | CC_A); |
| 205 | al &= 0x0f; |
| 206 | } |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 207 | env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | al | (ah << 8); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 208 | CC_SRC = eflags; |
| 209 | } |
| 210 | |
Blue Swirl | 7923057 | 2012-04-29 14:11:56 +0000 | [diff] [blame] | 211 | void helper_aas(CPUX86State *env) |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 212 | { |
| 213 | int icarry; |
| 214 | int al, ah, af; |
| 215 | int eflags; |
| 216 | |
Paolo Bonzini | 2455e9c | 2023-10-26 00:24:46 +0200 | [diff] [blame] | 217 | eflags = cpu_cc_compute_all(env); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 218 | af = eflags & CC_A; |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 219 | al = env->regs[R_EAX] & 0xff; |
| 220 | ah = (env->regs[R_EAX] >> 8) & 0xff; |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 221 | |
| 222 | icarry = (al < 6); |
| 223 | if (((al & 0x0f) > 9) || af) { |
| 224 | al = (al - 6) & 0x0f; |
| 225 | ah = (ah - 1 - icarry) & 0xff; |
| 226 | eflags |= CC_C | CC_A; |
| 227 | } else { |
| 228 | eflags &= ~(CC_C | CC_A); |
| 229 | al &= 0x0f; |
| 230 | } |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 231 | env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | al | (ah << 8); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 232 | CC_SRC = eflags; |
| 233 | } |
| 234 | |
Blue Swirl | 7923057 | 2012-04-29 14:11:56 +0000 | [diff] [blame] | 235 | void helper_daa(CPUX86State *env) |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 236 | { |
| 237 | int old_al, al, af, cf; |
| 238 | int eflags; |
| 239 | |
Paolo Bonzini | 2455e9c | 2023-10-26 00:24:46 +0200 | [diff] [blame] | 240 | eflags = cpu_cc_compute_all(env); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 241 | cf = eflags & CC_C; |
| 242 | af = eflags & CC_A; |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 243 | old_al = al = env->regs[R_EAX] & 0xff; |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 244 | |
| 245 | eflags = 0; |
| 246 | if (((al & 0x0f) > 9) || af) { |
| 247 | al = (al + 6) & 0xff; |
| 248 | eflags |= CC_A; |
| 249 | } |
| 250 | if ((old_al > 0x99) || cf) { |
| 251 | al = (al + 0x60) & 0xff; |
| 252 | eflags |= CC_C; |
| 253 | } |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 254 | env->regs[R_EAX] = (env->regs[R_EAX] & ~0xff) | al; |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 255 | /* well, speed is not an issue here, so we compute the flags by hand */ |
| 256 | eflags |= (al == 0) << 6; /* zf */ |
| 257 | eflags |= parity_table[al]; /* pf */ |
| 258 | eflags |= (al & 0x80); /* sf */ |
| 259 | CC_SRC = eflags; |
| 260 | } |
| 261 | |
Blue Swirl | 7923057 | 2012-04-29 14:11:56 +0000 | [diff] [blame] | 262 | void helper_das(CPUX86State *env) |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 263 | { |
| 264 | int al, al1, af, cf; |
| 265 | int eflags; |
| 266 | |
Paolo Bonzini | 2455e9c | 2023-10-26 00:24:46 +0200 | [diff] [blame] | 267 | eflags = cpu_cc_compute_all(env); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 268 | cf = eflags & CC_C; |
| 269 | af = eflags & CC_A; |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 270 | al = env->regs[R_EAX] & 0xff; |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 271 | |
| 272 | eflags = 0; |
| 273 | al1 = al; |
| 274 | if (((al & 0x0f) > 9) || af) { |
| 275 | eflags |= CC_A; |
| 276 | if (al < 6 || cf) { |
| 277 | eflags |= CC_C; |
| 278 | } |
| 279 | al = (al - 6) & 0xff; |
| 280 | } |
| 281 | if ((al1 > 0x99) || cf) { |
| 282 | al = (al - 0x60) & 0xff; |
| 283 | eflags |= CC_C; |
| 284 | } |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 285 | env->regs[R_EAX] = (env->regs[R_EAX] & ~0xff) | al; |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 286 | /* well, speed is not an issue here, so we compute the flags by hand */ |
| 287 | eflags |= (al == 0) << 6; /* zf */ |
| 288 | eflags |= parity_table[al]; /* pf */ |
| 289 | eflags |= (al & 0x80); /* sf */ |
| 290 | CC_SRC = eflags; |
| 291 | } |
| 292 | |
| 293 | #ifdef TARGET_X86_64 |
| 294 | static void add128(uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b) |
| 295 | { |
| 296 | *plow += a; |
| 297 | /* carry test */ |
| 298 | if (*plow < a) { |
| 299 | (*phigh)++; |
| 300 | } |
| 301 | *phigh += b; |
| 302 | } |
| 303 | |
| 304 | static void neg128(uint64_t *plow, uint64_t *phigh) |
| 305 | { |
| 306 | *plow = ~*plow; |
| 307 | *phigh = ~*phigh; |
| 308 | add128(plow, phigh, 1, 0); |
| 309 | } |
| 310 | |
| 311 | /* return TRUE if overflow */ |
| 312 | static int div64(uint64_t *plow, uint64_t *phigh, uint64_t b) |
| 313 | { |
| 314 | uint64_t q, r, a1, a0; |
| 315 | int i, qb, ab; |
| 316 | |
| 317 | a0 = *plow; |
| 318 | a1 = *phigh; |
| 319 | if (a1 == 0) { |
| 320 | q = a0 / b; |
| 321 | r = a0 % b; |
| 322 | *plow = q; |
| 323 | *phigh = r; |
| 324 | } else { |
| 325 | if (a1 >= b) { |
| 326 | return 1; |
| 327 | } |
| 328 | /* XXX: use a better algorithm */ |
| 329 | for (i = 0; i < 64; i++) { |
| 330 | ab = a1 >> 63; |
| 331 | a1 = (a1 << 1) | (a0 >> 63); |
| 332 | if (ab || a1 >= b) { |
| 333 | a1 -= b; |
| 334 | qb = 1; |
| 335 | } else { |
| 336 | qb = 0; |
| 337 | } |
| 338 | a0 = (a0 << 1) | qb; |
| 339 | } |
| 340 | #if defined(DEBUG_MULDIV) |
| 341 | printf("div: 0x%016" PRIx64 "%016" PRIx64 " / 0x%016" PRIx64 |
| 342 | ": q=0x%016" PRIx64 " r=0x%016" PRIx64 "\n", |
| 343 | *phigh, *plow, b, a0, a1); |
| 344 | #endif |
| 345 | *plow = a0; |
| 346 | *phigh = a1; |
| 347 | } |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | /* return TRUE if overflow */ |
| 352 | static int idiv64(uint64_t *plow, uint64_t *phigh, int64_t b) |
| 353 | { |
| 354 | int sa, sb; |
| 355 | |
| 356 | sa = ((int64_t)*phigh < 0); |
| 357 | if (sa) { |
| 358 | neg128(plow, phigh); |
| 359 | } |
| 360 | sb = (b < 0); |
| 361 | if (sb) { |
| 362 | b = -b; |
| 363 | } |
| 364 | if (div64(plow, phigh, b) != 0) { |
| 365 | return 1; |
| 366 | } |
| 367 | if (sa ^ sb) { |
| 368 | if (*plow > (1ULL << 63)) { |
| 369 | return 1; |
| 370 | } |
| 371 | *plow = -*plow; |
| 372 | } else { |
| 373 | if (*plow >= (1ULL << 63)) { |
| 374 | return 1; |
| 375 | } |
| 376 | } |
| 377 | if (sa) { |
| 378 | *phigh = -*phigh; |
| 379 | } |
| 380 | return 0; |
| 381 | } |
| 382 | |
Blue Swirl | 7923057 | 2012-04-29 14:11:56 +0000 | [diff] [blame] | 383 | void helper_divq_EAX(CPUX86State *env, target_ulong t0) |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 384 | { |
| 385 | uint64_t r0, r1; |
| 386 | |
| 387 | if (t0 == 0) { |
Pavel Dovgalyuk | cc33c5d | 2015-07-10 12:57:25 +0300 | [diff] [blame] | 388 | raise_exception_ra(env, EXCP00_DIVZ, GETPC()); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 389 | } |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 390 | r0 = env->regs[R_EAX]; |
liguang | 00f5e6f | 2013-05-28 16:21:02 +0800 | [diff] [blame] | 391 | r1 = env->regs[R_EDX]; |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 392 | if (div64(&r0, &r1, t0)) { |
Pavel Dovgalyuk | cc33c5d | 2015-07-10 12:57:25 +0300 | [diff] [blame] | 393 | raise_exception_ra(env, EXCP00_DIVZ, GETPC()); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 394 | } |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 395 | env->regs[R_EAX] = r0; |
liguang | 00f5e6f | 2013-05-28 16:21:02 +0800 | [diff] [blame] | 396 | env->regs[R_EDX] = r1; |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Blue Swirl | 7923057 | 2012-04-29 14:11:56 +0000 | [diff] [blame] | 399 | void helper_idivq_EAX(CPUX86State *env, target_ulong t0) |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 400 | { |
| 401 | uint64_t r0, r1; |
| 402 | |
| 403 | if (t0 == 0) { |
Pavel Dovgalyuk | cc33c5d | 2015-07-10 12:57:25 +0300 | [diff] [blame] | 404 | raise_exception_ra(env, EXCP00_DIVZ, GETPC()); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 405 | } |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 406 | r0 = env->regs[R_EAX]; |
liguang | 00f5e6f | 2013-05-28 16:21:02 +0800 | [diff] [blame] | 407 | r1 = env->regs[R_EDX]; |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 408 | if (idiv64(&r0, &r1, t0)) { |
Pavel Dovgalyuk | cc33c5d | 2015-07-10 12:57:25 +0300 | [diff] [blame] | 409 | raise_exception_ra(env, EXCP00_DIVZ, GETPC()); |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 410 | } |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 411 | env->regs[R_EAX] = r0; |
liguang | 00f5e6f | 2013-05-28 16:21:02 +0800 | [diff] [blame] | 412 | env->regs[R_EDX] = r1; |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 413 | } |
| 414 | #endif |
| 415 | |
Richard Henderson | f130073 | 2013-01-21 11:52:26 -0800 | [diff] [blame] | 416 | #if TARGET_LONG_BITS == 32 |
| 417 | # define ctztl ctz32 |
| 418 | # define clztl clz32 |
| 419 | #else |
| 420 | # define ctztl ctz64 |
| 421 | # define clztl clz64 |
| 422 | #endif |
| 423 | |
Richard Henderson | 0592f74 | 2013-01-23 18:09:43 -0800 | [diff] [blame] | 424 | target_ulong helper_pdep(target_ulong src, target_ulong mask) |
| 425 | { |
| 426 | target_ulong dest = 0; |
| 427 | int i, o; |
| 428 | |
| 429 | for (i = 0; mask != 0; i++) { |
| 430 | o = ctztl(mask); |
| 431 | mask &= mask - 1; |
| 432 | dest |= ((src >> i) & 1) << o; |
| 433 | } |
| 434 | return dest; |
| 435 | } |
| 436 | |
| 437 | target_ulong helper_pext(target_ulong src, target_ulong mask) |
| 438 | { |
| 439 | target_ulong dest = 0; |
| 440 | int i, o; |
| 441 | |
| 442 | for (o = 0; mask != 0; o++) { |
| 443 | i = ctztl(mask); |
| 444 | mask &= mask - 1; |
| 445 | dest |= ((src >> i) & 1) << o; |
| 446 | } |
| 447 | return dest; |
| 448 | } |
| 449 | |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 450 | #define SHIFT 0 |
Philippe Mathieu-Daudé | f1cc7c2 | 2023-06-06 15:29:54 +0200 | [diff] [blame] | 451 | #include "shift_helper_template.h.inc" |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 452 | #undef SHIFT |
| 453 | |
| 454 | #define SHIFT 1 |
Philippe Mathieu-Daudé | f1cc7c2 | 2023-06-06 15:29:54 +0200 | [diff] [blame] | 455 | #include "shift_helper_template.h.inc" |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 456 | #undef SHIFT |
| 457 | |
| 458 | #define SHIFT 2 |
Philippe Mathieu-Daudé | f1cc7c2 | 2023-06-06 15:29:54 +0200 | [diff] [blame] | 459 | #include "shift_helper_template.h.inc" |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 460 | #undef SHIFT |
| 461 | |
| 462 | #ifdef TARGET_X86_64 |
| 463 | #define SHIFT 3 |
Philippe Mathieu-Daudé | f1cc7c2 | 2023-06-06 15:29:54 +0200 | [diff] [blame] | 464 | #include "shift_helper_template.h.inc" |
Blue Swirl | d758207 | 2012-04-29 13:12:26 +0000 | [diff] [blame] | 465 | #undef SHIFT |
| 466 | #endif |
Richard Henderson | 07929f2 | 2015-11-18 12:55:47 +0100 | [diff] [blame] | 467 | |
| 468 | /* Test that BIT is enabled in CR4. If not, raise an illegal opcode |
| 469 | exception. This reduces the requirements for rare CR4 bits being |
| 470 | mapped into HFLAGS. */ |
| 471 | void helper_cr4_testbit(CPUX86State *env, uint32_t bit) |
| 472 | { |
| 473 | if (unlikely((env->cr[4] & bit) == 0)) { |
| 474 | raise_exception_ra(env, EXCP06_ILLOP, GETPC()); |
| 475 | } |
| 476 | } |
Richard Henderson | 369fd5c | 2019-03-14 20:01:42 -0700 | [diff] [blame] | 477 | |
| 478 | target_ulong HELPER(rdrand)(CPUX86State *env) |
| 479 | { |
| 480 | Error *err = NULL; |
| 481 | target_ulong ret; |
| 482 | |
| 483 | if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) { |
| 484 | qemu_log_mask(LOG_UNIMP, "rdrand: Crypto failure: %s", |
| 485 | error_get_pretty(err)); |
| 486 | error_free(err); |
| 487 | /* Failure clears CF and all other flags, and returns 0. */ |
| 488 | env->cc_src = 0; |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | /* Success sets CF and clears all others. */ |
| 493 | env->cc_src = CC_C; |
| 494 | return ret; |
| 495 | } |