fpmodule.c 4.95 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

/*
    NetWinder Floating Point Emulator
    (c) Rebel.com, 1998-1999
    (c) Philip Blundell, 1998-1999

    Direct questions, comments to Scott Bambrough <scottb@netwinder.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

24 25
#include "fpa11.h"

Linus Torvalds's avatar
Linus Torvalds committed
26 27 28 29 30
#include <linux/module.h>
#include <linux/version.h>
#include <linux/config.h>

/* XXX */
Russell King's avatar
Russell King committed
31
#include <linux/errno.h>
Linus Torvalds's avatar
Linus Torvalds committed
32 33 34 35 36 37 38 39 40 41 42 43 44
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/init.h>
/* XXX */

#include "softfloat.h"
#include "fpopcode.h"
#include "fpmodule.h"
#include "fpa11.inl"

/* kernel symbols required for signal handling */
45 46 47 48 49 50
#ifdef CONFIG_FPE_NWFPE_XP
#define NWFPE_BITS "extended"
#else
#define NWFPE_BITS "double"
#endif

Linus Torvalds's avatar
Linus Torvalds committed
51
#ifdef MODULE
52
void fp_send_sig(unsigned long sig, struct task_struct *p, int priv);
Linus Torvalds's avatar
Linus Torvalds committed
53 54
#if LINUX_VERSION_CODE > 0x20115
MODULE_AUTHOR("Scott Bambrough <scottb@rebel.com>");
55
MODULE_DESCRIPTION("NWFPE floating point emulator (" NWFPE_BITS " precision)");
Linus Torvalds's avatar
Linus Torvalds committed
56 57 58 59 60
#endif

#else
#define fp_send_sig	send_sig
#define kern_fp_enter	fp_enter
Linus Torvalds's avatar
Linus Torvalds committed
61 62

extern char fpe_type[];
Linus Torvalds's avatar
Linus Torvalds committed
63 64 65 66 67 68 69
#endif

/* kernel function prototypes required */
void fp_setup(void);

/* external declarations for saved kernel symbols */
extern void (*kern_fp_enter)(void);
70
extern void (*fp_init)(union fp_state *);
Linus Torvalds's avatar
Linus Torvalds committed
71

72
/* Original value of fp_enter from kernel before patched by fpe_init. */
Linus Torvalds's avatar
Linus Torvalds committed
73
static void (*orig_fp_enter)(void);
74
static void (*orig_fp_init)(union fp_state *);
Linus Torvalds's avatar
Linus Torvalds committed
75 76 77 78

/* forward declarations */
extern void nwfpe_enter(void);

Linus Torvalds's avatar
Linus Torvalds committed
79
static int __init fpe_init(void)
Linus Torvalds's avatar
Linus Torvalds committed
80
{
81 82 83 84 85 86 87 88 89 90 91 92 93
	if (sizeof(FPA11) > sizeof(union fp_state)) {
		printk(KERN_ERR "nwfpe: bad structure size\n");
		return -EINVAL;
	}

	if (sizeof(FPREG) != 12) {
		printk(KERN_ERR "nwfpe: bad register size\n");
		return -EINVAL;
	}
	if (fpe_type[0] && strcmp(fpe_type, "nwfpe"))
		return 0;

	/* Display title, version and copyright information. */
94 95
	printk(KERN_WARNING "NetWinder Floating Point Emulator V0.97 ("
	       NWFPE_BITS " precision)\n");
96 97 98 99 100

	/* Save pointer to the old FP handler and then patch ourselves in */
	orig_fp_enter = kern_fp_enter;
	orig_fp_init = fp_init;
	kern_fp_enter = nwfpe_enter;
101
	fp_init = nwfpe_init_fpa;
102 103

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
104 105
}

Linus Torvalds's avatar
Linus Torvalds committed
106
static void __exit fpe_exit(void)
Linus Torvalds's avatar
Linus Torvalds committed
107
{
108 109 110
	/* Restore the values we saved earlier. */
	kern_fp_enter = orig_fp_enter;
	fp_init = orig_fp_init;
Linus Torvalds's avatar
Linus Torvalds committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
}

/*
ScottB:  November 4, 1998

Moved this function out of softfloat-specialize into fpmodule.c.
This effectively isolates all the changes required for integrating with the
Linux kernel into fpmodule.c.  Porting to NetBSD should only require modifying
fpmodule.c to integrate with the NetBSD kernel (I hope!).

[1/1/99: Not quite true any more unfortunately.  There is Linux-specific
code to access data in user space in some other source files at the 
moment (grep for get_user / put_user calls).  --philb]

float_exception_flags is a global variable in SoftFloat.

This function is called by the SoftFloat routines to raise a floating
point exception.  We check the trap enable byte in the FPSR, and raise
a SIGFPE exception if necessary.  If not the relevant bits in the 
cumulative exceptions flag byte are set and we return.
*/

void float_raise(signed char flags)
{
135 136
	register unsigned int fpsr, cumulativeTraps;

Linus Torvalds's avatar
Linus Torvalds committed
137
#ifdef CONFIG_DEBUG_USER
138 139 140 141
	printk(KERN_DEBUG
	       "NWFPE: %s[%d] takes exception %08x at %p from %08x\n",
	       current->comm, current->pid, flags,
	       __builtin_return_address(0), GET_USERREG()[15]);
Linus Torvalds's avatar
Linus Torvalds committed
142 143
#endif

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	/* Keep SoftFloat exception flags up to date.  */
	float_exception_flags |= flags;

	/* Read fpsr and initialize the cumulativeTraps.  */
	fpsr = readFPSR();
	cumulativeTraps = 0;

	/* For each type of exception, the cumulative trap exception bit is only
	   set if the corresponding trap enable bit is not set.  */
	if ((!(fpsr & BIT_IXE)) && (flags & BIT_IXC))
		cumulativeTraps |= BIT_IXC;
	if ((!(fpsr & BIT_UFE)) && (flags & BIT_UFC))
		cumulativeTraps |= BIT_UFC;
	if ((!(fpsr & BIT_OFE)) && (flags & BIT_OFC))
		cumulativeTraps |= BIT_OFC;
	if ((!(fpsr & BIT_DZE)) && (flags & BIT_DZC))
		cumulativeTraps |= BIT_DZC;
	if ((!(fpsr & BIT_IOE)) && (flags & BIT_IOC))
		cumulativeTraps |= BIT_IOC;

	/* Set the cumulative exceptions flags.  */
	if (cumulativeTraps)
		writeFPSR(fpsr | cumulativeTraps);

	/* Raise an exception if necessary.  */
	if (fpsr & (flags << 16))
		fp_send_sig(SIGFPE, current, 1);
Linus Torvalds's avatar
Linus Torvalds committed
171 172 173 174
}

module_init(fpe_init);
module_exit(fpe_exit);