blob: ab85dc5540056151fc160bba0e919fba2c9be0fe [file] [log] [blame]
Blue Swirld7582072012-04-29 13:12:26 +00001/*
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 Pantd9ff33a2020-10-23 12:28:01 +00009 * version 2.1 of the License, or (at your option) any later version.
Blue Swirld7582072012-04-29 13:12:26 +000010 *
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 Maydellb6a0aa02016-01-26 18:17:03 +000020#include "qemu/osdep.h"
Philippe Mathieu-Daudécd617482022-02-07 09:27:56 +010021#include "qemu/log.h"
Blue Swirld7582072012-04-29 13:12:26 +000022#include "cpu.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010023#include "exec/exec-all.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010024#include "qemu/host-utils.h"
Richard Henderson2ef61752014-04-07 22:31:41 -070025#include "exec/helper-proto.h"
Richard Henderson369fd5c2019-03-14 20:01:42 -070026#include "qapi/error.h"
27#include "qemu/guest-random.h"
Claudio Fontanaed69e832020-12-12 16:55:14 +010028#include "helper-tcg.h"
Blue Swirld7582072012-04-29 13:12:26 +000029
30//#define DEBUG_MULDIV
31
32/* modulo 9 table */
33static 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 */
41static 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 Swirl79230572012-04-29 14:11:56 +000050void helper_divb_AL(CPUX86State *env, target_ulong t0)
Blue Swirld7582072012-04-29 13:12:26 +000051{
52 unsigned int num, den, q, r;
53
liguang4b34e3a2013-05-28 16:20:59 +080054 num = (env->regs[R_EAX] & 0xffff);
Blue Swirld7582072012-04-29 13:12:26 +000055 den = (t0 & 0xff);
56 if (den == 0) {
Pavel Dovgalyukcc33c5d2015-07-10 12:57:25 +030057 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
Blue Swirld7582072012-04-29 13:12:26 +000058 }
59 q = (num / den);
60 if (q > 0xff) {
Pavel Dovgalyukcc33c5d2015-07-10 12:57:25 +030061 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
Blue Swirld7582072012-04-29 13:12:26 +000062 }
63 q &= 0xff;
64 r = (num % den) & 0xff;
liguang4b34e3a2013-05-28 16:20:59 +080065 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | (r << 8) | q;
Blue Swirld7582072012-04-29 13:12:26 +000066}
67
Blue Swirl79230572012-04-29 14:11:56 +000068void helper_idivb_AL(CPUX86State *env, target_ulong t0)
Blue Swirld7582072012-04-29 13:12:26 +000069{
70 int num, den, q, r;
71
liguang4b34e3a2013-05-28 16:20:59 +080072 num = (int16_t)env->regs[R_EAX];
Blue Swirld7582072012-04-29 13:12:26 +000073 den = (int8_t)t0;
74 if (den == 0) {
Pavel Dovgalyukcc33c5d2015-07-10 12:57:25 +030075 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
Blue Swirld7582072012-04-29 13:12:26 +000076 }
77 q = (num / den);
78 if (q != (int8_t)q) {
Pavel Dovgalyukcc33c5d2015-07-10 12:57:25 +030079 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
Blue Swirld7582072012-04-29 13:12:26 +000080 }
81 q &= 0xff;
82 r = (num % den) & 0xff;
liguang4b34e3a2013-05-28 16:20:59 +080083 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | (r << 8) | q;
Blue Swirld7582072012-04-29 13:12:26 +000084}
85
Blue Swirl79230572012-04-29 14:11:56 +000086void helper_divw_AX(CPUX86State *env, target_ulong t0)
Blue Swirld7582072012-04-29 13:12:26 +000087{
88 unsigned int num, den, q, r;
89
liguang00f5e6f2013-05-28 16:21:02 +080090 num = (env->regs[R_EAX] & 0xffff) | ((env->regs[R_EDX] & 0xffff) << 16);
Blue Swirld7582072012-04-29 13:12:26 +000091 den = (t0 & 0xffff);
92 if (den == 0) {
Pavel Dovgalyukcc33c5d2015-07-10 12:57:25 +030093 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
Blue Swirld7582072012-04-29 13:12:26 +000094 }
95 q = (num / den);
96 if (q > 0xffff) {
Pavel Dovgalyukcc33c5d2015-07-10 12:57:25 +030097 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
Blue Swirld7582072012-04-29 13:12:26 +000098 }
99 q &= 0xffff;
100 r = (num % den) & 0xffff;
liguang4b34e3a2013-05-28 16:20:59 +0800101 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | q;
liguang00f5e6f2013-05-28 16:21:02 +0800102 env->regs[R_EDX] = (env->regs[R_EDX] & ~0xffff) | r;
Blue Swirld7582072012-04-29 13:12:26 +0000103}
104
Blue Swirl79230572012-04-29 14:11:56 +0000105void helper_idivw_AX(CPUX86State *env, target_ulong t0)
Blue Swirld7582072012-04-29 13:12:26 +0000106{
107 int num, den, q, r;
108
liguang00f5e6f2013-05-28 16:21:02 +0800109 num = (env->regs[R_EAX] & 0xffff) | ((env->regs[R_EDX] & 0xffff) << 16);
Blue Swirld7582072012-04-29 13:12:26 +0000110 den = (int16_t)t0;
111 if (den == 0) {
Pavel Dovgalyukcc33c5d2015-07-10 12:57:25 +0300112 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
Blue Swirld7582072012-04-29 13:12:26 +0000113 }
114 q = (num / den);
115 if (q != (int16_t)q) {
Pavel Dovgalyukcc33c5d2015-07-10 12:57:25 +0300116 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
Blue Swirld7582072012-04-29 13:12:26 +0000117 }
118 q &= 0xffff;
119 r = (num % den) & 0xffff;
liguang4b34e3a2013-05-28 16:20:59 +0800120 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | q;
liguang00f5e6f2013-05-28 16:21:02 +0800121 env->regs[R_EDX] = (env->regs[R_EDX] & ~0xffff) | r;
Blue Swirld7582072012-04-29 13:12:26 +0000122}
123
Blue Swirl79230572012-04-29 14:11:56 +0000124void helper_divl_EAX(CPUX86State *env, target_ulong t0)
Blue Swirld7582072012-04-29 13:12:26 +0000125{
126 unsigned int den, r;
127 uint64_t num, q;
128
liguang00f5e6f2013-05-28 16:21:02 +0800129 num = ((uint32_t)env->regs[R_EAX]) | ((uint64_t)((uint32_t)env->regs[R_EDX]) << 32);
Blue Swirld7582072012-04-29 13:12:26 +0000130 den = t0;
131 if (den == 0) {
Pavel Dovgalyukcc33c5d2015-07-10 12:57:25 +0300132 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
Blue Swirld7582072012-04-29 13:12:26 +0000133 }
134 q = (num / den);
135 r = (num % den);
136 if (q > 0xffffffff) {
Pavel Dovgalyukcc33c5d2015-07-10 12:57:25 +0300137 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
Blue Swirld7582072012-04-29 13:12:26 +0000138 }
liguang4b34e3a2013-05-28 16:20:59 +0800139 env->regs[R_EAX] = (uint32_t)q;
liguang00f5e6f2013-05-28 16:21:02 +0800140 env->regs[R_EDX] = (uint32_t)r;
Blue Swirld7582072012-04-29 13:12:26 +0000141}
142
Blue Swirl79230572012-04-29 14:11:56 +0000143void helper_idivl_EAX(CPUX86State *env, target_ulong t0)
Blue Swirld7582072012-04-29 13:12:26 +0000144{
145 int den, r;
146 int64_t num, q;
147
liguang00f5e6f2013-05-28 16:21:02 +0800148 num = ((uint32_t)env->regs[R_EAX]) | ((uint64_t)((uint32_t)env->regs[R_EDX]) << 32);
Blue Swirld7582072012-04-29 13:12:26 +0000149 den = t0;
150 if (den == 0) {
Pavel Dovgalyukcc33c5d2015-07-10 12:57:25 +0300151 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
Blue Swirld7582072012-04-29 13:12:26 +0000152 }
153 q = (num / den);
154 r = (num % den);
155 if (q != (int32_t)q) {
Pavel Dovgalyukcc33c5d2015-07-10 12:57:25 +0300156 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
Blue Swirld7582072012-04-29 13:12:26 +0000157 }
liguang4b34e3a2013-05-28 16:20:59 +0800158 env->regs[R_EAX] = (uint32_t)q;
liguang00f5e6f2013-05-28 16:21:02 +0800159 env->regs[R_EDX] = (uint32_t)r;
Blue Swirld7582072012-04-29 13:12:26 +0000160}
161
162/* bcd */
163
164/* XXX: exception */
Blue Swirl79230572012-04-29 14:11:56 +0000165void helper_aam(CPUX86State *env, int base)
Blue Swirld7582072012-04-29 13:12:26 +0000166{
167 int al, ah;
168
liguang4b34e3a2013-05-28 16:20:59 +0800169 al = env->regs[R_EAX] & 0xff;
Blue Swirld7582072012-04-29 13:12:26 +0000170 ah = al / base;
171 al = al % base;
liguang4b34e3a2013-05-28 16:20:59 +0800172 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | al | (ah << 8);
Blue Swirld7582072012-04-29 13:12:26 +0000173 CC_DST = al;
174}
175
Blue Swirl79230572012-04-29 14:11:56 +0000176void helper_aad(CPUX86State *env, int base)
Blue Swirld7582072012-04-29 13:12:26 +0000177{
178 int al, ah;
179
liguang4b34e3a2013-05-28 16:20:59 +0800180 al = env->regs[R_EAX] & 0xff;
181 ah = (env->regs[R_EAX] >> 8) & 0xff;
Blue Swirld7582072012-04-29 13:12:26 +0000182 al = ((ah * base) + al) & 0xff;
liguang4b34e3a2013-05-28 16:20:59 +0800183 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | al;
Blue Swirld7582072012-04-29 13:12:26 +0000184 CC_DST = al;
185}
186
Blue Swirl79230572012-04-29 14:11:56 +0000187void helper_aaa(CPUX86State *env)
Blue Swirld7582072012-04-29 13:12:26 +0000188{
189 int icarry;
190 int al, ah, af;
191 int eflags;
192
Paolo Bonzini2455e9c2023-10-26 00:24:46 +0200193 eflags = cpu_cc_compute_all(env);
Blue Swirld7582072012-04-29 13:12:26 +0000194 af = eflags & CC_A;
liguang4b34e3a2013-05-28 16:20:59 +0800195 al = env->regs[R_EAX] & 0xff;
196 ah = (env->regs[R_EAX] >> 8) & 0xff;
Blue Swirld7582072012-04-29 13:12:26 +0000197
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 }
liguang4b34e3a2013-05-28 16:20:59 +0800207 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | al | (ah << 8);
Blue Swirld7582072012-04-29 13:12:26 +0000208 CC_SRC = eflags;
209}
210
Blue Swirl79230572012-04-29 14:11:56 +0000211void helper_aas(CPUX86State *env)
Blue Swirld7582072012-04-29 13:12:26 +0000212{
213 int icarry;
214 int al, ah, af;
215 int eflags;
216
Paolo Bonzini2455e9c2023-10-26 00:24:46 +0200217 eflags = cpu_cc_compute_all(env);
Blue Swirld7582072012-04-29 13:12:26 +0000218 af = eflags & CC_A;
liguang4b34e3a2013-05-28 16:20:59 +0800219 al = env->regs[R_EAX] & 0xff;
220 ah = (env->regs[R_EAX] >> 8) & 0xff;
Blue Swirld7582072012-04-29 13:12:26 +0000221
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 }
liguang4b34e3a2013-05-28 16:20:59 +0800231 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | al | (ah << 8);
Blue Swirld7582072012-04-29 13:12:26 +0000232 CC_SRC = eflags;
233}
234
Blue Swirl79230572012-04-29 14:11:56 +0000235void helper_daa(CPUX86State *env)
Blue Swirld7582072012-04-29 13:12:26 +0000236{
237 int old_al, al, af, cf;
238 int eflags;
239
Paolo Bonzini2455e9c2023-10-26 00:24:46 +0200240 eflags = cpu_cc_compute_all(env);
Blue Swirld7582072012-04-29 13:12:26 +0000241 cf = eflags & CC_C;
242 af = eflags & CC_A;
liguang4b34e3a2013-05-28 16:20:59 +0800243 old_al = al = env->regs[R_EAX] & 0xff;
Blue Swirld7582072012-04-29 13:12:26 +0000244
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 }
liguang4b34e3a2013-05-28 16:20:59 +0800254 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xff) | al;
Blue Swirld7582072012-04-29 13:12:26 +0000255 /* 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 Swirl79230572012-04-29 14:11:56 +0000262void helper_das(CPUX86State *env)
Blue Swirld7582072012-04-29 13:12:26 +0000263{
264 int al, al1, af, cf;
265 int eflags;
266
Paolo Bonzini2455e9c2023-10-26 00:24:46 +0200267 eflags = cpu_cc_compute_all(env);
Blue Swirld7582072012-04-29 13:12:26 +0000268 cf = eflags & CC_C;
269 af = eflags & CC_A;
liguang4b34e3a2013-05-28 16:20:59 +0800270 al = env->regs[R_EAX] & 0xff;
Blue Swirld7582072012-04-29 13:12:26 +0000271
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 }
liguang4b34e3a2013-05-28 16:20:59 +0800285 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xff) | al;
Blue Swirld7582072012-04-29 13:12:26 +0000286 /* 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
294static 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
304static 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 */
312static 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 */
352static 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 Swirl79230572012-04-29 14:11:56 +0000383void helper_divq_EAX(CPUX86State *env, target_ulong t0)
Blue Swirld7582072012-04-29 13:12:26 +0000384{
385 uint64_t r0, r1;
386
387 if (t0 == 0) {
Pavel Dovgalyukcc33c5d2015-07-10 12:57:25 +0300388 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
Blue Swirld7582072012-04-29 13:12:26 +0000389 }
liguang4b34e3a2013-05-28 16:20:59 +0800390 r0 = env->regs[R_EAX];
liguang00f5e6f2013-05-28 16:21:02 +0800391 r1 = env->regs[R_EDX];
Blue Swirld7582072012-04-29 13:12:26 +0000392 if (div64(&r0, &r1, t0)) {
Pavel Dovgalyukcc33c5d2015-07-10 12:57:25 +0300393 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
Blue Swirld7582072012-04-29 13:12:26 +0000394 }
liguang4b34e3a2013-05-28 16:20:59 +0800395 env->regs[R_EAX] = r0;
liguang00f5e6f2013-05-28 16:21:02 +0800396 env->regs[R_EDX] = r1;
Blue Swirld7582072012-04-29 13:12:26 +0000397}
398
Blue Swirl79230572012-04-29 14:11:56 +0000399void helper_idivq_EAX(CPUX86State *env, target_ulong t0)
Blue Swirld7582072012-04-29 13:12:26 +0000400{
401 uint64_t r0, r1;
402
403 if (t0 == 0) {
Pavel Dovgalyukcc33c5d2015-07-10 12:57:25 +0300404 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
Blue Swirld7582072012-04-29 13:12:26 +0000405 }
liguang4b34e3a2013-05-28 16:20:59 +0800406 r0 = env->regs[R_EAX];
liguang00f5e6f2013-05-28 16:21:02 +0800407 r1 = env->regs[R_EDX];
Blue Swirld7582072012-04-29 13:12:26 +0000408 if (idiv64(&r0, &r1, t0)) {
Pavel Dovgalyukcc33c5d2015-07-10 12:57:25 +0300409 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
Blue Swirld7582072012-04-29 13:12:26 +0000410 }
liguang4b34e3a2013-05-28 16:20:59 +0800411 env->regs[R_EAX] = r0;
liguang00f5e6f2013-05-28 16:21:02 +0800412 env->regs[R_EDX] = r1;
Blue Swirld7582072012-04-29 13:12:26 +0000413}
414#endif
415
Richard Hendersonf1300732013-01-21 11:52:26 -0800416#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 Henderson0592f742013-01-23 18:09:43 -0800424target_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
437target_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 Swirld7582072012-04-29 13:12:26 +0000450#define SHIFT 0
Philippe Mathieu-Daudéf1cc7c22023-06-06 15:29:54 +0200451#include "shift_helper_template.h.inc"
Blue Swirld7582072012-04-29 13:12:26 +0000452#undef SHIFT
453
454#define SHIFT 1
Philippe Mathieu-Daudéf1cc7c22023-06-06 15:29:54 +0200455#include "shift_helper_template.h.inc"
Blue Swirld7582072012-04-29 13:12:26 +0000456#undef SHIFT
457
458#define SHIFT 2
Philippe Mathieu-Daudéf1cc7c22023-06-06 15:29:54 +0200459#include "shift_helper_template.h.inc"
Blue Swirld7582072012-04-29 13:12:26 +0000460#undef SHIFT
461
462#ifdef TARGET_X86_64
463#define SHIFT 3
Philippe Mathieu-Daudéf1cc7c22023-06-06 15:29:54 +0200464#include "shift_helper_template.h.inc"
Blue Swirld7582072012-04-29 13:12:26 +0000465#undef SHIFT
466#endif
Richard Henderson07929f22015-11-18 12:55:47 +0100467
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. */
471void 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 Henderson369fd5c2019-03-14 20:01:42 -0700477
478target_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}