1 /*
2  * Copyright (c) 2020 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/kernel_structs.h>
9 #include <kernel_internal.h>
10 #include <kernel_tls.h>
11 #include <zephyr/sys/util.h>
12 
13 #ifdef __CCAC__
14 extern char _arcmwdt_tls_start[];
15 extern char _arcmwdt_tls_size[];
16 
arch_tls_stack_setup(struct k_thread * new_thread,char * stack_ptr)17 size_t arch_tls_stack_setup(struct k_thread *new_thread, char *stack_ptr)
18 {
19 	size_t tls_size = (size_t)_arcmwdt_tls_size;
20 	size_t tls_size_aligned = ROUND_UP(tls_size, ARCH_STACK_PTR_ALIGN);
21 
22 	stack_ptr -= tls_size_aligned;
23 	memcpy(stack_ptr, _arcmwdt_tls_start, tls_size);
24 
25 	new_thread->tls = POINTER_TO_UINT(stack_ptr);
26 
27 	return tls_size_aligned;
28 }
29 
_mwget_tls(void)30 void *_Preserve_flags _mwget_tls(void)
31 {
32 	return (void *)(_current->tls);
33 }
34 
35 #else
arch_tls_stack_setup(struct k_thread * new_thread,char * stack_ptr)36 size_t arch_tls_stack_setup(struct k_thread *new_thread, char *stack_ptr)
37 {
38 	/*
39 	 * TLS area for ARC has some data fields following by
40 	 * thread data and bss. These fields are supposed to be
41 	 * used by toolchain and OS TLS code to aid in locating
42 	 * the TLS data/bss. Zephyr currently has no use for
43 	 * this so we can simply skip these. However, since GCC
44 	 * is generating code assuming these fields are there,
45 	 * we simply skip them when setting the TLS pointer.
46 	 */
47 
48 	/*
49 	 * Since we are populating things backwards,
50 	 * setup the TLS data/bss area first.
51 	 */
52 	stack_ptr -= z_tls_data_size();
53 	z_tls_copy(stack_ptr);
54 
55 	/* Skip two pointers due to toolchain */
56 	stack_ptr -= sizeof(uintptr_t) * 2;
57 
58 	/*
59 	 * Set thread TLS pointer which is used in
60 	 * context switch to point to TLS area.
61 	 */
62 	new_thread->tls = POINTER_TO_UINT(stack_ptr);
63 
64 	return (z_tls_data_size() + (sizeof(uintptr_t) * 2));
65 }
66 #endif
67