Re: [PATCH v3 1/3] docs/interop/firmware.json: add new enum FirmwareFormat

2024-03-28 Thread Thomas Weißschuh
Hi Hanna and Kevin,

any updates?

On Mon, Mar 11, 2024 at 02:29:25PM +0100, Markus Armbruster wrote:
> Thomas Weißschuh  writes:
> 
> > Only a small subset of all blockdev drivers make sense for firmware
> > images. Introduce and use a new enum to represent this.
> >
> > This also reduces the dependency of firmware.json on the global qapi
> > definitions.
> >
> > Suggested-by: Daniel P. Berrangé 
> > Signed-off-by: Thomas Weißschuh 
> > ---
> >  docs/interop/firmware.json | 18 --
> >  1 file changed, 16 insertions(+), 2 deletions(-)
> >
> > diff --git a/docs/interop/firmware.json b/docs/interop/firmware.json
> > index 54a1fc6c1041..0e619e8780e7 100644
> > --- a/docs/interop/firmware.json
> > +++ b/docs/interop/firmware.json
> > @@ -15,7 +15,11 @@
> >  ##
> >  
> >  { 'include' : 'machine.json' }
> > -{ 'include' : 'block-core.json' }
> > +
> > +{ 'pragma': {
> > +'documentation-exceptions': [
> > +'FirmwareFormat'
> > +] } }
> 
> Necessary because ...
> >  
> >  ##
> >  # @FirmwareOSInterface:
> > @@ -200,6 +204,16 @@
> >   'enrolled-keys', 'requires-smm', 'secure-boot',
> >   'verbose-dynamic', 'verbose-static' ] }
> >  
> > +##
> > +# @FirmwareFormat:
> > +#
> > +# Formats that are supported for firmware images.
> > +#
> 
> ... we don't document the members here.  Documenting them would be
> nicer.  We'd do that if we could steal it from BlockdevDriver, but
> there's nothing to steal there.
> 
> Mere observation; I'm not asking you to come up with documentation
> BlockdevDriver doesn't have.
> 
> > +# Since: 8.3
> > +##
> > +{ 'enum': 'FirmwareFormat',
> > +  'data': [ 'raw', 'qcow2' ] }
> 
> @raw or @file?  Kevin or Hanna, thoughts?

The existing descriptors in pc-bios/descriptors/ are using @raw.

> > +
> >  ##
> >  # @FirmwareFlashFile:
> >  #
> > @@ -219,7 +233,7 @@
> >  ##
> >  { 'struct' : 'FirmwareFlashFile',
> >'data'   : { 'filename' : 'str',
> > -   'format'   : 'BlockdevDriver' } }
> > +   'format'   : 'FirmwareFormat' } }
> >  
> >  
> >  ##

Thanks,
Thomas



[PATCH v7 4/7] hw/misc/pvpanic: add support for normal shutdowns

2024-03-23 Thread Thomas Weißschuh
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.

Acked-by: Cornelia Huck 
Signed-off-by: Thomas Weißschuh 
---
 hw/misc/pvpanic.c | 5 +
 include/hw/misc/pvpanic.h | 2 +-
 include/sysemu/runstate.h | 1 +
 system/runstate.c | 5 +
 4 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/hw/misc/pvpanic.c b/hw/misc/pvpanic.c
index a4982cc5928e..0e9505451a7a 100644
--- a/hw/misc/pvpanic.c
+++ b/hw/misc/pvpanic.c
@@ -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 */
diff --git a/include/hw/misc/pvpanic.h b/include/hw/misc/pvpanic.h
index 926aa64838f9..9ffb08bf08bf 100644
--- a/include/hw/misc/pvpanic.h
+++ b/include/hw/misc/pvpanic.h
@@ -25,7 +25,7 @@
 #endif
 #define PVPANIC_SHUTDOWN   (1 << 2)
 
-#define PVPANIC_EVENTS (PVPANIC_PANICKED | PVPANIC_CRASH_LOADED)
+#define PVPANIC_EVENTS (PVPANIC_PANICKED | PVPANIC_CRASH_LOADED | 
PVPANIC_SHUTDOWN)
 
 #define TYPE_PVPANIC_ISA_DEVICE "pvpanic"
 #define TYPE_PVPANIC_PCI_DEVICE "pvpanic-pci"
diff --git a/include/sysemu/runstate.h b/include/sysemu/runstate.h
index 0117d243c4ed..e210a37abf0f 100644
--- a/include/sysemu/runstate.h
+++ b/include/sysemu/runstate.h
@@ -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
diff --git a/system/runstate.c b/system/runstate.c
index d6ab860ecaa7..572499513034 100644
--- a/system/runstate.c
+++ b/system/runstate.c
@@ -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 &&

-- 
2.44.0




[PATCH v7 2/7] tests/qtest/pvpanic: use centralized definition of supported events

2024-03-23 Thread Thomas Weißschuh
Avoid the necessity to update all tests when new events are added
to the device.

Acked-by: Thomas Huth 
Reviewed-by: Cornelia Huck 
Signed-off-by: Thomas Weißschuh 
---
 tests/qtest/pvpanic-pci-test.c | 5 +++--
 tests/qtest/pvpanic-test.c | 5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/tests/qtest/pvpanic-pci-test.c b/tests/qtest/pvpanic-pci-test.c
index 2c05b376ba72..b372caf41dc0 100644
--- a/tests/qtest/pvpanic-pci-test.c
+++ b/tests/qtest/pvpanic-pci-test.c
@@ -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, , sizeof(val));
-g_assert_cmpuint(val, ==, 3);
+g_assert_cmpuint(val, ==, PVPANIC_EVENTS);
 
 val = 1;
 qpci_memwrite(dev, bar, 0, , sizeof(val));
@@ -67,7 +68,7 @@ static void test_panic(void)
 bar = qpci_iomap(dev, 0, NULL);
 
 qpci_memread(dev, bar, 0, , sizeof(val));
-g_assert_cmpuint(val, ==, 3);
+g_assert_cmpuint(val, ==, PVPANIC_EVENTS);
 
 val = 1;
 qpci_memwrite(dev, bar, 0, , sizeof(val));
diff --git a/tests/qtest/pvpanic-test.c b/tests/qtest/pvpanic-test.c
index 78f1cf8186b0..ccc603472f5d 100644
--- a/tests/qtest/pvpanic-test.c
+++ b/tests/qtest/pvpanic-test.c
@@ -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);
 

-- 
2.44.0




[PATCH v7 7/7] Revert "docs/specs/pvpanic: mark shutdown event as not implemented"

2024-03-23 Thread Thomas Weißschuh
The missing functionality has been implemented now.

This reverts commit e739d1935c461d0668057e9dbba9d06f728d29ec.

Signed-off-by: Thomas Weißschuh 
---
 docs/specs/pvpanic.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/docs/specs/pvpanic.rst b/docs/specs/pvpanic.rst
index b0f27860ec3b..61a80480edb8 100644
--- a/docs/specs/pvpanic.rst
+++ b/docs/specs/pvpanic.rst
@@ -29,7 +29,7 @@ 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 (to be implemented)
+bit 2
   a regular guest shutdown has happened and should be processed by the host
 
 PCI Interface

-- 
2.44.0




[PATCH v7 1/7] hw/misc/pvpanic: centralize definition of supported events

2024-03-23 Thread Thomas Weißschuh
The 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.

Reviewed-by: Thomas Huth 
Reviewed-by: Cornelia Huck 
Signed-off-by: Thomas Weißschuh 
---
 hw/misc/pvpanic-isa.c | 3 +--
 hw/misc/pvpanic-pci.c | 3 +--
 hw/misc/pvpanic.c | 3 +--
 include/hw/misc/pvpanic.h | 4 
 4 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/hw/misc/pvpanic-isa.c b/hw/misc/pvpanic-isa.c
index ccec50f61bbd..9a923b786907 100644
--- a/hw/misc/pvpanic-isa.c
+++ b/hw/misc/pvpanic-isa.c
@@ -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)
@@ -102,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(),
 };
 
diff --git a/hw/misc/pvpanic-pci.c b/hw/misc/pvpanic-pci.c
index 83be95d0d249..603c5c7600da 100644
--- a/hw/misc/pvpanic-pci.c
+++ b/hw/misc/pvpanic-pci.c
@@ -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)
 
@@ -55,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(),
 };
 
diff --git a/hw/misc/pvpanic.c b/hw/misc/pvpanic.c
index 1540e9091a45..a4982cc5928e 100644
--- a/hw/misc/pvpanic.c
+++ b/hw/misc/pvpanic.c
@@ -21,13 +21,12 @@
 #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)
 {
 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;
 }
diff --git a/include/hw/misc/pvpanic.h b/include/hw/misc/pvpanic.h
index fab94165d03d..947468b81b1a 100644
--- a/include/hw/misc/pvpanic.h
+++ b/include/hw/misc/pvpanic.h
@@ -18,6 +18,10 @@
 #include "exec/memory.h"
 #include "qom/object.h"
 
+#include "standard-headers/linux/pvpanic.h"
+
+#define PVPANIC_EVENTS (PVPANIC_PANICKED | PVPANIC_CRASH_LOADED)
+
 #define TYPE_PVPANIC_ISA_DEVICE "pvpanic"
 #define TYPE_PVPANIC_PCI_DEVICE "pvpanic-pci"
 

-- 
2.44.0




[PATCH v7 3/7] hw/misc/pvpanic: add local definition for PVPANIC_SHUTDOWN

2024-03-23 Thread Thomas Weißschuh
PVPANIC_* defines are imported from the kernel via
standard-header/pvpanic.h.
For that the kernel needs to pick up the changes from
qemu docs/specs/pvpanic.rst which takes time.

The actual value of the define is known as the authoritative source
comes from the qemu tree in docs/specs/pvpanic.rst, where it was added
in commit 73279cecca03 ("docs/specs/pvpanic: document shutdown event").

Signed-off-by: Thomas Weißschuh 
---
 include/hw/misc/pvpanic.h | 5 +
 1 file changed, 5 insertions(+)

diff --git a/include/hw/misc/pvpanic.h b/include/hw/misc/pvpanic.h
index 947468b81b1a..926aa64838f9 100644
--- a/include/hw/misc/pvpanic.h
+++ b/include/hw/misc/pvpanic.h
@@ -20,6 +20,11 @@
 
 #include "standard-headers/linux/pvpanic.h"
 
+#ifdef PVPANIC_SHUTDOWN
+#error PVPANIC_SHUTDOWN is already defined
+#endif
+#define PVPANIC_SHUTDOWN   (1 << 2)
+
 #define PVPANIC_EVENTS (PVPANIC_PANICKED | PVPANIC_CRASH_LOADED)
 
 #define TYPE_PVPANIC_ISA_DEVICE "pvpanic"

-- 
2.44.0




[PATCH v7 6/7] tests/qtest/pvpanic: add tests for pvshutdown event

2024-03-23 Thread Thomas Weißschuh
Validate that a shutdown via the pvpanic device emits the correct
QMP events.

Signed-off-by: Thomas Weißschuh 
Reviewed-by: Thomas Huth 
---
 tests/qtest/pvpanic-pci-test.c | 39 +++
 tests/qtest/pvpanic-test.c | 29 +
 2 files changed, 68 insertions(+)

diff --git a/tests/qtest/pvpanic-pci-test.c b/tests/qtest/pvpanic-pci-test.c
index b372caf41dc0..dc021c2fdf77 100644
--- a/tests/qtest/pvpanic-pci-test.c
+++ b/tests/qtest/pvpanic-pci-test.c
@@ -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, , sizeof(val));
+g_assert_cmpuint(val, ==, PVPANIC_EVENTS);
+
+val = PVPANIC_SHUTDOWN;
+qpci_memwrite(dev, bar, 0, , 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(, , 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();
 }
diff --git a/tests/qtest/pvpanic-test.c b/tests/qtest/pvpanic-test.c
index ccc603472f5d..d49d2ba9313e 100644
--- a/tests/qtest/pvpanic-test.c
+++ b/tests/qtest/pvpanic-test.c
@@ -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(, , 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();
 }

-- 
2.44.0




[PATCH v7 0/7] hw/misc/pvpanic: add support for normal shutdowns

2024-03-23 Thread Thomas Weißschuh
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.

Patch 4 adds a qemu-internal definition PVPANIC_SHUTDOWN.
It should be removed as soon as the kernel exposes the define and the
the new kernel header has been reimported into qemu.

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/20231104-pvpanic-shutdown-v1-1-5ee7c9b3e...@weissschuh.net/

Signed-off-by: Thomas Weißschuh 
---
Changes in v7:
- Keep standard-header/pvpanic.h
- Predefine PVPANIC_SHUTDOWN in include/hw/misc/pvpanic.h
- Fix alignment in QAPI to comply with newly enforced layout
- Update Since: tag in QAPI to 9.0
- Drop note from pvpanic spec about missing implementation
- Link to v6: 
https://lore.kernel.org/r/20240208-pvpanic-shutdown-v6-0-965580ac0...@t-8ch.de

Changes in v6:
- Replace magic constant "4" in tests with PVPANIC_SHUTDOWN
- Link to v5: 
https://lore.kernel.org/r/20240129-pvpanic-shutdown-v5-0-f5a060b87...@t-8ch.de

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/20240107-pvpanic-shutdown-v4-0-81500a7e4...@t-8ch.de

Changes in v4:
- Rebase on 8.2 master
- Resend after tree reopened and holidays
- Link to v3: 
https://lore.kernel.org/r/20231129-pvpanic-shutdown-v3-0-c9a2892fc...@t-8ch.de

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/20231128-pvpanic-shutdown-v2-0-830393b45...@t-8ch.de

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/20231104-pvpanic-shutdown-v1-0-023531578...@t-8ch.de

---
Alejandro Jimenez (1):
  pvpanic: Emit GUEST_PVSHUTDOWN QMP event on pvpanic shutdown signal

Thomas Weißschuh (6):
  hw/misc/pvpanic: centralize definition of supported events
  tests/qtest/pvpanic: use centralized definition of supported events
  hw/misc/pvpanic: add local definition for PVPANIC_SHUTDOWN
  hw/misc/pvpanic: add support for normal shutdowns
  tests/qtest/pvpanic: add tests for pvshutdown event
  Revert "docs/specs/pvpanic: mark shutdown event as not implemented"

 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  |  9 +
 include/sysemu/runstate.h  |  1 +
 qapi/run-state.json| 14 ++
 system/runstate.c  |  6 ++
 tests/qtest/pvpanic-pci-test.c | 44 --
 tests/qtest/pvpanic-test.c | 34 ++--
 10 files changed, 113 insertions(+), 11 deletions(-)
---
base-commit: 853546f8128476eefb701d4a55b2781bb3a46faa
change-id: 20231104-pvpanic-shutdown-02e4b4cb4949

Best regards,
-- 
Thomas Weißschuh 




[PATCH v7 5/7] pvpanic: Emit GUEST_PVSHUTDOWN QMP event on pvpanic shutdown signal

2024-03-23 Thread Thomas Weißschuh
From: Alejandro Jimenez 

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.

Signed-off-by: Alejandro Jimenez 
---
 qapi/run-state.json | 14 ++
 system/runstate.c   |  1 +
 2 files changed, 15 insertions(+)

diff --git a/qapi/run-state.json b/qapi/run-state.json
index 789fc34559ad..888a11e62011 100644
--- a/qapi/run-state.json
+++ b/qapi/run-state.json
@@ -455,6 +455,20 @@
 { 'event': 'GUEST_CRASHLOADED',
   'data': { 'action': 'GuestPanicAction', '*info': 'GuestPanicInformation' } }
 
+##
+# @GUEST_PVSHUTDOWN:
+#
+# Emitted when guest submits a shutdown request via pvpanic interface
+#
+# Since: 9.0
+#
+# Example:
+#
+# <- { "event": "GUEST_PVSHUTDOWN",
+#  "timestamp": { "seconds": 1648245259, "microseconds": 893771 } }
+##
+{ 'event': 'GUEST_PVSHUTDOWN' }
+
 ##
 # @GuestPanicAction:
 #
diff --git a/system/runstate.c b/system/runstate.c
index 572499513034..02b0a1f8b9d0 100644
--- a/system/runstate.c
+++ b/system/runstate.c
@@ -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);
 }
 

-- 
2.44.0




Re: [PATCH] hw/virtio: Add support for VDPA network simulation devices

2024-03-13 Thread Thomas Weißschuh
On 2024-02-21 15:38:02+0800, Hao Chen wrote:
> This patch adds support for VDPA network simulation devices.
> The device is developed based on virtio-net and tap backend,
> and supports hardware live migration function.
> 
> For more details, please refer to "docs/system/devices/vdpa-net.rst"
> 
> Signed-off-by: Hao Chen 
> ---
>  MAINTAINERS |   5 +
>  docs/system/device-emulation.rst|   1 +
>  docs/system/devices/vdpa-net.rst| 121 +
>  hw/net/virtio-net.c |  16 ++
>  hw/virtio/virtio-pci.c  | 189 +++-
>  hw/virtio/virtio.c  |  39 
>  include/hw/virtio/virtio-pci.h  |   5 +
>  include/hw/virtio/virtio.h  |  19 ++
>  include/standard-headers/linux/virtio_pci.h |   7 +
>  9 files changed, 399 insertions(+), 3 deletions(-)
>  create mode 100644 docs/system/devices/vdpa-net.rst

[..]

