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
13struct tls_index {
14	unsigned long ti_module;
15	unsigned long ti_offset;
16};
17
18
19static int32 gNextSlot = TLS_FIRST_FREE_SLOT;
20
21
22static inline void**
23get_tls()
24{
25	void **tls;
26	asm volatile("MRC p15, 0, %0, c13, c0, 3" : "=r" (tls));
27	return tls;
28}
29
30
31int32
32tls_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
42void *
43tls_get(int32 index)
44{
45	return get_tls()[index];
46}
47
48
49void **
50tls_address(int32 index)
51{
52	return get_tls() + index;
53}
54
55
56void
57tls_set(int32 index, void *value)
58{
59	get_tls()[index] = value;
60}
61
62
63void *
64__tls_get_addr(struct tls_index *ti)
65{
66	return __gRuntimeLoader->get_tls_address(ti->ti_module, ti->ti_offset);
67}
68