Back to index

glibc  2.9
tls.h
Go to the documentation of this file.
00001 /* Definition for thread-local data handling.  nptl/x86_64 version.
00002    Copyright (C) 2002,2003,2004,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 #ifndef __ASSEMBLER__
00024 # include <asm/prctl.h>     /* For ARCH_SET_FS.  */
00025 # include <stdbool.h>
00026 # include <stddef.h>
00027 # include <stdint.h>
00028 # include <stdlib.h>
00029 # include <sysdep.h>
00030 # include <kernel-features.h>
00031 
00032 
00033 /* Type for the dtv.  */
00034 typedef union dtv
00035 {
00036   size_t counter;
00037   struct
00038   {
00039     void *val;
00040     bool is_static;
00041   } pointer;
00042 } dtv_t;
00043 
00044 
00045 typedef struct
00046 {
00047   void *tcb;         /* Pointer to the TCB.  Not necessarily the
00048                         thread descriptor used by libpthread.  */
00049   dtv_t *dtv;
00050   void *self;        /* Pointer to the thread descriptor.  */
00051   int multiple_threads;
00052   int gscope_flag;
00053   uintptr_t sysinfo;
00054   uintptr_t stack_guard;
00055   uintptr_t pointer_guard;
00056   unsigned long int vgetcpu_cache[2];
00057 #ifndef __ASSUME_PRIVATE_FUTEX
00058   int private_futex;
00059 #endif
00060 } tcbhead_t;
00061 
00062 #else /* __ASSEMBLER__ */
00063 # include <tcb-offsets.h>
00064 #endif
00065 
00066 
00067 /* We require TLS support in the tools.  */
00068 #ifndef HAVE_TLS_SUPPORT
00069 # error "TLS support is required."
00070 #endif
00071 
00072 /* Alignment requirement for the stack.  */
00073 #define STACK_ALIGN  16
00074 
00075 
00076 #ifndef __ASSEMBLER__
00077 /* Get system call information.  */
00078 # include <sysdep.h>
00079 
00080 
00081 /* Get the thread descriptor definition.  */
00082 # include <nptl/descr.h>
00083 
00084 #ifndef LOCK_PREFIX
00085 # ifdef UP
00086 #  define LOCK_PREFIX       /* nothing */
00087 # else
00088 #  define LOCK_PREFIX       "lock;"
00089 # endif
00090 #endif
00091 
00092 /* This is the size of the initial TCB.  Can't be just sizeof (tcbhead_t),
00093    because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
00094    struct pthread even when not linked with -lpthread.  */
00095 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
00096 
00097 /* Alignment requirements for the initial TCB.  */
00098 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
00099 
00100 /* This is the size of the TCB.  */
00101 # define TLS_TCB_SIZE sizeof (struct pthread)
00102 
00103 /* Alignment requirements for the TCB.  */
00104 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
00105 
00106 /* The TCB can have any size and the memory following the address the
00107    thread pointer points to is unspecified.  Allocate the TCB there.  */
00108 # define TLS_TCB_AT_TP      1
00109 
00110 
00111 /* Install the dtv pointer.  The pointer passed is to the element with
00112    index -1 which contain the length.  */
00113 # define INSTALL_DTV(descr, dtvp) \
00114   ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
00115 
00116 /* Install new dtv for current thread.  */
00117 # define INSTALL_NEW_DTV(dtvp) \
00118   ({ struct pthread *__pd;                                           \
00119      THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
00120 
00121 /* Return dtv of given thread descriptor.  */
00122 # define GET_DTV(descr) \
00123   (((tcbhead_t *) (descr))->dtv)
00124 
00125 
00126 /* Macros to load from and store into segment registers.  */
00127 # define TLS_GET_FS() \
00128   ({ int __seg; __asm ("movl %%fs, %0" : "=q" (__seg)); __seg; })
00129 # define TLS_SET_FS(val) \
00130   __asm ("movl %0, %%fs" :: "q" (val))
00131 
00132 
00133 /* Code to initially initialize the thread pointer.  This might need
00134    special attention since 'errno' is not yet available and if the
00135    operation can cause a failure 'errno' must not be touched.
00136 
00137    We have to make the syscall for both uses of the macro since the
00138    address might be (and probably is) different.  */
00139 # define TLS_INIT_TP(thrdescr, secondcall) \
00140   ({ void *_thrdescr = (thrdescr);                                   \
00141      tcbhead_t *_head = _thrdescr;                                   \
00142      int _result;                                                    \
00143                                                                      \
00144      _head->tcb = _thrdescr;                                                \
00145      /* For now the thread descriptor is at the same address.  */           \
00146      _head->self = _thrdescr;                                               \
00147                                                                      \
00148      /* It is a simple syscall to set the %fs value for the thread.  */            \
00149      asm volatile ("syscall"                                                \
00150                  : "=a" (_result)                                    \
00151                  : "0" ((unsigned long int) __NR_arch_prctl),               \
00152                    "D" ((unsigned long int) ARCH_SET_FS),                   \
00153                    "S" (_thrdescr)                                   \
00154                  : "memory", "cc", "r11", "cx");                     \
00155                                                                      \
00156     _result ? "cannot set %fs base address for thread-local storage" : 0;     \
00157   })
00158 
00159 
00160 /* Return the address of the dtv for the current thread.  */
00161 # define THREAD_DTV() \
00162   ({ struct pthread *__pd;                                           \
00163      THREAD_GETMEM (__pd, header.dtv); })
00164 
00165 
00166 /* Return the thread descriptor for the current thread.
00167 
00168    The contained asm must *not* be marked volatile since otherwise
00169    assignments like
00170         pthread_descr self = thread_self();
00171    do not get optimized away.  */
00172 # define THREAD_SELF \
00173   ({ struct pthread *__self;                                                \
00174      asm ("movq %%fs:%c1,%q0" : "=r" (__self)                               \
00175          : "i" (offsetof (struct pthread, header.self)));                   \
00176      __self;})
00177 
00178 /* Magic for libthread_db to know how to do THREAD_SELF.  */
00179 # define DB_THREAD_SELF_INCLUDE  <sys/reg.h> /* For the FS constant.  */
00180 # define DB_THREAD_SELF CONST_THREAD_AREA (64, FS)
00181 
00182 /* Read member of the thread descriptor directly.  */
00183 # define THREAD_GETMEM(descr, member) \
00184   ({ __typeof (descr->member) __value;                                      \
00185      if (sizeof (__value) == 1)                                             \
00186        asm volatile ("movb %%fs:%P2,%b0"                             \
00187                    : "=q" (__value)                                         \
00188                    : "0" (0), "i" (offsetof (struct pthread, member)));     \
00189      else if (sizeof (__value) == 4)                                        \
00190        asm volatile ("movl %%fs:%P1,%0"                                     \
00191                    : "=r" (__value)                                         \
00192                    : "i" (offsetof (struct pthread, member)));       \
00193      else                                                            \
00194        {                                                             \
00195         if (sizeof (__value) != 8)                                   \
00196           /* There should not be any value with a size other than 1,        \
00197              4 or 8.  */                                             \
00198           abort ();                                                  \
00199                                                                      \
00200         asm volatile ("movq %%fs:%P1,%q0"                            \
00201                      : "=r" (__value)                                       \
00202                      : "i" (offsetof (struct pthread, member)));            \
00203        }                                                             \
00204      __value; })
00205 
00206 
00207 /* Same as THREAD_GETMEM, but the member offset can be non-constant.  */
00208 # define THREAD_GETMEM_NC(descr, member, idx) \
00209   ({ __typeof (descr->member[0]) __value;                            \
00210      if (sizeof (__value) == 1)                                             \
00211        asm volatile ("movb %%fs:%P2(%q3),%b0"                               \
00212                    : "=q" (__value)                                         \
00213                    : "0" (0), "i" (offsetof (struct pthread, member[0])),   \
00214                      "r" (idx));                                     \
00215      else if (sizeof (__value) == 4)                                        \
00216        asm volatile ("movl %%fs:%P1(,%q2,4),%0"                             \
00217                    : "=r" (__value)                                         \
00218                    : "i" (offsetof (struct pthread, member[0])), "r" (idx));\
00219      else                                                            \
00220        {                                                             \
00221         if (sizeof (__value) != 8)                                   \
00222           /* There should not be any value with a size other than 1,        \
00223              4 or 8.  */                                             \
00224           abort ();                                                  \
00225                                                                      \
00226         asm volatile ("movq %%fs:%P1(,%q2,8),%q0"                           \
00227                      : "=r" (__value)                                       \
00228                      : "i" (offsetof (struct pthread, member[0])),          \
00229                       "r" (idx));                                    \
00230        }                                                             \
00231      __value; })
00232 
00233 
00234 /* Loading addresses of objects on x86-64 needs to be treated special
00235    when generating PIC code.  */
00236 #ifdef __pic__
00237 # define IMM_MODE "nr"
00238 #else
00239 # define IMM_MODE "ir"
00240 #endif
00241 
00242 
00243 /* Same as THREAD_SETMEM, but the member offset can be non-constant.  */
00244 # define THREAD_SETMEM(descr, member, value) \
00245   ({ if (sizeof (descr->member) == 1)                                       \
00246        asm volatile ("movb %b0,%%fs:%P1" :                                  \
00247                    : "iq" (value),                                   \
00248                      "i" (offsetof (struct pthread, member)));       \
00249      else if (sizeof (descr->member) == 4)                                  \
00250        asm volatile ("movl %0,%%fs:%P1" :                            \
00251                    : IMM_MODE (value),                               \
00252                      "i" (offsetof (struct pthread, member)));       \
00253      else                                                            \
00254        {                                                             \
00255         if (sizeof (descr->member) != 8)                             \
00256           /* There should not be any value with a size other than 1,        \
00257              4 or 8.  */                                             \
00258           abort ();                                                  \
00259                                                                      \
00260         asm volatile ("movq %q0,%%fs:%P1" :                                 \
00261                      : IMM_MODE ((unsigned long int) value),                \
00262                       "i" (offsetof (struct pthread, member)));             \
00263        }})
00264 
00265 
00266 /* Set member of the thread descriptor directly.  */
00267 # define THREAD_SETMEM_NC(descr, member, idx, value) \
00268   ({ if (sizeof (descr->member[0]) == 1)                             \
00269        asm volatile ("movb %b0,%%fs:%P1(%q2)" :                             \
00270                    : "iq" (value),                                   \
00271                      "i" (offsetof (struct pthread, member[0])),            \
00272                      "r" (idx));                                     \
00273      else if (sizeof (descr->member[0]) == 4)                               \
00274        asm volatile ("movl %0,%%fs:%P1(,%q2,4)" :                           \
00275                    : IMM_MODE (value),                               \
00276                      "i" (offsetof (struct pthread, member[0])),            \
00277                      "r" (idx));                                     \
00278      else                                                            \
00279        {                                                             \
00280         if (sizeof (descr->member[0]) != 8)                                 \
00281           /* There should not be any value with a size other than 1,        \
00282              4 or 8.  */                                             \
00283           abort ();                                                  \
00284                                                                      \
00285         asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" :                         \
00286                      : IMM_MODE ((unsigned long int) value),                \
00287                       "i" (offsetof (struct pthread, member[0])),           \
00288                       "r" (idx));                                    \
00289        }})
00290 
00291 
00292 /* Atomic compare and exchange on TLS, returning old value.  */
00293 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
00294   ({ __typeof (descr->member) __ret;                                        \
00295      __typeof (oldval) __old = (oldval);                             \
00296      if (sizeof (descr->member) == 4)                                       \
00297        asm volatile (LOCK_PREFIX "cmpxchgl %2, %%fs:%P3"                    \
00298                    : "=a" (__ret)                                    \
00299                    : "0" (__old), "r" (newval),                      \
00300                      "i" (offsetof (struct pthread, member)));       \
00301      else                                                            \
00302        /* Not necessary for other sizes in the moment.  */                  \
00303        abort ();                                                     \
00304      __ret; })
00305 
00306 
00307 /* Atomic set bit.  */
00308 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
00309   (void) ({ if (sizeof ((descr)->member) == 4)                              \
00310              asm volatile (LOCK_PREFIX "orl %1, %%fs:%P0"                   \
00311                          :: "i" (offsetof (struct pthread, member)),        \
00312                             "ir" (1 << (bit)));                      \
00313            else                                                      \
00314              /* Not necessary for other sizes in the moment.  */            \
00315              abort (); })
00316 
00317 
00318 #define CALL_THREAD_FCT(descr) \
00319   ({ void *__res;                                                    \
00320      asm volatile ("movq %%fs:%P2, %%rdi\n\t"                               \
00321                  "callq *%%fs:%P1"                                   \
00322                  : "=a" (__res)                                      \
00323                  : "i" (offsetof (struct pthread, start_routine)),          \
00324                    "i" (offsetof (struct pthread, arg))              \
00325                  : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11",        \
00326                    "memory", "cc");                                         \
00327      __res; })
00328 
00329 
00330 /* Set the stack guard field in TCB head.  */
00331 # define THREAD_SET_STACK_GUARD(value) \
00332     THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
00333 # define THREAD_COPY_STACK_GUARD(descr) \
00334     ((descr)->header.stack_guard                                     \
00335      = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
00336 
00337 
00338 /* Set the pointer guard field in the TCB head.  */
00339 #define THREAD_SET_POINTER_GUARD(value) \
00340   THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
00341 #define THREAD_COPY_POINTER_GUARD(descr) \
00342   ((descr)->header.pointer_guard                                     \
00343    = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
00344 
00345 
00346 /* Get and set the global scope generation counter in the TCB head.  */
00347 #define THREAD_GSCOPE_FLAG_UNUSED 0
00348 #define THREAD_GSCOPE_FLAG_USED   1
00349 #define THREAD_GSCOPE_FLAG_WAIT   2
00350 #define THREAD_GSCOPE_RESET_FLAG() \
00351   do                                                                 \
00352     { int __res;                                                     \
00353       asm volatile ("xchgl %0, %%fs:%P1"                             \
00354                   : "=r" (__res)                                     \
00355                   : "i" (offsetof (struct pthread, header.gscope_flag)),    \
00356                     "0" (THREAD_GSCOPE_FLAG_UNUSED));                       \
00357       if (__res == THREAD_GSCOPE_FLAG_WAIT)                                 \
00358        lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);    \
00359     }                                                                \
00360   while (0)
00361 #define THREAD_GSCOPE_SET_FLAG() \
00362   THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
00363 #define THREAD_GSCOPE_WAIT() \
00364   GL(dl_wait_lookup_done) ()
00365 
00366 #endif /* __ASSEMBLER__ */
00367 
00368 #endif /* tls.h */