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