x86-64: Early TLS initialization

Allow early boot code to use thread local storage
when it is enabled.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
This commit is contained in:
Flavio Ceolin 2023-08-02 21:32:26 -07:00 committed by Chris Friedt
parent 596e77f562
commit 20c9bb856f
3 changed files with 37 additions and 1 deletions

View file

@ -17,5 +17,5 @@ zephyr_library_sources(
)
zephyr_library_sources_ifdef(CONFIG_USERSPACE intel64/userspace.S)
zephyr_library_sources_ifdef(CONFIG_THREAD_LOCAL_STORAGE intel64/tls.c)
zephyr_library_sources_ifdef(CONFIG_DEBUG_COREDUMP intel64/coredump.c)

View file

@ -261,6 +261,11 @@ enter_code64:
/* Enter C domain now that we have a stack set up, never to return */
movq %rbp, %rdi
#ifdef CONFIG_STACK_CANARIES_TLS
pushq %rsp
call z_x86_early_tls_update_gdt
popq %rsp
#endif
call z_x86_cpu_init
/* 64 bit OS entry point, used by EFI support. UEFI

View file

@ -0,0 +1,31 @@
/*
* Copyright (c) 2023 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <kernel_internal.h>
FUNC_NO_STACK_PROTECTOR
void z_x86_early_tls_update_gdt(char *stack_ptr)
{
uintptr_t *self_ptr;
uint32_t fs_base = X86_FS_BASE;
/*
* Since we are populating things backwards, store
* the pointer to the TLS area at top of stack.
*/
stack_ptr -= sizeof(uintptr_t);
self_ptr = (void *)stack_ptr;
*self_ptr = POINTER_TO_UINT(stack_ptr);
__asm__ volatile(
"movl %0, %%ecx;\n\t"
"movq %1, %%rax;\n\t"
"movq %1, %%rdx;\n\t"
"shrq $32, %%rdx;\n\t"
"wrmsr;\n\t"
:
: "r"(fs_base), "r"(POINTER_TO_UINT(self_ptr)));
}