diff options
Diffstat (limited to 'lib/libpthread/arch')
-rw-r--r-- | lib/libpthread/arch/alpha/alpha/_atomic_lock.S | 45 | ||||
-rw-r--r-- | lib/libpthread/arch/i386/i386/_atomic_lock.S | 42 | ||||
-rw-r--r-- | lib/libpthread/arch/i386/i386/ksd.c | 168 | ||||
-rw-r--r-- | lib/libpthread/arch/i386/i386/thr_enter_uts.S | 101 | ||||
-rw-r--r-- | lib/libpthread/arch/i386/i386/thr_getcontext.S | 157 | ||||
-rw-r--r-- | lib/libpthread/arch/i386/i386/thr_switch.S | 113 | ||||
-rw-r--r-- | lib/libpthread/arch/i386/include/atomic_ops.h | 51 | ||||
-rw-r--r-- | lib/libpthread/arch/i386/include/ksd.h | 145 | ||||
-rw-r--r-- | lib/libpthread/arch/i386/include/pthread_md.h | 54 | ||||
-rw-r--r-- | lib/libpthread/arch/ia64/ia64/_atomic_lock.S | 48 |
10 files changed, 924 insertions, 0 deletions
diff --git a/lib/libpthread/arch/alpha/alpha/_atomic_lock.S b/lib/libpthread/arch/alpha/alpha/_atomic_lock.S new file mode 100644 index 0000000..1cfb52f --- /dev/null +++ b/lib/libpthread/arch/alpha/alpha/_atomic_lock.S @@ -0,0 +1,45 @@ +/* + * Copyright (c) 1998 John Birrell <jb@cimlogic.com.au>. + * All rights reserved. + * copyright Douglas Santry 1996 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the above copyright is retained + * in the source form. + * + * THIS SOFTWARE IS PROVIDED BY Douglas Santry 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 Douglas Santry 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.h" + +/* + * Atomicly lock a location with an identifier provided the location + * is not currently locked. + * + * long _atomic_lock(long *); + * v0 will contain the return value (zero if lock obtained). + */ +LEAF(_atomic_lock,0) + LDGP(pv) + +0: ldq_l v0, 0(a0) /* read existing lock value */ + mov 1, t0 /* locked value to store */ + stq_c t0, 0(a0) /* attempt to store, status in t0 */ + beq t0, 1f /* branch foward to optimise prediction */ + mb /* sync with other processors */ + RET /* return with v0==0 if lock obtained */ +1: br 0b /* loop to try again */ +END(_atomic_lock) diff --git a/lib/libpthread/arch/i386/i386/_atomic_lock.S b/lib/libpthread/arch/i386/i386/_atomic_lock.S new file mode 100644 index 0000000..af49aff --- /dev/null +++ b/lib/libpthread/arch/i386/i386/_atomic_lock.S @@ -0,0 +1,42 @@ +/* + * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>. + * All rights reserved. + * copyright Douglas Santry 1996 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the above copyright is retained + * in the source form. + * + * THIS SOFTWARE IS PROVIDED BY Douglas Santry 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 Douglas Santry 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. + */ + +#if defined(LIBC_RCS) && !defined(lint) + .text + .asciz "$FreeBSD$" +#endif /* LIBC_RCS and not lint */ + +#include <machine/asm.h> + +/* + * Atomicly lock a location with an identifier provided the location + * is not currently locked. + * + * long _atomic_lock(long *); + * eax will contain the return value (zero if lock obtained). + */ +ENTRY(_atomic_lock) + movl 4(%esp), %ecx + movl $1, %eax + xchg %eax, (%ecx) + ret + diff --git a/lib/libpthread/arch/i386/i386/ksd.c b/lib/libpthread/arch/i386/i386/ksd.c new file mode 100644 index 0000000..791c636 --- /dev/null +++ b/lib/libpthread/arch/i386/i386/ksd.c @@ -0,0 +1,168 @@ +/*- + * 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. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/types.h> +#include <machine/cpufunc.h> +#include <machine/segments.h> +#include <machine/sysarch.h> + +#include <unistd.h> +#include <signal.h> +#include <stdlib.h> +#include <string.h> +#include <ucontext.h> + +#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; + +static void initialize(void); + +static 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 new file mode 100644 index 0000000..a617b72 --- /dev/null +++ b/lib/libpthread/arch/i386/i386/thr_enter_uts.S @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2002 Jonathan Mini <mini@freebsd.org>. + * Copyright (c) 2001 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 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 <machine/asm.h> +__FBSDID("$FreeBSD$"); + +/* + * Where do we define these? + */ +#define MC_SIZE 640 /* sizeof mcontext_t */ +#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 */ + +/* + * int _thread_enter_uts(kse_thr_mailbox *tm, kse_mailbox *km); + * + * Returns 0 on success, -1 otherwise. + */ +ENTRY(_thread_enter_uts) + movl 4(%esp), %eax /* get address of context */ + cmpl $0, %eax /* check for null pointer */ + jne 1f + movl $-1, %eax + jmp 2f +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)*/ /* we don't touch %gs */ + movl %fs, 8(%edx) + movl %es, 12(%edx) + movl %ds, 16(%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 */ + movl %ss, 76(%edx) + /* + * Don't save floating point registers here. + * + * This is an explicit call to get the current context, so + * the caller is done with the floating point registers. + * Contexts formed by involuntary switches, such as signal delivery, + * have floating point registers saved by the kernel. + */ + fnstcw MC_FP_CW_OFFSET(%edx) + 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 */ + 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 $MC_SIZE, MC_LEN_OFFSET(%edx) /* context is now valid */ + movl 8(%esp), %edx /* get address of mailbox */ + movl KM_STACK_SP_OFFSET(%edx), %eax /* get bottom of stack */ + addl KM_STACK_SIZE_OFFSET(%edx), %eax /* add length */ + movl %eax, %esp /* switch to the uts's stack */ + pushl %edx /* push the address of the mailbox */ + pushl KM_FUNC_OFFSET(%edx) /* .. the uts can return to itself */ + pushl KM_FUNC_OFFSET(%edx) /* push the address of the uts func */ +2: ret + 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 0000000..82066ef --- /dev/null +++ b/lib/libpthread/arch/i386/i386/thr_getcontext.S @@ -0,0 +1,157 @@ +/*- + * Copyright (c) 2001 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 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 <machine/asm.h> +__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 <machine/ucontext.h> */ +#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 new file mode 100644 index 0000000..f9c8b92 --- /dev/null +++ b/lib/libpthread/arch/i386/i386/thr_switch.S @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2002 Jonathan Mini <mini@freebsd.org>. + * Copyright (c) 2001 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 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 <machine/asm.h> +__FBSDID("$FreeBSD$"); + +/* + * Where do we define these? + */ +#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_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); + * + * Does not return on success, returns -1 otherwise. + */ +ENTRY(_thread_switch) + movl 4(%esp), %edx /* get address of thread_mailbox */ + /* .. ucontext_t is at offset 0 */ + cmpl $0, %edx /* check for null pointer */ + jne 1f + movl $-1, %eax + jmp 9f +1: cmpl $MC_SIZE, UC_MC_LEN_OFFSET(%edx) /* is context valid? */ + je 2f + movl $-1, %eax /* bzzzt, invalid context */ + jmp 9f +2: movl 8(%esp), %ecx /* get address of curthreadp */ + movl %edx, %ebx /* save the pointer for later */ + /* + * From here on, we don't touch the old stack. + */ + addl $UC_MC_OFFSET, %edx /* add offset to mcontext */ + /*movl 4(%edx), %gs*/ /* we don't touch %gs */ + 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 72(%edx), %esp /* switch to context defined stack */ + subl $4, %esp /* leave space for the return address */ + movl 60(%edx), %eax /* put return address at top of stack */ + movl %eax, (%esp) + 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) +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 */ + movl 40(%edx), %edx /* %edx */ + /* + * all registers are now moved out of mailbox, + * it's safe to set current thread pointer + */ + cmpl $0, %ecx + je 8f + movl %ebx,(%ecx) +8: popl %ecx /* %ecx off stack */ + popl %ebx /* %ebx off stack */ + popfl /* flags off stack */ +9: ret /* %eip off 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 0000000..1825b8c --- /dev/null +++ b/lib/libpthread/arch/i386/include/atomic_ops.h @@ -0,0 +1,51 @@ +/*- + * Copyright (c) 2001 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$ + */ + +#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 0000000..0f39fff --- /dev/null +++ b/lib/libpthread/arch/i386/include/ksd.h @@ -0,0 +1,145 @@ +/*- + * Copyright (C) 2003 David Xu <davidxu@freebsd.org> + * Copyright (c) 2001 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$ + */ + +#ifndef _KSD_H_ +#define _KSD_H_ + +#include <sys/types.h> + +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_get_tmbx(value) KSD_GET_PTR(mbx.km_curthread) +#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 0000000..cb5a344 --- /dev/null +++ b/lib/libpthread/arch/i386/include/pthread_md.h @@ -0,0 +1,54 @@ +/*- + * Copyright (c) 2002 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. 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 <setjmp.h> +#include <ucontext.h> + +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) + +#define THR_ALIGNBYTES 15 +#define THR_ALIGN(td) (((unsigned)(td) + THR_ALIGNBYTES) & ~THR_ALIGNBYTES) +#endif diff --git a/lib/libpthread/arch/ia64/ia64/_atomic_lock.S b/lib/libpthread/arch/ia64/ia64/_atomic_lock.S new file mode 100644 index 0000000..836b375 --- /dev/null +++ b/lib/libpthread/arch/ia64/ia64/_atomic_lock.S @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2002 Marcel Moolenaar + * 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 ``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$ + */ + +#if defined(LIBC_RCS) && !defined(lint) + .text + .asciz "$FreeBSD$" +#endif /* LIBC_RCS and not lint */ + +#include <machine/asm.h> + +/* + * Atomicly lock a location with an identifier provided the location + * is not currently locked. + * + * long _atomic_lock(long *); + * eax will contain the return value (zero if lock obtained). + */ +ENTRY(_atomic_lock, 1) + add r14 = 1, r0 + ;; + xchg8 r8 = [r32], r14 + br.ret.sptk rp +END(_atomic_lock) |