xref: /optee_os/core/arch/arm/kernel/thread.c (revision c2ce4186edb0412c8a0069ff4ee2eb2ec66d09fe)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2016, Linaro Limited
4  * Copyright (c) 2014, STMicroelectronics International N.V.
5  */
6 
7 #include <platform_config.h>
8 
9 #include <arm.h>
10 #include <assert.h>
11 #include <keep.h>
12 #include <kernel/asan.h>
13 #include <kernel/lockdep.h>
14 #include <kernel/misc.h>
15 #include <kernel/msg_param.h>
16 #include <kernel/panic.h>
17 #include <kernel/spinlock.h>
18 #include <kernel/tee_ta_manager.h>
19 #include <kernel/thread_defs.h>
20 #include <kernel/thread.h>
21 #include <kernel/virtualization.h>
22 #include <mm/core_memprot.h>
23 #include <mm/mobj.h>
24 #include <mm/tee_mm.h>
25 #include <mm/tee_mmu.h>
26 #include <mm/tee_pager.h>
27 #include <optee_msg.h>
28 #include <optee_rpc_cmd.h>
29 #include <smccc.h>
30 #include <sm/optee_smc.h>
31 #include <sm/sm.h>
32 #include <tee/tee_cryp_utl.h>
33 #include <tee/tee_fs_rpc.h>
34 #include <trace.h>
35 #include <util.h>
36 
37 #include "thread_private.h"
38 
39 #ifdef CFG_WITH_ARM_TRUSTED_FW
40 #define STACK_TMP_OFFS		0
41 #else
42 #define STACK_TMP_OFFS		SM_STACK_TMP_RESERVE_SIZE
43 #endif
44 
45 
46 #ifdef ARM32
47 #ifdef CFG_CORE_SANITIZE_KADDRESS
48 #define STACK_TMP_SIZE		(3072 + STACK_TMP_OFFS)
49 #else
50 #define STACK_TMP_SIZE		(1536 + STACK_TMP_OFFS)
51 #endif
52 #define STACK_THREAD_SIZE	8192
53 
54 #ifdef CFG_CORE_SANITIZE_KADDRESS
55 #define STACK_ABT_SIZE		3072
56 #else
57 #define STACK_ABT_SIZE		2048
58 #endif
59 
60 #endif /*ARM32*/
61 
62 #ifdef ARM64
63 #define STACK_TMP_SIZE		(2048 + STACK_TMP_OFFS)
64 #define STACK_THREAD_SIZE	8192
65 
66 #if TRACE_LEVEL > 0
67 #define STACK_ABT_SIZE		3072
68 #else
69 #define STACK_ABT_SIZE		1024
70 #endif
71 #endif /*ARM64*/
72 
73 struct thread_ctx threads[CFG_NUM_THREADS];
74 
75 struct thread_core_local thread_core_local[CFG_TEE_CORE_NB_CORE] __nex_bss;
76 
77 #ifdef CFG_WITH_STACK_CANARIES
78 #ifdef ARM32
79 #define STACK_CANARY_SIZE	(4 * sizeof(uint32_t))
80 #endif
81 #ifdef ARM64
82 #define STACK_CANARY_SIZE	(8 * sizeof(uint32_t))
83 #endif
84 #define START_CANARY_VALUE	0xdededede
85 #define END_CANARY_VALUE	0xabababab
86 #define GET_START_CANARY(name, stack_num) name[stack_num][0]
87 #define GET_END_CANARY(name, stack_num) \
88 	name[stack_num][sizeof(name[stack_num]) / sizeof(uint32_t) - 1]
89 #else
90 #define STACK_CANARY_SIZE	0
91 #endif
92 
93 #define DECLARE_STACK(name, num_stacks, stack_size, linkage) \
94 linkage uint32_t name[num_stacks] \
95 		[ROUNDUP(stack_size + STACK_CANARY_SIZE, STACK_ALIGNMENT) / \
96 		sizeof(uint32_t)] \
97 		__attribute__((section(".nozi_stack." # name), \
98 			       aligned(STACK_ALIGNMENT)))
99 
100 #define STACK_SIZE(stack) (sizeof(stack) - STACK_CANARY_SIZE / 2)
101 
102 #define GET_STACK(stack) \
103 	((vaddr_t)(stack) + STACK_SIZE(stack))
104 
105 DECLARE_STACK(stack_tmp, CFG_TEE_CORE_NB_CORE, STACK_TMP_SIZE, static);
106 DECLARE_STACK(stack_abt, CFG_TEE_CORE_NB_CORE, STACK_ABT_SIZE, static);
107 #ifndef CFG_WITH_PAGER
108 DECLARE_STACK(stack_thread, CFG_NUM_THREADS, STACK_THREAD_SIZE, static);
109 #endif
110 
111 const void *stack_tmp_export = (uint8_t *)stack_tmp + sizeof(stack_tmp[0]) -
112 			       (STACK_TMP_OFFS + STACK_CANARY_SIZE / 2);
113 const uint32_t stack_tmp_stride = sizeof(stack_tmp[0]);
114 
115 /*
116  * These stack setup info are required by secondary boot cores before they
117  * each locally enable the pager (the mmu). Hence kept in pager sections.
118  */
119 KEEP_PAGER(stack_tmp_export);
120 KEEP_PAGER(stack_tmp_stride);
121 
122 thread_smc_handler_t thread_std_smc_handler_ptr __nex_bss;
123 static thread_smc_handler_t thread_fast_smc_handler_ptr __nex_bss;
124 thread_nintr_handler_t thread_nintr_handler_ptr __nex_bss;
125 thread_pm_handler_t thread_cpu_on_handler_ptr __nex_bss;
126 thread_pm_handler_t thread_cpu_off_handler_ptr __nex_bss;
127 thread_pm_handler_t thread_cpu_suspend_handler_ptr __nex_bss;
128 thread_pm_handler_t thread_cpu_resume_handler_ptr __nex_bss;
129 thread_pm_handler_t thread_system_off_handler_ptr __nex_bss;
130 thread_pm_handler_t thread_system_reset_handler_ptr __nex_bss;
131 
132 #ifdef CFG_CORE_UNMAP_CORE_AT_EL0
133 static vaddr_t thread_user_kcode_va __nex_bss;
134 long thread_user_kcode_offset __nex_bss;
135 static size_t thread_user_kcode_size __nex_bss;
136 #endif
137 
138 #if defined(CFG_CORE_UNMAP_CORE_AT_EL0) && \
139 	defined(CFG_CORE_WORKAROUND_SPECTRE_BP_SEC) && defined(ARM64)
140 long thread_user_kdata_sp_offset __nex_bss;
141 static uint8_t thread_user_kdata_page[
142 	ROUNDUP(sizeof(thread_core_local), SMALL_PAGE_SIZE)]
143 	__aligned(SMALL_PAGE_SIZE)
144 #ifndef CFG_VIRTUALIZATION
145 	__section(".nozi.kdata_page");
146 #else
147 	__section(".nex_nozi.kdata_page");
148 #endif
149 #endif
150 
151 static unsigned int thread_global_lock __nex_bss = SPINLOCK_UNLOCK;
152 static bool thread_prealloc_rpc_cache;
153 
154 static unsigned int thread_rpc_pnum;
155 
156 static void init_canaries(void)
157 {
158 #ifdef CFG_WITH_STACK_CANARIES
159 	size_t n;
160 #define INIT_CANARY(name)						\
161 	for (n = 0; n < ARRAY_SIZE(name); n++) {			\
162 		uint32_t *start_canary = &GET_START_CANARY(name, n);	\
163 		uint32_t *end_canary = &GET_END_CANARY(name, n);	\
164 									\
165 		*start_canary = START_CANARY_VALUE;			\
166 		*end_canary = END_CANARY_VALUE;				\
167 		DMSG("#Stack canaries for %s[%zu] with top at %p",	\
168 			#name, n, (void *)(end_canary - 1));		\
169 		DMSG("watch *%p", (void *)end_canary);			\
170 	}
171 
172 	INIT_CANARY(stack_tmp);
173 	INIT_CANARY(stack_abt);
174 #if !defined(CFG_WITH_PAGER) && !defined(CFG_VIRTUALIZATION)
175 	INIT_CANARY(stack_thread);
176 #endif
177 #endif/*CFG_WITH_STACK_CANARIES*/
178 }
179 
180 #define CANARY_DIED(stack, loc, n) \
181 	do { \
182 		EMSG_RAW("Dead canary at %s of '%s[%zu]'", #loc, #stack, n); \
183 		panic(); \
184 	} while (0)
185 
186 void thread_check_canaries(void)
187 {
188 #ifdef CFG_WITH_STACK_CANARIES
189 	size_t n;
190 
191 	for (n = 0; n < ARRAY_SIZE(stack_tmp); n++) {
192 		if (GET_START_CANARY(stack_tmp, n) != START_CANARY_VALUE)
193 			CANARY_DIED(stack_tmp, start, n);
194 		if (GET_END_CANARY(stack_tmp, n) != END_CANARY_VALUE)
195 			CANARY_DIED(stack_tmp, end, n);
196 	}
197 
198 	for (n = 0; n < ARRAY_SIZE(stack_abt); n++) {
199 		if (GET_START_CANARY(stack_abt, n) != START_CANARY_VALUE)
200 			CANARY_DIED(stack_abt, start, n);
201 		if (GET_END_CANARY(stack_abt, n) != END_CANARY_VALUE)
202 			CANARY_DIED(stack_abt, end, n);
203 
204 	}
205 #if !defined(CFG_WITH_PAGER) && !defined(CFG_VIRTUALIZATION)
206 	for (n = 0; n < ARRAY_SIZE(stack_thread); n++) {
207 		if (GET_START_CANARY(stack_thread, n) != START_CANARY_VALUE)
208 			CANARY_DIED(stack_thread, start, n);
209 		if (GET_END_CANARY(stack_thread, n) != END_CANARY_VALUE)
210 			CANARY_DIED(stack_thread, end, n);
211 	}
212 #endif
213 #endif/*CFG_WITH_STACK_CANARIES*/
214 }
215 
216 static void lock_global(void)
217 {
218 	cpu_spin_lock(&thread_global_lock);
219 }
220 
221 static void unlock_global(void)
222 {
223 	cpu_spin_unlock(&thread_global_lock);
224 }
225 
226 #ifdef ARM32
227 uint32_t thread_get_exceptions(void)
228 {
229 	uint32_t cpsr = read_cpsr();
230 
231 	return (cpsr >> CPSR_F_SHIFT) & THREAD_EXCP_ALL;
232 }
233 
234 void thread_set_exceptions(uint32_t exceptions)
235 {
236 	uint32_t cpsr = read_cpsr();
237 
238 	/* Foreign interrupts must not be unmasked while holding a spinlock */
239 	if (!(exceptions & THREAD_EXCP_FOREIGN_INTR))
240 		assert_have_no_spinlock();
241 
242 	cpsr &= ~(THREAD_EXCP_ALL << CPSR_F_SHIFT);
243 	cpsr |= ((exceptions & THREAD_EXCP_ALL) << CPSR_F_SHIFT);
244 	write_cpsr(cpsr);
245 }
246 #endif /*ARM32*/
247 
248 #ifdef ARM64
249 uint32_t thread_get_exceptions(void)
250 {
251 	uint32_t daif = read_daif();
252 
253 	return (daif >> DAIF_F_SHIFT) & THREAD_EXCP_ALL;
254 }
255 
256 void thread_set_exceptions(uint32_t exceptions)
257 {
258 	uint32_t daif = read_daif();
259 
260 	/* Foreign interrupts must not be unmasked while holding a spinlock */
261 	if (!(exceptions & THREAD_EXCP_FOREIGN_INTR))
262 		assert_have_no_spinlock();
263 
264 	daif &= ~(THREAD_EXCP_ALL << DAIF_F_SHIFT);
265 	daif |= ((exceptions & THREAD_EXCP_ALL) << DAIF_F_SHIFT);
266 	write_daif(daif);
267 }
268 #endif /*ARM64*/
269 
270 uint32_t thread_mask_exceptions(uint32_t exceptions)
271 {
272 	uint32_t state = thread_get_exceptions();
273 
274 	thread_set_exceptions(state | (exceptions & THREAD_EXCP_ALL));
275 	return state;
276 }
277 
278 void thread_unmask_exceptions(uint32_t state)
279 {
280 	thread_set_exceptions(state & THREAD_EXCP_ALL);
281 }
282 
283 
284 struct thread_core_local *thread_get_core_local(void)
285 {
286 	uint32_t cpu_id = get_core_pos();
287 
288 	/*
289 	 * Foreign interrupts must be disabled before playing with core_local
290 	 * since we otherwise may be rescheduled to a different core in the
291 	 * middle of this function.
292 	 */
293 	assert(thread_get_exceptions() & THREAD_EXCP_FOREIGN_INTR);
294 
295 	assert(cpu_id < CFG_TEE_CORE_NB_CORE);
296 	return &thread_core_local[cpu_id];
297 }
298 
299 static void thread_lazy_save_ns_vfp(void)
300 {
301 #ifdef CFG_WITH_VFP
302 	struct thread_ctx *thr = threads + thread_get_id();
303 
304 	thr->vfp_state.ns_saved = false;
305 	vfp_lazy_save_state_init(&thr->vfp_state.ns);
306 #endif /*CFG_WITH_VFP*/
307 }
308 
309 static void thread_lazy_restore_ns_vfp(void)
310 {
311 #ifdef CFG_WITH_VFP
312 	struct thread_ctx *thr = threads + thread_get_id();
313 	struct thread_user_vfp_state *tuv = thr->vfp_state.uvfp;
314 
315 	assert(!thr->vfp_state.sec_lazy_saved && !thr->vfp_state.sec_saved);
316 
317 	if (tuv && tuv->lazy_saved && !tuv->saved) {
318 		vfp_lazy_save_state_final(&tuv->vfp, false /*!force_save*/);
319 		tuv->saved = true;
320 	}
321 
322 	vfp_lazy_restore_state(&thr->vfp_state.ns, thr->vfp_state.ns_saved);
323 	thr->vfp_state.ns_saved = false;
324 #endif /*CFG_WITH_VFP*/
325 }
326 
327 #ifdef ARM32
328 static void init_regs(struct thread_ctx *thread,
329 		struct thread_smc_args *args)
330 {
331 	thread->regs.pc = (uint32_t)thread_std_smc_entry;
332 
333 	/*
334 	 * Stdcalls starts in SVC mode with masked foreign interrupts, masked
335 	 * Asynchronous abort and unmasked native interrupts.
336 	 */
337 	thread->regs.cpsr = read_cpsr() & ARM32_CPSR_E;
338 	thread->regs.cpsr |= CPSR_MODE_SVC | CPSR_A |
339 			(THREAD_EXCP_FOREIGN_INTR << ARM32_CPSR_F_SHIFT);
340 	/* Enable thumb mode if it's a thumb instruction */
341 	if (thread->regs.pc & 1)
342 		thread->regs.cpsr |= CPSR_T;
343 	/* Reinitialize stack pointer */
344 	thread->regs.svc_sp = thread->stack_va_end;
345 
346 	/*
347 	 * Copy arguments into context. This will make the
348 	 * arguments appear in r0-r7 when thread is started.
349 	 */
350 	thread->regs.r0 = args->a0;
351 	thread->regs.r1 = args->a1;
352 	thread->regs.r2 = args->a2;
353 	thread->regs.r3 = args->a3;
354 	thread->regs.r4 = args->a4;
355 	thread->regs.r5 = args->a5;
356 	thread->regs.r6 = args->a6;
357 	thread->regs.r7 = args->a7;
358 }
359 #endif /*ARM32*/
360 
361 #ifdef ARM64
362 static void init_regs(struct thread_ctx *thread,
363 		struct thread_smc_args *args)
364 {
365 	thread->regs.pc = (uint64_t)thread_std_smc_entry;
366 
367 	/*
368 	 * Stdcalls starts in SVC mode with masked foreign interrupts, masked
369 	 * Asynchronous abort and unmasked native interrupts.
370 	 */
371 	thread->regs.cpsr = SPSR_64(SPSR_64_MODE_EL1, SPSR_64_MODE_SP_EL0,
372 				THREAD_EXCP_FOREIGN_INTR | DAIFBIT_ABT);
373 	/* Reinitialize stack pointer */
374 	thread->regs.sp = thread->stack_va_end;
375 
376 	/*
377 	 * Copy arguments into context. This will make the
378 	 * arguments appear in x0-x7 when thread is started.
379 	 */
380 	thread->regs.x[0] = args->a0;
381 	thread->regs.x[1] = args->a1;
382 	thread->regs.x[2] = args->a2;
383 	thread->regs.x[3] = args->a3;
384 	thread->regs.x[4] = args->a4;
385 	thread->regs.x[5] = args->a5;
386 	thread->regs.x[6] = args->a6;
387 	thread->regs.x[7] = args->a7;
388 
389 	/* Set up frame pointer as per the Aarch64 AAPCS */
390 	thread->regs.x[29] = 0;
391 }
392 #endif /*ARM64*/
393 
394 void thread_init_boot_thread(void)
395 {
396 	struct thread_core_local *l = thread_get_core_local();
397 
398 	thread_init_threads();
399 
400 	l->curr_thread = 0;
401 	threads[0].state = THREAD_STATE_ACTIVE;
402 }
403 
404 void thread_clr_boot_thread(void)
405 {
406 	struct thread_core_local *l = thread_get_core_local();
407 
408 	assert(l->curr_thread >= 0 && l->curr_thread < CFG_NUM_THREADS);
409 	assert(threads[l->curr_thread].state == THREAD_STATE_ACTIVE);
410 	threads[l->curr_thread].state = THREAD_STATE_FREE;
411 	l->curr_thread = -1;
412 }
413 
414 static void thread_alloc_and_run(struct thread_smc_args *args)
415 {
416 	size_t n;
417 	struct thread_core_local *l = thread_get_core_local();
418 	bool found_thread = false;
419 
420 	assert(l->curr_thread == -1);
421 
422 	lock_global();
423 
424 	for (n = 0; n < CFG_NUM_THREADS; n++) {
425 		if (threads[n].state == THREAD_STATE_FREE) {
426 			threads[n].state = THREAD_STATE_ACTIVE;
427 			found_thread = true;
428 			break;
429 		}
430 	}
431 
432 	unlock_global();
433 
434 	if (!found_thread) {
435 		args->a0 = OPTEE_SMC_RETURN_ETHREAD_LIMIT;
436 		return;
437 	}
438 
439 	l->curr_thread = n;
440 
441 	threads[n].flags = 0;
442 	init_regs(threads + n, args);
443 
444 	/* Save Hypervisor Client ID */
445 	threads[n].hyp_clnt_id = args->a7;
446 
447 	thread_lazy_save_ns_vfp();
448 	thread_resume(&threads[n].regs);
449 }
450 
451 #ifdef ARM32
452 static void copy_a0_to_a5(struct thread_ctx_regs *regs,
453 		struct thread_smc_args *args)
454 {
455 	/*
456 	 * Update returned values from RPC, values will appear in
457 	 * r0-r3 when thread is resumed.
458 	 */
459 	regs->r0 = args->a0;
460 	regs->r1 = args->a1;
461 	regs->r2 = args->a2;
462 	regs->r3 = args->a3;
463 	regs->r4 = args->a4;
464 	regs->r5 = args->a5;
465 }
466 #endif /*ARM32*/
467 
468 #ifdef ARM64
469 static void copy_a0_to_a5(struct thread_ctx_regs *regs,
470 		struct thread_smc_args *args)
471 {
472 	/*
473 	 * Update returned values from RPC, values will appear in
474 	 * x0-x3 when thread is resumed.
475 	 */
476 	regs->x[0] = args->a0;
477 	regs->x[1] = args->a1;
478 	regs->x[2] = args->a2;
479 	regs->x[3] = args->a3;
480 	regs->x[4] = args->a4;
481 	regs->x[5] = args->a5;
482 }
483 #endif /*ARM64*/
484 
485 #ifdef ARM32
486 static bool is_from_user(uint32_t cpsr)
487 {
488 	return (cpsr & ARM32_CPSR_MODE_MASK) == ARM32_CPSR_MODE_USR;
489 }
490 #endif
491 
492 #ifdef ARM64
493 static bool is_from_user(uint32_t cpsr)
494 {
495 	if (cpsr & (SPSR_MODE_RW_32 << SPSR_MODE_RW_SHIFT))
496 		return true;
497 	if (((cpsr >> SPSR_64_MODE_EL_SHIFT) & SPSR_64_MODE_EL_MASK) ==
498 	     SPSR_64_MODE_EL0)
499 		return true;
500 	return false;
501 }
502 #endif
503 
504 static bool is_user_mode(struct thread_ctx_regs *regs)
505 {
506 	return is_from_user((uint32_t)regs->cpsr);
507 }
508 
509 static void thread_resume_from_rpc(struct thread_smc_args *args)
510 {
511 	size_t n = args->a3; /* thread id */
512 	struct thread_core_local *l = thread_get_core_local();
513 	uint32_t rv = 0;
514 
515 	assert(l->curr_thread == -1);
516 
517 	lock_global();
518 
519 	if (n < CFG_NUM_THREADS &&
520 	    threads[n].state == THREAD_STATE_SUSPENDED &&
521 	    args->a7 == threads[n].hyp_clnt_id)
522 		threads[n].state = THREAD_STATE_ACTIVE;
523 	else
524 		rv = OPTEE_SMC_RETURN_ERESUME;
525 
526 	unlock_global();
527 
528 	if (rv) {
529 		args->a0 = rv;
530 		return;
531 	}
532 
533 	l->curr_thread = n;
534 
535 	if (is_user_mode(&threads[n].regs))
536 		tee_ta_update_session_utime_resume();
537 
538 	if (threads[n].have_user_map)
539 		core_mmu_set_user_map(&threads[n].user_map);
540 
541 	/*
542 	 * Return from RPC to request service of a foreign interrupt must not
543 	 * get parameters from non-secure world.
544 	 */
545 	if (threads[n].flags & THREAD_FLAGS_COPY_ARGS_ON_RETURN) {
546 		copy_a0_to_a5(&threads[n].regs, args);
547 		threads[n].flags &= ~THREAD_FLAGS_COPY_ARGS_ON_RETURN;
548 	}
549 
550 	thread_lazy_save_ns_vfp();
551 	thread_resume(&threads[n].regs);
552 }
553 
554 void thread_handle_fast_smc(struct thread_smc_args *args)
555 {
556 	thread_check_canaries();
557 
558 #ifdef CFG_VIRTUALIZATION
559 	if (!virt_set_guest(args->a7)) {
560 		args->a0 = OPTEE_SMC_RETURN_ENOTAVAIL;
561 		goto out;
562 	}
563 #endif
564 
565 	thread_fast_smc_handler_ptr(args);
566 
567 #ifdef CFG_VIRTUALIZATION
568 	virt_unset_guest();
569 #endif
570 	/* Fast handlers must not unmask any exceptions */
571 out:
572 	__maybe_unused;
573 	assert(thread_get_exceptions() == THREAD_EXCP_ALL);
574 }
575 
576 void thread_handle_std_smc(struct thread_smc_args *args)
577 {
578 	thread_check_canaries();
579 
580 #ifdef CFG_VIRTUALIZATION
581 	if (!virt_set_guest(args->a7)) {
582 		args->a0 = OPTEE_SMC_RETURN_ENOTAVAIL;
583 		return;
584 	}
585 #endif
586 
587 	if (args->a0 == OPTEE_SMC_CALL_RETURN_FROM_RPC)
588 		thread_resume_from_rpc(args);
589 	else
590 		thread_alloc_and_run(args);
591 
592 #ifdef CFG_VIRTUALIZATION
593 	virt_unset_guest();
594 #endif
595 
596 }
597 
598 /**
599  * Free physical memory previously allocated with thread_rpc_alloc_arg()
600  *
601  * @cookie:	cookie received when allocating the buffer
602  */
603 static void thread_rpc_free_arg(uint64_t cookie)
604 {
605 	if (cookie) {
606 		uint32_t rpc_args[THREAD_RPC_NUM_ARGS] = {
607 			OPTEE_SMC_RETURN_RPC_FREE
608 		};
609 
610 		reg_pair_from_64(cookie, rpc_args + 1, rpc_args + 2);
611 		thread_rpc(rpc_args);
612 	}
613 }
614 
615 /*
616  * Helper routine for the assembly function thread_std_smc_entry()
617  *
618  * Note: this function is weak just to make it possible to exclude it from
619  * the unpaged area.
620  */
621 void __weak __thread_std_smc_entry(struct thread_smc_args *args)
622 {
623 #ifdef CFG_VIRTUALIZATION
624 	virt_on_stdcall();
625 #endif
626 	thread_std_smc_handler_ptr(args);
627 
628 	if (args->a0 == OPTEE_SMC_RETURN_OK) {
629 		struct thread_ctx *thr = threads + thread_get_id();
630 
631 		tee_fs_rpc_cache_clear(&thr->tsd);
632 		if (!thread_prealloc_rpc_cache) {
633 			thread_rpc_free_arg(mobj_get_cookie(thr->rpc_mobj));
634 			mobj_free(thr->rpc_mobj);
635 			thr->rpc_arg = 0;
636 			thr->rpc_mobj = NULL;
637 		}
638 	}
639 }
640 
641 void *thread_get_tmp_sp(void)
642 {
643 	struct thread_core_local *l = thread_get_core_local();
644 
645 	return (void *)l->tmp_stack_va_end;
646 }
647 
648 #ifdef ARM64
649 vaddr_t thread_get_saved_thread_sp(void)
650 {
651 	struct thread_core_local *l = thread_get_core_local();
652 	int ct = l->curr_thread;
653 
654 	assert(ct != -1);
655 	return threads[ct].kern_sp;
656 }
657 #endif /*ARM64*/
658 
659 vaddr_t thread_stack_start(void)
660 {
661 	struct thread_ctx *thr;
662 	int ct = thread_get_id_may_fail();
663 
664 	if (ct == -1)
665 		return 0;
666 
667 	thr = threads + ct;
668 	return thr->stack_va_end - STACK_THREAD_SIZE;
669 }
670 
671 size_t thread_stack_size(void)
672 {
673 	return STACK_THREAD_SIZE;
674 }
675 
676 bool thread_is_from_abort_mode(void)
677 {
678 	struct thread_core_local *l = thread_get_core_local();
679 
680 	return (l->flags >> THREAD_CLF_SAVED_SHIFT) & THREAD_CLF_ABORT;
681 }
682 
683 #ifdef ARM32
684 bool thread_is_in_normal_mode(void)
685 {
686 	return (read_cpsr() & ARM32_CPSR_MODE_MASK) == ARM32_CPSR_MODE_SVC;
687 }
688 #endif
689 
690 #ifdef ARM64
691 bool thread_is_in_normal_mode(void)
692 {
693 	uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR);
694 	struct thread_core_local *l = thread_get_core_local();
695 	bool ret;
696 
697 	/* If any bit in l->flags is set we're handling some exception. */
698 	ret = !l->flags;
699 	thread_unmask_exceptions(exceptions);
700 
701 	return ret;
702 }
703 #endif
704 
705 void thread_state_free(void)
706 {
707 	struct thread_core_local *l = thread_get_core_local();
708 	int ct = l->curr_thread;
709 
710 	assert(ct != -1);
711 
712 	thread_lazy_restore_ns_vfp();
713 	tee_pager_release_phys(
714 		(void *)(threads[ct].stack_va_end - STACK_THREAD_SIZE),
715 		STACK_THREAD_SIZE);
716 
717 	lock_global();
718 
719 	assert(threads[ct].state == THREAD_STATE_ACTIVE);
720 	threads[ct].state = THREAD_STATE_FREE;
721 	threads[ct].flags = 0;
722 	l->curr_thread = -1;
723 
724 #ifdef CFG_VIRTUALIZATION
725 	virt_unset_guest();
726 #endif
727 	unlock_global();
728 }
729 
730 #ifdef CFG_WITH_PAGER
731 static void release_unused_kernel_stack(struct thread_ctx *thr,
732 					uint32_t cpsr __maybe_unused)
733 {
734 #ifdef ARM64
735 	/*
736 	 * If we're from user mode then thr->regs.sp is the saved user
737 	 * stack pointer and thr->kern_sp holds the last kernel stack
738 	 * pointer. But if we're from kernel mode then thr->kern_sp isn't
739 	 * up to date so we need to read from thr->regs.sp instead.
740 	 */
741 	vaddr_t sp = is_from_user(cpsr) ?  thr->kern_sp : thr->regs.sp;
742 #else
743 	vaddr_t sp = thr->regs.svc_sp;
744 #endif
745 	vaddr_t base = thr->stack_va_end - STACK_THREAD_SIZE;
746 	size_t len = sp - base;
747 
748 	tee_pager_release_phys((void *)base, len);
749 }
750 #else
751 static void release_unused_kernel_stack(struct thread_ctx *thr __unused,
752 					uint32_t cpsr __unused)
753 {
754 }
755 #endif
756 
757 int thread_state_suspend(uint32_t flags, uint32_t cpsr, vaddr_t pc)
758 {
759 	struct thread_core_local *l = thread_get_core_local();
760 	int ct = l->curr_thread;
761 
762 	assert(ct != -1);
763 
764 	thread_check_canaries();
765 
766 	release_unused_kernel_stack(threads + ct, cpsr);
767 
768 	if (is_from_user(cpsr)) {
769 		thread_user_save_vfp();
770 		tee_ta_update_session_utime_suspend();
771 		tee_ta_gprof_sample_pc(pc);
772 	}
773 	thread_lazy_restore_ns_vfp();
774 
775 	lock_global();
776 
777 	assert(threads[ct].state == THREAD_STATE_ACTIVE);
778 	threads[ct].flags |= flags;
779 	threads[ct].regs.cpsr = cpsr;
780 	threads[ct].regs.pc = pc;
781 	threads[ct].state = THREAD_STATE_SUSPENDED;
782 
783 	threads[ct].have_user_map = core_mmu_user_mapping_is_active();
784 	if (threads[ct].have_user_map) {
785 		core_mmu_get_user_map(&threads[ct].user_map);
786 		core_mmu_set_user_map(NULL);
787 	}
788 
789 	l->curr_thread = -1;
790 
791 #ifdef CFG_VIRTUALIZATION
792 	virt_unset_guest();
793 #endif
794 
795 	unlock_global();
796 
797 	return ct;
798 }
799 
800 #ifdef ARM32
801 static void set_tmp_stack(struct thread_core_local *l, vaddr_t sp)
802 {
803 	l->tmp_stack_va_end = sp;
804 	thread_set_irq_sp(sp);
805 	thread_set_fiq_sp(sp);
806 }
807 
808 static void set_abt_stack(struct thread_core_local *l, vaddr_t sp)
809 {
810 	l->abt_stack_va_end = sp;
811 	thread_set_abt_sp((vaddr_t)l);
812 	thread_set_und_sp((vaddr_t)l);
813 }
814 #endif /*ARM32*/
815 
816 #ifdef ARM64
817 static void set_tmp_stack(struct thread_core_local *l, vaddr_t sp)
818 {
819 	/*
820 	 * We're already using the tmp stack when this function is called
821 	 * so there's no need to assign it to any stack pointer. However,
822 	 * we'll need to restore it at different times so store it here.
823 	 */
824 	l->tmp_stack_va_end = sp;
825 }
826 
827 static void set_abt_stack(struct thread_core_local *l, vaddr_t sp)
828 {
829 	l->abt_stack_va_end = sp;
830 }
831 #endif /*ARM64*/
832 
833 bool thread_init_stack(uint32_t thread_id, vaddr_t sp)
834 {
835 	if (thread_id >= CFG_NUM_THREADS)
836 		return false;
837 	threads[thread_id].stack_va_end = sp;
838 	return true;
839 }
840 
841 int thread_get_id_may_fail(void)
842 {
843 	/*
844 	 * thread_get_core_local() requires foreign interrupts to be disabled
845 	 */
846 	uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR);
847 	struct thread_core_local *l = thread_get_core_local();
848 	int ct = l->curr_thread;
849 
850 	thread_unmask_exceptions(exceptions);
851 	return ct;
852 }
853 
854 int thread_get_id(void)
855 {
856 	int ct = thread_get_id_may_fail();
857 
858 	assert(ct >= 0 && ct < CFG_NUM_THREADS);
859 	return ct;
860 }
861 
862 static void init_handlers(const struct thread_handlers *handlers)
863 {
864 	thread_std_smc_handler_ptr = handlers->std_smc;
865 	thread_fast_smc_handler_ptr = handlers->fast_smc;
866 	thread_nintr_handler_ptr = handlers->nintr;
867 	thread_cpu_on_handler_ptr = handlers->cpu_on;
868 	thread_cpu_off_handler_ptr = handlers->cpu_off;
869 	thread_cpu_suspend_handler_ptr = handlers->cpu_suspend;
870 	thread_cpu_resume_handler_ptr = handlers->cpu_resume;
871 	thread_system_off_handler_ptr = handlers->system_off;
872 	thread_system_reset_handler_ptr = handlers->system_reset;
873 }
874 
875 #ifdef CFG_WITH_PAGER
876 static void init_thread_stacks(void)
877 {
878 	size_t n = 0;
879 
880 	/*
881 	 * Allocate virtual memory for thread stacks.
882 	 */
883 	for (n = 0; n < CFG_NUM_THREADS; n++) {
884 		tee_mm_entry_t *mm = NULL;
885 		vaddr_t sp = 0;
886 		size_t num_pages = 0;
887 		struct fobj *fobj = NULL;
888 
889 		/* Find vmem for thread stack and its protection gap */
890 		mm = tee_mm_alloc(&tee_mm_vcore,
891 				  SMALL_PAGE_SIZE + STACK_THREAD_SIZE);
892 		assert(mm);
893 
894 		/* Claim eventual physical page */
895 		tee_pager_add_pages(tee_mm_get_smem(mm), tee_mm_get_size(mm),
896 				    true);
897 
898 		num_pages = tee_mm_get_bytes(mm) / SMALL_PAGE_SIZE - 1;
899 		fobj = fobj_locked_paged_alloc(num_pages);
900 
901 		/* Add the area to the pager */
902 		tee_pager_add_core_area(tee_mm_get_smem(mm) + SMALL_PAGE_SIZE,
903 					PAGER_AREA_TYPE_LOCK, fobj);
904 		fobj_put(fobj);
905 
906 		/* init effective stack */
907 		sp = tee_mm_get_smem(mm) + tee_mm_get_bytes(mm);
908 		asan_tag_access((void *)tee_mm_get_smem(mm), (void *)sp);
909 		if (!thread_init_stack(n, sp))
910 			panic("init stack failed");
911 	}
912 }
913 #else
914 static void init_thread_stacks(void)
915 {
916 	size_t n;
917 
918 	/* Assign the thread stacks */
919 	for (n = 0; n < CFG_NUM_THREADS; n++) {
920 		if (!thread_init_stack(n, GET_STACK(stack_thread[n])))
921 			panic("thread_init_stack failed");
922 	}
923 }
924 #endif /*CFG_WITH_PAGER*/
925 
926 static void init_user_kcode(void)
927 {
928 #ifdef CFG_CORE_UNMAP_CORE_AT_EL0
929 	vaddr_t v = (vaddr_t)thread_excp_vect;
930 	vaddr_t ve = (vaddr_t)thread_excp_vect_end;
931 
932 	thread_user_kcode_va = ROUNDDOWN(v, CORE_MMU_USER_CODE_SIZE);
933 	ve = ROUNDUP(ve, CORE_MMU_USER_CODE_SIZE);
934 	thread_user_kcode_size = ve - thread_user_kcode_va;
935 
936 	core_mmu_get_user_va_range(&v, NULL);
937 	thread_user_kcode_offset = thread_user_kcode_va - v;
938 
939 #if defined(CFG_CORE_WORKAROUND_SPECTRE_BP_SEC) && defined(ARM64)
940 	/*
941 	 * When transitioning to EL0 subtract SP with this much to point to
942 	 * this special kdata page instead. SP is restored by add this much
943 	 * while transitioning back to EL1.
944 	 */
945 	v += thread_user_kcode_size;
946 	thread_user_kdata_sp_offset = (vaddr_t)thread_core_local - v;
947 #endif
948 #endif /*CFG_CORE_UNMAP_CORE_AT_EL0*/
949 }
950 
951 void thread_init_threads(void)
952 {
953 	size_t n;
954 
955 	init_thread_stacks();
956 	pgt_init();
957 
958 	mutex_lockdep_init();
959 
960 	for (n = 0; n < CFG_NUM_THREADS; n++) {
961 		TAILQ_INIT(&threads[n].tsd.sess_stack);
962 		SLIST_INIT(&threads[n].tsd.pgt_cache);
963 	}
964 
965 	for (n = 0; n < CFG_TEE_CORE_NB_CORE; n++)
966 		thread_core_local[n].curr_thread = -1;
967 }
968 
969 void thread_init_primary(const struct thread_handlers *handlers)
970 {
971 	init_handlers(handlers);
972 
973 	/* Initialize canaries around the stacks */
974 	init_canaries();
975 
976 	init_user_kcode();
977 }
978 
979 static void init_sec_mon(size_t pos __maybe_unused)
980 {
981 #if !defined(CFG_WITH_ARM_TRUSTED_FW)
982 	/* Initialize secure monitor */
983 	sm_init(GET_STACK(stack_tmp[pos]));
984 #endif
985 }
986 
987 static uint32_t __maybe_unused get_midr_implementer(uint32_t midr)
988 {
989 	return (midr >> MIDR_IMPLEMENTER_SHIFT) & MIDR_IMPLEMENTER_MASK;
990 }
991 
992 static uint32_t __maybe_unused get_midr_primary_part(uint32_t midr)
993 {
994 	return (midr >> MIDR_PRIMARY_PART_NUM_SHIFT) &
995 	       MIDR_PRIMARY_PART_NUM_MASK;
996 }
997 
998 #ifdef ARM64
999 static bool probe_workaround_available(void)
1000 {
1001 	int32_t r;
1002 
1003 	r = thread_smc(SMCCC_VERSION, 0, 0, 0);
1004 	if (r < 0)
1005 		return false;
1006 	if (r < 0x10001)	/* compare with version 1.1 */
1007 		return false;
1008 
1009 	/* Version >= 1.1, so SMCCC_ARCH_FEATURES is available */
1010 	r = thread_smc(SMCCC_ARCH_FEATURES, SMCCC_ARCH_WORKAROUND_1, 0, 0);
1011 	return r >= 0;
1012 }
1013 
1014 static vaddr_t __maybe_unused select_vector(vaddr_t a)
1015 {
1016 	if (probe_workaround_available()) {
1017 		DMSG("SMCCC_ARCH_WORKAROUND_1 (%#08" PRIx32 ") available",
1018 		     SMCCC_ARCH_WORKAROUND_1);
1019 		DMSG("SMC Workaround for CVE-2017-5715 used");
1020 		return a;
1021 	}
1022 
1023 	DMSG("SMCCC_ARCH_WORKAROUND_1 (%#08" PRIx32 ") unavailable",
1024 	     SMCCC_ARCH_WORKAROUND_1);
1025 	DMSG("SMC Workaround for CVE-2017-5715 not needed (if ARM-TF is up to date)");
1026 	return (vaddr_t)thread_excp_vect;
1027 }
1028 #else
1029 static vaddr_t __maybe_unused select_vector(vaddr_t a)
1030 {
1031 	return a;
1032 }
1033 #endif
1034 
1035 static vaddr_t get_excp_vect(void)
1036 {
1037 #ifdef CFG_CORE_WORKAROUND_SPECTRE_BP_SEC
1038 	uint32_t midr = read_midr();
1039 
1040 	if (get_midr_implementer(midr) != MIDR_IMPLEMENTER_ARM)
1041 		return (vaddr_t)thread_excp_vect;
1042 
1043 	switch (get_midr_primary_part(midr)) {
1044 #ifdef ARM32
1045 	case CORTEX_A8_PART_NUM:
1046 	case CORTEX_A9_PART_NUM:
1047 	case CORTEX_A17_PART_NUM:
1048 #endif
1049 	case CORTEX_A57_PART_NUM:
1050 	case CORTEX_A72_PART_NUM:
1051 	case CORTEX_A73_PART_NUM:
1052 	case CORTEX_A75_PART_NUM:
1053 		return select_vector((vaddr_t)thread_excp_vect_workaround);
1054 #ifdef ARM32
1055 	case CORTEX_A15_PART_NUM:
1056 		return select_vector((vaddr_t)thread_excp_vect_workaround_a15);
1057 #endif
1058 	default:
1059 		return (vaddr_t)thread_excp_vect;
1060 	}
1061 #endif /*CFG_CORE_WORKAROUND_SPECTRE_BP_SEC*/
1062 
1063 	return (vaddr_t)thread_excp_vect;
1064 }
1065 
1066 void thread_init_per_cpu(void)
1067 {
1068 	size_t pos = get_core_pos();
1069 	struct thread_core_local *l = thread_get_core_local();
1070 
1071 	init_sec_mon(pos);
1072 
1073 	set_tmp_stack(l, GET_STACK(stack_tmp[pos]) - STACK_TMP_OFFS);
1074 	set_abt_stack(l, GET_STACK(stack_abt[pos]));
1075 
1076 	thread_init_vbar(get_excp_vect());
1077 }
1078 
1079 struct thread_specific_data *thread_get_tsd(void)
1080 {
1081 	return &threads[thread_get_id()].tsd;
1082 }
1083 
1084 struct thread_ctx_regs *thread_get_ctx_regs(void)
1085 {
1086 	struct thread_core_local *l = thread_get_core_local();
1087 
1088 	assert(l->curr_thread != -1);
1089 	return &threads[l->curr_thread].regs;
1090 }
1091 
1092 void thread_set_foreign_intr(bool enable)
1093 {
1094 	/* thread_get_core_local() requires foreign interrupts to be disabled */
1095 	uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR);
1096 	struct thread_core_local *l;
1097 
1098 	l = thread_get_core_local();
1099 
1100 	assert(l->curr_thread != -1);
1101 
1102 	if (enable) {
1103 		threads[l->curr_thread].flags |=
1104 					THREAD_FLAGS_FOREIGN_INTR_ENABLE;
1105 		thread_set_exceptions(exceptions & ~THREAD_EXCP_FOREIGN_INTR);
1106 	} else {
1107 		/*
1108 		 * No need to disable foreign interrupts here since they're
1109 		 * already disabled above.
1110 		 */
1111 		threads[l->curr_thread].flags &=
1112 					~THREAD_FLAGS_FOREIGN_INTR_ENABLE;
1113 	}
1114 }
1115 
1116 void thread_restore_foreign_intr(void)
1117 {
1118 	/* thread_get_core_local() requires foreign interrupts to be disabled */
1119 	uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR);
1120 	struct thread_core_local *l;
1121 
1122 	l = thread_get_core_local();
1123 
1124 	assert(l->curr_thread != -1);
1125 
1126 	if (threads[l->curr_thread].flags & THREAD_FLAGS_FOREIGN_INTR_ENABLE)
1127 		thread_set_exceptions(exceptions & ~THREAD_EXCP_FOREIGN_INTR);
1128 }
1129 
1130 #ifdef CFG_WITH_VFP
1131 uint32_t thread_kernel_enable_vfp(void)
1132 {
1133 	uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR);
1134 	struct thread_ctx *thr = threads + thread_get_id();
1135 	struct thread_user_vfp_state *tuv = thr->vfp_state.uvfp;
1136 
1137 	assert(!vfp_is_enabled());
1138 
1139 	if (!thr->vfp_state.ns_saved) {
1140 		vfp_lazy_save_state_final(&thr->vfp_state.ns,
1141 					  true /*force_save*/);
1142 		thr->vfp_state.ns_saved = true;
1143 	} else if (thr->vfp_state.sec_lazy_saved &&
1144 		   !thr->vfp_state.sec_saved) {
1145 		/*
1146 		 * This happens when we're handling an abort while the
1147 		 * thread was using the VFP state.
1148 		 */
1149 		vfp_lazy_save_state_final(&thr->vfp_state.sec,
1150 					  false /*!force_save*/);
1151 		thr->vfp_state.sec_saved = true;
1152 	} else if (tuv && tuv->lazy_saved && !tuv->saved) {
1153 		/*
1154 		 * This can happen either during syscall or abort
1155 		 * processing (while processing a syscall).
1156 		 */
1157 		vfp_lazy_save_state_final(&tuv->vfp, false /*!force_save*/);
1158 		tuv->saved = true;
1159 	}
1160 
1161 	vfp_enable();
1162 	return exceptions;
1163 }
1164 
1165 void thread_kernel_disable_vfp(uint32_t state)
1166 {
1167 	uint32_t exceptions;
1168 
1169 	assert(vfp_is_enabled());
1170 
1171 	vfp_disable();
1172 	exceptions = thread_get_exceptions();
1173 	assert(exceptions & THREAD_EXCP_FOREIGN_INTR);
1174 	exceptions &= ~THREAD_EXCP_FOREIGN_INTR;
1175 	exceptions |= state & THREAD_EXCP_FOREIGN_INTR;
1176 	thread_set_exceptions(exceptions);
1177 }
1178 
1179 void thread_kernel_save_vfp(void)
1180 {
1181 	struct thread_ctx *thr = threads + thread_get_id();
1182 
1183 	assert(thread_get_exceptions() & THREAD_EXCP_FOREIGN_INTR);
1184 	if (vfp_is_enabled()) {
1185 		vfp_lazy_save_state_init(&thr->vfp_state.sec);
1186 		thr->vfp_state.sec_lazy_saved = true;
1187 	}
1188 }
1189 
1190 void thread_kernel_restore_vfp(void)
1191 {
1192 	struct thread_ctx *thr = threads + thread_get_id();
1193 
1194 	assert(thread_get_exceptions() & THREAD_EXCP_FOREIGN_INTR);
1195 	assert(!vfp_is_enabled());
1196 	if (thr->vfp_state.sec_lazy_saved) {
1197 		vfp_lazy_restore_state(&thr->vfp_state.sec,
1198 				       thr->vfp_state.sec_saved);
1199 		thr->vfp_state.sec_saved = false;
1200 		thr->vfp_state.sec_lazy_saved = false;
1201 	}
1202 }
1203 
1204 void thread_user_enable_vfp(struct thread_user_vfp_state *uvfp)
1205 {
1206 	struct thread_ctx *thr = threads + thread_get_id();
1207 	struct thread_user_vfp_state *tuv = thr->vfp_state.uvfp;
1208 
1209 	assert(thread_get_exceptions() & THREAD_EXCP_FOREIGN_INTR);
1210 	assert(!vfp_is_enabled());
1211 
1212 	if (!thr->vfp_state.ns_saved) {
1213 		vfp_lazy_save_state_final(&thr->vfp_state.ns,
1214 					  true /*force_save*/);
1215 		thr->vfp_state.ns_saved = true;
1216 	} else if (tuv && uvfp != tuv) {
1217 		if (tuv->lazy_saved && !tuv->saved) {
1218 			vfp_lazy_save_state_final(&tuv->vfp,
1219 						  false /*!force_save*/);
1220 			tuv->saved = true;
1221 		}
1222 	}
1223 
1224 	if (uvfp->lazy_saved)
1225 		vfp_lazy_restore_state(&uvfp->vfp, uvfp->saved);
1226 	uvfp->lazy_saved = false;
1227 	uvfp->saved = false;
1228 
1229 	thr->vfp_state.uvfp = uvfp;
1230 	vfp_enable();
1231 }
1232 
1233 void thread_user_save_vfp(void)
1234 {
1235 	struct thread_ctx *thr = threads + thread_get_id();
1236 	struct thread_user_vfp_state *tuv = thr->vfp_state.uvfp;
1237 
1238 	assert(thread_get_exceptions() & THREAD_EXCP_FOREIGN_INTR);
1239 	if (!vfp_is_enabled())
1240 		return;
1241 
1242 	assert(tuv && !tuv->lazy_saved && !tuv->saved);
1243 	vfp_lazy_save_state_init(&tuv->vfp);
1244 	tuv->lazy_saved = true;
1245 }
1246 
1247 void thread_user_clear_vfp(struct thread_user_vfp_state *uvfp)
1248 {
1249 	struct thread_ctx *thr = threads + thread_get_id();
1250 
1251 	if (uvfp == thr->vfp_state.uvfp)
1252 		thr->vfp_state.uvfp = NULL;
1253 	uvfp->lazy_saved = false;
1254 	uvfp->saved = false;
1255 }
1256 #endif /*CFG_WITH_VFP*/
1257 
1258 #ifdef ARM32
1259 static bool get_spsr(bool is_32bit, unsigned long entry_func, uint32_t *spsr)
1260 {
1261 	uint32_t s;
1262 
1263 	if (!is_32bit)
1264 		return false;
1265 
1266 	s = read_spsr();
1267 	s &= ~(CPSR_MODE_MASK | CPSR_T | CPSR_IT_MASK1 | CPSR_IT_MASK2);
1268 	s |= CPSR_MODE_USR;
1269 	if (entry_func & 1)
1270 		s |= CPSR_T;
1271 	*spsr = s;
1272 	return true;
1273 }
1274 #endif
1275 
1276 #ifdef ARM64
1277 static bool get_spsr(bool is_32bit, unsigned long entry_func, uint32_t *spsr)
1278 {
1279 	uint32_t s;
1280 
1281 	if (is_32bit) {
1282 		s = read_daif() & (SPSR_32_AIF_MASK << SPSR_32_AIF_SHIFT);
1283 		s |= SPSR_MODE_RW_32 << SPSR_MODE_RW_SHIFT;
1284 		s |= (entry_func & SPSR_32_T_MASK) << SPSR_32_T_SHIFT;
1285 	} else {
1286 		s = read_daif() & (SPSR_64_DAIF_MASK << SPSR_64_DAIF_SHIFT);
1287 	}
1288 
1289 	*spsr = s;
1290 	return true;
1291 }
1292 #endif
1293 
1294 uint32_t thread_enter_user_mode(unsigned long a0, unsigned long a1,
1295 		unsigned long a2, unsigned long a3, unsigned long user_sp,
1296 		unsigned long entry_func, bool is_32bit,
1297 		uint32_t *exit_status0, uint32_t *exit_status1)
1298 {
1299 	uint32_t spsr;
1300 
1301 	tee_ta_update_session_utime_resume();
1302 
1303 	if (!get_spsr(is_32bit, entry_func, &spsr)) {
1304 		*exit_status0 = 1; /* panic */
1305 		*exit_status1 = 0xbadbadba;
1306 		return 0;
1307 	}
1308 	return __thread_enter_user_mode(a0, a1, a2, a3, user_sp, entry_func,
1309 					spsr, exit_status0, exit_status1);
1310 }
1311 
1312 #ifdef CFG_CORE_UNMAP_CORE_AT_EL0
1313 void thread_get_user_kcode(struct mobj **mobj, size_t *offset,
1314 			   vaddr_t *va, size_t *sz)
1315 {
1316 	core_mmu_get_user_va_range(va, NULL);
1317 	*mobj = mobj_tee_ram;
1318 	*offset = thread_user_kcode_va - TEE_RAM_START;
1319 	*sz = thread_user_kcode_size;
1320 }
1321 #endif
1322 
1323 #if defined(CFG_CORE_UNMAP_CORE_AT_EL0) && \
1324 	defined(CFG_CORE_WORKAROUND_SPECTRE_BP_SEC) && defined(ARM64)
1325 void thread_get_user_kdata(struct mobj **mobj, size_t *offset,
1326 			   vaddr_t *va, size_t *sz)
1327 {
1328 	vaddr_t v;
1329 
1330 	core_mmu_get_user_va_range(&v, NULL);
1331 	*va = v + thread_user_kcode_size;
1332 	*mobj = mobj_tee_ram;
1333 	*offset = (vaddr_t)thread_user_kdata_page - TEE_RAM_START;
1334 	*sz = sizeof(thread_user_kdata_page);
1335 }
1336 #endif
1337 
1338 bool thread_disable_prealloc_rpc_cache(uint64_t *cookie)
1339 {
1340 	bool rv;
1341 	size_t n;
1342 	uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR);
1343 
1344 	lock_global();
1345 
1346 	for (n = 0; n < CFG_NUM_THREADS; n++) {
1347 		if (threads[n].state != THREAD_STATE_FREE) {
1348 			rv = false;
1349 			goto out;
1350 		}
1351 	}
1352 
1353 	rv = true;
1354 	for (n = 0; n < CFG_NUM_THREADS; n++) {
1355 		if (threads[n].rpc_arg) {
1356 			*cookie = mobj_get_cookie(threads[n].rpc_mobj);
1357 			mobj_free(threads[n].rpc_mobj);
1358 			threads[n].rpc_arg = NULL;
1359 			goto out;
1360 		}
1361 	}
1362 
1363 	*cookie = 0;
1364 	thread_prealloc_rpc_cache = false;
1365 out:
1366 	unlock_global();
1367 	thread_unmask_exceptions(exceptions);
1368 	return rv;
1369 }
1370 
1371 bool thread_enable_prealloc_rpc_cache(void)
1372 {
1373 	bool rv;
1374 	size_t n;
1375 	uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR);
1376 
1377 	lock_global();
1378 
1379 	for (n = 0; n < CFG_NUM_THREADS; n++) {
1380 		if (threads[n].state != THREAD_STATE_FREE) {
1381 			rv = false;
1382 			goto out;
1383 		}
1384 	}
1385 
1386 	rv = true;
1387 	thread_prealloc_rpc_cache = true;
1388 out:
1389 	unlock_global();
1390 	thread_unmask_exceptions(exceptions);
1391 	return rv;
1392 }
1393 
1394 /**
1395  * Allocates data for struct optee_msg_arg.
1396  *
1397  * @size:	size in bytes of struct optee_msg_arg
1398  *
1399  * @returns	mobj that describes allocated buffer or NULL on error
1400  */
1401 static struct mobj *thread_rpc_alloc_arg(size_t size)
1402 {
1403 	paddr_t pa;
1404 	uint64_t co;
1405 	uint32_t rpc_args[THREAD_RPC_NUM_ARGS] = {
1406 		OPTEE_SMC_RETURN_RPC_ALLOC, size
1407 	};
1408 	struct mobj *mobj = NULL;
1409 
1410 	thread_rpc(rpc_args);
1411 
1412 	pa = reg_pair_to_64(rpc_args[1], rpc_args[2]);
1413 	co = reg_pair_to_64(rpc_args[4], rpc_args[5]);
1414 
1415 	if (!ALIGNMENT_IS_OK(pa, struct optee_msg_arg))
1416 		goto err;
1417 
1418 	/* Check if this region is in static shared space */
1419 	if (core_pbuf_is(CORE_MEM_NSEC_SHM, pa, size))
1420 		mobj = mobj_shm_alloc(pa, size, co);
1421 #ifdef CFG_CORE_DYN_SHM
1422 	else if ((!(pa & SMALL_PAGE_MASK)) && size <= SMALL_PAGE_SIZE)
1423 		mobj = mobj_mapped_shm_alloc(&pa, 1, 0, co);
1424 #endif
1425 
1426 	if (!mobj)
1427 		goto err;
1428 
1429 	return mobj;
1430 err:
1431 	thread_rpc_free_arg(co);
1432 	mobj_free(mobj);
1433 	return NULL;
1434 }
1435 
1436 static bool set_rmem(struct optee_msg_param *param,
1437 		     struct thread_param *tpm)
1438 {
1439 	param->attr = tpm->attr - THREAD_PARAM_ATTR_MEMREF_IN +
1440 		      OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
1441 	param->u.rmem.offs = tpm->u.memref.offs;
1442 	param->u.rmem.size = tpm->u.memref.size;
1443 	if (tpm->u.memref.mobj) {
1444 		param->u.rmem.shm_ref = mobj_get_cookie(tpm->u.memref.mobj);
1445 		if (!param->u.rmem.shm_ref)
1446 			return false;
1447 	} else {
1448 		param->u.rmem.shm_ref = 0;
1449 	}
1450 
1451 	return true;
1452 }
1453 
1454 static bool set_tmem(struct optee_msg_param *param,
1455 		     struct thread_param *tpm)
1456 {
1457 	paddr_t pa = 0;
1458 	uint64_t shm_ref = 0;
1459 	struct mobj *mobj = tpm->u.memref.mobj;
1460 
1461 	param->attr = tpm->attr - THREAD_PARAM_ATTR_MEMREF_IN +
1462 		      OPTEE_MSG_ATTR_TYPE_TMEM_INPUT;
1463 	if (mobj) {
1464 		shm_ref = mobj_get_cookie(mobj);
1465 		if (!shm_ref)
1466 			return false;
1467 		if (mobj_get_pa(mobj, tpm->u.memref.offs, 0, &pa))
1468 			return false;
1469 	}
1470 
1471 	param->u.tmem.size = tpm->u.memref.size;
1472 	param->u.tmem.buf_ptr = pa;
1473 	param->u.tmem.shm_ref = shm_ref;
1474 
1475 	return true;
1476 }
1477 
1478 static uint32_t get_rpc_arg(uint32_t cmd, size_t num_params,
1479 			    struct thread_param *params, void **arg_ret,
1480 			    uint64_t *carg_ret)
1481 {
1482 	struct thread_ctx *thr = threads + thread_get_id();
1483 	struct optee_msg_arg *arg = thr->rpc_arg;
1484 	size_t sz = OPTEE_MSG_GET_ARG_SIZE(THREAD_RPC_MAX_NUM_PARAMS);
1485 
1486 	if (num_params > THREAD_RPC_MAX_NUM_PARAMS)
1487 		return TEE_ERROR_BAD_PARAMETERS;
1488 
1489 	if (!arg) {
1490 		struct mobj *mobj = thread_rpc_alloc_arg(sz);
1491 
1492 		if (!mobj)
1493 			return TEE_ERROR_OUT_OF_MEMORY;
1494 
1495 		arg = mobj_get_va(mobj, 0);
1496 		if (!arg) {
1497 			thread_rpc_free_arg(mobj_get_cookie(mobj));
1498 			return TEE_ERROR_OUT_OF_MEMORY;
1499 		}
1500 
1501 		thr->rpc_arg = arg;
1502 		thr->rpc_mobj = mobj;
1503 	}
1504 
1505 	memset(arg, 0, OPTEE_MSG_GET_ARG_SIZE(num_params));
1506 	arg->cmd = cmd;
1507 	arg->num_params = num_params;
1508 	arg->ret = TEE_ERROR_GENERIC; /* in case value isn't updated */
1509 
1510 	for (size_t n = 0; n < num_params; n++) {
1511 		switch (params[n].attr) {
1512 		case THREAD_PARAM_ATTR_NONE:
1513 			arg->params[n].attr = OPTEE_MSG_ATTR_TYPE_NONE;
1514 			break;
1515 		case THREAD_PARAM_ATTR_VALUE_IN:
1516 		case THREAD_PARAM_ATTR_VALUE_OUT:
1517 		case THREAD_PARAM_ATTR_VALUE_INOUT:
1518 			arg->params[n].attr = params[n].attr -
1519 					      THREAD_PARAM_ATTR_VALUE_IN +
1520 					      OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
1521 			arg->params[n].u.value.a = params[n].u.value.a;
1522 			arg->params[n].u.value.b = params[n].u.value.b;
1523 			arg->params[n].u.value.c = params[n].u.value.c;
1524 			break;
1525 		case THREAD_PARAM_ATTR_MEMREF_IN:
1526 		case THREAD_PARAM_ATTR_MEMREF_OUT:
1527 		case THREAD_PARAM_ATTR_MEMREF_INOUT:
1528 			if (!params[n].u.memref.mobj ||
1529 			    mobj_matches(params[n].u.memref.mobj,
1530 					 CORE_MEM_NSEC_SHM)) {
1531 				if (!set_tmem(arg->params + n, params + n))
1532 					return TEE_ERROR_BAD_PARAMETERS;
1533 			} else  if (mobj_matches(params[n].u.memref.mobj,
1534 						 CORE_MEM_REG_SHM)) {
1535 				if (!set_rmem(arg->params + n, params + n))
1536 					return TEE_ERROR_BAD_PARAMETERS;
1537 			} else {
1538 				return TEE_ERROR_BAD_PARAMETERS;
1539 			}
1540 			break;
1541 		default:
1542 			return TEE_ERROR_BAD_PARAMETERS;
1543 		}
1544 	}
1545 
1546 	*arg_ret = arg;
1547 	*carg_ret = mobj_get_cookie(thr->rpc_mobj);
1548 
1549 	return TEE_SUCCESS;
1550 }
1551 
1552 static uint32_t get_rpc_arg_res(struct optee_msg_arg *arg, size_t num_params,
1553 				struct thread_param *params)
1554 {
1555 	for (size_t n = 0; n < num_params; n++) {
1556 		switch (params[n].attr) {
1557 		case THREAD_PARAM_ATTR_VALUE_OUT:
1558 		case THREAD_PARAM_ATTR_VALUE_INOUT:
1559 			params[n].u.value.a = arg->params[n].u.value.a;
1560 			params[n].u.value.b = arg->params[n].u.value.b;
1561 			params[n].u.value.c = arg->params[n].u.value.c;
1562 			break;
1563 		case THREAD_PARAM_ATTR_MEMREF_OUT:
1564 		case THREAD_PARAM_ATTR_MEMREF_INOUT:
1565 			/*
1566 			 * rmem.size and tmem.size is the same type and
1567 			 * location.
1568 			 */
1569 			params[n].u.memref.size = arg->params[n].u.rmem.size;
1570 			break;
1571 		default:
1572 			break;
1573 		}
1574 	}
1575 
1576 	return arg->ret;
1577 }
1578 
1579 uint32_t thread_rpc_cmd(uint32_t cmd, size_t num_params,
1580 			struct thread_param *params)
1581 {
1582 	uint32_t rpc_args[THREAD_RPC_NUM_ARGS] = { OPTEE_SMC_RETURN_RPC_CMD };
1583 	void *arg = NULL;
1584 	uint64_t carg = 0;
1585 	uint32_t ret = 0;
1586 
1587 	/* The source CRYPTO_RNG_SRC_JITTER_RPC is safe to use here */
1588 	plat_prng_add_jitter_entropy(CRYPTO_RNG_SRC_JITTER_RPC,
1589 				     &thread_rpc_pnum);
1590 
1591 	ret = get_rpc_arg(cmd, num_params, params, &arg, &carg);
1592 	if (ret)
1593 		return ret;
1594 
1595 	reg_pair_from_64(carg, rpc_args + 1, rpc_args + 2);
1596 	thread_rpc(rpc_args);
1597 
1598 	return get_rpc_arg_res(arg, num_params, params);
1599 }
1600 
1601 /**
1602  * Free physical memory previously allocated with thread_rpc_alloc()
1603  *
1604  * @cookie:	cookie received when allocating the buffer
1605  * @bt:		must be the same as supplied when allocating
1606  * @mobj:	mobj that describes allocated buffer
1607  *
1608  * This function also frees corresponding mobj.
1609  */
1610 static void thread_rpc_free(unsigned int bt, uint64_t cookie, struct mobj *mobj)
1611 {
1612 	uint32_t rpc_args[THREAD_RPC_NUM_ARGS] = { OPTEE_SMC_RETURN_RPC_CMD };
1613 	void *arg = NULL;
1614 	uint64_t carg = 0;
1615 	struct thread_param param = THREAD_PARAM_VALUE(IN, bt, cookie, 0);
1616 	uint32_t ret = get_rpc_arg(OPTEE_RPC_CMD_SHM_FREE, 1, &param,
1617 				   &arg, &carg);
1618 
1619 	mobj_free(mobj);
1620 
1621 	if (!ret) {
1622 		reg_pair_from_64(carg, rpc_args + 1, rpc_args + 2);
1623 		thread_rpc(rpc_args);
1624 	}
1625 }
1626 
1627 static struct mobj *get_rpc_alloc_res(struct optee_msg_arg *arg,
1628 				      unsigned int bt)
1629 {
1630 	struct mobj *mobj = NULL;
1631 	uint64_t cookie = 0;
1632 
1633 	if (arg->ret || arg->num_params != 1)
1634 		return NULL;
1635 
1636 	if (arg->params[0].attr == OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT) {
1637 		cookie = arg->params[0].u.tmem.shm_ref;
1638 		mobj = mobj_shm_alloc(arg->params[0].u.tmem.buf_ptr,
1639 				      arg->params[0].u.tmem.size,
1640 				      cookie);
1641 	} else if (arg->params[0].attr == (OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
1642 					   OPTEE_MSG_ATTR_NONCONTIG)) {
1643 		cookie = arg->params[0].u.tmem.shm_ref;
1644 		mobj = msg_param_mobj_from_noncontig(
1645 			arg->params[0].u.tmem.buf_ptr,
1646 			arg->params[0].u.tmem.size,
1647 			cookie,
1648 			true);
1649 	} else {
1650 		return NULL;
1651 	}
1652 
1653 	if (!mobj) {
1654 		thread_rpc_free(bt, cookie, mobj);
1655 		return NULL;
1656 	}
1657 
1658 	assert(mobj_is_nonsec(mobj));
1659 
1660 	return mobj;
1661 }
1662 
1663 /**
1664  * Allocates shared memory buffer via RPC
1665  *
1666  * @size:	size in bytes of shared memory buffer
1667  * @align:	required alignment of buffer
1668  * @bt:		buffer type OPTEE_RPC_SHM_TYPE_*
1669  *
1670  * Returns a pointer to MOBJ for the memory on success, or NULL on failure.
1671  */
1672 static struct mobj *thread_rpc_alloc(size_t size, size_t align, unsigned int bt)
1673 {
1674 	uint32_t rpc_args[THREAD_RPC_NUM_ARGS] = { OPTEE_SMC_RETURN_RPC_CMD };
1675 	void *arg = NULL;
1676 	uint64_t carg = 0;
1677 	struct thread_param param = THREAD_PARAM_VALUE(IN, bt, size, align);
1678 	uint32_t ret = get_rpc_arg(OPTEE_RPC_CMD_SHM_ALLOC, 1, &param,
1679 				   &arg, &carg);
1680 
1681 	if (ret)
1682 		return NULL;
1683 
1684 	reg_pair_from_64(carg, rpc_args + 1, rpc_args + 2);
1685 	thread_rpc(rpc_args);
1686 
1687 	return get_rpc_alloc_res(arg, bt);
1688 }
1689 
1690 struct mobj *thread_rpc_alloc_payload(size_t size)
1691 {
1692 	return thread_rpc_alloc(size, 8, OPTEE_RPC_SHM_TYPE_APPL);
1693 }
1694 
1695 void thread_rpc_free_payload(struct mobj *mobj)
1696 {
1697 	thread_rpc_free(OPTEE_RPC_SHM_TYPE_APPL, mobj_get_cookie(mobj),
1698 			mobj);
1699 }
1700 
1701 struct mobj *thread_rpc_alloc_global_payload(size_t size)
1702 {
1703 	return thread_rpc_alloc(size, 8, OPTEE_RPC_SHM_TYPE_GLOBAL);
1704 }
1705 
1706 void thread_rpc_free_global_payload(struct mobj *mobj)
1707 {
1708 	thread_rpc_free(OPTEE_RPC_SHM_TYPE_GLOBAL, mobj_get_cookie(mobj),
1709 			mobj);
1710 }
1711