Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/target/i386/tcg/int_helper.c
Line
Count
Source
1
/*
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
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
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
20
#include "qemu/osdep.h"
21
#include "qemu/log.h"
22
#include "cpu.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"
28
29
//#define DEBUG_MULDIV
30
31
/* division, flags are undefined */
32
33
void HELPER(divb_AL)(CPUX86State *env, target_ulong t0)
34
0
{
35
0
    unsigned int num, den, q, r;
36
37
0
    num = (env->regs[R_EAX] & 0xffff);
38
0
    den = (t0 & 0xff);
39
0
    if (den == 0) {
40
0
        raise_exception_ra(env, EXCP00_DIVZ, GETPC());
41
0
    }
42
0
    q = (num / den);
43
0
    if (q > 0xff) {
44
0
        raise_exception_ra(env, EXCP00_DIVZ, GETPC());
45
0
    }
46
0
    q &= 0xff;
47
0
    r = (num % den) & 0xff;
48
0
    env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | (r << 8) | q;
49
0
}
50
51
void HELPER(idivb_AL)(CPUX86State *env, target_ulong t0)
52
0
{
53
0
    int num, den, q, r;
54
55
0
    num = (int16_t)env->regs[R_EAX];
56
0
    den = (int8_t)t0;
57
0
    if (den == 0) {
58
0
        raise_exception_ra(env, EXCP00_DIVZ, GETPC());
59
0
    }
60
0
    q = (num / den);
61
0
    if (q != (int8_t)q) {
62
0
        raise_exception_ra(env, EXCP00_DIVZ, GETPC());
63
0
    }
64
0
    q &= 0xff;
65
0
    r = (num % den) & 0xff;
66
0
    env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | (r << 8) | q;
67
0
}
68
69
void HELPER(divw_AX)(CPUX86State *env, target_ulong t0)
70
0
{
71
0
    unsigned int num, den, q, r;
72
73
0
    num = (env->regs[R_EAX] & 0xffff) | ((env->regs[R_EDX] & 0xffff) << 16);
74
0
    den = (t0 & 0xffff);
75
0
    if (den == 0) {
76
0
        raise_exception_ra(env, EXCP00_DIVZ, GETPC());
77
0
    }
78
0
    q = (num / den);
79
0
    if (q > 0xffff) {
80
0
        raise_exception_ra(env, EXCP00_DIVZ, GETPC());
81
0
    }
82
0
    q &= 0xffff;
83
0
    r = (num % den) & 0xffff;
84
0
    env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | q;
85
0
    env->regs[R_EDX] = (env->regs[R_EDX] & ~0xffff) | r;
86
0
}
87
88
void HELPER(idivw_AX)(CPUX86State *env, target_ulong t0)
89
0
{
90
0
    int num, den, q, r;
91
92
0
    num = (env->regs[R_EAX] & 0xffff) | ((env->regs[R_EDX] & 0xffff) << 16);
93
0
    den = (int16_t)t0;
94
0
    if (den == 0 || (num == INT32_MIN && den == -1)) {
95
0
        raise_exception_ra(env, EXCP00_DIVZ, GETPC());
96
0
    }
97
0
    q = (num / den);
98
0
    if (q != (int16_t)q) {
99
0
        raise_exception_ra(env, EXCP00_DIVZ, GETPC());
100
0
    }
101
0
    q &= 0xffff;
102
0
    r = (num % den) & 0xffff;
103
0
    env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | q;
104
0
    env->regs[R_EDX] = (env->regs[R_EDX] & ~0xffff) | r;
105
0
}
106
107
void HELPER(divl_EAX)(CPUX86State *env, target_ulong t0)
108
0
{
109
0
    unsigned int den, r;
110
0
    uint64_t num, q;
111
112
0
    num = ((uint32_t)env->regs[R_EAX]) | ((uint64_t)((uint32_t)env->regs[R_EDX]) << 32);
113
0
    den = t0;
114
0
    if (den == 0) {
115
0
        raise_exception_ra(env, EXCP00_DIVZ, GETPC());
116
0
    }
117
0
    q = (num / den);
118
0
    r = (num % den);
119
0
    if (q > 0xffffffff) {
120
0
        raise_exception_ra(env, EXCP00_DIVZ, GETPC());
121
0
    }
122
0
    env->regs[R_EAX] = (uint32_t)q;
123
0
    env->regs[R_EDX] = (uint32_t)r;
124
0
}
125
126
void HELPER(idivl_EAX)(CPUX86State *env, target_ulong t0)
127
0
{
128
0
    int den, r;
129
0
    int64_t num, q;
130
131
0
    num = ((uint32_t)env->regs[R_EAX]) | ((uint64_t)((uint32_t)env->regs[R_EDX]) << 32);
132
0
    den = t0;
133
0
    if (den == 0 || (num == INT64_MIN && den == -1)) {
134
0
        raise_exception_ra(env, EXCP00_DIVZ, GETPC());
135
0
    }
136
0
    q = (num / den);
137
0
    r = (num % den);
138
0
    if (q != (int32_t)q) {
139
0
        raise_exception_ra(env, EXCP00_DIVZ, GETPC());
140
0
    }
141
0
    env->regs[R_EAX] = (uint32_t)q;
142
0
    env->regs[R_EDX] = (uint32_t)r;
143
0
}
144
145
/* bcd */
146
147
target_ulong HELPER(aam)(target_ulong al, target_ulong base)
148
0
{
149
0
    int ah;
150
151
0
    al &= 0xff;
152
0
    ah = al / base;
153
0
    al = al % base;
154
0
    return al | (ah << 8);
155
0
}
156
157
target_ulong HELPER(aad)(target_ulong ax, target_ulong base)
158
0
{
159
0
    int al, ah;
160
161
0
    al = ax & 0xff;
162
0
    ah = (ax >> 8) & 0xff;
163
0
    al = ((ah * base) + al) & 0xff;
164
0
    return al;
165
0
}
166
167
void HELPER(aaa)(CPUX86State *env)
168
0
{
169
0
    int icarry;
170
0
    int al, ah, af;
171
0
    int eflags;
172
173
0
    eflags = cpu_cc_compute_all(env);
174
0
    af = eflags & CC_A;
175
0
    al = env->regs[R_EAX] & 0xff;
176
0
    ah = (env->regs[R_EAX] >> 8) & 0xff;
177
178
0
    icarry = (al > 0xf9);
179
0
    if (((al & 0x0f) > 9) || af) {
180
0
        al = (al + 6) & 0x0f;
181
0
        ah = (ah + 1 + icarry) & 0xff;
182
0
        eflags |= CC_C | CC_A;
183
0
    } else {
184
0
        eflags &= ~(CC_C | CC_A);
185
0
        al &= 0x0f;
186
0
    }
187
0
    env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | al | (ah << 8);
188
0
    CC_SRC = eflags;
189
0
    CC_OP = CC_OP_EFLAGS;
190
0
}
191
192
void HELPER(aas)(CPUX86State *env)
193
0
{
194
0
    int icarry;
195
0
    int al, ah, af;
196
0
    int eflags;
197
198
0
    eflags = cpu_cc_compute_all(env);
199
0
    af = eflags & CC_A;
200
0
    al = env->regs[R_EAX] & 0xff;
201
0
    ah = (env->regs[R_EAX] >> 8) & 0xff;
202
203
0
    icarry = (al < 6);
204
0
    if (((al & 0x0f) > 9) || af) {
205
0
        al = (al - 6) & 0x0f;
206
0
        ah = (ah - 1 - icarry) & 0xff;
207
0
        eflags |= CC_C | CC_A;
208
0
    } else {
209
0
        eflags &= ~(CC_C | CC_A);
210
0
        al &= 0x0f;
211
0
    }
212
0
    env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | al | (ah << 8);
213
0
    CC_SRC = eflags;
214
0
    CC_OP = CC_OP_EFLAGS;
215
0
}
216
217
void HELPER(daa)(CPUX86State *env)
218
0
{
219
0
    int old_al, al, af, cf;
220
0
    int eflags;
221
222
0
    eflags = cpu_cc_compute_all(env);
223
0
    cf = eflags & CC_C;
224
0
    af = eflags & CC_A;
225
0
    old_al = al = env->regs[R_EAX] & 0xff;
226
227
0
    eflags = 0;
228
0
    if (((al & 0x0f) > 9) || af) {
229
0
        al = (al + 6) & 0xff;
230
0
        eflags |= CC_A;
231
0
    }
232
0
    if ((old_al > 0x99) || cf) {
233
0
        al = (al + 0x60) & 0xff;
234
0
        eflags |= CC_C;
235
0
    }
236
0
    env->regs[R_EAX] = (env->regs[R_EAX] & ~0xff) | al;
237
    /* well, speed is not an issue here, so we compute the flags by hand */
238
0
    eflags |= (al == 0) << 6; /* zf */
239
0
    eflags |= compute_pf(al);
240
0
    eflags |= (al & 0x80); /* sf */
241
0
    CC_SRC = eflags;
242
0
    CC_OP = CC_OP_EFLAGS;
243
0
}
244
245
void HELPER(das)(CPUX86State *env)
246
0
{
247
0
    int al, al1, af, cf;
248
0
    int eflags;
249
250
0
    eflags = cpu_cc_compute_all(env);
251
0
    cf = eflags & CC_C;
252
0
    af = eflags & CC_A;
253
0
    al = env->regs[R_EAX] & 0xff;
254
255
0
    eflags = 0;
256
0
    al1 = al;
257
0
    if (((al & 0x0f) > 9) || af) {
258
0
        eflags |= CC_A;
259
0
        if (al < 6 || cf) {
260
0
            eflags |= CC_C;
261
0
        }
262
0
        al = (al - 6) & 0xff;
263
0
    }
264
0
    if ((al1 > 0x99) || cf) {
265
0
        al = (al - 0x60) & 0xff;
266
0
        eflags |= CC_C;
267
0
    }
268
0
    env->regs[R_EAX] = (env->regs[R_EAX] & ~0xff) | al;
269
    /* well, speed is not an issue here, so we compute the flags by hand */
270
0
    eflags |= (al == 0) << 6; /* zf */
271
0
    eflags |= compute_pf(al);
272
0
    eflags |= (al & 0x80); /* sf */
273
0
    CC_SRC = eflags;
274
0
    CC_OP = CC_OP_EFLAGS;
275
0
}
276
277
#ifdef TARGET_X86_64
278
static void add128(uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
279
{
280
    *plow += a;
281
    /* carry test */
282
    if (*plow < a) {
283
        (*phigh)++;
284
    }
285
    *phigh += b;
286
}
287
288
static void neg128(uint64_t *plow, uint64_t *phigh)
289
{
290
    *plow = ~*plow;
291
    *phigh = ~*phigh;
292
    add128(plow, phigh, 1, 0);
293
}
294
295
/* return TRUE if overflow */
296
static int div64(uint64_t *plow, uint64_t *phigh, uint64_t b)
297
{
298
    uint64_t q, r, a1, a0;
299
    int i, qb, ab;
300
301
    a0 = *plow;
302
    a1 = *phigh;
303
    if (a1 == 0) {
304
        q = a0 / b;
305
        r = a0 % b;
306
        *plow = q;
307
        *phigh = r;
308
    } else {
309
        if (a1 >= b) {
310
            return 1;
311
        }
312
        /* XXX: use a better algorithm */
313
        for (i = 0; i < 64; i++) {
314
            ab = a1 >> 63;
315
            a1 = (a1 << 1) | (a0 >> 63);
316
            if (ab || a1 >= b) {
317
                a1 -= b;
318
                qb = 1;
319
            } else {
320
                qb = 0;
321
            }
322
            a0 = (a0 << 1) | qb;
323
        }
324
#if defined(DEBUG_MULDIV)
325
        printf("div: 0x%016" PRIx64 "%016" PRIx64 " / 0x%016" PRIx64
326
               ": q=0x%016" PRIx64 " r=0x%016" PRIx64 "\n",
327
               *phigh, *plow, b, a0, a1);
328
#endif
329
        *plow = a0;
330
        *phigh = a1;
331
    }
332
    return 0;
333
}
334
335
/* return TRUE if overflow */
336
static int idiv64(uint64_t *plow, uint64_t *phigh, int64_t b)
337
{
338
    int sa, sb;
339
340
    sa = ((int64_t)*phigh < 0);
341
    if (sa) {
342
        neg128(plow, phigh);
343
    }
344
    sb = (b < 0);
345
    if (sb) {
346
        b = -b;
347
    }
348
    if (div64(plow, phigh, b) != 0) {
349
        return 1;
350
    }
351
    if (sa ^ sb) {
352
        if (*plow > (1ULL << 63)) {
353
            return 1;
354
        }
355
        *plow = -*plow;
356
    } else {
357
        if (*plow >= (1ULL << 63)) {
358
            return 1;
359
        }
360
    }
361
    if (sa) {
362
        *phigh = -*phigh;
363
    }
364
    return 0;
365
}
366
367
void HELPER(divq_EAX)(CPUX86State *env, target_ulong t0)
368
{
369
    uint64_t r0, r1;
370
371
    if (t0 == 0) {
372
        raise_exception_ra(env, EXCP00_DIVZ, GETPC());
373
    }
374
    r0 = env->regs[R_EAX];
375
    r1 = env->regs[R_EDX];
376
    if (div64(&r0, &r1, t0)) {
377
        raise_exception_ra(env, EXCP00_DIVZ, GETPC());
378
    }
379
    env->regs[R_EAX] = r0;
380
    env->regs[R_EDX] = r1;
381
}
382
383
void HELPER(idivq_EAX)(CPUX86State *env, target_ulong t0)
384
{
385
    uint64_t r0, r1;
386
387
    if (t0 == 0) {
388
        raise_exception_ra(env, EXCP00_DIVZ, GETPC());
389
    }
390
    r0 = env->regs[R_EAX];
391
    r1 = env->regs[R_EDX];
392
    if (idiv64(&r0, &r1, t0)) {
393
        raise_exception_ra(env, EXCP00_DIVZ, GETPC());
394
    }
395
    env->regs[R_EAX] = r0;
396
    env->regs[R_EDX] = r1;
397
}
398
#endif
399
400
#if TARGET_LONG_BITS == 32
401
0
# define ctztl  ctz32
402
# define clztl  clz32
403
#else
404
# define ctztl  ctz64
405
# define clztl  clz64
406
#endif
407
408
target_ulong HELPER(pdep)(target_ulong src, target_ulong mask)
409
0
{
410
0
    target_ulong dest = 0;
411
0
    int i, o;
412
413
0
    for (i = 0; mask != 0; i++) {
414
0
        o = ctztl(mask);
415
0
        mask &= mask - 1;
416
0
        dest |= ((src >> i) & 1) << o;
417
0
    }
418
0
    return dest;
419
0
}
420
421
target_ulong HELPER(pext)(target_ulong src, target_ulong mask)
422
0
{
423
0
    target_ulong dest = 0;
424
0
    int i, o;
425
426
0
    for (o = 0; mask != 0; o++) {
427
0
        i = ctztl(mask);
428
0
        mask &= mask - 1;
429
0
        dest |= ((src >> i) & 1) << o;
430
0
    }
431
0
    return dest;
432
0
}
433
434
/* Test that BIT is enabled in CR4.  If not, raise an illegal opcode
435
   exception.  This reduces the requirements for rare CR4 bits being
436
   mapped into HFLAGS.  */
437
void HELPER(cr4_testbit)(CPUX86State *env, uint32_t bit)
438
0
{
439
0
    if (unlikely((env->cr[4] & bit) == 0)) {
440
0
        raise_exception_ra(env, EXCP06_ILLOP, GETPC());
441
0
    }
442
0
}
443
444
target_ulong HELPER(rdrand)(CPUX86State *env)
445
0
{
446
0
    Error *err = NULL;
447
0
    target_ulong ret;
448
449
0
    if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
450
0
        qemu_log_mask(LOG_UNIMP, "rdrand: Crypto failure: %s",
451
0
                      error_get_pretty(err));
452
0
        error_free(err);
453
        /* Failure clears CF and all other flags, and returns 0.  */
454
0
        env->cc_src = 0;
455
0
        ret = 0;
456
0
    } else {
457
        /* Success sets CF and clears all others.  */
458
0
        env->cc_src = CC_C;
459
0
    }
460
0
    env->cc_op = CC_OP_EFLAGS;
461
0
    return ret;
462
0
}