Skip to contentSkip to conversation

Misc TCG & HW patches for 2026-11-17

GIT PULL Philippe Mathieu-Daudé 3 participants 26 messages Nov 16 – Nov 18

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

The following changes since commit 573577bff0138681ddd4ee2410c51bead7b53e74:

Merge tag 'pull-target-arm-20261116' of https://gitlab.com/pm215/qemu into staging (2026-11-16 12:09:43 -0800)

are available in the Git repository at:

https://github.com/philmd/qemu.git tags/hw-misc-20261117

for you to fetch changes up to 46baf870fa08abd39d4cfe918aa0f81a3281c7ea:

hw/virtio/virtio-iommu: Fix IOVA address lookup (2026-11-17 00:20:11 +0100)

Ignoring usual spurious checkpatch errors.

----------------------------------------------------------------
Misc fixes for 11.2

- Build system (obsolete Kconfig, QGA, illumos, macOS pvg)
- Fix wrong constant encoding in the vector .vi immediate emitters on RISC-V TCG
- USB device fixes (CCID card, U2F, MTP)
- m25p80, virtio-iommu, Xen backends
----------------------------------------------------------------

Bin Guo (1):
hw/usb/u2f: validate ring buffer indices on migration load

Brad Smith (1):
qga: fix uname() return value check in guest-get-osinfo

Chao Liu (1):
hw/block/m25p80: fix write enable latch after WRSR2

Eric Auger (1):
hw/virtio/virtio-iommu: Fix IOVA address lookup

John Snow (3):
build: remove vestigial CONFIG_SGA
build: update CONFIG_PVPANIC
hw/i386: move bios functions into x86-bios.c

Mohamed Mediouni (1):
pvg: disable by default

Peng Fan (1):
hw/xen: Mark legacy Xen backends as hotpluggable

Peter Maydell (1):
hw/usb/ccid-card-emulated: Use QemuSemaphore rather than QemuCond

Philippe Mathieu-Daudé (7):
tcg/riscv64: Factor encode_vi_raw() out of encode_vi() tcg/riscv64: Use a uimm5 encoder for vector shift immediates tcg/riscv64: Extract elements for vector arithmetic immediates tcg/riscv64: Fix constant handling in vector comparisons and selects tcg/riscv64: Fix logical vector immediate matching and emission tests/tcg/aarch64: Add vector immediate regression tests hw/usb/u2f-emulated: Use QemuSemaphore rather than QemuCond

Roman Bogorodskiy (2):
meson.build: fix getifaddrs() detection on illumos
qga: fix build on illumos

Tommaso Califano (3):
hw/usb/dev-mtp: Fix recursive delete of directories hw/usb/dev-mtp: Check length of class control request hw/usb/dev-mtp: Stop processing SendObjectInfo after an error

 configs/devices/i386-softmmu/default.mak |   6 +-
 meson.build                              |   2 +-
 tcg/riscv64/tcg-target-con-set.h         |   1 +
 tcg/riscv64/tcg-target-con-str.h         |   1 +
 hw/block/m25p80.c                        |   1 +
 hw/i386/x86-bios.c                       | 143 ++++++++++++++
 hw/i386/x86-common.c                     | 125 ------------
 hw/i386/x86.c                            |   1 -
 hw/usb/ccid-card-emulated.c              |  22 +--
 hw/usb/dev-mtp.c                         |  24 ++-
 hw/usb/u2f-emulated.c                    |  19 +-
 hw/usb/u2f.c                             |  31 +++
 hw/virtio/virtio-iommu.c                 |   2 +-
 hw/xen/xen-legacy-backend.c              |   7 +
 qga/commands-posix.c                     |   8 +-
 tests/tcg/aarch64/sve-vec-imm.c          | 233 +++++++++++++++++++++++
 tcg/riscv64/tcg-target.c.inc             | 116 ++++++++---
 hw/i386/meson.build                      |   1 +
 meson_options.txt                        |   2 +-
 tests/qtest/meson.build                  |   2 +-
 tests/tcg/aarch64/meson.build            |   1 +
 21 files changed, 555 insertions(+), 193 deletions(-)
 create mode 100644 hw/i386/x86-bios.c
 create mode 100644 tests/tcg/aarch64/sve-vec-imm.c
Signature · 1 line
2.55.0
#1 lore raw

[PULL 01/22] tcg/riscv64: Factor encode_vi_raw() out of encode_vi()

Move the OPIVI field layout into encode_vi_raw(), keeping the simm5 check in encode_vi(). The next commit adds a uimm5 variant for the vector shift instructions. No functional change.

Message-ID: <[email protected]>

---
 tcg/riscv64/tcg-target.c.inc | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
tcg/riscv64/tcg-target.c.inc +8 -2
index 6da52929a49b..319d5523fe7d 100644
@@ -535,12 +535,18 @@ static tcg_insn_unit encode_j(RISCVInsn opc, TCGReg rd, tcg_target_long imm)
 
 /* Type-OPIVI */
 
+static tcg_insn_unit encode_vi_raw(RISCVInsn opc, TCGReg rd, unsigned imm5,
+                                   TCGReg vs2, bool vm)
+{
+    return opc | (rd & 0x1f) << 7 | (imm5 & 0x1f) << 15 |
+           (vs2 & 0x1f) << 20 | (vm << 25);
+}
+
 static tcg_insn_unit encode_vi(RISCVInsn opc, TCGReg rd, tcg_target_long imm,
                                TCGReg vs2, bool vm)
 {
     tcg_debug_assert(imm == sextract32(imm, 0, 5));
-    return opc | (rd & 0x1f) << 7 | (imm & 0x1f) << 15 |
-           (vs2 & 0x1f) << 20 | (vm << 25);
+    return encode_vi_raw(opc, rd, imm, vs2, vm);
 }
 
 /* Type-OPIVV/OPMVV/OPIVX/OPMVX, Vector load and store */
Signature · 1 line
2.55.0
#2 lore raw

[PULL 03/22] tcg/riscv64: Extract elements for vector arithmetic immediates

In reply to Philippe Mathieu-Daudé

The .vi emitters pass the whole replicated constant (e.g. 0x0505050505050505) instead of one element. The encoding is still correct, because encode_vi() keeps the low 5 bits, but it trips the range assert added in 006352d69f14 on common vectorized guest code in debug builds. Extract the VECE element with vec_imm(), so the assert keeps checking that the emitter agrees with the constraint matcher.

Add vec_imm() to extract the VECE element, and use it.

Fixes 006352d69f14 ("tcg/riscv64: Assert immediate in range ...")

Message-ID: <[email protected]>

---
 tcg/riscv64/tcg-target.c.inc | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)
tcg/riscv64/tcg-target.c.inc +17 -6
index 002b3b4b780d..55685db44f3a 100644
@@ -736,6 +736,12 @@ static void tcg_out_opc_vi_u(TCGContext *s, RISCVInsn opc,
     tcg_out32(s, encode_vi_u(opc, vd, imm, vs2, true));
 }
 