> diff --git a/include/standard-headers/linux/virtio_pci.h 
> b/include/standard-headers/linux/virtio_pci.h
> index b7fdfd0668..fb5391cef6 100644
> --- a/include/standard-headers/linux/virtio_pci.h
> +++ b/include/standard-headers/linux/virtio_pci.h
> @@ -216,6 +216,13 @@ struct virtio_pci_cfg_cap {
>  #define VIRTIO_PCI_COMMON_Q_NDATA56
>  #define VIRTIO_PCI_COMMON_Q_RESET58
>  
> +#define LM_LOGGING_CTRL 0
> +#define LM_BASE_ADDR_LOW4
> +#define LM_BASE_ADDR_HIGH   8
> +#define LM_END_ADDR_LOW 12
> +#define LM_END_ADDR_HIGH16
> +#define LM_VRING_STATE_OFFSET   0x20

These changes are not in upstream Linux and will be undone by
./scripts/update-linux-headers.sh.

Are they intentionally in this header?

> +
>  #endif /* VIRTIO_PCI_NO_MODERN */
>  
>  #endif



[PATCH] docs/specs/pvpanic: mark shutdown event as not implemented

2024-03-13 Thread Thomas Weißschuh
Mention the fact that this event is not yet implemented
to avoid confusion.
As requested by Michael.

Signed-off-by: Thomas Weißschuh 
---
 docs/specs/pvpanic.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/docs/specs/pvpanic.rst b/docs/specs/pvpanic.rst
index 61a80480edb8..b0f27860ec3b 100644
--- a/docs/specs/pvpanic.rst
+++ b/docs/specs/pvpanic.rst
@@ -29,7 +29,7 @@ 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
+bit 2 (to be implemented)
   a regular guest shutdown has happened and should be processed by the host
 
 PCI Interface

---
base-commit: ba49d760eb04630e7b15f423ebecf6c871b8f77b
change-id: 20240313-pvpanic-note-fa3ce8d2165a

Best regards,
-- 
Thomas Weißschuh 




Re: [PATCH v3 1/3] docs/interop/firmware.json: add new enum FirmwareFormat

2024-03-11 Thread Thomas Weißschuh
On Mon, Mar 11, 2024 at 01:08:19PM +0100, Philippe Mathieu-Daudé wrote:
> On 11/3/24 12:46, Thomas Weißschuh wrote:
> > Only a small subset of all blockdev drivers make sense for firmware
> > images. Introduce and use a new enum to represent this.
> > 
> > This also reduces the dependency of firmware.json on the global qapi
> > definitions.
> > 
> > Suggested-by: Daniel P. Berrangé 
> > Signed-off-by: Thomas Weißschuh 
> > ---
> >   docs/interop/firmware.json | 18 --
> >   1 file changed, 16 insertions(+), 2 deletions(-)
> 
> 
> > +##
> > +# @FirmwareFormat:
> > +#
> > +# Formats that are supported for firmware images.
> > +#
> > +# Since: 8.3
> 
> That will be 9.0 :/

Thanks.

IMO it doesn't warrant a new revision, the maintainer can change it when
applying.

Locally I changed it, so if a new revision is necessary for other
reasons it will have the change.

> > +##
> > +{ 'enum': 'FirmwareFormat',
> > +  'data': [ 'raw', 'qcow2' ] }
> 



[PATCH v3 3/3] docs: add test for firmware.json QAPI

2024-03-11 Thread Thomas Weißschuh
To make sure that the QAPI description stays valid add a testcase.

Suggested-by: Philippe Mathieu-Daudé 
Link: 
https://lore.kernel.org/qemu-devel/d9ce0234-4beb-4b90-b14c-76810d3b8...@linaro.org/
Signed-off-by: Thomas Weißschuh 
---
 docs/meson.build | 5 +
 1 file changed, 5 insertions(+)

diff --git a/docs/meson.build b/docs/meson.build
index 9040f860ae1a..bcca45a342a3 100644
--- a/docs/meson.build
+++ b/docs/meson.build
@@ -99,3 +99,8 @@ if build_docs
   alias_target('html', sphinxdocs)
   alias_target('man', sphinxmans)
 endif
+
+test('QAPI firmware.json regression tests', python,
+ args: [qapi_gen.full_path(), '-o', meson.current_build_dir() / 'qapi',
+meson.current_source_dir() / 'interop/firmware.json'],
+ env: test_env, suite: ['qapi-schema', 'qapi-interop'])

-- 
2.44.0




[PATCH v3 1/3] docs/interop/firmware.json: add new enum FirmwareFormat

2024-03-11 Thread Thomas Weißschuh
Only a small subset of all blockdev drivers make sense for firmware
images. Introduce and use a new enum to represent this.

This also reduces the dependency of firmware.json on the global qapi
definitions.

Suggested-by: Daniel P. Berrangé 
Signed-off-by: Thomas Weißschuh 
---
 docs/interop/firmware.json | 18 --
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/docs/interop/firmware.json b/docs/interop/firmware.json
index 54a1fc6c1041..0e619e8780e7 100644
--- a/docs/interop/firmware.json
+++ b/docs/interop/firmware.json
@@ -15,7 +15,11 @@
 ##
 
 { 'include' : 'machine.json' }
-{ 'include' : 'block-core.json' }
+
+{ 'pragma': {
+'documentation-exceptions': [
+'FirmwareFormat'
+] } }
 
 ##
 # @FirmwareOSInterface:
@@ -200,6 +204,16 @@
  'enrolled-keys', 'requires-smm', 'secure-boot',
  'verbose-dynamic', 'verbose-static' ] }
 
+##
+# @FirmwareFormat:
+#
+# Formats that are supported for firmware images.
+#
+# Since: 8.3
+##
+{ 'enum': 'FirmwareFormat',
+  'data': [ 'raw', 'qcow2' ] }
+
 ##
 # @FirmwareFlashFile:
 #
@@ -219,7 +233,7 @@
 ##
 { 'struct' : 'FirmwareFlashFile',
   'data'   : { 'filename' : 'str',
-   'format'   : 'BlockdevDriver' } }
+   'format'   : 'FirmwareFormat' } }
 
 
 ##

-- 
2.44.0




[PATCH v3 0/3] docs/interop/firmware.json: scripts/qapi-gen.py compatibility

2024-03-11 Thread Thomas Weißschuh
docs/interop/firmware.json is currently not usable with qapi-gen.py due
to various non-functional issues.
Fix those issue to provide compatibility.

Signed-off-by: Thomas Weißschuh 
---
Changes in v3:
- Drop already picked up patches
- Drop include of pragma.json
- Introduce new enums FirmwareFormat and FirmwareArchitecture
- Link to v2: 
https://lore.kernel.org/r/20240307-qapi-firmware-json-v2-0-3b29eabb9...@linutronix.de

Changes in v2:
- Add review tag from Philippe
- Add Fixes tag (Philippe)
- Add testcase (Philippe)
- Link to v1: 
https://lore.kernel.org/r/20240306-qapi-firmware-json-v1-0-619f7122a...@linutronix.de

---
Thomas Weißschuh (3):
  docs/interop/firmware.json: add new enum FirmwareFormat
  docs/interop/firmware.json: add new enum FirmwareArchitecture
  docs: add test for firmware.json QAPI

 docs/interop/firmware.json | 36 
 docs/meson.build   |  5 +
 2 files changed, 37 insertions(+), 4 deletions(-)
---
base-commit: 7489f7f3f81dcb776df8c1b9a9db281fc21bf05f
change-id: 20240306-qapi-firmware-json-6fb1213936dd

Best regards,
-- 
Thomas Weißschuh 




[PATCH v3 2/3] docs/interop/firmware.json: add new enum FirmwareArchitecture

2024-03-11 Thread Thomas Weißschuh
Only a small subset of all architectures supported by qemu make use of
firmware files. Introduce and use a new enum to represent this.

This also removes the dependency of firmware.json on the global qapi
definitions.

Suggested-by: Daniel P. Berrangé 
Signed-off-by: Thomas Weißschuh 
---
 docs/interop/firmware.json | 20 +---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/docs/interop/firmware.json b/docs/interop/firmware.json
index 0e619e8780e7..54cae6a51b43 100644
--- a/docs/interop/firmware.json
+++ b/docs/interop/firmware.json
@@ -14,11 +14,13 @@
 # = Firmware
 ##
 
-{ 'include' : 'machine.json' }
-
 { 'pragma': {
 'documentation-exceptions': [
+'FirmwareArchitecture',
 'FirmwareFormat'
+],
+'member-name-exceptions': [
+'FirmwareArchitecture'
 ] } }
 
 ##
@@ -64,6 +66,18 @@
 { 'enum' : 'FirmwareDevice',
   'data' : [ 'flash', 'kernel', 'memory' ] }
 
+##
+# @FirmwareArchitecture:
+#
+# Enumerations of architectures for which Qemu uses additional firmware files.
+# The values are a subset of the enum SysEmuTarget.
+#
+# Since: 8.3
+##
+{ 'enum' : 'FirmwareArchitecture',
+  'data' : [ 'aarch64', 'arm', 'i386', 'loongarch64', 'x86_64' ] }
+
+
 ##
 # @FirmwareTarget:
 #
