Skip to contentSkip to conversation

Stop all qemu-cpu threads on a breakpoint

PATCH In review Ilya Leoshkevich 3 participants 37 messages Sep 23, 2024 – Jan 8, 2025

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

cover Stop all qemu-cpu threads on a breakpoint

Hi,

On reporting a breakpoint in a non-non-stop mode, GDB remotes must stop all threads. Currently qemu-user doesn't do that, breaking the debugging session for at least two reasons: concurrent access to the GDB socket, and an assertion within GDB [1].

This series fixes this by importing pause_all_vcpus() from qemu-system. This in turn requires introducing BQL and a few stubs to qemu-user.

Best regards,
Ilya

[1] https://gitlab.com/qemu-project/qemu/-/issues/2465

Ilya Leoshkevich (18):
gdbstub: Make gdb_get_char() static
gdbstub: Move phy_memory_mode to GDBSystemState
gdbstub: Move gdb_syscall_mode to GDBSyscallState
gdbstub: Factor out gdb_try_stop()
accel/tcg: Factor out cpu_exec_user()
qemu-thread: Introduce QEMU_MUTEX_INITIALIZER
qemu-thread: Introduce QEMU_COND_INITIALIZER
replay: Add replay_mutex_{lock,unlock}() stubs for qemu-user
qemu-timer: Provide qemu_clock_enable() stub for qemu-user
cpu: Use BQL in qemu-user
accel/tcg: Unify user implementations of qemu_cpu_kick()
cpu: Track CPUs executing syscalls
cpu: Implement cpu_thread_is_idle() for qemu-user
cpu: Introduce cpu_is_paused()
cpu: Set current_cpu early in qemu-user
cpu: Allow pausing and resuming CPUs in qemu-user
gdbstub: Pause all CPUs before sending stop replies tests/tcg: Stress test thread breakpoints

 accel/tcg/user-exec-stub.c                    |   4 -
 accel/tcg/user-exec.c                         |  55 ++++++
 bsd-user/aarch64/target_arch_cpu.h            |   6 +-
 bsd-user/arm/target_arch_cpu.h                |   5 +-
 bsd-user/freebsd/os-syscall.c                 |  10 +
 bsd-user/i386/target_arch_cpu.h               |   5 +-
 bsd-user/main.c                               |   8 +-
 bsd-user/x86_64/target_arch_cpu.h             |   5 +-
 cpu-common.c                                  | 179 ++++++++++++++++++
 gdbstub/gdbstub.c                             |  17 +-
 gdbstub/internals.h                           |   4 +-
 gdbstub/syscalls.c                            |  20 +-
 gdbstub/system.c                              |  18 +-
 gdbstub/user.c                                |  28 ++-
 include/exec/cpu-common.h                     |  15 ++
 include/exec/replay-core.h                    |  13 ++
 include/hw/core/cpu.h                         |   1 +
 include/qemu/thread-posix.h                   |   8 +
 include/qemu/thread-win32.h                   |   8 +
 include/sysemu/cpus.h                         |   6 -
 include/sysemu/replay.h                       |  13 --
 linux-user/aarch64/cpu_loop.c                 |   5 +-
 linux-user/alpha/cpu_loop.c                   |   5 +-
 linux-user/arm/cpu_loop.c                     |   5 +-
 linux-user/hexagon/cpu_loop.c                 |   5 +-
 linux-user/hppa/cpu_loop.c                    |   5 +-
 linux-user/i386/cpu_loop.c                    |   5 +-
 linux-user/loongarch64/cpu_loop.c             |   5 +-
 linux-user/m68k/cpu_loop.c                    |   5 +-
 linux-user/main.c                             |   9 +-
 linux-user/microblaze/cpu_loop.c              |   5 +-
 linux-user/mips/cpu_loop.c                    |   5 +-
 linux-user/openrisc/cpu_loop.c                |   5 +-
 linux-user/ppc/cpu_loop.c                     |   5 +-
 linux-user/riscv/cpu_loop.c                   |   5 +-
 linux-user/s390x/cpu_loop.c                   |   5 +-
 linux-user/sh4/cpu_loop.c                     |   5 +-
 linux-user/sparc/cpu_loop.c                   |   5 +-
 linux-user/syscall.c                          |  12 ++
 linux-user/xtensa/cpu_loop.c                  |   5 +-
 replay/stubs-system.c                         |   8 +
 stubs/meson.build                             |   8 +
 stubs/qemu-timer.c                            |   6 +
 stubs/replay-mutex.c                          |  10 +
 stubs/replay-tools.c                          |   8 -
 system/cpus.c                                 | 172 +----------------
 tests/tcg/multiarch/Makefile.target           |  13 +-
 .../gdbstub/test-thread-breakpoint-stress.py  |  28 +++
 .../tcg/multiarch/thread-breakpoint-stress.c  |  92 +++++++++
 49 files changed, 552 insertions(+), 327 deletions(-)
 create mode 100644 stubs/qemu-timer.c
 create mode 100644 stubs/replay-mutex.c
 create mode 100644 tests/tcg/multiarch/gdbstub/test-thread-breakpoint-stress.py
 create mode 100644 tests/tcg/multiarch/thread-breakpoint-stress.c
Signature · 1 line
2.46.0
#1 lore raw

PATCH 1/18 gdbstub: Make gdb_get_char() static

It's user-only since commit a7e0f9bd2ace ("gdbstub: abstract target specific details from gdb_put_packet_binary").

---
 gdbstub/internals.h | 2 --
 gdbstub/user.c      | 2 +-
 2 files changed, 1 insertion(+), 3 deletions(-)
gdbstub/internals.h +0 -2
index bf5a5c63029..5acc36846f3 100644
@@ -142,8 +142,6 @@ void gdb_create_default_process(GDBState *s);
 int gdb_signal_to_target(int sig);
 int gdb_target_signal_to_gdb(int sig);
 
-int gdb_get_char(void); /* user only */
-
 /**
  * gdb_continue() - handle continue in mode specific way.
  */
