[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/nptl/sysdeps/x86_64/tls.h

Version: -  FREEBSD  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  cheribsd  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1  -  FREEBSD-LIBC  -  FREEBSD8-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  3  -  10 

    1 /* Definition for thread-local data handling.  nptl/x86_64 version.
    2    Copyright (C) 2002,2003,2004,2005,2006,2007 Free Software Foundation, Inc.
    3    This file is part of the GNU C Library.
    4 
    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.
    9 
   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.
   14 
   15    You should have received a copy of the GNU Lesser General Public
   16    License along with the GNU C Library; if not, write to the Free
   17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   18    02111-1307 USA.  */
   19 
   20 #ifndef _TLS_H
   21 #define _TLS_H  1
   22 
   23 #ifndef __ASSEMBLER__
   24 # include <asm/prctl.h> /* For ARCH_SET_FS.  */
   25 # include <stdbool.h>
   26 # include <stddef.h>
   27 # include <stdint.h>
   28 # include <stdlib.h>
   29 # include <sysdep.h>
   30 # include <kernel-features.h>
   31 
   32 
   33 /* Type for the dtv.  */
   34 typedef union dtv
   35 {
   36   size_t counter;
   37   struct
   38   {
   39     void *val;
   40     bool is_static;
   41   } pointer;
   42 } dtv_t;
   43 
   44 
   45 typedef struct
   46 {
   47   void *tcb;            /* Pointer to the TCB.  Not necessarily the
   48                            thread descriptor used by libpthread.  */
   49   dtv_t *dtv;
   50   void *self;           /* Pointer to the thread descriptor.  */
   51   int multiple_threads;
   52   int gscope_flag;
   53   uintptr_t sysinfo;
   54   uintptr_t stack_guard;
   55   uintptr_t pointer_guard;
   56   unsigned long int vgetcpu_cache[2];
   57 #ifndef __ASSUME_PRIVATE_FUTEX
   58   int private_futex;
   59 #endif
   60 } tcbhead_t;
   61 
   62 #else /* __ASSEMBLER__ */
   63 # include <tcb-offsets.h>
   64 #endif
   65 
   66 
   67 /* We require TLS support in the tools.  */
   68 #ifndef HAVE_TLS_SUPPORT
   69 # error "TLS support is required."
   70 #endif
   71 
   72 /* Alignment requirement for the stack.  */
   73 #define STACK_ALIGN     16
   74 
   75 
   76 #ifndef __ASSEMBLER__
   77 /* Get system call information.  */
   78 # include <sysdep.h>
   79 
   80 
   81 /* Get the thread descriptor definition.  */
   82 # include <nptl/descr.h>
   83 
   84 #ifndef LOCK_PREFIX
   85 # ifdef UP
   86 #  define LOCK_PREFIX   /* nothing */
   87 # else
   88 #  define LOCK_PREFIX   "lock;"
   89 # endif
   90 #endif
   91 
   92 /* This is the size of the initial TCB.  Can't be just sizeof (tcbhead_t),
   93    because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
   94    struct pthread even when not linked with -lpthread.  */
   95 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
   96 
   97 /* Alignment requirements for the initial TCB.  */
   98 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
   99 
  100 /* This is the size of the TCB.  */
  101 # define TLS_TCB_SIZE sizeof (struct pthread)
  102 
  103 /* Alignment requirements for the TCB.  */
  104 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
  105 
  106 /* The TCB can have any size and the memory following the address the
  107    thread pointer points to is unspecified.  Allocate the TCB there.  */
  108 # define TLS_TCB_AT_TP  1
  109 
  110 
  111 /* Install the dtv pointer.  The pointer passed is to the element with
  112    index -1 which contain the length.  */
  113 # define INSTALL_DTV(descr, dtvp) \
  114   ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
  115 
  116 /* Install new dtv for current thread.  */
  117 # define INSTALL_NEW_DTV(dtvp) \
  118   ({ struct pthread *__pd;                                                    \
  119      THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
  120 
  121 /* Return dtv of given thread descriptor.  */
  122 # define GET_DTV(descr) \
  123   (((tcbhead_t *) (descr))->dtv)
  124 
  125 
  126 /* Macros to load from and store into segment registers.  */
  127 # define TLS_GET_FS() \
  128   ({ int __seg; __asm ("movl %%fs, %0" : "=q" (__seg)); __seg; })
  129 # define TLS_SET_FS(val) \
  130   __asm ("movl %0, %%fs" :: "q" (val))
  131 
  132 
  133 /* Code to initially initialize the thread pointer.  This might need
  134    special attention since 'errno' is not yet available and if the
  135    operation can cause a failure 'errno' must not be touched.
  136 
  137    We have to make the syscall for both uses of the macro since the
  138    address might be (and probably is) different.  */
  139 # define TLS_INIT_TP(thrdescr, secondcall) \
  140   ({ void *_thrdescr = (thrdescr);                                            \
  141      tcbhead_t *_head = _thrdescr;                                            \
  142      int _result;                                                             \
  143                                                                               \
  144      _head->tcb = _thrdescr;                                                  \
  145      /* For now the thread descriptor is at the same address.  */             \
  146      _head->self = _thrdescr;                                                 \
  147                                                                               \
  148      /* It is a simple syscall to set the %fs value for the thread.  */       \
  149      asm volatile ("syscall"                                                  \
  150                    : "=a" (_result)                                           \
  151                    : "" ((unsigned long int) __NR_arch_prctl),               \
  152                      "D" ((unsigned long int) ARCH_SET_FS),                   \
  153                      "S" (_thrdescr)                                          \
  154                    : "memory", "cc", "r11", "cx");                            \
  155                                                                               \
  156     _result ? "cannot set %fs base address for thread-local storage" : 0;     \
  157   })
  158 
  159 
  160 /* Return the address of the dtv for the current thread.  */
  161 # define THREAD_DTV() \
  162   ({ struct pthread *__pd;                                                    \
  163      THREAD_GETMEM (__pd, header.dtv); })
  164 
  165 
  166 /* Return the thread descriptor for the current thread.
  167 
  168    The contained asm must *not* be marked volatile since otherwise
  169    assignments like
  170         pthread_descr self = thread_self();
  171    do not get optimized away.  */
  172 # define THREAD_SELF \
  173   ({ struct pthread *__self;                                                  \
  174      asm ("movq %%fs:%c1,%q0" : "=r" (__self)                                 \
  175           : "i" (offsetof (struct pthread, header.self)));                    \
  176      __self;})
  177 
  178 /* Magic for libthread_db to know how to do THREAD_SELF.  */
  179 # define DB_THREAD_SELF_INCLUDE  <sys/reg.h> /* For the FS constant.  */
  180 # define DB_THREAD_SELF CONST_THREAD_AREA (64, FS)
  181 
  182 /* Read member of the thread descriptor directly.  */
  183 # define THREAD_GETMEM(descr, member) \
  184   ({ __typeof (descr->member) __value;                                        \
  185      if (sizeof (__value) == 1)                                               \
  186        asm volatile ("movb %%fs:%P2,%b0"                                      \
  187                      : "=q" (__value)                                         \
  188                      : "" (0), "i" (offsetof (struct pthread, member)));     \
  189      else if (sizeof (__value) == 4)                                          \
  190        asm volatile ("movl %%fs:%P1,%0"                                       \
  191                      : "=r" (__value)                                         \
  192                      : "i" (offsetof (struct pthread, member)));              \
  193      else                                                                     \
  194        {                                                                      \
  195          if (sizeof (__value) != 8)                                           \
  196            /* There should not be any value with a size other than 1,         \
  197               4 or 8.  */                                                     \
  198            abort ();                                                          \
  199                                                                               \
  200          asm volatile ("movq %%fs:%P1,%q0"                                    \
  201                        : "=r" (__value)                                       \
  202                        : "i" (offsetof (struct pthread, member)));            \
  203        }                                                                      \
  204      __value; })
  205 
  206 
  207 /* Same as THREAD_GETMEM, but the member offset can be non-constant.  */
  208 # define THREAD_GETMEM_NC(descr, member, idx) \
  209   ({ __typeof (descr->member[0]) __value;                                     \
  210      if (sizeof (__value) == 1)                                               \
  211        asm volatile ("movb %%fs:%P2(%q3),%b0"                                 \
  212                      : "=q" (__value)                                         \
  213                      : "" (0), "i" (offsetof (struct pthread, member[0])),   \
  214                        "r" (idx));                                            \
  215      else if (sizeof (__value) == 4)                                          \
  216        asm volatile ("movl %%fs:%P1(,%q2,4),%0"                               \
  217                      : "=r" (__value)                                         \
  218                      : "i" (offsetof (struct pthread, member[0])), "r" (idx));\
  219      else                                                                     \
  220        {                                                                      \
  221          if (sizeof (__value) != 8)                                           \
  222            /* There should not be any value with a size other than 1,         \
  223               4 or 8.  */                                                     \
  224            abort ();                                                          \
  225                                                                               \
  226          asm volatile ("movq %%fs:%P1(,%q2,8),%q0"                            \
  227                        : "=r" (__value)                                       \
  228                        : "i" (offsetof (struct pthread, member[0])),          \
  229                          "r" (idx));                                          \
  230        }                                                                      \
  231      __value; })
  232 
  233 
  234 /* Loading addresses of objects on x86-64 needs to be treated special
  235    when generating PIC code.  */
  236 #ifdef __pic__
  237 # define IMM_MODE "nr"
  238 #else
  239 # define IMM_MODE "ir"
  240 #endif
  241 
  242 
  243 /* Same as THREAD_SETMEM, but the member offset can be non-constant.  */
  244 # define THREAD_SETMEM(descr, member, value) \
  245   ({ if (sizeof (descr->member) == 1)                                         \
  246        asm volatile ("movb %b0,%%fs:%P1" :                                    \
  247                      : "iq" (value),                                          \
  248                        "i" (offsetof (struct pthread, member)));              \
  249      else if (sizeof (descr->member) == 4)                                    \
  250        asm volatile ("movl %0,%%fs:%P1" :                                     \
  251                      : IMM_MODE (value),                                      \
  252                        "i" (offsetof (struct pthread, member)));              \
  253      else                                                                     \
  254        {                                                                      \
  255          if (sizeof (descr->member) != 8)                                     \
  256            /* There should not be any value with a size other than 1,         \
  257               4 or 8.  */                                                     \
  258            abort ();                                                          \
  259                                                                               \
  260          asm volatile ("movq %q0,%%fs:%P1" :                                  \
  261                        : IMM_MODE ((unsigned long int) value),                \
  262                          "i" (offsetof (struct pthread, member)));            \
  263        }})
  264 
  265 
  266 /* Set member of the thread descriptor directly.  */
  267 # define THREAD_SETMEM_NC(descr, member, idx, value) \
  268   ({ if (sizeof (descr->member[0]) == 1)                                      \
  269        asm volatile ("movb %b0,%%fs:%P1(%q2)" :                               \
  270                      : "iq" (value),                                          \
  271                        "i" (offsetof (struct pthread, member[0])),            \
  272                        "r" (idx));                                            \
  273      else if (sizeof (descr->member[0]) == 4)                                 \
  274        asm volatile ("movl %0,%%fs:%P1(,%q2,4)" :                             \
  275                      : IMM_MODE (value),                                      \
  276                        "i" (offsetof (struct pthread, member[0])),            \
  277                        "r" (idx));                                            \
  278      else                                                                     \
  279        {                                                                      \
  280          if (sizeof (descr->member[0]) != 8)                                  \
  281            /* There should not be any value with a size other than 1,         \
  282               4 or 8.  */                                                     \
  283            abort ();                                                          \
  284                                                                               \
  285          asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" :                          \
  286                        : IMM_MODE ((unsigned long int) value),                \
  287                          "i" (offsetof (struct pthread, member[0])),          \
  288                          "r" (idx));                                          \
  289        }})
  290 
  291 
  292 /* Atomic compare and exchange on TLS, returning old value.  */
  293 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
  294   ({ __typeof (descr->member) __ret;                                          \
  295      __typeof (oldval) __old = (oldval);                                      \
  296      if (sizeof (descr->member) == 4)                                         \
  297        asm volatile (LOCK_PREFIX "cmpxchgl %2, %%fs:%P3"                      \
  298                      : "=a" (__ret)                                           \
  299                      : "" (__old), "r" (newval),                             \
  300                        "i" (offsetof (struct pthread, member)));              \
  301      else                                                                     \
  302        /* Not necessary for other sizes in the moment.  */                    \
  303        abort ();                                                              \
  304      __ret; })
  305 
  306 
  307 /* Atomic set bit.  */
  308 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
  309   (void) ({ if (sizeof ((descr)->member) == 4)                                \
  310               asm volatile (LOCK_PREFIX "orl %1, %%fs:%P0"                    \
  311                             :: "i" (offsetof (struct pthread, member)),       \
  312                                "ir" (1 << (bit)));                            \
  313             else                                                              \
  314               /* Not necessary for other sizes in the moment.  */             \
  315               abort (); })
  316 
  317 
  318 #define CALL_THREAD_FCT(descr) \
  319   ({ void *__res;                                                             \
  320      asm volatile ("movq %%fs:%P2, %%rdi\n\t"                                 \
  321                    "callq *%%fs:%P1"                                          \
  322                    : "=a" (__res)                                             \
  323                    : "i" (offsetof (struct pthread, start_routine)),          \
  324                      "i" (offsetof (struct pthread, arg))                     \
  325                    : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11",        \
  326                      "memory", "cc");                                         \
  327      __res; })
  328 
  329 
  330 /* Set the stack guard field in TCB head.  */
  331 # define THREAD_SET_STACK_GUARD(value) \
  332     THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
  333 # define THREAD_COPY_STACK_GUARD(descr) \
  334     ((descr)->header.stack_guard                                              \
  335      = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
  336 
  337 
  338 /* Set the pointer guard field in the TCB head.  */
  339 #define THREAD_SET_POINTER_GUARD(value) \
  340   THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
  341 #define THREAD_COPY_POINTER_GUARD(descr) \
  342   ((descr)->header.pointer_guard                                              \
  343    = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
  344 
  345 
  346 /* Get and set the global scope generation counter in the TCB head.  */
  347 #define THREAD_GSCOPE_FLAG_UNUSED 0
  348 #define THREAD_GSCOPE_FLAG_USED   1
  349 #define THREAD_GSCOPE_FLAG_WAIT   2
  350 #define THREAD_GSCOPE_RESET_FLAG() \
  351   do                                                                          \
  352     { int __res;                                                              \
  353       asm volatile ("xchgl %0, %%fs:%P1"                                      \
  354                     : "=r" (__res)                                            \
  355                     : "i" (offsetof (struct pthread, header.gscope_flag)),    \
  356                       "" (THREAD_GSCOPE_FLAG_UNUSED));                       \
  357       if (__res == THREAD_GSCOPE_FLAG_WAIT)                                   \
  358         lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);    \
  359     }                                                                         \
  360   while (0)
  361 #define THREAD_GSCOPE_SET_FLAG() \
  362   THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
  363 #define THREAD_GSCOPE_WAIT() \
  364   GL(dl_wait_lookup_done) ()
  365 
  366 #endif /* __ASSEMBLER__ */
  367 
  368 #endif  /* tls.h */

Cache object: 80fa92a038a7191a0f554b59bb95c5ef


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.