From 9fe0b6bde601877afbe7ac644d00c3bd6a263502 Mon Sep 17 00:00:00 2001 From: deischen Date: Fri, 18 Apr 2003 05:00:52 +0000 Subject: 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 ++++++++++ 4 files changed, 268 insertions(+), 2 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 (limited to 'lib/libkse/arch/i386') diff --git a/lib/libkse/arch/i386/i386/thr_enter_uts.S b/lib/libkse/arch/i386/i386/thr_enter_uts.S index 10edd3f..a617b72 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 0000000..217e893 --- /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 0000000..4d84ae5 --- /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 0000000..ae00759 --- /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 -- cgit v1.1