1  /* Definitions for thread-local data handling.  Hurd/x86_64 version.
       2     Copyright (C) 2003-2023 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, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #ifndef _X86_64_TLS_H
      20  #define _X86_64_TLS_H
      21  
      22  
      23  /* Some things really need not be machine-dependent.  */
      24  #include <sysdeps/mach/hurd/tls.h>
      25  
      26  
      27  #ifndef __ASSEMBLER__
      28  # include <dl-dtv.h>
      29  # include <mach/machine/thread_status.h>
      30  # include <errno.h>
      31  # include <assert.h>
      32  
      33  /* Type of the TCB.  */
      34  typedef struct
      35  {
      36    void *tcb;			/* Points to this structure.  */
      37    dtv_t *dtv;			/* Vector of pointers to TLS data.  */
      38    thread_t self_do_no_use;	/* This thread's control port.  */
      39    int __glibc_padding1;
      40    int multiple_threads;
      41    int gscope_flag;
      42    uintptr_t sysinfo;
      43    uintptr_t stack_guard;
      44    uintptr_t pointer_guard;
      45    long __glibc_padding2[2];
      46    int private_futex;
      47    int __glibc_padding3;
      48    /* Reservation of some values for the TM ABI.  */
      49    void *__private_tm[4];
      50    /* GCC split stack support.  */
      51    void *__private_ss;
      52    /* The lowest address of shadow stack.  */
      53    unsigned long long int ssp_base;
      54  
      55    /* Keep these fields last, so offsets of fields above can continue being
      56       compatible with the x86_64 NPTL version.  */
      57    mach_port_t reply_port;      /* This thread's reply port.  */
      58    struct hurd_sigstate *_hurd_sigstate;
      59  
      60    /* Used by the exception handling implementation in the dynamic loader.  */
      61    struct rtld_catch *rtld_catch;
      62  } tcbhead_t;
      63  
      64  /* GCC generates %fs:0x28 to access the stack guard.  */
      65  _Static_assert (offsetof (tcbhead_t, stack_guard) == 0x28,
      66                  "stack guard offset");
      67  /* libgcc uses %fs:0x70 to access the split stack pointer.  */
      68  _Static_assert (offsetof (tcbhead_t, __private_ss) == 0x70,
      69                  "split stack pointer offset");
      70  
      71  /* The TCB can have any size and the memory following the address the
      72     thread pointer points to is unspecified.  Allocate the TCB there.  */
      73  # define TLS_TCB_AT_TP	1
      74  # define TLS_DTV_AT_TP	0
      75  
      76  /* Alignment requirement for TCB.
      77  
      78     Some processors such as Intel Atom pay a big penalty on every
      79     access using a segment override if that segment's base is not
      80     aligned to the size of a cache line.  (See Intel 64 and IA-32
      81     Architectures Optimization Reference Manual, section 13.3.3.3,
      82     "Segment Base".)  On such machines, a cache line is 64 bytes.  */
      83  # define TCB_ALIGNMENT	64
      84  
      85  
      86  # define THREAD_SELF							\
      87    (*(tcbhead_t * __seg_fs *) offsetof (tcbhead_t, tcb))
      88  /* Read member of the thread descriptor directly.  */
      89  # define THREAD_GETMEM(descr, member)					\
      90    (*(__typeof (descr->member) __seg_fs *) offsetof (tcbhead_t, member))
      91  /* Write member of the thread descriptor directly.  */
      92  # define THREAD_SETMEM(descr, member, value)				\
      93    (*(__typeof (descr->member) __seg_fs *) offsetof (tcbhead_t, member) = value)
      94  
      95  
      96  /* Return the TCB address of a thread given its state.
      97     Note: this is expensive.  */
      98  static inline tcbhead_t * __attribute__ ((unused))
      99  THREAD_TCB (thread_t thread,
     100              const void *all_state __attribute__ ((unused)))
     101  {
     102    error_t err;
     103    /* Fetch the target thread's state.  */
     104    struct i386_fsgs_base_state state;
     105    mach_msg_type_number_t state_count = i386_FSGS_BASE_STATE_COUNT;
     106    err = __thread_get_state (thread, i386_FSGS_BASE_STATE,
     107                              (thread_state_t) &state,
     108                              &state_count);
     109    assert_perror (err);
     110    assert (state_count == i386_FSGS_BASE_STATE_COUNT);
     111    return (tcbhead_t *) state.fs_base;
     112  }
     113  
     114  /* Install new dtv for current thread.  */
     115  # define INSTALL_NEW_DTV(dtvp) THREAD_SETMEM (THREAD_SELF, dtv, dtvp)
     116  
     117  /* Return the address of the dtv for the current thread.  */
     118  # define THREAD_DTV() THREAD_GETMEM (THREAD_SELF, dtv)
     119  
     120  
     121  /* Set the stack guard field in TCB head.  */
     122  # define THREAD_SET_STACK_GUARD(value)					\
     123    THREAD_SETMEM (THREAD_SELF, stack_guard, value)
     124  # define THREAD_COPY_STACK_GUARD(descr)					\
     125    ((descr)->stack_guard							\
     126     = THREAD_GETMEM (THREAD_SELF, stack_guard))
     127  
     128  /* Set the pointer guard field in the TCB head.  */
     129  # define THREAD_SET_POINTER_GUARD(value)				\
     130    THREAD_SETMEM (THREAD_SELF, pointer_guard, value)
     131  # define THREAD_COPY_POINTER_GUARD(descr)				\
     132    ((descr)->pointer_guard						\
     133     = THREAD_GETMEM (THREAD_SELF, pointer_guard))
     134  
     135  /* From hurd.h, reproduced here to avoid a circular include.  */
     136  extern thread_t __hurd_thread_self (void);
     137  libc_hidden_proto (__hurd_thread_self)
     138  
     139  /* Set up TLS in the new thread of a fork child, copying from the original.  */
     140  static inline kern_return_t __attribute__ ((unused))
     141  _hurd_tls_fork (thread_t child, thread_t orig,
     142                  void *machine_state __attribute__ ((unused)))
     143  {
     144    error_t err;
     145    struct i386_fsgs_base_state state;
     146    mach_msg_type_number_t state_count = i386_FSGS_BASE_STATE_COUNT;
     147  
     148    if (orig != __hurd_thread_self ())
     149      {
     150        err = __thread_get_state (orig, i386_FSGS_BASE_STATE,
     151                                  (thread_state_t) &state,
     152                                  &state_count);
     153        if (err)
     154          return err;
     155        assert (state_count == i386_FSGS_BASE_STATE_COUNT);
     156      }
     157    else
     158      {
     159        /* It is illegal to call thread_get_state () on mach_thread_self ().
     160           But we're only interested in the value of fs_base, and since we're
     161           this thread, we know it points to our TCB.  */
     162        state.fs_base = (unsigned long) THREAD_SELF;
     163        state.gs_base = 0;
     164      }
     165  
     166    return __thread_set_state (child, i386_FSGS_BASE_STATE,
     167                               (thread_state_t) &state,
     168                               state_count);
     169  }
     170  
     171  static inline kern_return_t __attribute__ ((unused))
     172  _hurd_tls_new (thread_t child, tcbhead_t *tcb)
     173  {
     174    struct i386_fsgs_base_state state;
     175  
     176    tcb->tcb = tcb;
     177  
     178    /* Install the TCB address into FS base.  */
     179    state.fs_base = (uintptr_t) tcb;
     180    state.gs_base = 0;
     181    return __thread_set_state (child, i386_FSGS_BASE_STATE,
     182                               (thread_state_t) &state,
     183                               i386_FSGS_BASE_STATE_COUNT);
     184  }
     185  
     186  # if !defined (SHARED) || IS_IN (rtld)
     187  extern unsigned char __libc_tls_initialized;
     188  #  define __LIBC_NO_TLS() __builtin_expect (!__libc_tls_initialized, 0)
     189  
     190  static inline bool __attribute__ ((unused))
     191  _hurd_tls_init (tcbhead_t *tcb, bool full)
     192  {
     193    error_t err;
     194    thread_t self = __mach_thread_self ();
     195    extern mach_port_t __hurd_reply_port0;
     196  
     197    /* We always at least start the sigthread anyway.  */
     198    tcb->multiple_threads = 1;
     199    if (full)
     200      /* Take over the reply port we've been using.  */
     201      tcb->reply_port = __hurd_reply_port0;
     202  
     203    err = _hurd_tls_new (self, tcb);
     204    if (err == 0 && full)
     205      {
     206        __libc_tls_initialized = 1;
     207        /* This port is now owned by the TCB.  */
     208        __hurd_reply_port0 = MACH_PORT_NULL;
     209      }
     210    __mach_port_deallocate (__mach_task_self (), self);
     211    return err == 0;
     212  }
     213  
     214  #  define TLS_INIT_TP(descr) _hurd_tls_init ((tcbhead_t *) (descr), 1)
     215  # else /* defined (SHARED) && !IS_IN (rtld) */
     216  #  define __LIBC_NO_TLS() 0
     217  # endif
     218  
     219  
     220  
     221  /* Global scope switch support.  */
     222  # define THREAD_GSCOPE_FLAG_UNUSED 0
     223  # define THREAD_GSCOPE_FLAG_USED   1
     224  # define THREAD_GSCOPE_FLAG_WAIT   2
     225  
     226  # define THREAD_GSCOPE_SET_FLAG() \
     227    THREAD_SETMEM (THREAD_SELF, gscope_flag, THREAD_GSCOPE_FLAG_USED)
     228  
     229  # define THREAD_GSCOPE_RESET_FLAG() \
     230    ({									\
     231      int __flag;								\
     232      asm volatile ("xchgl %0, %%fs:%P1"					\
     233                    : "=r" (__flag)					\
     234                    : "i" (offsetof (tcbhead_t, gscope_flag)),		\
     235                      "0" (THREAD_GSCOPE_FLAG_UNUSED));			\
     236      if (__flag == THREAD_GSCOPE_FLAG_WAIT)				\
     237        lll_wake (THREAD_SELF->gscope_flag, LLL_PRIVATE);			\
     238    })
     239  
     240  
     241  
     242  #endif	/* __ASSEMBLER__ */
     243  #endif	/* x86_64/tls.h */