Add AMD Secure Nested Paging (SEV-SNP) Hypervisor Support
Subsystems (12)
v9 cover Add AMD Secure Nested Paging (SEV-SNP) Hypervisor Support
This patchset is also available at:
https://github.com/amdese/linux/commits/snp-host-v9-rfc
and is based on top of the following tree:
https://github.com/mdroth/linux/commits/kvm_gmem_solo_fixes
which in turn is based on Sean Christopherson's UPM base support tree, with a couple fixes/workarounds needed for SEV/SNP support. [1]
== OVERVIEW ==
This patchset implements SEV-SNP hypervisor support for linux.
This version is being posted as an RFC due to fairly extensive changes relating to transitioning the SEV-SNP implementation to using guest_memfd (gmem, aka Unmapped Private Memory) to manage private guest pages instead of the legacy SEV memory registration ioctls.
For this purpose we've added a number of hooks on top of gmem to plumb in necessary RMP table updates corresponding when mapping private memory into a guest's nested page table, and then restoring it to shared/hypervisor-owned state we free'ing gmem-allocated memory back to the host. Our hope is that some of these hooks can be re-used for other platforms as well, but have tried to make them as minimal as possible if they prove to be SNP-specific. For quicker review of this aspect, they are at the beginning of the series, directly on top of the gmem patchset.
Outside of UPM-related items, we've also included fairly extensive changes based on review feedback from v8 and would appreciate any feedback on those aspects as well.
== LAYOUT ==
PATCH 01-05: Pre-patches that add generic gmem and KVM MMU hooks to handle
plumbing gmem memory into CoCo guests, and make arch/x86/coco re-usability for common SEV host code instead of only guest code..
PATCH 06-22: Host SNP initialization code and CCP driver prep for handling
SNP cmds
PATCH 13-22: general SNP detection/enablement for host and CCP driver PATCH 23-46: core KVM support for running SEV-SNP guests
PATCH 47-51: misc handling for IOMMU support, guest request handling, and
debug infrastructure
== TESTING (note updated QEMU command-lines) ==
For testing this via QEMU, use the following tree:
https://github.com/amdese/qemu/commits/snp-wip-gmem
SEV-SNP with gmem/UPM enabled:
# set discard=none to disable discarding memory post-conversion, faster
# boot times, but increased memory usage
qemu-system-x86_64 -cpu EPYC-Milan-v2 \
-object memory-backend-memfd-private,id=ram1,size=1G,share=true \
-object sev-snp-guest,id=sev0,cbitpos=51,reduced-phys-bits=1,discard=both \
-machine q35,confidential-guest-support=sev0,memory-backend=ram1,kvm-type=protected \
...KVM selftests for UPM:
cd $kernel_src_dir
make -C tools/testing/selftests TARGETS="kvm" EXTRA_CFLAGS="-DDEBUG -I<path to kernel headers>" sudo tools/testing/selftests/kvm/x86_64/private_mem_conversions_test
== BACKGROUND (SEV-SNP) ==
This part of the Secure Encrypted Paging (SEV-SNP) series focuses on the changes required in a host OS for SEV-SNP support. The series builds upon SEV-SNP Guest Support now part of mainline.
This series provides the basic building blocks to support booting the SEV-SNP VMs, it does not cover all the security enhancement introduced by the SEV-SNP such as interrupt protection.
The CCP driver is enhanced to provide new APIs that use the SEV-SNP specific commands defined in the SEV-SNP firmware specification. The KVM driver uses those APIs to create and managed the SEV-SNP guests.
The GHCB specification version 2 introduces new set of NAE's that is used by the SEV-SNP guest to communicate with the hypervisor. The series provides support to handle the following new NAE events:
- Register GHCB GPA
- Page State Change Request
- Hypevisor feature
- Guest message request
When pages are marked as guest-owned in the RMP table, they are assigned to a specific guest/ASID, as well as a specific GFN with in the guest. Any attempts to map it in the RMP table to a different guest/ASID, or a different GFN within a guest/ASID, will result in an RMP nested page fault.
Prior to accessing a guest-owned page, the guest must validate it with a special PVALIDATE instruction which will set a special bit in the RMP table for the guest. This is the only way to set the validated bit outisde of the initial pre-encrypted guest payload/image; any attempts outside the guest to modify the RMP entry from that point forward will result in the validated bit being cleared, at which point the guest will trigger an exception if it attempts to access that page so it can be made aware of possible tampering.
One exception to this is the initial guest payload, which is pre-validated by the firmware prior to launching. The guest can use Guest Message requests to fetch an attestation report which will include the measurement of the initial image so that the guest can verify it was booted with the expected image/environment.
After boot, guests can use Page State Change requests to switch pages between shared/hypervisor-owned and private/guest-owned to share data for things like DMA, virtio buffers, and other GHCB requests.
In this implementation SEV-SNP, private guest memory is managed by a new kernel framework called guest_memfd (gmem). With gmem, a new KVM_SET_MEMORY_ATTRIBUTES KVM ioctl has been added to tell the KVM MMU whether a particular GFN should be backed by shared (normal) memory or private (gmem-allocated) memory. To tie into this, Page State Change requests are forward to userspace via KVM_EXIT_VMGEXIT exits, which will then issue the corresponding KVM_SET_MEMORY_ATTRIBUTES call to set the private/shared state in the KVM MMU.
The gmem / KVM MMU hooks added in this series will then update the RMP table entries for the backing PFNs to set them to guest-owned/private when mapping private pages into the guest via KVM MMU, or use the normal KVM MMU handling in the case of shared pages where the corresponding RMP table entries are left in the default shared/hypervisor-owned state.
== TODO / KNOWN ISSUES ==
* Add a per-arch CONFIG option for enabling platform-specific handling when invalidating gmem pages and free'ing the back to host, as opposed to the current approach which defaults to issuing invalidations to a weak-referenced stub implementation for non-x86 builds. Hoping for more feedback on general implementation first.
* This should incorporate all review feedback from v8, but if anything slipped through the cracks please let me know.
[1] https://lore.kernel.org/lkml/[email protected]/
Changes since v8:
* Rework gmem/UPM hooks based on Sean's latest gmem/UPM tree
* Move SEV lazy-pinning support out to a separate series which uses this series as a prereq instead of the other way around.
* Re-organize extended guest request patches into 3 patches encompassing SEV FD ioctls for host-wide certs, KVM ioctls for per-instance certs, and the guest request handling that consumes them. Also move them to the top of the series to better separate them for the core SNP patches (Alexey, Zhi, Ashish, Dov, Dionna, others)
* Various other changes/fixups for extended guests request handling (Dov, Alexey, Dionna)
* Use helper to calculate max RMP entry size and improve readability (Dave)
* Use architecture-independent GPA value for initial VMSA pages
* Ensure SEV_CMD_SNP_GUEST_REQUEST failures are indicated to guest (Alex)
* Allocate per-instance certs on-demand (Alex)
* comment fixup for RMP fault handling (Zhi)
* commit msg rewording for MSR-based PSCs (Zhi)
* update SNP command/struct definitions based on 1.54 ABI (Saban)
* use sev_deactivate_lock around SEV_CMD_SNP_DECOMMISSION (Saban)
* Various comment/commit fixups (Zhi, Alex, Kim, Vlastimil, Dave,
* kexec fixes for newer SNP firmwares (Ashish)
* Various other fixups and re-ordering of patches.
Changes since v7:
* Rebase to Sean's updated UPM base support tree
* Drop KVM_CAP_UNMAPPED_MEMORY and .private_mem_enabled x86 op in favor of kvm_arch_has_private_mem() and vm_type KVM_VM_CREATE arg
* Drop GHCB map/unmap refactoring and post map/unmap hooks as they are no longer needed with UPM
* Move .fault_is_private implementation to SNP patch range, no longer needed for SEV.
* Don't call attribute update / invalidation hooks under kvm->mmu_lock (Tom, Jarkko)
* Revert switch to using set_memory_p()/set_memory_np() in rmpupdate() due to it causing performance regression
* Commit fixups for 'fault_is_private'/'update_mem_attr' hooks, have 'fault_is_private' return bool (Boris)
* Split kvm_vm_set_region_attr() into separate patch. (Jarkko)
* Copy corrected CPUID page to userspace when firmware rejects it (Tom, Jarkko)
* Fix sev_dump_rmpentry() error-handling (Alper)
* Use adjusted cmd_buf pointer rather than sev->cmd_buf directly (Alper)
* Correct typo in SNP_GET_EXT_CONFIG documentation (Dov)
* Update struct kvm_sev_snp_launch_finish definition in amd-memory-encryption.rst (Tom)
* Fix snp_launch_update_vmsa replacing created_vcpus with online_vcpus
* Fix SNP_DBG_DECRYPT to not include len parameter.
* Fix SNP_LAUNCH_FINISH to copy host-data from userspace
Changes since v6:
* Added support for restrictedmem/UPM, and removed SEV-specific implementation of private memory management. As a result of this rework the following patches were no longer needed so were dropped:
- KVM: SVM: Mark the private vma unmergable for SEV-SNP guests
- KVM: SVM: Disallow registering memory range from HugeTLB for SNP guest
- KVM: x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by TDX and SNP
- KVM: x86: Introduce kvm_mmu_get_tdp_walk() for SEV-SNP use
* Moved RMP table entry structure definition (struct rmpentry) to sev.c, to not expose this non-architectural definition to rest of the kernel and making the structure private to SNP code. Also made RMP table entry accessors to be inline functions and removed all accessors which are not called more than once. Added a new function rmptable_entry() to index into the RMP table and return RMP table entry.
* Moved RMPUPDATE, PSMASH helper function declerations to x86 arch specific include namespace from linux namespace. Added comments for these helper functions.
* Introduce set_memory_p() to provide a way to change atributes of a memory range to be marked as present and added to the kernel directmap, and invalidating/restoring pages from directmap are now done using set_memory_np() and set_memory_p().
* Added detailed comments around user RMP #PF fault handling and simplified computation of the faulting pfn for large-pages.
* Added support to return pfn from dump_pagetable() to do SEV-specific fault handling, this is added a pre-patch. This support is now used to dump RMP entry in case of RMP #PF in show_fault_oops().
* Added a new generic SNP command params structure sev_data_snp_addr, which is used for all SNP firmware API commands requiring a single physical address parameter.
* Added support for new SNP_INIT_EX command with support for HV-Fixed page range list.
* Added support for new SNP_SHUTDOWN_EX command which allows disabling enforcement of SNP in the IOMMU. Also DF_FLUSH is done at SNP shutdown if it indicates DF_FLUSH is required.
* Make sev_do_cmd() a generic API interface for the hypervisor to issue commands to manage an SEV and SNP guest. Also removed the API wrappers used by the hypervisor to manage an SEV-SNP guest. All these APIs now invoke sev_do_cmd() directly.
* Introduce snp leaked pages list. If pages are unsafe to be released back to the page-allocator as they can't be reclaimed or transitioned back to hypervisor/shared state are now added
to this internal leaked pages list to prevent fatal page faults when accessing these pages. The function snp_leak_pages() is renamed to snp_mark_pages_offline() and is an external function available to both CCP driver and the SNP hypervisor code. Removed call to memory_failure() when leaking/marking pages offline.
* Remove snp_set_rmp_state() multiplexor code and add new separate helpers such as rmp_mark_pages_firmware() & rmp_mark_pages_shared(). The callers now issue snp_reclaim_pages() directly when needed as done by __snp_free_firmware_pages() and unmap_firmware_writeable(). All callers of snp_set_rmp_state() modified to call helpers rmp_mark_pages_firmware() or rmp_mark_pages_shared() as required.
* Change snp_reclaim_pages() to take physical address as an argument and clear C-bit from this physical address argument internally.
* Output parameter sev_user_data_ext_snp_config in sev_ioctl_snp_get_config() is memset to zero to avoid kernel memory leaking.
* Prevent race between sev_ioctl_snp_set_config() and snp_guest_ext_guest_request() for sev->snp_certs_data by acquiring sev->snp_certs_lock mutex.
* Zeroed out struct sev_user_data_snp_config in sev_ioctl_snp_set_config() to prevent leaking uninitialized kernel memory.
* Optimized snp_safe_alloc_page() by avoiding multiple calls to pfn_to_page() and checking for a hugepage using pfn instead of expanding to full physical address.
* Invoke host_rmp_make_shared() with leak parameter set to true if VMSA page cannot be transitioned back to shared state.
* Fix snp_launch_finish() to always sent the ID_AUTH struct to the firmware. Use params.auth_key_en indicator to set
if the ID_AUTH struct contains an author key or not.
* Cleanup snp_context_create() and allocate certs_data in this function using kzalloc() to prevent giving the guest uninitialized kernel memory.
* Remove the check for guest supplied buffer greater than the data provided by the hypervisor in snp_handle_ext_guest_request().
* Add check in sev_snp_ap_create() if a malicious guest can RMPADJUST a large page into VMSA which will hit the SNP erratum where the CPU will incorrectly signal an RMP violation #PF if a hugepage collides with the RMP entry of VMSA page, reject the AP CREATE request if VMSA address from guest is 2M aligned.
* Make VMSAVE target area memory allocation SNP safe, implemented workaround for an SNP erratum where the CPU will incorrectly signal an RMP violation #PF if a hugepage (2mb or 1gb) collides with the RMP entry of the VMSAVE target page.
* Fix handle_split_page_fault() to work with memfd backed pages.
* Add KVM commands for per-VM instance certificates.
* Add IOMMU_SNP_SHUTDOWN support, this adds support for Host kexec support with SNP.
Documentation/virt/coco/sev-guest.rst | 54 + Documentation/virt/kvm/api.rst | 34 + .../virt/kvm/x86/amd-memory-encryption.rst | 147 ++ arch/x86/Kbuild | 2 +- arch/x86/coco/Makefile | 3 +- arch/x86/coco/sev/Makefile | 3 + arch/x86/coco/sev/host.c | 524 ++++++ arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/kvm-x86-ops.h | 3 + arch/x86/include/asm/kvm_host.h | 23 + arch/x86/include/asm/msr-index.h | 11 +- arch/x86/include/asm/sev-common.h | 30 + arch/x86/include/asm/sev-host.h | 37 + arch/x86/include/asm/sev.h | 5 +- arch/x86/include/asm/svm.h | 6 + arch/x86/include/asm/trap_pf.h | 18 +- arch/x86/kernel/cpu/amd.c | 24 +- arch/x86/kernel/cpu/bugs.c | 7 +- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/lapic.c | 5 +- arch/x86/kvm/mmu.h | 2 - arch/x86/kvm/mmu/mmu.c | 15 +- arch/x86/kvm/mmu/mmu_internal.h | 39 +- arch/x86/kvm/svm/nested.c | 2 +- arch/x86/kvm/svm/sev.c | 1802 +++++++++++++++++--- arch/x86/kvm/svm/svm.c | 53 +- arch/x86/kvm/svm/svm.h | 38 +- arch/x86/kvm/x86.c | 17 + arch/x86/mm/fault.c | 21 + drivers/crypto/ccp/sev-dev.c | 1064 +++++++++++- drivers/crypto/ccp/sev-dev.h | 16 + drivers/iommu/amd/init.c | 57 +- include/linux/amd-iommu.h | 3 +- include/linux/kvm_host.h | 10 + include/linux/psp-sev.h | 304 +++- include/uapi/linux/kvm.h | 74 + include/uapi/linux/psp-sev.h | 71 + tools/arch/x86/include/asm/cpufeatures.h | 1 + virt/kvm/guest_mem.c | 48 +- virt/kvm/kvm_main.c | 75 +- 41 files changed, 4383 insertions(+), 275 deletions(-)
RFC PATCH v9 1/51 KVM: x86: Add gmem hook for initializing private memory
All gmem pages are expected to be 'private' as defined by a particular arch/platform. Platforms like SEV-SNP require additional operations to move these pages into a private state, so implement a hook that can be used to prepare this memory prior to mapping it into a guest.
In the case of SEV-SNP, whether or not a 2MB page can be mapped via a 2MB mapping in the guest's nested page table depends on whether or not any subpages within the range have already been initialized as private in the RMP table, so this hook will also be used by the KVM MMU to clamp the maximum mapping size accordingly.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 3 +++ arch/x86/kvm/mmu/mmu.c | 11 ++++++++++- 3 files changed, 14 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -133,6 +133,7 @@ KVM_X86_OP(msr_filter_changed) KVM_X86_OP(complete_emulated_msr) KVM_X86_OP(vcpu_deliver_sipi_vector) KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); +KVM_X86_OP_OPTIONAL_RET0(gmem_prepare) #undef KVM_X86_OP #undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +3 -0
@@ -1732,6 +1732,9 @@ struct kvm_x86_ops {* Returns vCPU specific APICv inhibit reasons */ unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu); + +int (*gmem_prepare)(struct kvm *kvm,struct kvm_memory_slot *slot, +kvm_pfn_t pfn,gfn_t gfn,u8 *max_level); };struct kvm_x86_nested_ops {
arch/x86/kvm/mmu/mmu.c +10 -1
@@ -4341,6 +4341,7 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) {int order, r; +u8 max_level;if (!kvm_slot_can_be_private (fault->slot))return kvm_do_memory_fault_exit (vcpu, fault);
@@ -4349,7 +4350,15 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu,if (r)return r; - fault->max_level =min (kvm_max_level_for_order (order), fault->max_level); + max_level =kvm_max_level_for_order (order); + r =static_call (kvm_x86_gmem_prepare)(vcpu->kvm, fault->slot, fault->pfn, + fault->gfn, &max_level); +if (r) { +kvm_release_pfn_clean (fault->pfn); +return r; + } + + fault->max_level =min (max_level, fault->max_level); fault->map_writable = !(fault->slot->flags &KVM_MEM_READONLY );return RET_PF_CONTINUE ; }
Signature · 1 line
2.25.1
RFC PATCH v9 2/51 KVM: x86: Add gmem hook for invalidating private memory
In reply to Michael RothTODO: add a CONFIG option that can be to completely skip arch invalidation loop and avoid __weak references for arch/platforms that don't need an additional invalidation hook.
In some cases, like with SEV-SNP, guest memory needs to be updated in a platform-specific manner before it can be safely freed back to the host. Add hooks to wire up handling of this sort when freeing memory in response to FALLOC_FL_PUNCH_HOLE operations.
Also issue invalidations of all allocated pages when releasing the gmem file so that the pages are not left in an unusable state when they get freed back to the host.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 6 ++++ include/linux/kvm_host.h | 3 ++ virt/kvm/guest_mem.c | 48 ++++++++++++++++++++++++++++-- 5 files changed, 57 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -134,6 +134,7 @@ KVM_X86_OP(complete_emulated_msr)KVM_X86_OP (vcpu_deliver_sipi_vector)KVM_X86_OP_OPTIONAL_RET0 (vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL_RET0(gmem_prepare) +KVM_X86_OP_OPTIONAL(gmem_invalidate)#undef KVM_X86_OP#undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1735,6 +1735,7 @@ struct kvm_x86_ops {int (*gmem_prepare)(struct kvm *kvm,struct kvm_memory_slot *slot,kvm_pfn_t pfn,gfn_t gfn,u8 *max_level); +void (*gmem_invalidate)(struct kvm *kvm,kvm_pfn_t start,kvm_pfn_t end); };struct kvm_x86_nested_ops {
arch/x86/kvm/x86.c +6 -0
@@ -13252,6 +13252,12 @@ bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_GPL (kvm_arch_no_poll); +#ifdef CONFIG_KVM_PRIVATE_MEM +void kvm_arch_gmem_invalidate (struct kvm *kvm,kvm_pfn_t start,kvm_pfn_t end) +{ +static_call_cond (kvm_x86_gmem_invalidate)(kvm, start, end); +} +#endif int kvm_spec_ctrl_test_value (u64 value) {
include/linux/kvm_host.h +3 -0
@@ -2343,6 +2343,7 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)#ifdef CONFIG_KVM_PRIVATE_MEM int kvm_gmem_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot,gfn_t gfn,kvm_pfn_t *pfn,int *order); +void kvm_arch_gmem_invalidate (struct kvm *kvm,kvm_pfn_t start,kvm_pfn_t end);#else static inline int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn,
@@ -2351,6 +2352,8 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm,KVM_BUG_ON (1 , kvm);return -EIO ; } + +void kvm_arch_gmem_invalidate (struct kvm *kvm,kvm_pfn_t start,kvm_pfn_t end) { }#endif /* CONFIG_KVM_PRIVATE_MEM */ #endif
virt/kvm/guest_mem.c +46 -2
74 lines not on this page · raw message
Signature · 1 line
2.25.1
Borislav Petkov
maintainerRe: [PATCH RFC v9 02/51] KVM: x86: Add gmem hook for invalidating private memory
Quoted 13 lines · Michael Roth
TODO: add a CONFIG option that can be to completely skip arch invalidation loop and avoid
__weakreferences forarch/platformsthat don't need an additional invalidation hook.In some cases, like with SEV-SNP, guest memory needs to be updated in a platform-specific manner before it can be safely freed back to the host. Add hooks to wire up handling of this sort when freeing memory in response to
FALLOC_FL_PUNCH_HOLEoperations.Also issue invalidations of all allocated pages when releasing the gmem file so that the pages are not left in an unusable state when they get freed back to the host.
Quoted 8 lines
Signed-off-by Michael Roth <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 6 ++++ include/linux/kvm_host.h | 3 ++ virt/kvm/guest_mem.c | 48 ++++++++++++++++++++++++++++-- 5 files changed, 57 insertions(+), 2 deletions(-)
ld: arch/x86/kvm/../../../virt/kvm/eventfd.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/../../../virt/kvm/binary_stats.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/arch/x86/kvm/../../../virt/kvm/binary_stats.c:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/../../../virt/kvm/vfio.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/../../../virt/kvm/coalesced_mmio.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/../../../virt/kvm/async_pf.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/../../../virt/kvm/irqchip.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/../../../virt/kvm/dirty_ring.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/../../../virt/kvm/pfncache.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/x86.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/emulate.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/i8259.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/irq.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/lapic.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/i8254.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/ioapic.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/irq_comm.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/cpuid.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/pmu.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/mtrr.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/hyperv.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/debugfs.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/mmu/mmu.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/mmu/page_track.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/mmu/spte.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/mmu/tdp_iter.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/arch/x86/kvm/mmu/tdp_iter.c:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/mmu/tdp_mmu.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld: arch/x86/kvm/smm.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here make[3]: *** [scripts/Makefile.build:452: arch/x86/kvm/kvm.o] Error 1
make[3]: *** Waiting for unfinished jobs....
make[2]: *** [scripts/Makefile.build:494: arch/x86/kvm] Error 2
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [scripts/Makefile.build:494: arch/x86] Error 2
make[1]: *** Waiting for unfinished jobs....
make: *** [Makefile:2028: .] Error 2
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerRe: [PATCH RFC v9 02/51] KVM: x86: Add gmem hook for invalidating private memory
Quoted 3 lines · Borislav Petkov
ld:
arch/x86/kvm/../../../virt/kvm/eventfd.o: in function `kvm_arch_gmem_invalidate':/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: multiple definition of `kvm_arch_gmem_invalidate'; arch/x86/kvm/../../../virt/kvm/kvm_main.o:/home/boris/kernel/2nd/linux/./include/linux/kvm_host.h:2356: first defined here
ld:arch/x86/kvm/../../../virt/kvm/binary_stats.o: in function `kvm_arch_gmem_invalidate':
Fix is trivial:
---
include/linux/kvm_host.h +1 -1
@@ -2353,7 +2353,7 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm,return -EIO ; } -void kvm_arch_gmem_invalidate (struct kvm *kvm,kvm_pfn_t start,kvm_pfn_t end) { } +static inline void kvm_arch_gmem_invalidate (struct kvm *kvm,kvm_pfn_t start,kvm_pfn_t end) { }#endif /* CONFIG_KVM_PRIVATE_MEM */ #endif
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRFC PATCH v9 3/51 KVM: x86: Use full 64-bit error code for kvm_mmu_do_page_fault
In reply to Michael RothThe upper bits will be needed in some cases to distinguish between nested page faults for private/shared pages, so pass along the full 64-bit value.
--- arch/x86/kvm/mmu/mmu.c | 3 +-- arch/x86/kvm/mmu/mmu_internal.h | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-)
arch/x86/kvm/mmu/mmu.c +1 -2
@@ -5829,8 +5829,7 @@ int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 err }if (r ==RET_PF_INVALID ) { - r =kvm_mmu_do_page_fault (vcpu, cr2_or_gpa, -lower_32_bits (error_code), false, + r =kvm_mmu_do_page_fault (vcpu, cr2_or_gpa, error_code, false, &emulation_type);if (KVM_BUG_ON (r ==RET_PF_INVALID , vcpu->kvm))return -EIO ;
arch/x86/kvm/mmu/mmu_internal.h +2 -2
@@ -283,11 +283,11 @@ enum { };static inline int kvm_mmu_do_page_fault (struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa, -u32 err,bool prefetch,int *emulation_type) +u64 err,bool prefetch,int *emulation_type) {struct kvm_page_fault fault = { .addr = cr2_or_gpa, - .error_code = err, + .error_code =lower_32_bits (err), .exec = err &PFERR_FETCH_MASK , .write = err &PFERR_WRITE_MASK , .present = err &PFERR_PRESENT_MASK ,
Signature · 1 line
2.25.1
Re: [PATCH RFC v9 03/51] KVM: x86: Use full 64-bit error code for kvm_mmu_do_page_fault
Quoted 44 lines · Michael Roth
The upper bits will be needed in some cases to distinguish between nested page faults for private/shared pages, so pass along the full 64-bit value.
Signed-off-by Michael Roth <[email protected]>--- arch/x86/kvm/mmu/mmu.c | 3 +-- arch/x86/kvm/mmu/mmu_internal.h | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-)arch/x86/kvm/mmu/mmu.c +1
-2@@ -5829,8 +5829,7 @@ int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 err }if (r ==RET_PF_INVALID ) { - r =kvm_mmu_do_page_fault (vcpu, cr2_or_gpa, -lower_32_bits (error_code), false, + r =kvm_mmu_do_page_fault (vcpu, cr2_or_gpa, error_code, false, &emulation_type);if (KVM_BUG_ON (r ==RET_PF_INVALID , vcpu->kvm))return -EIO ;arch/x86/kvm/mmu/mmu_internal.h +2
-2@@ -283,11 +283,11 @@ enum { };static inline int kvm_mmu_do_page_fault (struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa, -u32 err,bool prefetch,int *emulation_type) +u64 err,bool prefetch,int *emulation_type) {struct kvm_page_fault fault = { .addr = cr2_or_gpa, - .error_code = err, + .error_code =lower_32_bits (err), .exec = err &PFERR_FETCH_MASK , .write = err &PFERR_WRITE_MASK , .present = err &PFERR_PRESENT_MASK ,Signature · 1 line
2.25.1
Hi. I'd like to pass around 64bit for TDX and come up with the following one. Does it work for you?
From 53273b67e9be09129d35ac00cf9ce739d3fb4e2c Mon Sep 17 00:00:00 2001
Message-Id: <53273b67e9be09129d35ac00cf9ce739d3fb4e2c.1686752340.git.isaku.yamahata@intel.com>
From: Isaku Yamahata <[email protected]>
Date: Fri, 17 Mar 2023 12:58:42 -0700
Subject: [PATCH] KVM: x86/mmu: Pass round full 64-bit error code for the KVM
page fault
In some cases the full 64-bit error code for the KVM page fault will be needed to make this determination, so also update kvm_mmu_do_page_fault() to accept the full 64-bit value so it can be plumbed through to the callback.
The upper 32 bits of error code is discarded at kvm_mmu_page_fault() by lower_32_bits(). Now it's passed down as full 64 bits. It turns out that only FNAME(page_fault) depends on it. Move lower_32_bits() into FNAME(page_fault).
The accesses of fault->error_code are as follows
- FNAME(page_fault): change to explicitly use lower_32_bits()
- kvm_tdp_page_fault(): explicit mask with PFERR_LEVEL_MASK
- kvm_mmu_page_fault(): explicit mask with PFERR_RSVD_MASK, PFERR_NESTED_GUEST_PAGE
- mmutrace: changed u32 -> u64
- pgprintk(): change %x -> %llx
--- arch/x86/kvm/mmu.h | 2 +- arch/x86/kvm/mmu/mmu.c | 7 +++---- arch/x86/kvm/mmu/mmu_internal.h | 4 ++-- arch/x86/kvm/mmu/mmutrace.h | 2 +- arch/x86/kvm/mmu/paging_tmpl.h | 4 ++-- 5 files changed, 9 insertions(+), 10 deletions(-)
arch/x86/kvm/mmu.h +1 -1
@@ -176,7 +176,7 @@ static inline void kvm_mmu_refresh_passthrough_bits(struct kvm_vcpu *vcpu, }kvm_pfn_t kvm_mmu_map_tdp_page (struct kvm_vcpu *vcpu,gpa_t gpa, -u32 error_code,int max_level); +u64 error_code,int max_level);/* * Check if a given access (described through the I/D, W/R and U/S bits of a
arch/x86/kvm/mmu/mmu.c +3 -4
@@ -4941,7 +4941,7 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultstatic int nonpaging_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) { -pgprintk ("%s: gva %llx error %x\n" , __func__, fault->addr, fault->error_code); +pgprintk ("%s: gva %llx error %llx\n" , __func__, fault->addr, fault->error_code);/* This path builds a PAE pagetable, we can map 2mb pages at maximum. */ fault->max_level =PG_LEVEL_2M ;
@@ -5062,7 +5062,7 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) }kvm_pfn_t kvm_mmu_map_tdp_page (struct kvm_vcpu *vcpu,gpa_t gpa, -u32 error_code,int max_level) +u64 error_code,int max_level) {int r;struct kvm_page_fault fault = (struct kvm_page_fault ) {
@@ -6317,8 +6317,7 @@ int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 err }if (r ==RET_PF_INVALID ) { - r =kvm_mmu_do_page_fault (vcpu, cr2_or_gpa, -lower_32_bits (error_code), false, + r =kvm_mmu_do_page_fault (vcpu, cr2_or_gpa, error_code, false, &emulation_type);if (KVM_BUG_ON (r ==RET_PF_INVALID , vcpu->kvm))return -EIO ;
arch/x86/kvm/mmu/mmu_internal.h +2 -2
@@ -342,7 +342,7 @@ static inline bool is_nx_huge_page_enabled(struct kvm *kvm)struct kvm_page_fault {/* arguments to kvm_mmu_do_page_fault. */ const gpa_t addr; -const u32 error_code; +const u64 error_code;const bool prefetch;/* Derived from error_code. */
@@ -437,7 +437,7 @@ enum { };static inline int kvm_mmu_do_page_fault (struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa, -u32 err,bool prefetch,int *emulation_type) +u64 err,bool prefetch,int *emulation_type) {struct kvm_page_fault fault = { .addr = cr2_or_gpa,
arch/x86/kvm/mmu/mmutrace.h +1 -1
@@ -261,7 +261,7 @@ TRACE_EVENT(TP_STRUCT__entry (__field (int ,vcpu_id )__field (gpa_t, cr2_or_gpa) -__field (u32, error_code) +__field (u64, error_code) __field(u64 *, sptep)__field (u64, old_spte) __field(u64, new_spte)
arch/x86/kvm/mmu/paging_tmpl.h +2 -2
@@ -758,7 +758,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultstruct guest_walker walker;int r; -pgprintk ("%s: addr %llx err %x\n" , __func__, fault->addr, fault->error_code); +pgprintk ("%s: addr %llx err %llx\n" , __func__, fault->addr, fault->error_code);WARN_ON_ONCE (fault->is_tdp);/*
@@ -767,7 +767,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault* The bit needs to be cleared before walking guest page tables. */ r =FNAME (walk_addr)(&walker, vcpu, fault->addr, - fault->error_code & ~PFERR_RSVD_MASK ); +lower_32_bits (fault->error_code) & ~PFERR_RSVD_MASK );/* * The page is not mapped by the guest. Let the guest handle it.
Signature · 7 lines
2.25.1 -- Isaku Yamahata <[email protected]>
RFC PATCH v9 4/51 KVM: x86: Determine shared/private faults using a configurable mask
In reply to Michael RothThis will be used to determine whether or not an #NPF should be serviced using a normal page vs. a guarded/gmem one.
--- arch/x86/include/asm/kvm_host.h | 7 +++++++ arch/x86/kvm/mmu/mmu_internal.h | 35 ++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +7 -0
@@ -1445,6 +1445,13 @@ struct kvm_arch {*/ #define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1)struct kvm_mmu_memory_cache split_desc_cache; + +/* +* When set, used to determine whether a fault should be treated as +* private in the context of protected VMs which use a separate gmem +* pool to back private guest pages. +*/ +u64 mmu_private_fault_mask; };struct kvm_vm_stat {
arch/x86/kvm/mmu/mmu_internal.h +34 -1
@@ -252,6 +252,39 @@ struct kvm_page_fault {int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault); +static bool kvm_mmu_fault_is_private (struct kvm *kvm,gpa_t gpa,u64 err) +{ +struct kvm_memory_slot *slot; +bool private_fault = false; +gfn_t gfn =gpa_to_gfn (gpa); + + slot =gfn_to_memslot (kvm, gfn); +if (!slot) { +pr_debug ("%s: no slot, GFN: 0x%llx\n" , __func__, gfn); + goto out; + } + +if (!kvm_slot_can_be_private (slot)) { +pr_debug ("%s: slot is not private, GFN: 0x%llx\n" , __func__, gfn); + goto out; + } + +if (kvm->arch.mmu_private_fault_mask) { + private_fault = !!(err & kvm->arch.mmu_private_fault_mask); + goto out; + } + +/* +* Handling below is for UPM self-tests and guests that treat userspace +* as the authority on whether a fault should be private or not. +*/ + private_fault =kvm_mem_is_private (kvm, gpa >>PAGE_SHIFT ); + +out: +pr_debug ("%s: GFN: 0x%llx, private: %d\n" , __func__, gfn, private_fault); +return private_fault; +} +/* * Return values of handle_mmio_page_fault(), mmu.page_fault(), fast_page_fault(), * and of course kvm_mmu_do_page_fault().
@@ -301,7 +334,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .max_level = KVM_MAX_HUGEPAGE_LEVEL, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K, - .is_private = kvm_mem_is_private(vcpu->kvm, cr2_or_gpa >> PAGE_SHIFT), + .is_private = kvm_mmu_fault_is_private(vcpu->kvm, cr2_or_gpa, err), };int r;
Signature · 1 line
2.25.1
Re: [PATCH RFC v9 04/51] KVM: x86: Determine shared/private faults using a configurable mask
Quoted 46 lines · Michael Roth
This will be used to determine whether or not an #NPF should be serviced using a normal page vs. a guarded/gmem one.
Signed-off-by Michael Roth <[email protected]>--- arch/x86/include/asm/kvm_host.h | 7 +++++++ arch/x86/kvm/mmu/mmu_internal.h | 35 ++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-)arch/x86/include/asm/kvm_host.h +7
-0@@ -1445,6 +1445,13 @@ struct kvm_arch {*/ #define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1)struct kvm_mmu_memory_cache split_desc_cache; + +/* +* When set, used to determine whether a fault should be treated as +* private in the context of protected VMs which use a separate gmem +* pool to back private guest pages. +*/ +u64 mmu_private_fault_mask; };struct kvm_vm_stat {arch/x86/kvm/mmu/mmu_internal.h +11
-0@@ -252,6 +252,39 @@ struct kvm_page_fault {int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault); +static bool kvm_mmu_fault_is_private (struct kvm *kvm,gpa_t gpa,u64 err) +{ +struct kvm_memory_slot *slot; +bool private_fault = false; +gfn_t gfn =gpa_to_gfn (gpa); + + slot =gfn_to_memslot (kvm, gfn); +if (!slot) { +pr_debug ("%s: no slot, GFN: 0x%llx\n" , __func__, gfn); + goto out; + }
arch/x86/kvm/mmu/mmu_internal.h struct kvm_page_fault { · line 275
Quoted 10 lines
arch/x86/kvm/mmu/mmu_internal.h +10
-0+ +if (!kvm_slot_can_be_private (slot)) { +pr_debug ("%s: slot is not private, GFN: 0x%llx\n" , __func__, gfn); + goto out; + } + +if (kvm->arch.mmu_private_fault_mask) { + private_fault = !!(err & kvm->arch.mmu_private_fault_mask); + goto out; + }
What's the convention of err? Can we abstract it by introducing a new bit PFERR_PRIVATE_MASK? The caller sets it based on arch specific value. the logic will be
.is_private = err & PFERR_PRIVATE_MASK;
arch/x86/kvm/mmu/mmu_internal.h struct kvm_page_fault { · line 281
Quoted 6 lines
arch/x86/kvm/mmu/mmu_internal.h +6
-0+ +/* +* Handling below is for UPM self-tests and guests that treat userspace +* as the authority on whether a fault should be private or not. +*/ + private_fault =kvm_mem_is_private (kvm, gpa >>PAGE_SHIFT );
This code path is sad. One extra slot lookup and xarray look up. Without mmu lock, the result can change by other vcpu. Let's find a better way.
Quoted 21 lines
arch/x86/kvm/mmu/mmu_internal.h +7
-1+ +out: +pr_debug ("%s: GFN: 0x%llx, private: %d\n" , __func__, gfn, private_fault); +return private_fault; +} +/* * Return values of handle_mmio_page_fault(), mmu.page_fault(), fast_page_fault(), * and of course kvm_mmu_do_page_fault(). @@ -301,7 +334,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .max_level = KVM_MAX_HUGEPAGE_LEVEL, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K, - .is_private = kvm_mem_is_private(vcpu->kvm, cr2_or_gpa >> PAGE_SHIFT), + .is_private = kvm_mmu_fault_is_private(vcpu->kvm, cr2_or_gpa, err), };int r;Signature · 1 line
2.25.1
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH RFC v9 04/51] KVM: x86: Determine shared/private faults using a configurable mask
Quoted 63 lines · Isaku Yamahata
Quoted 46 lines · Michael Roth
This will be used to determine whether or not an #NPF should be serviced using a normal page vs. a guarded/gmem one.
Signed-off-by Michael Roth <[email protected]>--- arch/x86/include/asm/kvm_host.h | 7 +++++++ arch/x86/kvm/mmu/mmu_internal.h | 35 ++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-)arch/x86/include/asm/kvm_host.h +7
-0@@ -1445,6 +1445,13 @@ struct kvm_arch {*/ #define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1)struct kvm_mmu_memory_cache split_desc_cache; + +/* +* When set, used to determine whether a fault should be treated as +* private in the context of protected VMs which use a separate gmem +* pool to back private guest pages. +*/ +u64 mmu_private_fault_mask; };struct kvm_vm_stat {arch/x86/kvm/mmu/mmu_internal.h +11
-0@@ -252,6 +252,39 @@ struct kvm_page_fault {int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault); +static bool kvm_mmu_fault_is_private (struct kvm *kvm,gpa_t gpa,u64 err) +{ +struct kvm_memory_slot *slot; +bool private_fault = false; +gfn_t gfn =gpa_to_gfn (gpa); + + slot =gfn_to_memslot (kvm, gfn); +if (!slot) { +pr_debug ("%s: no slot, GFN: 0x%llx\n" , __func__, gfn); + goto out; + }arch/x86/kvm/mmu/mmu_internal.h struct kvm_page_fault { · line 275
Quoted 10 lines
arch/x86/kvm/mmu/mmu_internal.h +10
-0+ +if (!kvm_slot_can_be_private (slot)) { +pr_debug ("%s: slot is not private, GFN: 0x%llx\n" , __func__, gfn); + goto out; + } + +if (kvm->arch.mmu_private_fault_mask) { + private_fault = !!(err & kvm->arch.mmu_private_fault_mask); + goto out; + }What's the convention of err? Can we abstract it by introducing a new bit
PFERR_PRIVATE_MASK? The caller sets it based on arch specific value. the logic will be
Quoted 1 line
.is_private = err & PFERR_PRIVATE_MASK;
I'm not sure I understand the question. 'err' is just the page fault flags, and arch.mmu_private_fault_mask is something that can be set on a per-platform basis when running in a mode where shared/private access is recorded in the page fault flags during a #NPF.
I'm not sure how we'd keep the handling cross-platform by moving to a macro, since TDX uses a different bit, and we'd want to be able to build a SNP+TDX kernel that could run on either type of hardware.
Are you suggesting to reverse that and have err be set in a platform-specific way and then use a common PFERR_PRIVATE_MASK that's software-defined and consistent across platforms? That could work, but existing handling seems to use page fault flags as-is, keeping the hardware-set values, rather than modifying them to pass additional metadata, so it seems like it might make things more confusing to make an exception to that here. Or are there other cases where it's done that way?
Quoted 12 lines
arch/x86/kvm/mmu/mmu_internal.h struct kvm_page_fault { · line 281
Quoted 8 lines
arch/x86/kvm/mmu/mmu_internal.h +6
-0+ +/* +* Handling below is for UPM self-tests and guests that treat userspace +* as the authority on whether a fault should be private or not. +*/ + private_fault =kvm_mem_is_private (kvm, gpa >>PAGE_SHIFT );This code path is sad. One extra slot lookup and xarray look up. Without mmu lock, the result can change by other vcpu. Let's find a better way.
The intention was to rely on fault->mmu_seq to determine if a KVM_SET_MEMORY_ATTRIBUTES update came in after .private_fault was set so that fault handling could be retried, but that doesn't happen until kvm_faultin_pfn() which is *after* this is logged. So yes, I think there is a race here, and the approach you took in your Misc. series of keeping the kvm_mem_is_private() check inside kvm_faultin_pfn() is more efficient/correct.
If we can figure out a way to handle checking the fault flags in a way that works for both TDX/SNP (and KVM self-test use-case) we can consolidate around that.
-Mike
Quoted 25 lines
Quoted 22 lines
arch/x86/kvm/mmu/mmu_internal.h +7
-1+ +out: +pr_debug ("%s: GFN: 0x%llx, private: %d\n" , __func__, gfn, private_fault); +return private_fault; +} +/* * Return values of handle_mmio_page_fault(), mmu.page_fault(), fast_page_fault(), * and of course kvm_mmu_do_page_fault(). @@ -301,7 +334,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .max_level = KVM_MAX_HUGEPAGE_LEVEL, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K, - .is_private = kvm_mem_is_private(vcpu->kvm, cr2_or_gpa >> PAGE_SHIFT), + .is_private = kvm_mmu_fault_is_private(vcpu->kvm, cr2_or_gpa, err), };int r;Signature · 1 line
2.25.1Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH RFC v9 04/51] KVM: x86: Determine shared/private faults using a configurable mask
Quoted 75 lines · Michael Roth
Quoted 63 lines · Isaku Yamahata
Quoted 46 lines · Michael Roth
This will be used to determine whether or not an #NPF should be serviced using a normal page vs. a guarded/gmem one.
Signed-off-by Michael Roth <[email protected]>--- arch/x86/include/asm/kvm_host.h | 7 +++++++ arch/x86/kvm/mmu/mmu_internal.h | 35 ++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-)arch/x86/include/asm/kvm_host.h +7
-0@@ -1445,6 +1445,13 @@ struct kvm_arch {*/ #define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1)struct kvm_mmu_memory_cache split_desc_cache; + +/* +* When set, used to determine whether a fault should be treated as +* private in the context of protected VMs which use a separate gmem +* pool to back private guest pages. +*/ +u64 mmu_private_fault_mask; };struct kvm_vm_stat {arch/x86/kvm/mmu/mmu_internal.h +11
-0@@ -252,6 +252,39 @@ struct kvm_page_fault {int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault); +static bool kvm_mmu_fault_is_private (struct kvm *kvm,gpa_t gpa,u64 err) +{ +struct kvm_memory_slot *slot; +bool private_fault = false; +gfn_t gfn =gpa_to_gfn (gpa); + + slot =gfn_to_memslot (kvm, gfn); +if (!slot) { +pr_debug ("%s: no slot, GFN: 0x%llx\n" , __func__, gfn); + goto out; + }arch/x86/kvm/mmu/mmu_internal.h struct kvm_page_fault { · line 275
Quoted 10 lines
arch/x86/kvm/mmu/mmu_internal.h +10
-0+ +if (!kvm_slot_can_be_private (slot)) { +pr_debug ("%s: slot is not private, GFN: 0x%llx\n" , __func__, gfn); + goto out; + } + +if (kvm->arch.mmu_private_fault_mask) { + private_fault = !!(err & kvm->arch.mmu_private_fault_mask); + goto out; + }What's the convention of err? Can we abstract it by introducing a new bit
PFERR_PRIVATE_MASK? The caller sets it based on arch specific value. the logic will beQuoted 1 line
.is_private = err & PFERR_PRIVATE_MASK;I'm not sure I understand the question. 'err' is just the page fault flags, and arch.mmu_private_fault_mask is something that can be set on a per-platform basis when running in a mode where shared/private access is recorded in the page fault flags during a #NPF.
I'm not sure how we'd keep the handling cross-platform by moving to a macro, since TDX uses a different bit, and we'd want to be able to build a SNP+TDX kernel that could run on either type of hardware.
Quoted 7 lines
Are you suggesting to reverse that and have err be set in a platform-specific way and then use a common
PFERR_PRIVATE_MASKthat's software-defined and consistent across platforms? That could work, but existing handling seems to use page fault flags as-is, keeping the hardware-set values, rather than modifying them to pass additional metadata, so it seems like it might make things more confusing to make an exception to that here. Or are there other cases where it's done that way?
I meant the latter, making PFERR_PRIVATE_MASK common software-defined.
I think the SVM fault handler can use hardware value directly by carefully defining those PFERR values.
TDX doesn't have architectural bit in error code to indicate the private fault. It's coded in faulted address as shared bit. GPA bit 51 or 47. PFERR_{USER, WRITE, FETCH, PRESENT} are already software-defined value for VMX (and TDX). The fault handler for VMX, handle_ept_violation(), converts encoding. For TDX, PFERR_PRIVATE_MASK is just one more software defined bit.
I'm fine with either way, variable or macro. Which do you prefer?
- Define variable mmu_private_fault_mask (global or per struct kvm) The module initialization code, hardware_setup(), sets mmu_private_fault_mask.
- Define the software defined value, PFERR_PRIVATE_MASK.
The caller of kvm_mmu_page_fault() parses the hardware value and construct software defined error_code.
- any other?
Quoted 19 lines
Quoted 10 lines
arch/x86/kvm/mmu/mmu_internal.h struct kvm_page_fault { · line 281
Quoted 6 lines
arch/x86/kvm/mmu/mmu_internal.h +6
-0+ +/* +* Handling below is for UPM self-tests and guests that treat userspace +* as the authority on whether a fault should be private or not. +*/ + private_fault =kvm_mem_is_private (kvm, gpa >>PAGE_SHIFT );This code path is sad. One extra slot lookup and xarray look up. Without mmu lock, the result can change by other vcpu. Let's find a better way.
The intention was to rely on
fault->mmu_seqto determine if aKVM_SET_MEMORY_ATTRIBUTESupdate came in after .private_fault was set so that fault handling could be retried, but that doesn't happen untilkvm_faultin_pfn()which is *after* this is logged. So yes, I think there is a race here, and the approach you took in your Misc. series of keeping thekvm_mem_is_private()check insidekvm_faultin_pfn()is more efficient/correct.
Quoted 3 lines
If we can figure out a way to handle checking the fault flags in a way that works for both TDX/SNP (and KVM self-test use-case) we can consolidate around that.
I can think of the following ways. I think the second option is better because we don't need exit bit for error code.
- Introduce software defined error code
- Add a flags to struct kvm_arch for self-test use-case VM_TYPE_PROTECTED_VM. Set it to true for VM_TYPE_PROTECTED_VM case.
- any other?
Thanks,
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH RFC v9 04/51] KVM: x86: Determine shared/private faults using a configurable mask
Quoted 130 lines · Isaku Yamahata
Quoted 75 lines · Michael Roth
Quoted 63 lines · Isaku Yamahata
Quoted 46 lines · Michael Roth
This will be used to determine whether or not an #NPF should be serviced using a normal page vs. a guarded/gmem one.
Signed-off-by Michael Roth <[email protected]>--- arch/x86/include/asm/kvm_host.h | 7 +++++++ arch/x86/kvm/mmu/mmu_internal.h | 35 ++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-)arch/x86/include/asm/kvm_host.h +7
-0@@ -1445,6 +1445,13 @@ struct kvm_arch {*/ #define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1)struct kvm_mmu_memory_cache split_desc_cache; + +/* +* When set, used to determine whether a fault should be treated as +* private in the context of protected VMs which use a separate gmem +* pool to back private guest pages. +*/ +u64 mmu_private_fault_mask; };struct kvm_vm_stat {arch/x86/kvm/mmu/mmu_internal.h +11
-0@@ -252,6 +252,39 @@ struct kvm_page_fault {int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault); +static bool kvm_mmu_fault_is_private (struct kvm *kvm,gpa_t gpa,u64 err) +{ +struct kvm_memory_slot *slot; +bool private_fault = false; +gfn_t gfn =gpa_to_gfn (gpa); + + slot =gfn_to_memslot (kvm, gfn); +if (!slot) { +pr_debug ("%s: no slot, GFN: 0x%llx\n" , __func__, gfn); + goto out; + }arch/x86/kvm/mmu/mmu_internal.h struct kvm_page_fault { · line 275
Quoted 10 lines
arch/x86/kvm/mmu/mmu_internal.h +10
-0+ +if (!kvm_slot_can_be_private (slot)) { +pr_debug ("%s: slot is not private, GFN: 0x%llx\n" , __func__, gfn); + goto out; + } + +if (kvm->arch.mmu_private_fault_mask) { + private_fault = !!(err & kvm->arch.mmu_private_fault_mask); + goto out; + }What's the convention of err? Can we abstract it by introducing a new bit
PFERR_PRIVATE_MASK? The caller sets it based on arch specific value. the logic will beQuoted 1 line
.is_private = err & PFERR_PRIVATE_MASK;I'm not sure I understand the question. 'err' is just the page fault flags, and arch.mmu_private_fault_mask is something that can be set on a per-platform basis when running in a mode where shared/private access is recorded in the page fault flags during a #NPF.
I'm not sure how we'd keep the handling cross-platform by moving to a macro, since TDX uses a different bit, and we'd want to be able to build a SNP+TDX kernel that could run on either type of hardware.
Quoted 7 lines
Are you suggesting to reverse that and have err be set in a platform-specific way and then use a common
PFERR_PRIVATE_MASKthat's software-defined and consistent across platforms? That could work, but existing handling seems to use page fault flags as-is, keeping the hardware-set values, rather than modifying them to pass additional metadata, so it seems like it might make things more confusing to make an exception to that here. Or are there other cases where it's done that way?I meant the latter, making
PFERR_PRIVATE_MASKcommon software-defined.I think the SVM fault handler can use hardware value directly by carefully defining those PFERR values.
TDX doesn't have architectural bit in error code to indicate the private fault. It's coded in faulted address as shared bit. GPA bit 51 or 47. PFERR_{USER, WRITE, FETCH, PRESENT} are already software-defined value for VMX (and TDX). The fault handler for VMX,
handle_ept_violation(), converts encoding. For TDX,PFERR_PRIVATE_MASKis just one more software defined bit.I'm fine with either way, variable or macro. Which do you prefer?
- Define variable
mmu_private_fault_mask(global or per struct kvm) The module initialization code,hardware_setup(), setsmmu_private_fault_mask.
- Define the software defined value,PFERR_PRIVATE_MASK.
The caller ofkvm_mmu_page_fault()parses the hardware value and construct software definederror_code.
- any other?Quoted 19 lines
Quoted 10 lines
arch/x86/kvm/mmu/mmu_internal.h struct kvm_page_fault { · line 281
Quoted 6 lines
arch/x86/kvm/mmu/mmu_internal.h +6
-0+ +/* +* Handling below is for UPM self-tests and guests that treat userspace +* as the authority on whether a fault should be private or not. +*/ + private_fault =kvm_mem_is_private (kvm, gpa >>PAGE_SHIFT );This code path is sad. One extra slot lookup and xarray look up. Without mmu lock, the result can change by other vcpu. Let's find a better way.
The intention was to rely on
fault->mmu_seqto determine if aKVM_SET_MEMORY_ATTRIBUTESupdate came in after .private_fault was set so that fault handling could be retried, but that doesn't happen untilkvm_faultin_pfn()which is *after* this is logged. So yes, I think there is a race here, and the approach you took in your Misc. series of keeping thekvm_mem_is_private()check insidekvm_faultin_pfn()is more efficient/correct.Quoted 3 lines
If we can figure out a way to handle checking the fault flags in a way that works for both TDX/SNP (and KVM self-test use-case) we can consolidate around that.
Quoted 7 lines
I can think of the following ways. I think the second option is better because we don't need exit bit for error code.
- Introduce software defined error code
- Add a flags to structkvm_archfor self-test use-caseVM_TYPE_PROTECTED_VM. Set it to true forVM_TYPE_PROTECTED_VMcase.
- any other?
Vishal: hoping to get your thoughts here as well from the perspective of the KVM self-test use-case.
I was thinking that once we set fault->is_private, that sort of becomes our "software-defined" bit, and what KVM would use from that point forward to determine whether or not the access should be treated as a private one or not, and that whatever handler sets fault->is_private would encapsulate away all the platform-specific bit-checking needed to do that.
So if we were to straight-forwardly implement that based on how TDX currently handles checking for the shared bit in GPA, paired with how SEV-SNP handles checking for private bit in fault flags, it would look something like:
bool kvm_fault_is_private(kvm, gpa, err)
{
/* SEV-SNP handling */
if (kvm->arch.mmu_private_fault_mask)
return !!(err & arch.mmu_private_fault_mask); /* TDX handling */
if (kvm->arch.gfn_shared_mask)
return !!(gpa & arch.gfn_shared_mask);
return false;}
kvm_mmu_do_page_fault(vcpu, gpa, err, ...)
{
struct kvm_page_fault fault = {
...
.is_private = kvm_fault_is_private(vcpu->kvm, gpa, err)
};...
}
And then arch.mmu_private_fault_mask and arch.gfn_shared_mask would be set per-KVM-instance, just like they are now with current SNP and TDX patchsets, since stuff like KVM self-test wouldn't be setting those masks, so it makes sense to do it per-instance in that regard.
But that still gets a little awkward for the KVM self-test use-case where .is_private should sort of be ignored in favor of whatever the xarray reports via kvm_mem_is_private(). In your Misc. series I believe you handled this by introducing a PFERR_HASATTR_MASK bit so we can determine whether existing value of fault->is_private should be ignored/overwritten or not.
So maybe kvm_fault_is_private() needs to return an integer value instead, like:
enum {
KVM_FAULT_VMM_DEFINED,
KVM_FAULT_SHARED,
KVM_FAULT_PRIVATE,
}
bool kvm_fault_is_private(kvm, gpa, err)
{
/* SEV-SNP handling */
if (kvm->arch.mmu_private_fault_mask)
(err & arch.mmu_private_fault_mask) ? KVM_FAULT_PRIVATE : KVM_FAULT_SHARED /* TDX handling */
if (kvm->arch.gfn_shared_mask)
(gpa & arch.gfn_shared_mask) ? KVM_FAULT_SHARED : KVM_FAULT_PRIVATE
return KVM_FAULT_VMM_DEFINED;}
And then down in __kvm_faultin_pfn() we do:
if (fault->is_private == KVM_FAULT_VMM_DEFINED)
fault->is_private = kvm_mem_is_private(vcpu->kvm, fault->gfn);
else if (fault->is_private != kvm_mem_is_private(vcpu->kvm, fault->gfn))
return kvm_do_memory_fault_exit(vcpu, fault);
if (fault->is_private)
return kvm_faultin_pfn_private(vcpu, fault);Maybe kvm_fault_is_private() can be simplified based on what direction we end up taking WRT ongoing discussions like whether we decide to define KVM_X86_{SNP,TDX}_VM vm_types in addition to the KVM_X86_PROTECTED_VM type that the selftests uses, but hoping that for this path, any changes along that line can be encapsulated away in kvm_fault_is_private() without any/much further churn at the various call-sites like __kvm_faultin_pfn().
We could even push all the above logic down into the KVM self-tests, but have:
bool kvm_fault_is_private(kvm, gpa, err) {
return KVM_FAULT_VMM_DEFINED;
}And that would be enough to run self-tests as standalone series, with TDX/SNP should filling in kvm_fault_is_private() with their platform-specific handling.
Does that seem reasonable to you? At least as a starting point.
-Mike
Quoted 4 lines
Thanks,
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH RFC v9 04/51] KVM: x86: Determine shared/private faults using a configurable mask
Quoted 235 lines · Michael Roth
Quoted 130 lines · Isaku Yamahata
Quoted 75 lines · Michael Roth
Quoted 63 lines · Isaku Yamahata
Quoted 46 lines · Michael Roth
This will be used to determine whether or not an #NPF should be serviced using a normal page vs. a guarded/gmem one.
Signed-off-by Michael Roth <[email protected]>--- arch/x86/include/asm/kvm_host.h | 7 +++++++ arch/x86/kvm/mmu/mmu_internal.h | 35 ++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-)arch/x86/include/asm/kvm_host.h +7
-0@@ -1445,6 +1445,13 @@ struct kvm_arch {*/ #define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1)struct kvm_mmu_memory_cache split_desc_cache; + +/* +* When set, used to determine whether a fault should be treated as +* private in the context of protected VMs which use a separate gmem +* pool to back private guest pages. +*/ +u64 mmu_private_fault_mask; };struct kvm_vm_stat {arch/x86/kvm/mmu/mmu_internal.h +11
-0@@ -252,6 +252,39 @@ struct kvm_page_fault {int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault); +static bool kvm_mmu_fault_is_private (struct kvm *kvm,gpa_t gpa,u64 err) +{ +struct kvm_memory_slot *slot; +bool private_fault = false; +gfn_t gfn =gpa_to_gfn (gpa); + + slot =gfn_to_memslot (kvm, gfn); +if (!slot) { +pr_debug ("%s: no slot, GFN: 0x%llx\n" , __func__, gfn); + goto out; + }arch/x86/kvm/mmu/mmu_internal.h struct kvm_page_fault { · line 275
Quoted 10 lines
arch/x86/kvm/mmu/mmu_internal.h +10
-0+ +if (!kvm_slot_can_be_private (slot)) { +pr_debug ("%s: slot is not private, GFN: 0x%llx\n" , __func__, gfn); + goto out; + } + +if (kvm->arch.mmu_private_fault_mask) { + private_fault = !!(err & kvm->arch.mmu_private_fault_mask); + goto out; + }What's the convention of err? Can we abstract it by introducing a new bit
PFERR_PRIVATE_MASK? The caller sets it based on arch specific value. the logic will beQuoted 1 line
.is_private = err & PFERR_PRIVATE_MASK;I'm not sure I understand the question. 'err' is just the page fault flags, and arch.mmu_private_fault_mask is something that can be set on a per-platform basis when running in a mode where shared/private access is recorded in the page fault flags during a #NPF.
I'm not sure how we'd keep the handling cross-platform by moving to a macro, since TDX uses a different bit, and we'd want to be able to build a SNP+TDX kernel that could run on either type of hardware.
Quoted 7 lines
Are you suggesting to reverse that and have err be set in a platform-specific way and then use a common
PFERR_PRIVATE_MASKthat's software-defined and consistent across platforms? That could work, but existing handling seems to use page fault flags as-is, keeping the hardware-set values, rather than modifying them to pass additional metadata, so it seems like it might make things more confusing to make an exception to that here. Or are there other cases where it's done that way?I meant the latter, making
PFERR_PRIVATE_MASKcommon software-defined.I think the SVM fault handler can use hardware value directly by carefully defining those PFERR values.
TDX doesn't have architectural bit in error code to indicate the private fault. It's coded in faulted address as shared bit. GPA bit 51 or 47. PFERR_{USER, WRITE, FETCH, PRESENT} are already software-defined value for VMX (and TDX). The fault handler for VMX,
handle_ept_violation(), converts encoding. For TDX,PFERR_PRIVATE_MASKis just one more software defined bit.I'm fine with either way, variable or macro. Which do you prefer?
- Define variable
mmu_private_fault_mask(global or per struct kvm) The module initialization code,hardware_setup(), setsmmu_private_fault_mask.
- Define the software defined value,PFERR_PRIVATE_MASK.
The caller ofkvm_mmu_page_fault()parses the hardware value and construct software definederror_code.
- any other?Quoted 19 lines
Quoted 10 lines
arch/x86/kvm/mmu/mmu_internal.h struct kvm_page_fault { · line 281
Quoted 6 lines
arch/x86/kvm/mmu/mmu_internal.h +6
-0+ +/* +* Handling below is for UPM self-tests and guests that treat userspace +* as the authority on whether a fault should be private or not. +*/ + private_fault =kvm_mem_is_private (kvm, gpa >>PAGE_SHIFT );This code path is sad. One extra slot lookup and xarray look up. Without mmu lock, the result can change by other vcpu. Let's find a better way.
The intention was to rely on
fault->mmu_seqto determine if aKVM_SET_MEMORY_ATTRIBUTESupdate came in after .private_fault was set so that fault handling could be retried, but that doesn't happen untilkvm_faultin_pfn()which is *after* this is logged. So yes, I think there is a race here, and the approach you took in your Misc. series of keeping thekvm_mem_is_private()check insidekvm_faultin_pfn()is more efficient/correct.Quoted 3 lines
If we can figure out a way to handle checking the fault flags in a way that works for both TDX/SNP (and KVM self-test use-case) we can consolidate around that.
Quoted 7 lines
I can think of the following ways. I think the second option is better because we don't need exit bit for error code.
- Introduce software defined error code
- Add a flags to structkvm_archfor self-test use-caseVM_TYPE_PROTECTED_VM. Set it to true forVM_TYPE_PROTECTED_VMcase.
- any other?Vishal: hoping to get your thoughts here as well from the perspective of the KVM self-test use-case.
I was thinking that once we set
fault->is_private, that sort of becomes our "software-defined" bit, and what KVM would use from that point forward to determine whether or not the access should be treated as a private one or not, and that whatever handler setsfault->is_privatewould encapsulate away all the platform-specific bit-checking needed to do that.So if we were to straight-forwardly implement that based on how TDX currently handles checking for the shared bit in GPA, paired with how SEV-SNP handles checking for private bit in fault flags, it would look something like:
bool kvm_fault_is_private(kvm, gpa, err) { /* SEV-SNP handling */ if (kvm->arch.mmu_private_fault_mask) return !!(err & arch.mmu_private_fault_mask);/* TDX handling */ if (kvm->arch.gfn_shared_mask) return !!(gpa & arch.gfn_shared_mask); return false;}
kvm_mmu_do_page_fault(vcpu, gpa, err, ...) { struct kvm_page_fault fault = { ... .is_private = kvm_fault_is_private(vcpu->kvm, gpa, err) };...}
And then arch.mmu_private_fault_mask and arch.gfn_shared_mask would be set per-KVM-instance, just like they are now with current SNP and TDX patchsets, since stuff like KVM self-test wouldn't be setting those masks, so it makes sense to do it per-instance in that regard.
But that still gets a little awkward for the KVM self-test use-case where .is_private should sort of be ignored in favor of whatever the xarray reports via
kvm_mem_is_private(). In your Misc. series I believe you handled this by introducing aPFERR_HASATTR_MASKbit so we can determine whether existing value offault->is_privateshould be ignored/overwritten or not.So maybe
kvm_fault_is_private()needs to return an integer value instead, like:enum { KVM_FAULT_VMM_DEFINED, KVM_FAULT_SHARED, KVM_FAULT_PRIVATE, } bool kvm_fault_is_private(kvm, gpa, err) { /* SEV-SNP handling */ if (kvm->arch.mmu_private_fault_mask) (err & arch.mmu_private_fault_mask) ? KVM_FAULT_PRIVATE : KVM_FAULT_SHARED/* TDX handling */ if (kvm->arch.gfn_shared_mask) (gpa & arch.gfn_shared_mask) ? KVM_FAULT_SHARED : KVM_FAULT_PRIVATE return KVM_FAULT_VMM_DEFINED;}
And then down in
__kvm_faultin_pfn()we do:if (fault->is_private == KVM_FAULT_VMM_DEFINED) fault->is_private = kvm_mem_is_private(vcpu->kvm, fault->gfn); else if (fault->is_private != kvm_mem_is_private(vcpu->kvm, fault->gfn)) return kvm_do_memory_fault_exit(vcpu, fault); if (fault->is_private) return kvm_faultin_pfn_private(vcpu, fault);Maybe
kvm_fault_is_private()can be simplified based on what direction we end up taking WRT ongoing discussions like whether we decide to define KVM_X86_{SNP,TDX}_VMvm_typesin addition to theKVM_X86_PROTECTED_VMtype that the selftests uses, but hoping that for this path, any changes along that line can be encapsulated away inkvm_fault_is_private()without any/much further churn at the various call-sites like__kvm_faultin_pfn().We could even push all the above logic down into the KVM self-tests, but have:
bool kvm_fault_is_private(kvm, gpa, err) { return KVM_FAULT_VMM_DEFINED; }
Quoted 5 lines
And that would be enough to run self-tests as standalone series, with TDX/SNP should filling in
kvm_fault_is_private()with their platform-specific handling.Does that seem reasonable to you? At least as a starting point.
I tried to move the logic to the caller site and hide it from KVM MMU code. You'd like to move the logic down into KVM MMU side and make the difference explicit. Either way works for me. Let me respin my patches for the direction you propose.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH RFC v9 04/51] KVM: x86: Determine shared/private faults using a configurable mask
In reply to Michael RothQuoted 13 lines
So if we were to straight-forwardly implement that based on how TDX currently handles checking for the shared bit in GPA, paired with how SEV-SNP handles checking for private bit in fault flags, it would look something like:
bool kvm_fault_is_private(kvm, gpa, err) { /* SEV-SNP handling */ if (kvm->arch.mmu_private_fault_mask) return !!(err & arch.mmu_private_fault_mask);
Quoted 3 lines
/* TDX handling */
if (kvm->arch.gfn_shared_mask)
return !!(gpa & arch.gfn_shared_mask);The logic of the two are identical. I think they need to be converged.
Either SEV-SNP should convert the error code private bit to the gfn_shared_mask, or TDX's shared bit should be converted to some private error bit.
Perhaps converting SEV-SNP makes more sense because if I recall correctly SEV guest also has a C-bit, correct?
Or, ...
Quoted 9 lines
return false;}
kvm_mmu_do_page_fault(vcpu, gpa, err, ...) { struct kvm_page_fault fault = { ... .is_private = kvm_fault_is_private(vcpu->kvm, gpa, err)
... should we do something like:
.is_private = static_call(kvm_x86_fault_is_private)(vcpu->kvm, gpa, err);
?
Quoted 5 lines
}; ...}
Quoted 9 lines
And then arch.mmu_private_fault_mask and arch.gfn_shared_mask would be set per-KVM-instance, just like they are now with current SNP and TDX patchsets, since stuff like KVM self-test wouldn't be setting those masks, so it makes sense to do it per-instance in that regard.
But that still gets a little awkward for the KVM self-test use-case where .is_private should sort of be ignored in favor of whatever the xarray reports via
kvm_mem_is_private().
I must have missed something. Why does KVM self-test have impact to how does KVM handles private fault?
Quoted 29 lines
In your Misc. series I believe you
handled this by introducing aPFERR_HASATTR_MASKbit so we can determine whether existing value offault->is_privateshould be ignored/overwritten or not.So maybe
kvm_fault_is_private()needs to return an integer value instead, like:enum { KVM_FAULT_VMM_DEFINED, KVM_FAULT_SHARED, KVM_FAULT_PRIVATE, } bool kvm_fault_is_private(kvm, gpa, err) { /* SEV-SNP handling */ if (kvm->arch.mmu_private_fault_mask) (err & arch.mmu_private_fault_mask) ? KVM_FAULT_PRIVATE : KVM_FAULT_SHARED/* TDX handling */ if (kvm->arch.gfn_shared_mask) (gpa & arch.gfn_shared_mask) ? KVM_FAULT_SHARED : KVM_FAULT_PRIVATE return KVM_FAULT_VMM_DEFINED;}
And then down in
__kvm_faultin_pfn()we do:
Quoted 7 lines
if (fault->is_private == KVM_FAULT_VMM_DEFINED)
fault->is_private = kvm_mem_is_private(vcpu->kvm, fault->gfn);
else if (fault->is_private != kvm_mem_is_private(vcpu->kvm, fault->gfn))
return kvm_do_memory_fault_exit(vcpu, fault);
if (fault->is_private)
return kvm_faultin_pfn_private(vcpu, fault);What does KVM_FAULT_VMM_DEFINED mean, exactly?
Shouldn't the fault type come from _hardware_?
Re: [PATCH RFC v9 04/51] KVM: x86: Determine shared/private faults using a configurable mask
Quoted 13 lines · Kai
Quoted 13 lines
So if we were to straight-forwardly implement that based on how TDX currently handles checking for the shared bit in GPA, paired with how SEV-SNP handles checking for private bit in fault flags, it would look something like:
bool kvm_fault_is_private(kvm, gpa, err) { /* SEV-SNP handling */ if (kvm->arch.mmu_private_fault_mask) return !!(err & arch.mmu_private_fault_mask);
Quoted 5 lines
Quoted 3 lines
/* TDX handling */ if (kvm->arch.gfn_shared_mask) return !!(gpa & arch.gfn_shared_mask);The logic of the two are identical. I think they need to be converged.
I think they're just different enough that trying too hard to converge them might obfuscate things. If the determination didn't come from 2 completely different fields (gpa vs. fault flags) maybe it could be simplified a bit more, but have well-defined open-coded handler that gets called once to set fault->is_private during initial fault time so that that ugliness never needs to be looked at again by KVM MMU seems like a good way to keep things simple through the rest of the handling.
Quoted 3 lines
Either SEV-SNP should convert the error code private bit to the
gfn_shared_mask, or TDX's shared bit should be converted to some private error bit.
struct kvm_page_fault seems to be the preferred way to pass additional params/metadata around, and .is_private field was introduced to track this private/shared state as part of UPM base series:
https://lore.kernel.org/lkml/[email protected]/
So it seems like unecessary complexity to track/encode that state into other additional places rather than just encapsulating it all in fault->is_private (or some similar field), and synthesizing all this platform-specific handling into a well-defined value that's conveyed by something like fault->is_private in a way where KVM MMU doesn't need to worry as much about platform-specific stuff seems like a good thing, and in line with what the UPM base series was trying to do by adding the fault->is_private field.
So all I'm really proposing is that whatever SNP and TDX end up doing should center around setting that fault->is_private field and keeping everything contained there. If there are better ways to handle *how* that's done I don't have any complaints there, but moving/adding bits to GPA/error_flags after fault time just seems unecessary to me when fault->is_private field can serve that purpose just as well.
Quoted 3 lines
Perhaps converting SEV-SNP makes more sense because if I recall correctly SEV guest also has a C-bit, correct?
That's correct, but the C-bit doesn't show in the GPA that gets passed up to KVM during an #NPF, and instead gets encoded into the fault's error_flags.
Quoted 13 lines
Or, ...
Quoted 9 lines
return false;}
kvm_mmu_do_page_fault(vcpu, gpa, err, ...) { struct kvm_page_fault fault = { ... .is_private = kvm_fault_is_private(vcpu->kvm, gpa, err)
Quoted 4 lines
... should we do something like:
.is_private = static_call(kvm_x86_fault_is_private)(vcpu->kvm, gpa, err);
We actually had exactly this in v7 of SNP hypervisor patches:
https://lore.kernel.org/linux-coco/[email protected]/T/#m17841f5bfdfb8350d69d78c6831dd8f3a4748638
but Sean was hoping to avoid a callback, which is why we ended up using a bitmask in this version since it basically all that callback would need to do. It's unfortunately that we don't have a common scheme between SNP/TDX, but maybe that's still possible, I just think that whatever that ends up being, it should live and be contained inside whatever helper ends up setting fault->is_private.
There's some other awkwardness with a callback approach. It sort of ties into your question about selftests so I'll address it below...
Quoted 13 lines
?
Quoted 10 lines
}; ...}
And then arch.mmu_private_fault_mask and arch.gfn_shared_mask would be set per-KVM-instance, just like they are now with current SNP and TDX patchsets, since stuff like KVM self-test wouldn't be setting those masks, so it makes sense to do it per-instance in that regard.
Quoted 7 lines
Quoted 4 lines
But that still gets a little awkward for the KVM self-test use-case where .is_private should sort of be ignored in favor of whatever the xarray reports via
kvm_mem_is_private().I must have missed something. Why does KVM self-test have impact to how does KVM handles private fault?
The self-tests I'm referring to here are the ones from Vishal that shipped with v10 of Chao's UPM/fd-based private memory series, and also as part of Sean's gmem tree:
https://github.com/sean-jc/linux/commit/a0f5f8c911804f55935094ad3a277301704330a6
These exercise gmem/UPM handling without the need for any SNP/TDX hardware support. They do so by "trusting" the shared/private state that the VMM sets via KVM_SET_MEMORY_ATTRIBUTES. So if VMM says it should be private, KVM MMU will treat it as private, so we'd never get a mismatch, so KVM_EXIT_MEMORY_FAULT will never be generated.
Quoted 35 lines
Quoted 35 lines
In your Misc. series I believe you
handled this by introducing aPFERR_HASATTR_MASKbit so we can determine whether existing value offault->is_privateshould be ignored/overwritten or not.So maybe
kvm_fault_is_private()needs to return an integer value instead, like:enum { KVM_FAULT_VMM_DEFINED, KVM_FAULT_SHARED, KVM_FAULT_PRIVATE, } bool kvm_fault_is_private(kvm, gpa, err) { /* SEV-SNP handling */ if (kvm->arch.mmu_private_fault_mask) (err & arch.mmu_private_fault_mask) ? KVM_FAULT_PRIVATE : KVM_FAULT_SHARED/* TDX handling */ if (kvm->arch.gfn_shared_mask) (gpa & arch.gfn_shared_mask) ? KVM_FAULT_SHARED : KVM_FAULT_PRIVATE return KVM_FAULT_VMM_DEFINED;}
And then down in
__kvm_faultin_pfn()we do:if (fault->is_private == KVM_FAULT_VMM_DEFINED) fault->is_private = kvm_mem_is_private(vcpu->kvm, fault->gfn); else if (fault->is_private != kvm_mem_is_private(vcpu->kvm, fault->gfn)) return kvm_do_memory_fault_exit(vcpu, fault);
Quoted 7 lines
Quoted 2 lines
if (fault->is_private) return kvm_faultin_pfn_private(vcpu, fault);What does
KVM_FAULT_VMM_DEFINEDmean, exactly?Shouldn't the fault type come from _hardware_?
In above self-test use-case, there is no reliance on hardware support, and fault->is_private should always be treated as being whatever was set by the VMM via KVM_SET_MEMORY_ATTRIBUTES, so that's why I proposed the KVM_FAULT_VMM_DEFINED value to encode that case into fault->is_private so KVM MMU and handle protected self-test VMs of this sort.
In the future, this protected self-test VMs might become the basis of a new protected VM type where some sort of guest-issued hypercall can be used to set whether a fault should be treated as shared/private, rather than relying on VMM-defined value. There's some current discussion about that here:
https://lore.kernel.org/lkml/[email protected]/T/#me627bed3d9acf73ea882e8baa76dfcb27759c440
Going back to your callback question above, that makes things a little awkward, since kvm_x86_ops is statically defined for both kvm_amd/kvm_intel modules, and either can run these self-tests guests as well as these proposed "non-CC VMs" which rely on enlightened guest kernels instead of TDX/SNPhardware support for managing private/shared access.
So you either need to duplicate the handling for how to determine private/shared for these other types into the kvm_intel/kvm_amd callbacks, or have some way for the callback to say to "fall back to the common handling for self-tests and non-CC VMs". The latter is what we implemented in v8 of this series, but Isaku suggested it was a bit too heavyweight and proposed dropping the fall-back logic in favor of updating the kvm_x86_ops at run-time once we know whether or not it's a TDX/SNP guest:
https://lkml.iu.edu/hypermail/linux/kernel/2303.2/03009.html
which could work, but it still doesn't address Sean's desire to avoid callbacks completely, and still amounts to a somewhat convulated way to hide away TDX/SNP-specific bit checks for shared/private. Rather than hide them away in callbacks that are already frowned upon by maintainer, I think it makes sense to "open-code" all these checks in a common handler like kvm_fault_is_private() to we can make some progress toward a consensus, and then iterate on it from there rather than refining what may already be a dead-end path.
-Mike
Re: [PATCH RFC v9 04/51] KVM: x86: Determine shared/private faults using a configurable mask
Quoted 20 lines · Michael Roth
Quoted 13 lines · Kai
Quoted 13 lines
So if we were to straight-forwardly implement that based on how TDX currently handles checking for the shared bit in GPA, paired with how SEV-SNP handles checking for private bit in fault flags, it would look something like:
bool kvm_fault_is_private(kvm, gpa, err) { /* SEV-SNP handling */ if (kvm->arch.mmu_private_fault_mask) return !!(err & arch.mmu_private_fault_mask);Quoted 5 lines
Quoted 3 lines
/* TDX handling */ if (kvm->arch.gfn_shared_mask) return !!(gpa & arch.gfn_shared_mask);The logic of the two are identical. I think they need to be converged.
Quoted 7 lines
I think they're just different enough that trying too hard to converge them might obfuscate things. If the determination didn't come from 2 completely different fields (gpa vs. fault flags) maybe it could be simplified a bit more, but have well-defined open-coded handler that gets called once to set
fault->is_privateduring initial fault time so that that ugliness never needs to be looked at again by KVM MMU seems like a good way to keep things simple through the rest of the handling.
I actually agree with the second half that is after "but ...", but IIUC it doesn't conflict with converging arch.mmu_private_fault_mask and arch.gfn_shared_mask into one. No?
Quoted 10 lines
Quoted 4 lines
Either SEV-SNP should convert the error code private bit to the
gfn_shared_mask, or TDX's shared bit should be converted to some private error bit.struct
kvm_page_faultseems to be the preferred way to pass additional params/metadata around, and .is_private field was introduced to track this private/shared state as part of UPM base series:
Sure. Agreed.
Quoted 9 lines
So it seems like unecessary complexity to track/encode that state into other additional places rather than just encapsulating it all in
fault->is_private(or some similar field), and synthesizing all this platform-specific handling into a well-defined value that's conveyed by something likefault->is_privatein a way where KVM MMU doesn't need to worry as much about platform-specific stuff seems like a good thing, and in line with what the UPM base series was trying to do by adding thefault->is_privatefield.
Yes we should just set fault->is_private and pass to the rest of the common KVM MMU code.
Quoted 5 lines
So all I'm really proposing is that whatever SNP and TDX end up doing should center around setting that
fault->is_privatefield and keeping everything contained there.
I agree.
Quoted 4 lines
If there are better ways to handle *how*
that's done I don't have any complaints there, but moving/adding bits to GPA/error_flags after fault time just seems unecessary to me whenfault->is_privatefield can serve that purpose just as well.
Perhaps you missed my point. My point is arch.mmu_private_fault_mask and arch.gfn_shared_mask seem redundant because the logic around them are exactly the same. I do believe we should have fault->is_private passing to the common MMU code.
In fact, now I am wondering why we need to have "mmu_private_fault_mask" and "gfn_shared_mask" in _common_ KVM MMU code. We already have enough mechanism in KVM common code:
1) fault->is_private
2) kvm_mmu_page_role.private
3) an Xarray to tell whether a GFN is private or shared
I am not convinced that we need to have "mmu_private_fault_mask" and "gfn_shared_mask" in common KVM MMU code. Instead, they can be in AMD and Intel's vendor code.
Maybe it makes sense to have "gfn_shared_mask" in the KVM common code so that the fault handler can just strip away the "shared bit" at the very beginning (at least for TDX), but for the rest of the time I think we should already have enough infrastructure to handle private/shared mapping.
Btw, one minor issue is, if I recall correctly, for TDX the shared bit must be applied to the GFN for shared mapping in normal EPT. I guess AMD doesn't need that for shared mapping. So "gfn_shared_mask" maybe useful in this case, but w/o it I believe we can also achieve in another way via vendor callback.
Quoted 29 lines
Quoted 4 lines
Perhaps converting SEV-SNP makes more sense because if I recall correctly SEV guest also has a C-bit, correct?
That's correct, but the C-bit doesn't show in the GPA that gets passed up to KVM during an #NPF, and instead gets encoded into the fault's
error_flags.Quoted 13 lines
Or, ...
Quoted 9 lines
return false;}
kvm_mmu_do_page_fault(vcpu, gpa, err, ...) { struct kvm_page_fault fault = { ... .is_private = kvm_fault_is_private(vcpu->kvm, gpa, err)Quoted 4 lines
... should we do something like:
.is_private = static_call(kvm_x86_fault_is_private)(vcpu->kvm, gpa, err);We actually had exactly this in v7 of SNP hypervisor patches:
Quoted 8 lines
https://lore.kernel.org/linux-coco/[email protected]/T/#m17841f5bfdfb8350d69d78c6831dd8f3a4748638
but Sean was hoping to avoid a callback, which is why we ended up using a bitmask in this version since it basically all that callback would need to do. It's unfortunately that we don't have a common scheme between SNP/TDX, but maybe that's still possible, I just think that whatever that ends up being, it should live and be contained inside whatever helper ends up setting
fault->is_private.
Sure. If the function call can be avoided in fault handler then we should.
Quoted 30 lines
There's some other awkwardness with a callback approach. It sort of ties into your question about selftests so I'll address it below...
Quoted 13 lines
?
Quoted 10 lines
}; ...}
And then arch.mmu_private_fault_mask and arch.gfn_shared_mask would be set per-KVM-instance, just like they are now with current SNP and TDX patchsets, since stuff like KVM self-test wouldn't be setting those masks, so it makes sense to do it per-instance in that regard.
Quoted 7 lines
Quoted 4 lines
But that still gets a little awkward for the KVM self-test use-case where .is_private should sort of be ignored in favor of whatever the xarray reports via
kvm_mem_is_private().I must have missed something. Why does KVM self-test have impact to how does KVM handles private fault?
The self-tests I'm referring to here are the ones from Vishal that shipped with v10 of Chao's UPM/fd-based private memory series, and also as part of Sean's gmem tree:
Quoted 7 lines
https://github.com/sean-jc/linux/commit/a0f5f8c911804f55935094ad3a277301704330a6
These exercise gmem/UPM handling without the need for any SNP/TDX hardware support. They do so by "trusting" the shared/private state that the VMM sets via
KVM_SET_MEMORY_ATTRIBUTES. So if VMM says it should be private, KVM MMU will treat it as private, so we'd never get a mismatch, soKVM_EXIT_MEMORY_FAULTwill never be generated.
If KVM supports a test mode, or by reading another thread, KVM wants to support a software-based KVM_X86_PROTECTED_VM and distinguish it with hardware-based confidential VMs such as TDX and SEV-SNP:
then it's fine to handle it when setting up fault->is_private. But my point is KVM self-test itself should not impact how does KVM implement fault handler -- it is KVM itself wants to support additional software-based things.
[...]
(Thanks for explaining additional background).
Re: [PATCH RFC v9 04/51] KVM: x86: Determine shared/private faults using a configurable mask
Quoted 27 lines · Kai
Quoted 4 lines
If there are better ways to handle *how*
that's done I don't have any complaints there, but moving/adding bits to GPA/error_flags after fault time just seems unecessary to me whenfault->is_privatefield can serve that purpose just as well.Perhaps you missed my point. My point is arch.mmu_private_fault_mask and arch.gfn_shared_mask seem redundant because the logic around them are exactly the same. I do believe we should have
fault->is_privatepassing to the common MMU code.In fact, now I am wondering why we need to have "
mmu_private_fault_mask" and "gfn_shared_mask" in _common_ KVM MMU code. We already have enough mechanism in KVM common code:1)
fault->is_private
2) kvm_mmu_page_role.private
3) an Xarray to tell whether a GFN is private or sharedI am not convinced that we need to have "
mmu_private_fault_mask" and "gfn_shared_mask" in common KVM MMU code. Instead, they can be in AMD and Intel's vendor code.Maybe it makes sense to have "
gfn_shared_mask" in the KVM common code so that the fault handler can just strip away the "shared bit" at the very beginning (at least for TDX), but for the rest of the time I think we should already have enough infrastructure to handle private/shared mapping.
Quoted 4 lines
Btw, one minor issue is, if I recall correctly, for TDX the shared bit must be applied to the GFN for shared mapping in normal EPT. I guess AMD doesn't need that for shared mapping. So "
gfn_shared_mask" maybe useful in this case, but w/o it I believe we can also achieve in another way via vendor callback.
"2) kvm_mmu_page_role.private" above has different meaning.
a). The fault is private or not.
b). page table the fault handler is walking is private or conventional.
a.) is common for SNP, TDX and PROTECTED_VM. It makes sense in kvm_mmu_do_page_fault() and __kvm_faultin_pfn(). After kvm_faultin_pfn(), the fault handler can mostly forget it for SNP and PROTECTED_VM. (large page adjustment needs it, though.) This is what we're discussing in this thread.
b.) is specific to TDX. TDX KVM MMU introduces one more page table.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH RFC v9 04/51] KVM: x86: Determine shared/private faults using a configurable mask
Quoted 41 lines · Isaku Yamahata
Quoted 27 lines · Kai
Quoted 4 lines
If there are better ways to handle *how*
that's done I don't have any complaints there, but moving/adding bits to GPA/error_flags after fault time just seems unecessary to me whenfault->is_privatefield can serve that purpose just as well.Perhaps you missed my point. My point is arch.mmu_private_fault_mask and arch.gfn_shared_mask seem redundant because the logic around them are exactly the same. I do believe we should have
fault->is_privatepassing to the common MMU code.In fact, now I am wondering why we need to have "
mmu_private_fault_mask" and "gfn_shared_mask" in _common_ KVM MMU code. We already have enough mechanism in KVM common code:1)
fault->is_private
2) kvm_mmu_page_role.private
3) an Xarray to tell whether a GFN is private or sharedI am not convinced that we need to have "
mmu_private_fault_mask" and "gfn_shared_mask" in common KVM MMU code. Instead, they can be in AMD and Intel's vendor code.Maybe it makes sense to have "
gfn_shared_mask" in the KVM common code so that the fault handler can just strip away the "shared bit" at the very beginning (at least for TDX), but for the rest of the time I think we should already have enough infrastructure to handle private/shared mapping.Quoted 4 lines
Btw, one minor issue is, if I recall correctly, for TDX the shared bit must be applied to the GFN for shared mapping in normal EPT. I guess AMD doesn't need that for shared mapping. So "
gfn_shared_mask" maybe useful in this case, but w/o it I believe we can also achieve in another way via vendor callback."2) kvm_mmu_page_role.private" above has different meaning.
a). The fault is private or not.
b). page table the fault handler is walking is private or conventional.
Quoted 8 lines
a.) is common for SNP, TDX and
PROTECTED_VM. It makes sense inkvm_mmu_do_page_fault()and__kvm_faultin_pfn(). Afterkvm_faultin_pfn(), the fault handler can mostly forget it for SNP andPROTECTED_VM. (large page adjustment needs it, though.) This is what we're discussing in this thread.b.) is specific to TDX. TDX KVM MMU introduces one more page table.
I don't buy the last sentence. Even it's not necessarily for AMD from hardware's perspective, but the concept remains true for AMD too. So why cannot we use it for AMD?
Note "shared_gfn_mask" and kvm_mmu_page_role.private is kinda redundant too, I do believe we should avoid introducing a lot fields to KVM *common* code due to vendor differences (i.e., in this case, "gfn_shared_mask" and "mmu_private_fault_mask").
Re: [PATCH RFC v9 04/51] KVM: x86: Determine shared/private faults using a configurable mask
Quoted 47 lines · Kai
Quoted 46 lines · Isaku Yamahata
Quoted 27 lines · Kai
Quoted 4 lines
If there are better ways to handle *how*
that's done I don't have any complaints there, but moving/adding bits to GPA/error_flags after fault time just seems unecessary to me whenfault->is_privatefield can serve that purpose just as well.Perhaps you missed my point. My point is arch.mmu_private_fault_mask and arch.gfn_shared_mask seem redundant because the logic around them are exactly the same. I do believe we should have
fault->is_privatepassing to the common MMU code.In fact, now I am wondering why we need to have "
mmu_private_fault_mask" and "gfn_shared_mask" in _common_ KVM MMU code. We already have enough mechanism in KVM common code:1)
fault->is_private
2) kvm_mmu_page_role.private
3) an Xarray to tell whether a GFN is private or sharedI am not convinced that we need to have "
mmu_private_fault_mask" and "gfn_shared_mask" in common KVM MMU code. Instead, they can be in AMD and Intel's vendor code.Maybe it makes sense to have "
gfn_shared_mask" in the KVM common code so that the fault handler can just strip away the "shared bit" at the very beginning (at least for TDX), but for the rest of the time I think we should already have enough infrastructure to handle private/shared mapping.Quoted 4 lines
Btw, one minor issue is, if I recall correctly, for TDX the shared bit must be applied to the GFN for shared mapping in normal EPT. I guess AMD doesn't need that for shared mapping. So "
gfn_shared_mask" maybe useful in this case, but w/o it I believe we can also achieve in another way via vendor callback."2) kvm_mmu_page_role.private" above has different meaning.
a). The fault is private or not.
b). page table the fault handler is walking is private or conventional.a.) is common for SNP, TDX and
PROTECTED_VM. It makes sense inkvm_mmu_do_page_fault()and__kvm_faultin_pfn(). Afterkvm_faultin_pfn(), the fault handler can mostly forget it for SNP andPROTECTED_VM. (large page adjustment needs it, though.) This is what we're discussing in this thread.
Quoted 7 lines
Quoted 3 lines
b.) is specific to TDX. TDX KVM MMU introduces one more page table.
I don't buy the last sentence. Even it's not necessarily for AMD from hardware's perspective, but the concept remains true for AMD too. So why cannot we use it for AMD?
We can use it for AMD. Let me rephrase it.
TDX only uses it now. SEV-SNP may or may not use it at their option.
Signature · 1 line
Isaku Yamahata <[email protected]>
Borislav Petkov
maintainerRe: [PATCH RFC v9 04/51] KVM: x86: Determine shared/private faults using a configurable mask
In reply to Michael RothQuoted 14 lines · Michael Roth
This will be used to determine whether or not an #NPF should be serviced using a normal page vs. a guarded/gmem one.
Signed-off-by Michael Roth <[email protected]>--- arch/x86/include/asm/kvm_host.h | 7 +++++++ arch/x86/kvm/mmu/mmu_internal.h | 35 ++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-)arch/x86/include/asm/kvm_host.h +0
-0
arch/x86/include/asm/kvm_host.h struct kvm_arch { · line 1450
Quoted 6 lines
arch/x86/include/asm/kvm_host.h +3
-0@@ -1445,6 +1445,13 @@ struct kvm_arch {*/ #define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1)struct kvm_mmu_memory_cache split_desc_cache; + +/* +* When set, used to determine whether a fault should be treated as
^^^^^^^^
And when not set? Invalid?
I guess so, judging by the code below.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH RFC v9 04/51] KVM: x86: Determine shared/private faults using a configurable mask
Quoted 23 lines · Borislav Petkov
Quoted 14 lines · Michael Roth
This will be used to determine whether or not an #NPF should be serviced using a normal page vs. a guarded/gmem one.
Signed-off-by Michael Roth <[email protected]>--- arch/x86/include/asm/kvm_host.h | 7 +++++++ arch/x86/kvm/mmu/mmu_internal.h | 35 ++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-)arch/x86/include/asm/kvm_host.h +0
-0arch/x86/include/asm/kvm_host.h struct kvm_arch { · line 1450
Quoted 6 lines
arch/x86/include/asm/kvm_host.h +3
-0@@ -1445,6 +1445,13 @@ struct kvm_arch {*/ #define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1)struct kvm_mmu_memory_cache split_desc_cache; + +/* +* When set, used to determine whether a fault should be treated as ^^^^^^^^
Quoted 3 lines
And when not set? Invalid?
I guess so, judging by the code below.
Yes, or more specifically, "When not set, treat the value set by userspace via KVM_SET_MEMORY_ATTRIBUTES as the authority on whether to treat a fault as private or not. In this case, KVM_EXIT_MEMORY_FAULT events won't be generated, so there will never be a mismatch between what hardware indicates via page fault flags vs. what software has assigned via KVM_SET_MEMORY_ATTRIBUTES".
-Mike
Quoted 6 lines
Signature · 5 lines
-- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette
RFC PATCH v9 5/51 x86/coco: move CONFIG_HAS_CC_PLATFORM check down into coco/Makefile
In reply to Michael RothCurrently CONFIG_HAS_CC_PLATFORM is a prereq for building anything in arch/x86/coco, but that is generally only applicable for guest support.
For SEV-SNP, helpers related purely to host support will also live in arch/x86/coco. To allow for CoCo-related host support code in arch/x86/coco, move that check down into the Makefile and check for it specifically when needed.
--- arch/x86/Kbuild | 2 +- arch/x86/coco/Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/Kbuild +1 -1
@@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 -obj-$(CONFIG_ARCH_HAS_CC_PLATFORM) += coco/ +obj-y += coco/ obj-y += entry/
arch/x86/coco/Makefile +1 -1
@@ -3,6 +3,6 @@ CFLAGS_REMOVE_core.o = -pg KASAN_SANITIZE_core.o := n CFLAGS_core.o += -fno-stack-protector -obj-y += core.o +obj-$(CONFIG_ARCH_HAS_CC_PLATFORM) += core.o obj-$(CONFIG_INTEL_TDX_GUEST) += tdx/
Signature · 1 line
2.25.1
Re: [PATCH RFC v9 05/51] x86/coco: move CONFIG_HAS_CC_PLATFORM check down into coco/Makefile
Quoted 7 lines · Michael Roth
Currently
CONFIG_HAS_CC_PLATFORMis a prereq for building anything inarch/x86/coco, but that is generally only applicable for guest support.For SEV-SNP, helpers related purely to host support will also live in
arch/x86/coco. To allow for CoCo-related host support code inarch/x86/coco, move that check down into theMakefileand check for it specifically when needed.
Hm. TDX host support uses arch/x86/virt/vmx/tdx/. I think we need to be consistent here.
IIRC, Borislav proposed the scheme that TDX uses.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Borislav Petkov
maintainerRe: [PATCH RFC v9 05/51] x86/coco: move CONFIG_HAS_CC_PLATFORM check down into coco/Makefile
In reply to Michael RothQuoted 1 line · Michael Roth
Currently
CONFIG_HAS_CC_PLATFORMis a prereq for building anything in
^^^^^^
Use proper english words pls.
Quoted 6 lines
arch/x86/coco, but that is generally only applicable for guest support.For SEV-SNP, helpers related purely to host support will also live in
arch/x86/coco. To allow for CoCo-related host support code inarch/x86/coco, move that check down into theMakefileand check for it specifically when needed.
I have no clue what that means. Example?
The last time we talked about paths, we ended up agreeing on:
https://lore.kernel.org/all/[email protected]/
So your "helpers related purely to host support" should go to
arch/x86/virt/svm/sev*.c
And just to keep it simple, that should be
arch/x86/virt/svm/sev.c
and if there's real need to split that, we can do that later.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH RFC v9 05/51] x86/coco: move CONFIG_HAS_CC_PLATFORM check down into coco/Makefile
Quoted 6 lines · Borislav Petkov
Quoted 1 line · Michael Roth
Currently
CONFIG_HAS_CC_PLATFORMis a prereq for building anything in^^^^^^Use proper english words pls.
Quoted 8 lines
Quoted 6 lines
arch/x86/coco, but that is generally only applicable for guest support.For SEV-SNP, helpers related purely to host support will also live in
arch/x86/coco. To allow for CoCo-related host support code inarch/x86/coco, move that check down into theMakefileand check for it specifically when needed.I have no clue what that means. Example?
Basically, arch/x86/coco/Makefile is never processed if arch/x86/Kbuild indicates that CONFIG_HAS_CC_PLATFORM is not set. So if we want to have stuff in arch/x86/coco/Makefile that build for !CONFIG_HAS_CC_PLATFORM, like SNP host support, which does not rely on CONFIG_HAS_CC_PLATFORM being set, that check needs to be moved down into arch/x86/coco/Makefile.
Quoted 7 lines
The last time we talked about paths, we ended up agreeing on:
https://lore.kernel.org/all/[email protected]/
So your "helpers related purely to host support" should go to
Quoted 7 lines
arch/x86/virt/svm/sev*.cAnd just to keep it simple, that should be
arch/x86/virt/svm/sev.cand if there's real need to split that, we can do that later.
Ok, makes sense.
Thanks,
Mike
Quoted 8 lines
Thx.
Signature · 4 lines
Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette
Borislav Petkov
maintainerRe: [PATCH RFC v9 05/51] x86/coco: move CONFIG_HAS_CC_PLATFORM check down into coco/Makefile
Quoted 5 lines · Michael Roth
Basically,
arch/x86/coco/Makefileis never processed ifarch/x86/Kbuildindicates thatCONFIG_HAS_CC_PLATFORMis not set. So if we want to have stuff inarch/x86/coco/Makefilethat build for !CONFIG_HAS_CC_PLATFORM, like SNP host support, which does not rely onCONFIG_HAS_CC_PLATFORMbeing set, that check needs to be moved down intoarch/x86/coco/Makefile.
Ok, so if you put SNP host support into arch/x86/virt/svm/sev.c, that should work too and won't have any relation to CONFIG_HAS_CC_PLATFORM, right?
The CC_PLATFORM thing is a way to check for confidential computing guest features by abstracting the capabilities so that you don't have to check *each* and *every* conf guest type in the conditionals and thus go nuts.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH RFC v9 05/51] x86/coco: move CONFIG_HAS_CC_PLATFORM check down into coco/Makefile
Quoted 10 lines · Borislav Petkov
Quoted 5 lines · Michael Roth
Basically,
arch/x86/coco/Makefileis never processed ifarch/x86/Kbuildindicates thatCONFIG_HAS_CC_PLATFORMis not set. So if we want to have stuff inarch/x86/coco/Makefilethat build for !CONFIG_HAS_CC_PLATFORM, like SNP host support, which does not rely onCONFIG_HAS_CC_PLATFORMbeing set, that check needs to be moved down intoarch/x86/coco/Makefile.Ok, so if you put SNP host support into
arch/x86/virt/svm/sev.c, that should work too and won't have any relation toCONFIG_HAS_CC_PLATFORM, right?
Right, that works out just as well, and ends up being a bit more straightforward. I have it implemented here:
https://github.com/mdroth/linux/commits/snp-host-latest-v9b
https://github.com/mdroth/linux/commit/a889a2dd64b62d9c3bf74cf02e7d8d71c7061667
and dropped the patch that reworks arch/x86/coco/Makefile.
Thanks,
Mike
Quoted 12 lines
The
CC_PLATFORMthing is a way to check for confidential computing guest features by abstracting the capabilities so that you don't have to check *each* and *every* conf guest type in the conditionals and thus go nuts.Thx.
Signature · 4 lines
Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette
Re: [PATCH RFC v9 05/51] x86/coco: move CONFIG_HAS_CC_PLATFORM check down into coco/Makefile
In reply to Michael RothHi,
Quoted 6 lines · Michael Roth
Currently
CONFIG_HAS_CC_PLATFORMis a prereq for building anything inarch/x86/coco, but that is generally only applicable for guest support.> For SEV-SNP, helpers related purely to host support will also live inarch/x86/coco. To allow for CoCo-related host support code inarch/x86/coco, move that check down into theMakefileand check for it specifically when needed.
I think CONFIG_HAS_CC_PLATFORM is not meant to be guest specific (otherwise, we could have named it CONFIG_HAS_CC_GUEST). Will it create any issue if we enable it in host?
Quoted 32 lines
Cc Kirill A. Shutemov <[email protected]>Suggested-by Tom Lendacky <[email protected]>Signed-off-by Michael Roth <[email protected]>--- arch/x86/Kbuild | 2 +- arch/x86/coco/Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)arch/x86/Kbuild +1
-1@@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 -obj-$(CONFIG_ARCH_HAS_CC_PLATFORM) += coco/ +obj-y += coco/ obj-y += entry/arch/x86/coco/Makefile +1
-1@@ -3,6 +3,6 @@ CFLAGS_REMOVE_core.o = -pg KASAN_SANITIZE_core.o := n CFLAGS_core.o += -fno-stack-protector -obj-y += core.o +obj-$(CONFIG_ARCH_HAS_CC_PLATFORM) += core.o obj-$(CONFIG_INTEL_TDX_GUEST) += tdx/
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Tom Lendacky
maintainerRe: [PATCH RFC v9 05/51] x86/coco: move CONFIG_HAS_CC_PLATFORM check down into coco/Makefile
Quoted 11 lines · Sathyanarayanan Kuppuswamy
Hi,
Quoted 6 lines · Michael Roth
Currently
CONFIG_HAS_CC_PLATFORMis a prereq for building anything inarch/x86/coco, but that is generally only applicable for guest support.> For SEV-SNP, helpers related purely to host support will also live inarch/x86/coco. To allow for CoCo-related host support code inarch/x86/coco, move that check down into theMakefileand check for it specifically when needed.
Quoted 1 line
I think
CONFIG_HAS_CC_PLATFORMis not meant to be guest specific (otherwise,
Correct, it is used in bare-metal for SME support, so that needs to continue to work.
Thanks,
Tom
Quoted 36 lines
we could have named it
CONFIG_HAS_CC_GUEST). Will it create any issue if we enable it in host?Quoted 32 lines
Cc Kirill A. Shutemov <[email protected]>Suggested-by Tom Lendacky <[email protected]>Signed-off-by Michael Roth <[email protected]>--- arch/x86/Kbuild | 2 +- arch/x86/coco/Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)arch/x86/Kbuild +1
-1@@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 -obj-$(CONFIG_ARCH_HAS_CC_PLATFORM) += coco/ +obj-y += coco/ obj-y += entry/arch/x86/coco/Makefile +1
-1@@ -3,6 +3,6 @@ CFLAGS_REMOVE_core.o = -pg KASAN_SANITIZE_core.o := n CFLAGS_core.o += -fno-stack-protector -obj-y += core.o +obj-$(CONFIG_ARCH_HAS_CC_PLATFORM) += core.o obj-$(CONFIG_INTEL_TDX_GUEST) += tdx/
RFC PATCH v9 6/51 x86/cpufeatures: Add SEV-SNP CPU feature
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
Add CPU feature detection for Secure Encrypted Virtualization with Secure Nested Paging. This feature adds a strong memory integrity protection to help prevent malicious hypervisor-based attacks like data replay, memory re-mapping, and more.
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/kernel/cpu/amd.c | 5 +++-- tools/arch/x86/include/asm/cpufeatures.h | 1 + 3 files changed, 5 insertions(+), 2 deletions(-)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -431,6 +431,7 @@#define X86_FEATURE_SEV (19*32+ 1)/* AMD Secure Encrypted Virtualization */ #define X86_FEATURE_VM_PAGE_FLUSH (19*32+ 2)/* "" VM Page Flush MSR is supported */ #define X86_FEATURE_SEV_ES (19*32+ 3)/* AMD Secure Encrypted Virtualization - Encrypted State */ +#define X86_FEATURE_SEV_SNP (19*32+ 4)/* AMD Secure Encrypted Virtualization - Secure Nested Paging */ #define X86_FEATURE_V_TSC_AUX (19*32+ 9)/* "" Virtual TSC_AUX */ #define X86_FEATURE_SME_COHERENT (19*32+10)/* "" AMD hardware-enforced cache coherency */
arch/x86/kernel/cpu/amd.c +3 -2
@@ -558,8 +558,8 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c)* SME feature (set in scattered.c). * If the kernel has not enabled SME via any means then * don't advertise the SME feature. -* For SEV: If BIOS has not enabled SEV then don't advertise the -* SEV and SEV_ES feature (set in scattered.c). +* For SEV: If BIOS has not enabled SEV then don't advertise SEV and +* any additional functionality based on it. * * In all cases, since support for SME and SEV requires long mode, * don't advertise the feature under CONFIG_X86_32.
@@ -594,6 +594,7 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) clear_sev:setup_clear_cpu_cap (X86_FEATURE_SEV );setup_clear_cpu_cap (X86_FEATURE_SEV_ES ); +setup_clear_cpu_cap (X86_FEATURE_SEV_SNP ); } }
tools/arch/x86/include/asm/cpufeatures.h +1 -0
@@ -424,6 +424,7 @@#define X86_FEATURE_SEV (19*32+ 1)/* AMD Secure Encrypted Virtualization */ #define X86_FEATURE_VM_PAGE_FLUSH (19*32+ 2)/* "" VM Page Flush MSR is supported */ #define X86_FEATURE_SEV_ES (19*32+ 3)/* AMD Secure Encrypted Virtualization - Encrypted State */ +#define X86_FEATURE_SEV_SNP (19*32+ 4)/* AMD Secure Encrypted Virtualization - Secure Nested Paging */ #define X86_FEATURE_V_TSC_AUX (19*32+ 9)/* "" Virtual TSC_AUX */ #define X86_FEATURE_SME_COHERENT (19*32+10)/* "" AMD hardware-enforced cache coherency */
Signature · 1 line
2.25.1
RFC PATCH v9 7/51 x86/sev: Add the host SEV-SNP initialization support
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
The memory integrity guarantees of SEV-SNP are enforced through a new structure called the Reverse Map Table (RMP). The RMP is a single data structure shared across the system that contains one entry for every 4K page of DRAM that may be used by SEV-SNP VMs. APM2 section 15.36 details a number of steps needed to detect/enable SEV-SNP and RMP table support on the host:
- Detect SEV-SNP support based on CPUID bit
- Initialize the RMP table memory reported by the RMP base/end MSR registers and configure IOMMU to be compatible with RMP access restrictions
- Set the MtrrFixDramModEn bit in SYSCFG MSR
- Set the SecureNestedPagingEn and VMPLEn bits in the SYSCFG MSR
- Configure IOMMU
RMP table entry format is non-architectural and it can vary by processor. It is defined by the PPR. Restrict SNP support to CPU models/families which are compatible with the current RMP table entry format to guard against any undefined behavior when running on other system types. Future models/support will handle this through an architectural mechanism to allow for broader compatibility.
SNP host code depends on CONFIG_KVM_AMD_SEV config flag, which may be enabled even when CONFIG_AMD_MEM_ENCRYPT isn't set, so update the SNP-specific IOMMU helpers used here to rely on CONFIG_KVM_AMD_SEV instead of CONFIG_AMD_MEM_ENCRYPT.
[mdr: rework commit message to be clearer about what patch does, squash
in early_rmptable_check() handling from Tom]
--- arch/x86/coco/Makefile | 1 + arch/x86/coco/sev/Makefile | 3 + arch/x86/coco/sev/host.c | 212 +++++++++++++++++++++++ arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/msr-index.h | 11 +- arch/x86/include/asm/sev.h | 2 + arch/x86/kernel/cpu/amd.c | 19 ++ drivers/iommu/amd/init.c | 2 +- include/linux/amd-iommu.h | 2 +- 9 files changed, 256 insertions(+), 4 deletions(-) create mode 100644 arch/x86/coco/sev/Makefile create mode 100644 arch/x86/coco/sev/host.c
arch/x86/coco/Makefile +1 -0
@@ -6,3 +6,4 @@ CFLAGS_core.o += -fno-stack-protector obj-$(CONFIG_ARCH_HAS_CC_PLATFORM) += core.o obj-$(CONFIG_INTEL_TDX_GUEST) += tdx/ +obj-$(CONFIG_KVM_AMD_SEV) += sev/
arch/x86/coco/sev/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-y += host.o
arch/x86/coco/sev/host.c new file +212 -0
212 lines not on this page · raw message
arch/x86/include/asm/disabled-features.h +7 -1
@@ -99,6 +99,12 @@# define DISABLE_TDX_GUEST (1 << (X86_FEATURE_TDX_GUEST & 31))#endif +#ifdef CONFIG_KVM_AMD_SEV +# define DISABLE_SEV_SNP 0 +#else +# define DISABLE_SEV_SNP (1 << (X86_FEATURE_SEV_SNP & 31)) +#endif +/* * Make sure to add features to the correct mask */
@@ -123,7 +129,7 @@ DISABLE_ENQCMD)#define DISABLED_MASK17 0#define DISABLED_MASK18 0 -#define DISABLED_MASK19 0 +#define DISABLED_MASK19 (DISABLE_SEV_SNP)#define DISABLED_MASK20 0#define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 21)
arch/x86/include/asm/msr-index.h +10 -1
@@ -574,6 +574,8 @@#define MSR_AMD64_SEV_ENABLED BIT_ULL(MSR_AMD64_SEV_ENABLED_BIT)#define MSR_AMD64_SEV_ES_ENABLED BIT_ULL(MSR_AMD64_SEV_ES_ENABLED_BIT)#define MSR_AMD64_SEV_SNP_ENABLED BIT_ULL(MSR_AMD64_SEV_SNP_ENABLED_BIT) +#define MSR_AMD64_RMP_BASE 0xc0010132 +#define MSR_AMD64_RMP_END 0xc0010133/* SNP feature bits enabled by the hypervisor */ #define MSR_AMD64_SNP_VTOM BIT_ULL(3)
@@ -675,7 +677,14 @@#define MSR_K8_TOP_MEM2 0xc001001d#define MSR_AMD64_SYSCFG 0xc0010010#define MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT 23 -#define MSR_AMD64_SYSCFG_MEM_ENCRYPT BIT_ULL(MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT) +#define MSR_AMD64_SYSCFG_MEM_ENCRYPT BIT_ULL(MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT) +#define MSR_AMD64_SYSCFG_SNP_EN_BIT 24 +#define MSR_AMD64_SYSCFG_SNP_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_EN_BIT) +#define MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT 25 +#define MSR_AMD64_SYSCFG_SNP_VMPL_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT) +#define MSR_AMD64_SYSCFG_MFDM_BIT 19 +#define MSR_AMD64_SYSCFG_MFDM BIT_ULL(MSR_AMD64_SYSCFG_MFDM_BIT) +#define MSR_K8_INT_PENDING_MSG 0xc0010055/* C1E active bits in int pending message */ #define K8_INTP_C1E_ACTIVE_MASK 0x18000000
arch/x86/include/asm/sev.h +2 -0
@@ -197,6 +197,7 @@ void snp_set_wakeup_secondary_cpu(void);bool snp_init (struct boot_params *bp);void __init __noreturn snp_abort (void );int snp_issue_guest_request (u64 exit_code,struct snp_req_data *input,unsigned long *fw_err); +bool snp_get_rmptable_info (u64 *start,u64 *len);#else static inline void sev_es_ist_enter (struct pt_regs *regs) { }static inline void sev_es_ist_exit (void ) { }
@@ -221,6 +222,7 @@ static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *in {return -ENOTTY ; } +static inline bool snp_get_rmptable_info (u64 *start,u64 *len) {return false; }#endif #endif
arch/x86/kernel/cpu/amd.c +19 -0
@@ -20,6 +20,7 @@#include <asm/delay.h> #include <asm/debugreg.h> #include <asm/resctrl.h> +#include <asm/sev.h> #ifdef CONFIG_X86_64 # include <asm/mmconfig.h>
@@ -546,6 +547,20 @@ static void bsp_init_amd(struct cpuinfo_x86 *c)resctrl_cpu_detect (c); } +static bool early_rmptable_check (void ) +{ +u64 rmp_base, rmp_size; + +/* +* For early BSP initialization, max_pfn won't be set up yet, wait until +* it is set before performing the RMP table calculations. +*/ +if (!max_pfn) +return true; + +return snp_get_rmptable_info (&rmp_base, &rmp_size); +} +static void early_detect_mem_encrypt (struct cpuinfo_x86 *c) {u64 msr;
@@ -587,6 +602,9 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c)if (!(msr &MSR_K7_HWCR_SMMLOCK )) goto clear_sev; +if (cpu_has (c,X86_FEATURE_SEV_SNP ) && !early_rmptable_check ()) + goto clear_snp; +return ; clear_all:
@@ -594,6 +612,7 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) clear_sev:setup_clear_cpu_cap (X86_FEATURE_SEV );setup_clear_cpu_cap (X86_FEATURE_SEV_ES ); +clear_snp:setup_clear_cpu_cap (X86_FEATURE_SEV_SNP ); } }
drivers/iommu/amd/init.c +1 -1
@@ -3665,7 +3665,7 @@ int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64return iommu_pc_get_set_reg (iommu, bank, cntr, fxn, value, true); } -#ifdef CONFIG_AMD_MEM_ENCRYPT +#ifdef CONFIG_KVM_AMD_SEV int amd_iommu_snp_enable (void ) {/*
include/linux/amd-iommu.h +1 -1
@@ -206,7 +206,7 @@ int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value);struct amd_iommu *get_amd_iommu (unsigned int idx); -#ifdef CONFIG_AMD_MEM_ENCRYPT +#ifdef CONFIG_KVM_AMD_SEV int amd_iommu_snp_enable (void );#endif
Signature · 1 line
2.25.1
Re: [PATCH RFC v9 07/51] x86/sev: Add the host SEV-SNP initialization support
arch/x86/coco/sev/host.c line 109
Quoted 8 lines · Michael Roth
arch/x86/coco/sev/host.c new file +8
-0+/* +* Calculate the amount the memory that must be reserved by the BIOS to +* address the whole RAM, including the bookkeeping area. The RMP itself +* must also be covered. +*/ + max_rmp_pfn = max_pfn; +if (PHYS_PFN (rmp_end) > max_pfn) + max_rmp_pfn =PHYS_PFN (rmp_end);
Could you say a little here about how this deals with memory hotplug?
Borislav Petkov
maintainerRe: [PATCH RFC v9 07/51] x86/sev: Add the host SEV-SNP initialization support
Quoted 11 lines · Dave Hansen
arch/x86/coco/sev/host.c line 109
Quoted 8 lines · Michael Roth
arch/x86/coco/sev/host.c new file +8
-0+/* +* Calculate the amount the memory that must be reserved by the BIOS to +* address the whole RAM, including the bookkeeping area. The RMP itself +* must also be covered. +*/ + max_rmp_pfn = max_pfn; +if (PHYS_PFN (rmp_end) > max_pfn) + max_rmp_pfn =PHYS_PFN (rmp_end);Could you say a little here about how this deals with memory hotplug?
Does SNP hw even support memory hotplug?
I think in order to support that, you'd need some special dance because of the RMP table etc...
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH RFC v9 07/51] x86/sev: Add the host SEV-SNP initialization support
Quoted 11 lines · Borislav Petkov
Quoted 10 lines · Dave Hansen
arch/x86/coco/sev/host.c line 109
Quoted 8 lines · Michael Roth
arch/x86/coco/sev/host.c new file +8
-0+/* +* Calculate the amount the memory that must be reserved by the BIOS to +* address the whole RAM, including the bookkeeping area. The RMP itself +* must also be covered. +*/ + max_rmp_pfn = max_pfn; +if (PHYS_PFN (rmp_end) > max_pfn) + max_rmp_pfn =PHYS_PFN (rmp_end);Could you say a little here about how this deals with memory hotplug?
Quoted 4 lines
Does SNP hw even support memory hotplug?
I think in order to support that, you'd need some special dance because of the RMP table etc...
Yep, there's the hardware side and then there are fun nuggets like using mem= and then doing a software-only hot-add later after boot.
Also, if the hardware doesn't support any kind of hotplug, it would be great to point to the place in the spec where it says that.
Borislav Petkov
maintainerRe: [PATCH RFC v9 07/51] x86/sev: Add the host SEV-SNP initialization support
Quoted 2 lines · Dave Hansen
Yep, there's the hardware side and then there are fun nuggets like using mem= and then doing a software-only hot-add later after boot.
Ah, right, Mike, I think we need to check whether mem= has any effect on SNP.
Quoted 2 lines
Also, if the hardware doesn't support any kind of hotplug, it would be great to point to the place in the spec where it says that.
I honestly don't know. But I can't recall ever hearing about hardware memory hotplug so something worth to check too.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerRe: [PATCH RFC v9 07/51] x86/sev: Add the host SEV-SNP initialization support
In reply to Michael RothQuoted 6 lines · Michael Roth
From: Brijesh Singh <[email protected]>
The memory integrity guarantees of SEV-SNP are enforced through a new structure called the Reverse Map Table (RMP). The RMP is a single data structure shared across the system that contains one entry for every 4K page of DRAM that may be used by SEV-SNP VMs. APM2 section 15.36 details
Rather say 'APM v2, section "Secure Nested Paging (SEV-SNP)"' because the numbering is more likely to change than the name in the future. With the name, people can find it faster.
Quoted 11 lines
a number of steps needed to detect/enable SEV-SNP and RMP table support on the host:
- Detect SEV-SNP support based on CPUID bit
- Initialize the RMP table memory reported by the RMP base/end MSR registers and configure IOMMU to be compatible with RMP access restrictions
- Set the MtrrFixDramModEn bit in SYSCFG MSR
- Set the SecureNestedPagingEn and VMPLEn bits in the SYSCFG MSR
- Configure IOMMU
Quoted 6 lines
RMP table entry format is non-architectural and it can vary by processor. It is defined by the PPR. Restrict SNP support to CPU models/families which are compatible with the current RMP table entry format to guard against any undefined behavior when running on other system types. Future models/support will handle this through an architectural mechanism to allow for broader compatibility.
I'm guessing this is all for live migration between SNP hosts. If so, then there will have to be a guest API to handle the differences.
Quoted 4 lines
SNP host code depends on
CONFIG_KVM_AMD_SEVconfig flag, which may be enabled even whenCONFIG_AMD_MEM_ENCRYPTisn't set, so update the SNP-specific IOMMU helpers used here to rely onCONFIG_KVM_AMD_SEVinstead ofCONFIG_AMD_MEM_ENCRYPT.
Does that mean that even on CONFIG_AMD_MEM_ENCRYPT=n kernels, host SNP can function?
Do we even want that?
I'd expect that a host SNP kernel should have SME enabled too even though it is not absolutely necessary.
Quoted 8 lines
Co-developed-by Ashish Kalra <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Co-developed-by Tom Lendacky <[email protected]>Signed-off-by Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>[mdr: rework commit message to be clearer about what patch does, squash
inearly_rmptable_check()handling from Tom]Signed-off-by Michael Roth <[email protected]>
Quoted 13 lines
--- arch/x86/coco/Makefile | 1 + arch/x86/coco/sev/Makefile | 3 + arch/x86/coco/sev/host.c | 212 +++++++++++++++++++++++ arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/msr-index.h | 11 +- arch/x86/include/asm/sev.h | 2 + arch/x86/kernel/cpu/amd.c | 19 ++ drivers/iommu/amd/init.c | 2 +- include/linux/amd-iommu.h | 2 +- 9 files changed, 256 insertions(+), 4 deletions(-) create mode 100644 arch/x86/coco/sev/Makefile create mode 100644 arch/x86/coco/sev/host.c
Ignored review comments here:
https://lore.kernel.org/r/[email protected]
Ignoring this one for now too.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteTom Lendacky
maintainerRe: [PATCH RFC v9 07/51] x86/sev: Add the host SEV-SNP initialization support
Quoted 33 lines · Borislav Petkov
Quoted 6 lines · Michael Roth
From: Brijesh Singh <[email protected]>
The memory integrity guarantees of SEV-SNP are enforced through a new structure called the Reverse Map Table (RMP). The RMP is a single data structure shared across the system that contains one entry for every 4K page of DRAM that may be used by SEV-SNP VMs. APM2 section 15.36 details
Rather say 'APM v2, section "Secure Nested Paging (SEV-SNP)"' because the numbering is more likely to change than the name in the future. With the name, people can find it faster.
Quoted 11 lines
a number of steps needed to detect/enable SEV-SNP and RMP table support on the host:
- Detect SEV-SNP support based on CPUID bit
- Initialize the RMP table memory reported by the RMP base/end MSR registers and configure IOMMU to be compatible with RMP access restrictions
- Set the MtrrFixDramModEn bit in SYSCFG MSR
- Set the SecureNestedPagingEn and VMPLEn bits in the SYSCFG MSR
- Configure IOMMUQuoted 6 lines
RMP table entry format is non-architectural and it can vary by processor. It is defined by the PPR. Restrict SNP support to CPU models/families which are compatible with the current RMP table entry format to guard against any undefined behavior when running on other system types. Future models/support will handle this through an architectural mechanism to allow for broader compatibility.
I'm guessing this is all for live migration between SNP hosts. If so, then there will have to be a guest API to handle the differences.
Quoted 7 lines
Quoted 4 lines
SNP host code depends on
CONFIG_KVM_AMD_SEVconfig flag, which may be enabled even whenCONFIG_AMD_MEM_ENCRYPTisn't set, so update the SNP-specific IOMMU helpers used here to rely onCONFIG_KVM_AMD_SEVinstead ofCONFIG_AMD_MEM_ENCRYPT.Does that mean that even on
CONFIG_AMD_MEM_ENCRYPT=nkernels, host SNP can function?
Yes, because CONFIG_AMD_MEM_ENCRYPT is mainly for dealing with the encryption bit.
Quoted 2 lines
Do we even want that?
We support that today with SEV and SEV-ES guests. The host/hypervisor kernel does not need CONFIG_AMD_MEM_ENCRYPT=y in order to run SEV guests.
Quoted 3 lines
I'd expect that a host SNP kernel should have SME enabled too even though it is not absolutely necessary.
I recommend using TSME over SME.
Thanks,
Tom
Quoted 29 lines
Quoted 20 lines
Co-developed-by Ashish Kalra <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Co-developed-by Tom Lendacky <[email protected]>Signed-off-by Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>[mdr: rework commit message to be clearer about what patch does, squash
inearly_rmptable_check()handling from Tom]Signed-off-by Michael Roth <[email protected]>--- arch/x86/coco/Makefile | 1 + arch/x86/coco/sev/Makefile | 3 + arch/x86/coco/sev/host.c | 212 +++++++++++++++++++++++ arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/msr-index.h | 11 +- arch/x86/include/asm/sev.h | 2 + arch/x86/kernel/cpu/amd.c | 19 ++ drivers/iommu/amd/init.c | 2 +- include/linux/amd-iommu.h | 2 +- 9 files changed, 256 insertions(+), 4 deletions(-)Quoted 2 lines
create mode 100644
arch/x86/coco/sev/Makefile
create mode 100644arch/x86/coco/sev/host.cIgnored review comments here:
https://lore.kernel.org/r/[email protected]
Ignoring this one for now too.
Kalra, Ashish
maintainerRe: [PATCH RFC v9 07/51] x86/sev: Add the host SEV-SNP initialization support
In reply to Borislav PetkovHello Boris,
Quoted 30 lines · Borislav Petkov
Quoted 6 lines · Michael Roth
From: Brijesh Singh <[email protected]>
The memory integrity guarantees of SEV-SNP are enforced through a new structure called the Reverse Map Table (RMP). The RMP is a single data structure shared across the system that contains one entry for every 4K page of DRAM that may be used by SEV-SNP VMs. APM2 section 15.36 details
Rather say 'APM v2, section "Secure Nested Paging (SEV-SNP)"' because the numbering is more likely to change than the name in the future. With the name, people can find it faster.
Quoted 11 lines
a number of steps needed to detect/enable SEV-SNP and RMP table support on the host:
- Detect SEV-SNP support based on CPUID bit
- Initialize the RMP table memory reported by the RMP base/end MSR registers and configure IOMMU to be compatible with RMP access restrictions
- Set the MtrrFixDramModEn bit in SYSCFG MSR
- Set the SecureNestedPagingEn and VMPLEn bits in the SYSCFG MSR
- Configure IOMMUQuoted 6 lines
RMP table entry format is non-architectural and it can vary by processor. It is defined by the PPR. Restrict SNP support to CPU models/families which are compatible with the current RMP table entry format to guard against any undefined behavior when running on other system types. Future models/support will handle this through an architectural mechanism to allow for broader compatibility.
Quoted 2 lines
I'm guessing this is all for live migration between SNP hosts. If so, then there will have to be a guest API to handle the differences.
This is basically for the RMP table entry format/structure definition inarch/x86/coco/sev/host.c, as this is non-architectural it is defined in a .c file instead of a header file, so that the structure remains private (and restricted to that file) to the SNP host code and not exposed to the rest of the kernel.
As mentioned in the comments above, future CPU models may support RMP table accesses in an architectural way.
Quoted 9 lines
Quoted 5 lines
SNP host code depends on
CONFIG_KVM_AMD_SEVconfig flag, which may be enabled even whenCONFIG_AMD_MEM_ENCRYPTisn't set, so update the SNP-specific IOMMU helpers used here to rely onCONFIG_KVM_AMD_SEVinstead ofCONFIG_AMD_MEM_ENCRYPT.Does that mean that even on
CONFIG_AMD_MEM_ENCRYPT=nkernels, host SNP can function?
Yes, host SNP is supposed to function with CONFIG_AMD_MEM_ENCRYPT=n.
CONFIG_AMD_MEM_ENCRYPT=y is needed for SNP guest.
Quoted 4 lines
Do we even want that?
I'd expect that a host SNP kernel should have SME enabled too even though it is not absolutely necessary.
Yes, we typically test host SNP kernel with SME enabled.
Thanks,
Ashish
Quoted 29 lines
Quoted 20 lines
Co-developed-by Ashish Kalra <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Co-developed-by Tom Lendacky <[email protected]>Signed-off-by Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>[mdr: rework commit message to be clearer about what patch does, squash
inearly_rmptable_check()handling from Tom]Signed-off-by Michael Roth <[email protected]>--- arch/x86/coco/Makefile | 1 + arch/x86/coco/sev/Makefile | 3 + arch/x86/coco/sev/host.c | 212 +++++++++++++++++++++++ arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/msr-index.h | 11 +- arch/x86/include/asm/sev.h | 2 + arch/x86/kernel/cpu/amd.c | 19 ++ drivers/iommu/amd/init.c | 2 +- include/linux/amd-iommu.h | 2 +- 9 files changed, 256 insertions(+), 4 deletions(-)Quoted 2 lines
create mode 100644
arch/x86/coco/sev/Makefile
create mode 100644arch/x86/coco/sev/host.cIgnored review comments here:
https://lore.kernel.org/r/[email protected]
Ignoring this one for now too.
Re: [PATCH RFC v9 07/51] x86/sev: Add the host SEV-SNP initialization support
In reply to Michael RothQuoted 382 lines · Michael Roth
From: Brijesh Singh <[email protected]>
The memory integrity guarantees of SEV-SNP are enforced through a new structure called the Reverse Map Table (RMP). The RMP is a single data structure shared across the system that contains one entry for every 4K page of DRAM that may be used by SEV-SNP VMs. APM2 section 15.36 details a number of steps needed to detect/enable SEV-SNP and RMP table support on the host:
- Detect SEV-SNP support based on CPUID bit
- Initialize the RMP table memory reported by the RMP base/end MSR registers and configure IOMMU to be compatible with RMP access restrictions
- Set the MtrrFixDramModEn bit in SYSCFG MSR
- Set the SecureNestedPagingEn and VMPLEn bits in the SYSCFG MSR
- Configure IOMMURMP table entry format is non-architectural and it can vary by processor. It is defined by the PPR. Restrict SNP support to CPU models/families which are compatible with the current RMP table entry format to guard against any undefined behavior when running on other system types. Future models/support will handle this through an architectural mechanism to allow for broader compatibility.
SNP host code depends on
CONFIG_KVM_AMD_SEVconfig flag, which may be enabled even whenCONFIG_AMD_MEM_ENCRYPTisn't set, so update the SNP-specific IOMMU helpers used here to rely onCONFIG_KVM_AMD_SEVinstead ofCONFIG_AMD_MEM_ENCRYPT.Co-developed-by Ashish Kalra <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Co-developed-by Tom Lendacky <[email protected]>Signed-off-by Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>[mdr: rework commit message to be clearer about what patch does, squash
inearly_rmptable_check()handling from Tom]Signed-off-by Michael Roth <[email protected]>--- arch/x86/coco/Makefile | 1 + arch/x86/coco/sev/Makefile | 3 + arch/x86/coco/sev/host.c | 212 +++++++++++++++++++++++ arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/msr-index.h | 11 +- arch/x86/include/asm/sev.h | 2 + arch/x86/kernel/cpu/amd.c | 19 ++ drivers/iommu/amd/init.c | 2 +- include/linux/amd-iommu.h | 2 +- 9 files changed, 256 insertions(+), 4 deletions(-) create mode 100644 arch/x86/coco/sev/Makefile create mode 100644 arch/x86/coco/sev/host.carch/x86/coco/Makefile +1
-0@@ -6,3 +6,4 @@ CFLAGS_core.o += -fno-stack-protector obj-$(CONFIG_ARCH_HAS_CC_PLATFORM) += core.o obj-$(CONFIG_INTEL_TDX_GUEST) += tdx/ +obj-$(CONFIG_KVM_AMD_SEV) += sev/arch/x86/coco/sev/Makefile new file +3
-0@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-y += host.oarch/x86/coco/sev/host.c new file +212
-0@@ -0,0 +1,212 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* AMD SVM-SEV Host Support. +* +* Copyright (C) 2023 Advanced Micro Devices, Inc. +* +* Author: Ashish Kalra <[email protected]> +* +*/ + +#include <linux/cc_platform.h> +#include <linux/printk.h> +#include <linux/mm_types.h> +#include <linux/set_memory.h> +#include <linux/memblock.h> +#include <linux/kernel.h> +#include <linux/mm.h> +#include <linux/cpumask.h> +#include <linux/iommu.h> +#include <linux/amd-iommu.h> + +#include <asm/sev.h> +#include <asm/processor.h> +#include <asm/setup.h> +#include <asm/svm.h> +#include <asm/smp.h> +#include <asm/cpu.h> +#include <asm/apic.h> +#include <asm/cpuid.h> +#include <asm/cmdline.h> +#include <asm/iommu.h> + +/* +* The first 16KB from the RMP_BASE is used by the processor for the +* bookkeeping, the range needs to be added during the RMP entry lookup. +*/ +#define RMPTABLE_CPU_BOOKKEEPING_SZ 0x4000 + +static unsigned long rmptable_start__ro_after_init ; +static unsigned long rmptable_end__ro_after_init ; + +#undef pr_fmt +#define pr_fmt (fmt) "SEV-SNP: " fmt + +static int __mfd_enable (unsigned int cpu) +{ +u64 val; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return 0 ; + +rdmsrl (MSR_AMD64_SYSCFG , val); + + val |=MSR_AMD64_SYSCFG_MFDM ; + +wrmsrl (MSR_AMD64_SYSCFG , val); + +return 0 ; +} + +static __init void mfd_enable (void *arg) +{ +__mfd_enable (smp_processor_id ()); +} + +static int __snp_enable (unsigned int cpu) +{ +u64 val; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return 0 ; + +rdmsrl (MSR_AMD64_SYSCFG , val); + + val |=MSR_AMD64_SYSCFG_SNP_EN ; + val |=MSR_AMD64_SYSCFG_SNP_VMPL_EN ; + +wrmsrl (MSR_AMD64_SYSCFG , val); + +return 0 ; +} + +static __init void snp_enable (void *arg) +{ +__snp_enable (smp_processor_id ()); +} + +bool snp_get_rmptable_info (u64 *start,u64 *len) +{ +u64 max_rmp_pfn, calc_rmp_sz, rmp_sz, rmp_base, rmp_end; + +rdmsrl (MSR_AMD64_RMP_BASE , rmp_base); +rdmsrl (MSR_AMD64_RMP_END , rmp_end); + +if (!rmp_base || !rmp_end) { +pr_err ("Memory for the RMP table has not been reserved by BIOS\n" ); +return false; + } + + rmp_sz = rmp_end - rmp_base +1 ; + +/* +* Calculate the amount the memory that must be reserved by the BIOS to +* address the whole RAM, including the bookkeeping area. The RMP itself +* must also be covered. +*/ + max_rmp_pfn = max_pfn; +if (PHYS_PFN (rmp_end) > max_pfn) + max_rmp_pfn =PHYS_PFN (rmp_end); + + calc_rmp_sz = (max_rmp_pfn <<4 ) +RMPTABLE_CPU_BOOKKEEPING_SZ ; + +if (calc_rmp_sz > rmp_sz) { +pr_err ("Memory reserved for the RMP table does not cover full system RAM (expected 0x%llx got 0x%llx)\n" , + calc_rmp_sz, rmp_sz); +return false; + } + + *start = rmp_base; + *len = rmp_sz; + +return true; +} + +static __init int __snp_rmptable_init (void ) +{ +u64 rmp_base, sz; +void *start; +u64 val; + +if (!snp_get_rmptable_info (&rmp_base, &sz)) +return 1 ; + +pr_info ("RMP table physical address [0x%016llx - 0x%016llx]\n" , + rmp_base, rmp_base + sz -1 ); + + start =memremap (rmp_base, sz,MEMREMAP_WB ); +if (!start) { +pr_err ("Failed to map RMP table addr 0x%llx size 0x%llx\n" , rmp_base, sz); +return 1 ; + } + +/* +* Check if SEV-SNP is already enabled, this can happen in case of +* kexec boot. +*/ +rdmsrl (MSR_AMD64_SYSCFG , val); +if (val &MSR_AMD64_SYSCFG_SNP_EN ) + goto skip_enable; + +/* Initialize the RMP table to zero */ +memset (start,0 , sz); + +/* Flush the caches to ensure that data is written before SNP is enabled. */ +wbinvd_on_all_cpus (); + +/* MFDM must be enabled on all the CPUs prior to enabling SNP. */ +on_each_cpu (mfd_enable,NULL ,1 ); + +/* Enable SNP on all CPUs. */ +on_each_cpu (snp_enable,NULL ,1 ); + +skip_enable: + rmptable_start = (unsigned long )start; + rmptable_end = rmptable_start + sz -1 ; + +return 0 ; +} + +static int __init snp_rmptable_init (void ) +{ +int family, model; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return 0 ; + + family = boot_cpu_data.x86; + model = boot_cpu_data.x86_model; + +/* +* RMP table entry format is not architectural and it can vary by processor and +* is defined by the per-processor PPR. Restrict SNP support on the known CPU +* model and family for which the RMP table entry format is currently defined for. +*/ +if (!(family ==0x19 && model <=0xaf ) && !(family ==0x1a && model <=0xf )) + goto nosnp; + +if (amd_iommu_snp_enable ()) + goto nosnp; + +if (__snp_rmptable_init ()) + goto nosnp; + +cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"x86/rmptable_init:online" , __snp_enable,NULL ); + +return 0 ; + +nosnp: +setup_clear_cpu_cap (X86_FEATURE_SEV_SNP ); +return -ENOSYS ; +} + +/* +* This must be called after the PCI subsystem. This is because amd_iommu_snp_enable() +* is called to ensure the IOMMU supports the SEV-SNP feature, which can only be +* called after subsys_initcall(). +* +* NOTE: IOMMU is enforced by SNP to ensure that hypervisor cannot program DMA +* directly into guest private memory. In case of SNP, the IOMMU ensures that +* the page(s) used for DMA are hypervisor owned. +*/ +fs_initcall (snp_rmptable_init);arch/x86/include/asm/disabled-features.h +7
-1@@ -99,6 +99,12 @@# define DISABLE_TDX_GUEST (1 << (X86_FEATURE_TDX_GUEST & 31))#endif +#ifdef CONFIG_KVM_AMD_SEV +# define DISABLE_SEV_SNP 0 +#else +# define DISABLE_SEV_SNP (1 << (X86_FEATURE_SEV_SNP & 31)) +#endif +/* * Make sure to add features to the correct mask */ @@ -123,7 +129,7 @@ DISABLE_ENQCMD)#define DISABLED_MASK17 0#define DISABLED_MASK18 0 -#define DISABLED_MASK19 0 +#define DISABLED_MASK19 (DISABLE_SEV_SNP)#define DISABLED_MASK20 0#define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 21)arch/x86/include/asm/msr-index.h +10
-1@@ -574,6 +574,8 @@#define MSR_AMD64_SEV_ENABLED BIT_ULL(MSR_AMD64_SEV_ENABLED_BIT)#define MSR_AMD64_SEV_ES_ENABLED BIT_ULL(MSR_AMD64_SEV_ES_ENABLED_BIT)#define MSR_AMD64_SEV_SNP_ENABLED BIT_ULL(MSR_AMD64_SEV_SNP_ENABLED_BIT) +#define MSR_AMD64_RMP_BASE 0xc0010132 +#define MSR_AMD64_RMP_END 0xc0010133/* SNP feature bits enabled by the hypervisor */ #define MSR_AMD64_SNP_VTOM BIT_ULL(3)@@ -675,7 +677,14 @@#define MSR_K8_TOP_MEM2 0xc001001d#define MSR_AMD64_SYSCFG 0xc0010010#define MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT 23 -#define MSR_AMD64_SYSCFG_MEM_ENCRYPT BIT_ULL(MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT) +#define MSR_AMD64_SYSCFG_MEM_ENCRYPT BIT_ULL(MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT) +#define MSR_AMD64_SYSCFG_SNP_EN_BIT 24 +#define MSR_AMD64_SYSCFG_SNP_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_EN_BIT) +#define MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT 25 +#define MSR_AMD64_SYSCFG_SNP_VMPL_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT) +#define MSR_AMD64_SYSCFG_MFDM_BIT 19 +#define MSR_AMD64_SYSCFG_MFDM BIT_ULL(MSR_AMD64_SYSCFG_MFDM_BIT) +#define MSR_K8_INT_PENDING_MSG 0xc0010055/* C1E active bits in int pending message */ #define K8_INTP_C1E_ACTIVE_MASK 0x18000000arch/x86/include/asm/sev.h +2
-0@@ -197,6 +197,7 @@ void snp_set_wakeup_secondary_cpu(void);bool snp_init (struct boot_params *bp);void __init __noreturn snp_abort (void );int snp_issue_guest_request (u64 exit_code,struct snp_req_data *input,unsigned long *fw_err); +bool snp_get_rmptable_info (u64 *start,u64 *len);#else static inline void sev_es_ist_enter (struct pt_regs *regs) { }static inline void sev_es_ist_exit (void ) { }@@ -221,6 +222,7 @@ static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *in {return -ENOTTY ; } +static inline bool snp_get_rmptable_info (u64 *start,u64 *len) {return false; }#endif #endif arch/x86/kernel/cpu/amd.c +5
-0@@ -20,6 +20,7 @@#include <asm/delay.h> #include <asm/debugreg.h> #include <asm/resctrl.h> +#include <asm/sev.h> #ifdef CONFIG_X86_64 # include <asm/mmconfig.h> @@ -546,6 +547,20 @@ static void bsp_init_amd(struct cpuinfo_x86 *c)resctrl_cpu_detect (c); } +static bool early_rmptable_check (void ) +{ +u64 rmp_base, rmp_size; +
arch/x86/kernel/cpu/amd.c static void bsp_init_amd(struct cpuinfo_x86 *c) · line 563
Quoted 10 lines
arch/x86/kernel/cpu/amd.c +10
-0+/* +* For early BSP initialization, max_pfn won't be set up yet, wait until +* it is set before performing the RMP table calculations. +*/ +if (!max_pfn) +return true; + +return snp_get_rmptable_info (&rmp_base, &rmp_size); +} +
When CONFIG_AMD_MEM_ENCRYPT=y && CONFIG_KVM=n (=> CONFIG_KVM_AMD_SEV=n) this results in an undefined reference to snp_get_rmptable_info when linking this file. The header provides a stub when AMD_MEM_ENCRYPT=n but the definition is only compiled in when KVM_AMD_SEV=y
Jeremi
RFC PATCH v9 8/51 x86/speculation: Do not enable Automatic IBRS if SEV SNP is enabled
In reply to Michael RothFrom: Kim Phillips <[email protected]>
A hardware limitation prevents the host from enabling Automatic IBRS when SNP is enabled. Instead, fall back to retpolines.
--- arch/x86/kernel/cpu/bugs.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/bugs.c +6 -1
@@ -1507,7 +1507,12 @@ static void __init spectre_v2_select_mitigation(void)if (spectre_v2_in_ibrs_mode (mode)) {if (boot_cpu_has (X86_FEATURE_AUTOIBRS )) { -msr_set_bit (MSR_EFER , _EFER_AUTOIBRS); +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) { +msr_set_bit (MSR_EFER , _EFER_AUTOIBRS); + }else { +pr_err ("SNP feature available, not enabling AutoIBRS on the host.\n" ); + mode =spectre_v2_select_retpoline (); + } }else { x86_spec_ctrl_base |=SPEC_CTRL_IBRS ;update_spec_ctrl (x86_spec_ctrl_base);
Signature · 1 line
2.25.1
Re: [PATCH RFC v9 08/51] x86/speculation: Do not enable Automatic IBRS if SEV SNP is enabled
Quoted 2 lines · Michael Roth
A hardware limitation prevents the host from enabling Automatic IBRS when SNP is enabled. Instead, fall back to retpolines.
"Hardware limitation"? As in, it is a documented, architectural restriction? Or, it's a CPU bug?
Quoted 5 lines
arch/x86/kernel/cpu/bugs.c +0
-0
arch/x86/kernel/cpu/bugs.c static void __init spectre_v2_select_mitigation(void) · line 1515
Quoted 10 lines
arch/x86/kernel/cpu/bugs.c +6
-1@@ -1507,7 +1507,12 @@ static void __init spectre_v2_select_mitigation(void)if (spectre_v2_in_ibrs_mode (mode)) {if (boot_cpu_has (X86_FEATURE_AUTOIBRS )) { -msr_set_bit (MSR_EFER , _EFER_AUTOIBRS); +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) { +msr_set_bit (MSR_EFER , _EFER_AUTOIBRS); + }else { +pr_err ("SNP feature available, not enabling AutoIBRS on the host.\n" ); + mode =spectre_v2_select_retpoline (); + }
I think this would be nicer if you did something like:
if (cpu_feature_enabled(X86_FEATURE_SEV_SNP)) setup_clear_cpu_cap(X86_FEATURE_AUTOIBRS);
somewhere _else_ in the code instead of smack-dab in the middle of the mitigation selection.
Re: [PATCH RFC v9 08/51] x86/speculation: Do not enable Automatic IBRS if SEV SNP is enabled
Quoted 6 lines · Dave Hansen
Quoted 2 lines · Michael Roth
A hardware limitation prevents the host from enabling Automatic IBRS when SNP is enabled. Instead, fall back to retpolines.
"Hardware limitation"? As in, it is a documented, architectural restriction? Or, it's a CPU bug?
It's a documented, architectural restriction: When Secure Nested Paging (SEV-SNP) is enabled, processes running as host are protected, but those running with a non-zero ASID, are not.
Quoted 18 lines
Quoted 5 lines
arch/x86/kernel/cpu/bugs.c +0
-0arch/x86/kernel/cpu/bugs.c static void __init spectre_v2_select_mitigation(void) · line 1515
Quoted 10 lines
arch/x86/kernel/cpu/bugs.c +6
-1@@ -1507,7 +1507,12 @@ static void __init spectre_v2_select_mitigation(void)if (spectre_v2_in_ibrs_mode (mode)) {if (boot_cpu_has (X86_FEATURE_AUTOIBRS )) { -msr_set_bit (MSR_EFER , _EFER_AUTOIBRS); +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) { +msr_set_bit (MSR_EFER , _EFER_AUTOIBRS); + }else { +pr_err ("SNP feature available, not enabling AutoIBRS on the host.\n" ); + mode =spectre_v2_select_retpoline (); + }I think this would be nicer if you did something like:
Quoted 5 lines
if (cpu_feature_enabled(X86_FEATURE_SEV_SNP)) setup_clear_cpu_cap(X86_FEATURE_AUTOIBRS);somewhere _else_ in the code instead of smack-dab in the middle of the mitigation selection.
Good idea. How about this?:
From 6cf32e8d8426190b1bf1b1e04ceb35bf0bac784b Mon Sep 17 00:00:00 2001
From: Kim Phillips <[email protected]>
Date: Mon, 17 Jul 2023 14:08:15 -0500
Subject: [PATCH] x86/speculation: Do not enable Automatic IBRS if SEV SNP is
enabled
Automatic IBRS provides protection to [1]:
- Processes running at CPL=0
- Processes running as host when Secure Nested Paging (SEV-SNP) is enabled
i.e.,
(CPL < 3) || ((ASID == 0) && SNP)
Because of this limitation, do not enable Automatic IBRS when SNP is enabled. Instead, fall back to retpolines.
Note that the AutoIBRS feature may continue to be used within the guest.
[1] "AMD64 Architecture Programmer's Manual Volume 2: System Programming", Pub. 24593, rev. 3.41, June 2023, Part 1, Section 3.1.7 "Extended Feature Enable Register (EFER)", Automatic IBRS Enable (AIBRSE) Bit.
--- arch/x86/kernel/cpu/common.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/common.c +2 -1
@@ -1348,7 +1348,8 @@ static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)* AMD's AutoIBRS is equivalent to Intel's eIBRS - use the Intel feature * flag and protect from vendor-specific bugs via the whitelist. */ -if ((ia32_cap &ARCH_CAP_IBRS_ALL ) ||cpu_has (c,X86_FEATURE_AUTOIBRS )) { +if ((ia32_cap &ARCH_CAP_IBRS_ALL ) || (cpu_has (c,X86_FEATURE_AUTOIBRS ) && + !cpu_feature_enabled (X86_FEATURE_SEV_SNP ))) {setup_force_cpu_cap (X86_FEATURE_IBRS_ENHANCED );if (!cpu_matches (cpu_vuln_whitelist,NO_EIBRS_PBRSB ) && !(ia32_cap &ARCH_CAP_PBRSB_NO ))
Signature · 1 line
2.34.1
Re: [PATCH RFC v9 08/51] x86/speculation: Do not enable Automatic IBRS if SEV SNP is enabled
On 7/18/23 15:34, Kim Phillips wrote:
...
Quoted 11 lines
Automatic IBRS provides protection to [1]:
- Processes running at CPL=0
- Processes running as host when Secure Nested Paging (SEV-SNP) is enabledi.e.,
(CPL < 3) || ((ASID == 0) && SNP)
Because of this limitation, do not enable Automatic IBRS when SNP is enabled.
Gah, I found that hard to parse. I think it's because you're talking about an SEV-SNP host in one part and "SNP" in the other but _meaning_ SNP host and SNP guest.
Could I maybe suggest that you folks follow the TDX convention and actually add _GUEST and _HOST to the feature name be explicit about which side is which?
Quoted 1 line
Instead, fall back to retpolines.
Now I'm totally lost.
This is talking about falling back to retpolines ... in the kernel. But "Automatic IBRS provides protection to ... CPL < 3", aka. the kernel.
Quoted 2 lines
Note that the AutoIBRS feature may continue to be used within the guest.
What is this trying to say?
"AutoIBRS can still be used in a guest since it protects CPL < 3"
or
"The AutoIBRS bits can still be twiddled within the guest even though it doesn't do any good"
?
Quoted 15 lines
arch/x86/kernel/cpu/common.c +2
-1@@ -1348,7 +1348,8 @@ static void __init cpu_set_bug_bits(structcpuinfo_x86 *c) * AMD's AutoIBRS is equivalent to Intel's eIBRS - use the Intel feature * flag and protect from vendor-specific bugs via the whitelist. */ - if ((ia32_cap & ARCH_CAP_IBRS_ALL) || cpu_has(c, X86_FEATURE_AUTOIBRS)) { + if ((ia32_cap & ARCH_CAP_IBRS_ALL) || (cpu_has(c, X86_FEATURE_AUTOIBRS) && + !cpu_feature_enabled(X86_FEATURE_SEV_SNP))) { setup_force_cpu_cap(X86_FEATURE_IBRS_ENHANCED);if (!cpu_matches(cpu_vuln_whitelist,
NO_EIBRS_PBRSB) && !(ia32_cap &ARCH_CAP_PBRSB_NO))
Re: [PATCH RFC v9 08/51] x86/speculation: Do not enable Automatic IBRS if SEV SNP is enabled
Quoted 34 lines · Dave Hansen
On 7/18/23 15:34, Kim Phillips wrote:
...Quoted 11 lines
Automatic IBRS provides protection to [1]:
- Processes running at CPL=0
- Processes running as host when Secure Nested Paging (SEV-SNP) is enabledi.e.,
(CPL < 3) || ((ASID == 0) && SNP)
Because of this limitation, do not enable Automatic IBRS when SNP is enabled.
Gah, I found that hard to parse. I think it's because you're talking about an SEV-SNP host in one part and "SNP" in the other but _meaning_ SNP host and SNP guest.
Could I maybe suggest that you folks follow the TDX convention and actually add
_GUESTand_HOSTto the feature name be explicit about which side is which?Quoted 1 line
Instead, fall back to retpolines.
Now I'm totally lost.
This is talking about falling back to retpolines ... in the kernel. But "Automatic IBRS provides protection to ... CPL < 3", aka. the kernel.
Quoted 2 lines
Note that the AutoIBRS feature may continue to be used within the guest.
What is this trying to say?
Quoted 8 lines
"AutoIBRS can still be used in a guest since it protects CPL < 3"
or
"The AutoIBRS bits can still be twiddled within the guest even though it doesn't do any good"
?
Hopefully the commit text in this version will help answer all your questions?:
From 96dbd72d018287bc5b72f6083884e2125c9d09bc Mon Sep 17 00:00:00 2001
From: Kim Phillips <[email protected]>
Date: Mon, 17 Jul 2023 14:08:15 -0500
Subject: [PATCH] x86/speculation: Do not enable Automatic IBRS if SEV SNP is
enabled
Automatic IBRS provides protection to [1]:
- Processes running at CPL=0
- Processes running as host when Secure Nested Paging (SEV-SNP) is enabled
I.e., from the host side (ASID=0, based on host EFER.AutoIBRS) If SYSCFG[SNPEn]=0 then:
IBRS is enabled for supervisor mode (CPL < 3) only
If SYSCFG[SNPEn]=1 then:
IBRS is enabled at all CPLs
From the guest side (ASID!=0, based on guest EFER.AutoIBRS)
IBRS is enabled for supervisor mode (CPL < 3)
Therefore, don't enable Automatic IBRS in host mode if SNP is enabled, because it will penalize user-mode indirect branch performance. Have the kernel fall back to retpolines instead.
Note that the AutoIBRS feature may continue to be used within guests, where ASID != 0.
[1] "AMD64 Architecture Programmer's Manual Volume 2: System Programming", Pub. 24593, rev. 3.41, June 2023, Part 1, Section 3.1.7 "Extended Feature Enable Register (EFER)" - accessible via Link.
--- arch/x86/kernel/cpu/common.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/common.c +2 -1
@@ -1348,7 +1348,8 @@ static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)* AMD's AutoIBRS is equivalent to Intel's eIBRS - use the Intel feature * flag and protect from vendor-specific bugs via the whitelist. */ -if ((ia32_cap &ARCH_CAP_IBRS_ALL ) ||cpu_has (c,X86_FEATURE_AUTOIBRS )) { +if ((ia32_cap &ARCH_CAP_IBRS_ALL ) || (cpu_has (c,X86_FEATURE_AUTOIBRS ) && + !cpu_feature_enabled (X86_FEATURE_SEV_SNP ))) {setup_force_cpu_cap (X86_FEATURE_IBRS_ENHANCED );if (!cpu_matches (cpu_vuln_whitelist,NO_EIBRS_PBRSB ) && !(ia32_cap &ARCH_CAP_PBRSB_NO ))
Signature · 1 line
2.34.1
Re: [PATCH RFC v9 08/51] x86/speculation: Do not enable Automatic IBRS if SEV SNP is enabled
Quoted 2 lines · Kim Phillips
Hopefully the commit text in this version will help answer all your questions?:
To be honest, it didn't really. I kinda feel like I was having the APM contents tossed casually in my direction rather than being provided a fully considered explanation.
Here's what I came up with instead:
Host-side Automatic IBRS has different behavior based on whether SEV-SNP is enabled.
Without SEV-SNP, Automatic IBRS protects only the kernel. But when SEV-SNP is enabled, the Automatic IBRS protection umbrella widens to all host-side code, including userspace. This protection comes at a cost:
reduced userspace indirect branch performance.
To avoid this performance loss, nix using Automatic IBRS on SEV-SNP hosts. Fall back to retpolines instead.
=====
Is that about right?
I don't think any chit-chat about the guest side is even relevant.
This also absolutely needs a comment. Perhaps just pull the code up to the top level of the function and do this:
/* * Automatic IBRS imposes unacceptable overhead on host * userspace for SEV-SNP systems. Zap it instead. */ if (cpu_feature_enabled(X86_FEATURE_SEV_SNP)) setup_clear_cpu_cap(X86_FEATURE_AUTOIBRS);
BTW, I assume you've grumbled to folks about this. It's an awful shame the hardware (or ucode) was built this was. It's just throwing Automatic IBRS out the window because it's not architected in a nice way.
Is there any plan to improve this?
Re: [PATCH RFC v9 08/51] x86/speculation: Do not enable Automatic IBRS if SEV SNP is enabled
Quoted 7 lines · Dave Hansen
Quoted 2 lines · Kim Phillips
Hopefully the commit text in this version will help answer all your questions?:
To be honest, it didn't really. I kinda feel like I was having the APM contents tossed casually in my direction rather than being provided a fully considered explanation.
Sorry to hear that, it wasn't the intention.
Quoted 10 lines
Here's what I came up with instead:
Host-side Automatic IBRS has different behavior based on whether SEV-SNP is enabled.
Without SEV-SNP, Automatic IBRS protects only the kernel. But when SEV-SNP is enabled, the Automatic IBRS protection umbrella widens to all host-side code, including userspace. This protection comes at a cost:
reduced userspace indirect branch performance.
Quoted 6 lines
To avoid this performance loss, nix using Automatic IBRS on SEV-SNP hosts. Fall back to retpolines instead.
=====
Is that about right?
Sure, see new version below.
Quoted 11 lines
I don't think any chit-chat about the guest side is even relevant.
This also absolutely needs a comment. Perhaps just pull the code up to the top level of the function and do this:
/* * Automatic IBRS imposes unacceptable overhead on host * userspace for SEV-SNP systems. Zap it instead. */ if (cpu_feature_enabled(X86_FEATURE_SEV_SNP)) setup_clear_cpu_cap(X86_FEATURE_AUTOIBRS);
Clearing X86_FEATURE_AUTOIBRS won't work because it'll unnecessarily prohibit KVM from subsequently advertising the feature to guests.
I've tried to address the comment comment, see below.
Quoted 5 lines
BTW, I assume you've grumbled to folks about this. It's an awful shame the hardware (or ucode) was built this was. It's just throwing Automatic IBRS out the window because it's not architected in a nice way.
Is there any plan to improve this?
Sure. Until then:
From fb55df544ed066a3c8fdb1581932a23c03ce6d2c Mon Sep 17 00:00:00 2001
From: Kim Phillips <[email protected]>
Date: Mon, 17 Jul 2023 14:08:15 -0500
Subject: [PATCH] x86/speculation: Don't enable Automatic IBRS if SEV SNP is
enabled
Host-side Automatic IBRS has a different behaviour depending on whether SEV-SNP is enabled.
Without SEV-SNP, Automatic IBRS protects only the kernel. But when SEV-SNP is enabled, the Automatic IBRS protection umbrella widens to all host-side code, including userspace. This protection comes at a cost:
reduced userspace indirect branch performance.
To avoid this performance loss, don't use Automatic IBRS on SEV-SNP hosts. Fall back to retpolines instead.
--- arch/x86/kernel/cpu/common.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/common.c +4 -1
@@ -1347,8 +1347,11 @@ static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)/* * AMD's AutoIBRS is equivalent to Intel's eIBRS - use the Intel feature * flag and protect from vendor-specific bugs via the whitelist. +* Don't use AutoIBRS when SNP is enabled because it degrades host +* userspace indirect branch performance. */ -if ((ia32_cap &ARCH_CAP_IBRS_ALL ) ||cpu_has (c,X86_FEATURE_AUTOIBRS )) { +if ((ia32_cap &ARCH_CAP_IBRS_ALL ) || (cpu_has (c,X86_FEATURE_AUTOIBRS ) && + !cpu_feature_enabled (X86_FEATURE_SEV_SNP ))) {setup_force_cpu_cap (X86_FEATURE_IBRS_ENHANCED );if (!cpu_matches (cpu_vuln_whitelist,NO_EIBRS_PBRSB ) && !(ia32_cap &ARCH_CAP_PBRSB_NO ))
Signature · 1 line
2.34.1
RFC PATCH v9 9/51 x86/sev: Add RMP entry lookup helpers
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
The snp_lookup_page_in_rmptable() can be used by the host to read the RMP entry for a given page. The RMP entry format is documented in AMD PPR, see
https://bugzilla.kernel.org/attachment.cgi?id=296015.
--- arch/x86/coco/sev/host.c | 85 +++++++++++++++++++++++++++++++ arch/x86/include/asm/sev-common.h | 4 ++ arch/x86/include/asm/sev-host.h | 22 ++++++++ arch/x86/include/asm/sev.h | 3 -- 4 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 arch/x86/include/asm/sev-host.h
arch/x86/coco/sev/host.c +85 -0
@@ -30,11 +30,36 @@#include <asm/cmdline.h> #include <asm/iommu.h> +/* +* The RMP entry format is not architectural. The format is defined in PPR +* Family 19h Model 01h, Rev B1 processor. +*/ +struct rmpentry { +union { +struct { +u64 assigned :1 , + pagesize :1 , + immutable :1 , + rsvd1 :9 , + gpa :39 , + asid :10 , + vmsa :1 , + validated :1 , + rsvd2 :1 ; + } info; +u64 low; + }; +u64 high; +} __packed; +/* * The first 16KB from the RMP_BASE is used by the processor for the * bookkeeping, the range needs to be added during the RMP entry lookup. */ #define RMPTABLE_CPU_BOOKKEEPING_SZ 0x4000 +#define RMPENTRY_SHIFT 8 +#define rmptable_page_offset (x) (RMPTABLE_CPU_BOOKKEEPING_SZ + \ + (((unsigned long)x) >> RMPENTRY_SHIFT))static unsigned long rmptable_start__ro_after_init ;static unsigned long rmptable_end__ro_after_init ;
@@ -210,3 +235,63 @@ static int __init snp_rmptable_init(void)* the page(s) used for DMA are hypervisor owned. */ fs_initcall (snp_rmptable_init); + +static inline unsigned int rmpentry_assigned (const struct rmpentry *e) +{ +return e->info.assigned; +} + +static inline unsigned int rmpentry_pagesize (const struct rmpentry *e) +{ +return e->info.pagesize; +} + +static int rmptable_entry (unsigned long paddr,struct rmpentry *entry) +{ +unsigned long vaddr; + + vaddr = rmptable_start +rmptable_page_offset (paddr); +if (unlikely (vaddr > rmptable_end)) +return -EFAULT ; + + *entry = *(struct rmpentry *)vaddr; + +return 0 ; +} + +static int __snp_lookup_rmpentry (u64 pfn,struct rmpentry *entry,int *level) +{ +unsigned long paddr = pfn <<PAGE_SHIFT ; +struct rmpentry large_entry; +int ret; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return -ENXIO ; + + ret =rmptable_entry (paddr, entry); +if (ret) +return ret; + +/* Read a large RMP entry to get the correct page level used in RMP entry. */ + ret =rmptable_entry (paddr &PMD_MASK , &large_entry); +if (ret) +return ret; + + *level =RMP_TO_X86_PG_LEVEL (rmpentry_pagesize (&large_entry)); + +return 0 ; +} + +int snp_lookup_rmpentry (u64 pfn,bool *assigned,int *level) +{ +struct rmpentry e; +int ret; + + ret =__snp_lookup_rmpentry (pfn, &e, level); +if (ret) +return ret; + + *assigned = !!rmpentry_assigned (&e); +return 0 ; +} +EXPORT_SYMBOL_GPL (snp_lookup_rmpentry);
arch/x86/include/asm/sev-common.h +4 -0
@@ -171,4 +171,8 @@ struct snp_psc_desc {#define GHCB_ERR_INVALID_INPUT 5#define GHCB_ERR_INVALID_EVENT 6 +/* RMP page size */ +#define RMP_PG_SIZE_4K 0 +#define RMP_TO_X86_PG_LEVEL (level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) +#endif
arch/x86/include/asm/sev-host.h new file +22 -0
@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* AMD SVM-SEV Host Support. +* +* Copyright (C) 2023 Advanced Micro Devices, Inc. +* +* Author: Ashish Kalra <[email protected]> +* +*/ + +#ifndef __ASM_X86_SEV_HOST_H +#define __ASM_X86_SEV_HOST_H + +#include <asm/sev-common.h> + +#ifdef CONFIG_KVM_AMD_SEV +int snp_lookup_rmpentry (u64 pfn,bool *assigned,int *level); +#else +static inline int snp_lookup_rmpentry (u64 pfn,bool *assigned,int *level) {return 0 ; } +#endif + +#endif
arch/x86/include/asm/sev.h +0 -3
@@ -81,9 +81,6 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs);/* Software defined (when rFlags.CF = 1) */ #define PVALIDATE_FAIL_NOUPDATE 255 -/* RMP page size */ -#define RMP_PG_SIZE_4K 0 -#define RMPADJUST_VMSA_PAGE_BIT BIT(16)/* SNP Guest message request */
Signature · 1 line
2.25.1
Re: [PATCH RFC v9 09/51] x86/sev: Add RMP entry lookup helpers
Quoted 11 lines · Michael Roth
arch/x86/coco/sev/host.c +11
-0+/* +* The RMP entry format is not architectural. The format is defined in PPR +* Family 19h Model 01h, Rev B1 processor. +*/ +struct rmpentry { + union { + struct { + u64 assigned : 1, + pagesize : 1, + immutable : 1, + rsvd1 : 9,
arch/x86/coco/sev/host.c line 53
Quoted 10 lines
arch/x86/coco/sev/host.c +10
-0+ gpa : 39, + asid : 10, + vmsa : 1, + validated : 1, + rsvd2 : 1; + } info; +u64 low; + }; +u64 high; +} __packed;
What's 'high' used for? The PPR says it's reserved. Why not call it reserved?
It _looks_ like it's only used for a debugging pr_info(). It makes the struct look kinda goofy. I'd much rather limit the goofiness to the "dumping" code, like:
u64 *__e = (void *)e;
....
pr_info("RMPEntry paddr 0x%llx: [high=0x%016llx low=0x%016llx]\n",
pfn << PAGE_SHIFT, __e[0], __e[1]);BTW, why does it do any good to dump all these reserved fields?
arch/x86/coco/sev/host.c line 62
Quoted 8 lines
/*
* The first 16KB from theRMP_BASEis used by the processor for the
* bookkeeping, the range needs to be added during the RMP entry lookup. */
#defineRMPTABLE_CPU_BOOKKEEPING_SZ0x4000arch/x86/coco/sev/host.c +3
-0+#define RMPENTRY_SHIFT 8 +#define rmptable_page_offset (x) (RMPTABLE_CPU_BOOKKEEPING_SZ + \ + (((unsigned long)x) >> RMPENTRY_SHIFT))
Is RMPENTRY_SHIFT just log2(sizeof(struct rmpentry))? If so, wouldn't this be a *LOT* more obvious to be written:
unsigned long rmptable_offset(unsigned long paddr)
{
return RMPTABLE_CPU_BOOKKEEPING_SZ +
paddr / sizeof(struct rmpentry);
}??
It removes a cast, gives 'x' a real name, removes a random magic number #define and is clearer to boot.
Quoted 6 lines
static unsigned long
rmptable_start__ro_after_init; static unsigned longrmptable_end__ro_after_init;arch/x86/coco/sev/host.c +0
-0@@ -210,3 +235,63 @@ static int __init snp_rmptable_init(void)* the page(s) used for DMA are hypervisor owned. */ fs_initcall (snp_rmptable_init);
arch/x86/coco/sev/host.c static int __init snp_rmptable_init(void) · line 247
Quoted 10 lines
arch/x86/coco/sev/host.c +10
-0+ +static inline unsigned int rmpentry_assigned (const struct rmpentry *e) +{ +return e->info.assigned; +} + +static inline unsigned int rmpentry_pagesize (const struct rmpentry *e) +{ +return e->info.pagesize; +}
I think these are a little silly. If you're going to go to the trouble of using bitfields, you don't need helpers for every bitfield. I say either use bitfields without helpers or just regular old:
#define RMPENTRY_ASSIGNED_MASK BIT(1)
and then *maybe* you can make helpers for them.
arch/x86/coco/sev/host.c static int __init snp_rmptable_init(void) · line 255
Quoted 7 lines
arch/x86/coco/sev/host.c +7
-0+static int rmptable_entry (unsigned long paddr,struct rmpentry *entry) +{ +unsigned long vaddr; + + vaddr = rmptable_start +rmptable_page_offset (paddr); +if (unlikely (vaddr > rmptable_end)) +return -EFAULT ;
This seems like more of a WARN_ON_ONCE() kind of thing than just an error return. Isn't it a big deal if an invalid (non-RMP-covered) address makes it in here?
arch/x86/coco/sev/host.c static int __init snp_rmptable_init(void) · line 263
Quoted 7 lines
arch/x86/coco/sev/host.c +7
-0+ *entry = *(struct rmpentry *)vaddr; + +return 0 ; +} + +static int __snp_lookup_rmpentry (u64 pfn,struct rmpentry *entry,int *level) +{
I've been bitten by pfn vs. paddr quite a few times. I'd really encourage you to add it to the names if you're going to pass them around, like __snp_lookup_rmpentry_pfn().
arch/x86/coco/sev/host.c static int __init snp_rmptable_init(void) · line 269
Quoted 6 lines
arch/x86/coco/sev/host.c +6
-0+unsigned long paddr = pfn <<PAGE_SHIFT ; +struct rmpentry large_entry; +int ret; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return -ENXIO ;
OK, so if you're running on non-SNP hardware, you return -ENXIO here. Remember this please.
arch/x86/coco/sev/host.c static int __init snp_rmptable_init(void) · line 283
Quoted 13 lines
arch/x86/coco/sev/host.c +13
-0+ ret =rmptable_entry (paddr, entry); +if (ret) +return ret; + +/* Read a large RMP entry to get the correct page level used in RMP entry. */ + ret =rmptable_entry (paddr &PMD_MASK , &large_entry); +if (ret) +return ret; + + *level =RMP_TO_X86_PG_LEVEL (rmpentry_pagesize (&large_entry)); + +return 0 ; +}
This is a bit weird. Should it say something like this?
To do an 4k RMP lookup the hardware looks at two places in the RMP:
1. The actual 4k RMP entry
2. The 2M entry that "covers" the 4k entry
The page size is not indicated in the 4k entries at all. It is solely indicated by the 2M entry.
Quoted 42 lines
arch/x86/coco/sev/host.c +13
-0+int snp_lookup_rmpentry (u64 pfn,bool *assigned,int *level) +{ +struct rmpentry e; +int ret; + + ret =__snp_lookup_rmpentry (pfn, &e, level); +if (ret) +return ret; + + *assigned = !!rmpentry_assigned (&e); +return 0 ; +} +EXPORT_SYMBOL_GPL (snp_lookup_rmpentry);arch/x86/include/asm/sev-common.h +4
-0@@ -171,4 +171,8 @@ struct snp_psc_desc {#define GHCB_ERR_INVALID_INPUT 5#define GHCB_ERR_INVALID_EVENT 6 +/* RMP page size */ +#define RMP_PG_SIZE_4K 0 +#define RMP_TO_X86_PG_LEVEL (level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) +#endif arch/x86/include/asm/sev-host.h new file +10
-0@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* AMD SVM-SEV Host Support. +* +* Copyright (C) 2023 Advanced Micro Devices, Inc. +* +* Author: Ashish Kalra <[email protected]> +* +*/ +
arch/x86/include/asm/sev-host.h line 20
Quoted 10 lines
arch/x86/include/asm/sev-host.h new file +10
-0+#ifndef __ASM_X86_SEV_HOST_H +#define __ASM_X86_SEV_HOST_H + +#include <asm/sev-common.h> + +#ifdef CONFIG_KVM_AMD_SEV +int snp_lookup_rmpentry (u64 pfn,bool *assigned,int *level); +#else +static inline int snp_lookup_rmpentry (u64 pfn,bool *assigned,int *level) {return 0 ; } +#endif
Above, -ENXIO was returned when SEV-SNP was not supported. Here, 0 is returned when it is compiled out. That inconsistent.
Is snp_lookup_rmpentry() acceptable when SEV-SNP is in play? I'd like to see consistency between when it is compiled out and when it is compiled in but unsupported on the CPU.
Quoted 15 lines
arch/x86/include/asm/sev-host.h new file +1
-0+#endif arch/x86/include/asm/sev.h +0
-3@@ -81,9 +81,6 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs);/* Software defined (when rFlags.CF = 1) */ #define PVALIDATE_FAIL_NOUPDATE 255 -/* RMP page size */ -#define RMP_PG_SIZE_4K 0 -#define RMPADJUST_VMSA_PAGE_BIT BIT(16)/* SNP Guest message request */
Re: [PATCH RFC v9 09/51] x86/sev: Add RMP entry lookup helpers
Quoted 30 lines · Dave Hansen
Quoted 11 lines · Michael Roth
arch/x86/coco/sev/host.c +11
-0+/* +* The RMP entry format is not architectural. The format is defined in PPR +* Family 19h Model 01h, Rev B1 processor. +*/ +struct rmpentry { + union { + struct { + u64 assigned : 1, + pagesize : 1, + immutable : 1, + rsvd1 : 9,arch/x86/coco/sev/host.c line 53
Quoted 10 lines
arch/x86/coco/sev/host.c +10
-0+ gpa : 39, + asid : 10, + vmsa : 1, + validated : 1, + rsvd2 : 1; + } info; +u64 low; + }; +u64 high; +} __packed;What's 'high' used for? The PPR says it's reserved. Why not call it reserved?
It _looks_ like it's only used for a debugging
pr_info(). It makes the struct look kinda goofy. I'd much rather limit the goofiness to the "dumping" code, like:
Quoted 7 lines
u64 *__e = (void *)e; .... pr_info("RMPEntry paddr 0x%llx: [high=0x%016llx low=0x%016llx]\n", pfn << PAGE_SHIFT, __e[0], __e[1]);BTW, why does it do any good to dump all these reserved fields?
The reserved bits sometimes contain information that can be useful to pass along to folks on the firmware side, so would definitely be helpful to provide the full raw contents of the RMP entry.
So maybe something like this better captures the intended usage:
struct rmpentry {
union {
struct {
u64 assigned : 1,
pagesize : 1,
immutable : 1,
rsvd1 : 9,
gpa : 39,
asid : 10,
vmsa : 1,
validated : 1,
rsvd2 : 1;
u64 rsvd3;
} info;
u64 data[2];
};
} __packed;But dropping the union and casting to u64[] locally in the debug/dumping routine should work fine as well.
Quoted 18 lines
arch/x86/coco/sev/host.c line 62
Quoted 8 lines
/*
* The first 16KB from theRMP_BASEis used by the processor for the
* bookkeeping, the range needs to be added during the RMP entry lookup. */
#defineRMPTABLE_CPU_BOOKKEEPING_SZ0x4000arch/x86/coco/sev/host.c +3
-0+#define RMPENTRY_SHIFT 8 +#define rmptable_page_offset (x) (RMPTABLE_CPU_BOOKKEEPING_SZ + \ + (((unsigned long)x) >> RMPENTRY_SHIFT))Is
RMPENTRY_SHIFTjust log2(sizeof(struct rmpentry))? If so, wouldn't this be a *LOT* more obvious to be written:unsigned long
rmptable_offset(unsigned long paddr){ return RMPTABLE_CPU_BOOKKEEPING_SZ + paddr / sizeof(struct rmpentry); }
Quoted 4 lines
??
It removes a cast, gives 'x' a real name, removes a random magic number #define and is clearer to boot.
Yes, we ended up reworking this to be an array of struct rmpentry's that are indexed by PFN. That helps to simplify the lookup logic a good bit.
Quoted 22 lines
Quoted 7 lines
static unsigned long
rmptable_start__ro_after_init; static unsigned longrmptable_end__ro_after_init;arch/x86/coco/sev/host.c +0
-0@@ -210,3 +235,63 @@ static int __init snp_rmptable_init(void)* the page(s) used for DMA are hypervisor owned. */ fs_initcall (snp_rmptable_init);arch/x86/coco/sev/host.c static int __init snp_rmptable_init(void) · line 247
Quoted 10 lines
arch/x86/coco/sev/host.c +10
-0+ +static inline unsigned int rmpentry_assigned (const struct rmpentry *e) +{ +return e->info.assigned; +} + +static inline unsigned int rmpentry_pagesize (const struct rmpentry *e) +{ +return e->info.pagesize; +}I think these are a little silly. If you're going to go to the trouble of using bitfields, you don't need helpers for every bitfield. I say either use bitfields without helpers or just regular old:
Quoted 3 lines
#define
RMPENTRY_ASSIGNED_MASKBIT(1)and then *maybe* you can make helpers for them.
Yah, if we have the bitfields it makes sense to use them directly.
Quoted 12 lines
arch/x86/coco/sev/host.c static int __init snp_rmptable_init(void) · line 255
Quoted 8 lines
arch/x86/coco/sev/host.c +7
-0+static int rmptable_entry (unsigned long paddr,struct rmpentry *entry) +{ +unsigned long vaddr; + + vaddr = rmptable_start +rmptable_page_offset (paddr); +if (unlikely (vaddr > rmptable_end)) +return -EFAULT ;This seems like more of a
WARN_ON_ONCE()kind of thing than just an error return. Isn't it a big deal if an invalid (non-RMP-covered) address makes it in here?
Yes, now the lookups are by indexing into an array of struct rmpentry's, that scenario basically the same as an attempted out-of-bounds access, so definitely worth a warning.
Quoted 39 lines
arch/x86/coco/sev/host.c static int __init snp_rmptable_init(void) · line 263
Quoted 8 lines
arch/x86/coco/sev/host.c +7
-0+ *entry = *(struct rmpentry *)vaddr; + +return 0 ; +} + +static int __snp_lookup_rmpentry (u64 pfn,struct rmpentry *entry,int *level) +{I've been bitten by pfn vs. paddr quite a few times. I'd really encourage you to add it to the names if you're going to pass them around, like
__snp_lookup_rmpentry_pfn().arch/x86/coco/sev/host.c static int __init snp_rmptable_init(void) · line 269
Quoted 6 lines
arch/x86/coco/sev/host.c +6
-0+unsigned long paddr = pfn <<PAGE_SHIFT ; +struct rmpentry large_entry; +int ret; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return -ENXIO ;OK, so if you're running on non-SNP hardware, you return
-ENXIOhere. Remember this please.arch/x86/coco/sev/host.c static int __init snp_rmptable_init(void) · line 283
Quoted 13 lines
arch/x86/coco/sev/host.c +13
-0+ ret =rmptable_entry (paddr, entry); +if (ret) +return ret; + +/* Read a large RMP entry to get the correct page level used in RMP entry. */ + ret =rmptable_entry (paddr &PMD_MASK , &large_entry); +if (ret) +return ret; + + *level =RMP_TO_X86_PG_LEVEL (rmpentry_pagesize (&large_entry)); + +return 0 ; +}This is a bit weird. Should it say something like this?
Quoted 1 line
To do an 4k RMP lookup the hardware looks at two places in the RMP:
I'd word this as:
"To query all the relevant bit of an 4k RMP entry, the kernel must access
2 entries in the RMP table:"
Because it's possible hardware only looks at the 2M entry for hardware-based lookups, depending on where the access is coming from, or how the memory at the PFN range is mapped.
But otherwise it seems like an accurate description.
Quoted 63 lines
1. The actual 4k RMP entry
2. The 2M entry that "covers" the 4k entryThe page size is not indicated in the 4k entries at all. It is solely indicated by the 2M entry.
Quoted 42 lines
arch/x86/coco/sev/host.c +13
-0+int snp_lookup_rmpentry (u64 pfn,bool *assigned,int *level) +{ +struct rmpentry e; +int ret; + + ret =__snp_lookup_rmpentry (pfn, &e, level); +if (ret) +return ret; + + *assigned = !!rmpentry_assigned (&e); +return 0 ; +} +EXPORT_SYMBOL_GPL (snp_lookup_rmpentry);arch/x86/include/asm/sev-common.h +4
-0@@ -171,4 +171,8 @@ struct snp_psc_desc {#define GHCB_ERR_INVALID_INPUT 5#define GHCB_ERR_INVALID_EVENT 6 +/* RMP page size */ +#define RMP_PG_SIZE_4K 0 +#define RMP_TO_X86_PG_LEVEL (level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) +#endif arch/x86/include/asm/sev-host.h new file +10
-0@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* AMD SVM-SEV Host Support. +* +* Copyright (C) 2023 Advanced Micro Devices, Inc. +* +* Author: Ashish Kalra <[email protected]> +* +*/ +arch/x86/include/asm/sev-host.h line 20
Quoted 10 lines
arch/x86/include/asm/sev-host.h new file +10
-0+#ifndef __ASM_X86_SEV_HOST_H +#define __ASM_X86_SEV_HOST_H + +#include <asm/sev-common.h> + +#ifdef CONFIG_KVM_AMD_SEV +int snp_lookup_rmpentry (u64 pfn,bool *assigned,int *level); +#else +static inline int snp_lookup_rmpentry (u64 pfn,bool *assigned,int *level) {return 0 ; } +#endif Above,
-ENXIOwas returned when SEV-SNP was not supported. Here, 0 is returned when it is compiled out. That inconsistent.
Quoted 3 lines
Is
snp_lookup_rmpentry()acceptable when SEV-SNP is in play? I'd like to see consistency between when it is compiled out and when it is compiled in but unsupported on the CPU.
I really don't think anything in the kernel should be calling snp_lookup_rmpentry(), so I think it makes sense to adoption the -ENXIO convention here and in any other stubs where that applies.
Thanks,
Mike
Quoted 17 lines
Quoted 16 lines
arch/x86/include/asm/sev-host.h new file +1
-0+#endif arch/x86/include/asm/sev.h +0
-3@@ -81,9 +81,6 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs);/* Software defined (when rFlags.CF = 1) */ #define PVALIDATE_FAIL_NOUPDATE 255 -/* RMP page size */ -#define RMP_PG_SIZE_4K 0 -#define RMPADJUST_VMSA_PAGE_BIT BIT(16)/* SNP Guest message request */
Re: [PATCH RFC v9 09/51] x86/sev: Add RMP entry lookup helpers
Quoted 36 lines · Michael Roth
Quoted 35 lines · Dave Hansen
Quoted 11 lines · Michael Roth
arch/x86/coco/sev/host.c +11
-0+/* +* The RMP entry format is not architectural. The format is defined in PPR +* Family 19h Model 01h, Rev B1 processor. +*/ +struct rmpentry { + union { + struct { + u64 assigned : 1, + pagesize : 1, + immutable : 1, + rsvd1 : 9,arch/x86/coco/sev/host.c line 53
Quoted 10 lines
arch/x86/coco/sev/host.c +10
-0+ gpa : 39, + asid : 10, + vmsa : 1, + validated : 1, + rsvd2 : 1; + } info; +u64 low; + }; +u64 high; +} __packed;What's 'high' used for? The PPR says it's reserved. Why not call it reserved?
It _looks_ like it's only used for a debugging
pr_info(). It makes the struct look kinda goofy. I'd much rather limit the goofiness to the "dumping" code, like:u64 *__e = (void *)e; .... pr_info("RMPEntry paddr 0x%llx: [high=0x%016llx low=0x%016llx]\n", pfn << PAGE_SHIFT, __e[0], __e[1]);
Quoted 6 lines
Quoted 2 lines
BTW, why does it do any good to dump all these reserved fields?
The reserved bits sometimes contain information that can be useful to pass along to folks on the firmware side, so would definitely be helpful to provide the full raw contents of the RMP entry.
Ahh, OK. Could you include a comment to that effect, please?
Quoted 20 lines
So maybe something like this better captures the intended usage:
struct rmpentry { union { struct { u64 assigned : 1, pagesize : 1, immutable : 1, rsvd1 : 9, gpa : 39, asid : 10, vmsa : 1, validated : 1, rsvd2 : 1; u64 rsvd3; } info; u64 data[2]; }; } __packed;
Quoted 2 lines
But dropping the union and casting to u64[] locally in the debug/dumping routine should work fine as well.
Yeah, I'd suggest doing the nasty casting in the debug function. That makes it much more clear what the hardware is doing with the entries. The hardware doesn't treat the struct as 2*u64's at all.
...
Quoted 20 lines
Quoted 16 lines
arch/x86/coco/sev/host.c static int __init snp_rmptable_init(void) · line 283
Quoted 13 lines
arch/x86/coco/sev/host.c +13
-0+ ret =rmptable_entry (paddr, entry); +if (ret) +return ret; + +/* Read a large RMP entry to get the correct page level used in RMP entry. */ + ret =rmptable_entry (paddr &PMD_MASK , &large_entry); +if (ret) +return ret; + + *level =RMP_TO_X86_PG_LEVEL (rmpentry_pagesize (&large_entry)); + +return 0 ; +}This is a bit weird. Should it say something like this?
Quoted 1 line
To do an 4k RMP lookup the hardware looks at two places in the RMP:
I'd word this as:
Quoted 8 lines
"To query all the relevant bit of an 4k RMP entry, the kernel must access
2 entries in the RMP table:"Because it's possible hardware only looks at the 2M entry for hardware-based lookups, depending on where the access is coming from, or how the memory at the PFN range is mapped.
But otherwise it seems like an accurate description.
The wording you suggest is a bit imprecise. For a 2M-aligned 4k page, there is only *one* location, *one* entry.
Also, we're not doing a lookup for an RMP entry. We're doing it for a _pfn_ that results in an RMP entry.
How about this:
/*
* Find the authoritative RMP entry for a PFN. This can be either a 4k
* RMP entry or a special large RMP entry that is authoritative for a
* whole 2M area.
*/
...
Quoted 9 lines
Quoted 9 lines
arch/x86/include/asm/sev-host.h line 20
Quoted 5 lines
arch/x86/include/asm/sev-host.h new file +5
-0+#ifdef CONFIG_KVM_AMD_SEV +int snp_lookup_rmpentry (u64 pfn,bool *assigned,int *level); +#else +static inline int snp_lookup_rmpentry (u64 pfn,bool *assigned,int *level) {return 0 ; } +#endif Above,
-ENXIOwas returned when SEV-SNP was not supported. Here, 0 is returned when it is compiled out. That inconsistent.
Quoted 7 lines
Quoted 3 lines
Is
snp_lookup_rmpentry()acceptable when SEV-SNP is in play? I'd like to see consistency between when it is compiled out and when it is compiled in but unsupported on the CPU.I really don't think anything in the kernel should be calling
snp_lookup_rmpentry(), so I think it makes sense to adoption the-ENXIOconvention here and in any other stubs where that applies.
Sounds good to me. Just please make them consistent.
RFC PATCH v9 10/51 x86/fault: Add helper for dumping RMP entries
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
This information will be useful for debugging things like page faults due to RMP access violations and RMPUPDATE failures.
--- arch/x86/coco/sev/host.c | 43 +++++++++++++++++++++++++++++++++ arch/x86/include/asm/sev-host.h | 2 ++ 2 files changed, 45 insertions(+)
arch/x86/coco/sev/host.c +43 -0
@@ -295,3 +295,46 @@ int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level)return 0 ; }EXPORT_SYMBOL_GPL (snp_lookup_rmpentry); + +void sev_dump_rmpentry (u64 pfn) +{ +unsigned long pfn_end; +struct rmpentry e; +int level, ret; + + ret =__snp_lookup_rmpentry (pfn, &e, &level); +if (ret) { +pr_info ("Failed to read RMP entry for PFN 0x%llx, error %d\n" , pfn, ret); +return ; + } + +if (rmpentry_assigned (&e)) { +pr_info ("RMPEntry paddr 0x%llx: [high=0x%016llx low=0x%016llx]\n" , + pfn <<PAGE_SHIFT , e.high, e.low); +return ; + } + +/* +* If the RMP entry at the faulting pfn was not assigned, then not sure +* what caused the RMP violation. To get some useful debug information, +* iterate through the entire 2MB region, and dump the RMP entries if +* one of the bit in the RMP entry is set. +*/ + pfn = pfn & ~(PTRS_PER_PMD -1 ); + pfn_end = pfn +PTRS_PER_PMD ; + +while (pfn < pfn_end) { + ret =__snp_lookup_rmpentry (pfn, &e, &level); +if (ret) { +pr_info ("Failed to read RMP entry for PFN 0x%llx\n" , pfn); + pfn++; +continue ; + } + +if (e.low || e.high) +pr_info ("RMPEntry paddr 0x%llx: [high=0x%016llx low=0x%016llx]\n" , + pfn <<PAGE_SHIFT , e.high, e.low); + pfn++; + } +} +EXPORT_SYMBOL_GPL (sev_dump_rmpentry);
arch/x86/include/asm/sev-host.h +2 -0
@@ -15,8 +15,10 @@#ifdef CONFIG_KVM_AMD_SEV int snp_lookup_rmpentry (u64 pfn,bool *assigned,int *level); +void sev_dump_rmpentry (u64 pfn);#else static inline int snp_lookup_rmpentry (u64 pfn,bool *assigned,int *level) {return 0 ; } +static inline void sev_dump_rmpentry (u64 pfn) {}#endif #endif
Signature · 1 line
2.25.1
Re: [PATCH RFC v9 10/51] x86/fault: Add helper for dumping RMP entries
Quoted 13 lines · Michael Roth
arch/x86/coco/sev/host.c +13
-0+/* +* If the RMP entry at the faulting pfn was not assigned, then not sure +* what caused the RMP violation. To get some useful debug information, +* iterate through the entire 2MB region, and dump the RMP entries if +* one of the bit in the RMP entry is set. +*/ + pfn = pfn & ~(PTRS_PER_PMD -1 ); + pfn_end = pfn +PTRS_PER_PMD ; + +while (pfn < pfn_end) { + ret =__snp_lookup_rmpentry (pfn, &e, &level); +if (ret) { +pr_info ("Failed to read RMP entry for PFN 0x%llx\n" , pfn);
arch/x86/coco/sev/host.c int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) · line 339
Quoted 10 lines
arch/x86/coco/sev/host.c +10
-0+ pfn++; +continue ; + } + +if (e.low || e.high) +pr_info ("RMPEntry paddr 0x%llx: [high=0x%016llx low=0x%016llx]\n" , + pfn <<PAGE_SHIFT , e.high, e.low); + pfn++; + } +}
Dumping 511 lines of (possible) junk into the dmesg buffer seems a _bit_ rude here. I can see dumping out the 2M RMP entry, but not the other 510.
This also destroys the information about which pfn was being targeted for the dump in the first place. That seems unfortunate.
RFC PATCH v9 11/51 x86/traps: Define RMP violation #PF error code
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
Bit 31 in the page fault-error bit will be set when processor encounters an RMP violation.
While at it, use the BIT_ULL() macro.
Signed-off by: Ashish Kalra <[email protected]>
--- arch/x86/include/asm/trap_pf.h | 18 +++++++++++------- arch/x86/mm/fault.c | 1 + 2 files changed, 12 insertions(+), 7 deletions(-)
arch/x86/include/asm/trap_pf.h +11 -7
@@ -2,6 +2,8 @@#ifndef _ASM_X86_TRAP_PF_H#define _ASM_X86_TRAP_PF_H +#include <linux/bits.h> /* BIT() macro */ +/* * Page fault error code bits: *
@@ -12,15 +14,17 @@* bit 4 == 1: fault was an instruction fetch * bit 5 == 1: protection keys block access * bit 15 == 1: SGX MMU page-fault +* bit 31 == 1: fault was due to RMP violation */ enum x86_pf_error_code { -X86_PF_PROT =1 <<0 , -X86_PF_WRITE =1 <<1 , -X86_PF_USER =1 <<2 , -X86_PF_RSVD =1 <<3 , -X86_PF_INSTR =1 <<4 , -X86_PF_PK =1 <<5 , -X86_PF_SGX =1 <<15 , +X86_PF_PROT =BIT (0 ), +X86_PF_WRITE =BIT (1 ), +X86_PF_USER =BIT (2 ), +X86_PF_RSVD =BIT (3 ), +X86_PF_INSTR =BIT (4 ), +X86_PF_PK =BIT (5 ), +X86_PF_SGX =BIT (15 ), +X86_PF_RMP =BIT (31 ), };#endif /* _ASM_X86_TRAP_PF_H */
arch/x86/mm/fault.c +1 -0
@@ -546,6 +546,7 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long ad !(error_code &X86_PF_PROT ) ?"not-present page" : (error_code &X86_PF_RSVD ) ?"reserved bit violation" : (error_code &X86_PF_PK ) ?"protection keys violation" : + (error_code &X86_PF_RMP ) ?"RMP violation" : "permissions violation");if (!(error_code &X86_PF_USER ) &&user_mode (regs)) {
Signature · 1 line
2.25.1
Re: [PATCH RFC v9 11/51] x86/traps: Define RMP violation #PF error code
Quoted 6 lines · Michael Roth
From: Brijesh Singh <[email protected]>
Bit 31 in the page fault-error bit will be set when processor encounters an RMP violation.
While at it, use the
BIT_ULL()macro.
...
Quoted 7 lines
enum
x86_pf_error_code{arch/x86/include/asm/trap_pf.h +0
-6-X86_PF_PROT =1 <<0 , -X86_PF_WRITE =1 <<1 , -X86_PF_USER =1 <<2 , -X86_PF_RSVD =1 <<3 , -X86_PF_INSTR =1 <<4 , - X86_PF_PK = 1 << 5,
arch/x86/include/asm/trap_pf.h line 28
Quoted 10 lines
arch/x86/include/asm/trap_pf.h +8
-1- X86_PF_SGX = 1 << 15, +X86_PF_PROT =BIT (0 ), +X86_PF_WRITE =BIT (1 ), +X86_PF_USER =BIT (2 ), +X86_PF_RSVD =BIT (3 ), +X86_PF_INSTR =BIT (4 ), +X86_PF_PK =BIT (5 ), +X86_PF_SGX =BIT (15 ), + X86_PF_RMP = BIT(31), };
It would be nice if the changelog "BIT_ULL()" matched the code "BIT()". :)
With that fixed,
RFC PATCH v9 12/51 x86/fault: Report RMP page faults for kernel addresses
In reply to Michael RothRMP #PFs on kernel addresses are fatal and should never happen in practice. They indicate a bug in the host kernel somewhere, so dump some information about any RMP entries related to the faulting address to aid with debugging.
--- arch/x86/mm/fault.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
arch/x86/mm/fault.c +13 -0
@@ -33,6 +33,7 @@#include <asm/kvm_para.h> /* kvm_handle_async_pf */ #include <asm/vdso.h> /* fixup_vdso_exception() */ #include <asm/irq_stack.h> +#include <asm/sev-host.h> /* sev_dump_rmpentry() */ #define CREATE_TRACE_POINTS #include <asm/trace/exceptions.h>
@@ -579,6 +580,18 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long ad }dump_pagetable (address); + +if (error_code &X86_PF_RMP ) { +unsigned int level; +pgd_t *pgd; +pte_t *pte; + + pgd =__va (read_cr3_pa ()); + pgd +=pgd_index (address); + pte =lookup_address_in_pgd (pgd, address, &level); + +sev_dump_rmpentry (pte_pfn (*pte)); + } }static noinline void
Signature · 1 line
2.25.1
Re: [PATCH RFC v9 12/51] x86/fault: Report RMP page faults for kernel addresses
Quoted 4 lines · Michael Roth
arch/x86/mm/fault.c +3
-0dump_pagetable (address); + +if (error_code &X86_PF_RMP ) { +unsigned int level;
arch/x86/mm/fault.c show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long ad · line 595
Quoted 10 lines
arch/x86/mm/fault.c +9
-0+pgd_t *pgd; +pte_t *pte; + + pgd =__va (read_cr3_pa ()); + pgd +=pgd_index (address); + pte =lookup_address_in_pgd (pgd, address, &level); + +sev_dump_rmpentry (pte_pfn (*pte)); + } }
It would be nice to trim this hunk down. Can you make it:
if (error_code & X86_PF_RMP) sev_dump_rmpentry(address);
and hide the rest of the logic in the helper?
Oh, and lookup_address_in_pgd() can return NULL. It's not great to page fault in the page fault handler. Could you fix that up too, please?
RFC PATCH v9 13/51 x86/fault: Handle RMP page faults for user addresses
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
When SEV-SNP is enabled globally, a write from the host is subject to checks performed by the hardware against the RMP table (APM2 15.36.10) at the end of a page walk:
1. Assigned bit in the RMP table is not set (i.e page is shared).
2. Immutable bit in the RMP table is not set.
3. If the page table entry that gives the sPA indicates that the target page size is a large page, then all RMP entries for the 4KB constituting pages of the target must have the assigned bit 0.
Nothing constructive can come of an attempt by userspace to violate case
1) (which will result in writing garbage due to page encryption) or case
2) (userspace should not ever need or be allowed to write to a page that the host has specifically needed to mark immutable).
Case 3) is dependent on the hypervisor. In case of KVM, due to how shared/private pages are partitioned into separate memory pools via restricted/guarded memory, there should never be a case where a page in the private pool overlaps with a shared page: either it is a hugepage-sized allocation and all the sub-pages are private, or it is a single-page allocation, in which case it cannot overlap with anything but itself.
Therefore, for all 3 cases, it is appropriate to simply kill the userspace process if it ever generates an RMP #PF. Implement that logic here.
[mdr: drop all previous page-splitting logic since it is no longer
needed with restricted/guarded memory, update commit message accordingly]
--- arch/x86/mm/fault.c | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/mm/fault.c +7 -0
@@ -1329,6 +1329,13 @@ void do_user_addr_fault(struct pt_regs *regs,if (error_code &X86_PF_INSTR ) flags |=FAULT_FLAG_INSTRUCTION ; +if (error_code &X86_PF_RMP ) { +pr_err ("Unexpected RMP page fault for address 0x%lx, terminating process\n" , + address); +do_sigbus (regs, error_code, address,VM_FAULT_SIGBUS ); +return ; + } +#ifdef CONFIG_X86_64 /* * Faults in the vsyscall page might need emulation. The
Signature · 1 line
2.25.1
Re: [PATCH RFC v9 13/51] x86/fault: Handle RMP page faults for user addresses
Quoted 12 lines · Michael Roth
From: Brijesh Singh <[email protected]>
When SEV-SNP is enabled globally, a write from the host is subject to checks performed by the hardware against the RMP table (APM2 15.36.10) at the end of a page walk:
1. Assigned bit in the RMP table is not set (i.e page is shared).
2. Immutable bit in the RMP table is not set.
3. If the page table entry that gives the sPA indicates that the target page size is a large page, then all RMP entries for the 4KB constituting pages of the target must have the assigned bit 0.
Quoted 4 lines
Nothing constructive can come of an attempt by userspace to violate case
1) (which will result in writing garbage due to page encryption) or case
2) (userspace should not ever need or be allowed to write to a page that the host has specifically needed to mark immutable).
What does this _mean_? If nothing constructive can come of it, what does that mean for the kernel?
Quoted 11 lines
Case 3) is dependent on the hypervisor. In case of KVM, due to how shared/private pages are partitioned into separate memory pools via restricted/guarded memory, there should never be a case where a page in the private pool overlaps with a shared page: either it is a hugepage-sized allocation and all the sub-pages are private, or it is a single-page allocation, in which case it cannot overlap with anything but itself.
Therefore, for all 3 cases, it is appropriate to simply kill the userspace process if it ever generates an RMP #PF. Implement that logic here.
...
arch/x86/mm/fault.c void do_user_addr_fault(struct pt_regs *regs, · line 1338
Quoted 7 lines
arch/x86/mm/fault.c +7
-0+if (error_code &X86_PF_RMP ) { +pr_err ("Unexpected RMP page fault for address 0x%lx, terminating process\n" , + address); +do_sigbus (regs, error_code, address,VM_FAULT_SIGBUS ); +return ; + } +
This is special-snowflake code. You're making the argument that an RMP fault is a special snowflake and needs special handling.
Why should an RMP violation be any different than, say a write to a read-only page (that also ends in signal delivery)?
I kinda dislike the entire changelog here. I really don't know what point it's making or what it is arguing.
RFC PATCH v9 14/51 x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
The RMPUPDATE instruction writes a new RMP entry in the RMP Table. The hypervisor will use the instruction to add pages to the RMP table. See APM3 for details on the instruction operations.
The PSMASH instruction expands a 2MB RMP entry into a corresponding set of contiguous 4KB-Page RMP entries. The hypervisor will use this instruction to adjust the RMP entry without invalidating the previous RMP entry.
Add the following external interface API functions:
psmash():
Used to smash a 2MB aligned page into 4K pages while preserving the Validated bit in the RMP.
rmp_make_private():
Used to assign a page to guest using the RMPUPDATE instruction.
rmp_make_shared():
Used to transition a page to hypervisor/shared state using the RMPUPDATE instruction.
--- arch/x86/coco/sev/host.c | 94 +++++++++++++++++++++++++++++++ arch/x86/include/asm/sev-common.h | 14 +++++ arch/x86/include/asm/sev-host.h | 10 ++++ 3 files changed, 118 insertions(+)
arch/x86/coco/sev/host.c +94 -0
@@ -338,3 +338,97 @@ void sev_dump_rmpentry(u64 pfn) } }EXPORT_SYMBOL_GPL (sev_dump_rmpentry); + +/* +* PSMASH a 2MB aligned page into 4K pages in the RMP table while preserving the +* Validated bit. +*/ +int psmash (u64 pfn) +{ +unsigned long paddr = pfn <<PAGE_SHIFT ; +int ret; + +pr_debug ("%s: PFN: 0x%llx\n" , __func__, pfn); + +if (!pfn_valid (pfn)) +return -EINVAL ; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return -ENXIO ; + +/* Binutils version 2.36 supports the PSMASH mnemonic. */ + asmvolatile (".byte 0xF3, 0x0F, 0x01, 0xFF" + :"=a" (ret) + :"a" (paddr) + :"memory" ,"cc" ); + +return ret; +} +EXPORT_SYMBOL_GPL (psmash); + +static int rmpupdate (u64 pfn,struct rmp_state *val) +{ +unsigned long paddr = pfn <<PAGE_SHIFT ; +int ret, level, npages; +int attempts =0 ; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return -ENXIO ; + +do { +/* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ + asmvolatile (".byte 0xF2, 0x0F, 0x01, 0xFE" + :"=a" (ret) + :"a" (paddr),"c" ((unsigned long )val) + :"memory" ,"cc" ); + + attempts++; + }while (ret ==RMPUPDATE_FAIL_OVERLAP ); + +if (ret) { +pr_err ("RMPUPDATE failed after %d attempts, ret: %d, pfn: %llx, npages: %d, level: %d\n" , + attempts, ret, pfn, npages, level); +sev_dump_rmpentry (pfn); +dump_stack (); +return -EFAULT ; + } + +return 0 ; +} + +/* +* Assign a page to guest using the RMPUPDATE instruction. +*/ +int rmp_make_private (u64 pfn,u64 gpa,enum pg_level level,int asid,bool immutable) +{ +struct rmp_state val; + +pr_debug ("%s: GPA: 0x%llx, PFN: 0x%llx, level: %d, immutable: %d\n" , + __func__, gpa, pfn, level, immutable); + +memset (&val,0 ,sizeof (val)); + val.assigned =1 ; + val.asid = asid; + val.immutable = immutable; + val.gpa = gpa; + val.pagesize =X86_TO_RMP_PG_LEVEL (level); + +return rmpupdate (pfn, &val); +} +EXPORT_SYMBOL_GPL (rmp_make_private); + +/* +* Transition a page to hypervisor/shared state using the RMPUPDATE instruction. +*/ +int rmp_make_shared (u64 pfn,enum pg_level level) +{ +struct rmp_state val; + +pr_debug ("%s: PFN: 0x%llx, level: %d\n" , __func__, pfn, level); + +memset (&val,0 ,sizeof (val)); + val.pagesize =X86_TO_RMP_PG_LEVEL (level); + +return rmpupdate (pfn, &val); +} +EXPORT_SYMBOL_GPL (rmp_make_shared);
arch/x86/include/asm/sev-common.h +14 -0
@@ -171,8 +171,22 @@ struct snp_psc_desc {#define GHCB_ERR_INVALID_INPUT 5#define GHCB_ERR_INVALID_EVENT 6 +/* RMUPDATE detected 4K page and 2MB page overlap. */ +#define RMPUPDATE_FAIL_OVERLAP 4 +/* RMP page size */ #define RMP_PG_SIZE_4K 0 +#define RMP_PG_SIZE_2M 1#define RMP_TO_X86_PG_LEVEL (level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) +#define X86_TO_RMP_PG_LEVEL (level) (((level) == PG_LEVEL_4K) ? RMP_PG_SIZE_4K : RMP_PG_SIZE_2M) + +struct rmp_state { +u64 gpa; +u8 assigned; +u8 pagesize; +u8 immutable; +u8 rsvd; +u32 asid; +} __packed;#endif
arch/x86/include/asm/sev-host.h +10 -0
@@ -16,9 +16,19 @@#ifdef CONFIG_KVM_AMD_SEV int snp_lookup_rmpentry (u64 pfn,bool *assigned,int *level);void sev_dump_rmpentry (u64 pfn); +int psmash (u64 pfn); +int rmp_make_private (u64 pfn,u64 gpa,enum pg_level level,int asid,bool immutable); +int rmp_make_shared (u64 pfn,enum pg_level level);#else static inline int snp_lookup_rmpentry (u64 pfn,bool *assigned,int *level) {return 0 ; }static inline void sev_dump_rmpentry (u64 pfn) {} +static inline int psmash (u64 pfn) {return -ENXIO ; } +static inline int rmp_make_private (u64 pfn,u64 gpa,enum pg_level level,int asid, +bool immutable) +{ +return -ENODEV ; +} +static inline int rmp_make_shared (u64 pfn,enum pg_level level) {return -ENODEV ; }#endif #endif
Signature · 1 line
2.25.1
Re: [PATCH RFC v9 14/51] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
arch/x86/coco/sev/host.c void sev_dump_rmpentry(u64 pfn) · line 407
Quoted 9 lines · Michael Roth
arch/x86/coco/sev/host.c +9
-0+/* +* Assign a page to guest using the RMPUPDATE instruction. +*/ +int rmp_make_private (u64 pfn,u64 gpa,enum pg_level level,int asid,bool immutable) +{ +struct rmp_state val; + +pr_debug ("%s: GPA: 0x%llx, PFN: 0x%llx, level: %d, immutable: %d\n" , + __func__, gpa, pfn, level, immutable);
Is this needed *EVERY* time a page is assigned to a guest? As in, if I create a 4GB guest, I'll see a literal million of these pr_debug()s in dmesg?
RFC PATCH v9 15/51 x86/sev: Invalidate pages from the direct map when adding them to the RMP table
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
The integrity guarantee of SEV-SNP is enforced through the RMP table. The RMP is used with standard x86 and IOMMU page tables to enforce memory restrictions and page access rights. The RMP check is enforced as soon as SEV-SNP is enabled globally in the system. When hardware encounters an RMP-check failure, it raises a page-fault exception.
The rmp_make_private() and rmp_make_shared() helpers are used to add or remove the pages from the RMP table. Improve the rmp_make_private() to invalidate state so that pages cannot be used in the direct-map after they are added the RMP table, and restored to their default valid permission after the pages are removed from the RMP table.
--- arch/x86/coco/sev/host.c | 62 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+)
arch/x86/coco/sev/host.c +62 -0
@@ -366,6 +366,42 @@ int psmash(u64 pfn) }EXPORT_SYMBOL_GPL (psmash); +static int restore_direct_map (u64 pfn,int npages) +{ +int i, ret =0 ; + +for (i =0 ; i < npages; i++) { + ret =set_direct_map_default_noflush (pfn_to_page (pfn + i)); +if (ret) +break ; + } + +if (ret) +pr_warn ("Failed to restore direct map for pfn 0x%llx, ret: %d\n" , + pfn + i, ret); + +return ret; +} + +static int invalidate_direct_map (u64 pfn,int npages) +{ +int i, ret =0 ; + +for (i =0 ; i < npages; i++) { + ret =set_direct_map_invalid_noflush (pfn_to_page (pfn + i)); +if (ret) +break ; + } + +if (ret) { +pr_warn ("Failed to invalidate direct map for pfn 0x%llx, ret: %d\n" , + pfn + i, ret); +restore_direct_map (pfn, i); + } + +return ret; +} +static int rmpupdate (u64 pfn,struct rmp_state *val) {unsigned long paddr = pfn <<PAGE_SHIFT ;
@@ -375,6 +411,21 @@ static int rmpupdate(u64 pfn, struct rmp_state *val)if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP ))return -ENXIO ; + level =RMP_TO_X86_PG_LEVEL (val->pagesize); + npages =page_level_size (level) /PAGE_SIZE ; + +/* +* If page is getting assigned in the RMP table then unmap it from the +* direct map. +*/ +if (val->assigned) { +if (invalidate_direct_map (pfn, npages)) { +pr_err ("Failed to unmap %d pages at pfn 0x%llx from the direct_map\n" , + npages, pfn); +return -EFAULT ; + } + } + do { /* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFE"
@@ -393,6 +444,17 @@ static int rmpupdate(u64 pfn, struct rmp_state *val)return -EFAULT ; } +/* +* Restore the direct map after the page is removed from the RMP table. +*/ +if (!val->assigned) { +if (restore_direct_map (pfn, npages)) { +pr_err ("Failed to map %d pages at pfn 0x%llx into the direct_map\n" , + npages, pfn); +return -EFAULT ; + } + } +return 0 ; }
Signature · 1 line
2.25.1
RFC PATCH v9 16/51 crypto: ccp: Define the SEV-SNP commands
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
AMD introduced the next generation of SEV called SEV-SNP (Secure Nested Paging). SEV-SNP builds upon existing SEV and SEV-ES functionality while adding new hardware security protection.
Define the commands and structures used to communicate with the AMD-SP when creating and managing the SEV-SNP guests. The SEV-SNP firmware spec is available at developer.amd.com/sev.
[mdr: update SNP command list and SNP status struct based on current
spec]
--- drivers/crypto/ccp/sev-dev.c | 16 +++ include/linux/psp-sev.h | 246 +++++++++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 53 ++++++++ 3 files changed, 315 insertions(+)
drivers/crypto/ccp/sev-dev.c +16 -0
@@ -128,6 +128,8 @@ static int sev_cmd_buffer_len(int cmd)switch (cmd) {case SEV_CMD_INIT :return sizeof (struct sev_data_init );case SEV_CMD_INIT_EX :return sizeof (struct sev_data_init_ex ); +case SEV_CMD_SNP_SHUTDOWN_EX :return sizeof (struct sev_data_snp_shutdown_ex ); +case SEV_CMD_SNP_INIT_EX :return sizeof (struct sev_data_snp_init_ex );case SEV_CMD_PLATFORM_STATUS :return sizeof (struct sev_user_data_status );case SEV_CMD_PEK_CSR :return sizeof (struct sev_data_pek_csr );case SEV_CMD_PEK_CERT_IMPORT :return sizeof (struct sev_data_pek_cert_import );
@@ -156,6 +158,20 @@ static int sev_cmd_buffer_len(int cmd)case SEV_CMD_GET_ID :return sizeof (struct sev_data_get_id );case SEV_CMD_ATTESTATION_REPORT :return sizeof (struct sev_data_attestation_report );case SEV_CMD_SEND_CANCEL :return sizeof (struct sev_data_send_cancel ); +case SEV_CMD_SNP_GCTX_CREATE :return sizeof (struct sev_data_snp_addr ); +case SEV_CMD_SNP_LAUNCH_START :return sizeof (struct sev_data_snp_launch_start ); +case SEV_CMD_SNP_LAUNCH_UPDATE :return sizeof (struct sev_data_snp_launch_update ); +case SEV_CMD_SNP_ACTIVATE :return sizeof (struct sev_data_snp_activate ); +case SEV_CMD_SNP_DECOMMISSION :return sizeof (struct sev_data_snp_addr ); +case SEV_CMD_SNP_PAGE_RECLAIM :return sizeof (struct sev_data_snp_page_reclaim ); +case SEV_CMD_SNP_GUEST_STATUS :return sizeof (struct sev_data_snp_guest_status ); +case SEV_CMD_SNP_LAUNCH_FINISH :return sizeof (struct sev_data_snp_launch_finish ); +case SEV_CMD_SNP_DBG_DECRYPT :return sizeof (struct sev_data_snp_dbg ); +case SEV_CMD_SNP_DBG_ENCRYPT :return sizeof (struct sev_data_snp_dbg ); +case SEV_CMD_SNP_PAGE_UNSMASH :return sizeof (struct sev_data_snp_page_unsmash ); +case SEV_CMD_SNP_PLATFORM_STATUS :return sizeof (struct sev_data_snp_addr ); +case SEV_CMD_SNP_GUEST_REQUEST :return sizeof (struct sev_data_snp_guest_request ); +case SEV_CMD_SNP_CONFIG :return sizeof (struct sev_user_data_snp_config );default :return 0 ; }
include/linux/psp-sev.h +246 -0
258 lines not on this page · raw message
include/uapi/linux/psp-sev.h +53 -0
65 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC PATCH v9 17/51 crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
Before SNP VMs can be launched, the platform must be appropriately configured and initialized. Platform initialization is accomplished via the SNP_INIT command. Make sure to do a WBINVD and issue DF_FLUSH command to prepare for the first SNP guest launch after INIT.
During the execution of SNP_INIT command, the firmware configures and enables SNP security policy enforcement in many system components. Some system components write to regions of memory reserved by early x86 firmware (e.g. UEFI). Other system components write to regions provided by the operation system, hypervisor, or x86 firmware. Such system components can only write to HV-fixed pages or Default pages. They will error when attempting to write to other page states after SNP_INIT enables their SNP enforcement.
Starting in SNP firmware v1.52, the SNP_INIT_EX command takes a list of system physical address ranges to convert into the HV-fixed page states during the RMP initialization. If INIT_RMP is 1, hypervisors should provide all system physical address ranges that the hypervisor will never assign to a guest until the next RMP re-initialization. For instance, the memory that UEFI reserves should be included in the range list. This allows system components that occasionally write to memory (e.g. logging to UEFI reserved regions) to not fail due to RMP initialization and SNP enablement.
--- drivers/crypto/ccp/sev-dev.c | 242 +++++++++++++++++++++++++++++++++-- drivers/crypto/ccp/sev-dev.h | 2 + 2 files changed, 234 insertions(+), 10 deletions(-)
drivers/crypto/ccp/sev-dev.c +232 -10
@@ -27,6 +27,7 @@#include <asm/smp.h> #include <asm/cacheflush.h> +#include <asm/e820/types.h> #include "psp-dev.h" #include "sev-dev.h"
@@ -35,6 +36,10 @@#define SEV_FW_FILE "amd/sev.fw"#define SEV_FW_NAME_SIZE 64 +/* Minimum firmware version required for the SEV-SNP support */ +#define SNP_MIN_API_MAJOR 1 +#define SNP_MIN_API_MINOR 51 +static DEFINE_MUTEX (sev_cmd_mutex);static struct sev_misc_dev *misc_dev;
@@ -78,6 +83,14 @@ static void *sev_es_tmr;#define NV_LENGTH (32 * 1024)static void *sev_init_ex_buffer; +/* +* SEV_DATA_RANGE_LIST: +* Array containing range of pages that firmware transitions to HV-fixed +* page state. +*/ +struct sev_data_range_list *snp_range_list; +static int __sev_snp_init_locked (int *error); +static inline bool sev_version_greater_or_equal (u8 maj,u8 min) {struct sev_device *sev = psp_master->sev_data;
@@ -462,7 +475,8 @@ static int __sev_platform_init_locked(int *error) {struct psp_device *psp = psp_master;struct sev_device *sev; -int rc =0 , psp_ret =-1 ; +int psp_ret =-1 ; +int rc;int (*init_function)(int *error);if (!psp || !psp->sev_data)
@@ -473,6 +487,26 @@ static int __sev_platform_init_locked(int *error)if (sev->state ==SEV_STATE_INIT )return 0 ; + rc =__sev_snp_init_locked (error); +if (rc && rc != -ENODEV ) { +/* +* Don't abort the probe if SNP INIT failed, +* continue to initialize the legacy SEV firmware. +*/ +dev_err (sev->dev,"SEV-SNP: failed to INIT rc %d, error %#x\n" , rc, *error); + } + +if (!sev_es_tmr) { +/* Obtain the TMR memory area for SEV-ES use */ + sev_es_tmr =sev_fw_alloc (SEV_ES_TMR_SIZE ); +if (sev_es_tmr) +/* Must flush the cache before giving it to the firmware */ +clflush_cache_range (sev_es_tmr,SEV_ES_TMR_SIZE ); +else +dev_warn (sev->dev, +"SEV: TMR allocation failed, SEV-ES support unavailable\n" ); + } +if (sev_init_ex_buffer) { init_function = __sev_init_ex_locked; rc =sev_read_init_ex_file ();
@@ -832,6 +866,191 @@ static int sev_update_firmware(struct device *dev)return ret; } +static void snp_set_hsave_pa (void *arg) +{ +wrmsrl (MSR_VM_HSAVE_PA ,0 ); +} + +static int snp_filter_reserved_mem_regions (struct resource *rs,void *arg) +{ +struct sev_data_range_list *range_list = arg; +struct sev_data_range *range = &range_list->ranges[range_list->num_elements]; +size_t size; + +if ((range_list->num_elements *sizeof (struct sev_data_range ) + +sizeof (struct sev_data_range_list )) >PAGE_SIZE ) +return -E2BIG ; + +switch (rs->desc) { +case E820_TYPE_RESERVED : +case E820_TYPE_PMEM : +case E820_TYPE_ACPI : + range->base = rs->start &PAGE_MASK ; + size = (rs->end +1 ) - rs->start; + range->page_count = size >>PAGE_SHIFT ; + range_list->num_elements++; +break ; +default : +break ; + } + +return 0 ; +} + +static int __sev_snp_init_locked (int *error) +{ +struct psp_device *psp = psp_master; +struct sev_data_snp_init_ex data; +struct sev_device *sev; +int rc =0 ; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return -ENODEV ; + +if (!psp || !psp->sev_data) +return -ENODEV ; + + sev = psp->sev_data; + +if (sev->snp_initialized) +return 0 ; + +if (!sev_version_greater_or_equal (SNP_MIN_API_MAJOR ,SNP_MIN_API_MINOR )) { +dev_dbg (sev->dev,"SEV-SNP support requires firmware version >= %d:%d\n" , +SNP_MIN_API_MAJOR ,SNP_MIN_API_MINOR ); +return 0 ; + } + +/* +* The SNP_INIT requires the MSR_VM_HSAVE_PA must be set to 0h +* across all cores. +*/ +on_each_cpu (snp_set_hsave_pa,NULL ,1 ); + +/* +* Starting in SNP firmware v1.52, the SNP_INIT_EX command takes a list of +* system physical address ranges to convert into the HV-fixed page states +* during the RMP initialization. For instance, the memory that UEFI +* reserves should be included in the range list. This allows system +* components that occasionally write to memory (e.g. logging to UEFI +* reserved regions) to not fail due to RMP initialization and SNP enablement. +*/ +if (sev_version_greater_or_equal (SNP_MIN_API_MAJOR ,52 )) { +/* +* Firmware checks that the pages containing the ranges enumerated +* in the RANGES structure are either in the Default page state or in the +* firmware page state. +*/ + snp_range_list =kzalloc (PAGE_SIZE ,GFP_KERNEL ); +if (!snp_range_list) { +dev_err (sev->dev, +"SEV: SNP_INIT_EX range list memory allocation failed\n" ); +return -ENOMEM ; + } + +/* +* Retrieve all reserved memory regions setup by UEFI from the e820 memory map +* to be setup as HV-fixed pages. +*/ + + rc =walk_iomem_res_desc (IORES_DESC_NONE ,IORESOURCE_MEM ,0 , ~0 , + snp_range_list, snp_filter_reserved_mem_regions); +if (rc) { +dev_err (sev->dev, +"SEV: SNP_INIT_EX walk_iomem_res_desc failed rc = %d\n" , rc); +return rc; + } + +memset (&data,0 ,sizeof (data)); + data.init_rmp =1 ; + data.list_paddr_en =1 ; + data.list_paddr =__psp_pa (snp_range_list); + +/* +* Before invoking SNP_INIT_EX with INIT_RMP=1, make sure that +* all dirty cache lines containing the RMP are flushed. +* +* NOTE: that includes writes via RMPUPDATE instructions, which +* are also cacheable writes. +*/ +wbinvd_on_all_cpus (); + + rc =__sev_do_cmd_locked (SEV_CMD_SNP_INIT_EX , &data, error); +if (rc) +return rc; + }else { +/* +* SNP_INIT is equivalent to SNP_INIT_EX with INIT_RMP=1, so +* just as with that case, make sure all dirty cache lines +* containing the RMP are flushed. +*/ +wbinvd_on_all_cpus (); + + rc =__sev_do_cmd_locked (SEV_CMD_SNP_INIT ,NULL , error); +if (rc) +return rc; + } + +/* Prepare for first SNP guest launch after INIT */ +wbinvd_on_all_cpus (); + rc =__sev_do_cmd_locked (SEV_CMD_SNP_DF_FLUSH ,NULL , error); +if (rc) +return rc; + + sev->snp_initialized = true; +dev_dbg (sev->dev,"SEV-SNP firmware initialized\n" ); + +return rc; +} + +static int __sev_snp_shutdown_locked (int *error) +{ +struct sev_device *sev = psp_master->sev_data; +struct sev_data_snp_shutdown_ex data; +int ret; + +if (!sev->snp_initialized) +return 0 ; + +memset (&data,0 ,sizeof (data)); + data.length =sizeof (data); + data.iommu_snp_shutdown =1 ; + +wbinvd_on_all_cpus (); + +retry: + ret =__sev_do_cmd_locked (SEV_CMD_SNP_SHUTDOWN_EX , &data, error); +/* SHUTDOWN may require DF_FLUSH */ +if (*error ==SEV_RET_DFFLUSH_REQUIRED ) { + ret =__sev_do_cmd_locked (SEV_CMD_SNP_DF_FLUSH ,NULL ,NULL ); +if (ret) { +dev_err (sev->dev,"SEV-SNP DF_FLUSH failed\n" ); +return ret; + } + goto retry; + } +if (ret) { +dev_err (sev->dev,"SEV-SNP firmware shutdown failed\n" ); +return ret; + } + + sev->snp_initialized = false; +dev_dbg (sev->dev,"SEV-SNP firmware shutdown\n" ); + +return ret; +} + +static int sev_snp_shutdown (int *error) +{ +int rc; + +mutex_lock (&sev_cmd_mutex); + rc =__sev_snp_shutdown_locked (error); +mutex_unlock (&sev_cmd_mutex); + +return rc; +} +static int sev_ioctl_do_pek_import (struct sev_issue_cmd *argp,bool writable) {struct sev_device *sev = psp_master->sev_data;
@@ -1279,6 +1498,8 @@ int sev_dev_init(struct psp_device *psp)static void sev_firmware_shutdown (struct sev_device *sev) { +int error; +sev_platform_shutdown (NULL );if (sev_es_tmr) {
@@ -1295,6 +1516,13 @@ static void sev_firmware_shutdown(struct sev_device *sev) get_order(NV_LENGTH)); sev_init_ex_buffer =NULL ; } + +if (snp_range_list) { +kfree (snp_range_list); + snp_range_list =NULL ; + } + +sev_snp_shutdown (&error); }void sev_dev_destroy (struct psp_device *psp)
@@ -1350,15 +1578,6 @@ void sev_pci_init(void) } } -/* Obtain the TMR memory area for SEV-ES use */ - sev_es_tmr =sev_fw_alloc (SEV_ES_TMR_SIZE ); -if (sev_es_tmr) -/* Must flush the cache before giving it to the firmware */ -clflush_cache_range (sev_es_tmr,SEV_ES_TMR_SIZE ); -else -dev_warn (sev->dev, -"SEV: TMR allocation failed, SEV-ES support unavailable\n" ); -if (!psp_init_on_probe)return ;
@@ -1368,6 +1587,9 @@ void sev_pci_init(void)dev_err (sev->dev,"SEV: failed to INIT error %#x, rc %d\n" , error, rc); +dev_info (sev->dev,"SEV%s API:%d.%d build:%d\n" , sev->snp_initialized ? +"-SNP" :"" , sev->api_major, sev->api_minor, sev->build); +return ; err:
drivers/crypto/ccp/sev-dev.h +2 -0
@@ -52,6 +52,8 @@ struct sev_device {u8 build;void *cmd_buf; + +bool snp_initialized; };int sev_dev_init (struct psp_device *psp);
Signature · 1 line
2.25.1
RFC PATCH v9 18/51 crypto: ccp: Provide API to issue SEV and SNP commands
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
Make sev_do_cmd() a generic API interface for the hypervisor to issue commands to manage an SEV and SNP guest. The commands for SEV and SNP are defined in the SEV and SEV-SNP firmware specifications.
--- drivers/crypto/ccp/sev-dev.c | 3 ++- include/linux/psp-sev.h | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-)
drivers/crypto/ccp/sev-dev.c +2 -1
@@ -418,7 +418,7 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)return ret; } -static int sev_do_cmd (int cmd,void *data,int *psp_ret) +int sev_do_cmd (int cmd,void *data,int *psp_ret) {int rc;
@@ -428,6 +428,7 @@ static int sev_do_cmd(int cmd, void *data, int *psp_ret)return rc; } +EXPORT_SYMBOL_GPL (sev_do_cmd);static int __sev_init_locked (int *error) {
include/linux/psp-sev.h +17 -0
@@ -891,6 +891,20 @@ int sev_guest_df_flush(int *error);*/ int sev_guest_decommission (struct sev_data_decommission *data,int *error); +/** +* sev_do_cmd - perform SEV command +* +* @error: SEV command return code +* +* Returns: +* 0 if the SEV successfully processed the command +* -%ENODEV if the SEV device is not available +* -%ENOTSUPP if the SEV does not support SEV +* -%ETIMEDOUT if the SEV command timed out +* -%EIO if the SEV returned a non-zero return code +*/ +int sev_do_cmd (int cmd,void *data,int *psp_ret); +void *psp_copy_user_blob (u64 uaddr,u32 len);#else /* !CONFIG_CRYPTO_DEV_SP_PSP */
@@ -906,6 +920,9 @@ sev_guest_deactivate(struct sev_data_deactivate *data, int *error) { return -ENOstatic inline int sev_guest_decommission (struct sev_data_decommission *data,int *error) {return -ENODEV ; } +static inline int +sev_do_cmd (int cmd,void *data,int *psp_ret) {return -ENODEV ; } +static inline int sev_guest_activate (struct sev_data_activate *data,int *error) {return -ENODEV ; }
Signature · 1 line
2.25.1
RFC PATCH v9 19/51 x86/sev: Introduce snp leaked pages list
In reply to Michael RothFrom: Ashish Kalra <[email protected]>
Pages are unsafe to be released back to the page-allocator, if they have been transitioned to firmware/guest state and can't be reclaimed or transitioned back to hypervisor/shared state. In this case add them to an internal leaked pages list to ensure that they are not freed or touched/accessed to cause fatal page faults.
--- arch/x86/coco/sev/host.c | 28 ++++++++++++++++++++++++++++ arch/x86/include/asm/sev-host.h | 3 +++ 2 files changed, 31 insertions(+)
arch/x86/coco/sev/host.c +28 -0
@@ -64,6 +64,12 @@ struct rmpentry {static unsigned long rmptable_start__ro_after_init ;static unsigned long rmptable_end__ro_after_init ; +/* list of pages which are leaked and cannot be reclaimed */ +static LIST_HEAD (snp_leaked_pages_list); +static DEFINE_SPINLOCK (snp_leaked_pages_list_lock); + +static atomic_long_t snp_nr_leaked_pages =ATOMIC_LONG_INIT (0 ); +#undef pr_fmt#define pr_fmt (fmt) "SEV-SNP: " fmt
@@ -494,3 +500,25 @@ int rmp_make_shared(u64 pfn, enum pg_level level)return rmpupdate (pfn, &val); }EXPORT_SYMBOL_GPL (rmp_make_shared); + +void snp_leak_pages (unsigned long pfn,unsigned int npages) +{ +struct page *page =pfn_to_page (pfn); + +WARN (1 ,"psc failed, pfn 0x%lx pages %d (marked offline)\n" , pfn, npages); + +spin_lock (&snp_leaked_pages_list_lock); +while (npages--) { +/* +* Reuse the page's buddy list for chaining into the leaked +* pages list. This page should not be on a free list currently +* and is also unsafe to be added to a free list. +*/ +list_add_tail (&page->buddy_list, &snp_leaked_pages_list); +sev_dump_rmpentry (pfn); + pfn++; + } +spin_unlock (&snp_leaked_pages_list_lock); +atomic_long_inc (&snp_nr_leaked_pages); +} +EXPORT_SYMBOL_GPL (snp_leak_pages);
arch/x86/include/asm/sev-host.h +3 -0
@@ -19,6 +19,8 @@ void sev_dump_rmpentry(u64 pfn);int psmash (u64 pfn);int rmp_make_private (u64 pfn,u64 gpa,enum pg_level level,int asid,bool immutable);int rmp_make_shared (u64 pfn,enum pg_level level); +void snp_leak_pages (unsigned long pfn,unsigned int npages); +#else static inline int snp_lookup_rmpentry (u64 pfn,bool *assigned,int *level) {return 0 ; }static inline void sev_dump_rmpentry (u64 pfn) {}
@@ -29,6 +31,7 @@ static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asreturn -ENODEV ; }static inline int rmp_make_shared (u64 pfn,enum pg_level level) {return -ENODEV ; } +void snp_leak_pages (unsigned long pfn,unsigned int npages) {}#endif #endif
Signature · 1 line
2.25.1
Re: [PATCH RFC v9 19/51] x86/sev: Introduce snp leaked pages list
Quoted 73 lines · Michael Roth
From: Ashish Kalra <[email protected]>
Pages are unsafe to be released back to the page-allocator, if they have been transitioned to
firmware/gueststate and can't be reclaimed or transitioned back to hypervisor/shared state. In this case add them to an internal leaked pages list to ensure that they are not freed or touched/accessed to cause fatal page faults.Signed-off-by Ashish Kalra <[email protected]>[mdr: relocate to arch/x86/coco/sev/host.c]Signed-off-by Michael Roth <[email protected]>--- arch/x86/coco/sev/host.c | 28 ++++++++++++++++++++++++++++ arch/x86/include/asm/sev-host.h | 3 +++ 2 files changed, 31 insertions(+)arch/x86/coco/sev/host.c +28
-0@@ -64,6 +64,12 @@ struct rmpentry {static unsigned long rmptable_start__ro_after_init ;static unsigned long rmptable_end__ro_after_init ; +/* list of pages which are leaked and cannot be reclaimed */ +static LIST_HEAD (snp_leaked_pages_list); +static DEFINE_SPINLOCK (snp_leaked_pages_list_lock); + +static atomic_long_t snp_nr_leaked_pages =ATOMIC_LONG_INIT (0 ); +#undef pr_fmt#define pr_fmt (fmt) "SEV-SNP: " fmt@@ -494,3 +500,25 @@ int rmp_make_shared(u64 pfn, enum pg_level level)return rmpupdate (pfn, &val); }EXPORT_SYMBOL_GPL (rmp_make_shared); + +void snp_leak_pages (unsigned long pfn,unsigned int npages) +{ +struct page *page =pfn_to_page (pfn); + +WARN (1 ,"psc failed, pfn 0x%lx pages %d (marked offline)\n" , pfn, npages); + +spin_lock (&snp_leaked_pages_list_lock); +while (npages--) { +/* +* Reuse the page's buddy list for chaining into the leaked +* pages list. This page should not be on a free list currently +* and is also unsafe to be added to a free list. +*/ +list_add_tail (&page->buddy_list, &snp_leaked_pages_list); +sev_dump_rmpentry (pfn); + pfn++; + } +spin_unlock (&snp_leaked_pages_list_lock); +atomic_long_inc (&snp_nr_leaked_pages); +} +EXPORT_SYMBOL_GPL (snp_leak_pages);arch/x86/include/asm/sev-host.h +2
-0@@ -19,6 +19,8 @@ void sev_dump_rmpentry(u64 pfn);int psmash (u64 pfn);int rmp_make_private (u64 pfn,u64 gpa,enum pg_level level,int asid,bool immutable);int rmp_make_shared (u64 pfn,enum pg_level level); +void snp_leak_pages (unsigned long pfn,unsigned int npages); +#else static inline int snp_lookup_rmpentry (u64 pfn,bool *assigned,int *level) {return 0 ; }static inline void sev_dump_rmpentry (u64 pfn) {}
arch/x86/include/asm/sev-host.h static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int as · line 34
Quoted 4 lines
arch/x86/include/asm/sev-host.h +1
-0@@ -29,6 +31,7 @@ static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asreturn -ENODEV ; }static inline int rmp_make_shared (u64 pfn,enum pg_level level) {return -ENODEV ; } +void snp_leak_pages (unsigned long pfn,unsigned int npages) {}
This needs to be 'static inline' or the build fails with multiple definition errors. I'm building a guest kernel with CONFIG_KVM_AMD_SEV disabled.
Jeremi
Quoted 6 lines
arch/x86/include/asm/sev-host.h +0
-0#endif #endif Signature · 1 line
2.25.1
RFC PATCH v9 20/51 crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
The behavior and requirement for the SEV-legacy command is altered when the SNP firmware is in the INIT state. See SEV-SNP firmware specification for more details.
Allocate the Trusted Memory Region (TMR) as a 2mb sized/aligned region when SNP is enabled to satisfy new requirements for the SNP. Continue allocating a 1mb region for !SNP configuration.
While at it, provide API that can be used by others to allocate a page that can be used by the firmware. The immediate user for this API will be the KVM driver. The KVM driver to need to allocate a firmware context page during the guest creation. The context page need to be updated by the firmware. See the SEV-SNP specification for further details.
[mdr: use struct sev_data_snp_page_reclaim instead of passing paddr
directly to SEV_CMD_SNP_PAGE_RECLAIM]
--- drivers/crypto/ccp/sev-dev.c | 150 ++++++++++++++++++++++++++++++++--- include/linux/psp-sev.h | 9 +++ 2 files changed, 150 insertions(+), 9 deletions(-)
drivers/crypto/ccp/sev-dev.c +141 -9
@@ -91,6 +91,13 @@ static void *sev_init_ex_buffer;struct sev_data_range_list *snp_range_list;static int __sev_snp_init_locked (int *error); +/* When SEV-SNP is enabled the TMR needs to be 2MB aligned and 2MB size. */ +#define SEV_SNP_ES_TMR_SIZE (2 * 1024 * 1024) + +static size_t sev_es_tmr_size =SEV_ES_TMR_SIZE ; + +static int __sev_do_cmd_locked (int cmd,void *data,int *psp_ret); +static inline bool sev_version_greater_or_equal (u8 maj,u8 min) {struct sev_device *sev = psp_master->sev_data;
@@ -191,11 +198,131 @@ static int sev_cmd_buffer_len(int cmd)return 0 ; } +static int snp_reclaim_pages (unsigned long paddr,unsigned int npages,bool locked) +{ +/* Cbit maybe set in the paddr */ +unsigned long pfn =__sme_clr (paddr) >>PAGE_SHIFT ; +int ret, err, i, n =0 ; + +for (i =0 ; i < npages; i++, pfn++, n++) { +struct sev_data_snp_page_reclaim data = {0 }; + + data.paddr = pfn <<PAGE_SHIFT ; + +if (locked) + ret =__sev_do_cmd_locked (SEV_CMD_SNP_PAGE_RECLAIM , &data, &err); +else + ret =sev_do_cmd (SEV_CMD_SNP_PAGE_RECLAIM , &data, &err); + +if (ret) + goto cleanup; + + ret =rmp_make_shared (pfn,PG_LEVEL_4K ); +if (ret) + goto cleanup; + } + +return 0 ; + +cleanup: +/* +* If failed to reclaim the page then page is no longer safe to +* be release back to the system, leak it. +*/ +snp_leak_pages (pfn, npages - n); +return ret; +} + +static int rmp_mark_pages_firmware (unsigned long paddr,unsigned int npages,bool locked) +{ +/* Cbit maybe set in the paddr */ +unsigned long pfn =__sme_clr (paddr) >>PAGE_SHIFT ; +int rc, n =0 , i; + +for (i =0 ; i < npages; i++, n++, pfn++) { + rc =rmp_make_private (pfn,0 ,PG_LEVEL_4K ,0 , true); +if (rc) + goto cleanup; + } + +return 0 ; + +cleanup: +/* +* Try unrolling the firmware state changes by +* reclaiming the pages which were already changed to the +* firmware state. +*/ +snp_reclaim_pages (paddr, n, locked); + +return rc; +} + +static struct page *__snp_alloc_firmware_pages (gfp_t gfp_mask,int order,bool locked) +{ +unsigned long npages =1ul << order, paddr; +struct sev_device *sev; +struct page *page; + +if (!psp_master || !psp_master->sev_data) +return NULL ; + + page =alloc_pages (gfp_mask, order); +if (!page) +return NULL ; + +/* If SEV-SNP is initialized then add the page in RMP table. */ + sev = psp_master->sev_data; +if (!sev->snp_initialized) +return page; + + paddr =__pa ((unsigned long )page_address (page)); +if (rmp_mark_pages_firmware (paddr, npages, locked)) +return NULL ; + +return page; +} + +void *snp_alloc_firmware_page (gfp_t gfp_mask) +{ +struct page *page; + + page =__snp_alloc_firmware_pages (gfp_mask,0 , false); + +return page ?page_address (page) :NULL ; +} +EXPORT_SYMBOL_GPL (snp_alloc_firmware_page); + +static void __snp_free_firmware_pages (struct page *page,int order,bool locked) +{ +struct sev_device *sev = psp_master->sev_data; +unsigned long paddr, npages =1ul << order; + +if (!page) +return ; + + paddr =__pa ((unsigned long )page_address (page)); +if (sev->snp_initialized && +snp_reclaim_pages (paddr, npages, locked)) +return ; + +__free_pages (page, order); +} + +void snp_free_firmware_page (void *addr) +{ +if (!addr) +return ; + +__snp_free_firmware_pages (virt_to_page (addr),0 , false); +} +EXPORT_SYMBOL_GPL (snp_free_firmware_page); +static void *sev_fw_alloc (unsigned long len) {struct page *page; - page =alloc_pages (GFP_KERNEL ,get_order (len)); + page =__snp_alloc_firmware_pages (GFP_KERNEL ,get_order (len), false);if (!page)return NULL ;
@@ -443,7 +570,7 @@ static int __sev_init_locked(int *error) data.tmr_address =__pa (sev_es_tmr); data.flags |=SEV_INIT_FLAGS_SEV_ES ; - data.tmr_len =SEV_ES_TMR_SIZE ; + data.tmr_len = sev_es_tmr_size; }return __sev_do_cmd_locked (SEV_CMD_INIT , &data, error);
@@ -466,7 +593,7 @@ static int __sev_init_ex_locked(int *error) data.tmr_address =__pa (sev_es_tmr); data.flags |=SEV_INIT_FLAGS_SEV_ES ; - data.tmr_len =SEV_ES_TMR_SIZE ; + data.tmr_len = sev_es_tmr_size; }return __sev_do_cmd_locked (SEV_CMD_INIT_EX , &data, error);
@@ -499,14 +626,16 @@ static int __sev_platform_init_locked(int *error)if (!sev_es_tmr) {/* Obtain the TMR memory area for SEV-ES use */ - sev_es_tmr =sev_fw_alloc (SEV_ES_TMR_SIZE ); -if (sev_es_tmr) + sev_es_tmr =sev_fw_alloc (sev_es_tmr_size); +if (sev_es_tmr) {/* Must flush the cache before giving it to the firmware */ -clflush_cache_range (sev_es_tmr,SEV_ES_TMR_SIZE ); -else +if (!sev->snp_initialized) +clflush_cache_range (sev_es_tmr, sev_es_tmr_size); + }else {dev_warn (sev->dev,"SEV: TMR allocation failed, SEV-ES support unavailable\n" ); } + }if (sev_init_ex_buffer) { init_function = __sev_init_ex_locked;
@@ -1001,6 +1130,8 @@ static int __sev_snp_init_locked(int *error) sev->snp_initialized = true;dev_dbg (sev->dev,"SEV-SNP firmware initialized\n" ); + sev_es_tmr_size =SEV_SNP_ES_TMR_SIZE ; +return rc; }
@@ -1507,8 +1638,9 @@ static void sev_firmware_shutdown(struct sev_device *sev)/* The TMR area was encrypted, flush it from the cache */ wbinvd_on_all_cpus (); -free_pages ((unsigned long )sev_es_tmr, -get_order (SEV_ES_TMR_SIZE )); +__snp_free_firmware_pages (virt_to_page (sev_es_tmr), +get_order (sev_es_tmr_size), + false); sev_es_tmr =NULL ; }
include/linux/psp-sev.h +9 -0
@@ -906,6 +906,8 @@ int sev_guest_decommission(struct sev_data_decommission *data, int *error);int sev_do_cmd (int cmd,void *data,int *psp_ret);void *psp_copy_user_blob (u64 uaddr,u32 len); +void *snp_alloc_firmware_page (gfp_t mask); +void snp_free_firmware_page (void *addr);#else /* !CONFIG_CRYPTO_DEV_SP_PSP */
@@ -933,6 +935,13 @@ sev_issue_cmd_external_user(struct file *filep, unsigned int id, void *data, intstatic inline void *psp_copy_user_blob (u64 __user uaddr,u32 len) {return ERR_PTR (-EINVAL ); } +static inline void *snp_alloc_firmware_page (gfp_t mask) +{ +return NULL ; +} + +static inline void snp_free_firmware_page (void *addr) { } +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */
Signature · 1 line
2.25.1
RFC PATCH v9 21/51 crypto: ccp: Handle the legacy SEV command when SNP is enabled
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
The behavior of the SEV-legacy commands is altered when the SNP firmware is in the INIT state. When SNP is in INIT state, all the SEV-legacy commands that cause the firmware to write to memory must be in the firmware state before issuing the command..
A command buffer may contains a system physical address that the firmware may write to. There are two cases that need to be handled:
1) system physical address points to a guest memory
2) system physical address points to a host memory
To handle the case #1, change the page state to the firmware in the RMP table before issuing the command and restore the state to shared after the command completes.
For the case #2, use a bounce buffer to complete the request.
--- drivers/crypto/ccp/sev-dev.c | 371 ++++++++++++++++++++++++++++++++++- drivers/crypto/ccp/sev-dev.h | 12 ++ 2 files changed, 373 insertions(+), 10 deletions(-)
drivers/crypto/ccp/sev-dev.c +361 -10
@@ -28,6 +28,7 @@#include <asm/smp.h> #include <asm/cacheflush.h> #include <asm/e820/types.h> +#include <asm/sev-host.h> #include "psp-dev.h" #include "sev-dev.h"
@@ -258,6 +259,30 @@ static int rmp_mark_pages_firmware(unsigned long paddr, unsigned int npages, booreturn rc; } +static int rmp_mark_pages_shared (unsigned long paddr,unsigned int npages) +{ +/* Cbit maybe set in the paddr */ +unsigned long pfn =__sme_clr (paddr) >>PAGE_SHIFT ; +int rc, n =0 , i; + +for (i =0 ; i < npages; i++, pfn++, n++) { + rc =rmp_make_shared (pfn,PG_LEVEL_4K ); +if (rc) + goto cleanup; + } + +return 0 ; + +cleanup: +/* +* If failed to change the page state to shared, then its not safe +* to release the page back to the system, leak it. +*/ +snp_leak_pages (pfn, npages - n); + +return rc; +} +static struct page *__snp_alloc_firmware_pages (gfp_t gfp_mask,int order,bool locked) {unsigned long npages =1ul << order, paddr;
@@ -459,12 +484,295 @@ static int sev_write_init_ex_file_if_required(int cmd_id)return sev_write_init_ex_file (); } +static int alloc_snp_host_map (struct sev_device *sev) +{ +struct page *page; +int i; + +for (i =0 ; i <MAX_SNP_HOST_MAP_BUFS ; i++) { +struct snp_host_map *map = &sev->snp_host_map[i]; + +memset (map,0 ,sizeof (*map)); + + page =alloc_pages (GFP_KERNEL_ACCOUNT ,get_order (SEV_FW_BLOB_MAX_SIZE )); +if (!page) +return -ENOMEM ; + + map->host =page_address (page); + } + +return 0 ; +} + +static void free_snp_host_map (struct sev_device *sev) +{ +int i; + +for (i =0 ; i <MAX_SNP_HOST_MAP_BUFS ; i++) { +struct snp_host_map *map = &sev->snp_host_map[i]; + +if (map->host) { +__free_pages (virt_to_page (map->host),get_order (SEV_FW_BLOB_MAX_SIZE )); +memset (map,0 ,sizeof (*map)); + } + } +} + +static int map_firmware_writeable (u64 *paddr,u32 len,bool guest,struct snp_host_map *map) +{ +unsigned int npages =PAGE_ALIGN (len) >>PAGE_SHIFT ; + + map->active = false; + +if (!paddr || !len) +return 0 ; + + map->paddr = *paddr; + map->len = len; + +/* If paddr points to a guest memory then change the page state to firmwware. */ +if (guest) { +if (rmp_mark_pages_firmware (*paddr, npages, true)) +return -EFAULT ; + + goto done; + } + +if (!map->host) +return -ENOMEM ; + +/* Check if the pre-allocated buffer can be used to fullfil the request. */ +if (len >SEV_FW_BLOB_MAX_SIZE ) +return -EINVAL ; + +/* Transition the pre-allocated buffer to the firmware state. */ +if (rmp_mark_pages_firmware (__pa (map->host), npages, true)) +return -EFAULT ; + +/* Set the paddr to use pre-allocated firmware buffer */ + *paddr =__psp_pa (map->host); + +done: + map->active = true; +return 0 ; +} + +static int unmap_firmware_writeable (u64 *paddr,u32 len,bool guest,struct snp_host_map *map) +{ +unsigned int npages =PAGE_ALIGN (len) >>PAGE_SHIFT ; + +if (!map->active) +return 0 ; + +/* If paddr points to a guest memory then restore the page state to hypervisor. */ +if (guest) { +if (snp_reclaim_pages (*paddr, npages, true)) +return -EFAULT ; + + goto done; + } + +/* +* Transition the pre-allocated buffer to hypervisor state before the access. +* +* This is because while changing the page state to firmware, the kernel unmaps +* the pages from the direct map, and to restore the direct map the pages must +* be transitioned back to the shared state. +*/ +if (snp_reclaim_pages (__pa (map->host), npages, true)) +return -EFAULT ; + +/* Copy the response data firmware buffer to the callers buffer. */ +memcpy (__va (__sme_clr (map->paddr)), map->host,min_t (size_t, len, map->len)); + *paddr = map->paddr; + +done: + map->active = false; +return 0 ; +} + +static bool sev_legacy_cmd_buf_writable (int cmd) +{ +switch (cmd) { +case SEV_CMD_PLATFORM_STATUS : +case SEV_CMD_GUEST_STATUS : +case SEV_CMD_LAUNCH_START : +case SEV_CMD_RECEIVE_START : +case SEV_CMD_LAUNCH_MEASURE : +case SEV_CMD_SEND_START : +case SEV_CMD_SEND_UPDATE_DATA : +case SEV_CMD_SEND_UPDATE_VMSA : +case SEV_CMD_PEK_CSR : +case SEV_CMD_PDH_CERT_EXPORT : +case SEV_CMD_GET_ID : +case SEV_CMD_ATTESTATION_REPORT : +return true; +default : +return false; + } +} + +#define prep_buffer (name, addr, len, guest, map) \ + func(&((typeof(name *))cmd_buf)->addr, ((typeof(name *))cmd_buf)->len, guest, map) + +static int __snp_cmd_buf_copy (int cmd,void *cmd_buf,bool to_fw,int fw_err) +{ +int (*func)(u64 *paddr,u32 len,bool guest,struct snp_host_map *map); +struct sev_device *sev = psp_master->sev_data; +bool from_fw = !to_fw; + +/* +* After the command is completed, change the command buffer memory to +* hypervisor state. +* +* The immutable bit is automatically cleared by the firmware, so +* no not need to reclaim the page. +*/ +if (from_fw &&sev_legacy_cmd_buf_writable (cmd)) { +if (rmp_mark_pages_shared (__pa (cmd_buf),1 )) +return -EFAULT ; + +/* No need to go further if firmware failed to execute command. */ +if (fw_err) +return 0 ; + } + +if (to_fw) + func = map_firmware_writeable; +else + func = unmap_firmware_writeable; + +/* +* A command buffer may contains a system physical address. If the address +* points to a host memory then use an intermediate firmware page otherwise +* change the page state in the RMP table. +*/ +switch (cmd) { +case SEV_CMD_PDH_CERT_EXPORT : + if (prep_buffer(struct sev_data_pdh_cert_export, pdh_cert_address, + pdh_cert_len, false, &sev->snp_host_map[0 ])) + goto err; + if (prep_buffer(struct sev_data_pdh_cert_export, cert_chain_address, + cert_chain_len, false, &sev->snp_host_map[1 ])) + goto err; +break ; +case SEV_CMD_GET_ID : + if (prep_buffer(struct sev_data_get_id, address, len, + false, &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_PEK_CSR : + if (prep_buffer(struct sev_data_pek_csr, address, len, + false, &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_LAUNCH_UPDATE_DATA : + if (prep_buffer(struct sev_data_launch_update_data, address, len, + true, &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_LAUNCH_UPDATE_VMSA : + if (prep_buffer(struct sev_data_launch_update_vmsa, address, len, + true, &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_LAUNCH_MEASURE : + if (prep_buffer(struct sev_data_launch_measure, address, len, + false, &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_LAUNCH_UPDATE_SECRET : + if (prep_buffer(struct sev_data_launch_secret, guest_address, guest_len, + true, &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_DBG_DECRYPT : + if (prep_buffer(struct sev_data_dbg, dst_addr, len, false, + &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_DBG_ENCRYPT : + if (prep_buffer(struct sev_data_dbg, dst_addr, len, true, + &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_ATTESTATION_REPORT : + if (prep_buffer(struct sev_data_attestation_report, address, len, + false, &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_SEND_START : + if (prep_buffer(struct sev_data_send_start, session_address, + session_len, false, &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_SEND_UPDATE_DATA : + if (prep_buffer(struct sev_data_send_update_data, hdr_address, hdr_len, + false, &sev->snp_host_map[0 ])) + goto err; + if (prep_buffer(struct sev_data_send_update_data, trans_address, + trans_len, false, &sev->snp_host_map[1 ])) + goto err; +break ; +case SEV_CMD_SEND_UPDATE_VMSA : + if (prep_buffer(struct sev_data_send_update_vmsa, hdr_address, hdr_len, + false, &sev->snp_host_map[0 ])) + goto err; + if (prep_buffer(struct sev_data_send_update_vmsa, trans_address, + trans_len, false, &sev->snp_host_map[1 ])) + goto err; +break ; +case SEV_CMD_RECEIVE_UPDATE_DATA : + if (prep_buffer(struct sev_data_receive_update_data, guest_address, + guest_len, true, &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_RECEIVE_UPDATE_VMSA : + if (prep_buffer(struct sev_data_receive_update_vmsa, guest_address, + guest_len, true, &sev->snp_host_map[0 ])) + goto err; +break ; +default : +break ; + } + +/* The command buffer need to be in the firmware state. */ +if (to_fw &&sev_legacy_cmd_buf_writable (cmd)) { +if (rmp_mark_pages_firmware (__pa (cmd_buf),1 , true)) +return -EFAULT ; + } + +return 0 ; + +err: +return -EINVAL ; +} + +static inline bool need_firmware_copy (int cmd) +{ +struct sev_device *sev = psp_master->sev_data; + +/* After SNP is INIT'ed, the behavior of legacy SEV command is changed. */ +return ((cmd <SEV_CMD_SNP_INIT ) && sev->snp_initialized) ? true : false; +} + +static int snp_aware_copy_to_firmware (int cmd,void *data) +{ +return __snp_cmd_buf_copy (cmd, data, true,0 ); +} + +static int snp_aware_copy_from_firmware (int cmd,void *data,int fw_err) +{ +return __snp_cmd_buf_copy (cmd, data, false, fw_err); +} +static int __sev_do_cmd_locked (int cmd,void *data,int *psp_ret) {struct psp_device *psp = psp_master;struct sev_device *sev;unsigned int phys_lsb, phys_msb;unsigned int reg, ret =0 ; +void *cmd_buf;int buf_len;if (!psp || !psp->sev_data)
@@ -484,12 +792,28 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)* work for some memory, e.g. vmalloc'd addresses, and @data may not be * physically contiguous. */ -if (data) -memcpy (sev->cmd_buf, data, buf_len); +if (data) { +if (sev->cmd_buf_active >2 ) +return -EBUSY ; + + cmd_buf = sev->cmd_buf_active ? sev->cmd_buf_backup : sev->cmd_buf; + +memcpy (cmd_buf, data, buf_len); + sev->cmd_buf_active++; + +/* +* The behavior of the SEV-legacy commands is altered when the +* SNP firmware is in the INIT state. +*/ +if (need_firmware_copy (cmd) &&snp_aware_copy_to_firmware (cmd, cmd_buf)) +return -EFAULT ; + }else { + cmd_buf = sev->cmd_buf; + }/* Get the physical address of the command buffer */ - phys_lsb = data ?lower_32_bits (__psp_pa (sev->cmd_buf)) :0 ; - phys_msb = data ?upper_32_bits (__psp_pa (sev->cmd_buf)) :0 ; + phys_lsb = data ?lower_32_bits (__psp_pa (cmd_buf)) :0 ; + phys_msb = data ?upper_32_bits (__psp_pa (cmd_buf)) :0 ;dev_dbg (sev->dev,"sev command id %#x buffer 0x%08x%08x timeout %us\n" , cmd, phys_msb, phys_lsb, psp_timeout);
@@ -532,15 +856,24 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) ret =sev_write_init_ex_file_if_required (cmd); } -print_hex_dump_debug ("(out): " ,DUMP_PREFIX_OFFSET ,16 ,2 , data, - buf_len, false); -/* * Copy potential output from the PSP back to data. Do this even on * failure in case the caller wants to glean something from the error. */ -if (data) -memcpy (data, sev->cmd_buf, buf_len); +if (data) { +/* +* Restore the page state after the command completes. +*/ +if (need_firmware_copy (cmd) && +snp_aware_copy_from_firmware (cmd, cmd_buf, ret)) +return -EFAULT ; + +memcpy (data, cmd_buf, buf_len); + sev->cmd_buf_active--; + } + +print_hex_dump_debug ("(out): " ,DUMP_PREFIX_OFFSET ,16 ,2 , data, + buf_len, false);return ret; }
@@ -624,6 +957,14 @@ static int __sev_platform_init_locked(int *error)dev_err (sev->dev,"SEV-SNP: failed to INIT rc %d, error %#x\n" , rc, *error); } +/* +* Allocate the intermediate buffers used for the legacy command handling. +*/ +if (rc != -ENODEV &&alloc_snp_host_map (sev)) { +dev_notice (sev->dev,"Failed to alloc host map (disabling legacy SEV)\n" ); + goto skip_legacy; + } +if (!sev_es_tmr) {/* Obtain the TMR memory area for SEV-ES use */ sev_es_tmr =sev_fw_alloc (sev_es_tmr_size);
@@ -677,6 +1018,7 @@ static int __sev_platform_init_locked(int *error)dev_info (sev->dev,"SEV API:%d.%d build:%d\n" , sev->api_major, sev->api_minor, sev->build); +skip_legacy:return 0 ; }
@@ -1586,10 +1928,12 @@ int sev_dev_init(struct psp_device *psp)if (!sev) goto e_err; - sev->cmd_buf = (void *)devm_get_free_pages (dev,GFP_KERNEL ,0 ); + sev->cmd_buf = (void *)devm_get_free_pages (dev,GFP_KERNEL ,1 );if (!sev->cmd_buf) goto e_sev; + sev->cmd_buf_backup = (uint8_t *)sev->cmd_buf +PAGE_SIZE ; + psp->sev_data = sev; sev->dev = dev;
@@ -1655,6 +1999,12 @@ static void sev_firmware_shutdown(struct sev_device *sev) snp_range_list =NULL ; } +/* +* The host map need to clear the immutable bit so it must be free'd before the +* SNP firmware shutdown. +*/ +free_snp_host_map (sev); +sev_snp_shutdown (&error); }
@@ -1726,6 +2076,7 @@ void sev_pci_init(void)return ; err: +free_snp_host_map (sev); psp_master->sev_data =NULL ; }
drivers/crypto/ccp/sev-dev.h +12 -0
@@ -29,11 +29,20 @@#define SEV_CMDRESP_CMD_SHIFT 16#define SEV_CMDRESP_IOC BIT(0) +#define MAX_SNP_HOST_MAP_BUFS 2 +struct sev_misc_dev {struct kref refcount;struct miscdevice misc; }; +struct snp_host_map { +u64 paddr; +u32 len; +void *host; +bool active; +}; +struct sev_device {struct device *dev;struct psp_device *psp;
@@ -52,8 +61,11 @@ struct sev_device {u8 build;void *cmd_buf; +void *cmd_buf_backup; +int cmd_buf_active;bool snp_initialized; +struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS ]; };int sev_dev_init (struct psp_device *psp);
Signature · 1 line
2.25.1
RFC PATCH v9 22/51 crypto: ccp: Add the SNP_PLATFORM_STATUS command
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
The command can be used by the userspace to query the SNP platform status report. See the SEV-SNP spec for more details.
--- Documentation/virt/coco/sev-guest.rst | 27 ++++++++++++++++ drivers/crypto/ccp/sev-dev.c | 45 +++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 1 + 3 files changed, 73 insertions(+)
Documentation/virt/coco/sev-guest.rst +27 -0
@@ -61,6 +61,22 @@ counter (e.g. counter overflow), then -EIO will be returned. __u64 fw_err; }; +The host ioctl should be called to /dev/sev device. The ioctl accepts command +id and command input structure. + +:: + struct sev_issue_cmd { + /* Command ID */ + __u32 cmd; + + /* Command request structure */ + __u64 data; + + /* firmware error code on failure (see psp-sev.h) */ + __u32 error; + }; + + 2.1 SNP_GET_REPORT ------------------
@@ -118,6 +134,17 @@ be updated with the expected value. See GHCB specification for further detail on how to parse the certificate blob. +2.4 SNP_PLATFORM_STATUS +----------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_platform_status +:Returns (out): 0 on success, -negative on error + +The SNP_PLATFORM_STATUS command is used to query the SNP platform status. The +status includes API major, minor version and more. See the SEV-SNP +specification for further details. + 3. SEV-SNP CPUID Enforcement ============================
drivers/crypto/ccp/sev-dev.c +45 -0
57 lines not on this page · raw message
include/uapi/linux/psp-sev.h +1 -0
@@ -28,6 +28,7 @@ enum {SEV_PEK_CERT_IMPORT ,SEV_GET_ID ,/* This command is deprecated, use SEV_GET_ID2 */ SEV_GET_ID2 , +SNP_PLATFORM_STATUS , SEV_MAX, };
Signature · 1 line
2.25.1
RFC PATCH v9 23/51 KVM: SEV: Select CONFIG_KVM_PROTECTED_VM when CONFIG_KVM_AMD_SEV=y
In reply to Michael RothAMD SEV relies on the restricted/protected memory support to run guests in some cases (such as SEV lazy-pinning), so make sure to enable that support with the CONFIG_KVM_PROTECTED_VM build option.
--- arch/x86/kvm/Kconfig | 1 + 1 file changed, 1 insertion(+)
arch/x86/kvm/Kconfig +1 -0
@@ -126,6 +126,7 @@ config KVM_AMD_SEV bool "AMD Secure Encrypted Virtualization (SEV) support" depends on KVM_AMD && X86_64 depends on CRYPTO_DEV_SP_PSP && !(KVM_AMD=y && CRYPTO_DEV_CCP_DD=m) + select KVM_PROTECTED_VM help Provides support for launching Encrypted VMs (SEV) and Encrypted VMs with Encrypted State (SEV-ES) on AMD processors.
Signature · 1 line
2.25.1
RFC PATCH v9 24/51 KVM: SVM: Add support to handle AP reset MSR protocol
In reply to Michael RothFrom: Tom Lendacky <[email protected]>
Add support for AP Reset Hold being invoked using the GHCB MSR protocol, available in version 2 of the GHCB specification.
--- arch/x86/include/asm/sev-common.h | 2 ++ arch/x86/kvm/svm/sev.c | 56 ++++++++++++++++++++++++++----- arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 51 insertions(+), 8 deletions(-)
arch/x86/include/asm/sev-common.h +2 -0
@@ -56,6 +56,8 @@/* AP Reset Hold */ #define GHCB_MSR_AP_RESET_HOLD_REQ 0x006#define GHCB_MSR_AP_RESET_HOLD_RESP 0x007 +#define GHCB_MSR_AP_RESET_HOLD_RESULT_POS 12 +#define GHCB_MSR_AP_RESET_HOLD_RESULT_MASK GENMASK_ULL(51, 0)/* GHCB GPA Register */ #define GHCB_MSR_REG_GPA_REQ 0x012
arch/x86/kvm/svm/sev.c +48 -8
85 lines not on this page · raw message
arch/x86/kvm/svm/svm.h +1 -0
@@ -192,6 +192,7 @@ struct vcpu_sev_es_state {struct ghcb *ghcb;struct kvm_host_map ghcb_map;bool received_first_sipi; +unsigned int ap_reset_hold_type;/* SEV-ES scratch area support */ void *ghcb_sa;
Signature · 1 line
2.25.1
RFC PATCH v9 25/51 KVM: SVM: Add GHCB handling for Hypervisor Feature Support requests
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
Version 2 of the GHCB specification introduced advertisement of features that are supported by the Hypervisor.
Now that KVM supports version 2 of the GHCB specification, bump the maximum supported protocol version.
--- arch/x86/include/asm/sev-common.h | 2 ++ arch/x86/kvm/svm/sev.c | 14 ++++++++++++++ arch/x86/kvm/svm/svm.h | 3 ++- 3 files changed, 18 insertions(+), 1 deletion(-)
arch/x86/include/asm/sev-common.h +2 -0
@@ -101,6 +101,8 @@ enum psc_op {/* GHCB Hypervisor Feature Request/Response */ #define GHCB_MSR_HV_FT_REQ 0x080#define GHCB_MSR_HV_FT_RESP 0x081 +#define GHCB_MSR_HV_FT_POS 12 +#define GHCB_MSR_HV_FT_MASK GENMASK_ULL(51, 0)#define GHCB_MSR_HV_FT_RESP_VAL (v) \/* GHCBData[63:12] */ \ (((u64)(v) & GENMASK_ULL(63, 12)) >> 12)
arch/x86/kvm/svm/sev.c +14 -0
@@ -2538,6 +2538,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm)case SVM_VMGEXIT_AP_HLT_LOOP :case SVM_VMGEXIT_AP_JUMP_TABLE :case SVM_VMGEXIT_UNSUPPORTED_EVENT : +case SVM_VMGEXIT_HV_FEATURES :break ;default : reason =GHCB_ERR_INVALID_EVENT ;
@@ -2804,6 +2805,13 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)GHCB_MSR_INFO_MASK , GHCB_MSR_INFO_POS);break ; +case GHCB_MSR_HV_FT_REQ : { +set_ghcb_msr_bits (svm,GHCB_HV_FT_SUPPORTED , +GHCB_MSR_HV_FT_MASK ,GHCB_MSR_HV_FT_POS ); +set_ghcb_msr_bits (svm,GHCB_MSR_HV_FT_RESP , +GHCB_MSR_INFO_MASK ,GHCB_MSR_INFO_POS ); +break ; + }case GHCB_MSR_TERM_REQ : {u64 reason_set, reason_code;
@@ -2928,6 +2936,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ret =1 ;break ; } +case SVM_VMGEXIT_HV_FEATURES : { +ghcb_set_sw_exit_info_2 (ghcb,GHCB_HV_FT_SUPPORTED ); + + ret =1 ; +break ; + }case SVM_VMGEXIT_UNSUPPORTED_EVENT : vcpu_unimpl(vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n",
arch/x86/kvm/svm/svm.h +2 -1
@@ -711,9 +711,10 @@ void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu);/* sev.c */ -#define GHCB_VERSION_MAX 1ULL +#define GHCB_VERSION_MAX 2ULL#define GHCB_VERSION_MIN 1ULL +#define GHCB_HV_FT_SUPPORTED GHCB_HV_FT_SNPextern unsigned int max_sev_asid;
Signature · 1 line
2.25.1
RFC PATCH v9 26/51 KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
Implement a workaround for an SNP erratum where the CPU will incorrectly signal an RMP violation #PF if a hugepage (2mb or 1gb) collides with the RMP entry of a VMCB, VMSA or AVIC backing page.
When SEV-SNP is globally enabled, the CPU marks the VMCB, VMSA, and AVIC backing pages as "in-use" via a reserved bit in the corresponding RMP entry after a successful VMRUN. This is done for _all_ VMs, not just SNP-Active VMs.
If the hypervisor accesses an in-use page through a writable translation, the CPU will throw an RMP violation #PF. On early SNP hardware, if an in-use page is 2mb aligned and software accesses any part of the associated 2mb region with a hupage, the CPU will incorrectly treat the entire 2mb region as in-use and signal a spurious RMP violation #PF.
The recommended is to not use the hugepage for the VMCB, VMSA or AVIC backing page for similar reasons. Add a generic allocator that will ensure that the page returns is not hugepage (2mb or 1gb) and is safe to be used when SEV-SNP is enabled. Also implement similar handling for the VMCB/VMSA pages of nested guests.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/lapic.c | 5 ++++- arch/x86/kvm/svm/nested.c | 2 +- arch/x86/kvm/svm/sev.c | 33 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 17 ++++++++++++--- arch/x86/kvm/svm/svm.h | 1 + 7 files changed, 55 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -135,6 +135,7 @@ KVM_X86_OP(vcpu_deliver_sipi_vector)KVM_X86_OP_OPTIONAL_RET0 (vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL_RET0(gmem_prepare) KVM_X86_OP_OPTIONAL(gmem_invalidate) +KVM_X86_OP_OPTIONAL(alloc_apic_backing_page)#undef KVM_X86_OP#undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1743,6 +1743,7 @@ struct kvm_x86_ops {int (*gmem_prepare)(struct kvm *kvm,struct kvm_memory_slot *slot,kvm_pfn_t pfn,gfn_t gfn,u8 *max_level);void (*gmem_invalidate)(struct kvm *kvm,kvm_pfn_t start,kvm_pfn_t end); +void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); };struct kvm_x86_nested_ops {
arch/x86/kvm/lapic.c +4 -1
@@ -2769,7 +2769,10 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns) vcpu->arch.apic = apic; - apic->regs = (void *)get_zeroed_page (GFP_KERNEL_ACCOUNT ); +if (kvm_x86_ops.alloc_apic_backing_page) + apic->regs =static_call (kvm_x86_alloc_apic_backing_page)(vcpu); +else + apic->regs = (void *)get_zeroed_page (GFP_KERNEL_ACCOUNT );if (!apic->regs) {printk (KERN_ERR "malloc apic regs error for vcpu %x\n" , vcpu->vcpu_id);
arch/x86/kvm/svm/nested.c +1 -1
@@ -1185,7 +1185,7 @@ int svm_allocate_nested(struct vcpu_svm *svm)if (svm->nested.initialized)return 0 ; - vmcb02_page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmcb02_page =snp_safe_alloc_page (&svm->vcpu);if (!vmcb02_page)return -ENOMEM ; svm->nested.vmcb02.ptr =page_address (vmcb02_page);
arch/x86/kvm/svm/sev.c +33 -0
@@ -3104,3 +3104,36 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)break ; } } + +struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu) +{ +unsigned long pfn; +struct page *p; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); + +/* +* Allocate an SNP safe page to workaround the SNP erratum where +* the CPU will incorrectly signal an RMP violation #PF if a +* hugepage (2mb or 1gb) collides with the RMP entry of VMCB, VMSA +* or AVIC backing page. The recommeded workaround is to not use the +* hugepage. +* +* Allocate one extra page, use a page which is not 2mb aligned +* and free the other. +*/ + p =alloc_pages (GFP_KERNEL_ACCOUNT | __GFP_ZERO,1 ); +if (!p) +return NULL ; + +split_page (p,1 ); + + pfn =page_to_pfn (p); +if (IS_ALIGNED (pfn,PTRS_PER_PMD )) +__free_page (p++); +else +__free_page (p +1 ); + +return p; +}
arch/x86/kvm/svm/svm.c +14 -3
@@ -668,7 +668,7 @@ static int svm_cpu_init(int cpu)int ret = -ENOMEM ;memset (sd,0 ,sizeof (struct svm_cpu_data )); - sd->save_area =alloc_page (GFP_KERNEL | __GFP_ZERO); + sd->save_area =snp_safe_alloc_page (NULL );if (!sd->save_area)return ret;
@@ -1381,7 +1381,7 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu) svm =to_svm (vcpu); err = -ENOMEM ; - vmcb01_page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmcb01_page =snp_safe_alloc_page (vcpu);if (!vmcb01_page) goto out;
@@ -1390,7 +1390,7 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu)* SEV-ES guests require a separate VMSA page used to contain * the encrypted register state of the guest. */ - vmsa_page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmsa_page =snp_safe_alloc_page (vcpu);if (!vmsa_page) goto error_free_vmcb_page;
@@ -4770,6 +4770,16 @@ static int svm_vm_init(struct kvm *kvm)return 0 ; } +static void *svm_alloc_apic_backing_page (struct kvm_vcpu *vcpu) +{ +struct page *page =snp_safe_alloc_page (vcpu); + +if (!page) +return NULL ; + +return page_address (page); +} +static struct kvm_x86_ops svm_x86_ops__initdata = { .name =KBUILD_MODNAME ,
@@ -4900,6 +4910,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, .vcpu_get_apicv_inhibit_reasons = avic_vcpu_get_apicv_inhibit_reasons, + .alloc_apic_backing_page = svm_alloc_apic_backing_page, };/*
arch/x86/kvm/svm/svm.h +1 -0
@@ -741,6 +741,7 @@ void sev_es_vcpu_reset(struct vcpu_svm *svm);void sev_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector);void sev_es_prepare_switch_to_guest (struct sev_es_save_area *hostsa);void sev_es_unmap_ghcb (struct vcpu_svm *svm); +struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu);/* vmenter.S */
Signature · 1 line
2.25.1
RFC PATCH v9 27/51 KVM: SVM: Add initial SEV-SNP support
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
The next generation of SEV is called SEV-SNP (Secure Nested Paging). SEV-SNP builds upon existing SEV and SEV-ES functionality while adding new hardware based security protection. SEV-SNP adds strong memory encryption integrity protection to help prevent malicious hypervisor-based attacks such as data replay, memory re-mapping, and more, to create an isolated execution environment.
The SNP feature is added incrementally, the later patches adds a new module parameters that can be used to enabled SEV-SNP in the KVM.
--- arch/x86/kvm/svm/sev.c | 10 +++++++++- arch/x86/kvm/svm/svm.h | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/sev.c +9 -1
@@ -58,6 +58,9 @@ module_param_named(sev_es, sev_es_enabled, bool, 0444);#define sev_es_enabled false#endif /* CONFIG_KVM_AMD_SEV */ +/* enable/disable SEV-SNP support */ +static bool sev_snp_enabled; +#define AP_RESET_HOLD_NONE 0#define AP_RESET_HOLD_NAE_EVENT 1#define AP_RESET_HOLD_MSR_PROTO 2
@@ -2169,6 +2172,7 @@ void __init sev_hardware_setup(void) {#ifdef CONFIG_KVM_AMD_SEV unsigned int eax, ebx, ecx, edx, sev_asid_count, sev_es_asid_count; +bool sev_snp_supported = false;bool sev_es_supported = false;bool sev_supported = false;
@@ -2248,12 +2252,16 @@ void __init sev_hardware_setup(void)if (misc_cg_set_capacity (MISC_CG_RES_SEV_ES , sev_es_asid_count)) goto out; -pr_info ("SEV-ES supported: %u ASIDs\n" , sev_es_asid_count); sev_es_supported = true; + sev_snp_supported = sev_snp_enabled &&cpu_feature_enabled (X86_FEATURE_SEV_SNP ); + +pr_info ("SEV-ES %ssupported: %u ASIDs\n" , + sev_snp_supported ?"and SEV-SNP " :"" , sev_es_asid_count); out: sev_enabled = sev_supported; sev_es_enabled = sev_es_supported; + sev_snp_enabled = sev_snp_supported;#endif }
arch/x86/kvm/svm/svm.h +8 -0
@@ -77,6 +77,7 @@ enum {struct kvm_sev_info {bool active;/* SEV enabled guest */ bool es_active;/* SEV-ES enabled guest */ +bool snp_active;/* SEV-SNP enabled guest */ unsigned int asid;/* ASID used for this guest */ unsigned int handle;/* SEV firmware handle */ int fd;/* SEV device fd */
@@ -346,6 +347,13 @@ static __always_inline bool sev_es_guest(struct kvm *kvm)#endif } +static __always_inline bool sev_snp_guest(struct kvm *kvm) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; + +return sev_es_guest (kvm) && sev->snp_active; +} +static inline void vmcb_mark_all_dirty (struct vmcb *vmcb) { vmcb->control.clean =0 ;
Signature · 1 line
2.25.1
RFC PATCH v9 28/51 KVM: SVM: Add KVM_SNP_INIT command
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
The KVM_SNP_INIT command is used by the hypervisor to initialize the SEV-SNP platform context. In a typical workflow, this command should be the first command issued. When creating SEV-SNP guest, the VMM must use this command instead of the KVM_SEV_INIT or KVM_SEV_ES_INIT.
The flags value must be zero, it will be extended in future SNP support to communicate the optional features (such as restricted INT injection etc).
--- .../virt/kvm/x86/amd-memory-encryption.rst | 27 +++++++++++++ arch/x86/include/asm/svm.h | 1 + arch/x86/kvm/svm/sev.c | 39 ++++++++++++++++++- arch/x86/kvm/svm/svm.h | 4 ++ include/uapi/linux/kvm.h | 13 +++++++ 5 files changed, 83 insertions(+), 1 deletion(-)
Documentation/virt/kvm/x86/amd-memory-encryption.rst +27 -0
@@ -434,6 +434,33 @@ issued by the hypervisor to make the guest ready for execution. Returns: 0 on success, -negative on error +18. KVM_SNP_INIT +---------------- + +The KVM_SNP_INIT command can be used by the hypervisor to initialize SEV-SNP +context. In a typical workflow, this command should be the first command issued. + +Parameters (in/out): struct kvm_snp_init + +Returns: 0 on success, -negative on error + +:: + + struct kvm_snp_init { + __u64 flags; + }; + +The flags bitmap is defined as:: + + /* enable the restricted injection */ + #define KVM_SEV_SNP_RESTRICTED_INJET (1<<0) + + /* enable the restricted injection timer */ + #define KVM_SEV_SNP_RESTRICTED_TIMER_INJET (1<<1) + +If the specified flags is not supported then return -EOPNOTSUPP, and the supported +flags are returned. + References ==========
arch/x86/include/asm/svm.h +1 -0
@@ -288,6 +288,7 @@ static_assert((X2AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == X2AVIC_#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) +#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0)struct vmcb_seg {u16 selector;
arch/x86/kvm/svm/sev.c +38 -1
@@ -243,6 +243,25 @@ static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)sev_decommission (handle); } +static int verify_snp_init_flags (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_snp_init params; +int ret =0 ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params))) +return -EFAULT ; + +if (params.flags & ~SEV_SNP_SUPPORTED_FLAGS ) + ret = -EOPNOTSUPP ; + + params.flags =SEV_SNP_SUPPORTED_FLAGS ; + +if (copy_to_user ((void __user *)(uintptr_t )argp->data, ¶ms,sizeof (params))) + ret = -EFAULT ; + +return ret; +} +static int sev_guest_init (struct kvm *kvm,struct kvm_sev_cmd *argp) {struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;
@@ -256,12 +275,19 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; sev->active = true; - sev->es_active = argp->id ==KVM_SEV_ES_INIT ; + sev->es_active = (argp->id ==KVM_SEV_ES_INIT || argp->id ==KVM_SEV_SNP_INIT ); + sev->snp_active = argp->id ==KVM_SEV_SNP_INIT ; asid =sev_asid_new (sev);if (asid <0 ) goto e_no_asid; sev->asid = asid; +if (sev->snp_active) { + ret =verify_snp_init_flags (kvm, argp); +if (ret) + goto e_free; + } + ret =sev_platform_init (&argp->error);if (ret) goto e_free;
@@ -277,6 +303,7 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)sev_asid_free (sev); sev->asid =0 ; e_no_asid: + sev->snp_active = false; sev->es_active = false; sev->active = false;return ret;
@@ -612,6 +639,10 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) save->xss = svm->vcpu.arch.ia32_xss; save->dr6 = svm->vcpu.arch.dr6; +/* Enable the SEV-SNP feature */ +if (sev_snp_guest (svm->vcpu.kvm)) + save->sev_features |=SVM_SEV_FEAT_SNP_ACTIVE ; +pr_debug ("Virtual Machine Save Area (VMSA):\n" );print_hex_dump_debug ("" ,DUMP_PREFIX_NONE ,16 ,1 , save,sizeof (*save), false);
@@ -1864,6 +1895,12 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) }switch (sev_cmd.id) { +case KVM_SEV_SNP_INIT : +if (!sev_snp_enabled) { + r = -ENOTTY ; + goto out; + } + fallthrough;case KVM_SEV_ES_INIT :if (!sev_es_enabled) { r = -ENOTTY ;
arch/x86/kvm/svm/svm.h +4 -0
@@ -74,6 +74,9 @@ enum {/* TPR and CR2 are always written before VMRUN */ #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2)) +/* Supported init feature flags */ +#define SEV_SNP_SUPPORTED_FLAGS 0x0 +struct kvm_sev_info {bool active;/* SEV enabled guest */ bool es_active;/* SEV-ES enabled guest */
@@ -89,6 +92,7 @@ struct kvm_sev_info {struct list_head mirror_entry;/* Use as a list entry of mirrors */ struct misc_cg *misc_cg;/* For misc cgroup accounting */ atomic_t migration_in_progress; +u64 snp_init_flags; };struct kvm_svm {
include/uapi/linux/kvm.h +13 -0
@@ -1926,6 +1926,9 @@ enum sev_cmd_id {/* Guest Migration Extension */ KVM_SEV_SEND_CANCEL , +/* SNP specific commands */ +KVM_SEV_SNP_INIT , + KVM_SEV_NR_MAX, };
@@ -2022,6 +2025,16 @@ struct kvm_sev_receive_update_data {__u32 trans_len; }; +/* enable the restricted injection */ +#define KVM_SEV_SNP_RESTRICTED_INJET (1 << 0) + +/* enable the restricted injection timer */ +#define KVM_SEV_SNP_RESTRICTED_TIMER_INJET (1 << 1) + +struct kvm_snp_init { +__u64 flags; +}; +#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2)
Signature · 1 line
2.25.1
RFC PATCH v9 29/51 KVM: SVM: Add KVM_SEV_SNP_LAUNCH_START command
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
KVM_SEV_SNP_LAUNCH_START begins the launch process for an SEV-SNP guest. The command initializes a cryptographic digest context used to construct the measurement of the guest. If the guest is expected to be migrated, the command also binds a migration agent (MA) to the guest.
For more information see the SEV-SNP specification.
--- .../virt/kvm/x86/amd-memory-encryption.rst | 24 ++++ arch/x86/kvm/svm/sev.c | 126 +++++++++++++++++- arch/x86/kvm/svm/svm.h | 1 + include/uapi/linux/kvm.h | 10 ++ 4 files changed, 158 insertions(+), 3 deletions(-)
Documentation/virt/kvm/x86/amd-memory-encryption.rst +24 -0
@@ -461,6 +461,30 @@ The flags bitmap is defined as:: If the specified flags is not supported then return -EOPNOTSUPP, and the supported flags are returned. +19. KVM_SNP_LAUNCH_START +------------------------ + +The KVM_SNP_LAUNCH_START command is used for creating the memory encryption +context for the SEV-SNP guest. To create the encryption context, user must +provide a guest policy, migration agent (if any) and guest OS visible +workarounds value as defined SEV-SNP specification. + +Parameters (in): struct kvm_snp_launch_start + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_launch_start { + __u64 policy; /* Guest policy to use. */ + __u64 ma_uaddr; /* userspace address of migration agent */ + __u8 ma_en; /* 1 if the migration agent is enabled */ + __u8 imi_en; /* set IMI to 1. */ + __u8 gosvw[16]; /* guest OS visible workarounds */ + }; + +See the SEV-SNP specification for further detail on the launch input. + References ==========
arch/x86/kvm/svm/sev.c +123 -3
172 lines not on this page · raw message
arch/x86/kvm/svm/svm.h +1 -0
@@ -93,6 +93,7 @@ struct kvm_sev_info {struct misc_cg *misc_cg;/* For misc cgroup accounting */ atomic_t migration_in_progress;u64 snp_init_flags; +void *snp_context;/* SNP guest context page */ };struct kvm_svm {
include/uapi/linux/kvm.h +10 -0
@@ -1928,6 +1928,7 @@ enum sev_cmd_id {/* SNP specific commands */ KVM_SEV_SNP_INIT , +KVM_SEV_SNP_LAUNCH_START , KVM_SEV_NR_MAX, };
@@ -2035,6 +2036,15 @@ struct kvm_snp_init {__u64 flags; }; +struct kvm_sev_snp_launch_start { +__u64 policy; +__u64 ma_uaddr; +__u8 ma_en; +__u8 imi_en; +__u8 gosvw[16 ]; +__u8 pad[6 ]; +}; +#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2)
Signature · 1 line
2.25.1
Re: [PATCH RFC v9 29/51] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_START command
Quoted 7 lines
arch/x86/kvm/svm/sev.c +7
-0+ +static int snp_launch_start (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct sev_data_snp_launch_start start = {0 }; +struct kvm_sev_snp_launch_start params; +int rc;
arch/x86/kvm/svm/sev.c int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd) · line 1934
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ +if (!sev_snp_guest (kvm)) +return -ENOTTY ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params))) +return -EFAULT ; + + sev->snp_context =snp_context_create (kvm, argp); +if (!sev->snp_context) +return -ENOTTY ;
I commented on a previous series but I think the bug is still here. I think users can repeatedly call KVM_SEV_SNP_LAUNCH_START to have KVM keep allocating more snp_contexts above.
Should we check if the VM already has a |snp_context| and error out if so?
Quoted 22 lines
arch/x86/kvm/svm/sev.c +21
-0+ + start.gctx_paddr =__psp_pa (sev->snp_context); + start.policy = params.policy; +memcpy (start.gosvw, params.gosvw,sizeof (params.gosvw)); + rc =__sev_issue_cmd (argp->sev_fd,SEV_CMD_SNP_LAUNCH_START , &start, &argp->error); +if (rc) + goto e_free_context; + + sev->fd = argp->sev_fd; + rc =snp_bind_asid (kvm, &argp->error); +if (rc) + goto e_free_context; + +return 0 ; + +e_free_context: +snp_decommission_context (kvm); + +return rc; +} +
RFC PATCH v9 30/51 KVM: Add HVA range operator
In reply to Michael RothFrom: Vishal Annapurve <[email protected]>
Introduce HVA range operator so that other KVM subsystems can operate on HVA range.
--- include/linux/kvm_host.h | 6 +++++ virt/kvm/kvm_main.c | 49 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+)
include/linux/kvm_host.h +6 -0
@@ -1400,6 +1400,12 @@ void kvm_mmu_invalidate_begin(struct kvm *kvm);void kvm_mmu_invalidate_range_add (struct kvm *kvm,gfn_t start,gfn_t end);void kvm_mmu_invalidate_end (struct kvm *kvm); +typedef int (*kvm_hva_range_op_t )(struct kvm *kvm, +struct kvm_gfn_range *range,void *data); + +int kvm_vm_do_hva_range_op (struct kvm *kvm,unsigned long hva_start, +unsigned long hva_end,kvm_hva_range_op_t handler,void *data); +long kvm_arch_dev_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg); long kvm_arch_vcpu_ioctl(struct file *filp,
virt/kvm/kvm_main.c +49 -0
55 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC PATCH v9 31/51 KVM: Split out memory attribute xarray updates to helper function
In reply to Michael RothThis will be useful to other callers that need to update memory attributes for things like setting up the initial private memory payload for a guest.
--- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-)
include/linux/kvm_host.h +1 -0
@@ -991,6 +991,7 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module);void kvm_exit (void );void kvm_get_kvm (struct kvm *kvm); +int kvm_vm_set_region_attr (struct kvm *kvm,gfn_t start,gfn_t end,u64 attributes);bool kvm_get_kvm_safe (struct kvm *kvm);void kvm_put_kvm (struct kvm *kvm);bool file_is_kvm (struct file *file);
virt/kvm/kvm_main.c +18 -8
@@ -2466,12 +2466,28 @@ static void kvm_mem_attrs_changed(struct kvm *kvm, unsigned long attrs,kvm_flush_remote_tlbs (kvm); } +int kvm_vm_set_region_attr (struct kvm *kvm,gfn_t start,gfn_t end, +u64 attributes) +{ +gfn_t index; +void *entry; + + entry = attributes ?xa_mk_value (attributes) :NULL ; + +for (index = start; index < end; index++) +if (xa_err (xa_store (&kvm->mem_attr_array, index, entry, +GFP_KERNEL_ACCOUNT ))) +break ; + +return index; +} +EXPORT_SYMBOL_GPL (kvm_vm_set_region_attr); +static int kvm_vm_ioctl_set_mem_attributes (struct kvm *kvm,struct kvm_memory_attributes *attrs) {gfn_t start, end;unsigned long i; -void *entry;/* flags is currently not used. */ if (attrs->flags)
@@ -2486,8 +2502,6 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, start = attrs->address >>PAGE_SHIFT ; end = (attrs->address + attrs->size -1 +PAGE_SIZE ) >>PAGE_SHIFT ; - entry = attrs->attributes ?xa_mk_value (attrs->attributes) :NULL ; -mutex_lock (&kvm->slots_lock);KVM_MMU_LOCK (kvm);
@@ -2495,11 +2509,7 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm,kvm_mmu_invalidate_range_add (kvm, start, end);KVM_MMU_UNLOCK (kvm); -for (i = start; i < end; i++) { -if (xa_err (xa_store (&kvm->mem_attr_array, i, entry, -GFP_KERNEL_ACCOUNT ))) -break ; - } + i =kvm_vm_set_region_attr (kvm, start, end, attrs->attributes);KVM_MMU_LOCK (kvm);if (i > start)
Signature · 1 line
2.25.1
RFC PATCH v9 32/51 KVM: SVM: Add KVM_SEV_SNP_LAUNCH_UPDATE command
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
The KVM_SEV_SNP_LAUNCH_UPDATE command can be used to insert data into the guest's memory. The data is encrypted with the cryptographic context created with the KVM_SEV_SNP_LAUNCH_START.
In addition to the inserting data, it can insert a two special pages into the guests memory: the secrets page and the CPUID page.
While terminating the guest, reclaim the guest pages added in the RMP table. If the reclaim fails, then the page is no longer safe to be released back to the system and leak them.
For more information see the SEV-SNP specification.
--- .../virt/kvm/x86/amd-memory-encryption.rst | 28 +++ arch/x86/kvm/svm/sev.c | 189 ++++++++++++++++++ include/uapi/linux/kvm.h | 19 ++ 3 files changed, 236 insertions(+)
Documentation/virt/kvm/x86/amd-memory-encryption.rst +28 -0
@@ -485,6 +485,34 @@ Returns: 0 on success, -negative on error See the SEV-SNP specification for further detail on the launch input. +20. KVM_SNP_LAUNCH_UPDATE +------------------------- + +The KVM_SNP_LAUNCH_UPDATE is used for encrypting a memory region. It also +calculates a measurement of the memory contents. The measurement is a signature +of the memory contents that can be sent to the guest owner as an attestation +that the memory was encrypted correctly by the firmware. + +Parameters (in): struct kvm_snp_launch_update + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_launch_update { + __u64 start_gfn; /* Guest page number to start from. */ + __u64 uaddr; /* userspace address need to be encrypted */ + __u32 len; /* length of memory region */ + __u8 imi_page; /* 1 if memory is part of the IMI */ + __u8 page_type; /* page type */ + __u8 vmpl3_perms; /* VMPL3 permission mask */ + __u8 vmpl2_perms; /* VMPL2 permission mask */ + __u8 vmpl1_perms; /* VMPL1 permission mask */ + }; + +See the SEV-SNP spec for further details on how to build the VMPL permission +mask and page type. + References ==========
arch/x86/kvm/svm/sev.c +189 -0
207 lines not on this page · raw message
include/uapi/linux/kvm.h +19 -0
@@ -1929,6 +1929,7 @@ enum sev_cmd_id {/* SNP specific commands */ KVM_SEV_SNP_INIT ,KVM_SEV_SNP_LAUNCH_START , +KVM_SEV_SNP_LAUNCH_UPDATE , KVM_SEV_NR_MAX, };
@@ -2045,6 +2046,24 @@ struct kvm_sev_snp_launch_start {__u8 pad[6 ]; }; +#define KVM_SEV_SNP_PAGE_TYPE_NORMAL 0x1 +#define KVM_SEV_SNP_PAGE_TYPE_VMSA 0x2 +#define KVM_SEV_SNP_PAGE_TYPE_ZERO 0x3 +#define KVM_SEV_SNP_PAGE_TYPE_UNMEASURED 0x4 +#define KVM_SEV_SNP_PAGE_TYPE_SECRETS 0x5 +#define KVM_SEV_SNP_PAGE_TYPE_CPUID 0x6 + +struct kvm_sev_snp_launch_update { +__u64 start_gfn; +__u64 uaddr; +__u32 len; +__u8 imi_page; +__u8 page_type; +__u8 vmpl3_perms; +__u8 vmpl2_perms; +__u8 vmpl1_perms; +}; +#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2)
Signature · 1 line
2.25.1
RFC PATCH v9 33/51 KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
The KVM_SEV_SNP_LAUNCH_FINISH finalize the cryptographic digest and stores it as the measurement of the guest at launch.
While finalizing the launch flow, it also issues the LAUNCH_UPDATE command to encrypt the VMSA pages.
If its an SNP guest, then VMSA was added in the RMP entry as a guest owned page and also removed from the kernel direct map so flush it later after it is transitioned back to hypervisor state and restored in the direct map.
--- .../virt/kvm/x86/amd-memory-encryption.rst | 24 +++ arch/x86/kvm/svm/sev.c | 146 ++++++++++++++++++ include/uapi/linux/kvm.h | 14 ++ 3 files changed, 184 insertions(+)
Documentation/virt/kvm/x86/amd-memory-encryption.rst +24 -0
@@ -513,6 +513,30 @@ Returns: 0 on success, -negative on error See the SEV-SNP spec for further details on how to build the VMPL permission mask and page type. +21. KVM_SNP_LAUNCH_FINISH +------------------------- + +After completion of the SNP guest launch flow, the KVM_SNP_LAUNCH_FINISH command can be +issued to make the guest ready for the execution. + +Parameters (in): struct kvm_sev_snp_launch_finish + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_launch_finish { + __u64 id_block_uaddr; + __u64 id_auth_uaddr; + __u8 id_block_en; + __u8 auth_key_en; + __u8 host_data[32]; + __u8 pad[6]; + }; + + +See SEV-SNP specification for further details on launch finish input parameters. + References ==========
arch/x86/kvm/svm/sev.c +146 -0
182 lines not on this page · raw message
include/uapi/linux/kvm.h +14 -0
@@ -1930,6 +1930,7 @@ enum sev_cmd_id {KVM_SEV_SNP_INIT ,KVM_SEV_SNP_LAUNCH_START ,KVM_SEV_SNP_LAUNCH_UPDATE , +KVM_SEV_SNP_LAUNCH_FINISH , KVM_SEV_NR_MAX, };
@@ -2064,6 +2065,19 @@ struct kvm_sev_snp_launch_update {__u8 vmpl1_perms; }; +#define KVM_SEV_SNP_ID_BLOCK_SIZE 96 +#define KVM_SEV_SNP_ID_AUTH_SIZE 4096 +#define KVM_SEV_SNP_FINISH_DATA_SIZE 32 + +struct kvm_sev_snp_launch_finish { +__u64 id_block_uaddr; +__u64 id_auth_uaddr; +__u8 id_block_en; +__u8 auth_key_en; +__u8 host_data[KVM_SEV_SNP_FINISH_DATA_SIZE ]; +__u8 pad[6 ]; +}; +#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2)
Signature · 1 line
2.25.1
RFC PATCH v9 34/51 KVM: SVM: Add support to handle GHCB GPA register VMGEXIT
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
SEV-SNP guests are required to perform a GHCB GPA registration. Before using a GHCB GPA for a vCPU the first time, a guest must register the vCPU GHCB GPA. If hypervisor can work with the guest requested GPA then it must respond back with the same GPA otherwise return -1.
On VMEXIT, Verify that GHCB GPA matches with the registered value. If a mismatch is detected then abort the guest.
--- arch/x86/include/asm/sev-common.h | 8 ++++++++ arch/x86/kvm/svm/sev.c | 27 +++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 7 +++++++ 3 files changed, 42 insertions(+)
arch/x86/include/asm/sev-common.h +8 -0
@@ -59,6 +59,14 @@#define GHCB_MSR_AP_RESET_HOLD_RESULT_POS 12#define GHCB_MSR_AP_RESET_HOLD_RESULT_MASK GENMASK_ULL(51, 0) +/* Preferred GHCB GPA Request */ +#define GHCB_MSR_PREF_GPA_REQ 0x010 +#define GHCB_MSR_GPA_VALUE_POS 12 +#define GHCB_MSR_GPA_VALUE_MASK GENMASK_ULL(51, 0) + +#define GHCB_MSR_PREF_GPA_RESP 0x011 +#define GHCB_MSR_PREF_GPA_NONE 0xfffffffffffff +/* GHCB GPA Register */ #define GHCB_MSR_REG_GPA_REQ 0x012#define GHCB_MSR_REG_GPA_REQ_VAL (v) \
arch/x86/kvm/svm/sev.c +27 -0
@@ -3312,6 +3312,27 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS);break ; } +case GHCB_MSR_PREF_GPA_REQ : { +set_ghcb_msr_bits (svm,GHCB_MSR_PREF_GPA_NONE ,GHCB_MSR_GPA_VALUE_MASK , +GHCB_MSR_GPA_VALUE_POS ); +set_ghcb_msr_bits (svm,GHCB_MSR_PREF_GPA_RESP ,GHCB_MSR_INFO_MASK , +GHCB_MSR_INFO_POS ); +break ; + } +case GHCB_MSR_REG_GPA_REQ : { +u64 gfn; + + gfn =get_ghcb_msr_bits (svm,GHCB_MSR_GPA_VALUE_MASK , +GHCB_MSR_GPA_VALUE_POS ); + + svm->sev_es.ghcb_registered_gpa =gfn_to_gpa (gfn); + +set_ghcb_msr_bits (svm, gfn,GHCB_MSR_GPA_VALUE_MASK , +GHCB_MSR_GPA_VALUE_POS ); +set_ghcb_msr_bits (svm,GHCB_MSR_REG_GPA_RESP ,GHCB_MSR_INFO_MASK , +GHCB_MSR_INFO_POS ); +break ; + }case GHCB_MSR_TERM_REQ : {u64 reason_set, reason_code;
@@ -3378,6 +3399,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) exit_code =ghcb_get_sw_exit_code (ghcb); +/* SEV-SNP guest requires that the GHCB GPA must be registered */ +if (sev_snp_guest (svm->vcpu.kvm) && !ghcb_gpa_is_registered (svm, ghcb_gpa)) { +vcpu_unimpl (&svm->vcpu,"vmgexit: GHCB GPA [%#llx] is not registered.\n" , ghcb_gpa); +return -EINVAL ; + } + ret =sev_es_validate_vmgexit (svm);if (ret)return ret;
arch/x86/kvm/svm/svm.h +7 -0
@@ -205,6 +205,8 @@ struct vcpu_sev_es_state {u32 ghcb_sa_len;bool ghcb_sa_sync;bool ghcb_sa_free; + +u64 ghcb_registered_gpa; };struct vcpu_svm {
@@ -359,6 +361,11 @@ static __always_inline bool sev_snp_guest(struct kvm *kvm)return sev_es_guest (kvm) && sev->snp_active; } +static inline bool ghcb_gpa_is_registered (struct vcpu_svm *svm,u64 val) +{ +return svm->sev_es.ghcb_registered_gpa == val; +} +static inline void vmcb_mark_all_dirty (struct vmcb *vmcb) { vmcb->control.clean =0 ;
Signature · 1 line
2.25.1
RFC PATCH v9 35/51 KVM: SVM: Add KVM_EXIT_VMGEXIT
In reply to Michael RothFor private memslots, GHCB page state change requests will be forwarded to userspace for processing. Define a new KVM_EXIT_VMGEXIT for exits of this type, as well as other potential userspace handling for VMGEXITs in the future.
--- Documentation/virt/kvm/api.rst | 34 ++++++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 6 ++++++ 2 files changed, 40 insertions(+)
Documentation/virt/kvm/api.rst +34 -0
@@ -6780,6 +6780,40 @@ Please note that the kernel is allowed to use the kvm_run structure as the primary storage for certain register types. Therefore, the kernel may use the values in kvm_run even if the corresponding bit in kvm_dirty_regs is not set. +:: + + /* KVM_EXIT_VMGEXIT */ + struct { + __u64 ghcb_msr; /* GHCB MSR contents */ + __u64 ret; /* user -> kernel return value */ + } memory; + +If exit reason is KVM_EXIT_VMGEXIT then it indicates that an SEV-SNP guest has +issued a VMGEXIT instruction (as documented by the AMD Architecture +Programmer's Manual (APM)) to the hypervisor that needs to be serviced by +userspace. This is generally handled via the Guest-Hypervisor Communication +Block (GHCB) specification. The value of 'ghcb_msr' will be the contents of +the GHCB MSR register at the time of the VMGEXIT, which can either be the GPA +of the GHCB page for page-based GHCB requests, or an encoding of an MSR-based +GHCB request. The mechanism to distinguish between these two and determine the +type of request is the same as what is documented in the GHCB specification. + +Not all VMGEXITs or GHCB requests will be forwarded to userspace. Currently +this will only be the case for "SNP Page State Change" requests (PSCs), and +only for the subset of these which involve actual shared <-> private +transition. Userspace is expected to process these requests in accordance +with the GHCB specification and issue KVM_SET_MEMORY_ATTRIBUTE ioctls to +perform the shared/private transitions. + +GHCB page-based PSC requests require returning a 64-bit return value to the +guest via the SW_EXITINFO2 field of the vCPU's VMCB structure, as documented +in the GHCB. Userspace must set 'ret' to what the GHCB specification documents +the SW_EXITINFO2 VMCB field should be set to after processing a PSC request. + +For MSR-based PSC requests, userspace must set the value of 'ghcb_msr' to be +the same as what the GHCB specification documents the actual GHCB MSR register +should be set to after processing a PSC request. + 6. Capabilities that can be enabled on vCPUs ============================================
include/uapi/linux/kvm.h +6 -0
@@ -279,6 +279,7 @@ struct kvm_xen_exit {#define KVM_EXIT_RISCV_CSR 36#define KVM_EXIT_NOTIFY 37#define KVM_EXIT_MEMORY_FAULT 38 +#define KVM_EXIT_VMGEXIT 50/* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */
@@ -527,6 +528,11 @@ struct kvm_run {__u64 gpa;__u64 size; } memory; +/* KVM_EXIT_VMGEXIT */ +struct { +__u64 ghcb_msr;/* GHCB MSR contents */ +__u64 ret;/* user -> kernel */ + } vmgexit;/* Fix the size of the union. */ char padding[256 ]; };
Signature · 1 line
2.25.1
RFC PATCH v9 36/51 KVM: SVM: Add support to handle MSR based Page State Change VMGEXIT
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
SEV-SNP VMs can ask the hypervisor to change the page state in the RMP table to be private or shared using the Page State Change MSR protocol as defined in the GHCB specification.
When using gmem, private/shared memory is allocated through separate pools, and KVM relies on userspace issuing a KVM_SET_MEMORY_ATTRIBUTES KVM ioctl to tell KVM MMU whether or not a particular GFN should be backed by private memory or not.
Forward these page state change requests to userspace so that it can issue the expected KVM ioctls. The KVM MMU will handle updating the RMP entries when it is ready to map a private page into a guest.
--- arch/x86/kvm/svm/sev.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
arch/x86/kvm/svm/sev.c +16 -0
@@ -3233,6 +3233,15 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) svm->vmcb->control.ghcb_gpa = value; } +static int snp_complete_psc_msr_protocol (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); + +set_ghcb_msr (svm, vcpu->run->vmgexit.ghcb_msr); + +return 1 ;/* resume */ +} +static int sev_handle_vmgexit_msr_protocol (struct vcpu_svm *svm) {struct vmcb_control_area *control = &svm->vmcb->control;
@@ -3333,6 +3342,13 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_POS);break ; } +case GHCB_MSR_PSC_REQ : + vcpu->run->exit_reason =KVM_EXIT_VMGEXIT ; + vcpu->run->vmgexit.ghcb_msr = control->ghcb_gpa; + vcpu->arch.complete_userspace_io = snp_complete_psc_msr_protocol; + + ret =-1 ; +break ;case GHCB_MSR_TERM_REQ : {u64 reason_set, reason_code;
Signature · 1 line
2.25.1
RFC PATCH v9 37/51 KVM: SVM: Add support to handle Page State Change VMGEXIT
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
SEV-SNP VMs can ask the hypervisor to change the page state in the RMP table to be private or shared using the Page State Change NAE event as defined in the GHCB specification version 2.
Forward these requests to userspace as KVM_EXIT_VMGEXITs, similar to how it is done for requests that don't use a GHCB page.
--- arch/x86/kvm/svm/sev.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
arch/x86/kvm/svm/sev.c +16 -0
@@ -3039,6 +3039,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm)case SVM_VMGEXIT_AP_JUMP_TABLE :case SVM_VMGEXIT_UNSUPPORTED_EVENT :case SVM_VMGEXIT_HV_FEATURES : +case SVM_VMGEXIT_PSC :break ;default : reason =GHCB_ERR_INVALID_EVENT ;
@@ -3242,6 +3243,15 @@ static int snp_complete_psc_msr_protocol(struct kvm_vcpu *vcpu)return 1 ;/* resume */ } +static int snp_complete_psc (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); + +ghcb_set_sw_exit_info_2 (svm->sev_es.ghcb, vcpu->run->vmgexit.ret); + +return 1 ;/* resume */ +} +static int sev_handle_vmgexit_msr_protocol (struct vcpu_svm *svm) {struct vmcb_control_area *control = &svm->vmcb->control;
@@ -3485,6 +3495,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ret =1 ;break ; } +case SVM_VMGEXIT_PSC : +/* Let userspace handling allocating/deallocating backing pages. */ + vcpu->run->exit_reason =KVM_EXIT_VMGEXIT ; + vcpu->run->vmgexit.ghcb_msr = ghcb_gpa; + vcpu->arch.complete_userspace_io = snp_complete_psc; +break ;case SVM_VMGEXIT_UNSUPPORTED_EVENT : vcpu_unimpl(vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n",
Signature · 1 line
2.25.1
RFC PATCH v9 38/51 KVM: x86: Export the kvm_zap_gfn_range() for the SNP use
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
While resolving the RMP page fault, there may be cases where the page level between the RMP entry and TDP does not match and the 2M RMP entry must be split into 4K RMP entries. Or a 2M TDP page need to be broken into multiple of 4K pages.
To keep the RMP and TDP page level in sync, zap the gfn range after splitting the pages in the RMP entry. The zap should force the TDP to gets rebuilt with the new page level.
--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu.h | 2 -- arch/x86/kvm/mmu/mmu.c | 1 + 3 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1851,6 +1851,8 @@ void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm,void kvm_mmu_zap_all (struct kvm *kvm);void kvm_mmu_invalidate_mmio_sptes (struct kvm *kvm,u64 gen);void kvm_mmu_change_mmu_pages (struct kvm *kvm,unsigned long kvm_nr_mmu_pages); +void kvm_zap_gfn_range (struct kvm *kvm,gfn_t gfn_start,gfn_t gfn_end); +int load_pdptrs (struct kvm_vcpu *vcpu,unsigned long cr3);
arch/x86/kvm/mmu.h +0 -2
@@ -235,8 +235,6 @@ static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,return -(u32 )fault & errcode; } -void kvm_zap_gfn_range (struct kvm *kvm,gfn_t gfn_start,gfn_t gfn_end); -int kvm_arch_write_log_dirty (struct kvm_vcpu *vcpu);int kvm_mmu_post_init_vm (struct kvm *kvm);
arch/x86/kvm/mmu/mmu.c +1 -0
@@ -6732,6 +6732,7 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm,return need_tlb_flush; } +EXPORT_SYMBOL_GPL (kvm_zap_gfn_range);static void kvm_rmap_zap_collapsible_sptes (struct kvm *kvm,const struct kvm_memory_slot *slot)
Signature · 1 line
2.25.1
RFC PATCH v9 39/51 KVM: x86: Define RMP page fault error bits for #NPF
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
When SEV-SNP is enabled globally, the hardware places restrictions on all memory accesses based on the RMP entry, whether the hypervisor or a VM, performs the accesses. When hardware encounters an RMP access violation during a guest access, it will cause a #VMEXIT(NPF).
See APM2 section 16.36.10 for more details.
--- arch/x86/include/asm/kvm_host.h | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/x86/include/asm/kvm_host.h +8 -0
@@ -253,9 +253,13 @@ enum x86_intercept_stage;#define PFERR_FETCH_BIT 4#define PFERR_PK_BIT 5#define PFERR_SGX_BIT 15 +#define PFERR_GUEST_RMP_BIT 31#define PFERR_GUEST_FINAL_BIT 32#define PFERR_GUEST_PAGE_BIT 33#define PFERR_IMPLICIT_ACCESS_BIT 48 +#define PFERR_GUEST_ENC_BIT 34 +#define PFERR_GUEST_SIZEM_BIT 35 +#define PFERR_GUEST_VMPL_BIT 36#define PFERR_PRESENT_MASK BIT(PFERR_PRESENT_BIT)#define PFERR_WRITE_MASK BIT(PFERR_WRITE_BIT)
@@ -267,6 +271,10 @@ enum x86_intercept_stage;#define PFERR_GUEST_FINAL_MASK BIT_ULL(PFERR_GUEST_FINAL_BIT)#define PFERR_GUEST_PAGE_MASK BIT_ULL(PFERR_GUEST_PAGE_BIT)#define PFERR_IMPLICIT_ACCESS BIT_ULL(PFERR_IMPLICIT_ACCESS_BIT) +#define PFERR_GUEST_RMP_MASK BIT_ULL(PFERR_GUEST_RMP_BIT) +#define PFERR_GUEST_ENC_MASK BIT_ULL(PFERR_GUEST_ENC_BIT) +#define PFERR_GUEST_SIZEM_MASK BIT_ULL(PFERR_GUEST_SIZEM_BIT) +#define PFERR_GUEST_VMPL_MASK BIT_ULL(PFERR_GUEST_VMPL_BIT)#define PFERR_NESTED_GUEST_PAGE (PFERR_GUEST_PAGE_MASK | \ PFERR_WRITE_MASK | \
Signature · 1 line
2.25.1
RFC PATCH v9 40/51 KVM: SVM: Add support to handle RMP nested page faults
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
When SEV-SNP is enabled in the guest, the hardware places restrictions on all memory accesses based on the contents of the RMP table. When hardware encounters RMP check failure caused by the guest memory access it raises the #NPF. The error code contains additional information on the access type. See the APM volume 2 for additional information.
When using gmem, RMP faults resulting from mismatches between the state in the RMP table vs. what the guest expects via its page table result in KVM_EXIT_MEMORY_FAULTs being forwarded to userspace to handle. This means the only expected case that needs to be handled in the kernel is when the page size of the entry in the RMP table is larger than the mapping in the nested page table, in which case a PSMASH instruction needs to be issued to split the large RMP entry into individual 4K entries so that subsequent accesses can succeed.
--- arch/x86/kvm/svm/sev.c | 85 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 21 +++++++++-- arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 103 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/sev.c +85 -0
@@ -3234,6 +3234,13 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) svm->vmcb->control.ghcb_gpa = value; } +static int snp_rmptable_psmash (struct kvm *kvm,kvm_pfn_t pfn) +{ + pfn = pfn & ~(KVM_PAGES_PER_HPAGE (PG_LEVEL_2M ) -1 ); + +return psmash (pfn); +} +static int snp_complete_psc_msr_protocol (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu);
@@ -3696,3 +3703,81 @@ struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu)return p; } + +void handle_rmp_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u64 error_code) +{ +struct kvm_memory_slot *slot; +struct kvm *kvm = vcpu->kvm; +int order, rmp_level, ret; +bool assigned; +kvm_pfn_t pfn; +gfn_t gfn; + +/* +* Private memslots forward handling of implicit page state changes +* to userspace, so the only RMP faults expected here are for +* PFERR_GUEST_SIZEM_MASK. Anything else suggests that the RMP table +* has gotten out of sync with the private memslot. Generally... +* +* However, there is a transient case where access to an NPT mapping +* that has just been split/PSMASH'd can generate an RMP fault. In this +* case the PFERR_GUEST_SIZEM bit might not be set. In these cases it +* should be safe to ignore and let the guest retry, but allow for +* these to be optionally logged to diagnose exceptional cases. +*/ +if (!(error_code &PFERR_GUEST_SIZEM_MASK )) { +pr_debug_ratelimited ("Unexpected RMP fault for GPA 0x%llx, error_code 0x%llx" , + gpa, error_code); +return ; + } + + gfn = gpa >>PAGE_SHIFT ; + +/* +* Only RMPADJUST/PVALIDATE should cause PFERR_GUEST_SIZEM. +* +* For PVALIDATE, this should only happen if a guest PVALIDATEs a 4K GFN +* that is backed by a huge page in the host whose RMP entry has the +* hugepage/assigned bits set. With UPM, that should only ever happen +* for private pages. +* +* For RMPADJUST, this assumption might not hold, in which case handling +* for obtaining the PFN from HVA-backed memory may be needed. For now, +* just print warnings. +*/ +if (!kvm_mem_is_private (kvm, gfn)) { +pr_warn_ratelimited ("Unexpected RMP fault, size-mismatch for non-private GPA 0x%llx\n" , + gpa); +return ; + } + + slot =gfn_to_memslot (kvm, gfn); +if (!kvm_slot_can_be_private (slot)) { +pr_warn_ratelimited ("Unexpected RMP fault, non-private slot for GPA 0x%llx\n" , + gpa); +return ; + } + + ret =kvm_gmem_get_pfn (kvm, slot, gfn, &pfn, &order); +if (ret) { +pr_warn_ratelimited ("Unexpected RMP fault, no private backing page for GPA 0x%llx\n" , + gpa); +return ; + } + + ret =snp_lookup_rmpentry (pfn, &assigned, &rmp_level); +if (ret || !assigned) { +pr_warn_ratelimited ("Unexpected RMP fault, no assigned RMP entry found for GPA 0x%llx PFN 0x%llx error %d\n" , + gpa, pfn, ret); + goto out; + } + + ret =snp_rmptable_psmash (kvm, pfn); +if (ret) +pr_err_ratelimited ("Unable to split RMP entries for GPA 0x%llx PFN 0x%llx ret %d\n" , + gpa, pfn, ret); + +out: +kvm_zap_gfn_range (kvm, gfn, gfn +PTRS_PER_PMD ); +put_page (pfn_to_page (pfn)); +}
arch/x86/kvm/svm/svm.c +17 -4
@@ -1995,15 +1995,28 @@ static int pf_interception(struct kvm_vcpu *vcpu)static int npf_interception (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); +int rc;u64 fault_address = svm->vmcb->control.exit_info_2;u64 error_code = svm->vmcb->control.exit_info_1;trace_kvm_page_fault (vcpu, fault_address, error_code); -return kvm_mmu_page_fault (vcpu, fault_address, error_code, -static_cpu_has (X86_FEATURE_DECODEASSISTS ) ? - svm->vmcb->control.insn_bytes :NULL , - svm->vmcb->control.insn_len); + rc =kvm_mmu_page_fault (vcpu, fault_address, error_code, +static_cpu_has (X86_FEATURE_DECODEASSISTS ) ? + svm->vmcb->control.insn_bytes :NULL , + svm->vmcb->control.insn_len); + +/* +* rc == 0 indicates a userspace exit is needed to handle page +* transitions, so do that first before updating the RMP table. +*/ +if (error_code &PFERR_GUEST_RMP_MASK ) { +if (rc ==0 ) +return rc; +handle_rmp_page_fault (vcpu, fault_address, error_code); + } + +return rc; }static int db_interception (struct kvm_vcpu *vcpu)
arch/x86/kvm/svm/svm.h +1 -0
@@ -762,6 +762,7 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);void sev_es_prepare_switch_to_guest (struct sev_es_save_area *hostsa);void sev_es_unmap_ghcb (struct vcpu_svm *svm);struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu); +void handle_rmp_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u64 error_code);/* vmenter.S */
Signature · 1 line
2.25.1
RFC PATCH v9 41/51 KVM: SVM: Use a VMSA physical address variable for populating VMCB
In reply to Michael RothFrom: Tom Lendacky <[email protected]>
In preparation to support SEV-SNP AP Creation, use a variable that holds the VMSA physical address rather than converting the virtual address. This will allow SEV-SNP AP Creation to set the new physical address that will be used should the vCPU reset path be taken.
--- arch/x86/kvm/svm/sev.c | 5 ++--- arch/x86/kvm/svm/svm.c | 9 ++++++++- arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 11 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/sev.c +2 -3
@@ -3551,10 +3551,9 @@ static void sev_es_init_vmcb(struct vcpu_svm *svm)/* * An SEV-ES guest requires a VMSA area that is a separate from the -* VMCB page. Do not include the encryption mask on the VMSA physical -* address since hardware will access it using the guest key. +* VMCB page. */ - svm->vmcb->control.vmsa_pa =__pa (svm->sev_es.vmsa); + svm->vmcb->control.vmsa_pa = svm->sev_es.vmsa_pa;/* Can't intercept CR register access, HV can't modify CR registers */ svm_clr_intercept (svm,INTERCEPT_CR0_READ );
arch/x86/kvm/svm/svm.c +8 -1
@@ -1419,9 +1419,16 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu) svm->vmcb01.pa =__sme_set (page_to_pfn (vmcb01_page) <<PAGE_SHIFT );svm_switch_vmcb (svm, &svm->vmcb01); -if (vmsa_page) +if (vmsa_page) { svm->sev_es.vmsa =page_address (vmsa_page); +/* +* Do not include the encryption mask on the VMSA physical +* address since hardware will access it using the guest key. +*/ + svm->sev_es.vmsa_pa =__pa (svm->sev_es.vmsa); + } + svm->guest_state_loaded = false;return 0 ;
arch/x86/kvm/svm/svm.h +1 -0
@@ -197,6 +197,7 @@ struct vcpu_sev_es_state {struct sev_es_save_area *vmsa;struct ghcb *ghcb;struct kvm_host_map ghcb_map; +hpa_t vmsa_pa;bool received_first_sipi;unsigned int ap_reset_hold_type;
Signature · 1 line
2.25.1
RFC PATCH v9 42/51 KVM: SVM: Support SEV-SNP AP Creation NAE event
In reply to Michael RothFrom: Tom Lendacky <[email protected]>
Add support for the SEV-SNP AP Creation NAE event. This allows SEV-SNP guests to alter the register state of the APs on their own. This allows the guest a way of simulating INIT-SIPI.
A new event, KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, is created and used so as to avoid updating the VMSA pointer while the vCPU is running.
For CREATE
The guest supplies the GPA of the VMSA to be used for the vCPU with the specified APIC ID. The GPA is saved in the svm struct of the target vCPU, the KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event is added to the vCPU and then the vCPU is kicked.
For CREATE_ON_INIT:
The guest supplies the GPA of the VMSA to be used for the vCPU with the specified APIC ID the next time an INIT is performed. The GPA is saved in the svm struct of the target vCPU.
For DESTROY:
The guest indicates it wishes to stop the vCPU. The GPA is cleared from the svm struct, the KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event is added to vCPU and then the vCPU is kicked.
The KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event handler will be invoked as a result of the event or as a result of an INIT. The handler sets the vCPU to the KVM_MP_STATE_UNINITIALIZED state, so that any errors will leave the vCPU as not runnable. Any previous VMSA pages that were installed as part of an SEV-SNP AP Creation NAE event are un-pinned. If a new VMSA is to be installed, the VMSA guest page is pinned and set as the VMSA in the vCPU VMCB and the vCPU state is set to KVM_MP_STATE_RUNNABLE. If a new VMSA is not to be installed, the VMSA is cleared in the vCPU VMCB and the vCPU state is left as KVM_MP_STATE_UNINITIALIZED to prevent it from being run.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/asm/svm.h | 7 +- arch/x86/kvm/svm/sev.c | 240 ++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 3 + arch/x86/kvm/svm/svm.h | 8 +- arch/x86/kvm/x86.c | 11 ++ 6 files changed, 268 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -113,6 +113,7 @@ KVM_ARCH_REQ_FLAGS(31, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)#define KVM_REQ_HV_TLB_FLUSH \ KVM_ARCH_REQ_FLAGS(32, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) +#define KVM_REQ_UPDATE_PROTECTED_GUEST_STATE KVM_ARCH_REQ(34)#define CR0_RESERVED_BITS \ (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
arch/x86/include/asm/svm.h +6 -1
@@ -288,7 +288,12 @@ static_assert((X2AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == X2AVIC_#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) -#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +#define SVM_SEV_FEAT_RESTRICTED_INJECTION BIT(3) +#define SVM_SEV_FEAT_ALTERNATE_INJECTION BIT(4) +#define SVM_SEV_FEAT_INT_INJ_MODES \ + (SVM_SEV_FEAT_RESTRICTED_INJECTION | \ + SVM_SEV_FEAT_ALTERNATE_INJECTION)struct vmcb_seg {u16 selector;
arch/x86/kvm/svm/sev.c +240 -0
276 lines not on this page · raw message
arch/x86/kvm/svm/svm.c +3 -0
@@ -1358,6 +1358,9 @@ static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) svm->spec_ctrl =0 ; svm->virt_spec_ctrl =0 ; +if (init_event) +sev_snp_init_protected_guest_state (vcpu); +init_vmcb (vcpu);if (!init_event)
arch/x86/kvm/svm/svm.h +7 -1
@@ -94,6 +94,7 @@ struct kvm_sev_info {atomic_t migration_in_progress;u64 snp_init_flags;void *snp_context;/* SNP guest context page */ +u64 sev_features;/* Features set at VMSA creation */ };struct kvm_svm {
@@ -208,6 +209,10 @@ struct vcpu_sev_es_state {bool ghcb_sa_free;u64 ghcb_registered_gpa; + +struct mutex snp_vmsa_mutex;/* Used to handle concurrent updates of VMSA. */ +gpa_t snp_vmsa_gpa; +bool snp_ap_create; };struct vcpu_svm {
@@ -735,7 +740,7 @@ void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu);#define GHCB_VERSION_MAX 2ULL#define GHCB_VERSION_MIN 1ULL -#define GHCB_HV_FT_SUPPORTED GHCB_HV_FT_SNP +#define GHCB_HV_FT_SUPPORTED (GHCB_HV_FT_SNP | GHCB_HV_FT_SNP_AP_CREATION)extern unsigned int max_sev_asid;
@@ -764,6 +769,7 @@ void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa);void sev_es_unmap_ghcb (struct vcpu_svm *svm);struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu);void handle_rmp_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u64 error_code); +void sev_snp_init_protected_guest_state (struct kvm_vcpu *vcpu);/* vmenter.S */
arch/x86/kvm/x86.c +11 -0
@@ -10621,6 +10621,14 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (kvm_check_request (KVM_REQ_UPDATE_CPU_DIRTY_LOGGING , vcpu))static_call (kvm_x86_update_cpu_dirty_logging)(vcpu); + +if (kvm_check_request (KVM_REQ_UPDATE_PROTECTED_GUEST_STATE , vcpu)) { +kvm_vcpu_reset (vcpu, true); +if (vcpu->arch.mp_state !=KVM_MP_STATE_RUNNABLE ) { + r =1 ; + goto out; + } + } } if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win ||
@@ -12816,6 +12824,9 @@ static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)return true;#endif +if (kvm_test_request (KVM_REQ_UPDATE_PROTECTED_GUEST_STATE , vcpu)) +return true; +if (kvm_arch_interrupt_allowed (vcpu) && (kvm_cpu_has_interrupt (vcpu) ||kvm_guest_apic_has_interrupt (vcpu)))
Signature · 1 line
2.25.1
Re: [PATCH RFC v9 42/51] KVM: SVM: Support SEV-SNP AP Creation NAE event
Quoted 14 lines
arch/x86/kvm/svm/sev.c +14
-0+ +switch (request) { +case SVM_VMGEXIT_AP_CREATE_ON_INIT : + kick = false; + fallthrough; +case SVM_VMGEXIT_AP_CREATE : +if (!page_address_valid (vcpu, svm->vmcb->control.exit_info_2)) { +vcpu_unimpl (vcpu,"vmgexit: invalid AP VMSA address [%#llx] from guest\n" , + svm->vmcb->control.exit_info_2); + ret = -EINVAL ; + goto out; + } + +/*
arch/x86/kvm/svm/sev.c static int snp_complete_psc(struct kvm_vcpu *vcpu) · line 3458
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+* Malicious guest can RMPADJUST a large page into VMSA which +* will hit the SNP erratum where the CPU will incorrectly signal +* an RMP violation #PF if a hugepage collides with the RMP entry +* of VMSA page, reject the AP CREATE request if VMSA address from +* guest is 2M aligned. +*/ +if (IS_ALIGNED (svm->vmcb->control.exit_info_2,PMD_SIZE )) { +vcpu_unimpl (vcpu, +"vmgexit: AP VMSA address [%llx] from guest is unsafe as it is 2M aligned\n" , + svm->vmcb->control.exit_info_2);
In this case and maybe the above case can we instead return an error to the guest instead of a #GP?
I think Tom suggested: SW_EXITINFO1[31:0] to 2 (meaning there was an error with the VMGEXIT request) and SW_EXITINFO2 to 5 (The NAE event input was not valid (e.g., an invalid SW_EXITINFO1 value for the AP Jump Table NAE event).
This way a non-malicious but buggy or naive guest gets a little more information and a chance to retry the request?
Quoted 15 lines
arch/x86/kvm/svm/sev.c +15
-0+ ret = -EINVAL ; + goto out; + } + + target_svm->sev_es.snp_vmsa_gpa = svm->vmcb->control.exit_info_2; +break ; +case SVM_VMGEXIT_AP_DESTROY : +break ; +default : +vcpu_unimpl (vcpu,"vmgexit: invalid AP creation request [%#x] from guest\n" , + request); + ret = -EINVAL ; +break ; + } +
RFC PATCH v9 43/51 KVM: SEV: Configure MMU to check for private fault flags
In reply to Michael RothSEV-SNP uses a #NPF flag to indicate whether the guest expects a shared or a private page, which in turn dictates whether the KVM MMU should map a normal page or a gmem page. Set the appropriate mask to enable this handling for SEV-SNP guests.
--- arch/x86/kvm/svm/sev.c | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/kvm/svm/sev.c +2 -0
@@ -2006,6 +2006,8 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)if (rc) goto e_free_context; + kvm->arch.mmu_private_fault_mask =PFERR_GUEST_ENC_MASK ; +return 0 ; e_free_context:
Signature · 1 line
2.25.1
RFC PATCH v9 44/51 KVM: SEV: Implement gmem hook for initializing private pages
In reply to Michael RothThis will handle RMP table updates and direct map changes needed to put a page into a private state before mapping it into an SEV-SNP guest.
--- arch/x86/kvm/svm/sev.c | 95 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 2 + arch/x86/kvm/svm/svm.h | 2 + 3 files changed, 99 insertions(+)
arch/x86/kvm/svm/sev.c +95 -0
@@ -4022,3 +4022,98 @@ void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code)kvm_zap_gfn_range (kvm, gfn, gfn +PTRS_PER_PMD );put_page (pfn_to_page (pfn)); } + +/* Check if GFN range is marked private in the KVM/gmem xarray. */ +static bool is_gfn_range_private (struct kvm *kvm,gfn_t start,gfn_t end) +{ +gfn_t gfn = start; + +while (gfn++ < end) +if (!kvm_mem_is_private (kvm, gfn)) { +pr_debug ("%s: overlap detected, GFN 0x%llx start 0x%llx end 0x%llx\n" , + __func__, gfn, start, end); +return false; + } + +return true; +} + +/* Check that no pages in PFN range have already been set to private in RMP table. */ +static bool is_pfn_range_shared (kvm_pfn_t start,kvm_pfn_t end) +{ +kvm_pfn_t pfn = start; + +while (pfn++ < end) { +int ret, rmp_level; +bool assigned; + + ret =snp_lookup_rmpentry (pfn, &assigned, &rmp_level); +if (ret) { +pr_debug ("%s: failed to retrieve RMP entry, assuming overlap, PFN 0x%llx start 0x%llx end 0x%llx RMP level %d error %d\n" , + __func__, pfn, start, end, rmp_level, ret); +return false; + } + +if (assigned ==1 ) { +pr_debug ("%s: overlap detected, PFN 0x%llx start 0x%llx end 0x%llx RMP level %d\n" , + __func__, pfn, start, end, rmp_level); +return false; + } + } + +return true; +} + +static int get_supported_rmp_level (struct kvm *kvm,kvm_pfn_t pfn,gfn_t gfn) +{ +if (!IS_ALIGNED (pfn,PTRS_PER_PMD ) || !IS_ALIGNED (gfn,PTRS_PER_PMD )) +return PG_LEVEL_4K ; + +/* +* Check that both the desired GFN range states in the xarray, and +* current PFN range states in the RMP table, are conducive to +* creating a 2M private RMP entry. +*/ +if (is_gfn_range_private (kvm, gfn, gfn +PTRS_PER_PMD ) && +is_pfn_range_shared (pfn, pfn +PTRS_PER_PMD )) +return PG_LEVEL_2M ; + +return PG_LEVEL_4K ; +} + +int sev_gmem_prepare (struct kvm *kvm,struct kvm_memory_slot *slot, +kvm_pfn_t pfn,gfn_t gfn,u8 *max_level) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +int level, rc =0 ; +bool assigned; + +if (!sev_snp_guest (kvm)) +return 0 ; + + rc =snp_lookup_rmpentry (pfn, &assigned, &level); +if (rc) +return rc; + +/* No conversion needed, just clamp xax_level according to RMP entry. */ +if (assigned) + goto out_adjust_level; + +if (*max_level ==PG_LEVEL_4K ) + level =PG_LEVEL_4K ; +else + level =get_supported_rmp_level (kvm, pfn, gfn); + + rc =rmp_make_private (pfn,gfn_to_gpa (gfn), level, sev->asid, false); +if (rc) +pr_err_ratelimited ("%s: failed gfn %llx pfn %llx level %d rc %d\n" , + __func__, gfn, pfn, level, rc); + +out_adjust_level: +pr_debug ("%s: pfn %llx gfn %llx max_level %d level %d assigned %d\n" , + __func__, pfn, gfn, *max_level, level, assigned); +if (*max_level > level) + *max_level = level; + +return rc; +}
arch/x86/kvm/svm/svm.c +2 -0
@@ -4934,6 +4934,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, .vcpu_get_apicv_inhibit_reasons = avic_vcpu_get_apicv_inhibit_reasons, .alloc_apic_backing_page = svm_alloc_apic_backing_page, + + .gmem_prepare = sev_gmem_prepare, };/*
arch/x86/kvm/svm/svm.h +2 -0
@@ -770,6 +770,8 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm);struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu);void handle_rmp_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u64 error_code);void sev_snp_init_protected_guest_state (struct kvm_vcpu *vcpu); +int sev_gmem_prepare (struct kvm *kvm,struct kvm_memory_slot *slot, +kvm_pfn_t pfn,gfn_t gfn,u8 *max_level);/* vmenter.S */
Signature · 1 line
2.25.1
RFC PATCH v9 45/51 KVM: SEV: Implement gmem hook for invalidating private pages
In reply to Michael RothImplement a platform hook to do the work of restoring the direct map entries of gmem-managed pages and transitioning the corresponding RMP table entries back to the default shared/hypervisor-owned state.
--- arch/x86/kvm/svm/sev.c | 43 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 45 insertions(+)
arch/x86/kvm/svm/sev.c +43 -0
@@ -4117,3 +4117,46 @@ int sev_gmem_prepare(struct kvm *kvm, struct kvm_memory_slot *slot,return rc; } + +void sev_gmem_invalidate (struct kvm *kvm,kvm_pfn_t start,kvm_pfn_t end) +{ +kvm_pfn_t pfn; + +if (!sev_snp_guest (kvm)) +return ; + +pr_debug ("%s: kvm %p pfn 0x%llx pfn_end 0x%llx\n" , + __func__, kvm, start, end); + +for (pfn = start; pfn < end; pfn++) { +int rc, rmp_level; +bool assigned; + + rc =snp_lookup_rmpentry (pfn, &assigned, &rmp_level); +if (rc) { +pr_warn_ratelimited ("SEV: Failed to retrieve RMP entry for PFN 0x%llx error %d\n" , + pfn, rc); +continue ; + } + +if (!assigned) +continue ; + +/* +* If PFN is currently assigned as a 2M page, PSMASH it into +* individual 4K RMP entries before attempting to convert a +* 4K sub-page. +*/ +if (rmp_level >PG_LEVEL_4K ) { + rc =snp_rmptable_psmash (kvm, pfn); +if (rc) +pr_warn_ratelimited ("SEV: Failed to PSMASH RMP entry for PFN 0x%llx error %d\n" , + pfn, rc); + } + + rc =rmp_make_shared (pfn,PG_LEVEL_4K ); +if (rc) +pr_warn_ratelimited ("SEV: Failed to update RMP entry for PFN 0x%llx error %d\n" , + pfn, rc); + } +}
arch/x86/kvm/svm/svm.c +1 -0
@@ -4936,6 +4936,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .alloc_apic_backing_page = svm_alloc_apic_backing_page, .gmem_prepare = sev_gmem_prepare, + .gmem_invalidate = sev_gmem_invalidate, };/*
arch/x86/kvm/svm/svm.h +1 -0
@@ -772,6 +772,7 @@ void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code);void sev_snp_init_protected_guest_state (struct kvm_vcpu *vcpu);int sev_gmem_prepare (struct kvm *kvm,struct kvm_memory_slot *slot,kvm_pfn_t pfn,gfn_t gfn,u8 *max_level); +void sev_gmem_invalidate (struct kvm *kvm,kvm_pfn_t start,kvm_pfn_t end);/* vmenter.S */
Signature · 1 line
2.25.1
RFC PATCH v9 46/51 KVM: SVM: Add module parameter to enable the SEV-SNP
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
Add a module parameter than can be used to enable or disable the SEV-SNP feature. Now that KVM contains the support for the SNP set the GHCB hypervisor feature flag to indicate that SNP is supported.
--- arch/x86/kvm/svm/sev.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/sev.c +5 -3
@@ -54,14 +54,16 @@ module_param_named(sev, sev_enabled, bool, 0444);/* enable/disable SEV-ES support */ static bool sev_es_enabled = true;module_param_named (sev_es, sev_es_enabled, bool,0444 ); + +/* enable/disable SEV-SNP support */ +static bool sev_snp_enabled = true; +module_param_named (sev_snp, sev_snp_enabled, bool,0444 );#else #define sev_enabled false#define sev_es_enabled false +#define sev_snp_enabled false#endif /* CONFIG_KVM_AMD_SEV */ -/* enable/disable SEV-SNP support */ -static bool sev_snp_enabled; -#define AP_RESET_HOLD_NONE 0#define AP_RESET_HOLD_NAE_EVENT 1#define AP_RESET_HOLD_MSR_PROTO 2
Signature · 1 line
2.25.1
RFC PATCH v9 47/51 iommu/amd: Add IOMMU_SNP_SHUTDOWN support
In reply to Michael RothFrom: Ashish Kalra <[email protected]>
Add a new IOMMU API interface amd_iommu_snp_disable() to transition IOMMU pages to Hypervisor state from Reclaim state after SNP_SHUTDOWN_EX command. Invoke this API from the CCP driver after SNP_SHUTDOWN_EX command.
--- drivers/crypto/ccp/sev-dev.c | 20 +++++++++++++ drivers/iommu/amd/init.c | 55 ++++++++++++++++++++++++++++++++++++ include/linux/amd-iommu.h | 1 + 3 files changed, 76 insertions(+)
drivers/crypto/ccp/sev-dev.c +20 -0
@@ -24,6 +24,7 @@#include <linux/cpufeature.h> #include <linux/fs.h> #include <linux/fs_struct.h> +#include <linux/amd-iommu.h> #include <asm/smp.h> #include <asm/cacheflush.h>
@@ -1508,6 +1509,25 @@ static int __sev_snp_shutdown_locked(int *error)return ret; } +/* +* SNP_SHUTDOWN_EX with IOMMU_SNP_SHUTDOWN set to 1 disables SNP +* enforcement by the IOMMU and also transitions all pages +* associated with the IOMMU to the Reclaim state. +* Firmware was transitioning the IOMMU pages to Hypervisor state +* before version 1.53. But, accounting for the number of assigned +* 4kB pages in a 2M page was done incorrectly by not transitioning +* to the Reclaim state. This resulted in RMP #PF when later accessing +* the 2M page containing those pages during kexec boot. Hence, the +* firmware now transitions these pages to Reclaim state and hypervisor +* needs to transition these pages to shared state. SNP Firmware +* version 1.53 and above are needed for kexec boot. +*/ + ret =amd_iommu_snp_disable (); +if (ret) { +dev_err (sev->dev,"SNP IOMMU shutdown failed\n" ); +return ret; + } + sev->snp_initialized = false;dev_dbg (sev->dev,"SEV-SNP firmware shutdown\n" );
drivers/iommu/amd/init.c +55 -0
65 lines not on this page · raw message
include/linux/amd-iommu.h +1 -0
@@ -208,6 +208,7 @@ struct amd_iommu *get_amd_iommu(unsigned int idx);#ifdef CONFIG_KVM_AMD_SEV int amd_iommu_snp_enable (void ); +int amd_iommu_snp_disable (void );#endif #endif /* _ASM_X86_AMD_IOMMU_H */
Signature · 1 line
2.25.1
Suthikulpanit, Suravee
reviewerRe: [PATCH RFC v9 47/51] iommu/amd: Add IOMMU_SNP_SHUTDOWN support
Mike / Ashish
FYI, you might need to start including the change from this patch
https://lore.kernel.org/linux-iommu/[email protected]/T/
in this series since Christoph would like to remove the code from the current upstream, and re-introduce the change within this series instead.
Regards,
Suravee
Quoted 9 lines · Michael Roth
From: Ashish Kalra<[email protected]>
Add a new IOMMU API interface
amd_iommu_snp_disable()to transition IOMMU pages to Hypervisor state from Reclaim state afterSNP_SHUTDOWN_EXcommand. Invoke this API from the CCP driver afterSNP_SHUTDOWN_EXcommand.Signed-off-by Ashish Kalra<[email protected]>Signed-off-by Michael Roth<[email protected]>
RFC PATCH v9 48/51 crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
The SEV-SNP firmware provides the SNP_CONFIG command used to set the system-wide configuration value for SNP guests. The information includes the TCB version string to be reported in guest attestation reports.
Version 2 of the GHCB specification adds an NAE (SNP extended guest request) that a guest can use to query the reports that include additional certificates.
In both cases, userspace provided additional data is included in the attestation reports. The userspace will use the SNP_SET_EXT_CONFIG command to give the certificate blob and the reported TCB version string at once. Note that the specification defines certificate blob with a specific GUID format; the userspace is responsible for building the proper certificate blob. The ioctl treats it an opaque blob.
While it is not defined in the spec, but let's add SNP_GET_EXT_CONFIG command that can be used to obtain the data programmed through the SNP_SET_EXT_CONFIG.
--- Documentation/virt/coco/sev-guest.rst | 27 ++++ drivers/crypto/ccp/sev-dev.c | 178 ++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 2 + include/linux/psp-sev.h | 10 ++ include/uapi/linux/psp-sev.h | 17 +++ 5 files changed, 234 insertions(+)
Documentation/virt/coco/sev-guest.rst +27 -0
@@ -145,6 +145,33 @@ The SNP_PLATFORM_STATUS command is used to query the SNP platform status. The status includes API major, minor version and more. See the SEV-SNP specification for further details. +2.5 SNP_SET_EXT_CONFIG +---------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_ext_config +:Returns (out): 0 on success, -negative on error + +The SNP_SET_EXT_CONFIG is used to set the system-wide configuration such as +reported TCB version in the attestation report. The command is similar to +SNP_CONFIG command defined in the SEV-SNP spec. The main difference is the +command also accepts an additional certificate blob defined in the GHCB +specification. + +If the certs_address is zero, then the previous certificate blob will deleted. +For more information on the certificate blob layout, see the GHCB spec +(extended guest request message). + +2.6 SNP_GET_EXT_CONFIG +---------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_ext_config +:Returns (out): 0 on success, -negative on error + +The SNP_GET_EXT_CONFIG is used to query the system-wide configuration set +through the SNP_SET_EXT_CONFIG. + 3. SEV-SNP CPUID Enforcement ============================
drivers/crypto/ccp/sev-dev.c +178 -0
202 lines not on this page · raw message
drivers/crypto/ccp/sev-dev.h +2 -0
@@ -66,6 +66,8 @@ struct sev_device {bool snp_initialized;struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS ]; +struct sev_snp_certs *snp_certs; +struct sev_user_data_snp_config snp_config; };int sev_dev_init (struct psp_device *psp);
include/linux/psp-sev.h +10 -0
@@ -24,6 +24,16 @@#define SEV_FW_BLOB_MAX_SIZE 0x4000/* 16KB */ +struct sev_snp_certs { +void *data; +u32 len; +struct kref kref; +}; + +struct sev_snp_certs *sev_snp_certs_new (void *data,u32 len); +struct sev_snp_certs *sev_snp_certs_get (struct sev_snp_certs *certs); +void sev_snp_certs_put (struct sev_snp_certs *certs); +/** * SEV platform state */
include/uapi/linux/psp-sev.h +17 -0
@@ -29,6 +29,8 @@ enum {SEV_GET_ID ,/* This command is deprecated, use SEV_GET_ID2 */ SEV_GET_ID2 ,SNP_PLATFORM_STATUS , +SNP_SET_EXT_CONFIG , +SNP_GET_EXT_CONFIG , SEV_MAX, };
@@ -201,6 +203,21 @@ struct sev_user_data_snp_config {__u8 rsvd1[52 ]; } __packed; +/** +* struct sev_data_snp_ext_config - system wide configuration value for SNP. +* +* @config_address: address of the struct sev_user_data_snp_config or 0 when +* reported_tcb does not need to be updated. +* @certs_address: address of extended guest request certificate chain or +* 0 when previous certificate should be removed on SNP_SET_EXT_CONFIG. +* @certs_len: length of the certs +*/ +struct sev_user_data_ext_snp_config { +__u64 config_address;/* In */ +__u64 certs_address;/* In */ +__u32 certs_len;/* In */ +}; +/** * struct sev_issue_cmd - SEV ioctl parameters *
Signature · 1 line
2.25.1
Re: [PATCH RFC v9 48/51] crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command
Quoted 91 lines · Michael Roth
From: Brijesh Singh <[email protected]>
The SEV-SNP firmware provides the
SNP_CONFIGcommand used to set the system-wide configuration value for SNP guests. The information includes the TCB version string to be reported in guest attestation reports.Version 2 of the GHCB specification adds an NAE (SNP extended guest request) that a guest can use to query the reports that include additional certificates.
In both cases, userspace provided additional data is included in the attestation reports. The userspace will use the
SNP_SET_EXT_CONFIGcommand to give the certificate blob and the reported TCB version string at once. Note that the specification defines certificate blob with a specific GUID format; the userspace is responsible for building the proper certificate blob. The ioctl treats it an opaque blob.While it is not defined in the spec, but let's add
SNP_GET_EXT_CONFIGcommand that can be used to obtain the data programmed through theSNP_SET_EXT_CONFIG.Co-developed-by Alexey Kardashevskiy <[email protected]>Signed-off-by Alexey Kardashevskiy <[email protected]>Co-developed-by Dionna Glaze <[email protected]>Signed-off-by Dionna Glaze <[email protected]>Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>[mdr: squash in doc patch from Dionna]Signed-off-by Michael Roth <[email protected]>--- Documentation/virt/coco/sev-guest.rst | 27 ++++ drivers/crypto/ccp/sev-dev.c | 178 ++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 2 + include/linux/psp-sev.h | 10 ++ include/uapi/linux/psp-sev.h | 17 +++ 5 files changed, 234 insertions(+)Documentation/virt/coco/sev-guest.rst +27
-0@@ -145,6 +145,33 @@ The SNP_PLATFORM_STATUS command is used to query the SNP platform status. The status includes API major, minor version and more. See the SEV-SNP specification for further details. +2.5 SNP_SET_EXT_CONFIG +---------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_ext_config +:Returns (out): 0 on success, -negative on error + +The SNP_SET_EXT_CONFIG is used to set the system-wide configuration such as +reported TCB version in the attestation report. The command is similar to +SNP_CONFIG command defined in the SEV-SNP spec. The main difference is the +command also accepts an additional certificate blob defined in the GHCB +specification. + +If the certs_address is zero, then the previous certificate blob will deleted. +For more information on the certificate blob layout, see the GHCB spec +(extended guest request message). + +2.6 SNP_GET_EXT_CONFIG +---------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_ext_config +:Returns (out): 0 on success, -negative on error + +The SNP_GET_EXT_CONFIG is used to query the system-wide configuration set +through the SNP_SET_EXT_CONFIG. + 3. SEV-SNP CPUID Enforcement ============================drivers/crypto/ccp/sev-dev.c +4
-0@@ -1491,6 +1491,10 @@ static int __sev_snp_shutdown_locked(int *error) data.length =sizeof (data); data.iommu_snp_shutdown =1 ; +/* Free the memory used for caching the certificate data */ +sev_snp_certs_put (sev->snp_certs); + sev->snp_certs =NULL ; +wbinvd_on_all_cpus (); retry:
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) · line 1839
Quoted 7 lines
drivers/crypto/ccp/sev-dev.c +4
-0@@ -1829,6 +1833,126 @@ static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp)return ret; } +static int sev_ioctl_snp_get_config (struct sev_issue_cmd *argp) +{ +struct sev_device *sev = psp_master->sev_data; +struct sev_user_data_ext_snp_config input;
input = {0} would do as well as the memset() below but shorter.
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) · line 1846
Quoted 7 lines
drivers/crypto/ccp/sev-dev.c +7
-0+struct sev_snp_certs *snp_certs; +int ret; + +if (!sev->snp_initialized || !argp->data) +return -EINVAL ; + +memset (&input,0 ,sizeof (input));
but this memset() seems useless anyway because of copy_from_user() below.
Quoted 40 lines
drivers/crypto/ccp/sev-dev.c +40
-0+ +if (copy_from_user (&input, (void __user *)argp->data,sizeof (input))) +return -EFAULT ; + +/* Copy the TCB version programmed through the SET_CONFIG to userspace */ +if (input.config_address) { +if (copy_to_user ((void *__user )input.config_address, + &sev->snp_config,sizeof (struct sev_user_data_snp_config ))) +return -EFAULT ; + } + + snp_certs =sev_snp_certs_get (sev->snp_certs); + +/* Copy the extended certs programmed through the SNP_SET_CONFIG */ +if (input.certs_address && snp_certs) { +if (input.certs_len < snp_certs->len) { +/* Return the certs length to userspace */ + input.certs_len = snp_certs->len; + + ret = -EIO ; + goto e_done; + } + +if (copy_to_user ((void *__user )input.certs_address, + snp_certs->data, snp_certs->len)) { + ret = -EFAULT ; + goto put_exit; + } + } + + ret =0 ; + +e_done: +if (copy_to_user ((void __user *)argp->data, &input,sizeof (input))) + ret = -EFAULT ; + +put_exit: +sev_snp_certs_put (snp_certs); + +return ret;
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) · line 1896
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+} + +static int sev_ioctl_snp_set_config (struct sev_issue_cmd *argp,bool writable) +{ +struct sev_device *sev = psp_master->sev_data; +struct sev_user_data_ext_snp_config input; +struct sev_user_data_snp_config config; +struct sev_snp_certs *snp_certs =NULL ; +void *certs =NULL ; +int ret =0 ;
This '0' is not used below - it is always overwritten when "goto e_free" and the good exit is "return 0" and not "return ret". I'd suggest either not initializing it and letting gcc barf when some future change uses it, or initialize to something like -EFAULT.
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) · line 1904
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c +8
-0+ +if (!sev->snp_initialized || !argp->data) +return -EINVAL ; + +if (!writable) +return -EPERM ; + +memset (&input,0 ,sizeof (input));
same here.
Quoted 7 lines
drivers/crypto/ccp/sev-dev.c +7
-0+ +if (copy_from_user (&input, (void __user *)argp->data,sizeof (input))) +return -EFAULT ; + +/* Copy the certs from userspace */ +if (input.certs_address) { +if (!input.certs_len || !IS_ALIGNED (input.certs_len,PAGE_SIZE ))
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) · line 1921
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+return -EINVAL ; + + certs =psp_copy_user_blob (input.certs_address, input.certs_len); +if (IS_ERR (certs)) +return PTR_ERR (certs); + } + +/* Issue the PSP command to update the TCB version using the SNP_CONFIG. */ +if (input.config_address) { +memset (&config,0 ,sizeof (config));
and here.
Quoted 160 lines
drivers/crypto/ccp/sev-dev.c +88
-0+if (copy_from_user (&config, + (void __user *)input.config_address,sizeof (config))) { + ret = -EFAULT ; + goto e_free; + } + + ret =__sev_do_cmd_locked (SEV_CMD_SNP_CONFIG , &config, &argp->error); +if (ret) + goto e_free; + +memcpy (&sev->snp_config, &config,sizeof (config)); + } + +/* +* If the new certs are passed then cache it else free the old certs. +*/ +if (input.certs_len) { + snp_certs =sev_snp_certs_new (certs, input.certs_len); +if (!snp_certs) { + ret = -ENOMEM ; + goto e_free; + } + } + +sev_snp_certs_put (sev->snp_certs); + sev->snp_certs = snp_certs; + +return 0 ; + +e_free: +kfree (certs); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;@@ -1883,6 +2007,12 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SNP_PLATFORM_STATUS : ret =sev_ioctl_snp_platform_status (&input);break ; +case SNP_SET_EXT_CONFIG : + ret =sev_ioctl_snp_set_config (&input, writable); +break ; +case SNP_GET_EXT_CONFIG : + ret =sev_ioctl_snp_get_config (&input); +break ;default : ret = -EINVAL ; goto out;@@ -1931,6 +2061,54 @@ int sev_guest_df_flush(int *error) }EXPORT_SYMBOL_GPL (sev_guest_df_flush); +static void sev_snp_certs_release (struct kref *kref) +{ + struct sev_snp_certs *certs = container_of(kref, struct sev_snp_certs, kref); + +kfree (certs->data); +kfree (certs); +} + +struct sev_snp_certs *sev_snp_certs_new (void *data,u32 len) +{ +struct sev_snp_certs *certs; + +if (!len || !data) +return NULL ; + + certs =kzalloc (sizeof (*certs),GFP_KERNEL ); +if (!certs) +return NULL ; + + certs->data = data; + certs->len = len; +kref_init (&certs->kref); + +return certs; +} +EXPORT_SYMBOL_GPL (sev_snp_certs_new); + +struct sev_snp_certs *sev_snp_certs_get (struct sev_snp_certs *certs) +{ +if (!certs) +return NULL ; + +if (!kref_get_unless_zero (&certs->kref)) +return NULL ; + +return certs; +} +EXPORT_SYMBOL_GPL (sev_snp_certs_get); + +void sev_snp_certs_put (struct sev_snp_certs *certs) +{ +if (!certs) +return ; + +kref_put (&certs->kref, sev_snp_certs_release); +} +EXPORT_SYMBOL_GPL (sev_snp_certs_put); +static void sev_exit (struct kref *ref) {misc_deregister (&misc_dev->misc);drivers/crypto/ccp/sev-dev.h +2
-0@@ -66,6 +66,8 @@ struct sev_device {bool snp_initialized;struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS ]; +struct sev_snp_certs *snp_certs; +struct sev_user_data_snp_config snp_config; };int sev_dev_init (struct psp_device *psp);include/linux/psp-sev.h +10
-0@@ -24,6 +24,16 @@#define SEV_FW_BLOB_MAX_SIZE 0x4000/* 16KB */ +struct sev_snp_certs { +void *data; +u32 len; +struct kref kref; +}; + +struct sev_snp_certs *sev_snp_certs_new (void *data,u32 len); +struct sev_snp_certs *sev_snp_certs_get (struct sev_snp_certs *certs); +void sev_snp_certs_put (struct sev_snp_certs *certs); +/** * SEV platform state */ include/uapi/linux/psp-sev.h +6
-0@@ -29,6 +29,8 @@ enum {SEV_GET_ID ,/* This command is deprecated, use SEV_GET_ID2 */ SEV_GET_ID2 ,SNP_PLATFORM_STATUS , +SNP_SET_EXT_CONFIG , +SNP_GET_EXT_CONFIG , SEV_MAX, };@@ -201,6 +203,21 @@ struct sev_user_data_snp_config {__u8 rsvd1[52 ]; } __packed; +/** +* struct sev_data_snp_ext_config - system wide configuration value for SNP. +* +* @config_address: address of the struct sev_user_data_snp_config or 0 when
include/uapi/linux/psp-sev.h struct sev_user_data_snp_config { · line 219
Quoted 10 lines
include/uapi/linux/psp-sev.h +10
-0+* reported_tcb does not need to be updated. +* @certs_address: address of extended guest request certificate chain or +* 0 when previous certificate should be removed on SNP_SET_EXT_CONFIG. +* @certs_len: length of the certs +*/ +struct sev_user_data_ext_snp_config { +__u64 config_address;/* In */ +__u64 certs_address;/* In */ +__u32 certs_len;/* In */ +};
__packed or padding missing (there are other places like btw, I remember seeing quite a few of those). Thanks,
Quoted 4 lines
include/uapi/linux/psp-sev.h +1
-0+/** * struct sev_issue_cmd - SEV ioctl parameters *
Signature · 1 line
Alexey
RFC PATCH v9 49/51 x86/sev: Add KVM commands for per-instance certs
In reply to Michael RothFrom: Dionna Glaze <[email protected]>
The /dev/sev device has the ability to store host-wide certificates for the key used by the AMD-SP for SEV-SNP attestation report signing, but for hosts that want to specify additional certificates that are specific to the image launched in a VM, a different way is needed to communicate those certificates.
Add two new KVM ioctl to handle this: KVM_SEV_SNP_{GET,SET}_CERTS
The certificates that are set with this command are expected to follow the same format as the host certificates, but that format is opaque to the kernel.
The new behavior for custom certificates is that the extended guest request command will now return the overridden certificates if they were installed for the instance. The error condition for a too small data buffer is changed to return the overridden certificate data size if there is an overridden certificate set installed.
Setting a 0 length certificate returns the system state to only return the host certificates on an extended guest request.
Also increase the SEV_FW_BLOB_MAX_SIZE another 4K page to allow space for an extra certificate.
[mdr: remove used of "we" and "this patch" in commit log, squash in
documentation patch]
[aik: snp_handle_ext_guest_request() now uses the CCP's cert object
without copying things over, only refcounting needed.]
--- .../virt/kvm/x86/amd-memory-encryption.rst | 44 +++++++ arch/x86/kvm/svm/sev.c | 115 ++++++++++++++++++ arch/x86/kvm/svm/svm.h | 1 + include/linux/psp-sev.h | 2 +- include/uapi/linux/kvm.h | 12 ++ 5 files changed, 173 insertions(+), 1 deletion(-)
Documentation/virt/kvm/x86/amd-memory-encryption.rst +44 -0
@@ -537,6 +537,50 @@ Returns: 0 on success, -negative on error See SEV-SNP specification for further details on launch finish input parameters. +22. KVM_SEV_SNP_GET_CERTS +------------------------- + +After the SNP guest launch flow has started, the KVM_SEV_SNP_GET_CERTS command +can be issued to request the data that has been installed with the +KVM_SEV_SNP_SET_CERTS command. + +Parameters (in/out): struct kvm_sev_snp_get_certs + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_get_certs { + __u64 certs_uaddr; + __u64 certs_len + }; + +If no certs have been installed, then the return value is -ENOENT. +If the buffer specified in the struct is too small, the certs_len field will be +overwritten with the required bytes to receive all the certificate bytes and the +return value will be -EINVAL. + +23. KVM_SEV_SNP_SET_CERTS +------------------------- + +After the SNP guest launch flow has started, the KVM_SEV_SNP_SET_CERTS command +can be issued to override the /dev/sev certs data that is returned when a +guest issues an extended guest request. This is useful for instance-specific +extensions to the host certificates. + +Parameters (in/out): struct kvm_sev_snp_set_certs + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_set_certs { + __u64 certs_uaddr; + __u64 certs_len + }; + +The certs_len field may not exceed SEV_FW_BLOB_MAX_SIZE. + References ==========
arch/x86/kvm/svm/sev.c +115 -0
133 lines not on this page · raw message
arch/x86/kvm/svm/svm.h +1 -0
@@ -95,6 +95,7 @@ struct kvm_sev_info {u64 snp_init_flags;void *snp_context;/* SNP guest context page */ u64 sev_features;/* Features set at VMSA creation */ +struct sev_snp_certs *snp_certs; };struct kvm_svm {
include/linux/psp-sev.h +1 -1
@@ -22,7 +22,7 @@#define __psp_pa (x) __pa(x)#endif -#define SEV_FW_BLOB_MAX_SIZE 0x4000/* 16KB */ +#define SEV_FW_BLOB_MAX_SIZE 0x5000/* 20KB */ struct sev_snp_certs {void *data;
include/uapi/linux/kvm.h +12 -0
@@ -1937,6 +1937,8 @@ enum sev_cmd_id {KVM_SEV_SNP_LAUNCH_START ,KVM_SEV_SNP_LAUNCH_UPDATE ,KVM_SEV_SNP_LAUNCH_FINISH , +KVM_SEV_SNP_GET_CERTS , +KVM_SEV_SNP_SET_CERTS , KVM_SEV_NR_MAX, };
@@ -2084,6 +2086,16 @@ struct kvm_sev_snp_launch_finish {__u8 pad[6 ]; }; +struct kvm_sev_snp_get_certs { +__u64 certs_uaddr; +__u64 certs_len; +}; + +struct kvm_sev_snp_set_certs { +__u64 certs_uaddr; +__u64 certs_len; +}; +#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2)
Signature · 1 line
2.25.1
RFC PATCH v9 50/51 KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the SNP_GUEST_REQUEST API define in the SEV-SNP firmware specification.
The SNP_EXT_GUEST_REQUEST is similar to SNP_GUEST_REQUEST with the difference of an additional certificate blob that can be passed through the SNP_SET_CONFIG ioctl defined in the CCP driver. The CCP driver provides snp_guest_ext_guest_request() that is used by the KVM to get both the report and certificate data at once.
--- arch/x86/kvm/svm/sev.c | 175 +++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 1 + drivers/crypto/ccp/sev-dev.c | 15 +++ include/linux/psp-sev.h | 1 + 4 files changed, 192 insertions(+)
arch/x86/kvm/svm/sev.c +175 -0
@@ -328,6 +328,8 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) ret =verify_snp_init_flags (kvm, argp);if (ret) goto e_free; + +mutex_init (&sev->guest_req_lock); } ret =sev_platform_init (&argp->error);
@@ -2321,8 +2323,10 @@ static int snp_get_instance_certs(struct kvm *kvm, struct kvm_sev_cmd *argp)static void snp_replace_certs (struct kvm_sev_info *sev,struct sev_snp_certs *snp_certs) { +mutex_lock (&sev->guest_req_lock);sev_snp_certs_put (sev->snp_certs); sev->snp_certs = snp_certs; +mutex_unlock (&sev->guest_req_lock); }static int snp_set_instance_certs (struct kvm *kvm,struct kvm_sev_cmd *argp)
@@ -3171,6 +3175,8 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm)case SVM_VMGEXIT_UNSUPPORTED_EVENT :case SVM_VMGEXIT_HV_FEATURES :case SVM_VMGEXIT_PSC : +case SVM_VMGEXIT_GUEST_REQUEST : +case SVM_VMGEXIT_EXT_GUEST_REQUEST :break ;default : reason =GHCB_ERR_INVALID_EVENT ;
@@ -3604,6 +3610,163 @@ static int sev_snp_ap_creation(struct vcpu_svm *svm)return ret; } +static unsigned long snp_setup_guest_buf (struct vcpu_svm *svm, +struct sev_data_snp_guest_request *data, +gpa_t req_gpa,gpa_t resp_gpa) +{ +struct kvm_vcpu *vcpu = &svm->vcpu; +struct kvm *kvm = vcpu->kvm; +kvm_pfn_t req_pfn, resp_pfn; +struct kvm_sev_info *sev; + + sev = &to_kvm_svm (kvm)->sev_info; + +if (!IS_ALIGNED (req_gpa,PAGE_SIZE ) || !IS_ALIGNED (resp_gpa,PAGE_SIZE )) +return SEV_RET_INVALID_PARAM ; + + req_pfn =gfn_to_pfn (kvm,gpa_to_gfn (req_gpa)); +if (is_error_noslot_pfn (req_pfn)) +return SEV_RET_INVALID_ADDRESS ; + + resp_pfn =gfn_to_pfn (kvm,gpa_to_gfn (resp_gpa)); +if (is_error_noslot_pfn (resp_pfn)) +return SEV_RET_INVALID_ADDRESS ; + +if (rmp_make_private (resp_pfn,0 ,PG_LEVEL_4K ,0 , true)) +return SEV_RET_INVALID_ADDRESS ; + + data->gctx_paddr =__psp_pa (sev->snp_context); + data->req_paddr =__sme_set (req_pfn <<PAGE_SHIFT ); + data->res_paddr =__sme_set (resp_pfn <<PAGE_SHIFT ); + +return 0 ; +} + +static void snp_cleanup_guest_buf (struct sev_data_snp_guest_request *data,unsigned long *rc) +{ +u64 pfn =__sme_clr (data->res_paddr) >>PAGE_SHIFT ; +int ret; + + ret =snp_page_reclaim (pfn); +if (ret) + *rc =SEV_RET_INVALID_ADDRESS ; + + ret =rmp_make_shared (pfn,PG_LEVEL_4K ); +if (ret) + *rc =SEV_RET_INVALID_ADDRESS ; +} + +static void snp_handle_guest_request (struct vcpu_svm *svm,gpa_t req_gpa,gpa_t resp_gpa) +{ +struct sev_data_snp_guest_request data = {0 }; +struct kvm_vcpu *vcpu = &svm->vcpu; +struct kvm *kvm = vcpu->kvm; +struct kvm_sev_info *sev; +unsigned long rc; +int err; + +if (!sev_snp_guest (vcpu->kvm)) { + rc =SEV_RET_INVALID_GUEST ; + goto e_fail; + } + + sev = &to_kvm_svm (kvm)->sev_info; + +mutex_lock (&sev->guest_req_lock); + + rc =snp_setup_guest_buf (svm, &data, req_gpa, resp_gpa); +if (rc) + goto unlock; + + rc =sev_issue_cmd (kvm,SEV_CMD_SNP_GUEST_REQUEST , &data, &err); +if (rc) +/* Ensure an error value is returned to guest. */ + rc = err ? err :SEV_RET_INVALID_ADDRESS ; + +snp_cleanup_guest_buf (&data, &rc); + +unlock: +mutex_unlock (&sev->guest_req_lock); + +e_fail: +ghcb_set_sw_exit_info_2 (svm->sev_es.ghcb, rc); +} + +static void snp_handle_ext_guest_request (struct vcpu_svm *svm,gpa_t req_gpa,gpa_t resp_gpa) +{ +struct sev_data_snp_guest_request req = {0 }; +struct sev_snp_certs *snp_certs =NULL ; +struct kvm_vcpu *vcpu = &svm->vcpu; +struct kvm *kvm = vcpu->kvm; +unsigned long data_npages; +struct kvm_sev_info *sev; +unsigned long exitcode =0 ; +u64 data_gpa; +int err, rc; + +if (!sev_snp_guest (vcpu->kvm)) { + rc =SEV_RET_INVALID_GUEST ; + goto e_fail; + } + + sev = &to_kvm_svm (kvm)->sev_info; + + data_gpa = vcpu->arch.regs[VCPU_REGS_RAX ]; + data_npages = vcpu->arch.regs[VCPU_REGS_RBX ]; + +if (!IS_ALIGNED (data_gpa,PAGE_SIZE )) { + exitcode =SEV_RET_INVALID_ADDRESS ; + goto e_fail; + } + +mutex_lock (&sev->guest_req_lock); + + rc =snp_setup_guest_buf (svm, &req, req_gpa, resp_gpa); +if (rc) + goto unlock; + +/* +* If a VMM-specific certificate blob hasn't been provided, grab the +* host-wide one. +*/ + snp_certs =sev_snp_certs_get (sev->snp_certs); +if (!snp_certs) + snp_certs =sev_snp_global_certs_get (); + +/* +* If there is a host-wide or VMM-specific certificate blob available, +* make sure the guest has allocated enough space to store it. +* Otherwise, inform the guest how much space is needed. +*/ +if (snp_certs && (data_npages <<PAGE_SHIFT ) < snp_certs->len) { + vcpu->arch.regs[VCPU_REGS_RBX ] = snp_certs->len >>PAGE_SHIFT ; + exitcode =SNP_GUEST_REQ_INVALID_LEN ; + goto cleanup; + } + + rc =sev_issue_cmd (kvm,SEV_CMD_SNP_GUEST_REQUEST , &req, &err); +if (rc) { +/* pass the firmware error code */ + exitcode = err; + goto cleanup; + } + +/* Copy the certificate blob in the guest memory */ +if (sev->snp_certs && +kvm_write_guest (kvm, data_gpa, sev->snp_certs->data, sev->snp_certs->len)) + exitcode =SEV_RET_INVALID_ADDRESS ; + +cleanup: +sev_snp_certs_put (snp_certs); +snp_cleanup_guest_buf (&req, &exitcode); + +unlock: +mutex_unlock (&sev->guest_req_lock); + +e_fail: +ghcb_set_sw_exit_info_2 (svm->sev_es.ghcb, exitcode); +} +static int sev_handle_vmgexit_msr_protocol (struct vcpu_svm *svm) {struct vmcb_control_area *control = &svm->vmcb->control;
@@ -3863,6 +4026,18 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) SVM_EVTINJ_VALID); } + ret =1 ; +break ; +case SVM_VMGEXIT_GUEST_REQUEST : +snp_handle_guest_request (svm, control->exit_info_1, control->exit_info_2); + + ret =1 ; +break ; +case SVM_VMGEXIT_EXT_GUEST_REQUEST : +snp_handle_ext_guest_request (svm, + control->exit_info_1, + control->exit_info_2); + ret =1 ;break ;case SVM_VMGEXIT_UNSUPPORTED_EVENT :
arch/x86/kvm/svm/svm.h +1 -0
@@ -96,6 +96,7 @@ struct kvm_sev_info {void *snp_context;/* SNP guest context page */ u64 sev_features;/* Features set at VMSA creation */ struct sev_snp_certs *snp_certs; +struct mutex guest_req_lock;/* Lock for guest request handling */ };struct kvm_svm {
drivers/crypto/ccp/sev-dev.c +15 -0
@@ -2109,6 +2109,21 @@ void sev_snp_certs_put(struct sev_snp_certs *certs) }EXPORT_SYMBOL_GPL (sev_snp_certs_put); +struct sev_snp_certs *sev_snp_global_certs_get (void ) +{ +struct sev_device *sev; + +if (!psp_master || !psp_master->sev_data) +return NULL ; + + sev = psp_master->sev_data; +if (!sev->snp_initialized) +return NULL ; + +return sev_snp_certs_get (sev->snp_certs); +} +EXPORT_SYMBOL_GPL (sev_snp_global_certs_get); +static void sev_exit (struct kref *ref) {misc_deregister (&misc_dev->misc);
include/linux/psp-sev.h +1 -0
@@ -33,6 +33,7 @@ struct sev_snp_certs {struct sev_snp_certs *sev_snp_certs_new (void *data,u32 len);struct sev_snp_certs *sev_snp_certs_get (struct sev_snp_certs *certs);void sev_snp_certs_put (struct sev_snp_certs *certs); +struct sev_snp_certs *sev_snp_global_certs_get (void );/** * SEV platform state
Signature · 1 line
2.25.1
RFC PATCH v9 51/51 crypto: ccp: Add debug support for decrypting pages
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
Add support to decrypt guest encrypted memory. These API interfaces can be used for example to dump VMCBs on SNP guest exit.
--- drivers/crypto/ccp/sev-dev.c | 32 ++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 19 +++++++++++++++++++ 2 files changed, 51 insertions(+)
drivers/crypto/ccp/sev-dev.c +32 -0
@@ -2061,6 +2061,38 @@ int sev_guest_df_flush(int *error) }EXPORT_SYMBOL_GPL (sev_guest_df_flush); +int snp_guest_dbg_decrypt_page (u64 gctx_pfn,u64 src_pfn,u64 dst_pfn,int *error) +{ +struct sev_data_snp_dbg data = {0 }; +struct sev_device *sev; +int ret; + +if (!psp_master || !psp_master->sev_data) +return -ENODEV ; + + sev = psp_master->sev_data; + +if (!sev->snp_initialized) +return -EINVAL ; + + data.gctx_paddr = sme_me_mask | (gctx_pfn <<PAGE_SHIFT ); + data.src_addr = sme_me_mask | (src_pfn <<PAGE_SHIFT ); + data.dst_addr = sme_me_mask | (dst_pfn <<PAGE_SHIFT ); + +/* The destination page must be in the firmware state. */ +if (rmp_mark_pages_firmware (data.dst_addr,1 , false)) +return -EIO ; + + ret =sev_do_cmd (SEV_CMD_SNP_DBG_DECRYPT , &data, error); + +/* Restore the page state */ +if (snp_reclaim_pages (data.dst_addr,1 , false)) + ret = -EIO ; + +return ret; +} +EXPORT_SYMBOL_GPL (snp_guest_dbg_decrypt_page); +static void sev_snp_certs_release (struct kref *kref) { struct sev_snp_certs *certs = container_of(kref, struct sev_snp_certs, kref);
include/linux/psp-sev.h +19 -0
@@ -916,6 +916,20 @@ int sev_guest_decommission(struct sev_data_decommission *data, int *error);*/ int sev_do_cmd (int cmd,void *data,int *psp_ret); +/** +* snp_guest_dbg_decrypt_page - perform SEV SNP_DBG_DECRYPT command +* +* @sev_ret: sev command return code +* +* Returns: +* 0 if the SEV successfully processed the command +* -%ENODEV if the SEV device is not available +* -%ENOTSUPP if the SEV does not support SEV +* -%ETIMEDOUT if the SEV command timed out +* -%EIO if the SEV returned a non-zero return code +*/ +int snp_guest_dbg_decrypt_page (u64 gctx_pfn,u64 src_pfn,u64 dst_pfn,int *error); +void *psp_copy_user_blob (u64 uaddr,u32 len);void *snp_alloc_firmware_page (gfp_t mask);void snp_free_firmware_page (void *addr);
@@ -946,6 +960,11 @@ sev_issue_cmd_external_user(struct file *filep, unsigned int id, void *data, intstatic inline void *psp_copy_user_blob (u64 __user uaddr,u32 len) {return ERR_PTR (-EINVAL ); } +static inline int snp_guest_dbg_decrypt_page (u64 gctx_pfn,u64 src_pfn,u64 dst_pfn,int *error) +{ +return -ENODEV ; +} +static inline void *snp_alloc_firmware_page (gfp_t mask) {return NULL ;
Signature · 1 line
2.25.1