11da177e4SLinus Torvalds /* 23f7cac41SRalf Baechle * 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., 213f7cac41SRalf Baechle * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 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 283f7cac41SRalf Baechle * 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 * 333f7cac41SRalf Baechle * 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> 3783fd38caSAtsushi Nemoto #include <linux/debugfs.h> 3885c51c51SRalf Baechle #include <linux/percpu-defs.h> 397f788d2dSDeng-Cheng Zhu #include <linux/perf_event.h> 401da177e4SLinus Torvalds 41cd8ee345SRalf Baechle #include <asm/branch.h> 421da177e4SLinus Torvalds #include <asm/inst.h> 431da177e4SLinus Torvalds #include <asm/ptrace.h> 441da177e4SLinus Torvalds #include <asm/signal.h> 457c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 46cd8ee345SRalf Baechle 47f6843626SMaciej W. Rozycki #include <asm/cpu-info.h> 48cd8ee345SRalf Baechle #include <asm/processor.h> 491da177e4SLinus Torvalds #include <asm/fpu_emulator.h> 50102cedc3SLeonid Yegoshin #include <asm/fpu.h> 51b0a668fbSLeonid Yegoshin #include <asm/mips-r2-to-r6-emul.h> 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds #include "ieee754.h" 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds /* Function which emulates a floating point instruction. */ 561da177e4SLinus Torvalds 57eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *, 581da177e4SLinus Torvalds mips_instruction); 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds static int fpux_emu(struct pt_regs *, 61515b029dSDavid Daney struct mips_fpu_struct *, mips_instruction, void *__user *); 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds /* Control registers */ 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds #define FPCREG_RID 0 /* $0 = revision id */ 66c491cfa2SMaciej W. Rozycki #define FPCREG_FCCR 25 /* $25 = fccr */ 67c491cfa2SMaciej W. Rozycki #define FPCREG_FEXR 26 /* $26 = fexr */ 68c491cfa2SMaciej W. Rozycki #define FPCREG_FENR 28 /* $28 = fenr */ 691da177e4SLinus Torvalds #define FPCREG_CSR 31 /* $31 = csr */ 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds /* convert condition code register number to csr bit */ 72b0a668fbSLeonid Yegoshin const unsigned int fpucondbit[8] = { 73c491cfa2SMaciej W. Rozycki FPU_CSR_COND, 741da177e4SLinus Torvalds FPU_CSR_COND1, 751da177e4SLinus Torvalds FPU_CSR_COND2, 761da177e4SLinus Torvalds FPU_CSR_COND3, 771da177e4SLinus Torvalds FPU_CSR_COND4, 781da177e4SLinus Torvalds FPU_CSR_COND5, 791da177e4SLinus Torvalds FPU_CSR_COND6, 801da177e4SLinus Torvalds FPU_CSR_COND7 811da177e4SLinus Torvalds }; 821da177e4SLinus Torvalds 83102cedc3SLeonid Yegoshin /* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */ 84102cedc3SLeonid Yegoshin static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0}; 85102cedc3SLeonid Yegoshin static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0}; 86102cedc3SLeonid Yegoshin static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0}; 87102cedc3SLeonid Yegoshin static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0}; 88102cedc3SLeonid Yegoshin 89102cedc3SLeonid Yegoshin /* 90102cedc3SLeonid Yegoshin * This functions translates a 32-bit microMIPS instruction 91102cedc3SLeonid Yegoshin * into a 32-bit MIPS32 instruction. Returns 0 on success 92102cedc3SLeonid Yegoshin * and SIGILL otherwise. 93102cedc3SLeonid Yegoshin */ 94102cedc3SLeonid Yegoshin static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr) 95102cedc3SLeonid Yegoshin { 96102cedc3SLeonid Yegoshin union mips_instruction insn = *insn_ptr; 97102cedc3SLeonid Yegoshin union mips_instruction mips32_insn = insn; 98102cedc3SLeonid Yegoshin int func, fmt, op; 99102cedc3SLeonid Yegoshin 100102cedc3SLeonid Yegoshin switch (insn.mm_i_format.opcode) { 101102cedc3SLeonid Yegoshin case mm_ldc132_op: 102102cedc3SLeonid Yegoshin mips32_insn.mm_i_format.opcode = ldc1_op; 103102cedc3SLeonid Yegoshin mips32_insn.mm_i_format.rt = insn.mm_i_format.rs; 104102cedc3SLeonid Yegoshin mips32_insn.mm_i_format.rs = insn.mm_i_format.rt; 105102cedc3SLeonid Yegoshin break; 106102cedc3SLeonid Yegoshin case mm_lwc132_op: 107102cedc3SLeonid Yegoshin mips32_insn.mm_i_format.opcode = lwc1_op; 108102cedc3SLeonid Yegoshin mips32_insn.mm_i_format.rt = insn.mm_i_format.rs; 109102cedc3SLeonid Yegoshin mips32_insn.mm_i_format.rs = insn.mm_i_format.rt; 110102cedc3SLeonid Yegoshin break; 111102cedc3SLeonid Yegoshin case mm_sdc132_op: 112102cedc3SLeonid Yegoshin mips32_insn.mm_i_format.opcode = sdc1_op; 113102cedc3SLeonid Yegoshin mips32_insn.mm_i_format.rt = insn.mm_i_format.rs; 114102cedc3SLeonid Yegoshin mips32_insn.mm_i_format.rs = insn.mm_i_format.rt; 115102cedc3SLeonid Yegoshin break; 116102cedc3SLeonid Yegoshin case mm_swc132_op: 117102cedc3SLeonid Yegoshin mips32_insn.mm_i_format.opcode = swc1_op; 118102cedc3SLeonid Yegoshin mips32_insn.mm_i_format.rt = insn.mm_i_format.rs; 119102cedc3SLeonid Yegoshin mips32_insn.mm_i_format.rs = insn.mm_i_format.rt; 120102cedc3SLeonid Yegoshin break; 121102cedc3SLeonid Yegoshin case mm_pool32i_op: 122102cedc3SLeonid Yegoshin /* NOTE: offset is << by 1 if in microMIPS mode. */ 123102cedc3SLeonid Yegoshin if ((insn.mm_i_format.rt == mm_bc1f_op) || 124102cedc3SLeonid Yegoshin (insn.mm_i_format.rt == mm_bc1t_op)) { 125102cedc3SLeonid Yegoshin mips32_insn.fb_format.opcode = cop1_op; 126102cedc3SLeonid Yegoshin mips32_insn.fb_format.bc = bc_op; 127102cedc3SLeonid Yegoshin mips32_insn.fb_format.flag = 128102cedc3SLeonid Yegoshin (insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0; 129102cedc3SLeonid Yegoshin } else 130102cedc3SLeonid Yegoshin return SIGILL; 131102cedc3SLeonid Yegoshin break; 132102cedc3SLeonid Yegoshin case mm_pool32f_op: 133102cedc3SLeonid Yegoshin switch (insn.mm_fp0_format.func) { 134102cedc3SLeonid Yegoshin case mm_32f_01_op: 135102cedc3SLeonid Yegoshin case mm_32f_11_op: 136102cedc3SLeonid Yegoshin case mm_32f_02_op: 137102cedc3SLeonid Yegoshin case mm_32f_12_op: 138102cedc3SLeonid Yegoshin case mm_32f_41_op: 139102cedc3SLeonid Yegoshin case mm_32f_51_op: 140102cedc3SLeonid Yegoshin case mm_32f_42_op: 141102cedc3SLeonid Yegoshin case mm_32f_52_op: 142102cedc3SLeonid Yegoshin op = insn.mm_fp0_format.func; 143102cedc3SLeonid Yegoshin if (op == mm_32f_01_op) 144102cedc3SLeonid Yegoshin func = madd_s_op; 145102cedc3SLeonid Yegoshin else if (op == mm_32f_11_op) 146102cedc3SLeonid Yegoshin func = madd_d_op; 147102cedc3SLeonid Yegoshin else if (op == mm_32f_02_op) 148102cedc3SLeonid Yegoshin func = nmadd_s_op; 149102cedc3SLeonid Yegoshin else if (op == mm_32f_12_op) 150102cedc3SLeonid Yegoshin func = nmadd_d_op; 151102cedc3SLeonid Yegoshin else if (op == mm_32f_41_op) 152102cedc3SLeonid Yegoshin func = msub_s_op; 153102cedc3SLeonid Yegoshin else if (op == mm_32f_51_op) 154102cedc3SLeonid Yegoshin func = msub_d_op; 155102cedc3SLeonid Yegoshin else if (op == mm_32f_42_op) 156102cedc3SLeonid Yegoshin func = nmsub_s_op; 157102cedc3SLeonid Yegoshin else 158102cedc3SLeonid Yegoshin func = nmsub_d_op; 159102cedc3SLeonid Yegoshin mips32_insn.fp6_format.opcode = cop1x_op; 160102cedc3SLeonid Yegoshin mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr; 161102cedc3SLeonid Yegoshin mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft; 162102cedc3SLeonid Yegoshin mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs; 163102cedc3SLeonid Yegoshin mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd; 164102cedc3SLeonid Yegoshin mips32_insn.fp6_format.func = func; 165102cedc3SLeonid Yegoshin break; 166102cedc3SLeonid Yegoshin case mm_32f_10_op: 167102cedc3SLeonid Yegoshin func = -1; /* Invalid */ 168102cedc3SLeonid Yegoshin op = insn.mm_fp5_format.op & 0x7; 169102cedc3SLeonid Yegoshin if (op == mm_ldxc1_op) 170102cedc3SLeonid Yegoshin func = ldxc1_op; 171102cedc3SLeonid Yegoshin else if (op == mm_sdxc1_op) 172102cedc3SLeonid Yegoshin func = sdxc1_op; 173102cedc3SLeonid Yegoshin else if (op == mm_lwxc1_op) 174102cedc3SLeonid Yegoshin func = lwxc1_op; 175102cedc3SLeonid Yegoshin else if (op == mm_swxc1_op) 176102cedc3SLeonid Yegoshin func = swxc1_op; 177102cedc3SLeonid Yegoshin 178102cedc3SLeonid Yegoshin if (func != -1) { 179102cedc3SLeonid Yegoshin mips32_insn.r_format.opcode = cop1x_op; 180102cedc3SLeonid Yegoshin mips32_insn.r_format.rs = 181102cedc3SLeonid Yegoshin insn.mm_fp5_format.base; 182102cedc3SLeonid Yegoshin mips32_insn.r_format.rt = 183102cedc3SLeonid Yegoshin insn.mm_fp5_format.index; 184102cedc3SLeonid Yegoshin mips32_insn.r_format.rd = 0; 185102cedc3SLeonid Yegoshin mips32_insn.r_format.re = insn.mm_fp5_format.fd; 186102cedc3SLeonid Yegoshin mips32_insn.r_format.func = func; 187102cedc3SLeonid Yegoshin } else 188102cedc3SLeonid Yegoshin return SIGILL; 189102cedc3SLeonid Yegoshin break; 190102cedc3SLeonid Yegoshin case mm_32f_40_op: 191102cedc3SLeonid Yegoshin op = -1; /* Invalid */ 192102cedc3SLeonid Yegoshin if (insn.mm_fp2_format.op == mm_fmovt_op) 193102cedc3SLeonid Yegoshin op = 1; 194102cedc3SLeonid Yegoshin else if (insn.mm_fp2_format.op == mm_fmovf_op) 195102cedc3SLeonid Yegoshin op = 0; 196102cedc3SLeonid Yegoshin if (op != -1) { 197102cedc3SLeonid Yegoshin mips32_insn.fp0_format.opcode = cop1_op; 198102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fmt = 199102cedc3SLeonid Yegoshin sdps_format[insn.mm_fp2_format.fmt]; 200102cedc3SLeonid Yegoshin mips32_insn.fp0_format.ft = 201102cedc3SLeonid Yegoshin (insn.mm_fp2_format.cc<<2) + op; 202102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fs = 203102cedc3SLeonid Yegoshin insn.mm_fp2_format.fs; 204102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fd = 205102cedc3SLeonid Yegoshin insn.mm_fp2_format.fd; 206102cedc3SLeonid Yegoshin mips32_insn.fp0_format.func = fmovc_op; 207102cedc3SLeonid Yegoshin } else 208102cedc3SLeonid Yegoshin return SIGILL; 209102cedc3SLeonid Yegoshin break; 210102cedc3SLeonid Yegoshin case mm_32f_60_op: 211102cedc3SLeonid Yegoshin func = -1; /* Invalid */ 212102cedc3SLeonid Yegoshin if (insn.mm_fp0_format.op == mm_fadd_op) 213102cedc3SLeonid Yegoshin func = fadd_op; 214102cedc3SLeonid Yegoshin else if (insn.mm_fp0_format.op == mm_fsub_op) 215102cedc3SLeonid Yegoshin func = fsub_op; 216102cedc3SLeonid Yegoshin else if (insn.mm_fp0_format.op == mm_fmul_op) 217102cedc3SLeonid Yegoshin func = fmul_op; 218102cedc3SLeonid Yegoshin else if (insn.mm_fp0_format.op == mm_fdiv_op) 219102cedc3SLeonid Yegoshin func = fdiv_op; 220102cedc3SLeonid Yegoshin if (func != -1) { 221102cedc3SLeonid Yegoshin mips32_insn.fp0_format.opcode = cop1_op; 222102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fmt = 223102cedc3SLeonid Yegoshin sdps_format[insn.mm_fp0_format.fmt]; 224102cedc3SLeonid Yegoshin mips32_insn.fp0_format.ft = 225102cedc3SLeonid Yegoshin insn.mm_fp0_format.ft; 226102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fs = 227102cedc3SLeonid Yegoshin insn.mm_fp0_format.fs; 228102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fd = 229102cedc3SLeonid Yegoshin insn.mm_fp0_format.fd; 230102cedc3SLeonid Yegoshin mips32_insn.fp0_format.func = func; 231102cedc3SLeonid Yegoshin } else 232102cedc3SLeonid Yegoshin return SIGILL; 233102cedc3SLeonid Yegoshin break; 234102cedc3SLeonid Yegoshin case mm_32f_70_op: 235102cedc3SLeonid Yegoshin func = -1; /* Invalid */ 236102cedc3SLeonid Yegoshin if (insn.mm_fp0_format.op == mm_fmovn_op) 237102cedc3SLeonid Yegoshin func = fmovn_op; 238102cedc3SLeonid Yegoshin else if (insn.mm_fp0_format.op == mm_fmovz_op) 239102cedc3SLeonid Yegoshin func = fmovz_op; 240102cedc3SLeonid Yegoshin if (func != -1) { 241102cedc3SLeonid Yegoshin mips32_insn.fp0_format.opcode = cop1_op; 242102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fmt = 243102cedc3SLeonid Yegoshin sdps_format[insn.mm_fp0_format.fmt]; 244102cedc3SLeonid Yegoshin mips32_insn.fp0_format.ft = 245102cedc3SLeonid Yegoshin insn.mm_fp0_format.ft; 246102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fs = 247102cedc3SLeonid Yegoshin insn.mm_fp0_format.fs; 248102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fd = 249102cedc3SLeonid Yegoshin insn.mm_fp0_format.fd; 250102cedc3SLeonid Yegoshin mips32_insn.fp0_format.func = func; 251102cedc3SLeonid Yegoshin } else 252102cedc3SLeonid Yegoshin return SIGILL; 253102cedc3SLeonid Yegoshin break; 254102cedc3SLeonid Yegoshin case mm_32f_73_op: /* POOL32FXF */ 255102cedc3SLeonid Yegoshin switch (insn.mm_fp1_format.op) { 256102cedc3SLeonid Yegoshin case mm_movf0_op: 257102cedc3SLeonid Yegoshin case mm_movf1_op: 258102cedc3SLeonid Yegoshin case mm_movt0_op: 259102cedc3SLeonid Yegoshin case mm_movt1_op: 260102cedc3SLeonid Yegoshin if ((insn.mm_fp1_format.op & 0x7f) == 261102cedc3SLeonid Yegoshin mm_movf0_op) 262102cedc3SLeonid Yegoshin op = 0; 263102cedc3SLeonid Yegoshin else 264102cedc3SLeonid Yegoshin op = 1; 265102cedc3SLeonid Yegoshin mips32_insn.r_format.opcode = spec_op; 266102cedc3SLeonid Yegoshin mips32_insn.r_format.rs = insn.mm_fp4_format.fs; 267102cedc3SLeonid Yegoshin mips32_insn.r_format.rt = 268102cedc3SLeonid Yegoshin (insn.mm_fp4_format.cc << 2) + op; 269102cedc3SLeonid Yegoshin mips32_insn.r_format.rd = insn.mm_fp4_format.rt; 270102cedc3SLeonid Yegoshin mips32_insn.r_format.re = 0; 271102cedc3SLeonid Yegoshin mips32_insn.r_format.func = movc_op; 272102cedc3SLeonid Yegoshin break; 273102cedc3SLeonid Yegoshin case mm_fcvtd0_op: 274102cedc3SLeonid Yegoshin case mm_fcvtd1_op: 275102cedc3SLeonid Yegoshin case mm_fcvts0_op: 276102cedc3SLeonid Yegoshin case mm_fcvts1_op: 277102cedc3SLeonid Yegoshin if ((insn.mm_fp1_format.op & 0x7f) == 278102cedc3SLeonid Yegoshin mm_fcvtd0_op) { 279102cedc3SLeonid Yegoshin func = fcvtd_op; 280102cedc3SLeonid Yegoshin fmt = swl_format[insn.mm_fp3_format.fmt]; 281102cedc3SLeonid Yegoshin } else { 282102cedc3SLeonid Yegoshin func = fcvts_op; 283102cedc3SLeonid Yegoshin fmt = dwl_format[insn.mm_fp3_format.fmt]; 284102cedc3SLeonid Yegoshin } 285102cedc3SLeonid Yegoshin mips32_insn.fp0_format.opcode = cop1_op; 286102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fmt = fmt; 287102cedc3SLeonid Yegoshin mips32_insn.fp0_format.ft = 0; 288102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fs = 289102cedc3SLeonid Yegoshin insn.mm_fp3_format.fs; 290102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fd = 291102cedc3SLeonid Yegoshin insn.mm_fp3_format.rt; 292102cedc3SLeonid Yegoshin mips32_insn.fp0_format.func = func; 293102cedc3SLeonid Yegoshin break; 294102cedc3SLeonid Yegoshin case mm_fmov0_op: 295102cedc3SLeonid Yegoshin case mm_fmov1_op: 296102cedc3SLeonid Yegoshin case mm_fabs0_op: 297102cedc3SLeonid Yegoshin case mm_fabs1_op: 298102cedc3SLeonid Yegoshin case mm_fneg0_op: 299102cedc3SLeonid Yegoshin case mm_fneg1_op: 300102cedc3SLeonid Yegoshin if ((insn.mm_fp1_format.op & 0x7f) == 301102cedc3SLeonid Yegoshin mm_fmov0_op) 302102cedc3SLeonid Yegoshin func = fmov_op; 303102cedc3SLeonid Yegoshin else if ((insn.mm_fp1_format.op & 0x7f) == 304102cedc3SLeonid Yegoshin mm_fabs0_op) 305102cedc3SLeonid Yegoshin func = fabs_op; 306102cedc3SLeonid Yegoshin else 307102cedc3SLeonid Yegoshin func = fneg_op; 308102cedc3SLeonid Yegoshin mips32_insn.fp0_format.opcode = cop1_op; 309102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fmt = 310102cedc3SLeonid Yegoshin sdps_format[insn.mm_fp3_format.fmt]; 311102cedc3SLeonid Yegoshin mips32_insn.fp0_format.ft = 0; 312102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fs = 313102cedc3SLeonid Yegoshin insn.mm_fp3_format.fs; 314102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fd = 315102cedc3SLeonid Yegoshin insn.mm_fp3_format.rt; 316102cedc3SLeonid Yegoshin mips32_insn.fp0_format.func = func; 317102cedc3SLeonid Yegoshin break; 318102cedc3SLeonid Yegoshin case mm_ffloorl_op: 319102cedc3SLeonid Yegoshin case mm_ffloorw_op: 320102cedc3SLeonid Yegoshin case mm_fceill_op: 321102cedc3SLeonid Yegoshin case mm_fceilw_op: 322102cedc3SLeonid Yegoshin case mm_ftruncl_op: 323102cedc3SLeonid Yegoshin case mm_ftruncw_op: 324102cedc3SLeonid Yegoshin case mm_froundl_op: 325102cedc3SLeonid Yegoshin case mm_froundw_op: 326102cedc3SLeonid Yegoshin case mm_fcvtl_op: 327102cedc3SLeonid Yegoshin case mm_fcvtw_op: 328102cedc3SLeonid Yegoshin if (insn.mm_fp1_format.op == mm_ffloorl_op) 329102cedc3SLeonid Yegoshin func = ffloorl_op; 330102cedc3SLeonid Yegoshin else if (insn.mm_fp1_format.op == mm_ffloorw_op) 331102cedc3SLeonid Yegoshin func = ffloor_op; 332102cedc3SLeonid Yegoshin else if (insn.mm_fp1_format.op == mm_fceill_op) 333102cedc3SLeonid Yegoshin func = fceill_op; 334102cedc3SLeonid Yegoshin else if (insn.mm_fp1_format.op == mm_fceilw_op) 335102cedc3SLeonid Yegoshin func = fceil_op; 336102cedc3SLeonid Yegoshin else if (insn.mm_fp1_format.op == mm_ftruncl_op) 337102cedc3SLeonid Yegoshin func = ftruncl_op; 338102cedc3SLeonid Yegoshin else if (insn.mm_fp1_format.op == mm_ftruncw_op) 339102cedc3SLeonid Yegoshin func = ftrunc_op; 340102cedc3SLeonid Yegoshin else if (insn.mm_fp1_format.op == mm_froundl_op) 341102cedc3SLeonid Yegoshin func = froundl_op; 342102cedc3SLeonid Yegoshin else if (insn.mm_fp1_format.op == mm_froundw_op) 343102cedc3SLeonid Yegoshin func = fround_op; 344102cedc3SLeonid Yegoshin else if (insn.mm_fp1_format.op == mm_fcvtl_op) 345102cedc3SLeonid Yegoshin func = fcvtl_op; 346102cedc3SLeonid Yegoshin else 347102cedc3SLeonid Yegoshin func = fcvtw_op; 348102cedc3SLeonid Yegoshin mips32_insn.fp0_format.opcode = cop1_op; 349102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fmt = 350102cedc3SLeonid Yegoshin sd_format[insn.mm_fp1_format.fmt]; 351102cedc3SLeonid Yegoshin mips32_insn.fp0_format.ft = 0; 352102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fs = 353102cedc3SLeonid Yegoshin insn.mm_fp1_format.fs; 354102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fd = 355102cedc3SLeonid Yegoshin insn.mm_fp1_format.rt; 356102cedc3SLeonid Yegoshin mips32_insn.fp0_format.func = func; 357102cedc3SLeonid Yegoshin break; 358102cedc3SLeonid Yegoshin case mm_frsqrt_op: 359102cedc3SLeonid Yegoshin case mm_fsqrt_op: 360102cedc3SLeonid Yegoshin case mm_frecip_op: 361102cedc3SLeonid Yegoshin if (insn.mm_fp1_format.op == mm_frsqrt_op) 362102cedc3SLeonid Yegoshin func = frsqrt_op; 363102cedc3SLeonid Yegoshin else if (insn.mm_fp1_format.op == mm_fsqrt_op) 364102cedc3SLeonid Yegoshin func = fsqrt_op; 365102cedc3SLeonid Yegoshin else 366102cedc3SLeonid Yegoshin func = frecip_op; 367102cedc3SLeonid Yegoshin mips32_insn.fp0_format.opcode = cop1_op; 368102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fmt = 369102cedc3SLeonid Yegoshin sdps_format[insn.mm_fp1_format.fmt]; 370102cedc3SLeonid Yegoshin mips32_insn.fp0_format.ft = 0; 371102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fs = 372102cedc3SLeonid Yegoshin insn.mm_fp1_format.fs; 373102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fd = 374102cedc3SLeonid Yegoshin insn.mm_fp1_format.rt; 375102cedc3SLeonid Yegoshin mips32_insn.fp0_format.func = func; 376102cedc3SLeonid Yegoshin break; 377102cedc3SLeonid Yegoshin case mm_mfc1_op: 378102cedc3SLeonid Yegoshin case mm_mtc1_op: 379102cedc3SLeonid Yegoshin case mm_cfc1_op: 380102cedc3SLeonid Yegoshin case mm_ctc1_op: 3819355e59cSSteven J. Hill case mm_mfhc1_op: 3829355e59cSSteven J. Hill case mm_mthc1_op: 383102cedc3SLeonid Yegoshin if (insn.mm_fp1_format.op == mm_mfc1_op) 384102cedc3SLeonid Yegoshin op = mfc_op; 385102cedc3SLeonid Yegoshin else if (insn.mm_fp1_format.op == mm_mtc1_op) 386102cedc3SLeonid Yegoshin op = mtc_op; 387102cedc3SLeonid Yegoshin else if (insn.mm_fp1_format.op == mm_cfc1_op) 388102cedc3SLeonid Yegoshin op = cfc_op; 3899355e59cSSteven J. Hill else if (insn.mm_fp1_format.op == mm_ctc1_op) 390102cedc3SLeonid Yegoshin op = ctc_op; 3919355e59cSSteven J. Hill else if (insn.mm_fp1_format.op == mm_mfhc1_op) 3929355e59cSSteven J. Hill op = mfhc_op; 3939355e59cSSteven J. Hill else 3949355e59cSSteven J. Hill op = mthc_op; 395102cedc3SLeonid Yegoshin mips32_insn.fp1_format.opcode = cop1_op; 396102cedc3SLeonid Yegoshin mips32_insn.fp1_format.op = op; 397102cedc3SLeonid Yegoshin mips32_insn.fp1_format.rt = 398102cedc3SLeonid Yegoshin insn.mm_fp1_format.rt; 399102cedc3SLeonid Yegoshin mips32_insn.fp1_format.fs = 400102cedc3SLeonid Yegoshin insn.mm_fp1_format.fs; 401102cedc3SLeonid Yegoshin mips32_insn.fp1_format.fd = 0; 402102cedc3SLeonid Yegoshin mips32_insn.fp1_format.func = 0; 403102cedc3SLeonid Yegoshin break; 404102cedc3SLeonid Yegoshin default: 405102cedc3SLeonid Yegoshin return SIGILL; 406102cedc3SLeonid Yegoshin } 407102cedc3SLeonid Yegoshin break; 408102cedc3SLeonid Yegoshin case mm_32f_74_op: /* c.cond.fmt */ 409102cedc3SLeonid Yegoshin mips32_insn.fp0_format.opcode = cop1_op; 410102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fmt = 411102cedc3SLeonid Yegoshin sdps_format[insn.mm_fp4_format.fmt]; 412102cedc3SLeonid Yegoshin mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt; 413102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs; 414102cedc3SLeonid Yegoshin mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2; 415102cedc3SLeonid Yegoshin mips32_insn.fp0_format.func = 416102cedc3SLeonid Yegoshin insn.mm_fp4_format.cond | MM_MIPS32_COND_FC; 417102cedc3SLeonid Yegoshin break; 418102cedc3SLeonid Yegoshin default: 419102cedc3SLeonid Yegoshin return SIGILL; 420102cedc3SLeonid Yegoshin } 421102cedc3SLeonid Yegoshin break; 422102cedc3SLeonid Yegoshin default: 423102cedc3SLeonid Yegoshin return SIGILL; 424102cedc3SLeonid Yegoshin } 425102cedc3SLeonid Yegoshin 426102cedc3SLeonid Yegoshin *insn_ptr = mips32_insn; 427102cedc3SLeonid Yegoshin return 0; 428102cedc3SLeonid Yegoshin } 429102cedc3SLeonid Yegoshin 4301da177e4SLinus Torvalds /* 4311da177e4SLinus Torvalds * Redundant with logic already in kernel/branch.c, 4321da177e4SLinus Torvalds * embedded in compute_return_epc. At some point, 4331da177e4SLinus Torvalds * a single subroutine should be used across both 4341da177e4SLinus Torvalds * modules. 4351da177e4SLinus Torvalds */ 436432c6bacSPaul Burton int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn, 437102cedc3SLeonid Yegoshin unsigned long *contpc) 4381da177e4SLinus Torvalds { 439102cedc3SLeonid Yegoshin union mips_instruction insn = (union mips_instruction)dec_insn.insn; 440102cedc3SLeonid Yegoshin unsigned int fcr31; 441102cedc3SLeonid Yegoshin unsigned int bit = 0; 4428bcd84a4SDouglas Leung unsigned int bit0; 4438bcd84a4SDouglas Leung union fpureg *fpr; 444102cedc3SLeonid Yegoshin 445102cedc3SLeonid Yegoshin switch (insn.i_format.opcode) { 4461da177e4SLinus Torvalds case spec_op: 447102cedc3SLeonid Yegoshin switch (insn.r_format.func) { 4481da177e4SLinus Torvalds case jalr_op: 449ab4a92e6SPaul Burton if (insn.r_format.rd != 0) { 450102cedc3SLeonid Yegoshin regs->regs[insn.r_format.rd] = 451102cedc3SLeonid Yegoshin regs->cp0_epc + dec_insn.pc_inc + 452102cedc3SLeonid Yegoshin dec_insn.next_pc_inc; 453ab4a92e6SPaul Burton } 454102cedc3SLeonid Yegoshin /* Fall through */ 4551da177e4SLinus Torvalds case jr_op: 4565f9f41c4SMarkos Chandras /* For R6, JR already emulated in jalr_op */ 457143fefc8SMarkos Chandras if (NO_R6EMU && insn.r_format.func == jr_op) 4585f9f41c4SMarkos Chandras break; 459102cedc3SLeonid Yegoshin *contpc = regs->regs[insn.r_format.rs]; 4601da177e4SLinus Torvalds return 1; 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds break; 4631da177e4SLinus Torvalds case bcond_op: 464102cedc3SLeonid Yegoshin switch (insn.i_format.rt) { 4651da177e4SLinus Torvalds case bltzal_op: 4661da177e4SLinus Torvalds case bltzall_op: 467319824eaSMarkos Chandras if (NO_R6EMU && (insn.i_format.rs || 468319824eaSMarkos Chandras insn.i_format.rt == bltzall_op)) 469319824eaSMarkos Chandras break; 470319824eaSMarkos Chandras 471102cedc3SLeonid Yegoshin regs->regs[31] = regs->cp0_epc + 472102cedc3SLeonid Yegoshin dec_insn.pc_inc + 473102cedc3SLeonid Yegoshin dec_insn.next_pc_inc; 474102cedc3SLeonid Yegoshin /* Fall through */ 475102cedc3SLeonid Yegoshin case bltzl_op: 476319824eaSMarkos Chandras if (NO_R6EMU) 477319824eaSMarkos Chandras break; 478319824eaSMarkos Chandras case bltz_op: 479102cedc3SLeonid Yegoshin if ((long)regs->regs[insn.i_format.rs] < 0) 480102cedc3SLeonid Yegoshin *contpc = regs->cp0_epc + 481102cedc3SLeonid Yegoshin dec_insn.pc_inc + 482102cedc3SLeonid Yegoshin (insn.i_format.simmediate << 2); 483102cedc3SLeonid Yegoshin else 484102cedc3SLeonid Yegoshin *contpc = regs->cp0_epc + 485102cedc3SLeonid Yegoshin dec_insn.pc_inc + 486102cedc3SLeonid Yegoshin dec_insn.next_pc_inc; 4871da177e4SLinus Torvalds return 1; 488102cedc3SLeonid Yegoshin case bgezal_op: 489102cedc3SLeonid Yegoshin case bgezall_op: 490319824eaSMarkos Chandras if (NO_R6EMU && (insn.i_format.rs || 491319824eaSMarkos Chandras insn.i_format.rt == bgezall_op)) 492319824eaSMarkos Chandras break; 493319824eaSMarkos Chandras 494102cedc3SLeonid Yegoshin regs->regs[31] = regs->cp0_epc + 495102cedc3SLeonid Yegoshin dec_insn.pc_inc + 496102cedc3SLeonid Yegoshin dec_insn.next_pc_inc; 497102cedc3SLeonid Yegoshin /* Fall through */ 498102cedc3SLeonid Yegoshin case bgezl_op: 499319824eaSMarkos Chandras if (NO_R6EMU) 500319824eaSMarkos Chandras break; 501319824eaSMarkos Chandras case bgez_op: 502102cedc3SLeonid Yegoshin if ((long)regs->regs[insn.i_format.rs] >= 0) 503102cedc3SLeonid Yegoshin *contpc = regs->cp0_epc + 504102cedc3SLeonid Yegoshin dec_insn.pc_inc + 505102cedc3SLeonid Yegoshin (insn.i_format.simmediate << 2); 506102cedc3SLeonid Yegoshin else 507102cedc3SLeonid Yegoshin *contpc = regs->cp0_epc + 508102cedc3SLeonid Yegoshin dec_insn.pc_inc + 509102cedc3SLeonid Yegoshin dec_insn.next_pc_inc; 510102cedc3SLeonid Yegoshin return 1; 5111da177e4SLinus Torvalds } 5121da177e4SLinus Torvalds break; 5131da177e4SLinus Torvalds case jalx_op: 514102cedc3SLeonid Yegoshin set_isa16_mode(bit); 515102cedc3SLeonid Yegoshin case jal_op: 516102cedc3SLeonid Yegoshin regs->regs[31] = regs->cp0_epc + 517102cedc3SLeonid Yegoshin dec_insn.pc_inc + 518102cedc3SLeonid Yegoshin dec_insn.next_pc_inc; 519102cedc3SLeonid Yegoshin /* Fall through */ 520102cedc3SLeonid Yegoshin case j_op: 521102cedc3SLeonid Yegoshin *contpc = regs->cp0_epc + dec_insn.pc_inc; 522102cedc3SLeonid Yegoshin *contpc >>= 28; 523102cedc3SLeonid Yegoshin *contpc <<= 28; 524102cedc3SLeonid Yegoshin *contpc |= (insn.j_format.target << 2); 525102cedc3SLeonid Yegoshin /* Set microMIPS mode bit: XOR for jalx. */ 526102cedc3SLeonid Yegoshin *contpc ^= bit; 5271da177e4SLinus Torvalds return 1; 528102cedc3SLeonid Yegoshin case beql_op: 529319824eaSMarkos Chandras if (NO_R6EMU) 530319824eaSMarkos Chandras break; 531319824eaSMarkos Chandras case beq_op: 532102cedc3SLeonid Yegoshin if (regs->regs[insn.i_format.rs] == 533102cedc3SLeonid Yegoshin regs->regs[insn.i_format.rt]) 534102cedc3SLeonid Yegoshin *contpc = regs->cp0_epc + 535102cedc3SLeonid Yegoshin dec_insn.pc_inc + 536102cedc3SLeonid Yegoshin (insn.i_format.simmediate << 2); 537102cedc3SLeonid Yegoshin else 538102cedc3SLeonid Yegoshin *contpc = regs->cp0_epc + 539102cedc3SLeonid Yegoshin dec_insn.pc_inc + 540102cedc3SLeonid Yegoshin dec_insn.next_pc_inc; 541102cedc3SLeonid Yegoshin return 1; 542102cedc3SLeonid Yegoshin case bnel_op: 543319824eaSMarkos Chandras if (NO_R6EMU) 544319824eaSMarkos Chandras break; 545319824eaSMarkos Chandras case bne_op: 546102cedc3SLeonid Yegoshin if (regs->regs[insn.i_format.rs] != 547102cedc3SLeonid Yegoshin regs->regs[insn.i_format.rt]) 548102cedc3SLeonid Yegoshin *contpc = regs->cp0_epc + 549102cedc3SLeonid Yegoshin dec_insn.pc_inc + 550102cedc3SLeonid Yegoshin (insn.i_format.simmediate << 2); 551102cedc3SLeonid Yegoshin else 552102cedc3SLeonid Yegoshin *contpc = regs->cp0_epc + 553102cedc3SLeonid Yegoshin dec_insn.pc_inc + 554102cedc3SLeonid Yegoshin dec_insn.next_pc_inc; 555102cedc3SLeonid Yegoshin return 1; 556102cedc3SLeonid Yegoshin case blezl_op: 557e9d92d22SMarkos Chandras if (!insn.i_format.rt && NO_R6EMU) 558319824eaSMarkos Chandras break; 559319824eaSMarkos Chandras case blez_op: 560a8ff66f5SMarkos Chandras 561a8ff66f5SMarkos Chandras /* 562a8ff66f5SMarkos Chandras * Compact branches for R6 for the 563a8ff66f5SMarkos Chandras * blez and blezl opcodes. 564a8ff66f5SMarkos Chandras * BLEZ | rs = 0 | rt != 0 == BLEZALC 565a8ff66f5SMarkos Chandras * BLEZ | rs = rt != 0 == BGEZALC 566a8ff66f5SMarkos Chandras * BLEZ | rs != 0 | rt != 0 == BGEUC 567a8ff66f5SMarkos Chandras * BLEZL | rs = 0 | rt != 0 == BLEZC 568a8ff66f5SMarkos Chandras * BLEZL | rs = rt != 0 == BGEZC 569a8ff66f5SMarkos Chandras * BLEZL | rs != 0 | rt != 0 == BGEC 570a8ff66f5SMarkos Chandras * 571a8ff66f5SMarkos Chandras * For real BLEZ{,L}, rt is always 0. 572a8ff66f5SMarkos Chandras */ 573a8ff66f5SMarkos Chandras if (cpu_has_mips_r6 && insn.i_format.rt) { 574a8ff66f5SMarkos Chandras if ((insn.i_format.opcode == blez_op) && 575a8ff66f5SMarkos Chandras ((!insn.i_format.rs && insn.i_format.rt) || 576a8ff66f5SMarkos Chandras (insn.i_format.rs == insn.i_format.rt))) 577a8ff66f5SMarkos Chandras regs->regs[31] = regs->cp0_epc + 578a8ff66f5SMarkos Chandras dec_insn.pc_inc; 579a8ff66f5SMarkos Chandras *contpc = regs->cp0_epc + dec_insn.pc_inc + 580a8ff66f5SMarkos Chandras dec_insn.next_pc_inc; 581a8ff66f5SMarkos Chandras 582a8ff66f5SMarkos Chandras return 1; 583a8ff66f5SMarkos Chandras } 584102cedc3SLeonid Yegoshin if ((long)regs->regs[insn.i_format.rs] <= 0) 585102cedc3SLeonid Yegoshin *contpc = regs->cp0_epc + 586102cedc3SLeonid Yegoshin dec_insn.pc_inc + 587102cedc3SLeonid Yegoshin (insn.i_format.simmediate << 2); 588102cedc3SLeonid Yegoshin else 589102cedc3SLeonid Yegoshin *contpc = regs->cp0_epc + 590102cedc3SLeonid Yegoshin dec_insn.pc_inc + 591102cedc3SLeonid Yegoshin dec_insn.next_pc_inc; 592102cedc3SLeonid Yegoshin return 1; 593102cedc3SLeonid Yegoshin case bgtzl_op: 594e9d92d22SMarkos Chandras if (!insn.i_format.rt && NO_R6EMU) 595319824eaSMarkos Chandras break; 596319824eaSMarkos Chandras case bgtz_op: 597f1b44067SMarkos Chandras /* 598f1b44067SMarkos Chandras * Compact branches for R6 for the 599f1b44067SMarkos Chandras * bgtz and bgtzl opcodes. 600f1b44067SMarkos Chandras * BGTZ | rs = 0 | rt != 0 == BGTZALC 601f1b44067SMarkos Chandras * BGTZ | rs = rt != 0 == BLTZALC 602f1b44067SMarkos Chandras * BGTZ | rs != 0 | rt != 0 == BLTUC 603f1b44067SMarkos Chandras * BGTZL | rs = 0 | rt != 0 == BGTZC 604f1b44067SMarkos Chandras * BGTZL | rs = rt != 0 == BLTZC 605f1b44067SMarkos Chandras * BGTZL | rs != 0 | rt != 0 == BLTC 606f1b44067SMarkos Chandras * 607f1b44067SMarkos Chandras * *ZALC varint for BGTZ &&& rt != 0 608f1b44067SMarkos Chandras * For real GTZ{,L}, rt is always 0. 609f1b44067SMarkos Chandras */ 610f1b44067SMarkos Chandras if (cpu_has_mips_r6 && insn.i_format.rt) { 611f1b44067SMarkos Chandras if ((insn.i_format.opcode == blez_op) && 612f1b44067SMarkos Chandras ((!insn.i_format.rs && insn.i_format.rt) || 613f1b44067SMarkos Chandras (insn.i_format.rs == insn.i_format.rt))) 614f1b44067SMarkos Chandras regs->regs[31] = regs->cp0_epc + 615f1b44067SMarkos Chandras dec_insn.pc_inc; 616f1b44067SMarkos Chandras *contpc = regs->cp0_epc + dec_insn.pc_inc + 617f1b44067SMarkos Chandras dec_insn.next_pc_inc; 618f1b44067SMarkos Chandras 619f1b44067SMarkos Chandras return 1; 620f1b44067SMarkos Chandras } 621f1b44067SMarkos Chandras 622102cedc3SLeonid Yegoshin if ((long)regs->regs[insn.i_format.rs] > 0) 623102cedc3SLeonid Yegoshin *contpc = regs->cp0_epc + 624102cedc3SLeonid Yegoshin dec_insn.pc_inc + 625102cedc3SLeonid Yegoshin (insn.i_format.simmediate << 2); 626102cedc3SLeonid Yegoshin else 627102cedc3SLeonid Yegoshin *contpc = regs->cp0_epc + 628102cedc3SLeonid Yegoshin dec_insn.pc_inc + 629102cedc3SLeonid Yegoshin dec_insn.next_pc_inc; 630102cedc3SLeonid Yegoshin return 1; 6311b492600SPaul Burton case pop10_op: 6321b492600SPaul Burton case pop30_op: 633c893ce38SMarkos Chandras if (!cpu_has_mips_r6) 634c893ce38SMarkos Chandras break; 635c893ce38SMarkos Chandras if (insn.i_format.rt && !insn.i_format.rs) 636c893ce38SMarkos Chandras regs->regs[31] = regs->cp0_epc + 4; 637c893ce38SMarkos Chandras *contpc = regs->cp0_epc + dec_insn.pc_inc + 638c893ce38SMarkos Chandras dec_insn.next_pc_inc; 639c893ce38SMarkos Chandras 640c893ce38SMarkos Chandras return 1; 641c26d4219SDavid Daney #ifdef CONFIG_CPU_CAVIUM_OCTEON 642c26d4219SDavid Daney case lwc2_op: /* This is bbit0 on Octeon */ 643c26d4219SDavid Daney if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0) 644c26d4219SDavid Daney *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2); 645c26d4219SDavid Daney else 646c26d4219SDavid Daney *contpc = regs->cp0_epc + 8; 647c26d4219SDavid Daney return 1; 648c26d4219SDavid Daney case ldc2_op: /* This is bbit032 on Octeon */ 649c26d4219SDavid Daney if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0) 650c26d4219SDavid Daney *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2); 651c26d4219SDavid Daney else 652c26d4219SDavid Daney *contpc = regs->cp0_epc + 8; 653c26d4219SDavid Daney return 1; 654c26d4219SDavid Daney case swc2_op: /* This is bbit1 on Octeon */ 655c26d4219SDavid Daney if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) 656c26d4219SDavid Daney *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2); 657c26d4219SDavid Daney else 658c26d4219SDavid Daney *contpc = regs->cp0_epc + 8; 659c26d4219SDavid Daney return 1; 660c26d4219SDavid Daney case sdc2_op: /* This is bbit132 on Octeon */ 661c26d4219SDavid Daney if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) 662c26d4219SDavid Daney *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2); 663c26d4219SDavid Daney else 664c26d4219SDavid Daney *contpc = regs->cp0_epc + 8; 665c26d4219SDavid Daney return 1; 6668467ca01SMarkos Chandras #else 6678467ca01SMarkos Chandras case bc6_op: 6688467ca01SMarkos Chandras /* 6698467ca01SMarkos Chandras * Only valid for MIPS R6 but we can still end up 6708467ca01SMarkos Chandras * here from a broken userland so just tell emulator 6718467ca01SMarkos Chandras * this is not a branch and let it break later on. 6728467ca01SMarkos Chandras */ 6738467ca01SMarkos Chandras if (!cpu_has_mips_r6) 6748467ca01SMarkos Chandras break; 6758467ca01SMarkos Chandras *contpc = regs->cp0_epc + dec_insn.pc_inc + 6768467ca01SMarkos Chandras dec_insn.next_pc_inc; 6778467ca01SMarkos Chandras 6788467ca01SMarkos Chandras return 1; 67984fef630SMarkos Chandras case balc6_op: 68084fef630SMarkos Chandras if (!cpu_has_mips_r6) 68184fef630SMarkos Chandras break; 68284fef630SMarkos Chandras regs->regs[31] = regs->cp0_epc + 4; 68384fef630SMarkos Chandras *contpc = regs->cp0_epc + dec_insn.pc_inc + 68484fef630SMarkos Chandras dec_insn.next_pc_inc; 68584fef630SMarkos Chandras 68684fef630SMarkos Chandras return 1; 6871c66b79bSPaul Burton case pop66_op: 68869b9a2fdSMarkos Chandras if (!cpu_has_mips_r6) 68969b9a2fdSMarkos Chandras break; 69069b9a2fdSMarkos Chandras *contpc = regs->cp0_epc + dec_insn.pc_inc + 69169b9a2fdSMarkos Chandras dec_insn.next_pc_inc; 69269b9a2fdSMarkos Chandras 69369b9a2fdSMarkos Chandras return 1; 6941c66b79bSPaul Burton case pop76_op: 69528d6f93dSMarkos Chandras if (!cpu_has_mips_r6) 69628d6f93dSMarkos Chandras break; 69728d6f93dSMarkos Chandras if (!insn.i_format.rs) 69828d6f93dSMarkos Chandras regs->regs[31] = regs->cp0_epc + 4; 69928d6f93dSMarkos Chandras *contpc = regs->cp0_epc + dec_insn.pc_inc + 70028d6f93dSMarkos Chandras dec_insn.next_pc_inc; 70128d6f93dSMarkos Chandras 70228d6f93dSMarkos Chandras return 1; 703c26d4219SDavid Daney #endif 7041da177e4SLinus Torvalds case cop0_op: 7051da177e4SLinus Torvalds case cop1_op: 706c8a34581SMarkos Chandras /* Need to check for R6 bc1nez and bc1eqz branches */ 707c8a34581SMarkos Chandras if (cpu_has_mips_r6 && 708c8a34581SMarkos Chandras ((insn.i_format.rs == bc1eqz_op) || 709c8a34581SMarkos Chandras (insn.i_format.rs == bc1nez_op))) { 710c8a34581SMarkos Chandras bit = 0; 7118bcd84a4SDouglas Leung fpr = ¤t->thread.fpu.fpr[insn.i_format.rt]; 7128bcd84a4SDouglas Leung bit0 = get_fpr32(fpr, 0) & 0x1; 713c8a34581SMarkos Chandras switch (insn.i_format.rs) { 714c8a34581SMarkos Chandras case bc1eqz_op: 7158bcd84a4SDouglas Leung bit = bit0 == 0; 716c8a34581SMarkos Chandras break; 717c8a34581SMarkos Chandras case bc1nez_op: 7188bcd84a4SDouglas Leung bit = bit0 != 0; 719c8a34581SMarkos Chandras break; 720c8a34581SMarkos Chandras } 721c8a34581SMarkos Chandras if (bit) 722c8a34581SMarkos Chandras *contpc = regs->cp0_epc + 723c8a34581SMarkos Chandras dec_insn.pc_inc + 724c8a34581SMarkos Chandras (insn.i_format.simmediate << 2); 725c8a34581SMarkos Chandras else 726c8a34581SMarkos Chandras *contpc = regs->cp0_epc + 727c8a34581SMarkos Chandras dec_insn.pc_inc + 728c8a34581SMarkos Chandras dec_insn.next_pc_inc; 729c8a34581SMarkos Chandras 730c8a34581SMarkos Chandras return 1; 731c8a34581SMarkos Chandras } 732c8a34581SMarkos Chandras /* R2/R6 compatible cop1 instruction. Fall through */ 7331da177e4SLinus Torvalds case cop2_op: 7341da177e4SLinus Torvalds case cop1x_op: 735102cedc3SLeonid Yegoshin if (insn.i_format.rs == bc_op) { 736102cedc3SLeonid Yegoshin preempt_disable(); 737102cedc3SLeonid Yegoshin if (is_fpu_owner()) 738842dfc11SManuel Lauss fcr31 = read_32bit_cp1_register(CP1_STATUS); 739102cedc3SLeonid Yegoshin else 740102cedc3SLeonid Yegoshin fcr31 = current->thread.fpu.fcr31; 741102cedc3SLeonid Yegoshin preempt_enable(); 742102cedc3SLeonid Yegoshin 743102cedc3SLeonid Yegoshin bit = (insn.i_format.rt >> 2); 744102cedc3SLeonid Yegoshin bit += (bit != 0); 745102cedc3SLeonid Yegoshin bit += 23; 746102cedc3SLeonid Yegoshin switch (insn.i_format.rt & 3) { 747102cedc3SLeonid Yegoshin case 0: /* bc1f */ 748102cedc3SLeonid Yegoshin case 2: /* bc1fl */ 749102cedc3SLeonid Yegoshin if (~fcr31 & (1 << bit)) 750102cedc3SLeonid Yegoshin *contpc = regs->cp0_epc + 751102cedc3SLeonid Yegoshin dec_insn.pc_inc + 752102cedc3SLeonid Yegoshin (insn.i_format.simmediate << 2); 753102cedc3SLeonid Yegoshin else 754102cedc3SLeonid Yegoshin *contpc = regs->cp0_epc + 755102cedc3SLeonid Yegoshin dec_insn.pc_inc + 756102cedc3SLeonid Yegoshin dec_insn.next_pc_inc; 757102cedc3SLeonid Yegoshin return 1; 758102cedc3SLeonid Yegoshin case 1: /* bc1t */ 759102cedc3SLeonid Yegoshin case 3: /* bc1tl */ 760102cedc3SLeonid Yegoshin if (fcr31 & (1 << bit)) 761102cedc3SLeonid Yegoshin *contpc = regs->cp0_epc + 762102cedc3SLeonid Yegoshin dec_insn.pc_inc + 763102cedc3SLeonid Yegoshin (insn.i_format.simmediate << 2); 764102cedc3SLeonid Yegoshin else 765102cedc3SLeonid Yegoshin *contpc = regs->cp0_epc + 766102cedc3SLeonid Yegoshin dec_insn.pc_inc + 767102cedc3SLeonid Yegoshin dec_insn.next_pc_inc; 7681da177e4SLinus Torvalds return 1; 7691da177e4SLinus Torvalds } 770102cedc3SLeonid Yegoshin } 771102cedc3SLeonid Yegoshin break; 772102cedc3SLeonid Yegoshin } 7731da177e4SLinus Torvalds return 0; 7741da177e4SLinus Torvalds } 7751da177e4SLinus Torvalds 7761da177e4SLinus Torvalds /* 7771da177e4SLinus Torvalds * In the Linux kernel, we support selection of FPR format on the 778da0bac33SDavid Daney * basis of the Status.FR bit. If an FPU is not present, the FR bit 779da0bac33SDavid Daney * is hardwired to zero, which would imply a 32-bit FPU even for 780597ce172SPaul Burton * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS. 78151d943f0SRalf Baechle * FPU emu is slow and bulky and optimizing this function offers fairly 78251d943f0SRalf Baechle * sizeable benefits so we try to be clever and make this function return 78351d943f0SRalf Baechle * a constant whenever possible, that is on 64-bit kernels without O32 784597ce172SPaul Burton * compatibility enabled and on 32-bit without 64-bit FPU support. 7851da177e4SLinus Torvalds */ 786da0bac33SDavid Daney static inline int cop1_64bit(struct pt_regs *xcp) 787da0bac33SDavid Daney { 78897f2645fSMasahiro Yamada if (IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_MIPS32_O32)) 78951d943f0SRalf Baechle return 1; 79097f2645fSMasahiro Yamada else if (IS_ENABLED(CONFIG_32BIT) && 79197f2645fSMasahiro Yamada !IS_ENABLED(CONFIG_MIPS_O32_FP64_SUPPORT)) 792da0bac33SDavid Daney return 0; 79308a07904SRalf Baechle 794597ce172SPaul Burton return !test_thread_flag(TIF_32BIT_FPREGS); 795da0bac33SDavid Daney } 7961da177e4SLinus Torvalds 7974227a2d4SPaul Burton static inline bool hybrid_fprs(void) 7984227a2d4SPaul Burton { 7994227a2d4SPaul Burton return test_thread_flag(TIF_HYBRID_FPREGS); 8004227a2d4SPaul Burton } 8014227a2d4SPaul Burton 80247fa0c02SRalf Baechle #define SIFROMREG(si, x) \ 80347fa0c02SRalf Baechle do { \ 8044227a2d4SPaul Burton if (cop1_64bit(xcp) && !hybrid_fprs()) \ 805c8c0da6bSPaul Burton (si) = (int)get_fpr32(&ctx->fpr[x], 0); \ 806bbd426f5SPaul Burton else \ 807c8c0da6bSPaul Burton (si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \ 808bbd426f5SPaul Burton } while (0) 809da0bac33SDavid Daney 81047fa0c02SRalf Baechle #define SITOREG(si, x) \ 81147fa0c02SRalf Baechle do { \ 8124227a2d4SPaul Burton if (cop1_64bit(xcp) && !hybrid_fprs()) { \ 813ef1c47afSPaul Burton unsigned i; \ 814bbd426f5SPaul Burton set_fpr32(&ctx->fpr[x], 0, si); \ 815ef1c47afSPaul Burton for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \ 816ef1c47afSPaul Burton set_fpr32(&ctx->fpr[x], i, 0); \ 817ef1c47afSPaul Burton } else { \ 818bbd426f5SPaul Burton set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \ 819ef1c47afSPaul Burton } \ 820bbd426f5SPaul Burton } while (0) 8211da177e4SLinus Torvalds 822c8c0da6bSPaul Burton #define SIFROMHREG(si, x) ((si) = (int)get_fpr32(&ctx->fpr[x], 1)) 823ef1c47afSPaul Burton 82447fa0c02SRalf Baechle #define SITOHREG(si, x) \ 82547fa0c02SRalf Baechle do { \ 826ef1c47afSPaul Burton unsigned i; \ 827ef1c47afSPaul Burton set_fpr32(&ctx->fpr[x], 1, si); \ 828ef1c47afSPaul Burton for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \ 829ef1c47afSPaul Burton set_fpr32(&ctx->fpr[x], i, 0); \ 830ef1c47afSPaul Burton } while (0) 8311ac94400SLeonid Yegoshin 832bbd426f5SPaul Burton #define DIFROMREG(di, x) \ 833bbd426f5SPaul Burton ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0)) 834bbd426f5SPaul Burton 83547fa0c02SRalf Baechle #define DITOREG(di, x) \ 83647fa0c02SRalf Baechle do { \ 837ef1c47afSPaul Burton unsigned fpr, i; \ 838ef1c47afSPaul Burton fpr = (x) & ~(cop1_64bit(xcp) == 0); \ 839ef1c47afSPaul Burton set_fpr64(&ctx->fpr[fpr], 0, di); \ 840ef1c47afSPaul Burton for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \ 841ef1c47afSPaul Burton set_fpr64(&ctx->fpr[fpr], i, 0); \ 842ef1c47afSPaul Burton } while (0) 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x) 8451da177e4SLinus Torvalds #define SPTOREG(sp, x) SITOREG((sp).bits, x) 8461da177e4SLinus Torvalds #define DPFROMREG(dp, x) DIFROMREG((dp).bits, x) 8471da177e4SLinus Torvalds #define DPTOREG(dp, x) DITOREG((dp).bits, x) 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds /* 850d4f5b088SMaciej W. Rozycki * Emulate a CFC1 instruction. 851d4f5b088SMaciej W. Rozycki */ 852d4f5b088SMaciej W. Rozycki static inline void cop1_cfc(struct pt_regs *xcp, struct mips_fpu_struct *ctx, 853d4f5b088SMaciej W. Rozycki mips_instruction ir) 854d4f5b088SMaciej W. Rozycki { 855c491cfa2SMaciej W. Rozycki u32 fcr31 = ctx->fcr31; 856c491cfa2SMaciej W. Rozycki u32 value = 0; 857d4f5b088SMaciej W. Rozycki 858c491cfa2SMaciej W. Rozycki switch (MIPSInst_RD(ir)) { 859c491cfa2SMaciej W. Rozycki case FPCREG_CSR: 860c491cfa2SMaciej W. Rozycki value = fcr31; 861d4f5b088SMaciej W. Rozycki pr_debug("%p gpr[%d]<-csr=%08x\n", 862c491cfa2SMaciej W. Rozycki (void *)xcp->cp0_epc, MIPSInst_RT(ir), value); 863c491cfa2SMaciej W. Rozycki break; 864c491cfa2SMaciej W. Rozycki 865c491cfa2SMaciej W. Rozycki case FPCREG_FENR: 866c491cfa2SMaciej W. Rozycki if (!cpu_has_mips_r) 867c491cfa2SMaciej W. Rozycki break; 868c491cfa2SMaciej W. Rozycki value = (fcr31 >> (FPU_CSR_FS_S - MIPS_FENR_FS_S)) & 869c491cfa2SMaciej W. Rozycki MIPS_FENR_FS; 870c491cfa2SMaciej W. Rozycki value |= fcr31 & (FPU_CSR_ALL_E | FPU_CSR_RM); 871c491cfa2SMaciej W. Rozycki pr_debug("%p gpr[%d]<-enr=%08x\n", 872c491cfa2SMaciej W. Rozycki (void *)xcp->cp0_epc, MIPSInst_RT(ir), value); 873c491cfa2SMaciej W. Rozycki break; 874c491cfa2SMaciej W. Rozycki 875c491cfa2SMaciej W. Rozycki case FPCREG_FEXR: 876c491cfa2SMaciej W. Rozycki if (!cpu_has_mips_r) 877c491cfa2SMaciej W. Rozycki break; 878c491cfa2SMaciej W. Rozycki value = fcr31 & (FPU_CSR_ALL_X | FPU_CSR_ALL_S); 879c491cfa2SMaciej W. Rozycki pr_debug("%p gpr[%d]<-exr=%08x\n", 880c491cfa2SMaciej W. Rozycki (void *)xcp->cp0_epc, MIPSInst_RT(ir), value); 881c491cfa2SMaciej W. Rozycki break; 882c491cfa2SMaciej W. Rozycki 883c491cfa2SMaciej W. Rozycki case FPCREG_FCCR: 884c491cfa2SMaciej W. Rozycki if (!cpu_has_mips_r) 885c491cfa2SMaciej W. Rozycki break; 886c491cfa2SMaciej W. Rozycki value = (fcr31 >> (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) & 887c491cfa2SMaciej W. Rozycki MIPS_FCCR_COND0; 888c491cfa2SMaciej W. Rozycki value |= (fcr31 >> (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) & 889c491cfa2SMaciej W. Rozycki (MIPS_FCCR_CONDX & ~MIPS_FCCR_COND0); 890c491cfa2SMaciej W. Rozycki pr_debug("%p gpr[%d]<-ccr=%08x\n", 891c491cfa2SMaciej W. Rozycki (void *)xcp->cp0_epc, MIPSInst_RT(ir), value); 892c491cfa2SMaciej W. Rozycki break; 893c491cfa2SMaciej W. Rozycki 894c491cfa2SMaciej W. Rozycki case FPCREG_RID: 89503dce595SMaciej W. Rozycki value = boot_cpu_data.fpu_id; 896c491cfa2SMaciej W. Rozycki break; 897c491cfa2SMaciej W. Rozycki 898c491cfa2SMaciej W. Rozycki default: 899c491cfa2SMaciej W. Rozycki break; 900c491cfa2SMaciej W. Rozycki } 901c491cfa2SMaciej W. Rozycki 902d4f5b088SMaciej W. Rozycki if (MIPSInst_RT(ir)) 903d4f5b088SMaciej W. Rozycki xcp->regs[MIPSInst_RT(ir)] = value; 904d4f5b088SMaciej W. Rozycki } 905d4f5b088SMaciej W. Rozycki 906d4f5b088SMaciej W. Rozycki /* 907d4f5b088SMaciej W. Rozycki * Emulate a CTC1 instruction. 908d4f5b088SMaciej W. Rozycki */ 909d4f5b088SMaciej W. Rozycki static inline void cop1_ctc(struct pt_regs *xcp, struct mips_fpu_struct *ctx, 910d4f5b088SMaciej W. Rozycki mips_instruction ir) 911d4f5b088SMaciej W. Rozycki { 912c491cfa2SMaciej W. Rozycki u32 fcr31 = ctx->fcr31; 913d4f5b088SMaciej W. Rozycki u32 value; 9149b26616cSMaciej W. Rozycki u32 mask; 915d4f5b088SMaciej W. Rozycki 916d4f5b088SMaciej W. Rozycki if (MIPSInst_RT(ir) == 0) 917d4f5b088SMaciej W. Rozycki value = 0; 918d4f5b088SMaciej W. Rozycki else 919d4f5b088SMaciej W. Rozycki value = xcp->regs[MIPSInst_RT(ir)]; 920d4f5b088SMaciej W. Rozycki 921c491cfa2SMaciej W. Rozycki switch (MIPSInst_RD(ir)) { 922c491cfa2SMaciej W. Rozycki case FPCREG_CSR: 923d4f5b088SMaciej W. Rozycki pr_debug("%p gpr[%d]->csr=%08x\n", 924c491cfa2SMaciej W. Rozycki (void *)xcp->cp0_epc, MIPSInst_RT(ir), value); 925d4f5b088SMaciej W. Rozycki 9269b26616cSMaciej W. Rozycki /* Preserve read-only bits. */ 92703dce595SMaciej W. Rozycki mask = boot_cpu_data.fpu_msk31; 9289b26616cSMaciej W. Rozycki fcr31 = (value & ~mask) | (fcr31 & mask); 929c491cfa2SMaciej W. Rozycki break; 930c491cfa2SMaciej W. Rozycki 931c491cfa2SMaciej W. Rozycki case FPCREG_FENR: 932c491cfa2SMaciej W. Rozycki if (!cpu_has_mips_r) 933c491cfa2SMaciej W. Rozycki break; 934c491cfa2SMaciej W. Rozycki pr_debug("%p gpr[%d]->enr=%08x\n", 935c491cfa2SMaciej W. Rozycki (void *)xcp->cp0_epc, MIPSInst_RT(ir), value); 936c491cfa2SMaciej W. Rozycki fcr31 &= ~(FPU_CSR_FS | FPU_CSR_ALL_E | FPU_CSR_RM); 937c491cfa2SMaciej W. Rozycki fcr31 |= (value << (FPU_CSR_FS_S - MIPS_FENR_FS_S)) & 938c491cfa2SMaciej W. Rozycki FPU_CSR_FS; 939c491cfa2SMaciej W. Rozycki fcr31 |= value & (FPU_CSR_ALL_E | FPU_CSR_RM); 940c491cfa2SMaciej W. Rozycki break; 941c491cfa2SMaciej W. Rozycki 942c491cfa2SMaciej W. Rozycki case FPCREG_FEXR: 943c491cfa2SMaciej W. Rozycki if (!cpu_has_mips_r) 944c491cfa2SMaciej W. Rozycki break; 945c491cfa2SMaciej W. Rozycki pr_debug("%p gpr[%d]->exr=%08x\n", 946c491cfa2SMaciej W. Rozycki (void *)xcp->cp0_epc, MIPSInst_RT(ir), value); 947c491cfa2SMaciej W. Rozycki fcr31 &= ~(FPU_CSR_ALL_X | FPU_CSR_ALL_S); 948c491cfa2SMaciej W. Rozycki fcr31 |= value & (FPU_CSR_ALL_X | FPU_CSR_ALL_S); 949c491cfa2SMaciej W. Rozycki break; 950c491cfa2SMaciej W. Rozycki 951c491cfa2SMaciej W. Rozycki case FPCREG_FCCR: 952c491cfa2SMaciej W. Rozycki if (!cpu_has_mips_r) 953c491cfa2SMaciej W. Rozycki break; 954c491cfa2SMaciej W. Rozycki pr_debug("%p gpr[%d]->ccr=%08x\n", 955c491cfa2SMaciej W. Rozycki (void *)xcp->cp0_epc, MIPSInst_RT(ir), value); 956c491cfa2SMaciej W. Rozycki fcr31 &= ~(FPU_CSR_CONDX | FPU_CSR_COND); 957c491cfa2SMaciej W. Rozycki fcr31 |= (value << (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) & 958c491cfa2SMaciej W. Rozycki FPU_CSR_COND; 959c491cfa2SMaciej W. Rozycki fcr31 |= (value << (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) & 960c491cfa2SMaciej W. Rozycki FPU_CSR_CONDX; 961c491cfa2SMaciej W. Rozycki break; 962c491cfa2SMaciej W. Rozycki 963c491cfa2SMaciej W. Rozycki default: 964c491cfa2SMaciej W. Rozycki break; 965d4f5b088SMaciej W. Rozycki } 966c491cfa2SMaciej W. Rozycki 967c491cfa2SMaciej W. Rozycki ctx->fcr31 = fcr31; 968d4f5b088SMaciej W. Rozycki } 969d4f5b088SMaciej W. Rozycki 970d4f5b088SMaciej W. Rozycki /* 9711da177e4SLinus Torvalds * Emulate the single floating point instruction pointed at by EPC. 9721da177e4SLinus Torvalds * Two instructions if the instruction is in a branch delay slot. 9731da177e4SLinus Torvalds */ 9741da177e4SLinus Torvalds 975515b029dSDavid Daney static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx, 976102cedc3SLeonid Yegoshin struct mm_decoded_insn dec_insn, void *__user *fault_addr) 9771da177e4SLinus Torvalds { 978102cedc3SLeonid Yegoshin unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc; 97993583e17SPaul Burton unsigned int cond, cbit, bit0; 9803f7cac41SRalf Baechle mips_instruction ir; 9813f7cac41SRalf Baechle int likely, pc_inc; 98293583e17SPaul Burton union fpureg *fpr; 9833f7cac41SRalf Baechle u32 __user *wva; 9843f7cac41SRalf Baechle u64 __user *dva; 9853f7cac41SRalf Baechle u32 wval; 9863f7cac41SRalf Baechle u64 dval; 9873f7cac41SRalf Baechle int sig; 9881da177e4SLinus Torvalds 98970e4c234SRalf Baechle /* 99070e4c234SRalf Baechle * These are giving gcc a gentle hint about what to expect in 99170e4c234SRalf Baechle * dec_inst in order to do better optimization. 99270e4c234SRalf Baechle */ 99370e4c234SRalf Baechle if (!cpu_has_mmips && dec_insn.micro_mips_mode) 99470e4c234SRalf Baechle unreachable(); 99570e4c234SRalf Baechle 9961da177e4SLinus Torvalds /* XXX NEC Vr54xx bug workaround */ 997e7e9cae5SRalf Baechle if (delay_slot(xcp)) { 998102cedc3SLeonid Yegoshin if (dec_insn.micro_mips_mode) { 999102cedc3SLeonid Yegoshin if (!mm_isBranchInstr(xcp, dec_insn, &contpc)) 1000e7e9cae5SRalf Baechle clear_delay_slot(xcp); 1001102cedc3SLeonid Yegoshin } else { 1002102cedc3SLeonid Yegoshin if (!isBranchInstr(xcp, dec_insn, &contpc)) 1003e7e9cae5SRalf Baechle clear_delay_slot(xcp); 1004102cedc3SLeonid Yegoshin } 1005102cedc3SLeonid Yegoshin } 10061da177e4SLinus Torvalds 1007e7e9cae5SRalf Baechle if (delay_slot(xcp)) { 10081da177e4SLinus Torvalds /* 10091da177e4SLinus Torvalds * The instruction to be emulated is in a branch delay slot 10101da177e4SLinus Torvalds * which means that we have to emulate the branch instruction 10111da177e4SLinus Torvalds * BEFORE we do the cop1 instruction. 10121da177e4SLinus Torvalds * 10131da177e4SLinus Torvalds * This branch could be a COP1 branch, but in that case we 10141da177e4SLinus Torvalds * would have had a trap for that instruction, and would not 10151da177e4SLinus Torvalds * come through this route. 10161da177e4SLinus Torvalds * 10171da177e4SLinus Torvalds * Linux MIPS branch emulator operates on context, updating the 10181da177e4SLinus Torvalds * cp0_epc. 10191da177e4SLinus Torvalds */ 1020102cedc3SLeonid Yegoshin ir = dec_insn.next_insn; /* process delay slot instr */ 1021102cedc3SLeonid Yegoshin pc_inc = dec_insn.next_pc_inc; 1022333d1f67SRalf Baechle } else { 1023102cedc3SLeonid Yegoshin ir = dec_insn.insn; /* process current instr */ 1024102cedc3SLeonid Yegoshin pc_inc = dec_insn.pc_inc; 1025102cedc3SLeonid Yegoshin } 1026102cedc3SLeonid Yegoshin 1027102cedc3SLeonid Yegoshin /* 1028102cedc3SLeonid Yegoshin * Since microMIPS FPU instructios are a subset of MIPS32 FPU 1029102cedc3SLeonid Yegoshin * instructions, we want to convert microMIPS FPU instructions 1030102cedc3SLeonid Yegoshin * into MIPS32 instructions so that we could reuse all of the 1031102cedc3SLeonid Yegoshin * FPU emulation code. 1032102cedc3SLeonid Yegoshin * 1033102cedc3SLeonid Yegoshin * NOTE: We cannot do this for branch instructions since they 1034102cedc3SLeonid Yegoshin * are not a subset. Example: Cannot emulate a 16-bit 1035102cedc3SLeonid Yegoshin * aligned target address with a MIPS32 instruction. 1036102cedc3SLeonid Yegoshin */ 1037102cedc3SLeonid Yegoshin if (dec_insn.micro_mips_mode) { 1038102cedc3SLeonid Yegoshin /* 1039102cedc3SLeonid Yegoshin * If next instruction is a 16-bit instruction, then it 1040102cedc3SLeonid Yegoshin * it cannot be a FPU instruction. This could happen 1041102cedc3SLeonid Yegoshin * since we can be called for non-FPU instructions. 1042102cedc3SLeonid Yegoshin */ 1043102cedc3SLeonid Yegoshin if ((pc_inc == 2) || 1044102cedc3SLeonid Yegoshin (microMIPS32_to_MIPS32((union mips_instruction *)&ir) 1045102cedc3SLeonid Yegoshin == SIGILL)) 1046102cedc3SLeonid Yegoshin return SIGILL; 10471da177e4SLinus Torvalds } 10481da177e4SLinus Torvalds 10491da177e4SLinus Torvalds emul: 1050a8b0ca17SPeter Zijlstra perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0); 1051b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(emulated); 10521da177e4SLinus Torvalds switch (MIPSInst_OPCODE(ir)) { 10533f7cac41SRalf Baechle case ldc1_op: 10543f7cac41SRalf Baechle dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] + 10551da177e4SLinus Torvalds MIPSInst_SIMM(ir)); 1056b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(loads); 1057515b029dSDavid Daney 10583f7cac41SRalf Baechle if (!access_ok(VERIFY_READ, dva, sizeof(u64))) { 1059b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 10603f7cac41SRalf Baechle *fault_addr = dva; 10611da177e4SLinus Torvalds return SIGBUS; 10621da177e4SLinus Torvalds } 10633f7cac41SRalf Baechle if (__get_user(dval, dva)) { 1064515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 10653f7cac41SRalf Baechle *fault_addr = dva; 1066515b029dSDavid Daney return SIGSEGV; 1067515b029dSDavid Daney } 10683f7cac41SRalf Baechle DITOREG(dval, MIPSInst_RT(ir)); 10691da177e4SLinus Torvalds break; 10701da177e4SLinus Torvalds 10713f7cac41SRalf Baechle case sdc1_op: 10723f7cac41SRalf Baechle dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] + 10731da177e4SLinus Torvalds MIPSInst_SIMM(ir)); 1074b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(stores); 10753f7cac41SRalf Baechle DIFROMREG(dval, MIPSInst_RT(ir)); 10763f7cac41SRalf Baechle if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) { 1077b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 10783f7cac41SRalf Baechle *fault_addr = dva; 10791da177e4SLinus Torvalds return SIGBUS; 10801da177e4SLinus Torvalds } 10813f7cac41SRalf Baechle if (__put_user(dval, dva)) { 1082515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 10833f7cac41SRalf Baechle *fault_addr = dva; 1084515b029dSDavid Daney return SIGSEGV; 1085515b029dSDavid Daney } 10861da177e4SLinus Torvalds break; 10871da177e4SLinus Torvalds 10883f7cac41SRalf Baechle case lwc1_op: 10893f7cac41SRalf Baechle wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] + 10901da177e4SLinus Torvalds MIPSInst_SIMM(ir)); 1091b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(loads); 10923f7cac41SRalf Baechle if (!access_ok(VERIFY_READ, wva, sizeof(u32))) { 1093b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 10943f7cac41SRalf Baechle *fault_addr = wva; 10951da177e4SLinus Torvalds return SIGBUS; 10961da177e4SLinus Torvalds } 10973f7cac41SRalf Baechle if (__get_user(wval, wva)) { 1098515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 10993f7cac41SRalf Baechle *fault_addr = wva; 1100515b029dSDavid Daney return SIGSEGV; 1101515b029dSDavid Daney } 11023f7cac41SRalf Baechle SITOREG(wval, MIPSInst_RT(ir)); 11031da177e4SLinus Torvalds break; 11041da177e4SLinus Torvalds 11053f7cac41SRalf Baechle case swc1_op: 11063f7cac41SRalf Baechle wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] + 11071da177e4SLinus Torvalds MIPSInst_SIMM(ir)); 1108b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(stores); 11093f7cac41SRalf Baechle SIFROMREG(wval, MIPSInst_RT(ir)); 11103f7cac41SRalf Baechle if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) { 1111b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 11123f7cac41SRalf Baechle *fault_addr = wva; 11131da177e4SLinus Torvalds return SIGBUS; 11141da177e4SLinus Torvalds } 11153f7cac41SRalf Baechle if (__put_user(wval, wva)) { 1116515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 11173f7cac41SRalf Baechle *fault_addr = wva; 1118515b029dSDavid Daney return SIGSEGV; 1119515b029dSDavid Daney } 11201da177e4SLinus Torvalds break; 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds case cop1_op: 11231da177e4SLinus Torvalds switch (MIPSInst_RS(ir)) { 11241da177e4SLinus Torvalds case dmfc_op: 112508a07904SRalf Baechle if (!cpu_has_mips_3_4_5 && !cpu_has_mips64) 112608a07904SRalf Baechle return SIGILL; 112708a07904SRalf Baechle 11281da177e4SLinus Torvalds /* copregister fs -> gpr[rt] */ 11291da177e4SLinus Torvalds if (MIPSInst_RT(ir) != 0) { 11301da177e4SLinus Torvalds DIFROMREG(xcp->regs[MIPSInst_RT(ir)], 11311da177e4SLinus Torvalds MIPSInst_RD(ir)); 11321da177e4SLinus Torvalds } 11331da177e4SLinus Torvalds break; 11341da177e4SLinus Torvalds 11351da177e4SLinus Torvalds case dmtc_op: 113608a07904SRalf Baechle if (!cpu_has_mips_3_4_5 && !cpu_has_mips64) 113708a07904SRalf Baechle return SIGILL; 113808a07904SRalf Baechle 11391da177e4SLinus Torvalds /* copregister fs <- rt */ 11401da177e4SLinus Torvalds DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir)); 11411da177e4SLinus Torvalds break; 11421da177e4SLinus Torvalds 11431ac94400SLeonid Yegoshin case mfhc_op: 1144e8f80cc1SMarkos Chandras if (!cpu_has_mips_r2_r6) 11451ac94400SLeonid Yegoshin goto sigill; 11461ac94400SLeonid Yegoshin 11471ac94400SLeonid Yegoshin /* copregister rd -> gpr[rt] */ 11481ac94400SLeonid Yegoshin if (MIPSInst_RT(ir) != 0) { 11491ac94400SLeonid Yegoshin SIFROMHREG(xcp->regs[MIPSInst_RT(ir)], 11501ac94400SLeonid Yegoshin MIPSInst_RD(ir)); 11511ac94400SLeonid Yegoshin } 11521ac94400SLeonid Yegoshin break; 11531ac94400SLeonid Yegoshin 11541ac94400SLeonid Yegoshin case mthc_op: 1155e8f80cc1SMarkos Chandras if (!cpu_has_mips_r2_r6) 11561ac94400SLeonid Yegoshin goto sigill; 11571ac94400SLeonid Yegoshin 11581ac94400SLeonid Yegoshin /* copregister rd <- gpr[rt] */ 11591ac94400SLeonid Yegoshin SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir)); 11601ac94400SLeonid Yegoshin break; 11611ac94400SLeonid Yegoshin 11621da177e4SLinus Torvalds case mfc_op: 11631da177e4SLinus Torvalds /* copregister rd -> gpr[rt] */ 11641da177e4SLinus Torvalds if (MIPSInst_RT(ir) != 0) { 11651da177e4SLinus Torvalds SIFROMREG(xcp->regs[MIPSInst_RT(ir)], 11661da177e4SLinus Torvalds MIPSInst_RD(ir)); 11671da177e4SLinus Torvalds } 11681da177e4SLinus Torvalds break; 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds case mtc_op: 11711da177e4SLinus Torvalds /* copregister rd <- rt */ 11721da177e4SLinus Torvalds SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir)); 11731da177e4SLinus Torvalds break; 11741da177e4SLinus Torvalds 11753f7cac41SRalf Baechle case cfc_op: 11761da177e4SLinus Torvalds /* cop control register rd -> gpr[rt] */ 1177d4f5b088SMaciej W. Rozycki cop1_cfc(xcp, ctx, ir); 11781da177e4SLinus Torvalds break; 11791da177e4SLinus Torvalds 11803f7cac41SRalf Baechle case ctc_op: 11811da177e4SLinus Torvalds /* copregister rd <- rt */ 1182d4f5b088SMaciej W. Rozycki cop1_ctc(xcp, ctx, ir); 11831da177e4SLinus Torvalds if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) { 11841da177e4SLinus Torvalds return SIGFPE; 11851da177e4SLinus Torvalds } 11861da177e4SLinus Torvalds break; 11871da177e4SLinus Torvalds 1188c909ca71SMarkos Chandras case bc1eqz_op: 1189c909ca71SMarkos Chandras case bc1nez_op: 1190c909ca71SMarkos Chandras if (!cpu_has_mips_r6 || delay_slot(xcp)) 1191c909ca71SMarkos Chandras return SIGILL; 1192c909ca71SMarkos Chandras 1193c909ca71SMarkos Chandras cond = likely = 0; 119493583e17SPaul Burton fpr = ¤t->thread.fpu.fpr[MIPSInst_RT(ir)]; 119593583e17SPaul Burton bit0 = get_fpr32(fpr, 0) & 0x1; 1196c909ca71SMarkos Chandras switch (MIPSInst_RS(ir)) { 1197c909ca71SMarkos Chandras case bc1eqz_op: 119893583e17SPaul Burton cond = bit0 == 0; 1199c909ca71SMarkos Chandras break; 1200c909ca71SMarkos Chandras case bc1nez_op: 120193583e17SPaul Burton cond = bit0 != 0; 1202c909ca71SMarkos Chandras break; 1203c909ca71SMarkos Chandras } 1204c909ca71SMarkos Chandras goto branch_common; 1205c909ca71SMarkos Chandras 12063f7cac41SRalf Baechle case bc_op: 1207e7e9cae5SRalf Baechle if (delay_slot(xcp)) 12081da177e4SLinus Torvalds return SIGILL; 12091da177e4SLinus Torvalds 121008a07904SRalf Baechle if (cpu_has_mips_4_5_r) 121108a07904SRalf Baechle cbit = fpucondbit[MIPSInst_RT(ir) >> 2]; 121208a07904SRalf Baechle else 121308a07904SRalf Baechle cbit = FPU_CSR_COND; 121408a07904SRalf Baechle cond = ctx->fcr31 & cbit; 121508a07904SRalf Baechle 12163f7cac41SRalf Baechle likely = 0; 12171da177e4SLinus Torvalds switch (MIPSInst_RT(ir) & 3) { 12181da177e4SLinus Torvalds case bcfl_op: 12192d83fea7SMaciej W. Rozycki if (cpu_has_mips_2_3_4_5_r) 12201da177e4SLinus Torvalds likely = 1; 12212d83fea7SMaciej W. Rozycki /* Fall through */ 12221da177e4SLinus Torvalds case bcf_op: 12231da177e4SLinus Torvalds cond = !cond; 12241da177e4SLinus Torvalds break; 12251da177e4SLinus Torvalds case bctl_op: 12262d83fea7SMaciej W. Rozycki if (cpu_has_mips_2_3_4_5_r) 12271da177e4SLinus Torvalds likely = 1; 12282d83fea7SMaciej W. Rozycki /* Fall through */ 12291da177e4SLinus Torvalds case bct_op: 12301da177e4SLinus Torvalds break; 12311da177e4SLinus Torvalds } 1232c909ca71SMarkos Chandras branch_common: 1233e7e9cae5SRalf Baechle set_delay_slot(xcp); 12341da177e4SLinus Torvalds if (cond) { 12353f7cac41SRalf Baechle /* 12363f7cac41SRalf Baechle * Branch taken: emulate dslot instruction 12371da177e4SLinus Torvalds */ 12389ab4471cSMaciej W. Rozycki unsigned long bcpc; 12399ab4471cSMaciej W. Rozycki 12409ab4471cSMaciej W. Rozycki /* 12419ab4471cSMaciej W. Rozycki * Remember EPC at the branch to point back 12429ab4471cSMaciej W. Rozycki * at so that any delay-slot instruction 12439ab4471cSMaciej W. Rozycki * signal is not silently ignored. 12449ab4471cSMaciej W. Rozycki */ 12459ab4471cSMaciej W. Rozycki bcpc = xcp->cp0_epc; 1246102cedc3SLeonid Yegoshin xcp->cp0_epc += dec_insn.pc_inc; 12471da177e4SLinus Torvalds 1248102cedc3SLeonid Yegoshin contpc = MIPSInst_SIMM(ir); 1249102cedc3SLeonid Yegoshin ir = dec_insn.next_insn; 1250102cedc3SLeonid Yegoshin if (dec_insn.micro_mips_mode) { 1251102cedc3SLeonid Yegoshin contpc = (xcp->cp0_epc + (contpc << 1)); 1252102cedc3SLeonid Yegoshin 1253102cedc3SLeonid Yegoshin /* If 16-bit instruction, not FPU. */ 1254102cedc3SLeonid Yegoshin if ((dec_insn.next_pc_inc == 2) || 1255102cedc3SLeonid Yegoshin (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) { 1256102cedc3SLeonid Yegoshin 1257102cedc3SLeonid Yegoshin /* 1258102cedc3SLeonid Yegoshin * Since this instruction will 1259102cedc3SLeonid Yegoshin * be put on the stack with 1260102cedc3SLeonid Yegoshin * 32-bit words, get around 1261102cedc3SLeonid Yegoshin * this problem by putting a 1262102cedc3SLeonid Yegoshin * NOP16 as the second one. 1263102cedc3SLeonid Yegoshin */ 1264102cedc3SLeonid Yegoshin if (dec_insn.next_pc_inc == 2) 1265102cedc3SLeonid Yegoshin ir = (ir & (~0xffff)) | MM_NOP16; 1266102cedc3SLeonid Yegoshin 1267102cedc3SLeonid Yegoshin /* 1268102cedc3SLeonid Yegoshin * Single step the non-CP1 1269102cedc3SLeonid Yegoshin * instruction in the dslot. 1270102cedc3SLeonid Yegoshin */ 12719ab4471cSMaciej W. Rozycki sig = mips_dsemul(xcp, ir, 1272432c6bacSPaul Burton bcpc, contpc); 1273e4553573SMaciej W. Rozycki if (sig < 0) 1274e4553573SMaciej W. Rozycki break; 12759ab4471cSMaciej W. Rozycki if (sig) 12769ab4471cSMaciej W. Rozycki xcp->cp0_epc = bcpc; 12779ab4471cSMaciej W. Rozycki /* 12789ab4471cSMaciej W. Rozycki * SIGILL forces out of 12799ab4471cSMaciej W. Rozycki * the emulation loop. 12809ab4471cSMaciej W. Rozycki */ 12819ab4471cSMaciej W. Rozycki return sig ? sig : SIGILL; 1282515b029dSDavid Daney } 1283102cedc3SLeonid Yegoshin } else 1284102cedc3SLeonid Yegoshin contpc = (xcp->cp0_epc + (contpc << 2)); 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds switch (MIPSInst_OPCODE(ir)) { 12871da177e4SLinus Torvalds case lwc1_op: 12881da177e4SLinus Torvalds case swc1_op: 128908a07904SRalf Baechle goto emul; 12903f7cac41SRalf Baechle 12911da177e4SLinus Torvalds case ldc1_op: 12921da177e4SLinus Torvalds case sdc1_op: 12932d83fea7SMaciej W. Rozycki if (cpu_has_mips_2_3_4_5_r) 129408a07904SRalf Baechle goto emul; 129508a07904SRalf Baechle 12969ab4471cSMaciej W. Rozycki goto bc_sigill; 12973f7cac41SRalf Baechle 12981da177e4SLinus Torvalds case cop1_op: 129908a07904SRalf Baechle goto emul; 13003f7cac41SRalf Baechle 13011da177e4SLinus Torvalds case cop1x_op: 13022d83fea7SMaciej W. Rozycki if (cpu_has_mips_4_5_64_r2_r6) 13031da177e4SLinus Torvalds /* its one of ours */ 13041da177e4SLinus Torvalds goto emul; 130508a07904SRalf Baechle 13069ab4471cSMaciej W. Rozycki goto bc_sigill; 13073f7cac41SRalf Baechle 13081da177e4SLinus Torvalds case spec_op: 13092d83fea7SMaciej W. Rozycki switch (MIPSInst_FUNC(ir)) { 13102d83fea7SMaciej W. Rozycki case movc_op: 13112d83fea7SMaciej W. Rozycki if (cpu_has_mips_4_5_r) 13121da177e4SLinus Torvalds goto emul; 13132d83fea7SMaciej W. Rozycki 13149ab4471cSMaciej W. Rozycki goto bc_sigill; 13152d83fea7SMaciej W. Rozycki } 13161da177e4SLinus Torvalds break; 13179ab4471cSMaciej W. Rozycki 13189ab4471cSMaciej W. Rozycki bc_sigill: 13199ab4471cSMaciej W. Rozycki xcp->cp0_epc = bcpc; 13209ab4471cSMaciej W. Rozycki return SIGILL; 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds 13231da177e4SLinus Torvalds /* 13241da177e4SLinus Torvalds * Single step the non-cp1 13251da177e4SLinus Torvalds * instruction in the dslot 13261da177e4SLinus Torvalds */ 1327432c6bacSPaul Burton sig = mips_dsemul(xcp, ir, bcpc, contpc); 1328e4553573SMaciej W. Rozycki if (sig < 0) 1329e4553573SMaciej W. Rozycki break; 13309ab4471cSMaciej W. Rozycki if (sig) 13319ab4471cSMaciej W. Rozycki xcp->cp0_epc = bcpc; 13329ab4471cSMaciej W. Rozycki /* SIGILL forces out of the emulation loop. */ 13339ab4471cSMaciej W. Rozycki return sig ? sig : SIGILL; 13343f7cac41SRalf Baechle } else if (likely) { /* branch not taken */ 13351da177e4SLinus Torvalds /* 13361da177e4SLinus Torvalds * branch likely nullifies 13371da177e4SLinus Torvalds * dslot if not taken 13381da177e4SLinus Torvalds */ 1339102cedc3SLeonid Yegoshin xcp->cp0_epc += dec_insn.pc_inc; 1340102cedc3SLeonid Yegoshin contpc += dec_insn.pc_inc; 13411da177e4SLinus Torvalds /* 13421da177e4SLinus Torvalds * else continue & execute 13431da177e4SLinus Torvalds * dslot as normal insn 13441da177e4SLinus Torvalds */ 13451da177e4SLinus Torvalds } 13461da177e4SLinus Torvalds break; 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds default: 13491da177e4SLinus Torvalds if (!(MIPSInst_RS(ir) & 0x10)) 13501da177e4SLinus Torvalds return SIGILL; 13511da177e4SLinus Torvalds 13521da177e4SLinus Torvalds /* a real fpu computation instruction */ 13531da177e4SLinus Torvalds if ((sig = fpu_emu(xcp, ctx, ir))) 13541da177e4SLinus Torvalds return sig; 13551da177e4SLinus Torvalds } 13561da177e4SLinus Torvalds break; 13571da177e4SLinus Torvalds 13583f7cac41SRalf Baechle case cop1x_op: 13592d83fea7SMaciej W. Rozycki if (!cpu_has_mips_4_5_64_r2_r6) 136008a07904SRalf Baechle return SIGILL; 136108a07904SRalf Baechle 136208a07904SRalf Baechle sig = fpux_emu(xcp, ctx, ir, fault_addr); 1363515b029dSDavid Daney if (sig) 13641da177e4SLinus Torvalds return sig; 13651da177e4SLinus Torvalds break; 13661da177e4SLinus Torvalds 13671da177e4SLinus Torvalds case spec_op: 136808a07904SRalf Baechle if (!cpu_has_mips_4_5_r) 136908a07904SRalf Baechle return SIGILL; 137008a07904SRalf Baechle 13711da177e4SLinus Torvalds if (MIPSInst_FUNC(ir) != movc_op) 13721da177e4SLinus Torvalds return SIGILL; 13731da177e4SLinus Torvalds cond = fpucondbit[MIPSInst_RT(ir) >> 2]; 13741da177e4SLinus Torvalds if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0)) 13751da177e4SLinus Torvalds xcp->regs[MIPSInst_RD(ir)] = 13761da177e4SLinus Torvalds xcp->regs[MIPSInst_RS(ir)]; 13771da177e4SLinus Torvalds break; 13781da177e4SLinus Torvalds default: 13791ac94400SLeonid Yegoshin sigill: 13801da177e4SLinus Torvalds return SIGILL; 13811da177e4SLinus Torvalds } 13821da177e4SLinus Torvalds 13831da177e4SLinus Torvalds /* we did it !! */ 1384e70dfc10SAtsushi Nemoto xcp->cp0_epc = contpc; 1385e7e9cae5SRalf Baechle clear_delay_slot(xcp); 1386333d1f67SRalf Baechle 13871da177e4SLinus Torvalds return 0; 13881da177e4SLinus Torvalds } 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds /* 13911da177e4SLinus Torvalds * Conversion table from MIPS compare ops 48-63 13921da177e4SLinus Torvalds * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig); 13931da177e4SLinus Torvalds */ 13941da177e4SLinus Torvalds static const unsigned char cmptab[8] = { 13951da177e4SLinus Torvalds 0, /* cmp_0 (sig) cmp_sf */ 13961da177e4SLinus Torvalds IEEE754_CUN, /* cmp_un (sig) cmp_ngle */ 13971da177e4SLinus Torvalds IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */ 13981da177e4SLinus Torvalds IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */ 13991da177e4SLinus Torvalds IEEE754_CLT, /* cmp_olt (sig) cmp_lt */ 14001da177e4SLinus Torvalds IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */ 14011da177e4SLinus Torvalds IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */ 14021da177e4SLinus Torvalds IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */ 14031da177e4SLinus Torvalds }; 14041da177e4SLinus Torvalds 1405f8c3c671SMarkos Chandras static const unsigned char negative_cmptab[8] = { 1406f8c3c671SMarkos Chandras 0, /* Reserved */ 1407f8c3c671SMarkos Chandras IEEE754_CLT | IEEE754_CGT | IEEE754_CEQ, 1408f8c3c671SMarkos Chandras IEEE754_CLT | IEEE754_CGT | IEEE754_CUN, 1409f8c3c671SMarkos Chandras IEEE754_CLT | IEEE754_CGT, 1410f8c3c671SMarkos Chandras /* Reserved */ 1411f8c3c671SMarkos Chandras }; 1412f8c3c671SMarkos Chandras 14131da177e4SLinus Torvalds 14141da177e4SLinus Torvalds /* 14151da177e4SLinus Torvalds * Additional MIPS4 instructions 14161da177e4SLinus Torvalds */ 14171da177e4SLinus Torvalds 14181da177e4SLinus Torvalds #define DEF3OP(name, p, f1, f2, f3) \ 141947fa0c02SRalf Baechle static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \ 142047fa0c02SRalf Baechle union ieee754##p s, union ieee754##p t) \ 14211da177e4SLinus Torvalds { \ 1422cd21dfcfSRalf Baechle struct _ieee754_csr ieee754_csr_save; \ 14231da177e4SLinus Torvalds s = f1(s, t); \ 14241da177e4SLinus Torvalds ieee754_csr_save = ieee754_csr; \ 14251da177e4SLinus Torvalds s = f2(s, r); \ 14261da177e4SLinus Torvalds ieee754_csr_save.cx |= ieee754_csr.cx; \ 14271da177e4SLinus Torvalds ieee754_csr_save.sx |= ieee754_csr.sx; \ 14281da177e4SLinus Torvalds s = f3(s); \ 14291da177e4SLinus Torvalds ieee754_csr.cx |= ieee754_csr_save.cx; \ 14301da177e4SLinus Torvalds ieee754_csr.sx |= ieee754_csr_save.sx; \ 14311da177e4SLinus Torvalds return s; \ 14321da177e4SLinus Torvalds } 14331da177e4SLinus Torvalds 14342209bcb1SRalf Baechle static union ieee754dp fpemu_dp_recip(union ieee754dp d) 14351da177e4SLinus Torvalds { 14361da177e4SLinus Torvalds return ieee754dp_div(ieee754dp_one(0), d); 14371da177e4SLinus Torvalds } 14381da177e4SLinus Torvalds 14392209bcb1SRalf Baechle static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d) 14401da177e4SLinus Torvalds { 14411da177e4SLinus Torvalds return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d)); 14421da177e4SLinus Torvalds } 14431da177e4SLinus Torvalds 14442209bcb1SRalf Baechle static union ieee754sp fpemu_sp_recip(union ieee754sp s) 14451da177e4SLinus Torvalds { 14461da177e4SLinus Torvalds return ieee754sp_div(ieee754sp_one(0), s); 14471da177e4SLinus Torvalds } 14481da177e4SLinus Torvalds 14492209bcb1SRalf Baechle static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s) 14501da177e4SLinus Torvalds { 14511da177e4SLinus Torvalds return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s)); 14521da177e4SLinus Torvalds } 14531da177e4SLinus Torvalds 14541da177e4SLinus Torvalds DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, ); 14551da177e4SLinus Torvalds DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, ); 14561da177e4SLinus Torvalds DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg); 14571da177e4SLinus Torvalds DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg); 14581da177e4SLinus Torvalds DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, ); 14591da177e4SLinus Torvalds DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, ); 14601da177e4SLinus Torvalds DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg); 14611da177e4SLinus Torvalds DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg); 14621da177e4SLinus Torvalds 1463eae89076SAtsushi Nemoto static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx, 1464515b029dSDavid Daney mips_instruction ir, void *__user *fault_addr) 14651da177e4SLinus Torvalds { 14661da177e4SLinus Torvalds unsigned rcsr = 0; /* resulting csr */ 14671da177e4SLinus Torvalds 1468b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(cp1xops); 14691da177e4SLinus Torvalds 14701da177e4SLinus Torvalds switch (MIPSInst_FMA_FFMT(ir)) { 14711da177e4SLinus Torvalds case s_fmt:{ /* 0 */ 14721da177e4SLinus Torvalds 14732209bcb1SRalf Baechle union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp); 14742209bcb1SRalf Baechle union ieee754sp fd, fr, fs, ft; 14753fccc015SRalf Baechle u32 __user *va; 14761da177e4SLinus Torvalds u32 val; 14771da177e4SLinus Torvalds 14781da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 14791da177e4SLinus Torvalds case lwxc1_op: 14803fccc015SRalf Baechle va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] + 14811da177e4SLinus Torvalds xcp->regs[MIPSInst_FT(ir)]); 14821da177e4SLinus Torvalds 1483b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(loads); 1484515b029dSDavid Daney if (!access_ok(VERIFY_READ, va, sizeof(u32))) { 1485b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 1486515b029dSDavid Daney *fault_addr = va; 14871da177e4SLinus Torvalds return SIGBUS; 14881da177e4SLinus Torvalds } 1489515b029dSDavid Daney if (__get_user(val, va)) { 1490515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 1491515b029dSDavid Daney *fault_addr = va; 1492515b029dSDavid Daney return SIGSEGV; 1493515b029dSDavid Daney } 14941da177e4SLinus Torvalds SITOREG(val, MIPSInst_FD(ir)); 14951da177e4SLinus Torvalds break; 14961da177e4SLinus Torvalds 14971da177e4SLinus Torvalds case swxc1_op: 14983fccc015SRalf Baechle va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] + 14991da177e4SLinus Torvalds xcp->regs[MIPSInst_FT(ir)]); 15001da177e4SLinus Torvalds 1501b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(stores); 15021da177e4SLinus Torvalds 15031da177e4SLinus Torvalds SIFROMREG(val, MIPSInst_FS(ir)); 1504515b029dSDavid Daney if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) { 1505515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 1506515b029dSDavid Daney *fault_addr = va; 1507515b029dSDavid Daney return SIGBUS; 1508515b029dSDavid Daney } 15091da177e4SLinus Torvalds if (put_user(val, va)) { 1510b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 1511515b029dSDavid Daney *fault_addr = va; 1512515b029dSDavid Daney return SIGSEGV; 15131da177e4SLinus Torvalds } 15141da177e4SLinus Torvalds break; 15151da177e4SLinus Torvalds 15161da177e4SLinus Torvalds case madd_s_op: 15171da177e4SLinus Torvalds handler = fpemu_sp_madd; 15181da177e4SLinus Torvalds goto scoptop; 15191da177e4SLinus Torvalds case msub_s_op: 15201da177e4SLinus Torvalds handler = fpemu_sp_msub; 15211da177e4SLinus Torvalds goto scoptop; 15221da177e4SLinus Torvalds case nmadd_s_op: 15231da177e4SLinus Torvalds handler = fpemu_sp_nmadd; 15241da177e4SLinus Torvalds goto scoptop; 15251da177e4SLinus Torvalds case nmsub_s_op: 15261da177e4SLinus Torvalds handler = fpemu_sp_nmsub; 15271da177e4SLinus Torvalds goto scoptop; 15281da177e4SLinus Torvalds 15291da177e4SLinus Torvalds scoptop: 15301da177e4SLinus Torvalds SPFROMREG(fr, MIPSInst_FR(ir)); 15311da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 15321da177e4SLinus Torvalds SPFROMREG(ft, MIPSInst_FT(ir)); 15331da177e4SLinus Torvalds fd = (*handler) (fr, fs, ft); 15341da177e4SLinus Torvalds SPTOREG(fd, MIPSInst_FD(ir)); 15351da177e4SLinus Torvalds 15361da177e4SLinus Torvalds copcsr: 1537c4103526SDeng-Cheng Zhu if (ieee754_cxtest(IEEE754_INEXACT)) { 1538c4103526SDeng-Cheng Zhu MIPS_FPU_EMU_INC_STATS(ieee754_inexact); 15391da177e4SLinus Torvalds rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S; 1540c4103526SDeng-Cheng Zhu } 1541c4103526SDeng-Cheng Zhu if (ieee754_cxtest(IEEE754_UNDERFLOW)) { 1542c4103526SDeng-Cheng Zhu MIPS_FPU_EMU_INC_STATS(ieee754_underflow); 15431da177e4SLinus Torvalds rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S; 1544c4103526SDeng-Cheng Zhu } 1545c4103526SDeng-Cheng Zhu if (ieee754_cxtest(IEEE754_OVERFLOW)) { 1546c4103526SDeng-Cheng Zhu MIPS_FPU_EMU_INC_STATS(ieee754_overflow); 15471da177e4SLinus Torvalds rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S; 1548c4103526SDeng-Cheng Zhu } 1549c4103526SDeng-Cheng Zhu if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) { 1550c4103526SDeng-Cheng Zhu MIPS_FPU_EMU_INC_STATS(ieee754_invalidop); 15511da177e4SLinus Torvalds rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S; 1552c4103526SDeng-Cheng Zhu } 15531da177e4SLinus Torvalds 15541da177e4SLinus Torvalds ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr; 15551da177e4SLinus Torvalds if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) { 15563f7cac41SRalf Baechle /*printk ("SIGFPE: FPU csr = %08x\n", 15571da177e4SLinus Torvalds ctx->fcr31); */ 15581da177e4SLinus Torvalds return SIGFPE; 15591da177e4SLinus Torvalds } 15601da177e4SLinus Torvalds 15611da177e4SLinus Torvalds break; 15621da177e4SLinus Torvalds 15631da177e4SLinus Torvalds default: 15641da177e4SLinus Torvalds return SIGILL; 15651da177e4SLinus Torvalds } 15661da177e4SLinus Torvalds break; 15671da177e4SLinus Torvalds } 15681da177e4SLinus Torvalds 15691da177e4SLinus Torvalds case d_fmt:{ /* 1 */ 15702209bcb1SRalf Baechle union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp); 15712209bcb1SRalf Baechle union ieee754dp fd, fr, fs, ft; 15723fccc015SRalf Baechle u64 __user *va; 15731da177e4SLinus Torvalds u64 val; 15741da177e4SLinus Torvalds 15751da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 15761da177e4SLinus Torvalds case ldxc1_op: 15773fccc015SRalf Baechle va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] + 15781da177e4SLinus Torvalds xcp->regs[MIPSInst_FT(ir)]); 15791da177e4SLinus Torvalds 1580b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(loads); 1581515b029dSDavid Daney if (!access_ok(VERIFY_READ, va, sizeof(u64))) { 1582b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 1583515b029dSDavid Daney *fault_addr = va; 15841da177e4SLinus Torvalds return SIGBUS; 15851da177e4SLinus Torvalds } 1586515b029dSDavid Daney if (__get_user(val, va)) { 1587515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 1588515b029dSDavid Daney *fault_addr = va; 1589515b029dSDavid Daney return SIGSEGV; 1590515b029dSDavid Daney } 15911da177e4SLinus Torvalds DITOREG(val, MIPSInst_FD(ir)); 15921da177e4SLinus Torvalds break; 15931da177e4SLinus Torvalds 15941da177e4SLinus Torvalds case sdxc1_op: 15953fccc015SRalf Baechle va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] + 15961da177e4SLinus Torvalds xcp->regs[MIPSInst_FT(ir)]); 15971da177e4SLinus Torvalds 1598b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(stores); 15991da177e4SLinus Torvalds DIFROMREG(val, MIPSInst_FS(ir)); 1600515b029dSDavid Daney if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) { 1601b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 1602515b029dSDavid Daney *fault_addr = va; 16031da177e4SLinus Torvalds return SIGBUS; 16041da177e4SLinus Torvalds } 1605515b029dSDavid Daney if (__put_user(val, va)) { 1606515b029dSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 1607515b029dSDavid Daney *fault_addr = va; 1608515b029dSDavid Daney return SIGSEGV; 1609515b029dSDavid Daney } 16101da177e4SLinus Torvalds break; 16111da177e4SLinus Torvalds 16121da177e4SLinus Torvalds case madd_d_op: 16131da177e4SLinus Torvalds handler = fpemu_dp_madd; 16141da177e4SLinus Torvalds goto dcoptop; 16151da177e4SLinus Torvalds case msub_d_op: 16161da177e4SLinus Torvalds handler = fpemu_dp_msub; 16171da177e4SLinus Torvalds goto dcoptop; 16181da177e4SLinus Torvalds case nmadd_d_op: 16191da177e4SLinus Torvalds handler = fpemu_dp_nmadd; 16201da177e4SLinus Torvalds goto dcoptop; 16211da177e4SLinus Torvalds case nmsub_d_op: 16221da177e4SLinus Torvalds handler = fpemu_dp_nmsub; 16231da177e4SLinus Torvalds goto dcoptop; 16241da177e4SLinus Torvalds 16251da177e4SLinus Torvalds dcoptop: 16261da177e4SLinus Torvalds DPFROMREG(fr, MIPSInst_FR(ir)); 16271da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 16281da177e4SLinus Torvalds DPFROMREG(ft, MIPSInst_FT(ir)); 16291da177e4SLinus Torvalds fd = (*handler) (fr, fs, ft); 16301da177e4SLinus Torvalds DPTOREG(fd, MIPSInst_FD(ir)); 16311da177e4SLinus Torvalds goto copcsr; 16321da177e4SLinus Torvalds 16331da177e4SLinus Torvalds default: 16341da177e4SLinus Torvalds return SIGILL; 16351da177e4SLinus Torvalds } 16361da177e4SLinus Torvalds break; 16371da177e4SLinus Torvalds } 16381da177e4SLinus Torvalds 163951061b88SDeng-Cheng Zhu case 0x3: 164051061b88SDeng-Cheng Zhu if (MIPSInst_FUNC(ir) != pfetch_op) 16411da177e4SLinus Torvalds return SIGILL; 164251061b88SDeng-Cheng Zhu 16431da177e4SLinus Torvalds /* ignore prefx operation */ 16441da177e4SLinus Torvalds break; 16451da177e4SLinus Torvalds 16461da177e4SLinus Torvalds default: 16471da177e4SLinus Torvalds return SIGILL; 16481da177e4SLinus Torvalds } 16491da177e4SLinus Torvalds 16501da177e4SLinus Torvalds return 0; 16511da177e4SLinus Torvalds } 16521da177e4SLinus Torvalds 16531da177e4SLinus Torvalds 16541da177e4SLinus Torvalds 16551da177e4SLinus Torvalds /* 16561da177e4SLinus Torvalds * Emulate a single COP1 arithmetic instruction. 16571da177e4SLinus Torvalds */ 1658eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx, 16591da177e4SLinus Torvalds mips_instruction ir) 16601da177e4SLinus Torvalds { 16611da177e4SLinus Torvalds int rfmt; /* resulting format */ 16621da177e4SLinus Torvalds unsigned rcsr = 0; /* resulting csr */ 16633f7cac41SRalf Baechle unsigned int oldrm; 16643f7cac41SRalf Baechle unsigned int cbit; 16651da177e4SLinus Torvalds unsigned cond; 16661da177e4SLinus Torvalds union { 16672209bcb1SRalf Baechle union ieee754dp d; 16682209bcb1SRalf Baechle union ieee754sp s; 16691da177e4SLinus Torvalds int w; 16701da177e4SLinus Torvalds s64 l; 16711da177e4SLinus Torvalds } rv; /* resulting value */ 16723f7cac41SRalf Baechle u64 bits; 16731da177e4SLinus Torvalds 1674b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(cp1ops); 16751da177e4SLinus Torvalds switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) { 16761da177e4SLinus Torvalds case s_fmt: { /* 0 */ 16771da177e4SLinus Torvalds union { 16782209bcb1SRalf Baechle union ieee754sp(*b) (union ieee754sp, union ieee754sp); 16792209bcb1SRalf Baechle union ieee754sp(*u) (union ieee754sp); 16801da177e4SLinus Torvalds } handler; 16814b820d95SPaul Burton union ieee754sp fd, fs, ft; 16821da177e4SLinus Torvalds 16831da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 16841da177e4SLinus Torvalds /* binary ops */ 16851da177e4SLinus Torvalds case fadd_op: 16861da177e4SLinus Torvalds handler.b = ieee754sp_add; 16871da177e4SLinus Torvalds goto scopbop; 16881da177e4SLinus Torvalds case fsub_op: 16891da177e4SLinus Torvalds handler.b = ieee754sp_sub; 16901da177e4SLinus Torvalds goto scopbop; 16911da177e4SLinus Torvalds case fmul_op: 16921da177e4SLinus Torvalds handler.b = ieee754sp_mul; 16931da177e4SLinus Torvalds goto scopbop; 16941da177e4SLinus Torvalds case fdiv_op: 16951da177e4SLinus Torvalds handler.b = ieee754sp_div; 16961da177e4SLinus Torvalds goto scopbop; 16971da177e4SLinus Torvalds 16981da177e4SLinus Torvalds /* unary ops */ 16991da177e4SLinus Torvalds case fsqrt_op: 17002d83fea7SMaciej W. Rozycki if (!cpu_has_mips_2_3_4_5_r) 170108a07904SRalf Baechle return SIGILL; 170208a07904SRalf Baechle 17031da177e4SLinus Torvalds handler.u = ieee754sp_sqrt; 17041da177e4SLinus Torvalds goto scopuop; 17053f7cac41SRalf Baechle 170608a07904SRalf Baechle /* 170708a07904SRalf Baechle * Note that on some MIPS IV implementations such as the 170808a07904SRalf Baechle * R5000 and R8000 the FSQRT and FRECIP instructions do not 170908a07904SRalf Baechle * achieve full IEEE-754 accuracy - however this emulator does. 171008a07904SRalf Baechle */ 17111da177e4SLinus Torvalds case frsqrt_op: 17122d83fea7SMaciej W. Rozycki if (!cpu_has_mips_4_5_64_r2_r6) 171308a07904SRalf Baechle return SIGILL; 171408a07904SRalf Baechle 17151da177e4SLinus Torvalds handler.u = fpemu_sp_rsqrt; 17161da177e4SLinus Torvalds goto scopuop; 17173f7cac41SRalf Baechle 17181da177e4SLinus Torvalds case frecip_op: 17192d83fea7SMaciej W. Rozycki if (!cpu_has_mips_4_5_64_r2_r6) 172008a07904SRalf Baechle return SIGILL; 172108a07904SRalf Baechle 17221da177e4SLinus Torvalds handler.u = fpemu_sp_recip; 17231da177e4SLinus Torvalds goto scopuop; 172408a07904SRalf Baechle 17251da177e4SLinus Torvalds case fmovc_op: 172608a07904SRalf Baechle if (!cpu_has_mips_4_5_r) 172708a07904SRalf Baechle return SIGILL; 172808a07904SRalf Baechle 17291da177e4SLinus Torvalds cond = fpucondbit[MIPSInst_FT(ir) >> 2]; 17301da177e4SLinus Torvalds if (((ctx->fcr31 & cond) != 0) != 17311da177e4SLinus Torvalds ((MIPSInst_FT(ir) & 1) != 0)) 17321da177e4SLinus Torvalds return 0; 17331da177e4SLinus Torvalds SPFROMREG(rv.s, MIPSInst_FS(ir)); 17341da177e4SLinus Torvalds break; 17353f7cac41SRalf Baechle 17361da177e4SLinus Torvalds case fmovz_op: 173708a07904SRalf Baechle if (!cpu_has_mips_4_5_r) 173808a07904SRalf Baechle return SIGILL; 173908a07904SRalf Baechle 17401da177e4SLinus Torvalds if (xcp->regs[MIPSInst_FT(ir)] != 0) 17411da177e4SLinus Torvalds return 0; 17421da177e4SLinus Torvalds SPFROMREG(rv.s, MIPSInst_FS(ir)); 17431da177e4SLinus Torvalds break; 17443f7cac41SRalf Baechle 17451da177e4SLinus Torvalds case fmovn_op: 174608a07904SRalf Baechle if (!cpu_has_mips_4_5_r) 174708a07904SRalf Baechle return SIGILL; 174808a07904SRalf Baechle 17491da177e4SLinus Torvalds if (xcp->regs[MIPSInst_FT(ir)] == 0) 17501da177e4SLinus Torvalds return 0; 17511da177e4SLinus Torvalds SPFROMREG(rv.s, MIPSInst_FS(ir)); 17521da177e4SLinus Torvalds break; 17533f7cac41SRalf Baechle 175467613f02SMarkos Chandras case fseleqz_op: 175567613f02SMarkos Chandras if (!cpu_has_mips_r6) 175667613f02SMarkos Chandras return SIGILL; 175767613f02SMarkos Chandras 175867613f02SMarkos Chandras SPFROMREG(rv.s, MIPSInst_FT(ir)); 175967613f02SMarkos Chandras if (rv.w & 0x1) 176067613f02SMarkos Chandras rv.w = 0; 176167613f02SMarkos Chandras else 176267613f02SMarkos Chandras SPFROMREG(rv.s, MIPSInst_FS(ir)); 176367613f02SMarkos Chandras break; 176467613f02SMarkos Chandras 1765130fe357SMarkos Chandras case fselnez_op: 1766130fe357SMarkos Chandras if (!cpu_has_mips_r6) 1767130fe357SMarkos Chandras return SIGILL; 1768130fe357SMarkos Chandras 1769130fe357SMarkos Chandras SPFROMREG(rv.s, MIPSInst_FT(ir)); 1770130fe357SMarkos Chandras if (rv.w & 0x1) 1771130fe357SMarkos Chandras SPFROMREG(rv.s, MIPSInst_FS(ir)); 1772130fe357SMarkos Chandras else 1773130fe357SMarkos Chandras rv.w = 0; 1774130fe357SMarkos Chandras break; 1775130fe357SMarkos Chandras 1776e24c3becSMarkos Chandras case fmaddf_op: { 1777e24c3becSMarkos Chandras union ieee754sp ft, fs, fd; 1778e24c3becSMarkos Chandras 1779e24c3becSMarkos Chandras if (!cpu_has_mips_r6) 1780e24c3becSMarkos Chandras return SIGILL; 1781e24c3becSMarkos Chandras 1782e24c3becSMarkos Chandras SPFROMREG(ft, MIPSInst_FT(ir)); 1783e24c3becSMarkos Chandras SPFROMREG(fs, MIPSInst_FS(ir)); 1784e24c3becSMarkos Chandras SPFROMREG(fd, MIPSInst_FD(ir)); 1785e24c3becSMarkos Chandras rv.s = ieee754sp_maddf(fd, fs, ft); 1786e24c3becSMarkos Chandras break; 1787e24c3becSMarkos Chandras } 1788e24c3becSMarkos Chandras 178983d43305SMarkos Chandras case fmsubf_op: { 179083d43305SMarkos Chandras union ieee754sp ft, fs, fd; 179183d43305SMarkos Chandras 179283d43305SMarkos Chandras if (!cpu_has_mips_r6) 179383d43305SMarkos Chandras return SIGILL; 179483d43305SMarkos Chandras 179583d43305SMarkos Chandras SPFROMREG(ft, MIPSInst_FT(ir)); 179683d43305SMarkos Chandras SPFROMREG(fs, MIPSInst_FS(ir)); 179783d43305SMarkos Chandras SPFROMREG(fd, MIPSInst_FD(ir)); 179883d43305SMarkos Chandras rv.s = ieee754sp_msubf(fd, fs, ft); 179983d43305SMarkos Chandras break; 180083d43305SMarkos Chandras } 180183d43305SMarkos Chandras 1802400bd2e4SMarkos Chandras case frint_op: { 1803400bd2e4SMarkos Chandras union ieee754sp fs; 1804400bd2e4SMarkos Chandras 1805400bd2e4SMarkos Chandras if (!cpu_has_mips_r6) 1806400bd2e4SMarkos Chandras return SIGILL; 1807400bd2e4SMarkos Chandras 1808400bd2e4SMarkos Chandras SPFROMREG(fs, MIPSInst_FS(ir)); 1809400bd2e4SMarkos Chandras rv.l = ieee754sp_tlong(fs); 1810400bd2e4SMarkos Chandras rv.s = ieee754sp_flong(rv.l); 1811400bd2e4SMarkos Chandras goto copcsr; 1812400bd2e4SMarkos Chandras } 1813400bd2e4SMarkos Chandras 181438db37baSMarkos Chandras case fclass_op: { 181538db37baSMarkos Chandras union ieee754sp fs; 181638db37baSMarkos Chandras 181738db37baSMarkos Chandras if (!cpu_has_mips_r6) 181838db37baSMarkos Chandras return SIGILL; 181938db37baSMarkos Chandras 182038db37baSMarkos Chandras SPFROMREG(fs, MIPSInst_FS(ir)); 182138db37baSMarkos Chandras rv.w = ieee754sp_2008class(fs); 182238db37baSMarkos Chandras rfmt = w_fmt; 182338db37baSMarkos Chandras break; 182438db37baSMarkos Chandras } 182538db37baSMarkos Chandras 18264e9561b2SMarkos Chandras case fmin_op: { 18274e9561b2SMarkos Chandras union ieee754sp fs, ft; 18284e9561b2SMarkos Chandras 18294e9561b2SMarkos Chandras if (!cpu_has_mips_r6) 18304e9561b2SMarkos Chandras return SIGILL; 18314e9561b2SMarkos Chandras 18324e9561b2SMarkos Chandras SPFROMREG(ft, MIPSInst_FT(ir)); 18334e9561b2SMarkos Chandras SPFROMREG(fs, MIPSInst_FS(ir)); 18344e9561b2SMarkos Chandras rv.s = ieee754sp_fmin(fs, ft); 18354e9561b2SMarkos Chandras break; 18364e9561b2SMarkos Chandras } 18374e9561b2SMarkos Chandras 18384e9561b2SMarkos Chandras case fmina_op: { 18394e9561b2SMarkos Chandras union ieee754sp fs, ft; 18404e9561b2SMarkos Chandras 18414e9561b2SMarkos Chandras if (!cpu_has_mips_r6) 18424e9561b2SMarkos Chandras return SIGILL; 18434e9561b2SMarkos Chandras 18444e9561b2SMarkos Chandras SPFROMREG(ft, MIPSInst_FT(ir)); 18454e9561b2SMarkos Chandras SPFROMREG(fs, MIPSInst_FS(ir)); 18464e9561b2SMarkos Chandras rv.s = ieee754sp_fmina(fs, ft); 18474e9561b2SMarkos Chandras break; 18484e9561b2SMarkos Chandras } 18494e9561b2SMarkos Chandras 1850a79f5f9bSMarkos Chandras case fmax_op: { 1851a79f5f9bSMarkos Chandras union ieee754sp fs, ft; 1852a79f5f9bSMarkos Chandras 1853a79f5f9bSMarkos Chandras if (!cpu_has_mips_r6) 1854a79f5f9bSMarkos Chandras return SIGILL; 1855a79f5f9bSMarkos Chandras 1856a79f5f9bSMarkos Chandras SPFROMREG(ft, MIPSInst_FT(ir)); 1857a79f5f9bSMarkos Chandras SPFROMREG(fs, MIPSInst_FS(ir)); 1858a79f5f9bSMarkos Chandras rv.s = ieee754sp_fmax(fs, ft); 1859a79f5f9bSMarkos Chandras break; 1860a79f5f9bSMarkos Chandras } 1861a79f5f9bSMarkos Chandras 1862a79f5f9bSMarkos Chandras case fmaxa_op: { 1863a79f5f9bSMarkos Chandras union ieee754sp fs, ft; 1864a79f5f9bSMarkos Chandras 1865a79f5f9bSMarkos Chandras if (!cpu_has_mips_r6) 1866a79f5f9bSMarkos Chandras return SIGILL; 1867a79f5f9bSMarkos Chandras 1868a79f5f9bSMarkos Chandras SPFROMREG(ft, MIPSInst_FT(ir)); 1869a79f5f9bSMarkos Chandras SPFROMREG(fs, MIPSInst_FS(ir)); 1870a79f5f9bSMarkos Chandras rv.s = ieee754sp_fmaxa(fs, ft); 1871a79f5f9bSMarkos Chandras break; 1872a79f5f9bSMarkos Chandras } 1873a79f5f9bSMarkos Chandras 18741da177e4SLinus Torvalds case fabs_op: 18751da177e4SLinus Torvalds handler.u = ieee754sp_abs; 18761da177e4SLinus Torvalds goto scopuop; 18773f7cac41SRalf Baechle 18781da177e4SLinus Torvalds case fneg_op: 18791da177e4SLinus Torvalds handler.u = ieee754sp_neg; 18801da177e4SLinus Torvalds goto scopuop; 18813f7cac41SRalf Baechle 18821da177e4SLinus Torvalds case fmov_op: 18831da177e4SLinus Torvalds /* an easy one */ 18841da177e4SLinus Torvalds SPFROMREG(rv.s, MIPSInst_FS(ir)); 18851da177e4SLinus Torvalds goto copcsr; 18861da177e4SLinus Torvalds 18871da177e4SLinus Torvalds /* binary op on handler */ 18881da177e4SLinus Torvalds scopbop: 18891da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 18901da177e4SLinus Torvalds SPFROMREG(ft, MIPSInst_FT(ir)); 18911da177e4SLinus Torvalds 18921da177e4SLinus Torvalds rv.s = (*handler.b) (fs, ft); 18931da177e4SLinus Torvalds goto copcsr; 18941da177e4SLinus Torvalds scopuop: 18951da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 18961da177e4SLinus Torvalds rv.s = (*handler.u) (fs); 18971da177e4SLinus Torvalds goto copcsr; 18981da177e4SLinus Torvalds copcsr: 1899c4103526SDeng-Cheng Zhu if (ieee754_cxtest(IEEE754_INEXACT)) { 1900c4103526SDeng-Cheng Zhu MIPS_FPU_EMU_INC_STATS(ieee754_inexact); 19011da177e4SLinus Torvalds rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S; 1902c4103526SDeng-Cheng Zhu } 1903c4103526SDeng-Cheng Zhu if (ieee754_cxtest(IEEE754_UNDERFLOW)) { 1904c4103526SDeng-Cheng Zhu MIPS_FPU_EMU_INC_STATS(ieee754_underflow); 19051da177e4SLinus Torvalds rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S; 1906c4103526SDeng-Cheng Zhu } 1907c4103526SDeng-Cheng Zhu if (ieee754_cxtest(IEEE754_OVERFLOW)) { 1908c4103526SDeng-Cheng Zhu MIPS_FPU_EMU_INC_STATS(ieee754_overflow); 19091da177e4SLinus Torvalds rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S; 1910c4103526SDeng-Cheng Zhu } 1911c4103526SDeng-Cheng Zhu if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) { 1912c4103526SDeng-Cheng Zhu MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv); 19131da177e4SLinus Torvalds rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S; 1914c4103526SDeng-Cheng Zhu } 1915c4103526SDeng-Cheng Zhu if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) { 1916c4103526SDeng-Cheng Zhu MIPS_FPU_EMU_INC_STATS(ieee754_invalidop); 19171da177e4SLinus Torvalds rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S; 1918c4103526SDeng-Cheng Zhu } 19191da177e4SLinus Torvalds break; 19201da177e4SLinus Torvalds 19211da177e4SLinus Torvalds /* unary conv ops */ 19221da177e4SLinus Torvalds case fcvts_op: 19231da177e4SLinus Torvalds return SIGILL; /* not defined */ 19241da177e4SLinus Torvalds 19253f7cac41SRalf Baechle case fcvtd_op: 19261da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 19271da177e4SLinus Torvalds rv.d = ieee754dp_fsp(fs); 19281da177e4SLinus Torvalds rfmt = d_fmt; 19291da177e4SLinus Torvalds goto copcsr; 19301da177e4SLinus Torvalds 19313f7cac41SRalf Baechle case fcvtw_op: 19321da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 19331da177e4SLinus Torvalds rv.w = ieee754sp_tint(fs); 19341da177e4SLinus Torvalds rfmt = w_fmt; 19351da177e4SLinus Torvalds goto copcsr; 19361da177e4SLinus Torvalds 19371da177e4SLinus Torvalds case fround_op: 19381da177e4SLinus Torvalds case ftrunc_op: 19391da177e4SLinus Torvalds case fceil_op: 19403f7cac41SRalf Baechle case ffloor_op: 19412d83fea7SMaciej W. Rozycki if (!cpu_has_mips_2_3_4_5_r) 194208a07904SRalf Baechle return SIGILL; 194308a07904SRalf Baechle 19443f7cac41SRalf Baechle oldrm = ieee754_csr.rm; 19451da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 19462cfcf8a8SMaciej W. Rozycki ieee754_csr.rm = MIPSInst_FUNC(ir); 19471da177e4SLinus Torvalds rv.w = ieee754sp_tint(fs); 19481da177e4SLinus Torvalds ieee754_csr.rm = oldrm; 19491da177e4SLinus Torvalds rfmt = w_fmt; 19501da177e4SLinus Torvalds goto copcsr; 19511da177e4SLinus Torvalds 19524b820d95SPaul Burton case fsel_op: 19534b820d95SPaul Burton if (!cpu_has_mips_r6) 19544b820d95SPaul Burton return SIGILL; 19554b820d95SPaul Burton 19564b820d95SPaul Burton SPFROMREG(fd, MIPSInst_FD(ir)); 19574b820d95SPaul Burton if (fd.bits & 0x1) 19584b820d95SPaul Burton SPFROMREG(rv.s, MIPSInst_FT(ir)); 19594b820d95SPaul Burton else 19604b820d95SPaul Burton SPFROMREG(rv.s, MIPSInst_FS(ir)); 19614b820d95SPaul Burton break; 19624b820d95SPaul Burton 19633f7cac41SRalf Baechle case fcvtl_op: 19642d83fea7SMaciej W. Rozycki if (!cpu_has_mips_3_4_5_64_r2_r6) 196508a07904SRalf Baechle return SIGILL; 196608a07904SRalf Baechle 19671da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 19681da177e4SLinus Torvalds rv.l = ieee754sp_tlong(fs); 19691da177e4SLinus Torvalds rfmt = l_fmt; 19701da177e4SLinus Torvalds goto copcsr; 19711da177e4SLinus Torvalds 19721da177e4SLinus Torvalds case froundl_op: 19731da177e4SLinus Torvalds case ftruncl_op: 19741da177e4SLinus Torvalds case fceill_op: 19753f7cac41SRalf Baechle case ffloorl_op: 19762d83fea7SMaciej W. Rozycki if (!cpu_has_mips_3_4_5_64_r2_r6) 197708a07904SRalf Baechle return SIGILL; 197808a07904SRalf Baechle 19793f7cac41SRalf Baechle oldrm = ieee754_csr.rm; 19801da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 19812cfcf8a8SMaciej W. Rozycki ieee754_csr.rm = MIPSInst_FUNC(ir); 19821da177e4SLinus Torvalds rv.l = ieee754sp_tlong(fs); 19831da177e4SLinus Torvalds ieee754_csr.rm = oldrm; 19841da177e4SLinus Torvalds rfmt = l_fmt; 19851da177e4SLinus Torvalds goto copcsr; 19861da177e4SLinus Torvalds 19871da177e4SLinus Torvalds default: 1988f8c3c671SMarkos Chandras if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) { 19891da177e4SLinus Torvalds unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op; 19902209bcb1SRalf Baechle union ieee754sp fs, ft; 19911da177e4SLinus Torvalds 19921da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 19931da177e4SLinus Torvalds SPFROMREG(ft, MIPSInst_FT(ir)); 19941da177e4SLinus Torvalds rv.w = ieee754sp_cmp(fs, ft, 19951da177e4SLinus Torvalds cmptab[cmpop & 0x7], cmpop & 0x8); 19961da177e4SLinus Torvalds rfmt = -1; 19971da177e4SLinus Torvalds if ((cmpop & 0x8) && ieee754_cxtest 19981da177e4SLinus Torvalds (IEEE754_INVALID_OPERATION)) 19991da177e4SLinus Torvalds rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S; 20001da177e4SLinus Torvalds else 20011da177e4SLinus Torvalds goto copcsr; 20021da177e4SLinus Torvalds 20033f7cac41SRalf Baechle } else 20041da177e4SLinus Torvalds return SIGILL; 20051da177e4SLinus Torvalds break; 20061da177e4SLinus Torvalds } 20071da177e4SLinus Torvalds break; 20081da177e4SLinus Torvalds } 20091da177e4SLinus Torvalds 20101da177e4SLinus Torvalds case d_fmt: { 20114b820d95SPaul Burton union ieee754dp fd, fs, ft; 20121da177e4SLinus Torvalds union { 20132209bcb1SRalf Baechle union ieee754dp(*b) (union ieee754dp, union ieee754dp); 20142209bcb1SRalf Baechle union ieee754dp(*u) (union ieee754dp); 20151da177e4SLinus Torvalds } handler; 20161da177e4SLinus Torvalds 20171da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 20181da177e4SLinus Torvalds /* binary ops */ 20191da177e4SLinus Torvalds case fadd_op: 20201da177e4SLinus Torvalds handler.b = ieee754dp_add; 20211da177e4SLinus Torvalds goto dcopbop; 20221da177e4SLinus Torvalds case fsub_op: 20231da177e4SLinus Torvalds handler.b = ieee754dp_sub; 20241da177e4SLinus Torvalds goto dcopbop; 20251da177e4SLinus Torvalds case fmul_op: 20261da177e4SLinus Torvalds handler.b = ieee754dp_mul; 20271da177e4SLinus Torvalds goto dcopbop; 20281da177e4SLinus Torvalds case fdiv_op: 20291da177e4SLinus Torvalds handler.b = ieee754dp_div; 20301da177e4SLinus Torvalds goto dcopbop; 20311da177e4SLinus Torvalds 20321da177e4SLinus Torvalds /* unary ops */ 20331da177e4SLinus Torvalds case fsqrt_op: 203408a07904SRalf Baechle if (!cpu_has_mips_2_3_4_5_r) 203508a07904SRalf Baechle return SIGILL; 203608a07904SRalf Baechle 20371da177e4SLinus Torvalds handler.u = ieee754dp_sqrt; 20381da177e4SLinus Torvalds goto dcopuop; 203908a07904SRalf Baechle /* 204008a07904SRalf Baechle * Note that on some MIPS IV implementations such as the 204108a07904SRalf Baechle * R5000 and R8000 the FSQRT and FRECIP instructions do not 204208a07904SRalf Baechle * achieve full IEEE-754 accuracy - however this emulator does. 204308a07904SRalf Baechle */ 20441da177e4SLinus Torvalds case frsqrt_op: 20452d83fea7SMaciej W. Rozycki if (!cpu_has_mips_4_5_64_r2_r6) 204608a07904SRalf Baechle return SIGILL; 204708a07904SRalf Baechle 20481da177e4SLinus Torvalds handler.u = fpemu_dp_rsqrt; 20491da177e4SLinus Torvalds goto dcopuop; 20501da177e4SLinus Torvalds case frecip_op: 20512d83fea7SMaciej W. Rozycki if (!cpu_has_mips_4_5_64_r2_r6) 205208a07904SRalf Baechle return SIGILL; 205308a07904SRalf Baechle 20541da177e4SLinus Torvalds handler.u = fpemu_dp_recip; 20551da177e4SLinus Torvalds goto dcopuop; 20561da177e4SLinus Torvalds case fmovc_op: 205708a07904SRalf Baechle if (!cpu_has_mips_4_5_r) 205808a07904SRalf Baechle return SIGILL; 205908a07904SRalf Baechle 20601da177e4SLinus Torvalds cond = fpucondbit[MIPSInst_FT(ir) >> 2]; 20611da177e4SLinus Torvalds if (((ctx->fcr31 & cond) != 0) != 20621da177e4SLinus Torvalds ((MIPSInst_FT(ir) & 1) != 0)) 20631da177e4SLinus Torvalds return 0; 20641da177e4SLinus Torvalds DPFROMREG(rv.d, MIPSInst_FS(ir)); 20651da177e4SLinus Torvalds break; 20661da177e4SLinus Torvalds case fmovz_op: 206708a07904SRalf Baechle if (!cpu_has_mips_4_5_r) 206808a07904SRalf Baechle return SIGILL; 206908a07904SRalf Baechle 20701da177e4SLinus Torvalds if (xcp->regs[MIPSInst_FT(ir)] != 0) 20711da177e4SLinus Torvalds return 0; 20721da177e4SLinus Torvalds DPFROMREG(rv.d, MIPSInst_FS(ir)); 20731da177e4SLinus Torvalds break; 20741da177e4SLinus Torvalds case fmovn_op: 207508a07904SRalf Baechle if (!cpu_has_mips_4_5_r) 207608a07904SRalf Baechle return SIGILL; 207708a07904SRalf Baechle 20781da177e4SLinus Torvalds if (xcp->regs[MIPSInst_FT(ir)] == 0) 20791da177e4SLinus Torvalds return 0; 20801da177e4SLinus Torvalds DPFROMREG(rv.d, MIPSInst_FS(ir)); 20811da177e4SLinus Torvalds break; 208267613f02SMarkos Chandras 208367613f02SMarkos Chandras case fseleqz_op: 208467613f02SMarkos Chandras if (!cpu_has_mips_r6) 208567613f02SMarkos Chandras return SIGILL; 208667613f02SMarkos Chandras 208767613f02SMarkos Chandras DPFROMREG(rv.d, MIPSInst_FT(ir)); 208867613f02SMarkos Chandras if (rv.l & 0x1) 208967613f02SMarkos Chandras rv.l = 0; 209067613f02SMarkos Chandras else 209167613f02SMarkos Chandras DPFROMREG(rv.d, MIPSInst_FS(ir)); 209267613f02SMarkos Chandras break; 209367613f02SMarkos Chandras 2094130fe357SMarkos Chandras case fselnez_op: 2095130fe357SMarkos Chandras if (!cpu_has_mips_r6) 2096130fe357SMarkos Chandras return SIGILL; 2097130fe357SMarkos Chandras 2098130fe357SMarkos Chandras DPFROMREG(rv.d, MIPSInst_FT(ir)); 2099130fe357SMarkos Chandras if (rv.l & 0x1) 2100130fe357SMarkos Chandras DPFROMREG(rv.d, MIPSInst_FS(ir)); 2101130fe357SMarkos Chandras else 2102130fe357SMarkos Chandras rv.l = 0; 2103130fe357SMarkos Chandras break; 2104130fe357SMarkos Chandras 2105e24c3becSMarkos Chandras case fmaddf_op: { 2106e24c3becSMarkos Chandras union ieee754dp ft, fs, fd; 2107e24c3becSMarkos Chandras 2108e24c3becSMarkos Chandras if (!cpu_has_mips_r6) 2109e24c3becSMarkos Chandras return SIGILL; 2110e24c3becSMarkos Chandras 2111e24c3becSMarkos Chandras DPFROMREG(ft, MIPSInst_FT(ir)); 2112e24c3becSMarkos Chandras DPFROMREG(fs, MIPSInst_FS(ir)); 2113e24c3becSMarkos Chandras DPFROMREG(fd, MIPSInst_FD(ir)); 2114e24c3becSMarkos Chandras rv.d = ieee754dp_maddf(fd, fs, ft); 2115e24c3becSMarkos Chandras break; 2116e24c3becSMarkos Chandras } 2117e24c3becSMarkos Chandras 211883d43305SMarkos Chandras case fmsubf_op: { 211983d43305SMarkos Chandras union ieee754dp ft, fs, fd; 212083d43305SMarkos Chandras 212183d43305SMarkos Chandras if (!cpu_has_mips_r6) 212283d43305SMarkos Chandras return SIGILL; 212383d43305SMarkos Chandras 212483d43305SMarkos Chandras DPFROMREG(ft, MIPSInst_FT(ir)); 212583d43305SMarkos Chandras DPFROMREG(fs, MIPSInst_FS(ir)); 212683d43305SMarkos Chandras DPFROMREG(fd, MIPSInst_FD(ir)); 212783d43305SMarkos Chandras rv.d = ieee754dp_msubf(fd, fs, ft); 212883d43305SMarkos Chandras break; 212983d43305SMarkos Chandras } 213083d43305SMarkos Chandras 2131400bd2e4SMarkos Chandras case frint_op: { 2132400bd2e4SMarkos Chandras union ieee754dp fs; 2133400bd2e4SMarkos Chandras 2134400bd2e4SMarkos Chandras if (!cpu_has_mips_r6) 2135400bd2e4SMarkos Chandras return SIGILL; 2136400bd2e4SMarkos Chandras 2137400bd2e4SMarkos Chandras DPFROMREG(fs, MIPSInst_FS(ir)); 2138400bd2e4SMarkos Chandras rv.l = ieee754dp_tlong(fs); 2139400bd2e4SMarkos Chandras rv.d = ieee754dp_flong(rv.l); 2140400bd2e4SMarkos Chandras goto copcsr; 2141400bd2e4SMarkos Chandras } 2142400bd2e4SMarkos Chandras 214338db37baSMarkos Chandras case fclass_op: { 214438db37baSMarkos Chandras union ieee754dp fs; 214538db37baSMarkos Chandras 214638db37baSMarkos Chandras if (!cpu_has_mips_r6) 214738db37baSMarkos Chandras return SIGILL; 214838db37baSMarkos Chandras 214938db37baSMarkos Chandras DPFROMREG(fs, MIPSInst_FS(ir)); 215038db37baSMarkos Chandras rv.w = ieee754dp_2008class(fs); 215138db37baSMarkos Chandras rfmt = w_fmt; 215238db37baSMarkos Chandras break; 215338db37baSMarkos Chandras } 215438db37baSMarkos Chandras 21554e9561b2SMarkos Chandras case fmin_op: { 21564e9561b2SMarkos Chandras union ieee754dp fs, ft; 21574e9561b2SMarkos Chandras 21584e9561b2SMarkos Chandras if (!cpu_has_mips_r6) 21594e9561b2SMarkos Chandras return SIGILL; 21604e9561b2SMarkos Chandras 21614e9561b2SMarkos Chandras DPFROMREG(ft, MIPSInst_FT(ir)); 21624e9561b2SMarkos Chandras DPFROMREG(fs, MIPSInst_FS(ir)); 21634e9561b2SMarkos Chandras rv.d = ieee754dp_fmin(fs, ft); 21644e9561b2SMarkos Chandras break; 21654e9561b2SMarkos Chandras } 21664e9561b2SMarkos Chandras 21674e9561b2SMarkos Chandras case fmina_op: { 21684e9561b2SMarkos Chandras union ieee754dp fs, ft; 21694e9561b2SMarkos Chandras 21704e9561b2SMarkos Chandras if (!cpu_has_mips_r6) 21714e9561b2SMarkos Chandras return SIGILL; 21724e9561b2SMarkos Chandras 21734e9561b2SMarkos Chandras DPFROMREG(ft, MIPSInst_FT(ir)); 21744e9561b2SMarkos Chandras DPFROMREG(fs, MIPSInst_FS(ir)); 21754e9561b2SMarkos Chandras rv.d = ieee754dp_fmina(fs, ft); 21764e9561b2SMarkos Chandras break; 21774e9561b2SMarkos Chandras } 21784e9561b2SMarkos Chandras 2179a79f5f9bSMarkos Chandras case fmax_op: { 2180a79f5f9bSMarkos Chandras union ieee754dp fs, ft; 2181a79f5f9bSMarkos Chandras 2182a79f5f9bSMarkos Chandras if (!cpu_has_mips_r6) 2183a79f5f9bSMarkos Chandras return SIGILL; 2184a79f5f9bSMarkos Chandras 2185a79f5f9bSMarkos Chandras DPFROMREG(ft, MIPSInst_FT(ir)); 2186a79f5f9bSMarkos Chandras DPFROMREG(fs, MIPSInst_FS(ir)); 2187a79f5f9bSMarkos Chandras rv.d = ieee754dp_fmax(fs, ft); 2188a79f5f9bSMarkos Chandras break; 2189a79f5f9bSMarkos Chandras } 2190a79f5f9bSMarkos Chandras 2191a79f5f9bSMarkos Chandras case fmaxa_op: { 2192a79f5f9bSMarkos Chandras union ieee754dp fs, ft; 2193a79f5f9bSMarkos Chandras 2194a79f5f9bSMarkos Chandras if (!cpu_has_mips_r6) 2195a79f5f9bSMarkos Chandras return SIGILL; 2196a79f5f9bSMarkos Chandras 2197a79f5f9bSMarkos Chandras DPFROMREG(ft, MIPSInst_FT(ir)); 2198a79f5f9bSMarkos Chandras DPFROMREG(fs, MIPSInst_FS(ir)); 2199a79f5f9bSMarkos Chandras rv.d = ieee754dp_fmaxa(fs, ft); 2200a79f5f9bSMarkos Chandras break; 2201a79f5f9bSMarkos Chandras } 2202a79f5f9bSMarkos Chandras 22031da177e4SLinus Torvalds case fabs_op: 22041da177e4SLinus Torvalds handler.u = ieee754dp_abs; 22051da177e4SLinus Torvalds goto dcopuop; 22061da177e4SLinus Torvalds 22071da177e4SLinus Torvalds case fneg_op: 22081da177e4SLinus Torvalds handler.u = ieee754dp_neg; 22091da177e4SLinus Torvalds goto dcopuop; 22101da177e4SLinus Torvalds 22111da177e4SLinus Torvalds case fmov_op: 22121da177e4SLinus Torvalds /* an easy one */ 22131da177e4SLinus Torvalds DPFROMREG(rv.d, MIPSInst_FS(ir)); 22141da177e4SLinus Torvalds goto copcsr; 22151da177e4SLinus Torvalds 22161da177e4SLinus Torvalds /* binary op on handler */ 22173f7cac41SRalf Baechle dcopbop: 22181da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 22191da177e4SLinus Torvalds DPFROMREG(ft, MIPSInst_FT(ir)); 22201da177e4SLinus Torvalds 22211da177e4SLinus Torvalds rv.d = (*handler.b) (fs, ft); 22221da177e4SLinus Torvalds goto copcsr; 22233f7cac41SRalf Baechle dcopuop: 22241da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 22251da177e4SLinus Torvalds rv.d = (*handler.u) (fs); 22261da177e4SLinus Torvalds goto copcsr; 22271da177e4SLinus Torvalds 22283f7cac41SRalf Baechle /* 22293f7cac41SRalf Baechle * unary conv ops 22303f7cac41SRalf Baechle */ 22313f7cac41SRalf Baechle case fcvts_op: 22321da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 22331da177e4SLinus Torvalds rv.s = ieee754sp_fdp(fs); 22341da177e4SLinus Torvalds rfmt = s_fmt; 22351da177e4SLinus Torvalds goto copcsr; 22363f7cac41SRalf Baechle 22371da177e4SLinus Torvalds case fcvtd_op: 22381da177e4SLinus Torvalds return SIGILL; /* not defined */ 22391da177e4SLinus Torvalds 22403f7cac41SRalf Baechle case fcvtw_op: 22411da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 22421da177e4SLinus Torvalds rv.w = ieee754dp_tint(fs); /* wrong */ 22431da177e4SLinus Torvalds rfmt = w_fmt; 22441da177e4SLinus Torvalds goto copcsr; 22451da177e4SLinus Torvalds 22461da177e4SLinus Torvalds case fround_op: 22471da177e4SLinus Torvalds case ftrunc_op: 22481da177e4SLinus Torvalds case fceil_op: 22493f7cac41SRalf Baechle case ffloor_op: 225008a07904SRalf Baechle if (!cpu_has_mips_2_3_4_5_r) 225108a07904SRalf Baechle return SIGILL; 225208a07904SRalf Baechle 22533f7cac41SRalf Baechle oldrm = ieee754_csr.rm; 22541da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 22552cfcf8a8SMaciej W. Rozycki ieee754_csr.rm = MIPSInst_FUNC(ir); 22561da177e4SLinus Torvalds rv.w = ieee754dp_tint(fs); 22571da177e4SLinus Torvalds ieee754_csr.rm = oldrm; 22581da177e4SLinus Torvalds rfmt = w_fmt; 22591da177e4SLinus Torvalds goto copcsr; 22601da177e4SLinus Torvalds 22614b820d95SPaul Burton case fsel_op: 22624b820d95SPaul Burton if (!cpu_has_mips_r6) 22634b820d95SPaul Burton return SIGILL; 22644b820d95SPaul Burton 22654b820d95SPaul Burton DPFROMREG(fd, MIPSInst_FD(ir)); 22664b820d95SPaul Burton if (fd.bits & 0x1) 22674b820d95SPaul Burton DPFROMREG(rv.d, MIPSInst_FT(ir)); 22684b820d95SPaul Burton else 22694b820d95SPaul Burton DPFROMREG(rv.d, MIPSInst_FS(ir)); 22704b820d95SPaul Burton break; 22714b820d95SPaul Burton 22723f7cac41SRalf Baechle case fcvtl_op: 22732d83fea7SMaciej W. Rozycki if (!cpu_has_mips_3_4_5_64_r2_r6) 227408a07904SRalf Baechle return SIGILL; 227508a07904SRalf Baechle 22761da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 22771da177e4SLinus Torvalds rv.l = ieee754dp_tlong(fs); 22781da177e4SLinus Torvalds rfmt = l_fmt; 22791da177e4SLinus Torvalds goto copcsr; 22801da177e4SLinus Torvalds 22811da177e4SLinus Torvalds case froundl_op: 22821da177e4SLinus Torvalds case ftruncl_op: 22831da177e4SLinus Torvalds case fceill_op: 22843f7cac41SRalf Baechle case ffloorl_op: 22852d83fea7SMaciej W. Rozycki if (!cpu_has_mips_3_4_5_64_r2_r6) 228608a07904SRalf Baechle return SIGILL; 228708a07904SRalf Baechle 22883f7cac41SRalf Baechle oldrm = ieee754_csr.rm; 22891da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 22902cfcf8a8SMaciej W. Rozycki ieee754_csr.rm = MIPSInst_FUNC(ir); 22911da177e4SLinus Torvalds rv.l = ieee754dp_tlong(fs); 22921da177e4SLinus Torvalds ieee754_csr.rm = oldrm; 22931da177e4SLinus Torvalds rfmt = l_fmt; 22941da177e4SLinus Torvalds goto copcsr; 22951da177e4SLinus Torvalds 22961da177e4SLinus Torvalds default: 2297f8c3c671SMarkos Chandras if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) { 22981da177e4SLinus Torvalds unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op; 22992209bcb1SRalf Baechle union ieee754dp fs, ft; 23001da177e4SLinus Torvalds 23011da177e4SLinus Torvalds DPFROMREG(fs, MIPSInst_FS(ir)); 23021da177e4SLinus Torvalds DPFROMREG(ft, MIPSInst_FT(ir)); 23031da177e4SLinus Torvalds rv.w = ieee754dp_cmp(fs, ft, 23041da177e4SLinus Torvalds cmptab[cmpop & 0x7], cmpop & 0x8); 23051da177e4SLinus Torvalds rfmt = -1; 23061da177e4SLinus Torvalds if ((cmpop & 0x8) 23071da177e4SLinus Torvalds && 23081da177e4SLinus Torvalds ieee754_cxtest 23091da177e4SLinus Torvalds (IEEE754_INVALID_OPERATION)) 23101da177e4SLinus Torvalds rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S; 23111da177e4SLinus Torvalds else 23121da177e4SLinus Torvalds goto copcsr; 23131da177e4SLinus Torvalds 23141da177e4SLinus Torvalds } 23151da177e4SLinus Torvalds else { 23161da177e4SLinus Torvalds return SIGILL; 23171da177e4SLinus Torvalds } 23181da177e4SLinus Torvalds break; 23191da177e4SLinus Torvalds } 23201da177e4SLinus Torvalds break; 2321bbdd8147SMarkos Chandras } 23221da177e4SLinus Torvalds 2323bbdd8147SMarkos Chandras case w_fmt: { 2324bbdd8147SMarkos Chandras union ieee754dp fs; 2325bbdd8147SMarkos Chandras 23261da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 23271da177e4SLinus Torvalds case fcvts_op: 23281da177e4SLinus Torvalds /* convert word to single precision real */ 23291da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 23301da177e4SLinus Torvalds rv.s = ieee754sp_fint(fs.bits); 23311da177e4SLinus Torvalds rfmt = s_fmt; 23321da177e4SLinus Torvalds goto copcsr; 23331da177e4SLinus Torvalds case fcvtd_op: 23341da177e4SLinus Torvalds /* convert word to double precision real */ 23351da177e4SLinus Torvalds SPFROMREG(fs, MIPSInst_FS(ir)); 23361da177e4SLinus Torvalds rv.d = ieee754dp_fint(fs.bits); 23371da177e4SLinus Torvalds rfmt = d_fmt; 23381da177e4SLinus Torvalds goto copcsr; 2339f8c3c671SMarkos Chandras default: { 2340f8c3c671SMarkos Chandras /* Emulating the new CMP.condn.fmt R6 instruction */ 2341f8c3c671SMarkos Chandras #define CMPOP_MASK 0x7 2342f8c3c671SMarkos Chandras #define SIGN_BIT (0x1 << 3) 2343f8c3c671SMarkos Chandras #define PREDICATE_BIT (0x1 << 4) 2344f8c3c671SMarkos Chandras 2345f8c3c671SMarkos Chandras int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK; 2346f8c3c671SMarkos Chandras int sig = MIPSInst_FUNC(ir) & SIGN_BIT; 2347f8c3c671SMarkos Chandras union ieee754sp fs, ft; 2348f8c3c671SMarkos Chandras 2349f8c3c671SMarkos Chandras /* This is an R6 only instruction */ 2350f8c3c671SMarkos Chandras if (!cpu_has_mips_r6 || 2351f8c3c671SMarkos Chandras (MIPSInst_FUNC(ir) & 0x20)) 2352f8c3c671SMarkos Chandras return SIGILL; 2353f8c3c671SMarkos Chandras 2354f8c3c671SMarkos Chandras /* fmt is w_fmt for single precision so fix it */ 2355f8c3c671SMarkos Chandras rfmt = s_fmt; 2356f8c3c671SMarkos Chandras /* default to false */ 2357f8c3c671SMarkos Chandras rv.w = 0; 2358f8c3c671SMarkos Chandras 2359f8c3c671SMarkos Chandras /* CMP.condn.S */ 2360f8c3c671SMarkos Chandras SPFROMREG(fs, MIPSInst_FS(ir)); 2361f8c3c671SMarkos Chandras SPFROMREG(ft, MIPSInst_FT(ir)); 2362f8c3c671SMarkos Chandras 2363f8c3c671SMarkos Chandras /* positive predicates */ 2364f8c3c671SMarkos Chandras if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) { 2365f8c3c671SMarkos Chandras if (ieee754sp_cmp(fs, ft, cmptab[cmpop], 2366f8c3c671SMarkos Chandras sig)) 2367f8c3c671SMarkos Chandras rv.w = -1; /* true, all 1s */ 2368f8c3c671SMarkos Chandras if ((sig) && 2369f8c3c671SMarkos Chandras ieee754_cxtest(IEEE754_INVALID_OPERATION)) 2370f8c3c671SMarkos Chandras rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S; 2371f8c3c671SMarkos Chandras else 2372f8c3c671SMarkos Chandras goto copcsr; 2373f8c3c671SMarkos Chandras } else { 2374f8c3c671SMarkos Chandras /* negative predicates */ 2375f8c3c671SMarkos Chandras switch (cmpop) { 2376f8c3c671SMarkos Chandras case 1: 2377f8c3c671SMarkos Chandras case 2: 2378f8c3c671SMarkos Chandras case 3: 2379f8c3c671SMarkos Chandras if (ieee754sp_cmp(fs, ft, 2380f8c3c671SMarkos Chandras negative_cmptab[cmpop], 2381f8c3c671SMarkos Chandras sig)) 2382f8c3c671SMarkos Chandras rv.w = -1; /* true, all 1s */ 2383f8c3c671SMarkos Chandras if (sig && 2384f8c3c671SMarkos Chandras ieee754_cxtest(IEEE754_INVALID_OPERATION)) 2385f8c3c671SMarkos Chandras rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S; 2386f8c3c671SMarkos Chandras else 2387f8c3c671SMarkos Chandras goto copcsr; 2388f8c3c671SMarkos Chandras break; 23891da177e4SLinus Torvalds default: 2390f8c3c671SMarkos Chandras /* Reserved R6 ops */ 2391f8c3c671SMarkos Chandras pr_err("Reserved MIPS R6 CMP.condn.S operation\n"); 23921da177e4SLinus Torvalds return SIGILL; 23931da177e4SLinus Torvalds } 2394f8c3c671SMarkos Chandras } 23951da177e4SLinus Torvalds break; 23961da177e4SLinus Torvalds } 2397f8c3c671SMarkos Chandras } 2398f8c3c671SMarkos Chandras } 23991da177e4SLinus Torvalds 24003f7cac41SRalf Baechle case l_fmt: 240108a07904SRalf Baechle 24022d83fea7SMaciej W. Rozycki if (!cpu_has_mips_3_4_5_64_r2_r6) 240308a07904SRalf Baechle return SIGILL; 240408a07904SRalf Baechle 2405bbd426f5SPaul Burton DIFROMREG(bits, MIPSInst_FS(ir)); 2406bbd426f5SPaul Burton 24071da177e4SLinus Torvalds switch (MIPSInst_FUNC(ir)) { 24081da177e4SLinus Torvalds case fcvts_op: 24091da177e4SLinus Torvalds /* convert long to single precision real */ 2410bbd426f5SPaul Burton rv.s = ieee754sp_flong(bits); 24111da177e4SLinus Torvalds rfmt = s_fmt; 24121da177e4SLinus Torvalds goto copcsr; 24131da177e4SLinus Torvalds case fcvtd_op: 24141da177e4SLinus Torvalds /* convert long to double precision real */ 2415bbd426f5SPaul Burton rv.d = ieee754dp_flong(bits); 24161da177e4SLinus Torvalds rfmt = d_fmt; 24171da177e4SLinus Torvalds goto copcsr; 2418f8c3c671SMarkos Chandras default: { 2419f8c3c671SMarkos Chandras /* Emulating the new CMP.condn.fmt R6 instruction */ 2420f8c3c671SMarkos Chandras int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK; 2421f8c3c671SMarkos Chandras int sig = MIPSInst_FUNC(ir) & SIGN_BIT; 2422f8c3c671SMarkos Chandras union ieee754dp fs, ft; 2423f8c3c671SMarkos Chandras 2424f8c3c671SMarkos Chandras if (!cpu_has_mips_r6 || 2425f8c3c671SMarkos Chandras (MIPSInst_FUNC(ir) & 0x20)) 2426f8c3c671SMarkos Chandras return SIGILL; 2427f8c3c671SMarkos Chandras 2428f8c3c671SMarkos Chandras /* fmt is l_fmt for double precision so fix it */ 2429f8c3c671SMarkos Chandras rfmt = d_fmt; 2430f8c3c671SMarkos Chandras /* default to false */ 2431f8c3c671SMarkos Chandras rv.l = 0; 2432f8c3c671SMarkos Chandras 2433f8c3c671SMarkos Chandras /* CMP.condn.D */ 2434f8c3c671SMarkos Chandras DPFROMREG(fs, MIPSInst_FS(ir)); 2435f8c3c671SMarkos Chandras DPFROMREG(ft, MIPSInst_FT(ir)); 2436f8c3c671SMarkos Chandras 2437f8c3c671SMarkos Chandras /* positive predicates */ 2438f8c3c671SMarkos Chandras if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) { 2439f8c3c671SMarkos Chandras if (ieee754dp_cmp(fs, ft, 2440f8c3c671SMarkos Chandras cmptab[cmpop], sig)) 2441f8c3c671SMarkos Chandras rv.l = -1LL; /* true, all 1s */ 2442f8c3c671SMarkos Chandras if (sig && 2443f8c3c671SMarkos Chandras ieee754_cxtest(IEEE754_INVALID_OPERATION)) 2444f8c3c671SMarkos Chandras rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S; 2445f8c3c671SMarkos Chandras else 2446f8c3c671SMarkos Chandras goto copcsr; 2447f8c3c671SMarkos Chandras } else { 2448f8c3c671SMarkos Chandras /* negative predicates */ 2449f8c3c671SMarkos Chandras switch (cmpop) { 2450f8c3c671SMarkos Chandras case 1: 2451f8c3c671SMarkos Chandras case 2: 2452f8c3c671SMarkos Chandras case 3: 2453f8c3c671SMarkos Chandras if (ieee754dp_cmp(fs, ft, 2454f8c3c671SMarkos Chandras negative_cmptab[cmpop], 2455f8c3c671SMarkos Chandras sig)) 2456f8c3c671SMarkos Chandras rv.l = -1LL; /* true, all 1s */ 2457f8c3c671SMarkos Chandras if (sig && 2458f8c3c671SMarkos Chandras ieee754_cxtest(IEEE754_INVALID_OPERATION)) 2459f8c3c671SMarkos Chandras rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S; 2460f8c3c671SMarkos Chandras else 2461f8c3c671SMarkos Chandras goto copcsr; 2462f8c3c671SMarkos Chandras break; 24631da177e4SLinus Torvalds default: 2464f8c3c671SMarkos Chandras /* Reserved R6 ops */ 2465f8c3c671SMarkos Chandras pr_err("Reserved MIPS R6 CMP.condn.D operation\n"); 24661da177e4SLinus Torvalds return SIGILL; 24671da177e4SLinus Torvalds } 2468f8c3c671SMarkos Chandras } 24691da177e4SLinus Torvalds break; 2470f8c3c671SMarkos Chandras } 2471f8c3c671SMarkos Chandras } 24721da177e4SLinus Torvalds default: 24731da177e4SLinus Torvalds return SIGILL; 24741da177e4SLinus Torvalds } 24751da177e4SLinus Torvalds 24761da177e4SLinus Torvalds /* 24771da177e4SLinus Torvalds * Update the fpu CSR register for this operation. 24781da177e4SLinus Torvalds * If an exception is required, generate a tidy SIGFPE exception, 24791da177e4SLinus Torvalds * without updating the result register. 24801da177e4SLinus Torvalds * Note: cause exception bits do not accumulate, they are rewritten 24811da177e4SLinus Torvalds * for each op; only the flag/sticky bits accumulate. 24821da177e4SLinus Torvalds */ 24831da177e4SLinus Torvalds ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr; 24841da177e4SLinus Torvalds if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) { 24853f7cac41SRalf Baechle /*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */ 24861da177e4SLinus Torvalds return SIGFPE; 24871da177e4SLinus Torvalds } 24881da177e4SLinus Torvalds 24891da177e4SLinus Torvalds /* 24901da177e4SLinus Torvalds * Now we can safely write the result back to the register file. 24911da177e4SLinus Torvalds */ 24921da177e4SLinus Torvalds switch (rfmt) { 249308a07904SRalf Baechle case -1: 249408a07904SRalf Baechle 249508a07904SRalf Baechle if (cpu_has_mips_4_5_r) 2496c3b9b945SRob Kendrick cbit = fpucondbit[MIPSInst_FD(ir) >> 2]; 24971da177e4SLinus Torvalds else 249808a07904SRalf Baechle cbit = FPU_CSR_COND; 249908a07904SRalf Baechle if (rv.w) 250008a07904SRalf Baechle ctx->fcr31 |= cbit; 250108a07904SRalf Baechle else 250208a07904SRalf Baechle ctx->fcr31 &= ~cbit; 25031da177e4SLinus Torvalds break; 250408a07904SRalf Baechle 25051da177e4SLinus Torvalds case d_fmt: 25061da177e4SLinus Torvalds DPTOREG(rv.d, MIPSInst_FD(ir)); 25071da177e4SLinus Torvalds break; 25081da177e4SLinus Torvalds case s_fmt: 25091da177e4SLinus Torvalds SPTOREG(rv.s, MIPSInst_FD(ir)); 25101da177e4SLinus Torvalds break; 25111da177e4SLinus Torvalds case w_fmt: 25121da177e4SLinus Torvalds SITOREG(rv.w, MIPSInst_FD(ir)); 25131da177e4SLinus Torvalds break; 25141da177e4SLinus Torvalds case l_fmt: 25152d83fea7SMaciej W. Rozycki if (!cpu_has_mips_3_4_5_64_r2_r6) 251608a07904SRalf Baechle return SIGILL; 251708a07904SRalf Baechle 25181da177e4SLinus Torvalds DITOREG(rv.l, MIPSInst_FD(ir)); 25191da177e4SLinus Torvalds break; 25201da177e4SLinus Torvalds default: 25211da177e4SLinus Torvalds return SIGILL; 25221da177e4SLinus Torvalds } 25231da177e4SLinus Torvalds 25241da177e4SLinus Torvalds return 0; 25251da177e4SLinus Torvalds } 25261da177e4SLinus Torvalds 2527*13769ebaSMaciej W. Rozycki /* 2528*13769ebaSMaciej W. Rozycki * Emulate FPU instructions. 2529*13769ebaSMaciej W. Rozycki * 2530*13769ebaSMaciej W. Rozycki * If we use FPU hardware, then we have been typically called to handle 2531*13769ebaSMaciej W. Rozycki * an unimplemented operation, such as where an operand is a NaN or 2532*13769ebaSMaciej W. Rozycki * denormalized. In that case exit the emulation loop after a single 2533*13769ebaSMaciej W. Rozycki * iteration so as to let hardware execute any subsequent instructions. 2534*13769ebaSMaciej W. Rozycki * 2535*13769ebaSMaciej W. Rozycki * If we have no FPU hardware or it has been disabled, then continue 2536*13769ebaSMaciej W. Rozycki * emulating floating-point instructions until one of these conditions 2537*13769ebaSMaciej W. Rozycki * has occurred: 2538*13769ebaSMaciej W. Rozycki * 2539*13769ebaSMaciej W. Rozycki * - a non-FPU instruction has been encountered, 2540*13769ebaSMaciej W. Rozycki * 2541*13769ebaSMaciej W. Rozycki * - an attempt to emulate has ended with a signal, 2542*13769ebaSMaciej W. Rozycki * 2543*13769ebaSMaciej W. Rozycki * - the ISA mode has been switched. 2544*13769ebaSMaciej W. Rozycki * 2545*13769ebaSMaciej W. Rozycki * We need to terminate the emulation loop if we got switched to the 2546*13769ebaSMaciej W. Rozycki * MIPS16 mode, whether supported or not, so that we do not attempt 2547*13769ebaSMaciej W. Rozycki * to emulate a MIPS16 instruction as a regular MIPS FPU instruction. 2548*13769ebaSMaciej W. Rozycki * Similarly if we got switched to the microMIPS mode and only the 2549*13769ebaSMaciej W. Rozycki * regular MIPS mode is supported, so that we do not attempt to emulate 2550*13769ebaSMaciej W. Rozycki * a microMIPS instruction as a regular MIPS FPU instruction. Or if 2551*13769ebaSMaciej W. Rozycki * we got switched to the regular MIPS mode and only the microMIPS mode 2552*13769ebaSMaciej W. Rozycki * is supported, so that we do not attempt to emulate a regular MIPS 2553*13769ebaSMaciej W. Rozycki * instruction that should cause an Address Error exception instead. 2554*13769ebaSMaciej W. Rozycki * For simplicity we always terminate upon an ISA mode switch. 2555*13769ebaSMaciej W. Rozycki */ 2556e04582b7SAtsushi Nemoto int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx, 2557515b029dSDavid Daney int has_fpu, void *__user *fault_addr) 25581da177e4SLinus Torvalds { 2559333d1f67SRalf Baechle unsigned long oldepc, prevepc; 2560102cedc3SLeonid Yegoshin struct mm_decoded_insn dec_insn; 2561102cedc3SLeonid Yegoshin u16 instr[4]; 2562102cedc3SLeonid Yegoshin u16 *instr_ptr; 25631da177e4SLinus Torvalds int sig = 0; 25641da177e4SLinus Torvalds 25651da177e4SLinus Torvalds oldepc = xcp->cp0_epc; 25661da177e4SLinus Torvalds do { 25671da177e4SLinus Torvalds prevepc = xcp->cp0_epc; 25681da177e4SLinus Torvalds 2569102cedc3SLeonid Yegoshin if (get_isa16_mode(prevepc) && cpu_has_mmips) { 2570102cedc3SLeonid Yegoshin /* 2571102cedc3SLeonid Yegoshin * Get next 2 microMIPS instructions and convert them 2572102cedc3SLeonid Yegoshin * into 32-bit instructions. 2573102cedc3SLeonid Yegoshin */ 2574102cedc3SLeonid Yegoshin if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) || 2575102cedc3SLeonid Yegoshin (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) || 2576102cedc3SLeonid Yegoshin (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) || 2577102cedc3SLeonid Yegoshin (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) { 2578b6ee75edSDavid Daney MIPS_FPU_EMU_INC_STATS(errors); 25791da177e4SLinus Torvalds return SIGBUS; 25801da177e4SLinus Torvalds } 2581102cedc3SLeonid Yegoshin instr_ptr = instr; 2582102cedc3SLeonid Yegoshin 2583102cedc3SLeonid Yegoshin /* Get first instruction. */ 2584102cedc3SLeonid Yegoshin if (mm_insn_16bit(*instr_ptr)) { 2585102cedc3SLeonid Yegoshin /* Duplicate the half-word. */ 2586102cedc3SLeonid Yegoshin dec_insn.insn = (*instr_ptr << 16) | 2587102cedc3SLeonid Yegoshin (*instr_ptr); 2588102cedc3SLeonid Yegoshin /* 16-bit instruction. */ 2589102cedc3SLeonid Yegoshin dec_insn.pc_inc = 2; 2590102cedc3SLeonid Yegoshin instr_ptr += 1; 2591102cedc3SLeonid Yegoshin } else { 2592102cedc3SLeonid Yegoshin dec_insn.insn = (*instr_ptr << 16) | 2593102cedc3SLeonid Yegoshin *(instr_ptr+1); 2594102cedc3SLeonid Yegoshin /* 32-bit instruction. */ 2595102cedc3SLeonid Yegoshin dec_insn.pc_inc = 4; 2596102cedc3SLeonid Yegoshin instr_ptr += 2; 2597515b029dSDavid Daney } 2598102cedc3SLeonid Yegoshin /* Get second instruction. */ 2599102cedc3SLeonid Yegoshin if (mm_insn_16bit(*instr_ptr)) { 2600102cedc3SLeonid Yegoshin /* Duplicate the half-word. */ 2601102cedc3SLeonid Yegoshin dec_insn.next_insn = (*instr_ptr << 16) | 2602102cedc3SLeonid Yegoshin (*instr_ptr); 2603102cedc3SLeonid Yegoshin /* 16-bit instruction. */ 2604102cedc3SLeonid Yegoshin dec_insn.next_pc_inc = 2; 2605102cedc3SLeonid Yegoshin } else { 2606102cedc3SLeonid Yegoshin dec_insn.next_insn = (*instr_ptr << 16) | 2607102cedc3SLeonid Yegoshin *(instr_ptr+1); 2608102cedc3SLeonid Yegoshin /* 32-bit instruction. */ 2609102cedc3SLeonid Yegoshin dec_insn.next_pc_inc = 4; 2610102cedc3SLeonid Yegoshin } 2611102cedc3SLeonid Yegoshin dec_insn.micro_mips_mode = 1; 2612102cedc3SLeonid Yegoshin } else { 2613102cedc3SLeonid Yegoshin if ((get_user(dec_insn.insn, 2614102cedc3SLeonid Yegoshin (mips_instruction __user *) xcp->cp0_epc)) || 2615102cedc3SLeonid Yegoshin (get_user(dec_insn.next_insn, 2616102cedc3SLeonid Yegoshin (mips_instruction __user *)(xcp->cp0_epc+4)))) { 2617102cedc3SLeonid Yegoshin MIPS_FPU_EMU_INC_STATS(errors); 2618102cedc3SLeonid Yegoshin return SIGBUS; 2619102cedc3SLeonid Yegoshin } 2620102cedc3SLeonid Yegoshin dec_insn.pc_inc = 4; 2621102cedc3SLeonid Yegoshin dec_insn.next_pc_inc = 4; 2622102cedc3SLeonid Yegoshin dec_insn.micro_mips_mode = 0; 2623102cedc3SLeonid Yegoshin } 2624102cedc3SLeonid Yegoshin 2625102cedc3SLeonid Yegoshin if ((dec_insn.insn == 0) || 2626102cedc3SLeonid Yegoshin ((dec_insn.pc_inc == 2) && 2627102cedc3SLeonid Yegoshin ((dec_insn.insn & 0xffff) == MM_NOP16))) 2628102cedc3SLeonid Yegoshin xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */ 26291da177e4SLinus Torvalds else { 2630cd21dfcfSRalf Baechle /* 26312cfcf8a8SMaciej W. Rozycki * The 'ieee754_csr' is an alias of ctx->fcr31. 26322cfcf8a8SMaciej W. Rozycki * No need to copy ctx->fcr31 to ieee754_csr. 2633cd21dfcfSRalf Baechle */ 2634102cedc3SLeonid Yegoshin sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr); 26351da177e4SLinus Torvalds } 26361da177e4SLinus Torvalds 2637e04582b7SAtsushi Nemoto if (has_fpu) 26381da177e4SLinus Torvalds break; 26391da177e4SLinus Torvalds if (sig) 26401da177e4SLinus Torvalds break; 2641*13769ebaSMaciej W. Rozycki /* 2642*13769ebaSMaciej W. Rozycki * We have to check for the ISA bit explicitly here, 2643*13769ebaSMaciej W. Rozycki * because `get_isa16_mode' may return 0 if support 2644*13769ebaSMaciej W. Rozycki * for code compression has been globally disabled, 2645*13769ebaSMaciej W. Rozycki * or otherwise we may produce the wrong signal or 2646*13769ebaSMaciej W. Rozycki * even proceed successfully where we must not. 2647*13769ebaSMaciej W. Rozycki */ 2648*13769ebaSMaciej W. Rozycki if ((xcp->cp0_epc ^ prevepc) & 0x1) 2649*13769ebaSMaciej W. Rozycki break; 26501da177e4SLinus Torvalds 26511da177e4SLinus Torvalds cond_resched(); 26521da177e4SLinus Torvalds } while (xcp->cp0_epc > prevepc); 26531da177e4SLinus Torvalds 26541da177e4SLinus Torvalds /* SIGILL indicates a non-fpu instruction */ 26551da177e4SLinus Torvalds if (sig == SIGILL && xcp->cp0_epc != oldepc) 26563f7cac41SRalf Baechle /* but if EPC has advanced, then ignore it */ 26571da177e4SLinus Torvalds sig = 0; 26581da177e4SLinus Torvalds 26591da177e4SLinus Torvalds return sig; 26601da177e4SLinus Torvalds } 2661