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