mirror of https://gitee.com/openkylin/linux.git
Merge branch 'sev-v9-p2' of https://github.com/codomania/kvm
This part of Secure Encrypted 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_ENCRYPT_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 white paper: 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 SEV 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 add to EDKII/OVMF BIOS https://github.com/tianocore/edk2 Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
commit
65e38583c3
|
@ -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
|
||||
|
|
|
@ -0,0 +1,247 @@
|
|||
======================================
|
||||
Secure Encrypted Virtualization (SEV)
|
||||
======================================
|
||||
|
||||
Overview
|
||||
========
|
||||
|
||||
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.
|
||||
|
||||
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 [api-spec]_
|
||||
|
||||
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
|
||||
---------------
|
||||
|
||||
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.
|
||||
|
||||
Returns: 0 on success, -negative on error
|
||||
|
||||
2. KVM_SEV_LAUNCH_START
|
||||
-----------------------
|
||||
|
||||
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.
|
||||
|
||||
Parameters: struct kvm_sev_launch_start (in/out)
|
||||
|
||||
Returns: 0 on success, -negative on error
|
||||
|
||||
::
|
||||
|
||||
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
|
||||
-----------------------------
|
||||
|
||||
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.
|
||||
|
||||
Parameters (in): struct kvm_sev_launch_update_data
|
||||
|
||||
Returns: 0 on success, -negative on error
|
||||
|
||||
::
|
||||
|
||||
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
|
||||
-------------------------
|
||||
|
||||
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.
|
||||
|
||||
Parameters (in): struct kvm_sev_launch_measure
|
||||
|
||||
Returns: 0 on success, -negative on error
|
||||
|
||||
::
|
||||
|
||||
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
|
||||
------------------------
|
||||
|
||||
After completion of the launch flow, the KVM_SEV_LAUNCH_FINISH command can be
|
||||
issued to make the guest ready for the execution.
|
||||
|
||||
Returns: 0 on success, -negative on error
|
||||
|
||||
6. KVM_SEV_GUEST_STATUS
|
||||
-----------------------
|
||||
|
||||
The KVM_SEV_GUEST_STATUS command is used to retrieve status information about a
|
||||
SEV-enabled guest.
|
||||
|
||||
Parameters (out): struct kvm_sev_guest_status
|
||||
|
||||
Returns: 0 on success, -negative on error
|
||||
|
||||
::
|
||||
|
||||
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
|
||||
----------------------
|
||||
|
||||
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.
|
||||
|
||||
Parameters (in): struct kvm_sev_dbg
|
||||
|
||||
Returns: 0 on success, -negative on error
|
||||
|
||||
::
|
||||
|
||||
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
|
||||
----------------------
|
||||
|
||||
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.
|
||||
|
||||
Parameters (in): struct kvm_sev_dbg
|
||||
|
||||
Returns: 0 on success, -negative on error
|
||||
|
||||
::
|
||||
|
||||
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
|
||||
------------------------
|
||||
|
||||
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.
|
||||
|
||||
Parameters (in): struct kvm_sev_launch_secret
|
||||
|
||||
Returns: 0 on success, -negative on error
|
||||
|
||||
::
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
References
|
||||
==========
|
||||
|
||||
.. [white-paper] http://amd-dev.wpengine.netdna-cdn.com/wordpress/media/2013/12/AMD_Memory_Encryption_Whitepaper_v7-Public.pdf
|
||||
.. [api-spec] http://support.amd.com/TechDocs/55766_SEV-KM%20API_Specification.pdf
|
||||
.. [amd-apm] http://support.amd.com/TechDocs/24593.pdf (section 15.34)
|
||||
.. [kvm-forum] http://www.linux-kvm.org/images/7/74/02x08A-Thomas_Lendacky-AMDs_Virtualizatoin_Memory_Encryption_Technology.pdf
|
|
@ -3403,6 +3403,56 @@ 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.
|
||||
|
||||
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
|
||||
------------------------
|
||||
|
||||
|
|
|
@ -201,6 +201,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 */
|
||||
|
|
|
@ -761,6 +761,15 @@ 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 */
|
||||
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 {
|
||||
unsigned int n_used_mmu_pages;
|
||||
unsigned int n_requested_mmu_pages;
|
||||
|
@ -848,6 +857,8 @@ struct kvm_arch {
|
|||
|
||||
bool x2apic_format;
|
||||
bool x2apic_broadcast_quirk_disabled;
|
||||
|
||||
struct kvm_sev_info sev_info;
|
||||
};
|
||||
|
||||
struct kvm_vm_stat {
|
||||
|
@ -1081,6 +1092,10 @@ struct kvm_x86_ops {
|
|||
int (*pre_enter_smm)(struct kvm_vcpu *vcpu, char *smstate);
|
||||
int (*pre_leave_smm)(struct kvm_vcpu *vcpu, u64 smbase);
|
||||
int (*enable_smi_window)(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 {
|
||||
|
|
|
@ -382,6 +382,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
|
||||
|
||||
|
|
|
@ -146,6 +146,9 @@ 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)
|
||||
#define SVM_NESTED_CTL_SEV_ENABLE BIT(1)
|
||||
|
||||
struct __attribute__ ((__packed__)) vmcb_seg {
|
||||
u16 selector;
|
||||
u16 attrib;
|
||||
|
|
|
@ -556,6 +556,51 @@ static void bsp_init_amd(struct cpuinfo_x86 *c)
|
|||
}
|
||||
}
|
||||
|
||||
static void early_detect_mem_encrypt(struct cpuinfo_x86 *c)
|
||||
{
|
||||
u64 msr;
|
||||
|
||||
/*
|
||||
* BIOS support is required for SME and SEV.
|
||||
* For SME: If BIOS has enabled SME then adjust x86_phys_bits by
|
||||
* the SME physical address space reduction value.
|
||||
* If BIOS has not enabled SME then don't advertise the
|
||||
* SME feature (set in scattered.c).
|
||||
* For SEV: If BIOS has not enabled SEV then don't advertise the
|
||||
* SEV feature (set in scattered.c).
|
||||
*
|
||||
* In all cases, since support for SME and SEV requires long mode,
|
||||
* don't advertise the feature under CONFIG_X86_32.
|
||||
*/
|
||||
if (cpu_has(c, X86_FEATURE_SME) || cpu_has(c, X86_FEATURE_SEV)) {
|
||||
/* Check if memory encryption is enabled */
|
||||
rdmsrl(MSR_K8_SYSCFG, msr);
|
||||
if (!(msr & MSR_K8_SYSCFG_MEM_ENCRYPT))
|
||||
goto clear_all;
|
||||
|
||||
/*
|
||||
* Always adjust physical address bits. Even though this
|
||||
* will be a value above 32-bits this is still done for
|
||||
* CONFIG_X86_32 so that accurate values are reported.
|
||||
*/
|
||||
c->x86_phys_bits -= (cpuid_ebx(0x8000001f) >> 6) & 0x3f;
|
||||
|
||||
if (IS_ENABLED(CONFIG_X86_32))
|
||||
goto clear_all;
|
||||
|
||||
rdmsrl(MSR_K7_HWCR, msr);
|
||||
if (!(msr & MSR_K7_HWCR_SMMLOCK))
|
||||
goto clear_sev;
|
||||
|
||||
return;
|
||||
|
||||
clear_all:
|
||||
clear_cpu_cap(c, X86_FEATURE_SME);
|
||||
clear_sev:
|
||||
clear_cpu_cap(c, X86_FEATURE_SEV);
|
||||
}
|
||||
}
|
||||
|
||||
static void early_init_amd(struct cpuinfo_x86 *c)
|
||||
{
|
||||
u32 dummy;
|
||||
|
@ -627,26 +672,7 @@ static void early_init_amd(struct cpuinfo_x86 *c)
|
|||
if (cpu_has_amd_erratum(c, amd_erratum_400))
|
||||
set_cpu_bug(c, X86_BUG_AMD_E400);
|
||||
|
||||
/*
|
||||
* BIOS support is required for SME. If BIOS has enabled SME then
|
||||
* adjust x86_phys_bits by the SME physical address space reduction
|
||||
* value. If BIOS has not enabled SME then don't advertise the
|
||||
* feature (set in scattered.c). Also, since the SME support requires
|
||||
* long mode, don't advertise the feature under CONFIG_X86_32.
|
||||
*/
|
||||
if (cpu_has(c, X86_FEATURE_SME)) {
|
||||
u64 msr;
|
||||
|
||||
/* Check if SME is enabled */
|
||||
rdmsrl(MSR_K8_SYSCFG, msr);
|
||||
if (msr & MSR_K8_SYSCFG_MEM_ENCRYPT) {
|
||||
c->x86_phys_bits -= (cpuid_ebx(0x8000001f) >> 6) & 0x3f;
|
||||
if (IS_ENABLED(CONFIG_X86_32))
|
||||
clear_cpu_cap(c, X86_FEATURE_SME);
|
||||
} else {
|
||||
clear_cpu_cap(c, X86_FEATURE_SME);
|
||||
}
|
||||
}
|
||||
early_detect_mem_encrypt(c);
|
||||
}
|
||||
|
||||
static void init_amd_k8(struct cpuinfo_x86 *c)
|
||||
|
|
|
@ -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 }
|
||||
};
|
||||
|
||||
|
|
|
@ -81,6 +81,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
|
||||
|
|
|
@ -613,7 +613,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;
|
||||
|
|
|
@ -4950,6 +4950,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) {
|
||||
|
|
1178
arch/x86/kvm/svm.c
1178
arch/x86/kvm/svm.c
File diff suppressed because it is too large
Load Diff
|
@ -4335,6 +4335,36 @@ 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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
@ -32,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.
|
||||
|
|
|
@ -8,6 +8,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 \
|
||||
|
|
|
@ -0,0 +1,805 @@
|
|||
/*
|
||||
* AMD Platform Security Processor (PSP) interface
|
||||
*
|
||||
* Copyright (C) 2016-2017 Advanced Micro Devices, Inc.
|
||||
*
|
||||
* Author: Brijesh Singh <brijesh.singh@amd.com>
|
||||
*
|
||||
* 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"
|
||||
|
||||
#define DEVICE_NAME "sev"
|
||||
|
||||
static DEFINE_MUTEX(sev_cmd_mutex);
|
||||
static struct sev_misc_dev *misc_dev;
|
||||
static struct psp_device *psp_master;
|
||||
|
||||
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)
|
||||
{
|
||||
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 int __sev_platform_init_locked(int *error)
|
||||
{
|
||||
struct psp_device *psp = psp_master;
|
||||
int rc = 0;
|
||||
|
||||
if (!psp)
|
||||
return -ENODEV;
|
||||
|
||||
if (psp->sev_state == SEV_STATE_INIT)
|
||||
return 0;
|
||||
|
||||
rc = __sev_do_cmd_locked(SEV_CMD_INIT, &psp->init_cmd_buf, error);
|
||||
if (rc)
|
||||
return rc;
|
||||
|
||||
psp->sev_state = SEV_STATE_INIT;
|
||||
dev_dbg(psp->dev, "SEV firmware initialized\n");
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int sev_platform_init(int *error)
|
||||
{
|
||||
int rc;
|
||||
|
||||
mutex_lock(&sev_cmd_mutex);
|
||||
rc = __sev_platform_init_locked(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;
|
||||
}
|
||||
|
||||
static int sev_platform_shutdown(int *error)
|
||||
{
|
||||
int rc;
|
||||
|
||||
mutex_lock(&sev_cmd_mutex);
|
||||
rc = __sev_platform_shutdown_locked(NULL);
|
||||
mutex_unlock(&sev_cmd_mutex);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int sev_get_platform_state(int *state, int *error)
|
||||
{
|
||||
int rc;
|
||||
|
||||
rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS,
|
||||
&psp_master->status_cmd_buf, error);
|
||||
if (rc)
|
||||
return rc;
|
||||
|
||||
*state = psp_master->status_cmd_buf.state;
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int sev_ioctl_do_reset(struct sev_issue_cmd *argp)
|
||||
{
|
||||
int state, rc;
|
||||
|
||||
/*
|
||||
* The SEV spec requires that FACTORY_RESET must be issued in
|
||||
* UNINIT state. Before we go further lets check if any guest is
|
||||
* active.
|
||||
*
|
||||
* If FW is in WORKING state then deny the request otherwise issue
|
||||
* SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET.
|
||||
*
|
||||
*/
|
||||
rc = sev_get_platform_state(&state, &argp->error);
|
||||
if (rc)
|
||||
return rc;
|
||||
|
||||
if (state == SEV_STATE_WORKING)
|
||||
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 int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)
|
||||
{
|
||||
struct sev_user_data_status *data = &psp_master->status_cmd_buf;
|
||||
int ret;
|
||||
|
||||
ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, data, &argp->error);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
if (copy_to_user((void __user *)argp->data, data, sizeof(*data)))
|
||||
ret = -EFAULT;
|
||||
|
||||
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(&argp->error);
|
||||
if (rc)
|
||||
return rc;
|
||||
}
|
||||
|
||||
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(&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;
|
||||
}
|
||||
|
||||
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_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;
|
||||
|
||||
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;
|
||||
|
||||
/* If platform is not in INIT state then transition it to INIT */
|
||||
if (psp_master->sev_state != SEV_STATE_INIT) {
|
||||
ret = __sev_platform_init_locked(&argp->error);
|
||||
if (ret)
|
||||
goto e_free_oca;
|
||||
}
|
||||
|
||||
ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, data, &argp->error);
|
||||
|
||||
e_free_oca:
|
||||
kfree(oca_blob);
|
||||
e_free_pek:
|
||||
kfree(pek_blob);
|
||||
e_free:
|
||||
kfree(data);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int sev_ioctl_do_pdh_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;
|
||||
|
||||
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)
|
||||
goto cmd;
|
||||
|
||||
/* Allocate a physically contiguous buffer to store the PDH blob. */
|
||||
if ((input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE) ||
|
||||
!access_ok(VERIFY_WRITE, input.pdh_cert_address, input.pdh_cert_len)) {
|
||||
ret = -EFAULT;
|
||||
goto e_free;
|
||||
}
|
||||
|
||||
/* Allocate a physically contiguous buffer to store the cert chain blob. */
|
||||
if ((input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE) ||
|
||||
!access_ok(VERIFY_WRITE, input.cert_chain_address, input.cert_chain_len)) {
|
||||
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;
|
||||
|
||||
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:
|
||||
/* If platform is not in INIT state then transition it to INIT. */
|
||||
if (psp_master->sev_state != SEV_STATE_INIT) {
|
||||
ret = __sev_platform_init_locked(&argp->error);
|
||||
if (ret)
|
||||
goto e_free_cert;
|
||||
}
|
||||
|
||||
ret = __sev_do_cmd_locked(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;
|
||||
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;
|
||||
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;
|
||||
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;
|
||||
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;
|
||||
case SEV_PEK_CERT_IMPORT:
|
||||
ret = sev_ioctl_do_pek_import(&input);
|
||||
break;
|
||||
case SEV_PDH_CERT_EXPORT:
|
||||
ret = sev_ioctl_do_pdh_export(&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 = {
|
||||
.owner = THIS_MODULE,
|
||||
.unlocked_ioctl = sev_ioctl,
|
||||
};
|
||||
|
||||
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_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 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);
|
||||
}
|
||||
|
||||
static int sev_misc_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_misc_init(psp);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
ret = sev_init(psp);
|
||||
if (ret)
|
||||
goto e_irq;
|
||||
|
||||
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_irq:
|
||||
sp_free_psp_irq(psp->sp, psp);
|
||||
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;
|
||||
|
||||
if (psp->sev_misc)
|
||||
kref_put(&misc_dev->refcount, sev_exit);
|
||||
|
||||
sp_free_psp_irq(sp, psp);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
void psp_pci_init(void)
|
||||
{
|
||||
struct sev_user_data_status *status;
|
||||
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(&error);
|
||||
if (rc) {
|
||||
dev_err(sp->dev, "SEV: failed to INIT error %#x\n", error);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* Display SEV firmware version */
|
||||
status = &psp_master->status_cmd_buf;
|
||||
rc = sev_platform_status(status, &error);
|
||||
if (rc) {
|
||||
dev_err(sp->dev, "SEV: failed to get status error %#x\n", error);
|
||||
goto err;
|
||||
}
|
||||
|
||||
dev_info(sp->dev, "SEV API:%d.%d build:%d\n", status->api_major,
|
||||
status->api_minor, status->build);
|
||||
return;
|
||||
|
||||
err:
|
||||
psp_master = NULL;
|
||||
}
|
||||
|
||||
void psp_pci_exit(void)
|
||||
{
|
||||
if (!psp_master)
|
||||
return;
|
||||
|
||||
sev_platform_shutdown(NULL);
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
/*
|
||||
* AMD Platform Security Processor (PSP) interface driver
|
||||
*
|
||||
* Copyright (C) 2017 Advanced Micro Devices, Inc.
|
||||
*
|
||||
* Author: Brijesh Singh <brijesh.singh@amd.com>
|
||||
*
|
||||
* 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 <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
|
||||
|
||||
#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 sev_misc_dev {
|
||||
struct kref refcount;
|
||||
struct miscdevice misc;
|
||||
};
|
||||
|
||||
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;
|
||||
|
||||
int sev_state;
|
||||
unsigned int sev_int_rcvd;
|
||||
wait_queue_head_t sev_int_queue;
|
||||
struct sev_misc_dev *sev_misc;
|
||||
struct sev_user_data_status status_cmd_buf;
|
||||
struct sev_data_init init_cmd_buf;
|
||||
};
|
||||
|
||||
#endif /* __PSP_DEV_H */
|
|
@ -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
|
||||
|
@ -246,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
|
||||
|
||||
|
@ -265,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
|
||||
|
||||
|
|
|
@ -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,20 @@ 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_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 */
|
||||
|
||||
#endif
|
||||
|
|
|
@ -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,
|
||||
|
@ -236,6 +285,9 @@ static const struct sp_dev_vdata dev_vdata[] = {
|
|||
.bar = 2,
|
||||
#ifdef CONFIG_CRYPTO_DEV_SP_CCP
|
||||
.ccp_vdata = &ccpv5a,
|
||||
#endif
|
||||
#ifdef CONFIG_CRYPTO_DEV_SP_PSP
|
||||
.psp_vdata = &psp_entry
|
||||
#endif
|
||||
},
|
||||
{
|
||||
|
|
|
@ -0,0 +1,606 @@
|
|||
/*
|
||||
* AMD Secure Encrypted Virtualization (SEV) driver interface
|
||||
*
|
||||
* Copyright (C) 2016-2017 Advanced Micro Devices, Inc.
|
||||
*
|
||||
* Author: Brijesh Singh <brijesh.singh@amd.com>
|
||||
*
|
||||
* 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,
|
||||
};
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
#ifdef CONFIG_CRYPTO_DEV_SP_PSP
|
||||
|
||||
/**
|
||||
* sev_platform_init - perform SEV INIT command
|
||||
*
|
||||
* @error: SEV command return code
|
||||
*
|
||||
* Returns:
|
||||
* 0 if the SEV successfully processed the command
|
||||
* -%ENODEV if the SEV device is not available
|
||||
* -%ENOTSUPP if the SEV does not support SEV
|
||||
* -%ETIMEDOUT if the SEV command timed out
|
||||
* -%EIO if the SEV returned a non-zero return code
|
||||
*/
|
||||
int sev_platform_init(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);
|
||||
|
||||
void *psp_copy_user_blob(u64 __user uaddr, u32 len);
|
||||
|
||||
#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(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; }
|
||||
|
||||
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__ */
|
|
@ -1358,6 +1358,96 @@ 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)
|
||||
|
||||
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)
|
||||
|
||||
/* Secure Encrypted Virtualization command */
|
||||
enum sev_cmd_id {
|
||||
/* Guest initialization commands */
|
||||
KVM_SEV_INIT = 0,
|
||||
KVM_SEV_ES_INIT,
|
||||
/* Guest launch commands */
|
||||
KVM_SEV_LAUNCH_START,
|
||||
KVM_SEV_LAUNCH_UPDATE_DATA,
|
||||
KVM_SEV_LAUNCH_UPDATE_VMSA,
|
||||
KVM_SEV_LAUNCH_SECRET,
|
||||
KVM_SEV_LAUNCH_MEASURE,
|
||||
KVM_SEV_LAUNCH_FINISH,
|
||||
/* Guest migration commands (outgoing) */
|
||||
KVM_SEV_SEND_START,
|
||||
KVM_SEV_SEND_UPDATE_DATA,
|
||||
KVM_SEV_SEND_UPDATE_VMSA,
|
||||
KVM_SEV_SEND_FINISH,
|
||||
/* Guest migration commands (incoming) */
|
||||
KVM_SEV_RECEIVE_START,
|
||||
KVM_SEV_RECEIVE_UPDATE_DATA,
|
||||
KVM_SEV_RECEIVE_UPDATE_VMSA,
|
||||
KVM_SEV_RECEIVE_FINISH,
|
||||
/* Guest status and debug commands */
|
||||
KVM_SEV_GUEST_STATUS,
|
||||
KVM_SEV_DBG_DECRYPT,
|
||||
KVM_SEV_DBG_ENCRYPT,
|
||||
/* Guest certificates commands */
|
||||
KVM_SEV_CERT_EXPORT,
|
||||
|
||||
KVM_SEV_NR_MAX,
|
||||
};
|
||||
|
||||
struct kvm_sev_cmd {
|
||||
__u32 id;
|
||||
__u64 data;
|
||||
__u32 error;
|
||||
__u32 sev_fd;
|
||||
};
|
||||
|
||||
struct kvm_sev_launch_start {
|
||||
__u32 handle;
|
||||
__u32 policy;
|
||||
__u64 dh_uaddr;
|
||||
__u32 dh_len;
|
||||
__u64 session_uaddr;
|
||||
__u32 session_len;
|
||||
};
|
||||
|
||||
struct kvm_sev_launch_update_data {
|
||||
__u64 uaddr;
|
||||
__u32 len;
|
||||
};
|
||||
|
||||
|
||||
struct kvm_sev_launch_secret {
|
||||
__u64 hdr_uaddr;
|
||||
__u32 hdr_len;
|
||||
__u64 guest_uaddr;
|
||||
__u32 guest_len;
|
||||
__u64 trans_uaddr;
|
||||
__u32 trans_len;
|
||||
};
|
||||
|
||||
struct kvm_sev_launch_measure {
|
||||
__u64 uaddr;
|
||||
__u32 len;
|
||||
};
|
||||
|
||||
struct kvm_sev_guest_status {
|
||||
__u32 handle;
|
||||
__u32 policy;
|
||||
__u32 state;
|
||||
};
|
||||
|
||||
struct kvm_sev_dbg {
|
||||
__u64 src_uaddr;
|
||||
__u64 dst_uaddr;
|
||||
__u32 len;
|
||||
};
|
||||
|
||||
#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)
|
||||
#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)
|
||||
|
|
|
@ -0,0 +1,142 @@
|
|||
/*
|
||||
* Userspace interface for AMD Secure Encrypted Virtualization (SEV)
|
||||
* platform management commands.
|
||||
*
|
||||
* Copyright (C) 2016-2017 Advanced Micro Devices, Inc.
|
||||
*
|
||||
* Author: Brijesh Singh <brijesh.singh@amd.com>
|
||||
*
|
||||
* 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,
|
||||
};
|
||||
|
||||
/**
|
||||
* SEV Firmware status code
|
||||
*/
|
||||
typedef enum {
|
||||
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,
|
||||
} sev_ret_code;
|
||||
|
||||
/**
|
||||
* 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 */
|
Loading…
Reference in New Issue