about summary refs log tree commit diff
path: root/sysdeps/mach/hurd/i386/htl
diff options
context:
space:
mode:
authorSamuel Thibault <samuel.thibault@ens-lyon.org>2018-04-02 01:43:22 +0200
committerSamuel Thibault <samuel.thibault@ens-lyon.org>2018-04-02 01:44:14 +0200
commit33574c17eefcf326c1cd30eb9f915ad26d3d9ef2 (patch)
tree2c1b143f2b7a0ceba9fba000b70dd2889d56e60f /sysdeps/mach/hurd/i386/htl
parent03e2aa50fd512449025bba8c244d16338d8526a4 (diff)
downloadglibc-33574c17eefcf326c1cd30eb9f915ad26d3d9ef2.tar.gz
glibc-33574c17eefcf326c1cd30eb9f915ad26d3d9ef2.tar.xz
glibc-33574c17eefcf326c1cd30eb9f915ad26d3d9ef2.zip
hurd: Add hurd thread library
Contributed by

Agustina Arzille <avarzille@riseup.net>
Amos Jeffries <squid3@treenet.co.nz>
David Michael <fedora.dm0@gmail.com>
Marco Gerards <marco@gnu.org>
Marcus Brinkmann <marcus@gnu.org>
Neal H. Walfield <neal@gnu.org>
Pino Toscano <toscano.pino@tiscali.it>
Richard Braun <rbraun@sceen.net>
Roland McGrath <roland@gnu.org>
Samuel Thibault <samuel.thibault@ens-lyon.org>
Thomas DiModica <ricinwich@yahoo.com>
Thomas Schwinge <tschwinge@gnu.org>

	* htl: New directory.
	* sysdeps/htl: New directory.
	* sysdeps/hurd/htl: New directory.
	* sysdeps/i386/htl: New directory.
	* sysdeps/mach/htl: New directory.
	* sysdeps/mach/hurd/htl: New directory.
	* sysdeps/mach/hurd/i386/htl: New directory.
	* nscd/Depend, resolv/Depend, rt/Depend: Add htl dependency.
	* sysdeps/mach/hurd/i386/Implies: Add mach/hurd/i386/htl imply.
	* sysdeps/mach/hurd/i386/libpthread.abilist: New file.
