]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/nios2/nptl/tls.h
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / nios2 / nptl / tls.h
1 /* Definition for thread-local data handling. NPTL/Nios II version.
2 Copyright (C) 2012-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library. If not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #ifndef _TLS_H
20 #define _TLS_H 1
21
22 #include <dl-sysdep.h>
23
24 #ifndef __ASSEMBLER__
25 # include <stdbool.h>
26 # include <stddef.h>
27 # include <stdint.h>
28 # include <dl-dtv.h>
29
30 #else /* __ASSEMBLER__ */
31 # include <tcb-offsets.h>
32 #endif /* __ASSEMBLER__ */
33
34
35 #ifndef __ASSEMBLER__
36
37 /* Get system call information. */
38 # include <sysdep.h>
39
40 /* The TP points to the start of the thread blocks. */
41 # define TLS_DTV_AT_TP 1
42 # define TLS_TCB_AT_TP 0
43
44 /* Get the thread descriptor definition. */
45 # include <nptl/descr.h>
46
47 typedef struct
48 {
49 dtv_t *dtv;
50 uintptr_t pointer_guard;
51 unsigned spare[6];
52 } tcbhead_t;
53
54 register struct pthread *__thread_self __asm__("r23");
55
56 #define READ_THREAD_POINTER() ((void *) __thread_self)
57
58 /* This is the size of the initial TCB. Because our TCB is before the thread
59 pointer, we don't need this. */
60 # define TLS_INIT_TCB_SIZE 0
61
62 /* Alignment requirements for the initial TCB. */
63 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
64
65 /* This is the size of the TCB. Because our TCB is before the thread
66 pointer, we don't need this. */
67 # define TLS_TCB_SIZE 0
68
69 /* Alignment requirements for the TCB. */
70 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
71
72 /* This is the size we need before TCB - actually, it includes the TCB. */
73 # define TLS_PRE_TCB_SIZE \
74 (sizeof (struct pthread) \
75 + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
76
77 /* The thread pointer (in hardware register r23) points to the end of
78 the TCB + 0x7000, as for PowerPC and MIPS. */
79 # define TLS_TCB_OFFSET 0x7000
80
81 /* Install the dtv pointer. The pointer passed is to the element with
82 index -1 which contain the length. */
83 # define INSTALL_DTV(tcbp, dtvp) \
84 (((tcbhead_t *) (tcbp))[-1].dtv = (dtvp) + 1)
85
86 /* Install new dtv for current thread. */
87 # define INSTALL_NEW_DTV(dtv) \
88 (THREAD_DTV() = (dtv))
89
90 /* Return dtv of given thread descriptor. */
91 # define GET_DTV(tcbp) \
92 (((tcbhead_t *) (tcbp))[-1].dtv)
93
94 /* Code to initially initialize the thread pointer. */
95 # define TLS_INIT_TP(tcbp) \
96 (__thread_self = (struct pthread *) ((char *) tcbp + TLS_TCB_OFFSET), NULL)
97
98 /* Value passed to 'clone' for initialization of the thread register. */
99 # define TLS_DEFINE_INIT_TP(tp, pd) \
100 void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE
101
102 /* Return the address of the dtv for the current thread. */
103 # define THREAD_DTV() \
104 (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))[-1].dtv)
105
106 /* Return the thread descriptor for the current thread. */
107 # define THREAD_SELF \
108 ((struct pthread *) (READ_THREAD_POINTER () \
109 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
110
111 /* Magic for libthread_db to know how to do THREAD_SELF. */
112 # define DB_THREAD_SELF \
113 REGISTER (32, 32, 23 * 4, -TLS_PRE_TCB_SIZE - TLS_TCB_OFFSET)
114
115 /* Access to data in the thread descriptor is easy. */
116 # define THREAD_GETMEM(descr, member) \
117 descr->member
118 # define THREAD_GETMEM_NC(descr, member, idx) \
119 descr->member[idx]
120 # define THREAD_SETMEM(descr, member, value) \
121 descr->member = (value)
122 # define THREAD_SETMEM_NC(descr, member, idx, value) \
123 descr->member[idx] = (value)
124
125 # define THREAD_GET_POINTER_GUARD() \
126 (((tcbhead_t *) (READ_THREAD_POINTER () \
127 - TLS_TCB_OFFSET))[-1].pointer_guard)
128 # define THREAD_SET_POINTER_GUARD(value) \
129 (THREAD_GET_POINTER_GUARD () = (value))
130 # define THREAD_COPY_POINTER_GUARD(descr) \
131 (((tcbhead_t *) ((void *) (descr) \
132 + TLS_PRE_TCB_SIZE))[-1].pointer_guard \
133 = THREAD_GET_POINTER_GUARD())
134
135 /* l_tls_offset == 0 is perfectly valid on Nios II, so we have to use some
136 different value to mean unset l_tls_offset. */
137 # define NO_TLS_OFFSET -1
138
139 /* Get and set the global scope generation counter in struct pthread. */
140 #define THREAD_GSCOPE_IN_TCB 1
141 #define THREAD_GSCOPE_FLAG_UNUSED 0
142 #define THREAD_GSCOPE_FLAG_USED 1
143 #define THREAD_GSCOPE_FLAG_WAIT 2
144 #define THREAD_GSCOPE_RESET_FLAG() \
145 do \
146 { int __res \
147 = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
148 THREAD_GSCOPE_FLAG_UNUSED); \
149 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
150 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
151 } \
152 while (0)
153 #define THREAD_GSCOPE_SET_FLAG() \
154 do \
155 { \
156 THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
157 atomic_write_barrier (); \
158 } \
159 while (0)
160 #define THREAD_GSCOPE_WAIT() \
161 GL(dl_wait_lookup_done) ()
162
163 #endif /* __ASSEMBLER__ */
164
165 #endif /* tls.h */