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