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 174*51d943f0SRalf Baechle * 64-bit CPUs so we rather look at TIF_32BIT_REGS. 175*51d943f0SRalf Baechle * FPU emu is slow and bulky and optimizing this function offers fairly 176*51d943f0SRalf Baechle * sizeable benefits so we try to be clever and make this function return 177*51d943f0SRalf Baechle * a constant whenever possible, that is on 64-bit kernels without O32 178*51d943f0SRalf Baechle * compatibility enabled and on 32-bit kernels. 1791da177e4SLinus Torvalds */ 180da0bac33SDavid Daney static inline int cop1_64bit(struct pt_regs *xcp) 181da0bac33SDavid Daney { 182*51d943f0SRalf Baechle #if defined(CONFIG_64BIT) && !defined(CONFIG_MIPS32_O32) 183*51d943f0SRalf Baechle return 1; 184*51d943f0SRalf Baechle #elif defined(CONFIG_64BIT) && defined(CONFIG_MIPS32_O32) 185da0bac33SDavid Daney return !test_thread_flag(TIF_32BIT_REGS); 1861da177e4SLinus Torvalds #else 187da0bac33SDavid Daney return 0; 1881da177e4SLinus Torvalds #endif 189da0bac33SDavid Daney } 1901da177e4SLinus Torvalds 191da0bac33SDavid Daney #define SIFROMREG(si, x) ((si) = cop1_64bit(xcp) || !(x & 1) ? \ 192da0bac33SDavid Daney (int)ctx->fpr[x] : (int)(ctx->fpr[x & ~1] >> 32)) 193da0bac33SDavid Daney 194da0bac33SDavid Daney #define SITOREG(si, x) (ctx->fpr[x & ~(cop1_64bit(xcp) == 0)] = \ 195da0bac33SDavid Daney cop1_64bit(xcp) || !(x & 1) ? \ 1961da177e4SLinus Torvalds ctx->fpr[x & ~1] >> 32 << 32 | (u32)(si) : \ 1971da177e4SLinus Torvalds ctx->fpr[x & ~1] << 32 >> 32 | (u64)(si) << 32) 1981da177e4SLinus Torvalds 199da0bac33SDavid Daney #define DIFROMREG(di, x) ((di) = ctx->fpr[x & ~(cop1_64bit(xcp) == 0)]) 200da0bac33SDavid Daney #define DITOREG(di, x) (ctx->fpr[x & ~(cop1_64bit(xcp) == 0)] = (di)) 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x) 2031da177e4SLinus Torvalds #define SPTOREG(sp, x) SITOREG((sp).bits, x) 2041da177e4SLinus Torvalds #define DPFROMREG(dp, x) DIFROMREG((dp).bits, x) 2051da177e4SLinus Torvalds #define DPTOREG(dp, x) DITOREG((dp).bits, x) 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds /* 2081da177e4SLinus Torvalds * Emulate the single floating point instruction pointed at by EPC. 2091da177e4SLinus Torvalds * Two instructions if the instruction is in a branch delay slot. 2101da177e4SLinus Torvalds */ 2111da177e4SLinus Torvalds 212515b029dSDavid Daney static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx, 213515b029dSDavid Daney void *__user *fault_addr) 2141da177e4SLinus Torvalds { 2151da177e4SLinus Torvalds mips_instruction ir; 216e70dfc10SAtsushi Nemoto unsigned long emulpc, contpc; 2171da177e4SLinus Torvalds unsigned int cond; 2181da177e4SLinus Torvalds 219515b029dSDavid Daney if (!access_ok(VERIFY_READ, xcp->cp0_epc, sizeof(mips_instruction))) { 220b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 221515b029dSDavid Daney *fault_addr = (mips_instruction __user *)xcp->cp0_epc; 2221da177e4SLinus Torvalds return SIGBUS; 2231da177e4SLinus Torvalds } 224515b029dSDavid Daney if (__get_user(ir, (mips_instruction __user *) xcp->cp0_epc)) { 225515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 226515b029dSDavid Daney *fault_addr = (mips_instruction __user *)xcp->cp0_epc; 227515b029dSDavid Daney return SIGSEGV; 228515b029dSDavid Daney } 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds /* XXX NEC Vr54xx bug workaround */ 2311da177e4SLinus Torvalds if ((xcp->cp0_cause & CAUSEF_BD) && !isBranchInstr(&ir)) 2321da177e4SLinus Torvalds xcp->cp0_cause &= ~CAUSEF_BD; 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds if (xcp->cp0_cause & CAUSEF_BD) { 2351da177e4SLinus Torvalds /* 2361da177e4SLinus Torvalds * The instruction to be emulated is in a branch delay slot 2371da177e4SLinus Torvalds * which means that we have to emulate the branch instruction 2381da177e4SLinus Torvalds * BEFORE we do the cop1 instruction. 2391da177e4SLinus Torvalds * 2401da177e4SLinus Torvalds * This branch could be a COP1 branch, but in that case we 2411da177e4SLinus Torvalds * would have had a trap for that instruction, and would not 2421da177e4SLinus Torvalds * come through this route. 2431da177e4SLinus Torvalds * 2441da177e4SLinus Torvalds * Linux MIPS branch emulator operates on context, updating the 2451da177e4SLinus Torvalds * cp0_epc. 2461da177e4SLinus Torvalds */ 247e70dfc10SAtsushi Nemoto emulpc = xcp->cp0_epc + 4; /* Snapshot emulation target */ 2481da177e4SLinus Torvalds 249d8d4e3aeSManeesh Soni if (__compute_return_epc(xcp) < 0) { 2501da177e4SLinus Torvalds #ifdef CP1DBG 2511da177e4SLinus Torvalds printk("failed to emulate branch at %p\n", 252333d1f67SRalf Baechle (void *) (xcp->cp0_epc)); 2531da177e4SLinus Torvalds #endif 2541da177e4SLinus Torvalds return SIGILL; 2551da177e4SLinus Torvalds } 256515b029dSDavid Daney if (!access_ok(VERIFY_READ, emulpc, sizeof(mips_instruction))) { 257b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 258515b029dSDavid Daney *fault_addr = (mips_instruction __user *)emulpc; 2591da177e4SLinus Torvalds return SIGBUS; 2601da177e4SLinus Torvalds } 261515b029dSDavid Daney if (__get_user(ir, (mips_instruction __user *) emulpc)) { 262515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 263515b029dSDavid Daney *fault_addr = (mips_instruction __user *)emulpc; 264515b029dSDavid Daney return SIGSEGV; 265515b029dSDavid Daney } 2661da177e4SLinus Torvalds /* __compute_return_epc() will have updated cp0_epc */ 267e70dfc10SAtsushi Nemoto contpc = xcp->cp0_epc; 2681da177e4SLinus Torvalds /* In order not to confuse ptrace() et al, tweak context */ 269e70dfc10SAtsushi Nemoto xcp->cp0_epc = emulpc - 4; 270333d1f67SRalf Baechle } else { 271e70dfc10SAtsushi Nemoto emulpc = xcp->cp0_epc; 272e70dfc10SAtsushi Nemoto contpc = xcp->cp0_epc + 4; 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds emul: 276a8b0ca17SPeter Zijlstra perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0); 277b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(emulated); 2781da177e4SLinus Torvalds switch (MIPSInst_OPCODE(ir)) { 2791da177e4SLinus Torvalds case ldc1_op:{ 2803fccc015SRalf Baechle u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] + 2811da177e4SLinus Torvalds MIPSInst_SIMM(ir)); 2821da177e4SLinus Torvalds u64 val; 2831da177e4SLinus Torvalds 284b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(loads); 285515b029dSDavid Daney 286515b029dSDavid Daney if (!access_ok(VERIFY_READ, va, sizeof(u64))) { 287b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 288515b029dSDavid Daney *fault_addr = va; 2891da177e4SLinus Torvalds return SIGBUS; 2901da177e4SLinus Torvalds } 291515b029dSDavid Daney if (__get_user(val, va)) { 292515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 293515b029dSDavid Daney *fault_addr = va; 294515b029dSDavid Daney return SIGSEGV; 295515b029dSDavid Daney } 2961da177e4SLinus Torvalds DITOREG(val, MIPSInst_RT(ir)); 2971da177e4SLinus Torvalds break; 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds case sdc1_op:{ 3013fccc015SRalf Baechle u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] + 3021da177e4SLinus Torvalds MIPSInst_SIMM(ir)); 3031da177e4SLinus Torvalds u64 val; 3041da177e4SLinus Torvalds 305b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(stores); 3061da177e4SLinus Torvalds DIFROMREG(val, MIPSInst_RT(ir)); 307515b029dSDavid Daney if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) { 308b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 309515b029dSDavid Daney *fault_addr = va; 3101da177e4SLinus Torvalds return SIGBUS; 3111da177e4SLinus Torvalds } 312515b029dSDavid Daney if (__put_user(val, va)) { 313515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 314515b029dSDavid Daney *fault_addr = va; 315515b029dSDavid Daney return SIGSEGV; 316515b029dSDavid Daney } 3171da177e4SLinus Torvalds break; 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds case lwc1_op:{ 3213fccc015SRalf Baechle u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] + 3221da177e4SLinus Torvalds MIPSInst_SIMM(ir)); 3231da177e4SLinus Torvalds u32 val; 3241da177e4SLinus Torvalds 325b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(loads); 326515b029dSDavid Daney if (!access_ok(VERIFY_READ, va, sizeof(u32))) { 327b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 328515b029dSDavid Daney *fault_addr = va; 3291da177e4SLinus Torvalds return SIGBUS; 3301da177e4SLinus Torvalds } 331515b029dSDavid Daney if (__get_user(val, va)) { 332515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 333515b029dSDavid Daney *fault_addr = va; 334515b029dSDavid Daney return SIGSEGV; 335515b029dSDavid Daney } 3361da177e4SLinus Torvalds SITOREG(val, MIPSInst_RT(ir)); 3371da177e4SLinus Torvalds break; 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds case swc1_op:{ 3413fccc015SRalf Baechle u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] + 3421da177e4SLinus Torvalds MIPSInst_SIMM(ir)); 3431da177e4SLinus Torvalds u32 val; 3441da177e4SLinus Torvalds 345b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(stores); 3461da177e4SLinus Torvalds SIFROMREG(val, MIPSInst_RT(ir)); 347515b029dSDavid Daney if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) { 348b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 349515b029dSDavid Daney *fault_addr = va; 3501da177e4SLinus Torvalds return SIGBUS; 3511da177e4SLinus Torvalds } 352515b029dSDavid Daney if (__put_user(val, va)) { 353515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 354515b029dSDavid Daney *fault_addr = va; 355515b029dSDavid Daney return SIGSEGV; 356515b029dSDavid Daney } 3571da177e4SLinus Torvalds break; 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds case cop1_op: 3611da177e4SLinus Torvalds switch (MIPSInst_RS(ir)) { 3621da177e4SLinus Torvalds 3634b724efdSRalf Baechle #if defined(__mips64) 3641da177e4SLinus Torvalds case dmfc_op: 3651da177e4SLinus Torvalds /* copregister fs -> gpr[rt] */ 3661da177e4SLinus Torvalds if (MIPSInst_RT(ir) != 0) { 3671da177e4SLinus Torvalds DIFROMREG(xcp->regs[MIPSInst_RT(ir)], 3681da177e4SLinus Torvalds MIPSInst_RD(ir)); 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds break; 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds case dmtc_op: 3731da177e4SLinus Torvalds /* copregister fs <- rt */ 3741da177e4SLinus Torvalds DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir)); 3751da177e4SLinus Torvalds break; 3761da177e4SLinus Torvalds #endif 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds case mfc_op: 3791da177e4SLinus Torvalds /* copregister rd -> gpr[rt] */ 3801da177e4SLinus Torvalds if (MIPSInst_RT(ir) != 0) { 3811da177e4SLinus Torvalds SIFROMREG(xcp->regs[MIPSInst_RT(ir)], 3821da177e4SLinus Torvalds MIPSInst_RD(ir)); 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds break; 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds case mtc_op: 3871da177e4SLinus Torvalds /* copregister rd <- rt */ 3881da177e4SLinus Torvalds SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir)); 3891da177e4SLinus Torvalds break; 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds case cfc_op:{ 3921da177e4SLinus Torvalds /* cop control register rd -> gpr[rt] */ 3931da177e4SLinus Torvalds u32 value; 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds if (MIPSInst_RD(ir) == FPCREG_CSR) { 3961da177e4SLinus Torvalds value = ctx->fcr31; 3973f135530SShane McDonald value = (value & ~FPU_CSR_RM) | 3983f135530SShane McDonald mips_rm[modeindex(value)]; 3991da177e4SLinus Torvalds #ifdef CSRTRACE 4001da177e4SLinus Torvalds printk("%p gpr[%d]<-csr=%08x\n", 401333d1f67SRalf Baechle (void *) (xcp->cp0_epc), 4021da177e4SLinus Torvalds MIPSInst_RT(ir), value); 4031da177e4SLinus Torvalds #endif 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds else if (MIPSInst_RD(ir) == FPCREG_RID) 4061da177e4SLinus Torvalds value = 0; 4071da177e4SLinus Torvalds else 4081da177e4SLinus Torvalds value = 0; 4091da177e4SLinus Torvalds if (MIPSInst_RT(ir)) 4101da177e4SLinus Torvalds xcp->regs[MIPSInst_RT(ir)] = value; 4111da177e4SLinus Torvalds break; 4121da177e4SLinus Torvalds } 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds case ctc_op:{ 4151da177e4SLinus Torvalds /* copregister rd <- rt */ 4161da177e4SLinus Torvalds u32 value; 4171da177e4SLinus Torvalds 4181da177e4SLinus Torvalds if (MIPSInst_RT(ir) == 0) 4191da177e4SLinus Torvalds value = 0; 4201da177e4SLinus Torvalds else 4211da177e4SLinus Torvalds value = xcp->regs[MIPSInst_RT(ir)]; 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds /* we only have one writable control reg 4241da177e4SLinus Torvalds */ 4251da177e4SLinus Torvalds if (MIPSInst_RD(ir) == FPCREG_CSR) { 4261da177e4SLinus Torvalds #ifdef CSRTRACE 4271da177e4SLinus Torvalds printk("%p gpr[%d]->csr=%08x\n", 428333d1f67SRalf Baechle (void *) (xcp->cp0_epc), 4291da177e4SLinus Torvalds MIPSInst_RT(ir), value); 4301da177e4SLinus Torvalds #endif 43195e8f634SShane McDonald 43295e8f634SShane McDonald /* 43395e8f634SShane McDonald * Don't write reserved bits, 43495e8f634SShane McDonald * and convert to ieee library modes 43595e8f634SShane McDonald */ 43695e8f634SShane McDonald ctx->fcr31 = (value & 43795e8f634SShane McDonald ~(FPU_CSR_RSVD | FPU_CSR_RM)) | 43895e8f634SShane McDonald ieee_rm[modeindex(value)]; 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) { 4411da177e4SLinus Torvalds return SIGFPE; 4421da177e4SLinus Torvalds } 4431da177e4SLinus Torvalds break; 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds case bc_op:{ 4471da177e4SLinus Torvalds int likely = 0; 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds if (xcp->cp0_cause & CAUSEF_BD) 4501da177e4SLinus Torvalds return SIGILL; 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds #if __mips >= 4 4531da177e4SLinus Torvalds cond = ctx->fcr31 & fpucondbit[MIPSInst_RT(ir) >> 2]; 4541da177e4SLinus Torvalds #else 4551da177e4SLinus Torvalds cond = ctx->fcr31 & FPU_CSR_COND; 4561da177e4SLinus Torvalds #endif 4571da177e4SLinus Torvalds switch (MIPSInst_RT(ir) & 3) { 4581da177e4SLinus Torvalds case bcfl_op: 4591da177e4SLinus Torvalds likely = 1; 4601da177e4SLinus Torvalds case bcf_op: 4611da177e4SLinus Torvalds cond = !cond; 4621da177e4SLinus Torvalds break; 4631da177e4SLinus Torvalds case bctl_op: 4641da177e4SLinus Torvalds likely = 1; 4651da177e4SLinus Torvalds case bct_op: 4661da177e4SLinus Torvalds break; 4671da177e4SLinus Torvalds default: 4681da177e4SLinus Torvalds /* thats an illegal instruction */ 4691da177e4SLinus Torvalds return SIGILL; 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds xcp->cp0_cause |= CAUSEF_BD; 4731da177e4SLinus Torvalds if (cond) { 4741da177e4SLinus Torvalds /* branch taken: emulate dslot 4751da177e4SLinus Torvalds * instruction 4761da177e4SLinus Torvalds */ 4771da177e4SLinus Torvalds xcp->cp0_epc += 4; 478e70dfc10SAtsushi Nemoto contpc = (xcp->cp0_epc + 4791da177e4SLinus Torvalds (MIPSInst_SIMM(ir) << 2)); 4801da177e4SLinus Torvalds 481515b029dSDavid Daney if (!access_ok(VERIFY_READ, xcp->cp0_epc, 482515b029dSDavid Daney sizeof(mips_instruction))) { 483515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 484515b029dSDavid Daney *fault_addr = (mips_instruction __user *)xcp->cp0_epc; 485515b029dSDavid Daney return SIGBUS; 486515b029dSDavid Daney } 487515b029dSDavid Daney if (__get_user(ir, 4883fccc015SRalf Baechle (mips_instruction __user *) xcp->cp0_epc)) { 489b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 490515b029dSDavid Daney *fault_addr = (mips_instruction __user *)xcp->cp0_epc; 491515b029dSDavid Daney return SIGSEGV; 4921da177e4SLinus Torvalds } 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds switch (MIPSInst_OPCODE(ir)) { 4951da177e4SLinus Torvalds case lwc1_op: 4961da177e4SLinus Torvalds case swc1_op: 4974b724efdSRalf Baechle #if (__mips >= 2 || defined(__mips64)) 4981da177e4SLinus Torvalds case ldc1_op: 4991da177e4SLinus Torvalds case sdc1_op: 5001da177e4SLinus Torvalds #endif 5011da177e4SLinus Torvalds case cop1_op: 5021da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32 5031da177e4SLinus Torvalds case cop1x_op: 5041da177e4SLinus Torvalds #endif 5051da177e4SLinus Torvalds /* its one of ours */ 5061da177e4SLinus Torvalds goto emul; 5071da177e4SLinus Torvalds #if __mips >= 4 5081da177e4SLinus Torvalds case spec_op: 5091da177e4SLinus Torvalds if (MIPSInst_FUNC(ir) == movc_op) 5101da177e4SLinus Torvalds goto emul; 5111da177e4SLinus Torvalds break; 5121da177e4SLinus Torvalds #endif 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds /* 5161da177e4SLinus Torvalds * Single step the non-cp1 5171da177e4SLinus Torvalds * instruction in the dslot 5181da177e4SLinus Torvalds */ 519e70dfc10SAtsushi Nemoto return mips_dsemul(xcp, ir, contpc); 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds else { 5221da177e4SLinus Torvalds /* branch not taken */ 5231da177e4SLinus Torvalds if (likely) { 5241da177e4SLinus Torvalds /* 5251da177e4SLinus Torvalds * branch likely nullifies 5261da177e4SLinus Torvalds * dslot if not taken 5271da177e4SLinus Torvalds */ 5281da177e4SLinus Torvalds xcp->cp0_epc += 4; 5291da177e4SLinus Torvalds contpc += 4; 5301da177e4SLinus Torvalds /* 5311da177e4SLinus Torvalds * else continue & execute 5321da177e4SLinus Torvalds * dslot as normal insn 5331da177e4SLinus Torvalds */ 5341da177e4SLinus Torvalds } 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds break; 5371da177e4SLinus Torvalds } 5381da177e4SLinus Torvalds 5391da177e4SLinus Torvalds default: 5401da177e4SLinus Torvalds if (!(MIPSInst_RS(ir) & 0x10)) 5411da177e4SLinus Torvalds return SIGILL; 5421da177e4SLinus Torvalds { 5431da177e4SLinus Torvalds int sig; 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds /* a real fpu computation instruction */ 5461da177e4SLinus Torvalds if ((sig = fpu_emu(xcp, ctx, ir))) 5471da177e4SLinus Torvalds return sig; 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds } 5501da177e4SLinus Torvalds break; 5511da177e4SLinus Torvalds 5521da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32 5531da177e4SLinus Torvalds case cop1x_op:{ 554515b029dSDavid Daney int sig = fpux_emu(xcp, ctx, ir, fault_addr); 555515b029dSDavid Daney if (sig) 5561da177e4SLinus Torvalds return sig; 5571da177e4SLinus Torvalds break; 5581da177e4SLinus Torvalds } 5591da177e4SLinus Torvalds #endif 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds #if __mips >= 4 5621da177e4SLinus Torvalds case spec_op: 5631da177e4SLinus Torvalds if (MIPSInst_FUNC(ir) != movc_op) 5641da177e4SLinus Torvalds return SIGILL; 5651da177e4SLinus Torvalds cond = fpucondbit[MIPSInst_RT(ir) >> 2]; 5661da177e4SLinus Torvalds if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0)) 5671da177e4SLinus Torvalds xcp->regs[MIPSInst_RD(ir)] = 5681da177e4SLinus Torvalds xcp->regs[MIPSInst_RS(ir)]; 5691da177e4SLinus Torvalds break; 5701da177e4SLinus Torvalds #endif 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds default: 5731da177e4SLinus Torvalds return SIGILL; 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds /* we did it !! */ 577e70dfc10SAtsushi Nemoto xcp->cp0_epc = contpc; 5781da177e4SLinus Torvalds xcp->cp0_cause &= ~CAUSEF_BD; 579333d1f67SRalf Baechle 5801da177e4SLinus Torvalds return 0; 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds 5831da177e4SLinus Torvalds /* 5841da177e4SLinus Torvalds * Conversion table from MIPS compare ops 48-63 5851da177e4SLinus Torvalds * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig); 5861da177e4SLinus Torvalds */ 5871da177e4SLinus Torvalds static const unsigned char cmptab[8] = { 5881da177e4SLinus Torvalds 0, /* cmp_0 (sig) cmp_sf */ 5891da177e4SLinus Torvalds IEEE754_CUN, /* cmp_un (sig) cmp_ngle */ 5901da177e4SLinus Torvalds IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */ 5911da177e4SLinus Torvalds IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */ 5921da177e4SLinus Torvalds IEEE754_CLT, /* cmp_olt (sig) cmp_lt */ 5931da177e4SLinus Torvalds IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */ 5941da177e4SLinus Torvalds IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */ 5951da177e4SLinus Torvalds IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */ 5961da177e4SLinus Torvalds }; 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds /* 6021da177e4SLinus Torvalds * Additional MIPS4 instructions 6031da177e4SLinus Torvalds */ 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds #define DEF3OP(name, p, f1, f2, f3) \ 6061da177e4SLinus Torvalds static ieee754##p fpemu_##p##_##name(ieee754##p r, ieee754##p s, \ 6071da177e4SLinus Torvalds ieee754##p t) \ 6081da177e4SLinus Torvalds { \ 609cd21dfcfSRalf Baechle struct _ieee754_csr ieee754_csr_save; \ 6101da177e4SLinus Torvalds s = f1(s, t); \ 6111da177e4SLinus Torvalds ieee754_csr_save = ieee754_csr; \ 6121da177e4SLinus Torvalds s = f2(s, r); \ 6131da177e4SLinus Torvalds ieee754_csr_save.cx |= ieee754_csr.cx; \ 6141da177e4SLinus Torvalds ieee754_csr_save.sx |= ieee754_csr.sx; \ 6151da177e4SLinus Torvalds s = f3(s); \ 6161da177e4SLinus Torvalds ieee754_csr.cx |= ieee754_csr_save.cx; \ 6171da177e4SLinus Torvalds ieee754_csr.sx |= ieee754_csr_save.sx; \ 6181da177e4SLinus Torvalds return s; \ 6191da177e4SLinus Torvalds } 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds static ieee754dp fpemu_dp_recip(ieee754dp d) 6221da177e4SLinus Torvalds { 6231da177e4SLinus Torvalds return ieee754dp_div(ieee754dp_one(0), d); 6241da177e4SLinus Torvalds } 6251da177e4SLinus Torvalds 6261da177e4SLinus Torvalds static ieee754dp fpemu_dp_rsqrt(ieee754dp d) 6271da177e4SLinus Torvalds { 6281da177e4SLinus Torvalds return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d)); 6291da177e4SLinus Torvalds } 6301da177e4SLinus Torvalds 6311da177e4SLinus Torvalds static ieee754sp fpemu_sp_recip(ieee754sp s) 6321da177e4SLinus Torvalds { 6331da177e4SLinus Torvalds return ieee754sp_div(ieee754sp_one(0), s); 6341da177e4SLinus Torvalds } 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds static ieee754sp fpemu_sp_rsqrt(ieee754sp s) 6371da177e4SLinus Torvalds { 6381da177e4SLinus Torvalds return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s)); 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, ); 6421da177e4SLinus Torvalds DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, ); 6431da177e4SLinus Torvalds DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg); 6441da177e4SLinus Torvalds DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg); 6451da177e4SLinus Torvalds DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, ); 6461da177e4SLinus Torvalds DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, ); 6471da177e4SLinus Torvalds DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg); 6481da177e4SLinus Torvalds DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg); 6491da177e4SLinus Torvalds 650eae89076SAtsushi Nemoto static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx, 651515b029dSDavid Daney mips_instruction ir, void *__user *fault_addr) 6521da177e4SLinus Torvalds { 6531da177e4SLinus Torvalds unsigned rcsr = 0; /* resulting csr */ 6541da177e4SLinus Torvalds 655b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(cp1xops); 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds switch (MIPSInst_FMA_FFMT(ir)) { 6581da177e4SLinus Torvalds case s_fmt:{ /* 0 */ 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds ieee754sp(*handler) (ieee754sp, ieee754sp, ieee754sp); 6611da177e4SLinus Torvalds ieee754sp fd, fr, fs, ft; 6623fccc015SRalf Baechle u32 __user *va; 6631da177e4SLinus Torvalds u32 val; 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 6661da177e4SLinus Torvalds case lwxc1_op: 6673fccc015SRalf Baechle va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] + 6681da177e4SLinus Torvalds xcp->regs[MIPSInst_FT(ir)]); 6691da177e4SLinus Torvalds 670b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(loads); 671515b029dSDavid Daney if (!access_ok(VERIFY_READ, va, sizeof(u32))) { 672b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 673515b029dSDavid Daney *fault_addr = va; 6741da177e4SLinus Torvalds return SIGBUS; 6751da177e4SLinus Torvalds } 676515b029dSDavid Daney if (__get_user(val, va)) { 677515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 678515b029dSDavid Daney *fault_addr = va; 679515b029dSDavid Daney return SIGSEGV; 680515b029dSDavid Daney } 6811da177e4SLinus Torvalds SITOREG(val, MIPSInst_FD(ir)); 6821da177e4SLinus Torvalds break; 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds case swxc1_op: 6853fccc015SRalf Baechle va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] + 6861da177e4SLinus Torvalds xcp->regs[MIPSInst_FT(ir)]); 6871da177e4SLinus Torvalds 688b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(stores); 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds SIFROMREG(val, MIPSInst_FS(ir)); 691515b029dSDavid Daney if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) { 692515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 693515b029dSDavid Daney *fault_addr = va; 694515b029dSDavid Daney return SIGBUS; 695515b029dSDavid Daney } 6961da177e4SLinus Torvalds if (put_user(val, va)) { 697b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 698515b029dSDavid Daney *fault_addr = va; 699515b029dSDavid Daney return SIGSEGV; 7001da177e4SLinus Torvalds } 7011da177e4SLinus Torvalds break; 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds case madd_s_op: 7041da177e4SLinus Torvalds handler = fpemu_sp_madd; 7051da177e4SLinus Torvalds goto scoptop; 7061da177e4SLinus Torvalds case msub_s_op: 7071da177e4SLinus Torvalds handler = fpemu_sp_msub; 7081da177e4SLinus Torvalds goto scoptop; 7091da177e4SLinus Torvalds case nmadd_s_op: 7101da177e4SLinus Torvalds handler = fpemu_sp_nmadd; 7111da177e4SLinus Torvalds goto scoptop; 7121da177e4SLinus Torvalds case nmsub_s_op: 7131da177e4SLinus Torvalds handler = fpemu_sp_nmsub; 7141da177e4SLinus Torvalds goto scoptop; 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds scoptop: 7171da177e4SLinus Torvalds SPFROMREG(fr, MIPSInst_FR(ir)); 7181da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 7191da177e4SLinus Torvalds SPFROMREG(ft, MIPSInst_FT(ir)); 7201da177e4SLinus Torvalds fd = (*handler) (fr, fs, ft); 7211da177e4SLinus Torvalds SPTOREG(fd, MIPSInst_FD(ir)); 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds copcsr: 7241da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_INEXACT)) 7251da177e4SLinus Torvalds rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S; 7261da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_UNDERFLOW)) 7271da177e4SLinus Torvalds rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S; 7281da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_OVERFLOW)) 7291da177e4SLinus Torvalds rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S; 7301da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) 7311da177e4SLinus Torvalds rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S; 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr; 7341da177e4SLinus Torvalds if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) { 7351da177e4SLinus Torvalds /*printk ("SIGFPE: fpu csr = %08x\n", 7361da177e4SLinus Torvalds ctx->fcr31); */ 7371da177e4SLinus Torvalds return SIGFPE; 7381da177e4SLinus Torvalds } 7391da177e4SLinus Torvalds 7401da177e4SLinus Torvalds break; 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds default: 7431da177e4SLinus Torvalds return SIGILL; 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds break; 7461da177e4SLinus Torvalds } 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds case d_fmt:{ /* 1 */ 7491da177e4SLinus Torvalds ieee754dp(*handler) (ieee754dp, ieee754dp, ieee754dp); 7501da177e4SLinus Torvalds ieee754dp fd, fr, fs, ft; 7513fccc015SRalf Baechle u64 __user *va; 7521da177e4SLinus Torvalds u64 val; 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 7551da177e4SLinus Torvalds case ldxc1_op: 7563fccc015SRalf Baechle va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] + 7571da177e4SLinus Torvalds xcp->regs[MIPSInst_FT(ir)]); 7581da177e4SLinus Torvalds 759b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(loads); 760515b029dSDavid Daney if (!access_ok(VERIFY_READ, va, sizeof(u64))) { 761b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 762515b029dSDavid Daney *fault_addr = va; 7631da177e4SLinus Torvalds return SIGBUS; 7641da177e4SLinus Torvalds } 765515b029dSDavid Daney if (__get_user(val, va)) { 766515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 767515b029dSDavid Daney *fault_addr = va; 768515b029dSDavid Daney return SIGSEGV; 769515b029dSDavid Daney } 7701da177e4SLinus Torvalds DITOREG(val, MIPSInst_FD(ir)); 7711da177e4SLinus Torvalds break; 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds case sdxc1_op: 7743fccc015SRalf Baechle va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] + 7751da177e4SLinus Torvalds xcp->regs[MIPSInst_FT(ir)]); 7761da177e4SLinus Torvalds 777b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(stores); 7781da177e4SLinus Torvalds DIFROMREG(val, MIPSInst_FS(ir)); 779515b029dSDavid Daney if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) { 780b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 781515b029dSDavid Daney *fault_addr = va; 7821da177e4SLinus Torvalds return SIGBUS; 7831da177e4SLinus Torvalds } 784515b029dSDavid Daney if (__put_user(val, va)) { 785515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 786515b029dSDavid Daney *fault_addr = va; 787515b029dSDavid Daney return SIGSEGV; 788515b029dSDavid Daney } 7891da177e4SLinus Torvalds break; 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds case madd_d_op: 7921da177e4SLinus Torvalds handler = fpemu_dp_madd; 7931da177e4SLinus Torvalds goto dcoptop; 7941da177e4SLinus Torvalds case msub_d_op: 7951da177e4SLinus Torvalds handler = fpemu_dp_msub; 7961da177e4SLinus Torvalds goto dcoptop; 7971da177e4SLinus Torvalds case nmadd_d_op: 7981da177e4SLinus Torvalds handler = fpemu_dp_nmadd; 7991da177e4SLinus Torvalds goto dcoptop; 8001da177e4SLinus Torvalds case nmsub_d_op: 8011da177e4SLinus Torvalds handler = fpemu_dp_nmsub; 8021da177e4SLinus Torvalds goto dcoptop; 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds dcoptop: 8051da177e4SLinus Torvalds DPFROMREG(fr, MIPSInst_FR(ir)); 8061da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 8071da177e4SLinus Torvalds DPFROMREG(ft, MIPSInst_FT(ir)); 8081da177e4SLinus Torvalds fd = (*handler) (fr, fs, ft); 8091da177e4SLinus Torvalds DPTOREG(fd, MIPSInst_FD(ir)); 8101da177e4SLinus Torvalds goto copcsr; 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds default: 8131da177e4SLinus Torvalds return SIGILL; 8141da177e4SLinus Torvalds } 8151da177e4SLinus Torvalds break; 8161da177e4SLinus Torvalds } 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds case 0x7: /* 7 */ 8191da177e4SLinus Torvalds if (MIPSInst_FUNC(ir) != pfetch_op) { 8201da177e4SLinus Torvalds return SIGILL; 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds /* ignore prefx operation */ 8231da177e4SLinus Torvalds break; 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds default: 8261da177e4SLinus Torvalds return SIGILL; 8271da177e4SLinus Torvalds } 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds return 0; 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds #endif 8321da177e4SLinus Torvalds 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds /* 8361da177e4SLinus Torvalds * Emulate a single COP1 arithmetic instruction. 8371da177e4SLinus Torvalds */ 838eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx, 8391da177e4SLinus Torvalds mips_instruction ir) 8401da177e4SLinus Torvalds { 8411da177e4SLinus Torvalds int rfmt; /* resulting format */ 8421da177e4SLinus Torvalds unsigned rcsr = 0; /* resulting csr */ 8431da177e4SLinus Torvalds unsigned cond; 8441da177e4SLinus Torvalds union { 8451da177e4SLinus Torvalds ieee754dp d; 8461da177e4SLinus Torvalds ieee754sp s; 8471da177e4SLinus Torvalds int w; 848766160c2SYoichi Yuasa #ifdef __mips64 8491da177e4SLinus Torvalds s64 l; 8501da177e4SLinus Torvalds #endif 8511da177e4SLinus Torvalds } rv; /* resulting value */ 8521da177e4SLinus Torvalds 853b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(cp1ops); 8541da177e4SLinus Torvalds switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) { 8551da177e4SLinus Torvalds case s_fmt:{ /* 0 */ 8561da177e4SLinus Torvalds union { 8571da177e4SLinus Torvalds ieee754sp(*b) (ieee754sp, ieee754sp); 8581da177e4SLinus Torvalds ieee754sp(*u) (ieee754sp); 8591da177e4SLinus Torvalds } handler; 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 8621da177e4SLinus Torvalds /* binary ops */ 8631da177e4SLinus Torvalds case fadd_op: 8641da177e4SLinus Torvalds handler.b = ieee754sp_add; 8651da177e4SLinus Torvalds goto scopbop; 8661da177e4SLinus Torvalds case fsub_op: 8671da177e4SLinus Torvalds handler.b = ieee754sp_sub; 8681da177e4SLinus Torvalds goto scopbop; 8691da177e4SLinus Torvalds case fmul_op: 8701da177e4SLinus Torvalds handler.b = ieee754sp_mul; 8711da177e4SLinus Torvalds goto scopbop; 8721da177e4SLinus Torvalds case fdiv_op: 8731da177e4SLinus Torvalds handler.b = ieee754sp_div; 8741da177e4SLinus Torvalds goto scopbop; 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds /* unary ops */ 877587cb98fSRalf Baechle #if __mips >= 2 || defined(__mips64) 8781da177e4SLinus Torvalds case fsqrt_op: 8791da177e4SLinus Torvalds handler.u = ieee754sp_sqrt; 8801da177e4SLinus Torvalds goto scopuop; 8811da177e4SLinus Torvalds #endif 8821da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32 8831da177e4SLinus Torvalds case frsqrt_op: 8841da177e4SLinus Torvalds handler.u = fpemu_sp_rsqrt; 8851da177e4SLinus Torvalds goto scopuop; 8861da177e4SLinus Torvalds case frecip_op: 8871da177e4SLinus Torvalds handler.u = fpemu_sp_recip; 8881da177e4SLinus Torvalds goto scopuop; 8891da177e4SLinus Torvalds #endif 8901da177e4SLinus Torvalds #if __mips >= 4 8911da177e4SLinus Torvalds case fmovc_op: 8921da177e4SLinus Torvalds cond = fpucondbit[MIPSInst_FT(ir) >> 2]; 8931da177e4SLinus Torvalds if (((ctx->fcr31 & cond) != 0) != 8941da177e4SLinus Torvalds ((MIPSInst_FT(ir) & 1) != 0)) 8951da177e4SLinus Torvalds return 0; 8961da177e4SLinus Torvalds SPFROMREG(rv.s, MIPSInst_FS(ir)); 8971da177e4SLinus Torvalds break; 8981da177e4SLinus Torvalds case fmovz_op: 8991da177e4SLinus Torvalds if (xcp->regs[MIPSInst_FT(ir)] != 0) 9001da177e4SLinus Torvalds return 0; 9011da177e4SLinus Torvalds SPFROMREG(rv.s, MIPSInst_FS(ir)); 9021da177e4SLinus Torvalds break; 9031da177e4SLinus Torvalds case fmovn_op: 9041da177e4SLinus Torvalds if (xcp->regs[MIPSInst_FT(ir)] == 0) 9051da177e4SLinus Torvalds return 0; 9061da177e4SLinus Torvalds SPFROMREG(rv.s, MIPSInst_FS(ir)); 9071da177e4SLinus Torvalds break; 9081da177e4SLinus Torvalds #endif 9091da177e4SLinus Torvalds case fabs_op: 9101da177e4SLinus Torvalds handler.u = ieee754sp_abs; 9111da177e4SLinus Torvalds goto scopuop; 9121da177e4SLinus Torvalds case fneg_op: 9131da177e4SLinus Torvalds handler.u = ieee754sp_neg; 9141da177e4SLinus Torvalds goto scopuop; 9151da177e4SLinus Torvalds case fmov_op: 9161da177e4SLinus Torvalds /* an easy one */ 9171da177e4SLinus Torvalds SPFROMREG(rv.s, MIPSInst_FS(ir)); 9181da177e4SLinus Torvalds goto copcsr; 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds /* binary op on handler */ 9211da177e4SLinus Torvalds scopbop: 9221da177e4SLinus Torvalds { 9231da177e4SLinus Torvalds ieee754sp fs, ft; 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 9261da177e4SLinus Torvalds SPFROMREG(ft, MIPSInst_FT(ir)); 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds rv.s = (*handler.b) (fs, ft); 9291da177e4SLinus Torvalds goto copcsr; 9301da177e4SLinus Torvalds } 9311da177e4SLinus Torvalds scopuop: 9321da177e4SLinus Torvalds { 9331da177e4SLinus Torvalds ieee754sp fs; 9341da177e4SLinus Torvalds 9351da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 9361da177e4SLinus Torvalds rv.s = (*handler.u) (fs); 9371da177e4SLinus Torvalds goto copcsr; 9381da177e4SLinus Torvalds } 9391da177e4SLinus Torvalds copcsr: 9401da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_INEXACT)) 9411da177e4SLinus Torvalds rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S; 9421da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_UNDERFLOW)) 9431da177e4SLinus Torvalds rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S; 9441da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_OVERFLOW)) 9451da177e4SLinus Torvalds rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S; 9461da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) 9471da177e4SLinus Torvalds rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S; 9481da177e4SLinus Torvalds if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) 9491da177e4SLinus Torvalds rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S; 9501da177e4SLinus Torvalds break; 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds /* unary conv ops */ 9531da177e4SLinus Torvalds case fcvts_op: 9541da177e4SLinus Torvalds return SIGILL; /* not defined */ 9551da177e4SLinus Torvalds case fcvtd_op:{ 9561da177e4SLinus Torvalds ieee754sp fs; 9571da177e4SLinus Torvalds 9581da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 9591da177e4SLinus Torvalds rv.d = ieee754dp_fsp(fs); 9601da177e4SLinus Torvalds rfmt = d_fmt; 9611da177e4SLinus Torvalds goto copcsr; 9621da177e4SLinus Torvalds } 9631da177e4SLinus Torvalds case fcvtw_op:{ 9641da177e4SLinus Torvalds ieee754sp fs; 9651da177e4SLinus Torvalds 9661da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 9671da177e4SLinus Torvalds rv.w = ieee754sp_tint(fs); 9681da177e4SLinus Torvalds rfmt = w_fmt; 9691da177e4SLinus Torvalds goto copcsr; 9701da177e4SLinus Torvalds } 9711da177e4SLinus Torvalds 972587cb98fSRalf Baechle #if __mips >= 2 || defined(__mips64) 9731da177e4SLinus Torvalds case fround_op: 9741da177e4SLinus Torvalds case ftrunc_op: 9751da177e4SLinus Torvalds case fceil_op: 9761da177e4SLinus Torvalds case ffloor_op:{ 9771da177e4SLinus Torvalds unsigned int oldrm = ieee754_csr.rm; 9781da177e4SLinus Torvalds ieee754sp fs; 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 9813f135530SShane McDonald ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))]; 9821da177e4SLinus Torvalds rv.w = ieee754sp_tint(fs); 9831da177e4SLinus Torvalds ieee754_csr.rm = oldrm; 9841da177e4SLinus Torvalds rfmt = w_fmt; 9851da177e4SLinus Torvalds goto copcsr; 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds #endif /* __mips >= 2 */ 9881da177e4SLinus Torvalds 9894b724efdSRalf Baechle #if defined(__mips64) 9901da177e4SLinus Torvalds case fcvtl_op:{ 9911da177e4SLinus Torvalds ieee754sp fs; 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 9941da177e4SLinus Torvalds rv.l = ieee754sp_tlong(fs); 9951da177e4SLinus Torvalds rfmt = l_fmt; 9961da177e4SLinus Torvalds goto copcsr; 9971da177e4SLinus Torvalds } 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds case froundl_op: 10001da177e4SLinus Torvalds case ftruncl_op: 10011da177e4SLinus Torvalds case fceill_op: 10021da177e4SLinus Torvalds case ffloorl_op:{ 10031da177e4SLinus Torvalds unsigned int oldrm = ieee754_csr.rm; 10041da177e4SLinus Torvalds ieee754sp fs; 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 10073f135530SShane McDonald ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))]; 10081da177e4SLinus Torvalds rv.l = ieee754sp_tlong(fs); 10091da177e4SLinus Torvalds ieee754_csr.rm = oldrm; 10101da177e4SLinus Torvalds rfmt = l_fmt; 10111da177e4SLinus Torvalds goto copcsr; 10121da177e4SLinus Torvalds } 10134b724efdSRalf Baechle #endif /* defined(__mips64) */ 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds default: 10161da177e4SLinus Torvalds if (MIPSInst_FUNC(ir) >= fcmp_op) { 10171da177e4SLinus Torvalds unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op; 10181da177e4SLinus Torvalds ieee754sp fs, ft; 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 10211da177e4SLinus Torvalds SPFROMREG(ft, MIPSInst_FT(ir)); 10221da177e4SLinus Torvalds rv.w = ieee754sp_cmp(fs, ft, 10231da177e4SLinus Torvalds cmptab[cmpop & 0x7], cmpop & 0x8); 10241da177e4SLinus Torvalds rfmt = -1; 10251da177e4SLinus Torvalds if ((cmpop & 0x8) && ieee754_cxtest 10261da177e4SLinus Torvalds (IEEE754_INVALID_OPERATION)) 10271da177e4SLinus Torvalds rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S; 10281da177e4SLinus Torvalds else 10291da177e4SLinus Torvalds goto copcsr; 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds } 10321da177e4SLinus Torvalds else { 10331da177e4SLinus Torvalds return SIGILL; 10341da177e4SLinus Torvalds } 10351da177e4SLinus Torvalds break; 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds break; 10381da177e4SLinus Torvalds } 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds case d_fmt:{ 10411da177e4SLinus Torvalds union { 10421da177e4SLinus Torvalds ieee754dp(*b) (ieee754dp, ieee754dp); 10431da177e4SLinus Torvalds ieee754dp(*u) (ieee754dp); 10441da177e4SLinus Torvalds } handler; 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 10471da177e4SLinus Torvalds /* binary ops */ 10481da177e4SLinus Torvalds case fadd_op: 10491da177e4SLinus Torvalds handler.b = ieee754dp_add; 10501da177e4SLinus Torvalds goto dcopbop; 10511da177e4SLinus Torvalds case fsub_op: 10521da177e4SLinus Torvalds handler.b = ieee754dp_sub; 10531da177e4SLinus Torvalds goto dcopbop; 10541da177e4SLinus Torvalds case fmul_op: 10551da177e4SLinus Torvalds handler.b = ieee754dp_mul; 10561da177e4SLinus Torvalds goto dcopbop; 10571da177e4SLinus Torvalds case fdiv_op: 10581da177e4SLinus Torvalds handler.b = ieee754dp_div; 10591da177e4SLinus Torvalds goto dcopbop; 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds /* unary ops */ 1062587cb98fSRalf Baechle #if __mips >= 2 || defined(__mips64) 10631da177e4SLinus Torvalds case fsqrt_op: 10641da177e4SLinus Torvalds handler.u = ieee754dp_sqrt; 10651da177e4SLinus Torvalds goto dcopuop; 10661da177e4SLinus Torvalds #endif 10671da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32 10681da177e4SLinus Torvalds case frsqrt_op: 10691da177e4SLinus Torvalds handler.u = fpemu_dp_rsqrt; 10701da177e4SLinus Torvalds goto dcopuop; 10711da177e4SLinus Torvalds case frecip_op: 10721da177e4SLinus Torvalds handler.u = fpemu_dp_recip; 10731da177e4SLinus Torvalds goto dcopuop; 10741da177e4SLinus Torvalds #endif 10751da177e4SLinus Torvalds #if __mips >= 4 10761da177e4SLinus Torvalds case fmovc_op: 10771da177e4SLinus Torvalds cond = fpucondbit[MIPSInst_FT(ir) >> 2]; 10781da177e4SLinus Torvalds if (((ctx->fcr31 & cond) != 0) != 10791da177e4SLinus Torvalds ((MIPSInst_FT(ir) & 1) != 0)) 10801da177e4SLinus Torvalds return 0; 10811da177e4SLinus Torvalds DPFROMREG(rv.d, MIPSInst_FS(ir)); 10821da177e4SLinus Torvalds break; 10831da177e4SLinus Torvalds case fmovz_op: 10841da177e4SLinus Torvalds if (xcp->regs[MIPSInst_FT(ir)] != 0) 10851da177e4SLinus Torvalds return 0; 10861da177e4SLinus Torvalds DPFROMREG(rv.d, MIPSInst_FS(ir)); 10871da177e4SLinus Torvalds break; 10881da177e4SLinus Torvalds case fmovn_op: 10891da177e4SLinus Torvalds if (xcp->regs[MIPSInst_FT(ir)] == 0) 10901da177e4SLinus Torvalds return 0; 10911da177e4SLinus Torvalds DPFROMREG(rv.d, MIPSInst_FS(ir)); 10921da177e4SLinus Torvalds break; 10931da177e4SLinus Torvalds #endif 10941da177e4SLinus Torvalds case fabs_op: 10951da177e4SLinus Torvalds handler.u = ieee754dp_abs; 10961da177e4SLinus Torvalds goto dcopuop; 10971da177e4SLinus Torvalds 10981da177e4SLinus Torvalds case fneg_op: 10991da177e4SLinus Torvalds handler.u = ieee754dp_neg; 11001da177e4SLinus Torvalds goto dcopuop; 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds case fmov_op: 11031da177e4SLinus Torvalds /* an easy one */ 11041da177e4SLinus Torvalds DPFROMREG(rv.d, MIPSInst_FS(ir)); 11051da177e4SLinus Torvalds goto copcsr; 11061da177e4SLinus Torvalds 11071da177e4SLinus Torvalds /* binary op on handler */ 11081da177e4SLinus Torvalds dcopbop:{ 11091da177e4SLinus Torvalds ieee754dp fs, ft; 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 11121da177e4SLinus Torvalds DPFROMREG(ft, MIPSInst_FT(ir)); 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds rv.d = (*handler.b) (fs, ft); 11151da177e4SLinus Torvalds goto copcsr; 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds dcopuop:{ 11181da177e4SLinus Torvalds ieee754dp fs; 11191da177e4SLinus Torvalds 11201da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 11211da177e4SLinus Torvalds rv.d = (*handler.u) (fs); 11221da177e4SLinus Torvalds goto copcsr; 11231da177e4SLinus Torvalds } 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds /* unary conv ops */ 11261da177e4SLinus Torvalds case fcvts_op:{ 11271da177e4SLinus Torvalds ieee754dp fs; 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 11301da177e4SLinus Torvalds rv.s = ieee754sp_fdp(fs); 11311da177e4SLinus Torvalds rfmt = s_fmt; 11321da177e4SLinus Torvalds goto copcsr; 11331da177e4SLinus Torvalds } 11341da177e4SLinus Torvalds case fcvtd_op: 11351da177e4SLinus Torvalds return SIGILL; /* not defined */ 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds case fcvtw_op:{ 11381da177e4SLinus Torvalds ieee754dp fs; 11391da177e4SLinus Torvalds 11401da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 11411da177e4SLinus Torvalds rv.w = ieee754dp_tint(fs); /* wrong */ 11421da177e4SLinus Torvalds rfmt = w_fmt; 11431da177e4SLinus Torvalds goto copcsr; 11441da177e4SLinus Torvalds } 11451da177e4SLinus Torvalds 1146587cb98fSRalf Baechle #if __mips >= 2 || defined(__mips64) 11471da177e4SLinus Torvalds case fround_op: 11481da177e4SLinus Torvalds case ftrunc_op: 11491da177e4SLinus Torvalds case fceil_op: 11501da177e4SLinus Torvalds case ffloor_op:{ 11511da177e4SLinus Torvalds unsigned int oldrm = ieee754_csr.rm; 11521da177e4SLinus Torvalds ieee754dp fs; 11531da177e4SLinus Torvalds 11541da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 11553f135530SShane McDonald ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))]; 11561da177e4SLinus Torvalds rv.w = ieee754dp_tint(fs); 11571da177e4SLinus Torvalds ieee754_csr.rm = oldrm; 11581da177e4SLinus Torvalds rfmt = w_fmt; 11591da177e4SLinus Torvalds goto copcsr; 11601da177e4SLinus Torvalds } 11611da177e4SLinus Torvalds #endif 11621da177e4SLinus Torvalds 11634b724efdSRalf Baechle #if defined(__mips64) 11641da177e4SLinus Torvalds case fcvtl_op:{ 11651da177e4SLinus Torvalds ieee754dp fs; 11661da177e4SLinus Torvalds 11671da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 11681da177e4SLinus Torvalds rv.l = ieee754dp_tlong(fs); 11691da177e4SLinus Torvalds rfmt = l_fmt; 11701da177e4SLinus Torvalds goto copcsr; 11711da177e4SLinus Torvalds } 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds case froundl_op: 11741da177e4SLinus Torvalds case ftruncl_op: 11751da177e4SLinus Torvalds case fceill_op: 11761da177e4SLinus Torvalds case ffloorl_op:{ 11771da177e4SLinus Torvalds unsigned int oldrm = ieee754_csr.rm; 11781da177e4SLinus Torvalds ieee754dp fs; 11791da177e4SLinus Torvalds 11801da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 11813f135530SShane McDonald ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))]; 11821da177e4SLinus Torvalds rv.l = ieee754dp_tlong(fs); 11831da177e4SLinus Torvalds ieee754_csr.rm = oldrm; 11841da177e4SLinus Torvalds rfmt = l_fmt; 11851da177e4SLinus Torvalds goto copcsr; 11861da177e4SLinus Torvalds } 11874b724efdSRalf Baechle #endif /* __mips >= 3 */ 11881da177e4SLinus Torvalds 11891da177e4SLinus Torvalds default: 11901da177e4SLinus Torvalds if (MIPSInst_FUNC(ir) >= fcmp_op) { 11911da177e4SLinus Torvalds unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op; 11921da177e4SLinus Torvalds ieee754dp fs, ft; 11931da177e4SLinus Torvalds 11941da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 11951da177e4SLinus Torvalds DPFROMREG(ft, MIPSInst_FT(ir)); 11961da177e4SLinus Torvalds rv.w = ieee754dp_cmp(fs, ft, 11971da177e4SLinus Torvalds cmptab[cmpop & 0x7], cmpop & 0x8); 11981da177e4SLinus Torvalds rfmt = -1; 11991da177e4SLinus Torvalds if ((cmpop & 0x8) 12001da177e4SLinus Torvalds && 12011da177e4SLinus Torvalds ieee754_cxtest 12021da177e4SLinus Torvalds (IEEE754_INVALID_OPERATION)) 12031da177e4SLinus Torvalds rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S; 12041da177e4SLinus Torvalds else 12051da177e4SLinus Torvalds goto copcsr; 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds } 12081da177e4SLinus Torvalds else { 12091da177e4SLinus Torvalds return SIGILL; 12101da177e4SLinus Torvalds } 12111da177e4SLinus Torvalds break; 12121da177e4SLinus Torvalds } 12131da177e4SLinus Torvalds break; 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds 12161da177e4SLinus Torvalds case w_fmt:{ 12171da177e4SLinus Torvalds ieee754sp fs; 12181da177e4SLinus Torvalds 12191da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 12201da177e4SLinus Torvalds case fcvts_op: 12211da177e4SLinus Torvalds /* convert word to single precision real */ 12221da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 12231da177e4SLinus Torvalds rv.s = ieee754sp_fint(fs.bits); 12241da177e4SLinus Torvalds rfmt = s_fmt; 12251da177e4SLinus Torvalds goto copcsr; 12261da177e4SLinus Torvalds case fcvtd_op: 12271da177e4SLinus Torvalds /* convert word to double precision real */ 12281da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 12291da177e4SLinus Torvalds rv.d = ieee754dp_fint(fs.bits); 12301da177e4SLinus Torvalds rfmt = d_fmt; 12311da177e4SLinus Torvalds goto copcsr; 12321da177e4SLinus Torvalds default: 12331da177e4SLinus Torvalds return SIGILL; 12341da177e4SLinus Torvalds } 12351da177e4SLinus Torvalds break; 12361da177e4SLinus Torvalds } 12371da177e4SLinus Torvalds 12384b724efdSRalf Baechle #if defined(__mips64) 12391da177e4SLinus Torvalds case l_fmt:{ 12401da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 12411da177e4SLinus Torvalds case fcvts_op: 12421da177e4SLinus Torvalds /* convert long to single precision real */ 12431da177e4SLinus Torvalds rv.s = ieee754sp_flong(ctx->fpr[MIPSInst_FS(ir)]); 12441da177e4SLinus Torvalds rfmt = s_fmt; 12451da177e4SLinus Torvalds goto copcsr; 12461da177e4SLinus Torvalds case fcvtd_op: 12471da177e4SLinus Torvalds /* convert long to double precision real */ 12481da177e4SLinus Torvalds rv.d = ieee754dp_flong(ctx->fpr[MIPSInst_FS(ir)]); 12491da177e4SLinus Torvalds rfmt = d_fmt; 12501da177e4SLinus Torvalds goto copcsr; 12511da177e4SLinus Torvalds default: 12521da177e4SLinus Torvalds return SIGILL; 12531da177e4SLinus Torvalds } 12541da177e4SLinus Torvalds break; 12551da177e4SLinus Torvalds } 12561da177e4SLinus Torvalds #endif 12571da177e4SLinus Torvalds 12581da177e4SLinus Torvalds default: 12591da177e4SLinus Torvalds return SIGILL; 12601da177e4SLinus Torvalds } 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds /* 12631da177e4SLinus Torvalds * Update the fpu CSR register for this operation. 12641da177e4SLinus Torvalds * If an exception is required, generate a tidy SIGFPE exception, 12651da177e4SLinus Torvalds * without updating the result register. 12661da177e4SLinus Torvalds * Note: cause exception bits do not accumulate, they are rewritten 12671da177e4SLinus Torvalds * for each op; only the flag/sticky bits accumulate. 12681da177e4SLinus Torvalds */ 12691da177e4SLinus Torvalds ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr; 12701da177e4SLinus Torvalds if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) { 12711da177e4SLinus Torvalds /*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */ 12721da177e4SLinus Torvalds return SIGFPE; 12731da177e4SLinus Torvalds } 12741da177e4SLinus Torvalds 12751da177e4SLinus Torvalds /* 12761da177e4SLinus Torvalds * Now we can safely write the result back to the register file. 12771da177e4SLinus Torvalds */ 12781da177e4SLinus Torvalds switch (rfmt) { 12791da177e4SLinus Torvalds case -1:{ 12801da177e4SLinus Torvalds #if __mips >= 4 12811da177e4SLinus Torvalds cond = fpucondbit[MIPSInst_FD(ir) >> 2]; 12821da177e4SLinus Torvalds #else 12831da177e4SLinus Torvalds cond = FPU_CSR_COND; 12841da177e4SLinus Torvalds #endif 12851da177e4SLinus Torvalds if (rv.w) 12861da177e4SLinus Torvalds ctx->fcr31 |= cond; 12871da177e4SLinus Torvalds else 12881da177e4SLinus Torvalds ctx->fcr31 &= ~cond; 12891da177e4SLinus Torvalds break; 12901da177e4SLinus Torvalds } 12911da177e4SLinus Torvalds case d_fmt: 12921da177e4SLinus Torvalds DPTOREG(rv.d, MIPSInst_FD(ir)); 12931da177e4SLinus Torvalds break; 12941da177e4SLinus Torvalds case s_fmt: 12951da177e4SLinus Torvalds SPTOREG(rv.s, MIPSInst_FD(ir)); 12961da177e4SLinus Torvalds break; 12971da177e4SLinus Torvalds case w_fmt: 12981da177e4SLinus Torvalds SITOREG(rv.w, MIPSInst_FD(ir)); 12991da177e4SLinus Torvalds break; 13004b724efdSRalf Baechle #if defined(__mips64) 13011da177e4SLinus Torvalds case l_fmt: 13021da177e4SLinus Torvalds DITOREG(rv.l, MIPSInst_FD(ir)); 13031da177e4SLinus Torvalds break; 13041da177e4SLinus Torvalds #endif 13051da177e4SLinus Torvalds default: 13061da177e4SLinus Torvalds return SIGILL; 13071da177e4SLinus Torvalds } 13081da177e4SLinus Torvalds 13091da177e4SLinus Torvalds return 0; 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds 1312e04582b7SAtsushi Nemoto int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx, 1313515b029dSDavid Daney int has_fpu, void *__user *fault_addr) 13141da177e4SLinus Torvalds { 1315333d1f67SRalf Baechle unsigned long oldepc, prevepc; 13161da177e4SLinus Torvalds mips_instruction insn; 13171da177e4SLinus Torvalds int sig = 0; 13181da177e4SLinus Torvalds 13191da177e4SLinus Torvalds oldepc = xcp->cp0_epc; 13201da177e4SLinus Torvalds do { 13211da177e4SLinus Torvalds prevepc = xcp->cp0_epc; 13221da177e4SLinus Torvalds 1323515b029dSDavid Daney if (!access_ok(VERIFY_READ, xcp->cp0_epc, sizeof(mips_instruction))) { 1324b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 1325515b029dSDavid Daney *fault_addr = (mips_instruction __user *)xcp->cp0_epc; 13261da177e4SLinus Torvalds return SIGBUS; 13271da177e4SLinus Torvalds } 1328515b029dSDavid Daney if (__get_user(insn, (mips_instruction __user *) xcp->cp0_epc)) { 1329515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 1330515b029dSDavid Daney *fault_addr = (mips_instruction __user *)xcp->cp0_epc; 1331515b029dSDavid Daney return SIGSEGV; 1332515b029dSDavid Daney } 13331da177e4SLinus Torvalds if (insn == 0) 13341da177e4SLinus Torvalds xcp->cp0_epc += 4; /* skip nops */ 13351da177e4SLinus Torvalds else { 1336cd21dfcfSRalf Baechle /* 1337cd21dfcfSRalf Baechle * The 'ieee754_csr' is an alias of 1338cd21dfcfSRalf Baechle * ctx->fcr31. No need to copy ctx->fcr31 to 1339cd21dfcfSRalf Baechle * ieee754_csr. But ieee754_csr.rm is ieee 1340cd21dfcfSRalf Baechle * library modes. (not mips rounding mode) 1341cd21dfcfSRalf Baechle */ 1342cd21dfcfSRalf Baechle /* convert to ieee library modes */ 1343cd21dfcfSRalf Baechle ieee754_csr.rm = ieee_rm[ieee754_csr.rm]; 1344515b029dSDavid Daney sig = cop1Emulate(xcp, ctx, fault_addr); 1345cd21dfcfSRalf Baechle /* revert to mips rounding mode */ 1346cd21dfcfSRalf Baechle ieee754_csr.rm = mips_rm[ieee754_csr.rm]; 13471da177e4SLinus Torvalds } 13481da177e4SLinus Torvalds 1349e04582b7SAtsushi Nemoto if (has_fpu) 13501da177e4SLinus Torvalds break; 13511da177e4SLinus Torvalds if (sig) 13521da177e4SLinus Torvalds break; 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds cond_resched(); 13551da177e4SLinus Torvalds } while (xcp->cp0_epc > prevepc); 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds /* SIGILL indicates a non-fpu instruction */ 13581da177e4SLinus Torvalds if (sig == SIGILL && xcp->cp0_epc != oldepc) 13591da177e4SLinus Torvalds /* but if epc has advanced, then ignore it */ 13601da177e4SLinus Torvalds sig = 0; 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds return sig; 13631da177e4SLinus Torvalds } 136483fd38caSAtsushi Nemoto 136583fd38caSAtsushi Nemoto #ifdef CONFIG_DEBUG_FS 1366b6ee75edSDavid Daney 1367b6ee75edSDavid Daney static int fpuemu_stat_get(void *data, u64 *val) 1368b6ee75edSDavid Daney { 1369b6ee75edSDavid Daney int cpu; 1370b6ee75edSDavid Daney unsigned long sum = 0; 1371b6ee75edSDavid Daney for_each_online_cpu(cpu) { 1372b6ee75edSDavid Daney struct mips_fpu_emulator_stats *ps; 1373b6ee75edSDavid Daney local_t *pv; 1374b6ee75edSDavid Daney ps = &per_cpu(fpuemustats, cpu); 1375b6ee75edSDavid Daney pv = (void *)ps + (unsigned long)data; 1376b6ee75edSDavid Daney sum += local_read(pv); 1377b6ee75edSDavid Daney } 1378b6ee75edSDavid Daney *val = sum; 1379b6ee75edSDavid Daney return 0; 1380b6ee75edSDavid Daney } 1381b6ee75edSDavid Daney DEFINE_SIMPLE_ATTRIBUTE(fops_fpuemu_stat, fpuemu_stat_get, NULL, "%llu\n"); 1382b6ee75edSDavid Daney 138383fd38caSAtsushi Nemoto extern struct dentry *mips_debugfs_dir; 138483fd38caSAtsushi Nemoto static int __init debugfs_fpuemu(void) 138583fd38caSAtsushi Nemoto { 138683fd38caSAtsushi Nemoto struct dentry *d, *dir; 138783fd38caSAtsushi Nemoto 138883fd38caSAtsushi Nemoto if (!mips_debugfs_dir) 138983fd38caSAtsushi Nemoto return -ENODEV; 139083fd38caSAtsushi Nemoto dir = debugfs_create_dir("fpuemustats", mips_debugfs_dir); 1391ecab1f44SZhaolei if (!dir) 1392ecab1f44SZhaolei return -ENOMEM; 1393b6ee75edSDavid Daney 1394b6ee75edSDavid Daney #define FPU_STAT_CREATE(M) \ 1395b6ee75edSDavid Daney do { \ 1396b6ee75edSDavid Daney d = debugfs_create_file(#M , S_IRUGO, dir, \ 1397b6ee75edSDavid Daney (void *)offsetof(struct mips_fpu_emulator_stats, M), \ 1398b6ee75edSDavid Daney &fops_fpuemu_stat); \ 1399b6ee75edSDavid Daney if (!d) \ 1400b6ee75edSDavid Daney return -ENOMEM; \ 1401b6ee75edSDavid Daney } while (0) 1402b6ee75edSDavid Daney 1403b6ee75edSDavid Daney FPU_STAT_CREATE(emulated); 1404b6ee75edSDavid Daney FPU_STAT_CREATE(loads); 1405b6ee75edSDavid Daney FPU_STAT_CREATE(stores); 1406b6ee75edSDavid Daney FPU_STAT_CREATE(cp1ops); 1407b6ee75edSDavid Daney FPU_STAT_CREATE(cp1xops); 1408b6ee75edSDavid Daney FPU_STAT_CREATE(errors); 1409b6ee75edSDavid Daney 141083fd38caSAtsushi Nemoto return 0; 141183fd38caSAtsushi Nemoto } 141283fd38caSAtsushi Nemoto __initcall(debugfs_fpuemu); 141383fd38caSAtsushi Nemoto #endif 1414