fpmodule.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. NetWinder Floating Point Emulator
  3. (c) Rebel.com, 1998-1999
  4. (c) Philip Blundell, 1998-1999
  5. Direct questions, comments to Scott Bambrough <scottb@netwinder.org>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include "fpa11.h"
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. /* XXX */
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/kernel.h>
  25. #include <linux/signal.h>
  26. #include <linux/sched.h>
  27. #include <linux/init.h>
  28. #include <asm/thread_notify.h>
  29. #include "softfloat.h"
  30. #include "fpopcode.h"
  31. #include "fpmodule.h"
  32. #include "fpa11.inl"
  33. /* kernel symbols required for signal handling */
  34. #ifdef CONFIG_FPE_NWFPE_XP
  35. #define NWFPE_BITS "extended"
  36. #else
  37. #define NWFPE_BITS "double"
  38. #endif
  39. #ifdef MODULE
  40. void fp_send_sig(unsigned long sig, struct task_struct *p, int priv);
  41. #else
  42. #define fp_send_sig send_sig
  43. #define kern_fp_enter fp_enter
  44. extern char fpe_type[];
  45. #endif
  46. static int nwfpe_notify(struct notifier_block *self, unsigned long cmd, void *v)
  47. {
  48. struct thread_info *thread = v;
  49. if (cmd == THREAD_NOTIFY_FLUSH)
  50. nwfpe_init_fpa(&thread->fpstate);
  51. return NOTIFY_DONE;
  52. }
  53. static struct notifier_block nwfpe_notifier_block = {
  54. .notifier_call = nwfpe_notify,
  55. };
  56. /* kernel function prototypes required */
  57. void fp_setup(void);
  58. /* external declarations for saved kernel symbols */
  59. extern void (*kern_fp_enter)(void);
  60. /* Original value of fp_enter from kernel before patched by fpe_init. */
  61. static void (*orig_fp_enter)(void);
  62. /* forward declarations */
  63. extern void nwfpe_enter(void);
  64. static int __init fpe_init(void)
  65. {
  66. if (sizeof(FPA11) > sizeof(union fp_state)) {
  67. pr_err("nwfpe: bad structure size\n");
  68. return -EINVAL;
  69. }
  70. if (sizeof(FPREG) != 12) {
  71. pr_err("nwfpe: bad register size\n");
  72. return -EINVAL;
  73. }
  74. if (fpe_type[0] && strcmp(fpe_type, "nwfpe"))
  75. return 0;
  76. /* Display title, version and copyright information. */
  77. pr_info("NetWinder Floating Point Emulator V0.97 ("
  78. NWFPE_BITS " precision)\n");
  79. thread_register_notifier(&nwfpe_notifier_block);
  80. /* Save pointer to the old FP handler and then patch ourselves in */
  81. orig_fp_enter = kern_fp_enter;
  82. kern_fp_enter = nwfpe_enter;
  83. return 0;
  84. }
  85. static void __exit fpe_exit(void)
  86. {
  87. thread_unregister_notifier(&nwfpe_notifier_block);
  88. /* Restore the values we saved earlier. */
  89. kern_fp_enter = orig_fp_enter;
  90. }
  91. /*
  92. ScottB: November 4, 1998
  93. Moved this function out of softfloat-specialize into fpmodule.c.
  94. This effectively isolates all the changes required for integrating with the
  95. Linux kernel into fpmodule.c. Porting to NetBSD should only require modifying
  96. fpmodule.c to integrate with the NetBSD kernel (I hope!).
  97. [1/1/99: Not quite true any more unfortunately. There is Linux-specific
  98. code to access data in user space in some other source files at the
  99. moment (grep for get_user / put_user calls). --philb]
  100. This function is called by the SoftFloat routines to raise a floating
  101. point exception. We check the trap enable byte in the FPSR, and raise
  102. a SIGFPE exception if necessary. If not the relevant bits in the
  103. cumulative exceptions flag byte are set and we return.
  104. */
  105. #ifdef CONFIG_DEBUG_USER
  106. /* By default, ignore inexact errors as there are far too many of them to log */
  107. static int debug = ~BIT_IXC;
  108. #endif
  109. void float_raise(signed char flags)
  110. {
  111. register unsigned int fpsr, cumulativeTraps;
  112. #ifdef CONFIG_DEBUG_USER
  113. if (flags & debug)
  114. printk(KERN_DEBUG
  115. "NWFPE: %s[%d] takes exception %08x at %pf from %08lx\n",
  116. current->comm, current->pid, flags,
  117. __builtin_return_address(0), GET_USERREG()->ARM_pc);
  118. #endif
  119. /* Read fpsr and initialize the cumulativeTraps. */
  120. fpsr = readFPSR();
  121. cumulativeTraps = 0;
  122. /* For each type of exception, the cumulative trap exception bit is only
  123. set if the corresponding trap enable bit is not set. */
  124. if ((!(fpsr & BIT_IXE)) && (flags & BIT_IXC))
  125. cumulativeTraps |= BIT_IXC;
  126. if ((!(fpsr & BIT_UFE)) && (flags & BIT_UFC))
  127. cumulativeTraps |= BIT_UFC;
  128. if ((!(fpsr & BIT_OFE)) && (flags & BIT_OFC))
  129. cumulativeTraps |= BIT_OFC;
  130. if ((!(fpsr & BIT_DZE)) && (flags & BIT_DZC))
  131. cumulativeTraps |= BIT_DZC;
  132. if ((!(fpsr & BIT_IOE)) && (flags & BIT_IOC))
  133. cumulativeTraps |= BIT_IOC;
  134. /* Set the cumulative exceptions flags. */
  135. if (cumulativeTraps)
  136. writeFPSR(fpsr | cumulativeTraps);
  137. /* Raise an exception if necessary. */
  138. if (fpsr & (flags << 16))
  139. fp_send_sig(SIGFPE, current, 1);
  140. }
  141. module_init(fpe_init);
  142. module_exit(fpe_exit);
  143. MODULE_AUTHOR("Scott Bambrough <scottb@rebel.com>");
  144. MODULE_DESCRIPTION("NWFPE floating point emulator (" NWFPE_BITS " precision)");
  145. MODULE_LICENSE("GPL");
  146. #ifdef CONFIG_DEBUG_USER
  147. module_param(debug, int, 0644);
  148. #endif