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.
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    <http://www.gnu.org/licenses/>.  */
18 
19 #ifndef _TLS_H
20 #define _TLS_H	1
21 
22 #ifndef __ASSEMBLER__
23 # include <stdbool.h>
24 # include <stddef.h>
25 # include <stdint.h>
26 
27 /* Type for the dtv.  */
28 typedef union dtv
29 {
30   size_t counter;
31   struct
32   {
33     void *val;
34     bool is_static;
35   } pointer;
36 } dtv_t;
37 
38 #else /* __ASSEMBLER__ */
39 # include <tcb-offsets.h>
40 #endif /* __ASSEMBLER__ */
41 
42 /* We require TLS support in the tools.  */
43 #define HAVE_TLS_SUPPORT                1
44 #define HAVE_TLS_MODEL_ATTRIBUTE        1
45 #define HAVE___THREAD                   1
46 
47 /* Signal that TLS support is available.  */
48 #define USE_TLS	1
49 
50 /* We require TLS support in the tools.  */
51 #ifndef HAVE_TLS_SUPPORT
52 # error "TLS support is required."
53 #endif
54 
55 /* Signal that TLS support is available.  */
56 # define USE_TLS       1
57 
58 #ifndef __ASSEMBLER__
59 
60 /* Get system call information.  */
61 # include <sysdep.h>
62 
63 /* The TP points to the start of the thread blocks.  */
64 # define TLS_DTV_AT_TP	1
65 
66 /* We use the multiple_threads field in the pthread struct */
67 #define TLS_MULTIPLE_THREADS_IN_TCB	1
68 
69 /* Get the thread descriptor definition.  */
70 # include <../../descr.h>
71 
72 /* The stack_guard is accessed directly by GCC -fstack-protector code,
73    so it is a part of public ABI.  The dtv and pointer_guard fields
74    are private.  */
75 typedef struct
76 {
77   uintptr_t pointer_guard;
78   uintptr_t stack_guard;
79   dtv_t *dtv;
80 } tcbhead_t;
81 
82 /* This is the size of the initial TCB.  */
83 # define TLS_INIT_TCB_SIZE	0
84 
85 /* Alignment requirements for the initial TCB.  */
86 # define TLS_INIT_TCB_ALIGN	__alignof__ (struct pthread)
87 
88 /* This is the size of the TCB.  */
89 # define TLS_TCB_SIZE		0
90 
91 /* Alignment requirements for the TCB.  */
92 # define TLS_TCB_ALIGN		__alignof__ (struct pthread)
93 
94 /* This is the size we need before TCB.  */
95 # define TLS_PRE_TCB_SIZE \
96   (sizeof (struct pthread)						      \
97    + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
98 
99 # ifndef __powerpc64__
100 /* Register r2 (tp) is reserved by the ABI as "thread pointer". */
101 register void *__thread_register __asm__ ("r2");
102 #  define PT_THREAD_POINTER PT_R2
103 # else
104 /* Register r13 (tp) is reserved by the ABI as "thread pointer". */
105 register void *__thread_register __asm__ ("r13");
106 #  define PT_THREAD_POINTER PT_R13
107 # endif
108 
109 /* The following assumes that TP (R2 or R13) points to the end of the
110    TCB + 0x7000 (per the ABI).  This implies that TCB address is
111    TP - 0x7000.  As we define TLS_DTV_AT_TP we can
112    assume that the pthread struct is allocated immediately ahead of the
113    TCB.  This implies that the pthread_descr address is
114    TP - (TLS_PRE_TCB_SIZE + 0x7000).  */
115 # define TLS_TCB_OFFSET	0x7000
116 
117 /* Install the dtv pointer.  The pointer passed is to the element with
118    index -1 which contain the length.  */
119 # define INSTALL_DTV(tcbp, dtvp) \
120   ((tcbhead_t *) (tcbp))[-1].dtv = dtvp + 1
121 
122 /* Install new dtv for current thread.  */
123 # define INSTALL_NEW_DTV(dtv) (THREAD_DTV() = (dtv))
124 
125 /* Return dtv of given thread descriptor.  */
126 # define GET_DTV(tcbp)	(((tcbhead_t *) (tcbp))[-1].dtv)
127 
128 /* Code to initially initialize the thread pointer.  This might need
129    special attention since 'errno' is not yet available and if the
130    operation can cause a failure 'errno' must not be touched.  */
131 # define TLS_INIT_TP(tcbp, secondcall) \
132     (__thread_register = (void *) (tcbp) + TLS_TCB_OFFSET, NULL)
133 
134 /* Return the address of the dtv for the current thread.  */
135 # define THREAD_DTV() \
136     (((tcbhead_t *) (__thread_register - TLS_TCB_OFFSET))[-1].dtv)
137 
138 /* Return the thread descriptor for the current thread.  */
139 # define THREAD_SELF \
140     ((struct pthread *) (__thread_register \
141 			 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
142 
143 /* Magic for libthread_db to know how to do THREAD_SELF.  */
144 # define DB_THREAD_SELF							      \
145   REGISTER (32, 32, PT_THREAD_POINTER * 4,				      \
146 	    - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)			      \
147   REGISTER (64, 64, PT_THREAD_POINTER * 8,				      \
148 	    - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)
149 
150 /* Read member of the thread descriptor directly.  */
151 # define THREAD_GETMEM(descr, member) ((void)(descr), (THREAD_SELF)->member)
152 
153 /* Same as THREAD_GETMEM, but the member offset can be non-constant.  */
154 # define THREAD_GETMEM_NC(descr, member, idx) \
155     ((void)(descr), (THREAD_SELF)->member[idx])
156 
157 /* Set member of the thread descriptor directly.  */
158 # define THREAD_SETMEM(descr, member, value) \
159     ((void)(descr), (THREAD_SELF)->member = (value))
160 
161 /* Same as THREAD_SETMEM, but the member offset can be non-constant.  */
162 # define THREAD_SETMEM_NC(descr, member, idx, value) \
163     ((void)(descr), (THREAD_SELF)->member[idx] = (value))
164 
165 /* Set the stack guard field in TCB head.  */
166 # define THREAD_SET_STACK_GUARD(value) \
167     (((tcbhead_t *) ((char *) __thread_register				      \
168 		     - TLS_TCB_OFFSET))[-1].stack_guard = (value))
169 # define THREAD_COPY_STACK_GUARD(descr) \
170     (((tcbhead_t *) ((char *) (descr)					      \
171 		     + TLS_PRE_TCB_SIZE))[-1].stack_guard		      \
172      = ((tcbhead_t *) ((char *) __thread_register			      \
173 		       - TLS_TCB_OFFSET))[-1].stack_guard)
174 
175 /* Set the stack guard field in TCB head.  */
176 # define THREAD_GET_POINTER_GUARD() \
177     (((tcbhead_t *) ((char *) __thread_register				      \
178 		     - TLS_TCB_OFFSET))[-1].pointer_guard)
179 # define THREAD_SET_POINTER_GUARD(value) \
180     (THREAD_GET_POINTER_GUARD () = (value))
181 # define THREAD_COPY_POINTER_GUARD(descr) \
182     (((tcbhead_t *) ((char *) (descr)					      \
183 		     + TLS_PRE_TCB_SIZE))[-1].pointer_guard		      \
184      = THREAD_GET_POINTER_GUARD())
185 
186 /* l_tls_offset == 0 is perfectly valid on PPC, so we have to use some
187    different value to mean unset l_tls_offset.  */
188 # define NO_TLS_OFFSET		-1
189 
190 /* Get and set the global scope generation counter in struct pthread.  */
191 #define THREAD_GSCOPE_FLAG_UNUSED 0
192 #define THREAD_GSCOPE_FLAG_USED   1
193 #define THREAD_GSCOPE_FLAG_WAIT   2
194 #define THREAD_GSCOPE_RESET_FLAG() \
195   do									     \
196     { int __res								     \
197 	= atomic_exchange_rel (&THREAD_SELF->header.gscope_flag,	     \
198 			       THREAD_GSCOPE_FLAG_UNUSED);		     \
199       if (__res == THREAD_GSCOPE_FLAG_WAIT)				     \
200 	lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);   \
201     }									     \
202   while (0)
203 #define THREAD_GSCOPE_SET_FLAG() \
204   do									     \
205     {									     \
206       THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED;	     \
207       atomic_write_barrier ();						     \
208     }									     \
209   while (0)
210 #define THREAD_GSCOPE_WAIT() \
211   GL(dl_wait_lookup_done) ()
212 
213 #endif /* __ASSEMBLER__ */
214 
215 #endif	/* tls.h */
216