fpu.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #include <asm/system.h>
  16. #ifdef CONFIG_LAZY_SAVE_FPU
  17. struct task_struct *fpu_state_owner;
  18. #endif
  19. /*
  20. * error functions in FPU disabled exception
  21. */
  22. asmlinkage void fpu_disabled_in_kernel(struct pt_regs *regs)
  23. {
  24. die_if_no_fixup("An FPU Disabled exception happened in kernel space\n",
  25. regs, EXCEP_FPU_DISABLED);
  26. }
  27. /*
  28. * handle an FPU operational exception
  29. * - there's a possibility that if the FPU is asynchronous, the signal might
  30. * be meant for a process other than the current one
  31. */
  32. asmlinkage void fpu_exception(struct pt_regs *regs, enum exception_code code)
  33. {
  34. struct task_struct *tsk = current;
  35. siginfo_t info;
  36. u32 fpcr;
  37. if (!user_mode(regs))
  38. die_if_no_fixup("An FPU Operation exception happened in"
  39. " kernel space\n",
  40. regs, code);
  41. if (!is_using_fpu(tsk))
  42. die_if_no_fixup("An FPU Operation exception happened,"
  43. " but the FPU is not in use",
  44. regs, code);
  45. info.si_signo = SIGFPE;
  46. info.si_errno = 0;
  47. info.si_addr = (void *) tsk->thread.uregs->pc;
  48. info.si_code = FPE_FLTINV;
  49. unlazy_fpu(tsk);
  50. fpcr = tsk->thread.fpu_state.fpcr;
  51. if (fpcr & FPCR_EC_Z)
  52. info.si_code = FPE_FLTDIV;
  53. else if (fpcr & FPCR_EC_O)
  54. info.si_code = FPE_FLTOVF;
  55. else if (fpcr & FPCR_EC_U)
  56. info.si_code = FPE_FLTUND;
  57. else if (fpcr & FPCR_EC_I)
  58. info.si_code = FPE_FLTRES;
  59. force_sig_info(SIGFPE, &info, tsk);
  60. }
  61. /*
  62. * handle an FPU invalid_op exception
  63. * - Derived from DO_EINFO() macro in arch/mn10300/kernel/traps.c
  64. */
  65. asmlinkage void fpu_invalid_op(struct pt_regs *regs, enum exception_code code)
  66. {
  67. siginfo_t info;
  68. if (!user_mode(regs))
  69. die_if_no_fixup("FPU invalid opcode", regs, code);
  70. info.si_signo = SIGILL;
  71. info.si_errno = 0;
  72. info.si_code = ILL_COPROC;
  73. info.si_addr = (void *) regs->pc;
  74. force_sig_info(info.si_signo, &info, current);
  75. }
  76. /*
  77. * save the FPU state to a signal context
  78. */
  79. int fpu_setup_sigcontext(struct fpucontext *fpucontext)
  80. {
  81. struct task_struct *tsk = current;
  82. if (!is_using_fpu(tsk))
  83. return 0;
  84. /* transfer the current FPU state to memory and cause fpu_init() to be
  85. * triggered by the next attempted FPU operation by the current
  86. * process.
  87. */
  88. preempt_disable();
  89. #ifndef CONFIG_LAZY_SAVE_FPU
  90. if (tsk->thread.fpu_flags & THREAD_HAS_FPU) {
  91. fpu_save(&tsk->thread.fpu_state);
  92. tsk->thread.uregs->epsw &= ~EPSW_FE;
  93. tsk->thread.fpu_flags &= ~THREAD_HAS_FPU;
  94. }
  95. #else /* !CONFIG_LAZY_SAVE_FPU */
  96. if (fpu_state_owner == tsk) {
  97. fpu_save(&tsk->thread.fpu_state);
  98. fpu_state_owner->thread.uregs->epsw &= ~EPSW_FE;
  99. fpu_state_owner = NULL;
  100. }
  101. #endif /* !CONFIG_LAZY_SAVE_FPU */
  102. preempt_enable();
  103. /* we no longer have a valid current FPU state */
  104. clear_using_fpu(tsk);
  105. /* transfer the saved FPU state onto the userspace stack */
  106. if (copy_to_user(fpucontext,
  107. &tsk->thread.fpu_state,
  108. min(sizeof(struct fpu_state_struct),
  109. sizeof(struct fpucontext))))
  110. return -1;
  111. return 1;
  112. }
  113. /*
  114. * kill a process's FPU state during restoration after signal handling
  115. */
  116. void fpu_kill_state(struct task_struct *tsk)
  117. {
  118. /* disown anything left in the FPU */
  119. preempt_disable();
  120. #ifndef CONFIG_LAZY_SAVE_FPU
  121. if (tsk->thread.fpu_flags & THREAD_HAS_FPU) {
  122. tsk->thread.uregs->epsw &= ~EPSW_FE;
  123. tsk->thread.fpu_flags &= ~THREAD_HAS_FPU;
  124. }
  125. #else /* !CONFIG_LAZY_SAVE_FPU */
  126. if (fpu_state_owner == tsk) {
  127. fpu_state_owner->thread.uregs->epsw &= ~EPSW_FE;
  128. fpu_state_owner = NULL;
  129. }
  130. #endif /* !CONFIG_LAZY_SAVE_FPU */
  131. preempt_enable();
  132. /* we no longer have a valid current FPU state */
  133. clear_using_fpu(tsk);
  134. }
  135. /*
  136. * restore the FPU state from a signal context
  137. */
  138. int fpu_restore_sigcontext(struct fpucontext *fpucontext)
  139. {
  140. struct task_struct *tsk = current;
  141. int ret;
  142. /* load up the old FPU state */
  143. ret = copy_from_user(&tsk->thread.fpu_state, fpucontext,
  144. min(sizeof(struct fpu_state_struct),
  145. sizeof(struct fpucontext)));
  146. if (!ret)
  147. set_using_fpu(tsk);
  148. return ret;
  149. }
  150. /*
  151. * fill in the FPU structure for a core dump
  152. */
  153. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpreg)
  154. {
  155. struct task_struct *tsk = current;
  156. int fpvalid;
  157. fpvalid = is_using_fpu(tsk);
  158. if (fpvalid) {
  159. unlazy_fpu(tsk);
  160. memcpy(fpreg, &tsk->thread.fpu_state, sizeof(*fpreg));
  161. }
  162. return fpvalid;
  163. }