target/arm: Restrict v7A TCG cpus to TCG accel
[qemu/ar7.git] / target / i386 / tcg / int_helper.c
blob87fa7280eec7176d1d5ad2e4bf53ee08898e6b98
1 /*
2 * x86 integer helpers
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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/>.
20 #include "qemu/osdep.h"
21 #include "cpu.h"
22 #include "exec/exec-all.h"
23 #include "qemu/host-utils.h"
24 #include "exec/helper-proto.h"
25 #include "qapi/error.h"
26 #include "qemu/guest-random.h"
27 #include "helper-tcg.h"
29 //#define DEBUG_MULDIV
31 /* modulo 9 table */
32 static const uint8_t rclb_table[32] = {
33 0, 1, 2, 3, 4, 5, 6, 7,
34 8, 0, 1, 2, 3, 4, 5, 6,
35 7, 8, 0, 1, 2, 3, 4, 5,
36 6, 7, 8, 0, 1, 2, 3, 4,
39 /* modulo 17 table */
40 static const uint8_t rclw_table[32] = {
41 0, 1, 2, 3, 4, 5, 6, 7,
42 8, 9, 10, 11, 12, 13, 14, 15,
43 16, 0, 1, 2, 3, 4, 5, 6,
44 7, 8, 9, 10, 11, 12, 13, 14,
47 /* division, flags are undefined */
49 void helper_divb_AL(CPUX86State *env, target_ulong t0)
51 unsigned int num, den, q, r;
53 num = (env->regs[R_EAX] & 0xffff);
54 den = (t0 & 0xff);
55 if (den == 0) {
56 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
58 q = (num / den);
59 if (q > 0xff) {
60 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
62 q &= 0xff;
63 r = (num % den) & 0xff;
64 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | (r << 8) | q;
67 void helper_idivb_AL(CPUX86State *env, target_ulong t0)
69 int num, den, q, r;
71 num = (int16_t)env->regs[R_EAX];
72 den = (int8_t)t0;
73 if (den == 0) {
74 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
76 q = (num / den);
77 if (q != (int8_t)q) {
78 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
80 q &= 0xff;
81 r = (num % den) & 0xff;
82 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | (r << 8) | q;
85 void helper_divw_AX(CPUX86State *env, target_ulong t0)
87 unsigned int num, den, q, r;
89 num = (env->regs[R_EAX] & 0xffff) | ((env->regs[R_EDX] & 0xffff) << 16);
90 den = (t0 & 0xffff);
91 if (den == 0) {
92 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
94 q = (num / den);
95 if (q > 0xffff) {
96 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
98 q &= 0xffff;
99 r = (num % den) & 0xffff;
100 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | q;
101 env->regs[R_EDX] = (env->regs[R_EDX] & ~0xffff) | r;
104 void helper_idivw_AX(CPUX86State *env, target_ulong t0)
106 int num, den, q, r;
108 num = (env->regs[R_EAX] & 0xffff) | ((env->regs[R_EDX] & 0xffff) << 16);
109 den = (int16_t)t0;
110 if (den == 0) {
111 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
113 q = (num / den);
114 if (q != (int16_t)q) {
115 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
117 q &= 0xffff;
118 r = (num % den) & 0xffff;
119 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | q;
120 env->regs[R_EDX] = (env->regs[R_EDX] & ~0xffff) | r;
123 void helper_divl_EAX(CPUX86State *env, target_ulong t0)
125 unsigned int den, r;
126 uint64_t num, q;
128 num = ((uint32_t)env->regs[R_EAX]) | ((uint64_t)((uint32_t)env->regs[R_EDX]) << 32);
129 den = t0;
130 if (den == 0) {
131 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
133 q = (num / den);
134 r = (num % den);
135 if (q > 0xffffffff) {
136 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
138 env->regs[R_EAX] = (uint32_t)q;
139 env->regs[R_EDX] = (uint32_t)r;
142 void helper_idivl_EAX(CPUX86State *env, target_ulong t0)
144 int den, r;
145 int64_t num, q;
147 num = ((uint32_t)env->regs[R_EAX]) | ((uint64_t)((uint32_t)env->regs[R_EDX]) << 32);
148 den = t0;
149 if (den == 0) {
150 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
152 q = (num / den);
153 r = (num % den);
154 if (q != (int32_t)q) {
155 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
157 env->regs[R_EAX] = (uint32_t)q;
158 env->regs[R_EDX] = (uint32_t)r;
161 /* bcd */
163 /* XXX: exception */
164 void helper_aam(CPUX86State *env, int base)
166 int al, ah;
168 al = env->regs[R_EAX] & 0xff;
169 ah = al / base;
170 al = al % base;
171 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | al | (ah << 8);
172 CC_DST = al;
175 void helper_aad(CPUX86State *env, int base)
177 int al, ah;
179 al = env->regs[R_EAX] & 0xff;
180 ah = (env->regs[R_EAX] >> 8) & 0xff;
181 al = ((ah * base) + al) & 0xff;
182 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | al;
183 CC_DST = al;
186 void helper_aaa(CPUX86State *env)
188 int icarry;
189 int al, ah, af;
190 int eflags;
192 eflags = cpu_cc_compute_all(env, CC_OP);
193 af = eflags & CC_A;
194 al = env->regs[R_EAX] & 0xff;
195 ah = (env->regs[R_EAX] >> 8) & 0xff;
197 icarry = (al > 0xf9);
198 if (((al & 0x0f) > 9) || af) {
199 al = (al + 6) & 0x0f;
200 ah = (ah + 1 + icarry) & 0xff;
201 eflags |= CC_C | CC_A;
202 } else {
203 eflags &= ~(CC_C | CC_A);
204 al &= 0x0f;
206 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | al | (ah << 8);
207 CC_SRC = eflags;
210 void helper_aas(CPUX86State *env)
212 int icarry;
213 int al, ah, af;
214 int eflags;
216 eflags = cpu_cc_compute_all(env, CC_OP);
217 af = eflags & CC_A;
218 al = env->regs[R_EAX] & 0xff;
219 ah = (env->regs[R_EAX] >> 8) & 0xff;
221 icarry = (al < 6);
222 if (((al & 0x0f) > 9) || af) {
223 al = (al - 6) & 0x0f;
224 ah = (ah - 1 - icarry) & 0xff;
225 eflags |= CC_C | CC_A;
226 } else {
227 eflags &= ~(CC_C | CC_A);
228 al &= 0x0f;
230 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | al | (ah << 8);
231 CC_SRC = eflags;
234 void helper_daa(CPUX86State *env)
236 int old_al, al, af, cf;
237 int eflags;
239 eflags = cpu_cc_compute_all(env, CC_OP);
240 cf = eflags & CC_C;
241 af = eflags & CC_A;
242 old_al = al = env->regs[R_EAX] & 0xff;
244 eflags = 0;
245 if (((al & 0x0f) > 9) || af) {
246 al = (al + 6) & 0xff;
247 eflags |= CC_A;
249 if ((old_al > 0x99) || cf) {
250 al = (al + 0x60) & 0xff;
251 eflags |= CC_C;
253 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xff) | al;
254 /* well, speed is not an issue here, so we compute the flags by hand */
255 eflags |= (al == 0) << 6; /* zf */
256 eflags |= parity_table[al]; /* pf */
257 eflags |= (al & 0x80); /* sf */
258 CC_SRC = eflags;
261 void helper_das(CPUX86State *env)
263 int al, al1, af, cf;
264 int eflags;
266 eflags = cpu_cc_compute_all(env, CC_OP);
267 cf = eflags & CC_C;
268 af = eflags & CC_A;
269 al = env->regs[R_EAX] & 0xff;
271 eflags = 0;
272 al1 = al;
273 if (((al & 0x0f) > 9) || af) {
274 eflags |= CC_A;
275 if (al < 6 || cf) {
276 eflags |= CC_C;
278 al = (al - 6) & 0xff;
280 if ((al1 > 0x99) || cf) {
281 al = (al - 0x60) & 0xff;
282 eflags |= CC_C;
284 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xff) | al;
285 /* well, speed is not an issue here, so we compute the flags by hand */
286 eflags |= (al == 0) << 6; /* zf */
287 eflags |= parity_table[al]; /* pf */
288 eflags |= (al & 0x80); /* sf */
289 CC_SRC = eflags;
292 #ifdef TARGET_X86_64
293 static void add128(uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
295 *plow += a;
296 /* carry test */
297 if (*plow < a) {
298 (*phigh)++;
300 *phigh += b;
303 static void neg128(uint64_t *plow, uint64_t *phigh)
305 *plow = ~*plow;
306 *phigh = ~*phigh;
307 add128(plow, phigh, 1, 0);
310 /* return TRUE if overflow */
311 static int div64(uint64_t *plow, uint64_t *phigh, uint64_t b)
313 uint64_t q, r, a1, a0;
314 int i, qb, ab;
316 a0 = *plow;
317 a1 = *phigh;
318 if (a1 == 0) {
319 q = a0 / b;
320 r = a0 % b;
321 *plow = q;
322 *phigh = r;
323 } else {
324 if (a1 >= b) {
325 return 1;
327 /* XXX: use a better algorithm */
328 for (i = 0; i < 64; i++) {
329 ab = a1 >> 63;
330 a1 = (a1 << 1) | (a0 >> 63);
331 if (ab || a1 >= b) {
332 a1 -= b;
333 qb = 1;
334 } else {
335 qb = 0;
337 a0 = (a0 << 1) | qb;
339 #if defined(DEBUG_MULDIV)
340 printf("div: 0x%016" PRIx64 "%016" PRIx64 " / 0x%016" PRIx64
341 ": q=0x%016" PRIx64 " r=0x%016" PRIx64 "\n",
342 *phigh, *plow, b, a0, a1);
343 #endif
344 *plow = a0;
345 *phigh = a1;
347 return 0;
350 /* return TRUE if overflow */
351 static int idiv64(uint64_t *plow, uint64_t *phigh, int64_t b)
353 int sa, sb;
355 sa = ((int64_t)*phigh < 0);
356 if (sa) {
357 neg128(plow, phigh);
359 sb = (b < 0);
360 if (sb) {
361 b = -b;
363 if (div64(plow, phigh, b) != 0) {
364 return 1;
366 if (sa ^ sb) {
367 if (*plow > (1ULL << 63)) {
368 return 1;
370 *plow = -*plow;
371 } else {
372 if (*plow >= (1ULL << 63)) {
373 return 1;
376 if (sa) {
377 *phigh = -*phigh;
379 return 0;
382 void helper_divq_EAX(CPUX86State *env, target_ulong t0)
384 uint64_t r0, r1;
386 if (t0 == 0) {
387 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
389 r0 = env->regs[R_EAX];
390 r1 = env->regs[R_EDX];
391 if (div64(&r0, &r1, t0)) {
392 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
394 env->regs[R_EAX] = r0;
395 env->regs[R_EDX] = r1;
398 void helper_idivq_EAX(CPUX86State *env, target_ulong t0)
400 uint64_t r0, r1;
402 if (t0 == 0) {
403 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
405 r0 = env->regs[R_EAX];
406 r1 = env->regs[R_EDX];
407 if (idiv64(&r0, &r1, t0)) {
408 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
410 env->regs[R_EAX] = r0;
411 env->regs[R_EDX] = r1;
413 #endif
415 #if TARGET_LONG_BITS == 32
416 # define ctztl ctz32
417 # define clztl clz32
418 #else
419 # define ctztl ctz64
420 # define clztl clz64
421 #endif
423 target_ulong helper_pdep(target_ulong src, target_ulong mask)
425 target_ulong dest = 0;
426 int i, o;
428 for (i = 0; mask != 0; i++) {
429 o = ctztl(mask);
430 mask &= mask - 1;
431 dest |= ((src >> i) & 1) << o;
433 return dest;
436 target_ulong helper_pext(target_ulong src, target_ulong mask)
438 target_ulong dest = 0;
439 int i, o;
441 for (o = 0; mask != 0; o++) {
442 i = ctztl(mask);
443 mask &= mask - 1;
444 dest |= ((src >> i) & 1) << o;
446 return dest;
449 #define SHIFT 0
450 #include "shift_helper_template.h"
451 #undef SHIFT
453 #define SHIFT 1
454 #include "shift_helper_template.h"
455 #undef SHIFT
457 #define SHIFT 2
458 #include "shift_helper_template.h"
459 #undef SHIFT
461 #ifdef TARGET_X86_64
462 #define SHIFT 3
463 #include "shift_helper_template.h"
464 #undef SHIFT
465 #endif
467 /* Test that BIT is enabled in CR4. If not, raise an illegal opcode
468 exception. This reduces the requirements for rare CR4 bits being
469 mapped into HFLAGS. */
470 void helper_cr4_testbit(CPUX86State *env, uint32_t bit)
472 if (unlikely((env->cr[4] & bit) == 0)) {
473 raise_exception_ra(env, EXCP06_ILLOP, GETPC());
477 target_ulong HELPER(rdrand)(CPUX86State *env)
479 Error *err = NULL;
480 target_ulong ret;
482 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
483 qemu_log_mask(LOG_UNIMP, "rdrand: Crypto failure: %s",
484 error_get_pretty(err));
485 error_free(err);
486 /* Failure clears CF and all other flags, and returns 0. */
487 env->cc_src = 0;
488 return 0;
491 /* Success sets CF and clears all others. */
492 env->cc_src = CC_C;
493 return ret;