1 // SPDX-License-Identifier: GPL-2.0-only 2 #include <linux/kvm.h> 3 #include <linux/psp-sev.h> 4 #include <stdio.h> 5 #include <sys/ioctl.h> 6 #include <stdlib.h> 7 #include <errno.h> 8 #include <pthread.h> 9 10 #include "test_util.h" 11 #include "kvm_util.h" 12 #include "processor.h" 13 #include "svm_util.h" 14 #include "kselftest.h" 15 16 #define SVM_SEV_FEAT_DEBUG_SWAP 32u 17 18 /* 19 * Some features may have hidden dependencies, or may only work 20 * for certain VM types. Err on the side of safety and don't 21 * expect that all supported features can be passed one by one 22 * to KVM_SEV_INIT2. 23 * 24 * (Well, right now there's only one...) 25 */ 26 #define KNOWN_FEATURES SVM_SEV_FEAT_DEBUG_SWAP 27 28 int kvm_fd; 29 u64 supported_vmsa_features; 30 bool have_sev_es; 31 bool have_snp; 32 33 static int __sev_ioctl(int vm_fd, int cmd_id, void *data) 34 { 35 struct kvm_sev_cmd cmd = { 36 .id = cmd_id, 37 .data = (u64)data, 38 .sev_fd = open_sev_dev_path_or_exit(), 39 }; 40 int ret; 41 42 ret = ioctl(vm_fd, KVM_MEMORY_ENCRYPT_OP, &cmd); 43 TEST_ASSERT(ret < 0 || cmd.error == SEV_RET_SUCCESS, 44 "%d failed: fw error: %d\n", 45 cmd_id, cmd.error); 46 47 return ret; 48 } 49 50 static void test_init2(unsigned long vm_type, struct kvm_sev_init *init) 51 { 52 struct kvm_vm *vm; 53 int ret; 54 55 vm = vm_create_barebones_type(vm_type); 56 ret = __sev_ioctl(vm->fd, KVM_SEV_INIT2, init); 57 TEST_ASSERT(ret == 0, 58 "KVM_SEV_INIT2 return code is %d (expected 0), errno: %d", 59 ret, errno); 60 kvm_vm_free(vm); 61 } 62 63 static void test_init2_invalid(unsigned long vm_type, struct kvm_sev_init *init, const char *msg) 64 { 65 struct kvm_vm *vm; 66 int ret; 67 68 vm = vm_create_barebones_type(vm_type); 69 ret = __sev_ioctl(vm->fd, KVM_SEV_INIT2, init); 70 TEST_ASSERT(ret == -1 && errno == EINVAL, 71 "KVM_SEV_INIT2 should fail, %s.", 72 msg); 73 kvm_vm_free(vm); 74 } 75 76 void test_vm_types(void) 77 { 78 test_init2(KVM_X86_SEV_VM, &(struct kvm_sev_init){}); 79 80 if (have_sev_es) 81 test_init2(KVM_X86_SEV_ES_VM, &(struct kvm_sev_init){}); 82 83 if (have_snp) 84 test_init2(KVM_X86_SNP_VM, &(struct kvm_sev_init){}); 85 86 test_init2_invalid(0, &(struct kvm_sev_init){}, 87 "VM type is KVM_X86_DEFAULT_VM"); 88 if (kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM)) 89 test_init2_invalid(KVM_X86_SW_PROTECTED_VM, &(struct kvm_sev_init){}, 90 "VM type is KVM_X86_SW_PROTECTED_VM"); 91 } 92 93 void test_flags(u32 vm_type) 94 { 95 int i; 96 97 for (i = 0; i < 32; i++) 98 test_init2_invalid(vm_type, 99 &(struct kvm_sev_init){ .flags = BIT(i) }, 100 "invalid flag"); 101 } 102 103 void test_features(u32 vm_type, u64 supported_features) 104 { 105 int i; 106 107 for (i = 0; i < 64; i++) { 108 if (!(supported_features & BIT_ULL(i))) 109 test_init2_invalid(vm_type, 110 &(struct kvm_sev_init){ .vmsa_features = BIT_ULL(i) }, 111 "unknown feature"); 112 else if (KNOWN_FEATURES & BIT_ULL(i)) 113 test_init2(vm_type, 114 &(struct kvm_sev_init){ .vmsa_features = BIT_ULL(i) }); 115 } 116 } 117 118 int main(int argc, char *argv[]) 119 { 120 int kvm_fd = open_kvm_dev_path_or_exit(); 121 bool have_sev; 122 123 TEST_REQUIRE(__kvm_has_device_attr(kvm_fd, KVM_X86_GRP_SEV, 124 KVM_X86_SEV_VMSA_FEATURES) == 0); 125 kvm_device_attr_get(kvm_fd, KVM_X86_GRP_SEV, 126 KVM_X86_SEV_VMSA_FEATURES, 127 &supported_vmsa_features); 128 129 /* 130 * Whether a VM type is available depends on KVM, not just CPUID: e.g. 131 * when all SEV ASIDs are assigned to SEV-SNP, KVM does not offer the 132 * SEV VM type even though X86_FEATURE_SEV is set. Derive availability 133 * from KVM_CAP_VM_TYPES and only assert the one-way implication that a 134 * type offered by KVM must also be reported in CPUID. 135 */ 136 have_sev = kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SEV_VM); 137 TEST_ASSERT(!have_sev || kvm_cpu_has(X86_FEATURE_SEV), 138 "sev: SEV_VM supported without SEV in CPUID"); 139 140 TEST_REQUIRE(have_sev); 141 have_sev_es = kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SEV_ES_VM); 142 143 TEST_ASSERT(!have_sev_es || kvm_cpu_has(X86_FEATURE_SEV_ES), 144 "sev-es: SEV_ES_VM supported without SEV_ES in CPUID"); 145 146 have_snp = kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SNP_VM); 147 TEST_ASSERT(!have_snp || kvm_cpu_has(X86_FEATURE_SEV_SNP), 148 "sev-snp: SNP_VM supported without SEV_SNP in CPUID"); 149 150 test_vm_types(); 151 152 test_flags(KVM_X86_SEV_VM); 153 if (have_sev_es) 154 test_flags(KVM_X86_SEV_ES_VM); 155 if (have_snp) 156 test_flags(KVM_X86_SNP_VM); 157 158 test_features(KVM_X86_SEV_VM, 0); 159 if (have_sev_es) 160 test_features(KVM_X86_SEV_ES_VM, supported_vmsa_features); 161 if (have_snp) 162 test_features(KVM_X86_SNP_VM, supported_vmsa_features); 163 164 return 0; 165 } 166