i387.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #ifndef _ASM_X86_I387_H
  10. #define _ASM_X86_I387_H
  11. #ifndef __ASSEMBLY__
  12. #include <linux/sched.h>
  13. #include <linux/hardirq.h>
  14. struct pt_regs;
  15. struct user_i387_struct;
  16. extern int init_fpu(struct task_struct *child);
  17. extern void fpu_finit(struct fpu *fpu);
  18. extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);
  19. extern void math_state_restore(void);
  20. extern bool irq_fpu_usable(void);
  21. /*
  22. * Careful: __kernel_fpu_begin/end() must be called with preempt disabled
  23. * and they don't touch the preempt state on their own.
  24. * If you enable preemption after __kernel_fpu_begin(), preempt notifier
  25. * should call the __kernel_fpu_end() to prevent the kernel/user FPU
  26. * state from getting corrupted. KVM for example uses this model.
  27. *
  28. * All other cases use kernel_fpu_begin/end() which disable preemption
  29. * during kernel FPU usage.
  30. */
  31. extern void __kernel_fpu_begin(void);
  32. extern void __kernel_fpu_end(void);
  33. static inline void kernel_fpu_begin(void)
  34. {
  35. WARN_ON_ONCE(!irq_fpu_usable());
  36. preempt_disable();
  37. __kernel_fpu_begin();
  38. }
  39. static inline void kernel_fpu_end(void)
  40. {
  41. __kernel_fpu_end();
  42. preempt_enable();
  43. }
  44. /*
  45. * Some instructions like VIA's padlock instructions generate a spurious
  46. * DNA fault but don't modify SSE registers. And these instructions
  47. * get used from interrupt context as well. To prevent these kernel instructions
  48. * in interrupt context interacting wrongly with other user/kernel fpu usage, we
  49. * should use them only in the context of irq_ts_save/restore()
  50. */
  51. static inline int irq_ts_save(void)
  52. {
  53. /*
  54. * If in process context and not atomic, we can take a spurious DNA fault.
  55. * Otherwise, doing clts() in process context requires disabling preemption
  56. * or some heavy lifting like kernel_fpu_begin()
  57. */
  58. if (!in_atomic())
  59. return 0;
  60. if (read_cr0() & X86_CR0_TS) {
  61. clts();
  62. return 1;
  63. }
  64. return 0;
  65. }
  66. static inline void irq_ts_restore(int TS_state)
  67. {
  68. if (TS_state)
  69. stts();
  70. }
  71. /*
  72. * The question "does this thread have fpu access?"
  73. * is slightly racy, since preemption could come in
  74. * and revoke it immediately after the test.
  75. *
  76. * However, even in that very unlikely scenario,
  77. * we can just assume we have FPU access - typically
  78. * to save the FP state - we'll just take a #NM
  79. * fault and get the FPU access back.
  80. */
  81. static inline int user_has_fpu(void)
  82. {
  83. return current->thread.fpu.has_fpu;
  84. }
  85. extern void unlazy_fpu(struct task_struct *tsk);
  86. #endif /* __ASSEMBLY__ */
  87. #endif /* _ASM_X86_I387_H */