2.9
[glibc/nacl-glibc.git] / nptl / sysdeps / powerpc / tls.h
blob0f4d5290dd1e93fcae08d8880eca27a5b6411d4a
1 /* Definition for thread-local data handling. NPTL/PowerPC version.
2 Copyright (C) 2003, 2005, 2006, 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 #ifndef __ASSEMBLER__
53 /* Get system call information. */
54 # include <sysdep.h>
56 /* The TP points to the start of the thread blocks. */
57 # define TLS_DTV_AT_TP 1
59 /* We use the multiple_threads field in the pthread struct */
60 #define TLS_MULTIPLE_THREADS_IN_TCB 1
62 /* Get the thread descriptor definition. */
63 # include <nptl/descr.h>
65 /* The stack_guard is accessed directly by GCC -fstack-protector code,
66 so it is a part of public ABI. The dtv and pointer_guard fields
67 are private. */
68 typedef struct
70 uintptr_t pointer_guard;
71 uintptr_t stack_guard;
72 dtv_t *dtv;
73 } tcbhead_t;
75 /* This is the size of the initial TCB. */
76 # define TLS_INIT_TCB_SIZE 0
78 /* Alignment requirements for the initial TCB. */
79 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
81 /* This is the size of the TCB. */
82 # define TLS_TCB_SIZE 0
84 /* Alignment requirements for the TCB. */
85 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
87 /* This is the size we need before TCB. */
88 # define TLS_PRE_TCB_SIZE \
89 (sizeof (struct pthread) \
90 + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
92 # ifndef __powerpc64__
93 /* Register r2 (tp) is reserved by the ABI as "thread pointer". */
94 register void *__thread_register __asm__ ("r2");
95 # define PT_THREAD_POINTER PT_R2
96 # else
97 /* Register r13 (tp) is reserved by the ABI as "thread pointer". */
98 register void *__thread_register __asm__ ("r13");
99 # define PT_THREAD_POINTER PT_R13
100 # endif
102 /* The following assumes that TP (R2 or R13) points to the end of the
103 TCB + 0x7000 (per the ABI). This implies that TCB address is
104 TP - 0x7000. As we define TLS_DTV_AT_TP we can
105 assume that the pthread struct is allocated immediately ahead of the
106 TCB. This implies that the pthread_descr address is
107 TP - (TLS_PRE_TCB_SIZE + 0x7000). */
108 # define TLS_TCB_OFFSET 0x7000
110 /* Install the dtv pointer. The pointer passed is to the element with
111 index -1 which contain the length. */
112 # define INSTALL_DTV(tcbp, dtvp) \
113 ((tcbhead_t *) (tcbp))[-1].dtv = dtvp + 1
115 /* Install new dtv for current thread. */
116 # define INSTALL_NEW_DTV(dtv) (THREAD_DTV() = (dtv))
118 /* Return dtv of given thread descriptor. */
119 # define GET_DTV(tcbp) (((tcbhead_t *) (tcbp))[-1].dtv)
121 /* Code to initially initialize the thread pointer. This might need
122 special attention since 'errno' is not yet available and if the
123 operation can cause a failure 'errno' must not be touched. */
124 # define TLS_INIT_TP(tcbp, secondcall) \
125 (__thread_register = (void *) (tcbp) + TLS_TCB_OFFSET, NULL)
127 /* Return the address of the dtv for the current thread. */
128 # define THREAD_DTV() \
129 (((tcbhead_t *) (__thread_register - TLS_TCB_OFFSET))[-1].dtv)
131 /* Return the thread descriptor for the current thread. */
132 # define THREAD_SELF \
133 ((struct pthread *) (__thread_register \
134 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
136 /* Magic for libthread_db to know how to do THREAD_SELF. */
137 # define DB_THREAD_SELF \
138 REGISTER (32, 32, PT_THREAD_POINTER * 4, \
139 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE) \
140 REGISTER (64, 64, PT_THREAD_POINTER * 8, \
141 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)
143 /* Read member of the thread descriptor directly. */
144 # define THREAD_GETMEM(descr, member) ((void)(descr), (THREAD_SELF)->member)
146 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
147 # define THREAD_GETMEM_NC(descr, member, idx) \
148 ((void)(descr), (THREAD_SELF)->member[idx])
150 /* Set member of the thread descriptor directly. */
151 # define THREAD_SETMEM(descr, member, value) \
152 ((void)(descr), (THREAD_SELF)->member = (value))
154 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
155 # define THREAD_SETMEM_NC(descr, member, idx, value) \
156 ((void)(descr), (THREAD_SELF)->member[idx] = (value))
158 /* Set the stack guard field in TCB head. */
159 # define THREAD_SET_STACK_GUARD(value) \
160 (((tcbhead_t *) ((char *) __thread_register \
161 - TLS_TCB_OFFSET))[-1].stack_guard = (value))
162 # define THREAD_COPY_STACK_GUARD(descr) \
163 (((tcbhead_t *) ((char *) (descr) \
164 + TLS_PRE_TCB_SIZE))[-1].stack_guard \
165 = ((tcbhead_t *) ((char *) __thread_register \
166 - TLS_TCB_OFFSET))[-1].stack_guard)
168 /* Set the stack guard field in TCB head. */
169 # define THREAD_GET_POINTER_GUARD() \
170 (((tcbhead_t *) ((char *) __thread_register \
171 - TLS_TCB_OFFSET))[-1].pointer_guard)
172 # define THREAD_SET_POINTER_GUARD(value) \
173 (THREAD_GET_POINTER_GUARD () = (value))
174 # define THREAD_COPY_POINTER_GUARD(descr) \
175 (((tcbhead_t *) ((char *) (descr) \
176 + TLS_PRE_TCB_SIZE))[-1].pointer_guard \
177 = THREAD_GET_POINTER_GUARD())
179 /* l_tls_offset == 0 is perfectly valid on PPC, so we have to use some
180 different value to mean unset l_tls_offset. */
181 # define NO_TLS_OFFSET -1
183 /* Get and set the global scope generation counter in struct pthread. */
184 #define THREAD_GSCOPE_FLAG_UNUSED 0
185 #define THREAD_GSCOPE_FLAG_USED 1
186 #define THREAD_GSCOPE_FLAG_WAIT 2
187 #define THREAD_GSCOPE_RESET_FLAG() \
188 do \
189 { int __res \
190 = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
191 THREAD_GSCOPE_FLAG_UNUSED); \
192 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
193 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
195 while (0)
196 #define THREAD_GSCOPE_SET_FLAG() \
197 do \
199 THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
200 atomic_write_barrier (); \
202 while (0)
203 #define THREAD_GSCOPE_WAIT() \
204 GL(dl_wait_lookup_done) ()
206 #endif /* __ASSEMBLER__ */
208 #endif /* tls.h */