From dbd3856ccdbc0cb1a326c82f3d527d3eb3b1893e Mon Sep 17 00:00:00 2001 From: Cezar Craciunoiu Date: Mon, 7 Aug 2023 12:03:40 +0300 Subject: [PATCH] plat/common: Add support for pvpanic devices for x86 Unikraft already offers exit codes through the ISA device, but these are only a convention and are not understood by QEMU itself. When using `-device pvpanic`, a GUEST_PANICKED event[1] is sent to QEMU when the unikernel crashes. This event can be seen when using QMP[1] for example. The bits corresponding to each even are set as per the documentation[2]. The main usage of this is in kraftkit where unikernels are detached and exit codes are lost. Using this can signal when the unikernel crashes. [1] https://qemu-project.gitlab.io/qemu/interop/qemu-qmp-ref.html#qapidoc-152 [2] https://github.com/qemu/qemu/blob/master/docs/specs/pvpanic.txt Signed-off-by: Cezar Craciunoiu Reviewed-by: Marco Schlumpp Approved-by: Razvan Deaconescu Tested-by: Unikraft CI GitHub-Closes: #1022 --- plat/common/x86/cpu_native.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/plat/common/x86/cpu_native.c b/plat/common/x86/cpu_native.c index 7c216a477..166d4ae2f 100644 --- a/plat/common/x86/cpu_native.c +++ b/plat/common/x86/cpu_native.c @@ -55,6 +55,13 @@ unsigned long read_cr2(void) /* This corresponds to an 85 (42 << 1 | 1) return value from QEMU */ #define QEMU_ISA_DEBUG_EXIT_CRASH 42 +/* The port used by QEMU by default for the pvpanic device */ +#define QEMU_PVPANIC_EXIT_PORT 0x505 +/* This corresponds to a GUEST_PANICKED event for QEMU */ +#define QEMU_PVPANIC_GUEST_PANICKED (1 << 0) +/* This corresponds to a GUEST_CRASHLOADED event for QEMU */ +#define QEMU_PVPANIC_GUEST_CRASHLOADED (1 << 1) + /** * Trigger an exit() in QEMU with the code `value << 1 | 1`. * @param value the value used in the calculation of the exit code @@ -79,9 +86,13 @@ void system_off(enum ukplat_gstate request __maybe_unused) * device. * Should be harmless if it is not present. This is used to enable * automated tests on virtio. + * Also send a panic request to the pvpanic device. + * Should be also harmless and helps with automated tests. */ - if (request == UKPLAT_CRASH) + if (request == UKPLAT_CRASH) { qemu_debug_exit(QEMU_ISA_DEBUG_EXIT_CRASH); + outb(QEMU_PVPANIC_EXIT_PORT, QEMU_PVPANIC_GUEST_PANICKED); + } #endif /* CONFIG_KVM_VMM_QEMU */ /* -- 2.39.5