xref: /linux/arch/mips/math-emu/cp1emu.c (revision 333d1f6794b341df11f286f5dca123c6dc64a770)
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>
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include <asm/inst.h>
401da177e4SLinus Torvalds #include <asm/bootinfo.h>
411da177e4SLinus Torvalds #include <asm/cpu.h>
421da177e4SLinus Torvalds #include <asm/cpu-features.h>
431da177e4SLinus Torvalds #include <asm/processor.h>
441da177e4SLinus Torvalds #include <asm/ptrace.h>
451da177e4SLinus Torvalds #include <asm/signal.h>
461da177e4SLinus Torvalds #include <asm/mipsregs.h>
471da177e4SLinus Torvalds #include <asm/fpu_emulator.h>
481da177e4SLinus Torvalds #include <asm/uaccess.h>
491da177e4SLinus Torvalds #include <asm/branch.h>
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #include "ieee754.h"
521da177e4SLinus Torvalds #include "dsemul.h"
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds /* Strap kernel emulator for full MIPS IV emulation */
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds #ifdef __mips
571da177e4SLinus Torvalds #undef __mips
581da177e4SLinus Torvalds #endif
591da177e4SLinus Torvalds #define __mips 4
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds /* Function which emulates a floating point instruction. */
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds static int fpu_emu(struct pt_regs *, struct mips_fpu_soft_struct *,
641da177e4SLinus Torvalds 	mips_instruction);
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32
671da177e4SLinus Torvalds static int fpux_emu(struct pt_regs *,
681da177e4SLinus Torvalds 	struct mips_fpu_soft_struct *, mips_instruction);
691da177e4SLinus Torvalds #endif
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /* Further private data for which no space exists in mips_fpu_soft_struct */
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds struct mips_fpu_emulator_private fpuemuprivate;
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds /* Control registers */
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds #define FPCREG_RID	0	/* $0  = revision id */
781da177e4SLinus Torvalds #define FPCREG_CSR	31	/* $31 = csr */
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds /* Convert Mips rounding mode (0..3) to IEEE library modes. */
811da177e4SLinus Torvalds static const unsigned char ieee_rm[4] = {
821da177e4SLinus Torvalds 	IEEE754_RN, IEEE754_RZ, IEEE754_RU, IEEE754_RD
831da177e4SLinus Torvalds };
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds #if __mips >= 4
861da177e4SLinus Torvalds /* convert condition code register number to csr bit */
871da177e4SLinus Torvalds static const unsigned int fpucondbit[8] = {
881da177e4SLinus Torvalds 	FPU_CSR_COND0,
891da177e4SLinus Torvalds 	FPU_CSR_COND1,
901da177e4SLinus Torvalds 	FPU_CSR_COND2,
911da177e4SLinus Torvalds 	FPU_CSR_COND3,
921da177e4SLinus Torvalds 	FPU_CSR_COND4,
931da177e4SLinus Torvalds 	FPU_CSR_COND5,
941da177e4SLinus Torvalds 	FPU_CSR_COND6,
951da177e4SLinus Torvalds 	FPU_CSR_COND7
961da177e4SLinus Torvalds };
971da177e4SLinus Torvalds #endif
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds /*
1011da177e4SLinus Torvalds  * Redundant with logic already in kernel/branch.c,
1021da177e4SLinus Torvalds  * embedded in compute_return_epc.  At some point,
1031da177e4SLinus Torvalds  * a single subroutine should be used across both
1041da177e4SLinus Torvalds  * modules.
1051da177e4SLinus Torvalds  */
1061da177e4SLinus Torvalds static int isBranchInstr(mips_instruction * i)
1071da177e4SLinus Torvalds {
1081da177e4SLinus Torvalds 	switch (MIPSInst_OPCODE(*i)) {
1091da177e4SLinus Torvalds 	case spec_op:
1101da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(*i)) {
1111da177e4SLinus Torvalds 		case jalr_op:
1121da177e4SLinus Torvalds 		case jr_op:
1131da177e4SLinus Torvalds 			return 1;
1141da177e4SLinus Torvalds 		}
1151da177e4SLinus Torvalds 		break;
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	case bcond_op:
1181da177e4SLinus Torvalds 		switch (MIPSInst_RT(*i)) {
1191da177e4SLinus Torvalds 		case bltz_op:
1201da177e4SLinus Torvalds 		case bgez_op:
1211da177e4SLinus Torvalds 		case bltzl_op:
1221da177e4SLinus Torvalds 		case bgezl_op:
1231da177e4SLinus Torvalds 		case bltzal_op:
1241da177e4SLinus Torvalds 		case bgezal_op:
1251da177e4SLinus Torvalds 		case bltzall_op:
1261da177e4SLinus Torvalds 		case bgezall_op:
1271da177e4SLinus Torvalds 			return 1;
1281da177e4SLinus Torvalds 		}
1291da177e4SLinus Torvalds 		break;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	case j_op:
1321da177e4SLinus Torvalds 	case jal_op:
1331da177e4SLinus Torvalds 	case jalx_op:
1341da177e4SLinus Torvalds 	case beq_op:
1351da177e4SLinus Torvalds 	case bne_op:
1361da177e4SLinus Torvalds 	case blez_op:
1371da177e4SLinus Torvalds 	case bgtz_op:
1381da177e4SLinus Torvalds 	case beql_op:
1391da177e4SLinus Torvalds 	case bnel_op:
1401da177e4SLinus Torvalds 	case blezl_op:
1411da177e4SLinus Torvalds 	case bgtzl_op:
1421da177e4SLinus Torvalds 		return 1;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	case cop0_op:
1451da177e4SLinus Torvalds 	case cop1_op:
1461da177e4SLinus Torvalds 	case cop2_op:
1471da177e4SLinus Torvalds 	case cop1x_op:
1481da177e4SLinus Torvalds 		if (MIPSInst_RS(*i) == bc_op)
1491da177e4SLinus Torvalds 			return 1;
1501da177e4SLinus Torvalds 		break;
1511da177e4SLinus Torvalds 	}
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	return 0;
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds /*
1571da177e4SLinus Torvalds  * In the Linux kernel, we support selection of FPR format on the
1581da177e4SLinus Torvalds  * basis of the Status.FR bit.  This does imply that, if a full 32
1591da177e4SLinus Torvalds  * FPRs are desired, there needs to be a flip-flop that can be written
1601da177e4SLinus Torvalds  * to one at that bit position.  In any case, O32 MIPS ABI uses
1611da177e4SLinus Torvalds  * only the even FPRs (Status.FR = 0).
1621da177e4SLinus Torvalds  */
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds #define CP0_STATUS_FR_SUPPORT
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds #ifdef CP0_STATUS_FR_SUPPORT
1671da177e4SLinus Torvalds #define FR_BIT ST0_FR
1681da177e4SLinus Torvalds #else
1691da177e4SLinus Torvalds #define FR_BIT 0
1701da177e4SLinus Torvalds #endif
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds #define SIFROMREG(si,x)	((si) = \
1731da177e4SLinus Torvalds 			(xcp->cp0_status & FR_BIT) || !(x & 1) ? \
1741da177e4SLinus Torvalds 			(int)ctx->fpr[x] : \
1751da177e4SLinus Torvalds 			(int)(ctx->fpr[x & ~1] >> 32 ))
1761da177e4SLinus Torvalds #define SITOREG(si,x)	(ctx->fpr[x & ~((xcp->cp0_status & FR_BIT) == 0)] = \
1771da177e4SLinus Torvalds 			(xcp->cp0_status & FR_BIT) || !(x & 1) ? \
1781da177e4SLinus Torvalds 			ctx->fpr[x & ~1] >> 32 << 32 | (u32)(si) : \
1791da177e4SLinus Torvalds 			ctx->fpr[x & ~1] << 32 >> 32 | (u64)(si) << 32)
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds #define DIFROMREG(di,x)	((di) = \
1821da177e4SLinus Torvalds 			ctx->fpr[x & ~((xcp->cp0_status & FR_BIT) == 0)])
1831da177e4SLinus Torvalds #define DITOREG(di,x)	(ctx->fpr[x & ~((xcp->cp0_status & FR_BIT) == 0)] \
1841da177e4SLinus Torvalds 			= (di))
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds #define SPFROMREG(sp,x)	SIFROMREG((sp).bits,x)
1871da177e4SLinus Torvalds #define SPTOREG(sp,x)	SITOREG((sp).bits,x)
1881da177e4SLinus Torvalds #define DPFROMREG(dp,x)	DIFROMREG((dp).bits,x)
1891da177e4SLinus Torvalds #define DPTOREG(dp,x)	DITOREG((dp).bits,x)
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds /*
1921da177e4SLinus Torvalds  * Emulate the single floating point instruction pointed at by EPC.
1931da177e4SLinus Torvalds  * Two instructions if the instruction is in a branch delay slot.
1941da177e4SLinus Torvalds  */
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_soft_struct *ctx)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	mips_instruction ir;
199*333d1f67SRalf Baechle 	void * emulpc, *contpc;
2001da177e4SLinus Torvalds 	unsigned int cond;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	if (get_user(ir, (mips_instruction *) xcp->cp0_epc)) {
2031da177e4SLinus Torvalds 		fpuemuprivate.stats.errors++;
2041da177e4SLinus Torvalds 		return SIGBUS;
2051da177e4SLinus Torvalds 	}
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	/* XXX NEC Vr54xx bug workaround */
2081da177e4SLinus Torvalds 	if ((xcp->cp0_cause & CAUSEF_BD) && !isBranchInstr(&ir))
2091da177e4SLinus Torvalds 		xcp->cp0_cause &= ~CAUSEF_BD;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	if (xcp->cp0_cause & CAUSEF_BD) {
2121da177e4SLinus Torvalds 		/*
2131da177e4SLinus Torvalds 		 * The instruction to be emulated is in a branch delay slot
2141da177e4SLinus Torvalds 		 * which means that we have to  emulate the branch instruction
2151da177e4SLinus Torvalds 		 * BEFORE we do the cop1 instruction.
2161da177e4SLinus Torvalds 		 *
2171da177e4SLinus Torvalds 		 * This branch could be a COP1 branch, but in that case we
2181da177e4SLinus Torvalds 		 * would have had a trap for that instruction, and would not
2191da177e4SLinus Torvalds 		 * come through this route.
2201da177e4SLinus Torvalds 		 *
2211da177e4SLinus Torvalds 		 * Linux MIPS branch emulator operates on context, updating the
2221da177e4SLinus Torvalds 		 * cp0_epc.
2231da177e4SLinus Torvalds 		 */
224*333d1f67SRalf Baechle 		emulpc = (void *) (xcp->cp0_epc + 4);	/* Snapshot emulation target */
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 		if (__compute_return_epc(xcp)) {
2271da177e4SLinus Torvalds #ifdef CP1DBG
2281da177e4SLinus Torvalds 			printk("failed to emulate branch at %p\n",
229*333d1f67SRalf Baechle 				(void *) (xcp->cp0_epc));
2301da177e4SLinus Torvalds #endif
2311da177e4SLinus Torvalds 			return SIGILL;
2321da177e4SLinus Torvalds 		}
2331da177e4SLinus Torvalds 		if (get_user(ir, (mips_instruction *) emulpc)) {
2341da177e4SLinus Torvalds 			fpuemuprivate.stats.errors++;
2351da177e4SLinus Torvalds 			return SIGBUS;
2361da177e4SLinus Torvalds 		}
2371da177e4SLinus Torvalds 		/* __compute_return_epc() will have updated cp0_epc */
238*333d1f67SRalf Baechle 		contpc = (void *)  xcp->cp0_epc;
2391da177e4SLinus Torvalds 		/* In order not to confuse ptrace() et al, tweak context */
240*333d1f67SRalf Baechle 		xcp->cp0_epc = (unsigned long) emulpc - 4;
241*333d1f67SRalf Baechle 	} else {
242*333d1f67SRalf Baechle 		emulpc = (void *)  xcp->cp0_epc;
243*333d1f67SRalf Baechle 		contpc = (void *) (xcp->cp0_epc + 4);
2441da177e4SLinus Torvalds 	}
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds       emul:
2471da177e4SLinus Torvalds 	fpuemuprivate.stats.emulated++;
2481da177e4SLinus Torvalds 	switch (MIPSInst_OPCODE(ir)) {
2491da177e4SLinus Torvalds #ifndef SINGLE_ONLY_FPU
2501da177e4SLinus Torvalds 	case ldc1_op:{
251*333d1f67SRalf Baechle 		u64 *va = (void *) (xcp->regs[MIPSInst_RS(ir)] +
2521da177e4SLinus Torvalds 			MIPSInst_SIMM(ir));
2531da177e4SLinus Torvalds 		u64 val;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 		fpuemuprivate.stats.loads++;
2561da177e4SLinus Torvalds 		if (get_user(val, va)) {
2571da177e4SLinus Torvalds 			fpuemuprivate.stats.errors++;
2581da177e4SLinus Torvalds 			return SIGBUS;
2591da177e4SLinus Torvalds 		}
2601da177e4SLinus Torvalds 		DITOREG(val, MIPSInst_RT(ir));
2611da177e4SLinus Torvalds 		break;
2621da177e4SLinus Torvalds 	}
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	case sdc1_op:{
265*333d1f67SRalf Baechle 		u64 *va = (void *) (xcp->regs[MIPSInst_RS(ir)] +
2661da177e4SLinus Torvalds 			MIPSInst_SIMM(ir));
2671da177e4SLinus Torvalds 		u64 val;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 		fpuemuprivate.stats.stores++;
2701da177e4SLinus Torvalds 		DIFROMREG(val, MIPSInst_RT(ir));
2711da177e4SLinus Torvalds 		if (put_user(val, va)) {
2721da177e4SLinus Torvalds 			fpuemuprivate.stats.errors++;
2731da177e4SLinus Torvalds 			return SIGBUS;
2741da177e4SLinus Torvalds 		}
2751da177e4SLinus Torvalds 		break;
2761da177e4SLinus Torvalds 	}
2771da177e4SLinus Torvalds #endif
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	case lwc1_op:{
280*333d1f67SRalf Baechle 		u32 *va = (void *) (xcp->regs[MIPSInst_RS(ir)] +
2811da177e4SLinus Torvalds 			MIPSInst_SIMM(ir));
2821da177e4SLinus Torvalds 		u32 val;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 		fpuemuprivate.stats.loads++;
2851da177e4SLinus Torvalds 		if (get_user(val, va)) {
2861da177e4SLinus Torvalds 			fpuemuprivate.stats.errors++;
2871da177e4SLinus Torvalds 			return SIGBUS;
2881da177e4SLinus Torvalds 		}
2891da177e4SLinus Torvalds #ifdef SINGLE_ONLY_FPU
2901da177e4SLinus Torvalds 		if (MIPSInst_RT(ir) & 1) {
2911da177e4SLinus Torvalds 			/* illegal register in single-float mode */
2921da177e4SLinus Torvalds 			return SIGILL;
2931da177e4SLinus Torvalds 		}
2941da177e4SLinus Torvalds #endif
2951da177e4SLinus Torvalds 		SITOREG(val, MIPSInst_RT(ir));
2961da177e4SLinus Torvalds 		break;
2971da177e4SLinus Torvalds 	}
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	case swc1_op:{
300*333d1f67SRalf Baechle 		u32 *va = (void *) (xcp->regs[MIPSInst_RS(ir)] +
3011da177e4SLinus Torvalds 			MIPSInst_SIMM(ir));
3021da177e4SLinus Torvalds 		u32 val;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 		fpuemuprivate.stats.stores++;
3051da177e4SLinus Torvalds #ifdef SINGLE_ONLY_FPU
3061da177e4SLinus Torvalds 		if (MIPSInst_RT(ir) & 1) {
3071da177e4SLinus Torvalds 			/* illegal register in single-float mode */
3081da177e4SLinus Torvalds 			return SIGILL;
3091da177e4SLinus Torvalds 		}
3101da177e4SLinus Torvalds #endif
3111da177e4SLinus Torvalds 		SIFROMREG(val, MIPSInst_RT(ir));
3121da177e4SLinus Torvalds 		if (put_user(val, va)) {
3131da177e4SLinus Torvalds 			fpuemuprivate.stats.errors++;
3141da177e4SLinus Torvalds 			return SIGBUS;
3151da177e4SLinus Torvalds 		}
3161da177e4SLinus Torvalds 		break;
3171da177e4SLinus Torvalds 	}
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	case cop1_op:
3201da177e4SLinus Torvalds 		switch (MIPSInst_RS(ir)) {
3211da177e4SLinus Torvalds 
322766160c2SYoichi Yuasa #if defined(__mips64) && !defined(SINGLE_ONLY_FPU)
3231da177e4SLinus Torvalds 		case dmfc_op:
3241da177e4SLinus Torvalds 			/* copregister fs -> gpr[rt] */
3251da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
3261da177e4SLinus Torvalds 				DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
3271da177e4SLinus Torvalds 					MIPSInst_RD(ir));
3281da177e4SLinus Torvalds 			}
3291da177e4SLinus Torvalds 			break;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 		case dmtc_op:
3321da177e4SLinus Torvalds 			/* copregister fs <- rt */
3331da177e4SLinus Torvalds 			DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
3341da177e4SLinus Torvalds 			break;
3351da177e4SLinus Torvalds #endif
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 		case mfc_op:
3381da177e4SLinus Torvalds 			/* copregister rd -> gpr[rt] */
3391da177e4SLinus Torvalds #ifdef SINGLE_ONLY_FPU
3401da177e4SLinus Torvalds 			if (MIPSInst_RD(ir) & 1) {
3411da177e4SLinus Torvalds 				/* illegal register in single-float mode */
3421da177e4SLinus Torvalds 				return SIGILL;
3431da177e4SLinus Torvalds 			}
3441da177e4SLinus Torvalds #endif
3451da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
3461da177e4SLinus Torvalds 				SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
3471da177e4SLinus Torvalds 					MIPSInst_RD(ir));
3481da177e4SLinus Torvalds 			}
3491da177e4SLinus Torvalds 			break;
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 		case mtc_op:
3521da177e4SLinus Torvalds 			/* copregister rd <- rt */
3531da177e4SLinus Torvalds #ifdef SINGLE_ONLY_FPU
3541da177e4SLinus Torvalds 			if (MIPSInst_RD(ir) & 1) {
3551da177e4SLinus Torvalds 				/* illegal register in single-float mode */
3561da177e4SLinus Torvalds 				return SIGILL;
3571da177e4SLinus Torvalds 			}
3581da177e4SLinus Torvalds #endif
3591da177e4SLinus Torvalds 			SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
3601da177e4SLinus Torvalds 			break;
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 		case cfc_op:{
3631da177e4SLinus Torvalds 			/* cop control register rd -> gpr[rt] */
3641da177e4SLinus Torvalds 			u32 value;
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 			if (ir == CP1UNDEF) {
3671da177e4SLinus Torvalds 				return do_dsemulret(xcp);
3681da177e4SLinus Torvalds 			}
3691da177e4SLinus Torvalds 			if (MIPSInst_RD(ir) == FPCREG_CSR) {
3701da177e4SLinus Torvalds 				value = ctx->fcr31;
3711da177e4SLinus Torvalds #ifdef CSRTRACE
3721da177e4SLinus Torvalds 				printk("%p gpr[%d]<-csr=%08x\n",
373*333d1f67SRalf Baechle 					(void *) (xcp->cp0_epc),
3741da177e4SLinus Torvalds 					MIPSInst_RT(ir), value);
3751da177e4SLinus Torvalds #endif
3761da177e4SLinus Torvalds 			}
3771da177e4SLinus Torvalds 			else if (MIPSInst_RD(ir) == FPCREG_RID)
3781da177e4SLinus Torvalds 				value = 0;
3791da177e4SLinus Torvalds 			else
3801da177e4SLinus Torvalds 				value = 0;
3811da177e4SLinus Torvalds 			if (MIPSInst_RT(ir))
3821da177e4SLinus Torvalds 				xcp->regs[MIPSInst_RT(ir)] = value;
3831da177e4SLinus Torvalds 			break;
3841da177e4SLinus Torvalds 		}
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 		case ctc_op:{
3871da177e4SLinus Torvalds 			/* copregister rd <- rt */
3881da177e4SLinus Torvalds 			u32 value;
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) == 0)
3911da177e4SLinus Torvalds 				value = 0;
3921da177e4SLinus Torvalds 			else
3931da177e4SLinus Torvalds 				value = xcp->regs[MIPSInst_RT(ir)];
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 			/* we only have one writable control reg
3961da177e4SLinus Torvalds 			 */
3971da177e4SLinus Torvalds 			if (MIPSInst_RD(ir) == FPCREG_CSR) {
3981da177e4SLinus Torvalds #ifdef CSRTRACE
3991da177e4SLinus Torvalds 				printk("%p gpr[%d]->csr=%08x\n",
400*333d1f67SRalf Baechle 					(void *) (xcp->cp0_epc),
4011da177e4SLinus Torvalds 					MIPSInst_RT(ir), value);
4021da177e4SLinus Torvalds #endif
4031da177e4SLinus Torvalds 				ctx->fcr31 = value;
4041da177e4SLinus Torvalds 				/* copy new rounding mode and
4051da177e4SLinus Torvalds 				   flush bit to ieee library state! */
4061da177e4SLinus Torvalds 				ieee754_csr.nod = (ctx->fcr31 & 0x1000000) != 0;
4071da177e4SLinus Torvalds 				ieee754_csr.rm = ieee_rm[value & 0x3];
4081da177e4SLinus Torvalds 			}
4091da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
4101da177e4SLinus Torvalds 				return SIGFPE;
4111da177e4SLinus Torvalds 			}
4121da177e4SLinus Torvalds 			break;
4131da177e4SLinus Torvalds 		}
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 		case bc_op:{
4161da177e4SLinus Torvalds 			int likely = 0;
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 			if (xcp->cp0_cause & CAUSEF_BD)
4191da177e4SLinus Torvalds 				return SIGILL;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds #if __mips >= 4
4221da177e4SLinus Torvalds 			cond = ctx->fcr31 & fpucondbit[MIPSInst_RT(ir) >> 2];
4231da177e4SLinus Torvalds #else
4241da177e4SLinus Torvalds 			cond = ctx->fcr31 & FPU_CSR_COND;
4251da177e4SLinus Torvalds #endif
4261da177e4SLinus Torvalds 			switch (MIPSInst_RT(ir) & 3) {
4271da177e4SLinus Torvalds 			case bcfl_op:
4281da177e4SLinus Torvalds 				likely = 1;
4291da177e4SLinus Torvalds 			case bcf_op:
4301da177e4SLinus Torvalds 				cond = !cond;
4311da177e4SLinus Torvalds 				break;
4321da177e4SLinus Torvalds 			case bctl_op:
4331da177e4SLinus Torvalds 				likely = 1;
4341da177e4SLinus Torvalds 			case bct_op:
4351da177e4SLinus Torvalds 				break;
4361da177e4SLinus Torvalds 			default:
4371da177e4SLinus Torvalds 				/* thats an illegal instruction */
4381da177e4SLinus Torvalds 				return SIGILL;
4391da177e4SLinus Torvalds 			}
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 			xcp->cp0_cause |= CAUSEF_BD;
4421da177e4SLinus Torvalds 			if (cond) {
4431da177e4SLinus Torvalds 				/* branch taken: emulate dslot
4441da177e4SLinus Torvalds 				 * instruction
4451da177e4SLinus Torvalds 				 */
4461da177e4SLinus Torvalds 				xcp->cp0_epc += 4;
447*333d1f67SRalf Baechle 				contpc = (void *)
4481da177e4SLinus Torvalds 					(xcp->cp0_epc +
4491da177e4SLinus Torvalds 					(MIPSInst_SIMM(ir) << 2));
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 				if (get_user(ir, (mips_instruction *)
452*333d1f67SRalf Baechle 						(void *)  xcp->cp0_epc)) {
4531da177e4SLinus Torvalds 					fpuemuprivate.stats.errors++;
4541da177e4SLinus Torvalds 					return SIGBUS;
4551da177e4SLinus Torvalds 				}
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 				switch (MIPSInst_OPCODE(ir)) {
4581da177e4SLinus Torvalds 				case lwc1_op:
4591da177e4SLinus Torvalds 				case swc1_op:
4601da177e4SLinus Torvalds #if (__mips >= 2 || __mips64) && !defined(SINGLE_ONLY_FPU)
4611da177e4SLinus Torvalds 				case ldc1_op:
4621da177e4SLinus Torvalds 				case sdc1_op:
4631da177e4SLinus Torvalds #endif
4641da177e4SLinus Torvalds 				case cop1_op:
4651da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32
4661da177e4SLinus Torvalds 				case cop1x_op:
4671da177e4SLinus Torvalds #endif
4681da177e4SLinus Torvalds 					/* its one of ours */
4691da177e4SLinus Torvalds 					goto emul;
4701da177e4SLinus Torvalds #if __mips >= 4
4711da177e4SLinus Torvalds 				case spec_op:
4721da177e4SLinus Torvalds 					if (MIPSInst_FUNC(ir) == movc_op)
4731da177e4SLinus Torvalds 						goto emul;
4741da177e4SLinus Torvalds 					break;
4751da177e4SLinus Torvalds #endif
4761da177e4SLinus Torvalds 				}
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 				/*
4791da177e4SLinus Torvalds 				 * Single step the non-cp1
4801da177e4SLinus Torvalds 				 * instruction in the dslot
4811da177e4SLinus Torvalds 				 */
482*333d1f67SRalf Baechle 				return mips_dsemul(xcp, ir, (unsigned long) contpc);
4831da177e4SLinus Torvalds 			}
4841da177e4SLinus Torvalds 			else {
4851da177e4SLinus Torvalds 				/* branch not taken */
4861da177e4SLinus Torvalds 				if (likely) {
4871da177e4SLinus Torvalds 					/*
4881da177e4SLinus Torvalds 					 * branch likely nullifies
4891da177e4SLinus Torvalds 					 * dslot if not taken
4901da177e4SLinus Torvalds 					 */
4911da177e4SLinus Torvalds 					xcp->cp0_epc += 4;
4921da177e4SLinus Torvalds 					contpc += 4;
4931da177e4SLinus Torvalds 					/*
4941da177e4SLinus Torvalds 					 * else continue & execute
4951da177e4SLinus Torvalds 					 * dslot as normal insn
4961da177e4SLinus Torvalds 					 */
4971da177e4SLinus Torvalds 				}
4981da177e4SLinus Torvalds 			}
4991da177e4SLinus Torvalds 			break;
5001da177e4SLinus Torvalds 		}
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 		default:
5031da177e4SLinus Torvalds 			if (!(MIPSInst_RS(ir) & 0x10))
5041da177e4SLinus Torvalds 				return SIGILL;
5051da177e4SLinus Torvalds 			{
5061da177e4SLinus Torvalds 				int sig;
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 				/* a real fpu computation instruction */
5091da177e4SLinus Torvalds 				if ((sig = fpu_emu(xcp, ctx, ir)))
5101da177e4SLinus Torvalds 					return sig;
5111da177e4SLinus Torvalds 			}
5121da177e4SLinus Torvalds 		}
5131da177e4SLinus Torvalds 		break;
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32
5161da177e4SLinus Torvalds 	case cop1x_op:{
5171da177e4SLinus Torvalds 		int sig;
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 		if ((sig = fpux_emu(xcp, ctx, ir)))
5201da177e4SLinus Torvalds 			return sig;
5211da177e4SLinus Torvalds 		break;
5221da177e4SLinus Torvalds 	}
5231da177e4SLinus Torvalds #endif
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds #if __mips >= 4
5261da177e4SLinus Torvalds 	case spec_op:
5271da177e4SLinus Torvalds 		if (MIPSInst_FUNC(ir) != movc_op)
5281da177e4SLinus Torvalds 			return SIGILL;
5291da177e4SLinus Torvalds 		cond = fpucondbit[MIPSInst_RT(ir) >> 2];
5301da177e4SLinus Torvalds 		if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
5311da177e4SLinus Torvalds 			xcp->regs[MIPSInst_RD(ir)] =
5321da177e4SLinus Torvalds 				xcp->regs[MIPSInst_RS(ir)];
5331da177e4SLinus Torvalds 		break;
5341da177e4SLinus Torvalds #endif
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	default:
5371da177e4SLinus Torvalds 		return SIGILL;
5381da177e4SLinus Torvalds 	}
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 	/* we did it !! */
541*333d1f67SRalf Baechle 	xcp->cp0_epc = (unsigned long) contpc;
5421da177e4SLinus Torvalds 	xcp->cp0_cause &= ~CAUSEF_BD;
543*333d1f67SRalf Baechle 
5441da177e4SLinus Torvalds 	return 0;
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds /*
5481da177e4SLinus Torvalds  * Conversion table from MIPS compare ops 48-63
5491da177e4SLinus Torvalds  * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
5501da177e4SLinus Torvalds  */
5511da177e4SLinus Torvalds static const unsigned char cmptab[8] = {
5521da177e4SLinus Torvalds 	0,			/* cmp_0 (sig) cmp_sf */
5531da177e4SLinus Torvalds 	IEEE754_CUN,		/* cmp_un (sig) cmp_ngle */
5541da177e4SLinus Torvalds 	IEEE754_CEQ,		/* cmp_eq (sig) cmp_seq */
5551da177e4SLinus Torvalds 	IEEE754_CEQ | IEEE754_CUN,	/* cmp_ueq (sig) cmp_ngl  */
5561da177e4SLinus Torvalds 	IEEE754_CLT,		/* cmp_olt (sig) cmp_lt */
5571da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CUN,	/* cmp_ult (sig) cmp_nge */
5581da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ,	/* cmp_ole (sig) cmp_le */
5591da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN,	/* cmp_ule (sig) cmp_ngt */
5601da177e4SLinus Torvalds };
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds /*
5661da177e4SLinus Torvalds  * Additional MIPS4 instructions
5671da177e4SLinus Torvalds  */
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds #define DEF3OP(name, p, f1, f2, f3) \
5701da177e4SLinus Torvalds static ieee754##p fpemu_##p##_##name (ieee754##p r, ieee754##p s, \
5711da177e4SLinus Torvalds     ieee754##p t) \
5721da177e4SLinus Torvalds { \
5731da177e4SLinus Torvalds 	struct ieee754_csr ieee754_csr_save; \
5741da177e4SLinus Torvalds 	s = f1 (s, t); \
5751da177e4SLinus Torvalds 	ieee754_csr_save = ieee754_csr; \
5761da177e4SLinus Torvalds 	s = f2 (s, r); \
5771da177e4SLinus Torvalds 	ieee754_csr_save.cx |= ieee754_csr.cx; \
5781da177e4SLinus Torvalds 	ieee754_csr_save.sx |= ieee754_csr.sx; \
5791da177e4SLinus Torvalds 	s = f3 (s); \
5801da177e4SLinus Torvalds 	ieee754_csr.cx |= ieee754_csr_save.cx; \
5811da177e4SLinus Torvalds 	ieee754_csr.sx |= ieee754_csr_save.sx; \
5821da177e4SLinus Torvalds 	return s; \
5831da177e4SLinus Torvalds }
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds static ieee754dp fpemu_dp_recip(ieee754dp d)
5861da177e4SLinus Torvalds {
5871da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), d);
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds static ieee754dp fpemu_dp_rsqrt(ieee754dp d)
5911da177e4SLinus Torvalds {
5921da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
5931da177e4SLinus Torvalds }
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds static ieee754sp fpemu_sp_recip(ieee754sp s)
5961da177e4SLinus Torvalds {
5971da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), s);
5981da177e4SLinus Torvalds }
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds static ieee754sp fpemu_sp_rsqrt(ieee754sp s)
6011da177e4SLinus Torvalds {
6021da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
6031da177e4SLinus Torvalds }
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add,);
6061da177e4SLinus Torvalds DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub,);
6071da177e4SLinus Torvalds DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
6081da177e4SLinus Torvalds DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
6091da177e4SLinus Torvalds DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add,);
6101da177e4SLinus Torvalds DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub,);
6111da177e4SLinus Torvalds DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
6121da177e4SLinus Torvalds DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_soft_struct *ctx,
6151da177e4SLinus Torvalds 	mips_instruction ir)
6161da177e4SLinus Torvalds {
6171da177e4SLinus Torvalds 	unsigned rcsr = 0;	/* resulting csr */
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	fpuemuprivate.stats.cp1xops++;
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	switch (MIPSInst_FMA_FFMT(ir)) {
6221da177e4SLinus Torvalds 	case s_fmt:{		/* 0 */
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 		ieee754sp(*handler) (ieee754sp, ieee754sp, ieee754sp);
6251da177e4SLinus Torvalds 		ieee754sp fd, fr, fs, ft;
6261da177e4SLinus Torvalds 		u32 *va;
6271da177e4SLinus Torvalds 		u32 val;
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
6301da177e4SLinus Torvalds 		case lwxc1_op:
631*333d1f67SRalf Baechle 			va = (void *) (xcp->regs[MIPSInst_FR(ir)] +
6321da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 			fpuemuprivate.stats.loads++;
6351da177e4SLinus Torvalds 			if (get_user(val, va)) {
6361da177e4SLinus Torvalds 				fpuemuprivate.stats.errors++;
6371da177e4SLinus Torvalds 				return SIGBUS;
6381da177e4SLinus Torvalds 			}
6391da177e4SLinus Torvalds #ifdef SINGLE_ONLY_FPU
6401da177e4SLinus Torvalds 			if (MIPSInst_FD(ir) & 1) {
6411da177e4SLinus Torvalds 				/* illegal register in single-float
6421da177e4SLinus Torvalds 				 * mode
6431da177e4SLinus Torvalds 				 */
6441da177e4SLinus Torvalds 				return SIGILL;
6451da177e4SLinus Torvalds 			}
6461da177e4SLinus Torvalds #endif
6471da177e4SLinus Torvalds 			SITOREG(val, MIPSInst_FD(ir));
6481da177e4SLinus Torvalds 			break;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 		case swxc1_op:
651*333d1f67SRalf Baechle 			va = (void *) (xcp->regs[MIPSInst_FR(ir)] +
6521da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 			fpuemuprivate.stats.stores++;
6551da177e4SLinus Torvalds #ifdef SINGLE_ONLY_FPU
6561da177e4SLinus Torvalds 			if (MIPSInst_FS(ir) & 1) {
6571da177e4SLinus Torvalds 				/* illegal register in single-float
6581da177e4SLinus Torvalds 				 * mode
6591da177e4SLinus Torvalds 				 */
6601da177e4SLinus Torvalds 				return SIGILL;
6611da177e4SLinus Torvalds 			}
6621da177e4SLinus Torvalds #endif
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 			SIFROMREG(val, MIPSInst_FS(ir));
6651da177e4SLinus Torvalds 			if (put_user(val, va)) {
6661da177e4SLinus Torvalds 				fpuemuprivate.stats.errors++;
6671da177e4SLinus Torvalds 				return SIGBUS;
6681da177e4SLinus Torvalds 			}
6691da177e4SLinus Torvalds 			break;
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 		case madd_s_op:
6721da177e4SLinus Torvalds 			handler = fpemu_sp_madd;
6731da177e4SLinus Torvalds 			goto scoptop;
6741da177e4SLinus Torvalds 		case msub_s_op:
6751da177e4SLinus Torvalds 			handler = fpemu_sp_msub;
6761da177e4SLinus Torvalds 			goto scoptop;
6771da177e4SLinus Torvalds 		case nmadd_s_op:
6781da177e4SLinus Torvalds 			handler = fpemu_sp_nmadd;
6791da177e4SLinus Torvalds 			goto scoptop;
6801da177e4SLinus Torvalds 		case nmsub_s_op:
6811da177e4SLinus Torvalds 			handler = fpemu_sp_nmsub;
6821da177e4SLinus Torvalds 			goto scoptop;
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 		      scoptop:
6851da177e4SLinus Torvalds 			SPFROMREG(fr, MIPSInst_FR(ir));
6861da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
6871da177e4SLinus Torvalds 			SPFROMREG(ft, MIPSInst_FT(ir));
6881da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
6891da177e4SLinus Torvalds 			SPTOREG(fd, MIPSInst_FD(ir));
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 		      copcsr:
6921da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_INEXACT))
6931da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
6941da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_UNDERFLOW))
6951da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
6961da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_OVERFLOW))
6971da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
6981da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
6991da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 			ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
7021da177e4SLinus Torvalds 			if (ieee754_csr.nod)
7031da177e4SLinus Torvalds 				ctx->fcr31 |= 0x1000000;
7041da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
7051da177e4SLinus Torvalds 				/*printk ("SIGFPE: fpu csr = %08x\n",
7061da177e4SLinus Torvalds 				   ctx->fcr31); */
7071da177e4SLinus Torvalds 				return SIGFPE;
7081da177e4SLinus Torvalds 			}
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 			break;
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 		default:
7131da177e4SLinus Torvalds 			return SIGILL;
7141da177e4SLinus Torvalds 		}
7151da177e4SLinus Torvalds 		break;
7161da177e4SLinus Torvalds 	}
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds #ifndef SINGLE_ONLY_FPU
7191da177e4SLinus Torvalds 	case d_fmt:{		/* 1 */
7201da177e4SLinus Torvalds 		ieee754dp(*handler) (ieee754dp, ieee754dp, ieee754dp);
7211da177e4SLinus Torvalds 		ieee754dp fd, fr, fs, ft;
7221da177e4SLinus Torvalds 		u64 *va;
7231da177e4SLinus Torvalds 		u64 val;
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
7261da177e4SLinus Torvalds 		case ldxc1_op:
727*333d1f67SRalf Baechle 			va = (void *) (xcp->regs[MIPSInst_FR(ir)] +
7281da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 			fpuemuprivate.stats.loads++;
7311da177e4SLinus Torvalds 			if (get_user(val, va)) {
7321da177e4SLinus Torvalds 				fpuemuprivate.stats.errors++;
7331da177e4SLinus Torvalds 				return SIGBUS;
7341da177e4SLinus Torvalds 			}
7351da177e4SLinus Torvalds 			DITOREG(val, MIPSInst_FD(ir));
7361da177e4SLinus Torvalds 			break;
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 		case sdxc1_op:
739*333d1f67SRalf Baechle 			va = (void *) (xcp->regs[MIPSInst_FR(ir)] +
7401da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 			fpuemuprivate.stats.stores++;
7431da177e4SLinus Torvalds 			DIFROMREG(val, MIPSInst_FS(ir));
7441da177e4SLinus Torvalds 			if (put_user(val, va)) {
7451da177e4SLinus Torvalds 				fpuemuprivate.stats.errors++;
7461da177e4SLinus Torvalds 				return SIGBUS;
7471da177e4SLinus Torvalds 			}
7481da177e4SLinus Torvalds 			break;
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 		case madd_d_op:
7511da177e4SLinus Torvalds 			handler = fpemu_dp_madd;
7521da177e4SLinus Torvalds 			goto dcoptop;
7531da177e4SLinus Torvalds 		case msub_d_op:
7541da177e4SLinus Torvalds 			handler = fpemu_dp_msub;
7551da177e4SLinus Torvalds 			goto dcoptop;
7561da177e4SLinus Torvalds 		case nmadd_d_op:
7571da177e4SLinus Torvalds 			handler = fpemu_dp_nmadd;
7581da177e4SLinus Torvalds 			goto dcoptop;
7591da177e4SLinus Torvalds 		case nmsub_d_op:
7601da177e4SLinus Torvalds 			handler = fpemu_dp_nmsub;
7611da177e4SLinus Torvalds 			goto dcoptop;
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 		      dcoptop:
7641da177e4SLinus Torvalds 			DPFROMREG(fr, MIPSInst_FR(ir));
7651da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
7661da177e4SLinus Torvalds 			DPFROMREG(ft, MIPSInst_FT(ir));
7671da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
7681da177e4SLinus Torvalds 			DPTOREG(fd, MIPSInst_FD(ir));
7691da177e4SLinus Torvalds 			goto copcsr;
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 		default:
7721da177e4SLinus Torvalds 			return SIGILL;
7731da177e4SLinus Torvalds 		}
7741da177e4SLinus Torvalds 		break;
7751da177e4SLinus Torvalds 	}
7761da177e4SLinus Torvalds #endif
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	case 0x7:		/* 7 */
7791da177e4SLinus Torvalds 		if (MIPSInst_FUNC(ir) != pfetch_op) {
7801da177e4SLinus Torvalds 			return SIGILL;
7811da177e4SLinus Torvalds 		}
7821da177e4SLinus Torvalds 		/* ignore prefx operation */
7831da177e4SLinus Torvalds 		break;
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds 	default:
7861da177e4SLinus Torvalds 		return SIGILL;
7871da177e4SLinus Torvalds 	}
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	return 0;
7901da177e4SLinus Torvalds }
7911da177e4SLinus Torvalds #endif
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds /*
7961da177e4SLinus Torvalds  * Emulate a single COP1 arithmetic instruction.
7971da177e4SLinus Torvalds  */
7981da177e4SLinus Torvalds static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_soft_struct *ctx,
7991da177e4SLinus Torvalds 	mips_instruction ir)
8001da177e4SLinus Torvalds {
8011da177e4SLinus Torvalds 	int rfmt;		/* resulting format */
8021da177e4SLinus Torvalds 	unsigned rcsr = 0;	/* resulting csr */
8031da177e4SLinus Torvalds 	unsigned cond;
8041da177e4SLinus Torvalds 	union {
8051da177e4SLinus Torvalds 		ieee754dp d;
8061da177e4SLinus Torvalds 		ieee754sp s;
8071da177e4SLinus Torvalds 		int w;
808766160c2SYoichi Yuasa #ifdef __mips64
8091da177e4SLinus Torvalds 		s64 l;
8101da177e4SLinus Torvalds #endif
8111da177e4SLinus Torvalds 	} rv;			/* resulting value */
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	fpuemuprivate.stats.cp1ops++;
8141da177e4SLinus Torvalds 	switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
8151da177e4SLinus Torvalds 	case s_fmt:{		/* 0 */
8161da177e4SLinus Torvalds 		union {
8171da177e4SLinus Torvalds 			ieee754sp(*b) (ieee754sp, ieee754sp);
8181da177e4SLinus Torvalds 			ieee754sp(*u) (ieee754sp);
8191da177e4SLinus Torvalds 		} handler;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
8221da177e4SLinus Torvalds 			/* binary ops */
8231da177e4SLinus Torvalds 		case fadd_op:
8241da177e4SLinus Torvalds 			handler.b = ieee754sp_add;
8251da177e4SLinus Torvalds 			goto scopbop;
8261da177e4SLinus Torvalds 		case fsub_op:
8271da177e4SLinus Torvalds 			handler.b = ieee754sp_sub;
8281da177e4SLinus Torvalds 			goto scopbop;
8291da177e4SLinus Torvalds 		case fmul_op:
8301da177e4SLinus Torvalds 			handler.b = ieee754sp_mul;
8311da177e4SLinus Torvalds 			goto scopbop;
8321da177e4SLinus Torvalds 		case fdiv_op:
8331da177e4SLinus Torvalds 			handler.b = ieee754sp_div;
8341da177e4SLinus Torvalds 			goto scopbop;
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 			/* unary  ops */
8371da177e4SLinus Torvalds #if __mips >= 2 || __mips64
8381da177e4SLinus Torvalds 		case fsqrt_op:
8391da177e4SLinus Torvalds 			handler.u = ieee754sp_sqrt;
8401da177e4SLinus Torvalds 			goto scopuop;
8411da177e4SLinus Torvalds #endif
8421da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32
8431da177e4SLinus Torvalds 		case frsqrt_op:
8441da177e4SLinus Torvalds 			handler.u = fpemu_sp_rsqrt;
8451da177e4SLinus Torvalds 			goto scopuop;
8461da177e4SLinus Torvalds 		case frecip_op:
8471da177e4SLinus Torvalds 			handler.u = fpemu_sp_recip;
8481da177e4SLinus Torvalds 			goto scopuop;
8491da177e4SLinus Torvalds #endif
8501da177e4SLinus Torvalds #if __mips >= 4
8511da177e4SLinus Torvalds 		case fmovc_op:
8521da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
8531da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
8541da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
8551da177e4SLinus Torvalds 				return 0;
8561da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
8571da177e4SLinus Torvalds 			break;
8581da177e4SLinus Torvalds 		case fmovz_op:
8591da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
8601da177e4SLinus Torvalds 				return 0;
8611da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
8621da177e4SLinus Torvalds 			break;
8631da177e4SLinus Torvalds 		case fmovn_op:
8641da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
8651da177e4SLinus Torvalds 				return 0;
8661da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
8671da177e4SLinus Torvalds 			break;
8681da177e4SLinus Torvalds #endif
8691da177e4SLinus Torvalds 		case fabs_op:
8701da177e4SLinus Torvalds 			handler.u = ieee754sp_abs;
8711da177e4SLinus Torvalds 			goto scopuop;
8721da177e4SLinus Torvalds 		case fneg_op:
8731da177e4SLinus Torvalds 			handler.u = ieee754sp_neg;
8741da177e4SLinus Torvalds 			goto scopuop;
8751da177e4SLinus Torvalds 		case fmov_op:
8761da177e4SLinus Torvalds 			/* an easy one */
8771da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
8781da177e4SLinus Torvalds 			goto copcsr;
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 			/* binary op on handler */
8811da177e4SLinus Torvalds 		      scopbop:
8821da177e4SLinus Torvalds 			{
8831da177e4SLinus Torvalds 				ieee754sp fs, ft;
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 				SPFROMREG(fs, MIPSInst_FS(ir));
8861da177e4SLinus Torvalds 				SPFROMREG(ft, MIPSInst_FT(ir));
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 				rv.s = (*handler.b) (fs, ft);
8891da177e4SLinus Torvalds 				goto copcsr;
8901da177e4SLinus Torvalds 			}
8911da177e4SLinus Torvalds 		      scopuop:
8921da177e4SLinus Torvalds 			{
8931da177e4SLinus Torvalds 				ieee754sp fs;
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 				SPFROMREG(fs, MIPSInst_FS(ir));
8961da177e4SLinus Torvalds 				rv.s = (*handler.u) (fs);
8971da177e4SLinus Torvalds 				goto copcsr;
8981da177e4SLinus Torvalds 			}
8991da177e4SLinus Torvalds 		      copcsr:
9001da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_INEXACT))
9011da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
9021da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_UNDERFLOW))
9031da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
9041da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_OVERFLOW))
9051da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
9061da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_ZERO_DIVIDE))
9071da177e4SLinus Torvalds 				rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
9081da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
9091da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
9101da177e4SLinus Torvalds 			break;
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 			/* unary conv ops */
9131da177e4SLinus Torvalds 		case fcvts_op:
9141da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
9151da177e4SLinus Torvalds 		case fcvtd_op:{
9161da177e4SLinus Torvalds #ifdef SINGLE_ONLY_FPU
9171da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
9181da177e4SLinus Torvalds #else
9191da177e4SLinus Torvalds 			ieee754sp fs;
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
9221da177e4SLinus Torvalds 			rv.d = ieee754dp_fsp(fs);
9231da177e4SLinus Torvalds 			rfmt = d_fmt;
9241da177e4SLinus Torvalds 			goto copcsr;
9251da177e4SLinus Torvalds 		}
9261da177e4SLinus Torvalds #endif
9271da177e4SLinus Torvalds 		case fcvtw_op:{
9281da177e4SLinus Torvalds 			ieee754sp fs;
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
9311da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
9321da177e4SLinus Torvalds 			rfmt = w_fmt;
9331da177e4SLinus Torvalds 			goto copcsr;
9341da177e4SLinus Torvalds 		}
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds #if __mips >= 2 || __mips64
9371da177e4SLinus Torvalds 		case fround_op:
9381da177e4SLinus Torvalds 		case ftrunc_op:
9391da177e4SLinus Torvalds 		case fceil_op:
9401da177e4SLinus Torvalds 		case ffloor_op:{
9411da177e4SLinus Torvalds 			unsigned int oldrm = ieee754_csr.rm;
9421da177e4SLinus Torvalds 			ieee754sp fs;
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
9451da177e4SLinus Torvalds 			ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
9461da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
9471da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
9481da177e4SLinus Torvalds 			rfmt = w_fmt;
9491da177e4SLinus Torvalds 			goto copcsr;
9501da177e4SLinus Torvalds 		}
9511da177e4SLinus Torvalds #endif /* __mips >= 2 */
9521da177e4SLinus Torvalds 
953766160c2SYoichi Yuasa #if defined(__mips64) && !defined(SINGLE_ONLY_FPU)
9541da177e4SLinus Torvalds 		case fcvtl_op:{
9551da177e4SLinus Torvalds 			ieee754sp fs;
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
9581da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
9591da177e4SLinus Torvalds 			rfmt = l_fmt;
9601da177e4SLinus Torvalds 			goto copcsr;
9611da177e4SLinus Torvalds 		}
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 		case froundl_op:
9641da177e4SLinus Torvalds 		case ftruncl_op:
9651da177e4SLinus Torvalds 		case fceill_op:
9661da177e4SLinus Torvalds 		case ffloorl_op:{
9671da177e4SLinus Torvalds 			unsigned int oldrm = ieee754_csr.rm;
9681da177e4SLinus Torvalds 			ieee754sp fs;
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
9711da177e4SLinus Torvalds 			ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
9721da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
9731da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
9741da177e4SLinus Torvalds 			rfmt = l_fmt;
9751da177e4SLinus Torvalds 			goto copcsr;
9761da177e4SLinus Torvalds 		}
9771da177e4SLinus Torvalds #endif /* __mips64 && !fpu(single) */
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 		default:
9801da177e4SLinus Torvalds 			if (MIPSInst_FUNC(ir) >= fcmp_op) {
9811da177e4SLinus Torvalds 				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
9821da177e4SLinus Torvalds 				ieee754sp fs, ft;
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 				SPFROMREG(fs, MIPSInst_FS(ir));
9851da177e4SLinus Torvalds 				SPFROMREG(ft, MIPSInst_FT(ir));
9861da177e4SLinus Torvalds 				rv.w = ieee754sp_cmp(fs, ft,
9871da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
9881da177e4SLinus Torvalds 				rfmt = -1;
9891da177e4SLinus Torvalds 				if ((cmpop & 0x8) && ieee754_cxtest
9901da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
9911da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
9921da177e4SLinus Torvalds 				else
9931da177e4SLinus Torvalds 					goto copcsr;
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 			}
9961da177e4SLinus Torvalds 			else {
9971da177e4SLinus Torvalds 				return SIGILL;
9981da177e4SLinus Torvalds 			}
9991da177e4SLinus Torvalds 			break;
10001da177e4SLinus Torvalds 		}
10011da177e4SLinus Torvalds 		break;
10021da177e4SLinus Torvalds 	}
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds #ifndef SINGLE_ONLY_FPU
10051da177e4SLinus Torvalds 	case d_fmt:{
10061da177e4SLinus Torvalds 		union {
10071da177e4SLinus Torvalds 			ieee754dp(*b) (ieee754dp, ieee754dp);
10081da177e4SLinus Torvalds 			ieee754dp(*u) (ieee754dp);
10091da177e4SLinus Torvalds 		} handler;
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
10121da177e4SLinus Torvalds 			/* binary ops */
10131da177e4SLinus Torvalds 		case fadd_op:
10141da177e4SLinus Torvalds 			handler.b = ieee754dp_add;
10151da177e4SLinus Torvalds 			goto dcopbop;
10161da177e4SLinus Torvalds 		case fsub_op:
10171da177e4SLinus Torvalds 			handler.b = ieee754dp_sub;
10181da177e4SLinus Torvalds 			goto dcopbop;
10191da177e4SLinus Torvalds 		case fmul_op:
10201da177e4SLinus Torvalds 			handler.b = ieee754dp_mul;
10211da177e4SLinus Torvalds 			goto dcopbop;
10221da177e4SLinus Torvalds 		case fdiv_op:
10231da177e4SLinus Torvalds 			handler.b = ieee754dp_div;
10241da177e4SLinus Torvalds 			goto dcopbop;
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 			/* unary  ops */
10271da177e4SLinus Torvalds #if __mips >= 2 || __mips64
10281da177e4SLinus Torvalds 		case fsqrt_op:
10291da177e4SLinus Torvalds 			handler.u = ieee754dp_sqrt;
10301da177e4SLinus Torvalds 			goto dcopuop;
10311da177e4SLinus Torvalds #endif
10321da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32
10331da177e4SLinus Torvalds 		case frsqrt_op:
10341da177e4SLinus Torvalds 			handler.u = fpemu_dp_rsqrt;
10351da177e4SLinus Torvalds 			goto dcopuop;
10361da177e4SLinus Torvalds 		case frecip_op:
10371da177e4SLinus Torvalds 			handler.u = fpemu_dp_recip;
10381da177e4SLinus Torvalds 			goto dcopuop;
10391da177e4SLinus Torvalds #endif
10401da177e4SLinus Torvalds #if __mips >= 4
10411da177e4SLinus Torvalds 		case fmovc_op:
10421da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
10431da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
10441da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
10451da177e4SLinus Torvalds 				return 0;
10461da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
10471da177e4SLinus Torvalds 			break;
10481da177e4SLinus Torvalds 		case fmovz_op:
10491da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
10501da177e4SLinus Torvalds 				return 0;
10511da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
10521da177e4SLinus Torvalds 			break;
10531da177e4SLinus Torvalds 		case fmovn_op:
10541da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
10551da177e4SLinus Torvalds 				return 0;
10561da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
10571da177e4SLinus Torvalds 			break;
10581da177e4SLinus Torvalds #endif
10591da177e4SLinus Torvalds 		case fabs_op:
10601da177e4SLinus Torvalds 			handler.u = ieee754dp_abs;
10611da177e4SLinus Torvalds 			goto dcopuop;
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 		case fneg_op:
10641da177e4SLinus Torvalds 			handler.u = ieee754dp_neg;
10651da177e4SLinus Torvalds 			goto dcopuop;
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 		case fmov_op:
10681da177e4SLinus Torvalds 			/* an easy one */
10691da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
10701da177e4SLinus Torvalds 			goto copcsr;
10711da177e4SLinus Torvalds 
10721da177e4SLinus Torvalds 			/* binary op on handler */
10731da177e4SLinus Torvalds 		      dcopbop:{
10741da177e4SLinus Torvalds 				ieee754dp fs, ft;
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds 				DPFROMREG(fs, MIPSInst_FS(ir));
10771da177e4SLinus Torvalds 				DPFROMREG(ft, MIPSInst_FT(ir));
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 				rv.d = (*handler.b) (fs, ft);
10801da177e4SLinus Torvalds 				goto copcsr;
10811da177e4SLinus Torvalds 			}
10821da177e4SLinus Torvalds 		      dcopuop:{
10831da177e4SLinus Torvalds 				ieee754dp fs;
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 				DPFROMREG(fs, MIPSInst_FS(ir));
10861da177e4SLinus Torvalds 				rv.d = (*handler.u) (fs);
10871da177e4SLinus Torvalds 				goto copcsr;
10881da177e4SLinus Torvalds 			}
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 			/* unary conv ops */
10911da177e4SLinus Torvalds 		case fcvts_op:{
10921da177e4SLinus Torvalds 			ieee754dp fs;
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
10951da177e4SLinus Torvalds 			rv.s = ieee754sp_fdp(fs);
10961da177e4SLinus Torvalds 			rfmt = s_fmt;
10971da177e4SLinus Torvalds 			goto copcsr;
10981da177e4SLinus Torvalds 		}
10991da177e4SLinus Torvalds 		case fcvtd_op:
11001da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 		case fcvtw_op:{
11031da177e4SLinus Torvalds 			ieee754dp fs;
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
11061da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);	/* wrong */
11071da177e4SLinus Torvalds 			rfmt = w_fmt;
11081da177e4SLinus Torvalds 			goto copcsr;
11091da177e4SLinus Torvalds 		}
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds #if __mips >= 2 || __mips64
11121da177e4SLinus Torvalds 		case fround_op:
11131da177e4SLinus Torvalds 		case ftrunc_op:
11141da177e4SLinus Torvalds 		case fceil_op:
11151da177e4SLinus Torvalds 		case ffloor_op:{
11161da177e4SLinus Torvalds 			unsigned int oldrm = ieee754_csr.rm;
11171da177e4SLinus Torvalds 			ieee754dp fs;
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
11201da177e4SLinus Torvalds 			ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
11211da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);
11221da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
11231da177e4SLinus Torvalds 			rfmt = w_fmt;
11241da177e4SLinus Torvalds 			goto copcsr;
11251da177e4SLinus Torvalds 		}
11261da177e4SLinus Torvalds #endif
11271da177e4SLinus Torvalds 
1128766160c2SYoichi Yuasa #if defined(__mips64) && !defined(SINGLE_ONLY_FPU)
11291da177e4SLinus Torvalds 		case fcvtl_op:{
11301da177e4SLinus Torvalds 			ieee754dp fs;
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
11331da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
11341da177e4SLinus Torvalds 			rfmt = l_fmt;
11351da177e4SLinus Torvalds 			goto copcsr;
11361da177e4SLinus Torvalds 		}
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 		case froundl_op:
11391da177e4SLinus Torvalds 		case ftruncl_op:
11401da177e4SLinus Torvalds 		case fceill_op:
11411da177e4SLinus Torvalds 		case ffloorl_op:{
11421da177e4SLinus Torvalds 			unsigned int oldrm = ieee754_csr.rm;
11431da177e4SLinus Torvalds 			ieee754dp fs;
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
11461da177e4SLinus Torvalds 			ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
11471da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
11481da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
11491da177e4SLinus Torvalds 			rfmt = l_fmt;
11501da177e4SLinus Torvalds 			goto copcsr;
11511da177e4SLinus Torvalds 		}
11521da177e4SLinus Torvalds #endif /* __mips >= 3 && !fpu(single) */
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 		default:
11551da177e4SLinus Torvalds 			if (MIPSInst_FUNC(ir) >= fcmp_op) {
11561da177e4SLinus Torvalds 				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
11571da177e4SLinus Torvalds 				ieee754dp fs, ft;
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 				DPFROMREG(fs, MIPSInst_FS(ir));
11601da177e4SLinus Torvalds 				DPFROMREG(ft, MIPSInst_FT(ir));
11611da177e4SLinus Torvalds 				rv.w = ieee754dp_cmp(fs, ft,
11621da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
11631da177e4SLinus Torvalds 				rfmt = -1;
11641da177e4SLinus Torvalds 				if ((cmpop & 0x8)
11651da177e4SLinus Torvalds 					&&
11661da177e4SLinus Torvalds 					ieee754_cxtest
11671da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
11681da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
11691da177e4SLinus Torvalds 				else
11701da177e4SLinus Torvalds 					goto copcsr;
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 			}
11731da177e4SLinus Torvalds 			else {
11741da177e4SLinus Torvalds 				return SIGILL;
11751da177e4SLinus Torvalds 			}
11761da177e4SLinus Torvalds 			break;
11771da177e4SLinus Torvalds 		}
11781da177e4SLinus Torvalds 		break;
11791da177e4SLinus Torvalds 	}
11801da177e4SLinus Torvalds #endif /* ifndef SINGLE_ONLY_FPU */
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 	case w_fmt:{
11831da177e4SLinus Torvalds 		ieee754sp fs;
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
11861da177e4SLinus Torvalds 		case fcvts_op:
11871da177e4SLinus Torvalds 			/* convert word to single precision real */
11881da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
11891da177e4SLinus Torvalds 			rv.s = ieee754sp_fint(fs.bits);
11901da177e4SLinus Torvalds 			rfmt = s_fmt;
11911da177e4SLinus Torvalds 			goto copcsr;
11921da177e4SLinus Torvalds #ifndef SINGLE_ONLY_FPU
11931da177e4SLinus Torvalds 		case fcvtd_op:
11941da177e4SLinus Torvalds 			/* convert word to double precision real */
11951da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
11961da177e4SLinus Torvalds 			rv.d = ieee754dp_fint(fs.bits);
11971da177e4SLinus Torvalds 			rfmt = d_fmt;
11981da177e4SLinus Torvalds 			goto copcsr;
11991da177e4SLinus Torvalds #endif
12001da177e4SLinus Torvalds 		default:
12011da177e4SLinus Torvalds 			return SIGILL;
12021da177e4SLinus Torvalds 		}
12031da177e4SLinus Torvalds 		break;
12041da177e4SLinus Torvalds 	}
12051da177e4SLinus Torvalds 
1206766160c2SYoichi Yuasa #if defined(__mips64) && !defined(SINGLE_ONLY_FPU)
12071da177e4SLinus Torvalds 	case l_fmt:{
12081da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
12091da177e4SLinus Torvalds 		case fcvts_op:
12101da177e4SLinus Torvalds 			/* convert long to single precision real */
12111da177e4SLinus Torvalds 			rv.s = ieee754sp_flong(ctx->fpr[MIPSInst_FS(ir)]);
12121da177e4SLinus Torvalds 			rfmt = s_fmt;
12131da177e4SLinus Torvalds 			goto copcsr;
12141da177e4SLinus Torvalds 		case fcvtd_op:
12151da177e4SLinus Torvalds 			/* convert long to double precision real */
12161da177e4SLinus Torvalds 			rv.d = ieee754dp_flong(ctx->fpr[MIPSInst_FS(ir)]);
12171da177e4SLinus Torvalds 			rfmt = d_fmt;
12181da177e4SLinus Torvalds 			goto copcsr;
12191da177e4SLinus Torvalds 		default:
12201da177e4SLinus Torvalds 			return SIGILL;
12211da177e4SLinus Torvalds 		}
12221da177e4SLinus Torvalds 		break;
12231da177e4SLinus Torvalds 	}
12241da177e4SLinus Torvalds #endif
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 	default:
12271da177e4SLinus Torvalds 		return SIGILL;
12281da177e4SLinus Torvalds 	}
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 	/*
12311da177e4SLinus Torvalds 	 * Update the fpu CSR register for this operation.
12321da177e4SLinus Torvalds 	 * If an exception is required, generate a tidy SIGFPE exception,
12331da177e4SLinus Torvalds 	 * without updating the result register.
12341da177e4SLinus Torvalds 	 * Note: cause exception bits do not accumulate, they are rewritten
12351da177e4SLinus Torvalds 	 * for each op; only the flag/sticky bits accumulate.
12361da177e4SLinus Torvalds 	 */
12371da177e4SLinus Torvalds 	ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
12381da177e4SLinus Torvalds 	if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
12391da177e4SLinus Torvalds 		/*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */
12401da177e4SLinus Torvalds 		return SIGFPE;
12411da177e4SLinus Torvalds 	}
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	/*
12441da177e4SLinus Torvalds 	 * Now we can safely write the result back to the register file.
12451da177e4SLinus Torvalds 	 */
12461da177e4SLinus Torvalds 	switch (rfmt) {
12471da177e4SLinus Torvalds 	case -1:{
12481da177e4SLinus Torvalds #if __mips >= 4
12491da177e4SLinus Torvalds 		cond = fpucondbit[MIPSInst_FD(ir) >> 2];
12501da177e4SLinus Torvalds #else
12511da177e4SLinus Torvalds 		cond = FPU_CSR_COND;
12521da177e4SLinus Torvalds #endif
12531da177e4SLinus Torvalds 		if (rv.w)
12541da177e4SLinus Torvalds 			ctx->fcr31 |= cond;
12551da177e4SLinus Torvalds 		else
12561da177e4SLinus Torvalds 			ctx->fcr31 &= ~cond;
12571da177e4SLinus Torvalds 		break;
12581da177e4SLinus Torvalds 	}
12591da177e4SLinus Torvalds #ifndef SINGLE_ONLY_FPU
12601da177e4SLinus Torvalds 	case d_fmt:
12611da177e4SLinus Torvalds 		DPTOREG(rv.d, MIPSInst_FD(ir));
12621da177e4SLinus Torvalds 		break;
12631da177e4SLinus Torvalds #endif
12641da177e4SLinus Torvalds 	case s_fmt:
12651da177e4SLinus Torvalds 		SPTOREG(rv.s, MIPSInst_FD(ir));
12661da177e4SLinus Torvalds 		break;
12671da177e4SLinus Torvalds 	case w_fmt:
12681da177e4SLinus Torvalds 		SITOREG(rv.w, MIPSInst_FD(ir));
12691da177e4SLinus Torvalds 		break;
1270766160c2SYoichi Yuasa #if defined(__mips64) && !defined(SINGLE_ONLY_FPU)
12711da177e4SLinus Torvalds 	case l_fmt:
12721da177e4SLinus Torvalds 		DITOREG(rv.l, MIPSInst_FD(ir));
12731da177e4SLinus Torvalds 		break;
12741da177e4SLinus Torvalds #endif
12751da177e4SLinus Torvalds 	default:
12761da177e4SLinus Torvalds 		return SIGILL;
12771da177e4SLinus Torvalds 	}
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 	return 0;
12801da177e4SLinus Torvalds }
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds int fpu_emulator_cop1Handler(int xcptno, struct pt_regs *xcp,
12831da177e4SLinus Torvalds 	struct mips_fpu_soft_struct *ctx)
12841da177e4SLinus Torvalds {
1285*333d1f67SRalf Baechle 	unsigned long oldepc, prevepc;
12861da177e4SLinus Torvalds 	mips_instruction insn;
12871da177e4SLinus Torvalds 	int sig = 0;
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds 	oldepc = xcp->cp0_epc;
12901da177e4SLinus Torvalds 	do {
12911da177e4SLinus Torvalds 		prevepc = xcp->cp0_epc;
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 		if (get_user(insn, (mips_instruction *) xcp->cp0_epc)) {
12941da177e4SLinus Torvalds 			fpuemuprivate.stats.errors++;
12951da177e4SLinus Torvalds 			return SIGBUS;
12961da177e4SLinus Torvalds 		}
12971da177e4SLinus Torvalds 		if (insn == 0)
12981da177e4SLinus Torvalds 			xcp->cp0_epc += 4;	/* skip nops */
12991da177e4SLinus Torvalds 		else {
13001da177e4SLinus Torvalds 			/* Update ieee754_csr. Only relevant if we have a
13011da177e4SLinus Torvalds 			   h/w FPU */
13021da177e4SLinus Torvalds 			ieee754_csr.nod = (ctx->fcr31 & 0x1000000) != 0;
13031da177e4SLinus Torvalds 			ieee754_csr.rm = ieee_rm[ctx->fcr31 & 0x3];
13041da177e4SLinus Torvalds 			ieee754_csr.cx = (ctx->fcr31 >> 12) & 0x1f;
13051da177e4SLinus Torvalds 			sig = cop1Emulate(xcp, ctx);
13061da177e4SLinus Torvalds 		}
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 		if (cpu_has_fpu)
13091da177e4SLinus Torvalds 			break;
13101da177e4SLinus Torvalds 		if (sig)
13111da177e4SLinus Torvalds 			break;
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds 		cond_resched();
13141da177e4SLinus Torvalds 	} while (xcp->cp0_epc > prevepc);
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds 	/* SIGILL indicates a non-fpu instruction */
13171da177e4SLinus Torvalds 	if (sig == SIGILL && xcp->cp0_epc != oldepc)
13181da177e4SLinus Torvalds 		/* but if epc has advanced, then ignore it */
13191da177e4SLinus Torvalds 		sig = 0;
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds 	return sig;
13221da177e4SLinus Torvalds }
1323