blob: bbb11187ae6211914fe6fcc0544c8986be4a9931 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* Definition for thread-local data handling. NPTL/SH 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
22
23#ifndef __ASSEMBLER__
24# include <stdbool.h>
25# include <stddef.h>
26# include <stdint.h>
27# include <stdlib.h>
28# include <list.h>
29# include <sysdep.h>
30# include <bits/kernel-features.h>
31
32/* Type for the dtv. */
33typedef union dtv
34{
35 size_t counter;
36 struct
37 {
38 void *val;
39 bool is_static;
40 } pointer;
41} dtv_t;
42
43typedef struct
44{
45 dtv_t *dtv;
46 uintptr_t pointer_guard;
47} tcbhead_t;
48
49# define TLS_MULTIPLE_THREADS_IN_TCB 1
50
51#else /* __ASSEMBLER__ */
52# include <tcb-offsets.h>
53#endif /* __ASSEMBLER__ */
54
55
56/* We require TLS support in the tools. */
57#define HAVE_TLS_SUPPORT
58#define HAVE___THREAD 1
59#define HAVE_TLS_MODEL_ATTRIBUTE 1
60/* Signal that TLS support is available. */
61# define USE_TLS 1
62
63#ifndef __ASSEMBLER__
64
65/* Get system call information. */
66# include <sysdep.h>
67
68/* This is the size of the initial TCB. */
69# define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
70
71/* Alignment requirements for the initial TCB. */
72# define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
73
74/* This is the size of the TCB. */
75# define TLS_TCB_SIZE sizeof (tcbhead_t)
76
77/* This is the size we need before TCB. */
78# define TLS_PRE_TCB_SIZE sizeof (struct pthread)
79
80/* Alignment requirements for the TCB. */
81# define TLS_TCB_ALIGN __alignof__ (struct pthread)
82
83/* The TLS blocks start right after the TCB. */
84# define TLS_DTV_AT_TP 1
85
86/* Get the thread descriptor definition. */
87# include <descr.h>
88
89/* Install the dtv pointer. The pointer passed is to the element with
90 index -1 which contain the length. */
91# define INSTALL_DTV(tcbp, dtvp) \
92 ((tcbhead_t *) (tcbp))->dtv = (dtvp) + 1
93
94/* Install new dtv for current thread. */
95# define INSTALL_NEW_DTV(dtv) \
96 ({ tcbhead_t *__tcbp; \
97 __asm__ __volatile__ ("stc gbr,%0" : "=r" (__tcbp)); \
98 __tcbp->dtv = (dtv);})
99
100/* Return dtv of given thread descriptor. */
101# define GET_DTV(tcbp) \
102 (((tcbhead_t *) (tcbp))->dtv)
103
104/* Code to initially initialize the thread pointer. This might need
105 special attention since 'errno' is not yet available and if the
106 operation can cause a failure 'errno' must not be touched. */
107# define TLS_INIT_TP(tcbp, secondcall) \
108 ({ __asm__ __volatile__ ("ldc %0,gbr" : : "r" (tcbp)); 0; })
109
110/* Return the address of the dtv for the current thread. */
111# define THREAD_DTV() \
112 ({ tcbhead_t *__tcbp; \
113 __asm__ __volatile__ ("stc gbr,%0" : "=r" (__tcbp)); \
114 __tcbp->dtv;})
115
116/* Return the thread descriptor for the current thread.
117 The contained asm must *not* be marked volatile since otherwise
118 assignments like
119 struct pthread *self = thread_self();
120 do not get optimized away. */
121# define THREAD_SELF \
122 ({ struct pthread *__self; \
123 __asm__ ("stc gbr,%0" : "=r" (__self)); \
124 __self - 1;})
125
126/* Magic for libthread_db to know how to do THREAD_SELF. */
127# define DB_THREAD_SELF \
128 REGISTER (32, 32, REG_GBR * 4, -sizeof (struct pthread))
129
130/* Read member of the thread descriptor directly. */
131# define THREAD_GETMEM(descr, member) (descr->member)
132
133/* Same as THREAD_GETMEM, but the member offset can be non-constant. */
134# define THREAD_GETMEM_NC(descr, member, idx) (descr->member[idx])
135
136/* Set member of the thread descriptor directly. */
137# define THREAD_SETMEM(descr, member, value) \
138 descr->member = (value)
139
140/* Same as THREAD_SETMEM, but the member offset can be non-constant. */
141# define THREAD_SETMEM_NC(descr, member, idx, value) \
142 descr->member[idx] = (value)
143
144#define THREAD_GET_POINTER_GUARD() \
145 ({ tcbhead_t *__tcbp; \
146 __asm__ __volatile__ ("stc gbr,%0" : "=r" (__tcbp)); \
147 __tcbp->pointer_guard;})
148 #define THREAD_SET_POINTER_GUARD(value) \
149 ({ tcbhead_t *__tcbp; \
150 __asm__ __volatile__ ("stc gbr,%0" : "=r" (__tcbp)); \
151 __tcbp->pointer_guard = (value);})
152#define THREAD_COPY_POINTER_GUARD(descr) \
153 ({ tcbhead_t *__tcbp; \
154 __asm__ __volatile__ ("stc gbr,%0" : "=r" (__tcbp)); \
155 ((tcbhead_t *) (descr + 1))->pointer_guard = __tcbp->pointer_guard;})
156
157/* Get and set the global scope generation counter in struct pthread. */
158#define THREAD_GSCOPE_FLAG_UNUSED 0
159#define THREAD_GSCOPE_FLAG_USED 1
160#define THREAD_GSCOPE_FLAG_WAIT 2
161#define THREAD_GSCOPE_RESET_FLAG() \
162 do \
163 { int __res \
164 = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
165 THREAD_GSCOPE_FLAG_UNUSED); \
166 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
167 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
168 } \
169 while (0)
170#define THREAD_GSCOPE_SET_FLAG() \
171 do \
172 { \
173 THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
174 atomic_write_barrier (); \
175 } \
176 while (0)
177#define THREAD_GSCOPE_WAIT() \
178 GL(dl_wait_lookup_done) ()
179
180#endif /* __ASSEMBLER__ */
181
182#endif /* tls.h */