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 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"
28 //#define DEBUG_MULDIV
31 static const uint8_t rclb_table
[32] = {
32 0, 1, 2, 3, 4, 5, 6, 7,
33 8, 0, 1, 2, 3, 4, 5, 6,
34 7, 8, 0, 1, 2, 3, 4, 5,
35 6, 7, 8, 0, 1, 2, 3, 4,
39 static const uint8_t rclw_table
[32] = {
40 0, 1, 2, 3, 4, 5, 6, 7,
41 8, 9, 10, 11, 12, 13, 14, 15,
42 16, 0, 1, 2, 3, 4, 5, 6,
43 7, 8, 9, 10, 11, 12, 13, 14,
46 /* division, flags are undefined */
48 void helper_divb_AL(CPUX86State
*env
, target_ulong t0
)
50 unsigned int num
, den
, q
, r
;
52 num
= (env
->regs
[R_EAX
] & 0xffff);
55 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
59 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
62 r
= (num
% den
) & 0xff;
63 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | (r
<< 8) | q
;
66 void helper_idivb_AL(CPUX86State
*env
, target_ulong t0
)
70 num
= (int16_t)env
->regs
[R_EAX
];
73 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
77 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
80 r
= (num
% den
) & 0xff;
81 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | (r
<< 8) | q
;
84 void helper_divw_AX(CPUX86State
*env
, target_ulong t0
)
86 unsigned int num
, den
, q
, r
;
88 num
= (env
->regs
[R_EAX
] & 0xffff) | ((env
->regs
[R_EDX
] & 0xffff) << 16);
91 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
95 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
98 r
= (num
% den
) & 0xffff;
99 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | q
;
100 env
->regs
[R_EDX
] = (env
->regs
[R_EDX
] & ~0xffff) | r
;
103 void helper_idivw_AX(CPUX86State
*env
, target_ulong t0
)
107 num
= (env
->regs
[R_EAX
] & 0xffff) | ((env
->regs
[R_EDX
] & 0xffff) << 16);
110 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
113 if (q
!= (int16_t)q
) {
114 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
117 r
= (num
% den
) & 0xffff;
118 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | q
;
119 env
->regs
[R_EDX
] = (env
->regs
[R_EDX
] & ~0xffff) | r
;
122 void helper_divl_EAX(CPUX86State
*env
, target_ulong t0
)
127 num
= ((uint32_t)env
->regs
[R_EAX
]) | ((uint64_t)((uint32_t)env
->regs
[R_EDX
]) << 32);
130 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
134 if (q
> 0xffffffff) {
135 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
137 env
->regs
[R_EAX
] = (uint32_t)q
;
138 env
->regs
[R_EDX
] = (uint32_t)r
;
141 void helper_idivl_EAX(CPUX86State
*env
, target_ulong t0
)
146 num
= ((uint32_t)env
->regs
[R_EAX
]) | ((uint64_t)((uint32_t)env
->regs
[R_EDX
]) << 32);
149 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
153 if (q
!= (int32_t)q
) {
154 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
156 env
->regs
[R_EAX
] = (uint32_t)q
;
157 env
->regs
[R_EDX
] = (uint32_t)r
;
163 void helper_aam(CPUX86State
*env
, int base
)
167 al
= env
->regs
[R_EAX
] & 0xff;
170 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | al
| (ah
<< 8);
174 void helper_aad(CPUX86State
*env
, int base
)
178 al
= env
->regs
[R_EAX
] & 0xff;
179 ah
= (env
->regs
[R_EAX
] >> 8) & 0xff;
180 al
= ((ah
* base
) + al
) & 0xff;
181 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | al
;
185 void helper_aaa(CPUX86State
*env
)
191 eflags
= cpu_cc_compute_all(env
, CC_OP
);
193 al
= env
->regs
[R_EAX
] & 0xff;
194 ah
= (env
->regs
[R_EAX
] >> 8) & 0xff;
196 icarry
= (al
> 0xf9);
197 if (((al
& 0x0f) > 9) || af
) {
198 al
= (al
+ 6) & 0x0f;
199 ah
= (ah
+ 1 + icarry
) & 0xff;
200 eflags
|= CC_C
| CC_A
;
202 eflags
&= ~(CC_C
| CC_A
);
205 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | al
| (ah
<< 8);
209 void helper_aas(CPUX86State
*env
)
215 eflags
= cpu_cc_compute_all(env
, CC_OP
);
217 al
= env
->regs
[R_EAX
] & 0xff;
218 ah
= (env
->regs
[R_EAX
] >> 8) & 0xff;
221 if (((al
& 0x0f) > 9) || af
) {
222 al
= (al
- 6) & 0x0f;
223 ah
= (ah
- 1 - icarry
) & 0xff;
224 eflags
|= CC_C
| CC_A
;
226 eflags
&= ~(CC_C
| CC_A
);
229 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | al
| (ah
<< 8);
233 void helper_daa(CPUX86State
*env
)
235 int old_al
, al
, af
, cf
;
238 eflags
= cpu_cc_compute_all(env
, CC_OP
);
241 old_al
= al
= env
->regs
[R_EAX
] & 0xff;
244 if (((al
& 0x0f) > 9) || af
) {
245 al
= (al
+ 6) & 0xff;
248 if ((old_al
> 0x99) || cf
) {
249 al
= (al
+ 0x60) & 0xff;
252 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xff) | al
;
253 /* well, speed is not an issue here, so we compute the flags by hand */
254 eflags
|= (al
== 0) << 6; /* zf */
255 eflags
|= parity_table
[al
]; /* pf */
256 eflags
|= (al
& 0x80); /* sf */
260 void helper_das(CPUX86State
*env
)
265 eflags
= cpu_cc_compute_all(env
, CC_OP
);
268 al
= env
->regs
[R_EAX
] & 0xff;
272 if (((al
& 0x0f) > 9) || af
) {
277 al
= (al
- 6) & 0xff;
279 if ((al1
> 0x99) || cf
) {
280 al
= (al
- 0x60) & 0xff;
283 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xff) | al
;
284 /* well, speed is not an issue here, so we compute the flags by hand */
285 eflags
|= (al
== 0) << 6; /* zf */
286 eflags
|= parity_table
[al
]; /* pf */
287 eflags
|= (al
& 0x80); /* sf */
292 static void add128(uint64_t *plow
, uint64_t *phigh
, uint64_t a
, uint64_t b
)
302 static void neg128(uint64_t *plow
, uint64_t *phigh
)
306 add128(plow
, phigh
, 1, 0);
309 /* return TRUE if overflow */
310 static int div64(uint64_t *plow
, uint64_t *phigh
, uint64_t b
)
312 uint64_t q
, r
, a1
, a0
;
326 /* XXX: use a better algorithm */
327 for (i
= 0; i
< 64; i
++) {
329 a1
= (a1
<< 1) | (a0
>> 63);
338 #if defined(DEBUG_MULDIV)
339 printf("div: 0x%016" PRIx64
"%016" PRIx64
" / 0x%016" PRIx64
340 ": q=0x%016" PRIx64
" r=0x%016" PRIx64
"\n",
341 *phigh
, *plow
, b
, a0
, a1
);
349 /* return TRUE if overflow */
350 static int idiv64(uint64_t *plow
, uint64_t *phigh
, int64_t b
)
354 sa
= ((int64_t)*phigh
< 0);
362 if (div64(plow
, phigh
, b
) != 0) {
366 if (*plow
> (1ULL << 63)) {
371 if (*plow
>= (1ULL << 63)) {
381 void helper_divq_EAX(CPUX86State
*env
, target_ulong t0
)
386 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
388 r0
= env
->regs
[R_EAX
];
389 r1
= env
->regs
[R_EDX
];
390 if (div64(&r0
, &r1
, t0
)) {
391 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
393 env
->regs
[R_EAX
] = r0
;
394 env
->regs
[R_EDX
] = r1
;
397 void helper_idivq_EAX(CPUX86State
*env
, target_ulong t0
)
402 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
404 r0
= env
->regs
[R_EAX
];
405 r1
= env
->regs
[R_EDX
];
406 if (idiv64(&r0
, &r1
, t0
)) {
407 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
409 env
->regs
[R_EAX
] = r0
;
410 env
->regs
[R_EDX
] = r1
;
414 #if TARGET_LONG_BITS == 32
422 target_ulong
helper_pdep(target_ulong src
, target_ulong mask
)
424 target_ulong dest
= 0;
427 for (i
= 0; mask
!= 0; i
++) {
430 dest
|= ((src
>> i
) & 1) << o
;
435 target_ulong
helper_pext(target_ulong src
, target_ulong mask
)
437 target_ulong dest
= 0;
440 for (o
= 0; mask
!= 0; o
++) {
443 dest
|= ((src
>> i
) & 1) << o
;
449 #include "shift_helper_template.h"
453 #include "shift_helper_template.h"
457 #include "shift_helper_template.h"
462 #include "shift_helper_template.h"
466 /* Test that BIT is enabled in CR4. If not, raise an illegal opcode
467 exception. This reduces the requirements for rare CR4 bits being
468 mapped into HFLAGS. */
469 void helper_cr4_testbit(CPUX86State
*env
, uint32_t bit
)
471 if (unlikely((env
->cr
[4] & bit
) == 0)) {
472 raise_exception_ra(env
, EXCP06_ILLOP
, GETPC());
476 target_ulong
HELPER(rdrand
)(CPUX86State
*env
)
481 if (qemu_guest_getrandom(&ret
, sizeof(ret
), &err
) < 0) {
482 qemu_log_mask(LOG_UNIMP
, "rdrand: Crypto failure: %s",
483 error_get_pretty(err
));
485 /* Failure clears CF and all other flags, and returns 0. */
490 /* Success sets CF and clears all others. */