* [PATCH 1/3] cpu: Set current_cpu early in qemu-user
2025-01-13 13:36 [PATCH 0/3] gdbstub: Protect gdb_handlesig() with EXCLUSIVE_GUARD() Ilya Leoshkevich
@ 2025-01-13 13:36 ` Ilya Leoshkevich
2025-01-13 13:36 ` [PATCH 2/3] cpu: Introduce EXCLUSIVE_GUARD() Ilya Leoshkevich
2025-01-13 13:36 ` [PATCH 3/3] gdbstub: Protect gdb_handlesig() with EXCLUSIVE_GUARD() Ilya Leoshkevich
2 siblings, 0 replies; 4+ messages in thread
From: Ilya Leoshkevich @ 2025-01-13 13:36 UTC (permalink / raw)
To: Alex Bennée, Richard Henderson, Philippe Mathieu-Daudé
Cc: qemu-devel, Ilya Leoshkevich
gdb_handlesig() uses current_cpu.
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
bsd-user/main.c | 2 ++
linux-user/main.c | 2 ++
linux-user/syscall.c | 1 +
3 files changed, 5 insertions(+)
diff --git a/bsd-user/main.c b/bsd-user/main.c
index 0a5bc578365..aa052e515c9 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -627,6 +627,8 @@ int main(int argc, char **argv)
target_cpu_init(env, regs);
+ current_cpu = cpu;
+
if (gdbstub) {
gdbserver_start(gdbstub);
gdb_handlesig(cpu, 0, NULL, NULL, 0);
diff --git a/linux-user/main.c b/linux-user/main.c
index b97634a32dd..b2bf0688617 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -1022,6 +1022,8 @@ int main(int argc, char **argv, char **envp)
target_cpu_copy_regs(env, regs);
+ current_cpu = cpu;
+
if (gdbstub) {
if (gdbserver_start(gdbstub) < 0) {
fprintf(stderr, "qemu: could not open gdbserver on %s\n",
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 78c7c0b34ef..a101f177632 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -6542,6 +6542,7 @@ static void *clone_func(void *arg)
env = info->env;
cpu = env_cpu(env);
thread_cpu = cpu;
+ current_cpu = cpu;
ts = get_task_state(cpu);
info->tid = sys_gettid();
task_settid(ts);
--
2.47.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH 2/3] cpu: Introduce EXCLUSIVE_GUARD()
2025-01-13 13:36 [PATCH 0/3] gdbstub: Protect gdb_handlesig() with EXCLUSIVE_GUARD() Ilya Leoshkevich
2025-01-13 13:36 ` [PATCH 1/3] cpu: Set current_cpu early in qemu-user Ilya Leoshkevich
@ 2025-01-13 13:36 ` Ilya Leoshkevich
2025-01-13 13:36 ` [PATCH 3/3] gdbstub: Protect gdb_handlesig() with EXCLUSIVE_GUARD() Ilya Leoshkevich
2 siblings, 0 replies; 4+ messages in thread
From: Ilya Leoshkevich @ 2025-01-13 13:36 UTC (permalink / raw)
To: Alex Bennée, Richard Henderson, Philippe Mathieu-Daudé
Cc: qemu-devel, Ilya Leoshkevich
Add a macro that produces a start_exclusive() / end_exclusive() pair.
Useful to guarantee an exit from an exclusive section in large
functions.
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
include/hw/core/cpu.h | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
index c3ca0babcb3..a7d9d6e2b8c 100644
--- a/include/hw/core/cpu.h
+++ b/include/hw/core/cpu.h
@@ -1063,6 +1063,17 @@ void start_exclusive(void);
*/
void end_exclusive(void);
+static inline void exclusive_unlock_guard(int *exclusive_guard G_GNUC_UNUSED)
+{
+ end_exclusive();
+}
+
+#define EXCLUSIVE_GUARD() \
+ int glue(exclusive_guard, __COUNTER__) \
+ G_GNUC_UNUSED \
+ __attribute__((cleanup(exclusive_unlock_guard))) = \
+ (start_exclusive(), 0);
+
/**
* qemu_init_vcpu:
* @cpu: The vCPU to initialize.
--
2.47.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH 3/3] gdbstub: Protect gdb_handlesig() with EXCLUSIVE_GUARD()
2025-01-13 13:36 [PATCH 0/3] gdbstub: Protect gdb_handlesig() with EXCLUSIVE_GUARD() Ilya Leoshkevich
2025-01-13 13:36 ` [PATCH 1/3] cpu: Set current_cpu early in qemu-user Ilya Leoshkevich
2025-01-13 13:36 ` [PATCH 2/3] cpu: Introduce EXCLUSIVE_GUARD() Ilya Leoshkevich
@ 2025-01-13 13:36 ` Ilya Leoshkevich
2 siblings, 0 replies; 4+ messages in thread
From: Ilya Leoshkevich @ 2025-01-13 13:36 UTC (permalink / raw)
To: Alex Bennée, Richard Henderson, Philippe Mathieu-Daudé
Cc: qemu-devel, Ilya Leoshkevich
If multiple threads hit a breakpoint at the same time, GDB gets
confused [1]. Prevent this situation by stopping the other threads once
a thread hits a breakpoint.
[1] https://sourceware.org/bugzilla/show_bug.cgi?id=32023
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
gdbstub/user.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/gdbstub/user.c b/gdbstub/user.c
index 0b4bfa9c488..d72f8ca6106 100644
--- a/gdbstub/user.c
+++ b/gdbstub/user.c
@@ -200,6 +200,8 @@ int gdb_handlesig(CPUState *cpu, int sig, const char *reason, void *siginfo,
char buf[256];
int n;
+ EXCLUSIVE_GUARD();
+
if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
return sig;
}
--
2.47.1
^ permalink raw reply related [flat|nested] 4+ messages in thread