From: Tony Lindgren <tony.lindgren@linux.intel.com>
To: "Jörg Rödel" <joro@8bytes.org>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Sean Christopherson <seanjc@google.com>,
Michael Roth <michael.roth@amd.com>,
Liam Merwick <liam.merwick@oracle.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
Subject: Re: [PATCH v2 6/8] KVM: SEV: Add SNP vCPU state get and set commands
Date: Tue, 15 Sep 2026 14:30:36 +0300 [thread overview]
Message-ID: <aqksXLLcmFDkM9Xj@tlindgre-MOBL1> (raw)
In-Reply-To: <20260908103338.427254-7-joro@8bytes.org>
On Tue, Sep 08, 2026 at 12:33:36PM +0200, Jörg Rödel wrote:
> From: Joerg Roedel <joerg.roedel@amd.com>
> --- a/arch/x86/kvm/svm/sev.c
> +++ b/arch/x86/kvm/svm/sev.c
...
> +static int snp_get_vcpu_state(struct kvm_vcpu *vcpu,
> + struct kvm_sev_cmd *argp)
> +{
> + struct vcpu_svm *svm = to_svm(vcpu);
> + struct kvm *kvm = vcpu->kvm;
> + struct kvm_sev_snp_vcpu_state state = {};
> +
> + if (!is_sev_snp_guest(vcpu))
> + return -ENOTTY;
> + if (!to_kvm_sev_info(kvm)->snp_context)
> + return -EINVAL;
> +
> + guard(mutex)(&svm->sev_es.snp_vmsa_mutex);
> +
> + if (VALID_PAGE(svm->sev_es.snp_guest_vmsa_gpa) &&
> + VALID_PAGE(svm->vmcb->control.vmsa_pa)) {
> + state.vmsa_gpa = svm->sev_es.snp_guest_vmsa_gpa;
> + state.valid_fields |= KVM_SEV_SNP_VCPU_STATE_VMSA_VALID;
> + }
> +
> + if (VALID_PAGE(svm->vmcb->control.ghcb_gpa)) {
> + state.ghcb_gpa = svm->vmcb->control.ghcb_gpa;
> + state.valid_fields |= KVM_SEV_SNP_VCPU_STATE_GHCB_VALID;
> + }
> +
> + if (copy_to_user(u64_to_user_ptr(argp->data), &state, sizeof(state)))
> + return -EFAULT;
> +
> + return 0;
> +}
> +
> +static int snp_set_vcpu_state(struct kvm_vcpu *vcpu,
> + struct kvm_sev_cmd *argp)
> +{
> + struct vcpu_svm *svm = to_svm(vcpu);
> + struct kvm *kvm = vcpu->kvm;
> + struct kvm_sev_info *sev = to_kvm_sev_info(kvm);
> + struct kvm_sev_snp_vcpu_state state;
> + int ret;
> +
> + if (!is_sev_snp_guest(vcpu))
> + return -ENOTTY;
> + if (!sev->snp_direct_vmsa)
> + return -EINVAL;
> + if (!sev->snp_context || kvm->arch.pre_fault_allowed)
> + return -EINVAL;
> +
> + if (copy_from_user(&state, u64_to_user_ptr(argp->data), sizeof(state)))
> + return -EFAULT;
> +
> + if (memchr_inv(state.pad, 0, sizeof(state.pad)) ||
> + state.valid_fields & ~(KVM_SEV_SNP_VCPU_STATE_VMSA_VALID |
> + KVM_SEV_SNP_VCPU_STATE_GHCB_VALID))
> + return -EINVAL;
> +
> + if (state.valid_fields & KVM_SEV_SNP_VCPU_STATE_VMSA_VALID) {
> + if (!PAGE_ALIGNED(state.vmsa_gpa) ||
> + !page_address_valid(vcpu, state.vmsa_gpa) ||
> + IS_ALIGNED(state.vmsa_gpa, PMD_SIZE))
> + return -EINVAL;
> + }
> +
> + guard(mutex)(&svm->sev_es.snp_vmsa_mutex);
> +
> + if (state.valid_fields & KVM_SEV_SNP_VCPU_STATE_VMSA_VALID) {
> + ret = sev_snp_install_guest_vmsa(svm, state.vmsa_gpa);
> + if (ret)
> + return ret;
> + } else {
> + svm->sev_es.snp_has_guest_vmsa = true;
> + svm->sev_es.snp_guest_vmsa_gpa = INVALID_PAGE;
> + svm->vmcb->control.vmsa_pa = INVALID_PAGE;
> + }
> +
> + if (state.valid_fields & KVM_SEV_SNP_VCPU_STATE_GHCB_VALID)
> + svm->vmcb->control.ghcb_gpa = state.ghcb_gpa;
> + else
> + svm->vmcb->control.ghcb_gpa = INVALID_PAGE;
> +
> + vmcb_mark_all_dirty(svm->vmcb);
> + return 0;
> +}
> +
Nice, looks like snp_get/set_vcpu_state() could quite easily make use
of KVM_EXPORT/IMPORT_VCPU?
next prev parent reply other threads:[~2026-09-15 11:30 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-08 10:33 [PATCH v2 0/8] KVM: SVM: Support direct setting of VMSA for SEV-SNP guests Jörg Rödel
2026-09-08 10:33 ` [PATCH v2 1/8] KVM: SEV: Document SNP direct VMSA userspace ABI Jörg Rödel
2026-09-08 10:33 ` [PATCH v2 2/8] KVM: SVM: Implement GET_AP_APIC_IDS NAE event Jörg Rödel
2026-09-08 10:33 ` [PATCH v2 3/8] KVM: SVM: Hold SRCU while reloading guest-owned VMSAs Jörg Rödel
2026-09-08 10:33 ` [PATCH v2 4/8] KVM: SEV: Add direct VMSA capability Jörg Rödel
2026-09-08 10:33 ` [PATCH v2 5/8] KVM: SEV: Allow VMSA pages in SNP launch updates Jörg Rödel
2026-09-08 10:33 ` [PATCH v2 6/8] KVM: SEV: Add SNP vCPU state get and set commands Jörg Rödel
2026-09-15 11:30 ` Tony Lindgren [this message]
2026-09-08 10:33 ` [PATCH v2 7/8] KVM: selftests: Test the SNP APIC-ID-list GHCB request Jörg Rödel
2026-09-08 10:33 ` [PATCH v2 8/8] KVM: selftests: Test SNP vCPU state and direct VMSA launch Jörg Rödel
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=aqksXLLcmFDkM9Xj@tlindgre-MOBL1 \
--to=tony.lindgren@linux.intel.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=liam.merwick@oracle.com \
--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
all inboxes | Powered by JetHome®