1 /* Definition for thread-local data handling.  NPTL/Nios II version.
2    Copyright (C) 2012-2022 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 #else /* __ASSEMBLER__ */
31 # include <tcb-offsets.h>
32 #endif /* __ASSEMBLER__ */
33 
34 
35 #ifndef __ASSEMBLER__
36 
37 /* Get system call information.  */
38 # include <sysdep.h>
39 
40 /* The TP points to the start of the thread blocks.  */
41 # define TLS_DTV_AT_TP	1
42 # define TLS_TCB_AT_TP	0
43 
44 /* Get the thread descriptor definition.  */
45 # include <nptl/descr.h>
46 
47 typedef struct
48 {
49   dtv_t *dtv;
50   uintptr_t pointer_guard;
51   unsigned spare[6];
52 } tcbhead_t;
53 
54 register struct pthread *__thread_self __asm__("r23");
55 
56 #define READ_THREAD_POINTER() ((void *) __thread_self)
57 
58 /* This is the size of the initial TCB.  Because our TCB is before the thread
59    pointer, we don't need this.  */
60 # define TLS_INIT_TCB_SIZE	0
61 
62 /* This is the size of the TCB.  Because our TCB is before the thread
63    pointer, we don't need this.  */
64 # define TLS_TCB_SIZE		0
65 
66 /* This is the size we need before TCB - actually, it includes the TCB.  */
67 # define TLS_PRE_TCB_SIZE \
68   (sizeof (struct pthread)						      \
69    + ((sizeof (tcbhead_t) + __alignof (struct pthread) - 1)		      \
70       & ~(__alignof (struct pthread) - 1)))
71 
72 /* The thread pointer (in hardware register r23) points to the end of
73    the TCB + 0x7000, as for PowerPC and MIPS.  */
74 # define TLS_TCB_OFFSET 0x7000
75 
76 /* Install the dtv pointer.  The pointer passed is to the element with
77    index -1 which contain the length.  */
78 # define INSTALL_DTV(tcbp, dtvp) \
79   (((tcbhead_t *) (tcbp))[-1].dtv = (dtvp) + 1)
80 
81 /* Install new dtv for current thread.  */
82 # define INSTALL_NEW_DTV(dtv) \
83   (THREAD_DTV() = (dtv))
84 
85 /* Return dtv of given thread descriptor.  */
86 # define GET_DTV(tcbp) \
87   (((tcbhead_t *) (tcbp))[-1].dtv)
88 
89 /* Code to initially initialize the thread pointer.  */
90 # define TLS_INIT_TP(tcbp) \
91   (__thread_self = (struct pthread *) ((char *) tcbp + TLS_TCB_OFFSET), NULL)
92 
93 /* Value passed to 'clone' for initialization of the thread register.  */
94 # define TLS_DEFINE_INIT_TP(tp, pd) \
95   void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE
96 
97 /* Return the address of the dtv for the current thread.  */
98 # define THREAD_DTV() \
99   (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))[-1].dtv)
100 
101 /* Return the thread descriptor for the current thread.  */
102 # define THREAD_SELF \
103  ((struct pthread *) (READ_THREAD_POINTER ()			     \
104 		      - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
105 
106 /* Magic for libthread_db to know how to do THREAD_SELF.  */
107 # define DB_THREAD_SELF \
108   REGISTER (32, 32, 23 * 4, -TLS_PRE_TCB_SIZE - TLS_TCB_OFFSET)
109 
110 # include <tcb-access.h>
111 
112 # define THREAD_GET_POINTER_GUARD()				\
113   (((tcbhead_t *) (READ_THREAD_POINTER ()			\
114 		   - TLS_TCB_OFFSET))[-1].pointer_guard)
115 # define THREAD_SET_POINTER_GUARD(value)	\
116   (THREAD_GET_POINTER_GUARD () = (value))
117 # define THREAD_COPY_POINTER_GUARD(descr)				\
118   (((tcbhead_t *) ((void *) (descr)					\
119 		   + TLS_PRE_TCB_SIZE))[-1].pointer_guard		\
120    = THREAD_GET_POINTER_GUARD())
121 
122 /* l_tls_offset == 0 is perfectly valid on Nios II, so we have to use some
123    different value to mean unset l_tls_offset.  */
124 # define NO_TLS_OFFSET		-1
125 
126 /* Get and set the global scope generation counter in struct pthread.  */
127 #define THREAD_GSCOPE_FLAG_UNUSED 0
128 #define THREAD_GSCOPE_FLAG_USED   1
129 #define THREAD_GSCOPE_FLAG_WAIT   2
130 #define THREAD_GSCOPE_RESET_FLAG() \
131   do									     \
132     { int __res								     \
133 	= atomic_exchange_rel (&THREAD_SELF->header.gscope_flag,	     \
134 			       THREAD_GSCOPE_FLAG_UNUSED);		     \
135       if (__res == THREAD_GSCOPE_FLAG_WAIT)				     \
136 	lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);   \
137     }									     \
138   while (0)
139 #define THREAD_GSCOPE_SET_FLAG() \
140   do									     \
141     {									     \
142       THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED;	     \
143       atomic_write_barrier ();						     \
144     }									     \
145   while (0)
146 
147 #endif /* __ASSEMBLER__ */
148 
149 #endif	/* tls.h */
150