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