Diffstat (limited to 'sysdeps/mach/hurd/i386/htl')
-rw-r--r--sysdeps/mach/hurd/i386/htl/Implies2
-rw-r--r--sysdeps/mach/hurd/i386/htl/pt-machdep.c82
-rw-r--r--sysdeps/mach/hurd/i386/htl/pt-setup.c110
3 files changed, 194 insertions, 0 deletions
diff --git a/sysdeps/mach/hurd/i386/htl/Implies b/sysdeps/mach/hurd/i386/htl/Implies
new file mode 100644
index 0000000000..7a0f99d772
--- /dev/null
+++ b/sysdeps/mach/hurd/i386/htl/Implies
@@ -0,0 +1,2 @@
+mach/hurd/htl
+i386/htl
diff --git a/sysdeps/mach/hurd/i386/htl/pt-machdep.c b/sysdeps/mach/hurd/i386/htl/pt-machdep.c
new file mode 100644
index 0000000000..9b2083d2da
--- /dev/null
+++ b/sysdeps/mach/hurd/i386/htl/pt-machdep.c
@@ -0,0 +1,82 @@
+/* Machine dependent pthreads code.  Hurd/i386 version.
+   Copyright (C) 2000-2018 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If
+   not, see <http://www.gnu.org/licenses/>.  */
+
+#include <errno.h>
+
+#include <mach.h>
+#include <mach/i386/thread_status.h>
+#include <mach/i386/mach_i386.h>
+#include <mach/mig_errors.h>
+#include <mach/thread_status.h>
+
+#define HURD_TLS_DESC_DECL(desc, tcb)					      \
+  struct descriptor desc =						      \
+    {				/* low word: */				      \
+      0xffff			/* limit 0..15 */			      \
+      | (((unsigned int) (tcb)) << 16) /* base 0..15 */			      \
+      ,				/* high word: */			      \
+      ((((unsigned int) (tcb)) >> 16) & 0xff) /* base 16..23 */		      \
+      | ((0x12 | 0x60 | 0x80) << 8) /* access = ACC_DATA_W|ACC_PL_U|ACC_P */  \
+      | (0xf << 16)		/* limit 16..19 */			      \
+      | ((4 | 8) << 20)		/* granularity = SZ_32|SZ_G */		      \
+      | (((unsigned int) (tcb)) & 0xff000000) /* base 24..31 */		      \
+    }
+
+int
+__thread_set_pcsptp (thread_t thread,
+		     int set_ip, void *ip,
+		     int set_sp, void *sp,
+		     int set_tp, void *tp)
+{
+  error_t err;
+  struct i386_thread_state state;
+  mach_msg_type_number_t state_count;
+
+  state_count = i386_THREAD_STATE_COUNT;
+
+  err = __thread_get_state (thread, i386_REGS_SEGS_STATE,
+			    (thread_state_t) &state, &state_count);
+  if (err)
+    return err;
+
+  if (set_sp)
+    state.uesp = (unsigned int) sp;
+  if (set_ip)
+    state.eip = (unsigned int) ip;
+  if (set_tp)
+    {
+      HURD_TLS_DESC_DECL (desc, tp);
+      int sel;
+
+    asm ("mov %%gs, %w0": "=q" (sel):"0" (0));
+      if (__builtin_expect (sel, 0x48) & 4)	/* LDT selector */
+	err = __i386_set_ldt (thread, sel, &desc, 1);
+      else
+	err = __i386_set_gdt (thread, &sel, desc);
+      if (err)
+	return err;
+      state.gs = sel;
+    }
+
+  err = __thread_set_state (thread, i386_REGS_SEGS_STATE,
+			    (thread_state_t) &state, i386_THREAD_STATE_COUNT);
+  if (err)
+    return err;
+
+  return 0;
+}
diff --git a/sysdeps/mach/hurd/i386/htl/pt-setup.c b/sysdeps/mach/hurd/i386/htl/pt-setup.c
new file mode 100644
index 0000000000..a59f71b7be
--- /dev/null
+++ b/sysdeps/mach/hurd/i386/htl/pt-setup.c
@@ -0,0 +1,110 @@
+/* Setup thread stack.  Hurd/i386 version.
+   Copyright (C) 2000-2018 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If
+   not, see <http://www.gnu.org/licenses/>.  */
+
+#include <stdint.h>
+#include <assert.h>
+#include <mach.h>
+
+#include <pt-internal.h>
+
+/* The stack layout used on the i386 is:
+
+    -----------------
+   |  ARG            |
+    -----------------
+   |  START_ROUTINE  |
+    -----------------
+   |  0              |
+    -----------------
+ */
+
+/* Set up the stack for THREAD, such that it appears as if
+   START_ROUTINE and ARG were passed to the new thread's entry-point.
+   Return the stack pointer for the new thread.  */
+static void *
+stack_setup (struct __pthread *thread,
+	     void *(*start_routine) (void *), void *arg)
+{
+  error_t err;
+  uintptr_t *bottom, *top;
+
+  /* Calculate the top of the new stack.  */
+  bottom = thread->stackaddr;
+  top = (uintptr_t *) ((uintptr_t) bottom + thread->stacksize
+		       + ((thread->guardsize + __vm_page_size - 1)
+			  / __vm_page_size) * __vm_page_size);
+
+  if (start_routine != NULL)
+    {
+      /* And then the call frame.  */
+      top -= 3;
+      top = (uintptr_t *) ((uintptr_t) top & ~0xf);
+      top[2] = (uintptr_t) arg;	/* Argument to START_ROUTINE.  */
+      top[1] = (uintptr_t) start_routine;
+      top[0] = (uintptr_t) thread;
+      *--top = 0;		/* Fake return address.  */
+    }
+
+  if (thread->guardsize)
+    {
+      err = __vm_protect (__mach_task_self (), (vm_address_t) bottom,
+			  thread->guardsize, 0, 0);
+      assert_perror (err);
+    }
+
+  return top;
+}
+
+int
+__pthread_setup (struct __pthread *thread,
+		 void (*entry_point) (struct __pthread *, void *(*)(void *),
+				      void *), void *(*start_routine) (void *),
+		 void *arg)
+{
+  tcbhead_t *tcb;
+  error_t err;
+  mach_port_t ktid;
+
+  thread->mcontext.pc = entry_point;
+  thread->mcontext.sp = stack_setup (thread, start_routine, arg);
+
+  ktid = __mach_thread_self ();
+  if (thread->kernel_thread == ktid)
+    /* Fix up the TCB for the main thread.  The C library has already
+       installed a TCB, which we want to keep using.  This TCB must not
+       be freed so don't register it in the thread structure.  On the
+       other hand, it's not yet possible to reliably release a TCB.
+       Leave the unused one registered so that it doesn't leak.  The
+       only thing left to do is to correctly set the `self' member in
+       the already existing TCB.  */
+    tcb = THREAD_SELF;
+  else
+    {
+      err = __thread_set_pcsptp (thread->kernel_thread,
+				 1, thread->mcontext.pc,
+				 1, thread->mcontext.sp,
+				 1, thread->tcb);
+      assert_perror (err);
+      tcb = thread->tcb;
+    }
+  __mach_port_deallocate (__mach_task_self (), ktid);
+
+  tcb->self = thread->kernel_thread;
+
+  return 0;
+}