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 * 71da177e4SLinus Torvalds * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com 81da177e4SLinus Torvalds * Copyright (C) 2000 MIPS Technologies, Inc. 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * This program is free software; you can distribute it and/or modify it 111da177e4SLinus Torvalds * under the terms of the GNU General Public License (Version 2) as 121da177e4SLinus Torvalds * published by the Free Software Foundation. 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * This program is distributed in the hope it will be useful, but WITHOUT 151da177e4SLinus Torvalds * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 161da177e4SLinus Torvalds * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 171da177e4SLinus Torvalds * for more details. 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License along 201da177e4SLinus Torvalds * with this program; if not, write to the Free Software Foundation, Inc., 211da177e4SLinus Torvalds * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA. 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds * A complete emulator for MIPS coprocessor 1 instructions. This is 241da177e4SLinus Torvalds * required for #float(switch) or #float(trap), where it catches all 251da177e4SLinus Torvalds * COP1 instructions via the "CoProcessor Unusable" exception. 261da177e4SLinus Torvalds * 271da177e4SLinus Torvalds * More surprisingly it is also required for #float(ieee), to help out 281da177e4SLinus Torvalds * the hardware fpu at the boundaries of the IEEE-754 representation 291da177e4SLinus Torvalds * (denormalised values, infinities, underflow, etc). It is made 301da177e4SLinus Torvalds * quite nasty because emulation of some non-COP1 instructions is 311da177e4SLinus Torvalds * required, e.g. in branch delay slots. 321da177e4SLinus Torvalds * 331da177e4SLinus Torvalds * Note if you know that you won't have an fpu, then you'll get much 341da177e4SLinus Torvalds * better performance by compiling with -msoft-float! 351da177e4SLinus Torvalds */ 361da177e4SLinus Torvalds #include <linux/sched.h> 37b6ee75edSDavid Daney #include <linux/module.h> 3883fd38caSAtsushi Nemoto #include <linux/debugfs.h> 39*7f788d2dSDeng-Cheng Zhu #include <linux/perf_event.h> 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds #include <asm/inst.h> 421da177e4SLinus Torvalds #include <asm/bootinfo.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 531da177e4SLinus Torvalds /* Strap kernel emulator for full MIPS IV emulation */ 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds #ifdef __mips 561da177e4SLinus Torvalds #undef __mips 571da177e4SLinus Torvalds #endif 581da177e4SLinus Torvalds #define __mips 4 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds /* Function which emulates a floating point instruction. */ 611da177e4SLinus Torvalds 62eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *, 631da177e4SLinus Torvalds mips_instruction); 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32 661da177e4SLinus Torvalds static int fpux_emu(struct pt_regs *, 67eae89076SAtsushi Nemoto struct mips_fpu_struct *, mips_instruction); 681da177e4SLinus Torvalds #endif 691da177e4SLinus Torvalds 70eae89076SAtsushi Nemoto /* Further private data for which no space exists in mips_fpu_struct */ 711da177e4SLinus Torvalds 72b6ee75edSDavid Daney #ifdef CONFIG_DEBUG_FS 73b6ee75edSDavid Daney DEFINE_PER_CPU(struct mips_fpu_emulator_stats, fpuemustats); 74b6ee75edSDavid Daney #endif 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds /* Control registers */ 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds #define FPCREG_RID 0 /* $0 = revision id */ 791da177e4SLinus Torvalds #define FPCREG_CSR 31 /* $31 = csr */ 801da177e4SLinus Torvalds 8195e8f634SShane McDonald /* Determine rounding mode from the RM bits of the FCSR */ 8295e8f634SShane McDonald #define modeindex(v) ((v) & FPU_CSR_RM) 8395e8f634SShane McDonald 841da177e4SLinus Torvalds /* Convert Mips rounding mode (0..3) to IEEE library modes. */ 851da177e4SLinus Torvalds static const unsigned char ieee_rm[4] = { 86cd21dfcfSRalf Baechle [FPU_CSR_RN] = IEEE754_RN, 87cd21dfcfSRalf Baechle [FPU_CSR_RZ] = IEEE754_RZ, 88cd21dfcfSRalf Baechle [FPU_CSR_RU] = IEEE754_RU, 89cd21dfcfSRalf Baechle [FPU_CSR_RD] = IEEE754_RD, 90cd21dfcfSRalf Baechle }; 91cd21dfcfSRalf Baechle /* Convert IEEE library modes to Mips rounding mode (0..3). */ 92cd21dfcfSRalf Baechle static const unsigned char mips_rm[4] = { 93cd21dfcfSRalf Baechle [IEEE754_RN] = FPU_CSR_RN, 94cd21dfcfSRalf Baechle [IEEE754_RZ] = FPU_CSR_RZ, 95cd21dfcfSRalf Baechle [IEEE754_RD] = FPU_CSR_RD, 96cd21dfcfSRalf Baechle [IEEE754_RU] = FPU_CSR_RU, 971da177e4SLinus Torvalds }; 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds #if __mips >= 4 1001da177e4SLinus Torvalds /* convert condition code register number to csr bit */ 1011da177e4SLinus Torvalds static const unsigned int fpucondbit[8] = { 1021da177e4SLinus Torvalds FPU_CSR_COND0, 1031da177e4SLinus Torvalds FPU_CSR_COND1, 1041da177e4SLinus Torvalds FPU_CSR_COND2, 1051da177e4SLinus Torvalds FPU_CSR_COND3, 1061da177e4SLinus Torvalds FPU_CSR_COND4, 1071da177e4SLinus Torvalds FPU_CSR_COND5, 1081da177e4SLinus Torvalds FPU_CSR_COND6, 1091da177e4SLinus Torvalds FPU_CSR_COND7 1101da177e4SLinus Torvalds }; 1111da177e4SLinus Torvalds #endif 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds /* 1151da177e4SLinus Torvalds * Redundant with logic already in kernel/branch.c, 1161da177e4SLinus Torvalds * embedded in compute_return_epc. At some point, 1171da177e4SLinus Torvalds * a single subroutine should be used across both 1181da177e4SLinus Torvalds * modules. 1191da177e4SLinus Torvalds */ 1201da177e4SLinus Torvalds static int isBranchInstr(mips_instruction * i) 1211da177e4SLinus Torvalds { 1221da177e4SLinus Torvalds switch (MIPSInst_OPCODE(*i)) { 1231da177e4SLinus Torvalds case spec_op: 1241da177e4SLinus Torvalds switch (MIPSInst_FUNC(*i)) { 1251da177e4SLinus Torvalds case jalr_op: 1261da177e4SLinus Torvalds case jr_op: 1271da177e4SLinus Torvalds return 1; 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds break; 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds case bcond_op: 1321da177e4SLinus Torvalds switch (MIPSInst_RT(*i)) { 1331da177e4SLinus Torvalds case bltz_op: 1341da177e4SLinus Torvalds case bgez_op: 1351da177e4SLinus Torvalds case bltzl_op: 1361da177e4SLinus Torvalds case bgezl_op: 1371da177e4SLinus Torvalds case bltzal_op: 1381da177e4SLinus Torvalds case bgezal_op: 1391da177e4SLinus Torvalds case bltzall_op: 1401da177e4SLinus Torvalds case bgezall_op: 1411da177e4SLinus Torvalds return 1; 1421da177e4SLinus Torvalds } 1431da177e4SLinus Torvalds break; 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds case j_op: 1461da177e4SLinus Torvalds case jal_op: 1471da177e4SLinus Torvalds case jalx_op: 1481da177e4SLinus Torvalds case beq_op: 1491da177e4SLinus Torvalds case bne_op: 1501da177e4SLinus Torvalds case blez_op: 1511da177e4SLinus Torvalds case bgtz_op: 1521da177e4SLinus Torvalds case beql_op: 1531da177e4SLinus Torvalds case bnel_op: 1541da177e4SLinus Torvalds case blezl_op: 1551da177e4SLinus Torvalds case bgtzl_op: 1561da177e4SLinus Torvalds return 1; 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds case cop0_op: 1591da177e4SLinus Torvalds case cop1_op: 1601da177e4SLinus Torvalds case cop2_op: 1611da177e4SLinus Torvalds case cop1x_op: 1621da177e4SLinus Torvalds if (MIPSInst_RS(*i) == bc_op) 1631da177e4SLinus Torvalds return 1; 1641da177e4SLinus Torvalds break; 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds return 0; 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds /* 1711da177e4SLinus Torvalds * In the Linux kernel, we support selection of FPR format on the 172da0bac33SDavid Daney * basis of the Status.FR bit. If an FPU is not present, the FR bit 173da0bac33SDavid Daney * is hardwired to zero, which would imply a 32-bit FPU even for 174da0bac33SDavid Daney * 64-bit CPUs. For 64-bit kernels with no FPU we use TIF_32BIT_REGS 175da0bac33SDavid Daney * as a proxy for the FR bit so that a 64-bit FPU is emulated. In any 176da0bac33SDavid Daney * case, for a 32-bit kernel which uses the O32 MIPS ABI, only the 177da0bac33SDavid Daney * even FPRs are used (Status.FR = 0). 1781da177e4SLinus Torvalds */ 179da0bac33SDavid Daney static inline int cop1_64bit(struct pt_regs *xcp) 180da0bac33SDavid Daney { 181da0bac33SDavid Daney if (cpu_has_fpu) 182da0bac33SDavid Daney return xcp->cp0_status & ST0_FR; 183da0bac33SDavid Daney #ifdef CONFIG_64BIT 184da0bac33SDavid Daney return !test_thread_flag(TIF_32BIT_REGS); 1851da177e4SLinus Torvalds #else 186da0bac33SDavid Daney return 0; 1871da177e4SLinus Torvalds #endif 188da0bac33SDavid Daney } 1891da177e4SLinus Torvalds 190da0bac33SDavid Daney #define SIFROMREG(si, x) ((si) = cop1_64bit(xcp) || !(x & 1) ? \ 191da0bac33SDavid Daney (int)ctx->fpr[x] : (int)(ctx->fpr[x & ~1] >> 32)) 192da0bac33SDavid Daney 193da0bac33SDavid Daney #define SITOREG(si, x) (ctx->fpr[x & ~(cop1_64bit(xcp) == 0)] = \ 194da0bac33SDavid Daney cop1_64bit(xcp) || !(x & 1) ? \ 1951da177e4SLinus Torvalds ctx->fpr[x & ~1] >> 32 << 32 | (u32)(si) : \ 1961da177e4SLinus Torvalds ctx->fpr[x & ~1] << 32 >> 32 | (u64)(si) << 32) 1971da177e4SLinus Torvalds 198da0bac33SDavid Daney #define DIFROMREG(di, x) ((di) = ctx->fpr[x & ~(cop1_64bit(xcp) == 0)]) 199da0bac33SDavid Daney #define DITOREG(di, x) (ctx->fpr[x & ~(cop1_64bit(xcp) == 0)] = (di)) 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x) 2021da177e4SLinus Torvalds #define SPTOREG(sp, x) SITOREG((sp).bits, x) 2031da177e4SLinus Torvalds #define DPFROMREG(dp, x) DIFROMREG((dp).bits, x) 2041da177e4SLinus Torvalds #define DPTOREG(dp, x) DITOREG((dp).bits, x) 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds /* 2071da177e4SLinus Torvalds * Emulate the single floating point instruction pointed at by EPC. 2081da177e4SLinus Torvalds * Two instructions if the instruction is in a branch delay slot. 2091da177e4SLinus Torvalds */ 2101da177e4SLinus Torvalds 211eae89076SAtsushi Nemoto static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx) 2121da177e4SLinus Torvalds { 2131da177e4SLinus Torvalds mips_instruction ir; 214e70dfc10SAtsushi Nemoto unsigned long emulpc, contpc; 2151da177e4SLinus Torvalds unsigned int cond; 2161da177e4SLinus Torvalds 2173fccc015SRalf Baechle if (get_user(ir, (mips_instruction __user *) xcp->cp0_epc)) { 218b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 2191da177e4SLinus Torvalds return SIGBUS; 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds /* XXX NEC Vr54xx bug workaround */ 2231da177e4SLinus Torvalds if ((xcp->cp0_cause & CAUSEF_BD) && !isBranchInstr(&ir)) 2241da177e4SLinus Torvalds xcp->cp0_cause &= ~CAUSEF_BD; 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds if (xcp->cp0_cause & CAUSEF_BD) { 2271da177e4SLinus Torvalds /* 2281da177e4SLinus Torvalds * The instruction to be emulated is in a branch delay slot 2291da177e4SLinus Torvalds * which means that we have to emulate the branch instruction 2301da177e4SLinus Torvalds * BEFORE we do the cop1 instruction. 2311da177e4SLinus Torvalds * 2321da177e4SLinus Torvalds * This branch could be a COP1 branch, but in that case we 2331da177e4SLinus Torvalds * would have had a trap for that instruction, and would not 2341da177e4SLinus Torvalds * come through this route. 2351da177e4SLinus Torvalds * 2361da177e4SLinus Torvalds * Linux MIPS branch emulator operates on context, updating the 2371da177e4SLinus Torvalds * cp0_epc. 2381da177e4SLinus Torvalds */ 239e70dfc10SAtsushi Nemoto emulpc = xcp->cp0_epc + 4; /* Snapshot emulation target */ 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds if (__compute_return_epc(xcp)) { 2421da177e4SLinus Torvalds #ifdef CP1DBG 2431da177e4SLinus Torvalds printk("failed to emulate branch at %p\n", 244333d1f67SRalf Baechle (void *) (xcp->cp0_epc)); 2451da177e4SLinus Torvalds #endif 2461da177e4SLinus Torvalds return SIGILL; 2471da177e4SLinus Torvalds } 2483fccc015SRalf Baechle if (get_user(ir, (mips_instruction __user *) emulpc)) { 249b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 2501da177e4SLinus Torvalds return SIGBUS; 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds /* __compute_return_epc() will have updated cp0_epc */ 253e70dfc10SAtsushi Nemoto contpc = xcp->cp0_epc; 2541da177e4SLinus Torvalds /* In order not to confuse ptrace() et al, tweak context */ 255e70dfc10SAtsushi Nemoto xcp->cp0_epc = emulpc - 4; 256333d1f67SRalf Baechle } else { 257e70dfc10SAtsushi Nemoto emulpc = xcp->cp0_epc; 258e70dfc10SAtsushi Nemoto contpc = xcp->cp0_epc + 4; 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds emul: 262*7f788d2dSDeng-Cheng Zhu perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 263*7f788d2dSDeng-Cheng Zhu 1, 0, xcp, 0); 264b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(emulated); 2651da177e4SLinus Torvalds switch (MIPSInst_OPCODE(ir)) { 2661da177e4SLinus Torvalds case ldc1_op:{ 2673fccc015SRalf Baechle u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] + 2681da177e4SLinus Torvalds MIPSInst_SIMM(ir)); 2691da177e4SLinus Torvalds u64 val; 2701da177e4SLinus Torvalds 271b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(loads); 2721da177e4SLinus Torvalds if (get_user(val, va)) { 273b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 2741da177e4SLinus Torvalds return SIGBUS; 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds DITOREG(val, MIPSInst_RT(ir)); 2771da177e4SLinus Torvalds break; 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds case sdc1_op:{ 2813fccc015SRalf Baechle u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] + 2821da177e4SLinus Torvalds MIPSInst_SIMM(ir)); 2831da177e4SLinus Torvalds u64 val; 2841da177e4SLinus Torvalds 285b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(stores); 2861da177e4SLinus Torvalds DIFROMREG(val, MIPSInst_RT(ir)); 2871da177e4SLinus Torvalds if (put_user(val, va)) { 288b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 2891da177e4SLinus Torvalds return SIGBUS; 2901da177e4SLinus Torvalds } 2911da177e4SLinus Torvalds break; 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds case lwc1_op:{ 2953fccc015SRalf Baechle u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] + 2961da177e4SLinus Torvalds MIPSInst_SIMM(ir)); 2971da177e4SLinus Torvalds u32 val; 2981da177e4SLinus Torvalds 299b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(loads); 3001da177e4SLinus Torvalds if (get_user(val, va)) { 301b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 3021da177e4SLinus Torvalds return SIGBUS; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds SITOREG(val, MIPSInst_RT(ir)); 3051da177e4SLinus Torvalds break; 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds case swc1_op:{ 3093fccc015SRalf Baechle u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] + 3101da177e4SLinus Torvalds MIPSInst_SIMM(ir)); 3111da177e4SLinus Torvalds u32 val; 3121da177e4SLinus Torvalds 313b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(stores); 3141da177e4SLinus Torvalds SIFROMREG(val, MIPSInst_RT(ir)); 3151da177e4SLinus Torvalds if (put_user(val, va)) { 316b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 3171da177e4SLinus Torvalds return SIGBUS; 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds break; 3201da177e4SLinus Torvalds } 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds case cop1_op: 3231da177e4SLinus Torvalds switch (MIPSInst_RS(ir)) { 3241da177e4SLinus Torvalds 3254b724efdSRalf Baechle #if defined(__mips64) 3261da177e4SLinus Torvalds case dmfc_op: 3271da177e4SLinus Torvalds /* copregister fs -> gpr[rt] */ 3281da177e4SLinus Torvalds if (MIPSInst_RT(ir) != 0) { 3291da177e4SLinus Torvalds DIFROMREG(xcp->regs[MIPSInst_RT(ir)], 3301da177e4SLinus Torvalds MIPSInst_RD(ir)); 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds break; 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds case dmtc_op: 3351da177e4SLinus Torvalds /* copregister fs <- rt */ 3361da177e4SLinus Torvalds DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir)); 3371da177e4SLinus Torvalds break; 3381da177e4SLinus Torvalds #endif 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds case mfc_op: 3411da177e4SLinus Torvalds /* copregister rd -> gpr[rt] */ 3421da177e4SLinus Torvalds if (MIPSInst_RT(ir) != 0) { 3431da177e4SLinus Torvalds SIFROMREG(xcp->regs[MIPSInst_RT(ir)], 3441da177e4SLinus Torvalds MIPSInst_RD(ir)); 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds break; 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds case mtc_op: 3491da177e4SLinus Torvalds /* copregister rd <- rt */ 3501da177e4SLinus Torvalds SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir)); 3511da177e4SLinus Torvalds break; 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds case cfc_op:{ 3541da177e4SLinus Torvalds /* cop control register rd -> gpr[rt] */ 3551da177e4SLinus Torvalds u32 value; 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds if (MIPSInst_RD(ir) == FPCREG_CSR) { 3581da177e4SLinus Torvalds value = ctx->fcr31; 3593f135530SShane McDonald value = (value & ~FPU_CSR_RM) | 3603f135530SShane McDonald mips_rm[modeindex(value)]; 3611da177e4SLinus Torvalds #ifdef CSRTRACE 3621da177e4SLinus Torvalds printk("%p gpr[%d]<-csr=%08x\n", 363333d1f67SRalf Baechle (void *) (xcp->cp0_epc), 3641da177e4SLinus Torvalds MIPSInst_RT(ir), value); 3651da177e4SLinus Torvalds #endif 3661da177e4SLinus Torvalds } 3671da177e4SLinus Torvalds else if (MIPSInst_RD(ir) == FPCREG_RID) 3681da177e4SLinus Torvalds value = 0; 3691da177e4SLinus Torvalds else 3701da177e4SLinus Torvalds value = 0; 3711da177e4SLinus Torvalds if (MIPSInst_RT(ir)) 3721da177e4SLinus Torvalds xcp->regs[MIPSInst_RT(ir)] = value; 3731da177e4SLinus Torvalds break; 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds case ctc_op:{ 3771da177e4SLinus Torvalds /* copregister rd <- rt */ 3781da177e4SLinus Torvalds u32 value; 3791da177e4SLinus Torvalds 3801da177e4SLinus Torvalds if (MIPSInst_RT(ir) == 0) 3811da177e4SLinus Torvalds value = 0; 3821da177e4SLinus Torvalds else 3831da177e4SLinus Torvalds value = xcp->regs[MIPSInst_RT(ir)]; 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds /* we only have one writable control reg 3861da177e4SLinus Torvalds */ 3871da177e4SLinus Torvalds if (MIPSInst_RD(ir) == FPCREG_CSR) { 3881da177e4SLinus Torvalds #ifdef CSRTRACE 3891da177e4SLinus Torvalds printk("%p gpr[%d]->csr=%08x\n", 390333d1f67SRalf Baechle (void *) (xcp->cp0_epc), 3911da177e4SLinus Torvalds MIPSInst_RT(ir), value); 3921da177e4SLinus Torvalds #endif 39395e8f634SShane McDonald 39495e8f634SShane McDonald /* 39595e8f634SShane McDonald * Don't write reserved bits, 39695e8f634SShane McDonald * and convert to ieee library modes 39795e8f634SShane McDonald */ 39895e8f634SShane McDonald ctx->fcr31 = (value & 39995e8f634SShane McDonald ~(FPU_CSR_RSVD | FPU_CSR_RM)) | 40095e8f634SShane McDonald ieee_rm[modeindex(value)]; 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) { 4031da177e4SLinus Torvalds return SIGFPE; 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds break; 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds case bc_op:{ 4091da177e4SLinus Torvalds int likely = 0; 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds if (xcp->cp0_cause & CAUSEF_BD) 4121da177e4SLinus Torvalds return SIGILL; 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds #if __mips >= 4 4151da177e4SLinus Torvalds cond = ctx->fcr31 & fpucondbit[MIPSInst_RT(ir) >> 2]; 4161da177e4SLinus Torvalds #else 4171da177e4SLinus Torvalds cond = ctx->fcr31 & FPU_CSR_COND; 4181da177e4SLinus Torvalds #endif 4191da177e4SLinus Torvalds switch (MIPSInst_RT(ir) & 3) { 4201da177e4SLinus Torvalds case bcfl_op: 4211da177e4SLinus Torvalds likely = 1; 4221da177e4SLinus Torvalds case bcf_op: 4231da177e4SLinus Torvalds cond = !cond; 4241da177e4SLinus Torvalds break; 4251da177e4SLinus Torvalds case bctl_op: 4261da177e4SLinus Torvalds likely = 1; 4271da177e4SLinus Torvalds case bct_op: 4281da177e4SLinus Torvalds break; 4291da177e4SLinus Torvalds default: 4301da177e4SLinus Torvalds /* thats an illegal instruction */ 4311da177e4SLinus Torvalds return SIGILL; 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds xcp->cp0_cause |= CAUSEF_BD; 4351da177e4SLinus Torvalds if (cond) { 4361da177e4SLinus Torvalds /* branch taken: emulate dslot 4371da177e4SLinus Torvalds * instruction 4381da177e4SLinus Torvalds */ 4391da177e4SLinus Torvalds xcp->cp0_epc += 4; 440e70dfc10SAtsushi Nemoto contpc = (xcp->cp0_epc + 4411da177e4SLinus Torvalds (MIPSInst_SIMM(ir) << 2)); 4421da177e4SLinus Torvalds 4433fccc015SRalf Baechle if (get_user(ir, 4443fccc015SRalf Baechle (mips_instruction __user *) xcp->cp0_epc)) { 445b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 4461da177e4SLinus Torvalds return SIGBUS; 4471da177e4SLinus Torvalds } 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds switch (MIPSInst_OPCODE(ir)) { 4501da177e4SLinus Torvalds case lwc1_op: 4511da177e4SLinus Torvalds case swc1_op: 4524b724efdSRalf Baechle #if (__mips >= 2 || defined(__mips64)) 4531da177e4SLinus Torvalds case ldc1_op: 4541da177e4SLinus Torvalds case sdc1_op: 4551da177e4SLinus Torvalds #endif 4561da177e4SLinus Torvalds case cop1_op: 4571da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32 4581da177e4SLinus Torvalds case cop1x_op: 4591da177e4SLinus Torvalds #endif 4601da177e4SLinus Torvalds /* its one of ours */ 4611da177e4SLinus Torvalds goto emul; 4621da177e4SLinus Torvalds #if __mips >= 4 4631da177e4SLinus Torvalds case spec_op: 4641da177e4SLinus Torvalds if (MIPSInst_FUNC(ir) == movc_op) 4651da177e4SLinus Torvalds goto emul; 4661da177e4SLinus Torvalds break; 4671da177e4SLinus Torvalds #endif 4681da177e4SLinus Torvalds } 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds /* 4711da177e4SLinus Torvalds * Single step the non-cp1 4721da177e4SLinus Torvalds * instruction in the dslot 4731da177e4SLinus Torvalds */ 474e70dfc10SAtsushi Nemoto return mips_dsemul(xcp, ir, contpc); 4751da177e4SLinus Torvalds } 4761da177e4SLinus Torvalds else { 4771da177e4SLinus Torvalds /* branch not taken */ 4781da177e4SLinus Torvalds if (likely) { 4791da177e4SLinus Torvalds /* 4801da177e4SLinus Torvalds * branch likely nullifies 4811da177e4SLinus Torvalds * dslot if not taken 4821da177e4SLinus Torvalds */ 4831da177e4SLinus Torvalds xcp->cp0_epc += 4; 4841da177e4SLinus Torvalds contpc += 4; 4851da177e4SLinus Torvalds /* 4861da177e4SLinus Torvalds * else continue & execute 4871da177e4SLinus Torvalds * dslot as normal insn 4881da177e4SLinus Torvalds */ 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds break; 4921da177e4SLinus Torvalds } 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds default: 4951da177e4SLinus Torvalds if (!(MIPSInst_RS(ir) & 0x10)) 4961da177e4SLinus Torvalds return SIGILL; 4971da177e4SLinus Torvalds { 4981da177e4SLinus Torvalds int sig; 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds /* a real fpu computation instruction */ 5011da177e4SLinus Torvalds if ((sig = fpu_emu(xcp, ctx, ir))) 5021da177e4SLinus Torvalds return sig; 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds break; 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32 5081da177e4SLinus Torvalds case cop1x_op:{ 5091da177e4SLinus Torvalds int sig; 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds if ((sig = fpux_emu(xcp, ctx, ir))) 5121da177e4SLinus Torvalds return sig; 5131da177e4SLinus Torvalds break; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds #endif 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds #if __mips >= 4 5181da177e4SLinus Torvalds case spec_op: 5191da177e4SLinus Torvalds if (MIPSInst_FUNC(ir) != movc_op) 5201da177e4SLinus Torvalds return SIGILL; 5211da177e4SLinus Torvalds cond = fpucondbit[MIPSInst_RT(ir) >> 2]; 5221da177e4SLinus Torvalds if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0)) 5231da177e4SLinus Torvalds xcp->regs[MIPSInst_RD(ir)] = 5241da177e4SLinus Torvalds xcp->regs[MIPSInst_RS(ir)]; 5251da177e4SLinus Torvalds break; 5261da177e4SLinus Torvalds #endif 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds default: 5291da177e4SLinus Torvalds return SIGILL; 5301da177e4SLinus Torvalds } 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds /* we did it !! */ 533e70dfc10SAtsushi Nemoto xcp->cp0_epc = contpc; 5341da177e4SLinus Torvalds xcp->cp0_cause &= ~CAUSEF_BD; 535333d1f67SRalf Baechle 5361da177e4SLinus Torvalds return 0; 5371da177e4SLinus Torvalds } 5381da177e4SLinus Torvalds 5391da177e4SLinus Torvalds /* 5401da177e4SLinus Torvalds * Conversion table from MIPS compare ops 48-63 5411da177e4SLinus Torvalds * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig); 5421da177e4SLinus Torvalds */ 5431da177e4SLinus Torvalds static const unsigned char cmptab[8] = { 5441da177e4SLinus Torvalds 0, /* cmp_0 (sig) cmp_sf */ 5451da177e4SLinus Torvalds IEEE754_CUN, /* cmp_un (sig) cmp_ngle */ 5461da177e4SLinus Torvalds IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */ 5471da177e4SLinus Torvalds IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */ 5481da177e4SLinus Torvalds IEEE754_CLT, /* cmp_olt (sig) cmp_lt */ 5491da177e4SLinus Torvalds IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */ 5501da177e4SLinus Torvalds IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */ 5511da177e4SLinus Torvalds IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */ 5521da177e4SLinus Torvalds }; 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds /* 5581da177e4SLinus Torvalds * Additional MIPS4 instructions 5591da177e4SLinus Torvalds */ 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds #define DEF3OP(name, p, f1, f2, f3) \ 5621da177e4SLinus Torvalds static ieee754##p fpemu_##p##_##name(ieee754##p r, ieee754##p s, \ 5631da177e4SLinus Torvalds ieee754##p t) \ 5641da177e4SLinus Torvalds { \ 565cd21dfcfSRalf Baechle struct _ieee754_csr ieee754_csr_save; \ 5661da177e4SLinus Torvalds s = f1(s, t); \ 5671da177e4SLinus Torvalds ieee754_csr_save = ieee754_csr; \ 5681da177e4SLinus Torvalds s = f2(s, r); \ 5691da177e4SLinus Torvalds ieee754_csr_save.cx |= ieee754_csr.cx; \ 5701da177e4SLinus Torvalds ieee754_csr_save.sx |= ieee754_csr.sx; \ 5711da177e4SLinus Torvalds s = f3(s); \ 5721da177e4SLinus Torvalds ieee754_csr.cx |= ieee754_csr_save.cx; \ 5731da177e4SLinus Torvalds ieee754_csr.sx |= ieee754_csr_save.sx; \ 5741da177e4SLinus Torvalds return s; \ 5751da177e4SLinus Torvalds } 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds static ieee754dp fpemu_dp_recip(ieee754dp d) 5781da177e4SLinus Torvalds { 5791da177e4SLinus Torvalds return ieee754dp_div(ieee754dp_one(0), d); 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds static ieee754dp fpemu_dp_rsqrt(ieee754dp d) 5831da177e4SLinus Torvalds { 5841da177e4SLinus Torvalds return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d)); 5851da177e4SLinus Torvalds } 5861da177e4SLinus Torvalds 5871da177e4SLinus Torvalds static ieee754sp fpemu_sp_recip(ieee754sp s) 5881da177e4SLinus Torvalds { 5891da177e4SLinus Torvalds return ieee754sp_div(ieee754sp_one(0), s); 5901da177e4SLinus Torvalds } 5911da177e4SLinus Torvalds 5921da177e4SLinus Torvalds static ieee754sp fpemu_sp_rsqrt(ieee754sp s) 5931da177e4SLinus Torvalds { 5941da177e4SLinus Torvalds return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s)); 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, ); 5981da177e4SLinus Torvalds DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, ); 5991da177e4SLinus Torvalds DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg); 6001da177e4SLinus Torvalds DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg); 6011da177e4SLinus Torvalds DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, ); 6021da177e4SLinus Torvalds DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, ); 6031da177e4SLinus Torvalds DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg); 6041da177e4SLinus Torvalds DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg); 6051da177e4SLinus Torvalds 606eae89076SAtsushi Nemoto static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx, 6071da177e4SLinus Torvalds mips_instruction ir) 6081da177e4SLinus Torvalds { 6091da177e4SLinus Torvalds unsigned rcsr = 0; /* resulting csr */ 6101da177e4SLinus Torvalds 611b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(cp1xops); 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds switch (MIPSInst_FMA_FFMT(ir)) { 6141da177e4SLinus Torvalds case s_fmt:{ /* 0 */ 6151da177e4SLinus Torvalds 6161da177e4SLinus Torvalds ieee754sp(*handler) (ieee754sp, ieee754sp, ieee754sp); 6171da177e4SLinus Torvalds ieee754sp fd, fr, fs, ft; 6183fccc015SRalf Baechle u32 __user *va; 6191da177e4SLinus Torvalds u32 val; 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 6221da177e4SLinus Torvalds case lwxc1_op: 6233fccc015SRalf Baechle va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] + 6241da177e4SLinus Torvalds xcp->regs[MIPSInst_FT(ir)]); 6251da177e4SLinus Torvalds 626b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(loads); 6271da177e4SLinus Torvalds if (get_user(val, va)) { 628b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 6291da177e4SLinus Torvalds return SIGBUS; 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds SITOREG(val, MIPSInst_FD(ir)); 6321da177e4SLinus Torvalds break; 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds case swxc1_op: 6353fccc015SRalf Baechle va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] + 6361da177e4SLinus Torvalds xcp->regs[MIPSInst_FT(ir)]); 6371da177e4SLinus Torvalds 638b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(stores); 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds SIFROMREG(val, MIPSInst_FS(ir)); 6411da177e4SLinus Torvalds if (put_user(val, va)) { 642b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 6431da177e4SLinus Torvalds return SIGBUS; 6441da177e4SLinus Torvalds } 6451da177e4SLinus Torvalds break; 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds case madd_s_op: 6481da177e4SLinus Torvalds handler = fpemu_sp_madd; 6491da177e4SLinus Torvalds goto scoptop; 6501da177e4SLinus Torvalds case msub_s_op: 6511da177e4SLinus Torvalds handler = fpemu_sp_msub; 6521da177e4SLinus Torvalds goto scoptop; 6531da177e4SLinus Torvalds case nmadd_s_op: 6541da177e4SLinus Torvalds handler = fpemu_sp_nmadd; 6551da177e4SLinus Torvalds goto scoptop; 6561da177e4SLinus Torvalds case nmsub_s_op: 6571da177e4SLinus Torvalds handler = fpemu_sp_nmsub; 6581da177e4SLinus Torvalds goto scoptop; 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds scoptop: 6611da177e4SLinus Torvalds SPFROMREG(fr, MIPSInst_FR(ir)); 6621da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 6631da177e4SLinus Torvalds SPFROMREG(ft, MIPSInst_FT(ir)); 6641da177e4SLinus Torvalds fd = (*handler) (fr, fs, ft); 6651da177e4SLinus Torvalds SPTOREG(fd, MIPSInst_FD(ir)); 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds copcsr: 6681da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_INEXACT)) 6691da177e4SLinus Torvalds rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S; 6701da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_UNDERFLOW)) 6711da177e4SLinus Torvalds rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S; 6721da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_OVERFLOW)) 6731da177e4SLinus Torvalds rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S; 6741da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) 6751da177e4SLinus Torvalds rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S; 6761da177e4SLinus Torvalds 6771da177e4SLinus Torvalds ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr; 6781da177e4SLinus Torvalds if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) { 6791da177e4SLinus Torvalds /*printk ("SIGFPE: fpu csr = %08x\n", 6801da177e4SLinus Torvalds ctx->fcr31); */ 6811da177e4SLinus Torvalds return SIGFPE; 6821da177e4SLinus Torvalds } 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds break; 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds default: 6871da177e4SLinus Torvalds return SIGILL; 6881da177e4SLinus Torvalds } 6891da177e4SLinus Torvalds break; 6901da177e4SLinus Torvalds } 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds case d_fmt:{ /* 1 */ 6931da177e4SLinus Torvalds ieee754dp(*handler) (ieee754dp, ieee754dp, ieee754dp); 6941da177e4SLinus Torvalds ieee754dp fd, fr, fs, ft; 6953fccc015SRalf Baechle u64 __user *va; 6961da177e4SLinus Torvalds u64 val; 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 6991da177e4SLinus Torvalds case ldxc1_op: 7003fccc015SRalf Baechle va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] + 7011da177e4SLinus Torvalds xcp->regs[MIPSInst_FT(ir)]); 7021da177e4SLinus Torvalds 703b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(loads); 7041da177e4SLinus Torvalds if (get_user(val, va)) { 705b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 7061da177e4SLinus Torvalds return SIGBUS; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds DITOREG(val, MIPSInst_FD(ir)); 7091da177e4SLinus Torvalds break; 7101da177e4SLinus Torvalds 7111da177e4SLinus Torvalds case sdxc1_op: 7123fccc015SRalf Baechle va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] + 7131da177e4SLinus Torvalds xcp->regs[MIPSInst_FT(ir)]); 7141da177e4SLinus Torvalds 715b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(stores); 7161da177e4SLinus Torvalds DIFROMREG(val, MIPSInst_FS(ir)); 7171da177e4SLinus Torvalds if (put_user(val, va)) { 718b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 7191da177e4SLinus Torvalds return SIGBUS; 7201da177e4SLinus Torvalds } 7211da177e4SLinus Torvalds break; 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds case madd_d_op: 7241da177e4SLinus Torvalds handler = fpemu_dp_madd; 7251da177e4SLinus Torvalds goto dcoptop; 7261da177e4SLinus Torvalds case msub_d_op: 7271da177e4SLinus Torvalds handler = fpemu_dp_msub; 7281da177e4SLinus Torvalds goto dcoptop; 7291da177e4SLinus Torvalds case nmadd_d_op: 7301da177e4SLinus Torvalds handler = fpemu_dp_nmadd; 7311da177e4SLinus Torvalds goto dcoptop; 7321da177e4SLinus Torvalds case nmsub_d_op: 7331da177e4SLinus Torvalds handler = fpemu_dp_nmsub; 7341da177e4SLinus Torvalds goto dcoptop; 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds dcoptop: 7371da177e4SLinus Torvalds DPFROMREG(fr, MIPSInst_FR(ir)); 7381da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 7391da177e4SLinus Torvalds DPFROMREG(ft, MIPSInst_FT(ir)); 7401da177e4SLinus Torvalds fd = (*handler) (fr, fs, ft); 7411da177e4SLinus Torvalds DPTOREG(fd, MIPSInst_FD(ir)); 7421da177e4SLinus Torvalds goto copcsr; 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds default: 7451da177e4SLinus Torvalds return SIGILL; 7461da177e4SLinus Torvalds } 7471da177e4SLinus Torvalds break; 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds case 0x7: /* 7 */ 7511da177e4SLinus Torvalds if (MIPSInst_FUNC(ir) != pfetch_op) { 7521da177e4SLinus Torvalds return SIGILL; 7531da177e4SLinus Torvalds } 7541da177e4SLinus Torvalds /* ignore prefx operation */ 7551da177e4SLinus Torvalds break; 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds default: 7581da177e4SLinus Torvalds return SIGILL; 7591da177e4SLinus Torvalds } 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds return 0; 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds #endif 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds 7671da177e4SLinus Torvalds /* 7681da177e4SLinus Torvalds * Emulate a single COP1 arithmetic instruction. 7691da177e4SLinus Torvalds */ 770eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx, 7711da177e4SLinus Torvalds mips_instruction ir) 7721da177e4SLinus Torvalds { 7731da177e4SLinus Torvalds int rfmt; /* resulting format */ 7741da177e4SLinus Torvalds unsigned rcsr = 0; /* resulting csr */ 7751da177e4SLinus Torvalds unsigned cond; 7761da177e4SLinus Torvalds union { 7771da177e4SLinus Torvalds ieee754dp d; 7781da177e4SLinus Torvalds ieee754sp s; 7791da177e4SLinus Torvalds int w; 780766160c2SYoichi Yuasa #ifdef __mips64 7811da177e4SLinus Torvalds s64 l; 7821da177e4SLinus Torvalds #endif 7831da177e4SLinus Torvalds } rv; /* resulting value */ 7841da177e4SLinus Torvalds 785b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(cp1ops); 7861da177e4SLinus Torvalds switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) { 7871da177e4SLinus Torvalds case s_fmt:{ /* 0 */ 7881da177e4SLinus Torvalds union { 7891da177e4SLinus Torvalds ieee754sp(*b) (ieee754sp, ieee754sp); 7901da177e4SLinus Torvalds ieee754sp(*u) (ieee754sp); 7911da177e4SLinus Torvalds } handler; 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 7941da177e4SLinus Torvalds /* binary ops */ 7951da177e4SLinus Torvalds case fadd_op: 7961da177e4SLinus Torvalds handler.b = ieee754sp_add; 7971da177e4SLinus Torvalds goto scopbop; 7981da177e4SLinus Torvalds case fsub_op: 7991da177e4SLinus Torvalds handler.b = ieee754sp_sub; 8001da177e4SLinus Torvalds goto scopbop; 8011da177e4SLinus Torvalds case fmul_op: 8021da177e4SLinus Torvalds handler.b = ieee754sp_mul; 8031da177e4SLinus Torvalds goto scopbop; 8041da177e4SLinus Torvalds case fdiv_op: 8051da177e4SLinus Torvalds handler.b = ieee754sp_div; 8061da177e4SLinus Torvalds goto scopbop; 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds /* unary ops */ 809587cb98fSRalf Baechle #if __mips >= 2 || defined(__mips64) 8101da177e4SLinus Torvalds case fsqrt_op: 8111da177e4SLinus Torvalds handler.u = ieee754sp_sqrt; 8121da177e4SLinus Torvalds goto scopuop; 8131da177e4SLinus Torvalds #endif 8141da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32 8151da177e4SLinus Torvalds case frsqrt_op: 8161da177e4SLinus Torvalds handler.u = fpemu_sp_rsqrt; 8171da177e4SLinus Torvalds goto scopuop; 8181da177e4SLinus Torvalds case frecip_op: 8191da177e4SLinus Torvalds handler.u = fpemu_sp_recip; 8201da177e4SLinus Torvalds goto scopuop; 8211da177e4SLinus Torvalds #endif 8221da177e4SLinus Torvalds #if __mips >= 4 8231da177e4SLinus Torvalds case fmovc_op: 8241da177e4SLinus Torvalds cond = fpucondbit[MIPSInst_FT(ir) >> 2]; 8251da177e4SLinus Torvalds if (((ctx->fcr31 & cond) != 0) != 8261da177e4SLinus Torvalds ((MIPSInst_FT(ir) & 1) != 0)) 8271da177e4SLinus Torvalds return 0; 8281da177e4SLinus Torvalds SPFROMREG(rv.s, MIPSInst_FS(ir)); 8291da177e4SLinus Torvalds break; 8301da177e4SLinus Torvalds case fmovz_op: 8311da177e4SLinus Torvalds if (xcp->regs[MIPSInst_FT(ir)] != 0) 8321da177e4SLinus Torvalds return 0; 8331da177e4SLinus Torvalds SPFROMREG(rv.s, MIPSInst_FS(ir)); 8341da177e4SLinus Torvalds break; 8351da177e4SLinus Torvalds case fmovn_op: 8361da177e4SLinus Torvalds if (xcp->regs[MIPSInst_FT(ir)] == 0) 8371da177e4SLinus Torvalds return 0; 8381da177e4SLinus Torvalds SPFROMREG(rv.s, MIPSInst_FS(ir)); 8391da177e4SLinus Torvalds break; 8401da177e4SLinus Torvalds #endif 8411da177e4SLinus Torvalds case fabs_op: 8421da177e4SLinus Torvalds handler.u = ieee754sp_abs; 8431da177e4SLinus Torvalds goto scopuop; 8441da177e4SLinus Torvalds case fneg_op: 8451da177e4SLinus Torvalds handler.u = ieee754sp_neg; 8461da177e4SLinus Torvalds goto scopuop; 8471da177e4SLinus Torvalds case fmov_op: 8481da177e4SLinus Torvalds /* an easy one */ 8491da177e4SLinus Torvalds SPFROMREG(rv.s, MIPSInst_FS(ir)); 8501da177e4SLinus Torvalds goto copcsr; 8511da177e4SLinus Torvalds 8521da177e4SLinus Torvalds /* binary op on handler */ 8531da177e4SLinus Torvalds scopbop: 8541da177e4SLinus Torvalds { 8551da177e4SLinus Torvalds ieee754sp fs, ft; 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 8581da177e4SLinus Torvalds SPFROMREG(ft, MIPSInst_FT(ir)); 8591da177e4SLinus Torvalds 8601da177e4SLinus Torvalds rv.s = (*handler.b) (fs, ft); 8611da177e4SLinus Torvalds goto copcsr; 8621da177e4SLinus Torvalds } 8631da177e4SLinus Torvalds scopuop: 8641da177e4SLinus Torvalds { 8651da177e4SLinus Torvalds ieee754sp fs; 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 8681da177e4SLinus Torvalds rv.s = (*handler.u) (fs); 8691da177e4SLinus Torvalds goto copcsr; 8701da177e4SLinus Torvalds } 8711da177e4SLinus Torvalds copcsr: 8721da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_INEXACT)) 8731da177e4SLinus Torvalds rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S; 8741da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_UNDERFLOW)) 8751da177e4SLinus Torvalds rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S; 8761da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_OVERFLOW)) 8771da177e4SLinus Torvalds rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S; 8781da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) 8791da177e4SLinus Torvalds rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S; 8801da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) 8811da177e4SLinus Torvalds rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S; 8821da177e4SLinus Torvalds break; 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds /* unary conv ops */ 8851da177e4SLinus Torvalds case fcvts_op: 8861da177e4SLinus Torvalds return SIGILL; /* not defined */ 8871da177e4SLinus Torvalds case fcvtd_op:{ 8881da177e4SLinus Torvalds ieee754sp fs; 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 8911da177e4SLinus Torvalds rv.d = ieee754dp_fsp(fs); 8921da177e4SLinus Torvalds rfmt = d_fmt; 8931da177e4SLinus Torvalds goto copcsr; 8941da177e4SLinus Torvalds } 8951da177e4SLinus Torvalds case fcvtw_op:{ 8961da177e4SLinus Torvalds ieee754sp fs; 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 8991da177e4SLinus Torvalds rv.w = ieee754sp_tint(fs); 9001da177e4SLinus Torvalds rfmt = w_fmt; 9011da177e4SLinus Torvalds goto copcsr; 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds 904587cb98fSRalf Baechle #if __mips >= 2 || defined(__mips64) 9051da177e4SLinus Torvalds case fround_op: 9061da177e4SLinus Torvalds case ftrunc_op: 9071da177e4SLinus Torvalds case fceil_op: 9081da177e4SLinus Torvalds case ffloor_op:{ 9091da177e4SLinus Torvalds unsigned int oldrm = ieee754_csr.rm; 9101da177e4SLinus Torvalds ieee754sp fs; 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 9133f135530SShane McDonald ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))]; 9141da177e4SLinus Torvalds rv.w = ieee754sp_tint(fs); 9151da177e4SLinus Torvalds ieee754_csr.rm = oldrm; 9161da177e4SLinus Torvalds rfmt = w_fmt; 9171da177e4SLinus Torvalds goto copcsr; 9181da177e4SLinus Torvalds } 9191da177e4SLinus Torvalds #endif /* __mips >= 2 */ 9201da177e4SLinus Torvalds 9214b724efdSRalf Baechle #if defined(__mips64) 9221da177e4SLinus Torvalds case fcvtl_op:{ 9231da177e4SLinus Torvalds ieee754sp fs; 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 9261da177e4SLinus Torvalds rv.l = ieee754sp_tlong(fs); 9271da177e4SLinus Torvalds rfmt = l_fmt; 9281da177e4SLinus Torvalds goto copcsr; 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds 9311da177e4SLinus Torvalds case froundl_op: 9321da177e4SLinus Torvalds case ftruncl_op: 9331da177e4SLinus Torvalds case fceill_op: 9341da177e4SLinus Torvalds case ffloorl_op:{ 9351da177e4SLinus Torvalds unsigned int oldrm = ieee754_csr.rm; 9361da177e4SLinus Torvalds ieee754sp fs; 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 9393f135530SShane McDonald ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))]; 9401da177e4SLinus Torvalds rv.l = ieee754sp_tlong(fs); 9411da177e4SLinus Torvalds ieee754_csr.rm = oldrm; 9421da177e4SLinus Torvalds rfmt = l_fmt; 9431da177e4SLinus Torvalds goto copcsr; 9441da177e4SLinus Torvalds } 9454b724efdSRalf Baechle #endif /* defined(__mips64) */ 9461da177e4SLinus Torvalds 9471da177e4SLinus Torvalds default: 9481da177e4SLinus Torvalds if (MIPSInst_FUNC(ir) >= fcmp_op) { 9491da177e4SLinus Torvalds unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op; 9501da177e4SLinus Torvalds ieee754sp fs, ft; 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 9531da177e4SLinus Torvalds SPFROMREG(ft, MIPSInst_FT(ir)); 9541da177e4SLinus Torvalds rv.w = ieee754sp_cmp(fs, ft, 9551da177e4SLinus Torvalds cmptab[cmpop & 0x7], cmpop & 0x8); 9561da177e4SLinus Torvalds rfmt = -1; 9571da177e4SLinus Torvalds if ((cmpop & 0x8) && ieee754_cxtest 9581da177e4SLinus Torvalds (IEEE754_INVALID_OPERATION)) 9591da177e4SLinus Torvalds rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S; 9601da177e4SLinus Torvalds else 9611da177e4SLinus Torvalds goto copcsr; 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds } 9641da177e4SLinus Torvalds else { 9651da177e4SLinus Torvalds return SIGILL; 9661da177e4SLinus Torvalds } 9671da177e4SLinus Torvalds break; 9681da177e4SLinus Torvalds } 9691da177e4SLinus Torvalds break; 9701da177e4SLinus Torvalds } 9711da177e4SLinus Torvalds 9721da177e4SLinus Torvalds case d_fmt:{ 9731da177e4SLinus Torvalds union { 9741da177e4SLinus Torvalds ieee754dp(*b) (ieee754dp, ieee754dp); 9751da177e4SLinus Torvalds ieee754dp(*u) (ieee754dp); 9761da177e4SLinus Torvalds } handler; 9771da177e4SLinus Torvalds 9781da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 9791da177e4SLinus Torvalds /* binary ops */ 9801da177e4SLinus Torvalds case fadd_op: 9811da177e4SLinus Torvalds handler.b = ieee754dp_add; 9821da177e4SLinus Torvalds goto dcopbop; 9831da177e4SLinus Torvalds case fsub_op: 9841da177e4SLinus Torvalds handler.b = ieee754dp_sub; 9851da177e4SLinus Torvalds goto dcopbop; 9861da177e4SLinus Torvalds case fmul_op: 9871da177e4SLinus Torvalds handler.b = ieee754dp_mul; 9881da177e4SLinus Torvalds goto dcopbop; 9891da177e4SLinus Torvalds case fdiv_op: 9901da177e4SLinus Torvalds handler.b = ieee754dp_div; 9911da177e4SLinus Torvalds goto dcopbop; 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds /* unary ops */ 994587cb98fSRalf Baechle #if __mips >= 2 || defined(__mips64) 9951da177e4SLinus Torvalds case fsqrt_op: 9961da177e4SLinus Torvalds handler.u = ieee754dp_sqrt; 9971da177e4SLinus Torvalds goto dcopuop; 9981da177e4SLinus Torvalds #endif 9991da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32 10001da177e4SLinus Torvalds case frsqrt_op: 10011da177e4SLinus Torvalds handler.u = fpemu_dp_rsqrt; 10021da177e4SLinus Torvalds goto dcopuop; 10031da177e4SLinus Torvalds case frecip_op: 10041da177e4SLinus Torvalds handler.u = fpemu_dp_recip; 10051da177e4SLinus Torvalds goto dcopuop; 10061da177e4SLinus Torvalds #endif 10071da177e4SLinus Torvalds #if __mips >= 4 10081da177e4SLinus Torvalds case fmovc_op: 10091da177e4SLinus Torvalds cond = fpucondbit[MIPSInst_FT(ir) >> 2]; 10101da177e4SLinus Torvalds if (((ctx->fcr31 & cond) != 0) != 10111da177e4SLinus Torvalds ((MIPSInst_FT(ir) & 1) != 0)) 10121da177e4SLinus Torvalds return 0; 10131da177e4SLinus Torvalds DPFROMREG(rv.d, MIPSInst_FS(ir)); 10141da177e4SLinus Torvalds break; 10151da177e4SLinus Torvalds case fmovz_op: 10161da177e4SLinus Torvalds if (xcp->regs[MIPSInst_FT(ir)] != 0) 10171da177e4SLinus Torvalds return 0; 10181da177e4SLinus Torvalds DPFROMREG(rv.d, MIPSInst_FS(ir)); 10191da177e4SLinus Torvalds break; 10201da177e4SLinus Torvalds case fmovn_op: 10211da177e4SLinus Torvalds if (xcp->regs[MIPSInst_FT(ir)] == 0) 10221da177e4SLinus Torvalds return 0; 10231da177e4SLinus Torvalds DPFROMREG(rv.d, MIPSInst_FS(ir)); 10241da177e4SLinus Torvalds break; 10251da177e4SLinus Torvalds #endif 10261da177e4SLinus Torvalds case fabs_op: 10271da177e4SLinus Torvalds handler.u = ieee754dp_abs; 10281da177e4SLinus Torvalds goto dcopuop; 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds case fneg_op: 10311da177e4SLinus Torvalds handler.u = ieee754dp_neg; 10321da177e4SLinus Torvalds goto dcopuop; 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds case fmov_op: 10351da177e4SLinus Torvalds /* an easy one */ 10361da177e4SLinus Torvalds DPFROMREG(rv.d, MIPSInst_FS(ir)); 10371da177e4SLinus Torvalds goto copcsr; 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds /* binary op on handler */ 10401da177e4SLinus Torvalds dcopbop:{ 10411da177e4SLinus Torvalds ieee754dp fs, ft; 10421da177e4SLinus Torvalds 10431da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 10441da177e4SLinus Torvalds DPFROMREG(ft, MIPSInst_FT(ir)); 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds rv.d = (*handler.b) (fs, ft); 10471da177e4SLinus Torvalds goto copcsr; 10481da177e4SLinus Torvalds } 10491da177e4SLinus Torvalds dcopuop:{ 10501da177e4SLinus Torvalds ieee754dp fs; 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 10531da177e4SLinus Torvalds rv.d = (*handler.u) (fs); 10541da177e4SLinus Torvalds goto copcsr; 10551da177e4SLinus Torvalds } 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds /* unary conv ops */ 10581da177e4SLinus Torvalds case fcvts_op:{ 10591da177e4SLinus Torvalds ieee754dp fs; 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 10621da177e4SLinus Torvalds rv.s = ieee754sp_fdp(fs); 10631da177e4SLinus Torvalds rfmt = s_fmt; 10641da177e4SLinus Torvalds goto copcsr; 10651da177e4SLinus Torvalds } 10661da177e4SLinus Torvalds case fcvtd_op: 10671da177e4SLinus Torvalds return SIGILL; /* not defined */ 10681da177e4SLinus Torvalds 10691da177e4SLinus Torvalds case fcvtw_op:{ 10701da177e4SLinus Torvalds ieee754dp fs; 10711da177e4SLinus Torvalds 10721da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 10731da177e4SLinus Torvalds rv.w = ieee754dp_tint(fs); /* wrong */ 10741da177e4SLinus Torvalds rfmt = w_fmt; 10751da177e4SLinus Torvalds goto copcsr; 10761da177e4SLinus Torvalds } 10771da177e4SLinus Torvalds 1078587cb98fSRalf Baechle #if __mips >= 2 || defined(__mips64) 10791da177e4SLinus Torvalds case fround_op: 10801da177e4SLinus Torvalds case ftrunc_op: 10811da177e4SLinus Torvalds case fceil_op: 10821da177e4SLinus Torvalds case ffloor_op:{ 10831da177e4SLinus Torvalds unsigned int oldrm = ieee754_csr.rm; 10841da177e4SLinus Torvalds ieee754dp fs; 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 10873f135530SShane McDonald ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))]; 10881da177e4SLinus Torvalds rv.w = ieee754dp_tint(fs); 10891da177e4SLinus Torvalds ieee754_csr.rm = oldrm; 10901da177e4SLinus Torvalds rfmt = w_fmt; 10911da177e4SLinus Torvalds goto copcsr; 10921da177e4SLinus Torvalds } 10931da177e4SLinus Torvalds #endif 10941da177e4SLinus Torvalds 10954b724efdSRalf Baechle #if defined(__mips64) 10961da177e4SLinus Torvalds case fcvtl_op:{ 10971da177e4SLinus Torvalds ieee754dp fs; 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 11001da177e4SLinus Torvalds rv.l = ieee754dp_tlong(fs); 11011da177e4SLinus Torvalds rfmt = l_fmt; 11021da177e4SLinus Torvalds goto copcsr; 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds case froundl_op: 11061da177e4SLinus Torvalds case ftruncl_op: 11071da177e4SLinus Torvalds case fceill_op: 11081da177e4SLinus Torvalds case ffloorl_op:{ 11091da177e4SLinus Torvalds unsigned int oldrm = ieee754_csr.rm; 11101da177e4SLinus Torvalds ieee754dp fs; 11111da177e4SLinus Torvalds 11121da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 11133f135530SShane McDonald ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))]; 11141da177e4SLinus Torvalds rv.l = ieee754dp_tlong(fs); 11151da177e4SLinus Torvalds ieee754_csr.rm = oldrm; 11161da177e4SLinus Torvalds rfmt = l_fmt; 11171da177e4SLinus Torvalds goto copcsr; 11181da177e4SLinus Torvalds } 11194b724efdSRalf Baechle #endif /* __mips >= 3 */ 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds default: 11221da177e4SLinus Torvalds if (MIPSInst_FUNC(ir) >= fcmp_op) { 11231da177e4SLinus Torvalds unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op; 11241da177e4SLinus Torvalds ieee754dp fs, ft; 11251da177e4SLinus Torvalds 11261da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 11271da177e4SLinus Torvalds DPFROMREG(ft, MIPSInst_FT(ir)); 11281da177e4SLinus Torvalds rv.w = ieee754dp_cmp(fs, ft, 11291da177e4SLinus Torvalds cmptab[cmpop & 0x7], cmpop & 0x8); 11301da177e4SLinus Torvalds rfmt = -1; 11311da177e4SLinus Torvalds if ((cmpop & 0x8) 11321da177e4SLinus Torvalds && 11331da177e4SLinus Torvalds ieee754_cxtest 11341da177e4SLinus Torvalds (IEEE754_INVALID_OPERATION)) 11351da177e4SLinus Torvalds rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S; 11361da177e4SLinus Torvalds else 11371da177e4SLinus Torvalds goto copcsr; 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds else { 11411da177e4SLinus Torvalds return SIGILL; 11421da177e4SLinus Torvalds } 11431da177e4SLinus Torvalds break; 11441da177e4SLinus Torvalds } 11451da177e4SLinus Torvalds break; 11461da177e4SLinus Torvalds } 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds case w_fmt:{ 11491da177e4SLinus Torvalds ieee754sp fs; 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 11521da177e4SLinus Torvalds case fcvts_op: 11531da177e4SLinus Torvalds /* convert word to single precision real */ 11541da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 11551da177e4SLinus Torvalds rv.s = ieee754sp_fint(fs.bits); 11561da177e4SLinus Torvalds rfmt = s_fmt; 11571da177e4SLinus Torvalds goto copcsr; 11581da177e4SLinus Torvalds case fcvtd_op: 11591da177e4SLinus Torvalds /* convert word to double precision real */ 11601da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 11611da177e4SLinus Torvalds rv.d = ieee754dp_fint(fs.bits); 11621da177e4SLinus Torvalds rfmt = d_fmt; 11631da177e4SLinus Torvalds goto copcsr; 11641da177e4SLinus Torvalds default: 11651da177e4SLinus Torvalds return SIGILL; 11661da177e4SLinus Torvalds } 11671da177e4SLinus Torvalds break; 11681da177e4SLinus Torvalds } 11691da177e4SLinus Torvalds 11704b724efdSRalf Baechle #if defined(__mips64) 11711da177e4SLinus Torvalds case l_fmt:{ 11721da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 11731da177e4SLinus Torvalds case fcvts_op: 11741da177e4SLinus Torvalds /* convert long to single precision real */ 11751da177e4SLinus Torvalds rv.s = ieee754sp_flong(ctx->fpr[MIPSInst_FS(ir)]); 11761da177e4SLinus Torvalds rfmt = s_fmt; 11771da177e4SLinus Torvalds goto copcsr; 11781da177e4SLinus Torvalds case fcvtd_op: 11791da177e4SLinus Torvalds /* convert long to double precision real */ 11801da177e4SLinus Torvalds rv.d = ieee754dp_flong(ctx->fpr[MIPSInst_FS(ir)]); 11811da177e4SLinus Torvalds rfmt = d_fmt; 11821da177e4SLinus Torvalds goto copcsr; 11831da177e4SLinus Torvalds default: 11841da177e4SLinus Torvalds return SIGILL; 11851da177e4SLinus Torvalds } 11861da177e4SLinus Torvalds break; 11871da177e4SLinus Torvalds } 11881da177e4SLinus Torvalds #endif 11891da177e4SLinus Torvalds 11901da177e4SLinus Torvalds default: 11911da177e4SLinus Torvalds return SIGILL; 11921da177e4SLinus Torvalds } 11931da177e4SLinus Torvalds 11941da177e4SLinus Torvalds /* 11951da177e4SLinus Torvalds * Update the fpu CSR register for this operation. 11961da177e4SLinus Torvalds * If an exception is required, generate a tidy SIGFPE exception, 11971da177e4SLinus Torvalds * without updating the result register. 11981da177e4SLinus Torvalds * Note: cause exception bits do not accumulate, they are rewritten 11991da177e4SLinus Torvalds * for each op; only the flag/sticky bits accumulate. 12001da177e4SLinus Torvalds */ 12011da177e4SLinus Torvalds ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr; 12021da177e4SLinus Torvalds if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) { 12031da177e4SLinus Torvalds /*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */ 12041da177e4SLinus Torvalds return SIGFPE; 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds /* 12081da177e4SLinus Torvalds * Now we can safely write the result back to the register file. 12091da177e4SLinus Torvalds */ 12101da177e4SLinus Torvalds switch (rfmt) { 12111da177e4SLinus Torvalds case -1:{ 12121da177e4SLinus Torvalds #if __mips >= 4 12131da177e4SLinus Torvalds cond = fpucondbit[MIPSInst_FD(ir) >> 2]; 12141da177e4SLinus Torvalds #else 12151da177e4SLinus Torvalds cond = FPU_CSR_COND; 12161da177e4SLinus Torvalds #endif 12171da177e4SLinus Torvalds if (rv.w) 12181da177e4SLinus Torvalds ctx->fcr31 |= cond; 12191da177e4SLinus Torvalds else 12201da177e4SLinus Torvalds ctx->fcr31 &= ~cond; 12211da177e4SLinus Torvalds break; 12221da177e4SLinus Torvalds } 12231da177e4SLinus Torvalds case d_fmt: 12241da177e4SLinus Torvalds DPTOREG(rv.d, MIPSInst_FD(ir)); 12251da177e4SLinus Torvalds break; 12261da177e4SLinus Torvalds case s_fmt: 12271da177e4SLinus Torvalds SPTOREG(rv.s, MIPSInst_FD(ir)); 12281da177e4SLinus Torvalds break; 12291da177e4SLinus Torvalds case w_fmt: 12301da177e4SLinus Torvalds SITOREG(rv.w, MIPSInst_FD(ir)); 12311da177e4SLinus Torvalds break; 12324b724efdSRalf Baechle #if defined(__mips64) 12331da177e4SLinus Torvalds case l_fmt: 12341da177e4SLinus Torvalds DITOREG(rv.l, MIPSInst_FD(ir)); 12351da177e4SLinus Torvalds break; 12361da177e4SLinus Torvalds #endif 12371da177e4SLinus Torvalds default: 12381da177e4SLinus Torvalds return SIGILL; 12391da177e4SLinus Torvalds } 12401da177e4SLinus Torvalds 12411da177e4SLinus Torvalds return 0; 12421da177e4SLinus Torvalds } 12431da177e4SLinus Torvalds 1244e04582b7SAtsushi Nemoto int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx, 1245e04582b7SAtsushi Nemoto int has_fpu) 12461da177e4SLinus Torvalds { 1247333d1f67SRalf Baechle unsigned long oldepc, prevepc; 12481da177e4SLinus Torvalds mips_instruction insn; 12491da177e4SLinus Torvalds int sig = 0; 12501da177e4SLinus Torvalds 12511da177e4SLinus Torvalds oldepc = xcp->cp0_epc; 12521da177e4SLinus Torvalds do { 12531da177e4SLinus Torvalds prevepc = xcp->cp0_epc; 12541da177e4SLinus Torvalds 12553fccc015SRalf Baechle if (get_user(insn, (mips_instruction __user *) xcp->cp0_epc)) { 1256b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 12571da177e4SLinus Torvalds return SIGBUS; 12581da177e4SLinus Torvalds } 12591da177e4SLinus Torvalds if (insn == 0) 12601da177e4SLinus Torvalds xcp->cp0_epc += 4; /* skip nops */ 12611da177e4SLinus Torvalds else { 1262cd21dfcfSRalf Baechle /* 1263cd21dfcfSRalf Baechle * The 'ieee754_csr' is an alias of 1264cd21dfcfSRalf Baechle * ctx->fcr31. No need to copy ctx->fcr31 to 1265cd21dfcfSRalf Baechle * ieee754_csr. But ieee754_csr.rm is ieee 1266cd21dfcfSRalf Baechle * library modes. (not mips rounding mode) 1267cd21dfcfSRalf Baechle */ 1268cd21dfcfSRalf Baechle /* convert to ieee library modes */ 1269cd21dfcfSRalf Baechle ieee754_csr.rm = ieee_rm[ieee754_csr.rm]; 12701da177e4SLinus Torvalds sig = cop1Emulate(xcp, ctx); 1271cd21dfcfSRalf Baechle /* revert to mips rounding mode */ 1272cd21dfcfSRalf Baechle ieee754_csr.rm = mips_rm[ieee754_csr.rm]; 12731da177e4SLinus Torvalds } 12741da177e4SLinus Torvalds 1275e04582b7SAtsushi Nemoto if (has_fpu) 12761da177e4SLinus Torvalds break; 12771da177e4SLinus Torvalds if (sig) 12781da177e4SLinus Torvalds break; 12791da177e4SLinus Torvalds 12801da177e4SLinus Torvalds cond_resched(); 12811da177e4SLinus Torvalds } while (xcp->cp0_epc > prevepc); 12821da177e4SLinus Torvalds 12831da177e4SLinus Torvalds /* SIGILL indicates a non-fpu instruction */ 12841da177e4SLinus Torvalds if (sig == SIGILL && xcp->cp0_epc != oldepc) 12851da177e4SLinus Torvalds /* but if epc has advanced, then ignore it */ 12861da177e4SLinus Torvalds sig = 0; 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds return sig; 12891da177e4SLinus Torvalds } 129083fd38caSAtsushi Nemoto 129183fd38caSAtsushi Nemoto #ifdef CONFIG_DEBUG_FS 1292b6ee75edSDavid Daney 1293b6ee75edSDavid Daney static int fpuemu_stat_get(void *data, u64 *val) 1294b6ee75edSDavid Daney { 1295b6ee75edSDavid Daney int cpu; 1296b6ee75edSDavid Daney unsigned long sum = 0; 1297b6ee75edSDavid Daney for_each_online_cpu(cpu) { 1298b6ee75edSDavid Daney struct mips_fpu_emulator_stats *ps; 1299b6ee75edSDavid Daney local_t *pv; 1300b6ee75edSDavid Daney ps = &per_cpu(fpuemustats, cpu); 1301b6ee75edSDavid Daney pv = (void *)ps + (unsigned long)data; 1302b6ee75edSDavid Daney sum += local_read(pv); 1303b6ee75edSDavid Daney } 1304b6ee75edSDavid Daney *val = sum; 1305b6ee75edSDavid Daney return 0; 1306b6ee75edSDavid Daney } 1307b6ee75edSDavid Daney DEFINE_SIMPLE_ATTRIBUTE(fops_fpuemu_stat, fpuemu_stat_get, NULL, "%llu\n"); 1308b6ee75edSDavid Daney 130983fd38caSAtsushi Nemoto extern struct dentry *mips_debugfs_dir; 131083fd38caSAtsushi Nemoto static int __init debugfs_fpuemu(void) 131183fd38caSAtsushi Nemoto { 131283fd38caSAtsushi Nemoto struct dentry *d, *dir; 131383fd38caSAtsushi Nemoto 131483fd38caSAtsushi Nemoto if (!mips_debugfs_dir) 131583fd38caSAtsushi Nemoto return -ENODEV; 131683fd38caSAtsushi Nemoto dir = debugfs_create_dir("fpuemustats", mips_debugfs_dir); 1317ecab1f44SZhaolei if (!dir) 1318ecab1f44SZhaolei return -ENOMEM; 1319b6ee75edSDavid Daney 1320b6ee75edSDavid Daney #define FPU_STAT_CREATE(M) \ 1321b6ee75edSDavid Daney do { \ 1322b6ee75edSDavid Daney d = debugfs_create_file(#M , S_IRUGO, dir, \ 1323b6ee75edSDavid Daney (void *)offsetof(struct mips_fpu_emulator_stats, M), \ 1324b6ee75edSDavid Daney &fops_fpuemu_stat); \ 1325b6ee75edSDavid Daney if (!d) \ 1326b6ee75edSDavid Daney return -ENOMEM; \ 1327b6ee75edSDavid Daney } while (0) 1328b6ee75edSDavid Daney 1329b6ee75edSDavid Daney FPU_STAT_CREATE(emulated); 1330b6ee75edSDavid Daney FPU_STAT_CREATE(loads); 1331b6ee75edSDavid Daney FPU_STAT_CREATE(stores); 1332b6ee75edSDavid Daney FPU_STAT_CREATE(cp1ops); 1333b6ee75edSDavid Daney FPU_STAT_CREATE(cp1xops); 1334b6ee75edSDavid Daney FPU_STAT_CREATE(errors); 1335b6ee75edSDavid Daney 133683fd38caSAtsushi Nemoto return 0; 133783fd38caSAtsushi Nemoto } 133883fd38caSAtsushi Nemoto __initcall(debugfs_fpuemu); 133983fd38caSAtsushi Nemoto #endif 1340