]>
Commit | Line | Data |
---|---|---|
d4697bc9 | 1 | /* Copyright (C) 2011-2014 Free Software Foundation, Inc. |
63d143a2 CM |
2 | This file is part of the GNU C Library. |
3 | Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011. | |
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 | |
ab84e3ff PE |
16 | License along with the GNU C Library. If not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
63d143a2 CM |
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 | ||
29 | /* Type for the dtv. */ | |
30 | typedef union dtv | |
31 | { | |
32 | size_t counter; | |
33 | struct | |
34 | { | |
35 | void *val; | |
36 | bool is_static; | |
37 | } pointer; | |
38 | } dtv_t; | |
39 | ||
40 | #else /* __ASSEMBLER__ */ | |
41 | # include <tcb-offsets.h> | |
42 | #endif /* __ASSEMBLER__ */ | |
43 | ||
44 | ||
45 | #ifndef __ASSEMBLER__ | |
46 | ||
47 | /* Get system call information. */ | |
48 | # include <sysdep.h> | |
49 | ||
50 | /* The TP points to the start of the thread blocks. */ | |
51 | # define TLS_DTV_AT_TP 1 | |
498a2233 | 52 | # define TLS_TCB_AT_TP 0 |
63d143a2 CM |
53 | |
54 | /* We use the multiple_threads field in the pthread struct */ | |
55 | #define TLS_MULTIPLE_THREADS_IN_TCB 1 | |
56 | ||
57 | /* Get the thread descriptor definition. */ | |
58 | # include <nptl/descr.h> | |
59 | ||
60 | /* The stack_guard is accessed directly by GCC -fstack-protector code, | |
61 | so it is a part of public ABI. The dtv and pointer_guard fields | |
62 | are private. */ | |
63 | typedef struct | |
64 | { | |
65 | void *feedback_data; | |
66 | uintptr_t pointer_guard; | |
67 | uintptr_t stack_guard; | |
68 | dtv_t *dtv; | |
69 | } tcbhead_t; | |
70 | ||
71 | /* This is the size of the initial TCB. Because our TCB is before the thread | |
72 | pointer, we don't need this. */ | |
73 | # define TLS_INIT_TCB_SIZE 0 | |
74 | ||
75 | /* Alignment requirements for the initial TCB. */ | |
76 | # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread) | |
77 | ||
78 | /* This is the size of the TCB. Because our TCB is before the thread | |
79 | pointer, we don't need this. */ | |
80 | # define TLS_TCB_SIZE 0 | |
81 | ||
82 | /* Alignment requirements for the TCB. */ | |
83 | # define TLS_TCB_ALIGN __alignof__ (struct pthread) | |
84 | ||
85 | /* This is the size we need before TCB - actually, it includes the TCB. */ | |
86 | # define TLS_PRE_TCB_SIZE \ | |
87 | (sizeof (struct pthread) \ | |
88 | + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1))) | |
89 | ||
90 | /* Return the thread descriptor (tp) for the current thread. */ | |
91 | register void *__thread_pointer asm ("tp"); | |
92 | ||
93 | /* The thread pointer (in hardware register tp) points to the end of | |
94 | the TCB. The pthread_descr structure is immediately in front of the TCB. */ | |
95 | # define TLS_TCB_OFFSET 0 | |
96 | ||
97 | /* Install the dtv pointer. The pointer passed is to the element with | |
98 | index -1 which contain the length. */ | |
99 | # define INSTALL_DTV(tcbp, dtvp) \ | |
100 | (((tcbhead_t *) (tcbp))[-1].dtv = (dtvp) + 1) | |
101 | ||
102 | /* Install new dtv for current thread. */ | |
103 | # define INSTALL_NEW_DTV(dtv) (THREAD_DTV() = (dtv)) | |
104 | ||
105 | /* Return dtv of given thread descriptor. */ | |
106 | # define GET_DTV(tcbp) (((tcbhead_t *) (tcbp))[-1].dtv) | |
107 | ||
108 | /* Code to initially initialize the thread pointer (tp). */ | |
109 | # define TLS_INIT_TP(tcbp, secondcall) \ | |
110 | (__thread_pointer = (char *)(tcbp) + TLS_TCB_OFFSET, NULL) | |
111 | ||
112 | /* Return the address of the dtv for the current thread. */ | |
113 | # define THREAD_DTV() \ | |
114 | (((tcbhead_t *) (__thread_pointer - TLS_TCB_OFFSET))[-1].dtv) | |
115 | ||
116 | /* Return the thread descriptor for the current thread. */ | |
117 | # define THREAD_SELF \ | |
118 | ((struct pthread *) (__thread_pointer \ | |
119 | - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)) | |
120 | ||
121 | /* Magic for libthread_db to know how to do THREAD_SELF. */ | |
122 | #ifdef __tilegx__ | |
123 | # define DB_THREAD_SELF \ | |
124 | REGISTER (64, 64, REG_TP * 8, - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE) | |
125 | #else | |
126 | # define DB_THREAD_SELF \ | |
127 | REGISTER (32, 32, REG_TP * 4, - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE) | |
128 | #endif | |
129 | ||
130 | /* Read member of the thread descriptor directly. */ | |
131 | # define THREAD_GETMEM(descr, member) (descr->member) | |
132 | ||
133 | /* Same as THREAD_GETMEM, but the member offset can be non-constant. */ | |
134 | # define THREAD_GETMEM_NC(descr, member, idx) \ | |
135 | (descr->member[idx]) | |
136 | ||
137 | /* Set member of the thread descriptor directly. */ | |
138 | # define THREAD_SETMEM(descr, member, value) \ | |
139 | (descr->member = (value)) | |
140 | ||
141 | /* Same as THREAD_SETMEM, but the member offset can be non-constant. */ | |
142 | # define THREAD_SETMEM_NC(descr, member, idx, value) \ | |
143 | (descr->member[idx] = (value)) | |
144 | ||
145 | /* Set the stack guard field in TCB head. */ | |
146 | # define THREAD_SET_STACK_GUARD(value) \ | |
147 | (((tcbhead_t *) ((char *) __thread_pointer \ | |
148 | - TLS_TCB_OFFSET))[-1].stack_guard = (value)) | |
149 | # define THREAD_COPY_STACK_GUARD(descr) \ | |
150 | (((tcbhead_t *) ((char *) (descr) \ | |
151 | + TLS_PRE_TCB_SIZE))[-1].stack_guard \ | |
152 | = ((tcbhead_t *) ((char *) __thread_pointer \ | |
153 | - TLS_TCB_OFFSET))[-1].stack_guard) | |
154 | ||
155 | /* Set the pointer guard field in TCB head. */ | |
156 | # define THREAD_GET_POINTER_GUARD() \ | |
157 | (((tcbhead_t *) ((char *) __thread_pointer \ | |
158 | - TLS_TCB_OFFSET))[-1].pointer_guard) | |
159 | # define THREAD_SET_POINTER_GUARD(value) \ | |
160 | (THREAD_GET_POINTER_GUARD () = (value)) | |
161 | # define THREAD_COPY_POINTER_GUARD(descr) \ | |
162 | (((tcbhead_t *) ((char *) (descr) \ | |
163 | + TLS_PRE_TCB_SIZE))[-1].pointer_guard \ | |
164 | = THREAD_GET_POINTER_GUARD()) | |
165 | ||
166 | /* l_tls_offset == 0 is perfectly valid on Tile, so we have to use some | |
167 | different value to mean unset l_tls_offset. */ | |
168 | # define NO_TLS_OFFSET -1 | |
169 | ||
170 | /* Get and set the global scope generation counter in struct pthread. */ | |
171 | #define THREAD_GSCOPE_FLAG_UNUSED 0 | |
172 | #define THREAD_GSCOPE_FLAG_USED 1 | |
173 | #define THREAD_GSCOPE_FLAG_WAIT 2 | |
174 | #define THREAD_GSCOPE_RESET_FLAG() \ | |
175 | do \ | |
176 | { int __res \ | |
177 | = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \ | |
178 | THREAD_GSCOPE_FLAG_UNUSED); \ | |
179 | if (__res == THREAD_GSCOPE_FLAG_WAIT) \ | |
180 | lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \ | |
181 | } \ | |
182 | while (0) | |
183 | #define THREAD_GSCOPE_SET_FLAG() \ | |
184 | do \ | |
185 | { \ | |
186 | THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \ | |
187 | atomic_write_barrier (); \ | |
188 | } \ | |
189 | while (0) | |
190 | #define THREAD_GSCOPE_WAIT() \ | |
191 | GL(dl_wait_lookup_done) () | |
192 | ||
193 | #endif /* __ASSEMBLER__ */ | |
194 | ||
195 | #endif /* tls.h */ |