Back to index

glibc  2.9
tls.h
Go to the documentation of this file.
00001 /* Definitions for thread-local data handling.  linuxthreads/PPC version.
00002    Copyright (C) 2003, 2005 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
00022 
00023 #ifndef __ASSEMBLER__
00024 
00025 # include <pt-machine.h>
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 #ifdef HAVE_TLS_SUPPORT
00046 
00047 /* Signal that TLS support is available.  */
00048 # define USE_TLS     1
00049 
00050 # ifndef __ASSEMBLER__
00051 
00052 /* The stack_guard is accessed directly by GCC -fstack-protector code,
00053    so it is a part of public ABI.  The dtv and pointer_guard fields
00054    are private.  */
00055 typedef struct
00056 { 
00057   uintptr_t pointer_guard;
00058   uintptr_t stack_guard;
00059   dtv_t *dtv;
00060 } tcbhead_t;
00061 
00062 /* This is the size of the initial TCB.  */
00063 #  define TLS_INIT_TCB_SIZE 0
00064 
00065 /* Alignment requirements for the initial TCB.  */
00066 #  define TLS_INIT_TCB_ALIGN       __alignof__ (struct _pthread_descr_struct)
00067 
00068 /* This is the size of the TCB.  */
00069 #  define TLS_TCB_SIZE             0
00070 
00071 /* Alignment requirements for the TCB.  */
00072 #  define TLS_TCB_ALIGN            __alignof__ (struct _pthread_descr_struct)
00073 
00074 /* This is the size we need before TCB.  */
00075 #  define TLS_PRE_TCB_SIZE \
00076   (sizeof (struct _pthread_descr_struct)                             \
00077    + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
00078 
00079 /* The following assumes that TP (R2 or R13) is points to the end of the
00080    TCB + 0x7000 (per the ABI).  This implies that TCB address is
00081    TP - 0x7000.  As we define TLS_DTV_AT_TP we can
00082    assume that the pthread_descr is allocated immediately ahead of the
00083    TCB.  This implies that the pthread_descr address is
00084    TP - (TLS_PRE_TCB_SIZE + 0x7000).  */
00085 #define TLS_TCB_OFFSET             0x7000
00086 
00087 /* The DTV is allocated at the TP; the TCB is placed elsewhere.  */
00088 /* This is not really true for powerpc64.  We are following alpha
00089    where the DTV pointer is first doubleword in the TCB.  */
00090 #  define TLS_DTV_AT_TP 1
00091 
00092 /* Install the dtv pointer.  The pointer passed is to the element with
00093    index -1 which contain the length.  */
00094 #  define INSTALL_DTV(TCBP, DTVP) \
00095   (((tcbhead_t *) (TCBP))[-1].dtv = (DTVP) + 1)
00096 
00097 /* Install new dtv for current thread.  */
00098 #  define INSTALL_NEW_DTV(DTV) (THREAD_DTV() = (DTV))
00099 
00100 /* Return dtv of given thread descriptor.  */
00101 #  define GET_DTV(TCBP)     (((tcbhead_t *) (TCBP))[-1].dtv)
00102 
00103 /* We still need this define so that tcb-offsets.sym can override it and
00104    use THREAD_SELF to generate MULTIPLE_THREADS_OFFSET.  */
00105 #  define __thread_register ((void *) __thread_self)
00106 
00107 /* Code to initially initialize the thread pointer.  This might need
00108    special attention since 'errno' is not yet available and if the
00109    operation can cause a failure 'errno' must not be touched.
00110 
00111    The global register variable is declared in pt-machine.h with the
00112    wrong type, so we need some extra casts to get the desired result.
00113    This avoids a lvalue cast that gcc-3.4 does not like.  */
00114 # define TLS_INIT_TP(TCBP, SECONDCALL) \
00115     (__thread_self = (struct _pthread_descr_struct *) \
00116        ((void *) (TCBP) + TLS_TCB_OFFSET), NULL)
00117 
00118 /* Return the address of the dtv for the current thread.  */
00119 #  define THREAD_DTV() \
00120      (((tcbhead_t *) ((void *) __thread_self - TLS_TCB_OFFSET))[-1].dtv)
00121 
00122 /* Return the thread descriptor for the current thread.  */
00123 #  undef THREAD_SELF
00124 #  define THREAD_SELF \
00125     ((pthread_descr) (__thread_register \
00126                     - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
00127 
00128 #  undef INIT_THREAD_SELF
00129 #  define INIT_THREAD_SELF(DESCR, NR) \
00130      (__thread_self = (struct _pthread_descr_struct *)((void *) (DESCR) \
00131                          + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE))
00132 
00133 /* Make sure we have the p_multiple_threads member in the thread structure.
00134    See below.  */
00135 #  define TLS_MULTIPLE_THREADS_IN_TCB 1
00136 
00137 /* Set the stack guard field in TCB head.  */
00138 # define THREAD_SET_STACK_GUARD(value) \
00139     (((tcbhead_t *) ((char *) __thread_register                             \
00140                    - TLS_TCB_OFFSET))[-1].stack_guard = (value))
00141 # define THREAD_COPY_STACK_GUARD(descr) \
00142     (((tcbhead_t *) ((char *) (descr)                                       \
00143                    + TLS_PRE_TCB_SIZE))[-1].stack_guard              \
00144      = ((tcbhead_t *) ((char *) __thread_register                           \
00145                      - TLS_TCB_OFFSET))[-1].stack_guard)
00146 
00147 /* Set the stack guard field in TCB head.  */
00148 # define THREAD_GET_POINTER_GUARD() \
00149     (((tcbhead_t *) ((char *) __thread_register                             \
00150                    - TLS_TCB_OFFSET))[-1].pointer_guard)
00151 # define THREAD_SET_POINTER_GUARD(value) \
00152     (THREAD_GET_POINTER_GUARD () = (value))
00153 # define THREAD_COPY_POINTER_GUARD(descr) \
00154     (((tcbhead_t *) ((char *) (descr)                                       \
00155                    + TLS_PRE_TCB_SIZE))[-1].pointer_guard                   \
00156      = THREAD_GET_POINTER_GUARD())
00157 
00158 /* Get the thread descriptor definition.  */
00159 #  include <linuxthreads/descr.h>
00160 
00161 /* l_tls_offset == 0 is perfectly valid on PPC, so we have to use some
00162    different value to mean unset l_tls_offset.  */
00163 #  define NO_TLS_OFFSET     -1
00164 
00165 # endif /* __ASSEMBLER__ */
00166 
00167 #elif !defined __ASSEMBLER__
00168 
00169 /* This overlaps the start of the pthread_descr.  System calls
00170    and such use this to find the multiple_threads flag and need
00171    to use the same offset relative to the thread register in both
00172    single-threaded and multi-threaded code.  */
00173 typedef struct
00174 {
00175   void *tcb;                /* Never used.  */
00176   dtv_t *dtv;               /* Never used.  */
00177   void *self;               /* Used only if multithreaded, and rarely.  */
00178   int multiple_threads;            /* Only this member is really used.  */
00179 } tcbhead_t;
00180 
00181 #define NONTLS_INIT_TP                                                \
00182   do {                                                         \
00183     static const tcbhead_t nontls_init_tp = { .multiple_threads = 0 };       \
00184     __thread_self = (__typeof (__thread_self)) &nontls_init_tp;              \
00185   } while (0)
00186 
00187 #endif /* HAVE_TLS_SUPPORT */
00188 
00189 #endif /* tls.h */