test_vm_types() only exercised creation of the SEV/SEV-ES/SEV-SNP VM types that the platform actually supports, leaving the previously noted TODO ("check that unsupported types cannot be created") unaddressed. Add test_create_invalid_type() which issues a raw KVM_CREATE_VM for a given type and asserts it fails with -EINVAL, and invoke it for KVM_X86_SEV_ES_VM / KVM_X86_SNP_VM whenever the corresponding capability is not advertised. Because vm_create_barebones_type() asserts that KVM_CREATE_VM succeeds, the check is issued directly against the KVM fd so the negative path can be observed. While here, stop shadowing the file-scope kvm_fd with a local in main() so the new helper can use it. Signed-off-by: Hemanth Selam --- .../selftests/kvm/x86/sev_init2_tests.c | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kvm/x86/sev_init2_tests.c b/tools/testing/selftests/kvm/x86/sev_init2_tests.c index 8db88c355f16..724ff11f16c9 100644 --- a/tools/testing/selftests/kvm/x86/sev_init2_tests.c +++ b/tools/testing/selftests/kvm/x86/sev_init2_tests.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "test_util.h" #include "kvm_util.h" @@ -73,19 +74,34 @@ static void test_init2_invalid(unsigned long vm_type, struct kvm_sev_init *init, kvm_vm_free(vm); } +static void test_create_invalid_type(unsigned long vm_type, const char *msg) +{ + int fd = __kvm_ioctl(kvm_fd, KVM_CREATE_VM, (void *)vm_type); + + TEST_ASSERT(fd < 0 && errno == EINVAL, + "KVM_CREATE_VM should reject unsupported type (%s), got fd=%d errno=%d", + msg, fd, errno); + if (fd >= 0) + close(fd); +} + void test_vm_types(void) { test_init2(KVM_X86_SEV_VM, &(struct kvm_sev_init){}); /* - * TODO: check that unsupported types cannot be created. Probably - * a separate selftest. + * Types that aren't supported by the platform must be rejected by + * KVM_CREATE_VM itself, before any KVM_SEV_INIT2 can be attempted. */ if (have_sev_es) test_init2(KVM_X86_SEV_ES_VM, &(struct kvm_sev_init){}); + else + test_create_invalid_type(KVM_X86_SEV_ES_VM, "SEV-ES is unsupported"); if (have_snp) test_init2(KVM_X86_SNP_VM, &(struct kvm_sev_init){}); + else + test_create_invalid_type(KVM_X86_SNP_VM, "SEV-SNP is unsupported"); test_init2_invalid(0, &(struct kvm_sev_init){}, "VM type is KVM_X86_DEFAULT_VM"); @@ -121,9 +137,10 @@ void test_features(u32 vm_type, u64 supported_features) int main(int argc, char *argv[]) { - int kvm_fd = open_kvm_dev_path_or_exit(); bool have_sev; + kvm_fd = open_kvm_dev_path_or_exit(); + TEST_REQUIRE(__kvm_has_device_attr(kvm_fd, KVM_X86_GRP_SEV, KVM_X86_SEV_VMSA_FEATURES) == 0); kvm_device_attr_get(kvm_fd, KVM_X86_GRP_SEV, -- 2.43.7