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