2 * Copyright 2002 Andi Kleen, SuSE Labs.
3 * FXSAVE<->i387 conversion support. Based on code by Gareth Hughes.
4 * This is used for ptrace, signals and coredumps in 32bit emulation.
5 * $Id: fpu32.c,v 1.1 2002/03/21 14:16:32 ak Exp $
8 #include <linux/sched.h>
9 #include <asm/sigcontext32.h>
10 #include <asm/processor.h>
11 #include <asm/uaccess.h>
14 static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
16 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
18 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
20 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
21 /* and move the valid bits to the lower byte. */
22 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
23 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
24 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
28 static inline unsigned long twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
30 struct _fpxreg *st = NULL;
31 unsigned long tos = (fxsave->swd >> 11) & 7;
32 unsigned long twd = (unsigned long) fxsave->twd;
34 unsigned long ret = 0xffff0000;
37 #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
39 for (i = 0 ; i < 8 ; i++) {
41 st = FPREG_ADDR( fxsave, (i - tos) & 7 );
43 switch (st->exponent & 0x7fff) {
45 tag = 2; /* Special */
48 if ( !st->significand[0] &&
49 !st->significand[1] &&
50 !st->significand[2] &&
51 !st->significand[3] ) {
54 tag = 2; /* Special */
58 if (st->significand[3] & 0x8000) {
61 tag = 2; /* Special */
68 ret |= (tag << (2 * i));
75 static inline int convert_fxsr_from_user(struct i387_fxsave_struct *fxsave,
76 struct _fpstate_ia32 __user *buf)
79 struct _fpreg __user *from;
84 #define G(num,val) err |= __get_user(val, num + (u32 __user *)buf)
88 fxsave->twd = twd_i387_to_fxsr(fxsave->twd);
91 fxsave->fop = v>>16; /* cs ignored */
98 to = (struct _fpxreg *)&fxsave->st_space[0];
100 for (i = 0 ; i < 8 ; i++, to++, from++) {
101 if (__copy_from_user(to, from, sizeof(*from)))
108 static inline int convert_fxsr_to_user(struct _fpstate_ia32 __user *buf,
109 struct i387_fxsave_struct *fxsave,
110 struct pt_regs *regs,
111 struct task_struct *tsk)
113 struct _fpreg __user *to;
114 struct _fpxreg *from;
119 if (tsk == current) {
120 /* should be actually ds/cs at fpu exception time,
121 but that information is not available in 64bit mode. */
122 asm("movw %%ds,%0 " : "=r" (ds));
123 asm("movw %%cs,%0 " : "=r" (cs));
124 } else { /* ptrace. task has stopped. */
129 #define P(num,val) err |= __put_user(val, num + (u32 __user *)buf)
130 P(0, (u32)fxsave->cwd | 0xffff0000);
131 P(1, (u32)fxsave->swd | 0xffff0000);
132 P(2, twd_fxsr_to_i387(fxsave));
133 P(3, (u32)fxsave->rip);
134 P(4, cs | ((u32)fxsave->fop) << 16);
136 P(6, 0xffff0000 | ds);
143 from = (struct _fpxreg *) &fxsave->st_space[0];
144 for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
145 if (__copy_to_user(to, from, sizeof(*to)))
151 int restore_i387_ia32(struct task_struct *tsk, struct _fpstate_ia32 __user *buf, int fsave)
155 if (__copy_from_user(&tsk->thread.i387.fxsave,
157 sizeof(struct i387_fxsave_struct)))
159 tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
160 set_stopped_child_used_math(tsk);
162 return convert_fxsr_from_user(&tsk->thread.i387.fxsave, buf);
165 int save_i387_ia32(struct task_struct *tsk,
166 struct _fpstate_ia32 __user *buf,
167 struct pt_regs *regs,
173 if (convert_fxsr_to_user(buf, &tsk->thread.i387.fxsave, regs, tsk))
177 err |= __put_user(tsk->thread.i387.fxsave.swd, &buf->status);
180 err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
181 err |= __copy_to_user(&buf->_fxsr_env[0], &tsk->thread.i387.fxsave,
182 sizeof(struct i387_fxsave_struct));