1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/bitops.h>
8 #include <linux/rwsem.h>
9 #include <linux/types.h>
11 #include <asm/traps.h>
14 /* Retrieve the encoded trapnr from the specified return code. */
15 #define ENCLS_TRAPNR(r) ((r) & ~SGX_ENCLS_FAULT_FLAG)
17 /* Issue a WARN() about an ENCLS function. */
18 #define ENCLS_WARN(r, name) { \
21 WARN_ONCE(_r, "%s returned %d (0x%x)\n", (name), _r, _r); \
26 * encls_faulted() - Check if an ENCLS leaf faulted given an error code
27 * @ret: the return value of an ENCLS leaf function call
30 * - true: ENCLS leaf faulted.
33 static inline bool encls_faulted(int ret
)
35 return ret
& SGX_ENCLS_FAULT_FLAG
;
39 * encls_failed() - Check if an ENCLS function failed
40 * @ret: the return value of an ENCLS function call
42 * Check if an ENCLS function failed. This happens when the function causes a
43 * fault that is not caused by an EPCM conflict or when the function returns a
46 static inline bool encls_failed(int ret
)
48 if (encls_faulted(ret
))
49 return ENCLS_TRAPNR(ret
) != X86_TRAP_PF
;
55 * __encls_ret_N - encode an ENCLS function that returns an error code in EAX
56 * @rax: function number
57 * @inputs: asm inputs for the function
59 * Emit assembly for an ENCLS function that returns an error code, e.g. EREMOVE.
60 * And because SGX isn't complex enough as it is, function that return an error
61 * code also modify flags.
65 * SGX error code on failure
67 #define __encls_ret_N(rax, inputs...) \
71 "1: .byte 0x0f, 0x01, 0xcf;\n\t" \
73 _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_FAULT_SGX) \
80 #define __encls_ret_1(rax, rcx) \
82 __encls_ret_N(rax, "c"(rcx)); \
85 #define __encls_ret_2(rax, rbx, rcx) \
87 __encls_ret_N(rax, "b"(rbx), "c"(rcx)); \
90 #define __encls_ret_3(rax, rbx, rcx, rdx) \
92 __encls_ret_N(rax, "b"(rbx), "c"(rcx), "d"(rdx)); \
96 * __encls_N - encode an ENCLS function that doesn't return an error code
97 * @rax: function number
98 * @rbx_out: optional output variable
99 * @inputs: asm inputs for the function
101 * Emit assembly for an ENCLS function that does not return an error code, e.g.
102 * ECREATE. Leaves without error codes either succeed or fault. @rbx_out is an
103 * optional parameter for use by EDGBRD, which returns the requested value in
108 * trapnr with SGX_ENCLS_FAULT_FLAG set on fault
110 #define __encls_N(rax, rbx_out, inputs...) \
114 "1: .byte 0x0f, 0x01, 0xcf;\n\t" \
115 " xor %%eax,%%eax;\n" \
117 _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_FAULT_SGX) \
118 : "=a"(ret), "=b"(rbx_out) \
124 #define __encls_2(rax, rbx, rcx) \
126 unsigned long ign_rbx_out; \
127 __encls_N(rax, ign_rbx_out, "b"(rbx), "c"(rcx)); \
130 #define __encls_1_1(rax, data, rcx) \
132 unsigned long rbx_out; \
133 int ret = __encls_N(rax, rbx_out, "c"(rcx)); \
139 /* Initialize an EPC page into an SGX Enclave Control Structure (SECS) page. */
140 static inline int __ecreate(struct sgx_pageinfo
*pginfo
, void *secs
)
142 return __encls_2(ECREATE
, pginfo
, secs
);
145 /* Hash a 256 byte region of an enclave page to SECS:MRENCLAVE. */
146 static inline int __eextend(void *secs
, void *addr
)
148 return __encls_2(EEXTEND
, secs
, addr
);
152 * Associate an EPC page to an enclave either as a REG or TCS page
153 * populated with the provided data.
155 static inline int __eadd(struct sgx_pageinfo
*pginfo
, void *addr
)
157 return __encls_2(EADD
, pginfo
, addr
);
160 /* Finalize enclave build, initialize enclave for user code execution. */
161 static inline int __einit(void *sigstruct
, void *token
, void *secs
)
163 return __encls_ret_3(EINIT
, sigstruct
, secs
, token
);
166 /* Disassociate EPC page from its enclave and mark it as unused. */
167 static inline int __eremove(void *addr
)
169 return __encls_ret_1(EREMOVE
, addr
);
172 /* Copy data to an EPC page belonging to a debug enclave. */
173 static inline int __edbgwr(void *addr
, unsigned long *data
)
175 return __encls_2(EDGBWR
, *data
, addr
);
178 /* Copy data from an EPC page belonging to a debug enclave. */
179 static inline int __edbgrd(void *addr
, unsigned long *data
)
181 return __encls_1_1(EDGBRD
, *data
, addr
);
184 /* Track that software has completed the required TLB address clears. */
185 static inline int __etrack(void *addr
)
187 return __encls_ret_1(ETRACK
, addr
);
190 /* Load, verify, and unblock an EPC page. */
191 static inline int __eldu(struct sgx_pageinfo
*pginfo
, void *addr
,
194 return __encls_ret_3(ELDU
, pginfo
, addr
, va
);
197 /* Make EPC page inaccessible to enclave, ready to be written to memory. */
198 static inline int __eblock(void *addr
)
200 return __encls_ret_1(EBLOCK
, addr
);
203 /* Initialize an EPC page into a Version Array (VA) page. */
204 static inline int __epa(void *addr
)
206 unsigned long rbx
= SGX_PAGE_TYPE_VA
;
208 return __encls_2(EPA
, rbx
, addr
);
211 /* Invalidate an EPC page and write it out to main memory. */
212 static inline int __ewb(struct sgx_pageinfo
*pginfo
, void *addr
,
215 return __encls_ret_3(EWB
, pginfo
, addr
, va
);
218 /* Restrict the EPCM permissions of an EPC page. */
219 static inline int __emodpr(struct sgx_secinfo
*secinfo
, void *addr
)
221 return __encls_ret_2(EMODPR
, secinfo
, addr
);
224 /* Change the type of an EPC page. */
225 static inline int __emodt(struct sgx_secinfo
*secinfo
, void *addr
)
227 return __encls_ret_2(EMODT
, secinfo
, addr
);
230 /* Zero a page of EPC memory and add it to an initialized enclave. */
231 static inline int __eaug(struct sgx_pageinfo
*pginfo
, void *addr
)
233 return __encls_2(EAUG
, pginfo
, addr
);
236 #endif /* _X86_ENCLS_H */