fpmodule.c 5.0 KB

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