1 /* Definition for thread-local data handling. NPTL/PowerPC version.
2 Copyright (C) 2003, 2005, 2006, 2007, 2011 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
22 # include <dl-sysdep.h>
29 /* Type for the dtv. */
40 #else /* __ASSEMBLER__ */
41 # include <tcb-offsets.h>
42 #endif /* __ASSEMBLER__ */
47 /* Get system call information. */
50 /* The TP points to the start of the thread blocks. */
51 # define TLS_DTV_AT_TP 1
53 /* We use the multiple_threads field in the pthread struct */
54 #define TLS_MULTIPLE_THREADS_IN_TCB 1
56 /* Get the thread descriptor definition. */
57 # include <nptl/descr.h>
59 /* The stack_guard is accessed directly by GCC -fstack-protector code,
60 so it is a part of public ABI. The dtv and pointer_guard fields
64 uintptr_t pointer_guard
;
65 uintptr_t stack_guard
;
69 /* This is the size of the initial TCB. */
70 # define TLS_INIT_TCB_SIZE 0
72 /* Alignment requirements for the initial TCB. */
73 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
75 /* This is the size of the TCB. */
76 # define TLS_TCB_SIZE 0
78 /* Alignment requirements for the TCB. */
79 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
81 /* This is the size we need before TCB. */
82 # define TLS_PRE_TCB_SIZE \
83 (sizeof (struct pthread) \
84 + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
86 # ifndef __powerpc64__
87 /* Register r2 (tp) is reserved by the ABI as "thread pointer". */
88 register void *__thread_register
__asm__ ("r2");
89 # define PT_THREAD_POINTER PT_R2
91 /* Register r13 (tp) is reserved by the ABI as "thread pointer". */
92 register void *__thread_register
__asm__ ("r13");
93 # define PT_THREAD_POINTER PT_R13
96 /* The following assumes that TP (R2 or R13) points to the end of the
97 TCB + 0x7000 (per the ABI). This implies that TCB address is
98 TP - 0x7000. As we define TLS_DTV_AT_TP we can
99 assume that the pthread struct is allocated immediately ahead of the
100 TCB. This implies that the pthread_descr address is
101 TP - (TLS_PRE_TCB_SIZE + 0x7000). */
102 # define TLS_TCB_OFFSET 0x7000
104 /* Install the dtv pointer. The pointer passed is to the element with
105 index -1 which contain the length. */
106 # define INSTALL_DTV(tcbp, dtvp) \
107 ((tcbhead_t *) (tcbp))[-1].dtv = dtvp + 1
109 /* Install new dtv for current thread. */
110 # define INSTALL_NEW_DTV(dtv) (THREAD_DTV() = (dtv))
112 /* Return dtv of given thread descriptor. */
113 # define GET_DTV(tcbp) (((tcbhead_t *) (tcbp))[-1].dtv)
115 /* Code to initially initialize the thread pointer. This might need
116 special attention since 'errno' is not yet available and if the
117 operation can cause a failure 'errno' must not be touched. */
118 # define TLS_INIT_TP(tcbp, secondcall) \
119 (__thread_register = (void *) (tcbp) + TLS_TCB_OFFSET, NULL)
121 /* Return the address of the dtv for the current thread. */
122 # define THREAD_DTV() \
123 (((tcbhead_t *) (__thread_register - TLS_TCB_OFFSET))[-1].dtv)
125 /* Return the thread descriptor for the current thread. */
126 # define THREAD_SELF \
127 ((struct pthread *) (__thread_register \
128 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
130 /* Magic for libthread_db to know how to do THREAD_SELF. */
131 # define DB_THREAD_SELF \
132 REGISTER (32, 32, PT_THREAD_POINTER * 4, \
133 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE) \
134 REGISTER (64, 64, PT_THREAD_POINTER * 8, \
135 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)
137 /* Read member of the thread descriptor directly. */
138 # define THREAD_GETMEM(descr, member) ((void)(descr), (THREAD_SELF)->member)
140 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
141 # define THREAD_GETMEM_NC(descr, member, idx) \
142 ((void)(descr), (THREAD_SELF)->member[idx])
144 /* Set member of the thread descriptor directly. */
145 # define THREAD_SETMEM(descr, member, value) \
146 ((void)(descr), (THREAD_SELF)->member = (value))
148 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
149 # define THREAD_SETMEM_NC(descr, member, idx, value) \
150 ((void)(descr), (THREAD_SELF)->member[idx] = (value))
152 /* Set the stack guard field in TCB head. */
153 # define THREAD_SET_STACK_GUARD(value) \
154 (((tcbhead_t *) ((char *) __thread_register \
155 - TLS_TCB_OFFSET))[-1].stack_guard = (value))
156 # define THREAD_COPY_STACK_GUARD(descr) \
157 (((tcbhead_t *) ((char *) (descr) \
158 + TLS_PRE_TCB_SIZE))[-1].stack_guard \
159 = ((tcbhead_t *) ((char *) __thread_register \
160 - TLS_TCB_OFFSET))[-1].stack_guard)
162 /* Set the stack guard field in TCB head. */
163 # define THREAD_GET_POINTER_GUARD() \
164 (((tcbhead_t *) ((char *) __thread_register \
165 - TLS_TCB_OFFSET))[-1].pointer_guard)
166 # define THREAD_SET_POINTER_GUARD(value) \
167 (THREAD_GET_POINTER_GUARD () = (value))
168 # define THREAD_COPY_POINTER_GUARD(descr) \
169 (((tcbhead_t *) ((char *) (descr) \
170 + TLS_PRE_TCB_SIZE))[-1].pointer_guard \
171 = THREAD_GET_POINTER_GUARD())
173 /* l_tls_offset == 0 is perfectly valid on PPC, so we have to use some
174 different value to mean unset l_tls_offset. */
175 # define NO_TLS_OFFSET -1
177 /* Get and set the global scope generation counter in struct pthread. */
178 #define THREAD_GSCOPE_FLAG_UNUSED 0
179 #define THREAD_GSCOPE_FLAG_USED 1
180 #define THREAD_GSCOPE_FLAG_WAIT 2
181 #define THREAD_GSCOPE_RESET_FLAG() \
184 = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
185 THREAD_GSCOPE_FLAG_UNUSED); \
186 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
187 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
190 #define THREAD_GSCOPE_SET_FLAG() \
193 THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
194 atomic_write_barrier (); \
197 #define THREAD_GSCOPE_WAIT() \
198 GL(dl_wait_lookup_done) ()
200 #endif /* __ASSEMBLER__ */