x86: Secure Encrypted Virtualization (AMD)
Subsystems (7)
v6 cover x86: Secure Encrypted Virtualization (AMD)
This part of Secure Encryted Virtualization (SEV) patch series focuses on KVM changes required to create and manage SEV guests.
SEV is an extension to the AMD-V architecture which supports running encrypted virtual machine (VMs) under the control of a hypervisor. Encrypted VMs have their pages (code and data) secured such that only the guest itself has access to unencrypted version. Each encrypted VM is associated with a unique encryption key; if its data is accessed to a different entity using a different key the encrypted guest's data will be incorrectly decrypted, leading to unintelligible data. This security model ensures that hypervisor will no longer able to inspect or alter any guest code or data.
The key management of this feature is handled by a separate processor known as the AMD Secure Processor (AMD-SP) which is present on AMD SOCs. The SEV Key Management Specification (see below) provides a set of commands which can be used by hypervisor to load virtual machine keys through the AMD-SP driver.
The patch series adds a new ioctl in KVM driver (KVM_MEMORY_ENCRYPTION_OP). The ioctl will be used by qemu to issue SEV guest-specific commands defined in Key Management Specification.
The following links provide additional details:
AMD Memory Encryption whitepaper:
http://amd-dev.wpengine.netdna-cdn.com/wordpress/media/2013/12/AMD_Memory_Encryption_Whitepaper_v7-Public.pdf
AMD64 Architecture Programmer's Manual:
http://support.amd.com/TechDocs/24593.pdf
SME is section 7.10
SEV is section 15.34
Secure Encrypted Virutualization Key Management:
http://support.amd.com/TechDocs/55766_SEV-KM API_Specification.pdf
KVM Forum Presentation:
http://www.linux-kvm.org/images/7/74/02x08A-Thomas_Lendacky-AMDs_Virtualizatoin_Memory_Encryption_Technology.pdf
SEV Guest BIOS support:
SEV support has been interated into EDKII/OVMF BIOS
https://github.com/tianocore/edk2
SEV Part 1 patch series: https://marc.info/?l=kvm&m=150816835817641&w=2
--
The series is based on kvm/master commit : cc9085b68753 (Merge branch 'kvm-ppc-fixes')
Complete tree is available at:
repo: https://github.com/codomania/kvm.git
branch: sev-v6-p2
TODO:
* Add SEV guest migration command support
Changes since v5:
* split the PSP driver support into multiple patches
* multiple improvements from Boris
* remove mem_enc_enabled() ops
Changes since v4:
* Fixes to address kbuild robot errors
* Add 'sev' module params to allow enable/disable SEV feature
* Update documentation
* Multiple fixes to address v4 feedbacks
* Some coding style changes to address checkpatch reports
Changes since v3:
* Re-design the PSP interface support patch
* Rename the ioctls based on the feedbacks
* Improve documentation
* Fix i386 build issues
* Add LAUNCH_SECRET command
* Add new Kconfig option to enable SEV support
* Changes to address v3 feedbacks.
Changes since v2:
* Add KVM_MEMORY_ENCRYPT_REGISTER/UNREGISTER_RAM ioct to register encrypted memory ranges (recommend by Paolo)
* Extend kvm_x86_ops to provide new memory_encryption_enabled ops
* Enhance DEBUG DECRYPT/ENCRYPT commands to work with more than one page \
(recommended by Paolo)
* Optimize LAUNCH_UPDATE command to reduce the number of calls to AMD-SP driver
* Changes to address v2 feedbacks
Borislav Petkov (1):
crypto: ccp: Build the AMD secure processor driver only with AMD CPU
support
Brijesh Singh (34):
Documentation/virtual/kvm: Add AMD Secure Encrypted Virtualization
(SEV)
KVM: SVM: Prepare to reserve asid for SEV guest
KVM: X86: Extend CPUID range to include new leaf
KVM: Introduce KVM_MEMORY_ENCRYPT_OP ioctl
KVM: Introduce KVM_MEMORY_ENCRYPT_{UN,}REG_REGION ioctl
crypto: ccp: Define SEV userspace ioctl and command id
crypto: ccp: Define SEV key management command id
crypto: ccp: Add Platform Security Processor (PSP) device support
crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
crypto: ccp: Implement SEV_FACTORY_RESET ioctl command
crypto: ccp: Implement SEV_PLATFORM_STATUS ioctl command
crypto: ccp: Implement SEV_PEK_GEN ioctl command
crypto: ccp: Implement SEV_PDH_GEN ioctl command
crypto: ccp: Implement SEV_PEK_CSR ioctl command
crypto: ccp: Implement SEV_PEK_CERT_IMPORT ioctl command
crypto: ccp: Implement SEV_PDH_CERT_EXPORT ioctl command
KVM: X86: Add CONFIG_KVM_AMD_SEV
KVM: SVM: Add sev module_param
KVM: SVM: Reserve ASID range for SEV guest
KVM: Define SEV key management command id
KVM: SVM: Add KVM_SEV_INIT command
KVM: SVM: VMRUN should use assosiated ASID when SEV is enabled
KVM: SVM: Add support for KVM_SEV_LAUNCH_START command
KVM: SVM: Add support for KVM_SEV_LAUNCH_UPDATE_DATA command
KVM: SVM: Add support for KVM_SEV_LAUNCH_MEASURE command
KVM: SVM: Add support for SEV LAUNCH_FINISH command
KVM: SVM: Add support for SEV GUEST_STATUS command
KVM: SVM: Add support for SEV DEBUG_DECRYPT command
KVM: SVM: Add support for SEV DEBUG_ENCRYPT command
KVM: SVM: Add support for SEV LAUNCH_SECRET command
KVM: SVM: Pin guest memory when SEV is active
KVM: SVM: Clear C-bit from the page fault address
KVM: SVM: Do not install #UD intercept when SEV is enabled
KVM: X86: Restart the guest when insn_len is zero and SEV is enabled
Tom Lendacky (3):
x86/CPU/AMD: Add the Secure Encrypted Virtualization CPU feature
kvm: svm: prepare for new bit definition in nested_ctl
kvm: svm: Add SEV feature definitions to KVM
Documentation/virtual/kvm/00-INDEX | 3 + .../virtual/kvm/amd-memory-encryption.txt | 201 ++++ Documentation/virtual/kvm/api.txt | 50 + arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/kvm_host.h | 15 + arch/x86/include/asm/msr-index.h | 2 + arch/x86/include/asm/svm.h | 3 + arch/x86/kernel/cpu/amd.c | 66 +- arch/x86/kernel/cpu/scattered.c | 1 + arch/x86/kvm/Kconfig | 10 + arch/x86/kvm/cpuid.c | 2 +- arch/x86/kvm/mmu.c | 10 + arch/x86/kvm/svm.c | 1167 +++++++++++++++++++- arch/x86/kvm/x86.c | 30 + drivers/crypto/ccp/Kconfig | 12 + drivers/crypto/ccp/Makefile | 1 + drivers/crypto/ccp/psp-dev.c | 736 ++++++++++++ drivers/crypto/ccp/psp-dev.h | 80 ++ drivers/crypto/ccp/sp-dev.c | 26 + drivers/crypto/ccp/sp-dev.h | 24 +- drivers/crypto/ccp/sp-pci.c | 52 + include/linux/psp-sev.h | 657 +++++++++++ include/uapi/linux/kvm.h | 90 ++ include/uapi/linux/psp-sev.h | 113 ++ 24 files changed, 3322 insertions(+), 30 deletions(-) create mode 100644 Documentation/virtual/kvm/amd-memory-encryption.txt create mode 100644 drivers/crypto/ccp/psp-dev.c create mode 100644 drivers/crypto/ccp/psp-dev.h create mode 100644 include/linux/psp-sev.h create mode 100644 include/uapi/linux/psp-sev.h
Signature · 1 line
2.9.5
PATCH v6 1/38 Documentation/virtual/kvm: Add AMD Secure Encrypted Virtualization (SEV)
Create a Documentation entry to describe the AMD Secure Encrypted Virtualization (SEV) feature.
--- Documentation/virtual/kvm/00-INDEX | 3 ++ .../virtual/kvm/amd-memory-encryption.txt | 38 ++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 Documentation/virtual/kvm/amd-memory-encryption.txt
Documentation/virtual/kvm/00-INDEX +3 -0
@@ -26,3 +26,6 @@ s390-diag.txt - Diagnose hypercall description (for IBM S/390) timekeeping.txt - timekeeping virtualization for x86-based architectures. +amd-memory-encryption.txt + - notes on AMD Secure Encrypted Virtualization feature and SEV firmware + command description
Documentation/virtual/kvm/amd-memory-encryption.txt new file +38 -0
@@ -0,0 +1,38 @@ +Secure Encrypted Virtualization (SEV) is a feature found on AMD processors. + +SEV is an extension to the AMD-V architecture which supports running +virtual machines (VMs) under the control of a hypervisor. When enabled, +the memory contents of a VM will be transparently encrypted with a key +unique to that VM. + +The hypervisor can determine the SEV support through the CPUID +instruction. The CPUID function 0x8000001f reports information related +to SEV: + + 0x8000001f[eax]: + Bit[1] indicates support for SEV + ... + [ecx]: + Bits[31:0] Number of encrypted guests supported simultaneously + +If support for SEV is present, MSR 0xc001_0010 (MSR_K8_SYSCFG) and MSR 0xc001_0015 +(MSR_K7_HWCR) can be used to determine if it can be enabled: + + 0xc001_0010: + Bit[23] 1 = memory encryption can be enabled + 0 = memory encryption can not be enabled + + 0xc001_0015: + Bit[0] 1 = memory encryption can be enabled + 0 = memory encryption can not be enabled + +When SEV support is available, it can be enabled in a specific VM by +setting the SEV bit before executing VMRUN. + + VMCB[0x90]: + Bit[1] 1 = SEV is enabled + 0 = SEV is disabled + +SEV hardware uses ASIDs to associate a memory encryption key with a VM. +Hence, the ASID for the SEV-enabled guests must be from 1 to a maximum value +defined in the CPUID 0x8000001f[ecx] field.
Signature · 1 line
2.9.5
Jonathan Corbet
maintainerRe: [Part2 PATCH v6 01/38] Documentation/virtual/kvm: Add AMD Secure Encrypted Virtualization (SEV)
Quoted 2 lines · On Thu, 19 Oct 2017 21:33:36 -0500 Brijesh Singh <[email protected]> wrote:
Create a Documentation entry to describe the AMD Secure Encrypted Virtualization (SEV) feature.
A couple of quick notes...
- This is describing an x86 subarchitecture feature, so it seems to me that Documentation/x86 would be a more logical place for it. Yes, KVM can use this feature, but it's not a KVM feature and there might be something else that takes advantage of it too.
- Any chance of getting it formatted as RST? It's almost there already.
Beyond that, the document obviously falls far short of what one would have to know to use SEV; probably filling all that in would make it long indeed.
Thanks,
jon
Re: [Part2 PATCH v6 01/38] Documentation/virtual/kvm: Add AMD Secure Encrypted Virtualization (SEV)
Quoted 6 lines · Jonathan Corbet
Quoted 2 lines · On Thu, 19 Oct 2017 21:33:36 -0500 Brijesh Singh <[email protected]> wrote:
Create a Documentation entry to describe the AMD Secure Encrypted Virtualization (SEV) feature.
Quoted 7 lines
A couple of quick notes...
- This is describing an x86 subarchitecture feature, so it seems to me that
Documentation/x86would be a more logical place for it. Yes, KVM can use this feature, but it's not a KVM feature and there might be something else that takes advantage of it too.
In part1 of SEV patches, we do expand the Documentation/x86/amd-memory-eencryption.txt to explain the SEV architecture feature. In this patch, we focus on documenting the SEV specific changes from the KVM point of view and how it integrate with KVM.
Quoted 2 lines
- Any chance of getting it formatted as RST? It's almost there already.
Sure, next rev I will convert it to RST format.
Quoted 4 lines
Beyond that, the document obviously falls far short of what one would have to know to use SEV; probably filling all that in would make it long indeed.
In later part of patches (Patch 24/38 [1]), I expand this doc to provide high level overview of various SEV commands to support common lifecycle events of SEV guests, such as launching, running, snapshotting, migrating etc.
PATCH v6 2/38 x86/CPU/AMD: Add the Secure Encrypted Virtualization CPU feature
In reply to Brijesh SinghFrom: Tom Lendacky <[email protected]>
Update the CPU features to include identifying and reporting on the Secure Encrypted Virtualization (SEV) feature. SEV is identified by CPUID 0x8000001f, but requires BIOS support to enable it (set bit 23 of MSR_K8_SYSCFG and set bit 0 of MSR_K7_HWCR). Only show the SEV feature as available if reported by CPUID and enabled by BIOS.
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/msr-index.h | 2 ++ arch/x86/kernel/cpu/amd.c | 66 ++++++++++++++++++++++++++------------ arch/x86/kernel/cpu/scattered.c | 1 + 4 files changed, 50 insertions(+), 20 deletions(-)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -197,6 +197,7 @@#define X86_FEATURE_HW_PSTATE ( 7*32+ 8)/* AMD HW-PState */ #define X86_FEATURE_PROC_FEEDBACK ( 7*32+ 9)/* AMD ProcFeedbackInterface */ #define X86_FEATURE_SME ( 7*32+10)/* AMD Secure Memory Encryption */ +#define X86_FEATURE_SEV ( 7*32+11)/* AMD Secure Encrypted Virtualization */ #define X86_FEATURE_INTEL_PPIN ( 7*32+14)/* Intel Processor Inventory Number */ #define X86_FEATURE_INTEL_PT ( 7*32+15)/* Intel Processor Trace */
arch/x86/include/asm/msr-index.h +2 -0
@@ -378,6 +378,8 @@#define MSR_K7_PERFCTR3 0xc0010007#define MSR_K7_CLK_CTL 0xc001001b#define MSR_K7_HWCR 0xc0010015 +#define MSR_K7_HWCR_SMMLOCK_BIT 0 +#define MSR_K7_HWCR_SMMLOCK BIT_ULL(MSR_K7_HWCR_SMMLOCK_BIT)#define MSR_K7_FID_VID_CTL 0xc0010041#define MSR_K7_FID_VID_STATUS 0xc0010042
arch/x86/kernel/cpu/amd.c +46 -20
78 lines not on this page · raw message
arch/x86/kernel/cpu/scattered.c +1 -0
@@ -32,6 +32,7 @@ static const struct cpuid_bit cpuid_bits[] = { { X86_FEATURE_CPB, CPUID_EDX, 9, 0x80000007, 0 }, { X86_FEATURE_PROC_FEEDBACK, CPUID_EDX, 11, 0x80000007, 0 }, { X86_FEATURE_SME, CPUID_EAX, 0, 0x8000001f, 0 }, + { X86_FEATURE_SEV, CPUID_EAX, 1, 0x8000001f, 0 }, { 0, 0, 0, 0, 0 } };
Signature · 1 line
2.9.5
PATCH v6 3/38 kvm: svm: prepare for new bit definition in nested_ctl
In reply to Brijesh SinghFrom: Tom Lendacky <[email protected]>
Currently the nested_ctl variable in the vmcb_control_area structure is used to indicate nested paging support. The nested paging support field is actually defined as bit 0 of the field. In order to support a new feature flag the usage of the nested_ctl and nested paging support must be converted to operate on a single bit.
--- arch/x86/include/asm/svm.h | 2 ++ arch/x86/kvm/svm.c | 7 ++++--- 2 files changed, 6 insertions(+), 3 deletions(-)
arch/x86/include/asm/svm.h +2 -0
@@ -145,6 +145,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define SVM_VM_CR_SVM_LOCK_MASK 0x0008ULL#define SVM_VM_CR_SVM_DIS_MASK 0x0010ULL +#define SVM_NESTED_CTL_NP_ENABLE BIT(0) +struct __attribute__ ((__packed__ ))vmcb_seg {u16 selector;u16 attrib;
arch/x86/kvm/svm.c +4 -3
@@ -1296,7 +1296,7 @@ static void init_vmcb(struct vcpu_svm *svm)if (npt_enabled) {/* Setup VMCB for Nested Paging */ - control->nested_ctl =1 ; + control->nested_ctl |=SVM_NESTED_CTL_NP_ENABLE ;clr_intercept (svm,INTERCEPT_INVLPG );clr_exception_intercept (svm,PF_VECTOR );clr_cr_intercept (svm,INTERCEPT_CR3_READ );
@@ -2910,7 +2910,8 @@ static bool nested_vmcb_checks(struct vmcb *vmcb)if (vmcb->control.asid ==0 )return false; -if (vmcb->control.nested_ctl && !npt_enabled) +if ((vmcb->control.nested_ctl &SVM_NESTED_CTL_NP_ENABLE ) && + !npt_enabled)return false;return true;
@@ -2985,7 +2986,7 @@ static bool nested_svm_vmrun(struct vcpu_svm *svm)else svm->vcpu.arch.hflags &= ~HF_HIF_MASK ; -if (nested_vmcb->control.nested_ctl) { +if (nested_vmcb->control.nested_ctl &SVM_NESTED_CTL_NP_ENABLE ) {kvm_mmu_unload (&svm->vcpu); svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;nested_svm_init_mmu_context (&svm->vcpu);
Signature · 1 line
2.9.5
PATCH v6 4/38 kvm: svm: Add SEV feature definitions to KVM
In reply to Brijesh SinghFrom: Tom Lendacky <[email protected]>
Define the SEV enable bit for the VMCB control structure. The hypervisor will use this bit to enable SEV in the guest.
--- arch/x86/include/asm/svm.h | 1 + 1 file changed, 1 insertion(+)
arch/x86/include/asm/svm.h +1 -0
@@ -146,6 +146,7 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define SVM_VM_CR_SVM_DIS_MASK 0x0010ULL#define SVM_NESTED_CTL_NP_ENABLE BIT(0) +#define SVM_NESTED_CTL_SEV_ENABLE BIT(1)struct __attribute__ ((__packed__ ))vmcb_seg {u16 selector;
Signature · 1 line
2.9.5
PATCH v6 5/38 KVM: SVM: Prepare to reserve asid for SEV guest
In reply to Brijesh SinghCurrently, ASID allocation start at 1. Add a svm_vcpu_data.min_asid which allows supplying a dynamic start ASID.
--- arch/x86/kvm/svm.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kvm/svm.c +3 -1
@@ -525,6 +525,7 @@ struct svm_cpu_data {u64 asid_generation;u32 max_asid;u32 next_asid; +u32 min_asid;struct kvm_ldttss_desc *tss_desc;struct page *save_area;
@@ -782,6 +783,7 @@ static int svm_hardware_enable(void) sd->asid_generation =1 ; sd->max_asid =cpuid_ebx (SVM_CPUID_FUNC ) -1 ; sd->next_asid = sd->max_asid +1 ; + sd->min_asid =1 ; gdt =get_current_gdt_rw (); sd->tss_desc = (struct kvm_ldttss_desc *)(gdt +GDT_ENTRY_TSS );
@@ -2091,7 +2093,7 @@ static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd) {if (sd->next_asid > sd->max_asid) { ++sd->asid_generation; - sd->next_asid =1 ; + sd->next_asid = sd->min_asid; svm->vmcb->control.tlb_ctl =TLB_CONTROL_FLUSH_ALL_ASID ; }
Signature · 1 line
2.9.5
PATCH v6 6/38 KVM: X86: Extend CPUID range to include new leaf
In reply to Brijesh SinghThis CPUID leaf provides the memory encryption support information on AMD Platform. Its complete description is available in APM volume 2, Section 15.34
--- arch/x86/kvm/cpuid.c | 2 +- arch/x86/kvm/svm.c | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +1 -1
@@ -604,7 +604,7 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, entry->edx =0 ;break ;case 0x80000000 : - entry->eax =min (entry->eax,0x8000001a ); + entry->eax =min (entry->eax,0x8000001f );break ;case 0x80000001 : entry->edx &= kvm_cpuid_8000_0001_edx_x86_features;
arch/x86/kvm/svm.c +6 -0
@@ -5149,6 +5149,12 @@ static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) entry->edx |=SVM_FEATURE_NPT ;break ; +case 0x8000001F : +/* Support memory encryption cpuid if host supports it */ +if (boot_cpu_has (X86_FEATURE_SEV )) +cpuid (0x8000001f , &entry->eax, &entry->ebx, + &entry->ecx, &entry->edx); + } }
Signature · 1 line
2.9.5
PATCH v6 7/38 KVM: Introduce KVM_MEMORY_ENCRYPT_OP ioctl
In reply to Brijesh SinghIf the hardware supports memory encryption then the KVM_MEMORY_ENCRYPT_OP ioctl can be used by qemu to issue a platform specific memory encryption commands.
--- Documentation/virtual/kvm/api.txt | 16 ++++++++++++++++ arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/x86.c | 6 ++++++ include/uapi/linux/kvm.h | 2 ++ 4 files changed, 26 insertions(+)
Documentation/virtual/kvm/api.txt +16 -0
@@ -3390,6 +3390,22 @@ invalid, if invalid pages are written to (e.g. after the end of memory) or if no page table is present for the addresses (e.g. when using hugepages). +4.109 KVM_MEMORY_ENCRYPT_OP + +Capability: basic +Architectures: x86 +Type: system +Parameters: an opaque platform specific structure (in/out) +Returns: 0 on success; -1 on error + +If the platform supports creating encrypted VMs then this ioctl can be used +for issuing platform-specific memory encryption commands to manage those +encrypted VMs. + +Currently, this ioctl is used for issuing Secure Encrypted Virtualization +(SEV) commands on AMD Processors. The SEV commands are defined in +Documentation/virtual/kvm/amd-memory-encryption.txt. + 5. The kvm_run structure ------------------------
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1061,6 +1061,8 @@ struct kvm_x86_ops {void (*cancel_hv_timer)(struct kvm_vcpu *vcpu);void (*setup_mce)(struct kvm_vcpu *vcpu); + +int (*mem_enc_op)(struct kvm *kvm,void __user *argp); };struct kvm_arch_async_pf {
arch/x86/kvm/x86.c +6 -0
@@ -4270,6 +4270,12 @@ long kvm_arch_vm_ioctl(struct file *filp, r =kvm_vm_ioctl_enable_cap (kvm, &cap);break ; } +case KVM_MEMORY_ENCRYPT_OP : { + r = -ENOTTY ; +if (kvm_x86_ops->mem_enc_op) + r = kvm_x86_ops->mem_enc_op (kvm, argp); +break ; + }default : r = -ENOTTY ; }
include/uapi/linux/kvm.h +2 -0
@@ -1356,6 +1356,8 @@ struct kvm_s390_ucas_mapping {/* Available with KVM_CAP_S390_CMMA_MIGRATION */ #define KVM_S390_GET_CMMA_BITS _IOWR(KVMIO, 0xb8, struct kvm_s390_cmma_log)#define KVM_S390_SET_CMMA_BITS _IOW(KVMIO, 0xb9, struct kvm_s390_cmma_log) +/* Memory Encryption Commands */ +#define KVM_MEMORY_ENCRYPT_OP _IOWR(KVMIO, 0xba, unsigned long)#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)
Signature · 1 line
2.9.5
PATCH v6 8/38 KVM: Introduce KVM_MEMORY_ENCRYPT_{UN,}REG_REGION ioctl
In reply to Brijesh SinghIf hardware supports memory encryption then KVM_MEMORY_ENCRYPT_REG_REGION and KVM_MEMORY_ENCRYPT_UNREG_REGION ioctl's can be used by userspace to register/unregister the guest memory regions which may contain the encrypted data (e.g guest RAM, PCI BAR, SMRAM etc).
--- Documentation/virtual/kvm/api.txt | 34 ++++++++++++++++++++++++++++++++++ arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/x86.c | 24 ++++++++++++++++++++++++ include/uapi/linux/kvm.h | 8 ++++++++ 4 files changed, 68 insertions(+)
Documentation/virtual/kvm/api.txt +34 -0
@@ -3406,6 +3406,40 @@ Currently, this ioctl is used for issuing Secure Encrypted Virtualization (SEV) commands on AMD Processors. The SEV commands are defined in Documentation/virtual/kvm/amd-memory-encryption.txt. +4.110 KVM_MEMORY_ENCRYPT_REG_REGION + +Capability: basic +Architectures: x86 +Type: system +Parameters: struct kvm_enc_region (in) +Returns: 0 on success; -1 on error + +This ioctl can be used to register a guest memory region which may +contain encrypted data (e.g. guest RAM, SMRAM etc). + +It is used in the SEV-enabled guest. When encryption is enabled, a guest +memory region may contain encrypted data. The SEV memory encryption +engine uses a tweak such that two identical plaintext pages, each at +different locations will have differing ciphertexts. So swapping or +moving ciphertext of those pages will not result in plaintext being +swapped. So relocating (or migrating) physical backing pages for the SEV +guest will require some additional steps. + +Note: The current SEV key management spec does not provide commands to +swap or migrate (move) ciphertext pages. Hence, for now we pin the guest +memory region registered with the ioctl. + +4.111 KVM_MEMORY_ENCRYPT_UNREG_REGION + +Capability: basic +Architectures: x86 +Type: system +Parameters: struct kvm_enc_region (in) +Returns: 0 on success; -1 on error + +This ioctl can be used to unregister the guest memory region registered +with KVM_MEMORY_ENCRYPT_REG_REGION ioctl above. + 5. The kvm_run structure ------------------------
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1063,6 +1063,8 @@ struct kvm_x86_ops {void (*setup_mce)(struct kvm_vcpu *vcpu);int (*mem_enc_op)(struct kvm *kvm,void __user *argp); +int (*mem_enc_reg_region)(struct kvm *kvm,struct kvm_enc_region *argp); +int (*mem_enc_unreg_region)(struct kvm *kvm,struct kvm_enc_region *argp); };struct kvm_arch_async_pf {
arch/x86/kvm/x86.c +24 -0
@@ -4276,6 +4276,30 @@ long kvm_arch_vm_ioctl(struct file *filp, r = kvm_x86_ops->mem_enc_op (kvm, argp);break ; } +case KVM_MEMORY_ENCRYPT_REG_REGION : { +struct kvm_enc_region region; + + r = -EFAULT ; +if (copy_from_user (®ion, argp,sizeof (region))) + goto out; + + r = -ENOTTY ; +if (kvm_x86_ops->mem_enc_reg_region) + r = kvm_x86_ops->mem_enc_reg_region (kvm, ®ion); +break ; + } +case KVM_MEMORY_ENCRYPT_UNREG_REGION : { +struct kvm_enc_region region; + + r = -EFAULT ; +if (copy_from_user (®ion, argp,sizeof (region))) + goto out; + + r = -ENOTTY ; +if (kvm_x86_ops->mem_enc_unreg_region) + r = kvm_x86_ops->mem_enc_unreg_region (kvm, ®ion); +break ; + }default : r = -ENOTTY ; }
include/uapi/linux/kvm.h +8 -0
@@ -1359,6 +1359,14 @@ struct kvm_s390_ucas_mapping {/* Memory Encryption Commands */ #define KVM_MEMORY_ENCRYPT_OP _IOWR(KVMIO, 0xba, unsigned long) +struct kvm_enc_region { +__u64 addr; +__u64 size; +}; + +#define KVM_MEMORY_ENCRYPT_REG_REGION _IOR(KVMIO, 0xbb, struct kvm_enc_region) +#define KVM_MEMORY_ENCRYPT_UNREG_REGION _IOR(KVMIO, 0xbc, struct kvm_enc_region) +#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.9.5
Re: [Part2 PATCH v6 08/38] KVM: Introduce KVM_MEMORY_ENCRYPT_{UN,}REG_REGION ioctl
Quoted 17 lines · Brijesh Singh
If hardware supports memory encryption then
KVM_MEMORY_ENCRYPT_REG_REGIONandKVM_MEMORY_ENCRYPT_UNREG_REGIONioctl's can be used by userspace to register/unregister the guest memory regions which may contain the encrypted data (e.g guest RAM, PCI BAR, SMRAM etc).Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Joerg Roedel <[email protected]>Cc Borislav Petkov <[email protected]>Cc Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>
Quoted 6 lines
--- Documentation/virtual/kvm/api.txt | 34 ++++++++++++++++++++++++++++++++++ arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/x86.c | 24 ++++++++++++++++++++++++ include/uapi/linux/kvm.h | 8 ++++++++ 4 files changed, 68 insertions(+)
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.PATCH v6 9/38 crypto: ccp: Build the AMD secure processor driver only with AMD CPU support
In reply to Brijesh SinghFrom: Borislav Petkov <[email protected]>
This is AMD-specific hardware so present it in Kconfig only when AMD CPU support is enabled or on ARM64 where it is also used.
--- drivers/crypto/ccp/Kconfig | 1 + 1 file changed, 1 insertion(+)
drivers/crypto/ccp/Kconfig +1 -0
@@ -1,5 +1,6 @@ config CRYPTO_DEV_CCP_DD tristate "Secure Processor device driver" + depends on CPU_SUP_AMD || ARM64 default m help Provides AMD Secure Processor device driver.
Signature · 1 line
2.9.5
Re: [Part2 PATCH v6 09/38] crypto: ccp: Build the AMD secure processor driver only with AMD CPU support
Quoted 5 lines · Brijesh Singh
From: Borislav Petkov <[email protected]>
This is AMD-specific hardware so present it in
Kconfigonly when AMD CPU support is enabled or on ARM64 where it is also used.
Quoted 8 lines
Signed-off-by Borislav Petkov <[email protected]>Signed-off-by Brijesh Singh <[email protected]>Cc Brijesh Singh <[email protected]>Cc Tom Lendacky <[email protected]>Cc Gary Hook <[email protected]>Cc Herbert Xu <[email protected]>Cc "David S. Miller" <[email protected]>
Quoted 16 lines
--- drivers/crypto/ccp/Kconfig | 1 + 1 file changed, 1 insertion(+)drivers/crypto/ccp/Kconfig +1
-0@@ -1,5 +1,6 @@ config CRYPTO_DEV_CCP_DD tristate "Secure Processor device driver" + depends on CPU_SUP_AMD || ARM64 default m help Provides AMD Secure Processor device driver.
PATCH v6 10/38 crypto: ccp: Define SEV userspace ioctl and command id
In reply to Brijesh SinghAdd a include file which defines the ioctl and command id used for issuing SEV platform management specific commands.
--- include/uapi/linux/psp-sev.h | 113 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 include/uapi/linux/psp-sev.h
include/uapi/linux/psp-sev.h new file +113 -0
@@ -0,0 +1,113 @@ +/* +* Userspace interface for AMD Secure Encrypted Virtualization (SEV) +* platform management commands. +* +* Copyright (C) 2016-2017 Advanced Micro Devices, Inc. +* +* Author: Brijesh Singh <[email protected]> +* +* SEV spec 0.14 is available at: +* http://support.amd.com/TechDocs/55766_SEV-KM%20API_Specification.pdf +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef __PSP_SEV_USER_H__ +#define __PSP_SEV_USER_H__ + +#include <linux/types.h> + +/** +* SEV platform commands +*/ +enum { +SEV_FACTORY_RESET =0 , +SEV_PLATFORM_STATUS , +SEV_PEK_GEN , +SEV_PEK_CSR , +SEV_PDH_GEN , +SEV_PDH_CERT_EXPORT , +SEV_PEK_CERT_IMPORT , + +SEV_MAX , +}; + +/** +* struct sev_user_data_status - PLATFORM_STATUS command parameters +* +* @major: major API version +* @minor: minor API version +* @state: platform state +* @flags: platform config flags +* @build: firmware build id for API version +* @guest_count: number of active guests +*/ +struct sev_user_data_status { +__u8 api_major;/* Out */ +__u8 api_minor;/* Out */ +__u8 state;/* Out */ +__u32 flags;/* Out */ +__u8 build;/* Out */ +__u32 guest_count;/* Out */ +} __packed; + +/** +* struct sev_user_data_pek_csr - PEK_CSR command parameters +* +* @address: PEK certificate chain +* @length: length of certificate +*/ +struct sev_user_data_pek_csr { +__u64 address;/* In */ +__u32 length;/* In/Out */ +} __packed; + +/** +* struct sev_user_data_cert_import - PEK_CERT_IMPORT command parameters +* +* @pek_address: PEK certificate chain +* @pek_len: length of PEK certificate +* @oca_address: OCA certificate chain +* @oca_len: length of OCA certificate +*/ +struct sev_user_data_pek_cert_import { +__u64 pek_cert_address;/* In */ +__u32 pek_cert_len;/* In */ +__u64 oca_cert_address;/* In */ +__u32 oca_cert_len;/* In */ +} __packed; + +/** +* struct sev_user_data_pdh_cert_export - PDH_CERT_EXPORT command parameters +* +* @pdh_address: PDH certificate address +* @pdh_len: length of PDH certificate +* @cert_chain_address: PDH certificate chain +* @cert_chain_len: length of PDH certificate chain +*/ +struct sev_user_data_pdh_cert_export { +__u64 pdh_cert_address;/* In */ +__u32 pdh_cert_len;/* In/Out */ +__u64 cert_chain_address;/* In */ +__u32 cert_chain_len;/* In/Out */ +} __packed; + +/** +* struct sev_issue_cmd - SEV ioctl parameters +* +* @cmd: SEV commands to execute +* @opaque: pointer to the command structure +* @error: SEV FW return code on failure +*/ +struct sev_issue_cmd { +__u32 cmd;/* In */ +__u64 data;/* In */ +__u32 error;/* Out */ +} __packed; + +#define SEV_IOC_TYPE 'S' +#define SEV_ISSUE_CMD _IOWR(SEV_IOC_TYPE, 0x0, struct sev_issue_cmd) + +#endif /* __PSP_USER_SEV_H */
Signature · 1 line
2.9.5
Re: [Part2 PATCH v6 10/38] crypto: ccp: Define SEV userspace ioctl and command id
Quoted 15 lines · Brijesh Singh
Add a include file which defines the ioctl and command id used for issuing SEV platform management specific commands.
Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Borislav Petkov <[email protected]>Cc Herbert Xu <[email protected]>Cc Gary Hook <[email protected]>Cc Tom Lendacky <[email protected]>Improvements-by Borislav Petkov <[email protected]>Signed-off-by Brijesh Singh <[email protected]>Reviewed-by Borislav Petkov <[email protected]>
Quoted 125 lines
--- include/uapi/linux/psp-sev.h | 113 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+)create mode 100644
include/uapi/linux/psp-sev.hinclude/uapi/linux/psp-sev.h new file +113
-0@@ -0,0 +1,113 @@ +/* +* Userspace interface for AMD Secure Encrypted Virtualization (SEV) +* platform management commands. +* +* Copyright (C) 2016-2017 Advanced Micro Devices, Inc. +* +* Author: Brijesh Singh <[email protected]> +* +* SEV spec 0.14 is available at: +* http://support.amd.com/TechDocs/55766_SEV-KM%20API_Specification.pdf +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef __PSP_SEV_USER_H__ +#define __PSP_SEV_USER_H__ + +#include <linux/types.h> + +/** +* SEV platform commands +*/ +enum { +SEV_FACTORY_RESET =0 , +SEV_PLATFORM_STATUS , +SEV_PEK_GEN , +SEV_PEK_CSR , +SEV_PDH_GEN , +SEV_PDH_CERT_EXPORT , +SEV_PEK_CERT_IMPORT , + +SEV_MAX , +}; + +/** +* struct sev_user_data_status - PLATFORM_STATUS command parameters +* +* @major: major API version +* @minor: minor API version +* @state: platform state +* @flags: platform config flags +* @build: firmware build id for API version +* @guest_count: number of active guests +*/ +struct sev_user_data_status { +__u8 api_major;/* Out */ +__u8 api_minor;/* Out */ +__u8 state;/* Out */ +__u32 flags;/* Out */ +__u8 build;/* Out */ +__u32 guest_count;/* Out */ +} __packed; + +/** +* struct sev_user_data_pek_csr - PEK_CSR command parameters +* +* @address: PEK certificate chain +* @length: length of certificate +*/ +struct sev_user_data_pek_csr { +__u64 address;/* In */ +__u32 length;/* In/Out */ +} __packed; + +/** +* struct sev_user_data_cert_import - PEK_CERT_IMPORT command parameters +* +* @pek_address: PEK certificate chain +* @pek_len: length of PEK certificate +* @oca_address: OCA certificate chain +* @oca_len: length of OCA certificate +*/ +struct sev_user_data_pek_cert_import { +__u64 pek_cert_address;/* In */ +__u32 pek_cert_len;/* In */ +__u64 oca_cert_address;/* In */ +__u32 oca_cert_len;/* In */ +} __packed; + +/** +* struct sev_user_data_pdh_cert_export - PDH_CERT_EXPORT command parameters +* +* @pdh_address: PDH certificate address +* @pdh_len: length of PDH certificate +* @cert_chain_address: PDH certificate chain +* @cert_chain_len: length of PDH certificate chain +*/ +struct sev_user_data_pdh_cert_export { +__u64 pdh_cert_address;/* In */ +__u32 pdh_cert_len;/* In/Out */ +__u64 cert_chain_address;/* In */ +__u32 cert_chain_len;/* In/Out */ +} __packed; + +/** +* struct sev_issue_cmd - SEV ioctl parameters +* +* @cmd: SEV commands to execute +* @opaque: pointer to the command structure +* @error: SEV FW return code on failure +*/ +struct sev_issue_cmd { +__u32 cmd;/* In */ +__u64 data;/* In */ +__u32 error;/* Out */ +} __packed; + +#define SEV_IOC_TYPE 'S' +#define SEV_ISSUE_CMD _IOWR(SEV_IOC_TYPE, 0x0, struct sev_issue_cmd) + +#endif /* __PSP_USER_SEV_H */
PATCH v6 11/38 crypto: ccp: Define SEV key management command id
In reply to Brijesh SinghDefine Secure Encrypted Virtualization (SEV) key management command id and structure. The command definition is available in SEV KM [1] spec 0.14.
[1] http://support.amd.com/TechDocs/55766_SEV-KM API_Specification.pdf
--- include/linux/psp-sev.h | 494 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 494 insertions(+) create mode 100644 include/linux/psp-sev.h
include/linux/psp-sev.h new file +494 -0
@@ -0,0 +1,494 @@ +/* +* AMD Secure Encrypted Virtualization (SEV) driver interface +* +* Copyright (C) 2016-2017 Advanced Micro Devices, Inc. +* +* Author: Brijesh Singh <[email protected]> +* +* SEV spec 0.14 is available at: +* http://support.amd.com/TechDocs/55766_SEV-KM API_Specification.pdf +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef __PSP_SEV_H__ +#define __PSP_SEV_H__ + +#include <uapi/linux/psp-sev.h> + +#ifdef CONFIG_X86 +#include <linux/mem_encrypt.h> + +#define __psp_pa (x) __sme_pa(x) +#else +#define __psp_pa (x) __pa(x) +#endif + +#define SEV_FW_BLOB_MAX_SIZE 0x4000/* 16KB */ + +/** +* SEV platform state +*/ +enum sev_state { +SEV_STATE_UNINIT =0x0 , +SEV_STATE_INIT =0x1 , +SEV_STATE_WORKING =0x2 , + +SEV_STATE_MAX +}; + +/** +* SEV platform and guest management commands +*/ +enum sev_cmd { +/* platform commands */ +SEV_CMD_INIT =0x001 , +SEV_CMD_SHUTDOWN =0x002 , +SEV_CMD_FACTORY_RESET =0x003 , +SEV_CMD_PLATFORM_STATUS =0x004 , +SEV_CMD_PEK_GEN =0x005 , +SEV_CMD_PEK_CSR =0x006 , +SEV_CMD_PEK_CERT_IMPORT =0x007 , +SEV_CMD_PDH_CERT_EXPORT =0x008 , +SEV_CMD_PDH_GEN =0x009 , +SEV_CMD_DF_FLUSH =0x00A , + +/* Guest commands */ +SEV_CMD_DECOMMISSION =0x020 , +SEV_CMD_ACTIVATE =0x021 , +SEV_CMD_DEACTIVATE =0x022 , +SEV_CMD_GUEST_STATUS =0x023 , + +/* Guest launch commands */ +SEV_CMD_LAUNCH_START =0x030 , +SEV_CMD_LAUNCH_UPDATE_DATA =0x031 , +SEV_CMD_LAUNCH_UPDATE_VMSA =0x032 , +SEV_CMD_LAUNCH_MEASURE =0x033 , +SEV_CMD_LAUNCH_UPDATE_SECRET =0x034 , +SEV_CMD_LAUNCH_FINISH =0x035 , + +/* Guest migration commands (outgoing) */ +SEV_CMD_SEND_START =0x040 , +SEV_CMD_SEND_UPDATE_DATA =0x041 , +SEV_CMD_SEND_UPDATE_VMSA =0x042 , +SEV_CMD_SEND_FINISH =0x043 , + +/* Guest migration commands (incoming) */ +SEV_CMD_RECEIVE_START =0x050 , +SEV_CMD_RECEIVE_UPDATE_DATA =0x051 , +SEV_CMD_RECEIVE_UPDATE_VMSA =0x052 , +SEV_CMD_RECEIVE_FINISH =0x053 , + +/* Guest debug commands */ +SEV_CMD_DBG_DECRYPT =0x060 , +SEV_CMD_DBG_ENCRYPT =0x061 , + +SEV_CMD_MAX , +}; + +/** +* status code returned by the commands +*/ +enum psp_ret_code { +SEV_RET_SUCCESS =0 , +SEV_RET_INVALID_PLATFORM_STATE , +SEV_RET_INVALID_GUEST_STATE , +SEV_RET_INAVLID_CONFIG , + SEV_RET_INVALID_len, +SEV_RET_ALREADY_OWNED , +SEV_RET_INVALID_CERTIFICATE , +SEV_RET_POLICY_FAILURE , +SEV_RET_INACTIVE , +SEV_RET_INVALID_ADDRESS , +SEV_RET_BAD_SIGNATURE , +SEV_RET_BAD_MEASUREMENT , +SEV_RET_ASID_OWNED , +SEV_RET_INVALID_ASID , +SEV_RET_WBINVD_REQUIRED , +SEV_RET_DFFLUSH_REQUIRED , +SEV_RET_INVALID_GUEST , +SEV_RET_INVALID_COMMAND , +SEV_RET_ACTIVE , +SEV_RET_HWSEV_RET_PLATFORM , +SEV_RET_HWSEV_RET_UNSAFE , +SEV_RET_UNSUPPORTED , +SEV_RET_MAX , +}; + +/** +* struct sev_data_init - INIT command parameters +* +* @flags: processing flags +* @tmr_address: system physical address used for SEV-ES +* @tmr_len: len of tmr_address +*/ +struct sev_data_init { +u32 flags;/* In */ +u32 reserved;/* In */ +u64 tmr_address;/* In */ +u32 tmr_len;/* In */ +} __packed; + +/** +* struct sev_data_pek_csr - PEK_CSR command parameters +* +* @address: PEK certificate chain +* @len: len of certificate +*/ +struct sev_data_pek_csr { +u64 address;/* In */ +u32 len;/* In/Out */ +} __packed; + +/** +* struct sev_data_cert_import - PEK_CERT_IMPORT command parameters +* +* @pek_address: PEK certificate chain +* @pek_len: len of PEK certificate +* @oca_address: OCA certificate chain +* @oca_len: len of OCA certificate +*/ +struct sev_data_pek_cert_import { +u64 pek_cert_address;/* In */ +u32 pek_cert_len;/* In */ +u32 reserved;/* In */ +u64 oca_cert_address;/* In */ +u32 oca_cert_len;/* In */ +} __packed; + +/** +* struct sev_data_pdh_cert_export - PDH_CERT_EXPORT command parameters +* +* @pdh_address: PDH certificate address +* @pdh_len: len of PDH certificate +* @cert_chain_address: PDH certificate chain +* @cert_chain_len: len of PDH certificate chain +*/ +struct sev_data_pdh_cert_export { +u64 pdh_cert_address;/* In */ +u32 pdh_cert_len;/* In/Out */ +u32 reserved;/* In */ +u64 cert_chain_address;/* In */ +u32 cert_chain_len;/* In/Out */ +} __packed; + +/** +* struct sev_data_decommission - DECOMMISSION command parameters +* +* @handle: handle of the VM to decommission +*/ +struct sev_data_decommission { +u32 handle;/* In */ +} __packed; + +/** +* struct sev_data_activate - ACTIVATE command parameters +* +* @handle: handle of the VM to activate +* @asid: asid assigned to the VM +*/ +struct sev_data_activate { +u32 handle;/* In */ +u32 asid;/* In */ +} __packed; + +/** +* struct sev_data_deactivate - DEACTIVATE command parameters +* +* @handle: handle of the VM to deactivate +*/ +struct sev_data_deactivate { +u32 handle;/* In */ +} __packed; + +/** +* struct sev_data_guest_status - SEV GUEST_STATUS command parameters +* +* @handle: handle of the VM to retrieve status +* @policy: policy information for the VM +* @asid: current ASID of the VM +* @state: current state of the VM +*/ +struct sev_data_guest_status { +u32 handle;/* In */ +u32 policy;/* Out */ +u32 asid;/* Out */ +u8 state;/* Out */ +} __packed; + +/** +* struct sev_data_launch_start - LAUNCH_START command parameters +* +* @handle: handle assigned to the VM +* @policy: guest launch policy +* @dh_cert_address: physical address of DH certificate blob +* @dh_cert_len: len of DH certificate blob +* @session_address: physical address of session parameters +* @session_len: len of session parameters +*/ +struct sev_data_launch_start { +u32 handle;/* In/Out */ +u32 policy;/* In */ +u64 dh_cert_address;/* In */ +u32 dh_cert_len;/* In */ +u32 reserved;/* In */ +u64 session_address;/* In */ +u32 session_len;/* In */ +} __packed; + +/** +* struct sev_data_launch_update_data - LAUNCH_UPDATE_DATA command parameter +* +* @handle: handle of the VM to update +* @len: len of memory to be encrypted +* @address: physical address of memory region to encrypt +*/ +struct sev_data_launch_update_data { +u32 handle;/* In */ +u32 reserved; +u64 address;/* In */ +u32 len;/* In */ +} __packed; + +/** +* struct sev_data_launch_update_vmsa - LAUNCH_UPDATE_VMSA command +* +* @handle: handle of the VM +* @address: physical address of memory region to encrypt +* @len: len of memory region to encrypt +*/ +struct sev_data_launch_update_vmsa { +u32 handle;/* In */ +u32 reserved; +u64 address;/* In */ +u32 len;/* In */ +} __packed; + +/** +* struct sev_data_launch_measure - LAUNCH_MEASURE command parameters +* +* @handle: handle of the VM to process +* @address: physical address containing the measurement blob +* @len: len of measurement blob +*/ +struct sev_data_launch_measure { +u32 handle;/* In */ +u32 reserved; +u64 address;/* In */ +u32 len;/* In/Out */ +} __packed; + +/** +* struct sev_data_launch_secret - LAUNCH_SECRET command parameters +* +* @handle: handle of the VM to process +* @hdr_address: physical address containing the packet header +* @hdr_len: len of packet header +* @guest_address: system physical address of guest memory region +* @guest_len: len of guest_paddr +* @trans_address: physical address of transport memory buffer +* @trans_len: len of transport memory buffer +*/ +struct sev_data_launch_secret { +u32 handle;/* In */ +u32 reserved1; +u64 hdr_address;/* In */ +u32 hdr_len;/* In */ +u32 reserved2; +u64 guest_address;/* In */ +u32 guest_len;/* In */ +u32 reserved3; +u64 trans_address;/* In */ +u32 trans_len;/* In */ +} __packed; + +/** +* struct sev_data_launch_finish - LAUNCH_FINISH command parameters +* +* @handle: handle of the VM to process +*/ +struct sev_data_launch_finish { +u32 handle;/* In */ +} __packed; + +/** +* struct sev_data_send_start - SEND_START command parameters +* +* @handle: handle of the VM to process +* @policy: policy information for the VM +* @pdh_cert_address: physical address containing PDH certificate +* @pdh_cert_len: len of PDH certificate +* @plat_certs_address: physical address containing platform certificate +* @plat_certs_len: len of platform certificate +* @amd_certs_address: physical address containing AMD certificate +* @amd_certs_len: len of AMD certificate +* @session_address: physical address containing Session data +* @session_len: len of session data +*/ +struct sev_data_send_start { +u32 handle;/* In */ +u32 policy;/* Out */ +u64 pdh_cert_address;/* In */ +u32 pdh_cert_len;/* In */ +u32 reserved1; +u64 plat_cert_address;/* In */ +u32 plat_cert_len;/* In */ +u32 reserved2; +u64 amd_cert_address;/* In */ +u32 amd_cert_len;/* In */ +u32 reserved3; +u64 session_address;/* In */ +u32 session_len;/* In/Out */ +} __packed; + +/** +* struct sev_data_send_update - SEND_UPDATE_DATA command +* +* @handle: handle of the VM to process +* @hdr_address: physical address containing packet header +* @hdr_len: len of packet header +* @guest_address: physical address of guest memory region to send +* @guest_len: len of guest memory region to send +* @trans_address: physical address of host memory region +* @trans_len: len of host memory region +*/ +struct sev_data_send_update_data { +u32 handle;/* In */ +u32 reserved1; +u64 hdr_address;/* In */ +u32 hdr_len;/* In/Out */ +u32 reserved2; +u64 guest_address;/* In */ +u32 guest_len;/* In */ +u32 reserved3; +u64 trans_address;/* In */ +u32 trans_len;/* In */ +} __packed; + +/** +* struct sev_data_send_update - SEND_UPDATE_VMSA command +* +* @handle: handle of the VM to process +* @hdr_address: physical address containing packet header +* @hdr_len: len of packet header +* @guest_address: physical address of guest memory region to send +* @guest_len: len of guest memory region to send +* @trans_address: physical address of host memory region +* @trans_len: len of host memory region +*/ +struct sev_data_send_update_vmsa { +u32 handle;/* In */ +u64 hdr_address;/* In */ +u32 hdr_len;/* In/Out */ +u32 reserved2; +u64 guest_address;/* In */ +u32 guest_len;/* In */ +u32 reserved3; +u64 trans_address;/* In */ +u32 trans_len;/* In */ +} __packed; + +/** +* struct sev_data_send_finish - SEND_FINISH command parameters +* +* @handle: handle of the VM to process +*/ +struct sev_data_send_finish { +u32 handle;/* In */ +} __packed; + +/** +* struct sev_data_receive_start - RECEIVE_START command parameters +* +* @handle: handle of the VM to perform receive operation +* @pdh_cert_address: system physical address containing PDH certificate blob +* @pdh_cert_len: len of PDH certificate blob +* @session_address: system physical address containing session blob +* @session_len: len of session blob +*/ +struct sev_data_receive_start { +u32 handle;/* In/Out */ +u32 policy;/* In */ +u64 pdh_cert_address;/* In */ +u32 pdh_cert_len;/* In */ +u32 reserved1; +u64 session_address;/* In */ +u32 session_len;/* In */ +} __packed; + +/** +* struct sev_data_receive_update_data - RECEIVE_UPDATE_DATA command parameters +* +* @handle: handle of the VM to update +* @hdr_address: physical address containing packet header blob +* @hdr_len: len of packet header +* @guest_address: system physical address of guest memory region +* @guest_len: len of guest memory region +* @trans_address: system physical address of transport buffer +* @trans_len: len of transport buffer +*/ +struct sev_data_receive_update_data { +u32 handle;/* In */ +u32 reserved1; +u64 hdr_address;/* In */ +u32 hdr_len;/* In */ +u32 reserved2; +u64 guest_address;/* In */ +u32 guest_len;/* In */ +u32 reserved3; +u64 trans_address;/* In */ +u32 trans_len;/* In */ +} __packed; + +/** +* struct sev_data_receive_update_vmsa - RECEIVE_UPDATE_VMSA command parameters +* +* @handle: handle of the VM to update +* @hdr_address: physical address containing packet header blob +* @hdr_len: len of packet header +* @guest_address: system physical address of guest memory region +* @guest_len: len of guest memory region +* @trans_address: system physical address of transport buffer +* @trans_len: len of transport buffer +*/ +struct sev_data_receive_update_vmsa { +u32 handle;/* In */ +u32 reserved1; +u64 hdr_address;/* In */ +u32 hdr_len;/* In */ +u32 reserved2; +u64 guest_address;/* In */ +u32 guest_len;/* In */ +u32 reserved3; +u64 trans_address;/* In */ +u32 trans_len;/* In */ +} __packed; + +/** +* struct sev_data_receive_finish - RECEIVE_FINISH command parameters +* +* @handle: handle of the VM to finish +*/ +struct sev_data_receive_finish { +u32 handle;/* In */ +} __packed; + +/** +* struct sev_data_dbg - DBG_ENCRYPT/DBG_DECRYPT command parameters +* +* @handle: handle of the VM to perform debug operation +* @src_addr: source address of data to operate on +* @dst_addr: destination address of data to operate on +* @len: len of data to operate on +*/ +struct sev_data_dbg { +u32 handle;/* In */ +u32 reserved; +u64 src_addr;/* In */ +u64 dst_addr;/* In */ +u32 len;/* In */ +} __packed; + +#endif /* __PSP_SEV_H__ */
Signature · 1 line
2.9.5
Re: [Part2 PATCH v6 11/38] crypto: ccp: Define SEV key management command id
Quoted 6 lines · Brijesh Singh
Define Secure Encrypted Virtualization (SEV) key management command id and structure. The command definition is available in SEV KM [1] spec 0.14.
[1] http://support.amd.com/TechDocs/55766_SEV-KM API_Specification.pdf
Quoted 12 lines
Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Borislav Petkov <[email protected]>Cc Herbert Xu <[email protected]>Cc Gary Hook <[email protected]>Cc Tom Lendacky <[email protected]>Improvements-by Borislav Petkov <[email protected]>Signed-off-by Brijesh Singh <[email protected]>Reviewed-by Borislav Petkov <[email protected]>
Quoted 506 lines
--- include/linux/psp-sev.h | 494 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 494 insertions(+)create mode 100644
include/linux/psp-sev.hinclude/linux/psp-sev.h new file +494
-0@@ -0,0 +1,494 @@ +/* +* AMD Secure Encrypted Virtualization (SEV) driver interface +* +* Copyright (C) 2016-2017 Advanced Micro Devices, Inc. +* +* Author: Brijesh Singh <[email protected]> +* +* SEV spec 0.14 is available at: +* http://support.amd.com/TechDocs/55766_SEV-KM API_Specification.pdf +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef __PSP_SEV_H__ +#define __PSP_SEV_H__ + +#include <uapi/linux/psp-sev.h> + +#ifdef CONFIG_X86 +#include <linux/mem_encrypt.h> + +#define __psp_pa (x) __sme_pa(x) +#else +#define __psp_pa (x) __pa(x) +#endif + +#define SEV_FW_BLOB_MAX_SIZE 0x4000/* 16KB */ + +/** +* SEV platform state +*/ +enum sev_state { +SEV_STATE_UNINIT =0x0 , +SEV_STATE_INIT =0x1 , +SEV_STATE_WORKING =0x2 , + +SEV_STATE_MAX +}; + +/** +* SEV platform and guest management commands +*/ +enum sev_cmd { +/* platform commands */ +SEV_CMD_INIT =0x001 , +SEV_CMD_SHUTDOWN =0x002 , +SEV_CMD_FACTORY_RESET =0x003 , +SEV_CMD_PLATFORM_STATUS =0x004 , +SEV_CMD_PEK_GEN =0x005 , +SEV_CMD_PEK_CSR =0x006 , +SEV_CMD_PEK_CERT_IMPORT =0x007 , +SEV_CMD_PDH_CERT_EXPORT =0x008 , +SEV_CMD_PDH_GEN =0x009 , +SEV_CMD_DF_FLUSH =0x00A , + +/* Guest commands */ +SEV_CMD_DECOMMISSION =0x020 , +SEV_CMD_ACTIVATE =0x021 , +SEV_CMD_DEACTIVATE =0x022 , +SEV_CMD_GUEST_STATUS =0x023 , + +/* Guest launch commands */ +SEV_CMD_LAUNCH_START =0x030 , +SEV_CMD_LAUNCH_UPDATE_DATA =0x031 , +SEV_CMD_LAUNCH_UPDATE_VMSA =0x032 , +SEV_CMD_LAUNCH_MEASURE =0x033 , +SEV_CMD_LAUNCH_UPDATE_SECRET =0x034 , +SEV_CMD_LAUNCH_FINISH =0x035 , + +/* Guest migration commands (outgoing) */ +SEV_CMD_SEND_START =0x040 , +SEV_CMD_SEND_UPDATE_DATA =0x041 , +SEV_CMD_SEND_UPDATE_VMSA =0x042 , +SEV_CMD_SEND_FINISH =0x043 , + +/* Guest migration commands (incoming) */ +SEV_CMD_RECEIVE_START =0x050 , +SEV_CMD_RECEIVE_UPDATE_DATA =0x051 , +SEV_CMD_RECEIVE_UPDATE_VMSA =0x052 , +SEV_CMD_RECEIVE_FINISH =0x053 , + +/* Guest debug commands */ +SEV_CMD_DBG_DECRYPT =0x060 , +SEV_CMD_DBG_ENCRYPT =0x061 , + +SEV_CMD_MAX , +}; + +/** +* status code returned by the commands +*/ +enum psp_ret_code { +SEV_RET_SUCCESS =0 , +SEV_RET_INVALID_PLATFORM_STATE , +SEV_RET_INVALID_GUEST_STATE , +SEV_RET_INAVLID_CONFIG , + SEV_RET_INVALID_len, +SEV_RET_ALREADY_OWNED , +SEV_RET_INVALID_CERTIFICATE , +SEV_RET_POLICY_FAILURE , +SEV_RET_INACTIVE , +SEV_RET_INVALID_ADDRESS , +SEV_RET_BAD_SIGNATURE , +SEV_RET_BAD_MEASUREMENT , +SEV_RET_ASID_OWNED , +SEV_RET_INVALID_ASID , +SEV_RET_WBINVD_REQUIRED , +SEV_RET_DFFLUSH_REQUIRED , +SEV_RET_INVALID_GUEST , +SEV_RET_INVALID_COMMAND , +SEV_RET_ACTIVE , +SEV_RET_HWSEV_RET_PLATFORM , +SEV_RET_HWSEV_RET_UNSAFE , +SEV_RET_UNSUPPORTED , +SEV_RET_MAX , +}; + +/** +* struct sev_data_init - INIT command parameters +* +* @flags: processing flags +* @tmr_address: system physical address used for SEV-ES +* @tmr_len: len of tmr_address +*/ +struct sev_data_init { +u32 flags;/* In */ +u32 reserved;/* In */ +u64 tmr_address;/* In */ +u32 tmr_len;/* In */ +} __packed; + +/** +* struct sev_data_pek_csr - PEK_CSR command parameters +* +* @address: PEK certificate chain +* @len: len of certificate +*/ +struct sev_data_pek_csr { +u64 address;/* In */ +u32 len;/* In/Out */ +} __packed; + +/** +* struct sev_data_cert_import - PEK_CERT_IMPORT command parameters +* +* @pek_address: PEK certificate chain +* @pek_len: len of PEK certificate +* @oca_address: OCA certificate chain +* @oca_len: len of OCA certificate +*/ +struct sev_data_pek_cert_import { +u64 pek_cert_address;/* In */ +u32 pek_cert_len;/* In */ +u32 reserved;/* In */ +u64 oca_cert_address;/* In */ +u32 oca_cert_len;/* In */ +} __packed; + +/** +* struct sev_data_pdh_cert_export - PDH_CERT_EXPORT command parameters +* +* @pdh_address: PDH certificate address +* @pdh_len: len of PDH certificate +* @cert_chain_address: PDH certificate chain +* @cert_chain_len: len of PDH certificate chain +*/ +struct sev_data_pdh_cert_export { +u64 pdh_cert_address;/* In */ +u32 pdh_cert_len;/* In/Out */ +u32 reserved;/* In */ +u64 cert_chain_address;/* In */ +u32 cert_chain_len;/* In/Out */ +} __packed; + +/** +* struct sev_data_decommission - DECOMMISSION command parameters +* +* @handle: handle of the VM to decommission +*/ +struct sev_data_decommission { +u32 handle;/* In */ +} __packed; + +/** +* struct sev_data_activate - ACTIVATE command parameters +* +* @handle: handle of the VM to activate +* @asid: asid assigned to the VM +*/ +struct sev_data_activate { +u32 handle;/* In */ +u32 asid;/* In */ +} __packed; + +/** +* struct sev_data_deactivate - DEACTIVATE command parameters +* +* @handle: handle of the VM to deactivate +*/ +struct sev_data_deactivate { +u32 handle;/* In */ +} __packed; + +/** +* struct sev_data_guest_status - SEV GUEST_STATUS command parameters +* +* @handle: handle of the VM to retrieve status +* @policy: policy information for the VM +* @asid: current ASID of the VM +* @state: current state of the VM +*/ +struct sev_data_guest_status { +u32 handle;/* In */ +u32 policy;/* Out */ +u32 asid;/* Out */ +u8 state;/* Out */ +} __packed; + +/** +* struct sev_data_launch_start - LAUNCH_START command parameters +* +* @handle: handle assigned to the VM +* @policy: guest launch policy +* @dh_cert_address: physical address of DH certificate blob +* @dh_cert_len: len of DH certificate blob +* @session_address: physical address of session parameters +* @session_len: len of session parameters +*/ +struct sev_data_launch_start { +u32 handle;/* In/Out */ +u32 policy;/* In */ +u64 dh_cert_address;/* In */ +u32 dh_cert_len;/* In */ +u32 reserved;/* In */ +u64 session_address;/* In */ +u32 session_len;/* In */ +} __packed; + +/** +* struct sev_data_launch_update_data - LAUNCH_UPDATE_DATA command parameter +* +* @handle: handle of the VM to update +* @len: len of memory to be encrypted +* @address: physical address of memory region to encrypt +*/ +struct sev_data_launch_update_data { +u32 handle;/* In */ +u32 reserved; +u64 address;/* In */ +u32 len;/* In */ +} __packed; + +/** +* struct sev_data_launch_update_vmsa - LAUNCH_UPDATE_VMSA command +* +* @handle: handle of the VM +* @address: physical address of memory region to encrypt +* @len: len of memory region to encrypt +*/ +struct sev_data_launch_update_vmsa { +u32 handle;/* In */ +u32 reserved; +u64 address;/* In */ +u32 len;/* In */ +} __packed; + +/** +* struct sev_data_launch_measure - LAUNCH_MEASURE command parameters +* +* @handle: handle of the VM to process +* @address: physical address containing the measurement blob +* @len: len of measurement blob +*/ +struct sev_data_launch_measure { +u32 handle;/* In */ +u32 reserved; +u64 address;/* In */ +u32 len;/* In/Out */ +} __packed; + +/** +* struct sev_data_launch_secret - LAUNCH_SECRET command parameters +* +* @handle: handle of the VM to process +* @hdr_address: physical address containing the packet header +* @hdr_len: len of packet header +* @guest_address: system physical address of guest memory region +* @guest_len: len of guest_paddr +* @trans_address: physical address of transport memory buffer +* @trans_len: len of transport memory buffer +*/ +struct sev_data_launch_secret { +u32 handle;/* In */ +u32 reserved1; +u64 hdr_address;/* In */ +u32 hdr_len;/* In */ +u32 reserved2; +u64 guest_address;/* In */ +u32 guest_len;/* In */ +u32 reserved3; +u64 trans_address;/* In */ +u32 trans_len;/* In */ +} __packed; + +/** +* struct sev_data_launch_finish - LAUNCH_FINISH command parameters +* +* @handle: handle of the VM to process +*/ +struct sev_data_launch_finish { +u32 handle;/* In */ +} __packed; + +/** +* struct sev_data_send_start - SEND_START command parameters +* +* @handle: handle of the VM to process +* @policy: policy information for the VM +* @pdh_cert_address: physical address containing PDH certificate +* @pdh_cert_len: len of PDH certificate +* @plat_certs_address: physical address containing platform certificate +* @plat_certs_len: len of platform certificate +* @amd_certs_address: physical address containing AMD certificate +* @amd_certs_len: len of AMD certificate +* @session_address: physical address containing Session data +* @session_len: len of session data +*/ +struct sev_data_send_start { +u32 handle;/* In */ +u32 policy;/* Out */ +u64 pdh_cert_address;/* In */ +u32 pdh_cert_len;/* In */ +u32 reserved1; +u64 plat_cert_address;/* In */ +u32 plat_cert_len;/* In */ +u32 reserved2; +u64 amd_cert_address;/* In */ +u32 amd_cert_len;/* In */ +u32 reserved3; +u64 session_address;/* In */ +u32 session_len;/* In/Out */ +} __packed; + +/** +* struct sev_data_send_update - SEND_UPDATE_DATA command +* +* @handle: handle of the VM to process +* @hdr_address: physical address containing packet header +* @hdr_len: len of packet header +* @guest_address: physical address of guest memory region to send +* @guest_len: len of guest memory region to send +* @trans_address: physical address of host memory region +* @trans_len: len of host memory region +*/ +struct sev_data_send_update_data { +u32 handle;/* In */ +u32 reserved1; +u64 hdr_address;/* In */ +u32 hdr_len;/* In/Out */ +u32 reserved2; +u64 guest_address;/* In */ +u32 guest_len;/* In */ +u32 reserved3; +u64 trans_address;/* In */ +u32 trans_len;/* In */ +} __packed; + +/** +* struct sev_data_send_update - SEND_UPDATE_VMSA command +* +* @handle: handle of the VM to process +* @hdr_address: physical address containing packet header +* @hdr_len: len of packet header +* @guest_address: physical address of guest memory region to send +* @guest_len: len of guest memory region to send +* @trans_address: physical address of host memory region +* @trans_len: len of host memory region +*/ +struct sev_data_send_update_vmsa { +u32 handle;/* In */ +u64 hdr_address;/* In */ +u32 hdr_len;/* In/Out */ +u32 reserved2; +u64 guest_address;/* In */ +u32 guest_len;/* In */ +u32 reserved3; +u64 trans_address;/* In */ +u32 trans_len;/* In */ +} __packed; + +/** +* struct sev_data_send_finish - SEND_FINISH command parameters +* +* @handle: handle of the VM to process +*/ +struct sev_data_send_finish { +u32 handle;/* In */ +} __packed; + +/** +* struct sev_data_receive_start - RECEIVE_START command parameters +* +* @handle: handle of the VM to perform receive operation +* @pdh_cert_address: system physical address containing PDH certificate blob +* @pdh_cert_len: len of PDH certificate blob +* @session_address: system physical address containing session blob +* @session_len: len of session blob +*/ +struct sev_data_receive_start { +u32 handle;/* In/Out */ +u32 policy;/* In */ +u64 pdh_cert_address;/* In */ +u32 pdh_cert_len;/* In */ +u32 reserved1; +u64 session_address;/* In */ +u32 session_len;/* In */ +} __packed; + +/** +* struct sev_data_receive_update_data - RECEIVE_UPDATE_DATA command parameters +* +* @handle: handle of the VM to update +* @hdr_address: physical address containing packet header blob +* @hdr_len: len of packet header +* @guest_address: system physical address of guest memory region +* @guest_len: len of guest memory region +* @trans_address: system physical address of transport buffer +* @trans_len: len of transport buffer +*/ +struct sev_data_receive_update_data { +u32 handle;/* In */ +u32 reserved1; +u64 hdr_address;/* In */ +u32 hdr_len;/* In */ +u32 reserved2; +u64 guest_address;/* In */ +u32 guest_len;/* In */ +u32 reserved3; +u64 trans_address;/* In */ +u32 trans_len;/* In */ +} __packed; + +/** +* struct sev_data_receive_update_vmsa - RECEIVE_UPDATE_VMSA command parameters +* +* @handle: handle of the VM to update +* @hdr_address: physical address containing packet header blob +* @hdr_len: len of packet header +* @guest_address: system physical address of guest memory region +* @guest_len: len of guest memory region +* @trans_address: system physical address of transport buffer +* @trans_len: len of transport buffer +*/ +struct sev_data_receive_update_vmsa { +u32 handle;/* In */ +u32 reserved1; +u64 hdr_address;/* In */ +u32 hdr_len;/* In */ +u32 reserved2; +u64 guest_address;/* In */ +u32 guest_len;/* In */ +u32 reserved3; +u64 trans_address;/* In */ +u32 trans_len;/* In */ +} __packed; + +/** +* struct sev_data_receive_finish - RECEIVE_FINISH command parameters +* +* @handle: handle of the VM to finish +*/ +struct sev_data_receive_finish { +u32 handle;/* In */ +} __packed; + +/** +* struct sev_data_dbg - DBG_ENCRYPT/DBG_DECRYPT command parameters +* +* @handle: handle of the VM to perform debug operation +* @src_addr: source address of data to operate on +* @dst_addr: destination address of data to operate on +* @len: len of data to operate on +*/ +struct sev_data_dbg { +u32 handle;/* In */ +u32 reserved; +u64 src_addr;/* In */ +u64 dst_addr;/* In */ +u32 len;/* In */ +} __packed; + +#endif /* __PSP_SEV_H__ */
PATCH v6 12/38 crypto: ccp: Add Platform Security Processor (PSP) device support
In reply to Brijesh SinghThe Platform Security Processor (PSP) is part of the AMD Secure Processor (AMD-SP) functionality. The PSP is a dedicated processor that provides support for key management commands in Secure Encrypted Virtualization (SEV) mode, along with software-based Trusted Execution Environment (TEE) to enable third-party trusted applications.
Note that the key management functionality provided by the SEV firmware can be used outside of the kvm-amd driver hence it doesn't need to depend on CONFIG_KVM_AMD.
--- drivers/crypto/ccp/Kconfig | 11 +++++ drivers/crypto/ccp/Makefile | 1 + drivers/crypto/ccp/psp-dev.c | 105 +++++++++++++++++++++++++++++++++++++++++++ drivers/crypto/ccp/psp-dev.h | 59 ++++++++++++++++++++++++ drivers/crypto/ccp/sp-dev.c | 26 +++++++++++ drivers/crypto/ccp/sp-dev.h | 24 +++++++++- drivers/crypto/ccp/sp-pci.c | 52 +++++++++++++++++++++ 7 files changed, 277 insertions(+), 1 deletion(-) create mode 100644 drivers/crypto/ccp/psp-dev.c create mode 100644 drivers/crypto/ccp/psp-dev.h
drivers/crypto/ccp/Kconfig +11 -0
@@ -33,3 +33,14 @@ config CRYPTO_DEV_CCP_CRYPTO Support for using the cryptographic API with the AMD Cryptographic Coprocessor. This module supports offload of SHA and AES algorithms. If you choose 'M' here, this module will be called ccp_crypto. + +config CRYPTO_DEV_SP_PSP + bool "Platform Security Processor (PSP) device" + default y + depends on CRYPTO_DEV_CCP_DD && X86_64 + help + Provide support for the AMD Platform Security Processor (PSP). + The PSP is a dedicated processor that provides support for key + management commands in Secure Encrypted Virtualization (SEV) mode, + along with software-based Trusted Execution Environment (TEE) to + enable third-party trusted applications.
drivers/crypto/ccp/Makefile +1 -0
@@ -7,6 +7,7 @@ ccp-$(CONFIG_CRYPTO_DEV_SP_CCP) += ccp-dev.o \ ccp-dmaengine.o \ ccp-debugfs.o ccp-$(CONFIG_PCI) += sp-pci.o +ccp-$(CONFIG_CRYPTO_DEV_SP_PSP) += psp-dev.o obj-$(CONFIG_CRYPTO_DEV_CCP_CRYPTO) += ccp-crypto.o ccp-crypto-objs := ccp-crypto-main.o \
drivers/crypto/ccp/psp-dev.c new file +105 -0
105 lines not on this page · raw message
drivers/crypto/ccp/psp-dev.h new file +59 -0
59 lines not on this page · raw message
drivers/crypto/ccp/sp-dev.c +26 -0
@@ -198,6 +198,8 @@ int sp_init(struct sp_device *sp)if (sp->dev_vdata->ccp_vdata)ccp_dev_init (sp); +if (sp->dev_vdata->psp_vdata) +psp_dev_init (sp);return 0 ; }
@@ -206,6 +208,9 @@ void sp_destroy(struct sp_device *sp)if (sp->dev_vdata->ccp_vdata)ccp_dev_destroy (sp); +if (sp->dev_vdata->psp_vdata) +psp_dev_destroy (sp); +sp_del_device (sp); }
@@ -237,6 +242,27 @@ int sp_resume(struct sp_device *sp) }#endif +struct sp_device *sp_get_psp_master_device (void ) +{ +struct sp_device *i, *ret =NULL ; +unsigned long flags; + +write_lock_irqsave (&sp_unit_lock, flags); +if (list_empty (&sp_units)) + goto unlock; + +list_for_each_entry (i, &sp_units, entry) { +if (i->psp_data) +break ; + } + +if (i->get_psp_master_device) + ret = i->get_psp_master_device (); +unlock: +write_unlock_irqrestore (&sp_unit_lock, flags); +return ret; +} +static int __init sp_mod_init (void ) {#ifdef CONFIG_X86
drivers/crypto/ccp/sp-dev.h +23 -1
@@ -42,12 +42,17 @@ struct ccp_vdata {const unsigned int offset;const unsigned int rsamax; }; + +struct psp_vdata { +const unsigned int offset; +}; +/* Structure to hold SP device data */ struct sp_dev_vdata {const unsigned int bar;const struct ccp_vdata *ccp_vdata; -void *psp_vdata; +const struct psp_vdata *psp_vdata; };struct sp_device {
@@ -68,6 +73,10 @@ struct sp_device {/* DMA caching attribute support */ unsigned int axcache; +/* get and set master device */ +struct sp_device *(*get_psp_master_device)(void ); +void (*set_psp_master_device)(struct sp_device *); +bool irq_registered;bool use_tasklet;
@@ -103,6 +112,7 @@ void sp_free_ccp_irq(struct sp_device *sp, void *data);int sp_request_psp_irq (struct sp_device *sp,irq_handler_t handler,const char *name,void *data);void sp_free_psp_irq (struct sp_device *sp,void *data); +struct sp_device *sp_get_psp_master_device (void );#ifdef CONFIG_CRYPTO_DEV_SP_CCP
@@ -130,4 +140,16 @@ static inline int ccp_dev_resume(struct sp_device *sp) }#endif /* CONFIG_CRYPTO_DEV_SP_CCP */ +#ifdef CONFIG_CRYPTO_DEV_SP_PSP + +int psp_dev_init (struct sp_device *sp); +void psp_dev_destroy (struct sp_device *sp); + +#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ + +static inline int psp_dev_init (struct sp_device *sp) {return 0 ; } +static inline void psp_dev_destroy (struct sp_device *sp) { } + +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ +#endif
drivers/crypto/ccp/sp-pci.c +52 -0
88 lines not on this page · raw message
Signature · 1 line
2.9.5
Re: [Part2 PATCH v6 12/38] crypto: ccp: Add Platform Security Processor (PSP) device support
Quoted 10 lines · Brijesh Singh
The Platform Security Processor (PSP) is part of the AMD Secure Processor (AMD-SP) functionality. The PSP is a dedicated processor that provides support for key management commands in Secure Encrypted Virtualization (SEV) mode, along with software-based Trusted Execution Environment (TEE) to enable third-party trusted applications.
Note that the key management functionality provided by the SEV firmware can be used outside of the kvm-amd driver hence it doesn't need to depend on
CONFIG_KVM_AMD.
Quoted 12 lines
Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Borislav Petkov <[email protected]>Cc Herbert Xu <[email protected]>Cc Gary Hook <[email protected]>Cc Tom Lendacky <[email protected]>Improvements-by Borislav Petkov <[email protected]>Signed-off-by Brijesh Singh <[email protected]>Reviewed-by Borislav Petkov <[email protected]>
Quoted 428 lines
--- drivers/crypto/ccp/Kconfig | 11 +++++ drivers/crypto/ccp/Makefile | 1 + drivers/crypto/ccp/psp-dev.c | 105 +++++++++++++++++++++++++++++++++++++++++++ drivers/crypto/ccp/psp-dev.h | 59 ++++++++++++++++++++++++ drivers/crypto/ccp/sp-dev.c | 26 +++++++++++ drivers/crypto/ccp/sp-dev.h | 24 +++++++++- drivers/crypto/ccp/sp-pci.c | 52 +++++++++++++++++++++ 7 files changed, 277 insertions(+), 1 deletion(-)create mode 100644
drivers/crypto/ccp/psp-dev.c
create mode 100644drivers/crypto/ccp/psp-dev.hdrivers/crypto/ccp/Kconfig +11
-0@@ -33,3 +33,14 @@ config CRYPTO_DEV_CCP_CRYPTO Support for using the cryptographic API with the AMD Cryptographic Coprocessor. This module supports offload of SHA and AES algorithms. If you choose 'M' here, this module will be called ccp_crypto. + +config CRYPTO_DEV_SP_PSP + bool "Platform Security Processor (PSP) device" + default y + depends on CRYPTO_DEV_CCP_DD && X86_64 + help + Provide support for the AMD Platform Security Processor (PSP). + The PSP is a dedicated processor that provides support for key + management commands in Secure Encrypted Virtualization (SEV) mode, + along with software-based Trusted Execution Environment (TEE) to + enable third-party trusted applications.drivers/crypto/ccp/Makefile +1
-0@@ -7,6 +7,7 @@ ccp-$(CONFIG_CRYPTO_DEV_SP_CCP) += ccp-dev.o \ ccp-dmaengine.o \ ccp-debugfs.o ccp-$(CONFIG_PCI) += sp-pci.o +ccp-$(CONFIG_CRYPTO_DEV_SP_PSP) += psp-dev.o obj-$(CONFIG_CRYPTO_DEV_CCP_CRYPTO) += ccp-crypto.o ccp-crypto-objs := ccp-crypto-main.o \drivers/crypto/ccp/psp-dev.c new file +105
-0@@ -0,0 +1,105 @@ +/* +* AMD Platform Security Processor (PSP) interface +* +* Copyright (C) 2016-2017 Advanced Micro Devices, Inc. +* +* Author: Brijesh Singh <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/kthread.h> +#include <linux/sched.h> +#include <linux/interrupt.h> +#include <linux/spinlock.h> +#include <linux/spinlock_types.h> +#include <linux/types.h> +#include <linux/mutex.h> +#include <linux/delay.h> +#include <linux/hw_random.h> +#include <linux/ccp.h> + +#include "sp-dev.h" +#include "psp-dev.h" + +static struct psp_device *psp_alloc_struct (struct sp_device *sp) +{ +struct device *dev = sp->dev; +struct psp_device *psp; + + psp =devm_kzalloc (dev,sizeof (*psp),GFP_KERNEL ); +if (!psp) +return NULL ; + + psp->dev = dev; + psp->sp = sp; + +snprintf (psp->name,sizeof (psp->name),"psp-%u" , sp->ord); + +return psp; +} + +static irqreturn_t psp_irq_handler (int irq,void *data) +{ +return IRQ_HANDLED ; +} + +int psp_dev_init (struct sp_device *sp) +{ +struct device *dev = sp->dev; +struct psp_device *psp; +int ret; + + ret = -ENOMEM ; + psp =psp_alloc_struct (sp); +if (!psp) + goto e_err; + + sp->psp_data = psp; + + psp->vdata = (struct psp_vdata *)sp->dev_vdata->psp_vdata; +if (!psp->vdata) { + ret = -ENODEV ; +dev_err (dev,"missing driver data\n" ); + goto e_err; + } + + psp->io_regs = sp->io_map + psp->vdata->offset; + +/* Disable and clear interrupts until ready */ +iowrite32 (0 , psp->io_regs +PSP_P2CMSG_INTEN ); +iowrite32 (-1 , psp->io_regs +PSP_P2CMSG_INTSTS ); + +/* Request an irq */ + ret =sp_request_psp_irq (psp->sp, psp_irq_handler, psp->name, psp); +if (ret) { +dev_err (dev,"psp: unable to allocate an IRQ\n" ); + goto e_err; + } + +if (sp->set_psp_master_device) + sp->set_psp_master_device (sp); + +/* Enable interrupt */ +iowrite32 (-1 , psp->io_regs +PSP_P2CMSG_INTEN ); + +return 0 ; + +e_err: + sp->psp_data =NULL ; + +dev_notice (dev,"psp initialization failed\n" ); + +return ret; +} + +void psp_dev_destroy (struct sp_device *sp) +{ +struct psp_device *psp = sp->psp_data; + +sp_free_psp_irq (sp, psp); +}drivers/crypto/ccp/psp-dev.h new file +59
-0@@ -0,0 +1,59 @@ +/* +* AMD Platform Security Processor (PSP) interface driver +* +* Copyright (C) 2017 Advanced Micro Devices, Inc. +* +* Author: Brijesh Singh <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef __PSP_DEV_H__ +#define __PSP_DEV_H__ + +#include <linux/device.h> +#include <linux/pci.h> +#include <linux/spinlock.h> +#include <linux/mutex.h> +#include <linux/list.h> +#include <linux/wait.h> +#include <linux/dmapool.h> +#include <linux/hw_random.h> +#include <linux/bitops.h> +#include <linux/interrupt.h> +#include <linux/irqreturn.h> +#include <linux/dmaengine.h> + +#include "sp-dev.h" + +#define PSP_P2CMSG_INTEN 0x0110 +#define PSP_P2CMSG_INTSTS 0x0114 + +#define PSP_C2PMSG_ATTR_0 0x0118 +#define PSP_C2PMSG_ATTR_1 0x011c +#define PSP_C2PMSG_ATTR_2 0x0120 +#define PSP_C2PMSG_ATTR_3 0x0124 +#define PSP_P2CMSG_ATTR_0 0x0128 + +#define PSP_CMDRESP_CMD_SHIFT 16 +#define PSP_CMDRESP_IOC BIT(0) +#define PSP_CMDRESP_RESP BIT(31) +#define PSP_CMDRESP_ERR_MASK 0xffff + +#define MAX_PSP_NAME_LEN 16 + +struct psp_device { +struct list_head entry; + +struct psp_vdata *vdata; +char name[MAX_PSP_NAME_LEN ]; + +struct device *dev; +struct sp_device *sp; + +void __iomem *io_regs; +}; + +#endif /* __PSP_DEV_H */ drivers/crypto/ccp/sp-dev.c +26
-0@@ -198,6 +198,8 @@ int sp_init(struct sp_device *sp)if (sp->dev_vdata->ccp_vdata)ccp_dev_init (sp); +if (sp->dev_vdata->psp_vdata) +psp_dev_init (sp);return 0 ; }@@ -206,6 +208,9 @@ void sp_destroy(struct sp_device *sp)if (sp->dev_vdata->ccp_vdata)ccp_dev_destroy (sp); +if (sp->dev_vdata->psp_vdata) +psp_dev_destroy (sp); +sp_del_device (sp); }@@ -237,6 +242,27 @@ int sp_resume(struct sp_device *sp) }#endif +struct sp_device *sp_get_psp_master_device (void ) +{ +struct sp_device *i, *ret =NULL ; +unsigned long flags; + +write_lock_irqsave (&sp_unit_lock, flags); +if (list_empty (&sp_units)) + goto unlock; + +list_for_each_entry (i, &sp_units, entry) { +if (i->psp_data) +break ; + } + +if (i->get_psp_master_device) + ret = i->get_psp_master_device (); +unlock: +write_unlock_irqrestore (&sp_unit_lock, flags); +return ret; +} +static int __init sp_mod_init (void ) {#ifdef CONFIG_X86 drivers/crypto/ccp/sp-dev.h +23
-1@@ -42,12 +42,17 @@ struct ccp_vdata {const unsigned int offset;const unsigned int rsamax; }; + +struct psp_vdata { +const unsigned int offset; +}; +/* Structure to hold SP device data */ struct sp_dev_vdata {const unsigned int bar;const struct ccp_vdata *ccp_vdata; -void *psp_vdata; +const struct psp_vdata *psp_vdata; };struct sp_device {@@ -68,6 +73,10 @@ struct sp_device {/* DMA caching attribute support */ unsigned int axcache; +/* get and set master device */ +struct sp_device *(*get_psp_master_device)(void ); +void (*set_psp_master_device)(struct sp_device *); +bool irq_registered;bool use_tasklet;@@ -103,6 +112,7 @@ void sp_free_ccp_irq(struct sp_device *sp, void *data);int sp_request_psp_irq (struct sp_device *sp,irq_handler_t handler,const char *name,void *data);void sp_free_psp_irq (struct sp_device *sp,void *data); +struct sp_device *sp_get_psp_master_device (void );#ifdef CONFIG_CRYPTO_DEV_SP_CCP @@ -130,4 +140,16 @@ static inline int ccp_dev_resume(struct sp_device *sp) }#endif /* CONFIG_CRYPTO_DEV_SP_CCP */ +#ifdef CONFIG_CRYPTO_DEV_SP_PSP + +int psp_dev_init (struct sp_device *sp); +void psp_dev_destroy (struct sp_device *sp); + +#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ + +static inline int psp_dev_init (struct sp_device *sp) {return 0 ; } +static inline void psp_dev_destroy (struct sp_device *sp) { } + +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ +#endif drivers/crypto/ccp/sp-pci.c +52
-0@@ -25,6 +25,7 @@#include <linux/ccp.h> #include "ccp-dev.h" +#include "psp-dev.h" #define MSIX_VECTORS 2@@ -32,6 +33,7 @@ struct sp_pci {int msix_count;struct msix_entry msix_entry[MSIX_VECTORS ]; }; +static struct sp_device *sp_dev_master;static int sp_get_msix_irqs (struct sp_device *sp) {@@ -108,6 +110,45 @@ static void sp_free_irqs(struct sp_device *sp) sp->psp_irq =0 ; } +static bool sp_pci_is_master (struct sp_device *sp) +{ +struct device *dev_cur, *dev_new; +struct pci_dev *pdev_cur, *pdev_new; + + dev_new = sp->dev; + dev_cur = sp_dev_master->dev; + + pdev_new =to_pci_dev (dev_new); + pdev_cur =to_pci_dev (dev_cur); + +if (pdev_new->bus->number < pdev_cur->bus->number) +return true; + +if (PCI_SLOT (pdev_new->devfn) <PCI_SLOT (pdev_cur->devfn)) +return true; + +if (PCI_FUNC (pdev_new->devfn) <PCI_FUNC (pdev_cur->devfn)) +return true; + +return false; +} + +static void psp_set_master (struct sp_device *sp) +{ +if (!sp_dev_master) { + sp_dev_master = sp; +return ; + } + +if (sp_pci_is_master (sp)) + sp_dev_master = sp; +} + +static struct sp_device *psp_get_master (void ) +{ +return sp_dev_master; +} +static int sp_pci_probe (struct pci_dev *pdev,const struct pci_device_id *id) {struct sp_device *sp;@@ -166,6 +207,8 @@ static int sp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) goto e_err;pci_set_master (pdev); + sp->set_psp_master_device = psp_set_master; + sp->get_psp_master_device = psp_get_master; ret =dma_set_mask_and_coherent (dev,DMA_BIT_MASK (48 ));if (ret) {@@ -225,6 +268,12 @@ static int sp_pci_resume(struct pci_dev *pdev) }#endif +#ifdef CONFIG_CRYPTO_DEV_SP_PSP +static const struct psp_vdata psp_entry = { + .offset =0x10500 , +}; +#endif +static const struct sp_dev_vdata dev_vdata[] = { { .bar =2 ,@@ -237,6 +286,9 @@ static const struct sp_dev_vdata dev_vdata[] = {#ifdef CONFIG_CRYPTO_DEV_SP_CCP .ccp_vdata = &ccpv5a,#endif +#ifdef CONFIG_CRYPTO_DEV_SP_PSP + .psp_vdata = &psp_entry +#endif }, { .bar = 2,
PATCH v6 13/38 crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
In reply to Brijesh SinghAMD's new Secure Encrypted Virtualization (SEV) feature allows the memory contents of virtual machines to be transparently encrypted with a key unique to the VM. The programming and management of the encryption keys are handled by the AMD Secure Processor (AMD-SP) which exposes the commands for these tasks. The complete spec is available at:
http://support.amd.com/TechDocs/55766_SEV-KM%20API_Specification.pdf
Extend the AMD-SP driver to provide the following support:
- an in-kernel API to communicate with the SEV firmware. The API can be used by the hypervisor to create encryption context for a SEV guest.
- a userspace IOCTL to manage the platform certificates.
--- drivers/crypto/ccp/psp-dev.c | 306 +++++++++++++++++++++++++++++++++++++++++++ drivers/crypto/ccp/psp-dev.h | 21 +++ include/linux/psp-sev.h | 159 ++++++++++++++++++++++ 3 files changed, 486 insertions(+)
drivers/crypto/ccp/psp-dev.c +306 -0
@@ -26,6 +26,14 @@#include "sp-dev.h" #include "psp-dev.h" +#define DEVICE_NAME "sev" + +static DEFINE_MUTEX (sev_cmd_mutex); +static DEFINE_MUTEX (fw_init_mutex); + +static struct sev_misc_dev *sev_misc_dev; +static int fw_init_count; +static struct psp_device *psp_alloc_struct (struct sp_device *sp) {struct device *dev = sp->dev;
@@ -45,9 +53,298 @@ static struct psp_device *psp_alloc_struct(struct sp_device *sp)static irqreturn_t psp_irq_handler (int irq,void *data) { +struct psp_device *psp = data; +unsigned int status; +int reg; + +/* Read the interrupt status: */ + status =ioread32 (psp->io_regs +PSP_P2CMSG_INTSTS ); + +/* Check if it is command completion: */ +if (!(status &BIT (PSP_CMD_COMPLETE_REG ))) + goto done; + +/* Check if it is SEV command completion: */ + reg =ioread32 (psp->io_regs +PSP_CMDRESP ); +if (reg &PSP_CMDRESP_RESP ) { + psp->sev_int_rcvd =1 ; +wake_up (&psp->sev_int_queue); + } + +done: +/* Clear the interrupt status by writing the same value we read. */ +iowrite32 (status, psp->io_regs +PSP_P2CMSG_INTSTS ); +return IRQ_HANDLED ; } +static void sev_wait_cmd_ioc (struct psp_device *psp,unsigned int *reg) +{ + psp->sev_int_rcvd =0 ; + +wait_event (psp->sev_int_queue, psp->sev_int_rcvd); + *reg =ioread32 (psp->io_regs +PSP_CMDRESP ); +} + +static int sev_cmd_buffer_len (int cmd) +{ +switch (cmd) { +case SEV_CMD_INIT :return sizeof (struct sev_data_init ); +case SEV_CMD_PLATFORM_STATUS :return sizeof (struct sev_user_data_status ); +case SEV_CMD_PEK_CSR :return sizeof (struct sev_data_pek_csr ); +case SEV_CMD_PEK_CERT_IMPORT :return sizeof (struct sev_data_pek_cert_import ); +case SEV_CMD_PDH_CERT_EXPORT :return sizeof (struct sev_data_pdh_cert_export ); +case SEV_CMD_LAUNCH_START :return sizeof (struct sev_data_launch_start ); +case SEV_CMD_LAUNCH_UPDATE_DATA :return sizeof (struct sev_data_launch_update_data ); +case SEV_CMD_LAUNCH_UPDATE_VMSA :return sizeof (struct sev_data_launch_update_vmsa ); +case SEV_CMD_LAUNCH_FINISH :return sizeof (struct sev_data_launch_finish ); +case SEV_CMD_LAUNCH_MEASURE :return sizeof (struct sev_data_launch_measure ); +case SEV_CMD_ACTIVATE :return sizeof (struct sev_data_activate ); +case SEV_CMD_DEACTIVATE :return sizeof (struct sev_data_deactivate ); +case SEV_CMD_DECOMMISSION :return sizeof (struct sev_data_decommission ); +case SEV_CMD_GUEST_STATUS :return sizeof (struct sev_data_guest_status ); +case SEV_CMD_DBG_DECRYPT :return sizeof (struct sev_data_dbg ); +case SEV_CMD_DBG_ENCRYPT :return sizeof (struct sev_data_dbg ); +case SEV_CMD_SEND_START :return sizeof (struct sev_data_send_start ); +case SEV_CMD_SEND_UPDATE_DATA :return sizeof (struct sev_data_send_update_data ); +case SEV_CMD_SEND_UPDATE_VMSA :return sizeof (struct sev_data_send_update_vmsa ); +case SEV_CMD_SEND_FINISH :return sizeof (struct sev_data_send_finish ); +case SEV_CMD_RECEIVE_START :return sizeof (struct sev_data_receive_start ); +case SEV_CMD_RECEIVE_FINISH :return sizeof (struct sev_data_receive_finish ); +case SEV_CMD_RECEIVE_UPDATE_DATA :return sizeof (struct sev_data_receive_update_data ); +case SEV_CMD_RECEIVE_UPDATE_VMSA :return sizeof (struct sev_data_receive_update_vmsa ); +case SEV_CMD_LAUNCH_UPDATE_SECRET :return sizeof (struct sev_data_launch_secret ); +default :return 0 ; + } + +return 0 ; +} + +static int sev_do_cmd (int cmd,void *data,int *psp_ret) +{ +unsigned int phys_lsb, phys_msb; +unsigned int reg, ret =0 ; +struct psp_device *psp; +struct sp_device *sp; + + sp =sp_get_psp_master_device (); +if (!sp) +return -ENODEV ; + + psp = sp->psp_data; +if (!psp) +return -ENODEV ; + +/* Get the physical address of the command buffer */ + phys_lsb = data ?lower_32_bits (__psp_pa (data)) :0 ; + phys_msb = data ?upper_32_bits (__psp_pa (data)) :0 ; + +dev_dbg (psp->dev,"sev command id %#x buffer 0x%08x%08x\n" , + cmd, phys_msb, phys_lsb); + +print_hex_dump_debug ("(in): " ,DUMP_PREFIX_OFFSET ,16 ,2 , data, +sev_cmd_buffer_len (cmd), false); + +/* Only one command at a time... */ +mutex_lock (&sev_cmd_mutex); + +iowrite32 (phys_lsb, psp->io_regs +PSP_CMDBUFF_ADDR_LO ); +iowrite32 (phys_msb, psp->io_regs +PSP_CMDBUFF_ADDR_HI ); + + reg = cmd; + reg <<=PSP_CMDRESP_CMD_SHIFT ; + reg |=PSP_CMDRESP_IOC ; +iowrite32 (reg, psp->io_regs +PSP_CMDRESP ); + +/* wait for command completion */ +sev_wait_cmd_ioc (psp, ®); + +if (psp_ret) + *psp_ret = reg &PSP_CMDRESP_ERR_MASK ; + +if (reg &PSP_CMDRESP_ERR_MASK ) { +dev_dbg (psp->dev,"sev command %#x failed (%#010x)\n" , + cmd, reg &PSP_CMDRESP_ERR_MASK ); + ret = -EIO ; + } + +mutex_unlock (&sev_cmd_mutex); +print_hex_dump_debug ("(out): " ,DUMP_PREFIX_OFFSET ,16 ,2 , data, +sev_cmd_buffer_len (cmd), false); +return ret; +} + +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) +{ +return -ENOTTY ; +} + +static const struct file_operations sev_fops = { + .owner =THIS_MODULE , + .unlocked_ioctl = sev_ioctl, +}; + +static int __sev_platform_init (struct sev_data_init *data,int *error) +{ +int rc =0 ; + +mutex_lock (&fw_init_mutex); + +if (!fw_init_count) { + rc =sev_do_cmd (SEV_CMD_INIT , data, error); +if (rc) + goto unlock; + } + + fw_init_count++; + +unlock: +mutex_unlock (&fw_init_mutex); +return rc; + +} + +int sev_platform_init (struct sev_data_init *data,int *error) +{ +struct sev_data_init *input =NULL ; +int rc; + +if (!data) { + input =kzalloc (sizeof (*input),GFP_KERNEL ); +if (!input) +return -ENOMEM ; + + data = input; + } + + rc =__sev_platform_init (data, error); + +kfree (input); +return rc; +} +EXPORT_SYMBOL_GPL (sev_platform_init); + +int sev_platform_shutdown (int *error) +{ +int rc =0 ; + +mutex_lock (&fw_init_mutex); + +if (!fw_init_count) + goto unlock; + +if (fw_init_count ==1 ) { + rc =sev_do_cmd (SEV_CMD_SHUTDOWN ,0 , error); +if (rc) + goto unlock; + } + + fw_init_count--; + +unlock: +mutex_unlock (&fw_init_mutex); +return rc; +} +EXPORT_SYMBOL_GPL (sev_platform_shutdown); + +int sev_platform_status (struct sev_user_data_status *data,int *error) +{ +return sev_do_cmd (SEV_CMD_PLATFORM_STATUS , data, error); +} +EXPORT_SYMBOL_GPL (sev_platform_status); + +int sev_issue_cmd_external_user (struct file *filep,unsigned int cmd, +void *data,int *error) +{ +if (!filep || filep->f_op != &sev_fops) +return -EBADF ; + +return sev_do_cmd (cmd, data, error); +} +EXPORT_SYMBOL_GPL (sev_issue_cmd_external_user); + +int sev_guest_deactivate (struct sev_data_deactivate *data,int *error) +{ +return sev_do_cmd (SEV_CMD_DEACTIVATE , data, error); +} +EXPORT_SYMBOL_GPL (sev_guest_deactivate); + +int sev_guest_activate (struct sev_data_activate *data,int *error) +{ +return sev_do_cmd (SEV_CMD_ACTIVATE , data, error); +} +EXPORT_SYMBOL_GPL (sev_guest_activate); + +int sev_guest_decommission (struct sev_data_decommission *data,int *error) +{ +return sev_do_cmd (SEV_CMD_DECOMMISSION , data, error); +} +EXPORT_SYMBOL_GPL (sev_guest_decommission); + +int sev_guest_df_flush (int *error) +{ +return sev_do_cmd (SEV_CMD_DF_FLUSH ,0 , error); +} +EXPORT_SYMBOL_GPL (sev_guest_df_flush); + +static int sev_ops_init (struct psp_device *psp) +{ +struct device *dev = psp->dev; +int ret; + +/* +* SEV feature support can be detected on multiple devices but the SEV +* FW commands must be issued on the master. During probe, we do not +* know the master hence we create /dev/sev on the first device probe. +* sev_do_cmd() finds the right master device to which to issue the +* command to the firmware. +*/ +if (!sev_misc_dev) { +struct miscdevice *misc; + + sev_misc_dev =devm_kzalloc (dev,sizeof (*sev_misc_dev),GFP_KERNEL ); +if (!sev_misc_dev) +return -ENOMEM ; + + misc = &sev_misc_dev->misc; + misc->minor =MISC_DYNAMIC_MINOR ; + misc->name =DEVICE_NAME ; + misc->fops = &sev_fops; + + ret =misc_register (misc); +if (ret) +return ret; + +kref_init (&sev_misc_dev->refcount); + }else { +kref_get (&sev_misc_dev->refcount); + } + +init_waitqueue_head (&psp->sev_int_queue); + psp->sev_misc = sev_misc_dev; +dev_info (dev,"registered SEV device\n" ); + +return 0 ; +} + +static int sev_init (struct psp_device *psp) +{ +/* Check if device supports SEV feature */ +if (!(ioread32 (psp->io_regs +PSP_FEATURE_REG ) &1 )) { +dev_dbg (psp->dev,"device does not support SEV\n" ); +return 1 ; + } + +return sev_ops_init (psp); +} + +static void sev_exit (struct kref *ref) +{ + struct sev_misc_dev *sev_dev = container_of(ref, struct sev_misc_dev, refcount); + +misc_deregister (&sev_dev->misc); +} +int psp_dev_init (struct sp_device *sp) {struct device *dev = sp->dev;
@@ -84,11 +381,17 @@ int psp_dev_init(struct sp_device *sp)if (sp->set_psp_master_device) sp->set_psp_master_device (sp); + ret =sev_init (psp); +if (ret) + goto e_irq; +/* Enable interrupt */ iowrite32 (-1 , psp->io_regs +PSP_P2CMSG_INTEN );return 0 ; +e_irq: +sp_free_psp_irq (psp->sp, psp); e_err: sp->psp_data =NULL ;
@@ -101,5 +404,8 @@ void psp_dev_destroy(struct sp_device *sp) {struct psp_device *psp = sp->psp_data; +if (psp->sev_misc) +kref_put (&sev_misc_dev->refcount, sev_exit); +sp_free_psp_irq (sp, psp); }
drivers/crypto/ccp/psp-dev.h +21 -0
@@ -25,9 +25,21 @@#include <linux/interrupt.h> #include <linux/irqreturn.h> #include <linux/dmaengine.h> +#include <linux/psp-sev.h> +#include <linux/miscdevice.h> #include "sp-dev.h" +#define PSP_C2PMSG (_num) ((_num) << 2) +#define PSP_CMDRESP PSP_C2PMSG(32) +#define PSP_CMDBUFF_ADDR_LO PSP_C2PMSG(56) +#define PSP_CMDBUFF_ADDR_HI PSP_C2PMSG(57) +#define PSP_FEATURE_REG PSP_C2PMSG(63) + +#define PSP_P2CMSG (_num) ((_num) << 2) +#define PSP_CMD_COMPLETE_REG 1 +#define PSP_CMD_COMPLETE PSP_P2CMSG(PSP_CMD_COMPLETE_REG) +#define PSP_P2CMSG_INTEN 0x0110#define PSP_P2CMSG_INTSTS 0x0114
@@ -44,6 +56,11 @@#define MAX_PSP_NAME_LEN 16 +struct sev_misc_dev { +struct kref refcount; +struct miscdevice misc; +}; +struct psp_device {struct list_head entry;
@@ -54,6 +71,10 @@ struct psp_device {struct sp_device *sp;void __iomem *io_regs; + +unsigned int sev_int_rcvd; +wait_queue_head_t sev_int_queue; + struct sev_misc_dev *sev_misc; };#endif /* __PSP_DEV_H */
include/linux/psp-sev.h +159 -0
163 lines not on this page · raw message
Signature · 1 line
2.9.5
Borislav Petkov
maintainerRe: [Part2 PATCH v6 13/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
Quoted 6 lines · Brijesh Singh
AMD's new Secure Encrypted Virtualization (SEV) feature allows the memory contents of virtual machines to be transparently encrypted with a key unique to the VM. The programming and management of the encryption keys are handled by the AMD Secure Processor (AMD-SP) which exposes the commands for these tasks. The complete spec is available at:
Quoted 8 lines
http://support.amd.com/TechDocs/55766_SEV-KM%20API_Specification.pdf
Extend the AMD-SP driver to provide the following support:
- an in-kernel API to communicate with the SEV firmware. The API can be used by the hypervisor to create encryption context for a SEV guest.
- a userspace IOCTL to manage the platform certificates.
Just minor cleanups:
---
drivers/crypto/ccp/psp-dev.c +11 -11
@@ -31,7 +31,7 @@static DEFINE_MUTEX (sev_cmd_mutex);static DEFINE_MUTEX (fw_init_mutex); -static struct sev_misc_dev *sev_misc_dev; +static struct sev_misc_dev *misc_dev;static int fw_init_count;static struct psp_device *psp_alloc_struct (struct sp_device *sp)
@@ -299,14 +299,14 @@ static int sev_ops_init(struct psp_device *psp)* sev_do_cmd() finds the right master device to which to issue the * command to the firmware. */ -if (!sev_misc_dev) { +if (!misc_dev) {struct miscdevice *misc; - sev_misc_dev =devm_kzalloc (dev,sizeof (*sev_misc_dev),GFP_KERNEL ); -if (!sev_misc_dev) + misc_dev =devm_kzalloc (dev,sizeof (*misc_dev),GFP_KERNEL ); +if (!misc_dev)return -ENOMEM ; - misc = &sev_misc_dev->misc; + misc = &misc_dev->misc; misc->minor =MISC_DYNAMIC_MINOR ; misc->name =DEVICE_NAME ; misc->fops = &sev_fops;
@@ -315,13 +315,13 @@ static int sev_ops_init(struct psp_device *psp)if (ret)return ret; -kref_init (&sev_misc_dev->refcount); +kref_init (&misc_dev->refcount); }else { -kref_get (&sev_misc_dev->refcount); +kref_get (&misc_dev->refcount); }init_waitqueue_head (&psp->sev_int_queue); - psp->sev_misc = sev_misc_dev; + psp->sev_misc = misc_dev;dev_info (dev,"registered SEV device\n" );return 0 ;
@@ -340,9 +340,9 @@ static int sev_init(struct psp_device *psp)static void sev_exit (struct kref *ref) { - struct sev_misc_dev *sev_dev = container_of(ref, struct sev_misc_dev, refcount); + struct sev_misc_dev *misc_dev = container_of(ref, struct sev_misc_dev, refcount); -misc_deregister (&sev_dev->misc); +misc_deregister (&misc_dev->misc); }int psp_dev_init (struct sp_device *sp)
@@ -405,7 +405,7 @@ void psp_dev_destroy(struct sp_device *sp)struct psp_device *psp = sp->psp_data;if (psp->sev_misc) -kref_put (&sev_misc_dev->refcount, sev_exit); +kref_put (&misc_dev->refcount, sev_exit);sp_free_psp_irq (sp, psp); }
include/linux/psp-sev.h +1 -1
@@ -525,7 +525,7 @@ int sev_platform_shutdown(int *error);/** * sev_platform_status - perform SEV PLATFORM_STATUS command * -* @init: sev_data_status structure to be processed +* @status: sev_user_data_status structure to be processed * @error: SEV command return code * * Returns:
Signature · 4 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.Re: [Part2 PATCH v6 13/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
On 10/23/2017 02:34 AM, Borislav Petkov wrote:
...
Quoted 3 lines
Just minor cleanups:
Thanks Boris, I have applied your cleanups.
-Brijesh
Quoted 85 lines
---
drivers/crypto/ccp/psp-dev.c +11
-11@@ -31,7 +31,7 @@static DEFINE_MUTEX (sev_cmd_mutex);static DEFINE_MUTEX (fw_init_mutex); -static struct sev_misc_dev *sev_misc_dev; +static struct sev_misc_dev *misc_dev;static int fw_init_count;static struct psp_device *psp_alloc_struct (struct sp_device *sp)@@ -299,14 +299,14 @@ static int sev_ops_init(struct psp_device *psp)* sev_do_cmd() finds the right master device to which to issue the * command to the firmware. */ -if (!sev_misc_dev) { +if (!misc_dev) {struct miscdevice *misc; - sev_misc_dev =devm_kzalloc (dev,sizeof (*sev_misc_dev),GFP_KERNEL ); -if (!sev_misc_dev) + misc_dev =devm_kzalloc (dev,sizeof (*misc_dev),GFP_KERNEL ); +if (!misc_dev)return -ENOMEM ; - misc = &sev_misc_dev->misc; + misc = &misc_dev->misc; misc->minor =MISC_DYNAMIC_MINOR ; misc->name =DEVICE_NAME ; misc->fops = &sev_fops;@@ -315,13 +315,13 @@ static int sev_ops_init(struct psp_device *psp)if (ret)return ret; -kref_init (&sev_misc_dev->refcount); +kref_init (&misc_dev->refcount); }else { -kref_get (&sev_misc_dev->refcount); +kref_get (&misc_dev->refcount); }init_waitqueue_head (&psp->sev_int_queue); - psp->sev_misc = sev_misc_dev; + psp->sev_misc = misc_dev;dev_info (dev,"registered SEV device\n" );return 0 ;@@ -340,9 +340,9 @@ static int sev_init(struct psp_device *psp)static void sev_exit (struct kref *ref) { - struct sev_misc_dev *sev_dev = container_of(ref, struct sev_misc_dev, refcount); + struct sev_misc_dev *misc_dev = container_of(ref, struct sev_misc_dev, refcount); -misc_deregister (&sev_dev->misc); +misc_deregister (&misc_dev->misc); }int psp_dev_init (struct sp_device *sp)@@ -405,7 +405,7 @@ void psp_dev_destroy(struct sp_device *sp)struct psp_device *psp = sp->psp_data;if (psp->sev_misc) -kref_put (&sev_misc_dev->refcount, sev_exit); +kref_put (&misc_dev->refcount, sev_exit);sp_free_psp_irq (sp, psp); }include/linux/psp-sev.h +1
-1@@ -525,7 +525,7 @@ int sev_platform_shutdown(int *error);/** * sev_platform_status - perform SEV PLATFORM_STATUS command * -* @init: sev_data_status structure to be processed +* @status: sev_user_data_status structure to be processed * @error: SEV command return code * * Returns:
Borislav Petkov
maintainerRe: [Part2 PATCH v6 13/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
In reply to Brijesh Singhdrivers/crypto/ccp/psp-dev.c static struct psp_device *psp_alloc_struct(struct sp_device *sp) · line 193
Quoted 7 lines · Brijesh Singh
drivers/crypto/ccp/psp-dev.c +7
-0+static int __sev_platform_init (struct sev_data_init *data,int *error) +{ +int rc =0 ; + +mutex_lock (&fw_init_mutex); + +if (!fw_init_count) {
I still don't like global semaphores. Can you get the status and check for PSTATE.INIT state and do the init only if the platform is in PSTATE.UNINIT?
Signature · 4 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.Re: [Part2 PATCH v6 13/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
Quoted 13 lines · Borislav Petkov
drivers/crypto/ccp/psp-dev.c static struct psp_device *psp_alloc_struct(struct sp_device *sp) · line 193
Quoted 7 lines · Brijesh Singh
drivers/crypto/ccp/psp-dev.c +7
-0+static int __sev_platform_init (struct sev_data_init *data,int *error) +{ +int rc =0 ; + +mutex_lock (&fw_init_mutex); + +if (!fw_init_count) {I still don't like global semaphores. Can you get the status and check for PSTATE.INIT state and do the init only if the platform is in PSTATE.UNINIT?
Calling PLATFORM_GET_STATUS is not required, we can manage the state through a simple ref count variable. Issuing PSP commands will always be much more expensive compare to accessing a protected global variable. I would prefer to avoid invoking PSP command if possible. Additionally, the global semaphore is still needed to serialize the sev_platform_init() and sev_platform_shutdown() from multiple processes. e.g If process "A" calls sev_platform_init() and if it gets preempted due to whatever reason then we don't want another process to issue the shutdown command while process "A" is in middle of sev_platform_init().
-Brijesh
Borislav Petkov
maintainerRe: [Part2 PATCH v6 13/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
Quoted 3 lines · Brijesh Singh
Calling
PLATFORM_GET_STATUSis not required, we can manage the state through a simple ref count variable. Issuing PSP commands will always be much more expensive compare to accessing a protected global variable.
What does "protected" mean here?
In any case, that variable can be a simple bool as you use it as such.
Quoted 7 lines
I would prefer to avoid invoking PSP command if possible. Additionally, the global semaphore is still needed to serialize the
sev_platform_init()andsev_platform_shutdown()from multiple processes. e.g If process "A" callssev_platform_init()and if it gets preempted due to whatever reason then we don't want another process to issue the shutdown command while process "A" is in middle ofsev_platform_init().
How? You're holding fw_init_mutex.
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Re: [Part2 PATCH v6 13/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
Quoted 7 lines · Borislav Petkov
Quoted 3 lines · Brijesh Singh
Calling
PLATFORM_GET_STATUSis not required, we can manage the state through a simple ref count variable. Issuing PSP commands will always be much more expensive compare to accessing a protected global variable.What does "protected" mean here?
Access global variable after acquiring the semaphore.
Quoted 2 lines
In any case, that variable can be a simple bool as you use it as such.
I am not using the variable (fw_init_count) as boolean. The variable gets incremented in sev_platform_init() and decremented in sev_platform_shutdown(). In very first call to sev_platform_init (i.e when variable is zero) we issue PLATFORM_INIT command, similarly PLATFORM_SHUTDOWN is issued on the last (i.e when variable value is reached to zero). The variable is used as ref counter.
Quoted 10 lines
Quoted 7 lines
I would prefer to avoid invoking PSP command if possible. Additionally, the global semaphore is still needed to serialize the
sev_platform_init()andsev_platform_shutdown()from multiple processes. e.g If process "A" callssev_platform_init()and if it gets preempted due to whatever reason then we don't want another process to issue the shutdown command while process "A" is in middle ofsev_platform_init().How? You're holding
fw_init_mutex.
In your previous reply you comments on global semaphore (fw_init_mutex) and in response I tried to highlight why we need the global semaphore. Did I misunderstood your comment ?
-Brijesh
Borislav Petkov
maintainerRe: [Part2 PATCH v6 13/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
Quoted 1 line · Brijesh Singh
The variable is used as ref counter.
... and it can't be converted to a boolean because...?
Quoted 3 lines
In your previous reply you comments on global semaphore (
fw_init_mutex) and in response I tried to highlight why we need the global semaphore. Did I misunderstood your comment ?
Yes, what happens if you get preempted while holding the mutex? Will the other process be able to do anything?
Signature · 4 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.Re: [Part2 PATCH v6 13/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
Quoted 5 lines · Borislav Petkov
Quoted 1 line · Brijesh Singh
The variable is used as ref counter.
... and it can't be converted to a boolean because...?
SHUTDOWN command unconditionally transitions a platform to uninitialized state. The command does not care how many processes are actively using the PSP. We don't want to shutdown the firmware while other process is still using it.
e.g consider three processes (A, B, C)
Process A:
----------sev_platform_init()sev_do_cmd(..)
...
...sev_do_cmd(..)
...sev_platform_shutdown()
Process B:
-----------sev_platform_init()sev_do_cmd(...)sev_platform_shutdown()
Process C:
----------sev_platform_init()sev_do_cmd(...)sev_do_cmd(...)sev_do_cmd(...)sev_platform_shutdown()
As per the SEV spec section 5.1.2 (platform state machine), several commands require that platform should be initialized before issuing the actual command. As you can see Process B may finish quickly and SHUTDOWN from process B will simply uninitialize the firmware and cause unexpected result to process A and C.
Quoted 7 lines
Quoted 3 lines
In your previous reply you comments on global semaphore (
fw_init_mutex) and in response I tried to highlight why we need the global semaphore. Did I misunderstood your comment ?Yes, what happens if you get preempted while holding the mutex? Will the other process be able to do anything?
If other process tries to issue the sev_platform_init/shutdown() then they have to wait.
The sev_platform_init() and sev_platform_shutdown() uses the same global mutex. See the original code below.
+static int __sev_platform_init(struct sev_data_init *data, int *error) +{
+ int rc = 0;
+
+ mutex_lock(&fw_init_mutex);
+
+ if (!fw_init_count) {
+ rc = sev_do_cmd(SEV_CMD_INIT, data, error);
+ if (rc)
+ goto unlock;
+ }
+
+ fw_init_count++;
+
+unlock:
+ mutex_unlock(&fw_init_mutex);
+ return rc;
+
+}
+
+int sev_platform_shutdown(int *error)
+{
+ int rc = 0;
+
+ mutex_lock(&fw_init_mutex);
+
+ if (!fw_init_count)
+ goto unlock;
+
+ if (fw_init_count == 1) {
+ rc = sev_do_cmd(SEV_CMD_SHUTDOWN, 0, error);
+ if (rc)
+ goto unlock;
+ }
+
+ fw_init_count--;
+
+unlock:
+ mutex_unlock(&fw_init_mutex);
+ return rc;
+}
Borislav Petkov
maintainerRe: [Part2 PATCH v6 13/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
Quoted 4 lines · Brijesh Singh
SHUTDOWN command unconditionally transitions a platform to uninitialized state. The command does not care how many processes are actively using the PSP. We don't want to shutdown the firmware while other process is still using it.
So why do you have to init and shutdown the PSP each time you execute a command? Why isn't the PSP initialized, *exactly* *once* at driver init and shut down, also exactly once at driver exit?
Quoted 2 lines
If other process tries to issue the sev_platform_init/shutdown() then they have to wait.
Exactly, and not what you said earlier:
"If process "A" calls sev_platform_init() and if it gets preempted due to whatever reason then we don't want another process to issue the shutdown command while process "A" is in middle of sev_platform_init()."
IOW, if your critical regions are protected properly by a mutex, nothing like the above will happen.
But what you're trying to explain to me is that the fw_init_count is going to prevent a premature shutdown when it is > 1. But that's not what I meant...
Anyway, see my question above.
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Re: [Part2 PATCH v6 13/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
Quoted 9 lines · Borislav Petkov
Quoted 4 lines · Brijesh Singh
SHUTDOWN command unconditionally transitions a platform to uninitialized state. The command does not care how many processes are actively using the PSP. We don't want to shutdown the firmware while other process is still using it.
So why do you have to init and shutdown the PSP each time you execute a command? Why isn't the PSP initialized, *exactly* *once* at driver init and shut down, also exactly once at driver exit?
Wish we could do that but the following reasons makes things complicated:
1) The commands must be issued from the PSP master devices, at PSP initialization time we do not know the PSP 'master' device. Hence we will not able to invoke sev_platform_init() during the PSP initialization time.
2) some commands require the platform to be in UNINIT state -- e.g FACTORY_RESET. So, if we do the INIT at the PSP initialization time then we still need to perform the SHUTDOWN outside the normal code flow to handle these commands.
we can workaround #1 by adding some hooks in sp_pci_init() to invoke the PSP initialization routines after pci_register_driver() is done but #2 can get painful because it will require us calling the SHUTDOWN outside the sp_pci_exit() code flow.
-Brijesh
Borislav Petkov
maintainerRe: [Part2 PATCH v6 13/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
Quoted 4 lines · Brijesh Singh
we can workaround #1 by adding some hooks in
sp_pci_init()to invoke the PSP initialization routines afterpci_register_driver()is done but #2 can get painful because it will require us calling the SHUTDOWN outside thesp_pci_exit()code flow.
Ok, do that and init the PSP master and then put the device in UNINIT state only in the functions which execute those commands which need the device to be in UNINIT state, e.g., wrap the SEV_CMD_FACTORY_RESET glue in a command function which does put the device in the UNINIT state as a first step.
Then, when that function is done, put the device in the mode which the other commands would expect it to be in, e.g., INIT state.
This way you'll simplify the whole command flow considerably and won't have to "toggle" the device each time and will save yourself a lot of time on command execution.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.Re: [Part2 PATCH v6 13/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
Quoted 10 lines · Borislav Petkov
Quoted 4 lines · Brijesh Singh
we can workaround #1 by adding some hooks in
sp_pci_init()to invoke the PSP initialization routines afterpci_register_driver()is done but #2 can get painful because it will require us calling the SHUTDOWN outside thesp_pci_exit()code flow.Ok, do that and init the PSP master and then put the device in UNINIT state only in the functions which execute those commands which need the device to be in UNINIT state, e.g., wrap the
SEV_CMD_FACTORY_RESETglue in a command function which does put the device in the UNINIT state as a first step.
transiting a platform in UINIT state to handle the FACTORY_RESET can have a negative consequence.
Consider this scenario:
Process A
---------sev_launch_start(...)
while (count < 10000) {
sev_launch_update(...)
}
sev_launch_finish()
...
...
Process B:
---------
....sev_factory_reset();
....
If in order to handle the FACTORY_RESET we transition a platform in UINIT state then it will results as unexpected failure from the sev_launch_update() because the FACTORY_RESET command remove all the state information created by sev_launch_start() etc.
I think our design so far is simple, if command require INIT state then caller executes sev_platform_init(), then command and finish with sev_platform_shutdown(). If command does not require INIT state, then simply issue the command. e.g currently, when caller issues FACTORY_RESET then we pass command directly to PSP and if FW is in INIT state then FACTORY_RESET returns error (INVALID_STATE/EBUSY) and we propagate the error code to userspace. User can retry the command sometime later when nobody else is using the PSP.
Quoted 10 lines
Then, when that function is done, put the device in the mode which the other commands would expect it to be in, e.g., INIT state.
This way you'll simplify the whole command flow considerably and won't have to "toggle" the device each time and will save yourself a lot of time on command execution.
Thx.
Borislav Petkov
maintainerRe: [Part2 PATCH v6 13/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
Quoted 2 lines · Brijesh Singh
... User can retry the command sometime later when nobody else is using the PSP.
That still doesn't prevent you from doing two things:
* make that fw_init_count a proper kref instead of your homegrown thing
* do not preemptively execute commands on the PSP if you can't possibly know what the next command is going to be - instead, just put it in the required state only when you really have to. I.e., don't do all that unnecessary INIT -> CMD -> SHUTDOWN game for no reason.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.Re: [Part2 PATCH v6 13/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
Quoted 6 lines · Borislav Petkov
Quoted 2 lines · Brijesh Singh
... User can retry the command sometime later when nobody else is using the PSP.
That still doesn't prevent you from doing two things:
* make that
fw_init_counta proper kref instead of your homegrown thing
OK, I can use kref in next patch.
Quoted 5 lines
* do not preemptively execute commands on the PSP if you can't possibly know what the next command is going to be - instead, just put it in the required state only when you really have to. I.e., don't do all that unnecessary INIT -> CMD -> SHUTDOWN game for no reason.
Yep, we are doing state transition only when we really need to. At least so far I have tried to avoid making any unnecessary state transitions.
Quoted 2 lines
Thx.
Borislav Petkov
maintainerRe: [Part2 PATCH v6 13/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
Quoted 2 lines · Brijesh Singh
Yep, we are doing state transition only when we really need to. At least so far I have tried to avoid making any unnecessary state transitions.
So change all those which do INIT -> CMD -> SHUTDOWN to do only the command as the INIT state is the default state, AFAIU it.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [Part2 PATCH v6 13/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
Quoted 6 lines · Borislav Petkov
Quoted 2 lines · Brijesh Singh
Yep, we are doing state transition only when we really need to. At least so far I have tried to avoid making any unnecessary state transitions.
So change all those which do INIT -> CMD -> SHUTDOWN to do only the command as the INIT state is the default state, AFAIU it.
I don't that will work. It may be possible that I am not able to follow what exactly you have in mind. Please let me clarify it, on boot the firmware is in UINIT state.
Firmware maintain three states: UINIT, INIT and WORKING. Few commands can be executed in UINIT, several command can be executed in INIT, some command can be executed in WORKING and few commands can be executed in any states (see SEV spec for platform state). We do not have ioctls to issue the INIT and SHUTDOWN command.
As I have explained in previous messages, the SHUTDOWN unconditionally destory's the FW context hence we have refrained from providing the access for this command to userspace.
My approach is, when userspace issues a command we check if command requires INIT state, if so then we do INIT -> CMD -> SHUTDOWN. If command can be executed in any state then we issue the command . If command need to be executed in UINIT state then we *do not* do SHUTDOWN->CMD, instead we issue the cmd and PSP will fail with error code and caller can check the error code to determine why command failed.
If I go with your recommendation then I am not able to see who will transition the platform from UINIT -> INIT so that command can run?
Lets try with very simple example:
# modprobe ccp
/* FW is in INIT state */
# userspace runs
ioctl(fd, SEV_USER_PEK_GEN, &err)
This will fail because PEK_GEN require the platform in INIT state and nobody has done the state transition from INIT -> UINIT.
-Brijesh
Borislav Petkov
maintainerRe: [Part2 PATCH v6 13/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
Quoted 2 lines · Brijesh Singh
This will fail because
PEK_GENrequire the platform in INIT state and nobody has done the state transition from INIT -> UINIT.
Huh, FW is in INIT state and PEK_GEN wants it to be in INIT state. Typo?
Aaanyway, I don't like this whole notion of prematurely and predictively executing commands on the PSP if it is not needed. So how about executing only those commands which put the FW in the required state and then executing the actual command?
I.e., if a command needs to be executed in UINIT state, you put the PSP in that state before executing that command. If the command needs to be in INIT state, you put the PSP in INIT state first and so on...
For convenience, you could carry the current PSP state in some struct psp_dev member or whatever and query it before running the respective commands.
Hmmm?
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [Part2 PATCH v6 13/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
Quoted 4 lines · Borislav Petkov
Quoted 2 lines · Brijesh Singh
This will fail because
PEK_GENrequire the platform in INIT state and nobody has done the state transition from INIT -> UINIT.Huh, FW is in INIT state and
PEK_GENwants it to be in INIT state. Typo?
Yes it is typo. PEK_GEN wants FW to be in INIT state hence someone need to transition from UNINIT -> INIT.
Quoted 4 lines
Aaanyway, I don't like this whole notion of prematurely and predictively executing commands on the PSP if it is not needed. So how about executing only those commands which put the FW in the required state and then executing the actual command?
That's what I am doing except FACTORY_RESET. The FACTORY_RESET require the FW to be in UNINIT state, since we don't who else is running in parallel hence its not safe to issue the SHUTDOWN to transition from INIT -> UNINIT. If FW is not in correct state this command will fail with error code (INVALID_STATE) and user can retry (please note that user can always use PLATFORM_STATUS to query the current FW state before issuing a command). I see that we can do a small optimization -- since we already know the FW state hence we can avoid issuing PSP command when we know for sure that command will fail because we are not in correct state.
Quoted 4 lines
I.e., if a command needs to be executed in UINIT state, you put the PSP in that state before executing that command. If the command needs to be in INIT state, you put the PSP in INIT state first and so on...
If command needs INIT state and FW is not in INIT state then its safe to transition from UNINIT -> INIT. But if command needs UNINIT state and FW is in INIT state then its not safe to transition -- in those case we simply return EBUSY and let the user retry the command.
Borislav Petkov
maintainerRe: [Part2 PATCH v6 13/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
Quoted 2 lines · Brijesh Singh
Yes it is typo.
PEK_GENwants FW to be in INIT state hence someone need to transition from UNINIT -> INIT.
Which, once you've done it once on driver init, is there.
Quoted 1 line
That's what I am doing except
FACTORY_RESET.
Well, not really. Lemme pick a command at random...
PEK_CSR. For that, you do INIT -> PEK_CSR -> SHUTDOWN.
Doc says, platform needs to be in INIT or WORKING state. But nothing says you should shut it down. Spec says, SHUTDOWN transitions platform to UNINIT state. So when the next command comes in which needs the platform to be in INIT state, you go and INIT it again. For no reason *WHATSOEVER*!
I know, you're gonna say, but what if the next command needs a different state than INIT. Well, *then* you transition it, in the command function. When that function executes. But not before that and not in preparation that *maybe* the next command will be it.
Now, if you did:
INIT once during driver init
PEK_CSR
(platform remains in INIT state)
<--- the next command here can execute directly if it is allowed in INIT state.
Instead, the platform has been shutdown and you init it again. Do you see now what I mean?
IOW, once you init the PSP master, you should keep it in the INIT state
- or the state in which most commands expect it to be and thus save yourself all that unnecessary toggling. If a command needs it to be in a different state, only *then* you transition it.
Instead, what you have now is that you call INIT and SHUTDOWN around SEV_PEK_GEN, SEV_PDH_GEN, SEV_PEK_CSR, SEV_PEK_CERT_IMPORT, SEV_PDH_CERT_EXPORT and for all those, the platform must be in INIT (for some in WORKING state) but for all in INIT state and "The platform remains be in the same state after completion." So the whole SHUTDOWN -> INIT wankery in-between is a pure waste of electrons.
Quoted 3 lines
I see that we can do a small optimization -- since we already know
the FW state hence we can avoid issuing PSP command when we know for sure that command will fail because we are not in correct state.
As I said before, you should do that regardless by recording the current state of the PSP in variable so that you can save yourself the status querying.
Quoted 4 lines
If command needs INIT state and FW is not in INIT state then its safe to transition from UNINIT -> INIT. But if command needs UNINIT state and FW is in INIT state then its not safe to transition -- in those case we simply return EBUSY and let the user retry the command.
Whatever - that doesn't contradict what I'm proposing.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [Part2 PATCH v6 13/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
Quoted 27 lines · Borislav Petkov
Quoted 2 lines · Brijesh Singh
Yes it is typo.
PEK_GENwants FW to be in INIT state hence someone need to transition from UNINIT -> INIT.Which, once you've done it once on driver init, is there.
Quoted 1 line
That's what I am doing except
FACTORY_RESET.Well, not really. Lemme pick a command at random...
PEK_CSR. For that, you do INIT ->PEK_CSR-> SHUTDOWN.Doc says, platform needs to be in INIT or WORKING state. But nothing says you should shut it down. Spec says, SHUTDOWN transitions platform to UNINIT state. So when the next command comes in which needs the platform to be in INIT state, you go and INIT it again. For no reason *WHATSOEVER*!
I know, you're gonna say, but what if the next command needs a different state than INIT. Well, *then* you transition it, in the command function. When that function executes. But not before that and not in preparation that *maybe* the next command will be it.
Now, if you did:
INIT once during driver init
PEK_CSR
Quoted 7 lines
(platform remains in INIT state)
<--- the next command here can execute directly if it is allowed in INIT state.
Instead, the platform has been shutdown and you init it again. Do you see now what I mean?
Yes, I can see that with your proposal we may able to save some PSP interaction because after command execution we do not restore to the previous state. e.g before executing the PEK_CSR command if FW was in UINIT then we do UNINIT -> INIT and leave it to INIT state.
Quoted 4 lines
IOW, once you init the PSP master, you should keep it in the INIT state
- or the state in which most commands expect it to be and thus save yourself all that unnecessary toggling. If a command needs it to be in a different state, only *then* you transition it.
Let me implement it and send you the patch. I think the command execution function will look like this:
static int sev_ioctl_do_pek_csr(...)
{
....
....
mutex(&fw_init_mutex);
/* If FW is not in INIT state then initialize before executing command */ if (psp->sev_state != SEV_STATE_INIT) {
rc = sev_platform_init(...);
if (rc) {
mutex_unlock(&fw_init_mutex);
return rc;
}
}
rc = sev_do_cmd(....)
mutex_unlock(&fw_init_mutex);
return rc;
}
and factory reset will look like this
static int sev_ioctl_do_reset(...)
{
mutex(&fw_init_mutex);
/* If FW is not in UINIT state then shutdown before executing command */ if (psp->sev_state != SEV_STATE_INIT) sev_platform_shutdown(...);
rc = sev_do_cmd(....)
mutex_unlock(&fw_init_mutex);
return rc;
}
Quoted 20 lines
Instead, what you have now is that you call INIT and SHUTDOWN around
SEV_PEK_GEN,SEV_PDH_GEN,SEV_PEK_CSR,SEV_PEK_CERT_IMPORT,SEV_PDH_CERT_EXPORTand for all those, the platform must be in INIT (for some in WORKING state) but for all in INIT state and "The platform remains be in the same state after completion." So the whole SHUTDOWN -> INIT wankery in-between is a pure waste of electrons.Quoted 3 lines
I see that we can do a small optimization -- since we already know
the FW state hence we can avoid issuing PSP command when we know for sure that command will fail because we are not in correct state.As I said before, you should do that regardless by recording the current state of the PSP in variable so that you can save yourself the status querying.
Quoted 4 lines
If command needs INIT state and FW is not in INIT state then its safe to transition from UNINIT -> INIT. But if command needs UNINIT state and FW is in INIT state then its not safe to transition -- in those case we simply return EBUSY and let the user retry the command.
Whatever - that doesn't contradict what I'm proposing.
PATCH v6 16/38 crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
In reply to Brijesh SinghAMD's new Secure Encrypted Virtualization (SEV) feature allows the memory contents of virtual machines to be transparently encrypted with a key unique to the VM. The programming and management of the encryption keys are handled by the AMD Secure Processor (AMD-SP) which exposes the commands for these tasks. The complete spec is available at:
http://support.amd.com/TechDocs/55766_SEV-KM%20API_Specification.pdf
Extend the AMD-SP driver to provide the following support:
- an in-kernel API to communicate with the SEV firmware. The API can be used by the hypervisor to create encryption context for a SEV guest.
- a userspace IOCTL to manage the platform certificates.
---
Boris,
I have tried to minimize the INIT -> SHUTDOWN transition by keeping state information in sev_state variable. Since we INIT the platform during the modprobe time hence we no longer need the kref count and init mutex. Here are list of changes.
Changes since v6:
* Add functions to init and shutdown firmware during modprobe
* Add sev_state variable in psp_device to keep track of the INIT and SHUTDOWN state
* Don't allow caller to shutdown the FW because SHUTDOWN will be done during the module removal.
* Drop the fw_init_mutex and init_refcount because we no longer allow apps to INIT and UINIT the platform
drivers/crypto/ccp/psp-dev.c | 360 +++++++++++++++++++++++++++++++++++++++++++ drivers/crypto/ccp/psp-dev.h | 22 +++ drivers/crypto/ccp/sp-dev.c | 9 ++ drivers/crypto/ccp/sp-dev.h | 4 + include/linux/psp-sev.h | 158 +++++++++++++++++++ 5 files changed, 553 insertions(+)
drivers/crypto/ccp/psp-dev.c +360 -0
@@ -26,6 +26,15 @@#include "sp-dev.h" #include "psp-dev.h" +#define DEVICE_NAME "sev" + +static DEFINE_MUTEX (sev_cmd_mutex); +static struct sev_misc_dev *misc_dev; +static struct psp_device *psp_master; + +static int sev_platform_shutdown_locked (int *error); +static int sev_platform_init_locked (struct sev_data_init *data,int *error); +static struct psp_device *psp_alloc_struct (struct sp_device *sp) {struct device *dev = sp->dev;
@@ -45,9 +54,304 @@ static struct psp_device *psp_alloc_struct(struct sp_device *sp)static irqreturn_t psp_irq_handler (int irq,void *data) { +struct psp_device *psp = data; +unsigned int status; +int reg; + +/* Read the interrupt status: */ + status =ioread32 (psp->io_regs +PSP_P2CMSG_INTSTS ); + +/* Check if it is command completion: */ +if (!(status &BIT (PSP_CMD_COMPLETE_REG ))) + goto done; + +/* Check if it is SEV command completion: */ + reg =ioread32 (psp->io_regs +PSP_CMDRESP ); +if (reg &PSP_CMDRESP_RESP ) { + psp->sev_int_rcvd =1 ; +wake_up (&psp->sev_int_queue); + } + +done: +/* Clear the interrupt status by writing the same value we read. */ +iowrite32 (status, psp->io_regs +PSP_P2CMSG_INTSTS ); +return IRQ_HANDLED ; } +static void sev_wait_cmd_ioc (struct psp_device *psp,unsigned int *reg) +{ + psp->sev_int_rcvd =0 ; + +wait_event (psp->sev_int_queue, psp->sev_int_rcvd); + *reg =ioread32 (psp->io_regs +PSP_CMDRESP ); +} + +static int sev_cmd_buffer_len (int cmd) +{ +switch (cmd) { +case SEV_CMD_INIT :return sizeof (struct sev_data_init ); +case SEV_CMD_PLATFORM_STATUS :return sizeof (struct sev_user_data_status ); +case SEV_CMD_PEK_CSR :return sizeof (struct sev_data_pek_csr ); +case SEV_CMD_PEK_CERT_IMPORT :return sizeof (struct sev_data_pek_cert_import ); +case SEV_CMD_PDH_CERT_EXPORT :return sizeof (struct sev_data_pdh_cert_export ); +case SEV_CMD_LAUNCH_START :return sizeof (struct sev_data_launch_start ); +case SEV_CMD_LAUNCH_UPDATE_DATA :return sizeof (struct sev_data_launch_update_data ); +case SEV_CMD_LAUNCH_UPDATE_VMSA :return sizeof (struct sev_data_launch_update_vmsa ); +case SEV_CMD_LAUNCH_FINISH :return sizeof (struct sev_data_launch_finish ); +case SEV_CMD_LAUNCH_MEASURE :return sizeof (struct sev_data_launch_measure ); +case SEV_CMD_ACTIVATE :return sizeof (struct sev_data_activate ); +case SEV_CMD_DEACTIVATE :return sizeof (struct sev_data_deactivate ); +case SEV_CMD_DECOMMISSION :return sizeof (struct sev_data_decommission ); +case SEV_CMD_GUEST_STATUS :return sizeof (struct sev_data_guest_status ); +case SEV_CMD_DBG_DECRYPT :return sizeof (struct sev_data_dbg ); +case SEV_CMD_DBG_ENCRYPT :return sizeof (struct sev_data_dbg ); +case SEV_CMD_SEND_START :return sizeof (struct sev_data_send_start ); +case SEV_CMD_SEND_UPDATE_DATA :return sizeof (struct sev_data_send_update_data ); +case SEV_CMD_SEND_UPDATE_VMSA :return sizeof (struct sev_data_send_update_vmsa ); +case SEV_CMD_SEND_FINISH :return sizeof (struct sev_data_send_finish ); +case SEV_CMD_RECEIVE_START :return sizeof (struct sev_data_receive_start ); +case SEV_CMD_RECEIVE_FINISH :return sizeof (struct sev_data_receive_finish ); +case SEV_CMD_RECEIVE_UPDATE_DATA :return sizeof (struct sev_data_receive_update_data ); +case SEV_CMD_RECEIVE_UPDATE_VMSA :return sizeof (struct sev_data_receive_update_vmsa ); +case SEV_CMD_LAUNCH_UPDATE_SECRET :return sizeof (struct sev_data_launch_secret ); +default :return 0 ; + } + +return 0 ; +} + +static int sev_do_cmd_locked (int cmd,void *data,int *psp_ret) +{ +struct psp_device *psp = psp_master; +unsigned int phys_lsb, phys_msb; +unsigned int reg, ret =0 ; + +if (!psp) +return -ENODEV ; + +/* Get the physical address of the command buffer */ + phys_lsb = data ?lower_32_bits (__psp_pa (data)) :0 ; + phys_msb = data ?upper_32_bits (__psp_pa (data)) :0 ; + +dev_dbg (psp->dev,"sev command id %#x buffer 0x%08x%08x\n" , + cmd, phys_msb, phys_lsb); + +print_hex_dump_debug ("(in): " ,DUMP_PREFIX_OFFSET ,16 ,2 , data, +sev_cmd_buffer_len (cmd), false); + +iowrite32 (phys_lsb, psp->io_regs +PSP_CMDBUFF_ADDR_LO ); +iowrite32 (phys_msb, psp->io_regs +PSP_CMDBUFF_ADDR_HI ); + + reg = cmd; + reg <<=PSP_CMDRESP_CMD_SHIFT ; + reg |=PSP_CMDRESP_IOC ; +iowrite32 (reg, psp->io_regs +PSP_CMDRESP ); + +/* wait for command completion */ +sev_wait_cmd_ioc (psp, ®); + +if (psp_ret) + *psp_ret = reg &PSP_CMDRESP_ERR_MASK ; + +if (reg &PSP_CMDRESP_ERR_MASK ) { +dev_dbg (psp->dev,"sev command %#x failed (%#010x)\n" , + cmd, reg &PSP_CMDRESP_ERR_MASK ); + ret = -EIO ; + } + +print_hex_dump_debug ("(out): " ,DUMP_PREFIX_OFFSET ,16 ,2 , data, +sev_cmd_buffer_len (cmd), false); + +return ret; +} + +static int sev_do_cmd (int cmd,void *data,int *psp_ret) +{ +int rc; + +mutex_lock (&sev_cmd_mutex); + rc =sev_do_cmd_locked (cmd, data, psp_ret); +mutex_unlock (&sev_cmd_mutex); + +return rc; +} + +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) +{ +return -ENOTTY ; +} + +static const struct file_operations sev_fops = { + .owner =THIS_MODULE , + .unlocked_ioctl = sev_ioctl, +}; + +static int sev_platform_init_locked (struct sev_data_init *data,int *error) +{ +struct psp_device *psp = psp_master; +struct sev_data_init *input =NULL ; +int rc =0 ; + +if (!psp) +return -ENODEV ; + +if (psp->sev_state ==SEV_STATE_INIT ) +return 0 ; + +if (!data) { + input =kzalloc (sizeof (*input),GFP_KERNEL ); +if (!input) +return -ENOMEM ; + + data = input; + } + + rc =sev_do_cmd_locked (SEV_CMD_INIT , data, error); +if (rc) + goto e_free; + + psp->sev_state =SEV_STATE_INIT ; +dev_dbg (psp->dev,"SEV firmware intialized\n" ); + +e_free: +kfree (input); +return rc; +} + +int sev_platform_init (struct sev_data_init *data,int *error) +{ +int rc; + +mutex_lock (&sev_cmd_mutex); + rc =sev_platform_init_locked (data, error); +mutex_unlock (&sev_cmd_mutex); + +return rc; +} +EXPORT_SYMBOL_GPL (sev_platform_init); + +static int sev_platform_shutdown_locked (int *error) +{ +int ret; + + ret =sev_do_cmd_locked (SEV_CMD_SHUTDOWN ,0 , error); +if (ret) +return ret; + + psp_master->sev_state =SEV_STATE_UNINIT ; +dev_dbg (psp_master->dev,"SEV firmware shutdown\n" ); + +return ret; +} + +int sev_platform_shutdown (int *error) +{ +if (error) + *error =0 ; + +return 0 ; +} +EXPORT_SYMBOL_GPL (sev_platform_shutdown); + +int sev_platform_status (struct sev_user_data_status *data,int *error) +{ +return sev_do_cmd (SEV_CMD_PLATFORM_STATUS , data, error); +} +EXPORT_SYMBOL_GPL (sev_platform_status); + +int sev_issue_cmd_external_user (struct file *filep,unsigned int cmd, +void *data,int *error) +{ +if (!filep || filep->f_op != &sev_fops) +return -EBADF ; + +return sev_do_cmd (cmd, data, error); +} +EXPORT_SYMBOL_GPL (sev_issue_cmd_external_user); + +int sev_guest_deactivate (struct sev_data_deactivate *data,int *error) +{ +return sev_do_cmd (SEV_CMD_DEACTIVATE , data, error); +} +EXPORT_SYMBOL_GPL (sev_guest_deactivate); + +int sev_guest_activate (struct sev_data_activate *data,int *error) +{ +return sev_do_cmd (SEV_CMD_ACTIVATE , data, error); +} +EXPORT_SYMBOL_GPL (sev_guest_activate); + +int sev_guest_decommission (struct sev_data_decommission *data,int *error) +{ +return sev_do_cmd (SEV_CMD_DECOMMISSION , data, error); +} +EXPORT_SYMBOL_GPL (sev_guest_decommission); + +int sev_guest_df_flush (int *error) +{ +return sev_do_cmd (SEV_CMD_DF_FLUSH ,0 , error); +} +EXPORT_SYMBOL_GPL (sev_guest_df_flush); + +static int sev_ops_init (struct psp_device *psp) +{ +struct device *dev = psp->dev; +int ret; + +/* +* SEV feature support can be detected on multiple devices but the SEV +* FW commands must be issued on the master. During probe, we do not +* know the master hence we create /dev/sev on the first device probe. +* sev_do_cmd() finds the right master device to which to issue the +* command to the firmware. +*/ +if (!misc_dev) { +struct miscdevice *misc; + + misc_dev =devm_kzalloc (dev,sizeof (*misc_dev),GFP_KERNEL ); +if (!misc_dev) +return -ENOMEM ; + + misc = &misc_dev->misc; + misc->minor =MISC_DYNAMIC_MINOR ; + misc->name =DEVICE_NAME ; + misc->fops = &sev_fops; + + ret =misc_register (misc); +if (ret) +return ret; + +kref_init (&misc_dev->refcount); + }else { +kref_get (&misc_dev->refcount); + } + +init_waitqueue_head (&psp->sev_int_queue); + psp->sev_misc = misc_dev; +dev_dbg (dev,"registered SEV device\n" ); + +return 0 ; +} + +static int sev_init (struct psp_device *psp) +{ +/* Check if device supports SEV feature */ +if (!(ioread32 (psp->io_regs +PSP_FEATURE_REG ) &1 )) { +dev_dbg (psp->dev,"device does not support SEV\n" ); +return 1 ; + } + +return sev_ops_init (psp); +} + +static void sev_exit (struct kref *ref) +{ + struct sev_misc_dev *misc_dev = container_of(ref, struct sev_misc_dev, refcount); + +misc_deregister (&misc_dev->misc); +} +int psp_dev_init (struct sp_device *sp) {struct device *dev = sp->dev;
@@ -84,11 +388,17 @@ int psp_dev_init(struct sp_device *sp)if (sp->set_psp_master_device) sp->set_psp_master_device (sp); + ret =sev_init (psp); +if (ret) + goto e_irq; +/* Enable interrupt */ iowrite32 (-1 , psp->io_regs +PSP_P2CMSG_INTEN );return 0 ; +e_irq: +sp_free_psp_irq (psp->sp, psp); e_err: sp->psp_data =NULL ;
@@ -101,5 +411,55 @@ void psp_dev_destroy(struct sp_device *sp) {struct psp_device *psp = sp->psp_data; +if (psp->sev_misc) +kref_put (&misc_dev->refcount, sev_exit); +sp_free_psp_irq (sp, psp); } + +void psp_pci_init (void ) +{ +struct sev_user_data_status *data; +struct sp_device *sp; +int error, rc; + + sp =sp_get_psp_master_device (); +if (!sp) +return ; + + psp_master = sp->psp_data; + +/* Initialize the platform */ + rc =sev_platform_init (NULL , &error); +if (rc) { +dev_err (sp->dev,"SEV: failed to INIT error %#x\n" , error); +return ; + } + +/* Display SEV firmware version */ + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return ; + + rc =sev_platform_status (data, &error); +if (rc) { +dev_err (sp->dev,"SEV: failed to get status error %#x\n" , error); + goto e_free; + } + +dev_info (sp->dev,"SEV API:%d.%d build:%d\n" , + data->api_major, data->api_minor, data->build); + +e_free: +kfree (data); +} + +void psp_pci_exit (void ) +{ +if (!psp_master) +return ; + +mutex_lock (&sev_cmd_mutex); +sev_platform_shutdown_locked (NULL ); +mutex_unlock (&sev_cmd_mutex); +}
drivers/crypto/ccp/psp-dev.h +22 -0
@@ -25,9 +25,21 @@#include <linux/interrupt.h> #include <linux/irqreturn.h> #include <linux/dmaengine.h> +#include <linux/psp-sev.h> +#include <linux/miscdevice.h> #include "sp-dev.h" +#define PSP_C2PMSG (_num) ((_num) << 2) +#define PSP_CMDRESP PSP_C2PMSG(32) +#define PSP_CMDBUFF_ADDR_LO PSP_C2PMSG(56) +#define PSP_CMDBUFF_ADDR_HI PSP_C2PMSG(57) +#define PSP_FEATURE_REG PSP_C2PMSG(63) + +#define PSP_P2CMSG (_num) ((_num) << 2) +#define PSP_CMD_COMPLETE_REG 1 +#define PSP_CMD_COMPLETE PSP_P2CMSG(PSP_CMD_COMPLETE_REG) +#define PSP_P2CMSG_INTEN 0x0110#define PSP_P2CMSG_INTSTS 0x0114
@@ -44,6 +56,11 @@#define MAX_PSP_NAME_LEN 16 +struct sev_misc_dev { +struct kref refcount; +struct miscdevice misc; +}; +struct psp_device {struct list_head entry;
@@ -54,6 +71,11 @@ struct psp_device {struct sp_device *sp;void __iomem *io_regs; + +int sev_state; +unsigned int sev_int_rcvd; +wait_queue_head_t sev_int_queue; + struct sev_misc_dev *sev_misc; };#endif /* __PSP_DEV_H */
drivers/crypto/ccp/sp-dev.c +9 -0
@@ -272,6 +272,10 @@ static int __init sp_mod_init(void)if (ret)return ret; +#ifdef CONFIG_CRYPTO_DEV_SP_PSP +psp_pci_init (); +#endif +return 0 ;#endif
@@ -291,6 +295,11 @@ static int __init sp_mod_init(void) static void __exit sp_mod_exit(void) {#ifdef CONFIG_X86 + +#ifdef CONFIG_CRYPTO_DEV_SP_PSP +psp_pci_exit (); +#endif +sp_pci_exit ();#endif
drivers/crypto/ccp/sp-dev.h +4 -0
@@ -143,12 +143,16 @@ static inline int ccp_dev_resume(struct sp_device *sp)#ifdef CONFIG_CRYPTO_DEV_SP_PSP int psp_dev_init (struct sp_device *sp); +void psp_pci_init (void );void psp_dev_destroy (struct sp_device *sp); +void psp_pci_exit (void );#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ static inline int psp_dev_init (struct sp_device *sp) {return 0 ; } +static inline void psp_pci_init (void ) { }static inline void psp_dev_destroy (struct sp_device *sp) { } +static inline void psp_pci_exit (void ) { }#endif /* CONFIG_CRYPTO_DEV_SP_PSP */
include/linux/psp-sev.h +158 -0
162 lines not on this page · raw message
Signature · 1 line
2.9.5
Re: [Part2 PATCH v6.1 16/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
I just realized that this should be marked as "PATCH v6.1 13/38 ...". I had some debug patch before this hence it was pushed below in the stack.
Quoted 715 lines · Brijesh Singh
AMD's new Secure Encrypted Virtualization (SEV) feature allows the memory contents of virtual machines to be transparently encrypted with a key unique to the VM. The programming and management of the encryption keys are handled by the AMD Secure Processor (AMD-SP) which exposes the commands for these tasks. The complete spec is available at:
http://support.amd.com/TechDocs/55766_SEV-KM%20API_Specification.pdf
Extend the AMD-SP driver to provide the following support:
- an in-kernel API to communicate with the SEV firmware. The API can be used by the hypervisor to create encryption context for a SEV guest.
- a userspace IOCTL to manage the platform certificates.
Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Borislav Petkov <[email protected]>Cc Herbert Xu <[email protected]>Cc Gary Hook <[email protected]>Cc Tom Lendacky <[email protected]>Improvements-by Borislav Petkov <[email protected]>Signed-off-by Brijesh Singh <[email protected]>---
Boris,
I have tried to minimize the INIT -> SHUTDOWN transition by keeping state information in
sev_statevariable. Since we INIT the platform during the modprobe time hence we no longer need the kref count and init mutex. Here are list of changes.Changes since v6:
* Add functions to init and shutdown firmware during modprobe
* Addsev_statevariable inpsp_deviceto keep track of the INIT and SHUTDOWN state
* Don't allow caller to shutdown the FW because SHUTDOWN will be done during the module removal.
* Drop thefw_init_mutexandinit_refcountbecause we no longer allow apps to INIT and UINIT the platformdrivers/crypto/ccp/psp-dev.c | 360 +++++++++++++++++++++++++++++++++++++++++++ drivers/crypto/ccp/psp-dev.h | 22 +++ drivers/crypto/ccp/sp-dev.c | 9 ++ drivers/crypto/ccp/sp-dev.h | 4 + include/linux/psp-sev.h | 158 +++++++++++++++++++ 5 files changed, 553 insertions(+)drivers/crypto/ccp/psp-dev.c +360
-0@@ -26,6 +26,15 @@#include "sp-dev.h" #include "psp-dev.h" +#define DEVICE_NAME "sev" + +static DEFINE_MUTEX (sev_cmd_mutex); +static struct sev_misc_dev *misc_dev; +static struct psp_device *psp_master; + +static int sev_platform_shutdown_locked (int *error); +static int sev_platform_init_locked (struct sev_data_init *data,int *error); +static struct psp_device *psp_alloc_struct (struct sp_device *sp) {struct device *dev = sp->dev;@@ -45,9 +54,304 @@ static struct psp_device *psp_alloc_struct(struct sp_device *sp)static irqreturn_t psp_irq_handler (int irq,void *data) { +struct psp_device *psp = data; +unsigned int status; +int reg; + +/* Read the interrupt status: */ + status =ioread32 (psp->io_regs +PSP_P2CMSG_INTSTS ); + +/* Check if it is command completion: */ +if (!(status &BIT (PSP_CMD_COMPLETE_REG ))) + goto done; + +/* Check if it is SEV command completion: */ + reg =ioread32 (psp->io_regs +PSP_CMDRESP ); +if (reg &PSP_CMDRESP_RESP ) { + psp->sev_int_rcvd =1 ; +wake_up (&psp->sev_int_queue); + } + +done: +/* Clear the interrupt status by writing the same value we read. */ +iowrite32 (status, psp->io_regs +PSP_P2CMSG_INTSTS ); +return IRQ_HANDLED ; } +static void sev_wait_cmd_ioc (struct psp_device *psp,unsigned int *reg) +{ + psp->sev_int_rcvd =0 ; + +wait_event (psp->sev_int_queue, psp->sev_int_rcvd); + *reg =ioread32 (psp->io_regs +PSP_CMDRESP ); +} + +static int sev_cmd_buffer_len (int cmd) +{ +switch (cmd) { +case SEV_CMD_INIT :return sizeof (struct sev_data_init ); +case SEV_CMD_PLATFORM_STATUS :return sizeof (struct sev_user_data_status ); +case SEV_CMD_PEK_CSR :return sizeof (struct sev_data_pek_csr ); +case SEV_CMD_PEK_CERT_IMPORT :return sizeof (struct sev_data_pek_cert_import ); +case SEV_CMD_PDH_CERT_EXPORT :return sizeof (struct sev_data_pdh_cert_export ); +case SEV_CMD_LAUNCH_START :return sizeof (struct sev_data_launch_start ); +case SEV_CMD_LAUNCH_UPDATE_DATA :return sizeof (struct sev_data_launch_update_data ); +case SEV_CMD_LAUNCH_UPDATE_VMSA :return sizeof (struct sev_data_launch_update_vmsa ); +case SEV_CMD_LAUNCH_FINISH :return sizeof (struct sev_data_launch_finish ); +case SEV_CMD_LAUNCH_MEASURE :return sizeof (struct sev_data_launch_measure ); +case SEV_CMD_ACTIVATE :return sizeof (struct sev_data_activate ); +case SEV_CMD_DEACTIVATE :return sizeof (struct sev_data_deactivate ); +case SEV_CMD_DECOMMISSION :return sizeof (struct sev_data_decommission ); +case SEV_CMD_GUEST_STATUS :return sizeof (struct sev_data_guest_status ); +case SEV_CMD_DBG_DECRYPT :return sizeof (struct sev_data_dbg ); +case SEV_CMD_DBG_ENCRYPT :return sizeof (struct sev_data_dbg ); +case SEV_CMD_SEND_START :return sizeof (struct sev_data_send_start ); +case SEV_CMD_SEND_UPDATE_DATA :return sizeof (struct sev_data_send_update_data ); +case SEV_CMD_SEND_UPDATE_VMSA :return sizeof (struct sev_data_send_update_vmsa ); +case SEV_CMD_SEND_FINISH :return sizeof (struct sev_data_send_finish ); +case SEV_CMD_RECEIVE_START :return sizeof (struct sev_data_receive_start ); +case SEV_CMD_RECEIVE_FINISH :return sizeof (struct sev_data_receive_finish ); +case SEV_CMD_RECEIVE_UPDATE_DATA :return sizeof (struct sev_data_receive_update_data ); +case SEV_CMD_RECEIVE_UPDATE_VMSA :return sizeof (struct sev_data_receive_update_vmsa ); +case SEV_CMD_LAUNCH_UPDATE_SECRET :return sizeof (struct sev_data_launch_secret ); +default :return 0 ; + } + +return 0 ; +} + +static int sev_do_cmd_locked (int cmd,void *data,int *psp_ret) +{ +struct psp_device *psp = psp_master; +unsigned int phys_lsb, phys_msb; +unsigned int reg, ret =0 ; + +if (!psp) +return -ENODEV ; + +/* Get the physical address of the command buffer */ + phys_lsb = data ?lower_32_bits (__psp_pa (data)) :0 ; + phys_msb = data ?upper_32_bits (__psp_pa (data)) :0 ; + +dev_dbg (psp->dev,"sev command id %#x buffer 0x%08x%08x\n" , + cmd, phys_msb, phys_lsb); + +print_hex_dump_debug ("(in): " ,DUMP_PREFIX_OFFSET ,16 ,2 , data, +sev_cmd_buffer_len (cmd), false); + +iowrite32 (phys_lsb, psp->io_regs +PSP_CMDBUFF_ADDR_LO ); +iowrite32 (phys_msb, psp->io_regs +PSP_CMDBUFF_ADDR_HI ); + + reg = cmd; + reg <<=PSP_CMDRESP_CMD_SHIFT ; + reg |=PSP_CMDRESP_IOC ; +iowrite32 (reg, psp->io_regs +PSP_CMDRESP ); + +/* wait for command completion */ +sev_wait_cmd_ioc (psp, ®); + +if (psp_ret) + *psp_ret = reg &PSP_CMDRESP_ERR_MASK ; + +if (reg &PSP_CMDRESP_ERR_MASK ) { +dev_dbg (psp->dev,"sev command %#x failed (%#010x)\n" , + cmd, reg &PSP_CMDRESP_ERR_MASK ); + ret = -EIO ; + } + +print_hex_dump_debug ("(out): " ,DUMP_PREFIX_OFFSET ,16 ,2 , data, +sev_cmd_buffer_len (cmd), false); + +return ret; +} + +static int sev_do_cmd (int cmd,void *data,int *psp_ret) +{ +int rc; + +mutex_lock (&sev_cmd_mutex); + rc =sev_do_cmd_locked (cmd, data, psp_ret); +mutex_unlock (&sev_cmd_mutex); + +return rc; +} + +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) +{ +return -ENOTTY ; +} + +static const struct file_operations sev_fops = { + .owner =THIS_MODULE , + .unlocked_ioctl = sev_ioctl, +}; + +static int sev_platform_init_locked (struct sev_data_init *data,int *error) +{ +struct psp_device *psp = psp_master; +struct sev_data_init *input =NULL ; +int rc =0 ; + +if (!psp) +return -ENODEV ; + +if (psp->sev_state ==SEV_STATE_INIT ) +return 0 ; + +if (!data) { + input =kzalloc (sizeof (*input),GFP_KERNEL ); +if (!input) +return -ENOMEM ; + + data = input; + } + + rc =sev_do_cmd_locked (SEV_CMD_INIT , data, error); +if (rc) + goto e_free; + + psp->sev_state =SEV_STATE_INIT ; +dev_dbg (psp->dev,"SEV firmware intialized\n" ); + +e_free: +kfree (input); +return rc; +} + +int sev_platform_init (struct sev_data_init *data,int *error) +{ +int rc; + +mutex_lock (&sev_cmd_mutex); + rc =sev_platform_init_locked (data, error); +mutex_unlock (&sev_cmd_mutex); + +return rc; +} +EXPORT_SYMBOL_GPL (sev_platform_init); + +static int sev_platform_shutdown_locked (int *error) +{ +int ret; + + ret =sev_do_cmd_locked (SEV_CMD_SHUTDOWN ,0 , error); +if (ret) +return ret; + + psp_master->sev_state =SEV_STATE_UNINIT ; +dev_dbg (psp_master->dev,"SEV firmware shutdown\n" ); + +return ret; +} + +int sev_platform_shutdown (int *error) +{ +if (error) + *error =0 ; + +return 0 ; +} +EXPORT_SYMBOL_GPL (sev_platform_shutdown); + +int sev_platform_status (struct sev_user_data_status *data,int *error) +{ +return sev_do_cmd (SEV_CMD_PLATFORM_STATUS , data, error); +} +EXPORT_SYMBOL_GPL (sev_platform_status); + +int sev_issue_cmd_external_user (struct file *filep,unsigned int cmd, +void *data,int *error) +{ +if (!filep || filep->f_op != &sev_fops) +return -EBADF ; + +return sev_do_cmd (cmd, data, error); +} +EXPORT_SYMBOL_GPL (sev_issue_cmd_external_user); + +int sev_guest_deactivate (struct sev_data_deactivate *data,int *error) +{ +return sev_do_cmd (SEV_CMD_DEACTIVATE , data, error); +} +EXPORT_SYMBOL_GPL (sev_guest_deactivate); + +int sev_guest_activate (struct sev_data_activate *data,int *error) +{ +return sev_do_cmd (SEV_CMD_ACTIVATE , data, error); +} +EXPORT_SYMBOL_GPL (sev_guest_activate); + +int sev_guest_decommission (struct sev_data_decommission *data,int *error) +{ +return sev_do_cmd (SEV_CMD_DECOMMISSION , data, error); +} +EXPORT_SYMBOL_GPL (sev_guest_decommission); + +int sev_guest_df_flush (int *error) +{ +return sev_do_cmd (SEV_CMD_DF_FLUSH ,0 , error); +} +EXPORT_SYMBOL_GPL (sev_guest_df_flush); + +static int sev_ops_init (struct psp_device *psp) +{ +struct device *dev = psp->dev; +int ret; + +/* +* SEV feature support can be detected on multiple devices but the SEV +* FW commands must be issued on the master. During probe, we do not +* know the master hence we create /dev/sev on the first device probe. +* sev_do_cmd() finds the right master device to which to issue the +* command to the firmware. +*/ +if (!misc_dev) { +struct miscdevice *misc; + + misc_dev =devm_kzalloc (dev,sizeof (*misc_dev),GFP_KERNEL ); +if (!misc_dev) +return -ENOMEM ; + + misc = &misc_dev->misc; + misc->minor =MISC_DYNAMIC_MINOR ; + misc->name =DEVICE_NAME ; + misc->fops = &sev_fops; + + ret =misc_register (misc); +if (ret) +return ret; + +kref_init (&misc_dev->refcount); + }else { +kref_get (&misc_dev->refcount); + } + +init_waitqueue_head (&psp->sev_int_queue); + psp->sev_misc = misc_dev; +dev_dbg (dev,"registered SEV device\n" ); + +return 0 ; +} + +static int sev_init (struct psp_device *psp) +{ +/* Check if device supports SEV feature */ +if (!(ioread32 (psp->io_regs +PSP_FEATURE_REG ) &1 )) { +dev_dbg (psp->dev,"device does not support SEV\n" ); +return 1 ; + } + +return sev_ops_init (psp); +} + +static void sev_exit (struct kref *ref) +{ + struct sev_misc_dev *misc_dev = container_of(ref, struct sev_misc_dev, refcount); + +misc_deregister (&misc_dev->misc); +} +int psp_dev_init (struct sp_device *sp) {struct device *dev = sp->dev;@@ -84,11 +388,17 @@ int psp_dev_init(struct sp_device *sp)if (sp->set_psp_master_device) sp->set_psp_master_device (sp); + ret =sev_init (psp); +if (ret) + goto e_irq; +/* Enable interrupt */ iowrite32 (-1 , psp->io_regs +PSP_P2CMSG_INTEN );return 0 ; +e_irq: +sp_free_psp_irq (psp->sp, psp); e_err: sp->psp_data =NULL ;@@ -101,5 +411,55 @@ void psp_dev_destroy(struct sp_device *sp) {struct psp_device *psp = sp->psp_data; +if (psp->sev_misc) +kref_put (&misc_dev->refcount, sev_exit); +sp_free_psp_irq (sp, psp); } + +void psp_pci_init (void ) +{ +struct sev_user_data_status *data; +struct sp_device *sp; +int error, rc; + + sp =sp_get_psp_master_device (); +if (!sp) +return ; + + psp_master = sp->psp_data; + +/* Initialize the platform */ + rc =sev_platform_init (NULL , &error); +if (rc) { +dev_err (sp->dev,"SEV: failed to INIT error %#x\n" , error); +return ; + } + +/* Display SEV firmware version */ + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return ; + + rc =sev_platform_status (data, &error); +if (rc) { +dev_err (sp->dev,"SEV: failed to get status error %#x\n" , error); + goto e_free; + } + +dev_info (sp->dev,"SEV API:%d.%d build:%d\n" , + data->api_major, data->api_minor, data->build); + +e_free: +kfree (data); +} + +void psp_pci_exit (void ) +{ +if (!psp_master) +return ; + +mutex_lock (&sev_cmd_mutex); +sev_platform_shutdown_locked (NULL ); +mutex_unlock (&sev_cmd_mutex); +}drivers/crypto/ccp/psp-dev.h +22
-0@@ -25,9 +25,21 @@#include <linux/interrupt.h> #include <linux/irqreturn.h> #include <linux/dmaengine.h> +#include <linux/psp-sev.h> +#include <linux/miscdevice.h> #include "sp-dev.h" +#define PSP_C2PMSG (_num) ((_num) << 2) +#define PSP_CMDRESP PSP_C2PMSG(32) +#define PSP_CMDBUFF_ADDR_LO PSP_C2PMSG(56) +#define PSP_CMDBUFF_ADDR_HI PSP_C2PMSG(57) +#define PSP_FEATURE_REG PSP_C2PMSG(63) + +#define PSP_P2CMSG (_num) ((_num) << 2) +#define PSP_CMD_COMPLETE_REG 1 +#define PSP_CMD_COMPLETE PSP_P2CMSG(PSP_CMD_COMPLETE_REG) +#define PSP_P2CMSG_INTEN 0x0110#define PSP_P2CMSG_INTSTS 0x0114@@ -44,6 +56,11 @@#define MAX_PSP_NAME_LEN 16 +struct sev_misc_dev { +struct kref refcount; +struct miscdevice misc; +}; +struct psp_device {struct list_head entry;@@ -54,6 +71,11 @@ struct psp_device {struct sp_device *sp;void __iomem *io_regs; + +int sev_state; +unsigned int sev_int_rcvd; +wait_queue_head_t sev_int_queue; + struct sev_misc_dev *sev_misc; };#endif /* __PSP_DEV_H */ drivers/crypto/ccp/sp-dev.c +9
-0@@ -272,6 +272,10 @@ static int __init sp_mod_init(void)if (ret)return ret; +#ifdef CONFIG_CRYPTO_DEV_SP_PSP +psp_pci_init (); +#endif +return 0 ;#endif @@ -291,6 +295,11 @@ static int __init sp_mod_init(void) static void __exit sp_mod_exit(void) {#ifdef CONFIG_X86 + +#ifdef CONFIG_CRYPTO_DEV_SP_PSP +psp_pci_exit (); +#endif +sp_pci_exit ();#endif drivers/crypto/ccp/sp-dev.h +4
-0@@ -143,12 +143,16 @@ static inline int ccp_dev_resume(struct sp_device *sp)#ifdef CONFIG_CRYPTO_DEV_SP_PSP int psp_dev_init (struct sp_device *sp); +void psp_pci_init (void );void psp_dev_destroy (struct sp_device *sp); +void psp_pci_exit (void );#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ static inline int psp_dev_init (struct sp_device *sp) {return 0 ; } +static inline void psp_pci_init (void ) { }static inline void psp_dev_destroy (struct sp_device *sp) { } +static inline void psp_pci_exit (void ) { }#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ include/linux/psp-sev.h +158
-0@@ -491,4 +491,162 @@ struct sev_data_dbg {u32 len;/* In */ } __packed; +#ifdef CONFIG_CRYPTO_DEV_SP_PSP + +/** +* sev_platform_init - perform SEV INIT command +* +* @init: sev_data_init structure to be processed +* @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_platform_init (struct sev_data_init *init,int *error); + +/** +* sev_platform_shutdown - perform SEV SHUTDOWN 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_platform_shutdown (int *error); + +/** +* sev_platform_status - perform SEV PLATFORM_STATUS command +* +* @status: sev_user_data_status structure to be processed +* @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_platform_status (struct sev_user_data_status *status,int *error); + +/** +* sev_issue_cmd_external_user - issue SEV command by other driver with a file +* handle. +* +* This function can be used by other drivers to issue a SEV command on +* behalf of userspace. The caller must pass a valid SEV file descriptor +* so that we know that it has access to SEV device. +* +* @filep - SEV device file pointer +* @cmd - command to issue +* @data - command buffer +* @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 +* -%EINVAL if the SEV file descriptor is not valid +*/ +int sev_issue_cmd_external_user (struct file *filep,unsigned int id, +void *data,int *error); + +/** +* sev_guest_deactivate - perform SEV DEACTIVATE command +* +* @deactivate: sev_data_deactivate structure to be processed +* @sev_ret: sev command return code +* +* Returns: +* 0 if the sev successfully processed the command +* -%ENODEV if the sev device is not available +* -%ENOTSUPP if the sev does not support SEV +* -%ETIMEDOUT if the sev command timed out +* -%EIO if the sev returned a non-zero return code +*/ +int sev_guest_deactivate (struct sev_data_deactivate *data,int *error); + +/** +* sev_guest_activate - perform SEV ACTIVATE command +* +* @activate: sev_data_activate structure to be processed +* @sev_ret: sev command return code +* +* Returns: +* 0 if the sev successfully processed the command +* -%ENODEV if the sev device is not available +* -%ENOTSUPP if the sev does not support SEV +* -%ETIMEDOUT if the sev command timed out +* -%EIO if the sev returned a non-zero return code +*/ +int sev_guest_activate (struct sev_data_activate *data,int *error); + +/** +* sev_guest_df_flush - perform SEV DF_FLUSH command +* +* @sev_ret: sev command return code +* +* Returns: +* 0 if the sev successfully processed the command +* -%ENODEV if the sev device is not available +* -%ENOTSUPP if the sev does not support SEV +* -%ETIMEDOUT if the sev command timed out +* -%EIO if the sev returned a non-zero return code +*/ +int sev_guest_df_flush (int *error); + +/** +* sev_guest_decommission - perform SEV DECOMMISSION command +* +* @decommission: sev_data_decommission structure to be processed +* @sev_ret: sev command return code +* +* Returns: +* 0 if the sev successfully processed the command +* -%ENODEV if the sev device is not available +* -%ENOTSUPP if the sev does not support SEV +* -%ETIMEDOUT if the sev command timed out +* -%EIO if the sev returned a non-zero return code +*/ +int sev_guest_decommission (struct sev_data_decommission *data,int *error); + +#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ + +static inline int +sev_platform_status (struct sev_user_data_status *status,int *error) {return -ENODEV ; } + +static inline int +sev_platform_init (struct sev_data_init *init,int *error) {return -ENODEV ; } + +static inline int sev_platform_shutdown (int *error) {return -ENODEV ; } + +static inline int +sev_guest_deactivate (struct sev_data_deactivate *data,int *error) {return -ENODEV ; } + +static inline int +sev_guest_decommission (struct sev_data_decommission *data,int *error) {return -ENODEV ; } + +static inline int +sev_guest_activate (struct sev_data_activate *data,int *error) {return -ENODEV ; } + +static inline int sev_guest_df_flush (int *error) {return -ENODEV ; } + +static inline int +sev_issue_cmd_external_user (struct file *filep, +unsigned int id,void *data,int *error) +{ +return -ENODEV ; +} + +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ +#endif /* __PSP_SEV_H__ */
Borislav Petkov
maintainerRe: [Part2 PATCH v6.1 16/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
In reply to Brijesh SinghQuoted 5 lines · Brijesh Singh
AMD's new Secure Encrypted Virtualization (SEV) feature allows the memory contents of virtual machines to be transparently encrypted with a key unique to the VM. The programming and management of the encryption keys are handled by the AMD Secure Processor (AMD-SP) which exposes the commands for these tasks. The complete spec is available at:
...
Quoted 8 lines
Changes since v6:
* Add functions to init and shutdown firmware during modprobe
* Addsev_statevariable inpsp_deviceto keep track of the INIT and SHUTDOWN state
* Don't allow caller to shutdown the FW because SHUTDOWN will be done during the module removal.
* Drop thefw_init_mutexandinit_refcountbecause we no longer allow apps to INIT and UINIT the platform
Yes, it definitely looks much better this way. Thanks for doing that!
Quoted 14 lines
drivers/crypto/ccp/psp-dev.c | 360 +++++++++++++++++++++++++++++++++++++++++++ drivers/crypto/ccp/psp-dev.h | 22 +++ drivers/crypto/ccp/sp-dev.c | 9 ++ drivers/crypto/ccp/sp-dev.h | 4 + include/linux/psp-sev.h | 158 +++++++++++++++++++ 5 files changed, 553 insertions(+)drivers/crypto/ccp/psp-dev.c +0
-0@@ -26,6 +26,15 @@#include "sp-dev.h"
drivers/crypto/ccp/psp-dev.c line 36
Quoted 10 lines
drivers/crypto/ccp/psp-dev.c +8
-0#include "psp-dev.h" +#define DEVICE_NAME "sev" + +static DEFINE_MUTEX (sev_cmd_mutex); +static struct sev_misc_dev *misc_dev; +static struct psp_device *psp_master; + +static int sev_platform_shutdown_locked (int *error); +static int sev_platform_init_locked (struct sev_data_init *data,int *error);
Useless forward declarations.
Quoted 3 lines
drivers/crypto/ccp/psp-dev.c +0
-0static struct psp_device *psp_alloc_struct (struct sp_device *sp) {struct device *dev = sp->dev;
...
Quoted 1 line
+static int
sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
You can use the "__" prefix to denote that it is a lower-level helper:
__sev_do_cmd__sev_do_cmd_locked
Ditto for the other locked functions.
Quoted 42 lines
drivers/crypto/ccp/psp-dev.c +42
-0+struct psp_device *psp = psp_master; +unsigned int phys_lsb, phys_msb; +unsigned int reg, ret =0 ; + +if (!psp) +return -ENODEV ; + +/* Get the physical address of the command buffer */ + phys_lsb = data ?lower_32_bits (__psp_pa (data)) :0 ; + phys_msb = data ?upper_32_bits (__psp_pa (data)) :0 ; + +dev_dbg (psp->dev,"sev command id %#x buffer 0x%08x%08x\n" , + cmd, phys_msb, phys_lsb); + +print_hex_dump_debug ("(in): " ,DUMP_PREFIX_OFFSET ,16 ,2 , data, +sev_cmd_buffer_len (cmd), false); + +iowrite32 (phys_lsb, psp->io_regs +PSP_CMDBUFF_ADDR_LO ); +iowrite32 (phys_msb, psp->io_regs +PSP_CMDBUFF_ADDR_HI ); + + reg = cmd; + reg <<=PSP_CMDRESP_CMD_SHIFT ; + reg |=PSP_CMDRESP_IOC ; +iowrite32 (reg, psp->io_regs +PSP_CMDRESP ); + +/* wait for command completion */ +sev_wait_cmd_ioc (psp, ®); + +if (psp_ret) + *psp_ret = reg &PSP_CMDRESP_ERR_MASK ; + +if (reg &PSP_CMDRESP_ERR_MASK ) { +dev_dbg (psp->dev,"sev command %#x failed (%#010x)\n" , + cmd, reg &PSP_CMDRESP_ERR_MASK ); + ret = -EIO ; + } + +print_hex_dump_debug ("(out): " ,DUMP_PREFIX_OFFSET ,16 ,2 , data, +sev_cmd_buffer_len (cmd), false); + +return ret; +}
...
Quoted 9 lines
drivers/crypto/ccp/psp-dev.c +9
-0+static int sev_platform_init_locked (struct sev_data_init *data,int *error) +{ +struct psp_device *psp = psp_master; +struct sev_data_init *input =NULL ; +int rc =0 ; + +if (!psp) +return -ENODEV ; +
drivers/crypto/ccp/psp-dev.c static struct psp_device *psp_alloc_struct(struct sp_device *sp) · line 208
Quoted 10 lines
drivers/crypto/ccp/psp-dev.c +10
-0+if (psp->sev_state ==SEV_STATE_INIT ) +return 0 ; + +if (!data) { + input =kzalloc (sizeof (*input),GFP_KERNEL ); +if (!input) +return -ENOMEM ; + + data = input; + }
You can do the allocation in the enclosing function, outside of the critical region so that you can keep it shorter.
Or even better: if you're going to synchronize the commands with a mutex, you can define a static struct sev_data_init input in this file which you always hand in and then you can save yourself the kmalloc calls.
drivers/crypto/ccp/psp-dev.c static struct psp_device *psp_alloc_struct(struct sp_device *sp) · line 215
Quoted 7 lines
drivers/crypto/ccp/psp-dev.c +7
-0+ + rc =sev_do_cmd_locked (SEV_CMD_INIT , data, error); +if (rc) + goto e_free; + + psp->sev_state =SEV_STATE_INIT ; +dev_dbg (psp->dev,"SEV firmware intialized\n" );
WARNING: 'intialized' may be misspelled - perhaps 'initialized'? #254: FILE: drivers/crypto/ccp/psp-dev.c:215:
+ dev_dbg(psp->dev, "SEV firmware intialized\n");
WARNING: space prohibited between function name and open parenthesis '(' #445: FILE: drivers/crypto/ccp/psp-dev.c:440:
+ data = kzalloc(sizeof (*data), GFP_KERNEL);
Ok, tell me: how many times do I have to write:
"Please integrate scripts/checkpatch.pl into your patch creation workflow. Some of the warnings/errors *actually* make sense."
until you actually do it?
Quoted 29 lines
drivers/crypto/ccp/psp-dev.c +29
-0+ +e_free: +kfree (input); +return rc; +} + +int sev_platform_init (struct sev_data_init *data,int *error) +{ +int rc; + +mutex_lock (&sev_cmd_mutex); + rc =sev_platform_init_locked (data, error); +mutex_unlock (&sev_cmd_mutex); + +return rc; +} +EXPORT_SYMBOL_GPL (sev_platform_init); + +static int sev_platform_shutdown_locked (int *error) +{ +int ret; + + ret =sev_do_cmd_locked (SEV_CMD_SHUTDOWN ,0 , error); +if (ret) +return ret; + + psp_master->sev_state =SEV_STATE_UNINIT ; +dev_dbg (psp_master->dev,"SEV firmware shutdown\n" ); +
drivers/crypto/ccp/psp-dev.c static struct psp_device *psp_alloc_struct(struct sp_device *sp) · line 254
Quoted 10 lines
drivers/crypto/ccp/psp-dev.c +10
-0+return ret; +} + +int sev_platform_shutdown (int *error) +{ +if (error) + *error =0 ; + +return 0 ; +}
I'm guessing that that's just bare-bones and it will get filled up in the next patches. Otherwise it looks pretty useless.
If it is just to block the user from sending SHUTDOWN to the PSP master, just do that in the ioctl directly - no need to call some empty functions.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [Part2 PATCH v6.1 16/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
On 10/30/2017 12:21 PM, Borislav Petkov wrote:
...
Quoted 3 lines
Useless forward declarations.
Actually its helpful in other patches. I was trying to avoid making too many code movement in other patches to eliminate the forward declarations. I guess I can fix in v7.
Quoted 6 lines
Quoted 3 lines
static struct psp_device *psp_alloc_struct(struct sp_device *sp) { struct device *dev = sp->dev;...
Quoted 8 lines
Quoted 1 line
+static int
sev_do_cmd_locked(int cmd, void *data, int *psp_ret)You can use the "__" prefix to denote that it is a lower-level helper:
__sev_do_cmd__sev_do_cmd_lockedDitto for the other locked functions.
noted
Quoted 25 lines
...
Quoted 9 lines
drivers/crypto/ccp/psp-dev.c +9
-0+static int sev_platform_init_locked (struct sev_data_init *data,int *error) +{ +struct psp_device *psp = psp_master; +struct sev_data_init *input =NULL ; +int rc =0 ; + +if (!psp) +return -ENODEV ; +drivers/crypto/ccp/psp-dev.c static struct psp_device *psp_alloc_struct(struct sp_device *sp) · line 208
Quoted 10 lines
drivers/crypto/ccp/psp-dev.c +10
-0+if (psp->sev_state ==SEV_STATE_INIT ) +return 0 ; + +if (!data) { + input =kzalloc (sizeof (*input),GFP_KERNEL ); +if (!input) +return -ENOMEM ; + + data = input; + }You can do the allocation in the enclosing function, outside of the critical region so that you can keep it shorter.
Quoted 5 lines
Or even better: if you're going to synchronize the commands with a mutex, you can define a static struct
sev_data_initinput in this file which you always hand in and then you can save yourself the kmalloc calls.
If the buffer is allocated on the stack then there is no guarantee that __pa() will gives us a valid physical address. IIRC, when CONFIG_VMAP_STACK=y then stack space is mapped similar to vmalloc'd storage and __pa() will not work.
Since we need to pass the physical address to PSP hence variable allocated on the stack will not work.
I can certainly move the allocation outside, but then it may increase the code size in other functions. If its not a big deal then I would prefer to keep what we have.
...
Quoted 12 lines
drivers/crypto/ccp/psp-dev.c static struct psp_device *psp_alloc_struct(struct sp_device *sp) · line 254
Quoted 8 lines
drivers/crypto/ccp/psp-dev.c +8
-0+ +int sev_platform_shutdown (int *error) +{ +if (error) + *error =0 ; + +return 0 ; +}I'm guessing that that's just bare-bones and it will get filled up in the next patches. Otherwise it looks pretty useless.
Well, we are not expanding in other patches. I was also debating on what to do with this function. Since we need sev_platform_init() hence it made sense to add sev_platform_shutdown() as well. If we add the function then I wanted to make sure that we set the *error = SUCCESS so that caller knows that function succeeded.
Quoted 4 lines
If it is just to block the user from sending SHUTDOWN to the PSP master, just do that in the ioctl directly - no need to call some empty functions.
The function is not used by userspace ioctl, its used by kvm drv when it launch/terminates the SEV guest.
-Brijesh
Borislav Petkov
maintainerRe: [Part2 PATCH v6.1 16/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
Quoted 1 line · Brijesh Singh
If the buffer is allocated on the stack then there is no guarantee that
static global is not allocated on the stack.
Quoted 3 lines
I can certainly move the allocation outside, but then it may increase the code size in other functions. If its not a big deal then I would prefer to keep what we have.
Avoiding repeated k*alloc calls is always a good thing. Actually kmalloc-ing 20 bytes each time sounds like it is not worth the calling overhead to me.
Quoted 2 lines
The function is not used by userspace ioctl, its used by kvm drv when it launch/terminates the SEV guest.
Just do that directly in the ioctl instead of having a dumb function.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [Part2 PATCH v6.1 16/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
Quoted 3 lines · Borislav Petkov
Quoted 1 line · Brijesh Singh
If the buffer is allocated on the stack then there is no guarantee that
static global is not allocated on the stack.
Okay, Just tried static global with CONFIG_VMAP_STACK=y and I am getting wrong physical address with __pa. PSP command fails with error code "INVALID_ADDRESS". The same thing works fine with kmalloc() buffer.
Quoted 6 lines
Quoted 3 lines
I can certainly move the allocation outside, but then it may increase the code size in other functions. If its not a big deal then I would prefer to keep what we have.
Avoiding repeated k*alloc calls is always a good thing. Actually kmalloc-ing 20 bytes each time sounds like it is not worth the calling overhead to me.
To avoid repeated k*alloc calls, I could devm_kzalloc() these variable during sev_init() and reuse them when needed.
Borislav Petkov
maintainerRe: [Part2 PATCH v6.1 16/38] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
Quoted 3 lines · Brijesh Singh
Okay, Just tried static global with
CONFIG_VMAP_STACK=yand I am getting wrong physical address with__pa. PSP command fails with error code "INVALID_ADDRESS". The same thing works fine withkmalloc()buffer.
Ah, right, module space is vmalloc-ed.
Quoted 2 lines
To avoid repeated k*alloc calls, I could
devm_kzalloc()these variable duringsev_init()and reuse them when needed.
Yap, something like that.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.PATCH v6 14/38 crypto: ccp: Implement SEV_FACTORY_RESET ioctl command
In reply to Brijesh SinghThe SEV_FACTORY_RESET command can be used by the platform owner to reset the non-volatile SEV related data. The command is defined in SEV spec section 5.4
--- drivers/crypto/ccp/psp-dev.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-)
drivers/crypto/ccp/psp-dev.c +27 -1
@@ -176,7 +176,33 @@ static int sev_do_cmd(int cmd, void *data, int *psp_ret)static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) { -return -ENOTTY ; +void __user *argp = (void __user *)arg; +struct sev_issue_cmd input; +int ret = -EFAULT ; + +if (ioctl !=SEV_ISSUE_CMD ) +return -EINVAL ; + +if (copy_from_user (&input, argp,sizeof (struct sev_issue_cmd ))) +return -EFAULT ; + +if (input.cmd >SEV_MAX ) +return -EINVAL ; + +switch (input.cmd) { + +case SEV_FACTORY_RESET : + ret =sev_do_cmd (SEV_CMD_FACTORY_RESET ,0 , &input.error); +break ; +default : + ret = -EINVAL ; + goto out; + } + +if (copy_to_user (argp, &input,sizeof (struct sev_issue_cmd ))) + ret = -EFAULT ; +out: +return ret; }static const struct file_operations sev_fops = {
Signature · 1 line
2.9.5
Re: [Part2 PATCH v6 14/38] crypto: ccp: Implement SEV_FACTORY_RESET ioctl command
Quoted 15 lines · Brijesh Singh
The
SEV_FACTORY_RESETcommand can be used by the platform owner to reset the non-volatile SEV related data. The command is defined in SEV spec section 5.4Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Borislav Petkov <[email protected]>Cc Herbert Xu <[email protected]>Cc Gary Hook <[email protected]>Cc Tom Lendacky <[email protected]>Improvements-by Borislav Petkov <[email protected]>Signed-off-by Brijesh Singh <[email protected]>
Quoted 3 lines
--- drivers/crypto/ccp/psp-dev.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-)
Signature · 4 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.Re: [Part2 PATCH v6 14/38] crypto: ccp: Implement SEV_FACTORY_RESET ioctl command
In reply to Brijesh SinghQuoted 4 lines · Brijesh Singh
The
SEV_FACTORY_RESETcommand can be used by the platform owner to reset the non-volatile SEV related data. The command is defined in SEV spec section 5.4
Quoted 11 lines
Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Borislav Petkov <[email protected]>Cc Herbert Xu <[email protected]>Cc Gary Hook <[email protected]>Cc Tom Lendacky <[email protected]>Improvements-by Borislav Petkov <[email protected]>Signed-off-by Brijesh Singh <[email protected]>
Quoted 44 lines
--- drivers/crypto/ccp/psp-dev.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-)drivers/crypto/ccp/psp-dev.c +27
-1@@ -176,7 +176,33 @@ static int sev_do_cmd(int cmd, void *data, int *psp_ret)static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) { -return -ENOTTY ; +void __user *argp = (void __user *)arg; +struct sev_issue_cmd input; +int ret = -EFAULT ; + +if (ioctl !=SEV_ISSUE_CMD ) +return -EINVAL ; + +if (copy_from_user (&input, argp,sizeof (struct sev_issue_cmd ))) +return -EFAULT ; + +if (input.cmd >SEV_MAX ) +return -EINVAL ; + +switch (input.cmd) { + +case SEV_FACTORY_RESET : + ret =sev_do_cmd (SEV_CMD_FACTORY_RESET ,0 , &input.error); +break ; +default : + ret = -EINVAL ; + goto out; + } + +if (copy_to_user (argp, &input,sizeof (struct sev_issue_cmd ))) + ret = -EFAULT ; +out: +return ret; }static const struct file_operations sev_fops = {
PATCH v6 14/38 crypto: ccp: Implement SEV_FACTORY_RESET ioctl command
In reply to Brijesh SinghThe SEV_FACTORY_RESET command can be used by the platform owner to reset the non-volatile SEV related data. The command is defined in SEV spec section 5.4
---
Boris,
It may look confusing that we call PLATFORM_STATUS command to check the current FW state even when we keep the state in psp->sev_state. Per spec, PLATFORM_INIT transitions FW from UINIT -> INIT and SHUTDOWN from <ANY_STATE> -> UINIT but there are multiple commands which can transition FW from INIT -> WORKING state. Hence my thinking is, if we really need to know whether we are in WORKING state then invoke PLATFORM_STATUS. So far, FACTORY_RESET is where we need to know if we are in WORKING state to avoid shutdown the FW. In real world app this command may not be used that often hence I don't feel like adding more complexity to the code.
Changes since v6:
* If FW is in WORKING state then reject the command
* If FW is in INIT state then shutdown before issuing the command
drivers/crypto/ccp/psp-dev.c | 77 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-)
drivers/crypto/ccp/psp-dev.c +76 -1
@@ -177,9 +177,84 @@ static int sev_do_cmd(int cmd, void *data, int *psp_ret)return rc; } +static int sev_platform_state (int *state,int *error) +{ +struct sev_user_data_status *data; +int rc; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + rc =sev_do_cmd_locked (SEV_CMD_PLATFORM_STATUS , data, error); +if (rc) + goto e_free; + + *state = data->state; + +e_free: +kfree (data); +return rc; +} + +static int sev_ioctl_do_reset (struct sev_issue_cmd *argp) +{ +int state, rc; + + rc =sev_platform_state (&state, &argp->error); +if (rc) +return rc; + +if (state ==SEV_STATE_WORKING ) { + argp->error =SEV_RET_INVALID_PLATFORM_STATE ; +return -EBUSY ; + } + +if (state ==SEV_STATE_INIT ) { + rc =sev_platform_shutdown_locked (&argp->error); +if (rc) +return rc; + } + +return sev_do_cmd_locked (SEV_CMD_FACTORY_RESET ,0 , &argp->error); +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) { -return -ENOTTY ; +void __user *argp = (void __user *)arg; +struct sev_issue_cmd input; +int ret = -EFAULT ; + +if (!psp_master) +return -ENODEV ; + +if (ioctl !=SEV_ISSUE_CMD ) +return -EINVAL ; + +if (copy_from_user (&input, argp,sizeof (struct sev_issue_cmd ))) +return -EFAULT ; + +if (input.cmd >SEV_MAX ) +return -EINVAL ; + +mutex_lock (&sev_cmd_mutex); + +switch (input.cmd) { + +case SEV_FACTORY_RESET : + ret =sev_ioctl_do_reset (&input); +break ; +default : + ret = -EINVAL ; + goto out; + } + +if (copy_to_user (argp, &input,sizeof (struct sev_issue_cmd ))) + ret = -EFAULT ; +out: +mutex_unlock (&sev_cmd_mutex); + +return ret; }static const struct file_operations sev_fops = {
Signature · 1 line
2.9.5
PATCH v6 15/38 crypto: ccp: Implement SEV_PLATFORM_STATUS ioctl command
In reply to Brijesh SinghThe SEV_PLATFORM_STATUS command can be used by the platform owner to get the current status of the platform. The command is defined in SEV spec section 5.5.
--- drivers/crypto/ccp/psp-dev.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
drivers/crypto/ccp/psp-dev.c +24 -0
@@ -174,6 +174,27 @@ static int sev_do_cmd(int cmd, void *data, int *psp_ret)return ret; } +static int sev_ioctl_do_platform_status (struct sev_issue_cmd *argp) +{ +struct sev_user_data_status *data; +int ret; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + ret =sev_do_cmd (SEV_CMD_PLATFORM_STATUS , data, &argp->error); +if (ret) + goto e_free; + +if (copy_to_user ((void __user *)argp->data, data,sizeof (*data))) + ret = -EFAULT ; + +e_free: +kfree (data); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;
@@ -194,6 +215,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_FACTORY_RESET : ret =sev_do_cmd (SEV_CMD_FACTORY_RESET ,0 , &input.error);break ; +case SEV_PLATFORM_STATUS : + ret =sev_ioctl_do_platform_status (&input); +break ;default : ret = -EINVAL ; goto out;
Signature · 1 line
2.9.5
Re: [Part2 PATCH v6 15/38] crypto: ccp: Implement SEV_PLATFORM_STATUS ioctl command
Quoted 15 lines · Brijesh Singh
The
SEV_PLATFORM_STATUScommand can be used by the platform owner to get the current status of the platform. The command is defined in SEV spec section 5.5.Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Borislav Petkov <[email protected]>Cc Herbert Xu <[email protected]>Cc Gary Hook <[email protected]>Cc Tom Lendacky <[email protected]>Improvements-by Borislav Petkov <[email protected]>Signed-off-by Brijesh Singh <[email protected]>
Quoted 3 lines
--- drivers/crypto/ccp/psp-dev.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Re: [Part2 PATCH v6 15/38] crypto: ccp: Implement SEV_PLATFORM_STATUS ioctl command
In reply to Brijesh SinghQuoted 4 lines · Brijesh Singh
The
SEV_PLATFORM_STATUScommand can be used by the platform owner to get the current status of the platform. The command is defined in SEV spec section 5.5.
Quoted 11 lines
Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Borislav Petkov <[email protected]>Cc Herbert Xu <[email protected]>Cc Gary Hook <[email protected]>Cc Tom Lendacky <[email protected]>Improvements-by Borislav Petkov <[email protected]>Signed-off-by Brijesh Singh <[email protected]>
Quoted 47 lines
--- drivers/crypto/ccp/psp-dev.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)drivers/crypto/ccp/psp-dev.c +24
-0@@ -174,6 +174,27 @@ static int sev_do_cmd(int cmd, void *data, int *psp_ret)return ret; } +static int sev_ioctl_do_platform_status (struct sev_issue_cmd *argp) +{ +struct sev_user_data_status *data; +int ret; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + ret =sev_do_cmd (SEV_CMD_PLATFORM_STATUS , data, &argp->error); +if (ret) + goto e_free; + +if (copy_to_user ((void __user *)argp->data, data,sizeof (*data))) + ret = -EFAULT ; + +e_free: +kfree (data); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;@@ -194,6 +215,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_FACTORY_RESET : ret =sev_do_cmd (SEV_CMD_FACTORY_RESET ,0 , &input.error);break ; +case SEV_PLATFORM_STATUS : + ret =sev_ioctl_do_platform_status (&input); +break ;default : ret = -EINVAL ; goto out;
PATCH v6 15/38 crypto: ccp: Implement SEV_PEK_GEN ioctl command
In reply to Brijesh SinghThe SEV_PEK_GEN command is used to generate a new Platform Endorsement Key (PEK). The command is defined in SEV spec section 5.6.
---
Boris,
I had your R-b in v6 and have kept it. The changes in this patch are very minor. Let me know if you are okay with it - thanks
Changes since v6:
* use psp->sev_state to check if FW is in correct state before doing the INIT transition
drivers/crypto/ccp/psp-dev.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
drivers/crypto/ccp/psp-dev.c +16 -0
@@ -240,6 +240,19 @@ static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)return ret; } +static int sev_ioctl_do_pek_pdh_gen (int cmd,struct sev_issue_cmd *argp) +{ +int rc; + +if (psp_master->sev_state ==SEV_STATE_UNINIT ) { + rc =sev_platform_init_locked (NULL , &argp->error); +if (rc) +return rc; + } + +return sev_do_cmd_locked (cmd,0 , &argp->error); +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;
@@ -268,6 +281,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_PLATFORM_STATUS : ret =sev_ioctl_do_platform_status (&input);break ; +case SEV_PEK_GEN : + ret =sev_ioctl_do_pek_pdh_gen (SEV_CMD_PEK_GEN , &input); +break ;default : ret = -EINVAL ; goto out;
Signature · 1 line
2.9.5
PATCH v6 16/38 crypto: ccp: Implement SEV_PEK_GEN ioctl command
In reply to Brijesh SinghThe SEV_PEK_GEN command is used to generate a new Platform Endorsement Key (PEK). The command is defined in SEV spec section 5.6.
--- drivers/crypto/ccp/psp-dev.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
drivers/crypto/ccp/psp-dev.c +21 -0
@@ -195,6 +195,24 @@ static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)return ret; } +static int sev_ioctl_do_pek_pdh_gen (int cmd,struct sev_issue_cmd *argp) +{ +int ret, err; + + ret =sev_platform_init (NULL , &argp->error); +if (ret) +return ret; + + ret =sev_do_cmd (cmd,0 , &argp->error); + +if (sev_platform_shutdown (&err)) { + argp->error = err; + ret = -EIO ; + } + +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;
@@ -218,6 +236,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_PLATFORM_STATUS : ret =sev_ioctl_do_platform_status (&input);break ; +case SEV_PEK_GEN : + ret =sev_ioctl_do_pek_pdh_gen (SEV_CMD_PEK_GEN , &input); +break ;default : ret = -EINVAL ; goto out;
Signature · 1 line
2.9.5
Borislav Petkov
maintainerRe: [Part2 PATCH v6 16/38] crypto: ccp: Implement SEV_PEK_GEN ioctl command
Quoted 24 lines · Brijesh Singh
The
SEV_PEK_GENcommand is used to generate a new Platform Endorsement Key (PEK). The command is defined in SEV spec section 5.6.Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Borislav Petkov <[email protected]>Cc Herbert Xu <[email protected]>Cc Gary Hook <[email protected]>Cc Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- drivers/crypto/ccp/psp-dev.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)drivers/crypto/ccp/psp-dev.c +0
-0@@ -195,6 +195,24 @@ static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)return ret; }
drivers/crypto/ccp/psp-dev.c static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp) · line 206
Quoted 10 lines
drivers/crypto/ccp/psp-dev.c +9
-0+static int sev_ioctl_do_pek_pdh_gen (int cmd,struct sev_issue_cmd *argp) +{ +int ret, err; + + ret =sev_platform_init (NULL , &argp->error); +if (ret) +return ret; + + ret =sev_do_cmd (cmd,0 , &argp->error);
So this ret value gets potentially overwritten here. You need to either handle the case properly when sev_do_cmd() fails and sev_platform_shutdown() gets to issue SEV_CMD_SHUTDOWN (i.e., when it gets overwritten), or not write into ret at all by initializing it to 0 at function entry.
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Re: [Part2 PATCH v6 16/38] crypto: ccp: Implement SEV_PEK_GEN ioctl command
On 10/23/17 4:32 AM, Borislav Petkov wrote:
...
Quoted 6 lines
Quoted 6 lines
drivers/crypto/ccp/psp-dev.c +6
-0+static int sev_ioctl_do_pek_pdh_gen (int cmd,struct sev_issue_cmd *argp) +{ +int ret, err; + + ret =sev_platform_init (NULL , &argp->error); +if (ret)
Quoted 9 lines
drivers/crypto/ccp/psp-dev.c static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp) · line 206
Quoted 3 lines
drivers/crypto/ccp/psp-dev.c +3
-0+return ret; + + ret =sev_do_cmd (cmd,0 , &argp->error);So this ret value gets potentially overwritten here. You need to either handle the case properly when
sev_do_cmd()fails andsev_platform_shutdown()gets to issueSEV_CMD_SHUTDOWN(i.e., when it gets overwritten), or not write into ret at all by initializing it to 0 at function entry.
I am not sure if I am able to understand your feedback. The sev_platform_shutdown() is called unconditionally.
1) if sev_do_cmd() fails and sev_platform_shutdown() was success then 'ret' will contain the error code from sev_do_cmd().
2) if sev_do_cmd() was success but sev_platform_shutdown() fails then 'ret' will contain the error code from sev_platform_shutdown()
3) if both sev_do_cmd() and sev_platform_shutdown() fails then 'ret' will contain error code from the sev_platform_shutdown().
Borislav Petkov
maintainerRe: [Part2 PATCH v6 16/38] crypto: ccp: Implement SEV_PEK_GEN ioctl command
Quoted 2 lines · Brijesh Singh
I am not sure if I am able to understand your feedback. The
sev_platform_shutdown()is called unconditionally.
How's that:
If sev_do_cmd() fails and sev_do_cmd(SEV_CMD_SHUTDOWN, ...) in sev_platform_shutdown() fails, then the first ret from sev_do_cmd() is gone.
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Re: [Part2 PATCH v6 16/38] crypto: ccp: Implement SEV_PEK_GEN ioctl command
Quoted 8 lines · Borislav Petkov
Quoted 2 lines · Brijesh Singh
I am not sure if I am able to understand your feedback. The
sev_platform_shutdown()is called unconditionally.How's that:
If
sev_do_cmd()fails andsev_do_cmd(SEV_CMD_SHUTDOWN, ...)insev_platform_shutdown()fails, then the first ret fromsev_do_cmd()is gone.
If both the command fails then we return status from the last command. IIRC, in my previous patches I was returning status from sev_do_cmd() instead of sev_platform_shutdown() but based on our previous communication I thought you asked to return the status from the last failed command. Did I miss understood ?
-Brijesh
Borislav Petkov
maintainerRe: [Part2 PATCH v6 16/38] crypto: ccp: Implement SEV_PEK_GEN ioctl command
Quoted 5 lines · Brijesh Singh
If both the command fails then we return status from the last command. IIRC, in my previous patches I was returning status from
sev_do_cmd()instead ofsev_platform_shutdown()but based on our previous communication I thought you asked to return the status from the last failed command. Did I miss understood ?
So my problem is that it looks strange that you save an error value from sev_do_cmd() but you don't look at it. And as I said in the other mail, you should either ignore it and say so in a comment why it is OK to ignore it or handle it but not overwrite it without looking at it.
Does that make more sense?
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Re: [Part2 PATCH v6 16/38] crypto: ccp: Implement SEV_PEK_GEN ioctl command
Quoted 7 lines · Borislav Petkov
Quoted 5 lines · Brijesh Singh
If both the command fails then we return status from the last command. IIRC, in my previous patches I was returning status from
sev_do_cmd()instead ofsev_platform_shutdown()but based on our previous communication I thought you asked to return the status from the last failed command. Did I miss understood ?
Quoted 7 lines
So my problem is that it looks strange that you save an error value from
sev_do_cmd()but you don't look at it. And as I said in the other mail, you should either ignore it and say so in a comment why it is OK to ignore it or handle it but not overwrite it without looking at it.Does that make more sense?
I see your point, if both commands failed then I am now inclined towards ignoring the error code from shutdown command and add some comments explaining why its OK. thanks
-Brijesh
PATCH v6 16/38 crypto: ccp: Implement SEV_PEK_GEN ioctl command
In reply to Brijesh SinghThe SEV_PEK_GEN command is used to generate a new Platform Endorsement Key (PEK). The command is defined in SEV spec section 5.6.
---
Changes since v6:
* when sev_do_cmd() and sev_platform_shutdown() fails then propogate the error status code from sev_do_cmd() because it can give us much better reason for the failure.
drivers/crypto/ccp/psp-dev.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
drivers/crypto/ccp/psp-dev.c +31 -0
@@ -195,6 +195,34 @@ static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)return ret; } +static int sev_ioctl_do_pek_pdh_gen (int cmd,struct sev_issue_cmd *argp) +{ +int ret, err; + + ret =sev_platform_init (NULL , &argp->error); +if (ret) +return ret; + + ret =sev_do_cmd (cmd,0 , &argp->error); + +if (sev_platform_shutdown (&err)) { +/* +* If both sev_do_cmd() and sev_platform_shutdown() commands +* failed then propogate the error code from the sev_do_cmd() +* because it contains a useful status code for the command +* failure. +*/ +if (ret) + goto done; + + argp->error = err; + ret = -EIO ; + } + +done: +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;
@@ -218,6 +246,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_PLATFORM_STATUS : ret =sev_ioctl_do_platform_status (&input);break ; +case SEV_PEK_GEN : + ret =sev_ioctl_do_pek_pdh_gen (SEV_CMD_PEK_GEN , &input); +break ;default : ret = -EINVAL ; goto out;
Signature · 1 line
2.9.5
Re: [Part2 PATCH v6.1 16/38] crypto: ccp: Implement SEV_PEK_GEN ioctl command
Quoted 13 lines · Brijesh Singh
The
SEV_PEK_GENcommand is used to generate a new Platform Endorsement Key (PEK). The command is defined in SEV spec section 5.6.Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Borislav Petkov <[email protected]>Cc Herbert Xu <[email protected]>Cc Gary Hook <[email protected]>Cc Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>
Quoted 60 lines
---
Changes since v6:
* whensev_do_cmd()andsev_platform_shutdown()fails then propogate the error status code fromsev_do_cmd()because it can give us much better reason for the failure.drivers/crypto/ccp/psp-dev.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)drivers/crypto/ccp/psp-dev.c +31
-0@@ -195,6 +195,34 @@ static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)return ret; } +static int sev_ioctl_do_pek_pdh_gen (int cmd,struct sev_issue_cmd *argp) +{ +int ret, err; + + ret =sev_platform_init (NULL , &argp->error); +if (ret) +return ret; + + ret =sev_do_cmd (cmd,0 , &argp->error); + +if (sev_platform_shutdown (&err)) { +/* +* If both sev_do_cmd() and sev_platform_shutdown() commands +* failed then propogate the error code from the sev_do_cmd() +* because it contains a useful status code for the command +* failure. +*/ +if (ret) + goto done; + + argp->error = err; + ret = -EIO ; + } + +done: +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;@@ -218,6 +246,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_PLATFORM_STATUS : ret =sev_ioctl_do_platform_status (&input);break ; +case SEV_PEK_GEN : + ret =sev_ioctl_do_pek_pdh_gen (SEV_CMD_PEK_GEN , &input); +break ;default : ret = -EINVAL ; goto out;
Re: [Part2 PATCH v6.1 16/38] crypto: ccp: Implement SEV_PEK_GEN ioctl command
In reply to Brijesh SinghQuoted 15 lines · Brijesh Singh
The
SEV_PEK_GENcommand is used to generate a new Platform Endorsement Key (PEK). The command is defined in SEV spec section 5.6.Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Borislav Petkov <[email protected]>Cc Herbert Xu <[email protected]>Cc Gary Hook <[email protected]>Cc Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>---
Quoted 7 lines
Changes since v6:
* whensev_do_cmd()andsev_platform_shutdown()fails then propogate the error status code fromsev_do_cmd()because it can give us much better reason for the failure.drivers/crypto/ccp/psp-dev.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--PATCH v6 17/38 crypto: ccp: Implement SEV_PDH_GEN ioctl command
In reply to Brijesh SinghThe SEV_PDH_GEN command is used to re-generate the Platform Diffie-Hellman (PDH) key. The command is defined in SEV spec section 5.6.
--- drivers/crypto/ccp/psp-dev.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/crypto/ccp/psp-dev.c +3 -0
@@ -239,6 +239,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_PEK_GEN : ret =sev_ioctl_do_pek_pdh_gen (SEV_CMD_PEK_GEN , &input);break ; +case SEV_PDH_GEN : + ret =sev_ioctl_do_pek_pdh_gen (SEV_CMD_PDH_GEN , &input); +break ;default : ret = -EINVAL ; goto out;
Signature · 1 line
2.9.5
Re: [Part2 PATCH v6 17/38] crypto: ccp: Implement SEV_PDH_GEN ioctl command
Quoted 14 lines · Brijesh Singh
The
SEV_PDH_GENcommand is used to re-generate the Platform Diffie-Hellman (PDH) key. The command is defined in SEV spec section 5.6.Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Borislav Petkov <[email protected]>Cc Herbert Xu <[email protected]>Cc Gary Hook <[email protected]>Cc Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>
Quoted 3 lines
--- drivers/crypto/ccp/psp-dev.c | 3 +++ 1 file changed, 3 insertions(+)
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Re: [Part2 PATCH v6 17/38] crypto: ccp: Implement SEV_PDH_GEN ioctl command
In reply to Brijesh SinghQuoted 4 lines · Brijesh Singh
The
SEV_PDH_GENcommand is used to re-generate the Platform Diffie-Hellman (PDH) key. The command is defined in SEV spec section 5.6.
Quoted 10 lines
Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Borislav Petkov <[email protected]>Cc Herbert Xu <[email protected]>Cc Gary Hook <[email protected]>Cc Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>
Quoted 19 lines
--- drivers/crypto/ccp/psp-dev.c | 3 +++ 1 file changed, 3 insertions(+)drivers/crypto/ccp/psp-dev.c +3
-0@@ -239,6 +239,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_PEK_GEN : ret =sev_ioctl_do_pek_pdh_gen (SEV_CMD_PEK_GEN , &input);break ; +case SEV_PDH_GEN : + ret =sev_ioctl_do_pek_pdh_gen (SEV_CMD_PDH_GEN , &input); +break ;default : ret = -EINVAL ; goto out;
PATCH v6 18/38 crypto: ccp: Implement SEV_PEK_CSR ioctl command
In reply to Brijesh SinghThe SEV_PEK_CSR command can be used to generate a PEK certificate signing request. The command is defined in SEV spec section 5.7.
--- drivers/crypto/ccp/psp-dev.c | 69 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+)
drivers/crypto/ccp/psp-dev.c +69 -0
@@ -213,6 +213,72 @@ static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp)return ret; } +static int sev_ioctl_do_pek_csr (struct sev_issue_cmd *argp) +{ +struct sev_user_data_pek_csr input; +struct sev_data_pek_csr *data; +void *blob =NULL ; +int ret, err; + +if (copy_from_user (&input, (void __user *)argp->data,sizeof (input))) +return -EFAULT ; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + +/* userspace wants to query CSR length */ +if (!input.address || !input.length) + goto cmd; + +/* allocate a physically contiguous buffer to store the CSR blob */ +if (!access_ok (VERIFY_WRITE , input.address, input.length) || + input.length >SEV_FW_BLOB_MAX_SIZE ) { + ret = -EFAULT ; + goto e_free; + } + + blob =kmalloc (input.length,GFP_KERNEL ); +if (!blob) { + ret = -ENOMEM ; + goto e_free; + } + + data->address =__psp_pa (blob); + data->len = input.length; + +cmd: + ret =sev_platform_init (NULL , &argp->error); +if (ret) + goto e_free_blob; + + ret =sev_do_cmd (SEV_CMD_PEK_CSR , data, &argp->error); + +/* +* If we query the CSR length, FW responded with expected data +*/ + input.length = data->len; + +if (blob) { +if (copy_to_user ((void __user *)input.address, blob, input.length)) + ret = -EFAULT ; + } + +if (sev_platform_shutdown (&err)) { + ret = -EIO ; + argp->error = err; + } + +if (copy_to_user ((void __user *)argp->data, &input,sizeof (input))) + ret = -EFAULT ; + +e_free_blob: +kfree (blob); +e_free: +kfree (data); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;
@@ -242,6 +308,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_PDH_GEN : ret =sev_ioctl_do_pek_pdh_gen (SEV_CMD_PDH_GEN , &input);break ; +case SEV_PEK_CSR : + ret =sev_ioctl_do_pek_csr (&input); +break ;default : ret = -EINVAL ; goto out;
Signature · 1 line
2.9.5
Borislav Petkov
maintainerRe: [Part2 PATCH v6 18/38] crypto: ccp: Implement SEV_PEK_CSR ioctl command
Quoted 13 lines · Brijesh Singh
The
SEV_PEK_CSRcommand can be used to generate a PEK certificate signing request. The command is defined in SEV spec section 5.7.Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Borislav Petkov <[email protected]>Cc Herbert Xu <[email protected]>Cc Gary Hook <[email protected]>Cc Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>
Quoted 3 lines
--- drivers/crypto/ccp/psp-dev.c | 69 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+)
Quoted 30 lines
drivers/crypto/ccp/psp-dev.c +30
-0+static int sev_ioctl_do_pek_csr (struct sev_issue_cmd *argp) +{ +struct sev_user_data_pek_csr input; +struct sev_data_pek_csr *data; +void *blob =NULL ; +int ret, err; + +if (copy_from_user (&input, (void __user *)argp->data,sizeof (input))) +return -EFAULT ; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + +/* userspace wants to query CSR length */ +if (!input.address || !input.length) + goto cmd; + +/* allocate a physically contiguous buffer to store the CSR blob */ +if (!access_ok (VERIFY_WRITE , input.address, input.length) || + input.length >SEV_FW_BLOB_MAX_SIZE ) { + ret = -EFAULT ; + goto e_free; + } + + blob =kmalloc (input.length,GFP_KERNEL ); +if (!blob) { + ret = -ENOMEM ; + goto e_free; + }
drivers/crypto/ccp/psp-dev.c static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp) · line 255
Quoted 10 lines
drivers/crypto/ccp/psp-dev.c +10
-0+ + data->address =__psp_pa (blob); + data->len = input.length; + +cmd: + ret =sev_platform_init (NULL , &argp->error); +if (ret) + goto e_free_blob; + + ret =sev_do_cmd (SEV_CMD_PEK_CSR , data, &argp->error);
This is the same issue: nothing is handling the case where sev_do_cmd() here fails. If it doesn't matter, then you should simply do:
sev_do_cmd(SEV_CMD_PEK_CSR, data, &argp->error);
along with explaining why we don't care about the command failing. But simply writing into ret to overwrite it later, looks strange.
Signature · 4 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.PATCH v6 18/38 crypto: ccp: Implement SEV_PEK_CSR ioctl command
In reply to Brijesh SinghThe SEV_PEK_CSR command can be used to generate a PEK certificate signing request. The command is defined in SEV spec section 5.7.
---
Changes since v6:
* when sev_do_cmd() and sev_platform_shutdown() fails then propogate the error status code from sev_do_cmd() because it can give us much better reason for the failure.
drivers/crypto/ccp/psp-dev.c | 81 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+)
drivers/crypto/ccp/psp-dev.c +81 -0
@@ -223,6 +223,84 @@ static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp)return ret; } +static int sev_ioctl_do_pek_csr (struct sev_issue_cmd *argp) +{ +struct sev_user_data_pek_csr input; +struct sev_data_pek_csr *data; +void *blob =NULL ; +int ret, err; + +if (copy_from_user (&input, (void __user *)argp->data,sizeof (input))) +return -EFAULT ; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + +/* userspace wants to query CSR length */ +if (!input.address || !input.length) + goto cmd; + +/* allocate a physically contiguous buffer to store the CSR blob */ +if (!access_ok (VERIFY_WRITE , input.address, input.length) || + input.length >SEV_FW_BLOB_MAX_SIZE ) { + ret = -EFAULT ; + goto e_free; + } + + blob =kmalloc (input.length,GFP_KERNEL ); +if (!blob) { + ret = -ENOMEM ; + goto e_free; + } + + data->address =__psp_pa (blob); + data->len = input.length; + +cmd: + ret =sev_platform_init (NULL , &argp->error); +if (ret) + goto e_free_blob; + + ret =sev_do_cmd (SEV_CMD_PEK_CSR , data, &argp->error); + +/* +* If we query the CSR length, FW responded with expected data +*/ + input.length = data->len; + +if (sev_platform_shutdown (&err)) { +/* +* If both sev_do_cmd() and sev_platform_shutdown() commands +* failed then propogate the error code from the sev_do_cmd() +* because it contains a useful status code for the command +* failure. +*/ +if (ret) + goto e_free_blob; + + ret = -EIO ; + argp->error = err; + goto e_free_blob; + } + +if (copy_to_user ((void __user *)argp->data, &input,sizeof (input))) { + ret = -EFAULT ; + goto e_free_blob; + } + +if (blob) { +if (copy_to_user ((void __user *)input.address, blob, input.length)) + ret = -EFAULT ; + } + +e_free_blob: +kfree (blob); +e_free: +kfree (data); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;
@@ -252,6 +330,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_PDH_GEN : ret =sev_ioctl_do_pek_pdh_gen (SEV_CMD_PDH_GEN , &input);break ; +case SEV_PEK_CSR : + ret =sev_ioctl_do_pek_csr (&input); +break ;default : ret = -EINVAL ; goto out;
Signature · 1 line
2.9.5
Re: [Part2 PATCH v6.1 18/38] crypto: ccp: Implement SEV_PEK_CSR ioctl command
Quoted 13 lines · Brijesh Singh
The
SEV_PEK_CSRcommand can be used to generate a PEK certificate signing request. The command is defined in SEV spec section 5.7.Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Borislav Petkov <[email protected]>Cc Herbert Xu <[email protected]>Cc Gary Hook <[email protected]>Cc Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>
Quoted 1 line
---
Quoted 109 lines
Changes since v6:
* whensev_do_cmd()andsev_platform_shutdown()fails then propogate the error status code fromsev_do_cmd()because it can give us much better reason for the failure.drivers/crypto/ccp/psp-dev.c | 81 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+)drivers/crypto/ccp/psp-dev.c +81
-0@@ -223,6 +223,84 @@ static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp)return ret; } +static int sev_ioctl_do_pek_csr (struct sev_issue_cmd *argp) +{ +struct sev_user_data_pek_csr input; +struct sev_data_pek_csr *data; +void *blob =NULL ; +int ret, err; + +if (copy_from_user (&input, (void __user *)argp->data,sizeof (input))) +return -EFAULT ; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + +/* userspace wants to query CSR length */ +if (!input.address || !input.length) + goto cmd; + +/* allocate a physically contiguous buffer to store the CSR blob */ +if (!access_ok (VERIFY_WRITE , input.address, input.length) || + input.length >SEV_FW_BLOB_MAX_SIZE ) { + ret = -EFAULT ; + goto e_free; + } + + blob =kmalloc (input.length,GFP_KERNEL ); +if (!blob) { + ret = -ENOMEM ; + goto e_free; + } + + data->address =__psp_pa (blob); + data->len = input.length; + +cmd: + ret =sev_platform_init (NULL , &argp->error); +if (ret) + goto e_free_blob; + + ret =sev_do_cmd (SEV_CMD_PEK_CSR , data, &argp->error); + +/* +* If we query the CSR length, FW responded with expected data +*/ + input.length = data->len; + +if (sev_platform_shutdown (&err)) { +/* +* If both sev_do_cmd() and sev_platform_shutdown() commands +* failed then propogate the error code from the sev_do_cmd() +* because it contains a useful status code for the command +* failure. +*/ +if (ret) + goto e_free_blob; + + ret = -EIO ; + argp->error = err; + goto e_free_blob; + } + +if (copy_to_user ((void __user *)argp->data, &input,sizeof (input))) { + ret = -EFAULT ; + goto e_free_blob; + } + +if (blob) { +if (copy_to_user ((void __user *)input.address, blob, input.length)) + ret = -EFAULT ; + } + +e_free_blob: +kfree (blob); +e_free: +kfree (data); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;@@ -252,6 +330,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_PDH_GEN : ret =sev_ioctl_do_pek_pdh_gen (SEV_CMD_PDH_GEN , &input);break ; +case SEV_PEK_CSR : + ret =sev_ioctl_do_pek_csr (&input); +break ;default : ret = -EINVAL ; goto out;
PATCH v6 18/38 crypto: ccp: Implement SEV_PEK_CSR ioctl command
In reply to Brijesh SinghThe SEV_PEK_CSR command can be used to generate a PEK certificate signing request. The command is defined in SEV spec section 5.7.
---
Changes since v6.1:
* use psp->sev_state to check the state before transition to INIT
drivers/crypto/ccp/psp-dev.c | 68 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+)
drivers/crypto/ccp/psp-dev.c +68 -0
@@ -253,6 +253,71 @@ static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp)return sev_do_cmd_locked (cmd,0 , &argp->error); } +static int sev_ioctl_do_pek_csr (struct sev_issue_cmd *argp) +{ +struct sev_user_data_pek_csr input; +struct sev_data_pek_csr *data; +void *blob =NULL ; +int ret; + +if (copy_from_user (&input, (void __user *)argp->data,sizeof (input))) +return -EFAULT ; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + +/* userspace wants to query CSR length */ +if (!input.address || !input.length) + goto cmd; + +/* allocate a physically contiguous buffer to store the CSR blob */ +if (!access_ok (VERIFY_WRITE , input.address, input.length) || + input.length >SEV_FW_BLOB_MAX_SIZE ) { + ret = -EFAULT ; + goto e_free; + } + + blob =kmalloc (input.length,GFP_KERNEL ); +if (!blob) { + ret = -ENOMEM ; + goto e_free; + } + + data->address =__psp_pa (blob); + data->len = input.length; + +cmd: +if (psp_master->sev_state ==SEV_STATE_UNINIT ) { + ret =sev_platform_init_locked (NULL , &argp->error); +if (ret) + goto e_free_blob; + } + + ret =sev_do_cmd_locked (SEV_CMD_PEK_CSR , data, &argp->error); + +/* +* If we query the CSR length, FW responded with expected data +*/ + input.length = data->len; + +if (copy_to_user ((void __user *)argp->data, &input,sizeof (input))) { + ret = -EFAULT ; + goto e_free_blob; + } + +if (blob) { +if (copy_to_user ((void __user *)input.address, blob, input.length)) + ret = -EFAULT ; + } + +e_free_blob: +kfree (blob); +e_free: +kfree (data); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;
@@ -287,6 +352,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_PDH_GEN : ret =sev_ioctl_do_pek_pdh_gen (SEV_CMD_PDH_GEN , &input);break ; +case SEV_PEK_CSR : + ret =sev_ioctl_do_pek_csr (&input); +break ;default : ret = -EINVAL ; goto out;
Signature · 1 line
2.9.5
PATCH v6 18/38 crypto: ccp: Implement SEV_PEK_CSR ioctl command
In reply to Brijesh SinghThe SEV_PEK_CSR command can be used to generate a PEK certificate signing request. The command is defined in SEV spec section 5.7.
---
Changes since v6:
* when sev_do_cmd() and sev_platform_shutdown() fails then propogate the error status code from sev_do_cmd() because it can give us much better reason for the failure.
drivers/crypto/ccp/psp-dev.c | 81 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+)
drivers/crypto/ccp/psp-dev.c +81 -0
@@ -223,6 +223,84 @@ static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp)return ret; } +static int sev_ioctl_do_pek_csr (struct sev_issue_cmd *argp) +{ +struct sev_user_data_pek_csr input; +struct sev_data_pek_csr *data; +void *blob =NULL ; +int ret, err; + +if (copy_from_user (&input, (void __user *)argp->data,sizeof (input))) +return -EFAULT ; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + +/* userspace wants to query CSR length */ +if (!input.address || !input.length) + goto cmd; + +/* allocate a physically contiguous buffer to store the CSR blob */ +if (!access_ok (VERIFY_WRITE , input.address, input.length) || + input.length >SEV_FW_BLOB_MAX_SIZE ) { + ret = -EFAULT ; + goto e_free; + } + + blob =kmalloc (input.length,GFP_KERNEL ); +if (!blob) { + ret = -ENOMEM ; + goto e_free; + } + + data->address =__psp_pa (blob); + data->len = input.length; + +cmd: + ret =sev_platform_init (NULL , &argp->error); +if (ret) + goto e_free_blob; + + ret =sev_do_cmd (SEV_CMD_PEK_CSR , data, &argp->error); + +/* +* If we query the CSR length, FW responded with expected data +*/ + input.length = data->len; + +if (sev_platform_shutdown (&err)) { +/* +* If both sev_do_cmd() and sev_platform_shutdown() commands +* failed then propogate the error code from the sev_do_cmd() +* because it contains a useful status code for the command +* failure. +*/ +if (ret) + goto e_free_blob; + + ret = -EIO ; + argp->error = err; + goto e_free_blob; + } + +if (copy_to_user ((void __user *)argp->data, &input,sizeof (input))) { + ret = -EFAULT ; + goto e_free_blob; + } + +if (blob) { +if (copy_to_user ((void __user *)input.address, blob, input.length)) + ret = -EFAULT ; + } + +e_free_blob: +kfree (blob); +e_free: +kfree (data); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;
@@ -252,6 +330,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_PDH_GEN : ret =sev_ioctl_do_pek_pdh_gen (SEV_CMD_PDH_GEN , &input);break ; +case SEV_PEK_CSR : + ret =sev_ioctl_do_pek_csr (&input); +break ;default : ret = -EINVAL ; goto out;
Signature · 1 line
2.9.5
PATCH v6 19/38 crypto: ccp: Implement SEV_PEK_CERT_IMPORT ioctl command
In reply to Brijesh SinghThe SEV_PEK_CERT_IMPORT command can be used to import the signed PEK certificate. The command is defined in SEV spec section 5.8.
--- drivers/crypto/ccp/psp-dev.c | 83 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 4 +++ 2 files changed, 87 insertions(+)
drivers/crypto/ccp/psp-dev.c +83 -0
@@ -279,6 +279,86 @@ static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp)return ret; } +void *psp_copy_user_blob (u64 __user uaddr,u32 len) +{ +void *data; + +if (!uaddr || !len) +return ERR_PTR (-EINVAL ); + +/* verify that blob length does not exceed our limit */ +if (len >SEV_FW_BLOB_MAX_SIZE ) +return ERR_PTR (-EINVAL ); + + data =kmalloc (len,GFP_KERNEL ); +if (!data) +return ERR_PTR (-ENOMEM ); + +if (copy_from_user (data, (void __user *)(uintptr_t )uaddr, len)) + goto e_free; + +return data; + +e_free: +kfree (data); +return ERR_PTR (-EFAULT ); +} +EXPORT_SYMBOL_GPL (psp_copy_user_blob); + +static int sev_ioctl_do_pek_cert_import (struct sev_issue_cmd *argp) +{ +struct sev_user_data_pek_cert_import input; +struct sev_data_pek_cert_import *data; +void *pek_blob, *oca_blob; +int ret, err; + +if (copy_from_user (&input, (void __user *)argp->data,sizeof (input))) +return -EFAULT ; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + +/* copy PEK certificate blobs from userspace */ + pek_blob =psp_copy_user_blob (input.pek_cert_address, input.pek_cert_len); +if (IS_ERR (pek_blob)) { + ret =PTR_ERR (pek_blob); + goto e_free; + } + + data->pek_cert_address =__psp_pa (pek_blob); + data->pek_cert_len = input.pek_cert_len; + +/* copy PEK certificate blobs from userspace */ + oca_blob =psp_copy_user_blob (input.oca_cert_address, input.oca_cert_len); +if (IS_ERR (oca_blob)) { + ret =PTR_ERR (oca_blob); + goto e_free_pek; + } + + data->oca_cert_address =__psp_pa (oca_blob); + data->oca_cert_len = input.oca_cert_len; + + ret =sev_platform_init (NULL , &argp->error); +if (ret) + goto e_free_oca; + + ret =sev_do_cmd (SEV_CMD_PEK_CERT_IMPORT , data, &argp->error); + +if (sev_platform_shutdown (&err)) { + ret = -EIO ; + argp->error = err; + } + +e_free_oca: +kfree (oca_blob); +e_free_pek: +kfree (pek_blob); +e_free: +kfree (data); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;
@@ -311,6 +391,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_PEK_CSR : ret =sev_ioctl_do_pek_csr (&input);break ; +case SEV_PEK_CERT_IMPORT : + ret =sev_ioctl_do_pek_cert_import (&input); +break ;default : ret = -EINVAL ; goto out;
include/linux/psp-sev.h +4 -0
@@ -620,6 +620,8 @@ int sev_guest_df_flush(int *error);*/ int sev_guest_decommission (struct sev_data_decommission *data,int *error); +void *psp_copy_user_blob (u64 __user uaddr,u32 len); +#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ static inline int
@@ -648,6 +650,8 @@ sev_issue_cmd_external_user(struct file *filep,return -ENODEV ; } +static inline void *psp_copy_user_blob (u64 __user uaddr,u32 len) {return ERR_PTR (-EINVAL ); } +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */
Signature · 1 line
2.9.5
PATCH v6 19/38 crypto: ccp: Implement SEV_PEK_CERT_IMPORT ioctl command
The SEV_PEK_CERT_IMPORT command can be used to import the signed PEK certificate. The command is defined in SEV spec section 5.8.
---
Changes since v6:
* when sev_do_cmd() and sev_platform_shutdown() fails then propogate the error status code from sev_do_cmd() because it can give us much better reason for the failure.
drivers/crypto/ccp/psp-dev.c | 92 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 4 ++ 2 files changed, 96 insertions(+)
drivers/crypto/ccp/psp-dev.c +92 -0
@@ -301,6 +301,95 @@ static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp)return ret; } +void *psp_copy_user_blob (u64 __user uaddr,u32 len) +{ +void *data; + +if (!uaddr || !len) +return ERR_PTR (-EINVAL ); + +/* verify that blob length does not exceed our limit */ +if (len >SEV_FW_BLOB_MAX_SIZE ) +return ERR_PTR (-EINVAL ); + + data =kmalloc (len,GFP_KERNEL ); +if (!data) +return ERR_PTR (-ENOMEM ); + +if (copy_from_user (data, (void __user *)(uintptr_t )uaddr, len)) + goto e_free; + +return data; + +e_free: +kfree (data); +return ERR_PTR (-EFAULT ); +} +EXPORT_SYMBOL_GPL (psp_copy_user_blob); + +static int sev_ioctl_do_pek_cert_import (struct sev_issue_cmd *argp) +{ +struct sev_user_data_pek_cert_import input; +struct sev_data_pek_cert_import *data; +void *pek_blob, *oca_blob; +int ret, err; + +if (copy_from_user (&input, (void __user *)argp->data,sizeof (input))) +return -EFAULT ; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + +/* copy PEK certificate blobs from userspace */ + pek_blob =psp_copy_user_blob (input.pek_cert_address, input.pek_cert_len); +if (IS_ERR (pek_blob)) { + ret =PTR_ERR (pek_blob); + goto e_free; + } + + data->pek_cert_address =__psp_pa (pek_blob); + data->pek_cert_len = input.pek_cert_len; + +/* copy PEK certificate blobs from userspace */ + oca_blob =psp_copy_user_blob (input.oca_cert_address, input.oca_cert_len); +if (IS_ERR (oca_blob)) { + ret =PTR_ERR (oca_blob); + goto e_free_pek; + } + + data->oca_cert_address =__psp_pa (oca_blob); + data->oca_cert_len = input.oca_cert_len; + + ret =sev_platform_init (NULL , &argp->error); +if (ret) + goto e_free_oca; + + ret =sev_do_cmd (SEV_CMD_PEK_CERT_IMPORT , data, &argp->error); + +if (sev_platform_shutdown (&err)) { +/* +* If both sev_do_cmd() and sev_platform_shutdown() commands +* failed then propogate the error code from the sev_do_cmd() +* because it contains a useful status code for the command +* failure. +*/ +if (ret) + goto e_free_oca; + + ret = -EIO ; + argp->error = err; + } + +e_free_oca: +kfree (oca_blob); +e_free_pek: +kfree (pek_blob); +e_free: +kfree (data); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;
@@ -333,6 +422,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_PEK_CSR : ret =sev_ioctl_do_pek_csr (&input);break ; +case SEV_PEK_CERT_IMPORT : + ret =sev_ioctl_do_pek_cert_import (&input); +break ;default : ret = -EINVAL ; goto out;
include/linux/psp-sev.h +4 -0
@@ -620,6 +620,8 @@ int sev_guest_df_flush(int *error);*/ int sev_guest_decommission (struct sev_data_decommission *data,int *error); +void *psp_copy_user_blob (u64 __user uaddr,u32 len); +#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ static inline int
@@ -648,6 +650,8 @@ sev_issue_cmd_external_user(struct file *filep,return -ENODEV ; } +static inline void *psp_copy_user_blob (u64 __user uaddr,u32 len) {return ERR_PTR (-EINVAL ); } +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */
Signature · 1 line
2.9.5
Re: [Part2 PATCH v6.1 19/38] crypto: ccp: Implement SEV_PEK_CERT_IMPORT ioctl command
Quoted 13 lines · Brijesh Singh
The
SEV_PEK_CERT_IMPORTcommand can be used to import the signed PEK certificate. The command is defined in SEV spec section 5.8.Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Borislav Petkov <[email protected]>Cc Herbert Xu <[email protected]>Cc Gary Hook <[email protected]>Cc Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>
Quoted 144 lines
---
Changes since v6:
* whensev_do_cmd()andsev_platform_shutdown()fails then propogate the error status code fromsev_do_cmd()because it can give us much better reason for the failure.drivers/crypto/ccp/psp-dev.c | 92 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 4 ++ 2 files changed, 96 insertions(+)drivers/crypto/ccp/psp-dev.c +92
-0@@ -301,6 +301,95 @@ static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp)return ret; } +void *psp_copy_user_blob (u64 __user uaddr,u32 len) +{ +void *data; + +if (!uaddr || !len) +return ERR_PTR (-EINVAL ); + +/* verify that blob length does not exceed our limit */ +if (len >SEV_FW_BLOB_MAX_SIZE ) +return ERR_PTR (-EINVAL ); + + data =kmalloc (len,GFP_KERNEL ); +if (!data) +return ERR_PTR (-ENOMEM ); + +if (copy_from_user (data, (void __user *)(uintptr_t )uaddr, len)) + goto e_free; + +return data; + +e_free: +kfree (data); +return ERR_PTR (-EFAULT ); +} +EXPORT_SYMBOL_GPL (psp_copy_user_blob); + +static int sev_ioctl_do_pek_cert_import (struct sev_issue_cmd *argp) +{ +struct sev_user_data_pek_cert_import input; +struct sev_data_pek_cert_import *data; +void *pek_blob, *oca_blob; +int ret, err; + +if (copy_from_user (&input, (void __user *)argp->data,sizeof (input))) +return -EFAULT ; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + +/* copy PEK certificate blobs from userspace */ + pek_blob =psp_copy_user_blob (input.pek_cert_address, input.pek_cert_len); +if (IS_ERR (pek_blob)) { + ret =PTR_ERR (pek_blob); + goto e_free; + } + + data->pek_cert_address =__psp_pa (pek_blob); + data->pek_cert_len = input.pek_cert_len; + +/* copy PEK certificate blobs from userspace */ + oca_blob =psp_copy_user_blob (input.oca_cert_address, input.oca_cert_len); +if (IS_ERR (oca_blob)) { + ret =PTR_ERR (oca_blob); + goto e_free_pek; + } + + data->oca_cert_address =__psp_pa (oca_blob); + data->oca_cert_len = input.oca_cert_len; + + ret =sev_platform_init (NULL , &argp->error); +if (ret) + goto e_free_oca; + + ret =sev_do_cmd (SEV_CMD_PEK_CERT_IMPORT , data, &argp->error); + +if (sev_platform_shutdown (&err)) { +/* +* If both sev_do_cmd() and sev_platform_shutdown() commands +* failed then propogate the error code from the sev_do_cmd() +* because it contains a useful status code for the command +* failure. +*/ +if (ret) + goto e_free_oca; + + ret = -EIO ; + argp->error = err; + } + +e_free_oca: +kfree (oca_blob); +e_free_pek: +kfree (pek_blob); +e_free: +kfree (data); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;@@ -333,6 +422,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_PEK_CSR : ret =sev_ioctl_do_pek_csr (&input);break ; +case SEV_PEK_CERT_IMPORT : + ret =sev_ioctl_do_pek_cert_import (&input); +break ;default : ret = -EINVAL ; goto out;include/linux/psp-sev.h +4
-0@@ -620,6 +620,8 @@ int sev_guest_df_flush(int *error);*/ int sev_guest_decommission (struct sev_data_decommission *data,int *error); +void *psp_copy_user_blob (u64 __user uaddr,u32 len); +#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ static inline int@@ -648,6 +650,8 @@ sev_issue_cmd_external_user(struct file *filep,return -ENODEV ; } +static inline void *psp_copy_user_blob (u64 __user uaddr,u32 len) {return ERR_PTR (-EINVAL ); } +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */
PATCH v6 20/38 crypto: ccp: Implement SEV_PDH_CERT_EXPORT ioctl command
In reply to Brijesh SinghThe SEV_PDH_CERT_EXPORT command can be used to export the PDH and its certificate chain. The command is defined in SEV spec section 5.10.
--- drivers/crypto/ccp/psp-dev.c | 99 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+)
drivers/crypto/ccp/psp-dev.c +99 -0
@@ -359,6 +359,102 @@ static int sev_ioctl_do_pek_cert_import(struct sev_issue_cmd *argp)return ret; } +static int sev_ioctl_do_pdh_cert_export (struct sev_issue_cmd *argp) +{ +struct sev_user_data_pdh_cert_export input; +void *pdh_blob =NULL , *cert_blob =NULL ; +struct sev_data_pdh_cert_export *data; +int ret, err; + +if (copy_from_user (&input, (void __user *)argp->data,sizeof (input))) +return -EFAULT ; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + +/* Userspace wants to query the certificate length */ +if (!input.pdh_cert_address || !input.pdh_cert_len || + !input.cert_chain_address || !input.cert_chain_address) + goto cmd; + +/* allocate a physically contiguous buffer to store the PDH blob */ +if (!access_ok (VERIFY_WRITE , input.pdh_cert_address, input.pdh_cert_len) || + (input.pdh_cert_len >SEV_FW_BLOB_MAX_SIZE )) { + ret = -EFAULT ; + goto e_free; + } + + pdh_blob =kmalloc (input.pdh_cert_len,GFP_KERNEL ); +if (!pdh_blob) { + ret = -ENOMEM ; + goto e_free; + } + + data->pdh_cert_address =__psp_pa (pdh_blob); + data->pdh_cert_len = input.pdh_cert_len; + +/* allocate a physically contiguous buffer to store the cert chain blob */ +if (!access_ok (VERIFY_WRITE , input.cert_chain_address, input.cert_chain_len) || + (input.cert_chain_len >SEV_FW_BLOB_MAX_SIZE )) { + ret = -EFAULT ; + goto e_free_pdh; + } + + cert_blob =kmalloc (input.cert_chain_len,GFP_KERNEL ); +if (!cert_blob) { + ret = -ENOMEM ; + goto e_free_pdh; + } + + data->cert_chain_address =__psp_pa (cert_blob); + data->cert_chain_len = input.cert_chain_len; + +cmd: + ret =sev_platform_init (NULL , &argp->error); +if (ret) + goto e_free_cert; + + ret =sev_do_cmd (SEV_CMD_PDH_CERT_EXPORT , data, &argp->error); + +/* +* If we query the length, FW responded with expected data +*/ + input.cert_chain_len = data->cert_chain_len; + input.pdh_cert_len = data->pdh_cert_len; + +if (copy_to_user ((void __user *)argp->data, &input,sizeof (input))) + ret = -EFAULT ; + +if (sev_platform_shutdown (&err)) { + ret = -EIO ; + argp->error = err; + goto e_free_cert; + } + +if (pdh_blob) { +if (copy_to_user ((void __user *)input.pdh_cert_address, + pdh_blob, input.pdh_cert_len)) { + ret = -EFAULT ; + goto e_free_cert; + } + } + +if (cert_blob) { +if (copy_to_user ((void __user *)input.cert_chain_address, + cert_blob, input.cert_chain_len)) + ret = -EFAULT ; + } + +e_free_cert: +kfree (cert_blob); +e_free_pdh: +kfree (pdh_blob); +e_free: +kfree (data); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;
@@ -394,6 +490,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_PEK_CERT_IMPORT : ret =sev_ioctl_do_pek_cert_import (&input);break ; +case SEV_PDH_CERT_EXPORT : + ret =sev_ioctl_do_pdh_cert_export (&input); +break ;default : ret = -EINVAL ; goto out;
Signature · 1 line
2.9.5
PATCH v6 20/38 crypto: ccp: Implement SEV_PDH_CERT_EXPORT ioctl command
The SEV_PDH_CERT_EXPORT command can be used to export the PDH and its certificate chain. The command is defined in SEV spec section 5.10.
---
Changes since v6:
* when sev_do_cmd() and sev_platform_shutdown() fails then propogate the error status code from sev_do_cmd() because it can give us much better reason for the failure.
drivers/crypto/ccp/psp-dev.c | 110 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+)
drivers/crypto/ccp/psp-dev.c +110 -0
@@ -390,6 +390,113 @@ static int sev_ioctl_do_pek_cert_import(struct sev_issue_cmd *argp)return ret; } +static int sev_ioctl_do_pdh_cert_export (struct sev_issue_cmd *argp) +{ +struct sev_user_data_pdh_cert_export input; +void *pdh_blob =NULL , *cert_blob =NULL ; +struct sev_data_pdh_cert_export *data; +int ret, err; + +if (copy_from_user (&input, (void __user *)argp->data,sizeof (input))) +return -EFAULT ; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + +/* Userspace wants to query the certificate length */ +if (!input.pdh_cert_address || !input.pdh_cert_len || + !input.cert_chain_address || !input.cert_chain_address) + goto cmd; + +/* allocate a physically contiguous buffer to store the PDH blob */ +if (!access_ok (VERIFY_WRITE , input.pdh_cert_address, input.pdh_cert_len) || + (input.pdh_cert_len >SEV_FW_BLOB_MAX_SIZE )) { + ret = -EFAULT ; + goto e_free; + } + + pdh_blob =kmalloc (input.pdh_cert_len,GFP_KERNEL ); +if (!pdh_blob) { + ret = -ENOMEM ; + goto e_free; + } + + data->pdh_cert_address =__psp_pa (pdh_blob); + data->pdh_cert_len = input.pdh_cert_len; + +/* allocate a physically contiguous buffer to store the cert chain blob */ +if (!access_ok (VERIFY_WRITE , input.cert_chain_address, input.cert_chain_len) || + (input.cert_chain_len >SEV_FW_BLOB_MAX_SIZE )) { + ret = -EFAULT ; + goto e_free_pdh; + } + + cert_blob =kmalloc (input.cert_chain_len,GFP_KERNEL ); +if (!cert_blob) { + ret = -ENOMEM ; + goto e_free_pdh; + } + + data->cert_chain_address =__psp_pa (cert_blob); + data->cert_chain_len = input.cert_chain_len; + +cmd: + ret =sev_platform_init (NULL , &argp->error); +if (ret) + goto e_free_cert; + + ret =sev_do_cmd (SEV_CMD_PDH_CERT_EXPORT , data, &argp->error); + +/* +* If we query the length, FW responded with expected data +*/ + input.cert_chain_len = data->cert_chain_len; + input.pdh_cert_len = data->pdh_cert_len; + +if (sev_platform_shutdown (&err)) { +/* +* If both sev_do_cmd() and sev_platform_shutdown() commands +* failed then propogate the error code from the sev_do_cmd() +* because it contains a useful status code for the command +* failure. +*/ +if (ret) + goto e_free_cert; + + ret = -EIO ; + argp->error = err; + goto e_free_cert; + } + +if (copy_to_user ((void __user *)argp->data, &input,sizeof (input))) { + ret = -EFAULT ; + goto e_free_cert; + } + +if (pdh_blob) { +if (copy_to_user ((void __user *)input.pdh_cert_address, + pdh_blob, input.pdh_cert_len)) { + ret = -EFAULT ; + goto e_free_cert; + } + } + +if (cert_blob) { +if (copy_to_user ((void __user *)input.cert_chain_address, + cert_blob, input.cert_chain_len)) + ret = -EFAULT ; + } + +e_free_cert: +kfree (cert_blob); +e_free_pdh: +kfree (pdh_blob); +e_free: +kfree (data); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;
@@ -425,6 +532,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_PEK_CERT_IMPORT : ret =sev_ioctl_do_pek_cert_import (&input);break ; +case SEV_PDH_CERT_EXPORT : + ret =sev_ioctl_do_pdh_cert_export (&input); +break ;default : ret = -EINVAL ; goto out;
Signature · 1 line
2.9.5
Re: [Part2 PATCH v6.1 20/38] crypto: ccp: Implement SEV_PDH_CERT_EXPORT ioctl command
Quoted 13 lines · Brijesh Singh
The
SEV_PDH_CERT_EXPORTcommand can be used to export the PDH and its certificate chain. The command is defined in SEV spec section 5.10.Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Borislav Petkov <[email protected]>Cc Herbert Xu <[email protected]>Cc Gary Hook <[email protected]>Cc Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>
Quoted 139 lines
---
Changes since v6:
* whensev_do_cmd()andsev_platform_shutdown()fails then propogate the error status code fromsev_do_cmd()because it can give us much better reason for the failure.drivers/crypto/ccp/psp-dev.c | 110 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+)drivers/crypto/ccp/psp-dev.c +110
-0@@ -390,6 +390,113 @@ static int sev_ioctl_do_pek_cert_import(struct sev_issue_cmd *argp)return ret; } +static int sev_ioctl_do_pdh_cert_export (struct sev_issue_cmd *argp) +{ +struct sev_user_data_pdh_cert_export input; +void *pdh_blob =NULL , *cert_blob =NULL ; +struct sev_data_pdh_cert_export *data; +int ret, err; + +if (copy_from_user (&input, (void __user *)argp->data,sizeof (input))) +return -EFAULT ; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + +/* Userspace wants to query the certificate length */ +if (!input.pdh_cert_address || !input.pdh_cert_len || + !input.cert_chain_address || !input.cert_chain_address) + goto cmd; + +/* allocate a physically contiguous buffer to store the PDH blob */ +if (!access_ok (VERIFY_WRITE , input.pdh_cert_address, input.pdh_cert_len) || + (input.pdh_cert_len >SEV_FW_BLOB_MAX_SIZE )) { + ret = -EFAULT ; + goto e_free; + } + + pdh_blob =kmalloc (input.pdh_cert_len,GFP_KERNEL ); +if (!pdh_blob) { + ret = -ENOMEM ; + goto e_free; + } + + data->pdh_cert_address =__psp_pa (pdh_blob); + data->pdh_cert_len = input.pdh_cert_len; + +/* allocate a physically contiguous buffer to store the cert chain blob */ +if (!access_ok (VERIFY_WRITE , input.cert_chain_address, input.cert_chain_len) || + (input.cert_chain_len >SEV_FW_BLOB_MAX_SIZE )) { + ret = -EFAULT ; + goto e_free_pdh; + } + + cert_blob =kmalloc (input.cert_chain_len,GFP_KERNEL ); +if (!cert_blob) { + ret = -ENOMEM ; + goto e_free_pdh; + } + + data->cert_chain_address =__psp_pa (cert_blob); + data->cert_chain_len = input.cert_chain_len; + +cmd: + ret =sev_platform_init (NULL , &argp->error); +if (ret) + goto e_free_cert; + + ret =sev_do_cmd (SEV_CMD_PDH_CERT_EXPORT , data, &argp->error); + +/* +* If we query the length, FW responded with expected data +*/ + input.cert_chain_len = data->cert_chain_len; + input.pdh_cert_len = data->pdh_cert_len; + +if (sev_platform_shutdown (&err)) { +/* +* If both sev_do_cmd() and sev_platform_shutdown() commands +* failed then propogate the error code from the sev_do_cmd() +* because it contains a useful status code for the command +* failure. +*/ +if (ret) + goto e_free_cert; + + ret = -EIO ; + argp->error = err; + goto e_free_cert; + } + +if (copy_to_user ((void __user *)argp->data, &input,sizeof (input))) { + ret = -EFAULT ; + goto e_free_cert; + } + +if (pdh_blob) { +if (copy_to_user ((void __user *)input.pdh_cert_address, + pdh_blob, input.pdh_cert_len)) { + ret = -EFAULT ; + goto e_free_cert; + } + } + +if (cert_blob) { +if (copy_to_user ((void __user *)input.cert_chain_address, + cert_blob, input.cert_chain_len)) + ret = -EFAULT ; + } + +e_free_cert: +kfree (cert_blob); +e_free_pdh: +kfree (pdh_blob); +e_free: +kfree (data); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;@@ -425,6 +532,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_PEK_CERT_IMPORT : ret =sev_ioctl_do_pek_cert_import (&input);break ; +case SEV_PDH_CERT_EXPORT : + ret =sev_ioctl_do_pdh_cert_export (&input); +break ;default : ret = -EINVAL ; goto out;
PATCH v6 21/38 KVM: X86: Add CONFIG_KVM_AMD_SEV
In reply to Brijesh SinghThe config option can be used to enable SEV support on AMD Processors.
--- arch/x86/kvm/Kconfig | 10 ++++++++++ 1 file changed, 10 insertions(+)
arch/x86/kvm/Kconfig +10 -0
@@ -80,6 +80,16 @@ config KVM_AMD To compile this as a module, choose M here: the module will be called kvm-amd. +config KVM_AMD_SEV + def_bool y + bool "AMD Secure Encrypted Virtualization (SEV) support" + depends on KVM_AMD && X86_64 + select CRYPTO_DEV_CCP + select CRYPTO_DEV_CCP_DD + select CRYPTO_DEV_SP_PSP + ---help--- + Provides support for launching Encrypted VMs on AMD processors. + config KVM_MMU_AUDIT bool "Audit KVM MMU" depends on KVM && TRACEPOINTS
Signature · 1 line
2.9.5
PATCH v6 22/38 KVM: SVM: Add sev module_param
In reply to Brijesh SinghThe module parameter can be used to control the SEV feature support.
--- arch/x86/kvm/svm.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/kvm/svm.c +4 -0
@@ -284,6 +284,10 @@ module_param(vls, int, 0444);static int vgif = true;module_param (vgif, int,0444 ); +/* enable/disable SEV support */ +static int sev =IS_ENABLED (CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT ); +module_param (sev, int,0444 ); +static void svm_set_cr0 (struct kvm_vcpu *vcpu,unsigned long cr0);static void svm_flush_tlb (struct kvm_vcpu *vcpu);static void svm_complete_interrupts (struct vcpu_svm *svm);
Signature · 1 line
2.9.5
PATCH v6 23/38 KVM: SVM: Reserve ASID range for SEV guest
In reply to Brijesh SinghA SEV-enabled guest must use ASIDs from the defined subset, while non-SEV guests can use the remaining ASID range. The range of allowed SEV guest ASIDs is [1 - CPUID_8000_001F[ECX][31:0]].
--- arch/x86/kvm/svm.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)
arch/x86/kvm/svm.c +22 -1
@@ -323,6 +323,8 @@ enum {#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL +static unsigned int max_sev_asid; +static inline void mark_all_dirty (struct vmcb *vmcb) { vmcb->control.clean =0 ;
@@ -787,7 +789,7 @@ static int svm_hardware_enable(void) sd->asid_generation =1 ; sd->max_asid =cpuid_ebx (SVM_CPUID_FUNC ) -1 ; sd->next_asid = sd->max_asid +1 ; - sd->min_asid =1 ; + sd->min_asid = max_sev_asid +1 ; gdt =get_current_gdt_rw (); sd->tss_desc = (struct kvm_ldttss_desc *)(gdt +GDT_ENTRY_TSS );
@@ -1054,6 +1056,15 @@ static int avic_ga_log_notifier(u32 ga_tag)return 0 ; } +/* +* Get the maximum number of encrypted guests: +* Fn8001_001F[ECX][31:0]: Number of supported guests. +*/ +static __init void sev_hardware_setup (void ) +{ + max_sev_asid =cpuid_ecx (0x8000001F ); +} +static __init int svm_hardware_setup (void ) {int cpu;
@@ -1084,6 +1095,16 @@ static __init int svm_hardware_setup(void) kvm_tsc_scaling_ratio_frac_bits =32 ; } +if (sev) { +if (boot_cpu_has (X86_FEATURE_SEV ) && +IS_ENABLED (CONFIG_KVM_AMD_SEV )) { +sev_hardware_setup (); +pr_info ("SEV supported\n" ); + }else { + sev = false; + } + } +if (nested) {printk (KERN_INFO "kvm: Nested Virtualization enabled\n" );kvm_enable_efer_bits (EFER_SVME |EFER_LMSLE );
Signature · 1 line
2.9.5
Re: [Part2 PATCH v6 23/38] KVM: SVM: Reserve ASID range for SEV guest
Quoted 17 lines · Brijesh Singh
A SEV-enabled guest must use ASIDs from the defined subset, while non-SEV guests can use the remaining ASID range. The range of allowed SEV guest ASIDs is [1 - CPUID_8000_001F[ECX][31:0]].
Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Joerg Roedel <[email protected]>Cc Borislav Petkov <[email protected]>Cc Tom Lendacky <[email protected]>Improvements-by Borislav Petkov <[email protected]>Signed-off-by Brijesh Singh <[email protected]>
Quoted 3 lines
--- arch/x86/kvm/svm.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--PATCH v6 24/38 KVM: Define SEV key management command id
In reply to Brijesh SinghDefine Secure Encrypted Virtualization (SEV) key management command id and structure. The command definition is available in SEV KM [1] spec 0.14 (http://support.amd.com/TechDocs/55766_SEV-KM API_Specification.pdf) and Documentation/virtual/kvm/amd-memory-encryption.txt.
--- .../virtual/kvm/amd-memory-encryption.txt | 163 +++++++++++++++++++++ include/uapi/linux/kvm.h | 80 ++++++++++ 2 files changed, 243 insertions(+)
Documentation/virtual/kvm/amd-memory-encryption.txt +163 -0
@@ -36,3 +36,166 @@ setting the SEV bit before executing VMRUN. SEV hardware uses ASIDs to associate a memory encryption key with a VM. Hence, the ASID for the SEV-enabled guests must be from 1 to a maximum value defined in the CPUID 0x8000001f[ecx] field. + +SEV Key Management +------------------ + +The SEV guest key management is handled by a separate processor called the AMD +Secure Processor (AMD-SP). Firmware running inside the AMD-SP provides a secure +key management interface to perform common hypervisor activities such as +encrypting bootstrap code, snapshot, migrating and debugging the guest. For more +information, see the SEV Key Management spec: +http://support.amd.com/TechDocs/55766_SEV-KM%20API_Specification.pdf + +KVM implements the following commands to support common lifecycle events of SEV +guests, such as launching, running, snapshotting, migrating and decommissioning. + +1. KVM_SEV_INIT + +Returns: 0 on success, -negative on error + +The KVM_SEV_INIT command is used by the hypervisor to initialize the SEV platform +context. In a typical workflow, this command should be the first command issued. + +2. KVM_SEV_LAUNCH_START + +Parameters: struct kvm_sev_launch_start (in/out) +Returns: 0 on success, -negative on error + +The KVM_SEV_LAUNCH_START command is used for creating the memory encryption +context. To create the encryption context, user must provide a guest policy, +the owner's public Diffie-Hellman (PDH) key and session information. + +struct kvm_sev_launch_start { + __u32 handle; /* if zero then firmware creates a new handle */ + __u32 policy; /* guest's policy */ + + __u64 dh_uaddr; /* userspace address pointing to the guest owner's PDH key */ + __u32 dh_len; + + __u64 session_addr; /* userspace address which points to the guest session information */ + __u32 session_len; +}; + +On success, the 'handle' field contains a new handle and on error, a negative value. + +For more details, see SEV spec Section 6.2. + +3. KVM_SEV_LAUNCH_UPDATE_DATA + +Parameters (in): struct kvm_sev_launch_update_data +Returns: 0 on success, -negative on error + +The KVM_SEV_LAUNCH_UPDATE_DATA 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. + +struct kvm_sev_launch_update { + __u64 uaddr; /* userspace address to be encrypted (must be 16-byte aligned) */ + __u32 len; /* length of the data to be encrypted (must be 16-byte aligned) */ +}; + +For more details, see SEV spec Section 6.3. + +4. KVM_SEV_LAUNCH_MEASURE + +Parameters (in): struct kvm_sev_launch_measure +Returns: 0 on success, -negative on error + +The KVM_SEV_LAUNCH_MEASURE command is used to retrieve the measurement of the +data encrypted by the KVM_SEV_LAUNCH_UPDATE_DATA command. The guest owner may +wait to provide the guest with confidential information until it can verify the +measurement. Since the guest owner knows the initial contents of the guest at +boot, the measurement can be verified by comparing it to what the guest owner +expects. + +struct kvm_sev_launch_measure { + __u64 uaddr; /* where to copy the measurement */ + __u32 len; /* length of measurement blob */ +}; + +For more details on the measurement verification flow, see SEV spec Section 6.4. + +5. KVM_SEV_LAUNCH_FINISH + +Returns: 0 on success, -negative on error + +After completion of the launch flow, the KVM_SEV_LAUNCH_FINISH command can be +issued to make the guest ready for the execution. + +6. KVM_SEV_GUEST_STATUS + +Parameters (out): struct kvm_sev_guest_status +Returns: 0 on success, -negative on error + +The KVM_SEV_GUEST_STATUS command is used to retrieve status information about a +SEV-enabled guest. + +struct kvm_sev_guest_status { + __u32 handle; /* guest handle */ + __u32 policy; /* guest policy */ + __u8 state; /* guest state (see enum below) */ +}; + +SEV guest state: + +enum { + SEV_STATE_INVALID = 0; + SEV_STATE_LAUNCHING, /* guest is currently being launched */ + SEV_STATE_SECRET, /* guest is being launched and ready to accept the ciphertext data */ + SEV_STATE_RUNNING, /* guest is fully launched and running */ + SEV_STATE_RECEIVING, /* guest is being migrated in from another SEV machine */ + SEV_STATE_SENDING /* guest is getting migrated out to another SEV machine */ +}; + +7. KVM_SEV_DBG_DECRYPT + +Parameters (in): struct kvm_sev_dbg +Returns: 0 on success, -negative on error + +The KVM_SEV_DEBUG_DECRYPT command can be used by the hypervisor to request the +firmware to decrypt the data at the given memory region. + +struct kvm_sev_dbg { + __u64 src_uaddr; /* userspace address of data to decrypt */ + __u64 dst_uaddr; /* userspace address of destination */ + __u32 len; /* length of memory region to decrypt */ +}; + +The command returns an error if the guest policy does not allow debugging. + +8. KVM_SEV_DBG_ENCRYPT + +Parameters (in): struct kvm_sev_dbg +Returns: 0 on success, -negative on error + +The KVM_SEV_DEBUG_ENCRYPT command can be used by the hypervisor to request the +firmware to encrypt the data at the given memory region. + +struct kvm_sev_dbg { + __u64 src_uaddr; /* userspace address of data to encrypt */ + __u64 dst_uaddr; /* userspace address of destination */ + __u32 len; /* length of memory region to encrypt */ +}; + +The command returns an error if the guest policy does not allow debugging. + +9. KVM_SEV_LAUNCH_SECRET + +Parameters (in): struct kvm_sev_launch_secret +Returns: 0 on success, -negative on error + +The KVM_SEV_LAUNCH_SECRET command can be used by the hypervisor to inject secret +data after the measurement has been validated by the guest owner. + +struct kvm_sev_launch_secret { + __u64 hdr_uaddr; /* userspace address containing the packet header */ + __u32 hdr_len; + + __u64 guest_uaddr; /* the guest memory region where the secret should be injected */ + __u32 guest_len; + + __u64 trans_uaddr; /* the hypervisor memory region which contains the secret */ + __u32 trans_len; +};
include/uapi/linux/kvm.h +80 -0
86 lines not on this page · raw message
Signature · 1 line
2.9.5
Re: [Part2 PATCH v6 24/38] KVM: Define SEV key management command id
Quoted 5 lines · Brijesh Singh
Define Secure Encrypted Virtualization (SEV) key management command id and structure. The command definition is available in SEV KM [1] spec 0.14 (http://support.amd.com/TechDocs/55766_SEV-KM API_Specification.pdf) and
Documentation/virtual/kvm/amd-memory-encryption.txt.
Quoted 12 lines
Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Joerg Roedel <[email protected]>Cc Borislav Petkov <[email protected]>Cc Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>
Pls add my Improvements-by tag.
Also
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--PATCH v6 25/38 KVM: SVM: Add KVM_SEV_INIT command
In reply to Brijesh SinghThe command initializes the SEV platform context and allocates a new ASID for this guest from the SEV ASID pool. The firmware must be initialized before we issue any guest launch commands to create a new memory encryption context.
--- arch/x86/include/asm/kvm_host.h | 7 +++ arch/x86/kvm/svm.c | 132 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 138 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +7 -0
@@ -747,6 +747,11 @@ enum kvm_irqchip_mode { KVM_IRQCHIP_SPLIT, /* created with KVM_CAP_SPLIT_IRQCHIP */ }; +struct kvm_sev_info { +bool active;/* SEV enabled guest */ +unsigned int asid;/* ASID used for this guest */ +}; +struct kvm_arch {unsigned int n_used_mmu_pages;unsigned int n_requested_mmu_pages;
@@ -834,6 +839,8 @@ struct kvm_arch {bool x2apic_format;bool x2apic_broadcast_quirk_disabled; + +struct kvm_sev_info sev_info; };struct kvm_vm_stat {
arch/x86/kvm/svm.c +131 -1
196 lines not on this page · raw message
Signature · 1 line
2.9.5
Borislav Petkov
maintainerRe: [Part2 PATCH v6 25/38] KVM: SVM: Add KVM_SEV_INIT command
Quoted 21 lines · Brijesh Singh
The command initializes the SEV platform context and allocates a new ASID for this guest from the SEV ASID pool. The firmware must be initialized before we issue any guest launch commands to create a new memory encryption context.
Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Joerg Roedel <[email protected]>Cc Borislav Petkov <[email protected]>Cc Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/kvm_host.h | 7 +++ arch/x86/kvm/svm.c | 132 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 138 insertions(+), 1 deletion(-)
...
Quoted 5 lines
arch/x86/kvm/svm.c +1
-0@@ -1167,10 +1187,18 @@ static __init int svm_hardware_setup(void)return r; } +static __exit void sev_hardware_unsetup(void)
arch/x86/kvm/svm.c static __init int svm_hardware_setup(void) · line 1200
Quoted 10 lines
arch/x86/kvm/svm.c +6
-0+{ +kfree (sev_asid_bitmap); +} + static __exit void svm_hardware_unsetup(void) {int cpu; +if (svm_sev_enabled ()) +sev_hardware_unsetup ();
Move that svm_sev_enabled() check into the function.
Quoted 15 lines
arch/x86/kvm/svm.c +4
-0+for_each_possible_cpu (cpu)svm_cpu_uninit (cpu);@@ -1361,6 +1389,9 @@ static void init_vmcb(struct vcpu_svm *svm) svm->vmcb->control.int_ctl |=V_GIF_ENABLE_MASK ; } +if (sev_guest (svm->vcpu.kvm)) + svm->vmcb->control.nested_ctl |=SVM_NESTED_CTL_SEV_ENABLE ; +mark_all_dirty (svm->vmcb);enable_gif (svm);
arch/x86/kvm/svm.c static int avic_init_backing_page(struct kvm_vcpu *vcpu) · line 1483
Quoted 10 lines
arch/x86/kvm/svm.c +7
-0@@ -1443,6 +1474,28 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)return 0 ; } +static void sev_asid_free (struct kvm *kvm) +{ +struct kvm_sev_info *sev = &kvm->arch.sev_info; +int pos, asid; + +if (!svm_sev_enabled ()) +return ;
You're already checking !sev_guest() below - no need to do the svm_sev_enabled() check again.
Quoted 33 lines
arch/x86/kvm/svm.c +21
-0+ + asid = sev->asid; + pos = asid -1 ; +clear_bit (pos, sev_asid_bitmap); +} + +static void sev_vm_destroy (struct kvm *kvm) +{ +if (!sev_guest (kvm)) +return ; + +sev_platform_shutdown (NULL ); +sev_asid_free (kvm); +} +static void avic_vm_destroy (struct kvm *kvm) {unsigned long flags;@@ -1461,6 +1514,12 @@ static void avic_vm_destroy(struct kvm *kvm)spin_unlock_irqrestore (&svm_vm_data_hash_lock, flags); } +static void svm_vm_destroy (struct kvm *kvm) +{ +avic_vm_destroy (kvm); +sev_vm_destroy (kvm); +} +static int avic_vm_init (struct kvm *kvm) {arch/x86/kvm/svm.c +0
-0unsigned long flags;
arch/x86/kvm/svm.c static void svm_setup_mce(struct kvm_vcpu *vcpu) · line 5494
Quoted 9 lines
arch/x86/kvm/svm.c +6
-0@@ -5427,6 +5486,75 @@ static void svm_setup_mce(struct kvm_vcpu *vcpu) vcpu->arch.mcg_cap &=0x1ff ; } +static int sev_asid_new (void ) +{ +int pos; + +if (!svm_sev_enabled ()) +return -EINVAL ;
You already checked that in svm_mem_enc_op() - no need to do it again.
Quoted 9 lines
arch/x86/kvm/svm.c +9
-0+ pos =find_first_zero_bit (sev_asid_bitmap, max_sev_asid); +if (pos >= max_sev_asid) +return -EBUSY ; + +set_bit (pos, sev_asid_bitmap); +return pos +1 ; +} + +static int sev_guest_init (struct kvm *kvm,struct kvm_sev_cmd *argp)
arch/x86/kvm/svm.c static void svm_setup_mce(struct kvm_vcpu *vcpu) · line 5514
Quoted 10 lines
arch/x86/kvm/svm.c +10
-0+{ +struct kvm_sev_info *sev = &kvm->arch.sev_info; +int asid, ret; + + ret =sev_platform_init (NULL , &argp->error); +if (ret) +return ret; + + ret = -EBUSY ; + asid =sev_asid_new ();
This operation is cheaper so do it first and then sev_platform_init() so that in case sev_asid_new() fails, you don't "toggle" the PSP unnecessarily.
Quoted 13 lines
arch/x86/kvm/svm.c +13
-0+if (asid <0 ) + goto e_shutdown; + + sev->active = true; + sev->asid = asid; + +return 0 ; + +e_shutdown: +sev_platform_shutdown (&argp->error); +return ret; +} +
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Re: [Part2 PATCH v6 25/38] KVM: SVM: Add KVM_SEV_INIT command
On 10/27/17 3:24 PM, Borislav Petkov wrote:
...
Quoted 8 lines
arch/x86/kvm/svm.c static __init int svm_hardware_setup(void) · line 1200
Quoted 7 lines
arch/x86/kvm/svm.c +3
-0+ static __exit void svm_hardware_unsetup(void) {int cpu; +if (svm_sev_enabled ()) +sev_hardware_unsetup ();Move that
svm_sev_enabled()check into the function.
Sure, I will remove the check.
Quoted 19 lines
Quoted 19 lines
arch/x86/kvm/svm.c +4
-0+for_each_possible_cpu (cpu)svm_cpu_uninit (cpu);@@ -1361,6 +1389,9 @@ static void init_vmcb(struct vcpu_svm *svm) svm->vmcb->control.int_ctl |=V_GIF_ENABLE_MASK ; } +if (sev_guest (svm->vcpu.kvm)) + svm->vmcb->control.nested_ctl |=SVM_NESTED_CTL_SEV_ENABLE ; +mark_all_dirty (svm->vmcb);enable_gif (svm);@@ -1443,6 +1474,28 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)return 0 ; }
Quoted 9 lines
arch/x86/kvm/svm.c static int avic_init_backing_page(struct kvm_vcpu *vcpu) · line 1483
Quoted 7 lines
arch/x86/kvm/svm.c +7
-0+static void sev_asid_free (struct kvm *kvm) +{ +struct kvm_sev_info *sev = &kvm->arch.sev_info; +int pos, asid; + +if (!svm_sev_enabled ()) +return ;You're already checking !sev_guest() below - no need to do the
svm_sev_enabled()check again.
Agreed, I will remove it.
Quoted 6 lines
arch/x86/kvm/svm.c static void svm_setup_mce(struct kvm_vcpu *vcpu) · line 5494
Quoted 5 lines
arch/x86/kvm/svm.c +5
-0+{ +int pos; + +if (!svm_sev_enabled ()) +return -EINVAL ;You already checked that in
svm_mem_enc_op()- no need to do it again.
Agreed, I will remove it
Quoted 15 lines
Quoted 15 lines
arch/x86/kvm/svm.c +14
-0+ pos =find_first_zero_bit (sev_asid_bitmap, max_sev_asid); +if (pos >= max_sev_asid) +return -EBUSY ; + +set_bit (pos, sev_asid_bitmap); +return pos +1 ; +} + +static int sev_guest_init (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_info *sev = &kvm->arch.sev_info; +int asid, ret; + + ret =sev_platform_init (NULL , &argp->error);
Quoted 8 lines
arch/x86/kvm/svm.c static void svm_setup_mce(struct kvm_vcpu *vcpu) · line 5514
Quoted 5 lines
arch/x86/kvm/svm.c +5
-0+if (ret) +return ret; + + ret = -EBUSY ; + asid =sev_asid_new ();This operation is cheaper so do it first and then
sev_platform_init()so that in casesev_asid_new()fails, you don't "toggle" the PSP unnecessarily.
Sure, I will change the order.
PATCH v6 26/38 KVM: SVM: VMRUN should use assosiated ASID when SEV is enabled
In reply to Brijesh SinghSEV hardware uses ASIDs to associate a memory encryption key with a guest VM. During guest creation, a SEV VM uses the SEV_CMD_ACTIVATE command to bind a particular ASID to the guest. Lets make sure that the VMCB is programmed with the bound ASID before a VMRUN.
--- arch/x86/kvm/svm.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-)
arch/x86/kvm/svm.c +56 -2
@@ -213,6 +213,9 @@ struct vcpu_svm {*/ struct list_head ir_list;spinlock_t ir_list_lock; + +/* which host CPU was used for running this vcpu */ +unsigned int last_cpu; };/*
@@ -340,6 +343,13 @@ static inline bool sev_guest(struct kvm *kvm)return sev->active; } +static inline int sev_get_asid (struct kvm *kvm) +{ +struct kvm_sev_info *sev = &kvm->arch.sev_info; + +return sev->asid; +} +static inline void mark_all_dirty (struct vmcb *vmcb) { vmcb->control.clean =0 ;
@@ -550,6 +560,9 @@ struct svm_cpu_data {struct kvm_ldttss_desc *tss_desc;struct page *save_area; + +/* index = sev_asid, value = vmcb pointer */ +struct vmcb **sev_vmcbs; }; static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
@@ -863,6 +876,7 @@ static void svm_cpu_uninit(int cpu)return ;per_cpu (svm_data,raw_smp_processor_id ()) =NULL ; +kfree (sd->sev_vmcbs);__free_page (sd->save_area);kfree (sd); }
@@ -876,11 +890,18 @@ static int svm_cpu_init(int cpu)if (!sd)return -ENOMEM ; sd->cpu = cpu; - sd->save_area =alloc_page (GFP_KERNEL ); r = -ENOMEM ; + sd->save_area =alloc_page (GFP_KERNEL );if (!sd->save_area) goto err_1; +if (svm_sev_enabled ()) { + r = -ENOMEM ; + sd->sev_vmcbs =kmalloc ((max_sev_asid +1 ) *sizeof (void *),GFP_KERNEL ); +if (!sd->sev_vmcbs) + goto err_1; + } +per_cpu (svm_data, cpu) = sd;return 0 ;
@@ -1477,7 +1498,8 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)static void sev_asid_free (struct kvm *kvm) {struct kvm_sev_info *sev = &kvm->arch.sev_info; -int pos, asid; +struct svm_cpu_data *sd; +int pos, asid, cpu;if (!svm_sev_enabled ())return ;
@@ -1485,6 +1507,11 @@ static void sev_asid_free(struct kvm *kvm) asid = sev->asid; pos = asid -1 ;clear_bit (pos, sev_asid_bitmap); + +for_each_possible_cpu (cpu) { + sd =per_cpu (svm_data, cpu); + sd->sev_vmcbs[pos] =NULL ; + } }static void sev_vm_destroy (struct kvm *kvm)
@@ -4419,12 +4446,39 @@ static void reload_tss(struct kvm_vcpu *vcpu)load_TR_desc (); } +static void pre_sev_run (struct vcpu_svm *svm,int cpu) +{ +struct svm_cpu_data *sd =per_cpu (svm_data, cpu); +int asid =sev_get_asid (svm->vcpu.kvm); + +/* Assign the asid allocated with this SEV guest */ + svm->vmcb->control.asid = asid; + +/* +* Flush guest TLB: +* +* 1) when different VMCB for the same ASID is to be run on the same host CPU. +* 2) or this VMCB was executed on different host CPU in previous VMRUNs. +*/ +if (sd->sev_vmcbs[asid] == svm->vmcb && + svm->last_cpu == cpu) +return ; + + svm->last_cpu = cpu; + sd->sev_vmcbs[asid] = svm->vmcb; + svm->vmcb->control.tlb_ctl =TLB_CONTROL_FLUSH_ASID ; +mark_dirty (svm->vmcb,VMCB_ASID ); +} +static void pre_svm_run (struct vcpu_svm *svm) {int cpu =raw_smp_processor_id ();struct svm_cpu_data *sd =per_cpu (svm_data, cpu); +if (sev_guest (svm->vcpu.kvm)) +return pre_sev_run (svm, cpu); +/* FIXME: handle wraparound of asid_generation */ if (svm->asid_generation != sd->asid_generation)new_asid (svm, sd);
Signature · 1 line
2.9.5
PATCH v6 27/38 KVM: SVM: Add support for KVM_SEV_LAUNCH_START command
In reply to Brijesh SinghThe KVM_SEV_LAUNCH_START command is used to create a memory encryption context within the SEV firmware. In order to do so, the guest owner should provide the guest's policy, its public Diffie-Hellman (PDH) key and session information. The command implements the LAUNCH_START flow defined in SEV spec Section 6.2.
--- arch/x86/include/asm/kvm_host.h | 2 + arch/x86/kvm/svm.c | 153 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -750,6 +750,8 @@ enum kvm_irqchip_mode {struct kvm_sev_info {bool active;/* SEV enabled guest */ unsigned int asid;/* ASID used for this guest */ +unsigned int handle;/* SEV firmware handle */ +int fd;/* SEV device fd */ };struct kvm_arch {
arch/x86/kvm/svm.c +153 -0
176 lines not on this page · raw message
Signature · 1 line
2.9.5
PATCH v6 28/38 KVM: SVM: Add support for KVM_SEV_LAUNCH_UPDATE_DATA command
In reply to Brijesh SinghThe command is used for encrypting the guest memory region using the VM encryption key (VEK) created during KVM_SEV_LAUNCH_START.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm.c | 191 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 190 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -752,6 +752,7 @@ struct kvm_sev_info {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 kvm_arch {
arch/x86/kvm/svm.c +189 -2
239 lines not on this page · raw message
Signature · 1 line
2.9.5
Re: [Part2 PATCH v6 28/38] KVM: SVM: Add support for KVM_SEV_LAUNCH_UPDATE_DATA command
Quoted 15 lines · Brijesh Singh
The command is used for encrypting the guest memory region using the VM encryption key (VEK) created during
KVM_SEV_LAUNCH_START.Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Joerg Roedel <[email protected]>Cc Borislav Petkov <[email protected]>Cc Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>
Quoted 4 lines
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm.c | 191 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 190 insertions(+), 2 deletions(-)
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--PATCH v6 29/38 KVM: SVM: Add support for KVM_SEV_LAUNCH_MEASURE command
In reply to Brijesh SinghThe command is used to retrieve the measurement of contents encrypted through the KVM_SEV_LAUNCH_UPDATE_DATA command.
--- arch/x86/kvm/svm.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+)
arch/x86/kvm/svm.c +63 -0
@@ -5913,6 +5913,66 @@ static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; } +static int sev_launch_measure (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_info *sev = &kvm->arch.sev_info; +struct sev_data_launch_measure *data; +struct kvm_sev_launch_measure params; +void *blob; +int ret; + +if (!sev_guest (kvm)) +return -ENOTTY ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params))) +return -EFAULT ; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + blob =NULL ; +if (params.uaddr) { +if (params.len >SEV_FW_BLOB_MAX_SIZE ) { + ret = -EINVAL ; + goto e_free; + } + +if (!access_ok (VERIFY_WRITE , params.uaddr, params.len)) { + ret = -EFAULT ; + goto e_free; + } + + ret = -ENOMEM ; + blob =kmalloc (params.len,GFP_KERNEL ); +if (!blob) + goto e_free; + + data->address =__psp_pa (blob); + data->len = params.len; + } + + data->handle = sev->handle; + ret =sev_issue_cmd (kvm,SEV_CMD_LAUNCH_MEASURE , data, &argp->error); + + params.len = data->len; +if (copy_to_user ((void __user *)(uintptr_t )argp->data, ¶ms,sizeof (params))) { + ret = -EFAULT ; + goto e_free_blob; + } + +if (blob) { +if (copy_to_user ((void __user *)(uintptr_t )params.uaddr, blob, params.len)) + ret = -EFAULT ; + } + +e_free_blob: +kfree (blob); +e_free: +kfree (data); +return ret; +} +static int svm_mem_enc_op (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;
@@ -5936,6 +5996,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)case KVM_SEV_LAUNCH_UPDATE_DATA : r =sev_launch_update_data (kvm, &sev_cmd);break ; +case KVM_SEV_LAUNCH_MEASURE : + r =sev_launch_measure (kvm, &sev_cmd); +break ;default : r = -EINVAL ; goto out;
Signature · 1 line
2.9.5
Re: [Part2 PATCH v6 29/38] KVM: SVM: Add support for KVM_SEV_LAUNCH_MEASURE command
Quoted 15 lines · Brijesh Singh
The command is used to retrieve the measurement of contents encrypted through the
KVM_SEV_LAUNCH_UPDATE_DATAcommand.Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Joerg Roedel <[email protected]>Cc Borislav Petkov <[email protected]>Cc Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>
Quoted 3 lines
--- arch/x86/kvm/svm.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+)
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--PATCH v6 30/38 KVM: SVM: Add support for SEV LAUNCH_FINISH command
In reply to Brijesh SinghThe command is used for finializing the SEV guest launch process.
--- arch/x86/kvm/svm.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
arch/x86/kvm/svm.c +23 -0
@@ -5973,6 +5973,26 @@ static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; } +static int sev_launch_finish (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_info *sev = &kvm->arch.sev_info; +struct sev_data_launch_finish *data; +int ret; + +if (!sev_guest (kvm)) +return -ENOTTY ; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->handle = sev->handle; + ret =sev_issue_cmd (kvm,SEV_CMD_LAUNCH_FINISH , data, &argp->error); + +kfree (data); +return ret; +} +static int svm_mem_enc_op (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;
@@ -5999,6 +6019,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)case KVM_SEV_LAUNCH_MEASURE : r =sev_launch_measure (kvm, &sev_cmd);break ; +case KVM_SEV_LAUNCH_FINISH : + r =sev_launch_finish (kvm, &sev_cmd); +break ;default : r = -EINVAL ; goto out;
Signature · 1 line
2.9.5
Re: [Part2 PATCH v6 30/38] KVM: SVM: Add support for SEV LAUNCH_FINISH command
Quoted 14 lines · Brijesh Singh
The command is used for finializing the SEV guest launch process.
Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Joerg Roedel <[email protected]>Cc Borislav Petkov <[email protected]>Cc Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>
Quoted 3 lines
--- arch/x86/kvm/svm.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--PATCH v6 31/38 KVM: SVM: Add support for SEV GUEST_STATUS command
In reply to Brijesh SinghThe command is used for querying the SEV guest information.
--- arch/x86/kvm/svm.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
arch/x86/kvm/svm.c +33 -0
@@ -5993,6 +5993,36 @@ static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; } +static int sev_guest_status (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_info *sev = &kvm->arch.sev_info; +struct kvm_sev_guest_status params; +struct sev_data_guest_status *data; +int ret; + +if (!sev_guest (kvm)) +return -ENOTTY ; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->handle = sev->handle; + ret =sev_issue_cmd (kvm,SEV_CMD_GUEST_STATUS , data, &argp->error); +if (ret) + goto e_free; + + params.policy = data->policy; + params.state = data->state; + params.handle = data->handle; + +if (copy_to_user ((void __user *)(uintptr_t )argp->data, ¶ms,sizeof (params))) + ret = -EFAULT ; +e_free: +kfree (data); +return ret; +} +static int svm_mem_enc_op (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;
@@ -6022,6 +6052,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)case KVM_SEV_LAUNCH_FINISH : r =sev_launch_finish (kvm, &sev_cmd);break ; +case KVM_SEV_GUEST_STATUS : + r =sev_guest_status (kvm, &sev_cmd); +break ;default : r = -EINVAL ; goto out;
Signature · 1 line
2.9.5
Re: [Part2 PATCH v6 31/38] KVM: SVM: Add support for SEV GUEST_STATUS command
Quoted 14 lines · Brijesh Singh
The command is used for querying the SEV guest information.
Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Joerg Roedel <[email protected]>Cc Borislav Petkov <[email protected]>Cc Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>
Quoted 3 lines
--- arch/x86/kvm/svm.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--PATCH v6 32/38 KVM: SVM: Add support for SEV DEBUG_DECRYPT command
In reply to Brijesh SinghThe command is used for decrypting a guest memory region for debug purposes.
--- arch/x86/kvm/svm.c | 179 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+)
arch/x86/kvm/svm.c +179 -0
@@ -6023,6 +6023,182 @@ static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; } +static int __sev_issue_dbg_cmd (struct kvm *kvm,unsigned long src, +unsigned long dst,int size, +int *error,bool enc) +{ +struct kvm_sev_info *sev = &kvm->arch.sev_info; +struct sev_data_dbg *data; +int ret; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->handle = sev->handle; + data->dst_addr = dst; + data->src_addr = src; + data->len = size; + + ret =sev_issue_cmd (kvm, + enc ?SEV_CMD_DBG_ENCRYPT :SEV_CMD_DBG_DECRYPT , + data, error); +kfree (data); +return ret; +} + +/* +* Decrypt source memory into userspace or kernel buffer. If destination buffer +* or len is not aligned to 16-byte boundary then it uses intermediate buffer. +*/ +static int __sev_dbg_decrypt (struct kvm *kvm,unsigned long paddr, +unsigned long __user dst_uaddr, +unsigned long dst_kaddr,unsigned long dst_paddr, +int size,int *error) +{ +int ret, offset =0 , len = size; +struct page *tpage =NULL ; + +/* +* Debug command works with 16-byte aligned inputs, check if all inputs +* (src, dst and len) are 16-byte aligned. If one of the input is not +* aligned then we decrypt more than requested into a temporary buffer +* and copy the porition of data into destination buffer. +*/ +if (!IS_ALIGNED (paddr,16 ) || + !IS_ALIGNED (dst_paddr,16 ) || + !IS_ALIGNED (size,16 )) { + tpage = (void *)alloc_page (GFP_KERNEL ); +if (!tpage) +return -ENOMEM ; + + dst_paddr =__sme_page_pa (tpage); + +/* +* if source buffer is not aligned then offset will be used +* when copying the data from the temporary buffer into +* destination buffer. +*/ + offset = paddr &15 ; + +/* its safe to read more than requested size. */ + len =round_up (size + offset,16 ); + + paddr =round_down (paddr,16 ); + +/* +* The temporary buffer may have mapping with C=0 or C=1 on x86 +* side but PSP will will write the memory region with C=0. +* Lets make sure x86 cache for this memory range is flushed so +* that we can see the recent contents after the command +* completes. +*/ +clflush_cache_range (page_address (tpage),PAGE_SIZE ); + } + + ret =__sev_issue_dbg_cmd (kvm, paddr, dst_paddr, len, error, false); + +/* +* If temporary buffer is used then copy the data from temporary buffer +* into destination buffer. +*/ +if (!ret && tpage) { +/* +* If destination buffer is a userspace buffer then use +* copy_to_user otherwise memcpy. +*/ +if (dst_uaddr) { +if (copy_to_user ((void __user *)(uintptr_t )dst_uaddr, +page_address (tpage) + offset, size)) + ret = -EFAULT ; + }else { +memcpy ((void *)dst_kaddr,page_address (tpage) + offset, size); + } + } + +if (tpage) +__free_page (tpage); + +return ret; +} + +static int sev_dbg_crypt (struct kvm *kvm,struct kvm_sev_cmd *argp,bool dec) +{ +unsigned long vaddr, vaddr_end, next_vaddr; +unsigned long dst_vaddr, dst_vaddr_end; +struct page **src_p, **dst_p; +struct kvm_sev_dbg debug; +unsigned long n; +int ret, size; + +if (!sev_guest (kvm)) +return -ENOTTY ; + +if (copy_from_user (&debug, (void __user *)(uintptr_t )argp->data,sizeof (debug))) +return -EFAULT ; + + vaddr = debug.src_uaddr; + size = debug.len; + vaddr_end = vaddr + size; + dst_vaddr = debug.dst_uaddr; + dst_vaddr_end = dst_vaddr + size; + +for (; vaddr < vaddr_end; vaddr = next_vaddr) { +int len, s_off, d_off; + +/* lock userspace source and destination page */ + src_p =sev_pin_memory (kvm, vaddr &PAGE_MASK ,PAGE_SIZE , &n,0 ); +if (!src_p) +return -EFAULT ; + + dst_p =sev_pin_memory (kvm, dst_vaddr &PAGE_MASK ,PAGE_SIZE , &n,1 ); +if (!dst_p) { +sev_unpin_memory (kvm, src_p, n); +return -EFAULT ; + } + +/* +* Depending on the operation type (encrypt or decrypt), PSP +* will access the source and destination buffer with C=0 or +* C=1. Lets make sure that caches are flushed so that data gets +* accessed with correct C-bit. +*/ +sev_clflush_pages (src_p,1 ); +sev_clflush_pages (dst_p,1 ); + +/* +* since user buffer may not be page aligned, calculate the +* offset within the page. +*/ + s_off = vaddr & ~PAGE_MASK ; + d_off = dst_vaddr & ~PAGE_MASK ; + len =min_t (size_t, (PAGE_SIZE - s_off), size); + + ret =__sev_dbg_decrypt (kvm, +__sme_page_pa (src_p[0 ]) + s_off, + dst_vaddr,0 , +__sme_page_pa (dst_p[0 ]) + d_off, + len, &argp->error); + +sev_unpin_memory (kvm, src_p,1 ); +sev_unpin_memory (kvm, dst_p,1 ); + +if (ret) + goto err; + + next_vaddr = vaddr + len; + dst_vaddr = dst_vaddr + len; + size -= len; + } +err: +return ret; +} + +static int sev_dbg_decrypt (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +return sev_dbg_crypt (kvm, argp, true); +} +static int svm_mem_enc_op (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;
@@ -6055,6 +6231,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)case KVM_SEV_GUEST_STATUS : r =sev_guest_status (kvm, &sev_cmd);break ; +case KVM_SEV_DBG_DECRYPT : + r =sev_dbg_decrypt (kvm, &sev_cmd); +break ;default : r = -EINVAL ; goto out;
Signature · 1 line
2.9.5
Borislav Petkov
maintainerRe: [Part2 PATCH v6 32/38] KVM: SVM: Add support for SEV DEBUG_DECRYPT command
Quoted 107 lines · Brijesh Singh
The command is used for decrypting a guest memory region for debug purposes.
Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Joerg Roedel <[email protected]>Cc Borislav Petkov <[email protected]>Cc Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/kvm/svm.c | 179 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+)arch/x86/kvm/svm.c +80
-0@@ -6023,6 +6023,182 @@ static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; } +static int __sev_issue_dbg_cmd (struct kvm *kvm,unsigned long src, +unsigned long dst,int size, +int *error,bool enc) +{ +struct kvm_sev_info *sev = &kvm->arch.sev_info; +struct sev_data_dbg *data; +int ret; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->handle = sev->handle; + data->dst_addr = dst; + data->src_addr = src; + data->len = size; + + ret =sev_issue_cmd (kvm, + enc ?SEV_CMD_DBG_ENCRYPT :SEV_CMD_DBG_DECRYPT , + data, error); +kfree (data); +return ret; +} + +/* +* Decrypt source memory into userspace or kernel buffer. If destination buffer +* or len is not aligned to 16-byte boundary then it uses intermediate buffer. +*/ +static int __sev_dbg_decrypt (struct kvm *kvm,unsigned long paddr, +unsigned long __user dst_uaddr, +unsigned long dst_kaddr,unsigned long dst_paddr, +int size,int *error) +{ +int ret, offset =0 , len = size; +struct page *tpage =NULL ; + +/* +* Debug command works with 16-byte aligned inputs, check if all inputs +* (src, dst and len) are 16-byte aligned. If one of the input is not +* aligned then we decrypt more than requested into a temporary buffer +* and copy the porition of data into destination buffer. +*/ +if (!IS_ALIGNED (paddr,16 ) || + !IS_ALIGNED (dst_paddr,16 ) || + !IS_ALIGNED (size,16 )) { + tpage = (void *)alloc_page (GFP_KERNEL ); +if (!tpage) +return -ENOMEM ; + + dst_paddr =__sme_page_pa (tpage); + +/* +* if source buffer is not aligned then offset will be used +* when copying the data from the temporary buffer into +* destination buffer. +*/ + offset = paddr &15 ; + +/* its safe to read more than requested size. */ + len =round_up (size + offset,16 ); + + paddr =round_down (paddr,16 ); + +/* +* The temporary buffer may have mapping with C=0 or C=1 on x86 +* side but PSP will will write the memory region with C=0. +* Lets make sure x86 cache for this memory range is flushed so +* that we can see the recent contents after the command +* completes. +*/ +clflush_cache_range (page_address (tpage),PAGE_SIZE ); + } + + ret =__sev_issue_dbg_cmd (kvm, paddr, dst_paddr, len, error, false); + +/* +* If temporary buffer is used then copy the data from temporary buffer +* into destination buffer. +*/ +if (!ret && tpage) {
arch/x86/kvm/svm.c static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 6115
Quoted 10 lines
arch/x86/kvm/svm.c +10
-0+/* +* If destination buffer is a userspace buffer then use +* copy_to_user otherwise memcpy. +*/ +if (dst_uaddr) { +if (copy_to_user ((void __user *)(uintptr_t )dst_uaddr, +page_address (tpage) + offset, size)) + ret = -EFAULT ; + }else { +memcpy ((void *)dst_kaddr,page_address (tpage) + offset, size);
arch/x86/kvm/svm.c: In function ‘svm_mem_enc_op’:arch/x86/kvm/svm.c:6115:4: warning: argument 1 null where non-null expected [-Wnonnull]
memcpy((void *)dst_kaddr, page_address(tpage) + offset, size);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from ./arch/x86/include/asm/string.h:4:0,
from ./include/linux/string.h:18,
from ./include/linux/bitmap.h:8,
from ./include/linux/cpumask.h:11,
from ./arch/x86/include/asm/cpumask.h:4,
from ./arch/x86/include/asm/msr.h:10,
from ./arch/x86/include/asm/processor.h:20,
from ./arch/x86/include/asm/cpufeature.h:4,
from ./arch/x86/include/asm/thread_info.h:52,
from ./include/linux/thread_info.h:37,
from ./arch/x86/include/asm/preempt.h:6,
from ./include/linux/preempt.h:80,
from ./include/linux/hardirq.h:4,
from ./include/linux/kvm_host.h:10,
from arch/x86/kvm/svm.c:20:
./arch/x86/include/asm/string_64.h:31:14: note: in a call to function ‘memcpy’ declared here
extern void *memcpy(void *to, const void *from, size_t len);
^~~~~~Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Re: [Part2 PATCH v6 32/38] KVM: SVM: Add support for SEV DEBUG_DECRYPT command
On 10/27/17 3:25 PM, Borislav Petkov wrote:
...
Quoted 17 lines
arch/x86/kvm/svm.c static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 6115
Quoted 3 lines
arch/x86/kvm/svm.c +3
-0+ ret = -EFAULT ; + }else { +memcpy ((void *)dst_kaddr,page_address (tpage) + offset, size);
arch/x86/kvm/svm.c: In function ‘svm_mem_enc_op’:arch/x86/kvm/svm.c:6115:4: warning: argument 1 null where non-null expected [-Wnonnull]memcpy((void *)dst_kaddr, page_address(tpage) + offset, size); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from ./arch/x86/include/asm/string.h:4:0, from ./include/linux/string.h:18, from ./include/linux/bitmap.h:8, from ./include/linux/cpumask.h:11, from ./arch/x86/include/asm/cpumask.h:4, from ./arch/x86/include/asm/msr.h:10, from ./arch/x86/include/asm/processor.h:20, from ./arch/x86/include/asm/cpufeature.h:4, from ./arch/x86/include/asm/thread_info.h:52, from ./include/linux/thread_info.h:37,
Quoted 9 lines
from ./arch/x86/include/asm/preempt.h:6,
from ./include/linux/preempt.h:80,
from ./include/linux/hardirq.h:4,
from ./include/linux/kvm_host.h:10,
from arch/x86/kvm/svm.c:20:
./arch/x86/include/asm/string_64.h:31:14: note: in a call to function ‘memcpy’ declared here
extern void *memcpy(void *to, const void *from, size_t len);
^~~~~~I am not able to reproduce the build warning in my setup, what config option or compiler you are using to trigger this issue ? I am on Fedora with gcc 6.4.x. Logically, the code looks okay. I can try to rearrange code to fix the warning. I have not touched this function since couple of rev, have u seen similar warning in previous versions (v5, v4 etc). thanks
-Brijesh
Borislav Petkov
maintainerRe: [Part2 PATCH v6 32/38] KVM: SVM: Add support for SEV DEBUG_DECRYPT command
Quoted 2 lines · Brijesh Singh
I am not able to reproduce the build warning in my setup, what config option or compiler you are using to trigger this issue ?
gcc7 has gotten smarter.
Quoted 4 lines
I am on Fedora with gcc 6.4.x. Logically, the code looks okay. I can try to rearrange code to fix the warning. I have not touched this function since couple of rev, have u seen similar warning in previous versions (v5, v4 etc). thanks
Lemme see:
sev_dbg_crypt() does
ret = __sev_dbg_decrypt(kvm,
__sme_page_pa(src_p[0]) + s_off,
dst_vaddr, 0,
__sme_page_pa(dst_p[0]) + d_off,
len, &argp->error);and that 4th argument is 0. IINM, that's dst_kaddr and you're doing
memcpy((void *)dst_kaddr, page_address(tpage) + offset, size); ^^^^^^^^^^^^^^^^
BOOM!
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Re: [Part2 PATCH v6 32/38] KVM: SVM: Add support for SEV DEBUG_DECRYPT command
On 10/30/17 10:12 AM, Borislav Petkov wrote:
...
Quoted 10 lines
Lemme see:
sev_dbg_crypt()doesret = __sev_dbg_decrypt(kvm, __sme_page_pa(src_p[0]) + s_off, dst_vaddr, 0, __sme_page_pa(dst_p[0]) + d_off, len, &argp->error);
Quoted 4 lines
and that 4th argument is 0. IINM, that's
dst_kaddrand you're doingmemcpy((void *)dst_kaddr, page_address(tpage) + offset, size); ^^^^^^^^^^^^^^^^
The 3rd argument 'dst_uaddr' should always contain a valid value and 4th argument should not be used. The else statement should not be used during DBG_DECRYPT command.
/*
* If destination buffer is a userspace buffer then use
* copy_to_user otherwise memcpy.
*/
if (dst_uaddr) {
if (copy_to_user((void __user *)(uintptr_t)dst_uaddr,
page_address(tpage) + offset, size))
ret = -EFAULT;
} else {
memcpy((void *)dst_kaddr, page_address(tpage) + offset, size);
}
Here is sequence
sev_dbg_crypt() does:
dst_vaddr = params.dst_uaddr;
.......
for(...) {
dst_p = sev_pin_memory(..., dst_vaddr ...) if (!dst_p) {
return -EFAULT;
}
ret = __sev_dbg_decrypt(kvm,
__sme_page_pa(src_p[0]) + s_off,
dst_vaddr, 0,
__sme_page_pa(dst_p[0]) + d_off,
len, &argp->error);....
The 3rd argument will be zero when we are handling the DBG_ENCRYPT with length not aligned to 16-byte boundary. In that case we allocate a intermediate buffer (dst_kaddr). I will try with gcc7 and look into restructure code to fix the compiler warning.
PATCH v6 33/38 KVM: SVM: Add support for SEV DEBUG_ENCRYPT command
In reply to Brijesh SinghThe command copies a plaintext into guest memory and encrypts it using the VM encryption key. The command will be used for debug purposes (e.g setting breakpoints through gdbserver)
--- arch/x86/kvm/svm.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 114 insertions(+), 5 deletions(-)
arch/x86/kvm/svm.c +114 -5
@@ -6122,6 +6122,99 @@ static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long paddr,return ret; } +static int __sev_dbg_encrypt (struct kvm *kvm,unsigned long __user vaddr, +unsigned long paddr,unsigned long __user dst_vaddr, +unsigned long dst_paddr,int size,int *error) +{ +struct page *src_tpage =NULL ; +struct page *dst_tpage =NULL ; +int ret, len = size; + +/* +* If source buffer is not 16-byte aligned then we copy the data from +* source buffer into a PAGE aligned intermediate (src_tpage) buffer +* and use this intermediate buffer as source buffer. +*/ +if (!IS_ALIGNED (vaddr,16 )) { + src_tpage =alloc_page (GFP_KERNEL ); +if (!src_tpage) +return -ENOMEM ; + +if (copy_from_user (page_address (src_tpage), + (void __user *)(uintptr_t )vaddr, size)) { +__free_page (src_tpage); +return -EFAULT ; + } + paddr =__sme_page_pa (src_tpage); + +clflush_cache_range (page_address (src_tpage),PAGE_SIZE ); + } + +/* +* If destination buffer or length is not 16-byte aligned then: +* - decrypt portion of destination buffer into intermediate buffer +* (dst_tpage) +* - copy the source data into intermediate buffer +* - use the intermediate buffer as source buffer +*/ +if (!IS_ALIGNED (dst_vaddr,16 ) || + !IS_ALIGNED (size,16 )) { +int dst_offset; + + dst_tpage =alloc_page (GFP_KERNEL ); +if (!dst_tpage) { + ret = -ENOMEM ; + goto e_free; + } + +/* decrypt destination buffer into intermediate buffer */ + ret =__sev_dbg_decrypt (kvm, +round_down (dst_paddr,16 ), +0 , + (unsigned long )page_address (dst_tpage), +__sme_page_pa (dst_tpage), +round_up (size,16 ), + error); +if (ret) + goto e_free; + + dst_offset = dst_paddr &15 ; + +/* +* modify the intermediate buffer with data from source buffer. +*/ +if (src_tpage) +memcpy (page_address (dst_tpage) + dst_offset, +page_address (src_tpage), size); +else { +if (copy_from_user (page_address (dst_tpage) + dst_offset, + (void __user *)(uintptr_t )vaddr, size)) { + ret = -EFAULT ; + goto e_free; + } + } + + +/* use intermediate buffer as source */ + paddr =__sme_page_pa (dst_tpage); + +clflush_cache_range (page_address (dst_tpage),PAGE_SIZE ); + +/* now we have length and destination buffer aligned */ + dst_paddr =round_down (dst_paddr,16 ); + len =round_up (size,16 ); + } + + ret =__sev_issue_dbg_cmd (kvm, paddr, dst_paddr, len, error, true); + +e_free: +if (src_tpage) +__free_page (src_tpage); +if (dst_tpage) +__free_page (dst_tpage); +return ret; +} +static int sev_dbg_crypt (struct kvm *kvm,struct kvm_sev_cmd *argp,bool dec) {unsigned long vaddr, vaddr_end, next_vaddr;
@@ -6174,11 +6267,19 @@ static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec) d_off = dst_vaddr & ~PAGE_MASK ; len =min_t (size_t, (PAGE_SIZE - s_off), size); - ret =__sev_dbg_decrypt (kvm, -__sme_page_pa (src_p[0 ]) + s_off, - dst_vaddr,0 , -__sme_page_pa (dst_p[0 ]) + d_off, - len, &argp->error); +if (dec) + ret =__sev_dbg_decrypt (kvm, +__sme_page_pa (src_p[0 ]) + s_off, + dst_vaddr,0 , +__sme_page_pa (dst_p[0 ]) + d_off, + len, &argp->error); +else + ret =__sev_dbg_encrypt (kvm, + vaddr, +__sme_page_pa (src_p[0 ]) + s_off, + dst_vaddr, +__sme_page_pa (dst_p[0 ]) + d_off, + len, &argp->error);sev_unpin_memory (kvm, src_p,1 );sev_unpin_memory (kvm, dst_p,1 );
@@ -6199,6 +6300,11 @@ static int sev_dbg_decrypt(struct kvm *kvm, struct kvm_sev_cmd *argp)return sev_dbg_crypt (kvm, argp, true); } +static int sev_dbg_encrypt (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +return sev_dbg_crypt (kvm, argp, false); +} +static int svm_mem_enc_op (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;
@@ -6234,6 +6340,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)case KVM_SEV_DBG_DECRYPT : r =sev_dbg_decrypt (kvm, &sev_cmd);break ; +case KVM_SEV_DBG_ENCRYPT : + r =sev_dbg_encrypt (kvm, &sev_cmd); +break ;default : r = -EINVAL ; goto out;
Signature · 1 line
2.9.5
PATCH v6 34/38 KVM: SVM: Add support for SEV LAUNCH_SECRET command
In reply to Brijesh SinghThe command is used for injecting a secret into the guest memory region.
--- arch/x86/kvm/svm.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+)
arch/x86/kvm/svm.c +71 -0
@@ -6305,6 +6305,74 @@ static int sev_dbg_encrypt(struct kvm *kvm, struct kvm_sev_cmd *argp)return sev_dbg_crypt (kvm, argp, false); } +static int sev_launch_secret (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_info *sev = &kvm->arch.sev_info; +struct sev_data_launch_secret *data; +struct kvm_sev_launch_secret params; +struct page **pages; +void *blob, *hdr; +unsigned long n; +int ret; + +if (!sev_guest (kvm)) +return -ENOTTY ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params))) +return -EFAULT ; + +/* pin the guest memory region */ + pages =sev_pin_memory (kvm, params.guest_uaddr, params.guest_len, &n,1 ); +if (!pages) +return -ENOMEM ; + +/* +* The secret must be copied into contiguous memory region, lets verify +* that pinned memory pages are contiguous. +*/ +if (get_num_contig_pages (0 , pages, n) != n) { + ret = -EINVAL ; + goto e_unpin_memory; + } + + ret = -ENOMEM ; + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) + goto e_unpin_memory; + +/* copy the secret from userspace into a kernel buffer */ + blob =psp_copy_user_blob (params.trans_uaddr, params.trans_len); +if (IS_ERR (blob)) { + ret =PTR_ERR (blob); + goto e_free; + } + + data->trans_address =__psp_pa (blob); + data->trans_len = params.trans_len; + +/* copy the packet header from userspace into a kernel buffer */ + hdr =psp_copy_user_blob (params.hdr_uaddr, params.hdr_len); +if (IS_ERR (hdr)) { + ret =PTR_ERR (hdr); + goto e_free_blob; + } + data->trans_address =__psp_pa (blob); + data->trans_len = params.trans_len; + + data->handle = sev->handle; + ret =sev_issue_cmd (kvm,SEV_CMD_LAUNCH_UPDATE_SECRET , data, &argp->error); + +kfree (hdr); + +e_free_blob: +kfree (blob); +e_free: +kfree (data); +e_unpin_memory: +sev_unpin_memory (kvm, pages, n); +return ret; +} +static int svm_mem_enc_op (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;
@@ -6343,6 +6411,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)case KVM_SEV_DBG_ENCRYPT : r =sev_dbg_encrypt (kvm, &sev_cmd);break ; +case KVM_SEV_LAUNCH_SECRET : + r =sev_launch_secret (kvm, &sev_cmd); +break ;default : r = -EINVAL ; goto out;
Signature · 1 line
2.9.5
PATCH v6 35/38 KVM: SVM: Pin guest memory when SEV is active
In reply to Brijesh SinghThe SEV memory encryption engine uses a tweak such that two identical plaintext pages at different location will have different ciphertext. So swapping or moving ciphertext of two pages will not result in plaintext being swapped. Relocating (or migrating) physical backing pages for a SEV guest will require some additional steps. The current SEV key management spec does not provide commands to swap or migrate (move) ciphertext pages. For now, we pin the guest memory registered through KVM_MEMORY_ENCRYPT_REGISTER_REGION ioctl.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm.c | 112 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -753,6 +753,7 @@ struct kvm_sev_info {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 */ };struct kvm_arch {
arch/x86/kvm/svm.c +112 -0
149 lines not on this page · raw message
Signature · 1 line
2.9.5
PATCH v6 36/38 KVM: SVM: Clear C-bit from the page fault address
In reply to Brijesh SinghWhen SEV is active, on #NPF the page fault address will contain the C-bit. We must clear the C-bit before handling the fault.
--- arch/x86/kvm/svm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/svm.c +1 -1
@@ -2403,7 +2403,7 @@ static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)static int pf_interception (struct vcpu_svm *svm) { -u64 fault_address = svm->vmcb->control.exit_info_2; +u64 fault_address =__sme_clr (svm->vmcb->control.exit_info_2);u64 error_code = svm->vmcb->control.exit_info_1; return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address,
Signature · 1 line
2.9.5
PATCH v6 37/38 KVM: SVM: Do not install #UD intercept when SEV is enabled
In reply to Brijesh SinghOn #UD, x86_emulate_instruction() fetches the data from guest memory and decodes the instruction bytes to assist further. When SEV is enabled, the instruction bytes will be encrypted using the guest-specific key and the hypervisor will no longer able to fetch the instruction bytes to assist UD handling. By not installing intercept we let the guest receive and handle #UD.
--- arch/x86/kvm/svm.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kvm/svm.c +3 -1
@@ -1421,8 +1421,10 @@ static void init_vmcb(struct vcpu_svm *svm) svm->vmcb->control.int_ctl |=V_GIF_ENABLE_MASK ; } -if (sev_guest (svm->vcpu.kvm)) +if (sev_guest (svm->vcpu.kvm)) { svm->vmcb->control.nested_ctl |=SVM_NESTED_CTL_SEV_ENABLE ; +clr_exception_intercept (svm,UD_VECTOR ); + }mark_all_dirty (svm->vmcb);
Signature · 1 line
2.9.5
PATCH v6 38/38 KVM: X86: Restart the guest when insn_len is zero and SEV is enabled
In reply to Brijesh SinghOn AMD platforms, under certain conditions insn_len may be zero on #NPF. This can happen if a guest gets a page-fault on data access but the HW table walker is not able to read the instruction page (e.g instruction page is not present in memory).
Typically, when insn_len is zero, x86_emulate_instruction() walks the guest page table and fetches the instruction bytes from guest memory. When SEV is enabled, the guest memory is encrypted with guest-specific key hence hypervisor will not able to fetch the instruction bytes. In those cases we simply restart the guest.
I have encountered this issue when running kernbench inside the guest.
--- arch/x86/kvm/mmu.c | 10 ++++++++++ arch/x86/kvm/svm.c | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-)
arch/x86/kvm/mmu.c +10 -0
@@ -4954,6 +4954,16 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u64 error_code,if (mmio_info_in_cache (vcpu, cr2, direct)) emulation_type =0 ; emulate: +/* +* On AMD platforms, under certain conditions insn_len may be zero on #NPF. +* This can happen if a guest gets a page-fault on data access but the HW +* table walker is not able to read the instruction page (e.g instruction +* page is not present in memory). In those cases we simply restart the +* guest. +*/ +if (unlikely (insn && !insn_len)) +return 1 ; + er =x86_emulate_instruction (vcpu, cr2, emulation_type, insn, insn_len);switch (er) {
arch/x86/kvm/svm.c +2 -1
@@ -2409,7 +2409,8 @@ static int pf_interception(struct vcpu_svm *svm)u64 error_code = svm->vmcb->control.exit_info_1;return kvm_handle_page_fault (&svm->vcpu, error_code, fault_address, - svm->vmcb->control.insn_bytes, +static_cpu_has (X86_FEATURE_DECODEASSISTS ) ? + svm->vmcb->control.insn_bytes :NULL , svm->vmcb->control.insn_len, !npt_enabled); }
Signature · 1 line
2.9.5
Hi Herbert and Paolo,
Quoted 12 lines · Brijesh Singh
This part of Secure Encryted Virtualization (SEV) patch series focuses on KVM changes required to create and manage SEV guests.
SEV is an extension to the AMD-V architecture which supports running encrypted virtual machine (VMs) under the control of a hypervisor. Encrypted VMs have their pages (code and data) secured such that only the guest itself has access to unencrypted version. Each encrypted VM is associated with a unique encryption key; if its data is accessed to a different entity using a different key the encrypted guest's data will be incorrectly decrypted, leading to unintelligible data. This security model ensures that hypervisor will no longer able to inspect or alter any guest code or data.
Quoted 4 lines
The key management of this feature is handled by a separate processor known as the AMD Secure Processor (AMD-SP) which is present on AMD SOCs. The SEV Key Management Specification (see below) provides a set of commands which can be used by hypervisor to load virtual machine keys through the AMD-SP driver.
Since the PSP patches touches both the CCP and KVM driver, hence I was wondering if you guys have any thought on how PSP patches will be merged? I am talking about Patch 9 to 20 from this series. I have ensured that patches apply cleanly on both kvm/master and cryptodev-2.6/master. We can do this in one of two ways:
- Paolo can merge the PSP support through the KVM branch
or
- Herbert can create a topic branch with PSP changes and Paolo can use that topic branch.
Any visibility will help my next submission. thank you.
-Brijesh
Hi Herbert,
Quoted 3 lines · Brijesh Singh
Hi Herbert and Paolo,
....
Quoted 7 lines
Since the PSP patches touches both the CCP and KVM driver, hence I was wondering if you guys have any thought on how PSP patches will be merged? I am talking about Patch 9 to 20 from this series. I have ensured that patches apply cleanly on both kvm/master and cryptodev-2.6/master. We can do this in one of two ways:
Quoted 9 lines
- Paolo can merge the PSP support through the KVM branch
or
- Herbert can create a topic branch with PSP changes and Paolo can use that topic branch.
Any visibility will help my next submission. thank you.
Just checking, any thought on this?
Are you okay if the PSP changes are merged through the KVM tree?
-Brijesh