Back to index

glibc  2.9
tls.h
Go to the documentation of this file.
00001 /* Definition for thread-local data handling.  NPTL/PowerPC version.
00002    Copyright (C) 2003, 2005, 2006, 2007 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #ifndef _TLS_H
00021 #define _TLS_H       1
00022 
00023 # include <dl-sysdep.h>
00024 
00025 #ifndef __ASSEMBLER__
00026 # include <stdbool.h>
00027 # include <stddef.h>
00028 # include <stdint.h>
00029 
00030 /* Type for the dtv.  */
00031 typedef union dtv
00032 {
00033   size_t counter;
00034   struct
00035   {
00036     void *val;
00037     bool is_static;
00038   } pointer;
00039 } dtv_t;
00040 
00041 #else /* __ASSEMBLER__ */
00042 # include <tcb-offsets.h>
00043 #endif /* __ASSEMBLER__ */
00044 
00045 
00046 /* We require TLS support in the tools.  */
00047 #ifndef HAVE_TLS_SUPPORT
00048 # error "TLS support is required."
00049 #endif
00050 
00051 #ifndef __ASSEMBLER__
00052 
00053 /* Get system call information.  */
00054 # include <sysdep.h>
00055 
00056 /* The TP points to the start of the thread blocks.  */
00057 # define TLS_DTV_AT_TP      1
00058 
00059 /* We use the multiple_threads field in the pthread struct */
00060 #define TLS_MULTIPLE_THREADS_IN_TCB       1
00061 
00062 /* Get the thread descriptor definition.  */
00063 # include <nptl/descr.h>
00064 
00065 /* The stack_guard is accessed directly by GCC -fstack-protector code,
00066    so it is a part of public ABI.  The dtv and pointer_guard fields
00067    are private.  */
00068 typedef struct
00069 {
00070   uintptr_t pointer_guard;
00071   uintptr_t stack_guard;
00072   dtv_t *dtv;
00073 } tcbhead_t;
00074 
00075 /* This is the size of the initial TCB.  */
00076 # define TLS_INIT_TCB_SIZE  0
00077 
00078 /* Alignment requirements for the initial TCB.  */
00079 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
00080 
00081 /* This is the size of the TCB.  */
00082 # define TLS_TCB_SIZE              0
00083 
00084 /* Alignment requirements for the TCB.  */
00085 # define TLS_TCB_ALIGN             __alignof__ (struct pthread)
00086 
00087 /* This is the size we need before TCB.  */
00088 # define TLS_PRE_TCB_SIZE \
00089   (sizeof (struct pthread)                                           \
00090    + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
00091 
00092 # ifndef __powerpc64__
00093 /* Register r2 (tp) is reserved by the ABI as "thread pointer". */
00094 register void *__thread_register __asm__ ("r2");
00095 #  define PT_THREAD_POINTER PT_R2
00096 # else
00097 /* Register r13 (tp) is reserved by the ABI as "thread pointer". */
00098 register void *__thread_register __asm__ ("r13");
00099 #  define PT_THREAD_POINTER PT_R13
00100 # endif
00101 
00102 /* The following assumes that TP (R2 or R13) points to the end of the
00103    TCB + 0x7000 (per the ABI).  This implies that TCB address is
00104    TP - 0x7000.  As we define TLS_DTV_AT_TP we can
00105    assume that the pthread struct is allocated immediately ahead of the
00106    TCB.  This implies that the pthread_descr address is
00107    TP - (TLS_PRE_TCB_SIZE + 0x7000).  */
00108 # define TLS_TCB_OFFSET     0x7000
00109 
00110 /* Install the dtv pointer.  The pointer passed is to the element with
00111    index -1 which contain the length.  */
00112 # define INSTALL_DTV(tcbp, dtvp) \
00113   ((tcbhead_t *) (tcbp))[-1].dtv = dtvp + 1
00114 
00115 /* Install new dtv for current thread.  */
00116 # define INSTALL_NEW_DTV(dtv) (THREAD_DTV() = (dtv))
00117 
00118 /* Return dtv of given thread descriptor.  */
00119 # define GET_DTV(tcbp)      (((tcbhead_t *) (tcbp))[-1].dtv)
00120 
00121 /* Code to initially initialize the thread pointer.  This might need
00122    special attention since 'errno' is not yet available and if the
00123    operation can cause a failure 'errno' must not be touched.  */
00124 # define TLS_INIT_TP(tcbp, secondcall) \
00125     (__thread_register = (void *) (tcbp) + TLS_TCB_OFFSET, NULL)
00126 
00127 /* Return the address of the dtv for the current thread.  */
00128 # define THREAD_DTV() \
00129     (((tcbhead_t *) (__thread_register - TLS_TCB_OFFSET))[-1].dtv)
00130 
00131 /* Return the thread descriptor for the current thread.  */
00132 # define THREAD_SELF \
00133     ((struct pthread *) (__thread_register \
00134                       - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
00135 
00136 /* Magic for libthread_db to know how to do THREAD_SELF.  */
00137 # define DB_THREAD_SELF                                                     \
00138   REGISTER (32, 32, PT_THREAD_POINTER * 4,                                  \
00139            - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)                      \
00140   REGISTER (64, 64, PT_THREAD_POINTER * 8,                                  \
00141            - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)
00142 
00143 /* Read member of the thread descriptor directly.  */
00144 # define THREAD_GETMEM(descr, member) ((void)(descr), (THREAD_SELF)->member)
00145 
00146 /* Same as THREAD_GETMEM, but the member offset can be non-constant.  */
00147 # define THREAD_GETMEM_NC(descr, member, idx) \
00148     ((void)(descr), (THREAD_SELF)->member[idx])
00149 
00150 /* Set member of the thread descriptor directly.  */
00151 # define THREAD_SETMEM(descr, member, value) \
00152     ((void)(descr), (THREAD_SELF)->member = (value))
00153 
00154 /* Same as THREAD_SETMEM, but the member offset can be non-constant.  */
00155 # define THREAD_SETMEM_NC(descr, member, idx, value) \
00156     ((void)(descr), (THREAD_SELF)->member[idx] = (value))
00157 
00158 /* Set the stack guard field in TCB head.  */
00159 # define THREAD_SET_STACK_GUARD(value) \
00160     (((tcbhead_t *) ((char *) __thread_register                             \
00161                    - TLS_TCB_OFFSET))[-1].stack_guard = (value))
00162 # define THREAD_COPY_STACK_GUARD(descr) \
00163     (((tcbhead_t *) ((char *) (descr)                                       \
00164                    + TLS_PRE_TCB_SIZE))[-1].stack_guard              \
00165      = ((tcbhead_t *) ((char *) __thread_register                           \
00166                      - TLS_TCB_OFFSET))[-1].stack_guard)
00167 
00168 /* Set the stack guard field in TCB head.  */
00169 # define THREAD_GET_POINTER_GUARD() \
00170     (((tcbhead_t *) ((char *) __thread_register                             \
00171                    - TLS_TCB_OFFSET))[-1].pointer_guard)
00172 # define THREAD_SET_POINTER_GUARD(value) \
00173     (THREAD_GET_POINTER_GUARD () = (value))
00174 # define THREAD_COPY_POINTER_GUARD(descr) \
00175     (((tcbhead_t *) ((char *) (descr)                                       \
00176                    + TLS_PRE_TCB_SIZE))[-1].pointer_guard                   \
00177      = THREAD_GET_POINTER_GUARD())
00178 
00179 /* l_tls_offset == 0 is perfectly valid on PPC, so we have to use some
00180    different value to mean unset l_tls_offset.  */
00181 # define NO_TLS_OFFSET             -1
00182 
00183 /* Get and set the global scope generation counter in struct pthread.  */
00184 #define THREAD_GSCOPE_FLAG_UNUSED 0
00185 #define THREAD_GSCOPE_FLAG_USED   1
00186 #define THREAD_GSCOPE_FLAG_WAIT   2
00187 #define THREAD_GSCOPE_RESET_FLAG() \
00188   do                                                                \
00189     { int __res                                                            \
00190        = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag,            \
00191                             THREAD_GSCOPE_FLAG_UNUSED);             \
00192       if (__res == THREAD_GSCOPE_FLAG_WAIT)                                \
00193        lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);   \
00194     }                                                               \
00195   while (0)
00196 #define THREAD_GSCOPE_SET_FLAG() \
00197   do                                                                \
00198     {                                                               \
00199       THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED;           \
00200       atomic_write_barrier ();                                             \
00201     }                                                               \
00202   while (0)
00203 #define THREAD_GSCOPE_WAIT() \
00204   GL(dl_wait_lookup_done) ()
00205 
00206 #endif /* __ASSEMBLER__ */
00207 
00208 #endif /* tls.h */