root/mtcp/NOTES-x86_64/tls-i386.h

/* [<][>][^][v][top][bottom][index][help] */
   1 /* Definition for thread-local data handling.  nptl/i386 version.
   2    Copyright (C) 2002, 2003, 2004, 2005 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 #include <dl-sysdep.h>
  24 #ifndef __ASSEMBLER__
  25 # include <stdbool.h>
  26 # include <stddef.h>
  27 # include <stdint.h>
  28 # include <stdlib.h>
  29 # include <list.h>
  30 
  31 
  32 /* Type for the dtv.  */
  33 typedef union dtv
  34 {
  35   size_t counter;
  36   struct
  37   {
  38     void *val;
  39     bool is_static;
  40   } pointer;
  41 } dtv_t;
  42 
  43 
  44 typedef struct
  45 {
  46   void *tcb;            /* Pointer to the TCB.  Not necessarily the
  47                            thread descriptor used by libpthread.  */
  48   dtv_t *dtv;
  49   void *self;           /* Pointer to the thread descriptor.  */
  50   int multiple_threads;
  51   uintptr_t sysinfo;
  52   uintptr_t stack_guard;
  53   uintptr_t pointer_guard;
  54 } tcbhead_t;
  55 
  56 # define TLS_MULTIPLE_THREADS_IN_TCB 1
  57 
  58 #else /* __ASSEMBLER__ */
  59 # include <tcb-offsets.h>
  60 #endif
  61 
  62 
  63 /* We require TLS support in the tools.  */
  64 #ifndef HAVE_TLS_SUPPORT
  65 # error "TLS support is required."
  66 #endif
  67 
  68 /* Signal that TLS support is available.  */
  69 #define USE_TLS 1
  70 
  71 /* Alignment requirement for the stack.  For IA-32 this is governed by
  72    the SSE memory functions.  */
  73 #define STACK_ALIGN     16
  74 
  75 #ifndef __ASSEMBLER__
  76 /* Get system call information.  */
  77 # include <sysdep.h>
  78 
  79 /* The old way: using LDT.  */
  80 
  81 /* Structure passed to `modify_ldt', 'set_thread_area', and 'clone' calls.  */
  82 struct user_desc
  83 {
  84   unsigned int entry_number;
  85   unsigned long int base_addr;
  86   unsigned int limit;
  87   unsigned int seg_32bit:1;
  88   unsigned int contents:2;
  89   unsigned int read_exec_only:1;
  90   unsigned int limit_in_pages:1;
  91   unsigned int seg_not_present:1;
  92   unsigned int useable:1;
  93   unsigned int empty:25;
  94 };
  95 
  96 /* Initializing bit fields is slow.  We speed it up by using a union.  */
  97 union user_desc_init
  98 {
  99   struct user_desc desc;
 100   unsigned int vals[4];
 101 };
 102 
 103 
 104 /* Get the thread descriptor definition.  */
 105 # include <nptl/descr.h>
 106 
 107 /* This is the size of the initial TCB.  Can't be just sizeof (tcbhead_t),
 108    because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
 109    struct pthread even when not linked with -lpthread.  */
 110 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
 111 
 112 /* Alignment requirements for the initial TCB.  */
 113 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
 114 
 115 /* This is the size of the TCB.  */
 116 # define TLS_TCB_SIZE sizeof (struct pthread)
 117 
 118 /* Alignment requirements for the TCB.  */
 119 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
 120 
 121 /* The TCB can have any size and the memory following the address the
 122    thread pointer points to is unspecified.  Allocate the TCB there.  */
 123 # define TLS_TCB_AT_TP  1
 124 
 125 
 126 /* Install the dtv pointer.  The pointer passed is to the element with
 127    index -1 which contain the length.  */
 128 # define INSTALL_DTV(descr, dtvp) \
 129   ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
 130 
 131 /* Install new dtv for current thread.  */
 132 # define INSTALL_NEW_DTV(dtvp) \
 133   ({ struct pthread *__pd;                                                    \
 134      THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
 135 
 136 /* Return dtv of given thread descriptor.  */
 137 # define GET_DTV(descr) \
 138   (((tcbhead_t *) (descr))->dtv)
 139 
 140 #define THREAD_SELF_SYSINFO     THREAD_GETMEM (THREAD_SELF, header.sysinfo)
 141 #define THREAD_SYSINFO(pd)      ((pd)->header.sysinfo)
 142 
 143 /* Macros to load from and store into segment registers.  */
 144 # ifndef TLS_GET_GS
 145 #  define TLS_GET_GS() \
 146   ({ int __seg; __asm ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
 147 # endif
 148 # ifndef TLS_SET_GS
 149 #  define TLS_SET_GS(val) \
 150   __asm ("movw %w0, %%gs" :: "q" (val))
 151 # endif
 152 
 153 
 154 # ifndef __NR_set_thread_area
 155 #  define __NR_set_thread_area 243
 156 # endif
 157 # ifndef TLS_FLAG_WRITABLE
 158 #  define TLS_FLAG_WRITABLE             0x00000001
 159 # endif
 160 
 161 // XXX Enable for the real world.
 162 #if 0
 163 # ifndef __ASSUME_SET_THREAD_AREA
 164 #  error "we need set_thread_area"
 165 # endif
 166 #endif
 167 
 168 # ifdef __PIC__
 169 #  define TLS_EBX_ARG "r"
 170 #  define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
 171 # else
 172 #  define TLS_EBX_ARG "b"
 173 #  define TLS_LOAD_EBX
 174 # endif
 175 
 176 #if defined NEED_DL_SYSINFO
 177 # define INIT_SYSINFO \
 178   _head->sysinfo = GLRO(dl_sysinfo)
 179 #else
 180 # define INIT_SYSINFO
 181 #endif
 182 
 183 #ifndef LOCK_PREFIX
 184 # ifdef UP
 185 #  define LOCK_PREFIX  /* nothing */
 186 # else
 187 #  define LOCK_PREFIX "lock;"
 188 # endif
 189 #endif
 190 
 191 /* Code to initially initialize the thread pointer.  This might need
 192    special attention since 'errno' is not yet available and if the
 193    operation can cause a failure 'errno' must not be touched.  */
 194 # define TLS_INIT_TP(thrdescr, secondcall) \
 195   ({ void *_thrdescr = (thrdescr);                                            \
 196      tcbhead_t *_head = _thrdescr;                                            \
 197      union user_desc_init _segdescr;                                          \
 198      int _result;                                                             \
 199                                                                               \
 200      _head->tcb = _thrdescr;                                                  \
 201      /* For now the thread descriptor is at the same address.  */             \
 202      _head->self = _thrdescr;                                                 \
 203      /* New syscall handling support.  */                                     \
 204      INIT_SYSINFO;                                                            \
 205                                                                               \
 206      /* The 'entry_number' field.  Let the kernel pick a value.  */           \
 207      if (secondcall)                                                          \
 208        _segdescr.vals[0] = TLS_GET_GS () >> 3;                                \
 209      else                                                                     \
 210        _segdescr.vals[0] = -1;                                                \
 211      /* The 'base_addr' field.  Pointer to the TCB.  */                       \
 212      _segdescr.vals[1] = (unsigned long int) _thrdescr;                       \
 213      /* The 'limit' field.  We use 4GB which is 0xfffff pages.  */            \
 214      _segdescr.vals[2] = 0xfffff;                                             \
 215      /* Collapsed value of the bitfield:                                      \
 216           .seg_32bit = 1                                                      \
 217           .contents = 0                                                       \
 218           .read_exec_only = 0                                                 \
 219           .limit_in_pages = 1                                                 \
 220           .seg_not_present = 0                                                \
 221           .useable = 1 */                                                     \
 222      _segdescr.vals[3] = 0x51;                                                \
 223                                                                               \
 224      /* Install the TLS.  */                                                  \
 225      asm volatile (TLS_LOAD_EBX                                               \
 226                    "int $0x80\n\t"                                            \
 227                    TLS_LOAD_EBX                                               \
 228                    : "=a" (_result), "=m" (_segdescr.desc.entry_number)       \
 229                    : "0" (__NR_set_thread_area),                              \
 230                      TLS_EBX_ARG (&_segdescr.desc), "m" (_segdescr.desc));    \
 231                                                                               \
 232      if (_result == 0)                                                        \
 233        /* We know the index in the GDT, now load the segment register.        \
 234           The use of the GDT is described by the value 3 in the lower         \
 235           three bits of the segment descriptor value.                         \
 236                                                                               \
 237           Note that we have to do this even if the numeric value of           \
 238           the descriptor does not change.  Loading the segment register       \
 239           causes the segment information from the GDT to be loaded            \
 240           which is necessary since we have changed it.   */                   \
 241        TLS_SET_GS (_segdescr.desc.entry_number * 8 + 3);                      \
 242                                                                               \
 243      _result == 0 ? NULL                                                      \
 244      : "set_thread_area failed when setting up thread-local storage\n"; })
 245 
 246 
 247 /* Return the address of the dtv for the current thread.  */
 248 # define THREAD_DTV() \
 249   ({ struct pthread *__pd;                                                    \
 250      THREAD_GETMEM (__pd, header.dtv); })
 251 
 252 
 253 /* Return the thread descriptor for the current thread.
 254 
 255    The contained asm must *not* be marked volatile since otherwise
 256    assignments like
 257         pthread_descr self = thread_self();
 258    do not get optimized away.  */
 259 # define THREAD_SELF \
 260   ({ struct pthread *__self;                                                  \
 261      asm ("movl %%gs:%c1,%0" : "=r" (__self)                                  \
 262           : "i" (offsetof (struct pthread, header.self)));                    \
 263      __self;})
 264 
 265 /* Magic for libthread_db to know how to do THREAD_SELF.  */
 266 # define DB_THREAD_SELF \
 267   REGISTER_THREAD_AREA (32, offsetof (struct user_regs_struct, xgs), 3) \
 268   REGISTER_THREAD_AREA (64, 26 * 8, 3) /* x86-64's user_regs_struct->gs */
 269 
 270 
 271 /* Read member of the thread descriptor directly.  */
 272 # define THREAD_GETMEM(descr, member) \
 273   ({ __typeof (descr->member) __value;                                        \
 274      if (sizeof (__value) == 1)                                               \
 275        asm volatile ("movb %%gs:%P2,%b0"                                      \
 276                      : "=q" (__value)                                         \
 277                      : "0" (0), "i" (offsetof (struct pthread, member)));     \
 278      else if (sizeof (__value) == 4)                                          \
 279        asm volatile ("movl %%gs:%P1,%0"                                       \
 280                      : "=r" (__value)                                         \
 281                      : "i" (offsetof (struct pthread, member)));              \
 282      else                                                                     \
 283        {                                                                      \
 284          if (sizeof (__value) != 8)                                           \
 285            /* There should not be any value with a size other than 1,         \
 286               4 or 8.  */                                                     \
 287            abort ();                                                          \
 288                                                                               \
 289          asm volatile ("movl %%gs:%P1,%%eax\n\t"                              \
 290                        "movl %%gs:%P2,%%edx"                                  \
 291                        : "=A" (__value)                                       \
 292                        : "i" (offsetof (struct pthread, member)),             \
 293                          "i" (offsetof (struct pthread, member) + 4));        \
 294        }                                                                      \
 295      __value; })
 296 
 297 
 298 /* Same as THREAD_GETMEM, but the member offset can be non-constant.  */
 299 # define THREAD_GETMEM_NC(descr, member, idx) \
 300   ({ __typeof (descr->member[0]) __value;                                     \
 301      if (sizeof (__value) == 1)                                               \
 302        asm volatile ("movb %%gs:%P2(%3),%b0"                                  \
 303                      : "=q" (__value)                                         \
 304                      : "0" (0), "i" (offsetof (struct pthread, member[0])),   \
 305                      "r" (idx));                                              \
 306      else if (sizeof (__value) == 4)                                          \
 307        asm volatile ("movl %%gs:%P1(,%2,4),%0"                                \
 308                      : "=r" (__value)                                         \
 309                      : "i" (offsetof (struct pthread, member[0])),            \
 310                        "r" (idx));                                            \
 311      else                                                                     \
 312        {                                                                      \
 313          if (sizeof (__value) != 8)                                           \
 314            /* There should not be any value with a size other than 1,         \
 315               4 or 8.  */                                                     \
 316            abort ();                                                          \
 317                                                                               \
 318          asm volatile  ("movl %%gs:%P1(,%2,8),%%eax\n\t"                      \
 319                         "movl %%gs:4+%P1(,%2,8),%%edx"                        \
 320                         : "=&A" (__value)                                     \
 321                         : "i" (offsetof (struct pthread, member[0])),         \
 322                           "r" (idx));                                         \
 323        }                                                                      \
 324      __value; })
 325 
 326 
 327 /* Same as THREAD_SETMEM, but the member offset can be non-constant.  */
 328 # define THREAD_SETMEM(descr, member, value) \
 329   ({ if (sizeof (descr->member) == 1)                                         \
 330        asm volatile ("movb %b0,%%gs:%P1" :                                    \
 331                      : "iq" (value),                                          \
 332                        "i" (offsetof (struct pthread, member)));              \
 333      else if (sizeof (descr->member) == 4)                                    \
 334        asm volatile ("movl %0,%%gs:%P1" :                                     \
 335                      : "ir" (value),                                          \
 336                        "i" (offsetof (struct pthread, member)));              \
 337      else                                                                     \
 338        {                                                                      \
 339          if (sizeof (descr->member) != 8)                                     \
 340            /* There should not be any value with a size other than 1,         \
 341               4 or 8.  */                                                     \
 342            abort ();                                                          \
 343                                                                               \
 344          asm volatile ("movl %%eax,%%gs:%P1\n\t"                              \
 345                        "movl %%edx,%%gs:%P2" :                                \
 346                        : "A" (value),                                         \
 347                          "i" (offsetof (struct pthread, member)),             \
 348                          "i" (offsetof (struct pthread, member) + 4));        \
 349        }})
 350 
 351 
 352 /* Set member of the thread descriptor directly.  */
 353 # define THREAD_SETMEM_NC(descr, member, idx, value) \
 354   ({ if (sizeof (descr->member[0]) == 1)                                      \
 355        asm volatile ("movb %b0,%%gs:%P1(%2)" :                                \
 356                      : "iq" (value),                                          \
 357                        "i" (offsetof (struct pthread, member)),               \
 358                        "r" (idx));                                            \
 359      else if (sizeof (descr->member[0]) == 4)                                 \
 360        asm volatile ("movl %0,%%gs:%P1(,%2,4)" :                              \
 361                      : "ir" (value),                                          \
 362                        "i" (offsetof (struct pthread, member)),               \
 363                        "r" (idx));                                            \
 364      else                                                                     \
 365        {                                                                      \
 366          if (sizeof (descr->member[0]) != 8)                                  \
 367            /* There should not be any value with a size other than 1,         \
 368               4 or 8.  */                                                     \
 369            abort ();                                                          \
 370                                                                               \
 371          asm volatile ("movl %%eax,%%gs:%P1(,%2,8)\n\t"                       \
 372                        "movl %%edx,%%gs:4+%P1(,%2,8)" :                       \
 373                        : "A" (value),                                         \
 374                          "i" (offsetof (struct pthread, member)),             \
 375                          "r" (idx));                                          \
 376        }})
 377 
 378 
 379 /* Atomic compare and exchange on TLS, returning old value.  */
 380 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
 381   ({ __typeof (descr->member) __ret;                                          \
 382      __typeof (oldval) __old = (oldval);                                      \
 383      if (sizeof (descr->member) == 4)                                         \
 384        asm volatile (LOCK_PREFIX "cmpxchgl %2, %%gs:%P3"                      \
 385                      : "=a" (__ret)                                           \
 386                      : "0" (__old), "r" (newval),                             \
 387                        "i" (offsetof (struct pthread, member)));              \
 388      else                                                                     \
 389        /* Not necessary for other sizes in the moment.  */                    \
 390        abort ();                                                              \
 391      __ret; })
 392 
 393 
 394 /* Atomic set bit.  */
 395 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
 396   (void) ({ if (sizeof ((descr)->member) == 4)                                \
 397               asm volatile (LOCK_PREFIX "orl %1, %%gs:%P0"                    \
 398                             :: "i" (offsetof (struct pthread, member)),       \
 399                                "ir" (1 << (bit)));                            \
 400             else                                                              \
 401               /* Not necessary for other sizes in the moment.  */             \
 402               abort (); })
 403 
 404 
 405 /* Call the user-provided thread function.  */
 406 #define CALL_THREAD_FCT(descr) \
 407   ({ void *__res;                                                             \
 408      int __ignore1, __ignore2;                                                \
 409      asm volatile ("pushl %%eax\n\t"                                          \
 410                    "pushl %%eax\n\t"                                          \
 411                    "pushl %%eax\n\t"                                          \
 412                    "pushl %%gs:%P4\n\t"                                       \
 413                    "call *%%gs:%P3\n\t"                                       \
 414                    "addl $16, %%esp"                                          \
 415                    : "=a" (__res), "=c" (__ignore1), "=d" (__ignore2)         \
 416                    : "i" (offsetof (struct pthread, start_routine)),          \
 417                      "i" (offsetof (struct pthread, arg)));                   \
 418      __res; })
 419 
 420 
 421 /* Set the stack guard field in TCB head.  */
 422 #define THREAD_SET_STACK_GUARD(value) \
 423   THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
 424 #define THREAD_COPY_STACK_GUARD(descr) \
 425   ((descr)->header.stack_guard                                                \
 426    = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
 427 
 428 
 429 /* Set the pointer guard field in the TCB head.  */
 430 #define THREAD_SET_POINTER_GUARD(value) \
 431   THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
 432 #define THREAD_COPY_POINTER_GUARD(descr) \
 433   ((descr)->header.pointer_guard                                              \
 434    = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
 435 
 436 
 437 #endif /* __ASSEMBLER__ */
 438 
 439 #endif  /* tls.h */

/* [<][>][^][v][top][bottom][index][help] */