summary refs log tree commit diff
path: root/sysdeps/mach/hurd/i386
diff options
context:
space:
mode:
authorSamuel Thibault <samuel.thibault@ens-lyon.org>2018-03-17 23:27:34 +0100
committerSamuel Thibault <samuel.thibault@ens-lyon.org>2018-03-17 23:29:57 +0100
commitdc33bef307ad6eeb1cc4e4e230b69c6058890388 (patch)
tree94b8499e652a04871c4cb48225d85e9c5af455c9 /sysdeps/mach/hurd/i386
parent34e6a8694d9dd80558150f8542359b93c6c57e44 (diff)
downloadglibc-dc33bef307ad6eeb1cc4e4e230b69c6058890388.tar.gz
glibc-dc33bef307ad6eeb1cc4e4e230b69c6058890388.tar.xz
glibc-dc33bef307ad6eeb1cc4e4e230b69c6058890388.zip
hurd: Replace threadvars with TLS
This gets rid of a lot of kludge and gets closer to other ports.

	* hurd/Makefile (headers): Remove threadvar.h.
	(inline-headers): Remove threadvar.h.
	* hurd/Versions (GLIBC_2.0: Remove __hurd_sigthread_stack_base,
	__hurd_sigthread_stack_end, __hurd_sigthread_variables,
	__hurd_threadvar_max, __hurd_errno_location.
	(HURD_CTHREADS_0.3): Add pthread_getattr_np, pthread_attr_getstack.
	* hurd/hurd/signal.h: Do not include <hurd/threadvar.h>.
	(_hurd_self_sigstate): Use THREAD_SELF to get _hurd_sigstate.
	(_HURD_SIGNAL_H_EXTERN_INLINE): Use THREAD_SELF to get _hurd_sigstate,
	unless TLS is not initialized yet, in which case we do not need a
	critical section yet anyway.
	* hurd/hurd/threadvar.h: Include <tls.h>, do not include
	<machine-sp.h>.
	(__hurd_sigthread_variables, __hurd_threadvar_max): Remove variables
	declarations.
	(__hurd_threadvar_index): Remove enum.
	(_HURD_THREADVAR_H_EXTERN_INLINE): Remove macro.
	(__hurd_threadvar_location_from_sp,__hurd_threadvar_location): Remove
	inlines.
	(__hurd_reply_port0): New variable declaration.
	(__hurd_local_reply_port): New macro.
	* hurd/hurdsig.c (__hurd_sigthread_variables): Remove variable.
	(interrupted_reply_port_location): Add thread_t parameter.  Use it
	with THREAD_TCB to access thread-local variables.
	(_hurdsig_abort_rpcs): Pass ss->thread to
	interrupted_reply_port_location.
	(_hurd_internal_post_signal): Likewise.
	(_hurdsig_init): Use presence of cthread_fork instead of
	__hurd_threadvar_stack_mask to start signal thread by hand.
	Remove signal thread threadvar initialization.
	* hurd/hurdstartup.c: Do not include <hurd/threadvar.h>
	* hurd/sigunwind.c: Include <hurd/threadvar.h>
	(_hurdsig_longjmp_from_handler): Use __hurd_local_reply_port instead
	of threadvar.
	* sysdeps/mach/hurd/Versions (libc.GLIBC_PRIVATE): Add
	__libc_lock_self0.
	(ld.GLIBC_2.0): Remove __hurd_sigthread_stack_base,
	__hurd_sigthread_stack_end, __hurd_sigthread_variables.
	(ld.GLIBC_PRIVATE): Add __libc_lock_self0.
	* sysdeps/mach/hurd/cthreads.c: Add __libc_lock_self0.
	* sysdeps/mach/hurd/dl-sysdep.c (errno, __hurd_sigthread_stack_base,
	__hurd_sigthread_stack_end, __hurd_sigthread_variables, threadvars,
	__hurd_threadvar_stack_offset, __hurd_threadvar_stack_mask): Do not
	define variables.
	* sysdeps/mach/hurd/errno-loc.c: Do not include <errno.h> and
	<hurd/threadvar.h>.
	[IS_IN(rtld)] (rtld_errno): New variable.
	[IS_IN(rtld)] (__errno_location): New weak function.
	[!IS_IN(rtld)]: Include "../../../csu/errno-loc.c".
	* sysdeps/mach/hurd/errno.c: Remove file.
	* sysdeps/mach/hurd/fork.c: Include <hurd/threadvar.h>
	(__fork): Remove THREADVAR_SPACE macro and its use.
	* sysdeps/mach/hurd/i386/init-first.c (__hurd_threadvar_max): Remove
	variable.
	(init): Do not initialize threadvar.
	* sysdeps/mach/hurd/i386/libc.abilist (__hurd_threadvar_max): Remove
	symbol.
	* sysdeps/mach/hurd/i386/sigreturn.c (__sigreturn): Use
	__hurd_local_reply_port instead of threadvar.
	* sysdeps/mach/hurd/i386/tls.h (tcbhead_t): Add reply_port and
	_hurd_sigstate fields.
	(HURD_DESC_TLS, __LIBC_NO_TLS, THREAD_TCB): New macro.
	* sysdeps/mach/hurd/i386/trampoline.c: Remove outdated comment.
	* sysdeps/mach/hurd/libc-lock.h: Do not include <hurd/threadvar.h>.
	(__libc_lock_owner_self): Use &__libc_lock_self0 and THREAD_SELF
	instead of threadvar.
	* sysdeps/mach/hurd/libc-tsd.h: Remove file.
	* sysdeps/mach/hurd/mig-reply.c (GETPORT, reply_port): Remove macros.
	(use_threadvar, global_reply_port): Remove variables.
	(__hurd_reply_port0): New variable.
	(__mig_get_reply_port): Use __hurd_local_reply_port and
	__hurd_reply_port0 instead of threadvar.
	(__mig_dealloc_reply_port): Likewise.
	(__mig_init): Do not initialize threadvar.
	* sysdeps/mach/hurd/profil.c: Fix comment.
Diffstat (limited to 'sysdeps/mach/hurd/i386')
-rw-r--r--sysdeps/mach/hurd/i386/init-first.c37
-rw-r--r--sysdeps/mach/hurd/i386/libc.abilist1
-rw-r--r--sysdeps/mach/hurd/i386/sigreturn.c5
-rw-r--r--sysdeps/mach/hurd/i386/tls.h33
-rw-r--r--sysdeps/mach/hurd/i386/trampoline.c2
5 files changed, 35 insertions, 43 deletions
diff --git a/sysdeps/mach/hurd/i386/init-first.c b/sysdeps/mach/hurd/i386/init-first.c
index 965b53ca9e..f4a4df33de 100644
--- a/sysdeps/mach/hurd/i386/init-first.c
+++ b/sysdeps/mach/hurd/i386/init-first.c
@@ -35,7 +35,6 @@ extern void __mach_init (void);
 extern void __init_misc (int, char **, char **);
 extern void __libc_global_ctors (void);
 
-unsigned int __hurd_threadvar_max;
 unsigned long int __hurd_threadvar_stack_offset;
 unsigned long int __hurd_threadvar_stack_mask;
 
@@ -169,15 +168,6 @@ init (int *data)
   char **argv = (void *) (data + 1);
   char **envp = &argv[argc + 1];
   struct hurd_startup_data *d;
-  unsigned long int threadvars[_HURD_THREADVAR_MAX];
-
-  /* Provide temporary storage for thread-specific variables on the
-     startup stack so the cthreads initialization code can use them
-     for malloc et al, or so we can use malloc below for the real
-     threadvars array.  */
-  memset (threadvars, 0, sizeof threadvars);
-  threadvars[_HURD_THREADVAR_LOCALE] = (unsigned long int) &_nl_global_locale;
-  __hurd_threadvar_stack_offset = (unsigned long int) threadvars;
 
   /* Since the cthreads initialization code uses malloc, and the
      malloc initialization code needs to get at the environment, make
@@ -190,13 +180,6 @@ init (int *data)
     ++envp;
   d = (void *) ++envp;
 
-  /* 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
-     how much space to leave for thread variables.  */
-  if (__hurd_threadvar_max < _HURD_THREADVAR_MAX)
-    __hurd_threadvar_max = _HURD_THREADVAR_MAX;
-
 
   /* After possibly switching stacks, call `init1' (above) with the user
      code as the return address, and the argument data immediately above
@@ -212,11 +195,6 @@ init (int *data)
 
       __libc_stack_end = newsp;
 
-      /* Copy per-thread variables from that temporary
-	 area onto the new cthread stack.  */
-      memcpy (__hurd_threadvar_location_from_sp (0, newsp),
-	      threadvars, sizeof threadvars);
-
       /* Copy the argdata from the old stack to the new one.  */
       newsp = memcpy (newsp - ((char *) &d[1] - (char *) data), data,
 		      (char *) d - (char *) data);
@@ -257,25 +235,10 @@ init (int *data)
     }
   else
     {
-      /* We are not using cthreads, so we will have just a single allocated
-	 area for the per-thread variables of the main user thread.  */
-      unsigned long int *array;
-      unsigned int i;
       int usercode;
 
       void call_init1 (void);
 
-      array = malloc (__hurd_threadvar_max * sizeof (unsigned long int));
-      if (array == NULL)
-	__libc_fatal ("Can't allocate single-threaded thread variables.");
-
-      /* Copy per-thread variables from the temporary array into the
-	 newly malloc'd space.  */
-      memcpy (array, threadvars, sizeof threadvars);
-      __hurd_threadvar_stack_offset = (unsigned long int) array;
-      for (i = _HURD_THREADVAR_MAX; i < __hurd_threadvar_max; ++i)
-	array[i] = 0;
-
       /* The argument data is just above the stack frame we will unwind by
 	 returning.  Mutate our own return address to run the code below.  */
       /* The following expression would typically be written as
diff --git a/sysdeps/mach/hurd/i386/libc.abilist b/sysdeps/mach/hurd/i386/libc.abilist
index 991c41b374..9545e898c1 100644
--- a/sysdeps/mach/hurd/i386/libc.abilist
+++ b/sysdeps/mach/hurd/i386/libc.abilist
@@ -285,7 +285,6 @@ GLIBC_2.2.6 __gettimeofday F
 GLIBC_2.2.6 __gmtime_r F
 GLIBC_2.2.6 __h_errno_location F
 GLIBC_2.2.6 __hurd_fail F
-GLIBC_2.2.6 __hurd_threadvar_max D 0x4
 GLIBC_2.2.6 __hurd_threadvar_stack_mask D 0x4
 GLIBC_2.2.6 __hurd_threadvar_stack_offset D 0x4
 GLIBC_2.2.6 __isalnum_l F
diff --git a/sysdeps/mach/hurd/i386/sigreturn.c b/sysdeps/mach/hurd/i386/sigreturn.c
index 8ec1d4b91f..07633e17c8 100644
--- a/sysdeps/mach/hurd/i386/sigreturn.c
+++ b/sysdeps/mach/hurd/i386/sigreturn.c
@@ -68,7 +68,7 @@ __sigreturn (struct sigcontext *scp)
 
   if (scp->sc_onstack)
     {
-      ss->sigaltstack.ss_flags &= ~SS_ONSTACK; /* XXX threadvars */
+      ss->sigaltstack.ss_flags &= ~SS_ONSTACK;
       /* XXX cannot unlock until off sigstack */
       abort ();
     }
