1 /* Definition for thread-local data handling.  NPTL/MIPS version.
2    Copyright (C) 2005-2021 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    <https://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 # include <dl-dtv.h>
29 
30 /* Get system call information.  */
31 # include <sysdep.h>
32 
33 #ifdef __mips16
34 /* MIPS16 uses GCC builtin to access the TP.  */
35 # define READ_THREAD_POINTER() (__builtin_thread_pointer ())
36 #else
37 /* Note: rd must be $v1 to be ABI-conformant.  */
38 # if defined (__mips_isa_rev) &&  __mips_isa_rev >= 2
39 #  define READ_THREAD_POINTER() \
40      ({ void *__result;							      \
41         asm volatile ("rdhwr\t%0, $29" : "=v" (__result));	      	      \
42         __result; })
43 # else
44 #  define READ_THREAD_POINTER() \
45      ({ void *__result;							      \
46         asm volatile (".set\tpush\n\t.set\tmips32r2\n\t"			      \
47 		      "rdhwr\t%0, $29\n\t.set\tpop" : "=v" (__result));	      \
48         __result; })
49 # endif
50 #endif
51 
52 #else /* __ASSEMBLER__ */
53 # if __mips_isa_rev >= 2
54 #  define READ_THREAD_POINTER(rd) rdhwr	rd, $29
55 # else
56 #  define READ_THREAD_POINTER(rd) \
57 	 .set	push;							      \
58 	 .set	mips32r2;						      \
59 	 rdhwr	rd, $29;						      \
60 	 .set	pop
61 # endif
62 #endif /* __ASSEMBLER__ */
63 
64 
65 #ifndef __ASSEMBLER__
66 
67 /* The TP points to the start of the thread blocks.  */
68 # define TLS_DTV_AT_TP	1
69 # define TLS_TCB_AT_TP	0
70 
71 /* Get the thread descriptor definition.  */
72 # include <nptl/descr.h>
73 
74 typedef struct
75 {
76   dtv_t *dtv;
77   void *private;
78 } tcbhead_t;
79 
80 /* This is the size of the initial TCB.  Because our TCB is before the thread
81    pointer, we don't need this.  */
82 # define TLS_INIT_TCB_SIZE	0
83 
84 /* This is the size of the TCB.  Because our TCB is before the thread
85    pointer, we don't need this.  */
86 # define TLS_TCB_SIZE		0
87 
88 /* This is the size we need before TCB - actually, it includes the TCB.  */
89 # define TLS_PRE_TCB_SIZE \
90   (sizeof (struct pthread)						      \
91    + ((sizeof (tcbhead_t) + __alignof (struct pthread) - 1)		      \
92       & ~(__alignof (struct pthread) - 1)))
93 
94 /* The thread pointer (in hardware register $29) points to the end of
95    the TCB + 0x7000, as for PowerPC.  The pthread_descr structure is
96    immediately in front of the TCB.  */
97 # define TLS_TCB_OFFSET	0x7000
98 
99 /* Install the dtv pointer.  The pointer passed is to the element with
100    index -1 which contain the length.  */
101 # define INSTALL_DTV(tcbp, dtvp) \
102   (((tcbhead_t *) (tcbp))[-1].dtv = (dtvp) + 1)
103 
104 /* Install new dtv for current thread.  */
105 # define INSTALL_NEW_DTV(dtv) \
106   (THREAD_DTV() = (dtv))
107 
108 /* Return dtv of given thread descriptor.  */
109 # define GET_DTV(tcbp) \
110   (((tcbhead_t *) (tcbp))[-1].dtv)
111 
112 /* Code to initially initialize the thread pointer.  This might need
113    special attention since 'errno' is not yet available and if the
114    operation can cause a failure 'errno' must not be touched.  */
115 # define TLS_INIT_TP(tcbp) \
116   ({ long int result_var;						\
117      result_var = INTERNAL_SYSCALL_CALL (set_thread_area, 		\
118 				    (char *) (tcbp) + TLS_TCB_OFFSET);	\
119      INTERNAL_SYSCALL_ERROR_P (result_var)				\
120        ? "unknown error" : NULL; })
121 
122 /* Value passed to 'clone' for initialization of the thread register.  */
123 # define TLS_DEFINE_INIT_TP(tp, pd) \
124   void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE
125 
126 /* Return the address of the dtv for the current thread.  */
127 # define THREAD_DTV() \
128   (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))[-1].dtv)
129 
130 /* Return the thread descriptor for the current thread.  */
131 # define THREAD_SELF \
132  ((struct pthread *) (READ_THREAD_POINTER ()			     \
133 		      - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
134 
135 /* Magic for libthread_db to know how to do THREAD_SELF.  */
136 # define DB_THREAD_SELF \
137   CONST_THREAD_AREA (32, TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE)
138 
139 /* Access to data in the thread descriptor is easy.  */
140 # include <tcb-access.h>
141 
142 /* l_tls_offset == 0 is perfectly valid on MIPS, so we have to use some
143    different value to mean unset l_tls_offset.  */
144 # define NO_TLS_OFFSET		-1
145 
146 /* Get and set the global scope generation counter in struct pthread.  */
147 #define THREAD_GSCOPE_FLAG_UNUSED 0
148 #define THREAD_GSCOPE_FLAG_USED   1
149 #define THREAD_GSCOPE_FLAG_WAIT   2
150 #define THREAD_GSCOPE_RESET_FLAG() \
151   do									     \
152     { int __res								     \
153 	= atomic_exchange_rel (&THREAD_SELF->header.gscope_flag,	     \
154 			       THREAD_GSCOPE_FLAG_UNUSED);		     \
155       if (__res == THREAD_GSCOPE_FLAG_WAIT)				     \
156 	lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);   \
157     }									     \
158   while (0)
159 #define THREAD_GSCOPE_SET_FLAG() \
160   do									     \
161     {									     \
162       THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED;	     \
163       atomic_write_barrier ();						     \
164     }									     \
165   while (0)
166 
167 #endif /* __ASSEMBLER__ */
168 
169 #endif	/* tls.h */
170