1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /*
3  * Copyright(c) 2016-20 Intel Corporation.
4  */
5 #ifndef _UAPI_ASM_X86_SGX_H
6 #define _UAPI_ASM_X86_SGX_H
7 
8 #include <linux/types.h>
9 #include <linux/ioctl.h>
10 
11 /**
12  * enum sgx_page_flags - page control flags
13  * %SGX_PAGE_MEASURE:	Measure the page contents with a sequence of
14  *			ENCLS[EEXTEND] operations.
15  */
16 enum sgx_page_flags {
17 	SGX_PAGE_MEASURE	= 0x01,
18 };
19 
20 #define SGX_MAGIC 0xA4
21 
22 #define SGX_IOC_ENCLAVE_CREATE \
23 	_IOW(SGX_MAGIC, 0x00, struct sgx_enclave_create)
24 #define SGX_IOC_ENCLAVE_ADD_PAGES \
25 	_IOWR(SGX_MAGIC, 0x01, struct sgx_enclave_add_pages)
26 #define SGX_IOC_ENCLAVE_INIT \
27 	_IOW(SGX_MAGIC, 0x02, struct sgx_enclave_init)
28 #define SGX_IOC_ENCLAVE_PROVISION \
29 	_IOW(SGX_MAGIC, 0x03, struct sgx_enclave_provision)
30 #define SGX_IOC_VEPC_REMOVE_ALL \
31 	_IO(SGX_MAGIC, 0x04)
32 
33 /**
34  * struct sgx_enclave_create - parameter structure for the
35  *                             %SGX_IOC_ENCLAVE_CREATE ioctl
36  * @src:	address for the SECS page data
37  */
38 struct sgx_enclave_create  {
39 	__u64	src;
40 };
41 
42 /**
43  * struct sgx_enclave_add_pages - parameter structure for the
44  *                                %SGX_IOC_ENCLAVE_ADD_PAGE ioctl
45  * @src:	start address for the page data
46  * @offset:	starting page offset
47  * @length:	length of the data (multiple of the page size)
48  * @secinfo:	address for the SECINFO data
49  * @flags:	page control flags
50  * @count:	number of bytes added (multiple of the page size)
51  */
52 struct sgx_enclave_add_pages {
53 	__u64 src;
54 	__u64 offset;
55 	__u64 length;
56 	__u64 secinfo;
57 	__u64 flags;
58 	__u64 count;
59 };
60 
61 /**
62  * struct sgx_enclave_init - parameter structure for the
63  *                           %SGX_IOC_ENCLAVE_INIT ioctl
64  * @sigstruct:	address for the SIGSTRUCT data
65  */
66 struct sgx_enclave_init {
67 	__u64 sigstruct;
68 };
69 
70 /**
71  * struct sgx_enclave_provision - parameter structure for the
72  *				  %SGX_IOC_ENCLAVE_PROVISION ioctl
73  * @fd:		file handle of /dev/sgx_provision
74  */
75 struct sgx_enclave_provision {
76 	__u64 fd;
77 };
78 
79 struct sgx_enclave_run;
80 
81 /**
82  * typedef sgx_enclave_user_handler_t - Exit handler function accepted by
83  *					__vdso_sgx_enter_enclave()
84  * @run:	The run instance given by the caller
85  *
86  * The register parameters contain the snapshot of their values at enclave
87  * exit. An invalid ENCLU function number will cause -EINVAL to be returned
88  * to the caller.
89  *
90  * Return:
91  * - <= 0:	The given value is returned back to the caller.
92  * - > 0:	ENCLU function to invoke, either EENTER or ERESUME.
93  */
94 typedef int (*sgx_enclave_user_handler_t)(long rdi, long rsi, long rdx,
95 					  long rsp, long r8, long r9,
96 					  struct sgx_enclave_run *run);
97 
98 /**
99  * struct sgx_enclave_run - the execution context of __vdso_sgx_enter_enclave()
100  * @tcs:			TCS used to enter the enclave
101  * @function:			The last seen ENCLU function (EENTER, ERESUME or EEXIT)
102  * @exception_vector:		The interrupt vector of the exception
103  * @exception_error_code:	The exception error code pulled out of the stack
104  * @exception_addr:		The address that triggered the exception
105  * @user_handler:		User provided callback run on exception
106  * @user_data:			Data passed to the user handler
107  * @reserved			Reserved for future extensions
108  *
109  * If @user_handler is provided, the handler will be invoked on all return paths
110  * of the normal flow.  The user handler may transfer control, e.g. via a
111  * longjmp() call or a C++ exception, without returning to
112  * __vdso_sgx_enter_enclave().
113  */
114 struct sgx_enclave_run {
115 	__u64 tcs;
116 	__u32 function;
117 	__u16 exception_vector;
118 	__u16 exception_error_code;
119 	__u64 exception_addr;
120 	__u64 user_handler;
121 	__u64 user_data;
122 	__u8  reserved[216];
123 };
124 
125 /**
126  * typedef vdso_sgx_enter_enclave_t - Prototype for __vdso_sgx_enter_enclave(),
127  *				      a vDSO function to enter an SGX enclave.
128  * @rdi:	Pass-through value for RDI
129  * @rsi:	Pass-through value for RSI
130  * @rdx:	Pass-through value for RDX
131  * @function:	ENCLU function, must be EENTER or ERESUME
132  * @r8:		Pass-through value for R8
133  * @r9:		Pass-through value for R9
134  * @run:	struct sgx_enclave_run, must be non-NULL
135  *
136  * NOTE: __vdso_sgx_enter_enclave() does not ensure full compliance with the
137  * x86-64 ABI, e.g. doesn't handle XSAVE state.  Except for non-volatile
138  * general purpose registers, EFLAGS.DF, and RSP alignment, preserving/setting
139  * state in accordance with the x86-64 ABI is the responsibility of the enclave
140  * and its runtime, i.e. __vdso_sgx_enter_enclave() cannot be called from C
141  * code without careful consideration by both the enclave and its runtime.
142  *
143  * All general purpose registers except RAX, RBX and RCX are passed as-is to the
144  * enclave.  RAX, RBX and RCX are consumed by EENTER and ERESUME and are loaded
145  * with @function, asynchronous exit pointer, and @run.tcs respectively.
146  *
147  * RBP and the stack are used to anchor __vdso_sgx_enter_enclave() to the
148  * pre-enclave state, e.g. to retrieve @run.exception and @run.user_handler
149  * after an enclave exit.  All other registers are available for use by the
150  * enclave and its runtime, e.g. an enclave can push additional data onto the
151  * stack (and modify RSP) to pass information to the optional user handler (see
152  * below).
153  *
154  * Most exceptions reported on ENCLU, including those that occur within the
155  * enclave, are fixed up and reported synchronously instead of being delivered
156  * via a standard signal. Debug Exceptions (#DB) and Breakpoints (#BP) are
157  * never fixed up and are always delivered via standard signals. On synchronously
158  * reported exceptions, -EFAULT is returned and details about the exception are
159  * recorded in @run.exception, the optional sgx_enclave_exception struct.
160  *
161  * Return:
162  * - 0:		ENCLU function was successfully executed.
163  * - -EINVAL:	Invalid ENCL number (neither EENTER nor ERESUME).
164  */
165 typedef int (*vdso_sgx_enter_enclave_t)(unsigned long rdi, unsigned long rsi,
166 					unsigned long rdx, unsigned int function,
167 					unsigned long r8,  unsigned long r9,
168 					struct sgx_enclave_run *run);
169 
170 #endif /* _UAPI_ASM_X86_SGX_H */
171