@@ -85,7 +99,7 @@
 # Since: 3.0
 ##
 { 'struct' : 'FirmwareTarget',
-  'data'   : { 'architecture' : 'SysEmuTarget',
+  'data'   : { 'architecture' : 'FirmwareArchitecture',
'machines' : [ 'str' ] } }
 
 ##

-- 
2.44.0




[PATCH] docs/specs/pvpanic: document shutdown event

2024-03-10 Thread Thomas Weißschuh
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.

Signed-off-by: Thomas Weißschuh 
---
This patch was split out from my earlier pvpanic-shutdown series [0].
The original series dropped the usage of the linux/pvpanic.h UAPI
header.
As we decided to keep the linux header [1] this first commit only adds
the spec definition.
Then a patch to Linux will add the new events to linux/pvpanic.h.
After this the rest of the changes to QEMU will be submitted again.

[0] 
https://lore.kernel.org/qemu-devel/20240208-pvpanic-shutdown-v6-0-965580ac0...@t-8ch.de/
[1] https://lore.kernel.org/lkml/20240213053953-mutt-send-email-...@kernel.org/
---
 docs/specs/pvpanic.rst | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/docs/specs/pvpanic.rst b/docs/specs/pvpanic.rst
index f894bc19555f..61a80480edb8 100644
--- a/docs/specs/pvpanic.rst
+++ b/docs/specs/pvpanic.rst
@@ -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 regular guest shutdown has happened and should be processed by the host
 
 PCI Interface
 -

---
base-commit: f901bf11b3ddf852e591593b09b8aa7a177f9a0b
change-id: 20240310-pvpanic-shutdown-spec-4ea2172529e8

Best regards,
-- 
Thomas Weißschuh 




Re: [PATCH 3/4] docs/interop/firmware.json: Use full include paths

2024-03-08 Thread Thomas Weißschuh
On Fri, Mar 08, 2024 at 04:19:42PM +0100, Markus Armbruster wrote:
> Thomas Weißschuh  writes:
> 
> > The included files are part of the toplevel QAPI directory and need to
> > be included from there.
> >
> > Signed-off-by: Thomas Weißschuh 
> > ---
> >  docs/interop/firmware.json | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> >
> > diff --git a/docs/interop/firmware.json b/docs/interop/firmware.json
> > index 54a1fc6c1041..4ac840e2b413 100644
> > --- a/docs/interop/firmware.json
> > +++ b/docs/interop/firmware.json
> > @@ -14,8 +14,8 @@
> >  # = Firmware
> >  ##
> >  
> > -{ 'include' : 'machine.json' }
> > -{ 'include' : 'block-core.json' }
> > +{ 'include' : '../../qapi/machine.json' }
> > +{ 'include' : '../../qapi/block-core.json' }
> >  
> >  ##
> >  # @FirmwareOSInterface:
> 
> The coupling with the main QAPI schema is unfortunate.
> 
> The purpose of docs/interop/firmware.json is to serve as schema for
> firmware descriptions: a firmware description is a JSON object that
> conforms to this schema's struct Firmware.
> 
> Such a description should be installed along with each firmware binary.
> 
> QAPI tooling can be used to check conformance: parse the firmware
> description JSON object, feed it to the generated visit_type_Firmware().
> Success implies conformance.
> 
> If you find more uses for the C struct Firmware created by
> visit_type_Firmware(), more power to you.

I am writing a tool "qemu-firmware" that can be used to query and
introspect those installed JSON description files.
This is where my changes are coming from.

That tool is meant to be pushed to qemu upstream but it's not ready yet.

> firmware.json needs machine.json for SysEmuTarget, and block-core.json
> for BlockdevDriver.  The largest and the third-largest QAPI module just
> for two enums.  Almost a quarter Mebibyte of code.
> 
> qapi-gen.py generates more than 12kSLOC.  Without the include (and with
> the enums dumbed down to 'str' to enable that), it generates less than
> 800.

The generated code also doesn't link properly because it has
dependencies on various parts of qemu internals.

In my "qemu-firmware" branch I have two more commits that split
SysEmuTarget and BlockdevDriver into their own JSON files.
Then everything works nicely.
These commits were not submitted yet as they felt specific to my
usecase.

> We could use Sphinx to generate a manual from firmware.json's document.
> Except that manual would be useless, because of its 11,000 lines of
> HTML, less than 800 are for firmware.json.
> 
> Options:
> 
> * Live with the mess.
> 
> * Refactor QAPI modules so firmware.json can include just the enums.
> 
>   Drawback: we spread the mess into qapi/.  Ugh.

As mentioned above, this is what I ended up doing and which I prefer for
my usecase.

> * Copy the enums to firmware.json.
> 
>   Drawback: we risk them going stale.
> 
> * Dumb down to 'str'.
> 
>   Drawback: the conformance check no longer enforces the value of
>   FirmwareTarget member @architecture and FirmwareFlashFile member
>   @format is valid.
> 
> Thoughts?



[PATCH v2 0/5] docs/interop/firmware.json: scripts/qapi-gen.py compatibility

2024-03-07 Thread Thomas Weißschuh
docs/interop/firmware.json is currently not usable with qapi-gen.py due
to various non-functional issues.
Fix those issue to provide compatibility.

Signed-off-by: Thomas Weißschuh 
---
Changes in v2:
- Add review tag from Philippe
- Add Fixes tag (Philippe)
- Add testcase (Philippe)
- Link to v1: 
https://lore.kernel.org/r/20240306-qapi-firmware-json-v1-0-619f7122a...@linutronix.de

---
Thomas Weißschuh (5):
  docs/interop/firmware.json: Align examples
  docs/interop/firmware.json: Fix doc for FirmwareFlashMode
  docs/interop/firmware.json: Use full include paths
  docs/interop/firmware.json: Include pragma.json
  docs: add test for firmware.json QAPI

 docs/interop/firmware.json | 392 +++--
 docs/meson.build   |   5 +
 2 files changed, 202 insertions(+), 195 deletions(-)
---
base-commit: 8f6330a807f2642dc2a3cdf33347aa28a4c00a87
change-id: 20240306-qapi-firmware-json-6fb1213936dd

Best regards,
-- 
Thomas Weißschuh 




[PATCH v2 5/5] docs: add test for firmware.json QAPI

2024-03-07 Thread Thomas Weißschuh
To make sure that the QAPI description stays valid add a testcase.

Suggested-by: Philippe Mathieu-Daudé 
Link: 
https://lore.kernel.org/qemu-devel/d9ce0234-4beb-4b90-b14c-76810d3b8...@linaro.org/
Signed-off-by: Thomas Weißschuh 
---
 docs/meson.build | 5 +
 1 file changed, 5 insertions(+)

diff --git a/docs/meson.build b/docs/meson.build
index 9040f860ae1a..bcca45a342a3 100644
--- a/docs/meson.build
+++ b/docs/meson.build
@@ -99,3 +99,8 @@ if build_docs
   alias_target('html', sphinxdocs)
   alias_target('man', sphinxmans)
 endif
+
+test('QAPI firmware.json regression tests', python,
+ args: [qapi_gen.full_path(), '-o', meson.current_build_dir() / 'qapi',
+meson.current_source_dir() / 'interop/firmware.json'],
+ env: test_env, suite: ['qapi-schema', 'qapi-interop'])

-- 
2.44.0




[PATCH v2 4/5] docs/interop/firmware.json: Include pragma.json

2024-03-07 Thread Thomas Weißschuh
The files included by firmware.json use names that do not satisfy the
generators requirements.
By including pragma.json these errors are suppressed.

Signed-off-by: Thomas Weißschuh 
---
 docs/interop/firmware.json | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/docs/interop/firmware.json b/docs/interop/firmware.json
index 4ac840e2b413..a7e9a22a0046 100644
--- a/docs/interop/firmware.json
+++ b/docs/interop/firmware.json
@@ -10,6 +10,8 @@
 # This work is licensed under the terms of the GNU GPL, version 2 or
 # later. See the COPYING file in the top-level directory.
 
+{ 'include' : '../../qapi/pragma.json' }
+
 ##
 # = Firmware
 ##

-- 
2.44.0




[PATCH v2 3/5] docs/interop/firmware.json: Use full include paths

2024-03-07 Thread Thomas Weißschuh
The included files are part of the toplevel QAPI directory and need to
be included from there.

Signed-off-by: Thomas Weißschuh 
---
 docs/interop/firmware.json | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/docs/interop/firmware.json b/docs/interop/firmware.json
index 54a1fc6c1041..4ac840e2b413 100644
--- a/docs/interop/firmware.json
+++ b/docs/interop/firmware.json
@@ -14,8 +14,8 @@
 # = Firmware
 ##
 
-{ 'include' : 'machine.json' }
-{ 'include' : 'block-core.json' }
+{ 'include' : '../../qapi/machine.json' }
+{ 'include' : '../../qapi/block-core.json' }
 
 ##
 # @FirmwareOSInterface:

-- 
2.44.0




[PATCH v2 1/5] docs/interop/firmware.json: Align examples

2024-03-07 Thread Thomas Weißschuh
The QAPI generator now validates the alignment and rejects this file.

Signed-off-by: Thomas Weißschuh 
---
 docs/interop/firmware.json | 384 ++---
 1 file changed, 192 insertions(+), 192 deletions(-)

diff --git a/docs/interop/firmware.json b/docs/interop/firmware.json
index cc8f86918681..a024f1b9bf3f 100644
--- a/docs/interop/firmware.json
+++ b/docs/interop/firmware.json
@@ -435,203 +435,203 @@
 #
 # Examples:
 #
-# {
-# "description": "SeaBIOS",
-# "interface-types": [
-# "bios"
-# ],
-# "mapping": {
-# "device": "memory",
-# "filename": "/usr/share/seabios/bios-256k.bin"
-# },
-# "targets": [
-# {
-# "architecture": "i386",
-# "machines": [
-# "pc-i440fx-*",
-# "pc-q35-*"
-# ]
+# {
+# "description": "SeaBIOS",
+# "interface-types": [
+# "bios"
+# ],
+# "mapping": {
+# "device": "memory",
+# "filename": "/usr/share/seabios/bios-256k.bin"
 # },
-# {
-# "architecture": "x86_64",
-# "machines": [
-# "pc-i440fx-*",
-# "pc-q35-*"
-# ]
-# }
-# ],
-# "features": [
-# "acpi-s3",
-# "acpi-s4"
-# ],
-# "tags": [
-# "CONFIG_BOOTSPLASH=n",
-# "CONFIG_ROM_SIZE=256",
-# "CONFIG_USE_SMM=n"
-# ]
-# }
-#
-# {
-# "description": "OVMF with SB+SMM, empty varstore",
-# "interface-types": [
-# "uefi"
-# ],
-# "mapping": {
-# "device": "flash",
-# "executable": {
-# "filename": "/usr/share/OVMF/OVMF_CODE.secboot.fd",
-# "format": "raw"
+# "targets": [
+# {
+# "architecture": "i386",
+# "machines": [
+# "pc-i440fx-*",
+# "pc-q35-*"
+# ]
+# },
+# {
+# "architecture": "x86_64",
+# "machines": [
+# "pc-i440fx-*",
+# "pc-q35-*"
+# ]
+# }
+# ],
+# "features": [
+# "acpi-s3",
+# "acpi-s4"
+# ],
+# "tags": [
+# "CONFIG_BOOTSPLASH=n",
+# "CONFIG_ROM_SIZE=256",
+# "CONFIG_USE_SMM=n"
+# ]
+# }
+#
+# {
+# "description": "OVMF with SB+SMM, empty varstore",
+# "interface-types": [
+# "uefi"
+# ],
+# "mapping": {
+# "device": "flash",
+# "executable": {
+# "filename": "/usr/share/OVMF/OVMF_CODE.secboot.fd",
+# "format": "raw"
+# },
+# "nvram-template": {
+# "filename": "/usr/share/OVMF/OVMF_VARS.fd",
+# "format": "raw"
+# }
 # },
-# "nvram-template": {
-# "filename": "/usr/share/OVMF/OVMF_VARS.fd",
-# "format": "raw"
-# }
-# },
-# "targets": [
-# {
-# "architecture": "x86_64",
-# "machines": [
-# "pc-q35-*"
-# ]
-# }
-# ],
-# "features": [
-# "acpi-s3",
-# "amd-sev",
-# "requires-smm",
-# "secure-boot",
-# "verbose-dynamic"
-# ],
-# "tags": [
-# "-a IA32",
-# "-a X64",
-# "-p OvmfPkg/OvmfPkgIa32X64.dsc",
-# "-t GCC48",
-# "-b DEBUG",
-# "-D SMM_REQUIRE",
-# "-D SECURE_BOOT_ENABLE",
-# "-D FD_SIZE_4MB"
-# ]
-# }
-#
-# {
-# "description": "OVMF with SB+SMM, SB enabled, MS certs enrolled",
-# "interface-types": [
-# "uefi&quo

[PATCH v2 2/5] docs/interop/firmware.json: Fix doc for FirmwareFlashMode

2024-03-07 Thread Thomas Weißschuh
The doc title did not match the actual definition.

Fixes: 2720ceda05 ("docs: expand firmware descriptor to allow flash without 
NVRAM")
Signed-off-by: Thomas Weißschuh 
Reviewed-by: Philippe Mathieu-Daudé 
---
 docs/interop/firmware.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/docs/interop/firmware.json b/docs/interop/firmware.json
index a024f1b9bf3f..54a1fc6c1041 100644
--- a/docs/interop/firmware.json
+++ b/docs/interop/firmware.json
@@ -223,7 +223,7 @@
 
 
 ##
-# @FirmwareFlashType:
+# @FirmwareFlashMode:
 #
 # Describes how the firmware build handles code versus variable
 # persistence.

-- 
2.44.0




Re: [PATCH 0/4] docs/interop/firmware.json: scripts/qapi-gen.py compatibility

2024-03-06 Thread Thomas Weißschuh
On Wed, Mar 06, 2024 at 05:15:34PM +0100, Philippe Mathieu-Daudé wrote:
> On 6/3/24 11:31, Thomas Weißschuh wrote:
> > docs/interop/firmware.json is currently not usable with qapi-gen.py due
> > to various non-functional issues.
> > Fix those issue to provide compatibility.
> 
> Could we add some lines in docs/meson.build to cover this files
> during our CI tests?

Sounds good.

I am also developing a new tool which would use the generated QAPI
files which I plan to submit soonish.

> > 
> > Signed-off-by: Thomas Weißschuh 
> > ---
> > Thomas Weißschuh (4):
> >docs/interop/firmware.json: Align examples
> >docs/interop/firmware.json: Fix doc for FirmwareFlashMode
> >docs/interop/firmware.json: Use full include paths
> >docs/interop/firmware.json: Include pragma.json
> > 
> >   docs/interop/firmware.json | 392 
> > +++--
> >   1 file changed, 197 insertions(+), 195 deletions(-)
> > ---
> > base-commit: db596ae19040574e41d086e78469014191d7d7fc
> > change-id: 20240306-qapi-firmware-json-6fb1213936dd
> > 
> > Best regards,



[PATCH 3/4] docs/interop/firmware.json: Use full include paths

2024-03-06 Thread Thomas Weißschuh
The included files are part of the toplevel QAPI directory and need to
be included from there.

Signed-off-by: Thomas Weißschuh 
---
 docs/interop/firmware.json | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/docs/interop/firmware.json b/docs/interop/firmware.json
index 54a1fc6c1041..4ac840e2b413 100644
--- a/docs/interop/firmware.json
+++ b/docs/interop/firmware.json
@@ -14,8 +14,8 @@
 # = Firmware
 ##
 
-{ 'include' : 'machine.json' }
-{ 'include' : 'block-core.json' }
+{ 'include' : '../../qapi/machine.json' }
+{ 'include' : '../../qapi/block-core.json' }
 
 ##
 # @FirmwareOSInterface:

-- 
2.44.0




[PATCH 1/4] docs/interop/firmware.json: Align examples

2024-03-06 Thread Thomas Weißschuh
The QAPI generator now validates the alignment and rejects this file.

Signed-off-by: Thomas Weißschuh 
---
 docs/interop/firmware.json | 384 ++---
 1 file changed, 192 insertions(+), 192 deletions(-)

diff --git a/docs/interop/firmware.json b/docs/interop/firmware.json
index cc8f86918681..a024f1b9bf3f 100644
--- a/docs/interop/firmware.json
+++ b/docs/interop/firmware.json
@@ -435,203 +435,203 @@
 #
 # Examples:
 #
-# {
-# "description": "SeaBIOS",
-# "interface-types": [
-# "bios"
-# ],
-# "mapping": {
-# "device": "memory",
-# "filename": "/usr/share/seabios/bios-256k.bin"
-# },
-# "targets": [
-# {
-# "architecture": "i386",
-# "machines": [
-# "pc-i440fx-*",
-# "pc-q35-*"
-# ]
+# {
+# "description": "SeaBIOS",
+# "interface-types": [
+# "bios"
+# ],
+# "mapping": {
+# "device": "memory",
+# "filename": "/usr/share/seabios/bios-256k.bin"
 # },
-# {
-# "architecture": "x86_64",
-# "machines": [
-# "pc-i440fx-*",
-# "pc-q35-*"
-# ]
-# }
-# ],
-# "features": [
-# "acpi-s3",
-# "acpi-s4"
-# ],
-# "tags": [
-# "CONFIG_BOOTSPLASH=n",
-# "CONFIG_ROM_SIZE=256",
-# "CONFIG_USE_SMM=n"
-# ]
-# }
-#
-# {
-# "description": "OVMF with SB+SMM, empty varstore",
-# "interface-types": [
-# "uefi"
-# ],
-# "mapping": {
-# "device": "flash",
-# "executable": {
-# "filename": "/usr/share/OVMF/OVMF_CODE.secboot.fd",
-# "format": "raw"
+# "targets": [
+# {
+# "architecture": "i386",
+# "machines": [
+# "pc-i440fx-*",
+# "pc-q35-*"
+# ]
+# },
+# {
+# "architecture": "x86_64",
+# "machines": [
+# "pc-i440fx-*",
+# "pc-q35-*"
+# ]
+# }
+# ],
+# "features": [
+# "acpi-s3",
+# "acpi-s4"
+# ],
+# "tags": [
+# "CONFIG_BOOTSPLASH=n",
+# "CONFIG_ROM_SIZE=256",
+# "CONFIG_USE_SMM=n"
+# ]
+# }
+#
+# {
+# "description": "OVMF with SB+SMM, empty varstore",
+# "interface-types": [
+# "uefi"
+# ],
+# "mapping": {
+# "device": "flash",
+# "executable": {
+# "filename": "/usr/share/OVMF/OVMF_CODE.secboot.fd",
+# "format": "raw"
+# },
+# "nvram-template": {
+# "filename": "/usr/share/OVMF/OVMF_VARS.fd",
+# "format": "raw"
+# }
 # },
-# "nvram-template": {
-# "filename": "/usr/share/OVMF/OVMF_VARS.fd",
-# "format": "raw"
-# }
-# },
-# "targets": [
-# {
-# "architecture": "x86_64",
-# "machines": [
-# "pc-q35-*"
-# ]
-# }
-# ],
-# "features": [
-# "acpi-s3",
-# "amd-sev",
-# "requires-smm",
-# "secure-boot",
-# "verbose-dynamic"
-# ],
-# "tags": [
-# "-a IA32",
-# "-a X64",
-# "-p OvmfPkg/OvmfPkgIa32X64.dsc",
-# "-t GCC48",
-# "-b DEBUG",
-# "-D SMM_REQUIRE",
-# "-D SECURE_BOOT_ENABLE",
-# "-D FD_SIZE_4MB"
-# ]
-# }
-#
-# {
-# "description": "OVMF with SB+SMM, SB enabled, MS certs enrolled",
-# "interface-types": [
-# "uefi&quo

[PATCH 0/4] docs/interop/firmware.json: scripts/qapi-gen.py compatibility

2024-03-06 Thread Thomas Weißschuh
docs/interop/firmware.json is currently not usable with qapi-gen.py due
to various non-functional issues.
Fix those issue to provide compatibility.

Signed-off-by: Thomas Weißschuh 
---
Thomas Weißschuh (4):
  docs/interop/firmware.json: Align examples
  docs/interop/firmware.json: Fix doc for FirmwareFlashMode
  docs/interop/firmware.json: Use full include paths
  docs/interop/firmware.json: Include pragma.json

 docs/interop/firmware.json | 392 +++--
 1 file changed, 197 insertions(+), 195 deletions(-)
---
base-commit: db596ae19040574e41d086e78469014191d7d7fc
change-id: 20240306-qapi-firmware-json-6fb1213936dd

Best regards,
-- 
Thomas Weißschuh 




[PATCH 4/4] docs/interop/firmware.json: Include pragma.json

2024-03-06 Thread Thomas Weißschuh
The files included by firmware.json use names that do not satisfy the
generators requirements.
By including pragma.json these errors are suppressed.

Signed-off-by: Thomas Weißschuh 
---
 docs/interop/firmware.json | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/docs/interop/firmware.json b/docs/interop/firmware.json
index 4ac840e2b413..a7e9a22a0046 100644
--- a/docs/interop/firmware.json
+++ b/docs/interop/firmware.json
@@ -10,6 +10,8 @@
 # This work is licensed under the terms of the GNU GPL, version 2 or
 # later. See the COPYING file in the top-level directory.
 
+{ 'include' : '../../qapi/pragma.json' }
+
 ##
 # = Firmware
 ##

-- 
2.44.0




[PATCH 2/4] docs/interop/firmware.json: Fix doc for FirmwareFlashMode

2024-03-06 Thread Thomas Weißschuh
The doc title did not match the actual definition.

Signed-off-by: Thomas Weißschuh 
---
 docs/interop/firmware.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/docs/interop/firmware.json b/docs/interop/firmware.json
index a024f1b9bf3f..54a1fc6c1041 100644
--- a/docs/interop/firmware.json
+++ b/docs/interop/firmware.json
@@ -223,7 +223,7 @@
 
 
 ##
-# @FirmwareFlashType:
+# @FirmwareFlashMode:
 #
 # Describes how the firmware build handles code versus variable
 # persistence.

-- 
2.44.0




Re: [PATCH] net/slirp: Use newer slirp_*_hostxfwd API

2024-02-22 Thread Thomas Weißschuh
Hi Nicholas,

On Tue, Mar 22, 2022 at 06:58:36PM -0700, Nicholas Ngai wrote:
> Pinging this. It’s a bit old, though the patch still applies cleanly to
> master as far as I can tell.
> 
> Link to patchew is
> https://patchew.org/QEMU/20210925214820.18078-1-nicho...@ngai.me/.
> 
> I’d love to get https://gitlab.com/qemu-project/qemu/-/issues/347 addressed
> once libslirp makes a release with added Unix-to-TCP support in the hostxfwd
> API, but this patch is a requirement for that first.

I'm also interested in this PATCH and a resolution to issue 347.

FYI your patch triggers checkpatch warnings, see [0].
Maybe you can resend the patch with the review tags and the checkpatch
warnings cleaned up.

Also it would be useful to know how the patch changes the version
requirements of the libslirp dependency.
(The version requirement should also be enforced in meson.build)
Also the commit in subprojects/slirp.wrap should be high enough,
which seems to already be the case however.

It seems it requires libslirp 4.6.0 from 2021-06-14, which is only
available from Debian 12 or Ubuntu 22.04 and no release of RHEL.


Thomas

[0] 
https://patchew.org/QEMU/20210925214820.18078-1-nicho...@ngai.me/logs/testing.checkpatch/



[PATCH v6 1/6] linux-headers: drop pvpanic.h

2024-02-08 Thread Thomas Weißschuh
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.

Link: https://lore.kernel.org/lkml/2023110431-pacemaker-pruning-0e4c@gregkh/
Reviewed-by: Thomas Huth 
Reviewed-by: Cornelia Huck 
Signed-off-by: Thomas Weißschuh 
---
 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(-)

diff --git a/hw/misc/pvpanic-isa.c b/hw/misc/pvpanic-isa.c
index ccec50f61bbd..ef438a31fbe9 100644
--- a/hw/misc/pvpanic-isa.c
+++ b/hw/misc/pvpanic-isa.c
@@ -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)
diff --git a/hw/misc/pvpanic-pci.c b/hw/misc/pvpanic-pci.c
index c01e4ce8646a..01e269b55284 100644
--- a/hw/misc/pvpanic-pci.c
+++ b/hw/misc/pvpanic-pci.c
@@ -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)
 
diff --git a/hw/misc/pvpanic.c b/hw/misc/pvpanic.c
index 1540e9091a45..4915ef256e74 100644
--- a/hw/misc/pvpanic.c
+++ b/hw/misc/pvpanic.c
@@ -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)
 {
diff --git a/include/hw/misc/pvpanic.h b/include/hw/misc/pvpanic.h
index fab94165d03d..dffca827f77a 100644
--- a/include/hw/misc/pvpanic.h
+++ b/include/hw/misc/pvpanic.h
@@ -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"
 
diff --git a/include/standard-headers/linux/pvpanic.h 
b/include/standard-headers/linux/pvpanic.h
deleted file mode 100644
index 54b7485390d3..
--- a/include/standard-headers/linux/pvpanic.h
+++ /dev/null
@@ -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__ */
diff --git a/scripts/update-linux-headers.sh b/scripts/update-linux-headers.sh
index a0006eec6fd1..c4fea51c93fd 100755
--- a/scripts/update-linux-headers.sh
+++ b/scripts/update-linux-headers.sh
@@ -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"

-- 
2.43.0




[PATCH v6 4/6] hw/misc/pvpanic: add support for normal shutdowns

2024-02-08 Thread Thomas Weißschuh
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.

Acked-by: Cornelia Huck 
Signed-off-by: Thomas Weißschuh 
---
 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(-)

diff --git a/docs/specs/pvpanic.rst b/docs/specs/pvpanic.rst
index f894bc19555f..796cc0348a38 100644
--- a/docs/specs/pvpanic.rst
+++ b/docs/specs/pvpanic.rst
@@ -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
 -
diff --git a/hw/misc/pvpanic.c b/hw/misc/pvpanic.c
index a4982cc5928e..0e9505451a7a 100644
--- a/hw/misc/pvpanic.c
+++ b/hw/misc/pvpanic.c
@@ -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 */
diff --git a/include/hw/misc/pvpanic.h b/include/hw/misc/pvpanic.h
index 48f2ec4c86a1..9e36a02d5a4f 100644
--- a/include/hw/misc/pvpanic.h
+++ b/include/hw/misc/pvpanic.h
@@ -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"
diff --git a/include/sysemu/runstate.h b/include/sysemu/runstate.h
index 0117d243c4ed..e210a37abf0f 100644
--- a/include/sysemu/runstate.h
+++ b/include/sysemu/runstate.h
@@ -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
diff --git a/system/runstate.c b/system/runstate.c
index d6ab860ecaa7..572499513034 100644
--- a/system/runstate.c
+++ b/system/runstate.c
@@ -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 &&

-- 
2.43.0




[PATCH v6 5/6] pvpanic: Emit GUEST_PVSHUTDOWN QMP event on pvpanic shutdown signal

2024-02-08 Thread Thomas Weißschuh
From: Alejandro Jimenez 

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.

Signed-off-by: Alejandro Jimenez 
---
 qapi/run-state.json | 14 ++
 system/runstate.c   |  1 +
 2 files changed, 15 insertions(+)

diff --git a/qapi/run-state.json b/qapi/run-state.json
index 08bc99cb8561..d5a63e14ba7e 100644
--- a/qapi/run-state.json
+++ b/qapi/run-state.json
@@ -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:
 #
diff --git a/system/runstate.c b/system/runstate.c
index 572499513034..02b0a1f8b9d0 100644
--- a/system/runstate.c
+++ b/system/runstate.c
@@ -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);
 }
 

-- 
2.43.0




[PATCH v6 2/6] hw/misc/pvpanic: centralize definition of supported events

2024-02-08 Thread Thomas Weißschuh
The 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.

Reviewed-by: Thomas Huth 
Reviewed-by: Cornelia Huck 
Signed-off-by: Thomas Weißschuh 
---
 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(-)

diff --git a/hw/misc/pvpanic-isa.c b/hw/misc/pvpanic-isa.c
index ef438a31fbe9..9a923b786907 100644
--- a/hw/misc/pvpanic-isa.c
+++ b/hw/misc/pvpanic-isa.c
@@ -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(),
 };
 
diff --git a/hw/misc/pvpanic-pci.c b/hw/misc/pvpanic-pci.c
index 01e269b55284..be4063121e1d 100644
--- a/hw/misc/pvpanic-pci.c
+++ b/hw/misc/pvpanic-pci.c
@@ -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(),
 };
 
diff --git a/hw/misc/pvpanic.c b/hw/misc/pvpanic.c
index 4915ef256e74..a4982cc5928e 100644
--- a/hw/misc/pvpanic.c
+++ b/hw/misc/pvpanic.c
@@ -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;
 }
diff --git a/include/hw/misc/pvpanic.h b/include/hw/misc/pvpanic.h
index dffca827f77a..48f2ec4c86a1 100644
--- a/include/hw/misc/pvpanic.h
+++ b/include/hw/misc/pvpanic.h
@@ -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"

-- 
2.43.0




[PATCH v6 3/6] tests/qtest/pvpanic: use centralized definition of supported events

2024-02-08 Thread Thomas Weißschuh
Avoid the necessity to update all tests when new events are added
to the device.

Acked-by: Thomas Huth 
Reviewed-by: Cornelia Huck 
Signed-off-by: Thomas Weißschuh 
---
 tests/qtest/pvpanic-pci-test.c | 5 +++--
 tests/qtest/pvpanic-test.c | 5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/tests/qtest/pvpanic-pci-test.c b/tests/qtest/pvpanic-pci-test.c
