2.5-18.1
[glibc.git] / nptl / sysdeps / powerpc / tls.h
blobad5698c6f8516f2eab8e52c226ebb81824490c2f
1 /* Definition for thread-local data handling. NPTL/PowerPC version.
2 Copyright (C) 2003, 2005, 2007 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifndef _TLS_H
21 #define _TLS_H 1
23 # include <dl-sysdep.h>
25 #ifndef __ASSEMBLER__
26 # include <stdbool.h>
27 # include <stddef.h>
28 # include <stdint.h>
30 /* Type for the dtv. */
31 typedef union dtv
33 size_t counter;
34 struct
36 void *val;
37 bool is_static;
38 } pointer;
39 } dtv_t;
41 #else /* __ASSEMBLER__ */
42 # include <tcb-offsets.h>
43 #endif /* __ASSEMBLER__ */
46 /* We require TLS support in the tools. */
47 #ifndef HAVE_TLS_SUPPORT
48 # error "TLS support is required."
49 #endif
51 /* Signal that TLS support is available. */
52 # define USE_TLS 1
54 #ifndef __ASSEMBLER__
56 /* Get system call information. */
57 # include <sysdep.h>
59 /* The TP points to the start of the thread blocks. */
60 # define TLS_DTV_AT_TP 1
62 /* We use the multiple_threads field in the pthread struct */
63 #define TLS_MULTIPLE_THREADS_IN_TCB 1
65 /* Get the thread descriptor definition. */
66 # include <nptl/descr.h>
68 /* The stack_guard is accessed directly by GCC -fstack-protector code,
69 so it is a part of public ABI. The dtv and pointer_guard fields
70 are private. */
71 typedef struct
73 uintptr_t pointer_guard;
74 uintptr_t stack_guard;
75 dtv_t *dtv;
76 } tcbhead_t;
78 /* This is the size of the initial TCB. */
79 # define TLS_INIT_TCB_SIZE 0
81 /* Alignment requirements for the initial TCB. */
82 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
84 /* This is the size of the TCB. */
85 # define TLS_TCB_SIZE 0
87 /* Alignment requirements for the TCB. */
88 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
90 /* This is the size we need before TCB. */
91 # define TLS_PRE_TCB_SIZE \
92 (sizeof (struct pthread) \
93 + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
95 # ifndef __powerpc64__
96 /* Register r2 (tp) is reserved by the ABI as "thread pointer". */
97 register void *__thread_register __asm__ ("r2");
98 # define PT_THREAD_POINTER PT_R2
99 # else
100 /* Register r13 (tp) is reserved by the ABI as "thread pointer". */
101 register void *__thread_register __asm__ ("r13");
102 # define PT_THREAD_POINTER PT_R13
103 # endif
105 /* The following assumes that TP (R2 or R13) points to the end of the
106 TCB + 0x7000 (per the ABI). This implies that TCB address is
107 TP - 0x7000. As we define TLS_DTV_AT_TP we can
108 assume that the pthread struct is allocated immediately ahead of the
109 TCB. This implies that the pthread_descr address is
110 TP - (TLS_PRE_TCB_SIZE + 0x7000). */
111 # define TLS_TCB_OFFSET 0x7000
113 /* Install the dtv pointer. The pointer passed is to the element with
114 index -1 which contain the length. */
115 # define INSTALL_DTV(tcbp, dtvp) \
116 ((tcbhead_t *) (tcbp))[-1].dtv = dtvp + 1
118 /* Install new dtv for current thread. */
119 # define INSTALL_NEW_DTV(dtv) (THREAD_DTV() = (dtv))
121 /* Return dtv of given thread descriptor. */
122 # define GET_DTV(tcbp) (((tcbhead_t *) (tcbp))[-1].dtv)
124 /* Code to initially initialize the thread pointer. This might need
125 special attention since 'errno' is not yet available and if the
126 operation can cause a failure 'errno' must not be touched. */
127 # define TLS_INIT_TP(tcbp, secondcall) \
128 (__thread_register = (void *) (tcbp) + TLS_TCB_OFFSET, NULL)
130 /* Return the address of the dtv for the current thread. */
131 # define THREAD_DTV() \
132 (((tcbhead_t *) (__thread_register - TLS_TCB_OFFSET))[-1].dtv)
134 /* Return the thread descriptor for the current thread. */
135 # define THREAD_SELF \
136 ((struct pthread *) (__thread_register \
137 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
139 /* Magic for libthread_db to know how to do THREAD_SELF. */
140 # define DB_THREAD_SELF \
141 REGISTER (32, 32, PT_THREAD_POINTER * 4, \
142 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE) \
143 REGISTER (64, 64, PT_THREAD_POINTER * 8, \
144 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)
146 /* Read member of the thread descriptor directly. */
147 # define THREAD_GETMEM(descr, member) ((void)(descr), (THREAD_SELF)->member)
149 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
150 # define THREAD_GETMEM_NC(descr, member, idx) \
151 ((void)(descr), (THREAD_SELF)->member[idx])
153 /* Set member of the thread descriptor directly. */
154 # define THREAD_SETMEM(descr, member, value) \
155 ((void)(descr), (THREAD_SELF)->member = (value))
157 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
158 # define THREAD_SETMEM_NC(descr, member, idx, value) \
159 ((void)(descr), (THREAD_SELF)->member[idx] = (value))
161 /* Set the stack guard field in TCB head. */
162 # define THREAD_SET_STACK_GUARD(value) \
163 (((tcbhead_t *) ((char *) __thread_register \
164 - TLS_TCB_OFFSET))[-1].stack_guard = (value))
165 # define THREAD_COPY_STACK_GUARD(descr) \
166 (((tcbhead_t *) ((char *) (descr) \
167 + TLS_PRE_TCB_SIZE))[-1].stack_guard \
168 = ((tcbhead_t *) ((char *) __thread_register \
169 - TLS_TCB_OFFSET))[-1].stack_guard)
171 /* Set the stack guard field in TCB head. */
172 # define THREAD_GET_POINTER_GUARD() \
173 (((tcbhead_t *) ((char *) __thread_register \
174 - TLS_TCB_OFFSET))[-1].pointer_guard)
175 # define THREAD_SET_POINTER_GUARD(value) \
176 (THREAD_GET_POINTER_GUARD () = (value))
177 # define THREAD_COPY_POINTER_GUARD(descr) \
178 (((tcbhead_t *) ((char *) (descr) \
179 + TLS_PRE_TCB_SIZE))[-1].pointer_guard \
180 = THREAD_GET_POINTER_GUARD())
182 /* l_tls_offset == 0 is perfectly valid on PPC, so we have to use some
183 different value to mean unset l_tls_offset. */
184 # define NO_TLS_OFFSET -1
186 /* Get and set the global scope generation counter in struct pthread. */
187 #define THREAD_GSCOPE_FLAG_UNUSED 0
188 #define THREAD_GSCOPE_FLAG_USED 1
189 #define THREAD_GSCOPE_FLAG_WAIT 2
190 #define THREAD_GSCOPE_RESET_FLAG() \
191 do \
192 { int __res \
193 = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
194 THREAD_GSCOPE_FLAG_UNUSED); \
195 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
196 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1); \
198 while (0)
199 #define THREAD_GSCOPE_SET_FLAG() \
200 do \
202 THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
203 atomic_write_barrier (); \
205 while (0)
206 #define THREAD_GSCOPE_WAIT() \
207 GL(dl_wait_lookup_done) ()
209 #endif /* __ASSEMBLER__ */
211 #endif /* tls.h */