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"
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
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,
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);
56 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
60 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
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
)
71 num
= (int16_t)env
->regs
[R_EAX
];
74 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
78 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
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);
92 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
96 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
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
)
108 num
= (env
->regs
[R_EAX
] & 0xffff) | ((env
->regs
[R_EDX
] & 0xffff) << 16);
111 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
114 if (q
!= (int16_t)q
) {
115 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
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
)
128 num
= ((uint32_t)env
->regs
[R_EAX
]) | ((uint64_t)((uint32_t)env
->regs
[R_EDX
]) << 32);
131 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
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
)
147 num
= ((uint32_t)env
->regs
[R_EAX
]) | ((uint64_t)((uint32_t)env
->regs
[R_EDX
]) << 32);
150 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
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
;
164 void helper_aam(CPUX86State
*env
, int base
)
168 al
= env
->regs
[R_EAX
] & 0xff;
171 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | al
| (ah
<< 8);
175 void helper_aad(CPUX86State
*env
, int base
)
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
;
186 void helper_aaa(CPUX86State
*env
)
192 eflags
= cpu_cc_compute_all(env
, CC_OP
);
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
;
203 eflags
&= ~(CC_C
| CC_A
);
206 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | al
| (ah
<< 8);
210 void helper_aas(CPUX86State
*env
)
216 eflags
= cpu_cc_compute_all(env
, CC_OP
);
218 al
= env
->regs
[R_EAX
] & 0xff;
219 ah
= (env
->regs
[R_EAX
] >> 8) & 0xff;
222 if (((al
& 0x0f) > 9) || af
) {
223 al
= (al
- 6) & 0x0f;
224 ah
= (ah
- 1 - icarry
) & 0xff;
225 eflags
|= CC_C
| CC_A
;
227 eflags
&= ~(CC_C
| CC_A
);
230 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | al
| (ah
<< 8);
234 void helper_daa(CPUX86State
*env
)
236 int old_al
, al
, af
, cf
;
239 eflags
= cpu_cc_compute_all(env
, CC_OP
);
242 old_al
= al
= env
->regs
[R_EAX
] & 0xff;
245 if (((al
& 0x0f) > 9) || af
) {
246 al
= (al
+ 6) & 0xff;
249 if ((old_al
> 0x99) || cf
) {
250 al
= (al
+ 0x60) & 0xff;
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 */
261 void helper_das(CPUX86State
*env
)
266 eflags
= cpu_cc_compute_all(env
, CC_OP
);
269 al
= env
->regs
[R_EAX
] & 0xff;
273 if (((al
& 0x0f) > 9) || af
) {
278 al
= (al
- 6) & 0xff;
280 if ((al1
> 0x99) || cf
) {
281 al
= (al
- 0x60) & 0xff;
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 */
293 static void add128(uint64_t *plow
, uint64_t *phigh
, uint64_t a
, uint64_t b
)
303 static void neg128(uint64_t *plow
, uint64_t *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
;
327 /* XXX: use a better algorithm */
328 for (i
= 0; i
< 64; i
++) {
330 a1
= (a1
<< 1) | (a0
>> 63);
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
);
350 /* return TRUE if overflow */
351 static int idiv64(uint64_t *plow
, uint64_t *phigh
, int64_t b
)
355 sa
= ((int64_t)*phigh
< 0);
363 if (div64(plow
, phigh
, b
) != 0) {
367 if (*plow
> (1ULL << 63)) {
372 if (*plow
>= (1ULL << 63)) {
382 void helper_divq_EAX(CPUX86State
*env
, target_ulong t0
)
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
)
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
;
415 #if TARGET_LONG_BITS == 32
423 target_ulong
helper_pdep(target_ulong src
, target_ulong mask
)
425 target_ulong dest
= 0;
428 for (i
= 0; mask
!= 0; i
++) {
431 dest
|= ((src
>> i
) & 1) << o
;
436 target_ulong
helper_pext(target_ulong src
, target_ulong mask
)
438 target_ulong dest
= 0;
441 for (o
= 0; mask
!= 0; o
++) {
444 dest
|= ((src
>> i
) & 1) << o
;
450 #include "shift_helper_template.h"
454 #include "shift_helper_template.h"
458 #include "shift_helper_template.h"
463 #include "shift_helper_template.h"
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
)
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
));
486 /* Failure clears CF and all other flags, and returns 0. */
491 /* Success sets CF and clears all others. */