Blame sysdeps/mach/hurd/i386/tls.h

Packit 6c4009
/* Definitions for thread-local data handling.  Hurd/i386 version.
Packit 6c4009
   Copyright (C) 2003-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#ifndef _I386_TLS_H
Packit 6c4009
#define _I386_TLS_H
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Some things really need not be machine-dependent.  */
Packit 6c4009
#include <sysdeps/mach/hurd/tls.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
#ifndef __ASSEMBLER__
Packit 6c4009
# include <dl-dtv.h>
Packit 6c4009
Packit 6c4009
/* Type of the TCB.  */
Packit 6c4009
typedef struct
Packit 6c4009
{
Packit 6c4009
  void *tcb;			/* Points to this structure.  */
Packit 6c4009
  dtv_t *dtv;			/* Vector of pointers to TLS data.  */
Packit 6c4009
  thread_t self;		/* This thread's control port.  */
Packit 6c4009
  int multiple_threads;
Packit 6c4009
  uintptr_t sysinfo;
Packit 6c4009
  uintptr_t stack_guard;
Packit 6c4009
  uintptr_t pointer_guard;
Packit 6c4009
  int gscope_flag;
Packit 6c4009
  int private_futex;
Packit 6c4009
  /* Reservation of some values for the TM ABI.  */
Packit 6c4009
  void *__private_tm[4];
Packit 6c4009
  /* GCC split stack support.  */
Packit 6c4009
  void *__private_ss;
Packit 6c4009
Packit 6c4009
  /* Keep this field last, so fields above can continue being compatible with
Packit 6c4009
     the Linux version. */
Packit 6c4009
  mach_port_t reply_port;      /* This thread's reply port.  */
Packit 6c4009
  struct hurd_sigstate *_hurd_sigstate;
Packit 6c4009
} tcbhead_t;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Return tcbhead_t from a TLS segment descriptor.  */
Packit 6c4009
# define HURD_DESC_TLS(desc)						      \
Packit 6c4009
  ({									      \
Packit 6c4009
   (tcbhead_t *) (   (desc->low_word >> 16)				      \
Packit 6c4009
                  | ((desc->high_word & 0xff) << 16)			      \
Packit 6c4009
                  |  (desc->high_word & 0xff000000));			      \
Packit 6c4009
  })
Packit 6c4009
Packit 6c4009
/* Return 1 if TLS is not initialized yet.  */
Packit 6c4009
#define __LIBC_NO_TLS()							      \
Packit 6c4009
  ({ unsigned short ds, gs;						      \
Packit 6c4009
     asm ("movw %%ds,%w0; movw %%gs,%w1" : "=q" (ds), "=q" (gs));	      \
Packit 6c4009
     __builtin_expect (ds == gs, 0); })
Packit 6c4009
Packit 6c4009
/* The TCB can have any size and the memory following the address the
Packit 6c4009
   thread pointer points to is unspecified.  Allocate the TCB there.  */
Packit 6c4009
#define TLS_TCB_AT_TP	1
Packit 6c4009
#define TLS_DTV_AT_TP	0
Packit 6c4009
Packit 6c4009
/* Alignment requirement for TCB.
Packit 6c4009
Packit 6c4009
   Some processors such as Intel Atom pay a big penalty on every
Packit 6c4009
   access using a segment override if that segment's base is not
Packit 6c4009
   aligned to the size of a cache line.  (See Intel 64 and IA-32
Packit 6c4009
   Architectures Optimization Reference Manual, section 13.3.3.3,
Packit 6c4009
   "Segment Base".)  On such machines, a cache line is 64 bytes.  */
Packit 6c4009
#define TCB_ALIGNMENT		64
Packit 6c4009
Packit 6c4009
#ifndef __ASSEMBLER__
Packit 6c4009
Packit 6c4009
/* Use i386-specific RPCs to arrange that %gs segment register prefix
Packit 6c4009
   addresses the TCB in each thread.  */
Packit 6c4009
# include <mach/i386/mach_i386.h>
Packit 6c4009
Packit 6c4009
# ifndef HAVE_I386_SET_GDT
Packit 6c4009
#  define __i386_set_gdt(thr, sel, desc) ((void) (thr), (void) (sel), (void) (desc), MIG_BAD_ID)
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
# include <errno.h>
Packit 6c4009
# include <assert.h>
Packit 6c4009
Packit 6c4009
# define HURD_TLS_DESC_DECL(desc, tcb)					      \
Packit 6c4009
  struct descriptor desc =						      \
