xref: /linux/arch/x86/include/uapi/asm/sgx.h (revision 45d546b8c109d69f6659d58b2ace005b2f07f557)
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 #define SGX_IOC_ENCLAVE_RESTRICT_PERMISSIONS \
33 	_IOWR(SGX_MAGIC, 0x05, struct sgx_enclave_restrict_permissions)
34 #define SGX_IOC_ENCLAVE_MODIFY_TYPES \
35 	_IOWR(SGX_MAGIC, 0x06, struct sgx_enclave_modify_types)
36 
37 /**
38  * struct sgx_enclave_create - parameter structure for the
39  *                             %SGX_IOC_ENCLAVE_CREATE ioctl
40  * @src:	address for the SECS page data
41  */
42 struct sgx_enclave_create  {
43 	__u64	src;
44 };
45 
46 /**
47  * struct sgx_enclave_add_pages - parameter structure for the
48  *                                %SGX_IOC_ENCLAVE_ADD_PAGE ioctl
49  * @src:	start address for the page data
50  * @offset:	starting page offset
51  * @length:	length of the data (multiple of the page size)
52  * @secinfo:	address for the SECINFO data
53  * @flags:	page control flags
54  * @count:	number of bytes added (multiple of the page size)
55  */
56 struct sgx_enclave_add_pages {
57 	__u64 src;
58 	__u64 offset;
59 	__u64 length;
60 	__u64 secinfo;
61 	__u64 flags;
62 	__u64 count;
63 };
64 
65 /**
66  * struct sgx_enclave_init - parameter structure for the
67  *                           %SGX_IOC_ENCLAVE_INIT ioctl
68  * @sigstruct:	address for the SIGSTRUCT data
69  */
70 struct sgx_enclave_init {
71 	__u64 sigstruct;
72 };
73 
74 /**
75  * struct sgx_enclave_provision - parameter structure for the
76  *				  %SGX_IOC_ENCLAVE_PROVISION ioctl
77  * @fd:		file handle of /dev/sgx_provision
78  */
79 struct sgx_enclave_provision {
80 	__u64 fd;
81 };
82 
83 /**
84  * struct sgx_enclave_restrict_permissions - parameters for ioctl
85  *                                        %SGX_IOC_ENCLAVE_RESTRICT_PERMISSIONS
86  * @offset:	starting page offset (page aligned relative to enclave base
87  *		address defined in SECS)
88  * @length:	length of memory (multiple of the page size)
89  * @permissions:new permission bits for pages in range described by @offset
90  *              and @length
91  * @result:	(output) SGX result code of ENCLS[EMODPR] function
92  * @count:	(output) bytes successfully changed (multiple of page size)
93  */
94 struct sgx_enclave_restrict_permissions {
95 	__u64 offset;
96 	__u64 length;
97 	__u64 permissions;
98 	__u64 result;
99 	__u64 count;
100 };
101 
102 /**
103  * struct sgx_enclave_modify_types - parameters for ioctl
104  *                                   %SGX_IOC_ENCLAVE_MODIFY_TYPES
105  * @offset:	starting page offset (page aligned relative to enclave base
106  *		address defined in SECS)
107  * @length:	length of memory (multiple of the page size)
108  * @page_type:	new type for pages in range described by @offset and @length
109  * @result:	(output) SGX result code of ENCLS[EMODT] function
110  * @count:	(output) bytes successfully changed (multiple of page size)
111  */
112 struct sgx_enclave_modify_types {
113 	__u64 offset;
114 	__u64 length;
115 	__u64 page_type;
116 	__u64 result;
117 	__u64 count;
118 };
119 
120 struct sgx_enclave_run;
121 
122 /**
123  * typedef sgx_enclave_user_handler_t - Exit handler function accepted by
124  *					__vdso_sgx_enter_enclave()
125  * @run:	The run instance given by the caller
126  *
127  * The register parameters contain the snapshot of their values at enclave
128  * exit. An invalid ENCLU function number will cause -EINVAL to be returned
129  * to the caller.
130  *
131  * Return:
132  * - <= 0:	The given value is returned back to the caller.
133  * - > 0:	ENCLU function to invoke, either EENTER or ERESUME.
134  */
135 typedef int (*sgx_enclave_user_handler_t)(long rdi, long rsi, long rdx,
136 					  long rsp, long r8, long r9,
137 					  struct sgx_enclave_run *run);
138 
139 /**
140  * struct sgx_enclave_run - the execution context of __vdso_sgx_enter_enclave()
141  * @tcs:			TCS used to enter the enclave
142  * @function:			The last seen ENCLU function (EENTER, ERESUME or EEXIT)
143  * @exception_vector:		The interrupt vector of the exception
144  * @exception_error_code:	The exception error code pulled out of the stack
145  * @exception_addr:		The address that triggered the exception
146  * @user_handler:		User provided callback run on exception
147  * @user_data:			Data passed to the user handler
148  * @reserved			Reserved for future extensions
149  *
150  * If @user_handler is provided, the handler will be invoked on all return paths
151  * of the normal flow.  The user handler may transfer control, e.g. via a
152  * longjmp() call or a C++ exception, without returning to
153  * __vdso_sgx_enter_enclave().
154  */
155 struct sgx_enclave_run {
156 	__u64 tcs;
157 	__u32 function;
158 	__u16 exception_vector;
159 	__u16 exception_error_code;
160 	__u64 exception_addr;
161 	__u64 user_handler;
162 	__u64 user_data;
163 	__u8  reserved[216];
164 };
165 
166 /**
167  * typedef vdso_sgx_enter_enclave_t - Prototype for __vdso_sgx_enter_enclave(),
168  *				      a vDSO function to enter an SGX enclave.
169  * @rdi:	Pass-through value for RDI
170  * @rsi:	Pass-through value for RSI
171  * @rdx:	Pass-through value for RDX
172  * @function:	ENCLU function, must be EENTER or ERESUME
173  * @r8:		Pass-through value for R8
174  * @r9:		Pass-through value for R9
175  * @run:	struct sgx_enclave_run, must be non-NULL
176  *
177  * NOTE: __vdso_sgx_enter_enclave() does not ensure full compliance with the
178  * x86-64 ABI, e.g. doesn't handle XSAVE state.  Except for non-volatile
179  * general purpose registers, EFLAGS.DF, and RSP alignment, preserving/setting
180  * state in accordance with the x86-64 ABI is the responsibility of the enclave
181  * and its runtime, i.e. __vdso_sgx_enter_enclave() cannot be called from C
182  * code without careful consideration by both the enclave and its runtime.
183  *
184  * All general purpose registers except RAX, RBX and RCX are passed as-is to the
185  * enclave.  RAX, RBX and RCX are consumed by EENTER and ERESUME and are loaded
186  * with @function, asynchronous exit pointer, and @run.tcs respectively.
187  *
188  * RBP and the stack are used to anchor __vdso_sgx_enter_enclave() to the
189  * pre-enclave state, e.g. to retrieve @run.exception and @run.user_handler
190  * after an enclave exit.  All other registers are available for use by the
191  * enclave and its runtime, e.g. an enclave can push additional data onto the
192  * stack (and modify RSP) to pass information to the optional user handler (see
193  * below).
194  *
195  * Most exceptions reported on ENCLU, including those that occur within the
196  * enclave, are fixed up and reported synchronously instead of being delivered
197  * via a standard signal. Debug Exceptions (#DB) and Breakpoints (#BP) are
198  * never fixed up and are always delivered via standard signals. On synchronously
199  * reported exceptions, -EFAULT is returned and details about the exception are
200  * recorded in @run.exception, the optional sgx_enclave_exception struct.
201  *
202  * Return:
203  * - 0:		ENCLU function was successfully executed.
204  * - -EINVAL:	Invalid ENCL number (neither EENTER nor ERESUME).
205  */
206 typedef int (*vdso_sgx_enter_enclave_t)(unsigned long rdi, unsigned long rsi,
207 					unsigned long rdx, unsigned int function,
208 					unsigned long r8,  unsigned long r9,
209 					struct sgx_enclave_run *run);
210 
211 #endif /* _UAPI_ASM_X86_SGX_H */
212