Risc-V/gdb: replace exit(0) with proper shutdown
Subsystems (2)
cover Risc-V/gdb: replace exit(0) with proper shutdown
This serie replaces some of the call to exit(0) in hardware used by Risc-V boards or made when gdb is requested to exit. Otherwise, the gdb connection can be too abruptly disconnected resulting in the last gdb packet "Wxx" being not sent.
As qemu_system_shutdown_request doesn't allow to pass the exit code, only perform the above modification on a sucessful exit.
Clément Chigot (3):
hw/misc/sifive_test.c: replace exit(0) with proper shutdown hw/char: riscv_htif: replace exit(0) with proper shutdown
gdbstub: replace exit(0) with proper shutdown
gdbstub/gdbstub.c | 3 +-- gdbstub/softmmu.c | 11 +++++++++++ gdbstub/user.c | 2 ++ hw/char/riscv_htif.c | 12 +++++++++++- hw/misc/sifive_test.c | 4 +++- 5 files changed, 28 insertions(+), 4 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/3 hw/misc/sifive_test.c: replace exit(0) with proper shutdown
This replaces the exit(0) call by a shutdown request, ensuring a proper cleanup of Qemu. Otherwise, some connections like gdb could be broken without being correctly flushed.
--- hw/misc/sifive_test.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
hw/misc/sifive_test.c +3 -1
@@ -25,6 +25,7 @@#include "qemu/module.h" #include "sysemu/runstate.h" #include "hw/misc/sifive_test.h" +#include "sysemu/sysemu.h" static uint64_t sifive_test_read (void *opaque,hwaddr addr,unsigned int size) {
@@ -41,7 +42,8 @@ static void sifive_test_write(void *opaque, hwaddr addr,case FINISHER_FAIL :exit (code);case FINISHER_PASS : -exit (0 ); +qemu_system_shutdown_request (SHUTDOWN_CAUSE_GUEST_SHUTDOWN ); +return ;case FINISHER_RESET :qemu_system_reset_request (SHUTDOWN_CAUSE_GUEST_RESET );return ;
Signature · 1 line
2.25.1
Re: [PATCH 1/3] hw/misc/sifive_test.c: replace exit(0) with proper shutdown
Quoted 6 lines · Clément Chigot
This replaces the exit(0) call by a shutdown request, ensuring a proper cleanup of Qemu. Otherwise, some connections like gdb could be broken without being correctly flushed.
Signed-off-by Clément Chigot <[email protected]>
Alistair
Quoted 30 lines
--- hw/misc/sifive_test.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)hw/misc/sifive_test.c +3
-2@@ -25,6 +25,7 @@#include "qemu/module.h" #include "sysemu/runstate.h" #include "hw/misc/sifive_test.h" +#include "sysemu/sysemu.h" static uint64_t sifive_test_read (void *opaque,hwaddr addr,unsigned int size) {@@ -41,7 +42,8 @@ static void sifive_test_write(void *opaque, hwaddr addr,case FINISHER_FAIL :exit (code);case FINISHER_PASS : -exit (0 ); +qemu_system_shutdown_request (SHUTDOWN_CAUSE_GUEST_SHUTDOWN ); +return ;case FINISHER_RESET :qemu_system_reset_request (SHUTDOWN_CAUSE_GUEST_RESET );return ; --2.25.1
PATCH 2/3 hw/char: riscv_htif: replace exit(0) with proper shutdown
In reply to Clément ChigotThis replaces the exit(0) call by a shutdown request, ensuring a proper cleanup of Qemu. Otherwise, some connections like gdb could be broken without being correctly flushed.
--- hw/char/riscv_htif.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
hw/char/riscv_htif.c +11 -1
@@ -31,6 +31,7 @@#include "qemu/error-report.h" #include "exec/address-spaces.h" #include "sysemu/dma.h" +#include "sysemu/runstate.h" #define RISCV_DEBUG_HTIF 0#define HTIF_DEBUG (fmt, ...) \
@@ -205,7 +206,16 @@ static void htif_handle_tohost_write(HTIFState *s, uint64_t val_written)g_free (sig_data); } -exit (exit_code); +/* +* Shutdown request is a clean way to stop the QEMU, compared +* to a direct call to exit(). But we can't pass the exit code +* through it so avoid doing that when it can matter. +*/ +if (exit_code) { +exit (exit_code); + }else { +qemu_system_shutdown_request (SHUTDOWN_CAUSE_GUEST_SHUTDOWN ); + } }else {uint64_t syscall[8 ];cpu_physical_memory_read (payload, syscall,sizeof (syscall));
Signature · 1 line
2.25.1
Re: [PATCH 2/3] hw/char: riscv_htif: replace exit(0) with proper shutdown
Quoted 6 lines · Clément Chigot
This replaces the exit(0) call by a shutdown request, ensuring a proper cleanup of Qemu. Otherwise, some connections like gdb could be broken without being correctly flushed.
Signed-off-by Clément Chigot <[email protected]>
Alistair
Quoted 38 lines
--- hw/char/riscv_htif.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)hw/char/riscv_htif.c +11
-2@@ -31,6 +31,7 @@#include "qemu/error-report.h" #include "exec/address-spaces.h" #include "sysemu/dma.h" +#include "sysemu/runstate.h" #define RISCV_DEBUG_HTIF 0#define HTIF_DEBUG (fmt, ...) \@@ -205,7 +206,16 @@ static void htif_handle_tohost_write(HTIFState *s, uint64_t val_written)g_free (sig_data); } -exit (exit_code); +/* +* Shutdown request is a clean way to stop the QEMU, compared +* to a direct call to exit(). But we can't pass the exit code +* through it so avoid doing that when it can matter. +*/ +if (exit_code) { +exit (exit_code); + }else { +qemu_system_shutdown_request (SHUTDOWN_CAUSE_GUEST_SHUTDOWN ); + } }else {uint64_t syscall[8 ];cpu_physical_memory_read (payload, syscall,sizeof (syscall)); --2.25.1
PATCH 3/3 gdbstub: replace exit(0) with proper shutdown
In reply to Clément ChigotThis replaces the exit(0) call by a shutdown request, ensuring a proper cleanup of Qemu. Otherwise, some connections could be broken without being correctly flushed.
--- gdbstub/gdbstub.c | 3 +-- gdbstub/softmmu.c | 11 +++++++++++ gdbstub/user.c | 2 ++ 3 files changed, 14 insertions(+), 2 deletions(-)
gdbstub/gdbstub.c +1 -2
@@ -1298,7 +1298,6 @@ static void handle_v_kill(GArray *params, void *user_ctx)gdb_put_packet ("OK" );error_report ("QEMU: Terminated via GDBstub" );gdb_exit (0 ); -exit (0 ); }static const GdbCmdParseEntry gdb_v_commands_table[] = {
@@ -1818,7 +1817,7 @@ static int gdb_handle_packet(const char *line_buf)/* Kill the target */ error_report ("QEMU: Terminated via GDBstub" );gdb_exit (0 ); -exit (0 ); +break ;case 'D' : {static const GdbCmdParseEntry detach_cmd_desc = {
gdbstub/softmmu.c +11 -0
@@ -434,6 +434,17 @@ void gdb_exit(int code) }qemu_chr_fe_deinit (&gdbserver_system_state.chr, true); + +/* +* Shutdown request is a clean way to stop the QEMU, compared +* to a direct call to exit(). But we can't pass the exit code +* through it so avoid doing that when it can matter. +*/ +if (code) { +exit (code); + }else { +qemu_system_shutdown_request (SHUTDOWN_CAUSE_GUEST_SHUTDOWN ); + } }/*
gdbstub/user.c +2 -0
@@ -113,6 +113,8 @@ void gdb_exit(int code)gdb_put_packet (buf); gdbserver_state.allow_stop_reply = false; } + +exit (code); }int gdb_handlesig (CPUState *cpu,int sig)
Signature · 1 line
2.25.1
Re: [PATCH 3/3] gdbstub: replace exit(0) with proper shutdown
Quoted 60 lines · Clément Chigot
This replaces the exit(0) call by a shutdown request, ensuring a proper cleanup of Qemu. Otherwise, some connections could be broken without being correctly flushed.
Signed-off-by Clément Chigot <[email protected]>--- gdbstub/gdbstub.c | 3 +-- gdbstub/softmmu.c | 11 +++++++++++ gdbstub/user.c | 2 ++ 3 files changed, 14 insertions(+), 2 deletions(-)gdbstub/gdbstub.c +1
-2@@ -1298,7 +1298,6 @@ static void handle_v_kill(GArray *params, void *user_ctx)gdb_put_packet ("OK" );error_report ("QEMU: Terminated via GDBstub" );gdb_exit (0 ); -exit (0 ); }static const GdbCmdParseEntry gdb_v_commands_table[] = {@@ -1818,7 +1817,7 @@ static int gdb_handle_packet(const char *line_buf)/* Kill the target */ error_report ("QEMU: Terminated via GDBstub" );gdb_exit (0 ); -exit (0 ); +break ;case 'D' : {static const GdbCmdParseEntry detach_cmd_desc = {gdbstub/softmmu.c +11
-0@@ -434,6 +434,17 @@ void gdb_exit(int code) }qemu_chr_fe_deinit (&gdbserver_system_state.chr, true); + +/* +* Shutdown request is a clean way to stop the QEMU, compared +* to a direct call to exit(). But we can't pass the exit code +* through it so avoid doing that when it can matter. +*/ +if (code) { +exit (code); + }else { +qemu_system_shutdown_request (SHUTDOWN_CAUSE_GUEST_SHUTDOWN ); + } }/* gdbstub/user.c +0
-0
gdbstub/user.c void gdb_exit(int code) · line 118
Quoted 6 lines
gdbstub/user.c +2
-0@@ -113,6 +113,8 @@ void gdb_exit(int code)gdb_put_packet (buf); gdbserver_state.allow_stop_reply = false; } + +exit (code); }
These are not the only places that call gdb_exit(). Notably, qemu_cleanup() calls it, and I'm pretty sure it does not expect that gdb_exit() will either call exit() or qemu_system_shutdown_request(), because it's already in the process of cleaning up and stopping the system.
If we send the "we're exiting" report to the gdb process, that ought to be sufficient. If we're not actually managing to send that last packet to gdb because we don't flush the data out to the file descriptor, then it seems to me that the fix ought to be to ensure we do do that flush as part of gdb_exit() not to rearrange or try to avoid all the subsequent calls to exit().
-- PMM
Re: [PATCH 3/3] gdbstub: replace exit(0) with proper shutdown
Quoted 67 lines · Peter Maydell
Quoted 65 lines · Clément Chigot
This replaces the exit(0) call by a shutdown request, ensuring a proper cleanup of Qemu. Otherwise, some connections could be broken without being correctly flushed.
Signed-off-by Clément Chigot <[email protected]>--- gdbstub/gdbstub.c | 3 +-- gdbstub/softmmu.c | 11 +++++++++++ gdbstub/user.c | 2 ++ 3 files changed, 14 insertions(+), 2 deletions(-)gdbstub/gdbstub.c +1
-2@@ -1298,7 +1298,6 @@ static void handle_v_kill(GArray *params, void *user_ctx)gdb_put_packet ("OK" );error_report ("QEMU: Terminated via GDBstub" );gdb_exit (0 ); -exit (0 ); }static const GdbCmdParseEntry gdb_v_commands_table[] = {@@ -1818,7 +1817,7 @@ static int gdb_handle_packet(const char *line_buf)/* Kill the target */ error_report ("QEMU: Terminated via GDBstub" );gdb_exit (0 ); -exit (0 ); +break ;case 'D' : {static const GdbCmdParseEntry detach_cmd_desc = {gdbstub/softmmu.c +11
-0@@ -434,6 +434,17 @@ void gdb_exit(int code) }qemu_chr_fe_deinit (&gdbserver_system_state.chr, true); + +/* +* Shutdown request is a clean way to stop the QEMU, compared +* to a direct call to exit(). But we can't pass the exit code +* through it so avoid doing that when it can matter. +*/ +if (code) { +exit (code); + }else { +qemu_system_shutdown_request (SHUTDOWN_CAUSE_GUEST_SHUTDOWN ); + } }/* gdbstub/user.c +2
-0@@ -113,6 +113,8 @@ void gdb_exit(int code)gdb_put_packet (buf); gdbserver_state.allow_stop_reply = false; } + +exit (code);
Quoted 8 lines
gdbstub/user.c void gdb_exit(int code) · line 118
Quoted 1 line
gdbstub/user.c +0
-0}These are not the only places that call
gdb_exit(). Notably,qemu_cleanup()calls it, and I'm pretty sure it does not expect thatgdb_exit()will either callexit()orqemu_system_shutdown_request(), because it's already in the process of cleaning up and stopping the system.
Indeed, I did miss that. I used to have it directly in gdb_handle_packet and in handle_v_kill. But now that the support of softmmu and user has been splitted, I thought putting it in gdb_exit was a solution.
However, IIUC the code, the second request will simply be ignored, the main loop (where the requests matter) have been already exited. I see what I can do anyway to avoid this double request.
Quoted 7 lines
If we send the "we're exiting" report to the gdb process, that ought to be sufficient. If we're not actually managing to send that last packet to gdb because we don't flush the data out to the file descriptor, then it seems to me that the fix ought to be to ensure we do do that flush as part of
gdb_exit()not to rearrange or try to avoid all the subsequent calls toexit().
Here, I'm seeing the symptoms with a gdb connection being closed too sharply and a fd not being flush. But looking at the qemu_cleanup(), many things could require a proper cleanup which will be skipped by a simple exit(0). This could lead to many unexpected side effects.
Thanks,
Clément