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