1 /* Definitions for thread-local data handling.  NPTL/C-SKY version.
2    Copyright (C) 2018-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 #ifndef __ASSEMBLER__
23 
24 # include <stdbool.h>
25 # include <stddef.h>
26 # include <stdint.h>
27 # include <dl-dtv.h>
28 
29 /* Define r31 as thread pointer register.  */
30 # define READ_THREAD_POINTER() \
31   ({ void *__result;						\
32      __asm__ __volatile__ ("mov %0, r31"			\
33                            : "=r" (__result));			\
34      __result; })
35 
36 #else
37 /* Define r31 as thread pointer register.  */
38 # define READ_THREAD_POINTER() \
39 	mov r0, r31;
40 #endif /* __ASSEMBLER__ */
41 
42 #ifndef __ASSEMBLER__
43 
44 /* Get system call information.  */
45 # include <sysdep.h>
46 
47 /* The TP points to the start of the thread blocks.  */
48 # define TLS_DTV_AT_TP	1
49 # define TLS_TCB_AT_TP	0
50 
51 /* Get the thread descriptor definition.  */
52 # include <nptl/descr.h>
53 
54 typedef struct
55 {
56   dtv_t *dtv;
57   void *private;
58 } tcbhead_t;
59 
60 /* This is the size of the initial TCB.  */
61 # define TLS_INIT_TCB_SIZE	sizeof (tcbhead_t)
62 
63 /* This is the size of the TCB.  */
64 # define TLS_TCB_SIZE		sizeof (tcbhead_t)
65 
66 /* This is the size we need before TCB.  */
67 # define TLS_PRE_TCB_SIZE	sizeof (struct pthread)
68 
69 /* The thread pointer tp points to the end of the TCB.
70    The pthread_descr structure is immediately in front of the TCB.  */
71 # define TLS_TCB_OFFSET		0
72 
73 /* Install the dtv pointer.  The pointer passed is to the element with
74    index -1 which contain the length.  */
75 # define INSTALL_DTV(tcbp, dtvp) \
76   (((tcbhead_t *) (tcbp))->dtv = (dtvp) + 1)
77 
78 /* Install new dtv for current thread.  */
79 # define INSTALL_NEW_DTV(dtv) \
80   (THREAD_DTV() = (dtv))
81 
82 /* Return dtv of given thread descriptor.  */
83 # define GET_DTV(tcbp) \
84   (((tcbhead_t *) (tcbp))->dtv)
85 
86 # define TLS_DEFINE_INIT_TP(tp, pd) void *tp = (pd) + 1
87 
88 /* Code to initially initialize the thread pointer.  This might need
89    special attention since 'errno' is not yet available and if the
90    operation can cause a failure 'errno' must not be touched.  */
91 # define TLS_INIT_TP(tcbp) \
92   ({ long int result_var;						\
93      result_var = INTERNAL_SYSCALL_CALL (set_thread_area, 		\
94                     (char *) (tcbp) + TLS_TCB_OFFSET);			\
95      INTERNAL_SYSCALL_ERROR_P (result_var)				\
96        ? "unknown error" : NULL; })
97 
98 /* Return the address of the dtv for the current thread.  */
99 # define THREAD_DTV() \
100   (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))->dtv)
101 
102 /* Return the thread descriptor for the current thread.  */
103 # undef THREAD_SELF
104 # define THREAD_SELF \
105   ((struct pthread *) (READ_THREAD_POINTER ()				\
106 		       - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
107 
108 /* Magic for libthread_db to know how to do THREAD_SELF.  */
109 # define DB_THREAD_SELF \
110   CONST_THREAD_AREA (32, sizeof (struct pthread))
111 
112 # include <tcb-access.h>
113 
114 /* Get and set the global scope generation counter in struct pthread.  */
115 # define THREAD_GSCOPE_FLAG_UNUSED 0
116 # define THREAD_GSCOPE_FLAG_USED   1
117 # define THREAD_GSCOPE_FLAG_WAIT   2
118 # define THREAD_GSCOPE_RESET_FLAG() \
119   do									      \
120     { int __res								      \
121 	= atomic_exchange_rel (&THREAD_SELF->header.gscope_flag,	      \
122 			       THREAD_GSCOPE_FLAG_UNUSED);		      \
123       if (__res == THREAD_GSCOPE_FLAG_WAIT)				      \
124 	lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);    \
125     }									      \
126   while (0)
127 # define THREAD_GSCOPE_SET_FLAG() \
128   do									      \
129     {									      \
130       THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED;	      \
131       atomic_write_barrier ();						      \
132     }									      \
133   while (0)
134 
135 #endif /* __ASSEMBLER__ */
136 
137 #endif /* tls.h */
138