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