powerpc: abort transaction in syscalls
[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   /* Indicate if HTM capable (ISA 2.07).  */
67   int tm_capable;
68   /* Reservation for Dynamic System Optimizer ABI.  */
69   uintptr_t dso_slot2;
70   uintptr_t dso_slot1;
71   /* Reservation for tar register (ISA 2.07).  */
72   uintptr_t tar_save;
73   /* GCC split stack support.  */
74   void *__private_ss;
75   /* Reservation for the Event-Based Branching ABI.  */
76   uintptr_t ebb_handler;
77   uintptr_t ebb_ctx_pointer;
78   uintptr_t ebb_reserved1;
79   uintptr_t ebb_reserved2;
80   uintptr_t pointer_guard;
81   uintptr_t stack_guard;
82   dtv_t *dtv;
83 } tcbhead_t;
84
85 /* This is the size of the initial TCB.  */
86 # define TLS_INIT_TCB_SIZE      0
87
88 /* Alignment requirements for the initial TCB.  */
89 # define TLS_INIT_TCB_ALIGN     __alignof__ (struct pthread)
90
91 /* This is the size of the TCB.  */
92 # define TLS_TCB_SIZE           0
93
94 /* Alignment requirements for the TCB.  */
95 # define TLS_TCB_ALIGN          __alignof__ (struct pthread)
96
97 /* This is the size we need before TCB.  */
98 # define TLS_PRE_TCB_SIZE \
99   (sizeof (struct pthread)                                                    \
100    + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
101
102 # ifndef __powerpc64__
103 /* Register r2 (tp) is reserved by the ABI as "thread pointer". */
104 register void *__thread_register __asm__ ("r2");
105 #  define PT_THREAD_POINTER PT_R2
106 # else
107 /* Register r13 (tp) is reserved by the ABI as "thread pointer". */
108 register void *__thread_register __asm__ ("r13");
109 #  define PT_THREAD_POINTER PT_R13
110 # endif
111
112 /* The following assumes that TP (R2 or R13) points to the end of the
113    TCB + 0x7000 (per the ABI).  This implies that TCB address is
114    TP - 0x7000.  As we define TLS_DTV_AT_TP we can
115    assume that the pthread struct is allocated immediately ahead of the
116    TCB.  This implies that the pthread_descr address is
117    TP - (TLS_PRE_TCB_SIZE + 0x7000).  */
118 # define TLS_TCB_OFFSET 0x7000
119
120 /* Install the dtv pointer.  The pointer passed is to the element with
121    index -1 which contain the length.  */
122 # define INSTALL_DTV(tcbp, dtvp) \
123   ((tcbhead_t *) (tcbp))[-1].dtv = dtvp + 1
124
125 /* Install new dtv for current thread.  */
126 # define INSTALL_NEW_DTV(dtv) (THREAD_DTV() = (dtv))
127
128 /* Return dtv of given thread descriptor.  */
129 # define GET_DTV(tcbp)  (((tcbhead_t *) (tcbp))[-1].dtv)
130
131 /* Code to initially initialize the thread pointer.  This might need
132    special attention since 'errno' is not yet available and if the
133    operation can cause a failure 'errno' must not be touched.  */
134 # define TLS_INIT_TP(tcbp) \
135   ({                                                                          \
136     __thread_register = (void *) (tcbp) + TLS_TCB_OFFSET;                     \
137     THREAD_SET_TM_CAPABLE (GLRO (dl_hwcap2) & PPC_FEATURE2_HAS_HTM ? 1 : 0);  \
138     NULL;                                                                     \
139   })
140
141 /* Value passed to 'clone' for initialization of the thread register.  */
142 # define TLS_DEFINE_INIT_TP(tp, pd) \
143     void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE;             \
144     (((tcbhead_t *) ((char *) tp - TLS_TCB_OFFSET))[-1].tm_capable) =         \
145       THREAD_GET_TM_CAPABLE ();
146
147 /* Return the address of the dtv for the current thread.  */
148 # define THREAD_DTV() \
149     (((tcbhead_t *) (__thread_register - TLS_TCB_OFFSET))[-1].dtv)
150
151 /* Return the thread descriptor for the current thread.  */
152 # define THREAD_SELF \
153     ((struct pthread *) (__thread_register \
154                          - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
155
156 /* Magic for libthread_db to know how to do THREAD_SELF.  */
157 # define DB_THREAD_SELF                                                       \
158   REGISTER (32, 32, PT_THREAD_POINTER * 4,                                    \
159             - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)                              \
160   REGISTER (64, 64, PT_THREAD_POINTER * 8,                                    \
161             - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)
162
163 /* Read member of the thread descriptor directly.  */
164 # define THREAD_GETMEM(descr, member) ((void)(descr), (THREAD_SELF)->member)
165
166 /* Same as THREAD_GETMEM, but the member offset can be non-constant.  */
167 # define THREAD_GETMEM_NC(descr, member, idx) \
168     ((void)(descr), (THREAD_SELF)->member[idx])
169
170 /* Set member of the thread descriptor directly.  */
171 # define THREAD_SETMEM(descr, member, value) \
172     ((void)(descr), (THREAD_SELF)->member = (value))
173
174 /* Same as THREAD_SETMEM, but the member offset can be non-constant.  */
175 # define THREAD_SETMEM_NC(descr, member, idx, value) \
176     ((void)(descr), (THREAD_SELF)->member[idx] = (value))
177
178 /* Set the stack guard field in TCB head.  */
179 # define THREAD_SET_STACK_GUARD(value) \
180     (((tcbhead_t *) ((char *) __thread_register                               \
181                      - TLS_TCB_OFFSET))[-1].stack_guard = (value))
182 # define THREAD_COPY_STACK_GUARD(descr) \
183     (((tcbhead_t *) ((char *) (descr)                                         \
184                      + TLS_PRE_TCB_SIZE))[-1].stack_guard                     \
185      = ((tcbhead_t *) ((char *) __thread_register                             \
186                        - TLS_TCB_OFFSET))[-1].stack_guard)
187
188 /* Set the stack guard field in TCB head.  */
189 # define THREAD_GET_POINTER_GUARD() \
190     (((tcbhead_t *) ((char *) __thread_register                               \
191                      - TLS_TCB_OFFSET))[-1].pointer_guard)
192 # define THREAD_SET_POINTER_GUARD(value) \
193     (THREAD_GET_POINTER_GUARD () = (value))
194 # define THREAD_COPY_POINTER_GUARD(descr) \
195     (((tcbhead_t *) ((char *) (descr)                                         \
196                      + TLS_PRE_TCB_SIZE))[-1].pointer_guard                   \
197      = THREAD_GET_POINTER_GUARD())
198
199 /* tm_capable field in TCB head.  */
200 # define THREAD_GET_TM_CAPABLE() \
201     (((tcbhead_t *) ((char *) __thread_register                               \
202                      - TLS_TCB_OFFSET))[-1].tm_capable)
203 # define THREAD_SET_TM_CAPABLE(value) \
204     (THREAD_GET_TM_CAPABLE () = (value))
205
206 /* l_tls_offset == 0 is perfectly valid on PPC, so we have to use some
207    different value to mean unset l_tls_offset.  */
208 # define NO_TLS_OFFSET          -1
209
210 /* Get and set the global scope generation counter in struct pthread.  */
211 #define THREAD_GSCOPE_FLAG_UNUSED 0
212 #define THREAD_GSCOPE_FLAG_USED   1
213 #define THREAD_GSCOPE_FLAG_WAIT   2
214 #define THREAD_GSCOPE_RESET_FLAG() \
215   do                                                                         \
216     { int __res                                                              \
217         = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag,             \
218                                THREAD_GSCOPE_FLAG_UNUSED);                   \
219       if (__res == THREAD_GSCOPE_FLAG_WAIT)                                  \
220         lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);   \
221     }                                                                        \
222   while (0)
223 #define THREAD_GSCOPE_SET_FLAG() \
224   do                                                                         \
225     {                                                                        \
226       THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED;             \
227       atomic_write_barrier ();                                               \
228     }                                                                        \
229   while (0)
230 #define THREAD_GSCOPE_WAIT() \
231   GL(dl_wait_lookup_done) ()
232
233 #endif /* __ASSEMBLER__ */
234
235 #endif  /* tls.h */