Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 1994 Linus Torvalds |
| 3 | * |
| 4 | * Pentium III FXSR, SSE support |
| 5 | * General FPU state handling cleanups |
| 6 | * Gareth Hughes <gareth@valinux.com>, May 2000 |
| 7 | */ |
Alexey Dobriyan | 129f694 | 2005-06-23 00:08:33 -0700 | [diff] [blame] | 8 | #include <linux/module.h> |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 9 | #include <linux/regset.h> |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 10 | #include <linux/sched.h> |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 11 | #include <linux/bootmem.h> |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <asm/sigcontext.h> |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 14 | #include <asm/processor.h> |
| 15 | #include <asm/math_emu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/uaccess.h> |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 17 | #include <asm/ptrace.h> |
| 18 | #include <asm/i387.h> |
| 19 | #include <asm/user.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 21 | #ifdef CONFIG_X86_64 |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 22 | # include <asm/sigcontext32.h> |
| 23 | # include <asm/user32.h> |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 24 | #else |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 25 | # define save_i387_ia32 save_i387 |
| 26 | # define restore_i387_ia32 restore_i387 |
| 27 | # define _fpstate_ia32 _fpstate |
| 28 | # define user_i387_ia32_struct user_i387_struct |
| 29 | # define user32_fxsr_struct user_fxsr_struct |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 30 | #endif |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #ifdef CONFIG_MATH_EMULATION |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 33 | # define HAVE_HWFP (boot_cpu_data.hard_math) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #else |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 35 | # define HAVE_HWFP 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #endif |
| 37 | |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 38 | static unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu; |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 39 | unsigned int xstate_size; |
| 40 | static struct i387_fxsave_struct fx_scratch __cpuinitdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 42 | void __cpuinit mxcsr_feature_mask_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { |
| 44 | unsigned long mask = 0; |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | clts(); |
| 47 | if (cpu_has_fxsr) { |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 48 | memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct)); |
| 49 | asm volatile("fxsave %0" : : "m" (fx_scratch)); |
| 50 | mask = fx_scratch.mxcsr_mask; |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 51 | if (mask == 0) |
| 52 | mask = 0x0000ffbf; |
| 53 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | mxcsr_feature_mask &= mask; |
| 55 | stts(); |
| 56 | } |
| 57 | |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 58 | void __init init_thread_xstate(void) |
| 59 | { |
| 60 | if (cpu_has_fxsr) |
| 61 | xstate_size = sizeof(struct i387_fxsave_struct); |
| 62 | #ifdef CONFIG_X86_32 |
| 63 | else |
| 64 | xstate_size = sizeof(struct i387_fsave_struct); |
| 65 | #endif |
| 66 | init_task.thread.xstate = alloc_bootmem(xstate_size); |
| 67 | } |
| 68 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 69 | #ifdef CONFIG_X86_64 |
| 70 | /* |
| 71 | * Called at bootup to set up the initial FPU state that is later cloned |
| 72 | * into all processes. |
| 73 | */ |
| 74 | void __cpuinit fpu_init(void) |
| 75 | { |
| 76 | unsigned long oldcr0 = read_cr0(); |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 77 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 78 | set_in_cr4(X86_CR4_OSFXSR); |
| 79 | set_in_cr4(X86_CR4_OSXMMEXCPT); |
| 80 | |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 81 | write_cr0(oldcr0 & ~(X86_CR0_TS|X86_CR0_EM)); /* clear TS and EM */ |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 82 | |
| 83 | mxcsr_feature_mask_init(); |
| 84 | /* clean state in init */ |
| 85 | current_thread_info()->status = 0; |
| 86 | clear_used_math(); |
| 87 | } |
| 88 | #endif /* CONFIG_X86_64 */ |
| 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | /* |
| 91 | * The _current_ task is using the FPU for the first time |
| 92 | * so initialize it and set the mxcsr to its default |
| 93 | * value at reset if we support XMM instructions and then |
| 94 | * remeber the current task has used the FPU. |
| 95 | */ |
| 96 | void init_fpu(struct task_struct *tsk) |
| 97 | { |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 98 | if (tsk_used_math(tsk)) { |
| 99 | if (tsk == current) |
| 100 | unlazy_fpu(tsk); |
| 101 | return; |
| 102 | } |
| 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | if (cpu_has_fxsr) { |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 105 | struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave; |
| 106 | |
| 107 | memset(fx, 0, xstate_size); |
| 108 | fx->cwd = 0x37f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | if (cpu_has_xmm) |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 110 | fx->mxcsr = MXCSR_DEFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } else { |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 112 | struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave; |
| 113 | memset(fp, 0, xstate_size); |
| 114 | fp->cwd = 0xffff037fu; |
| 115 | fp->swd = 0xffff0000u; |
| 116 | fp->twd = 0xffffffffu; |
| 117 | fp->fos = 0xffff0000u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 119 | /* |
| 120 | * Only the device not available exception or ptrace can call init_fpu. |
| 121 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | set_stopped_child_used_math(tsk); |
| 123 | } |
| 124 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 125 | int fpregs_active(struct task_struct *target, const struct user_regset *regset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 127 | return tsk_used_math(target) ? regset->n : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 129 | |
| 130 | int xfpregs_active(struct task_struct *target, const struct user_regset *regset) |
| 131 | { |
| 132 | return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0; |
| 133 | } |
| 134 | |
| 135 | int xfpregs_get(struct task_struct *target, const struct user_regset *regset, |
| 136 | unsigned int pos, unsigned int count, |
| 137 | void *kbuf, void __user *ubuf) |
| 138 | { |
| 139 | if (!cpu_has_fxsr) |
| 140 | return -ENODEV; |
| 141 | |
Suresh Siddha | 18a8622 | 2008-03-03 13:01:08 -0800 | [diff] [blame] | 142 | init_fpu(target); |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 143 | |
| 144 | return user_regset_copyout(&pos, &count, &kbuf, &ubuf, |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 145 | &target->thread.xstate->fxsave, 0, -1); |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | int xfpregs_set(struct task_struct *target, const struct user_regset *regset, |
| 149 | unsigned int pos, unsigned int count, |
| 150 | const void *kbuf, const void __user *ubuf) |
| 151 | { |
| 152 | int ret; |
| 153 | |
| 154 | if (!cpu_has_fxsr) |
| 155 | return -ENODEV; |
| 156 | |
Suresh Siddha | 18a8622 | 2008-03-03 13:01:08 -0800 | [diff] [blame] | 157 | init_fpu(target); |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 158 | set_stopped_child_used_math(target); |
| 159 | |
| 160 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 161 | &target->thread.xstate->fxsave, 0, -1); |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 162 | |
| 163 | /* |
| 164 | * mxcsr reserved bits must be masked to zero for security reasons. |
| 165 | */ |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 166 | target->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask; |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 167 | |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | /* |
| 174 | * FPU tag word conversions. |
| 175 | */ |
| 176 | |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 177 | static inline unsigned short twd_i387_to_fxsr(unsigned short twd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
| 179 | unsigned int tmp; /* to avoid 16 bit prefixes in the code */ |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | /* Transform each pair of bits into 01 (valid) or 00 (empty) */ |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 182 | tmp = ~twd; |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 183 | tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */ |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 184 | /* and move the valid bits to the lower byte. */ |
| 185 | tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */ |
| 186 | tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */ |
| 187 | tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */ |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 188 | |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 189 | return tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 192 | #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16); |
| 193 | #define FP_EXP_TAG_VALID 0 |
| 194 | #define FP_EXP_TAG_ZERO 1 |
| 195 | #define FP_EXP_TAG_SPECIAL 2 |
| 196 | #define FP_EXP_TAG_EMPTY 3 |
| 197 | |
| 198 | static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 200 | struct _fpxreg *st; |
| 201 | u32 tos = (fxsave->swd >> 11) & 7; |
| 202 | u32 twd = (unsigned long) fxsave->twd; |
| 203 | u32 tag; |
| 204 | u32 ret = 0xffff0000u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | int i; |
| 206 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 207 | for (i = 0; i < 8; i++, twd >>= 1) { |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 208 | if (twd & 0x1) { |
| 209 | st = FPREG_ADDR(fxsave, (i - tos) & 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 211 | switch (st->exponent & 0x7fff) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | case 0x7fff: |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 213 | tag = FP_EXP_TAG_SPECIAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | break; |
| 215 | case 0x0000: |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 216 | if (!st->significand[0] && |
| 217 | !st->significand[1] && |
| 218 | !st->significand[2] && |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 219 | !st->significand[3]) |
| 220 | tag = FP_EXP_TAG_ZERO; |
| 221 | else |
| 222 | tag = FP_EXP_TAG_SPECIAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | break; |
| 224 | default: |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 225 | if (st->significand[3] & 0x8000) |
| 226 | tag = FP_EXP_TAG_VALID; |
| 227 | else |
| 228 | tag = FP_EXP_TAG_SPECIAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | break; |
| 230 | } |
| 231 | } else { |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 232 | tag = FP_EXP_TAG_EMPTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 234 | ret |= tag << (2 * i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | return ret; |
| 237 | } |
| 238 | |
| 239 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | * FXSR floating point environment conversions. |
| 241 | */ |
| 242 | |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 243 | static void |
| 244 | convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | { |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 246 | struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave; |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 247 | struct _fpreg *to = (struct _fpreg *) &env->st_space[0]; |
| 248 | struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | int i; |
| 250 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 251 | env->cwd = fxsave->cwd | 0xffff0000u; |
| 252 | env->swd = fxsave->swd | 0xffff0000u; |
| 253 | env->twd = twd_fxsr_to_i387(fxsave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 255 | #ifdef CONFIG_X86_64 |
| 256 | env->fip = fxsave->rip; |
| 257 | env->foo = fxsave->rdp; |
| 258 | if (tsk == current) { |
| 259 | /* |
| 260 | * should be actually ds/cs at fpu exception time, but |
| 261 | * that information is not available in 64bit mode. |
| 262 | */ |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 263 | asm("mov %%ds, %[fos]" : [fos] "=r" (env->fos)); |
| 264 | asm("mov %%cs, %[fcs]" : [fcs] "=r" (env->fcs)); |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 265 | } else { |
| 266 | struct pt_regs *regs = task_pt_regs(tsk); |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 267 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 268 | env->fos = 0xffff0000 | tsk->thread.ds; |
| 269 | env->fcs = regs->cs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | } |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 271 | #else |
| 272 | env->fip = fxsave->fip; |
Jan Beulich | 609b529 | 2008-03-05 08:35:14 +0000 | [diff] [blame] | 273 | env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16); |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 274 | env->foo = fxsave->foo; |
| 275 | env->fos = fxsave->fos; |
| 276 | #endif |
| 277 | |
| 278 | for (i = 0; i < 8; ++i) |
| 279 | memcpy(&to[i], &from[i], sizeof(to[0])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 282 | static void convert_to_fxsr(struct task_struct *tsk, |
| 283 | const struct user_i387_ia32_struct *env) |
| 284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | { |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 286 | struct i387_fxsave_struct *fxsave = &tsk->thread.xstate->fxsave; |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 287 | struct _fpreg *from = (struct _fpreg *) &env->st_space[0]; |
| 288 | struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | int i; |
| 290 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 291 | fxsave->cwd = env->cwd; |
| 292 | fxsave->swd = env->swd; |
| 293 | fxsave->twd = twd_i387_to_fxsr(env->twd); |
| 294 | fxsave->fop = (u16) ((u32) env->fcs >> 16); |
| 295 | #ifdef CONFIG_X86_64 |
| 296 | fxsave->rip = env->fip; |
| 297 | fxsave->rdp = env->foo; |
| 298 | /* cs and ds ignored */ |
| 299 | #else |
| 300 | fxsave->fip = env->fip; |
| 301 | fxsave->fcs = (env->fcs & 0xffff); |
| 302 | fxsave->foo = env->foo; |
| 303 | fxsave->fos = env->fos; |
| 304 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 306 | for (i = 0; i < 8; ++i) |
| 307 | memcpy(&to[i], &from[i], sizeof(from[0])); |
| 308 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 310 | int fpregs_get(struct task_struct *target, const struct user_regset *regset, |
| 311 | unsigned int pos, unsigned int count, |
| 312 | void *kbuf, void __user *ubuf) |
| 313 | { |
| 314 | struct user_i387_ia32_struct env; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 316 | if (!HAVE_HWFP) |
| 317 | return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf); |
| 318 | |
Suresh Siddha | 18a8622 | 2008-03-03 13:01:08 -0800 | [diff] [blame] | 319 | init_fpu(target); |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 320 | |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 321 | if (!cpu_has_fxsr) { |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 322 | return user_regset_copyout(&pos, &count, &kbuf, &ubuf, |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 323 | &target->thread.xstate->fsave, 0, |
| 324 | -1); |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 325 | } |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 326 | |
| 327 | if (kbuf && pos == 0 && count == sizeof(env)) { |
| 328 | convert_from_fxsr(kbuf, target); |
| 329 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 331 | |
| 332 | convert_from_fxsr(&env, target); |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 333 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 334 | return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1); |
| 335 | } |
| 336 | |
| 337 | int fpregs_set(struct task_struct *target, const struct user_regset *regset, |
| 338 | unsigned int pos, unsigned int count, |
| 339 | const void *kbuf, const void __user *ubuf) |
| 340 | { |
| 341 | struct user_i387_ia32_struct env; |
| 342 | int ret; |
| 343 | |
| 344 | if (!HAVE_HWFP) |
| 345 | return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf); |
| 346 | |
Suresh Siddha | 18a8622 | 2008-03-03 13:01:08 -0800 | [diff] [blame] | 347 | init_fpu(target); |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 348 | set_stopped_child_used_math(target); |
| 349 | |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 350 | if (!cpu_has_fxsr) { |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 351 | return user_regset_copyin(&pos, &count, &kbuf, &ubuf, |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 352 | &target->thread.xstate->fsave, 0, -1); |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 353 | } |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 354 | |
| 355 | if (pos > 0 || count < sizeof(env)) |
| 356 | convert_from_fxsr(&env, target); |
| 357 | |
| 358 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1); |
| 359 | if (!ret) |
| 360 | convert_to_fxsr(target, &env); |
| 361 | |
| 362 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /* |
| 366 | * Signal frame handlers. |
| 367 | */ |
| 368 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 369 | static inline int save_i387_fsave(struct _fpstate_ia32 __user *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | { |
| 371 | struct task_struct *tsk = current; |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 372 | struct i387_fsave_struct *fp = &tsk->thread.xstate->fsave; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 374 | unlazy_fpu(tsk); |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 375 | fp->status = fp->swd; |
| 376 | if (__copy_to_user(buf, fp, sizeof(struct i387_fsave_struct))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | return -1; |
| 378 | return 1; |
| 379 | } |
| 380 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 381 | static int save_i387_fxsave(struct _fpstate_ia32 __user *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | { |
| 383 | struct task_struct *tsk = current; |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 384 | struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave; |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 385 | struct user_i387_ia32_struct env; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | int err = 0; |
| 387 | |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 388 | unlazy_fpu(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 390 | convert_from_fxsr(&env, tsk); |
| 391 | if (__copy_to_user(buf, &env, sizeof(env))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | return -1; |
| 393 | |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 394 | err |= __put_user(fx->swd, &buf->status); |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 395 | err |= __put_user(X86_FXSR_MAGIC, &buf->magic); |
| 396 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | return -1; |
| 398 | |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 399 | if (__copy_to_user(&buf->_fxsr_env[0], fx, |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 400 | sizeof(struct i387_fxsave_struct))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | return -1; |
| 402 | return 1; |
| 403 | } |
| 404 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 405 | int save_i387_ia32(struct _fpstate_ia32 __user *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | { |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 407 | if (!used_math()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | return 0; |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 409 | /* |
| 410 | * This will cause a "finit" to be triggered by the next |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | * attempted FPU operation by the 'current' process. |
| 412 | */ |
| 413 | clear_used_math(); |
| 414 | |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 415 | if (!HAVE_HWFP) { |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 416 | return fpregs_soft_get(current, NULL, |
| 417 | 0, sizeof(struct user_i387_ia32_struct), |
| 418 | NULL, buf) ? -1 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | } |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 420 | |
| 421 | if (cpu_has_fxsr) |
| 422 | return save_i387_fxsave(buf); |
| 423 | else |
| 424 | return save_i387_fsave(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 427 | static inline int restore_i387_fsave(struct _fpstate_ia32 __user *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | { |
| 429 | struct task_struct *tsk = current; |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 430 | |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 431 | clear_fpu(tsk); |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 432 | return __copy_from_user(&tsk->thread.xstate->fsave, buf, |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 433 | sizeof(struct i387_fsave_struct)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 436 | static int restore_i387_fxsave(struct _fpstate_ia32 __user *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | struct task_struct *tsk = current; |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 439 | struct user_i387_ia32_struct env; |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 440 | int err; |
| 441 | |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 442 | clear_fpu(tsk); |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 443 | err = __copy_from_user(&tsk->thread.xstate->fxsave, &buf->_fxsr_env[0], |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 444 | sizeof(struct i387_fxsave_struct)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | /* mxcsr reserved bits must be masked to zero for security reasons */ |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame^] | 446 | tsk->thread.xstate->fxsave.mxcsr &= mxcsr_feature_mask; |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 447 | if (err || __copy_from_user(&env, buf, sizeof(env))) |
| 448 | return 1; |
| 449 | convert_to_fxsr(tsk, &env); |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 450 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 451 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 454 | int restore_i387_ia32(struct _fpstate_ia32 __user *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | { |
| 456 | int err; |
| 457 | |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 458 | if (HAVE_HWFP) { |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 459 | if (cpu_has_fxsr) |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 460 | err = restore_i387_fxsave(buf); |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 461 | else |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 462 | err = restore_i387_fsave(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } else { |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 464 | err = fpregs_soft_set(current, NULL, |
| 465 | 0, sizeof(struct user_i387_ia32_struct), |
| 466 | NULL, buf) != 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | } |
| 468 | set_used_math(); |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | return err; |
| 471 | } |
| 472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | /* |
| 474 | * FPU state for core dumps. |
Roland McGrath | 60b3b9a | 2008-01-30 13:31:55 +0100 | [diff] [blame] | 475 | * This is only used for a.out dumps now. |
| 476 | * It is declared generically using elf_fpregset_t (which is |
| 477 | * struct user_i387_struct) but is in fact only used for 32-bit |
| 478 | * dumps, so on 64-bit it is really struct user_i387_ia32_struct. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | */ |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 480 | int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | struct task_struct *tsk = current; |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 483 | int fpvalid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | |
| 485 | fpvalid = !!used_math(); |
Roland McGrath | 60b3b9a | 2008-01-30 13:31:55 +0100 | [diff] [blame] | 486 | if (fpvalid) |
| 487 | fpvalid = !fpregs_get(tsk, NULL, |
| 488 | 0, sizeof(struct user_i387_ia32_struct), |
| 489 | fpu, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
| 491 | return fpvalid; |
| 492 | } |
Alexey Dobriyan | 129f694 | 2005-06-23 00:08:33 -0700 | [diff] [blame] | 493 | EXPORT_SYMBOL(dump_fpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | |
Roland McGrath | 60b3b9a | 2008-01-30 13:31:55 +0100 | [diff] [blame] | 495 | #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */ |