From 7eb44dbcd3f693b9031a8c005aab9ea268774aea Mon Sep 17 00:00:00 2001 From: Wu Zhou Date: Thu, 3 Aug 2023 20:15:48 -0700 Subject: [PATCH] dm: vm_event: send poweroff event on pm port write When the virtual PM port is written, we can infer that guest has just initiated a poweroff action. So we send a poweroff event upon this port write. The DM event handler will try to emit it (to Libvirt). Developers can write app/script to decide what to do with this event. Tracked-On: #8547 Signed-off-by: Wu Zhou Reviewed-by: Jian Jun Chen --- devicemodel/arch/x86/pm.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/devicemodel/arch/x86/pm.c b/devicemodel/arch/x86/pm.c index 3b3a60685..c2d054bec 100644 --- a/devicemodel/arch/x86/pm.c +++ b/devicemodel/arch/x86/pm.c @@ -42,6 +42,7 @@ #include "lpc.h" #include "monitor.h" #include "log.h" +#include "vm_event.h" static pthread_mutex_t pm_lock = PTHREAD_MUTEX_INITIALIZER; static struct mevent *power_button; @@ -242,6 +243,14 @@ power_button_handler(int signal, enum ev_type type, void *arg) inject_power_button_event(arg); } +static void +send_poweroff_event(void) +{ + struct vm_event event; + event.type = VM_EVENT_POWEROFF; + dm_send_vm_event(&event); +} + static int pm1_control_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, uint32_t *eax, void *arg) @@ -265,6 +274,7 @@ pm1_control_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, */ if (*eax & VIRTUAL_PM1A_SLP_EN) { if ((pm1_control & VIRTUAL_PM1A_SLP_TYP) >> 10 == 5) { + send_poweroff_event(); vm_suspend(ctx, VM_SUSPEND_POWEROFF); }