m68k: add NPTL/TLS support
[uclibc-ng.git] / libpthread / nptl / sysdeps / m68k / tls.h
blob213442da487b15c37be49acade28afcd154704a4
1 /* Definition for thread-local data handling. NPTL/m68k version.
2 Copyright (C) 2010-2017 Free Software Foundation, Inc.
3 Contributed by Maxim Kuvyrkov <maxim@codesourcery.com>, 2010.
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.
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.
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/>. */
19 #ifndef _TLS_H
20 #define _TLS_H 1
22 #ifndef __ASSEMBLER__
23 # include <stdbool.h>
24 # include <stddef.h>
25 # include <stdint.h>
27 /* Type for the dtv. */
28 typedef union dtv
30 size_t counter;
31 struct
33 void *val;
34 bool is_static;
35 } pointer;
36 } dtv_t;
38 /* Value used for dtv entries for which the allocation is delayed. */
39 #define TLS_DTV_UNALLOCATED ((void *) -1l)
41 #else /* __ASSEMBLER__ */
42 # include <tcb-offsets.h>
43 #endif /* __ASSEMBLER__ */
45 /* We require TLS support in the tools. */
46 #define HAVE_TLS_SUPPORT 1
47 #define HAVE_TLS_MODEL_ATTRIBUTE 1
48 #define HAVE___THREAD 1
50 /* Signal that TLS support is available. */
51 #define USE_TLS 1
53 #ifndef __ASSEMBLER__
55 /* Get system call information. */
56 # include <sysdep.h>
58 /* The TP points to the start of the thread blocks. */
59 # define TLS_DTV_AT_TP 1
61 /* Get the thread descriptor definition. */
62 # include <../../descr.h>
64 typedef struct
66 dtv_t *dtv;
67 void *private;
68 } tcbhead_t;
70 /* This is the size of the initial TCB. Because our TCB is before the thread
71 pointer, we don't need this. */
72 # define TLS_INIT_TCB_SIZE 0
74 /* Alignment requirements for the initial TCB. */
75 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
77 /* This is the size of the TCB. Because our TCB is before the thread
78 pointer, we don't need this. */
79 # define TLS_TCB_SIZE 0
81 /* Alignment requirements for the TCB. */
82 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
84 /* This is the size we need before TCB - actually, it includes the TCB. */
85 # define TLS_PRE_TCB_SIZE \
86 (sizeof (struct pthread) \
87 + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
89 /* The thread pointer (TP) points to the end of the
90 TCB + 0x7000, as for PowerPC and MIPS. This implies that TCB address is
91 TP - 0x7000. As we define TLS_DTV_AT_TP we can
92 assume that the pthread struct is allocated immediately ahead of the
93 TCB. This implies that the pthread_descr address is
94 TP - (TLS_PRE_TCB_SIZE + 0x7000). */
95 # define TLS_TCB_OFFSET 0x7000
97 /* Install the dtv pointer. The pointer passed is to the element with
98 index -1 which contain the length. */
99 # define INSTALL_DTV(tcbp, dtvp) \
100 ((tcbhead_t *) (tcbp))[-1].dtv = dtvp + 1
102 /* Install new dtv for current thread. */
103 # define INSTALL_NEW_DTV(dtv) \
104 (THREAD_DTV () = (dtv))
106 /* Return dtv of given thread descriptor. */
107 # define GET_DTV(tcbp) \
108 (((tcbhead_t *) (tcbp))[-1].dtv)
110 /* Code to initially initialize the thread pointer. This might need
111 special attention since 'errno' is not yet available and if the
112 operation can cause a failure 'errno' must not be touched. */
113 # define TLS_INIT_TP(tcbp, secondcall) \
114 ({ \
115 INTERNAL_SYSCALL_DECL (err); \
116 int result_var; \
117 result_var = INTERNAL_SYSCALL (set_thread_area, err, 1, \
118 ((void *) (tcbp)) + TLS_TCB_OFFSET); \
119 INTERNAL_SYSCALL_ERROR_P (result_var, err) \
120 ? "unknown error" : NULL; })
122 # define TLS_DEFINE_INIT_TP(tp, pd) \
123 void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE
125 extern void * __m68k_read_tp (void);
127 /* Return the address of the dtv for the current thread. */
128 # define THREAD_DTV() \
129 (((tcbhead_t *) (__m68k_read_tp () - TLS_TCB_OFFSET))[-1].dtv)
131 /* Return the thread descriptor for the current thread. */
132 # define THREAD_SELF \
133 ((struct pthread *) (__m68k_read_tp () - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
135 /* Magic for libthread_db to know how to do THREAD_SELF. */
136 # define DB_THREAD_SELF \
137 CONST_THREAD_AREA (32, TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE)
139 /* Access to data in the thread descriptor is easy. */
140 # define THREAD_GETMEM(descr, member) \
141 descr->member
142 # define THREAD_GETMEM_NC(descr, member, idx) \
143 descr->member[idx]
144 # define THREAD_SETMEM(descr, member, value) \
145 descr->member = (value)
146 # define THREAD_SETMEM_NC(descr, member, idx, value) \
147 descr->member[idx] = (value)
149 /* l_tls_offset == 0 is perfectly valid on M68K, so we have to use some
150 different value to mean unset l_tls_offset. */
151 # define NO_TLS_OFFSET -1
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); \
165 while (0)
166 #define THREAD_GSCOPE_SET_FLAG() \
167 do \
169 THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
170 atomic_write_barrier (); \
172 while (0)
173 #define THREAD_GSCOPE_WAIT() \
174 GL(dl_wait_lookup_done) ()
176 #endif /* __ASSEMBLER__ */
178 #endif /* tls.h */