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