fpu.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <linux/sched/signal.h>
  2. #include <linux/slab.h>
  3. #include <asm/processor.h>
  4. #include <asm/fpu.h>
  5. #include <asm/traps.h>
  6. #include <asm/ptrace.h>
  7. int init_fpu(struct task_struct *tsk)
  8. {
  9. if (tsk_used_math(tsk)) {
  10. if ((boot_cpu_data.flags & CPU_HAS_FPU) && tsk == current)
  11. unlazy_fpu(tsk, task_pt_regs(tsk));
  12. return 0;
  13. }
  14. /*
  15. * Memory allocation at the first usage of the FPU and other state.
  16. */
  17. if (!tsk->thread.xstate) {
  18. tsk->thread.xstate = kmem_cache_alloc(task_xstate_cachep,
  19. GFP_KERNEL);
  20. if (!tsk->thread.xstate)
  21. return -ENOMEM;
  22. }
  23. if (boot_cpu_data.flags & CPU_HAS_FPU) {
  24. struct sh_fpu_hard_struct *fp = &tsk->thread.xstate->hardfpu;
  25. memset(fp, 0, xstate_size);
  26. fp->fpscr = FPSCR_INIT;
  27. } else {
  28. struct sh_fpu_soft_struct *fp = &tsk->thread.xstate->softfpu;
  29. memset(fp, 0, xstate_size);
  30. fp->fpscr = FPSCR_INIT;
  31. }
  32. set_stopped_child_used_math(tsk);
  33. return 0;
  34. }
  35. #ifdef CONFIG_SH_FPU
  36. void __fpu_state_restore(void)
  37. {
  38. struct task_struct *tsk = current;
  39. restore_fpu(tsk);
  40. task_thread_info(tsk)->status |= TS_USEDFPU;
  41. tsk->thread.fpu_counter++;
  42. }
  43. void fpu_state_restore(struct pt_regs *regs)
  44. {
  45. struct task_struct *tsk = current;
  46. if (unlikely(!user_mode(regs))) {
  47. printk(KERN_ERR "BUG: FPU is used in kernel mode.\n");
  48. BUG();
  49. return;
  50. }
  51. if (!tsk_used_math(tsk)) {
  52. local_irq_enable();
  53. /*
  54. * does a slab alloc which can sleep
  55. */
  56. if (init_fpu(tsk)) {
  57. /*
  58. * ran out of memory!
  59. */
  60. do_group_exit(SIGKILL);
  61. return;
  62. }
  63. local_irq_disable();
  64. }
  65. grab_fpu(regs);
  66. __fpu_state_restore();
  67. }
  68. BUILD_TRAP_HANDLER(fpu_state_restore)
  69. {
  70. TRAP_HANDLER_DECL;
  71. fpu_state_restore(regs);
  72. }
  73. #endif /* CONFIG_SH_FPU */