xref: /optee_os/core/include/kernel/thread.h (revision ba2a6adb764f1310ad3c3091d89de84274f86b02)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * Copyright (c) 2016-2017, Linaro Limited
5  * Copyright (c) 2020-2021, Arm Limited
6  */
7 
8 #ifndef KERNEL_THREAD_H
9 #define KERNEL_THREAD_H
10 
11 #ifndef __ASSEMBLER__
12 #include <types_ext.h>
13 #include <compiler.h>
14 #include <mm/pgt_cache.h>
15 #endif
16 #include <util.h>
17 #include <kernel/thread_arch.h>
18 
19 #define THREAD_FLAGS_COPY_ARGS_ON_RETURN	BIT(0)
20 #define THREAD_FLAGS_FOREIGN_INTR_ENABLE	BIT(1)
21 #define THREAD_FLAGS_EXIT_ON_FOREIGN_INTR	BIT(2)
22 #define THREAD_FLAGS_FFA_ONLY			BIT(3)
23 
24 #define THREAD_ID_0		0
25 #define THREAD_ID_INVALID	-1
26 
27 #define THREAD_RPC_MAX_NUM_PARAMS	U(4)
28 
29 #ifndef __ASSEMBLER__
30 
31 struct thread_specific_data {
32 	TAILQ_HEAD(, ts_session) sess_stack;
33 	struct ts_ctx *ctx;
34 #ifdef CFG_CORE_FFA
35 	uint32_t rpc_target_info;
36 #endif
37 	uint32_t abort_type;
38 	uint32_t abort_descr;
39 	vaddr_t abort_va;
40 	unsigned int abort_core;
41 	struct thread_abort_regs abort_regs;
42 #ifdef CFG_CORE_DEBUG_CHECK_STACKS
43 	bool stackcheck_recursion;
44 #endif
45 	unsigned int syscall_recursion;
46 #ifdef CFG_FAULT_MITIGATION
47 	struct ftmn_func_arg *ftmn_arg;
48 #endif
49 };
50 
51 void thread_init_canaries(void);
52 void thread_init_primary(void);
53 void thread_init_per_cpu(void);
54 
55 struct thread_core_local *thread_get_core_local(void);
56 
57 /*
58  * Sets the stacks to be used by the different threads. Use THREAD_ID_0 for
59  * first stack, THREAD_ID_0 + 1 for the next and so on.
60  *
61  * Returns true on success and false on errors.
62  */
63 bool thread_init_stack(uint32_t stack_id, vaddr_t sp);
64 
65 /*
66  * Initializes thread contexts. Called in thread_init_boot_thread() if
67  * virtualization is disabled. Virtualization subsystem calls it for
68  * every new guest otherwise.
69  */
70 void thread_init_threads(void);
71 
72 /*
73  * Called by the init CPU. Sets temporary stack mode for all CPUs
74  * (curr_thread = -1 and THREAD_CLF_TMP) and sets the temporary stack limit for
75  * the init CPU.
76  */
77 void thread_init_thread_core_local(void);
78 void thread_init_core_local_stacks(void);
79 
80 #if defined(CFG_CORE_PAUTH)
81 void thread_init_thread_pauth_keys(void);
82 void thread_init_core_local_pauth_keys(void);
83 #else
84 static inline void thread_init_thread_pauth_keys(void) { }
85 static inline void thread_init_core_local_pauth_keys(void) { }
86 #endif
87 
88 /*
89  * Initializes a thread to be used during boot
90  */
91 void thread_init_boot_thread(void);
92 
93 /*
94  * Clears the current thread id
95  * Only supposed to be used during initialization.
96  */
97 void thread_clr_boot_thread(void);
98 
99 /*
100  * Returns current thread id.
101  */
102 short int thread_get_id(void);
103 
104 /*
105  * Returns current thread id, return -1 on failure.
106  */
107 short int thread_get_id_may_fail(void);
108 
109 /* Returns Thread Specific Data (TSD) pointer. */
110 struct thread_specific_data *thread_get_tsd(void);
111 
112 /*
113  * Sets foreign interrupts status for current thread, must only be called
114  * from an active thread context.
115  *
116  * enable == true  -> enable foreign interrupts
117  * enable == false -> disable foreign interrupts
118  */
119 void thread_set_foreign_intr(bool enable);
120 
121 /*
122  * Restores the foreign interrupts status (in CPSR) for current thread, must
123  * only be called from an active thread context.
124  */
125 void thread_restore_foreign_intr(void);
126 
127 /*
128  * thread_get_exceptions() - return current exception mask
129  */
130 uint32_t thread_get_exceptions(void);
131 
132 /*
133  * thread_set_exceptions() - set exception mask
134  * @exceptions: exception mask to set
135  *
136  * Any previous exception mask is replaced by this exception mask, that is,
137  * old bits are cleared and replaced by these.
138  */
139 void thread_set_exceptions(uint32_t exceptions);
140 
141 /*
142  * thread_mask_exceptions() - Masks (disables) specified asynchronous exceptions
143  * @exceptions	exceptions to mask
144  * @returns old exception state
145  */
146 uint32_t thread_mask_exceptions(uint32_t exceptions);
147 
148 /*
149  * thread_unmask_exceptions() - Unmasks asynchronous exceptions
150  * @state	Old asynchronous exception state to restore (returned by
151  *		thread_mask_exceptions())
152  */
153 void thread_unmask_exceptions(uint32_t state);
154 
155 
156 static inline bool __nostackcheck thread_foreign_intr_disabled(void)
157 {
158 	return !!(thread_get_exceptions() & THREAD_EXCP_FOREIGN_INTR);
159 }
160 
161 /*
162  * thread_enter_user_mode() - Enters user mode
163  * @a0:		Passed in r/x0 for user_func
164  * @a1:		Passed in r/x1 for user_func
165  * @a2:		Passed in r/x2 for user_func
166  * @a3:		Passed in r/x3 for user_func
167  * @user_sp:	Assigned sp value in user mode
168  * @user_func:	Function to execute in user mode
169  * @is_32bit:   True if TA should execute in Aarch32, false if Aarch64
170  * @exit_status0: Pointer to opaque exit staus 0
171  * @exit_status1: Pointer to opaque exit staus 1
172  *
173  * This functions enters user mode with the argument described above,
174  * @exit_status0 and @exit_status1 are filled in by thread_unwind_user_mode()
175  * when returning back to the caller of this function through an exception
176  * handler.
177  *
178  * @Returns what's passed in "ret" to thread_unwind_user_mode()
179  */
180 uint32_t thread_enter_user_mode(unsigned long a0, unsigned long a1,
181 		unsigned long a2, unsigned long a3, unsigned long user_sp,
182 		unsigned long entry_func, bool is_32bit,
183 		uint32_t *exit_status0, uint32_t *exit_status1);
184 
185 /*
186  * thread_unwind_user_mode() - Unwinds kernel stack from user entry
187  * @ret:	Value to return from thread_enter_user_mode()
188  * @exit_status0: Exit status 0
189  * @exit_status1: Exit status 1
190  *
191  * This is the function that exception handlers can return into
192  * to resume execution in kernel mode instead of user mode.
193  *
194  * This function is closely coupled with thread_enter_user_mode() since it
195  * need to restore registers saved by thread_enter_user_mode() and when it
196  * returns make it look like thread_enter_user_mode() just returned. It is
197  * expected that the stack pointer is where thread_enter_user_mode() left
198  * it. The stack will be unwound and the function will return to where
199  * thread_enter_user_mode() was called from.  Exit_status0 and exit_status1
200  * are filled in the corresponding pointers supplied to
201  * thread_enter_user_mode().
202  */
203 void thread_unwind_user_mode(uint32_t ret, uint32_t exit_status0,
204 		uint32_t exit_status1);
205 
206 /*
207  * Returns the start address (bottom) of the stack for the current thread,
208  * zero if there is no current thread.
209  */
210 vaddr_t thread_stack_start(void);
211 
212 
213 /* Returns the stack size for the current thread */
214 size_t thread_stack_size(void);
215 
216 /*
217  * Returns the start (top, lowest address) and end (bottom, highest address) of
218  * the current stack (thread, temporary or abort stack).
219  * When CFG_CORE_DEBUG_CHECK_STACKS=y, the @hard parameter tells if the hard or
220  * soft limits are queried. The difference between soft and hard is that for the
221  * latter, the stack start includes some additional space to let any function
222  * overflow the soft limit and still be able to print a stack dump in this case.
223  */
224 bool get_stack_limits(vaddr_t *start, vaddr_t *end, bool hard);
225 
226 static inline bool __nostackcheck get_stack_soft_limits(vaddr_t *start,
227 							vaddr_t *end)
228 {
229 	return get_stack_limits(start, end, false);
230 }
231 
232 static inline bool __nostackcheck get_stack_hard_limits(vaddr_t *start,
233 							vaddr_t *end)
234 {
235 	return get_stack_limits(start, end, true);
236 }
237 
238 bool thread_is_in_normal_mode(void);
239 
240 /*
241  * Returns true if previous exeception also was in abort mode.
242  *
243  * Note: it's only valid to call this function from an abort exception
244  * handler before interrupts has been re-enabled.
245  */
246 bool thread_is_from_abort_mode(void);
247 
248 /**
249  * Allocates data for payload buffers.
250  *
251  * @size:	size in bytes of payload buffer
252  *
253  * @returns	mobj that describes allocated buffer or NULL on error
254  */
255 struct mobj *thread_rpc_alloc_payload(size_t size);
256 
257 /**
258  * Free physical memory previously allocated with thread_rpc_alloc_payload()
259  *
260  * @mobj:	mobj that describes the buffer
261  */
262 void thread_rpc_free_payload(struct mobj *mobj);
263 
264 /**
265  * Allocate data for payload buffers only shared with the non-secure kernel
266  *
267  * @size:	size in bytes of payload buffer
268  *
269  * @returns	mobj that describes allocated buffer or NULL on error
270  */
271 struct mobj *thread_rpc_alloc_kernel_payload(size_t size);
272 
273 /**
274  * Free physical memory previously allocated with
275  * thread_rpc_alloc_kernel_payload()
276  *
277  * @mobj:	mobj that describes the buffer
278  */
279 void thread_rpc_free_kernel_payload(struct mobj *mobj);
280 
281 struct thread_param_memref {
282 	size_t offs;
283 	size_t size;
284 	struct mobj *mobj;
285 };
286 
287 struct thread_param_value {
288 	uint64_t a;
289 	uint64_t b;
290 	uint64_t c;
291 };
292 
293 /*
294  * Note that there's some arithmetics done on the value so it's important
295  * to keep in IN, OUT, INOUT order.
296  */
297 enum thread_param_attr {
298 	THREAD_PARAM_ATTR_NONE = 0,
299 	THREAD_PARAM_ATTR_VALUE_IN,
300 	THREAD_PARAM_ATTR_VALUE_OUT,
301 	THREAD_PARAM_ATTR_VALUE_INOUT,
302 	THREAD_PARAM_ATTR_MEMREF_IN,
303 	THREAD_PARAM_ATTR_MEMREF_OUT,
304 	THREAD_PARAM_ATTR_MEMREF_INOUT,
305 };
306 
307 struct thread_param {
308 	enum thread_param_attr attr;
309 	union {
310 		struct thread_param_memref memref;
311 		struct thread_param_value value;
312 	} u;
313 };
314 
315 #define THREAD_PARAM_MEMREF(_direction, _mobj, _offs, _size) \
316 	(struct thread_param){ \
317 		.attr = THREAD_PARAM_ATTR_MEMREF_ ## _direction, .u.memref = { \
318 		.mobj = (_mobj), .offs = (_offs), .size = (_size) } \
319 	}
320 
321 #define THREAD_PARAM_VALUE(_direction, _a, _b, _c) \
322 	(struct thread_param){ \
323 		.attr = THREAD_PARAM_ATTR_VALUE_ ## _direction, .u.value = { \
324 		.a = (_a), .b = (_b), .c = (_c) } \
325 	}
326 
327 /**
328  * Does an RPC using a preallocated argument buffer
329  * @cmd: RPC cmd
330  * @num_params: number of parameters
331  * @params: RPC parameters
332  * @returns RPC return value
333  */
334 uint32_t thread_rpc_cmd(uint32_t cmd, size_t num_params,
335 		struct thread_param *params);
336 
337 /**
338  * Allocate data for payload buffers.
339  * Buffer is exported to user mode applications.
340  *
341  * @size:	size in bytes of payload buffer
342  *
343  * @returns	mobj that describes allocated buffer or NULL on error
344  */
345 struct mobj *thread_rpc_alloc_global_payload(size_t size);
346 
347 /**
348  * Free physical memory previously allocated with
349  * thread_rpc_alloc_global_payload()
350  *
351  * @mobj:	mobj that describes the buffer
352  */
353 void thread_rpc_free_global_payload(struct mobj *mobj);
354 
355 /*
356  * enum thread_shm_type - type of non-secure shared memory
357  * @THREAD_SHM_TYPE_APPLICATION - user space application shared memory
358  * @THREAD_SHM_TYPE_KERNEL_PRIVATE - kernel private shared memory
359  * @THREAD_SHM_TYPE_GLOBAL - user space and kernel shared memory
360  */
361 enum thread_shm_type {
362 	THREAD_SHM_TYPE_APPLICATION,
363 	THREAD_SHM_TYPE_KERNEL_PRIVATE,
364 	THREAD_SHM_TYPE_GLOBAL,
365 };
366 
367 /*
368  * enum thread_shm_cache_user - user of a cache allocation
369  * @THREAD_SHM_CACHE_USER_SOCKET - socket communication
370  * @THREAD_SHM_CACHE_USER_FS - filesystem access
371  * @THREAD_SHM_CACHE_USER_I2C - I2C communication
372  *
373  * To ensure that each user of the shared memory cache doesn't interfere
374  * with each other a unique ID per user is used.
375  */
376 enum thread_shm_cache_user {
377 	THREAD_SHM_CACHE_USER_SOCKET,
378 	THREAD_SHM_CACHE_USER_FS,
379 	THREAD_SHM_CACHE_USER_I2C,
380 };
381 
382 /*
383  * Returns a pointer to the cached RPC memory. Each thread and @user tuple
384  * has a unique cache. The pointer is guaranteed to point to a large enough
385  * area or to be NULL.
386  */
387 void *thread_rpc_shm_cache_alloc(enum thread_shm_cache_user user,
388 				 enum thread_shm_type shm_type,
389 				 size_t size, struct mobj **mobj);
390 
391 #endif /*__ASSEMBLER__*/
392 
393 #endif /*KERNEL_THREAD_H*/
394