2008-01-30 05:30:07 -07:00
|
|
|
/*
|
2005-04-16 16:20:36 -06:00
|
|
|
* Copyright 2002 Andi Kleen, SuSE Labs.
|
|
|
|
* FXSAVE<->i387 conversion support. Based on code by Gareth Hughes.
|
|
|
|
* This is used for ptrace, signals and coredumps in 32bit emulation.
|
2008-01-30 05:30:07 -07:00
|
|
|
*/
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <asm/sigcontext32.h>
|
|
|
|
#include <asm/processor.h>
|
|
|
|
#include <asm/uaccess.h>
|
|
|
|
#include <asm/i387.h>
|
|
|
|
|
|
|
|
static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
|
|
|
|
{
|
|
|
|
unsigned int tmp; /* to avoid 16 bit prefixes in the code */
|
2008-01-30 05:30:07 -07:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
/* Transform each pair of bits into 01 (valid) or 00 (empty) */
|
2008-01-30 05:30:07 -07:00
|
|
|
tmp = ~twd;
|
|
|
|
tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
|
|
|
|
/* and move the valid bits to the lower byte. */
|
|
|
|
tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
|
|
|
|
tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
|
|
|
|
tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
|
|
|
|
return tmp;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2008-01-30 05:30:07 -07:00
|
|
|
#define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
|
|
|
|
#define FP_EXP_TAG_VALID 0
|
|
|
|
#define FP_EXP_TAG_ZERO 1
|
|
|
|
#define FP_EXP_TAG_SPECIAL 2
|
|
|
|
#define FP_EXP_TAG_EMPTY 3
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
static inline unsigned long twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
|
|
|
|
{
|
2008-01-30 05:30:07 -07:00
|
|
|
struct _fpxreg *st;
|
2005-04-16 16:20:36 -06:00
|
|
|
unsigned long tos = (fxsave->swd >> 11) & 7;
|
|
|
|
unsigned long twd = (unsigned long) fxsave->twd;
|
|
|
|
unsigned long tag;
|
|
|
|
unsigned long ret = 0xffff0000;
|
|
|
|
int i;
|
|
|
|
|
2008-01-30 05:30:07 -07:00
|
|
|
for (i = 0; i < 8; i++, twd >>= 1) {
|
2005-04-16 16:20:36 -06:00
|
|
|
if (twd & 0x1) {
|
2008-01-30 05:30:07 -07:00
|
|
|
st = FPREG_ADDR(fxsave, (i - tos) & 7);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
switch (st->exponent & 0x7fff) {
|
|
|
|
case 0x7fff:
|
2008-01-30 05:30:07 -07:00
|
|
|
tag = FP_EXP_TAG_SPECIAL;
|
2005-04-16 16:20:36 -06:00
|
|
|
break;
|
|
|
|
case 0x0000:
|
2008-01-30 05:30:07 -07:00
|
|
|
if (!st->significand[0] &&
|
|
|
|
!st->significand[1] &&
|
|
|
|
!st->significand[2] &&
|
|
|
|
!st->significand[3])
|
|
|
|
tag = FP_EXP_TAG_ZERO;
|
|
|
|
else
|
|
|
|
tag = FP_EXP_TAG_SPECIAL;
|
2005-04-16 16:20:36 -06:00
|
|
|
break;
|
|
|
|
default:
|
2008-01-30 05:30:07 -07:00
|
|
|
if (st->significand[3] & 0x8000)
|
|
|
|
tag = FP_EXP_TAG_VALID;
|
|
|
|
else
|
|
|
|
tag = FP_EXP_TAG_SPECIAL;
|
2005-04-16 16:20:36 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2008-01-30 05:30:07 -07:00
|
|
|
tag = FP_EXP_TAG_EMPTY;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2008-01-30 05:30:07 -07:00
|
|
|
ret |= tag << (2 * i);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-01-30 05:30:07 -07:00
|
|
|
#define G(num, val) err |= __get_user(val, num + (u32 __user *)buf)
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
static inline int convert_fxsr_from_user(struct i387_fxsave_struct *fxsave,
|
|
|
|
struct _fpstate_ia32 __user *buf)
|
|
|
|
{
|
|
|
|
struct _fpxreg *to;
|
|
|
|
struct _fpreg __user *from;
|
2008-01-30 05:30:07 -07:00
|
|
|
int i, err = 0;
|
2005-04-16 16:20:36 -06:00
|
|
|
u32 v;
|
|
|
|
|
|
|
|
G(0, fxsave->cwd);
|
|
|
|
G(1, fxsave->swd);
|
|
|
|
G(2, fxsave->twd);
|
|
|
|
fxsave->twd = twd_i387_to_fxsr(fxsave->twd);
|
|
|
|
G(3, fxsave->rip);
|
|
|
|
G(4, v);
|
2008-01-30 05:30:07 -07:00
|
|
|
/* cs ignored */
|
|
|
|
fxsave->fop = v>>16;
|
2005-04-16 16:20:36 -06:00
|
|
|
G(5, fxsave->rdp);
|
|
|
|
/* 6: ds ignored */
|
2008-01-30 05:30:07 -07:00
|
|
|
if (err)
|
|
|
|
return -1;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
to = (struct _fpxreg *)&fxsave->st_space[0];
|
|
|
|
from = &buf->_st[0];
|
2008-01-30 05:30:07 -07:00
|
|
|
for (i = 0; i < 8; i++, to++, from++) {
|
2005-04-16 16:20:36 -06:00
|
|
|
if (__copy_from_user(to, from, sizeof(*from)))
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-30 05:30:07 -07:00
|
|
|
#define P(num, val) err |= __put_user(val, num + (u32 __user *)buf)
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
static inline int convert_fxsr_to_user(struct _fpstate_ia32 __user *buf,
|
|
|
|
struct i387_fxsave_struct *fxsave,
|
|
|
|
struct pt_regs *regs,
|
|
|
|
struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
struct _fpreg __user *to;
|
|
|
|
struct _fpxreg *from;
|
2008-01-30 05:30:07 -07:00
|
|
|
int i, err = 0;
|
|
|
|
u16 cs, ds;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
if (tsk == current) {
|
2008-01-30 05:30:07 -07:00
|
|
|
/*
|
|
|
|
* should be actually ds/cs at fpu exception time, but
|
|
|
|
* that information is not available in 64bit mode.
|
|
|
|
*/
|
|
|
|
asm("movw %%ds,%0 " : "=r" (ds));
|
|
|
|
asm("movw %%cs,%0 " : "=r" (cs));
|
|
|
|
} else {
|
|
|
|
/* ptrace. task has stopped. */
|
2005-04-16 16:20:36 -06:00
|
|
|
ds = tsk->thread.ds;
|
|
|
|
cs = regs->cs;
|
2008-01-30 05:30:07 -07:00
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
P(0, (u32)fxsave->cwd | 0xffff0000);
|
|
|
|
P(1, (u32)fxsave->swd | 0xffff0000);
|
|
|
|
P(2, twd_fxsr_to_i387(fxsave));
|
|
|
|
P(3, (u32)fxsave->rip);
|
2008-01-30 05:30:07 -07:00
|
|
|
P(4, cs | ((u32)fxsave->fop) << 16);
|
2005-04-16 16:20:36 -06:00
|
|
|
P(5, fxsave->rdp);
|
|
|
|
P(6, 0xffff0000 | ds);
|
|
|
|
|
2008-01-30 05:30:07 -07:00
|
|
|
if (err)
|
|
|
|
return -1;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
to = &buf->_st[0];
|
|
|
|
from = (struct _fpxreg *) &fxsave->st_space[0];
|
2008-01-30 05:30:07 -07:00
|
|
|
for (i = 0; i < 8; i++, to++, from++) {
|
2005-04-16 16:20:36 -06:00
|
|
|
if (__copy_to_user(to, from, sizeof(*to)))
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-30 05:30:07 -07:00
|
|
|
int restore_i387_ia32(struct task_struct *tsk,
|
|
|
|
struct _fpstate_ia32 __user *buf, int fsave)
|
|
|
|
{
|
2005-04-16 16:20:36 -06:00
|
|
|
clear_fpu(tsk);
|
2008-01-30 05:30:07 -07:00
|
|
|
if (!fsave) {
|
|
|
|
if (__copy_from_user(&tsk->thread.i387.fxsave,
|
2005-04-16 16:20:36 -06:00
|
|
|
&buf->_fxsr_env[0],
|
|
|
|
sizeof(struct i387_fxsave_struct)))
|
|
|
|
return -1;
|
|
|
|
tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
|
|
|
|
set_stopped_child_used_math(tsk);
|
2008-01-30 05:30:07 -07:00
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
return convert_fxsr_from_user(&tsk->thread.i387.fxsave, buf);
|
2008-01-30 05:30:07 -07:00
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-01-30 05:30:07 -07:00
|
|
|
int save_i387_ia32(struct task_struct *tsk, struct _fpstate_ia32 __user *buf,
|
|
|
|
struct pt_regs *regs, int fsave)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
init_fpu(tsk);
|
|
|
|
if (convert_fxsr_to_user(buf, &tsk->thread.i387.fxsave, regs, tsk))
|
|
|
|
return -1;
|
|
|
|
if (fsave)
|
|
|
|
return 0;
|
|
|
|
err |= __put_user(tsk->thread.i387.fxsave.swd, &buf->status);
|
2008-01-30 05:30:07 -07:00
|
|
|
if (fsave)
|
|
|
|
return err ? -1 : 1;
|
2005-04-16 16:20:36 -06:00
|
|
|
err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
|
|
|
|
err |= __copy_to_user(&buf->_fxsr_env[0], &tsk->thread.i387.fxsave,
|
|
|
|
sizeof(struct i387_fxsave_struct));
|
|
|
|
return err ? -1 : 1;
|
|
|
|
}
|