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