index 2c05b376ba72..b372caf41dc0 100644
--- a/tests/qtest/pvpanic-pci-test.c
+++ b/tests/qtest/pvpanic-pci-test.c
@@ -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, , sizeof(val));
-g_assert_cmpuint(val, ==, 3);
+g_assert_cmpuint(val, ==, PVPANIC_EVENTS);
 
 val = 1;
 qpci_memwrite(dev, bar, 0, , sizeof(val));
@@ -67,7 +68,7 @@ static void test_panic(void)
 bar = qpci_iomap(dev, 0, NULL);
 
 qpci_memread(dev, bar, 0, , sizeof(val));
-g_assert_cmpuint(val, ==, 3);
+g_assert_cmpuint(val, ==, PVPANIC_EVENTS);
 
 val = 1;
 qpci_memwrite(dev, bar, 0, , sizeof(val));
diff --git a/tests/qtest/pvpanic-test.c b/tests/qtest/pvpanic-test.c
index 78f1cf8186b0..ccc603472f5d 100644
--- a/tests/qtest/pvpanic-test.c
+++ b/tests/qtest/pvpanic-test.c
@@ -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);
 

-- 
2.43.0




[PATCH v6 0/6] hw/misc/pvpanic: add support for normal shutdowns

2024-02-08 Thread Thomas Weißschuh
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/20231104-pvpanic-shutdown-v1-1-5ee7c9b3e...@weissschuh.net/

Signed-off-by: Thomas Weißschuh 
---
Changes in v6:
- Replace magic constant "4" in tests with PVPANIC_SHUTDOWN
- Link to v5: 
https://lore.kernel.org/r/20240129-pvpanic-shutdown-v5-0-f5a060b87...@t-8ch.de

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/20240107-pvpanic-shutdown-v4-0-81500a7e4...@t-8ch.de

Changes in v4:
- Rebase on 8.2 master
- Resend after tree reopened and holidays
- Link to v3: 
https://lore.kernel.org/r/20231129-pvpanic-shutdown-v3-0-c9a2892fc...@t-8ch.de

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/20231128-pvpanic-shutdown-v2-0-830393b45...@t-8ch.de

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/20231104-pvpanic-shutdown-v1-0-023531578...@t-8ch.de

---
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,
-- 
Thomas Weißschuh 




[PATCH v6 6/6] tests/qtest/pvpanic: add tests for pvshutdown event

2024-02-08 Thread Thomas Weißschuh
Validate that a shutdown via the pvpanic device emits the correct
QMP events.

Signed-off-by: Thomas Weißschuh 
Reviewed-by: Thomas Huth 
---
 tests/qtest/pvpanic-pci-test.c | 39 +++
 tests/qtest/pvpanic-test.c | 29 +
 2 files changed, 68 insertions(+)

diff --git a/tests/qtest/pvpanic-pci-test.c b/tests/qtest/pvpanic-pci-test.c
index b372caf41dc0..dc021c2fdf77 100644
--- a/tests/qtest/pvpanic-pci-test.c
+++ b/tests/qtest/pvpanic-pci-test.c
@@ -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, , sizeof(val));
+g_assert_cmpuint(val, ==, PVPANIC_EVENTS);
+
+val = PVPANIC_SHUTDOWN;
+qpci_memwrite(dev, bar, 0, , 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(, , 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();
 }
diff --git a/tests/qtest/pvpanic-test.c b/tests/qtest/pvpanic-test.c
index ccc603472f5d..d49d2ba9313e 100644
--- a/tests/qtest/pvpanic-test.c
+++ b/tests/qtest/pvpanic-test.c
@@ -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(, , 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();
 }

-- 
2.43.0




[PATCH v5 1/6] linux-headers: drop pvpanic.h

2024-01-29 Thread Thomas Weißschuh
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.

Link: https://lore.kernel.org/lkml/2023110431-pacemaker-pruning-0e4c@gregkh/
Reviewed-by: Thomas Huth 
Reviewed-by: Cornelia Huck 
Signed-off-by: Thomas Weißschuh 
---
 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(-)

diff --git a/hw/misc/pvpanic-isa.c b/hw/misc/pvpanic-isa.c
index ccec50f61bbd..ef438a31fbe9 100644
--- a/hw/misc/pvpanic-isa.c
+++ b/hw/misc/pvpanic-isa.c
@@ -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)
diff --git a/hw/misc/pvpanic-pci.c b/hw/misc/pvpanic-pci.c
index c01e4ce8646a..01e269b55284 100644
--- a/hw/misc/pvpanic-pci.c
+++ b/hw/misc/pvpanic-pci.c
@@ -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)
 
diff --git a/hw/misc/pvpanic.c b/hw/misc/pvpanic.c
index 1540e9091a45..4915ef256e74 100644
--- a/hw/misc/pvpanic.c
+++ b/hw/misc/pvpanic.c
@@ -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)
 {
diff --git a/include/hw/misc/pvpanic.h b/include/hw/misc/pvpanic.h
index fab94165d03d..dffca827f77a 100644
--- a/include/hw/misc/pvpanic.h
+++ b/include/hw/misc/pvpanic.h
@@ -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"
 
diff --git a/include/standard-headers/linux/pvpanic.h 
b/include/standard-headers/linux/pvpanic.h
deleted file mode 100644
index 54b7485390d3..
--- a/include/standard-headers/linux/pvpanic.h
+++ /dev/null
@@ -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__ */
diff --git a/scripts/update-linux-headers.sh b/scripts/update-linux-headers.sh
index a0006eec6fd1..c4fea51c93fd 100755
--- a/scripts/update-linux-headers.sh
+++ b/scripts/update-linux-headers.sh
@@ -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"

-- 
2.43.0




[PATCH v5 3/6] tests/qtest/pvpanic: use centralized definition of supported events

2024-01-29 Thread Thomas Weißschuh
Avoid the necessity to update all tests when new events are added
to the device.

Acked-by: Thomas Huth 
Reviewed-by: Cornelia Huck 
Signed-off-by: Thomas Weißschuh 
---
 tests/qtest/pvpanic-pci-test.c | 5 +++--
 tests/qtest/pvpanic-test.c | 5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/tests/qtest/pvpanic-pci-test.c b/tests/qtest/pvpanic-pci-test.c
index 2c05b376ba72..b372caf41dc0 100644
--- a/tests/qtest/pvpanic-pci-test.c
+++ b/tests/qtest/pvpanic-pci-test.c
@@ -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, , sizeof(val));
-g_assert_cmpuint(val, ==, 3);
+g_assert_cmpuint(val, ==, PVPANIC_EVENTS);
 
 val = 1;
 qpci_memwrite(dev, bar, 0, , sizeof(val));
@@ -67,7 +68,7 @@ static void test_panic(void)
 bar = qpci_iomap(dev, 0, NULL);
 
 qpci_memread(dev, bar, 0, , sizeof(val));
-g_assert_cmpuint(val, ==, 3);
+g_assert_cmpuint(val, ==, PVPANIC_EVENTS);
 
 val = 1;
 qpci_memwrite(dev, bar, 0, , sizeof(val));
diff --git a/tests/qtest/pvpanic-test.c b/tests/qtest/pvpanic-test.c
index 78f1cf8186b0..ccc603472f5d 100644
--- a/tests/qtest/pvpanic-test.c
+++ b/tests/qtest/pvpanic-test.c
@@ -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);
 

-- 
2.43.0




[PATCH v5 2/6] hw/misc/pvpanic: centralize definition of supported events

2024-01-29 Thread Thomas Weißschuh
The 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.

Reviewed-by: Thomas Huth 
Reviewed-by: Cornelia Huck 
Signed-off-by: Thomas Weißschuh 
---
 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(-)

diff --git a/hw/misc/pvpanic-isa.c b/hw/misc/pvpanic-isa.c
index ef438a31fbe9..9a923b786907 100644
--- a/hw/misc/pvpanic-isa.c
+++ b/hw/misc/pvpanic-isa.c
@@ -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(),
 };
 
diff --git a/hw/misc/pvpanic-pci.c b/hw/misc/pvpanic-pci.c
index 01e269b55284..be4063121e1d 100644
--- a/hw/misc/pvpanic-pci.c
+++ b/hw/misc/pvpanic-pci.c
@@ -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(),
 };
 
diff --git a/hw/misc/pvpanic.c b/hw/misc/pvpanic.c
index 4915ef256e74..a4982cc5928e 100644
--- a/hw/misc/pvpanic.c
+++ b/hw/misc/pvpanic.c
@@ -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;
 }
diff --git a/include/hw/misc/pvpanic.h b/include/hw/misc/pvpanic.h
index dffca827f77a..48f2ec4c86a1 100644
--- a/include/hw/misc/pvpanic.h
+++ b/include/hw/misc/pvpanic.h
@@ -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"

-- 
2.43.0




[PATCH v5 4/6] hw/misc/pvpanic: add support for normal shutdowns

2024-01-29 Thread Thomas Weißschuh
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.

Acked-by: Cornelia Huck 
Signed-off-by: Thomas Weißschuh 
---
 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(-)

diff --git a/docs/specs/pvpanic.rst b/docs/specs/pvpanic.rst
index f894bc19555f..796cc0348a38 100644
--- a/docs/specs/pvpanic.rst
+++ b/docs/specs/pvpanic.rst
@@ -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
 -
diff --git a/hw/misc/pvpanic.c b/hw/misc/pvpanic.c
index a4982cc5928e..0e9505451a7a 100644
--- a/hw/misc/pvpanic.c
+++ b/hw/misc/pvpanic.c
@@ -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 */
diff --git a/include/hw/misc/pvpanic.h b/include/hw/misc/pvpanic.h
index 48f2ec4c86a1..9e36a02d5a4f 100644
--- a/include/hw/misc/pvpanic.h
+++ b/include/hw/misc/pvpanic.h
@@ -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"
diff --git a/include/sysemu/runstate.h b/include/sysemu/runstate.h
index 0117d243c4ed..e210a37abf0f 100644
--- a/include/sysemu/runstate.h
+++ b/include/sysemu/runstate.h
@@ -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
diff --git a/system/runstate.c b/system/runstate.c
index d6ab860ecaa7..572499513034 100644
--- a/system/runstate.c
+++ b/system/runstate.c
@@ -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 &&

-- 
2.43.0




[PATCH v5 6/6] tests/qtest/pvpanic: add tests for pvshutdown event

2024-01-29 Thread Thomas Weißschuh
Validate that a shutdown via the pvpanic device emits the correct
QMP events.

Signed-off-by: Thomas Weißschuh 
---
 tests/qtest/pvpanic-pci-test.c | 39 +++
 tests/qtest/pvpanic-test.c | 29 +
 2 files changed, 68 insertions(+)

diff --git a/tests/qtest/pvpanic-pci-test.c b/tests/qtest/pvpanic-pci-test.c
index b372caf41dc0..e1c05d383219 100644
--- a/tests/qtest/pvpanic-pci-test.c
+++ b/tests/qtest/pvpanic-pci-test.c
@@ -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, , sizeof(val));
+g_assert_cmpuint(val, ==, PVPANIC_EVENTS);
+
+val = 4;
+qpci_memwrite(dev, bar, 0, , 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(, , 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();
 }
diff --git a/tests/qtest/pvpanic-test.c b/tests/qtest/pvpanic-test.c
index ccc603472f5d..ff1f25f46586 100644
--- a/tests/qtest/pvpanic-test.c
+++ b/tests/qtest/pvpanic-test.c
@@ -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, 0x4);
+
+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(, , 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();
 }

-- 
2.43.0




[PATCH v5 5/6] pvpanic: Emit GUEST_PVSHUTDOWN QMP event on pvpanic shutdown signal

2024-01-29 Thread Thomas Weißschuh
From: Alejandro Jimenez 

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.

Signed-off-by: Alejandro Jimenez 
---
 qapi/run-state.json | 14 ++
 system/runstate.c   |  1 +
 2 files changed, 15 insertions(+)

diff --git a/qapi/run-state.json b/qapi/run-state.json
index 08bc99cb8561..d5a63e14ba7e 100644
--- a/qapi/run-state.json
+++ b/qapi/run-state.json
@@ -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:
 #
diff --git a/system/runstate.c b/system/runstate.c
index 572499513034..02b0a1f8b9d0 100644
--- a/system/runstate.c
+++ b/system/runstate.c
@@ -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);
 }
 

-- 
2.43.0




[PATCH v5 0/6] hw/misc/pvpanic: add support for normal shutdowns

2024-01-29 Thread Thomas Weißschuh
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, that seems 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/20231104-pvpanic-shutdown-v1-1-5ee7c9b3e...@weissschuh.net/

Signed-off-by: Thomas Weißschuh 
---
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/20240107-pvpanic-shutdown-v4-0-81500a7e4...@t-8ch.de

Changes in v4:
- Rebase on 8.2 master
- Resend after tree reopened and holidays
- Link to v3: 
https://lore.kernel.org/r/20231129-pvpanic-shutdown-v3-0-c9a2892fc...@t-8ch.de

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/20231128-pvpanic-shutdown-v2-0-830393b45...@t-8ch.de

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/20231104-pvpanic-shutdown-v1-0-023531578...@t-8ch.de

---
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: 11be70677c70fdccd452a3233653949b79e97908
change-id: 20231104-pvpanic-shutdown-02e4b4cb4949

Best regards,
-- 
Thomas Weißschuh 




Re: Re: [PATCH v4 4/4] hw/misc/pvpanic: add support for normal shutdowns

2024-01-26 Thread Thomas Weißschuh
Hi Alejandro,

On 2024-01-26 13:47:33-0500, Alejandro Jimenez wrote:
> On 1/7/24 09:05, Thomas Weißschuh wrote:
> > 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.
> > 
> > Signed-off-by: Thomas Weißschuh 
> > ---
> >   docs/specs/pvpanic.rst| 2 ++
> >   hw/misc/pvpanic.c | 5 +
> >   include/hw/misc/pvpanic.h | 3 ++-
> >   3 files changed, 9 insertions(+), 1 deletion(-)
> > 
> [snip]
> 
> >   -
> > diff --git a/hw/misc/pvpanic.c b/hw/misc/pvpanic.c
> > index a4982cc5928e..246f9ae4e992 100644
> > --- a/hw/misc/pvpanic.c
> > +++ b/hw/misc/pvpanic.c
> > @@ -40,6 +40,11 @@ static void handle_event(int event)
> >   qemu_system_guest_crashloaded(NULL);
> >   return;
> >   }
> > +
> > +if (event & PVPANIC_SHUTDOWN) {
> > +qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
> 
> I would suggest that instead of directly requesting a system shutdown,
> we should follow the same convention/handling of the other pvpanic
> events and emit a QMP message signaling the specific event that took
> place, to help a management layer application that might be listening
> to determine the cause of the shutdown. It can also be a helpful
> signal to let us know if a guest is (ab)using the new functionality.

This sounds reasonable, thanks for the suggestion and patch.

> If you agree with my reasoning and you'd allow me to piggyback on your
> series, please add my complementary [PATCH 5/4] change that implements
> the suggestion:

I picked up the patch and will test and resend the series in a few days.

[snip]

If one of the maintainers reads this:

Maybe patch 1, 2 and 3 could already be picked up as they seem not to be
controversial.
Then I can also continue to remove the UAPI header on the kernel side.

Thomas



[PATCH v3 0/2] linux-user: two fixes to coredump generation

2024-01-20 Thread Thomas Weißschuh
Signed-off-by: Thomas Weißschuh 
---
Changes in v3:
- Add braces to if statements
- Add Reviewed-by from Richard
- Link to v2: 
https://lore.kernel.org/r/20240107-qemu-user-dumpable-v2-0-54e3bcfc0...@t-8ch.de

Changes in v2:
- Rebase on 8.2 master
- Resend after closed tree and holidays
- Link to v1: 
https://lore.kernel.org/r/20231115-qemu-user-dumpable-v1-0-edbe7f0fb...@t-8ch.de

---
Thomas Weißschuh (2):
  linux-user/elfload: test return value of getrlimit
  linux-user/elfload: check PR_GET_DUMPABLE before creating coredump

 linux-user/elfload.c | 10 --
 1 file changed, 8 insertions(+), 2 deletions(-)
---
base-commit: 3f2a357b95845ea0bf7463eff6661e43b97d1afc
change-id: 20231115-qemu-user-dumpable-d499c0396103

Best regards,
-- 
Thomas Weißschuh 




[PATCH v3 1/2] linux-user/elfload: test return value of getrlimit

2024-01-20 Thread Thomas Weißschuh
Should getrlimit() fail the value of dumpsize.rlimit_cur may not be
initialized. Avoid reading garbage data by checking the return value of
getrlimit.

Reviewed-by: Richard Henderson 
Signed-off-by: Thomas Weißschuh 
---
 linux-user/elfload.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index cf9e74468b11..c5968719380a 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -4667,9 +4667,9 @@ static int elf_core_dump(int signr, const CPUArchState 
*env)
 init_note_info();
 
 errno = 0;
-getrlimit(RLIMIT_CORE, );
-if (dumpsize.rlim_cur == 0)
+if (getrlimit(RLIMIT_CORE, ) == 0 && dumpsize.rlim_cur == 0) {
 return 0;
+}
 
 corefile = core_dump_filename(ts);
 

-- 
2.43.0




[PATCH v3 2/2] linux-user/elfload: check PR_GET_DUMPABLE before creating coredump

2024-01-20 Thread Thomas Weißschuh
A process can opt-out of coredump creation by calling
prctl(PR_SET_DUMPABLE, 0).
linux-user passes this call from the guest through to the
operating system.
>From there it can be read back again to avoid creating coredumps from
qemu-user itself if the guest chose so.

Reviewed-by: Richard Henderson 
Signed-off-by: Thomas Weißschuh 
---
 linux-user/elfload.c | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index c5968719380a..daf7ef843564 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -2,6 +2,7 @@
 #include "qemu/osdep.h"
 #include 
 
+#include 
 #include 
 #include 
 
@@ -4667,6 +4668,11 @@ static int elf_core_dump(int signr, const CPUArchState 
*env)
 init_note_info();
 
 errno = 0;
+
+if (prctl(PR_GET_DUMPABLE) == 0) {
+return 0;
+}
+
 if (getrlimit(RLIMIT_CORE, ) == 0 && dumpsize.rlim_cur == 0) {
 return 0;
 }

-- 
2.43.0




Re: Re: [PATCH v2 0/2] linux-user: two fixes to coredump generation

2024-01-09 Thread Thomas Weißschuh
On 2024-01-10 08:33:11+1100, Richard Henderson wrote:
> On 1/8/24 01:01, Thomas Weißschuh wrote:
> > Signed-off-by: Thomas Weißschuh 
> > ---
> > Changes in v2:
> > - Rebase on 8.2 master
> > - Resend after closed tree and holidays
> > - Link to v1: 
> > https://lore.kernel.org/r/20231115-qemu-user-dumpable-v1-0-edbe7f0fb...@t-8ch.de
> > 
> > ---
> > Thomas Weißschuh (2):
> >linux-user/elfload: test return value of getrlimit
> >linux-user/elfload: check PR_GET_DUMPABLE before creating coredump
> > 
> >   linux-user/elfload.c | 8 ++--
> >   1 file changed, 6 insertions(+), 2 deletions(-)
> > ---
> > base-commit: 0c1eccd368af8805ec0fb11e6cf25d0684d37328
> > change-id: 20231115-qemu-user-dumpable-d499c0396103
> > 
> > Best regards,
> 
> Both patches look good for correctness, but both have style issues: need
> braces on those if statements.
> 
> With that fixed,
> Reviewed-by: Richard Henderson 

Thanks,

I added the braces for the next revision, which I'll send after waiting
some more feedback.



[PATCH v4 3/4] tests/qtest/pvpanic: use centralized definition of supported events

2024-01-07 Thread Thomas Weißschuh
Avoid the necessity to update all tests when new events are added
to the device.

Acked-by: Thomas Huth 
Signed-off-by: Thomas Weißschuh 
---
 tests/qtest/pvpanic-pci-test.c | 5 +++--
 tests/qtest/pvpanic-test.c | 5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/tests/qtest/pvpanic-pci-test.c b/tests/qtest/pvpanic-pci-test.c
index 2c05b376ba72..b372caf41dc0 100644
--- a/tests/qtest/pvpanic-pci-test.c
+++ b/tests/qtest/pvpanic-pci-test.c
@@ -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, , sizeof(val));
-g_assert_cmpuint(val, ==, 3);
+g_assert_cmpuint(val, ==, PVPANIC_EVENTS);
 
 val = 1;
 qpci_memwrite(dev, bar, 0, , sizeof(val));