+/* Constants are replicated by VECE; extract one element for .vi forms. */
+static int64_t vec_imm(TCGArg arg, unsigned vece)
+{
+    return sextract64(arg, 0, 8 << vece);
+}
+
 static void tcg_out_opc_vv_vi(TCGContext *s, RISCVInsn o_vv, RISCVInsn o_vi,
                               TCGReg vd, TCGReg vs2, TCGArg vi1, int c_vi1)
 {
@@ -2805,12 +2811,13 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
         break;
     case INDEX_op_add_vec:
         set_vtype_len_sew(s, type, vece);
-        tcg_out_opc_vv_vi(s, OPC_VADD_VV, OPC_VADD_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv_vi(s, OPC_VADD_VV, OPC_VADD_VI, a0, a1,
+                          c2 ? vec_imm(a2, vece) : a2, c2);
         break;
     case INDEX_op_sub_vec:
         set_vtype_len_sew(s, type, vece);
         if (const_args[1]) {
-            tcg_out_opc_vi(s, OPC_VRSUB_VI, a0, a2, a1);
+            tcg_out_opc_vi(s, OPC_VRSUB_VI, a0, a2, vec_imm(a1, vece));
         } else {
             tcg_out_opc_vv(s, OPC_VSUB_VV, a0, a1, a2);
         }
@@ -2841,19 +2848,23 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
         break;
     case INDEX_op_ssadd_vec:
         set_vtype_len_sew(s, type, vece);
-        tcg_out_opc_vv_vi(s, OPC_VSADD_VV, OPC_VSADD_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv_vi(s, OPC_VSADD_VV, OPC_VSADD_VI, a0, a1,
+                          c2 ? vec_imm(a2, vece) : a2, c2);
         break;
     case INDEX_op_sssub_vec:
         set_vtype_len_sew(s, type, vece);
-        tcg_out_opc_vv_vi(s, OPC_VSSUB_VV, OPC_VSSUB_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv_vi(s, OPC_VSSUB_VV, OPC_VSSUB_VI, a0, a1,
+                          c2 ? vec_imm(a2, vece) : a2, c2);
         break;
     case INDEX_op_usadd_vec:
         set_vtype_len_sew(s, type, vece);
-        tcg_out_opc_vv_vi(s, OPC_VSADDU_VV, OPC_VSADDU_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv_vi(s, OPC_VSADDU_VV, OPC_VSADDU_VI, a0, a1,
+                          c2 ? vec_imm(a2, vece) : a2, c2);
         break;
     case INDEX_op_ussub_vec:
         set_vtype_len_sew(s, type, vece);
-        tcg_out_opc_vv_vi(s, OPC_VSSUBU_VV, OPC_VSSUBU_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv_vi(s, OPC_VSSUBU_VV, OPC_VSSUBU_VI, a0, a1,
+                          c2 ? vec_imm(a2, vece) : a2, c2);
         break;
     case INDEX_op_smax_vec:
         set_vtype_len_sew(s, type, vece);
Signature · 1 line
2.55.0
#3 lore raw

[PULL 02/22] tcg/riscv64: Use a uimm5 encoder for vector shift immediates

In reply to Philippe Mathieu-Daudé

RVV shift .vi forms take an unsigned 5-bit immediate, but encode_vi() checks for simm5, so shift counts 16..31 trip the debug assertion (and are undefined behaviour in release builds).

Add encode_vi_u() and use it in tcg_out_vshifti().

Fixes 006352d69f14 ("tcg/riscv64: Assert immediate in range ...")

Message-ID: <[email protected]>

---
 tcg/riscv64/tcg-target.c.inc | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
tcg/riscv64/tcg-target.c.inc +16 -1
index 319d5523fe7d..002b3b4b780d 100644
@@ -542,6 +542,7 @@ static tcg_insn_unit encode_vi_raw(RISCVInsn opc, TCGReg rd, unsigned imm5,
            (vs2 & 0x1f) << 20 | (vm << 25);
 }
 
+/* OPIVI with a signed immediate (simm5). */
 static tcg_insn_unit encode_vi(RISCVInsn opc, TCGReg rd, tcg_target_long imm,
                                TCGReg vs2, bool vm)
 {
@@ -549,6 +550,14 @@ static tcg_insn_unit encode_vi(RISCVInsn opc, TCGReg rd, tcg_target_long imm,
     return encode_vi_raw(opc, rd, imm, vs2, vm);
 }
 
+/* OPIVI with an unsigned immediate (uimm5) */
+static tcg_insn_unit encode_vi_u(RISCVInsn opc, TCGReg rd, unsigned imm,
+                                 TCGReg vs2, bool vm)
+{
+    tcg_debug_assert(imm == extract32(imm, 0, 5));
+    return encode_vi_raw(opc, rd, imm, vs2, vm);
+}
+
 /* Type-OPIVV/OPMVV/OPIVX/OPMVX, Vector load and store */
 
 static tcg_insn_unit encode_v(RISCVInsn opc, TCGReg d, TCGReg s1,
@@ -721,6 +730,12 @@ static void tcg_out_opc_vi(TCGContext *s, RISCVInsn opc,
     tcg_out32(s, encode_vi(opc, vd, imm, vs2, true));
 }
 
+static void tcg_out_opc_vi_u(TCGContext *s, RISCVInsn opc,
+                             TCGReg vd, TCGReg vs2, unsigned imm)
+{
+    tcg_out32(s, encode_vi_u(opc, vd, imm, vs2, true));
+}
+
 static void tcg_out_opc_vv_vi(TCGContext *s, RISCVInsn o_vv, RISCVInsn o_vi,
                               TCGReg vd, TCGReg vs2, TCGArg vi1, int c_vi1)
 {
@@ -1579,7 +1594,7 @@ static void tcg_out_vshifti(TCGContext *s, RISCVInsn opc_vi, RISCVInsn opc_vx,
                              TCGReg dst, TCGReg src, unsigned imm)
 {
     if (imm < 32) {
-        tcg_out_opc_vi(s, opc_vi, dst, src, imm);
+        tcg_out_opc_vi_u(s, opc_vi, dst, src, imm);
     } else {
         tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_TMP0, imm);
         tcg_out_opc_vx(s, opc_vx, dst, src, TCG_REG_TMP0);
Signature · 1 line
2.55.0
#4 lore raw

[PULL 05/22] tcg/riscv64: Fix logical vector immediate matching and emission

In reply to Philippe Mathieu-Daudé

and/or/xor_vec are bitwise, so the generic layer doesn't track a meaningful VECE for them (it is always 0). The backend nevertheless matched the constant as a byte element, and emitted the .vi form at whatever SEW the previous op left behind:
"bic v4.8h, #0xf0" became "vand.vi ..., 15".

Add the 'N' constraint: find the smallest element size at which the constant replicates; the constant matches if that element fits simm5. The emitter then sets SEW to that size. A wider element can't fit when the smallest doesn't: its upper half repeats the smaller element, which would then be 0 or -1.

Message-ID: <[email protected]>

---
 tcg/riscv64/tcg-target-con-set.h |  1 +
 tcg/riscv64/tcg-target-con-str.h |  1 +
 tcg/riscv64/tcg-target.c.inc     | 53 +++++++++++++++++++++++++++-----
 3 files changed, 48 insertions(+), 7 deletions(-)
tcg/riscv64/tcg-target-con-set.h +1 -0
index 0fc26d3f98b4..9b4e219fac16 100644
@@ -26,4 +26,5 @@ C_O1_I2(v, v, v)
 C_O1_I2(v, vK, v)
 C_O1_I2(v, v, vK)
 C_O1_I2(v, v, vL)
+C_O1_I2(v, v, vN)
 C_O1_I4(v, v, vL, vK, vK)
tcg/riscv64/tcg-target-con-str.h +1 -0
index c04e15ddfa3c..d7e8b2ce4f2f 100644
@@ -19,3 +19,4 @@ CONST('I', TCG_CT_CONST_S12)
 CONST('K', TCG_CT_CONST_S5)
 CONST('L', TCG_CT_CONST_CMP_VI)
 CONST('M', TCG_CT_CONST_M12)
+CONST('N', TCG_CT_CONST_LOGICAL_VI)
tcg/riscv64/tcg-target.c.inc +46 -7

96 lines not on this page · raw message

Signature · 1 line
2.55.0
#5 lore raw

[PULL 04/22] tcg/riscv64: Fix constant handling in vector comparisons and selects

In reply to Philippe Mathieu-Daudé

tcg_out_cmpsel() takes constants as TCGReg and passes the whole replicated value to the .vi encoders instead of one element. The encoding is still correct, because encode_vi() keeps the low 5 bits, but it trips the range assert added in 006352d69f14 in debug builds. E.g. "cmlt v0.8h, #0" computes 0 - 1 in unsigned TCGReg, giving 0xffffffff instead of -1.

Use TCGArg for these operands and extract the element with vec_imm() before encoding, including before the LT/GE adjustment, so the assert keeps checking that the emitter agrees with the constraint matcher.

Fixes 006352d69f14 ("tcg/riscv64: Assert immediate in range ...")

Message-ID: <[email protected]>

---
 tcg/riscv64/tcg-target.c.inc | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
tcg/riscv64/tcg-target.c.inc +7 -6
index 55685db44f3a..902e600a1f51 100644
@@ -1556,9 +1556,9 @@ static void tcg_out_cltz(TCGContext *s, TCGType type, RISCVInsn insn,
 
 static void tcg_out_cmpsel(TCGContext *s, TCGType type, unsigned vece,
                            TCGCond cond, TCGReg ret,
-                           TCGReg cmp1, TCGReg cmp2, bool c_cmp2,
-                           TCGReg val1, bool c_val1,
-                           TCGReg val2, bool c_val2)
+                           TCGReg cmp1, TCGArg cmp2, bool c_cmp2,
+                           TCGArg val1, bool c_val1,
+                           TCGArg val2, bool c_val2)
 {
     set_vtype_len_sew(s, type, vece);
 
@@ -1575,7 +1575,7 @@ static void tcg_out_cmpsel(TCGContext *s, TCGType type, unsigned vece,
     /* Perform the comparison into V0 mask. */
     if (c_cmp2) {
         tcg_out_opc_vi(s, tcg_cmpcond_to_rvv_vi[cond].op, TCG_REG_V0, cmp1,
-                       cmp2 - tcg_cmpcond_to_rvv_vi[cond].adjust);
+                       vec_imm(cmp2, vece) - tcg_cmpcond_to_rvv_vi[cond].adjust);
     } else if (tcg_cmpcond_to_rvv_vv[cond].swap) {
         tcg_out_opc_vv(s, tcg_cmpcond_to_rvv_vv[cond].op,
                        TCG_REG_V0, cmp2, cmp1);
@@ -1585,11 +1585,12 @@ static void tcg_out_cmpsel(TCGContext *s, TCGType type, unsigned vece,
     }
     if (c_val1) {
         if (c_val2) {
-            tcg_out_opc_vi(s, OPC_VMV_V_I, ret, 0, val2);
+            tcg_out_opc_vi(s, OPC_VMV_V_I, ret, 0, vec_imm(val2, vece));
             val2 = ret;
         }
         /* vd[i] == v0.mask[i] ? imm : vs2[i] */
-        tcg_out_opc_vim_mask(s, OPC_VMERGE_VIM, ret, val2, val1);
+        tcg_out_opc_vim_mask(s, OPC_VMERGE_VIM, ret, val2,
+                             vec_imm(val1, vece));
     } else {
         /* vd[i] == v0.mask[i] ? vs1[i] : vs2[i] */
         tcg_out_opc_vvm_mask(s, OPC_VMERGE_VVM, ret, val2, val1);
Signature · 1 line
2.55.0
#6 lore raw

[PULL 07/22] meson.build: fix getifaddrs() detection on illumos

In reply to Philippe Mathieu-Daudé

From: Roman Bogorodskiy <[email protected]>

Currently, getifaddrs() fails to get detected on illumos because it requires linking with libsocket which meson is not doing.

Fix by adding socket to getifaddrs dependencies. This unlocks 'guest-network-get-interfaces' implementation for illumos.

Message-ID: <[email protected]>

---
 meson.build | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
meson.build +1 -1
index d7bcb581c181..58286a3f8a71 100644
@@ -2643,7 +2643,7 @@ config_host_data.set('CONFIG_TIMERFD', cc.has_function('timerfd_create'))
 config_host_data.set('CONFIG_GETLOADAVG', cc.has_function('getloadavg'))
 config_host_data.set('CONFIG_ARPA_INET_64', cc.has_function('htonll'))
 config_host_data.set('HAVE_COPY_FILE_RANGE', cc.has_function('copy_file_range'))
-config_host_data.set('HAVE_GETIFADDRS', cc.has_function('getifaddrs'))
+config_host_data.set('HAVE_GETIFADDRS', cc.has_function('getifaddrs', dependencies: socket))
 config_host_data.set('HAVE_GLIB_WITH_SLICE_ALLOCATOR', glib_has_gslice)
 config_host_data.set('HAVE_OPENPTY', cc.has_function('openpty', dependencies: util))
 config_host_data.set('HAVE_STRCHRNUL', cc.has_function('strchrnul', prefix: osdep_prefix))
Signature · 1 line
2.55.0
#7 lore raw

[PULL 06/22] tests/tcg/aarch64: Add vector immediate regression tests

In reply to Philippe Mathieu-Daudé

Run SVE/AdvSIMD immediate operations after an operation of another element size, at every SVE vector length, against scalar references.

Catches backends encoding the replicated constant or using a stale element size, as tcg/riscv64 did.

AI-used-for: Regression tests

Message-ID: <[email protected]>

---
 tests/tcg/aarch64/sve-vec-imm.c | 233 ++++++++++++++++++++++++++++++++
 tests/tcg/aarch64/meson.build   |   1 +
 2 files changed, 234 insertions(+)
 create mode 100644 tests/tcg/aarch64/sve-vec-imm.c
tests/tcg/aarch64/sve-vec-imm.c new file +233 -0
new file mode 100644
index 000000000000..62391824946b
@@ -0,0 +1,233 @@
+/*
+ * Vector operations with immediates, at every SVE vector length.
+ *
+ * Each case first runs an operation with a given element size, then
+ * an operation with an immediate at another element size, within the
+ * same TB.  This catches TCG backends that encode the immediate from
+ * the whole replicated constant, or apply it at a stale element size.
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/prctl.h>
+
+#define MAXVL 256
+
+static uint8_t in[MAXVL], out[MAXVL];
+
+typedef void test_fn(void);
+typedef uint64_t ref_fn(uint64_t x);
+
+#define TEST(NAME, SETUP, OP)                                       \
+static void NAME(void)                                              \
+{                                                                   \
+    asm volatile("ldr z0, [%0]\n\t"                                 \
+                 "ldr z1, [%0]\n\t"                                 \
+                 "mov z2.d, #0\n\t"                                 \
+                 SETUP "\n\t"                                       \
+                 OP "\n\t"                                          \
+                 "str z0, [%1]"                                     \
+                 : : "r"(in), "r"(out) : "z0", "z1", "z2", "memory"); \
+}
+
+#define SET_B  "add z1.b, z1.b, z1.b"
+#define SET_D  "add z1.d, z1.d, z1.d"
+
+static int64_t sext(uint64_t x, int bits)
+{
+    return (int64_t)(x << (64 - bits)) >> (64 - bits);
+}
+
+static uint64_t sat_s(int64_t x, int bits)
+{
+    int64_t max = (1ll << (bits - 1)) - 1;
+    return x > max ? max : x < -max - 1 ? -max - 1 : x;
+}
+
+static uint64_t sat_u(int64_t x, int bits)
+{
+    int64_t max = (1ll << bits) - 1;
+    return x > max ? max : x < 0 ? 0 : x;
+}
+
+static uint64_t ror(uint64_t x, int n, int bits)
+{
+    uint64_t m = bits == 64 ? -1ull : (1ull << bits) - 1;
+    return ((x >> n) | (x << (bits - n))) & m;
+}
+
+/* Arithmetic immediates (add_vec, sub_vec, saturating ops). */
+TEST(add_b5, SET_D, "add z0.b, z0.b, #5")
+static uint64_t r_add_b5(uint64_t x) { return x + 5; }
+TEST(add_h3, SET_B, "add z0.h, z0.h, #3")
+static uint64_t r_add_h3(uint64_t x) { return x + 3; }
+TEST(sub_s7, SET_B, "sub z0.s, z0.s, #7")
+static uint64_t r_sub_s7(uint64_t x) { return x - 7; }
+TEST(subr_h3, SET_B, "subr z0.h, z0.h, #3")
+static uint64_t r_subr_h3(uint64_t x) { return 3 - x; }
+TEST(sqadd_b3, SET_D, "sqadd z0.b, z0.b, #3")
+static uint64_t r_sqadd_b3(uint64_t x) { return sat_s(sext(x, 8) + 3, 8); }
+TEST(uqadd_h9, SET_B, "uqadd z0.h, z0.h, #9")
+static uint64_t r_uqadd_h9(uint64_t x) { return sat_u(x + 9, 16); }
+TEST(sqsub_s4, SET_B, "sqsub z0.s, z0.s, #4")
+static uint64_t r_sqsub_s4(uint64_t x) { return sat_s(sext(x, 32) - 4, 32); }
+TEST(uqsub_s2, SET_B, "uqsub z0.s, z0.s, #2")
+static uint64_t r_uqsub_s2(uint64_t x) { return sat_u((int64_t)x - 2, 32); }
+
+/* Logical immediates (and_vec, or_vec, xor_vec). */
+TEST(and_d_f, SET_B, "and z0.d, z0.d, #0xf")
+static uint64_t r_and_f(uint64_t x) { return x & 0xf; }
+TEST(and_b_f, SET_D, "and z0.b, z0.b, #0xf")
+TEST(orr_d_m16, SET_B, "orr z0.d, z0.d, #0xfffffffffffffff0")
+static uint64_t r_orr_m16(uint64_t x) { return x | -16ull; }
+TEST(eor_b_f, SET_D, "eor z0.b, z0.b, #0xf")
+static uint64_t r_eor_f(uint64_t x) { return x ^ 0xf; }
+TEST(orr_h_f, SET_D, "orr z0.h, z0.h, #0xf")
+static uint64_t r_orr_f(uint64_t x) { return x | 0xf; }
+TEST(eor_s_1, SET_B, "eor z0.s, z0.s, #0x1")
+static uint64_t r_eor_1(uint64_t x) { return x ^ 1; }
+
+/* Shift immediates, including counts above 15 and 31. */
+TEST(lsl_d40, SET_B, "lsl z0.d, z0.d, #40")
+static uint64_t r_lsl_d40(uint64_t x) { return x << 40; }
+TEST(asr_d63, SET_B, "asr z0.d, z0.d, #63")
+static uint64_t r_asr_d63(uint64_t x) { return sext(x, 64) >> 63; }
+TEST(lsr_s20, SET_D, "lsr z0.s, z0.s, #20")
+static uint64_t r_lsr_s20(uint64_t x) { return x >> 20; }
+TEST(lsl_s31, SET_D, "lsl z0.s, z0.s, #31")
+static uint64_t r_lsl_s31(uint64_t x) { return x << 31; }
+TEST(asr_h15, SET_D, "asr z0.h, z0.h, #15")
+static uint64_t r_asr_h15(uint64_t x) { return sext(x, 16) >> 15; }
+TEST(xar_d40, SET_B, "xar z0.d, z0.d, z2.d, #40")
+static uint64_t r_xar_d40(uint64_t x) { return ror(x, 40, 64); }
+TEST(xar_s20, SET_D, "xar z0.s, z0.s, z2.s, #20")
+static uint64_t r_xar_s20(uint64_t x) { return ror(x, 20, 32); }
+
+/* Compare with zero (cmp_vec), including the adjusted LT/GE forms. */
+TEST(cmeq_h0, SET_B, "cmeq v0.8h, v0.8h, #0")
+static uint64_t r_cmeq0(uint64_t x) { return x == 0 ? -1ull : 0; }
+TEST(cmgt_b0, SET_D, "cmgt v0.16b, v0.16b, #0")
+static uint64_t r_cmgt_b0(uint64_t x) { return sext(x, 8) > 0 ? -1ull : 0; }
+TEST(cmlt_b0, SET_D, "cmlt v0.16b, v0.16b, #0")
+static uint64_t r_cmlt_b0(uint64_t x) { return sext(x, 8) < 0 ? -1ull : 0; }
+TEST(cmlt_h0, SET_B, "cmlt v0.8h, v0.8h, #0")
+static uint64_t r_cmlt_h0(uint64_t x) { return sext(x, 16) < 0 ? -1ull : 0; }
+TEST(cmge_s0, SET_B, "cmge v0.4s, v0.4s, #0")
+static uint64_t r_cmge_s0(uint64_t x) { return sext(x, 32) >= 0 ? -1ull : 0; }
+TEST(cmle_d0, SET_B, "cmle v0.2d, v0.2d, #0")
+static uint64_t r_cmle_d0(uint64_t x) { return sext(x, 64) <= 0 ? -1ull : 0; }
+
+/* Vector constants (dupi_vec): vmv.v.i, 0/-1, and wide immediates. */
+TEST(movi_s5, SET_B, "movi v0.4s, #5")
+static uint64_t r_5(uint64_t x) { return 5; }
+TEST(movi_h99, SET_D, "movi v0.8h, #0x99")
+static uint64_t r_99(uint64_t x) { return 0x99; }
+TEST(movi_d_mask, SET_B, "movi v0.2d, #0xff00ff00ff00ff00")
+static uint64_t r_mask(uint64_t x) { return 0xff00ff00ff00ff00ull; }
+TEST(mvni_s0, SET_D, "mvni v0.4s, #0")
+static uint64_t r_m1(uint64_t x) { return -1ull; }
+
+static const struct {
+    const char *name;
+    test_fn *fn;
+    ref_fn *ref;
+    int esz;        /* element size in bytes */
+    int neon;       /* AdvSIMD op: only the low 16 bytes, rest zeroed */
+} tests[] = {
+    { "add .d; add .b, #5", add_b5, r_add_b5, 1 },
+    { "add .b; add .h, #3", add_h3, r_add_h3, 2 },
+    { "add .b; sub .s, #7", sub_s7, r_sub_s7, 4 },
+    { "add .b; subr .h, #3", subr_h3, r_subr_h3, 2 },
+    { "add .d; sqadd .b, #3", sqadd_b3, r_sqadd_b3, 1 },
+    { "add .b; uqadd .h, #9", uqadd_h9, r_uqadd_h9, 2 },
+    { "add .b; sqsub .s, #4", sqsub_s4, r_sqsub_s4, 4 },
+    { "add .b; uqsub .s, #2", uqsub_s2, r_uqsub_s2, 4 },
+
+    { "add .b; and .d, #0xf", and_d_f, r_and_f, 8 },
+    { "add .d; and .b, #0xf", and_b_f, r_and_f, 1 },
+    { "add .b; orr .d, #-16", orr_d_m16, r_orr_m16, 8 },
+    { "add .d; eor .b, #0xf", eor_b_f, r_eor_f, 1 },
+    { "add .d; orr .h, #0xf", orr_h_f, r_orr_f, 2 },
+    { "add .b; eor .s, #1", eor_s_1, r_eor_1, 4 },
+
+    { "add .b; lsl .d, #40", lsl_d40, r_lsl_d40, 8 },
+    { "add .b; asr .d, #63", asr_d63, r_asr_d63, 8 },
+    { "add .d; lsr .s, #20", lsr_s20, r_lsr_s20, 4 },
+    { "add .d; lsl .s, #31", lsl_s31, r_lsl_s31, 4 },
+    { "add .d; asr .h, #15", asr_h15, r_asr_h15, 2 },
+    { "add .b; xar .d, #40", xar_d40, r_xar_d40, 8 },
+    { "add .d; xar .s, #20", xar_s20, r_xar_s20, 4 },
+
+    { "add .b; cmeq .8h, #0", cmeq_h0, r_cmeq0, 2, 1 },
+    { "add .d; cmgt .16b, #0", cmgt_b0, r_cmgt_b0, 1, 1 },
+    { "add .d; cmlt .16b, #0", cmlt_b0, r_cmlt_b0, 1, 1 },
+    { "add .b; cmlt .8h, #0", cmlt_h0, r_cmlt_h0, 2, 1 },
+    { "add .b; cmge .4s, #0", cmge_s0, r_cmge_s0, 4, 1 },
+    { "add .b; cmle .2d, #0", cmle_d0, r_cmle_d0, 8, 1 },
+
+    { "add .b; movi .4s, #5", movi_s5, r_5, 4, 1 },
+    { "add .d; movi .8h, #0x99", movi_h99, r_99, 2, 1 },
+    { "add .b; movi .2d, #0xff00...", movi_d_mask, r_mask, 8, 1 },
+    { "add .d; mvni .4s, #0", mvni_s0, r_m1, 4, 1 },
+};
+
+static int check(int vl, int t)
+{
+    int esz = tests[t].esz;
+    int n = tests[t].neon ? 16 : vl;
+    uint64_t mask = esz == 8 ? -1ull : (1ull << (esz * 8)) - 1;
+
+    for (int i = 0; i < vl; i += esz) {
+        uint64_t x = 0, got = 0, exp = 0;
+
+        memcpy(&x, in + i, esz);
+        memcpy(&got, out + i, esz);
+        if (i < n) {
+            exp = tests[t].ref(x) & mask;
+        }
+        if (got != exp) {
+            fprintf(stderr, "vl %d: %s: byte %d: got 0x%llx, "
+                    "expected 0x%llx\n", vl, tests[t].name, i,
+                    (unsigned long long)got, (unsigned long long)exp);
+            return 1;
+        }
+    }
+    return 0;
+}
+
+int main(void)
+{
+    int err = 0, nvl = 0;
+
+    for (int i = 0; i < MAXVL; i++) {
+        in[i] = 0xa5 ^ (i * 0x37);
+    }
+    /* Zero, saturating and sign-boundary elements in the low 16 bytes. */
+    in[0] = 0x7e;                       /* sqadd .b overflow */
+    in[2] = in[3] = 0;                  /* cmeq .h, zero */
+    in[8] = 1;                          /* uqsub .s underflow */
+    in[9] = in[10] = in[11] = 0;
+    in[12] = 0xfa; in[13] = 0xff;       /* uqadd .h overflow */
+
+    for (int vl = 16; vl <= MAXVL; vl *= 2) {
+        if (prctl(PR_SVE_SET_VL, vl, 0, 0, 0, 0) != vl) {
+            continue;
+        }
+        nvl++;
+        for (int t = 0; t < sizeof(tests) / sizeof(tests[0]); t++) {
+            memset(out, 0xee, sizeof(out));
+            tests[t].fn();
+            err |= check(vl, t);
+        }
+    }
+    if (nvl == 0) {
+        fprintf(stderr, "no SVE vector length available\n");
+        return 1;
+    }
+    return err;
+}
tests/tcg/aarch64/meson.build +1 -0
index 52eb67ae6ace..2033356e3625 100644
@@ -145,6 +145,7 @@ tests += {
 tests += {
   'test-826.c': {'cc_feat': 'sve2'},
   'sve-while-ptr.c': {'cc_feat': 'sve2'},
+  'sve-vec-imm.c': {'cc_feat': 'sve2'},
 }
 
 # FPRCVT test
Signature · 1 line
2.55.0
#8 lore raw

[PULL 08/22] qga: fix build on illumos

In reply to Philippe Mathieu-Daudé

From: Roman Bogorodskiy <[email protected]>

Currently, build fails on illumos with:

../qga/commands-posix.c: In function 'qmp_guest_get_load':
../qga/commands-posix.c:1408:9: error: implicit declaration of function 'getloadavg' [-Wimplicit-function-declaration]

 1408 |     if (getloadavg(loadavg, G_N_ELEMENTS(loadavg)) < 0) {
      |         ^~~~~~~~~~

../qga/commands-posix.c:1408:9: warning: nested extern declaration of 'getloadavg' [-Wnested-externs]

This happens because including <sys/loadavg.h> which is needed for getloadavg() on illumos is nested under the "#ifdef HAVE_GETIFADDRS" block, and is not included when HAVE_GETIFADDRS is not defined.

As getloadavg() does not depend on HAVE_GETIFADDRS, include <sys/loadavg.h> outside of the "#ifdef HAVE_GETIFADDRS".

Message-ID: <[email protected]>

---
 qga/commands-posix.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
qga/commands-posix.c +3 -3
index cfa5627a61d8..ae53eb356965 100644
@@ -16,6 +16,9 @@
 #include <sys/utsname.h>
 #include <sys/wait.h>
 #include <dirent.h>
+#if defined(CONFIG_SOLARIS) && defined(CONFIG_GETLOADAVG)
+#include <sys/loadavg.h>
+#endif
 #include "qga-qapi-commands.h"
 #include "qapi/error.h"
 #include "qemu/host-utils.h"
@@ -43,9 +46,6 @@
 #include <net/ethernet.h>
 #endif
 #ifdef CONFIG_SOLARIS
-#ifdef CONFIG_GETLOADAVG
-#include <sys/loadavg.h>
-#endif
 #include <sys/sockio.h>
 #endif
 #endif
Signature · 1 line
2.55.0
#9 lore raw

[PULL 09/22] qga: fix uname() return value check in guest-get-osinfo

In reply to Philippe Mathieu-Daudé

From: Brad Smith <[email protected]>

POSIX only specifies that uname() returns a non-negative value on success. Solaris returns 1, so guest-get-osinfo treated every successful call as a failure and reported a stale errno.

Check for a negative return value instead, as linux-user/uname.c already does.

Message-ID: <[email protected]>

---
 qga/commands-posix.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
qga/commands-posix.c +1 -1
index ae53eb356965..39e772b3ff32 100644
@@ -1336,7 +1336,7 @@ GuestOSInfo *qmp_guest_get_osinfo(Error **errp)
 
     info = g_new0(GuestOSInfo, 1);
 
-    if (uname(&kinfo) != 0) {
+    if (uname(&kinfo) < 0) {
         error_setg_errno(errp, errno, "uname failed");
     } else {
         info->kernel_version = g_strdup(kinfo.version);
Signature · 1 line
2.55.0
#10 lore raw

[PULL 10/22] build: remove vestigial CONFIG_SGA

In reply to Philippe Mathieu-Daudé

From: John Snow <[email protected]>

This configuration knob no longer exists as CONFIG_SGA was removed quite some time ago. Remove it from our sample config files.

Message-ID: <[email protected]>

Fixes b482fb43deb3 ("hw/misc/sga: Remove the deprecated "sga" device")

Message-ID: <[email protected]>
[PMD: Unconditionally add the test (danpb)]

Message-ID: <[email protected]>

---
 configs/devices/i386-softmmu/default.mak | 1 -
 tests/qtest/meson.build                  | 2 +-
 2 files changed, 1 insertion(+), 2 deletions(-)
configs/devices/i386-softmmu/default.mak +0 -1
index bc0479a7e0a3..b935ee613329 100644
@@ -17,7 +17,6 @@
 #CONFIG_PVPANIC=n
 #CONFIG_QXL=n
 #CONFIG_SEV=n
-#CONFIG_SGA=n
 #CONFIG_TDX=n
 #CONFIG_TEST_DEVICES=n
 #CONFIG_TPM_CRB=n
tests/qtest/meson.build +1 -1
index 626791d1eb48..6e2938d913ef 100644
@@ -74,7 +74,7 @@ qtests_i386 = \
   (config_all_devices.has_key('CONFIG_I440FX') ? ['numa-test'] : []) +                      \
   (config_all_devices.has_key('CONFIG_I440FX') ? ['test-x86-cpuid-compat'] : []) +          \
   (config_all_devices.has_key('CONFIG_ISA_TESTDEV') ? ['endianness-test'] : []) +           \
-  (config_all_devices.has_key('CONFIG_SGA') ? ['boot-serial-test'] : []) +                  \
+  ['boot-serial-test'] + \
   (config_all_devices.has_key('CONFIG_ISA_IPMI_KCS') ? ['ipmi-kcs-test'] : []) +            \
   (host_os == 'linux' and                                                                  \
    config_all_devices.has_key('CONFIG_ISA_IPMI_BT') and
Signature · 1 line
2.55.0
#11 lore raw

[PULL 11/22] build: update CONFIG_PVPANIC

In reply to Philippe Mathieu-Daudé

From: John Snow <[email protected]>

The symbol for CONFIG_PVPANIC has been changed, update the dummy config file to reference the new names.

Message-ID: <[email protected]>

Fixes 677726ef1ebc ("hw/misc/pvpanic: split-out generic and bus dependent code")
---
 configs/devices/i386-softmmu/default.mak | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
configs/devices/i386-softmmu/default.mak +4 -1
index b935ee613329..96716e30b9b0 100644
@@ -14,7 +14,10 @@
 #CONFIG_PCI_IPMI_BT=n
 #CONFIG_IPMI_SSIF=n
 #CONFIG_PCI_DEVICES=n
-#CONFIG_PVPANIC=n
+#CONFIG_PVPANIC_COMMON=n
+#CONFIG_PVPANIC_ISA=n
+#CONFIG_PVPANIC_PCI=n
+#CONFIG_PVPANIC_MMIO=n
 #CONFIG_QXL=n
 #CONFIG_SEV=n
 #CONFIG_TDX=n
Signature · 1 line
2.55.0
#12 lore raw

[PULL 12/22] pvg: disable by default

In reply to Philippe Mathieu-Daudé

From: Mohamed Mediouni <[email protected]>

macOS 27 removes support for x86 processors. As such, support for ParavirtualizedGraphics as a public API has been removed.

Current QEMU does not compile with ParavirtualizedGraphics enabled and the arm64 side of PVG is not documented or supported by Apple for third-party use.

Message-ID: <[email protected]>

---
 meson_options.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
meson_options.txt +1 -1
index 9ae8e44ab54e..ada233473eab 100644
@@ -208,7 +208,7 @@ option('lzfse', type : 'feature', value : 'auto',
        description: 'lzfse support for DMG images')
 option('lzo', type : 'feature', value : 'auto',
        description: 'lzo compression support')
-option('pvg', type: 'feature', value: 'auto',
+option('pvg', type: 'feature', value: 'disabled',
        description: 'macOS paravirtualized graphics support')
 option('rbd', type : 'feature', value : 'auto',
        description: 'Ceph block device driver')
Signature · 1 line
2.55.0
#13 lore raw

[PULL 13/22] hw/i386: move bios functions into x86-bios.c

In reply to Philippe Mathieu-Daudé

From: John Snow <[email protected]>

Currently, these functions are located in x86-common.c. However, when configuring QEMU with --without-default-devices, we encounter a linking error at final assembly time where these functions cannot be located.

Split these functions out into a new x86-bios.c module to allow defaultless compilations of x86_64 to succeed.

Message-ID: <[email protected]>

---
 hw/i386/x86-bios.c   | 143 +++++++++++++++++++++++++++++++++++++++++++
 hw/i386/x86-common.c | 125 -------------------------------------
 hw/i386/x86.c        |   1 -
 hw/i386/meson.build  |   1 +
 4 files changed, 144 insertions(+), 126 deletions(-)
 create mode 100644 hw/i386/x86-bios.c
hw/i386/x86-bios.c new file +143 -0
new file mode 100644
index 000000000000..525e42e7252f
@@ -0,0 +1,143 @@
+/*
+ * x86 bios loading functions
+ *
+ * Copyright (c) 2003-2004 Fabrice Bellard
+ * Copyright (c) 2019, 2024-2026 Red Hat, Inc.
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/datadir.h"
+#include "qapi/error.h"
+#include "qemu/units.h"
+
+#include "hw/core/loader.h"
+#include "hw/i386/x86.h"
+#include "kvm/tdx.h"
+#include "target/i386/sev.h"
+
+void x86_isa_bios_init(MemoryRegion *isa_bios, MemoryRegion *isa_memory,
+                       MemoryRegion *bios, bool read_only)
+{
+    uint64_t bios_size = memory_region_size(bios);
+    uint64_t isa_bios_size = MIN(bios_size, 128 * KiB);
+
+    memory_region_init_alias(isa_bios, NULL, "isa-bios", bios,
+                             bios_size - isa_bios_size, isa_bios_size);
+    memory_region_add_subregion_overlap(isa_memory, 1 * MiB - isa_bios_size,
+                                        isa_bios, 1);
+    memory_region_set_readonly(isa_bios, read_only);
+}
+
+static int get_bios_size(X86MachineState *x86ms,
+                         const char *bios_name, char *filename)
+{
+    int bios_size;
+
+    if (filename) {
+        bios_size = get_image_size(filename, NULL);
+    } else {
+        bios_size = -1;
+    }
+    if (bios_size <= 0 ||
+        (bios_size % 65536) != 0) {
+        goto bios_error;
+    }
+
+    return bios_size;
+
+ bios_error:
+    fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
+    exit(1);
+}
+
+static void load_bios_from_file(X86MachineState *x86ms, const char *bios_name,
+                                char *filename, int bios_size,
+                                bool isapc_ram_fw)
+{
+    ssize_t ret;
+
+    /* BIOS load */
+    if (machine_require_guest_memfd_private(MACHINE(x86ms))) {
+        memory_region_init_ram_guest_memfd_private(
+            &x86ms->bios, NULL, "pc.bios", bios_size, &error_fatal);
+        if (is_tdx_vm()) {
+            tdx_set_tdvf_region(&x86ms->bios);
+        }
+    } else {
+        memory_region_init_ram(&x86ms->bios, NULL, "pc.bios",
+                               bios_size, &error_fatal);
+    }
+    if (sev_enabled() || is_tdx_vm()) {
+        /*
+         * The concept of a "reset" simply doesn't exist for
+         * confidential computing guests, we have to destroy and
+         * re-launch them instead.  So there is no need to register
+         * the firmware as rom to properly re-initialize on reset.
+         * Just go for a straight file load instead.
+         */
+        void *ptr = memory_region_get_ram_ptr(&x86ms->bios);
+        load_image_size(filename, ptr, bios_size);
+        x86_firmware_configure(0x100000000ULL - bios_size, ptr, bios_size);
+    } else {
+        memory_region_set_readonly(&x86ms->bios, !isapc_ram_fw);
+        ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1);
+        if (ret != 0) {
+            goto bios_error;
+        }
+    }
+
+    return;
+
+ bios_error:
+    fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
+    exit(1);
+}
+
+void x86_bios_rom_reload(X86MachineState *x86ms)
+{
+    int bios_size;
+    const char *bios_name;
+    char *filename;
+
+    if (memory_region_size(&x86ms->bios) == 0) {
+        /* if -bios is not used */
+        return;
+    }
+
+    bios_name = MACHINE(x86ms)->firmware ?: "bios.bin";
+    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
+
+    bios_size = get_bios_size(x86ms, bios_name, filename);
+
+    void *ptr = memory_region_get_ram_ptr(&x86ms->bios);
+    load_image_size(filename, ptr, bios_size);
+    x86_firmware_configure(0x100000000ULL - bios_size, ptr, bios_size);
+}
+
+void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware,
+                       MemoryRegion *rom_memory, bool isapc_ram_fw)
+{
+    int bios_size;
+    const char *bios_name;
+    g_autofree char *filename;
+
+    bios_name = MACHINE(x86ms)->firmware ?: default_firmware;
+    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
+
+    bios_size = get_bios_size(x86ms, bios_name, filename);
+    load_bios_from_file(x86ms, bios_name, filename, bios_size, isapc_ram_fw);
+
+    if (!machine_require_guest_memfd_private(MACHINE(x86ms))) {
+        /* map the last 128KB of the BIOS in ISA space */
+        x86_isa_bios_init(&x86ms->isa_bios, rom_memory, &x86ms->bios,
+                          !isapc_ram_fw);
+    }
+
+    /* map all the bios at the top of memory */
+    memory_region_add_subregion(rom_memory,
+                                (uint32_t)(-bios_size),
+                                &x86ms->bios);
+    return;
+}
hw/i386/x86-common.c +0 -125

128 lines not on this page · raw message

hw/i386/x86.c +0 -1
index 4b75b7406a81..5f842351daff 100644
@@ -35,7 +35,6 @@
 #include "hw/acpi/aml-build.h"
 #include "hw/i386/x86.h"
 #include "hw/i386/topology.h"
-
 #include "hw/core/nmi.h"
 #include "kvm/kvm_i386.h"
 
hw/i386/meson.build +1 -0
index e16930c43197..6bb73b8731cc 100644
@@ -5,6 +5,7 @@ i386_ss.add(files(
   'monitor.c',
   'multiboot.c',
   'x86.c',
+  'x86-bios.c',
   'x86-cpu.c',
 ))
 
Signature · 1 line
2.55.0
#14 lore raw

[PULL 15/22] hw/usb/ccid-card-emulated: Use QemuSemaphore rather than QemuCond

In reply to Philippe Mathieu-Daudé

From: Peter Maydell <[email protected]>

The emulated CCID card uses a QemuMutex and QemuCond to communicate with its helper thread, but all it's trying to do here is "make the helper thread loop wait until is signalled that it has more work to do". The existing code functions correctly, but "wait until the other thread signals you" is more simply implemented with a QemuSemaphore.

This avoids a Coverity complaint that we're not using the condition variable in the standard

   lock mutex;
   while (!some_predicate) {
       cond_wait(condvar, mutex);
   }
   do stuff;
   unlock mutex;

pattern.

Resolves: Coverity CID 1547639

Message-ID: <[email protected]>

---
 hw/usb/ccid-card-emulated.c | 22 +++++++---------------
 1 file changed, 7 insertions(+), 15 deletions(-)
hw/usb/ccid-card-emulated.c +7 -15
index 6c8eec39faa9..c504c52a7cdc 100644
@@ -129,8 +129,7 @@ struct EmulatedState {
     VReader *reader;
     QSIMPLEQ_HEAD(, EmulEvent) guest_apdu_list;
     QemuMutex vreader_mutex; /* and guest_apdu_list mutex */
-    QemuMutex handle_apdu_mutex;
-    QemuCond handle_apdu_cond;
+    QemuSemaphore handle_apdu_sem;
     EventNotifier notifier;
     int      quit_apdu_thread;
     QemuThread apdu_thread_id;
@@ -149,9 +148,7 @@ static void emulated_apdu_from_guest(CCIDCardState *base,
     qemu_mutex_lock(&card->vreader_mutex);
     QSIMPLEQ_INSERT_TAIL(&card->guest_apdu_list, event, entry);
     qemu_mutex_unlock(&card->vreader_mutex);
-    qemu_mutex_lock(&card->handle_apdu_mutex);
-    qemu_cond_signal(&card->handle_apdu_cond);
-    qemu_mutex_unlock(&card->handle_apdu_mutex);
+    qemu_sem_post(&card->handle_apdu_sem);
 }
 
 static const uint8_t *emulated_get_atr(CCIDCardState *base, uint32_t *len)
@@ -238,9 +235,7 @@ static void *handle_apdu_thread(void* arg)
     EmulEvent *event;
 
     while (1) {
-        qemu_mutex_lock(&card->handle_apdu_mutex);
-        qemu_cond_wait(&card->handle_apdu_cond, &card->handle_apdu_mutex);
-        qemu_mutex_unlock(&card->handle_apdu_mutex);
+        qemu_sem_wait(&card->handle_apdu_sem);
         if (card->quit_apdu_thread) {
             card->quit_apdu_thread = 0; /* debugging */
             break;
@@ -496,8 +491,7 @@ static void emulated_realize(CCIDCardState *base, Error **errp)
     QSIMPLEQ_INIT(&card->guest_apdu_list);
     qemu_mutex_init(&card->event_list_mutex);
     qemu_mutex_init(&card->vreader_mutex);
-    qemu_mutex_init(&card->handle_apdu_mutex);
-    qemu_cond_init(&card->handle_apdu_cond);
+    qemu_sem_init(&card->handle_apdu_sem, 0);
     card->reader = NULL;
     card->quit_apdu_thread = 0;
     if (init_event_notifier(card, errp) < 0) {
@@ -556,8 +550,7 @@ static void emulated_realize(CCIDCardState *base, Error **errp)
 out2:
     clean_event_notifier(card);
 out1:
-    qemu_cond_destroy(&card->handle_apdu_cond);
-    qemu_mutex_destroy(&card->handle_apdu_mutex);
+    qemu_sem_destroy(&card->handle_apdu_sem);
     qemu_mutex_destroy(&card->vreader_mutex);
     qemu_mutex_destroy(&card->event_list_mutex);
 }
@@ -571,13 +564,12 @@ static void emulated_unrealize(CCIDCardState *base)
     qemu_thread_join(&card->event_thread_id);
 
     card->quit_apdu_thread = 1; /* stop handle_apdu thread */
-    qemu_cond_signal(&card->handle_apdu_cond);
+    qemu_sem_post(&card->handle_apdu_sem);
     qemu_thread_join(&card->apdu_thread_id);
 
     clean_event_notifier(card);
     /* threads exited, can destroy all condvars/mutexes */
-    qemu_cond_destroy(&card->handle_apdu_cond);
-    qemu_mutex_destroy(&card->handle_apdu_mutex);
+    qemu_sem_destroy(&card->handle_apdu_sem);
     qemu_mutex_destroy(&card->vreader_mutex);
     qemu_mutex_destroy(&card->event_list_mutex);
 
Signature · 1 line
2.55.0
#15 lore raw

[PULL 18/22] hw/usb/dev-mtp: Check length of class control request

In reply to Philippe Mathieu-Daudé

From: Tommaso Califano <[email protected]>

usb_mtp_handle_control() reads a 16-bit event code from the request data without checking that the host actually sent it. A request with wLength < 2 makes the device act on stale bytes left in the control buffer.

STALL requests that are too short to hold the event code.

Fixes 47bff13cea8d ("dev-mtp: add support for canceling transaction")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4529

Message-ID: <[email protected]>

---
 hw/usb/dev-mtp.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/usb/dev-mtp.c +4 -0
index bc5a6f089178..92dc9a9e21d6 100644
@@ -1516,6 +1516,10 @@ static void usb_mtp_handle_control(USBDevice *dev, USBPacket *p,
 
     switch (request) {
     case ClassInterfaceOutRequest | 0x64:
+        if (length < sizeof(*event)) {
+            p->status = USB_RET_STALL;
+            break;
+        }
         if (*event == EVT_CANCEL_TRANSACTION) {
             g_free(s->result);
             s->result = NULL;
Signature · 1 line
2.55.0
#16 lore raw

[PULL 20/22] hw/block/m25p80: fix write enable latch after WRSR2

In reply to Philippe Mathieu-Daudé

From: Chao Liu <[email protected]>

The flash clears the write enable latch when a WRSR2 command completes, just like a WRSR write; otherwise a following program or erase could proceed without a new WREN.

Fixes 9785731ec4f7 ("hw/block:m25p80: Support write status register 2 command (0x31) for w25q01jvq")

Message-ID: <0c80a9c01452a8695b46a6d9fa920f9a14f4de47.1794558546.git.w-yf22@mails.tsinghua.edu.cn>

[PMD: Directly assign @write_enable]
---
 hw/block/m25p80.c | 1 +
 1 file changed, 1 insertion(+)
hw/block/m25p80.c +1 -0
index baf798c6b5cc..92e67fdc5e04 100644
@@ -840,6 +840,7 @@ static void complete_collecting_data(Flash *s)
         default:
             break;
         }
+        s->write_enable = false;
         break;
     case BRWR:
     case EXTEND_ADDR_WRITE:
Signature · 1 line
2.55.0
#17 lore raw

[PULL 19/22] hw/usb/dev-mtp: Stop processing SendObjectInfo after an error

In reply to Philippe Mathieu-Daudé

From: Tommaso Califano <[email protected]>

When SendObjectInfo fails its checks, usb_mtp_command() queues an error response and then falls through. It still allocates s->data_out. The data phase is then dropped because a result is pending, so s->data_out is never released.

Return right after queueing the error response.

Fixes 53735bef108c ("usb-mtp: Advertise SendObjectInfo for write support")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4529

Message-ID: <[email protected]>

---
 hw/usb/dev-mtp.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/usb/dev-mtp.c +5 -0
index 92dc9a9e21d6..a8b89c938b79 100644
@@ -1393,14 +1393,17 @@ static void usb_mtp_command(MTPState *s, MTPControl *c)
         if (!FLAG_SET(s, MTP_FLAG_WRITABLE)) {
             usb_mtp_queue_result(s, RES_STORE_READ_ONLY,
                                  c->trans, 0, 0, 0, 0);
+            return;
         } else if (c->argv[0] && (c->argv[0] != QEMU_STORAGE_ID)) {
             /* First parameter points to storage id or is 0 */
             usb_mtp_queue_result(s, RES_STORE_NOT_AVAILABLE, c->trans,
                                  0, 0, 0, 0);
+            return;
         } else if (c->argv[1] && !c->argv[0]) {
             /* If second parameter is specified, first must also be specified */
             usb_mtp_queue_result(s, RES_DESTINATION_UNSUPPORTED, c->trans,
                                  0, 0, 0, 0);
+            return;
         } else {
             uint32_t handle = c->argv[1];
             if (handle == 0xFFFFFFFF || handle == 0) {
@@ -1412,9 +1415,11 @@ static void usb_mtp_command(MTPState *s, MTPControl *c)
             if (o == NULL) {
                 usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE, c->trans,
                                      0, 0, 0, 0);
+                return;
             } else if (o->format != FMT_ASSOCIATION) {
                 usb_mtp_queue_result(s, RES_INVALID_PARENT_OBJECT, c->trans,
                                      0, 0, 0, 0);
+                return;
             }
         }
         if (o) {
Signature · 1 line
2.55.0
#18 lore raw

[PULL 14/22] hw/usb/u2f: validate ring buffer indices on migration load

In reply to Philippe Mathieu-Daudé

From: Bin Guo <[email protected]>

The U2F key pending_in ring buffer uses uint8_t start/end/num indices over a 32-entry array, all serialized in the vmstate. A malicious migration stream can inject values >= U2FHID_PENDING_IN_NUM (32), causing an out-of-bounds heap read in u2f_pending_in_get() on the first USB IN token after migration completes.

Add a post_load callback to validate the restored indices and reset the ring buffer to empty on any out-of-range value. Pending packets are non-critical (the device will regenerate them), so resetting the ring is preferable to rejecting migration entirely.

As defense-in-depth, also add a bounds clamp in u2f_pending_in_get() so that even if corrupted state somehow reaches the consumer path, the index is folded into range before array access.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4483

Message-ID: <[email protected]>

---
 hw/usb/u2f.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
hw/usb/u2f.c +31 -0
index d6291852f631..9ca9879f3e0c 100644
@@ -228,6 +228,11 @@ static uint8_t *u2f_pending_in_get(U2FKeyState *key)
         return NULL;
     }
 
+    /* Clamp to valid range in case of corrupted state (e.g. migration) */
+    if (key->pending_in_start >= U2FHID_PENDING_IN_NUM) {
+        key->pending_in_start %= U2FHID_PENDING_IN_NUM;
+    }
+
     index = key->pending_in_start;
     key->pending_in_start = (index + 1) % U2FHID_PENDING_IN_NUM;
     --key->pending_in_num;
@@ -301,10 +306,36 @@ static void u2f_key_realize(USBDevice *dev, Error **errp)
     key->ep = usb_ep_get(dev, USB_TOKEN_IN, 1);
 }
 
+static int u2f_key_post_load(void *opaque, int version_id)
+{
+    U2FKeyState *key = opaque;
+
+    /*
+     * Validate pending_in ring buffer indices restored from the migration
+     * stream.  An attacker-controlled stream could inject values >= 32
+     * (U2FHID_PENDING_IN_NUM), causing out-of-bounds heap access in
+     * u2f_pending_in_get() on the first IN token after migration.
+     *
+     * Reset the ring on any invalid value: pending packets are non-critical
+     * (they will be regenerated by the device), so rejecting migration
+     * entirely is not necessary.
+     */
+    if (key->pending_in_start >= U2FHID_PENDING_IN_NUM ||
+        key->pending_in_end >= U2FHID_PENDING_IN_NUM ||
+        key->pending_in_num > U2FHID_PENDING_IN_NUM) {
+        key->pending_in_start = 0;
+        key->pending_in_end = 0;
+        key->pending_in_num = 0;
+    }
+
+    return 0;
+}
+
 const VMStateDescription vmstate_u2f_key = {
     .name = "u2f-key",
     .version_id = 1,
     .minimum_version_id = 1,
+    .post_load = u2f_key_post_load,
     .fields = (const VMStateField[]) {
         VMSTATE_USB_DEVICE(dev, U2FKeyState),
         VMSTATE_UINT8(idle, U2FKeyState),
Signature · 1 line
2.55.0
#19 lore raw

[PULL 16/22] hw/usb/u2f-emulated: Use QemuSemaphore rather than QemuCond

In reply to Philippe Mathieu-Daudé

The emulation thread only waits to be signalled that a packet is pending, which a semaphore expresses directly. This also avoids the Coverity complaint that the condition variable is not used in the standard lock / while (!predicate) / cond_wait pattern. Same change as ccid-card-emulated in previous commit 9590693b1d29 ("hw/usb/ccid-card-emulated: Use QemuSemaphore rather than QemuCond").

Message-Id: <[email protected]>

---
 hw/usb/u2f-emulated.c | 19 ++++++-------------
 1 file changed, 6 insertions(+), 13 deletions(-)
hw/usb/u2f-emulated.c +6 -13
index b82a31501934..0aabfc514b7b 100644
@@ -90,8 +90,7 @@ struct U2FEmulatedState {
     QemuMutex pending_out_mutex;
 
     /* Emulation thread and sync */
-    QemuCond key_cond;
-    QemuMutex key_mutex;
+    QemuSemaphore key_sem;
     QemuThread key_thread;
     bool stop_thread;
     EventNotifier notifier;
@@ -148,9 +147,7 @@ static void u2f_emulated_recv_from_guest(U2FKeyState *base,
     u2f_pending_out_add(key, packet);
     qemu_mutex_unlock(&key->pending_out_mutex);
 
-    qemu_mutex_lock(&key->key_mutex);
-    qemu_cond_signal(&key->key_cond);
-    qemu_mutex_unlock(&key->key_mutex);
+    qemu_sem_post(&key->key_sem);
 }
 
 static void *u2f_emulated_thread(void* arg)
@@ -162,9 +159,7 @@ static void *u2f_emulated_thread(void* arg)
 
     while (true) {
         /* Wait signal */
-        qemu_mutex_lock(&key->key_mutex);
-        qemu_cond_wait(&key->key_cond, &key->key_mutex);
-        qemu_mutex_unlock(&key->key_mutex);
+        qemu_sem_wait(&key->key_sem);
 
         /* Exit thread check */
         if (key->stop_thread) {
@@ -331,10 +326,9 @@ static void u2f_emulated_realize(U2FKeyState *base, Error **errp)
     event_notifier_set_handler(&key->notifier, u2f_emulated_event_handler);
 
     /* Synchronization */
-    qemu_cond_init(&key->key_cond);
+    qemu_sem_init(&key->key_sem, 0);
     qemu_mutex_init(&key->vdev_mutex);
     qemu_mutex_init(&key->pending_out_mutex);
-    qemu_mutex_init(&key->key_mutex);
     u2f_emulated_reset(key);
 
     /* Thread */
@@ -349,7 +343,7 @@ static void u2f_emulated_unrealize(U2FKeyState *base)
 
     /* Thread */
     key->stop_thread = true;
-    qemu_cond_signal(&key->key_cond);
+    qemu_sem_post(&key->key_sem);
     qemu_thread_join(&key->key_thread);
 
     /* Notifier */
@@ -357,9 +351,8 @@ static void u2f_emulated_unrealize(U2FKeyState *base)
     event_notifier_cleanup(&key->notifier);
 
     /* Synchronization */
-    qemu_cond_destroy(&key->key_cond);
+    qemu_sem_destroy(&key->key_sem);
     qemu_mutex_destroy(&key->vdev_mutex);
-    qemu_mutex_destroy(&key->key_mutex);
     qemu_mutex_destroy(&key->pending_out_mutex);
 
     /* Vdev */
Signature · 1 line
2.55.0
#20 lore raw

[PULL 17/22] hw/usb/dev-mtp: Fix recursive delete of directories

In reply to Philippe Mathieu-Daudé

From: Tommaso Califano <[email protected]>

usb_mtp_deletefn() only recursed into the grandchildren of the target, never deleting the files directly inside it. The directory was left non-empty, so rmdir() on the host always failed, and objects nested more than two levels deep were silently kept.

Recurse into every child of the target instead. Since the root directory can now actually become empty, skip the root object itself: rmdir()'ing it and freeing it would leave s->objects empty.

Fixes ec6206a68f70 ("usb-mtp: Support delete of mtp objects")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4529

Message-ID: <[email protected]>

---
 hw/usb/dev-mtp.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
hw/usb/dev-mtp.c +8 -7
index 8507e516ab1e..bc5a6f089178 100644
@@ -1152,19 +1152,20 @@ enum {
 
 static int usb_mtp_deletefn(MTPState *s, MTPObject *o, uint32_t trans)
 {
-    MTPObject *iter, *iter2, *iter2_next;
+    MTPObject *iter, *iter_next;
     int ret = 0;
 
     /*
      * TODO: Add support for Protection Status
      */
 
-    QLIST_FOREACH(iter, &o->children, list) {
-        if (iter->format == FMT_ASSOCIATION) {
-            QLIST_FOREACH_SAFE(iter2, &iter->children, list, iter2_next) {
-                ret |= usb_mtp_deletefn(s, iter2, trans);
-            }
-        }
+    QLIST_FOREACH_SAFE(iter, &o->children, list, iter_next) {
+        ret |= usb_mtp_deletefn(s, iter, trans);
+    }
+
+    /* Avoid deleting the mtp root */
+    if (o->parent == NULL) {
+        return ret ? ret : DELETE_SUCCESS;
     }
 
     if (o->format == FMT_UNDEFINED_OBJECT) {
Signature · 1 line
2.55.0
#21 lore raw

[PULL 22/22] hw/virtio/virtio-iommu: Fix IOVA address lookup

In reply to Philippe Mathieu-Daudé

From: Eric Auger <[email protected]>

The translation code checks the address to be translated belongs to mapped segments by calling g_tree_lookup_extended(). This uses the interval_cmp() function. However the interval is badly formed with the .low=addr and .high= addr + 1 instead of both being set to addr.

As a result an attempt to translate an unmapped byte just before a mapped segment will not fault as expected.

Example: only [0x2000, 0x2fff] IOVA range is mapped. An attempt to translate 0x1fff will succeed

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4754

Message-ID: <[email protected]>

---
 hw/virtio/virtio-iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/virtio/virtio-iommu.c +1 -1
index 208815096fc0..b9ecab01eee4 100644
@@ -1185,7 +1185,7 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr addr,
     GList *l;
 
     interval.low = addr;
-    interval.high = addr + 1;
+    interval.high = addr;
     granule = ctz64(s->config.page_size_mask);
 
     IOMMUTLBEntry entry = {
Signature · 1 line
2.55.0
#22 lore raw

[PULL 21/22] hw/xen: Mark legacy Xen backends as hotpluggable

In reply to Philippe Mathieu-Daudé

From: Peng Fan <[email protected]>

Commit 250e797ceada ("hw/xen: Have legacy Xen backend inherit from DYNAMIC_SYS_BUS_DEVICE") changed TYPE_XENBACKEND to inherit from TYPE_DYNAMIC_SYS_BUS_DEVICE, whose class sets hotpluggable = false.

Legacy Xen backends are created from xenstore at runtime, after the machine is ready, so device_initfn() marks them as hotplugged. The new class setting makes device_set_realized() reject them with:

Device 'xen-backend' does not support hotplugging

Since xen_be_get_xendev() realizes the device with &error_fatal, QEMU then exits. For example, "xl usbdev-attach" triggers this when the qusb backend is created dynamically, killing the guest. The same flag also causes qdev_unplug() to ignore these devices.

Override hotpluggable in xendev_class_init() to restore the behavior before the offending commit.

Fixes 250e797ceada ("hw/xen: Have legacy Xen backend inherit from DYNAMIC_SYS_BUS_DEVICE")

Message-ID: <[email protected]>

---
 hw/xen/xen-legacy-backend.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/xen/xen-legacy-backend.c +7 -0
index 7977b527127b..bf4396c9460d 100644
@@ -640,6 +640,13 @@ static void xendev_class_init(ObjectClass *klass, const void *data)
     DeviceClass *dc = DEVICE_CLASS(klass);
 
     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
+    /*
+     * Legacy Xen backends are created and destroyed dynamically from
+     * xenstore at runtime, after the machine has been initialized.
+     * TYPE_DYNAMIC_SYS_BUS_DEVICE marks devices as not hotpluggable,
+     * so override that here.
+     */
+    dc->hotpluggable = true;
     dc->bus_type = TYPE_XENSYSBUS;
 }
 
Signature · 1 line
2.55.0
#23 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 4 lines · Philippe Mathieu-Daudé

Tommaso Califano (3):
hw/usb/dev-mtp: Fix recursive delete of directories hw/usb/dev-mtp: Check length of class control request hw/usb/dev-mtp: Stop processing SendObjectInfo after an error

Hi!

Philippe, there were a 7-patch series by Tommaso, grouped into a single patch set because they touch the same file and all together fix a single issue --
https://gitlab.com/qemu-project/qemu/-/work_items/4529

Did you omit the other 4 changes because of the review comments?

The original patchset cover letter is
https://lore.kernel.org/qemu-devel/[email protected]/

Thanks,

/mjt

#24 lore raw
Quoted 7 lines · Michael Tokarev
Quoted 4 lines · Philippe Mathieu-Daudé

Tommaso Califano (3):
   hw/usb/dev-mtp: Fix recursive delete of directories hw/usb/dev-mtp: Check length of class control request hw/usb/dev-mtp: Stop processing SendObjectInfo after an error

Quoted 6 lines

Hi!

Philippe, there were a 7-patch series by Tommaso, grouped into a single patch set because they touch the same file and all together fix a single issue --
  https://gitlab.com/qemu-project/qemu/-/work_items/4529

I missed all are needed for the issue to be fixed :/

Quoted 1 line

Did you omit the other 4 changes because of the review comments?

Yeah, as individual fixes they seems worthwhile in the release, unfortunately I couldn't merge all of them because the comments were not addressed by the time we had to tag the RC.

Quoted 9 lines

The original patchset cover letter is
  https://lore.kernel.org/qemu-devel/20261112174907.158825-1-
[email protected]/

Thanks,

/mjt

#25 lore raw

Hi,

Quoted 8 lines · Philippe Mathieu-Daudé
Quoted 7 lines · Michael Tokarev
Quoted 4 lines · Philippe Mathieu-Daudé

Tommaso Califano (3):
   hw/usb/dev-mtp: Fix recursive delete of directories hw/usb/dev-mtp: Check length of class control request hw/usb/dev-mtp: Stop processing SendObjectInfo after an error

Quoted 9 lines
Quoted 6 lines

Hi!

Philippe, there were a 7-patch series by Tommaso, grouped into a single patch set because they touch the same file and all together fix a single issue --
  https://gitlab.com/qemu-project/qemu/-/work_items/4529

I missed all are needed for the issue to be fixed :/

Since this issue covers multiple independent bugs, feel free to cherry-pick only the commits you want. I’ll follow up with a new series for the rest, and either open a new GitLab issue for the remaining bugs or reopen the existing one once it auto-closes.

Quoted 6 lines
Quoted 1 line

Did you omit the other 4 changes because of the review comments?

Yeah, as individual fixes they seems worthwhile in the release, unfortunately I couldn't merge all of them because the comments were not addressed by the time we had to tag the RC.

Unfortunately, I just realized when (and what) the RC date is, and I had been holding off a bit waiting for reviews :(

Regards,
Tommaso