From: "Jörg Rödel" <joro@8bytes.org>
To: Paolo Bonzini <pbonzini@redhat.com>,
Sean Christopherson <seanjc@google.com>
Cc: 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, joro@8bytes.org
Subject: [PATCH v4 1/7] KVM: SEV: Document SNP direct VMSA userspace ABI
Date: Tue, 1 Dec 2026 10:45:19 +0100 [thread overview]
Message-ID: <20261201094525.1240736-2-joro@8bytes.org> (raw)
In-Reply-To: <20261201094525.1240736-1-joro@8bytes.org>
From: Joerg Roedel <joerg.roedel@amd.com>
Describe how to enable direct VMSAs and the guest APIC ID list, then
measure VMSA pages before launch finish and associate them with protected
vCPUs after launch finish succeeds.
Document the SNP vCPU state commands, their GPA and GHCB registration
rules, and how guest_memfd lookup and guest entry validate VMSA backing.
Codex assisted with comparing the ABI documentation against the preceding
patches and verified the updates with the KVM documentation build and
checkpatch.
Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
---
.../virt/kvm/x86/amd-memory-encryption.rst | 113 ++++++++++++++++--
1 file changed, 102 insertions(+), 11 deletions(-)
diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst
index eeb0812cb10a..a38f5694de44 100644
--- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst
+++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst
@@ -49,13 +49,14 @@ defined in the CPUID 0x8000001f[ecx] field.
The KVM_MEMORY_ENCRYPT_OP ioctl
===============================
-The main ioctl to access SEV is KVM_MEMORY_ENCRYPT_OP, which operates on
-the VM file descriptor. If the argument to KVM_MEMORY_ENCRYPT_OP is NULL,
-the ioctl returns 0 if SEV is enabled and ``ENOTTY`` if it is disabled
-(on some older versions of Linux, the ioctl tries to run normally even
-with a NULL argument, and therefore will likely return ``EFAULT`` instead
-of zero if SEV is enabled). If non-NULL, the argument to
-KVM_MEMORY_ENCRYPT_OP must be a struct kvm_sev_cmd::
+The main ioctl to access SEV is KVM_MEMORY_ENCRYPT_OP. Most SEV commands
+operate on the VM file descriptor. The SNP vCPU state commands operate on
+a vCPU file descriptor. If the argument to KVM_MEMORY_ENCRYPT_OP on the
+VM file descriptor is NULL, the ioctl returns 0 if SEV is enabled and
+``ENOTTY`` if it is disabled (on some older versions of Linux, the ioctl
+tries to run normally even with a NULL argument, and therefore will likely
+return ``EFAULT`` instead of zero if SEV is enabled). If non-NULL, the
+argument to KVM_MEMORY_ENCRYPT_OP must be a struct kvm_sev_cmd::
struct kvm_sev_cmd {
__u32 id;
@@ -70,8 +71,8 @@ another struct containing arguments specific to command. The ``sev_fd``
should point to a file descriptor that is opened on the ``/dev/sev``
device, if needed (see individual commands).
-On output, ``error`` is zero on success, or an error code. Error codes
-are defined in ``<linux/psp-dev.h>``.
+For commands that report firmware status, ``error`` is zero on success or
+contains an error code defined in ``<linux/psp-dev.h>``.
KVM implements the following commands to support common lifecycle events of SEV
guests, such as launching, running, snapshotting, migrating and decommissioning.
@@ -542,6 +543,7 @@ Returns: 0 on success, < 0 on error, -EAGAIN if caller should retry
where the allowed values for page_type are #define'd as::
KVM_SEV_SNP_PAGE_TYPE_NORMAL
+ KVM_SEV_SNP_PAGE_TYPE_VMSA
KVM_SEV_SNP_PAGE_TYPE_ZERO
KVM_SEV_SNP_PAGE_TYPE_UNMEASURED
KVM_SEV_SNP_PAGE_TYPE_SECRETS
@@ -550,12 +552,101 @@ where the allowed values for page_type are #define'd as::
See the SEV-SNP spec [snp-fw-abi]_ for further details on how each page type is
used/measured.
-20. KVM_SEV_SNP_LAUNCH_FINISH
+``KVM_SEV_SNP_PAGE_TYPE_VMSA`` creates VMSA pages as part of the measured
+initial image and requires ``KVM_CAP_SNP_DIRECT_VMSA`` to be enabled. Each
+request must contain exactly one 4 KiB VMSA page. KVM requires VMPL 0 and a
+``sev_features`` value that matches the VM's configured VMSA features.
+Userspace can issue multiple requests before ``KVM_SEV_SNP_LAUNCH_FINISH``.
+After launch finish succeeds, use ``KVM_SEV_SNP_SET_VCPU_STATE`` on the
+intended vCPU file descriptor to associate a measured VMSA with that vCPU.
+
+20. KVM_SEV_SNP_GET_VCPU_STATE / KVM_SEV_SNP_SET_VCPU_STATE
+------------------------------------------------------------
+
+Issue ``KVM_MEMORY_ENCRYPT_OP`` on the target vCPU file descriptor and set
+``kvm_sev_cmd.data`` to point to the state structure. Both commands are
+specific to SNP and require successful ``KVM_SEV_SNP_LAUNCH_FINISH``. The
+``sev_fd`` and ``error`` fields of ``kvm_sev_cmd`` are unused. Support for
+these commands is reported by ``KVM_CHECK_EXTENSION`` with
+``KVM_CAP_SNP_VCPU_STATE``.
+
+Parameters: struct kvm_sev_snp_vcpu_state (out for GET, in for SET)
+
+Returns: 0 on success, -negative on error
+
+::
+
+ #define KVM_SEV_SNP_VCPU_STATE_VMSA_VALID _BITULL(0)
+ #define KVM_SEV_SNP_VCPU_STATE_GHCB_VALID _BITULL(1)
+
+ struct kvm_sev_snp_vcpu_state {
+ __u64 valid_fields;
+ __u64 vmsa_gpa;
+ __u64 ghcb_gpa;
+ __u64 pad[5]; /* Must be zero */
+ };
+
+``KVM_SEV_SNP_GET_VCPU_STATE`` returns the tracked guest-owned VMSA GPA and
+the registered GHCB GPA, setting the corresponding bit in ``valid_fields``
+for each valid address. An absent address and the reserved fields are
+returned as zero. GET works in both direct and legacy VMSA modes. It reports
+the tracked VMSA GPA even while its host mapping is pending reload. SNP AP
+creation and destruction are reflected once KVM applies the request.
+
+``KVM_SEV_SNP_SET_VCPU_STATE`` sets addresses whose validity bits are set and
+clears the VMSA association or GHCB registration whose bit is clear. SET
+requires ``KVM_CAP_SNP_DIRECT_VMSA`` to be enabled and the target vCPU to
+have been protected by launch finish. Create the target vCPU before launch
+finish. Reserved fields must be zero, and ``valid_fields`` may contain only
+the defined bits.
+
+A VMSA GPA must fit in the vCPU's guest physical address space, be 4 KiB
+aligned, and not be 2 MiB aligned. SET resolves the GPA through a
+guest_memfd-backed memslot. The lookup can fault in a backing page and
+assign it as ordinary private memory in the Reverse Map Table (RMP). SET
+can return ``-EAGAIN`` if the backing is invalidated during the lookup.
+KVM re-resolves the tracked GPA before guest entry if the backing is later
+invalidated.
+
+To supply an initial VMSA, prepare the private page with
+``KVM_SEV_SNP_PAGE_TYPE_VMSA`` before launch finish. Firmware marks launch
+VMSAs in the RMP, and the guest can mark runtime VMSAs with ``RMPADJUST``.
+The processor checks that the page is marked as a VMSA at guest entry.
+A successful SET can be followed by a guest-entry failure if the page is
+still ordinary data.
+
+The GHCB GPA represents the GHCB registration. SET installs or clears the
+registration independently of the GHCB MSR, which carries reset and protocol
+values. For the GHCB field, SET checks only that the GPA is nonzero, is
+4 KiB aligned, and fits in the vCPU's guest physical address space.
+
+``KVM_CAP_SNP_DIRECT_VMSA`` is a VM-scoped capability that selects direct-VMSA
+mode. Userspace enables it with ``KVM_ENABLE_CAP`` on a ``KVM_X86_SNP_VM``
+before creating any vCPUs. ``flags`` and all elements of ``args`` must be
+zero. Enabling it also exposes the GHCB APIC ID List feature and enables
+the corresponding ``GET_AP_APIC_IDS`` event. The list contains the IDs
+passed to ``KVM_CREATE_VCPU``, including for vCPUs without an installed VMSA.
+
+KVM creates direct-mode vCPUs without a VMSA. Userspace supplies the initial
+VMSA pages and controls their contents, GPAs, and measurement order
+independently of the number of vCPUs created. Only launch updates contribute
+VMSAs to the measurement in this mode. A measured VMSA is part of the launch
+measurement regardless of whether it is later associated with a vCPU.
+
+21. KVM_SEV_SNP_LAUNCH_FINISH
-----------------------------
After completion of the SNP guest launch flow, the KVM_SEV_SNP_LAUNCH_FINISH
command can be issued to make the guest ready for execution.
+In direct-VMSA mode, launch finish validates all existing vCPUs before
+protecting them and enabling LBR virtualization. It requires unprotected
+vCPUs, with guest debugging disabled and only the fixed-one bit permitted
+in DR7. Before a direct-VMSA vCPU can enter the guest, it needs a VMSA
+installed by userspace with ``KVM_SEV_SNP_SET_VCPU_STATE`` after successful
+launch finish or by the guest through SNP AP creation. In legacy VMSA mode,
+launch finish generates and measures a KVM-owned VMSA for each existing vCPU.
+
Parameters (in): struct kvm_sev_snp_launch_finish
Returns: 0 on success, -negative on error
@@ -578,7 +669,7 @@ Returns: 0 on success, -negative on error
See SNP_LAUNCH_FINISH in the SEV-SNP specification [snp-fw-abi]_ for further
details on the input parameters in ``struct kvm_sev_snp_launch_finish``.
-21. KVM_SEV_SNP_ENABLE_REQ_CERTS
+22. KVM_SEV_SNP_ENABLE_REQ_CERTS
--------------------------------
The KVM_SEV_SNP_ENABLE_REQ_CERTS command will configure KVM to exit to
--
2.53.0
next prev parent reply other threads:[~2026-12-01 9:45 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 ` Jörg Rödel [this message]
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 10:00 ` sashiko-bot
2026-12-01 10:23 ` 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-01 9:45 ` [PATCH v4 7/7] KVM: selftests: Test the SNP APIC-ID-list GHCB request 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=20261201094525.1240736-2-joro@8bytes.org \
--to=joro@8bytes.org \
--cc=James.Bottomley@HansenPartnership.com \
--cc=coconut-svsm@lists.linux.dev \
--cc=joerg.roedel@amd.com \
--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