Add AMD Secure Nested Paging (SEV-SNP) Hypervisor Support
Subsystems (12)
v5 cover Add AMD Secure Nested Paging (SEV-SNP) Hypervisor Support
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 Part-1.
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.
The series is based on the commit:
SNP part1 commit and
fa7a549d321a (kvm/next, next) KVM: x86: accept userspace interrupt only if no event is injected
TODO:
* Add support for command to ratelimit the guest message request.
Changes since v4:
* Move the RMP entry definition to x86 specific header file.
* Move the dump RMP entry function to SEV specific file.
* Use BIT_ULL while defining the #PF bit fields.
* Add helper function to check the IOMMU support for SEV-SNP feature.
* Add helper functions for the page state transition.
* Map and unmap the pages from the direct map after page is added or removed in RMP table.
* Enforce the minimum SEV-SNP firmware version.
* Extend the LAUNCH_UPDATE to accept the base_gfn and remove the logic to calculate the gfn from the hva.
* Add a check in LAUNCH_UPDATE to ensure that all the pages are shared before calling the PSP.
* Mark the memory failure when failing to remove the page from the RMP table or clearing the immutable bit.
* Exclude the encrypted hva range from the KSM.
* Remove the gfn tracking during the kvm_gfn_map() and use SRCU to syncronize the PSC and gfn mapping.
* Allow PSC on the registered hva range only.
* Add support for the Preferred GPA VMGEXIT.
* Simplify the PSC handling routines.
* Use the static_call() for the newly added kvm_x86_ops.
* Remove the long-lived GHCB map.
* Move the snp enable module parameter to the end of the file.
* Remove the kvm_x86_op for the RMP fault handling. Call the fault handler directly from the #NPF interception.
Changes since v3:
* Add support for extended guest message request.
* Add ioctl to query the SNP Platform status.
* Add ioctl to get and set the SNP config.
* Add check to verify that memory reserved for the RMP covers the full system RAM.
* Start the SNP specific commands from 256 instead of 255.
* Multiple cleanup and fixes based on the review feedback.
Changes since v2:
* Add AP creation support.
* Drop the patch to handle the RMP fault for the kernel address.
* Add functions to track the write access from the hypervisor.
* Do not enable the SNP feature when IOMMU is disabled or is in passthrough mode.
* Dump the RMP entry on RMP violation for the debug.
* Shorten the GHCB macro names.
* Start the SNP_INIT command id from 255 to give some gap for the legacy SEV.
* Sync the header with the latest 0.9 SNP spec.
Changes since v1:
* Add AP reset MSR protocol VMGEXIT NAE.
* Add Hypervisor features VMGEXIT NAE.
* Move the RMP table initialization and RMPUPDATE/PSMASH helper in arch/x86/kernel/sev.c.
* Add support to map/unmap SEV legacy command buffer to firmware state when SNP is active.
* Enhance PSP driver to provide helper to allocate/free memory used for the firmware context page.
* Add support to handle RMP fault for the kernel address.
* Add support to handle GUEST_REQUEST NAE event for attestation.
* Rename RMP table lookup helper.
* Drop typedef from rmpentry struct definition.
* Drop SNP static key and use cpu_feature_enabled() to check whether SEV-SNP is active.
* Multiple cleanup/fixes to address Boris review feedback.
Brijesh Singh (40):
x86/cpufeatures: Add SEV-SNP CPU feature
iommu/amd: Introduce function to check SEV-SNP support 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: Invalid pages from direct map when adding it to RMP table x86/traps: Define RMP violation #PF error code
x86/fault: Add support to handle the RMP fault for user address x86/fault: Add support to dump RMP entry on fault
crypto: ccp: shutdown SEV firmware on kexec
crypto:ccp: Define the SEV-SNP commands
crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP crypto:ccp: Provide APIs to issue SEV-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: Mark the private vma unmerable for SEV-SNP guests
KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command
KVM: X86: Keep the NPT and RMP page level in sync
KVM: x86: Introduce kvm_mmu_get_tdp_walk() for SEV-SNP use
KVM: x86: Define RMP page fault error bits for #NPF
KVM: x86: Update page-fault trace to log full 64-bit error code
KVM: SVM: Do not use long-lived GHCB map while setting scratch area
KVM: SVM: Remove the long-lived GHCB host map
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: SVM: Introduce ops for the post gfn map and unmap
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
Sean Christopherson (2):
KVM: x86/mmu: Move 'pfn' variable to caller of direct_page_fault()
KVM: x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by TDX and SNP
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
Documentation/virt/coco/sevguest.rst | 55 + .../virt/kvm/amd-memory-encryption.rst | 102 + 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 | 20 + arch/x86/include/asm/msr-index.h | 6 + arch/x86/include/asm/sev-common.h | 28 + arch/x86/include/asm/sev.h | 45 + arch/x86/include/asm/svm.h | 7 + arch/x86/include/asm/trap_pf.h | 18 +- arch/x86/kernel/cpu/amd.c | 3 +- arch/x86/kernel/sev.c | 361 ++++ arch/x86/kvm/lapic.c | 5 +- arch/x86/kvm/mmu.h | 7 +- arch/x86/kvm/mmu/mmu.c | 84 +- arch/x86/kvm/svm/sev.c | 1676 ++++++++++++++++- arch/x86/kvm/svm/svm.c | 62 +- arch/x86/kvm/svm/svm.h | 74 +- arch/x86/kvm/trace.h | 40 +- arch/x86/kvm/x86.c | 92 +- arch/x86/mm/fault.c | 84 +- drivers/crypto/ccp/sev-dev.c | 924 ++++++++- drivers/crypto/ccp/sev-dev.h | 17 + drivers/crypto/ccp/sp-pci.c | 12 + drivers/iommu/amd/init.c | 30 + include/linux/iommu.h | 9 + include/linux/mm.h | 6 +- include/linux/psp-sev.h | 346 ++++ include/linux/sev.h | 32 + include/uapi/linux/kvm.h | 56 + include/uapi/linux/psp-sev.h | 60 + mm/memory.c | 13 + tools/arch/x86/include/asm/cpufeatures.h | 1 + 34 files changed, 4088 insertions(+), 201 deletions(-) create mode 100644 include/linux/sev.h
Signature · 1 line
2.17.1
PATCH v5 1/45 x86/cpufeatures: Add SEV-SNP CPU feature
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 | 3 ++- tools/arch/x86/include/asm/cpufeatures.h | 1 + 3 files changed, 4 insertions(+), 1 deletion(-)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -398,6 +398,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_SME_COHERENT (19*32+10)/* "" AMD hardware-enforced cache coherency */ /*
arch/x86/kernel/cpu/amd.c +2 -1
@@ -586,7 +586,7 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c)* If BIOS has not enabled SME then don't advertise the * SME feature (set in scattered.c). * For SEV: If BIOS has not enabled SEV then don't advertise the -* SEV and SEV_ES feature (set in scattered.c). +* SEV, SEV_ES and SEV_SNP feature. * * In all cases, since support for SME and SEV requires long mode, * don't advertise the feature under CONFIG_X86_32.
@@ -618,6 +618,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
@@ -398,6 +398,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_SME_COHERENT (19*32+10)/* "" AMD hardware-enforced cache coherency */ /*
Signature · 1 line
2.17.1
PATCH v5 2/45 iommu/amd: Introduce function to check SEV-SNP support
In reply to Brijesh SinghThe SEV-SNP support requires that IOMMU must to enabled, see the IOMMU spec section 2.12 for further details. If IOMMU is not enabled or the SNPSup extended feature register is not set then the SNP_INIT command (used for initializing firmware) will fail.
The iommu_sev_snp_supported() can be used to check if IOMMU supports the SEV-SNP feature.
--- drivers/iommu/amd/init.c | 30 ++++++++++++++++++++++++++++++ include/linux/iommu.h | 9 +++++++++ 2 files changed, 39 insertions(+)
drivers/iommu/amd/init.c +30 -0
@@ -3320,3 +3320,33 @@ int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64return iommu_pc_get_set_reg (iommu, bank, cntr, fxn, value, true); } + +bool iommu_sev_snp_supported (void ) +{ +struct amd_iommu *iommu; + +/* +* The SEV-SNP support requires that IOMMU must be enabled, and is +* not configured in the passthrough mode. +*/ +if (no_iommu ||iommu_default_passthrough ()) { +pr_err ("SEV-SNP: IOMMU is either disabled or configured in passthrough mode.\n" ); +return false; + } + +/* +* Iterate through all the IOMMUs and verify the SNPSup feature is +* enabled. +*/ +for_each_iommu (iommu) { +if (!iommu_feature (iommu,FEATURE_SNP )) { +pr_err ("SNPSup is disabled (devid: %02x:%02x.%x)\n" , +PCI_BUS_NUM (iommu->devid),PCI_SLOT (iommu->devid), +PCI_FUNC (iommu->devid)); +return false; + } + } + +return true; +} +EXPORT_SYMBOL_GPL (iommu_sev_snp_supported);
include/linux/iommu.h +9 -0
@@ -604,6 +604,12 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev,void iommu_sva_unbind_device (struct iommu_sva *handle);u32 iommu_sva_get_pasid (struct iommu_sva *handle); +#ifdef CONFIG_AMD_MEM_ENCRYPT +bool iommu_sev_snp_supported (void ); +#else +static inline bool iommu_sev_snp_supported (void ) {return false; } +#endif +#else /* CONFIG_IOMMU_API */ struct iommu_ops {};
@@ -999,6 +1005,9 @@ static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev) {return NULL ; } + +static inline bool iommu_sev_snp_supported (void ) {return false; } +#endif /* CONFIG_IOMMU_API */ /**
Signature · 1 line
2.17.1
PATCH v5 3/45 x86/sev: Add the host SEV-SNP initialization support
In reply to Brijesh SinghThe 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.
--- arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/msr-index.h | 6 + arch/x86/kernel/sev.c | 144 +++++++++++++++++++++++ 3 files changed, 157 insertions(+), 1 deletion(-)
arch/x86/include/asm/disabled-features.h +7 -1
@@ -65,6 +65,12 @@# define DISABLE_SGX (1 << (X86_FEATURE_SGX & 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 */
@@ -88,7 +94,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 +6 -0
@@ -485,6 +485,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
@@ -542,6 +544,10 @@#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_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_K8_INT_PENDING_MSG 0xc0010055/* C1E active bits in int pending message */ #define K8_INTP_C1E_ACTIVE_MASK 0x18000000
arch/x86/kernel/sev.c +144 -0
170 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v5 4/45 x86/sev: Add RMP entry lookup helpers
In reply to Brijesh SinghThe 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 | 27 ++++++++++++++++++++++++ arch/x86/kernel/sev.c | 43 ++++++++++++++++++++++++++++++++++++++ include/linux/sev.h | 30 ++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 include/linux/sev.h
arch/x86/include/asm/sev.h +27 -0
@@ -9,6 +9,7 @@#define __ASM_ENCRYPTED_STATE_H#include <linux/types.h> +#include <linux/sev.h> #include <asm/insn.h> #include <asm/sev-common.h> #include <asm/bootparam.h>
@@ -77,6 +78,32 @@ 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) + +/* +* The RMP entry format is not architectural. The format is defined in PPR +* Family 19h Model 01h, Rev B1 processor. +*/ +struct __packed rmpentry { +union { +struct { +u64 assigned :1 , + pagesize :1 , + immutable :1 , + rsvd1 :9 , + gpa :39 , + asid :10 , + vmsa :1 , + validated :1 , + rsvd2 :1 ; + } info; +u64 low; + }; +u64 high; +}; + +#define rmpentry_assigned (x) ((x)->info.assigned) +#define rmpentry_pagesize (x) ((x)->info.pagesize)#define RMPADJUST_VMSA_PAGE_BIT BIT(16)
arch/x86/kernel/sev.c +43 -0
52 lines not on this page · raw message
include/linux/sev.h new file +30 -0
@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* AMD Secure Encrypted Virtualization +* +* Author: Brijesh Singh <[email protected]> +*/ + +#ifndef __LINUX_SEV_H +#define __LINUX_SEV_H + +/* RMUPDATE detected 4K page and 2MB page overlap. */ +#define RMPUPDATE_FAIL_OVERLAP 7 + +#ifdef CONFIG_AMD_MEM_ENCRYPT +int snp_lookup_rmpentry (u64 pfn,int *level); +int psmash (u64 pfn); +int rmp_make_private (u64 pfn,u64 gpa,enum pg_level level,int asid,bool immutable); +int rmp_make_shared (u64 pfn,enum pg_level level); +#else +static inline int snp_lookup_rmpentry (u64 pfn,int *level) {return 0 ; } +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 /* CONFIG_AMD_MEM_ENCRYPT */ +#endif /* __LINUX_SEV_H */
Signature · 1 line
2.17.1
PATCH v5 5/45 x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
In reply to Brijesh SinghThe 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.
--- arch/x86/include/asm/sev.h | 11 ++++++ arch/x86/kernel/sev.c | 72 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+)
arch/x86/include/asm/sev.h +11 -0
@@ -78,7 +78,9 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs);/* 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)/* * The RMP entry format is not architectural. The format is defined in PPR
@@ -107,6 +109,15 @@ struct __packed rmpentry {#define RMPADJUST_VMSA_PAGE_BIT BIT(16) +struct rmpupdate { +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);
arch/x86/kernel/sev.c +72 -0
75 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v5 6/45 x86/sev: Invalid pages from direct map when adding it to RMP table
In reply to Brijesh SinghThe 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 checks 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 invalid state so that pages cannot be used in the direct-map after its added in the RMP table, and restore to its default valid permission after the pages are removed from the RMP table.
--- arch/x86/kernel/sev.c | 61 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-)
arch/x86/kernel/sev.c +60 -1
@@ -2441,10 +2441,42 @@ int psmash(u64 pfn) }EXPORT_SYMBOL_GPL (psmash); +static int restore_direct_map (u64 pfn,int npages) +{ +int i, ret =0 ; + +for (i =0 ; i < npages; i++) { + ret =set_direct_map_default_noflush (pfn_to_page (pfn + i)); +if (ret) + goto cleanup; + } + +cleanup: +WARN (ret >0 ,"Failed to restore direct map for pfn 0x%llx\n" , pfn + i); +return ret; +} + +static int invalid_direct_map (unsigned long 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; + } + +return 0 ; + +cleanup: +restore_direct_map (pfn, i); +return ret; +} +static int rmpupdate (u64 pfn,struct rmpupdate *val) {unsigned long paddr = pfn <<PAGE_SHIFT ; -int ret; +int ret, level, npages;if (!pfn_valid (pfn))return -EINVAL ;
@@ -2452,11 +2484,38 @@ static int rmpupdate(u64 pfn, struct rmpupdate *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 (invalid_direct_map (pfn, npages)) { +pr_err ("Failed to unmap pfn 0x%llx pages %d from direct_map\n" , + pfn, npages); +return -EFAULT ; + } + } +/* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ asmvolatile (".byte 0xF2, 0x0F, 0x01, 0xFE" :"=a" (ret) :"a" (paddr),"c" ((unsigned long )val) :"memory" ,"cc" ); + +/* +* Restore the direct map after the page is removed from the RMP table. +*/ +if (!ret && !val->assigned) { +if (restore_direct_map (pfn, npages)) { +pr_err ("Failed to map pfn 0x%llx pages %d in direct_map\n" , + pfn, npages); +return -EFAULT ; + } + } +return ret; }
Signature · 1 line
2.17.1
PATCH v5 7/45 x86/traps: Define RMP violation #PF error code
In reply to Brijesh SinghBit 31 in the page fault-error bit will be set when processor encounters an RMP violation.
While at it, use the BIT_ULL() macro.
--- 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_ULL (0 ), +X86_PF_WRITE =BIT_ULL (1 ), +X86_PF_USER =BIT_ULL (2 ), +X86_PF_RSVD =BIT_ULL (3 ), +X86_PF_INSTR =BIT_ULL (4 ), +X86_PF_PK =BIT_ULL (5 ), +X86_PF_SGX =BIT_ULL (15 ), +X86_PF_RMP =BIT_ULL (31 ), };#endif /* _ASM_X86_TRAP_PF_H */
arch/x86/mm/fault.c +1 -0
@@ -545,6 +545,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.17.1
PATCH v5 8/45 x86/fault: Add support to handle the RMP fault for user address
In reply to Brijesh SinghWhen 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 | 66 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/mm.h | 6 ++++- mm/memory.c | 13 +++++++++ 3 files changed, 84 insertions(+), 1 deletion(-)
arch/x86/mm/fault.c +66 -0
@@ -19,6 +19,7 @@#include <linux/uaccess.h> /* faulthandler_disabled() */ #include <linux/efi.h> /* efi_crash_gracefully_on_page_fault()*/ #include <linux/mm_types.h> +#include <linux/sev.h> /* snp_lookup_rmpentry() */ #include <asm/cpufeature.h> /* boot_cpu_has, ... */ #include <asm/traps.h> /* dotraplinkage, ... */
@@ -1202,6 +1203,60 @@ do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, }NOKPROBE_SYMBOL (do_kern_addr_fault); +static inline size_t pages_per_hpage (int level) +{ +return page_level_size (level) /PAGE_SIZE ; +} + +/* +* Return 1 if the caller need to retry, 0 if it the address need to be split +* in order to resolve the fault. +*/ +static int handle_user_rmp_page_fault (struct pt_regs *regs,unsigned long error_code, +unsigned long address) +{ +int rmp_level, level; +pte_t *pte; +u64 pfn; + + pte =lookup_address_in_mm (current->mm, 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 1 ; + + pfn =pte_pfn (*pte); + +/* If its large page then calculte the fault pfn */ +if (level >PG_LEVEL_4K ) { +unsigned long mask; + + mask =pages_per_hpage (level) -pages_per_hpage (level -1 ); + pfn |= (address >>PAGE_SHIFT ) & mask; + } + +/* +* If its a guest private page, then the fault cannot be resolved. +* Send a SIGBUS to terminate the process. +*/ +if (snp_lookup_rmpentry (pfn, &rmp_level)) { +do_sigbus (regs, error_code, address,VM_FAULT_SIGBUS ); +return 1 ; + } + +/* +* The backing page level is higher than the RMP page level, request +* to split the page. +*/ +if (level > rmp_level) +return 0 ; + +return 1 ; +} +/* * Handle faults in the user portion of the address space. Nothing in here * should check X86_PF_USER without a specific justification: for almost
@@ -1299,6 +1354,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 +5 -1
@@ -447,6 +447,8 @@ extern pgprot_t protection_map[16];* @FAULT_FLAG_REMOTE: The fault is not for current task/mm. * @FAULT_FLAG_INSTRUCTION: The fault was during an instruction fetch. * @FAULT_FLAG_INTERRUPTIBLE: The fault can be interrupted by non-fatal signals. +* @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
@@ -478,6 +480,7 @@ enum fault_flag {FAULT_FLAG_REMOTE =1 <<7 ,FAULT_FLAG_INSTRUCTION =1 <<8 ,FAULT_FLAG_INTERRUPTIBLE =1 <<9 , + FAULT_FLAG_PAGE_SPLIT = 1 << 10, };/*
@@ -517,7 +520,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
mm/memory.c +13 -0
@@ -4589,6 +4589,15 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf)return 0 ; } +static int handle_split_page_fault (struct vm_fault *vmf) +{ +if (!IS_ENABLED (CONFIG_AMD_MEM_ENCRYPT )) +return VM_FAULT_SIGBUS ; + +__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 *
@@ -4666,6 +4675,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.17.1
PATCH v5 9/45 x86/fault: Add support to dump RMP entry on fault
In reply to Brijesh SinghWhen SEV-SNP is enabled globally, a write from the host goes through the RMP check. If the hardware encounters the check failure, then it raises the #PF (with RMP set). Dump the RMP entry at the faulting pfn to help the debug.
--- arch/x86/include/asm/sev.h | 7 +++++++ arch/x86/kernel/sev.c | 43 ++++++++++++++++++++++++++++++++++++++ arch/x86/mm/fault.c | 17 +++++++++++---- include/linux/sev.h | 2 ++ 4 files changed, 65 insertions(+), 4 deletions(-)
arch/x86/include/asm/sev.h +7 -0
@@ -106,6 +106,11 @@ struct __packed rmpentry {#define rmpentry_assigned (x) ((x)->info.assigned)#define rmpentry_pagesize (x) ((x)->info.pagesize) +#define rmpentry_vmsa (x) ((x)->info.vmsa) +#define rmpentry_asid (x) ((x)->info.asid) +#define rmpentry_validated (x) ((x)->info.validated) +#define rmpentry_gpa (x) ((unsigned long)(x)->info.gpa) +#define rmpentry_immutable (x) ((x)->info.immutable)#define RMPADJUST_VMSA_PAGE_BIT BIT(16)
@@ -165,6 +170,7 @@ void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op opvoid snp_set_memory_shared (unsigned long vaddr,unsigned int npages);void snp_set_memory_private (unsigned long vaddr,unsigned int npages);void snp_set_wakeup_secondary_cpu (void ); +void dump_rmpentry (u64 pfn);#ifdef __BOOT_COMPRESSEDbool sev_snp_enabled (void );#else
@@ -188,6 +194,7 @@ static inline void snp_set_memory_shared(unsigned long vaddr, unsigned int npagestatic inline void snp_set_memory_private (unsigned long vaddr,unsigned int npages) { }static inline void snp_set_wakeup_secondary_cpu (void ) { }static inline void sev_snp_cpuid_init (struct boot_params *bp) { } +static inline void dump_rmpentry (u64 pfn) {}#ifdef __BOOT_COMPRESSEDstatic inline bool sev_snp_enabled {return false; }#else
arch/x86/kernel/sev.c +43 -0
49 lines not on this page · raw message
arch/x86/mm/fault.c +13 -4
@@ -33,6 +33,7 @@#include <asm/pgtable_areas.h> /* VMALLOC_START, ... */ #include <asm/kvm_para.h> /* kvm_handle_async_pf */ #include <asm/vdso.h> /* fixup_vdso_exception() */ +#include <asm/sev.h> /* dump_rmpentry() */ #define CREATE_TRACE_POINTS #include <asm/trace/exceptions.h>
@@ -289,7 +290,7 @@ static bool low_pfn(unsigned long pfn)return pfn < max_low_pfn; } -static void dump_pagetable (unsigned long address) +static void dump_pagetable (unsigned long address,bool show_rmpentry) {pgd_t *base =__va (read_cr3_pa ());pgd_t *pgd = &base[pgd_index (address)];
@@ -345,10 +346,11 @@ static int bad_address(void *p)return get_kernel_nofault (dummy, (unsigned long *)p); } -static void dump_pagetable (unsigned long address) +static void dump_pagetable (unsigned long address,bool show_rmpentry) {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;
@@ -366,6 +368,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;
@@ -374,6 +377,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;
@@ -382,6 +386,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;
@@ -390,9 +395,13 @@ 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" ); + +if (show_rmpentry) +dump_rmpentry (pfn);return ; bad:pr_info ("BAD\n" );
@@ -578,7 +587,7 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long adshow_ldttss (&gdt,"TR" , tr); } -dump_pagetable (address); +dump_pagetable (address, error_code &X86_PF_RMP ); }static noinline void
@@ -595,7 +604,7 @@ pgtable_bad(struct pt_regs *regs, unsigned long error_code,printk (KERN_ALERT "%s: Corrupted page table at address %lx\n" , tsk->comm, address); -dump_pagetable (address); +dump_pagetable (address, false);if (__die ("Bad pagetable" , regs, error_code)) sig =0 ;
include/linux/sev.h +2 -0
@@ -16,6 +16,7 @@ int snp_lookup_rmpentry(u64 pfn, int *level);int psmash (u64 pfn);int rmp_make_private (u64 pfn,u64 gpa,enum pg_level level,int asid,bool immutable);int rmp_make_shared (u64 pfn,enum pg_level level); +void dump_rmpentry (u64 pfn);#else static inline int snp_lookup_rmpentry (u64 pfn,int *level) {return 0 ; }static inline int psmash (u64 pfn) {return -ENXIO ; }
@@ -25,6 +26,7 @@ static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asreturn -ENODEV ; }static inline int rmp_make_shared (u64 pfn,enum pg_level level) {return -ENODEV ; } +static inline void dump_rmpentry (u64 pfn) { }#endif /* CONFIG_AMD_MEM_ENCRYPT */ #endif /* __LINUX_SEV_H */
Signature · 1 line
2.17.1
PATCH v5 10/45 crypto: ccp: shutdown SEV firmware on kexec
In reply to Brijesh SinghThe commit 97f9ac3db6612 ("crypto: ccp - Add support for SEV-ES to the PSP driver") added support to allocate Trusted Memory Region (TMR) used during the SEV-ES firmware initialization. The TMR gets locked during the firmware initialization and unlocked during the shutdown. While the TMR is locked, access to it is disallowed.
Currently, the CCP driver does not shutdown the firmware during the kexec reboot, leaving the TMR memory locked.
Register a callback to shutdown the SEV firmware on the kexec boot.
--- drivers/crypto/ccp/sev-dev.c | 49 +++++++++++++++++------------------- drivers/crypto/ccp/sp-pci.c | 12 +++++++++ 2 files changed, 35 insertions(+), 26 deletions(-)
drivers/crypto/ccp/sev-dev.c +23 -26
@@ -300,6 +300,9 @@ static int __sev_platform_shutdown_locked(int *error)struct sev_device *sev = psp_master->sev_data;int ret; +if (sev->state ==SEV_STATE_UNINIT ) +return 0 ; + ret =__sev_do_cmd_locked (SEV_CMD_SHUTDOWN ,NULL , error);if (ret)return ret;
@@ -1019,6 +1022,20 @@ int sev_dev_init(struct psp_device *psp)return ret; } +static void sev_firmware_shutdown (struct sev_device *sev) +{ +sev_platform_shutdown (NULL ); + +if (sev_es_tmr) { +/* 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 )); + sev_es_tmr =NULL ; + } +} +void sev_dev_destroy (struct psp_device *psp) {struct sev_device *sev = psp->sev_data;
@@ -1026,6 +1043,8 @@ void sev_dev_destroy(struct psp_device *psp)if (!sev)return ; +sev_firmware_shutdown (sev); +if (sev->misc)kref_put (&misc_dev->refcount, sev_exit);
@@ -1056,21 +1075,6 @@ void sev_pci_init(void)if (sev_get_api_version ()) goto err; -/* -* If platform is not in UNINIT state then firmware upgrade and/or -* platform INIT command will fail. These command require UNINIT state. -* -* In a normal boot we should never run into case where the firmware -* is not in UNINIT state on boot. But in case of kexec boot, a reboot -* may not go through a typical shutdown sequence and may leave the -* firmware in INIT or WORKING state. -*/ - -if (sev->state !=SEV_STATE_UNINIT ) { -sev_platform_shutdown (NULL ); - sev->state =SEV_STATE_UNINIT ; - } -if (sev_version_greater_or_equal (0 ,15 ) &&sev_update_firmware (sev->dev) ==0 )sev_get_api_version ();
@@ -1115,17 +1119,10 @@ void sev_pci_init(void)void sev_pci_exit (void ) { -if (!psp_master->sev_data) -return ; - -sev_platform_shutdown (NULL ); +struct sev_device *sev = psp_master->sev_data; -if (sev_es_tmr) { -/* The TMR area was encrypted, flush it from the cache */ -wbinvd_on_all_cpus (); +if (!sev) +return ; -free_pages ((unsigned long )sev_es_tmr, -get_order (SEV_ES_TMR_SIZE )); - sev_es_tmr =NULL ; - } +sev_firmware_shutdown (sev); }
drivers/crypto/ccp/sp-pci.c +12 -0
@@ -241,6 +241,17 @@ static int sp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)return ret; } +static void sp_pci_shutdown (struct pci_dev *pdev) +{ +struct device *dev = &pdev->dev; +struct sp_device *sp =dev_get_drvdata (dev); + +if (!sp) +return ; + +sp_destroy (sp); +} +static void sp_pci_remove (struct pci_dev *pdev) {struct device *dev = &pdev->dev;
@@ -371,6 +382,7 @@ static struct pci_driver sp_pci_driver = { .id_table = sp_pci_table, .probe = sp_pci_probe, .remove = sp_pci_remove, + .shutdown = sp_pci_shutdown, .driver.pm = &sp_pci_pm_ops, };
Signature · 1 line
2.17.1
PATCH v5 11/45 crypto:ccp: Define the SEV-SNP commands
In reply to Brijesh SinghAMD 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 | 222 +++++++++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 42 +++++++ 3 files changed, 279 insertions(+), 1 deletion(-)
drivers/crypto/ccp/sev-dev.c +15 -1
@@ -134,7 +134,21 @@ static int sev_cmd_buffer_len(int cmd)case SEV_CMD_DOWNLOAD_FIRMWARE :return sizeof (struct sev_data_download_firmware );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_SEND_CANCEL :return sizeof (struct sev_data_send_cancel ); +case SEV_CMD_SNP_GCTX_CREATE :return sizeof (struct sev_data_snp_gctx_create ); +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_decommission ); +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_platform_status_buf ); +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 +222 -0
234 lines not on this page · raw message
include/uapi/linux/psp-sev.h +42 -0
54 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v5 12/45 crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP
In reply to Brijesh SinghBefore SNP VMs can be launched, the platform must be appropriately configured and initialized. Platform initialization is accomplished via the SNP_INIT command.
--- drivers/crypto/ccp/sev-dev.c | 123 +++++++++++++++++++++++++++++++++-- drivers/crypto/ccp/sev-dev.h | 2 + include/linux/psp-sev.h | 16 +++++ 3 files changed, 136 insertions(+), 5 deletions(-)
drivers/crypto/ccp/sev-dev.c +118 -5
@@ -32,6 +32,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 30 +static DEFINE_MUTEX (sev_cmd_mutex);static struct sev_misc_dev *misc_dev;
@@ -598,6 +602,95 @@ 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 __sev_snp_init_locked (int *error) +{ +struct psp_device *psp = psp_master; +struct sev_device *sev; +int rc =0 ; + +if (!psp || !psp->sev_data) +return -ENODEV ; + + sev = psp->sev_data; + +if (sev->snp_inited) +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 ); + +/* Prepare for first SEV guest launch after INIT */ +wbinvd_on_all_cpus (); + +/* Issue the SNP_INIT firmware command. */ + rc =__sev_do_cmd_locked (SEV_CMD_SNP_INIT ,NULL , error); +if (rc) +return rc; + + sev->snp_inited = true; +dev_dbg (sev->dev,"SEV-SNP firmware initialized\n" ); + +return rc; +} + +int sev_snp_init (int *error) +{ +int rc; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return -ENODEV ; + +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; +int ret; + +if (!sev->snp_inited) +return 0 ; + +/* SHUTDOWN requires the DF_FLUSH */ +wbinvd_on_all_cpus (); +__sev_do_cmd_locked (SEV_CMD_SNP_DF_FLUSH ,NULL ,NULL ); + + ret =__sev_do_cmd_locked (SEV_CMD_SNP_SHUTDOWN ,NULL , error); +if (ret) { +dev_err (sev->dev,"SEV-SNP firmware shutdown failed\n" ); +return ret; + } + + sev->snp_inited = 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 (NULL ); +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;
@@ -1048,6 +1141,8 @@ static void sev_firmware_shutdown(struct sev_device *sev) get_order(SEV_ES_TMR_SIZE)); sev_es_tmr =NULL ; } + +sev_snp_shutdown (NULL ); }void sev_dev_destroy (struct psp_device *psp)
@@ -1093,6 +1188,26 @@ void sev_pci_init(void) sev_update_firmware(sev->dev) == 0)sev_get_api_version (); +/* +* If boot CPU supports the 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); +if (rc) { +/* +* If we failed to INIT SNP then don't abort the probe. +* 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 */ tmr_page =alloc_pages (GFP_KERNEL ,get_order (SEV_ES_TMR_SIZE ));if (tmr_page) {
@@ -1117,13 +1232,11 @@ void sev_pci_init(void) rc =sev_platform_init (&error); } -if (rc) { +if (rc)dev_err (sev->dev,"SEV: failed to INIT error %#x\n" , error); -return ; - } -dev_info (sev->dev,"SEV API:%d.%d build:%d\n" , sev->api_major, - sev->api_minor, sev->build); +dev_info (sev->dev,"SEV%s API:%d.%d build:%d\n" , sev->snp_inited ? +"-SNP" :"" , sev->api_major, sev->api_minor, sev->build);return ;
drivers/crypto/ccp/sev-dev.h +2 -0
@@ -52,6 +52,8 @@ struct sev_device {u8 build;void *cmd_buf; + +bool snp_inited; };int sev_dev_init (struct psp_device *psp);
include/linux/psp-sev.h +16 -0
@@ -748,6 +748,20 @@ struct sev_data_snp_init_ex {*/ int sev_platform_init (int *error); +/** +* sev_snp_init - perform SEV SNP_INIT 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_snp_init (int *error); +/** * sev_platform_status - perform SEV PLATFORM_STATUS command *
@@ -855,6 +869,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) {return -ENODEV ; } +static inline int sev_guest_deactivate (struct sev_data_deactivate *data,int *error) {return -ENODEV ; }
Signature · 1 line
2.17.1
PATCH v5 13/45 crypto:ccp: Provide APIs to issue SEV-SNP commands
In reply to Brijesh SinghProvide the APIs for the hypervisor to manage an SEV-SNP guest. The commands for SEV-SNP is defined in the SEV-SNP firmware specification.
--- drivers/crypto/ccp/sev-dev.c | 24 ++++++++++++ include/linux/psp-sev.h | 73 ++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+)
drivers/crypto/ccp/sev-dev.c +24 -0
@@ -1025,6 +1025,30 @@ int sev_guest_df_flush(int *error) }EXPORT_SYMBOL_GPL (sev_guest_df_flush); +int snp_guest_decommission (struct sev_data_snp_decommission *data,int *error) +{ +return sev_do_cmd (SEV_CMD_SNP_DECOMMISSION , data, error); +} +EXPORT_SYMBOL_GPL (snp_guest_decommission); + +int snp_guest_df_flush (int *error) +{ +return sev_do_cmd (SEV_CMD_SNP_DF_FLUSH ,NULL , error); +} +EXPORT_SYMBOL_GPL (snp_guest_df_flush); + +int snp_guest_page_reclaim (struct sev_data_snp_page_reclaim *data,int *error) +{ +return sev_do_cmd (SEV_CMD_SNP_PAGE_RECLAIM , data, error); +} +EXPORT_SYMBOL_GPL (snp_guest_page_reclaim); + +int snp_guest_dbg_decrypt (struct sev_data_snp_dbg *data,int *error) +{ +return sev_do_cmd (SEV_CMD_SNP_DBG_DECRYPT , data, error); +} +EXPORT_SYMBOL_GPL (snp_guest_dbg_decrypt); +static void sev_exit (struct kref *ref) {misc_deregister (&misc_dev->misc);
include/linux/psp-sev.h +73 -0
85 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v5 14/45 crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
In reply to Brijesh SinghThe 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 satify 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 | 169 ++++++++++++++++++++++++++++++++++- include/linux/psp-sev.h | 11 +++ 2 files changed, 176 insertions(+), 4 deletions(-)
drivers/crypto/ccp/sev-dev.c +165 -4
@@ -62,6 +62,14 @@ static int psp_timeout;#define SEV_ES_TMR_SIZE (1024 * 1024)static void *sev_es_tmr; +/* 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 int sev_do_cmd (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;
@@ -159,6 +167,156 @@ static int sev_cmd_buffer_len(int cmd)return 0 ; } +static void snp_leak_pages (unsigned long pfn,unsigned int npages) +{ +WARN (1 ,"psc failed, pfn 0x%lx pages %d (leaking)\n" , pfn, npages); +while (npages--) { +memory_failure (pfn,0 ); +dump_rmpentry (pfn); + pfn++; + } +} + +static int snp_reclaim_pages (unsigned long pfn,unsigned int npages,bool locked) +{ +struct sev_data_snp_page_reclaim data; +int ret, err, i, n =0 ; + +for (i =0 ; i < npages; i++) { +memset (&data,0 ,sizeof (data)); + data.paddr = pfn <<PAGE_SHIFT ; + +if (locked) + ret =__sev_do_cmd_locked (SEV_CMD_SNP_PAGE_RECLAIM , &data, &err); +else + ret =sev_do_cmd (SEV_CMD_SNP_PAGE_RECLAIM , &data, &err); +if (ret) + goto cleanup; + + ret =rmp_make_shared (pfn,PG_LEVEL_4K ); +if (ret) + goto cleanup; + + pfn++; + n++; + } + +return 0 ; + +cleanup: +/* +* If failed to reclaim the page then page is no longer safe to +* be released, leak it. +*/ +snp_leak_pages (pfn, npages - n); +return ret; +} + +static inline int rmp_make_firmware (unsigned long pfn,int level) +{ +return rmp_make_private (pfn,0 , level,0 , true); +} + +static int snp_set_rmp_state (unsigned long paddr,unsigned int npages,bool to_fw,bool locked, +bool need_reclaim) +{ +unsigned long pfn =__sme_clr (paddr) >>PAGE_SHIFT ;/* Cbit maybe set in the paddr */ +int rc, n =0 , i; + +for (i =0 ; i < npages; i++) { +if (to_fw) + rc =rmp_make_firmware (pfn,PG_LEVEL_4K ); +else + rc = need_reclaim ?snp_reclaim_pages (pfn,1 , locked) : +rmp_make_shared (pfn,PG_LEVEL_4K ); +if (rc) + goto cleanup; + + pfn++; + n++; + } + +return 0 ; + +cleanup: +/* Try unrolling the firmware state changes */ +if (to_fw) { +/* +* Reclaim the pages which were already changed to the +* firmware state. +*/ +snp_reclaim_pages (paddr >>PAGE_SHIFT , n, locked); + +return rc; + } + +/* +* If failed to change the page state to shared, then its not safe +* to release the page back to the system, leak it. +*/ +snp_leak_pages (pfn, npages - n); + +return rc; +} + +static struct page *__snp_alloc_firmware_pages (gfp_t gfp_mask,int order,bool locked) +{ +unsigned long npages =1ul << order, paddr; +struct sev_device *sev; +struct page *page; + +if (!psp_master || !psp_master->sev_data) +return ERR_PTR (-EINVAL ); + + 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_inited) +return page; + + paddr =__pa ((unsigned long )page_address (page)); +if (snp_set_rmp_state (paddr, npages, true, locked, false)) +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) +{ +unsigned long paddr, npages =1ul << order; + +if (!page) +return ; + + paddr =__pa ((unsigned long )page_address (page)); +if (snp_set_rmp_state (paddr, npages, false, locked, true)) +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 (snp_free_firmware_page); +static int __sev_do_cmd_locked (int cmd,void *data,int *psp_ret) {struct psp_device *psp = psp_master;
@@ -281,7 +439,7 @@ static int __sev_platform_init_locked(int *error) data.flags |=SEV_INIT_FLAGS_SEV_ES ; data.tmr_address = tmr_pa; - data.tmr_len =SEV_ES_TMR_SIZE ; + data.tmr_len = sev_es_tmr_size; } rc =__sev_do_cmd_locked (SEV_CMD_INIT , &data, error);
@@ -638,6 +796,8 @@ static int __sev_snp_init_locked(int *error) sev->snp_inited = true;dev_dbg (sev->dev,"SEV-SNP firmware initialized\n" ); + sev_es_tmr_size =SEV_SNP_ES_TMR_SIZE ; +return rc; }
@@ -1161,8 +1321,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 ; }
@@ -1233,7 +1394,7 @@ void sev_pci_init(void) }/* Obtain the TMR memory area for SEV-ES use */ - tmr_page =alloc_pages (GFP_KERNEL ,get_order (SEV_ES_TMR_SIZE )); + tmr_page =__snp_alloc_firmware_pages (GFP_KERNEL ,get_order (sev_es_tmr_size), false);if (tmr_page) { sev_es_tmr =page_address (tmr_page); }else {
include/linux/psp-sev.h +11 -0
@@ -12,6 +12,8 @@#ifndef __PSP_SEV_H__#define __PSP_SEV_H__ +#include <linux/sev.h> +#include <uapi/linux/psp-sev.h> #ifdef CONFIG_X86
@@ -919,6 +921,8 @@ int snp_guest_page_reclaim(struct sev_data_snp_page_reclaim *data, int *error);int snp_guest_dbg_decrypt (struct sev_data_snp_dbg *data,int *error);void *psp_copy_user_blob (u64 uaddr,u32 len); +void *snp_alloc_firmware_page (gfp_t mask); +void snp_free_firmware_page (void *addr);#else /* !CONFIG_CRYPTO_DEV_SP_PSP */
@@ -960,6 +964,13 @@ static inline int snp_guest_dbg_decrypt(struct sev_data_snp_dbg *data, int *erroreturn -ENODEV ; } +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.17.1
PATCH v5 15/45 crypto: ccp: Handle the legacy SEV command when SNP is enabled
In reply to Brijesh SinghThe 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 | 346 ++++++++++++++++++++++++++++++++++- drivers/crypto/ccp/sev-dev.h | 12 ++ 2 files changed, 348 insertions(+), 10 deletions(-)
drivers/crypto/ccp/sev-dev.c +336 -10
@@ -317,12 +317,295 @@ void snp_free_firmware_page(void *addr) }EXPORT_SYMBOL (snp_free_firmware_page); +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 (snp_set_rmp_state (*paddr, npages, true, true, false)) +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 (snp_set_rmp_state (__pa (map->host), npages, true, true, false)) +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_set_rmp_state (*paddr, npages, false, true, 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 we must +* transition the pages to shared state. +*/ +if (snp_set_rmp_state (__pa (map->host), npages, false, true, 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 (snp_set_rmp_state (__pa (cmd_buf),1 , false, true, false)) +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 (snp_set_rmp_state (__pa (cmd_buf),1 , true, true, false)) +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_inited) ? 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)
@@ -342,12 +625,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, sev->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);
@@ -388,15 +687,24 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) ret = -EIO ; } -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; }
@@ -1271,10 +1579,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;
@@ -1327,6 +1637,12 @@ static void sev_firmware_shutdown(struct sev_device *sev) sev_es_tmr =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 (NULL ); }
@@ -1391,6 +1707,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 */
@@ -1420,12 +1744,14 @@ void sev_pci_init(void)if (rc)dev_err (sev->dev,"SEV: failed to INIT error %#x\n" , error); +skip_legacy:dev_info (sev->dev,"SEV%s API:%d.%d build:%d\n" , sev->snp_inited ?"-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_inited; +struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS ]; };int sev_dev_init (struct psp_device *psp);
Signature · 1 line
2.17.1
PATCH v5 16/45 crypto: ccp: Add the SNP_PLATFORM_STATUS command
In reply to Brijesh SinghThe 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/sevguest.rst | 27 +++++++++++++++++ drivers/crypto/ccp/sev-dev.c | 45 ++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 1 + 3 files changed, 73 insertions(+)
Documentation/virt/coco/sevguest.rst +27 -0
@@ -52,6 +52,22 @@ to execute due to the firmware error, then fw_err code will be set. __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 ------------------
@@ -107,3 +123,14 @@ length of the blob is lesser than expected then snp_ext_report_req.certs_len wil be updated with the expected value. See GHCB specification for further detail on how to parse the certificate blob. + +2.3 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.
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.17.1
PATCH v5 17/45 crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command
In reply to Brijesh SinghThe 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/sevguest.rst | 28 +++++++ drivers/crypto/ccp/sev-dev.c | 115 +++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 3 + include/uapi/linux/psp-sev.h | 17 ++++ 4 files changed, 163 insertions(+)
Documentation/virt/coco/sevguest.rst +28 -0
@@ -134,3 +134,31 @@ See GHCB specification for further detail on how to parse the certificate blob. 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.4 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 previous certificate blob will deleted. +For more information on the certificate blob layout, see the GHCB spec +(extended guest request message). + + +2.4 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_SET_EXT_CONFIG is used to query the system-wide configuration set +through the SNP_SET_EXT_CONFIG.
drivers/crypto/ccp/sev-dev.c +115 -0
133 lines not on this page · raw message
drivers/crypto/ccp/sev-dev.h +3 -0
@@ -66,6 +66,9 @@ struct sev_device {bool snp_inited;struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS ]; +void *snp_certs_data; +u32 snp_certs_len; +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, };
@@ -190,6 +192,21 @@ struct sev_user_data_snp_config {__u8 rsvd[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.17.1
PATCH v5 18/45 crypto: ccp: Provide APIs to query extended attestation report
In reply to Brijesh SinghVersion 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 | 43 ++++++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 24 ++++++++++++++++++++ 2 files changed, 67 insertions(+)
drivers/crypto/ccp/sev-dev.c +43 -0
@@ -22,6 +22,7 @@#include <linux/firmware.h> #include <linux/gfp.h> #include <linux/cpufeature.h> +#include <linux/sev-guest.h> #include <asm/smp.h>
@@ -1677,6 +1678,48 @@ int snp_guest_dbg_decrypt(struct sev_data_snp_dbg *data, int *error) }EXPORT_SYMBOL_GPL (snp_guest_dbg_decrypt); +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_inited) +return -EINVAL ; + +/* +* 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 ; +return -EINVAL ; + } + + rc =sev_do_cmd (SEV_CMD_SNP_GUEST_REQUEST , data, (int *)&fw_err); +if (rc) +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 ; + } + +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 +24 -0
@@ -924,6 +924,23 @@ void *psp_copy_user_blob(u64 uaddr, u32 len);void *snp_alloc_firmware_page (gfp_t mask);void snp_free_firmware_page (void *addr); +/** +* 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. +*/ +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
@@ -971,6 +988,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.17.1
PATCH v5 19/45 KVM: SVM: Add support to handle AP reset MSR protocol
In reply to Brijesh SinghFrom: 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
@@ -188,6 +188,7 @@ struct vcpu_svm {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.17.1
PATCH v5 20/45 KVM: SVM: Provide the Hypervisor Feature support VMGEXIT
In reply to Brijesh SinghVersion 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
@@ -97,6 +97,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
@@ -2184,6 +2184,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 : goto vmgexit_err;
@@ -2438,6 +2439,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;
@@ -2553,6 +2561,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
@@ -548,9 +548,10 @@ void svm_vcpu_unblocking(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.17.1
PATCH v5 21/45 KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
In reply to Brijesh SinghImplement 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 | 1 + arch/x86/kvm/lapic.c | 5 ++++- arch/x86/kvm/svm/sev.c | 35 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 16 ++++++++++++-- arch/x86/kvm/svm/svm.h | 1 + 6 files changed, 56 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -122,6 +122,7 @@ KVM_X86_OP_NULL(enable_direct_tlbflush) KVM_X86_OP_NULL(migrate_timers) KVM_X86_OP(msr_filter_changed) KVM_X86_OP_NULL(complete_emulated_msr) +KVM_X86_OP(alloc_apic_backing_page)#undef KVM_X86_OP#undef KVM_X86_OP_NULL
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1453,6 +1453,7 @@ struct kvm_x86_ops {int (*complete_emulated_msr)(struct kvm_vcpu *vcpu,int err);void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu,u8 vector); +void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); };struct kvm_x86_nested_ops {
arch/x86/kvm/lapic.c +4 -1
@@ -2457,7 +2457,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 +35 -0
@@ -2703,3 +2703,38 @@ 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_to_phys (pfn),PMD_SIZE )) { + pfn++; +__free_page (p); + }else { +__free_page (pfn_to_page (pfn +1 )); + } + +return pfn_to_page (pfn); +}
arch/x86/kvm/svm/svm.c +14 -2
@@ -1368,7 +1368,7 @@ static int svm_create_vcpu(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;
@@ -1377,7 +1377,7 @@ static int svm_create_vcpu(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;
@@ -4539,6 +4539,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 = { .hardware_unsetup = svm_hardware_teardown, .hardware_enable = svm_hardware_enable,
@@ -4667,6 +4677,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .complete_emulated_msr = svm_complete_emulated_msr, .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, + + .alloc_apic_backing_page = svm_alloc_apic_backing_page, };static struct kvm_x86_init_ops svm_init_ops__initdata = {
arch/x86/kvm/svm/svm.h +1 -0
@@ -575,6 +575,7 @@ void sev_es_create_vcpu(struct vcpu_svm *svm);void sev_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector);void sev_es_prepare_guest_switch (struct vcpu_svm *svm,unsigned int cpu);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.17.1
PATCH v5 22/45 KVM: SVM: Add initial SEV-SNP support
In reply to Brijesh SinghThe 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
@@ -1836,6 +1839,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;
@@ -1896,12 +1900,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
@@ -72,6 +72,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 */
@@ -246,6 +247,13 @@ static 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.17.1
PATCH v5 23/45 KVM: SVM: Add KVM_SNP_INIT command
In reply to Brijesh SinghThe 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/amd-memory-encryption.rst | 27 ++++++++++++ arch/x86/include/asm/svm.h | 2 + arch/x86/kvm/svm/sev.c | 44 ++++++++++++++++++- arch/x86/kvm/svm/svm.h | 4 ++ include/uapi/linux/kvm.h | 13 ++++++ 5 files changed, 88 insertions(+), 2 deletions(-)
Documentation/virt/kvm/amd-memory-encryption.rst +27 -0
@@ -427,6 +427,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 +2 -0
@@ -218,6 +218,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define SVM_NESTED_CTL_SEV_ENABLE BIT(1)#define SVM_NESTED_CTL_SEV_ES_ENABLE BIT(2) +#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +struct vmcb_seg {u16 selector;u16 attrib;
arch/x86/kvm/svm/sev.c +42 -2
76 lines not on this page · raw message
arch/x86/kvm/svm/svm.h +4 -0
@@ -69,6 +69,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 */
@@ -81,6 +84,7 @@ struct kvm_sev_info {u64 ap_jump_table;/* SEV-ES AP Jump Table address */ struct kvm *enc_context_owner;/* Owner of copied encryption context */ struct misc_cg *misc_cg;/* For misc cgroup accounting */ +u64 snp_init_flags; };struct kvm_svm {
include/uapi/linux/kvm.h +13 -0
@@ -1712,6 +1712,9 @@ enum sev_cmd_id {/* Guest Migration Extension */ KVM_SEV_SEND_CANCEL , +/* SNP specific commands */ +KVM_SEV_SNP_INIT , + KVM_SEV_NR_MAX, };
@@ -1808,6 +1811,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.17.1
PATCH v5 24/45 KVM: SVM: Add KVM_SEV_SNP_LAUNCH_START command
In reply to Brijesh SinghKVM_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/amd-memory-encryption.rst | 24 ++++ arch/x86/kvm/svm/sev.c | 116 +++++++++++++++++- arch/x86/kvm/svm/svm.h | 1 + include/uapi/linux/kvm.h | 10 ++ 4 files changed, 148 insertions(+), 3 deletions(-)
Documentation/virt/kvm/amd-memory-encryption.rst +24 -0
@@ -454,6 +454,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 migtation 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 +113 -3
169 lines not on this page · raw message
arch/x86/kvm/svm/svm.h +1 -0
@@ -85,6 +85,7 @@ struct kvm_sev_info {struct kvm *enc_context_owner;/* Owner of copied encryption context */ struct misc_cg *misc_cg;/* For misc cgroup accounting */ u64 snp_init_flags; +void *snp_context;/* SNP guest context page */ };struct kvm_svm {
include/uapi/linux/kvm.h +10 -0
@@ -1714,6 +1714,7 @@ enum sev_cmd_id {/* SNP specific commands */ KVM_SEV_SNP_INIT , +KVM_SEV_SNP_LAUNCH_START , KVM_SEV_NR_MAX, };
@@ -1821,6 +1822,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.17.1
PATCH v5 25/45 KVM: SVM: Add KVM_SEV_SNP_LAUNCH_UPDATE command
In reply to Brijesh SinghThe 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/amd-memory-encryption.rst | 29 +++ arch/x86/kvm/svm/sev.c | 187 ++++++++++++++++++ include/uapi/linux/kvm.h | 19 ++ 3 files changed, 235 insertions(+)
Documentation/virt/kvm/amd-memory-encryption.rst +29 -0
@@ -478,6 +478,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 +187 -0
217 lines not on this page · raw message
include/uapi/linux/kvm.h +19 -0
@@ -1715,6 +1715,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, };
@@ -1831,6 +1832,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.17.1
PATCH v5 27/45 KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command
In reply to Brijesh SinghThe 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.
--- .../virt/kvm/amd-memory-encryption.rst | 22 ++++ arch/x86/kvm/svm/sev.c | 116 ++++++++++++++++++ include/uapi/linux/kvm.h | 14 +++ 3 files changed, 152 insertions(+)
Documentation/virt/kvm/amd-memory-encryption.rst +22 -0
@@ -506,6 +506,28 @@ 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]; + }; + + +See SEV-SNP specification for further details on launch finish input parameters. References ==========
arch/x86/kvm/svm/sev.c +116 -0
144 lines not on this page · raw message
include/uapi/linux/kvm.h +14 -0
@@ -1716,6 +1716,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, };
@@ -1850,6 +1851,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.17.1
PATCH v5 28/45 KVM: X86: Keep the NPT and RMP page level in sync
In reply to Brijesh SinghWhen 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 | 5 ++++ arch/x86/kvm/svm/sev.c | 46 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 1 + 6 files changed, 56 insertions(+)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -123,6 +123,7 @@ KVM_X86_OP_NULL(migrate_timers) KVM_X86_OP(msr_filter_changed) KVM_X86_OP_NULL(complete_emulated_msr) KVM_X86_OP(alloc_apic_backing_page) +KVM_X86_OP_NULL(rmp_page_level_adjust)#undef KVM_X86_OP#undef KVM_X86_OP_NULL
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1453,7 +1453,9 @@ struct kvm_x86_ops {int (*complete_emulated_msr)(struct kvm_vcpu *vcpu,int err);void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu,u8 vector); +void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); +void (*rmp_page_level_adjust)(struct kvm *kvm,kvm_pfn_t pfn,int *level); };struct kvm_x86_nested_ops {
arch/x86/kvm/mmu/mmu.c +5 -0
@@ -43,6 +43,7 @@#include <linux/hash.h> #include <linux/kern_levels.h> #include <linux/kthread.h> +#include <linux/sev.h> #include <asm/page.h> #include <asm/memtype.h>
@@ -2818,6 +2819,10 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,if (unlikely (!pte))return PG_LEVEL_4K ; +/* Adjust the page level based on the SEV-SNP RMP page level. */ +if (kvm_x86_ops.rmp_page_level_adjust) +static_call (kvm_x86_rmp_page_level_adjust)(kvm, pfn, &level); +return level; }
arch/x86/kvm/svm/sev.c +46 -0
49 lines not on this page · raw message
arch/x86/kvm/svm/svm.c +1 -0
@@ -4679,6 +4679,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, .alloc_apic_backing_page = svm_alloc_apic_backing_page, + .rmp_page_level_adjust = sev_rmp_page_level_adjust, };static struct kvm_x86_init_ops svm_init_ops__initdata = {
arch/x86/kvm/svm/svm.h +1 -0
@@ -589,6 +589,7 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);void sev_es_prepare_guest_switch (struct vcpu_svm *svm,unsigned int cpu);void sev_es_unmap_ghcb (struct vcpu_svm *svm);struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu); +void sev_rmp_page_level_adjust (struct kvm *kvm,kvm_pfn_t pfn,int *level);/* vmenter.S */
Signature · 1 line
2.17.1
PATCH v5 26/45 KVM: SVM: Mark the private vma unmerable for SEV-SNP guests
In reply to Brijesh SinghWhen SEV-SNP is enabled, the guest private pages are added in the RMP table; while adding the pages, the rmp_make_private() unmaps the pages from the direct map. If KSM attempts to access those unmapped pages then it will trigger #PF (page-not-present).
Encrypted guest pages cannot be shared between the process, so an userspace should not mark the region mergeable but to be safe, mark the process vma unmerable before adding the pages in the RMP table.
--- arch/x86/kvm/svm/sev.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
arch/x86/kvm/svm/sev.c +32 -0
@@ -18,11 +18,13 @@#include <linux/processor.h> #include <linux/trace_events.h> #include <linux/sev.h> +#include <linux/ksm.h> #include <asm/fpu/internal.h> #include <asm/pkru.h> #include <asm/trapnr.h> #include <asm/sev.h> +#include <asm/mman.h> #include "x86.h" #include "svm.h"
@@ -1683,6 +1685,30 @@ static bool is_hva_registered(struct kvm *kvm, hva_t hva, size_t len)return false; } +static int snp_mark_unmergable (struct kvm *kvm,u64 start,u64 size) +{ +struct vm_area_struct *vma; +u64 end = start + size; +int ret; + +do { + vma =find_vma_intersection (kvm->mm, start, end); +if (!vma) { + ret = -EINVAL ; +break ; + } + + ret =ksm_madvise (vma, vma->vm_start, vma->vm_end, +MADV_UNMERGEABLE , &vma->vm_flags); +if (ret) +break ; + + start = vma->vm_end; + }while (end > vma->vm_end); + +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;
@@ -1707,6 +1733,12 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)if (!is_hva_registered (kvm, params.uaddr, params.len))return -EINVAL ; +mmap_write_lock (kvm->mm); + ret =snp_mark_unmergable (kvm, params.uaddr, params.len); +mmap_write_unlock (kvm->mm); +if (ret) +return -EFAULT ; +/* * The userspace memory is already locked so technically we don't * need to lock it again. Later part of the function needs to know
Signature · 1 line
2.17.1
PATCH v5 29/45 KVM: x86/mmu: Move 'pfn' variable to caller of direct_page_fault()
In reply to Brijesh SinghFrom: Sean Christopherson <[email protected]>
When adding pages prior to boot, TDX will need the resulting host pfn so that it can be passed to TDADDPAGE (TDX-SEAM always works with physical addresses as it has its own page tables). Start plumbing pfn back up the page fault stack.
--- arch/x86/kvm/mmu/mmu.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-)
arch/x86/kvm/mmu/mmu.c +14 -10
@@ -3818,7 +3818,8 @@ static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn, }static int direct_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u32 error_code, -bool prefault,int max_level,bool is_tdp) +bool prefault,int max_level,bool is_tdp, +kvm_pfn_t *pfn) {bool is_tdp_mmu_fault =is_tdp_mmu (vcpu->arch.mmu);bool write = error_code &PFERR_WRITE_MASK ;
@@ -3826,7 +3827,6 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code,gfn_t gfn = gpa >>PAGE_SHIFT ;unsigned long mmu_seq; -kvm_pfn_t pfn;hva_t hva;int r;
@@ -3846,11 +3846,11 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, mmu_seq = vcpu->kvm->mmu_notifier_seq;smp_rmb (); -if (try_async_pf (vcpu, prefault, gfn, gpa, &pfn, &hva, +if (try_async_pf (vcpu, prefault, gfn, gpa, pfn, &hva, write, &map_writable))return RET_PF_RETRY ; -if (handle_abnormal_pfn (vcpu, is_tdp ?0 : gpa, gfn, pfn,ACC_ALL , &r)) +if (handle_abnormal_pfn (vcpu, is_tdp ?0 : gpa, gfn, *pfn,ACC_ALL , &r))return r; r =RET_PF_RETRY ;
@@ -3860,7 +3860,8 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code,else write_lock (&vcpu->kvm->mmu_lock); -if (!is_noslot_pfn (pfn) &&mmu_notifier_retry_hva (vcpu->kvm, mmu_seq, hva)) +if (!is_noslot_pfn (*pfn) && +mmu_notifier_retry_hva (vcpu->kvm, mmu_seq, hva)) goto out_unlock; r =make_mmu_pages_available (vcpu);if (r)
@@ -3868,9 +3869,9 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code,if (is_tdp_mmu_fault) r =kvm_tdp_mmu_map (vcpu, gpa, error_code, map_writable, max_level, - pfn, prefault); + *pfn, prefault);else - r =__direct_map (vcpu, gpa, error_code, map_writable, max_level, pfn, + r =__direct_map (vcpu, gpa, error_code, map_writable, max_level, *pfn, prefault, is_tdp); out_unlock:
@@ -3878,18 +3879,20 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code,read_unlock (&vcpu->kvm->mmu_lock); else write_unlock(&vcpu->kvm->mmu_lock); -kvm_release_pfn_clean (pfn); +kvm_release_pfn_clean (*pfn);return r; }static int nonpaging_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u32 error_code,bool prefault) { +kvm_pfn_t pfn; +pgprintk ("%s: gva %lx error %x\n" , __func__, gpa, error_code);/* This path builds a PAE pagetable, we can map 2mb pages at maximum. */ return direct_page_fault (vcpu, gpa &PAGE_MASK , error_code, prefault, -PG_LEVEL_2M , false); +PG_LEVEL_2M , false, &pfn); } int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code,
@@ -3928,6 +3931,7 @@ EXPORT_SYMBOL_GPL(kvm_handle_page_fault);int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u32 error_code,bool prefault) { +kvm_pfn_t pfn;int max_level; for (max_level = KVM_MAX_HUGEPAGE_LEVEL;
@@ -3941,7 +3945,7 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, }return direct_page_fault (vcpu, gpa, error_code, prefault, - max_level, true); + max_level, true, &pfn); }static void nonpaging_init_context (struct kvm_mmu *context)
Signature · 1 line
2.17.1
PATCH v5 30/45 KVM: x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by TDX and SNP
In reply to Brijesh SinghFrom: Sean Christopherson <[email protected]>
Introduce a helper to directly (pun intended) fault-in a TDP page without having to go through the full page fault path. This allows TDX to get the resulting pfn and also allows the RET_PF_* enums to stay in mmu.c where they belong.
--- arch/x86/kvm/mmu.h | 3 +++ arch/x86/kvm/mmu/mmu.c | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+)
arch/x86/kvm/mmu.h +3 -0
@@ -127,6 +127,9 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,return vcpu->arch.mmu->page_fault (vcpu, cr2_or_gpa, err, prefault); } +kvm_pfn_t kvm_mmu_map_tdp_page (struct kvm_vcpu *vcpu,gpa_t gpa, +u32 error_code,int max_level); +/* * Currently, we have two sorts of write-protection, a) the first one * write-protects guest page to sync the guest modification, b) another one is
arch/x86/kvm/mmu/mmu.c +25 -0
@@ -3948,6 +3948,31 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, max_level, true, &pfn); } +kvm_pfn_t kvm_mmu_map_tdp_page (struct kvm_vcpu *vcpu,gpa_t gpa, +u32 error_code,int max_level) +{ +kvm_pfn_t pfn; +int r; + +if (mmu_topup_memory_caches (vcpu, false)) +return KVM_PFN_ERR_FAULT ; + +/* +* Loop on the page fault path to handle the case where an mmu_notifier +* invalidation triggers RET_PF_RETRY. In the normal page fault path, +* KVM needs to resume the guest in case the invalidation changed any +* of the page fault properties, i.e. the gpa or error code. For this +* path, the gpa and error code are fixed by the caller, and the caller +* expects failure if and only if the page fault can't be fixed. +*/ +do { + r =direct_page_fault (vcpu, gpa, error_code, false, max_level, + true, &pfn); + }while (r ==RET_PF_RETRY && !is_error_noslot_pfn (pfn)); +return pfn; +} +EXPORT_SYMBOL_GPL (kvm_mmu_map_tdp_page); +static void nonpaging_init_context (struct kvm_mmu *context) { context->page_fault = nonpaging_page_fault;
Signature · 1 line
2.17.1
PATCH v5 31/45 KVM: x86: Introduce kvm_mmu_get_tdp_walk() for SEV-SNP use
In reply to Brijesh SinghThe SEV-SNP VMs may call the page state change VMGEXIT to add the GPA as private or shared in the RMP table. The page state change VMGEXIT will contain the RMP page level to be used in the RMP entry. If the page level between the TDP and RMP does not match then, it will result in nested-page-fault (RMP violation).
The SEV-SNP VMGEXIT handler will use the kvm_mmu_get_tdp_walk() to get the current page-level in the TDP for the given GPA and calculate a workable page level. If a GPA is mapped as a 4K-page in the TDP, but the guest requested to add the GPA as a 2M in the RMP entry then the 2M request will be broken into 4K-pages to keep the RMP and TDP page-levels in sync.
--- arch/x86/kvm/mmu.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)
arch/x86/kvm/mmu.h +2 -0
@@ -117,6 +117,8 @@ static inline void kvm_mmu_load_pgd(struct kvm_vcpu *vcpu)int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u32 error_code,bool prefault); +bool kvm_mmu_get_tdp_walk (struct kvm_vcpu *vcpu,gpa_t gpa,kvm_pfn_t *pfn,int *level); +static inline int kvm_mmu_do_page_fault (struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa,u32 err,bool prefault) {
arch/x86/kvm/mmu/mmu.c +29 -0
@@ -3973,6 +3973,35 @@ kvm_pfn_t kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, }EXPORT_SYMBOL_GPL (kvm_mmu_map_tdp_page); +bool kvm_mmu_get_tdp_walk (struct kvm_vcpu *vcpu,gpa_t gpa,kvm_pfn_t *pfn,int *level) +{ +u64 sptes[PT64_ROOT_MAX_LEVEL +1 ]; +int leaf, root; + +if (is_tdp_mmu (vcpu->arch.mmu)) + leaf =kvm_tdp_mmu_get_walk (vcpu, gpa, sptes, &root); +else + leaf =get_walk (vcpu, gpa, sptes, &root); + +if (unlikely (leaf <0 )) +return false; + +/* Check if the leaf SPTE is present */ +if (!is_shadow_present_pte (sptes[leaf])) +return false; + + *pfn =spte_to_pfn (sptes[leaf]); +if (leaf >PG_LEVEL_4K ) { +u64 page_mask =KVM_PAGES_PER_HPAGE (leaf) -KVM_PAGES_PER_HPAGE (leaf -1 ); + *pfn |= (gpa_to_gfn (gpa) & page_mask); + } + + *level = leaf; + +return true; +} +EXPORT_SYMBOL_GPL (kvm_mmu_get_tdp_walk); +static void nonpaging_init_context (struct kvm_mmu *context) { context->page_fault = nonpaging_page_fault;
Signature · 1 line
2.17.1
PATCH v5 32/45 KVM: x86: Define RMP page fault error bits for #NPF
In reply to Brijesh SinghWhen 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
@@ -239,8 +239,12 @@ 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_GUEST_ENC_BIT 34 +#define PFERR_GUEST_SIZEM_BIT 35 +#define PFERR_GUEST_VMPL_BIT 36#define PFERR_PRESENT_MASK (1U << PFERR_PRESENT_BIT)#define PFERR_WRITE_MASK (1U << PFERR_WRITE_BIT)
@@ -251,6 +255,10 @@ enum x86_intercept_stage;#define PFERR_SGX_MASK (1U << PFERR_SGX_BIT)#define PFERR_GUEST_FINAL_MASK (1ULL << PFERR_GUEST_FINAL_BIT)#define PFERR_GUEST_PAGE_MASK (1ULL << PFERR_GUEST_PAGE_BIT) +#define PFERR_GUEST_RMP_MASK (1ULL << PFERR_GUEST_RMP_BIT) +#define PFERR_GUEST_ENC_MASK (1ULL << PFERR_GUEST_ENC_BIT) +#define PFERR_GUEST_SIZEM_MASK (1ULL << PFERR_GUEST_SIZEM_BIT) +#define PFERR_GUEST_VMPL_MASK (1ULL << PFERR_GUEST_VMPL_BIT)#define PFERR_NESTED_GUEST_PAGE (PFERR_GUEST_PAGE_MASK | \ PFERR_WRITE_MASK | \
Signature · 1 line
2.17.1
PATCH v5 33/45 KVM: x86: Update page-fault trace to log full 64-bit error code
In reply to Brijesh SinghThe #NPT error code is a 64-bit value but the trace prints only the lower 32-bits. Some of the fault error code (e.g PFERR_GUEST_FINAL_MASK) are available in the upper 32-bits.
--- arch/x86/kvm/trace.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/trace.h +3 -3
@@ -365,12 +365,12 @@ TRACE_EVENT(kvm_inj_exception,* Tracepoint for page fault. */ TRACE_EVENT(kvm_page_fault, - TP_PROTO(unsigned long fault_address, unsigned int error_code), + TP_PROTO(unsigned long fault_address, u64 error_code), TP_ARGS(fault_address, error_code), TP_STRUCT__entry( __field( unsigned long, fault_address ) - __field( unsigned int, error_code ) + __field( u64, error_code ) ), TP_fast_assign(
@@ -378,7 +378,7 @@ TRACE_EVENT(kvm_page_fault, __entry->error_code = error_code; ), -TP_printk ("address %lx error_code %x" , +TP_printk ("address %lx error_code %llx" , __entry->fault_address, __entry->error_code) );
Signature · 1 line
2.17.1
PATCH v5 34/45 KVM: SVM: Do not use long-lived GHCB map while setting scratch area
In reply to Brijesh SinghThe setup_vmgexit_scratch() function may rely on a long-lived GHCB mapping if the GHCB shared buffer area was used for the scratch area. In preparation for eliminating the long-lived GHCB mapping, always allocate a buffer for the scratch area so it can be accessed without the GHCB mapping.
--- arch/x86/kvm/svm/sev.c | 70 +++++++++++++++++++----------------------- arch/x86/kvm/svm/svm.h | 3 +- 2 files changed, 34 insertions(+), 39 deletions(-)
arch/x86/kvm/svm/sev.c +32 -38
@@ -2490,8 +2490,7 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu)__free_page (virt_to_page (svm->vmsa)); skip_vmsa_free: -if (svm->ghcb_sa_free) -kfree (svm->ghcb_sa); +kfree (svm->ghcb_sa); }static void dump_ghcb (struct vcpu_svm *svm)
@@ -2579,6 +2578,9 @@ static void sev_es_sync_from_ghcb(struct vcpu_svm *svm) control->exit_info_1 =ghcb_get_sw_exit_info_1 (ghcb); control->exit_info_2 =ghcb_get_sw_exit_info_2 (ghcb); +/* Copy the GHCB scratch area GPA */ + svm->ghcb_sa_gpa =ghcb_get_sw_scratch (ghcb); +/* Clear the valid entries fields */ memset (ghcb->save.valid_bitmap,0 ,sizeof (ghcb->save.valid_bitmap)); }
@@ -2714,22 +2716,12 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm)if (!svm->ghcb)return ; -if (svm->ghcb_sa_free) { -/* -* The scratch area lives outside the GHCB, so there is a -* buffer that, depending on the operation performed, may -* need to be synced, then freed. -*/ -if (svm->ghcb_sa_sync) { -kvm_write_guest (svm->vcpu.kvm, -ghcb_get_sw_scratch (svm->ghcb), - svm->ghcb_sa, svm->ghcb_sa_len); - svm->ghcb_sa_sync = false; - } - -kfree (svm->ghcb_sa); - svm->ghcb_sa =NULL ; - svm->ghcb_sa_free = false; +/* Sync the scratch buffer area. */ +if (svm->ghcb_sa_sync) { +kvm_write_guest (svm->vcpu.kvm, +ghcb_get_sw_scratch (svm->ghcb), + svm->ghcb_sa, svm->ghcb_sa_len); + svm->ghcb_sa_sync = false; }trace_kvm_vmgexit_exit (svm->vcpu.vcpu_id, svm->ghcb);
@@ -2767,12 +2759,11 @@ void pre_sev_run(struct vcpu_svm *svm, int cpu)static bool setup_vmgexit_scratch (struct vcpu_svm *svm,bool sync,u64 len) {struct vmcb_control_area *control = &svm->vmcb->control; -struct ghcb *ghcb = svm->ghcb;u64 ghcb_scratch_beg, ghcb_scratch_end;u64 scratch_gpa_beg, scratch_gpa_end;void *scratch_va; - scratch_gpa_beg =ghcb_get_sw_scratch (ghcb); + scratch_gpa_beg = svm->ghcb_sa_gpa;if (!scratch_gpa_beg) {pr_err ("vmgexit: scratch gpa not provided\n" );return false;
@@ -2802,9 +2793,6 @@ static bool setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) scratch_gpa_beg, scratch_gpa_end);return false; } - - scratch_va = (void *)svm->ghcb; - scratch_va += (scratch_gpa_beg - control->ghcb_gpa); }else {/* * The guest memory must be read into a kernel buffer, so
@@ -2815,29 +2803,35 @@ static bool setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) len, GHCB_SCRATCH_AREA_LIMIT);return false; } + } + +if (svm->ghcb_sa_alloc_len < len) { scratch_va =kzalloc (len,GFP_KERNEL_ACCOUNT );if (!scratch_va)return false; -if (kvm_read_guest (svm->vcpu.kvm, scratch_gpa_beg, scratch_va, len)) { -/* Unable to copy scratch area from guest */ -pr_err ("vmgexit: kvm_read_guest for scratch area failed\n" ); - -kfree (scratch_va); -return false; - } -/* -* The scratch area is outside the GHCB. The operation will -* dictate whether the buffer needs to be synced before running -* the vCPU next time (i.e. a read was requested so the data -* must be written back to the guest memory). +* Free the old scratch area and switch to using newly +* allocated. */ - svm->ghcb_sa_sync = sync; - svm->ghcb_sa_free = true; +kfree (svm->ghcb_sa); + + svm->ghcb_sa_alloc_len = len; + svm->ghcb_sa = scratch_va; } - svm->ghcb_sa = scratch_va; +if (kvm_read_guest (svm->vcpu.kvm, scratch_gpa_beg, svm->ghcb_sa, len)) { +/* Unable to copy scratch area from guest */ +pr_err ("vmgexit: kvm_read_guest for scratch area failed\n" ); +return false; + } + +/* +* The operation will dictate whether the buffer needs to be synced +* before running the vCPU next time (i.e. a read was requested so +* the data must be written back to the guest memory). +*/ + svm->ghcb_sa_sync = sync; svm->ghcb_sa_len = len;return true;
arch/x86/kvm/svm/svm.h +2 -1
@@ -199,8 +199,9 @@ struct vcpu_svm {/* SEV-ES scratch area support */ void *ghcb_sa;u64 ghcb_sa_len; +u64 ghcb_sa_gpa; +u32 ghcb_sa_alloc_len;bool ghcb_sa_sync; -bool ghcb_sa_free;bool guest_state_loaded; };
Signature · 1 line
2.17.1
PATCH v5 35/45 KVM: SVM: Remove the long-lived GHCB host map
In reply to Brijesh SinghOn VMGEXIT, sev_handle_vmgexit() creates a host mapping for the GHCB GPA, and unmaps it just before VM-entry. This long-lived GHCB map is used by the VMGEXIT handler through accessors such as ghcb_{set_get}_xxx().
A long-lived GHCB map can cause issue when SEV-SNP is enabled. When SEV-SNP is enabled the mapped GPA needs to be protected against a page state change.
To eliminate the long-lived GHCB mapping, update the GHCB sync operations to explicitly map the GHCB before access and unmap it after access is complete. This requires that the setting of the GHCBs sw_exit_info_{1,2} fields be done during sev_es_sync_to_ghcb(), so create two new fields in the vcpu_svm struct to hold these values when required to be set outside of the GHCB mapping.
--- arch/x86/kvm/svm/sev.c | 129 ++++++++++++++++++++++++++--------------- arch/x86/kvm/svm/svm.c | 12 ++-- arch/x86/kvm/svm/svm.h | 24 +++++++- 3 files changed, 111 insertions(+), 54 deletions(-)
arch/x86/kvm/svm/sev.c +83 -46
@@ -2493,15 +2493,40 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu)kfree (svm->ghcb_sa); } +static inline int svm_map_ghcb (struct vcpu_svm *svm,struct kvm_host_map *map) +{ +struct vmcb_control_area *control = &svm->vmcb->control; +u64 gfn =gpa_to_gfn (control->ghcb_gpa); + +if (kvm_vcpu_map (&svm->vcpu, gfn, map)) { +/* Unable to map GHCB from guest */ +pr_err ("error mapping GHCB GFN [%#llx] from guest\n" , gfn); +return -EFAULT ; + } + +return 0 ; +} + +static inline void svm_unmap_ghcb (struct vcpu_svm *svm,struct kvm_host_map *map) +{ +kvm_vcpu_unmap (&svm->vcpu, map, true); +} +static void dump_ghcb (struct vcpu_svm *svm) { -struct ghcb *ghcb = svm->ghcb; +struct kvm_host_map map;unsigned int nbits; +struct ghcb *ghcb; + +if (svm_map_ghcb (svm, &map)) +return ; + + ghcb = map.hva;/* Re-use the dump_invalid_vmcb module parameter */ if (!dump_invalid_vmcb) {pr_warn_ratelimited ("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n" ); -return ; + goto e_unmap; } nbits =sizeof (ghcb->save.valid_bitmap) *8 ;
@@ -2516,12 +2541,21 @@ static void dump_ghcb(struct vcpu_svm *svm)pr_err ("%-20s%016llx is_valid: %u\n" ,"sw_scratch" , ghcb->save.sw_scratch,ghcb_sw_scratch_is_valid (ghcb));pr_err ("%-20s%*pb\n" ,"valid_bitmap" , nbits, ghcb->save.valid_bitmap); + +e_unmap: +svm_unmap_ghcb (svm, &map); } -static void sev_es_sync_to_ghcb (struct vcpu_svm *svm) +static bool sev_es_sync_to_ghcb (struct vcpu_svm *svm) {struct kvm_vcpu *vcpu = &svm->vcpu; -struct ghcb *ghcb = svm->ghcb; +struct kvm_host_map map; +struct ghcb *ghcb; + +if (svm_map_ghcb (svm, &map)) +return false; + + ghcb = map.hva;/* * The GHCB protocol so far allows for the following data
@@ -2535,13 +2569,24 @@ static void sev_es_sync_to_ghcb(struct vcpu_svm *svm)ghcb_set_rbx (ghcb, vcpu->arch.regs[VCPU_REGS_RBX ]);ghcb_set_rcx (ghcb, vcpu->arch.regs[VCPU_REGS_RCX ]);ghcb_set_rdx (ghcb, vcpu->arch.regs[VCPU_REGS_RDX ]); + +/* +* Copy the return values from the exit_info_{1,2}. +*/ +ghcb_set_sw_exit_info_1 (ghcb, svm->ghcb_sw_exit_info_1); +ghcb_set_sw_exit_info_2 (ghcb, svm->ghcb_sw_exit_info_2); + +trace_kvm_vmgexit_exit (svm->vcpu.vcpu_id, ghcb); + +svm_unmap_ghcb (svm, &map); + +return true; } -static void sev_es_sync_from_ghcb (struct vcpu_svm *svm) +static void sev_es_sync_from_ghcb (struct vcpu_svm *svm,struct ghcb *ghcb) {struct vmcb_control_area *control = &svm->vmcb->control;struct kvm_vcpu *vcpu = &svm->vcpu; -struct ghcb *ghcb = svm->ghcb;u64 exit_code;/*
@@ -2585,13 +2630,18 @@ static void sev_es_sync_from_ghcb(struct vcpu_svm *svm)memset (ghcb->save.valid_bitmap,0 ,sizeof (ghcb->save.valid_bitmap)); } -static int sev_es_validate_vmgexit (struct vcpu_svm *svm) +static int sev_es_validate_vmgexit (struct vcpu_svm *svm,u64 *exit_code) { -struct kvm_vcpu *vcpu; +struct kvm_vcpu *vcpu = &svm->vcpu; +struct kvm_host_map map;struct ghcb *ghcb; -u64 exit_code =0 ; - ghcb = svm->ghcb; +if (svm_map_ghcb (svm, &map)) +return -EFAULT ; + + ghcb = map.hva; + +trace_kvm_vmgexit_enter (vcpu->vcpu_id, ghcb);/* Only GHCB Usage code 0 is supported */ if (ghcb->ghcb_usage)
@@ -2601,7 +2651,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm)* Retrieve the exit code now even though is may not be marked valid * as it could help with debugging. */ - exit_code =ghcb_get_sw_exit_code (ghcb); + *exit_code =ghcb_get_sw_exit_code (ghcb); if (!ghcb_sw_exit_code_is_valid(ghcb) || !ghcb_sw_exit_info_1_is_valid(ghcb) ||
@@ -2685,6 +2735,9 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) goto vmgexit_err; } +sev_es_sync_from_ghcb (svm, ghcb); + +svm_unmap_ghcb (svm, &map);return 0 ; vmgexit_err:
@@ -2695,16 +2748,17 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) ghcb->ghcb_usage); }else {vcpu_unimpl (vcpu,"vmgexit: exit reason %#llx is not valid\n" , - exit_code); + *exit_code);dump_ghcb (svm); } vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; vcpu->run->internal.suberror =KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON ; vcpu->run->internal.ndata =2 ; - vcpu->run->internal.data[0 ] = exit_code; + vcpu->run->internal.data[0 ] = *exit_code; vcpu->run->internal.data[1 ] = vcpu->arch.last_vmentry_cpu; +svm_unmap_ghcb (svm, &map);return -EINVAL ; }
@@ -2713,23 +2767,20 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm)/* Clear any indication that the vCPU is in a type of AP Reset Hold */ svm->ap_reset_hold_type =AP_RESET_HOLD_NONE ; -if (!svm->ghcb) +if (!svm->ghcb_in_use)return ;/* Sync the scratch buffer area. */ if (svm->ghcb_sa_sync) {kvm_write_guest (svm->vcpu.kvm, -ghcb_get_sw_scratch (svm->ghcb), + svm->ghcb_sa_gpa, svm->ghcb_sa, svm->ghcb_sa_len); svm->ghcb_sa_sync = false; } -trace_kvm_vmgexit_exit (svm->vcpu.vcpu_id, svm->ghcb); -sev_es_sync_to_ghcb (svm); -kvm_vcpu_unmap (&svm->vcpu, &svm->ghcb_map, true); - svm->ghcb =NULL ; + svm->ghcb_in_use = false; }void pre_sev_run (struct vcpu_svm *svm,int cpu)
@@ -2961,7 +3012,6 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)struct vcpu_svm *svm =to_svm (vcpu);struct vmcb_control_area *control = &svm->vmcb->control;u64 ghcb_gpa, exit_code; -struct ghcb *ghcb;int ret;/* Validate the GHCB */
@@ -2974,27 +3024,14 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)return -EINVAL ; } -if (kvm_vcpu_map (vcpu, ghcb_gpa >>PAGE_SHIFT , &svm->ghcb_map)) { -/* Unable to map GHCB from guest */ -vcpu_unimpl (vcpu,"vmgexit: error mapping GHCB [%#llx] from guest\n" , - ghcb_gpa); -return -EINVAL ; - } - - svm->ghcb = svm->ghcb_map.hva; - ghcb = svm->ghcb_map.hva; - -trace_kvm_vmgexit_enter (vcpu->vcpu_id, ghcb); - - exit_code =ghcb_get_sw_exit_code (ghcb); - - ret =sev_es_validate_vmgexit (svm); + ret =sev_es_validate_vmgexit (svm, &exit_code);if (ret)return ret; -sev_es_sync_from_ghcb (svm); -ghcb_set_sw_exit_info_1 (ghcb,0 ); -ghcb_set_sw_exit_info_2 (ghcb,0 ); + svm->ghcb_in_use = true; + +svm_set_ghcb_sw_exit_info_1 (vcpu,0 ); +svm_set_ghcb_sw_exit_info_2 (vcpu,0 ); ret = -EINVAL ;switch (exit_code) {
@@ -3033,23 +3070,23 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)break ;case 1 :/* Get AP jump table address */ -ghcb_set_sw_exit_info_2 (ghcb, sev->ap_jump_table); +svm_set_ghcb_sw_exit_info_2 (vcpu, sev->ap_jump_table);break ;default :pr_err ("svm: vmgexit: unsupported AP jump table request - exit_info_1=%#llx\n" , control->exit_info_1); -ghcb_set_sw_exit_info_1 (ghcb,1 ); -ghcb_set_sw_exit_info_2 (ghcb, -X86_TRAP_UD | -SVM_EVTINJ_TYPE_EXEPT | -SVM_EVTINJ_VALID ); +svm_set_ghcb_sw_exit_info_1 (vcpu,1 ); +svm_set_ghcb_sw_exit_info_2 (vcpu, +X86_TRAP_UD | +SVM_EVTINJ_TYPE_EXEPT | +SVM_EVTINJ_VALID ); } ret =1 ;break ; }case SVM_VMGEXIT_HV_FEATURES : { -ghcb_set_sw_exit_info_2 (ghcb,GHCB_HV_FT_SUPPORTED ); +svm_set_ghcb_sw_exit_info_2 (vcpu,GHCB_HV_FT_SUPPORTED ); ret =1 ;break ;
@@ -3171,7 +3208,7 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)* Return from an AP Reset Hold VMGEXIT, where the guest will * set the CS and RIP. Set SW_EXIT_INFO_2 to a non-zero value. */ -ghcb_set_sw_exit_info_2 (svm->ghcb,1 ); +svm_set_ghcb_sw_exit_info_2 (vcpu,1 );break ;case AP_RESET_HOLD_MSR_PROTO :/*
arch/x86/kvm/svm/svm.c +6 -6
@@ -2786,14 +2786,14 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)static int svm_complete_emulated_msr (struct kvm_vcpu *vcpu,int err) {struct vcpu_svm *svm =to_svm (vcpu); -if (!err || !sev_es_guest (vcpu->kvm) ||WARN_ON_ONCE (!svm->ghcb)) +if (!err || !sev_es_guest (vcpu->kvm) ||WARN_ON_ONCE (!svm->ghcb_in_use))return kvm_complete_insn_gp (vcpu, err); -ghcb_set_sw_exit_info_1 (svm->ghcb,1 ); -ghcb_set_sw_exit_info_2 (svm->ghcb, -X86_TRAP_GP | -SVM_EVTINJ_TYPE_EXEPT | -SVM_EVTINJ_VALID ); +svm_set_ghcb_sw_exit_info_1 (vcpu,1 ); +svm_set_ghcb_sw_exit_info_2 (vcpu, +X86_TRAP_GP | +SVM_EVTINJ_TYPE_EXEPT | +SVM_EVTINJ_VALID );return 1 ; }
arch/x86/kvm/svm/svm.h +22 -2
@@ -191,8 +191,7 @@ struct vcpu_svm {/* SEV-ES support */ struct sev_es_save_area *vmsa; -struct ghcb *ghcb; -struct kvm_host_map ghcb_map; +bool ghcb_in_use;bool received_first_sipi;unsigned int ap_reset_hold_type;
@@ -204,6 +203,13 @@ struct vcpu_svm {bool ghcb_sa_sync;bool guest_state_loaded; + +/* +* SEV-ES support to hold the sw_exit_info return values to be +* sync'ed to the GHCB when mapped. +*/ +u64 ghcb_sw_exit_info_1; +u64 ghcb_sw_exit_info_2; };struct svm_cpu_data {
@@ -503,6 +509,20 @@ void nested_sync_control_from_vmcb02(struct vcpu_svm *svm);void nested_vmcb02_compute_g_pat (struct vcpu_svm *svm);void svm_switch_vmcb (struct vcpu_svm *svm,struct kvm_vmcb_info *target_vmcb); +static inline void svm_set_ghcb_sw_exit_info_1 (struct kvm_vcpu *vcpu,u64 val) +{ +struct vcpu_svm *svm =to_svm (vcpu); + + svm->ghcb_sw_exit_info_1 = val; +} + +static inline void svm_set_ghcb_sw_exit_info_2 (struct kvm_vcpu *vcpu,u64 val) +{ +struct vcpu_svm *svm =to_svm (vcpu); + + svm->ghcb_sw_exit_info_2 = val; +} +extern struct kvm_x86_nested_ops svm_nested_ops;/* avic.c */
Signature · 1 line
2.17.1
PATCH v5 36/45 KVM: SVM: Add support to handle GHCB GPA register VMGEXIT
In reply to Brijesh SinghSEV-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
@@ -2984,6 +2984,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->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;
@@ -3024,6 +3045,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)return -EINVAL ; } +/* 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, &exit_code);if (ret)return ret;
arch/x86/kvm/svm/svm.h +7 -0
@@ -210,6 +210,8 @@ struct vcpu_svm {*/ u64 ghcb_sw_exit_info_1;u64 ghcb_sw_exit_info_2; + +u64 ghcb_registered_gpa; };struct svm_cpu_data {
@@ -266,6 +268,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->ghcb_registered_gpa == val; +} +static inline void vmcb_mark_all_dirty (struct vmcb *vmcb) { vmcb->control.clean =0 ;
Signature · 1 line
2.17.1
PATCH v5 37/45 KVM: SVM: Add support to handle MSR based Page State Change VMGEXIT
In reply to Brijesh SinghSEV-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.
Before changing the page state in the RMP entry, lookup the page in the NPT to make sure that there is a valid mapping for it. If the mapping exist then try to find a workable page level between the NPT and RMP for the page. If the page is not mapped in the NPT, then create a fault such that it gets mapped before we change the page state in the RMP entry.
--- arch/x86/include/asm/sev-common.h | 9 ++ arch/x86/kvm/svm/sev.c | 197 ++++++++++++++++++++++++++++++ arch/x86/kvm/trace.h | 34 ++++++ arch/x86/kvm/x86.c | 1 + 4 files changed, 241 insertions(+)
arch/x86/include/asm/sev-common.h +9 -0
@@ -89,6 +89,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) | \
@@ -98,6 +102,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 +197 -0
215 lines not on this page · raw message
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
@@ -1711,6 +1712,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
@@ -12371,3 +12371,4 @@ 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);
Signature · 1 line
2.17.1
PATCH v5 38/45 KVM: SVM: Add support to handle Page State Change VMGEXIT
In reply to Brijesh SinghSEV-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.
--- arch/x86/include/asm/sev-common.h | 7 +++ arch/x86/kvm/svm/sev.c | 82 +++++++++++++++++++++++++++++-- 2 files changed, 84 insertions(+), 5 deletions(-)
arch/x86/include/asm/sev-common.h +7 -0
@@ -126,6 +126,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 +77 -5
130 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v5 39/45 KVM: SVM: Introduce ops for the post gfn map and unmap
In reply to Brijesh SinghWhen SEV-SNP is enabled in the guest VM, the guest memory pages can either be a private or shared. A write from the hypervisor goes through the RMP checks. If hardware sees that hypervisor is attempting to write to a guest private page, then it triggers an RMP violation #PF.
To avoid the RMP violation, add post_{map,unmap}_gfn() ops that can be used to verify that its safe to map a given guest page. Use the SRCU to protect against the page state change for existing mapped pages.
--- arch/x86/include/asm/kvm-x86-ops.h | 2 + arch/x86/include/asm/kvm_host.h | 4 ++ arch/x86/kvm/svm/sev.c | 69 +++++++++++++++++++++----- arch/x86/kvm/svm/svm.c | 4 ++ arch/x86/kvm/svm/svm.h | 8 +++ arch/x86/kvm/x86.c | 78 +++++++++++++++++++++++++++--- 6 files changed, 146 insertions(+), 19 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +2 -0
@@ -124,6 +124,8 @@ KVM_X86_OP(msr_filter_changed) KVM_X86_OP_NULL(complete_emulated_msr) KVM_X86_OP(alloc_apic_backing_page) KVM_X86_OP_NULL(rmp_page_level_adjust) +KVM_X86_OP(post_map_gfn) +KVM_X86_OP(post_unmap_gfn) #undef KVM_X86_OP#undef KVM_X86_OP_NULL
arch/x86/include/asm/kvm_host.h +4 -0
@@ -1463,7 +1463,11 @@ struct kvm_x86_ops {void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu,u8 vector);void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); +void (*rmp_page_level_adjust)(struct kvm *kvm,kvm_pfn_t pfn,int *level); + +int (*post_map_gfn)(struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int *token); +void (*post_unmap_gfn)(struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int token); };struct kvm_x86_nested_ops {
arch/x86/kvm/svm/sev.c +58 -11
154 lines not on this page · raw message
arch/x86/kvm/svm/svm.c +4 -0
@@ -4679,7 +4679,11 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, .alloc_apic_backing_page = svm_alloc_apic_backing_page, + .rmp_page_level_adjust = sev_rmp_page_level_adjust, + + .post_map_gfn = sev_post_map_gfn, + .post_unmap_gfn = sev_post_unmap_gfn, };static struct kvm_x86_init_ops svm_init_ops__initdata = {
arch/x86/kvm/svm/svm.h +8 -0
@@ -76,16 +76,22 @@ 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 */ +unsigned long pages_locked;/* Number of pages locked */ struct list_head regions_list;/* List of registered regions */ +u64 ap_jump_table;/* SEV-ES AP Jump Table address */ +struct kvm *enc_context_owner;/* Owner of copied encryption context */ struct misc_cg *misc_cg;/* For misc cgroup accounting */ +u64 snp_init_flags;void *snp_context;/* SNP guest context page */ +struct srcu_struct psc_srcu; };struct kvm_svm {
@@ -618,6 +624,8 @@ void sev_es_prepare_guest_switch(struct vcpu_svm *svm, unsigned int cpu);void sev_es_unmap_ghcb (struct vcpu_svm *svm);struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu);void sev_rmp_page_level_adjust (struct kvm *kvm,kvm_pfn_t pfn,int *level); +int sev_post_map_gfn (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int *token); +void sev_post_unmap_gfn (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int token);/* vmenter.S */
arch/x86/kvm/x86.c +70 -8
138 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v5 40/45 KVM: x86: Export the kvm_zap_gfn_range() for the SNP use
In reply to Brijesh SinghWhile resolving the RMP page fault, we may run into 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, we will 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
@@ -1561,6 +1561,8 @@ void kvm_mmu_zap_all(struct kvm *kvm);void kvm_mmu_invalidate_mmio_sptes (struct kvm *kvm,u64 gen);unsigned long kvm_mmu_calculate_default_mmu_pages (struct kvm *kvm);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,struct kvm_mmu *mmu,unsigned long cr3);
arch/x86/kvm/mmu.h +0 -2
@@ -228,8 +228,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
@@ -5748,6 +5748,7 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm,return need_tlb_flush; } +EXPORT_SYMBOL_GPL (kvm_zap_gfn_range);void kvm_mmu_zap_collapsible_sptes (struct kvm *kvm,const struct kvm_memory_slot *memslot)
Signature · 1 line
2.17.1
PATCH v5 41/45 KVM: SVM: Add support to handle the RMP nested page fault
In reply to Brijesh SinghWhen 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.
--- arch/x86/kvm/svm/sev.c | 76 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 14 +++++--- arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 87 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/sev.c +76 -0
@@ -3651,3 +3651,79 @@ void sev_post_unmap_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int token)srcu_read_unlock (&sev->psc_srcu, token); } + +void handle_rmp_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u64 error_code) +{ +int rmp_level, npt_level, rc, assigned; +struct kvm *kvm = vcpu->kvm; +gfn_t gfn =gpa_to_gfn (gpa); +bool need_psc = false; +enum psc_op psc_op; +kvm_pfn_t pfn; +bool private; + +write_lock (&kvm->mmu_lock); + +if (unlikely (!kvm_mmu_get_tdp_walk (vcpu, gpa, &pfn, &npt_level))) + goto unlock; + + assigned =snp_lookup_rmpentry (pfn, &rmp_level); +if (unlikely (assigned <0 )) + goto unlock; + + private = !!(error_code &PFERR_GUEST_ENC_MASK ); + +/* +* If the fault was due to size mismatch, or NPT and RMP page level's +* are not in sync, then use PSMASH to split the RMP entry into 4K. +*/ +if ((error_code &PFERR_GUEST_SIZEM_MASK ) || + (npt_level ==PG_LEVEL_4K && rmp_level ==PG_LEVEL_2M && private)) { + rc =snp_rmptable_psmash (kvm, pfn); +if (rc) +pr_err_ratelimited ("psmash failed, gpa 0x%llx pfn 0x%llx rc %d\n" , + gpa, pfn, rc); + goto out; + } + +/* +* If it's a private access, and the page is not assigned in the +* RMP table, create a new private RMP entry. This can happen if +* guest did not use the PSC VMGEXIT to transition the page state +* before the access. +*/ +if (!assigned && private) { + need_psc =1 ; + psc_op =SNP_PAGE_STATE_PRIVATE ; + goto out; + } + +/* +* If it's a shared access, but the page is private in the RMP table +* then make the page shared in the RMP table. This can happen if +* the guest did not use the PSC VMGEXIT to transition the page +* state before the access. +*/ +if (assigned && !private) { + need_psc =1 ; + psc_op =SNP_PAGE_STATE_SHARED ; + } + +out: +write_unlock (&kvm->mmu_lock); + +if (need_psc) + rc =__snp_handle_page_state_change (vcpu, psc_op, gpa,PG_LEVEL_4K ); + +/* +* The fault handler has updated the RMP pagesize, zap the existing +* rmaps for large entry ranges so that nested page table gets rebuilt +* with the updated RMP pagesize. +*/ + gfn =gpa_to_gfn (gpa) & ~(KVM_PAGES_PER_HPAGE (PG_LEVEL_2M ) -1 ); +kvm_zap_gfn_range (kvm, gfn, gfn +PTRS_PER_PMD ); +return ; + +unlock: +write_unlock (&kvm->mmu_lock); +}
arch/x86/kvm/svm/svm.c +10 -4
@@ -1933,15 +1933,21 @@ 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 (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); + +if (error_code &PFERR_GUEST_RMP_MASK ) +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
@@ -626,6 +626,7 @@ struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu);void sev_rmp_page_level_adjust (struct kvm *kvm,kvm_pfn_t pfn,int *level);int sev_post_map_gfn (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int *token);void sev_post_unmap_gfn (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int token); +void handle_rmp_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u64 error_code);/* vmenter.S */
Signature · 1 line
2.17.1
PATCH v5 42/45 KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event
In reply to Brijesh SinghVersion 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 | 197 +++++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/svm/svm.h | 2 + 2 files changed, 193 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/sev.c +191 -6
@@ -19,6 +19,7 @@#include <linux/trace_events.h> #include <linux/sev.h> #include <linux/ksm.h> +#include <linux/sev-guest.h> #include <asm/fpu/internal.h> #include <asm/pkru.h>
@@ -338,6 +339,7 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)init_srcu_struct (&sev->psc_srcu); ret =sev_snp_init (&argp->error); +mutex_init (&sev->guest_req_lock); }else { ret =sev_platform_init (&argp->error); }
@@ -1602,23 +1604,39 @@ static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)static void *snp_context_create (struct kvm *kvm,struct kvm_sev_cmd *argp) { +void *context =NULL , *certs_data =NULL , *resp_page =NULL ; +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;struct sev_data_snp_gctx_create data = {}; -void *context;int rc; +/* Allocate memory used for the certs data in SNP guest request */ + certs_data =kmalloc (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; + +/* Allocate a firmware buffer used during the guest command handling. */ + resp_page =snp_alloc_firmware_page (GFP_KERNEL_ACCOUNT ); +if (!resp_page) + 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)
@@ -2248,6 +2266,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 ; }
@@ -2746,6 +2766,8 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm, u64 *exit_code)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 : goto vmgexit_err;
@@ -3161,6 +3183,155 @@ static unsigned long snp_handle_page_state_change(struct vcpu_svm *svm)return rc ?map_to_psc_vmgexit_code (rc) :0 ; } +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: +svm_set_ghcb_sw_exit_info_2 (vcpu, 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; + } + +/* Verify that requested blob will fit in certificate buffer */ +if ((data_npages <<PAGE_SHIFT ) >SEV_FW_BLOB_MAX_SIZE ) { + rc =SEV_RET_INVALID_PARAM ; + 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: +svm_set_ghcb_sw_exit_info_2 (vcpu, rc); +} +static int sev_handle_vmgexit_msr_protocol (struct vcpu_svm *svm) {struct vmcb_control_area *control = &svm->vmcb->control;
@@ -3404,6 +3575,20 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)svm_set_ghcb_sw_exit_info_2 (vcpu, rc);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
@@ -92,6 +92,8 @@ struct kvm_sev_info {u64 snp_init_flags;void *snp_context;/* SNP guest context page */ struct srcu_struct psc_srcu; +void *snp_certs_data; +struct mutex guest_req_lock; };struct kvm_svm {
Signature · 1 line
2.17.1
PATCH v5 43/45 KVM: SVM: Use a VMSA physical address variable for populating VMCB
In reply to Brijesh SinghFrom: 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
@@ -3619,10 +3619,9 @@ 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->vmsa); + svm->vmcb->control.vmsa_pa = svm->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
@@ -1409,9 +1409,16 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu) svm->vmcb01.ptr =page_address (vmcb01_page); svm->vmcb01.pa =__sme_set (page_to_pfn (vmcb01_page) <<PAGE_SHIFT ); -if (vmsa_page) +if (vmsa_page) { svm->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->vmsa_pa =__pa (svm->vmsa); + } + svm->guest_state_loaded = false;svm_switch_vmcb (svm, &svm->vmcb01);
arch/x86/kvm/svm/svm.h +1 -0
@@ -199,6 +199,7 @@ struct vcpu_svm {/* SEV-ES support */ struct sev_es_save_area *vmsa; +hpa_t vmsa_pa;bool ghcb_in_use;bool received_first_sipi;unsigned int ap_reset_hold_type;
Signature · 1 line
2.17.1
PATCH v5 44/45 KVM: SVM: Support SEV-SNP AP Creation NAE event
In reply to Brijesh SinghFrom: 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-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 3 + arch/x86/include/asm/svm.h | 7 +- arch/x86/kvm/svm/sev.c | 211 +++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 6 +- arch/x86/kvm/svm/svm.h | 9 ++ arch/x86/kvm/x86.c | 13 +- 7 files changed, 247 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -126,6 +126,7 @@ KVM_X86_OP(alloc_apic_backing_page) KVM_X86_OP_NULL(rmp_page_level_adjust) KVM_X86_OP(post_map_gfn) KVM_X86_OP(post_unmap_gfn) +KVM_X86_OP(update_protected_guest_state)#undef KVM_X86_OP#undef KVM_X86_OP_NULL
arch/x86/include/asm/kvm_host.h +3 -0
@@ -91,6 +91,7 @@#define KVM_REQ_MSR_FILTER_CHANGED KVM_ARCH_REQ(29)#define KVM_REQ_UPDATE_CPU_DIRTY_LOGGING \ KVM_ARCH_REQ_FLAGS(30, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) +#define KVM_REQ_UPDATE_PROTECTED_GUEST_STATE KVM_ARCH_REQ(31)#define CR0_RESERVED_BITS \ (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
@@ -1468,6 +1469,8 @@ struct kvm_x86_ops {int (*post_map_gfn)(struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int *token);void (*post_unmap_gfn)(struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int token); + +int (*update_protected_guest_state)(struct kvm_vcpu *vcpu); };struct kvm_x86_nested_ops {
arch/x86/include/asm/svm.h +6 -1
@@ -218,7 +218,12 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define SVM_NESTED_CTL_SEV_ENABLE BIT(1)#define SVM_NESTED_CTL_SEV_ES_ENABLE BIT(2) -#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 +211 -0
247 lines not on this page · raw message
arch/x86/kvm/svm/svm.c +5 -1
@@ -1336,7 +1336,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) { +if (init_event) { +sev_snp_init_protected_guest_state (vcpu); + }else { vcpu->arch.apic_base =APIC_DEFAULT_PHYS_BASE |MSR_IA32_APICBASE_ENABLE ;if (kvm_vcpu_is_reset_bsp (vcpu))
@@ -4697,6 +4699,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .post_map_gfn = sev_post_map_gfn, .post_unmap_gfn = sev_post_unmap_gfn, + + .update_protected_guest_state = sev_snp_update_protected_guest_state, };static struct kvm_x86_init_ops svm_init_ops__initdata = {
arch/x86/kvm/svm/svm.h +9 -0
@@ -94,6 +94,8 @@ struct kvm_sev_info {struct srcu_struct psc_srcu;void *snp_certs_data;struct mutex guest_req_lock; + +u64 sev_features;/* Features set at VMSA creation */ };struct kvm_svm {
@@ -221,6 +223,11 @@ struct vcpu_svm {u64 ghcb_sw_exit_info_2;u64 ghcb_registered_gpa; + +struct mutex snp_vmsa_mutex; +gpa_t snp_vmsa_gpa; +kvm_pfn_t snp_vmsa_pfn; +bool snp_vmsa_update_on_init;/* SEV-SNP AP Creation on INIT-SIPI */ };struct svm_cpu_data {
@@ -630,6 +637,8 @@ void sev_rmp_page_level_adjust(struct kvm *kvm, kvm_pfn_t pfn, int *level);int sev_post_map_gfn (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int *token);void sev_post_unmap_gfn (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int token);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_snp_update_protected_guest_state (struct kvm_vcpu *vcpu);/* vmenter.S */
arch/x86/kvm/x86.c +12 -1
@@ -9576,6 +9576,16 @@ 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)) { + r =static_call (kvm_x86_update_protected_guest_state)(vcpu); +if (!r) { + vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; + goto out; + }else if (vcpu->arch.mp_state !=KVM_MP_STATE_RUNNABLE ) { + goto out; + } + } } if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win ||
@@ -11656,7 +11666,8 @@ static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)if (!list_empty_careful (&vcpu->async_pf.done))return true; -if (kvm_apic_has_events (vcpu)) +if (kvm_apic_has_events (vcpu) || +kvm_test_request (KVM_REQ_UPDATE_PROTECTED_GUEST_STATE , vcpu))return true;if (vcpu->arch.pv.pv_unhalted)
Signature · 1 line
2.17.1
PATCH v5 45/45 KVM: SVM: Add module parameter to enable the SEV-SNP
In reply to Brijesh SinghAdd 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
@@ -59,14 +59,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
@@ -608,7 +608,7 @@ void svm_vcpu_unblocking(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.17.1
Re: [PATCH Part2 v5 08/45] x86/fault: Add support to handle the RMP fault for user address
In reply to Brijesh Singhmm/memory.c static vm_fault_t handle_pte_fault(struct vm_fault *vmf) · line 4599
Quoted 8 lines · Brijesh Singh
mm/memory.c +8
-0+static int handle_split_page_fault (struct vm_fault *vmf) +{ +if (!IS_ENABLED (CONFIG_AMD_MEM_ENCRYPT )) +return VM_FAULT_SIGBUS ; + +__split_huge_pmd (vmf->vma, vmf->pmd, vmf->address, false,NULL ); +return 0 ; +}
We had a whole conversation the last time this was posted about huge page types *other* than THP. I don't see any comprehension of those types or what would happen if one of those was used with SEV-SNP.
What was the result of those review comments?
I'm still worried that hugetlbfs (and others) are not properly handled by this series.
Re: [PATCH Part2 v5 08/45] x86/fault: Add support to handle the RMP fault for user address
Hi Dave,
Quoted 14 lines · Dave Hansen
mm/memory.c static vm_fault_t handle_pte_fault(struct vm_fault *vmf) · line 4599
Quoted 8 lines · Brijesh Singh
mm/memory.c +8
-0+static int handle_split_page_fault (struct vm_fault *vmf) +{ +if (!IS_ENABLED (CONFIG_AMD_MEM_ENCRYPT )) +return VM_FAULT_SIGBUS ; + +__split_huge_pmd (vmf->vma, vmf->pmd, vmf->address, false,NULL ); +return 0 ; +}We had a whole conversation the last time this was posted about huge page types *other* than THP. I don't see any comprehension of those types or what would happen if one of those was used with SEV-SNP.
Quoted 2 lines
What was the result of those review comments?
Based on previous review comments Sean was not keen on KVM having perform this detection and abort the guest SEV-SNP VM launch. So, I didn't implemented the check and waiting for more discussion before going at it.
SEV-SNP guest requires the VMM to register the guest backing pages before the VM launch. Personally, I would prefer KVM to check the backing page type during the registration and fail to register if its hugetlbfs (and others) to avoid us get into situation where we could not split the hugepage.
thanks
Quoted 3 lines
I'm still worried that hugetlbfs (and others) are not properly handled by this series.
Re: [PATCH Part2 v5 08/45] x86/fault: Add support to handle the RMP fault for user address
Quoted 15 lines · Brijesh Singh
Quoted 14 lines · Dave Hansen
mm/memory.c static vm_fault_t handle_pte_fault(struct vm_fault *vmf) · line 4599
Quoted 8 lines · Brijesh Singh
mm/memory.c +8
-0+static int handle_split_page_fault (struct vm_fault *vmf) +{ + if (!IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) + return VM_FAULT_SIGBUS; + + __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL); + return 0; +}We had a whole conversation the last time this was posted about huge page types *other* than THP. I don't see any comprehension of those types or what would happen if one of those was used with SEV-SNP.
Quoted 6 lines
Quoted 1 line
What was the result of those review comments?
Based on previous review comments Sean was not keen on KVM having perform this detection and abort the guest SEV-SNP VM launch. So, I didn't implemented the check and waiting for more discussion before going at it.
OK. But, you need to *acknowledge* the situation somewhere. Maybe the cover letter of the series, maybe in this changelog.
As it stands, it looks like you're simply ignoring _some_ reviewer feedback.
Quoted 5 lines
SEV-SNP guest requires the VMM to register the guest backing pages before the VM launch. Personally, I would prefer KVM to check the backing page type during the registration and fail to register if its hugetlbfs (and others) to avoid us get into situation where we could not split the hugepage.
It *has* to be done in KVM, IMNHO.
The core kernel really doesn't know much about SEV. It *really* doesn't know when its memory is being exposed to a virtualization architecture that doesn't know how to split TLBs like every single one before it.
This essentially *must* be done at the time that the KVM code realizes that it's being asked to shove a non-splittable page mapping into the SEV hardware structures.
The only other alternative is raising a signal from the fault handler when the page can't be split. That's a *LOT* nastier because it's so much later in the process.
It's either that, or figure out a way to split hugetlbfs (and DAX) mappings in a failsafe way.
Re: [PATCH Part2 v5 08/45] x86/fault: Add support to handle the RMP fault for user address
Quoted 10 lines · Dave Hansen
It *has* to be done in KVM, IMNHO.
The core kernel really doesn't know much about SEV. It *really* doesn't know when its memory is being exposed to a virtualization architecture that doesn't know how to split TLBs like every single one before it.
This essentially *must* be done at the time that the KVM code realizes that it's being asked to shove a non-splittable page mapping into the SEV hardware structures.
Quoted 6 lines
The only other alternative is raising a signal from the fault handler when the page can't be split. That's a *LOT* nastier because it's so much later in the process.
It's either that, or figure out a way to split hugetlbfs (and DAX) mappings in a failsafe way.
Yes, I agree with that. KVM needs a check to disallow HugeTLB pages in SEV-SNP guests, at least as a temporary workaround. When HugeTLBfs mappings can be split into smaller pages the check can be removed.
Regards,
Joerg
Re: [PATCH Part2 v5 08/45] x86/fault: Add support to handle the RMP fault for user address
Quoted 15 lines · Joerg Roedel
Quoted 14 lines · Dave Hansen
It *has* to be done in KVM, IMNHO.
The core kernel really doesn't know much about SEV. It *really* doesn't know when its memory is being exposed to a virtualization architecture that doesn't know how to split TLBs like every single one before it.
This essentially *must* be done at the time that the KVM code realizes that it's being asked to shove a non-splittable page mapping into the SEV hardware structures.
The only other alternative is raising a signal from the fault handler when the page can't be split. That's a *LOT* nastier because it's so much later in the process.
Quoted 6 lines
Quoted 2 lines
It's either that, or figure out a way to split hugetlbfs (and DAX) mappings in a failsafe way.
Yes, I agree with that. KVM needs a check to disallow HugeTLB pages in SEV-SNP guests, at least as a temporary workaround. When HugeTLBfs mappings can be split into smaller pages the check can be removed.
FTR, this is Sean's reply with concerns in v4:
https://lore.kernel.org/linux-coco/YPCuTiNET%[email protected]/
I think there are two main arguments there:
- it's not KVM business to decide
- guest may do all page state changes with 2mb granularity so it might be fine with hugetlb
The latter might become true, but I think it's more probable that sooner hugetlbfs will learn to split the mappings to base pages - I know people plan to work on that. At that point qemu will have to recognize if the host kernel is the new one that can do this splitting vs older one that can't. Preferably without relying on kernel version number, as backports exist. Thus, trying to register a hugetlbfs range that either is rejected (kernel can't split) or passes (kernel can split) seems like a straightforward way. So I'm also in favor of adding that, hopefuly temporary, check.
Vlastimil
Quoted 4 lines
Regards,
Joerg
Tom Lendacky
maintainerRe: [PATCH Part2 v5 08/45] x86/fault: Add support to handle the RMP fault for user address
Quoted 31 lines · Vlastimil Babka
Quoted 15 lines · Joerg Roedel
Quoted 14 lines · Dave Hansen
It *has* to be done in KVM, IMNHO.
The core kernel really doesn't know much about SEV. It *really* doesn't know when its memory is being exposed to a virtualization architecture that doesn't know how to split TLBs like every single one before it.
This essentially *must* be done at the time that the KVM code realizes that it's being asked to shove a non-splittable page mapping into the SEV hardware structures.
The only other alternative is raising a signal from the fault handler when the page can't be split. That's a *LOT* nastier because it's so much later in the process.
Quoted 6 lines
Quoted 2 lines
It's either that, or figure out a way to split hugetlbfs (and DAX) mappings in a failsafe way.
Yes, I agree with that. KVM needs a check to disallow HugeTLB pages in SEV-SNP guests, at least as a temporary workaround. When HugeTLBfs mappings can be split into smaller pages the check can be removed.
I think there are two main arguments there:
- it's not KVM business to decide
- guest may do all page state changes with 2mb granularity so it might be fine with hugetlb
Quoted 8 lines
The latter might become true, but I think it's more probable that sooner hugetlbfs will learn to split the mappings to base pages - I know people plan to work on that. At that point qemu will have to recognize if the host kernel is the new one that can do this splitting vs older one that can't. Preferably without relying on kernel version number, as backports exist. Thus, trying to register a hugetlbfs range that either is rejected (kernel can't split) or passes (kernel can split) seems like a straightforward way. So I'm also in favor of adding that, hopefuly temporary, check.
If that's the direction taken, I think we'd be able to use a KVM_CAP_ value that can be queried by the VMM to make the determination.
Thanks,
Tom
Quoted 8 lines
Vlastimil
Quoted 4 lines
Regards,
Joerg
Re: [PATCH Part2 v5 17/45] crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command
In reply to Brijesh SinghQuoted 6 lines · Brijesh Singh
+2.4
SNP_SET_EXT_CONFIG
+----------------------
+:Technology: sev-snp
+:Type: hypervisor ioctl cmd
+:Parameters (in): structsev_data_snp_ext_config
+:Returns (out): 0 on success, -negative on error
Documentation/virt/coco/sevguest.rst See GHCB specification for further detail on how to parse the certificate blob. · line 153
Quoted 10 lines
Documentation/virt/coco/sevguest.rst +10
-0+ +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 previous certificate blob will deleted. +For more information on the certificate blob layout, see the GHCB spec +(extended guest request message).
Hi Brijesh,
Just to be clear, is the documentation you're referring to regarding the layout of the certificate blob specified on page 47 of the GHCB spec? More specifically, is it the struct cert_table on that page?
https://developer.amd.com/wp-content/resources/56421.pdf
If so, where is the VCEK certificate layout documented?
Connor
Quoted 14 lines
include/uapi/linux/psp-sev.h +14
-0+/** +* 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 */ +};
Re: [PATCH Part2 v5 17/45] crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command
Quoted 19 lines · Connor Kuehl
Quoted 6 lines · Brijesh Singh
+2.4
SNP_SET_EXT_CONFIG
+----------------------
+:Technology: sev-snp
+:Type: hypervisor ioctl cmd
+:Parameters (in): structsev_data_snp_ext_config
+:Returns (out): 0 on success, -negative on errorDocumentation/virt/coco/sevguest.rst See GHCB specification for further detail on how to parse the certificate blob. · line 153
Quoted 10 lines
Documentation/virt/coco/sevguest.rst +10
-0+ +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 previous certificate blob will deleted. +For more information on the certificate blob layout, see the GHCB spec +(extended guest request message).Hi Brijesh,
Quoted 3 lines
Just to be clear, is the documentation you're referring to regarding the layout of the certificate blob specified on page 47 of the GHCB spec? More specifically, is it the
struct cert_tableon that page?
Yes that is correct.
Quoted 4 lines
If so, where is the VCEK certificate layout documented?
You can get the VCEK from the KDS using the chip id. The certificate is standard X.509.
thanks
Quoted 17 lines
Connor
Quoted 14 lines
include/uapi/linux/psp-sev.h +14
-0+/** +* 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 */ +};
Re: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
In reply to Brijesh SinghHi Brijesh,
Quoted 40 lines · Brijesh Singh
The
KVM_SNP_INITcommand 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 theKVM_SEV_INITorKVM_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).
Co-developed-by Pavan Kumar Paluri <[email protected]>Signed-off-by Pavan Kumar Paluri <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- .../virt/kvm/amd-memory-encryption.rst | 27 ++++++++++++ arch/x86/include/asm/svm.h | 2 + arch/x86/kvm/svm/sev.c | 44 ++++++++++++++++++- arch/x86/kvm/svm/svm.h | 4 ++ include/uapi/linux/kvm.h | 13 ++++++ 5 files changed, 88 insertions(+), 2 deletions(-)Documentation/virt/kvm/amd-memory-encryption.rst +13
-0@@ -427,6 +427,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 {
Documentation/virt/kvm/amd-memory-encryption.rst issued by the hypervisor to make the guest ready for execution. · line 452
Quoted 10 lines
Documentation/virt/kvm/amd-memory-encryption.rst +10
-0+ __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)
Typo in these flags: s/INJET/INJECT/
Also in the actual .h file below.
-Dov
Quoted 158 lines
Documentation/virt/kvm/amd-memory-encryption.rst +4
-0+ +If the specified flags is not supported then return -EOPNOTSUPP, and the supported +flags are returned. + References ==========arch/x86/include/asm/svm.h +2
-0@@ -218,6 +218,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define SVM_NESTED_CTL_SEV_ENABLE BIT(1)#define SVM_NESTED_CTL_SEV_ES_ENABLE BIT(2) +#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +struct vmcb_seg {u16 selector;u16 attrib;arch/x86/kvm/svm/sev.c +42
-2@@ -235,10 +235,30 @@ static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)sev_decommission (handle); } +static int verify_snp_init_flags (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_snp_init params; +int ret =0 ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params))) +return -EFAULT ; + +if (params.flags & ~SEV_SNP_SUPPORTED_FLAGS ) + ret = -EOPNOTSUPP ; + + params.flags =SEV_SNP_SUPPORTED_FLAGS ; + +if (copy_to_user ((void __user *)(uintptr_t )argp->data, ¶ms,sizeof (params))) + ret = -EFAULT ; + +return ret; +} +static int sev_guest_init (struct kvm *kvm,struct kvm_sev_cmd *argp) { +bool es_active = (argp->id ==KVM_SEV_ES_INIT || argp->id ==KVM_SEV_SNP_INIT );struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; -bool es_active = argp->id ==KVM_SEV_ES_INIT ; +bool snp_active = argp->id ==KVM_SEV_SNP_INIT ;int asid, ret;if (kvm->created_vcpus)@@ -249,12 +269,22 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; sev->es_active = es_active; + sev->snp_active = snp_active; asid =sev_asid_new (sev);if (asid <0 ) goto e_no_asid; sev->asid = asid; - ret =sev_platform_init (&argp->error); +if (snp_active) { + ret =verify_snp_init_flags (kvm, argp); +if (ret) + goto e_free; + + ret =sev_snp_init (&argp->error); + }else { + ret =sev_platform_init (&argp->error); + } +if (ret) goto e_free;@@ -600,6 +630,10 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) save->pkru = svm->vcpu.arch.pkru; save->xss = svm->vcpu.arch.ia32_xss; +/* Enable the SEV-SNP feature */ +if (sev_snp_guest (svm->vcpu.kvm)) + save->sev_features |=SVM_SEV_FEAT_SNP_ACTIVE ; +return 0 ; }@@ -1532,6 +1566,12 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp) }switch (sev_cmd.id) { +case KVM_SEV_SNP_INIT : +if (!sev_snp_enabled) { + r = -ENOTTY ; + goto out; + } + fallthrough;case KVM_SEV_ES_INIT :if (!sev_es_enabled) { r = -ENOTTY ;arch/x86/kvm/svm/svm.h +4
-0@@ -69,6 +69,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 */ @@ -81,6 +84,7 @@ struct kvm_sev_info {u64 ap_jump_table;/* SEV-ES AP Jump Table address */ struct kvm *enc_context_owner;/* Owner of copied encryption context */ struct misc_cg *misc_cg;/* For misc cgroup accounting */ +u64 snp_init_flags; };struct kvm_svm {include/uapi/linux/kvm.h +13
-0@@ -1712,6 +1712,9 @@ enum sev_cmd_id {/* Guest Migration Extension */ KVM_SEV_SEND_CANCEL , +/* SNP specific commands */ +KVM_SEV_SNP_INIT , + KVM_SEV_NR_MAX, };@@ -1808,6 +1811,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)
Re: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
Hi Dov,
Quoted 8 lines · Dov Murik
+ /* enable the restricted injection */
Documentation/virt/kvm/amd-memory-encryption.rst issued by the hypervisor to make the guest ready for execution. · line 452
Quoted 4 lines
Documentation/virt/kvm/amd-memory-encryption.rst +4
-0+ #define KVM_SEV_SNP_RESTRICTED_INJET (1<<0) + + /* enable the restricted injection timer */ + #define KVM_SEV_SNP_RESTRICTED_TIMER_INJET (1<<1)Typo in these flags: s/INJET/INJECT/
Also in the actual .h file below.
Noted. thanks
Re: [PATCH Part2 v5 16/45] crypto: ccp: Add the SNP_PLATFORM_STATUS command
In reply to Brijesh SinghQuoted 78 lines · Brijesh Singh
The command can be used by the userspace to query the SNP platform status report. See the SEV-SNP spec for more details.
Signed-off-by Brijesh Singh <[email protected]>--- Documentation/virt/coco/sevguest.rst | 27 +++++++++++++++++ drivers/crypto/ccp/sev-dev.c | 45 ++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 1 + 3 files changed, 73 insertions(+)Documentation/virt/coco/sevguest.rst +27
-0@@ -52,6 +52,22 @@ to execute due to the firmware error, then fw_err code will be set. __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 ------------------@@ -107,3 +123,14 @@ length of the blob is lesser than expected then snp_ext_report_req.certs_len wil be updated with the expected value. See GHCB specification for further detail on how to parse the certificate blob. + +2.3 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.drivers/crypto/ccp/sev-dev.c +17
-0@@ -1394,6 +1394,48 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)return ret; } +static int sev_ioctl_snp_platform_status (struct sev_issue_cmd *argp) +{ +struct sev_device *sev = psp_master->sev_data; +struct sev_data_snp_platform_status_buf buf; +struct page *status_page; +void *data; +int ret; + +if (!sev->snp_inited || !argp->data) +return -EINVAL ; + + status_page =alloc_page (GFP_KERNEL_ACCOUNT ); +if (!status_page) +return -ENOMEM ; + + data =page_address (status_page); +if (snp_set_rmp_state (__pa (data),1 , true, true, false)) {
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable) · line 1423
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+__free_pages (status_page,0 ); +return -EFAULT ; + } + + buf.status_paddr =__psp_pa (data); + ret =__sev_do_cmd_locked (SEV_CMD_SNP_PLATFORM_STATUS , &buf, &argp->error); + +/* Change the page state before accessing it */ +if (snp_set_rmp_state (__pa (data),1 , false, true, true)) { +snp_leak_pages (__pa (data) >>PAGE_SHIFT ,1 );
Calling snp_leak_pages() here seems wrong, because snp_set_rmp_state() calls snp_leak_pages() when it returns an error.
Quoted 44 lines
drivers/crypto/ccp/sev-dev.c +18
-0+return -EFAULT ; + } + +if (ret) + goto cleanup; + +if (copy_to_user ((void __user *)argp->data, data, +sizeof (struct sev_user_data_snp_status ))) + ret = -EFAULT ; + +cleanup: +__free_pages (status_page,0 ); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;@@ -1445,6 +1487,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_GET_ID2 : ret =sev_ioctl_do_get_id2 (&input);break ; +case SNP_PLATFORM_STATUS : + ret =sev_ioctl_snp_platform_status (&input); +break ;default : ret = -EINVAL ; goto out;include/uapi/linux/psp-sev.h +1
-1@@ -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, }; --2.17.1
Re: [PATCH Part2 v5 17/45] crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command
In reply to Brijesh Singh`
Quoted 97 lines · Brijesh Singh
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]>--- Documentation/virt/coco/sevguest.rst | 28 +++++++ drivers/crypto/ccp/sev-dev.c | 115 +++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 3 + include/uapi/linux/psp-sev.h | 17 ++++ 4 files changed, 163 insertions(+)Documentation/virt/coco/sevguest.rst +28
-0@@ -134,3 +134,31 @@ See GHCB specification for further detail on how to parse the certificate blob. 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.4 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 previous certificate blob will deleted. +For more information on the certificate blob layout, see the GHCB spec +(extended guest request message). + + +2.4 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_SET_EXT_CONFIG is used to query the system-wide configuration set +through the SNP_SET_EXT_CONFIG.drivers/crypto/ccp/sev-dev.c +18
-0@@ -1132,6 +1132,10 @@ static int __sev_snp_shutdown_locked(int *error)if (!sev->snp_inited)return 0 ; +/* Free the memory used for caching the certificate data */ +kfree (sev->snp_certs_data); + sev->snp_certs_data =NULL ; +/* SHUTDOWN requires the DF_FLUSH */ wbinvd_on_all_cpus ();__sev_do_cmd_locked (SEV_CMD_SNP_DF_FLUSH ,NULL ,NULL );@@ -1436,6 +1440,111 @@ 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_inited || !argp->data) +return -EINVAL ; + +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) {
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) · line 1466
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+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;
This API to retrieve the length of the certs seems pretty odd. We only return the length if the input.certs_address is non-NULL. But if we know the length how did we allocate an address to write to input.certs_address?
Quoted 32 lines
drivers/crypto/ccp/sev-dev.c +32
-0+ + ret = -ENOSR ; + goto e_done; + } + +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_inited || !argp->data) +return -EINVAL ; + +if (!writable) +return -EPERM ;
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) · line 1508
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+ +if (copy_from_user (&input, (void __user *)argp->data,sizeof (input))) +return -EFAULT ; + +/* Copy the certs from userspace */ +if (input.certs_address) { +if (!input.certs_len || !IS_ALIGNED (input.certs_len,PAGE_SIZE )) +return -EINVAL ; + + certs =psp_copy_user_blob (input.certs_address, input.certs_len);
Is psp_copy_user_blob() implemented in this patch series? When I searched through the patches, I only found an implementation that always returns an error. But maybe I missed the implementation?
Also, out of curiosity, any reason we cannot use copy_from_user here?
Quoted 43 lines
drivers/crypto/ccp/sev-dev.c +39
-0+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) { +if (copy_from_user (&config, + (void __user *)input.config_address,sizeof (config))) { + ret = -EFAULT ; + goto e_free; + } + + ret =__sev_do_cmd_locked (SEV_CMD_SNP_CONFIG , &config, &argp->error); +if (ret) + goto e_free; + +memcpy (&sev->snp_config, &config,sizeof (config)); + } + +/* +* If the new certs are passed then cache it else free the old certs. +*/ +if (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 ; + } + +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;
drivers/crypto/ccp/sev-dev.c static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) · line 1607
Quoted 9 lines
drivers/crypto/ccp/sev-dev.c +6
-0@@ -1490,6 +1599,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 ;
What is the intended use of SNP_GET_EXT_CONFIG. Yes, I get that it returns the "EXT config" previously set via SNP_SET_EXT_CONFIG. But presumably the caller can keep track of what it's previously passed to SNP_SET_EXT_CONFIG. Does it really need to call into the kernel to get these certs?
Quoted 55 lines
drivers/crypto/ccp/sev-dev.c +0
-0default : ret = -EINVAL ; goto out;drivers/crypto/ccp/sev-dev.h +3
-0@@ -66,6 +66,9 @@ struct sev_device {bool snp_inited;struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS ]; +void *snp_certs_data; +u32 snp_certs_len; +struct sev_user_data_snp_config snp_config; };int sev_dev_init (struct psp_device *psp);include/uapi/linux/psp-sev.h +17
-1@@ -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, };@@ -190,6 +192,21 @@ struct sev_user_data_snp_config {__u8 rsvd[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 * -- 2.17.1
Re: [PATCH Part2 v5 18/45] crypto: ccp: Provide APIs to query extended attestation report
In reply to Brijesh SinghQuoted 41 lines · Brijesh Singh
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]>--- drivers/crypto/ccp/sev-dev.c | 43 ++++++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 24 ++++++++++++++++++++ 2 files changed, 67 insertions(+)drivers/crypto/ccp/sev-dev.c +11
-0@@ -22,6 +22,7 @@#include <linux/firmware.h> #include <linux/gfp.h> #include <linux/cpufeature.h> +#include <linux/sev-guest.h> #include <asm/smp.h> @@ -1677,6 +1678,48 @@ int snp_guest_dbg_decrypt(struct sev_data_snp_dbg *data, int *error) }EXPORT_SYMBOL_GPL (snp_guest_dbg_decrypt); +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 ; +
drivers/crypto/ccp/sev-dev.c int snp_guest_dbg_decrypt(struct sev_data_snp_dbg *data, int *error) · line 1700
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+ sev = psp_master->sev_data; + +if (!sev->snp_inited) +return -EINVAL ; + +/* +* 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 ;
Is this calculation for expected_npages correct? Assume that sev->snp_certs_len is less than a page (e.g., 2000). Then, this calculation will return 0 for expected_npages, rather than round up to 1.
Quoted 70 lines
drivers/crypto/ccp/sev-dev.c +22
-0+if (*npages < expected_npages) { + *npages = expected_npages; + *fw_err =SNP_GUEST_REQ_INVALID_LEN ; +return -EINVAL ; + } + + rc =sev_do_cmd (SEV_CMD_SNP_GUEST_REQUEST , data, (int *)&fw_err); +if (rc) +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 ; + } + +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 +24
-1@@ -924,6 +924,23 @@ void *psp_copy_user_blob(u64 uaddr, u32 len);void *snp_alloc_firmware_page (gfp_t mask);void snp_free_firmware_page (void *addr); +/** +* 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. +*/ +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@@ -971,6 +988,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__ */ --2.17.1
Re: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
In reply to Brijesh SinghQuoted 170 lines · Brijesh Singh
The
KVM_SNP_INITcommand 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 theKVM_SEV_INITorKVM_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).
Co-developed-by Pavan Kumar Paluri <[email protected]>Signed-off-by Pavan Kumar Paluri <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- .../virt/kvm/amd-memory-encryption.rst | 27 ++++++++++++ arch/x86/include/asm/svm.h | 2 + arch/x86/kvm/svm/sev.c | 44 ++++++++++++++++++- arch/x86/kvm/svm/svm.h | 4 ++ include/uapi/linux/kvm.h | 13 ++++++ 5 files changed, 88 insertions(+), 2 deletions(-)Documentation/virt/kvm/amd-memory-encryption.rst +27
-0@@ -427,6 +427,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 +2
-0@@ -218,6 +218,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define SVM_NESTED_CTL_SEV_ENABLE BIT(1)#define SVM_NESTED_CTL_SEV_ES_ENABLE BIT(2) +#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +struct vmcb_seg {u16 selector;u16 attrib;arch/x86/kvm/svm/sev.c +42
-2@@ -235,10 +235,30 @@ static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)sev_decommission (handle); } +static int verify_snp_init_flags (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_snp_init params; +int ret =0 ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params))) +return -EFAULT ; + +if (params.flags & ~SEV_SNP_SUPPORTED_FLAGS ) + ret = -EOPNOTSUPP ; + + params.flags =SEV_SNP_SUPPORTED_FLAGS ; + +if (copy_to_user ((void __user *)(uintptr_t )argp->data, ¶ms,sizeof (params))) + ret = -EFAULT ; + +return ret; +} +static int sev_guest_init (struct kvm *kvm,struct kvm_sev_cmd *argp) { +bool es_active = (argp->id ==KVM_SEV_ES_INIT || argp->id ==KVM_SEV_SNP_INIT );struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; -bool es_active = argp->id ==KVM_SEV_ES_INIT ; +bool snp_active = argp->id ==KVM_SEV_SNP_INIT ;int asid, ret;if (kvm->created_vcpus)@@ -249,12 +269,22 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; sev->es_active = es_active; + sev->snp_active = snp_active; asid =sev_asid_new (sev);if (asid <0 ) goto e_no_asid; sev->asid = asid; - ret =sev_platform_init (&argp->error); +if (snp_active) { + ret =verify_snp_init_flags (kvm, argp); +if (ret) + goto e_free; + + ret =sev_snp_init (&argp->error); + }else { + ret =sev_platform_init (&argp->error); + } +if (ret) goto e_free;@@ -600,6 +630,10 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) save->pkru = svm->vcpu.arch.pkru; save->xss = svm->vcpu.arch.ia32_xss; +/* Enable the SEV-SNP feature */ +if (sev_snp_guest (svm->vcpu.kvm)) + save->sev_features |=SVM_SEV_FEAT_SNP_ACTIVE ; +return 0 ; }@@ -1532,6 +1566,12 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp) }switch (sev_cmd.id) { +case KVM_SEV_SNP_INIT : +if (!sev_snp_enabled) { + r = -ENOTTY ; + goto out; + } + fallthrough;case KVM_SEV_ES_INIT :if (!sev_es_enabled) { r = -ENOTTY ;arch/x86/kvm/svm/svm.h +3
-0@@ -69,6 +69,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 */
arch/x86/kvm/svm/svm.h struct kvm_sev_info { · line 87
Quoted 4 lines
arch/x86/kvm/svm/svm.h +1
-0@@ -81,6 +84,7 @@ struct kvm_sev_info {u64 ap_jump_table;/* SEV-ES AP Jump Table address */ struct kvm *enc_context_owner;/* Owner of copied encryption context */ struct misc_cg *misc_cg;/* For misc cgroup accounting */ +u64 snp_init_flags;
This field never gets set anywhere. Should it get set in verify_snp_init_flags()?
Quoted 38 lines
arch/x86/kvm/svm/svm.h +0
-0};struct kvm_svm {include/uapi/linux/kvm.h +13
-1@@ -1712,6 +1712,9 @@ enum sev_cmd_id {/* Guest Migration Extension */ KVM_SEV_SEND_CANCEL , +/* SNP specific commands */ +KVM_SEV_SNP_INIT , + KVM_SEV_NR_MAX, };@@ -1808,6 +1811,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) --2.17.1
Re: [PATCH Part2 v5 18/45] crypto: ccp: Provide APIs to query extended attestation report
In reply to Marc OrrQuoted 49 lines · Marc Orr
Quoted 48 lines · Brijesh Singh
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]>--- drivers/crypto/ccp/sev-dev.c | 43 ++++++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 24 ++++++++++++++++++++ 2 files changed, 67 insertions(+)drivers/crypto/ccp/sev-dev.c +18
-0@@ -22,6 +22,7 @@#include <linux/firmware.h> #include <linux/gfp.h> #include <linux/cpufeature.h> +#include <linux/sev-guest.h> #include <asm/smp.h> @@ -1677,6 +1678,48 @@ int snp_guest_dbg_decrypt(struct sev_data_snp_dbg *data, int *error) }EXPORT_SYMBOL_GPL (snp_guest_dbg_decrypt); +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_inited) +return -EINVAL ; + +/* +* Check if there is enough space to copy the certificate chain. Otherwise
Quoted 9 lines
drivers/crypto/ccp/sev-dev.c int snp_guest_dbg_decrypt(struct sev_data_snp_dbg *data, int *error) · line 1700
Quoted 3 lines
drivers/crypto/ccp/sev-dev.c +3
-0+* return ERROR code defined in the GHCB specification. +*/ + expected_npages = sev->snp_certs_len >>PAGE_SHIFT ;Is this calculation for
expected_npagescorrect? Assume thatsev->snp_certs_lenis less than a page (e.g., 2000). Then, this calculation will return0forexpected_npages, rather than round up to 1.
In patch 17 in sev_ioctl_snp_set_config there's a check that certs_len is aligned to PAGE_SIZE (and this value is later in that function assigned to sev->snp_certs_len):
+ /* Copy the certs from userspace */
+ if (input.certs_address) {
+ if (!input.certs_len || !IS_ALIGNED(input.certs_len, PAGE_SIZE))
+ return -EINVAL;
but I agree that rounding up (DIV_ROUND_UP) or asserting that sev->snp_certs_len is aligned to PAGE_SIZE (and non-zero) makes sense here.
-Dov
Quoted 70 lines
Quoted 70 lines
drivers/crypto/ccp/sev-dev.c +22
-0+if (*npages < expected_npages) { + *npages = expected_npages; + *fw_err =SNP_GUEST_REQ_INVALID_LEN ; +return -EINVAL ; + } + + rc =sev_do_cmd (SEV_CMD_SNP_GUEST_REQUEST , data, (int *)&fw_err); +if (rc) +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 ; + } + +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 +24
-1@@ -924,6 +924,23 @@ void *psp_copy_user_blob(u64 uaddr, u32 len);void *snp_alloc_firmware_page (gfp_t mask);void snp_free_firmware_page (void *addr); +/** +* 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. +*/ +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@@ -971,6 +988,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__ */ --2.17.1
Re: [PATCH Part2 v5 16/45] crypto: ccp: Add the SNP_PLATFORM_STATUS command
In reply to Marc OrrQuoted 83 lines · Marc Orr
Quoted 82 lines · Brijesh Singh
The command can be used by the userspace to query the SNP platform status report. See the SEV-SNP spec for more details.
Signed-off-by Brijesh Singh <[email protected]>--- Documentation/virt/coco/sevguest.rst | 27 +++++++++++++++++ drivers/crypto/ccp/sev-dev.c | 45 ++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 1 + 3 files changed, 73 insertions(+)Documentation/virt/coco/sevguest.rst +27
-0@@ -52,6 +52,22 @@ to execute due to the firmware error, then fw_err code will be set. __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 ------------------@@ -107,3 +123,14 @@ length of the blob is lesser than expected then snp_ext_report_req.certs_len wil be updated with the expected value. See GHCB specification for further detail on how to parse the certificate blob. + +2.3 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.drivers/crypto/ccp/sev-dev.c +22
-0@@ -1394,6 +1394,48 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)return ret; } +static int sev_ioctl_snp_platform_status (struct sev_issue_cmd *argp) +{ +struct sev_device *sev = psp_master->sev_data; +struct sev_data_snp_platform_status_buf buf; +struct page *status_page; +void *data; +int ret; + +if (!sev->snp_inited || !argp->data) +return -EINVAL ; + + status_page =alloc_page (GFP_KERNEL_ACCOUNT ); +if (!status_page) +return -ENOMEM ; + + data =page_address (status_page); +if (snp_set_rmp_state (__pa (data),1 , true, true, false)) { +__free_pages (status_page,0 ); +return -EFAULT ; + } + + buf.status_paddr =__psp_pa (data);
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable) · line 1423
Quoted 5 lines
drivers/crypto/ccp/sev-dev.c +5
-0+ ret =__sev_do_cmd_locked (SEV_CMD_SNP_PLATFORM_STATUS , &buf, &argp->error); + +/* Change the page state before accessing it */ +if (snp_set_rmp_state (__pa (data),1 , false, true, true)) { +snp_leak_pages (__pa (data) >>PAGE_SHIFT ,1 );Calling
snp_leak_pages()here seems wrong, becausesnp_set_rmp_state()callssnp_leak_pages()when it returns an error.
Agreed, i will fix in next rev.
Quoted 45 lines
Quoted 45 lines
drivers/crypto/ccp/sev-dev.c +18
-0+return -EFAULT ; + } + +if (ret) + goto cleanup; + +if (copy_to_user ((void __user *)argp->data, data, +sizeof (struct sev_user_data_snp_status ))) + ret = -EFAULT ; + +cleanup: +__free_pages (status_page,0 ); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;@@ -1445,6 +1487,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_GET_ID2 : ret =sev_ioctl_do_get_id2 (&input);break ; +case SNP_PLATFORM_STATUS : + ret =sev_ioctl_snp_platform_status (&input); +break ;default : ret = -EINVAL ; goto out;include/uapi/linux/psp-sev.h +1
-1@@ -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, }; --2.17.1
Re: [PATCH Part2 v5 17/45] crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command
In reply to Marc OrrQuoted 105 lines · Marc Orr
`
Quoted 102 lines · Brijesh Singh
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]>--- Documentation/virt/coco/sevguest.rst | 28 +++++++ drivers/crypto/ccp/sev-dev.c | 115 +++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 3 + include/uapi/linux/psp-sev.h | 17 ++++ 4 files changed, 163 insertions(+)Documentation/virt/coco/sevguest.rst +28
-0@@ -134,3 +134,31 @@ See GHCB specification for further detail on how to parse the certificate blob. 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.4 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 previous certificate blob will deleted. +For more information on the certificate blob layout, see the GHCB spec +(extended guest request message). + + +2.4 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_SET_EXT_CONFIG is used to query the system-wide configuration set +through the SNP_SET_EXT_CONFIG.drivers/crypto/ccp/sev-dev.c +24
-0@@ -1132,6 +1132,10 @@ static int __sev_snp_shutdown_locked(int *error)if (!sev->snp_inited)return 0 ; +/* Free the memory used for caching the certificate data */ +kfree (sev->snp_certs_data); + sev->snp_certs_data =NULL ; +/* SHUTDOWN requires the DF_FLUSH */ wbinvd_on_all_cpus ();__sev_do_cmd_locked (SEV_CMD_SNP_DF_FLUSH ,NULL ,NULL );@@ -1436,6 +1440,111 @@ 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_inited || !argp->data) +return -EINVAL ; + +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 */
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) · line 1466
Quoted 4 lines
drivers/crypto/ccp/sev-dev.c +4
-0+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;This API to retrieve the length of the certs seems pretty odd. We only return the length if the input.certs_address is non-NULL. But if we know the length how did we allocate an address to write to
input.certs_address?
Ah good point, I should provide an option to query the length when input.cert_address == 0. This will make it much cleaner that there are two approaches to get the length.
Quoted 46 lines
Quoted 32 lines
drivers/crypto/ccp/sev-dev.c +32
-0+ + ret = -ENOSR ; + goto e_done; + } + +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_inited || !argp->data) +return -EINVAL ; + +if (!writable) +return -EPERM ;drivers/crypto/ccp/sev-dev.c static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) · line 1508
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+ +if (copy_from_user (&input, (void __user *)argp->data,sizeof (input))) +return -EFAULT ; + +/* Copy the certs from userspace */ +if (input.certs_address) { +if (!input.certs_len || !IS_ALIGNED (input.certs_len,PAGE_SIZE )) +return -EINVAL ; + + certs =psp_copy_user_blob (input.certs_address, input.certs_len);Is
psp_copy_user_blob()implemented in this patch series? When I searched through the patches, I only found an implementation that always returns an error. But maybe I missed the implementation?
Quoted 1 line
Also, out of curiosity, any reason we cannot use
copy_from_userhere?
psp_copy_user_blob() is a wrapper around memdup_user() -- which does kmalloc() + copy_to_user(). The wrapper performs some additional checks such as the blob size etc, we limit the blob size to 16K to avoid copying a random large data from userspace.
Quoted 49 lines
Quoted 49 lines
drivers/crypto/ccp/sev-dev.c +42
-0+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) { +if (copy_from_user (&config, + (void __user *)input.config_address,sizeof (config))) { + ret = -EFAULT ; + goto e_free; + } + + ret =__sev_do_cmd_locked (SEV_CMD_SNP_CONFIG , &config, &argp->error); +if (ret) + goto e_free; + +memcpy (&sev->snp_config, &config,sizeof (config)); + } + +/* +* If the new certs are passed then cache it else free the old certs. +*/ +if (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 ; + } + +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;@@ -1490,6 +1599,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 ;
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) · line 1607
Quoted 3 lines
drivers/crypto/ccp/sev-dev.c +3
-0+case SNP_GET_EXT_CONFIG : + ret =sev_ioctl_snp_get_config (&input); +break ;What is the intended use of
SNP_GET_EXT_CONFIG. Yes, I get that it returns the "EXT config" previously set viaSNP_SET_EXT_CONFIG. But presumably the caller can keep track of what it's previously passed toSNP_SET_EXT_CONFIG. Does it really need to call into the kernel to get these certs?
This is mainly to help in the cases where the provisioning tools may not keep track of the programmed TCB version and would prefer to read the TCB version before updating.
Re: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
In reply to Marc OrrQuoted 163 lines · Marc Orr
Quoted 162 lines · Brijesh Singh
The
KVM_SNP_INITcommand 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 theKVM_SEV_INITorKVM_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).
Co-developed-by Pavan Kumar Paluri <[email protected]>Signed-off-by Pavan Kumar Paluri <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- .../virt/kvm/amd-memory-encryption.rst | 27 ++++++++++++ arch/x86/include/asm/svm.h | 2 + arch/x86/kvm/svm/sev.c | 44 ++++++++++++++++++- arch/x86/kvm/svm/svm.h | 4 ++ include/uapi/linux/kvm.h | 13 ++++++ 5 files changed, 88 insertions(+), 2 deletions(-)Documentation/virt/kvm/amd-memory-encryption.rst +27
-0@@ -427,6 +427,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 +2
-0@@ -218,6 +218,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define SVM_NESTED_CTL_SEV_ENABLE BIT(1)#define SVM_NESTED_CTL_SEV_ES_ENABLE BIT(2) +#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +struct vmcb_seg {u16 selector;u16 attrib;arch/x86/kvm/svm/sev.c +42
-2@@ -235,10 +235,30 @@ static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)sev_decommission (handle); } +static int verify_snp_init_flags (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_snp_init params; +int ret =0 ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params))) +return -EFAULT ; + +if (params.flags & ~SEV_SNP_SUPPORTED_FLAGS ) + ret = -EOPNOTSUPP ; + + params.flags =SEV_SNP_SUPPORTED_FLAGS ; + +if (copy_to_user ((void __user *)(uintptr_t )argp->data, ¶ms,sizeof (params))) + ret = -EFAULT ; + +return ret; +} +static int sev_guest_init (struct kvm *kvm,struct kvm_sev_cmd *argp) { +bool es_active = (argp->id ==KVM_SEV_ES_INIT || argp->id ==KVM_SEV_SNP_INIT );struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; -bool es_active = argp->id ==KVM_SEV_ES_INIT ; +bool snp_active = argp->id ==KVM_SEV_SNP_INIT ;int asid, ret;if (kvm->created_vcpus)@@ -249,12 +269,22 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; sev->es_active = es_active; + sev->snp_active = snp_active; asid =sev_asid_new (sev);if (asid <0 ) goto e_no_asid; sev->asid = asid; - ret =sev_platform_init (&argp->error); +if (snp_active) { + ret =verify_snp_init_flags (kvm, argp); +if (ret) + goto e_free; + + ret =sev_snp_init (&argp->error); + }else { + ret =sev_platform_init (&argp->error); + } +if (ret) goto e_free;@@ -600,6 +630,10 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) save->pkru = svm->vcpu.arch.pkru; save->xss = svm->vcpu.arch.ia32_xss; +/* Enable the SEV-SNP feature */ +if (sev_snp_guest (svm->vcpu.kvm)) + save->sev_features |=SVM_SEV_FEAT_SNP_ACTIVE ; +return 0 ; }@@ -1532,6 +1566,12 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp) }switch (sev_cmd.id) { +case KVM_SEV_SNP_INIT : +if (!sev_snp_enabled) { + r = -ENOTTY ; + goto out; + } + fallthrough;case KVM_SEV_ES_INIT :if (!sev_es_enabled) { r = -ENOTTY ;arch/x86/kvm/svm/svm.h +0
-0@@ -69,6 +69,9 @@ enum {/* TPR and CR2 are always written before VMRUN */ #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
Quoted 13 lines
arch/x86/kvm/svm/svm.h struct kvm_sev_info { · line 87
Quoted 11 lines
arch/x86/kvm/svm/svm.h +4
-0+/* 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 */ @@ -81,6 +84,7 @@ struct kvm_sev_info {u64 ap_jump_table;/* SEV-ES AP Jump Table address */ struct kvm *enc_context_owner;/* Owner of copied encryption context */ struct misc_cg *misc_cg;/* For misc cgroup accounting */ +u64 snp_init_flags;This field never gets set anywhere. Should it get set in
verify_snp_init_flags()?
Actually the supported flag value is zero, so didn't update it. But to make code cleaner I will set the flag after the negotiation.
Quoted 39 lines
Quoted 39 lines
arch/x86/kvm/svm/svm.h +0
-0};struct kvm_svm {include/uapi/linux/kvm.h +13
-1@@ -1712,6 +1712,9 @@ enum sev_cmd_id {/* Guest Migration Extension */ KVM_SEV_SEND_CANCEL , +/* SNP specific commands */ +KVM_SEV_SNP_INIT , + KVM_SEV_NR_MAX, };@@ -1808,6 +1811,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) --2.17.1
Re: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
In reply to Brijesh SinghQuoted 95 lines · Brijesh Singh
The
KVM_SNP_INITcommand 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 theKVM_SEV_INITorKVM_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).
Co-developed-by Pavan Kumar Paluri <[email protected]>Signed-off-by Pavan Kumar Paluri <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- .../virt/kvm/amd-memory-encryption.rst | 27 ++++++++++++ arch/x86/include/asm/svm.h | 2 + arch/x86/kvm/svm/sev.c | 44 ++++++++++++++++++- arch/x86/kvm/svm/svm.h | 4 ++ include/uapi/linux/kvm.h | 13 ++++++ 5 files changed, 88 insertions(+), 2 deletions(-)Documentation/virt/kvm/amd-memory-encryption.rst +27
-0@@ -427,6 +427,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 +2
-0@@ -218,6 +218,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define SVM_NESTED_CTL_SEV_ENABLE BIT(1)#define SVM_NESTED_CTL_SEV_ES_ENABLE BIT(2) +#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +struct vmcb_seg {u16 selector;u16 attrib;arch/x86/kvm/svm/sev.c +16
-0@@ -235,10 +235,30 @@ static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)sev_decommission (handle); } +static int verify_snp_init_flags (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_snp_init params; +int ret =0 ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params))) +return -EFAULT ; + +if (params.flags & ~SEV_SNP_SUPPORTED_FLAGS ) + ret = -EOPNOTSUPP ; + + params.flags =SEV_SNP_SUPPORTED_FLAGS ; + +if (copy_to_user ((void __user *)(uintptr_t )argp->data, ¶ms,sizeof (params))) + ret = -EFAULT ; +
arch/x86/kvm/svm/sev.c static void sev_unbind_asid(struct kvm *kvm, unsigned int handle) · line 262
Quoted 10 lines
arch/x86/kvm/svm/sev.c +5
-1+return ret; +} +static int sev_guest_init (struct kvm *kvm,struct kvm_sev_cmd *argp) { +bool es_active = (argp->id ==KVM_SEV_ES_INIT || argp->id ==KVM_SEV_SNP_INIT );struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; -bool es_active = argp->id ==KVM_SEV_ES_INIT ; +bool snp_active = argp->id ==KVM_SEV_SNP_INIT ;int asid, ret;
Not sure if this is the patch place for this but I think you want to disallow svm_vm_copy_asid_from() if snp_active == true.
Quoted 107 lines
arch/x86/kvm/svm/sev.c +21
-1if (kvm->created_vcpus)@@ -249,12 +269,22 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; sev->es_active = es_active; + sev->snp_active = snp_active; asid =sev_asid_new (sev);if (asid <0 ) goto e_no_asid; sev->asid = asid; - ret =sev_platform_init (&argp->error); +if (snp_active) { + ret =verify_snp_init_flags (kvm, argp); +if (ret) + goto e_free; + + ret =sev_snp_init (&argp->error); + }else { + ret =sev_platform_init (&argp->error); + } +if (ret) goto e_free;@@ -600,6 +630,10 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) save->pkru = svm->vcpu.arch.pkru; save->xss = svm->vcpu.arch.ia32_xss; +/* Enable the SEV-SNP feature */ +if (sev_snp_guest (svm->vcpu.kvm)) + save->sev_features |=SVM_SEV_FEAT_SNP_ACTIVE ; +return 0 ; }@@ -1532,6 +1566,12 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp) }switch (sev_cmd.id) { +case KVM_SEV_SNP_INIT : +if (!sev_snp_enabled) { + r = -ENOTTY ; + goto out; + } + fallthrough;case KVM_SEV_ES_INIT :if (!sev_es_enabled) { r = -ENOTTY ;arch/x86/kvm/svm/svm.h +4
-0@@ -69,6 +69,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 */ @@ -81,6 +84,7 @@ struct kvm_sev_info {u64 ap_jump_table;/* SEV-ES AP Jump Table address */ struct kvm *enc_context_owner;/* Owner of copied encryption context */ struct misc_cg *misc_cg;/* For misc cgroup accounting */ +u64 snp_init_flags; };struct kvm_svm {include/uapi/linux/kvm.h +13
-1@@ -1712,6 +1712,9 @@ enum sev_cmd_id {/* Guest Migration Extension */ KVM_SEV_SEND_CANCEL , +/* SNP specific commands */ +KVM_SEV_SNP_INIT , + KVM_SEV_NR_MAX, };@@ -1808,6 +1811,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) --2.17.1
Borislav Petkov
maintainerRe: [PATCH Part2 v5 01/45] x86/cpufeatures: Add SEV-SNP CPU feature
In reply to Brijesh SinghQuoted 17 lines · Brijesh Singh
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]>--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/kernel/cpu/amd.c | 3 ++- tools/arch/x86/include/asm/cpufeatures.h | 1 + 3 files changed, 4 insertions(+), 1 deletion(-)arch/x86/include/asm/cpufeatures.h +0
-0
arch/x86/include/asm/cpufeatures.h line 401
Quoted 4 lines
arch/x86/include/asm/cpufeatures.h +1
-0@@ -398,6 +398,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 */
s/AMD Secure Encrypted Virtualization/AMD SEV/g
Bit 1 above already has that string - no need for repeating it everywhere.
Also, note the vertical alignment (space after the '+'):
(19*32+ 4)
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerRe: [PATCH Part2 v5 02/45] iommu/amd: Introduce function to check SEV-SNP support
In reply to Brijesh SinghQuoted 1 line · Brijesh Singh
The SEV-SNP support requires that IOMMU must to enabled, see the IOMMU
s/must to/is/
Quoted 5 lines
spec section 2.12 for further details. If IOMMU is not enabled or the SNPSup extended feature register is not set then the
SNP_INITcommand (used for initializing firmware) will fail.The
iommu_sev_snp_supported()can be used to check if IOMMU supports the
"can be used"?
Just say what is going to use it.
Quoted 36 lines
SEV-SNP feature.
Signed-off-by Brijesh Singh <[email protected]>--- drivers/iommu/amd/init.c | 30 ++++++++++++++++++++++++++++++ include/linux/iommu.h | 9 +++++++++ 2 files changed, 39 insertions(+)drivers/iommu/amd/init.c +20
-0@@ -3320,3 +3320,33 @@ int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64return iommu_pc_get_set_reg (iommu, bank, cntr, fxn, value, true); } + +bool iommu_sev_snp_supported (void ) +{ +struct amd_iommu *iommu; + +/* +* The SEV-SNP support requires that IOMMU must be enabled, and is +* not configured in the passthrough mode. +*/ +if (no_iommu ||iommu_default_passthrough ()) { +pr_err ("SEV-SNP: IOMMU is either disabled or configured in passthrough mode.\n" ); +return false; + } + +/* +* Iterate through all the IOMMUs and verify the SNPSup feature is +* enabled. +*/ +for_each_iommu (iommu) { +if (!iommu_feature (iommu,FEATURE_SNP )) {
drivers/iommu/amd/init.c int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 · line 3352
Quoted 10 lines
drivers/iommu/amd/init.c +10
-0+pr_err ("SNPSup is disabled (devid: %02x:%02x.%x)\n" , +PCI_BUS_NUM (iommu->devid),PCI_SLOT (iommu->devid), +PCI_FUNC (iommu->devid)); +return false; + } + } + +return true; +} +EXPORT_SYMBOL_GPL (iommu_sev_snp_supported);
That export is not needed.
Quoted 18 lines
include/linux/iommu.h +6
-0@@ -604,6 +604,12 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev,void iommu_sva_unbind_device (struct iommu_sva *handle);u32 iommu_sva_get_pasid (struct iommu_sva *handle); +#ifdef CONFIG_AMD_MEM_ENCRYPT +bool iommu_sev_snp_supported (void ); +#else +static inline bool iommu_sev_snp_supported (void ) {return false; } +#endif +#else /* CONFIG_IOMMU_API */ struct iommu_ops {};
include/linux/iommu.h static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev) · line 1010
Quoted 6 lines
include/linux/iommu.h +3
-0@@ -999,6 +1005,9 @@ static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev) {return NULL ; } + +static inline bool iommu_sev_snp_supported (void ) {return false; } +
Most of those stubs and ifdeffery is not needed if you put the function itself in
#ifdef CONFIG_AMD_MEM_ENCRYPT
...
#endif
as it is called by sev.c only, AFAICT, and latter is enabled by CONFIG_AMD_MEM_ENCRYPT anyway.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH Part2 v5 01/45] x86/cpufeatures: Add SEV-SNP CPU feature
In reply to Borislav PetkovQuoted 25 lines · Borislav Petkov
Quoted 17 lines · Brijesh Singh
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]>--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/kernel/cpu/amd.c | 3 ++- tools/arch/x86/include/asm/cpufeatures.h | 1 + 3 files changed, 4 insertions(+), 1 deletion(-)arch/x86/include/asm/cpufeatures.h +0
-0arch/x86/include/asm/cpufeatures.h line 401
Quoted 4 lines
arch/x86/include/asm/cpufeatures.h +1
-0@@ -398,6 +398,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 */ s/AMD Secure Encrypted Virtualization/AMD SEV/g
Quoted 7 lines
Bit 1 above already has that string - no need for repeating it everywhere.
Also, note the vertical alignment (space after the '+'):
(19*32+ 4)
Noted. I will fix in next rev. thanks
Re: [PATCH Part2 v5 16/45] crypto: ccp: Add the SNP_PLATFORM_STATUS command
In reply to Brijesh SinghQuoted 79 lines · * Brijesh Singh ([email protected]) wrote:
The command can be used by the userspace to query the SNP platform status report. See the SEV-SNP spec for more details.
Signed-off-by Brijesh Singh <[email protected]>--- Documentation/virt/coco/sevguest.rst | 27 +++++++++++++++++ drivers/crypto/ccp/sev-dev.c | 45 ++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 1 + 3 files changed, 73 insertions(+)Documentation/virt/coco/sevguest.rst +27
-0@@ -52,6 +52,22 @@ to execute due to the firmware error, then fw_err code will be set. __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 ------------------@@ -107,3 +123,14 @@ length of the blob is lesser than expected then snp_ext_report_req.certs_len wil be updated with the expected value. See GHCB specification for further detail on how to parse the certificate blob. + +2.3 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.drivers/crypto/ccp/sev-dev.c +19
-0@@ -1394,6 +1394,48 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)return ret; } +static int sev_ioctl_snp_platform_status (struct sev_issue_cmd *argp) +{ +struct sev_device *sev = psp_master->sev_data; +struct sev_data_snp_platform_status_buf buf; +struct page *status_page; +void *data; +int ret; + +if (!sev->snp_inited || !argp->data) +return -EINVAL ; + + status_page =alloc_page (GFP_KERNEL_ACCOUNT ); +if (!status_page) +return -ENOMEM ; + + data =page_address (status_page); +if (snp_set_rmp_state (__pa (data),1 , true, true, false)) { +__free_pages (status_page,0 ); +return -EFAULT ;
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable) · line 1425
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+ } + + buf.status_paddr =__psp_pa (data); + ret =__sev_do_cmd_locked (SEV_CMD_SNP_PLATFORM_STATUS , &buf, &argp->error); + +/* Change the page state before accessing it */ +if (snp_set_rmp_state (__pa (data),1 , false, true, true)) { +snp_leak_pages (__pa (data) >>PAGE_SHIFT ,1 ); +return -EFAULT ; + }
Could we find a way of returning some of these errors into the output, rather than interepreting them from errno values?
Dave
Quoted 41 lines
drivers/crypto/ccp/sev-dev.c +15
-0+if (ret) + goto cleanup; + +if (copy_to_user ((void __user *)argp->data, data, +sizeof (struct sev_user_data_snp_status ))) + ret = -EFAULT ; + +cleanup: +__free_pages (status_page,0 ); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;@@ -1445,6 +1487,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_GET_ID2 : ret =sev_ioctl_do_get_id2 (&input);break ; +case SNP_PLATFORM_STATUS : + ret =sev_ioctl_snp_platform_status (&input); +break ;default : ret = -EINVAL ; goto out;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.17.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH Part2 v5 21/45] KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
In reply to Brijesh SinghQuoted 7 lines · * Brijesh Singh ([email protected]) wrote:
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.
Can you explain what 'globally enabled' means?
Or more specifically, can we trip this bug on public hardware that has the SNP enabled in the bios, but no SNP init in the host OS?
Dave
Quoted 170 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 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]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/lapic.c | 5 ++++- arch/x86/kvm/svm/sev.c | 35 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 16 ++++++++++++-- arch/x86/kvm/svm/svm.h | 1 + 6 files changed, 56 insertions(+), 3 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -122,6 +122,7 @@ KVM_X86_OP_NULL(enable_direct_tlbflush) KVM_X86_OP_NULL(migrate_timers) KVM_X86_OP(msr_filter_changed) KVM_X86_OP_NULL(complete_emulated_msr) +KVM_X86_OP(alloc_apic_backing_page)#undef KVM_X86_OP#undef KVM_X86_OP_NULLarch/x86/include/asm/kvm_host.h +1
-0@@ -1453,6 +1453,7 @@ struct kvm_x86_ops {int (*complete_emulated_msr)(struct kvm_vcpu *vcpu,int err);void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu,u8 vector); +void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); };struct kvm_x86_nested_ops {arch/x86/kvm/lapic.c +4
-1@@ -2457,7 +2457,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 +35
-0@@ -2703,3 +2703,38 @@ 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_to_phys (pfn),PMD_SIZE )) { + pfn++; +__free_page (p); + }else { +__free_page (pfn_to_page (pfn +1 )); + } + +return pfn_to_page (pfn); +}arch/x86/kvm/svm/svm.c +14
-2@@ -1368,7 +1368,7 @@ static int svm_create_vcpu(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;@@ -1377,7 +1377,7 @@ static int svm_create_vcpu(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;@@ -4539,6 +4539,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 = { .hardware_unsetup = svm_hardware_teardown, .hardware_enable = svm_hardware_enable,@@ -4667,6 +4677,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .complete_emulated_msr = svm_complete_emulated_msr, .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, + + .alloc_apic_backing_page = svm_alloc_apic_backing_page, };static struct kvm_x86_init_ops svm_init_ops__initdata = {arch/x86/kvm/svm/svm.h +1
-0@@ -575,6 +575,7 @@ void sev_es_create_vcpu(struct vcpu_svm *svm);void sev_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector);void sev_es_prepare_guest_switch (struct vcpu_svm *svm,unsigned int cpu);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.17.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH Part2 v5 26/45] KVM: SVM: Mark the private vma unmerable for SEV-SNP guests
In reply to Brijesh SinghQuoted 8 lines · * Brijesh Singh ([email protected]) wrote:
When SEV-SNP is enabled, the guest private pages are added in the RMP table; while adding the pages, the
rmp_make_private()unmaps the pages from the direct map. If KSM attempts to access those unmapped pages then it will trigger #PF (page-not-present).Encrypted guest pages cannot be shared between the process, so an userspace should not mark the region mergeable but to be safe, mark the process vma unmerable before adding the pages in the RMP table.
^^^^^^^^^
(and in the subject) -> unmergeable
Quoted 25 lines
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/kvm/svm/sev.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)arch/x86/kvm/svm/sev.c +2
-0@@ -18,11 +18,13 @@#include <linux/processor.h> #include <linux/trace_events.h> #include <linux/sev.h> +#include <linux/ksm.h> #include <asm/fpu/internal.h> #include <asm/pkru.h> #include <asm/trapnr.h> #include <asm/sev.h> +#include <asm/mman.h> #include "x86.h" #include "svm.h"
arch/x86/kvm/svm/sev.c static bool is_hva_registered(struct kvm *kvm, hva_t hva, size_t len) · line 1688
Quoted 4 lines
arch/x86/kvm/svm/sev.c +1
-0@@ -1683,6 +1685,30 @@ static bool is_hva_registered(struct kvm *kvm, hva_t hva, size_t len)return false; } +static int snp_mark_unmergable (struct kvm *kvm,u64 start,u64 size)
^^^^^^^^^^
arch/x86/kvm/svm/sev.c static bool is_hva_registered(struct kvm *kvm, hva_t hva, size_t len) · line 1691
Quoted 3 lines
arch/x86/kvm/svm/sev.c +3
-0+{ +struct vm_area_struct *vma; +u64 end = start + size;
Do you need to worry about wrap there? (User supplied start/size?)
Dave
Quoted 40 lines
arch/x86/kvm/svm/sev.c +26
-0+int ret; + +do { + vma =find_vma_intersection (kvm->mm, start, end); +if (!vma) { + ret = -EINVAL ; +break ; + } + + ret =ksm_madvise (vma, vma->vm_start, vma->vm_end, +MADV_UNMERGEABLE , &vma->vm_flags); +if (ret) +break ; + + start = vma->vm_end; + }while (end > vma->vm_end); + +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;@@ -1707,6 +1733,12 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)if (!is_hva_registered (kvm, params.uaddr, params.len))return -EINVAL ; +mmap_write_lock (kvm->mm); + ret =snp_mark_unmergable (kvm, params.uaddr, params.len); +mmap_write_unlock (kvm->mm); +if (ret) +return -EFAULT ; +/* * The userspace memory is already locked so technically we don't * need to lock it again. Later part of the function needs to know Signature · 1 line
2.17.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH Part2 v5 16/45] crypto: ccp: Add the SNP_PLATFORM_STATUS command
In reply to Dr. David Alan GilbertQuoted 84 lines · Dr. David Alan Gilbert
Quoted 83 lines · * Brijesh Singh ([email protected]) wrote:
The command can be used by the userspace to query the SNP platform status report. See the SEV-SNP spec for more details.
Signed-off-by Brijesh Singh <[email protected]>--- Documentation/virt/coco/sevguest.rst | 27 +++++++++++++++++ drivers/crypto/ccp/sev-dev.c | 45 ++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 1 + 3 files changed, 73 insertions(+)Documentation/virt/coco/sevguest.rst +27
-0@@ -52,6 +52,22 @@ to execute due to the firmware error, then fw_err code will be set. __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 ------------------@@ -107,3 +123,14 @@ length of the blob is lesser than expected then snp_ext_report_req.certs_len wil be updated with the expected value. See GHCB specification for further detail on how to parse the certificate blob. + +2.3 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.drivers/crypto/ccp/sev-dev.c +23
-0@@ -1394,6 +1394,48 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)return ret; } +static int sev_ioctl_snp_platform_status (struct sev_issue_cmd *argp) +{ +struct sev_device *sev = psp_master->sev_data; +struct sev_data_snp_platform_status_buf buf; +struct page *status_page; +void *data; +int ret; + +if (!sev->snp_inited || !argp->data) +return -EINVAL ; + + status_page =alloc_page (GFP_KERNEL_ACCOUNT ); +if (!status_page) +return -ENOMEM ; + + data =page_address (status_page); +if (snp_set_rmp_state (__pa (data),1 , true, true, false)) { +__free_pages (status_page,0 ); +return -EFAULT ; + } + + buf.status_paddr =__psp_pa (data); + ret =__sev_do_cmd_locked (SEV_CMD_SNP_PLATFORM_STATUS , &buf, &argp->error);
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable) · line 1425
Quoted 6 lines
drivers/crypto/ccp/sev-dev.c +6
-0+ +/* Change the page state before accessing it */ +if (snp_set_rmp_state (__pa (data),1 , false, true, true)) { +snp_leak_pages (__pa (data) >>PAGE_SHIFT ,1 ); +return -EFAULT ; + }Could we find a way of returning some of these errors into the output, rather than interepreting them from errno values?
I can try to find a corresponding fw error code and use it where applicable. e.g in this case I can use the SEV_RET_INVALID_PAGE_STATE.
thanks
Re: [PATCH Part2 v5 21/45] KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
In reply to Dr. David Alan GilbertQuoted 9 lines · Dr. David Alan Gilbert
Quoted 7 lines · * Brijesh Singh ([email protected]) wrote:
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.Can you explain what 'globally enabled' means?
This means that SNP is enabled in host SYSCFG_MSR.Snp=1. Once its enabled then RMP checks are enforced.
Quoted 2 lines
Or more specifically, can we trip this bug on public hardware that has the SNP enabled in the bios, but no SNP init in the host OS?
Enabling the SNP support on host is 3 step process:
step1 (bios): reserve memory for the RMP table.
step2 (host): initialize the RMP table memory, set the SYSCFG msr to enable the SNP feature
step3 (host): call the SNP_INIT to initialize the SNP firmware (this is needed only if you ever plan to launch SNP guest from this host).
The "SNP globally enabled" means the step 1 to 2. The RMP checks are enforced as soon as step 2 is completed.
thanks
Quoted 173 lines
Dave
Quoted 170 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 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]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/lapic.c | 5 ++++- arch/x86/kvm/svm/sev.c | 35 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 16 ++++++++++++-- arch/x86/kvm/svm/svm.h | 1 + 6 files changed, 56 insertions(+), 3 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -122,6 +122,7 @@ KVM_X86_OP_NULL(enable_direct_tlbflush) KVM_X86_OP_NULL(migrate_timers) KVM_X86_OP(msr_filter_changed) KVM_X86_OP_NULL(complete_emulated_msr) +KVM_X86_OP(alloc_apic_backing_page)#undef KVM_X86_OP#undef KVM_X86_OP_NULLarch/x86/include/asm/kvm_host.h +1
-0@@ -1453,6 +1453,7 @@ struct kvm_x86_ops {int (*complete_emulated_msr)(struct kvm_vcpu *vcpu,int err);void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu,u8 vector); +void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); };struct kvm_x86_nested_ops {arch/x86/kvm/lapic.c +4
-1@@ -2457,7 +2457,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 +35
-0@@ -2703,3 +2703,38 @@ 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_to_phys (pfn),PMD_SIZE )) { + pfn++; +__free_page (p); + }else { +__free_page (pfn_to_page (pfn +1 )); + } + +return pfn_to_page (pfn); +}arch/x86/kvm/svm/svm.c +14
-2@@ -1368,7 +1368,7 @@ static int svm_create_vcpu(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;@@ -1377,7 +1377,7 @@ static int svm_create_vcpu(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;@@ -4539,6 +4539,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 = { .hardware_unsetup = svm_hardware_teardown, .hardware_enable = svm_hardware_enable,@@ -4667,6 +4677,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .complete_emulated_msr = svm_complete_emulated_msr, .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, + + .alloc_apic_backing_page = svm_alloc_apic_backing_page, };static struct kvm_x86_init_ops svm_init_ops__initdata = {arch/x86/kvm/svm/svm.h +1
-0@@ -575,6 +575,7 @@ void sev_es_create_vcpu(struct vcpu_svm *svm);void sev_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector);void sev_es_prepare_guest_switch (struct vcpu_svm *svm,unsigned int cpu);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.17.1
Re: [PATCH Part2 v5 21/45] KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
Quoted 23 lines · * Brijesh Singh ([email protected]) wrote:
Quoted 9 lines · Dr. David Alan Gilbert
Quoted 7 lines · * Brijesh Singh ([email protected]) wrote:
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.Can you explain what 'globally enabled' means?
This means that SNP is enabled in host SYSCFG_MSR.Snp=1. Once its enabled then RMP checks are enforced.
Quoted 2 lines
Or more specifically, can we trip this bug on public hardware that has the SNP enabled in the bios, but no SNP init in the host OS?
Enabling the SNP support on host is 3 step process:
step1 (bios): reserve memory for the RMP table.
Quoted 8 lines
step2 (host): initialize the RMP table memory, set the SYSCFG msr to enable the SNP feature
step3 (host): call the
SNP_INITto initialize the SNP firmware (this is needed only if you ever plan to launch SNP guest from this host).The "SNP globally enabled" means the step 1 to 2. The RMP checks are enforced as soon as step 2 is completed.
So I think that means we don't need to backport this to older kernels that don't know about SNP but might run on SNP enabled hardware (1), since those kernels won't do step2.
Dave
Quoted 176 lines
thanks
Quoted 173 lines
Dave
Quoted 170 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 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]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/lapic.c | 5 ++++- arch/x86/kvm/svm/sev.c | 35 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 16 ++++++++++++-- arch/x86/kvm/svm/svm.h | 1 + 6 files changed, 56 insertions(+), 3 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -122,6 +122,7 @@ KVM_X86_OP_NULL(enable_direct_tlbflush) KVM_X86_OP_NULL(migrate_timers) KVM_X86_OP(msr_filter_changed) KVM_X86_OP_NULL(complete_emulated_msr) +KVM_X86_OP(alloc_apic_backing_page)#undef KVM_X86_OP#undef KVM_X86_OP_NULLarch/x86/include/asm/kvm_host.h +1
-0@@ -1453,6 +1453,7 @@ struct kvm_x86_ops {int (*complete_emulated_msr)(struct kvm_vcpu *vcpu,int err);void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu,u8 vector); +void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); };struct kvm_x86_nested_ops {arch/x86/kvm/lapic.c +4
-1@@ -2457,7 +2457,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 +35
-0@@ -2703,3 +2703,38 @@ 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_to_phys (pfn),PMD_SIZE )) { + pfn++; +__free_page (p); + }else { +__free_page (pfn_to_page (pfn +1 )); + } + +return pfn_to_page (pfn); +}arch/x86/kvm/svm/svm.c +14
-2@@ -1368,7 +1368,7 @@ static int svm_create_vcpu(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;@@ -1377,7 +1377,7 @@ static int svm_create_vcpu(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;@@ -4539,6 +4539,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 = { .hardware_unsetup = svm_hardware_teardown, .hardware_enable = svm_hardware_enable,@@ -4667,6 +4677,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .complete_emulated_msr = svm_complete_emulated_msr, .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, + + .alloc_apic_backing_page = svm_alloc_apic_backing_page, };static struct kvm_x86_init_ops svm_init_ops__initdata = {arch/x86/kvm/svm/svm.h +1
-0@@ -575,6 +575,7 @@ void sev_es_create_vcpu(struct vcpu_svm *svm);void sev_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector);void sev_es_prepare_guest_switch (struct vcpu_svm *svm,unsigned int cpu);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.17.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH Part2 v5 21/45] KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
In reply to Brijesh SinghQuoted 24 lines · Brijesh Singh
Quoted 9 lines · Dr. David Alan Gilbert
Quoted 7 lines · * Brijesh Singh ([email protected]) wrote:
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.Can you explain what 'globally enabled' means?
This means that SNP is enabled in host SYSCFG_MSR.Snp=1. Once its enabled then RMP checks are enforced.
Quoted 2 lines
Or more specifically, can we trip this bug on public hardware that has the SNP enabled in the bios, but no SNP init in the host OS?
Enabling the SNP support on host is 3 step process:
step1 (bios): reserve memory for the RMP table.
Quoted 8 lines
step2 (host): initialize the RMP table memory, set the SYSCFG msr to enable the SNP feature
step3 (host): call the
SNP_INITto initialize the SNP firmware (this is needed only if you ever plan to launch SNP guest from this host).The "SNP globally enabled" means the step 1 to 2. The RMP checks are enforced as soon as step 2 is completed.
It might be good to update the code to reflect this reply, such that the new snp_safe_alloc_page() function introduced in this patch only deviates from "normal" page allocation logic when these two conditions are met. We could introduce a global variable, snp_globally_enabled that defaults to false and gets set to true after we write the SYSCFG MSR.
Quoted 76 lines
thanks
Quoted 74 lines
Dave
Quoted 71 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 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]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/lapic.c | 5 ++++- arch/x86/kvm/svm/sev.c | 35 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 16 ++++++++++++-- arch/x86/kvm/svm/svm.h | 1 + 6 files changed, 56 insertions(+), 3 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -122,6 +122,7 @@ KVM_X86_OP_NULL(enable_direct_tlbflush) KVM_X86_OP_NULL(migrate_timers) KVM_X86_OP(msr_filter_changed) KVM_X86_OP_NULL(complete_emulated_msr) +KVM_X86_OP(alloc_apic_backing_page)#undef KVM_X86_OP#undef KVM_X86_OP_NULLarch/x86/include/asm/kvm_host.h +1
-0@@ -1453,6 +1453,7 @@ struct kvm_x86_ops {int (*complete_emulated_msr)(struct kvm_vcpu *vcpu,int err);void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu,u8 vector); +void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); };struct kvm_x86_nested_ops {arch/x86/kvm/lapic.c +4
-1@@ -2457,7 +2457,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 +0
-0@@ -2703,3 +2703,38 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)break ; } }
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/svm/sev.c void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) · line 2713
Quoted 8 lines
arch/x86/kvm/svm/sev.c +8
-0+ +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);
Continuing my other comment, above: if we introduce a snp_globally_enabled var, we could use that here, rather than cpu_feature_enabled(X86_FEATURE_SEV_SNP).
Quoted 91 lines
Quoted 91 lines
Quoted 91 lines
arch/x86/kvm/svm/sev.c +27
-0+ +/* +* 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_to_phys (pfn),PMD_SIZE )) { + pfn++; +__free_page (p); + }else { +__free_page (pfn_to_page (pfn +1 )); + } + +return pfn_to_page (pfn); +}arch/x86/kvm/svm/svm.c +14
-2@@ -1368,7 +1368,7 @@ static int svm_create_vcpu(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;@@ -1377,7 +1377,7 @@ static int svm_create_vcpu(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;@@ -4539,6 +4539,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 = { .hardware_unsetup = svm_hardware_teardown, .hardware_enable = svm_hardware_enable,@@ -4667,6 +4677,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .complete_emulated_msr = svm_complete_emulated_msr, .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, + + .alloc_apic_backing_page = svm_alloc_apic_backing_page, };static struct kvm_x86_init_ops svm_init_ops__initdata = {arch/x86/kvm/svm/svm.h +1
-1@@ -575,6 +575,7 @@ void sev_es_create_vcpu(struct vcpu_svm *svm);void sev_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector);void sev_es_prepare_guest_switch (struct vcpu_svm *svm,unsigned int cpu);void sev_es_unmap_ghcb (struct vcpu_svm *svm); +struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu);/* vmenter.S */ --2.17.1
Re: [PATCH Part2 v5 21/45] KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
Quoted 11 lines · Marc Orr
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/svm/sev.c void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) · line 2713
Quoted 8 lines
arch/x86/kvm/svm/sev.c +8
-0+ +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);Continuing my other comment, above: if we introduce a
snp_globally_enabledvar, we could use that here, rather thancpu_feature_enabled(X86_FEATURE_SEV_SNP).
Maybe I am missing something, what is wrong with cpu_feature_enabled(...) check ? It's same as creating a global variable. The feature enabled bit is not set if the said is not enabled. See the patch #3 [1] in this series.
[1]
https://lore.kernel.org/linux-mm/[email protected]/T/#m2ac1242b33abfcd0d9fb22a89f4c103eacf67ea7
thanks
Re: [PATCH Part2 v5 21/45] KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
Quoted 22 lines · Brijesh Singh
Quoted 11 lines · Marc Orr
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/svm/sev.c void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) · line 2713
Quoted 8 lines
arch/x86/kvm/svm/sev.c +8
-0+ +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);Continuing my other comment, above: if we introduce a
snp_globally_enabledvar, we could use that here, rather thancpu_feature_enabled(X86_FEATURE_SEV_SNP).Maybe I am missing something, what is wrong with
cpu_feature_enabled(...)check ? It's same as creating a global variable. The feature enabled bit is not set if the said is not enabled. See the patch #3 [1] in this series.
Quoted 4 lines
[1]
https://lore.kernel.org/linux-mm/[email protected]/T/#m2ac1242b33abfcd0d9fb22a89f4c103eacf67ea7thanks
You are right. Patch #3 does exactly what I was asking for in snp_rmptable_init(). Thanks!
Re: [PATCH Part2 v5 21/45] KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
In reply to Dr. David Alan GilbertQuoted 24 lines · Dr. David Alan Gilbert
Quoted 23 lines · * Brijesh Singh ([email protected]) wrote:
Quoted 9 lines · Dr. David Alan Gilbert
Quoted 7 lines · * Brijesh Singh ([email protected]) wrote:
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.Can you explain what 'globally enabled' means?
This means that SNP is enabled in host SYSCFG_MSR.Snp=1. Once its enabled then RMP checks are enforced.
Quoted 2 lines
Or more specifically, can we trip this bug on public hardware that has the SNP enabled in the bios, but no SNP init in the host OS?
Enabling the SNP support on host is 3 step process:
step1 (bios): reserve memory for the RMP table.
step2 (host): initialize the RMP table memory, set the SYSCFG msr to enable the SNP feature
Quoted 8 lines
Quoted 5 lines
step3 (host): call the
SNP_INITto initialize the SNP firmware (this is needed only if you ever plan to launch SNP guest from this host).The "SNP globally enabled" means the step 1 to 2. The RMP checks are enforced as soon as step 2 is completed.
So I think that means we don't need to backport this to older kernels that don't know about SNP but might run on SNP enabled hardware (1), since those kernels won't do step2.
Correct.
thanks
Borislav Petkov
maintainerRe: [PATCH Part2 v5 03/45] x86/sev: Add the host SEV-SNP initialization support
In reply to Brijesh Singharch/x86/include/asm/msr-index.h line 550
Quoted 8 lines · Brijesh Singh
arch/x86/include/asm/msr-index.h +4
-0@@ -542,6 +544,10 @@#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_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)
While you're here, move all defines that belong to that MSR which we decided it is architectural after all, above the definition of MSR_AMD64_NB_CFG above.
Quoted 19 lines
arch/x86/include/asm/msr-index.h +0
-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 +2
-0@@ -24,6 +24,8 @@#include <linux/sev-guest.h> #include <linux/platform_device.h> #include <linux/io.h> +#include <linux/cpumask.h> +#include <linux/iommu.h> #include <asm/cpu_entry_area.h> #include <asm/stacktrace.h> @@ -40,11 +42,19 @@#include <asm/efi.h> #include <asm/cpuid.h>
arch/x86/kernel/sev.c line 53
Quoted 10 lines
arch/x86/kernel/sev.c +4
-0#include <asm/setup.h> +#include <asm/apic.h> +#include <asm/iommu.h> #include "sev-internal.h" #define DR7_RESET_VALUE 0x400 +/* +* The first 16KB from the RMP_BASE is used by the processor for the
s/for the/for/
arch/x86/kernel/sev.c line 54
Quoted 1 line
arch/x86/kernel/sev.c +1
-0+* bookkeeping, the range need to be added during the RMP entry lookup.
s/need/needs/ ... s/during the/during /
arch/x86/kernel/sev.c line 56
Quoted 2 lines
arch/x86/kernel/sev.c +2
-0+*/ +#define RMPTABLE_CPU_BOOKKEEPING_SZ 0x4000
Too long: just call it RMP_BASE_OFFSET.
Quoted 14 lines
arch/x86/kernel/sev.c +14
-0+static bool get_rmptable_info (u64 *start,u64 *len) +{ +u64 calc_rmp_sz, rmp_sz, rmp_base, rmp_end, nr_pages; + +rdmsrl (MSR_AMD64_RMP_BASE , rmp_base); +rdmsrl (MSR_AMD64_RMP_END , rmp_end); + +if (!rmp_base || !rmp_end) { +pr_info ("Memory for the RMP table has not been reserved by BIOS\n" ); +return false; + } + + rmp_sz = rmp_end - rmp_base +1 ; +
arch/x86/kernel/sev.c static int __init add_snp_guest_request(void) · line 2297
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+/* +* Calculate the amount the memory that must be reserved by the BIOS to +* address the full system RAM. The reserved memory should also cover the +* RMP table itself. +* +* See PPR Family 19h Model 01h, Revision B1 section 2.1.5.2 for more +* information on memory requirement. +*/ + nr_pages =totalram_pages (); + calc_rmp_sz = (((rmp_sz >>PAGE_SHIFT ) + nr_pages) <<4 ) +RMPTABLE_CPU_BOOKKEEPING_SZ ;
use totalram_pages() directly here - nr_pages is assigned to only once.
arch/x86/kernel/sev.c static int __init add_snp_guest_request(void) · line 2308
Quoted 11 lines
arch/x86/kernel/sev.c +11
-0+ +if (calc_rmp_sz > rmp_sz) { +pr_info ("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);
"RMP table physical address range: [ ... - ... ]\n", ...
...
Quoted 3 lines
+/*
+ * This must be called after the PCI subsystem. This is because before enabling
+ * the SNP feature we need to ensure that IOMMU supports the SEV-SNP feature.
"... it needs to be ensured that... "
Quoted 7 lines
+ * The
iommu_sev_snp_support()is used for checking the feature, and it is
+ * available aftersubsys_initcall().
+ */
+fs_initcall(snp_rmptable_init);Signature · 1 line
2.17.1
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerRe: [PATCH Part2 v5 04/45] x86/sev: Add RMP entry lookup helpers
In reply to Brijesh SinghQuoted 7 lines · Brijesh Singh
arch/x86/include/asm/sev.h +7
-0+struct __packed rmpentry { + union { + struct { + u64 assigned : 1, + pagesize : 1, + immutable : 1, + rsvd1 : 9,
arch/x86/include/asm/sev.h extern bool handle_vc_boot_ghcb(struct pt_regs *regs); · line 103
Quoted 10 lines
arch/x86/include/asm/sev.h +10
-0+ gpa : 39, + asid : 10, + vmsa : 1, + validated : 1, + rsvd2 : 1; + } info; +u64 low; + }; +u64 high; +};
__packed goes at the end of the struct definition.
arch/x86/include/asm/sev.h extern bool handle_vc_boot_ghcb(struct pt_regs *regs); · line 106
Quoted 3 lines
arch/x86/include/asm/sev.h +3
-0+ +#define rmpentry_assigned (x) ((x)->info.assigned) +#define rmpentry_pagesize (x) ((x)->info.pagesize)
Inline functions pls so that you can get typechecking too.
Quoted 21 lines
#define
RMPADJUST_VMSA_PAGE_BITBIT(16)arch/x86/kernel/sev.c +3
-0@@ -54,6 +54,8 @@* bookkeeping, the range need to be added during the RMP entry lookup. */ #define RMPTABLE_CPU_BOOKKEEPING_SZ 0x4000 +#define RMPENTRY_SHIFT 8 +#define rmptable_page_offset (x) (RMPTABLE_CPU_BOOKKEEPING_SZ + (((unsigned long)x) >> RMPENTRY_SHIFT))/* For early boot hypervisor communication in SEV-ES enabled guests */ static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE);@@ -2376,3 +2378,44 @@ static int __init snp_rmptable_init(void)* available after subsys_initcall(). */ fs_initcall (snp_rmptable_init); +
arch/x86/kernel/sev.c static int __init snp_rmptable_init(void) · line 2391
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+static struct rmpentry *__snp_lookup_rmpentry (u64 pfn,int *level) +{ +unsigned long vaddr, paddr = pfn <<PAGE_SHIFT ; +struct rmpentry *entry, *large_entry; + +if (!pfn_valid (pfn)) +return ERR_PTR (-EINVAL ); + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return ERR_PTR (-ENXIO );
I think that check should happen first.
arch/x86/kernel/sev.c static int __init snp_rmptable_init(void) · line 2395
Quoted 4 lines
arch/x86/kernel/sev.c +4
-0+ + vaddr = rmptable_start +rmptable_page_offset (paddr); +if (unlikely (vaddr > rmptable_end)) +return ERR_PTR (-ENXIO );
Maybe the above -E should be -ENOENT instead so that you have unique error types for each check to facilitate debugging.
arch/x86/kernel/sev.c static int __init snp_rmptable_init(void) · line 2399
Quoted 4 lines
arch/x86/kernel/sev.c +4
-0+ + entry = (struct rmpentry *)vaddr; + +/* Read a large RMP entry to get the correct page level used in RMP entry. */
That comment needs rewriting.
arch/x86/kernel/sev.c static int __init snp_rmptable_init(void) · line 2410
Quoted 11 lines
arch/x86/kernel/sev.c +11
-0+ vaddr = rmptable_start +rmptable_page_offset (paddr &PMD_MASK ); + large_entry = (struct rmpentry *)vaddr; + *level =RMP_TO_X86_PG_LEVEL (rmpentry_pagesize (large_entry)); + +return entry; +} + +/* +* Return 1 if the RMP entry is assigned, 0 if it exists but is not assigned, +* and -errno if there is no corresponding RMP entry. +*/
kernel-doc format since it is being exported.
arch/x86/kernel/sev.c static int __init snp_rmptable_init(void) · line 2421
Quoted 11 lines
arch/x86/kernel/sev.c +11
-0+int snp_lookup_rmpentry (u64 pfn,int *level) +{ +struct rmpentry *e; + + e =__snp_lookup_rmpentry (pfn, level); +if (IS_ERR (e)) +return PTR_ERR (e); + +return !!rmpentry_assigned (e); +} +EXPORT_SYMBOL_GPL (snp_lookup_rmpentry);
This export is for kvm, I presume?
Quoted 27 lines
include/linux/sev.h new file +20
-0@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* AMD Secure Encrypted Virtualization +* +* Author: Brijesh Singh <[email protected]> +*/ + +#ifndef __LINUX_SEV_H +#define __LINUX_SEV_H + +/* RMUPDATE detected 4K page and 2MB page overlap. */ +#define RMPUPDATE_FAIL_OVERLAP 7 + +#ifdef CONFIG_AMD_MEM_ENCRYPT +int snp_lookup_rmpentry (u64 pfn,int *level); +int psmash (u64 pfn); +int rmp_make_private (u64 pfn,u64 gpa,enum pg_level level,int asid,bool immutable); +int rmp_make_shared (u64 pfn,enum pg_level level); +#else +static inline int snp_lookup_rmpentry (u64 pfn,int *level) {return 0 ; }
include/linux/sev.h line 30
Quoted 10 lines
include/linux/sev.h new file +10
-0+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 /* CONFIG_AMD_MEM_ENCRYPT */ +#endif /* __LINUX_SEV_H */
What is going to use this linux/ namespace header?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerRe: [PATCH Part2 v5 05/45] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
In reply to Brijesh SinghQuoted 29 lines · Brijesh Singh
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.
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/sev.h | 11 ++++++ arch/x86/kernel/sev.c | 72 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+)arch/x86/include/asm/sev.h +2
-0@@ -78,7 +78,9 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs);/* 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)/* * The RMP entry format is not architectural. The format is defined in PPR
arch/x86/include/asm/sev.h struct __packed rmpentry { · line 112
Quoted 4 lines
arch/x86/include/asm/sev.h +1
-0@@ -107,6 +109,15 @@ struct __packed rmpentry {#define RMPADJUST_VMSA_PAGE_BIT BIT(16) +struct rmpupdate {
Function is called the same way - maybe this should be called rmpupdate_desc or so.
Quoted 16 lines
arch/x86/include/asm/sev.h +8
-0+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);arch/x86/kernel/sev.c +0
-0
arch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2422
Quoted 4 lines
arch/x86/kernel/sev.c +1
-0@@ -2419,3 +2419,75 @@ int snp_lookup_rmpentry(u64 pfn, int *level)return !!rmpentry_assigned (e); }EXPORT_SYMBOL_GPL (snp_lookup_rmpentry); +
<--- kernel-doc comment.
arch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2432
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+int psmash (u64 pfn) +{ +unsigned long paddr = pfn <<PAGE_SHIFT ; +int ret; + +if (!pfn_valid (pfn)) +return -EINVAL ; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return -ENXIO ;
Make that the first check pls.
arch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2442
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+ +/* Binutils version 2.36 supports the PSMASH mnemonic. */ + asmvolatile (".byte 0xF3, 0x0F, 0x01, 0xFF" + :"=a" (ret) + :"a" (paddr) + :"memory" ,"cc" ); + +return ret; +} +EXPORT_SYMBOL_GPL (psmash);
That's for kvm?
arch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2453
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+static int rmpupdate (u64 pfn,struct rmpupdate *val) +{ +unsigned long paddr = pfn <<PAGE_SHIFT ; +int ret; + +if (!pfn_valid (pfn)) +return -EINVAL ; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return -ENXIO ;
Also first check.
Quoted 5 lines
arch/x86/kernel/sev.c +5
-0+ +/* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ + asmvolatile (".byte 0xF2, 0x0F, 0x01, 0xFE" + :"=a" (ret) + : "a"(paddr), "c"((unsigned long)val)
arch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2468
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+ : "memory", "cc"); +return ret; +} + +int rmp_make_private (u64 pfn,u64 gpa,enum pg_level level,int asid,bool immutable) +{ +struct rmpupdate val; + +if (!pfn_valid (pfn)) +return -EINVAL ;
rmpupdate() does that check too so choose one place please and kill the other.
Quoted 14 lines
arch/x86/kernel/sev.c +14
-0+memset (&val,0 ,sizeof (val)); + val.assigned =1 ; + val.asid = asid; + val.immutable = immutable; + val.gpa = gpa; + val.pagesize =X86_TO_RMP_PG_LEVEL (level); + +return rmpupdate (pfn, &val); +} +EXPORT_SYMBOL_GPL (rmp_make_private); + +int rmp_make_shared (u64 pfn,enum pg_level level) +{ +struct rmpupdate val;
arch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2493
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+ +if (!pfn_valid (pfn)) +return -EINVAL ; + +memset (&val,0 ,sizeof (val)); + val.pagesize =X86_TO_RMP_PG_LEVEL (level); + +return rmpupdate (pfn, &val); +} +EXPORT_SYMBOL_GPL (rmp_make_shared);
Both exports for kvm I assume?
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH Part2 v5 04/45] x86/sev: Add RMP entry lookup helpers
In reply to Borislav PetkovHi Boris,
I agreed with all of your comment, responding to your specific questions.
On 9/24/21 4:49 AM, Borislav Petkov wrote:
...
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+} +EXPORT_SYMBOL_GPL(snp_lookup_rmpentry);This export is for kvm, I presume?
yes, both KVM and CCP (i.e PSP) driver will need to lookup RMP entries.
Quoted 32 lines
Quoted 32 lines
include/linux/sev.h new file +25
-0@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* AMD Secure Encrypted Virtualization +* +* Author: Brijesh Singh <[email protected]> +*/ + +#ifndef __LINUX_SEV_H +#define __LINUX_SEV_H + +/* RMUPDATE detected 4K page and 2MB page overlap. */ +#define RMPUPDATE_FAIL_OVERLAP 7 + +#ifdef CONFIG_AMD_MEM_ENCRYPT +int snp_lookup_rmpentry(u64 pfn, int *level); +int psmash(u64 pfn); +int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asid, bool immutable); +int rmp_make_shared(u64 pfn, enum pg_level level); +#else +static inline int snp_lookup_rmpentry(u64 pfn, int *level) { return 0; } +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;
Quoted 9 lines
include/linux/sev.h line 30
Quoted 6 lines
include/linux/sev.h new file +5
-0+} +static inline int rmp_make_shared (u64 pfn,enum pg_level level) {return -ENODEV ; } + +#endif /* CONFIG_AMD_MEM_ENCRYPT */ +#endif /* __LINUX_SEV_H */ What is going to use this linux/ namespace header?
The kvm and ccp drivers.
Re: [PATCH Part2 v5 04/45] x86/sev: Add RMP entry lookup helpers
Quoted 6 lines · Brijesh Singh
Quoted 4 lines
Quoted 1 line
What is going to use this linux/ namespace header?
The kvm and ccp drivers.
Currently, we have only x86 drivers using it, are you thinking to move this to arch/x86/include/asm/ ?
thanks
Re: [PATCH Part2 v5 05/45] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
In reply to Borislav PetkovQuoted 31 lines · Borislav Petkov
Quoted 30 lines · Brijesh Singh
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.
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/sev.h | 11 ++++++ arch/x86/kernel/sev.c | 72 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+)arch/x86/include/asm/sev.h +2
-0@@ -78,7 +78,9 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs);/* 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)/* * The RMP entry format is not architectural. The format is defined in PPR @@ -107,6 +109,15 @@ struct __packed rmpentry {
Quoted 6 lines
arch/x86/include/asm/sev.h struct __packed rmpentry { · line 112
Quoted 3 lines
arch/x86/include/asm/sev.h +1
-0#define RMPADJUST_VMSA_PAGE_BIT BIT(16) +struct rmpupdate {Function is called the same way - maybe this should be called
rmpupdate_descor so.
Noted.
Quoted 6 lines
arch/x86/include/asm/sev.h struct __packed rmpentry { · line 116
Quoted 4 lines
arch/x86/include/asm/sev.h +4
-0+ +return ret; +} +EXPORT_SYMBOL_GPL (psmash);That's for kvm?
Yes.
...
Quoted 3 lines
arch/x86/include/asm/sev.h struct __packed rmpentry { · line 117
Quoted 1 line
arch/x86/include/asm/sev.h +1
-0+EXPORT_SYMBOL_GPL (rmp_make_shared);Both exports for kvm I assume?
yes, both KVM and CCP drivers need them.
thanks
Re: [PATCH Part2 v5 25/45] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_UPDATE command
In reply to Brijesh SinghQuoted 135 lines · Brijesh Singh
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.
Signed-off-by Brijesh Singh <[email protected]>--- .../virt/kvm/amd-memory-encryption.rst | 29 +++ arch/x86/kvm/svm/sev.c | 187 ++++++++++++++++++ include/uapi/linux/kvm.h | 19 ++ 3 files changed, 235 insertions(+)Documentation/virt/kvm/amd-memory-encryption.rst +29
-0@@ -478,6 +478,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 +52
-0@@ -17,6 +17,7 @@#include <linux/misc_cgroup.h> #include <linux/processor.h> #include <linux/trace_events.h> +#include <linux/sev.h> #include <asm/fpu/internal.h> #include <asm/pkru.h> @@ -227,6 +228,49 @@ static void sev_decommission(unsigned int handle)sev_guest_decommission (&decommission,NULL ); } +static inline void snp_leak_pages (u64 pfn,enum pg_level level) +{ +unsigned int npages =page_level_size (level) >>PAGE_SHIFT ; + +WARN (1 ,"psc failed pfn 0x%llx pages %d (leaking)\n" , pfn, npages); + +while (npages) { +memory_failure (pfn,0 ); +dump_rmpentry (pfn); + npages--; + pfn++; + } +} + +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 =snp_guest_page_reclaim (&data, &err); +if (rc) { +/* +* If the reclaim failed, then page is no longer safe +* to use. +*/ +snp_leak_pages (pfn,PG_LEVEL_4K ); + } + +return rc; +} + +static int host_rmp_make_shared (u64 pfn,enum pg_level level,bool leak) +{ +int rc; + + rc =rmp_make_shared (pfn, level); +if (rc && leak) +snp_leak_pages (pfn, level); + +return rc; +} +static void sev_unbind_asid (struct kvm *kvm,unsigned int handle) {struct sev_data_deactivate deactivate;@@ -1620,6 +1664,123 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)return rc; } +static bool is_hva_registered (struct kvm *kvm,hva_t hva,size_t len) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct list_head *head = &sev->regions_list; +struct enc_region *i; + +lockdep_assert_held (&kvm->lock); +
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1684
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+list_for_each_entry (i, head, list) { +u64 start = i->uaddr; +u64 end = start + i->size; + +if (start <= hva && end >= (hva + len)) +return true; + } + +return false; +}
Internally we actually register the guest memory in chunks for various reasons. So for our largest SEV VM we have 768 1 GB entries in |sev->regions_list|. This was OK before because no look ups were done. Now that we are performing a look ups a linked list with linear time lookups seems not ideal, could we switch the back data structure here to something more conducive too fast lookups?
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1691
Quoted 7 lines
arch/x86/kvm/svm/sev.c +7
-0+ +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 sev_data_snp_launch_update data = {0 }; +struct kvm_sev_snp_launch_update params; +unsigned long npages, pfn, n =0 ;
Could we have a slightly more descriptive name for |n|? nprivate maybe? Also why not zero in the loop below?
for (i = 0, n = 0; i < npages; ++i)
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1694
Quoted 3 lines
arch/x86/kvm/svm/sev.c +3
-0+int *error = &argp->error; +struct page **inpages; +int ret, i, level;
Should |i| be an unsigned long since it can is tracked in a for loop with "i < npages" npages being an unsigned long? (|n| too)
Quoted 18 lines
arch/x86/kvm/svm/sev.c +18
-0+u64 gfn; + +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 ; + +/* Verify that the specified address range is registered. */ +if (!is_hva_registered (kvm, params.uaddr, params.len)) +return -EINVAL ; + +/* +* The userspace memory is already locked so technically we don't +* need to lock it again. Later part of the function needs to know
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1722
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+* pfn so call the sev_pin_memory() so that we can get the list of +* pages to iterate through. +*/ + inpages =sev_pin_memory (kvm, params.uaddr, params.len, &npages,1 ); +if (!inpages) +return -ENOMEM ; + +/* +* Verify that all the pages are marked shared in the RMP table before +* going further. This is avoid the cases where the userspace may try
This is *too* avoid cases...
Quoted 27 lines
arch/x86/kvm/svm/sev.c +27
-0+* updating the same page twice. +*/ +for (i =0 ; i < npages; i++) { +if (snp_lookup_rmpentry (page_to_pfn (inpages[i]), &level) !=0 ) { +sev_unpin_memory (kvm, inpages, npages); +return -EFAULT ; + } + } + + gfn = params.start_gfn; + level =PG_LEVEL_4K ; + data.gctx_paddr =__psp_pa (sev->snp_context); + +for (i =0 ; i < npages; i++) { + pfn =page_to_pfn (inpages[i]); + + ret =rmp_make_private (pfn, gfn <<PAGE_SHIFT , level,sev_get_asid (kvm), true); +if (ret) { + ret = -EFAULT ; + goto e_unpin; + } + + n++; + data.address =__sme_page_pa (inpages[i]); + data.page_size =X86_TO_RMP_PG_LEVEL (level); + data.page_type = params.page_type; + data.vmpl3_perms = params.vmpl3_perms;
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1759
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ 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) { +/* +* If the command failed then need to reclaim the page. +*/ +snp_page_reclaim (pfn); + goto e_unpin; + }
Hmm if this call fails after the first iteration of this loop it will lead to a hard to reproduce LaunchDigest right? Say if we are SnpLaunchUpdating just 2 pages A and B. If we first call this ioctl and A is SNP_LAUNCH_UPDATED'd but B fails, we then make A shared again in the RMP. So we must call the ioctl with 2 pages again, after fixing the issue with page B. Now the Launch digest has something like Hash(A) then HASH(A & B) right (overly simplified) so A will be included twice right? I am not sure if anything better can be done here but might be worth documenting IIUC.
Quoted 108 lines
arch/x86/kvm/svm/sev.c +50
-0+ + gfn++; + } + +e_unpin: +/* Content of memory is updated, mark pages dirty */ +for (i =0 ; i < n; i++) { +set_page_dirty_lock (inpages[i]); +mark_page_accessed (inpages[i]); + +/* +* If its an error, then update RMP entry to change page ownership +* to the hypervisor. +*/ +if (ret) +host_rmp_make_shared (pfn, level, true); + } + +/* Unlock the user pages */ +sev_unpin_memory (kvm, inpages, npages); + +return ret; +} +int svm_mem_enc_op (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;@@ -1712,6 +1873,9 @@ int svm_mem_enc_op(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;@@ -1794,6 +1958,29 @@ find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)static void __unregister_enc_region_locked (struct kvm *kvm,struct enc_region *region) { +unsigned long i, pfn; +int level; + +/* +* The guest memory pages are assigned in the RMP table. Unassign it +* before releasing the memory. +*/ +if (sev_snp_guest (kvm)) { +for (i =0 ; i < region->npages; i++) { + pfn =page_to_pfn (region->pages[i]); + +if (!snp_lookup_rmpentry (pfn, &level)) +continue ; + +cond_resched (); + +if (level >PG_LEVEL_4K ) + pfn &= ~(KVM_PAGES_PER_HPAGE (PG_LEVEL_2M ) -1 ); + +host_rmp_make_shared (pfn, level, true); + } + } +sev_unpin_memory (kvm, region->pages, region->npages);list_del (®ion->list);kfree (region);include/uapi/linux/kvm.h +19
-1@@ -1715,6 +1715,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, };@@ -1831,6 +1832,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) --2.17.1
Re: [PATCH Part2 v5 25/45] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_UPDATE command
On 9/27/21 11:43 AM, Peter Gonda wrote:
...
Quoted 18 lines
Quoted 18 lines
arch/x86/kvm/svm/sev.c +17
-0+static bool is_hva_registered (struct kvm *kvm,hva_t hva,size_t len) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct list_head *head = &sev->regions_list; +struct enc_region *i; + +lockdep_assert_held (&kvm->lock); + +list_for_each_entry (i, head, list) { +u64 start = i->uaddr; +u64 end = start + i->size; + +if (start <= hva && end >= (hva + len)) +return true; + } + +return false;
Quoted 9 lines
Quoted 1 line
+}
Internally we actually register the guest memory in chunks for various reasons. So for our largest SEV VM we have 768 1 GB entries in |sev->regions_list|. This was OK before because no look ups were done. Now that we are performing a look ups a linked list with linear time lookups seems not ideal, could we switch the back data structure here to something more conducive too fast lookups?
Quoted 1 line
+
Interesting, for qemu we had very few number of regions so there was no strong reason for me to think something otherwise. Do you have any preference on what data structure you will use ?
Quoted 10 lines
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1691
Quoted 6 lines
arch/x86/kvm/svm/sev.c +6
-0+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 sev_data_snp_launch_update data = {0 }; +struct kvm_sev_snp_launch_update params; +unsigned long npages, pfn, n =0 ;Could we have a slightly more descriptive name for |n|? nprivate maybe? Also why not zero in the loop below?
Sure, I will pick a better name and no need to zero above. I will fix it.
Quoted 9 lines
for (i = 0, n = 0; i < npages; ++i)
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1694
Quoted 3 lines
arch/x86/kvm/svm/sev.c +3
-0+int *error = &argp->error; +struct page **inpages; +int ret, i, level;Should |i| be an unsigned long since it can is tracked in a for loop with "i < npages" npages being an unsigned long? (|n| too)
Noted.
Quoted 22 lines
Quoted 22 lines
arch/x86/kvm/svm/sev.c +22
-0+u64 gfn; + +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 ; + +/* Verify that the specified address range is registered. */ +if (!is_hva_registered (kvm, params.uaddr, params.len)) +return -EINVAL ; + +/* +* The userspace memory is already locked so technically we don't +* need to lock it again. Later part of the function needs to know +* pfn so call the sev_pin_memory() so that we can get the list of +* pages to iterate through. +*/ + inpages =sev_pin_memory (kvm, params.uaddr, params.len, &npages,1 );
Quoted 9 lines
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1722
Quoted 6 lines
arch/x86/kvm/svm/sev.c +6
-0+if (!inpages) +return -ENOMEM ; + +/* +* Verify that all the pages are marked shared in the RMP table before +* going further. This is avoid the cases where the userspace may try This is *too* avoid cases...
Noted
Quoted 39 lines
Quoted 27 lines
arch/x86/kvm/svm/sev.c +27
-0+* updating the same page twice. +*/ +for (i =0 ; i < npages; i++) { +if (snp_lookup_rmpentry (page_to_pfn (inpages[i]), &level) !=0 ) { +sev_unpin_memory (kvm, inpages, npages); +return -EFAULT ; + } + } + + gfn = params.start_gfn; + level =PG_LEVEL_4K ; + data.gctx_paddr =__psp_pa (sev->snp_context); + +for (i =0 ; i < npages; i++) { + pfn =page_to_pfn (inpages[i]); + + ret =rmp_make_private (pfn, gfn <<PAGE_SHIFT , level,sev_get_asid (kvm), true); +if (ret) { + ret = -EFAULT ; + goto e_unpin; + } + + n++; + data.address =__sme_page_pa (inpages[i]); + data.page_size =X86_TO_RMP_PG_LEVEL (level); + data.page_type = params.page_type; + data.vmpl3_perms = params.vmpl3_perms;arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1759
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ 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) { +/* +* If the command failed then need to reclaim the page. +*/ +snp_page_reclaim (pfn); + goto e_unpin; + }Hmm if this call fails after the first iteration of this loop it will
Quoted 9 lines
lead to a hard to reproduce LaunchDigest right? Say if we are SnpLaunchUpdating just 2 pages A and B. If we first call this ioctl and A is SNP_LAUNCH_UPDATED'd but B fails, we then make A shared again in the RMP. So we must call the ioctl with 2 pages again, after fixing the issue with page B. Now the Launch digest has something like Hash(A) then
HASH(A & B)right (overly simplified) so A will be included twice right? I am not sure if anything better can be done here but might be worth documenting IIUC.
I can add a comment in documentation that if a LAUNCH_UPDATE fails then user need to destroy the existing context and start from the beginning. I am not sure if we want to support the partial update cases. But in case we have two choices a) decommission the context on failure or b) add a new command to destroy the existing context.
Quoted 108 lines
Quoted 108 lines
arch/x86/kvm/svm/sev.c +50
-0+ + gfn++; + } + +e_unpin: +/* Content of memory is updated, mark pages dirty */ +for (i =0 ; i < n; i++) { +set_page_dirty_lock (inpages[i]); +mark_page_accessed (inpages[i]); + +/* +* If its an error, then update RMP entry to change page ownership +* to the hypervisor. +*/ +if (ret) +host_rmp_make_shared (pfn, level, true); + } + +/* Unlock the user pages */ +sev_unpin_memory (kvm, inpages, npages); + +return ret; +} +int svm_mem_enc_op (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;@@ -1712,6 +1873,9 @@ int svm_mem_enc_op(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;@@ -1794,6 +1958,29 @@ find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)static void __unregister_enc_region_locked (struct kvm *kvm,struct enc_region *region) { +unsigned long i, pfn; +int level; + +/* +* The guest memory pages are assigned in the RMP table. Unassign it +* before releasing the memory. +*/ +if (sev_snp_guest (kvm)) { +for (i =0 ; i < region->npages; i++) { + pfn =page_to_pfn (region->pages[i]); + +if (!snp_lookup_rmpentry (pfn, &level)) +continue ; + +cond_resched (); + +if (level >PG_LEVEL_4K ) + pfn &= ~(KVM_PAGES_PER_HPAGE (PG_LEVEL_2M ) -1 ); + +host_rmp_make_shared (pfn, level, true); + } + } +sev_unpin_memory (kvm, region->pages, region->npages);list_del (®ion->list);kfree (region);include/uapi/linux/kvm.h +19
-1@@ -1715,6 +1715,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, };@@ -1831,6 +1832,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) --2.17.1
Re: [PATCH Part2 v5 37/45] KVM: SVM: Add support to handle MSR based Page State Change VMGEXIT
In reply to Brijesh SinghQuoted 69 lines · * Brijesh Singh ([email protected]) wrote:
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.
Before changing the page state in the RMP entry, lookup the page in the NPT to make sure that there is a valid mapping for it. If the mapping exist then try to find a workable page level between the NPT and RMP for the page. If the page is not mapped in the NPT, then create a fault such that it gets mapped before we change the page state in the RMP entry.
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/sev-common.h | 9 ++ arch/x86/kvm/svm/sev.c | 197 ++++++++++++++++++++++++++++++ arch/x86/kvm/trace.h | 34 ++++++ arch/x86/kvm/x86.c | 1 + 4 files changed, 241 insertions(+)arch/x86/include/asm/sev-common.h +9
-0@@ -89,6 +89,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) | \@@ -98,6 +102,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 +9
-0@@ -31,6 +31,7 @@#include "svm_ops.h" #include "cpuid.h" #include "trace.h" +#include "mmu.h" #define __ex (x) __kvm_handle_fault_on_reboot(x)@@ -2905,6 +2906,181 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) svm->vmcb->control.ghcb_gpa = value; } +static int snp_rmptable_psmash (struct kvm *kvm,kvm_pfn_t pfn) +{ + pfn = pfn & ~(KVM_PAGES_PER_HPAGE (PG_LEVEL_2M ) -1 ); + +return psmash (pfn); +} + +static int snp_make_page_shared (struct kvm *kvm,gpa_t gpa,kvm_pfn_t pfn,int level)
....
arch/x86/kvm/svm/sev.c static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) · line 3028
Quoted 3 lines
arch/x86/kvm/svm/sev.c +3
-0+ +/* +* Mark the userspace range unmerable before adding the pages
^^^^^^^^^ typo
Quoted 25 lines
arch/x86/kvm/svm/sev.c +25
-0+* in the RMP table. +*/ +mmap_write_lock (kvm->mm); + rc =snp_mark_unmergable (kvm, hva,page_level_size (level)); +mmap_write_unlock (kvm->mm); +if (rc) +return -EINVAL ; + } + +write_lock (&kvm->mmu_lock); + + rc =kvm_mmu_get_tdp_walk (vcpu, gpa, &pfn, &npt_level); +if (!rc) { +/* +* This may happen if another vCPU unmapped the page +* before we acquire the lock. Retry the PSC. +*/ +write_unlock (&kvm->mmu_lock); +return 0 ; + } + +/* +* Adjust the level so that we don't go higher than the backing +* page level. +*/
arch/x86/kvm/svm/sev.c static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) · line 3063
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ level =min_t (size_t, level, npt_level); + +trace_kvm_snp_psc (vcpu->vcpu_id, pfn, gpa, op, level); + +switch (op) { +case SNP_PAGE_STATE_SHARED : + rc =snp_make_page_shared (kvm, gpa, pfn, level); +break ; +case SNP_PAGE_STATE_PRIVATE : + rc =rmp_make_private (pfn, gpa, level, sev->asid, false);
Minor nit; it seems a shame that snp_make_page_shared and rmp_make_private both take gpa, pfn, level - in different orders.
Dave
Quoted 116 lines
arch/x86/kvm/svm/sev.c +41
-0+break ; +default : + rc = -EINVAL ; +break ; + } + +write_unlock (&kvm->mmu_lock); + +if (rc) { +pr_err_ratelimited ("Error op %d gpa %llx pfn %llx level %d rc %d\n" , + op, gpa, pfn, level, rc); +return rc; + } + + gpa = gpa +page_level_size (level); + } + +return 0 ; +} +static int sev_handle_vmgexit_msr_protocol (struct vcpu_svm *svm) {struct vmcb_control_area *control = &svm->vmcb->control;@@ -3005,6 +3181,27 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_POS);break ; } +case GHCB_MSR_PSC_REQ : { +gfn_t gfn; +int ret; +enum psc_op op; + + gfn =get_ghcb_msr_bits (svm,GHCB_MSR_PSC_GFN_MASK ,GHCB_MSR_PSC_GFN_POS ); + op =get_ghcb_msr_bits (svm,GHCB_MSR_PSC_OP_MASK ,GHCB_MSR_PSC_OP_POS ); + + ret =__snp_handle_page_state_change (vcpu, op,gfn_to_gpa (gfn),PG_LEVEL_4K ); + +if (ret) +set_ghcb_msr_bits (svm,GHCB_MSR_PSC_ERROR , +GHCB_MSR_PSC_ERROR_MASK ,GHCB_MSR_PSC_ERROR_POS ); +else +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 ); +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@@ -1711,6 +1712,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@@ -12371,3 +12371,4 @@ 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);Signature · 1 line
2.17.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH Part2 v5 38/45] KVM: SVM: Add support to handle Page State Change VMGEXIT
In reply to Brijesh SinghQuoted 92 lines · * Brijesh Singh ([email protected]) wrote:
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.
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/sev-common.h | 7 +++ arch/x86/kvm/svm/sev.c | 82 +++++++++++++++++++++++++++++-- 2 files changed, 84 insertions(+), 5 deletions(-)arch/x86/include/asm/sev-common.h +7
-0@@ -126,6 +126,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 +10
-5@@ -2731,6 +2731,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm, u64 *exit_code)case SVM_VMGEXIT_AP_JUMP_TABLE :case SVM_VMGEXIT_UNSUPPORTED_EVENT :case SVM_VMGEXIT_HV_FEATURES : +case SVM_VMGEXIT_PSC :break ;default : goto vmgexit_err;@@ -3004,13 +3005,13 @@ static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, enum psc_op op,*/ rc =snp_check_and_build_npt (vcpu, gpa, level);if (rc) -return -EINVAL ; +return PSC_UNDEF_ERR ;if (op ==SNP_PAGE_STATE_PRIVATE ) {hva_t hva;if (snp_gpa_to_hva (kvm, gpa, &hva)) -return -EINVAL ; +return PSC_UNDEF_ERR ;/* * Verify that the hva range is registered. This enforcement is @@ -3022,7 +3023,7 @@ static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, enum psc_op op, rc =is_hva_registered (kvm, hva,page_level_size (level));mutex_unlock (&kvm->lock);if (!rc) -return -EINVAL ; +return PSC_UNDEF_ERR ;/* * Mark the userspace range unmerable before adding the pages @@ -3032,7 +3033,7 @@ static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, enum psc_op op, rc =snp_mark_unmergable (kvm, hva,page_level_size (level));mmap_write_unlock (kvm->mm);if (rc) -return -EINVAL ; +return PSC_UNDEF_ERR ; }write_lock (&kvm->mmu_lock);@@ -3062,8 +3063,11 @@ static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, enum psc_op op,case SNP_PAGE_STATE_PRIVATE : rc =rmp_make_private (pfn, gpa, level, sev->asid, false);break ; +case SNP_PAGE_STATE_PSMASH : +case SNP_PAGE_STATE_UNSMASH : +/* TODO: Add support to handle it */ default : - rc = -EINVAL ; + rc =PSC_INVALID_ENTRY ;break ; }@@ -3081,6 +3085,65 @@ static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, enum psc_op op,return 0 ; } +static inline unsigned long map_to_psc_vmgexit_code (int rc)
arch/x86/kvm/svm/sev.c static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, enum psc_op op, · line 3098
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+{ +switch (rc) { +case PSC_INVALID_HDR : +return ((1ul <<32 ) |1 ); +case PSC_INVALID_ENTRY : +return ((1ul <<32 ) |2 ); +case RMPUPDATE_FAIL_OVERLAP : +return ((3ul <<32 ) |2 ); +default :return (4ul <<32 ); + }
Are these the values defined in 56421 section 4.1.6 ?
If so, that says:
SW_EXITINFO2[63:32] == 0x00000100
The hypervisor encountered some other error situation and was not able to complete the request identified by page_state_change_header.cur_entry. It is left to the guest to decide how to proceed in this situation.
so it looks like the default should be 0x100 rather than 4?
(It's a shame they're all magical constants, it would be nice if the standard have them names)
Dave
Quoted 71 lines
arch/x86/kvm/svm/sev.c +57
-0+} + +static unsigned long snp_handle_page_state_change (struct vcpu_svm *svm) +{ +struct kvm_vcpu *vcpu = &svm->vcpu; +int level, op, rc =PSC_UNDEF_ERR ; +struct snp_psc_desc *info; +struct psc_entry *entry; +u16 cur, end; +gpa_t gpa; + +if (!sev_snp_guest (vcpu->kvm)) +return PSC_INVALID_HDR ; + +if (!setup_vmgexit_scratch (svm, true,sizeof (*info))) { +pr_err ("vmgexit: scratch area is not setup.\n" ); +return PSC_INVALID_HDR ; + } + + info = (struct snp_psc_desc *)svm->ghcb_sa; + cur = info->hdr.cur_entry; + end = info->hdr.end_entry; + +if (cur >=VMGEXIT_PSC_MAX_ENTRY || + end >=VMGEXIT_PSC_MAX_ENTRY || cur > end) +return PSC_INVALID_ENTRY ; + +for (; cur <= end; cur++) { + entry = &info->entries[cur]; + gpa =gfn_to_gpa (entry->gfn); + level =RMP_TO_X86_PG_LEVEL (entry->pagesize); + op = entry->operation; + +if (!IS_ALIGNED (gpa,page_level_size (level))) { + rc =PSC_INVALID_ENTRY ; + goto out; + } + + rc =__snp_handle_page_state_change (vcpu, op, gpa, level); +if (rc) + goto out; + } + +out: + info->hdr.cur_entry = cur; +return rc ?map_to_psc_vmgexit_code (rc) :0 ; +} +static int sev_handle_vmgexit_msr_protocol (struct vcpu_svm *svm) {struct vmcb_control_area *control = &svm->vmcb->control;@@ -3315,6 +3378,15 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ret =1 ;break ; } +case SVM_VMGEXIT_PSC : { +unsigned long rc; + + ret =1 ; + + rc =snp_handle_page_state_change (svm); +svm_set_ghcb_sw_exit_info_2 (vcpu, rc); +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.17.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH Part2 v5 38/45] KVM: SVM: Add support to handle Page State Change VMGEXIT
Quoted 110 lines · Dr. David Alan Gilbert
Quoted 92 lines · * Brijesh Singh ([email protected]) wrote:
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.
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/sev-common.h | 7 +++ arch/x86/kvm/svm/sev.c | 82 +++++++++++++++++++++++++++++-- 2 files changed, 84 insertions(+), 5 deletions(-)arch/x86/include/asm/sev-common.h +7
-0@@ -126,6 +126,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 +10
-5@@ -2731,6 +2731,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm, u64 *exit_code)case SVM_VMGEXIT_AP_JUMP_TABLE :case SVM_VMGEXIT_UNSUPPORTED_EVENT :case SVM_VMGEXIT_HV_FEATURES : +case SVM_VMGEXIT_PSC :break ;default : goto vmgexit_err;@@ -3004,13 +3005,13 @@ static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, enum psc_op op,*/ rc =snp_check_and_build_npt (vcpu, gpa, level);if (rc) -return -EINVAL ; +return PSC_UNDEF_ERR ;if (op ==SNP_PAGE_STATE_PRIVATE ) {hva_t hva;if (snp_gpa_to_hva (kvm, gpa, &hva)) -return -EINVAL ; +return PSC_UNDEF_ERR ;/* * Verify that the hva range is registered. This enforcement is @@ -3022,7 +3023,7 @@ static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, enum psc_op op, rc =is_hva_registered (kvm, hva,page_level_size (level));mutex_unlock (&kvm->lock);if (!rc) -return -EINVAL ; +return PSC_UNDEF_ERR ;/* * Mark the userspace range unmerable before adding the pages @@ -3032,7 +3033,7 @@ static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, enum psc_op op, rc =snp_mark_unmergable (kvm, hva,page_level_size (level));mmap_write_unlock (kvm->mm);if (rc) -return -EINVAL ; +return PSC_UNDEF_ERR ; }write_lock (&kvm->mmu_lock);@@ -3062,8 +3063,11 @@ static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, enum psc_op op,case SNP_PAGE_STATE_PRIVATE : rc =rmp_make_private (pfn, gpa, level, sev->asid, false);break ; +case SNP_PAGE_STATE_PSMASH : +case SNP_PAGE_STATE_UNSMASH : +/* TODO: Add support to handle it */ default : - rc = -EINVAL ; + rc =PSC_INVALID_ENTRY ;break ; }@@ -3081,6 +3085,65 @@ static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, enum psc_op op,return 0 ; } +static inline unsigned long map_to_psc_vmgexit_code (int rc)arch/x86/kvm/svm/sev.c static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, enum psc_op op, · line 3098
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+{ +switch (rc) { +case PSC_INVALID_HDR : +return ((1ul <<32 ) |1 ); +case PSC_INVALID_ENTRY : +return ((1ul <<32 ) |2 ); +case RMPUPDATE_FAIL_OVERLAP : +return ((3ul <<32 ) |2 ); +default :return (4ul <<32 ); + }Are these the values defined in 56421 section 4.1.6 ?
If so, that says:
SW_EXITINFO2[63:32] ==0x00000100
The hypervisor encountered some other error situation and was not able to complete the request identified by page_state_change_header.cur_entry. It is left to the guest to decide how to proceed in this situation.
Quoted 1 line
so it looks like the default should be
0x100rather than 4?
Ah good catch, it should be 0x100. I will fix it.
Quoted 2 lines
(It's a shame they're all magical constants, it would be nice if the standard have them names)
In early RFC's I had a macros to build the error code but based on feedback's went with this function with open coded values.
thanks
Re: [PATCH Part2 v5 41/45] KVM: SVM: Add support to handle the RMP nested page fault
In reply to Brijesh SinghQuoted 74 lines · * Brijesh Singh ([email protected]) wrote:
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.
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/kvm/svm/sev.c | 76 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 14 +++++--- arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 87 insertions(+), 4 deletions(-)arch/x86/kvm/svm/sev.c +53
-0@@ -3651,3 +3651,79 @@ void sev_post_unmap_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int token)srcu_read_unlock (&sev->psc_srcu, token); } + +void handle_rmp_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u64 error_code) +{ +int rmp_level, npt_level, rc, assigned; +struct kvm *kvm = vcpu->kvm; +gfn_t gfn =gpa_to_gfn (gpa); +bool need_psc = false; +enum psc_op psc_op; +kvm_pfn_t pfn; +bool private; + +write_lock (&kvm->mmu_lock); + +if (unlikely (!kvm_mmu_get_tdp_walk (vcpu, gpa, &pfn, &npt_level))) + goto unlock; + + assigned =snp_lookup_rmpentry (pfn, &rmp_level); +if (unlikely (assigned <0 )) + goto unlock; + + private = !!(error_code &PFERR_GUEST_ENC_MASK ); + +/* +* If the fault was due to size mismatch, or NPT and RMP page level's +* are not in sync, then use PSMASH to split the RMP entry into 4K. +*/ +if ((error_code &PFERR_GUEST_SIZEM_MASK ) || + (npt_level ==PG_LEVEL_4K && rmp_level ==PG_LEVEL_2M && private)) { + rc =snp_rmptable_psmash (kvm, pfn); +if (rc) +pr_err_ratelimited ("psmash failed, gpa 0x%llx pfn 0x%llx rc %d\n" , + gpa, pfn, rc); + goto out; + } + +/* +* If it's a private access, and the page is not assigned in the +* RMP table, create a new private RMP entry. This can happen if +* guest did not use the PSC VMGEXIT to transition the page state +* before the access. +*/ +if (!assigned && private) { + need_psc =1 ; + psc_op =SNP_PAGE_STATE_PRIVATE ; + goto out; + } + +/* +* If it's a shared access, but the page is private in the RMP table +* then make the page shared in the RMP table. This can happen if +* the guest did not use the PSC VMGEXIT to transition the page +* state before the access. +*/
arch/x86/kvm/svm/sev.c void sev_post_unmap_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int token) · line 3716
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+if (assigned && !private) { + need_psc =1 ; + psc_op =SNP_PAGE_STATE_SHARED ; + } + +out: +write_unlock (&kvm->mmu_lock); + +if (need_psc) + rc =__snp_handle_page_state_change (vcpu, psc_op, gpa,PG_LEVEL_4K );
That 'rc' never goes anywhere - should it?
Quoted 24 lines
arch/x86/kvm/svm/sev.c +12
-0+/* +* The fault handler has updated the RMP pagesize, zap the existing +* rmaps for large entry ranges so that nested page table gets rebuilt +* with the updated RMP pagesize. +*/ + gfn =gpa_to_gfn (gpa) & ~(KVM_PAGES_PER_HPAGE (PG_LEVEL_2M ) -1 ); +kvm_zap_gfn_range (kvm, gfn, gfn +PTRS_PER_PMD ); +return ; + +unlock: +write_unlock (&kvm->mmu_lock); +}arch/x86/kvm/svm/svm.c +1
-0@@ -1933,15 +1933,21 @@ 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;
arch/x86/kvm/svm/svm.c static int pf_interception(struct kvm_vcpu *vcpu) · line 1945
Quoted 10 lines
arch/x86/kvm/svm/svm.c +4
-4trace_kvm_page_fault (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);
If kvm_mmu_page_fault failed, (rc!=0) do you still want to call your handler?
Dave
Quoted 23 lines
arch/x86/kvm/svm/svm.c +4
-0+if (error_code &PFERR_GUEST_RMP_MASK ) +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@@ -626,6 +626,7 @@ struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu);void sev_rmp_page_level_adjust (struct kvm *kvm,kvm_pfn_t pfn,int *level);int sev_post_map_gfn (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int *token);void sev_post_unmap_gfn (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int token); +void handle_rmp_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u64 error_code);/* vmenter.S */ Signature · 1 line
2.17.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Borislav Petkov
maintainerRe: [PATCH Part2 v5 04/45] x86/sev: Add RMP entry lookup helpers
In reply to Brijesh SinghQuoted 2 lines · Brijesh Singh
Currently, we have only x86 drivers using it, are you thinking to move this to
arch/x86/include/asm/?
arch/x86/include/asm/sev.h, yap.
We can always create it later if needed by other architectures.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerRe: [PATCH Part2 v5 06/45] x86/sev: Invalid pages from direct map when adding it to RMP table
In reply to Brijesh SinghQuoted 1 line · Brijesh Singh
Subject: Re: [PATCH Part2 v5 06/45] x86/sev: Invalid pages from direct map when adding it to RMP table
That subject needs to have a verb. I think that verb should be "Invalidate".
Quoted 6 lines
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 checks failure, it raises a page-fault exception.
Quoted 7 lines
The
rmp_make_private()andrmp_make_shared()helpers are used to add or remove the pages from the RMP table.Improve the
rmp_make_private()to
invalid state so that pages cannot be used in the direct-map after its added in the RMP table, and restore to its default valid permission after the pages are removed from the RMP table.
That sentence needs rewriting into proper english.
The more important thing is, though, this doesn't talk about *why* you're doing this: you want to remove pages from the direct map when they're in the RMP table because something might modify the page and then the RMP check will fail?
Also, set_direct_map_invalid_noflush() simply clears the Present and RW bit of a pte.
So what's up?
Quoted 10 lines
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/kernel/sev.c | 61 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-)arch/x86/kernel/sev.c +0
-0
arch/x86/kernel/sev.c int psmash(u64 pfn) · line 2444
Quoted 4 lines
arch/x86/kernel/sev.c +1
-0@@ -2441,10 +2441,42 @@ int psmash(u64 pfn) }EXPORT_SYMBOL_GPL (psmash); +static int restore_direct_map (u64 pfn,int npages)
restore_pages_in_direct_map()
arch/x86/kernel/sev.c int psmash(u64 pfn) · line 2452
Quoted 8 lines
arch/x86/kernel/sev.c +8
-0+{ +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; + }
So this is looping over a set of virtually contiguous pages, I presume, and if so, you should add a function called
set_memory_p_rw()
to arch/x86/mm/pat/set_memory.c which does
return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_PRESENT | _PAGE_RW), 0);
so that you can do all pages in one go.
arch/x86/kernel/sev.c int psmash(u64 pfn) · line 2459
Quoted 7 lines
arch/x86/kernel/sev.c +7
-0+ +cleanup: +WARN (ret >0 ,"Failed to restore direct map for pfn 0x%llx\n" , pfn + i); +return ret; +} + +static int invalid_direct_map (unsigned long pfn,int npages)
invalidate_pages_in_direct_map()
or so.
arch/x86/kernel/sev.c int psmash(u64 pfn) · line 2464
Quoted 5 lines
arch/x86/kernel/sev.c +5
-0+{ +int i, ret =0 ; + +for (i =0 ; i < npages; i++) { + ret =set_direct_map_invalid_noflush (pfn_to_page (pfn + i));
Same as above but that helper should do the reverse:
set_memory_np_ro()
{
return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT | _PAGE_RW), 0);
}Btw, please add those helpers in a separate patch.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerRe: [PATCH Part2 v5 07/45] x86/traps: Define RMP violation #PF error code
In reply to Brijesh SinghQuoted 6 lines · Brijesh Singh
enum
x86_pf_error_code{arch/x86/include/asm/trap_pf.h +0
-5-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,
arch/x86/include/asm/trap_pf.h line 27
Quoted 10 lines
arch/x86/include/asm/trap_pf.h +8
-2-X86_PF_PK =1 <<5 , - X86_PF_SGX = 1 << 15, +X86_PF_PROT =BIT_ULL (0 ), +X86_PF_WRITE =BIT_ULL (1 ), +X86_PF_USER =BIT_ULL (2 ), +X86_PF_RSVD =BIT_ULL (3 ), +X86_PF_INSTR =BIT_ULL (4 ), +X86_PF_PK =BIT_ULL (5 ), +X86_PF_SGX =BIT_ULL (15 ), + X86_PF_RMP = BIT_ULL(31),
Those are tested against error_code mostly, which is unsigned long so it looks like you wanna use _BITUL() here. Not that it matters on x86-64 but if we want to be precise...
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerRe: [PATCH Part2 v5 08/45] x86/fault: Add support to handle the RMP fault for user address
In reply to Brijesh Singharch/x86/mm/fault.c do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, · line 1217
Quoted 3 lines · Brijesh Singh
arch/x86/mm/fault.c +3
-0+static int handle_user_rmp_page_fault (struct pt_regs *regs,unsigned long error_code, +unsigned long address) +{
#ifdef CONFIG_AMD_MEM_ENCRYPT
Quoted 10 lines
arch/x86/mm/fault.c +10
-0+int rmp_level, level; +pte_t *pte; +u64 pfn; + + pte =lookup_address_in_mm (current->mm, address, &level); + +/* +* It can happen if there was a race between an unmap event and +* the RMP fault delivery. +*/
arch/x86/mm/fault.c do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, · line 1237
Quoted 10 lines
arch/x86/mm/fault.c +10
-0+if (!pte || !pte_present (*pte)) +return 1 ; + + pfn =pte_pfn (*pte); + +/* If its large page then calculte the fault pfn */ +if (level >PG_LEVEL_4K ) { +unsigned long mask; + + mask =pages_per_hpage (level) -pages_per_hpage (level -1 );
Just use two helper variables named properly instead of this oneliner:
pages_level = page_level_size(level) / PAGE_SIZE; pages_prev_level = page_level_size(level - 1) / PAGE_SIZE;
Quoted 10 lines
arch/x86/mm/fault.c +10
-0+ pfn |= (address >>PAGE_SHIFT ) & mask; + } + +/* +* If its a guest private page, then the fault cannot be resolved. +* Send a SIGBUS to terminate the process. +*/ +if (snp_lookup_rmpentry (pfn, &rmp_level)) { +do_sigbus (regs, error_code, address,VM_FAULT_SIGBUS ); +return 1 ;
arch/x86/mm/fault.c do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, · line 1257
Quoted 10 lines
arch/x86/mm/fault.c +10
-0+ } + +/* +* The backing page level is higher than the RMP page level, request +* to split the page. +*/ +if (level > rmp_level) +return 0 ; + +return 1 ;
#else WARN_ONONCE(1); return -1;
#endif
and also handle that -1 negative value at the call site.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerRe: [PATCH Part2 v5 09/45] x86/fault: Add support to dump RMP entry on fault
In reply to Brijesh SinghQuoted 18 lines · Brijesh Singh
When SEV-SNP is enabled globally, a write from the host goes through the RMP check. If the hardware encounters the check failure, then it raises the #PF (with RMP set). Dump the RMP entry at the faulting pfn to help the debug.
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/sev.h | 7 +++++++ arch/x86/kernel/sev.c | 43 ++++++++++++++++++++++++++++++++++++++ arch/x86/mm/fault.c | 17 +++++++++++---- include/linux/sev.h | 2 ++ 4 files changed, 65 insertions(+), 4 deletions(-)arch/x86/include/asm/sev.h +0
-0
arch/x86/include/asm/sev.h struct __packed rmpentry { · line 113
Quoted 8 lines
arch/x86/include/asm/sev.h +5
-0@@ -106,6 +106,11 @@ struct __packed rmpentry {#define rmpentry_assigned (x) ((x)->info.assigned)#define rmpentry_pagesize (x) ((x)->info.pagesize) +#define rmpentry_vmsa (x) ((x)->info.vmsa) +#define rmpentry_asid (x) ((x)->info.asid) +#define rmpentry_validated (x) ((x)->info.validated) +#define rmpentry_gpa (x) ((unsigned long)(x)->info.gpa) +#define rmpentry_immutable (x) ((x)->info.immutable)
If some of those accessors are going to be used only in dump_rmpentry(), then you don't really need them.
Quoted 24 lines
arch/x86/include/asm/sev.h +2
-0#define RMPADJUST_VMSA_PAGE_BIT BIT(16)@@ -165,6 +170,7 @@ void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op opvoid snp_set_memory_shared (unsigned long vaddr,unsigned int npages);void snp_set_memory_private (unsigned long vaddr,unsigned int npages);void snp_set_wakeup_secondary_cpu (void ); +void dump_rmpentry (u64 pfn);#ifdef __BOOT_COMPRESSEDbool sev_snp_enabled (void );#else @@ -188,6 +194,7 @@ static inline void snp_set_memory_shared(unsigned long vaddr, unsigned int npagestatic inline void snp_set_memory_private (unsigned long vaddr,unsigned int npages) { }static inline void snp_set_wakeup_secondary_cpu (void ) { }static inline void sev_snp_cpuid_init (struct boot_params *bp) { } +static inline void dump_rmpentry (u64 pfn) {}#ifdef __BOOT_COMPRESSEDstatic inline bool sev_snp_enabled {return false; }#else arch/x86/kernel/sev.c +0
-0
arch/x86/kernel/sev.c static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) · line 2407
Quoted 4 lines
arch/x86/kernel/sev.c +1
-0@@ -2404,6 +2404,49 @@ static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level)return entry; } +void dump_rmpentry (u64 pfn)
snp_dump_rmpentry()
Quoted 4 lines
arch/x86/kernel/sev.c +4
-0+{ +unsigned long pfn_end; +struct rmpentry *e; +int level;
arch/x86/kernel/sev.c static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) · line 2421
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+ + e =__snp_lookup_rmpentry (pfn, &level); +if (!e) { +pr_alert ("failed to read RMP entry pfn 0x%llx\n" , pfn); +return ; + } + +if (rmpentry_assigned (e)) { + pr_alert("RMPEntry paddr 0x%llx [assigned=%d immutable=%d pagesize=%d gpa=0x%lx" + " asid=%d vmsa=%d validated=%d]\n", pfn << PAGE_SHIFT,
WARNING: quoted string split across lines
#174: FILE: arch/x86/kernel/sev.c:2421:
+ pr_alert("RMPEntry paddr 0x%llx [assigned=%d immutable=%d pagesize=%d gpa=0x%lx"
+ " asid=%d vmsa=%d validated=%d]\n", pfn << PAGE_SHIFT,
Quoted 9 lines
arch/x86/kernel/sev.c +9
-0+rmpentry_assigned (e),rmpentry_immutable (e),rmpentry_pagesize (e), +rmpentry_gpa (e),rmpentry_asid (e),rmpentry_vmsa (e), + rmpentry_validated(e)); +return ; + } + +/* +* If the RMP entry at the faulting pfn was not assigned, then we do not +* know what caused the RMP violation. To get some useful debug information,
arch/x86/kernel/sev.c static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) · line 2440
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+* let iterate through the entire 2MB region, and dump the RMP entries if +* one of the bit in the RMP entry is set. +*/ + pfn = pfn & ~(PTRS_PER_PMD -1 ); + pfn_end = pfn +PTRS_PER_PMD ; + +while (pfn < pfn_end) { + e =__snp_lookup_rmpentry (pfn, &level); +if (!e) +return ;
return? You mean "continue;" ?
arch/x86/kernel/sev.c static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) · line 2448
Quoted 8 lines
arch/x86/kernel/sev.c +8
-0+ +if (e->low || e->high) +pr_alert ("RMPEntry paddr 0x%llx: [high=0x%016llx low=0x%016llx]\n" , + pfn <<PAGE_SHIFT , e->high, e->low); + pfn++; + } +} +EXPORT_SYMBOL_GPL (dump_rmpentry);
Why is that exported? Some module will be calling it too?
Quoted 17 lines
arch/x86/kernel/sev.c +1
-0+/* * Return 1 if the RMP entry is assigned, 0 if it exists but is not assigned, * and -errno if there is no corresponding RMP entry. arch/x86/mm/fault.c +1
-0@@ -33,6 +33,7 @@#include <asm/pgtable_areas.h> /* VMALLOC_START, ... */ #include <asm/kvm_para.h> /* kvm_handle_async_pf */ #include <asm/vdso.h> /* fixup_vdso_exception() */ +#include <asm/sev.h> /* dump_rmpentry() */ #define CREATE_TRACE_POINTS #include <asm/trace/exceptions.h>
arch/x86/mm/fault.c static bool low_pfn(unsigned long pfn) · line 293
Quoted 5 lines
arch/x86/mm/fault.c +1
-1@@ -289,7 +290,7 @@ static bool low_pfn(unsigned long pfn)return pfn < max_low_pfn; } -static void dump_pagetable (unsigned long address) +static void dump_pagetable (unsigned long address,bool show_rmpentry)
I think passing in error_code and testing X86_PF_RMP inside should make this a bit more palatable than simply "grafting" SNP-specific functionality to generic paths.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH Part2 v5 42/45] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event
In reply to Brijesh SinghQuoted 266 lines · Brijesh Singh
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]>--- arch/x86/kvm/svm/sev.c | 197 +++++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/svm/svm.h | 2 + 2 files changed, 193 insertions(+), 6 deletions(-)arch/x86/kvm/svm/sev.c +181
-6@@ -19,6 +19,7 @@#include <linux/trace_events.h> #include <linux/sev.h> #include <linux/ksm.h> +#include <linux/sev-guest.h> #include <asm/fpu/internal.h> #include <asm/pkru.h> @@ -338,6 +339,7 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)init_srcu_struct (&sev->psc_srcu); ret =sev_snp_init (&argp->error); +mutex_init (&sev->guest_req_lock); }else { ret =sev_platform_init (&argp->error); }@@ -1602,23 +1604,39 @@ static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)static void *snp_context_create (struct kvm *kvm,struct kvm_sev_cmd *argp) { +void *context =NULL , *certs_data =NULL , *resp_page =NULL ; +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;struct sev_data_snp_gctx_create data = {}; -void *context;int rc; +/* Allocate memory used for the certs data in SNP guest request */ + certs_data =kmalloc (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; + +/* Allocate a firmware buffer used during the guest command handling. */ + resp_page =snp_alloc_firmware_page (GFP_KERNEL_ACCOUNT ); +if (!resp_page) + 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)@@ -2248,6 +2266,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 ; }@@ -2746,6 +2766,8 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm, u64 *exit_code)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 : goto vmgexit_err;@@ -3161,6 +3183,155 @@ static unsigned long snp_handle_page_state_change(struct vcpu_svm *svm)return rc ?map_to_psc_vmgexit_code (rc) :0 ; } +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: +svm_set_ghcb_sw_exit_info_2 (vcpu, 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; + } + +/* Verify that requested blob will fit in certificate buffer */ +if ((data_npages <<PAGE_SHIFT ) >SEV_FW_BLOB_MAX_SIZE ) { + rc =SEV_RET_INVALID_PARAM ; + 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: +svm_set_ghcb_sw_exit_info_2 (vcpu, rc); +} +static int sev_handle_vmgexit_msr_protocol (struct vcpu_svm *svm) {struct vmcb_control_area *control = &svm->vmcb->control;@@ -3404,6 +3575,20 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)svm_set_ghcb_sw_exit_info_2 (vcpu, rc);break ; } +case SVM_VMGEXIT_GUEST_REQUEST : { +snp_handle_guest_request (svm, control->exit_info_1, control->exit_info_2); + + ret =1 ;
arch/x86/kvm/svm/sev.c int sev_handle_vmgexit(struct kvm_vcpu *vcpu) · line 3591
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+break ; + } +case SVM_VMGEXIT_EXT_GUEST_REQUEST : { +snp_handle_ext_guest_request (svm, + control->exit_info_1, + control->exit_info_2); + + ret =1 ; +break ; + }
Don't we need some sort of rate limiting here? Since the PSP is a shared resource that the host needs access to as well as other guests it seems a little dangerous to let the guest spam this interface.
Quoted 19 lines
arch/x86/kvm/svm/sev.c +0
-0case 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@@ -92,6 +92,8 @@ struct kvm_sev_info {u64 snp_init_flags;void *snp_context;/* SNP guest context page */ struct srcu_struct psc_srcu; +void *snp_certs_data; +struct mutex guest_req_lock; };struct kvm_svm { --2.17.1
Re: [PATCH Part2 v5 42/45] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event
In reply to Brijesh SinghQuoted 217 lines · Brijesh Singh
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]>--- arch/x86/kvm/svm/sev.c | 197 +++++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/svm/svm.h | 2 + 2 files changed, 193 insertions(+), 6 deletions(-)arch/x86/kvm/svm/sev.c +139
-6@@ -19,6 +19,7 @@#include <linux/trace_events.h> #include <linux/sev.h> #include <linux/ksm.h> +#include <linux/sev-guest.h> #include <asm/fpu/internal.h> #include <asm/pkru.h> @@ -338,6 +339,7 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)init_srcu_struct (&sev->psc_srcu); ret =sev_snp_init (&argp->error); +mutex_init (&sev->guest_req_lock); }else { ret =sev_platform_init (&argp->error); }@@ -1602,23 +1604,39 @@ static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)static void *snp_context_create (struct kvm *kvm,struct kvm_sev_cmd *argp) { +void *context =NULL , *certs_data =NULL , *resp_page =NULL ; +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;struct sev_data_snp_gctx_create data = {}; -void *context;int rc; +/* Allocate memory used for the certs data in SNP guest request */ + certs_data =kmalloc (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; + +/* Allocate a firmware buffer used during the guest command handling. */ + resp_page =snp_alloc_firmware_page (GFP_KERNEL_ACCOUNT ); +if (!resp_page) + 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)@@ -2248,6 +2266,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 ; }@@ -2746,6 +2766,8 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm, u64 *exit_code)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 : goto vmgexit_err;@@ -3161,6 +3183,155 @@ static unsigned long snp_handle_page_state_change(struct vcpu_svm *svm)return rc ?map_to_psc_vmgexit_code (rc) :0 ; } +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: +svm_set_ghcb_sw_exit_info_2 (vcpu, 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; + } + +/* Verify that requested blob will fit in certificate buffer */ +if ((data_npages <<PAGE_SHIFT ) >SEV_FW_BLOB_MAX_SIZE ) { + rc =SEV_RET_INVALID_PARAM ; + goto e_fail;
arch/x86/kvm/svm/sev.c static unsigned long snp_handle_page_state_change(struct vcpu_svm *svm) · line 3306
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ } + +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);
What's the point of caching the certs from the sev_device in the sev_info struct if the snp_guest_ext_guest_request() function always seems to re-copy the certs back into |sev->snp_certs_data| on every call? I am probably missing something.
Quoted 68 lines
arch/x86/kvm/svm/sev.c +42
-0+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: +svm_set_ghcb_sw_exit_info_2 (vcpu, rc); +} +static int sev_handle_vmgexit_msr_protocol (struct vcpu_svm *svm) {struct vmcb_control_area *control = &svm->vmcb->control;@@ -3404,6 +3575,20 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)svm_set_ghcb_sw_exit_info_2 (vcpu, rc);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
-1@@ -92,6 +92,8 @@ struct kvm_sev_info {u64 snp_init_flags;void *snp_context;/* SNP guest context page */ struct srcu_struct psc_srcu; +void *snp_certs_data; +struct mutex guest_req_lock; };struct kvm_svm { --2.17.1
Re: [PATCH Part2 v5 06/45] x86/sev: Invalid pages from direct map when adding it to RMP table
In reply to Borislav PetkovHi Boris,
On 9/29/21 9:34 AM, Borislav Petkov wrote:
...
Quoted 10 lines
Quoted 4 lines
Improve the
rmp_make_private()to
invalid state so that pages cannot be used in the direct-map after its added in the RMP table, and restore to its default valid permission after the pages are removed from the RMP table.That sentence needs rewriting into proper english.
The more important thing is, though, this doesn't talk about *why* you're doing this: you want to remove pages from the direct map when they're in the RMP table because something might modify the page and then the RMP check will fail?
I'll work to improve the commit description.
Quoted 4 lines
Also,
set_direct_map_invalid_noflush()simply clears the Present and RW bit of a pte.So what's up?
The set_direct_map_invalid_noflush() does two steps
1) Split the host page table (if needed)
2) Clear the present and RW bit from pte
In previous patches I was using the set_memory_4k() to split the direct map before adding the pages in the RMP table. Based on Sean's review comment [1], I switched to using set_direct_map_invalid_noflush() so that page is not just split but also removed from the direct map. The thought process is if in the future set_direct_map_default_noflush() is improved to restore the large mapping then it will all work transparently.
[1] https://lore.kernel.org/lkml/[email protected]/#t
Quoted 27 lines
Quoted 10 lines
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/kernel/sev.c | 61 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-)arch/x86/kernel/sev.c +0
-0arch/x86/kernel/sev.c int psmash(u64 pfn) · line 2444
Quoted 4 lines
arch/x86/kernel/sev.c +1
-0@@ -2441,10 +2441,42 @@ int psmash(u64 pfn) }EXPORT_SYMBOL_GPL (psmash); +static int restore_direct_map (u64 pfn,int npages)
restore_pages_in_direct_map()arch/x86/kernel/sev.c int psmash(u64 pfn) · line 2452
Quoted 8 lines
arch/x86/kernel/sev.c +8
-0+{ +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; + }So this is looping over a set of virtually contiguous pages, I presume, and if so, you should add a function called
Quoted 7 lines
set_memory_p_rw()to
arch/x86/mm/pat/set_memory.cwhich doesreturn change_page_attr_set(&addr, numpages, __pgprot(_PAGE_PRESENT | _PAGE_RW), 0);so that you can do all pages in one go.
I will look into it.
thanks
Re: [PATCH Part2 v5 32/45] KVM: x86: Define RMP page fault error bits for #NPF
In reply to Brijesh SinghQuoted 7 lines · Brijesh Singh
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.
nit: Section # should be 15.36.10 (rather than 16.36.10). Also, is it better to put section headings, rather than numbers in the commit logs and comments? Someone mentioned to me that the section numbering in APM and SDM can move around over time, but the section titles tend to be more stable. I'm not sure how true this is, so feel free to disregard this comment.
Quoted 37 lines
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/kvm_host.h | 8 ++++++++ 1 file changed, 8 insertions(+)arch/x86/include/asm/kvm_host.h +8
-1@@ -239,8 +239,12 @@ 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_GUEST_ENC_BIT 34 +#define PFERR_GUEST_SIZEM_BIT 35 +#define PFERR_GUEST_VMPL_BIT 36#define PFERR_PRESENT_MASK (1U << PFERR_PRESENT_BIT)#define PFERR_WRITE_MASK (1U << PFERR_WRITE_BIT)@@ -251,6 +255,10 @@ enum x86_intercept_stage;#define PFERR_SGX_MASK (1U << PFERR_SGX_BIT)#define PFERR_GUEST_FINAL_MASK (1ULL << PFERR_GUEST_FINAL_BIT)#define PFERR_GUEST_PAGE_MASK (1ULL << PFERR_GUEST_PAGE_BIT) +#define PFERR_GUEST_RMP_MASK (1ULL << PFERR_GUEST_RMP_BIT) +#define PFERR_GUEST_ENC_MASK (1ULL << PFERR_GUEST_ENC_BIT) +#define PFERR_GUEST_SIZEM_MASK (1ULL << PFERR_GUEST_SIZEM_BIT) +#define PFERR_GUEST_VMPL_MASK (1ULL << PFERR_GUEST_VMPL_BIT)#define PFERR_NESTED_GUEST_PAGE (PFERR_GUEST_PAGE_MASK | \ PFERR_WRITE_MASK | \ --2.17.1
Borislav Petkov
maintainerRe: [PATCH Part2 v5 06/45] x86/sev: Invalid pages from direct map when adding it to RMP table
In reply to Brijesh SinghQuoted 3 lines · Brijesh Singh
. The thought process is if in the future
set_direct_map_default_noflush()is improved to restore the large mapping then it will all work transparently.
That's only scratching the surface of the *why* this is done so please explain why this dance is being done in a comment above the code so that it is clear.
It is not really obvious why that hiding from the direct map is being done.
Good reason from that memfd_secret mail are:
"* Prevent cross-process secret userspace memory exposures. Once the secret memory is allocated, the user can't accidentally pass it into the kernel to be transmitted somewhere. The secreremem pages cannot be accessed via the direct map and they are disallowed in GUP."
and in general hiding RMP pages from the direct map is a nice additional protection.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerRe: [PATCH Part2 v5 32/45] KVM: x86: Define RMP page fault error bits for #NPF
In reply to Marc OrrQuoted 7 lines · Marc Orr
Quoted 6 lines · Brijesh Singh
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).
Quoted 8 lines
Quoted 1 line
See APM2 section 16.36.10 for more details.
nit: Section # should be 15.36.10 (rather than 16.36.10). Also, is it better to put section headings, rather than numbers in the commit logs and comments? Someone mentioned to me that the section numbering in APM and SDM can move around over time, but the section titles tend to be more stable. I'm not sure how true this is, so feel free to disregard this comment.
No, that comment is correct, please make it unambiguous so that if someone's looking later, someone can find the section even in future docs. (I'm hoping they don't change headings, that is...).
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH Part2 v5 25/45] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_UPDATE command
In reply to Brijesh SinghQuoted 31 lines · Brijesh Singh
On 9/27/21 11:43 AM, Peter Gonda wrote:
...Quoted 26 lines
Quoted 9 lines
arch/x86/kvm/svm/sev.c +8
-0+static bool is_hva_registered (struct kvm *kvm,hva_t hva,size_t len) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct list_head *head = &sev->regions_list; +struct enc_region *i; + +lockdep_assert_held (&kvm->lock); +arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1684
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+list_for_each_entry (i, head, list) { +u64 start = i->uaddr; +u64 end = start + i->size; + +if (start <= hva && end >= (hva + len)) +return true; + } + +return false; +}Internally we actually register the guest memory in chunks for various reasons. So for our largest SEV VM we have 768 1 GB entries in |sev->regions_list|. This was OK before because no look ups were done. Now that we are performing a look ups a linked list with linear time lookups seems not ideal, could we switch the back data structure here to something more conducive too fast lookups?
Quoted 5 lines
Quoted 1 line
Quoted 1 line
+
Interesting, for qemu we had very few number of regions so there was no strong reason for me to think something otherwise. Do you have any preference on what data structure you will use ?
Chatted offline. I think this is fine for now, we won't want to use our userspace demand pinning with SNP yet.
Quoted 108 lines
Quoted 11 lines
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1691
Quoted 7 lines
arch/x86/kvm/svm/sev.c +6
-0+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 sev_data_snp_launch_update data = {0 }; +struct kvm_sev_snp_launch_update params; +unsigned long npages, pfn, n =0 ;Could we have a slightly more descriptive name for |n|? nprivate maybe? Also why not zero in the loop below?
Sure, I will pick a better name and no need to zero above. I will fix it.
Quoted 9 lines
for (i = 0, n = 0; i < npages; ++i)
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1694
Quoted 3 lines
arch/x86/kvm/svm/sev.c +3
-0+int *error = &argp->error; +struct page **inpages; +int ret, i, level;Should |i| be an unsigned long since it can is tracked in a for loop with "i < npages" npages being an unsigned long? (|n| too)
Noted.
Quoted 22 lines
Quoted 22 lines
arch/x86/kvm/svm/sev.c +22
-0+u64 gfn; + +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 ; + +/* Verify that the specified address range is registered. */ +if (!is_hva_registered (kvm, params.uaddr, params.len)) +return -EINVAL ; + +/* +* The userspace memory is already locked so technically we don't +* need to lock it again. Later part of the function needs to know +* pfn so call the sev_pin_memory() so that we can get the list of +* pages to iterate through. +*/ + inpages =sev_pin_memory (kvm, params.uaddr, params.len, &npages,1 );Quoted 9 lines
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1722
Quoted 6 lines
arch/x86/kvm/svm/sev.c +6
-0+if (!inpages) +return -ENOMEM ; + +/* +* Verify that all the pages are marked shared in the RMP table before +* going further. This is avoid the cases where the userspace may try This is *too* avoid cases...
Noted
Quoted 39 lines
Quoted 27 lines
arch/x86/kvm/svm/sev.c +27
-0+* updating the same page twice. +*/ +for (i =0 ; i < npages; i++) { +if (snp_lookup_rmpentry (page_to_pfn (inpages[i]), &level) !=0 ) { +sev_unpin_memory (kvm, inpages, npages); +return -EFAULT ; + } + } + + gfn = params.start_gfn; + level =PG_LEVEL_4K ; + data.gctx_paddr =__psp_pa (sev->snp_context); + +for (i =0 ; i < npages; i++) { + pfn =page_to_pfn (inpages[i]); + + ret =rmp_make_private (pfn, gfn <<PAGE_SHIFT , level,sev_get_asid (kvm), true); +if (ret) { + ret = -EFAULT ; + goto e_unpin; + } + + n++; + data.address =__sme_page_pa (inpages[i]); + data.page_size =X86_TO_RMP_PG_LEVEL (level); + data.page_type = params.page_type; + data.vmpl3_perms = params.vmpl3_perms;arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1759
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ 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) { +/* +* If the command failed then need to reclaim the page. +*/ +snp_page_reclaim (pfn); + goto e_unpin; + }Hmm if this call fails after the first iteration of this loop it will
Quoted 9 lines
lead to a hard to reproduce LaunchDigest right? Say if we are SnpLaunchUpdating just 2 pages A and B. If we first call this ioctl and A is SNP_LAUNCH_UPDATED'd but B fails, we then make A shared again in the RMP. So we must call the ioctl with 2 pages again, after fixing the issue with page B. Now the Launch digest has something like Hash(A) then
HASH(A & B)right (overly simplified) so A will be included twice right? I am not sure if anything better can be done here but might be worth documenting IIUC.
Quoted 6 lines
I can add a comment in documentation that if a
LAUNCH_UPDATEfails then user need to destroy the existing context and start from the beginning. I am not sure if we want to support the partial update cases. But in case we have two choices a) decommission the context on failure or b) add a new command to destroy the existing context.
Agreed supporting the partial update case seems very tricky.
Quoted 109 lines
Quoted 109 lines
Quoted 109 lines
arch/x86/kvm/svm/sev.c +50
-0+ + gfn++; + } + +e_unpin: +/* Content of memory is updated, mark pages dirty */ +for (i =0 ; i < n; i++) { +set_page_dirty_lock (inpages[i]); +mark_page_accessed (inpages[i]); + +/* +* If its an error, then update RMP entry to change page ownership +* to the hypervisor. +*/ +if (ret) +host_rmp_make_shared (pfn, level, true); + } + +/* Unlock the user pages */ +sev_unpin_memory (kvm, inpages, npages); + +return ret; +} +int svm_mem_enc_op (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;@@ -1712,6 +1873,9 @@ int svm_mem_enc_op(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;@@ -1794,6 +1958,29 @@ find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)static void __unregister_enc_region_locked (struct kvm *kvm,struct enc_region *region) { +unsigned long i, pfn; +int level; + +/* +* The guest memory pages are assigned in the RMP table. Unassign it +* before releasing the memory. +*/ +if (sev_snp_guest (kvm)) { +for (i =0 ; i < region->npages; i++) { + pfn =page_to_pfn (region->pages[i]); + +if (!snp_lookup_rmpentry (pfn, &level)) +continue ; + +cond_resched (); + +if (level >PG_LEVEL_4K ) + pfn &= ~(KVM_PAGES_PER_HPAGE (PG_LEVEL_2M ) -1 ); + +host_rmp_make_shared (pfn, level, true); + } + } +sev_unpin_memory (kvm, region->pages, region->npages);list_del (®ion->list);kfree (region);include/uapi/linux/kvm.h +19
-1@@ -1715,6 +1715,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, };@@ -1831,6 +1832,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) --2.17.1
Sean Christopherson
maintainerRe: [PATCH Part2 v5 26/45] KVM: SVM: Mark the private vma unmerable for SEV-SNP guests
In reply to Brijesh SinghQuoted 8 lines · Brijesh Singh
When SEV-SNP is enabled, the guest private pages are added in the RMP table; while adding the pages, the
rmp_make_private()unmaps the pages from the direct map. If KSM attempts to access those unmapped pages then it will trigger #PF (page-not-present).Encrypted guest pages cannot be shared between the process, so an userspace should not mark the region mergeable but to be safe, mark the process vma unmerable before adding the pages in the RMP table.
To be safe from what? Does the !PRESENT #PF crash the kernel?
Quoted 55 lines
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/kvm/svm/sev.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)arch/x86/kvm/svm/sev.c +26
-0@@ -18,11 +18,13 @@#include <linux/processor.h> #include <linux/trace_events.h> #include <linux/sev.h> +#include <linux/ksm.h> #include <asm/fpu/internal.h> #include <asm/pkru.h> #include <asm/trapnr.h> #include <asm/sev.h> +#include <asm/mman.h> #include "x86.h" #include "svm.h" @@ -1683,6 +1685,30 @@ static bool is_hva_registered(struct kvm *kvm, hva_t hva, size_t len)return false; } +static int snp_mark_unmergable (struct kvm *kvm,u64 start,u64 size) +{ +struct vm_area_struct *vma; +u64 end = start + size; +int ret; + +do { + vma =find_vma_intersection (kvm->mm, start, end); +if (!vma) { + ret = -EINVAL ; +break ; + } + + ret =ksm_madvise (vma, vma->vm_start, vma->vm_end, +MADV_UNMERGEABLE , &vma->vm_flags); +if (ret) +break ; + + start = vma->vm_end; + }while (end > vma->vm_end); + +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;
arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1738
Quoted 6 lines
arch/x86/kvm/svm/sev.c +3
-0@@ -1707,6 +1733,12 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)if (!is_hva_registered (kvm, params.uaddr, params.len))return -EINVAL ; +mmap_write_lock (kvm->mm); + ret =snp_mark_unmergable (kvm, params.uaddr, params.len); +mmap_write_unlock (kvm->mm);
This does not, and practically speaking cannot, work. There are multiple TOCTOU bugs, here and in __snp_handle_page_state_change(). Userspace can madvise() the range at any later point, munmap()/mmap() the entire range, mess with the memslots in the PSC case, and so on and so forth. Relying on MADV_UNMERGEABLE for functional correctness simply cannot work in KVM, barring mmu_notifier and a big pile of code.
Quoted 9 lines
arch/x86/kvm/svm/sev.c +3
-0+if (ret) +return -EFAULT ; +/* * The userspace memory is already locked so technically we don't * need to lock it again. Later part of the function needs to know Signature · 1 line
2.17.1
Sean Christopherson
maintainerRe: [PATCH Part2 v5 20/45] KVM: SVM: Provide the Hypervisor Feature support VMGEXIT
In reply to Brijesh SinghQuoted 39 lines · Brijesh Singh
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.
Signed-off-by Brijesh Singh <[email protected]>--- 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@@ -97,6 +97,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 +1
-0@@ -2184,6 +2184,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 : goto vmgexit_err;
arch/x86/kvm/svm/sev.c static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) · line 2442
Quoted 4 lines
arch/x86/kvm/svm/sev.c +1
-0@@ -2438,6 +2439,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 : {
Unnecessary braces.
Quoted 10 lines
arch/x86/kvm/svm/sev.c +6
-0+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;
arch/x86/kvm/svm/sev.c int sev_handle_vmgexit(struct kvm_vcpu *vcpu) · line 2564
Quoted 4 lines
arch/x86/kvm/svm/sev.c +1
-0@@ -2553,6 +2561,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ret =1 ;break ; } +case SVM_VMGEXIT_HV_FEATURES : {
Same here.
Quoted 8 lines
arch/x86/kvm/svm/sev.c +5
-0+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",
Sean Christopherson
maintainerRe: [PATCH Part2 v5 21/45] KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
In reply to Brijesh SinghQuoted 3 lines · Brijesh Singh
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.
...
Quoted 18 lines
arch/x86/kvm/svm/svm.c +10
-0@@ -4539,6 +4539,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 = { .hardware_unsetup = svm_hardware_teardown, .hardware_enable = svm_hardware_enable,
arch/x86/kvm/svm/svm.c static struct kvm_x86_ops svm_x86_ops __initdata = { · line 4681
Quoted 5 lines
arch/x86/kvm/svm/svm.c +2
-0@@ -4667,6 +4677,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .complete_emulated_msr = svm_complete_emulated_msr, .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, + + .alloc_apic_backing_page = svm_alloc_apic_backing_page,
IMO, this should be guarded by a module param or X86_BUG_* to make it clear that this is a bug and not working as intended.
And doesn't the APIC page need these shenanigans iff AVIC is enabled? (the module param, not necessarily in the VM)
Quoted 18 lines
arch/x86/kvm/svm/svm.c +0
-0};static struct kvm_x86_init_ops svm_init_ops__initdata = {arch/x86/kvm/svm/svm.h +1
-0@@ -575,6 +575,7 @@ void sev_es_create_vcpu(struct vcpu_svm *svm);void sev_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector);void sev_es_prepare_guest_switch (struct vcpu_svm *svm,unsigned int cpu);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.17.1
Sean Christopherson
maintainerRe: [PATCH Part2 v5 37/45] KVM: SVM: Add support to handle MSR based Page State Change VMGEXIT
In reply to Brijesh SinghQuoted 51 lines · Brijesh Singh
arch/x86/kvm/svm/sev.c +36
-0@@ -31,6 +31,7 @@#include "svm_ops.h" #include "cpuid.h" #include "trace.h" +#include "mmu.h" #define __ex (x) __kvm_handle_fault_on_reboot(x)@@ -2905,6 +2906,181 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) svm->vmcb->control.ghcb_gpa = value; } +static int snp_rmptable_psmash (struct kvm *kvm,kvm_pfn_t pfn) +{ + pfn = pfn & ~(KVM_PAGES_PER_HPAGE (PG_LEVEL_2M ) -1 ); + +return psmash (pfn); +} + +static int snp_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); +} + +static int snp_check_and_build_npt (struct kvm_vcpu *vcpu,gpa_t gpa,int level) +{ +struct kvm *kvm = vcpu->kvm;
arch/x86/kvm/svm/sev.c static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) · line 2953
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+int rc, npt_level; +kvm_pfn_t pfn; + +/* +* Get the pfn and level for the gpa from the nested page table. +* +* If the tdp walk fails, then its safe to say that there is no +* valid mapping for this gpa. Create a fault to build the map. +*/ +write_lock (&kvm->mmu_lock);
SEV (or any vendor code for that matter) should not be taking mmu_lock. All of KVM has somewhat fungible borders between the various components, but IMO this crosses firmly into "this belongs in the MMU" territory.
For example, I highly doubt this actually need to take mmu_lock for write. More below.
arch/x86/kvm/svm/sev.c static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) · line 2955
Quoted 2 lines
arch/x86/kvm/svm/sev.c +2
-0+ rc =kvm_mmu_get_tdp_walk (vcpu, gpa, &pfn, &npt_level); +write_unlock (&kvm->mmu_lock);
What's the point of this walk? As soon as mmu_lock is dropped, all bets are off. At best this is a strong hint. It doesn't hurt anything per se, it's just a waste of cycles.
arch/x86/kvm/svm/sev.c static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) · line 2957
Quoted 2 lines
arch/x86/kvm/svm/sev.c +2
-0+if (!rc) { + pfn =kvm_mmu_map_tdp_page (vcpu, gpa,PFERR_USER_MASK , level);
Same here.
Quoted 18 lines
arch/x86/kvm/svm/sev.c +18
-0+if (is_error_noslot_pfn (pfn)) +return -EINVAL ; + } + +return 0 ; +} + +static int snp_gpa_to_hva (struct kvm *kvm,gpa_t gpa,hva_t *hva) +{ +struct kvm_memory_slot *slot; +gfn_t gfn =gpa_to_gfn (gpa); +int idx; + + idx =srcu_read_lock (&kvm->srcu); + slot =gfn_to_memslot (kvm, gfn); +if (!slot) { +srcu_read_unlock (&kvm->srcu, idx); +return -EINVAL ;
arch/x86/kvm/svm/sev.c static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) · line 2985
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ } + +/* +* Note, using the __gfn_to_hva_memslot() is not solely for performance, +* it's also necessary to avoid the "writable" check in __gfn_to_hva_many(), +* which will always fail on read-only memslots due to gfn_to_hva() assuming +* writes. +*/ + *hva =__gfn_to_hva_memslot (slot, gfn); +srcu_read_unlock (&kvm->srcu, idx);
*hva is effectively invalidated the instance kvm->srcu is unlocked, e.g. a pending memslot update can complete immediately after and delete/move the backing memslot.
Quoted 27 lines
arch/x86/kvm/svm/sev.c +27
-0+ +return 0 ; +} + +static int __snp_handle_page_state_change (struct kvm_vcpu *vcpu,enum psc_op op,gpa_t gpa, +int level) +{ +struct kvm_sev_info *sev = &to_kvm_svm (vcpu->kvm)->sev_info; +struct kvm *kvm = vcpu->kvm; +int rc, npt_level; +kvm_pfn_t pfn; +gpa_t gpa_end; + + gpa_end = gpa +page_level_size (level); + +while (gpa < gpa_end) { +/* +* If the gpa is not present in the NPT then build the NPT. +*/ + rc =snp_check_and_build_npt (vcpu, gpa, level); +if (rc) +return -EINVAL ; + +if (op ==SNP_PAGE_STATE_PRIVATE ) { +hva_t hva; + +if (snp_gpa_to_hva (kvm, gpa, &hva))
arch/x86/kvm/svm/sev.c static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) · line 3022
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+return -EINVAL ; + +/* +* Verify that the hva range is registered. This enforcement is +* required to avoid the cases where a page is marked private +* in the RMP table but never gets cleanup during the VM +* termination path. +*/ +mutex_lock (&kvm->lock); + rc =is_hva_registered (kvm, hva,page_level_size (level));
This will get a false negative if a hva+size spans two contiguous regions.
Also, storing a boolean return in a variable that is an int _and_ was already used for the kernel's standard
arch/x86/kvm/svm/sev.c static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) · line 3023
Quoted 1 line
arch/x86/kvm/svm/sev.c +1
-0+mutex_unlock (&kvm->lock);
This is also subject to races, e.g. userspace unregisters the hva immediately after this check, before KVM makes whatever conversion it makes below.
A linear walk through a list to find a range is also a bad idea, e.g. pathological worst case scenario is that userspace has created tens of thousands of individual regions. There is no restriction on the number of regions, just the number of pages that can be pinned.
I dislike the svm_(un)register_enc_region() scheme in general, but at least for SEV and SEV-ES the code is isolated, e.g. KVM is little more than a dump pipe to let userspace pin pages. I would like to go the opposite direction and work towards eliminating regions_list (or at least making it optional), not build more stuff on top.
The more I look at this, the more strongly I feel that private <=> shared conversions belong in the MMU, and that KVM's SPTEs should be the single source of truth for shared vs. private. E.g. add a SPTE_TDP_PRIVATE_MASK in the software available bits. I believe the only hiccup is the snafu where not zapping _all_ SPTEs on memslot deletion breaks QEMU+VFIO+GPU, i.e. KVM would lose its canonical info on unrelated memslot deletion.
But that is a solvable problem. Ideally the bug, wherever it is, would be root caused and fixed. I believe Peter (and Marc?) is going to work on reproducing the bug.
If we are unable to root cause and fix the bug, I think a viable workaround would be to clear the hardware present bit in unrelated SPTEs, but keep the SPTEs themselves. The idea mostly the same as the ZAPPED_PRIVATE concept from the initial TDX RFC. MMU notifier invalidations, memslot removal, RMP restoration, etc... would all continue to work since the SPTEs is still there, and KVM's page fault handler could audit any "blocked" SPTE when it's refaulted (I'm pretty sure it'd be impossible for the PFN to change, since any PFN change would require a memslot update or mmu_notifier invalidation).
The downside to that approach is that it would require walking all SPTEs to do a memslot deletion, i.e. we'd lose the "fast zap" behavior. If that's a performance issue, the behavior could be opt-in (but not for SNP/TDX).
arch/x86/kvm/svm/sev.c static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) · line 3033
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+if (!rc) +return -EINVAL ; + +/* +* Mark the userspace range unmerable before adding the pages +* in the RMP table. +*/ +mmap_write_lock (kvm->mm); + rc =snp_mark_unmergable (kvm, hva,page_level_size (level)); +mmap_write_unlock (kvm->mm);
As mentioned in an earlier patch, this simply cannot work.
arch/x86/kvm/svm/sev.c static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) · line 3040
Quoted 7 lines
arch/x86/kvm/svm/sev.c +7
-0+if (rc) +return -EINVAL ; + } + +write_lock (&kvm->mmu_lock); + + rc =kvm_mmu_get_tdp_walk (vcpu, gpa, &pfn, &npt_level);
Same comment about the bool into int. Though in this case I'd say have kvm_mmu_get_tdp_walk() return 0/-errno, not a bool. Boolean returns for helpers without "is_", "test_", etc... are generally confusing.
arch/x86/kvm/svm/sev.c static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) · line 3047
Quoted 7 lines
arch/x86/kvm/svm/sev.c +7
-0+if (!rc) { +/* +* This may happen if another vCPU unmapped the page +* before we acquire the lock. Retry the PSC. +*/ +write_unlock (&kvm->mmu_lock); +return 0 ;
How will the caller (guest?) know to retry the PSC if KVM returns "success"?
Quoted 9 lines
arch/x86/kvm/svm/sev.c +9
-0+ } + +/* +* Adjust the level so that we don't go higher than the backing +* page level. +*/ + level =min_t (size_t, level, npt_level); + +trace_kvm_snp_psc (vcpu->vcpu_id, pfn, gpa, op, level);
arch/x86/kvm/svm/sev.c static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) · line 3066
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ +switch (op) { +case SNP_PAGE_STATE_SHARED : + rc =snp_make_page_shared (kvm, gpa, pfn, level); +break ; +case SNP_PAGE_STATE_PRIVATE : + rc =rmp_make_private (pfn, gpa, level, sev->asid, false); +break ; +default : + rc = -EINVAL ;
Not that it really matters, because I don't think the MADV_* approach is viable, but this neglects to undo snp_mark_unmergable() on failure.
Quoted 20 lines
arch/x86/kvm/svm/sev.c +17
-0+break ; + } + +write_unlock (&kvm->mmu_lock); + +if (rc) { +pr_err_ratelimited ("Error op %d gpa %llx pfn %llx level %d rc %d\n" , + op, gpa, pfn, level, rc); +return rc; + } + + gpa = gpa +page_level_size (level); + } + +return 0 ; +} +static int sev_handle_vmgexit_msr_protocol (struct vcpu_svm *svm) {struct vmcb_control_area *control = &svm->vmcb->control;
Sean Christopherson
maintainerRe: [PATCH Part2 v5 39/45] KVM: SVM: Introduce ops for the post gfn map and unmap
In reply to Brijesh SinghQuoted 8 lines · Brijesh Singh
When SEV-SNP is enabled in the guest VM, the guest memory pages can either be a private or shared. A write from the hypervisor goes through the RMP checks. If hardware sees that hypervisor is attempting to write to a guest private page, then it triggers an RMP violation #PF.
To avoid the RMP violation, add post_{map,unmap}_gfn() ops that can be used to verify that its safe to map a given guest page. Use the SRCU to protect against the page state change for existing mapped pages.
SRCU isn't protecting anything. The synchronize_srcu_expedited() in the PSC code forces it to wait for existing maps to go away, but it doesn't prevent new maps from being created while the actual RMP updates are in-flight. Most telling is that the RMP updates happen _after_ the synchronize_srcu_expedited() call.
This also doesn't handle kvm_{read,write}_guest_cached().
I can't help but note that the private memslots idea[*] would handle this gracefully, e.g. the memslot lookup would fail, and any change in private memslots would invalidate the cache due to a generation mismatch.
KSM is another mess that would Just Work.
I'm not saying that SNP should be blocked on support for unmapping guest private memory, but I do think we should strongly consider focusing on that effort rather than trying to fix things piecemeal throughout KVM. I don't think it's too absurd to say that it might actually be faster overall. And I 100% think that having a cohesive design and uABI for SNP and TDX would be hugely beneficial to KVM.
[*] https://lkml.kernel.org/r/[email protected]
arch/x86/kvm/svm/sev.c void sev_rmp_page_level_adjust(struct kvm *kvm, kvm_pfn_t pfn, int *level) · line 3635
Quoted 11 lines
arch/x86/kvm/svm/sev.c +11
-0+int sev_post_map_gfn (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int *token) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +int level; + +if (!sev_snp_guest (kvm)) +return 0 ; + + *token =srcu_read_lock (&sev->psc_srcu); + +/* If pfn is not added as private then fail */
This comment and the pr_err() are backwards, and confused the heck out of me. snp_lookup_rmpentry() returns '1' if the pfn is assigned, a.k.a. private. That means this code throws an error if the page is private, i.e. requires the page to be shared. Which makes sense given the use cases, it's just incredibly confusing.
arch/x86/kvm/svm/sev.c void sev_rmp_page_level_adjust(struct kvm *kvm, kvm_pfn_t pfn, int *level) · line 3636
Quoted 1 line
arch/x86/kvm/svm/sev.c +1
-0+if (snp_lookup_rmpentry (pfn, &level) ==1 ) {
Any reason not to provide e.g. rmp_is_shared() and rmp_is_private() so that callers don't have to care as much about the return values? The -errno/0/1 semantics are all but guarantee to bite us in the rear at some point.
Actually, peeking at other patches, I think it already has. This usage in __unregister_enc_region_locked() is wrong:
/*
* The guest memory pages are assigned in the RMP table. Unassign it
* before releasing the memory.
*/
if (sev_snp_guest(kvm)) {
for (i = 0; i < region->npages; i++) {
pfn = page_to_pfn(region->pages[i]);
if (!snp_lookup_rmpentry(pfn, &level)) <-- attempts make_shared() on non-existent entry
continue;
cond_resched();
if (level > PG_LEVEL_4K)
pfn &= ~(KVM_PAGES_PER_HPAGE(PG_LEVEL_2M) - 1);
host_rmp_make_shared(pfn, level, true);
}
}Quoted 19 lines
arch/x86/kvm/svm/sev.c +7
-0+srcu_read_unlock (&sev->psc_srcu, *token); +pr_err_ratelimited ("failed to map private gfn 0x%llx pfn 0x%llx\n" , gfn, pfn); +return -EBUSY ; + } + +return 0 ; +}static struct kvm_x86_init_ops svm_init_ops__initdata = {arch/x86/kvm/svm/svm.h +1
-0@@ -76,16 +76,22 @@ 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 */
arch/x86/kvm/svm/svm.h struct kvm_sev_info { · line 91
Quoted 10 lines
arch/x86/kvm/svm/svm.h +4
-0int fd;/* SEV device fd */ +unsigned long pages_locked;/* Number of pages locked */ struct list_head regions_list;/* List of registered regions */ +u64 ap_jump_table;/* SEV-ES AP Jump Table address */ +struct kvm *enc_context_owner;/* Owner of copied encryption context */ struct misc_cg *misc_cg;/* For misc cgroup accounting */ +
Unrelated whitespace changes.
Quoted 4 lines
arch/x86/kvm/svm/svm.h +1
-0u64 snp_init_flags;void *snp_context;/* SNP guest context page */ +struct srcu_struct psc_srcu; };
Re: [PATCH Part2 v5 26/45] KVM: SVM: Mark the private vma unmerable for SEV-SNP guests
In reply to Sean ChristophersonQuoted 10 lines · Sean Christopherson
Quoted 8 lines · Brijesh Singh
When SEV-SNP is enabled, the guest private pages are added in the RMP table; while adding the pages, the
rmp_make_private()unmaps the pages from the direct map. If KSM attempts to access those unmapped pages then it will trigger #PF (page-not-present).Encrypted guest pages cannot be shared between the process, so an userspace should not mark the region mergeable but to be safe, mark the process vma unmerable before adding the pages in the RMP table.
To be safe from what? Does the !PRESENT #PF crash the kernel?
Yes, kernel crashes when KSM attempts to access to an unmaped pfn.
[...]
Quoted 8 lines
arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1738
Quoted 3 lines
arch/x86/kvm/svm/sev.c +3
-0+mmap_write_lock (kvm->mm); + ret =snp_mark_unmergable (kvm, params.uaddr, params.len); +mmap_write_unlock (kvm->mm);This does not, and practically speaking cannot, work. There are multiple TOCTOU bugs, here and in
__snp_handle_page_state_change(). Userspace canmadvise()the range at any later point, munmap()/mmap() the entire range, mess with the memslots in the PSC case, and so on and so forth. Relying onMADV_UNMERGEABLEfor functional correctness simply cannot work in KVM, barringmmu_notifierand a big pile of code.
AFAICT, ksm does not exclude the unmapped pfn from its scan list. We need to tell ksm somehow to exclude the unmapped pfn from its scan list. I understand that if userspace is messing with us, we have an issue, but it's a userspace bug ;) To fix it right, we need to enhance ksm to exclude the pfn when it is getting unmapped from the direct map. I believe that work can be done outside of the SNP series. I am okay to drop snp_mark_unmerable(), and until then, we just run with KSM disabled. Thoughts?
thanks
Sean Christopherson
maintainerRe: [PATCH Part2 v5 26/45] KVM: SVM: Mark the private vma unmerable for SEV-SNP guests
Quoted 8 lines · Brijesh Singh
Quoted 6 lines · Sean Christopherson
Quoted 5 lines · Brijesh Singh
When SEV-SNP is enabled, the guest private pages are added in the RMP table; while adding the pages, the
rmp_make_private()unmaps the pages from the direct map. If KSM attempts to access those unmapped pages then it will trigger #PF (page-not-present).
Quoted 6 lines
Quoted 4 lines
Quoted 3 lines
Encrypted guest pages cannot be shared between the process, so an userspace should not mark the region mergeable but to be safe, mark the process vma unmerable before adding the pages in the RMP table.
To be safe from what? Does the !PRESENT #PF crash the kernel?
Yes, kernel crashes when KSM attempts to access to an unmaped pfn.
Is this problem unique to nuking the direct map (patch 05), or would it also be a problem (in the form of an RMP violation) if the direct map were demoted to 4k pages?
Quoted 20 lines
[...]
Quoted 8 lines
arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1738
Quoted 3 lines
arch/x86/kvm/svm/sev.c +3
-0+mmap_write_lock (kvm->mm); + ret =snp_mark_unmergable (kvm, params.uaddr, params.len); +mmap_write_unlock (kvm->mm);This does not, and practically speaking cannot, work. There are multiple TOCTOU bugs, here and in
__snp_handle_page_state_change(). Userspace canmadvise()the range at any later point, munmap()/mmap() the entire range, mess with the memslots in the PSC case, and so on and so forth. Relying onMADV_UNMERGEABLEfor functional correctness simply cannot work in KVM, barringmmu_notifierand a big pile of code.AFAICT, ksm does not exclude the unmapped pfn from its scan list. We need to tell ksm somehow to exclude the unmapped pfn from its scan list. I understand that if userspace is messing with us, we have an issue, but it's a userspace bug ;) To fix it right, we need to enhance ksm to exclude the pfn when it is getting unmapped from the direct map. I believe that work can be done outside of the SNP series. I am okay to drop
snp_mark_unmerable(), and until then, we just run with KSM disabled. Thoughts?thanks