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> 397f788d2dSDeng-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 *, 67515b029dSDavid Daney struct mips_fpu_struct *, mips_instruction, void *__user *); 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 211515b029dSDavid Daney static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx, 212515b029dSDavid Daney void *__user *fault_addr) 2131da177e4SLinus Torvalds { 2141da177e4SLinus Torvalds mips_instruction ir; 215e70dfc10SAtsushi Nemoto unsigned long emulpc, contpc; 2161da177e4SLinus Torvalds unsigned int cond; 2171da177e4SLinus Torvalds 218515b029dSDavid Daney if (!access_ok(VERIFY_READ, xcp->cp0_epc, sizeof(mips_instruction))) { 219b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 220515b029dSDavid Daney *fault_addr = (mips_instruction __user *)xcp->cp0_epc; 2211da177e4SLinus Torvalds return SIGBUS; 2221da177e4SLinus Torvalds } 223515b029dSDavid Daney if (__get_user(ir, (mips_instruction __user *) xcp->cp0_epc)) { 224515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 225515b029dSDavid Daney *fault_addr = (mips_instruction __user *)xcp->cp0_epc; 226515b029dSDavid Daney return SIGSEGV; 227515b029dSDavid Daney } 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds /* XXX NEC Vr54xx bug workaround */ 2301da177e4SLinus Torvalds if ((xcp->cp0_cause & CAUSEF_BD) && !isBranchInstr(&ir)) 2311da177e4SLinus Torvalds xcp->cp0_cause &= ~CAUSEF_BD; 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds if (xcp->cp0_cause & CAUSEF_BD) { 2341da177e4SLinus Torvalds /* 2351da177e4SLinus Torvalds * The instruction to be emulated is in a branch delay slot 2361da177e4SLinus Torvalds * which means that we have to emulate the branch instruction 2371da177e4SLinus Torvalds * BEFORE we do the cop1 instruction. 2381da177e4SLinus Torvalds * 2391da177e4SLinus Torvalds * This branch could be a COP1 branch, but in that case we 2401da177e4SLinus Torvalds * would have had a trap for that instruction, and would not 2411da177e4SLinus Torvalds * come through this route. 2421da177e4SLinus Torvalds * 2431da177e4SLinus Torvalds * Linux MIPS branch emulator operates on context, updating the 2441da177e4SLinus Torvalds * cp0_epc. 2451da177e4SLinus Torvalds */ 246e70dfc10SAtsushi Nemoto emulpc = xcp->cp0_epc + 4; /* Snapshot emulation target */ 2471da177e4SLinus Torvalds 248*d8d4e3aeSManeesh Soni if (__compute_return_epc(xcp) < 0) { 2491da177e4SLinus Torvalds #ifdef CP1DBG 2501da177e4SLinus Torvalds printk("failed to emulate branch at %p\n", 251333d1f67SRalf Baechle (void *) (xcp->cp0_epc)); 2521da177e4SLinus Torvalds #endif 2531da177e4SLinus Torvalds return SIGILL; 2541da177e4SLinus Torvalds } 255515b029dSDavid Daney if (!access_ok(VERIFY_READ, emulpc, sizeof(mips_instruction))) { 256b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 257515b029dSDavid Daney *fault_addr = (mips_instruction __user *)emulpc; 2581da177e4SLinus Torvalds return SIGBUS; 2591da177e4SLinus Torvalds } 260515b029dSDavid Daney if (__get_user(ir, (mips_instruction __user *) emulpc)) { 261515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 262515b029dSDavid Daney *fault_addr = (mips_instruction __user *)emulpc; 263515b029dSDavid Daney return SIGSEGV; 264515b029dSDavid Daney } 2651da177e4SLinus Torvalds /* __compute_return_epc() will have updated cp0_epc */ 266e70dfc10SAtsushi Nemoto contpc = xcp->cp0_epc; 2671da177e4SLinus Torvalds /* In order not to confuse ptrace() et al, tweak context */ 268e70dfc10SAtsushi Nemoto xcp->cp0_epc = emulpc - 4; 269333d1f67SRalf Baechle } else { 270e70dfc10SAtsushi Nemoto emulpc = xcp->cp0_epc; 271e70dfc10SAtsushi Nemoto contpc = xcp->cp0_epc + 4; 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds emul: 275a8b0ca17SPeter Zijlstra perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0); 276b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(emulated); 2771da177e4SLinus Torvalds switch (MIPSInst_OPCODE(ir)) { 2781da177e4SLinus Torvalds case ldc1_op:{ 2793fccc015SRalf Baechle u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] + 2801da177e4SLinus Torvalds MIPSInst_SIMM(ir)); 2811da177e4SLinus Torvalds u64 val; 2821da177e4SLinus Torvalds 283b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(loads); 284515b029dSDavid Daney 285515b029dSDavid Daney if (!access_ok(VERIFY_READ, va, sizeof(u64))) { 286b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 287515b029dSDavid Daney *fault_addr = va; 2881da177e4SLinus Torvalds return SIGBUS; 2891da177e4SLinus Torvalds } 290515b029dSDavid Daney if (__get_user(val, va)) { 291515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 292515b029dSDavid Daney *fault_addr = va; 293515b029dSDavid Daney return SIGSEGV; 294515b029dSDavid Daney } 2951da177e4SLinus Torvalds DITOREG(val, MIPSInst_RT(ir)); 2961da177e4SLinus Torvalds break; 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds case sdc1_op:{ 3003fccc015SRalf Baechle u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] + 3011da177e4SLinus Torvalds MIPSInst_SIMM(ir)); 3021da177e4SLinus Torvalds u64 val; 3031da177e4SLinus Torvalds 304b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(stores); 3051da177e4SLinus Torvalds DIFROMREG(val, MIPSInst_RT(ir)); 306515b029dSDavid Daney if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) { 307b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 308515b029dSDavid Daney *fault_addr = va; 3091da177e4SLinus Torvalds return SIGBUS; 3101da177e4SLinus Torvalds } 311515b029dSDavid Daney if (__put_user(val, va)) { 312515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 313515b029dSDavid Daney *fault_addr = va; 314515b029dSDavid Daney return SIGSEGV; 315515b029dSDavid Daney } 3161da177e4SLinus Torvalds break; 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds case lwc1_op:{ 3203fccc015SRalf Baechle u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] + 3211da177e4SLinus Torvalds MIPSInst_SIMM(ir)); 3221da177e4SLinus Torvalds u32 val; 3231da177e4SLinus Torvalds 324b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(loads); 325515b029dSDavid Daney if (!access_ok(VERIFY_READ, va, sizeof(u32))) { 326b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 327515b029dSDavid Daney *fault_addr = va; 3281da177e4SLinus Torvalds return SIGBUS; 3291da177e4SLinus Torvalds } 330515b029dSDavid Daney if (__get_user(val, va)) { 331515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 332515b029dSDavid Daney *fault_addr = va; 333515b029dSDavid Daney return SIGSEGV; 334515b029dSDavid Daney } 3351da177e4SLinus Torvalds SITOREG(val, MIPSInst_RT(ir)); 3361da177e4SLinus Torvalds break; 3371da177e4SLinus Torvalds } 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds case swc1_op:{ 3403fccc015SRalf Baechle u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] + 3411da177e4SLinus Torvalds MIPSInst_SIMM(ir)); 3421da177e4SLinus Torvalds u32 val; 3431da177e4SLinus Torvalds 344b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(stores); 3451da177e4SLinus Torvalds SIFROMREG(val, MIPSInst_RT(ir)); 346515b029dSDavid Daney if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) { 347b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 348515b029dSDavid Daney *fault_addr = va; 3491da177e4SLinus Torvalds return SIGBUS; 3501da177e4SLinus Torvalds } 351515b029dSDavid Daney if (__put_user(val, va)) { 352515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 353515b029dSDavid Daney *fault_addr = va; 354515b029dSDavid Daney return SIGSEGV; 355515b029dSDavid Daney } 3561da177e4SLinus Torvalds break; 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds case cop1_op: 3601da177e4SLinus Torvalds switch (MIPSInst_RS(ir)) { 3611da177e4SLinus Torvalds 3624b724efdSRalf Baechle #if defined(__mips64) 3631da177e4SLinus Torvalds case dmfc_op: 3641da177e4SLinus Torvalds /* copregister fs -> gpr[rt] */ 3651da177e4SLinus Torvalds if (MIPSInst_RT(ir) != 0) { 3661da177e4SLinus Torvalds DIFROMREG(xcp->regs[MIPSInst_RT(ir)], 3671da177e4SLinus Torvalds MIPSInst_RD(ir)); 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds break; 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds case dmtc_op: 3721da177e4SLinus Torvalds /* copregister fs <- rt */ 3731da177e4SLinus Torvalds DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir)); 3741da177e4SLinus Torvalds break; 3751da177e4SLinus Torvalds #endif 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds case mfc_op: 3781da177e4SLinus Torvalds /* copregister rd -> gpr[rt] */ 3791da177e4SLinus Torvalds if (MIPSInst_RT(ir) != 0) { 3801da177e4SLinus Torvalds SIFROMREG(xcp->regs[MIPSInst_RT(ir)], 3811da177e4SLinus Torvalds MIPSInst_RD(ir)); 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds break; 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds case mtc_op: 3861da177e4SLinus Torvalds /* copregister rd <- rt */ 3871da177e4SLinus Torvalds SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir)); 3881da177e4SLinus Torvalds break; 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds case cfc_op:{ 3911da177e4SLinus Torvalds /* cop control register rd -> gpr[rt] */ 3921da177e4SLinus Torvalds u32 value; 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds if (MIPSInst_RD(ir) == FPCREG_CSR) { 3951da177e4SLinus Torvalds value = ctx->fcr31; 3963f135530SShane McDonald value = (value & ~FPU_CSR_RM) | 3973f135530SShane McDonald mips_rm[modeindex(value)]; 3981da177e4SLinus Torvalds #ifdef CSRTRACE 3991da177e4SLinus Torvalds printk("%p gpr[%d]<-csr=%08x\n", 400333d1f67SRalf Baechle (void *) (xcp->cp0_epc), 4011da177e4SLinus Torvalds MIPSInst_RT(ir), value); 4021da177e4SLinus Torvalds #endif 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds else if (MIPSInst_RD(ir) == FPCREG_RID) 4051da177e4SLinus Torvalds value = 0; 4061da177e4SLinus Torvalds else 4071da177e4SLinus Torvalds value = 0; 4081da177e4SLinus Torvalds if (MIPSInst_RT(ir)) 4091da177e4SLinus Torvalds xcp->regs[MIPSInst_RT(ir)] = value; 4101da177e4SLinus Torvalds break; 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds case ctc_op:{ 4141da177e4SLinus Torvalds /* copregister rd <- rt */ 4151da177e4SLinus Torvalds u32 value; 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds if (MIPSInst_RT(ir) == 0) 4181da177e4SLinus Torvalds value = 0; 4191da177e4SLinus Torvalds else 4201da177e4SLinus Torvalds value = xcp->regs[MIPSInst_RT(ir)]; 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds /* we only have one writable control reg 4231da177e4SLinus Torvalds */ 4241da177e4SLinus Torvalds if (MIPSInst_RD(ir) == FPCREG_CSR) { 4251da177e4SLinus Torvalds #ifdef CSRTRACE 4261da177e4SLinus Torvalds printk("%p gpr[%d]->csr=%08x\n", 427333d1f67SRalf Baechle (void *) (xcp->cp0_epc), 4281da177e4SLinus Torvalds MIPSInst_RT(ir), value); 4291da177e4SLinus Torvalds #endif 43095e8f634SShane McDonald 43195e8f634SShane McDonald /* 43295e8f634SShane McDonald * Don't write reserved bits, 43395e8f634SShane McDonald * and convert to ieee library modes 43495e8f634SShane McDonald */ 43595e8f634SShane McDonald ctx->fcr31 = (value & 43695e8f634SShane McDonald ~(FPU_CSR_RSVD | FPU_CSR_RM)) | 43795e8f634SShane McDonald ieee_rm[modeindex(value)]; 4381da177e4SLinus Torvalds } 4391da177e4SLinus Torvalds if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) { 4401da177e4SLinus Torvalds return SIGFPE; 4411da177e4SLinus Torvalds } 4421da177e4SLinus Torvalds break; 4431da177e4SLinus Torvalds } 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds case bc_op:{ 4461da177e4SLinus Torvalds int likely = 0; 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds if (xcp->cp0_cause & CAUSEF_BD) 4491da177e4SLinus Torvalds return SIGILL; 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds #if __mips >= 4 4521da177e4SLinus Torvalds cond = ctx->fcr31 & fpucondbit[MIPSInst_RT(ir) >> 2]; 4531da177e4SLinus Torvalds #else 4541da177e4SLinus Torvalds cond = ctx->fcr31 & FPU_CSR_COND; 4551da177e4SLinus Torvalds #endif 4561da177e4SLinus Torvalds switch (MIPSInst_RT(ir) & 3) { 4571da177e4SLinus Torvalds case bcfl_op: 4581da177e4SLinus Torvalds likely = 1; 4591da177e4SLinus Torvalds case bcf_op: 4601da177e4SLinus Torvalds cond = !cond; 4611da177e4SLinus Torvalds break; 4621da177e4SLinus Torvalds case bctl_op: 4631da177e4SLinus Torvalds likely = 1; 4641da177e4SLinus Torvalds case bct_op: 4651da177e4SLinus Torvalds break; 4661da177e4SLinus Torvalds default: 4671da177e4SLinus Torvalds /* thats an illegal instruction */ 4681da177e4SLinus Torvalds return SIGILL; 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds xcp->cp0_cause |= CAUSEF_BD; 4721da177e4SLinus Torvalds if (cond) { 4731da177e4SLinus Torvalds /* branch taken: emulate dslot 4741da177e4SLinus Torvalds * instruction 4751da177e4SLinus Torvalds */ 4761da177e4SLinus Torvalds xcp->cp0_epc += 4; 477e70dfc10SAtsushi Nemoto contpc = (xcp->cp0_epc + 4781da177e4SLinus Torvalds (MIPSInst_SIMM(ir) << 2)); 4791da177e4SLinus Torvalds 480515b029dSDavid Daney if (!access_ok(VERIFY_READ, xcp->cp0_epc, 481515b029dSDavid Daney sizeof(mips_instruction))) { 482515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 483515b029dSDavid Daney *fault_addr = (mips_instruction __user *)xcp->cp0_epc; 484515b029dSDavid Daney return SIGBUS; 485515b029dSDavid Daney } 486515b029dSDavid Daney if (__get_user(ir, 4873fccc015SRalf Baechle (mips_instruction __user *) xcp->cp0_epc)) { 488b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 489515b029dSDavid Daney *fault_addr = (mips_instruction __user *)xcp->cp0_epc; 490515b029dSDavid Daney return SIGSEGV; 4911da177e4SLinus Torvalds } 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds switch (MIPSInst_OPCODE(ir)) { 4941da177e4SLinus Torvalds case lwc1_op: 4951da177e4SLinus Torvalds case swc1_op: 4964b724efdSRalf Baechle #if (__mips >= 2 || defined(__mips64)) 4971da177e4SLinus Torvalds case ldc1_op: 4981da177e4SLinus Torvalds case sdc1_op: 4991da177e4SLinus Torvalds #endif 5001da177e4SLinus Torvalds case cop1_op: 5011da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32 5021da177e4SLinus Torvalds case cop1x_op: 5031da177e4SLinus Torvalds #endif 5041da177e4SLinus Torvalds /* its one of ours */ 5051da177e4SLinus Torvalds goto emul; 5061da177e4SLinus Torvalds #if __mips >= 4 5071da177e4SLinus Torvalds case spec_op: 5081da177e4SLinus Torvalds if (MIPSInst_FUNC(ir) == movc_op) 5091da177e4SLinus Torvalds goto emul; 5101da177e4SLinus Torvalds break; 5111da177e4SLinus Torvalds #endif 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds /* 5151da177e4SLinus Torvalds * Single step the non-cp1 5161da177e4SLinus Torvalds * instruction in the dslot 5171da177e4SLinus Torvalds */ 518e70dfc10SAtsushi Nemoto return mips_dsemul(xcp, ir, contpc); 5191da177e4SLinus Torvalds } 5201da177e4SLinus Torvalds else { 5211da177e4SLinus Torvalds /* branch not taken */ 5221da177e4SLinus Torvalds if (likely) { 5231da177e4SLinus Torvalds /* 5241da177e4SLinus Torvalds * branch likely nullifies 5251da177e4SLinus Torvalds * dslot if not taken 5261da177e4SLinus Torvalds */ 5271da177e4SLinus Torvalds xcp->cp0_epc += 4; 5281da177e4SLinus Torvalds contpc += 4; 5291da177e4SLinus Torvalds /* 5301da177e4SLinus Torvalds * else continue & execute 5311da177e4SLinus Torvalds * dslot as normal insn 5321da177e4SLinus Torvalds */ 5331da177e4SLinus Torvalds } 5341da177e4SLinus Torvalds } 5351da177e4SLinus Torvalds break; 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds default: 5391da177e4SLinus Torvalds if (!(MIPSInst_RS(ir) & 0x10)) 5401da177e4SLinus Torvalds return SIGILL; 5411da177e4SLinus Torvalds { 5421da177e4SLinus Torvalds int sig; 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds /* a real fpu computation instruction */ 5451da177e4SLinus Torvalds if ((sig = fpu_emu(xcp, ctx, ir))) 5461da177e4SLinus Torvalds return sig; 5471da177e4SLinus Torvalds } 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds break; 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32 5521da177e4SLinus Torvalds case cop1x_op:{ 553515b029dSDavid Daney int sig = fpux_emu(xcp, ctx, ir, fault_addr); 554515b029dSDavid Daney if (sig) 5551da177e4SLinus Torvalds return sig; 5561da177e4SLinus Torvalds break; 5571da177e4SLinus Torvalds } 5581da177e4SLinus Torvalds #endif 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds #if __mips >= 4 5611da177e4SLinus Torvalds case spec_op: 5621da177e4SLinus Torvalds if (MIPSInst_FUNC(ir) != movc_op) 5631da177e4SLinus Torvalds return SIGILL; 5641da177e4SLinus Torvalds cond = fpucondbit[MIPSInst_RT(ir) >> 2]; 5651da177e4SLinus Torvalds if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0)) 5661da177e4SLinus Torvalds xcp->regs[MIPSInst_RD(ir)] = 5671da177e4SLinus Torvalds xcp->regs[MIPSInst_RS(ir)]; 5681da177e4SLinus Torvalds break; 5691da177e4SLinus Torvalds #endif 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds default: 5721da177e4SLinus Torvalds return SIGILL; 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds /* we did it !! */ 576e70dfc10SAtsushi Nemoto xcp->cp0_epc = contpc; 5771da177e4SLinus Torvalds xcp->cp0_cause &= ~CAUSEF_BD; 578333d1f67SRalf Baechle 5791da177e4SLinus Torvalds return 0; 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds /* 5831da177e4SLinus Torvalds * Conversion table from MIPS compare ops 48-63 5841da177e4SLinus Torvalds * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig); 5851da177e4SLinus Torvalds */ 5861da177e4SLinus Torvalds static const unsigned char cmptab[8] = { 5871da177e4SLinus Torvalds 0, /* cmp_0 (sig) cmp_sf */ 5881da177e4SLinus Torvalds IEEE754_CUN, /* cmp_un (sig) cmp_ngle */ 5891da177e4SLinus Torvalds IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */ 5901da177e4SLinus Torvalds IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */ 5911da177e4SLinus Torvalds IEEE754_CLT, /* cmp_olt (sig) cmp_lt */ 5921da177e4SLinus Torvalds IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */ 5931da177e4SLinus Torvalds IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */ 5941da177e4SLinus Torvalds IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */ 5951da177e4SLinus Torvalds }; 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds /* 6011da177e4SLinus Torvalds * Additional MIPS4 instructions 6021da177e4SLinus Torvalds */ 6031da177e4SLinus Torvalds 6041da177e4SLinus Torvalds #define DEF3OP(name, p, f1, f2, f3) \ 6051da177e4SLinus Torvalds static ieee754##p fpemu_##p##_##name(ieee754##p r, ieee754##p s, \ 6061da177e4SLinus Torvalds ieee754##p t) \ 6071da177e4SLinus Torvalds { \ 608cd21dfcfSRalf Baechle struct _ieee754_csr ieee754_csr_save; \ 6091da177e4SLinus Torvalds s = f1(s, t); \ 6101da177e4SLinus Torvalds ieee754_csr_save = ieee754_csr; \ 6111da177e4SLinus Torvalds s = f2(s, r); \ 6121da177e4SLinus Torvalds ieee754_csr_save.cx |= ieee754_csr.cx; \ 6131da177e4SLinus Torvalds ieee754_csr_save.sx |= ieee754_csr.sx; \ 6141da177e4SLinus Torvalds s = f3(s); \ 6151da177e4SLinus Torvalds ieee754_csr.cx |= ieee754_csr_save.cx; \ 6161da177e4SLinus Torvalds ieee754_csr.sx |= ieee754_csr_save.sx; \ 6171da177e4SLinus Torvalds return s; \ 6181da177e4SLinus Torvalds } 6191da177e4SLinus Torvalds 6201da177e4SLinus Torvalds static ieee754dp fpemu_dp_recip(ieee754dp d) 6211da177e4SLinus Torvalds { 6221da177e4SLinus Torvalds return ieee754dp_div(ieee754dp_one(0), d); 6231da177e4SLinus Torvalds } 6241da177e4SLinus Torvalds 6251da177e4SLinus Torvalds static ieee754dp fpemu_dp_rsqrt(ieee754dp d) 6261da177e4SLinus Torvalds { 6271da177e4SLinus Torvalds return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d)); 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds static ieee754sp fpemu_sp_recip(ieee754sp s) 6311da177e4SLinus Torvalds { 6321da177e4SLinus Torvalds return ieee754sp_div(ieee754sp_one(0), s); 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds static ieee754sp fpemu_sp_rsqrt(ieee754sp s) 6361da177e4SLinus Torvalds { 6371da177e4SLinus Torvalds return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s)); 6381da177e4SLinus Torvalds } 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, ); 6411da177e4SLinus Torvalds DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, ); 6421da177e4SLinus Torvalds DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg); 6431da177e4SLinus Torvalds DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg); 6441da177e4SLinus Torvalds DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, ); 6451da177e4SLinus Torvalds DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, ); 6461da177e4SLinus Torvalds DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg); 6471da177e4SLinus Torvalds DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg); 6481da177e4SLinus Torvalds 649eae89076SAtsushi Nemoto static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx, 650515b029dSDavid Daney mips_instruction ir, void *__user *fault_addr) 6511da177e4SLinus Torvalds { 6521da177e4SLinus Torvalds unsigned rcsr = 0; /* resulting csr */ 6531da177e4SLinus Torvalds 654b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(cp1xops); 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds switch (MIPSInst_FMA_FFMT(ir)) { 6571da177e4SLinus Torvalds case s_fmt:{ /* 0 */ 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds ieee754sp(*handler) (ieee754sp, ieee754sp, ieee754sp); 6601da177e4SLinus Torvalds ieee754sp fd, fr, fs, ft; 6613fccc015SRalf Baechle u32 __user *va; 6621da177e4SLinus Torvalds u32 val; 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 6651da177e4SLinus Torvalds case lwxc1_op: 6663fccc015SRalf Baechle va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] + 6671da177e4SLinus Torvalds xcp->regs[MIPSInst_FT(ir)]); 6681da177e4SLinus Torvalds 669b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(loads); 670515b029dSDavid Daney if (!access_ok(VERIFY_READ, va, sizeof(u32))) { 671b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 672515b029dSDavid Daney *fault_addr = va; 6731da177e4SLinus Torvalds return SIGBUS; 6741da177e4SLinus Torvalds } 675515b029dSDavid Daney if (__get_user(val, va)) { 676515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 677515b029dSDavid Daney *fault_addr = va; 678515b029dSDavid Daney return SIGSEGV; 679515b029dSDavid Daney } 6801da177e4SLinus Torvalds SITOREG(val, MIPSInst_FD(ir)); 6811da177e4SLinus Torvalds break; 6821da177e4SLinus Torvalds 6831da177e4SLinus Torvalds case swxc1_op: 6843fccc015SRalf Baechle va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] + 6851da177e4SLinus Torvalds xcp->regs[MIPSInst_FT(ir)]); 6861da177e4SLinus Torvalds 687b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(stores); 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds SIFROMREG(val, MIPSInst_FS(ir)); 690515b029dSDavid Daney if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) { 691515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 692515b029dSDavid Daney *fault_addr = va; 693515b029dSDavid Daney return SIGBUS; 694515b029dSDavid Daney } 6951da177e4SLinus Torvalds if (put_user(val, va)) { 696b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 697515b029dSDavid Daney *fault_addr = va; 698515b029dSDavid Daney return SIGSEGV; 6991da177e4SLinus Torvalds } 7001da177e4SLinus Torvalds break; 7011da177e4SLinus Torvalds 7021da177e4SLinus Torvalds case madd_s_op: 7031da177e4SLinus Torvalds handler = fpemu_sp_madd; 7041da177e4SLinus Torvalds goto scoptop; 7051da177e4SLinus Torvalds case msub_s_op: 7061da177e4SLinus Torvalds handler = fpemu_sp_msub; 7071da177e4SLinus Torvalds goto scoptop; 7081da177e4SLinus Torvalds case nmadd_s_op: 7091da177e4SLinus Torvalds handler = fpemu_sp_nmadd; 7101da177e4SLinus Torvalds goto scoptop; 7111da177e4SLinus Torvalds case nmsub_s_op: 7121da177e4SLinus Torvalds handler = fpemu_sp_nmsub; 7131da177e4SLinus Torvalds goto scoptop; 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds scoptop: 7161da177e4SLinus Torvalds SPFROMREG(fr, MIPSInst_FR(ir)); 7171da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 7181da177e4SLinus Torvalds SPFROMREG(ft, MIPSInst_FT(ir)); 7191da177e4SLinus Torvalds fd = (*handler) (fr, fs, ft); 7201da177e4SLinus Torvalds SPTOREG(fd, MIPSInst_FD(ir)); 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds copcsr: 7231da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_INEXACT)) 7241da177e4SLinus Torvalds rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S; 7251da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_UNDERFLOW)) 7261da177e4SLinus Torvalds rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S; 7271da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_OVERFLOW)) 7281da177e4SLinus Torvalds rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S; 7291da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) 7301da177e4SLinus Torvalds rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S; 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr; 7331da177e4SLinus Torvalds if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) { 7341da177e4SLinus Torvalds /*printk ("SIGFPE: fpu csr = %08x\n", 7351da177e4SLinus Torvalds ctx->fcr31); */ 7361da177e4SLinus Torvalds return SIGFPE; 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds 7391da177e4SLinus Torvalds break; 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds default: 7421da177e4SLinus Torvalds return SIGILL; 7431da177e4SLinus Torvalds } 7441da177e4SLinus Torvalds break; 7451da177e4SLinus Torvalds } 7461da177e4SLinus Torvalds 7471da177e4SLinus Torvalds case d_fmt:{ /* 1 */ 7481da177e4SLinus Torvalds ieee754dp(*handler) (ieee754dp, ieee754dp, ieee754dp); 7491da177e4SLinus Torvalds ieee754dp fd, fr, fs, ft; 7503fccc015SRalf Baechle u64 __user *va; 7511da177e4SLinus Torvalds u64 val; 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 7541da177e4SLinus Torvalds case ldxc1_op: 7553fccc015SRalf Baechle va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] + 7561da177e4SLinus Torvalds xcp->regs[MIPSInst_FT(ir)]); 7571da177e4SLinus Torvalds 758b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(loads); 759515b029dSDavid Daney if (!access_ok(VERIFY_READ, va, sizeof(u64))) { 760b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 761515b029dSDavid Daney *fault_addr = va; 7621da177e4SLinus Torvalds return SIGBUS; 7631da177e4SLinus Torvalds } 764515b029dSDavid Daney if (__get_user(val, va)) { 765515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 766515b029dSDavid Daney *fault_addr = va; 767515b029dSDavid Daney return SIGSEGV; 768515b029dSDavid Daney } 7691da177e4SLinus Torvalds DITOREG(val, MIPSInst_FD(ir)); 7701da177e4SLinus Torvalds break; 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds case sdxc1_op: 7733fccc015SRalf Baechle va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] + 7741da177e4SLinus Torvalds xcp->regs[MIPSInst_FT(ir)]); 7751da177e4SLinus Torvalds 776b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(stores); 7771da177e4SLinus Torvalds DIFROMREG(val, MIPSInst_FS(ir)); 778515b029dSDavid Daney if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) { 779b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 780515b029dSDavid Daney *fault_addr = va; 7811da177e4SLinus Torvalds return SIGBUS; 7821da177e4SLinus Torvalds } 783515b029dSDavid Daney if (__put_user(val, va)) { 784515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 785515b029dSDavid Daney *fault_addr = va; 786515b029dSDavid Daney return SIGSEGV; 787515b029dSDavid Daney } 7881da177e4SLinus Torvalds break; 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds case madd_d_op: 7911da177e4SLinus Torvalds handler = fpemu_dp_madd; 7921da177e4SLinus Torvalds goto dcoptop; 7931da177e4SLinus Torvalds case msub_d_op: 7941da177e4SLinus Torvalds handler = fpemu_dp_msub; 7951da177e4SLinus Torvalds goto dcoptop; 7961da177e4SLinus Torvalds case nmadd_d_op: 7971da177e4SLinus Torvalds handler = fpemu_dp_nmadd; 7981da177e4SLinus Torvalds goto dcoptop; 7991da177e4SLinus Torvalds case nmsub_d_op: 8001da177e4SLinus Torvalds handler = fpemu_dp_nmsub; 8011da177e4SLinus Torvalds goto dcoptop; 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds dcoptop: 8041da177e4SLinus Torvalds DPFROMREG(fr, MIPSInst_FR(ir)); 8051da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 8061da177e4SLinus Torvalds DPFROMREG(ft, MIPSInst_FT(ir)); 8071da177e4SLinus Torvalds fd = (*handler) (fr, fs, ft); 8081da177e4SLinus Torvalds DPTOREG(fd, MIPSInst_FD(ir)); 8091da177e4SLinus Torvalds goto copcsr; 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds default: 8121da177e4SLinus Torvalds return SIGILL; 8131da177e4SLinus Torvalds } 8141da177e4SLinus Torvalds break; 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds case 0x7: /* 7 */ 8181da177e4SLinus Torvalds if (MIPSInst_FUNC(ir) != pfetch_op) { 8191da177e4SLinus Torvalds return SIGILL; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds /* ignore prefx operation */ 8221da177e4SLinus Torvalds break; 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds default: 8251da177e4SLinus Torvalds return SIGILL; 8261da177e4SLinus Torvalds } 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds return 0; 8291da177e4SLinus Torvalds } 8301da177e4SLinus Torvalds #endif 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds /* 8351da177e4SLinus Torvalds * Emulate a single COP1 arithmetic instruction. 8361da177e4SLinus Torvalds */ 837eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx, 8381da177e4SLinus Torvalds mips_instruction ir) 8391da177e4SLinus Torvalds { 8401da177e4SLinus Torvalds int rfmt; /* resulting format */ 8411da177e4SLinus Torvalds unsigned rcsr = 0; /* resulting csr */ 8421da177e4SLinus Torvalds unsigned cond; 8431da177e4SLinus Torvalds union { 8441da177e4SLinus Torvalds ieee754dp d; 8451da177e4SLinus Torvalds ieee754sp s; 8461da177e4SLinus Torvalds int w; 847766160c2SYoichi Yuasa #ifdef __mips64 8481da177e4SLinus Torvalds s64 l; 8491da177e4SLinus Torvalds #endif 8501da177e4SLinus Torvalds } rv; /* resulting value */ 8511da177e4SLinus Torvalds 852b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(cp1ops); 8531da177e4SLinus Torvalds switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) { 8541da177e4SLinus Torvalds case s_fmt:{ /* 0 */ 8551da177e4SLinus Torvalds union { 8561da177e4SLinus Torvalds ieee754sp(*b) (ieee754sp, ieee754sp); 8571da177e4SLinus Torvalds ieee754sp(*u) (ieee754sp); 8581da177e4SLinus Torvalds } handler; 8591da177e4SLinus Torvalds 8601da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 8611da177e4SLinus Torvalds /* binary ops */ 8621da177e4SLinus Torvalds case fadd_op: 8631da177e4SLinus Torvalds handler.b = ieee754sp_add; 8641da177e4SLinus Torvalds goto scopbop; 8651da177e4SLinus Torvalds case fsub_op: 8661da177e4SLinus Torvalds handler.b = ieee754sp_sub; 8671da177e4SLinus Torvalds goto scopbop; 8681da177e4SLinus Torvalds case fmul_op: 8691da177e4SLinus Torvalds handler.b = ieee754sp_mul; 8701da177e4SLinus Torvalds goto scopbop; 8711da177e4SLinus Torvalds case fdiv_op: 8721da177e4SLinus Torvalds handler.b = ieee754sp_div; 8731da177e4SLinus Torvalds goto scopbop; 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds /* unary ops */ 876587cb98fSRalf Baechle #if __mips >= 2 || defined(__mips64) 8771da177e4SLinus Torvalds case fsqrt_op: 8781da177e4SLinus Torvalds handler.u = ieee754sp_sqrt; 8791da177e4SLinus Torvalds goto scopuop; 8801da177e4SLinus Torvalds #endif 8811da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32 8821da177e4SLinus Torvalds case frsqrt_op: 8831da177e4SLinus Torvalds handler.u = fpemu_sp_rsqrt; 8841da177e4SLinus Torvalds goto scopuop; 8851da177e4SLinus Torvalds case frecip_op: 8861da177e4SLinus Torvalds handler.u = fpemu_sp_recip; 8871da177e4SLinus Torvalds goto scopuop; 8881da177e4SLinus Torvalds #endif 8891da177e4SLinus Torvalds #if __mips >= 4 8901da177e4SLinus Torvalds case fmovc_op: 8911da177e4SLinus Torvalds cond = fpucondbit[MIPSInst_FT(ir) >> 2]; 8921da177e4SLinus Torvalds if (((ctx->fcr31 & cond) != 0) != 8931da177e4SLinus Torvalds ((MIPSInst_FT(ir) & 1) != 0)) 8941da177e4SLinus Torvalds return 0; 8951da177e4SLinus Torvalds SPFROMREG(rv.s, MIPSInst_FS(ir)); 8961da177e4SLinus Torvalds break; 8971da177e4SLinus Torvalds case fmovz_op: 8981da177e4SLinus Torvalds if (xcp->regs[MIPSInst_FT(ir)] != 0) 8991da177e4SLinus Torvalds return 0; 9001da177e4SLinus Torvalds SPFROMREG(rv.s, MIPSInst_FS(ir)); 9011da177e4SLinus Torvalds break; 9021da177e4SLinus Torvalds case fmovn_op: 9031da177e4SLinus Torvalds if (xcp->regs[MIPSInst_FT(ir)] == 0) 9041da177e4SLinus Torvalds return 0; 9051da177e4SLinus Torvalds SPFROMREG(rv.s, MIPSInst_FS(ir)); 9061da177e4SLinus Torvalds break; 9071da177e4SLinus Torvalds #endif 9081da177e4SLinus Torvalds case fabs_op: 9091da177e4SLinus Torvalds handler.u = ieee754sp_abs; 9101da177e4SLinus Torvalds goto scopuop; 9111da177e4SLinus Torvalds case fneg_op: 9121da177e4SLinus Torvalds handler.u = ieee754sp_neg; 9131da177e4SLinus Torvalds goto scopuop; 9141da177e4SLinus Torvalds case fmov_op: 9151da177e4SLinus Torvalds /* an easy one */ 9161da177e4SLinus Torvalds SPFROMREG(rv.s, MIPSInst_FS(ir)); 9171da177e4SLinus Torvalds goto copcsr; 9181da177e4SLinus Torvalds 9191da177e4SLinus Torvalds /* binary op on handler */ 9201da177e4SLinus Torvalds scopbop: 9211da177e4SLinus Torvalds { 9221da177e4SLinus Torvalds ieee754sp fs, ft; 9231da177e4SLinus Torvalds 9241da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 9251da177e4SLinus Torvalds SPFROMREG(ft, MIPSInst_FT(ir)); 9261da177e4SLinus Torvalds 9271da177e4SLinus Torvalds rv.s = (*handler.b) (fs, ft); 9281da177e4SLinus Torvalds goto copcsr; 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds scopuop: 9311da177e4SLinus Torvalds { 9321da177e4SLinus Torvalds ieee754sp fs; 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 9351da177e4SLinus Torvalds rv.s = (*handler.u) (fs); 9361da177e4SLinus Torvalds goto copcsr; 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds copcsr: 9391da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_INEXACT)) 9401da177e4SLinus Torvalds rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S; 9411da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_UNDERFLOW)) 9421da177e4SLinus Torvalds rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S; 9431da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_OVERFLOW)) 9441da177e4SLinus Torvalds rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S; 9451da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) 9461da177e4SLinus Torvalds rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S; 9471da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) 9481da177e4SLinus Torvalds rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S; 9491da177e4SLinus Torvalds break; 9501da177e4SLinus Torvalds 9511da177e4SLinus Torvalds /* unary conv ops */ 9521da177e4SLinus Torvalds case fcvts_op: 9531da177e4SLinus Torvalds return SIGILL; /* not defined */ 9541da177e4SLinus Torvalds case fcvtd_op:{ 9551da177e4SLinus Torvalds ieee754sp fs; 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 9581da177e4SLinus Torvalds rv.d = ieee754dp_fsp(fs); 9591da177e4SLinus Torvalds rfmt = d_fmt; 9601da177e4SLinus Torvalds goto copcsr; 9611da177e4SLinus Torvalds } 9621da177e4SLinus Torvalds case fcvtw_op:{ 9631da177e4SLinus Torvalds ieee754sp fs; 9641da177e4SLinus Torvalds 9651da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 9661da177e4SLinus Torvalds rv.w = ieee754sp_tint(fs); 9671da177e4SLinus Torvalds rfmt = w_fmt; 9681da177e4SLinus Torvalds goto copcsr; 9691da177e4SLinus Torvalds } 9701da177e4SLinus Torvalds 971587cb98fSRalf Baechle #if __mips >= 2 || defined(__mips64) 9721da177e4SLinus Torvalds case fround_op: 9731da177e4SLinus Torvalds case ftrunc_op: 9741da177e4SLinus Torvalds case fceil_op: 9751da177e4SLinus Torvalds case ffloor_op:{ 9761da177e4SLinus Torvalds unsigned int oldrm = ieee754_csr.rm; 9771da177e4SLinus Torvalds ieee754sp fs; 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 9803f135530SShane McDonald ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))]; 9811da177e4SLinus Torvalds rv.w = ieee754sp_tint(fs); 9821da177e4SLinus Torvalds ieee754_csr.rm = oldrm; 9831da177e4SLinus Torvalds rfmt = w_fmt; 9841da177e4SLinus Torvalds goto copcsr; 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds #endif /* __mips >= 2 */ 9871da177e4SLinus Torvalds 9884b724efdSRalf Baechle #if defined(__mips64) 9891da177e4SLinus Torvalds case fcvtl_op:{ 9901da177e4SLinus Torvalds ieee754sp fs; 9911da177e4SLinus Torvalds 9921da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 9931da177e4SLinus Torvalds rv.l = ieee754sp_tlong(fs); 9941da177e4SLinus Torvalds rfmt = l_fmt; 9951da177e4SLinus Torvalds goto copcsr; 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds case froundl_op: 9991da177e4SLinus Torvalds case ftruncl_op: 10001da177e4SLinus Torvalds case fceill_op: 10011da177e4SLinus Torvalds case ffloorl_op:{ 10021da177e4SLinus Torvalds unsigned int oldrm = ieee754_csr.rm; 10031da177e4SLinus Torvalds ieee754sp fs; 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 10063f135530SShane McDonald ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))]; 10071da177e4SLinus Torvalds rv.l = ieee754sp_tlong(fs); 10081da177e4SLinus Torvalds ieee754_csr.rm = oldrm; 10091da177e4SLinus Torvalds rfmt = l_fmt; 10101da177e4SLinus Torvalds goto copcsr; 10111da177e4SLinus Torvalds } 10124b724efdSRalf Baechle #endif /* defined(__mips64) */ 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds default: 10151da177e4SLinus Torvalds if (MIPSInst_FUNC(ir) >= fcmp_op) { 10161da177e4SLinus Torvalds unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op; 10171da177e4SLinus Torvalds ieee754sp fs, ft; 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 10201da177e4SLinus Torvalds SPFROMREG(ft, MIPSInst_FT(ir)); 10211da177e4SLinus Torvalds rv.w = ieee754sp_cmp(fs, ft, 10221da177e4SLinus Torvalds cmptab[cmpop & 0x7], cmpop & 0x8); 10231da177e4SLinus Torvalds rfmt = -1; 10241da177e4SLinus Torvalds if ((cmpop & 0x8) && ieee754_cxtest 10251da177e4SLinus Torvalds (IEEE754_INVALID_OPERATION)) 10261da177e4SLinus Torvalds rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S; 10271da177e4SLinus Torvalds else 10281da177e4SLinus Torvalds goto copcsr; 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds } 10311da177e4SLinus Torvalds else { 10321da177e4SLinus Torvalds return SIGILL; 10331da177e4SLinus Torvalds } 10341da177e4SLinus Torvalds break; 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds break; 10371da177e4SLinus Torvalds } 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds case d_fmt:{ 10401da177e4SLinus Torvalds union { 10411da177e4SLinus Torvalds ieee754dp(*b) (ieee754dp, ieee754dp); 10421da177e4SLinus Torvalds ieee754dp(*u) (ieee754dp); 10431da177e4SLinus Torvalds } handler; 10441da177e4SLinus Torvalds 10451da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 10461da177e4SLinus Torvalds /* binary ops */ 10471da177e4SLinus Torvalds case fadd_op: 10481da177e4SLinus Torvalds handler.b = ieee754dp_add; 10491da177e4SLinus Torvalds goto dcopbop; 10501da177e4SLinus Torvalds case fsub_op: 10511da177e4SLinus Torvalds handler.b = ieee754dp_sub; 10521da177e4SLinus Torvalds goto dcopbop; 10531da177e4SLinus Torvalds case fmul_op: 10541da177e4SLinus Torvalds handler.b = ieee754dp_mul; 10551da177e4SLinus Torvalds goto dcopbop; 10561da177e4SLinus Torvalds case fdiv_op: 10571da177e4SLinus Torvalds handler.b = ieee754dp_div; 10581da177e4SLinus Torvalds goto dcopbop; 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds /* unary ops */ 1061587cb98fSRalf Baechle #if __mips >= 2 || defined(__mips64) 10621da177e4SLinus Torvalds case fsqrt_op: 10631da177e4SLinus Torvalds handler.u = ieee754dp_sqrt; 10641da177e4SLinus Torvalds goto dcopuop; 10651da177e4SLinus Torvalds #endif 10661da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32 10671da177e4SLinus Torvalds case frsqrt_op: 10681da177e4SLinus Torvalds handler.u = fpemu_dp_rsqrt; 10691da177e4SLinus Torvalds goto dcopuop; 10701da177e4SLinus Torvalds case frecip_op: 10711da177e4SLinus Torvalds handler.u = fpemu_dp_recip; 10721da177e4SLinus Torvalds goto dcopuop; 10731da177e4SLinus Torvalds #endif 10741da177e4SLinus Torvalds #if __mips >= 4 10751da177e4SLinus Torvalds case fmovc_op: 10761da177e4SLinus Torvalds cond = fpucondbit[MIPSInst_FT(ir) >> 2]; 10771da177e4SLinus Torvalds if (((ctx->fcr31 & cond) != 0) != 10781da177e4SLinus Torvalds ((MIPSInst_FT(ir) & 1) != 0)) 10791da177e4SLinus Torvalds return 0; 10801da177e4SLinus Torvalds DPFROMREG(rv.d, MIPSInst_FS(ir)); 10811da177e4SLinus Torvalds break; 10821da177e4SLinus Torvalds case fmovz_op: 10831da177e4SLinus Torvalds if (xcp->regs[MIPSInst_FT(ir)] != 0) 10841da177e4SLinus Torvalds return 0; 10851da177e4SLinus Torvalds DPFROMREG(rv.d, MIPSInst_FS(ir)); 10861da177e4SLinus Torvalds break; 10871da177e4SLinus Torvalds case fmovn_op: 10881da177e4SLinus Torvalds if (xcp->regs[MIPSInst_FT(ir)] == 0) 10891da177e4SLinus Torvalds return 0; 10901da177e4SLinus Torvalds DPFROMREG(rv.d, MIPSInst_FS(ir)); 10911da177e4SLinus Torvalds break; 10921da177e4SLinus Torvalds #endif 10931da177e4SLinus Torvalds case fabs_op: 10941da177e4SLinus Torvalds handler.u = ieee754dp_abs; 10951da177e4SLinus Torvalds goto dcopuop; 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds case fneg_op: 10981da177e4SLinus Torvalds handler.u = ieee754dp_neg; 10991da177e4SLinus Torvalds goto dcopuop; 11001da177e4SLinus Torvalds 11011da177e4SLinus Torvalds case fmov_op: 11021da177e4SLinus Torvalds /* an easy one */ 11031da177e4SLinus Torvalds DPFROMREG(rv.d, MIPSInst_FS(ir)); 11041da177e4SLinus Torvalds goto copcsr; 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds /* binary op on handler */ 11071da177e4SLinus Torvalds dcopbop:{ 11081da177e4SLinus Torvalds ieee754dp fs, ft; 11091da177e4SLinus Torvalds 11101da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 11111da177e4SLinus Torvalds DPFROMREG(ft, MIPSInst_FT(ir)); 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds rv.d = (*handler.b) (fs, ft); 11141da177e4SLinus Torvalds goto copcsr; 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds dcopuop:{ 11171da177e4SLinus Torvalds ieee754dp fs; 11181da177e4SLinus Torvalds 11191da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 11201da177e4SLinus Torvalds rv.d = (*handler.u) (fs); 11211da177e4SLinus Torvalds goto copcsr; 11221da177e4SLinus Torvalds } 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds /* unary conv ops */ 11251da177e4SLinus Torvalds case fcvts_op:{ 11261da177e4SLinus Torvalds ieee754dp fs; 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 11291da177e4SLinus Torvalds rv.s = ieee754sp_fdp(fs); 11301da177e4SLinus Torvalds rfmt = s_fmt; 11311da177e4SLinus Torvalds goto copcsr; 11321da177e4SLinus Torvalds } 11331da177e4SLinus Torvalds case fcvtd_op: 11341da177e4SLinus Torvalds return SIGILL; /* not defined */ 11351da177e4SLinus Torvalds 11361da177e4SLinus Torvalds case fcvtw_op:{ 11371da177e4SLinus Torvalds ieee754dp fs; 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 11401da177e4SLinus Torvalds rv.w = ieee754dp_tint(fs); /* wrong */ 11411da177e4SLinus Torvalds rfmt = w_fmt; 11421da177e4SLinus Torvalds goto copcsr; 11431da177e4SLinus Torvalds } 11441da177e4SLinus Torvalds 1145587cb98fSRalf Baechle #if __mips >= 2 || defined(__mips64) 11461da177e4SLinus Torvalds case fround_op: 11471da177e4SLinus Torvalds case ftrunc_op: 11481da177e4SLinus Torvalds case fceil_op: 11491da177e4SLinus Torvalds case ffloor_op:{ 11501da177e4SLinus Torvalds unsigned int oldrm = ieee754_csr.rm; 11511da177e4SLinus Torvalds ieee754dp fs; 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 11543f135530SShane McDonald ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))]; 11551da177e4SLinus Torvalds rv.w = ieee754dp_tint(fs); 11561da177e4SLinus Torvalds ieee754_csr.rm = oldrm; 11571da177e4SLinus Torvalds rfmt = w_fmt; 11581da177e4SLinus Torvalds goto copcsr; 11591da177e4SLinus Torvalds } 11601da177e4SLinus Torvalds #endif 11611da177e4SLinus Torvalds 11624b724efdSRalf Baechle #if defined(__mips64) 11631da177e4SLinus Torvalds case fcvtl_op:{ 11641da177e4SLinus Torvalds ieee754dp fs; 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 11671da177e4SLinus Torvalds rv.l = ieee754dp_tlong(fs); 11681da177e4SLinus Torvalds rfmt = l_fmt; 11691da177e4SLinus Torvalds goto copcsr; 11701da177e4SLinus Torvalds } 11711da177e4SLinus Torvalds 11721da177e4SLinus Torvalds case froundl_op: 11731da177e4SLinus Torvalds case ftruncl_op: 11741da177e4SLinus Torvalds case fceill_op: 11751da177e4SLinus Torvalds case ffloorl_op:{ 11761da177e4SLinus Torvalds unsigned int oldrm = ieee754_csr.rm; 11771da177e4SLinus Torvalds ieee754dp fs; 11781da177e4SLinus Torvalds 11791da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 11803f135530SShane McDonald ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))]; 11811da177e4SLinus Torvalds rv.l = ieee754dp_tlong(fs); 11821da177e4SLinus Torvalds ieee754_csr.rm = oldrm; 11831da177e4SLinus Torvalds rfmt = l_fmt; 11841da177e4SLinus Torvalds goto copcsr; 11851da177e4SLinus Torvalds } 11864b724efdSRalf Baechle #endif /* __mips >= 3 */ 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds default: 11891da177e4SLinus Torvalds if (MIPSInst_FUNC(ir) >= fcmp_op) { 11901da177e4SLinus Torvalds unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op; 11911da177e4SLinus Torvalds ieee754dp fs, ft; 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 11941da177e4SLinus Torvalds DPFROMREG(ft, MIPSInst_FT(ir)); 11951da177e4SLinus Torvalds rv.w = ieee754dp_cmp(fs, ft, 11961da177e4SLinus Torvalds cmptab[cmpop & 0x7], cmpop & 0x8); 11971da177e4SLinus Torvalds rfmt = -1; 11981da177e4SLinus Torvalds if ((cmpop & 0x8) 11991da177e4SLinus Torvalds && 12001da177e4SLinus Torvalds ieee754_cxtest 12011da177e4SLinus Torvalds (IEEE754_INVALID_OPERATION)) 12021da177e4SLinus Torvalds rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S; 12031da177e4SLinus Torvalds else 12041da177e4SLinus Torvalds goto copcsr; 12051da177e4SLinus Torvalds 12061da177e4SLinus Torvalds } 12071da177e4SLinus Torvalds else { 12081da177e4SLinus Torvalds return SIGILL; 12091da177e4SLinus Torvalds } 12101da177e4SLinus Torvalds break; 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds break; 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds case w_fmt:{ 12161da177e4SLinus Torvalds ieee754sp fs; 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 12191da177e4SLinus Torvalds case fcvts_op: 12201da177e4SLinus Torvalds /* convert word to single precision real */ 12211da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 12221da177e4SLinus Torvalds rv.s = ieee754sp_fint(fs.bits); 12231da177e4SLinus Torvalds rfmt = s_fmt; 12241da177e4SLinus Torvalds goto copcsr; 12251da177e4SLinus Torvalds case fcvtd_op: 12261da177e4SLinus Torvalds /* convert word to double precision real */ 12271da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 12281da177e4SLinus Torvalds rv.d = ieee754dp_fint(fs.bits); 12291da177e4SLinus Torvalds rfmt = d_fmt; 12301da177e4SLinus Torvalds goto copcsr; 12311da177e4SLinus Torvalds default: 12321da177e4SLinus Torvalds return SIGILL; 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds break; 12351da177e4SLinus Torvalds } 12361da177e4SLinus Torvalds 12374b724efdSRalf Baechle #if defined(__mips64) 12381da177e4SLinus Torvalds case l_fmt:{ 12391da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 12401da177e4SLinus Torvalds case fcvts_op: 12411da177e4SLinus Torvalds /* convert long to single precision real */ 12421da177e4SLinus Torvalds rv.s = ieee754sp_flong(ctx->fpr[MIPSInst_FS(ir)]); 12431da177e4SLinus Torvalds rfmt = s_fmt; 12441da177e4SLinus Torvalds goto copcsr; 12451da177e4SLinus Torvalds case fcvtd_op: 12461da177e4SLinus Torvalds /* convert long to double precision real */ 12471da177e4SLinus Torvalds rv.d = ieee754dp_flong(ctx->fpr[MIPSInst_FS(ir)]); 12481da177e4SLinus Torvalds rfmt = d_fmt; 12491da177e4SLinus Torvalds goto copcsr; 12501da177e4SLinus Torvalds default: 12511da177e4SLinus Torvalds return SIGILL; 12521da177e4SLinus Torvalds } 12531da177e4SLinus Torvalds break; 12541da177e4SLinus Torvalds } 12551da177e4SLinus Torvalds #endif 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds default: 12581da177e4SLinus Torvalds return SIGILL; 12591da177e4SLinus Torvalds } 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds /* 12621da177e4SLinus Torvalds * Update the fpu CSR register for this operation. 12631da177e4SLinus Torvalds * If an exception is required, generate a tidy SIGFPE exception, 12641da177e4SLinus Torvalds * without updating the result register. 12651da177e4SLinus Torvalds * Note: cause exception bits do not accumulate, they are rewritten 12661da177e4SLinus Torvalds * for each op; only the flag/sticky bits accumulate. 12671da177e4SLinus Torvalds */ 12681da177e4SLinus Torvalds ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr; 12691da177e4SLinus Torvalds if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) { 12701da177e4SLinus Torvalds /*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */ 12711da177e4SLinus Torvalds return SIGFPE; 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds /* 12751da177e4SLinus Torvalds * Now we can safely write the result back to the register file. 12761da177e4SLinus Torvalds */ 12771da177e4SLinus Torvalds switch (rfmt) { 12781da177e4SLinus Torvalds case -1:{ 12791da177e4SLinus Torvalds #if __mips >= 4 12801da177e4SLinus Torvalds cond = fpucondbit[MIPSInst_FD(ir) >> 2]; 12811da177e4SLinus Torvalds #else 12821da177e4SLinus Torvalds cond = FPU_CSR_COND; 12831da177e4SLinus Torvalds #endif 12841da177e4SLinus Torvalds if (rv.w) 12851da177e4SLinus Torvalds ctx->fcr31 |= cond; 12861da177e4SLinus Torvalds else 12871da177e4SLinus Torvalds ctx->fcr31 &= ~cond; 12881da177e4SLinus Torvalds break; 12891da177e4SLinus Torvalds } 12901da177e4SLinus Torvalds case d_fmt: 12911da177e4SLinus Torvalds DPTOREG(rv.d, MIPSInst_FD(ir)); 12921da177e4SLinus Torvalds break; 12931da177e4SLinus Torvalds case s_fmt: 12941da177e4SLinus Torvalds SPTOREG(rv.s, MIPSInst_FD(ir)); 12951da177e4SLinus Torvalds break; 12961da177e4SLinus Torvalds case w_fmt: 12971da177e4SLinus Torvalds SITOREG(rv.w, MIPSInst_FD(ir)); 12981da177e4SLinus Torvalds break; 12994b724efdSRalf Baechle #if defined(__mips64) 13001da177e4SLinus Torvalds case l_fmt: 13011da177e4SLinus Torvalds DITOREG(rv.l, MIPSInst_FD(ir)); 13021da177e4SLinus Torvalds break; 13031da177e4SLinus Torvalds #endif 13041da177e4SLinus Torvalds default: 13051da177e4SLinus Torvalds return SIGILL; 13061da177e4SLinus Torvalds } 13071da177e4SLinus Torvalds 13081da177e4SLinus Torvalds return 0; 13091da177e4SLinus Torvalds } 13101da177e4SLinus Torvalds 1311e04582b7SAtsushi Nemoto int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx, 1312515b029dSDavid Daney int has_fpu, void *__user *fault_addr) 13131da177e4SLinus Torvalds { 1314333d1f67SRalf Baechle unsigned long oldepc, prevepc; 13151da177e4SLinus Torvalds mips_instruction insn; 13161da177e4SLinus Torvalds int sig = 0; 13171da177e4SLinus Torvalds 13181da177e4SLinus Torvalds oldepc = xcp->cp0_epc; 13191da177e4SLinus Torvalds do { 13201da177e4SLinus Torvalds prevepc = xcp->cp0_epc; 13211da177e4SLinus Torvalds 1322515b029dSDavid Daney if (!access_ok(VERIFY_READ, xcp->cp0_epc, sizeof(mips_instruction))) { 1323b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 1324515b029dSDavid Daney *fault_addr = (mips_instruction __user *)xcp->cp0_epc; 13251da177e4SLinus Torvalds return SIGBUS; 13261da177e4SLinus Torvalds } 1327515b029dSDavid Daney if (__get_user(insn, (mips_instruction __user *) xcp->cp0_epc)) { 1328515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 1329515b029dSDavid Daney *fault_addr = (mips_instruction __user *)xcp->cp0_epc; 1330515b029dSDavid Daney return SIGSEGV; 1331515b029dSDavid Daney } 13321da177e4SLinus Torvalds if (insn == 0) 13331da177e4SLinus Torvalds xcp->cp0_epc += 4; /* skip nops */ 13341da177e4SLinus Torvalds else { 1335cd21dfcfSRalf Baechle /* 1336cd21dfcfSRalf Baechle * The 'ieee754_csr' is an alias of 1337cd21dfcfSRalf Baechle * ctx->fcr31. No need to copy ctx->fcr31 to 1338cd21dfcfSRalf Baechle * ieee754_csr. But ieee754_csr.rm is ieee 1339cd21dfcfSRalf Baechle * library modes. (not mips rounding mode) 1340cd21dfcfSRalf Baechle */ 1341cd21dfcfSRalf Baechle /* convert to ieee library modes */ 1342cd21dfcfSRalf Baechle ieee754_csr.rm = ieee_rm[ieee754_csr.rm]; 1343515b029dSDavid Daney sig = cop1Emulate(xcp, ctx, fault_addr); 1344cd21dfcfSRalf Baechle /* revert to mips rounding mode */ 1345cd21dfcfSRalf Baechle ieee754_csr.rm = mips_rm[ieee754_csr.rm]; 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds 1348e04582b7SAtsushi Nemoto if (has_fpu) 13491da177e4SLinus Torvalds break; 13501da177e4SLinus Torvalds if (sig) 13511da177e4SLinus Torvalds break; 13521da177e4SLinus Torvalds 13531da177e4SLinus Torvalds cond_resched(); 13541da177e4SLinus Torvalds } while (xcp->cp0_epc > prevepc); 13551da177e4SLinus Torvalds 13561da177e4SLinus Torvalds /* SIGILL indicates a non-fpu instruction */ 13571da177e4SLinus Torvalds if (sig == SIGILL && xcp->cp0_epc != oldepc) 13581da177e4SLinus Torvalds /* but if epc has advanced, then ignore it */ 13591da177e4SLinus Torvalds sig = 0; 13601da177e4SLinus Torvalds 13611da177e4SLinus Torvalds return sig; 13621da177e4SLinus Torvalds } 136383fd38caSAtsushi Nemoto 136483fd38caSAtsushi Nemoto #ifdef CONFIG_DEBUG_FS 1365b6ee75edSDavid Daney 1366b6ee75edSDavid Daney static int fpuemu_stat_get(void *data, u64 *val) 1367b6ee75edSDavid Daney { 1368b6ee75edSDavid Daney int cpu; 1369b6ee75edSDavid Daney unsigned long sum = 0; 1370b6ee75edSDavid Daney for_each_online_cpu(cpu) { 1371b6ee75edSDavid Daney struct mips_fpu_emulator_stats *ps; 1372b6ee75edSDavid Daney local_t *pv; 1373b6ee75edSDavid Daney ps = &per_cpu(fpuemustats, cpu); 1374b6ee75edSDavid Daney pv = (void *)ps + (unsigned long)data; 1375b6ee75edSDavid Daney sum += local_read(pv); 1376b6ee75edSDavid Daney } 1377b6ee75edSDavid Daney *val = sum; 1378b6ee75edSDavid Daney return 0; 1379b6ee75edSDavid Daney } 1380b6ee75edSDavid Daney DEFINE_SIMPLE_ATTRIBUTE(fops_fpuemu_stat, fpuemu_stat_get, NULL, "%llu\n"); 1381b6ee75edSDavid Daney 138283fd38caSAtsushi Nemoto extern struct dentry *mips_debugfs_dir; 138383fd38caSAtsushi Nemoto static int __init debugfs_fpuemu(void) 138483fd38caSAtsushi Nemoto { 138583fd38caSAtsushi Nemoto struct dentry *d, *dir; 138683fd38caSAtsushi Nemoto 138783fd38caSAtsushi Nemoto if (!mips_debugfs_dir) 138883fd38caSAtsushi Nemoto return -ENODEV; 138983fd38caSAtsushi Nemoto dir = debugfs_create_dir("fpuemustats", mips_debugfs_dir); 1390ecab1f44SZhaolei if (!dir) 1391ecab1f44SZhaolei return -ENOMEM; 1392b6ee75edSDavid Daney 1393b6ee75edSDavid Daney #define FPU_STAT_CREATE(M) \ 1394b6ee75edSDavid Daney do { \ 1395b6ee75edSDavid Daney d = debugfs_create_file(#M , S_IRUGO, dir, \ 1396b6ee75edSDavid Daney (void *)offsetof(struct mips_fpu_emulator_stats, M), \ 1397b6ee75edSDavid Daney &fops_fpuemu_stat); \ 1398b6ee75edSDavid Daney if (!d) \ 1399b6ee75edSDavid Daney return -ENOMEM; \ 1400b6ee75edSDavid Daney } while (0) 1401b6ee75edSDavid Daney 1402b6ee75edSDavid Daney FPU_STAT_CREATE(emulated); 1403b6ee75edSDavid Daney FPU_STAT_CREATE(loads); 1404b6ee75edSDavid Daney FPU_STAT_CREATE(stores); 1405b6ee75edSDavid Daney FPU_STAT_CREATE(cp1ops); 1406b6ee75edSDavid Daney FPU_STAT_CREATE(cp1xops); 1407b6ee75edSDavid Daney FPU_STAT_CREATE(errors); 1408b6ee75edSDavid Daney 140983fd38caSAtsushi Nemoto return 0; 141083fd38caSAtsushi Nemoto } 141183fd38caSAtsushi Nemoto __initcall(debugfs_fpuemu); 141283fd38caSAtsushi Nemoto #endif 1413