From 9fe0b6bde601877afbe7ac644d00c3bd6a263502 Mon Sep 17 00:00:00 2001 From: deischen Date: Fri, 18 Apr 2003 05:00:52 +0000 Subject: [PATCH] Add architecture dependent atomic ops (atomic_swap only), KSE specific data, and userland versions of [gs]etcontext(). Modify the UTS entry and exit functions to account of FPU validity and format. --- lib/libkse/arch/i386/i386/thr_enter_uts.S | 8 +- lib/libkse/arch/i386/i386/thr_getcontext.S | 157 +++++++++++++++++ lib/libkse/arch/i386/include/atomic_ops.h | 51 ++++++ lib/libkse/arch/i386/include/pthread_md.h | 54 ++++++ lib/libpthread/arch/i386/i386/ksd.c | 166 ++++++++++++++++++ lib/libpthread/arch/i386/i386/thr_enter_uts.S | 8 +- .../arch/i386/i386/thr_getcontext.S | 157 +++++++++++++++++ lib/libpthread/arch/i386/i386/thr_switch.S | 32 +++- lib/libpthread/arch/i386/include/atomic_ops.h | 51 ++++++ lib/libpthread/arch/i386/include/ksd.h | 144 +++++++++++++++ lib/libpthread/arch/i386/include/pthread_md.h | 54 ++++++ 11 files changed, 870 insertions(+), 12 deletions(-) create mode 100644 lib/libkse/arch/i386/i386/thr_getcontext.S create mode 100644 lib/libkse/arch/i386/include/atomic_ops.h create mode 100644 lib/libkse/arch/i386/include/pthread_md.h create mode 100644 lib/libpthread/arch/i386/i386/ksd.c create mode 100644 lib/libpthread/arch/i386/i386/thr_getcontext.S create mode 100644 lib/libpthread/arch/i386/include/atomic_ops.h create mode 100644 lib/libpthread/arch/i386/include/ksd.h create mode 100644 lib/libpthread/arch/i386/include/pthread_md.h diff --git a/lib/libkse/arch/i386/i386/thr_enter_uts.S b/lib/libkse/arch/i386/i386/thr_enter_uts.S index 10edd3fabfe6..a617b72eccc6 100644 --- a/lib/libkse/arch/i386/i386/thr_enter_uts.S +++ b/lib/libkse/arch/i386/i386/thr_enter_uts.S @@ -35,7 +35,10 @@ __FBSDID("$FreeBSD$"); #define UC_MC_OFFSET 16 /* offset to mcontext from ucontext */ #define MC_LEN_OFFSET 80 /* offset to mc_len from mcontext */ #define MC_FP_CW_OFFSET 96 /* offset to FP control word */ +#define MC_FPFMT_OFFSET 84 /* offset to mc_fpformat from mcontext */ +#define MC_FPFMT_NODEV 0x10000 #define MC_OWNEDFP_OFFSET 88 /* offset to mc_ownedfp from mcontext */ +#define MC_OWNEDFP_NONE 0x20000 #define KM_STACK_SP_OFFSET 36 /* offset to km_stack.ss_sp */ #define KM_STACK_SIZE_OFFSET 40 /* offset to km_stack.ss_sp */ #define KM_FUNC_OFFSET 32 /* offset to km_func */ @@ -54,7 +57,7 @@ ENTRY(_thread_enter_uts) 1: pushl %edx /* save value of edx */ movl %eax, %edx /* get address of context */ addl $UC_MC_OFFSET, %edx /* add offset to mcontext */ - movl %gs, 4(%edx) + /*movl %gs, 4(%edx)*/ /* we don't touch %gs */ movl %fs, 8(%edx) movl %es, 12(%edx) movl %ds, 16(%edx) @@ -78,7 +81,8 @@ ENTRY(_thread_enter_uts) * have floating point registers saved by the kernel. */ fnstcw MC_FP_CW_OFFSET(%edx) - movl $0, MC_OWNEDFP_OFFSET(%edx) /* no FP */ + movl $MC_OWNEDFP_NONE, MC_OWNEDFP_OFFSET(%edx) /* no FP */ + movl $MC_FPFMT_NODEV, MC_FPFMT_OFFSET(%edx) /* no FP */ pushfl /* get eflags */ popl %eax movl %eax, 68(%edx) /* store eflags */ diff --git a/lib/libkse/arch/i386/i386/thr_getcontext.S b/lib/libkse/arch/i386/i386/thr_getcontext.S new file mode 100644 index 000000000000..217e89361e6a --- /dev/null +++ b/lib/libkse/arch/i386/i386/thr_getcontext.S @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2001 Daniel Eischen . + * 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 DANIEL EISCHEN 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. + */ + +#include +__FBSDID("$FreeBSD$"); + +/* + * Where do we define these? + */ +#define UC_MC_OFFSET 16 /* offset to mcontext from ucontext */ +#define MC_LEN_OFFSET 80 /* offset to mc_len from mcontext */ +#define MC_LEN 640 /* mc_len */ +#define MC_FPFMT_OFFSET 84 +#define MC_FPFMT_NODEV 0x10000 +#define MC_FPFMT_387 0x10001 +#define MC_FPFMT_XMM 0x10002 +#define MC_OWNEDFP_OFFSET 88 +#define MC_OWNEDFP_NONE 0x20000 +#define MC_OWNEDFP_FPU 0x20001 +#define MC_OWNEDFP_PCB 0x20002 +#define MC_FPREGS_OFFSET 96 /* offset to FP regs from mcontext */ +#define MC_FP_CW_OFFSET 96 /* offset to FP control word */ + +/* + * int thr_setcontext(ucontext_t *ucp) + * + * Restores the context in ucp. + * + * Returns 0 if there are no errors; -1 otherwise + */ + .weak CNAME(_thr_setcontext) + .set CNAME(_thr_setcontext),CNAME(__thr_setcontext) +ENTRY(__thr_setcontext) + movl 4(%esp), %eax /* get address of context and sigset */ + cmpl $0, %eax /* check for null pointer */ + jne 1f + movl $-1, %eax + jmp 7f +1: addl $UC_MC_OFFSET, %eax /* add offset to mcontext */ + cmpl $MC_LEN, MC_LEN_OFFSET(%eax) /* is context valid? */ + je 2f + movl $-1, %eax /* bzzzt, invalid context */ + jmp 7f +/*2: movl 4(%edx), %gs*/ /* we don't touch %gs */ +2: movl 8(%edx), %fs + movl 12(%edx), %es + movl 16(%edx), %ds + movl 76(%edx), %ss + movl 20(%edx), %edi + movl 24(%edx), %esi + movl 28(%edx), %ebp + movl %esp, %ecx /* save current stack in ecx */ + movl 72(%edx), %esp /* switch to context defined stack */ + movl 60(%edx), %eax /* put return address at top of stack */ + pushl %eax + movl 44(%edx), %eax /* get ecx from context, */ + pushl %eax /* push on top of stack */ + movl 48(%edx), %eax /* get eax from context, */ + pushl %eax /* push on top of stack */ + /* + * if (mc_fpowned == MC_OWNEDFP_FPU || mc_fpowned == MC_OWNEDFP_PCB) { + * if (mc_fpformat == MC_FPFMT_387) + * restore 387 FP register format + * else if (mc_fpformat == MC_FPFMT_XMM) + * restore XMM/SSE FP register format + * } + */ + cmpl $MC_OWNEDFP_FPU, MC_OWNEDFP_OFFSET(%edx) + je 3f + cmpl $MC_OWNEDFP_PCB, MC_OWNEDFP_OFFSET(%edx) + jne 5f +3: cmpl $MC_FPFMT_387, MC_FPFMT_OFFSET(%edx) + jne 5f + frstor MC_FPREGS_OFFSET(%edx) /* restore 387 FP regs */ + jmp 5f +4: cmpl $MC_FPFMT_XMM, MC_FPFMT_OFFSET(%edx) + jne 5f + fxrstor MC_FPREGS_OFFSET(%edx) /* restore XMM FP regs */ + jmp 6f +5: fninit + fldcw MC_FP_CW_OFFSET(%edx) +6: pushl 68(%edx) /* restore flags register */ + popf + movl 36(%edx), %ebx /* restore ebx and edx */ + movl 40(%edx), %edx + popl %eax /* restore eax and ecx last */ + popl %ecx +7: ret + +/* + * int thr_getcontext(ucontext_t *ucp); + * + * Returns 0 if there are no errors; -1 otherwise + */ + .weak CNAME(_thr_getcontext) + .set CNAME(_thr_getcontext),CNAME(__thr_getcontext) +ENTRY(__thr_getcontext) + movl 4(%esp), %eax /* get address of context */ + cmpl $0, %eax /* check for null pointer */ + jne 1f + movl $-1, %eax + jmp 2f + movl 4(%esp), %eax /* get address of context and sigset */ +1: pushl %edx /* save value of edx */ + movl 8(%esp), %edx /* get address of context */ + addl $UC_MC_OFFSET, %edx /* add offset to mcontext */ + /*movl %gs, 4(%edx)*/ /* we don't touch %gs */ + movl %fs, 8(%edx) + movl %es, 12(%edx) + movl %ds, 16(%edx) + movl %ss, 76(%edx) + movl %edi, 20(%edx) + movl %esi, 24(%edx) + movl %ebp, 28(%edx) + movl %ebx, 36(%edx) + movl $0, 48(%edx) /* store successful return in eax */ + popl %eax /* get saved value of edx */ + movl %eax, 40(%edx) /* save edx */ + movl %ecx, 44(%edx) + movl (%esp), %eax /* get return address */ + movl %eax, 60(%edx) /* save return address */ + fnstcw MC_FP_CW_OFFSET(%edx) + movl $MC_LEN, MC_LEN_OFFSET(%edx) + movl $MC_FPFMT_NODEV, MC_FPFMT_OFFSET(%edx) /* no FP */ + movl $MC_OWNEDFP_NONE, MC_OWNEDFP_OFFSET(%edx) /* no FP */ + pushfl + popl %eax /* get eflags */ + movl %eax, 68(%edx) /* store eflags */ + movl %esp, %eax /* setcontext pushes the return */ + addl $4, %eax /* address onto the top of the */ + movl %eax, 72(%edx) /* stack; account for this */ + movl 40(%edx), %edx /* restore edx -- is this needed? */ + xorl %eax, %eax /* return 0 */ +2: ret diff --git a/lib/libkse/arch/i386/include/atomic_ops.h b/lib/libkse/arch/i386/include/atomic_ops.h new file mode 100644 index 000000000000..4d84ae574676 --- /dev/null +++ b/lib/libkse/arch/i386/include/atomic_ops.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2001 Daniel Eischen + * 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$ + */ + +#ifndef _ATOMIC_OPS_H_ +#define _ATOMIC_OPS_H_ + +/* + * Atomic swap: + * Atomic (tmp = *dst, *dst = val), then *res = tmp + * + * void atomic_swap_long(long *dst, long val, long *res); + */ +static inline void +atomic_swap_long(long *dst, long val, long *res) +{ + __asm __volatile( + "xchgl %2, %1; movl %2, %0" + : "=m" (*res) : "m" (*dst), "r" (val) : "memory"); +} + +#define atomic_swap_int(d, v, r) \ + atomic_swap_long((long *)(d), (long)(v), (long *)(r)) + +#define atomic_swap_ptr atomic_swap_int + +#endif diff --git a/lib/libkse/arch/i386/include/pthread_md.h b/lib/libkse/arch/i386/include/pthread_md.h new file mode 100644 index 000000000000..ae00759aa9ce --- /dev/null +++ b/lib/libkse/arch/i386/include/pthread_md.h @@ -0,0 +1,54 @@ +/*- + * Copyright (c) 2002 Daniel Eischen . + * 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 for the thread kernel. + */ +#ifndef _PTHREAD_MD_H_ +#define _PTHREAD_MD_H_ + +#include +#include +#include + +extern int _thread_enter_uts(struct kse_thr_mailbox *, struct kse_mailbox *); +extern int _thread_switch(struct kse_thr_mailbox *, struct kse_thr_mailbox **); +extern int _thr_setcontext(ucontext_t *); +extern int _thr_getcontext(ucontext_t *); + +/* + * These are needed to ensure an application doesn't attempt to jump + * between stacks of different threads. They return the stack of + * jmp_buf, sigjmp_buf, and ucontext respectively. + */ +#define GET_STACK_JB(jb) ((unsigned long)((jb)[0]._jb[2])) +#define GET_STACK_SJB(sjb) ((unsigned long)((sjb)[0]._sjb[2])) +#define GET_STACK_UC(ucp) ((unsigned long)((ucp)->uc_mcontext.mc_esp)) + +#define THR_GETCONTEXT(ucp) _thr_getcontext(ucp) +#define THR_SETCONTEXT(ucp) _thr_setcontext(ucp) +#endif diff --git a/lib/libpthread/arch/i386/i386/ksd.c b/lib/libpthread/arch/i386/i386/ksd.c new file mode 100644 index 000000000000..e1adde3be94a --- /dev/null +++ b/lib/libpthread/arch/i386/i386/ksd.c @@ -0,0 +1,166 @@ +/*- + * Copyright (C) 2003 David Xu + * Copyright (c) 2001 Daniel Eischen + * 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. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "ksd.h" + +#define LDT_ENTRIES 8192 +#define LDT_WORDS (8192/sizeof(unsigned int)) +#define LDT_RESERVED NLDT + +static unsigned int ldt_mask[LDT_WORDS]; +static int initialized = 0; + +void +initialize(void) +{ + int i, j; + + memset(ldt_mask, 0xFF, sizeof(ldt_mask)); + /* Reserve system predefined LDT entries */ + for (i = 0; i < LDT_RESERVED; ++i) { + j = i / 32; + ldt_mask[j] &= ~(1 << (i % 32)); + } + initialized = 1; +} + +static u_int +alloc_ldt_entry(void) +{ + u_int i, j, index; + + index = 0; + for (i = 0; i < LDT_WORDS; ++i) { + if (ldt_mask[i] != 0) { + j = bsfl(ldt_mask[i]); + ldt_mask[i] &= ~(1 << j); + index = i * 32 + j; + break; + } + } + return (index); +} + +static void +free_ldt_entry(u_int index) +{ + u_int i, j; + + if (index < LDT_RESERVED || index >= LDT_ENTRIES) + return; + i = index / 32; + j = index % 32; + ldt_mask[i] |= (1 << j); +} + +/* + * Initialize KSD. This also includes setting up the LDT. + */ +int +_ksd_create(struct ksd *ksd, void *base, int size) +{ + union descriptor ldt; + + if (initialized == 0) + initialize(); + ksd->ldt = alloc_ldt_entry(); + if (ksd->ldt == 0) + return (-1); + ksd->base = base; + ksd->size = size; + ldt.sd.sd_hibase = (unsigned int)ksd->base >> 24; + ldt.sd.sd_lobase = (unsigned int)ksd->base & 0xFFFFFF; + ldt.sd.sd_hilimit = (size >> 16) & 0xF; + ldt.sd.sd_lolimit = ksd->size & 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 */ + if (i386_set_ldt(ksd->ldt, &ldt, 1) < 0) { + free_ldt_entry(ksd->ldt); + return (-1); + } + ksd->flags = KSDF_INITIALIZED; + return (0); +} + +void +_ksd_destroy(struct ksd *ksd) +{ + if ((ksd->flags & KSDF_INITIALIZED) != 0) { + free_ldt_entry(ksd->ldt); + } +} + +int +_ksd_getprivate(struct ksd *ksd, void **base, int *size) +{ + + if ((ksd == NULL) || ((ksd->flags & KSDF_INITIALIZED) == 0)) + return (-1); + else { + *base = ksd->base; + *size = ksd->size; + return (0); + } +} + +/* + * This assumes that the LDT is already setup. Just set %gs to + * reference it. + */ +int +_ksd_setprivate(struct ksd *ksd) +{ + int val; + int ret; + + if ((ksd->flags & KSDF_INITIALIZED) == 0) + ret = -1; + else { + val = (ksd->ldt << 3) | 7; + __asm __volatile("movl %0, %%gs" : : "r" (val)); + ret = 0; + } + return (ret); +} diff --git a/lib/libpthread/arch/i386/i386/thr_enter_uts.S b/lib/libpthread/arch/i386/i386/thr_enter_uts.S index 10edd3fabfe6..a617b72eccc6 100644 --- a/lib/libpthread/arch/i386/i386/thr_enter_uts.S +++ b/lib/libpthread/arch/i386/i386/thr_enter_uts.S @@ -35,7 +35,10 @@ __FBSDID("$FreeBSD$"); #define UC_MC_OFFSET 16 /* offset to mcontext from ucontext */ #define MC_LEN_OFFSET 80 /* offset to mc_len from mcontext */ #define MC_FP_CW_OFFSET 96 /* offset to FP control word */ +#define MC_FPFMT_OFFSET 84 /* offset to mc_fpformat from mcontext */ +#define MC_FPFMT_NODEV 0x10000 #define MC_OWNEDFP_OFFSET 88 /* offset to mc_ownedfp from mcontext */ +#define MC_OWNEDFP_NONE 0x20000 #define KM_STACK_SP_OFFSET 36 /* offset to km_stack.ss_sp */ #define KM_STACK_SIZE_OFFSET 40 /* offset to km_stack.ss_sp */ #define KM_FUNC_OFFSET 32 /* offset to km_func */ @@ -54,7 +57,7 @@ ENTRY(_thread_enter_uts) 1: pushl %edx /* save value of edx */ movl %eax, %edx /* get address of context */ addl $UC_MC_OFFSET, %edx /* add offset to mcontext */ - movl %gs, 4(%edx) + /*movl %gs, 4(%edx)*/ /* we don't touch %gs */ movl %fs, 8(%edx) movl %es, 12(%edx) movl %ds, 16(%edx) @@ -78,7 +81,8 @@ ENTRY(_thread_enter_uts) * have floating point registers saved by the kernel. */ fnstcw MC_FP_CW_OFFSET(%edx) - movl $0, MC_OWNEDFP_OFFSET(%edx) /* no FP */ + movl $MC_OWNEDFP_NONE, MC_OWNEDFP_OFFSET(%edx) /* no FP */ + movl $MC_FPFMT_NODEV, MC_FPFMT_OFFSET(%edx) /* no FP */ pushfl /* get eflags */ popl %eax movl %eax, 68(%edx) /* store eflags */ diff --git a/lib/libpthread/arch/i386/i386/thr_getcontext.S b/lib/libpthread/arch/i386/i386/thr_getcontext.S new file mode 100644 index 000000000000..217e89361e6a --- /dev/null +++ b/lib/libpthread/arch/i386/i386/thr_getcontext.S @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2001 Daniel Eischen . + * 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 DANIEL EISCHEN 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. + */ + +#include +__FBSDID("$FreeBSD$"); + +/* + * Where do we define these? + */ +#define UC_MC_OFFSET 16 /* offset to mcontext from ucontext */ +#define MC_LEN_OFFSET 80 /* offset to mc_len from mcontext */ +#define MC_LEN 640 /* mc_len */ +#define MC_FPFMT_OFFSET 84 +#define MC_FPFMT_NODEV 0x10000 +#define MC_FPFMT_387 0x10001 +#define MC_FPFMT_XMM 0x10002 +#define MC_OWNEDFP_OFFSET 88 +#define MC_OWNEDFP_NONE 0x20000 +#define MC_OWNEDFP_FPU 0x20001 +#define MC_OWNEDFP_PCB 0x20002 +#define MC_FPREGS_OFFSET 96 /* offset to FP regs from mcontext */ +#define MC_FP_CW_OFFSET 96 /* offset to FP control word */ + +/* + * int thr_setcontext(ucontext_t *ucp) + * + * Restores the context in ucp. + * + * Returns 0 if there are no errors; -1 otherwise + */ + .weak CNAME(_thr_setcontext) + .set CNAME(_thr_setcontext),CNAME(__thr_setcontext) +ENTRY(__thr_setcontext) + movl 4(%esp), %eax /* get address of context and sigset */ + cmpl $0, %eax /* check for null pointer */ + jne 1f + movl $-1, %eax + jmp 7f +1: addl $UC_MC_OFFSET, %eax /* add offset to mcontext */ + cmpl $MC_LEN, MC_LEN_OFFSET(%eax) /* is context valid? */ + je 2f + movl $-1, %eax /* bzzzt, invalid context */ + jmp 7f +/*2: movl 4(%edx), %gs*/ /* we don't touch %gs */ +2: movl 8(%edx), %fs + movl 12(%edx), %es + movl 16(%edx), %ds + movl 76(%edx), %ss + movl 20(%edx), %edi + movl 24(%edx), %esi + movl 28(%edx), %ebp + movl %esp, %ecx /* save current stack in ecx */ + movl 72(%edx), %esp /* switch to context defined stack */ + movl 60(%edx), %eax /* put return address at top of stack */ + pushl %eax + movl 44(%edx), %eax /* get ecx from context, */ + pushl %eax /* push on top of stack */ + movl 48(%edx), %eax /* get eax from context, */ + pushl %eax /* push on top of stack */ + /* + * if (mc_fpowned == MC_OWNEDFP_FPU || mc_fpowned == MC_OWNEDFP_PCB) { + * if (mc_fpformat == MC_FPFMT_387) + * restore 387 FP register format + * else if (mc_fpformat == MC_FPFMT_XMM) + * restore XMM/SSE FP register format + * } + */ + cmpl $MC_OWNEDFP_FPU, MC_OWNEDFP_OFFSET(%edx) + je 3f + cmpl $MC_OWNEDFP_PCB, MC_OWNEDFP_OFFSET(%edx) + jne 5f +3: cmpl $MC_FPFMT_387, MC_FPFMT_OFFSET(%edx) + jne 5f + frstor MC_FPREGS_OFFSET(%edx) /* restore 387 FP regs */ + jmp 5f +4: cmpl $MC_FPFMT_XMM, MC_FPFMT_OFFSET(%edx) + jne 5f + fxrstor MC_FPREGS_OFFSET(%edx) /* restore XMM FP regs */ + jmp 6f +5: fninit + fldcw MC_FP_CW_OFFSET(%edx) +6: pushl 68(%edx) /* restore flags register */ + popf + movl 36(%edx), %ebx /* restore ebx and edx */ + movl 40(%edx), %edx + popl %eax /* restore eax and ecx last */ + popl %ecx +7: ret + +/* + * int thr_getcontext(ucontext_t *ucp); + * + * Returns 0 if there are no errors; -1 otherwise + */ + .weak CNAME(_thr_getcontext) + .set CNAME(_thr_getcontext),CNAME(__thr_getcontext) +ENTRY(__thr_getcontext) + movl 4(%esp), %eax /* get address of context */ + cmpl $0, %eax /* check for null pointer */ + jne 1f + movl $-1, %eax + jmp 2f + movl 4(%esp), %eax /* get address of context and sigset */ +1: pushl %edx /* save value of edx */ + movl 8(%esp), %edx /* get address of context */ + addl $UC_MC_OFFSET, %edx /* add offset to mcontext */ + /*movl %gs, 4(%edx)*/ /* we don't touch %gs */ + movl %fs, 8(%edx) + movl %es, 12(%edx) + movl %ds, 16(%edx) + movl %ss, 76(%edx) + movl %edi, 20(%edx) + movl %esi, 24(%edx) + movl %ebp, 28(%edx) + movl %ebx, 36(%edx) + movl $0, 48(%edx) /* store successful return in eax */ + popl %eax /* get saved value of edx */ + movl %eax, 40(%edx) /* save edx */ + movl %ecx, 44(%edx) + movl (%esp), %eax /* get return address */ + movl %eax, 60(%edx) /* save return address */ + fnstcw MC_FP_CW_OFFSET(%edx) + movl $MC_LEN, MC_LEN_OFFSET(%edx) + movl $MC_FPFMT_NODEV, MC_FPFMT_OFFSET(%edx) /* no FP */ + movl $MC_OWNEDFP_NONE, MC_OWNEDFP_OFFSET(%edx) /* no FP */ + pushfl + popl %eax /* get eflags */ + movl %eax, 68(%edx) /* store eflags */ + movl %esp, %eax /* setcontext pushes the return */ + addl $4, %eax /* address onto the top of the */ + movl %eax, 72(%edx) /* stack; account for this */ + movl 40(%edx), %edx /* restore edx -- is this needed? */ + xorl %eax, %eax /* return 0 */ +2: ret diff --git a/lib/libpthread/arch/i386/i386/thr_switch.S b/lib/libpthread/arch/i386/i386/thr_switch.S index db32e5503a9e..27881dda24f8 100644 --- a/lib/libpthread/arch/i386/i386/thr_switch.S +++ b/lib/libpthread/arch/i386/i386/thr_switch.S @@ -34,9 +34,17 @@ __FBSDID("$FreeBSD$"); #define MC_SIZE 640 /* sizeof mcontext_t */ #define UC_MC_OFFSET 16 /* offset to mcontext from ucontext */ #define UC_MC_LEN_OFFSET 96 /* offset to mc_len from mcontext */ -#define MC_FP_REGS_OFFSET 96 /* offset to FP regs from mcontext */ +#define MC_FPREGS_OFFSET 96 /* offset to FP regs from mcontext */ #define MC_FP_CW_OFFSET 96 /* offset to FP control word */ +#define MC_FPFMT_OFFSET 84 /* offset to mc_fpformat from mcontext */ +#define MC_FPFMT_NODEV 0x10000 +#define MC_FPFMT_387 0x10001 +#define MC_FPFMT_XMM 0x10002 #define MC_OWNEDFP_OFFSET 88 /* offset to mc_ownedfp from mcontext */ +#define MC_OWNEDFP_NONE 0x20000 +#define MC_OWNEDFP_FPU 0x20001 +#define MC_OWNEDFP_PCB 0x20002 + /* * int _thread_switch(kse_thr_mailbox *td, thread_mailbox **curthreadp); @@ -60,7 +68,7 @@ ENTRY(_thread_switch) * From here on, we don't touch the old stack. */ addl $UC_MC_OFFSET, %edx /* add offset to mcontext */ - movl 4(%edx), %gs + /*movl 4(%edx), %gs*/ /* we don't touch %gs */ movl 8(%edx), %fs movl 12(%edx), %es movl 16(%edx), %ds @@ -72,13 +80,21 @@ ENTRY(_thread_switch) subl $4, %esp /* leave space for the return address */ movl 60(%edx), %eax /* put return address at top of stack */ movl %eax, (%esp) - cmpl $0, MC_OWNEDFP_OFFSET(%edx) /* are FP regs valid? */ - jz 3f - frstor MC_FP_REGS_OFFSET(%edx) /* restore FP regs */ - jmp 4f -3: fninit + cmpl $MC_OWNEDFP_FPU, MC_OWNEDFP_OFFSET(%edx) + je 4f + cmpl $MC_OWNEDFP_PCB, MC_OWNEDFP_OFFSET(%edx) + jne 6f +4: cmpl $MC_FPFMT_387, MC_FPFMT_OFFSET(%edx) + jne 5f + frstor MC_FPREGS_OFFSET(%edx) /* restore 387 FP regs */ + jmp 7f +5: cmpl $MC_FPFMT_XMM, MC_FPFMT_OFFSET(%edx) + jne 6f + fxrstor MC_FPREGS_OFFSET(%edx) /* restore XMM/SSE FP regs */ + jmp 7f +6: fninit fldcw MC_FP_CW_OFFSET(%edx) -4: movl 48(%edx), %eax /* restore ax, bx, cx, dx */ +7: movl 48(%edx), %eax /* restore ax, bx, cx, dx */ pushl 68(%edx) /* flags on stack */ pushl 36(%edx) /* %ebx on stack */ pushl 44(%edx) /* %ecx on stack */ diff --git a/lib/libpthread/arch/i386/include/atomic_ops.h b/lib/libpthread/arch/i386/include/atomic_ops.h new file mode 100644 index 000000000000..4d84ae574676 --- /dev/null +++ b/lib/libpthread/arch/i386/include/atomic_ops.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2001 Daniel Eischen + * 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$ + */ + +#ifndef _ATOMIC_OPS_H_ +#define _ATOMIC_OPS_H_ + +/* + * Atomic swap: + * Atomic (tmp = *dst, *dst = val), then *res = tmp + * + * void atomic_swap_long(long *dst, long val, long *res); + */ +static inline void +atomic_swap_long(long *dst, long val, long *res) +{ + __asm __volatile( + "xchgl %2, %1; movl %2, %0" + : "=m" (*res) : "m" (*dst), "r" (val) : "memory"); +} + +#define atomic_swap_int(d, v, r) \ + atomic_swap_long((long *)(d), (long)(v), (long *)(r)) + +#define atomic_swap_ptr atomic_swap_int + +#endif diff --git a/lib/libpthread/arch/i386/include/ksd.h b/lib/libpthread/arch/i386/include/ksd.h new file mode 100644 index 000000000000..bdbb83bf3d02 --- /dev/null +++ b/lib/libpthread/arch/i386/include/ksd.h @@ -0,0 +1,144 @@ +/*- + * Copyright (C) 2003 David Xu + * Copyright (c) 2001 Daniel Eischen + * 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$ + */ + +#ifndef _KSD_H_ +#define _KSD_H_ + +#include + +struct pthread; +struct __ucontext; +struct kse; + +/* + * KSE Specific Data. + */ +struct ksd { + int ldt; +#define KSDF_INITIALIZED 0x01 + long flags; + void *base; + long size; +}; + +/* + * Evaluates to the byte offset of the per-kse variable name. + */ +#define __ksd_offset(name) __offsetof(struct kse, name) + +/* + * Evaluates to the type of the per-kse variable name. + */ +#define __ksd_type(name) __typeof(((struct kse *)0)->name) + + +/* + * Evaluates to the value of the per-kse variable name. + */ +#define __KSD_GET_PTR(name) ({ \ + void *__result; \ + \ + u_int __i; \ + __asm __volatile("movl %%gs:%1, %0" \ + : "=r" (__i) \ + : "m" (*(u_int *)(__ksd_offset(name)))); \ + __result = (void *)__i; \ + \ + __result; \ +}) + +/* + * Evaluates to the value of the per-kse variable name. + */ +#define __KSD_GET32(name) ({ \ + __ksd_type(name) __result; \ + \ + u_int __i; \ + __asm __volatile("movl %%gs:%1, %0" \ + : "=r" (__i) \ + : "m" (*(u_int *)(__ksd_offset(name)))); \ + __result = *(__ksd_type(name) *)&__i; \ + \ + __result; \ +}) + +/* + * Sets the value of the per-cpu variable name to value val. + */ +#define __KSD_SET32(name, val) ({ \ + __ksd_type(name) __val = (val); \ + \ + u_int __i; \ + __i = *(u_int *)&__val; \ + __asm __volatile("movl %1,%%gs:%0" \ + : "=m" (*(u_int *)(__ksd_offset(name))) \ + : "r" (__i)); \ +}) + +static __inline u_long +__ksd_readandclear32(volatile u_long *addr) +{ + u_long result; + + __asm __volatile ( + " xorl %0, %0;" + " xchgl %%gs:%1, %0;" + "# __ksd_readandclear32" + : "=&r" (result) + : "m" (*addr)); + return (result); +} + +#define __KSD_READANDCLEAR32(name) ({ \ + __ksd_type(name) __result; \ + \ + __result = (__ksd_type(name)) \ + __ksd_readandclear32((u_long *)__ksd_offset(name)); \ + __result; \ +}) + +/* + * All members of struct kse are prefixed with k_. + */ +#define KSD_GET_PTR(member) __KSD_GET_PTR(k_ ## member) +#define KSD_SET_PTR(member, val) __KSD_SET32(k_ ## member, val) +#define KSD_READANDCLEAR_PTR(member) __KSD_READANDCLEAR32(k_ ## member) + +#define _ksd_curkse ((struct kse *)KSD_GET_PTR(mbx.km_udata)) +#define _ksd_curthread KSD_GET_PTR(curthread) +#define _ksd_set_tmbx(value) KSD_SET_PTR(mbx.km_curthread, (void *)value) +#define _ksd_readandclear_tmbx KSD_READANDCLEAR_PTR(mbx.km_curthread) + +int _ksd_create(struct ksd *ksd, void *base, int size); +void _ksd_destroy(struct ksd *ksd); +int _ksd_getprivate(struct ksd *ksd, void **base, int *size); +int _ksd_setprivate(struct ksd *ksd); +#endif diff --git a/lib/libpthread/arch/i386/include/pthread_md.h b/lib/libpthread/arch/i386/include/pthread_md.h new file mode 100644 index 000000000000..ae00759aa9ce --- /dev/null +++ b/lib/libpthread/arch/i386/include/pthread_md.h @@ -0,0 +1,54 @@ +/*- + * Copyright (c) 2002 Daniel Eischen . + * 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 for the thread kernel. + */ +#ifndef _PTHREAD_MD_H_ +#define _PTHREAD_MD_H_ + +#include +#include +#include + +extern int _thread_enter_uts(struct kse_thr_mailbox *, struct kse_mailbox *); +extern int _thread_switch(struct kse_thr_mailbox *, struct kse_thr_mailbox **); +extern int _thr_setcontext(ucontext_t *); +extern int _thr_getcontext(ucontext_t *); + +/* + * These are needed to ensure an application doesn't attempt to jump + * between stacks of different threads. They return the stack of + * jmp_buf, sigjmp_buf, and ucontext respectively. + */ +#define GET_STACK_JB(jb) ((unsigned long)((jb)[0]._jb[2])) +#define GET_STACK_SJB(sjb) ((unsigned long)((sjb)[0]._sjb[2])) +#define GET_STACK_UC(ucp) ((unsigned long)((ucp)->uc_mcontext.mc_esp)) + +#define THR_GETCONTEXT(ucp) _thr_getcontext(ucp) +#define THR_SETCONTEXT(ucp) _thr_setcontext(ucp) +#endif