tls
authorThomas Schwinge <thomas@schwinge.name>
Sat, 3 Mar 2018 10:47:56 +0000 (10:47 +0000)
committerAurelien Jarno <aurel32@debian.org>
Sat, 3 Mar 2018 10:47:56 +0000 (10:47 +0000)
TLS support.

All by Samuel Thibault.

glibc-2.8/debian/patches/hurd-i386/local-tls-support.diff 3151

2009-07-30  Samuel Thibault  <samuel.thibault@gnu.org>

Align up includes as on Linux, to fix build.
* sysdeps/mach/hurd/tls.h: Include <sysdep.h>.

Gbp-Pq: Topic hurd-i386
Gbp-Pq: Name tg-tls.diff

15 files changed:
hurd/hurdfault.c
hurd/hurdsig.c
include/errno.h
mach/mach.h
mach/setup-thread.c
sysdeps/generic/thread_state.h
sysdeps/mach/hurd/fork.c
sysdeps/mach/hurd/i386/init-first.c
sysdeps/mach/hurd/i386/tls.h
sysdeps/mach/hurd/i386/trampoline.c
sysdeps/mach/hurd/libc-lock.h
sysdeps/mach/hurd/profil.c
sysdeps/mach/hurd/setitimer.c
sysdeps/mach/i386/thread_state.h
sysdeps/mach/thread_state.h

index b5a4056d49c7c848faadfc1c102792845fb2975f..794def7b67cbc42e2a1649b5595bca6304689092 100644 (file)
@@ -204,6 +204,8 @@ _hurdsig_fault_init (void)
   /* This state will be restored when we fault.
      It runs the function above.  */
   memset (&state, 0, sizeof state);
+
+  MACHINE_THREAD_STATE_FIX_NEW (&state);
   MACHINE_THREAD_STATE_SET_PC (&state, faulted);
   MACHINE_THREAD_STATE_SET_SP (&state, faultstack, sizeof faultstack);
 
index 4f5bb9a8da3b648fcfdeeaab6855e10023f9b936..3e587a4b843cef08200b045c29e2f5b94f056afc 100644 (file)
@@ -1266,6 +1266,8 @@ _hurdsig_init (const int *intarray, size_t intarraysize)
                                 (vm_address_t *) &__hurd_sigthread_stack_base,
                                 &stacksize);
       assert_perror (err);
+      err = __mach_setup_tls (_hurd_msgport_thread);
+      assert_perror (err);
 
       __hurd_sigthread_stack_end = __hurd_sigthread_stack_base + stacksize;
       __hurd_sigthread_variables =
index 3c3d2288c8bb2a8dab9f1b49e36b7e06463a3ff1..76420b1c80bba8d8d596f9a1ff97b35cb7701b5a 100644 (file)
@@ -24,13 +24,15 @@ extern int rtld_errno attribute_hidden;
 
 #  include <tls.h>
 
-#  undef  errno
-#  if IS_IN (libc)
-#   define errno __libc_errno
-#  else
-#   define errno errno         /* For #ifndef errno tests.  */
-#  endif
+#  if !defined(__GNU__)
+#   undef  errno
+#   if IS_IN (libc)
+#    define errno __libc_errno
+#   else
+#    define errno errno                /* For #ifndef errno tests.  */
+#   endif
 extern __thread int errno attribute_tls_model_ie;
+#  endif
 
 # endif        /* IS_IN_LIB */
 
index 684be91cc3d8f53ada8711e57f0d3b188a4d0480..93398b0da5c9bf31290314a41857ec5574e1a975 100644 (file)
@@ -96,5 +96,8 @@ kern_return_t mach_setup_thread (task_t task, thread_t thread, void *pc,
                                 vm_address_t *stack_base,
                                 vm_size_t *stack_size);
 
+/* Give THREAD a TLS area.  */
+kern_return_t __mach_setup_tls (thread_t thread);
+kern_return_t mach_setup_tls (thread_t thread);
 
 #endif /* mach.h */
