From: Joerg Roedel 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 --- .../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 ````. +For commands that report firmware status, ``error`` is zero on success or +contains an error code defined in ````. 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