Linux Documentation
 help / color / mirror / Atom feed
From: Liam Merwick <liam.merwick@oracle.com>
To: "Jörg Rödel" <joro@8bytes.org>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Sean Christopherson" <seanjc@google.com>
Cc: Michael Roth <michael.roth@amd.com>,
	Vishal Annapurve <vannapurve@google.com>,
	Ninad Naik <ninadnaik07@gmail.com>,
	Joerg Roedel <joerg.roedel@amd.com>,
	Tom Lendacky <thomas.lendacky@amd.com>,
	James Bottomley <James.Bottomley@HansenPartnership.com>,
	kvm@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	coconut-svsm@lists.linux.dev, liam.merwick@oracle.com
Subject: Re: [PATCH v4 6/7] KVM: selftests: Test SNP vCPU state and direct VMSA launch
Date: Wed, 2 Dec 2026 17:55:49 +0000	[thread overview]
Message-ID: <d3c06156-2eee-4151-b73b-08aa3f3940e4@oracle.com> (raw)
In-Reply-To: <20261201094525.1240736-7-joro@8bytes.org>

> From: Joerg Roedel <joerg.roedel@amd.com>
> 
> Test direct and legacy SNP launch. Validate VMSA launch updates and
> measure the initial VMSAs before launch finish. Check that GET and SET
> reject requests before successful finish, that a debug-enabled AP
> prevents partial protection, and that the BSP boots from the second
> VMSA after reassociation.
> 
> Round-trip and clear GHCB registration without changing the reset GHCB
> MSR. Reject invalid state requests and verify that SET accepts ordinary
> data backing while VMRUN rejects it. Exercise guest_memfd hole punching,
> memslot replacement and removal, and VMSA reload. Check that SVM reports
> a missing or invalid VMSA through KVM_EXIT_FAIL_ENTRY.
> 
> Codex assisted with test development, comparison with the documented
> ABI, compilation, and checkpatch. Runtime testing requires an SNP host
> with /dev/kvm.
> 
> Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>

Tested-by: Liam Merwick <liam.merwick@oracle.com>
Reviewed-by: Liam Merwick <liam.merwick@oracle.com>


