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