qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PULL 0/9] Accelerators & CPU patches for Halloween 2025
@ 2025-10-31 21:15 Philippe Mathieu-Daudé
  2025-10-31 21:15 ` [PULL 1/9] cpus: Access CPUState::thread_kicked atomically Philippe Mathieu-Daudé
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-10-31 21:15 UTC (permalink / raw)
  To: qemu-devel

The following changes since commit c494afbb7d552604ad26036127655c534a2645e5:

  Merge tag 'pull-nvme-20251030' of https://gitlab.com/birkelund/qemu into staging (2025-10-31 12:56:05 +0100)

are available in the Git repository at:

  https://github.com/philmd/qemu.git tags/accel-cpus-20251031

for you to fetch changes up to 0da6099730dbd44a6a96ad18774e5553d1ca7d21:

  rx: cpu: fix interrupts check in rx_cpu_do_interrupt() (2025-10-31 22:10:50 +0100)

Following checkpatch.pl error ignored:

  8/9 Checking commit 46f767bd6814 (tests/unit: add unit test for qemu_hexdump())
  ERROR: unnecessary whitespace before a quoted newline
  #59: FILE: tests/unit/test-cutils.c:3662:
  +            "s is \n";

  total: 1 errors, 0 warnings, 56 lines checked

----------------------------------------------------------------
Generic CPUs / accelerators patch queue

- Access CPUState::thread_kicked atomically
- Make async_safe_run_on_cpu() safe on HVF
- Fix bql_locked status with condvar APIs
- Document cpu_memory_rw_debug()
- Rename init_clocks() -> qemu_init_clocks() to avoid name clashing
- Fix QEMU_HEXDUMP_LINE_WIDTH logic
- Fix interrupts check in rx_cpu_do_interrupt()
----------------------------------------------------------------

Alex Bennée (1):
  timers: properly prefix init_clocks()

Igor Mammedov (1):
  rx: cpu: fix interrupts check in rx_cpu_do_interrupt()

Peter Xu (1):
  bql: Fix bql_locked status with condvar APIs

Philippe Mathieu-Daudé (4):
  cpus: Access CPUState::thread_kicked atomically
  accel/hvf: Make async_safe_run_on_cpu() safe
  accel/tcg: Use cpu_is_stopped() helper to access CPUState::stopped
  exec/cpu: Declare cpu_memory_rw_debug() in 'hw/core/cpu.h' and
    document

Vladimir Sementsov-Ogievskiy (2):
  util/hexdump: fix QEMU_HEXDUMP_LINE_WIDTH logic
  tests/unit: add unit test for qemu_hexdump()

 include/exec/cpu-common.h         |  4 ---
 include/hw/core/cpu.h             | 20 ++++++++++++++
 include/qemu/main-loop.h          | 18 +++++++++++++
 include/qemu/timer.h              |  5 ++--
 util/qemu-thread-common.h         |  7 +++++
 accel/tcg/tcg-accel-ops-rr.c      |  2 +-
 stubs/iothread-lock.c             |  9 +++++++
 system/cpus.c                     | 18 ++++++++++---
 target/arm/hvf/hvf.c              |  2 ++
 target/i386/hvf/hvf.c             |  2 ++
 target/rx/helper.c                | 45 ++++++++++++++-----------------
 tests/unit/test-aio-multithread.c |  2 +-
 tests/unit/test-cutils.c          | 45 +++++++++++++++++++++++++++++++
 util/hexdump.c                    | 38 ++++++++++++++++----------
 util/main-loop.c                  |  2 +-
 util/qemu-timer.c                 |  2 +-
 16 files changed, 168 insertions(+), 53 deletions(-)

-- 
2.51.0



^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PULL 1/9] cpus: Access CPUState::thread_kicked atomically
  2025-10-31 21:15 [PULL 0/9] Accelerators & CPU patches for Halloween 2025 Philippe Mathieu-Daudé
@ 2025-10-31 21:15 ` Philippe Mathieu-Daudé
  2025-10-31 21:15 ` [PULL 2/9] accel/hvf: Make async_safe_run_on_cpu() safe Philippe Mathieu-Daudé
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-10-31 21:15 UTC (permalink / raw)
  To: qemu-devel

cpus_kick_thread() is called via cpu_exit() -> qemu_cpu_kick(),
and also via gdb_syscall_handling(). Access the CPUState field
using atomic accesses. See commit 8ac2ca02744 ("accel: use atomic
accesses for exit_request") for rationale.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Message-Id: <20250925025520.71805-3-philmd@linaro.org>
---
 system/cpus.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/system/cpus.c b/system/cpus.c
index aa7bfcf56e5..74f5a5bd4e2 100644
--- a/system/cpus.c
+++ b/system/cpus.c
@@ -480,10 +480,10 @@ void qemu_process_cpu_events(CPUState *cpu)
 
 void cpus_kick_thread(CPUState *cpu)
 {
-    if (cpu->thread_kicked) {
+    if (qatomic_read(&cpu->thread_kicked)) {
         return;
     }
-    cpu->thread_kicked = true;
+    qatomic_set(&cpu->thread_kicked, true);
 
 #ifndef _WIN32
     int err = pthread_kill(cpu->thread->thread, SIG_IPI);
-- 
2.51.0



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PULL 2/9] accel/hvf: Make async_safe_run_on_cpu() safe
  2025-10-31 21:15 [PULL 0/9] Accelerators & CPU patches for Halloween 2025 Philippe Mathieu-Daudé
  2025-10-31 21:15 ` [PULL 1/9] cpus: Access CPUState::thread_kicked atomically Philippe Mathieu-Daudé
