1 /* Definition for thread-local data handling. NPTL/Nios II version.
2 Copyright (C) 2012-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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/>. */
22 #include <dl-sysdep.h>
30 #else /* __ASSEMBLER__ */
31 # include <tcb-offsets.h>
32 #endif /* __ASSEMBLER__ */
37 /* Get system call information. */
40 /* The TP points to the start of the thread blocks. */
41 # define TLS_DTV_AT_TP 1
42 # define TLS_TCB_AT_TP 0
44 /* Get the thread descriptor definition. */
45 # include <nptl/descr.h>
50 uintptr_t pointer_guard
;
54 register struct pthread
*__thread_self
__asm__("r23");
56 #define READ_THREAD_POINTER() ((void *) __thread_self)
58 /* This is the size of the initial TCB. Because our TCB is before the thread
59 pointer, we don't need this. */
60 # define TLS_INIT_TCB_SIZE 0
62 /* Alignment requirements for the initial TCB. */
63 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
65 /* This is the size of the TCB. Because our TCB is before the thread
66 pointer, we don't need this. */
67 # define TLS_TCB_SIZE 0
69 /* Alignment requirements for the TCB. */
70 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
72 /* This is the size we need before TCB - actually, it includes the TCB. */
73 # define TLS_PRE_TCB_SIZE \
74 (sizeof (struct pthread) \
75 + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
77 /* The thread pointer (in hardware register r23) points to the end of
78 the TCB + 0x7000, as for PowerPC and MIPS. */
79 # define TLS_TCB_OFFSET 0x7000
81 /* Install the dtv pointer. The pointer passed is to the element with
82 index -1 which contain the length. */
83 # define INSTALL_DTV(tcbp, dtvp) \
84 (((tcbhead_t *) (tcbp))[-1].dtv = (dtvp) + 1)
86 /* Install new dtv for current thread. */
87 # define INSTALL_NEW_DTV(dtv) \
88 (THREAD_DTV() = (dtv))
90 /* Return dtv of given thread descriptor. */
91 # define GET_DTV(tcbp) \
92 (((tcbhead_t *) (tcbp))[-1].dtv)
94 /* Code to initially initialize the thread pointer. */
95 # define TLS_INIT_TP(tcbp) \
96 (__thread_self = (struct pthread *) ((char *) tcbp + TLS_TCB_OFFSET), NULL)
98 /* Value passed to 'clone' for initialization of the thread register. */
99 # define TLS_DEFINE_INIT_TP(tp, pd) \
100 void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE
102 /* Return the address of the dtv for the current thread. */
103 # define THREAD_DTV() \
104 (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))[-1].dtv)
106 /* Return the thread descriptor for the current thread. */
107 # define THREAD_SELF \
108 ((struct pthread *) (READ_THREAD_POINTER () \
109 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
111 /* Magic for libthread_db to know how to do THREAD_SELF. */
112 # define DB_THREAD_SELF \
113 REGISTER (32, 32, 23 * 4, -TLS_PRE_TCB_SIZE - TLS_TCB_OFFSET)
115 /* Access to data in the thread descriptor is easy. */
116 # define THREAD_GETMEM(descr, member) \
118 # define THREAD_GETMEM_NC(descr, member, idx) \
120 # define THREAD_SETMEM(descr, member, value) \
121 descr->member = (value)
122 # define THREAD_SETMEM_NC(descr, member, idx, value) \
123 descr->member[idx] = (value)
125 # define THREAD_GET_POINTER_GUARD() \
126 (((tcbhead_t *) (READ_THREAD_POINTER () \
127 - TLS_TCB_OFFSET))[-1].pointer_guard)
128 # define THREAD_SET_POINTER_GUARD(value) \
129 (THREAD_GET_POINTER_GUARD () = (value))
130 # define THREAD_COPY_POINTER_GUARD(descr) \
131 (((tcbhead_t *) ((void *) (descr) \
132 + TLS_PRE_TCB_SIZE))[-1].pointer_guard \
133 = THREAD_GET_POINTER_GUARD())
135 /* l_tls_offset == 0 is perfectly valid on Nios II, so we have to use some
136 different value to mean unset l_tls_offset. */
137 # define NO_TLS_OFFSET -1
139 /* Get and set the global scope generation counter in struct pthread. */
140 #define THREAD_GSCOPE_FLAG_UNUSED 0
141 #define THREAD_GSCOPE_FLAG_USED 1
142 #define THREAD_GSCOPE_FLAG_WAIT 2
143 #define THREAD_GSCOPE_RESET_FLAG() \
146 = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
147 THREAD_GSCOPE_FLAG_UNUSED); \
148 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
149 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
152 #define THREAD_GSCOPE_SET_FLAG() \
155 THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
156 atomic_write_barrier (); \
159 #define THREAD_GSCOPE_WAIT() \
160 GL(dl_wait_lookup_done) ()
162 #endif /* __ASSEMBLER__ */