Back to index

glibc  2.9
tls.h
Go to the documentation of this file.
00001 /* Definition for thread-local data handling.  nptl/i386 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 #include <dl-sysdep.h>
00024 #ifndef __ASSEMBLER__
00025 # include <stdbool.h>
00026 # include <stddef.h>
00027 # include <stdint.h>
00028 # include <stdlib.h>
00029 # include <list.h>
00030 # include <sysdep.h>
00031 # include <kernel-features.h>
00032 
00033 
00034 /* Type for the dtv.  */
00035 typedef union dtv
00036 {
00037   size_t counter;
00038   struct
00039   {
00040     void *val;
00041     bool is_static;
00042   } pointer;
00043 } dtv_t;
00044 
00045 
00046 typedef struct
00047 {
00048   void *tcb;         /* Pointer to the TCB.  Not necessarily the
00049                         thread descriptor used by libpthread.  */
00050   dtv_t *dtv;
00051   void *self;        /* Pointer to the thread descriptor.  */
00052   int multiple_threads;
00053   uintptr_t sysinfo;
00054   uintptr_t stack_guard;
00055   uintptr_t pointer_guard;
00056   int gscope_flag;
00057 #ifndef __ASSUME_PRIVATE_FUTEX
00058   int private_futex;
00059 #endif
00060 } tcbhead_t;
00061 
00062 # define TLS_MULTIPLE_THREADS_IN_TCB 1
00063 
00064 #else /* __ASSEMBLER__ */
00065 # include <tcb-offsets.h>
00066 #endif
00067 
00068 
00069 /* We require TLS support in the tools.  */
00070 #ifndef HAVE_TLS_SUPPORT
00071 # error "TLS support is required."
00072 #endif
00073 
00074 /* Alignment requirement for the stack.  For IA-32 this is governed by
00075    the SSE memory functions.  */
00076 #define STACK_ALIGN  16
00077 
00078 #ifndef __ASSEMBLER__
00079 /* Get system call information.  */
00080 # include <sysdep.h>
00081 
00082 /* The old way: using LDT.  */
00083 
00084 /* Structure passed to `modify_ldt', 'set_thread_area', and 'clone' calls.  */
00085 struct user_desc
00086 {
00087   unsigned int entry_number;
00088   unsigned long int base_addr;
00089   unsigned int limit;
00090   unsigned int seg_32bit:1;
00091   unsigned int contents:2;
00092   unsigned int read_exec_only:1;
00093   unsigned int limit_in_pages:1;
00094   unsigned int seg_not_present:1;
00095   unsigned int useable:1;
00096   unsigned int empty:25;
00097 };
00098 
00099 /* Initializing bit fields is slow.  We speed it up by using a union.  */
00100 union user_desc_init
00101 {
00102   struct user_desc desc;
00103   unsigned int vals[4];
00104 };
00105 
00106 
00107 /* Get the thread descriptor definition.  */
00108 # include <nptl/descr.h>
00109 
00110 /* This is the size of the initial TCB.  Can't be just sizeof (tcbhead_t),
00111    because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
00112    struct pthread even when not linked with -lpthread.  */
00113 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
00114 
00115 /* Alignment requirements for the initial TCB.  */
00116 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
00117 
00118 /* This is the size of the TCB.  */
00119 # define TLS_TCB_SIZE sizeof (struct pthread)
00120 
00121 /* Alignment requirements for the TCB.  */
00122 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
00123 
00124 /* The TCB can have any size and the memory following the address the
00125    thread pointer points to is unspecified.  Allocate the TCB there.  */
00126 # define TLS_TCB_AT_TP      1
00127 
00128 
00129 /* Install the dtv pointer.  The pointer passed is to the element with
00130    index -1 which contain the length.  */
00131 # define INSTALL_DTV(descr, dtvp) \
00132   ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
00133 
00134 /* Install new dtv for current thread.  */
00135 # define INSTALL_NEW_DTV(dtvp) \
00136   ({ struct pthread *__pd;                                           \
00137      THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
00138 
00139 /* Return dtv of given thread descriptor.  */
00140 # define GET_DTV(descr) \
00141   (((tcbhead_t *) (descr))->dtv)
00142 
00143 #define THREAD_SELF_SYSINFO THREAD_GETMEM (THREAD_SELF, header.sysinfo)
00144 #define THREAD_SYSINFO(pd)  ((pd)->header.sysinfo)
00145 
00146 /* Macros to load from and store into segment registers.  */
00147 # ifndef TLS_GET_GS
00148 #  define TLS_GET_GS() \
00149   ({ int __seg; __asm ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
00150 # endif
00151 # ifndef TLS_SET_GS
00152 #  define TLS_SET_GS(val) \
00153   __asm ("movw %w0, %%gs" :: "q" (val))
00154 # endif
00155 
00156 
00157 # ifndef __NR_set_thread_area
00158 #  define __NR_set_thread_area 243
00159 # endif
00160 # ifndef TLS_FLAG_WRITABLE
00161 #  define TLS_FLAG_WRITABLE        0x00000001
00162 # endif
00163 
00164 // XXX Enable for the real world.
00165 #if 0
00166 # ifndef __ASSUME_SET_THREAD_AREA
00167 #  error "we need set_thread_area"
00168 # endif
00169 #endif
00170 
00171 # ifdef __PIC__
00172 #  define TLS_EBX_ARG "r"
00173 #  define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
00174 # else
00175 #  define TLS_EBX_ARG "b"
00176 #  define TLS_LOAD_EBX
00177 # endif
00178 
00179 #if defined NEED_DL_SYSINFO
00180 # define INIT_SYSINFO \
00181   _head->sysinfo = GLRO(dl_sysinfo)
00182 #else
00183 # define INIT_SYSINFO
00184 #endif
00185 
00186 #ifndef LOCK_PREFIX
00187 # ifdef UP
00188 #  define LOCK_PREFIX  /* nothing */
00189 # else
00190 #  define LOCK_PREFIX "lock;"
00191 # endif
00192 #endif
00193 
00194 /* Code to initially initialize the thread pointer.  This might need
00195    special attention since 'errno' is not yet available and if the
00196    operation can cause a failure 'errno' must not be touched.  */
00197 # define TLS_INIT_TP(thrdescr, secondcall) \
00198   ({ void *_thrdescr = (thrdescr);                                   \
00199      tcbhead_t *_head = _thrdescr;                                   \
00200      union user_desc_init _segdescr;                                        \
00201      int _result;                                                    \
00202                                                                      \
00203      _head->tcb = _thrdescr;                                                \
00204      /* For now the thread descriptor is at the same address.  */           \
00205      _head->self = _thrdescr;                                               \
00206      /* New syscall handling support.  */                            \
00207      INIT_SYSINFO;                                                   \
00208                                                                      \
00209      /* The 'entry_number' field.  Let the kernel pick a value.  */         \
00210      if (secondcall)                                                 \
00211        _segdescr.vals[0] = TLS_GET_GS () >> 3;                              \
00212      else                                                            \
00213        _segdescr.vals[0] = -1;                                              \
00214      /* The 'base_addr' field.  Pointer to the TCB.  */                     \
00215      _segdescr.vals[1] = (unsigned long int) _thrdescr;                     \
00216      /* The 'limit' field.  We use 4GB which is 0xfffff pages.  */          \
00217      _segdescr.vals[2] = 0xfffff;                                    \
00218      /* Collapsed value of the bitfield:                             \
00219          .seg_32bit = 1                                              \
00220          .contents = 0                                                      \
00221          .read_exec_only = 0                                                \
00222          .limit_in_pages = 1                                                \
00223          .seg_not_present = 0                                               \
00224          .useable = 1 */                                             \
00225      _segdescr.vals[3] = 0x51;                                              \
00226                                                                      \
00227      /* Install the TLS.  */                                                \
00228      asm volatile (TLS_LOAD_EBX                                             \
00229                  "int $0x80\n\t"                                     \
00230                  TLS_LOAD_EBX                                               \
00231                  : "=a" (_result), "=m" (_segdescr.desc.entry_number)       \
00232                  : "0" (__NR_set_thread_area),                       \
00233                    TLS_EBX_ARG (&_segdescr.desc), "m" (_segdescr.desc));    \
00234                                                                      \
00235      if (_result == 0)                                                      \
00236        /* We know the index in the GDT, now load the segment register.             \
00237          The use of the GDT is described by the value 3 in the lower        \
00238          three bits of the segment descriptor value.                        \
00239                                                                      \
00240          Note that we have to do this even if the numeric value of          \
00241          the descriptor does not change.  Loading the segment register             \
00242          causes the segment information from the GDT to be loaded           \
00243          which is necessary since we have changed it.   */                  \
00244        TLS_SET_GS (_segdescr.desc.entry_number * 8 + 3);                    \
00245                                                                      \
00246      _result == 0 ? NULL                                             \
00247      : "set_thread_area failed when setting up thread-local storage\n"; })
00248 
00249 
00250 /* Return the address of the dtv for the current thread.  */
00251 # define THREAD_DTV() \
00252   ({ struct pthread *__pd;                                           \
00253      THREAD_GETMEM (__pd, header.dtv); })
00254 
00255 
00256 /* Return the thread descriptor for the current thread.
00257 
00258    The contained asm must *not* be marked volatile since otherwise
00259    assignments like
00260         pthread_descr self = thread_self();
00261    do not get optimized away.  */
00262 # define THREAD_SELF \
00263   ({ struct pthread *__self;                                                \
00264      asm ("movl %%gs:%c1,%0" : "=r" (__self)                                \
00265          : "i" (offsetof (struct pthread, header.self)));                   \
00266      __self;})
00267 
00268 /* Magic for libthread_db to know how to do THREAD_SELF.  */
00269 # define DB_THREAD_SELF \
00270   REGISTER_THREAD_AREA (32, offsetof (struct user_regs_struct, xgs), 3) \
00271   REGISTER_THREAD_AREA (64, 26 * 8, 3) /* x86-64's user_regs_struct->gs */
00272 
00273 
00274 /* Read member of the thread descriptor directly.  */
00275 # define THREAD_GETMEM(descr, member) \
00276   ({ __typeof (descr->member) __value;                                      \
00277      if (sizeof (__value) == 1)                                             \
00278        asm volatile ("movb %%gs:%P2,%b0"                             \
00279                    : "=q" (__value)                                         \
00280                    : "0" (0), "i" (offsetof (struct pthread, member)));     \
00281      else if (sizeof (__value) == 4)                                        \
00282        asm volatile ("movl %%gs:%P1,%0"                                     \
00283                    : "=r" (__value)                                         \
00284                    : "i" (offsetof (struct pthread, member)));       \
00285      else                                                            \
00286        {                                                             \
00287         if (sizeof (__value) != 8)                                   \
00288           /* There should not be any value with a size other than 1,        \
00289              4 or 8.  */                                             \
00290           abort ();                                                  \
00291                                                                      \
00292         asm volatile ("movl %%gs:%P1,%%eax\n\t"                      \
00293                      "movl %%gs:%P2,%%edx"                                  \
00294                      : "=A" (__value)                                       \
00295                      : "i" (offsetof (struct pthread, member)),             \
00296                       "i" (offsetof (struct pthread, member) + 4));         \
00297        }                                                             \
00298      __value; })
00299 
00300 
00301 /* Same as THREAD_GETMEM, but the member offset can be non-constant.  */
00302 # define THREAD_GETMEM_NC(descr, member, idx) \
00303   ({ __typeof (descr->member[0]) __value;                            \
00304      if (sizeof (__value) == 1)                                             \
00305        asm volatile ("movb %%gs:%P2(%3),%b0"                                \
00306                    : "=q" (__value)                                         \
00307                    : "0" (0), "i" (offsetof (struct pthread, member[0])),   \
00308                    "r" (idx));                                       \
00309      else if (sizeof (__value) == 4)                                        \
00310        asm volatile ("movl %%gs:%P1(,%2,4),%0"                              \
00311                    : "=r" (__value)                                         \
00312                    : "i" (offsetof (struct pthread, member[0])),            \
00313                      "r" (idx));                                     \
00314      else                                                            \
00315        {                                                             \
00316         if (sizeof (__value) != 8)                                   \
00317           /* There should not be any value with a size other than 1,        \
00318              4 or 8.  */                                             \
00319           abort ();                                                  \
00320                                                                      \
00321         asm volatile  ("movl %%gs:%P1(,%2,8),%%eax\n\t"              \
00322                      "movl %%gs:4+%P1(,%2,8),%%edx"                         \
00323                      : "=&A" (__value)                               \
00324                      : "i" (offsetof (struct pthread, member[0])),          \
00325                        "r" (idx));                                   \
00326        }                                                             \
00327      __value; })
00328 
00329 
00330 /* Same as THREAD_SETMEM, but the member offset can be non-constant.  */
00331 # define THREAD_SETMEM(descr, member, value) \
00332   ({ if (sizeof (descr->member) == 1)                                       \
00333        asm volatile ("movb %b0,%%gs:%P1" :                                  \
00334                    : "iq" (value),                                   \
00335                      "i" (offsetof (struct pthread, member)));       \
00336      else if (sizeof (descr->member) == 4)                                  \
00337        asm volatile ("movl %0,%%gs:%P1" :                            \
00338                    : "ir" (value),                                   \
00339                      "i" (offsetof (struct pthread, member)));       \
00340      else                                                            \
00341        {                                                             \
00342         if (sizeof (descr->member) != 8)                             \
00343           /* There should not be any value with a size other than 1,        \
00344              4 or 8.  */                                             \
00345           abort ();                                                  \
00346                                                                      \
00347         asm volatile ("movl %%eax,%%gs:%P1\n\t"                      \
00348                      "movl %%edx,%%gs:%P2" :                                \
00349                      : "A" (value),                                         \
00350                       "i" (offsetof (struct pthread, member)),       \
00351                       "i" (offsetof (struct pthread, member) + 4));         \
00352        }})
00353 
00354 
00355 /* Set member of the thread descriptor directly.  */
00356 # define THREAD_SETMEM_NC(descr, member, idx, value) \
00357   ({ if (sizeof (descr->member[0]) == 1)                             \
00358        asm volatile ("movb %b0,%%gs:%P1(%2)" :                              \
00359                    : "iq" (value),                                   \
00360                      "i" (offsetof (struct pthread, member)),               \
00361                      "r" (idx));                                     \
00362      else if (sizeof (descr->member[0]) == 4)                               \
00363        asm volatile ("movl %0,%%gs:%P1(,%2,4)" :                     \
00364                    : "ir" (value),                                   \
00365                      "i" (offsetof (struct pthread, member)),               \
00366                      "r" (idx));                                     \
00367      else                                                            \
00368        {                                                             \
00369         if (sizeof (descr->member[0]) != 8)                                 \
00370           /* There should not be any value with a size other than 1,        \
00371              4 or 8.  */                                             \
00372           abort ();                                                  \
00373                                                                      \
00374         asm volatile ("movl %%eax,%%gs:%P1(,%2,8)\n\t"                      \
00375                      "movl %%edx,%%gs:4+%P1(,%2,8)" :                       \
00376                      : "A" (value),                                         \
00377                       "i" (offsetof (struct pthread, member)),       \
00378                       "r" (idx));                                    \
00379        }})
00380 
00381 
00382 /* Atomic compare and exchange on TLS, returning old value.  */
00383 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
00384   ({ __typeof (descr->member) __ret;                                        \
00385      __typeof (oldval) __old = (oldval);                             \
00386      if (sizeof (descr->member) == 4)                                       \
00387        asm volatile (LOCK_PREFIX "cmpxchgl %2, %%gs:%P3"                    \
00388                    : "=a" (__ret)                                    \
00389                    : "0" (__old), "r" (newval),                      \
00390                      "i" (offsetof (struct pthread, member)));       \
00391      else                                                            \
00392        /* Not necessary for other sizes in the moment.  */                  \
00393        abort ();                                                     \
00394      __ret; })
00395 
00396 
00397 /* Atomic set bit.  */
00398 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
00399   (void) ({ if (sizeof ((descr)->member) == 4)                              \
00400              asm volatile (LOCK_PREFIX "orl %1, %%gs:%P0"                   \
00401                          :: "i" (offsetof (struct pthread, member)),        \
00402                             "ir" (1 << (bit)));                      \
00403            else                                                      \
00404              /* Not necessary for other sizes in the moment.  */            \
00405              abort (); })
00406 
00407 
00408 /* Call the user-provided thread function.  */
00409 #define CALL_THREAD_FCT(descr) \
00410   ({ void *__res;                                                    \
00411      int __ignore1, __ignore2;                                              \
00412      asm volatile ("pushl %%eax\n\t"                                        \
00413                  "pushl %%eax\n\t"                                   \
00414                  "pushl %%eax\n\t"                                   \
00415                  "pushl %%gs:%P4\n\t"                                       \
00416                  "call *%%gs:%P3\n\t"                                       \
00417                  "addl $16, %%esp"                                   \
00418                  : "=a" (__res), "=c" (__ignore1), "=d" (__ignore2)         \
00419                  : "i" (offsetof (struct pthread, start_routine)),          \
00420                    "i" (offsetof (struct pthread, arg)));                   \
00421      __res; })
00422 
00423 
00424 /* Set the stack guard field in TCB head.  */
00425 #define THREAD_SET_STACK_GUARD(value) \
00426   THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
00427 #define THREAD_COPY_STACK_GUARD(descr) \
00428   ((descr)->header.stack_guard                                              \
00429    = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
00430 
00431 
00432 /* Set the pointer guard field in the TCB head.  */
00433 #define THREAD_SET_POINTER_GUARD(value) \
00434   THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
00435 #define THREAD_COPY_POINTER_GUARD(descr) \
00436   ((descr)->header.pointer_guard                                     \
00437    = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
00438 
00439 
00440 /* Get and set the global scope generation counter in the TCB head.  */
00441 #define THREAD_GSCOPE_FLAG_UNUSED 0
00442 #define THREAD_GSCOPE_FLAG_USED   1
00443 #define THREAD_GSCOPE_FLAG_WAIT   2
00444 #define THREAD_GSCOPE_RESET_FLAG() \
00445   do                                                                 \
00446     { int __res;                                                     \
00447       asm volatile ("xchgl %0, %%gs:%P1"                             \
00448                   : "=r" (__res)                                     \
00449                   : "i" (offsetof (struct pthread, header.gscope_flag)),    \
00450                     "0" (THREAD_GSCOPE_FLAG_UNUSED));                       \
00451       if (__res == THREAD_GSCOPE_FLAG_WAIT)                                 \
00452        lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);    \
00453     }                                                                \
00454   while (0)
00455 #define THREAD_GSCOPE_SET_FLAG() \
00456   THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
00457 #define THREAD_GSCOPE_WAIT() \
00458   GL(dl_wait_lookup_done) ()
00459 
00460 #endif /* __ASSEMBLER__ */
00461 
00462 #endif /* tls.h */