blob: f93228212bbc8a369220ad019e7768dc4135c293 [file] [log] [blame]
xf.libfc6e712025-02-07 01:54:34 -08001/* Definition for thread-local data handling. NPTL/m68k version.
2 Copyright (C) 2010-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Maxim Kuvyrkov <maxim@codesourcery.com>, 2010.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library. If not, see
18 <http://www.gnu.org/licenses/>. */
19
20#ifndef _TLS_H
21#define _TLS_H 1
22
23#include <dl-sysdep.h>
24
25#ifndef __ASSEMBLER__
26# include <stdbool.h>
27# include <stddef.h>
28# include <stdint.h>
29
30/* Type for the dtv. */
31typedef union dtv
32{
33 size_t counter;
34 struct
35 {
36 void *val;
37 bool is_static;
38 } pointer;
39} dtv_t;
40
41#else /* __ASSEMBLER__ */
42# include <tcb-offsets.h>
43#endif /* __ASSEMBLER__ */
44
45#ifndef __ASSEMBLER__
46
47/* Get system call information. */
48# include <sysdep.h>
49
50/* The TP points to the start of the thread blocks. */
51# define TLS_DTV_AT_TP 1
52# define TLS_TCB_AT_TP 0
53
54/* Get the thread descriptor definition. */
55# include <nptl/descr.h>
56
57typedef struct
58{
59 dtv_t *dtv;
60 void *private;
61} tcbhead_t;
62
63/* This is the size of the initial TCB. Because our TCB is before the thread
64 pointer, we don't need this. */
65# define TLS_INIT_TCB_SIZE 0
66
67/* Alignment requirements for the initial TCB. */
68# define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
69
70/* This is the size of the TCB. Because our TCB is before the thread
71 pointer, we don't need this. */
72# define TLS_TCB_SIZE 0
73
74/* Alignment requirements for the TCB. */
75# define TLS_TCB_ALIGN __alignof__ (struct pthread)
76
77/* This is the size we need before TCB - actually, it includes the TCB. */
78# define TLS_PRE_TCB_SIZE \
79 (sizeof (struct pthread) \
80 + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
81
82/* The thread pointer (TP) points to the end of the
83 TCB + 0x7000, as for PowerPC and MIPS. This implies that TCB address is
84 TP - 0x7000. As we define TLS_DTV_AT_TP we can
85 assume that the pthread struct is allocated immediately ahead of the
86 TCB. This implies that the pthread_descr address is
87 TP - (TLS_PRE_TCB_SIZE + 0x7000). */
88# define TLS_TCB_OFFSET 0x7000
89
90/* Install the dtv pointer. The pointer passed is to the element with
91 index -1 which contain the length. */
92# define INSTALL_DTV(tcbp, dtvp) \
93 ((tcbhead_t *) (tcbp))[-1].dtv = dtvp + 1
94
95/* Install new dtv for current thread. */
96# define INSTALL_NEW_DTV(dtv) \
97 (THREAD_DTV () = (dtv))
98
99/* Return dtv of given thread descriptor. */
100# define GET_DTV(tcbp) \
101 (((tcbhead_t *) (tcbp))[-1].dtv)
102
103/* Code to initially initialize the thread pointer. This might need
104 special attention since 'errno' is not yet available and if the
105 operation can cause a failure 'errno' must not be touched. */
106# define TLS_INIT_TP(tcbp) \
107 ({ \
108 INTERNAL_SYSCALL_DECL (err); \
109 int _sys_result; \
110 \
111 _sys_result = INTERNAL_SYSCALL (set_thread_area, err, 1, \
112 ((void *) (tcbp)) + TLS_TCB_OFFSET); \
113 INTERNAL_SYSCALL_ERROR_P (_sys_result, err) ? "unknown error" : NULL; })
114
115# define TLS_DEFINE_INIT_TP(tp, pd) \
116 void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE
117
118extern void * __m68k_read_tp (void);
119
120/* Return the address of the dtv for the current thread. */
121# define THREAD_DTV() \
122 (((tcbhead_t *) (__m68k_read_tp () - TLS_TCB_OFFSET))[-1].dtv)
123
124/* Return the thread descriptor for the current thread. */
125# define THREAD_SELF \
126 ((struct pthread *) (__m68k_read_tp () - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
127
128/* Magic for libthread_db to know how to do THREAD_SELF. */
129# define DB_THREAD_SELF \
130 CONST_THREAD_AREA (32, TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE)
131
132/* Access to data in the thread descriptor is easy. */
133# define THREAD_GETMEM(descr, member) \
134 descr->member
135# define THREAD_GETMEM_NC(descr, member, idx) \
136 descr->member[idx]
137# define THREAD_SETMEM(descr, member, value) \
138 descr->member = (value)
139# define THREAD_SETMEM_NC(descr, member, idx, value) \
140 descr->member[idx] = (value)
141
142/* l_tls_offset == 0 is perfectly valid on M68K, so we have to use some
143 different value to mean unset l_tls_offset. */
144# define NO_TLS_OFFSET -1
145
146/* Get and set the global scope generation counter in struct pthread. */
147#define THREAD_GSCOPE_FLAG_UNUSED 0
148#define THREAD_GSCOPE_FLAG_USED 1
149#define THREAD_GSCOPE_FLAG_WAIT 2
150#define THREAD_GSCOPE_RESET_FLAG() \
151 do \
152 { int __res \
153 = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
154 THREAD_GSCOPE_FLAG_UNUSED); \
155 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
156 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
157 } \
158 while (0)
159#define THREAD_GSCOPE_SET_FLAG() \
160 do \
161 { \
162 THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
163 atomic_write_barrier (); \
164 } \
165 while (0)
166#define THREAD_GSCOPE_WAIT() \
167 GL(dl_wait_lookup_done) ()
168
169#endif /* __ASSEMBLER__ */
170
171#endif /* tls.h */