Save and restore FPU state properly in ucontext_t's.

Reviewed by:	deischen, julian
Approved by:	-arch
This commit is contained in:
Jonathan Mini 2002-09-16 19:24:31 +00:00
parent b3466b3fe0
commit f990583656
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=103406
4 changed files with 29 additions and 36 deletions

View File

@ -3,4 +3,4 @@
SRCS+= _ctx_start.S _setjmp.S alloca.S getcontext.S fabs.S frexp.c \
infinity.c isinf.c ldexp.c makecontext.c modf.S \
rfork_thread.S setjmp.S sigsetjmp.S swapcontext.c
rfork_thread.S setjmp.S sigsetjmp.S swapcontext.c signalcontext.c

View File

@ -3,4 +3,4 @@
SRCS+= _ctx_start.S _setjmp.S alloca.S getcontext.S fabs.S frexp.c \
infinity.c isinf.c ldexp.c makecontext.c modf.S \
rfork_thread.S setjmp.S sigsetjmp.S swapcontext.c
rfork_thread.S setjmp.S sigsetjmp.S swapcontext.c signalcontext.c

View File

@ -30,14 +30,13 @@ __FBSDID("$FreeBSD$");
/*
* Where do we define these?
*/
#define UC_MC_VALID 0x0001 /* __UC_MC_VALID <machine/ucontext.h> */
#define UC_FP_VALID 0x0002 /* __UC_FP_VALID <machine/ucontext.h> */
#define MC_FP_VALID UC_MC_VALID | UC_FP_VALID
#define MC_SIZE 640 /* sizeof mcontext_t */
#define UC_MC_OFFSET 16 /* offset to mcontext from ucontext */
#define UC_FLAGS_OFFSET 208 /* offset to flags from ucontext */
#define MC_FLAGS_OFFSET 192 /* offset to flags from mcontext */
#define MC_FP_REGS_OFFSET 80 /* offset to FP regs from mcontext */
#define MC_FP_CW_OFFSET 80 /* offset to FP control word */
#define UC_MC_LEN_OFFSET 96 /* offset to mc_len from mcontext */
#define MC_LEN_OFFSET 80 /* offset to mc_len from mcontext */
#define MC_FP_REGS_OFFSET 96 /* offset to FP regs from mcontext */
#define MC_FP_CW_OFFSET 96 /* offset to FP control word */
#define MC_OWNEDFP_OFFSET 88 /* offset to mc_ownedfp from mcontext */
/*
* int setcontext(ucontext_t *ucp);
@ -54,8 +53,8 @@ ENTRY(__setcontext)
jne 1f
movl $-1, %eax
jmp 5f
1: testl $UC_MC_VALID, UC_FLAGS_OFFSET(%eax) /* is context valid? */
jnz 2f
1: cmpl $MC_SIZE, UC_MC_LEN_OFFSET(%eax) /* is context valid? */
je 2f
movl $-1, %eax /* bzzzt, invalid context */
jmp 5f
2: PIC_PROLOGUE
@ -80,18 +79,19 @@ ENTRY(__setcontext)
subl $4, %esp /* leave space for the return address */
movl 60(%edx), %eax /* put return address at top of stack */
movl %eax, (%esp)
testl $UC_FP_VALID, MC_FLAGS_OFFSET(%edx) /* are FP regs valid? */
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
fldcw MC_FP_CW_OFFSET(%edx)
4: movl 68(%edx), %eax /* restore flags register */
sahf
movl 48(%edx), %eax /* restore ax, bx, cx, and dx last */
4: movl 48(%edx), %eax /* restore ax, bx, cx */
movl 36(%edx), %ebx
movl 44(%edx), %ecx
movl 40(%edx), %edx
pushl 68(%edx) /* flags on stack */
pushl 40(%edx) /* %edx on stack */
popl %edx /* %edx off stack */
popf /* flags off stack */
5: ret
/*
@ -109,8 +109,8 @@ ENTRY(__getcontext)
jne 1f
movl $-1, %eax
jmp 2f
movl 4(%esp), %eax /* get address of context and sigset */
1: PIC_PROLOGUE
1: movl 4(%esp), %eax /* get address of context and sigset */
PIC_PROLOGUE
pushl %eax /* oset = &ucp->uc_sigmask */
pushl $0 /* set = NULL */
pushl $3 /* how = SIG_SETMASK */
@ -136,30 +136,23 @@ ENTRY(__getcontext)
movl %ecx, 44(%edx)
movl (%esp), %eax /* get return address */
movl %eax, 60(%edx) /* save return address */
movl %ss, 76(%edx)
/*
* XXX - Do we really need to save floating point registers?
* Don't save floating point registers here.
*
* This is an explicit call to get the current context, so
* shouldn't the caller be done with the floating point registers?
* the caller is done with the floating point registers.
* Contexts formed by involuntary switches, such as signal delivery,
* should have floating point registers saved by the kernel.
*
* As of this writing, the kernel doesn't yet save the FPU state
* on signal delivery, so a setcontext on the interrupted context
* may yield incorrect results regardless.
* have floating point registers saved by the kernel.
*/
#if 1
fnstcw MC_FP_CW_OFFSET(%edx)
movl $UC_MC_VALID, MC_FLAGS_OFFSET(%edx) /* mcontext valid, no FP */
#else
fnsave MC_FP_REGS_OFFSET(%edx) /* save FP regs */
movl $MC_FP_VALID, MC_FLAGS_OFFSET(%edx) /* mcontext and FP valid */
#endif
movl $0, MC_OWNEDFP_OFFSET(%edx) /* no FP */
lahf /* 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 $MC_SIZE, MC_LEN_OFFSET(%edx) /* context is now valid */
movl 40(%edx), %edx /* restore edx -- is this needed? */
xorl %eax, %eax /* return 0 */
2: ret

View File

@ -29,11 +29,11 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/signal.h>
#include <sys/ucontext.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ucontext.h>
#include <unistd.h>
/* Prototypes */
@ -53,7 +53,7 @@ _ctx_done (ucontext_t *ucp)
* to be restarted without being reinitialized (via
* setcontext or swapcontext).
*/
ucp->uc_mcontext.mc_flags = 0;
ucp->uc_mcontext.mc_len = 0;
/* Set context to next one in link */
/* XXX - what to do for error, abort? */
@ -80,14 +80,14 @@ __makecontext(ucontext_t *ucp, void (*start)(void), int argc, ...)
* a void function. At least make sure that the context
* isn't valid so it can't be used without an error.
*/
ucp->uc_mcontext.mc_flags = 0;
ucp->uc_mcontext.mc_len = 0;
}
/* XXX - Do we want to sanity check argc? */
else if ((argc < 0) || (argc > NCARGS)) {
ucp->uc_mcontext.mc_flags = 0;
ucp->uc_mcontext.mc_len = 0;
}
/* Make sure the context is valid. */
else if ((ucp->uc_mcontext.mc_flags & __UC_MC_VALID) != 0) {
else if (ucp->uc_mcontext.mc_len == sizeof(mcontext_t)) {
/*
* Arrange the stack as follows:
*