Stop all qemu-cpu threads on a breakpoint
Subsystems (2)
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
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
@@ -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
@@ -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
Re: [PATCH 01/18] gdbstub: Make gdb_get_char() static
Quoted 8 lines · Ilya Leoshkevich
It's user-only since commit
a7e0f9bd2ace("gdbstub: abstract target specific details fromgdb_put_packet_binary").Signed-off-by Ilya Leoshkevich<[email protected]>--- gdbstub/internals.h | 2 -- gdbstub/user.c | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-)
r~
PATCH 2/18 gdbstub: Move phy_memory_mode to GDBSystemState
In reply to Ilya LeoshkevichFollow 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
@@ -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
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.
Signed-off-by Ilya Leoshkevich <[email protected]>--- gdbstub/system.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)gdbstub/system.c +0
-0
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
-6GDBSystemState 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" ); }
PATCH 3/18 gdbstub: Move gdb_syscall_mode to GDBSyscallState
In reply to Ilya LeoshkevichFollow 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
@@ -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
Re: [PATCH 03/18] gdbstub: Move gdb_syscall_mode to GDBSyscallState
Quoted 7 lines · Ilya Leoshkevich
Follow the convention that all the pieces of the global stub state must be inside a single struct.
Signed-off-by Ilya Leoshkevich<[email protected]>--- gdbstub/syscalls.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
r~
PATCH 4/18 gdbstub: Factor out gdb_try_stop()
In reply to Ilya LeoshkevichMove 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
@@ -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
@@ -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
@@ -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
@@ -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
Re: [PATCH 04/18] gdbstub: Factor out gdb_try_stop()
Quoted 9 lines · Ilya Leoshkevich
Move checking and setting
allow_stop_replyinto a function.Signed-off-by Ilya Leoshkevich<[email protected]>--- gdbstub/gdbstub.c | 15 +++++++++++---- gdbstub/internals.h | 2 ++ gdbstub/system.c | 6 ++---- gdbstub/user.c | 11 ++++------- 4 files changed, 19 insertions(+), 15 deletions(-)
r~
PATCH 5/18 accel/tcg: Factor out cpu_exec_user()
In reply to Ilya LeoshkevichAll 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
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.Signed-off-by Ilya Leoshkevich<[email protected]>--- 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~
PATCH 6/18 qemu-thread: Introduce QEMU_MUTEX_INITIALIZER
In reply to Ilya LeoshkevichAllow 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
@@ -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
@@ -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
Re: [PATCH 06/18] qemu-thread: Introduce QEMU_MUTEX_INITIALIZER
Quoted 7 lines · Ilya Leoshkevich
Allow static initialization of mutexes.
Signed-off-by Ilya Leoshkevich<[email protected]>--- include/qemu/thread-posix.h | 6 ++++++ include/qemu/thread-win32.h | 6 ++++++ 2 files changed, 12 insertions(+)
r~
PATCH 7/18 qemu-thread: Introduce QEMU_COND_INITIALIZER
In reply to Ilya LeoshkevichAllow 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
@@ -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
@@ -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
Re: [PATCH 07/18] qemu-thread: Introduce QEMU_COND_INITIALIZER
Quoted 7 lines · Ilya Leoshkevich
Allow static initialization of condition variables.
Signed-off-by Ilya Leoshkevich<[email protected]>--- include/qemu/thread-posix.h | 2 ++ include/qemu/thread-win32.h | 2 ++ 2 files changed, 4 insertions(+)
r~
PATCH 8/18 replay: Add replay_mutex_{lock, unlock}() stubs for qemu-user
In reply to Ilya LeoshkevichSharing 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH 9/18 qemu-timer: Provide qemu_clock_enable() stub for qemu-user
In reply to Ilya LeoshkevichSharing 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
@@ -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
@@ -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
PATCH 10/18 cpu: Use BQL in qemu-user
In reply to Ilya LeoshkevichCurrently 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH 11/18 accel/tcg: Unify user implementations of qemu_cpu_kick()
In reply to Ilya Leoshkevichlinux-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
@@ -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
@@ -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
@@ -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
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.Signed-off-by Ilya Leoshkevich<[email protected]>--- accel/tcg/user-exec.c | 5 +++++ bsd-user/main.c | 5 ----- linux-user/main.c | 5 ----- 3 files changed, 5 insertions(+), 10 deletions(-)
r~
PATCH 12/18 cpu: Track CPUs executing syscalls
In reply to Ilya LeoshkevichCPUs 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH 13/18 cpu: Implement cpu_thread_is_idle() for qemu-user
In reply to Ilya LeoshkevichStopped 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH 14/18 cpu: Introduce cpu_is_paused()
In reply to Ilya LeoshkevichA 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
@@ -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
@@ -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
@@ -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
PATCH 15/18 cpu: Set current_cpu early in qemu-user
In reply to Ilya Leoshkevichqemu_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
@@ -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
@@ -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
@@ -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
PATCH 16/18 cpu: Allow pausing and resuming CPUs in qemu-user
In reply to Ilya LeoshkevichMove 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
@@ -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
@@ -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
@@ -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
@@ -20,11 +20,7 @@ void dummy_start_vcpu_thread(CPUState *);#define VCPU_THREAD_NAME_SIZE 16void 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
PATCH 17/18 gdbstub: Pause all CPUs before sending stop replies
In reply to Ilya LeoshkevichThis 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
@@ -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
@@ -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
PATCH 18/18 tests/tcg: Stress test thread breakpoints
In reply to Ilya LeoshkevichAdd 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
@@ -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
@@ -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
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: Makegdb_get_char()static
gdbstub: Movephy_memory_modeto GDBSystemState
gdbstub: Movegdb_syscall_modeto GDBSyscallState
gdbstub: Factor outgdb_try_stop()
accel/tcg: Factor outcpu_exec_user()
qemu-thread: IntroduceQEMU_MUTEX_INITIALIZER
qemu-thread: IntroduceQEMU_COND_INITIALIZER
replay: Add replay_mutex_{lock,unlock}() stubs for qemu-user
qemu-timer: Provideqemu_clock_enable()stub for qemu-user
cpu: Use BQL in qemu-user
accel/tcg: Unify user implementations ofqemu_cpu_kick()
cpu: Track CPUs executing syscalls
cpu: Implementcpu_thread_is_idle()for qemu-user
cpu: Introducecpu_is_paused()
cpu: Setcurrent_cpuearly 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 breakpointsaccel/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 100644stubs/replay-mutex.c
create mode 100644 tests/tcg/multiarch/gdbstub/test-thread-breakpoint-stress.py
create mode 100644tests/tcg/multiarch/thread-breakpoint-stress.c
Correction: the subject should have "qemu-user" instead of "qemu-cpu".
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: Provideqemu_clock_enable()stub for qemu-user
cpu: Use BQL in qemu-user
all sorts of other infrastructure?
r~
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: Provideqemu_clock_enable()stub for qemu-user
cpu: Use BQL in qemu-userall 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
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: Provideqemu_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~
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: Provideqemu_clock_enable()stub for qemu-user
cpu: Use BQL in qemu-userQuoted 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); };
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: Provideqemu_clock_enable()stub for qemu- user
cpu: Use BQL in qemu-userQuoted 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 likepause()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?
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 likepause()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~
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 likepause()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__SIGRTMINto 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).
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: Makegdb_get_char()static
gdbstub: Movephy_memory_modeto GDBSystemState
gdbstub: Movegdb_syscall_modeto GDBSyscallState
gdbstub: Factor outgdb_try_stop()
accel/tcg: Factor outcpu_exec_user()
qemu-thread: IntroduceQEMU_MUTEX_INITIALIZER
qemu-thread: IntroduceQEMU_COND_INITIALIZER
replay: Add replay_mutex_{lock,unlock}() stubs for qemu-user
qemu-timer: Provideqemu_clock_enable()stub for qemu-user
cpu: Use BQL in qemu-user
accel/tcg: Unify user implementations ofqemu_cpu_kick()
cpu: Track CPUs executing syscalls
cpu: Implementcpu_thread_is_idle()for qemu-user
cpu: Introducecpu_is_paused()
cpu: Setcurrent_cpuearly 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 breakpointsaccel/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
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: Makegdb_get_char()static
gdbstub: Movephy_memory_modeto GDBSystemState
gdbstub: Movegdb_syscall_modeto GDBSyscallState
gdbstub: Factor outgdb_try_stop()
accel/tcg: Factor outcpu_exec_user()
qemu-thread: IntroduceQEMU_MUTEX_INITIALIZER
qemu-thread: IntroduceQEMU_COND_INITIALIZER
replay: Add replay_mutex_{lock,unlock}() stubs for qemu-user
qemu-timer: Provideqemu_clock_enable()stub for qemu-user
cpu: Use BQL in qemu-user
accel/tcg: Unify user implementations ofqemu_cpu_kick()
cpu: Track CPUs executing syscalls
cpu: Implementcpu_thread_is_idle()for qemu-user
cpu: Introducecpu_is_paused()
cpu: Setcurrent_cpuearly 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 breakpointsaccel/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 100644stubs/replay-mutex.c
create mode 100644 tests/tcg/multiarch/gdbstub/test-thread-breakpoint-stress.py
create mode 100644tests/tcg/multiarch/thread-breakpoint-stress.c