@ 2025-10-31 21:15 ` Philippe Mathieu-Daudé
  2025-10-31 21:15 ` [PULL 3/9] accel/tcg: Use cpu_is_stopped() helper to access CPUState::stopped Philippe Mathieu-Daudé
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-10-31 21:15 UTC (permalink / raw)
  To: qemu-devel

Wrap hv_vcpu_run() calls with cpu_exec_start/end() in order to
have the main loop perform more exclusive sections while all
vCPUs are quiescent.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <20250925025520.71805-4-philmd@linaro.org>
---
 target/arm/hvf/hvf.c  | 2 ++
 target/i386/hvf/hvf.c | 2 ++
 2 files changed, 4 insertions(+)

diff --git a/target/arm/hvf/hvf.c b/target/arm/hvf/hvf.c
index 0658a99a2d1..1e50632557e 100644
--- a/target/arm/hvf/hvf.c
+++ b/target/arm/hvf/hvf.c
@@ -1803,7 +1803,9 @@ int hvf_vcpu_exec(CPUState *cpu)
     flush_cpu_state(cpu);
 
     bql_unlock();
+    cpu_exec_start(cpu);
     r = hv_vcpu_run(cpu->accel->fd);
+    cpu_exec_end(cpu);
     bql_lock();
     switch (r) {
     case HV_SUCCESS:
diff --git a/target/i386/hvf/hvf.c b/target/i386/hvf/hvf.c
index 33f723a76a7..8cd1a800e0a 100644
--- a/target/i386/hvf/hvf.c
+++ b/target/i386/hvf/hvf.c
@@ -749,8 +749,10 @@ int hvf_vcpu_exec(CPUState *cpu)
             return EXCP_HLT;
         }
 
+        cpu_exec_start(cpu);
         hv_return_t r = hv_vcpu_run_until(cpu->accel->fd, HV_DEADLINE_FOREVER);
         assert_hvf_ok(r);
+        cpu_exec_end(cpu);
 
         /* handle VMEXIT */
         uint64_t exit_reason = rvmcs(cpu->accel->fd, VMCS_EXIT_REASON);
-- 
2.51.0



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PULL 3/9] accel/tcg: Use cpu_is_stopped() helper to access CPUState::stopped
  2025-10-31 21:15 [PULL 0/9] Accelerators & CPU patches for Halloween 2025 Philippe Mathieu-Daudé
  2025-10-31 21:15 ` [PULL 1/9] cpus: Access CPUState::thread_kicked atomically Philippe Mathieu-Daudé
  2025-10-31 21:15 ` [PULL 2/9] accel/hvf: Make async_safe_run_on_cpu() safe Philippe Mathieu-Daudé
@ 2025-10-31 21:15 ` Philippe Mathieu-Daudé
  2025-10-31 21:15 ` [PULL 4/9] bql: Fix bql_locked status with condvar APIs Philippe Mathieu-Daudé
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-10-31 21:15 UTC (permalink / raw)
  To: qemu-devel

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <20250925025520.71805-5-philmd@linaro.org>
---
 accel/tcg/tcg-accel-ops-rr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/accel/tcg/tcg-accel-ops-rr.c b/accel/tcg/tcg-accel-ops-rr.c
index 2fb46439971..f84342e0449 100644
--- a/accel/tcg/tcg-accel-ops-rr.c
+++ b/accel/tcg/tcg-accel-ops-rr.c
@@ -197,7 +197,7 @@ static void *rr_cpu_thread_fn(void *arg)
     qemu_guest_random_seed_thread_part2(cpu->random_seed);
 
     /* wait for initial kick-off after machine start */
