xref: /haiku/src/system/libroot/os/arch/riscv64/tls.cpp (revision 830f67ef991407f287dbc1238aa5f5906d90c991)
1 /*
2  * Copyright 2019-2021, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include <runtime_loader/runtime_loader.h>
7 
8 #include "support/TLS.h"
9 #include "tls.h"
10 
11 
12 struct tls_index {
13 	unsigned long ti_module;
14 	unsigned long ti_offset;
15 };
16 
17 extern "C" void* __tls_get_addr(struct tls_index* ti);
18 
19 // TODO: use std::atomic<int> here like x86_64
20 static int32 gNextSlot = TLS_FIRST_FREE_SLOT;
21 
22 
23 static inline void**
24 get_tls()
25 {
26 	void** tls;
27 	__asm__ __volatile__ ("mv	%0, tp" : "=r" (tls));
28 	return tls;
29 }
30 
31 
32 int32
33 tls_allocate(void)
34 {
35 	if (gNextSlot < TLS_MAX_KEYS) {
36 		int32 next = gNextSlot++;
37 		if (next < TLS_MAX_KEYS)
38 			return next;
39 	}
40 
41 	return B_NO_MEMORY;
42 }
43 
44 
45 void *
46 tls_get(int32 index)
47 {
48 	return get_tls()[index];
49 }
50 
51 
52 void **
53 tls_address(int32 index)
54 {
55 	return get_tls() + index;
56 }
57 
58 
59 void
60 tls_set(int32 index, void *value)
61 {
62 	get_tls()[index] = value;
63 }
64 
65 
66 void*
67 __tls_get_addr(struct tls_index* ti)
68 {
69 	return __gRuntimeLoader->get_tls_address(ti->ti_module, ti->ti_offset);
70 }
71