From 831e32af8cc20041086903fce78d77fff64a1aa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Mon, 6 Jan 2003 08:05:09 +0000 Subject: [PATCH] Userland part of the TLS implementation for x86. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@2362 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kernel/libroot/os/Jamfile | 1 + src/kernel/libroot/os/tls.c | 72 +++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 src/kernel/libroot/os/tls.c diff --git a/src/kernel/libroot/os/Jamfile b/src/kernel/libroot/os/Jamfile index e6595b7b6a..ad9c01fb3f 100644 --- a/src/kernel/libroot/os/Jamfile +++ b/src/kernel/libroot/os/Jamfile @@ -11,6 +11,7 @@ KernelMergeObject os_main.o : <$(SOURCE_GRIST)>team.c <$(SOURCE_GRIST)>thread.c <$(SOURCE_GRIST)>time.c + <$(SOURCE_GRIST)>tls.c <$(SOURCE_GRIST)>syscalls.S : -fPIC -DPIC diff --git a/src/kernel/libroot/os/tls.c b/src/kernel/libroot/os/tls.c new file mode 100644 index 0000000000..59f91be833 --- /dev/null +++ b/src/kernel/libroot/os/tls.c @@ -0,0 +1,72 @@ +/* +** Copyright 2002, Axel Dörfler, axeld@pinc-software.de. All rights reserved. +** Distributed under the terms of the OpenBeOS License. +*/ + + +// we don't want to have the inline assembly included here +#ifndef _NO_INLINE_ASM +# define _NO_INLINE_ASM 1 +#endif + +#include "support/TLS.h" + + +// ToDo: we probably need to set some standard slots (like it's done in BeOS)! +// Slot 1 is obviously the thread ID, but there are 8 pre-allocated slots in BeOS. +// I have no idea what the other slots are used for; they are all NULL when the +// thread has just started, but slots 0 & 1. +// Slot 0 seems to be an address somewhere on the stack. Another slot is most probably +// reserved for "errno", the others could have been used to make some POSIX calls +// thread-safe - but we also have to find out which supposely non-thread-safe POSIX +// calls are thread-safe in BeOS. +static int32 gNextSlot = 0; + + +int32 +tls_allocate(void) +{ + int32 next = atomic_add(&gNextSlot, 1); + if (next >= TLS_MAX_KEYS) + return B_NO_MEMORY; + + return next; +} + + +#if __INTEL__ && __GNUC__ + +void * +tls_get(int32 index) +{ + void *ret; + __asm__ __volatile__ ( + "movl %%fs:(,%%edx, 4), %%eax \n\t" + : "=a"(ret) : "d"(index) ); + return ret; +} + + +void ** +tls_address(int32 index) +{ + void *ret; + __asm__ __volatile__ ( + "movl %%fs:0, %%eax \n\t" + "leal (%%eax, %%edx, 4), %%eax \n\t" + : "=a"(ret) : "d"(index) ); + return ret; +} + + +void +tls_set(int32 index, void *value) +{ + __asm__ __volatile__ ( + "movl %%eax, %%fs:(,%%edx, 4) \n\t" + : : "d"(index), "a"(value) ); +} + +#else +# error "TLS has not been implemented for this platform" +#endif