xref: /haiku/src/system/libroot/os/arch/x86/tls.c (revision cda5b8808fd0262f0fac472f6cfa809f846a83cf)
1 /*
2  * Copyright 2003, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 // we don't want to have the inline assembly included here
8 #ifndef _NO_INLINE_ASM
9 #	define _NO_INLINE_ASM 1
10 #endif
11 
12 #include "support/TLS.h"
13 #include "tls.h"
14 
15 
16 static int32 gNextSlot = TLS_FIRST_FREE_SLOT;
17 
18 
19 int32
20 tls_allocate(void)
21 {
22 	int32 next = atomic_add(&gNextSlot, 1);
23 	if (next >= TLS_MAX_KEYS)
24 		return B_NO_MEMORY;
25 
26 	return next;
27 }
28 
29 
30 void *
31 tls_get(int32 index)
32 {
33 	void *ret;
34 	__asm__ __volatile__ (
35 		"movl	%%fs:(,%%edx, 4), %%eax \n\t"
36 		: "=a"(ret) : "d"(index) );
37 	return ret;
38 }
39 
40 
41 void **
42 tls_address(int32 index)
43 {
44 	void **ret;
45 	__asm__ __volatile__ (
46 		"movl	%%fs:0, %%eax \n\t"
47 		"leal	(%%eax, %%edx, 4), %%eax \n\t"
48 		: "=a"(ret) : "d"(index) );
49 	return ret;
50 }
51 
52 
53 void
54 tls_set(int32 index, void *value)
55 {
56 	__asm__ __volatile__ (
57 		"movl	%%eax, %%fs:(,%%edx, 4) \n\t"
58 		: : "d"(index), "a"(value) );
59 }
60 
61