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