gdbstub/user.c +1 -1
index b36033bc7a2..6a493c5ba3a 100644
@@ -103,7 +103,7 @@ typedef struct {
 
 static GDBUserState gdbserver_user_state;
 
-int gdb_get_char(void)
+static int gdb_get_char(void)
 {
     uint8_t ch;
     int ret;
Signature · 1 line
2.46.0
#3 lore raw

PATCH 2/18 gdbstub: Move phy_memory_mode to GDBSystemState

In reply to Ilya Leoshkevich

Follow the convention that all the pieces of the global stub state must be inside a single struct.

---
 gdbstub/system.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
gdbstub/system.c +6 -6
index 1ad87fe7fdf..5ce357c6c2b 100644
@@ -35,6 +35,7 @@
 typedef struct {
     CharBackend chr;
     Chardev *mon_chr;
+    int phy_memory_mode;
 } GDBSystemState;
 
 GDBSystemState gdbserver_system_state;
@@ -445,14 +446,12 @@ void gdb_qemu_exit(int code)
 /*
  * Memory access
  */
-static int phy_memory_mode;
-
 int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr,
                                uint8_t *buf, int len, bool is_write)
 {
     CPUClass *cc;
 
-    if (phy_memory_mode) {
+    if (gdbserver_system_state.phy_memory_mode) {
         if (is_write) {
             cpu_physical_memory_write(addr, buf, len);
         } else {
@@ -491,7 +490,8 @@ bool gdb_can_reverse(void)
 void gdb_handle_query_qemu_phy_mem_mode(GArray *params,
                                         void *ctx)
 {
-    g_string_printf(gdbserver_state.str_buf, "%d", phy_memory_mode);
+    g_string_printf(gdbserver_state.str_buf, "%d",
+                    gdbserver_system_state.phy_memory_mode);
     gdb_put_strbuf();
 }
 
@@ -503,9 +503,9 @@ void gdb_handle_set_qemu_phy_mem_mode(GArray *params, void *ctx)
     }
 
     if (!gdb_get_cmd_param(params, 0)->val_ul) {
-        phy_memory_mode = 0;
+        gdbserver_system_state.phy_memory_mode = 0;
     } else {
-        phy_memory_mode = 1;
+        gdbserver_system_state.phy_memory_mode = 1;
     }
     gdb_put_packet("OK");
 }
Signature · 1 line
2.46.0
Reviewed-by on v1 2/18
#4 lore raw

Re: [PATCH 02/18] gdbstub: Move phy_memory_mode to GDBSystemState

Quoted 13 lines · Ilya Leoshkevich

Follow the convention that all the pieces of the global stub state must be inside a single struct.

---
  gdbstub/system.c | 12 ++++++------
  1 file changed, 6 insertions(+), 6 deletions(-)
gdbstub/system.c +0 -0
index 1ad87fe7fdf..5ce357c6c2b 100644

gdbstub/system.c line 39

Quoted 5 lines
gdbstub/system.c +1 -0
@@ -35,6 +35,7 @@
  typedef struct {
      CharBackend chr;
      Chardev *mon_chr;
+    int phy_memory_mode;
  } GDBSystemState;

While you're at it, this should be a bool.

Anyway,

r~

Quoted 40 lines
gdbstub/system.c +5 -6
  GDBSystemState gdbserver_system_state;
@@ -445,14 +446,12 @@ void gdb_qemu_exit(int code)
  /*
   * Memory access
   */
-static int phy_memory_mode;
-
  int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr,
                                 uint8_t *buf, int len, bool is_write)
  {
      CPUClass *cc;
 
-    if (phy_memory_mode) {
+    if (gdbserver_system_state.phy_memory_mode) {
          if (is_write) {
              cpu_physical_memory_write(addr, buf, len);
          } else {
@@ -491,7 +490,8 @@ bool gdb_can_reverse(void)
  void gdb_handle_query_qemu_phy_mem_mode(GArray *params,
                                          void *ctx)
  {
-    g_string_printf(gdbserver_state.str_buf, "%d", phy_memory_mode);
+    g_string_printf(gdbserver_state.str_buf, "%d",
+                    gdbserver_system_state.phy_memory_mode);
      gdb_put_strbuf();
  }
 
@@ -503,9 +503,9 @@ void gdb_handle_set_qemu_phy_mem_mode(GArray *params, void *ctx)
      }
 
      if (!gdb_get_cmd_param(params, 0)->val_ul) {
-        phy_memory_mode = 0;
+        gdbserver_system_state.phy_memory_mode = 0;
      } else {
-        phy_memory_mode = 1;
+        gdbserver_system_state.phy_memory_mode = 1;
      }
      gdb_put_packet("OK");
  }
#5 lore raw

PATCH 3/18 gdbstub: Move gdb_syscall_mode to GDBSyscallState

In reply to Ilya Leoshkevich

Follow the convention that all the pieces of the global stub state must be inside a single struct.

---
 gdbstub/syscalls.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
gdbstub/syscalls.c +10 -10
index 4e1295b782d..42307f0abb1 100644
@@ -24,6 +24,11 @@
 typedef struct {
     char syscall_buf[256];
     gdb_syscall_complete_cb current_syscall_cb;
+    enum {
+        GDB_SYS_UNKNOWN,
+        GDB_SYS_ENABLED,
+        GDB_SYS_DISABLED,
+    } mode;
 } GDBSyscallState;
 
 static GDBSyscallState gdbserver_syscall_state;
@@ -37,12 +42,6 @@ static bool gdb_attached(void)
     return gdbserver_state.init && gdbserver_state.c_cpu;
 }
 
-static enum {
-    GDB_SYS_UNKNOWN,
-    GDB_SYS_ENABLED,
-    GDB_SYS_DISABLED,
-} gdb_syscall_mode;
-
 /* Decide if either remote gdb syscalls or native file IO should be used. */
 int use_gdb_syscalls(void)
 {
@@ -57,16 +56,17 @@ int use_gdb_syscalls(void)
 
     /* -semihosting-config target=auto */
     /* On the first call check if gdb is connected and remember. */
-    if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
-        gdb_syscall_mode = gdb_attached() ? GDB_SYS_ENABLED : GDB_SYS_DISABLED;
+    if (gdbserver_syscall_state.mode == GDB_SYS_UNKNOWN) {
+        gdbserver_syscall_state.mode = gdb_attached() ? GDB_SYS_ENABLED :
+                                                        GDB_SYS_DISABLED;
     }
-    return gdb_syscall_mode == GDB_SYS_ENABLED;
+    return gdbserver_syscall_state.mode == GDB_SYS_ENABLED;
 }
 
 /* called when the stub detaches */
 void gdb_disable_syscalls(void)
 {
-    gdb_syscall_mode = GDB_SYS_DISABLED;
+    gdbserver_syscall_state.mode = GDB_SYS_DISABLED;
 }
 
 void gdb_syscall_reset(void)
Signature · 1 line
2.46.0
#7 lore raw

PATCH 4/18 gdbstub: Factor out gdb_try_stop()

In reply to Ilya Leoshkevich

Move checking and setting allow_stop_reply into a function.

---
 gdbstub/gdbstub.c   | 15 +++++++++++----
 gdbstub/internals.h |  2 ++
 gdbstub/system.c    |  6 ++----
 gdbstub/user.c      | 11 ++++-------
 4 files changed, 19 insertions(+), 15 deletions(-)
gdbstub/gdbstub.c +11 -4
index d08568cea0e..a096104b07a 100644
@@ -1422,11 +1422,10 @@ static void handle_v_attach(GArray *params, void *user_ctx)
     gdbserver_state.g_cpu = cpu;
     gdbserver_state.c_cpu = cpu;
 
-    if (gdbserver_state.allow_stop_reply) {
+    if (gdb_try_stop()) {
         g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
         gdb_append_thread_id(cpu, gdbserver_state.str_buf);
         g_string_append_c(gdbserver_state.str_buf, ';');
-        gdbserver_state.allow_stop_reply = false;
 cleanup:
         gdb_put_strbuf();
     }
@@ -2016,12 +2015,11 @@ static void handle_gen_set(GArray *params, void *user_ctx)
 
 static void handle_target_halt(GArray *params, void *user_ctx)
 {
-    if (gdbserver_state.allow_stop_reply) {
+    if (gdb_try_stop()) {
         g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
         gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf);
         g_string_append_c(gdbserver_state.str_buf, ';');
         gdb_put_strbuf();
-        gdbserver_state.allow_stop_reply = false;
     }
     /*
      * Remove all the breakpoints when this query is issued,
@@ -2493,3 +2491,12 @@ void gdb_create_default_process(GDBState *s)
     process->target_xml = NULL;
 }
 
+bool gdb_try_stop(void)
+{
+    if (!gdbserver_state.allow_stop_reply) {
+        return false;
+    }
+
+    gdbserver_state.allow_stop_reply = false;
+    return true;
+}
gdbstub/internals.h +2 -0
index 5acc36846f3..310861e581b 100644
@@ -215,4 +215,6 @@ void gdb_breakpoint_remove_all(CPUState *cs);
 int gdb_target_memory_rw_debug(CPUState *cs, hwaddr addr,
                                uint8_t *buf, int len, bool is_write);
 
+bool gdb_try_stop(void);
+
 #endif /* GDBSTUB_INTERNALS_H */
gdbstub/system.c +2 -4
index 5ce357c6c2b..fbe9528569c 100644
@@ -141,7 +141,7 @@ static void gdb_vm_state_change(void *opaque, bool running, RunState state)
         return;
     }
 
-    if (!gdbserver_state.allow_stop_reply) {
+    if (!gdb_try_stop()) {
         return;
     }
 
@@ -211,7 +211,6 @@ static void gdb_vm_state_change(void *opaque, bool running, RunState state)
 
 send_packet:
     gdb_put_packet(buf->str);
-    gdbserver_state.allow_stop_reply = false;
 
     /* disable single step if it was enabled */
     cpu_single_step(cpu, 0);
@@ -428,10 +427,9 @@ void gdb_exit(int code)
 
     trace_gdbstub_op_exiting((uint8_t)code);
 
-    if (gdbserver_state.allow_stop_reply) {
+    if (gdb_try_stop()) {
         snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
         gdb_put_packet(buf);
-        gdbserver_state.allow_stop_reply = false;
     }
 
     qemu_chr_fe_deinit(&gdbserver_system_state.chr, true);
gdbstub/user.c +4 -7
index 6a493c5ba3a..77ba227fc3b 100644
@@ -181,10 +181,9 @@ void gdb_exit(int code)
 
     trace_gdbstub_op_exiting((uint8_t)code);
 
-    if (gdbserver_state.allow_stop_reply) {
+    if (gdb_try_stop()) {
         snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
         gdb_put_packet(buf);
-        gdbserver_state.allow_stop_reply = false;
     }
 
 }
@@ -222,7 +221,7 @@ int gdb_handlesig(CPUState *cpu, int sig, const char *reason, void *siginfo,
 
     if (sig != 0) {
         gdb_set_stop_cpu(cpu);
-        if (gdbserver_state.allow_stop_reply) {
+        if (gdb_try_stop()) {
             g_string_printf(gdbserver_state.str_buf,
                             "T%02xthread:", gdb_target_signal_to_gdb(sig));
             gdb_append_thread_id(cpu, gdbserver_state.str_buf);
@@ -231,7 +230,6 @@ int gdb_handlesig(CPUState *cpu, int sig, const char *reason, void *siginfo,
                 g_string_append(gdbserver_state.str_buf, reason);
             }
             gdb_put_strbuf();
-            gdbserver_state.allow_stop_reply = false;
         }
     }
     /*
@@ -276,13 +274,12 @@ void gdb_signalled(CPUArchState *env, int sig)
     char buf[4];
 
     if (!gdbserver_state.init || gdbserver_user_state.fd < 0 ||
-        !gdbserver_state.allow_stop_reply) {
+        !gdb_try_stop()) {
         return;
     }
 
     snprintf(buf, sizeof(buf), "X%02x", gdb_target_signal_to_gdb(sig));
     gdb_put_packet(buf);
-    gdbserver_state.allow_stop_reply = false;
 }
 
 static void gdb_accept_init(int fd)
@@ -502,7 +499,7 @@ void gdbserver_fork_end(CPUState *cpu, pid_t pid)
         gdbserver_user_state.fork_peer_pid = pid;
         gdbserver_user_state.fork_peer_tid = pid;
 
-        if (!gdbserver_state.allow_stop_reply) {
+        if (!gdb_try_stop()) {
             goto fail;
         }
         g_string_printf(gdbserver_state.str_buf,
Signature · 1 line
2.46.0
Reviewed-by on v1 4/18
#8 lore raw

Re: [PATCH 04/18] gdbstub: Factor out gdb_try_stop()

Quoted 9 lines · Ilya Leoshkevich

Move checking and setting allow_stop_reply into a function.

---
  gdbstub/gdbstub.c   | 15 +++++++++++----
  gdbstub/internals.h |  2 ++
  gdbstub/system.c    |  6 ++----
  gdbstub/user.c      | 11 ++++-------
  4 files changed, 19 insertions(+), 15 deletions(-)

r~

#9 lore raw

PATCH 5/18 accel/tcg: Factor out cpu_exec_user()

In reply to Ilya Leoshkevich

All linux-user cpu_loop() implementations contain the same sequence of function calls. Factor them out so that they can be changed in one place.

---
 accel/tcg/user-exec.c              | 12 ++++++++++++
 bsd-user/aarch64/target_arch_cpu.h |  6 +-----
 bsd-user/arm/target_arch_cpu.h     |  5 +----
 bsd-user/i386/target_arch_cpu.h    |  5 +----
 bsd-user/x86_64/target_arch_cpu.h  |  5 +----
 include/exec/cpu-common.h          |  2 ++
 linux-user/aarch64/cpu_loop.c      |  5 +----
 linux-user/alpha/cpu_loop.c        |  5 +----
 linux-user/arm/cpu_loop.c          |  5 +----
 linux-user/hexagon/cpu_loop.c      |  5 +----
 linux-user/hppa/cpu_loop.c         |  5 +----
 linux-user/i386/cpu_loop.c         |  5 +----
 linux-user/loongarch64/cpu_loop.c  |  5 +----
 linux-user/m68k/cpu_loop.c         |  5 +----
 linux-user/microblaze/cpu_loop.c   |  5 +----
 linux-user/mips/cpu_loop.c         |  5 +----
 linux-user/openrisc/cpu_loop.c     |  5 +----
 linux-user/ppc/cpu_loop.c          |  5 +----
 linux-user/riscv/cpu_loop.c        |  5 +----
 linux-user/s390x/cpu_loop.c        |  5 +----
 linux-user/sh4/cpu_loop.c          |  5 +----
 linux-user/sparc/cpu_loop.c        |  5 +----
 linux-user/xtensa/cpu_loop.c       |  5 +----
 23 files changed, 35 insertions(+), 85 deletions(-)
accel/tcg/user-exec.c +12 -0
index 7ddc47b0ba4..ca3e8e988ee 100644
@@ -1288,3 +1288,15 @@ static void *atomic_mmu_lookup(CPUState *cpu, vaddr addr, MemOpIdx oi,
 #define DATA_SIZE 16
 #include "atomic_template.h"
 #endif
+
+int cpu_exec_user(CPUState *cs)
+{
+    int trapnr;
+
+    cpu_exec_start(cs);
+    trapnr = cpu_exec(cs);
+    cpu_exec_end(cs);
+    process_queued_cpu_work(cs);
+
+    return trapnr;
+}
bsd-user/aarch64/target_arch_cpu.h +1 -5
index b288e0d069b..6ab6c07e973 100644
@@ -51,11 +51,7 @@ static inline void target_cpu_loop(CPUARMState *env)
     abi_long ret;
 
     for (;;) {
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
-
+        trapnr = cpu_exec_user(cs);
         switch (trapnr) {
         case EXCP_SWI:
             /* See arm64/arm64/trap.c cpu_fetch_syscall_args() */
bsd-user/arm/target_arch_cpu.h +1 -4
index 517d0087644..2fa97c168c0 100644
@@ -43,10 +43,7 @@ static inline void target_cpu_loop(CPUARMState *env)
     CPUState *cs = env_cpu(env);
 
     for (;;) {
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
+        trapnr = cpu_exec_user(cs);
         switch (trapnr) {
         case EXCP_UDEF:
         case EXCP_NOCP:
bsd-user/i386/target_arch_cpu.h +1 -4
index 9bf2c4244b7..cbc4d77daba 100644
@@ -110,10 +110,7 @@ static inline void target_cpu_loop(CPUX86State *env)
     /* target_siginfo_t info; */
 
     for (;;) {
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
+        trapnr = cpu_exec_user(cs);
 
         switch (trapnr) {
         case 0x80: {
bsd-user/x86_64/target_arch_cpu.h +1 -4
index 4094d61da1a..5442056d009 100644
@@ -118,10 +118,7 @@ static inline void target_cpu_loop(CPUX86State *env)
     /* target_siginfo_t info; */
 
     for (;;) {
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
+        trapnr = cpu_exec_user(cs);
 
         switch (trapnr) {
         case EXCP_SYSCALL:
include/exec/cpu-common.h +2 -0
index 2e1b499cb71..4a7b43f9aa3 100644
@@ -226,6 +226,8 @@ G_NORETURN void cpu_loop_exit_restore(CPUState *cpu, uintptr_t pc);
 
 /* accel/tcg/cpu-exec.c */
 int cpu_exec(CPUState *cpu);
+/* accel/tcg/user-exec.c */
+int cpu_exec_user(CPUState *cs);
 
 /**
  * env_archcpu(env)
linux-user/aarch64/cpu_loop.c +1 -4
index 71cdc8be50c..9104996fd46 100644
@@ -83,10 +83,7 @@ void cpu_loop(CPUARMState *env)
     abi_long ret;
 
     for (;;) {
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
+        trapnr = cpu_exec_user(cs);
 
         switch (trapnr) {
         case EXCP_SWI:
linux-user/alpha/cpu_loop.c +1 -4
index 2ea039aa71f..dda42aa9ee7 100644
@@ -32,10 +32,7 @@ void cpu_loop(CPUAlphaState *env)
     while (1) {
         bool arch_interrupt = true;
 
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
+        trapnr = cpu_exec_user(cs);
 
         switch (trapnr) {
         case EXCP_RESET:
linux-user/arm/cpu_loop.c +1 -4
index ec665862d93..81fb01c4f95 100644
@@ -325,10 +325,7 @@ void cpu_loop(CPUARMState *env)
     abi_ulong ret;
 
     for(;;) {
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
+        trapnr = cpu_exec_user(cs);
 
         switch(trapnr) {
         case EXCP_UDEF:
linux-user/hexagon/cpu_loop.c +1 -4
index d41159e52ad..678d8a42abb 100644
@@ -33,10 +33,7 @@ void cpu_loop(CPUHexagonState *env)
     target_ulong ret;
 
     for (;;) {
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
+        trapnr = cpu_exec_user(cs);
 
         switch (trapnr) {
         case EXCP_INTERRUPT:
linux-user/hppa/cpu_loop.c +1 -4
index bc093b8fe8b..121763a56cb 100644
@@ -114,10 +114,7 @@ void cpu_loop(CPUHPPAState *env)
     int trapnr;
 
     while (1) {
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
+        trapnr = cpu_exec_user(cs);
 
         switch (trapnr) {
         case EXCP_SYSCALL:
linux-user/i386/cpu_loop.c +1 -4
index 92beb6830cc..8707a3eaf25 100644
@@ -210,10 +210,7 @@ void cpu_loop(CPUX86State *env)
     abi_ulong ret;
 
     for(;;) {
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
+        trapnr = cpu_exec_user(cs);
 
         switch(trapnr) {
         case 0x80:
linux-user/loongarch64/cpu_loop.c +1 -4
index 73d7b6796a4..1735c1c4ff1 100644
@@ -18,10 +18,7 @@ void cpu_loop(CPULoongArchState *env)
     abi_long ret;
 
     for (;;) {
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
+        trapnr = cpu_exec_user(cs);
 
         switch (trapnr) {
         case EXCP_INTERRUPT:
linux-user/m68k/cpu_loop.c +1 -4
index f79b8e4ab05..8e2f9161c64 100644
@@ -30,10 +30,7 @@ void cpu_loop(CPUM68KState *env)
     unsigned int n;
 
     for(;;) {
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
+        trapnr = cpu_exec_user(cs);
 
         switch(trapnr) {
         case EXCP_ILLEGAL:
linux-user/microblaze/cpu_loop.c +1 -4
index 212e62d0a62..3867a62c07b 100644
@@ -29,10 +29,7 @@ void cpu_loop(CPUMBState *env)
     CPUState *cs = env_cpu(env);
 
     while (1) {
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
+        trapnr = cpu_exec_user(cs);
 
         switch (trapnr) {
         case EXCP_INTERRUPT:
linux-user/mips/cpu_loop.c +1 -4
index 462387a0737..d14af50cbbe 100644
@@ -71,10 +71,7 @@ void cpu_loop(CPUMIPSState *env)
 # endif
 
     for(;;) {
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
+        trapnr = cpu_exec_user(cs);
 
         switch(trapnr) {
         case EXCP_SYSCALL:
linux-user/openrisc/cpu_loop.c +1 -4
index a7aa586c8f9..f1eb799bd27 100644
@@ -30,10 +30,7 @@ void cpu_loop(CPUOpenRISCState *env)
     abi_long ret;
 
     for (;;) {
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
+        trapnr = cpu_exec_user(cs);
 
         switch (trapnr) {
         case EXCP_SYSCALL:
linux-user/ppc/cpu_loop.c +1 -4
index 02204ad8beb..53a6ad996f3 100644
@@ -74,10 +74,7 @@ void cpu_loop(CPUPPCState *env)
     for(;;) {
         bool arch_interrupt;
 
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
+        trapnr = cpu_exec_user(cs);
 
         arch_interrupt = true;
         switch (trapnr) {
linux-user/riscv/cpu_loop.c +1 -4
index 52c49c2e426..e9013b312d2 100644
@@ -33,10 +33,7 @@ void cpu_loop(CPURISCVState *env)
     target_ulong ret;
 
     for (;;) {
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
+        trapnr = cpu_exec_user(cs);
 
         switch (trapnr) {
         case EXCP_INTERRUPT:
linux-user/s390x/cpu_loop.c +1 -4
index 8b7ac2879ef..d8231403b65 100644
@@ -61,10 +61,7 @@ void cpu_loop(CPUS390XState *env)
     abi_long ret;
 
     while (1) {
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
+        trapnr = cpu_exec_user(cs);
 
         switch (trapnr) {
         case EXCP_INTERRUPT:
linux-user/sh4/cpu_loop.c +1 -4
index c805f9db110..282d955ee92 100644
@@ -31,10 +31,7 @@ void cpu_loop(CPUSH4State *env)
     while (1) {
         bool arch_interrupt = true;
 
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
+        trapnr = cpu_exec_user(cs);
 
         switch (trapnr) {
         case 0x160:
linux-user/sparc/cpu_loop.c +1 -4
index 50424a54df5..8923a44d607 100644
@@ -217,10 +217,7 @@ void cpu_loop (CPUSPARCState *env)
     abi_long ret;
 
     while (1) {
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
+        trapnr = cpu_exec_user(cs);
 
         switch (trapnr) {
         case TARGET_TT_SYSCALL:
linux-user/xtensa/cpu_loop.c +1 -4
index d51ce053926..7eb21415213 100644
@@ -130,10 +130,7 @@ void cpu_loop(CPUXtensaState *env)
     int trapnr;
 
     while (1) {
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
+        trapnr = cpu_exec_user(cs);
 
         env->sregs[PS] &= ~PS_EXCM;
         switch (trapnr) {
Signature · 1 line
2.46.0
Reviewed-by on v1 5/18
#10 lore raw

Re: [PATCH 05/18] accel/tcg: Factor out cpu_exec_user()

Quoted 22 lines · Ilya Leoshkevich

All linux-user cpu_loop() implementations contain the same sequence of function calls. Factor them out so that they can be changed in one place.

---
  accel/tcg/user-exec.c              | 12 ++++++++++++
  bsd-user/aarch64/target_arch_cpu.h |  6 +-----
  bsd-user/arm/target_arch_cpu.h     |  5 +----
  bsd-user/i386/target_arch_cpu.h    |  5 +----
  bsd-user/x86_64/target_arch_cpu.h  |  5 +----
  include/exec/cpu-common.h          |  2 ++
  linux-user/aarch64/cpu_loop.c      |  5 +----
  linux-user/alpha/cpu_loop.c        |  5 +----
  linux-user/arm/cpu_loop.c          |  5 +----
  linux-user/hexagon/cpu_loop.c      |  5 +----
  linux-user/hppa/cpu_loop.c         |  5 +----
  linux-user/i386/cpu_loop.c         |  5 +----
  linux-user/loongarch64/cpu_loop.c  |  5 +----
  linux-user/m68k/cpu_loop.c         |  5 +----
  linux-user/microblaze/cpu_loop.c   |  5 +----
  linux-user/mips/cpu_loop.c         |  5 +----
Quoted 8 lines
  linux-user/openrisc/cpu_loop.c     |  5 +----
  linux-user/ppc/cpu_loop.c          |  5 +----
  linux-user/riscv/cpu_loop.c        |  5 +----
  linux-user/s390x/cpu_loop.c        |  5 +----
  linux-user/sh4/cpu_loop.c          |  5 +----
  linux-user/sparc/cpu_loop.c        |  5 +----
  linux-user/xtensa/cpu_loop.c       |  5 +----
  23 files changed, 35 insertions(+), 85 deletions(-)

r~

#11 lore raw

PATCH 6/18 qemu-thread: Introduce QEMU_MUTEX_INITIALIZER

In reply to Ilya Leoshkevich

Allow static initialization of mutexes.

---
 include/qemu/thread-posix.h | 6 ++++++
 include/qemu/thread-win32.h | 6 ++++++
 2 files changed, 12 insertions(+)
include/qemu/thread-posix.h +6 -0
index 5f2f3d1386b..fc0846bfa7c 100644
@@ -13,6 +13,12 @@ struct QemuMutex {
     bool initialized;
 };
 
+#ifdef CONFIG_DEBUG_MUTEX
+#define QEMU_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER, NULL, 0, true}
+#else
+#define QEMU_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER, true}
+#endif
+
 /*
  * QemuRecMutex cannot be a typedef of QemuMutex lest we have two
  * compatible cases in _Generic.  See qemu/lockable.h.
include/qemu/thread-win32.h +6 -0
index d95af4498fc..ed1f2d0e733 100644
@@ -12,6 +12,12 @@ struct QemuMutex {
     bool initialized;
 };
 
+#ifdef CONFIG_DEBUG_MUTEX
+#define QEMU_MUTEX_INITIALIZER {SRWLOCK_INIT, NULL, 0, true}
+#else
+#define QEMU_MUTEX_INITIALIZER {SRWLOCK_INIT, true}
+#endif
+
 typedef struct QemuRecMutex QemuRecMutex;
 struct QemuRecMutex {
     CRITICAL_SECTION lock;
Signature · 1 line
2.46.0
#13 lore raw

PATCH 7/18 qemu-thread: Introduce QEMU_COND_INITIALIZER

In reply to Ilya Leoshkevich

Allow static initialization of condition variables.

---
 include/qemu/thread-posix.h | 2 ++
 include/qemu/thread-win32.h | 2 ++
 2 files changed, 4 insertions(+)
include/qemu/thread-posix.h +2 -0
index fc0846bfa7c..ed08181a9c6 100644
@@ -32,6 +32,8 @@ struct QemuCond {
     bool initialized;
 };
 
+#define QEMU_COND_INITIALIZER {PTHREAD_COND_INITIALIZER, true}
+
 struct QemuSemaphore {
     QemuMutex mutex;
     QemuCond cond;
include/qemu/thread-win32.h +2 -0
index ed1f2d0e733..e1b014fcac4 100644
@@ -29,6 +29,8 @@ struct QemuCond {
     bool initialized;
 };
 
+#define QEMU_COND_INITIALIZER {CONDITION_VARIABLE_INIT, true}
+
 struct QemuSemaphore {
     HANDLE sema;
     bool initialized;
Signature · 1 line
2.46.0
#15 lore raw

PATCH 8/18 replay: Add replay_mutex_{lock, unlock}() stubs for qemu-user

In reply to Ilya Leoshkevich

Sharing pause_all_vcpus() with qemu-user requires providing no-op definitions of replay mutex functions. Make these functions available via replay-core.h and move the existing stubs to a separate file.

---
 include/exec/replay-core.h | 13 +++++++++++++
 include/sysemu/replay.h    | 13 -------------
 replay/stubs-system.c      |  8 ++++++++
 stubs/meson.build          |  4 ++++
 stubs/replay-mutex.c       | 10 ++++++++++
 stubs/replay-tools.c       |  8 --------
 6 files changed, 35 insertions(+), 21 deletions(-)
 create mode 100644 stubs/replay-mutex.c
include/exec/replay-core.h +13 -0
index 244c77acce5..74766bae90d 100644
@@ -77,4 +77,17 @@ void replay_save_random(int ret, void *buf, size_t len);
 /* Loads the saved values for the random number generator */
 int replay_read_random(void *buf, size_t len);
 
+/* Replay locking
+ *
+ * The locks are needed to protect the shared structures and log file
+ * when doing record/replay. They also are the main sync-point between
+ * the main-loop thread and the vCPU thread. This was a role
+ * previously filled by the BQL which has been busy trying to reduce
+ * its impact across the code. This ensures blocks of events stay
+ * sequential and reproducible.
+ */
+
+void replay_mutex_lock(void);
+void replay_mutex_unlock(void);
+
 #endif
include/sysemu/replay.h +0 -13
index 8102fa54f01..d6e0342b27c 100644
@@ -51,19 +51,6 @@ typedef struct ReplayNetState ReplayNetState;
 /* Name of the initial VM snapshot */
 extern char *replay_snapshot;
 
-/* Replay locking
- *
- * The locks are needed to protect the shared structures and log file
- * when doing record/replay. They also are the main sync-point between
- * the main-loop thread and the vCPU thread. This was a role
- * previously filled by the BQL which has been busy trying to reduce
- * its impact across the code. This ensures blocks of events stay
- * sequential and reproducible.
- */
-
-void replay_mutex_lock(void);
-void replay_mutex_unlock(void);
-
 /* Processing the instructions */
 
 /*! Returns number of executed instructions. */
replay/stubs-system.c +8 -0
index 50cefdb2d69..45984e3b098 100644
@@ -94,3 +94,11 @@ void qmp_replay_seek(int64_t icount, Error **errp)
     error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
               "replay support not available");
 }
+
+void replay_mutex_lock(void)
+{
+}
+
+void replay_mutex_unlock(void)
+{
+}
stubs/meson.build +4 -0
index 772a3e817df..ab4b98a0e18 100644
@@ -46,6 +46,10 @@ if have_block or have_ga
   stub_ss.add(files('qmp-quit.c'))
 endif
 
+if have_block or have_ga or have_user
+  stub_ss.add(files('replay-mutex.c'))
+endif
+
 if have_block or have_user
   stub_ss.add(files('qtest.c'))
   stub_ss.add(files('vm-stop.c'))
stubs/replay-mutex.c new file +10 -0
new file mode 100644
index 00000000000..08c69139bcb
@@ -0,0 +1,10 @@
+#include "qemu/osdep.h"
+#include "exec/replay-core.h"
+
+void replay_mutex_lock(void)
+{
+}
+
+void replay_mutex_unlock(void)
+{
+}
stubs/replay-tools.c +0 -8
index 3e8ca3212d9..2bca8ab6657 100644
@@ -41,14 +41,6 @@ bool replay_checkpoint(ReplayCheckpoint checkpoint)
     return true;
 }
 
-void replay_mutex_lock(void)
-{
-}
-
-void replay_mutex_unlock(void)
-{
-}
-
 void replay_register_char_driver(struct Chardev *chr)
 {
 }
Signature · 1 line
2.46.0
#16 lore raw

PATCH 9/18 qemu-timer: Provide qemu_clock_enable() stub for qemu-user

In reply to Ilya Leoshkevich

Sharing pause_all_vcpus() with qemu-user requires a no-op implementation of qemu_clock_enable().

---
 stubs/meson.build  | 4 ++++
 stubs/qemu-timer.c | 6 ++++++
 2 files changed, 10 insertions(+)
 create mode 100644 stubs/qemu-timer.c
stubs/meson.build +4 -0
index ab4b98a0e18..9e78a0b9745 100644
@@ -46,6 +46,10 @@ if have_block or have_ga
   stub_ss.add(files('qmp-quit.c'))
 endif
 
+if have_user
+  stub_ss.add(files('qemu-timer.c'))
+endif
+
 if have_block or have_ga or have_user
   stub_ss.add(files('replay-mutex.c'))
 endif
stubs/qemu-timer.c new file +6 -0
new file mode 100644
index 00000000000..27b67f7b313
@@ -0,0 +1,6 @@
+#include "qemu/osdep.h"
+#include "qemu/timer.h"
+
+void qemu_clock_enable(QEMUClockType type, bool enabled)
+{
+}
Signature · 1 line
2.46.0
#17 lore raw

PATCH 10/18 cpu: Use BQL in qemu-user

In reply to Ilya Leoshkevich

Currently BQL is stubbed out in qemu-user. However, enabling the ability to pause and resume CPUs requires BQL, so introduce it.

---
 accel/tcg/user-exec.c         |  2 ++
 bsd-user/freebsd/os-syscall.c |  6 ++++
 bsd-user/main.c               |  2 ++
 cpu-common.c                  | 45 ++++++++++++++++++++++++++++++
 gdbstub/user.c                |  5 ++++
 linux-user/main.c             |  3 ++
 linux-user/syscall.c          |  6 ++++
 system/cpus.c                 | 52 ++---------------------------------
 8 files changed, 72 insertions(+), 49 deletions(-)
accel/tcg/user-exec.c +2 -0
index ca3e8e988ee..d56882c87f3 100644
@@ -1293,9 +1293,11 @@ int cpu_exec_user(CPUState *cs)
 {
     int trapnr;
 
+    bql_unlock();
     cpu_exec_start(cs);
     trapnr = cpu_exec(cs);
     cpu_exec_end(cs);
+    bql_lock();
     process_queued_cpu_work(cs);
 
     return trapnr;
bsd-user/freebsd/os-syscall.c +6 -0
index ca2f6fdb66e..c2849d43223 100644
@@ -32,6 +32,7 @@
 #include "qemu.h"
 #include "signal-common.h"
 #include "user/syscall-trace.h"
+#include "qemu/main-loop.h"
 
 /* BSD independent syscall shims */
 #include "bsd-file.h"
@@ -935,16 +936,21 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
 {
     abi_long ret;
 
+    bql_unlock();
+
     if (do_strace) {
         print_freebsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
     }
 
     ret = freebsd_syscall(cpu_env, num, arg1, arg2, arg3, arg4, arg5, arg6,
                           arg7, arg8);
+
     if (do_strace) {
         print_freebsd_syscall_ret(num, ret);
     }
 
+    bql_lock();
+
     return ret;
 }
 
bsd-user/main.c +2 -0
index cc980e6f401..ba5b54c228d 100644
@@ -48,6 +48,7 @@
 #include "qemu/guest-random.h"
 #include "gdbstub/user.h"
 #include "exec/page-vary.h"
+#include "qemu/main-loop.h"
 
 #include "host-os.h"
 #include "target_arch_cpu.h"
@@ -616,6 +617,7 @@ int main(int argc, char **argv)
 
     target_cpu_init(env, regs);
 
+    bql_lock();
     if (gdbstub) {
         gdbserver_start(gdbstub);
         gdb_handlesig(cpu, 0, NULL, NULL, 0);
cpu-common.c +45 -0

48 lines not on this page · raw message

gdbstub/user.c +5 -0
index 77ba227fc3b..82007b09db6 100644
@@ -12,6 +12,7 @@
 #include "qemu/osdep.h"
 #include "qemu/bitops.h"
 #include "qemu/cutils.h"
+#include "qemu/main-loop.h"
 #include "qemu/sockets.h"
 #include "exec/hwaddr.h"
 #include "exec/tb-flush.h"
@@ -169,6 +170,8 @@ void gdb_exit(int code)
 {
     char buf[4];
 
+    BQL_LOCK_GUARD();
+
     if (!gdbserver_state.init) {
         return;
     }
@@ -464,6 +467,8 @@ void gdbserver_fork_end(CPUState *cpu, pid_t pid)
     char b;
     int fd;
 
+    BQL_LOCK_GUARD();
+
     if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
         return;
     }
linux-user/main.c +3 -0
index 8143a0d4b02..016f60bf3dc 100644
@@ -57,6 +57,7 @@
 #include "user-mmap.h"
 #include "tcg/perf.h"
 #include "exec/page-vary.h"
+#include "qemu/main-loop.h"
 
 #ifdef CONFIG_SEMIHOSTING
 #include "semihosting/semihost.h"
@@ -1011,6 +1012,8 @@ int main(int argc, char **argv, char **envp)
 
     target_cpu_copy_regs(env, regs);
 
+    bql_lock();
+
     if (gdbstub) {
         if (gdbserver_start(gdbstub) < 0) {
             fprintf(stderr, "qemu: could not open gdbserver on %s\n",
linux-user/syscall.c +6 -0
index b693aeff5bb..ff34ae11340 100644
@@ -144,6 +144,7 @@
 #include "qapi/error.h"
 #include "fd-trans.h"
 #include "cpu_loop-common.h"
+#include "qemu/main-loop.h"
 
 #ifndef CLONE_IO
 #define CLONE_IO                0x80000000      /* Clone io context */
@@ -6529,6 +6530,7 @@ static void *clone_func(void *arg)
     /* Wait until the parent has finished initializing the tls state.  */
     pthread_mutex_lock(&clone_lock);
     pthread_mutex_unlock(&clone_lock);
+    bql_lock();
     cpu_loop(env);
     /* never exits */
     return NULL;
@@ -13772,6 +13774,8 @@ abi_long do_syscall(CPUArchState *cpu_env, int num, abi_long arg1,
     record_syscall_start(cpu, num, arg1,
                          arg2, arg3, arg4, arg5, arg6, arg7, arg8);
 
+    bql_unlock();
+
     if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
         print_syscall(cpu_env, num, arg1, arg2, arg3, arg4, arg5, arg6);
     }
@@ -13784,6 +13788,8 @@ abi_long do_syscall(CPUArchState *cpu_env, int num, abi_long arg1,
                           arg3, arg4, arg5, arg6);
     }
 
+    bql_lock();
+
     record_syscall_return(cpu, num, ret);
     return ret;
 }
system/cpus.c +3 -49

97 lines not on this page · raw message

Signature · 1 line
2.46.0
#18 lore raw

PATCH 11/18 accel/tcg: Unify user implementations of qemu_cpu_kick()

In reply to Ilya Leoshkevich

linux-user and bsd-user have the same implementation. Move it to user-exec.c.

---
 accel/tcg/user-exec.c | 5 +++++
 bsd-user/main.c       | 5 -----
 linux-user/main.c     | 5 -----
 3 files changed, 5 insertions(+), 10 deletions(-)
accel/tcg/user-exec.c +5 -0
index d56882c87f3..7bd6e94b8e8 100644
@@ -1302,3 +1302,8 @@ int cpu_exec_user(CPUState *cs)
 
     return trapnr;
 }
+
+void qemu_cpu_kick(CPUState *cpu)
+{
+    cpu_exit(cpu);
+}
bsd-user/main.c +0 -5
index ba5b54c228d..b424a21f643 100644
@@ -208,11 +208,6 @@ bool qemu_cpu_is_self(CPUState *cpu)
     return thread_cpu == cpu;
 }
 
-void qemu_cpu_kick(CPUState *cpu)
-{
-    cpu_exit(cpu);
-}
-
 /* Assumes contents are already zeroed.  */
 static void init_task_state(TaskState *ts)
 {
linux-user/main.c +0 -5
index 016f60bf3dc..60091cf3053 100644
@@ -186,11 +186,6 @@ bool qemu_cpu_is_self(CPUState *cpu)
     return thread_cpu == cpu;
 }
 
-void qemu_cpu_kick(CPUState *cpu)
-{
-    cpu_exit(cpu);
-}
-
 void task_settid(TaskState *ts)
 {
     if (ts->ts_tid == 0) {
Signature · 1 line
2.46.0
Reviewed-by on v1 11/18
#19 lore raw

Re: [PATCH 11/18] accel/tcg: Unify user implementations of qemu_cpu_kick()

Quoted 9 lines · Ilya Leoshkevich

linux-user and bsd-user have the same implementation. Move it to user-exec.c.

---
  accel/tcg/user-exec.c | 5 +++++
  bsd-user/main.c       | 5 -----
  linux-user/main.c     | 5 -----
  3 files changed, 5 insertions(+), 10 deletions(-)

r~

#20 lore raw

PATCH 12/18 cpu: Track CPUs executing syscalls

In reply to Ilya Leoshkevich

CPUs that execute syscalls should be considered paused by all_vcpus_paused(). Lay the groundwork by introducing a bool field in CPUState to track this. The field is not used by sysemu, but it's only one byte, so it should not be a problem.

---
 accel/tcg/user-exec.c         | 10 ++++++++++
 bsd-user/freebsd/os-syscall.c |  4 ++++
 include/exec/cpu-common.h     |  2 ++
 include/hw/core/cpu.h         |  1 +
 linux-user/syscall.c          |  5 +++++
 5 files changed, 22 insertions(+)
accel/tcg/user-exec.c +10 -0
index 7bd6e94b8e8..3ebace1e833 100644
@@ -1307,3 +1307,13 @@ void qemu_cpu_kick(CPUState *cpu)
 {
     cpu_exit(cpu);
 }
+
+void cpu_enter_syscall(CPUState *cs)
+{
+    cs->in_syscall = true;
+}
+
+void cpu_exit_syscall(CPUState *cs)
+{
+    cs->in_syscall = false;
+}
bsd-user/freebsd/os-syscall.c +4 -0
index c2849d43223..9f54345e11b 100644
@@ -936,6 +936,8 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
 {
     abi_long ret;
 
+    cpu_enter_syscall(env_cpu(cpu_env));
+
     bql_unlock();
 
     if (do_strace) {
@@ -951,6 +953,8 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
 
     bql_lock();
 
+    cpu_exit_syscall(env_cpu(cpu_env));
+
     return ret;
 }
 
include/exec/cpu-common.h +2 -0
index 4a7b43f9aa3..32bd3cad83f 100644
@@ -228,6 +228,8 @@ G_NORETURN void cpu_loop_exit_restore(CPUState *cpu, uintptr_t pc);
 int cpu_exec(CPUState *cpu);
 /* accel/tcg/user-exec.c */
 int cpu_exec_user(CPUState *cs);
+void cpu_enter_syscall(CPUState *cs);
+void cpu_exit_syscall(CPUState *cs);
 
 /**
  * env_archcpu(env)
include/hw/core/cpu.h +1 -0
index 1c9c775df65..d073a79731b 100644
@@ -475,6 +475,7 @@ struct CPUState {
     bool created;
     bool stop;
     bool stopped;
+    bool in_syscall;
 
     /* Should CPU start in powered-off state? */
     bool start_powered_off;
linux-user/syscall.c +5 -0
index ff34ae11340..344c2e65234 100644
@@ -13771,6 +13771,8 @@ abi_long do_syscall(CPUArchState *cpu_env, int num, abi_long arg1,
     }
 #endif
 
+    cpu_enter_syscall(cpu);
+
     record_syscall_start(cpu, num, arg1,
                          arg2, arg3, arg4, arg5, arg6, arg7, arg8);
 
@@ -13791,5 +13793,8 @@ abi_long do_syscall(CPUArchState *cpu_env, int num, abi_long arg1,
     bql_lock();
 
     record_syscall_return(cpu, num, ret);
+
+    cpu_exit_syscall(cpu);
+
     return ret;
 }
Signature · 1 line
2.46.0
#21 lore raw

PATCH 13/18 cpu: Implement cpu_thread_is_idle() for qemu-user

In reply to Ilya Leoshkevich

Stopped CPUs are parked until cpu_thread_is_idle() is true, so implement it for qemu-user. Share a part of the qemu-system's implementation.

---
 accel/tcg/user-exec.c     | 12 ++++++++++++
 cpu-common.c              | 19 +++++++++++++++++++
 include/exec/cpu-common.h |  3 +++
 include/sysemu/cpus.h     |  1 -
 system/cpus.c             | 17 ++++-------------
 5 files changed, 38 insertions(+), 14 deletions(-)
accel/tcg/user-exec.c +12 -0
index 3ebace1e833..57a13c81fc4 100644
@@ -1317,3 +1317,15 @@ void cpu_exit_syscall(CPUState *cs)
 {
     cs->in_syscall = false;
 }
+
+bool cpu_is_stopped(CPUState *cpu)
+{
+    return cpu->stopped;
+}
+
+bool cpu_thread_is_idle(CPUState *cpu)
+{
+    int ret = cpu_thread_is_idle_common(cpu);
+
+    return ret == -1 ? true : ret;
+}
cpu-common.c +19 -0
index cb7c10a3915..2822ee9373d 100644
@@ -497,3 +497,22 @@ void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
 {
     do_run_on_cpu(cpu, func, data, &bql);
 }
+
+bool cpu_work_list_empty(CPUState *cpu)
+{
+    return QSIMPLEQ_EMPTY_ATOMIC(&cpu->work_list);
+}
+
+int cpu_thread_is_idle_common(CPUState *cpu)
+{
+    if (cpu->stop || !cpu_work_list_empty(cpu)) {
+        return 0;
+    }
+    if (cpu_is_stopped(cpu)) {
+        return 1;
+    }
+    if (!cpu->halted || cpu_has_work(cpu)) {
+        return 0;
+    }
+    return -1;
+}
include/exec/cpu-common.h +3 -0
index 32bd3cad83f..d7fc24bc13d 100644
@@ -231,6 +231,9 @@ int cpu_exec_user(CPUState *cs);
 void cpu_enter_syscall(CPUState *cs);
 void cpu_exit_syscall(CPUState *cs);
 
+int cpu_thread_is_idle_common(CPUState *cpu);
+bool cpu_thread_is_idle(CPUState *cpu);
+
 /**
  * env_archcpu(env)
  * @env: The architecture environment
include/sysemu/cpus.h +0 -1
index b4a566cfe75..bfa3fd45650 100644
@@ -21,7 +21,6 @@ void dummy_start_vcpu_thread(CPUState *);
 
 void cpus_kick_thread(CPUState *cpu);
 bool cpu_work_list_empty(CPUState *cpu);
-bool cpu_thread_is_idle(CPUState *cpu);
 bool all_cpu_threads_idle(void);
 bool cpu_can_run(CPUState *cpu);
 void qemu_wait_io_event_common(CPUState *cpu);
system/cpus.c +4 -13
index fe84b822798..13072be26fa 100644
@@ -75,21 +75,12 @@ bool cpu_is_stopped(CPUState *cpu)
     return cpu->stopped || !runstate_is_running();
 }
 
-bool cpu_work_list_empty(CPUState *cpu)
-{
-    return QSIMPLEQ_EMPTY_ATOMIC(&cpu->work_list);
-}
-
 bool cpu_thread_is_idle(CPUState *cpu)
 {
-    if (cpu->stop || !cpu_work_list_empty(cpu)) {
-        return false;
-    }
-    if (cpu_is_stopped(cpu)) {
-        return true;
-    }
-    if (!cpu->halted || cpu_has_work(cpu)) {
-        return false;
+    int ret = cpu_thread_is_idle_common(cpu);
+
+    if (ret != -1) {
+        return ret;
     }
     if (cpus_accel->cpu_thread_is_idle) {
         return cpus_accel->cpu_thread_is_idle(cpu);
Signature · 1 line
2.46.0
#22 lore raw

PATCH 14/18 cpu: Introduce cpu_is_paused()

In reply to Ilya Leoshkevich

A qemu-system CPU is considered paused as a result of an external request. A qemu-user CPU, in addition to that, should be considered paused when it's executing a syscall.

---
 accel/tcg/user-exec.c     | 5 +++++
 include/exec/cpu-common.h | 1 +
 system/cpus.c             | 7 ++++++-
 3 files changed, 12 insertions(+), 1 deletion(-)
accel/tcg/user-exec.c +5 -0
index 57a13c81fc4..de4753cded7 100644
@@ -1329,3 +1329,8 @@ bool cpu_thread_is_idle(CPUState *cpu)
 
     return ret == -1 ? true : ret;
 }
+
+bool cpu_is_paused(CPUState *cpu)
+{
+    return cpu->stopped || cpu->in_syscall;
+}
include/exec/cpu-common.h +1 -0
index d7fc24bc13d..e8b530ed889 100644
@@ -233,6 +233,7 @@ void cpu_exit_syscall(CPUState *cs);
 
 int cpu_thread_is_idle_common(CPUState *cpu);
 bool cpu_thread_is_idle(CPUState *cpu);
+bool cpu_is_paused(CPUState *cpu);
 
 /**
  * env_archcpu(env)
system/cpus.c +6 -1
index 13072be26fa..407140c41f6 100644
@@ -530,12 +530,17 @@ void cpu_resume(CPUState *cpu)
     qemu_cpu_kick(cpu);
 }
 
+bool cpu_is_paused(CPUState *cpu)
+{
+    return cpu->stopped;
+}
+
 static bool all_vcpus_paused(void)
 {
     CPUState *cpu;
 
     CPU_FOREACH(cpu) {
-        if (!cpu->stopped) {
+        if (!cpu_is_paused(cpu)) {
             return false;
         }
     }
Signature · 1 line
2.46.0
#23 lore raw

PATCH 15/18 cpu: Set current_cpu early in qemu-user

In reply to Ilya Leoshkevich

qemu_plugin_get_registers() may be called before cpu_exec(), and it requires current_cpu.

---
 bsd-user/main.c      | 1 +
 linux-user/main.c    | 1 +
 linux-user/syscall.c | 1 +
 3 files changed, 3 insertions(+)
bsd-user/main.c +1 -0
index b424a21f643..fb70aadbcee 100644
@@ -617,6 +617,7 @@ int main(int argc, char **argv)
         gdbserver_start(gdbstub);
         gdb_handlesig(cpu, 0, NULL, NULL, 0);
     }
+    current_cpu = cpu;
     cpu_loop(env);
     /* never exits */
     return 0;
linux-user/main.c +1 -0
index 60091cf3053..4a794445d72 100644
@@ -1022,6 +1022,7 @@ int main(int argc, char **argv, char **envp)
     qemu_semihosting_guestfd_init();
 #endif
 
+    current_cpu = cpu;
     cpu_loop(env);
     /* never exits */
     return 0;
linux-user/syscall.c +1 -0
index 344c2e65234..46a8ba7098c 100644
@@ -6531,6 +6531,7 @@ static void *clone_func(void *arg)
     pthread_mutex_lock(&clone_lock);
     pthread_mutex_unlock(&clone_lock);
     bql_lock();
+    current_cpu = cpu;
     cpu_loop(env);
     /* never exits */
     return NULL;
Signature · 1 line
2.46.0
#24 lore raw

PATCH 16/18 cpu: Allow pausing and resuming CPUs in qemu-user

In reply to Ilya Leoshkevich

Move the respective functions from sysemu to cpu-common.

---
 accel/tcg/user-exec-stub.c |   4 --
 accel/tcg/user-exec.c      |  11 +++-
 cpu-common.c               | 115 +++++++++++++++++++++++++++++++++++++
 include/exec/cpu-common.h  |   7 +++
 include/sysemu/cpus.h      |   5 --
 system/cpus.c              | 108 ----------------------------------
 6 files changed, 132 insertions(+), 118 deletions(-)
accel/tcg/user-exec-stub.c +0 -4
index 4fbe2dbdc88..e79f2e88498 100644
@@ -2,10 +2,6 @@
 #include "hw/core/cpu.h"
 #include "exec/replay-core.h"
 
-void cpu_resume(CPUState *cpu)
-{
-}
-
 void cpu_remove_sync(CPUState *cpu)
 {
 }
accel/tcg/user-exec.c +10 -1
index de4753cded7..3399b074485 100644
@@ -1293,12 +1293,19 @@ int cpu_exec_user(CPUState *cs)
 {
     int trapnr;
 
+    do {
+        qemu_wait_io_event(cs);
+    } while (!cpu_can_run(cs));
+
     bql_unlock();
     cpu_exec_start(cs);
     trapnr = cpu_exec(cs);
     cpu_exec_end(cs);
     bql_lock();
-    process_queued_cpu_work(cs);
+
+    do {
+        qemu_wait_io_event(cs);
+    } while (!cpu_can_run(cs));
 
     return trapnr;
 }
@@ -1306,11 +1313,13 @@ int cpu_exec_user(CPUState *cs)
 void qemu_cpu_kick(CPUState *cpu)
 {
     cpu_exit(cpu);
+    qemu_cond_broadcast(cpu->halt_cond);
 }
 
 void cpu_enter_syscall(CPUState *cs)
 {
     cs->in_syscall = true;
+    qemu_pause_cond_broadcast();
 }
 
 void cpu_exit_syscall(CPUState *cs)
cpu-common.c +115 -0

136 lines not on this page · raw message

include/exec/cpu-common.h +7 -0
index e8b530ed889..a54368c5b69 100644
@@ -234,6 +234,13 @@ void cpu_exit_syscall(CPUState *cs);
 int cpu_thread_is_idle_common(CPUState *cpu);
 bool cpu_thread_is_idle(CPUState *cpu);
 bool cpu_is_paused(CPUState *cpu);
+bool cpu_can_run(CPUState *cpu);
+void qemu_wait_io_event_common(CPUState *cpu);
+void qemu_wait_io_event(CPUState *cpu);
+void pause_all_vcpus(void);
+void qemu_pause_cond_broadcast(void);
+bool cpu_work_list_empty(CPUState *cpu);
+
 
 /**
  * env_archcpu(env)
include/sysemu/cpus.h +0 -5
index bfa3fd45650..ebfd0b77bcd 100644
@@ -20,11 +20,7 @@ void dummy_start_vcpu_thread(CPUState *);
 #define VCPU_THREAD_NAME_SIZE 16
 
 void cpus_kick_thread(CPUState *cpu);
-bool cpu_work_list_empty(CPUState *cpu);
 bool all_cpu_threads_idle(void);
-bool cpu_can_run(CPUState *cpu);
-void qemu_wait_io_event_common(CPUState *cpu);
-void qemu_wait_io_event(CPUState *cpu);
 void cpu_thread_signal_created(CPUState *cpu);
 void cpu_thread_signal_destroyed(CPUState *cpu);
 void cpu_handle_guest_debug(CPUState *cpu);
@@ -34,7 +30,6 @@ void cpu_handle_guest_debug(CPUState *cpu);
 bool qemu_in_vcpu_thread(void);
 void qemu_init_cpu_loop(void);
 void resume_all_vcpus(void);
-void pause_all_vcpus(void);
 void cpu_stop_current(void);
 
 extern int icount_align_option;
system/cpus.c +0 -108

140 lines not on this page · raw message

Signature · 1 line
2.46.0
#25 lore raw

PATCH 17/18 gdbstub: Pause all CPUs before sending stop replies

In reply to Ilya Leoshkevich

This is required by the GDB remote protocol.

---
 gdbstub/gdbstub.c |  2 ++
 gdbstub/user.c    | 10 +++++++++-
 2 files changed, 11 insertions(+), 1 deletion(-)
gdbstub/gdbstub.c +2 -0
index a096104b07a..be632f8b214 100644
@@ -2497,6 +2497,8 @@ bool gdb_try_stop(void)
         return false;
     }
 
+    pause_all_vcpus();
+
     gdbserver_state.allow_stop_reply = false;
     return true;
 }
gdbstub/user.c +9 -1
index 82007b09db6..3095c846a99 100644
@@ -458,6 +458,7 @@ static void disable_gdbstub(CPUState *thread_cpu)
         cpu_breakpoint_remove_all(cpu, BP_GDB);
         /* no cpu_watchpoint_remove_all for user-mode */
         cpu_single_step(cpu, 0);
+        cpu_resume(cpu);
     }
     tb_flush(thread_cpu);
 }
@@ -650,9 +651,16 @@ int gdb_continue_partial(char *newstates)
      * previous situation, where only one CPU would be single-stepped.
      */
     CPU_FOREACH(cpu) {
-        if (newstates[cpu->cpu_index] == 's') {
+        switch (newstates[cpu->cpu_index]) {
+        case 's':
             trace_gdbstub_op_stepping(cpu->cpu_index);
             cpu_single_step(cpu, gdbserver_state.sstep_flags);
+            QEMU_FALLTHROUGH;
+        case 'c':
+        case 'C':
+        case 'S':
+            cpu_resume(cpu);
+            break;
         }
     }
     gdbserver_user_state.running_state = 1;
Signature · 1 line
2.46.0
#26 lore raw

PATCH 18/18 tests/tcg: Stress test thread breakpoints

In reply to Ilya Leoshkevich

Add a test to prevent regressions.

---
 tests/tcg/multiarch/Makefile.target           | 13 ++-
 .../gdbstub/test-thread-breakpoint-stress.py  | 28 ++++++
 .../tcg/multiarch/thread-breakpoint-stress.c  | 92 +++++++++++++++++++
 3 files changed, 132 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/multiarch/gdbstub/test-thread-breakpoint-stress.py
 create mode 100644 tests/tcg/multiarch/thread-breakpoint-stress.c
tests/tcg/multiarch/Makefile.target +12 -1
index 5e3391ec9d2..0a9cd037094 100644
@@ -42,6 +42,9 @@ munmap-pthread: LDFLAGS+=-pthread
 vma-pthread: CFLAGS+=-pthread
 vma-pthread: LDFLAGS+=-pthread
 
+thread-breakpoint-stress: CFLAGS+=-pthread
+thread-breakpoint-stress: LDFLAGS+=-pthread
+
 # The vma-pthread seems very sensitive on gitlab and we currently
 # don't know if its exposing a real bug or the test is flaky.
 ifneq ($(GITLAB_CI),)
@@ -127,6 +130,13 @@ run-gdbstub-follow-fork-mode-parent: follow-fork-mode
 		--bin $< --test $(MULTIARCH_SRC)/gdbstub/follow-fork-mode-parent.py, \
 	following parents on fork)
 
+run-gdbstub-thread-breakpoint-stress: thread-breakpoint-stress
+	$(call run-test, $@, $(GDB_SCRIPT) \
+		--gdb $(GDB) \
+		--qemu $(QEMU) --qargs "$(QEMU_OPTS)" \
+		--bin $< --test $(MULTIARCH_SRC)/gdbstub/test-thread-breakpoint-stress.py, \
+	hitting many breakpoints on different threads)
+
 else
 run-gdbstub-%:
 	$(call skip-test, "gdbstub test $*", "need working gdb with $(patsubst -%,,$(TARGET_NAME)) support")
@@ -136,7 +146,8 @@ EXTRA_RUNS += run-gdbstub-sha1 run-gdbstub-qxfer-auxv-read \
 	      run-gdbstub-registers run-gdbstub-prot-none \
 	      run-gdbstub-catch-syscalls run-gdbstub-follow-fork-mode-child \
 	      run-gdbstub-follow-fork-mode-parent \
-	      run-gdbstub-qxfer-siginfo-read
+	      run-gdbstub-qxfer-siginfo-read \
+	      run-gdbstub-thread-breakpoint-stress
 
 # ARM Compatible Semi Hosting Tests
 #
tests/tcg/multiarch/gdbstub/test-thread-breakpoint-stress.py new file +28 -0
new file mode 100644
index 00000000000..489d238f02d
@@ -0,0 +1,28 @@
+"""Test multiple threads hitting breakpoints.
+
+SPDX-License-Identifier: GPL-2.0-or-later
+"""
+from test_gdbstub import main, report
+
+
+N_BREAK_THREADS = 2
+N_BREAKS = 100
+
+
+def run_test():
+    """Run through the tests one by one"""
+    if gdb.selected_inferior().architecture().name() == "MicroBlaze":
+        print("SKIP: Atomics are broken on MicroBlaze")
+        exit(0)
+    gdb.execute("break break_here")
+    gdb.execute("continue")
+    for _ in range(N_BREAK_THREADS * N_BREAKS):
+        counter1 = int(gdb.parse_and_eval("s->counter"))
+        counter2 = int(gdb.parse_and_eval("s->counter"))
+        report(counter1 == counter2, "{} == {}".format(counter1, counter2))
+        gdb.execute("continue")
+    exitcode = int(gdb.parse_and_eval("$_exitcode"))
+    report(exitcode == 0, "{} == 0".format(exitcode))
+
+
+main(run_test)
tests/tcg/multiarch/thread-breakpoint-stress.c new file +92 -0

92 lines not on this page · raw message

Signature · 1 line
2.46.0
#27 lore raw
In reply to Ilya Leoshkevich
Quoted 88 lines · Ilya Leoshkevich

Hi,

On reporting a breakpoint in a non-non-stop mode, GDB remotes must stop
all threads. Currently qemu-user doesn't do that, breaking the debugging session for at least two reasons: concurrent access to the GDB socket, and an assertion within GDB [1].

This series fixes this by importing pause_all_vcpus() from qemu- system.
This in turn requires introducing BQL and a few stubs to qemu-user.

Best regards,
Ilya

[1] https://gitlab.com/qemu-project/qemu/-/issues/2465

Ilya Leoshkevich (18):
  gdbstub: Make gdb_get_char() static
  gdbstub: Move phy_memory_mode to GDBSystemState
  gdbstub: Move gdb_syscall_mode to GDBSyscallState
  gdbstub: Factor out gdb_try_stop()
  accel/tcg: Factor out cpu_exec_user()
  qemu-thread: Introduce QEMU_MUTEX_INITIALIZER
  qemu-thread: Introduce QEMU_COND_INITIALIZER
  replay: Add replay_mutex_{lock,unlock}() stubs for qemu-user
  qemu-timer: Provide qemu_clock_enable() stub for qemu-user
  cpu: Use BQL in qemu-user
  accel/tcg: Unify user implementations of qemu_cpu_kick()
  cpu: Track CPUs executing syscalls
  cpu: Implement cpu_thread_is_idle() for qemu-user
  cpu: Introduce cpu_is_paused()
  cpu: Set current_cpu early in qemu-user
  cpu: Allow pausing and resuming CPUs in qemu-user
  gdbstub: Pause all CPUs before sending stop replies tests/tcg: Stress test thread breakpoints

 accel/tcg/user-exec-stub.c                    |   4 - accel/tcg/user-exec.c                         |  55 ++++++ bsd-user/aarch64/target_arch_cpu.h            |   6 +- bsd-user/arm/target_arch_cpu.h                |   5 +- bsd-user/freebsd/os-syscall.c                 |  10 + bsd-user/i386/target_arch_cpu.h               |   5 +- bsd-user/main.c                               |   8 +- bsd-user/x86_64/target_arch_cpu.h             |   5 +- cpu-common.c                                  | 179
++++++++++++++++++
 gdbstub/gdbstub.c                             |  17 +- gdbstub/internals.h                           |   4 +- gdbstub/syscalls.c                            |  20 +- gdbstub/system.c                              |  18 +- gdbstub/user.c                                |  28 ++- include/exec/cpu-common.h                     |  15 ++ include/exec/replay-core.h                    |  13 ++ include/hw/core/cpu.h                         |   1 + include/qemu/thread-posix.h                   |   8 + include/qemu/thread-win32.h                   |   8 + include/sysemu/cpus.h                         |   6 - include/sysemu/replay.h                       |  13 -- linux-user/aarch64/cpu_loop.c                 |   5 +- linux-user/alpha/cpu_loop.c                   |   5 +- linux-user/arm/cpu_loop.c                     |   5 +- linux-user/hexagon/cpu_loop.c                 |   5 +- linux-user/hppa/cpu_loop.c                    |   5 +- linux-user/i386/cpu_loop.c                    |   5 +- linux-user/loongarch64/cpu_loop.c             |   5 +- linux-user/m68k/cpu_loop.c                    |   5 +- linux-user/main.c                             |   9 +- linux-user/microblaze/cpu_loop.c              |   5 +- linux-user/mips/cpu_loop.c                    |   5 +- linux-user/openrisc/cpu_loop.c                |   5 +- linux-user/ppc/cpu_loop.c                     |   5 +- linux-user/riscv/cpu_loop.c                   |   5 +- linux-user/s390x/cpu_loop.c                   |   5 +- linux-user/sh4/cpu_loop.c                     |   5 +- linux-user/sparc/cpu_loop.c                   |   5 +- linux-user/syscall.c                          |  12 ++ linux-user/xtensa/cpu_loop.c                  |   5 +- replay/stubs-system.c                         |   8 + stubs/meson.build                             |   8 + stubs/qemu-timer.c                            |   6 + stubs/replay-mutex.c                          |  10 + stubs/replay-tools.c                          |   8 - system/cpus.c                                 | 172 +--------------- -
 tests/tcg/multiarch/Makefile.target           |  13 +- .../gdbstub/test-thread-breakpoint-stress.py  |  28 +++ .../tcg/multiarch/thread-breakpoint-stress.c  |  92 +++++++++

Quoted 6 lines
 49 files changed, 552 insertions(+), 327 deletions(-)

 create mode 100644 stubs/qemu-timer.c
 create mode 100644 stubs/replay-mutex.c
 create mode 100644 tests/tcg/multiarch/gdbstub/test-thread- breakpoint-stress.py
 create mode 100644 tests/tcg/multiarch/thread-breakpoint-stress.c

Correction: the subject should have "qemu-user" instead of "qemu-cpu".

#28 lore raw
In reply to Ilya Leoshkevich
Quoted 9 lines · Ilya Leoshkevich

Hi,

On reporting a breakpoint in a non-non-stop mode, GDB remotes must stop all threads. Currently qemu-user doesn't do that, breaking the debugging session for at least two reasons: concurrent access to the GDB socket, and an assertion within GDB [1].

This series fixes this by importing pause_all_vcpus() from qemu-system. This in turn requires introducing BQL and a few stubs to qemu-user.

I would have expected you to reuse (some portion of) start_exclusive, which is already part of qemu-user. Is there a reason you chose a solution which requires...

Quoted 3 lines

replay: Add replay_mutex_{lock,unlock}() stubs for qemu-user
qemu-timer: Provide qemu_clock_enable() stub for qemu-user
cpu: Use BQL in qemu-user

all sorts of other infrastructure?

r~

#29 lore raw
Quoted 19 lines · Richard Henderson
Quoted 9 lines · Ilya Leoshkevich

Hi,

On reporting a breakpoint in a non-non-stop mode, GDB remotes must stop
all threads. Currently qemu-user doesn't do that, breaking the debugging session for at least two reasons: concurrent access to the
GDB socket, and an assertion within GDB [1].

Quoted 3 lines

This series fixes this by importing pause_all_vcpus() from qemu- system.
This in turn requires introducing BQL and a few stubs to qemu-user.

I would have expected you to reuse (some portion of) start_exclusive, which is already
part of qemu-user.  Is there a reason you chose a solution which requires...

Quoted 8 lines
Quoted 3 lines

   replay: Add replay_mutex_{lock,unlock}() stubs for qemu-user
   qemu-timer: Provide qemu_clock_enable() stub for qemu-user
   cpu: Use BQL in qemu-user

all sorts of other infrastructure?

r~

I don't think start_exclusive() would protect the gdb socket from concurrent accesses (e.g., if two threads are simultaneously stopped).

I have a patch [1] that introduces a big gdbstub lock for that, but it looks more complex than just extending BQL to qemu-user. Also, the BQL-based pause/resume code already works for the system mode and is well tested.

[1]
https://gitlab.com/iii-i/qemu/-/commit/0944716218820f8bdfdcf80acc6c39a48b91670c

#30 lore raw
Quoted 24 lines · Ilya Leoshkevich
Quoted 23 lines · Richard Henderson
Quoted 9 lines · Ilya Leoshkevich

Hi,

On reporting a breakpoint in a non-non-stop mode, GDB remotes must stop
all threads. Currently qemu-user doesn't do that, breaking the debugging session for at least two reasons: concurrent access to the
GDB socket, and an assertion within GDB [1].

Quoted 3 lines

This series fixes this by importing pause_all_vcpus() from qemu- system.
This in turn requires introducing BQL and a few stubs to qemu-user.

I would have expected you to reuse (some portion of) start_exclusive, which is already
part of qemu-user.  Is there a reason you chose a solution which requires...

Quoted 3 lines

   replay: Add replay_mutex_{lock,unlock}() stubs for qemu-user
   qemu-timer: Provide qemu_clock_enable() stub for qemu-user
   cpu: Use BQL in qemu-user

Quoted 7 lines
Quoted 4 lines

all sorts of other infrastructure?

r~

I don't think start_exclusive() would protect the gdb socket from concurrent accesses (e.g., if two threads are simultaneously stopped).

Of course it would, otherwise "exclusive" has no meaning. All other cpus are blocked in exclusive_idle().

Importantly, no cpus are blocked in syscalls, where the kernel can modify memory behind gdbstub's back (e.g. read). I think considering "in_syscall" to be "paused" a mistake.

r~

#31 lore raw
Quoted 43 lines · Richard Henderson
Quoted 30 lines · Ilya Leoshkevich
Quoted 29 lines · Richard Henderson
Quoted 11 lines · Ilya Leoshkevich

Hi,

On reporting a breakpoint in a non-non-stop mode, GDB remotes must
stop
all threads. Currently qemu-user doesn't do that, breaking the debugging session for at least two reasons: concurrent access to
the
GDB socket, and an assertion within GDB [1].

Quoted 5 lines

This series fixes this by importing pause_all_vcpus() from qemu-
system.
This in turn requires introducing BQL and a few stubs to qemu- user.

I would have expected you to reuse (some portion of) start_exclusive,
which is already
part of qemu-user.  Is there a reason you chose a solution which requires...

Quoted 4 lines

    replay: Add replay_mutex_{lock,unlock}() stubs for qemu- user
    qemu-timer: Provide qemu_clock_enable() stub for qemu-user
    cpu: Use BQL in qemu-user

Quoted 8 lines
Quoted 4 lines

all sorts of other infrastructure?

r~

I don't think start_exclusive() would protect the gdb socket from concurrent accesses (e.g., if two threads are simultaneously stopped).

Of course it would, otherwise "exclusive" has no meaning. All other cpus are blocked in exclusive_idle().

Quoted 7 lines

Importantly, no cpus are blocked in syscalls, where the kernel can modify memory behind
gdbstub's back (e.g. read).  I think considering "in_syscall" to be "paused" a mistake.

r~

How can we handle the long-running syscalls?
Just waiting sounds unsatisfying.
Sending a reserved host signal may alter the guest's behaviour if a syscall like pause() is interrupted.
What do you think about SIGSTOP-ping the "in_syscall" threads? A quick experiment shows that it should be completely invisible to the guest - the following program continues to run after SIGSTOP/SIGCONT:

#include <sys/syscall.h>
#include <unistd.h>
int main(void) { syscall(__NR_pause); };

#32 lore raw
Quoted 65 lines · Ilya Leoshkevich
Quoted 47 lines · Richard Henderson
Quoted 34 lines · Ilya Leoshkevich
Quoted 33 lines · Richard Henderson
Quoted 12 lines · Ilya Leoshkevich

Hi,

On reporting a breakpoint in a non-non-stop mode, GDB remotes must
stop
all threads. Currently qemu-user doesn't do that, breaking the
debugging session for at least two reasons: concurrent access to
the
GDB socket, and an assertion within GDB [1].

Quoted 6 lines

This series fixes this by importing pause_all_vcpus() from qemu-
system.
This in turn requires introducing BQL and a few stubs to qemu-
user.

I would have expected you to reuse (some portion of) start_exclusive,
which is already
part of qemu-user.  Is there a reason you chose a solution which
requires...

Quoted 5 lines

    replay: Add replay_mutex_{lock,unlock}() stubs for qemu- user
    qemu-timer: Provide qemu_clock_enable() stub for qemu- user
    cpu: Use BQL in qemu-user

Quoted 8 lines
Quoted 4 lines

all sorts of other infrastructure?

r~

I don't think start_exclusive() would protect the gdb socket from concurrent accesses (e.g., if two threads are simultaneously stopped).

Of course it would, otherwise "exclusive" has no meaning. All other cpus are blocked in exclusive_idle().

Quoted 7 lines

Importantly, no cpus are blocked in syscalls, where the kernel can modify memory behind
gdbstub's back (e.g. read).  I think considering "in_syscall" to be "paused" a mistake.

r~

How can we handle the long-running syscalls?
Just waiting sounds unsatisfying.
Sending a reserved host signal may alter the guest's behaviour if a syscall like pause() is interrupted.
What do you think about SIGSTOP-ping the "in_syscall" threads? A quick experiment shows that it should be completely invisible to the
guest - the following program continues to run after SIGSTOP/SIGCONT:

Quoted 3 lines

#include <sys/syscall.h>
#include <unistd.h>
int main(void) { syscall(__NR_pause); };

Hmm, no, that won't work: SIGSTOP would stop all threads.

So I wonder if reserving a host signal for interrupting "in_syscall" threads would be an acceptable tradeoff?

#33 lore raw
Quoted 9 lines · Ilya Leoshkevich
Quoted 9 lines

How can we handle the long-running syscalls?
Just waiting sounds unsatisfying.
Sending a reserved host signal may alter the guest's behaviour if a syscall like pause() is interrupted.
What do you think about SIGSTOP-ping the "in_syscall" threads? A quick experiment shows that it should be completely invisible to the
guest - the following program continues to run after SIGSTOP/SIGCONT:

Quoted 8 lines
Quoted 3 lines

#include <sys/syscall.h>
#include <unistd.h>
int main(void) { syscall(__NR_pause); };

Hmm, no, that won't work: SIGSTOP would stop all threads.

So I wonder if reserving a host signal for interrupting "in_syscall" threads would be an acceptable tradeoff?

Could work, yes. We already steal SIGRTMIN for guest abort (to distinguish from host abort), and remap guest __SIGRTMIN to host SIGRTMIN+1. Grabbing SIGRTMIN+1 should work ok, modulo the existing problem of presenting the guest with an incomplete set of signals.

I've wondered from time to time about multiplexing signals in this space, but I think that runs afoul of having a consistent mapping for interprocess signaling.

r~

#34 lore raw
Quoted 30 lines · Richard Henderson
Quoted 16 lines · Ilya Leoshkevich
Quoted 12 lines

How can we handle the long-running syscalls?
Just waiting sounds unsatisfying.
Sending a reserved host signal may alter the guest's behaviour if a
syscall like pause() is interrupted.
What do you think about SIGSTOP-ping the "in_syscall" threads? A quick experiment shows that it should be completely invisible to
the
guest - the following program continues to run after SIGSTOP/SIGCONT:

Quoted 3 lines

#include <sys/syscall.h>
#include <unistd.h>
int main(void) { syscall(__NR_pause); };

Quoted 5 lines

Hmm, no, that won't work: SIGSTOP would stop all threads.

So I wonder if reserving a host signal for interrupting "in_syscall"
threads would be an acceptable tradeoff?

Could work, yes.  We already steal SIGRTMIN for guest abort (to distinguish from host
abort), and remap guest __SIGRTMIN to host SIGRTMIN+1.  Grabbing SIGRTMIN+1 should work
ok, modulo the existing problem of presenting the guest with an incomplete set of signals.

Quoted 6 lines

I've wondered from time to time about multiplexing signals in this space, but I think that
runs afoul of having a consistent mapping for interprocess signaling.

r~

I tried to think through how this would work in conjunction with start_exclusive(), and there is one problem I don't see a good solution for. Maybe you will have an idea.

The way I'm thinking of implementing this is as follows:

- Reserve the host's SIGRTMIN+1 and tweak host_signal_handler() to do nothing for this signal.

- In gdb_try_stop(), call start_exclusive(). After it returns, some threads will be parked in exclusive_idle(). Some other threads will be on their way to getting parked, and this needs to actually happen before gdb_try_stop() can proceed. For example, the ones that are executing handle_pending_signal() may change memory and CPU state. IIUC start_exclusive() will not wait for them, because they are not "running". I think a global counter protected by qemu_cpu_list_lock and paired with a new condition variable should be enough for this.

- Threads executing long-running syscalls will need to be interrupted by SIGRTMIN+1. These syscalls will return -EINTR and will need to be manually restarted so as not to disturb poorly written guests. This needs to happen only if there are no pending guest signals.

- Here is a minor problem: how to identify threads which need to be signalled? in_syscall may not be enough. But maybe signalling all threads won't hurt too much. The parked ones won't notice anyway.

- But here is the major problem: what if we signal a thread just before it starts executing a long-running syscall? Such thread will be stuck and we'll need to signal it again. But how to determine that this needs to be done?

An obvious solution is to signal all threads in a loop with a 0.1s delay until the counter reaches n_threads. But it's quite ugly.

Ideally SIGRTMIN+1 should be blocked most of the time. Then we should identify all places where long-running syscalls may be invoked and unblock SIGRTMIN+1 atomically with executing them. But I'm not aware of such mechanism (I have an extremely vague recollection that someone managed to abuse rseq for this, but we shouldn't be relying on rseq being available anyway).

#35 lore raw
In reply to Ilya Leoshkevich
Quoted 9 lines · Ilya Leoshkevich

Hi,

On reporting a breakpoint in a non-non-stop mode, GDB remotes must stop all threads. Currently qemu-user doesn't do that, breaking the debugging session for at least two reasons: concurrent access to the GDB socket, and an assertion within GDB [1].

This series fixes this by importing pause_all_vcpus() from qemu-system. This in turn requires introducing BQL and a few stubs to qemu-user.

Is there a conclusion to this design choice? I'd like to avoid bringing in a bunch of system-mode infrastructure if the existing exclusive code would work. For that I'll defer to the linux-user maintainer or Richard who knows the code better than I do.

I could certainly harvest the early clean-up patches to keep the delta low while the details are worked out. Is there going to be a v2?

Quoted 80 lines

Best regards,
Ilya

[1] https://gitlab.com/qemu-project/qemu/-/issues/2465

Ilya Leoshkevich (18):
gdbstub: Make gdb_get_char() static
gdbstub: Move phy_memory_mode to GDBSystemState
gdbstub: Move gdb_syscall_mode to GDBSyscallState
gdbstub: Factor out gdb_try_stop()
accel/tcg: Factor out cpu_exec_user()
qemu-thread: Introduce QEMU_MUTEX_INITIALIZER
qemu-thread: Introduce QEMU_COND_INITIALIZER
replay: Add replay_mutex_{lock,unlock}() stubs for qemu-user
qemu-timer: Provide qemu_clock_enable() stub for qemu-user
cpu: Use BQL in qemu-user
accel/tcg: Unify user implementations of qemu_cpu_kick()
cpu: Track CPUs executing syscalls
cpu: Implement cpu_thread_is_idle() for qemu-user
cpu: Introduce cpu_is_paused()
cpu: Set current_cpu early in qemu-user
cpu: Allow pausing and resuming CPUs in qemu-user
gdbstub: Pause all CPUs before sending stop replies tests/tcg: Stress test thread breakpoints

 accel/tcg/user-exec-stub.c                    |   4 -
 accel/tcg/user-exec.c                         |  55 ++++++
 bsd-user/aarch64/target_arch_cpu.h            |   6 +-
 bsd-user/arm/target_arch_cpu.h                |   5 +-
 bsd-user/freebsd/os-syscall.c                 |  10 +
 bsd-user/i386/target_arch_cpu.h               |   5 +-
 bsd-user/main.c                               |   8 +-
 bsd-user/x86_64/target_arch_cpu.h             |   5 +-
 cpu-common.c                                  | 179 ++++++++++++++++++
 gdbstub/gdbstub.c                             |  17 +-
 gdbstub/internals.h                           |   4 +-
 gdbstub/syscalls.c                            |  20 +-
 gdbstub/system.c                              |  18 +-
 gdbstub/user.c                                |  28 ++-
 include/exec/cpu-common.h                     |  15 ++
 include/exec/replay-core.h                    |  13 ++
 include/hw/core/cpu.h                         |   1 +
 include/qemu/thread-posix.h                   |   8 +
 include/qemu/thread-win32.h                   |   8 +
 include/sysemu/cpus.h                         |   6 -
 include/sysemu/replay.h                       |  13 --
 linux-user/aarch64/cpu_loop.c                 |   5 +-
 linux-user/alpha/cpu_loop.c                   |   5 +-
 linux-user/arm/cpu_loop.c                     |   5 +-
 linux-user/hexagon/cpu_loop.c                 |   5 +-
 linux-user/hppa/cpu_loop.c                    |   5 +-
 linux-user/i386/cpu_loop.c                    |   5 +-
 linux-user/loongarch64/cpu_loop.c             |   5 +-
 linux-user/m68k/cpu_loop.c                    |   5 +-
 linux-user/main.c                             |   9 +-
 linux-user/microblaze/cpu_loop.c              |   5 +-
 linux-user/mips/cpu_loop.c                    |   5 +-
 linux-user/openrisc/cpu_loop.c                |   5 +-
 linux-user/ppc/cpu_loop.c                     |   5 +-
 linux-user/riscv/cpu_loop.c                   |   5 +-
 linux-user/s390x/cpu_loop.c                   |   5 +-
 linux-user/sh4/cpu_loop.c                     |   5 +-
 linux-user/sparc/cpu_loop.c                   |   5 +-
 linux-user/syscall.c                          |  12 ++
 linux-user/xtensa/cpu_loop.c                  |   5 +-
 replay/stubs-system.c                         |   8 +
 stubs/meson.build                             |   8 +
 stubs/qemu-timer.c                            |   6 +
 stubs/replay-mutex.c                          |  10 +
 stubs/replay-tools.c                          |   8 -
 system/cpus.c                                 | 172 +----------------
 tests/tcg/multiarch/Makefile.target           |  13 +-
 .../gdbstub/test-thread-breakpoint-stress.py  |  28 +++
 .../tcg/multiarch/thread-breakpoint-stress.c  |  92 +++++++++
 49 files changed, 552 insertions(+), 327 deletions(-)
 create mode 100644 stubs/qemu-timer.c
 create mode 100644 stubs/replay-mutex.c
 create mode 100644 tests/tcg/multiarch/gdbstub/test-thread-breakpoint-stress.py
 create mode 100644 tests/tcg/multiarch/thread-breakpoint-stress.c
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#36 lore raw
Quoted 15 lines · Alex Bennée
Quoted 9 lines · Ilya Leoshkevich

Hi,

On reporting a breakpoint in a non-non-stop mode, GDB remotes must stop
all threads. Currently qemu-user doesn't do that, breaking the debugging session for at least two reasons: concurrent access to the
GDB socket, and an assertion within GDB [1].

Quoted 3 lines

This series fixes this by importing pause_all_vcpus() from qemu- system.
This in turn requires introducing BQL and a few stubs to qemu-user.

Quoted 7 lines

Is there a conclusion to this design choice? I'd like to avoid bringing
in a bunch of system-mode infrastructure if the existing exclusive code
would work. For that I'll defer to the linux-user maintainer or Richard
who knows the code better than I do.

I wanted to re-implement parking CPUs using a reserved host signal. I've submitted the foundations for this in [1], and I'm currently waiting for the review.

[1]
https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 4 lines

I could certainly harvest the early clean-up patches to keep the delta
low while the details are worked out. Is there going to be a v2?

It would be great if clean-up patches could be taken separately, since IMHO they make sense on their own. I plan to send a v2 after [1] is integrated.

Quoted 83 lines
Quoted 82 lines

Best regards,
Ilya

[1] https://gitlab.com/qemu-project/qemu/-/issues/2465

Ilya Leoshkevich (18):
  gdbstub: Make gdb_get_char() static
  gdbstub: Move phy_memory_mode to GDBSystemState
  gdbstub: Move gdb_syscall_mode to GDBSyscallState
  gdbstub: Factor out gdb_try_stop()
  accel/tcg: Factor out cpu_exec_user()
  qemu-thread: Introduce QEMU_MUTEX_INITIALIZER
  qemu-thread: Introduce QEMU_COND_INITIALIZER
  replay: Add replay_mutex_{lock,unlock}() stubs for qemu-user
  qemu-timer: Provide qemu_clock_enable() stub for qemu-user
  cpu: Use BQL in qemu-user
  accel/tcg: Unify user implementations of qemu_cpu_kick()
  cpu: Track CPUs executing syscalls
  cpu: Implement cpu_thread_is_idle() for qemu-user
  cpu: Introduce cpu_is_paused()
  cpu: Set current_cpu early in qemu-user
  cpu: Allow pausing and resuming CPUs in qemu-user
  gdbstub: Pause all CPUs before sending stop replies tests/tcg: Stress test thread breakpoints

 accel/tcg/user-exec-stub.c                    |   4 - accel/tcg/user-exec.c                         |  55 ++++++ bsd-user/aarch64/target_arch_cpu.h            |   6 +- bsd-user/arm/target_arch_cpu.h                |   5 +- bsd-user/freebsd/os-syscall.c                 |  10 + bsd-user/i386/target_arch_cpu.h               |   5 +- bsd-user/main.c                               |   8 +- bsd-user/x86_64/target_arch_cpu.h             |   5 +- cpu-common.c                                  | 179
++++++++++++++++++
 gdbstub/gdbstub.c                             |  17 +- gdbstub/internals.h                           |   4 +- gdbstub/syscalls.c                            |  20 +- gdbstub/system.c                              |  18 +- gdbstub/user.c                                |  28 ++- include/exec/cpu-common.h                     |  15 ++ include/exec/replay-core.h                    |  13 ++ include/hw/core/cpu.h                         |   1 + include/qemu/thread-posix.h                   |   8 + include/qemu/thread-win32.h                   |   8 + include/sysemu/cpus.h                         |   6 - include/sysemu/replay.h                       |  13 -- linux-user/aarch64/cpu_loop.c                 |   5 +- linux-user/alpha/cpu_loop.c                   |   5 +- linux-user/arm/cpu_loop.c                     |   5 +- linux-user/hexagon/cpu_loop.c                 |   5 +- linux-user/hppa/cpu_loop.c                    |   5 +- linux-user/i386/cpu_loop.c                    |   5 +- linux-user/loongarch64/cpu_loop.c             |   5 +- linux-user/m68k/cpu_loop.c                    |   5 +- linux-user/main.c                             |   9 +- linux-user/microblaze/cpu_loop.c              |   5 +- linux-user/mips/cpu_loop.c                    |   5 +- linux-user/openrisc/cpu_loop.c                |   5 +- linux-user/ppc/cpu_loop.c                     |   5 +- linux-user/riscv/cpu_loop.c                   |   5 +- linux-user/s390x/cpu_loop.c                   |   5 +- linux-user/sh4/cpu_loop.c                     |   5 +- linux-user/sparc/cpu_loop.c                   |   5 +- linux-user/syscall.c                          |  12 ++ linux-user/xtensa/cpu_loop.c                  |   5 +- replay/stubs-system.c                         |   8 + stubs/meson.build                             |   8 + stubs/qemu-timer.c                            |   6 + stubs/replay-mutex.c                          |  10 + stubs/replay-tools.c                          |   8 - system/cpus.c                                 | 172 +-------------
---
 tests/tcg/multiarch/Makefile.target           |  13 +- .../gdbstub/test-thread-breakpoint-stress.py  |  28 +++ .../tcg/multiarch/thread-breakpoint-stress.c  |  92 +++++++++

 49 files changed, 552 insertions(+), 327 deletions(-)

 create mode 100644 stubs/qemu-timer.c
 create mode 100644 stubs/replay-mutex.c
 create mode 100644 tests/tcg/multiarch/gdbstub/test-thread- breakpoint-stress.py
 create mode 100644 tests/tcg/multiarch/thread-breakpoint-stress.c