Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: selftests: SEV-SNP test for KVM_SEV_INIT2

Add the X86_FEATURE_SEV_SNP CPU feature to the architectural definition
for the SEV-SNP VM type to exercise the KVM_SEV_INIT2 call. Ensure that
the SNP test is skipped in scenarios where CPUID supports it but KVM
does not, preventing reporting of failure in such cases.

Reviewed-by: Nikunj A Dadhania <nikunj@amd.com>
Signed-off-by: Pratik R. Sampat <prsampat@amd.com>
Link: https://lore.kernel.org/r/20250305230000.231025-3-prsampat@amd.com
[sean: use the same pattern as SEV and SEV-ES]
Signed-off-by: Sean Christopherson <seanjc@google.com>

authored by

Pratik R. Sampat and committed by
Sean Christopherson
68ed692e 45eb2914

+14
+1
tools/testing/selftests/kvm/include/x86/processor.h
··· 203 203 #define X86_FEATURE_IDLE_HLT KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 30) 204 204 #define X86_FEATURE_SEV KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 1) 205 205 #define X86_FEATURE_SEV_ES KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 3) 206 + #define X86_FEATURE_SEV_SNP KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 4) 206 207 #define X86_FEATURE_PERFMON_V2 KVM_X86_CPU_FEATURE(0x80000022, 0, EAX, 0) 207 208 #define X86_FEATURE_LBR_PMC_FREEZE KVM_X86_CPU_FEATURE(0x80000022, 0, EAX, 2) 208 209
+13
tools/testing/selftests/kvm/x86/sev_init2_tests.c
··· 28 28 int kvm_fd; 29 29 u64 supported_vmsa_features; 30 30 bool have_sev_es; 31 + bool have_snp; 31 32 32 33 static int __sev_ioctl(int vm_fd, int cmd_id, void *data) 33 34 { ··· 83 82 */ 84 83 if (have_sev_es) 85 84 test_init2(KVM_X86_SEV_ES_VM, &(struct kvm_sev_init){}); 85 + 86 + if (have_snp) 87 + test_init2(KVM_X86_SNP_VM, &(struct kvm_sev_init){}); 86 88 87 89 test_init2_invalid(0, &(struct kvm_sev_init){}, 88 90 "VM type is KVM_X86_DEFAULT_VM"); ··· 142 138 "sev-es: KVM_CAP_VM_TYPES (%x) does not match cpuid (checking %x)", 143 139 kvm_check_cap(KVM_CAP_VM_TYPES), 1 << KVM_X86_SEV_ES_VM); 144 140 141 + have_snp = kvm_cpu_has(X86_FEATURE_SEV_SNP); 142 + TEST_ASSERT(have_snp == !!(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SNP_VM)), 143 + "sev-snp: KVM_CAP_VM_TYPES (%x) indicates SNP support (bit %d), but CPUID does not", 144 + kvm_check_cap(KVM_CAP_VM_TYPES), KVM_X86_SNP_VM); 145 + 145 146 test_vm_types(); 146 147 147 148 test_flags(KVM_X86_SEV_VM); 148 149 if (have_sev_es) 149 150 test_flags(KVM_X86_SEV_ES_VM); 151 + if (have_snp) 152 + test_flags(KVM_X86_SNP_VM); 150 153 151 154 test_features(KVM_X86_SEV_VM, 0); 152 155 if (have_sev_es) 153 156 test_features(KVM_X86_SEV_ES_VM, supported_vmsa_features); 157 + if (have_snp) 158 + test_features(KVM_X86_SNP_VM, supported_vmsa_features); 154 159 155 160 return 0; 156 161 }