Remove mips dependency on alpha.
[jlayton/glibc.git] / ports / sysdeps / m68k / nptl / tls.h
1 /* Definition for thread-local data handling.  NPTL/m68k version.
2    Copyright (C) 2010-2014 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.  */
31 typedef 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
53 /* Get the thread descriptor definition.  */
54 # include <nptl/descr.h>
55
56 typedef struct
57 {
58   dtv_t *dtv;
59   void *private;
60 } tcbhead_t;
61
62 /* This is the size of the initial TCB.  Because our TCB is before the thread
63    pointer, we don't need this.  */
64 # define TLS_INIT_TCB_SIZE      0
65
66 /* Alignment requirements for the initial TCB.  */
67 # define TLS_INIT_TCB_ALIGN     __alignof__ (struct pthread)
68
69 /* This is the size of the TCB.  Because our TCB is before the thread
70    pointer, we don't need this.  */
71 # define TLS_TCB_SIZE           0
72
73 /* Alignment requirements for the TCB.  */
74 # define TLS_TCB_ALIGN          __alignof__ (struct pthread)
75
76 /* This is the size we need before TCB - actually, it includes the TCB.  */
77 # define TLS_PRE_TCB_SIZE                                               \
78   (sizeof (struct pthread)                                              \
79    + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
80
81 /* The thread pointer (TP) points to the end of the
82    TCB + 0x7000, as for PowerPC and MIPS.  This implies that TCB address is
83    TP - 0x7000.  As we define TLS_DTV_AT_TP we can
84    assume that the pthread struct is allocated immediately ahead of the
85    TCB.  This implies that the pthread_descr address is
86    TP - (TLS_PRE_TCB_SIZE + 0x7000).  */
87 # define TLS_TCB_OFFSET 0x7000
88
89 /* Install the dtv pointer.  The pointer passed is to the element with
90    index -1 which contain the length.  */
91 # define INSTALL_DTV(tcbp, dtvp) \
92   ((tcbhead_t *) (tcbp))[-1].dtv = dtvp + 1
93
94 /* Install new dtv for current thread.  */
95 # define INSTALL_NEW_DTV(dtv) \
96   (THREAD_DTV () = (dtv))
97
98 /* Return dtv of given thread descriptor.  */
99 # define GET_DTV(tcbp) \
100   (((tcbhead_t *) (tcbp))[-1].dtv)
101
102 /* Code to initially initialize the thread pointer.  This might need
103    special attention since 'errno' is not yet available and if the
104    operation can cause a failure 'errno' must not be touched.  */
105 # define TLS_INIT_TP(tcbp, secondcall)                                  \
106   ({                                                                    \
107     INTERNAL_SYSCALL_DECL (err);                                        \
108     int _sys_result;                                                    \
109                                                                         \
110     _sys_result = INTERNAL_SYSCALL (set_thread_area, err, 1,            \
111                                     ((void *) (tcbp)) + TLS_TCB_OFFSET); \
112     INTERNAL_SYSCALL_ERROR_P (_sys_result, err) ? "unknown error" : NULL; })
113
114 extern void * __m68k_read_tp (void);
115
116 /* Return the address of the dtv for the current thread.  */
117 # define THREAD_DTV() \
118   (((tcbhead_t *) (__m68k_read_tp () - TLS_TCB_OFFSET))[-1].dtv)
119
120 /* Return the thread descriptor for the current thread.  */
121 # define THREAD_SELF \
122   ((struct pthread *) (__m68k_read_tp () - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
123
124 /* Magic for libthread_db to know how to do THREAD_SELF.  */
125 # define DB_THREAD_SELF \
126   CONST_THREAD_AREA (32, TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE)
127
128 /* Access to data in the thread descriptor is easy.  */
129 # define THREAD_GETMEM(descr, member) \
130   descr->member
131 # define THREAD_GETMEM_NC(descr, member, idx) \
132   descr->member[idx]
133 # define THREAD_SETMEM(descr, member, value) \
134   descr->member = (value)
135 # define THREAD_SETMEM_NC(descr, member, idx, value) \
136   descr->member[idx] = (value)
137
138 /* l_tls_offset == 0 is perfectly valid on M68K, so we have to use some
139    different value to mean unset l_tls_offset.  */
140 # define NO_TLS_OFFSET          -1
141
142 /* Get and set the global scope generation counter in struct pthread.  */
143 #define THREAD_GSCOPE_FLAG_UNUSED 0
144 #define THREAD_GSCOPE_FLAG_USED   1
145 #define THREAD_GSCOPE_FLAG_WAIT   2
146 #define THREAD_GSCOPE_RESET_FLAG()                                      \
147   do                                                                    \
148     { int __res                                                         \
149         = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag,        \
150                                THREAD_GSCOPE_FLAG_UNUSED);              \
151       if (__res == THREAD_GSCOPE_FLAG_WAIT)                             \
152         lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
153     }                                                                   \
154   while (0)
155 #define THREAD_GSCOPE_SET_FLAG()                                        \
156   do                                                                    \
157     {                                                                   \
158       THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED;        \
159       atomic_write_barrier ();                                          \
160     }                                                                   \
161   while (0)
162 #define THREAD_GSCOPE_WAIT() \
163   GL(dl_wait_lookup_done) ()
164
165 #endif /* __ASSEMBLER__ */
166
167 #endif  /* tls.h */