xref: /haiku/src/system/libroot/os/arch/arm/tls.c (revision 52f7c9389475e19fc21487b38064b4390eeb6fea)
1 /*
2  * Copyright 2019-2022, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Copyright 2003, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
6  * Distributed under the terms of the MIT License.
7  */
8 
9 #ifndef _NO_INLINE_ASM
10 #	define _NO_INLINE_ASM 1
11 #endif
12 
13 #include <runtime_loader/runtime_loader.h>
14 
15 #include <support/TLS.h>
16 #include <tls.h>
17 
18 
19 struct tls_index {
20 	unsigned long ti_module;
21 	unsigned long ti_offset;
22 };
23 
24 
25 static int32 gNextSlot = TLS_FIRST_FREE_SLOT;
26 
27 
28 static inline void**
29 get_tls()
30 {
31 	void **tls;
32 	asm volatile("MRC p15, 0, %0, c13, c0, 3" : "=r" (tls));
33 	return tls;
34 }
35 
36 
37 int32
38 tls_allocate(void)
39 {
40 	int32 next = atomic_add(&gNextSlot, 1);
41 	if (next >= TLS_MAX_KEYS)
42 		return B_NO_MEMORY;
43 
44 	return next;
45 }
46 
47 
48 void *
49 tls_get(int32 index)
50 {
51 	return get_tls()[index];
52 }
53 
54 
55 void **
56 tls_address(int32 index)
57 {
58 	return get_tls() + index;
59 }
60 
61 
62 void
63 tls_set(int32 index, void *value)
64 {
65 	get_tls()[index] = value;
66 }
67 
68 
69 void *
70 __tls_get_addr(struct tls_index *ti)
71 {
72 	return __gRuntimeLoader->get_tls_address(ti->ti_module, ti->ti_offset);
73 }
74 
75