fpmodule.c 5.1 KB

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