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"
23 #include "exec/exec-all.h"
24 #include "qemu/host-utils.h"
25 #include "exec/helper-proto.h"
26 #include "qapi/error.h"
27 #include "qemu/guest-random.h"
28 #include "helper-tcg.h"
30 //#define DEBUG_MULDIV
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,
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,
48 /* division, flags are undefined */
50 void helper_divb_AL(CPUX86State
*env
, target_ulong t0
)
52 unsigned int num
, den
, q
, r
;
54 num
= (env
->regs
[R_EAX
] & 0xffff);
57 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
61 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
64 r
= (num
% den
) & 0xff;
65 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | (r
<< 8) | q
;
68 void helper_idivb_AL(CPUX86State
*env
, target_ulong t0
)
72 num
= (int16_t)env
->regs
[R_EAX
];
75 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
79 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
82 r
= (num
% den
) & 0xff;
83 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | (r
<< 8) | q
;
86 void helper_divw_AX(CPUX86State
*env
, target_ulong t0
)
88 unsigned int num
, den
, q
, r
;
90 num
= (env
->regs
[R_EAX
] & 0xffff) | ((env
->regs
[R_EDX
] & 0xffff) << 16);
93 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
97 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
100 r
= (num
% den
) & 0xffff;
101 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | q
;
102 env
->regs
[R_EDX
] = (env
->regs
[R_EDX
] & ~0xffff) | r
;
105 void helper_idivw_AX(CPUX86State
*env
, target_ulong t0
)
109 num
= (env
->regs
[R_EAX
] & 0xffff) | ((env
->regs
[R_EDX
] & 0xffff) << 16);
112 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
115 if (q
!= (int16_t)q
) {
116 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
119 r
= (num
% den
) & 0xffff;
120 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | q
;
121 env
->regs
[R_EDX
] = (env
->regs
[R_EDX
] & ~0xffff) | r
;
124 void helper_divl_EAX(CPUX86State
*env
, target_ulong t0
)
129 num
= ((uint32_t)env
->regs
[R_EAX
]) | ((uint64_t)((uint32_t)env
->regs
[R_EDX
]) << 32);
132 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
136 if (q
> 0xffffffff) {
137 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
139 env
->regs
[R_EAX
] = (uint32_t)q
;
140 env
->regs
[R_EDX
] = (uint32_t)r
;
143 void helper_idivl_EAX(CPUX86State
*env
, target_ulong t0
)
148 num
= ((uint32_t)env
->regs
[R_EAX
]) | ((uint64_t)((uint32_t)env
->regs
[R_EDX
]) << 32);
151 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
155 if (q
!= (int32_t)q
) {
156 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
158 env
->regs
[R_EAX
] = (uint32_t)q
;
159 env
->regs
[R_EDX
] = (uint32_t)r
;
165 void helper_aam(CPUX86State
*env
, int base
)
169 al
= env
->regs
[R_EAX
] & 0xff;
172 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | al
| (ah
<< 8);
176 void helper_aad(CPUX86State
*env
, int base
)
180 al
= env
->regs
[R_EAX
] & 0xff;
181 ah
= (env
->regs
[R_EAX
] >> 8) & 0xff;
182 al
= ((ah
* base
) + al
) & 0xff;
183 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | al
;
187 void helper_aaa(CPUX86State
*env
)
193 eflags
= cpu_cc_compute_all(env
, CC_OP
);
195 al
= env
->regs
[R_EAX
] & 0xff;
196 ah
= (env
->regs
[R_EAX
] >> 8) & 0xff;
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
;
204 eflags
&= ~(CC_C
| CC_A
);
207 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | al
| (ah
<< 8);
211 void helper_aas(CPUX86State
*env
)
217 eflags
= cpu_cc_compute_all(env
, CC_OP
);
219 al
= env
->regs
[R_EAX
] & 0xff;
220 ah
= (env
->regs
[R_EAX
] >> 8) & 0xff;
223 if (((al
& 0x0f) > 9) || af
) {
224 al
= (al
- 6) & 0x0f;
225 ah
= (ah
- 1 - icarry
) & 0xff;
226 eflags
|= CC_C
| CC_A
;
228 eflags
&= ~(CC_C
| CC_A
);
231 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | al
| (ah
<< 8);
235 void helper_daa(CPUX86State
*env
)
237 int old_al
, al
, af
, cf
;
240 eflags
= cpu_cc_compute_all(env
, CC_OP
);
243 old_al
= al
= env
->regs
[R_EAX
] & 0xff;
246 if (((al
& 0x0f) > 9) || af
) {
247 al
= (al
+ 6) & 0xff;
250 if ((old_al
> 0x99) || cf
) {
251 al
= (al
+ 0x60) & 0xff;
254 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xff) | al
;
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 */
262 void helper_das(CPUX86State
*env
)
267 eflags
= cpu_cc_compute_all(env
, CC_OP
);
270 al
= env
->regs
[R_EAX
] & 0xff;
274 if (((al
& 0x0f) > 9) || af
) {
279 al
= (al
- 6) & 0xff;
281 if ((al1
> 0x99) || cf
) {
282 al
= (al
- 0x60) & 0xff;
285 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xff) | al
;
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 */
294 static void add128(uint64_t *plow
, uint64_t *phigh
, uint64_t a
, uint64_t b
)
304 static void neg128(uint64_t *plow
, uint64_t *phigh
)
308 add128(plow
, phigh
, 1, 0);
311 /* return TRUE if overflow */
312 static int div64(uint64_t *plow
, uint64_t *phigh
, uint64_t b
)
314 uint64_t q
, r
, a1
, a0
;
328 /* XXX: use a better algorithm */
329 for (i
= 0; i
< 64; i
++) {
331 a1
= (a1
<< 1) | (a0
>> 63);
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
);
351 /* return TRUE if overflow */
352 static int idiv64(uint64_t *plow
, uint64_t *phigh
, int64_t b
)
356 sa
= ((int64_t)*phigh
< 0);
364 if (div64(plow
, phigh
, b
) != 0) {
368 if (*plow
> (1ULL << 63)) {
373 if (*plow
>= (1ULL << 63)) {
383 void helper_divq_EAX(CPUX86State
*env
, target_ulong t0
)
388 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
390 r0
= env
->regs
[R_EAX
];
391 r1
= env
->regs
[R_EDX
];
392 if (div64(&r0
, &r1
, t0
)) {
393 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
395 env
->regs
[R_EAX
] = r0
;
396 env
->regs
[R_EDX
] = r1
;
399 void helper_idivq_EAX(CPUX86State
*env
, target_ulong t0
)
404 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
406 r0
= env
->regs
[R_EAX
];
407 r1
= env
->regs
[R_EDX
];
408 if (idiv64(&r0
, &r1
, t0
)) {
409 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
411 env
->regs
[R_EAX
] = r0
;
412 env
->regs
[R_EDX
] = r1
;
416 #if TARGET_LONG_BITS == 32
424 target_ulong
helper_pdep(target_ulong src
, target_ulong mask
)
426 target_ulong dest
= 0;
429 for (i
= 0; mask
!= 0; i
++) {
432 dest
|= ((src
>> i
) & 1) << o
;
437 target_ulong
helper_pext(target_ulong src
, target_ulong mask
)
439 target_ulong dest
= 0;
442 for (o
= 0; mask
!= 0; o
++) {
445 dest
|= ((src
>> i
) & 1) << o
;
451 #include "shift_helper_template.h"
455 #include "shift_helper_template.h"
459 #include "shift_helper_template.h"
464 #include "shift_helper_template.h"
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
)
473 if (unlikely((env
->cr
[4] & bit
) == 0)) {
474 raise_exception_ra(env
, EXCP06_ILLOP
, GETPC());
478 target_ulong
HELPER(rdrand
)(CPUX86State
*env
)
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
));
487 /* Failure clears CF and all other flags, and returns 0. */
492 /* Success sets CF and clears all others. */