Skip to contentSkip to conversation

tcg patch queue 20260923

GIT PULL Applied Richard Henderson 1 participant 18 messages Sep 23 – Sep 24

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

The following changes since commit 014b1f730c0faa08067f8e9b5829e62ea3d4b177:

Merge tag 'pull-9p-20260923' of https://github.com/cschoenebeck/qemu into staging (2026-09-23 07:36:52 -0700)

are available in the Git repository at:

https://gitlab.com/rth7680/qemu.git tags/pull-tcg-20260923

for you to fetch changes up to 666879d5477ec41ab46fc10a27d18cbc13e8789e:

accel/tcg: fold the dynamic cflags into CPUState::tcg_cflags (2026-09-23 13:28:00 -0700)

----------------------------------------------------------------
accel: Change AccelOpsClass.update_guest_debug to void accel/tcg: Fix TLB_MMIO check in tlb_plugin_lookup() accel/tcg: Skip the can_do_io stores in user-only builds accel/tcg: Allow cross-page goto_tb chaining in user-only builds accel/tcg: fold the dynamic cflags into CPUState::tcg_cflags accel/tcg: Use TLB_FORCE_SLOW not TLB_MMIO for system plugins accel/tcg: Allow some address space wraparound during translation tcg/optimize: Fix fold_multiply2 vs 1
tcg/optimize: Fix expansion/simplification of deposit tcg/x86_64: Use the mulx insn from the bmi2 isa tcg/riscv64: Set vtype before whole-register vector loads

----------------------------------------------------------------
Anton Blanchard (1):
accel/tcg: Fix TLB_MMIO check in tlb_plugin_lookup()

Matt Turner (3):
accel/tcg: Skip the can_do_io stores in user-only builds accel/tcg: Allow cross-page goto_tb chaining in user-only builds accel/tcg: fold the dynamic cflags into CPUState::tcg_cflags

Richard Henderson (11):
accel/tcg: Use TLB_FORCE_SLOW not TLB_MMIO for system plugins tests/guest-debug/run-test: Add --pargs
tcg/optimize: Fix fold_multiply2 vs 1
tests/tcg/arm: Add regression test for #4448 tcg/optimize: Fix expansion/simplification of deposit tests/tcg/s390x: Add regression test for #4449 accel/tcg: Allow some address space wraparound during translation
tcg: Sort all_outop[] by INDEX name
tcg/x86_64: Use the mulx insn from the bmi2 isa tcg/riscv64: Set vtype before whole-register vector loads
accel: Change AccelOpsClass.update_guest_debug to void

Tao Cui (1):
tests/tcg/i386/system: Add regression test for translator_ld wraparound

 accel/tcg/internal-common.h             |  11 +-
 include/accel/accel-cpu-ops.h           |   2 +-
 include/exec/translator.h               |   3 +
 include/gdbstub/user.h                  |  11 +
 include/system/hvf_int.h                |   2 +-
 include/system/tcg.h                    |  12 ++
 tcg/x86_64/tcg-target-con-set.h         |   2 +
 accel/hvf/hvf-accel-ops.c               |   3 +-
 accel/kvm/kvm-accel-ops.c               |   4 +-
 accel/tcg/cpu-exec-common.c             |  37 +++-
 accel/tcg/cpu-exec.c                    |   3 +
 accel/tcg/cputlb.c                      |  16 +-
 accel/tcg/tcg-accel-ops.c               |   1 +
 accel/tcg/translator.c                  |  92 ++++++--
 cpu-target.c                            |   5 +-
 gdbstub/user.c                          |  14 ++
 monitor/hmp-cmds.c                      |   7 +
 system/runstate-hmp-cmds.c              |  18 +-
 tcg/optimize.c                          |  46 ++--
 tcg/tcg.c                               |  39 ++--
 tests/tcg/arm/issue4448.c               |  51 +++++
 tests/tcg/multiarch/test-xpage-chain.c  | 360 ++++++++++++++++++++++++++++++++
 tests/tcg/s390x/issue4449.c             |  31 +++
 tcg/riscv64/tcg-target.c.inc            |   6 +
 tcg/x86_64/tcg-target.c.inc             |  36 +++-
 tests/guest-debug/run-test.py           |   5 +
 tests/tcg/arm/meson.build               |   1 +
 tests/tcg/i386/system/meson.build       |   4 +
 tests/tcg/i386/system/wraparound.S      |  35 ++++
 tests/tcg/multiarch/gdbstub/xpage-bp.py |  37 ++++
 tests/tcg/multiarch/meson.build         |   7 +
 tests/tcg/s390x/meson.build             |   1 +
 32 files changed, 810 insertions(+), 92 deletions(-)
 create mode 100644 tests/tcg/arm/issue4448.c
 create mode 100644 tests/tcg/multiarch/test-xpage-chain.c
 create mode 100644 tests/tcg/s390x/issue4449.c
 create mode 100644 tests/tcg/i386/system/wraparound.S
 create mode 100644 tests/tcg/multiarch/gdbstub/xpage-bp.py
#1 lore raw

[PULL 02/16] accel/tcg: Fix TLB_MMIO check in tlb_plugin_lookup()

From: Anton Blanchard <[email protected]>

Update tlb_plugin_lookup() to check TLB_MMIO in slow_flags.

Fixes 24b5e0fdb543 ("include/exec: Move TLB_MMIO, TLB_DISCARD_WRITE to slow flags")

Message-ID: <[email protected]>

---
 accel/tcg/cputlb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
