fpu.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* MN10300 FPU management
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <asm/uaccess.h>
  12. #include <asm/fpu.h>
  13. #include <asm/elf.h>
  14. #include <asm/exceptions.h>
  15. #ifdef CONFIG_LAZY_SAVE_FPU
  16. struct task_struct *fpu_state_owner;
  17. #endif
  18. /*
  19. * error functions in FPU disabled exception
  20. */
  21. asmlinkage void fpu_disabled_in_kernel(struct pt_regs *regs)
  22. {
  23. die_if_no_fixup("An FPU Disabled exception happened in kernel space\n",
  24. regs, EXCEP_FPU_DISABLED);
  25. }
  26. /*
  27. * handle an FPU operational exception
  28. * - there's a possibility that if the FPU is asynchronous, the signal might
  29. * be meant for a process other than the current one
  30. */
  31. asmlinkage void fpu_exception(struct pt_regs *regs, enum exception_code code)
  32. {
  33. struct task_struct *tsk = current;
  34. siginfo_t info;
  35. u32 fpcr;
  36. if (!user_mode(regs))
  37. die_if_no_fixup("An FPU Operation exception happened in"
  38. " kernel space\n",
  39. regs, code);
  40. if (!is_using_fpu(tsk))
  41. die_if_no_fixup("An FPU Operation exception happened,"
  42. " but the FPU is not in use",
  43. regs, code);
  44. info.si_signo = SIGFPE;
  45. info.si_errno = 0;
  46. info.si_addr = (void *) tsk->thread.uregs->pc;
  47. info.si_code = FPE_FLTINV;
  48. unlazy_fpu(tsk);
  49. fpcr = tsk->thread.fpu_state.fpcr;
  50. if (fpcr & FPCR_EC_Z)
  51. info.si_code = FPE_FLTDIV;
  52. else if (fpcr & FPCR_EC_O)
  53. info.si_code = FPE_FLTOVF;
  54. else if (fpcr & FPCR_EC_U)
  55. info.si_code = FPE_FLTUND;
  56. else if (fpcr & FPCR_EC_I)
  57. info.si_code = FPE_FLTRES;
  58. force_sig_info(SIGFPE, &info, tsk);
  59. }
  60. /*
  61. * save the FPU state to a signal context
  62. */
  63. int fpu_setup_sigcontext(struct fpucontext *fpucontext)
  64. {
  65. struct task_struct *tsk = current;
  66. if (!is_using_fpu(tsk))
  67. return 0;
  68. /* transfer the current FPU state to memory and cause fpu_init() to be
  69. * triggered by the next attempted FPU operation by the current
  70. * process.
  71. */
  72. preempt_disable();
  73. #ifndef CONFIG_LAZY_SAVE_FPU
  74. if (tsk->thread.fpu_flags & THREAD_HAS_FPU) {
  75. fpu_save(&tsk->thread.fpu_state);
  76. tsk->thread.uregs->epsw &= ~EPSW_FE;
  77. tsk->thread.fpu_flags &= ~THREAD_HAS_FPU;
  78. }
  79. #else /* !CONFIG_LAZY_SAVE_FPU */
  80. if (fpu_state_owner == tsk) {
  81. fpu_save(&tsk->thread.fpu_state);
  82. fpu_state_owner->thread.uregs->epsw &= ~EPSW_FE;
  83. fpu_state_owner = NULL;
  84. }
  85. #endif /* !CONFIG_LAZY_SAVE_FPU */
  86. preempt_enable();
  87. /* we no longer have a valid current FPU state */
  88. clear_using_fpu(tsk);
  89. /* transfer the saved FPU state onto the userspace stack */
  90. if (copy_to_user(fpucontext,
  91. &tsk->thread.fpu_state,
  92. min(sizeof(struct fpu_state_struct),
  93. sizeof(struct fpucontext))))
  94. return -1;
  95. return 1;
  96. }
  97. /*
  98. * kill a process's FPU state during restoration after signal handling
  99. */
  100. void fpu_kill_state(struct task_struct *tsk)
  101. {
  102. /* disown anything left in the FPU */
  103. preempt_disable();
  104. #ifndef CONFIG_LAZY_SAVE_FPU
  105. if (tsk->thread.fpu_flags & THREAD_HAS_FPU) {
  106. tsk->thread.uregs->epsw &= ~EPSW_FE;
  107. tsk->thread.fpu_flags &= ~THREAD_HAS_FPU;
  108. }
  109. #else /* !CONFIG_LAZY_SAVE_FPU */
  110. if (fpu_state_owner == tsk) {
  111. fpu_state_owner->thread.uregs->epsw &= ~EPSW_FE;
  112. fpu_state_owner = NULL;
  113. }
  114. #endif /* !CONFIG_LAZY_SAVE_FPU */
  115. preempt_enable();
  116. /* we no longer have a valid current FPU state */
  117. clear_using_fpu(tsk);
  118. }
  119. /*
  120. * restore the FPU state from a signal context
  121. */
  122. int fpu_restore_sigcontext(struct fpucontext *fpucontext)
  123. {
  124. struct task_struct *tsk = current;
  125. int ret;
  126. /* load up the old FPU state */
  127. ret = copy_from_user(&tsk->thread.fpu_state, fpucontext,
  128. min(sizeof(struct fpu_state_struct),
  129. sizeof(struct fpucontext)));
  130. if (!ret)
  131. set_using_fpu(tsk);
  132. return ret;
  133. }
  134. /*
  135. * fill in the FPU structure for a core dump
  136. */
  137. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpreg)
  138. {
  139. struct task_struct *tsk = current;
  140. int fpvalid;
  141. fpvalid = is_using_fpu(tsk);
  142. if (fpvalid) {
  143. unlazy_fpu(tsk);
  144. memcpy(fpreg, &tsk->thread.fpu_state, sizeof(*fpreg));
  145. }
  146. return fpvalid;
  147. }