xref: /openbmc/linux/arch/x86/include/asm/sgx.h (revision 370839c2)
18ca52cc3SSean Christopherson /* SPDX-License-Identifier: GPL-2.0 */
28ca52cc3SSean Christopherson /**
38ca52cc3SSean Christopherson  * Copyright(c) 2016-20 Intel Corporation.
48ca52cc3SSean Christopherson  *
58ca52cc3SSean Christopherson  * Intel Software Guard Extensions (SGX) support.
68ca52cc3SSean Christopherson  */
78ca52cc3SSean Christopherson #ifndef _ASM_X86_SGX_H
88ca52cc3SSean Christopherson #define _ASM_X86_SGX_H
98ca52cc3SSean Christopherson 
108ca52cc3SSean Christopherson #include <linux/bits.h>
118ca52cc3SSean Christopherson #include <linux/types.h>
128ca52cc3SSean Christopherson 
138ca52cc3SSean Christopherson /*
148ca52cc3SSean Christopherson  * This file contains both data structures defined by SGX architecture and Linux
158ca52cc3SSean Christopherson  * defined software data structures and functions.  The two should not be mixed
16c4342633SIngo Molnar  * together for better readability.  The architectural definitions come first.
178ca52cc3SSean Christopherson  */
188ca52cc3SSean Christopherson 
198ca52cc3SSean Christopherson /* The SGX specific CPUID function. */
208ca52cc3SSean Christopherson #define SGX_CPUID		0x12
218ca52cc3SSean Christopherson /* EPC enumeration. */
228ca52cc3SSean Christopherson #define SGX_CPUID_EPC		2
238ca52cc3SSean Christopherson /* An invalid EPC section, i.e. the end marker. */
248ca52cc3SSean Christopherson #define SGX_CPUID_EPC_INVALID	0x0
258ca52cc3SSean Christopherson /* A valid EPC section. */
268ca52cc3SSean Christopherson #define SGX_CPUID_EPC_SECTION	0x1
278ca52cc3SSean Christopherson /* The bitmask for the EPC section type. */
288ca52cc3SSean Christopherson #define SGX_CPUID_EPC_MASK	GENMASK(3, 0)
298ca52cc3SSean Christopherson 
309c55c78aSSean Christopherson enum sgx_encls_function {
319c55c78aSSean Christopherson 	ECREATE	= 0x00,
329c55c78aSSean Christopherson 	EADD	= 0x01,
339c55c78aSSean Christopherson 	EINIT	= 0x02,
349c55c78aSSean Christopherson 	EREMOVE	= 0x03,
359c55c78aSSean Christopherson 	EDGBRD	= 0x04,
369c55c78aSSean Christopherson 	EDGBWR	= 0x05,
379c55c78aSSean Christopherson 	EEXTEND	= 0x06,
389c55c78aSSean Christopherson 	ELDU	= 0x08,
399c55c78aSSean Christopherson 	EBLOCK	= 0x09,
409c55c78aSSean Christopherson 	EPA	= 0x0A,
419c55c78aSSean Christopherson 	EWB	= 0x0B,
429c55c78aSSean Christopherson 	ETRACK	= 0x0C,
4332ddda8eSSean Christopherson 	EAUG	= 0x0D,
4432ddda8eSSean Christopherson 	EMODPR	= 0x0E,
4532ddda8eSSean Christopherson 	EMODT	= 0x0F,
469c55c78aSSean Christopherson };
479c55c78aSSean Christopherson 
488ca52cc3SSean Christopherson /**
495ce8e39fSPeter Zijlstra  * SGX_ENCLS_FAULT_FLAG - flag signifying an ENCLS return code is a trapnr
505ce8e39fSPeter Zijlstra  *
515ce8e39fSPeter Zijlstra  * ENCLS has its own (positive value) error codes and also generates
525ce8e39fSPeter Zijlstra  * ENCLS specific #GP and #PF faults.  And the ENCLS values get munged
535ce8e39fSPeter Zijlstra  * with system error codes as everything percolates back up the stack.
545ce8e39fSPeter Zijlstra  * Unfortunately (for us), we need to precisely identify each unique
555ce8e39fSPeter Zijlstra  * error code, e.g. the action taken if EWB fails varies based on the
565ce8e39fSPeter Zijlstra  * type of fault and on the exact SGX error code, i.e. we can't simply
575ce8e39fSPeter Zijlstra  * convert all faults to -EFAULT.
585ce8e39fSPeter Zijlstra  *
595ce8e39fSPeter Zijlstra  * To make all three error types coexist, we set bit 30 to identify an
605ce8e39fSPeter Zijlstra  * ENCLS fault.  Bit 31 (technically bits N:31) is used to differentiate
615ce8e39fSPeter Zijlstra  * between positive (faults and SGX error codes) and negative (system
625ce8e39fSPeter Zijlstra  * error codes) values.
635ce8e39fSPeter Zijlstra  */
645ce8e39fSPeter Zijlstra #define SGX_ENCLS_FAULT_FLAG 0x40000000
655ce8e39fSPeter Zijlstra 
665ce8e39fSPeter Zijlstra /**
678ca52cc3SSean Christopherson  * enum sgx_return_code - The return code type for ENCLS, ENCLU and ENCLV
680fb2126dSReinette Chatre  * %SGX_EPC_PAGE_CONFLICT:	Page is being written by other ENCLS function.
698ca52cc3SSean Christopherson  * %SGX_NOT_TRACKED:		Previous ETRACK's shootdown sequence has not
708ca52cc3SSean Christopherson  *				been completed yet.
718ca52cc3SSean Christopherson  * %SGX_CHILD_PRESENT		SECS has child pages present in the EPC.
728ca52cc3SSean Christopherson  * %SGX_INVALID_EINITTOKEN:	EINITTOKEN is invalid and enclave signer's
738ca52cc3SSean Christopherson  *				public key does not match IA32_SGXLEPUBKEYHASH.
740fb2126dSReinette Chatre  * %SGX_PAGE_NOT_MODIFIABLE:	The EPC page cannot be modified because it
750fb2126dSReinette Chatre  *				is in the PENDING or MODIFIED state.
768ca52cc3SSean Christopherson  * %SGX_UNMASKED_EVENT:		An unmasked event, e.g. INTR, was received
778ca52cc3SSean Christopherson  */
788ca52cc3SSean Christopherson enum sgx_return_code {
790fb2126dSReinette Chatre 	SGX_EPC_PAGE_CONFLICT		= 7,
808ca52cc3SSean Christopherson 	SGX_NOT_TRACKED			= 11,
818ca52cc3SSean Christopherson 	SGX_CHILD_PRESENT		= 13,
828ca52cc3SSean Christopherson 	SGX_INVALID_EINITTOKEN		= 16,
830fb2126dSReinette Chatre 	SGX_PAGE_NOT_MODIFIABLE		= 20,
848ca52cc3SSean Christopherson 	SGX_UNMASKED_EVENT		= 128,
858ca52cc3SSean Christopherson };
868ca52cc3SSean Christopherson 
878ca52cc3SSean Christopherson /* The modulus size for 3072-bit RSA keys. */
888ca52cc3SSean Christopherson #define SGX_MODULUS_SIZE 384
898ca52cc3SSean Christopherson 
908ca52cc3SSean Christopherson /**
918ca52cc3SSean Christopherson  * enum sgx_miscselect - additional information to an SSA frame
928ca52cc3SSean Christopherson  * %SGX_MISC_EXINFO:	Report #PF or #GP to the SSA frame.
938ca52cc3SSean Christopherson  *
948ca52cc3SSean Christopherson  * Save State Area (SSA) is a stack inside the enclave used to store processor
958ca52cc3SSean Christopherson  * state when an exception or interrupt occurs. This enum defines additional
968ca52cc3SSean Christopherson  * information stored to an SSA frame.
978ca52cc3SSean Christopherson  */
988ca52cc3SSean Christopherson enum sgx_miscselect {
998ca52cc3SSean Christopherson 	SGX_MISC_EXINFO		= BIT(0),
1008ca52cc3SSean Christopherson };
1018ca52cc3SSean Christopherson 
1028ca52cc3SSean Christopherson #define SGX_MISC_RESERVED_MASK	GENMASK_ULL(63, 1)
1038ca52cc3SSean Christopherson 
1048ca52cc3SSean Christopherson #define SGX_SSA_GPRS_SIZE		184
1058ca52cc3SSean Christopherson #define SGX_SSA_MISC_EXINFO_SIZE	16
1068ca52cc3SSean Christopherson 
1078ca52cc3SSean Christopherson /**
1088ca52cc3SSean Christopherson  * enum sgx_attributes - the attributes field in &struct sgx_secs
1098ca52cc3SSean Christopherson  * %SGX_ATTR_INIT:		Enclave can be entered (is initialized).
1108ca52cc3SSean Christopherson  * %SGX_ATTR_DEBUG:		Allow ENCLS(EDBGRD) and ENCLS(EDBGWR).
1118ca52cc3SSean Christopherson  * %SGX_ATTR_MODE64BIT:		Tell that this a 64-bit enclave.
1128ca52cc3SSean Christopherson  * %SGX_ATTR_PROVISIONKEY:      Allow to use provisioning keys for remote
1138ca52cc3SSean Christopherson  *				attestation.
1148ca52cc3SSean Christopherson  * %SGX_ATTR_KSS:		Allow to use key separation and sharing (KSS).
1158ca52cc3SSean Christopherson  * %SGX_ATTR_EINITTOKENKEY:	Allow to use token signing key that is used to
1168ca52cc3SSean Christopherson  *				sign cryptographic tokens that can be passed to
1178ca52cc3SSean Christopherson  *				EINIT as an authorization to run an enclave.
118*370839c2SDave Hansen  * %SGX_ATTR_ASYNC_EXIT_NOTIFY:	Allow enclaves to be notified after an
119*370839c2SDave Hansen  *				asynchronous exit has occurred.
1208ca52cc3SSean Christopherson  */
1218ca52cc3SSean Christopherson enum sgx_attribute {
1228ca52cc3SSean Christopherson 	SGX_ATTR_INIT		   = BIT(0),
1238ca52cc3SSean Christopherson 	SGX_ATTR_DEBUG		   = BIT(1),
1248ca52cc3SSean Christopherson 	SGX_ATTR_MODE64BIT	   = BIT(2),
125*370839c2SDave Hansen 				  /* BIT(3) is reserved */
1268ca52cc3SSean Christopherson 	SGX_ATTR_PROVISIONKEY	   = BIT(4),
1278ca52cc3SSean Christopherson 	SGX_ATTR_EINITTOKENKEY	   = BIT(5),
128*370839c2SDave Hansen 				  /* BIT(6) is for CET */
1298ca52cc3SSean Christopherson 	SGX_ATTR_KSS		   = BIT(7),
130*370839c2SDave Hansen 				  /* BIT(8) is reserved */
131*370839c2SDave Hansen 				  /* BIT(9) is reserved */
132*370839c2SDave Hansen 	SGX_ATTR_ASYNC_EXIT_NOTIFY = BIT(10),
1338ca52cc3SSean Christopherson };
1348ca52cc3SSean Christopherson 
135*370839c2SDave Hansen #define SGX_ATTR_RESERVED_MASK	(BIT_ULL(3) | \
136*370839c2SDave Hansen 				 BIT_ULL(6) | \
137*370839c2SDave Hansen 				 BIT_ULL(8) | \
138*370839c2SDave Hansen 				 BIT_ULL(9) | \
139*370839c2SDave Hansen 				 GENMASK_ULL(63, 11))
140*370839c2SDave Hansen 
141*370839c2SDave Hansen #define SGX_ATTR_UNPRIV_MASK	(SGX_ATTR_DEBUG	    | \
142*370839c2SDave Hansen 				 SGX_ATTR_MODE64BIT | \
143*370839c2SDave Hansen 				 SGX_ATTR_KSS	    | \
144*370839c2SDave Hansen 				 SGX_ATTR_ASYNC_EXIT_NOTIFY)
145*370839c2SDave Hansen 
146*370839c2SDave Hansen #define SGX_ATTR_PRIV_MASK	(SGX_ATTR_PROVISIONKEY	| \
147*370839c2SDave Hansen 				 SGX_ATTR_EINITTOKENKEY)
1488ca52cc3SSean Christopherson 
1498ca52cc3SSean Christopherson /**
1508ca52cc3SSean Christopherson  * struct sgx_secs - SGX Enclave Control Structure (SECS)
1518ca52cc3SSean Christopherson  * @size:		size of the address space
1528ca52cc3SSean Christopherson  * @base:		base address of the  address space
1538ca52cc3SSean Christopherson  * @ssa_frame_size:	size of an SSA frame
1548ca52cc3SSean Christopherson  * @miscselect:		additional information stored to an SSA frame
1558ca52cc3SSean Christopherson  * @attributes:		attributes for enclave
1568ca52cc3SSean Christopherson  * @xfrm:		XSave-Feature Request Mask (subset of XCR0)
1578ca52cc3SSean Christopherson  * @mrenclave:		SHA256-hash of the enclave contents
1588ca52cc3SSean Christopherson  * @mrsigner:		SHA256-hash of the public key used to sign the SIGSTRUCT
1598ca52cc3SSean Christopherson  * @config_id:		a user-defined value that is used in key derivation
1608ca52cc3SSean Christopherson  * @isv_prod_id:	a user-defined value that is used in key derivation
1618ca52cc3SSean Christopherson  * @isv_svn:		a user-defined value that is used in key derivation
1628ca52cc3SSean Christopherson  * @config_svn:		a user-defined value that is used in key derivation
1638ca52cc3SSean Christopherson  *
1648ca52cc3SSean Christopherson  * SGX Enclave Control Structure (SECS) is a special enclave page that is not
1658ca52cc3SSean Christopherson  * visible in the address space. In fact, this structure defines the address
1668ca52cc3SSean Christopherson  * range and other global attributes for the enclave and it is the first EPC
1678ca52cc3SSean Christopherson  * page created for any enclave. It is moved from a temporary buffer to an EPC
1688ca52cc3SSean Christopherson  * by the means of ENCLS[ECREATE] function.
1698ca52cc3SSean Christopherson  */
1708ca52cc3SSean Christopherson struct sgx_secs {
1718ca52cc3SSean Christopherson 	u64 size;
1728ca52cc3SSean Christopherson 	u64 base;
1738ca52cc3SSean Christopherson 	u32 ssa_frame_size;
1748ca52cc3SSean Christopherson 	u32 miscselect;
1758ca52cc3SSean Christopherson 	u8  reserved1[24];
1768ca52cc3SSean Christopherson 	u64 attributes;
1778ca52cc3SSean Christopherson 	u64 xfrm;
1788ca52cc3SSean Christopherson 	u32 mrenclave[8];
1798ca52cc3SSean Christopherson 	u8  reserved2[32];
1808ca52cc3SSean Christopherson 	u32 mrsigner[8];
1818ca52cc3SSean Christopherson 	u8  reserved3[32];
1828ca52cc3SSean Christopherson 	u32 config_id[16];
1838ca52cc3SSean Christopherson 	u16 isv_prod_id;
1848ca52cc3SSean Christopherson 	u16 isv_svn;
1858ca52cc3SSean Christopherson 	u16 config_svn;
1868ca52cc3SSean Christopherson 	u8  reserved4[3834];
1878ca52cc3SSean Christopherson } __packed;
1888ca52cc3SSean Christopherson 
1898ca52cc3SSean Christopherson /**
1908ca52cc3SSean Christopherson  * enum sgx_tcs_flags - execution flags for TCS
1918ca52cc3SSean Christopherson  * %SGX_TCS_DBGOPTIN:	If enabled allows single-stepping and breakpoints
1928ca52cc3SSean Christopherson  *			inside an enclave. It is cleared by EADD but can
1938ca52cc3SSean Christopherson  *			be set later with EDBGWR.
1948ca52cc3SSean Christopherson  */
1958ca52cc3SSean Christopherson enum sgx_tcs_flags {
1968ca52cc3SSean Christopherson 	SGX_TCS_DBGOPTIN	= 0x01,
1978ca52cc3SSean Christopherson };
1988ca52cc3SSean Christopherson 
1998ca52cc3SSean Christopherson #define SGX_TCS_RESERVED_MASK	GENMASK_ULL(63, 1)
2008ca52cc3SSean Christopherson #define SGX_TCS_RESERVED_SIZE	4024
2018ca52cc3SSean Christopherson 
2028ca52cc3SSean Christopherson /**
2038ca52cc3SSean Christopherson  * struct sgx_tcs - Thread Control Structure (TCS)
2048ca52cc3SSean Christopherson  * @state:		used to mark an entered TCS
2058ca52cc3SSean Christopherson  * @flags:		execution flags (cleared by EADD)
2068ca52cc3SSean Christopherson  * @ssa_offset:		SSA stack offset relative to the enclave base
2078ca52cc3SSean Christopherson  * @ssa_index:		the current SSA frame index (cleard by EADD)
2088ca52cc3SSean Christopherson  * @nr_ssa_frames:	the number of frame in the SSA stack
2098ca52cc3SSean Christopherson  * @entry_offset:	entry point offset relative to the enclave base
2108ca52cc3SSean Christopherson  * @exit_addr:		address outside the enclave to exit on an exception or
2118ca52cc3SSean Christopherson  *			interrupt
2128ca52cc3SSean Christopherson  * @fs_offset:		offset relative to the enclave base to become FS
2138ca52cc3SSean Christopherson  *			segment inside the enclave
2148ca52cc3SSean Christopherson  * @gs_offset:		offset relative to the enclave base to become GS
2158ca52cc3SSean Christopherson  *			segment inside the enclave
2168ca52cc3SSean Christopherson  * @fs_limit:		size to become a new FS-limit (only 32-bit enclaves)
2178ca52cc3SSean Christopherson  * @gs_limit:		size to become a new GS-limit (only 32-bit enclaves)
2188ca52cc3SSean Christopherson  *
2198ca52cc3SSean Christopherson  * Thread Control Structure (TCS) is an enclave page visible in its address
2208ca52cc3SSean Christopherson  * space that defines an entry point inside the enclave. A thread enters inside
2218ca52cc3SSean Christopherson  * an enclave by supplying address of TCS to ENCLU(EENTER). A TCS can be entered
2228ca52cc3SSean Christopherson  * by only one thread at a time.
2238ca52cc3SSean Christopherson  */
2248ca52cc3SSean Christopherson struct sgx_tcs {
2258ca52cc3SSean Christopherson 	u64 state;
2268ca52cc3SSean Christopherson 	u64 flags;
2278ca52cc3SSean Christopherson 	u64 ssa_offset;
2288ca52cc3SSean Christopherson 	u32 ssa_index;
2298ca52cc3SSean Christopherson 	u32 nr_ssa_frames;
2308ca52cc3SSean Christopherson 	u64 entry_offset;
2318ca52cc3SSean Christopherson 	u64 exit_addr;
2328ca52cc3SSean Christopherson 	u64 fs_offset;
2338ca52cc3SSean Christopherson 	u64 gs_offset;
2348ca52cc3SSean Christopherson 	u32 fs_limit;
2358ca52cc3SSean Christopherson 	u32 gs_limit;
2368ca52cc3SSean Christopherson 	u8  reserved[SGX_TCS_RESERVED_SIZE];
2378ca52cc3SSean Christopherson } __packed;
2388ca52cc3SSean Christopherson 
2398ca52cc3SSean Christopherson /**
2408ca52cc3SSean Christopherson  * struct sgx_pageinfo - an enclave page descriptor
2418ca52cc3SSean Christopherson  * @addr:	address of the enclave page
2428ca52cc3SSean Christopherson  * @contents:	pointer to the page contents
2438ca52cc3SSean Christopherson  * @metadata:	pointer either to a SECINFO or PCMD instance
2448ca52cc3SSean Christopherson  * @secs:	address of the SECS page
2458ca52cc3SSean Christopherson  */
2468ca52cc3SSean Christopherson struct sgx_pageinfo {
2478ca52cc3SSean Christopherson 	u64 addr;
2488ca52cc3SSean Christopherson 	u64 contents;
2498ca52cc3SSean Christopherson 	u64 metadata;
2508ca52cc3SSean Christopherson 	u64 secs;
2518ca52cc3SSean Christopherson } __packed __aligned(32);
2528ca52cc3SSean Christopherson 
2538ca52cc3SSean Christopherson 
2548ca52cc3SSean Christopherson /**
2558ca52cc3SSean Christopherson  * enum sgx_page_type - bits in the SECINFO flags defining the page type
2568ca52cc3SSean Christopherson  * %SGX_PAGE_TYPE_SECS:	a SECS page
2578ca52cc3SSean Christopherson  * %SGX_PAGE_TYPE_TCS:	a TCS page
2588ca52cc3SSean Christopherson  * %SGX_PAGE_TYPE_REG:	a regular page
2598ca52cc3SSean Christopherson  * %SGX_PAGE_TYPE_VA:	a VA page
2608ca52cc3SSean Christopherson  * %SGX_PAGE_TYPE_TRIM:	a page in trimmed state
2618cb7b502SReinette Chatre  *
2628cb7b502SReinette Chatre  * Make sure when making changes to this enum that its values can still fit
2638cb7b502SReinette Chatre  * in the bitfield within &struct sgx_encl_page
2648ca52cc3SSean Christopherson  */
2658ca52cc3SSean Christopherson enum sgx_page_type {
2668ca52cc3SSean Christopherson 	SGX_PAGE_TYPE_SECS,
2678ca52cc3SSean Christopherson 	SGX_PAGE_TYPE_TCS,
2688ca52cc3SSean Christopherson 	SGX_PAGE_TYPE_REG,
2698ca52cc3SSean Christopherson 	SGX_PAGE_TYPE_VA,
2708ca52cc3SSean Christopherson 	SGX_PAGE_TYPE_TRIM,
2718ca52cc3SSean Christopherson };
2728ca52cc3SSean Christopherson 
2738ca52cc3SSean Christopherson #define SGX_NR_PAGE_TYPES	5
2748ca52cc3SSean Christopherson #define SGX_PAGE_TYPE_MASK	GENMASK(7, 0)
2758ca52cc3SSean Christopherson 
2768ca52cc3SSean Christopherson /**
2778ca52cc3SSean Christopherson  * enum sgx_secinfo_flags - the flags field in &struct sgx_secinfo
2788ca52cc3SSean Christopherson  * %SGX_SECINFO_R:	allow read
2798ca52cc3SSean Christopherson  * %SGX_SECINFO_W:	allow write
2808ca52cc3SSean Christopherson  * %SGX_SECINFO_X:	allow execution
2818ca52cc3SSean Christopherson  * %SGX_SECINFO_SECS:	a SECS page
2828ca52cc3SSean Christopherson  * %SGX_SECINFO_TCS:	a TCS page
2838ca52cc3SSean Christopherson  * %SGX_SECINFO_REG:	a regular page
2848ca52cc3SSean Christopherson  * %SGX_SECINFO_VA:	a VA page
2858ca52cc3SSean Christopherson  * %SGX_SECINFO_TRIM:	a page in trimmed state
2868ca52cc3SSean Christopherson  */
2878ca52cc3SSean Christopherson enum sgx_secinfo_flags {
2888ca52cc3SSean Christopherson 	SGX_SECINFO_R			= BIT(0),
2898ca52cc3SSean Christopherson 	SGX_SECINFO_W			= BIT(1),
2908ca52cc3SSean Christopherson 	SGX_SECINFO_X			= BIT(2),
2918ca52cc3SSean Christopherson 	SGX_SECINFO_SECS		= (SGX_PAGE_TYPE_SECS << 8),
2928ca52cc3SSean Christopherson 	SGX_SECINFO_TCS			= (SGX_PAGE_TYPE_TCS << 8),
2938ca52cc3SSean Christopherson 	SGX_SECINFO_REG			= (SGX_PAGE_TYPE_REG << 8),
2948ca52cc3SSean Christopherson 	SGX_SECINFO_VA			= (SGX_PAGE_TYPE_VA << 8),
2958ca52cc3SSean Christopherson 	SGX_SECINFO_TRIM		= (SGX_PAGE_TYPE_TRIM << 8),
2968ca52cc3SSean Christopherson };
2978ca52cc3SSean Christopherson 
2988ca52cc3SSean Christopherson #define SGX_SECINFO_PERMISSION_MASK	GENMASK_ULL(2, 0)
2998ca52cc3SSean Christopherson #define SGX_SECINFO_PAGE_TYPE_MASK	(SGX_PAGE_TYPE_MASK << 8)
3008ca52cc3SSean Christopherson #define SGX_SECINFO_RESERVED_MASK	~(SGX_SECINFO_PERMISSION_MASK | \
3018ca52cc3SSean Christopherson 					  SGX_SECINFO_PAGE_TYPE_MASK)
3028ca52cc3SSean Christopherson 
3038ca52cc3SSean Christopherson /**
3048ca52cc3SSean Christopherson  * struct sgx_secinfo - describes attributes of an EPC page
3058ca52cc3SSean Christopherson  * @flags:	permissions and type
3068ca52cc3SSean Christopherson  *
3078ca52cc3SSean Christopherson  * Used together with ENCLS leaves that add or modify an EPC page to an
3088ca52cc3SSean Christopherson  * enclave to define page permissions and type.
3098ca52cc3SSean Christopherson  */
3108ca52cc3SSean Christopherson struct sgx_secinfo {
3118ca52cc3SSean Christopherson 	u64 flags;
3128ca52cc3SSean Christopherson 	u8  reserved[56];
3138ca52cc3SSean Christopherson } __packed __aligned(64);
3148ca52cc3SSean Christopherson 
3158ca52cc3SSean Christopherson #define SGX_PCMD_RESERVED_SIZE 40
3168ca52cc3SSean Christopherson 
3178ca52cc3SSean Christopherson /**
3188ca52cc3SSean Christopherson  * struct sgx_pcmd - Paging Crypto Metadata (PCMD)
3198ca52cc3SSean Christopherson  * @enclave_id:	enclave identifier
3208ca52cc3SSean Christopherson  * @mac:	MAC over PCMD, page contents and isvsvn
3218ca52cc3SSean Christopherson  *
3228ca52cc3SSean Christopherson  * PCMD is stored for every swapped page to the regular memory. When ELDU loads
3238ca52cc3SSean Christopherson  * the page back it recalculates the MAC by using a isvsvn number stored in a
3248ca52cc3SSean Christopherson  * VA page. Together these two structures bring integrity and rollback
3258ca52cc3SSean Christopherson  * protection.
3268ca52cc3SSean Christopherson  */
3278ca52cc3SSean Christopherson struct sgx_pcmd {
3288ca52cc3SSean Christopherson 	struct sgx_secinfo secinfo;
3298ca52cc3SSean Christopherson 	u64 enclave_id;
3308ca52cc3SSean Christopherson 	u8  reserved[SGX_PCMD_RESERVED_SIZE];
3318ca52cc3SSean Christopherson 	u8  mac[16];
3328ca52cc3SSean Christopherson } __packed __aligned(128);
3338ca52cc3SSean Christopherson 
3348ca52cc3SSean Christopherson #define SGX_SIGSTRUCT_RESERVED1_SIZE 84
3358ca52cc3SSean Christopherson #define SGX_SIGSTRUCT_RESERVED2_SIZE 20
3368ca52cc3SSean Christopherson #define SGX_SIGSTRUCT_RESERVED3_SIZE 32
3378ca52cc3SSean Christopherson #define SGX_SIGSTRUCT_RESERVED4_SIZE 12
3388ca52cc3SSean Christopherson 
3398ca52cc3SSean Christopherson /**
3408ca52cc3SSean Christopherson  * struct sgx_sigstruct_header -  defines author of the enclave
3418ca52cc3SSean Christopherson  * @header1:		constant byte string
3428ca52cc3SSean Christopherson  * @vendor:		must be either 0x0000 or 0x8086
3438ca52cc3SSean Christopherson  * @date:		YYYYMMDD in BCD
344ea5bc7b9SLinus Torvalds  * @header2:		constant byte string
3458ca52cc3SSean Christopherson  * @swdefined:		software defined value
3468ca52cc3SSean Christopherson  */
3478ca52cc3SSean Christopherson struct sgx_sigstruct_header {
3488ca52cc3SSean Christopherson 	u64 header1[2];
3498ca52cc3SSean Christopherson 	u32 vendor;
3508ca52cc3SSean Christopherson 	u32 date;
3518ca52cc3SSean Christopherson 	u64 header2[2];
3528ca52cc3SSean Christopherson 	u32 swdefined;
3538ca52cc3SSean Christopherson 	u8  reserved1[84];
3548ca52cc3SSean Christopherson } __packed;
3558ca52cc3SSean Christopherson 
3568ca52cc3SSean Christopherson /**
3578ca52cc3SSean Christopherson  * struct sgx_sigstruct_body - defines contents of the enclave
3588ca52cc3SSean Christopherson  * @miscselect:		additional information stored to an SSA frame
3598ca52cc3SSean Christopherson  * @misc_mask:		required miscselect in SECS
3608ca52cc3SSean Christopherson  * @attributes:		attributes for enclave
3618ca52cc3SSean Christopherson  * @xfrm:		XSave-Feature Request Mask (subset of XCR0)
3628ca52cc3SSean Christopherson  * @attributes_mask:	required attributes in SECS
3638ca52cc3SSean Christopherson  * @xfrm_mask:		required XFRM in SECS
3648ca52cc3SSean Christopherson  * @mrenclave:		SHA256-hash of the enclave contents
3658ca52cc3SSean Christopherson  * @isvprodid:		a user-defined value that is used in key derivation
3668ca52cc3SSean Christopherson  * @isvsvn:		a user-defined value that is used in key derivation
3678ca52cc3SSean Christopherson  */
3688ca52cc3SSean Christopherson struct sgx_sigstruct_body {
3698ca52cc3SSean Christopherson 	u32 miscselect;
3708ca52cc3SSean Christopherson 	u32 misc_mask;
3718ca52cc3SSean Christopherson 	u8  reserved2[20];
3728ca52cc3SSean Christopherson 	u64 attributes;
3738ca52cc3SSean Christopherson 	u64 xfrm;
3748ca52cc3SSean Christopherson 	u64 attributes_mask;
3758ca52cc3SSean Christopherson 	u64 xfrm_mask;
3768ca52cc3SSean Christopherson 	u8  mrenclave[32];
3778ca52cc3SSean Christopherson 	u8  reserved3[32];
3788ca52cc3SSean Christopherson 	u16 isvprodid;
3798ca52cc3SSean Christopherson 	u16 isvsvn;
3808ca52cc3SSean Christopherson } __packed;
3818ca52cc3SSean Christopherson 
3828ca52cc3SSean Christopherson /**
3838ca52cc3SSean Christopherson  * struct sgx_sigstruct - an enclave signature
3848ca52cc3SSean Christopherson  * @header:		defines author of the enclave
3858ca52cc3SSean Christopherson  * @modulus:		the modulus of the public key
3868ca52cc3SSean Christopherson  * @exponent:		the exponent of the public key
3878ca52cc3SSean Christopherson  * @signature:		the signature calculated over the fields except modulus,
3888ca52cc3SSean Christopherson  * @body:		defines contents of the enclave
3898ca52cc3SSean Christopherson  * @q1:			a value used in RSA signature verification
3908ca52cc3SSean Christopherson  * @q2:			a value used in RSA signature verification
3918ca52cc3SSean Christopherson  *
3928ca52cc3SSean Christopherson  * Header and body are the parts that are actual signed. The remaining fields
3938ca52cc3SSean Christopherson  * define the signature of the enclave.
3948ca52cc3SSean Christopherson  */
3958ca52cc3SSean Christopherson struct sgx_sigstruct {
3968ca52cc3SSean Christopherson 	struct sgx_sigstruct_header header;
3978ca52cc3SSean Christopherson 	u8  modulus[SGX_MODULUS_SIZE];
3988ca52cc3SSean Christopherson 	u32 exponent;
3998ca52cc3SSean Christopherson 	u8  signature[SGX_MODULUS_SIZE];
4008ca52cc3SSean Christopherson 	struct sgx_sigstruct_body body;
4018ca52cc3SSean Christopherson 	u8  reserved4[12];
4028ca52cc3SSean Christopherson 	u8  q1[SGX_MODULUS_SIZE];
4038ca52cc3SSean Christopherson 	u8  q2[SGX_MODULUS_SIZE];
4048ca52cc3SSean Christopherson } __packed;
4058ca52cc3SSean Christopherson 
4068ca52cc3SSean Christopherson #define SGX_LAUNCH_TOKEN_SIZE 304
4078ca52cc3SSean Christopherson 
4088ca52cc3SSean Christopherson /*
4098ca52cc3SSean Christopherson  * Do not put any hardware-defined SGX structure representations below this
4108ca52cc3SSean Christopherson  * comment!
4118ca52cc3SSean Christopherson  */
4128ca52cc3SSean Christopherson 
413d155030bSSean Christopherson #ifdef CONFIG_X86_SGX_KVM
414d155030bSSean Christopherson int sgx_virt_ecreate(struct sgx_pageinfo *pageinfo, void __user *secs,
415d155030bSSean Christopherson 		     int *trapnr);
416d155030bSSean Christopherson int sgx_virt_einit(void __user *sigstruct, void __user *token,
417d155030bSSean Christopherson 		   void __user *secs, u64 *lepubkeyhash, int *trapnr);
418d155030bSSean Christopherson #endif
419d155030bSSean Christopherson 
420b3754e5dSSean Christopherson int sgx_set_attribute(unsigned long *allowed_attributes,
421b3754e5dSSean Christopherson 		      unsigned int attribute_fd);
422b3754e5dSSean Christopherson 
4238ca52cc3SSean Christopherson #endif /* _ASM_X86_SGX_H */
424