@@ -67,7 +68,7 @@ static void test_panic(void)
 bar = qpci_iomap(dev, 0, NULL);
 
 qpci_memread(dev, bar, 0, , sizeof(val));
-g_assert_cmpuint(val, ==, 3);
+g_assert_cmpuint(val, ==, PVPANIC_EVENTS);
 
 val = 1;
 qpci_memwrite(dev, bar, 0, , sizeof(val));
diff --git a/tests/qtest/pvpanic-test.c b/tests/qtest/pvpanic-test.c
index 78f1cf8186b0..ccc603472f5d 100644
--- a/tests/qtest/pvpanic-test.c
+++ b/tests/qtest/pvpanic-test.c
@@ -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);
 

-- 
2.43.0




[PATCH v4 4/4] hw/misc/pvpanic: add support for normal shutdowns

2024-01-07 Thread Thomas Weißschuh
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.

Signed-off-by: Thomas Weißschuh 
---
 docs/specs/pvpanic.rst| 2 ++
 hw/misc/pvpanic.c | 5 +
 include/hw/misc/pvpanic.h | 3 ++-
 3 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/docs/specs/pvpanic.rst b/docs/specs/pvpanic.rst
index f894bc19555f..796cc0348a38 100644
--- a/docs/specs/pvpanic.rst
+++ b/docs/specs/pvpanic.rst
@@ -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
 -
diff --git a/hw/misc/pvpanic.c b/hw/misc/pvpanic.c
index a4982cc5928e..246f9ae4e992 100644
--- a/hw/misc/pvpanic.c
+++ b/hw/misc/pvpanic.c
@@ -40,6 +40,11 @@ static void handle_event(int event)
 qemu_system_guest_crashloaded(NULL);
 return;
 }
+
+if (event & PVPANIC_SHUTDOWN) {
+qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
+return;
+}
 }
 
 /* return supported events on read */
diff --git a/include/hw/misc/pvpanic.h b/include/hw/misc/pvpanic.h
index 48f2ec4c86a1..9e36a02d5a4f 100644
--- a/include/hw/misc/pvpanic.h
+++ b/include/hw/misc/pvpanic.h
@@ -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"

-- 
2.43.0




[PATCH v4 2/4] hw/misc/pvpanic: centralize definition of supported events

2024-01-07 Thread Thomas Weißschuh
The 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.

Signed-off-by: Thomas Weißschuh 
---
 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(-)

diff --git a/hw/misc/pvpanic-isa.c b/hw/misc/pvpanic-isa.c
index ef438a31fbe9..9a923b786907 100644
--- a/hw/misc/pvpanic-isa.c
+++ b/hw/misc/pvpanic-isa.c
@@ -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(),
 };
 
diff --git a/hw/misc/pvpanic-pci.c b/hw/misc/pvpanic-pci.c
index 01e269b55284..be4063121e1d 100644
--- a/hw/misc/pvpanic-pci.c
+++ b/hw/misc/pvpanic-pci.c
@@ -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(),
 };
 
diff --git a/hw/misc/pvpanic.c b/hw/misc/pvpanic.c
index 4915ef256e74..a4982cc5928e 100644
--- a/hw/misc/pvpanic.c
+++ b/hw/misc/pvpanic.c
@@ -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;
 }
diff --git a/include/hw/misc/pvpanic.h b/include/hw/misc/pvpanic.h
index dffca827f77a..48f2ec4c86a1 100644
--- a/include/hw/misc/pvpanic.h
+++ b/include/hw/misc/pvpanic.h
@@ -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"

-- 
2.43.0




[PATCH v4 1/4] linux-headers: drop pvpanic.h

2024-01-07 Thread Thomas Weißschuh
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.

Link: https://lore.kernel.org/lkml/2023110431-pacemaker-pruning-0e4c@gregkh/
Signed-off-by: Thomas Weißschuh 
---
 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(-)

diff --git a/hw/misc/pvpanic-isa.c b/hw/misc/pvpanic-isa.c
index ccec50f61bbd..ef438a31fbe9 100644
--- a/hw/misc/pvpanic-isa.c
+++ b/hw/misc/pvpanic-isa.c
@@ -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)
diff --git a/hw/misc/pvpanic-pci.c b/hw/misc/pvpanic-pci.c
index c01e4ce8646a..01e269b55284 100644
--- a/hw/misc/pvpanic-pci.c
+++ b/hw/misc/pvpanic-pci.c
@@ -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)
 
diff --git a/hw/misc/pvpanic.c b/hw/misc/pvpanic.c
index 1540e9091a45..4915ef256e74 100644
--- a/hw/misc/pvpanic.c
+++ b/hw/misc/pvpanic.c
@@ -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)
 {
diff --git a/include/hw/misc/pvpanic.h b/include/hw/misc/pvpanic.h
index fab94165d03d..dffca827f77a 100644
--- a/include/hw/misc/pvpanic.h
+++ b/include/hw/misc/pvpanic.h
@@ -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"
 
diff --git a/include/standard-headers/linux/pvpanic.h 
b/include/standard-headers/linux/pvpanic.h
deleted file mode 100644
index 54b7485390d3..
--- a/include/standard-headers/linux/pvpanic.h
+++ /dev/null
@@ -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__ */
diff --git a/scripts/update-linux-headers.sh b/scripts/update-linux-headers.sh
index 34295c0fe55b..555bdc8af2eb 100755
--- a/scripts/update-linux-headers.sh
+++ b/scripts/update-linux-headers.sh
@@ -215,8 +215,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"

-- 
2.43.0




[PATCH v4 0/4] hw/misc/pvpanic: add support for normal shutdowns

2024-01-07 Thread Thomas Weißschuh
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.

I sent an RFC[0] for this before to qemu-devel and lkml which didn't
generate feedback, so let's discuss the concrete proposal.

Patch 1 and 2 are general cleanups, that seems useful even without this
proposal being implemented.

A corresponding patch has been submitted for Linux [1].
This is also where the request was voiced to drop move away from a
pvpanic uapi header in Linux.

[0] https://lore.kernel.org/all/984794aa-4af0-4c68-a74e-7420ec315...@t-8ch.de/
[1] 
https://lore.kernel.org/lkml/20231104-pvpanic-shutdown-v1-1-5ee7c9b3e...@weissschuh.net/

Signed-off-by: Thomas Weißschuh 
---
Changes in v4:
- Rebase on 8.2 master
- Resend after tree reopened and holidays
- Link to v3: 
https://lore.kernel.org/r/20231129-pvpanic-shutdown-v3-0-c9a2892fc...@t-8ch.de

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/20231128-pvpanic-shutdown-v2-0-830393b45...@t-8ch.de

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/20231104-pvpanic-shutdown-v1-0-023531578...@t-8ch.de

---
Thomas Weißschuh (4):
  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

 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 -
 scripts/update-linux-headers.sh  | 3 +--
 tests/qtest/pvpanic-pci-test.c   | 5 +++--
 tests/qtest/pvpanic-test.c   | 5 +++--
 9 files changed, 22 insertions(+), 21 deletions(-)
---
base-commit: 0c1eccd368af8805ec0fb11e6cf25d0684d37328
change-id: 20231104-pvpanic-shutdown-02e4b4cb4949

Best regards,
-- 
Thomas Weißschuh 




[PATCH v2 0/2] linux-user: two fixes to coredump generation

2024-01-07 Thread Thomas Weißschuh
Signed-off-by: Thomas Weißschuh 
---
Changes in v2:
- Rebase on 8.2 master
- Resend after closed tree and holidays
- Link to v1: 
https://lore.kernel.org/r/20231115-qemu-user-dumpable-v1-0-edbe7f0fb...@t-8ch.de

---
Thomas Weißschuh (2):
  linux-user/elfload: test return value of getrlimit
  linux-user/elfload: check PR_GET_DUMPABLE before creating coredump

 linux-user/elfload.c | 8 ++--
 1 file changed, 6 insertions(+), 2 deletions(-)
---
base-commit: 0c1eccd368af8805ec0fb11e6cf25d0684d37328
change-id: 20231115-qemu-user-dumpable-d499c0396103

Best regards,
-- 
Thomas Weißschuh 




[PATCH v2 2/2] linux-user/elfload: check PR_GET_DUMPABLE before creating coredump

2024-01-07 Thread Thomas Weißschuh
A process can opt-out of coredump creation by calling
prctl(PR_SET_DUMPABLE, 0).
linux-user passes this call from the guest through to the
operating system.
>From there it can be read back again to avoid creating coredumps from
qemu-user itself if the guest chose so.

Signed-off-by: Thomas Weißschuh 
---
 linux-user/elfload.c | 5 +
 1 file changed, 5 insertions(+)

diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index 74c9ecda1806..956cb3ae2da5 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -2,6 +2,7 @@
 #include "qemu/osdep.h"
 #include 
 
+#include 
 #include 
 #include 
 
@@ -4667,6 +4668,10 @@ static int elf_core_dump(int signr, const CPUArchState 
*env)
 init_note_info();
 
 errno = 0;
+
+if (prctl(PR_GET_DUMPABLE) == 0)
+return 0;
+
 if (getrlimit(RLIMIT_CORE, ) == 0 && dumpsize.rlim_cur == 0)
 return 0;
 

-- 
2.43.0




[PATCH v2 1/2] linux-user/elfload: test return value of getrlimit

2024-01-07 Thread Thomas Weißschuh
Should getrlimit() fail the value of dumpsize.rlimit_cur may not be
initialized. Avoid reading garbage data by checking the return value of
getrlimit.

Signed-off-by: Thomas Weißschuh 
---
 linux-user/elfload.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index cf9e74468b11..74c9ecda1806 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -4667,8 +4667,7 @@ static int elf_core_dump(int signr, const CPUArchState 
*env)
 init_note_info();
 
 errno = 0;
-getrlimit(RLIMIT_CORE, );
-if (dumpsize.rlim_cur == 0)
+if (getrlimit(RLIMIT_CORE, ) == 0 && dumpsize.rlim_cur == 0)
 return 0;
 
 corefile = core_dump_filename(ts);

-- 
2.43.0




[PATCH v3 4/4] hw/misc/pvpanic: add support for normal shutdowns

2023-11-29 Thread Thomas Weißschuh
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.

Signed-off-by: Thomas Weißschuh 
---
 docs/specs/pvpanic.rst| 2 ++
 hw/misc/pvpanic.c | 5 +
 include/hw/misc/pvpanic.h | 3 ++-
 3 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/docs/specs/pvpanic.rst b/docs/specs/pvpanic.rst
index f894bc19555f..796cc0348a38 100644
--- a/docs/specs/pvpanic.rst
+++ b/docs/specs/pvpanic.rst
@@ -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
 -
diff --git a/hw/misc/pvpanic.c b/hw/misc/pvpanic.c
index a4982cc5928e..246f9ae4e992 100644
--- a/hw/misc/pvpanic.c
+++ b/hw/misc/pvpanic.c
@@ -40,6 +40,11 @@ static void handle_event(int event)
 qemu_system_guest_crashloaded(NULL);
 return;
 }
+
+if (event & PVPANIC_SHUTDOWN) {
+qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
+return;
+}
 }
 
 /* return supported events on read */
diff --git a/include/hw/misc/pvpanic.h b/include/hw/misc/pvpanic.h
index 48f2ec4c86a1..9e36a02d5a4f 100644
--- a/include/hw/misc/pvpanic.h
+++ b/include/hw/misc/pvpanic.h
@@ -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"

-- 
2.43.0




[PATCH v3 3/4] tests/qtest/pvpanic: use centralized definition of supported events

2023-11-29 Thread Thomas Weißschuh
Avoid the necessity to update all tests when new events are added
to the device.

Acked-by: Thomas Huth 
Signed-off-by: Thomas Weißschuh 
---
 tests/qtest/pvpanic-pci-test.c | 5 +++--
 tests/qtest/pvpanic-test.c | 5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/tests/qtest/pvpanic-pci-test.c b/tests/qtest/pvpanic-pci-test.c
index 2c05b376ba72..b372caf41dc0 100644
--- a/tests/qtest/pvpanic-pci-test.c
+++ b/tests/qtest/pvpanic-pci-test.c
@@ -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, , sizeof(val));
-g_assert_cmpuint(val, ==, 3);
+g_assert_cmpuint(val, ==, PVPANIC_EVENTS);
 
 val = 1;
 qpci_memwrite(dev, bar, 0, , sizeof(val));
@@ -67,7 +68,7 @@ static void test_panic(void)
 bar = qpci_iomap(dev, 0, NULL);
 
 qpci_memread(dev, bar, 0, , sizeof(val));
-g_assert_cmpuint(val, ==, 3);
+g_assert_cmpuint(val, ==, PVPANIC_EVENTS);
 
 val = 1;
 qpci_memwrite(dev, bar, 0, , sizeof(val));
diff --git a/tests/qtest/pvpanic-test.c b/tests/qtest/pvpanic-test.c
index 78f1cf8186b0..ccc603472f5d 100644
--- a/tests/qtest/pvpanic-test.c
+++ b/tests/qtest/pvpanic-test.c
@@ -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);
 

-- 
2.43.0




[PATCH v3 2/4] hw/misc/pvpanic: centralize definition of supported events

2023-11-29 Thread Thomas Weißschuh
The 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.

Signed-off-by: Thomas Weißschuh 
---
 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(-)

diff --git a/hw/misc/pvpanic-isa.c b/hw/misc/pvpanic-isa.c
index ef438a31fbe9..9a923b786907 100644
--- a/hw/misc/pvpanic-isa.c
+++ b/hw/misc/pvpanic-isa.c
@@ -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(),
 };
 
diff --git a/hw/misc/pvpanic-pci.c b/hw/misc/pvpanic-pci.c
index 1de138357b95..8898d280d2ef 100644
--- a/hw/misc/pvpanic-pci.c
+++ b/hw/misc/pvpanic-pci.c
@@ -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(),
 };
 
diff --git a/hw/misc/pvpanic.c b/hw/misc/pvpanic.c
index 4915ef256e74..a4982cc5928e 100644
--- a/hw/misc/pvpanic.c
+++ b/hw/misc/pvpanic.c
@@ -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;
 }
diff --git a/include/hw/misc/pvpanic.h b/include/hw/misc/pvpanic.h
index dffca827f77a..48f2ec4c86a1 100644
--- a/include/hw/misc/pvpanic.h
+++ b/include/hw/misc/pvpanic.h
@@ -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"

-- 
2.43.0




[PATCH v3 1/4] linux-headers: drop pvpanic.h

2023-11-29 Thread Thomas Weißschuh
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.

Link: https://lore.kernel.org/lkml/2023110431-pacemaker-pruning-0e4c@gregkh/
Signed-off-by: Thomas Weißschuh 
---
 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(-)

diff --git a/hw/misc/pvpanic-isa.c b/hw/misc/pvpanic-isa.c
index ccec50f61bbd..ef438a31fbe9 100644
--- a/hw/misc/pvpanic-isa.c
+++ b/hw/misc/pvpanic-isa.c
@@ -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)
diff --git a/hw/misc/pvpanic-pci.c b/hw/misc/pvpanic-pci.c
index fbcaa50731b3..1de138357b95 100644
--- a/hw/misc/pvpanic-pci.c
+++ b/hw/misc/pvpanic-pci.c
@@ -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)
 
