1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2016-2022, Linaro Limited
4  * Copyright (c) 2014, STMicroelectronics International N.V.
5  * Copyright (c) 2020-2021, Arm Limited
6  */
7 
8 #include <config.h>
9 #include <crypto/crypto.h>
10 #include <kernel/asan.h>
11 #include <kernel/lockdep.h>
12 #include <kernel/misc.h>
13 #include <kernel/panic.h>
14 #include <kernel/spinlock.h>
15 #include <kernel/thread.h>
16 #include <kernel/thread_private.h>
17 #include <mm/mobj.h>
18 
19 struct thread_ctx threads[CFG_NUM_THREADS];
20 
21 struct thread_core_local thread_core_local[CFG_TEE_CORE_NB_CORE] __nex_bss;
22 
23 /*
24  * Stacks
25  *
26  * [Lower addresses on the left]
27  *
28  * [ STACK_CANARY_SIZE/2 | STACK_CHECK_EXTRA | STACK_XXX_SIZE | STACK_CANARY_SIZE/2 ]
29  * ^                     ^                   ^                ^
30  * stack_xxx[n]          "hard" top          "soft" top       bottom
31  */
32 
33 #ifdef CFG_WITH_STACK_CANARIES
34 #define START_CANARY_VALUE	0xdededede
35 #define END_CANARY_VALUE	0xabababab
36 #define GET_START_CANARY(name, stack_num) name[stack_num][0]
37 #define GET_END_CANARY(name, stack_num) \
38 	name[stack_num][sizeof(name[stack_num]) / sizeof(uint32_t) - 1]
39 #endif
40 
41 #define DECLARE_STACK(name, num_stacks, stack_size, linkage) \
42 linkage uint32_t name[num_stacks] \
43 		[ROUNDUP(stack_size + STACK_CANARY_SIZE + STACK_CHECK_EXTRA, \
44 			 STACK_ALIGNMENT) / sizeof(uint32_t)] \
45 		__attribute__((section(".nozi_stack." # name), \
46 			       aligned(STACK_ALIGNMENT)))
47 
48 #define GET_STACK(stack) ((vaddr_t)(stack) + STACK_SIZE(stack))
49 
50 DECLARE_STACK(stack_tmp, CFG_TEE_CORE_NB_CORE, STACK_TMP_SIZE,
51 	      /* global linkage */);
52 DECLARE_STACK(stack_abt, CFG_TEE_CORE_NB_CORE, STACK_ABT_SIZE, static);
53 #ifndef CFG_WITH_PAGER
54 DECLARE_STACK(stack_thread, CFG_NUM_THREADS, STACK_THREAD_SIZE, static);
55 #endif
56 
57 #define GET_STACK_TOP_HARD(stack, n) \
58 	((vaddr_t)&(stack)[n] + STACK_CANARY_SIZE / 2)
59 #define GET_STACK_TOP_SOFT(stack, n) \
60 	(GET_STACK_TOP_HARD(stack, n) + STACK_CHECK_EXTRA)
61 #define GET_STACK_BOTTOM(stack, n) ((vaddr_t)&(stack)[n] + sizeof(stack[n]) - \
62 				    STACK_CANARY_SIZE / 2)
63 
64 const uint32_t stack_tmp_stride __section(".identity_map.stack_tmp_stride") =
65 	sizeof(stack_tmp[0]);
66 
67 /*
68  * This stack setup info is required by secondary boot cores before they
69  * each locally enable the pager (the mmu). Hence kept in pager sections.
70  */
71 DECLARE_KEEP_PAGER(stack_tmp_stride);
72 
73 static unsigned int thread_global_lock __nex_bss = SPINLOCK_UNLOCK;
74 
thread_init_canaries(void)75 void thread_init_canaries(void)
76 {
77 #ifdef CFG_WITH_STACK_CANARIES
78 	size_t n;
79 #define INIT_CANARY(name)						\
80 	for (n = 0; n < ARRAY_SIZE(name); n++) {			\
81 		uint32_t *start_canary = &GET_START_CANARY(name, n);	\
82 		uint32_t *end_canary = &GET_END_CANARY(name, n);	\
83 									\
84 		*start_canary = START_CANARY_VALUE;			\
85 		*end_canary = END_CANARY_VALUE;				\
86 	}
87 
88 	INIT_CANARY(stack_tmp);
89 	INIT_CANARY(stack_abt);
90 #if !defined(CFG_WITH_PAGER) && !defined(CFG_VIRTUALIZATION)
91 	INIT_CANARY(stack_thread);
92 #endif
93 #endif/*CFG_WITH_STACK_CANARIES*/
94 }
95 
96 #define CANARY_DIED(stack, loc, n, addr) \
97 	do { \
98 		EMSG_RAW("Dead canary at %s of '%s[%zu]' (%p)", #loc, #stack, \
99 			 n, (void *)addr); \
100 		panic(); \
101 	} while (0)
102 
thread_check_canaries(void)103 void thread_check_canaries(void)
104 {
105 #ifdef CFG_WITH_STACK_CANARIES
106 	uint32_t *canary = NULL;
107 	size_t n = 0;
108 
109 	for (n = 0; n < ARRAY_SIZE(stack_tmp); n++) {
110 		canary = &GET_START_CANARY(stack_tmp, n);
111 		if (*canary != START_CANARY_VALUE)
112 			CANARY_DIED(stack_tmp, start, n, canary);
113 		canary = &GET_END_CANARY(stack_tmp, n);
114 		if (*canary != END_CANARY_VALUE)
115 			CANARY_DIED(stack_tmp, end, n, canary);
116 	}
117 
118 	for (n = 0; n < ARRAY_SIZE(stack_abt); n++) {
119 		canary = &GET_START_CANARY(stack_abt, n);
120 		if (*canary != START_CANARY_VALUE)
121 			CANARY_DIED(stack_abt, start, n, canary);
122 		canary = &GET_END_CANARY(stack_abt, n);
123 		if (*canary != END_CANARY_VALUE)
124 			CANARY_DIED(stack_abt, end, n, canary);
125 	}
126 #if !defined(CFG_WITH_PAGER) && !defined(CFG_VIRTUALIZATION)
127 	for (n = 0; n < ARRAY_SIZE(stack_thread); n++) {
128 		canary = &GET_START_CANARY(stack_thread, n);
129 		if (*canary != START_CANARY_VALUE)
130 			CANARY_DIED(stack_thread, start, n, canary);
131 		canary = &GET_END_CANARY(stack_thread, n);
132 		if (*canary != END_CANARY_VALUE)
133 			CANARY_DIED(stack_thread, end, n, canary);
134 	}
135 #endif
136 #endif/*CFG_WITH_STACK_CANARIES*/
137 }
138 
thread_lock_global(void)139 void thread_lock_global(void)
140 {
141 	cpu_spin_lock(&thread_global_lock);
142 }
143 
thread_unlock_global(void)144 void thread_unlock_global(void)
145 {
146 	cpu_spin_unlock(&thread_global_lock);
147 }
148 
149 static struct thread_core_local * __nostackcheck
get_core_local(unsigned int pos)150 get_core_local(unsigned int pos)
151 {
152 	/*
153 	 * Foreign interrupts must be disabled before playing with core_local
154 	 * since we otherwise may be rescheduled to a different core in the
155 	 * middle of this function.
156 	 */
157 	assert(thread_get_exceptions() & THREAD_EXCP_FOREIGN_INTR);
158 
159 	assert(pos < CFG_TEE_CORE_NB_CORE);
160 	return &thread_core_local[pos];
161 }
162 
thread_get_core_local(void)163 struct thread_core_local * __nostackcheck thread_get_core_local(void)
164 {
165 	unsigned int pos = get_core_pos();
166 
167 	return get_core_local(pos);
168 }
169 
170 #ifdef CFG_CORE_DEBUG_CHECK_STACKS
print_stack_limits(void)171 static void print_stack_limits(void)
172 {
173 	size_t n = 0;
174 	vaddr_t __maybe_unused start = 0;
175 	vaddr_t __maybe_unused end = 0;
176 
177 	for (n = 0; n < CFG_TEE_CORE_NB_CORE; n++) {
178 		start = GET_STACK_TOP_SOFT(stack_tmp, n);
179 		end = GET_STACK_BOTTOM(stack_tmp, n);
180 		DMSG("tmp [%zu] 0x%" PRIxVA "..0x%" PRIxVA, n, start, end);
181 	}
182 	for (n = 0; n < CFG_TEE_CORE_NB_CORE; n++) {
183 		start = GET_STACK_TOP_SOFT(stack_abt, n);
184 		end = GET_STACK_BOTTOM(stack_abt, n);
185 		DMSG("abt [%zu] 0x%" PRIxVA "..0x%" PRIxVA, n, start, end);
186 	}
187 	for (n = 0; n < CFG_NUM_THREADS; n++) {
188 		end = threads[n].stack_va_end;
189 		start = end - STACK_THREAD_SIZE + STACK_CHECK_EXTRA;
190 		DMSG("thr [%zu] 0x%" PRIxVA "..0x%" PRIxVA, n, start, end);
191 	}
192 }
193 
check_stack_limits(void)194 static void check_stack_limits(void)
195 {
196 	vaddr_t stack_start = 0;
197 	vaddr_t stack_end = 0;
198 	/* Any value in the current stack frame will do */
199 	vaddr_t current_sp = (vaddr_t)&stack_start;
200 
201 	if (!get_stack_soft_limits(&stack_start, &stack_end))
202 		panic("Unknown stack limits");
203 	if (current_sp < stack_start || current_sp > stack_end) {
204 		EMSG("Stack pointer out of range: 0x%" PRIxVA " not in [0x%"
205 		     PRIxVA " .. 0x%" PRIxVA "]", current_sp, stack_start,
206 		     stack_end);
207 		print_stack_limits();
208 		panic();
209 	}
210 }
211 
get_stackcheck_recursion_flag(void)212 static bool * __nostackcheck get_stackcheck_recursion_flag(void)
213 {
214 	uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR);
215 	unsigned int pos = get_core_pos();
216 	struct thread_core_local *l = get_core_local(pos);
217 	int ct = l->curr_thread;
218 	bool *p = NULL;
219 
220 	if (l->flags & (THREAD_CLF_ABORT | THREAD_CLF_TMP))
221 		p = &l->stackcheck_recursion;
222 	else if (!l->flags)
223 		p = &threads[ct].tsd.stackcheck_recursion;
224 
225 	thread_unmask_exceptions(exceptions);
226 	return p;
227 }
228 
229 void __cyg_profile_func_enter(void *this_fn, void *call_site);
__cyg_profile_func_enter(void * this_fn __unused,void * call_site __unused)230 void __nostackcheck __cyg_profile_func_enter(void *this_fn __unused,
231 					     void *call_site __unused)
232 {
233 	bool *p = get_stackcheck_recursion_flag();
234 
235 	assert(p);
236 	if (*p)
237 		return;
238 	*p = true;
239 	check_stack_limits();
240 	*p = false;
241 }
242 
243 void __cyg_profile_func_exit(void *this_fn, void *call_site);
__cyg_profile_func_exit(void * this_fn __unused,void * call_site __unused)244 void __nostackcheck __cyg_profile_func_exit(void *this_fn __unused,
245 					    void *call_site __unused)
246 {
247 }
248 #else
print_stack_limits(void)249 static void print_stack_limits(void)
250 {
251 }
252 #endif
253 
thread_init_boot_thread(void)254 void thread_init_boot_thread(void)
255 {
256 	struct thread_core_local *l = thread_get_core_local();
257 
258 	thread_init_threads();
259 
260 	l->curr_thread = 0;
261 	threads[0].state = THREAD_STATE_ACTIVE;
262 }
263 
thread_clr_boot_thread(void)264 void __nostackcheck thread_clr_boot_thread(void)
265 {
266 	struct thread_core_local *l = thread_get_core_local();
267 
268 	assert(l->curr_thread >= 0 && l->curr_thread < CFG_NUM_THREADS);
269 	assert(threads[l->curr_thread].state == THREAD_STATE_ACTIVE);
270 	threads[l->curr_thread].state = THREAD_STATE_FREE;
271 	l->curr_thread = THREAD_ID_INVALID;
272 }
273 
thread_get_tmp_sp(void)274 void __nostackcheck *thread_get_tmp_sp(void)
275 {
276 	struct thread_core_local *l = thread_get_core_local();
277 
278 	/*
279 	 * Called from assembly when switching to the temporary stack, so flags
280 	 * need updating
281 	 */
282 	l->flags |= THREAD_CLF_TMP;
283 
284 	return (void *)l->tmp_stack_va_end;
285 }
286 
thread_stack_start(void)287 vaddr_t thread_stack_start(void)
288 {
289 	struct thread_ctx *thr;
290 	int ct = thread_get_id_may_fail();
291 
292 	if (ct == THREAD_ID_INVALID)
293 		return 0;
294 
295 	thr = threads + ct;
296 	return thr->stack_va_end - STACK_THREAD_SIZE;
297 }
298 
thread_stack_size(void)299 size_t thread_stack_size(void)
300 {
301 	return STACK_THREAD_SIZE;
302 }
303 
get_stack_limits(vaddr_t * start,vaddr_t * end,bool hard)304 bool get_stack_limits(vaddr_t *start, vaddr_t *end, bool hard)
305 {
306 	uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR);
307 	unsigned int pos = get_core_pos();
308 	struct thread_core_local *l = get_core_local(pos);
309 	int ct = l->curr_thread;
310 	bool ret = false;
311 
312 	if (l->flags & THREAD_CLF_TMP) {
313 		if (hard)
314 			*start = GET_STACK_TOP_HARD(stack_tmp, pos);
315 		else
316 			*start = GET_STACK_TOP_SOFT(stack_tmp, pos);
317 		*end = GET_STACK_BOTTOM(stack_tmp, pos);
318 		ret = true;
319 	} else if (l->flags & THREAD_CLF_ABORT) {
320 		if (hard)
321 			*start = GET_STACK_TOP_HARD(stack_abt, pos);
322 		else
323 			*start = GET_STACK_TOP_SOFT(stack_abt, pos);
324 		*end = GET_STACK_BOTTOM(stack_abt, pos);
325 		ret = true;
326 	} else if (!l->flags) {
327 		if (ct < 0 || ct >= CFG_NUM_THREADS)
328 			goto out;
329 
330 		*end = threads[ct].stack_va_end;
331 		*start = *end - STACK_THREAD_SIZE;
332 		if (!hard)
333 			*start += STACK_CHECK_EXTRA;
334 		ret = true;
335 	}
336 out:
337 	thread_unmask_exceptions(exceptions);
338 	return ret;
339 }
340 
thread_is_from_abort_mode(void)341 bool thread_is_from_abort_mode(void)
342 {
343 	struct thread_core_local *l = thread_get_core_local();
344 
345 	return (l->flags >> THREAD_CLF_SAVED_SHIFT) & THREAD_CLF_ABORT;
346 }
347 
348 /*
349  * This function should always be accurate, but it might be possible to
350  * implement a more efficient depending on cpu architecture.
351  */
thread_is_in_normal_mode(void)352 bool __weak thread_is_in_normal_mode(void)
353 {
354 	uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR);
355 	struct thread_core_local *l = thread_get_core_local();
356 	bool ret;
357 
358 	/*
359 	 * If any bit in l->flags is set aside from THREAD_CLF_TMP we're
360 	 * handling some exception.
361 	 */
362 	ret = (l->curr_thread != THREAD_ID_INVALID) &&
363 	      !(l->flags & ~THREAD_CLF_TMP);
364 	thread_unmask_exceptions(exceptions);
365 
366 	return ret;
367 }
368 
thread_get_id_may_fail(void)369 short int thread_get_id_may_fail(void)
370 {
371 	/*
372 	 * thread_get_core_local() requires foreign interrupts to be disabled
373 	 */
374 	uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR);
375 	struct thread_core_local *l = thread_get_core_local();
376 	short int ct = l->curr_thread;
377 
378 	thread_unmask_exceptions(exceptions);
379 	return ct;
380 }
381 
thread_get_id(void)382 short int thread_get_id(void)
383 {
384 	short int ct = thread_get_id_may_fail();
385 
386 	/* Thread ID has to fit in a short int */
387 	COMPILE_TIME_ASSERT(CFG_NUM_THREADS <= SHRT_MAX);
388 	assert(ct >= 0 && ct < CFG_NUM_THREADS);
389 	return ct;
390 }
391 
392 #ifdef CFG_WITH_PAGER
init_thread_stacks(void)393 static void init_thread_stacks(void)
394 {
395 	size_t n = 0;
396 
397 	/*
398 	 * Allocate virtual memory for thread stacks.
399 	 */
400 	for (n = 0; n < CFG_NUM_THREADS; n++) {
401 		tee_mm_entry_t *mm = NULL;
402 		vaddr_t sp = 0;
403 		size_t num_pages = 0;
404 		struct fobj *fobj = NULL;
405 
406 		/* Find vmem for thread stack and its protection gap */
407 		mm = tee_mm_alloc(&tee_mm_vcore,
408 				  SMALL_PAGE_SIZE + STACK_THREAD_SIZE);
409 		assert(mm);
410 
411 		/* Claim eventual physical page */
412 		tee_pager_add_pages(tee_mm_get_smem(mm), tee_mm_get_size(mm),
413 				    true);
414 
415 		num_pages = tee_mm_get_bytes(mm) / SMALL_PAGE_SIZE - 1;
416 		fobj = fobj_locked_paged_alloc(num_pages);
417 
418 		/* Add the region to the pager */
419 		tee_pager_add_core_region(tee_mm_get_smem(mm) + SMALL_PAGE_SIZE,
420 					  PAGED_REGION_TYPE_LOCK, fobj);
421 		fobj_put(fobj);
422 
423 		/* init effective stack */
424 		sp = tee_mm_get_smem(mm) + tee_mm_get_bytes(mm);
425 		asan_tag_access((void *)tee_mm_get_smem(mm), (void *)sp);
426 		if (!thread_init_stack(n, sp))
427 			panic("init stack failed");
428 	}
429 }
430 #else
init_thread_stacks(void)431 static void init_thread_stacks(void)
432 {
433 	size_t n;
434 
435 	/* Assign the thread stacks */
436 	for (n = 0; n < CFG_NUM_THREADS; n++) {
437 		if (!thread_init_stack(n, GET_STACK_BOTTOM(stack_thread, n)))
438 			panic("thread_init_stack failed");
439 	}
440 }
441 #endif /*CFG_WITH_PAGER*/
442 
thread_init_threads(void)443 void thread_init_threads(void)
444 {
445 	size_t n = 0;
446 
447 	init_thread_stacks();
448 	print_stack_limits();
449 	pgt_init();
450 
451 	mutex_lockdep_init();
452 
453 	for (n = 0; n < CFG_NUM_THREADS; n++)
454 		TAILQ_INIT(&threads[n].tsd.sess_stack);
455 }
456 
thread_init_thread_core_local(void)457 void __nostackcheck thread_init_thread_core_local(void)
458 {
459 	size_t n = 0;
460 	struct thread_core_local *tcl = thread_core_local;
461 
462 	for (n = 0; n < CFG_TEE_CORE_NB_CORE; n++) {
463 		tcl[n].curr_thread = THREAD_ID_INVALID;
464 		tcl[n].flags = THREAD_CLF_TMP;
465 	}
466 	tcl[0].tmp_stack_va_end = GET_STACK_BOTTOM(stack_tmp, 0);
467 }
468 
thread_init_core_local_stacks(void)469 void thread_init_core_local_stacks(void)
470 {
471 	size_t n = 0;
472 	struct thread_core_local *tcl = thread_core_local;
473 
474 	for (n = 0; n < CFG_TEE_CORE_NB_CORE; n++) {
475 		tcl[n].tmp_stack_va_end = GET_STACK_BOTTOM(stack_tmp, n) -
476 					  STACK_TMP_OFFS;
477 		tcl[n].abt_stack_va_end = GET_STACK_BOTTOM(stack_abt, n);
478 	}
479 }
480 
481 #if defined(CFG_CORE_PAUTH)
thread_init_thread_pauth_keys(void)482 void thread_init_thread_pauth_keys(void)
483 {
484 	size_t n = 0;
485 
486 	for (n = 0; n < CFG_NUM_THREADS; n++)
487 		if (crypto_rng_read(&threads[n].keys, sizeof(threads[n].keys)))
488 			panic("Failed to init thread pauth keys");
489 }
490 
thread_init_core_local_pauth_keys(void)491 void thread_init_core_local_pauth_keys(void)
492 {
493 	struct thread_core_local *tcl = thread_core_local;
494 	size_t n = 0;
495 
496 	for (n = 0; n < CFG_TEE_CORE_NB_CORE; n++)
497 		if (crypto_rng_read(&tcl[n].keys, sizeof(tcl[n].keys)))
498 			panic("Failed to init core local pauth keys");
499 }
500 #endif
501 
thread_get_tsd(void)502 struct thread_specific_data *thread_get_tsd(void)
503 {
504 	return &threads[thread_get_id()].tsd;
505 }
506 
thread_get_ctx_regs(void)507 struct thread_ctx_regs * __nostackcheck thread_get_ctx_regs(void)
508 {
509 	struct thread_core_local *l = thread_get_core_local();
510 
511 	assert(l->curr_thread != THREAD_ID_INVALID);
512 	return &threads[l->curr_thread].regs;
513 }
514 
thread_set_foreign_intr(bool enable)515 void thread_set_foreign_intr(bool enable)
516 {
517 	/* thread_get_core_local() requires foreign interrupts to be disabled */
518 	uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR);
519 	struct thread_core_local *l;
520 
521 	l = thread_get_core_local();
522 
523 	assert(l->curr_thread != THREAD_ID_INVALID);
524 
525 	if (enable) {
526 		threads[l->curr_thread].flags |=
527 					THREAD_FLAGS_FOREIGN_INTR_ENABLE;
528 		thread_set_exceptions(exceptions & ~THREAD_EXCP_FOREIGN_INTR);
529 	} else {
530 		/*
531 		 * No need to disable foreign interrupts here since they're
532 		 * already disabled above.
533 		 */
534 		threads[l->curr_thread].flags &=
535 					~THREAD_FLAGS_FOREIGN_INTR_ENABLE;
536 	}
537 }
538 
thread_restore_foreign_intr(void)539 void thread_restore_foreign_intr(void)
540 {
541 	/* thread_get_core_local() requires foreign interrupts to be disabled */
542 	uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR);
543 	struct thread_core_local *l;
544 
545 	l = thread_get_core_local();
546 
547 	assert(l->curr_thread != THREAD_ID_INVALID);
548 
549 	if (threads[l->curr_thread].flags & THREAD_FLAGS_FOREIGN_INTR_ENABLE)
550 		thread_set_exceptions(exceptions & ~THREAD_EXCP_FOREIGN_INTR);
551 }
552 
alloc_shm(enum thread_shm_type shm_type,size_t size)553 static struct mobj *alloc_shm(enum thread_shm_type shm_type, size_t size)
554 {
555 	switch (shm_type) {
556 	case THREAD_SHM_TYPE_APPLICATION:
557 		return thread_rpc_alloc_payload(size);
558 	case THREAD_SHM_TYPE_KERNEL_PRIVATE:
559 		return thread_rpc_alloc_kernel_payload(size);
560 	case THREAD_SHM_TYPE_GLOBAL:
561 		return thread_rpc_alloc_global_payload(size);
562 	default:
563 		return NULL;
564 	}
565 }
566 
clear_shm_cache_entry(struct thread_shm_cache_entry * ce)567 static void clear_shm_cache_entry(struct thread_shm_cache_entry *ce)
568 {
569 	if (ce->mobj) {
570 		switch (ce->type) {
571 		case THREAD_SHM_TYPE_APPLICATION:
572 			thread_rpc_free_payload(ce->mobj);
573 			break;
574 		case THREAD_SHM_TYPE_KERNEL_PRIVATE:
575 			thread_rpc_free_kernel_payload(ce->mobj);
576 			break;
577 		case THREAD_SHM_TYPE_GLOBAL:
578 			thread_rpc_free_global_payload(ce->mobj);
579 			break;
580 		default:
581 			assert(0); /* "can't happen" */
582 			break;
583 		}
584 	}
585 	ce->mobj = NULL;
586 	ce->size = 0;
587 }
588 
589 static struct thread_shm_cache_entry *
get_shm_cache_entry(enum thread_shm_cache_user user)590 get_shm_cache_entry(enum thread_shm_cache_user user)
591 {
592 	struct thread_shm_cache *cache = &threads[thread_get_id()].shm_cache;
593 	struct thread_shm_cache_entry *ce = NULL;
594 
595 	SLIST_FOREACH(ce, cache, link)
596 		if (ce->user == user)
597 			return ce;
598 
599 	ce = calloc(1, sizeof(*ce));
600 	if (ce) {
601 		ce->user = user;
602 		SLIST_INSERT_HEAD(cache, ce, link);
603 	}
604 
605 	return ce;
606 }
607 
thread_rpc_shm_cache_alloc(enum thread_shm_cache_user user,enum thread_shm_type shm_type,size_t size,struct mobj ** mobj)608 void *thread_rpc_shm_cache_alloc(enum thread_shm_cache_user user,
609 				 enum thread_shm_type shm_type,
610 				 size_t size, struct mobj **mobj)
611 {
612 	struct thread_shm_cache_entry *ce = NULL;
613 	size_t sz = size;
614 	paddr_t p = 0;
615 	void *va = NULL;
616 
617 	if (!size)
618 		return NULL;
619 
620 	ce = get_shm_cache_entry(user);
621 	if (!ce)
622 		return NULL;
623 
624 	/*
625 	 * Always allocate in page chunks as normal world allocates payload
626 	 * memory as complete pages.
627 	 */
628 	sz = ROUNDUP(size, SMALL_PAGE_SIZE);
629 
630 	if (ce->type != shm_type || sz > ce->size) {
631 		clear_shm_cache_entry(ce);
632 
633 		ce->mobj = alloc_shm(shm_type, sz);
634 		if (!ce->mobj)
635 			return NULL;
636 
637 		if (mobj_get_pa(ce->mobj, 0, 0, &p))
638 			goto err;
639 
640 		if (!IS_ALIGNED_WITH_TYPE(p, uint64_t))
641 			goto err;
642 
643 		va = mobj_get_va(ce->mobj, 0, sz);
644 		if (!va)
645 			goto err;
646 
647 		ce->size = sz;
648 		ce->type = shm_type;
649 	} else {
650 		va = mobj_get_va(ce->mobj, 0, sz);
651 		if (!va)
652 			goto err;
653 	}
654 	*mobj = ce->mobj;
655 
656 	return va;
657 err:
658 	clear_shm_cache_entry(ce);
659 	return NULL;
660 }
661 
thread_rpc_shm_cache_clear(struct thread_shm_cache * cache)662 void thread_rpc_shm_cache_clear(struct thread_shm_cache *cache)
663 {
664 	while (true) {
665 		struct thread_shm_cache_entry *ce = SLIST_FIRST(cache);
666 
667 		if (!ce)
668 			break;
669 		SLIST_REMOVE_HEAD(cache, link);
670 		clear_shm_cache_entry(ce);
671 		free(ce);
672 	}
673 }
674