1 /* Definitions for thread-local data handling.  NPTL/sparc version.
2    Copyright (C) 2003-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
21 
22 #include <dl-sysdep.h>
23 #ifndef __ASSEMBLER__
24 # include <stdbool.h>
25 # include <stddef.h>
26 # include <stdint.h>
27 # include <stdlib.h>
28 # include <list.h>
29 # include <kernel-features.h>
30 # include <dl-dtv.h>
31 
32 typedef struct
33 {
34   void *tcb;		/* Pointer to the TCB.  Not necessary the
35 			   thread descriptor used by libpthread.  */
36   dtv_t *dtv;
37   void *self;
38   int multiple_threads;
39 #if __WORDSIZE == 64
40   int gscope_flag;
41 #endif
42   uintptr_t sysinfo;
43   uintptr_t stack_guard;
44   uintptr_t pointer_guard;
45 #if __WORDSIZE != 64
46   int gscope_flag;
47 #endif
48 } tcbhead_t;
49 
50 #else /* __ASSEMBLER__ */
51 # include <tcb-offsets.h>
52 #endif /* __ASSEMBLER__ */
53 
54 
55 #ifndef __ASSEMBLER__
56 /* Get system call information.  */
57 # include <sysdep.h>
58 
59 register struct pthread *__thread_self __asm__("%g7");
60 
61 /* This is the size of the initial TCB.  Can't be just sizeof (tcbhead_t),
62    because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
63    struct pthread even when not linked with -lpthread.  */
64 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
65 
66 /* This is the size of the TCB.  */
67 # define TLS_TCB_SIZE sizeof (struct pthread)
68 
69 /* The TCB can have any size and the memory following the address the
70    thread pointer points to is unspecified.  Allocate the TCB there.  */
71 # define TLS_TCB_AT_TP	1
72 # define TLS_DTV_AT_TP	0
73 
74 /* Get the thread descriptor definition.  */
75 # include <nptl/descr.h>
76 
77 /* Install the dtv pointer.  The pointer passed is to the element with
78    index -1 which contain the length.  */
79 # define INSTALL_DTV(descr, dtvp) \
80   ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
81 
82 /* Install new dtv for current thread.  */
83 # define INSTALL_NEW_DTV(DTV) \
84   (((tcbhead_t *) __thread_self)->dtv = (DTV))
85 
86 /* Return dtv of given thread descriptor.  */
87 # define GET_DTV(descr) \
88   (((tcbhead_t *) (descr))->dtv)
89 
90 /* Code to initially initialize the thread pointer.  */
91 # define TLS_INIT_TP(descr) \
92   (__thread_self = (__typeof (__thread_self)) (descr), NULL)
93 
94 /* Value passed to 'clone' for initialization of the thread register.  */
95 # define TLS_DEFINE_INIT_TP(tp, pd) void *tp = (pd)
96 
97 /* Return the address of the dtv for the current thread.  */
98 # define THREAD_DTV() \
99   (((tcbhead_t *) __thread_self)->dtv)
100 
101 /* Return the thread descriptor for the current thread.  */
102 #define THREAD_SELF  __thread_self
103 
104 /* Magic for libthread_db to know how to do THREAD_SELF.  */
105 # define DB_THREAD_SELF \
106   REGISTER (32, 32, 10 * 4, 0) \
107   REGISTER (64, __WORDSIZE, (6 * 8) + (__WORDSIZE==64?0:4), 0)
108 
109 # include <tcb-access.h>
110 
111 /* Set the stack guard field in TCB head.  */
112 #define THREAD_SET_STACK_GUARD(value) \
113   THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
114 # define THREAD_COPY_STACK_GUARD(descr) \
115   ((descr)->header.stack_guard \
116    = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
117 
118 /* Get/set the stack guard field in TCB head.  */
119 #define THREAD_GET_POINTER_GUARD() \
120   THREAD_GETMEM (THREAD_SELF, header.pointer_guard)
121 #define THREAD_SET_POINTER_GUARD(value) \
122   THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
123 # define THREAD_COPY_POINTER_GUARD(descr) \
124   ((descr)->header.pointer_guard = THREAD_GET_POINTER_GUARD ())
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