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