-    while (first_cpu->stopped) {
+    while (cpu_is_stopped(first_cpu)) {
         qemu_cond_wait_bql(first_cpu->halt_cond);
 
         /* process any pending work */
-- 
2.51.0



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PULL 4/9] bql: Fix bql_locked status with condvar APIs
  2025-10-31 21:15 [PULL 0/9] Accelerators & CPU patches for Halloween 2025 Philippe Mathieu-Daudé
                   ` (2 preceding siblings ...)
  2025-10-31 21:15 ` [PULL 3/9] accel/tcg: Use cpu_is_stopped() helper to access CPUState::stopped Philippe Mathieu-Daudé
@ 2025-10-31 21:15 ` Philippe Mathieu-Daudé
  2025-10-31 21:15 ` [PULL 5/9] exec/cpu: Declare cpu_memory_rw_debug() in 'hw/core/cpu.h' and document Philippe Mathieu-Daudé
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-10-31 21:15 UTC (permalink / raw)
  To: qemu-devel

From: Peter Xu <peterx@redhat.com>

QEMU has a per-thread "bql_locked" variable stored in TLS section, showing
whether the current thread is holding the BQL lock.

It's a pretty handy variable.  Function-wise, QEMU have codes trying to
conditionally take bql, relying on the var reflecting the locking status
(e.g. BQL_LOCK_GUARD), or in a GDB debugging session, we could also look at
the variable (in reality, co_tls_bql_locked), to see which thread is
currently holding the bql.

When using that as a debugging facility, sometimes we can observe multiple
threads holding bql at the same time. It's because QEMU's condvar APIs
bypassed the bql_*() API, hence they do not update bql_locked even if they
have released the mutex while waiting.

It can cause confusion if one does "thread apply all p co_tls_bql_locked"
and see multiple threads reporting true.

Fix this by moving the bql status updates into the mutex debug hooks.  Now
the variable should always reflect the reality.

Signed-off-by: Peter Xu <peterx@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Message-ID: <20250904223158.1276992-1-peterx@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 include/qemu/main-loop.h  | 18 ++++++++++++++++++
 util/qemu-thread-common.h |  7 +++++++
 stubs/iothread-lock.c     |  9 +++++++++
 system/cpus.c             | 14 ++++++++++++--
 4 files changed, 46 insertions(+), 2 deletions(-)

diff --git a/include/qemu/main-loop.h b/include/qemu/main-loop.h
index 4e2436b1968..0d55c636b21 100644
--- a/include/qemu/main-loop.h
+++ b/include/qemu/main-loop.h
@@ -270,6 +270,24 @@ void rust_bql_mock_lock(void);
  */
 bool bql_locked(void);
 
+/**
+ * mutex_is_bql:
+ *
+ * @mutex: the mutex pointer
+ *
+ * Returns whether the mutex is the BQL.
+ */
+bool mutex_is_bql(QemuMutex *mutex);
+
+/**
+ * bql_update_status:
+ *
+ * @locked: update status on whether the BQL is locked
+ *
+ * NOTE: this should normally only be invoked when the status changed.
+ */
+void bql_update_status(bool locked);
+
 /**
  * bql_block: Allow/deny releasing the BQL
  *
diff --git a/util/qemu-thread-common.h b/util/qemu-thread-common.h
index 2af6b120853..09331843ba9 100644
--- a/util/qemu-thread-common.h
+++ b/util/qemu-thread-common.h
@@ -14,6 +14,7 @@
 #define QEMU_THREAD_COMMON_H
 
 #include "qemu/thread.h"
+#include "qemu/main-loop.h"
 #include "trace.h"
 
 static inline void qemu_mutex_post_init(QemuMutex *mutex)
@@ -39,6 +40,9 @@ static inline void qemu_mutex_post_lock(QemuMutex *mutex,
     mutex->line = line;
 #endif
     trace_qemu_mutex_locked(mutex, file, line);
+    if (mutex_is_bql(mutex)) {
+        bql_update_status(true);
+    }
 }
 
 static inline void qemu_mutex_pre_unlock(QemuMutex *mutex,
@@ -49,6 +53,9 @@ static inline void qemu_mutex_pre_unlock(QemuMutex *mutex,
     mutex->line = 0;
 #endif
     trace_qemu_mutex_unlock(mutex, file, line);
+    if (mutex_is_bql(mutex)) {
+        bql_update_status(false);
+    }
 }
 
 #endif
diff --git a/stubs/iothread-lock.c b/stubs/iothread-lock.c
index 6050c081f53..c89c9c7228f 100644
--- a/stubs/iothread-lock.c
+++ b/stubs/iothread-lock.c
@@ -34,3 +34,12 @@ void bql_block_unlock(bool increase)
     assert((new_value > bql_unlock_blocked) == increase);
     bql_unlock_blocked = new_value;
 }
+
+bool mutex_is_bql(QemuMutex *mutex)
+{
+    return false;
+}
+
+void bql_update_status(bool locked)
+{
+}
diff --git a/system/cpus.c b/system/cpus.c
index 74f5a5bd4e2..ef2d2f241fa 100644
--- a/system/cpus.c
+++ b/system/cpus.c
@@ -524,6 +524,18 @@ bool qemu_in_vcpu_thread(void)
 
 QEMU_DEFINE_STATIC_CO_TLS(bool, bql_locked)
 
+bool mutex_is_bql(QemuMutex *mutex)
+{
+    return mutex == &bql;
+}
+
+void bql_update_status(bool locked)
+{
+    /* This function should only be used when an update happened.. */
+    assert(bql_locked() != locked);
+    set_bql_locked(locked);
+}
+
 static uint32_t bql_unlock_blocked;
 
 void bql_block_unlock(bool increase)
