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