1 /* Definition for thread-local data handling.  NPTL/ARM version.
2    Copyright (C) 2005-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 _ARM_NPTL_TLS_H
20 #define _ARM_NPTL_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 #endif /* __ASSEMBLER__ */
30 
31 
32 #ifndef __ASSEMBLER__
33 
34 /* The TP points to the start of the thread blocks.  */
35 # define TLS_DTV_AT_TP	1
36 # define TLS_TCB_AT_TP	0
37 
38 /* Get the thread descriptor definition.  */
39 # include <nptl/descr.h>
40 
41 typedef struct
42 {
43   dtv_t *dtv;
44   void *private;
45 } tcbhead_t;
46 
47 /* This is the size of the initial TCB.  */
48 # define TLS_INIT_TCB_SIZE	sizeof (tcbhead_t)
49 
50 /* This is the size of the TCB.  */
51 # define TLS_TCB_SIZE		sizeof (tcbhead_t)
52 
53 /* This is the size we need before TCB.  */
54 # define TLS_PRE_TCB_SIZE	sizeof (struct pthread)
55 
56 /* Install the dtv pointer.  The pointer passed is to the element with
57    index -1 which contain the length.  */
58 # define INSTALL_DTV(tcbp, dtvp) \
59   (((tcbhead_t *) (tcbp))->dtv = (dtvp) + 1)
60 
61 /* Install new dtv for current thread.  */
62 # define INSTALL_NEW_DTV(dtv) \
63   (THREAD_DTV() = (dtv))
64 
65 /* Return dtv of given thread descriptor.  */
66 # define GET_DTV(tcbp) \
67   (((tcbhead_t *) (tcbp))->dtv)
68 
69 # define TLS_DEFINE_INIT_TP(tp, pd) void *tp = (pd) + 1
70 
71 /* Return the address of the dtv for the current thread.  */
72 # define THREAD_DTV() \
73   (((tcbhead_t *) __builtin_thread_pointer ())->dtv)
74 
75 /* Return the thread descriptor for the current thread.  */
76 # define THREAD_SELF \
77  ((struct pthread *)__builtin_thread_pointer () - 1)
78 
79 /* Magic for libthread_db to know how to do THREAD_SELF.  */
80 # define DB_THREAD_SELF \
81   CONST_THREAD_AREA (32, sizeof (struct pthread))
82 
83 # include <tcb-access.h>
84 
85 /* Get and set the global scope generation counter in struct pthread.  */
86 #define THREAD_GSCOPE_FLAG_UNUSED 0
87 #define THREAD_GSCOPE_FLAG_USED   1
88 #define THREAD_GSCOPE_FLAG_WAIT   2
89 #define THREAD_GSCOPE_RESET_FLAG() \
90   do									     \
91     { int __res								     \
92 	= atomic_exchange_rel (&THREAD_SELF->header.gscope_flag,	     \
93 			       THREAD_GSCOPE_FLAG_UNUSED);		     \
94       if (__res == THREAD_GSCOPE_FLAG_WAIT)				     \
95 	lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);   \
96     }									     \
97   while (0)
98 #define THREAD_GSCOPE_SET_FLAG() \
99   do									     \
100     {									     \
101       THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED;	     \
102       atomic_write_barrier ();						     \
103     }									     \
104   while (0)
105 
106 #endif /* __ASSEMBLER__ */
107 
108 #endif	/* tls.h */
109