Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / testing / selftests / kvm / x86_64 / sev_init2_tests.c
blob3fb967f40c6a1626d1d2cff1032c0e2562c8a205
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>
10 #include "test_util.h"
11 #include "kvm_util.h"
12 #include "processor.h"
13 #include "svm_util.h"
14 #include "kselftest.h"
16 #define SVM_SEV_FEAT_DEBUG_SWAP 32u
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.
24 * (Well, right now there's only one...)
26 #define KNOWN_FEATURES SVM_SEV_FEAT_DEBUG_SWAP
28 int kvm_fd;
29 u64 supported_vmsa_features;
30 bool have_sev_es;
32 static int __sev_ioctl(int vm_fd, int cmd_id, void *data)
34 struct kvm_sev_cmd cmd = {
35 .id = cmd_id,
36 .data = (uint64_t)data,
37 .sev_fd = open_sev_dev_path_or_exit(),
39 int ret;
41 ret = ioctl(vm_fd, KVM_MEMORY_ENCRYPT_OP, &cmd);
42 TEST_ASSERT(ret < 0 || cmd.error == SEV_RET_SUCCESS,
43 "%d failed: fw error: %d\n",
44 cmd_id, cmd.error);
46 return ret;
49 static void test_init2(unsigned long vm_type, struct kvm_sev_init *init)
51 struct kvm_vm *vm;
52 int ret;
54 vm = vm_create_barebones_type(vm_type);
55 ret = __sev_ioctl(vm->fd, KVM_SEV_INIT2, init);
56 TEST_ASSERT(ret == 0,
57 "KVM_SEV_INIT2 return code is %d (expected 0), errno: %d",
58 ret, errno);
59 kvm_vm_free(vm);
62 static void test_init2_invalid(unsigned long vm_type, struct kvm_sev_init *init, const char *msg)
64 struct kvm_vm *vm;
65 int ret;
67 vm = vm_create_barebones_type(vm_type);
68 ret = __sev_ioctl(vm->fd, KVM_SEV_INIT2, init);
69 TEST_ASSERT(ret == -1 && errno == EINVAL,
70 "KVM_SEV_INIT2 should fail, %s.",
71 msg);
72 kvm_vm_free(vm);
75 void test_vm_types(void)
77 test_init2(KVM_X86_SEV_VM, &(struct kvm_sev_init){});
80 * TODO: check that unsupported types cannot be created. Probably
81 * a separate selftest.
83 if (have_sev_es)
84 test_init2(KVM_X86_SEV_ES_VM, &(struct kvm_sev_init){});
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");
93 void test_flags(uint32_t vm_type)
95 int i;
97 for (i = 0; i < 32; i++)
98 test_init2_invalid(vm_type,
99 &(struct kvm_sev_init){ .flags = BIT(i) },
100 "invalid flag");
103 void test_features(uint32_t vm_type, uint64_t supported_features)
105 int i;
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) });
118 int main(int argc, char *argv[])
120 int kvm_fd = open_kvm_dev_path_or_exit();
121 bool have_sev;
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);
129 have_sev = kvm_cpu_has(X86_FEATURE_SEV);
130 TEST_ASSERT(have_sev == !!(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SEV_VM)),
131 "sev: KVM_CAP_VM_TYPES (%x) does not match cpuid (checking %x)",
132 kvm_check_cap(KVM_CAP_VM_TYPES), 1 << KVM_X86_SEV_VM);
134 TEST_REQUIRE(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SEV_VM));
135 have_sev_es = kvm_cpu_has(X86_FEATURE_SEV_ES);
137 TEST_ASSERT(have_sev_es == !!(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SEV_ES_VM)),
138 "sev-es: KVM_CAP_VM_TYPES (%x) does not match cpuid (checking %x)",
139 kvm_check_cap(KVM_CAP_VM_TYPES), 1 << KVM_X86_SEV_ES_VM);
141 test_vm_types();
143 test_flags(KVM_X86_SEV_VM);
144 if (have_sev_es)
145 test_flags(KVM_X86_SEV_ES_VM);
147 test_features(KVM_X86_SEV_VM, 0);
148 if (have_sev_es)
149 test_features(KVM_X86_SEV_ES_VM, supported_vmsa_features);
151 return 0;