@@ -564,14 +576,12 @@ void bql_lock_impl(const char *file, int line)
 
     g_assert(!bql_locked());
     bql_lock_fn(&bql, file, line);
-    set_bql_locked(true);
 }
 
 void bql_unlock(void)
 {
     g_assert(bql_locked());
     g_assert(!bql_unlock_blocked);
-    set_bql_locked(false);
     qemu_mutex_unlock(&bql);
 }
 
-- 
2.51.0



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PULL 5/9] exec/cpu: Declare cpu_memory_rw_debug() in 'hw/core/cpu.h' and document
  2025-10-31 21:15 [PULL 0/9] Accelerators & CPU patches for Halloween 2025 Philippe Mathieu-Daudé
                   ` (3 preceding siblings ...)
  2025-10-31 21:15 ` [PULL 4/9] bql: Fix bql_locked status with condvar APIs Philippe Mathieu-Daudé
@ 2025-10-31 21:15 ` Philippe Mathieu-Daudé
  2025-10-31 21:15 ` [PULL 6/9] timers: properly prefix init_clocks() Philippe Mathieu-Daudé
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-10-31 21:15 UTC (permalink / raw)
  To: qemu-devel

cpu_memory_rw_debug() dispatches to CPUClass::memory_rw_debug(),
move its declaration closer to the CPU API. Document.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Message-Id: <20251001150529.14122-22-philmd@linaro.org>
---
 include/exec/cpu-common.h |  4 ----
 include/hw/core/cpu.h     | 20 ++++++++++++++++++++
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/include/exec/cpu-common.h b/include/exec/cpu-common.h
index 67e15c8e507..e0be4ee2b8f 100644
--- a/include/exec/cpu-common.h
+++ b/include/exec/cpu-common.h
@@ -150,10 +150,6 @@ typedef int (RAMBlockIterFunc)(RAMBlock *rb, void *opaque);
 
 int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque);
 
-/* Returns: 0 on success, -1 on error */
-int cpu_memory_rw_debug(CPUState *cpu, vaddr addr,
-                        void *ptr, size_t len, bool is_write);
-
 /* vl.c */
 void list_cpus(void);
 
diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
index e79e8e0a8ee..9615051774d 100644
--- a/include/hw/core/cpu.h
+++ b/include/hw/core/cpu.h
@@ -688,6 +688,26 @@ int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
                              void *opaque);
 
+/**
+ * cpu_memory_rw_debug:
+ * @cpu: The CPU whose memory is to be accessed
+ * @addr: guest virtual address
+ * @ptr: buffer with the data transferred
+ * @len: the number of bytes to read or write
+ * @is_write: indicates the transfer direction
+ *
+ * Take a virtual address, convert it to a physical address via
+ * an MMU lookup using the current settings of the specified CPU,
+ * and then perform the access (using address_space_rw() for
+ * reads or address_space_write_rom() for writes).
+ *
+ * This function is intended for use by the GDB stub and similar code.
+ *
+ * Returns: 0 on success, -1 on error
+ */
+int cpu_memory_rw_debug(CPUState *cpu, vaddr addr,
+                        void *ptr, size_t len, bool is_write);
+
 /**
  * cpu_get_crash_info:
  * @cpu: The CPU to get crash information for
-- 
2.51.0



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PULL 6/9] timers: properly prefix init_clocks()
  2025-10-31 21:15 [PULL 0/9] Accelerators & CPU patches for Halloween 2025 Philippe Mathieu-Daudé
                   ` (4 preceding siblings ...)
  2025-10-31 21:15 ` [PULL 5/9] exec/cpu: Declare cpu_memory_rw_debug() in 'hw/core/cpu.h' and document Philippe Mathieu-Daudé
@ 2025-10-31 21:15 ` Philippe Mathieu-Daudé
  2025-10-31 21:15 ` [PULL 7/9] util/hexdump: fix QEMU_HEXDUMP_LINE_WIDTH logic Philippe Mathieu-Daudé
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-10-31 21:15 UTC (permalink / raw)
  To: qemu-devel

From: Alex Bennée <alex.bennee@linaro.org>

Otherwise we run the risk of name clashing, for example with
stm32l4x5_usart-test.c should we shuffle the includes.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Message-ID: <20251030173302.1379174-1-alex.bennee@linaro.org>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 include/qemu/timer.h              | 5 +++--
 tests/unit/test-aio-multithread.c | 2 +-
 util/main-loop.c                  | 2 +-
 util/qemu-timer.c                 | 2 +-
 4 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/include/qemu/timer.h b/include/qemu/timer.h
index 406d7411203..8b561cd6960 100644
--- a/include/qemu/timer.h
+++ b/include/qemu/timer.h
@@ -786,11 +786,12 @@ static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2)
 }
 
 /**
- * initclocks:
+ * qemu_init_clocks:
+ * @notify_cb: optional call-back for timer expiry
  *
  * Initialise the clock & timer infrastructure
  */
