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