Skip to content

Commit

Permalink
!tls: move tls handling to libphoenix
Browse files Browse the repository at this point in the history
JIRA: RTOS-921
  • Loading branch information
badochov committed Sep 24, 2024
1 parent 96c4c08 commit 8b89a84
Show file tree
Hide file tree
Showing 30 changed files with 622 additions and 26 deletions.
2 changes: 1 addition & 1 deletion arch/arm/v7a/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# Author: Pawel Pisarczyk
#

OBJS += $(addprefix $(PREFIX_O)arch/arm/v7a/, syscalls.o reboot.o)
OBJS += $(addprefix $(PREFIX_O)arch/arm/v7a/, syscalls.o reboot.o tls.o)
39 changes: 39 additions & 0 deletions arch/arm/v7a/tls.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Phoenix-RTOS
*
* libphoenix
*
* tls (armv7a)
*
* Copyright 2024 Phoenix Systems
* Author: Hubert Badocha
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/


#include <sys/tls.h>


struct tls_tcb *__tls_getTcb(void)
{
void *tcb;
/* clang-format off */
__asm__ volatile("mrc p15, 0, %0, cr13, cr0, 3" : "=r"(tcb));
/* clang-format on */
return tcb;
}


void *__tls_tcbPtrToTlsPtr(struct tls_tcb *tcb)
{
return (void *)tcb;
}


void __tls_archInit(void)
{
/* Nothing to do. */
}
22 changes: 21 additions & 1 deletion arch/arm/v7m/tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
* %LICENSE%
*/


#include <sys/types.h>
#include <sys/tls.h>


volatile uintptr_t arm_tls_ptr __attribute__((section("armtls"))) = 0;
volatile uintptr_t arm_tls_ptr = 0;


void *read_tls_ptr(void)
Expand All @@ -32,3 +34,21 @@ void __attribute__((naked)) __aeabi_read_tp(void)
"bl read_tls_ptr;"
"pop {r1-r4,r12,pc}");
}


struct tls_tcb *__tls_getTcb(void)
{
return (struct tls_tcb *)read_tls_ptr();
}


void *__tls_tcbPtrToTlsPtr(struct tls_tcb *tcb)
{
return (void *)tcb;
}


void __tls_archInit(void)
{
tlsSetReg((void *)&arm_tls_ptr);
}
22 changes: 21 additions & 1 deletion arch/arm/v8m/tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
* %LICENSE%
*/


#include <sys/types.h>
#include <sys/tls.h>


volatile uintptr_t arm_tls_ptr __attribute__((section("armtls"))) = 0;
volatile uintptr_t arm_tls_ptr = 0;


void *read_tls_ptr(void)
Expand All @@ -32,3 +34,21 @@ void __attribute__((naked)) __aeabi_read_tp(void)
"bl read_tls_ptr;"
"pop {r1-r4,r12,pc}");
}


struct tls_tcb *__tls_getTcb(void)
{
return (struct tls_tcb *)read_tls_ptr();
}


void *__tls_tcbPtrToTlsPtr(struct tls_tcb *tcb)
{
return (void *)tcb;
}


void __tls_archInit(void)
{
tlsSetReg((void *)&arm_tls_ptr);
}
2 changes: 1 addition & 1 deletion arch/arm/v8r/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
# %LICENSE%
#

OBJS += $(addprefix $(PREFIX_O)arch/arm/v8r/, syscalls.o reboot.o)
OBJS += $(addprefix $(PREFIX_O)arch/arm/v8r/, syscalls.o reboot.o tls.o)
39 changes: 39 additions & 0 deletions arch/arm/v8r/tls.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Phoenix-RTOS
*
* libphoenix
*
* tls (armv8r)
*
* Copyright 2024 Phoenix Systems
* Author: Hubert Badocha
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/


#include <sys/tls.h>


struct tls_tcb *__tls_getTcb(void)
{
void *tcb;
/* clang-format off */
__asm__ volatile("mrc p15, 0, %0, cr13, cr0, 3" : "=r"(tcb));
/* clang-format on */
return tcb;
}


void *__tls_tcbPtrToTlsPtr(struct tls_tcb *tcb)
{
return (void *)tcb;
}


void __tls_archInit(void)
{
/* Nothing to do. */
}
2 changes: 1 addition & 1 deletion arch/ia32/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
# Author: Pawel Pisarczyk
#

OBJS += $(addprefix $(PREFIX_O)arch/ia32/, syscalls.o jmp.o signal.o string.o reboot.o)
OBJS += $(addprefix $(PREFIX_O)arch/ia32/, syscalls.o jmp.o signal.o string.o reboot.o tls.o)
CRT0_OBJS += $(addprefix $(PREFIX_O)arch/ia32/, crt0.o)
39 changes: 39 additions & 0 deletions arch/ia32/tls.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Phoenix-RTOS
*
* libphoenix
*
* tls (ia32)
*
* Copyright 2024 Phoenix Systems
* Author: Hubert Badocha
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/


#include <sys/tls.h>


struct tls_tcb *__tls_getTcb(void)
{
struct tls_tcb *tcb;
/* clang-format off */
__asm__ volatile("movl %%gs:0, %0" : "=r"(tcb));
/* clang-format on */
return tcb;
}


void *__tls_tcbPtrToTlsPtr(struct tls_tcb *tcb)
{
return (void *)tcb;
}


void __tls_archInit(void)
{
/* Nothing to do. */
}
2 changes: 1 addition & 1 deletion arch/riscv64/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
# Author: Pawel Pisarczyk
#