accel/tcg/cputlb.c +1 -1
index ae5af014960..dc83f3bad8c 100644
@@ -1589,7 +1589,7 @@ bool tlb_plugin_lookup(CPUState *cpu, vaddr addr, int mmu_idx,
     data->phys_addr = full->phys_addr | (addr & ~TARGET_PAGE_MASK);
 
     /* We must have an iotlb entry for MMIO */
-    if (tlb_addr & TLB_MMIO) {
+    if (full->slow_flags[access_type] & TLB_MMIO) {
         MemoryRegionSection *section = full->section;
         data->is_io = true;
         data->mr = section->mr;
Signature · 1 line
2.53.0
#2 lore raw

[PULL 03/16] accel/tcg: Skip the can_do_io stores in user-only builds

In reply to Richard Henderson

From: Matt Turner <[email protected]>

Every translation block stores to cpu->neg.can_do_io twice: false before the first instruction, true before the last one. Nothing reads it in a user-only build. There is no memory-mapped I/O in linux-user, and every reader is in system_ss: cputlb.c, watchpoint.c, icount-common.c and tcg-accel-ops-icount.c.

Message-ID: <[email protected]>

---
 accel/tcg/translator.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
accel/tcg/translator.c +13 -1
index 57daded60ff..e9943006947 100644
@@ -21,12 +21,14 @@
 #include "disas/disas.h"
 #include "tb-internal.h"
 
+#ifndef CONFIG_USER_ONLY
 static void set_can_do_io(DisasContextBase *db, bool val)
 {
     QEMU_BUILD_BUG_ON(sizeof_field(CPUState, neg.can_do_io) != 1);
     tcg_gen_st8_i32(tcg_constant_i32(val), tcg_env,
                     offsetof(CPUState, neg.can_do_io) - sizeof(CPUState));
 }
+#endif
 
 bool translator_io_start(DisasContextBase *db)
 {
@@ -125,8 +127,10 @@ void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns,
 {
     uint32_t cflags = tb_cflags(tb);
     TCGOp *icount_start_insn;
-    TCGOp *first_insn_start = NULL;
     bool plugin_enabled;
+#ifndef CONFIG_USER_ONLY
+    TCGOp *first_insn_start = NULL;
+#endif
 
     tcg_ctx->addr_type = addr_type;
 
@@ -160,9 +164,11 @@ void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns,
         *max_insns = ++db->num_insns;
         ops->insn_start(db, cpu);
         db->insn_start = tcg_last_op();
+#ifndef CONFIG_USER_ONLY
         if (first_insn_start == NULL) {
             first_insn_start = db->insn_start;
         }
+#endif
         tcg_debug_assert(db->is_jmp == DISAS_NEXT);  /* no early exit */
 
         if (plugin_enabled) {
@@ -207,9 +213,14 @@ void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns,
     ops->tb_stop(db, cpu);
     gen_tb_end(tb, cflags, icount_start_insn, db->num_insns);
 
+#ifndef CONFIG_USER_ONLY
     /*
      * Manage can_do_io for the translation block: set to false before
      * the first insn and set to true before the last insn.
+     *
+     * Nothing reads can_do_io in user-only builds.  There is no MMIO
+     * there, and every reader (cputlb.c, watchpoint.c, icount) is in
+     * system_ss, so skip the two stores per TB entirely.
      */
     if (db->num_insns == 1) {
         tcg_debug_assert(first_insn_start == db->insn_start);
@@ -221,6 +232,7 @@ void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns,
     tcg_ctx->emit_before_op = db->insn_start;
     set_can_do_io(db, true);
     tcg_ctx->emit_before_op = NULL;
+#endif
 
     /* May be used by disas_log or plugin callbacks. */
     tb->size = db->pc_next - db->pc_first;
Signature · 1 line
2.53.0
#3 lore raw

[PULL 01/16] accel/tcg: Use TLB_FORCE_SLOW not TLB_MMIO for system plugins

In reply to Richard Henderson

In b79a9b6e5b, we stopped using TLB_MMIO for user-only plugins, but erroneously thought we were already doing the same for system. Do that now.

---
 accel/tcg/cputlb.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
accel/tcg/cputlb.c +5 -9
index 7f7c208ba12..ae5af014960 100644
@@ -1369,7 +1369,6 @@ static int probe_access_internal(CPUState *cpu, vaddr addr,
     uint64_t tlb_addr = tlb_read_idx(entry, access_type);
     vaddr page_addr = addr & TARGET_PAGE_MASK;
     int flags = TLB_FLAGS_MASK & ~TLB_FORCE_SLOW;
-    bool force_mmio = check_mem_cbs && cpu_plugin_mem_cbs_enabled(cpu);
     CPUTLBEntryFull *full;
 
     if (!tlb_hit_page(tlb_addr, page_addr)) {
@@ -1399,16 +1398,13 @@ static int probe_access_internal(CPUState *cpu, vaddr addr,
 
     *pfull = full = &cpu->neg.tlb.d[mmu_idx].fulltlb[index];
     flags |= full->slow_flags[access_type];
-
-    /* Fold all "mmio-like" bits into TLB_MMIO.  This is not RAM.  */
-    if (unlikely(flags & ~(TLB_WATCHPOINT | TLB_NOTDIRTY | TLB_CHECK_ALIGNED))
-        || (access_type != MMU_INST_FETCH && force_mmio)) {
-        *phost = NULL;
-        return TLB_MMIO;
+    if (check_mem_cbs && cpu_plugin_mem_cbs_enabled(cpu)) {
+        flags |= TLB_FORCE_SLOW;
     }
 
-    /* Everything else is RAM. */
-    *phost = (void *)((uintptr_t)addr + entry->addend);
+    *phost = (flags & ~(TLB_WATCHPOINT | TLB_NOTDIRTY | TLB_CHECK_ALIGNED)
+              ? NULL
+              : (void *)((uintptr_t)addr + entry->addend));
     return flags;
 }
 
Signature · 1 line
2.53.0
#4 lore raw

[PULL 09/16] tests/tcg/s390x: Add regression test for #4449

In reply to Richard Henderson
---
 tests/tcg/s390x/issue4449.c | 31 +++++++++++++++++++++++++++++++
 tests/tcg/s390x/meson.build |  1 +
 2 files changed, 32 insertions(+)
 create mode 100644 tests/tcg/s390x/issue4449.c
tests/tcg/s390x/issue4449.c new file +31 -0
new file mode 100644
index 00000000000..0c9c80e8cac
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include <assert.h>
+
+#define CASE(name, old, cst, sh)                                        \
+    static void __attribute__((noinline)) case_##name(void) {           \
+        unsigned long t1, got;                                          \
+        __asm__("lhi  %0," #cst "\n\t"   /* low 32 known, high not */   \
+                "lghi %1," #old "\n\t"   /* whole register constant */  \
+                "sllk %1,%0," #sh "\n\t" /* 32-bit write = deposit */   \
+                "brc 15,1f\n\t"          /* ends the translation block */ \
+                "nop\n"                                                 \
+                "1:"                                                    \
+                : "=r"(t1), "=r"(got) : : );                            \
+        unsigned long want = (unsigned)(cst) << (sh);                   \
+        assert(got == want);                                            \
+    }
+
+CASE(a, 256, 128, 1);
+CASE(b, 128, 128, 0);
+CASE(c, 512, 128, 2);
+CASE(d,   2,   1, 1);
+
+int main()
+{
+    case_a();
+    case_b();
+    case_c();
+    case_d();
+    return 0;
+}
tests/tcg/s390x/meson.build +1 -0
index 57ea6c5ca55..39a480033de 100644
@@ -84,6 +84,7 @@ tests += {
   'cpacf-pcc.c': {},
   'cpacf-pckmo.c': {},
   'cpacf-prno.c': {},
+  'issue4449.c': {},
 }
 
 foreach src: tests_s390x_pgm
Signature · 1 line
2.53.0
#5 lore raw

[PULL 16/16] accel/tcg: fold the dynamic cflags into CPUState::tcg_cflags

In reply to Richard Henderson

From: Matt Turner <[email protected]>

curr_cflags() is called once per TB dispatch, from helper_lookup_tb_ptr() and from the cpu_exec() loop. It recomputes the same value every time:

    uint32_t cflags = cpu->tcg_cflags;
    if (unlikely(cpu_single_stepping(cpu))) { ... }
    else if (qatomic_read(&one_insn_per_tb)) { ... }
    else if (qemu_loglevel_mask(CPU_LOG_TB_NOCHAIN)) { ... }

That is three loads and three branches on the hottest path in the interpreter, for state that changes only when gdb enables single-step, when one-insn-per-tb is toggled, or when the log mask changes.

None of the three has to be sampled at dispatch time. Fold each into CPUState::tcg_cflags where it changes and curr_cflags() becomes a single load of a field that TB lookup has to read anyway.

The derived bits -- CF_COUNT_MASK, CF_NO_GOTO_TB, CF_NO_GOTO_PTR and CF_SINGLE_STEP -- are never set by tcg_cflags_set(), so tcg_update_cflags() can recompute them in place without disturbing the rest, and conversely tcg_cflags_set() ORs in its bits without disturbing them.

There are three places to call it:

- tcg_exec_realizefn(), so that a CPU created after the command line has been parsed starts out with the right value. This covers user-only, where tcg_cpu_init_cflags() is not reached. linux-user's cpu_copy() copies tcg_cflags wholesale, so a cloned thread inherits it.

- cpu_single_step(), which changes one CPU. gdb is the only caller that matters; in system mode it runs with the vCPUs stopped, and in user mode gdb_continue_partial() can reach a thread that is still running, because gdb_handlesig() stops only the thread that trapped. That is exactly the plain cross-thread store to another CPU's CPUState that cpu->singlestep_flags already was, read back by that CPU through cpu_single_stepping() in curr_cflags(). This patch changes which field carries it, not who writes it or how.

- hmp_one_insn_per_tb() and hmp_log(), which change every CPU while the vCPUs are running, so the update is queued with async_run_on_cpu() and each CPU writes its own cflags from its own thread. The command line spellings of those two settings need nothing: they are parsed before any CPU is realized, so tcg_exec_realizefn() picks them up.

Measured with qemu-alpha running an emulated alpha gcc 16.2.0 compiling the SQLite 3.45.1 amalgamation (255k lines, -O2) on an x86-64 host, in a build configured with --enable-lto:

    before: 1,646,994,254,249 instructions
    after:  1,562,204,796,597 instructions   -5.15%

That workload issues 8.4 billion dispatches, so the per-call saving is small but the aggregate is not. The emulated compiler produces byte-identical output before and after.

Wall clock does not move: 133.19s to 132.58s, a 0.46% difference against a run-to-run spread larger than that. The removed work is a few predictable loads and branches that the host executes largely in parallel with the surrounding dispatch, so this patch is worth taking for the instruction count and for what it enables, not for a time saving that can be measured on its own.

v4: Update the cflags from the HMP handlers for 'log' and 'one-insn-per-tb'
rather than from qemu_set_log_internal() and the accelerator property setter. Those are the paths that reach a running vCPU, and the monitor is the only thing that does. Suggested by Richard Henderson.

v4: Queue the per-CPU update with async_run_on_cpu() rather than
async_safe_run_on_cpu(). Halting the other vCPUs buys nothing: the queued work already runs on the owning CPU's own thread. Suggested by Alex Bennee, who also asked whether there are cross-vCPU updates of tcg_cflags at all. With this change the monitor path has none: the only remaining writer from another thread is cpu_single_step(), above, which is neither new nor made worse here.

v4: Move the stub to accel/stubs/, which is where the other accelerator
stubs live.

Message-ID: <[email protected]>

---
 accel/tcg/internal-common.h | 11 +++++++++--
 include/system/tcg.h        | 12 ++++++++++++
 accel/tcg/cpu-exec-common.c | 37 ++++++++++++++++++++++++++++++++++---
 accel/tcg/cpu-exec.c        |  3 +++
 accel/tcg/tcg-accel-ops.c   |  1 +
 cpu-target.c                |  5 ++++-
 monitor/hmp-cmds.c          |  7 +++++++
 system/runstate-hmp-cmds.c  | 18 ++++++++++++------
 8 files changed, 82 insertions(+), 12 deletions(-)
accel/tcg/internal-common.h +9 -2
index 9e7be2d78df..853d1b51eeb 100644
@@ -69,8 +69,15 @@ void tlb_destroy(CPUState *cpu);
 bool tcg_exec_realizefn(CPUState *cpu, Error **errp);
 void tcg_exec_unrealizefn(CPUState *cpu);
 
-/* current cflags for hashing/comparison */
-uint32_t curr_cflags(CPUState *cpu);
+/*
+ * Current cflags for hashing/comparison.  Everything that feeds into the
+ * value is folded into CPUState::tcg_cflags when it changes, by
+ * tcg_update_cflags(), so that TB dispatch only has to load it.
+ */
+static inline uint32_t curr_cflags(CPUState *cpu)
+{
+    return cpu->tcg_cflags;
+}
 
 void tb_check_watchpoint(CPUState *cpu, uintptr_t retaddr);
 
include/system/tcg.h +12 -0
index 7622dcea302..2c2dbc753b3 100644
@@ -17,6 +17,18 @@ extern bool tcg_allowed;
 #define tcg_enabled() 0
 #endif
 
+/*
+ * Recompute the parts of CPUState::tcg_cflags that TB dispatch consumes but
+ * tcg_cflags_set() does not provide: gdb single-step, one-insn-per-tb and
+ * the CPU_LOG_TB_NOCHAIN log flag.  Call whenever one of those changes.
+ *
+ * tcg_update_cflags() updates one CPU and must be called from that CPU's
+ * thread, or with it stopped.  tcg_update_all_cflags() updates every CPU
+ * and is safe to call from the monitor while the vCPUs run.
+ */
+void tcg_update_cflags(CPUState *cpu);
+void tcg_update_all_cflags(void);
+
 /**
  * qemu_tcg_mttcg_enabled:
  * Check whether we are running MultiThread TCG or not.
accel/tcg/cpu-exec-common.c +34 -3
index 44e84344f3b..68077c8fd9d 100644
@@ -21,11 +21,19 @@
 #include "exec/log.h"
 #include "system/tcg.h"
 #include "qemu/plugin.h"
+#include "qemu/main-loop.h"
 #include "accel/tcg/cpu-loop.h"
 #include "internal-common.h"
 
 bool tcg_allowed;
 
+/*
+ * The bits of CPUState::tcg_cflags that tcg_cflags_set() never sets, because
+ * they are derived from gdb single-step, one-insn-per-tb and -d nochain.
+ */
+#define CF_DERIVED  (CF_COUNT_MASK | CF_NO_GOTO_TB | CF_NO_GOTO_PTR | \
+                     CF_SINGLE_STEP)
+
 bool tcg_cflags_has(CPUState *cpu, uint32_t flags)
 {
     return cpu->tcg_cflags & flags;
@@ -33,12 +41,13 @@ bool tcg_cflags_has(CPUState *cpu, uint32_t flags)
 
 void tcg_cflags_set(CPUState *cpu, uint32_t flags)
 {
+    assert((flags & CF_DERIVED) == 0);
     cpu->tcg_cflags |= flags;
 }
 
-uint32_t curr_cflags(CPUState *cpu)
+void tcg_update_cflags(CPUState *cpu)
 {
-    uint32_t cflags = cpu->tcg_cflags;
+    uint32_t cflags = cpu->tcg_cflags & ~CF_DERIVED;
 
     /*
      * Record gdb single-step.  We should be exiting the TB by raising
@@ -55,7 +64,29 @@ uint32_t curr_cflags(CPUState *cpu)
         cflags |= CF_NO_GOTO_TB;
     }
 
-    return cflags;
+    cpu->tcg_cflags = cflags;
+}
+
+static void tcg_update_cflags_work(CPUState *cpu, run_on_cpu_data data)
+{
+    tcg_update_cflags(cpu);
+}
+
+void tcg_update_all_cflags(void)
+{
+    CPUState *cpu;
+
+    g_assert(bql_locked());
+
+    /*
+     * one-insn-per-tb and -d nochain can both be changed from the monitor
+     * while the vCPUs are running.  Queue the update onto each CPU rather
+     * than writing tcg_cflags from here, so that the field is only ever
+     * written by the CPU that owns it.
+     */
+    CPU_FOREACH(cpu) {
+        async_run_on_cpu(cpu, tcg_update_cflags_work, RUN_ON_CPU_NULL);
+    }
 }
 
 /* exit the current TB, but without causing any exception to be raised */
accel/tcg/cpu-exec.c +3 -0
index 46b723cb734..5226cfb7650 100644
@@ -1071,6 +1071,9 @@ bool tcg_exec_realizefn(CPUState *cpu, Error **errp)
         tcg_target_initialized = true;
     }
 
+    /* Pick up one-insn-per-tb and -d nochain from the command line. */
+    tcg_update_cflags(cpu);
+
     cpu->tb_jmp_cache = g_new0(CPUJumpCache, 1);
     tlb_init(cpu);
 #ifndef CONFIG_USER_ONLY
accel/tcg/tcg-accel-ops.c +1 -0
index 9c3d2214162..61e0051b53b 100644
@@ -222,6 +222,7 @@ static void tcg_accel_ops_init(AccelClass *ac)
     ops->insert_gdbstub_breakpoint = tcg_insert_gdbstub_breakpoint;
     ops->remove_gdbstub_breakpoint = tcg_remove_gdbstub_breakpoint;
     ops->remove_all_gdbstub_breakpoints = tcg_remove_all_gdbstub_breakpoints;
+    ops->update_guest_debug = tcg_update_cflags;
 }
 
 static void tcg_accel_ops_class_init(ObjectClass *oc, const void *data)
cpu-target.c +4 -1
index 4783845c9bf..9bd1605e831 100644
@@ -24,6 +24,7 @@
 #include "exec/replay-core.h"
 #include "exec/log.h"
 #include "hw/core/cpu.h"
+#include "system/tcg.h"
 #include "trace/trace-root.h"
 
 /* enable or disable single step mode. EXCP_DEBUG is returned by the
@@ -35,7 +36,9 @@ void cpu_single_step(CPUState *cpu, unsigned flags)
                                           cpu->singlestep_flags, flags);
         cpu->singlestep_flags = flags;
 
-#if !defined(CONFIG_USER_ONLY)
+#ifdef CONFIG_USER_ONLY
+        tcg_update_cflags(cpu);
+#else
         const AccelOpsClass *ops = cpus_get_accel();
         if (ops->update_guest_debug) {
             ops->update_guest_debug(cpu);
monitor/hmp-cmds.c +7 -0
index 91701ddf331..9b1b94f8ea9 100644
@@ -40,6 +40,7 @@
 #include "system/hw_accel.h"
 #include "system/memory.h"
 #include "system/system.h"
+#include "system/tcg.h"
 #include "disas/disas.h"
 
 /* Please update hmp-commands.hx when adding or changing commands */
@@ -340,6 +341,12 @@ void hmp_log(MonitorHMP *hmp, const QDict *qdict)
 
     if (!qemu_set_log(mask, &err)) {
         error_report_err(err);
+        return;
+    }
+
+    /* CPU_LOG_TB_NOCHAIN feeds into the per-CPU cflags. */
+    if (tcg_enabled()) {
+        tcg_update_all_cflags();
     }
 }
 
system/runstate-hmp-cmds.c +12 -6
index ad70b53f8ab..eb34d14d062 100644
@@ -22,6 +22,7 @@
 #include "qapi/qapi-commands-run-state.h"
 #include "qobject/qdict.h"
 #include "qemu/accel.h"
+#include "system/tcg.h"
 
 void hmp_info_status(MonitorHMP *hmp, const QDict *qdict)
 {
@@ -43,16 +44,17 @@ void hmp_info_status(MonitorHMP *hmp, const QDict *qdict)
 
 void hmp_one_insn_per_tb(MonitorHMP *hmp, const QDict *qdict)
 {
-    const char *option = qdict_get_try_str(qdict, "option");
-    AccelState *accel = current_accel();
+    const char *option;
+    AccelState *accel;
     bool newval;
 
-    if (!object_property_find(OBJECT(accel), "one-insn-per-tb")) {
-        monitor_hmp_printf(hmp,
-                           "This accelerator does not support setting one-insn-per-tb\n");
+    if (!tcg_enabled()) {
+        monitor_hmp_printf(hmp, "This accelerator does not support "
+                           "setting one-insn-per-tb\n");
         return;
     }
 
+    option = qdict_get_try_str(qdict, "option");
     if (!option || !strcmp(option, "on")) {
         newval = true;
     } else if (!strcmp(option, "off")) {
@@ -61,9 +63,13 @@ void hmp_one_insn_per_tb(MonitorHMP *hmp, const QDict *qdict)
         monitor_hmp_printf(hmp, "unexpected option %s\n", option);
         return;
     }
-    /* If the property exists then setting it can never fail */
+
+    accel = current_accel();
     object_property_set_bool(OBJECT(accel), "one-insn-per-tb",
                              newval, &error_abort);
+
+    /* one-insn-per-tb feeds into the per-CPU cflags. */
+    tcg_update_all_cflags();
 }
 
 void hmp_watchdog_action(MonitorHMP *hmp, const QDict *qdict)
Signature · 1 line
2.53.0
#6 lore raw

[PULL 05/16] accel/tcg: Allow cross-page goto_tb chaining in user-only builds

In reply to Richard Henderson

From: Matt Turner <[email protected]>

translator_use_goto_tb() refuses to chain unless the destination is on the same page as the start of the TB. For guests whose text is much larger than a page this is expensive: an emulated alpha gcc compiling a 255k line translation unit takes the indirect dispatch path for 8.4 billion of its 34.2 billion TB exits, and a large share of those are ordinary direct branches that simply crossed an 8 KiB page boundary.

The restriction was made unconditional by d3a2a1d803 ("accel/tcg:
Introduce translator_use_goto_tb"), whose rationale was:

Various targets avoid the page crossing test for CONFIG_USER_ONLY, but that is wrong: mmap and mprotect can change page permissions.

That is true, but in user-only builds the invalidation path already covers it. There are no page tables: every mmap, mprotect and munmap reaches page_set_flags(), which calls tb_invalidate_phys_range() whenever the flags actually change, and tb_phys_invalidate() calls tb_jmp_unlink() to reset incoming jumps. A chained cross-page jump is therefore broken whenever the destination page's permissions change. This is not true in system mode, where TBs are keyed by physical address and a page table change invalidates nothing, so the restriction is kept there.

The rule protects one more thing, which the original rationale does not
mention: it guarantees that execution cannot enter a page without a TB lookup, and so without check_for_breakpoints(). That is what makes a breakpoint set after a block was translated take effect, since insertion deliberately invalidates nothing. A link established before the breakpoint was set would jump straight over it.

So the chaining is only enabled for a run that can never acquire a breakpoint. In user-only mode every breakpoint comes from gdb -- BP_CPU is g_assert_not_reached() there, and the guest cannot ask for one -- and gdb has to be requested with -g before the first block is translated, even though with suspend=n it may connect later. gdb_may_set_breakpoints() reports whether it was, and is fixed for the lifetime of the process.

Add tests/tcg/multiarch/test-xpage-chain.c to cover both hazards directly. It writes the last instruction of one page and the first of the next, so that the fall-through between them is a cross-page goto_tb, runs it 200000 times so the chain is established, then checks that mprotect(PROT_NONE) makes the next call fault, and that different code written into the page once it is mapped back runs rather than a stale translation.

The two instructions -- set the return value register, and return -- are all the architecture specific code there is; thirteen architectures supply them and the rest skip.

The test detects the hazard it is meant to detect: with the tb_invalidate_phys_range() call in page_set_flags() commented out, it fails both phases, executing page B after PROT_NONE and returning the stale result.

Run with -b, the same binary stops once the chain is established and lets tests/tcg/multiarch/gdbstub/xpage-bp.py set a breakpoint on the far side of it, which the next call has to stop on.
With gdb_may_set_breakpoints() forced to false so that the chaining stays on under gdb, that breakpoint is missed and the test fails, which is what makes it a test of the gate rather than of gdb.

[rth: Update for meson test infrastructure]

Message-ID: <[email protected]>

---
 include/gdbstub/user.h                  |  11 +
 accel/tcg/translator.c                  |  33 ++-
 gdbstub/user.c                          |  14 +
 tests/tcg/multiarch/test-xpage-chain.c  | 360 ++++++++++++++++++++++++
 tests/tcg/multiarch/gdbstub/xpage-bp.py |  37 +++
 tests/tcg/multiarch/meson.build         |   7 +
 6 files changed, 461 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/multiarch/test-xpage-chain.c
 create mode 100644 tests/tcg/multiarch/gdbstub/xpage-bp.py
include/gdbstub/user.h +11 -0
index 654986d483b..c091cd97586 100644
@@ -11,6 +11,17 @@
 
 #define MAX_SIGINFO_LENGTH 128
 
+/**
+ * gdb_may_set_breakpoints() - whether a breakpoint can ever be inserted
+ *
+ * In user-only mode every breakpoint comes from gdb, and gdb is only ever
+ * reachable if -g was given at startup, before the guest ran a single
+ * instruction.  A run that has no gdbstub can therefore never acquire a
+ * breakpoint, which lets translation take shortcuts that a breakpoint
+ * would invalidate.  Stays true once true, even if gdb detaches.
+ */
+bool gdb_may_set_breakpoints(void);
+
 /**
  * gdb_handlesig() - yield control to gdb
  * @cpu: CPU
accel/tcg/translator.c +32 -1
index e9943006947..3f08c1c15c7 100644
@@ -15,6 +15,9 @@
 #include "accel/tcg/cpu-mmu-index.h"
 #include "exec/target_page.h"
 #include "exec/translator.h"
+#ifdef CONFIG_USER_ONLY
+#include "gdbstub/user.h"
+#endif
 #include "exec/plugin-gen.h"
 #include "tcg/tcg-op-common.h"
 #include "internal-common.h"
@@ -110,6 +113,34 @@ bool translator_is_same_page(const DisasContextBase *db, vaddr addr)
     return ((addr ^ db->pc_first) & TARGET_PAGE_MASK) == 0;
 }
 
+/*
+ * Whether a direct jump may be chained to a destination outside the page
+ * the TB started in.
+ *
+ * In user-only mode there are no page tables.  Every mmap, mprotect and
+ * munmap goes through page_set_flags(), which calls tb_invalidate_phys_range()
+ * whenever a change in flags so warrants, and tb_phys_invalidate() unlinks
+ * incoming jumps.  A cross-page link is therefore broken whenever the
+ * destination page's permissions change.
+ *
+ * What the same-page rule also provides is that execution cannot enter a page
+ * without a TB lookup, and so without check_for_breakpoints(), which is what
+ * makes a breakpoint set after a block was translated take effect.  Nothing
+ * invalidates on breakpoint insertion, so a link established beforehand would
+ * jump straight over it.  In user-only mode breakpoints only ever come from
+ * gdb -- BP_CPU is g_assert_not_reached() there and the guest has no way to
+ * ask for one -- and gdb has to be requested with -g before the first block
+ * is translated, so a run that has no gdbstub can never acquire a breakpoint.
+ */
+static bool use_cross_page_goto_tb(void)
+{
+#ifdef CONFIG_USER_ONLY
+    return !gdb_may_set_breakpoints();
+#else
+    return false;
+#endif
+}
+
 bool translator_use_goto_tb(DisasContextBase *db, vaddr dest)
 {
     /* Suppress goto_tb if requested. */
@@ -118,7 +149,7 @@ bool translator_use_goto_tb(DisasContextBase *db, vaddr dest)
     }
 
     /* Check for the dest on the same page as the start of the TB.  */
-    return translator_is_same_page(db, dest);
+    return use_cross_page_goto_tb() || translator_is_same_page(db, dest);
 }
 
 void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns,
gdbstub/user.c +14 -0
index 9e6f9a6f376..d810f0f38c3 100644
@@ -470,6 +470,18 @@ static void *gdbserver_accept_thread(void *arg)
 
 #define USAGE "\nUsage: -g {port|path}[,suspend={y|n}]"
 
+/*
+ * Set before the guest runs and never cleared, so that code translated at
+ * any point can rely on it: with suspend=n gdb may connect long after
+ * startup, and once connected it can insert a breakpoint at any time.
+ */
+static bool gdbserver_requested;
+
+bool gdb_may_set_breakpoints(void)
+{
+    return gdbserver_requested;
+}
+
 bool gdbserver_start(const char *args, Error **errp)
 {
     g_auto(GStrv) argv = g_strsplit(args, ",", 0);
@@ -513,6 +525,8 @@ bool gdbserver_start(const char *args, Error **errp)
         return false;
     }
 
+    gdbserver_requested = true;
+
     if (suspend) {
         if (gdbserver_accept(port, gdb_fd, port_or_path)) {
             gdb_handlesig(first_cpu, 0, NULL, NULL, 0);
tests/tcg/multiarch/test-xpage-chain.c new file +360 -0

360 lines not on this page · raw message

tests/tcg/multiarch/gdbstub/xpage-bp.py new file +37 -0
new file mode 100644
index 00000000000..f40024f16de
@@ -0,0 +1,37 @@
+"""Test that a breakpoint set after a cross-page chain is established is hit.
+
+translator_use_goto_tb() lets a direct branch chain to another page in
+user-only builds, which is only safe because a run with no gdbstub can never
+acquire a breakpoint.  This runs with one, so the chaining must be off and
+the breakpoint must still be reached.
+
+This runs as a sourced script (via -x, via run-test.py).
+
+SPDX-License-Identifier: GPL-2.0-or-later
+"""
+from test_gdbstub import main, report
+
+
+def run_test():
+    """Run through the tests one by one"""
+    gdb.Breakpoint("break_here")
+    gdb.execute("continue")
+
+    # The chain exists by now; put a breakpoint on the far side of it.
+    target = int(gdb.parse_and_eval("(unsigned long)page_b_entry"))
+    if target == 0:
+        report(True, "no code emitters for this architecture, skipped")
+        return
+    gdb.execute("break *{}".format(target))
+    gdb.execute("continue")
+
+    pc = int(gdb.parse_and_eval("(unsigned long)$pc"))
+    report(pc == target, "stopped at {:#x}, expected {:#x}".format(pc, target))
+
+    gdb.execute("delete")
+    gdb.execute("continue")
+    exitcode = int(gdb.parse_and_eval("$_exitcode"))
+    report(exitcode == 0, "{} == 0".format(exitcode))
+
+
+main(run_test)
tests/tcg/multiarch/meson.build +7 -0
index 508fdb585b7..c3816d3a218 100644
@@ -48,6 +48,7 @@ tests += {
   multiarch/'sigreturn-sigmask.c': {'cflags': ['-lpthread']},
   multiarch/'tb-link.c': {'cflags': ['-lpthread']},
   multiarch/'test-mmap.c': {},
+  multiarch/'test-xpage-chain.c': {'cflags': ['-lpthread']},
   multiarch/'testthread.c': {'cflags': ['-lpthread']},
   multiarch/'threadcount.c': {'cflags': ['-lpthread']},
 }
@@ -118,6 +119,12 @@ tests += {
     'gdb_test': ['--test', files('gdbstub/follow-fork-mode-parent.py')],
   },
 }
+tests += {
+  multiarch/'test-xpage-chain.c': {
+    'test_name': 'xpage-chain',
+    'gdb_test': ['--test', files('gdbstub/xpage-bp.py'), '--pargs=-b'],
+  },
+}
 
 # Specific plugin tests
 # Test plugin memory access instrumentation
Signature · 1 line
2.53.0
#7 lore raw

[PULL 04/16] tests/guest-debug/run-test: Add --pargs

In reply to Richard Henderson

Add --pargs=<string> to pass command-line arguments to the user-mode program. Assert when using --pargs with a system mode program.

---
 tests/guest-debug/run-test.py | 5 +++++
 1 file changed, 5 insertions(+)
tests/guest-debug/run-test.py +5 -0
index 75e9c92e036..838c1c26971 100755
@@ -24,6 +24,7 @@ def get_args():
     parser.add_argument("--qemu", help="Qemu binary for test",
                         required=True)
     parser.add_argument("--qargs", help="Qemu arguments for test")
+    parser.add_argument("--pargs", help="Program arguments for test")
     parser.add_argument("--binary", help="Binary to debug",
                         required=True)
     parser.add_argument("--test", help="GDB test script")
@@ -81,6 +82,8 @@ def log(output, msg):
             suspend = ' -S'
         cmd = f'{args.qemu} {args.qargs} {args.binary}' \
             f'{suspend} -gdb unix:path={socket_name},server=on'
+        # There is no guest program command-line in system mode.
+        assert not args.pargs
     else:
         if args.no_suspend:
             suspend = ',suspend=n'
@@ -88,6 +91,8 @@ def log(output, msg):
             suspend = ''
         cmd = f'{args.qemu} {args.qargs} -g {socket_name}{suspend}' \
             f' {args.binary}'
+        if args.pargs:
+            cmd += f' {args.pargs}'
 
     log(output, "QEMU CMD: %s" % (cmd))
     inferior = subprocess.Popen(shlex.split(cmd))
Signature · 1 line
2.53.0
#8 lore raw

[PULL 08/16] tcg/optimize: Fix expansion/simplification of deposit

In reply to Richard Henderson

When we computed z_mask/o_mask for the deposit, we may accidentally prove that the result is a constant.

If we let the rest of the code expand as-is, when can we get to the end we'll see the constant, note that it's the same as the current value, remove what we think is "the opcode" that computes this same value. But in this case we've been expanding deposit to multiple opcodes and the removal of only the last opcode breaks the expansion.

Fix this by noting the constant result early, before expanding, allowing the entire operation to be removed.

Fixes 5f747705a4 ("tcg/optimize: Lower unsupported deposit during optimize")

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

---
 tcg/optimize.c | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)
tcg/optimize.c +17 -12
index bd91220650b..1e4d2a0a425 100644
@@ -1708,6 +1708,23 @@ static bool fold_deposit(OptContext *ctx, TCGOp *op)
     type_mask = MAKE_64BIT_MASK(0, width);
     len_mask = MAKE_64BIT_MASK(0, len);
 
+    /*
+     * Compute result masks before calling other fold_* subroutines
+     * which could modify the masks of our inputs.
+     */
+    z_mask = deposit64(t1->z_mask, ofs, len, t2->z_mask);
+    o_mask = deposit64(t1->o_mask, ofs, len, t2->o_mask);
+    if (ofs + len < width) {
+        s_mask = t1->s_mask & ~MAKE_64BIT_MASK(0, ofs + len);
+    } else {
+        s_mask = t2->s_mask << ofs;
+    }
+
+    /* Sometimes we prove a constant from non-constants. */
+    if (z_mask == o_mask) {
+        return tcg_opt_gen_movi(ctx, op, op->args[0], z_mask);
+    }
+
     /* Inserting all-zero into a value. */
     if ((t2->z_mask & len_mask) == 0) {
         op->opc = INDEX_op_and;
@@ -1740,18 +1757,6 @@ static bool fold_deposit(OptContext *ctx, TCGOp *op)
         return fold_or(ctx, op);
     }
 
-    /*
-     * Compute result masks before calling other fold_* subroutines
-     * which could modify the masks of our inputs.
-     */
-    z_mask = deposit64(t1->z_mask, ofs, len, t2->z_mask);
-    o_mask = deposit64(t1->o_mask, ofs, len, t2->o_mask);
-    if (ofs + len < width) {
-        s_mask = t1->s_mask & ~MAKE_64BIT_MASK(0, ofs + len);
-    } else {
-        s_mask = t2->s_mask << ofs;
-    }
-
     /* Inserting a value into zero. */
     if (ti_is_const_val(t1, 0)) {
         uint64_t need_mask;
Signature · 1 line
2.53.0
#9 lore raw

[PULL 06/16] tcg/optimize: Fix fold_multiply2 vs 1

In reply to Richard Henderson

For muls2, we returned from fold_multiply2 without finish_folding or any other mechanism to update value tracking for the rh output. We might as well do this via fold_shift, which will also mark rh as having all copies of the sign bit.

For the movi paths of multiply vs 0 or mulu2, we're better off tail calling to tcg_opt_gen_movi rather than explicitly returning true.

Fixes c9349965ce4 ("tcg: Optimize INDEX_op_mul[us]2 for 0 and 1")

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

---
 tcg/optimize.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
tcg/optimize.c +7 -10
index d291c844ca8..bd91220650b 100644
@@ -2213,6 +2213,7 @@ static bool fold_mul_highpart(OptContext *ctx, TCGOp *op)
     return finish_folding(ctx, op);
 }
 
+static bool fold_shift(OptContext *ctx, TCGOp *op);
 static bool fold_multiply2(OptContext *ctx, TCGOp *op)
 {
     swap_commutative(op->args[0], &op->args[2], &op->args[3]);
@@ -2253,15 +2254,13 @@ static bool fold_multiply2(OptContext *ctx, TCGOp *op)
             /* The proper opcode is supplied by tcg_opt_gen_mov. */
             op2 = opt_insert_before(ctx, op, 0, 2);
             tcg_opt_gen_movi(ctx, op, rl, l);
-            tcg_opt_gen_movi(ctx, op2, rh, h);
-            return true;
+            return tcg_opt_gen_movi(ctx, op2, rh, h);
         }
 
         if (b == 0) {
             op2 = opt_insert_before(ctx, op, 0, 2);
             tcg_opt_gen_movi(ctx, op2, rl, 0);
-            tcg_opt_gen_movi(ctx, op, rh, 0);
-            return true;
+            return tcg_opt_gen_movi(ctx, op, rh, 0);
         }
         if (b == 1) {
             op2 = opt_insert_before(ctx, op, 0, 2);
@@ -2269,20 +2268,18 @@ static bool fold_multiply2(OptContext *ctx, TCGOp *op)
 
             switch (op->opc) {
             case INDEX_op_mulu2:
-                tcg_opt_gen_movi(ctx, op, rh, 0);
-                break;
+                return tcg_opt_gen_movi(ctx, op, rh, 0);
             case INDEX_op_muls2:
                 op->opc = INDEX_op_sar;
                 op->args[0] = rh;
                 op->args[1] = rl;
                 op->args[2] =
                     arg_new_constant(ctx, tcg_type_size(ctx->type) * 8 - 1);
-                break;
+                return fold_shift(ctx, op);
             default:
-                g_assert_not_reached();
+                break;
             }
-
-            return true;
+            g_assert_not_reached();
         }
     }
     return finish_folding(ctx, op);
Signature · 1 line
2.53.0
#10 lore raw

[PULL 14/16] tcg/riscv64: Set vtype before whole-register vector loads

In reply to Richard Henderson

With vtype uninitialized, VILL might be set, leading to SIGILL. Also, assert vtype initialized for whole-register stores and moves.

Fixes f63e7089b4 ("tcg/riscv: Add basic support for vector")
---
 tcg/riscv64/tcg-target.c.inc | 6 ++++++
 1 file changed, 6 insertions(+)
tcg/riscv64/tcg-target.c.inc +6 -0
index 2ce9d47a633..de9a3570b23 100644
@@ -775,6 +775,7 @@ static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
     case TCG_TYPE_V64:
     case TCG_TYPE_V128:
     case TCG_TYPE_V256:
+        tcg_debug_assert(s->riscv_cur_type != TCG_TYPE_COUNT);
         {
             int lmul = type - riscv_lg2_vlenb;
             int nf = 1 << MAX(lmul, 0);
@@ -1014,6 +1015,10 @@ static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg,
             unsigned idx = type - riscv_lg2_vlenb;
 
             tcg_debug_assert(idx < ARRAY_SIZE(whole_reg_ld));
+            /* We must initialize vtype to something to avoid VILL. */
+            if (s->riscv_cur_type == TCG_TYPE_COUNT) {
+                set_vtype(s, type, MO_8);
+            }
             insn = whole_reg_ld[idx];
         } else {
             static const RISCVInsn unit_stride_ld[] = {
@@ -1046,6 +1051,7 @@ static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
     case TCG_TYPE_V64:
     case TCG_TYPE_V128:
     case TCG_TYPE_V256:
+        tcg_debug_assert(s->riscv_cur_type != TCG_TYPE_COUNT);
         if (type >= riscv_lg2_vlenb) {
             static const RISCVInsn whole_reg_st[] = {
                 OPC_VS1R_V, OPC_VS2R_V, OPC_VS4R_V, OPC_VS8R_V
Signature · 1 line
2.53.0
#11 lore raw

[PULL 12/16] tcg: Sort all_outop[] by INDEX name

In reply to Richard Henderson

Most of the opcodes were already sorted, but then there were a set at the bottom that were left over from the split between 32 and 64-bit hosts.

---
 tcg/tcg.c | 39 +++++++++++++++++++--------------------
 1 file changed, 19 insertions(+), 20 deletions(-)
tcg/tcg.c +19 -20
index 489df0e7386..ecc1f54c1bf 100644
@@ -1158,32 +1158,39 @@ static const TCGOutOpLoad outop_ld = {
 /* Register allocation descriptions for every TCGOpcode. */
 static const TCGOutOp * const all_outop[NB_OPS] = {
     OUTOP(INDEX_op_add, TCGOutOpBinary, outop_add),
+    /* addc1o is implemented with set_carry + addcio */
+    OUTOP(INDEX_op_addc1o, TCGOutOpBinary, outop_addcio),
     OUTOP(INDEX_op_addci, TCGOutOpAddSubCarry, outop_addci),
     OUTOP(INDEX_op_addcio, TCGOutOpBinary, outop_addcio),
     OUTOP(INDEX_op_addco, TCGOutOpBinary, outop_addco),
-    /* addc1o is implemented with set_carry + addcio */
-    OUTOP(INDEX_op_addc1o, TCGOutOpBinary, outop_addcio),
     OUTOP(INDEX_op_and, TCGOutOpBinary, outop_and),
     OUTOP(INDEX_op_andc, TCGOutOpBinary, outop_andc),
     OUTOP(INDEX_op_brcond, TCGOutOpBrcond, outop_brcond),
     OUTOP(INDEX_op_bswap16, TCGOutOpBswap, outop_bswap16),
     OUTOP(INDEX_op_bswap32, TCGOutOpBswap, outop_bswap32),
+    OUTOP(INDEX_op_bswap64, TCGOutOpUnary, outop_bswap64),
     OUTOP(INDEX_op_clz, TCGOutOpBinary, outop_clz),
     OUTOP(INDEX_op_ctpop, TCGOutOpUnary, outop_ctpop),
     OUTOP(INDEX_op_ctz, TCGOutOpBinary, outop_ctz),
     OUTOP(INDEX_op_deposit, TCGOutOpDeposit, outop_deposit),
     OUTOP(INDEX_op_divs, TCGOutOpBinary, outop_divs),
-    OUTOP(INDEX_op_divu, TCGOutOpBinary, outop_divu),
     OUTOP(INDEX_op_divs2, TCGOutOpDivRem, outop_divs2),
+    OUTOP(INDEX_op_divu, TCGOutOpBinary, outop_divu),
     OUTOP(INDEX_op_divu2, TCGOutOpDivRem, outop_divu2),
     OUTOP(INDEX_op_eqv, TCGOutOpBinary, outop_eqv),
     OUTOP(INDEX_op_extract, TCGOutOpExtract, outop_extract),
     OUTOP(INDEX_op_extract2, TCGOutOpExtract2, outop_extract2),
-    OUTOP(INDEX_op_ld8u, TCGOutOpLoad, outop_ld8u),
-    OUTOP(INDEX_op_ld8s, TCGOutOpLoad, outop_ld8s),
-    OUTOP(INDEX_op_ld16u, TCGOutOpLoad, outop_ld16u),
-    OUTOP(INDEX_op_ld16s, TCGOutOpLoad, outop_ld16s),
+    OUTOP(INDEX_op_ext_i32_i64, TCGOutOpUnary, outop_exts_i32_i64),
+    OUTOP(INDEX_op_extrh_i64_i32, TCGOutOpUnary, outop_extrh_i64_i32),
+    OUTOP(INDEX_op_extrl_i64_i32, TCGOutOpUnary, outop_extrl_i64_i32),
+    OUTOP(INDEX_op_extu_i32_i64, TCGOutOpUnary, outop_extu_i32_i64),
     OUTOP(INDEX_op_ld, TCGOutOpLoad, outop_ld),
+    OUTOP(INDEX_op_ld8s, TCGOutOpLoad, outop_ld8s),
+    OUTOP(INDEX_op_ld8u, TCGOutOpLoad, outop_ld8u),
+    OUTOP(INDEX_op_ld16s, TCGOutOpLoad, outop_ld16s),
+    OUTOP(INDEX_op_ld16u, TCGOutOpLoad, outop_ld16u),
+    OUTOP(INDEX_op_ld32s, TCGOutOpLoad, outop_ld32s),
+    OUTOP(INDEX_op_ld32u, TCGOutOpLoad, outop_ld32u),
     OUTOP(INDEX_op_movcond, TCGOutOpMovcond, outop_movcond),
     OUTOP(INDEX_op_mul, TCGOutOpBinary, outop_mul),
     OUTOP(INDEX_op_muls2, TCGOutOpMul2, outop_muls2),
@@ -1203,7 +1210,9 @@ static const TCGOutOp * const all_outop[NB_OPS] = {
     OUTOP(INDEX_op_qemu_st2, TCGOutOpQemuLdSt2, outop_qemu_st2),
     OUTOP(INDEX_op_rems, TCGOutOpBinary, outop_rems),
     OUTOP(INDEX_op_remu, TCGOutOpBinary, outop_remu),
+    OUTOP(INDEX_op_revbit8, TCGOutOpUnary, outop_revbit8),
     OUTOP(INDEX_op_revbit32, TCGOutOpBswap, outop_revbit32),
+    OUTOP(INDEX_op_revbit64, TCGOutOpUnary, outop_revbit64),
     OUTOP(INDEX_op_rotl, TCGOutOpBinary, outop_rotl),
     OUTOP(INDEX_op_rotr, TCGOutOpBinary, outop_rotr),
     OUTOP(INDEX_op_sar, TCGOutOpBinary, outop_sar),
@@ -1216,28 +1225,18 @@ static const TCGOutOp * const all_outop[NB_OPS] = {
     OUTOP(INDEX_op_st, TCGOutOpStore, outop_st),
     OUTOP(INDEX_op_st8, TCGOutOpStore, outop_st8),
     OUTOP(INDEX_op_st16, TCGOutOpStore, outop_st16),
+    OUTOP(INDEX_op_st32, TCGOutOpStore, outop_st),
     OUTOP(INDEX_op_sub, TCGOutOpSubtract, outop_sub),
+    /* subb1o is implemented with set_borrow + subbio */
+    OUTOP(INDEX_op_subb1o, TCGOutOpAddSubCarry, outop_subbio),
     OUTOP(INDEX_op_subbi, TCGOutOpAddSubCarry, outop_subbi),
     OUTOP(INDEX_op_subbio, TCGOutOpAddSubCarry, outop_subbio),
     OUTOP(INDEX_op_subbo, TCGOutOpAddSubCarry, outop_subbo),
-    /* subb1o is implemented with set_borrow + subbio */
-    OUTOP(INDEX_op_subb1o, TCGOutOpAddSubCarry, outop_subbio),
     OUTOP(INDEX_op_umax, TCGOutOpBinary, outop_umax),
     OUTOP(INDEX_op_umin, TCGOutOpBinary, outop_umin),
     OUTOP(INDEX_op_xor, TCGOutOpBinary, outop_xor),
 
     [INDEX_op_goto_ptr] = &outop_goto_ptr,
-
-    OUTOP(INDEX_op_bswap64, TCGOutOpUnary, outop_bswap64),
-    OUTOP(INDEX_op_ext_i32_i64, TCGOutOpUnary, outop_exts_i32_i64),
-    OUTOP(INDEX_op_extu_i32_i64, TCGOutOpUnary, outop_extu_i32_i64),
-    OUTOP(INDEX_op_extrl_i64_i32, TCGOutOpUnary, outop_extrl_i64_i32),
-    OUTOP(INDEX_op_extrh_i64_i32, TCGOutOpUnary, outop_extrh_i64_i32),
-    OUTOP(INDEX_op_ld32u, TCGOutOpLoad, outop_ld32u),
-    OUTOP(INDEX_op_ld32s, TCGOutOpLoad, outop_ld32s),
-    OUTOP(INDEX_op_revbit8, TCGOutOpUnary, outop_revbit8),
-    OUTOP(INDEX_op_revbit64, TCGOutOpUnary, outop_revbit64),
-    OUTOP(INDEX_op_st32, TCGOutOpStore, outop_st),
 };
 
 #undef OUTOP
Signature · 1 line
2.53.0
#12 lore raw

[PULL 10/16] accel/tcg: Allow some address space wraparound during translation

In reply to Richard Henderson

Allow the two pages of translation to be non-contiguous. As long as the target translator wraps pc properly, this allows address space wraparound to function correctly.

This avoids an assert on a valid i386 system-mode test case.

---
 include/exec/translator.h |  3 +++
 accel/tcg/translator.c    | 45 ++++++++++++++++++++++++---------------
 2 files changed, 31 insertions(+), 17 deletions(-)
include/exec/translator.h +3 -0
index 978dee25add..104e6d4f361 100644
@@ -55,6 +55,8 @@ typedef enum DisasJumpType {
  * @pc_first: Address of first guest instruction in this TB.
  * @pc_next: Address of next guest instruction in this TB (current during
  *           disassembly).
+ * @pc_second_page: Address of the beginning of the second page of this TB,
+ *                  or -1 if the TB does not yet extend to a second page.
  * @is_jmp: What instruction to disassemble next.
  * @num_insns: Number of translated instructions (including current).
  * @max_insns: Maximum number of instructions to be translated in this TB.
@@ -69,6 +71,7 @@ struct DisasContextBase {
     TranslationBlock *tb;
     vaddr pc_first;
     vaddr pc_next;
+    vaddr pc_second_page;
     DisasJumpType is_jmp;
     int num_insns;
     int max_insns;
accel/tcg/translator.c +28 -17

66 lines not on this page · raw message

Signature · 1 line
2.53.0
#13 lore raw

[PULL 11/16] tests/tcg/i386/system: Add regression test for translator_ld wraparound

In reply to Richard Henderson

From: Tao Cui <[email protected]>

Add a test case that reaches an instruction straddling the end of the 32-bit address space (0xfffffffe).

The top page (0xfffff000) is SeaBIOS ROM, so the cross-boundary byte is the ROM's own 0x00 (add r/m8, r8) at 0xffffffff, whose modrm is fetched from [0x0]. A short exit stub is placed there. The case runs on qemu-system-i386 since the bug is 32-bit only.

Message-ID: <[email protected]>

[rth: Simplify and convert to meson test harness]
---
 tests/tcg/i386/system/meson.build  |  4 ++++
 tests/tcg/i386/system/wraparound.S | 35 ++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)
 create mode 100644 tests/tcg/i386/system/wraparound.S
tests/tcg/i386/system/meson.build +4 -0
index d3f73997c7f..c08563d0ab5 100644
@@ -34,6 +34,10 @@ foreach t: tcg_tests['multiarch-softmmu']['tests']
   endforeach
 endforeach
 
+tests += {
+  'wraparound.S': { 'cflags': cflags, 'qemu_args': ['-m', '4G'] + qemu_def_args }
+}
+
 if 'qemu-system-i386' in emulators
   tcg_tests += {
     'i386-softmmu': {
tests/tcg/i386/system/wraparound.S new file +35 -0
new file mode 100644
index 00000000000..9c77b3a8468
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Regression test for the translator_ld crash when an instruction
+ * straddles the end of the 32-bit address space (i386).
+ */
+
+        .code32
+        .section .text
+
+main:
+        /*
+         * The top page (0xfffff000) is SeaBIOS ROM and cannot be written.
+         * Its byte at 0xffffffff (0x00 = "add r/m8, r8") already crosses the
+         * page boundary into page1 at 0x0, which is exactly the case
+         * translator_ld must handle without aborting.  Reaching 0xfffffffe
+         * runs the ROM's cld, then that add; the add's modrm is fetched from
+         * [0x0], which is RAM, so build a short exit stub there:
+         *
+         *   [0x0] c0                 modrm -> "add al, al" (reg; EIP -> 1)
+         *   [0x1] c3                 ret
+         *
+         * Note: this relies on the SeaBIOS byte at 0xffffffff being 0x00
+         * (add r/m8, r8); if that ever changes, the stub below must move.
+         *
+         * Note that eax = 0 before and after the stub, so this becomes
+         * the exit code of the test.
+         */
+        xor     %eax, %eax
+        movw    $0xc3c0, (%eax)
+        movl    $0xfffffffe, %ecx
+        jmp     *%ecx
+
+        .globl  main
+        .type   main, @function
+        .size   main, . - main
Signature · 1 line
2.53.0
#14 lore raw

[PULL 07/16] tests/tcg/arm: Add regression test for #4448

In reply to Richard Henderson
---
 tests/tcg/arm/issue4448.c | 51 +++++++++++++++++++++++++++++++++++++++
 tests/tcg/arm/meson.build |  1 +
 2 files changed, 52 insertions(+)
 create mode 100644 tests/tcg/arm/issue4448.c
tests/tcg/arm/issue4448.c new file +51 -0
new file mode 100644
index 00000000000..0a498373a89
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include <assert.h>
+#include <stdint.h>
+
+/* Signed 64-bit product, constant operand first then second. */
+#define SMULL_CONST_LHS(k, x, lo, hi) \
+    __asm__("mov r0, #" #k "\n\tsmull %0, %1, r0, %2" \
+            : "=&r"(lo), "=&r"(hi) : "r"(x) : "r0", "cc")
+
+#define SMULL_CONST_RHS(k, x, lo, hi) \
+    __asm__("mov r0, #" #k "\n\tsmull %0, %1, %2, r0" \
+            : "=&r"(lo), "=&r"(hi) : "r"(x) : "r0", "cc")
+
+/* Unsigned 64-bit product, for contrast: this one is folded correctly. */
+#define UMULL_CONST_LHS(k, x, lo, hi) \
+    __asm__("mov r0, #" #k "\n\tumull %0, %1, r0, %2" \
+            : "=&r"(lo), "=&r"(hi) : "r"(x) : "r0", "cc")
+
+static int64_t s64(int32_t hi, int32_t lo)
+{
+    return ((int64_t)hi << 32) | (uint32_t)lo;
+}
+
+static uint64_t u64(int32_t hi, int32_t lo)
+{
+    return ((uint64_t)(uint32_t)hi << 32) | (uint32_t)lo;
+}
+
+int main(void)
+{
+    static const int32_t values[] = { 1961612, -1479053948, -7, 0 };
+
+    for (unsigned i = 0; i < sizeof(values) / sizeof(values[0]); i++) {
+        int32_t x = values[i];
+        int32_t lo, hi;
+
+        SMULL_CONST_LHS(1, x, lo, hi);
+        assert(x == s64(hi, lo));
+
+        SMULL_CONST_RHS(1, x, lo, hi);
+        assert(x == s64(hi, lo));
+
+        UMULL_CONST_LHS(1, x, lo, hi);
+        assert((uint32_t)x == u64(hi, lo));
+
+        SMULL_CONST_LHS(0, x, lo, hi);
+        assert(0 == u64(hi, lo));
+    }
+    return 0;
+}
tests/tcg/arm/meson.build +1 -0
index 1b491cea026..5c2af2189c5 100644
@@ -39,6 +39,7 @@ tests += {
     'expected_output': 'fcvt.ref',
   },
   'pcalign-a32.c': {'cflags': ['-marm']},
+  'issue4448.c': {},
 }
 
 # Vector SHA1
Signature · 1 line
2.53.0
#15 lore raw

[PULL 13/16] tcg/x86_64: Use the mulx insn from the bmi2 isa

In reply to Richard Henderson

This vex encoded instruction can be used or either mulu2 or muluh. This replaces the highly constrained mul insn from the base x86 isa.

---
 tcg/x86_64/tcg-target-con-set.h |  2 ++
 tcg/x86_64/tcg-target.c.inc     | 36 ++++++++++++++++++++++++++++++---
 2 files changed, 35 insertions(+), 3 deletions(-)
tcg/x86_64/tcg-target-con-set.h +2 -0
index 458d69c3c07..7df9bf49177 100644
@@ -42,6 +42,7 @@ C_O1_I2(r, 0, reZ)
 C_O1_I2(r, 0, ri)
 C_O1_I2(r, 0, rI)
 C_O1_I2(r, L, L)
+C_O1_I2(r, r, d)
 C_O1_I2(r, r, r)
 C_O1_I2(r, r, re)
 C_O1_I2(r, r, ri)
@@ -56,5 +57,6 @@ C_O1_I4(r, r, reT, r, 0)
 C_O1_I4(r, r, r, ri, ri)
 C_O2_I1(r, r, L)
 C_O2_I2(a, d, a, r)
+C_O2_I2(r, r, r, d)
 C_O2_I2(r, r, L, L)
 C_O2_I3(a, d, 0, 1, r)
tcg/x86_64/tcg-target.c.inc +33 -3
index 2c8f1f3e580..590c9c24020 100644
@@ -274,6 +274,7 @@ static bool tcg_target_const_match(int64_t val, int ct,
 #define OPC_MOVSLQ	(0x63 | P_REXW)
 #define OPC_MOVZBL	(0xb6 | P_EXT)
 #define OPC_MOVZWL	(0xb7 | P_EXT)
+#define OPC_MULX        (0xf6 | P_SIMDF2 | P_EXT38)
 #define OPC_PABSB       (0x1c | P_EXT38 | P_DATA16)
 #define OPC_PABSW       (0x1d | P_EXT38 | P_DATA16)
 #define OPC_PABSD       (0x1e | P_EXT38 | P_DATA16)
@@ -2732,19 +2733,48 @@ static const TCGOutOpBinary outop_mulsh = {
     .base.static_constraint = C_NotImplemented,
 };
 
+static void tgen_muluh(TCGContext *s, TCGType type,
+                       TCGReg a0, TCGReg a1, TCGReg a2)
+{
+    /*
+     * Note from the architecture manual:
+     * If the first and second operand are identical,
+     * it will contain the high half of the multiplication result.
+     */
+    int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW;
+    tcg_out_vex_modrm(s, OPC_MULX + rexw, a0, a0, a1);
+}
+
+static TCGConstraintSetIndex cset_muluh(TCGType type, unsigned flags)
+{
+    return have_bmi2 ? C_O1_I2(r, r, d) : C_NotImplemented;
+}
+
 static const TCGOutOpBinary outop_muluh = {
-    .base.static_constraint = C_NotImplemented,
+    .base.static_constraint = C_Dynamic,
+    .base.dynamic_constraint = cset_muluh,
+    .out_rrr = tgen_muluh,
 };
 
 static void tgen_mulu2(TCGContext *s, TCGType type,
                        TCGReg a0, TCGReg a1, TCGReg a2, TCGReg a3)
 {
     int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW;
-    tcg_out_modrm(s, OPC_GRP3_Ev + rexw, EXT3_MUL, a3);
+    if (have_bmi2) {
+        tcg_out_vex_modrm(s, OPC_MULX + rexw, a1, a0, a2);
+    } else {
+        tcg_out_modrm(s, OPC_GRP3_Ev + rexw, EXT3_MUL, a3);
+    }
+}
+
+static TCGConstraintSetIndex cset_mulu2(TCGType type, unsigned flags)
+{
+    return have_bmi2 ? C_O2_I2(r, r, r, d) : C_O2_I2(a, d, a, r);
 }
 
 static const TCGOutOpMul2 outop_mulu2 = {
-    .base.static_constraint = C_O2_I2(a, d, a, r),
+    .base.static_constraint = C_Dynamic,
+    .base.dynamic_constraint = cset_mulu2,
     .out_rrrr = tgen_mulu2,
 };
 
Signature · 1 line
2.53.0
#16 lore raw

[PULL 15/16] accel: Change AccelOpsClass.update_guest_debug to void

In reply to Richard Henderson

The return value is never used, so drop it.

---
 include/accel/accel-cpu-ops.h | 2 +-
 include/system/hvf_int.h      | 2 +-
 accel/hvf/hvf-accel-ops.c     | 3 +--
 accel/kvm/kvm-accel-ops.c     | 4 ++--
 4 files changed, 5 insertions(+), 6 deletions(-)
include/accel/accel-cpu-ops.h +1 -1
index f0c7ee7542c..af71fd9ab34 100644
@@ -85,7 +85,7 @@ struct AccelOpsClass {
     int64_t (*get_elapsed_ticks)(void);
 
     /* gdbstub hooks */
-    int (*update_guest_debug)(CPUState *cpu);
+    void (*update_guest_debug)(CPUState *cpu);
     int (*insert_gdbstub_breakpoint)(CPUState *cpu, GdbBreakpointType type,
                                      vaddr addr, vaddr len);
     int (*remove_gdbstub_breakpoint)(CPUState *cpu, GdbBreakpointType type,
include/system/hvf_int.h +1 -1
index a01691ce172..1b419b5428a 100644
@@ -105,7 +105,7 @@ void hvf_arch_remove_all_gdbstub_hw_breakpoints(void);
  * Update guest to enable or disable debugging. Per-arch specifics will be
  * handled by calling down to hvf_arch_update_guest_debug.
  */
-int hvf_update_guest_debug(CPUState *cpu);
+void hvf_update_guest_debug(CPUState *cpu);
 
 bool hvf_arch_cpu_realize(CPUState *cpu, Error **errp);
 uint32_t hvf_arch_get_default_ipa_bit_size(void);
accel/hvf/hvf-accel-ops.c +1 -2
index d2276d8513e..6491a109ba4 100644
@@ -227,10 +227,9 @@ static void do_hvf_update_guest_debug(CPUState *cpu, run_on_cpu_data arg)
     hvf_arch_update_guest_debug(cpu);
 }
 
-int hvf_update_guest_debug(CPUState *cpu)
+void hvf_update_guest_debug(CPUState *cpu)
 {
     run_on_cpu(cpu, do_hvf_update_guest_debug, RUN_ON_CPU_NULL);
-    return 0;
 }
 
 static int hvf_insert_gdbstub_breakpoint(CPUState *cpu, GdbBreakpointType type,
accel/kvm/kvm-accel-ops.c +2 -2
index c8e7aa38709..47244d33f7d 100644
@@ -86,9 +86,9 @@ static bool kvm_cpus_are_resettable(void)
 }
 
 #ifdef TARGET_KVM_HAVE_GUEST_DEBUG
-static int kvm_update_guest_debug_ops(CPUState *cpu)
+static void kvm_update_guest_debug_ops(CPUState *cpu)
 {
-    return kvm_update_guest_debug(cpu, 0);
+    kvm_update_guest_debug(cpu, 0);
 }
 #endif
 
Signature · 1 line
2.53.0
author Applied
#17 lore raw
In reply to Richard Henderson
Quoted 12 lines · Richard Henderson

The following changes since commit 014b1f730c0faa08067f8e9b5829e62ea3d4b177:

   Merge tag 'pull-9p-20260923' ofhttps://github.com/cschoenebeck/qemu into staging (2026-09-23 07:36:52 -0700)

are available in the Git repository at:

   https://gitlab.com/rth7680/qemu.git tags/pull-tcg-20260923-v2

for you to fetch changes up to 666879d5477ec41ab46fc10a27d18cbc13e8789e:

   accel/tcg: fold the dynamic cflags into CPUState::tcg_cflags (2026-09-23 13:28:00 -0700)
Quoted 12 lines

----------------------------------------------------------------
accel: Change AccelOpsClass.update_guest_debug to void accel/tcg: Fix TLB_MMIO check in tlb_plugin_lookup() accel/tcg: Skip the can_do_io stores in user-only builds accel/tcg: Allow cross-page goto_tb chaining in user-only builds accel/tcg: fold the dynamic cflags into CPUState::tcg_cflags accel/tcg: Use TLB_FORCE_SLOW not TLB_MMIO for system plugins accel/tcg: Allow some address space wraparound during translation tcg/optimize: Fix fold_multiply2 vs 1
tcg/optimize: Fix expansion/simplification of deposit tcg/x86_64: Use the mulx insn from the bmi2 isa tcg/riscv64: Set vtype before whole-register vector loads

Applied, thanks.

r~