From dcb0d603d612effa3a293dc06a5535e2a46b7736 Mon Sep 17 00:00:00 2001 From: Alexander Bluhm Date: Tue, 5 Nov 2024 23:16:47 +0000 Subject: vmd(8) resets psp(4) Use shutdown and init to reset psp(4) on vmd(8) startup. This helps when hacking on vmd(8) and crashing it. The psp(4) reset cleans up all remnants of dead VMs from psp(4). Otherwise one would have to reboot the machine. from hshoexer@; OK mlarkin@ --- usr.sbin/vmd/psp.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++- usr.sbin/vmd/vmd.c | 9 +++----- usr.sbin/vmd/vmd.h | 3 ++- 3 files changed, 67 insertions(+), 8 deletions(-) (limited to 'usr.sbin') diff --git a/usr.sbin/vmd/psp.c b/usr.sbin/vmd/psp.c index 5705e1bb0e9..2de8cf4ac0b 100644 --- a/usr.sbin/vmd/psp.c +++ b/usr.sbin/vmd/psp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: psp.c,v 1.2 2024/09/26 01:45:13 jsg Exp $ */ +/* $OpenBSD: psp.c,v 1.3 2024/11/05 23:16:46 bluhm Exp $ */ /* * Copyright (c) 2023, 2024 Hans-Joerg Hoexer @@ -21,6 +21,8 @@ #include +#include +#include #include #include "vmd.h" @@ -267,3 +269,62 @@ psp_guest_shutdown(uint32_t handle) return (0); } + +/* + * Initialize PSP. + */ +static int +psp_init(void) +{ + if (ioctl(env->vmd_psp_fd, PSP_IOC_INIT) < 0) { + log_warn("%s: ioctl", __func__); + return (-1); + } + + return (0); +} + +/* + * Shutdown PSP. + */ +static int +psp_shutdown(void) +{ + if (ioctl(env->vmd_psp_fd, PSP_IOC_SHUTDOWN) < 0) { + log_warn("%s: ioctl", __func__); + return (-1); + } + + return (0); +} + +/* + * Reset PSP. + * + * Shut PSP down, then re-initialize it. This clears and resets + * all active contexts. + */ +static int +psp_reset(void) +{ + int ret; + + if ((ret = psp_shutdown()) < 0 || (ret = psp_init()) < 0) + return (ret); + + return (0); +} + +void +psp_setup(void) +{ + env->vmd_psp_fd = open(PSP_NODE, O_RDWR); + if (env->vmd_psp_fd == -1) { + if (errno != ENXIO) + log_debug("%s: failed to open %s", __func__, PSP_NODE); + return; + } + + if (psp_reset() < 0) + fatalx("%s: failed to reset PSP", __func__); +} diff --git a/usr.sbin/vmd/vmd.c b/usr.sbin/vmd/vmd.c index 91ac69e6c0d..5ea0ad48dc1 100644 --- a/usr.sbin/vmd/vmd.c +++ b/usr.sbin/vmd/vmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vmd.c,v 1.161 2024/09/26 01:45:13 jsg Exp $ */ +/* $OpenBSD: vmd.c,v 1.162 2024/11/05 23:16:46 bluhm Exp $ */ /* * Copyright (c) 2015 Reyk Floeter @@ -842,11 +842,8 @@ main(int argc, char **argv) if (!env->vmd_noaction) proc_connect(ps); - if (env->vmd_noaction == 0 && proc_id == PROC_PARENT) { - env->vmd_psp_fd = open(PSP_NODE, O_RDWR); - if (env->vmd_psp_fd == -1) - log_debug("%s: failed to open %s", __func__, PSP_NODE); - } + if (env->vmd_noaction == 0 && proc_id == PROC_PARENT) + psp_setup(); if (vmd_configure() == -1) fatalx("configuration failed"); diff --git a/usr.sbin/vmd/vmd.h b/usr.sbin/vmd/vmd.h index 8d53530198f..fee378b5d49 100644 --- a/usr.sbin/vmd/vmd.h +++ b/usr.sbin/vmd/vmd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: vmd.h,v 1.128 2024/09/11 15:42:52 bluhm Exp $ */ +/* $OpenBSD: vmd.h,v 1.129 2024/11/05 23:16:46 bluhm Exp $ */ /* * Copyright (c) 2015 Mike Larkin @@ -595,6 +595,7 @@ int psp_launch_measure(uint32_t); int psp_launch_finish(uint32_t); int psp_activate(uint32_t, uint32_t); int psp_guest_shutdown(uint32_t); +void psp_setup(void); /* sev.c */ int sev_init(struct vmd_vm *); -- cgit v1.2.3