]>
sourceware.org Git - glibc.git/blob - sysdeps/i386/nptl/tls.h
d010e14920931a121d57eab51c0a4d0f465e0ac0
1 /* Definition for thread-local data handling. nptl/i386 version.
2 Copyright (C) 2002-2021 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 <https://www.gnu.org/licenses/>. */
22 #include <dl-sysdep.h>
29 # include <libc-pointer-arith.h> /* For cast_to_integer. */
30 # include <kernel-features.h>
35 void *tcb
; /* Pointer to the TCB. Not necessarily the
36 thread descriptor used by libpthread. */
38 void *self
; /* Pointer to the thread descriptor. */
41 uintptr_t stack_guard
;
42 uintptr_t pointer_guard
;
44 /* Bit 0: X86_FEATURE_1_IBT.
45 Bit 1: X86_FEATURE_1_SHSTK.
47 unsigned int feature_1
;
48 /* Reservation of some values for the TM ABI. */
49 void *__private_tm
[3];
50 /* GCC split stack support. */
52 /* The lowest address of shadow stack, */
53 unsigned long ssp_base
;
56 /* morestack.S in libgcc uses offset 0x30 to access __private_ss, */
57 _Static_assert (offsetof (tcbhead_t
, __private_ss
) == 0x30,
58 "offset of __private_ss != 0x30");
60 # define TLS_MULTIPLE_THREADS_IN_TCB 1
62 #else /* __ASSEMBLER__ */
63 # include <tcb-offsets.h>
67 /* Alignment requirement for the stack. For IA-32 this is governed by
68 the SSE memory functions. */
69 #define STACK_ALIGN 16
72 /* Get system call information. */
75 /* The old way: using LDT. */
77 /* Structure passed to `modify_ldt', 'set_thread_area', and 'clone' calls. */
80 unsigned int entry_number
;
81 unsigned long int base_addr
;
83 unsigned int seg_32bit
:1;
84 unsigned int contents
:2;
85 unsigned int read_exec_only
:1;
86 unsigned int limit_in_pages
:1;
87 unsigned int seg_not_present
:1;
88 unsigned int useable
:1;
89 unsigned int empty
:25;
92 /* Initializing bit fields is slow. We speed it up by using a union. */
95 struct user_desc desc
;
100 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
101 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
102 struct pthread even when not linked with -lpthread. */
103 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
105 /* Alignment requirements for the initial TCB. */
106 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
108 /* This is the size of the TCB. */
109 # define TLS_TCB_SIZE sizeof (struct pthread)
111 /* Alignment requirements for the TCB. */
112 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
114 /* The TCB can have any size and the memory following the address the
115 thread pointer points to is unspecified. Allocate the TCB there. */
116 # define TLS_TCB_AT_TP 1
117 # define TLS_DTV_AT_TP 0
119 /* Get the thread descriptor definition. */
120 # include <nptl/descr.h>
123 /* Install the dtv pointer. The pointer passed is to the element with
124 index -1 which contain the length. */
125 # define INSTALL_DTV(descr, dtvp) \
126 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
128 /* Install new dtv for current thread. */
129 # define INSTALL_NEW_DTV(dtvp) \
130 ({ struct pthread *__pd; \
131 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
133 /* Return dtv of given thread descriptor. */
134 # define GET_DTV(descr) \
135 (((tcbhead_t *) (descr))->dtv)
137 /* Macros to load from and store into segment registers. */
139 # define TLS_GET_GS() \
140 ({ int __seg; __asm ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
143 # define TLS_SET_GS(val) \
144 __asm ("movw %w0, %%gs" :: "q" (val))
147 #ifdef NEED_DL_SYSINFO
148 # define INIT_SYSINFO \
149 _head->sysinfo = GLRO(dl_sysinfo)
150 # define SETUP_THREAD_SYSINFO(pd) \
151 ((pd)->header.sysinfo = THREAD_GETMEM (THREAD_SELF, header.sysinfo))
152 # define CHECK_THREAD_SYSINFO(pd) \
153 assert ((pd)->header.sysinfo == THREAD_GETMEM (THREAD_SELF, header.sysinfo))
155 # define INIT_SYSINFO
158 #define LOCK_PREFIX "lock;"
160 static inline void __attribute__ ((unused
, always_inline
))
161 tls_fill_user_desc (union user_desc_init
*desc
,
162 unsigned int entry_number
,
165 desc
->vals
[0] = entry_number
;
166 /* The 'base_addr' field. Pointer to the TCB. */
167 desc
->vals
[1] = (unsigned long int) pd
;
168 /* The 'limit' field. We use 4GB which is 0xfffff pages. */
169 desc
->vals
[2] = 0xfffff;
170 /* Collapsed value of the bitfield:
177 desc
->vals
[3] = 0x51;
180 /* Code to initially initialize the thread pointer. This might need
181 special attention since 'errno' is not yet available and if the
182 operation can cause a failure 'errno' must not be touched. */
183 # define TLS_INIT_TP(thrdescr) \
184 ({ void *_thrdescr = (thrdescr); \
185 tcbhead_t *_head = _thrdescr; \
186 union user_desc_init _segdescr; \
189 _head->tcb = _thrdescr; \
190 /* For now the thread descriptor is at the same address. */ \
191 _head->self = _thrdescr; \
192 /* New syscall handling support. */ \
195 /* Let the kernel pick a value for the 'entry_number' field. */ \
196 tls_fill_user_desc (&_segdescr, -1, _thrdescr); \
198 /* Install the TLS. */ \
199 _result = INTERNAL_SYSCALL_CALL (set_thread_area, &_segdescr.desc); \
202 /* We know the index in the GDT, now load the segment register. \
203 The use of the GDT is described by the value 3 in the lower \
204 three bits of the segment descriptor value. \
206 Note that we have to do this even if the numeric value of \
207 the descriptor does not change. Loading the segment register \
208 causes the segment information from the GDT to be loaded \
209 which is necessary since we have changed it. */ \
210 TLS_SET_GS (_segdescr.desc.entry_number * 8 + 3); \
212 _result == 0 ? NULL \
213 : "set_thread_area failed when setting up thread-local storage\n"; })
215 # define TLS_DEFINE_INIT_TP(tp, pd) \
216 union user_desc_init _segdescr; \
217 /* Find the 'entry_number' field that the kernel selected in TLS_INIT_TP. \
218 The first three bits of the segment register value select the GDT, \
219 ignore them. We get the index from the value of the %gs register in \
220 the current thread. */ \
221 tls_fill_user_desc (&_segdescr, TLS_GET_GS () >> 3, pd); \
222 const struct user_desc *tp = &_segdescr.desc
225 /* Return the address of the dtv for the current thread. */
226 # define THREAD_DTV() \
227 ({ struct pthread *__pd; \
228 THREAD_GETMEM (__pd, header.dtv); })
231 /* Return the thread descriptor for the current thread.
233 The contained asm must *not* be marked volatile since otherwise
235 pthread_descr self = thread_self();
236 do not get optimized away. */
237 # if __GNUC_PREREQ (6, 0)
238 # define THREAD_SELF \
239 (*(struct pthread *__seg_gs *) offsetof (struct pthread, header.self))
241 # define THREAD_SELF \
242 ({ struct pthread *__self; \
243 asm ("movl %%gs:%c1,%0" : "=r" (__self) \
244 : "i" (offsetof (struct pthread, header.self))); \
248 /* Magic for libthread_db to know how to do THREAD_SELF. */
249 # define DB_THREAD_SELF \
250 REGISTER_THREAD_AREA (32, offsetof (struct user_regs_struct, xgs), 3) \
251 REGISTER_THREAD_AREA (64, 26 * 8, 3) /* x86-64's user_regs_struct->gs */
253 # include <tcb-access.h>
255 /* Set the stack guard field in TCB head. */
256 #define THREAD_SET_STACK_GUARD(value) \
257 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
258 #define THREAD_COPY_STACK_GUARD(descr) \
259 ((descr)->header.stack_guard \
260 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
263 /* Set the pointer guard field in the TCB head. */
264 #define THREAD_SET_POINTER_GUARD(value) \
265 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
266 #define THREAD_COPY_POINTER_GUARD(descr) \
267 ((descr)->header.pointer_guard \
268 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
271 /* Get and set the global scope generation counter in the TCB head. */
272 #define THREAD_GSCOPE_FLAG_UNUSED 0
273 #define THREAD_GSCOPE_FLAG_USED 1
274 #define THREAD_GSCOPE_FLAG_WAIT 2
275 #define THREAD_GSCOPE_RESET_FLAG() \
278 asm volatile ("xchgl %0, %%gs:%P1" \
280 : "i" (offsetof (struct pthread, header.gscope_flag)), \
281 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
282 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
283 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
286 #define THREAD_GSCOPE_SET_FLAG() \
287 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
289 #endif /* __ASSEMBLER__ */
This page took 0.046829 seconds and 4 git commands to generate.