> ---
>   tools/testing/selftests/kvm/Makefile.kvm      |   1 +
>   tools/testing/selftests/kvm/include/x86/sev.h | 101 ++++
>   tools/testing/selftests/kvm/lib/x86/sev.c     |  79 +++
>   .../kvm/x86/sev_snp_direct_vmsa_test.c        | 476 ++++++++++++++++++
>   4 files changed, 657 insertions(+)
>   create mode 100644 tools/testing/selftests/kvm/x86/sev_snp_direct_vmsa_test.c
> 
> diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
> index 283c59d7649c..2086b6e1ee25 100644
> --- a/tools/testing/selftests/kvm/Makefile.kvm
> +++ b/tools/testing/selftests/kvm/Makefile.kvm
> @@ -154,6 +154,7 @@ TEST_GEN_PROGS_x86 += x86/xen_vmcall_test
>   TEST_GEN_PROGS_x86 += x86/sev_dbg_test
>   TEST_GEN_PROGS_x86 += x86/sev_init2_tests
>   TEST_GEN_PROGS_x86 += x86/sev_migrate_tests
> +TEST_GEN_PROGS_x86 += x86/sev_snp_direct_vmsa_test
>   TEST_GEN_PROGS_x86 += x86/sev_smoke_test
>   TEST_GEN_PROGS_x86 += x86/amx_test
>   TEST_GEN_PROGS_x86 += x86/max_vcpuid_cap_test
> diff --git a/tools/testing/selftests/kvm/include/x86/sev.h b/tools/testing/selftests/kvm/include/x86/sev.h
> index dec383e59a47..1def4f9b246b 100644
> --- a/tools/testing/selftests/kvm/include/x86/sev.h
> +++ b/tools/testing/selftests/kvm/include/x86/sev.h
> @@ -31,6 +31,59 @@ enum sev_guest_state {
>   
>   #define GHCB_MSR_TERM_REQ	0x100
>   
> +#define SNP_ACTIVE_SEV_FEATURE	BIT_ULL(0)
> +#define VMSA_PMD_SIZE		BIT_ULL(21)
> +#define VMSA_MIN_GPA		(VMSA_PMD_SIZE + PAGE_SIZE)
> +
> +struct snp_test_vmsa {
> +	struct vmcb_seg es, cs, ss, ds, fs, gs;
> +	struct vmcb_seg gdtr, ldtr, idtr, tr;
> +	u64 pl0_ssp, pl1_ssp, pl2_ssp, pl3_ssp;
> +	u64 u_cet;
> +	u8 reserved_0xc8[2];
> +	u8 vmpl;
> +	u8 cpl;
> +	u8 reserved_0xcc[4];
> +	u64 efer;
> +	u8 reserved_0xd8[104];
> +	u64 xss;
> +	u64 cr4, cr3, cr0, dr7, dr6, rflags, rip;
> +	u64 dr0, dr1, dr2, dr3;
> +	u64 dr0_addr_mask, dr1_addr_mask, dr2_addr_mask, dr3_addr_mask;
> +	u8 reserved_0x1c0[24];
> +	u64 rsp, s_cet, ssp, isst_addr, rax;
> +	u64 star, lstar, cstar, sfmask, kernel_gs_base;
> +	u64 sysenter_cs, sysenter_esp, sysenter_eip, cr2;
> +	u8 reserved_0x248[32];
> +	u64 g_pat, dbgctl, br_from, br_to, last_excp_from, last_excp_to;
> +	u8 reserved_0x298[80];
> +	u32 pkru, tsc_aux;
> +	u64 tsc_scale, tsc_offset;
> +	u8 reserved_0x300[8];
> +	u64 rcx, rdx, rbx, reserved_0x320, rbp, rsi, rdi;
> +	u64 r8, r9, r10, r11, r12, r13, r14, r15;
> +	u8 reserved_0x380[16];
> +	u64 guest_exit_info_1, guest_exit_info_2, guest_exit_int_info, guest_nrip;
> +	u64 sev_features, vintr_ctrl, guest_exit_code, virtual_tom, tlb_id, pcpu_id;
> +	u64 event_inj, xcr0;
> +	u8 reserved_0x3f0[16];
> +	u64 x87_dp;
> +	u32 mxcsr;
> +	u16 x87_ftw, x87_fsw, x87_fcw, x87_fop, x87_ds, x87_cs;
> +	u64 x87_rip;
> +	u8 fpreg_x87[80];
> +	u8 fpreg_xmm[256];
> +	u8 fpreg_ymm[256];
> +} __packed;
> +
> +static_assert(offsetof(struct snp_test_vmsa, vmpl) == 0xca, "VMSA VMPL offset");
> +static_assert(offsetof(struct snp_test_vmsa, rip) == 0x178, "VMSA RIP offset");
> +static_assert(offsetof(struct snp_test_vmsa, sev_features) == 0x3b0, "VMSA SEV features offset");
> +static_assert(offsetof(struct snp_test_vmsa, xcr0) == 0x3e8, "VMSA XCR0 offset");
> +
> +void snp_prepare_vmsa(struct kvm_vcpu *vcpu, struct snp_test_vmsa *vmsa,
> +		      void *entry);
> +
>   static inline bool is_sev_snp_vm(struct kvm_vm *vm)
>   {
>   	return vm->type == KVM_X86_SNP_VM;
> @@ -100,6 +153,30 @@ static inline u64 snp_default_policy(void)
>   	__TEST_ASSERT_VM_VCPU_IOCTL(!ret, #cmd,	ret, vm);		\
>   })
>   
> +static inline int __vcpu_sev_ioctl(struct kvm_vcpu *vcpu, u32 cmd, void *arg)
> +{
> +	union {
> +		struct kvm_sev_cmd c;
> +		unsigned long raw;
> +	} sev_cmd = { .c = {
> +		.id = cmd,
> +		.data = (u64)arg,
> +		.sev_fd = vcpu->vm->arch.sev_fd,
> +	} };
> +	int ret;
> +
> +	ret = __vcpu_ioctl(vcpu, KVM_MEMORY_ENCRYPT_OP, &sev_cmd.raw);
> +	return ret ?: sev_cmd.c.error;
> +}
> +
> +#define vcpu_sev_ioctl(vcpu, cmd, arg)				\
> +({								\
> +	struct kvm_vcpu *__vcpu = (vcpu);				\
> +	int ret = __vcpu_sev_ioctl(__vcpu, cmd, arg);		\
> +								\
> +	__TEST_ASSERT_VM_VCPU_IOCTL(!ret, #cmd, ret, __vcpu->vm);	\
> +})
> +
>   void sev_vm_init(struct kvm_vm *vm);
>   void sev_es_vm_init(struct kvm_vm *vm);
>   void snp_vm_init(struct kvm_vm *vm);
> @@ -144,6 +221,30 @@ static inline void snp_launch_update_data(struct kvm_vm *vm, gpa_t gpa,
>   	vm_sev_ioctl(vm, KVM_SEV_SNP_LAUNCH_UPDATE, &update_data);
>   }
>   
> +static inline void snp_launch_update_vmsa(struct kvm_vm *vm, gpa_t gpa,
> +					  void *vmsa)
> +{
> +	vm_mem_set_private(vm, gpa, PAGE_SIZE);
> +	snp_launch_update_data(vm, gpa, (u64)vmsa, PAGE_SIZE,
> +			       KVM_SEV_SNP_PAGE_TYPE_VMSA);
> +}
> +
> +static inline void snp_get_vcpu_state(struct kvm_vcpu *vcpu,
> +				      struct kvm_sev_snp_vcpu_state *state)
> +{
> +	vcpu_sev_ioctl(vcpu, KVM_SEV_SNP_GET_VCPU_STATE, state);
> +}
> +
> +static inline void snp_set_vcpu_state(struct kvm_vcpu *vcpu, gpa_t vmsa_gpa)
> +{
> +	struct kvm_sev_snp_vcpu_state state = {
> +		.vmsa_gpa = vmsa_gpa,
> +		.valid_fields = KVM_SEV_SNP_VCPU_STATE_VMSA_VALID,
> +	};
> +
> +	vcpu_sev_ioctl(vcpu, KVM_SEV_SNP_SET_VCPU_STATE, &state);
> +}
> +
>   static inline void sev_dbg_crypt_memory(struct kvm_vm *vm, unsigned int cmd,
>   					void *dst, void *src, unsigned int len)
>   {
> diff --git a/tools/testing/selftests/kvm/lib/x86/sev.c b/tools/testing/selftests/kvm/lib/x86/sev.c
> index 93f916903461..b9fbed4cd7ea 100644
> --- a/tools/testing/selftests/kvm/lib/x86/sev.c
> +++ b/tools/testing/selftests/kvm/lib/x86/sev.c
> @@ -71,6 +71,85 @@ void sev_es_vm_init(struct kvm_vm *vm)
>   	}
>   }
>   
> +static void copy_segment(struct vmcb_seg *dst, const struct kvm_segment *src)
> +{
> +	dst->selector = src->selector;
> +	dst->base = src->base;
> +	dst->limit = src->limit;
> +	dst->attrib = src->type |
> +		(src->s << SVM_SELECTOR_S_SHIFT) |
> +		(src->dpl << SVM_SELECTOR_DPL_SHIFT) |
> +		((src->present && !src->unusable) << SVM_SELECTOR_P_SHIFT) |
> +		(src->avl << SVM_SELECTOR_AVL_SHIFT) |
> +		(src->l << SVM_SELECTOR_L_SHIFT) |
> +		(src->db << SVM_SELECTOR_DB_SHIFT) |
> +		(src->g << SVM_SELECTOR_G_SHIFT);
> +}
> +
> +static void copy_dtable(struct vmcb_seg *dst, const struct kvm_dtable *src)
> +{
> +	dst->base = src->base;
> +	dst->limit = src->limit;
> +}
> +
> +void snp_prepare_vmsa(struct kvm_vcpu *vcpu, struct snp_test_vmsa *vmsa,
> +		      void *entry)
> +{
> +	struct kvm_sregs sregs;
> +	struct kvm_regs regs;
> +
> +	memset(vmsa, 0, PAGE_SIZE);
> +	vcpu_sregs_get(vcpu, &sregs);
> +	vcpu_regs_get(vcpu, &regs);
> +
> +	copy_segment(&vmsa->es, &sregs.es);
> +	copy_segment(&vmsa->cs, &sregs.cs);
> +	copy_segment(&vmsa->ss, &sregs.ss);
> +	copy_segment(&vmsa->ds, &sregs.ds);
> +	copy_segment(&vmsa->fs, &sregs.fs);
> +	copy_segment(&vmsa->gs, &sregs.gs);
> +	copy_dtable(&vmsa->gdtr, &sregs.gdt);
> +	copy_segment(&vmsa->ldtr, &sregs.ldt);
> +	copy_dtable(&vmsa->idtr, &sregs.idt);
> +	copy_segment(&vmsa->tr, &sregs.tr);
> +
> +	vmsa->cpl = sregs.cs.dpl;
> +	/*
> +	 * KVM_GET_SREGS exposes the guest-visible EFER and therefore omits
> +	 * SVME, which KVM normally adds to the hardware VMSA itself.
> +	 */
> +	vmsa->efer = sregs.efer | EFER_SVME;
> +	vmsa->cr4 = sregs.cr4;
> +	vmsa->cr3 = sregs.cr3;
> +	vmsa->cr0 = sregs.cr0;
> +	vmsa->dr7 = 0x400;
> +	vmsa->dr6 = 0xffff0ff0;
> +	vmsa->rflags = regs.rflags;
> +	vmsa->rip = (u64)entry;
> +	vmsa->rsp = regs.rsp;
> +	vmsa->rax = regs.rax;
> +	vmsa->rcx = regs.rcx;
> +	vmsa->rdx = regs.rdx;
> +	vmsa->rbx = regs.rbx;
> +	vmsa->rbp = regs.rbp;
> +	vmsa->rsi = regs.rsi;
> +	vmsa->rdi = regs.rdi;
> +	vmsa->r8 = regs.r8;
> +	vmsa->r9 = regs.r9;
> +	vmsa->r10 = regs.r10;
> +	vmsa->r11 = regs.r11;
> +	vmsa->r12 = regs.r12;
> +	vmsa->r13 = regs.r13;
> +	vmsa->r14 = regs.r14;
> +	vmsa->r15 = regs.r15;
> +	vmsa->g_pat = 0x0007040600070406ULL;
> +	vmsa->sev_features = SNP_ACTIVE_SEV_FEATURE;
> +	/* The selftest compiler and string helpers can use SSE instructions. */
> +	vmsa->xcr0 = XFEATURE_MASK_FP | XFEATURE_MASK_SSE;
> +	vmsa->mxcsr = 0x1f80;
> +	vmsa->x87_fcw = 0x37f;
> +}
> +
>   void snp_vm_init(struct kvm_vm *vm)
>   {
>   	struct kvm_sev_init init = { 0 };
> diff --git a/tools/testing/selftests/kvm/x86/sev_snp_direct_vmsa_test.c b/tools/testing/selftests/kvm/x86/sev_snp_direct_vmsa_test.c
> new file mode 100644
> index 000000000000..be15b43f85cb
> --- /dev/null
> +++ b/tools/testing/selftests/kvm/x86/sev_snp_direct_vmsa_test.c
> @@ -0,0 +1,476 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +#include <errno.h>
> +#include <stdlib.h>
> +
> +#include "kvm_util.h"
> +#include "processor.h"
> +#include "sev.h"
> +#include "svm_util.h"
> +
> +#define DIRECT_MARKER	0x444952454354564dULL
> +#define LEGACY_MARKER	0x4c4547414359564dULL
> +#define GHCB_MSR_INFO_MASK	GENMASK_ULL(11, 0)
> +#define GHCB_MSR_SEV_INFO_RESP	0x001
> +
> +struct direct_results {
> +	u64 marker;
> +	u64 initial_ghcb_msr;
> +};
> +
> +static void guest_direct_entry(struct direct_results *results)
> +{
> +	results->initial_ghcb_msr = rdmsr(MSR_AMD64_SEV_ES_GHCB);
> +	results->marker = DIRECT_MARKER;
> +	wrmsr(MSR_AMD64_SEV_ES_GHCB, GHCB_MSR_TERM_REQ);
> +	vmgexit();
> +}
> +
> +static void guest_legacy_entry(u64 *marker)
> +{
> +	*marker = LEGACY_MARKER;
> +	wrmsr(MSR_AMD64_SEV_ES_GHCB, GHCB_MSR_TERM_REQ);
> +	vmgexit();
> +}
> +
> +static void expect_launch_update_vmsa_error(struct kvm_vm *vm, gpa_t gpa,
> +					    void *vmsa, u64 size)
> +{
> +	struct kvm_sev_snp_launch_update update = {
> +		.gfn_start = gpa >> PAGE_SHIFT,
> +		.uaddr = (u64)vmsa,
> +		.len = size,
> +		.type = KVM_SEV_SNP_PAGE_TYPE_VMSA,
> +	};
> +
> +	errno = 0;
> +	TEST_ASSERT_EQ(__vm_sev_ioctl(vm, KVM_SEV_SNP_LAUNCH_UPDATE, &update), -1);
> +	TEST_ASSERT_EQ(errno, EINVAL);
> +}
> +
> +static void exclude_from_normal_launch(struct kvm_vm *vm, gpa_t gpa,
> +				       unsigned int npages)
> +{
> +	struct userspace_mem_region *region;
> +
> +	region = memslot2region(vm, vm->memslots[MEM_REGION_TEST_DATA]);
> +	sparsebit_clear_num(region->protected_phy_pages, gpa >> PAGE_SHIFT, npages);
> +}
> +
> +static void assert_vcpu_terminated(struct kvm_vcpu *vcpu)
> +{
> +	vcpu_run(vcpu);
> +	TEST_ASSERT_EQ(vcpu->run->exit_reason, KVM_EXIT_SYSTEM_EVENT);
> +	TEST_ASSERT_EQ(vcpu->run->system_event.type, KVM_SYSTEM_EVENT_SEV_TERM);
> +}
> +
> +static void assert_vcpu_entry_failed(struct kvm_vcpu *vcpu)
> +{
> +	vcpu_run(vcpu);
> +	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_FAIL_ENTRY);
> +	TEST_ASSERT_EQ(vcpu->run->fail_entry.hardware_entry_failure_reason,
> +		       SVM_EXIT_ERR);
> +}
> +
> +static struct kvm_vm *create_direct_vmsa_vm(unsigned int nr_vcpus,
> +					    void *guest_code,
> +					    struct kvm_vcpu **vcpus)
> +{
> +	struct vm_shape shape = {
> +		.mode = VM_MODE_DEFAULT,
> +		.type = KVM_X86_SNP_VM,
> +	};
> +	struct kvm_vm *vm;
> +	unsigned int i;
> +
> +	vm = __vm_create(shape, nr_vcpus, 0);
> +	vm_enable_cap(vm, KVM_CAP_SNP_DIRECT_VMSA, 0);
> +	for (i = 0; i < nr_vcpus; i++)
> +		vcpus[i] = vm_vcpu_add(vm, i, guest_code);
> +	kvm_arch_vm_finalize_vcpus(vm);
> +
> +	return vm;
> +}
> +
> +static void expect_set_vcpu_state_error(struct kvm_vcpu *vcpu,
> +					struct kvm_sev_snp_vcpu_state *state,
> +					int expected_errno)
> +{
> +	errno = 0;
> +	TEST_ASSERT_EQ(__vcpu_sev_ioctl(vcpu, KVM_SEV_SNP_SET_VCPU_STATE, state), -1);
> +	TEST_ASSERT_EQ(errno, expected_errno);
> +}
> +
> +static void expect_vcpu_state_unavailable(struct kvm_vcpu *vcpu,
> +					  struct kvm_sev_snp_vcpu_state *state,
> +					  int expected_errno)
> +{
> +	errno = 0;
> +	TEST_ASSERT_EQ(__vcpu_sev_ioctl(vcpu, KVM_SEV_SNP_GET_VCPU_STATE, state), -1);
> +	TEST_ASSERT_EQ(errno, expected_errno);
> +	expect_set_vcpu_state_error(vcpu, state, expected_errno);
> +}
> +
> +static void test_direct_vmsa(void)
> +{
> +	struct snp_test_vmsa *vmsas, *selected_vmsa;
> +	struct kvm_sev_snp_vcpu_state state = {};
> +	struct kvm_mp_state mp_state = {
> +		.mp_state = KVM_MP_STATE_UNINITIALIZED,
> +	};
> +	struct kvm_guest_debug debug = {
> +		.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP,
> +	};
> +	struct kvm_sev_snp_launch_finish finish = {};
> +	struct kvm_vcpu *vcpus[2];
> +	struct kvm_vcpu *vcpu, *ap;
> +	struct kvm_vm *vm;
> +	struct direct_results *results;
> +	gva_t marker_gva;
> +	gpa_t vmsa_gpa;
> +
> +	vm = create_direct_vmsa_vm(ARRAY_SIZE(vcpus), guest_legacy_entry, vcpus);
> +	vcpu = vcpus[0];
> +	ap = vcpus[1];
> +	/* Restore normal AP state after vm_vcpu_add() makes it runnable. */
> +	vcpu_mp_state_set(ap, &mp_state);
> +	marker_gva = vm_alloc_shared(vm, PAGE_SIZE, KVM_UTIL_MIN_VADDR,
> +				     MEM_REGION_TEST_DATA);
> +	results = addr_gva2hva(vm, marker_gva);
> +	vcpu_args_set(vcpu, 1, marker_gva);
> +
> +	vmsa_gpa = ____vm_phy_pages_alloc(vm, 2, VMSA_MIN_GPA,
> +					vm->memslots[MEM_REGION_TEST_DATA], true, false);
> +	TEST_ASSERT(vmsa_gpa & (VMSA_PMD_SIZE - 1), "unsafe VMSA GPA");
> +	vmsas = aligned_alloc(PAGE_SIZE, 2 * PAGE_SIZE);
> +	TEST_ASSERT(vmsas, "Failed to allocate VMSA source pages");
> +	selected_vmsa = (void *)vmsas + PAGE_SIZE;
> +	snp_prepare_vmsa(vcpu, &vmsas[0], guest_legacy_entry);
> +	snp_prepare_vmsa(vcpu, selected_vmsa, guest_direct_entry);
> +
> +	snp_vm_launch_start(vm, snp_default_policy());
> +	vm_mem_set_private(vm, vmsa_gpa, 2 * PAGE_SIZE);
> +	expect_launch_update_vmsa_error(vm, vmsa_gpa, vmsas, 2 * PAGE_SIZE);
> +
> +	vmsas[0].vmpl = 1;
> +	expect_launch_update_vmsa_error(vm, vmsa_gpa, vmsas, PAGE_SIZE);
> +	vmsas[0].vmpl = 0;
> +	vmsas[0].sev_features = 0;
> +	expect_launch_update_vmsa_error(vm, vmsa_gpa, vmsas, PAGE_SIZE);
> +	vmsas[0].sev_features = SNP_ACTIVE_SEV_FEATURE;
> +
> +	snp_launch_update_vmsa(vm, vmsa_gpa, vmsas);
> +	snp_launch_update_vmsa(vm, vmsa_gpa + PAGE_SIZE, selected_vmsa);
> +	exclude_from_normal_launch(vm, vmsa_gpa, 2);
> +	snp_vm_launch_update(vm);
> +
> +	state.valid_fields = KVM_SEV_SNP_VCPU_STATE_VMSA_VALID;
> +	state.vmsa_gpa = vmsa_gpa;
> +	expect_vcpu_state_unavailable(vcpu, &state, EINVAL);
> +	memset(results, 0, sizeof(*results));
> +	assert_vcpu_entry_failed(vcpu);
> +	TEST_ASSERT_EQ(results->marker, 0);
> +
> +	/* A debug-enabled AP must not cause partial protection of the BSP. */
> +	vcpu_ioctl(ap, KVM_SET_GUEST_DEBUG, &debug);
> +	TEST_ASSERT_EQ(__vm_sev_ioctl(vm, KVM_SEV_SNP_LAUNCH_FINISH, &finish), -1);
> +	TEST_ASSERT_EQ(errno, EINVAL);
> +	debug.control = 0;
> +	vcpu_ioctl(ap, KVM_SET_GUEST_DEBUG, &debug);
> +	snp_vm_launch_finish(vm);
> +
> +	/* Rebind to the second VMSA and register a GHCB in the same request. */
> +	snp_set_vcpu_state(vcpu, vmsa_gpa);
> +	snp_get_vcpu_state(vcpu, &state);
> +	TEST_ASSERT_EQ(state.valid_fields, KVM_SEV_SNP_VCPU_STATE_VMSA_VALID);
> +	TEST_ASSERT_EQ(state.vmsa_gpa, vmsa_gpa);
> +	TEST_ASSERT_EQ(state.ghcb_gpa, 0);
> +
> +	state.valid_fields |= KVM_SEV_SNP_VCPU_STATE_GHCB_VALID;
> +	state.vmsa_gpa = vmsa_gpa + PAGE_SIZE;
> +	state.ghcb_gpa = PAGE_SIZE;
> +	vcpu_sev_ioctl(vcpu, KVM_SEV_SNP_SET_VCPU_STATE, &state);
> +	snp_get_vcpu_state(vcpu, &state);
> +	TEST_ASSERT_EQ(state.valid_fields, KVM_SEV_SNP_VCPU_STATE_VMSA_VALID |
> +		       KVM_SEV_SNP_VCPU_STATE_GHCB_VALID);
> +	TEST_ASSERT_EQ(state.vmsa_gpa, vmsa_gpa + PAGE_SIZE);
> +	TEST_ASSERT_EQ(state.ghcb_gpa, PAGE_SIZE);
> +
> +	vcpu_mp_state_get(ap, &mp_state);
> +	TEST_ASSERT_EQ(mp_state.mp_state, KVM_MP_STATE_UNINITIALIZED);
> +	assert_vcpu_terminated(vcpu);
> +	TEST_ASSERT_EQ(results->marker, DIRECT_MARKER);
> +	TEST_ASSERT_EQ(results->initial_ghcb_msr & GHCB_MSR_INFO_MASK,
> +		       GHCB_MSR_SEV_INFO_RESP);
> +
> +	state.valid_fields = KVM_SEV_SNP_VCPU_STATE_GHCB_VALID;
> +	vcpu_sev_ioctl(vcpu, KVM_SEV_SNP_SET_VCPU_STATE, &state);
> +	snp_get_vcpu_state(vcpu, &state);
> +	TEST_ASSERT_EQ(state.valid_fields, KVM_SEV_SNP_VCPU_STATE_GHCB_VALID);
> +	TEST_ASSERT_EQ(state.vmsa_gpa, 0);
> +	TEST_ASSERT_EQ(state.ghcb_gpa, PAGE_SIZE);
> +
> +	free(vmsas);
> +	kvm_vm_free(vm);
> +}
> +
> +static void test_ghcb_registration(void)
> +{
> +	struct kvm_sev_snp_vcpu_state state = {};
> +	struct kvm_vcpu *vcpu;
> +	struct kvm_vm *vm;
> +
> +	vm = create_direct_vmsa_vm(1, guest_legacy_entry, &vcpu);
> +	snp_vm_launch_start(vm, snp_default_policy());
> +	snp_vm_launch_update(vm);
> +	snp_vm_launch_finish(vm);
> +	snp_get_vcpu_state(vcpu, &state);
> +	TEST_ASSERT_EQ(state.valid_fields, 0);
> +
> +	state.valid_fields = KVM_SEV_SNP_VCPU_STATE_GHCB_VALID;
> +	state.ghcb_gpa = PAGE_SIZE;
> +	vcpu_sev_ioctl(vcpu, KVM_SEV_SNP_SET_VCPU_STATE, &state);
> +	memset(&state, 0, sizeof(state));
> +	snp_get_vcpu_state(vcpu, &state);
> +	TEST_ASSERT_EQ(state.valid_fields, KVM_SEV_SNP_VCPU_STATE_GHCB_VALID);
> +	TEST_ASSERT_EQ(state.ghcb_gpa, PAGE_SIZE);
> +
> +	state.ghcb_gpa = 0;
> +	expect_set_vcpu_state_error(vcpu, &state, EINVAL);
> +	state.ghcb_gpa = GHCB_MSR_TERM_REQ;
> +	expect_set_vcpu_state_error(vcpu, &state, EINVAL);
> +	state.ghcb_gpa = BIT_ULL(63);
> +	expect_set_vcpu_state_error(vcpu, &state, EINVAL);
> +	snp_get_vcpu_state(vcpu, &state);
> +	TEST_ASSERT_EQ(state.valid_fields, KVM_SEV_SNP_VCPU_STATE_GHCB_VALID);
> +	TEST_ASSERT_EQ(state.ghcb_gpa, PAGE_SIZE);
> +
> +	state.valid_fields = 0;
> +	vcpu_sev_ioctl(vcpu, KVM_SEV_SNP_SET_VCPU_STATE, &state);
> +	snp_get_vcpu_state(vcpu, &state);
> +	TEST_ASSERT_EQ(state.valid_fields, 0);
> +	TEST_ASSERT_EQ(state.ghcb_gpa, 0);
> +	kvm_vm_free(vm);
> +}
> +
> +static void test_failed_launch_finish(void)
> +{
> +	struct kvm_sev_snp_vcpu_state state = {
> +		.valid_fields = KVM_SEV_SNP_VCPU_STATE_GHCB_VALID,
> +		.ghcb_gpa = PAGE_SIZE,
> +	};
> +	struct kvm_sev_snp_launch_finish finish = {
> +		.id_block_en = 1,
> +		.id_block_uaddr = -1ULL,
> +	};
> +	struct kvm_vcpu *vcpu;
> +	struct kvm_vm *vm;
> +
> +	vm = create_direct_vmsa_vm(1, guest_legacy_entry, &vcpu);
> +	snp_vm_launch_start(vm, snp_default_policy());
> +	snp_vm_launch_update(vm);
> +
> +	/* Bad ID data fails after the vCPU has already been protected. */
> +	errno = 0;
> +	TEST_ASSERT_EQ(__vm_sev_ioctl(vm, KVM_SEV_SNP_LAUNCH_FINISH, &finish), -1);
> +	TEST_ASSERT_EQ(errno, EFAULT);
> +	expect_vcpu_state_unavailable(vcpu, &state, EINVAL);
> +	assert_vcpu_entry_failed(vcpu);
> +	kvm_vm_free(vm);
> +}
> +
> +static void test_vmsa_memslot_replacement(void)
> +{
> +	const gpa_t gpa = BIT_ULL(32) + PAGE_SIZE;
> +	const u32 slot = 10;
> +	struct kvm_sev_snp_vcpu_state state;
> +	struct snp_test_vmsa *vmsa;
> +	struct kvm_vcpu *vcpu;
> +	struct kvm_vm *vm;
> +
> +	vm = create_direct_vmsa_vm(1, guest_legacy_entry, &vcpu);
> +	vmsa = aligned_alloc(PAGE_SIZE, PAGE_SIZE);
> +	TEST_ASSERT(vmsa, "Failed to allocate a VMSA source page");
> +	snp_prepare_vmsa(vcpu, vmsa, guest_legacy_entry);
> +	snp_vm_launch_start(vm, snp_default_policy());
> +
> +	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, gpa, slot, 1,
> +				    KVM_MEM_GUEST_MEMFD);
> +	snp_launch_update_vmsa(vm, gpa, vmsa);
> +	snp_vm_launch_update(vm);
> +	snp_vm_launch_finish(vm);
> +	snp_set_vcpu_state(vcpu, gpa);
> +
> +	/* SET can fault in a new page after hole punching. */
> +	vm_guest_mem_punch_hole(vm, gpa, PAGE_SIZE);
> +	snp_set_vcpu_state(vcpu, gpa);
> +	assert_vcpu_entry_failed(vcpu);
> +	snp_get_vcpu_state(vcpu, &state);
> +	TEST_ASSERT_EQ(state.valid_fields, KVM_SEV_SNP_VCPU_STATE_VMSA_VALID);
> +	TEST_ASSERT_EQ(state.vmsa_gpa, gpa);
> +
> +	vm_mem_region_delete(vm, slot);
> +	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, gpa, slot, 1,
> +				    KVM_MEM_GUEST_MEMFD);
> +	vm_mem_set_private(vm, gpa, PAGE_SIZE);
> +	/* GET still reports the association while reload is pending. */
> +	snp_get_vcpu_state(vcpu, &state);
> +	TEST_ASSERT_EQ(state.valid_fields, KVM_SEV_SNP_VCPU_STATE_VMSA_VALID);
> +	TEST_ASSERT_EQ(state.vmsa_gpa, gpa);
> +
> +	/* The replacement page is ordinary data and fails guest entry. */
> +	assert_vcpu_entry_failed(vcpu);
> +	snp_get_vcpu_state(vcpu, &state);
> +	TEST_ASSERT_EQ(state.valid_fields, KVM_SEV_SNP_VCPU_STATE_VMSA_VALID);
> +	TEST_ASSERT_EQ(state.vmsa_gpa, gpa);
> +
> +	vm_mem_region_delete(vm, slot);
> +	expect_set_vcpu_state_error(vcpu, &state, EINVAL);
> +	/* With no slot to resolve, reload clears the tracked GPA. */
> +	assert_vcpu_entry_failed(vcpu);
> +	snp_get_vcpu_state(vcpu, &state);
> +	TEST_ASSERT_EQ(state.valid_fields, 0);
> +	TEST_ASSERT_EQ(state.vmsa_gpa, 0);
> +
> +	free(vmsa);
> +	kvm_vm_free(vm);
> +}
> +
> +static void test_invalid_requests(void)
> +{
> +	const gpa_t shared_gpa = BIT_ULL(32) + PAGE_SIZE;
> +	struct kvm_sev_snp_vcpu_state state = {
> +		.valid_fields = KVM_SEV_SNP_VCPU_STATE_VMSA_VALID,
> +	};
> +	struct kvm_sev_snp_launch_update update = {
> +		.len = PAGE_SIZE,
> +		.type = KVM_SEV_SNP_PAGE_TYPE_ZERO,
> +	};
> +	struct kvm_vcpu *vcpu;
> +	struct kvm_vm *vm;
> +	gpa_t private_gpa;
> +
> +	vm = vm_create_with_one_vcpu(&vcpu, guest_legacy_entry);
> +	expect_vcpu_state_unavailable(vcpu, &state, ENOTTY);
> +	kvm_vm_free(vm);
> +
> +	vm = create_direct_vmsa_vm(1, guest_legacy_entry, &vcpu);
> +	snp_vm_launch_start(vm, snp_default_policy());
> +	private_gpa = ____vm_phy_pages_alloc(vm, 1, VMSA_MIN_GPA,
> +					    vm->memslots[MEM_REGION_TEST_DATA], true, false);
> +	TEST_ASSERT(private_gpa & (VMSA_PMD_SIZE - 1), "unsafe VMSA GPA");
> +	vm_mem_set_private(vm, private_gpa, PAGE_SIZE);
> +	update.gfn_start = private_gpa >> PAGE_SHIFT;
> +	vm_sev_ioctl(vm, KVM_SEV_SNP_LAUNCH_UPDATE, &update);
> +	exclude_from_normal_launch(vm, private_gpa, 1);
> +	snp_vm_launch_update(vm);
> +	snp_vm_launch_finish(vm);
> +
> +	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, shared_gpa, 10, 1, 0);
> +
> +	state.vmsa_gpa = private_gpa;
> +	state.pad[4] = 1;
> +	expect_set_vcpu_state_error(vcpu, &state, EINVAL);
> +	state.pad[4] = 0;
> +	state.valid_fields |= BIT_ULL(2);
> +	expect_set_vcpu_state_error(vcpu, &state, EINVAL);
> +	state.valid_fields &= ~BIT_ULL(2);
> +	state.vmsa_gpa = private_gpa + 1;
> +	expect_set_vcpu_state_error(vcpu, &state, EINVAL);
> +	state.vmsa_gpa = VMSA_PMD_SIZE;
> +	expect_set_vcpu_state_error(vcpu, &state, EINVAL);
> +	state.vmsa_gpa = BIT_ULL(63) + PAGE_SIZE;
> +	expect_set_vcpu_state_error(vcpu, &state, EINVAL);
> +
> +	/* Failed VMSA lookup must leave the GHCB registration unchanged. */
> +	state.vmsa_gpa = shared_gpa;
> +	state.ghcb_gpa = PAGE_SIZE;
> +	state.valid_fields |= KVM_SEV_SNP_VCPU_STATE_GHCB_VALID;
> +	expect_set_vcpu_state_error(vcpu, &state, EFAULT);
> +
> +	snp_get_vcpu_state(vcpu, &state);
> +	TEST_ASSERT_EQ(state.valid_fields, 0);
> +	TEST_ASSERT_EQ(state.ghcb_gpa, 0);
> +
> +	/* SET accepts existing data backing.  VMRUN checks the RMP VMSA bit. */
> +	snp_set_vcpu_state(vcpu, private_gpa);
> +	snp_get_vcpu_state(vcpu, &state);
> +	TEST_ASSERT_EQ(state.valid_fields, KVM_SEV_SNP_VCPU_STATE_VMSA_VALID);
> +	TEST_ASSERT_EQ(state.vmsa_gpa, private_gpa);
> +	assert_vcpu_entry_failed(vcpu);
> +
> +	kvm_vm_free(vm);
> +}
> +
> +static void test_direct_vmsa_capability(void)
> +{
> +	struct kvm_enable_cap cap = {
> +		.cap = KVM_CAP_SNP_DIRECT_VMSA,
> +	};
> +	struct vm_shape shape = {
> +		.mode = VM_MODE_DEFAULT,
> +		.type = KVM_X86_SNP_VM,
> +	};
> +	struct kvm_vm *vm;
> +
> +	vm = vm_create_barebones();
> +	TEST_ASSERT_EQ(__vm_enable_cap(vm, KVM_CAP_SNP_DIRECT_VMSA, 0), -1);
> +	TEST_ASSERT_EQ(errno, EINVAL);
> +	kvm_vm_free(vm);
> +
> +	vm = __vm_create(shape, 1, 0);
> +	TEST_ASSERT_EQ(__vm_enable_cap(vm, KVM_CAP_SNP_DIRECT_VMSA, 1), -1);
> +	TEST_ASSERT_EQ(errno, EINVAL);
> +	cap.args[3] = 1;
> +	TEST_ASSERT_EQ(__vm_ioctl(vm, KVM_ENABLE_CAP, &cap), -1);
> +	TEST_ASSERT_EQ(errno, EINVAL);
> +	cap.args[3] = 0;
> +	cap.flags = 1;
> +	TEST_ASSERT_EQ(__vm_ioctl(vm, KVM_ENABLE_CAP, &cap), -1);
> +	TEST_ASSERT_EQ(errno, EINVAL);
> +	vm_enable_cap(vm, KVM_CAP_SNP_DIRECT_VMSA, 0);
> +	vm_vcpu_add(vm, 0, guest_legacy_entry);
> +	TEST_ASSERT_EQ(__vm_enable_cap(vm, KVM_CAP_SNP_DIRECT_VMSA, 0), -1);
> +	TEST_ASSERT_EQ(errno, EINVAL);
> +	kvm_vm_free(vm);
> +}
> +
> +static void test_legacy_launch(void)
> +{
> +	struct kvm_sev_snp_vcpu_state state;
> +	struct kvm_vcpu *vcpu;
> +	struct kvm_vm *vm;
> +	gva_t marker_gva;
> +	u64 *marker;
> +
> +	vm = vm_sev_create_with_one_vcpu(KVM_X86_SNP_VM, guest_legacy_entry, &vcpu);
> +	marker_gva = vm_alloc_shared(vm, PAGE_SIZE, KVM_UTIL_MIN_VADDR,
> +				     MEM_REGION_TEST_DATA);
> +	marker = addr_gva2hva(vm, marker_gva);
> +	vcpu_args_set(vcpu, 1, marker_gva);
> +	vm_sev_launch(vm, snp_default_policy(), NULL);
> +	snp_get_vcpu_state(vcpu, &state);
> +	TEST_ASSERT_EQ(state.valid_fields, 0);
> +	TEST_ASSERT_EQ(state.vmsa_gpa, 0);
> +	expect_set_vcpu_state_error(vcpu, &state, EINVAL);
> +
> +	*marker = 0;
> +	assert_vcpu_terminated(vcpu);
> +	TEST_ASSERT_EQ(*marker, LEGACY_MARKER);
> +	kvm_vm_free(vm);
> +}
> +
> +int main(int argc, char *argv[])
> +{
> +	TEST_REQUIRE(kvm_check_cap(KVM_CAP_SNP_VCPU_STATE));
> +	TEST_REQUIRE(kvm_check_cap(KVM_CAP_SNP_DIRECT_VMSA));
> +	TEST_ASSERT(sizeof(struct snp_test_vmsa) <= PAGE_SIZE, "VMSA structure is too large");
> +
> +	test_direct_vmsa_capability();
> +	test_ghcb_registration();
> +	test_failed_launch_finish();
> +	test_vmsa_memslot_replacement();
> +	test_invalid_requests();
> +	test_direct_vmsa();
> +	test_legacy_launch();
> +	return 0;
> +}
> -- 
> 2.53.0
> 

  reply	other threads:[~2026-12-02 17:56 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  9:45 [PATCH v4 0/7] KVM: SVM: Support direct setting of VMSA for SEV-SNP guests Jörg Rödel
2026-12-01  9:45 ` [PATCH v4 1/7] KVM: SEV: Document SNP direct VMSA userspace ABI Jörg Rödel
2026-12-01  9:45 ` [PATCH v4 2/7] KVM: SEV: Add direct VMSA capability Jörg Rödel
2026-12-01  9:45 ` [PATCH v4 3/7] KVM: SVM: Implement GET_AP_APIC_IDS NAE event Jörg Rödel
2026-12-01  9:45 ` [PATCH v4 4/7] KVM: SEV: Allow VMSA pages in SNP launch updates Jörg Rödel
2026-12-01  9:45 ` [PATCH v4 5/7] KVM: SEV: Add SNP vCPU state get and set commands Jörg Rödel
2026-12-01  9:45 ` [PATCH v4 6/7] KVM: selftests: Test SNP vCPU state and direct VMSA launch Jörg Rödel
2026-12-02 17:55   ` Liam Merwick [this message]
2026-12-01  9:45 ` [PATCH v4 7/7] KVM: selftests: Test the SNP APIC-ID-list GHCB request Jörg Rödel
2026-12-02 17:57   ` Liam Merwick

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d3c06156-2eee-4151-b73b-08aa3f3940e4@oracle.com \
    --to=liam.merwick@oracle.com \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=coconut-svsm@lists.linux.dev \
    --cc=joerg.roedel@amd.com \
    --cc=joro@8bytes.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=michael.roth@amd.com \
    --cc=ninadnaik07@gmail.com \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=thomas.lendacky@amd.com \
    --cc=vannapurve@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox