162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/**
362306a36Sopenharmony_ci * Copyright(c) 2016-20 Intel Corporation.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Intel Software Guard Extensions (SGX) support.
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci#ifndef _ASM_X86_SGX_H
862306a36Sopenharmony_ci#define _ASM_X86_SGX_H
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/bits.h>
1162306a36Sopenharmony_ci#include <linux/types.h>
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci/*
1462306a36Sopenharmony_ci * This file contains both data structures defined by SGX architecture and Linux
1562306a36Sopenharmony_ci * defined software data structures and functions.  The two should not be mixed
1662306a36Sopenharmony_ci * together for better readability.  The architectural definitions come first.
1762306a36Sopenharmony_ci */
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci/* The SGX specific CPUID function. */
2062306a36Sopenharmony_ci#define SGX_CPUID		0x12
2162306a36Sopenharmony_ci/* EPC enumeration. */
2262306a36Sopenharmony_ci#define SGX_CPUID_EPC		2
2362306a36Sopenharmony_ci/* An invalid EPC section, i.e. the end marker. */
2462306a36Sopenharmony_ci#define SGX_CPUID_EPC_INVALID	0x0
2562306a36Sopenharmony_ci/* A valid EPC section. */
2662306a36Sopenharmony_ci#define SGX_CPUID_EPC_SECTION	0x1
2762306a36Sopenharmony_ci/* The bitmask for the EPC section type. */
2862306a36Sopenharmony_ci#define SGX_CPUID_EPC_MASK	GENMASK(3, 0)
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_cienum sgx_encls_function {
3162306a36Sopenharmony_ci	ECREATE	= 0x00,
3262306a36Sopenharmony_ci	EADD	= 0x01,
3362306a36Sopenharmony_ci	EINIT	= 0x02,
3462306a36Sopenharmony_ci	EREMOVE	= 0x03,
3562306a36Sopenharmony_ci	EDGBRD	= 0x04,
3662306a36Sopenharmony_ci	EDGBWR	= 0x05,
3762306a36Sopenharmony_ci	EEXTEND	= 0x06,
3862306a36Sopenharmony_ci	ELDU	= 0x08,
3962306a36Sopenharmony_ci	EBLOCK	= 0x09,
4062306a36Sopenharmony_ci	EPA	= 0x0A,
4162306a36Sopenharmony_ci	EWB	= 0x0B,
4262306a36Sopenharmony_ci	ETRACK	= 0x0C,
4362306a36Sopenharmony_ci	EAUG	= 0x0D,
4462306a36Sopenharmony_ci	EMODPR	= 0x0E,
4562306a36Sopenharmony_ci	EMODT	= 0x0F,
4662306a36Sopenharmony_ci};
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci/**
4962306a36Sopenharmony_ci * SGX_ENCLS_FAULT_FLAG - flag signifying an ENCLS return code is a trapnr
5062306a36Sopenharmony_ci *
5162306a36Sopenharmony_ci * ENCLS has its own (positive value) error codes and also generates
5262306a36Sopenharmony_ci * ENCLS specific #GP and #PF faults.  And the ENCLS values get munged
5362306a36Sopenharmony_ci * with system error codes as everything percolates back up the stack.
5462306a36Sopenharmony_ci * Unfortunately (for us), we need to precisely identify each unique
5562306a36Sopenharmony_ci * error code, e.g. the action taken if EWB fails varies based on the
5662306a36Sopenharmony_ci * type of fault and on the exact SGX error code, i.e. we can't simply
5762306a36Sopenharmony_ci * convert all faults to -EFAULT.
5862306a36Sopenharmony_ci *
5962306a36Sopenharmony_ci * To make all three error types coexist, we set bit 30 to identify an
6062306a36Sopenharmony_ci * ENCLS fault.  Bit 31 (technically bits N:31) is used to differentiate
6162306a36Sopenharmony_ci * between positive (faults and SGX error codes) and negative (system
6262306a36Sopenharmony_ci * error codes) values.
6362306a36Sopenharmony_ci */
6462306a36Sopenharmony_ci#define SGX_ENCLS_FAULT_FLAG 0x40000000
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci/**
6762306a36Sopenharmony_ci * enum sgx_return_code - The return code type for ENCLS, ENCLU and ENCLV
6862306a36Sopenharmony_ci * %SGX_EPC_PAGE_CONFLICT:	Page is being written by other ENCLS function.
6962306a36Sopenharmony_ci * %SGX_NOT_TRACKED:		Previous ETRACK's shootdown sequence has not
7062306a36Sopenharmony_ci *				been completed yet.
7162306a36Sopenharmony_ci * %SGX_CHILD_PRESENT		SECS has child pages present in the EPC.
7262306a36Sopenharmony_ci * %SGX_INVALID_EINITTOKEN:	EINITTOKEN is invalid and enclave signer's
7362306a36Sopenharmony_ci *				public key does not match IA32_SGXLEPUBKEYHASH.
7462306a36Sopenharmony_ci * %SGX_PAGE_NOT_MODIFIABLE:	The EPC page cannot be modified because it
7562306a36Sopenharmony_ci *				is in the PENDING or MODIFIED state.
7662306a36Sopenharmony_ci * %SGX_UNMASKED_EVENT:		An unmasked event, e.g. INTR, was received
7762306a36Sopenharmony_ci */
7862306a36Sopenharmony_cienum sgx_return_code {
7962306a36Sopenharmony_ci	SGX_EPC_PAGE_CONFLICT		= 7,
8062306a36Sopenharmony_ci	SGX_NOT_TRACKED			= 11,
8162306a36Sopenharmony_ci	SGX_CHILD_PRESENT		= 13,
8262306a36Sopenharmony_ci	SGX_INVALID_EINITTOKEN		= 16,
8362306a36Sopenharmony_ci	SGX_PAGE_NOT_MODIFIABLE		= 20,
8462306a36Sopenharmony_ci	SGX_UNMASKED_EVENT		= 128,
8562306a36Sopenharmony_ci};
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci/* The modulus size for 3072-bit RSA keys. */
8862306a36Sopenharmony_ci#define SGX_MODULUS_SIZE 384
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci/**
9162306a36Sopenharmony_ci * enum sgx_miscselect - additional information to an SSA frame
9262306a36Sopenharmony_ci * %SGX_MISC_EXINFO:	Report #PF or #GP to the SSA frame.
9362306a36Sopenharmony_ci *
9462306a36Sopenharmony_ci * Save State Area (SSA) is a stack inside the enclave used to store processor
9562306a36Sopenharmony_ci * state when an exception or interrupt occurs. This enum defines additional
9662306a36Sopenharmony_ci * information stored to an SSA frame.
9762306a36Sopenharmony_ci */
9862306a36Sopenharmony_cienum sgx_miscselect {
9962306a36Sopenharmony_ci	SGX_MISC_EXINFO		= BIT(0),
10062306a36Sopenharmony_ci};
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci#define SGX_MISC_RESERVED_MASK	GENMASK_ULL(63, 1)
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci#define SGX_SSA_GPRS_SIZE		184
10562306a36Sopenharmony_ci#define SGX_SSA_MISC_EXINFO_SIZE	16
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci/**
10862306a36Sopenharmony_ci * enum sgx_attributes - the attributes field in &struct sgx_secs
10962306a36Sopenharmony_ci * %SGX_ATTR_INIT:		Enclave can be entered (is initialized).
11062306a36Sopenharmony_ci * %SGX_ATTR_DEBUG:		Allow ENCLS(EDBGRD) and ENCLS(EDBGWR).
11162306a36Sopenharmony_ci * %SGX_ATTR_MODE64BIT:		Tell that this a 64-bit enclave.
11262306a36Sopenharmony_ci * %SGX_ATTR_PROVISIONKEY:      Allow to use provisioning keys for remote
11362306a36Sopenharmony_ci *				attestation.
11462306a36Sopenharmony_ci * %SGX_ATTR_KSS:		Allow to use key separation and sharing (KSS).
11562306a36Sopenharmony_ci * %SGX_ATTR_EINITTOKENKEY:	Allow to use token signing key that is used to
11662306a36Sopenharmony_ci *				sign cryptographic tokens that can be passed to
11762306a36Sopenharmony_ci *				EINIT as an authorization to run an enclave.
11862306a36Sopenharmony_ci * %SGX_ATTR_ASYNC_EXIT_NOTIFY:	Allow enclaves to be notified after an
11962306a36Sopenharmony_ci *				asynchronous exit has occurred.
12062306a36Sopenharmony_ci */
12162306a36Sopenharmony_cienum sgx_attribute {
12262306a36Sopenharmony_ci	SGX_ATTR_INIT		   = BIT(0),
12362306a36Sopenharmony_ci	SGX_ATTR_DEBUG		   = BIT(1),
12462306a36Sopenharmony_ci	SGX_ATTR_MODE64BIT	   = BIT(2),
12562306a36Sopenharmony_ci				  /* BIT(3) is reserved */
12662306a36Sopenharmony_ci	SGX_ATTR_PROVISIONKEY	   = BIT(4),
12762306a36Sopenharmony_ci	SGX_ATTR_EINITTOKENKEY	   = BIT(5),
12862306a36Sopenharmony_ci				  /* BIT(6) is for CET */
12962306a36Sopenharmony_ci	SGX_ATTR_KSS		   = BIT(7),
13062306a36Sopenharmony_ci				  /* BIT(8) is reserved */
13162306a36Sopenharmony_ci				  /* BIT(9) is reserved */
13262306a36Sopenharmony_ci	SGX_ATTR_ASYNC_EXIT_NOTIFY = BIT(10),
13362306a36Sopenharmony_ci};
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci#define SGX_ATTR_RESERVED_MASK	(BIT_ULL(3) | \
13662306a36Sopenharmony_ci				 BIT_ULL(6) | \
13762306a36Sopenharmony_ci				 BIT_ULL(8) | \
13862306a36Sopenharmony_ci				 BIT_ULL(9) | \
13962306a36Sopenharmony_ci				 GENMASK_ULL(63, 11))
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci#define SGX_ATTR_UNPRIV_MASK	(SGX_ATTR_DEBUG	    | \
14262306a36Sopenharmony_ci				 SGX_ATTR_MODE64BIT | \
14362306a36Sopenharmony_ci				 SGX_ATTR_KSS	    | \
14462306a36Sopenharmony_ci				 SGX_ATTR_ASYNC_EXIT_NOTIFY)
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci#define SGX_ATTR_PRIV_MASK	(SGX_ATTR_PROVISIONKEY	| \
14762306a36Sopenharmony_ci				 SGX_ATTR_EINITTOKENKEY)
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci/**
15062306a36Sopenharmony_ci * struct sgx_secs - SGX Enclave Control Structure (SECS)
15162306a36Sopenharmony_ci * @size:		size of the address space
15262306a36Sopenharmony_ci * @base:		base address of the  address space
15362306a36Sopenharmony_ci * @ssa_frame_size:	size of an SSA frame
15462306a36Sopenharmony_ci * @miscselect:		additional information stored to an SSA frame
15562306a36Sopenharmony_ci * @attributes:		attributes for enclave
15662306a36Sopenharmony_ci * @xfrm:		XSave-Feature Request Mask (subset of XCR0)
15762306a36Sopenharmony_ci * @mrenclave:		SHA256-hash of the enclave contents
15862306a36Sopenharmony_ci * @mrsigner:		SHA256-hash of the public key used to sign the SIGSTRUCT
15962306a36Sopenharmony_ci * @config_id:		a user-defined value that is used in key derivation
16062306a36Sopenharmony_ci * @isv_prod_id:	a user-defined value that is used in key derivation
16162306a36Sopenharmony_ci * @isv_svn:		a user-defined value that is used in key derivation
16262306a36Sopenharmony_ci * @config_svn:		a user-defined value that is used in key derivation
16362306a36Sopenharmony_ci *
16462306a36Sopenharmony_ci * SGX Enclave Control Structure (SECS) is a special enclave page that is not
16562306a36Sopenharmony_ci * visible in the address space. In fact, this structure defines the address
16662306a36Sopenharmony_ci * range and other global attributes for the enclave and it is the first EPC
16762306a36Sopenharmony_ci * page created for any enclave. It is moved from a temporary buffer to an EPC
16862306a36Sopenharmony_ci * by the means of ENCLS[ECREATE] function.
16962306a36Sopenharmony_ci */
17062306a36Sopenharmony_cistruct sgx_secs {
17162306a36Sopenharmony_ci	u64 size;
17262306a36Sopenharmony_ci	u64 base;
17362306a36Sopenharmony_ci	u32 ssa_frame_size;
17462306a36Sopenharmony_ci	u32 miscselect;
17562306a36Sopenharmony_ci	u8  reserved1[24];
17662306a36Sopenharmony_ci	u64 attributes;
17762306a36Sopenharmony_ci	u64 xfrm;
17862306a36Sopenharmony_ci	u32 mrenclave[8];
17962306a36Sopenharmony_ci	u8  reserved2[32];
18062306a36Sopenharmony_ci	u32 mrsigner[8];
18162306a36Sopenharmony_ci	u8  reserved3[32];
18262306a36Sopenharmony_ci	u32 config_id[16];
18362306a36Sopenharmony_ci	u16 isv_prod_id;
18462306a36Sopenharmony_ci	u16 isv_svn;
18562306a36Sopenharmony_ci	u16 config_svn;
18662306a36Sopenharmony_ci	u8  reserved4[3834];
18762306a36Sopenharmony_ci} __packed;
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci/**
19062306a36Sopenharmony_ci * enum sgx_tcs_flags - execution flags for TCS
19162306a36Sopenharmony_ci * %SGX_TCS_DBGOPTIN:	If enabled allows single-stepping and breakpoints
19262306a36Sopenharmony_ci *			inside an enclave. It is cleared by EADD but can
19362306a36Sopenharmony_ci *			be set later with EDBGWR.
19462306a36Sopenharmony_ci */
19562306a36Sopenharmony_cienum sgx_tcs_flags {
19662306a36Sopenharmony_ci	SGX_TCS_DBGOPTIN	= 0x01,
19762306a36Sopenharmony_ci};
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci#define SGX_TCS_RESERVED_MASK	GENMASK_ULL(63, 1)
20062306a36Sopenharmony_ci#define SGX_TCS_RESERVED_SIZE	4024
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci/**
20362306a36Sopenharmony_ci * struct sgx_tcs - Thread Control Structure (TCS)
20462306a36Sopenharmony_ci * @state:		used to mark an entered TCS
20562306a36Sopenharmony_ci * @flags:		execution flags (cleared by EADD)
20662306a36Sopenharmony_ci * @ssa_offset:		SSA stack offset relative to the enclave base
20762306a36Sopenharmony_ci * @ssa_index:		the current SSA frame index (cleard by EADD)
20862306a36Sopenharmony_ci * @nr_ssa_frames:	the number of frame in the SSA stack
20962306a36Sopenharmony_ci * @entry_offset:	entry point offset relative to the enclave base
21062306a36Sopenharmony_ci * @exit_addr:		address outside the enclave to exit on an exception or
21162306a36Sopenharmony_ci *			interrupt
21262306a36Sopenharmony_ci * @fs_offset:		offset relative to the enclave base to become FS
21362306a36Sopenharmony_ci *			segment inside the enclave
21462306a36Sopenharmony_ci * @gs_offset:		offset relative to the enclave base to become GS
21562306a36Sopenharmony_ci *			segment inside the enclave
21662306a36Sopenharmony_ci * @fs_limit:		size to become a new FS-limit (only 32-bit enclaves)
21762306a36Sopenharmony_ci * @gs_limit:		size to become a new GS-limit (only 32-bit enclaves)
21862306a36Sopenharmony_ci *
21962306a36Sopenharmony_ci * Thread Control Structure (TCS) is an enclave page visible in its address
22062306a36Sopenharmony_ci * space that defines an entry point inside the enclave. A thread enters inside
22162306a36Sopenharmony_ci * an enclave by supplying address of TCS to ENCLU(EENTER). A TCS can be entered
22262306a36Sopenharmony_ci * by only one thread at a time.
22362306a36Sopenharmony_ci */
22462306a36Sopenharmony_cistruct sgx_tcs {
22562306a36Sopenharmony_ci	u64 state;
22662306a36Sopenharmony_ci	u64 flags;
22762306a36Sopenharmony_ci	u64 ssa_offset;
22862306a36Sopenharmony_ci	u32 ssa_index;
22962306a36Sopenharmony_ci	u32 nr_ssa_frames;
23062306a36Sopenharmony_ci	u64 entry_offset;
23162306a36Sopenharmony_ci	u64 exit_addr;
23262306a36Sopenharmony_ci	u64 fs_offset;
23362306a36Sopenharmony_ci	u64 gs_offset;
23462306a36Sopenharmony_ci	u32 fs_limit;
23562306a36Sopenharmony_ci	u32 gs_limit;
23662306a36Sopenharmony_ci	u8  reserved[SGX_TCS_RESERVED_SIZE];
23762306a36Sopenharmony_ci} __packed;
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci/**
24062306a36Sopenharmony_ci * struct sgx_pageinfo - an enclave page descriptor
24162306a36Sopenharmony_ci * @addr:	address of the enclave page
24262306a36Sopenharmony_ci * @contents:	pointer to the page contents
24362306a36Sopenharmony_ci * @metadata:	pointer either to a SECINFO or PCMD instance
24462306a36Sopenharmony_ci * @secs:	address of the SECS page
24562306a36Sopenharmony_ci */
24662306a36Sopenharmony_cistruct sgx_pageinfo {
24762306a36Sopenharmony_ci	u64 addr;
24862306a36Sopenharmony_ci	u64 contents;
24962306a36Sopenharmony_ci	u64 metadata;
25062306a36Sopenharmony_ci	u64 secs;
25162306a36Sopenharmony_ci} __packed __aligned(32);
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci/**
25562306a36Sopenharmony_ci * enum sgx_page_type - bits in the SECINFO flags defining the page type
25662306a36Sopenharmony_ci * %SGX_PAGE_TYPE_SECS:	a SECS page
25762306a36Sopenharmony_ci * %SGX_PAGE_TYPE_TCS:	a TCS page
25862306a36Sopenharmony_ci * %SGX_PAGE_TYPE_REG:	a regular page
25962306a36Sopenharmony_ci * %SGX_PAGE_TYPE_VA:	a VA page
26062306a36Sopenharmony_ci * %SGX_PAGE_TYPE_TRIM:	a page in trimmed state
26162306a36Sopenharmony_ci *
26262306a36Sopenharmony_ci * Make sure when making changes to this enum that its values can still fit
26362306a36Sopenharmony_ci * in the bitfield within &struct sgx_encl_page
26462306a36Sopenharmony_ci */
26562306a36Sopenharmony_cienum sgx_page_type {
26662306a36Sopenharmony_ci	SGX_PAGE_TYPE_SECS,
26762306a36Sopenharmony_ci	SGX_PAGE_TYPE_TCS,
26862306a36Sopenharmony_ci	SGX_PAGE_TYPE_REG,
26962306a36Sopenharmony_ci	SGX_PAGE_TYPE_VA,
27062306a36Sopenharmony_ci	SGX_PAGE_TYPE_TRIM,
27162306a36Sopenharmony_ci};
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci#define SGX_NR_PAGE_TYPES	5
27462306a36Sopenharmony_ci#define SGX_PAGE_TYPE_MASK	GENMASK(7, 0)
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci/**
27762306a36Sopenharmony_ci * enum sgx_secinfo_flags - the flags field in &struct sgx_secinfo
27862306a36Sopenharmony_ci * %SGX_SECINFO_R:	allow read
27962306a36Sopenharmony_ci * %SGX_SECINFO_W:	allow write
28062306a36Sopenharmony_ci * %SGX_SECINFO_X:	allow execution
28162306a36Sopenharmony_ci * %SGX_SECINFO_SECS:	a SECS page
28262306a36Sopenharmony_ci * %SGX_SECINFO_TCS:	a TCS page
28362306a36Sopenharmony_ci * %SGX_SECINFO_REG:	a regular page
28462306a36Sopenharmony_ci * %SGX_SECINFO_VA:	a VA page
28562306a36Sopenharmony_ci * %SGX_SECINFO_TRIM:	a page in trimmed state
28662306a36Sopenharmony_ci */
28762306a36Sopenharmony_cienum sgx_secinfo_flags {
28862306a36Sopenharmony_ci	SGX_SECINFO_R			= BIT(0),
28962306a36Sopenharmony_ci	SGX_SECINFO_W			= BIT(1),
29062306a36Sopenharmony_ci	SGX_SECINFO_X			= BIT(2),
29162306a36Sopenharmony_ci	SGX_SECINFO_SECS		= (SGX_PAGE_TYPE_SECS << 8),
29262306a36Sopenharmony_ci	SGX_SECINFO_TCS			= (SGX_PAGE_TYPE_TCS << 8),
29362306a36Sopenharmony_ci	SGX_SECINFO_REG			= (SGX_PAGE_TYPE_REG << 8),
29462306a36Sopenharmony_ci	SGX_SECINFO_VA			= (SGX_PAGE_TYPE_VA << 8),
29562306a36Sopenharmony_ci	SGX_SECINFO_TRIM		= (SGX_PAGE_TYPE_TRIM << 8),
29662306a36Sopenharmony_ci};
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci#define SGX_SECINFO_PERMISSION_MASK	GENMASK_ULL(2, 0)
29962306a36Sopenharmony_ci#define SGX_SECINFO_PAGE_TYPE_MASK	(SGX_PAGE_TYPE_MASK << 8)
30062306a36Sopenharmony_ci#define SGX_SECINFO_RESERVED_MASK	~(SGX_SECINFO_PERMISSION_MASK | \
30162306a36Sopenharmony_ci					  SGX_SECINFO_PAGE_TYPE_MASK)
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci/**
30462306a36Sopenharmony_ci * struct sgx_secinfo - describes attributes of an EPC page
30562306a36Sopenharmony_ci * @flags:	permissions and type
30662306a36Sopenharmony_ci *
30762306a36Sopenharmony_ci * Used together with ENCLS leaves that add or modify an EPC page to an
30862306a36Sopenharmony_ci * enclave to define page permissions and type.
30962306a36Sopenharmony_ci */
31062306a36Sopenharmony_cistruct sgx_secinfo {
31162306a36Sopenharmony_ci	u64 flags;
31262306a36Sopenharmony_ci	u8  reserved[56];
31362306a36Sopenharmony_ci} __packed __aligned(64);
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_ci#define SGX_PCMD_RESERVED_SIZE 40
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ci/**
31862306a36Sopenharmony_ci * struct sgx_pcmd - Paging Crypto Metadata (PCMD)
31962306a36Sopenharmony_ci * @enclave_id:	enclave identifier
32062306a36Sopenharmony_ci * @mac:	MAC over PCMD, page contents and isvsvn
32162306a36Sopenharmony_ci *
32262306a36Sopenharmony_ci * PCMD is stored for every swapped page to the regular memory. When ELDU loads
32362306a36Sopenharmony_ci * the page back it recalculates the MAC by using a isvsvn number stored in a
32462306a36Sopenharmony_ci * VA page. Together these two structures bring integrity and rollback
32562306a36Sopenharmony_ci * protection.
32662306a36Sopenharmony_ci */
32762306a36Sopenharmony_cistruct sgx_pcmd {
32862306a36Sopenharmony_ci	struct sgx_secinfo secinfo;
32962306a36Sopenharmony_ci	u64 enclave_id;
33062306a36Sopenharmony_ci	u8  reserved[SGX_PCMD_RESERVED_SIZE];
33162306a36Sopenharmony_ci	u8  mac[16];
33262306a36Sopenharmony_ci} __packed __aligned(128);
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci#define SGX_SIGSTRUCT_RESERVED1_SIZE 84
33562306a36Sopenharmony_ci#define SGX_SIGSTRUCT_RESERVED2_SIZE 20
33662306a36Sopenharmony_ci#define SGX_SIGSTRUCT_RESERVED3_SIZE 32
33762306a36Sopenharmony_ci#define SGX_SIGSTRUCT_RESERVED4_SIZE 12
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ci/**
34062306a36Sopenharmony_ci * struct sgx_sigstruct_header -  defines author of the enclave
34162306a36Sopenharmony_ci * @header1:		constant byte string
34262306a36Sopenharmony_ci * @vendor:		must be either 0x0000 or 0x8086
34362306a36Sopenharmony_ci * @date:		YYYYMMDD in BCD
34462306a36Sopenharmony_ci * @header2:		constant byte string
34562306a36Sopenharmony_ci * @swdefined:		software defined value
34662306a36Sopenharmony_ci */
34762306a36Sopenharmony_cistruct sgx_sigstruct_header {
34862306a36Sopenharmony_ci	u64 header1[2];
34962306a36Sopenharmony_ci	u32 vendor;
35062306a36Sopenharmony_ci	u32 date;
35162306a36Sopenharmony_ci	u64 header2[2];
35262306a36Sopenharmony_ci	u32 swdefined;
35362306a36Sopenharmony_ci	u8  reserved1[84];
35462306a36Sopenharmony_ci} __packed;
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci/**
35762306a36Sopenharmony_ci * struct sgx_sigstruct_body - defines contents of the enclave
35862306a36Sopenharmony_ci * @miscselect:		additional information stored to an SSA frame
35962306a36Sopenharmony_ci * @misc_mask:		required miscselect in SECS
36062306a36Sopenharmony_ci * @attributes:		attributes for enclave
36162306a36Sopenharmony_ci * @xfrm:		XSave-Feature Request Mask (subset of XCR0)
36262306a36Sopenharmony_ci * @attributes_mask:	required attributes in SECS
36362306a36Sopenharmony_ci * @xfrm_mask:		required XFRM in SECS
36462306a36Sopenharmony_ci * @mrenclave:		SHA256-hash of the enclave contents
36562306a36Sopenharmony_ci * @isvprodid:		a user-defined value that is used in key derivation
36662306a36Sopenharmony_ci * @isvsvn:		a user-defined value that is used in key derivation
36762306a36Sopenharmony_ci */
36862306a36Sopenharmony_cistruct sgx_sigstruct_body {
36962306a36Sopenharmony_ci	u32 miscselect;
37062306a36Sopenharmony_ci	u32 misc_mask;
37162306a36Sopenharmony_ci	u8  reserved2[20];
37262306a36Sopenharmony_ci	u64 attributes;
37362306a36Sopenharmony_ci	u64 xfrm;
37462306a36Sopenharmony_ci	u64 attributes_mask;
37562306a36Sopenharmony_ci	u64 xfrm_mask;
37662306a36Sopenharmony_ci	u8  mrenclave[32];
37762306a36Sopenharmony_ci	u8  reserved3[32];
37862306a36Sopenharmony_ci	u16 isvprodid;
37962306a36Sopenharmony_ci	u16 isvsvn;
38062306a36Sopenharmony_ci} __packed;
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ci/**
38362306a36Sopenharmony_ci * struct sgx_sigstruct - an enclave signature
38462306a36Sopenharmony_ci * @header:		defines author of the enclave
38562306a36Sopenharmony_ci * @modulus:		the modulus of the public key
38662306a36Sopenharmony_ci * @exponent:		the exponent of the public key
38762306a36Sopenharmony_ci * @signature:		the signature calculated over the fields except modulus,
38862306a36Sopenharmony_ci * @body:		defines contents of the enclave
38962306a36Sopenharmony_ci * @q1:			a value used in RSA signature verification
39062306a36Sopenharmony_ci * @q2:			a value used in RSA signature verification
39162306a36Sopenharmony_ci *
39262306a36Sopenharmony_ci * Header and body are the parts that are actual signed. The remaining fields
39362306a36Sopenharmony_ci * define the signature of the enclave.
39462306a36Sopenharmony_ci */
39562306a36Sopenharmony_cistruct sgx_sigstruct {
39662306a36Sopenharmony_ci	struct sgx_sigstruct_header header;
39762306a36Sopenharmony_ci	u8  modulus[SGX_MODULUS_SIZE];
39862306a36Sopenharmony_ci	u32 exponent;
39962306a36Sopenharmony_ci	u8  signature[SGX_MODULUS_SIZE];
40062306a36Sopenharmony_ci	struct sgx_sigstruct_body body;
40162306a36Sopenharmony_ci	u8  reserved4[12];
40262306a36Sopenharmony_ci	u8  q1[SGX_MODULUS_SIZE];
40362306a36Sopenharmony_ci	u8  q2[SGX_MODULUS_SIZE];
40462306a36Sopenharmony_ci} __packed;
40562306a36Sopenharmony_ci
40662306a36Sopenharmony_ci#define SGX_LAUNCH_TOKEN_SIZE 304
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_ci/*
40962306a36Sopenharmony_ci * Do not put any hardware-defined SGX structure representations below this
41062306a36Sopenharmony_ci * comment!
41162306a36Sopenharmony_ci */
41262306a36Sopenharmony_ci
41362306a36Sopenharmony_ci#ifdef CONFIG_X86_SGX_KVM
41462306a36Sopenharmony_ciint sgx_virt_ecreate(struct sgx_pageinfo *pageinfo, void __user *secs,
41562306a36Sopenharmony_ci		     int *trapnr);
41662306a36Sopenharmony_ciint sgx_virt_einit(void __user *sigstruct, void __user *token,
41762306a36Sopenharmony_ci		   void __user *secs, u64 *lepubkeyhash, int *trapnr);
41862306a36Sopenharmony_ci#endif
41962306a36Sopenharmony_ci
42062306a36Sopenharmony_ciint sgx_set_attribute(unsigned long *allowed_attributes,
42162306a36Sopenharmony_ci		      unsigned int attribute_fd);
42262306a36Sopenharmony_ci
42362306a36Sopenharmony_ci#endif /* _ASM_X86_SGX_H */
424