summaryrefslogtreecommitdiffstats
path: root/lib/libthr/arch/i386
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libthr/arch/i386')
-rw-r--r--lib/libthr/arch/i386/Makefile.inc4
-rw-r--r--lib/libthr/arch/i386/i386/_curthread.S17
-rw-r--r--lib/libthr/arch/i386/i386/_setcurthread.c136
-rw-r--r--lib/libthr/arch/i386/i386/pthread_md.c84
-rw-r--r--lib/libthr/arch/i386/include/pthread_md.h116
5 files changed, 202 insertions, 155 deletions
diff --git a/lib/libthr/arch/i386/Makefile.inc b/lib/libthr/arch/i386/Makefile.inc
index 0c86284..508d2b4 100644
--- a/lib/libthr/arch/i386/Makefile.inc
+++ b/lib/libthr/arch/i386/Makefile.inc
@@ -1,5 +1,5 @@
# $FreeBSD$
-.PATH: ${.CURDIR}/sys ${.CURDIR}/arch/${MACHINE_ARCH}/${MACHINE_ARCH}
+.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/${MACHINE_ARCH}
-SRCS+= _setcurthread.c _curthread.S
+SRCS+= pthread_md.c
diff --git a/lib/libthr/arch/i386/i386/_curthread.S b/lib/libthr/arch/i386/i386/_curthread.S
deleted file mode 100644
index eb658df..0000000
--- a/lib/libthr/arch/i386/i386/_curthread.S
+++ /dev/null
@@ -1,17 +0,0 @@
-/* $FreeBSD$ */
-
-#include <machine/asm.h>
-
-ENTRY(_get_curthread)
- cmpl $0, _thread_initial
- je nothreads
- movl %gs:8, %eax
- ret
-nothreads:
- xor %eax, %eax
- ret
-
-ENTRY(_set_gs)
- movl 4(%esp), %eax
- movl %eax, %gs
- ret
diff --git a/lib/libthr/arch/i386/i386/_setcurthread.c b/lib/libthr/arch/i386/i386/_setcurthread.c
deleted file mode 100644
index d91c64f..0000000
--- a/lib/libthr/arch/i386/i386/_setcurthread.c
+++ /dev/null
@@ -1,136 +0,0 @@
-/*
- * Copyright (c) 2003, Jeffrey Roberson <jeff@freebsd.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice unmodified, this list of conditions, and the following
- * disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * $FreeBSD$
- */
-
-#include <sys/types.h>
-#include <sys/ucontext.h>
-
-#include <pthread.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <machine/sysarch.h>
-#include <machine/segments.h>
-
-#include "thr_private.h"
-#include "rtld_tls.h"
-
-/* in _curthread.S */
-extern void _set_gs(int);
-
-struct tcb {
- struct tcb *tcb_self; /* required by rtld */
- void *tcb_dtv; /* required by rtld */
- struct pthread *tcb_thread;
- int tcb_ldt;
-};
-
-void
-_retire_thread(void *entry)
-{
- struct tcb *tcb = (struct tcb *)entry;
-
- i386_set_ldt(tcb->tcb_ldt, NULL, 1);
- _rtld_free_tls(tcb, sizeof(struct tcb), 16);
-}
-
-void *
-_set_curthread(ucontext_t *uc, struct pthread *thr, int *err)
-{
-#ifndef COMPAT_32BIT
- union descriptor desc;
-#endif
- struct tcb *tcb;
- void *oldtls;
-#ifndef COMPAT_32BIT
- int ldt_index;
-#endif
-
- *err = 0;
-
- if (uc == NULL && thr->arch_id != NULL) {
-#ifdef COMPAT_32BIT
- _amd64_set_gsbase(thr->arch_id);
-#endif
- return (thr->arch_id);
- }
-
- if (uc == NULL) {
- __asm __volatile("movl %%gs:0, %0" : "=r" (oldtls));
- } else {
- oldtls = NULL;
- }
-
- /*
- * Allocate and initialise a new TLS block with enough extra
- * space for our self pointer.
- */
- tcb = _rtld_allocate_tls(oldtls, sizeof(struct tcb), 16);
-
- /*
- * Cache the address of the thread structure here, after
- * rtld's two words of private space.
- */
- tcb->tcb_thread = thr;
-
-#ifndef COMPAT_32BIT
- bzero(&desc, sizeof(desc));
-
- /*
- * Set up the descriptor to point at the TLS block.
- */
- desc.sd.sd_lolimit = 0xFFFF;
- desc.sd.sd_lobase = (unsigned int)tcb & 0xFFFFFF;
- desc.sd.sd_type = SDT_MEMRW;
- desc.sd.sd_dpl = SEL_UPL;
- desc.sd.sd_p = 1;
- desc.sd.sd_hilimit = 0xF;
- desc.sd.sd_xx = 0;
- desc.sd.sd_def32 = 1;
- desc.sd.sd_gran = 1;
- desc.sd.sd_hibase = (unsigned int)tcb >> 24;
-
- /* Get a slot from the process' LDT list */
- ldt_index = i386_set_ldt(LDT_AUTO_ALLOC, &desc, 1);
- if (ldt_index == -1)
- abort();
- tcb->tcb_ldt = ldt_index;
- /*
- * Set up our gs with the index into the ldt for this entry.
- */
- if (uc != NULL)
- uc->uc_mcontext.mc_gs = LSEL(ldt_index, SEL_UPL);
- else
- _set_gs(LSEL(ldt_index, SEL_UPL));
-#else
- if (uc == NULL)
- _amd64_set_gsbase(tcb);
-#endif
-
- return (tcb);
-}
diff --git a/lib/libthr/arch/i386/i386/pthread_md.c b/lib/libthr/arch/i386/i386/pthread_md.c
new file mode 100644
index 0000000..e0adb25
--- /dev/null
+++ b/lib/libthr/arch/i386/i386/pthread_md.c
@@ -0,0 +1,84 @@
+/*-
+ * Copyright (C) 2003 David Xu <davidxu@freebsd.org>
+ * Copyright (c) 2001,2003 Daniel Eischen <deischen@freebsd.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Neither the name of the author nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/types.h>
+#include <machine/segments.h>
+#include <machine/sysarch.h>
+#include <string.h>
+#include <rtld_tls.h>
+
+#include "pthread_md.h"
+
+struct tcb *
+_tcb_ctor(struct pthread *thread, int initial)
+{
+#ifndef COMPAT_32BIT
+ union descriptor ldt;
+#endif
+ struct tcb *tcb;
+ void *oldtls;
+
+ if (initial)
+ __asm __volatile("movl %%gs:0, %0" : "=r" (oldtls));
+ else
+ oldtls = NULL;
+
+ tcb = _rtld_allocate_tls(oldtls, sizeof(struct tcb), 16);
+ if (tcb) {
+ tcb->tcb_thread = thread;
+#ifndef COMPAT_32BIT
+ ldt.sd.sd_hibase = (unsigned int)tcb >> 24;
+ ldt.sd.sd_lobase = (unsigned int)tcb & 0xFFFFFF;
+ ldt.sd.sd_hilimit = (sizeof(struct tcb) >> 16) & 0xF;
+ ldt.sd.sd_lolimit = sizeof(struct tcb) & 0xFFFF;
+ ldt.sd.sd_type = SDT_MEMRWA;
+ ldt.sd.sd_dpl = SEL_UPL;
+ ldt.sd.sd_p = 1;
+ ldt.sd.sd_xx = 0;
+ ldt.sd.sd_def32 = 1;
+ ldt.sd.sd_gran = 0; /* no more than 1M */
+ tcb->tcb_ldt = i386_set_ldt(LDT_AUTO_ALLOC, &ldt, 1);
+ if (tcb->tcb_ldt < 0) {
+ _rtld_free_tls(tcb, sizeof(struct tcb), 16);
+ tcb = NULL;
+ }
+#endif
+ }
+ return (tcb);
+}
+
+void
+_tcb_dtor(struct tcb *tcb)
+{
+#ifndef COMPAT_32BIT
+ if (tcb->tcb_ldt >= 0)
+ i386_set_ldt(tcb->tcb_ldt, NULL, 1);
+#endif
+ _rtld_free_tls(tcb, sizeof(struct tcb), 16);
+}
diff --git a/lib/libthr/arch/i386/include/pthread_md.h b/lib/libthr/arch/i386/include/pthread_md.h
new file mode 100644
index 0000000..721ddac
--- /dev/null
+++ b/lib/libthr/arch/i386/include/pthread_md.h
@@ -0,0 +1,116 @@
+/*-
+ * Copyright (c) 2002 Daniel Eischen <deischen@freebsd.org>.
+ * Copyright (c) 2005 David Xu <davidxu@freebsd.org>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+/*
+ * Machine-dependent thread prototypes/definitions.
+ */
+#ifndef _PTHREAD_MD_H_
+#define _PTHREAD_MD_H_
+
+#include <stddef.h>
+#include <sys/types.h>
+#include <machine/sysarch.h>
+
+#define DTV_OFFSET offsetof(struct tcb, tcb_dtv)
+
+/*
+ * Variant II tcb, first two members are required by rtld,
+ * %gs points to the structure.
+ */
+struct tcb {
+ struct tcb *tcb_self; /* required by rtld */
+ void *tcb_dtv; /* required by rtld */
+ struct pthread *tcb_thread;
+ int tcb_ldt;
+};
+
+/*
+ * Evaluates to the byte offset of the per-tcb variable name.
+ */
+#define __tcb_offset(name) __offsetof(struct tcb, name)
+
+/*
+ * Evaluates to the type of the per-tcb variable name.
+ */
+#define __tcb_type(name) __typeof(((struct tcb *)0)->name)
+
+/*
+ * Evaluates to the value of the per-tcb variable name.
+ */
+#define TCB_GET32(name) ({ \
+ __tcb_type(name) __result; \
+ \
+ u_int __i; \
+ __asm __volatile("movl %%gs:%1, %0" \
+ : "=r" (__i) \
+ : "m" (*(u_int *)(__tcb_offset(name)))); \
+ __result = (__tcb_type(name))__i; \
+ \
+ __result; \
+})
+
+/*
+ * The constructors.
+ */
+struct tcb *_tcb_ctor(struct pthread *, int);
+void _tcb_dtor(struct tcb *tcb);
+
+/* Called from the thread to set its private data. */
+static __inline void
+_tcb_set(struct tcb *tcb)
+{
+#ifndef COMPAT_32BIT
+ int val;
+
+ val = (tcb->tcb_ldt << 3) | 7;
+ __asm __volatile("movl %0, %%gs" : : "r" (val));
+#else
+ _amd64_set_gsbase(tcb);
+#endif
+
+}
+
+/* Get the current kcb. */
+static __inline struct tcb *
+_tcb_get(void)
+{
+ return (TCB_GET32(tcb_self));
+}
+
+extern struct pthread *_thr_initial;
+
+/* Get the current thread. */
+static __inline struct pthread *
+_get_curthread(void)
+{
+ if (_thr_initial)
+ return (TCB_GET32(tcb_thread));
+ return (NULL);
+}
+#endif
OpenPOWER on IntegriCloud