xref: /openbmc/linux/arch/x86/include/uapi/asm/sgx.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1888d2491SJarkko Sakkinen /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2888d2491SJarkko Sakkinen /*
3888d2491SJarkko Sakkinen  * Copyright(c) 2016-20 Intel Corporation.
4888d2491SJarkko Sakkinen  */
5888d2491SJarkko Sakkinen #ifndef _UAPI_ASM_X86_SGX_H
6888d2491SJarkko Sakkinen #define _UAPI_ASM_X86_SGX_H
7888d2491SJarkko Sakkinen 
8888d2491SJarkko Sakkinen #include <linux/types.h>
9888d2491SJarkko Sakkinen #include <linux/ioctl.h>
10888d2491SJarkko Sakkinen 
11c6d26d37SJarkko Sakkinen /**
12bab8c183SMauro Carvalho Chehab  * enum sgx_page_flags - page control flags
13c6d26d37SJarkko Sakkinen  * %SGX_PAGE_MEASURE:	Measure the page contents with a sequence of
14c6d26d37SJarkko Sakkinen  *			ENCLS[EEXTEND] operations.
15c6d26d37SJarkko Sakkinen  */
16c6d26d37SJarkko Sakkinen enum sgx_page_flags {
17c6d26d37SJarkko Sakkinen 	SGX_PAGE_MEASURE	= 0x01,
18c6d26d37SJarkko Sakkinen };
19c6d26d37SJarkko Sakkinen 
20888d2491SJarkko Sakkinen #define SGX_MAGIC 0xA4
21888d2491SJarkko Sakkinen 
22888d2491SJarkko Sakkinen #define SGX_IOC_ENCLAVE_CREATE \
23888d2491SJarkko Sakkinen 	_IOW(SGX_MAGIC, 0x00, struct sgx_enclave_create)
24c6d26d37SJarkko Sakkinen #define SGX_IOC_ENCLAVE_ADD_PAGES \
25c6d26d37SJarkko Sakkinen 	_IOWR(SGX_MAGIC, 0x01, struct sgx_enclave_add_pages)
269d0c151bSJarkko Sakkinen #define SGX_IOC_ENCLAVE_INIT \
279d0c151bSJarkko Sakkinen 	_IOW(SGX_MAGIC, 0x02, struct sgx_enclave_init)
28c82c6186SJarkko Sakkinen #define SGX_IOC_ENCLAVE_PROVISION \
29c82c6186SJarkko Sakkinen 	_IOW(SGX_MAGIC, 0x03, struct sgx_enclave_provision)
30ae095b16SPaolo Bonzini #define SGX_IOC_VEPC_REMOVE_ALL \
31ae095b16SPaolo Bonzini 	_IO(SGX_MAGIC, 0x04)
32ff08530aSReinette Chatre #define SGX_IOC_ENCLAVE_RESTRICT_PERMISSIONS \
33ff08530aSReinette Chatre 	_IOWR(SGX_MAGIC, 0x05, struct sgx_enclave_restrict_permissions)
3445d546b8SReinette Chatre #define SGX_IOC_ENCLAVE_MODIFY_TYPES \
3545d546b8SReinette Chatre 	_IOWR(SGX_MAGIC, 0x06, struct sgx_enclave_modify_types)
36*9849bb27SReinette Chatre #define SGX_IOC_ENCLAVE_REMOVE_PAGES \
37*9849bb27SReinette Chatre 	_IOWR(SGX_MAGIC, 0x07, struct sgx_enclave_remove_pages)
38888d2491SJarkko Sakkinen 
39888d2491SJarkko Sakkinen /**
40888d2491SJarkko Sakkinen  * struct sgx_enclave_create - parameter structure for the
41888d2491SJarkko Sakkinen  *                             %SGX_IOC_ENCLAVE_CREATE ioctl
42888d2491SJarkko Sakkinen  * @src:	address for the SECS page data
43888d2491SJarkko Sakkinen  */
44888d2491SJarkko Sakkinen struct sgx_enclave_create  {
45888d2491SJarkko Sakkinen 	__u64	src;
46888d2491SJarkko Sakkinen };
47888d2491SJarkko Sakkinen 
48c6d26d37SJarkko Sakkinen /**
49c6d26d37SJarkko Sakkinen  * struct sgx_enclave_add_pages - parameter structure for the
50c6d26d37SJarkko Sakkinen  *                                %SGX_IOC_ENCLAVE_ADD_PAGE ioctl
51c6d26d37SJarkko Sakkinen  * @src:	start address for the page data
52c6d26d37SJarkko Sakkinen  * @offset:	starting page offset
53c6d26d37SJarkko Sakkinen  * @length:	length of the data (multiple of the page size)
54c6d26d37SJarkko Sakkinen  * @secinfo:	address for the SECINFO data
55c6d26d37SJarkko Sakkinen  * @flags:	page control flags
56c6d26d37SJarkko Sakkinen  * @count:	number of bytes added (multiple of the page size)
57c6d26d37SJarkko Sakkinen  */
58c6d26d37SJarkko Sakkinen struct sgx_enclave_add_pages {
59c6d26d37SJarkko Sakkinen 	__u64 src;
60c6d26d37SJarkko Sakkinen 	__u64 offset;
61c6d26d37SJarkko Sakkinen 	__u64 length;
62c6d26d37SJarkko Sakkinen 	__u64 secinfo;
63c6d26d37SJarkko Sakkinen 	__u64 flags;
64c6d26d37SJarkko Sakkinen 	__u64 count;
65c6d26d37SJarkko Sakkinen };
66c6d26d37SJarkko Sakkinen 
679d0c151bSJarkko Sakkinen /**
689d0c151bSJarkko Sakkinen  * struct sgx_enclave_init - parameter structure for the
699d0c151bSJarkko Sakkinen  *                           %SGX_IOC_ENCLAVE_INIT ioctl
709d0c151bSJarkko Sakkinen  * @sigstruct:	address for the SIGSTRUCT data
719d0c151bSJarkko Sakkinen  */
729d0c151bSJarkko Sakkinen struct sgx_enclave_init {
739d0c151bSJarkko Sakkinen 	__u64 sigstruct;
749d0c151bSJarkko Sakkinen };
759d0c151bSJarkko Sakkinen 
76c82c6186SJarkko Sakkinen /**
77c82c6186SJarkko Sakkinen  * struct sgx_enclave_provision - parameter structure for the
78c82c6186SJarkko Sakkinen  *				  %SGX_IOC_ENCLAVE_PROVISION ioctl
79c82c6186SJarkko Sakkinen  * @fd:		file handle of /dev/sgx_provision
80c82c6186SJarkko Sakkinen  */
81c82c6186SJarkko Sakkinen struct sgx_enclave_provision {
82c82c6186SJarkko Sakkinen 	__u64 fd;
83c82c6186SJarkko Sakkinen };
84c82c6186SJarkko Sakkinen 
85ff08530aSReinette Chatre /**
86ff08530aSReinette Chatre  * struct sgx_enclave_restrict_permissions - parameters for ioctl
87ff08530aSReinette Chatre  *                                        %SGX_IOC_ENCLAVE_RESTRICT_PERMISSIONS
88ff08530aSReinette Chatre  * @offset:	starting page offset (page aligned relative to enclave base
89ff08530aSReinette Chatre  *		address defined in SECS)
90ff08530aSReinette Chatre  * @length:	length of memory (multiple of the page size)
91ff08530aSReinette Chatre  * @permissions:new permission bits for pages in range described by @offset
92ff08530aSReinette Chatre  *              and @length
93ff08530aSReinette Chatre  * @result:	(output) SGX result code of ENCLS[EMODPR] function
94ff08530aSReinette Chatre  * @count:	(output) bytes successfully changed (multiple of page size)
95ff08530aSReinette Chatre  */
96ff08530aSReinette Chatre struct sgx_enclave_restrict_permissions {
97ff08530aSReinette Chatre 	__u64 offset;
98ff08530aSReinette Chatre 	__u64 length;
99ff08530aSReinette Chatre 	__u64 permissions;
100ff08530aSReinette Chatre 	__u64 result;
101ff08530aSReinette Chatre 	__u64 count;
102ff08530aSReinette Chatre };
103ff08530aSReinette Chatre 
10445d546b8SReinette Chatre /**
10545d546b8SReinette Chatre  * struct sgx_enclave_modify_types - parameters for ioctl
10645d546b8SReinette Chatre  *                                   %SGX_IOC_ENCLAVE_MODIFY_TYPES
10745d546b8SReinette Chatre  * @offset:	starting page offset (page aligned relative to enclave base
10845d546b8SReinette Chatre  *		address defined in SECS)
10945d546b8SReinette Chatre  * @length:	length of memory (multiple of the page size)
11045d546b8SReinette Chatre  * @page_type:	new type for pages in range described by @offset and @length
11145d546b8SReinette Chatre  * @result:	(output) SGX result code of ENCLS[EMODT] function
11245d546b8SReinette Chatre  * @count:	(output) bytes successfully changed (multiple of page size)
11345d546b8SReinette Chatre  */
11445d546b8SReinette Chatre struct sgx_enclave_modify_types {
11545d546b8SReinette Chatre 	__u64 offset;
11645d546b8SReinette Chatre 	__u64 length;
11745d546b8SReinette Chatre 	__u64 page_type;
11845d546b8SReinette Chatre 	__u64 result;
11945d546b8SReinette Chatre 	__u64 count;
12045d546b8SReinette Chatre };
12145d546b8SReinette Chatre 
122*9849bb27SReinette Chatre /**
123*9849bb27SReinette Chatre  * struct sgx_enclave_remove_pages - %SGX_IOC_ENCLAVE_REMOVE_PAGES parameters
124*9849bb27SReinette Chatre  * @offset:	starting page offset (page aligned relative to enclave base
125*9849bb27SReinette Chatre  *		address defined in SECS)
126*9849bb27SReinette Chatre  * @length:	length of memory (multiple of the page size)
127*9849bb27SReinette Chatre  * @count:	(output) bytes successfully changed (multiple of page size)
128*9849bb27SReinette Chatre  *
129*9849bb27SReinette Chatre  * Regular (PT_REG) or TCS (PT_TCS) can be removed from an initialized
130*9849bb27SReinette Chatre  * enclave if the system supports SGX2. First, the %SGX_IOC_ENCLAVE_MODIFY_TYPES
131*9849bb27SReinette Chatre  * ioctl() should be used to change the page type to PT_TRIM. After that
132*9849bb27SReinette Chatre  * succeeds ENCLU[EACCEPT] should be run from within the enclave and then
133*9849bb27SReinette Chatre  * %SGX_IOC_ENCLAVE_REMOVE_PAGES can be used to complete the page removal.
134*9849bb27SReinette Chatre  */
135*9849bb27SReinette Chatre struct sgx_enclave_remove_pages {
136*9849bb27SReinette Chatre 	__u64 offset;
137*9849bb27SReinette Chatre 	__u64 length;
138*9849bb27SReinette Chatre 	__u64 count;
139*9849bb27SReinette Chatre };
140*9849bb27SReinette Chatre 
14184664369SSean Christopherson struct sgx_enclave_run;
14284664369SSean Christopherson 
14384664369SSean Christopherson /**
14484664369SSean Christopherson  * typedef sgx_enclave_user_handler_t - Exit handler function accepted by
14584664369SSean Christopherson  *					__vdso_sgx_enter_enclave()
14684664369SSean Christopherson  * @run:	The run instance given by the caller
14784664369SSean Christopherson  *
14884664369SSean Christopherson  * The register parameters contain the snapshot of their values at enclave
14984664369SSean Christopherson  * exit. An invalid ENCLU function number will cause -EINVAL to be returned
15084664369SSean Christopherson  * to the caller.
15184664369SSean Christopherson  *
15284664369SSean Christopherson  * Return:
15384664369SSean Christopherson  * - <= 0:	The given value is returned back to the caller.
15484664369SSean Christopherson  * - > 0:	ENCLU function to invoke, either EENTER or ERESUME.
15584664369SSean Christopherson  */
15684664369SSean Christopherson typedef int (*sgx_enclave_user_handler_t)(long rdi, long rsi, long rdx,
15784664369SSean Christopherson 					  long rsp, long r8, long r9,
15884664369SSean Christopherson 					  struct sgx_enclave_run *run);
15984664369SSean Christopherson 
16084664369SSean Christopherson /**
16184664369SSean Christopherson  * struct sgx_enclave_run - the execution context of __vdso_sgx_enter_enclave()
16284664369SSean Christopherson  * @tcs:			TCS used to enter the enclave
16384664369SSean Christopherson  * @function:			The last seen ENCLU function (EENTER, ERESUME or EEXIT)
16484664369SSean Christopherson  * @exception_vector:		The interrupt vector of the exception
16584664369SSean Christopherson  * @exception_error_code:	The exception error code pulled out of the stack
16684664369SSean Christopherson  * @exception_addr:		The address that triggered the exception
16784664369SSean Christopherson  * @user_handler:		User provided callback run on exception
16884664369SSean Christopherson  * @user_data:			Data passed to the user handler
16984664369SSean Christopherson  * @reserved			Reserved for future extensions
17084664369SSean Christopherson  *
17184664369SSean Christopherson  * If @user_handler is provided, the handler will be invoked on all return paths
17284664369SSean Christopherson  * of the normal flow.  The user handler may transfer control, e.g. via a
17384664369SSean Christopherson  * longjmp() call or a C++ exception, without returning to
17484664369SSean Christopherson  * __vdso_sgx_enter_enclave().
17584664369SSean Christopherson  */
17684664369SSean Christopherson struct sgx_enclave_run {
17784664369SSean Christopherson 	__u64 tcs;
17884664369SSean Christopherson 	__u32 function;
17984664369SSean Christopherson 	__u16 exception_vector;
18084664369SSean Christopherson 	__u16 exception_error_code;
18184664369SSean Christopherson 	__u64 exception_addr;
18284664369SSean Christopherson 	__u64 user_handler;
18384664369SSean Christopherson 	__u64 user_data;
18484664369SSean Christopherson 	__u8  reserved[216];
18584664369SSean Christopherson };
18684664369SSean Christopherson 
18784664369SSean Christopherson /**
18884664369SSean Christopherson  * typedef vdso_sgx_enter_enclave_t - Prototype for __vdso_sgx_enter_enclave(),
18984664369SSean Christopherson  *				      a vDSO function to enter an SGX enclave.
19084664369SSean Christopherson  * @rdi:	Pass-through value for RDI
19184664369SSean Christopherson  * @rsi:	Pass-through value for RSI
19284664369SSean Christopherson  * @rdx:	Pass-through value for RDX
19384664369SSean Christopherson  * @function:	ENCLU function, must be EENTER or ERESUME
19484664369SSean Christopherson  * @r8:		Pass-through value for R8
19584664369SSean Christopherson  * @r9:		Pass-through value for R9
19684664369SSean Christopherson  * @run:	struct sgx_enclave_run, must be non-NULL
19784664369SSean Christopherson  *
19884664369SSean Christopherson  * NOTE: __vdso_sgx_enter_enclave() does not ensure full compliance with the
19984664369SSean Christopherson  * x86-64 ABI, e.g. doesn't handle XSAVE state.  Except for non-volatile
20084664369SSean Christopherson  * general purpose registers, EFLAGS.DF, and RSP alignment, preserving/setting
20184664369SSean Christopherson  * state in accordance with the x86-64 ABI is the responsibility of the enclave
20284664369SSean Christopherson  * and its runtime, i.e. __vdso_sgx_enter_enclave() cannot be called from C
20384664369SSean Christopherson  * code without careful consideration by both the enclave and its runtime.
20484664369SSean Christopherson  *
20584664369SSean Christopherson  * All general purpose registers except RAX, RBX and RCX are passed as-is to the
20684664369SSean Christopherson  * enclave.  RAX, RBX and RCX are consumed by EENTER and ERESUME and are loaded
20784664369SSean Christopherson  * with @function, asynchronous exit pointer, and @run.tcs respectively.
20884664369SSean Christopherson  *
20984664369SSean Christopherson  * RBP and the stack are used to anchor __vdso_sgx_enter_enclave() to the
21084664369SSean Christopherson  * pre-enclave state, e.g. to retrieve @run.exception and @run.user_handler
21184664369SSean Christopherson  * after an enclave exit.  All other registers are available for use by the
21284664369SSean Christopherson  * enclave and its runtime, e.g. an enclave can push additional data onto the
21384664369SSean Christopherson  * stack (and modify RSP) to pass information to the optional user handler (see
21484664369SSean Christopherson  * below).
21584664369SSean Christopherson  *
21684664369SSean Christopherson  * Most exceptions reported on ENCLU, including those that occur within the
21784664369SSean Christopherson  * enclave, are fixed up and reported synchronously instead of being delivered
21884664369SSean Christopherson  * via a standard signal. Debug Exceptions (#DB) and Breakpoints (#BP) are
219d9f6e12fSIngo Molnar  * never fixed up and are always delivered via standard signals. On synchronously
22084664369SSean Christopherson  * reported exceptions, -EFAULT is returned and details about the exception are
22184664369SSean Christopherson  * recorded in @run.exception, the optional sgx_enclave_exception struct.
22284664369SSean Christopherson  *
22384664369SSean Christopherson  * Return:
22484664369SSean Christopherson  * - 0:		ENCLU function was successfully executed.
22584664369SSean Christopherson  * - -EINVAL:	Invalid ENCL number (neither EENTER nor ERESUME).
22684664369SSean Christopherson  */
22784664369SSean Christopherson typedef int (*vdso_sgx_enter_enclave_t)(unsigned long rdi, unsigned long rsi,
22884664369SSean Christopherson 					unsigned long rdx, unsigned int function,
22984664369SSean Christopherson 					unsigned long r8,  unsigned long r9,
23084664369SSean Christopherson 					struct sgx_enclave_run *run);
23184664369SSean Christopherson 
232888d2491SJarkko Sakkinen #endif /* _UAPI_ASM_X86_SGX_H */
233