Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 1 | /* |
| 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 | * x86-64 work by Andi Kleen 2002 |
| 8 | */ |
| 9 | |
Ingo Molnar | 78f7f1e | 2015-04-24 02:54:44 +0200 | [diff] [blame] | 10 | #ifndef _ASM_X86_FPU_INTERNAL_H |
| 11 | #define _ASM_X86_FPU_INTERNAL_H |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 12 | |
Suresh Siddha | 050902c | 2012-07-24 16:05:27 -0700 | [diff] [blame] | 13 | #include <linux/compat.h> |
Ingo Molnar | 952f07e | 2015-04-26 16:56:05 +0200 | [diff] [blame] | 14 | #include <linux/sched.h> |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 15 | #include <linux/slab.h> |
Ingo Molnar | f89e32e | 2015-04-22 10:58:10 +0200 | [diff] [blame] | 16 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 17 | #include <asm/user.h> |
Ingo Molnar | df6b35f | 2015-04-24 02:46:00 +0200 | [diff] [blame] | 18 | #include <asm/fpu/api.h> |
Ingo Molnar | 669ebab | 2015-04-28 08:41:33 +0200 | [diff] [blame] | 19 | #include <asm/fpu/xstate.h> |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 20 | |
Ingo Molnar | 6ffc152 | 2015-04-29 20:24:14 +0200 | [diff] [blame] | 21 | /* |
| 22 | * High level FPU state handling functions: |
| 23 | */ |
Ingo Molnar | 0c306bc | 2015-04-30 12:59:30 +0200 | [diff] [blame] | 24 | extern void fpu__activate_curr(struct fpu *fpu); |
Ingo Molnar | 05602812 | 2015-05-27 12:22:29 +0200 | [diff] [blame] | 25 | extern void fpu__activate_fpstate_read(struct fpu *fpu); |
Ingo Molnar | 6a81d7e | 2015-05-27 12:22:29 +0200 | [diff] [blame^] | 26 | extern void fpu__activate_fpstate_write(struct fpu *fpu); |
Ingo Molnar | 6ffc152 | 2015-04-29 20:24:14 +0200 | [diff] [blame] | 27 | extern void fpu__save(struct fpu *fpu); |
Ingo Molnar | e1884d6 | 2015-05-04 11:49:58 +0200 | [diff] [blame] | 28 | extern void fpu__restore(struct fpu *fpu); |
Ingo Molnar | 82c0e45 | 2015-04-29 21:09:18 +0200 | [diff] [blame] | 29 | extern int fpu__restore_sig(void __user *buf, int ia32_frame); |
Ingo Molnar | 6ffc152 | 2015-04-29 20:24:14 +0200 | [diff] [blame] | 30 | extern void fpu__drop(struct fpu *fpu); |
| 31 | extern int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu); |
Ingo Molnar | 04c8e01 | 2015-04-29 20:35:33 +0200 | [diff] [blame] | 32 | extern void fpu__clear(struct fpu *fpu); |
Ingo Molnar | b1b64dc | 2015-05-05 15:56:33 +0200 | [diff] [blame] | 33 | extern int fpu__exception_code(struct fpu *fpu, int trap_nr); |
| 34 | extern int dump_fpu(struct pt_regs *ptregs, struct user_i387_struct *fpstate); |
Ingo Molnar | 6ffc152 | 2015-04-29 20:24:14 +0200 | [diff] [blame] | 35 | |
Ingo Molnar | b1b64dc | 2015-05-05 15:56:33 +0200 | [diff] [blame] | 36 | /* |
| 37 | * Boot time FPU initialization functions: |
| 38 | */ |
| 39 | extern void fpu__init_cpu(void); |
| 40 | extern void fpu__init_system_xstate(void); |
| 41 | extern void fpu__init_cpu_xstate(void); |
| 42 | extern void fpu__init_system(struct cpuinfo_x86 *c); |
Ingo Molnar | 952f07e | 2015-04-26 16:56:05 +0200 | [diff] [blame] | 43 | extern void fpu__init_check_bugs(void); |
| 44 | extern void fpu__resume_cpu(void); |
| 45 | |
Ingo Molnar | e97131a | 2015-05-05 11:34:49 +0200 | [diff] [blame] | 46 | /* |
| 47 | * Debugging facility: |
| 48 | */ |
| 49 | #ifdef CONFIG_X86_DEBUG_FPU |
| 50 | # define WARN_ON_FPU(x) WARN_ON_ONCE(x) |
| 51 | #else |
| 52 | # define WARN_ON_FPU(x) ({ 0; }) |
| 53 | #endif |
| 54 | |
Rik van Riel | 1c927ee | 2015-02-06 15:02:01 -0500 | [diff] [blame] | 55 | /* |
Ingo Molnar | b1b64dc | 2015-05-05 15:56:33 +0200 | [diff] [blame] | 56 | * FPU related CPU feature flag helper routines: |
Rik van Riel | 1c927ee | 2015-02-06 15:02:01 -0500 | [diff] [blame] | 57 | */ |
Suresh Siddha | 5d2bd70 | 2012-09-06 14:58:52 -0700 | [diff] [blame] | 58 | static __always_inline __pure bool use_eager_fpu(void) |
| 59 | { |
Matt Fleming | c6b4069 | 2014-03-27 15:10:40 -0700 | [diff] [blame] | 60 | return static_cpu_has_safe(X86_FEATURE_EAGER_FPU); |
Suresh Siddha | 5d2bd70 | 2012-09-06 14:58:52 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 63 | static __always_inline __pure bool use_xsaveopt(void) |
| 64 | { |
Matt Fleming | c6b4069 | 2014-03-27 15:10:40 -0700 | [diff] [blame] | 65 | return static_cpu_has_safe(X86_FEATURE_XSAVEOPT); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static __always_inline __pure bool use_xsave(void) |
| 69 | { |
Matt Fleming | c6b4069 | 2014-03-27 15:10:40 -0700 | [diff] [blame] | 70 | return static_cpu_has_safe(X86_FEATURE_XSAVE); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static __always_inline __pure bool use_fxsr(void) |
| 74 | { |
Matt Fleming | c6b4069 | 2014-03-27 15:10:40 -0700 | [diff] [blame] | 75 | return static_cpu_has_safe(X86_FEATURE_FXSR); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Ingo Molnar | b1b64dc | 2015-05-05 15:56:33 +0200 | [diff] [blame] | 78 | /* |
| 79 | * fpstate handling functions: |
| 80 | */ |
| 81 | |
| 82 | extern union fpregs_state init_fpstate; |
| 83 | |
| 84 | extern void fpstate_init(union fpregs_state *state); |
| 85 | #ifdef CONFIG_MATH_EMULATION |
| 86 | extern void fpstate_init_soft(struct swregs_state *soft); |
| 87 | #else |
| 88 | static inline void fpstate_init_soft(struct swregs_state *soft) {} |
| 89 | #endif |
| 90 | static inline void fpstate_init_fxstate(struct fxregs_state *fx) |
| 91 | { |
| 92 | fx->cwd = 0x37f; |
| 93 | fx->mxcsr = MXCSR_DEFAULT; |
| 94 | } |
Ingo Molnar | 36e49e7f | 2015-04-28 11:25:02 +0200 | [diff] [blame] | 95 | extern void fpstate_sanitize_xstate(struct fpu *fpu); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 96 | |
H. Peter Anvin | 49b8c69 | 2012-09-21 17:18:44 -0700 | [diff] [blame] | 97 | #define user_insn(insn, output, input...) \ |
| 98 | ({ \ |
| 99 | int err; \ |
| 100 | asm volatile(ASM_STAC "\n" \ |
| 101 | "1:" #insn "\n\t" \ |
| 102 | "2: " ASM_CLAC "\n" \ |
| 103 | ".section .fixup,\"ax\"\n" \ |
| 104 | "3: movl $-1,%[err]\n" \ |
| 105 | " jmp 2b\n" \ |
| 106 | ".previous\n" \ |
| 107 | _ASM_EXTABLE(1b, 3b) \ |
| 108 | : [err] "=r" (err), output \ |
| 109 | : "0"(0), input); \ |
| 110 | err; \ |
| 111 | }) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 112 | |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 113 | #define check_insn(insn, output, input...) \ |
| 114 | ({ \ |
| 115 | int err; \ |
| 116 | asm volatile("1:" #insn "\n\t" \ |
| 117 | "2:\n" \ |
| 118 | ".section .fixup,\"ax\"\n" \ |
| 119 | "3: movl $-1,%[err]\n" \ |
| 120 | " jmp 2b\n" \ |
| 121 | ".previous\n" \ |
| 122 | _ASM_EXTABLE(1b, 3b) \ |
| 123 | : [err] "=r" (err), output \ |
| 124 | : "0"(0), input); \ |
| 125 | err; \ |
| 126 | }) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 127 | |
Ingo Molnar | c47ada3 | 2015-04-30 17:15:32 +0200 | [diff] [blame] | 128 | static inline int copy_fregs_to_user(struct fregs_state __user *fx) |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 129 | { |
H. Peter Anvin | 49b8c69 | 2012-09-21 17:18:44 -0700 | [diff] [blame] | 130 | return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx)); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Ingo Molnar | c47ada3 | 2015-04-30 17:15:32 +0200 | [diff] [blame] | 133 | static inline int copy_fxregs_to_user(struct fxregs_state __user *fx) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 134 | { |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 135 | if (config_enabled(CONFIG_X86_32)) |
H. Peter Anvin | 49b8c69 | 2012-09-21 17:18:44 -0700 | [diff] [blame] | 136 | return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx)); |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 137 | else if (config_enabled(CONFIG_AS_FXSAVEQ)) |
H. Peter Anvin | 49b8c69 | 2012-09-21 17:18:44 -0700 | [diff] [blame] | 138 | return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx)); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 139 | |
Ingo Molnar | c681314 | 2015-04-30 11:34:09 +0200 | [diff] [blame] | 140 | /* See comment in copy_fxregs_to_kernel() below. */ |
H. Peter Anvin | 49b8c69 | 2012-09-21 17:18:44 -0700 | [diff] [blame] | 141 | return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx)); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Ingo Molnar | c47ada3 | 2015-04-30 17:15:32 +0200 | [diff] [blame] | 144 | static inline int copy_kernel_to_fxregs(struct fxregs_state *fx) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 145 | { |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 146 | if (config_enabled(CONFIG_X86_32)) |
| 147 | return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx)); |
| 148 | else if (config_enabled(CONFIG_AS_FXSAVEQ)) |
| 149 | return check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx)); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 150 | |
Ingo Molnar | c681314 | 2015-04-30 11:34:09 +0200 | [diff] [blame] | 151 | /* See comment in copy_fxregs_to_kernel() below. */ |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 152 | return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx), |
| 153 | "m" (*fx)); |
| 154 | } |
| 155 | |
Ingo Molnar | c47ada3 | 2015-04-30 17:15:32 +0200 | [diff] [blame] | 156 | static inline int copy_user_to_fxregs(struct fxregs_state __user *fx) |
H. Peter Anvin | e139e95 | 2012-09-25 15:42:18 -0700 | [diff] [blame] | 157 | { |
| 158 | if (config_enabled(CONFIG_X86_32)) |
| 159 | return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx)); |
| 160 | else if (config_enabled(CONFIG_AS_FXSAVEQ)) |
| 161 | return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx)); |
| 162 | |
Ingo Molnar | c681314 | 2015-04-30 11:34:09 +0200 | [diff] [blame] | 163 | /* See comment in copy_fxregs_to_kernel() below. */ |
H. Peter Anvin | e139e95 | 2012-09-25 15:42:18 -0700 | [diff] [blame] | 164 | return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx), |
| 165 | "m" (*fx)); |
| 166 | } |
| 167 | |
Ingo Molnar | c47ada3 | 2015-04-30 17:15:32 +0200 | [diff] [blame] | 168 | static inline int copy_kernel_to_fregs(struct fregs_state *fx) |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 169 | { |
| 170 | return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx)); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Ingo Molnar | c47ada3 | 2015-04-30 17:15:32 +0200 | [diff] [blame] | 173 | static inline int copy_user_to_fregs(struct fregs_state __user *fx) |
H. Peter Anvin | e139e95 | 2012-09-25 15:42:18 -0700 | [diff] [blame] | 174 | { |
| 175 | return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx)); |
| 176 | } |
| 177 | |
Ingo Molnar | c681314 | 2015-04-30 11:34:09 +0200 | [diff] [blame] | 178 | static inline void copy_fxregs_to_kernel(struct fpu *fpu) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 179 | { |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 180 | if (config_enabled(CONFIG_X86_32)) |
Ingo Molnar | 7366ed7 | 2015-04-27 04:19:39 +0200 | [diff] [blame] | 181 | asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state.fxsave)); |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 182 | else if (config_enabled(CONFIG_AS_FXSAVEQ)) |
Ingo Molnar | 7366ed7 | 2015-04-27 04:19:39 +0200 | [diff] [blame] | 183 | asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state.fxsave)); |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 184 | else { |
| 185 | /* Using "rex64; fxsave %0" is broken because, if the memory |
| 186 | * operand uses any extended registers for addressing, a second |
| 187 | * REX prefix will be generated (to the assembler, rex64 |
| 188 | * followed by semicolon is a separate instruction), and hence |
| 189 | * the 64-bitness is lost. |
| 190 | * |
| 191 | * Using "fxsaveq %0" would be the ideal choice, but is only |
| 192 | * supported starting with gas 2.16. |
| 193 | * |
| 194 | * Using, as a workaround, the properly prefixed form below |
| 195 | * isn't accepted by any binutils version so far released, |
| 196 | * complaining that the same type of prefix is used twice if |
| 197 | * an extended register is needed for addressing (fix submitted |
| 198 | * to mainline 2005-11-21). |
| 199 | * |
Ingo Molnar | 7366ed7 | 2015-04-27 04:19:39 +0200 | [diff] [blame] | 200 | * asm volatile("rex64/fxsave %0" : "=m" (fpu->state.fxsave)); |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 201 | * |
| 202 | * This, however, we can work around by forcing the compiler to |
| 203 | * select an addressing mode that doesn't require extended |
| 204 | * registers. |
| 205 | */ |
| 206 | asm volatile( "rex64/fxsave (%[fx])" |
Ingo Molnar | 7366ed7 | 2015-04-27 04:19:39 +0200 | [diff] [blame] | 207 | : "=m" (fpu->state.fxsave) |
| 208 | : [fx] "R" (&fpu->state.fxsave)); |
Suresh Siddha | 0ca5bd0 | 2012-07-24 16:05:28 -0700 | [diff] [blame] | 209 | } |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 212 | /* These macros all use (%edi)/(%rdi) as the single memory argument. */ |
| 213 | #define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27" |
| 214 | #define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37" |
| 215 | #define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f" |
| 216 | #define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f" |
| 217 | #define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f" |
| 218 | |
| 219 | /* xstate instruction fault handler: */ |
| 220 | #define xstate_fault(__err) \ |
| 221 | \ |
| 222 | ".section .fixup,\"ax\"\n" \ |
| 223 | \ |
Ingo Molnar | 87b6559 | 2015-05-25 11:27:46 +0200 | [diff] [blame] | 224 | "3: movl $-2,%[_err]\n" \ |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 225 | " jmp 2b\n" \ |
| 226 | \ |
| 227 | ".previous\n" \ |
| 228 | \ |
| 229 | _ASM_EXTABLE(1b, 3b) \ |
Ingo Molnar | 87b6559 | 2015-05-25 11:27:46 +0200 | [diff] [blame] | 230 | : [_err] "=r" (__err) |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 231 | |
| 232 | /* |
| 233 | * This function is called only during boot time when x86 caps are not set |
| 234 | * up and alternative can not be used yet. |
| 235 | */ |
Ingo Molnar | 8c05f05 | 2015-05-24 09:23:25 +0200 | [diff] [blame] | 236 | static inline void copy_xregs_to_kernel_booting(struct xregs_state *xstate) |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 237 | { |
| 238 | u64 mask = -1; |
| 239 | u32 lmask = mask; |
| 240 | u32 hmask = mask >> 32; |
| 241 | int err = 0; |
| 242 | |
| 243 | WARN_ON(system_state != SYSTEM_BOOTING); |
| 244 | |
| 245 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
| 246 | asm volatile("1:"XSAVES"\n\t" |
| 247 | "2:\n\t" |
| 248 | xstate_fault(err) |
Ingo Molnar | 87b6559 | 2015-05-25 11:27:46 +0200 | [diff] [blame] | 249 | : "D" (xstate), "m" (*xstate), "a" (lmask), "d" (hmask), "0" (err) |
| 250 | : "memory"); |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 251 | else |
| 252 | asm volatile("1:"XSAVE"\n\t" |
| 253 | "2:\n\t" |
| 254 | xstate_fault(err) |
Ingo Molnar | 87b6559 | 2015-05-25 11:27:46 +0200 | [diff] [blame] | 255 | : "D" (xstate), "m" (*xstate), "a" (lmask), "d" (hmask), "0" (err) |
| 256 | : "memory"); |
Ingo Molnar | 8c05f05 | 2015-05-24 09:23:25 +0200 | [diff] [blame] | 257 | |
| 258 | /* We should never fault when copying to a kernel buffer: */ |
| 259 | WARN_ON_FPU(err); |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /* |
| 263 | * This function is called only during boot time when x86 caps are not set |
| 264 | * up and alternative can not be used yet. |
| 265 | */ |
Ingo Molnar | 8c05f05 | 2015-05-24 09:23:25 +0200 | [diff] [blame] | 266 | static inline void copy_kernel_to_xregs_booting(struct xregs_state *xstate, u64 mask) |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 267 | { |
| 268 | u32 lmask = mask; |
| 269 | u32 hmask = mask >> 32; |
| 270 | int err = 0; |
| 271 | |
| 272 | WARN_ON(system_state != SYSTEM_BOOTING); |
| 273 | |
| 274 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
| 275 | asm volatile("1:"XRSTORS"\n\t" |
| 276 | "2:\n\t" |
| 277 | xstate_fault(err) |
Ingo Molnar | 87b6559 | 2015-05-25 11:27:46 +0200 | [diff] [blame] | 278 | : "D" (xstate), "m" (*xstate), "a" (lmask), "d" (hmask), "0" (err) |
| 279 | : "memory"); |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 280 | else |
| 281 | asm volatile("1:"XRSTOR"\n\t" |
| 282 | "2:\n\t" |
| 283 | xstate_fault(err) |
Ingo Molnar | 87b6559 | 2015-05-25 11:27:46 +0200 | [diff] [blame] | 284 | : "D" (xstate), "m" (*xstate), "a" (lmask), "d" (hmask), "0" (err) |
| 285 | : "memory"); |
Ingo Molnar | 8c05f05 | 2015-05-24 09:23:25 +0200 | [diff] [blame] | 286 | |
| 287 | /* We should never fault when copying from a kernel buffer: */ |
| 288 | WARN_ON_FPU(err); |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Save processor xstate to xsave area. |
| 293 | */ |
Ingo Molnar | 8c05f05 | 2015-05-24 09:23:25 +0200 | [diff] [blame] | 294 | static inline void copy_xregs_to_kernel(struct xregs_state *xstate) |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 295 | { |
| 296 | u64 mask = -1; |
| 297 | u32 lmask = mask; |
| 298 | u32 hmask = mask >> 32; |
| 299 | int err = 0; |
| 300 | |
| 301 | WARN_ON(!alternatives_patched); |
| 302 | |
| 303 | /* |
| 304 | * If xsaves is enabled, xsaves replaces xsaveopt because |
| 305 | * it supports compact format and supervisor states in addition to |
| 306 | * modified optimization in xsaveopt. |
| 307 | * |
| 308 | * Otherwise, if xsaveopt is enabled, xsaveopt replaces xsave |
| 309 | * because xsaveopt supports modified optimization which is not |
| 310 | * supported by xsave. |
| 311 | * |
| 312 | * If none of xsaves and xsaveopt is enabled, use xsave. |
| 313 | */ |
| 314 | alternative_input_2( |
| 315 | "1:"XSAVE, |
| 316 | XSAVEOPT, |
| 317 | X86_FEATURE_XSAVEOPT, |
| 318 | XSAVES, |
| 319 | X86_FEATURE_XSAVES, |
Ingo Molnar | 87dafd4 | 2015-05-25 10:57:06 +0200 | [diff] [blame] | 320 | [xstate] "D" (xstate), "a" (lmask), "d" (hmask) : |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 321 | "memory"); |
| 322 | asm volatile("2:\n\t" |
| 323 | xstate_fault(err) |
Ingo Molnar | 685c961 | 2015-05-25 11:59:35 +0200 | [diff] [blame] | 324 | : "0" (err) |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 325 | : "memory"); |
| 326 | |
Ingo Molnar | 8c05f05 | 2015-05-24 09:23:25 +0200 | [diff] [blame] | 327 | /* We should never fault when copying to a kernel buffer: */ |
| 328 | WARN_ON_FPU(err); |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Restore processor xstate from xsave area. |
| 333 | */ |
Ingo Molnar | 8c05f05 | 2015-05-24 09:23:25 +0200 | [diff] [blame] | 334 | static inline void copy_kernel_to_xregs(struct xregs_state *xstate, u64 mask) |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 335 | { |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 336 | u32 lmask = mask; |
| 337 | u32 hmask = mask >> 32; |
Ingo Molnar | 685c961 | 2015-05-25 11:59:35 +0200 | [diff] [blame] | 338 | int err = 0; |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 339 | |
| 340 | /* |
| 341 | * Use xrstors to restore context if it is enabled. xrstors supports |
| 342 | * compacted format of xsave area which is not supported by xrstor. |
| 343 | */ |
| 344 | alternative_input( |
| 345 | "1: " XRSTOR, |
| 346 | XRSTORS, |
| 347 | X86_FEATURE_XSAVES, |
Ingo Molnar | 87dafd4 | 2015-05-25 10:57:06 +0200 | [diff] [blame] | 348 | "D" (xstate), "m" (*xstate), "a" (lmask), "d" (hmask) |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 349 | : "memory"); |
| 350 | |
| 351 | asm volatile("2:\n" |
| 352 | xstate_fault(err) |
Ingo Molnar | 685c961 | 2015-05-25 11:59:35 +0200 | [diff] [blame] | 353 | : "0" (err) |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 354 | : "memory"); |
| 355 | |
Ingo Molnar | 8c05f05 | 2015-05-24 09:23:25 +0200 | [diff] [blame] | 356 | /* We should never fault when copying from a kernel buffer: */ |
| 357 | WARN_ON_FPU(err); |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /* |
| 361 | * Save xstate to user space xsave area. |
| 362 | * |
| 363 | * We don't use modified optimization because xrstor/xrstors might track |
| 364 | * a different application. |
| 365 | * |
| 366 | * We don't use compacted format xsave area for |
| 367 | * backward compatibility for old applications which don't understand |
| 368 | * compacted format of xsave area. |
| 369 | */ |
| 370 | static inline int copy_xregs_to_user(struct xregs_state __user *buf) |
| 371 | { |
| 372 | int err; |
| 373 | |
| 374 | /* |
| 375 | * Clear the xsave header first, so that reserved fields are |
| 376 | * initialized to zero. |
| 377 | */ |
| 378 | err = __clear_user(&buf->header, sizeof(buf->header)); |
| 379 | if (unlikely(err)) |
| 380 | return -EFAULT; |
| 381 | |
| 382 | __asm__ __volatile__(ASM_STAC "\n" |
| 383 | "1:"XSAVE"\n" |
| 384 | "2: " ASM_CLAC "\n" |
| 385 | xstate_fault(err) |
Ingo Molnar | 685c961 | 2015-05-25 11:59:35 +0200 | [diff] [blame] | 386 | : "D" (buf), "a" (-1), "d" (-1), "0" (err) |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 387 | : "memory"); |
| 388 | return err; |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Restore xstate from user space xsave area. |
| 393 | */ |
| 394 | static inline int copy_user_to_xregs(struct xregs_state __user *buf, u64 mask) |
| 395 | { |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 396 | struct xregs_state *xstate = ((__force struct xregs_state *)buf); |
| 397 | u32 lmask = mask; |
| 398 | u32 hmask = mask >> 32; |
Ingo Molnar | 685c961 | 2015-05-25 11:59:35 +0200 | [diff] [blame] | 399 | int err = 0; |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 400 | |
| 401 | __asm__ __volatile__(ASM_STAC "\n" |
| 402 | "1:"XRSTOR"\n" |
| 403 | "2: " ASM_CLAC "\n" |
| 404 | xstate_fault(err) |
Ingo Molnar | 685c961 | 2015-05-25 11:59:35 +0200 | [diff] [blame] | 405 | : "D" (xstate), "a" (lmask), "d" (hmask), "0" (err) |
Ingo Molnar | fd169b0 | 2015-05-25 09:55:39 +0200 | [diff] [blame] | 406 | : "memory"); /* memory required? */ |
| 407 | return err; |
| 408 | } |
| 409 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 410 | /* |
| 411 | * These must be called with preempt disabled. Returns |
Ingo Molnar | 4f83634 | 2015-04-27 02:53:16 +0200 | [diff] [blame] | 412 | * 'true' if the FPU state is still intact and we can |
| 413 | * keep registers active. |
| 414 | * |
| 415 | * The legacy FNSAVE instruction cleared all FPU state |
| 416 | * unconditionally, so registers are essentially destroyed. |
| 417 | * Modern FPU state can be kept in registers, if there are |
Ingo Molnar | 1bc6b05 | 2015-04-27 03:32:18 +0200 | [diff] [blame] | 418 | * no pending FP exceptions. |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 419 | */ |
Ingo Molnar | 4f83634 | 2015-04-27 02:53:16 +0200 | [diff] [blame] | 420 | static inline int copy_fpregs_to_fpstate(struct fpu *fpu) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 421 | { |
Ingo Molnar | 1bc6b05 | 2015-04-27 03:32:18 +0200 | [diff] [blame] | 422 | if (likely(use_xsave())) { |
Ingo Molnar | c681314 | 2015-04-30 11:34:09 +0200 | [diff] [blame] | 423 | copy_xregs_to_kernel(&fpu->state.xsave); |
Ingo Molnar | 1bc6b05 | 2015-04-27 03:32:18 +0200 | [diff] [blame] | 424 | return 1; |
| 425 | } |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 426 | |
Ingo Molnar | 1bc6b05 | 2015-04-27 03:32:18 +0200 | [diff] [blame] | 427 | if (likely(use_fxsr())) { |
Ingo Molnar | c681314 | 2015-04-30 11:34:09 +0200 | [diff] [blame] | 428 | copy_fxregs_to_kernel(fpu); |
Ingo Molnar | 1bc6b05 | 2015-04-27 03:32:18 +0200 | [diff] [blame] | 429 | return 1; |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /* |
Ingo Molnar | 1bc6b05 | 2015-04-27 03:32:18 +0200 | [diff] [blame] | 433 | * Legacy FPU register saving, FNSAVE always clears FPU registers, |
| 434 | * so we have to mark them inactive: |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 435 | */ |
Ingo Molnar | 87dafd4 | 2015-05-25 10:57:06 +0200 | [diff] [blame] | 436 | asm volatile("fnsave %[fp]; fwait" : [fp] "=m" (fpu->state.fsave)); |
Ingo Molnar | 4f83634 | 2015-04-27 02:53:16 +0200 | [diff] [blame] | 437 | |
Ingo Molnar | 4f83634 | 2015-04-27 02:53:16 +0200 | [diff] [blame] | 438 | return 0; |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 439 | } |
| 440 | |
Ingo Molnar | 0e75c54 | 2015-04-29 20:10:43 +0200 | [diff] [blame] | 441 | static inline int __copy_fpstate_to_fpregs(struct fpu *fpu) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 442 | { |
Ingo Molnar | 8c05f05 | 2015-05-24 09:23:25 +0200 | [diff] [blame] | 443 | if (use_xsave()) { |
| 444 | copy_kernel_to_xregs(&fpu->state.xsave, -1); |
| 445 | return 0; |
| 446 | } else { |
| 447 | if (use_fxsr()) |
| 448 | return copy_kernel_to_fxregs(&fpu->state.fxsave); |
| 449 | else |
| 450 | return copy_kernel_to_fregs(&fpu->state.fsave); |
| 451 | } |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 452 | } |
| 453 | |
Ingo Molnar | 0e75c54 | 2015-04-29 20:10:43 +0200 | [diff] [blame] | 454 | static inline int copy_fpstate_to_fpregs(struct fpu *fpu) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 455 | { |
Borislav Petkov | 6ca7a8a | 2014-12-21 15:02:23 +0100 | [diff] [blame] | 456 | /* |
| 457 | * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is |
| 458 | * pending. Clear the x87 state here by setting it to fixed values. |
| 459 | * "m" is a random variable that should be in L1. |
| 460 | */ |
Borislav Petkov | 9b13a93 | 2014-06-18 00:06:23 +0200 | [diff] [blame] | 461 | if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) { |
Linus Torvalds | 26bef13 | 2014-01-11 19:15:52 -0800 | [diff] [blame] | 462 | asm volatile( |
| 463 | "fnclex\n\t" |
| 464 | "emms\n\t" |
| 465 | "fildl %P[addr]" /* set F?P to defined value */ |
Ingo Molnar | d5cea9b | 2015-04-24 14:19:26 +0200 | [diff] [blame] | 466 | : : [addr] "m" (fpu->fpregs_active)); |
Linus Torvalds | 26bef13 | 2014-01-11 19:15:52 -0800 | [diff] [blame] | 467 | } |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 468 | |
Ingo Molnar | 0e75c54 | 2015-04-29 20:10:43 +0200 | [diff] [blame] | 469 | return __copy_fpstate_to_fpregs(fpu); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 470 | } |
| 471 | |
Ingo Molnar | 87dafd4 | 2015-05-25 10:57:06 +0200 | [diff] [blame] | 472 | extern int copy_fpstate_to_sigframe(void __user *buf, void __user *fp, int size); |
Ingo Molnar | b1b64dc | 2015-05-05 15:56:33 +0200 | [diff] [blame] | 473 | |
| 474 | /* |
| 475 | * FPU context switch related helper methods: |
| 476 | */ |
| 477 | |
| 478 | DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx); |
| 479 | |
| 480 | /* |
| 481 | * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx, |
| 482 | * on this CPU. |
| 483 | * |
| 484 | * This will disable any lazy FPU state restore of the current FPU state, |
| 485 | * but if the current thread owns the FPU, it will still be saved by. |
| 486 | */ |
| 487 | static inline void __cpu_disable_lazy_restore(unsigned int cpu) |
| 488 | { |
| 489 | per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL; |
| 490 | } |
| 491 | |
| 492 | static inline int fpu_want_lazy_restore(struct fpu *fpu, unsigned int cpu) |
| 493 | { |
| 494 | return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu; |
| 495 | } |
| 496 | |
| 497 | |
Ingo Molnar | 32b49b3 | 2015-04-27 08:58:45 +0200 | [diff] [blame] | 498 | /* |
| 499 | * Wrap lazy FPU TS handling in a 'hw fpregs activation/deactivation' |
| 500 | * idiom, which is then paired with the sw-flag (fpregs_active) later on: |
| 501 | */ |
| 502 | |
| 503 | static inline void __fpregs_activate_hw(void) |
| 504 | { |
| 505 | if (!use_eager_fpu()) |
| 506 | clts(); |
| 507 | } |
| 508 | |
| 509 | static inline void __fpregs_deactivate_hw(void) |
| 510 | { |
| 511 | if (!use_eager_fpu()) |
| 512 | stts(); |
| 513 | } |
| 514 | |
| 515 | /* Must be paired with an 'stts' (fpregs_deactivate_hw()) after! */ |
Ingo Molnar | 723c58e | 2015-04-24 14:28:01 +0200 | [diff] [blame] | 516 | static inline void __fpregs_deactivate(struct fpu *fpu) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 517 | { |
Ingo Molnar | e97131a | 2015-05-05 11:34:49 +0200 | [diff] [blame] | 518 | WARN_ON_FPU(!fpu->fpregs_active); |
| 519 | |
Ingo Molnar | d5cea9b | 2015-04-24 14:19:26 +0200 | [diff] [blame] | 520 | fpu->fpregs_active = 0; |
Ingo Molnar | 36b544d | 2015-04-23 12:18:28 +0200 | [diff] [blame] | 521 | this_cpu_write(fpu_fpregs_owner_ctx, NULL); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 522 | } |
| 523 | |
Ingo Molnar | 32b49b3 | 2015-04-27 08:58:45 +0200 | [diff] [blame] | 524 | /* Must be paired with a 'clts' (fpregs_activate_hw()) before! */ |
Ingo Molnar | dfaea4e | 2015-04-24 14:26:47 +0200 | [diff] [blame] | 525 | static inline void __fpregs_activate(struct fpu *fpu) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 526 | { |
Ingo Molnar | e97131a | 2015-05-05 11:34:49 +0200 | [diff] [blame] | 527 | WARN_ON_FPU(fpu->fpregs_active); |
| 528 | |
Ingo Molnar | d5cea9b | 2015-04-24 14:19:26 +0200 | [diff] [blame] | 529 | fpu->fpregs_active = 1; |
Ingo Molnar | c0311f6 | 2015-04-23 12:24:59 +0200 | [diff] [blame] | 530 | this_cpu_write(fpu_fpregs_owner_ctx, fpu); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /* |
Ingo Molnar | 952f07e | 2015-04-26 16:56:05 +0200 | [diff] [blame] | 534 | * The question "does this thread have fpu access?" |
| 535 | * is slightly racy, since preemption could come in |
| 536 | * and revoke it immediately after the test. |
| 537 | * |
| 538 | * However, even in that very unlikely scenario, |
| 539 | * we can just assume we have FPU access - typically |
| 540 | * to save the FP state - we'll just take a #NM |
| 541 | * fault and get the FPU access back. |
| 542 | */ |
Ingo Molnar | 3c6dffa | 2015-04-28 12:28:08 +0200 | [diff] [blame] | 543 | static inline int fpregs_active(void) |
Ingo Molnar | 952f07e | 2015-04-26 16:56:05 +0200 | [diff] [blame] | 544 | { |
| 545 | return current->thread.fpu.fpregs_active; |
| 546 | } |
| 547 | |
| 548 | /* |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 549 | * Encapsulate the CR0.TS handling together with the |
| 550 | * software flag. |
| 551 | * |
| 552 | * These generally need preemption protection to work, |
| 553 | * do try to avoid using these on their own. |
| 554 | */ |
Ingo Molnar | 232f62c | 2015-04-24 14:30:38 +0200 | [diff] [blame] | 555 | static inline void fpregs_activate(struct fpu *fpu) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 556 | { |
Ingo Molnar | 32b49b3 | 2015-04-27 08:58:45 +0200 | [diff] [blame] | 557 | __fpregs_activate_hw(); |
Ingo Molnar | dfaea4e | 2015-04-24 14:26:47 +0200 | [diff] [blame] | 558 | __fpregs_activate(fpu); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 559 | } |
| 560 | |
Ingo Molnar | 66af8e2 | 2015-04-24 14:31:27 +0200 | [diff] [blame] | 561 | static inline void fpregs_deactivate(struct fpu *fpu) |
| 562 | { |
| 563 | __fpregs_deactivate(fpu); |
Ingo Molnar | 32b49b3 | 2015-04-27 08:58:45 +0200 | [diff] [blame] | 564 | __fpregs_deactivate_hw(); |
Ingo Molnar | 66af8e2 | 2015-04-24 14:31:27 +0200 | [diff] [blame] | 565 | } |
| 566 | |
Borislav Petkov | b85e67d | 2015-03-16 10:21:55 +0100 | [diff] [blame] | 567 | /* |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 568 | * FPU state switching for scheduling. |
| 569 | * |
| 570 | * This is a two-stage process: |
| 571 | * |
| 572 | * - switch_fpu_prepare() saves the old state and |
| 573 | * sets the new state of the CR0.TS bit. This is |
| 574 | * done within the context of the old process. |
| 575 | * |
| 576 | * - switch_fpu_finish() restores the new state as |
| 577 | * necessary. |
| 578 | */ |
| 579 | typedef struct { int preload; } fpu_switch_t; |
| 580 | |
Ingo Molnar | cb8818b | 2015-04-23 17:39:04 +0200 | [diff] [blame] | 581 | static inline fpu_switch_t |
| 582 | switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 583 | { |
| 584 | fpu_switch_t fpu; |
| 585 | |
Suresh Siddha | 304bced | 2012-08-24 14:13:02 -0700 | [diff] [blame] | 586 | /* |
| 587 | * If the task has used the math, pre-load the FPU on xsave processors |
| 588 | * or if the past 5 consecutive context-switches used math. |
| 589 | */ |
Ingo Molnar | c5bedc6 | 2015-04-23 12:49:20 +0200 | [diff] [blame] | 590 | fpu.preload = new_fpu->fpstate_active && |
Ingo Molnar | cb8818b | 2015-04-23 17:39:04 +0200 | [diff] [blame] | 591 | (use_eager_fpu() || new_fpu->counter > 5); |
Rik van Riel | 1361ef2 | 2015-02-06 15:02:03 -0500 | [diff] [blame] | 592 | |
Ingo Molnar | d5cea9b | 2015-04-24 14:19:26 +0200 | [diff] [blame] | 593 | if (old_fpu->fpregs_active) { |
Ingo Molnar | 4f83634 | 2015-04-27 02:53:16 +0200 | [diff] [blame] | 594 | if (!copy_fpregs_to_fpstate(old_fpu)) |
Ingo Molnar | cb8818b | 2015-04-23 17:39:04 +0200 | [diff] [blame] | 595 | old_fpu->last_cpu = -1; |
Rik van Riel | 1361ef2 | 2015-02-06 15:02:03 -0500 | [diff] [blame] | 596 | else |
Ingo Molnar | cb8818b | 2015-04-23 17:39:04 +0200 | [diff] [blame] | 597 | old_fpu->last_cpu = cpu; |
Rik van Riel | 1361ef2 | 2015-02-06 15:02:03 -0500 | [diff] [blame] | 598 | |
Ingo Molnar | 36b544d | 2015-04-23 12:18:28 +0200 | [diff] [blame] | 599 | /* But leave fpu_fpregs_owner_ctx! */ |
Ingo Molnar | d5cea9b | 2015-04-24 14:19:26 +0200 | [diff] [blame] | 600 | old_fpu->fpregs_active = 0; |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 601 | |
| 602 | /* Don't change CR0.TS if we just switch! */ |
| 603 | if (fpu.preload) { |
Ingo Molnar | cb8818b | 2015-04-23 17:39:04 +0200 | [diff] [blame] | 604 | new_fpu->counter++; |
Ingo Molnar | dfaea4e | 2015-04-24 14:26:47 +0200 | [diff] [blame] | 605 | __fpregs_activate(new_fpu); |
Ingo Molnar | 7366ed7 | 2015-04-27 04:19:39 +0200 | [diff] [blame] | 606 | prefetch(&new_fpu->state); |
Ingo Molnar | 32b49b3 | 2015-04-27 08:58:45 +0200 | [diff] [blame] | 607 | } else { |
| 608 | __fpregs_deactivate_hw(); |
| 609 | } |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 610 | } else { |
Ingo Molnar | cb8818b | 2015-04-23 17:39:04 +0200 | [diff] [blame] | 611 | old_fpu->counter = 0; |
| 612 | old_fpu->last_cpu = -1; |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 613 | if (fpu.preload) { |
Ingo Molnar | cb8818b | 2015-04-23 17:39:04 +0200 | [diff] [blame] | 614 | new_fpu->counter++; |
Ingo Molnar | 66ddc2c | 2015-04-23 17:25:44 +0200 | [diff] [blame] | 615 | if (fpu_want_lazy_restore(new_fpu, cpu)) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 616 | fpu.preload = 0; |
| 617 | else |
Ingo Molnar | 7366ed7 | 2015-04-27 04:19:39 +0200 | [diff] [blame] | 618 | prefetch(&new_fpu->state); |
Ingo Molnar | 232f62c | 2015-04-24 14:30:38 +0200 | [diff] [blame] | 619 | fpregs_activate(new_fpu); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | return fpu; |
| 623 | } |
| 624 | |
| 625 | /* |
Ingo Molnar | b1b64dc | 2015-05-05 15:56:33 +0200 | [diff] [blame] | 626 | * Misc helper functions: |
| 627 | */ |
| 628 | |
| 629 | /* |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 630 | * By the time this gets called, we've already cleared CR0.TS and |
| 631 | * given the process the FPU if we are going to preload the FPU |
| 632 | * state - all we need to do is to conditionally restore the register |
| 633 | * state itself. |
| 634 | */ |
Ingo Molnar | 384a23f | 2015-04-23 17:43:27 +0200 | [diff] [blame] | 635 | static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch) |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 636 | { |
Ingo Molnar | 384a23f | 2015-04-23 17:43:27 +0200 | [diff] [blame] | 637 | if (fpu_switch.preload) { |
Ingo Molnar | e97131a | 2015-05-05 11:34:49 +0200 | [diff] [blame] | 638 | if (unlikely(copy_fpstate_to_fpregs(new_fpu))) { |
| 639 | WARN_ON_FPU(1); |
Ingo Molnar | fbce778 | 2015-04-30 07:12:46 +0200 | [diff] [blame] | 640 | fpu__clear(new_fpu); |
Ingo Molnar | e97131a | 2015-05-05 11:34:49 +0200 | [diff] [blame] | 641 | } |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
| 645 | /* |
Oleg Nesterov | fb14b4e | 2015-03-11 18:34:09 +0100 | [diff] [blame] | 646 | * Needs to be preemption-safe. |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 647 | * |
Suresh Siddha | 377ffbc | 2012-08-24 14:12:58 -0700 | [diff] [blame] | 648 | * NOTE! user_fpu_begin() must be used only immediately before restoring |
Oleg Nesterov | fb14b4e | 2015-03-11 18:34:09 +0100 | [diff] [blame] | 649 | * the save state. It does not do any saving/restoring on its own. In |
| 650 | * lazy FPU mode, it is just an optimization to avoid a #NM exception, |
| 651 | * the task can lose the FPU right after preempt_enable(). |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 652 | */ |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 653 | static inline void user_fpu_begin(void) |
| 654 | { |
Ingo Molnar | 4540d3f | 2015-04-23 12:31:17 +0200 | [diff] [blame] | 655 | struct fpu *fpu = ¤t->thread.fpu; |
| 656 | |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 657 | preempt_disable(); |
Ingo Molnar | 3c6dffa | 2015-04-28 12:28:08 +0200 | [diff] [blame] | 658 | if (!fpregs_active()) |
Ingo Molnar | 232f62c | 2015-04-24 14:30:38 +0200 | [diff] [blame] | 659 | fpregs_activate(fpu); |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 660 | preempt_enable(); |
| 661 | } |
| 662 | |
Ingo Molnar | b1b64dc | 2015-05-05 15:56:33 +0200 | [diff] [blame] | 663 | /* |
| 664 | * MXCSR and XCR definitions: |
| 665 | */ |
| 666 | |
| 667 | extern unsigned int mxcsr_feature_mask; |
| 668 | |
| 669 | #define XCR_XFEATURE_ENABLED_MASK 0x00000000 |
| 670 | |
| 671 | static inline u64 xgetbv(u32 index) |
| 672 | { |
| 673 | u32 eax, edx; |
| 674 | |
| 675 | asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */ |
| 676 | : "=a" (eax), "=d" (edx) |
| 677 | : "c" (index)); |
| 678 | return eax + ((u64)edx << 32); |
| 679 | } |
| 680 | |
| 681 | static inline void xsetbv(u32 index, u64 value) |
| 682 | { |
| 683 | u32 eax = value; |
| 684 | u32 edx = value >> 32; |
| 685 | |
| 686 | asm volatile(".byte 0x0f,0x01,0xd1" /* xsetbv */ |
| 687 | : : "a" (eax), "d" (edx), "c" (index)); |
| 688 | } |
| 689 | |
Ingo Molnar | 78f7f1e | 2015-04-24 02:54:44 +0200 | [diff] [blame] | 690 | #endif /* _ASM_X86_FPU_INTERNAL_H */ |