fpu.c 1.6 KB

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