1 /* Definition for thread-local data handling.  NPTL/Nios II version.
2    Copyright (C) 2012-2016 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	1
21 
22 #ifndef __ASSEMBLER__
23 # include <stdbool.h>
24 # include <stddef.h>
25 # include <stdint.h>
26 
27 /* Type for the dtv.  */
28 typedef union dtv
29 {
30   size_t counter;
31   struct
32   {
33     void *val;
34     bool is_static;
35   } pointer;
36 } dtv_t;
37 
38 #else /* __ASSEMBLER__ */
39 # include <tcb-offsets.h>
40 #endif /* __ASSEMBLER__ */
41 
42 /* We require TLS support in the tools.  */
43 #define HAVE_TLS_SUPPORT                1
44 #define HAVE_TLS_MODEL_ATTRIBUTE        1
45 #define HAVE___THREAD                   1
46 
47 /* Signal that TLS support is available.  */
48 #define USE_TLS	1
49 
50 #ifndef __ASSEMBLER__
51 
52 /* Get system call information.  */
53 # include <sysdep.h>
54 
55 /* The TP points to the start of the thread blocks.  */
56 # define TLS_DTV_AT_TP	1
57 
58 /* Get the thread descriptor definition.  */
59 # include <../../descr.h>
60 
61 typedef struct
62 {
63   dtv_t *dtv;
64   uintptr_t pointer_guard;
65   unsigned spare[6];
66 } tcbhead_t;
67 
68 register struct pthread *__thread_self __asm__("r23");
69 
70 #define READ_THREAD_POINTER() ((void *) __thread_self)
71 
72 /* This is the size of the initial TCB.  Because our TCB is before the thread
73    pointer, we don't need this.  */
74 # define TLS_INIT_TCB_SIZE	0
75 
76 /* Alignment requirements for the initial TCB.  */
77 # define TLS_INIT_TCB_ALIGN	__alignof__ (struct pthread)
78 
79 /* This is the size of the TCB.  Because our TCB is before the thread
80    pointer, we don't need this.  */
81 # define TLS_TCB_SIZE		0
82 
83 /* Alignment requirements for the TCB.  */
84 # define TLS_TCB_ALIGN		__alignof__ (struct pthread)
85 
86 /* This is the size we need before TCB - actually, it includes the TCB.  */
87 # define TLS_PRE_TCB_SIZE \
88   (sizeof (struct pthread)						      \
89    + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
90 
91 /* The thread pointer (in hardware register r23) points to the end of
92    the TCB + 0x7000, as for PowerPC and MIPS.  */
93 # define TLS_TCB_OFFSET 0x7000
94 
95 /* Install the dtv pointer.  The pointer passed is to the element with
96    index -1 which contain the length.  */
97 # define INSTALL_DTV(tcbp, dtvp) \
98   (((tcbhead_t *) (tcbp))[-1].dtv = (dtvp) + 1)
99 
100 /* Install new dtv for current thread.  */
101 # define INSTALL_NEW_DTV(dtv) \
102   (THREAD_DTV() = (dtv))
103 
104 /* Return dtv of given thread descriptor.  */
105 # define GET_DTV(tcbp) \
106   (((tcbhead_t *) (tcbp))[-1].dtv)
107 
108 /* Code to initially initialize the thread pointer.  */
109 # define TLS_INIT_TP(tcbp, secondcall) \
110   (__thread_self = (struct pthread *) ((char *) tcbp + TLS_TCB_OFFSET), NULL)
111 
112 /* Value passed to 'clone' for initialization of the thread register.  */
113 # define TLS_DEFINE_INIT_TP(tp, pd) \
114   void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE
115 
116 /* Return the address of the dtv for the current thread.  */
117 # define THREAD_DTV() \
118   (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))[-1].dtv)
119 
120 /* Return the thread descriptor for the current thread.  */
121 # define THREAD_SELF \
122  ((struct pthread *) (READ_THREAD_POINTER ()			     \
123 		      - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
124 
125 /* Magic for libthread_db to know how to do THREAD_SELF.  */
126 # define DB_THREAD_SELF \
127   REGISTER (32, 32, 23 * 4, -TLS_PRE_TCB_SIZE - TLS_TCB_OFFSET)
128 
129 /* Access to data in the thread descriptor is easy.  */
130 # define THREAD_GETMEM(descr, member) \
131   descr->member
132 # define THREAD_GETMEM_NC(descr, member, idx) \
133   descr->member[idx]
134 # define THREAD_SETMEM(descr, member, value) \
135   descr->member = (value)
136 # define THREAD_SETMEM_NC(descr, member, idx, value) \
137   descr->member[idx] = (value)
138 
139 # define THREAD_GET_POINTER_GUARD()				\
140   (((tcbhead_t *) (READ_THREAD_POINTER ()			\
141 		   - TLS_TCB_OFFSET))[-1].pointer_guard)
142 # define THREAD_SET_POINTER_GUARD(value)	\
143   (THREAD_GET_POINTER_GUARD () = (value))
144 # define THREAD_COPY_POINTER_GUARD(descr)				\
145   (((tcbhead_t *) ((void *) (descr)					\
146 		   + TLS_PRE_TCB_SIZE))[-1].pointer_guard		\
147    = THREAD_GET_POINTER_GUARD())
148 
149 /* l_tls_offset == 0 is perfectly valid on Nios II, so we have to use some
150    different value to mean unset l_tls_offset.  */
151 # define NO_TLS_OFFSET		-1
152 
153 /* Get and set the global scope generation counter in struct pthread.  */
154 #define THREAD_GSCOPE_FLAG_UNUSED 0
155 #define THREAD_GSCOPE_FLAG_USED   1
156 #define THREAD_GSCOPE_FLAG_WAIT   2
157 #define THREAD_GSCOPE_RESET_FLAG() \
158   do									     \
159     { int __res								     \
160 	= atomic_exchange_rel (&THREAD_SELF->header.gscope_flag,	     \
161 			       THREAD_GSCOPE_FLAG_UNUSED);		     \
162       if (__res == THREAD_GSCOPE_FLAG_WAIT)				     \
163 	lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);   \
164     }									     \
165   while (0)
166 #define THREAD_GSCOPE_SET_FLAG() \
167   do									     \
168     {									     \
169       THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED;	     \
170       atomic_write_barrier ();						     \
171     }									     \
172   while (0)
173 #define THREAD_GSCOPE_WAIT() \
174   GL(dl_wait_lookup_done) ()
175 
176 #endif /* __ASSEMBLER__ */
177 
178 #endif	/* tls.h */
179