@@ -77,8 +77,7 @@ __sigreturn (struct sigcontext *scp)
 
   /* Destroy the MiG reply port used by the signal handler, and restore the
      reply port in use by the thread when interrupted.  */
-  reply_port =
-    (mach_port_t *) __hurd_threadvar_location (_HURD_THREADVAR_MIG_REPLY);
+  reply_port = &__hurd_local_reply_port;
   if (*reply_port)
     {
       mach_port_t port = *reply_port;
diff --git a/sysdeps/mach/hurd/i386/tls.h b/sysdeps/mach/hurd/i386/tls.h
index 4c78685045..6cd1730887 100644
--- a/sysdeps/mach/hurd/i386/tls.h
+++ b/sysdeps/mach/hurd/i386/tls.h
@@ -43,9 +43,27 @@ typedef struct
   void *__private_tm[4];
   /* GCC split stack support.  */
   void *__private_ss;
+
+  /* Keep this field last, so fields above can continue being compatible with
+     the Linux version. */
+  mach_port_t reply_port;      /* This thread's reply port.  */
+  struct hurd_sigstate *_hurd_sigstate;
 } tcbhead_t;
 #endif
 
+/* Return tcbhead_t from a TLS segment descriptor.  */
+# define HURD_DESC_TLS(desc)						      \
+  ({									      \
+   (tcbhead_t *) (   (desc->low_word >> 16)				      \
+                  | ((desc->high_word & 0xff) << 16)			      \
+                  |  (desc->high_word & 0xff000000));			      \
+  })
+
+/* Return 1 if TLS is not initialized yet.  */
+#define __LIBC_NO_TLS()							      \
+  ({ unsigned short ds, gs;						      \
+     asm ("movw %%ds,%w0; movw %%gs,%w1" : "=q" (ds), "=q" (gs));	      \
+     __builtin_expect(ds == gs, 0); })
 
 /* The TCB can have any size and the memory following the address the
    thread pointer points to is unspecified.  Allocate the TCB there.  */