diff --git a/hw/misc/pvpanic.c b/hw/misc/pvpanic.c
index 1540e9091a45..4915ef256e74 100644
--- a/hw/misc/pvpanic.c
+++ b/hw/misc/pvpanic.c
@@ -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)
 {
diff --git a/include/hw/misc/pvpanic.h b/include/hw/misc/pvpanic.h
index fab94165d03d..dffca827f77a 100644
--- a/include/hw/misc/pvpanic.h
+++ b/include/hw/misc/pvpanic.h
@@ -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"
 
diff --git a/include/standard-headers/linux/pvpanic.h 
b/include/standard-headers/linux/pvpanic.h
deleted file mode 100644
index 54b7485390d3..
--- a/include/standard-headers/linux/pvpanic.h
+++ /dev/null
@@ -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__ */
diff --git a/scripts/update-linux-headers.sh b/scripts/update-linux-headers.sh
index 34295c0fe55b..555bdc8af2eb 100755
--- a/scripts/update-linux-headers.sh
+++ b/scripts/update-linux-headers.sh
@@ -215,8 +215,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"

-- 
2.43.0




[PATCH v3 0/4] hw/misc/pvpanic: add support for normal shutdowns

2023-11-29 Thread Thomas Weißschuh
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.

I sent an RFC[0] for this before to qemu-devel and lkml which didn't
generate feedback, so let's discuss the concrete proposal.

Patch 1 and 2 are general cleanups, that seems useful even without this
proposal being implemented.

A corresponding patch has been submitted for Linux [1].
This is also where the request was voiced to drop move away from a
pvpanic uapi header in Linux.

[0] https://lore.kernel.org/all/984794aa-4af0-4c68-a74e-7420ec315...@t-8ch.de/
[1] 
https://lore.kernel.org/lkml/20231104-pvpanic-shutdown-v1-1-5ee7c9b3e...@weissschuh.net/

Signed-off-by: Thomas Weißschuh 
---
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/20231128-pvpanic-shutdown-v2-0-830393b45...@t-8ch.de

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/20231104-pvpanic-shutdown-v1-0-023531578...@t-8ch.de

---
Thomas Weißschuh (4):
  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

 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 -
 scripts/update-linux-headers.sh  | 3 +--
 tests/qtest/pvpanic-pci-test.c   | 5 +++--
 tests/qtest/pvpanic-test.c   | 5 +++--
 9 files changed, 22 insertions(+), 21 deletions(-)
---
base-commit: abf635ddfe3242df907f58967f3c1e6763bbca2d
change-id: 20231104-pvpanic-shutdown-02e4b4cb4949

Best regards,
-- 
Thomas Weißschuh 




Re: [PATCH v2 3/3] hw/misc/pvpanic: add support for normal shutdowns

2023-11-29 Thread Thomas Weißschuh
On 2023-11-29 14:15:14+0100, Cornelia Huck wrote:
> On Wed, Nov 29 2023, Thomas Weißschuh  wrote:
> > On 2023-11-29 09:23:46+0100, Cornelia Huck wrote:
> >> On Tue, Nov 28 2023, Thomas Weißschuh  wrote:
> >> > diff --git a/include/standard-headers/linux/pvpanic.h 
> >> > b/include/standard-headers/linux/pvpanic.h
> >> > index 54b7485390d3..38e53ad45929 100644
> >> > --- a/include/standard-headers/linux/pvpanic.h
> >> > +++ b/include/standard-headers/linux/pvpanic.h
> >> > @@ -5,5 +5,6 @@
> >> >  
> >> >  #define PVPANIC_PANICKED(1 << 0)
> >> >  #define PVPANIC_CRASH_LOADED(1 << 1)
> >> > +#define PVPANIC_SHUTDOWN(1 << 2)
> >> >  
> >> >  #endif /* __PVPANIC_H__ */
> >> >
> >> 
> >> This hunk needs to come in via a separate headers update, or has to be
> >> split out into a placeholder patch if it is not included in the Linux
> >> kernel yet.
> >
> > Greg KH actually want this header removed from the Linux UAPI headers,
> > as it is not in fact a Linux UAPI [0].
> > It's also a weird workflow to have the specification in qemu but the
> > header as part of Linux that is re-imported in qemu.
> >
> > What do you think about maintaining the header as a private part of qemu
> > and dropping it from Linux UAPI?
> >
> > Contrary to my response to Greg this wouldn't break old versions of
> > qemu, as qemu is using a private copy that would still exist there.
> >
> > [0] https://lore.kernel.org/lkml/2023110431-pacemaker-pruning-0e4c@gregkh/
> 
> Hm... we have a bunch of examples where we use things exported via the
> Linux uapi header files that are not a kernel<->userspace interface, but
> rather a host<->guest interface (sometimes defining the interface,
> sometimes more as a convenience mechanism). I agree that this is not
> quite what the Linux uapi is supposed to be (and yes, it's weird), but
> we've being doing that for many years now and changing it would be a
> non-zero effort (and we'd have to figure out another way to make sure
> the kernel and QEMU do not diverge if there's no authorative third party
> around.)
> 
> In the case of the pvpanic device, this seems manageable, though; if we
> decide to go that way, we should
> 
> 1. copy the header on the QEMU side somewhere else under include/ and
>remove it from the header update script

There is already include/hw/misc/pvpanic.h which seems to be the best
place.

> 2. wait until this hits QEMU mainline (so nobody will try to run the old
>update script)
> 3. move the uapi file on the Linux side
> 
> (We've had changes in the kernel break the update script before, but if
> we can do it more smoothly, I'd prefer that way -- the kernel merge
> window won't open before the new year anyway, and by that time, we'll
> have the QEMU tree open again.)

The kernel side isn't urgent anyways.

> Main downside is that you'd have extra hassle for something that looks
> like a straightforward feature, which is not ideal. (Also, are we sure
> that nobody else consumes that header file?)

Otherwise I have the hassle on the kernel side :-)

Debian codesearch did not find other users.

> I'm not sure if dealing with the other host<->guest interfaces that get
> copied over is worth the effort, though...
> 
> Opinions?

I'll resend a new revision that drops the import this evening, if
nothing new comes up.



Re: [PATCH v2 3/3] hw/misc/pvpanic: add support for normal shutdowns

2023-11-29 Thread Thomas Weißschuh
On 2023-11-29 09:23:46+0100, Cornelia Huck wrote:
> On Tue, Nov 28 2023, Thomas Weißschuh  wrote:
> 
> > 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.
> >
> > Signed-off-by: Thomas Weißschuh 
> > ---
> >  docs/specs/pvpanic.rst   | 2 ++
> >  hw/misc/pvpanic.c| 5 +
> >  include/hw/misc/pvpanic.h| 2 +-
> >  include/standard-headers/linux/pvpanic.h | 1 +
> >  4 files changed, 9 insertions(+), 1 deletion(-)
> 
> (...)
> 
> > diff --git a/include/standard-headers/linux/pvpanic.h 
> > b/include/standard-headers/linux/pvpanic.h
> > index 54b7485390d3..38e53ad45929 100644
> > --- a/include/standard-headers/linux/pvpanic.h
> > +++ b/include/standard-headers/linux/pvpanic.h
> > @@ -5,5 +5,6 @@
> >  
> >  #define PVPANIC_PANICKED   (1 << 0)
> >  #define PVPANIC_CRASH_LOADED   (1 << 1)
> > +#define PVPANIC_SHUTDOWN   (1 << 2)
> >  
> >  #endif /* __PVPANIC_H__ */
> >
> 
> This hunk needs to come in via a separate headers update, or has to be
> split out into a placeholder patch if it is not included in the Linux
> kernel yet.

Greg KH actually want this header removed from the Linux UAPI headers,
as it is not in fact a Linux UAPI [0].
It's also a weird workflow to have the specification in qemu but the
header as part of Linux that is re-imported in qemu.

What do you think about maintaining the header as a private part of qemu
and dropping it from Linux UAPI?

Contrary to my response to Greg this wouldn't break old versions of
qemu, as qemu is using a private copy that would still exist there.

[0] https://lore.kernel.org/lkml/2023110431-pacemaker-pruning-0e4c@gregkh/



[PATCH v2 3/3] hw/misc/pvpanic: add support for normal shutdowns

2023-11-28 Thread Thomas Weißschuh
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.

Signed-off-by: Thomas Weißschuh 
---
 docs/specs/pvpanic.rst   | 2 ++
 hw/misc/pvpanic.c| 5 +
 include/hw/misc/pvpanic.h| 2 +-
 include/standard-headers/linux/pvpanic.h | 1 +
 4 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/docs/specs/pvpanic.rst b/docs/specs/pvpanic.rst
index f894bc19555f..796cc0348a38 100644
--- a/docs/specs/pvpanic.rst
+++ b/docs/specs/pvpanic.rst
@@ -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
 -
diff --git a/hw/misc/pvpanic.c b/hw/misc/pvpanic.c
index a4982cc5928e..246f9ae4e992 100644
--- a/hw/misc/pvpanic.c
+++ b/hw/misc/pvpanic.c
@@ -40,6 +40,11 @@ static void handle_event(int event)
 qemu_system_guest_crashloaded(NULL);
 return;
 }
+
+if (event & PVPANIC_SHUTDOWN) {
+qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
+return;
+}
 }
 
 /* return supported events on read */
diff --git a/include/hw/misc/pvpanic.h b/include/hw/misc/pvpanic.h
index 198047dc86ff..fa76ad93d998 100644
--- a/include/hw/misc/pvpanic.h
+++ b/include/hw/misc/pvpanic.h
@@ -23,7 +23,7 @@
 #define TYPE_PVPANIC_PCI_DEVICE "pvpanic-pci"
 
 #define PVPANIC_IOPORT_PROP "ioport"
-#define PVPANIC_EVENTS (PVPANIC_PANICKED | PVPANIC_CRASH_LOADED)
+#define PVPANIC_EVENTS (PVPANIC_PANICKED | PVPANIC_CRASH_LOADED | 
PVPANIC_SHUTDOWN)
 
 /*
  * PVPanicState for any device type
diff --git a/include/standard-headers/linux/pvpanic.h 
b/include/standard-headers/linux/pvpanic.h
index 54b7485390d3..38e53ad45929 100644
--- a/include/standard-headers/linux/pvpanic.h
+++ b/include/standard-headers/linux/pvpanic.h
@@ -5,5 +5,6 @@
 
 #define PVPANIC_PANICKED   (1 << 0)
 #define PVPANIC_CRASH_LOADED   (1 << 1)
+#define PVPANIC_SHUTDOWN   (1 << 2)
 
 #endif /* __PVPANIC_H__ */

-- 
2.43.0




[PATCH v2 2/3] tests/qtest/pvpanic: use centralized definition of supported events

2023-11-28 Thread Thomas Weißschuh
Avoid the necessity to update all tests when new events are added
to the device.

Acked-by: Thomas Huth 
Signed-off-by: Thomas Weißschuh 
---
 tests/qtest/pvpanic-pci-test.c | 5 +++--
 tests/qtest/pvpanic-test.c | 5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/tests/qtest/pvpanic-pci-test.c b/tests/qtest/pvpanic-pci-test.c
index 2c05b376ba72..b372caf41dc0 100644
--- a/tests/qtest/pvpanic-pci-test.c
+++ b/tests/qtest/pvpanic-pci-test.c
@@ -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, , sizeof(val));
-g_assert_cmpuint(val, ==, 3);
+g_assert_cmpuint(val, ==, PVPANIC_EVENTS);
 
 val = 1;
 qpci_memwrite(dev, bar, 0, , sizeof(val));
@@ -67,7 +68,7 @@ static void test_panic(void)
 bar = qpci_iomap(dev, 0, NULL);
 
 qpci_memread(dev, bar, 0, , sizeof(val));
-g_assert_cmpuint(val, ==, 3);
+g_assert_cmpuint(val, ==, PVPANIC_EVENTS);
 
 val = 1;
 qpci_memwrite(dev, bar, 0, , sizeof(val));
diff --git a/tests/qtest/pvpanic-test.c b/tests/qtest/pvpanic-test.c
index 78f1cf8186b0..ccc603472f5d 100644
--- a/tests/qtest/pvpanic-test.c
+++ b/tests/qtest/pvpanic-test.c
@@ -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);
 

-- 
2.43.0




[PATCH v2 0/3] hw/misc/pvpanic: add support for normal shutdowns

2023-11-28 Thread Thomas Weißschuh
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.

I sent an RFC[0] for this before to qemu-devel and lkml which didn't
generate feedback, so let's discuss the concrete proposal.

Patch 1 and 2 are general cleanups, that seems useful even without this
proposal being implemented.

I'll send the corresponding Linux patch to LKML.

[0] https://lore.kernel.org/all/984794aa-4af0-4c68-a74e-7420ec315...@t-8ch.de/

Signed-off-by: Thomas Weißschuh 
---
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/20231104-pvpanic-shutdown-v1-0-023531578...@t-8ch.de

---
Thomas Weißschuh (3):
  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

 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| 2 ++
 include/standard-headers/linux/pvpanic.h | 1 +
 tests/qtest/pvpanic-pci-test.c   | 5 +++--
 tests/qtest/pvpanic-test.c   | 5 +++--
 8 files changed, 19 insertions(+), 10 deletions(-)
---
base-commit: 9155a938cf8fdcb29b760acb8a742bb48be9000f
change-id: 20231104-pvpanic-shutdown-02e4b4cb4949

Best regards,
-- 
Thomas Weißschuh 




[PATCH v2 1/3] hw/misc/pvpanic: centralize definition of supported events

2023-11-28 Thread Thomas Weißschuh
The 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.

Signed-off-by: Thomas Weißschuh 
---
 hw/misc/pvpanic-isa.c | 3 +--
 hw/misc/pvpanic-pci.c | 3 +--
 hw/misc/pvpanic.c | 3 +--
 include/hw/misc/pvpanic.h | 2 ++
 4 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/hw/misc/pvpanic-isa.c b/hw/misc/pvpanic-isa.c
index ccec50f61bbd..9a923b786907 100644
--- a/hw/misc/pvpanic-isa.c
+++ b/hw/misc/pvpanic-isa.c
@@ -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)
@@ -102,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(),
 };
 
diff --git a/hw/misc/pvpanic-pci.c b/hw/misc/pvpanic-pci.c
index fbcaa50731b3..8898d280d2ef 100644
--- a/hw/misc/pvpanic-pci.c
+++ b/hw/misc/pvpanic-pci.c
@@ -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)
 
@@ -55,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(),
 };
 
diff --git a/hw/misc/pvpanic.c b/hw/misc/pvpanic.c
index 1540e9091a45..a4982cc5928e 100644
--- a/hw/misc/pvpanic.c
+++ b/hw/misc/pvpanic.c
@@ -21,13 +21,12 @@
 #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)
 {
 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;
 }
diff --git a/include/hw/misc/pvpanic.h b/include/hw/misc/pvpanic.h
index fab94165d03d..198047dc86ff 100644
--- a/include/hw/misc/pvpanic.h
+++ b/include/hw/misc/pvpanic.h
@@ -17,11 +17,13 @@
 
 #include "exec/memory.h"
 #include "qom/object.h"
+#include "standard-headers/linux/pvpanic.h"
 
 #define TYPE_PVPANIC_ISA_DEVICE "pvpanic"
 #define TYPE_PVPANIC_PCI_DEVICE "pvpanic-pci"
 
 #define PVPANIC_IOPORT_PROP "ioport"
+#define PVPANIC_EVENTS (PVPANIC_PANICKED | PVPANIC_CRASH_LOADED)
 
 /*
  * PVPanicState for any device type

-- 
2.43.0




[PATCH 2/2] linux-user/elfload: check PR_GET_DUMPABLE before creating coredump

2023-11-15 Thread Thomas Weißschuh
A process can opt-out of coredump creation by calling
prctl(PR_SET_DUMPABLE, 0).
linux-user passes this call from the guest through to the
operating system.
>From there it can be read back again to avoid creating coredumps from
qemu-user itself if the guest chose so.

Signed-off-by: Thomas Weißschuh 
---
 linux-user/elfload.c | 5 +
 1 file changed, 5 insertions(+)

diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index 799fe8497346..76d5740af0ca 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -2,6 +2,7 @@
 #include "qemu/osdep.h"
 #include 
 
+#include 
 #include 
 #include 
 
@@ -4667,6 +4668,10 @@ static int elf_core_dump(int signr, const CPUArchState 
*env)
 init_note_info();
 
 errno = 0;
+
+if (prctl(PR_GET_DUMPABLE) == 0)
+return 0;
+
 if (getrlimit(RLIMIT_CORE, ) == 0 && dumpsize.rlim_cur == 0)
 return 0;
 

-- 
2.42.1




[PATCH 0/2] linux-user: two fixes to coredump generation

2023-11-15 Thread Thomas Weißschuh
Signed-off-by: Thomas Weißschuh 
---
Thomas Weißschuh (2):
  linux-user/elfload: test return value of getrlimit
  linux-user/elfload: check PR_GET_DUMPABLE before creating coredump

 linux-user/elfload.c | 8 ++--
 1 file changed, 6 insertions(+), 2 deletions(-)
---
base-commit: 9c673a41eefc50f1cb2fe3c083e7de842c7d276a
change-id: 20231115-qemu-user-dumpable-d499c0396103

Best regards,
-- 
Thomas Weißschuh 




[PATCH 1/2] linux-user/elfload: test return value of getrlimit

2023-11-15 Thread Thomas Weißschuh
Should getrlimit() fail the value of dumpsize.rlimit_cur may not be
initialized. Avoid reading garbage data by checking the return value of
getrlimit.

Signed-off-by: Thomas Weißschuh 
---
 linux-user/elfload.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index 4cd6891d7b6a..799fe8497346 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -4667,8 +4667,7 @@ static int elf_core_dump(int signr, const CPUArchState 
*env)
 init_note_info();
 
 errno = 0;
-getrlimit(RLIMIT_CORE, );
-if (dumpsize.rlim_cur == 0)
+if (getrlimit(RLIMIT_CORE, ) == 0 && dumpsize.rlim_cur == 0)
 return 0;
 
 corefile = core_dump_filename(ts);

-- 
2.42.1




[PATCH RFC 2/3] tests/qtest/pvanic: use centralized definition of supported events

2023-11-04 Thread Thomas Weißschuh
Avoid the necessity to update all tests when new events are added
to the device.

Signed-off-by: Thomas Weißschuh 
---
 tests/qtest/pvpanic-pci-test.c | 5 +++--
 tests/qtest/pvpanic-test.c | 5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/tests/qtest/pvpanic-pci-test.c b/tests/qtest/pvpanic-pci-test.c
index 2c05b376ba72..b372caf41dc0 100644
--- a/tests/qtest/pvpanic-pci-test.c
+++ b/tests/qtest/pvpanic-pci-test.c
@@ -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, , sizeof(val));
-g_assert_cmpuint(val, ==, 3);
+g_assert_cmpuint(val, ==, PVPANIC_EVENTS);
 
 val = 1;
 qpci_memwrite(dev, bar, 0, , sizeof(val));
@@ -67,7 +68,7 @@ static void test_panic(void)
 bar = qpci_iomap(dev, 0, NULL);
 
 qpci_memread(dev, bar, 0, , sizeof(val));
-g_assert_cmpuint(val, ==, 3);
+g_assert_cmpuint(val, ==, PVPANIC_EVENTS);
 
 val = 1;
 qpci_memwrite(dev, bar, 0, , sizeof(val));
diff --git a/tests/qtest/pvpanic-test.c b/tests/qtest/pvpanic-test.c
index 78f1cf8186b0..ccc603472f5d 100644
--- a/tests/qtest/pvpanic-test.c
+++ b/tests/qtest/pvpanic-test.c
@@ -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);
 

-- 
2.42.0




[PATCH RFC 1/3] hw/misc/pvpanic: centralize definition of supported events

2023-11-04 Thread Thomas Weißschuh
The 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.

Signed-off-by: Thomas Weißschuh 
---
 hw/misc/pvpanic-isa.c | 3 +--
 hw/misc/pvpanic-pci.c | 3 +--
 hw/misc/pvpanic.c | 3 +--
 include/hw/misc/pvpanic.h | 2 ++
 4 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/hw/misc/pvpanic-isa.c b/hw/misc/pvpanic-isa.c
index ccec50f61bbd..9a923b786907 100644
--- a/hw/misc/pvpanic-isa.c
+++ b/hw/misc/pvpanic-isa.c
@@ -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)
@@ -102,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(),
 };
 
diff --git a/hw/misc/pvpanic-pci.c b/hw/misc/pvpanic-pci.c
index fbcaa50731b3..8898d280d2ef 100644
--- a/hw/misc/pvpanic-pci.c
+++ b/hw/misc/pvpanic-pci.c
@@ -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)
 
@@ -55,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(),
 };
 
diff --git a/hw/misc/pvpanic.c b/hw/misc/pvpanic.c
index 1540e9091a45..a4982cc5928e 100644
--- a/hw/misc/pvpanic.c
+++ b/hw/misc/pvpanic.c
@@ -21,13 +21,12 @@
 #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)
 {
 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;
 }
diff --git a/include/hw/misc/pvpanic.h b/include/hw/misc/pvpanic.h
index fab94165d03d..198047dc86ff 100644
--- a/include/hw/misc/pvpanic.h
+++ b/include/hw/misc/pvpanic.h
@@ -17,11 +17,13 @@
 
 #include "exec/memory.h"
 #include "qom/object.h"
+#include "standard-headers/linux/pvpanic.h"
 
 #define TYPE_PVPANIC_ISA_DEVICE "pvpanic"
 #define TYPE_PVPANIC_PCI_DEVICE "pvpanic-pci"
 
 #define PVPANIC_IOPORT_PROP "ioport"
+#define PVPANIC_EVENTS (PVPANIC_PANICKED | PVPANIC_CRASH_LOADED)
 
 /*
  * PVPanicState for any device type

-- 
2.42.0




[PATCH RFC 3/3] hw/misc/pvpanic: add support for normal shutdowns

2023-11-04 Thread Thomas Weißschuh
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.

Signed-off-by: Thomas Weißschuh 
---
 docs/specs/pvpanic.rst   | 2 ++
 hw/misc/pvpanic.c| 5 +
 include/hw/misc/pvpanic.h| 2 +-
 include/standard-headers/linux/pvpanic.h | 1 +
 4 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/docs/specs/pvpanic.rst b/docs/specs/pvpanic.rst
index f894bc19555f..796cc0348a38 100644
--- a/docs/specs/pvpanic.rst
+++ b/docs/specs/pvpanic.rst
@@ -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
 -
diff --git a/hw/misc/pvpanic.c b/hw/misc/pvpanic.c
index a4982cc5928e..246f9ae4e992 100644
--- a/hw/misc/pvpanic.c
+++ b/hw/misc/pvpanic.c
@@ -40,6 +40,11 @@ static void handle_event(int event)
 qemu_system_guest_crashloaded(NULL);
 return;
 }
+
+if (event & PVPANIC_SHUTDOWN) {
+qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
+return;
+}
 }
 
 /* return supported events on read */
