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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.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> |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 19 | #include <asm/fpu-internal.h> |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 20 | #include <asm/user.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Oleg Nesterov | 14e153e | 2015-01-15 20:19:43 +0100 | [diff] [blame] | 22 | static DEFINE_PER_CPU(bool, in_kernel_fpu); |
| 23 | |
Oleg Nesterov | 7575637 | 2015-01-15 20:20:28 +0100 | [diff] [blame^] | 24 | void kernel_fpu_disable(void) |
| 25 | { |
| 26 | WARN_ON(this_cpu_read(in_kernel_fpu)); |
| 27 | this_cpu_write(in_kernel_fpu, true); |
| 28 | } |
| 29 | |
| 30 | void kernel_fpu_enable(void) |
| 31 | { |
| 32 | this_cpu_write(in_kernel_fpu, false); |
| 33 | } |
| 34 | |
Linus Torvalds | 8546c00 | 2012-02-21 10:25:45 -0800 | [diff] [blame] | 35 | /* |
| 36 | * Were we in an interrupt that interrupted kernel mode? |
| 37 | * |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 38 | * On others, we can do a kernel_fpu_begin/end() pair *ONLY* if that |
Linus Torvalds | 8546c00 | 2012-02-21 10:25:45 -0800 | [diff] [blame] | 39 | * pair does nothing at all: the thread must not have fpu (so |
| 40 | * that we don't try to save the FPU state), and TS must |
| 41 | * be set (so that the clts/stts pair does nothing that is |
| 42 | * visible in the interrupted kernel thread). |
Pekka Riikonen | 5187b28 | 2013-05-13 14:32:07 +0200 | [diff] [blame] | 43 | * |
| 44 | * Except for the eagerfpu case when we return 1 unless we've already |
| 45 | * been eager and saved the state in kernel_fpu_begin(). |
Linus Torvalds | 8546c00 | 2012-02-21 10:25:45 -0800 | [diff] [blame] | 46 | */ |
| 47 | static inline bool interrupted_kernel_fpu_idle(void) |
| 48 | { |
Oleg Nesterov | 14e153e | 2015-01-15 20:19:43 +0100 | [diff] [blame] | 49 | if (this_cpu_read(in_kernel_fpu)) |
| 50 | return false; |
| 51 | |
Suresh Siddha | 5d2bd70 | 2012-09-06 14:58:52 -0700 | [diff] [blame] | 52 | if (use_eager_fpu()) |
Pekka Riikonen | 5187b28 | 2013-05-13 14:32:07 +0200 | [diff] [blame] | 53 | return __thread_has_fpu(current); |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 54 | |
Linus Torvalds | 8546c00 | 2012-02-21 10:25:45 -0800 | [diff] [blame] | 55 | return !__thread_has_fpu(current) && |
| 56 | (read_cr0() & X86_CR0_TS); |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Were we in user mode (or vm86 mode) when we were |
| 61 | * interrupted? |
| 62 | * |
| 63 | * Doing kernel_fpu_begin/end() is ok if we are running |
| 64 | * in an interrupt context from user mode - we'll just |
| 65 | * save the FPU state as required. |
| 66 | */ |
| 67 | static inline bool interrupted_user_mode(void) |
| 68 | { |
| 69 | struct pt_regs *regs = get_irq_regs(); |
| 70 | return regs && user_mode_vm(regs); |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Can we use the FPU in kernel mode with the |
| 75 | * whole "kernel_fpu_begin/end()" sequence? |
| 76 | * |
| 77 | * It's always ok in process context (ie "not interrupt") |
| 78 | * but it is sometimes ok even from an irq. |
| 79 | */ |
| 80 | bool irq_fpu_usable(void) |
| 81 | { |
| 82 | return !in_interrupt() || |
| 83 | interrupted_user_mode() || |
| 84 | interrupted_kernel_fpu_idle(); |
| 85 | } |
| 86 | EXPORT_SYMBOL(irq_fpu_usable); |
| 87 | |
Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 88 | void __kernel_fpu_begin(void) |
Linus Torvalds | 8546c00 | 2012-02-21 10:25:45 -0800 | [diff] [blame] | 89 | { |
| 90 | struct task_struct *me = current; |
| 91 | |
Oleg Nesterov | 14e153e | 2015-01-15 20:19:43 +0100 | [diff] [blame] | 92 | this_cpu_write(in_kernel_fpu, true); |
| 93 | |
Linus Torvalds | 8546c00 | 2012-02-21 10:25:45 -0800 | [diff] [blame] | 94 | if (__thread_has_fpu(me)) { |
Pekka Riikonen | 5187b28 | 2013-05-13 14:32:07 +0200 | [diff] [blame] | 95 | __save_init_fpu(me); |
Suresh Siddha | 5d2bd70 | 2012-09-06 14:58:52 -0700 | [diff] [blame] | 96 | } else if (!use_eager_fpu()) { |
Alex Shi | c6ae41e | 2012-05-11 15:35:27 +0800 | [diff] [blame] | 97 | this_cpu_write(fpu_owner_task, NULL); |
Linus Torvalds | 8546c00 | 2012-02-21 10:25:45 -0800 | [diff] [blame] | 98 | clts(); |
| 99 | } |
| 100 | } |
Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 101 | EXPORT_SYMBOL(__kernel_fpu_begin); |
Linus Torvalds | 8546c00 | 2012-02-21 10:25:45 -0800 | [diff] [blame] | 102 | |
Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 103 | void __kernel_fpu_end(void) |
Linus Torvalds | 8546c00 | 2012-02-21 10:25:45 -0800 | [diff] [blame] | 104 | { |
Oleg Nesterov | 33a3ebd | 2015-01-15 20:20:05 +0100 | [diff] [blame] | 105 | struct task_struct *me = current; |
| 106 | |
| 107 | if (__thread_has_fpu(me)) { |
| 108 | if (WARN_ON(restore_fpu_checking(me))) |
| 109 | drop_init_fpu(me); |
| 110 | } else if (!use_eager_fpu()) { |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 111 | stts(); |
Suresh Siddha | 731bd6a9 | 2014-02-02 22:56:23 -0800 | [diff] [blame] | 112 | } |
Oleg Nesterov | 14e153e | 2015-01-15 20:19:43 +0100 | [diff] [blame] | 113 | |
| 114 | this_cpu_write(in_kernel_fpu, false); |
Linus Torvalds | 8546c00 | 2012-02-21 10:25:45 -0800 | [diff] [blame] | 115 | } |
Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 116 | EXPORT_SYMBOL(__kernel_fpu_end); |
Linus Torvalds | 8546c00 | 2012-02-21 10:25:45 -0800 | [diff] [blame] | 117 | |
| 118 | void unlazy_fpu(struct task_struct *tsk) |
| 119 | { |
| 120 | preempt_disable(); |
| 121 | if (__thread_has_fpu(tsk)) { |
| 122 | __save_init_fpu(tsk); |
| 123 | __thread_fpu_end(tsk); |
| 124 | } else |
Vineet Gupta | c375f15 | 2013-11-12 15:08:46 -0800 | [diff] [blame] | 125 | tsk->thread.fpu_counter = 0; |
Linus Torvalds | 8546c00 | 2012-02-21 10:25:45 -0800 | [diff] [blame] | 126 | preempt_enable(); |
| 127 | } |
| 128 | EXPORT_SYMBOL(unlazy_fpu); |
| 129 | |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 130 | unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu; |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame] | 131 | unsigned int xstate_size; |
Xiaotian Feng | f45755b | 2010-08-13 15:19:11 +0800 | [diff] [blame] | 132 | EXPORT_SYMBOL_GPL(xstate_size); |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 133 | static struct i387_fxsave_struct fx_scratch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 135 | static void mxcsr_feature_mask_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
| 137 | unsigned long mask = 0; |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | if (cpu_has_fxsr) { |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame] | 140 | memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct)); |
H.J. Lu | eaa5a99 | 2013-07-26 09:11:56 -0700 | [diff] [blame] | 141 | asm volatile("fxsave %0" : "+m" (fx_scratch)); |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame] | 142 | mask = fx_scratch.mxcsr_mask; |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 143 | if (mask == 0) |
| 144 | mask = 0x0000ffbf; |
| 145 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | mxcsr_feature_mask &= mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 149 | static void init_thread_xstate(void) |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame] | 150 | { |
Robert Richter | 0e49bf6 | 2010-07-21 19:03:52 +0200 | [diff] [blame] | 151 | /* |
| 152 | * Note that xstate_size might be overwriten later during |
| 153 | * xsave_init(). |
| 154 | */ |
| 155 | |
H. Peter Anvin | 60e019e | 2013-04-29 16:04:20 +0200 | [diff] [blame] | 156 | if (!cpu_has_fpu) { |
Robert Richter | 1f999ab | 2010-07-21 19:03:57 +0200 | [diff] [blame] | 157 | /* |
| 158 | * Disable xsave as we do not support it if i387 |
| 159 | * emulation is enabled. |
| 160 | */ |
| 161 | setup_clear_cpu_cap(X86_FEATURE_XSAVE); |
| 162 | setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT); |
Suresh Siddha | e8a496a | 2008-05-23 16:26:37 -0700 | [diff] [blame] | 163 | xstate_size = sizeof(struct i387_soft_struct); |
| 164 | return; |
| 165 | } |
| 166 | |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame] | 167 | if (cpu_has_fxsr) |
| 168 | xstate_size = sizeof(struct i387_fxsave_struct); |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame] | 169 | else |
| 170 | xstate_size = sizeof(struct i387_fsave_struct); |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 173 | /* |
| 174 | * Called at bootup to set up the initial FPU state that is later cloned |
| 175 | * into all processes. |
| 176 | */ |
Robert Richter | 0e49bf6 | 2010-07-21 19:03:52 +0200 | [diff] [blame] | 177 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 178 | void fpu_init(void) |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 179 | { |
Brian Gerst | 6ac8bac | 2010-09-03 21:17:09 -0400 | [diff] [blame] | 180 | unsigned long cr0; |
| 181 | unsigned long cr4_mask = 0; |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 182 | |
H. Peter Anvin | 60e019e | 2013-04-29 16:04:20 +0200 | [diff] [blame] | 183 | #ifndef CONFIG_MATH_EMULATION |
| 184 | if (!cpu_has_fpu) { |
| 185 | pr_emerg("No FPU found and no math emulation present\n"); |
| 186 | pr_emerg("Giving up\n"); |
| 187 | for (;;) |
| 188 | asm volatile("hlt"); |
| 189 | } |
| 190 | #endif |
Brian Gerst | 6ac8bac | 2010-09-03 21:17:09 -0400 | [diff] [blame] | 191 | if (cpu_has_fxsr) |
| 192 | cr4_mask |= X86_CR4_OSFXSR; |
| 193 | if (cpu_has_xmm) |
| 194 | cr4_mask |= X86_CR4_OSXMMEXCPT; |
| 195 | if (cr4_mask) |
| 196 | set_in_cr4(cr4_mask); |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 197 | |
Brian Gerst | 6ac8bac | 2010-09-03 21:17:09 -0400 | [diff] [blame] | 198 | cr0 = read_cr0(); |
| 199 | cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */ |
H. Peter Anvin | 60e019e | 2013-04-29 16:04:20 +0200 | [diff] [blame] | 200 | if (!cpu_has_fpu) |
Brian Gerst | 6ac8bac | 2010-09-03 21:17:09 -0400 | [diff] [blame] | 201 | cr0 |= X86_CR0_EM; |
| 202 | write_cr0(cr0); |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 203 | |
Fenghua Yu | 6f5298c | 2012-11-13 11:32:50 -0800 | [diff] [blame] | 204 | /* |
| 205 | * init_thread_xstate is only called once to avoid overriding |
| 206 | * xstate_size during boot time or during CPU hotplug. |
| 207 | */ |
| 208 | if (xstate_size == 0) |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 209 | init_thread_xstate(); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 210 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 211 | mxcsr_feature_mask_init(); |
Suresh Siddha | 5d2bd70 | 2012-09-06 14:58:52 -0700 | [diff] [blame] | 212 | xsave_init(); |
| 213 | eager_fpu_init(); |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 214 | } |
Robert Richter | 0e49bf6 | 2010-07-21 19:03:52 +0200 | [diff] [blame] | 215 | |
Sheng Yang | 5ee481d | 2010-05-17 17:22:23 +0800 | [diff] [blame] | 216 | void fpu_finit(struct fpu *fpu) |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 217 | { |
H. Peter Anvin | 60e019e | 2013-04-29 16:04:20 +0200 | [diff] [blame] | 218 | if (!cpu_has_fpu) { |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 219 | finit_soft_fpu(&fpu->state->soft); |
| 220 | return; |
| 221 | } |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 222 | |
| 223 | if (cpu_has_fxsr) { |
Suresh Siddha | 5d2bd70 | 2012-09-06 14:58:52 -0700 | [diff] [blame] | 224 | fx_finit(&fpu->state->fxsave); |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 225 | } else { |
| 226 | struct i387_fsave_struct *fp = &fpu->state->fsave; |
| 227 | memset(fp, 0, xstate_size); |
| 228 | fp->cwd = 0xffff037fu; |
| 229 | fp->swd = 0xffff0000u; |
| 230 | fp->twd = 0xffffffffu; |
| 231 | fp->fos = 0xffff0000u; |
| 232 | } |
| 233 | } |
Sheng Yang | 5ee481d | 2010-05-17 17:22:23 +0800 | [diff] [blame] | 234 | EXPORT_SYMBOL_GPL(fpu_finit); |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | /* |
| 237 | * The _current_ task is using the FPU for the first time |
| 238 | * so initialize it and set the mxcsr to its default |
| 239 | * value at reset if we support XMM instructions and then |
Lucas De Marchi | 0d2eb44 | 2011-03-17 16:24:16 -0300 | [diff] [blame] | 240 | * remember the current task has used the FPU. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | */ |
Suresh Siddha | aa283f4 | 2008-03-10 15:28:05 -0700 | [diff] [blame] | 242 | int init_fpu(struct task_struct *tsk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | { |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 244 | int ret; |
| 245 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 246 | if (tsk_used_math(tsk)) { |
H. Peter Anvin | 60e019e | 2013-04-29 16:04:20 +0200 | [diff] [blame] | 247 | if (cpu_has_fpu && tsk == current) |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 248 | unlazy_fpu(tsk); |
Oleg Nesterov | 089f9fb | 2012-04-16 22:48:15 +0200 | [diff] [blame] | 249 | tsk->thread.fpu.last_cpu = ~0; |
Suresh Siddha | aa283f4 | 2008-03-10 15:28:05 -0700 | [diff] [blame] | 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Memory allocation at the first usage of the FPU and other state. |
| 255 | */ |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 256 | ret = fpu_alloc(&tsk->thread.fpu); |
| 257 | if (ret) |
| 258 | return ret; |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 259 | |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 260 | fpu_finit(&tsk->thread.fpu); |
Suresh Siddha | e8a496a | 2008-05-23 16:26:37 -0700 | [diff] [blame] | 261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | set_stopped_child_used_math(tsk); |
Suresh Siddha | aa283f4 | 2008-03-10 15:28:05 -0700 | [diff] [blame] | 263 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
Avi Kivity | e5c3014 | 2011-01-11 12:15:54 +0200 | [diff] [blame] | 265 | EXPORT_SYMBOL_GPL(init_fpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Suresh Siddha | 5b3efd5 | 2010-02-11 11:50:59 -0800 | [diff] [blame] | 267 | /* |
| 268 | * The xstateregs_active() routine is the same as the fpregs_active() routine, |
| 269 | * as the "regset->n" for the xstate regset will be updated based on the feature |
| 270 | * capabilites supported by the xsave. |
| 271 | */ |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 272 | int fpregs_active(struct task_struct *target, const struct user_regset *regset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | { |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 274 | return tsk_used_math(target) ? regset->n : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | } |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 276 | |
| 277 | int xfpregs_active(struct task_struct *target, const struct user_regset *regset) |
| 278 | { |
| 279 | return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0; |
| 280 | } |
| 281 | |
| 282 | int xfpregs_get(struct task_struct *target, const struct user_regset *regset, |
| 283 | unsigned int pos, unsigned int count, |
| 284 | void *kbuf, void __user *ubuf) |
| 285 | { |
Suresh Siddha | aa283f4 | 2008-03-10 15:28:05 -0700 | [diff] [blame] | 286 | int ret; |
| 287 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 288 | if (!cpu_has_fxsr) |
| 289 | return -ENODEV; |
| 290 | |
Suresh Siddha | aa283f4 | 2008-03-10 15:28:05 -0700 | [diff] [blame] | 291 | ret = init_fpu(target); |
| 292 | if (ret) |
| 293 | return ret; |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 294 | |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 295 | sanitize_i387_state(target); |
| 296 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 297 | return user_regset_copyout(&pos, &count, &kbuf, &ubuf, |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 298 | &target->thread.fpu.state->fxsave, 0, -1); |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | int xfpregs_set(struct task_struct *target, const struct user_regset *regset, |
| 302 | unsigned int pos, unsigned int count, |
| 303 | const void *kbuf, const void __user *ubuf) |
| 304 | { |
| 305 | int ret; |
| 306 | |
| 307 | if (!cpu_has_fxsr) |
| 308 | return -ENODEV; |
| 309 | |
Suresh Siddha | aa283f4 | 2008-03-10 15:28:05 -0700 | [diff] [blame] | 310 | ret = init_fpu(target); |
| 311 | if (ret) |
| 312 | return ret; |
| 313 | |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 314 | sanitize_i387_state(target); |
| 315 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 316 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 317 | &target->thread.fpu.state->fxsave, 0, -1); |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 318 | |
| 319 | /* |
| 320 | * mxcsr reserved bits must be masked to zero for security reasons. |
| 321 | */ |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 322 | target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask; |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 323 | |
Suresh Siddha | 42deec6 | 2008-07-29 10:29:26 -0700 | [diff] [blame] | 324 | /* |
| 325 | * update the header bits in the xsave header, indicating the |
| 326 | * presence of FP and SSE state. |
| 327 | */ |
| 328 | if (cpu_has_xsave) |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 329 | target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FPSSE; |
Suresh Siddha | 42deec6 | 2008-07-29 10:29:26 -0700 | [diff] [blame] | 330 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 331 | return ret; |
| 332 | } |
| 333 | |
Suresh Siddha | 5b3efd5 | 2010-02-11 11:50:59 -0800 | [diff] [blame] | 334 | int xstateregs_get(struct task_struct *target, const struct user_regset *regset, |
| 335 | unsigned int pos, unsigned int count, |
| 336 | void *kbuf, void __user *ubuf) |
| 337 | { |
| 338 | int ret; |
| 339 | |
| 340 | if (!cpu_has_xsave) |
| 341 | return -ENODEV; |
| 342 | |
| 343 | ret = init_fpu(target); |
| 344 | if (ret) |
| 345 | return ret; |
| 346 | |
| 347 | /* |
Suresh Siddha | ff7fbc7 | 2010-02-22 14:51:33 -0800 | [diff] [blame] | 348 | * Copy the 48bytes defined by the software first into the xstate |
| 349 | * memory layout in the thread struct, so that we can copy the entire |
| 350 | * xstateregs to the user using one user_regset_copyout(). |
Suresh Siddha | 5b3efd5 | 2010-02-11 11:50:59 -0800 | [diff] [blame] | 351 | */ |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 352 | memcpy(&target->thread.fpu.state->fxsave.sw_reserved, |
Suresh Siddha | ff7fbc7 | 2010-02-22 14:51:33 -0800 | [diff] [blame] | 353 | xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes)); |
Suresh Siddha | 5b3efd5 | 2010-02-11 11:50:59 -0800 | [diff] [blame] | 354 | |
| 355 | /* |
Suresh Siddha | ff7fbc7 | 2010-02-22 14:51:33 -0800 | [diff] [blame] | 356 | * Copy the xstate memory layout. |
Suresh Siddha | 5b3efd5 | 2010-02-11 11:50:59 -0800 | [diff] [blame] | 357 | */ |
| 358 | ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 359 | &target->thread.fpu.state->xsave, 0, -1); |
Suresh Siddha | 5b3efd5 | 2010-02-11 11:50:59 -0800 | [diff] [blame] | 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | int xstateregs_set(struct task_struct *target, const struct user_regset *regset, |
| 364 | unsigned int pos, unsigned int count, |
| 365 | const void *kbuf, const void __user *ubuf) |
| 366 | { |
| 367 | int ret; |
| 368 | struct xsave_hdr_struct *xsave_hdr; |
| 369 | |
| 370 | if (!cpu_has_xsave) |
| 371 | return -ENODEV; |
| 372 | |
| 373 | ret = init_fpu(target); |
| 374 | if (ret) |
| 375 | return ret; |
| 376 | |
| 377 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 378 | &target->thread.fpu.state->xsave, 0, -1); |
Suresh Siddha | 5b3efd5 | 2010-02-11 11:50:59 -0800 | [diff] [blame] | 379 | |
| 380 | /* |
| 381 | * mxcsr reserved bits must be masked to zero for security reasons. |
| 382 | */ |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 383 | target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask; |
Suresh Siddha | 5b3efd5 | 2010-02-11 11:50:59 -0800 | [diff] [blame] | 384 | |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 385 | xsave_hdr = &target->thread.fpu.state->xsave.xsave_hdr; |
Suresh Siddha | 5b3efd5 | 2010-02-11 11:50:59 -0800 | [diff] [blame] | 386 | |
| 387 | xsave_hdr->xstate_bv &= pcntxt_mask; |
| 388 | /* |
| 389 | * These bits must be zero. |
| 390 | */ |
Fenghua Yu | 21e726c | 2014-05-29 11:12:39 -0700 | [diff] [blame] | 391 | memset(xsave_hdr->reserved, 0, 48); |
Suresh Siddha | 5b3efd5 | 2010-02-11 11:50:59 -0800 | [diff] [blame] | 392 | |
| 393 | return ret; |
| 394 | } |
| 395 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 396 | #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | /* |
| 399 | * FPU tag word conversions. |
| 400 | */ |
| 401 | |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 402 | static inline unsigned short twd_i387_to_fxsr(unsigned short twd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | { |
| 404 | unsigned int tmp; /* to avoid 16 bit prefixes in the code */ |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | /* Transform each pair of bits into 01 (valid) or 00 (empty) */ |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 407 | tmp = ~twd; |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 408 | tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */ |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 409 | /* and move the valid bits to the lower byte. */ |
| 410 | tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */ |
| 411 | tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */ |
| 412 | tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */ |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 413 | |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 414 | return tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Phil Carmody | 497888c | 2011-07-14 15:07:13 +0300 | [diff] [blame] | 417 | #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16) |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 418 | #define FP_EXP_TAG_VALID 0 |
| 419 | #define FP_EXP_TAG_ZERO 1 |
| 420 | #define FP_EXP_TAG_SPECIAL 2 |
| 421 | #define FP_EXP_TAG_EMPTY 3 |
| 422 | |
| 423 | static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | { |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 425 | struct _fpxreg *st; |
| 426 | u32 tos = (fxsave->swd >> 11) & 7; |
| 427 | u32 twd = (unsigned long) fxsave->twd; |
| 428 | u32 tag; |
| 429 | u32 ret = 0xffff0000u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | int i; |
| 431 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 432 | for (i = 0; i < 8; i++, twd >>= 1) { |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 433 | if (twd & 0x1) { |
| 434 | st = FPREG_ADDR(fxsave, (i - tos) & 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 436 | switch (st->exponent & 0x7fff) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | case 0x7fff: |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 438 | tag = FP_EXP_TAG_SPECIAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | break; |
| 440 | case 0x0000: |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 441 | if (!st->significand[0] && |
| 442 | !st->significand[1] && |
| 443 | !st->significand[2] && |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 444 | !st->significand[3]) |
| 445 | tag = FP_EXP_TAG_ZERO; |
| 446 | else |
| 447 | tag = FP_EXP_TAG_SPECIAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | break; |
| 449 | default: |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 450 | if (st->significand[3] & 0x8000) |
| 451 | tag = FP_EXP_TAG_VALID; |
| 452 | else |
| 453 | tag = FP_EXP_TAG_SPECIAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | break; |
| 455 | } |
| 456 | } else { |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 457 | tag = FP_EXP_TAG_EMPTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | } |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 459 | ret |= tag << (2 * i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | } |
| 461 | return ret; |
| 462 | } |
| 463 | |
| 464 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | * FXSR floating point environment conversions. |
| 466 | */ |
| 467 | |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 468 | void |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 469 | 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] | 470 | { |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 471 | struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave; |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 472 | struct _fpreg *to = (struct _fpreg *) &env->st_space[0]; |
| 473 | struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | int i; |
| 475 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 476 | env->cwd = fxsave->cwd | 0xffff0000u; |
| 477 | env->swd = fxsave->swd | 0xffff0000u; |
| 478 | env->twd = twd_fxsr_to_i387(fxsave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 480 | #ifdef CONFIG_X86_64 |
| 481 | env->fip = fxsave->rip; |
| 482 | env->foo = fxsave->rdp; |
Brian Gerst | 10c11f3 | 2010-09-03 21:17:13 -0400 | [diff] [blame] | 483 | /* |
| 484 | * should be actually ds/cs at fpu exception time, but |
| 485 | * that information is not available in 64bit mode. |
| 486 | */ |
| 487 | env->fcs = task_pt_regs(tsk)->cs; |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 488 | if (tsk == current) { |
Brian Gerst | 10c11f3 | 2010-09-03 21:17:13 -0400 | [diff] [blame] | 489 | savesegment(ds, env->fos); |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 490 | } else { |
Brian Gerst | 10c11f3 | 2010-09-03 21:17:13 -0400 | [diff] [blame] | 491 | env->fos = tsk->thread.ds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
Brian Gerst | 10c11f3 | 2010-09-03 21:17:13 -0400 | [diff] [blame] | 493 | env->fos |= 0xffff0000; |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 494 | #else |
| 495 | env->fip = fxsave->fip; |
Jan Beulich | 609b529 | 2008-03-05 08:35:14 +0000 | [diff] [blame] | 496 | env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16); |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 497 | env->foo = fxsave->foo; |
| 498 | env->fos = fxsave->fos; |
| 499 | #endif |
| 500 | |
| 501 | for (i = 0; i < 8; ++i) |
| 502 | memcpy(&to[i], &from[i], sizeof(to[0])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 505 | void convert_to_fxsr(struct task_struct *tsk, |
| 506 | const struct user_i387_ia32_struct *env) |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 507 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | { |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 509 | struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave; |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 510 | struct _fpreg *from = (struct _fpreg *) &env->st_space[0]; |
| 511 | struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | int i; |
| 513 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 514 | fxsave->cwd = env->cwd; |
| 515 | fxsave->swd = env->swd; |
| 516 | fxsave->twd = twd_i387_to_fxsr(env->twd); |
| 517 | fxsave->fop = (u16) ((u32) env->fcs >> 16); |
| 518 | #ifdef CONFIG_X86_64 |
| 519 | fxsave->rip = env->fip; |
| 520 | fxsave->rdp = env->foo; |
| 521 | /* cs and ds ignored */ |
| 522 | #else |
| 523 | fxsave->fip = env->fip; |
| 524 | fxsave->fcs = (env->fcs & 0xffff); |
| 525 | fxsave->foo = env->foo; |
| 526 | fxsave->fos = env->fos; |
| 527 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 529 | for (i = 0; i < 8; ++i) |
| 530 | memcpy(&to[i], &from[i], sizeof(from[0])); |
| 531 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 533 | int fpregs_get(struct task_struct *target, const struct user_regset *regset, |
| 534 | unsigned int pos, unsigned int count, |
| 535 | void *kbuf, void __user *ubuf) |
| 536 | { |
| 537 | struct user_i387_ia32_struct env; |
Suresh Siddha | aa283f4 | 2008-03-10 15:28:05 -0700 | [diff] [blame] | 538 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
Suresh Siddha | aa283f4 | 2008-03-10 15:28:05 -0700 | [diff] [blame] | 540 | ret = init_fpu(target); |
| 541 | if (ret) |
| 542 | return ret; |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 543 | |
H. Peter Anvin | 60e019e | 2013-04-29 16:04:20 +0200 | [diff] [blame] | 544 | if (!static_cpu_has(X86_FEATURE_FPU)) |
Suresh Siddha | e8a496a | 2008-05-23 16:26:37 -0700 | [diff] [blame] | 545 | return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf); |
| 546 | |
H. Peter Anvin | 60e019e | 2013-04-29 16:04:20 +0200 | [diff] [blame] | 547 | if (!cpu_has_fxsr) |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 548 | return user_regset_copyout(&pos, &count, &kbuf, &ubuf, |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 549 | &target->thread.fpu.state->fsave, 0, |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame] | 550 | -1); |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 551 | |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 552 | sanitize_i387_state(target); |
| 553 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 554 | if (kbuf && pos == 0 && count == sizeof(env)) { |
| 555 | convert_from_fxsr(kbuf, target); |
| 556 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | } |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 558 | |
| 559 | convert_from_fxsr(&env, target); |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 560 | |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 561 | return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1); |
| 562 | } |
| 563 | |
| 564 | int fpregs_set(struct task_struct *target, const struct user_regset *regset, |
| 565 | unsigned int pos, unsigned int count, |
| 566 | const void *kbuf, const void __user *ubuf) |
| 567 | { |
| 568 | struct user_i387_ia32_struct env; |
| 569 | int ret; |
| 570 | |
Suresh Siddha | aa283f4 | 2008-03-10 15:28:05 -0700 | [diff] [blame] | 571 | ret = init_fpu(target); |
| 572 | if (ret) |
| 573 | return ret; |
| 574 | |
Suresh Siddha | 29104e1 | 2010-07-19 16:05:49 -0700 | [diff] [blame] | 575 | sanitize_i387_state(target); |
| 576 | |
H. Peter Anvin | 60e019e | 2013-04-29 16:04:20 +0200 | [diff] [blame] | 577 | if (!static_cpu_has(X86_FEATURE_FPU)) |
Suresh Siddha | e8a496a | 2008-05-23 16:26:37 -0700 | [diff] [blame] | 578 | return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf); |
| 579 | |
H. Peter Anvin | 60e019e | 2013-04-29 16:04:20 +0200 | [diff] [blame] | 580 | if (!cpu_has_fxsr) |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 581 | return user_regset_copyin(&pos, &count, &kbuf, &ubuf, |
H. Peter Anvin | 60e019e | 2013-04-29 16:04:20 +0200 | [diff] [blame] | 582 | &target->thread.fpu.state->fsave, 0, |
| 583 | -1); |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 584 | |
| 585 | if (pos > 0 || count < sizeof(env)) |
| 586 | convert_from_fxsr(&env, target); |
| 587 | |
| 588 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1); |
| 589 | if (!ret) |
| 590 | convert_to_fxsr(target, &env); |
| 591 | |
Suresh Siddha | 42deec6 | 2008-07-29 10:29:26 -0700 | [diff] [blame] | 592 | /* |
| 593 | * update the header bit in the xsave header, indicating the |
| 594 | * presence of FP. |
| 595 | */ |
| 596 | if (cpu_has_xsave) |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 597 | target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FP; |
Roland McGrath | 4421011 | 2008-01-30 13:31:50 +0100 | [diff] [blame] | 598 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | * FPU state for core dumps. |
Roland McGrath | 60b3b9a | 2008-01-30 13:31:55 +0100 | [diff] [blame] | 603 | * This is only used for a.out dumps now. |
| 604 | * It is declared generically using elf_fpregset_t (which is |
| 605 | * struct user_i387_struct) but is in fact only used for 32-bit |
| 606 | * 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] | 607 | */ |
Cyrill Gorcunov | 3b095a0 | 2008-01-30 13:31:26 +0100 | [diff] [blame] | 608 | int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | struct task_struct *tsk = current; |
Ingo Molnar | f668964 | 2008-03-05 15:37:32 +0100 | [diff] [blame] | 611 | int fpvalid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
| 613 | fpvalid = !!used_math(); |
Roland McGrath | 60b3b9a | 2008-01-30 13:31:55 +0100 | [diff] [blame] | 614 | if (fpvalid) |
| 615 | fpvalid = !fpregs_get(tsk, NULL, |
| 616 | 0, sizeof(struct user_i387_ia32_struct), |
| 617 | fpu, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
| 619 | return fpvalid; |
| 620 | } |
Alexey Dobriyan | 129f694 | 2005-06-23 00:08:33 -0700 | [diff] [blame] | 621 | EXPORT_SYMBOL(dump_fpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | |
Roland McGrath | 60b3b9a | 2008-01-30 13:31:55 +0100 | [diff] [blame] | 623 | #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */ |
H. Peter Anvin | 60e019e | 2013-04-29 16:04:20 +0200 | [diff] [blame] | 624 | |
| 625 | static int __init no_387(char *s) |
| 626 | { |
| 627 | setup_clear_cpu_cap(X86_FEATURE_FPU); |
| 628 | return 1; |
| 629 | } |
| 630 | |
| 631 | __setup("no387", no_387); |
| 632 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 633 | void fpu_detect(struct cpuinfo_x86 *c) |
H. Peter Anvin | 60e019e | 2013-04-29 16:04:20 +0200 | [diff] [blame] | 634 | { |
| 635 | unsigned long cr0; |
| 636 | u16 fsw, fcw; |
| 637 | |
| 638 | fsw = fcw = 0xffff; |
| 639 | |
| 640 | cr0 = read_cr0(); |
| 641 | cr0 &= ~(X86_CR0_TS | X86_CR0_EM); |
| 642 | write_cr0(cr0); |
| 643 | |
| 644 | asm volatile("fninit ; fnstsw %0 ; fnstcw %1" |
| 645 | : "+m" (fsw), "+m" (fcw)); |
| 646 | |
| 647 | if (fsw == 0 && (fcw & 0x103f) == 0x003f) |
| 648 | set_cpu_cap(c, X86_FEATURE_FPU); |
| 649 | else |
| 650 | clear_cpu_cap(c, X86_FEATURE_FPU); |
| 651 | |
| 652 | /* The final cr0 value is set in fpu_init() */ |
| 653 | } |