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. #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. * save the FPU state to a signal context
  63. */
  64. int fpu_setup_sigcontext(struct fpucontext *fpucontext)
  65. {
  66. struct task_struct *tsk = current;
  67. if (!is_using_fpu(tsk))
  68. return 0;
  69. /* transfer the current FPU state to memory and cause fpu_init() to be
  70. * triggered by the next attempted FPU operation by the current
  71. * process.
  72. */
  73. preempt_disable();
  74. #ifndef CONFIG_LAZY_SAVE_FPU
  75. if (tsk->thread.fpu_flags & THREAD_HAS_FPU) {
  76. fpu_save(&tsk->thread.fpu_state);
  77. tsk->thread.uregs->epsw &= ~EPSW_FE;
  78. tsk->thread.fpu_flags &= ~THREAD_HAS_FPU;
  79. }
  80. #else /* !CONFIG_LAZY_SAVE_FPU */
  81. if (fpu_state_owner == tsk) {
  82. fpu_save(&tsk->thread.fpu_state);
  83. fpu_state_owner->thread.uregs->epsw &= ~EPSW_FE;
  84. fpu_state_owner = NULL;
  85. }
  86. #endif /* !CONFIG_LAZY_SAVE_FPU */
  87. preempt_enable();
  88. /* we no longer have a valid current FPU state */
  89. clear_using_fpu(tsk);
  90. /* transfer the saved FPU state onto the userspace stack */
  91. if (copy_to_user(fpucontext,
  92. &tsk->thread.fpu_state,
  93. min(sizeof(struct fpu_state_struct),
  94. sizeof(struct fpucontext))))
  95. return -1;
  96. return 1;
  97. }
  98. /*
  99. * kill a process's FPU state during restoration after signal handling
  100. */
  101. void fpu_kill_state(struct task_struct *tsk)
  102. {
  103. /* disown anything left in the FPU */
  104. preempt_disable();
  105. #ifndef CONFIG_LAZY_SAVE_FPU
  106. if (tsk->thread.fpu_flags & THREAD_HAS_FPU) {
  107. tsk->thread.uregs->epsw &= ~EPSW_FE;
  108. tsk->thread.fpu_flags &= ~THREAD_HAS_FPU;
  109. }
  110. #else /* !CONFIG_LAZY_SAVE_FPU */
  111. if (fpu_state_owner == tsk) {
  112. fpu_state_owner->thread.uregs->epsw &= ~EPSW_FE;
  113. fpu_state_owner = NULL;
  114. }
  115. #endif /* !CONFIG_LAZY_SAVE_FPU */
  116. preempt_enable();
  117. /* we no longer have a valid current FPU state */
  118. clear_using_fpu(tsk);
  119. }
  120. /*
  121. * restore the FPU state from a signal context
  122. */
  123. int fpu_restore_sigcontext(struct fpucontext *fpucontext)
  124. {
  125. struct task_struct *tsk = current;
  126. int ret;
  127. /* load up the old FPU state */
  128. ret = copy_from_user(&tsk->thread.fpu_state, fpucontext,
  129. min(sizeof(struct fpu_state_struct),
  130. sizeof(struct fpucontext)));
  131. if (!ret)
  132. set_using_fpu(tsk);
  133. return ret;
  134. }
  135. /*
  136. * fill in the FPU structure for a core dump
  137. */
  138. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpreg)
  139. {
  140. struct task_struct *tsk = current;
  141. int fpvalid;
  142. fpvalid = is_using_fpu(tsk);
  143. if (fpvalid) {
  144. unlazy_fpu(tsk);
  145. memcpy(fpreg, &tsk->thread.fpu_state, sizeof(*fpreg));
  146. }
  147. return fpvalid;
  148. }