When a VM is destroyed (bhyvectl --destroy) while a vCPU thread is
handling an exit in bhyve, every call on the VM fails. vm_loop()
handles that for vm_run() and ends bhyve with BHYVE_EXIT_ERROR, but a
vCPU in the middle of an exit hits one of the assert(error == 0) after
a register update or an exception injection (emulate_inout(),
vmexit_rdmsr(), vm_inject_fault()), or reports "Failed to emulate
instruction" and returns VMEXIT_ABORT: bhyve dies of SIGABRT and
leaves a core. With a guest doing a lot of port I/O or MSR accesses
that bhyve emulates, a forced stop hits this regularly.
Add vm_check_destroyed(), which exits with BHYVE_EXIT_ERROR, as
vm_loop() does, if the VM is gone. Call it before those asserts and
on the instruction emulation failure paths on amd64, arm64 and
riscv. A failure on a VM that still exists asserts or aborts as
before.
Signed-off-by: Wanpeng Qian <wanpengqian@gmail.com>
Sponsored by: keelos.dev