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