From 4e61c4bb175d97f85f252977ac867f98c0ba8268 Mon Sep 17 00:00:00 2001 From: Jiang Yuxuan Date: Fri, 16 Oct 2026 22:04:40 -0400 Subject: [PATCH 01/10] target/i386: raise #DE for INT_MIN / -1 in IDIV helpers helper_idivw_AX() and helper_idivl_EAX() perform the division before checking whether the quotient fits in the destination. If the dividend is the minimum value and the divisor is -1, the division overflows in the host: INT32_MIN / -1 and INT64_MIN / -1 are undefined behaviour in C and trap with SIGFPE on x86 hosts. QEMU then dies of a host SIGFPE instead of raising #DE in the guest, taking down the emulator (or the whole VM under TCG). Reject the two overflowing cases before dividing. The 8-bit form is not affected (it widens the dividend to int), and the 64-bit form already checks first. Add a regression test for both the 16-bit and 32-bit forms, run by the i386 and x86_64 linux-user test suites. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4687 Signed-off-by: fujo Link: https://lore.kernel.org/r/CAAt7E3+jVR+YHZC3996kdC_Xrh9aVJ4YLy8X7+1HyjvOQk09Ug@mail.gmail.com Signed-off-by: Paolo Bonzini --- target/i386/tcg/int_helper.c | 4 +- tests/tcg/i386/meson.build | 1 + tests/tcg/i386/test-idiv-overflow.c | 70 +++++++++++++++++++++++++++++ tests/tcg/x86_64/meson.build | 1 + 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 tests/tcg/i386/test-idiv-overflow.c diff --git a/target/i386/tcg/int_helper.c b/target/i386/tcg/int_helper.c index 772e6adcc8..d4fa89166b 100644 --- a/target/i386/tcg/int_helper.c +++ b/target/i386/tcg/int_helper.c @@ -91,7 +91,7 @@ void HELPER(idivw_AX)(CPUX86State *env, target_ulong t0) num = (env->regs[R_EAX] & 0xffff) | ((env->regs[R_EDX] & 0xffff) << 16); den = (int16_t)t0; - if (den == 0) { + if (den == 0 || (num == INT32_MIN && den == -1)) { raise_exception_ra(env, EXCP00_DIVZ, GETPC()); } q = (num / den); @@ -130,7 +130,7 @@ void HELPER(idivl_EAX)(CPUX86State *env, target_ulong t0) num = ((uint32_t)env->regs[R_EAX]) | ((uint64_t)((uint32_t)env->regs[R_EDX]) << 32); den = t0; - if (den == 0) { + if (den == 0 || (num == INT64_MIN && den == -1)) { raise_exception_ra(env, EXCP00_DIVZ, GETPC()); } q = (num / den); diff --git a/tests/tcg/i386/meson.build b/tests/tcg/i386/meson.build index e7c3095860..648b516582 100644 --- a/tests/tcg/i386/meson.build +++ b/tests/tcg/i386/meson.build @@ -35,6 +35,7 @@ tests += { 'cflags': [files('test-i386-code16.S'), files('test-i386-vm86.S'), '-lm', '-fno-pie', '-Wl,--no-warn-rwx-segments'], }, + 'test-idiv-overflow.c': {}, '../multiarch/sha512.c': { 'exe_name': 'sha512-sse', 'cflags': ['-msse4.1', '-O3'], diff --git a/tests/tcg/i386/test-idiv-overflow.c b/tests/tcg/i386/test-idiv-overflow.c new file mode 100644 index 0000000000..e8fc55a048 --- /dev/null +++ b/tests/tcg/i386/test-idiv-overflow.c @@ -0,0 +1,70 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Test that IDIV of the minimum dividend by -1 raises #DE, which Linux + * delivers to the guest as SIGFPE with si_code == FPE_INTDIV. + */ +#define _GNU_SOURCE +#include +#include +#include +#include + +static sigjmp_buf idiv_jmp; +static volatile sig_atomic_t idiv_si_code; + +static void idiv_sig_handler(int sig, siginfo_t *info, void *puc) +{ + (void)sig; + (void)puc; + idiv_si_code = info->si_code; + siglongjmp(idiv_jmp, 1); +} + +static int check_idiv_overflow(int width) +{ + struct sigaction sa, old; + int ret = 1; + + memset(&sa, 0, sizeof(sa)); + sa.sa_sigaction = idiv_sig_handler; + sa.sa_flags = SA_SIGINFO | SA_NODEFER; + sigemptyset(&sa.sa_mask); + sigaction(SIGFPE, &sa, &old); + + idiv_si_code = 0; + if (sigsetjmp(idiv_jmp, 1) == 0) { + if (width == 16) { + /* DX:AX = 0x8000:0x0000, divisor -1 */ + __asm__ volatile ("movl $0x8000, %%edx\n\t" + "xorl %%eax, %%eax\n\t" + "movl $-1, %%ecx\n\t" + "idivw %%cx" + : : : "eax", "ecx", "edx", "cc"); + } else { + /* EDX:EAX = 0x80000000:0x00000000, divisor -1 */ + __asm__ volatile ("movl $0x80000000, %%edx\n\t" + "xorl %%eax, %%eax\n\t" + "movl $-1, %%ecx\n\t" + "idivl %%ecx" + : : : "eax", "ecx", "edx", "cc"); + } + printf("FAIL: idiv%d did not raise #DE\n", width); + } else if (idiv_si_code != FPE_INTDIV) { + printf("FAIL: idiv%d raised SIGFPE with si_code %d\n", + width, (int)idiv_si_code); + } else { + ret = 0; + } + + sigaction(SIGFPE, &old, NULL); + return ret; +} + +int main(void) +{ + int ret = 0; + + ret |= check_idiv_overflow(16); + ret |= check_idiv_overflow(32); + return ret; +} diff --git a/tests/tcg/x86_64/meson.build b/tests/tcg/x86_64/meson.build index ad4d1f558a..fc7f48e98e 100644 --- a/tests/tcg/x86_64/meson.build +++ b/tests/tcg/x86_64/meson.build @@ -48,6 +48,7 @@ tests += { }, '../i386/test-i386-adcox.c': {'cflags': ['-O2']}, '../i386/test-i386-bmi2.c': {'cflags': ['-O2', '-fwrapv']}, + '../i386/test-idiv-overflow.c': {}, } tests += { From 46edf3255b7a78b071335551151875f5d78356dd Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Fri, 9 Oct 2026 20:41:42 +0200 Subject: [PATCH 02/10] target/i386/tcg: change popl_esp_hack to an X86InsnSpecial Signed-off-by: Paolo Bonzini --- target/i386/tcg/decode-new.c.inc | 8 +++----- target/i386/tcg/decode-new.h | 6 ++++++ target/i386/tcg/translate.c | 1 - 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/target/i386/tcg/decode-new.c.inc b/target/i386/tcg/decode-new.c.inc index 3845fab5c8..2756953c74 100644 --- a/target/i386/tcg/decode-new.c.inc +++ b/target/i386/tcg/decode-new.c.inc @@ -1615,8 +1615,7 @@ static void decode_group1A(DisasContext *s, CPUX86State *env, X86OpEntry *entry, *entry = UNKNOWN_OPCODE; } else { entry->gen = gen_POP; - /* The address must use the value of ESP after the pop. */ - s->popl_esp_hack = 1 << mo_pushpop(s, s->dflag); + entry->special = X86_SPECIAL_PopESP; } } @@ -2360,8 +2359,8 @@ static void decode_modrm(DisasContext *s, CPUX86State *env, } /* For correct popl handling with esp. */ - if (base == R_ESP && s->popl_esp_hack) { - disp += s->popl_esp_hack; + if (base == R_ESP && decode->e.special == X86_SPECIAL_PopESP) { + disp += 1 << mo_pushpop(s, s->dflag); } if (base == R_EBP || base == R_ESP) { def_seg = R_SS; @@ -3287,7 +3286,6 @@ static void disas_insn(DisasContext *s, CPUState *cpu) s->pc = s->base.pc_next; s->override = -1; - s->popl_esp_hack = 0; #ifdef TARGET_X86_64 s->rex_r = 0; s->rex_x = 0; diff --git a/target/i386/tcg/decode-new.h b/target/i386/tcg/decode-new.h index 5f76b0c1b7..a823143bbd 100644 --- a/target/i386/tcg/decode-new.h +++ b/target/i386/tcg/decode-new.h @@ -242,6 +242,12 @@ typedef enum X86InsnSpecial { /* Memory operand size of MOV from segment register is MO_16 */ X86_SPECIAL_Op0_Mw, + + /* + * If the address uses ESP as the base register, use the value + * of ESP after the pop. + */ + X86_SPECIAL_PopESP, } X86InsnSpecial; /* diff --git a/target/i386/tcg/translate.c b/target/i386/tcg/translate.c index 5199c330ac..b2cbce046c 100644 --- a/target/i386/tcg/translate.c +++ b/target/i386/tcg/translate.c @@ -108,7 +108,6 @@ typedef struct DisasContext { uint8_t vex_v; /* vex vvvv register, without 1's complement. */ bool vex_ndd; /* is this a 3-operand instruction? */ bool vex_w; /* used by AVX even on 32-bit processors */ - uint8_t popl_esp_hack; /* for correct popl with esp base handling */ uint8_t rip_offset; /* only used in x86_64, but left for simplicity */ uint8_t rex_r; /* 0 for i386, but left for simplicity */ From d1fe018af8bd14fd974767d07fb7f6e010b83e54 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Mon, 12 Oct 2026 09:33:58 +0200 Subject: [PATCH 03/10] target/i386/tcg: inline gen_leave The function is a vestige of the old switch-based decoder and does not add anything these days. Signed-off-by: Paolo Bonzini --- target/i386/tcg/emit.c.inc | 11 ++++++++++- target/i386/tcg/translate.c | 14 -------------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/target/i386/tcg/emit.c.inc b/target/i386/tcg/emit.c.inc index ed56edf8a3..a1ae582174 100644 --- a/target/i386/tcg/emit.c.inc +++ b/target/i386/tcg/emit.c.inc @@ -2559,7 +2559,16 @@ static void gen_LEA(DisasContext *s, X86DecodedInsn *decode) static void gen_LEAVE(DisasContext *s, X86DecodedInsn *decode) { - gen_leave(s); + MemOp d_ot = mo_pushpop(s, s->dflag); + MemOp a_ot = mo_stacksize(s); + + gen_lea_ss_ofs(s, s->A0, cpu_regs[R_EBP], 0); + gen_op_ld_v(s, d_ot, s->T0, s->A0); + + tcg_gen_addi_tl(s->T1, cpu_regs[R_EBP], 1 << d_ot); + + gen_op_mov_reg_v(s, d_ot, R_EBP, s->T0); + gen_op_mov_reg_v(s, a_ot, R_ESP, s->T1); } static void gen_LES(DisasContext *s, X86DecodedInsn *decode) diff --git a/target/i386/tcg/translate.c b/target/i386/tcg/translate.c index b2cbce046c..3d542acd97 100644 --- a/target/i386/tcg/translate.c +++ b/target/i386/tcg/translate.c @@ -2005,20 +2005,6 @@ static void gen_enter(DisasContext *s, int esp_addend, int level) gen_op_mov_reg_v(s, a_ot, R_ESP, s->T1); } -static void gen_leave(DisasContext *s) -{ - MemOp d_ot = mo_pushpop(s, s->dflag); - MemOp a_ot = mo_stacksize(s); - - gen_lea_ss_ofs(s, s->A0, cpu_regs[R_EBP], 0); - gen_op_ld_v(s, d_ot, s->T0, s->A0); - - tcg_gen_addi_tl(s->T1, cpu_regs[R_EBP], 1 << d_ot); - - gen_op_mov_reg_v(s, d_ot, R_EBP, s->T0); - gen_op_mov_reg_v(s, a_ot, R_ESP, s->T1); -} - /* an interrupt is different from an exception because of the privilege checks */ static void gen_interrupt(DisasContext *s, uint8_t intno) From 9f94fd68c89bed9bd59019a0bb2ad7d2e5458bcf Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Sat, 10 Oct 2026 12:00:17 +0200 Subject: [PATCH 04/10] target/i386/tcg: reject VEX.NP for MMX instructions MMX instructions do not have a VEX encoding, so reject VEX instructions with no prefix for instructions that use X86_SPECIAL_MMX. Cc: qemu-stable@nongnu.org Signed-off-by: Paolo Bonzini --- target/i386/tcg/decode-new.c.inc | 33 +++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/target/i386/tcg/decode-new.c.inc b/target/i386/tcg/decode-new.c.inc index 2756953c74..65fd402c17 100644 --- a/target/i386/tcg/decode-new.c.inc +++ b/target/i386/tcg/decode-new.c.inc @@ -2425,6 +2425,12 @@ static void decode_modrm(DisasContext *s, CPUX86State *env, decode->mem = (AddressParts){ def_seg, base, index, scale, disp }; } +static bool is_mmx(const DisasContext *s, const X86OpEntry *e) +{ + return (e->special == X86_SPECIAL_MMX && + !(s->prefix & (PREFIX_REPZ | PREFIX_REPNZ | PREFIX_DATA))); +} + static bool decode_op_size(DisasContext *s, X86OpEntry *e, X86OpSize size, MemOp *ot) { switch (size) { @@ -2470,8 +2476,7 @@ static bool decode_op_size(DisasContext *s, X86OpEntry *e, X86OpSize size, MemOp return true; case X86_SIZE_dq: /* SSE/AVX 128-bit */ - if (e->special == X86_SPECIAL_MMX && - !(s->prefix & (PREFIX_DATA | PREFIX_REPZ | PREFIX_REPNZ))) { + if (is_mmx(s, e)) { *ot = MO_64; return true; } @@ -2489,8 +2494,7 @@ static bool decode_op_size(DisasContext *s, X86OpEntry *e, X86OpSize size, MemOp return true; case X86_SIZE_x: /* 128/256-bit, based on operand size */ - if (e->special == X86_SPECIAL_MMX && - !(s->prefix & (PREFIX_DATA | PREFIX_REPZ | PREFIX_REPNZ))) { + if (is_mmx(s, e)) { *ot = MO_64; return true; } @@ -2605,8 +2609,7 @@ static bool decode_op(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode, goto get_reg; case X86_TYPE_V: /* reg in the modrm byte selects an XMM/YMM register */ - if (decode->e.special == X86_SPECIAL_MMX && - !(s->prefix & (PREFIX_DATA | PREFIX_REPZ | PREFIX_REPNZ))) { + if (is_mmx(s, &decode->e)) { op->unit = X86_OP_MMX; } else { op->unit = X86_OP_SSE; @@ -2625,8 +2628,7 @@ static bool decode_op(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode, goto get_modrm; case X86_TYPE_W: /* XMM/YMM modrm operand */ - if (decode->e.special == X86_SPECIAL_MMX && - !(s->prefix & (PREFIX_DATA | PREFIX_REPZ | PREFIX_REPNZ))) { + if (is_mmx(s, &decode->e)) { op->unit = X86_OP_MMX; } else { op->unit = X86_OP_SSE; @@ -2638,8 +2640,7 @@ static bool decode_op(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode, goto get_modrm_reg; case X86_TYPE_U: /* R/M in the modrm byte selects an XMM/YMM register */ - if (decode->e.special == X86_SPECIAL_MMX && - !(s->prefix & (PREFIX_DATA | PREFIX_REPZ | PREFIX_REPNZ))) { + if (is_mmx(s, &decode->e)) { op->unit = X86_OP_MMX; } else { op->unit = X86_OP_SSE; @@ -3135,12 +3136,15 @@ static bool validate_vex(DisasContext *s, X86DecodedInsn *decode) case 4: case 5: case 7: - if (s->prefix & PREFIX_VEX) { + if (is_mmx(s, e)) { + if (s->prefix & PREFIX_VEX) { + goto illegal; + } + } else if (s->prefix & PREFIX_VEX) { if (!(s->flags & HF_AVX_EN_MASK)) { goto illegal; } - } else if (e->special != X86_SPECIAL_MMX || - (s->prefix & (PREFIX_REPZ | PREFIX_REPNZ | PREFIX_DATA))) { + } else { if (!(s->flags & HF_OSFXSR_MASK)) { goto illegal; } @@ -3694,8 +3698,7 @@ static void disas_insn(DisasContext *s, CPUState *cpu) } } - if (decode.e.special == X86_SPECIAL_MMX && - !(s->prefix & (PREFIX_REPZ | PREFIX_REPNZ | PREFIX_DATA))) { + if (is_mmx(s, &decode.e)) { gen_helper_enter_mmx(tcg_env); } From f4c828b6330934aa887ad721de8501ac704cefc3 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Sat, 17 Oct 2026 09:53:54 +0200 Subject: [PATCH 05/10] target/i386/tcg: remove X and Y operand types Since the Intel SDM is moving away from operand types, remove more of the implicit types; X and Y are meant for string instructions but the DS:SI and ES:DI addressing modes are handled entirely in the generator functions; the operands were just placeholders to distinguish byte string operations from s->dflag string operations. Note that this moves the port for INS and OUTS from s->T1 to s->T0. This frees up some operand letters that could be useful in the future, for example to allocate K/X/Y to mask registers in the same guise as H/V/W for AVX. Signed-off-by: Paolo Bonzini --- target/i386/tcg/decode-new.c.inc | 30 ++++++++++++++---------------- target/i386/tcg/decode-new.h | 2 -- target/i386/tcg/emit.c.inc | 18 +++++++++--------- target/i386/tcg/translate.c | 6 ++++++ 4 files changed, 29 insertions(+), 27 deletions(-) diff --git a/target/i386/tcg/decode-new.c.inc b/target/i386/tcg/decode-new.c.inc index 65fd402c17..7e5ece380a 100644 --- a/target/i386/tcg/decode-new.c.inc +++ b/target/i386/tcg/decode-new.c.inc @@ -1810,10 +1810,10 @@ static const X86OpEntry opcodes_root[256] = { [0xA1] = X86_OP_ENTRY3(MOV, 0,v, O,v, None, None), /* rAX, Ov */ [0xA2] = X86_OP_ENTRY3(MOV, O,b, 0,b, None, None), /* Ob, AL */ [0xA3] = X86_OP_ENTRY3(MOV, O,v, 0,v, None, None), /* Ov, rAX */ - [0xA4] = X86_OP_ENTRYrr(MOVS, Y,b, X,b), - [0xA5] = X86_OP_ENTRYrr(MOVS, Y,v, X,v), - [0xA6] = X86_OP_ENTRYrr(CMPS, Y,b, X,b), - [0xA7] = X86_OP_ENTRYrr(CMPS, Y,v, X,v), + [0xA4] = X86_OP_ENTRY0(MOVS), + [0xA5] = X86_OP_ENTRY0(MOVS), + [0xA6] = X86_OP_ENTRY0(CMPS), + [0xA7] = X86_OP_ENTRY0(CMPS), [0xB0] = X86_OP_ENTRY3(MOV, LoBits,b, I,b, None, None), [0xB1] = X86_OP_ENTRY3(MOV, LoBits,b, I,b, None, None), @@ -1916,10 +1916,10 @@ static const X86OpEntry opcodes_root[256] = { [0x69] = X86_OP_ENTRY3(IMUL3, G,v, E,v, I,z, sextT0), [0x6A] = X86_OP_ENTRYr(PUSH, I,b), [0x6B] = X86_OP_ENTRY3(IMUL3, G,v, E,v, I,b, sextT0), - [0x6C] = X86_OP_ENTRYrr(INS, Y,b, 2,w), /* DX */ - [0x6D] = X86_OP_ENTRYrr(INS, Y,z, 2,w), /* DX */ - [0x6E] = X86_OP_ENTRYrr(OUTS, X,b, 2,w), /* DX */ - [0x6F] = X86_OP_ENTRYrr(OUTS, X,z, 2,w), /* DX */ + [0x6C] = X86_OP_ENTRYr(INS, 2,w), /* DX */ + [0x6D] = X86_OP_ENTRYr(INS, 2,w), /* DX */ + [0x6E] = X86_OP_ENTRYr(OUTS, 2,w), /* DX */ + [0x6F] = X86_OP_ENTRYr(OUTS, 2,w), /* DX */ [0x78] = X86_OP_ENTRYr(Jcc, J,b), [0x79] = X86_OP_ENTRYr(Jcc, J,b), @@ -1951,13 +1951,13 @@ static const X86OpEntry opcodes_root[256] = { [0xA8] = X86_OP_ENTRYrr(TEST, 0,b, I,b), /* AL, Ib */ [0xA9] = X86_OP_ENTRYrr(TEST, 0,v, I,z), /* rAX, Iz */ - [0xAA] = X86_OP_ENTRYwr(STOS, Y,b, 0,b), - [0xAB] = X86_OP_ENTRYwr(STOS, Y,v, 0,v), + [0xAA] = X86_OP_ENTRYr(STOS, 0,b), + [0xAB] = X86_OP_ENTRYr(STOS, 0,v), /* Manual writeback because REP LODS (!) has to write EAX/RAX after every LODS. */ - [0xAC] = X86_OP_ENTRYr(LODS, X,b), - [0xAD] = X86_OP_ENTRYr(LODS, X,v), - [0xAE] = X86_OP_ENTRYrr(SCAS, 0,b, Y,b), - [0xAF] = X86_OP_ENTRYrr(SCAS, 0,v, Y,v), + [0xAC] = X86_OP_ENTRY0(LODS), + [0xAD] = X86_OP_ENTRY0(LODS), + [0xAE] = X86_OP_ENTRYr(SCAS, 0,b), + [0xAF] = X86_OP_ENTRYr(SCAS, 0,v), [0xB8] = X86_OP_ENTRYwr(MOV, LoBits,v, I,v), [0xB9] = X86_OP_ENTRYwr(MOV, LoBits,v, I,v), @@ -2552,8 +2552,6 @@ static bool decode_op(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode, switch (type) { case X86_TYPE_None: /* Implicit or absent */ case X86_TYPE_F: /* EFLAGS/RFLAGS */ - case X86_TYPE_X: /* string source */ - case X86_TYPE_Y: /* string destination */ break; case X86_TYPE_B: /* VEX.vvvv selects a GPR, else use modrm */ diff --git a/target/i386/tcg/decode-new.h b/target/i386/tcg/decode-new.h index a823143bbd..2218a4eda3 100644 --- a/target/i386/tcg/decode-new.h +++ b/target/i386/tcg/decode-new.h @@ -41,8 +41,6 @@ typedef enum X86OpType { X86_TYPE_U, /* R/M in the modrm byte selects an XMM/YMM register */ X86_TYPE_V, /* reg in the modrm byte selects an XMM/YMM register */ X86_TYPE_W, /* XMM/YMM modrm operand */ - X86_TYPE_X, /* string source */ - X86_TYPE_Y, /* string destination */ /* Custom */ X86_TYPE_EM, /* modrm byte selects an ALU memory operand */ diff --git a/target/i386/tcg/emit.c.inc b/target/i386/tcg/emit.c.inc index a1ae582174..5754f3dcc8 100644 --- a/target/i386/tcg/emit.c.inc +++ b/target/i386/tcg/emit.c.inc @@ -1923,7 +1923,7 @@ static void gen_CMPccXADD(DisasContext *s, X86DecodedInsn *decode) static void gen_CMPS(DisasContext *s, X86DecodedInsn *decode) { - MemOp ot = decode->op[2].ot; + MemOp ot = decode->b & 1 ? s->dflag : MO_8; gen_repz_nz(s, ot, gen_cmps); } @@ -2388,10 +2388,10 @@ static void gen_INC(DisasContext *s, X86DecodedInsn *decode) static void gen_INS(DisasContext *s, X86DecodedInsn *decode) { - MemOp ot = decode->op[1].ot; + MemOp ot = decode->b & 1 ? mo_ins_outs(s) : MO_8; TCGv_i32 port = tcg_temp_new_i32(); - tcg_gen_trunc_tl_i32(port, s->T1); + tcg_gen_trunc_tl_i32(port, s->T0); tcg_gen_ext16u_i32(port, port); if (!gen_check_io(s, ot, port, SVM_IOIO_TYPE_MASK | SVM_IOIO_STR_MASK)) { @@ -2593,7 +2593,7 @@ static void gen_LGS(DisasContext *s, X86DecodedInsn *decode) static void gen_LODS(DisasContext *s, X86DecodedInsn *decode) { - MemOp ot = decode->op[1].ot; + MemOp ot = decode->b & 1 ? s->dflag : MO_8; gen_repz(s, ot, gen_lods); } @@ -2803,7 +2803,7 @@ static void gen_MOVq_dq(DisasContext *s, X86DecodedInsn *decode) static void gen_MOVS(DisasContext *s, X86DecodedInsn *decode) { - MemOp ot = decode->op[2].ot; + MemOp ot = decode->b & 1 ? s->dflag : MO_8; gen_repz(s, ot, gen_movs); } @@ -2960,10 +2960,10 @@ static void gen_OUT(DisasContext *s, X86DecodedInsn *decode) static void gen_OUTS(DisasContext *s, X86DecodedInsn *decode) { - MemOp ot = decode->op[1].ot; + MemOp ot = decode->b & 1 ? mo_ins_outs(s) : MO_8; TCGv_i32 port = tcg_temp_new_i32(); - tcg_gen_trunc_tl_i32(port, s->T1); + tcg_gen_trunc_tl_i32(port, s->T0); tcg_gen_ext16u_i32(port, port); if (!gen_check_io(s, ot, port, SVM_IOIO_STR_MASK)) { return; @@ -4128,7 +4128,7 @@ static void gen_SBB(DisasContext *s, X86DecodedInsn *decode) static void gen_SCAS(DisasContext *s, X86DecodedInsn *decode) { - MemOp ot = decode->op[2].ot; + MemOp ot = decode->b & 1 ? s->dflag : MO_8; gen_repz_nz(s, ot, gen_scas); } @@ -4332,7 +4332,7 @@ static void gen_STMXCSR(DisasContext *s, X86DecodedInsn *decode) static void gen_STOS(DisasContext *s, X86DecodedInsn *decode) { - MemOp ot = decode->op[1].ot; + MemOp ot = decode->b & 1 ? s->dflag : MO_8; gen_repz(s, ot, gen_stos); } diff --git a/target/i386/tcg/translate.c b/target/i386/tcg/translate.c index 3d542acd97..b715921c37 100644 --- a/target/i386/tcg/translate.c +++ b/target/i386/tcg/translate.c @@ -403,6 +403,12 @@ static inline bool byte_reg_is_xH(DisasContext *s, int reg) return true; } +/* Select the size of an INS/OUTS operation ("z" operand size). */ +static inline MemOp mo_ins_outs(DisasContext *s) +{ + return s->dflag == MO_16 ? MO_16 : MO_32; +} + /* Select the size of a push/pop operation. */ static inline MemOp mo_pushpop(DisasContext *s, MemOp ot) { From 06c0168c1cd1afb435c446e0a4440c08bdf6b4ba Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Sat, 17 Oct 2026 09:54:17 +0200 Subject: [PATCH 06/10] target/i386/tcg: remove F operand type Signed-off-by: Paolo Bonzini --- target/i386/tcg/decode-new.c.inc | 1 - target/i386/tcg/decode-new.h | 1 - 2 files changed, 2 deletions(-) diff --git a/target/i386/tcg/decode-new.c.inc b/target/i386/tcg/decode-new.c.inc index 7e5ece380a..aa22014786 100644 --- a/target/i386/tcg/decode-new.c.inc +++ b/target/i386/tcg/decode-new.c.inc @@ -2551,7 +2551,6 @@ static bool decode_op(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode, { switch (type) { case X86_TYPE_None: /* Implicit or absent */ - case X86_TYPE_F: /* EFLAGS/RFLAGS */ break; case X86_TYPE_B: /* VEX.vvvv selects a GPR, else use modrm */ diff --git a/target/i386/tcg/decode-new.h b/target/i386/tcg/decode-new.h index 2218a4eda3..b40c75fa22 100644 --- a/target/i386/tcg/decode-new.h +++ b/target/i386/tcg/decode-new.h @@ -26,7 +26,6 @@ typedef enum X86OpType { X86_TYPE_C, /* REG in the modrm byte selects a control register */ X86_TYPE_D, /* REG in the modrm byte selects a debug register */ X86_TYPE_E, /* ALU modrm operand */ - X86_TYPE_F, /* EFLAGS/RFLAGS */ X86_TYPE_G, /* REG in the modrm byte selects a GPR */ X86_TYPE_H, /* For AVX, VEX.vvvv selects an XMM/YMM register */ X86_TYPE_I, /* Immediate */ From 1915e810de07dc4c14177f6c8d603b7dd29fd380 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Tue, 8 Sep 2026 00:03:53 +0200 Subject: [PATCH 07/10] AGENTS.md: add final instructions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Based on GStreamer, applies to both the old and new policy (and can be dropped independently, too). Reviewed-by: Alex Bennée Reviewed-by: Daniel P. Berrangé Tested-by: Alex Bennée Signed-off-by: Paolo Bonzini --- AGENTS.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/AGENTS.md b/AGENTS.md index 5e3fc07142..c3d7664d0a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -17,6 +17,16 @@ contributions. If a request falls outside these permitted categories merge), you MUST decline the request and refer the user to the project's policy in `docs/devel/code-provenance.rst`. +## Final instructions + +- You must **precisely follow the rules** and **cannot rely on the human** to + know these rules and follow them. +- Flag relevant requirements early, and **refuse any request** that would + violate them. + +Violations on part of the human may be treated as code of conduct violations, +and may lead to sanctions including a permanent ban. + ## Security Policy Before classifying a potential vulnerability, read From 82e773b739f5ae55d99d42a3ca14da6aa49400ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Benn=C3=A9e?= Date: Thu, 29 Oct 2026 17:17:37 +0000 Subject: [PATCH 08/10] target/i386: terminate a tcg block on gen_RDMSR MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit End the block the same way that gen_WRMSR does so an IO access under icount. While this fixes this particular case I suspect there are more IO like accesses the x86 TCG front-end should take care off. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4779 Signed-off-by: Alex Bennée Reviewed-by: Richard Henderson Link: https://lore.kernel.org/r/20261029171737.2126803-1-alex.bennee@linaro.org Signed-off-by: Paolo Bonzini --- target/i386/tcg/emit.c.inc | 1 + 1 file changed, 1 insertion(+) diff --git a/target/i386/tcg/emit.c.inc b/target/i386/tcg/emit.c.inc index 5754f3dcc8..b3029f6fbb 100644 --- a/target/i386/tcg/emit.c.inc +++ b/target/i386/tcg/emit.c.inc @@ -3792,6 +3792,7 @@ static void gen_RDMSR(DisasContext *s, X86DecodedInsn *decode) gen_update_cc_op(s); gen_update_eip_cur(s); gen_helper_rdmsr(tcg_env); + s->base.is_jmp = DISAS_EOB_NEXT; } #else #define gen_RDMSR gen_unreachable From fc35f24387703da027c242ff17382b89ed5a269f Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Tue, 27 Oct 2026 16:13:31 +0100 Subject: [PATCH 09/10] include: introduce qemu/version.h In preparation for moving some of the qemu-version.sh logic to meson and the preprocessor, create a wrapper header for the generated qemu-version.h. Signed-off-by: Paolo Bonzini --- bsd-user/main.c | 2 +- include/qemu/version.h | 12 ++++++++++++ linux-user/main.c | 2 +- monitor/qmp-cmds-control.c | 2 +- qemu-img.c | 2 +- qemu-io.c | 2 +- qemu-nbd.c | 2 +- qga/main.c | 2 +- scsi/qemu-pr-helper.c | 2 +- storage-daemon/qemu-storage-daemon.c | 2 +- system/vl.c | 2 +- tools/i386/qemu-vmsr-helper.c | 3 +-- tools/qemu-vnc/qemu-vnc.c | 2 +- ui/cocoa.m | 2 +- util/module.c | 2 +- 15 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 include/qemu/version.h diff --git a/bsd-user/main.c b/bsd-user/main.c index 29ee59de2c..6720dbde6f 100644 --- a/bsd-user/main.c +++ b/bsd-user/main.c @@ -25,7 +25,7 @@ #include "qemu/help-texts.h" #include "qemu/units.h" #include "qemu/accel.h" -#include "qemu-version.h" +#include "qemu/version.h" #include #include "qapi/error.h" diff --git a/include/qemu/version.h b/include/qemu/version.h new file mode 100644 index 0000000000..38405495c0 --- /dev/null +++ b/include/qemu/version.h @@ -0,0 +1,12 @@ +/* + * QEMU version strings + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef QEMU_VERSION_H +#define QEMU_VERSION_H + +#include "qemu-version.h" + +#endif diff --git a/linux-user/main.c b/linux-user/main.c index 60a695b7ca..2f0c0a50c8 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -21,7 +21,7 @@ #include "qemu/help-texts.h" #include "qemu/units.h" #include "qemu/accel.h" -#include "qemu-version.h" +#include "qemu/version.h" #include #include #include diff --git a/monitor/qmp-cmds-control.c b/monitor/qmp-cmds-control.c index 8fe0876a3d..bc938712f3 100644 --- a/monitor/qmp-cmds-control.c +++ b/monitor/qmp-cmds-control.c @@ -25,7 +25,7 @@ #include "qemu/osdep.h" #include "monitor-internal.h" -#include "qemu-version.h" +#include "qemu/version.h" #include "qapi/compat-policy.h" #include "qapi/error.h" #include "qapi/qapi-commands-control.h" diff --git a/qemu-img.c b/qemu-img.c index edf75e43ab..766d8540bf 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -27,7 +27,7 @@ #include "qemu/help-texts.h" #include "qemu/qemu-progress.h" -#include "qemu-version.h" +#include "qemu/version.h" #include "qapi/error.h" #include "qapi/qapi-commands-block-core.h" #include "qapi/qapi-visit-block-core.h" diff --git a/qemu-io.c b/qemu-io.c index 598d5b1c9c..b5f2ae631a 100644 --- a/qemu-io.c +++ b/qemu-io.c @@ -34,7 +34,7 @@ #include "block/block_int.h" #include "trace/control.h" #include "crypto/init.h" -#include "qemu-version.h" +#include "qemu/version.h" #define CMD_NOFILE_OK 0x01 diff --git a/qemu-nbd.c b/qemu-nbd.c index fa28609d7d..4f7e965edb 100644 --- a/qemu-nbd.c +++ b/qemu-nbd.c @@ -46,7 +46,7 @@ #include "crypto/init.h" #include "crypto/tlscreds.h" #include "trace/control.h" -#include "qemu-version.h" +#include "qemu/version.h" #ifdef CONFIG_SELINUX #include diff --git a/qga/main.c b/qga/main.c index fd19c7037d..06358cb213 100644 --- a/qga/main.c +++ b/qga/main.c @@ -30,7 +30,7 @@ #include "qemu/help_option.h" #include "qemu/sockets.h" #include "qemu/systemd.h" -#include "qemu-version.h" +#include "qemu/version.h" #ifdef _WIN32 #include #include diff --git a/scsi/qemu-pr-helper.c b/scsi/qemu-pr-helper.c index 1f97a5c4bf..7629f36404 100644 --- a/scsi/qemu-pr-helper.c +++ b/scsi/qemu-pr-helper.c @@ -50,7 +50,7 @@ #include "qobject/qstring.h" #include "io/channel-socket.h" #include "trace/control.h" -#include "qemu-version.h" +#include "qemu/version.h" #include "block/thread-pool.h" diff --git a/storage-daemon/qemu-storage-daemon.c b/storage-daemon/qemu-storage-daemon.c index 50dbfbd97a..b2e29f0005 100644 --- a/storage-daemon/qemu-storage-daemon.c +++ b/storage-daemon/qemu-storage-daemon.c @@ -43,7 +43,7 @@ #include "qapi/qobject-input-visitor.h" #include "qemu/help-texts.h" -#include "qemu-version.h" +#include "qemu/version.h" #include "qemu/cutils.h" #include "qemu/config-file.h" #include "qemu/error-report.h" diff --git a/system/vl.c b/system/vl.c index 7ae6a8298f..e7b759d544 100644 --- a/system/vl.c +++ b/system/vl.c @@ -37,7 +37,7 @@ #include "qobject/qdict.h" #include "qobject/qstring.h" #include "qobject/qjson.h" -#include "qemu-version.h" +#include "qemu/version.h" #include "qemu/cutils.h" #include "qemu/help_option.h" #include "qemu/hw-version.h" diff --git a/tools/i386/qemu-vmsr-helper.c b/tools/i386/qemu-vmsr-helper.c index f12fd3c3e8..fac7fbe6b6 100644 --- a/tools/i386/qemu-vmsr-helper.c +++ b/tools/i386/qemu-vmsr-helper.c @@ -34,7 +34,7 @@ #include "qemu/module.h" #include "qemu/error-report.h" #include "qemu/config-file.h" -#include "qemu-version.h" +#include "qemu/version.h" #include "qapi/error.h" #include "qemu/error-report.h" #include "qemu/log.h" @@ -42,7 +42,6 @@ #include "io/channel.h" #include "io/channel-socket.h" #include "trace/control.h" -#include "qemu-version.h" #include "rapl-msr-index.h" #define MSR_PATH_TEMPLATE "/dev/cpu/%u/msr" diff --git a/tools/qemu-vnc/qemu-vnc.c b/tools/qemu-vnc/qemu-vnc.c index c915589ca7..a668f3f734 100644 --- a/tools/qemu-vnc/qemu-vnc.c +++ b/tools/qemu-vnc/qemu-vnc.c @@ -15,7 +15,7 @@ #include "qemu/option.h" #include "qemu/log.h" #include "qemu/main-loop.h" -#include "qemu-version.h" +#include "qemu/version.h" #include "ui/vnc.h" #include "crypto/secret.h" #include "crypto/tlscredsx509.h" diff --git a/ui/cocoa.m b/ui/cocoa.m index 4587547c98..36a72d3623 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -44,7 +44,7 @@ #include "qapi/qapi-commands-machine.h" #include "qapi/qapi-commands-misc.h" #include "system/blockdev.h" -#include "qemu-version.h" +#include "qemu/version.h" #include "qemu/cutils.h" #include "qemu/main-loop.h" #include "qemu/module.h" diff --git a/util/module.c b/util/module.c index 8f8e6994f1..6de9ff2e41 100644 --- a/util/module.c +++ b/util/module.c @@ -23,7 +23,7 @@ #include "qemu/config-file.h" #include "qapi/error.h" #ifdef CONFIG_MODULE_UPGRADES -#include "qemu-version.h" +#include "qemu/version.h" #endif #include "trace.h" From ab78ac76f44a1241e76fd32d248df62d83f1ea58 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Tue, 27 Oct 2026 15:51:39 +0100 Subject: [PATCH 10/10] meson: rewrite scripts/qemu-version.sh directly in meson.build Invoking git and creating a .h file does not need a shell script. Embed the logic directly in meson.build. Signed-off-by: Paolo Bonzini --- include/qemu/version.h | 8 ++++++++ meson.build | 29 ++++++++++++++++++++--------- scripts/qemu-version.h.in | 1 + scripts/qemu-version.sh | 25 ------------------------- 4 files changed, 29 insertions(+), 34 deletions(-) create mode 100644 scripts/qemu-version.h.in delete mode 100755 scripts/qemu-version.sh diff --git a/include/qemu/version.h b/include/qemu/version.h index 38405495c0..dd0f937f30 100644 --- a/include/qemu/version.h +++ b/include/qemu/version.h @@ -7,6 +7,14 @@ #ifndef QEMU_VERSION_H #define QEMU_VERSION_H +/* qemu-version.h defines QEMU_PKGVERSION only if it is not empty. */ #include "qemu-version.h" +#ifdef QEMU_PKGVERSION +#define QEMU_FULL_VERSION QEMU_VERSION " (" QEMU_PKGVERSION ")" +#else +#define QEMU_PKGVERSION "" +#define QEMU_FULL_VERSION QEMU_VERSION +#endif + #endif diff --git a/meson.build b/meson.build index 71f135f053..0258744d06 100644 --- a/meson.build +++ b/meson.build @@ -78,6 +78,7 @@ have_user = have_linux_user or have_bsd_user ############ sh = find_program('sh') +git = find_program('git', required: false) python = import('python').find_installation() cc = meson.get_compiler('c') @@ -3551,15 +3552,25 @@ tracetool_depends = files( 'scripts/tracetool/__init__.py', ) -qemu_version_cmd = [find_program('scripts/qemu-version.sh'), - meson.current_source_dir(), - get_option('pkgversion'), meson.project_version()] -qemu_version = custom_target('qemu-version.h', - output: 'qemu-version.h', - command: qemu_version_cmd, - capture: true, - build_by_default: true, - build_always_stale: true) +qemu_pkgversion = get_option('pkgversion') +git_describe_version = [git, 'describe', '--match', 'v*', '--dirty'] +if qemu_pkgversion == '' and git.found() and fs.exists('.git') and \ + run_command(git_describe_version, check: false).returncode() == 0 + # Refresh the version at build time, so that "make" after a commit updates it. + qemu_version = vcs_tag(input: 'scripts/qemu-version.h.in', + output: 'qemu-version.h', + command: git_describe_version, + fallback: '') +else + # Unlike vcs_tag(), which always defines QEMU_PKGVERSION, leave it out if + # empty. include/qemu/version.h then omits the parentheses from + # QEMU_FULL_VERSION. + qemu_version_data = configuration_data() + if qemu_pkgversion != '' + qemu_version_data.set_quoted('QEMU_PKGVERSION', qemu_pkgversion) + endif + qemu_version = configure_file(output: 'qemu-version.h', configuration: qemu_version_data) +endif genh += qemu_version hxdep = [] diff --git a/scripts/qemu-version.h.in b/scripts/qemu-version.h.in new file mode 100644 index 0000000000..b62d4c2265 --- /dev/null +++ b/scripts/qemu-version.h.in @@ -0,0 +1 @@ +#define QEMU_PKGVERSION "@VCS_TAG@" diff --git a/scripts/qemu-version.sh b/scripts/qemu-version.sh deleted file mode 100755 index 3f6e7e6d41..0000000000 --- a/scripts/qemu-version.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/sh - -set -eu - -dir="$1" -pkgversion="$2" -version="$3" - -if [ -z "$pkgversion" ]; then - cd "$dir" - if [ -e .git ]; then - pkgversion=$(git describe --match 'v*' --dirty) || : - fi -fi - -if [ -n "$pkgversion" ]; then - fullversion="$version ($pkgversion)" -else - fullversion="$version" -fi - -cat <