fpu.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. struct task_struct *fpu_state_owner;
  16. /*
  17. * handle an exception due to the FPU being disabled
  18. */
  19. asmlinkage void fpu_disabled(struct pt_regs *regs, enum exception_code code)
  20. {
  21. struct task_struct *tsk = current;
  22. if (!user_mode(regs))
  23. die_if_no_fixup("An FPU Disabled exception happened in"
  24. " kernel space\n",
  25. regs, code);
  26. #ifdef CONFIG_FPU
  27. preempt_disable();
  28. /* transfer the last process's FPU state to memory */
  29. if (fpu_state_owner) {
  30. fpu_save(&fpu_state_owner->thread.fpu_state);
  31. fpu_state_owner->thread.uregs->epsw &= ~EPSW_FE;
  32. }
  33. /* the current process now owns the FPU state */
  34. fpu_state_owner = tsk;
  35. regs->epsw |= EPSW_FE;
  36. /* load the FPU with the current process's FPU state or invent a new
  37. * clean one if the process doesn't have one */
  38. if (is_using_fpu(tsk)) {
  39. fpu_restore(&tsk->thread.fpu_state);
  40. } else {
  41. fpu_init_state();
  42. set_using_fpu(tsk);
  43. }
  44. preempt_enable();
  45. #else
  46. {
  47. siginfo_t info;
  48. info.si_signo = SIGFPE;
  49. info.si_errno = 0;
  50. info.si_addr = (void *) tsk->thread.uregs->pc;
  51. info.si_code = FPE_FLTINV;
  52. force_sig_info(SIGFPE, &info, tsk);
  53. }
  54. #endif /* CONFIG_FPU */
  55. }
  56. /*
  57. * handle an FPU operational exception
  58. * - there's a possibility that if the FPU is asynchronous, the signal might
  59. * be meant for a process other than the current one
  60. */
  61. asmlinkage void fpu_exception(struct pt_regs *regs, enum exception_code code)
  62. {
  63. struct task_struct *tsk = fpu_state_owner;
  64. siginfo_t info;
  65. if (!user_mode(regs))
  66. die_if_no_fixup("An FPU Operation exception happened in"
  67. " kernel space\n",
  68. regs, code);
  69. if (!tsk)
  70. die_if_no_fixup("An FPU Operation exception happened,"
  71. " but the FPU is not in use",
  72. regs, code);
  73. info.si_signo = SIGFPE;
  74. info.si_errno = 0;
  75. info.si_addr = (void *) tsk->thread.uregs->pc;
  76. info.si_code = FPE_FLTINV;
  77. #ifdef CONFIG_FPU
  78. {
  79. u32 fpcr;
  80. /* get FPCR (we need to enable the FPU whilst we do this) */
  81. asm volatile(" or %1,epsw \n"
  82. #ifdef CONFIG_MN10300_PROC_MN103E010
  83. " nop \n"
  84. " nop \n"
  85. " nop \n"
  86. #endif
  87. " fmov fpcr,%0 \n"
  88. #ifdef CONFIG_MN10300_PROC_MN103E010
  89. " nop \n"
  90. " nop \n"
  91. " nop \n"
  92. #endif
  93. " and %2,epsw \n"
  94. : "=&d"(fpcr)
  95. : "i"(EPSW_FE), "i"(~EPSW_FE)
  96. );
  97. if (fpcr & FPCR_EC_Z)
  98. info.si_code = FPE_FLTDIV;
  99. else if (fpcr & FPCR_EC_O)
  100. info.si_code = FPE_FLTOVF;
  101. else if (fpcr & FPCR_EC_U)
  102. info.si_code = FPE_FLTUND;
  103. else if (fpcr & FPCR_EC_I)
  104. info.si_code = FPE_FLTRES;
  105. }
  106. #endif
  107. force_sig_info(SIGFPE, &info, tsk);
  108. }
  109. /*
  110. * save the FPU state to a signal context
  111. */
  112. int fpu_setup_sigcontext(struct fpucontext *fpucontext)
  113. {
  114. #ifdef CONFIG_FPU
  115. struct task_struct *tsk = current;
  116. if (!is_using_fpu(tsk))
  117. return 0;
  118. /* transfer the current FPU state to memory and cause fpu_init() to be
  119. * triggered by the next attempted FPU operation by the current
  120. * process.
  121. */
  122. preempt_disable();
  123. if (fpu_state_owner == tsk) {
  124. fpu_save(&tsk->thread.fpu_state);
  125. fpu_state_owner->thread.uregs->epsw &= ~EPSW_FE;
  126. fpu_state_owner = NULL;
  127. }
  128. preempt_enable();
  129. /* we no longer have a valid current FPU state */
  130. clear_using_fpu(tsk);
  131. /* transfer the saved FPU state onto the userspace stack */
  132. if (copy_to_user(fpucontext,
  133. &tsk->thread.fpu_state,
  134. min(sizeof(struct fpu_state_struct),
  135. sizeof(struct fpucontext))))
  136. return -1;
  137. return 1;
  138. #else
  139. return 0;
  140. #endif
  141. }
  142. /*
  143. * kill a process's FPU state during restoration after signal handling
  144. */
  145. void fpu_kill_state(struct task_struct *tsk)
  146. {
  147. #ifdef CONFIG_FPU
  148. /* disown anything left in the FPU */
  149. preempt_disable();
  150. if (fpu_state_owner == tsk) {
  151. fpu_state_owner->thread.uregs->epsw &= ~EPSW_FE;
  152. fpu_state_owner = NULL;
  153. }
  154. preempt_enable();
  155. #endif
  156. /* we no longer have a valid current FPU state */
  157. clear_using_fpu(tsk);
  158. }
  159. /*
  160. * restore the FPU state from a signal context
  161. */
  162. int fpu_restore_sigcontext(struct fpucontext *fpucontext)
  163. {
  164. struct task_struct *tsk = current;
  165. int ret;
  166. /* load up the old FPU state */
  167. ret = copy_from_user(&tsk->thread.fpu_state,
  168. fpucontext,
  169. min(sizeof(struct fpu_state_struct),
  170. sizeof(struct fpucontext)));
  171. if (!ret)
  172. set_using_fpu(tsk);
  173. return ret;
  174. }
  175. /*
  176. * fill in the FPU structure for a core dump
  177. */
  178. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpreg)
  179. {
  180. struct task_struct *tsk = current;
  181. int fpvalid;
  182. fpvalid = is_using_fpu(tsk);
  183. if (fpvalid) {
  184. unlazy_fpu(tsk);
  185. memcpy(fpreg, &tsk->thread.fpu_state, sizeof(*fpreg));
  186. }
  187. return fpvalid;
  188. }