diff --git a/include/hw/misc/pvpanic.h b/include/hw/misc/pvpanic.h
index 198047dc86ff..fa76ad93d998 100644
--- a/include/hw/misc/pvpanic.h
+++ b/include/hw/misc/pvpanic.h
@@ -23,7 +23,7 @@
 #define TYPE_PVPANIC_PCI_DEVICE "pvpanic-pci"
 
 #define PVPANIC_IOPORT_PROP "ioport"
-#define PVPANIC_EVENTS (PVPANIC_PANICKED | PVPANIC_CRASH_LOADED)
+#define PVPANIC_EVENTS (PVPANIC_PANICKED | PVPANIC_CRASH_LOADED | 
PVPANIC_SHUTDOWN)
 
 /*
  * PVPanicState for any device type
diff --git a/include/standard-headers/linux/pvpanic.h 
b/include/standard-headers/linux/pvpanic.h
index 54b7485390d3..38e53ad45929 100644
--- a/include/standard-headers/linux/pvpanic.h
+++ b/include/standard-headers/linux/pvpanic.h
@@ -5,5 +5,6 @@
 
 #define PVPANIC_PANICKED   (1 << 0)
 #define PVPANIC_CRASH_LOADED   (1 << 1)
+#define PVPANIC_SHUTDOWN   (1 << 2)
 
 #endif /* __PVPANIC_H__ */

-- 
2.42.0




[PATCH RFC 0/3] hw/misc/pvpanic: add support for normal shutdowns

2023-11-04 Thread Thomas Weißschuh
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.

I sent an RFC[0] for this before to qemu-devel and lkml which didn't
generate feedback, so let's discuss the concrete proposal.

Patch 1 and 2 are general cleanups, that seems useful even without this
proposal being implemented.

I'll send the corresponding Linux patch to LKML.

[0] https://lore.kernel.org/all/984794aa-4af0-4c68-a74e-7420ec315...@t-8ch.de/

Signed-off-by: Thomas Weißschuh 
---
Thomas Weißschuh (3):
  hw/misc/pvpanic: centralize definition of supported events
  tests/qtest/pvanic: use centralized definition of supported events
  hw/misc/pvpanic: add support for normal shutdowns

 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| 2 ++
 include/standard-headers/linux/pvpanic.h | 1 +
 tests/qtest/pvpanic-pci-test.c   | 5 +++--
 tests/qtest/pvpanic-test.c   | 5 +++--
 8 files changed, 19 insertions(+), 10 deletions(-)
---
base-commit: d762bf97931b58839316b68a570eecc6143c9e3e
change-id: 20231104-pvpanic-shutdown-02e4b4cb4949

Best regards,
-- 
Thomas Weißschuh 




[RFC] pvpanic notifications for non-panic reboot events

2023-10-26 Thread Thomas Weißschuh
Hi everybody,

the mechanism for a (Linux) guest to signal a regular shutdown event to
QEMU seems fairly architecture specific and dependent on kernel
configuration.

The existing pvpanic protocol [0] could be extended fairly easily to
also cover these events.

Any thoughts?

Thanks,
Thomas

[0] https://github.com/qemu/qemu/blob/master/docs/specs/pvpanic.txt



[PATCH v2] hw/loongarch: remove global loaderparams variable

2023-10-10 Thread Thomas Weißschuh
Passing the struct around explicitly makes the control-flow more
obvious.

Reviewed-by: Philippe Mathieu-Daudé 
Signed-off-by: Thomas Weißschuh 
---
Changes in v2:
- Drop initialize loaderparams variable
- Link to v1: 
https://lore.kernel.org/qemu-devel/20231009210018.249776-1-tho...@t-8ch.de/
---
 hw/loongarch/virt.c | 50 +++---
 1 file changed, 27 insertions(+), 23 deletions(-)

diff --git a/hw/loongarch/virt.c b/hw/loongarch/virt.c
index 2629128aeda4..88bf0b0e97e1 100644
--- a/hw/loongarch/virt.c
+++ b/hw/loongarch/virt.c
@@ -47,6 +47,13 @@
 #include "qemu/error-report.h"
 
 
+struct loaderparams {
+uint64_t ram_size;
+const char *kernel_filename;
+const char *kernel_cmdline;
+const char *initrd_filename;
+};
+
 static void virt_flash_create(LoongArchMachineState *lams)
 {
 DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01);
@@ -411,24 +418,17 @@ static const MemoryRegionOps loongarch_virt_pm_ops = {
 }
 };
 
-static struct _loaderparams {
-uint64_t ram_size;
-const char *kernel_filename;
-const char *kernel_cmdline;
-const char *initrd_filename;
-} loaderparams;
-
 static uint64_t cpu_loongarch_virt_to_phys(void *opaque, uint64_t addr)
 {
 return addr & MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
 }
 
-static int64_t load_kernel_info(void)
+static int64_t load_kernel_info(const struct loaderparams *loaderparams)
 {
 uint64_t kernel_entry, kernel_low, kernel_high;
 ssize_t kernel_size;
 
-kernel_size = load_elf(loaderparams.kernel_filename, NULL,
+kernel_size = load_elf(loaderparams->kernel_filename, NULL,
cpu_loongarch_virt_to_phys, NULL,
_entry, _low,
_high, NULL, 0,
@@ -436,7 +436,7 @@ static int64_t load_kernel_info(void)
 
 if (kernel_size < 0) {
 error_report("could not load kernel '%s': %s",
- loaderparams.kernel_filename,
+ loaderparams->kernel_filename,
  load_elf_strerror(kernel_size));
 exit(1);
 }
@@ -728,7 +728,8 @@ static void reset_load_elf(void *opaque)
 }
 }
 
-static void fw_cfg_add_kernel_info(FWCfgState *fw_cfg)
+static void fw_cfg_add_kernel_info(const struct loaderparams *loaderparams,
+   FWCfgState *fw_cfg)
 {
 /*
  * Expose the kernel, the command line, and the initrd in fw_cfg.
@@ -737,36 +738,38 @@ static void fw_cfg_add_kernel_info(FWCfgState *fw_cfg)
  */
 load_image_to_fw_cfg(fw_cfg,
  FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA,
- loaderparams.kernel_filename,
+ loaderparams->kernel_filename,
  false);
 
-if (loaderparams.initrd_filename) {
+if (loaderparams->initrd_filename) {
 load_image_to_fw_cfg(fw_cfg,
  FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA,
- loaderparams.initrd_filename, false);
+ loaderparams->initrd_filename, false);
 }
 
-if (loaderparams.kernel_cmdline) {
+if (loaderparams->kernel_cmdline) {
 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
-   strlen(loaderparams.kernel_cmdline) + 1);
+   strlen(loaderparams->kernel_cmdline) + 1);
 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA,
-  loaderparams.kernel_cmdline);
+  loaderparams->kernel_cmdline);
 }
 }
 
-static void loongarch_firmware_boot(LoongArchMachineState *lams)
+static void loongarch_firmware_boot(LoongArchMachineState *lams,
+const struct loaderparams *loaderparams)
 {
-fw_cfg_add_kernel_info(lams->fw_cfg);
+fw_cfg_add_kernel_info(loaderparams, lams->fw_cfg);
 }
 
