Add AMD Secure Nested Paging (SEV-SNP) Hypervisor Support
Subsystems (12)
RFC PATCH v8 9/56 KVM: SEV: Handle memory backed by restricted memfd
In reply to Michael RothFrom: Nikunj A Dadhania <[email protected]>
Do not pin the guest memory backed by a restrictedmem backend, as pages in the restrictedmem are already pinned. Instead, populate the pages array for these guests using the already-pinned pages provided by restrictedmem backend.
--- arch/x86/kvm/svm/sev.c | 68 +++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 10 deletions(-)
arch/x86/kvm/svm/sev.c +58 -10
@@ -383,9 +383,46 @@ static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; } +static int sev_private_mem_get_pages_handler (struct kvm *kvm,struct kvm_gfn_range *range, +void *data) +{ +struct kvm_memory_slot *memslot = range->slot; +struct page **pages = data; +int ret =0 , i =0 ; +kvm_pfn_t pfn; +gfn_t gfn; + +for (gfn = range->start; gfn < range->end; gfn++) { +int order; + + ret =kvm_restrictedmem_get_pfn (memslot, gfn, &pfn, &order); +if (ret) +return ret; + +if (is_error_noslot_pfn (pfn)) +return -EFAULT ; + + pages[i++] =pfn_to_page (pfn); + } + +return ret; +} + +static int sev_private_mem_get_pages (struct kvm *kvm,unsigned long addr, +unsigned long size,unsigned long npages, +struct page **pages) +{ +return kvm_vm_do_hva_range_op (kvm, addr, addr + size, + sev_private_mem_get_pages_handler, pages); +} +/* * Legacy SEV guest pin the pages and return the array populated with pinned * pages. +* +* SEV guests using restricted memfd backend, pages are already marked as +* unmovable and unevictable. Populate the pages array for these guests using +* restrictedmem get_pfn. */ static struct page **sev_memory_get_pages(struct kvm *kvm, unsigned long uaddr, unsigned long ulen, unsigned long *n,
@@ -393,7 +430,7 @@ static struct page **sev_memory_get_pages(struct kvm *kvm, unsigned long uaddr, {struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;unsigned long npages, size; -int npinned; +int npinned =0 ;unsigned long locked, lock_limit;struct page **pages;unsigned long first, last;
@@ -429,16 +466,25 @@ static struct page **sev_memory_get_pages(struct kvm *kvm, unsigned long uaddr,if (!pages)return ERR_PTR (-ENOMEM ); -/* Pin the user virtual address. */ - npinned =pin_user_pages_fast (uaddr, npages, write ?FOLL_WRITE :0 , pages); -if (npinned != npages) { -pr_err ("SEV: Failure locking %lu pages.\n" , npages); - ret = -ENOMEM ; - goto err; +if (kvm_arch_has_private_mem (kvm)) { +/* Get the PFN from memfile */ +if (sev_private_mem_get_pages (kvm, uaddr, ulen, npages, pages)) { +pr_err ("%s: ERROR: unable to find slot for uaddr %lx" , __func__, uaddr); + ret = -ENOMEM ; + goto err; + } + }else { +/* Pin the user virtual address. */ + npinned =pin_user_pages_fast (uaddr, npages, write ?FOLL_WRITE :0 , pages); +if (npinned != npages) { +pr_err ("SEV: Failure locking %lu pages.\n" , npages); + ret = -ENOMEM ; + goto err; + } + sev->pages_locked = locked; } *n = npages; - sev->pages_locked = locked;return pages;
@@ -455,9 +501,11 @@ static void sev_memory_put_pages(struct kvm *kvm, struct page **pages, {struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; -unpin_user_pages (pages, npages); +if (!kvm_arch_has_private_mem (kvm)) { +unpin_user_pages (pages, npages); + sev->pages_locked -= npages; + }kvfree (pages); - sev->pages_locked -= npages; }static void sev_clflush_pages (struct page *pages[],unsigned long npages)
Signature · 1 line
2.25.1
RFC PATCH v8 10/56 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
@@ -420,6 +420,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
@@ -417,6 +417,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
v8 cover Add AMD Secure Nested Paging (SEV-SNP) Hypervisor Support
This patchset is also available at:
https://github.com/amdese/linux/commits/upmv10-host-snp-v8-rfc
and is based on top of the following tree:
https://github.com/mdroth/linux/commits/upm_base_support_fixes
which in turn is based on Sean Christopherson's UPM base support tree, with some fixes/workarounds needed for SEV/SNP support.[1]
== OVERVIEW ==
This version is being posted as an RFC due to fairly extensive changes relating to transitioning the SEV-SNP implementation to using restricted/private memslots (aka Unmapped Private Memory) to manage private guest pages instead of the legacy SEV memory registration ioctls.
Alongside that work we've also been investigating leveraging UPM to to implement lazy-pinning support for SEV guests, rather than the legacy SEV memory registration ioctls which rely on pinning everything in advance.
For both of these SEV and SEV-SNP use-cases we've needed to add a number of hooks in the restricted, so we thought it would be useful for this version at least to include both UPM-based SEV and SNP implementations so can see if these hooks might be needed for other archs/platforms and start consolidating around whether/how they should be defined for general usage. There are still some TODOs in this area, but we hope this implementation is complete enough to at least outline the required additions needed for using UPM for these use-cases.
Outside of UPM-related items, we've also included fairly extensive changes based on review feedback from v6/v7 and would appreciate any feedback on those aspects as well.
== LAYOUT ==
PATCH 01-03: pre-patches that add the UPM hooks and KVM capability needed
to switch between UPM and legacy SEV memory registration.
PATCH 04-09: implement SEV lazy-pinning using UPM to manage private memory PATCH 10-28: general SNP detection/enablement for host and CCP driver PATCH 29-56: SNP hypervisor support
== TESTING (note updated QEMU command-lines) ==
For testing this via QEMU, use the following tree:
https://github.com/amdese/qemu/commits/upmv10b-snpv3-wip
SEV-SNP with UPM:
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 \
-machine q35,confidential-guest-support=sev0,memory-backend=ram1,kvm-type=protected \
...SEV with UPM (requires patched OVMF[2]):
qemu-system-x86_64 -cpu EPYC-Milan-v2 \
-object memory-backend-memfd-private,id=ram1,size=1G,share=true \
-object sev-guest,id=sev0,cbitpos=51,reduced-phys-bits=1 \
-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" 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
The RMP check is enforced as soon as SEV-SNP is enabled. Not every memory access requires an RMP check. In particular, the read accesses from the hypervisor do not require RMP checks because the data confidentiality is already protected via memory encryption. When hardware encounters an RMP checks failure, it raises a page-fault exception. If RMP check failure is due to the page-size mismatch, then split the large page to resolve the fault.
The series does not provide support for the interrupt security and migration and those feature will be added after the base support.
== BACKGROUND (SEV Lazy-pinning) ==
The current implementation of SEV relies on a KVM_MEM_ENCRYPT_REG_REGION ioctl to pin all pages in advance, since they may be used as private pages by the guest. Previous iterations of SNP also relied on this interface. With UPM however, the initial shared pages are not converted to private, instead private pages are allocated from a separate restrictedmem backend, which, by design, will handle the pinning internally upon allocation, which generally occurs when the guest faults the page in for the first time. This provides all the necessary characteristic to support lazy-pinning to improve boot times for SEV guests, and well as a bare minimum implementation of a UPM-enabled guest that can provide basic infrastructure and testing flexibility that SNP can then build on, which is why it's included with this series for now.
It should be noted that SEV guests don't have a mean to issue explicit page state changes like SNP guests can via GHCB page-state change requests. They do however need a way to inform the host of shared pages so that non-restricted memory can be used. This is done via the MAP_GPA_RANGE KVM hypercall interface which was introduced in guest kernels to inform the host of shared pages to support live migration. This allows support for existing guest kernels, however OVMF is still lacking enablement for MAP_GPA_RANGE hypercalls, which is why a patched version is needed there.
== TODO / KNOWN ISSUES ==
* Failures in the rmpupdate() helper have been observed when running many concurrent SNP guests while THP is disabled for restricted/private guest pages. This has not been observed when running with THP enabled via echo always >/sys/kernel/mm/transparent_hugepages/shmem_enabled
* CCP driver init ordering considerations (Jarkko)
* Reclaim firmware pages in case of SNP command failure (Alper)
* Return 0 to userspace when generating KVM_EXIT_VMGEXIT (Tom)
* Rework interfaces for issuing SNP guest requests to firmware (Alexey)
* Incorporate guest request throttling patches from Dionna
* Incorporate suggested updates for instant certificate support (Dionna)
[1] https://github.com/mdroth/edk2/commits/upmv8-seves-v1
[2] https://lore.kernel.org/lkml/[email protected]/
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.
----------------------------------------------------------------
Ashish Kalra (4):
x86/fault: Return pfn from dump_pagetable() for SEV-specific fault handling.
crypto: ccp: Introduce snp leaked pages list
KVM: SVM: Make VMSAVE target area memory allocation SNP safe iommu/amd: Add IOMMU_SNP_SHUTDOWN support
Brijesh Singh (32):
x86/cpufeatures: Add SEV-SNP CPU feature
x86/sev: Add the host SEV-SNP initialization support
x86/sev: Add RMP entry lookup helpers
x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction x86/sev: Invalidate pages from the direct map when adding them to the RMP table x86/traps: Define RMP violation #PF error code
x86/fault: Add support to handle the RMP fault for user address crypto:ccp: Define the SEV-SNP commands
crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP crypto:ccp: Provide API to issue SEV and SNP commands
crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
crypto: ccp: Handle the legacy SEV command when SNP is enabled
crypto: ccp: Add the SNP_PLATFORM_STATUS command
crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command
crypto: ccp: Provide APIs to query extended attestation report
KVM: SVM: Provide the Hypervisor Feature support VMGEXIT
KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
KVM: SVM: Add initial SEV-SNP support
KVM: SVM: Add KVM_SNP_INIT command
KVM: SVM: Add KVM_SEV_SNP_LAUNCH_START command
KVM: SVM: Add KVM_SEV_SNP_LAUNCH_UPDATE command
KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command
KVM: X86: Keep the NPT and RMP page level in sync
KVM: x86: Define RMP page fault error bits for #NPF
KVM: SVM: Add support to handle GHCB GPA register VMGEXIT
KVM: SVM: Add support to handle MSR based Page State Change VMGEXIT
KVM: SVM: Add support to handle Page State Change VMGEXIT
KVM: x86: Export the kvm_zap_gfn_range() for the SNP use
KVM: SVM: Add support to handle the RMP nested page fault
KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event
KVM: SVM: Add module parameter to enable the SEV-SNP
ccp: Add support to decrypt the page
Dionna Glaze (2):
x86/sev: Add KVM commands for instance certs x86/sev: Document KVM_SEV_SNP_{G,S}ET_CERTS
Hugh Dickins (1):
x86/fault: fix handle_split_page_fault() to work with memfd backed pages
Michael Roth (10):
KVM: x86: Add 'fault_is_private' x86 op
KVM: x86: Add 'update_mem_attr' x86 op
KVM: x86: Add platform hooks for private memory invalidations
KVM: SEV: Require KVM_PROTECTED_VM when AMD_MEM_ENCRYPT is enabled
KVM: Split out memory attribute xarray updates to helper function x86/fault: Add helper for dumping RMP entries
KVM: SVM: Add KVM_EXIT_VMGEXIT
KVM: SVM: Add SNP-specific handling for memory attribute updates
KVM: SVM: Implement .fault_is_private callback for SNP
KVM: SEV: Handle restricted memory invalidations for SNP
Nikunj A Dadhania (2):
KVM: SEV: Rename sev_{pin,unpin}_memory
KVM: SEV: Handle memory backed by restricted memfd
Tom Lendacky (3):
KVM: SVM: Add support to handle AP reset MSR protocol
KVM: SVM: Use a VMSA physical address variable for populating VMCB
KVM: SVM: Support SEV-SNP AP Creation NAE event
Vishal Annapurve (2):
KVM: Add HVA range operator
KVM: SEV: Populate private memory fd during LAUNCH_UPDATE_DATA
Documentation/virt/coco/sev-guest.rst | 54 + .../virt/kvm/x86/amd-memory-encryption.rst | 147 ++ arch/x86/Kconfig | 1 + arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/kvm-x86-ops.h | 5 + arch/x86/include/asm/kvm_host.h | 19 + arch/x86/include/asm/msr-index.h | 11 +- arch/x86/include/asm/sev-common.h | 28 + arch/x86/include/asm/sev.h | 28 + arch/x86/include/asm/svm.h | 6 + arch/x86/include/asm/trap_pf.h | 18 +- arch/x86/kernel/cpu/amd.c | 5 +- arch/x86/kernel/sev.c | 472 +++++ arch/x86/kvm/lapic.c | 5 +- arch/x86/kvm/mmu.h | 2 - arch/x86/kvm/mmu/mmu.c | 31 +- arch/x86/kvm/mmu/mmu_internal.h | 37 +- arch/x86/kvm/svm/sev.c | 2089 +++++++++++++++++--- arch/x86/kvm/svm/svm.c | 55 +- arch/x86/kvm/svm/svm.h | 43 +- arch/x86/kvm/trace.h | 34 + arch/x86/kvm/x86.c | 10 + arch/x86/mm/fault.c | 118 +- drivers/crypto/ccp/sev-dev.c | 1054 +++++++++- drivers/crypto/ccp/sev-dev.h | 18 + drivers/iommu/amd/init.c | 53 + include/linux/amd-iommu.h | 1 + include/linux/kvm_host.h | 14 + include/linux/mm.h | 3 +- include/linux/mm_types.h | 3 + include/linux/psp-sev.h | 351 +++- include/uapi/linux/kvm.h | 74 + include/uapi/linux/psp-sev.h | 62 + mm/memory.c | 15 + mm/restrictedmem.c | 12 +- tools/arch/x86/include/asm/cpufeatures.h | 1 + virt/kvm/kvm_main.c | 117 +- 38 files changed, 4714 insertions(+), 291 deletions(-)
RFC PATCH v8 11/56 x86/sev: Add the host SEV-SNP initialization support
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. The goal of RMP is to track the owner of each page of memory. Pages of memory can be owned by the hypervisor, owned by a specific VM or owned by the AMD-SP. See APM2 section 15.36.3 for more detail on RMP.
The RMP table is used to enforce access control to memory. The table itself is not directly writable by the software. New CPU instructions (RMPUPDATE, PVALIDATE, RMPADJUST) are used to manipulate the RMP entries.
Based on the platform configuration, the BIOS reserves the memory used for the RMP table. The start and end address of the RMP table must be queried by reading the RMP_BASE and RMP_END MSRs. If the RMP_BASE and RMP_END are not set then disable the SEV-SNP feature.
The SEV-SNP feature is enabled only after the RMP table is successfully initialized.
Also set SYSCFG.MFMD when enabling SNP as SEV-SNP FW >= 1.51 requires that SYSCFG.MFMD must be se
RMP table entry format is non-architectural and it can vary by processor and is defined by the PPR. Restrict SNP support on the known CPU model and family for which the RMP table entry format is currently defined for.
--- arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/msr-index.h | 11 +- arch/x86/kernel/sev.c | 175 +++++++++++++++++++++++ 3 files changed, 192 insertions(+), 2 deletions(-)
arch/x86/include/asm/disabled-features.h +7 -1
@@ -87,6 +87,12 @@# define DISABLE_TDX_GUEST (1 << (X86_FEATURE_TDX_GUEST & 31))#endif +#ifdef CONFIG_AMD_MEM_ENCRYPT +# 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 */
@@ -110,7 +116,7 @@ DISABLE_ENQCMD)#define DISABLED_MASK17 0#define DISABLED_MASK18 0 -#define DISABLED_MASK19 0 +#define DISABLED_MASK19 (DISABLE_SEV_SNP)#define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 20)#endif /* _ASM_X86_DISABLED_FEATURES_H */
arch/x86/include/asm/msr-index.h +10 -1
@@ -565,6 +565,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#define MSR_AMD64_VIRT_SPEC_CTRL 0xc001011f
@@ -649,7 +651,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/kernel/sev.c +175 -0
202 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC PATCH v8 12/56 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/include/asm/sev.h | 4 +- arch/x86/kernel/sev.c | 84 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-)
arch/x86/include/asm/sev.h +3 -1
@@ -83,7 +83,7 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs);/* 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)#define RMPADJUST_VMSA_PAGE_BIT BIT(16)/* SNP Guest message request */
@@ -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); +int snp_lookup_rmpentry (u64 pfn,int *level);#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 int snp_lookup_rmpentry (u64 pfn,int *level) {return 0 ; }#endif #endif
arch/x86/kernel/sev.c +84 -0
98 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC PATCH v8 13/56 x86/fault: Add helper for dumping RMP entries
In reply to Michael RothThis information will be useful for debugging things like page faults due to RMP access violations and RMPUPDATE failures.
--- arch/x86/include/asm/sev.h | 2 ++ arch/x86/kernel/sev.c | 48 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+)
arch/x86/include/asm/sev.h +2 -0
@@ -198,6 +198,7 @@ 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);int snp_lookup_rmpentry (u64 pfn,int *level); +void sev_dump_rmpentry (u64 pfn);#else static inline void sev_es_ist_enter (struct pt_regs *regs) { }static inline void sev_es_ist_exit (void ) { }
@@ -223,6 +224,7 @@ static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *inreturn -ENOTTY ; }static inline int snp_lookup_rmpentry (u64 pfn,int *level) {return 0 ; } +static inline void sev_dump_rmpentry (u64 pfn) {}#endif #endif
arch/x86/kernel/sev.c +48 -0
54 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC PATCH v8 14/56 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/include/asm/sev.h | 24 +++++++++ arch/x86/kernel/sev.c | 108 +++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+)
arch/x86/include/asm/sev.h +24 -0
@@ -80,10 +80,15 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs);/* Software defined (when rFlags.CF = 1) */ #define PVALIDATE_FAIL_NOUPDATE 255 +/* RMUPDATE detected 4K page and 2MB page overlap. */ +#define RMPUPDATE_FAIL_OVERLAP 7/* 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) +#define RMPADJUST_VMSA_PAGE_BIT BIT(16)/* SNP Guest message request */
@@ -133,6 +138,15 @@ struct snp_secrets_page_layout {u8 rsvd3[3840 ]; } __packed; +struct rmp_state { +u64 gpa; +u8 assigned; +u8 pagesize; +u8 immutable; +u8 rsvd; +u32 asid; +} __packed; +#ifdef CONFIG_AMD_MEM_ENCRYPT extern struct static_key_false sev_es_enable_key;extern void __sev_es_ist_enter (struct pt_regs *regs);
@@ -199,6 +213,9 @@ void __init __noreturn snp_abort(void);int snp_issue_guest_request (u64 exit_code,struct snp_req_data *input,unsigned long *fw_err);int snp_lookup_rmpentry (u64 pfn,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 void sev_es_ist_enter (struct pt_regs *regs) { }static inline void sev_es_ist_exit (void ) { }
@@ -225,6 +242,13 @@ static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *in }static inline int snp_lookup_rmpentry (u64 pfn,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
arch/x86/kernel/sev.c +108 -0
111 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC PATCH v8 15/56 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/kernel/sev.c | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+)
arch/x86/kernel/sev.c +57 -0
@@ -2595,6 +2595,37 @@ 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) + goto cleanup; + } + +cleanup: +WARN (ret >0 ,"Failed to restore direct map for pfn 0x%llx\n" , pfn + i); +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) + goto cleanup; + } + +cleanup: +WARN (ret >0 ,"Failed to invalidate direct map for pfn 0x%llx\n" , pfn + i); +restore_direct_map (pfn, i); +return ret; +} +static int rmpupdate (u64 pfn,struct rmp_state *val) {int max_attempts =4 *num_present_cpus ();
@@ -2605,6 +2636,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"
@@ -2630,6 +2676,17 @@ static int rmpupdate(u64 pfn, struct rmp_state *val) attempts, val->asid, ret, pfn, npages); } +/* +* 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 v8 16/56 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
@@ -567,6 +567,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
RFC PATCH v8 17/56 x86/fault: Add support to handle the RMP fault for user address
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
When SEV-SNP is enabled globally, a write from the host goes through the RMP check. When the host writes to pages, hardware checks the following conditions at the end of page walk:
1. Assigned bit in the RMP table is zero (i.e page is shared).
2. 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.
3. Immutable bit in the RMP table is not zero.
The hardware will raise page fault if one of the above conditions is not met. Try resolving the fault instead of taking fault again and again. If the host attempts to write to the guest private memory then send the SIGBUS signal to kill the process. If the page level between the host and RMP entry does not match, then split the address to keep the RMP and host page levels in sync.
--- arch/x86/mm/fault.c | 104 ++++++++++++++++++++++++++++++++++++++- include/linux/mm.h | 3 +- include/linux/mm_types.h | 3 ++ mm/memory.c | 10 ++++ 4 files changed, 118 insertions(+), 2 deletions(-)
arch/x86/mm/fault.c +103 -1
@@ -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.h> /* snp_lookup_rmpentry() */ #define CREATE_TRACE_POINTS #include <asm/trace/exceptions.h>
@@ -414,6 +415,7 @@ static void dump_pagetable(unsigned long address)pr_cont ("PTE %lx" ,pte_val (*pte)); out:pr_cont ("\n" ); +return ; bad:pr_info ("BAD\n" );
@@ -527,6 +529,8 @@ static void show_ldttss(const struct desc_ptr *gdt, const char *name, u16 index)static void show_fault_oops (struct pt_regs *regs,unsigned long error_code,unsigned long address) { +unsigned long pfn; +if (!oops_may_print ())return ;
@@ -599,7 +603,10 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long adshow_ldttss (&gdt,"TR" , tr); } -dump_pagetable (address); + pfn =dump_pagetable (address); + +if (error_code &X86_PF_RMP ) +sev_dump_rmpentry (pfn); }static noinline void
@@ -1240,6 +1247,90 @@ do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, }NOKPROBE_SYMBOL (do_kern_addr_fault); +enum rmp_pf_ret { +RMP_PF_SPLIT =0 , +RMP_PF_RETRY =1 , +RMP_PF_UNMAP =2 , +}; + +/* +* The goal of RMP faulting routine is really to check whether the +* page that faulted should be accessible. That can be determined +* simply by looking at the RMP entry for the 4k address being accessed. +* If that entry has Assigned=1 then it's a bad address. It could be +* because the 2MB region was assigned as a large page, or it could be +* because the region is all 4k pages and that 4k was assigned. +* In either case, it's a bad access. +* There are basically two main possibilities: +* 1. The 2M entry has Assigned=1 and Page_Size=1. Then all 511 middle +* entries also have Assigned=1. This entire 2M region is a guest page. +* 2. The 2M entry has Assigned=0 and Page_Size=0. Then the 511 middle +* entries can be anything, this region consists of individual 4k assignments. +*/ +static int handle_user_rmp_page_fault (struct pt_regs *regs,unsigned long error_code, +unsigned long address) +{ +int rmp_level, level; +pgd_t *pgd; +pte_t *pte; +u64 pfn; + + pgd =__va (read_cr3_pa ()); + pgd +=pgd_index (address); + + pte =lookup_address_in_pgd (pgd, address, &level); + +/* +* It can happen if there was a race between an unmap event and +* the RMP fault delivery. +*/ +if (!pte || !pte_present (*pte)) +return RMP_PF_UNMAP ; + +/* +* RMP page fault handler follows this algorithm: +* 1. Compute the pfn for the 4kb page being accessed +* 2. Read that RMP entry -- If it is assigned then kill the process +* 3. Otherwise, check the level from the host page table +* If level=PG_LEVEL_4K then the page is already smashed +* so just retry the instruction +* 4. If level=PG_LEVEL_2M/1G, then the host page needs to be split +*/ + + pfn =pte_pfn (*pte); + +/* If its large page then calculte the fault pfn */ +if (level >PG_LEVEL_4K ) + pfn = pfn |PFN_DOWN (address & (page_level_size (level) -1 )); + +/* +* If its a guest private page, then the fault cannot be resolved. +* Send a SIGBUS to terminate the process. +* +* As documented in APM vol3 pseudo-code for RMPUPDATE, when the 2M range +* is covered by a valid (Assigned=1) 2M entry, the middle 511 4k entries +* also have Assigned=1. This means that if there is an access to a page +* which happens to lie within an Assigned 2M entry, the 4k RMP entry +* will also have Assigned=1. Therefore, the kernel should see that +* the page is not a valid page and the fault cannot be resolved. +*/ +if (snp_lookup_rmpentry (pfn, &rmp_level)) { +pr_info ("Fatal RMP page fault, terminating process, entry assigned for pfn 0x%llx\n" , + pfn); +do_sigbus (regs, error_code, address,VM_FAULT_SIGBUS ); +return RMP_PF_RETRY ; + } + +/* +* The backing page level is higher than the RMP page level, request +* to split the page. +*/ +if (level > rmp_level) +return RMP_PF_SPLIT ; + +return RMP_PF_RETRY ; +} +/* * Handle faults in the user portion of the address space. Nothing in here * should check X86_PF_USER without a specific justification: for almost
@@ -1337,6 +1428,17 @@ void do_user_addr_fault(struct pt_regs *regs,if (error_code &X86_PF_INSTR ) flags |=FAULT_FLAG_INSTRUCTION ; +/* +* If its an RMP violation, try resolving it. +*/ +if (error_code &X86_PF_RMP ) { +if (handle_user_rmp_page_fault (regs, error_code, address)) +return ; + +/* Ask to split the page */ + flags |=FAULT_FLAG_PAGE_SPLIT ; + } +#ifdef CONFIG_X86_64 /* * Faults in the vsyscall page might need emulation. The
include/linux/mm.h +2 -1
@@ -466,7 +466,8 @@ static inline bool fault_flag_allow_retry_first(enum fault_flag flags) { FAULT_FLAG_USER, "USER" }, \ { FAULT_FLAG_REMOTE, "REMOTE" }, \ { FAULT_FLAG_INSTRUCTION, "INSTRUCTION" }, \ - { FAULT_FLAG_INTERRUPTIBLE, "INTERRUPTIBLE" } + { FAULT_FLAG_INTERRUPTIBLE, "INTERRUPTIBLE" }, \ + { FAULT_FLAG_PAGE_SPLIT, "PAGESPLIT" }/* * vm_fault is filled by the pagefault handler and passed to the vma's
include/linux/mm_types.h +3 -0
@@ -962,6 +962,8 @@ typedef struct {* mapped R/O. * @FAULT_FLAG_ORIG_PTE_VALID: whether the fault has vmf->orig_pte cached. * We should only access orig_pte if this flag set. +* @FAULT_FLAG_PAGE_SPLIT: The fault was due page size mismatch, split the +* region to smaller page size and retry. * * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify * whether we would allow page faults to retry by specifying these two
@@ -999,6 +1001,7 @@ enum fault_flag {FAULT_FLAG_INTERRUPTIBLE =1 <<9 ,FAULT_FLAG_UNSHARE =1 <<10 ,FAULT_FLAG_ORIG_PTE_VALID =1 <<11 , + FAULT_FLAG_PAGE_SPLIT = 1 << 12, };typedef unsigned int __bitwise zap_flags_t ;
mm/memory.c +10 -0
@@ -4996,6 +4996,12 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf)return 0 ; } +static int handle_split_page_fault (struct vm_fault *vmf) +{ +__split_huge_pmd (vmf->vma, vmf->pmd, vmf->address, false,NULL ); +return 0 ; +} +/* * By the time we get here, we already hold the mm semaphore *
@@ -5078,6 +5084,10 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,pmd_migration_entry_wait (mm, vmf.pmd);return 0 ; } + +if (flags &FAULT_FLAG_PAGE_SPLIT ) +return handle_split_page_fault (&vmf); +if (pmd_trans_huge (vmf.orig_pmd) ||pmd_devmap (vmf.orig_pmd)) {if (pmd_protnone (vmf.orig_pmd) &&vma_is_accessible (vma))return do_huge_pmd_numa_page (&vmf);
Signature · 1 line
2.25.1
RFC PATCH v8 18/56 x86/fault: fix handle_split_page_fault() to work with memfd backed pages
In reply to Michael RothFrom: Hugh Dickins <[email protected]>
When the address is backed by a memfd, the code to split the page does nothing more than remove the PMD from the page tables. So immediately install a PTE to ensure that any other pages in that 2MB region are brought back as in 4K pages.
--- mm/memory.c | 5 +++++ 1 file changed, 5 insertions(+)
mm/memory.c +5 -0
@@ -4999,6 +4999,11 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf)static int handle_split_page_fault (struct vm_fault *vmf) {__split_huge_pmd (vmf->vma, vmf->pmd, vmf->address, false,NULL ); +/* +* Install a PTE immediately to ensure that any other pages in +* this 2MB region are brought back in as 4K pages. +*/ +__pte_alloc (vmf->vma->vm_mm, vmf->pmd);return 0 ; }
Signature · 1 line
2.25.1
RFC PATCH v8 19/56 x86/fault: Return pfn from dump_pagetable() for SEV-specific fault handling.
In reply to Michael RothFrom: Ashish Kalra <[email protected]>
Return pfn from dump_pagetable() to do SEV-specific fault handling. Used for handling SNP RMP page fault.
--- arch/x86/mm/fault.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
arch/x86/mm/fault.c +11 -4
@@ -311,7 +311,7 @@ static bool low_pfn(unsigned long pfn)return pfn < max_low_pfn; } -static void dump_pagetable (unsigned long address) +static unsigned long dump_pagetable (unsigned long address) {pgd_t *base =__va (read_cr3_pa ());pgd_t *pgd = &base[pgd_index (address)];
@@ -345,8 +345,10 @@ static void dump_pagetable(unsigned long address) pte =pte_offset_kernel (pmd, address);pr_cont ("*pte = %0*Lx " ,sizeof (*pte) *2 , (u64 )pte_val (*pte)); +return 0 ; out:pr_cont ("\n" ); + return 0; }#else /* CONFIG_X86_64: */
@@ -367,10 +369,11 @@ static int bad_address(void *p)return get_kernel_nofault (dummy, (unsigned long *)p); } -static void dump_pagetable (unsigned long address) +static unsigned long dump_pagetable (unsigned long address) {pgd_t *base =__va (read_cr3_pa ());pgd_t *pgd = base +pgd_index (address); +unsigned long pfn;p4d_t *p4d;pud_t *pud;pmd_t *pmd;
@@ -388,6 +391,7 @@ static void dump_pagetable(unsigned long address)if (bad_address (p4d)) goto bad; + pfn =p4d_pfn (*p4d);pr_cont ("P4D %lx " ,p4d_val (*p4d));if (!p4d_present (*p4d) ||p4d_large (*p4d)) goto out;
@@ -396,6 +400,7 @@ static void dump_pagetable(unsigned long address)if (bad_address (pud)) goto bad; + pfn =pud_pfn (*pud);pr_cont ("PUD %lx " ,pud_val (*pud));if (!pud_present (*pud) ||pud_large (*pud)) goto out;
@@ -404,6 +409,7 @@ static void dump_pagetable(unsigned long address)if (bad_address (pmd)) goto bad; + pfn =pmd_pfn (*pmd);pr_cont ("PMD %lx " ,pmd_val (*pmd));if (!pmd_present (*pmd) ||pmd_large (*pmd)) goto out;
@@ -412,13 +418,14 @@ static void dump_pagetable(unsigned long address)if (bad_address (pte)) goto bad; + pfn =pte_pfn (*pte);pr_cont ("PTE %lx" ,pte_val (*pte)); out:pr_cont ("\n" ); - -return ; +return pfn; bad:pr_info ("BAD\n" ); + return -1; }#endif /* CONFIG_X86_64 */
Signature · 1 line
2.25.1
RFC PATCH v8 20/56 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.
--- drivers/crypto/ccp/sev-dev.c | 16 +++ include/linux/psp-sev.h | 247 +++++++++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 44 +++++++ 3 files changed, 307 insertions(+)
drivers/crypto/ccp/sev-dev.c +16 -0
@@ -126,6 +126,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 );
@@ -154,6 +156,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 +247 -0
259 lines not on this page · raw message
include/uapi/linux/psp-sev.h +44 -0
56 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC PATCH v8 1/56 KVM: x86: Add 'fault_is_private' x86 op
In reply to Michael RothThis callback is used by the KVM MMU to check whether a #NPF was for a private GPA or not.
In some cases the full 64-bit error code for the #NPF 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.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 3 +-- arch/x86/kvm/mmu/mmu_internal.h | 37 +++++++++++++++++++++++++++--- 4 files changed, 37 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -131,6 +131,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(fault_is_private); #undef KVM_X86_OP #undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1643,6 +1643,7 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level); +bool (*fault_is_private)(struct kvm *kvm,gpa_t gpa,u64 error_code,bool *private_fault);bool (*has_wbinvd_exit)(void );
arch/x86/kvm/mmu/mmu.c +1 -2
@@ -5724,8 +5724,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);if (KVM_BUG_ON (r ==RET_PF_INVALID , vcpu->kvm))return -EIO ; }
arch/x86/kvm/mmu/mmu_internal.h +34 -3
@@ -231,6 +231,37 @@ 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 (static_call (kvm_x86_fault_is_private)(kvm, gpa, err, &private_fault)) + 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().
@@ -262,11 +293,11 @@ enum { };static inline int kvm_mmu_do_page_fault (struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa, -u32 err,bool prefetch) +u64 err,bool prefetch) {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 ,
@@ -280,7 +311,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
RFC PATCH v8 21/56 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 | 225 +++++++++++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 2 + include/linux/psp-sev.h | 17 +++ 3 files changed, 244 insertions(+)
drivers/crypto/ccp/sev-dev.c +225 -0
@@ -26,6 +26,7 @@#include <linux/fs_struct.h> #include <asm/smp.h> +#include <asm/e820/types.h> #include "psp-dev.h" #include "sev-dev.h"
@@ -34,6 +35,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;
@@ -76,6 +81,13 @@ 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 inline bool sev_version_greater_or_equal (u8 maj,u8 min) {struct sev_device *sev = psp_master->sev_data;
@@ -830,6 +842,186 @@ 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 (!psp || !psp->sev_data) +return -ENODEV ; + + sev = psp->sev_data; + +if (sev->snp_initialized) +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 =sev_fw_alloc (PAGE_SIZE ); +if (!snp_range_list) { +dev_err (sev->dev, +"SEV: SNP_INIT_EX range list memory allocation failed\n" ); +return -ENOMEM ; + } + +memset (snp_range_list,0 ,PAGE_SIZE ); + +/* +* 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 =__pa (snp_range_list); + + rc =__sev_do_cmd_locked (SEV_CMD_SNP_INIT_EX , &data, error); +if (rc) +return rc; + }else { + 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; +} + +int sev_snp_init (int *error,bool init_on_probe) +{ +int rc; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return -ENODEV ; + +if (init_on_probe && !psp_init_on_probe) +return 0 ; + +mutex_lock (&sev_cmd_mutex); + rc =__sev_snp_init_locked (error); +mutex_unlock (&sev_cmd_mutex); + +return rc; +} +EXPORT_SYMBOL_GPL (sev_snp_init); + +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;
@@ -1270,6 +1462,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) {
@@ -1286,6 +1480,14 @@ static void sev_firmware_shutdown(struct sev_device *sev) get_order(NV_LENGTH)); sev_init_ex_buffer =NULL ; } + +if (snp_range_list) { +free_pages ((unsigned long )snp_range_list, +get_order (PAGE_SIZE )); + snp_range_list =NULL ; + } + +sev_snp_shutdown (&error); }void sev_dev_destroy (struct psp_device *psp)
@@ -1341,6 +1543,26 @@ void sev_pci_init(void) } } +/* +* If boot CPU supports SNP, then first attempt to initialize +* the SNP firmware. +*/ +if (cpu_feature_enabled (X86_FEATURE_SEV_SNP )) { +if (!sev_version_greater_or_equal (SNP_MIN_API_MAJOR ,SNP_MIN_API_MINOR )) { +dev_err (sev->dev,"SEV-SNP support requires firmware version >= %d:%d\n" , +SNP_MIN_API_MAJOR ,SNP_MIN_API_MINOR ); + }else { + rc =sev_snp_init (&error, true); +if (rc) { +/* +* 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 error %#x\n" , error); + } + } + } +/* Obtain the TMR memory area for SEV-ES use */ sev_es_tmr =sev_fw_alloc (SEV_ES_TMR_SIZE );if (!sev_es_tmr)
@@ -1356,6 +1578,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);
include/linux/psp-sev.h +17 -0
@@ -794,6 +794,21 @@ struct sev_data_snp_shutdown_ex {*/ int sev_platform_init (int *error); +/** +* sev_snp_init - perform SEV SNP_INIT command +* +* @error: SEV command return code +* @init_on_probe: indicates if called during module probe/init +* +* 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_snp_init (int *error,bool init_on_probe); +/** * sev_platform_status - perform SEV PLATFORM_STATUS command *
@@ -901,6 +916,8 @@ sev_platform_status(struct sev_user_data_status *status, int *error) { return -Estatic inline int sev_platform_init (int *error) {return -ENODEV ; } +static inline int sev_snp_init (int *error,bool init_on_probe) {return -ENODEV ; } +static inline int sev_guest_deactivate (struct sev_data_deactivate *data,int *error) {return -ENODEV ; }
Signature · 1 line
2.25.1
RFC PATCH v8 22/56 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
@@ -415,7 +415,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;
@@ -425,6 +425,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
@@ -907,6 +907,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 */
@@ -924,6 +938,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 v8 23/56 crypto: ccp: 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.
--- drivers/crypto/ccp/sev-dev.c | 28 ++++++++++++++++++++++++++++ include/linux/psp-sev.h | 8 ++++++++ 2 files changed, 36 insertions(+)
drivers/crypto/ccp/sev-dev.c +28 -0
@@ -42,6 +42,12 @@static DEFINE_MUTEX (sev_cmd_mutex);static struct sev_misc_dev *misc_dev; +/* 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 ); +static int psp_cmd_timeout =100 ;module_param (psp_cmd_timeout, int,0644 );MODULE_PARM_DESC (psp_cmd_timeout," default timeout value, in seconds, for PSP commands" );
@@ -188,6 +194,28 @@ static int sev_cmd_buffer_len(int cmd)return 0 ; } +void snp_mark_pages_offline (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_mark_pages_offline); +static void *sev_fw_alloc (unsigned long len) {struct page *page;
include/linux/psp-sev.h +8 -0
@@ -923,6 +923,12 @@ int sev_do_cmd(int cmd, void *data, int *psp_ret);void *psp_copy_user_blob (u64 uaddr,u32 len); +/** +* sev_mark_pages_offline - insert non-reclaimed firmware/guest pages +* into a leaked pages list. +*/ +void snp_mark_pages_offline (unsigned long pfn,unsigned int npages); +#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ static inline int
@@ -951,6 +957,8 @@ 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 ); } +void snp_mark_pages_offline (unsigned long pfn,unsigned int npages) {} +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */
Signature · 1 line
2.25.1
RFC PATCH v8 24/56 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.
--- drivers/crypto/ccp/sev-dev.c | 148 +++++++++++++++++++++++++++++++++-- include/linux/psp-sev.h | 9 +++ 2 files changed, 149 insertions(+), 8 deletions(-)
drivers/crypto/ccp/sev-dev.c +140 -8
@@ -94,6 +94,13 @@ static void *sev_init_ex_buffer;*/ struct sev_data_range_list *snp_range_list; +/* 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;
@@ -216,11 +223,134 @@ void snp_mark_pages_offline(unsigned long pfn, unsigned int npages) }EXPORT_SYMBOL_GPL (snp_mark_pages_offline); +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 ; + +if (!pfn_valid (pfn)) { +pr_err ("%s: Invalid PFN %lx\n" , __func__, pfn); +return 0 ; + } + +for (i =0 ; i < npages; i++, pfn++, n++) { + paddr = pfn <<PAGE_SHIFT ; + +if (locked) + ret =__sev_do_cmd_locked (SEV_CMD_SNP_PAGE_RECLAIM , &paddr, &err); +else + ret =sev_do_cmd (SEV_CMD_SNP_PAGE_RECLAIM , &paddr, &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_mark_pages_offline (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 ;
@@ -468,7 +598,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);
@@ -491,7 +621,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);
@@ -982,6 +1112,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; }
@@ -1499,8 +1631,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 ; }
@@ -1511,8 +1644,7 @@ static void sev_firmware_shutdown(struct sev_device *sev) }if (snp_range_list) { -free_pages ((unsigned long )snp_range_list, -get_order (PAGE_SIZE )); +snp_free_firmware_page (snp_range_list); snp_range_list =NULL ; }
@@ -1593,7 +1725,7 @@ void sev_pci_init(void) }/* Obtain the TMR memory area for SEV-ES use */ - sev_es_tmr =sev_fw_alloc (SEV_ES_TMR_SIZE ); + sev_es_tmr =sev_fw_alloc (sev_es_tmr_size);if (!sev_es_tmr)dev_warn (sev->dev,"SEV: TMR allocation failed, SEV-ES support unavailable\n" );
include/linux/psp-sev.h +9 -0
@@ -922,6 +922,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);/** * sev_mark_pages_offline - insert non-reclaimed firmware/guest pages
@@ -959,6 +961,13 @@ static inline void *psp_copy_user_blob(u64 __user uaddr, u32 len) { return ERR_Pvoid snp_mark_pages_offline (unsigned long pfn,unsigned int npages) {} +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 v8 25/56 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
@@ -27,6 +27,7 @@#include <asm/smp.h> #include <asm/e820/types.h> +#include <asm/sev.h> #include "psp-dev.h" #include "sev-dev.h"
@@ -286,6 +287,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_mark_pages_offline (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;
@@ -487,12 +512,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)
@@ -512,12 +820,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);
@@ -560,15 +884,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; }
@@ -1579,10 +1912,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;
@@ -1648,6 +1983,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); }
@@ -1722,6 +2063,14 @@ void sev_pci_init(void)dev_err (sev->dev,"SEV-SNP: failed to INIT error %#x\n" , error); } } + +/* +* Allocate the intermediate buffers used for the legacy command handling. +*/ +if (alloc_snp_host_map (sev)) { +dev_notice (sev->dev,"Failed to alloc host map (disabling legacy SEV)\n" ); + goto skip_legacy; + } }/* Obtain the TMR memory area for SEV-ES use */
@@ -1739,12 +2088,14 @@ void sev_pci_init(void)dev_err (sev->dev,"SEV: failed to INIT error %#x, rc %d\n" , error, rc); +skip_legacy: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: +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 v8 26/56 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 v8 27/56 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 | 123 ++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 4 + include/uapi/linux/psp-sev.h | 17 ++++ 4 files changed, 171 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 +123 -0
147 lines not on this page · raw message
drivers/crypto/ccp/sev-dev.h +4 -0
@@ -66,6 +66,10 @@ struct sev_device {bool snp_initialized;struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS ]; +void *snp_certs_data; +u32 snp_certs_len; +struct mutex snp_certs_lock; +struct sev_user_data_snp_config snp_config; };int sev_dev_init (struct psp_device *psp);
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, };
@@ -192,6 +194,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
RFC PATCH v8 28/56 crypto: ccp: Provide APIs to query extended attestation report
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
Version 2 of the GHCB specification defines VMGEXIT that is used to get the extended attestation report. The extended attestation report includes the certificate blobs provided through the SNP_SET_EXT_CONFIG.
The snp_guest_ext_guest_request() will be used by the hypervisor to get the extended attestation report. See the GHCB specification for more details.
--- drivers/crypto/ccp/sev-dev.c | 47 ++++++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 33 +++++++++++++++++++++++++ 2 files changed, 80 insertions(+)
drivers/crypto/ccp/sev-dev.c +47 -0
@@ -2017,6 +2017,53 @@ int sev_guest_df_flush(int *error) }EXPORT_SYMBOL_GPL (sev_guest_df_flush); +int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data, +unsigned long vaddr,unsigned long *npages,unsigned long *fw_err) +{ +unsigned long expected_npages; +struct sev_device *sev; +int rc; + +if (!psp_master || !psp_master->sev_data) +return -ENODEV ; + + sev = psp_master->sev_data; + +if (!sev->snp_initialized) +return -EINVAL ; + +mutex_lock (&sev->snp_certs_lock); +/* +* Check if there is enough space to copy the certificate chain. Otherwise +* return ERROR code defined in the GHCB specification. +*/ + expected_npages = sev->snp_certs_len >>PAGE_SHIFT ; +if (*npages < expected_npages) { + *npages = expected_npages; + *fw_err =SNP_GUEST_REQ_INVALID_LEN ; +mutex_unlock (&sev->snp_certs_lock); +return -EINVAL ; + } + + rc =sev_do_cmd (SEV_CMD_SNP_GUEST_REQUEST , data, (int *)fw_err); +if (rc) { +mutex_unlock (&sev->snp_certs_lock); +return rc; + } + +/* Copy the certificate blob */ +if (sev->snp_certs_data) { + *npages = expected_npages; +memcpy ((void *)vaddr, sev->snp_certs_data, *npages <<PAGE_SHIFT ); + }else { + *npages =0 ; + } + +mutex_unlock (&sev->snp_certs_lock); +return rc; +} +EXPORT_SYMBOL_GPL (snp_guest_ext_guest_request); +static void sev_exit (struct kref *ref) {misc_deregister (&misc_dev->misc);
include/linux/psp-sev.h +33 -0
@@ -931,6 +931,32 @@ void snp_free_firmware_page(void *addr);*/ void snp_mark_pages_offline (unsigned long pfn,unsigned int npages); +/** +* snp_guest_ext_guest_request - perform the SNP extended guest request command +* defined in the GHCB specification. +* +* @data: the input guest request structure +* @vaddr: address where the certificate blob need to be copied. +* @npages: number of pages for the certificate blob. +* If the specified page count is less than the certificate blob size, then the +* required page count is returned with error code defined in the GHCB spec. +* If the specified page count is more than the certificate blob size, then +* page count is updated to reflect the amount of valid data copied in the +* vaddr. +* +* @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_ext_guest_request (struct sev_data_snp_guest_request *data, +unsigned long vaddr,unsigned long *npages, +unsigned long *error); +#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ static inline int
@@ -968,6 +994,13 @@ static inline void *snp_alloc_firmware_page(gfp_t mask)static inline void snp_free_firmware_page (void *addr) { } +static inline int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data, +unsigned long vaddr,unsigned long *n, +unsigned long *error) +{ +return -ENODEV ; +} +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */
Signature · 1 line
2.25.1
RFC PATCH v8 29/56 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
@@ -191,6 +191,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 v8 30/56 KVM: SVM: Provide the Hypervisor Feature support VMGEXIT
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
@@ -2675,6 +2675,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 ;
@@ -2941,6 +2942,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;
@@ -3065,6 +3073,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
@@ -662,9 +662,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 0extern unsigned int max_sev_asid;
Signature · 1 line
2.25.1
RFC PATCH v8 2/56 KVM: x86: Add 'update_mem_attr' x86 op
In reply to Michael RothThis callback will do any platform-specific handling needed for converting pages between shared/private.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 13 +++++++++++++ include/linux/kvm_host.h | 4 ++++ virt/kvm/kvm_main.c | 29 +++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -132,6 +132,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 (fault_is_private); +KVM_X86_OP_OPTIONAL_RET0(update_mem_attr)#undef KVM_X86_OP#undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1644,6 +1644,8 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);bool (*fault_is_private)(struct kvm *kvm,gpa_t gpa,u64 error_code,bool *private_fault); +int (*update_mem_attr)(struct kvm_memory_slot *slot,unsigned int attr, +gfn_t start,gfn_t end);bool (*has_wbinvd_exit)(void );
arch/x86/kvm/mmu/mmu.c +13 -0
@@ -7251,4 +7251,17 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm, linfo_update_mixed(gfn, slot, level, mixed); } } + +void kvm_arch_post_set_memory_attributes (struct kvm *kvm, +struct kvm_memory_slot *slot, +unsigned long attrs, +gfn_t start,gfn_t end) +{ +int ret; + + ret =static_call (kvm_x86_update_mem_attr)(slot, attrs, start, end); +if (ret) +pr_warn_ratelimited ("Failed to update GFN range 0x%llx-0x%llx with attributes 0x%lx. Ret: %d\n" , + start, end, attrs, ret); +}#endif
include/linux/kvm_host.h +4 -0
@@ -2330,6 +2330,10 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm,struct kvm_memory_slot *slot, unsigned long attrs, gfn_t start, gfn_t end); +void kvm_arch_post_set_memory_attributes (struct kvm *kvm, +struct kvm_memory_slot *slot, +unsigned long attrs, +gfn_t start,gfn_t end);static inline bool kvm_mem_is_private (struct kvm *kvm,gfn_t gfn) {
virt/kvm/kvm_main.c +29 -0
@@ -2561,6 +2561,32 @@ static void kvm_mem_attrs_changed(struct kvm *kvm, unsigned long attrs,kvm_flush_remote_tlbs (kvm); } +static void kvm_post_mem_attrs_changed (struct kvm *kvm,unsigned long attrs, +gfn_t start_orig,gfn_t end_orig) +{ +struct kvm_memory_slot *slot; +struct kvm_memslots *slots; +struct kvm_memslot_iter iter; +int i; + +for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) { + slots =__kvm_memslots (kvm, i); + +kvm_for_each_memslot_in_gfn_range (&iter, slots, start_orig, end_orig) { +gfn_t start, end; + + slot = iter.slot; + start =max (start_orig, slot->base_gfn); + end =min (end_orig, slot->base_gfn + slot->npages); + +if (start >= end) +continue ; + +kvm_arch_post_set_memory_attributes (kvm, slot, attrs, start, end); + } + } +} +static int kvm_vm_ioctl_set_mem_attributes (struct kvm *kvm,struct kvm_memory_attributes *attrs) {
@@ -2602,6 +2628,9 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm,kvm_mmu_invalidate_end (kvm);KVM_MMU_UNLOCK (kvm); +if (i > start) +kvm_post_mem_attrs_changed (kvm, attrs->attributes, start, i); +mutex_unlock (&kvm->slots_lock); attrs->address = i <<PAGE_SHIFT ;
Signature · 1 line
2.25.1
RFC PATCH v8 31/56 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" in the RMP 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. 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.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/lapic.c | 5 ++++- arch/x86/kvm/svm/sev.c | 33 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 15 ++++++++++++-- arch/x86/kvm/svm/svm.h | 1 + 6 files changed, 54 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -131,6 +131,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(alloc_apic_backing_page) KVM_X86_OP_OPTIONAL_RET0(fault_is_private); KVM_X86_OP_OPTIONAL_RET0(update_mem_attr) KVM_X86_OP_OPTIONAL(invalidate_restricted_mem)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1729,6 +1729,8 @@ struct kvm_x86_ops {* Returns vCPU specific APICv inhibit reasons */ unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu); + +void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); };struct kvm_x86_nested_ops {
arch/x86/kvm/lapic.c +4 -1
@@ -2740,7 +2740,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/sev.c +33 -0
@@ -3241,3 +3241,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 +13 -2
@@ -1372,7 +1372,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;
@@ -1381,7 +1381,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;
@@ -4696,6 +4696,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 ,
@@ -4824,6 +4834,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
@@ -692,6 +692,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 v8 32/56 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
@@ -2306,6 +2309,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;
@@ -2385,12 +2389,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
@@ -76,6 +76,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 */
@@ -323,6 +324,13 @@ static __always_inline bool sev_es_guest(struct kvm *kvm)#endif } +static 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 v8 33/56 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 | 44 ++++++++++++++++++- arch/x86/kvm/svm/svm.h | 4 ++ include/uapi/linux/kvm.h | 13 ++++++ 5 files changed, 87 insertions(+), 2 deletions(-)
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
@@ -278,6 +278,7 @@ enum avic_ipi_failure_cause {#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF)#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL +#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0)struct vmcb_seg {u16 selector;
arch/x86/kvm/svm/sev.c +42 -2
79 lines not on this page · raw message
arch/x86/kvm/svm/svm.h +4 -0
@@ -73,6 +73,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 */
@@ -88,6 +91,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
@@ -1917,6 +1917,9 @@ enum sev_cmd_id {/* Guest Migration Extension */ KVM_SEV_SEND_CANCEL , +/* SNP specific commands */ +KVM_SEV_SNP_INIT , + KVM_SEV_NR_MAX, };
@@ -2013,6 +2016,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 v8 34/56 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 | 121 +++++++++++++++++- arch/x86/kvm/svm/svm.h | 1 + include/uapi/linux/kvm.h | 10 ++ 4 files changed, 153 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 +118 -3
167 lines not on this page · raw message
arch/x86/kvm/svm/svm.h +1 -0
@@ -92,6 +92,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
@@ -1919,6 +1919,7 @@ enum sev_cmd_id {/* SNP specific commands */ KVM_SEV_SNP_INIT , +KVM_SEV_SNP_LAUNCH_START , KVM_SEV_NR_MAX, };
@@ -2026,6 +2027,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
RFC PATCH v8 35/56 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 | 29 +++ arch/x86/kvm/svm/sev.c | 190 ++++++++++++++++++ include/uapi/linux/kvm.h | 19 ++ 3 files changed, 238 insertions(+)
Documentation/virt/kvm/x86/amd-memory-encryption.rst +29 -0
@@ -485,6 +485,35 @@ 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 +190 -0
208 lines not on this page · raw message
include/uapi/linux/kvm.h +19 -0
@@ -1920,6 +1920,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, };
@@ -2036,6 +2037,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 v8 36/56 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 | 23 ++++ arch/x86/kvm/svm/sev.c | 122 ++++++++++++++++++ include/uapi/linux/kvm.h | 14 ++ 3 files changed, 159 insertions(+)
Documentation/virt/kvm/x86/amd-memory-encryption.rst +23 -0
@@ -513,6 +513,29 @@ 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 +122 -0
145 lines not on this page · raw message
include/uapi/linux/kvm.h +14 -0
@@ -1921,6 +1921,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, };
@@ -2055,6 +2056,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 v8 37/56 KVM: X86: Keep the NPT and RMP page level in sync
In reply to Michael RothFrom: Brijesh Singh <[email protected]>
When running an SEV-SNP VM, the sPA used to index the RMP entry is obtained through the NPT translation (gva->gpa->spa). The NPT page level is checked against the page level programmed in the RMP entry. If the page level does not match, then it will cause a nested page fault with the RMP bit set to indicate the RMP violation.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 9 ++++++ arch/x86/kvm/svm/sev.c | 51 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 2 ++ arch/x86/kvm/svm/svm.h | 1 + 6 files changed, 66 insertions(+)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -135,6 +135,7 @@ KVM_X86_OP_OPTIONAL(alloc_apic_backing_page)KVM_X86_OP_OPTIONAL_RET0 (fault_is_private); KVM_X86_OP_OPTIONAL_RET0(update_mem_attr) KVM_X86_OP_OPTIONAL(invalidate_restricted_mem) +KVM_X86_OP_OPTIONAL(adjust_mapping_level)#undef KVM_X86_OP#undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1731,6 +1731,8 @@ struct kvm_x86_ops {unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu);void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); + +void (*adjust_mapping_level)(struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int *level); };struct kvm_x86_nested_ops {
arch/x86/kvm/mmu/mmu.c +9 -0
@@ -3081,6 +3081,7 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn, out:local_irq_restore (flags); +return level; }
@@ -3141,6 +3142,14 @@ void kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault fault->req_level =__kvm_mmu_max_mapping_level (vcpu->kvm, slot, fault->gfn, fault->max_level, fault->is_private); +if (kvm_slot_can_be_private (slot)) { +int req_level = fault->req_level; + +static_call_cond (kvm_x86_adjust_mapping_level)(vcpu->kvm, fault->gfn, fault->pfn, + &req_level); + fault->req_level = req_level; + } +if (fault->req_level ==PG_LEVEL_4K || fault->huge_page_disallowed)return ;
arch/x86/kvm/svm/sev.c +51 -0
54 lines not on this page · raw message
arch/x86/kvm/svm/svm.c +2 -0
@@ -4835,6 +4835,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, + + .adjust_mapping_level = sev_adjust_mapping_level, };/*
arch/x86/kvm/svm/svm.h +1 -0
@@ -706,6 +706,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 sev_adjust_mapping_level (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int *level);/* vmenter.S */
Signature · 1 line
2.25.1
RFC PATCH v8 38/56 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 v8 39/56 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
@@ -3424,6 +3424,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;
@@ -3490,6 +3511,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
@@ -204,6 +204,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 {
@@ -336,6 +338,11 @@ static 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 v8 40/56 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.
--- include/uapi/linux/kvm.h | 6 ++++++ 1 file changed, 6 insertions(+)
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 */ +__u8 error;/* user -> kernel */ + } vmgexit;/* Fix the size of the union. */ char padding[256 ]; };
Signature · 1 line
2.25.1
RFC PATCH v8 3/56 KVM: x86: Add platform hooks for private memory invalidations
In reply to Michael RothIn 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 to the invalidation notifiers for restricted memory.
Also issue invalidations of all allocated pages during notifier/memslot unbinding so that the pages are not left in an unusable state when they eventually get freed back to the host upon FD release.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 5 +++++ include/linux/kvm_host.h | 3 +++ mm/restrictedmem.c | 12 +++++++++++- virt/kvm/kvm_main.c | 12 +++++++++++- 6 files changed, 32 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -133,6 +133,7 @@ KVM_X86_OP(vcpu_deliver_sipi_vector)KVM_X86_OP_OPTIONAL_RET0 (vcpu_get_apicv_inhibit_reasons);KVM_X86_OP_OPTIONAL_RET0 (fault_is_private); KVM_X86_OP_OPTIONAL_RET0(update_mem_attr) +KVM_X86_OP_OPTIONAL(invalidate_restricted_mem)#undef KVM_X86_OP#undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1646,6 +1646,7 @@ struct kvm_x86_ops {bool (*fault_is_private)(struct kvm *kvm,gpa_t gpa,u64 error_code,bool *private_fault);int (*update_mem_attr)(struct kvm_memory_slot *slot,unsigned int attr,gfn_t start,gfn_t end); +void (*invalidate_restricted_mem)(struct kvm_memory_slot *slot,gfn_t start,gfn_t end);bool (*has_wbinvd_exit)(void );
arch/x86/kvm/mmu/mmu.c +5 -0
@@ -7264,4 +7264,9 @@ void kvm_arch_post_set_memory_attributes(struct kvm *kvm,pr_warn_ratelimited ("Failed to update GFN range 0x%llx-0x%llx with attributes 0x%lx. Ret: %d\n" , start, end, attrs, ret); } + +void kvm_arch_invalidate_restricted_mem (struct kvm_memory_slot *slot,gfn_t start,gfn_t end) +{ +static_call_cond (kvm_x86_invalidate_restricted_mem)(slot, start, end); +}#endif
include/linux/kvm_host.h +3 -0
@@ -2341,6 +2341,9 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE; } + +void kvm_arch_invalidate_restricted_mem (struct kvm_memory_slot *slot,gfn_t start,gfn_t end); +#else static inline bool kvm_mem_is_private (struct kvm *kvm,gfn_t gfn) {
mm/restrictedmem.c +11 -1
@@ -17,7 +17,7 @@ struct restrictedmem {static int restrictedmem_release (struct inode *inode,struct file *file) { -struct restrictedmem *rm = inode->i_mapping->private_data; +struct restrictedmem *rm = file->f_mapping->private_data;xa_destroy (&rm->bindings);fput (rm->memfd);
@@ -305,10 +305,20 @@ void restrictedmem_unbind(struct file *file, pgoff_t start, pgoff_t end, struct restrictedmem_notifier *notifier) {struct restrictedmem *rm = file->f_mapping->private_data; +unsigned long index; +pr_debug ("%s: unregistering notifier, invalidating page offsets 0x%lx-0x%lx\n" , + __func__, start, end);down_write (&rm->lock); + +xa_for_each_range (&rm->bindings, index, notifier, start, end) + notifier->ops->invalidate_start (notifier, start, end); +xa_for_each_range (&rm->bindings, index, notifier, start, end) + notifier->ops->invalidate_end (notifier, start, end); +xa_store_range (&rm->bindings, start, end,NULL ,GFP_KERNEL );synchronize_rcu (); +up_write (&rm->lock); }EXPORT_SYMBOL_GPL (restrictedmem_unbind);
virt/kvm/kvm_main.c +11 -1
@@ -960,8 +960,15 @@ static void kvm_restrictedmem_invalidate_begin(struct restrictedmem_notifier *nostruct kvm *kvm = slot->kvm;int idx; -if (restrictedmem_get_gfn_range (slot, start, end, &gfn_range)) +if (restrictedmem_get_gfn_range (slot, start, end, &gfn_range)) { +pr_debug ("%s: Invalidation skipped, slot: %d, start: 0x%lx, end: 0x%lx, restrictedmem.index: 0x%lx\n" , + __func__, slot->id, start, end, slot->restrictedmem.index);return ; + } + +pr_debug ("%s: slot: %d, start: 0x%lx, end: 0x%lx, restrictedmem.index: 0x%lx, gfn_start: 0x%llx, gfn_end: 0x%llx\n" , + __func__, slot->id, start, end, slot->restrictedmem.index, gfn_range.start, + gfn_range.end); idx =srcu_read_lock (&kvm->srcu);KVM_MMU_LOCK (kvm);
@@ -972,7 +979,10 @@ static void kvm_restrictedmem_invalidate_begin(struct restrictedmem_notifier *nokvm_flush_remote_tlbs (kvm);KVM_MMU_UNLOCK (kvm); +srcu_read_unlock (&kvm->srcu, idx); + +kvm_arch_invalidate_restricted_mem (slot, gfn_range.start, gfn_range.end); } static void kvm_restrictedmem_invalidate_end(struct restrictedmem_notifier *notifier,
Signature · 1 line
2.25.1
RFC PATCH v8 41/56 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.
Forward these requests to userspace via KVM_EXIT_VMGEXIT so the VMM can issue the KVM ioctls to update the page state accordingly.
--- arch/x86/include/asm/sev-common.h | 9 ++++++++ arch/x86/kvm/svm/sev.c | 25 +++++++++++++++++++++++ arch/x86/kvm/trace.h | 34 +++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 1 + 4 files changed, 69 insertions(+)
arch/x86/include/asm/sev-common.h +9 -0
@@ -93,6 +93,10 @@ enum psc_op { };#define GHCB_MSR_PSC_REQ 0x014 +#define GHCB_MSR_PSC_GFN_POS 12 +#define GHCB_MSR_PSC_GFN_MASK GENMASK_ULL(39, 0) +#define GHCB_MSR_PSC_OP_POS 52 +#define GHCB_MSR_PSC_OP_MASK 0xf#define GHCB_MSR_PSC_REQ_GFN (gfn, op) \/* GHCBData[55:52] */ \ (((u64)((op) & 0xf) << 52) | \
@@ -102,6 +106,11 @@ enum psc_op { GHCB_MSR_PSC_REQ)#define GHCB_MSR_PSC_RESP 0x015 +#define GHCB_MSR_PSC_ERROR_POS 32 +#define GHCB_MSR_PSC_ERROR_MASK GENMASK_ULL(31, 0) +#define GHCB_MSR_PSC_ERROR GENMASK_ULL(31, 0) +#define GHCB_MSR_PSC_RSVD_POS 12 +#define GHCB_MSR_PSC_RSVD_MASK GENMASK_ULL(19, 0)#define GHCB_MSR_PSC_RESP_VAL (val) \/* GHCBData[63:32] */ \ (((u64)(val) & GENMASK_ULL(63, 32)) >> 32)
arch/x86/kvm/svm/sev.c +25 -0
@@ -30,6 +30,7 @@#include "svm_ops.h" #include "cpuid.h" #include "trace.h" +#include "mmu.h" #ifndef CONFIG_KVM_AMD_SEV /*
@@ -3345,6 +3346,23 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) svm->vmcb->control.ghcb_gpa = value; } +/* +* TODO: need to get the value set by userspace in vcpu->run->vmgexit.ghcb_msr +* and process that here accordingly. +*/ +static int snp_complete_psc_msr_protocol (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); + +set_ghcb_msr_bits (svm,0 , +GHCB_MSR_PSC_ERROR_MASK ,GHCB_MSR_PSC_ERROR_POS ); + +set_ghcb_msr_bits (svm,0 ,GHCB_MSR_PSC_RSVD_MASK ,GHCB_MSR_PSC_RSVD_POS ); +set_ghcb_msr_bits (svm,GHCB_MSR_PSC_RESP ,GHCB_MSR_INFO_MASK ,GHCB_MSR_INFO_POS ); + +return 1 ;/* resume */ +} +static int sev_handle_vmgexit_msr_protocol (struct vcpu_svm *svm) {struct vmcb_control_area *control = &svm->vmcb->control;
@@ -3445,6 +3463,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;
arch/x86/kvm/trace.h +34 -0
@@ -7,6 +7,7 @@#include <asm/svm.h> #include <asm/clocksource.h> #include <asm/pvclock-abi.h> +#include <asm/sev-common.h> #undef TRACE_SYSTEM#define TRACE_SYSTEM kvm
@@ -1831,6 +1832,39 @@ TRACE_EVENT(kvm_vmgexit_msr_protocol_exit, __entry->vcpu_id, __entry->ghcb_gpa, __entry->result) ); +/* +* Tracepoint for the SEV-SNP page state change processing +*/ +#define psc_operation \ + {SNP_PAGE_STATE_PRIVATE, "private"}, \ + {SNP_PAGE_STATE_SHARED, "shared"} \ + +TRACE_EVENT (kvm_snp_psc, + TP_PROTO(unsigned int vcpu_id, u64 pfn, u64 gpa, u8 op, int level), +TP_ARGS (vcpu_id, pfn, gpa, op, level), + +TP_STRUCT__entry ( + __field(int, vcpu_id) + __field(u64, pfn) + __field(u64, gpa) + __field(u8, op) +__field (int, level) + ), + +TP_fast_assign ( + __entry->vcpu_id = vcpu_id; + __entry->pfn = pfn; + __entry->gpa = gpa; + __entry->op = op; + __entry->level = level; + ), + +TP_printk ("vcpu %u, pfn %llx, gpa %llx, op %s, level %d" , + __entry->vcpu_id, __entry->pfn, __entry->gpa, +__print_symbolic (__entry->op, psc_operation), + __entry->level) +); +#endif /* _TRACE_KVM_H */ #undef TRACE_INCLUDE_PATH
arch/x86/kvm/x86.c +1 -0
@@ -13515,6 +13515,7 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_enter);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_vmgexit_exit);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_vmgexit_msr_protocol_enter);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_vmgexit_msr_protocol_exit); +EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_snp_psc);static int __init kvm_x86_init (void ) {
Signature · 1 line
2.25.1
RFC PATCH v8 42/56 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/include/asm/sev-common.h | 7 +++++++ arch/x86/kvm/svm/sev.c | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+)
arch/x86/include/asm/sev-common.h +7 -0
@@ -130,6 +130,13 @@ enum psc_op {/* SNP Page State Change NAE event */ #define VMGEXIT_PSC_MAX_ENTRY 253 +/* The page state change hdr structure in not valid */ +#define PSC_INVALID_HDR 1 +/* The hdr.cur_entry or hdr.end_entry is not valid */ +#define PSC_INVALID_ENTRY 2 +/* Page state change encountered undefined error */ +#define PSC_UNDEF_ERR 3 +struct psc_hdr {u16 cur_entry;u16 end_entry;
arch/x86/kvm/svm/sev.c +20 -0
@@ -3152,6 +3152,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 ;
@@ -3363,6 +3364,19 @@ static int snp_complete_psc_msr_protocol(struct kvm_vcpu *vcpu)return 1 ;/* resume */ } +/* +* TODO: need to process the GHCB contents and report the proper error code +* instead of assuming success. +*/ +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,0 ); + +return 1 ; +} +static int sev_handle_vmgexit_msr_protocol (struct vcpu_svm *svm) {struct vmcb_control_area *control = &svm->vmcb->control;
@@ -3606,6 +3620,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 v8 43/56 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
@@ -1845,6 +1845,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
@@ -211,8 +211,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
@@ -6615,6 +6615,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 v8 44/56 KVM: SVM: Add support to handle the RMP nested page fault
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.
Page state changes are handled by userspace, so if an RMP fault is triggered as a result of an RMP NPT fault, exit to userspace just like with explicit page-state change requests.
RMP NPT faults can also occur if the guest pvalidates a 2M page as 4K, in which case the RMP entries need to be PSMASH'd. Handle this case immediately in the kernel.
--- arch/x86/kvm/svm/sev.c | 84 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 21 +++++++++-- arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 102 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/sev.c +84 -0
@@ -3347,6 +3347,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); +} +/* * TODO: need to get the value set by userspace in vcpu->run->vmgexit.ghcb_msr * and process that here accordingly.
@@ -3872,3 +3879,80 @@ void sev_adjust_mapping_level(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int *lepr_debug ("%s: GFN: 0x%llx, PFN: 0x%llx, level: %d, rmp_level: %d, level_orig: %d, assigned: %d\n" , __func__, gfn, pfn, *level, rmp_level, level_orig, assigned); } + +void handle_rmp_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u64 error_code) +{ +int order, rmp_level, assigned, ret; +struct kvm_memory_slot *slot; +struct kvm *kvm = vcpu->kvm; +kvm_pfn_t pfn; +gfn_t gfn; + +/* +* Private memslots punt handling of implicit page state changes to +* userspace, so the only RMP faults expected here for +* PFERR_GUEST_SIZEM_MASK. Anything else suggests that the RMP table has +* gotten out of sync with the private memslot. +* +* TODO: However, this case has also been noticed when an access occurs +* to an NPT mapping that has just been split/PSMASHED, in which case +* PFERR_GUEST_SIZEM_MASK might not be set. In those cases it should be +* safe to ignore and let the guest retry, but log these just in case +* for now. +*/ +if (!(error_code &PFERR_GUEST_SIZEM_MASK )) { +pr_warn_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_restrictedmem_get_pfn (slot, gfn, &pfn, &order); +if (ret) { +pr_warn_ratelimited ("Unexpected RMP fault, no private backing page for GPA 0x%llx\n" , + gpa); +return ; + } + + assigned =snp_lookup_rmpentry (pfn, &rmp_level); +if (assigned !=1 ) { +pr_warn_ratelimited ("Unexpected RMP fault, no assigned RMP entry for GPA 0x%llx\n" , + gpa); + 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
@@ -1976,15 +1976,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
@@ -714,6 +714,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 sev_adjust_mapping_level (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int *level); +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 v8 45/56 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 | 185 +++++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/svm/svm.h | 2 + 2 files changed, 181 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/sev.c +179 -6
@@ -327,6 +327,7 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)if (ret) goto e_free; +mutex_init (&sev->guest_req_lock); ret =sev_snp_init (&argp->error, false); }else { ret =sev_platform_init (&argp->error);
@@ -2059,23 +2060,34 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)*/ static void *snp_context_create (struct kvm *kvm,struct kvm_sev_cmd *argp) { +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;struct sev_data_snp_addr data = {}; -void *context; +void *context, *certs_data;int rc; +/* Allocate memory used for the certs data in SNP guest request */ + certs_data =kzalloc (SEV_FW_BLOB_MAX_SIZE ,GFP_KERNEL_ACCOUNT ); +if (!certs_data) +return NULL ; +/* Allocate memory for context page */ context =snp_alloc_firmware_page (GFP_KERNEL_ACCOUNT );if (!context) -return NULL ; + goto e_free; data.gctx_paddr =__psp_pa (context); rc =__sev_issue_cmd (argp->sev_fd,SEV_CMD_SNP_GCTX_CREATE , &data, &argp->error); -if (rc) { -snp_free_firmware_page (context); -return NULL ; - } +if (rc) + goto e_free; + + sev->snp_certs_data = certs_data;return context; + +e_free: +snp_free_firmware_page (context); +kfree (certs_data); +return NULL ; }static int snp_bind_asid (struct kvm *kvm,int *error)
@@ -2693,6 +2705,8 @@ static int snp_decommission_context(struct kvm *kvm)snp_free_firmware_page (sev->snp_context); sev->snp_context =NULL ; +kfree (sev->snp_certs_data); +return 0 ; }
@@ -3153,6 +3167,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 ;
@@ -3384,6 +3400,149 @@ static int snp_complete_psc(struct kvm_vcpu *vcpu)return 1 ; } +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) +/* use the firmware error code */ + rc = err; + +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 kvm_vcpu *vcpu = &svm->vcpu; +struct kvm *kvm = vcpu->kvm; +unsigned long data_npages; +struct kvm_sev_info *sev; +unsigned long rc, err; +u64 data_gpa; + +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 )) { + rc =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; + + rc =snp_guest_ext_guest_request (&req, (unsigned long )sev->snp_certs_data, + &data_npages, &err); +if (rc) { +/* +* If buffer length is small then return the expected +* length in rbx. +*/ +if (err ==SNP_GUEST_REQ_INVALID_LEN ) + vcpu->arch.regs[VCPU_REGS_RBX ] = data_npages; + +/* pass the firmware error code */ + rc = err; + goto cleanup; + } + +/* Copy the certificate blob in the guest memory */ +if (data_npages && +kvm_write_guest (kvm, data_gpa, sev->snp_certs_data, data_npages <<PAGE_SHIFT )) + rc =SEV_RET_INVALID_ADDRESS ; + +cleanup: +snp_cleanup_guest_buf (&req, &rc); + +unlock: +mutex_unlock (&sev->guest_req_lock); + +e_fail: +ghcb_set_sw_exit_info_2 (svm->sev_es.ghcb, rc); +} +static int sev_handle_vmgexit_msr_protocol (struct vcpu_svm *svm) {struct vmcb_control_area *control = &svm->vmcb->control;
@@ -3633,6 +3792,20 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) vcpu->run->vmgexit.ghcb_msr = ghcb_gpa; vcpu->arch.complete_userspace_io = snp_complete_psc;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 : vcpu_unimpl(vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n",
arch/x86/kvm/svm/svm.h +2 -0
@@ -93,6 +93,8 @@ struct kvm_sev_info {atomic_t migration_in_progress;u64 snp_init_flags;void *snp_context;/* SNP guest context page */ +void *snp_certs_data; +struct mutex guest_req_lock;/* Lock for guest request handling */ };struct kvm_svm {
Signature · 1 line
2.25.1
RFC PATCH v8 46/56 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
@@ -3849,10 +3849,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
@@ -1410,9 +1410,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
@@ -198,6 +198,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 v8 47/56 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 | 245 ++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 3 + arch/x86/kvm/svm/svm.h | 7 + arch/x86/kvm/x86.c | 9 ++ 6 files changed, 271 insertions(+), 1 deletion(-)
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
@@ -278,7 +278,12 @@ enum avic_ipi_failure_cause {#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF)#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL -#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 +245 -0
281 lines not on this page · raw message
arch/x86/kvm/svm/svm.c +3 -0
@@ -1349,6 +1349,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 -0
@@ -95,6 +95,8 @@ struct kvm_sev_info {void *snp_context;/* SNP guest context page */ void *snp_certs_data;struct mutex guest_req_lock;/* Lock for guest request handling */ + +u64 sev_features;/* Features set at VMSA creation */ };struct kvm_svm {
@@ -209,6 +211,10 @@ struct vcpu_sev_es_state {bool ghcb_sa_free;u64 ghcb_registered_gpa; + +struct mutex snp_vmsa_mutex; +gpa_t snp_vmsa_gpa; +bool snp_ap_create; };struct vcpu_svm {
@@ -718,6 +724,7 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm);struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu);void sev_adjust_mapping_level (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int *level);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 +9 -0
@@ -10501,6 +10501,12 @@ 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 ) + goto out; + } } if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win ||
@@ -12698,6 +12704,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
RFC PATCH v8 48/56 KVM: SVM: Add SNP-specific handling for memory attribute updates
In reply to Michael RothThis will handle RMP table updates and direct map changes needed for page state conversions requested by userspace.
--- arch/x86/kvm/svm/sev.c | 126 +++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 2 + 3 files changed, 129 insertions(+)
arch/x86/kvm/svm/sev.c +126 -0
@@ -3381,6 +3381,31 @@ static int snp_rmptable_psmash(struct kvm *kvm, kvm_pfn_t pfn)return psmash (pfn); } +static int snp_make_page_shared (struct kvm *kvm,gpa_t gpa,kvm_pfn_t pfn,int level) +{ +int rc, rmp_level; + + rc =snp_lookup_rmpentry (pfn, &rmp_level); +if (rc <0 ) +return -EINVAL ; + +/* If page is not assigned then do nothing */ +if (!rc) +return 0 ; + +/* +* Is the page part of an existing 2MB RMP entry ? Split the 2MB into +* multiple of 4K-page before making the memory shared. +*/ +if (level ==PG_LEVEL_4K && rmp_level ==PG_LEVEL_2M ) { + rc =snp_rmptable_psmash (kvm, pfn); +if (rc) +return rc; + } + +return rmp_make_shared (pfn, level); +} +/* * TODO: need to get the value set by userspace in vcpu->run->vmgexit.ghcb_msr * and process that here accordingly.
@@ -4373,3 +4398,104 @@ 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)); } + +static inline u8 order_to_level (int order) +{ +BUILD_BUG_ON (KVM_MAX_HUGEPAGE_LEVEL >PG_LEVEL_1G ); + +if (order >=KVM_HPAGE_GFN_SHIFT (PG_LEVEL_1G )) +return PG_LEVEL_1G ; + +if (order >=KVM_HPAGE_GFN_SHIFT (PG_LEVEL_2M )) +return PG_LEVEL_2M ; + +return PG_LEVEL_4K ; +} + +int sev_update_mem_attr (struct kvm_memory_slot *slot,unsigned int attr, +gfn_t start,gfn_t end) +{ +struct kvm_sev_info *sev = &to_kvm_svm (slot->kvm)->sev_info; +enum psc_op op = (attr &KVM_MEMORY_ATTRIBUTE_PRIVATE ) ?SNP_PAGE_STATE_PRIVATE + :SNP_PAGE_STATE_SHARED ; +gfn_t gfn = start; + +pr_debug ("%s: GFN 0x%llx - 0x%llx, op: %d\n" , __func__, start, end, op); + +if (!sev_snp_guest (slot->kvm)) +return 0 ; + +if (!kvm_slot_can_be_private (slot)) { +pr_err_ratelimited ("%s: memslot for gfn: 0x%llx is not private.\n" , + __func__, gfn); +return -EPERM ; + } + +while (gfn < end) { +kvm_pfn_t pfn; +int level =PG_LEVEL_4K ;/* TODO: take actual order into account */ +gpa_t gpa =gfn_to_gpa (gfn); +int npages =1 ; +int order; +int rc; + +/* +* No work to do if there was never a page allocated from private +* memory. If there was a page that was deallocated previously, +* the invalidation notifier should have restored the page to +* shared. +*/ + rc =kvm_restrictedmem_get_pfn (slot, gfn, &pfn, &order); +if (rc) { +pr_warn_ratelimited ("%s: failed to retrieve gfn 0x%llx from private FD\n" , + __func__, gfn); + gfn++; +continue ; + } + +/* +* TODO: The RMP entry's hugepage bit is ignored for +* shared/unassigned pages. Either handle looping through each +* sub-page as part of snp_make_page_shared(), or remove the +* level argument. +*/ +if (op ==SNP_PAGE_STATE_PRIVATE && order && +IS_ALIGNED (gfn,1 << order) && (gfn + (1 << order)) <= end) { + level =order_to_level (order); + npages =1 << order; + } + +/* +* Grab the PFN from private memslot and update the RMP entry. +* It may be worthwhile to go ahead and map it into the TDP at +* this point if the guest is doing lazy acceptance, but for +* up-front bulk shared->private conversions it's not likely +* the guest will try to access the PFN any time soon, so for +* now just take the let KVM MMU handle faulting it on the next +* access. +*/ +switch (op) { +case SNP_PAGE_STATE_SHARED : + rc =snp_make_page_shared (slot->kvm, gpa, pfn, level); +break ; +case SNP_PAGE_STATE_PRIVATE : + rc =rmp_make_private (pfn, gpa, level, sev->asid, false); +break ; +default : + rc =PSC_INVALID_ENTRY ; +break ; + } + +put_page (pfn_to_page (pfn)); + +if (rc) { +pr_err_ratelimited ("%s: failed op %d gpa %llx pfn %llx level %d rc %d\n" , + __func__, op, gpa, pfn, level, rc); +return -EINVAL ; + } + + gfn += npages; + } + +return 0 ; +}
arch/x86/kvm/svm/svm.c +1 -0
@@ -4860,6 +4860,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .alloc_apic_backing_page = svm_alloc_apic_backing_page, .adjust_mapping_level = sev_adjust_mapping_level, + .update_mem_attr = sev_update_mem_attr, };/*
arch/x86/kvm/svm/svm.h +2 -0
@@ -725,6 +725,8 @@ struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu);void sev_adjust_mapping_level (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int *level);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_update_mem_attr (struct kvm_memory_slot *slot,unsigned int attr, +gfn_t start,gfn_t end);/* vmenter.S */
Signature · 1 line
2.25.1
RFC PATCH v8 49/56 KVM: SVM: Implement .fault_is_private callback for SNP
In reply to Michael RothKVM MMU will use this to determine whether an #NPF should be serviced with restricted memory or not.
--- arch/x86/kvm/svm/sev.c | 10 ++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 2 ++ 3 files changed, 13 insertions(+)
arch/x86/kvm/svm/sev.c +10 -0
@@ -4499,3 +4499,13 @@ int sev_update_mem_attr(struct kvm_memory_slot *slot, unsigned int attr,return 0 ; } + +bool sev_fault_is_private (struct kvm *kvm,gpa_t gpa,u64 error_code,bool *private_fault) +{ +if (!sev_snp_guest (kvm)) +return false; + + *private_fault = (error_code &PFERR_GUEST_ENC_MASK ) ? true : false; + +return true; +}
arch/x86/kvm/svm/svm.c +1 -0
@@ -4861,6 +4861,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .adjust_mapping_level = sev_adjust_mapping_level, .update_mem_attr = sev_update_mem_attr, + .fault_is_private = sev_fault_is_private, };/*
arch/x86/kvm/svm/svm.h +2 -0
@@ -728,6 +728,8 @@ void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu);int sev_update_mem_attr (struct kvm_memory_slot *slot,unsigned int attr,gfn_t start,gfn_t end); +bool sev_fault_is_private (struct kvm *kvm,gpa_t gpa,u64 error_code,bool *private_fault); +/* vmenter.S */ void __svm_sev_es_vcpu_run (struct vcpu_svm *svm,bool spec_ctrl_intercepted);
Signature · 1 line
2.25.1
RFC PATCH v8 50/56 KVM: SEV: Handle restricted memory invalidations for SNP
In reply to Michael RothImplement a platform hook to do the work of restoring the direct map entries and cleaning up RMP table entries for restricted memory that is being freed back to the host.
--- arch/x86/kvm/svm/sev.c | 62 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 64 insertions(+)
arch/x86/kvm/svm/sev.c +62 -0
@@ -4509,3 +4509,65 @@ bool sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *privreturn true; } + +void sev_invalidate_private_range (struct kvm_memory_slot *slot,gfn_t start,gfn_t end) +{ +gfn_t gfn = start; + +if (!sev_snp_guest (slot->kvm)) +return ; + +if (!kvm_slot_can_be_private (slot)) { +pr_warn_ratelimited ("SEV: Memslot for GFN: 0x%llx is not private.\n" , + gfn); +return ; + } + +while (gfn <= end) { +gpa_t gpa =gfn_to_gpa (gfn); +int level =PG_LEVEL_4K ; +int order, rc; +kvm_pfn_t pfn; + + rc =kvm_restrictedmem_get_pfn (slot, gfn, &pfn, &order); +if (rc) { +pr_warn_ratelimited ("SEV: Failed to retrieve restricted PFN for GFN 0x%llx, rc: %d\n" , + gfn, rc); + gfn++; +continue ; + } + +if (order) { +int rmp_level; + +if (IS_ALIGNED (gpa,page_level_size (PG_LEVEL_2M )) && + gpa +page_level_size (PG_LEVEL_2M ) <=gfn_to_gpa (end)) + level =PG_LEVEL_2M ; +else +pr_debug ("%s: GPA 0x%llx is not aligned to 2M, skipping 2M directmap restoration\n" , + __func__, gpa); + +/* +* TODO: It may still be possible to restore 2M mapping here, +* but keep it simple for now. +*/ +if (level ==PG_LEVEL_2M && + (!snp_lookup_rmpentry (pfn, &rmp_level) || rmp_level ==PG_LEVEL_4K )) { +pr_debug ("%s: PFN 0x%llx is not mapped as 2M private range, skipping 2M directmap restoration\n" , + __func__, pfn); + level =PG_LEVEL_4K ; + } + } + +pr_debug ("%s: GPA %llx PFN %llx order %d level %d\n" , + __func__, gpa, pfn, order, level); + rc =snp_make_page_shared (slot->kvm, gpa, pfn, level); +if (rc) +pr_err ("SEV: Failed to restore page to shared, GPA: 0x%llx PFN: 0x%llx order: %d rc: %d\n" , + gpa, pfn, order, rc); + + gfn +=page_level_size (level) >>PAGE_SHIFT ; +put_page (pfn_to_page (pfn)); +cond_resched (); + } +}
arch/x86/kvm/svm/svm.c +1 -0
@@ -4862,6 +4862,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .adjust_mapping_level = sev_adjust_mapping_level, .update_mem_attr = sev_update_mem_attr, .fault_is_private = sev_fault_is_private, + .invalidate_restricted_mem = sev_invalidate_private_range, };/*
arch/x86/kvm/svm/svm.h +1 -0
@@ -727,6 +727,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_update_mem_attr (struct kvm_memory_slot *slot,unsigned int attr,gfn_t start,gfn_t end); +void sev_invalidate_private_range (struct kvm_memory_slot *slot,gfn_t start,gfn_t end);bool sev_fault_is_private (struct kvm *kvm,gpa_t gpa,u64 error_code,bool *private_fault);
Signature · 1 line
2.25.1
RFC PATCH v8 4/56 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 | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+)
include/linux/kvm_host.h +6 -0
@@ -1402,6 +1402,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 +48 -0
54 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC PATCH v8 51/56 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 | 7 ++++--- arch/x86/kvm/svm/svm.h | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/sev.c +4 -3
@@ -55,14 +55,15 @@ 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#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
arch/x86/kvm/svm/svm.h +1 -1
@@ -694,7 +694,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 0 +#define GHCB_HV_FT_SUPPORTED (GHCB_HV_FT_SNP | GHCB_HV_FT_SNP_AP_CREATION)extern unsigned int max_sev_asid;
Signature · 1 line
2.25.1
RFC PATCH v8 52/56 ccp: Add support to decrypt the page
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 | 22 ++++++++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-)
drivers/crypto/ccp/sev-dev.c +32 -0
@@ -2017,6 +2017,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); +int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data,unsigned long vaddr,unsigned long *npages,unsigned long *fw_err) {
include/linux/psp-sev.h +20 -2
@@ -710,7 +710,6 @@ struct sev_data_snp_dbg {u64 gctx_paddr;/* In */ u64 src_addr;/* In */ u64 dst_addr;/* In */ -u32 len;/* In */ } __packed;/**
@@ -913,13 +912,27 @@ int sev_guest_decommission(struct sev_data_decommission *data, int *error);* @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); + +/** +* 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 sev_do_cmd (int cmd,void *data,int *psp_ret); +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);
@@ -987,6 +1000,11 @@ static inline void *psp_copy_user_blob(u64 __user uaddr, u32 len) { return ERR_Pvoid snp_mark_pages_offline (unsigned long pfn,unsigned int npages) {} +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
RFC PATCH v8 53/56 KVM: SVM: Make VMSAVE target area memory allocation SNP safe
In reply to Michael RothFrom: Ashish Kalra <[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 the VMSAVE target page.
When SEV-SNP is globally enabled, the CPU marks the VMSAVE target page as "InUse" while the VMSAVE instruction is executing. If another CPU writes to a different page in the same 2MB region while the VMSAVE is executing, the CPU will throw an RMP violation #PF.
Use the snp safe generic allocator for allocating the VMSA target page which will ensure that the page returned is not a hugepage, as it is already being used for the allocating the VMCB, VMSA and AVIC backing page.
--- arch/x86/kvm/svm/svm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +1 -1
@@ -665,7 +665,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;
Signature · 1 line
2.25.1
RFC PATCH v8 54/56 x86/sev: Add KVM commands for 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.
--- arch/x86/kvm/svm/sev.c | 111 ++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/svm/svm.h | 1 + include/linux/psp-sev.h | 2 +- include/uapi/linux/kvm.h | 12 +++++ 4 files changed, 123 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/sev.c +109 -2
@@ -2089,6 +2089,7 @@ static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp) goto e_free; sev->snp_certs_data = certs_data; + sev->snp_certs_len =0 ;return context;
@@ -2404,6 +2405,86 @@ static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; } +static int snp_get_instance_certs (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct kvm_sev_snp_get_certs params; + +if (!sev_snp_guest (kvm)) +return -ENOTTY ; + +if (!sev->snp_context) +return -EINVAL ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data, +sizeof (params))) +return -EFAULT ; + +/* No instance certs set. */ +if (!sev->snp_certs_len) +return -ENOENT ; + +if (params.certs_len < sev->snp_certs_len) { +/* Output buffer too small. Return the required size. */ + params.certs_len = sev->snp_certs_len; + +if (copy_to_user ((void __user *)(uintptr_t )argp->data, ¶ms, +sizeof (params))) +return -EFAULT ; + +return -EINVAL ; + } + +if (copy_to_user ((void __user *)(uintptr_t )params.certs_uaddr, + sev->snp_certs_data, sev->snp_certs_len)) +return -EFAULT ; + +return 0 ; +} + +static int snp_set_instance_certs (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +unsigned long length =SEV_FW_BLOB_MAX_SIZE ; +void *to_certs = sev->snp_certs_data; +struct kvm_sev_snp_set_certs params; + +if (!sev_snp_guest (kvm)) +return -ENOTTY ; + +if (!sev->snp_context) +return -EINVAL ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data, +sizeof (params))) +return -EFAULT ; + +if (params.certs_len >SEV_FW_BLOB_MAX_SIZE ) +return -EINVAL ; + +/* +* Setting a length of 0 is the same as "uninstalling" instance- +* specific certificates. +*/ +if (params.certs_len ==0 ) { + sev->snp_certs_len =0 ; +return 0 ; + } + +/* Page-align the length */ + length = (params.certs_len +PAGE_SIZE -1 ) &PAGE_MASK ; + +if (copy_from_user (to_certs, + (void __user *)(uintptr_t )params.certs_uaddr, + params.certs_len)) { +return -EFAULT ; + } + + sev->snp_certs_len = length; + +return 0 ; +} +int sev_mem_enc_ioctl (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;
@@ -2503,6 +2584,12 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)case KVM_SEV_SNP_LAUNCH_FINISH : r =snp_launch_finish (kvm, &sev_cmd);break ; +case KVM_SEV_SNP_GET_CERTS : + r =snp_get_instance_certs (kvm, &sev_cmd); +break ; +case KVM_SEV_SNP_SET_CERTS : + r =snp_set_instance_certs (kvm, &sev_cmd); +break ;default : r = -EINVAL ; goto out;
@@ -3550,8 +3637,28 @@ static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gpif (rc) goto unlock; - rc =snp_guest_ext_guest_request (&req, (unsigned long )sev->snp_certs_data, - &data_npages, &err); +/* +* If the VMM has overridden the certs, then change the error message +* if the size is inappropriate for the override. Otherwise, use a +* regular guest request and copy back the instance certs. +*/ +if (sev->snp_certs_len) { +if ((data_npages <<PAGE_SHIFT ) < sev->snp_certs_len) { + rc = -EINVAL ; + err =SNP_GUEST_REQ_INVALID_LEN ; + goto datalen; + } + rc =sev_issue_cmd (kvm,SEV_CMD_SNP_GUEST_REQUEST , &req, + (int *)&err); + }else { + rc =snp_guest_ext_guest_request (&req, + (unsigned long )sev->snp_certs_data, + &data_npages, &err); + } +datalen: +if (sev->snp_certs_len) + data_npages = sev->snp_certs_len >>PAGE_SHIFT ; +if (rc) {/* * If buffer length is small then return the expected
arch/x86/kvm/svm/svm.h +1 -0
@@ -94,6 +94,7 @@ struct kvm_sev_info {u64 snp_init_flags;void *snp_context;/* SNP guest context page */ void *snp_certs_data; +unsigned int snp_certs_len;/* Size of instance override for certs */ struct mutex guest_req_lock;/* Lock for guest request handling */ u64 sev_features;/* Features set at VMSA creation */
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 */ /** * SEV platform state
include/uapi/linux/kvm.h +12 -0
@@ -1928,6 +1928,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, };
@@ -2075,6 +2077,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 v8 55/56 x86/sev: Document KVM_SEV_SNP_{G,S}ET_CERTS
In reply to Michael RothFrom: Dionna Glaze <[email protected]>
Update the KVM_MEMORY_ENCRYPT_OP documentation to include the new commands for overriding the host certificates that the guest receives from an extended guest request.
--- .../virt/kvm/x86/amd-memory-encryption.rst | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+)
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 ==========
Signature · 1 line
2.25.1
RFC PATCH v8 56/56 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 | 53 ++++++++++++++++++++++++++++++++++++ include/linux/amd-iommu.h | 1 + 3 files changed, 74 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/e820/types.h>
@@ -1503,6 +1504,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 +53 -0
63 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_AMD_MEM_ENCRYPT 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
RFC PATCH v8 5/56 KVM: SEV: Require KVM_PROTECTED_VM when AMD_MEM_ENCRYPT is enabled
In reply to Michael RothAMD_MEM_ENCRYPT implies SEV support, which now relies on support provided by the KVM_PROTECTED_VM config option.
An argument can be made that SEV running in non-protected-VM-mode is still possible, and so this should be configurable, but AMD_MEM_ENCRYPT will also imply SEV-SNP, for which KVM_PROTECTED_VM is required in all cases.
--- arch/x86/Kconfig | 1 + 1 file changed, 1 insertion(+)
arch/x86/Kconfig +1 -0
@@ -1546,6 +1546,7 @@ config AMD_MEM_ENCRYPT select INSTRUCTION_DECODER select ARCH_HAS_CC_PLATFORM select X86_MEM_ENCRYPT + select KVM_PROTECTED_VM help Say yes to enable support for the encryption of system memory. This requires an AMD processor that supports Secure Memory
Signature · 1 line
2.25.1
RFC PATCH v8 6/56 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
@@ -993,6 +993,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
@@ -2645,12 +2645,28 @@ static void kvm_post_mem_attrs_changed(struct kvm *kvm, unsigned long attrs, } } +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)
@@ -2665,8 +2681,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);
@@ -2674,11 +2688,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 v8 7/56 KVM: SEV: Populate private memory fd during LAUNCH_UPDATE_DATA
In reply to Michael RothFrom: Vishal Annapurve <[email protected]>
This change adds handling of HVA ranges to copy contents to private memory while doing sev launch update data.
mem_attr array is updated during LAUNCH_UPDATE_DATA to ensure that encrypted memory is marked as private.
[mdr: Use gfn_to_hva_memslot_prot() for shared GFN handler to deal with
read-only slots for ROMs. Split kvm_vm_set_region_attr into separate patch.]
--- arch/x86/kvm/svm/sev.c | 103 +++++++++++++++++++++++++++++++++++++---- virt/kvm/kvm_main.c | 2 + 2 files changed, 96 insertions(+), 9 deletions(-)
arch/x86/kvm/svm/sev.c +94 -9
@@ -494,23 +494,26 @@ static unsigned long get_num_contig_pages(unsigned long idx,return pages; } -static int sev_launch_update_data (struct kvm *kvm,struct kvm_sev_cmd *argp) +static int sev_launch_update_shared_gfn_handler (struct kvm *kvm, +struct kvm_gfn_range *range, +struct kvm_sev_cmd *argp) {unsigned long vaddr, vaddr_end, next_vaddr, npages, pages, size, i;struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; -struct kvm_sev_launch_update_data params;struct sev_data_launch_update_data data;struct page **inpages;int ret; -if (!sev_guest (kvm)) -return -ENOTTY ; - -if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params))) -return -EFAULT ; + vaddr =gfn_to_hva_memslot_prot (range->slot, range->start,NULL ); +pr_debug ("%s: shared GFN: %llx, slot.id: %d, slot.base_gfn: %llx, slot.userspace_addr: %lx, slot.flags: %x, vaddr: %lx\n" , + __func__, range->start, range->slot->id, range->slot->base_gfn, + range->slot->userspace_addr, range->slot->flags, vaddr); +if (kvm_is_error_hva (vaddr)) { +pr_err ("vaddr is erroneous 0x%lx\n" , vaddr); +return -EINVAL ; + } - vaddr = params.uaddr; - size = params.len; + size = (range->end - range->start) <<PAGE_SHIFT ; vaddr_end = vaddr + size;/* Lock the user memory. */
@@ -562,6 +565,88 @@ static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; } +static int sev_launch_update_priv_gfn_handler (struct kvm *kvm, +struct kvm_gfn_range *range, +struct kvm_sev_cmd *argp) +{ +struct sev_data_launch_update_data data; +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +gfn_t gfn; +kvm_pfn_t pfn; +struct kvm_memory_slot *memslot = range->slot; +int ret =0 ; + + data.reserved =0 ; + data.handle = sev->handle; + +for (gfn = range->start; gfn < range->end; gfn++) { +int order; +void *kvaddr; + + ret =kvm_restrictedmem_get_pfn (memslot, gfn, &pfn, &order); +if (ret) + goto e_ret; + + kvaddr =pfn_to_kaddr (pfn); +if (!virt_addr_valid (kvaddr)) { +pr_debug ("%s: Invalid kvaddr 0x%llx\n" , __func__, (uint64_t )kvaddr); + ret = -EINVAL ; + goto e_ret; + } + + ret =kvm_read_guest_page (kvm, gfn, kvaddr,0 ,PAGE_SIZE ); +if (ret) { +pr_debug ("%s: Guest read failed 0x%x\n" , __func__, ret); + goto e_ret; + } + +if (!cpu_feature_enabled (X86_FEATURE_SME_COHERENT )) +clflush_cache_range (kvaddr,PAGE_SIZE ); + + data.len =PAGE_SIZE ; + data.address =__sme_set (pfn <<PAGE_SHIFT ); + ret =sev_issue_cmd (kvm,SEV_CMD_LAUNCH_UPDATE_DATA , &data, &argp->error); +if (ret) + goto e_ret; +kvm_release_pfn_clean (pfn); + } + +/* +* Memory attribute updates via KVM_SET_MEMORY_ATTRIBUTES are serialized +* via kvm->slots_lock, so use the same protocol for updating them here. +*/ +mutex_lock (&kvm->slots_lock); +kvm_vm_set_region_attr (kvm, range->start, range->end,KVM_MEMORY_ATTRIBUTE_PRIVATE ); +mutex_unlock (&kvm->slots_lock); +e_ret: +return ret; +} + +static int sev_launch_update_gfn_handler (struct kvm *kvm,struct kvm_gfn_range *range, +void *data) +{ +struct kvm_sev_cmd *argp = (struct kvm_sev_cmd *)data; + +if (kvm_slot_can_be_private (range->slot)) +return sev_launch_update_priv_gfn_handler (kvm, range, argp); + +return sev_launch_update_shared_gfn_handler (kvm, range, argp); +} + +static int sev_launch_update_data (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_launch_update_data params; + +if (!sev_guest (kvm)) +return -ENOTTY ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params))) +return -EFAULT ; + +return kvm_vm_do_hva_range_op (kvm, params.uaddr, params.uaddr + params.len, + sev_launch_update_gfn_handler, argp); +} +static int sev_es_sync_vmsa (struct vcpu_svm *svm) {struct sev_es_save_area *save = svm->sev_es.vmsa;
virt/kvm/kvm_main.c +2 -0
@@ -689,6 +689,7 @@ int kvm_vm_do_hva_range_op(struct kvm *kvm, unsigned long hva_start,return ret; } +EXPORT_SYMBOL_GPL (kvm_vm_do_hva_range_op); static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn, unsigned long start,
@@ -2850,6 +2851,7 @@ unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot,return hva; } +EXPORT_SYMBOL_GPL (gfn_to_hva_memslot_prot);unsigned long gfn_to_hva_prot (struct kvm *kvm,gfn_t gfn,bool *writable) {
Signature · 1 line
2.25.1
RFC PATCH v8 8/56 KVM: SEV: Rename sev_{pin,unpin}_memory
In reply to Michael RothFrom: Nikunj A Dadhania <[email protected]>
Rename sev_{pin|unpin}_memory to sev_memory_{get|put}_pages. Apart from pinning the pages, sev_pin_memory also populates the pages array which is used by its callers. SEV guest using restricted memfd do not to pin the memory but will require the pages array to be populated. Rename the function appropriately.
No functional change intended.
--- arch/x86/kvm/svm/sev.c | 62 ++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 29 deletions(-)
arch/x86/kvm/svm/sev.c +33 -29
@@ -383,9 +383,13 @@ static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; } -static struct page **sev_pin_memory (struct kvm *kvm,unsigned long uaddr, -unsigned long ulen,unsigned long *n, -int write) +/* +* Legacy SEV guest pin the pages and return the array populated with pinned +* pages. +*/ +static struct page **sev_memory_get_pages (struct kvm *kvm,unsigned long uaddr, +unsigned long ulen,unsigned long *n, +int write) {struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;unsigned long npages, size;
@@ -446,8 +450,8 @@ static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,return ERR_PTR (ret); } -static void sev_unpin_memory (struct kvm *kvm,struct page **pages, -unsigned long npages) +static void sev_memory_put_pages (struct kvm *kvm,struct page **pages, +unsigned long npages) {struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;
@@ -517,7 +521,7 @@ static int sev_launch_update_shared_gfn_handler(struct kvm *kvm, vaddr_end = vaddr + size;/* Lock the user memory. */ - inpages =sev_pin_memory (kvm, vaddr, size, &npages,1 ); + inpages =sev_memory_get_pages (kvm, vaddr, size, &npages,1 );if (IS_ERR (inpages))return PTR_ERR (inpages);
@@ -548,20 +552,20 @@ static int sev_launch_update_shared_gfn_handler(struct kvm *kvm, data.address =__sme_page_pa (inpages[i]) + offset; ret =sev_issue_cmd (kvm,SEV_CMD_LAUNCH_UPDATE_DATA , &data, &argp->error);if (ret) - goto e_unpin; + goto e_put_pages; size -= len; next_vaddr = vaddr + len; } -e_unpin: +e_put_pages:/* content of memory is updated, mark pages dirty */ for (i =0 ; i < npages; i++) {set_page_dirty_lock (inpages[i]);mark_page_accessed (inpages[i]); }/* unlock the user pages */ -sev_unpin_memory (kvm, inpages, npages); +sev_memory_put_pages (kvm, inpages, npages);return ret; }
@@ -1028,13 +1032,13 @@ static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)int len, s_off, d_off;/* lock userspace source and destination page */ - src_p =sev_pin_memory (kvm, vaddr &PAGE_MASK ,PAGE_SIZE , &n,0 ); + src_p =sev_memory_get_pages (kvm, vaddr &PAGE_MASK ,PAGE_SIZE , &n,0 );if (IS_ERR (src_p))return PTR_ERR (src_p); - dst_p =sev_pin_memory (kvm, dst_vaddr &PAGE_MASK ,PAGE_SIZE , &n,1 ); + dst_p =sev_memory_get_pages (kvm, dst_vaddr &PAGE_MASK ,PAGE_SIZE , &n,1 );if (IS_ERR (dst_p)) { -sev_unpin_memory (kvm, src_p, n); +sev_memory_put_pages (kvm, src_p, n);return PTR_ERR (dst_p); }
@@ -1068,8 +1072,8 @@ static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec) (void __user *)dst_vaddr, len, &argp->error); -sev_unpin_memory (kvm, src_p, n); -sev_unpin_memory (kvm, dst_p, n); +sev_memory_put_pages (kvm, src_p, n); +sev_memory_put_pages (kvm, dst_p, n);if (ret) goto err;
@@ -1098,7 +1102,7 @@ static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params)))return -EFAULT ; - pages =sev_pin_memory (kvm, params.guest_uaddr, params.guest_len, &n,1 ); + pages =sev_memory_get_pages (kvm, params.guest_uaddr, params.guest_len, &n,1 );if (IS_ERR (pages))return PTR_ERR (pages);
@@ -1114,7 +1118,7 @@ static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)*/ if (get_num_contig_pages (0 , pages, n) != n) { ret = -EINVAL ; - goto e_unpin_memory; + goto e_put_pages; }memset (&data,0 ,sizeof (data));
@@ -1126,7 +1130,7 @@ static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) blob =psp_copy_user_blob (params.trans_uaddr, params.trans_len);if (IS_ERR (blob)) { ret =PTR_ERR (blob); - goto e_unpin_memory; + goto e_put_pages; } data.trans_address =__psp_pa (blob);
@@ -1147,13 +1151,13 @@ static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) e_free_blob:kfree (blob); -e_unpin_memory: +e_put_pages:/* content of memory is updated, mark pages dirty */ for (i =0 ; i < n; i++) {set_page_dirty_lock (pages[i]);mark_page_accessed (pages[i]); } -sev_unpin_memory (kvm, pages, n); +sev_memory_put_pages (kvm, pages, n);return ret; }
@@ -1383,8 +1387,8 @@ static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)return -EINVAL ;/* Pin guest memory */ - guest_page =sev_pin_memory (kvm, params.guest_uaddr &PAGE_MASK , -PAGE_SIZE , &n,0 ); + guest_page =sev_memory_get_pages (kvm, params.guest_uaddr &PAGE_MASK , +PAGE_SIZE , &n,0 );if (IS_ERR (guest_page))return PTR_ERR (guest_page);
@@ -1392,7 +1396,7 @@ static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) ret = -ENOMEM ; hdr =kzalloc (params.hdr_len,GFP_KERNEL_ACCOUNT );if (!hdr) - goto e_unpin; + goto e_put_pages; trans_data =kzalloc (params.trans_len,GFP_KERNEL_ACCOUNT );if (!trans_data)
@@ -1431,8 +1435,8 @@ static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)kfree (trans_data); e_free_hdr:kfree (hdr); -e_unpin: -sev_unpin_memory (kvm, guest_page, n); +e_put_pages: +sev_memory_put_pages (kvm, guest_page, n);return ret; }
@@ -1579,8 +1583,8 @@ static int sev_receive_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) data.trans_len = params.trans_len;/* Pin guest memory */ - guest_page =sev_pin_memory (kvm, params.guest_uaddr &PAGE_MASK , -PAGE_SIZE , &n,1 ); + guest_page =sev_memory_get_pages (kvm, params.guest_uaddr &PAGE_MASK , +PAGE_SIZE , &n,1 );if (IS_ERR (guest_page)) { ret =PTR_ERR (guest_page); goto e_free_trans;
@@ -1602,7 +1606,7 @@ static int sev_receive_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) ret =sev_issue_cmd (kvm,SEV_CMD_RECEIVE_UPDATE_DATA , &data, &argp->error); -sev_unpin_memory (kvm, guest_page, n); +sev_memory_put_pages (kvm, guest_page, n); e_free_trans:kfree (trans);
@@ -2037,7 +2041,7 @@ int sev_mem_enc_register_region(struct kvm *kvm,return -ENOMEM ;mutex_lock (&kvm->lock); - region->pages =sev_pin_memory (kvm, range->addr, range->size, ®ion->npages,1 ); + region->pages =sev_memory_get_pages (kvm, range->addr, range->size, ®ion->npages,1 );if (IS_ERR (region->pages)) { ret =PTR_ERR (region->pages);mutex_unlock (&kvm->lock);
@@ -2084,7 +2088,7 @@ find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)static void __unregister_enc_region_locked (struct kvm *kvm,struct enc_region *region) { -sev_unpin_memory (kvm, region->pages, region->npages); +sev_memory_put_pages (kvm, region->pages, region->npages);list_del (®ion->list);kfree (region); }
Signature · 1 line
2.25.1
Re: [PATCH RFC v8 18/56] x86/fault: fix handle_split_page_fault() to work with memfd backed pages
In reply to Michael RothQuoted 1 line · Michael Roth
From: Hugh Dickins <[email protected]>
No.
Quoted 7 lines
When the address is backed by a memfd, the code to split the page does nothing more than remove the PMD from the page tables. So immediately install a PTE to ensure that any other pages in that 2MB region are brought back as in 4K pages.
Signed-off-by Hugh Dickins <[email protected]>
No. Suggested-by would be okay.
Quoted 1 line
Cc Hugh Dickins <[email protected]>
Thanks. I'm really sorry to be such a jobsworth,
and have nothing more constructive to say than I did before in
https://lore.kernel.org/linux-mm/[email protected]/
(please re-read), but adding a Signed-off-by where none was given is wrong; and I'm not ever going to comprehend enough of the context to give it.
Best of luck for the series,
Hugh
Quoted 24 lines
Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- mm/memory.c | 5 +++++ 1 file changed, 5 insertions(+)mm/memory.c +5
-0@@ -4999,6 +4999,11 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf)static int handle_split_page_fault (struct vm_fault *vmf) {__split_huge_pmd (vmf->vma, vmf->pmd, vmf->address, false,NULL ); +/* +* Install a PTE immediately to ensure that any other pages in +* this 2MB region are brought back in as 4K pages. +*/ +__pte_alloc (vmf->vma->vm_mm, vmf->pmd);return 0 ; }Signature · 1 line
2.25.1
Re: [PATCH RFC v8 11/56] x86/sev: Add the host SEV-SNP initialization support
In reply to Michael RothQuoted 20 lines · On Mon, 20 Feb 2023 12:38:02 -0600 Michael Roth <[email protected]> wrote:
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. The goal of RMP is to track the owner of each page of memory. Pages of memory can be owned by the hypervisor, owned by a specific VM or owned by the AMD-SP. See APM2 section 15.36.3 for more detail on RMP.
The RMP table is used to enforce access control to memory. The table itself is not directly writable by the software. New CPU instructions (RMPUPDATE, PVALIDATE, RMPADJUST) are used to manipulate the RMP entries.
Based on the platform configuration, the BIOS reserves the memory used for the RMP table. The start and end address of the RMP table must be queried by reading the
RMP_BASEandRMP_ENDMSRs. If theRMP_BASEandRMP_ENDare not set then disable the SEV-SNP feature.
Quoted 6 lines
The SEV-SNP feature is enabled only after the RMP table is successfully initialized.
Also set SYSCFG.MFMD when enabling SNP as SEV-SNP FW >= 1.51 requires that SYSCFG.MFMD must be se
^ unfinished sentence.
Quoted 55 lines
RMP table entry format is non-architectural and it can vary by processor and is defined by the PPR. Restrict SNP support on the known CPU model and family for which the RMP table entry format is currently defined for.
Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/msr-index.h | 11 +- arch/x86/kernel/sev.c | 175 +++++++++++++++++++++++ 3 files changed, 192 insertions(+), 2 deletions(-)arch/x86/include/asm/disabled-features.h +7
-1@@ -87,6 +87,12 @@# define DISABLE_TDX_GUEST (1 << (X86_FEATURE_TDX_GUEST & 31))#endif +#ifdef CONFIG_AMD_MEM_ENCRYPT +# 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 */ @@ -110,7 +116,7 @@ DISABLE_ENQCMD)#define DISABLED_MASK17 0#define DISABLED_MASK18 0 -#define DISABLED_MASK19 0 +#define DISABLED_MASK19 (DISABLE_SEV_SNP)#define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 20)#endif /* _ASM_X86_DISABLED_FEATURES_H */ arch/x86/include/asm/msr-index.h +2
-0@@ -565,6 +565,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#define MSR_AMD64_VIRT_SPEC_CTRL 0xc001011f@@ -649,7 +651,14 @@#define MSR_K8_TOP_MEM2 0xc001001d
arch/x86/include/asm/msr-index.h line 660
Quoted 10 lines
arch/x86/include/asm/msr-index.h +7
-1#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)
^ an extra tab?
Quoted 215 lines
arch/x86/include/asm/msr-index.h +1
-0+#define MSR_K8_INT_PENDING_MSG 0xc0010055/* C1E active bits in int pending message */ #define K8_INTP_C1E_ACTIVE_MASK 0x18000000arch/x86/kernel/sev.c +175
-0@@ -22,6 +22,9 @@#include <linux/efi.h> #include <linux/platform_device.h> #include <linux/io.h> +#include <linux/cpumask.h> +#include <linux/iommu.h> +#include <linux/amd-iommu.h> #include <asm/cpu_entry_area.h> #include <asm/stacktrace.h> @@ -38,6 +41,7 @@#include <asm/apic.h> #include <asm/cpuid.h> #include <asm/cmdline.h> +#include <asm/iommu.h> #define DR7_RESET_VALUE 0x400@@ -57,6 +61,12 @@#define AP_INIT_CR0_DEFAULT 0x60000010#define AP_INIT_MXCSR_DEFAULT 0x1f80 +/* +* 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 +/* For early boot hypervisor communication in SEV-ES enabled guests */ static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE);@@ -69,6 +79,9 @@ static struct ghcb *boot_ghcb __section(".data");/* Bitmap of SEV features supported by the hypervisor */ static u64 sev_hv_features__ro_after_init ; +static unsigned long rmptable_start__ro_after_init ; +static unsigned long rmptable_end__ro_after_init ; +/* #VC handler runtime per-CPU data */ struct sev_es_runtime_data {struct ghcb ghcb_page;@@ -2260,3 +2273,165 @@ static int __init snp_init_platform_device(void)return 0 ; }device_initcall (snp_init_platform_device); + +#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 ()); +} + +static bool get_rmptable_info (u64 *start,u64 *len) +{ +u64 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. The reserved memory should also cover the +* RMP table itself. +*/ + calc_rmp_sz = (((rmp_sz >>PAGE_SHIFT ) +totalram_pages ()) <<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; + +pr_info ("RMP table physical address [0x%016llx - 0x%016llx]\n" , rmp_base, rmp_end); + +return true; +} + +static __init int snp_rmptable_init (void ) +{ +u64 rmp_base, sz; +void *start; +u64 val; + +if (!get_rmptable_info (&rmp_base, &sz)) +return 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; + +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_host_init (void ) +{ +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return 0 ; + +/* +* 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 (boot_cpu_data.x86 !=0x19 || boot_cpu_data.x86_model >0xaf ) + 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 -ENODEV ; +} + +/* +* 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_host_init);
Re: [PATCH RFC v8 18/56] x86/fault: fix handle_split_page_fault() to work with memfd backed pages
In reply to Hugh DickinsQuoted 16 lines · Hugh Dickins
Quoted 1 line · Michael Roth
From: Hugh Dickins <[email protected]>
No.
Quoted 7 lines
When the address is backed by a memfd, the code to split the page does nothing more than remove the PMD from the page tables. So immediately install a PTE to ensure that any other pages in that 2MB region are brought back as in 4K pages.
Signed-off-by Hugh Dickins <[email protected]>No. Suggested-by would be okay.
Quoted 7 lines
Quoted 1 line
Cc Hugh Dickins <[email protected]>Thanks. I'm really sorry to be such a jobsworth,
and have nothing more constructive to say than I did before in
https://lore.kernel.org/linux-mm/[email protected]/
(please re-read), but adding a Signed-off-by where none was given is wrong; and I'm not ever going to comprehend enough of the context to give it.
Hi Hugh,
Sorry for the mix-up, I'd intended to remove this patch before submitting. I'll make sure to remove it from future postings.
Quoted 2 lines
Best of luck for the series,
Thank you!
-Mike
Quoted 26 lines
Hugh
Quoted 24 lines
Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- mm/memory.c | 5 +++++ 1 file changed, 5 insertions(+)mm/memory.c +5
-0@@ -4999,6 +4999,11 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf)static int handle_split_page_fault (struct vm_fault *vmf) {__split_huge_pmd (vmf->vma, vmf->pmd, vmf->address, false,NULL ); +/* +* Install a PTE immediately to ensure that any other pages in +* this 2MB region are brought back in as 4K pages. +*/ +__pte_alloc (vmf->vma->vm_mm, vmf->pmd);return 0 ; }Signature · 1 line
2.25.1
Re: [PATCH RFC v8 19/56] x86/fault: Return pfn from dump_pagetable() for SEV-specific fault handling.
In reply to Michael RothQuoted 5 lines · On Mon, 20 Feb 2023 12:38:10 -0600 Michael Roth <[email protected]> wrote:
From: Ashish Kalra <[email protected]>
Return pfn from
dump_pagetable()to do SEV-specific fault handling. Used for handling SNP RMP page fault.
It would be better to merge this patch with PATCH 13.
Quoted 84 lines
Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- arch/x86/mm/fault.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)arch/x86/mm/fault.c +11
-4@@ -311,7 +311,7 @@ static bool low_pfn(unsigned long pfn)return pfn < max_low_pfn; } -static void dump_pagetable (unsigned long address) +static unsigned long dump_pagetable (unsigned long address) {pgd_t *base =__va (read_cr3_pa ());pgd_t *pgd = &base[pgd_index (address)];@@ -345,8 +345,10 @@ static void dump_pagetable(unsigned long address) pte =pte_offset_kernel (pmd, address);pr_cont ("*pte = %0*Lx " ,sizeof (*pte) *2 , (u64 )pte_val (*pte)); +return 0 ; out:pr_cont ("\n" ); + return 0; }#else /* CONFIG_X86_64: */ @@ -367,10 +369,11 @@ static int bad_address(void *p)return get_kernel_nofault (dummy, (unsigned long *)p); } -static void dump_pagetable (unsigned long address) +static unsigned long dump_pagetable (unsigned long address) {pgd_t *base =__va (read_cr3_pa ());pgd_t *pgd = base +pgd_index (address); +unsigned long pfn;p4d_t *p4d;pud_t *pud;pmd_t *pmd;@@ -388,6 +391,7 @@ static void dump_pagetable(unsigned long address)if (bad_address (p4d)) goto bad; + pfn =p4d_pfn (*p4d);pr_cont ("P4D %lx " ,p4d_val (*p4d));if (!p4d_present (*p4d) ||p4d_large (*p4d)) goto out;@@ -396,6 +400,7 @@ static void dump_pagetable(unsigned long address)if (bad_address (pud)) goto bad; + pfn =pud_pfn (*pud);pr_cont ("PUD %lx " ,pud_val (*pud));if (!pud_present (*pud) ||pud_large (*pud)) goto out;@@ -404,6 +409,7 @@ static void dump_pagetable(unsigned long address)if (bad_address (pmd)) goto bad; + pfn =pmd_pfn (*pmd);pr_cont ("PMD %lx " ,pmd_val (*pmd));if (!pmd_present (*pmd) ||pmd_large (*pmd)) goto out;@@ -412,13 +418,14 @@ static void dump_pagetable(unsigned long address)if (bad_address (pte)) goto bad; + pfn =pte_pfn (*pte);pr_cont ("PTE %lx" ,pte_val (*pte)); out:pr_cont ("\n" ); - -return ; +return pfn; bad:pr_info ("BAD\n" ); + return -1; }#endif /* CONFIG_X86_64 */
Re: [PATCH RFC v8 04/56] KVM: Add HVA range operator
In reply to Michael RothQuoted 35 lines · On Mon, 20 Feb 2023 12:37:55 -0600 Michael Roth <[email protected]> wrote:
From: Vishal Annapurve <[email protected]>
Introduce HVA range operator so that other KVM subsystems can operate on HVA range.
Signed-off-by Vishal Annapurve <[email protected]>[mdr: minor checkpatch alignment fixups]Signed-off-by Michael Roth <[email protected]>--- include/linux/kvm_host.h | 6 +++++ virt/kvm/kvm_main.c | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+)include/linux/kvm_host.h +6
-0@@ -1402,6 +1402,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 +0
-0
virt/kvm/kvm_main.c static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, · line 644
Quoted 3 lines
virt/kvm/kvm_main.c +0
-0@@ -642,6 +642,54 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm,return (int )ret; }
Below function seems a reduced duplicate of __kvm_handle_hva_range() in virt/kvm/kvm_main.c. It would be nice to factor __kvm_handle_hva_range().
Quoted 51 lines
virt/kvm/kvm_main.c +48
-0+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) +{ +int ret =0 ; +struct kvm_gfn_range gfn_range; +struct kvm_memory_slot *slot; +struct kvm_memslots *slots; +int i, idx; + +if (WARN_ON_ONCE (hva_end <= hva_start)) +return -EINVAL ; + + idx =srcu_read_lock (&kvm->srcu); + +for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) { +struct interval_tree_node *node; + + slots =__kvm_memslots (kvm, i); +kvm_for_each_memslot_in_hva_range (node, slots, + hva_start, hva_end -1 ) { +unsigned long start, end; + + slot = container_of(node, struct kvm_memory_slot, + hva_node[slots->node_idx]); + start =max (hva_start, slot->userspace_addr); + end =min (hva_end, slot->userspace_addr + + (slot->npages <<PAGE_SHIFT )); + +/* +* {gfn(page) | page intersects with [hva_start, hva_end)} = +* {gfn_start, gfn_start+1, ..., gfn_end-1}. +*/ + gfn_range.start =hva_to_gfn_memslot (start, slot); + gfn_range.end =hva_to_gfn_memslot (end +PAGE_SIZE -1 , slot); + gfn_range.slot = slot; + + ret =handler (kvm, &gfn_range, data); +if (ret) + goto e_ret; + } + } + +e_ret: +srcu_read_unlock (&kvm->srcu, idx); + +return ret; +} + static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn, unsigned long start, unsigned long end,
Re: [PATCH RFC v8 24/56] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
In reply to Michael RothQuoted 30 lines · On Mon, 20 Feb 2023 12:38:15 -0600 Michael Roth <[email protected]> wrote:
From: 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.
Co-developed-by Ashish Kalra <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Michael Roth <[email protected]>--- drivers/crypto/ccp/sev-dev.c | 148 +++++++++++++++++++++++++++++++++-- include/linux/psp-sev.h | 9 +++ 2 files changed, 149 insertions(+), 8 deletions(-)drivers/crypto/ccp/sev-dev.c +0
-0
drivers/crypto/ccp/sev-dev.c static void *sev_init_ex_buffer; · line 98
Quoted 5 lines
drivers/crypto/ccp/sev-dev.c +2
-0@@ -94,6 +94,13 @@ static void *sev_init_ex_buffer;*/ struct sev_data_range_list *snp_range_list; +/* When SEV-SNP is enabled the TMR needs to be 2MB aligned and 2MB size. */ +#define SEV_SNP_ES_TMR_SIZE (2 * 1024 * 1024)
It would be better to re-use the kernel size definition macros. E.g. SZ_2MB.
Quoted 9 lines
drivers/crypto/ccp/sev-dev.c +5
-0+ +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;
drivers/crypto/ccp/sev-dev.c void snp_mark_pages_offline(unsigned long pfn, unsigned int npages) · line 228
Quoted 6 lines
drivers/crypto/ccp/sev-dev.c +3
-0@@ -216,11 +223,134 @@ void snp_mark_pages_offline(unsigned long pfn, unsigned int npages) }EXPORT_SYMBOL_GPL (snp_mark_pages_offline); +static int snp_reclaim_pages (unsigned long paddr,unsigned int npages,bool locked) +{ +/* Cbit maybe set in the paddr */
This is confusing.
I suppose C-bit is treated as a attribute of PTE in the kernel not part of the PA. It means only a PTE might carry a C-bit.
The paddr is from __pa(page_address()). It is not extracted from a PTE. Thus, the return from them should never have a C-bit.
BTW: Wouldn't it be better to have pfn as input param instead of paddr?
The caller has struct page, calling snp_reclaim_pages(page_to_pfn(page), xxxxx) would be much clearer than the current conversion:page_address() (struct page is converted to VA), __pa() (VA is converted to PA) in the caller and then PA is converted to pfn here.
drivers/crypto/ccp/sev-dev.c void snp_mark_pages_offline(unsigned long pfn, unsigned int npages) · line 231
Quoted 3 lines
drivers/crypto/ccp/sev-dev.c +3
-0+unsigned long pfn =__sme_clr (paddr) >>PAGE_SHIFT ; +int ret, err, i, n =0 ; +
should be unsigned int i, n; as the input param npage is unsigned int.
Quoted 23 lines
drivers/crypto/ccp/sev-dev.c +23
-0+if (!pfn_valid (pfn)) { +pr_err ("%s: Invalid PFN %lx\n" , __func__, pfn); +return 0 ; + } + +for (i =0 ; i < npages; i++, pfn++, n++) { + paddr = pfn <<PAGE_SHIFT ; + +if (locked) + ret =__sev_do_cmd_locked (SEV_CMD_SNP_PAGE_RECLAIM , &paddr, &err); +else + ret =sev_do_cmd (SEV_CMD_SNP_PAGE_RECLAIM , &paddr, &err); + +if (ret) + goto cleanup; + + ret =rmp_make_shared (pfn,PG_LEVEL_4K ); +if (ret) + goto cleanup; + } + +return 0 ; +
drivers/crypto/ccp/sev-dev.c void snp_mark_pages_offline(unsigned long pfn, unsigned int npages) · line 264
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+cleanup: +/* +* If failed to reclaim the page then page is no longer safe to +* be release back to the system, leak it. +*/ +snp_mark_pages_offline (pfn, npages - n); +return ret; +} + +static int rmp_mark_pages_firmware (unsigned long paddr,unsigned int npages,bool locked)
The same comment as above. Better take pfn or page instead of paddr with redundant conversions.
Quoted 178 lines
drivers/crypto/ccp/sev-dev.c +94
-8+{ +/* 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 ;@@ -468,7 +598,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);@@ -491,7 +621,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);@@ -982,6 +1112,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; }@@ -1499,8 +1631,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 ; }@@ -1511,8 +1644,7 @@ static void sev_firmware_shutdown(struct sev_device *sev) }if (snp_range_list) { -free_pages ((unsigned long )snp_range_list, -get_order (PAGE_SIZE )); +snp_free_firmware_page (snp_range_list); snp_range_list =NULL ; }@@ -1593,7 +1725,7 @@ void sev_pci_init(void) }/* Obtain the TMR memory area for SEV-ES use */ - sev_es_tmr =sev_fw_alloc (SEV_ES_TMR_SIZE ); + sev_es_tmr =sev_fw_alloc (sev_es_tmr_size);if (!sev_es_tmr)dev_warn (sev->dev,"SEV: TMR allocation failed, SEV-ES support unavailable\n" );include/linux/psp-sev.h +9
-0@@ -922,6 +922,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);/** * sev_mark_pages_offline - insert non-reclaimed firmware/guest pages @@ -959,6 +961,13 @@ static inline void *psp_copy_user_blob(u64 __user uaddr, u32 len) { return ERR_Pvoid snp_mark_pages_offline (unsigned long pfn,unsigned int npages) {} +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__ */
Re: [PATCH RFC v8 54/56] x86/sev: Add KVM commands for instance certs
In reply to Michael RothHi Mike,
Quoted 115 lines · Michael Roth
From: Dionna Glaze <[email protected]>
The
/dev/sevdevice 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_SIZEanother 4K page to allow space for an extra certificate.Cc Tom Lendacky <[email protected]>Cc Paolo Bonzini <[email protected]>Signed-off-by Dionna Glaze <[email protected]>Signed-off-by Ashish Kalra <[email protected]>[mdr: remove used of "we" and "this patch" in commit log]Signed-off-by Michael Roth <[email protected]>--- arch/x86/kvm/svm/sev.c | 111 ++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/svm/svm.h | 1 + include/linux/psp-sev.h | 2 +- include/uapi/linux/kvm.h | 12 +++++ 4 files changed, 123 insertions(+), 3 deletions(-)arch/x86/kvm/svm/sev.c +60
-0@@ -2089,6 +2089,7 @@ static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp) goto e_free; sev->snp_certs_data = certs_data; + sev->snp_certs_len =0 ;return context;@@ -2404,6 +2405,86 @@ static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; } +static int snp_get_instance_certs (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct kvm_sev_snp_get_certs params; + +if (!sev_snp_guest (kvm)) +return -ENOTTY ; + +if (!sev->snp_context) +return -EINVAL ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data, +sizeof (params))) +return -EFAULT ; + +/* No instance certs set. */ +if (!sev->snp_certs_len) +return -ENOENT ; + +if (params.certs_len < sev->snp_certs_len) { +/* Output buffer too small. Return the required size. */ + params.certs_len = sev->snp_certs_len; + +if (copy_to_user ((void __user *)(uintptr_t )argp->data, ¶ms, +sizeof (params))) +return -EFAULT ; + +return -EINVAL ; + } + +if (copy_to_user ((void __user *)(uintptr_t )params.certs_uaddr, + sev->snp_certs_data, sev->snp_certs_len)) +return -EFAULT ; + +return 0 ; +} + +static int snp_set_instance_certs (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +unsigned long length =SEV_FW_BLOB_MAX_SIZE ; +void *to_certs = sev->snp_certs_data; +struct kvm_sev_snp_set_certs params; + +if (!sev_snp_guest (kvm)) +return -ENOTTY ; + +if (!sev->snp_context) +return -EINVAL ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data, +sizeof (params))) +return -EFAULT ; + +if (params.certs_len >SEV_FW_BLOB_MAX_SIZE ) +return -EINVAL ; + +/* +* Setting a length of 0 is the same as "uninstalling" instance-
arch/x86/kvm/svm/sev.c static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2476
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+* specific certificates. +*/ +if (params.certs_len ==0 ) { + sev->snp_certs_len =0 ; +return 0 ; + } + +/* Page-align the length */ + length = (params.certs_len +PAGE_SIZE -1 ) &PAGE_MASK ; +
In comments on v7 [1] Dionna agreed adding cleanup here:
/* The size could shrink and leave garbage at the end. */ memset(sev->snp_certs_data, 0, SEV_FW_BLOB_MAX_SIZE);
(we can use 'to_certs' in the first argument)
but it wasn't added in v8.
-Dov
Quoted 113 lines
arch/x86/kvm/svm/sev.c +39
-2+if (copy_from_user (to_certs, + (void __user *)(uintptr_t )params.certs_uaddr, + params.certs_len)) { +return -EFAULT ; + } + + sev->snp_certs_len = length; + +return 0 ; +} +int sev_mem_enc_ioctl (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;@@ -2503,6 +2584,12 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)case KVM_SEV_SNP_LAUNCH_FINISH : r =snp_launch_finish (kvm, &sev_cmd);break ; +case KVM_SEV_SNP_GET_CERTS : + r =snp_get_instance_certs (kvm, &sev_cmd); +break ; +case KVM_SEV_SNP_SET_CERTS : + r =snp_set_instance_certs (kvm, &sev_cmd); +break ;default : r = -EINVAL ; goto out;@@ -3550,8 +3637,28 @@ static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gpif (rc) goto unlock; - rc =snp_guest_ext_guest_request (&req, (unsigned long )sev->snp_certs_data, - &data_npages, &err); +/* +* If the VMM has overridden the certs, then change the error message +* if the size is inappropriate for the override. Otherwise, use a +* regular guest request and copy back the instance certs. +*/ +if (sev->snp_certs_len) { +if ((data_npages <<PAGE_SHIFT ) < sev->snp_certs_len) { + rc = -EINVAL ; + err =SNP_GUEST_REQ_INVALID_LEN ; + goto datalen; + } + rc =sev_issue_cmd (kvm,SEV_CMD_SNP_GUEST_REQUEST , &req, + (int *)&err); + }else { + rc =snp_guest_ext_guest_request (&req, + (unsigned long )sev->snp_certs_data, + &data_npages, &err); + } +datalen: +if (sev->snp_certs_len) + data_npages = sev->snp_certs_len >>PAGE_SHIFT ; +if (rc) {/* * If buffer length is small then return the expected arch/x86/kvm/svm/svm.h +1
-0@@ -94,6 +94,7 @@ struct kvm_sev_info {u64 snp_init_flags;void *snp_context;/* SNP guest context page */ void *snp_certs_data; +unsigned int snp_certs_len;/* Size of instance override for certs */ struct mutex guest_req_lock;/* Lock for guest request handling */ u64 sev_features;/* Features set at VMSA creation */ 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 */ /** * SEV platform state include/uapi/linux/kvm.h +12
-0@@ -1928,6 +1928,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, };@@ -2075,6 +2077,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)
Kalra, Ashish
maintainerRe: [PATCH RFC v8 24/56] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
In reply to Zhi WangQuoted 9 lines
drivers/crypto/ccp/sev-dev.c void snp_mark_pages_offline(unsigned long pfn, unsigned int npages) · line 228
Quoted 3 lines
drivers/crypto/ccp/sev-dev.c +3
-0+static int snp_reclaim_pages (unsigned long paddr,unsigned int npages,bool locked) +{ +/* Cbit maybe set in the paddr */ This is confusing.
I suppose C-bit is treated as a attribute of PTE in the kernel not part of the PA. It means only a PTE might carry a C-bit.
snp_reclaim_pages() is also called for reclaiming guest memory, in which case the (guest) paddr will have the C-bit set. Hence this C-bit handling is done within snp_reclaim_pages() so that the callers don't need to handle it explicitly.
Quoted 44 lines
The paddr is from __pa(page_address()). It is not extracted from a PTE. Thus, the return from them should never have a C-bit.
BTW: Wouldn't it be better to have pfn as input param instead of paddr?
The caller has struct page, calling
snp_reclaim_pages(page_to_pfn(page), xxxxx)would be much clearer than the current conversion:page_address()(struct page is converted to VA),__pa()(VA is converted to PA) in the caller and then PA is converted to pfn here.drivers/crypto/ccp/sev-dev.c void snp_mark_pages_offline(unsigned long pfn, unsigned int npages) · line 231
Quoted 3 lines
drivers/crypto/ccp/sev-dev.c +3
-0+unsigned long pfn =__sme_clr (paddr) >>PAGE_SHIFT ; +int ret, err, i, n =0 ; +should be unsigned int i, n; as the input param npage is unsigned int.
Quoted 28 lines
drivers/crypto/ccp/sev-dev.c +28
-0+if (!pfn_valid (pfn)) { +pr_err ("%s: Invalid PFN %lx\n" , __func__, pfn); +return 0 ; + } + +for (i =0 ; i < npages; i++, pfn++, n++) { + paddr = pfn <<PAGE_SHIFT ; + +if (locked) + ret =__sev_do_cmd_locked (SEV_CMD_SNP_PAGE_RECLAIM , &paddr, &err); +else + ret =sev_do_cmd (SEV_CMD_SNP_PAGE_RECLAIM , &paddr, &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. +*/
Quoted 9 lines
drivers/crypto/ccp/sev-dev.c void snp_mark_pages_offline(unsigned long pfn, unsigned int npages) · line 264
Quoted 5 lines
drivers/crypto/ccp/sev-dev.c +5
-0+snp_mark_pages_offline (pfn, npages - n); +return ret; +} + +static int rmp_mark_pages_firmware (unsigned long paddr,unsigned int npages,bool locked)The same comment as above. Better take pfn or page instead of paddr with redundant conversions.
Again, the paddr can point to guest memory so it can have C-bit set.
Thanks,
Ashish
Quoted 24 lines
Quoted 24 lines
drivers/crypto/ccp/sev-dev.c +24
-0+{ +/* 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; +} +
Re: [PATCH RFC v8 24/56] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
Quoted 6 lines · Ashish
Quoted 6 lines
drivers/crypto/ccp/sev-dev.c void snp_mark_pages_offline(unsigned long pfn, unsigned int npages) · line 228
Quoted 3 lines
drivers/crypto/ccp/sev-dev.c +3
-0+static int snp_reclaim_pages (unsigned long paddr,unsigned int npages,bool locked) +{ +/* Cbit maybe set in the paddr */ This is confusing.
Quoted 8 lines
Quoted 3 lines
I suppose C-bit is treated as a attribute of PTE in the kernel not part of the PA. It means only a PTE might carry a C-bit.
snp_reclaim_pages()is also called for reclaiming guest memory, in which case the (guest) paddr will have the C-bit set. Hence this C-bit handling is done withinsnp_reclaim_pages()so that the callers don't need to handle it explicitly.
Thanks for the explanation.
Do you mean it will be used like that in the later patch? Sorry if it is in the later patch as I was making progress slowly. It is quite a big patch set.
At least, I don't see that kind of usage in the current patch. Feel free to correct me if I am wrong.
The call chains:
__snp_free_firmware_page()
snp_reclaim_pages();
As __snp_free_firmware_page() takes struct page*, all the follwing coversion from it would not carry C-bit.
__snp_alloc_firmware_pages()
rmp_mark_pages_firmware()
snp_reclaim_pages()
As __snp_alloc_firmware_page() allocates page with struct page*, the same conclusion as above.
Quoted 85 lines
Quoted 46 lines
The paddr is from __pa(page_address()). It is not extracted from a PTE. Thus, the return from them should never have a C-bit.
BTW: Wouldn't it be better to have pfn as input param instead of paddr?
The caller has struct page, calling
snp_reclaim_pages(page_to_pfn(page), xxxxx)would be much clearer than the current conversion:page_address()(struct page is converted to VA),__pa()(VA is converted to PA) in the caller and then PA is converted to pfn here.drivers/crypto/ccp/sev-dev.c void snp_mark_pages_offline(unsigned long pfn, unsigned int npages) · line 231
Quoted 3 lines
drivers/crypto/ccp/sev-dev.c +3
-0+unsigned long pfn =__sme_clr (paddr) >>PAGE_SHIFT ; +int ret, err, i, n =0 ; +should be unsigned int i, n; as the input param npage is unsigned int.
Quoted 28 lines
drivers/crypto/ccp/sev-dev.c +28
-0+if (!pfn_valid (pfn)) { +pr_err ("%s: Invalid PFN %lx\n" , __func__, pfn); +return 0 ; + } + +for (i =0 ; i < npages; i++, pfn++, n++) { + paddr = pfn <<PAGE_SHIFT ; + +if (locked) + ret =__sev_do_cmd_locked (SEV_CMD_SNP_PAGE_RECLAIM , &paddr, &err); +else + ret =sev_do_cmd (SEV_CMD_SNP_PAGE_RECLAIM , &paddr, &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. +*/ Quoted 9 lines
drivers/crypto/ccp/sev-dev.c void snp_mark_pages_offline(unsigned long pfn, unsigned int npages) · line 264
Quoted 5 lines
drivers/crypto/ccp/sev-dev.c +5
-0+snp_mark_pages_offline (pfn, npages - n); +return ret; +} + +static int rmp_mark_pages_firmware (unsigned long paddr,unsigned int npages,bool locked)The same comment as above. Better take pfn or page instead of paddr with redundant conversions.
Again, the paddr can point to guest memory so it can have C-bit set.
Thanks,
AshishQuoted 24 lines
Quoted 24 lines
drivers/crypto/ccp/sev-dev.c +24
-0+{ +/* 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; +} +
Re: [PATCH RFC v8 10/56] x86/cpufeatures: Add SEV-SNP CPU feature
In reply to Michael RothQuoted 10 lines · Michael Roth
From: 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.
Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>Signed-off-by Ashish Kalra <[email protected]>
Too many signed-off-by's. Are you missing Co-developed-by?
Quoted 24 lines
Signed-off-by Michael Roth <[email protected]>--- 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@@ -420,6 +420,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 +0
-0
arch/x86/kernel/cpu/amd.c static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) · line 562
Quoted 5 lines
arch/x86/kernel/cpu/amd.c +0
-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).
Did you remove the related scattered.c code mentioned above in a different patch?
Quoted 25 lines
arch/x86/kernel/cpu/amd.c +3
-0+* 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@@ -417,6 +417,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 · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Kalra, Ashish
maintainerRe: [PATCH RFC v8 24/56] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
In reply to Zhi WangQuoted 18 lines · Zhi Wang
Quoted 6 lines · Ashish
Quoted 6 lines
drivers/crypto/ccp/sev-dev.c void snp_mark_pages_offline(unsigned long pfn, unsigned int npages) · line 228
Quoted 3 lines
drivers/crypto/ccp/sev-dev.c +3
-0+static int snp_reclaim_pages (unsigned long paddr,unsigned int npages,bool locked) +{ +/* Cbit maybe set in the paddr */ This is confusing.
Quoted 8 lines
Quoted 3 lines
I suppose C-bit is treated as a attribute of PTE in the kernel not part of the PA. It means only a PTE might carry a C-bit.
snp_reclaim_pages()is also called for reclaiming guest memory, in which case the (guest) paddr will have the C-bit set. Hence this C-bit handling is done withinsnp_reclaim_pages()so that the callers don't need to handle it explicitly.
Quoted 5 lines
Thanks for the explanation.
Do you mean it will be used like that in the later patch? Sorry if it is in the later patch as I was making progress slowly. It is quite a big patch set.
Yes, these are callers in later patches, like the following code path in patch 25:
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 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;
}
...
...Or, the following as part of patch 52:
int snp_guest_dbg_decrypt_page(u64 gctx_pfn, u64 src_pfn, u64 dst_pfn, int *error)
{
...
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))
...
...Thanks,
Ashish
Quoted 104 lines
At least, I don't see that kind of usage in the current patch. Feel free to correct me if I am wrong.
The call chains:
__snp_free_firmware_page()snp_reclaim_pages();As
__snp_free_firmware_page()takes struct page*, all the follwing coversion from it would not carry C-bit.
__snp_alloc_firmware_pages()
rmp_mark_pages_firmware()
snp_reclaim_pages()As
__snp_alloc_firmware_page()allocates page with struct page*, the same conclusion as above.Quoted 85 lines
Quoted 46 lines
The paddr is from __pa(page_address()). It is not extracted from a PTE. Thus, the return from them should never have a C-bit.
BTW: Wouldn't it be better to have pfn as input param instead of paddr?
The caller has struct page, calling
snp_reclaim_pages(page_to_pfn(page), xxxxx)would be much clearer than the current conversion:page_address()(struct page is converted to VA),__pa()(VA is converted to PA) in the caller and then PA is converted to pfn here.drivers/crypto/ccp/sev-dev.c void snp_mark_pages_offline(unsigned long pfn, unsigned int npages) · line 231
Quoted 3 lines
drivers/crypto/ccp/sev-dev.c +3
-0+unsigned long pfn =__sme_clr (paddr) >>PAGE_SHIFT ; +int ret, err, i, n =0 ; +should be unsigned int i, n; as the input param npage is unsigned int.
Quoted 28 lines
drivers/crypto/ccp/sev-dev.c +28
-0+if (!pfn_valid (pfn)) { +pr_err ("%s: Invalid PFN %lx\n" , __func__, pfn); +return 0 ; + } + +for (i =0 ; i < npages; i++, pfn++, n++) { + paddr = pfn <<PAGE_SHIFT ; + +if (locked) + ret =__sev_do_cmd_locked (SEV_CMD_SNP_PAGE_RECLAIM , &paddr, &err); +else + ret =sev_do_cmd (SEV_CMD_SNP_PAGE_RECLAIM , &paddr, &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. +*/ Quoted 9 lines
drivers/crypto/ccp/sev-dev.c void snp_mark_pages_offline(unsigned long pfn, unsigned int npages) · line 264
Quoted 5 lines
drivers/crypto/ccp/sev-dev.c +5
-0+snp_mark_pages_offline (pfn, npages - n); +return ret; +} + +static int rmp_mark_pages_firmware (unsigned long paddr,unsigned int npages,bool locked)The same comment as above. Better take pfn or page instead of paddr with redundant conversions.
Again, the paddr can point to guest memory so it can have C-bit set.
Thanks,
AshishQuoted 24 lines
Quoted 24 lines
drivers/crypto/ccp/sev-dev.c +24
-0+{ +/* 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; +} +
Re: [PATCH RFC v8 27/56] crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command
In reply to Michael RothQuoted 109 lines · On Mon, 20 Feb 2023 12:38:18 -0600 Michael Roth <[email protected]> wrote:
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.Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- Documentation/virt/coco/sev-guest.rst | 27 ++++++ drivers/crypto/ccp/sev-dev.c | 123 ++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 4 + include/uapi/linux/psp-sev.h | 17 ++++ 4 files changed, 171 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 +25
-0@@ -1481,6 +1481,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 */ +kfree (sev->snp_certs_data); + sev->snp_certs_data =NULL ; +wbinvd_on_all_cpus (); retry:@@ -1793,6 +1797,118 @@ 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; +int ret; + +if (!sev->snp_initialized || !argp->data) +return -EINVAL ; + +memset (&input,0 ,sizeof (input)); + +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 ; + } +
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) · line 1830
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+/* Copy the extended certs programmed through the SNP_SET_CONFIG */ +if (input.certs_address && sev->snp_certs_data) { +if (input.certs_len < sev->snp_certs_len) { +/* Return the certs length to userspace */ + input.certs_len = sev->snp_certs_len; + + ret = -ENOSR ; + goto e_done; + } +
What about if input.certs_len > sev->snp_certs_len? Is it possbile for the userspace to know the length of data in the buffer? (I guess it might be able to know the certs len through the blob data, but a comment here would be nice)
Quoted 155 lines
drivers/crypto/ccp/sev-dev.c +88
-0+if (copy_to_user ((void *__user )input.certs_address, + sev->snp_certs_data, sev->snp_certs_len)) +return -EFAULT ; + } + + ret =0 ; + +e_done: +if (copy_to_user ((void __user *)argp->data, &input,sizeof (input))) + ret = -EFAULT ; + +return ret; +} + +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; +void *certs =NULL ; +int ret =0 ; + +if (!sev->snp_initialized || !argp->data) +return -EINVAL ; + +if (!writable) +return -EPERM ; + +memset (&input,0 ,sizeof (input)); + +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 )) +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)); +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. +*/ +mutex_lock (&sev->snp_certs_lock); +if (certs) { +kfree (sev->snp_certs_data); + sev->snp_certs_data = certs; + sev->snp_certs_len = input.certs_len; + }else { +kfree (sev->snp_certs_data); + sev->snp_certs_data =NULL ; + sev->snp_certs_len =0 ; + } +mutex_unlock (&sev->snp_certs_lock); + +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;@@ -1847,6 +1963,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;@@ -1962,6 +2084,7 @@ int sev_dev_init(struct psp_device *psp) goto e_sev; sev->cmd_buf_backup = (uint8_t *)sev->cmd_buf +PAGE_SIZE ; +mutex_init (&sev->snp_certs_lock); psp->sev_data = sev;drivers/crypto/ccp/sev-dev.h +4
-0@@ -66,6 +66,10 @@ struct sev_device {bool snp_initialized;struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS ]; +void *snp_certs_data; +u32 snp_certs_len; +struct mutex snp_certs_lock; +struct sev_user_data_snp_config snp_config; };int sev_dev_init (struct psp_device *psp);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, };@@ -192,6 +194,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 *
Tom Lendacky
maintainerRe: [PATCH RFC v8 27/56] crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command
Quoted 74 lines · Zhi Wang
Quoted 71 lines · On Mon, 20 Feb 2023 12:38:18 -0600 Michael Roth <[email protected]> wrote:
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.Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- Documentation/virt/coco/sev-guest.rst | 27 ++++++ drivers/crypto/ccp/sev-dev.c | 123 ++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 4 + include/uapi/linux/psp-sev.h | 17 ++++ 4 files changed, 171 insertions(+)drivers/crypto/ccp/sev-dev.c +24
-0@@ -1481,6 +1481,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 */ +kfree (sev->snp_certs_data); + sev->snp_certs_data =NULL ; +wbinvd_on_all_cpus (); retry:@@ -1793,6 +1797,118 @@ 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; +int ret; + +if (!sev->snp_initialized || !argp->data) +return -EINVAL ; + +memset (&input,0 ,sizeof (input)); + +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 ; + }
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) · line 1827
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c +8
-0+ +/* Copy the extended certs programmed through the SNP_SET_CONFIG */ +if (input.certs_address && sev->snp_certs_data) { +if (input.certs_len < sev->snp_certs_len) { +/* Return the certs length to userspace */ + input.certs_len = sev->snp_certs_len; + + ret = -ENOSR ;
We should be consistent with the other SEV ioctls that return required lengths and return -EIO here instead -ENOSR.
Thanks,
Tom
Quoted 21 lines
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) · line 1830
Quoted 3 lines
drivers/crypto/ccp/sev-dev.c +3
-0+ goto e_done; + } +What about if input.certs_len >
sev->snp_certs_len? Is it possbile for the userspace to know the length of data in the buffer? (I guess it might be able to know the certs len through the blob data, but a comment here would be nice)Quoted 13 lines
drivers/crypto/ccp/sev-dev.c +13
-0+if (copy_to_user ((void *__user )input.certs_address, + sev->snp_certs_data, sev->snp_certs_len)) +return -EFAULT ; + } + + ret =0 ; + +e_done: +if (copy_to_user ((void __user *)argp->data, &input,sizeof (input))) + ret = -EFAULT ; + +return ret; +}
Re: [PATCH RFC v8 28/56] crypto: ccp: Provide APIs to query extended attestation report
In reply to Michael RothOn Mon, 20 Feb 2023 12:38:19 -0600
Michael Roth <[email protected]> wrote:
It seems in the discussion:
https://lore.kernel.org/lkml/[email protected]/,
this API is going to be removed. Will that fix land in this patch series or not? If not, It would be better to mention it in the comment message of this one or patch 45.
If yes, I guess this patch is not needed.
Quoted 127 lines
From: Brijesh Singh <[email protected]>
Version 2 of the GHCB specification defines VMGEXIT that is used to get the extended attestation report. The extended attestation report includes the certificate blobs provided through the
SNP_SET_EXT_CONFIG.The
snp_guest_ext_guest_request()will be used by the hypervisor to get the extended attestation report. See the GHCB specification for more details.Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- drivers/crypto/ccp/sev-dev.c | 47 ++++++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 33 +++++++++++++++++++++++++ 2 files changed, 80 insertions(+)drivers/crypto/ccp/sev-dev.c +47
-0@@ -2017,6 +2017,53 @@ int sev_guest_df_flush(int *error) }EXPORT_SYMBOL_GPL (sev_guest_df_flush); +int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data, +unsigned long vaddr,unsigned long *npages,unsigned long *fw_err) +{ +unsigned long expected_npages; +struct sev_device *sev; +int rc; + +if (!psp_master || !psp_master->sev_data) +return -ENODEV ; + + sev = psp_master->sev_data; + +if (!sev->snp_initialized) +return -EINVAL ; + +mutex_lock (&sev->snp_certs_lock); +/* +* Check if there is enough space to copy the certificate chain. Otherwise +* return ERROR code defined in the GHCB specification. +*/ + expected_npages = sev->snp_certs_len >>PAGE_SHIFT ; +if (*npages < expected_npages) { + *npages = expected_npages; + *fw_err =SNP_GUEST_REQ_INVALID_LEN ; +mutex_unlock (&sev->snp_certs_lock); +return -EINVAL ; + } + + rc =sev_do_cmd (SEV_CMD_SNP_GUEST_REQUEST , data, (int *)fw_err); +if (rc) { +mutex_unlock (&sev->snp_certs_lock); +return rc; + } + +/* Copy the certificate blob */ +if (sev->snp_certs_data) { + *npages = expected_npages; +memcpy ((void *)vaddr, sev->snp_certs_data, *npages <<PAGE_SHIFT ); + }else { + *npages =0 ; + } + +mutex_unlock (&sev->snp_certs_lock); +return rc; +} +EXPORT_SYMBOL_GPL (snp_guest_ext_guest_request); +static void sev_exit (struct kref *ref) {misc_deregister (&misc_dev->misc);include/linux/psp-sev.h +33
-0@@ -931,6 +931,32 @@ void snp_free_firmware_page(void *addr);*/ void snp_mark_pages_offline (unsigned long pfn,unsigned int npages); +/** +* snp_guest_ext_guest_request - perform the SNP extended guest request command +* defined in the GHCB specification. +* +* @data: the input guest request structure +* @vaddr: address where the certificate blob need to be copied. +* @npages: number of pages for the certificate blob. +* If the specified page count is less than the certificate blob size, then the +* required page count is returned with error code defined in the GHCB spec. +* If the specified page count is more than the certificate blob size, then +* page count is updated to reflect the amount of valid data copied in the +* vaddr. +* +* @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_ext_guest_request (struct sev_data_snp_guest_request *data, +unsigned long vaddr,unsigned long *npages, +unsigned long *error); +#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ static inline int@@ -968,6 +994,13 @@ static inline void *snp_alloc_firmware_page(gfp_t mask)static inline void snp_free_firmware_page (void *addr) { } +static inline int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data, +unsigned long vaddr,unsigned long *n, +unsigned long *error) +{ +return -ENODEV ; +} +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */
Re: [PATCH RFC v8 31/56] KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
In reply to Michael RothQuoted 8 lines · On Mon, 20 Feb 2023 12:38:22 -0600 Michael Roth <[email protected]> wrote:
From: 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" in the RMP after a successful VMRUN. This
Is this "in-use" bit part of an RMP entry? If yes, better list its name in APM.
Quoted 1 line
is done for _all_ VMs, not just SNP-Active VMs.
_All_ VMs? Do you mean SEV VMs and SEVSNP VMs? I guess legacy VM is not affected, right?
Quoted 5 lines
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
^hugepage
Quoted 94 lines
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. 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.
Co-developed-by Marc Orr <[email protected]>Signed-off-by Marc Orr <[email protected]>Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/lapic.c | 5 ++++- arch/x86/kvm/svm/sev.c | 33 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 15 ++++++++++++-- arch/x86/kvm/svm/svm.h | 1 + 6 files changed, 54 insertions(+), 3 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -131,6 +131,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(alloc_apic_backing_page) KVM_X86_OP_OPTIONAL_RET0(fault_is_private); KVM_X86_OP_OPTIONAL_RET0(update_mem_attr) KVM_X86_OP_OPTIONAL(invalidate_restricted_mem)arch/x86/include/asm/kvm_host.h +2
-0@@ -1729,6 +1729,8 @@ struct kvm_x86_ops {* Returns vCPU specific APICv inhibit reasons */ unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu); + +void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); };struct kvm_x86_nested_ops {arch/x86/kvm/lapic.c +4
-1@@ -2740,7 +2740,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/sev.c +23
-0@@ -3241,3 +3241,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 ; +
arch/x86/kvm/svm/sev.c void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) · line 3276
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+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; +}
The duplicate allocation routine in snp_alloc_vmsa_page() in sev.c can be replaced with snp_safe_alloc_page().
Quoted 59 lines
arch/x86/kvm/svm/svm.c +13
-2@@ -1372,7 +1372,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;@@ -1381,7 +1381,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;@@ -4696,6 +4696,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 ,@@ -4824,6 +4834,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@@ -692,6 +692,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 */
Kalra, Ashish
maintainerRe: [PATCH RFC v8 28/56] crypto: ccp: Provide APIs to query extended attestation report
In reply to Zhi WangQuoted 10 lines · Zhi Wang
On Mon, 20 Feb 2023 12:38:19 -0600
Michael Roth <[email protected]> wrote:It seems in the discussion:
https://lore.kernel.org/lkml/[email protected]/,
this API is going to be removed. Will that fix land in this patch series or not? If not, It would be better to mention it in the comment message of this one or patch 45.
If yes, I guess this patch is not needed.
This API is definitely not going to be removed.
There will be some fixes and optimizations added to the API implementation (as per the discussions) and that will be included in v9.
Thanks,
Ashish
Quoted 128 lines
Quoted 127 lines
From: Brijesh Singh <[email protected]>
Version 2 of the GHCB specification defines VMGEXIT that is used to get the extended attestation report. The extended attestation report includes the certificate blobs provided through the
SNP_SET_EXT_CONFIG.The
snp_guest_ext_guest_request()will be used by the hypervisor to get the extended attestation report. See the GHCB specification for more details.Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- drivers/crypto/ccp/sev-dev.c | 47 ++++++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 33 +++++++++++++++++++++++++ 2 files changed, 80 insertions(+)drivers/crypto/ccp/sev-dev.c +47
-0@@ -2017,6 +2017,53 @@ int sev_guest_df_flush(int *error) }EXPORT_SYMBOL_GPL (sev_guest_df_flush); +int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data, +unsigned long vaddr,unsigned long *npages,unsigned long *fw_err) +{ +unsigned long expected_npages; +struct sev_device *sev; +int rc; + +if (!psp_master || !psp_master->sev_data) +return -ENODEV ; + + sev = psp_master->sev_data; + +if (!sev->snp_initialized) +return -EINVAL ; + +mutex_lock (&sev->snp_certs_lock); +/* +* Check if there is enough space to copy the certificate chain. Otherwise +* return ERROR code defined in the GHCB specification. +*/ + expected_npages = sev->snp_certs_len >>PAGE_SHIFT ; +if (*npages < expected_npages) { + *npages = expected_npages; + *fw_err =SNP_GUEST_REQ_INVALID_LEN ; +mutex_unlock (&sev->snp_certs_lock); +return -EINVAL ; + } + + rc =sev_do_cmd (SEV_CMD_SNP_GUEST_REQUEST , data, (int *)fw_err); +if (rc) { +mutex_unlock (&sev->snp_certs_lock); +return rc; + } + +/* Copy the certificate blob */ +if (sev->snp_certs_data) { + *npages = expected_npages; +memcpy ((void *)vaddr, sev->snp_certs_data, *npages <<PAGE_SHIFT ); + }else { + *npages =0 ; + } + +mutex_unlock (&sev->snp_certs_lock); +return rc; +} +EXPORT_SYMBOL_GPL (snp_guest_ext_guest_request); +static void sev_exit (struct kref *ref) {misc_deregister (&misc_dev->misc);include/linux/psp-sev.h +33
-0@@ -931,6 +931,32 @@ void snp_free_firmware_page(void *addr);*/ void snp_mark_pages_offline (unsigned long pfn,unsigned int npages); +/** +* snp_guest_ext_guest_request - perform the SNP extended guest request command +* defined in the GHCB specification. +* +* @data: the input guest request structure +* @vaddr: address where the certificate blob need to be copied. +* @npages: number of pages for the certificate blob. +* If the specified page count is less than the certificate blob size, then the +* required page count is returned with error code defined in the GHCB spec. +* If the specified page count is more than the certificate blob size, then +* page count is updated to reflect the amount of valid data copied in the +* vaddr. +* +* @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_ext_guest_request (struct sev_data_snp_guest_request *data, +unsigned long vaddr,unsigned long *npages, +unsigned long *error); +#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ static inline int@@ -968,6 +994,13 @@ static inline void *snp_alloc_firmware_page(gfp_t mask)static inline void snp_free_firmware_page (void *addr) { } +static inline int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data, +unsigned long vaddr,unsigned long *n, +unsigned long *error) +{ +return -ENODEV ; +} +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */
Kalra, Ashish
maintainerRe: [PATCH RFC v8 27/56] crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command
In reply to Zhi WangQuoted 118 lines · Zhi Wang
Quoted 115 lines · On Mon, 20 Feb 2023 12:38:18 -0600 Michael Roth <[email protected]> wrote:
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.Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- Documentation/virt/coco/sev-guest.rst | 27 ++++++ drivers/crypto/ccp/sev-dev.c | 123 ++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 4 + include/uapi/linux/psp-sev.h | 17 ++++ 4 files changed, 171 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 +31
-0@@ -1481,6 +1481,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 */ +kfree (sev->snp_certs_data); + sev->snp_certs_data =NULL ; +wbinvd_on_all_cpus (); retry:@@ -1793,6 +1797,118 @@ 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; +int ret; + +if (!sev->snp_initialized || !argp->data) +return -EINVAL ; + +memset (&input,0 ,sizeof (input)); + +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 ; + } + +/* Copy the extended certs programmed through the SNP_SET_CONFIG */ +if (input.certs_address && sev->snp_certs_data) { +if (input.certs_len < sev->snp_certs_len) { +/* Return the certs length to userspace */ + input.certs_len = sev->snp_certs_len; +
Quoted 9 lines
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) · line 1830
Quoted 4 lines
drivers/crypto/ccp/sev-dev.c +4
-0+ ret = -ENOSR ; + goto e_done; + } +What about if input.certs_len >
sev->snp_certs_len? Is it possbile for the userspace to know the length of data in the buffer? (I guess it might be able to know the certs len through the blob data, but a comment here would be nice)
If userspace provides an input buffer/length smaller then snp_certs_len, then the above returns the "required" certs length back to userspace.
And what is the issue if input.certs_len > sev->snp_certs_len, the buffer returned back to userspace is sev->snp_certs_len as below.
Thanks,
Ashish
Quoted 156 lines
Quoted 155 lines
drivers/crypto/ccp/sev-dev.c +88
-0+if (copy_to_user ((void *__user )input.certs_address, + sev->snp_certs_data, sev->snp_certs_len)) +return -EFAULT ; + } + + ret =0 ; + +e_done: +if (copy_to_user ((void __user *)argp->data, &input,sizeof (input))) + ret = -EFAULT ; + +return ret; +} + +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; +void *certs =NULL ; +int ret =0 ; + +if (!sev->snp_initialized || !argp->data) +return -EINVAL ; + +if (!writable) +return -EPERM ; + +memset (&input,0 ,sizeof (input)); + +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 )) +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)); +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. +*/ +mutex_lock (&sev->snp_certs_lock); +if (certs) { +kfree (sev->snp_certs_data); + sev->snp_certs_data = certs; + sev->snp_certs_len = input.certs_len; + }else { +kfree (sev->snp_certs_data); + sev->snp_certs_data =NULL ; + sev->snp_certs_len =0 ; + } +mutex_unlock (&sev->snp_certs_lock); + +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;@@ -1847,6 +1963,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;@@ -1962,6 +2084,7 @@ int sev_dev_init(struct psp_device *psp) goto e_sev; sev->cmd_buf_backup = (uint8_t *)sev->cmd_buf +PAGE_SIZE ; +mutex_init (&sev->snp_certs_lock); psp->sev_data = sev;drivers/crypto/ccp/sev-dev.h +4
-0@@ -66,6 +66,10 @@ struct sev_device {bool snp_initialized;struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS ]; +void *snp_certs_data; +u32 snp_certs_len; +struct mutex snp_certs_lock; +struct sev_user_data_snp_config snp_config; };int sev_dev_init (struct psp_device *psp);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, };@@ -192,6 +194,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 *
Re: [PATCH RFC v8 10/56] x86/cpufeatures: Add SEV-SNP CPU feature
In reply to Sathyanarayanan KuppuswamyQuoted 35 lines · Sathyanarayanan Kuppuswamy
Quoted 10 lines · Michael Roth
From: 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.
Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Too many signed-off-by's. Are you missing Co-developed-by?
Quoted 19 lines
Signed-off-by Michael Roth <[email protected]>--- 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@@ -420,6 +420,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 */
Quoted 13 lines
arch/x86/kernel/cpu/amd.c static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) · line 562
Quoted 10 lines
arch/x86/kernel/cpu/amd.c +0
-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). Did you remove the related
scattered.ccode mentioned above in a different patch?
That is part of the following commit:
commit fb35d30fe5b06cc24444f0405da8fbe0be5330d1
Author: Sean Christopherson <[email protected]> Date: Fri Jan 22 12:40:46 2021 -0800
x86/cpufeatures: Assign dedicated feature word for
CPUID_0x8000001F[EAX]
Collect the scattered SME/SEV related feature flags into a dedicated word. There are now five recognized features in CPUID.0x8000001F.EAX, with at least one more on the horizon (SEV-SNP). Using a dedicated
word
allows KVM to use its automagic CPUID adjustment logic when reporting the set of supported features to userspace.
No functional change intended.
Link:
https://lkml.kernel.org/r/[email protected]
Thanks,
Ashish
Quoted 26 lines
Quoted 25 lines
arch/x86/kernel/cpu/amd.c +3
-0+* 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@@ -417,6 +417,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 */
Re: [PATCH RFC v8 27/56] crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command
In reply to Kalra, AshishQuoted 129 lines · Ashish
Quoted 118 lines · Zhi Wang
Quoted 115 lines · On Mon, 20 Feb 2023 12:38:18 -0600 Michael Roth <[email protected]> wrote:
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.Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- Documentation/virt/coco/sev-guest.rst | 27 ++++++ drivers/crypto/ccp/sev-dev.c | 123 ++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 4 + include/uapi/linux/psp-sev.h | 17 ++++ 4 files changed, 171 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 +31
-0@@ -1481,6 +1481,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 */ +kfree (sev->snp_certs_data); + sev->snp_certs_data =NULL ; +wbinvd_on_all_cpus (); retry:@@ -1793,6 +1797,118 @@ 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; +int ret; + +if (!sev->snp_initialized || !argp->data) +return -EINVAL ; + +memset (&input,0 ,sizeof (input)); + +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 ; + } + +/* Copy the extended certs programmed through the SNP_SET_CONFIG */ +if (input.certs_address && sev->snp_certs_data) { +if (input.certs_len < sev->snp_certs_len) { +/* Return the certs length to userspace */ + input.certs_len = sev->snp_certs_len; +Quoted 9 lines
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) · line 1830
Quoted 4 lines
drivers/crypto/ccp/sev-dev.c +4
-0+ ret = -ENOSR ; + goto e_done; + } +What about if input.certs_len >
sev->snp_certs_len? Is it possbile for the userspace to know the length of data in the buffer? (I guess it might be able to know the certs len through the blob data, but a comment here would be nice)
Quoted 6 lines
If userspace provides an input buffer/length smaller then
snp_certs_len, then the above returns the "required" certs length back to userspace.And what is the issue if input.certs_len >
sev->snp_certs_len, the buffer returned back to userspace issev->snp_certs_lenas below.
My point is: How can the userspace know the length of return data is shorter than input.certs_len when input.certs_len > sev->snp_serts_len? as the length is only returned when input.certs_len < sev->snp_certs_len.
Quoted 159 lines
Thanks,
AshishQuoted 156 lines
Quoted 155 lines
drivers/crypto/ccp/sev-dev.c +88
-0+if (copy_to_user ((void *__user )input.certs_address, + sev->snp_certs_data, sev->snp_certs_len)) +return -EFAULT ; + } + + ret =0 ; + +e_done: +if (copy_to_user ((void __user *)argp->data, &input,sizeof (input))) + ret = -EFAULT ; + +return ret; +} + +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; +void *certs =NULL ; +int ret =0 ; + +if (!sev->snp_initialized || !argp->data) +return -EINVAL ; + +if (!writable) +return -EPERM ; + +memset (&input,0 ,sizeof (input)); + +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 )) +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)); +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. +*/ +mutex_lock (&sev->snp_certs_lock); +if (certs) { +kfree (sev->snp_certs_data); + sev->snp_certs_data = certs; + sev->snp_certs_len = input.certs_len; + }else { +kfree (sev->snp_certs_data); + sev->snp_certs_data =NULL ; + sev->snp_certs_len =0 ; + } +mutex_unlock (&sev->snp_certs_lock); + +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;@@ -1847,6 +1963,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;@@ -1962,6 +2084,7 @@ int sev_dev_init(struct psp_device *psp) goto e_sev; sev->cmd_buf_backup = (uint8_t *)sev->cmd_buf +PAGE_SIZE ; +mutex_init (&sev->snp_certs_lock); psp->sev_data = sev;drivers/crypto/ccp/sev-dev.h +4
-0@@ -66,6 +66,10 @@ struct sev_device {bool snp_initialized;struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS ]; +void *snp_certs_data; +u32 snp_certs_len; +struct mutex snp_certs_lock; +struct sev_user_data_snp_config snp_config; };int sev_dev_init (struct psp_device *psp);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, };@@ -192,6 +194,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 *
Re: [PATCH RFC v8 28/56] crypto: ccp: Provide APIs to query extended attestation report
In reply to Kalra, AshishQuoted 12 lines · Ashish
Quoted 10 lines · Zhi Wang
On Mon, 20 Feb 2023 12:38:19 -0600
Michael Roth <[email protected]> wrote:It seems in the discussion:
https://lore.kernel.org/lkml/[email protected]/,
this API is going to be removed. Will that fix land in this patch series or not? If not, It would be better to mention it in the comment message of this one or patch 45.
If yes, I guess this patch is not needed.
Quoted 5 lines
This API is definitely not going to be removed.
There will be some fixes and optimizations added to the API implementation (as per the discussions) and that will be included in v9.
Thanks.
I should use the term "this API is going to be refined" as snp_guest_ext_guest_request() is going to be renamed and refined. I gave this comment because when digging this patch, I found this API was going to be changed in the discussion based on v7 when digging this patch. It would be really nice to mention it in the v8 so that some review efforts can be saved. For example, some people might choose to skip reviewing this one in v8 and get back on it in the next version when it is ready. Or people can also evaluate the possible changes in v9 when reviewing this part.
Quoted 131 lines
Thanks,
AshishQuoted 128 lines
Quoted 127 lines
From: Brijesh Singh <[email protected]>
Version 2 of the GHCB specification defines VMGEXIT that is used to get the extended attestation report. The extended attestation report includes the certificate blobs provided through the
SNP_SET_EXT_CONFIG.The
snp_guest_ext_guest_request()will be used by the hypervisor to get the extended attestation report. See the GHCB specification for more details.Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- drivers/crypto/ccp/sev-dev.c | 47 ++++++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 33 +++++++++++++++++++++++++ 2 files changed, 80 insertions(+)drivers/crypto/ccp/sev-dev.c +47
-0@@ -2017,6 +2017,53 @@ int sev_guest_df_flush(int *error) }EXPORT_SYMBOL_GPL (sev_guest_df_flush); +int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data, +unsigned long vaddr,unsigned long *npages,unsigned long *fw_err) +{ +unsigned long expected_npages; +struct sev_device *sev; +int rc; + +if (!psp_master || !psp_master->sev_data) +return -ENODEV ; + + sev = psp_master->sev_data; + +if (!sev->snp_initialized) +return -EINVAL ; + +mutex_lock (&sev->snp_certs_lock); +/* +* Check if there is enough space to copy the certificate chain. Otherwise +* return ERROR code defined in the GHCB specification. +*/ + expected_npages = sev->snp_certs_len >>PAGE_SHIFT ; +if (*npages < expected_npages) { + *npages = expected_npages; + *fw_err =SNP_GUEST_REQ_INVALID_LEN ; +mutex_unlock (&sev->snp_certs_lock); +return -EINVAL ; + } + + rc =sev_do_cmd (SEV_CMD_SNP_GUEST_REQUEST , data, (int *)fw_err); +if (rc) { +mutex_unlock (&sev->snp_certs_lock); +return rc; + } + +/* Copy the certificate blob */ +if (sev->snp_certs_data) { + *npages = expected_npages; +memcpy ((void *)vaddr, sev->snp_certs_data, *npages <<PAGE_SHIFT ); + }else { + *npages =0 ; + } + +mutex_unlock (&sev->snp_certs_lock); +return rc; +} +EXPORT_SYMBOL_GPL (snp_guest_ext_guest_request); +static void sev_exit (struct kref *ref) {misc_deregister (&misc_dev->misc);include/linux/psp-sev.h +33
-0@@ -931,6 +931,32 @@ void snp_free_firmware_page(void *addr);*/ void snp_mark_pages_offline (unsigned long pfn,unsigned int npages); +/** +* snp_guest_ext_guest_request - perform the SNP extended guest request command +* defined in the GHCB specification. +* +* @data: the input guest request structure +* @vaddr: address where the certificate blob need to be copied. +* @npages: number of pages for the certificate blob. +* If the specified page count is less than the certificate blob size, then the +* required page count is returned with error code defined in the GHCB spec. +* If the specified page count is more than the certificate blob size, then +* page count is updated to reflect the amount of valid data copied in the +* vaddr. +* +* @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_ext_guest_request (struct sev_data_snp_guest_request *data, +unsigned long vaddr,unsigned long *npages, +unsigned long *error); +#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ static inline int@@ -968,6 +994,13 @@ static inline void *snp_alloc_firmware_page(gfp_t mask)static inline void snp_free_firmware_page (void *addr) { } +static inline int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data, +unsigned long vaddr,unsigned long *n, +unsigned long *error) +{ +return -ENODEV ; +} +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */
Tom Lendacky
maintainerRe: [PATCH RFC v8 27/56] crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command
In reply to Zhi WangQuoted 135 lines · Zhi Wang
Quoted 132 lines · Ashish
Quoted 118 lines · Zhi Wang
Quoted 115 lines · On Mon, 20 Feb 2023 12:38:18 -0600 Michael Roth <[email protected]> wrote:
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.Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- Documentation/virt/coco/sev-guest.rst | 27 ++++++ drivers/crypto/ccp/sev-dev.c | 123 ++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 4 + include/uapi/linux/psp-sev.h | 17 ++++ 4 files changed, 171 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 +31
-0@@ -1481,6 +1481,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 */ +kfree (sev->snp_certs_data); + sev->snp_certs_data =NULL ; +wbinvd_on_all_cpus (); retry:@@ -1793,6 +1797,118 @@ 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; +int ret; + +if (!sev->snp_initialized || !argp->data) +return -EINVAL ; + +memset (&input,0 ,sizeof (input)); + +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 ; + } + +/* Copy the extended certs programmed through the SNP_SET_CONFIG */ +if (input.certs_address && sev->snp_certs_data) { +if (input.certs_len < sev->snp_certs_len) { +/* Return the certs length to userspace */ + input.certs_len = sev->snp_certs_len; +Quoted 9 lines
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) · line 1830
Quoted 4 lines
drivers/crypto/ccp/sev-dev.c +4
-0+ ret = -ENOSR ; + goto e_done; + } +What about if input.certs_len >
sev->snp_certs_len? Is it possbile for the userspace to know the length of data in the buffer? (I guess it might be able to know the certs len through the blob data, but a comment here would be nice)If userspace provides an input buffer/length smaller then
snp_certs_len, then the above returns the "required" certs length back to userspace.
Quoted 7 lines
Quoted 3 lines
And what is the issue if input.certs_len >
sev->snp_certs_len, the buffer returned back to userspace issev->snp_certs_lenas below.My point is: How can the userspace know the length of return data is shorter than input.certs_len when input.certs_len >
sev->snp_serts_len? as the length is only returned when input.certs_len <sev->snp_certs_len.
The returned data has a defined format that can be used to calculate the overall length.
Thanks,
Tom
Quoted 161 lines
Quoted 160 lines
Thanks,
AshishQuoted 156 lines
Quoted 155 lines
drivers/crypto/ccp/sev-dev.c +88
-0+if (copy_to_user ((void *__user )input.certs_address, + sev->snp_certs_data, sev->snp_certs_len)) +return -EFAULT ; + } + + ret =0 ; + +e_done: +if (copy_to_user ((void __user *)argp->data, &input,sizeof (input))) + ret = -EFAULT ; + +return ret; +} + +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; +void *certs =NULL ; +int ret =0 ; + +if (!sev->snp_initialized || !argp->data) +return -EINVAL ; + +if (!writable) +return -EPERM ; + +memset (&input,0 ,sizeof (input)); + +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 )) +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)); +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. +*/ +mutex_lock (&sev->snp_certs_lock); +if (certs) { +kfree (sev->snp_certs_data); + sev->snp_certs_data = certs; + sev->snp_certs_len = input.certs_len; + }else { +kfree (sev->snp_certs_data); + sev->snp_certs_data =NULL ; + sev->snp_certs_len =0 ; + } +mutex_unlock (&sev->snp_certs_lock); + +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;@@ -1847,6 +1963,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;@@ -1962,6 +2084,7 @@ int sev_dev_init(struct psp_device *psp) goto e_sev; sev->cmd_buf_backup = (uint8_t *)sev->cmd_buf +PAGE_SIZE ; +mutex_init (&sev->snp_certs_lock); psp->sev_data = sev;drivers/crypto/ccp/sev-dev.h +4
-0@@ -66,6 +66,10 @@ struct sev_device {bool snp_initialized;struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS ]; +void *snp_certs_data; +u32 snp_certs_len; +struct mutex snp_certs_lock; +struct sev_user_data_snp_config snp_config; };int sev_dev_init (struct psp_device *psp);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, };@@ -192,6 +194,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 *
Re: [PATCH RFC v8 32/56] KVM: SVM: Add initial SEV-SNP support
In reply to Michael RothQuoted 73 lines · On Mon, 20 Feb 2023 12:38:23 -0600 Michael Roth <[email protected]> wrote:
From: 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.
Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- 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@@ -2306,6 +2309,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;@@ -2385,12 +2389,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 +1
-0@@ -76,6 +76,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 */
arch/x86/kvm/svm/svm.h static __always_inline bool sev_es_guest(struct kvm *kvm) · line 333
Quoted 10 lines
arch/x86/kvm/svm/svm.h +7
-0@@ -323,6 +324,13 @@ static __always_inline bool sev_es_guest(struct kvm *kvm)#endif } +static 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; +} +
Maybe also use __always_inline like sev_es_guest() above?
It seems solved some warnings before:
Quoted 3 lines
arch/x86/kvm/svm/svm.h +0
-0static inline void vmcb_mark_all_dirty (struct vmcb *vmcb) { vmcb->control.clean =0 ;
Re: [PATCH RFC v8 34/56] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_START command
In reply to Michael RothQuoted 124 lines · On Mon, 20 Feb 2023 12:38:25 -0600 Michael Roth <[email protected]> wrote:
From: Brijesh Singh <[email protected]>
KVM_SEV_SNP_LAUNCH_STARTbegins 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.
Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- .../virt/kvm/x86/amd-memory-encryption.rst | 24 ++++ arch/x86/kvm/svm/sev.c | 121 +++++++++++++++++- arch/x86/kvm/svm/svm.h | 1 + include/uapi/linux/kvm.h | 10 ++ 4 files changed, 153 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 +35
-2@@ -22,6 +22,7 @@#include <asm/pkru.h> #include <asm/trapnr.h> #include <asm/fpu/xcr.h> +#include <asm/sev.h> #include "mmu.h" #include "x86.h" @@ -75,6 +76,8 @@ static unsigned int nr_asids;static unsigned long *sev_asid_bitmap;static unsigned long *sev_reclaim_asid_bitmap; +static int snp_decommission_context (struct kvm *kvm); +struct enc_region {struct list_head list;unsigned long npages;@@ -100,12 +103,17 @@ static int sev_flush_asids(int min_asid, int max_asid)down_write (&sev_deactivate_lock);wbinvd_on_all_cpus (); - ret =sev_guest_df_flush (&error); + +if (sev_snp_enabled) + ret =sev_do_cmd (SEV_CMD_SNP_DF_FLUSH ,NULL , &error); +else + ret =sev_guest_df_flush (&error);up_write (&sev_deactivate_lock);if (ret) -pr_err ("SEV: DF_FLUSH failed, ret=%d, error=%#x\n" , ret, error); +pr_err ("SEV%s: DF_FLUSH failed, ret=%d, error=%#x\n" , + sev_snp_enabled ?"-SNP" :"" , ret, error);return ret; }@@ -2011,6 +2019,80 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)return ret; } +/* +* The guest context contains all the information, keys and metadata +* associated with the guest that the firmware tracks to implement SEV +* and SNP features. The firmware stores the guest context in hypervisor +* provide page via the SNP_GCTX_CREATE command. +*/ +static void *snp_context_create (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct sev_data_snp_addr data = {}; +void *context; +int rc; + +/* Allocate memory for context page */ + context =snp_alloc_firmware_page (GFP_KERNEL_ACCOUNT ); +if (!context) +return NULL ; + + data.gctx_paddr =__psp_pa (context); + rc =__sev_issue_cmd (argp->sev_fd,SEV_CMD_SNP_GCTX_CREATE , &data, &argp->error); +if (rc) { +snp_free_firmware_page (context); +return NULL ; + } + +return context;
arch/x86/kvm/svm/sev.c int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd) · line 2056
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+} + +static int snp_bind_asid (struct kvm *kvm,int *error) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct sev_data_snp_activate data = {0 }; + + data.gctx_paddr =__psp_pa (sev->snp_context); + data.asid =sev_get_asid (kvm); +return sev_issue_cmd (kvm,SEV_CMD_SNP_ACTIVATE , &data, error);
According to the SNP ABI specification[1] 8.10 SNP_ACTIVATE:
"The firmware checks that a DF_FLUSH is not required. If a DF_FLUSH is required, the firmware returns DFFLUSH_REQUIRED. Note that all ASIDs are marked to require a DF_FLUSH at reset."
Do we need a SNP_DF_FLUSH here before calling SNP_ACTIVATE or handle the situation if the PSP firmware returns DFFLUSH_REQUIRED?
Quoted 138 lines
arch/x86/kvm/svm/sev.c +73
-1+} + +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; + +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 ; + + 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; +} +int sev_mem_enc_ioctl (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;@@ -2101,6 +2183,9 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)case KVM_SEV_RECEIVE_FINISH : r =sev_receive_finish (kvm, &sev_cmd);break ; +case KVM_SEV_SNP_LAUNCH_START : + r =snp_launch_start (kvm, &sev_cmd); +break ;default : r = -EINVAL ; goto out;@@ -2292,6 +2377,28 @@ int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd)return ret; } +static int snp_decommission_context (struct kvm *kvm) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct sev_data_snp_addr data = {}; +int ret; + +/* If context is not created then do nothing */ +if (!sev->snp_context) +return 0 ; + + data.gctx_paddr =__sme_pa (sev->snp_context); + ret =sev_do_cmd (SEV_CMD_SNP_DECOMMISSION , &data,NULL ); +if (WARN_ONCE (ret,"failed to release guest context" )) +return ret; + +/* free the context page now */ +snp_free_firmware_page (sev->snp_context); + sev->snp_context =NULL ; + +return 0 ; +} +void sev_vm_destroy (struct kvm *kvm) {struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;@@ -2333,7 +2440,15 @@ void sev_vm_destroy(struct kvm *kvm) } } -sev_unbind_asid (kvm, sev->handle); +if (sev_snp_guest (kvm)) { +if (snp_decommission_context (kvm)) { +WARN_ONCE (1 ,"Failed to free SNP guest context, leaking asid!\n" ); +return ; + } + }else { +sev_unbind_asid (kvm, sev->handle); + } +sev_asid_free (sev); }arch/x86/kvm/svm/svm.h +1
-0@@ -92,6 +92,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@@ -1919,6 +1919,7 @@ enum sev_cmd_id {/* SNP specific commands */ KVM_SEV_SNP_INIT , +KVM_SEV_SNP_LAUNCH_START , KVM_SEV_NR_MAX, };@@ -2026,6 +2027,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)
Re: [PATCH RFC v8 45/56] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event
In reply to Michael RothQuoted 37 lines · Michael Roth
From: 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_REQUESTAPI define in the SEV-SNP firmware specification.The
SNP_EXT_GUEST_REQUESTis similar toSNP_GUEST_REQUESTwith the difference of an additional certificate blob that can be passed through theSNP_SET_CONFIGioctl defined in the CCP driver. The CCP driver providessnp_guest_ext_guest_request()that is used by the KVM to get both the report and certificate data at once.Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- arch/x86/kvm/svm/sev.c | 185 +++++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/svm/svm.h | 2 + 2 files changed, 181 insertions(+), 6 deletions(-)arch/x86/kvm/svm/sev.c +1
-0@@ -327,6 +327,7 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)if (ret) goto e_free; +mutex_init (&sev->guest_req_lock); ret =sev_snp_init (&argp->error, false); }else { ret =sev_platform_init (&argp->error);@@ -2059,23 +2060,34 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)*/ static void *snp_context_create (struct kvm *kvm,struct kvm_sev_cmd *argp) {
arch/x86/kvm/svm/sev.c int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd) · line 2071
Quoted 10 lines
arch/x86/kvm/svm/sev.c +6
-1+struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;struct sev_data_snp_addr data = {}; -void *context; +void *context, *certs_data;int rc; +/* Allocate memory used for the certs data in SNP guest request */ + certs_data =kzalloc (SEV_FW_BLOB_MAX_SIZE ,GFP_KERNEL_ACCOUNT ); +if (!certs_data) +return NULL ;
I don't understand why this is part of the context creation, which again is part of the KVM_SEV_SNP_LAUNCH_START op. Would you mind to create a separate op for this and then check later on while you use the buffer whether it was ever allocated?
Alex
Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
Re: [PATCH RFC v8 35/56] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_UPDATE command
In reply to Michael RothQuoted 94 lines · On Mon, 20 Feb 2023 12:38:26 -0600 Michael Roth <[email protected]> wrote:
From: Brijesh Singh <[email protected]>
The
KVM_SEV_SNP_LAUNCH_UPDATEcommand can be used to insert data into the guest's memory. The data is encrypted with the cryptographic context created with theKVM_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.
Co-developed-by Michael Roth <[email protected]>Signed-off-by Michael Roth <[email protected]>Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>--- .../virt/kvm/x86/amd-memory-encryption.rst | 29 +++ arch/x86/kvm/svm/sev.c | 190 ++++++++++++++++++ include/uapi/linux/kvm.h | 19 ++ 3 files changed, 238 insertions(+)Documentation/virt/kvm/x86/amd-memory-encryption.rst +29
-0@@ -485,6 +485,35 @@ 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 +21
-0@@ -234,6 +234,37 @@ static void sev_decommission(unsigned int handle)sev_guest_decommission (&decommission,NULL ); } +static int snp_page_reclaim (u64 pfn) +{ +struct sev_data_snp_page_reclaim data = {0 }; +int err, rc; + + data.paddr =__sme_set (pfn <<PAGE_SHIFT ); + rc =sev_do_cmd (SEV_CMD_SNP_PAGE_RECLAIM , &data, &err); +if (rc) { +/* +* If the reclaim failed, then page is no longer safe +* to use. +*/ +snp_mark_pages_offline (pfn, +page_level_size (PG_LEVEL_4K ) >>PAGE_SHIFT ); + } + +return rc; +} + +static int host_rmp_make_shared (u64 pfn,enum pg_level level,bool leak) +{
arch/x86/kvm/svm/sev.c static void sev_decommission(unsigned int handle) · line 267
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+int rc; + + rc =rmp_make_shared (pfn, level); +if (rc && leak) +snp_mark_pages_offline (pfn, +page_level_size (level) >>PAGE_SHIFT ); + +return rc; +} +
PATCH 24 has similar functions. It would be better to expose them.
Quoted 213 lines
arch/x86/kvm/svm/sev.c +159
-0static void sev_unbind_asid (struct kvm *kvm,unsigned int handle) {struct sev_data_deactivate deactivate;@@ -2093,6 +2124,162 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)return rc; } +static int snp_launch_update_gfn_handler (struct kvm *kvm, +struct kvm_gfn_range *range, +void *opaque) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct kvm_memory_slot *memslot = range->slot; +struct sev_data_snp_launch_update data = {0 }; +struct kvm_sev_snp_launch_update params; +struct kvm_sev_cmd *argp = opaque; +int *error = &argp->error; +int i, n =0 , ret =0 ; +unsigned long npages; +kvm_pfn_t *pfns; +gfn_t gfn; + +if (!kvm_slot_can_be_private (memslot)) { +pr_err ("SEV-SNP requires restricted memory.\n" ); +return -EINVAL ; + } + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params))) { +pr_err ("Failed to copy user parameters for SEV-SNP launch.\n" ); +return -EFAULT ; + } + + data.gctx_paddr =__psp_pa (sev->snp_context); + + npages = range->end - range->start; + pfns =kvmalloc_array (npages,sizeof (*pfns),GFP_KERNEL_ACCOUNT ); +if (!pfns) +return -ENOMEM ; + +pr_debug ("%s: GFN range 0x%llx-0x%llx, type %d\n" , __func__, + range->start, range->end, params.page_type); + +for (gfn = range->start, i =0 ; gfn < range->end; gfn++, i++) { +int order, level; +void *kvaddr; + + ret =kvm_restrictedmem_get_pfn (memslot, gfn, &pfns[i], &order); +if (ret) + goto e_release; + + n++; + ret =snp_lookup_rmpentry ((u64 )pfns[i], &level); +if (ret) { +pr_err ("Failed to ensure GFN 0x%llx is in initial shared state, ret: %d\n" , + gfn, ret); +return -EFAULT ; + } + + kvaddr =pfn_to_kaddr (pfns[i]); +if (!virt_addr_valid (kvaddr)) { +pr_err ("Invalid HVA 0x%llx for GFN 0x%llx\n" , (uint64_t )kvaddr, gfn); + ret = -EINVAL ; + goto e_release; + } + + ret =kvm_read_guest_page (kvm, gfn, kvaddr,0 ,PAGE_SIZE ); +if (ret) { +pr_err ("Guest read failed, ret: 0x%x\n" , ret); + goto e_release; + } + + ret =rmp_make_private (pfns[i], gfn <<PAGE_SHIFT ,PG_LEVEL_4K , +sev_get_asid (kvm), true); +if (ret) { + ret = -EFAULT ; + goto e_release; + } + + data.address =__sme_set (pfns[i] <<PAGE_SHIFT ); + data.page_size =X86_TO_RMP_PG_LEVEL (PG_LEVEL_4K ); + data.page_type = params.page_type; + data.vmpl3_perms = params.vmpl3_perms; + data.vmpl2_perms = params.vmpl2_perms; + data.vmpl1_perms = params.vmpl1_perms; + ret =__sev_issue_cmd (argp->sev_fd,SEV_CMD_SNP_LAUNCH_UPDATE , + &data, error); +if (ret) { +pr_err ("SEV-SNP launch update failed, ret: 0x%x, fw_error: 0x%x\n" , + ret, *error); +snp_page_reclaim (pfns[i]); + +/* +* When invalid CPUID function entries are detected, the firmware +* corrects these entries for debugging purpose and leaves the +* page unencrypted so it can be provided users for debugging +* and error-reporting. +* +* Copy the corrected CPUID page back to shared memory so +* userpsace can retrieve this information. +*/ +if (params.page_type ==SNP_PAGE_TYPE_CPUID && + *error ==SEV_RET_INVALID_PARAM ) { +int ret; + +host_rmp_make_shared (pfns[i],PG_LEVEL_4K , true); + + ret =kvm_write_guest_page (kvm, gfn, kvaddr,0 ,PAGE_SIZE ); +if (ret) +pr_err ("Failed to write CPUID page back to userspace, ret: 0x%x\n" , + ret); + } + + + goto e_release; + } + } + +/* +* Memory attribute updates via KVM_SET_MEMORY_ATTRIBUTES are serialized +* via kvm->slots_lock, so use the same protocol for updating them here. +*/ +mutex_lock (&kvm->slots_lock); +kvm_vm_set_region_attr (kvm, range->start, range->end,KVM_MEMORY_ATTRIBUTE_PRIVATE ); +mutex_unlock (&kvm->slots_lock); + +e_release: +/* Content of memory is updated, mark pages dirty */ +for (i =0 ; i < n; i++) { +set_page_dirty (pfn_to_page (pfns[i])); +mark_page_accessed (pfn_to_page (pfns[i])); + +/* +* If its an error, then update RMP entry to change page ownership +* to the hypervisor. +*/ +if (ret) +host_rmp_make_shared (pfns[i],PG_LEVEL_4K , true); + +put_page (pfn_to_page (pfns[i])); + } + +kvfree (pfns); +return ret; +} + +static int snp_launch_update (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct kvm_sev_snp_launch_update params; + +if (!sev_snp_guest (kvm)) +return -ENOTTY ; + +if (!sev->snp_context) +return -EINVAL ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params))) +return -EFAULT ; + +return kvm_vm_do_hva_range_op (kvm, params.uaddr, params.uaddr + params.len, + snp_launch_update_gfn_handler, argp); +} +int sev_mem_enc_ioctl (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;@@ -2186,6 +2373,9 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)case KVM_SEV_SNP_LAUNCH_START : r =snp_launch_start (kvm, &sev_cmd);break ; +case KVM_SEV_SNP_LAUNCH_UPDATE : + r =snp_launch_update (kvm, &sev_cmd); +break ;default : r = -EINVAL ; goto out;include/uapi/linux/kvm.h +19
-0@@ -1920,6 +1920,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, };@@ -2036,6 +2037,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)
Re: [PATCH RFC v8 47/56] KVM: SVM: Support SEV-SNP AP Creation NAE event
In reply to Michael RothQuoted 36 lines · Michael Roth
From: 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, theKVM_REQ_UPDATE_PROTECTED_GUEST_STATEevent 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, theKVM_REQ_UPDATE_PROTECTED_GUEST_STATEevent is added to vCPU and then the vCPU is kicked.The
KVM_REQ_UPDATE_PROTECTED_GUEST_STATEevent handler will be invoked as a result of the event or as a result of an INIT. The handler sets the vCPU to theKVM_MP_STATE_UNINITIALIZEDstate, 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 toKVM_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 asKVM_MP_STATE_UNINITIALIZEDto prevent it from being run.
Quoted 5 lines
Signed-off-by Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>[mdr: add handling for restrictedmem]Signed-off-by Michael Roth <[email protected]>
What is the intended boot sequence for SEV-SNP guests? FWIW with this interface in place, guests will typically use in-guest VMSA pages to hold secondary vcpu state. But that means we're now allocating 4kb of memory for every vcpu that we create that will be for most of the guest's lifetime superfluous.
Wouldn't it make more sense to have a model where we only allocate the VMSA for the boot CPU and leave secondary allocation to the guest? We already need firmware changes for SEV-SNP - may as well make this one more.
[...]
Quoted 7 lines
arch/x86/kvm/svm/sev.c +7
-0+ +static int sev_snp_ap_creation (struct vcpu_svm *svm) +{ +struct kvm_sev_info *sev = &to_kvm_svm (svm->vcpu.kvm)->sev_info; +struct kvm_vcpu *vcpu = &svm->vcpu; +struct kvm_vcpu *target_vcpu; +struct vcpu_svm *target_svm;
arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3688
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+unsigned int request; +unsigned int apic_id; +bool kick; +int ret; + + request =lower_32_bits (svm->vmcb->control.exit_info_1); + apic_id =upper_32_bits (svm->vmcb->control.exit_info_1); + +/* Validate the APIC ID */ + target_vcpu =kvm_get_vcpu_by_id (vcpu->kvm, apic_id);
Out of curiosity: The target CPU can be my own vCPU, right?
Quoted 44 lines
arch/x86/kvm/svm/sev.c +44
-0+if (!target_vcpu) { +vcpu_unimpl (vcpu,"vmgexit: invalid AP APIC ID [%#x] from guest\n" , + apic_id); +return -EINVAL ; + } + + ret =0 ; + + target_svm =to_svm (target_vcpu); + +/* +* The target vCPU is valid, so the vCPU will be kicked unless the +* request is for CREATE_ON_INIT. For any errors at this stage, the +* kick will place the vCPU in an non-runnable state. +*/ + kick = true; + +mutex_lock (&target_svm->sev_es.snp_vmsa_mutex); + + target_svm->sev_es.snp_vmsa_gpa =INVALID_PAGE ; + target_svm->sev_es.snp_ap_create = true; + +/* Interrupt injection mode shouldn't change for AP creation */ +if (request <SVM_VMGEXIT_AP_DESTROY ) { +u64 sev_features; + + sev_features = vcpu->arch.regs[VCPU_REGS_RAX ]; + sev_features ^= sev->sev_features; +if (sev_features &SVM_SEV_FEAT_INT_INJ_MODES ) { +vcpu_unimpl (vcpu,"vmgexit: invalid AP injection mode [%#lx] from guest\n" , + vcpu->arch.regs[VCPU_REGS_RAX ]); + ret = -EINVAL ; + goto out; + } + } + +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);
arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3742
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ ret = -EINVAL ; + goto out; + } + +/* +* 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.
This will break genuine current Linux kernels that just happen to allocate a guest page, no? In fact, given enough vCPUs you're almost guaranteed to hit an aligned structure somewhere. What is the guest supposed to do in that situation?
arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3754
Quoted 12 lines
arch/x86/kvm/svm/sev.c +12
-0+*/ +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); + ret = -EINVAL ; + goto out; + } + + target_svm->sev_es.snp_vmsa_gpa = svm->vmcb->control.exit_info_2; +break ; +case SVM_VMGEXIT_AP_DESTROY :
I don't understand the destroy path. Why does this case destroy anything?
arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3766
Quoted 12 lines
arch/x86/kvm/svm/sev.c +12
-0+break ; +default : +vcpu_unimpl (vcpu,"vmgexit: invalid AP creation request [%#x] from guest\n" , + request); + ret = -EINVAL ; +break ; + } + +out: +if (kick) { +if (target_vcpu->arch.mp_state ==KVM_MP_STATE_UNINITIALIZED ) + target_vcpu->arch.mp_state =KVM_MP_STATE_RUNNABLE ;
What if the guest AP goes through a create -> destroy -> create cycle? Will it stay runnable while destroyed?
Alex
arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3779
Quoted 13 lines
arch/x86/kvm/svm/sev.c +10
-0+ +kvm_make_request (KVM_REQ_UPDATE_PROTECTED_GUEST_STATE , target_vcpu); +kvm_vcpu_kick (target_vcpu); + } + +mutex_unlock (&target_svm->sev_es.snp_vmsa_mutex); + +return ret; +} +static int sev_handle_vmgexit_msr_protocol (struct vcpu_svm *svm) {struct vmcb_control_area *control = &svm->vmcb->control;
Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
Re: [PATCH RFC v8 41/56] KVM: SVM: Add support to handle MSR based Page State Change VMGEXIT
In reply to Michael RothQuoted 9 lines · On Mon, 20 Feb 2023 12:38:32 -0600 Michael Roth <[email protected]> wrote:
From: 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.
Forward these requests to userspace via
KVM_EXIT_VMGEXITso the VMM can issue the KVM ioctls to update the page state accordingly.
It would be better to describe the design purpose. Like, why should the page state change VMGEIXT be forwarded to the userspace instead of being handled in the kernel.
Quoted 152 lines
Co-developed-by Michael Roth <[email protected]>Signed-off-by Michael Roth <[email protected]>Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>--- arch/x86/include/asm/sev-common.h | 9 ++++++++ arch/x86/kvm/svm/sev.c | 25 +++++++++++++++++++++++ arch/x86/kvm/trace.h | 34 +++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 1 + 4 files changed, 69 insertions(+)arch/x86/include/asm/sev-common.h +9
-0@@ -93,6 +93,10 @@ enum psc_op { };#define GHCB_MSR_PSC_REQ 0x014 +#define GHCB_MSR_PSC_GFN_POS 12 +#define GHCB_MSR_PSC_GFN_MASK GENMASK_ULL(39, 0) +#define GHCB_MSR_PSC_OP_POS 52 +#define GHCB_MSR_PSC_OP_MASK 0xf#define GHCB_MSR_PSC_REQ_GFN (gfn, op) \/* GHCBData[55:52] */ \ (((u64)((op) & 0xf) << 52) | \@@ -102,6 +106,11 @@ enum psc_op { GHCB_MSR_PSC_REQ)#define GHCB_MSR_PSC_RESP 0x015 +#define GHCB_MSR_PSC_ERROR_POS 32 +#define GHCB_MSR_PSC_ERROR_MASK GENMASK_ULL(31, 0) +#define GHCB_MSR_PSC_ERROR GENMASK_ULL(31, 0) +#define GHCB_MSR_PSC_RSVD_POS 12 +#define GHCB_MSR_PSC_RSVD_MASK GENMASK_ULL(19, 0)#define GHCB_MSR_PSC_RESP_VAL (val) \/* GHCBData[63:32] */ \ (((u64)(val) & GENMASK_ULL(63, 32)) >> 32)arch/x86/kvm/svm/sev.c +25
-0@@ -30,6 +30,7 @@#include "svm_ops.h" #include "cpuid.h" #include "trace.h" +#include "mmu.h" #ifndef CONFIG_KVM_AMD_SEV /* @@ -3345,6 +3346,23 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) svm->vmcb->control.ghcb_gpa = value; } +/* +* TODO: need to get the value set by userspace in vcpu->run->vmgexit.ghcb_msr +* and process that here accordingly. +*/ +static int snp_complete_psc_msr_protocol (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); + +set_ghcb_msr_bits (svm,0 , +GHCB_MSR_PSC_ERROR_MASK ,GHCB_MSR_PSC_ERROR_POS ); + +set_ghcb_msr_bits (svm,0 ,GHCB_MSR_PSC_RSVD_MASK ,GHCB_MSR_PSC_RSVD_POS ); +set_ghcb_msr_bits (svm,GHCB_MSR_PSC_RESP ,GHCB_MSR_INFO_MASK ,GHCB_MSR_INFO_POS ); + +return 1 ;/* resume */ +} +static int sev_handle_vmgexit_msr_protocol (struct vcpu_svm *svm) {struct vmcb_control_area *control = &svm->vmcb->control;@@ -3445,6 +3463,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;arch/x86/kvm/trace.h +34
-0@@ -7,6 +7,7 @@#include <asm/svm.h> #include <asm/clocksource.h> #include <asm/pvclock-abi.h> +#include <asm/sev-common.h> #undef TRACE_SYSTEM#define TRACE_SYSTEM kvm@@ -1831,6 +1832,39 @@ TRACE_EVENT(kvm_vmgexit_msr_protocol_exit, __entry->vcpu_id, __entry->ghcb_gpa, __entry->result) ); +/* +* Tracepoint for the SEV-SNP page state change processing +*/ +#define psc_operation \ + {SNP_PAGE_STATE_PRIVATE, "private"}, \ + {SNP_PAGE_STATE_SHARED, "shared"} \ + +TRACE_EVENT (kvm_snp_psc, + TP_PROTO(unsigned int vcpu_id, u64 pfn, u64 gpa, u8 op, int level), +TP_ARGS (vcpu_id, pfn, gpa, op, level), + +TP_STRUCT__entry ( + __field(int, vcpu_id) + __field(u64, pfn) + __field(u64, gpa) + __field(u8, op) +__field (int, level) + ), + +TP_fast_assign ( + __entry->vcpu_id = vcpu_id; + __entry->pfn = pfn; + __entry->gpa = gpa; + __entry->op = op; + __entry->level = level; + ), + +TP_printk ("vcpu %u, pfn %llx, gpa %llx, op %s, level %d" , + __entry->vcpu_id, __entry->pfn, __entry->gpa, +__print_symbolic (__entry->op, psc_operation), + __entry->level) +); +#endif /* _TRACE_KVM_H */ #undef TRACE_INCLUDE_PATHarch/x86/kvm/x86.c +1
-0@@ -13515,6 +13515,7 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_enter);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_vmgexit_exit);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_vmgexit_msr_protocol_enter);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_vmgexit_msr_protocol_exit); +EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_snp_psc);static int __init kvm_x86_init (void ) {
Tom Lendacky
maintainerRe: [PATCH RFC v8 34/56] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_START command
In reply to Zhi WangQuoted 140 lines · Zhi Wang
Quoted 124 lines · On Mon, 20 Feb 2023 12:38:25 -0600 Michael Roth <[email protected]> wrote:
From: Brijesh Singh <[email protected]>
KVM_SEV_SNP_LAUNCH_STARTbegins 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.
Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- .../virt/kvm/x86/amd-memory-encryption.rst | 24 ++++ arch/x86/kvm/svm/sev.c | 121 +++++++++++++++++- arch/x86/kvm/svm/svm.h | 1 + include/uapi/linux/kvm.h | 10 ++ 4 files changed, 153 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 +35
-2@@ -22,6 +22,7 @@#include <asm/pkru.h> #include <asm/trapnr.h> #include <asm/fpu/xcr.h> +#include <asm/sev.h> #include "mmu.h" #include "x86.h" @@ -75,6 +76,8 @@ static unsigned int nr_asids;static unsigned long *sev_asid_bitmap;static unsigned long *sev_reclaim_asid_bitmap; +static int snp_decommission_context (struct kvm *kvm); +struct enc_region {struct list_head list;unsigned long npages;@@ -100,12 +103,17 @@ static int sev_flush_asids(int min_asid, int max_asid)down_write (&sev_deactivate_lock);wbinvd_on_all_cpus (); - ret =sev_guest_df_flush (&error); + +if (sev_snp_enabled) + ret =sev_do_cmd (SEV_CMD_SNP_DF_FLUSH ,NULL , &error); +else + ret =sev_guest_df_flush (&error);up_write (&sev_deactivate_lock);if (ret) -pr_err ("SEV: DF_FLUSH failed, ret=%d, error=%#x\n" , ret, error); +pr_err ("SEV%s: DF_FLUSH failed, ret=%d, error=%#x\n" , + sev_snp_enabled ?"-SNP" :"" , ret, error);return ret; }@@ -2011,6 +2019,80 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)return ret; } +/* +* The guest context contains all the information, keys and metadata +* associated with the guest that the firmware tracks to implement SEV +* and SNP features. The firmware stores the guest context in hypervisor +* provide page via the SNP_GCTX_CREATE command. +*/ +static void *snp_context_create (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct sev_data_snp_addr data = {}; +void *context; +int rc; + +/* Allocate memory for context page */ + context =snp_alloc_firmware_page (GFP_KERNEL_ACCOUNT ); +if (!context) +return NULL ; + + data.gctx_paddr =__psp_pa (context); + rc =__sev_issue_cmd (argp->sev_fd,SEV_CMD_SNP_GCTX_CREATE , &data, &argp->error); +if (rc) { +snp_free_firmware_page (context); +return NULL ; + } + +return context;arch/x86/kvm/svm/sev.c int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd) · line 2056
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+} + +static int snp_bind_asid (struct kvm *kvm,int *error) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct sev_data_snp_activate data = {0 }; + + data.gctx_paddr =__psp_pa (sev->snp_context); + data.asid =sev_get_asid (kvm); +return sev_issue_cmd (kvm,SEV_CMD_SNP_ACTIVATE , &data, error);According to the SNP ABI specification[1] 8.10
SNP_ACTIVATE:
Quoted 8 lines
"The firmware checks that a
DF_FLUSHis not required. If aDF_FLUSHis required, the firmware returnsDFFLUSH_REQUIRED. Note that all ASIDs are marked to require aDF_FLUSHat reset."Do we need a
SNP_DF_FLUSHhere before callingSNP_ACTIVATEor handle the situation if the PSP firmware returnsDFFLUSH_REQUIRED?
This is related to ASID use. An initial DF_FLUSH is done which allows any SNP ASID to be used once without requiring a DF_FLUSH. Once an ASID has been used, it cannot be re-used until a DF_FLUSH is performed. The ASID recycling code takes care of that.
Thanks,
Tom
Quoted 140 lines
Quoted 139 lines
arch/x86/kvm/svm/sev.c +73
-1+} + +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; + +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 ; + + 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; +} +int sev_mem_enc_ioctl (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;@@ -2101,6 +2183,9 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)case KVM_SEV_RECEIVE_FINISH : r =sev_receive_finish (kvm, &sev_cmd);break ; +case KVM_SEV_SNP_LAUNCH_START : + r =snp_launch_start (kvm, &sev_cmd); +break ;default : r = -EINVAL ; goto out;@@ -2292,6 +2377,28 @@ int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd)return ret; } +static int snp_decommission_context (struct kvm *kvm) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct sev_data_snp_addr data = {}; +int ret; + +/* If context is not created then do nothing */ +if (!sev->snp_context) +return 0 ; + + data.gctx_paddr =__sme_pa (sev->snp_context); + ret =sev_do_cmd (SEV_CMD_SNP_DECOMMISSION , &data,NULL ); +if (WARN_ONCE (ret,"failed to release guest context" )) +return ret; + +/* free the context page now */ +snp_free_firmware_page (sev->snp_context); + sev->snp_context =NULL ; + +return 0 ; +} +void sev_vm_destroy (struct kvm *kvm) {struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;@@ -2333,7 +2440,15 @@ void sev_vm_destroy(struct kvm *kvm) } } -sev_unbind_asid (kvm, sev->handle); +if (sev_snp_guest (kvm)) { +if (snp_decommission_context (kvm)) { +WARN_ONCE (1 ,"Failed to free SNP guest context, leaking asid!\n" ); +return ; + } + }else { +sev_unbind_asid (kvm, sev->handle); + } +sev_asid_free (sev); }arch/x86/kvm/svm/svm.h +1
-0@@ -92,6 +92,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@@ -1919,6 +1919,7 @@ enum sev_cmd_id {/* SNP specific commands */ KVM_SEV_SNP_INIT , +KVM_SEV_SNP_LAUNCH_START , KVM_SEV_NR_MAX, };@@ -2026,6 +2027,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)
Re: [PATCH RFC v8 19/56] x86/fault: Return pfn from dump_pagetable() for SEV-specific fault handling.
In reply to Michael RothQuoted 36 lines · Michael Roth
From: Ashish Kalra <[email protected]>
Return pfn from
dump_pagetable()to do SEV-specific fault handling. Used for handling SNP RMP page fault.Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- arch/x86/mm/fault.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)arch/x86/mm/fault.c +3
-1@@ -311,7 +311,7 @@ static bool low_pfn(unsigned long pfn)return pfn < max_low_pfn; } -static void dump_pagetable (unsigned long address) +static unsigned long dump_pagetable (unsigned long address) {pgd_t *base =__va (read_cr3_pa ());pgd_t *pgd = &base[pgd_index (address)];@@ -345,8 +345,10 @@ static void dump_pagetable(unsigned long address) pte =pte_offset_kernel (pmd, address);pr_cont ("*pte = %0*Lx " ,sizeof (*pte) *2 , (u64 )pte_val (*pte)); +return 0 ; out:pr_cont ("\n" ); + return 0; }#else /* CONFIG_X86_64: */
arch/x86/mm/fault.c static int bad_address(void *p) · line 376
Quoted 9 lines
arch/x86/mm/fault.c +2
-1@@ -367,10 +369,11 @@ static int bad_address(void *p)return get_kernel_nofault (dummy, (unsigned long *)p); } -static void dump_pagetable (unsigned long address) +static unsigned long dump_pagetable (unsigned long address) {pgd_t *base =__va (read_cr3_pa ());pgd_t *pgd = base +pgd_index (address); +unsigned long pfn;
pfn should be initialized otherwise this function may return an uninitialized value.
Quoted 50 lines
arch/x86/mm/fault.c +6
-2p4d_t *p4d;pud_t *pud;pmd_t *pmd;@@ -388,6 +391,7 @@ static void dump_pagetable(unsigned long address)if (bad_address (p4d)) goto bad; + pfn =p4d_pfn (*p4d);pr_cont ("P4D %lx " ,p4d_val (*p4d));if (!p4d_present (*p4d) ||p4d_large (*p4d)) goto out;@@ -396,6 +400,7 @@ static void dump_pagetable(unsigned long address)if (bad_address (pud)) goto bad; + pfn =pud_pfn (*pud);pr_cont ("PUD %lx " ,pud_val (*pud));if (!pud_present (*pud) ||pud_large (*pud)) goto out;@@ -404,6 +409,7 @@ static void dump_pagetable(unsigned long address)if (bad_address (pmd)) goto bad; + pfn =pmd_pfn (*pmd);pr_cont ("PMD %lx " ,pmd_val (*pmd));if (!pmd_present (*pmd) ||pmd_large (*pmd)) goto out;@@ -412,13 +418,14 @@ static void dump_pagetable(unsigned long address)if (bad_address (pte)) goto bad; + pfn =pte_pfn (*pte);pr_cont ("PTE %lx" ,pte_val (*pte)); out:pr_cont ("\n" ); - -return ; +return pfn; bad:pr_info ("BAD\n" ); + return -1; }#endif /* CONFIG_X86_64 */ Signature · 1 line
2.25.1
Re: [PATCH RFC v8 44/56] KVM: SVM: Add support to handle the RMP nested page fault
In reply to Michael RothQuoted 49 lines · On Mon, 20 Feb 2023 12:38:35 -0600 Michael Roth <[email protected]> wrote:
From: 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.
Page state changes are handled by userspace, so if an RMP fault is triggered as a result of an RMP NPT fault, exit to userspace just like with explicit page-state change requests.
RMP NPT faults can also occur if the guest pvalidates a 2M page as 4K, in which case the RMP entries need to be PSMASH'd. Handle this case immediately in the kernel.
Co-developed-by Michael Roth <[email protected]>Signed-off-by Michael Roth <[email protected]>Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>--- arch/x86/kvm/svm/sev.c | 84 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 21 +++++++++-- arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 102 insertions(+), 4 deletions(-)arch/x86/kvm/svm/sev.c +8
-0@@ -3347,6 +3347,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); +} +/* * TODO: need to get the value set by userspace in vcpu->run->vmgexit.ghcb_msr * and process that here accordingly. @@ -3872,3 +3879,80 @@ void sev_adjust_mapping_level(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int *lepr_debug ("%s: GFN: 0x%llx, PFN: 0x%llx, level: %d, rmp_level: %d, level_orig: %d, assigned: %d\n" , __func__, gfn, pfn, *level, rmp_level, level_orig, assigned); } +
arch/x86/kvm/svm/sev.c void sev_adjust_mapping_level(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int *le · line 3892
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+void handle_rmp_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u64 error_code) +{ +int order, rmp_level, assigned, ret; +struct kvm_memory_slot *slot; +struct kvm *kvm = vcpu->kvm; +kvm_pfn_t pfn; +gfn_t gfn; + +/* +* Private memslots punt handling of implicit page state changes to
^put
Quoted 115 lines
arch/x86/kvm/svm/sev.c +66
-0+* userspace, so the only RMP faults expected here for +* PFERR_GUEST_SIZEM_MASK. Anything else suggests that the RMP table has +* gotten out of sync with the private memslot. +* +* TODO: However, this case has also been noticed when an access occurs +* to an NPT mapping that has just been split/PSMASHED, in which case +* PFERR_GUEST_SIZEM_MASK might not be set. In those cases it should be +* safe to ignore and let the guest retry, but log these just in case +* for now. +*/ +if (!(error_code &PFERR_GUEST_SIZEM_MASK )) { +pr_warn_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_restrictedmem_get_pfn (slot, gfn, &pfn, &order); +if (ret) { +pr_warn_ratelimited ("Unexpected RMP fault, no private backing page for GPA 0x%llx\n" , + gpa); +return ; + } + + assigned =snp_lookup_rmpentry (pfn, &rmp_level); +if (assigned !=1 ) { +pr_warn_ratelimited ("Unexpected RMP fault, no assigned RMP entry for GPA 0x%llx\n" , + gpa); + 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@@ -1976,15 +1976,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@@ -714,6 +714,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 sev_adjust_mapping_level (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int *level); +void handle_rmp_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u64 error_code);/* vmenter.S */
Re: [PATCH RFC v8 45/56] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event
In reply to Michael RothQuoted 161 lines · On Mon, 20 Feb 2023 12:38:36 -0600 Michael Roth <[email protected]> wrote:
From: 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_REQUESTAPI define in the SEV-SNP firmware specification.The
SNP_EXT_GUEST_REQUESTis similar toSNP_GUEST_REQUESTwith the difference of an additional certificate blob that can be passed through theSNP_SET_CONFIGioctl defined in the CCP driver. The CCP driver providessnp_guest_ext_guest_request()that is used by the KVM to get both the report and certificate data at once.Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- arch/x86/kvm/svm/sev.c | 185 +++++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/svm/svm.h | 2 + 2 files changed, 181 insertions(+), 6 deletions(-)arch/x86/kvm/svm/sev.c +87
-6@@ -327,6 +327,7 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)if (ret) goto e_free; +mutex_init (&sev->guest_req_lock); ret =sev_snp_init (&argp->error, false); }else { ret =sev_platform_init (&argp->error);@@ -2059,23 +2060,34 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)*/ static void *snp_context_create (struct kvm *kvm,struct kvm_sev_cmd *argp) { +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;struct sev_data_snp_addr data = {}; -void *context; +void *context, *certs_data;int rc; +/* Allocate memory used for the certs data in SNP guest request */ + certs_data =kzalloc (SEV_FW_BLOB_MAX_SIZE ,GFP_KERNEL_ACCOUNT ); +if (!certs_data) +return NULL ; +/* Allocate memory for context page */ context =snp_alloc_firmware_page (GFP_KERNEL_ACCOUNT );if (!context) -return NULL ; + goto e_free; data.gctx_paddr =__psp_pa (context); rc =__sev_issue_cmd (argp->sev_fd,SEV_CMD_SNP_GCTX_CREATE , &data, &argp->error); -if (rc) { -snp_free_firmware_page (context); -return NULL ; - } +if (rc) + goto e_free; + + sev->snp_certs_data = certs_data;return context; + +e_free: +snp_free_firmware_page (context); +kfree (certs_data); +return NULL ; }static int snp_bind_asid (struct kvm *kvm,int *error)@@ -2693,6 +2705,8 @@ static int snp_decommission_context(struct kvm *kvm)snp_free_firmware_page (sev->snp_context); sev->snp_context =NULL ; +kfree (sev->snp_certs_data); +return 0 ; }@@ -3153,6 +3167,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 ;@@ -3384,6 +3400,149 @@ static int snp_complete_psc(struct kvm_vcpu *vcpu)return 1 ; } +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);
arch/x86/kvm/svm/sev.c static int snp_complete_psc(struct kvm_vcpu *vcpu) · line 3477
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+if (rc) + goto unlock; + + rc =sev_issue_cmd (kvm,SEV_CMD_SNP_GUEST_REQUEST , &data, &err); +if (rc) +/* use the firmware error code */ + rc = err; + +snp_cleanup_guest_buf (&data, &rc); +
I am curious about the reason of having a shared-private and private-shared conversion before and after issuing the command to firmware.
Is it because the firmware requires the resp page has to be a private page? while the req page is not. (I understand that the req/resp page should be shared before returnning to guest due to GHCB spec)
Quoted 105 lines
arch/x86/kvm/svm/sev.c +82
-0+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 kvm_vcpu *vcpu = &svm->vcpu; +struct kvm *kvm = vcpu->kvm; +unsigned long data_npages; +struct kvm_sev_info *sev; +unsigned long rc, err; +u64 data_gpa; + +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 )) { + rc =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; + + rc =snp_guest_ext_guest_request (&req, (unsigned long )sev->snp_certs_data, + &data_npages, &err); +if (rc) { +/* +* If buffer length is small then return the expected +* length in rbx. +*/ +if (err ==SNP_GUEST_REQ_INVALID_LEN ) + vcpu->arch.regs[VCPU_REGS_RBX ] = data_npages; + +/* pass the firmware error code */ + rc = err; + goto cleanup; + } + +/* Copy the certificate blob in the guest memory */ +if (data_npages && +kvm_write_guest (kvm, data_gpa, sev->snp_certs_data, data_npages <<PAGE_SHIFT )) + rc =SEV_RET_INVALID_ADDRESS ; + +cleanup: +snp_cleanup_guest_buf (&req, &rc); + +unlock: +mutex_unlock (&sev->guest_req_lock); + +e_fail: +ghcb_set_sw_exit_info_2 (svm->sev_es.ghcb, rc); +} +static int sev_handle_vmgexit_msr_protocol (struct vcpu_svm *svm) {struct vmcb_control_area *control = &svm->vmcb->control;@@ -3633,6 +3792,20 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) vcpu->run->vmgexit.ghcb_msr = ghcb_gpa; vcpu->arch.complete_userspace_io = snp_complete_psc;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 : vcpu_unimpl(vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n",arch/x86/kvm/svm/svm.h +2
-0@@ -93,6 +93,8 @@ struct kvm_sev_info {atomic_t migration_in_progress;u64 snp_init_flags;void *snp_context;/* SNP guest context page */ +void *snp_certs_data; +struct mutex guest_req_lock;/* Lock for guest request handling */ };struct kvm_svm {
Re: [PATCH RFC v8 47/56] KVM: SVM: Support SEV-SNP AP Creation NAE event
In reply to Alexander GrafQuoted 158 lines · On Fri, 24 Feb 2023 13:37:48 +0100 Alexander Graf <[email protected]> wrote:
Quoted 36 lines · Michael Roth
From: 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, theKVM_REQ_UPDATE_PROTECTED_GUEST_STATEevent 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, theKVM_REQ_UPDATE_PROTECTED_GUEST_STATEevent is added to vCPU and then the vCPU is kicked.The
KVM_REQ_UPDATE_PROTECTED_GUEST_STATEevent handler will be invoked as a result of the event or as a result of an INIT. The handler sets the vCPU to theKVM_MP_STATE_UNINITIALIZEDstate, 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 toKVM_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 asKVM_MP_STATE_UNINITIALIZEDto prevent it from being run.Quoted 5 lines
Signed-off-by Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>[mdr: add handling for restrictedmem]Signed-off-by Michael Roth <[email protected]>What is the intended boot sequence for SEV-SNP guests? FWIW with this interface in place, guests will typically use in-guest VMSA pages to hold secondary vcpu state. But that means we're now allocating 4kb of memory for every vcpu that we create that will be for most of the guest's lifetime superfluous.
Wouldn't it make more sense to have a model where we only allocate the VMSA for the boot CPU and leave secondary allocation to the guest? We already need firmware changes for SEV-SNP - may as well make this one more.
[...]
Quoted 7 lines
arch/x86/kvm/svm/sev.c +7
-0+ +static int sev_snp_ap_creation (struct vcpu_svm *svm) +{ +struct kvm_sev_info *sev = &to_kvm_svm (svm->vcpu.kvm)->sev_info; +struct kvm_vcpu *vcpu = &svm->vcpu; +struct kvm_vcpu *target_vcpu; +struct vcpu_svm *target_svm;arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3688
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+unsigned int request; +unsigned int apic_id; +bool kick; +int ret; + + request =lower_32_bits (svm->vmcb->control.exit_info_1); + apic_id =upper_32_bits (svm->vmcb->control.exit_info_1); + +/* Validate the APIC ID */ + target_vcpu =kvm_get_vcpu_by_id (vcpu->kvm, apic_id);Out of curiosity: The target CPU can be my own vCPU, right?
Quoted 44 lines
arch/x86/kvm/svm/sev.c +44
-0+if (!target_vcpu) { +vcpu_unimpl (vcpu,"vmgexit: invalid AP APIC ID [%#x] from guest\n" , + apic_id); +return -EINVAL ; + } + + ret =0 ; + + target_svm =to_svm (target_vcpu); + +/* +* The target vCPU is valid, so the vCPU will be kicked unless the +* request is for CREATE_ON_INIT. For any errors at this stage, the +* kick will place the vCPU in an non-runnable state. +*/ + kick = true; + +mutex_lock (&target_svm->sev_es.snp_vmsa_mutex); + + target_svm->sev_es.snp_vmsa_gpa =INVALID_PAGE ; + target_svm->sev_es.snp_ap_create = true; + +/* Interrupt injection mode shouldn't change for AP creation */ +if (request <SVM_VMGEXIT_AP_DESTROY ) { +u64 sev_features; + + sev_features = vcpu->arch.regs[VCPU_REGS_RAX ]; + sev_features ^= sev->sev_features; +if (sev_features &SVM_SEV_FEAT_INT_INJ_MODES ) { +vcpu_unimpl (vcpu,"vmgexit: invalid AP injection mode [%#lx] from guest\n" , + vcpu->arch.regs[VCPU_REGS_RAX ]); + ret = -EINVAL ; + goto out; + } + } + +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);arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3742
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ ret = -EINVAL ; + goto out; + } + +/* +* 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. This will break genuine current Linux kernels that just happen to allocate a guest page, no? In fact, given enough vCPUs you're almost guaranteed to hit an aligned structure somewhere. What is the guest supposed to do in that situation?
arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3754
Quoted 12 lines
arch/x86/kvm/svm/sev.c +12
-0+*/ +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); + ret = -EINVAL ; + goto out; + } + + target_svm->sev_es.snp_vmsa_gpa = svm->vmcb->control.exit_info_2; +break ; +case SVM_VMGEXIT_AP_DESTROY :I don't understand the destroy path. Why does this case destroy anything?
Quoted 16 lines
arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3766
Quoted 12 lines
arch/x86/kvm/svm/sev.c +12
-0+break ; +default : +vcpu_unimpl (vcpu,"vmgexit: invalid AP creation request [%#x] from guest\n" , + request); + ret = -EINVAL ; +break ; + } + +out: +if (kick) { +if (target_vcpu->arch.mp_state ==KVM_MP_STATE_UNINITIALIZED ) + target_vcpu->arch.mp_state =KVM_MP_STATE_RUNNABLE ;What if the guest AP goes through a create -> destroy -> create cycle? Will it stay runnable while destroyed?
The code is not very straightforward.
1) target_svm->sev_es.snp_vmsa_gpa is set as INVALID_PAGE in the beginning of this function.
2) If a DESTROY is hit in this function, target_svm->sev_es.snp_vmsa_gpa will be left as INVALID_PAGE.
3) At the end of this function, it calls kvm_make_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE).
4) In the vcpu_enter_guest(), the kvm_vcpu_reset()->sev_snp_init_protected_guest_state() ->__sev_snp_init_protected_guest_state() is called.
5) The mp_state is set to KVM_MP_STATE_STOPPED by default and the runtime VMSA is cleared. Then the it will be initialized according to the guest's configuration.
6) As the snp_vmsa_gpa is set as INVALID_PAGE in 1, the mp_state will be left as KVM_MP_STATE_STOPPED.
7) With this code piece:
+ kvm_vcpu_reset(vcpu, true);
+ if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
+ goto out;
vcpu_enter_guest() bails out.
Quoted 29 lines
Alex
arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3779
Quoted 13 lines
arch/x86/kvm/svm/sev.c +10
-0+ +kvm_make_request (KVM_REQ_UPDATE_PROTECTED_GUEST_STATE , target_vcpu); +kvm_vcpu_kick (target_vcpu); + } + +mutex_unlock (&target_svm->sev_es.snp_vmsa_mutex); + +return ret; +} +static int sev_handle_vmgexit_msr_protocol (struct vcpu_svm *svm) {struct vmcb_control_area *control = &svm->vmcb->control;Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
Re: [PATCH RFC v8 01/56] KVM: x86: Add 'fault_is_private' x86 op
In reply to Michael RothOn Mon, 20 Feb 2023 12:37:52 -0600
Michael Roth <[email protected]> wrote:
Basically, I don't think kvm_mmu_fault_is_private() is promising after going through both SNP and TDX patches:
1) Fault path is critical. kvm_mmu_fault_is_private() is always doing a gfn_to _memslot() no matter SNP/TDX is enabled or not. It might mostly hits the slots->last_used_slot, but the worst case is going through an RB-tree search.
Adding extra overhead on the generic fault path needs to be re-considered carefully. At least, check if the guest is a CC(SNP/TDX) guest.
2) Just after the gfn_to_memslot() in kvm_mmu_fault_is_private(), there is another gfn_to_memslot():
static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
u64 err, bool prefetch)
{
struct kvm_page_fault fault = {
.addr = cr2_or_gpa,
.error_code = lower_32_bits(err),
.exec = err & PFERR_FETCH_MASK,
.write = err & PFERR_WRITE_MASK,
.present = err & PFERR_PRESENT_MASK,
.rsvd = err & PFERR_RSVD_MASK,
.user = err & PFERR_USER_MASK,
.prefetch = prefetch,
.is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault),
.nx_huge_page_workaround_enabled =
is_nx_huge_page_enabled(vcpu->kvm),
.max_level = KVM_MAX_HUGEPAGE_LEVEL,
.req_level = PG_LEVEL_4K,
.goal_level = PG_LEVEL_4K,
.is_private = kvm_mmu_fault_is_private(vcpu->kvm, cr2_or_gpa, err),
};
int r;
if (vcpu->arch.mmu->root_role.direct) {
fault.gfn = fault.addr >> PAGE_SHIFT;
/* here */
fault.slot = kvm_vcpu_gfn_to_memslot(vcpu, fault.gfn);
}I was thinking if checking the private slot and kvm_slot_can_be_private() is necessary in kvm_mmu_fault_is_private().
TDP MMU is expecting fault.is_private to indicate if CPU thinks the fault is private or not (For SNP, it is in PF error code, for TDX it is the shared bit in the fault GPA). TDP MMU will check if the slot is a private slot or not, leave the userspace to handle it when they thinks differently.
My points:
1) Resolving the PFER in kvm_x86_ops.fault_is_private and setting fault.is_private is enough. The rest can be handled by the TDP MMU.
2) Put the kvm_x86_ops.fault_is_private in a separate patch so that TDX series can include it. (64bit-error code part can stay in another patch)
Quoted 119 lines
This callback is used by the KVM MMU to check whether a #NPF was for a private GPA or not.
In some cases the full 64-bit error code for the #NPF 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.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/mmu/mmu.c | 3 +-- arch/x86/kvm/mmu/mmu_internal.h | 37 +++++++++++++++++++++++++++--- 4 files changed, 37 insertions(+), 5 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -131,6 +131,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(fault_is_private); #undef KVM_X86_OP #undef KVM_X86_OP_OPTIONALarch/x86/include/asm/kvm_host.h +1
-0@@ -1643,6 +1643,7 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level); +bool (*fault_is_private)(struct kvm *kvm,gpa_t gpa,u64 error_code,bool *private_fault);bool (*has_wbinvd_exit)(void );arch/x86/kvm/mmu/mmu.c +1
-2@@ -5724,8 +5724,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);if (KVM_BUG_ON (r ==RET_PF_INVALID , vcpu->kvm))return -EIO ; }arch/x86/kvm/mmu/mmu_internal.h +34
-3@@ -231,6 +231,37 @@ 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 (static_call (kvm_x86_fault_is_private)(kvm, gpa, err, &private_fault)) + 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(). @@ -262,11 +293,11 @@ enum { };static inline int kvm_mmu_do_page_fault (struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa, -u32 err,bool prefetch) +u64 err,bool prefetch) {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 ,@@ -280,7 +311,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;
Re: [PATCH RFC v8 50/56] KVM: SEV: Handle restricted memory invalidations for SNP
In reply to Michael RothQuoted 23 lines · On Mon, 20 Feb 2023 12:38:41 -0600 Michael Roth <[email protected]> wrote:
Implement a platform hook to do the work of restoring the direct map entries and cleaning up RMP table entries for restricted memory that is being freed back to the host.
Signed-off-by Michael Roth <[email protected]>--- arch/x86/kvm/svm/sev.c | 62 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 64 insertions(+)arch/x86/kvm/svm/sev.c +4
-0@@ -4509,3 +4509,65 @@ bool sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *privreturn true; } + +void sev_invalidate_private_range (struct kvm_memory_slot *slot,gfn_t start,gfn_t end) +{ +gfn_t gfn = start;
arch/x86/kvm/svm/sev.c bool sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *priv · line 4525
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ +if (!sev_snp_guest (slot->kvm)) +return ; + +if (!kvm_slot_can_be_private (slot)) { +pr_warn_ratelimited ("SEV: Memslot for GFN: 0x%llx is not private.\n" , + gfn); +return ; + } +
This is a generic check for both SNP and TDX, it should be moved to kvm_restrictedmem_invalidate_begin().
Quoted 72 lines
arch/x86/kvm/svm/sev.c +48
-0+while (gfn <= end) { +gpa_t gpa =gfn_to_gpa (gfn); +int level =PG_LEVEL_4K ; +int order, rc; +kvm_pfn_t pfn; + + rc =kvm_restrictedmem_get_pfn (slot, gfn, &pfn, &order); +if (rc) { +pr_warn_ratelimited ("SEV: Failed to retrieve restricted PFN for GFN 0x%llx, rc: %d\n" , + gfn, rc); + gfn++; +continue ; + } + +if (order) { +int rmp_level; + +if (IS_ALIGNED (gpa,page_level_size (PG_LEVEL_2M )) && + gpa +page_level_size (PG_LEVEL_2M ) <=gfn_to_gpa (end)) + level =PG_LEVEL_2M ; +else +pr_debug ("%s: GPA 0x%llx is not aligned to 2M, skipping 2M directmap restoration\n" , + __func__, gpa); + +/* +* TODO: It may still be possible to restore 2M mapping here, +* but keep it simple for now. +*/ +if (level ==PG_LEVEL_2M && + (!snp_lookup_rmpentry (pfn, &rmp_level) || rmp_level ==PG_LEVEL_4K )) { +pr_debug ("%s: PFN 0x%llx is not mapped as 2M private range, skipping 2M directmap restoration\n" , + __func__, pfn); + level =PG_LEVEL_4K ; + } + } + +pr_debug ("%s: GPA %llx PFN %llx order %d level %d\n" , + __func__, gpa, pfn, order, level); + rc =snp_make_page_shared (slot->kvm, gpa, pfn, level); +if (rc) +pr_err ("SEV: Failed to restore page to shared, GPA: 0x%llx PFN: 0x%llx order: %d rc: %d\n" , + gpa, pfn, order, rc); + + gfn +=page_level_size (level) >>PAGE_SHIFT ; +put_page (pfn_to_page (pfn)); +cond_resched (); + } +}arch/x86/kvm/svm/svm.c +1
-0@@ -4862,6 +4862,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .adjust_mapping_level = sev_adjust_mapping_level, .update_mem_attr = sev_update_mem_attr, .fault_is_private = sev_fault_is_private, + .invalidate_restricted_mem = sev_invalidate_private_range, };/* arch/x86/kvm/svm/svm.h +1
-0@@ -727,6 +727,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_update_mem_attr (struct kvm_memory_slot *slot,unsigned int attr,gfn_t start,gfn_t end); +void sev_invalidate_private_range (struct kvm_memory_slot *slot,gfn_t start,gfn_t end);bool sev_fault_is_private (struct kvm *kvm,gpa_t gpa,u64 error_code,bool *private_fault);
Re: [PATCH RFC v8 51/56] KVM: SVM: Add module parameter to enable the SEV-SNP
In reply to Michael RothQuoted 19 lines · On Mon, 20 Feb 2023 12:38:42 -0600 Michael Roth <[email protected]> wrote:
From: 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.
Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Michael Roth <[email protected]>--- arch/x86/kvm/svm/sev.c | 7 ++++--- arch/x86/kvm/svm/svm.h | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-)arch/x86/kvm/svm/sev.c +0
-0
arch/x86/kvm/svm/sev.c module_param_named(sev, sev_enabled, bool, 0444); · line 64
Quoted 10 lines
arch/x86/kvm/svm/sev.c +4
-0@@ -55,14 +55,15 @@ 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
Guess we also need #define sev_snp_enabled false.
Quoted 13 lines
arch/x86/kvm/svm/sev.c +0
-3#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 2arch/x86/kvm/svm/svm.h +0
-0
arch/x86/kvm/svm/svm.h void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu); · line 697
Quoted 5 lines
arch/x86/kvm/svm/svm.h +1
-1@@ -694,7 +694,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 0 +#define GHCB_HV_FT_SUPPORTED (GHCB_HV_FT_SNP | GHCB_HV_FT_SNP_AP_CREATION)
This is not related to the topic of this patch, should be merged into related patches.
Quoted 3 lines
arch/x86/kvm/svm/svm.h +0
-0extern unsigned int max_sev_asid;
Re: [PATCH RFC v8 15/56] x86/sev: Invalidate pages from the direct map when adding them to the RMP table
In reply to Michael RothQuoted 48 lines · Michael Roth
From: 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()andrmp_make_shared()helpers are used to add or remove the pages from the RMP table. Improve thermp_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.Co-developed-by Ashish Kalra <[email protected]>Signed-off-by Ashish Kalra <[email protected]>Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Michael Roth <[email protected]>--- arch/x86/kernel/sev.c | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+)arch/x86/kernel/sev.c +18
-0@@ -2595,6 +2595,37 @@ 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) + goto cleanup; + } + +cleanup: +WARN (ret >0 ,"Failed to restore direct map for pfn 0x%llx\n" , pfn + i); +return ret; +} + +static int invalidate_direct_map (u64 pfn,int npages) +{ +int i, ret =0 ;
arch/x86/kernel/sev.c int psmash(u64 pfn) · line 2625
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+ +for (i =0 ; i < npages; i++) { + ret =set_direct_map_invalid_noflush (pfn_to_page (pfn + i)); +if (ret) + goto cleanup; + } + +cleanup: +WARN (ret >0 ,"Failed to invalidate direct map for pfn 0x%llx\n" , pfn + i); +restore_direct_map (pfn, i);
This immediately restores the direct map after invalidating it. It probably needs to put behind if(ret).
Regards, Tom
Quoted 49 lines
arch/x86/kernel/sev.c +29
-1+return ret; +} +static int rmpupdate (u64 pfn,struct rmp_state *val) {int max_attempts =4 *num_present_cpus ();@@ -2605,6 +2636,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"@@ -2630,6 +2676,17 @@ static int rmpupdate(u64 pfn, struct rmp_state *val) attempts, val->asid, ret, pfn, npages); } +/* +* 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 ; } --2.25.1
Re: [PATCH RFC v8 15/56] x86/sev: Invalidate pages from the direct map when adding them to the RMP table
In reply to Michael RothQuoted 8 lines · Michael Roth
From: 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.
Quoted 5 lines
The
rmp_make_private()andrmp_make_shared()helpers are used to add or remove the pages from the RMP table. Improve thermp_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.
This is a purely "what" changelog. It doesn't explain the "why" at all.
Could you please elaborate on why this unmapping operation is necessary?
Re: [PATCH RFC v8 17/56] x86/fault: Add support to handle the RMP fault for user address
In reply to Michael RothQuoted 10 lines · Michael Roth
mm/memory.c +6
-0+static int handle_split_page_fault (struct vm_fault *vmf) +{ +__split_huge_pmd (vmf->vma, vmf->pmd, vmf->address, false,NULL ); +return 0 ; +} +/* * By the time we get here, we already hold the mm semaphore *
mm/memory.c static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, · line 5089
Quoted 6 lines
mm/memory.c +3
-0@@ -5078,6 +5084,10 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,pmd_migration_entry_wait (mm, vmf.pmd);return 0 ; } + +if (flags &FAULT_FLAG_PAGE_SPLIT ) +return handle_split_page_fault (&vmf);
I asked this long ago, but how do you prevent these faults from occurring on hugetlbfs mappings that can't be split?
Quoted 7 lines · Michael Roth
The RMP check is enforced as soon as SEV-SNP is enabled. Not every memory access requires an RMP check. In particular, the read accesses from the hypervisor do not require RMP checks because the data confidentiality is already protected via memory encryption. When hardware encounters an RMP checks failure, it raises a page-fault exception. If RMP check failure is due to the page-size mismatch, then split the large page to resolve the fault.
What does this all _mean_?
When does the kernel need to care about a "page-size mismatch"?
Re: [PATCH RFC v8 47/56] KVM: SVM: Support SEV-SNP AP Creation NAE event
In reply to Zhi WangQuoted 196 lines · Zhi Wang
Quoted 153 lines · On Fri, 24 Feb 2023 13:37:48 +0100 Alexander Graf <[email protected]> wrote:
Quoted 36 lines · Michael Roth
From: 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, theKVM_REQ_UPDATE_PROTECTED_GUEST_STATEevent 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, theKVM_REQ_UPDATE_PROTECTED_GUEST_STATEevent is added to vCPU and then the vCPU is kicked.The
KVM_REQ_UPDATE_PROTECTED_GUEST_STATEevent handler will be invoked as a result of the event or as a result of an INIT. The handler sets the vCPU to theKVM_MP_STATE_UNINITIALIZEDstate, 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 toKVM_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 asKVM_MP_STATE_UNINITIALIZEDto prevent it from being run.Quoted 5 lines
Signed-off-by Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>[mdr: add handling for restrictedmem]Signed-off-by Michael Roth <[email protected]>What is the intended boot sequence for SEV-SNP guests? FWIW with this interface in place, guests will typically use in-guest VMSA pages to hold secondary vcpu state. But that means we're now allocating 4kb of memory for every vcpu that we create that will be for most of the guest's lifetime superfluous.
Wouldn't it make more sense to have a model where we only allocate the VMSA for the boot CPU and leave secondary allocation to the guest? We already need firmware changes for SEV-SNP - may as well make this one more.
[...]
Quoted 7 lines
arch/x86/kvm/svm/sev.c +7
-0+ +static int sev_snp_ap_creation (struct vcpu_svm *svm) +{ +struct kvm_sev_info *sev = &to_kvm_svm (svm->vcpu.kvm)->sev_info; +struct kvm_vcpu *vcpu = &svm->vcpu; +struct kvm_vcpu *target_vcpu; +struct vcpu_svm *target_svm;arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3688
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+unsigned int request; +unsigned int apic_id; +bool kick; +int ret; + + request =lower_32_bits (svm->vmcb->control.exit_info_1); + apic_id =upper_32_bits (svm->vmcb->control.exit_info_1); + +/* Validate the APIC ID */ + target_vcpu =kvm_get_vcpu_by_id (vcpu->kvm, apic_id);Out of curiosity: The target CPU can be my own vCPU, right?
Quoted 44 lines
arch/x86/kvm/svm/sev.c +44
-0+if (!target_vcpu) { +vcpu_unimpl (vcpu,"vmgexit: invalid AP APIC ID [%#x] from guest\n" , + apic_id); +return -EINVAL ; + } + + ret =0 ; + + target_svm =to_svm (target_vcpu); + +/* +* The target vCPU is valid, so the vCPU will be kicked unless the +* request is for CREATE_ON_INIT. For any errors at this stage, the +* kick will place the vCPU in an non-runnable state. +*/ + kick = true; + +mutex_lock (&target_svm->sev_es.snp_vmsa_mutex); + + target_svm->sev_es.snp_vmsa_gpa =INVALID_PAGE ; + target_svm->sev_es.snp_ap_create = true; + +/* Interrupt injection mode shouldn't change for AP creation */ +if (request <SVM_VMGEXIT_AP_DESTROY ) { +u64 sev_features; + + sev_features = vcpu->arch.regs[VCPU_REGS_RAX ]; + sev_features ^= sev->sev_features; +if (sev_features &SVM_SEV_FEAT_INT_INJ_MODES ) { +vcpu_unimpl (vcpu,"vmgexit: invalid AP injection mode [%#lx] from guest\n" , + vcpu->arch.regs[VCPU_REGS_RAX ]); + ret = -EINVAL ; + goto out; + } + } + +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);arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3742
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ ret = -EINVAL ; + goto out; + } + +/* +* 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. This will break genuine current Linux kernels that just happen to allocate a guest page, no? In fact, given enough vCPUs you're almost guaranteed to hit an aligned structure somewhere. What is the guest supposed to do in that situation?
arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3754
Quoted 12 lines
arch/x86/kvm/svm/sev.c +12
-0+*/ +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); + ret = -EINVAL ; + goto out; + } + + target_svm->sev_es.snp_vmsa_gpa = svm->vmcb->control.exit_info_2; +break ; +case SVM_VMGEXIT_AP_DESTROY :I don't understand the destroy path. Why does this case destroy anything?
Quoted 15 lines
arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3766
Quoted 12 lines
arch/x86/kvm/svm/sev.c +12
-0+break ; +default : +vcpu_unimpl (vcpu,"vmgexit: invalid AP creation request [%#x] from guest\n" , + request); + ret = -EINVAL ; +break ; + } + +out: +if (kick) { +if (target_vcpu->arch.mp_state ==KVM_MP_STATE_UNINITIALIZED ) + target_vcpu->arch.mp_state =KVM_MP_STATE_RUNNABLE ;What if the guest AP goes through a create -> destroy -> create cycle? Will it stay runnable while destroyed?
The code is not very straightforward.
1)
target_svm->sev_es.snp_vmsa_gpais set asINVALID_PAGEin the beginning of this function.2) If a DESTROY is hit in this function,
target_svm->sev_es.snp_vmsa_gpawill be left asINVALID_PAGE.3) At the end of this function, it calls
kvm_make_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE).4) In the
vcpu_enter_guest(), the kvm_vcpu_reset()->sev_snp_init_protected_guest_state() ->__sev_snp_init_protected_guest_state() is called.5) The
mp_stateis set toKVM_MP_STATE_STOPPEDby default and the runtime VMSA is cleared. Then the it will be initialized according to the guest's configuration.6) As the
snp_vmsa_gpais set asINVALID_PAGEin 1, themp_statewill be left asKVM_MP_STATE_STOPPED.7) With this code piece:
arch/x86/kvm/x86.c +3
-0+kvm_vcpu_reset (vcpu, true); +if (vcpu->arch.mp_state !=KVM_MP_STATE_RUNNABLE ) + goto out;
Quoted 1 line
vcpu_enter_guest()bails out.
Thanks a lot Zhi for the detailed explanation! I think this code flow wants to become slightly more obvious. For example, if we just said
case SVM_VMGEXIT_AP_DESTROY:
/* This will tell __sev_snp_update_protected_guest_state to unmap the VMSA */ target_svm->sev_es.snp_vmsa_gpa = INVALID_PAGE;
break;
We'd get a big win in readability with little effort. It makes it immediately obvious where to look for the destroy operation.
Alex
Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879