]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mips/nptl/tls.h
Add MIPS NPTL support
[thirdparty/glibc.git] / sysdeps / mips / nptl / tls.h
1 /* Definition for thread-local data handling. NPTL/MIPS version.
2 Copyright (C) 2005 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
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 /* Note: rd must be $v1 to be ABI-conformant. */
42 # define READ_THREAD_POINTER() \
43 ({ void *__result; \
44 asm volatile (".set\tpush\n\t.set\tmips32r2\n\t" \
45 "rdhwr\t%0, $29\n\t.set\tpop" : "=v" (__result)); \
46 __result; })
47
48 #else /* __ASSEMBLER__ */
49 # include <tcb-offsets.h>
50
51 # define READ_THREAD_POINTER(rd) \
52 .set push; \
53 .set mips32r2; \
54 rdhwr rd, $29; \
55 .set pop
56 #endif /* __ASSEMBLER__ */
57
58
59 /* We require TLS support in the tools. */
60 #ifndef HAVE_TLS_SUPPORT
61 # error "TLS support is required."
62 #endif
63
64 /* Signal that TLS support is available. */
65 #define USE_TLS 1
66
67 #ifndef __ASSEMBLER__
68
69 /* Get system call information. */
70 # include <sysdep.h>
71
72 /* The TP points to the start of the thread blocks. */
73 # define TLS_DTV_AT_TP 1
74
75 /* Get the thread descriptor definition. */
76 # include <nptl/descr.h>
77
78 typedef struct
79 {
80 dtv_t *dtv;
81 void *private;
82 } tcbhead_t;
83
84 /* This is the size of the initial TCB. Because our TCB is before the thread
85 pointer, we don't need this. */
86 # define TLS_INIT_TCB_SIZE 0
87
88 /* Alignment requirements for the initial TCB. */
89 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
90
91 /* This is the size of the TCB. Because our TCB is before the thread
92 pointer, we don't need this. */
93 # define TLS_TCB_SIZE 0
94
95 /* Alignment requirements for the TCB. */
96 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
97
98 /* This is the size we need before TCB - actually, it includes the TCB. */
99 # define TLS_PRE_TCB_SIZE \
100 (sizeof (struct pthread) \
101 + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
102
103 /* The thread pointer (in hardware register $29) points to the end of
104 the TCB + 0x7000, as for PowerPC. The pthread_descr structure is
105 immediately in front of the TCB. */
106 # define TLS_TCB_OFFSET 0x7000
107
108 /* Install the dtv pointer. The pointer passed is to the element with
109 index -1 which contain the length. */
110 # define INSTALL_DTV(tcbp, dtvp) \
111 (((tcbhead_t *) (tcbp))[-1].dtv = (dtvp) + 1)
112
113 /* Install new dtv for current thread. */
114 # define INSTALL_NEW_DTV(dtv) \
115 (THREAD_DTV() = (dtv))
116
117 /* Return dtv of given thread descriptor. */
118 # define GET_DTV(tcbp) \
119 (((tcbhead_t *) (tcbp))[-1].dtv)
120
121 /* Code to initially initialize the thread pointer. This might need
122 special attention since 'errno' is not yet available and if the
123 operation can cause a failure 'errno' must not be touched. */
124 # define TLS_INIT_TP(tcbp, secondcall) \
125 ({ INTERNAL_SYSCALL_DECL (err); \
126 long result_var; \
127 result_var = INTERNAL_SYSCALL (set_thread_area, err, 1, \
128 (char *) (tcbp) + TLS_TCB_OFFSET); \
129 INTERNAL_SYSCALL_ERROR_P (result_var, err) \
130 ? "unknown error" : NULL; })
131
132 /* Return the address of the dtv for the current thread. */
133 # define THREAD_DTV() \
134 (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))[-1].dtv)
135
136 /* Return the thread descriptor for the current thread. */
137 # define THREAD_SELF \
138 ((struct pthread *) (READ_THREAD_POINTER () \
139 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
140
141 /* Magic for libthread_db to know how to do THREAD_SELF. */
142 # define DB_THREAD_SELF \
143 CONST_THREAD_AREA (32, TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE)
144
145 /* Access to data in the thread descriptor is easy. */
146 # define THREAD_GETMEM(descr, member) \
147 descr->member
148 # define THREAD_GETMEM_NC(descr, member, idx) \
149 descr->member[idx]
150 # define THREAD_SETMEM(descr, member, value) \
151 descr->member = (value)
152 # define THREAD_SETMEM_NC(descr, member, idx, value) \
153 descr->member[idx] = (value)
154
155 /* l_tls_offset == 0 is perfectly valid on MIPS, so we have to use some
156 different value to mean unset l_tls_offset. */
157 # define NO_TLS_OFFSET -1
158
159 #endif /* __ASSEMBLER__ */
160
161 #endif /* tls.h */