fpu.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. int ret;
  56. /*
  57. * does a slab alloc which can sleep
  58. */
  59. local_irq_enable();
  60. ret = init_fpu(tsk);
  61. local_irq_disable();
  62. if (ret) {
  63. /*
  64. * ran out of memory!
  65. */
  66. force_sig(SIGKILL);
  67. return;
  68. }
  69. }
  70. grab_fpu(regs);
  71. __fpu_state_restore();
  72. }
  73. BUILD_TRAP_HANDLER(fpu_state_restore)
  74. {
  75. TRAP_HANDLER_DECL;
  76. fpu_state_restore(regs);
  77. }
  78. #endif /* CONFIG_SH_FPU */