fpmodule.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/version.h>
  21. #include <linux/config.h>
  22. /* XXX */
  23. #include <linux/errno.h>
  24. #include <linux/types.h>
  25. #include <linux/kernel.h>
  26. #include <linux/signal.h>
  27. #include <linux/sched.h>
  28. #include <linux/init.h>
  29. /* XXX */
  30. #include "softfloat.h"
  31. #include "fpopcode.h"
  32. #include "fpmodule.h"
  33. #include "fpa11.inl"
  34. /* kernel symbols required for signal handling */
  35. typedef struct task_struct* PTASK;
  36. #ifdef MODULE
  37. void fp_send_sig(unsigned long sig, PTASK p, int priv);
  38. #if LINUX_VERSION_CODE > 0x20115
  39. MODULE_AUTHOR("Scott Bambrough <scottb@rebel.com>");
  40. MODULE_DESCRIPTION("NWFPE floating point emulator");
  41. #endif
  42. #else
  43. #define fp_send_sig send_sig
  44. #define kern_fp_enter fp_enter
  45. extern char fpe_type[];
  46. #endif
  47. /* kernel function prototypes required */
  48. void fp_setup(void);
  49. /* external declarations for saved kernel symbols */
  50. extern void (*kern_fp_enter)(void);
  51. /* Original value of fp_enter from kernel before patched by fpe_init. */
  52. static void (*orig_fp_enter)(void);
  53. /* forward declarations */
  54. extern void nwfpe_enter(void);
  55. #ifdef MODULE
  56. /*
  57. * Return 0 if we can be unloaded. This can only happen if
  58. * kern_fp_enter is still pointing at nwfpe_enter
  59. */
  60. static int fpe_unload(void)
  61. {
  62. return (kern_fp_enter == nwfpe_enter) ? 0 : 1;
  63. }
  64. #endif
  65. static int __init fpe_init(void)
  66. {
  67. if (sizeof(FPA11) > sizeof(union fp_state)) {
  68. printk(KERN_ERR "nwfpe: bad structure size\n");
  69. return -EINVAL;
  70. }
  71. if (sizeof(FPREG) != 12) {
  72. printk(KERN_ERR "nwfpe: bad register size\n");
  73. return -EINVAL;
  74. }
  75. #ifdef MODULE
  76. if (!mod_member_present(&__this_module, can_unload))
  77. return -EINVAL;
  78. __this_module.can_unload = fpe_unload;
  79. #else
  80. if (fpe_type[0] && strcmp(fpe_type, "nwfpe"))
  81. return 0;
  82. #endif
  83. /* Display title, version and copyright information. */
  84. printk(KERN_WARNING "NetWinder Floating Point Emulator V0.95 "
  85. "(c) 1998-1999 Rebel.com\n");
  86. /* Save pointer to the old FP handler and then patch ourselves in */
  87. orig_fp_enter = kern_fp_enter;
  88. kern_fp_enter = nwfpe_enter;
  89. return 0;
  90. }
  91. static void __exit fpe_exit(void)
  92. {
  93. /* Restore the values we saved earlier. */
  94. kern_fp_enter = orig_fp_enter;
  95. }
  96. /*
  97. ScottB: November 4, 1998
  98. Moved this function out of softfloat-specialize into fpmodule.c.
  99. This effectively isolates all the changes required for integrating with the
  100. Linux kernel into fpmodule.c. Porting to NetBSD should only require modifying
  101. fpmodule.c to integrate with the NetBSD kernel (I hope!).
  102. [1/1/99: Not quite true any more unfortunately. There is Linux-specific
  103. code to access data in user space in some other source files at the
  104. moment (grep for get_user / put_user calls). --philb]
  105. float_exception_flags is a global variable in SoftFloat.
  106. This function is called by the SoftFloat routines to raise a floating
  107. point exception. We check the trap enable byte in the FPSR, and raise
  108. a SIGFPE exception if necessary. If not the relevant bits in the
  109. cumulative exceptions flag byte are set and we return.
  110. */
  111. void float_raise(signed char flags)
  112. {
  113. register unsigned int fpsr, cumulativeTraps;
  114. #ifdef CONFIG_DEBUG_USER
  115. printk(KERN_DEBUG "NWFPE: %s[%d] takes exception %08x at %p from %08x\n",
  116. current->comm, current->pid, flags,
  117. __builtin_return_address(0), GET_USERREG()[15]);
  118. #endif
  119. /* Keep SoftFloat exception flags up to date. */
  120. float_exception_flags |= flags;
  121. /* Read fpsr and initialize the cumulativeTraps. */
  122. fpsr = readFPSR();
  123. cumulativeTraps = 0;
  124. /* For each type of exception, the cumulative trap exception bit is only
  125. set if the corresponding trap enable bit is not set. */
  126. if ((!(fpsr & BIT_IXE)) && (flags & BIT_IXC))
  127. cumulativeTraps |= BIT_IXC;
  128. if ((!(fpsr & BIT_UFE)) && (flags & BIT_UFC))
  129. cumulativeTraps |= BIT_UFC;
  130. if ((!(fpsr & BIT_OFE)) && (flags & BIT_OFC))
  131. cumulativeTraps |= BIT_OFC;
  132. if ((!(fpsr & BIT_DZE)) && (flags & BIT_DZC))
  133. cumulativeTraps |= BIT_DZC;
  134. if ((!(fpsr & BIT_IOE)) && (flags & BIT_IOC))
  135. cumulativeTraps |= BIT_IOC;
  136. /* Set the cumulative exceptions flags. */
  137. if (cumulativeTraps)
  138. writeFPSR(fpsr | cumulativeTraps);
  139. /* Raise an exception if necessary. */
  140. if (fpsr & (flags << 16))
  141. fp_send_sig(SIGFPE, current, 1);
  142. }
  143. module_init(fpe_init);
  144. module_exit(fpe_exit);