xref: /linux/arch/mips/math-emu/cp1emu.c (revision da0bac33413b2888d3623dad3ad19ce76b688f07)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * cp1emu.c: a MIPS coprocessor 1 (fpu) instruction emulator
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * MIPS floating point support
51da177e4SLinus Torvalds  * Copyright (C) 1994-2000 Algorithmics Ltd.
61da177e4SLinus Torvalds  * http://www.algor.co.uk
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
91da177e4SLinus Torvalds  * Copyright (C) 2000  MIPS Technologies, Inc.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *  This program is free software; you can distribute it and/or modify it
121da177e4SLinus Torvalds  *  under the terms of the GNU General Public License (Version 2) as
131da177e4SLinus Torvalds  *  published by the Free Software Foundation.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *  This program is distributed in the hope it will be useful, but WITHOUT
161da177e4SLinus Torvalds  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
171da177e4SLinus Torvalds  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
181da177e4SLinus Torvalds  *  for more details.
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *  You should have received a copy of the GNU General Public License along
211da177e4SLinus Torvalds  *  with this program; if not, write to the Free Software Foundation, Inc.,
221da177e4SLinus Torvalds  *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * A complete emulator for MIPS coprocessor 1 instructions.  This is
251da177e4SLinus Torvalds  * required for #float(switch) or #float(trap), where it catches all
261da177e4SLinus Torvalds  * COP1 instructions via the "CoProcessor Unusable" exception.
271da177e4SLinus Torvalds  *
281da177e4SLinus Torvalds  * More surprisingly it is also required for #float(ieee), to help out
291da177e4SLinus Torvalds  * the hardware fpu at the boundaries of the IEEE-754 representation
301da177e4SLinus Torvalds  * (denormalised values, infinities, underflow, etc).  It is made
311da177e4SLinus Torvalds  * quite nasty because emulation of some non-COP1 instructions is
321da177e4SLinus Torvalds  * required, e.g. in branch delay slots.
331da177e4SLinus Torvalds  *
341da177e4SLinus Torvalds  * Note if you know that you won't have an fpu, then you'll get much
351da177e4SLinus Torvalds  * better performance by compiling with -msoft-float!
361da177e4SLinus Torvalds  */
371da177e4SLinus Torvalds #include <linux/sched.h>
3883fd38caSAtsushi Nemoto #include <linux/debugfs.h>
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #include <asm/inst.h>
411da177e4SLinus Torvalds #include <asm/bootinfo.h>
421da177e4SLinus Torvalds #include <asm/processor.h>
431da177e4SLinus Torvalds #include <asm/ptrace.h>
441da177e4SLinus Torvalds #include <asm/signal.h>
451da177e4SLinus Torvalds #include <asm/mipsregs.h>
461da177e4SLinus Torvalds #include <asm/fpu_emulator.h>
471da177e4SLinus Torvalds #include <asm/uaccess.h>
481da177e4SLinus Torvalds #include <asm/branch.h>
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #include "ieee754.h"
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds /* Strap kernel emulator for full MIPS IV emulation */
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds #ifdef __mips
551da177e4SLinus Torvalds #undef __mips
561da177e4SLinus Torvalds #endif
571da177e4SLinus Torvalds #define __mips 4
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds /* Function which emulates a floating point instruction. */
601da177e4SLinus Torvalds 
61eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
621da177e4SLinus Torvalds 	mips_instruction);
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32
651da177e4SLinus Torvalds static int fpux_emu(struct pt_regs *,
66eae89076SAtsushi Nemoto 	struct mips_fpu_struct *, mips_instruction);
671da177e4SLinus Torvalds #endif
681da177e4SLinus Torvalds 
69eae89076SAtsushi Nemoto /* Further private data for which no space exists in mips_fpu_struct */
701da177e4SLinus Torvalds 
714a99d1e2SRalf Baechle struct mips_fpu_emulator_stats fpuemustats;
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /* Control registers */
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds #define FPCREG_RID	0	/* $0  = revision id */
761da177e4SLinus Torvalds #define FPCREG_CSR	31	/* $31 = csr */
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds /* Convert Mips rounding mode (0..3) to IEEE library modes. */
791da177e4SLinus Torvalds static const unsigned char ieee_rm[4] = {
80cd21dfcfSRalf Baechle 	[FPU_CSR_RN] = IEEE754_RN,
81cd21dfcfSRalf Baechle 	[FPU_CSR_RZ] = IEEE754_RZ,
82cd21dfcfSRalf Baechle 	[FPU_CSR_RU] = IEEE754_RU,
83cd21dfcfSRalf Baechle 	[FPU_CSR_RD] = IEEE754_RD,
84cd21dfcfSRalf Baechle };
85cd21dfcfSRalf Baechle /* Convert IEEE library modes to Mips rounding mode (0..3). */
86cd21dfcfSRalf Baechle static const unsigned char mips_rm[4] = {
87cd21dfcfSRalf Baechle 	[IEEE754_RN] = FPU_CSR_RN,
88cd21dfcfSRalf Baechle 	[IEEE754_RZ] = FPU_CSR_RZ,
89cd21dfcfSRalf Baechle 	[IEEE754_RD] = FPU_CSR_RD,
90cd21dfcfSRalf Baechle 	[IEEE754_RU] = FPU_CSR_RU,
911da177e4SLinus Torvalds };
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds #if __mips >= 4
941da177e4SLinus Torvalds /* convert condition code register number to csr bit */
951da177e4SLinus Torvalds static const unsigned int fpucondbit[8] = {
961da177e4SLinus Torvalds 	FPU_CSR_COND0,
971da177e4SLinus Torvalds 	FPU_CSR_COND1,
981da177e4SLinus Torvalds 	FPU_CSR_COND2,
991da177e4SLinus Torvalds 	FPU_CSR_COND3,
1001da177e4SLinus Torvalds 	FPU_CSR_COND4,
1011da177e4SLinus Torvalds 	FPU_CSR_COND5,
1021da177e4SLinus Torvalds 	FPU_CSR_COND6,
1031da177e4SLinus Torvalds 	FPU_CSR_COND7
1041da177e4SLinus Torvalds };
1051da177e4SLinus Torvalds #endif
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds /*
1091da177e4SLinus Torvalds  * Redundant with logic already in kernel/branch.c,
1101da177e4SLinus Torvalds  * embedded in compute_return_epc.  At some point,
1111da177e4SLinus Torvalds  * a single subroutine should be used across both
1121da177e4SLinus Torvalds  * modules.
1131da177e4SLinus Torvalds  */
1141da177e4SLinus Torvalds static int isBranchInstr(mips_instruction * i)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	switch (MIPSInst_OPCODE(*i)) {
1171da177e4SLinus Torvalds 	case spec_op:
1181da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(*i)) {
1191da177e4SLinus Torvalds 		case jalr_op:
1201da177e4SLinus Torvalds 		case jr_op:
1211da177e4SLinus Torvalds 			return 1;
1221da177e4SLinus Torvalds 		}
1231da177e4SLinus Torvalds 		break;
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	case bcond_op:
1261da177e4SLinus Torvalds 		switch (MIPSInst_RT(*i)) {
1271da177e4SLinus Torvalds 		case bltz_op:
1281da177e4SLinus Torvalds 		case bgez_op:
1291da177e4SLinus Torvalds 		case bltzl_op:
1301da177e4SLinus Torvalds 		case bgezl_op:
1311da177e4SLinus Torvalds 		case bltzal_op:
1321da177e4SLinus Torvalds 		case bgezal_op:
1331da177e4SLinus Torvalds 		case bltzall_op:
1341da177e4SLinus Torvalds 		case bgezall_op:
1351da177e4SLinus Torvalds 			return 1;
1361da177e4SLinus Torvalds 		}
1371da177e4SLinus Torvalds 		break;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	case j_op:
1401da177e4SLinus Torvalds 	case jal_op:
1411da177e4SLinus Torvalds 	case jalx_op:
1421da177e4SLinus Torvalds 	case beq_op:
1431da177e4SLinus Torvalds 	case bne_op:
1441da177e4SLinus Torvalds 	case blez_op:
1451da177e4SLinus Torvalds 	case bgtz_op:
1461da177e4SLinus Torvalds 	case beql_op:
1471da177e4SLinus Torvalds 	case bnel_op:
1481da177e4SLinus Torvalds 	case blezl_op:
1491da177e4SLinus Torvalds 	case bgtzl_op:
1501da177e4SLinus Torvalds 		return 1;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	case cop0_op:
1531da177e4SLinus Torvalds 	case cop1_op:
1541da177e4SLinus Torvalds 	case cop2_op:
1551da177e4SLinus Torvalds 	case cop1x_op:
1561da177e4SLinus Torvalds 		if (MIPSInst_RS(*i) == bc_op)
1571da177e4SLinus Torvalds 			return 1;
1581da177e4SLinus Torvalds 		break;
1591da177e4SLinus Torvalds 	}
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	return 0;
1621da177e4SLinus Torvalds }
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds /*
1651da177e4SLinus Torvalds  * In the Linux kernel, we support selection of FPR format on the
166*da0bac33SDavid Daney  * basis of the Status.FR bit.  If an FPU is not present, the FR bit
167*da0bac33SDavid Daney  * is hardwired to zero, which would imply a 32-bit FPU even for
168*da0bac33SDavid Daney  * 64-bit CPUs.  For 64-bit kernels with no FPU we use TIF_32BIT_REGS
169*da0bac33SDavid Daney  * as a proxy for the FR bit so that a 64-bit FPU is emulated.  In any
170*da0bac33SDavid Daney  * case, for a 32-bit kernel which uses the O32 MIPS ABI, only the
171*da0bac33SDavid Daney  * even FPRs are used (Status.FR = 0).
1721da177e4SLinus Torvalds  */
173*da0bac33SDavid Daney static inline int cop1_64bit(struct pt_regs *xcp)
174*da0bac33SDavid Daney {
175*da0bac33SDavid Daney 	if (cpu_has_fpu)
176*da0bac33SDavid Daney 		return xcp->cp0_status & ST0_FR;
177*da0bac33SDavid Daney #ifdef CONFIG_64BIT
178*da0bac33SDavid Daney 	return !test_thread_flag(TIF_32BIT_REGS);
1791da177e4SLinus Torvalds #else
180*da0bac33SDavid Daney 	return 0;
1811da177e4SLinus Torvalds #endif
182*da0bac33SDavid Daney }
1831da177e4SLinus Torvalds 
184*da0bac33SDavid Daney #define SIFROMREG(si, x) ((si) = cop1_64bit(xcp) || !(x & 1) ? \
185*da0bac33SDavid Daney 			(int)ctx->fpr[x] : (int)(ctx->fpr[x & ~1] >> 32))
186*da0bac33SDavid Daney 
187*da0bac33SDavid Daney #define SITOREG(si, x)	(ctx->fpr[x & ~(cop1_64bit(xcp) == 0)] = \
188*da0bac33SDavid Daney 			cop1_64bit(xcp) || !(x & 1) ? \
1891da177e4SLinus Torvalds 			ctx->fpr[x & ~1] >> 32 << 32 | (u32)(si) : \
1901da177e4SLinus Torvalds 			ctx->fpr[x & ~1] << 32 >> 32 | (u64)(si) << 32)
1911da177e4SLinus Torvalds 
192*da0bac33SDavid Daney #define DIFROMREG(di, x) ((di) = ctx->fpr[x & ~(cop1_64bit(xcp) == 0)])
193*da0bac33SDavid Daney #define DITOREG(di, x)	(ctx->fpr[x & ~(cop1_64bit(xcp) == 0)] = (di))
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
1961da177e4SLinus Torvalds #define SPTOREG(sp, x)	SITOREG((sp).bits, x)
1971da177e4SLinus Torvalds #define DPFROMREG(dp, x)	DIFROMREG((dp).bits, x)
1981da177e4SLinus Torvalds #define DPTOREG(dp, x)	DITOREG((dp).bits, x)
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds /*
2011da177e4SLinus Torvalds  * Emulate the single floating point instruction pointed at by EPC.
2021da177e4SLinus Torvalds  * Two instructions if the instruction is in a branch delay slot.
2031da177e4SLinus Torvalds  */
2041da177e4SLinus Torvalds 
205eae89076SAtsushi Nemoto static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx)
2061da177e4SLinus Torvalds {
2071da177e4SLinus Torvalds 	mips_instruction ir;
208e70dfc10SAtsushi Nemoto 	unsigned long emulpc, contpc;
2091da177e4SLinus Torvalds 	unsigned int cond;
2101da177e4SLinus Torvalds 
2113fccc015SRalf Baechle 	if (get_user(ir, (mips_instruction __user *) xcp->cp0_epc)) {
2124a99d1e2SRalf Baechle 		fpuemustats.errors++;
2131da177e4SLinus Torvalds 		return SIGBUS;
2141da177e4SLinus Torvalds 	}
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	/* XXX NEC Vr54xx bug workaround */
2171da177e4SLinus Torvalds 	if ((xcp->cp0_cause & CAUSEF_BD) && !isBranchInstr(&ir))
2181da177e4SLinus Torvalds 		xcp->cp0_cause &= ~CAUSEF_BD;
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	if (xcp->cp0_cause & CAUSEF_BD) {
2211da177e4SLinus Torvalds 		/*
2221da177e4SLinus Torvalds 		 * The instruction to be emulated is in a branch delay slot
2231da177e4SLinus Torvalds 		 * which means that we have to  emulate the branch instruction
2241da177e4SLinus Torvalds 		 * BEFORE we do the cop1 instruction.
2251da177e4SLinus Torvalds 		 *
2261da177e4SLinus Torvalds 		 * This branch could be a COP1 branch, but in that case we
2271da177e4SLinus Torvalds 		 * would have had a trap for that instruction, and would not
2281da177e4SLinus Torvalds 		 * come through this route.
2291da177e4SLinus Torvalds 		 *
2301da177e4SLinus Torvalds 		 * Linux MIPS branch emulator operates on context, updating the
2311da177e4SLinus Torvalds 		 * cp0_epc.
2321da177e4SLinus Torvalds 		 */
233e70dfc10SAtsushi Nemoto 		emulpc = xcp->cp0_epc + 4;	/* Snapshot emulation target */
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 		if (__compute_return_epc(xcp)) {
2361da177e4SLinus Torvalds #ifdef CP1DBG
2371da177e4SLinus Torvalds 			printk("failed to emulate branch at %p\n",
238333d1f67SRalf Baechle 				(void *) (xcp->cp0_epc));
2391da177e4SLinus Torvalds #endif
2401da177e4SLinus Torvalds 			return SIGILL;
2411da177e4SLinus Torvalds 		}
2423fccc015SRalf Baechle 		if (get_user(ir, (mips_instruction __user *) emulpc)) {
2434a99d1e2SRalf Baechle 			fpuemustats.errors++;
2441da177e4SLinus Torvalds 			return SIGBUS;
2451da177e4SLinus Torvalds 		}
2461da177e4SLinus Torvalds 		/* __compute_return_epc() will have updated cp0_epc */
247e70dfc10SAtsushi Nemoto 		contpc = xcp->cp0_epc;
2481da177e4SLinus Torvalds 		/* In order not to confuse ptrace() et al, tweak context */
249e70dfc10SAtsushi Nemoto 		xcp->cp0_epc = emulpc - 4;
250333d1f67SRalf Baechle 	} else {
251e70dfc10SAtsushi Nemoto 		emulpc = xcp->cp0_epc;
252e70dfc10SAtsushi Nemoto 		contpc = xcp->cp0_epc + 4;
2531da177e4SLinus Torvalds 	}
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds       emul:
2564a99d1e2SRalf Baechle 	fpuemustats.emulated++;
2571da177e4SLinus Torvalds 	switch (MIPSInst_OPCODE(ir)) {
2581da177e4SLinus Torvalds 	case ldc1_op:{
2593fccc015SRalf Baechle 		u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
2601da177e4SLinus Torvalds 			MIPSInst_SIMM(ir));
2611da177e4SLinus Torvalds 		u64 val;
2621da177e4SLinus Torvalds 
2634a99d1e2SRalf Baechle 		fpuemustats.loads++;
2641da177e4SLinus Torvalds 		if (get_user(val, va)) {
2654a99d1e2SRalf Baechle 			fpuemustats.errors++;
2661da177e4SLinus Torvalds 			return SIGBUS;
2671da177e4SLinus Torvalds 		}
2681da177e4SLinus Torvalds 		DITOREG(val, MIPSInst_RT(ir));
2691da177e4SLinus Torvalds 		break;
2701da177e4SLinus Torvalds 	}
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	case sdc1_op:{
2733fccc015SRalf Baechle 		u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
2741da177e4SLinus Torvalds 			MIPSInst_SIMM(ir));
2751da177e4SLinus Torvalds 		u64 val;
2761da177e4SLinus Torvalds 
2774a99d1e2SRalf Baechle 		fpuemustats.stores++;
2781da177e4SLinus Torvalds 		DIFROMREG(val, MIPSInst_RT(ir));
2791da177e4SLinus Torvalds 		if (put_user(val, va)) {
2804a99d1e2SRalf Baechle 			fpuemustats.errors++;
2811da177e4SLinus Torvalds 			return SIGBUS;
2821da177e4SLinus Torvalds 		}
2831da177e4SLinus Torvalds 		break;
2841da177e4SLinus Torvalds 	}
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	case lwc1_op:{
2873fccc015SRalf Baechle 		u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
2881da177e4SLinus Torvalds 			MIPSInst_SIMM(ir));
2891da177e4SLinus Torvalds 		u32 val;
2901da177e4SLinus Torvalds 
2914a99d1e2SRalf Baechle 		fpuemustats.loads++;
2921da177e4SLinus Torvalds 		if (get_user(val, va)) {
2934a99d1e2SRalf Baechle 			fpuemustats.errors++;
2941da177e4SLinus Torvalds 			return SIGBUS;
2951da177e4SLinus Torvalds 		}
2961da177e4SLinus Torvalds 		SITOREG(val, MIPSInst_RT(ir));
2971da177e4SLinus Torvalds 		break;
2981da177e4SLinus Torvalds 	}
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	case swc1_op:{
3013fccc015SRalf Baechle 		u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
3021da177e4SLinus Torvalds 			MIPSInst_SIMM(ir));
3031da177e4SLinus Torvalds 		u32 val;
3041da177e4SLinus Torvalds 
3054a99d1e2SRalf Baechle 		fpuemustats.stores++;
3061da177e4SLinus Torvalds 		SIFROMREG(val, MIPSInst_RT(ir));
3071da177e4SLinus Torvalds 		if (put_user(val, va)) {
3084a99d1e2SRalf Baechle 			fpuemustats.errors++;
3091da177e4SLinus Torvalds 			return SIGBUS;
3101da177e4SLinus Torvalds 		}
3111da177e4SLinus Torvalds 		break;
3121da177e4SLinus Torvalds 	}
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	case cop1_op:
3151da177e4SLinus Torvalds 		switch (MIPSInst_RS(ir)) {
3161da177e4SLinus Torvalds 
3174b724efdSRalf Baechle #if defined(__mips64)
3181da177e4SLinus Torvalds 		case dmfc_op:
3191da177e4SLinus Torvalds 			/* copregister fs -> gpr[rt] */
3201da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
3211da177e4SLinus Torvalds 				DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
3221da177e4SLinus Torvalds 					MIPSInst_RD(ir));
3231da177e4SLinus Torvalds 			}
3241da177e4SLinus Torvalds 			break;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 		case dmtc_op:
3271da177e4SLinus Torvalds 			/* copregister fs <- rt */
3281da177e4SLinus Torvalds 			DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
3291da177e4SLinus Torvalds 			break;
3301da177e4SLinus Torvalds #endif
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 		case mfc_op:
3331da177e4SLinus Torvalds 			/* copregister rd -> gpr[rt] */
3341da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
3351da177e4SLinus Torvalds 				SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
3361da177e4SLinus Torvalds 					MIPSInst_RD(ir));
3371da177e4SLinus Torvalds 			}
3381da177e4SLinus Torvalds 			break;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 		case mtc_op:
3411da177e4SLinus Torvalds 			/* copregister rd <- rt */
3421da177e4SLinus Torvalds 			SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
3431da177e4SLinus Torvalds 			break;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 		case cfc_op:{
3461da177e4SLinus Torvalds 			/* cop control register rd -> gpr[rt] */
3471da177e4SLinus Torvalds 			u32 value;
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 			if (MIPSInst_RD(ir) == FPCREG_CSR) {
3501da177e4SLinus Torvalds 				value = ctx->fcr31;
351cd21dfcfSRalf Baechle 				value = (value & ~0x3) | mips_rm[value & 0x3];
3521da177e4SLinus Torvalds #ifdef CSRTRACE
3531da177e4SLinus Torvalds 				printk("%p gpr[%d]<-csr=%08x\n",
354333d1f67SRalf Baechle 					(void *) (xcp->cp0_epc),
3551da177e4SLinus Torvalds 					MIPSInst_RT(ir), value);
3561da177e4SLinus Torvalds #endif
3571da177e4SLinus Torvalds 			}
3581da177e4SLinus Torvalds 			else if (MIPSInst_RD(ir) == FPCREG_RID)
3591da177e4SLinus Torvalds 				value = 0;
3601da177e4SLinus Torvalds 			else
3611da177e4SLinus Torvalds 				value = 0;
3621da177e4SLinus Torvalds 			if (MIPSInst_RT(ir))
3631da177e4SLinus Torvalds 				xcp->regs[MIPSInst_RT(ir)] = value;
3641da177e4SLinus Torvalds 			break;
3651da177e4SLinus Torvalds 		}
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 		case ctc_op:{
3681da177e4SLinus Torvalds 			/* copregister rd <- rt */
3691da177e4SLinus Torvalds 			u32 value;
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) == 0)
3721da177e4SLinus Torvalds 				value = 0;
3731da177e4SLinus Torvalds 			else
3741da177e4SLinus Torvalds 				value = xcp->regs[MIPSInst_RT(ir)];
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 			/* we only have one writable control reg
3771da177e4SLinus Torvalds 			 */
3781da177e4SLinus Torvalds 			if (MIPSInst_RD(ir) == FPCREG_CSR) {
3791da177e4SLinus Torvalds #ifdef CSRTRACE
3801da177e4SLinus Torvalds 				printk("%p gpr[%d]->csr=%08x\n",
381333d1f67SRalf Baechle 					(void *) (xcp->cp0_epc),
3821da177e4SLinus Torvalds 					MIPSInst_RT(ir), value);
3831da177e4SLinus Torvalds #endif
384cd21dfcfSRalf Baechle 				value &= (FPU_CSR_FLUSH | FPU_CSR_ALL_E | FPU_CSR_ALL_S | 0x03);
385cd21dfcfSRalf Baechle 				ctx->fcr31 &= ~(FPU_CSR_FLUSH | FPU_CSR_ALL_E | FPU_CSR_ALL_S | 0x03);
386cd21dfcfSRalf Baechle 				/* convert to ieee library modes */
387cd21dfcfSRalf Baechle 				ctx->fcr31 |= (value & ~0x3) | ieee_rm[value & 0x3];
3881da177e4SLinus Torvalds 			}
3891da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
3901da177e4SLinus Torvalds 				return SIGFPE;
3911da177e4SLinus Torvalds 			}
3921da177e4SLinus Torvalds 			break;
3931da177e4SLinus Torvalds 		}
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 		case bc_op:{
3961da177e4SLinus Torvalds 			int likely = 0;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 			if (xcp->cp0_cause & CAUSEF_BD)
3991da177e4SLinus Torvalds 				return SIGILL;
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds #if __mips >= 4
4021da177e4SLinus Torvalds 			cond = ctx->fcr31 & fpucondbit[MIPSInst_RT(ir) >> 2];
4031da177e4SLinus Torvalds #else
4041da177e4SLinus Torvalds 			cond = ctx->fcr31 & FPU_CSR_COND;
4051da177e4SLinus Torvalds #endif
4061da177e4SLinus Torvalds 			switch (MIPSInst_RT(ir) & 3) {
4071da177e4SLinus Torvalds 			case bcfl_op:
4081da177e4SLinus Torvalds 				likely = 1;
4091da177e4SLinus Torvalds 			case bcf_op:
4101da177e4SLinus Torvalds 				cond = !cond;
4111da177e4SLinus Torvalds 				break;
4121da177e4SLinus Torvalds 			case bctl_op:
4131da177e4SLinus Torvalds 				likely = 1;
4141da177e4SLinus Torvalds 			case bct_op:
4151da177e4SLinus Torvalds 				break;
4161da177e4SLinus Torvalds 			default:
4171da177e4SLinus Torvalds 				/* thats an illegal instruction */
4181da177e4SLinus Torvalds 				return SIGILL;
4191da177e4SLinus Torvalds 			}
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 			xcp->cp0_cause |= CAUSEF_BD;
4221da177e4SLinus Torvalds 			if (cond) {
4231da177e4SLinus Torvalds 				/* branch taken: emulate dslot
4241da177e4SLinus Torvalds 				 * instruction
4251da177e4SLinus Torvalds 				 */
4261da177e4SLinus Torvalds 				xcp->cp0_epc += 4;
427e70dfc10SAtsushi Nemoto 				contpc = (xcp->cp0_epc +
4281da177e4SLinus Torvalds 					(MIPSInst_SIMM(ir) << 2));
4291da177e4SLinus Torvalds 
4303fccc015SRalf Baechle 				if (get_user(ir,
4313fccc015SRalf Baechle 				    (mips_instruction __user *) xcp->cp0_epc)) {
4324a99d1e2SRalf Baechle 					fpuemustats.errors++;
4331da177e4SLinus Torvalds 					return SIGBUS;
4341da177e4SLinus Torvalds 				}
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 				switch (MIPSInst_OPCODE(ir)) {
4371da177e4SLinus Torvalds 				case lwc1_op:
4381da177e4SLinus Torvalds 				case swc1_op:
4394b724efdSRalf Baechle #if (__mips >= 2 || defined(__mips64))
4401da177e4SLinus Torvalds 				case ldc1_op:
4411da177e4SLinus Torvalds 				case sdc1_op:
4421da177e4SLinus Torvalds #endif
4431da177e4SLinus Torvalds 				case cop1_op:
4441da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32
4451da177e4SLinus Torvalds 				case cop1x_op:
4461da177e4SLinus Torvalds #endif
4471da177e4SLinus Torvalds 					/* its one of ours */
4481da177e4SLinus Torvalds 					goto emul;
4491da177e4SLinus Torvalds #if __mips >= 4
4501da177e4SLinus Torvalds 				case spec_op:
4511da177e4SLinus Torvalds 					if (MIPSInst_FUNC(ir) == movc_op)
4521da177e4SLinus Torvalds 						goto emul;
4531da177e4SLinus Torvalds 					break;
4541da177e4SLinus Torvalds #endif
4551da177e4SLinus Torvalds 				}
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 				/*
4581da177e4SLinus Torvalds 				 * Single step the non-cp1
4591da177e4SLinus Torvalds 				 * instruction in the dslot
4601da177e4SLinus Torvalds 				 */
461e70dfc10SAtsushi Nemoto 				return mips_dsemul(xcp, ir, contpc);
4621da177e4SLinus Torvalds 			}
4631da177e4SLinus Torvalds 			else {
4641da177e4SLinus Torvalds 				/* branch not taken */
4651da177e4SLinus Torvalds 				if (likely) {
4661da177e4SLinus Torvalds 					/*
4671da177e4SLinus Torvalds 					 * branch likely nullifies
4681da177e4SLinus Torvalds 					 * dslot if not taken
4691da177e4SLinus Torvalds 					 */
4701da177e4SLinus Torvalds 					xcp->cp0_epc += 4;
4711da177e4SLinus Torvalds 					contpc += 4;
4721da177e4SLinus Torvalds 					/*
4731da177e4SLinus Torvalds 					 * else continue & execute
4741da177e4SLinus Torvalds 					 * dslot as normal insn
4751da177e4SLinus Torvalds 					 */
4761da177e4SLinus Torvalds 				}
4771da177e4SLinus Torvalds 			}
4781da177e4SLinus Torvalds 			break;
4791da177e4SLinus Torvalds 		}
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 		default:
4821da177e4SLinus Torvalds 			if (!(MIPSInst_RS(ir) & 0x10))
4831da177e4SLinus Torvalds 				return SIGILL;
4841da177e4SLinus Torvalds 			{
4851da177e4SLinus Torvalds 				int sig;
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 				/* a real fpu computation instruction */
4881da177e4SLinus Torvalds 				if ((sig = fpu_emu(xcp, ctx, ir)))
4891da177e4SLinus Torvalds 					return sig;
4901da177e4SLinus Torvalds 			}
4911da177e4SLinus Torvalds 		}
4921da177e4SLinus Torvalds 		break;
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32
4951da177e4SLinus Torvalds 	case cop1x_op:{
4961da177e4SLinus Torvalds 		int sig;
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 		if ((sig = fpux_emu(xcp, ctx, ir)))
4991da177e4SLinus Torvalds 			return sig;
5001da177e4SLinus Torvalds 		break;
5011da177e4SLinus Torvalds 	}
5021da177e4SLinus Torvalds #endif
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds #if __mips >= 4
5051da177e4SLinus Torvalds 	case spec_op:
5061da177e4SLinus Torvalds 		if (MIPSInst_FUNC(ir) != movc_op)
5071da177e4SLinus Torvalds 			return SIGILL;
5081da177e4SLinus Torvalds 		cond = fpucondbit[MIPSInst_RT(ir) >> 2];
5091da177e4SLinus Torvalds 		if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
5101da177e4SLinus Torvalds 			xcp->regs[MIPSInst_RD(ir)] =
5111da177e4SLinus Torvalds 				xcp->regs[MIPSInst_RS(ir)];
5121da177e4SLinus Torvalds 		break;
5131da177e4SLinus Torvalds #endif
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	default:
5161da177e4SLinus Torvalds 		return SIGILL;
5171da177e4SLinus Torvalds 	}
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	/* we did it !! */
520e70dfc10SAtsushi Nemoto 	xcp->cp0_epc = contpc;
5211da177e4SLinus Torvalds 	xcp->cp0_cause &= ~CAUSEF_BD;
522333d1f67SRalf Baechle 
5231da177e4SLinus Torvalds 	return 0;
5241da177e4SLinus Torvalds }
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds /*
5271da177e4SLinus Torvalds  * Conversion table from MIPS compare ops 48-63
5281da177e4SLinus Torvalds  * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
5291da177e4SLinus Torvalds  */
5301da177e4SLinus Torvalds static const unsigned char cmptab[8] = {
5311da177e4SLinus Torvalds 	0,			/* cmp_0 (sig) cmp_sf */
5321da177e4SLinus Torvalds 	IEEE754_CUN,		/* cmp_un (sig) cmp_ngle */
5331da177e4SLinus Torvalds 	IEEE754_CEQ,		/* cmp_eq (sig) cmp_seq */
5341da177e4SLinus Torvalds 	IEEE754_CEQ | IEEE754_CUN,	/* cmp_ueq (sig) cmp_ngl  */
5351da177e4SLinus Torvalds 	IEEE754_CLT,		/* cmp_olt (sig) cmp_lt */
5361da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CUN,	/* cmp_ult (sig) cmp_nge */
5371da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ,	/* cmp_ole (sig) cmp_le */
5381da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN,	/* cmp_ule (sig) cmp_ngt */
5391da177e4SLinus Torvalds };
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds /*
5451da177e4SLinus Torvalds  * Additional MIPS4 instructions
5461da177e4SLinus Torvalds  */
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds #define DEF3OP(name, p, f1, f2, f3) \
5491da177e4SLinus Torvalds static ieee754##p fpemu_##p##_##name(ieee754##p r, ieee754##p s, \
5501da177e4SLinus Torvalds     ieee754##p t) \
5511da177e4SLinus Torvalds { \
552cd21dfcfSRalf Baechle 	struct _ieee754_csr ieee754_csr_save; \
5531da177e4SLinus Torvalds 	s = f1(s, t); \
5541da177e4SLinus Torvalds 	ieee754_csr_save = ieee754_csr; \
5551da177e4SLinus Torvalds 	s = f2(s, r); \
5561da177e4SLinus Torvalds 	ieee754_csr_save.cx |= ieee754_csr.cx; \
5571da177e4SLinus Torvalds 	ieee754_csr_save.sx |= ieee754_csr.sx; \
5581da177e4SLinus Torvalds 	s = f3(s); \
5591da177e4SLinus Torvalds 	ieee754_csr.cx |= ieee754_csr_save.cx; \
5601da177e4SLinus Torvalds 	ieee754_csr.sx |= ieee754_csr_save.sx; \
5611da177e4SLinus Torvalds 	return s; \
5621da177e4SLinus Torvalds }
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds static ieee754dp fpemu_dp_recip(ieee754dp d)
5651da177e4SLinus Torvalds {
5661da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), d);
5671da177e4SLinus Torvalds }
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds static ieee754dp fpemu_dp_rsqrt(ieee754dp d)
5701da177e4SLinus Torvalds {
5711da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds static ieee754sp fpemu_sp_recip(ieee754sp s)
5751da177e4SLinus Torvalds {
5761da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), s);
5771da177e4SLinus Torvalds }
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds static ieee754sp fpemu_sp_rsqrt(ieee754sp s)
5801da177e4SLinus Torvalds {
5811da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
5821da177e4SLinus Torvalds }
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
5851da177e4SLinus Torvalds DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
5861da177e4SLinus Torvalds DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
5871da177e4SLinus Torvalds DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
5881da177e4SLinus Torvalds DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
5891da177e4SLinus Torvalds DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
5901da177e4SLinus Torvalds DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
5911da177e4SLinus Torvalds DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
5921da177e4SLinus Torvalds 
593eae89076SAtsushi Nemoto static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
5941da177e4SLinus Torvalds 	mips_instruction ir)
5951da177e4SLinus Torvalds {
5961da177e4SLinus Torvalds 	unsigned rcsr = 0;	/* resulting csr */
5971da177e4SLinus Torvalds 
5984a99d1e2SRalf Baechle 	fpuemustats.cp1xops++;
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 	switch (MIPSInst_FMA_FFMT(ir)) {
6011da177e4SLinus Torvalds 	case s_fmt:{		/* 0 */
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 		ieee754sp(*handler) (ieee754sp, ieee754sp, ieee754sp);
6041da177e4SLinus Torvalds 		ieee754sp fd, fr, fs, ft;
6053fccc015SRalf Baechle 		u32 __user *va;
6061da177e4SLinus Torvalds 		u32 val;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
6091da177e4SLinus Torvalds 		case lwxc1_op:
6103fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
6111da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
6121da177e4SLinus Torvalds 
6134a99d1e2SRalf Baechle 			fpuemustats.loads++;
6141da177e4SLinus Torvalds 			if (get_user(val, va)) {
6154a99d1e2SRalf Baechle 				fpuemustats.errors++;
6161da177e4SLinus Torvalds 				return SIGBUS;
6171da177e4SLinus Torvalds 			}
6181da177e4SLinus Torvalds 			SITOREG(val, MIPSInst_FD(ir));
6191da177e4SLinus Torvalds 			break;
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 		case swxc1_op:
6223fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
6231da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
6241da177e4SLinus Torvalds 
6254a99d1e2SRalf Baechle 			fpuemustats.stores++;
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 			SIFROMREG(val, MIPSInst_FS(ir));
6281da177e4SLinus Torvalds 			if (put_user(val, va)) {
6294a99d1e2SRalf Baechle 				fpuemustats.errors++;
6301da177e4SLinus Torvalds 				return SIGBUS;
6311da177e4SLinus Torvalds 			}
6321da177e4SLinus Torvalds 			break;
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 		case madd_s_op:
6351da177e4SLinus Torvalds 			handler = fpemu_sp_madd;
6361da177e4SLinus Torvalds 			goto scoptop;
6371da177e4SLinus Torvalds 		case msub_s_op:
6381da177e4SLinus Torvalds 			handler = fpemu_sp_msub;
6391da177e4SLinus Torvalds 			goto scoptop;
6401da177e4SLinus Torvalds 		case nmadd_s_op:
6411da177e4SLinus Torvalds 			handler = fpemu_sp_nmadd;
6421da177e4SLinus Torvalds 			goto scoptop;
6431da177e4SLinus Torvalds 		case nmsub_s_op:
6441da177e4SLinus Torvalds 			handler = fpemu_sp_nmsub;
6451da177e4SLinus Torvalds 			goto scoptop;
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 		      scoptop:
6481da177e4SLinus Torvalds 			SPFROMREG(fr, MIPSInst_FR(ir));
6491da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
6501da177e4SLinus Torvalds 			SPFROMREG(ft, MIPSInst_FT(ir));
6511da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
6521da177e4SLinus Torvalds 			SPTOREG(fd, MIPSInst_FD(ir));
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 		      copcsr:
6551da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_INEXACT))
6561da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
6571da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_UNDERFLOW))
6581da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
6591da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_OVERFLOW))
6601da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
6611da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
6621da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 			ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
6651da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
6661da177e4SLinus Torvalds 				/*printk ("SIGFPE: fpu csr = %08x\n",
6671da177e4SLinus Torvalds 				   ctx->fcr31); */
6681da177e4SLinus Torvalds 				return SIGFPE;
6691da177e4SLinus Torvalds 			}
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 			break;
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 		default:
6741da177e4SLinus Torvalds 			return SIGILL;
6751da177e4SLinus Torvalds 		}
6761da177e4SLinus Torvalds 		break;
6771da177e4SLinus Torvalds 	}
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	case d_fmt:{		/* 1 */
6801da177e4SLinus Torvalds 		ieee754dp(*handler) (ieee754dp, ieee754dp, ieee754dp);
6811da177e4SLinus Torvalds 		ieee754dp fd, fr, fs, ft;
6823fccc015SRalf Baechle 		u64 __user *va;
6831da177e4SLinus Torvalds 		u64 val;
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
6861da177e4SLinus Torvalds 		case ldxc1_op:
6873fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
6881da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
6891da177e4SLinus Torvalds 
6904a99d1e2SRalf Baechle 			fpuemustats.loads++;
6911da177e4SLinus Torvalds 			if (get_user(val, va)) {
6924a99d1e2SRalf Baechle 				fpuemustats.errors++;
6931da177e4SLinus Torvalds 				return SIGBUS;
6941da177e4SLinus Torvalds 			}
6951da177e4SLinus Torvalds 			DITOREG(val, MIPSInst_FD(ir));
6961da177e4SLinus Torvalds 			break;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 		case sdxc1_op:
6993fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
7001da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
7011da177e4SLinus Torvalds 
7024a99d1e2SRalf Baechle 			fpuemustats.stores++;
7031da177e4SLinus Torvalds 			DIFROMREG(val, MIPSInst_FS(ir));
7041da177e4SLinus Torvalds 			if (put_user(val, va)) {
7054a99d1e2SRalf Baechle 				fpuemustats.errors++;
7061da177e4SLinus Torvalds 				return SIGBUS;
7071da177e4SLinus Torvalds 			}
7081da177e4SLinus Torvalds 			break;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 		case madd_d_op:
7111da177e4SLinus Torvalds 			handler = fpemu_dp_madd;
7121da177e4SLinus Torvalds 			goto dcoptop;
7131da177e4SLinus Torvalds 		case msub_d_op:
7141da177e4SLinus Torvalds 			handler = fpemu_dp_msub;
7151da177e4SLinus Torvalds 			goto dcoptop;
7161da177e4SLinus Torvalds 		case nmadd_d_op:
7171da177e4SLinus Torvalds 			handler = fpemu_dp_nmadd;
7181da177e4SLinus Torvalds 			goto dcoptop;
7191da177e4SLinus Torvalds 		case nmsub_d_op:
7201da177e4SLinus Torvalds 			handler = fpemu_dp_nmsub;
7211da177e4SLinus Torvalds 			goto dcoptop;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 		      dcoptop:
7241da177e4SLinus Torvalds 			DPFROMREG(fr, MIPSInst_FR(ir));
7251da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
7261da177e4SLinus Torvalds 			DPFROMREG(ft, MIPSInst_FT(ir));
7271da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
7281da177e4SLinus Torvalds 			DPTOREG(fd, MIPSInst_FD(ir));
7291da177e4SLinus Torvalds 			goto copcsr;
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 		default:
7321da177e4SLinus Torvalds 			return SIGILL;
7331da177e4SLinus Torvalds 		}
7341da177e4SLinus Torvalds 		break;
7351da177e4SLinus Torvalds 	}
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 	case 0x7:		/* 7 */
7381da177e4SLinus Torvalds 		if (MIPSInst_FUNC(ir) != pfetch_op) {
7391da177e4SLinus Torvalds 			return SIGILL;
7401da177e4SLinus Torvalds 		}
7411da177e4SLinus Torvalds 		/* ignore prefx operation */
7421da177e4SLinus Torvalds 		break;
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	default:
7451da177e4SLinus Torvalds 		return SIGILL;
7461da177e4SLinus Torvalds 	}
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	return 0;
7491da177e4SLinus Torvalds }
7501da177e4SLinus Torvalds #endif
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds /*
7551da177e4SLinus Torvalds  * Emulate a single COP1 arithmetic instruction.
7561da177e4SLinus Torvalds  */
757eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
7581da177e4SLinus Torvalds 	mips_instruction ir)
7591da177e4SLinus Torvalds {
7601da177e4SLinus Torvalds 	int rfmt;		/* resulting format */
7611da177e4SLinus Torvalds 	unsigned rcsr = 0;	/* resulting csr */
7621da177e4SLinus Torvalds 	unsigned cond;
7631da177e4SLinus Torvalds 	union {
7641da177e4SLinus Torvalds 		ieee754dp d;
7651da177e4SLinus Torvalds 		ieee754sp s;
7661da177e4SLinus Torvalds 		int w;
767766160c2SYoichi Yuasa #ifdef __mips64
7681da177e4SLinus Torvalds 		s64 l;
7691da177e4SLinus Torvalds #endif
7701da177e4SLinus Torvalds 	} rv;			/* resulting value */
7711da177e4SLinus Torvalds 
7724a99d1e2SRalf Baechle 	fpuemustats.cp1ops++;
7731da177e4SLinus Torvalds 	switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
7741da177e4SLinus Torvalds 	case s_fmt:{		/* 0 */
7751da177e4SLinus Torvalds 		union {
7761da177e4SLinus Torvalds 			ieee754sp(*b) (ieee754sp, ieee754sp);
7771da177e4SLinus Torvalds 			ieee754sp(*u) (ieee754sp);
7781da177e4SLinus Torvalds 		} handler;
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
7811da177e4SLinus Torvalds 			/* binary ops */
7821da177e4SLinus Torvalds 		case fadd_op:
7831da177e4SLinus Torvalds 			handler.b = ieee754sp_add;
7841da177e4SLinus Torvalds 			goto scopbop;
7851da177e4SLinus Torvalds 		case fsub_op:
7861da177e4SLinus Torvalds 			handler.b = ieee754sp_sub;
7871da177e4SLinus Torvalds 			goto scopbop;
7881da177e4SLinus Torvalds 		case fmul_op:
7891da177e4SLinus Torvalds 			handler.b = ieee754sp_mul;
7901da177e4SLinus Torvalds 			goto scopbop;
7911da177e4SLinus Torvalds 		case fdiv_op:
7921da177e4SLinus Torvalds 			handler.b = ieee754sp_div;
7931da177e4SLinus Torvalds 			goto scopbop;
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 			/* unary  ops */
796587cb98fSRalf Baechle #if __mips >= 2 || defined(__mips64)
7971da177e4SLinus Torvalds 		case fsqrt_op:
7981da177e4SLinus Torvalds 			handler.u = ieee754sp_sqrt;
7991da177e4SLinus Torvalds 			goto scopuop;
8001da177e4SLinus Torvalds #endif
8011da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32
8021da177e4SLinus Torvalds 		case frsqrt_op:
8031da177e4SLinus Torvalds 			handler.u = fpemu_sp_rsqrt;
8041da177e4SLinus Torvalds 			goto scopuop;
8051da177e4SLinus Torvalds 		case frecip_op:
8061da177e4SLinus Torvalds 			handler.u = fpemu_sp_recip;
8071da177e4SLinus Torvalds 			goto scopuop;
8081da177e4SLinus Torvalds #endif
8091da177e4SLinus Torvalds #if __mips >= 4
8101da177e4SLinus Torvalds 		case fmovc_op:
8111da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
8121da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
8131da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
8141da177e4SLinus Torvalds 				return 0;
8151da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
8161da177e4SLinus Torvalds 			break;
8171da177e4SLinus Torvalds 		case fmovz_op:
8181da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
8191da177e4SLinus Torvalds 				return 0;
8201da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
8211da177e4SLinus Torvalds 			break;
8221da177e4SLinus Torvalds 		case fmovn_op:
8231da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
8241da177e4SLinus Torvalds 				return 0;
8251da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
8261da177e4SLinus Torvalds 			break;
8271da177e4SLinus Torvalds #endif
8281da177e4SLinus Torvalds 		case fabs_op:
8291da177e4SLinus Torvalds 			handler.u = ieee754sp_abs;
8301da177e4SLinus Torvalds 			goto scopuop;
8311da177e4SLinus Torvalds 		case fneg_op:
8321da177e4SLinus Torvalds 			handler.u = ieee754sp_neg;
8331da177e4SLinus Torvalds 			goto scopuop;
8341da177e4SLinus Torvalds 		case fmov_op:
8351da177e4SLinus Torvalds 			/* an easy one */
8361da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
8371da177e4SLinus Torvalds 			goto copcsr;
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 			/* binary op on handler */
8401da177e4SLinus Torvalds 		      scopbop:
8411da177e4SLinus Torvalds 			{
8421da177e4SLinus Torvalds 				ieee754sp fs, ft;
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 				SPFROMREG(fs, MIPSInst_FS(ir));
8451da177e4SLinus Torvalds 				SPFROMREG(ft, MIPSInst_FT(ir));
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 				rv.s = (*handler.b) (fs, ft);
8481da177e4SLinus Torvalds 				goto copcsr;
8491da177e4SLinus Torvalds 			}
8501da177e4SLinus Torvalds 		      scopuop:
8511da177e4SLinus Torvalds 			{
8521da177e4SLinus Torvalds 				ieee754sp fs;
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 				SPFROMREG(fs, MIPSInst_FS(ir));
8551da177e4SLinus Torvalds 				rv.s = (*handler.u) (fs);
8561da177e4SLinus Torvalds 				goto copcsr;
8571da177e4SLinus Torvalds 			}
8581da177e4SLinus Torvalds 		      copcsr:
8591da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_INEXACT))
8601da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
8611da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_UNDERFLOW))
8621da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
8631da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_OVERFLOW))
8641da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
8651da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_ZERO_DIVIDE))
8661da177e4SLinus Torvalds 				rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
8671da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
8681da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
8691da177e4SLinus Torvalds 			break;
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 			/* unary conv ops */
8721da177e4SLinus Torvalds 		case fcvts_op:
8731da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
8741da177e4SLinus Torvalds 		case fcvtd_op:{
8751da177e4SLinus Torvalds 			ieee754sp fs;
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
8781da177e4SLinus Torvalds 			rv.d = ieee754dp_fsp(fs);
8791da177e4SLinus Torvalds 			rfmt = d_fmt;
8801da177e4SLinus Torvalds 			goto copcsr;
8811da177e4SLinus Torvalds 		}
8821da177e4SLinus Torvalds 		case fcvtw_op:{
8831da177e4SLinus Torvalds 			ieee754sp fs;
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
8861da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
8871da177e4SLinus Torvalds 			rfmt = w_fmt;
8881da177e4SLinus Torvalds 			goto copcsr;
8891da177e4SLinus Torvalds 		}
8901da177e4SLinus Torvalds 
891587cb98fSRalf Baechle #if __mips >= 2 || defined(__mips64)
8921da177e4SLinus Torvalds 		case fround_op:
8931da177e4SLinus Torvalds 		case ftrunc_op:
8941da177e4SLinus Torvalds 		case fceil_op:
8951da177e4SLinus Torvalds 		case ffloor_op:{
8961da177e4SLinus Torvalds 			unsigned int oldrm = ieee754_csr.rm;
8971da177e4SLinus Torvalds 			ieee754sp fs;
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
9001da177e4SLinus Torvalds 			ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
9011da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
9021da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
9031da177e4SLinus Torvalds 			rfmt = w_fmt;
9041da177e4SLinus Torvalds 			goto copcsr;
9051da177e4SLinus Torvalds 		}
9061da177e4SLinus Torvalds #endif /* __mips >= 2 */
9071da177e4SLinus Torvalds 
9084b724efdSRalf Baechle #if defined(__mips64)
9091da177e4SLinus Torvalds 		case fcvtl_op:{
9101da177e4SLinus Torvalds 			ieee754sp fs;
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
9131da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
9141da177e4SLinus Torvalds 			rfmt = l_fmt;
9151da177e4SLinus Torvalds 			goto copcsr;
9161da177e4SLinus Torvalds 		}
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 		case froundl_op:
9191da177e4SLinus Torvalds 		case ftruncl_op:
9201da177e4SLinus Torvalds 		case fceill_op:
9211da177e4SLinus Torvalds 		case ffloorl_op:{
9221da177e4SLinus Torvalds 			unsigned int oldrm = ieee754_csr.rm;
9231da177e4SLinus Torvalds 			ieee754sp fs;
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
9261da177e4SLinus Torvalds 			ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
9271da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
9281da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
9291da177e4SLinus Torvalds 			rfmt = l_fmt;
9301da177e4SLinus Torvalds 			goto copcsr;
9311da177e4SLinus Torvalds 		}
9324b724efdSRalf Baechle #endif /* defined(__mips64) */
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds 		default:
9351da177e4SLinus Torvalds 			if (MIPSInst_FUNC(ir) >= fcmp_op) {
9361da177e4SLinus Torvalds 				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
9371da177e4SLinus Torvalds 				ieee754sp fs, ft;
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 				SPFROMREG(fs, MIPSInst_FS(ir));
9401da177e4SLinus Torvalds 				SPFROMREG(ft, MIPSInst_FT(ir));
9411da177e4SLinus Torvalds 				rv.w = ieee754sp_cmp(fs, ft,
9421da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
9431da177e4SLinus Torvalds 				rfmt = -1;
9441da177e4SLinus Torvalds 				if ((cmpop & 0x8) && ieee754_cxtest
9451da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
9461da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
9471da177e4SLinus Torvalds 				else
9481da177e4SLinus Torvalds 					goto copcsr;
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 			}
9511da177e4SLinus Torvalds 			else {
9521da177e4SLinus Torvalds 				return SIGILL;
9531da177e4SLinus Torvalds 			}
9541da177e4SLinus Torvalds 			break;
9551da177e4SLinus Torvalds 		}
9561da177e4SLinus Torvalds 		break;
9571da177e4SLinus Torvalds 	}
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 	case d_fmt:{
9601da177e4SLinus Torvalds 		union {
9611da177e4SLinus Torvalds 			ieee754dp(*b) (ieee754dp, ieee754dp);
9621da177e4SLinus Torvalds 			ieee754dp(*u) (ieee754dp);
9631da177e4SLinus Torvalds 		} handler;
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
9661da177e4SLinus Torvalds 			/* binary ops */
9671da177e4SLinus Torvalds 		case fadd_op:
9681da177e4SLinus Torvalds 			handler.b = ieee754dp_add;
9691da177e4SLinus Torvalds 			goto dcopbop;
9701da177e4SLinus Torvalds 		case fsub_op:
9711da177e4SLinus Torvalds 			handler.b = ieee754dp_sub;
9721da177e4SLinus Torvalds 			goto dcopbop;
9731da177e4SLinus Torvalds 		case fmul_op:
9741da177e4SLinus Torvalds 			handler.b = ieee754dp_mul;
9751da177e4SLinus Torvalds 			goto dcopbop;
9761da177e4SLinus Torvalds 		case fdiv_op:
9771da177e4SLinus Torvalds 			handler.b = ieee754dp_div;
9781da177e4SLinus Torvalds 			goto dcopbop;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 			/* unary  ops */
981587cb98fSRalf Baechle #if __mips >= 2 || defined(__mips64)
9821da177e4SLinus Torvalds 		case fsqrt_op:
9831da177e4SLinus Torvalds 			handler.u = ieee754dp_sqrt;
9841da177e4SLinus Torvalds 			goto dcopuop;
9851da177e4SLinus Torvalds #endif
9861da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32
9871da177e4SLinus Torvalds 		case frsqrt_op:
9881da177e4SLinus Torvalds 			handler.u = fpemu_dp_rsqrt;
9891da177e4SLinus Torvalds 			goto dcopuop;
9901da177e4SLinus Torvalds 		case frecip_op:
9911da177e4SLinus Torvalds 			handler.u = fpemu_dp_recip;
9921da177e4SLinus Torvalds 			goto dcopuop;
9931da177e4SLinus Torvalds #endif
9941da177e4SLinus Torvalds #if __mips >= 4
9951da177e4SLinus Torvalds 		case fmovc_op:
9961da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
9971da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
9981da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
9991da177e4SLinus Torvalds 				return 0;
10001da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
10011da177e4SLinus Torvalds 			break;
10021da177e4SLinus Torvalds 		case fmovz_op:
10031da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
10041da177e4SLinus Torvalds 				return 0;
10051da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
10061da177e4SLinus Torvalds 			break;
10071da177e4SLinus Torvalds 		case fmovn_op:
10081da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
10091da177e4SLinus Torvalds 				return 0;
10101da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
10111da177e4SLinus Torvalds 			break;
10121da177e4SLinus Torvalds #endif
10131da177e4SLinus Torvalds 		case fabs_op:
10141da177e4SLinus Torvalds 			handler.u = ieee754dp_abs;
10151da177e4SLinus Torvalds 			goto dcopuop;
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 		case fneg_op:
10181da177e4SLinus Torvalds 			handler.u = ieee754dp_neg;
10191da177e4SLinus Torvalds 			goto dcopuop;
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 		case fmov_op:
10221da177e4SLinus Torvalds 			/* an easy one */
10231da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
10241da177e4SLinus Torvalds 			goto copcsr;
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 			/* binary op on handler */
10271da177e4SLinus Torvalds 		      dcopbop:{
10281da177e4SLinus Torvalds 				ieee754dp fs, ft;
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 				DPFROMREG(fs, MIPSInst_FS(ir));
10311da177e4SLinus Torvalds 				DPFROMREG(ft, MIPSInst_FT(ir));
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds 				rv.d = (*handler.b) (fs, ft);
10341da177e4SLinus Torvalds 				goto copcsr;
10351da177e4SLinus Torvalds 			}
10361da177e4SLinus Torvalds 		      dcopuop:{
10371da177e4SLinus Torvalds 				ieee754dp fs;
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 				DPFROMREG(fs, MIPSInst_FS(ir));
10401da177e4SLinus Torvalds 				rv.d = (*handler.u) (fs);
10411da177e4SLinus Torvalds 				goto copcsr;
10421da177e4SLinus Torvalds 			}
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 			/* unary conv ops */
10451da177e4SLinus Torvalds 		case fcvts_op:{
10461da177e4SLinus Torvalds 			ieee754dp fs;
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
10491da177e4SLinus Torvalds 			rv.s = ieee754sp_fdp(fs);
10501da177e4SLinus Torvalds 			rfmt = s_fmt;
10511da177e4SLinus Torvalds 			goto copcsr;
10521da177e4SLinus Torvalds 		}
10531da177e4SLinus Torvalds 		case fcvtd_op:
10541da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 		case fcvtw_op:{
10571da177e4SLinus Torvalds 			ieee754dp fs;
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
10601da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);	/* wrong */
10611da177e4SLinus Torvalds 			rfmt = w_fmt;
10621da177e4SLinus Torvalds 			goto copcsr;
10631da177e4SLinus Torvalds 		}
10641da177e4SLinus Torvalds 
1065587cb98fSRalf Baechle #if __mips >= 2 || defined(__mips64)
10661da177e4SLinus Torvalds 		case fround_op:
10671da177e4SLinus Torvalds 		case ftrunc_op:
10681da177e4SLinus Torvalds 		case fceil_op:
10691da177e4SLinus Torvalds 		case ffloor_op:{
10701da177e4SLinus Torvalds 			unsigned int oldrm = ieee754_csr.rm;
10711da177e4SLinus Torvalds 			ieee754dp fs;
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
10741da177e4SLinus Torvalds 			ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
10751da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);
10761da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
10771da177e4SLinus Torvalds 			rfmt = w_fmt;
10781da177e4SLinus Torvalds 			goto copcsr;
10791da177e4SLinus Torvalds 		}
10801da177e4SLinus Torvalds #endif
10811da177e4SLinus Torvalds 
10824b724efdSRalf Baechle #if defined(__mips64)
10831da177e4SLinus Torvalds 		case fcvtl_op:{
10841da177e4SLinus Torvalds 			ieee754dp fs;
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
10871da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
10881da177e4SLinus Torvalds 			rfmt = l_fmt;
10891da177e4SLinus Torvalds 			goto copcsr;
10901da177e4SLinus Torvalds 		}
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 		case froundl_op:
10931da177e4SLinus Torvalds 		case ftruncl_op:
10941da177e4SLinus Torvalds 		case fceill_op:
10951da177e4SLinus Torvalds 		case ffloorl_op:{
10961da177e4SLinus Torvalds 			unsigned int oldrm = ieee754_csr.rm;
10971da177e4SLinus Torvalds 			ieee754dp fs;
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
11001da177e4SLinus Torvalds 			ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
11011da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
11021da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
11031da177e4SLinus Torvalds 			rfmt = l_fmt;
11041da177e4SLinus Torvalds 			goto copcsr;
11051da177e4SLinus Torvalds 		}
11064b724efdSRalf Baechle #endif /* __mips >= 3 */
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds 		default:
11091da177e4SLinus Torvalds 			if (MIPSInst_FUNC(ir) >= fcmp_op) {
11101da177e4SLinus Torvalds 				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
11111da177e4SLinus Torvalds 				ieee754dp fs, ft;
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 				DPFROMREG(fs, MIPSInst_FS(ir));
11141da177e4SLinus Torvalds 				DPFROMREG(ft, MIPSInst_FT(ir));
11151da177e4SLinus Torvalds 				rv.w = ieee754dp_cmp(fs, ft,
11161da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
11171da177e4SLinus Torvalds 				rfmt = -1;
11181da177e4SLinus Torvalds 				if ((cmpop & 0x8)
11191da177e4SLinus Torvalds 					&&
11201da177e4SLinus Torvalds 					ieee754_cxtest
11211da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
11221da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
11231da177e4SLinus Torvalds 				else
11241da177e4SLinus Torvalds 					goto copcsr;
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 			}
11271da177e4SLinus Torvalds 			else {
11281da177e4SLinus Torvalds 				return SIGILL;
11291da177e4SLinus Torvalds 			}
11301da177e4SLinus Torvalds 			break;
11311da177e4SLinus Torvalds 		}
11321da177e4SLinus Torvalds 		break;
11331da177e4SLinus Torvalds 	}
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 	case w_fmt:{
11361da177e4SLinus Torvalds 		ieee754sp fs;
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
11391da177e4SLinus Torvalds 		case fcvts_op:
11401da177e4SLinus Torvalds 			/* convert word to single precision real */
11411da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
11421da177e4SLinus Torvalds 			rv.s = ieee754sp_fint(fs.bits);
11431da177e4SLinus Torvalds 			rfmt = s_fmt;
11441da177e4SLinus Torvalds 			goto copcsr;
11451da177e4SLinus Torvalds 		case fcvtd_op:
11461da177e4SLinus Torvalds 			/* convert word to double precision real */
11471da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
11481da177e4SLinus Torvalds 			rv.d = ieee754dp_fint(fs.bits);
11491da177e4SLinus Torvalds 			rfmt = d_fmt;
11501da177e4SLinus Torvalds 			goto copcsr;
11511da177e4SLinus Torvalds 		default:
11521da177e4SLinus Torvalds 			return SIGILL;
11531da177e4SLinus Torvalds 		}
11541da177e4SLinus Torvalds 		break;
11551da177e4SLinus Torvalds 	}
11561da177e4SLinus Torvalds 
11574b724efdSRalf Baechle #if defined(__mips64)
11581da177e4SLinus Torvalds 	case l_fmt:{
11591da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
11601da177e4SLinus Torvalds 		case fcvts_op:
11611da177e4SLinus Torvalds 			/* convert long to single precision real */
11621da177e4SLinus Torvalds 			rv.s = ieee754sp_flong(ctx->fpr[MIPSInst_FS(ir)]);
11631da177e4SLinus Torvalds 			rfmt = s_fmt;
11641da177e4SLinus Torvalds 			goto copcsr;
11651da177e4SLinus Torvalds 		case fcvtd_op:
11661da177e4SLinus Torvalds 			/* convert long to double precision real */
11671da177e4SLinus Torvalds 			rv.d = ieee754dp_flong(ctx->fpr[MIPSInst_FS(ir)]);
11681da177e4SLinus Torvalds 			rfmt = d_fmt;
11691da177e4SLinus Torvalds 			goto copcsr;
11701da177e4SLinus Torvalds 		default:
11711da177e4SLinus Torvalds 			return SIGILL;
11721da177e4SLinus Torvalds 		}
11731da177e4SLinus Torvalds 		break;
11741da177e4SLinus Torvalds 	}
11751da177e4SLinus Torvalds #endif
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 	default:
11781da177e4SLinus Torvalds 		return SIGILL;
11791da177e4SLinus Torvalds 	}
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 	/*
11821da177e4SLinus Torvalds 	 * Update the fpu CSR register for this operation.
11831da177e4SLinus Torvalds 	 * If an exception is required, generate a tidy SIGFPE exception,
11841da177e4SLinus Torvalds 	 * without updating the result register.
11851da177e4SLinus Torvalds 	 * Note: cause exception bits do not accumulate, they are rewritten
11861da177e4SLinus Torvalds 	 * for each op; only the flag/sticky bits accumulate.
11871da177e4SLinus Torvalds 	 */
11881da177e4SLinus Torvalds 	ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
11891da177e4SLinus Torvalds 	if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
11901da177e4SLinus Torvalds 		/*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */
11911da177e4SLinus Torvalds 		return SIGFPE;
11921da177e4SLinus Torvalds 	}
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 	/*
11951da177e4SLinus Torvalds 	 * Now we can safely write the result back to the register file.
11961da177e4SLinus Torvalds 	 */
11971da177e4SLinus Torvalds 	switch (rfmt) {
11981da177e4SLinus Torvalds 	case -1:{
11991da177e4SLinus Torvalds #if __mips >= 4
12001da177e4SLinus Torvalds 		cond = fpucondbit[MIPSInst_FD(ir) >> 2];
12011da177e4SLinus Torvalds #else
12021da177e4SLinus Torvalds 		cond = FPU_CSR_COND;
12031da177e4SLinus Torvalds #endif
12041da177e4SLinus Torvalds 		if (rv.w)
12051da177e4SLinus Torvalds 			ctx->fcr31 |= cond;
12061da177e4SLinus Torvalds 		else
12071da177e4SLinus Torvalds 			ctx->fcr31 &= ~cond;
12081da177e4SLinus Torvalds 		break;
12091da177e4SLinus Torvalds 	}
12101da177e4SLinus Torvalds 	case d_fmt:
12111da177e4SLinus Torvalds 		DPTOREG(rv.d, MIPSInst_FD(ir));
12121da177e4SLinus Torvalds 		break;
12131da177e4SLinus Torvalds 	case s_fmt:
12141da177e4SLinus Torvalds 		SPTOREG(rv.s, MIPSInst_FD(ir));
12151da177e4SLinus Torvalds 		break;
12161da177e4SLinus Torvalds 	case w_fmt:
12171da177e4SLinus Torvalds 		SITOREG(rv.w, MIPSInst_FD(ir));
12181da177e4SLinus Torvalds 		break;
12194b724efdSRalf Baechle #if defined(__mips64)
12201da177e4SLinus Torvalds 	case l_fmt:
12211da177e4SLinus Torvalds 		DITOREG(rv.l, MIPSInst_FD(ir));
12221da177e4SLinus Torvalds 		break;
12231da177e4SLinus Torvalds #endif
12241da177e4SLinus Torvalds 	default:
12251da177e4SLinus Torvalds 		return SIGILL;
12261da177e4SLinus Torvalds 	}
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 	return 0;
12291da177e4SLinus Torvalds }
12301da177e4SLinus Torvalds 
1231e04582b7SAtsushi Nemoto int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1232e04582b7SAtsushi Nemoto 	int has_fpu)
12331da177e4SLinus Torvalds {
1234333d1f67SRalf Baechle 	unsigned long oldepc, prevepc;
12351da177e4SLinus Torvalds 	mips_instruction insn;
12361da177e4SLinus Torvalds 	int sig = 0;
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 	oldepc = xcp->cp0_epc;
12391da177e4SLinus Torvalds 	do {
12401da177e4SLinus Torvalds 		prevepc = xcp->cp0_epc;
12411da177e4SLinus Torvalds 
12423fccc015SRalf Baechle 		if (get_user(insn, (mips_instruction __user *) xcp->cp0_epc)) {
12434a99d1e2SRalf Baechle 			fpuemustats.errors++;
12441da177e4SLinus Torvalds 			return SIGBUS;
12451da177e4SLinus Torvalds 		}
12461da177e4SLinus Torvalds 		if (insn == 0)
12471da177e4SLinus Torvalds 			xcp->cp0_epc += 4;	/* skip nops */
12481da177e4SLinus Torvalds 		else {
1249cd21dfcfSRalf Baechle 			/*
1250cd21dfcfSRalf Baechle 			 * The 'ieee754_csr' is an alias of
1251cd21dfcfSRalf Baechle 			 * ctx->fcr31.  No need to copy ctx->fcr31 to
1252cd21dfcfSRalf Baechle 			 * ieee754_csr.  But ieee754_csr.rm is ieee
1253cd21dfcfSRalf Baechle 			 * library modes. (not mips rounding mode)
1254cd21dfcfSRalf Baechle 			 */
1255cd21dfcfSRalf Baechle 			/* convert to ieee library modes */
1256cd21dfcfSRalf Baechle 			ieee754_csr.rm = ieee_rm[ieee754_csr.rm];
12571da177e4SLinus Torvalds 			sig = cop1Emulate(xcp, ctx);
1258cd21dfcfSRalf Baechle 			/* revert to mips rounding mode */
1259cd21dfcfSRalf Baechle 			ieee754_csr.rm = mips_rm[ieee754_csr.rm];
12601da177e4SLinus Torvalds 		}
12611da177e4SLinus Torvalds 
1262e04582b7SAtsushi Nemoto 		if (has_fpu)
12631da177e4SLinus Torvalds 			break;
12641da177e4SLinus Torvalds 		if (sig)
12651da177e4SLinus Torvalds 			break;
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 		cond_resched();
12681da177e4SLinus Torvalds 	} while (xcp->cp0_epc > prevepc);
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 	/* SIGILL indicates a non-fpu instruction */
12711da177e4SLinus Torvalds 	if (sig == SIGILL && xcp->cp0_epc != oldepc)
12721da177e4SLinus Torvalds 		/* but if epc has advanced, then ignore it */
12731da177e4SLinus Torvalds 		sig = 0;
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 	return sig;
12761da177e4SLinus Torvalds }
127783fd38caSAtsushi Nemoto 
127883fd38caSAtsushi Nemoto #ifdef CONFIG_DEBUG_FS
127983fd38caSAtsushi Nemoto extern struct dentry *mips_debugfs_dir;
128083fd38caSAtsushi Nemoto static int __init debugfs_fpuemu(void)
128183fd38caSAtsushi Nemoto {
128283fd38caSAtsushi Nemoto 	struct dentry *d, *dir;
128383fd38caSAtsushi Nemoto 	int i;
128483fd38caSAtsushi Nemoto 	static struct {
128583fd38caSAtsushi Nemoto 		const char *name;
128683fd38caSAtsushi Nemoto 		unsigned int *v;
128783fd38caSAtsushi Nemoto 	} vars[] __initdata = {
128883fd38caSAtsushi Nemoto 		{ "emulated", &fpuemustats.emulated },
128983fd38caSAtsushi Nemoto 		{ "loads",    &fpuemustats.loads },
129083fd38caSAtsushi Nemoto 		{ "stores",   &fpuemustats.stores },
129183fd38caSAtsushi Nemoto 		{ "cp1ops",   &fpuemustats.cp1ops },
129283fd38caSAtsushi Nemoto 		{ "cp1xops",  &fpuemustats.cp1xops },
129383fd38caSAtsushi Nemoto 		{ "errors",   &fpuemustats.errors },
129483fd38caSAtsushi Nemoto 	};
129583fd38caSAtsushi Nemoto 
129683fd38caSAtsushi Nemoto 	if (!mips_debugfs_dir)
129783fd38caSAtsushi Nemoto 		return -ENODEV;
129883fd38caSAtsushi Nemoto 	dir = debugfs_create_dir("fpuemustats", mips_debugfs_dir);
1299ecab1f44SZhaolei 	if (!dir)
1300ecab1f44SZhaolei 		return -ENOMEM;
130183fd38caSAtsushi Nemoto 	for (i = 0; i < ARRAY_SIZE(vars); i++) {
130283fd38caSAtsushi Nemoto 		d = debugfs_create_u32(vars[i].name, S_IRUGO, dir, vars[i].v);
1303ecab1f44SZhaolei 		if (!d)
1304ecab1f44SZhaolei 			return -ENOMEM;
130583fd38caSAtsushi Nemoto 	}
130683fd38caSAtsushi Nemoto 	return 0;
130783fd38caSAtsushi Nemoto }
130883fd38caSAtsushi Nemoto __initcall(debugfs_fpuemu);
130983fd38caSAtsushi Nemoto #endif
1310