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