Packit 6c4009
    {				/* low word: */				      \
Packit 6c4009
      0xffff			/* limit 0..15 */			      \
Packit 6c4009
      | (((unsigned int) (tcb)) << 16) /* base 0..15 */			      \
Packit 6c4009
      ,				/* high word: */			      \
Packit 6c4009
      ((((unsigned int) (tcb)) >> 16) & 0xff) /* base 16..23 */		      \
Packit 6c4009
      | ((0x12 | 0x60 | 0x80) << 8) /* access = ACC_DATA_W|ACC_PL_U|ACC_P */  \
Packit 6c4009
      | (0xf << 16)		/* limit 16..19 */			      \
Packit 6c4009
      | ((4 | 8) << 20)		/* granularity = SZ_32|SZ_G */		      \
Packit 6c4009
      | (((unsigned int) (tcb)) & 0xff000000) /* base 24..31 */		      \
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
# define HURD_SEL_LDT(sel) (__builtin_expect ((sel) & 4, 0))
Packit 6c4009
Packit 6c4009
static inline const char * __attribute__ ((unused))
Packit 6c4009
_hurd_tls_init (tcbhead_t *tcb)
Packit 6c4009
{
Packit 6c4009
  HURD_TLS_DESC_DECL (desc, tcb);
Packit 6c4009
  thread_t self = __mach_thread_self ();
Packit 6c4009
  const char *msg = NULL;
Packit 6c4009
Packit 6c4009
  /* This field is used by TLS accesses to get our "thread pointer"
Packit 6c4009
     from the TLS point of view.  */
Packit 6c4009
  tcb->tcb = tcb;
Packit 6c4009
  /* We always at least start the sigthread anyway.  */
Packit 6c4009
  tcb->multiple_threads = 1;
Packit 6c4009
Packit 6c4009
  /* Get the first available selector.  */
Packit 6c4009
  int sel = -1;
Packit 6c4009
  error_t err = __i386_set_gdt (self, &sel, desc);
Packit 6c4009
  if (err == MIG_BAD_ID)
Packit 6c4009
    {
Packit 6c4009
      /* Old kernel, use a per-thread LDT.  */
Packit 6c4009
      sel = 0x27;
Packit 6c4009
      err = __i386_set_ldt (self, sel, &desc, 1);
Packit 6c4009
      assert_perror (err);
Packit 6c4009
      if (err)
Packit 6c4009
      {
Packit 6c4009
	msg = "i386_set_ldt failed";
Packit 6c4009
	goto out;
Packit 6c4009
      }
Packit 6c4009
    }
Packit 6c4009
  else if (err)
Packit 6c4009
    {
Packit 6c4009
      assert_perror (err); /* Separate from above with different line #. */
Packit 6c4009
      msg = "i386_set_gdt failed";
Packit 6c4009
      goto out;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now install the new selector.  */
Packit 6c4009
  asm volatile ("mov %w0, %%gs" :: "q" (sel));
Packit 6c4009
Packit 6c4009
out:
Packit 6c4009
  __mach_port_deallocate (__mach_task_self (), self);
Packit 6c4009
  return msg;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Code to initially initialize the thread pointer.  This might need
Packit 6c4009
   special attention since 'errno' is not yet available and if the
Packit 6c4009
   operation can cause a failure 'errno' must not be touched.  */
Packit 6c4009
# define TLS_INIT_TP(descr) \
Packit 6c4009
    _hurd_tls_init ((tcbhead_t *) (descr))
Packit 6c4009
Packit 6c4009
/* Return the TCB address of the current thread.  */
Packit 6c4009
# define THREAD_SELF							      \
Packit 6c4009
  ({ tcbhead_t *__tcb;							      \
Packit 6c4009
     __asm__ ("movl %%gs:%c1,%0" : "=r" (__tcb)				      \
Packit 6c4009
	      : "i" (offsetof (tcbhead_t, tcb)));			      \
Packit 6c4009
     __tcb;})
Packit 6c4009
Packit 6c4009
/* Return the TCB address of a thread given its state.
Packit 6c4009
   Note: this is expensive.  */
Packit 6c4009
# define THREAD_TCB(thread, thread_state)				      \
Packit 6c4009
  ({ int __sel = (thread_state)->basic.gs;				      \
Packit 6c4009
     struct descriptor __desc, *___desc = &__desc;			      \
Packit 6c4009
     unsigned int __count = 1;						      \
Packit 6c4009
     kern_return_t __err;						      \
Packit 6c4009
     if (HURD_SEL_LDT (__sel))						      \
Packit 6c4009
       __err = __i386_get_ldt ((thread), __sel, 1, &___desc, &__count);	      \
Packit 6c4009
     else								      \
Packit 6c4009
       __err = __i386_get_gdt ((thread), __sel, &__desc);		      \
Packit 6c4009
     assert_perror (__err);						      \
Packit 6c4009
     assert (__count == 1);						      \
Packit 6c4009
     HURD_DESC_TLS (___desc);})
Packit 6c4009
Packit 6c4009
/* Install new dtv for current thread.  */
Packit 6c4009
# define INSTALL_NEW_DTV(dtvp)						      \
Packit 6c4009
  ({ asm volatile ("movl %0,%%gs:%P1"					      \
Packit 6c4009
		   : : "ir" (dtvp), "i" (offsetof (tcbhead_t, dtv))); })
Packit 6c4009
Packit 6c4009
/* Return the address of the dtv for the current thread.  */
Packit 6c4009
# define THREAD_DTV()							      \
Packit 6c4009
  ({ dtv_t *_dtv;							      \
Packit 6c4009
     asm ("movl %%gs:%P1,%0" : "=q" (_dtv) : "i" (offsetof (tcbhead_t, dtv)));\
Packit 6c4009
     _dtv; })
Packit 6c4009
Packit 6c4009
# include <mach/machine/thread_status.h>
Packit 6c4009
Packit 6c4009
/* Set up TLS in the new thread of a fork child, copying from the original.  */
Packit 6c4009
static inline kern_return_t __attribute__ ((unused))
Packit 6c4009
_hurd_tls_fork (thread_t child, thread_t orig, struct i386_thread_state *state)
Packit 6c4009
{
Packit 6c4009
  /* Fetch the selector set by _hurd_tls_init.  */
Packit 6c4009
  int sel;
Packit 6c4009
  asm ("mov %%gs, %w0" : "=q" (sel) : "0" (0));
Packit 6c4009
  if (sel == state->ds)		/* _hurd_tls_init was never called.  */
Packit 6c4009
    return 0;
Packit 6c4009
Packit 6c4009
  struct descriptor desc, *_desc = &des;;
Packit 6c4009
  error_t err;
Packit 6c4009
  unsigned int count = 1;
Packit 6c4009
Packit 6c4009
  if (HURD_SEL_LDT (sel))
Packit 6c4009
    err = __i386_get_ldt (orig, sel, 1, &_desc, &count);
Packit 6c4009
  else
Packit 6c4009
    err = __i386_get_gdt (orig, sel, &desc);
Packit 6c4009
Packit 6c4009
  assert_perror (err);
Packit 6c4009
  if (err)
Packit 6c4009
    return err;
Packit 6c4009
Packit 6c4009
  if (HURD_SEL_LDT (sel))
Packit 6c4009
    err = __i386_set_ldt (child, sel, &desc, 1);
Packit 6c4009
  else
Packit 6c4009
    err = __i386_set_gdt (child, &sel, desc);
Packit 6c4009
Packit 6c4009
  state->gs = sel;
Packit 6c4009
  return err;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static inline kern_return_t __attribute__ ((unused))
Packit 6c4009
_hurd_tls_new (thread_t child, struct i386_thread_state *state, tcbhead_t *tcb)
Packit 6c4009
{
Packit 6c4009
  /* Fetch the selector set by _hurd_tls_init.  */
Packit 6c4009
  int sel;
Packit 6c4009
  asm ("mov %%gs, %w0" : "=q" (sel) : "0" (0));
Packit 6c4009
  if (sel == state->ds)		/* _hurd_tls_init was never called.  */
Packit 6c4009
    return 0;
Packit 6c4009
Packit 6c4009
  HURD_TLS_DESC_DECL (desc, tcb);
Packit 6c4009
  error_t err;
Packit 6c4009
Packit 6c4009
  tcb->tcb = tcb;
Packit 6c4009
  tcb->self = child;
Packit 6c4009
Packit 6c4009
  if (HURD_SEL_LDT (sel))
Packit 6c4009
    err = __i386_set_ldt (child, sel, &desc, 1);
Packit 6c4009
  else
Packit 6c4009
    err = __i386_set_gdt (child, &sel, desc);
Packit 6c4009
Packit 6c4009
  state->gs = sel;
Packit 6c4009
  return err;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#endif	/* !__ASSEMBLER__ */
Packit 6c4009
Packit 6c4009
#endif	/* i386/tls.h */