OBJS += $(addprefix $(PREFIX_O)arch/riscv64/, syscalls.o string.o signal.o reboot.o jmp.o)
OBJS += $(addprefix $(PREFIX_O)arch/riscv64/, syscalls.o string.o signal.o reboot.o jmp.o tls.o)
CRT0_OBJS += $(addprefix $(PREFIX_O)arch/riscv64/, crt0.o)
43 changes: 43 additions & 0 deletions arch/riscv64/tls.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Phoenix-RTOS
*
* libphoenix
*
* tls (riscv64)
*
* Copyright 2024 Phoenix Systems
* Author: Hubert Badocha
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/


#include <sys/tls.h>


/* As per RISC-V ABI:
* RISC-V uses Variant I as described by the ELF TLS specification, with tp containing the address one past the end of the TCB */
struct tls_tcb *__tls_getTcb(void)
{
void *tcb;
/* clang-format off */
__asm__ volatile("addi %[tcb], tp, %[offset]"
: [tcb] "=r"(tcb)
: [offset] "n"(-sizeof(struct tls_tcb)));
/* clang-format on */
return tcb;
}


void *__tls_tcbPtrToTlsPtr(struct tls_tcb *tcb)
{
return (void *)(tcb + 1);
}


void __tls_archInit(void)
{
/* Nothing to do. */
}
2 changes: 1 addition & 1 deletion arch/sparcv8leon3/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
# Author: Lukasz Leczkowski
#

OBJS += $(addprefix $(PREFIX_O)arch/sparcv8leon3/, syscalls.o jmp.o signal.o string.o reboot.o)
OBJS += $(addprefix $(PREFIX_O)arch/sparcv8leon3/, syscalls.o jmp.o signal.o string.o reboot.o tls.o)
CRT0_OBJS += $(addprefix $(PREFIX_O)arch/sparcv8leon3/, crt0.o)
36 changes: 36 additions & 0 deletions arch/sparcv8leon3/tls.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Phoenix-RTOS
*
* libphoenix
*
* tls (sparc)
*
* Copyright 2024 Phoenix Systems
* Author: Hubert Badocha
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/


#include <sys/tls.h>


struct tls_tcb *__tls_getTcb(void)
{
register struct tls_tcb *tcb asm("g7");

return tcb;
}


void *__tls_tcbPtrToTlsPtr(struct tls_tcb *tcb)
{
return (void *)tcb;
}


void __tls_archInit(void) {
/* Nothing to do. */
}
13 changes: 13 additions & 0 deletions crt0-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,26 @@ extern int main(int argc, char **argv);

char **environ;
const char *argv_progname;
char **auxvStart; /* char** instead of void* to make iteration easier and don't waste any stack space. */


__attribute__((noreturn)) void _startc(void (*cleanup)(void), int argc, char **argv, char **env)
{
environ = env;
argv_progname = *argv;

if (env == NULL) {
auxvStart = argv;
}
else {
auxvStart = env;
}

while ((*auxvStart) != NULL) {
auxvStart++;
}
auxvStart++;

_libc_init();

/* cleanup function is not NULL when the dynamic linker is used */
Expand Down
15 changes: 9 additions & 6 deletions errno/errno.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@

#include <sys/rb.h>
#include <sys/threads.h>
#include <sys/tls.h>
#include <errno.h>
#include <arch.h>


#ifdef __LIBPHOENIX_ARCH_TLS_SUPPORTED
#ifdef __LIBPHOENIX_ARCH_USE_TLS

static __thread int __errno_tls;
static int __errno_singlethread;

#else

Expand All @@ -45,7 +47,7 @@ static int errno_cmp(rbnode_t *n1, rbnode_t *n2)

int *__errno_location(void)
{
#ifndef __LIBPHOENIX_ARCH_TLS_SUPPORTED
#ifndef __LIBPHOENIX_ARCH_USE_TLS
struct __errno_t *e, r;
r.tid = gettid();

Expand All @@ -58,14 +60,15 @@ int *__errno_location(void)

return &errno_global;
#else
return &__errno_tls;
/* Errno maybe called before TLS is initialized(malloc) */
return (__isthreaded == 0) ? &__errno_singlethread : &__errno_tls;
#endif
}


void _errno_new(struct __errno_t *e)
{
#ifndef __LIBPHOENIX_ARCH_TLS_SUPPORTED
#ifndef __LIBPHOENIX_ARCH_USE_TLS
e->no = 0;
e->tid = gettid();

Expand All @@ -78,7 +81,7 @@ void _errno_new(struct __errno_t *e)

void _errno_remove(struct __errno_t *e)
{
#ifndef __LIBPHOENIX_ARCH_TLS_SUPPORTED
#ifndef __LIBPHOENIX_ARCH_USE_TLS
mutexLock(errno_common.lock);
lib_rbRemove(&errno_common.tree, &e->linkage);
mutexUnlock(errno_common.lock);
Expand All @@ -88,7 +91,7 @@ void _errno_remove(struct __errno_t *e)

void _errno_init(void)
{
#ifndef __LIBPHOENIX_ARCH_TLS_SUPPORTED
#ifndef __LIBPHOENIX_ARCH_USE_TLS
mutexCreate(&errno_common.lock);
lib_rbInit(&errno_common.tree, errno_cmp, NULL);
#endif
Expand Down
Loading

0 comments on commit 8b89a84

Please sign in to comment.