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