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.
7 #include <linux/sched.h>
8 #include <asm/sigcontext32.h>
9 #include <asm/processor.h>
10 #include <asm/uaccess.h>
12 #include <asm/user32.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
29 twd_fxsr_to_i387(const struct i387_fxsave_struct *fxsave)
31 struct _fpxreg *st = NULL;
32 unsigned long tos = (fxsave->swd >> 11) & 7;
33 unsigned long twd = (unsigned long) fxsave->twd;
35 unsigned long ret = 0xffff0000;
38 #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16);
40 for (i = 0 ; i < 8 ; i++) {
42 st = FPREG_ADDR( fxsave, (i - tos) & 7 );
44 switch (st->exponent & 0x7fff) {
46 tag = 2; /* Special */
49 if ( !st->significand[0] &&
50 !st->significand[1] &&
51 !st->significand[2] &&
52 !st->significand[3] ) {
55 tag = 2; /* Special */
59 if (st->significand[3] & 0x8000) {
62 tag = 2; /* Special */
69 ret |= (tag << (2 * i));
77 convert_fxsr_env_from_i387(struct i387_fxsave_struct *fxsave, const u32 env[7])
80 #define G(num,val) val = env[num]
84 fxsave->twd = twd_i387_to_fxsr(fxsave->twd);
87 fxsave->fop = v>>16; /* cs ignored */
93 static inline int convert_fxsr_from_user(struct i387_fxsave_struct *fxsave,
94 struct _fpstate_ia32 __user *buf)
98 struct _fpreg __user *from;
101 if (__copy_from_user(env, buf, sizeof(env)))
104 convert_fxsr_env_from_i387(fxsave, env);
106 to = (struct _fpxreg *)&fxsave->st_space[0];
108 for (i = 0 ; i < 8 ; i++, to++, from++) {
109 if (__copy_from_user(to, from, sizeof(*from)))
117 convert_fxsr_env_to_i387(struct task_struct *tsk, struct pt_regs *regs,
118 u32 env[7], const struct i387_fxsave_struct *fxsave)
122 if (tsk == current) {
123 /* should be actually ds/cs at fpu exception time,
124 but that information is not available in 64bit mode. */
125 asm("movw %%ds,%0 " : "=r" (ds));
126 asm("movw %%cs,%0 " : "=r" (cs));
127 } else { /* ptrace. task has stopped. */
132 #define P(num,val) env[num] = val
133 P(0, (u32)fxsave->cwd | 0xffff0000);
134 P(1, (u32)fxsave->swd | 0xffff0000);
135 P(2, twd_fxsr_to_i387(fxsave));
136 P(3, (u32)fxsave->rip);
137 P(4, cs | ((u32)fxsave->fop) << 16);
139 P(6, 0xffff0000 | ds);
144 static inline int convert_fxsr_to_user(struct _fpstate_ia32 __user *buf,
145 struct i387_fxsave_struct *fxsave,
146 struct pt_regs *regs,
147 struct task_struct *tsk)
149 struct _fpreg __user *to;
150 struct _fpxreg *from;
154 convert_fxsr_env_to_i387(tsk, regs, env, fxsave);
155 if (__copy_to_user(buf, env, sizeof(env)))
159 from = (struct _fpxreg *) &fxsave->st_space[0];
160 for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
161 if (__copy_to_user(to, from, sizeof(*to)))
167 int restore_i387_ia32(struct task_struct *tsk, struct _fpstate_ia32 __user *buf, int fsave)
171 if (__copy_from_user(&tsk->thread.i387.fxsave,
173 sizeof(struct i387_fxsave_struct)))
175 tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
176 set_stopped_child_used_math(tsk);
178 return convert_fxsr_from_user(&tsk->thread.i387.fxsave, buf);
181 int save_i387_ia32(struct task_struct *tsk,
182 struct _fpstate_ia32 __user *buf,
183 struct pt_regs *regs,
189 if (convert_fxsr_to_user(buf, &tsk->thread.i387.fxsave, regs, tsk))
193 err |= __put_user(tsk->thread.i387.fxsave.swd, &buf->status);
196 err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
197 err |= __copy_to_user(&buf->_fxsr_env[0], &tsk->thread.i387.fxsave,
198 sizeof(struct i387_fxsave_struct));
202 int get_fpregs32(struct user_i387_ia32_struct *buf, struct task_struct *tsk)
204 struct pt_regs *regs = ((struct pt_regs *)tsk->thread.rsp0) - 1;
206 const struct _fpxreg *from;
209 convert_fxsr_env_to_i387(tsk, regs,
210 (u32 *) buf, &tsk->thread.i387.fxsave);
212 to = (struct _fpreg *) buf->st_space;
213 from = (const struct _fpxreg *) &tsk->thread.i387.fxsave.st_space[0];
214 for (i = 0; i < 8; i++, to++, from++)
215 *to = *(const struct _fpreg *) from;
221 set_fpregs32(struct task_struct *tsk, const struct user_i387_ia32_struct *buf)
224 const struct _fpreg *from;
227 convert_fxsr_env_from_i387(&tsk->thread.i387.fxsave, (u32 *) buf);
229 to = (struct _fpxreg *) &tsk->thread.i387.fxsave.st_space[0];
230 from = (const struct _fpreg *) buf->st_space;
231 for (i = 0; i < 8; i++, to++, from++)
232 *(struct _fpreg *) to = *from;