]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: selftests: Introduce SEV VM type check
authorPratik R. Sampat <prsampat@amd.com>
Wed, 5 Mar 2025 22:59:56 +0000 (16:59 -0600)
committerSean Christopherson <seanjc@google.com>
Fri, 2 May 2025 19:32:33 +0000 (12:32 -0700)
In preparation for SNP, declutter the vm type check by introducing a
SEV-SNP VM type check as well as a transitive set of helper functions.

The SNP VM type is the subset of SEV-ES. Similarly, the SEV-ES and SNP
types are subset of the SEV VM type check.

Signed-off-by: Pratik R. Sampat <prsampat@amd.com>
Link: https://lore.kernel.org/r/20250305230000.231025-7-prsampat@amd.com
[sean: make the helpers static inlines]
Signed-off-by: Sean Christopherson <seanjc@google.com>
tools/testing/selftests/kvm/include/x86/sev.h
tools/testing/selftests/kvm/lib/x86/processor.c
tools/testing/selftests/kvm/x86/sev_smoke_test.c

index 3003dc837fb708d8397a92894fcf4f8a15be956f..4a4e763f1b7b161935ed0c6e6f821594400879e5 100644 (file)
@@ -27,6 +27,21 @@ enum sev_guest_state {
 
 #define GHCB_MSR_TERM_REQ      0x100
 
+static inline bool is_sev_snp_vm(struct kvm_vm *vm)
+{
+       return vm->type == KVM_X86_SNP_VM;
+}
+
+static inline bool is_sev_es_vm(struct kvm_vm *vm)
+{
+       return is_sev_snp_vm(vm) || vm->type == KVM_X86_SEV_ES_VM;
+}
+
+static inline bool is_sev_vm(struct kvm_vm *vm)
+{
+       return is_sev_es_vm(vm) || vm->type == KVM_X86_SEV_VM;
+}
+
 void sev_vm_launch(struct kvm_vm *vm, uint32_t policy);
 void sev_vm_launch_measure(struct kvm_vm *vm, uint8_t *measurement);
 void sev_vm_launch_finish(struct kvm_vm *vm);
index bd5a802fa7a56dacac2e7bcd93863c95dde8a948..a92dc1dad085158f0f18ff1c149d68058124ee62 100644 (file)
@@ -639,7 +639,7 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm)
        sync_global_to_guest(vm, host_cpu_is_amd);
        sync_global_to_guest(vm, is_forced_emulation_enabled);
 
-       if (vm->type == KVM_X86_SEV_VM || vm->type == KVM_X86_SEV_ES_VM) {
+       if (is_sev_vm(vm)) {
                struct kvm_sev_init init = { 0 };
 
                vm_sev_ioctl(vm, KVM_SEV_INIT2, &init);
@@ -1156,7 +1156,7 @@ void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits)
 
 void kvm_init_vm_address_properties(struct kvm_vm *vm)
 {
-       if (vm->type == KVM_X86_SEV_VM || vm->type == KVM_X86_SEV_ES_VM) {
+       if (is_sev_vm(vm)) {
                vm->arch.sev_fd = open_sev_dev_path_or_exit();
                vm->arch.c_bit = BIT_ULL(this_cpu_property(X86_PROPERTY_SEV_C_BIT));
                vm->gpa_tag_mask = vm->arch.c_bit;
index b990034ea5a531c4133aa855d104b39c42ff68c1..b3de38272a37bf38d71d4e09241fd1d832f4161f 100644 (file)
@@ -124,7 +124,7 @@ static void test_sev(void *guest_code, uint64_t policy)
        for (;;) {
                vcpu_run(vcpu);
 
-               if (policy & SEV_POLICY_ES) {
+               if (is_sev_es_vm(vm)) {
                        TEST_ASSERT(vcpu->run->exit_reason == KVM_EXIT_SYSTEM_EVENT,
                                    "Wanted SYSTEM_EVENT, got %s",
                                    exit_reason_str(vcpu->run->exit_reason));