-void init_clocks(QEMUTimerListNotifyCB *notify_cb);
+void qemu_init_clocks(QEMUTimerListNotifyCB *notify_cb);
 
 static inline int64_t get_max_clock_jump(void)
 {
diff --git a/tests/unit/test-aio-multithread.c b/tests/unit/test-aio-multithread.c
index 0ead6bf34ad..c24200a7121 100644
--- a/tests/unit/test-aio-multithread.c
+++ b/tests/unit/test-aio-multithread.c
@@ -443,7 +443,7 @@ static void test_multi_mutex_10(void)
 
 int main(int argc, char **argv)
 {
-    init_clocks(NULL);
+    qemu_init_clocks(NULL);
 
     g_test_init(&argc, &argv, NULL);
     g_test_add_func("/aio/multi/lifecycle", test_lifecycle);
diff --git a/util/main-loop.c b/util/main-loop.c
index b8ddda8f5ee..b462598f76e 100644
--- a/util/main-loop.c
+++ b/util/main-loop.c
@@ -162,7 +162,7 @@ int qemu_init_main_loop(Error **errp)
     int ret;
     GSource *src;
 
-    init_clocks(qemu_timer_notify_cb);
+    qemu_init_clocks(qemu_timer_notify_cb);
 
     ret = qemu_signal_init(errp);
     if (ret) {
diff --git a/util/qemu-timer.c b/util/qemu-timer.c
index 56f11b6a641..2a6be4c7f95 100644
--- a/util/qemu-timer.c
+++ b/util/qemu-timer.c
@@ -637,7 +637,7 @@ static void qemu_virtual_clock_set_ns(int64_t time)
     return cpus_set_virtual_clock(time);
 }
 
-void init_clocks(QEMUTimerListNotifyCB *notify_cb)
+void qemu_init_clocks(QEMUTimerListNotifyCB *notify_cb)
 {
     QEMUClockType type;
     for (type = 0; type < QEMU_CLOCK_MAX; type++) {
-- 
2.51.0



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PULL 7/9] util/hexdump: fix QEMU_HEXDUMP_LINE_WIDTH logic
  2025-10-31 21:15 [PULL 0/9] Accelerators & CPU patches for Halloween 2025 Philippe Mathieu-Daudé
                   ` (5 preceding siblings ...)
  2025-10-31 21:15 ` [PULL 6/9] timers: properly prefix init_clocks() Philippe Mathieu-Daudé
@ 2025-10-31 21:15 ` Philippe Mathieu-Daudé
  2025-10-31 21:15 ` [PULL 8/9] tests/unit: add unit test for qemu_hexdump() Philippe Mathieu-Daudé
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-10-31 21:15 UTC (permalink / raw)
  To: qemu-devel

From: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>

QEMU_HEXDUMP_LINE_WIDTH calculation doesn't correspond to
qemu_hexdump_line(). This leads to last line of the dump (when
length is not multiply of 16) has badly aligned ASCII part.

Let's calculate length the same way.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Tested-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Message-ID: <20251031190246.257153-2-vsementsov@yandex-team.ru>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 util/hexdump.c | 38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)

diff --git a/util/hexdump.c b/util/hexdump.c
index f29ffceb746..7cfc5472613 100644
--- a/util/hexdump.c
+++ b/util/hexdump.c
@@ -22,6 +22,19 @@ static inline char hexdump_nibble(unsigned x)
     return (x < 10 ? '0' : 'a' - 10) + x;
 }
 
+static size_t hexdump_line_length(size_t buf_len, size_t unit_len,
+                                  size_t block_len)
+{
+    size_t est = buf_len * 2;
+    if (unit_len) {
+        est += buf_len / unit_len;
+    }
+    if (block_len) {
+        est += buf_len / block_len;
+    }
+    return est;
+}
+
 GString *qemu_hexdump_line(GString *str, const void *vbuf, size_t len,
                            size_t unit_len, size_t block_len)
 {
@@ -30,14 +43,8 @@ GString *qemu_hexdump_line(GString *str, const void *vbuf, size_t len,
 
     if (str == NULL) {
         /* Estimate the length of the output to avoid reallocs. */
-        size_t est = len * 2;
-        if (unit_len) {
-            est += len / unit_len;
-        }
-        if (block_len) {
-            est += len / block_len;
-        }
-        str = g_string_sized_new(est + 1);
+        str = g_string_sized_new(hexdump_line_length(len, unit_len, block_len)
+                                 + 1);
     }
 
     for (u = 0, b = 0; len; u++, b++, len--, buf++) {
@@ -76,13 +83,16 @@ static void asciidump_line(char *line, const void *bufptr, size_t len)
 }
 
 #define QEMU_HEXDUMP_LINE_BYTES 16
-#define QEMU_HEXDUMP_LINE_WIDTH \
-    (QEMU_HEXDUMP_LINE_BYTES * 2 + QEMU_HEXDUMP_LINE_BYTES / 4)
+#define QEMU_HEXDUMP_UNIT 1
+#define QEMU_HEXDUMP_BLOCK 4
 
 void qemu_hexdump(FILE *fp, const char *prefix,
                   const void *bufptr, size_t size)
 {
-    g_autoptr(GString) str = g_string_sized_new(QEMU_HEXDUMP_LINE_WIDTH + 1);
+    int width = hexdump_line_length(QEMU_HEXDUMP_LINE_BYTES,
+                                    QEMU_HEXDUMP_UNIT,
+                                    QEMU_HEXDUMP_BLOCK);
+    g_autoptr(GString) str = g_string_sized_new(width + 1);
     char ascii[QEMU_HEXDUMP_LINE_BYTES + 1];
     size_t b, len;
 
@@ -90,11 +100,11 @@ void qemu_hexdump(FILE *fp, const char *prefix,
         len = MIN(size - b, QEMU_HEXDUMP_LINE_BYTES);
 
         g_string_truncate(str, 0);
-        qemu_hexdump_line(str, bufptr + b, len, 1, 4);
+        qemu_hexdump_line(str, bufptr + b, len,
+                          QEMU_HEXDUMP_UNIT, QEMU_HEXDUMP_BLOCK);
         asciidump_line(ascii, bufptr + b, len);
 
-        fprintf(fp, "%s: %04zx: %-*s %s\n",
-                prefix, b, QEMU_HEXDUMP_LINE_WIDTH, str->str, ascii);
+        fprintf(fp, "%s: %04zx: %-*s %s\n", prefix, b, width, str->str, ascii);
     }
 
 }
-- 
2.51.0



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PULL 8/9] tests/unit: add unit test for qemu_hexdump()
  2025-10-31 21:15 [PULL 0/9] Accelerators & CPU patches for Halloween 2025 Philippe Mathieu-Daudé
                   ` (6 preceding siblings ...)
  2025-10-31 21:15 ` [PULL 7/9] util/hexdump: fix QEMU_HEXDUMP_LINE_WIDTH logic Philippe Mathieu-Daudé
@ 2025-10-31 21:15 ` Philippe Mathieu-Daudé
  2025-10-31 21:15 ` [PULL 9/9] rx: cpu: fix interrupts check in rx_cpu_do_interrupt() Philippe Mathieu-Daudé
  2025-11-01 11:13 ` [PULL 0/9] Accelerators & CPU patches for Halloween 2025 Richard Henderson
  9 siblings, 0 replies; 11+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-10-31 21:15 UTC (permalink / raw)
  To: qemu-devel

From: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>

Test, that fix in previous commit make sense.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Tested-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Message-ID: <20251031190246.257153-3-vsementsov@yandex-team.ru>
[PMD: Wrap long lines]
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 tests/unit/test-cutils.c | 45 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/tests/unit/test-cutils.c b/tests/unit/test-cutils.c
index 227acc59955..67b1cded64a 100644
--- a/tests/unit/test-cutils.c
+++ b/tests/unit/test-cutils.c
@@ -3626,6 +3626,46 @@ static void test_si_prefix(void)
     g_assert_cmpstr(si_prefix(18), ==, "E");
 }
 
+static void test_qemu_hexdump_alignment(void)
+{
+    /*
+     * Test that ASCII part is properly aligned for incomplete lines.
+     * This test catches the bug that was fixed in previous commit
+     * "util/hexdump: fix QEMU_HEXDUMP_LINE_WIDTH logic".
+     *
+     * We use data that is not aligned to 16 bytes, so last line
+     * is incomplete.
+     */
+    const uint8_t data[] = {
+        /* First line: 16 bytes */
+        0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f,  /* "Hello Wo" */
+        0x72, 0x6c, 0x64, 0x21, 0x20, 0x54, 0x68, 0x69,  /* "rld! Thi" */
+        /* Second line: 5 bytes (incomplete) */
+        0x73, 0x20, 0x69, 0x73, 0x20                     /* "s is " */
+    };
+    char *output = NULL;
+    size_t size;
+    FILE *stream = open_memstream(&output, &size);
+
+    g_assert_nonnull(stream);
+
+    qemu_hexdump(stream, "test", data, sizeof(data));
+    fclose(stream);
+
+    g_assert_nonnull(output);
+
+    /* We expect proper alignment of "s is" part on the second line */
+    const char *expected =
+        "test: 0000: 48 65 6c 6c  6f 20 57 6f  72 6c 64 21  20 54 68 69   "
+            "Hello World! Thi\n"
+        "test: 0010: 73 20 69 73  20                                      "
+            "s is \n";
+
+    g_assert_cmpstr(output, ==, expected);
+
+    free(output);
+}
+
 int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
@@ -3995,5 +4035,10 @@ int main(int argc, char **argv)
                     test_iec_binary_prefix);
     g_test_add_func("/cutils/si_prefix",
                     test_si_prefix);
+
+    /* qemu_hexdump() test */
+    g_test_add_func("/cutils/qemu_hexdump/alignment",
+                    test_qemu_hexdump_alignment);
+
     return g_test_run();
 }
-- 
2.51.0



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PULL 9/9] rx: cpu: fix interrupts check in rx_cpu_do_interrupt()
  2025-10-31 21:15 [PULL 0/9] Accelerators & CPU patches for Halloween 2025 Philippe Mathieu-Daudé
                   ` (7 preceding siblings ...)
  2025-10-31 21:15 ` [PULL 8/9] tests/unit: add unit test for qemu_hexdump() Philippe Mathieu-Daudé
@ 2025-10-31 21:15 ` Philippe Mathieu-Daudé
  2025-11-01 11:13 ` [PULL 0/9] Accelerators & CPU patches for Halloween 2025 Richard Henderson
  9 siblings, 0 replies; 11+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-10-31 21:15 UTC (permalink / raw)
  To: qemu-devel

From: Igor Mammedov <imammedo@redhat.com>

Commit 87511341c30 broke interrupt handling, replacing interrupts
fetch with a bool and then the remaining code attempting to check
individual bits on that bool value, which effectively masked those
interrupts.

Fix it by checking individual interrupt bits directly instead of
old 'fetch then check' approach.

Fixes: 87511341c30d ("add cpu_test_interrupt()/cpu_set_interrupt() helpers and use them tree wide")
Reported-by: Thomas Huth <thuth@redhat.com>
Signed-off-by: Igor Mammedov <imammedo@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Tested-by: Thomas Huth <thuth@redhat.com>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Message-ID: <20251030165932.138512-1-imammedo@redhat.com>
[PMD: Rebased on commit dde21df2393 "call plugin trap callbacks"]
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 target/rx/helper.c | 45 ++++++++++++++++++++-------------------------
 1 file changed, 20 insertions(+), 25 deletions(-)

diff --git a/target/rx/helper.c b/target/rx/helper.c
index ef47e32add8..e9a7aaf610d 100644
--- a/target/rx/helper.c
+++ b/target/rx/helper.c
@@ -41,11 +41,9 @@ void rx_cpu_unpack_psw(CPURXState *env, uint32_t psw, int rte)
     env->psw_c = FIELD_EX32(psw, PSW, C);
 }
 
-#define INT_FLAGS (CPU_INTERRUPT_HARD | CPU_INTERRUPT_FIR)
 void rx_cpu_do_interrupt(CPUState *cs)
 {
     CPURXState *env = cpu_env(cs);
-    int do_irq = cpu_test_interrupt(cs, INT_FLAGS);
     uint32_t save_psw;
     uint64_t last_pc = env->pc;
 
@@ -59,29 +57,26 @@ void rx_cpu_do_interrupt(CPUState *cs)
     save_psw = rx_cpu_pack_psw(env);
     env->psw_pm = env->psw_i = env->psw_u = 0;
 
-    if (do_irq) {
-        if (do_irq & CPU_INTERRUPT_FIR) {
-            env->bpc = env->pc;
-            env->bpsw = save_psw;
-            env->pc = env->fintv;
-            env->psw_ipl = 15;
-            cpu_reset_interrupt(cs, CPU_INTERRUPT_FIR);
-            qemu_set_irq(env->ack, env->ack_irq);
-            qemu_plugin_vcpu_interrupt_cb(cs, last_pc);
-            qemu_log_mask(CPU_LOG_INT, "fast interrupt raised\n");
-        } else if (do_irq & CPU_INTERRUPT_HARD) {
-            env->isp -= 4;
-            cpu_stl_data(env, env->isp, save_psw);
-            env->isp -= 4;
-            cpu_stl_data(env, env->isp, env->pc);
-            env->pc = cpu_ldl_data(env, env->intb + env->ack_irq * 4);
-            env->psw_ipl = env->ack_ipl;
-            cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
-            qemu_set_irq(env->ack, env->ack_irq);
-            qemu_plugin_vcpu_interrupt_cb(cs, last_pc);
-            qemu_log_mask(CPU_LOG_INT,
-                          "interrupt 0x%02x raised\n", env->ack_irq);
-        }
+    if (cpu_test_interrupt(cs, CPU_INTERRUPT_FIR)) {
+        env->bpc = env->pc;
+        env->bpsw = save_psw;
+        env->pc = env->fintv;
+        env->psw_ipl = 15;
+        cpu_reset_interrupt(cs, CPU_INTERRUPT_FIR);
+        qemu_set_irq(env->ack, env->ack_irq);
+        qemu_plugin_vcpu_interrupt_cb(cs, last_pc);
+        qemu_log_mask(CPU_LOG_INT, "fast interrupt raised\n");
+    } else if (cpu_test_interrupt(cs, CPU_INTERRUPT_HARD)) {
+        env->isp -= 4;
+        cpu_stl_data(env, env->isp, save_psw);
+        env->isp -= 4;
+        cpu_stl_data(env, env->isp, env->pc);
+        env->pc = cpu_ldl_data(env, env->intb + env->ack_irq * 4);
+        env->psw_ipl = env->ack_ipl;
+        cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
+        qemu_set_irq(env->ack, env->ack_irq);
+        qemu_plugin_vcpu_interrupt_cb(cs, last_pc);
+        qemu_log_mask(CPU_LOG_INT, "interrupt 0x%02x raised\n", env->ack_irq);
     } else {
         uint32_t vec = cs->exception_index;
         const char *expname = "unknown exception";
-- 
2.51.0



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PULL 0/9] Accelerators & CPU patches for Halloween 2025
  2025-10-31 21:15 [PULL 0/9] Accelerators & CPU patches for Halloween 2025 Philippe Mathieu-Daudé
                   ` (8 preceding siblings ...)
  2025-10-31 21:15 ` [PULL 9/9] rx: cpu: fix interrupts check in rx_cpu_do_interrupt() Philippe Mathieu-Daudé
@ 2025-11-01 11:13 ` Richard Henderson
  9 siblings, 0 replies; 11+ messages in thread
From: Richard Henderson @ 2025-11-01 11:13 UTC (permalink / raw)
  To: qemu-devel

On 10/31/25 22:15, Philippe Mathieu-Daudé wrote:
> The following changes since commit c494afbb7d552604ad26036127655c534a2645e5:
> 
>    Merge tag 'pull-nvme-20251030' of https://gitlab.com/birkelund/qemu into staging (2025-10-31 12:56:05 +0100)
> 
> are available in the Git repository at:
> 
>    https://github.com/philmd/qemu.git tags/accel-cpus-20251031
> 
> for you to fetch changes up to 0da6099730dbd44a6a96ad18774e5553d1ca7d21:
> 
>    rx: cpu: fix interrupts check in rx_cpu_do_interrupt() (2025-10-31 22:10:50 +0100)
> 
> Following checkpatch.pl error ignored:
> 
>    8/9 Checking commit 46f767bd6814 (tests/unit: add unit test for qemu_hexdump())
>    ERROR: unnecessary whitespace before a quoted newline
>    #59: FILE: tests/unit/test-cutils.c:3662:
>    +            "s is \n";
> 
>    total: 1 errors, 0 warnings, 56 lines checked
> 
> ----------------------------------------------------------------
> Generic CPUs / accelerators patch queue
> 
> - Access CPUState::thread_kicked atomically
> - Make async_safe_run_on_cpu() safe on HVF
> - Fix bql_locked status with condvar APIs
> - Document cpu_memory_rw_debug()
> - Rename init_clocks() -> qemu_init_clocks() to avoid name clashing
> - Fix QEMU_HEXDUMP_LINE_WIDTH logic
> - Fix interrupts check in rx_cpu_do_interrupt()
> ----------------------------------------------------------------

This has merge conflicts with Peter's target-arm queue.
Please rebase vs master and resend.


r~


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2025-11-01 11:13 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-31 21:15 [PULL 0/9] Accelerators & CPU patches for Halloween 2025 Philippe Mathieu-Daudé
2025-10-31 21:15 ` [PULL 1/9] cpus: Access CPUState::thread_kicked atomically Philippe Mathieu-Daudé
2025-10-31 21:15 ` [PULL 2/9] accel/hvf: Make async_safe_run_on_cpu() safe Philippe Mathieu-Daudé
2025-10-31 21:15 ` [PULL 3/9] accel/tcg: Use cpu_is_stopped() helper to access CPUState::stopped Philippe Mathieu-Daudé
2025-10-31 21:15 ` [PULL 4/9] bql: Fix bql_locked status with condvar APIs Philippe Mathieu-Daudé
2025-10-31 21:15 ` [PULL 5/9] exec/cpu: Declare cpu_memory_rw_debug() in 'hw/core/cpu.h' and document Philippe Mathieu-Daudé
2025-10-31 21:15 ` [PULL 6/9] timers: properly prefix init_clocks() Philippe Mathieu-Daudé
2025-10-31 21:15 ` [PULL 7/9] util/hexdump: fix QEMU_HEXDUMP_LINE_WIDTH logic Philippe Mathieu-Daudé
2025-10-31 21:15 ` [PULL 8/9] tests/unit: add unit test for qemu_hexdump() Philippe Mathieu-Daudé
2025-10-31 21:15 ` [PULL 9/9] rx: cpu: fix interrupts check in rx_cpu_do_interrupt() Philippe Mathieu-Daudé
2025-11-01 11:13 ` [PULL 0/9] Accelerators & CPU patches for Halloween 2025 Richard Henderson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).