Path: blob/master/tools/testing/selftests/kvm/x86/sev_init2_tests.c
38245 views
// SPDX-License-Identifier: GPL-2.0-only1#include <linux/kvm.h>2#include <linux/psp-sev.h>3#include <stdio.h>4#include <sys/ioctl.h>5#include <stdlib.h>6#include <errno.h>7#include <pthread.h>89#include "test_util.h"10#include "kvm_util.h"11#include "processor.h"12#include "svm_util.h"13#include "kselftest.h"1415#define SVM_SEV_FEAT_DEBUG_SWAP 32u1617/*18* Some features may have hidden dependencies, or may only work19* for certain VM types. Err on the side of safety and don't20* expect that all supported features can be passed one by one21* to KVM_SEV_INIT2.22*23* (Well, right now there's only one...)24*/25#define KNOWN_FEATURES SVM_SEV_FEAT_DEBUG_SWAP2627int kvm_fd;28u64 supported_vmsa_features;29bool have_sev_es;30bool have_snp;3132static int __sev_ioctl(int vm_fd, int cmd_id, void *data)33{34struct kvm_sev_cmd cmd = {35.id = cmd_id,36.data = (uint64_t)data,37.sev_fd = open_sev_dev_path_or_exit(),38};39int ret;4041ret = ioctl(vm_fd, KVM_MEMORY_ENCRYPT_OP, &cmd);42TEST_ASSERT(ret < 0 || cmd.error == SEV_RET_SUCCESS,43"%d failed: fw error: %d\n",44cmd_id, cmd.error);4546return ret;47}4849static void test_init2(unsigned long vm_type, struct kvm_sev_init *init)50{51struct kvm_vm *vm;52int ret;5354vm = vm_create_barebones_type(vm_type);55ret = __sev_ioctl(vm->fd, KVM_SEV_INIT2, init);56TEST_ASSERT(ret == 0,57"KVM_SEV_INIT2 return code is %d (expected 0), errno: %d",58ret, errno);59kvm_vm_free(vm);60}6162static void test_init2_invalid(unsigned long vm_type, struct kvm_sev_init *init, const char *msg)63{64struct kvm_vm *vm;65int ret;6667vm = vm_create_barebones_type(vm_type);68ret = __sev_ioctl(vm->fd, KVM_SEV_INIT2, init);69TEST_ASSERT(ret == -1 && errno == EINVAL,70"KVM_SEV_INIT2 should fail, %s.",71msg);72kvm_vm_free(vm);73}7475void test_vm_types(void)76{77test_init2(KVM_X86_SEV_VM, &(struct kvm_sev_init){});7879/*80* TODO: check that unsupported types cannot be created. Probably81* a separate selftest.82*/83if (have_sev_es)84test_init2(KVM_X86_SEV_ES_VM, &(struct kvm_sev_init){});8586if (have_snp)87test_init2(KVM_X86_SNP_VM, &(struct kvm_sev_init){});8889test_init2_invalid(0, &(struct kvm_sev_init){},90"VM type is KVM_X86_DEFAULT_VM");91if (kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM))92test_init2_invalid(KVM_X86_SW_PROTECTED_VM, &(struct kvm_sev_init){},93"VM type is KVM_X86_SW_PROTECTED_VM");94}9596void test_flags(uint32_t vm_type)97{98int i;99100for (i = 0; i < 32; i++)101test_init2_invalid(vm_type,102&(struct kvm_sev_init){ .flags = BIT(i) },103"invalid flag");104}105106void test_features(uint32_t vm_type, uint64_t supported_features)107{108int i;109110for (i = 0; i < 64; i++) {111if (!(supported_features & BIT_ULL(i)))112test_init2_invalid(vm_type,113&(struct kvm_sev_init){ .vmsa_features = BIT_ULL(i) },114"unknown feature");115else if (KNOWN_FEATURES & BIT_ULL(i))116test_init2(vm_type,117&(struct kvm_sev_init){ .vmsa_features = BIT_ULL(i) });118}119}120121int main(int argc, char *argv[])122{123int kvm_fd = open_kvm_dev_path_or_exit();124bool have_sev;125126TEST_REQUIRE(__kvm_has_device_attr(kvm_fd, KVM_X86_GRP_SEV,127KVM_X86_SEV_VMSA_FEATURES) == 0);128kvm_device_attr_get(kvm_fd, KVM_X86_GRP_SEV,129KVM_X86_SEV_VMSA_FEATURES,130&supported_vmsa_features);131132have_sev = kvm_cpu_has(X86_FEATURE_SEV);133TEST_ASSERT(have_sev == !!(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SEV_VM)),134"sev: KVM_CAP_VM_TYPES (%x) does not match cpuid (checking %x)",135kvm_check_cap(KVM_CAP_VM_TYPES), 1 << KVM_X86_SEV_VM);136137TEST_REQUIRE(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SEV_VM));138have_sev_es = kvm_cpu_has(X86_FEATURE_SEV_ES);139140TEST_ASSERT(have_sev_es == !!(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SEV_ES_VM)),141"sev-es: KVM_CAP_VM_TYPES (%x) does not match cpuid (checking %x)",142kvm_check_cap(KVM_CAP_VM_TYPES), 1 << KVM_X86_SEV_ES_VM);143144have_snp = kvm_cpu_has(X86_FEATURE_SEV_SNP);145TEST_ASSERT(have_snp == !!(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SNP_VM)),146"sev-snp: KVM_CAP_VM_TYPES (%x) indicates SNP support (bit %d), but CPUID does not",147kvm_check_cap(KVM_CAP_VM_TYPES), KVM_X86_SNP_VM);148149test_vm_types();150151test_flags(KVM_X86_SEV_VM);152if (have_sev_es)153test_flags(KVM_X86_SEV_ES_VM);154if (have_snp)155test_flags(KVM_X86_SNP_VM);156157test_features(KVM_X86_SEV_VM, 0);158if (have_sev_es)159test_features(KVM_X86_SEV_ES_VM, supported_vmsa_features);160if (have_snp)161test_features(KVM_X86_SNP_VM, supported_vmsa_features);162163return 0;164}165166167