index 2eeacc2c2eaa7e1bbc2c5cbeb53c9415569cbc86..a8e03feacd9e2bab2d12f3692376cfca89f9a408 100644 (file)
@@ -19,6 +19,7 @@
 #include <thread_state.h>
 #include <string.h>
 #include <mach/machine/vm_param.h>
+#include <ldsodefs.h>
 #include "sysdep.h"            /* Defines stack direction.  */
 
 #define        STACK_SIZE      (16 * 1024 * 1024) /* 16MB, arbitrary.  */
@@ -72,8 +73,35 @@ __mach_setup_thread (task_t task, thread_t thread, void *pc,
   if (error = __vm_protect (task, stack, __vm_page_size, 0, VM_PROT_NONE))
     return error;
 
-  return __thread_set_state (thread, MACHINE_THREAD_STATE_FLAVOR,
+  return __thread_set_state (thread, MACHINE_NEW_THREAD_STATE_FLAVOR,
                             (natural_t *) &ts, tssize);
 }
 
 weak_alias (__mach_setup_thread, mach_setup_thread)
+
+/* Give THREAD a TLS area.  */
+kern_return_t
+__mach_setup_tls (thread_t thread)
+{
+  kern_return_t error;
+  struct machine_thread_state ts;
+  mach_msg_type_number_t tssize = MACHINE_THREAD_STATE_COUNT;
+  tcbhead_t *tcb;
+
+  if (error = __thread_get_state (thread, MACHINE_THREAD_STATE_FLAVOR,
+                            (natural_t *) &ts, &tssize))
+    return error;
+  assert (tssize == MACHINE_THREAD_STATE_COUNT);
+
+  tcb = _dl_allocate_tls(NULL);
+  if (!tcb)
+    return KERN_RESOURCE_SHORTAGE;
+
+  _hurd_tls_new(thread, &ts, tcb);
+
+  error = __thread_set_state (thread, MACHINE_THREAD_STATE_FLAVOR,
+                            (natural_t *) &ts, tssize);
+  return error;
+}
+
+weak_alias (__mach_setup_tls, mach_setup_tls)
index 32994c313ddc4f3478403ca313540355b7a188d0..7c61dbc9315d7f1c6843b547a0fa058740d28bee 100644 (file)
@@ -22,6 +22,7 @@
 
 /* Replace <machine> with "i386" or "mips" or whatever.  */
 
+#define MACHINE_NEW_THREAD_STATE_FLAVOR        <machine>_NEW_THREAD_STATE
 #define MACHINE_THREAD_STATE_FLAVOR    <machine>_THREAD_STATE
 #define MACHINE_THREAD_STATE_COUNT     <machine>_THREAD_STATE_COUNT
 
index b55eecffc6e21c5229afc013ad715b8e038f86f9..cd2c0b24fb704d06353de1b94c89c702b9ca4d1f 100644 (file)
@@ -507,6 +507,11 @@ __fork (void)
 #endif
       MACHINE_THREAD_STATE_SET_PC (&state,
                                   (unsigned long int) _hurd_msgport_receive);
+
+      /* Do special thread setup for TLS if needed.  */
+      if (err = _hurd_tls_fork (sigthread, _hurd_msgport_thread, &state))
+       LOSE;
+
       if (err = __thread_set_state (sigthread, MACHINE_THREAD_STATE_FLAVOR,
                                    (natural_t *) &state, statecount))
        LOSE;
@@ -517,7 +522,7 @@ __fork (void)
       _hurd_longjmp_thread_state (&state, env, 1);
 
       /* Do special thread setup for TLS if needed.  */
-      if (err = _hurd_tls_fork (thread, &state))
+      if (err = _hurd_tls_fork (thread, ss->thread, &state))
        LOSE;
 
       if (err = __thread_set_state (thread, MACHINE_THREAD_STATE_FLAVOR,
index 965b53ca9e97acdf73a37a2cd86ce1e3bb1b2be5..90200c365a5a3a96fcb6924f378403278931aac0 100644 (file)
@@ -110,31 +110,11 @@ init1 (int argc, char *arg0, ...)
      data block; the argument strings start there.  */
   if ((void *) d == argv[0])
     {
-#ifndef SHARED
-      /* With a new enough linker (binutils-2.23 or better),
-         the magic __ehdr_start symbol will be available and
-         __libc_start_main will have done this that way already.  */
-      if (_dl_phdr == NULL)
-        {
-          /* We may need to see our own phdrs, e.g. for TLS setup.
-             Try the usual kludge to find the headers without help from
-             the exec server.  */
-          extern const void __executable_start;
-          const ElfW(Ehdr) *const ehdr = &__executable_start;
-          _dl_phdr = (const void *) ehdr + ehdr->e_phoff;
-          _dl_phnum = ehdr->e_phnum;
-          assert (ehdr->e_phentsize == sizeof (ElfW(Phdr)));
-        }
-#endif
       return;
     }
 
 #ifndef SHARED
   __libc_enable_secure = d->flags & EXEC_SECURE;
-
-  _dl_phdr = (ElfW(Phdr) *) d->phdr;
-  _dl_phnum = d->phdrsz / sizeof (ElfW(Phdr));
-  assert (d->phdrsz % sizeof (ElfW(Phdr)) == 0);
 #endif
 
   _hurd_init_dtable = d->dtable;
@@ -190,6 +170,40 @@ init (int *data)
     ++envp;
   d = (void *) ++envp;
 
+#ifndef SHARED
+  /* If we are the bootstrap task started by the kernel,
+     then after the environment pointers there is no Hurd
+     data block; the argument strings start there.  */
+  if ((void *) d == argv[0])
+    {
+      /* With a new enough linker (binutils-2.23 or better),
+         the magic __ehdr_start symbol will be available and
+         __libc_start_main will have done this that way already.  */
+      if (_dl_phdr == NULL)
+        {
+          /* We may need to see our own phdrs, e.g. for TLS setup.
+             Try the usual kludge to find the headers without help from
+             the exec server.  */
+          extern const void __executable_start;
+          const ElfW(Ehdr) *const ehdr = &__executable_start;
+          _dl_phdr = (const void *) ehdr + ehdr->e_phoff;
+          _dl_phnum = ehdr->e_phnum;
+          assert (ehdr->e_phentsize == sizeof (ElfW(Phdr)));
+        }
+    }
+  else
+    {
+      _dl_phdr = (ElfW(Phdr) *) d->phdr;
+      _dl_phnum = d->phdrsz / sizeof (ElfW(Phdr));
+      assert (d->phdrsz % sizeof (ElfW(Phdr)) == 0);
+    }
+
+  /* We need to setup TLS before starting sigthread */
+  extern void __pthread_initialize_minimal(void);
+  if (__pthread_initialize_minimal != NULL)
+    __pthread_initialize_minimal();
+#endif
+
   /* The user might have defined a value for this, to get more variables.
      Otherwise it will be zero on startup.  We must make sure it is set
      properly before before cthreads initialization, so cthreads can know
index b194a49e349e71d173a18de1873b5b23cfa39578..8a26ccd863185f9969a6ddd86bd6027e13a4eb86 100644 (file)
@@ -52,6 +52,15 @@ typedef struct
 #define TLS_TCB_AT_TP  1
 #define TLS_DTV_AT_TP  0
 
+/* Alignment requirement for TCB.
+
+   Some processors such as Intel Atom pay a big penalty on every
+   access using a segment override if that segment's base is not
+   aligned to the size of a cache line.  (See Intel 64 and IA-32
+   Architectures Optimization Reference Manual, section 13.3.3.3,
+   "Segment Base".)  On such machines, a cache line is 64 bytes.  */
+#define TCB_ALIGNMENT          64
+
 #ifndef __ASSEMBLER__
 
 /* Use i386-specific RPCs to arrange that %gs segment register prefix
@@ -93,7 +102,7 @@ _hurd_tls_init (tcbhead_t *tcb)
 
   /* Get the first available selector.  */
   int sel = -1;
-  error_t err = __i386_set_gdt (tcb->self, &sel, desc);
+  kern_return_t err = __i386_set_gdt (tcb->self, &sel, desc);
   if (err == MIG_BAD_ID)
     {
       /* Old kernel, use a per-thread LDT.  */
@@ -141,9 +150,40 @@ _hurd_tls_init (tcbhead_t *tcb)
 
 # include <mach/machine/thread_status.h>
 
-/* Set up TLS in the new thread of a fork child, copying from our own.  */
-static inline error_t __attribute__ ((unused))
-_hurd_tls_fork (thread_t child, struct i386_thread_state *state)
+/* Set up TLS in the new thread of a fork child, copying from the original.  */
+static inline kern_return_t __attribute__ ((unused))
+_hurd_tls_fork (thread_t child, thread_t orig, struct i386_thread_state *state)
+{
+  /* Fetch the selector set by _hurd_tls_init.  */
+  int sel;
+  asm ("mov %%gs, %w0" : "=q" (sel) : "0" (0));
+  if (sel == state->ds)                /* _hurd_tls_init was never called.  */
+    return 0;
+
+  struct descriptor desc, *_desc = &desc;
+  int err;
+  unsigned int count = 1;
+
+  if (__builtin_expect (sel, 0x48) & 4) /* LDT selector */
+    err = __i386_get_ldt (orig, sel, 1, &_desc, &count);
+  else
+    err = __i386_get_gdt (orig, sel, &desc);
+
+  assert_perror (err);
+  if (err)
+    return err;
+
+  if (__builtin_expect (sel, 0x48) & 4) /* LDT selector */
+    err = __i386_set_ldt (child, sel, &desc, 1);
+  else
+    err = __i386_set_gdt (child, &sel, desc);
+
+  state->gs = sel;
+  return err;
+}
+
+static inline kern_return_t __attribute__ ((unused))
+_hurd_tls_new (thread_t child, struct i386_thread_state *state, tcbhead_t *tcb)
 {
   /* Fetch the selector set by _hurd_tls_init.  */
   int sel;
@@ -151,11 +191,13 @@ _hurd_tls_fork (thread_t child, struct i386_thread_state *state)
   if (sel == state->ds)                /* _hurd_tls_init was never called.  */
     return 0;
 
-  tcbhead_t *const tcb = THREAD_SELF;
   HURD_TLS_DESC_DECL (desc, tcb);
-  error_t err;
+  kern_return_t err;
+
+  tcb->tcb = tcb;
+  tcb->self = child;
 
-  if (__builtin_expect (sel, 0x50) & 4) /* LDT selector */
+  if (__builtin_expect (sel, 0x48) & 4) /* LDT selector */
     err = __i386_set_ldt (child, sel, &desc, 1);
   else
     err = __i386_set_gdt (child, &sel, desc);
index d240ca72a472237cf4fe784e4b2853e9e04b58cb..0c68759f42c26c835257e9c78bb44ed01559c2af 100644 (file)
@@ -62,7 +62,7 @@ _hurd_setup_sighandler (struct hurd_sigstate *ss, __sighandler_t handler,
                  sizeof (state->basic));
          memcpy (&state->fpu, &ss->context->sc_i386_float_state,
                  sizeof (state->fpu));
-         state->set |= (1 << i386_THREAD_STATE) | (1 << i386_FLOAT_STATE);
+         state->set |= (1 << i386_REGS_SEGS_STATE) | (1 << i386_FLOAT_STATE);
        }
     }
 
index 371912efb7b2ea5c2a3511b4786877f1a64caea1..11be081d8c75487e7337ce186e24a927d319b22d 100644 (file)
@@ -20,6 +20,9 @@
 #define _LIBC_LOCK_H 1
 
 #if (_LIBC - 0) || (_CTHREADS_ - 0)
+#if (_LIBC - 0)
+#include <tls.h>
+#endif
 #include <cthreads.h>
 #include <hurd/threadvar.h>
 
index b3f201b016364c6f08d0f2cfb2d034900e05c51a..da0cdafe6c169148379d445256350b11dd04bd2e 100644 (file)
@@ -70,6 +70,8 @@ update_waiter (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
       if (! err)
        err = __mach_setup_thread (__mach_task_self (), profile_thread,
                                   &profile_waiter, NULL, NULL);
+      if (! err)
+       err = __mach_setup_tls(profile_thread);
     }
   else
     err = 0;
index a0d294162b17c0b46e7aab5d05ca443b1b019bc1..f5dfb7da84fce34d6b6b6ced32cbc68d2293c906 100644 (file)
@@ -221,11 +221,12 @@ setitimer_locked (const struct itimerval *new, struct itimerval *old,
            goto out;
          _hurd_itimer_thread_stack_base = 0; /* Anywhere.  */
          _hurd_itimer_thread_stack_size = __vm_page_size; /* Small stack.  */
-         if (err = __mach_setup_thread (__mach_task_self (),
+         if ((err = __mach_setup_thread (__mach_task_self (),
                                         _hurd_itimer_thread,
                                         &timer_thread,
                                         &_hurd_itimer_thread_stack_base,
                                         &_hurd_itimer_thread_stack_size))
+             || (err = __mach_setup_tls(_hurd_itimer_thread)))
            {
              __thread_terminate (_hurd_itimer_thread);
              _hurd_itimer_thread = MACH_PORT_NULL;
index 56d91dfdb70a4fd62f45bef49bed7f085e4b4b89..45910f5b2479ceb1247adc35657bc0ddade892c1 100644 (file)
@@ -21,7 +21,8 @@
 
 #include <mach/machine/thread_status.h>
 
-#define MACHINE_THREAD_STATE_FLAVOR    i386_THREAD_STATE
+#define MACHINE_NEW_THREAD_STATE_FLAVOR        i386_THREAD_STATE
+#define MACHINE_THREAD_STATE_FLAVOR    i386_REGS_SEGS_STATE
 #define MACHINE_THREAD_STATE_COUNT     i386_THREAD_STATE_COUNT
 
 #define machine_thread_state i386_thread_state
 #define SP uesp
 #define SYSRETURN eax
 
+#define MACHINE_THREAD_STATE_FIX_NEW(ts) do { \
+       asm ("mov %%cs, %w0" : "=q" ((ts)->cs)); \
+       asm ("mov %%ds, %w0" : "=q" ((ts)->ds)); \
+       asm ("mov %%es, %w0" : "=q" ((ts)->es)); \
+       asm ("mov %%fs, %w0" : "=q" ((ts)->fs)); \
+       asm ("mov %%gs, %w0" : "=q" ((ts)->gs)); \
+} while(0)
+
 struct machine_thread_all_state
   {
     int set;                   /* Mask of bits (1 << FLAVOR).  */
index bc4feefe0194d9a5fc8fa8b24de09b2e264adfb0..9c560989aaaf9ce59f9b9669bcb2f758a8be379e 100644 (file)
@@ -37,6 +37,9 @@
   ((ts)->SP = (unsigned long int) (stack) + (size))
 #endif
 #endif
+#ifndef MACHINE_THREAD_STATE_FIX_NEW
+#define MACHINE_THREAD_STATE_FIX_NEW(ts)
+#endif
 
 /* These functions are of use in machine-dependent signal trampoline
    implementations.  */