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