]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/x86/include/asm/fpu-internal.h
x86/fpu: Change fpu_owner_task to fpu_fpregs_owner_ctx
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / include / asm / fpu-internal.h
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
10 #ifndef _FPU_INTERNAL_H
11 #define _FPU_INTERNAL_H
12
13 #include <linux/regset.h>
14 #include <linux/compat.h>
15 #include <linux/slab.h>
16
17 #include <asm/user.h>
18 #include <asm/i387.h>
19 #include <asm/xsave.h>
20
21 #ifdef CONFIG_X86_64
22 # include <asm/sigcontext32.h>
23 # include <asm/user32.h>
24 struct ksignal;
25 int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
26 compat_sigset_t *set, struct pt_regs *regs);
27 int ia32_setup_frame(int sig, struct ksignal *ksig,
28 compat_sigset_t *set, struct pt_regs *regs);
29 #else
30 # define user_i387_ia32_struct user_i387_struct
31 # define user32_fxsr_struct user_fxsr_struct
32 # define ia32_setup_frame __setup_frame
33 # define ia32_setup_rt_frame __setup_rt_frame
34 #endif
35
36 extern unsigned int mxcsr_feature_mask;
37 extern void fpu__cpu_init(void);
38 extern void eager_fpu_init(void);
39
40 DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
41
42 extern void convert_from_fxsr(struct user_i387_ia32_struct *env,
43 struct task_struct *tsk);
44 extern void convert_to_fxsr(struct task_struct *tsk,
45 const struct user_i387_ia32_struct *env);
46
47 extern user_regset_active_fn fpregs_active, xfpregs_active;
48 extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get,
49 xstateregs_get;
50 extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set,
51 xstateregs_set;
52
53 /*
54 * xstateregs_active == fpregs_active. Please refer to the comment
55 * at the definition of fpregs_active.
56 */
57 #define xstateregs_active fpregs_active
58
59 #ifdef CONFIG_MATH_EMULATION
60 extern void finit_soft_fpu(struct i387_soft_struct *soft);
61 #else
62 static inline void finit_soft_fpu(struct i387_soft_struct *soft) {}
63 #endif
64
65 /*
66 * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx,
67 * on this CPU.
68 *
69 * This will disable any lazy FPU state restore of the current FPU state,
70 * but if the current thread owns the FPU, it will still be saved by.
71 */
72 static inline void __cpu_disable_lazy_restore(unsigned int cpu)
73 {
74 per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
75 }
76
77 /*
78 * Used to indicate that the FPU state in memory is newer than the FPU
79 * state in registers, and the FPU state should be reloaded next time the
80 * task is run. Only safe on the current task, or non-running tasks.
81 */
82 static inline void task_disable_lazy_fpu_restore(struct task_struct *tsk)
83 {
84 tsk->thread.fpu.last_cpu = ~0;
85 }
86
87 static inline int fpu_lazy_restore(struct task_struct *new, unsigned int cpu)
88 {
89 return &new->thread.fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) &&
90 cpu == new->thread.fpu.last_cpu;
91 }
92
93 static inline int is_ia32_compat_frame(void)
94 {
95 return config_enabled(CONFIG_IA32_EMULATION) &&
96 test_thread_flag(TIF_IA32);
97 }
98
99 static inline int is_ia32_frame(void)
100 {
101 return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame();
102 }
103
104 static inline int is_x32_frame(void)
105 {
106 return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32);
107 }
108
109 #define X87_FSW_ES (1 << 7) /* Exception Summary */
110
111 static __always_inline __pure bool use_eager_fpu(void)
112 {
113 return static_cpu_has_safe(X86_FEATURE_EAGER_FPU);
114 }
115
116 static __always_inline __pure bool use_xsaveopt(void)
117 {
118 return static_cpu_has_safe(X86_FEATURE_XSAVEOPT);
119 }
120
121 static __always_inline __pure bool use_xsave(void)
122 {
123 return static_cpu_has_safe(X86_FEATURE_XSAVE);
124 }
125
126 static __always_inline __pure bool use_fxsr(void)
127 {
128 return static_cpu_has_safe(X86_FEATURE_FXSR);
129 }
130
131 static inline void fx_finit(struct i387_fxsave_struct *fx)
132 {
133 fx->cwd = 0x37f;
134 fx->mxcsr = MXCSR_DEFAULT;
135 }
136
137 extern void __sanitize_i387_state(struct task_struct *);
138
139 static inline void sanitize_i387_state(struct task_struct *tsk)
140 {
141 if (!use_xsaveopt())
142 return;
143 __sanitize_i387_state(tsk);
144 }
145
146 #define user_insn(insn, output, input...) \
147 ({ \
148 int err; \
149 asm volatile(ASM_STAC "\n" \
150 "1:" #insn "\n\t" \
151 "2: " ASM_CLAC "\n" \
152 ".section .fixup,\"ax\"\n" \
153 "3: movl $-1,%[err]\n" \
154 " jmp 2b\n" \
155 ".previous\n" \
156 _ASM_EXTABLE(1b, 3b) \
157 : [err] "=r" (err), output \
158 : "0"(0), input); \
159 err; \
160 })
161
162 #define check_insn(insn, output, input...) \
163 ({ \
164 int err; \
165 asm volatile("1:" #insn "\n\t" \
166 "2:\n" \
167 ".section .fixup,\"ax\"\n" \
168 "3: movl $-1,%[err]\n" \
169 " jmp 2b\n" \
170 ".previous\n" \
171 _ASM_EXTABLE(1b, 3b) \
172 : [err] "=r" (err), output \
173 : "0"(0), input); \
174 err; \
175 })
176
177 static inline int fsave_user(struct i387_fsave_struct __user *fx)
178 {
179 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
180 }
181
182 static inline int fxsave_user(struct i387_fxsave_struct __user *fx)
183 {
184 if (config_enabled(CONFIG_X86_32))
185 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
186 else if (config_enabled(CONFIG_AS_FXSAVEQ))
187 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
188
189 /* See comment in fpu_fxsave() below. */
190 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
191 }
192
193 static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
194 {
195 if (config_enabled(CONFIG_X86_32))
196 return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
197 else if (config_enabled(CONFIG_AS_FXSAVEQ))
198 return check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
199
200 /* See comment in fpu_fxsave() below. */
201 return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
202 "m" (*fx));
203 }
204
205 static inline int fxrstor_user(struct i387_fxsave_struct __user *fx)
206 {
207 if (config_enabled(CONFIG_X86_32))
208 return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
209 else if (config_enabled(CONFIG_AS_FXSAVEQ))
210 return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
211
212 /* See comment in fpu_fxsave() below. */
213 return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
214 "m" (*fx));
215 }
216
217 static inline int frstor_checking(struct i387_fsave_struct *fx)
218 {
219 return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
220 }
221
222 static inline int frstor_user(struct i387_fsave_struct __user *fx)
223 {
224 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
225 }
226
227 static inline void fpu_fxsave(struct fpu *fpu)
228 {
229 if (config_enabled(CONFIG_X86_32))
230 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state->fxsave));
231 else if (config_enabled(CONFIG_AS_FXSAVEQ))
232 asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state->fxsave));
233 else {
234 /* Using "rex64; fxsave %0" is broken because, if the memory
235 * operand uses any extended registers for addressing, a second
236 * REX prefix will be generated (to the assembler, rex64
237 * followed by semicolon is a separate instruction), and hence
238 * the 64-bitness is lost.
239 *
240 * Using "fxsaveq %0" would be the ideal choice, but is only
241 * supported starting with gas 2.16.
242 *
243 * Using, as a workaround, the properly prefixed form below
244 * isn't accepted by any binutils version so far released,
245 * complaining that the same type of prefix is used twice if
246 * an extended register is needed for addressing (fix submitted
247 * to mainline 2005-11-21).
248 *
249 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state->fxsave));
250 *
251 * This, however, we can work around by forcing the compiler to
252 * select an addressing mode that doesn't require extended
253 * registers.
254 */
255 asm volatile( "rex64/fxsave (%[fx])"
256 : "=m" (fpu->state->fxsave)
257 : [fx] "R" (&fpu->state->fxsave));
258 }
259 }
260
261 /*
262 * These must be called with preempt disabled. Returns
263 * 'true' if the FPU state is still intact.
264 */
265 static inline int fpu_save_init(struct fpu *fpu)
266 {
267 if (use_xsave()) {
268 xsave_state(&fpu->state->xsave);
269
270 /*
271 * xsave header may indicate the init state of the FP.
272 */
273 if (!(fpu->state->xsave.xsave_hdr.xstate_bv & XSTATE_FP))
274 return 1;
275 } else if (use_fxsr()) {
276 fpu_fxsave(fpu);
277 } else {
278 asm volatile("fnsave %[fx]; fwait"
279 : [fx] "=m" (fpu->state->fsave));
280 return 0;
281 }
282
283 /*
284 * If exceptions are pending, we need to clear them so
285 * that we don't randomly get exceptions later.
286 *
287 * FIXME! Is this perhaps only true for the old-style
288 * irq13 case? Maybe we could leave the x87 state
289 * intact otherwise?
290 */
291 if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES)) {
292 asm volatile("fnclex");
293 return 0;
294 }
295 return 1;
296 }
297
298 static inline int fpu_restore_checking(struct fpu *fpu)
299 {
300 if (use_xsave())
301 return fpu_xrstor_checking(&fpu->state->xsave);
302 else if (use_fxsr())
303 return fxrstor_checking(&fpu->state->fxsave);
304 else
305 return frstor_checking(&fpu->state->fsave);
306 }
307
308 static inline int restore_fpu_checking(struct task_struct *tsk)
309 {
310 /*
311 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
312 * pending. Clear the x87 state here by setting it to fixed values.
313 * "m" is a random variable that should be in L1.
314 */
315 if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) {
316 asm volatile(
317 "fnclex\n\t"
318 "emms\n\t"
319 "fildl %P[addr]" /* set F?P to defined value */
320 : : [addr] "m" (tsk->thread.fpu.has_fpu));
321 }
322
323 return fpu_restore_checking(&tsk->thread.fpu);
324 }
325
326 /* Must be paired with an 'stts' after! */
327 static inline void __thread_clear_has_fpu(struct fpu *fpu)
328 {
329 fpu->has_fpu = 0;
330 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
331 }
332
333 /* Must be paired with a 'clts' before! */
334 static inline void __thread_set_has_fpu(struct task_struct *tsk)
335 {
336 tsk->thread.fpu.has_fpu = 1;
337 this_cpu_write(fpu_fpregs_owner_ctx, &tsk->thread.fpu);
338 }
339
340 /*
341 * Encapsulate the CR0.TS handling together with the
342 * software flag.
343 *
344 * These generally need preemption protection to work,
345 * do try to avoid using these on their own.
346 */
347 static inline void __thread_fpu_end(struct task_struct *tsk)
348 {
349 __thread_clear_has_fpu(&tsk->thread.fpu);
350 if (!use_eager_fpu())
351 stts();
352 }
353
354 static inline void __thread_fpu_begin(struct task_struct *tsk)
355 {
356 if (!use_eager_fpu())
357 clts();
358 __thread_set_has_fpu(tsk);
359 }
360
361 static inline void drop_fpu(struct task_struct *tsk)
362 {
363 struct fpu *fpu = &tsk->thread.fpu;
364 /*
365 * Forget coprocessor state..
366 */
367 preempt_disable();
368 tsk->thread.fpu.counter = 0;
369
370 if (fpu->has_fpu) {
371 /* Ignore delayed exceptions from user space */
372 asm volatile("1: fwait\n"
373 "2:\n"
374 _ASM_EXTABLE(1b, 2b));
375 __thread_fpu_end(tsk);
376 }
377
378 clear_stopped_child_used_math(tsk);
379 preempt_enable();
380 }
381
382 static inline void restore_init_xstate(void)
383 {
384 if (use_xsave())
385 xrstor_state(init_xstate_buf, -1);
386 else
387 fxrstor_checking(&init_xstate_buf->i387);
388 }
389
390 /*
391 * Reset the FPU state in the eager case and drop it in the lazy case (later use
392 * will reinit it).
393 */
394 static inline void fpu_reset_state(struct task_struct *tsk)
395 {
396 if (!use_eager_fpu())
397 drop_fpu(tsk);
398 else
399 restore_init_xstate();
400 }
401
402 /*
403 * FPU state switching for scheduling.
404 *
405 * This is a two-stage process:
406 *
407 * - switch_fpu_prepare() saves the old state and
408 * sets the new state of the CR0.TS bit. This is
409 * done within the context of the old process.
410 *
411 * - switch_fpu_finish() restores the new state as
412 * necessary.
413 */
414 typedef struct { int preload; } fpu_switch_t;
415
416 static inline fpu_switch_t switch_fpu_prepare(struct task_struct *old, struct task_struct *new, int cpu)
417 {
418 struct fpu *old_fpu = &old->thread.fpu;
419 fpu_switch_t fpu;
420
421 /*
422 * If the task has used the math, pre-load the FPU on xsave processors
423 * or if the past 5 consecutive context-switches used math.
424 */
425 fpu.preload = tsk_used_math(new) &&
426 (use_eager_fpu() || new->thread.fpu.counter > 5);
427
428 if (old_fpu->has_fpu) {
429 if (!fpu_save_init(&old->thread.fpu))
430 task_disable_lazy_fpu_restore(old);
431 else
432 old->thread.fpu.last_cpu = cpu;
433
434 /* But leave fpu_fpregs_owner_ctx! */
435 old->thread.fpu.has_fpu = 0;
436
437 /* Don't change CR0.TS if we just switch! */
438 if (fpu.preload) {
439 new->thread.fpu.counter++;
440 __thread_set_has_fpu(new);
441 prefetch(new->thread.fpu.state);
442 } else if (!use_eager_fpu())
443 stts();
444 } else {
445 old->thread.fpu.counter = 0;
446 task_disable_lazy_fpu_restore(old);
447 if (fpu.preload) {
448 new->thread.fpu.counter++;
449 if (fpu_lazy_restore(new, cpu))
450 fpu.preload = 0;
451 else
452 prefetch(new->thread.fpu.state);
453 __thread_fpu_begin(new);
454 }
455 }
456 return fpu;
457 }
458
459 /*
460 * By the time this gets called, we've already cleared CR0.TS and
461 * given the process the FPU if we are going to preload the FPU
462 * state - all we need to do is to conditionally restore the register
463 * state itself.
464 */
465 static inline void switch_fpu_finish(struct task_struct *new, fpu_switch_t fpu)
466 {
467 if (fpu.preload) {
468 if (unlikely(restore_fpu_checking(new)))
469 fpu_reset_state(new);
470 }
471 }
472
473 /*
474 * Signal frame handlers...
475 */
476 extern int save_xstate_sig(void __user *buf, void __user *fx, int size);
477 extern int __restore_xstate_sig(void __user *buf, void __user *fx, int size);
478
479 static inline int xstate_sigframe_size(void)
480 {
481 return use_xsave() ? xstate_size + FP_XSTATE_MAGIC2_SIZE : xstate_size;
482 }
483
484 static inline int restore_xstate_sig(void __user *buf, int ia32_frame)
485 {
486 void __user *buf_fx = buf;
487 int size = xstate_sigframe_size();
488
489 if (ia32_frame && use_fxsr()) {
490 buf_fx = buf + sizeof(struct i387_fsave_struct);
491 size += sizeof(struct i387_fsave_struct);
492 }
493
494 return __restore_xstate_sig(buf, buf_fx, size);
495 }
496
497 /*
498 * Needs to be preemption-safe.
499 *
500 * NOTE! user_fpu_begin() must be used only immediately before restoring
501 * the save state. It does not do any saving/restoring on its own. In
502 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
503 * the task can lose the FPU right after preempt_enable().
504 */
505 static inline void user_fpu_begin(void)
506 {
507 preempt_disable();
508 if (!user_has_fpu())
509 __thread_fpu_begin(current);
510 preempt_enable();
511 }
512
513 static inline void __save_fpu(struct task_struct *tsk)
514 {
515 if (use_xsave()) {
516 if (unlikely(system_state == SYSTEM_BOOTING))
517 xsave_state_booting(&tsk->thread.fpu.state->xsave);
518 else
519 xsave_state(&tsk->thread.fpu.state->xsave);
520 } else
521 fpu_fxsave(&tsk->thread.fpu);
522 }
523
524 /*
525 * i387 state interaction
526 */
527 static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
528 {
529 if (cpu_has_fxsr) {
530 return tsk->thread.fpu.state->fxsave.cwd;
531 } else {
532 return (unsigned short)tsk->thread.fpu.state->fsave.cwd;
533 }
534 }
535
536 static inline unsigned short get_fpu_swd(struct task_struct *tsk)
537 {
538 if (cpu_has_fxsr) {
539 return tsk->thread.fpu.state->fxsave.swd;
540 } else {
541 return (unsigned short)tsk->thread.fpu.state->fsave.swd;
542 }
543 }
544
545 static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
546 {
547 if (cpu_has_xmm) {
548 return tsk->thread.fpu.state->fxsave.mxcsr;
549 } else {
550 return MXCSR_DEFAULT;
551 }
552 }
553
554 extern void fpstate_cache_init(void);
555
556 extern int fpstate_alloc(struct fpu *fpu);
557 extern void fpstate_free(struct fpu *fpu);
558 extern int fpu__copy(struct task_struct *dst, struct task_struct *src);
559
560 static inline unsigned long
561 alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx,
562 unsigned long *size)
563 {
564 unsigned long frame_size = xstate_sigframe_size();
565
566 *buf_fx = sp = round_down(sp - frame_size, 64);
567 if (ia32_frame && use_fxsr()) {
568 frame_size += sizeof(struct i387_fsave_struct);
569 sp -= sizeof(struct i387_fsave_struct);
570 }
571
572 *size = frame_size;
573 return sp;
574 }
575
576 #endif