-static void loongarch_direct_kernel_boot(LoongArchMachineState *lams)
+static void loongarch_direct_kernel_boot(LoongArchMachineState *lams,
+ const struct loaderparams 
*loaderparams)
 {
 MachineState *machine = MACHINE(lams);
 int64_t kernel_addr = 0;
 LoongArchCPU *lacpu;
 int i;
 
-kernel_addr = load_kernel_info();
+kernel_addr = load_kernel_info(loaderparams);
 if (!machine->firmware) {
 for (i = 0; i < machine->smp.cpus; i++) {
 lacpu = LOONGARCH_CPU(qemu_get_cpu(i));
@@ -793,6 +796,7 @@ static void loongarch_init(MachineState *machine)
 MachineClass *mc = MACHINE_GET_CLASS(machine);
 CPUState *cpu;
 char *ramName = NULL;
+struct loaderparams loaderparams = { };
 
 if (!cpu_model) {
 cpu_model = LOONGARCH_CPU_TYPE_NAME("la464");
@@ -898,9 +902,9 @@ static void loongarch_init(MachineState *machine)
 /* load the kernel. */
 if (loaderparams.kernel_filename) {

[PATCH] hw/loongarch: remove global loaderparams variable

2023-10-09 Thread Thomas Weißschuh
Passing the struct around explicitly makes the control-flow more
obvious.

Signed-off-by: Thomas Weißschuh 
---
 hw/loongarch/virt.c | 50 -
 1 file changed, 27 insertions(+), 23 deletions(-)

diff --git a/hw/loongarch/virt.c b/hw/loongarch/virt.c
index 2629128aeda4..ea7eb408c071 100644
--- a/hw/loongarch/virt.c
+++ b/hw/loongarch/virt.c
@@ -47,6 +47,13 @@
 #include "qemu/error-report.h"
 
 
+struct loaderparams {
+uint64_t ram_size;
+const char *kernel_filename;
+const char *kernel_cmdline;
+const char *initrd_filename;
+};
+
 static void virt_flash_create(LoongArchMachineState *lams)
 {
 DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01);
@@ -411,24 +418,17 @@ static const MemoryRegionOps loongarch_virt_pm_ops = {
 }
 };
 
-static struct _loaderparams {
-uint64_t ram_size;
-const char *kernel_filename;
-const char *kernel_cmdline;
-const char *initrd_filename;
-} loaderparams;
-
 static uint64_t cpu_loongarch_virt_to_phys(void *opaque, uint64_t addr)
 {
 return addr & MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
 }
 
-static int64_t load_kernel_info(void)
+static int64_t load_kernel_info(const struct loaderparams *loaderparams)
 {
 uint64_t kernel_entry, kernel_low, kernel_high;
 ssize_t kernel_size;
 
-kernel_size = load_elf(loaderparams.kernel_filename, NULL,
+kernel_size = load_elf(loaderparams->kernel_filename, NULL,
cpu_loongarch_virt_to_phys, NULL,
_entry, _low,
_high, NULL, 0,
@@ -436,7 +436,7 @@ static int64_t load_kernel_info(void)
 
 if (kernel_size < 0) {
 error_report("could not load kernel '%s': %s",
- loaderparams.kernel_filename,
+ loaderparams->kernel_filename,
  load_elf_strerror(kernel_size));
 exit(1);
 }
@@ -728,7 +728,8 @@ static void reset_load_elf(void *opaque)
 }
 }
 
-static void fw_cfg_add_kernel_info(FWCfgState *fw_cfg)
+static void fw_cfg_add_kernel_info(const struct loaderparams *loaderparams,
+   FWCfgState *fw_cfg)
 {
 /*
  * Expose the kernel, the command line, and the initrd in fw_cfg.
@@ -737,36 +738,38 @@ static void fw_cfg_add_kernel_info(FWCfgState *fw_cfg)
  */
 load_image_to_fw_cfg(fw_cfg,
  FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA,
- loaderparams.kernel_filename,
+ loaderparams->kernel_filename,
  false);
 
-if (loaderparams.initrd_filename) {
+if (loaderparams->initrd_filename) {
 load_image_to_fw_cfg(fw_cfg,
  FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA,
- loaderparams.initrd_filename, false);
+ loaderparams->initrd_filename, false);
 }
 
-if (loaderparams.kernel_cmdline) {
+if (loaderparams->kernel_cmdline) {
 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
-   strlen(loaderparams.kernel_cmdline) + 1);
+   strlen(loaderparams->kernel_cmdline) + 1);
 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA,
-  loaderparams.kernel_cmdline);
+  loaderparams->kernel_cmdline);
 }
 }
 
-static void loongarch_firmware_boot(LoongArchMachineState *lams)
+static void loongarch_firmware_boot(LoongArchMachineState *lams,
+const struct loaderparams *loaderparams)
 {
-fw_cfg_add_kernel_info(lams->fw_cfg);
+fw_cfg_add_kernel_info(loaderparams, lams->fw_cfg);
 }
 
-static void loongarch_direct_kernel_boot(LoongArchMachineState *lams)
+static void loongarch_direct_kernel_boot(LoongArchMachineState *lams,
+ const struct loaderparams 
*loaderparams)
 {
 MachineState *machine = MACHINE(lams);
 int64_t kernel_addr = 0;
 LoongArchCPU *lacpu;
 int i;
 
-kernel_addr = load_kernel_info();
+kernel_addr = load_kernel_info(loaderparams);
 if (!machine->firmware) {
 for (i = 0; i < machine->smp.cpus; i++) {
 lacpu = LOONGARCH_CPU(qemu_get_cpu(i));
@@ -793,6 +796,7 @@ static void loongarch_init(MachineState *machine)
 MachineClass *mc = MACHINE_GET_CLASS(machine);
 CPUState *cpu;
 char *ramName = NULL;
+struct loaderparams loaderparams;
 
 if (!cpu_model) {
 cpu_model = LOONGARCH_CPU_TYPE_NAME("la464");
@@ -898,9 +902,9 @@ static void loongarch_init(MachineState *machine)
 /* load the kernel. */
 if (loaderparams.kernel_filename) {
 if (lams->bios_loaded) {
-loongarch_firmware_boot(lams);
+loongarch_firmware_boot(lams, );
 } else {
-loongarch_direct_kernel_boot(lams);
+loong

[PATCH] linux-user: report ENOTTY for unknown ioctls

2023-04-26 Thread Thomas Weißschuh
The correct error number for unknown ioctls is ENOTTY.

ENOSYS would mean that the ioctl() syscall itself is not implemented,
which is very improbable and unexpected for userspace.

ENOTTY means "Inappropriate ioctl for device". This is what the kernel
returns on unknown ioctls, what qemu is trying to express and what
userspace is prepared to handle.

Signed-off-by: Thomas Weißschuh 
---
 linux-user/syscall.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 69f740ff98c8..c5955313a063 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -5747,7 +5747,7 @@ static abi_long do_ioctl(int fd, int cmd, abi_long arg)
 if (ie->target_cmd == 0) {
 qemu_log_mask(
 LOG_UNIMP, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
-return -TARGET_ENOSYS;
+return -TARGET_ENOTTY;
 }
 if (ie->target_cmd == cmd)
 break;
@@ -5759,7 +5759,7 @@ static abi_long do_ioctl(int fd, int cmd, abi_long arg)
 } else if (!ie->host_cmd) {
 /* Some architectures define BSD ioctls in their headers
that are not implemented in Linux.  */
-return -TARGET_ENOSYS;
+return -TARGET_ENOTTY;
 }
 
 switch(arg_type[0]) {
@@ -5817,7 +5817,7 @@ static abi_long do_ioctl(int fd, int cmd, abi_long arg)
 qemu_log_mask(LOG_UNIMP,
   "Unsupported ioctl type: cmd=0x%04lx type=%d\n",
   (long)cmd, arg_type[0]);
-ret = -TARGET_ENOSYS;
+ret = -TARGET_ENOTTY;
 break;
 }
 return ret;

base-commit: a14b8206c5edcbbad1c71256ea9b44c3b382a9f5
-- 
2.40.1




[PATCH 1/2] linux-user: Add move_mount() syscall

2023-04-24 Thread Thomas Weißschuh
Signed-off-by: Thomas Weißschuh 
---
 linux-user/syscall.c | 27 +++
 1 file changed, 27 insertions(+)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 69f740ff98c8..95e370130cee 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -9139,6 +9139,33 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int 
num, abi_long arg1,
 unlock_user(p, arg1, 0);
 return ret;
 #endif
+#ifdef TARGET_NR_move_mount
+case TARGET_NR_move_mount:
+{
+void *p2, *p4;
+
+if (!arg2 || !arg4) {
+return -TARGET_EFAULT;
+}
+
+p2 = lock_user_string(arg2);
+if (!p2) {
+return -TARGET_EFAULT;
+}
+
+p4 = lock_user_string(arg4);
+if (!p4) {
+unlock_user(p2, arg2, 0);
+return -TARGET_EFAULT;
+}
+ret = get_errno(move_mount(arg1, p2, arg3, p4, arg5));
+
+unlock_user(p2, arg2, 0);
+unlock_user(p4, arg4, 0);
+
+return ret;
+}
+#endif
 #ifdef TARGET_NR_stime /* not on alpha */
 case TARGET_NR_stime:
 {

base-commit: 81072abf1575b11226b3779af76dc71dfa85ee5d
-- 
2.40.0




[PATCH 2/2] linux-user: Add open_tree() syscall

2023-04-24 Thread Thomas Weißschuh
Signed-off-by: Thomas Weißschuh 
---
 linux-user/syscall.c | 26 ++
 1 file changed, 26 insertions(+)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 95e370130cee..140bd2c36e0f 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -9166,6 +9166,32 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int 
num, abi_long arg1,
 return ret;
 }
 #endif
+#ifdef TARGET_NR_open_tree
+case TARGET_NR_open_tree:
+{
+void *p2;
+
+if (!arg2) {
+return -TARGET_EFAULT;
+}
+
+p2 = lock_user_string(arg2);
+if (!p2) {
+return -TARGET_EFAULT;
+}
+
+int host_flags = arg3 & ~TARGET_O_CLOEXEC;
+if (arg3 & TARGET_O_CLOEXEC) {
+host_flags |= O_CLOEXEC;
+}
+
+ret = get_errno(open_tree(arg1, p2, host_flags));
+
+unlock_user(p2, arg2, 0);
+
+return ret;
+}
+#endif
 #ifdef TARGET_NR_stime /* not on alpha */
 case TARGET_NR_stime:
 {
-- 
2.40.0




Re: [PATCH 1/2] tests/tcg/multiarch: Make the system memory test work on big-endian

2023-04-22 Thread Thomas Weißschuh
On 2023-04-22 02:58:07+0200, Ilya Leoshkevich wrote:
> Make sure values are stored in memory as little-endian regardless of
> the host endianness.
> 
> Signed-off-by: Ilya Leoshkevich 
> ---
>  tests/tcg/multiarch/system/memory.c | 24 
>  1 file changed, 24 insertions(+)
> 
> diff --git a/tests/tcg/multiarch/system/memory.c 
> b/tests/tcg/multiarch/system/memory.c
> index 214f7d4f54b..8efb440 100644
> --- a/tests/tcg/multiarch/system/memory.c
> +++ b/tests/tcg/multiarch/system/memory.c
> @@ -121,6 +121,9 @@ static void init_test_data_u16(int offset)
>  for (i = 0; i < max; i++) {
>  uint8_t low = count++, high = count++;
>  word = BYTE_SHIFT(high, 1) | BYTE_SHIFT(low, 0);
> +#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
> +word = __builtin_bswap16(word);
> +#endif

These looks like a usecase for cpu_to_le16() and friends.

>  *ptr++ = word;
>  pdot(i);
>  }
> @@ -142,6 +145,9 @@ static void init_test_data_u32(int offset)
>  uint8_t b4 = count++, b3 = count++;
>  uint8_t b2 = count++, b1 = count++;
>  word = BYTE_SHIFT(b1, 3) | BYTE_SHIFT(b2, 2) | BYTE_SHIFT(b3, 1) | 
> b4;
> +#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
> +word = __builtin_bswap32(word);
> +#endif
>  *ptr++ = word;
>  pdot(i);
>  }
> [..]



[PATCH v2 0/2] linux-user: Fix mincore() with PROT_NONE

2023-04-22 Thread Thomas Weißschuh
The kernel does not require PROT_READ for addresses passed to mincore.

v1: https://lore.kernel.org/qemu-devel/20230416195103.607948-1-tho...@t-8ch.de/
v1 -> v2:
* Introduce symbolic flag VERIFY_NONE instead of hardcoding "0"

Thomas Weißschuh (2):
  linux-user: Add new flag VERIFY_NONE
  linux-user: Don't require PROT_READ for mincore

 linux-user/qemu.h| 1 +
 linux-user/syscall.c | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)


base-commit: 1cc6e1a20144c0ae360cbeb0e035fdee1bd80609
-- 
2.40.0




[PATCH v2 1/2] linux-user: Add new flag VERIFY_NONE

2023-04-22 Thread Thomas Weißschuh
This can be used to validate that an address range is mapped but without
being readable or writable.

It will be used by an updated implementation of mincore().

Signed-off-by: Thomas Weißschuh 
---
 linux-user/qemu.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/linux-user/qemu.h b/linux-user/qemu.h
index e2e93fbd1d5d..92f9f5af41c7 100644
--- a/linux-user/qemu.h
+++ b/linux-user/qemu.h
@@ -168,6 +168,7 @@ abi_long do_brk(abi_ulong new_brk);
 
 /* user access */
 
+#define VERIFY_NONE  0
 #define VERIFY_READ  PAGE_READ
 #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
 
-- 
2.40.0




[PATCH v2 2/2] linux-user: Don't require PROT_READ for mincore

2023-04-22 Thread Thomas Weißschuh
The kernel does not require PROT_READ for addresses passed to mincore.
For example the fincore(1) tool from util-linux uses PROT_NONE and
currently does not work under qemu-user.

Example (with fincore(1) from util-linux 2.38):

$ fincore /proc/self/exe
RES PAGES  SIZE FILE
24K 6 22.1K /proc/self/exe

$ qemu-x86_64 /usr/bin/fincore /proc/self/exe
fincore: failed to do mincore: /proc/self/exe: Cannot allocate memory

With this patch:

$ ./build/qemu-x86_64 /usr/bin/fincore /proc/self/exe
RES PAGES  SIZE FILE
24K 6 22.1K /proc/self/exe

Signed-off-by: Thomas Weißschuh 
---
 linux-user/syscall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 69f740ff98c8..5ec848b459f7 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -11897,7 +11897,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int 
num, abi_long arg1,
 #ifdef TARGET_NR_mincore
 case TARGET_NR_mincore:
 {
-void *a = lock_user(VERIFY_READ, arg1, arg2, 0);
+void *a = lock_user(VERIFY_NONE, arg1, arg2, 0);
 if (!a) {
 return -TARGET_ENOMEM;
 }
-- 
2.40.0




[PATCH] linux-user: Don't require PROT_READ for mincore

2023-04-16 Thread Thomas Weißschuh
The kernel does not require PROT_READ for addresses passed to mincore.
For example the fincore(1) tool from util-linux uses PROT_NONE and
currently does not work under qemu-user.

Example (with fincore(1) from util-linux 2.38):

$ fincore /proc/self/exe
RES PAGES  SIZE FILE
24K 6 22.1K /proc/self/exe

$ qemu-x86_64 /usr/bin/fincore /proc/self/exe
fincore: failed to do mincore: /proc/self/exe: Cannot allocate memory

With this patch:

$ ./build/qemu-x86_64 /usr/bin/fincore /proc/self/exe
RES PAGES  SIZE FILE
24K 6 22.1K /proc/self/exe

Signed-off-by: Thomas Weißschuh 
---
 linux-user/syscall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 69f740ff98c8..0ebcbf3d7f90 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -11897,7 +11897,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int 
num, abi_long arg1,
 #ifdef TARGET_NR_mincore
 case TARGET_NR_mincore:
 {
-void *a = lock_user(VERIFY_READ, arg1, arg2, 0);
+void *a = lock_user(0, arg1, arg2, 0);
 if (!a) {
 return -TARGET_ENOMEM;
 }

base-commit: 7dbd6f8a27e30fe14adb3d5869097cddf24038d6
-- 
2.40.0




[PATCH] firmware: qemu_fw_cfg: make kobj_type structure constant

2023-02-26 Thread Thomas Weißschuh
Since commit ee6d3dd4ed48 ("driver core: make kobj_type constant.")
the driver core allows the usage of const struct kobj_type.

Take advantage of this to constify the structure definition to prevent
modification at runtime.

Signed-off-by: Thomas Weißschuh 
---
 drivers/firmware/qemu_fw_cfg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/firmware/qemu_fw_cfg.c b/drivers/firmware/qemu_fw_cfg.c
index a69399a6b7c0..f41de793f41b 100644
--- a/drivers/firmware/qemu_fw_cfg.c
+++ b/drivers/firmware/qemu_fw_cfg.c
@@ -452,7 +452,7 @@ static void fw_cfg_sysfs_release_entry(struct kobject *kobj)
 }
 
 /* kobj_type: ties together all properties required to register an entry */
-static struct kobj_type fw_cfg_sysfs_entry_ktype = {
+static const struct kobj_type fw_cfg_sysfs_entry_ktype = {
.default_groups = fw_cfg_sysfs_entry_groups,
.sysfs_ops = _cfg_sysfs_attr_ops,
.release = fw_cfg_sysfs_release_entry,

---
base-commit: 2fcd07b7ccd5fd10b2120d298363e4e6c53ccf9c
change-id: 20230227-kobj_type-firmware-qemu-7746b6320db0

Best regards,
-- 
Thomas Weißschuh 




[PATCH v2] vmdk: allow specification of tools version

2021-09-13 Thread Thomas Weißschuh
VMDK files support an attribute that represents the version of the guest
tools that are installed on the disk.
This attribute is used by vSphere before a machine has been started to
determine if the VM has the guest tools installed.
This is important when configuring "Operating system customizations" in
vSphere, as it checks for the presence of the guest tools before
allowing those customizations.
Thus when the VM has not yet booted normally it would be impossible to
customize it, therefore preventing a customized first-boot.

The attribute should not hurt on disks that do not have the guest tools
installed and indeed the VMware tools also unconditionally add this
attribute.
(Defaulting to the value "2147483647", as is done in this patch)

Signed-off-by: Thomas Weißschuh 
---

v1: 
https://lore.kernel.org/qemu-devel/20210908174250.12946-1-thomas.weissschuh@zeiss.com/

v1 -> v2:
* Expand QAPI docs (Eric Blake)

 block/vmdk.c | 24 
 qapi/block-core.json |  3 +++
 2 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/block/vmdk.c b/block/vmdk.c
index 4499f136bd..93ef6426b0 100644
--- a/block/vmdk.c
+++ b/block/vmdk.c
@@ -60,6 +60,7 @@
 #define VMDK_ZEROED  (-3)
 
 #define BLOCK_OPT_ZEROED_GRAIN "zeroed_grain"
+#define BLOCK_OPT_TOOLSVERSION "toolsversion"
 
 typedef struct {
 uint32_t version;
@@ -2344,6 +2345,7 @@ static int coroutine_fn vmdk_co_do_create(int64_t size,
   BlockdevVmdkAdapterType adapter_type,
   const char *backing_file,
   const char *hw_version,
+  const char *toolsversion,
   bool compat6,
   bool zeroed_grain,
   vmdk_create_extent_fn extent_fn,
@@ -2384,7 +2386,8 @@ static int coroutine_fn vmdk_co_do_create(int64_t size,
 "ddb.geometry.cylinders = \"%" PRId64 "\"\n"
 "ddb.geometry.heads = \"%" PRIu32 "\"\n"
 "ddb.geometry.sectors = \"63\"\n"
-"ddb.adapterType = \"%s\"\n";
+"ddb.adapterType = \"%s\"\n"
+"ddb.toolsVersion = \"%s\"\n";
 
 ext_desc_lines = g_string_new(NULL);
 
@@ -2401,6 +2404,9 @@ static int coroutine_fn vmdk_co_do_create(int64_t size,
 if (!hw_version) {
 hw_version = "4";
 }
+if (!toolsversion) {
+toolsversion = "2147483647";
+}
 
 if (adapter_type != BLOCKDEV_VMDK_ADAPTER_TYPE_IDE) {
 /* that's the number of heads with which vmware operates when
@@ -2525,7 +2531,8 @@ static int coroutine_fn vmdk_co_do_create(int64_t size,
size /
(int64_t)(63 * number_heads * BDRV_SECTOR_SIZE),
number_heads,
-   BlockdevVmdkAdapterType_str(adapter_type));
+   BlockdevVmdkAdapterType_str(adapter_type),
+   toolsversion);
 desc_len = strlen(desc);
 /* the descriptor offset = 0x200 */
 if (!split && !flat) {
@@ -2617,6 +2624,7 @@ static int coroutine_fn vmdk_co_create_opts(BlockDriver 
*drv,
 BlockdevVmdkAdapterType adapter_type_enum;
 char *backing_file = NULL;
 char *hw_version = NULL;
+char *toolsversion = NULL;
 char *fmt = NULL;
 BlockdevVmdkSubformat subformat;
 int ret = 0;
@@ -2649,6 +2657,7 @@ static int coroutine_fn vmdk_co_create_opts(BlockDriver 
*drv,
 adapter_type = qemu_opt_get_del(opts, BLOCK_OPT_ADAPTER_TYPE);
 backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
 hw_version = qemu_opt_get_del(opts, BLOCK_OPT_HWVERSION);
+toolsversion = qemu_opt_get_del(opts, BLOCK_OPT_TOOLSVERSION);
 compat6 = qemu_opt_get_bool_del(opts, BLOCK_OPT_COMPAT6, false);
 if (strcmp(hw_version, "undefined") == 0) {
 g_free(hw_version);
@@ -2692,14 +2701,15 @@ static int coroutine_fn vmdk_co_create_opts(BlockDriver 
*drv,
 .opts = opts,
 };
 ret = vmdk_co_do_create(total_size, subformat, adapter_type_enum,
-backing_file, hw_version, compat6, zeroed_grain,
-vmdk_co_create_opts_cb, , errp);
+backing_file, hw_version, toolsversion, compat6,
+zeroed_grain, vmdk_co_create_opts_cb, , errp);
 
 exit:
 g_free(backing_fmt);
 g_free(adapter_type);
 g_free(backing_file);
 g_free(hw_version);
+g_free(toolsversion);
 g_free(fmt);
 g_free(desc);
 g_free(path);
@@ -2782,6 +2792,7 @@ static int coroutine_fn 
vmdk_co_create(BlockdevCreateOptions *create_options,
  

[PATCH] vmdk: allow specification of tools version

2021-09-08 Thread Thomas Weißschuh
VMDK files support an attribute that represents the version of the guest
tools that are installed on the disk.
This attribute is used by vSphere before a machine has been started to
determine if the VM has the guest tools installed.
This is important when configuring "Operating system customizations" in
vSphere, as it checks for the presence of the guest tools before
allowing those customizations.
Thus when the VM has not yet booted normally it would be impossible to
customize it, therefore preventing a customized first-boot.

The attribute should not hurt on disks that do not have the guest tools
installed and indeed the VMware tools also unconditionally add this
attribute.
(Defaulting to the value "2147483647", as is done in this patch)

Signed-off-by: Thomas Weißschuh 
---
 block/vmdk.c | 24 
 qapi/block-core.json |  2 ++
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/block/vmdk.c b/block/vmdk.c
index 4499f136bd..93ef6426b0 100644
--- a/block/vmdk.c
+++ b/block/vmdk.c
@@ -60,6 +60,7 @@
 #define VMDK_ZEROED  (-3)
 
 #define BLOCK_OPT_ZEROED_GRAIN "zeroed_grain"
+#define BLOCK_OPT_TOOLSVERSION "toolsversion"
 
 typedef struct {
 uint32_t version;
@@ -2344,6 +2345,7 @@ static int coroutine_fn vmdk_co_do_create(int64_t size,
   BlockdevVmdkAdapterType adapter_type,
   const char *backing_file,
   const char *hw_version,
+  const char *toolsversion,
   bool compat6,
   bool zeroed_grain,
   vmdk_create_extent_fn extent_fn,
@@ -2384,7 +2386,8 @@ static int coroutine_fn vmdk_co_do_create(int64_t size,
 "ddb.geometry.cylinders = \"%" PRId64 "\"\n"
 "ddb.geometry.heads = \"%" PRIu32 "\"\n"
 "ddb.geometry.sectors = \"63\"\n"
-"ddb.adapterType = \"%s\"\n";
+"ddb.adapterType = \"%s\"\n"
+"ddb.toolsVersion = \"%s\"\n";
 
 ext_desc_lines = g_string_new(NULL);
 
@@ -2401,6 +2404,9 @@ static int coroutine_fn vmdk_co_do_create(int64_t size,
 if (!hw_version) {
 hw_version = "4";
 }
+if (!toolsversion) {
+toolsversion = "2147483647";
+}
 
 if (adapter_type != BLOCKDEV_VMDK_ADAPTER_TYPE_IDE) {
 /* that's the number of heads with which vmware operates when
@@ -2525,7 +2531,8 @@ static int coroutine_fn vmdk_co_do_create(int64_t size,
size /
(int64_t)(63 * number_heads * BDRV_SECTOR_SIZE),
number_heads,
-   BlockdevVmdkAdapterType_str(adapter_type));
+   BlockdevVmdkAdapterType_str(adapter_type),
+   toolsversion);
 desc_len = strlen(desc);
 /* the descriptor offset = 0x200 */
 if (!split && !flat) {
@@ -2617,6 +2624,7 @@ static int coroutine_fn vmdk_co_create_opts(BlockDriver 
*drv,
 BlockdevVmdkAdapterType adapter_type_enum;
 char *backing_file = NULL;
 char *hw_version = NULL;
+char *toolsversion = NULL;
 char *fmt = NULL;
 BlockdevVmdkSubformat subformat;
 int ret = 0;
@@ -2649,6 +2657,7 @@ static int coroutine_fn vmdk_co_create_opts(BlockDriver 
*drv,
 adapter_type = qemu_opt_get_del(opts, BLOCK_OPT_ADAPTER_TYPE);
 backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
 hw_version = qemu_opt_get_del(opts, BLOCK_OPT_HWVERSION);
+toolsversion = qemu_opt_get_del(opts, BLOCK_OPT_TOOLSVERSION);
 compat6 = qemu_opt_get_bool_del(opts, BLOCK_OPT_COMPAT6, false);
 if (strcmp(hw_version, "undefined") == 0) {
 g_free(hw_version);
@@ -2692,14 +2701,15 @@ static int coroutine_fn vmdk_co_create_opts(BlockDriver 
*drv,
 .opts = opts,
 };
 ret = vmdk_co_do_create(total_size, subformat, adapter_type_enum,
-backing_file, hw_version, compat6, zeroed_grain,
-vmdk_co_create_opts_cb, , errp);
+backing_file, hw_version, toolsversion, compat6,
+zeroed_grain, vmdk_co_create_opts_cb, , errp);
 
 exit:
 g_free(backing_fmt);
 g_free(adapter_type);
 g_free(backing_file);
 g_free(hw_version);
+g_free(toolsversion);
 g_free(fmt);
 g_free(desc);
 g_free(path);
@@ -2782,6 +2792,7 @@ static int coroutine_fn 
vmdk_co_create(BlockdevCreateOptions *create_options,
 opts->adapter_type,
 opts->backing_file,
 opts->hwversio