[XTF PATCH v3 2/3] x86: Allow exiting QEMU in TCG/QEMU

Alejandro Vallejo posted 3 patches 3 weeks, 3 days ago
[XTF PATCH v3 2/3] x86: Allow exiting QEMU in TCG/QEMU
Posted by Alejandro Vallejo 3 weeks, 3 days ago
If QEMU has a debug isa-debug-exit device, we can simply write to it
to exit rather than spinning after a failed hypercall.

There's nothing to signal its existence, so it's a best-effort write
after a shutdown attempt via hypercall.

Signed-off-by: Alejandro Vallejo <alejandro.garciavallejo@amd.com>
---
v3:
  * Moved arch_shutdown() from HVM/PV folders to arch/x86/traps.c
    There's nothing terribly specific about it.
  * Gated calling hypercall_shutdown() on cpu_has_xen being set
---
 arch/x86/traps.c        | 15 +++++++++++++++
 common/lib.c            |  2 +-
 common/report.c         |  8 +++++---
 include/xtf/framework.h |  3 +++
 4 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/arch/x86/traps.c b/arch/x86/traps.c
index 1f82752..b530719 100644
--- a/arch/x86/traps.c
+++ b/arch/x86/traps.c
@@ -80,6 +80,21 @@ void __weak do_evtchn(struct cpu_regs *regs)
     panic("Unhandled evtchn upcall\n");
 }
 
+void arch_shutdown(unsigned int reason)
+{
+    if ( cpu_has_xen )
+        hypercall_shutdown(reason);
+
+    /*
+     * Not running under Xen. Attempt exit via the QEMU ISA debug exit device on
+     * its default port.
+     *
+     * QEMU's rc is (reason << 1) | 1, if "-device isa-debug-exit" is set.
+     */
+    if ( IS_DEFINED(CONFIG_HVM) )
+        outb(reason, 0x501);
+}
+
 /*
  * Local variables:
  * mode: C
diff --git a/common/lib.c b/common/lib.c
index 7f1813f..f4de22e 100644
--- a/common/lib.c
+++ b/common/lib.c
@@ -25,7 +25,7 @@ void __noreturn panic(const char *fmt, ...)
 
     printk("******************************\n");
 
-    hypercall_shutdown(SHUTDOWN_crash);
+    arch_shutdown(SHUTDOWN_crash);
     arch_crash_hard();
 }
 
diff --git a/common/report.c b/common/report.c
index ffdf098..158876e 100644
--- a/common/report.c
+++ b/common/report.c
@@ -1,6 +1,8 @@
+#include <xtf/framework.h>
 #include <xtf/lib.h>
 #include <xtf/report.h>
-#include <xtf/hypercall.h>
+
+#include <xen/sched.h>
 
 enum test_status {
     STATUS_RUNNING, /**< Test not yet completed.       */
@@ -124,8 +126,8 @@ bool xtf_status_reported(void)
 void xtf_exit(void)
 {
     xtf_report_status();
-    hypercall_shutdown(SHUTDOWN_poweroff);
-    panic("xtf_exit(): hypercall_shutdown(SHUTDOWN_poweroff) returned\n");
+    arch_shutdown(SHUTDOWN_poweroff);
+    panic("xtf_exit(): arch_shutdown(SHUTDOWN_poweroff) returned\n");
 }
 
 /*
diff --git a/include/xtf/framework.h b/include/xtf/framework.h
index 95de195..e852882 100644
--- a/include/xtf/framework.h
+++ b/include/xtf/framework.h
@@ -16,6 +16,9 @@ void arch_setup(void);
 /* Set up test-specific configuration. */
 void test_setup(void);
 
+/* Stop the machine. See SHUTDOWN_poweroff et al for reasons */
+void arch_shutdown(unsigned int reason);
+
 /*
  * In the case that normal shutdown actions have failed, contain execution as
  * best as possible.
-- 
2.43.0
Re: [XTF PATCH v3 2/3] x86: Allow exiting QEMU in TCG/QEMU
Posted by Alejandro Vallejo 3 weeks, 3 days ago
On Mon Oct 6, 2025 at 12:25 PM CEST, Alejandro Vallejo wrote:
> If QEMU has a debug isa-debug-exit device, we can simply write to it
> to exit rather than spinning after a failed hypercall.
>
> There's nothing to signal its existence, so it's a best-effort write
> after a shutdown attempt via hypercall.
>
> Signed-off-by: Alejandro Vallejo <alejandro.garciavallejo@amd.com>
> ---
> v3:
>   * Moved arch_shutdown() from HVM/PV folders to arch/x86/traps.c
>     There's nothing terribly specific about it.
>   * Gated calling hypercall_shutdown() on cpu_has_xen being set
> ---
>  arch/x86/traps.c        | 15 +++++++++++++++
>  common/lib.c            |  2 +-
>  common/report.c         |  8 +++++---
>  include/xtf/framework.h |  3 +++
>  4 files changed, 24 insertions(+), 4 deletions(-)
>
> diff --git a/arch/x86/traps.c b/arch/x86/traps.c
> index 1f82752..b530719 100644
> --- a/arch/x86/traps.c
> +++ b/arch/x86/traps.c
> @@ -80,6 +80,21 @@ void __weak do_evtchn(struct cpu_regs *regs)
>      panic("Unhandled evtchn upcall\n");
>  }

Bah. Missing "#include <xtf/hypercall.h>"

Otherwise works as intended.

Cheers,
Alejandro