[PATCH 7/7] accel/kvm: Exit gracefully when KVM is not supported

Philippe Mathieu-Daudé
2021-02-19T11:46:46Z

Now that we added the 'kvm_supported' field to MachineClass
and all our machines able to use KVM have this field set,
we can check it in kvm_init() and exit gracefully with
a friendly error message.

Before:

$ qemu-system-aarch64 -M raspi3b -enable-kvm
qemu-system-aarch64: /build/qemu-ETIdrs/qemu-4.2/exec.c:865: cpu_address_space_init: Assertion `asidx == 0 || !kvm_enabled()' failed.
Aborted

$ qemu-system-aarch64 -M xlnx-zcu102 -enable-kvm -smp 6
qemu-system-aarch64: kvm_init_vcpu: kvm_arch_init_vcpu failed (0): Invalid argument

After:

$ qemu-system-aarch64 -M raspi3b -enable-kvm
Machine 'raspi3b' does not support KVM

$ qemu-system-aarch64 -M xlnx-zcu102 -enable-kvm -smp 6
Machine 'xlnx-zcu102' does not support KVM

Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
accel/kvm/kvm-all.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
index b069938d881..8a8d3f64248 100644
--- a/accel/kvm/kvm-all.c
+++ b/accel/kvm/kvm-all.c
@@ -2001,6 +2001,12 @@ static int kvm_init(MachineState *ms)

s = KVM_STATE(ms->accelerator);

+ if (!mc->kvm_supported) {
+ ret = -EINVAL;
+ fprintf(stderr, "Machine '%s' does not support KVM\n", mc->name);
+ exit(1);
+ }
+
/*
* On systems where the kernel can support different base page
* sizes, host page size may be different from TARGET_PAGE_SIZE,
--
2.26.2