]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/sysdeps/powerpc/tls.h
[BZ #4772]
[thirdparty/glibc.git] / nptl / sysdeps / powerpc / tls.h
CommitLineData
f3c13160 1/* Definition for thread-local data handling. NPTL/PowerPC version.
0a54ab53 2 Copyright (C) 2003, 2005, 2006, 2007 Free Software Foundation, Inc.
f3c13160
RM
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20#ifndef _TLS_H
21#define _TLS_H 1
22
23# include <dl-sysdep.h>
24
25#ifndef __ASSEMBLER__
9dcafc55 26# include <stdbool.h>
f3c13160
RM
27# include <stddef.h>
28# include <stdint.h>
29
30/* Type for the dtv. */
31typedef union dtv
32{
33 size_t counter;
9dcafc55
UD
34 struct
35 {
36 void *val;
37 bool is_static;
38 } pointer;
f3c13160
RM
39} dtv_t;
40
41#else /* __ASSEMBLER__ */
42# include <tcb-offsets.h>
43#endif /* __ASSEMBLER__ */
44
45
46/* We require TLS support in the tools. */
47#ifndef HAVE_TLS_SUPPORT
48# error "TLS support is required."
49#endif
50
f3c13160
RM
51#ifndef __ASSEMBLER__
52
53/* Get system call information. */
54# include <sysdep.h>
55
56/* The TP points to the start of the thread blocks. */
57# define TLS_DTV_AT_TP 1
58
94659495
RM
59/* We use the multiple_threads field in the pthread struct */
60#define TLS_MULTIPLE_THREADS_IN_TCB 1
f3c13160
RM
61
62/* Get the thread descriptor definition. */
63# include <nptl/descr.h>
64
35f1e827 65/* The stack_guard is accessed directly by GCC -fstack-protector code,
991fa82b
UD
66 so it is a part of public ABI. The dtv and pointer_guard fields
67 are private. */
f3c13160
RM
68typedef struct
69{
00c2b3b9 70 uintptr_t pointer_guard;
35f1e827 71 uintptr_t stack_guard;
f3c13160
RM
72 dtv_t *dtv;
73} tcbhead_t;
74
75/* This is the size of the initial TCB. */
299601a1 76# define TLS_INIT_TCB_SIZE 0
f3c13160
RM
77
78/* Alignment requirements for the initial TCB. */
299601a1 79# define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
f3c13160
RM
80
81/* This is the size of the TCB. */
299601a1 82# define TLS_TCB_SIZE 0
f3c13160
RM
83
84/* Alignment requirements for the TCB. */
299601a1 85# define TLS_TCB_ALIGN __alignof__ (struct pthread)
f3c13160
RM
86
87/* This is the size we need before TCB. */
468777e1
UD
88# define TLS_PRE_TCB_SIZE \
89 (sizeof (struct pthread) \
90 + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
f3c13160
RM
91
92# ifndef __powerpc64__
93/* Register r2 (tp) is reserved by the ABI as "thread pointer". */
94register 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". */
98register void *__thread_register __asm__ ("r13");
99# define PT_THREAD_POINTER PT_R13
100# endif
101
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
299601a1 104 TP - 0x7000. As we define TLS_DTV_AT_TP we can
f3c13160
RM
105 assume that the pthread struct is allocated immediately ahead of the
106 TCB. This implies that the pthread_descr address is
299601a1 107 TP - (TLS_PRE_TCB_SIZE + 0x7000). */
f3c13160
RM
108# define TLS_TCB_OFFSET 0x7000
109
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) \
299601a1 113 ((tcbhead_t *) (tcbp))[-1].dtv = dtvp + 1
f3c13160
RM
114
115/* Install new dtv for current thread. */
116# define INSTALL_NEW_DTV(dtv) (THREAD_DTV() = (dtv))
117
118/* Return dtv of given thread descriptor. */
299601a1 119# define GET_DTV(tcbp) (((tcbhead_t *) (tcbp))[-1].dtv)
f3c13160
RM
120
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) \
299601a1 125 (__thread_register = (void *) (tcbp) + TLS_TCB_OFFSET, NULL)
f3c13160
RM
126
127/* Return the address of the dtv for the current thread. */
128# define THREAD_DTV() \
35f1e827 129 (((tcbhead_t *) (__thread_register - TLS_TCB_OFFSET))[-1].dtv)
f3c13160
RM
130
131/* Return the thread descriptor for the current thread. */
132# define THREAD_SELF \
133 ((struct pthread *) (__thread_register \
299601a1 134 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
f3c13160 135
7f08f55a
RM
136/* Magic for libthread_db to know how to do THREAD_SELF. */
137# define DB_THREAD_SELF \
35f1e827 138 REGISTER (32, 32, PT_THREAD_POINTER * 4, \
7f08f55a 139 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE) \
35f1e827 140 REGISTER (64, 64, PT_THREAD_POINTER * 8, \
7f08f55a
RM
141 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)
142
f3c13160
RM
143/* Read member of the thread descriptor directly. */
144# define THREAD_GETMEM(descr, member) ((void)(descr), (THREAD_SELF)->member)
145
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])
149
150/* Set member of the thread descriptor directly. */
151# define THREAD_SETMEM(descr, member, value) \
152 ((void)(descr), (THREAD_SELF)->member = (value))
153
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))
157
35f1e827
UD
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)
167
00c2b3b9
UD
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())
178
299601a1
UD
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
182
991fa82b 183/* Get and set the global scope generation counter in struct pthread. */
0a54ab53
UD
184#define THREAD_GSCOPE_FLAG_UNUSED 0
185#define THREAD_GSCOPE_FLAG_USED 1
186#define THREAD_GSCOPE_FLAG_WAIT 2
0a54ab53
UD
187#define THREAD_GSCOPE_RESET_FLAG() \
188 do \
189 { int __res \
991fa82b 190 = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
0a54ab53
UD
191 THREAD_GSCOPE_FLAG_UNUSED); \
192 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
835abc5c 193 lll_private_futex_wake (&THREAD_SELF->header.gscope_flag, 1); \
0a54ab53
UD
194 } \
195 while (0)
196#define THREAD_GSCOPE_SET_FLAG() \
991fa82b
UD
197 do \
198 { \
199 THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
200 atomic_write_barrier (); \
201 } \
202 while (0)
0a54ab53
UD
203#define THREAD_GSCOPE_WAIT() \
204 GL(dl_wait_lookup_done) ()
205
f3c13160
RM
206#endif /* __ASSEMBLER__ */
207
208#endif /* tls.h */