hw/misc/pvpanic: add support for normal shutdowns
v6 cover hw/misc/pvpanic: add support for normal shutdowns
Shutdown requests are normally hardware dependent.
By extending pvpanic to also handle shutdown requests, guests can submit such requests with an easily implementable and cross-platform mechanism.
The background is the usage of minimal Linux kernels with different architectures for testing purposes.
Poweroff support varies highly per architecture and requires a bunch of code to be compiled to work.
pvpanic on the other hand is very small and uniform.
Patch 1, 2 and 3 are general cleanups, which seem useful even without this proposal being implemented.
They should also be ready to be picked up from the series on their own.
A corresponding patch has been submitted for Linux [0]. This is also where the request was voiced to drop move away from a pvpanic uapi header in Linux.
[0] https://lore.kernel.org/lkml/[email protected]/
---
Changes in v6:
- Replace magic constant "4" in tests with PVPANIC_SHUTDOWN
- Link to v5: https://lore.kernel.org/r/[email protected]
Changes in v5:
- Add patch from Alejandro to emit a QMP event.
- Update cover letter.
- Add tests.
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes in v4:
- Rebase on 8.2 master
- Resend after tree reopened and holidays
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Drop from Linux imported pvpanic header as discussed with Cornelia and requested by Greg
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Remove RFC status
- Add Ack from Thomas to 2nd patch
- Fix typo in title of 2nd patch
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Alejandro Jimenez (1):
pvpanic: Emit GUEST_PVSHUTDOWN QMP event on pvpanic shutdown signal
Thomas Weißschuh (5):
linux-headers: drop pvpanic.h
hw/misc/pvpanic: centralize definition of supported events tests/qtest/pvpanic: use centralized definition of supported events hw/misc/pvpanic: add support for normal shutdowns tests/qtest/pvpanic: add tests for pvshutdown event
docs/specs/pvpanic.rst | 2 ++ hw/misc/pvpanic-isa.c | 3 +-- hw/misc/pvpanic-pci.c | 3 +-- hw/misc/pvpanic.c | 8 ++++-- include/hw/misc/pvpanic.h | 5 ++++ include/standard-headers/linux/pvpanic.h | 9 ------- include/sysemu/runstate.h | 1 + qapi/run-state.json | 14 ++++++++++ scripts/update-linux-headers.sh | 3 +-- system/runstate.c | 6 +++++ tests/qtest/pvpanic-pci-test.c | 44 ++++++++++++++++++++++++++++++-- tests/qtest/pvpanic-test.c | 34 ++++++++++++++++++++++-- 12 files changed, 111 insertions(+), 21 deletions(-)
---
base-commit: 9e34f127f419b3941b36dfdfac79640dc81e97e2
change-id: 20231104-pvpanic-shutdown-02e4b4cb4949
Best regards,
Signature · 1 line
Thomas Weißschuh <[email protected]>
PATCH v6 1/6 linux-headers: drop pvpanic.h
misc/pvpanic.h from the Linux UAPI does not define a Linux UAPI but a qemu device API.
This leads to a weird process when updates to the interface are needed:
1) Change to the specification in the qemu tree
2) Change to the header in the Linux tree
3) Re-import of the header into Qemu.
The kernel prefers to drop the header anyways.
Prepare for the removal from the Linux UAPI headers by moving the contents to the existing pvpanic.h header.
--- hw/misc/pvpanic-isa.c | 1 - hw/misc/pvpanic-pci.c | 1 - hw/misc/pvpanic.c | 1 - include/hw/misc/pvpanic.h | 3 +++ include/standard-headers/linux/pvpanic.h | 9 --------- scripts/update-linux-headers.sh | 3 +-- 6 files changed, 4 insertions(+), 14 deletions(-)
hw/misc/pvpanic-isa.c +0 -1
@@ -21,7 +21,6 @@#include "hw/misc/pvpanic.h" #include "qom/object.h" #include "hw/isa/isa.h" -#include "standard-headers/linux/pvpanic.h" #include "hw/acpi/acpi_aml_interface.h" OBJECT_DECLARE_SIMPLE_TYPE (PVPanicISAState,PVPANIC_ISA_DEVICE )
hw/misc/pvpanic-pci.c +0 -1
@@ -21,7 +21,6 @@#include "hw/misc/pvpanic.h" #include "qom/object.h" #include "hw/pci/pci_device.h" -#include "standard-headers/linux/pvpanic.h" OBJECT_DECLARE_SIMPLE_TYPE (PVPanicPCIState,PVPANIC_PCI_DEVICE )
hw/misc/pvpanic.c +0 -1
@@ -21,7 +21,6 @@#include "hw/qdev-properties.h" #include "hw/misc/pvpanic.h" #include "qom/object.h" -#include "standard-headers/linux/pvpanic.h" static void handle_event (int event) {
include/hw/misc/pvpanic.h +3 -0
@@ -18,6 +18,9 @@#include "exec/memory.h" #include "qom/object.h" +#define PVPANIC_PANICKED (1 << 0) +#define PVPANIC_CRASH_LOADED (1 << 1) +#define TYPE_PVPANIC_ISA_DEVICE "pvpanic"#define TYPE_PVPANIC_PCI_DEVICE "pvpanic-pci"
include/standard-headers/linux/pvpanic.h deleted +0 -9
@@ -1,9 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ - -#ifndef __PVPANIC_H__ -#define __PVPANIC_H__ - -#define PVPANIC_PANICKED (1 << 0) -#define PVPANIC_CRASH_LOADED (1 << 1) - -#endif /* __PVPANIC_H__ */
scripts/update-linux-headers.sh +1 -2
@@ -218,8 +218,7 @@ for i in "$tmpdir"/include/linux/*virtio*.h \ "$tmpdir/include/linux/const.h" \ "$tmpdir/include/linux/kernel.h" \ "$tmpdir/include/linux/vhost_types.h" \ - "$tmpdir/include/linux/sysinfo.h" \ - "$tmpdir/include/misc/pvpanic.h"; do + "$tmpdir/include/linux/sysinfo.h"; do cp_portable "$i" "$output/include/standard-headers/linux" done mkdir -p "$output/include/standard-headers/drm"
Signature · 1 line
2.43.0
Re: [PATCH v6 1/6] linux-headers: drop pvpanic.h
Quoted 10 lines · Thomas Weißschuh
misc/pvpanic.hfrom the Linux UAPI does not define a Linux UAPI but a qemu device API.This leads to a weird process when updates to the interface are needed:
1) Change to the specification in the qemu tree
2) Change to the header in the Linux tree
3) Re-import of the header into Qemu.The kernel prefers to drop the header anyways.
Quoted 7 lines
Prepare for the removal from the Linux UAPI headers by moving the contents to the existing
pvpanic.hheader.
I don't think I agree with greg. The interaction is with hypervisor not with userspace but linux does not have a separate directory for that and the implications are mostly the same.
Quoted 91 lines
--- hw/misc/pvpanic-isa.c | 1 - hw/misc/pvpanic-pci.c | 1 - hw/misc/pvpanic.c | 1 - include/hw/misc/pvpanic.h | 3 +++ include/standard-headers/linux/pvpanic.h | 9 --------- scripts/update-linux-headers.sh | 3 +-- 6 files changed, 4 insertions(+), 14 deletions(-)hw/misc/pvpanic-isa.c +0
-1@@ -21,7 +21,6 @@#include "hw/misc/pvpanic.h" #include "qom/object.h" #include "hw/isa/isa.h" -#include "standard-headers/linux/pvpanic.h" #include "hw/acpi/acpi_aml_interface.h" OBJECT_DECLARE_SIMPLE_TYPE (PVPanicISAState,PVPANIC_ISA_DEVICE )hw/misc/pvpanic-pci.c +0
-1@@ -21,7 +21,6 @@#include "hw/misc/pvpanic.h" #include "qom/object.h" #include "hw/pci/pci_device.h" -#include "standard-headers/linux/pvpanic.h" OBJECT_DECLARE_SIMPLE_TYPE (PVPanicPCIState,PVPANIC_PCI_DEVICE )hw/misc/pvpanic.c +0
-1@@ -21,7 +21,6 @@#include "hw/qdev-properties.h" #include "hw/misc/pvpanic.h" #include "qom/object.h" -#include "standard-headers/linux/pvpanic.h" static void handle_event (int event) {include/hw/misc/pvpanic.h +3
-0@@ -18,6 +18,9 @@#include "exec/memory.h" #include "qom/object.h" +#define PVPANIC_PANICKED (1 << 0) +#define PVPANIC_CRASH_LOADED (1 << 1) +#define TYPE_PVPANIC_ISA_DEVICE "pvpanic"#define TYPE_PVPANIC_PCI_DEVICE "pvpanic-pci"include/standard-headers/linux/pvpanic.h deleted +0
-9@@ -1,9 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ - -#ifndef __PVPANIC_H__ -#define __PVPANIC_H__ - -#define PVPANIC_PANICKED (1 << 0) -#define PVPANIC_CRASH_LOADED (1 << 1) - -#endif /* __PVPANIC_H__ */ scripts/update-linux-headers.sh +1
-2@@ -218,8 +218,7 @@ for i in "$tmpdir"/include/linux/*virtio*.h \ "$tmpdir/include/linux/const.h" \ "$tmpdir/include/linux/kernel.h" \ "$tmpdir/include/linux/vhost_types.h" \ - "$tmpdir/include/linux/sysinfo.h" \ - "$tmpdir/include/misc/pvpanic.h"; do + "$tmpdir/include/linux/sysinfo.h"; do cp_portable "$i" "$output/include/standard-headers/linux" done mkdir -p "$output/include/standard-headers/drm"Signature · 2 lines
-- 2.43.0
PATCH v6 2/6 hw/misc/pvpanic: centralize definition of supported events
In reply to Thomas WeißschuhThe different components of pvpanic duplicate the list of supported events. Move it to the shared header file to minimize changes when new events are added.
--- hw/misc/pvpanic-isa.c | 2 +- hw/misc/pvpanic-pci.c | 2 +- hw/misc/pvpanic.c | 2 +- include/hw/misc/pvpanic.h | 1 + 4 files changed, 4 insertions(+), 3 deletions(-)
hw/misc/pvpanic-isa.c +1 -1
@@ -101,7 +101,7 @@ static void build_pvpanic_isa_aml(AcpiDevAmlIf *adev, Aml *scope)static Property pvpanic_isa_properties[] = {DEFINE_PROP_UINT16 (PVPANIC_IOPORT_PROP , PVPanicISAState, ioport,0x505 ),DEFINE_PROP_UINT8 ("events" , PVPanicISAState, pvpanic.events, -PVPANIC_PANICKED |PVPANIC_CRASH_LOADED ), +PVPANIC_EVENTS ),DEFINE_PROP_END_OF_LIST (), };
hw/misc/pvpanic-pci.c +1 -1
@@ -54,7 +54,7 @@ static void pvpanic_pci_realizefn(PCIDevice *dev, Error **errp)static Property pvpanic_pci_properties[] = {DEFINE_PROP_UINT8 ("events" , PVPanicPCIState, pvpanic.events, -PVPANIC_PANICKED |PVPANIC_CRASH_LOADED ), +PVPANIC_EVENTS ),DEFINE_PROP_END_OF_LIST (), };
hw/misc/pvpanic.c +1 -1
@@ -26,7 +26,7 @@ static void handle_event(int event) {static bool logged; -if (event & ~(PVPANIC_PANICKED |PVPANIC_CRASH_LOADED ) && !logged) { +if (event & ~PVPANIC_EVENTS && !logged) {qemu_log_mask (LOG_GUEST_ERROR ,"pvpanic: unknown event %#x.\n" , event); logged = true; }
include/hw/misc/pvpanic.h +1 -0
@@ -20,6 +20,7 @@#define PVPANIC_PANICKED (1 << 0)#define PVPANIC_CRASH_LOADED (1 << 1) +#define PVPANIC_EVENTS (PVPANIC_PANICKED | PVPANIC_CRASH_LOADED)#define TYPE_PVPANIC_ISA_DEVICE "pvpanic"#define TYPE_PVPANIC_PCI_DEVICE "pvpanic-pci"
Signature · 1 line
2.43.0
PATCH v6 3/6 tests/qtest/pvpanic: use centralized definition of supported events
In reply to Thomas WeißschuhAvoid the necessity to update all tests when new events are added to the device.
--- tests/qtest/pvpanic-pci-test.c | 5 +++-- tests/qtest/pvpanic-test.c | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-)
tests/qtest/pvpanic-pci-test.c +3 -2
@@ -16,6 +16,7 @@#include "qapi/qmp/qdict.h" #include "libqos/pci.h" #include "libqos/pci-pc.h" +#include "hw/misc/pvpanic.h" #include "hw/pci/pci_regs.h" static void test_panic_nopause (void )
@@ -34,7 +35,7 @@ static void test_panic_nopause(void) bar =qpci_iomap (dev,0 ,NULL );qpci_memread (dev, bar,0 , &val,sizeof (val)); - g_assert_cmpuint(val, ==, 3); + g_assert_cmpuint(val, ==, PVPANIC_EVENTS); val =1 ;qpci_memwrite (dev, bar,0 , &val,sizeof (val));
@@ -67,7 +68,7 @@ static void test_panic(void) bar =qpci_iomap (dev,0 ,NULL );qpci_memread (dev, bar,0 , &val,sizeof (val)); - g_assert_cmpuint(val, ==, 3); + g_assert_cmpuint(val, ==, PVPANIC_EVENTS); val =1 ;qpci_memwrite (dev, bar,0 , &val,sizeof (val));
tests/qtest/pvpanic-test.c +3 -2
@@ -10,6 +10,7 @@#include "qemu/osdep.h" #include "libqtest.h" #include "qapi/qmp/qdict.h" +#include "hw/misc/pvpanic.h" static void test_panic_nopause (void ) {
@@ -20,7 +21,7 @@ static void test_panic_nopause(void) qts =qtest_init ("-device pvpanic -action panic=none" ); val =qtest_inb (qts,0x505 ); - g_assert_cmpuint(val, ==, 3); + g_assert_cmpuint(val, ==, PVPANIC_EVENTS);qtest_outb (qts,0x505 ,0x1 );
@@ -43,7 +44,7 @@ static void test_panic(void) qts =qtest_init ("-device pvpanic -action panic=pause" ); val =qtest_inb (qts,0x505 ); - g_assert_cmpuint(val, ==, 3); + g_assert_cmpuint(val, ==, PVPANIC_EVENTS);qtest_outb (qts,0x505 ,0x1 );
Signature · 1 line
2.43.0
PATCH v6 4/6 hw/misc/pvpanic: add support for normal shutdowns
In reply to Thomas WeißschuhShutdown requests are normally hardware dependent.
By extending pvpanic to also handle shutdown requests, guests can submit such requests with an easily implementable and cross-platform mechanism.
--- docs/specs/pvpanic.rst | 2 ++ hw/misc/pvpanic.c | 5 +++++ include/hw/misc/pvpanic.h | 3 ++- include/sysemu/runstate.h | 1 + system/runstate.c | 5 +++++ 5 files changed, 15 insertions(+), 1 deletion(-)
docs/specs/pvpanic.rst +2 -0
@@ -29,6 +29,8 @@ bit 1 a guest panic has happened and will be handled by the guest; the host should record it or report it, but should not affect the execution of the guest. +bit 2 + a guest shutdown has happened and should be processed by the host PCI Interface -------------
hw/misc/pvpanic.c +5 -0
@@ -40,6 +40,11 @@ static void handle_event(int event)qemu_system_guest_crashloaded (NULL );return ; } + +if (event &PVPANIC_SHUTDOWN ) { +qemu_system_guest_pvshutdown (); +return ; + } }/* return supported events on read */
include/hw/misc/pvpanic.h +2 -1
@@ -20,7 +20,8 @@#define PVPANIC_PANICKED (1 << 0)#define PVPANIC_CRASH_LOADED (1 << 1) -#define PVPANIC_EVENTS (PVPANIC_PANICKED | PVPANIC_CRASH_LOADED) +#define PVPANIC_SHUTDOWN (1 << 2) +#define PVPANIC_EVENTS (PVPANIC_PANICKED | PVPANIC_CRASH_LOADED | PVPANIC_SHUTDOWN)#define TYPE_PVPANIC_ISA_DEVICE "pvpanic"#define TYPE_PVPANIC_PCI_DEVICE "pvpanic-pci"
include/sysemu/runstate.h +1 -0
@@ -104,6 +104,7 @@ void qemu_system_killed(int signal, pid_t pid);void qemu_system_reset (ShutdownCause reason);void qemu_system_guest_panicked (GuestPanicInformation *info);void qemu_system_guest_crashloaded (GuestPanicInformation *info); +void qemu_system_guest_pvshutdown (void );bool qemu_system_dump_in_progress (void );#endif
system/runstate.c +5 -0
@@ -572,6 +572,11 @@ void qemu_system_guest_crashloaded(GuestPanicInformation *info)qapi_free_GuestPanicInformation (info); } +void qemu_system_guest_pvshutdown (void ) +{ +qemu_system_shutdown_request (SHUTDOWN_CAUSE_GUEST_SHUTDOWN ); +} +void qemu_system_reset_request (ShutdownCause reason) { if (reboot_action == REBOOT_ACTION_SHUTDOWN &&
Signature · 1 line
2.43.0
PATCH v6 5/6 pvpanic: Emit GUEST_PVSHUTDOWN QMP event on pvpanic shutdown signal
In reply to Thomas WeißschuhFrom: Alejandro Jimenez <[email protected]>
Emit a QMP event on receiving a PVPANIC_SHUTDOWN event. Even though a typical SHUTDOWN event will be sent, it will be indistinguishable from a shutdown originating from other cases (e.g. KVM exit due to KVM_SYSTEM_EVENT_SHUTDOWN) that also issue the guest-shutdown cause.
A management layer application can detect the new GUEST_PVSHUTDOWN event to determine if the guest is using the pvpanic interface to request shutdowns.
--- qapi/run-state.json | 14 ++++++++++++++ system/runstate.c | 1 + 2 files changed, 15 insertions(+)
qapi/run-state.json +14 -0
@@ -460,6 +460,20 @@ { 'event': 'GUEST_CRASHLOADED', 'data': { 'action': 'GuestPanicAction', '*info': 'GuestPanicInformation' } } +## +# @GUEST_PVSHUTDOWN: +# +# Emitted when guest submits a shutdown request via pvpanic interface +# +# Since: 8.3 +# +# Example: +# +# <- { "event": "GUEST_PVSHUTDOWN", +# "timestamp": { "seconds": 1648245259, "microseconds": 893771 } } +## +{ 'event': 'GUEST_PVSHUTDOWN' } + ## # @GuestPanicAction: #
system/runstate.c +1 -0
@@ -574,6 +574,7 @@ void qemu_system_guest_crashloaded(GuestPanicInformation *info)void qemu_system_guest_pvshutdown (void ) { +qapi_event_send_guest_pvshutdown ();qemu_system_shutdown_request (SHUTDOWN_CAUSE_GUEST_SHUTDOWN ); }
Signature · 1 line
2.43.0
PATCH v6 6/6 tests/qtest/pvpanic: add tests for pvshutdown event
In reply to Thomas WeißschuhValidate that a shutdown via the pvpanic device emits the correct QMP events.
--- tests/qtest/pvpanic-pci-test.c | 39 +++++++++++++++++++++++++++++++++++++++ tests/qtest/pvpanic-test.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+)
tests/qtest/pvpanic-pci-test.c +39 -0
@@ -85,11 +85,50 @@ static void test_panic(void)qtest_quit (qts); } +static void test_pvshutdown (void ) +{ +uint8_t val; +QDict *response, *data; +QTestState *qts; +QPCIBus *pcibus; +QPCIDevice *dev; +QPCIBar bar; + + qts =qtest_init ("-device pvpanic-pci,addr=04.0" ); + pcibus =qpci_new_pc (qts,NULL ); + dev =qpci_device_find (pcibus,QPCI_DEVFN (0x4 ,0x0 )); +qpci_device_enable (dev); + bar =qpci_iomap (dev,0 ,NULL ); + +qpci_memread (dev, bar,0 , &val,sizeof (val)); + g_assert_cmpuint(val, ==, PVPANIC_EVENTS); + + val =PVPANIC_SHUTDOWN ; +qpci_memwrite (dev, bar,0 , &val,sizeof (val)); + + response =qtest_qmp_eventwait_ref (qts,"GUEST_PVSHUTDOWN" ); +qobject_unref (response); + + response =qtest_qmp_eventwait_ref (qts,"SHUTDOWN" ); +g_assert (qdict_haskey (response,"data" )); + data =qdict_get_qdict (response,"data" ); +g_assert (qdict_haskey (data,"guest" )); +g_assert (qdict_get_bool (data,"guest" )); +g_assert (qdict_haskey (data,"reason" )); + g_assert_cmpstr(qdict_get_str(data, "reason"), ==, "guest-shutdown"); +qobject_unref (response); + +g_free (dev); +qpci_free_pc (pcibus); +qtest_quit (qts); +} +int main (int argc,char **argv) {g_test_init (&argc, &argv,NULL );qtest_add_func ("/pvpanic-pci/panic" , test_panic);qtest_add_func ("/pvpanic-pci/panic-nopause" , test_panic_nopause); +qtest_add_func ("/pvpanic-pci/pvshutdown" , test_pvshutdown);return g_test_run (); }
tests/qtest/pvpanic-test.c +29 -0
@@ -58,11 +58,40 @@ static void test_panic(void)qtest_quit (qts); } +static void test_pvshutdown (void ) +{ +uint8_t val; +QDict *response, *data; +QTestState *qts; + + qts =qtest_init ("-device pvpanic" ); + + val =qtest_inb (qts,0x505 ); + g_assert_cmpuint(val, ==, PVPANIC_EVENTS); + +qtest_outb (qts,0x505 ,PVPANIC_SHUTDOWN ); + + response =qtest_qmp_eventwait_ref (qts,"GUEST_PVSHUTDOWN" ); +qobject_unref (response); + + response =qtest_qmp_eventwait_ref (qts,"SHUTDOWN" ); +g_assert (qdict_haskey (response,"data" )); + data =qdict_get_qdict (response,"data" ); +g_assert (qdict_haskey (data,"guest" )); +g_assert (qdict_get_bool (data,"guest" )); +g_assert (qdict_haskey (data,"reason" )); + g_assert_cmpstr(qdict_get_str(data, "reason"), ==, "guest-shutdown"); +qobject_unref (response); + +qtest_quit (qts); +} +int main (int argc,char **argv) {g_test_init (&argc, &argv,NULL );qtest_add_func ("/pvpanic/panic" , test_panic);qtest_add_func ("/pvpanic/panic-nopause" , test_panic_nopause); +qtest_add_func ("/pvpanic/pvshutdown" , test_pvshutdown);return g_test_run (); }
Signature · 1 line
2.43.0