fpu.c 1.8 KB

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