1 /*
2  * Copyright (c) 2018 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Thread entry
10  *
11  * This file provides the common thread entry function
12  */
13 
14 #include <zephyr/kernel.h>
15 #ifdef CONFIG_CURRENT_THREAD_USE_TLS
16 #include <zephyr/random/random.h>
17 
18 __thread k_tid_t z_tls_current;
19 #endif
20 
21 #ifdef CONFIG_STACK_CANARIES_TLS
22 extern __thread volatile uintptr_t __stack_chk_guard;
23 #endif /* CONFIG_STACK_CANARIES_TLS */
24 
25 /*
26  * Common thread entry point function (used by all threads)
27  *
28  * This routine invokes the actual thread entry point function and passes
29  * it three arguments. It also handles graceful termination of the thread
30  * if the entry point function ever returns.
31  *
32  * This routine does not return, and is marked as such so the compiler won't
33  * generate preamble code that is only used by functions that actually return.
34  */
z_thread_entry(k_thread_entry_t entry,void * p1,void * p2,void * p3)35 FUNC_NORETURN void z_thread_entry(k_thread_entry_t entry,
36 				 void *p1, void *p2, void *p3)
37 {
38 #ifdef CONFIG_CURRENT_THREAD_USE_TLS
39 	z_tls_current = k_sched_current_thread_query();
40 #endif
41 #ifdef CONFIG_STACK_CANARIES_TLS
42 	uintptr_t stack_guard;
43 
44 	sys_rand_get((uint8_t *)&stack_guard, sizeof(stack_guard));
45 	__stack_chk_guard = stack_guard;
46 	__stack_chk_guard <<= 8;
47 #endif	/* CONFIG_STACK_CANARIES */
48 	entry(p1, p2, p3);
49 
50 	k_thread_abort(k_current_get());
51 
52 	/*
53 	 * Compiler can't tell that k_thread_abort() won't return and issues a
54 	 * warning unless we tell it that control never gets this far.
55 	 */
56 
57 	CODE_UNREACHABLE; /* LCOV_EXCL_LINE */
58 }
59