@@ -138,6 +156,21 @@ _hurd_tls_init (tcbhead_t *tcb)
 	      : "i" (offsetof (tcbhead_t, tcb)));			      \
      __tcb;})
 
+/* Return the TCB address of a thread given its state.
+   Note: this is expensive.  */
+# define THREAD_TCB(thread, thread_state)				      \
+  ({ int __sel = (thread_state)->basic.gs;				      \
+     struct descriptor __desc, *___desc = &__desc;			      \
+     unsigned int __count = 1;						      \
+     kern_return_t __err;						      \
+     if (HURD_SEL_LDT(__sel))						      \
+       __err = __i386_get_ldt ((thread), __sel, 1, &___desc, &__count);	      \
+     else								      \
+       __err = __i386_get_gdt ((thread), __sel, &__desc);		      \
+     assert_perror (__err);						      \
+     assert (__count == 1);						      \
+     HURD_DESC_TLS(___desc);})
+
 /* Install new dtv for current thread.  */
 # define INSTALL_NEW_DTV(dtvp)						      \
   ({ asm volatile ("movl %0,%%gs:%P1"					      \
diff --git a/sysdeps/mach/hurd/i386/trampoline.c b/sysdeps/mach/hurd/i386/trampoline.c
index 0c68759f42..0e795f9389 100644
--- a/sysdeps/mach/hurd/i386/trampoline.c
+++ b/sysdeps/mach/hurd/i386/trampoline.c
@@ -79,8 +79,6 @@ _hurd_setup_sighandler (struct hurd_sigstate *ss, __sighandler_t handler,
     {
       sigsp = ss->sigaltstack.ss_sp + ss->sigaltstack.ss_size;
       ss->sigaltstack.ss_flags |= SS_ONSTACK;
-      /* XXX need to set up base of new stack for
-	 per-thread variables, cthreads.  */
     }
   /* This code has intimate knowledge of the special mach_msg system call
      done in intr-msg.c; that code does (see intr-msg.h):