diff mbox series

[v2,2/3] hw/char: riscv_htif: replace exit(0) with proper shutdown

Message ID 20230823070704.363098-1-chigot@adacore.com (mailing list archive)
State New, archived
Headers show
Series [v2,1/3] hw/misc/sifive_test.c: replace exit(0) with proper shutdown | expand

Commit Message

Clément Chigot Aug. 23, 2023, 7:07 a.m. UTC
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 <chigot@adacore.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
---
 hw/char/riscv_htif.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

Comments

Peter Maydell Sept. 4, 2023, 9:44 a.m. UTC | #1
On Wed, 23 Aug 2023 at 08:07, Clément Chigot <chigot@adacore.com> wrote:
>
> 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 <chigot@adacore.com>
> Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
> ---
>  hw/char/riscv_htif.c | 12 +++++++++++-
>  1 file changed, 11 insertions(+), 1 deletion(-)
>
> diff --git a/hw/char/riscv_htif.c b/hw/char/riscv_htif.c
> index 37d3ccc76b..c49d20a221 100644
> --- a/hw/char/riscv_htif.c
> +++ b/hw/char/riscv_htif.c
> @@ -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);
> +                }

This is wrong for the same reason that patch 3 is wrong.

Also, does the guest code really expect to handle execution
continuing after this point? qemu_system_shutdown_request()
is asynchronous.

thanks
-- PMM
diff mbox series

Patch

diff --git a/hw/char/riscv_htif.c b/hw/char/riscv_htif.c
index 37d3ccc76b..c49d20a221 100644
--- a/hw/char/riscv_htif.c
+++ b/hw/char/riscv_htif.c
@@ -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));