arc: enable thread local storage

This adds the necessary bits to support thread local storage
in ARC.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
This commit is contained in:
Daniel Leung 2020-10-30 10:21:40 -07:00 committed by Carles Cufí
commit c7704d8c66
6 changed files with 79 additions and 1 deletions

View file

@ -21,6 +21,7 @@ config ARC
select ARCH_IS_SET
select HAS_DTS
imply XIP
select ARCH_HAS_THREAD_LOCAL_STORAGE
help
ARC architecture

View file

@ -12,4 +12,8 @@ zephyr_cc_option(-fno-delete-null-pointer-checks)
zephyr_cc_option_ifdef(CONFIG_ARC_USE_UNALIGNED_MEM_ACCESS -munaligned-access)
# Instruct compiler to use register R26 as thread pointer
# for thread local storage.
zephyr_cc_option_ifdef(CONFIG_THREAD_LOCAL_STORAGE -mtp-regno=26)
add_subdirectory(core)

View file

@ -32,6 +32,8 @@ zephyr_library_sources_ifdef(CONFIG_USERSPACE userspace.S)
zephyr_library_sources_ifdef(CONFIG_ARC_CONNECT arc_connect.c)
zephyr_library_sources_ifdef(CONFIG_ARC_CONNECT arc_smp.c)
zephyr_library_sources_ifdef(CONFIG_THREAD_LOCAL_STORAGE tls.c)
add_subdirectory_ifdef(CONFIG_ARC_CORE_MPU mpu)
add_subdirectory_ifdef(CONFIG_ARC_SECURE_FIRMWARE secureshield)

View file

@ -108,6 +108,25 @@ static struct init_stack_frame *get_iframe(struct k_thread *thread,
return Z_STACK_PTR_TO_FRAME(struct init_stack_frame, stack_ptr);
}
/*
* Pre-populate values in the registers inside _callee_saved_stack struct
* so these registers have pre-defined values when new thread begins
* execution. For example, setting up the thread pointer for thread local
* storage here so the thread starts with thread pointer already set up.
*/
static inline void arch_setup_callee_saved_regs(struct k_thread *thread,
uintptr_t stack_ptr)
{
_callee_saved_stack_t *regs = UINT_TO_POINTER(stack_ptr);
ARG_UNUSED(regs);
#ifdef CONFIG_THREAD_LOCAL_STORAGE
/* R26 is used for thread pointer */
regs->r26 = thread->tls;
#endif
}
/*
* The initial context is a basic stack frame that contains arguments for
* z_thread_entry() return address, that points at z_thread_entry()
@ -164,6 +183,9 @@ void arch_new_thread(struct k_thread *thread, k_thread_stack_t *stack,
thread->arch.relinquish_cause = _CAUSE_COOP;
thread->callee_saved.sp =
(uint32_t)iframe - ___callee_saved_stack_t_SIZEOF;
arch_setup_callee_saved_regs(thread, thread->callee_saved.sp);
/* initial values in all other regs/k_thread entries are irrelevant */
}

42
arch/arc/core/tls.c Normal file
View file

@ -0,0 +1,42 @@
/*
* Copyright (c) 2020 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <kernel.h>
#include <kernel_structs.h>
#include <kernel_internal.h>
#include <kernel_tls.h>
#include <sys/util.h>
size_t arch_tls_stack_setup(struct k_thread *new_thread, char *stack_ptr)
{
/*
* TLS area for ARC has some data fields following by
* thread data and bss. These fields are supposed to be
* used by toolchain and OS TLS code to aid in locating
* the TLS data/bss. Zephyr currently has no use for
* this so we can simply skip these. However, since GCC
* is generating code assuming these fields are there,
* we simply skip them when setting the TLS pointer.
*/
/*
* Since we are populating things backwards,
* setup the TLS data/bss area first.
*/
stack_ptr -= z_tls_data_size();
z_tls_copy(stack_ptr);
/* Skip two pointers due to toolchain */
stack_ptr -= sizeof(uintptr_t) * 2;
/*
* Set thread TLS pointer which is used in
* context switch to point to TLS area.
*/
new_thread->tls = POINTER_TO_UINT(stack_ptr);
return (z_tls_data_size() + (sizeof(uintptr_t) * 2));
}

View file

@ -98,6 +98,7 @@ SECTIONS {
_image_rodata_start = .;
#include <linker/common-rom.ld>
#include <linker/thread-local-storage.ld>
SECTION_PROLOGUE(_RODATA_SECTION_NAME,,) {
KEEP(*(.openocd_dbg))
@ -220,5 +221,11 @@ SECTIONS {
KEEP(*(.gnu.attributes))
}
/DISCARD/ : { *(.note.GNU-stack) }
/DISCARD/ : {
*(.note.GNU-stack)
*(.got.plt)
*(.igot.plt)
*(.got)
*(.igot)
}
}