Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * cp1emu.c: a MIPS coprocessor 1 (fpu) instruction emulator |
| 3 | * |
| 4 | * MIPS floating point support |
| 5 | * Copyright (C) 1994-2000 Algorithmics Ltd. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com |
| 8 | * Copyright (C) 2000 MIPS Technologies, Inc. |
| 9 | * |
| 10 | * This program is free software; you can distribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License (Version 2) as |
| 12 | * published by the Free Software Foundation. |
| 13 | * |
| 14 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 17 | * for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA. |
| 22 | * |
| 23 | * A complete emulator for MIPS coprocessor 1 instructions. This is |
| 24 | * required for #float(switch) or #float(trap), where it catches all |
| 25 | * COP1 instructions via the "CoProcessor Unusable" exception. |
| 26 | * |
| 27 | * More surprisingly it is also required for #float(ieee), to help out |
| 28 | * the hardware fpu at the boundaries of the IEEE-754 representation |
| 29 | * (denormalised values, infinities, underflow, etc). It is made |
| 30 | * quite nasty because emulation of some non-COP1 instructions is |
| 31 | * required, e.g. in branch delay slots. |
| 32 | * |
| 33 | * Note if you know that you won't have an fpu, then you'll get much |
| 34 | * better performance by compiling with -msoft-float! |
| 35 | */ |
| 36 | #include <linux/sched.h> |
Atsushi Nemoto | 83fd38c | 2007-07-07 23:21:49 +0900 | [diff] [blame] | 37 | #include <linux/debugfs.h> |
Ralf Baechle | 85c51c5 | 2014-04-16 02:46:11 +0200 | [diff] [blame] | 38 | #include <linux/percpu-defs.h> |
Deng-Cheng Zhu | 7f788d2 | 2010-10-12 19:37:21 +0800 | [diff] [blame] | 39 | #include <linux/perf_event.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Ralf Baechle | cd8ee34 | 2014-04-16 02:09:53 +0200 | [diff] [blame] | 41 | #include <asm/branch.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <asm/inst.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <asm/ptrace.h> |
| 44 | #include <asm/signal.h> |
Ralf Baechle | cd8ee34 | 2014-04-16 02:09:53 +0200 | [diff] [blame] | 45 | #include <asm/uaccess.h> |
| 46 | |
| 47 | #include <asm/processor.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <asm/fpu_emulator.h> |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 49 | #include <asm/fpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | #include "ieee754.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | /* Strap kernel emulator for full MIPS IV emulation */ |
| 54 | |
| 55 | #ifdef __mips |
| 56 | #undef __mips |
| 57 | #endif |
| 58 | #define __mips 4 |
| 59 | |
| 60 | /* Function which emulates a floating point instruction. */ |
| 61 | |
Atsushi Nemoto | eae8907 | 2006-05-16 01:26:03 +0900 | [diff] [blame] | 62 | static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | mips_instruction); |
| 64 | |
| 65 | #if __mips >= 4 && __mips != 32 |
| 66 | static int fpux_emu(struct pt_regs *, |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 67 | struct mips_fpu_struct *, mips_instruction, void *__user *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #endif |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | /* Control registers */ |
| 71 | |
| 72 | #define FPCREG_RID 0 /* $0 = revision id */ |
| 73 | #define FPCREG_CSR 31 /* $31 = csr */ |
| 74 | |
Shane McDonald | 95e8f63 | 2010-05-06 23:26:57 -0600 | [diff] [blame] | 75 | /* Determine rounding mode from the RM bits of the FCSR */ |
| 76 | #define modeindex(v) ((v) & FPU_CSR_RM) |
| 77 | |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 78 | /* microMIPS bitfields */ |
| 79 | #define MM_POOL32A_MINOR_MASK 0x3f |
| 80 | #define MM_POOL32A_MINOR_SHIFT 0x6 |
| 81 | #define MM_MIPS32_COND_FC 0x30 |
| 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | /* Convert Mips rounding mode (0..3) to IEEE library modes. */ |
| 84 | static const unsigned char ieee_rm[4] = { |
Ralf Baechle | cd21dfc | 2005-04-28 13:39:10 +0000 | [diff] [blame] | 85 | [FPU_CSR_RN] = IEEE754_RN, |
| 86 | [FPU_CSR_RZ] = IEEE754_RZ, |
| 87 | [FPU_CSR_RU] = IEEE754_RU, |
| 88 | [FPU_CSR_RD] = IEEE754_RD, |
| 89 | }; |
| 90 | /* Convert IEEE library modes to Mips rounding mode (0..3). */ |
| 91 | static const unsigned char mips_rm[4] = { |
| 92 | [IEEE754_RN] = FPU_CSR_RN, |
| 93 | [IEEE754_RZ] = FPU_CSR_RZ, |
| 94 | [IEEE754_RD] = FPU_CSR_RD, |
| 95 | [IEEE754_RU] = FPU_CSR_RU, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | #if __mips >= 4 |
| 99 | /* convert condition code register number to csr bit */ |
| 100 | static const unsigned int fpucondbit[8] = { |
| 101 | FPU_CSR_COND0, |
| 102 | FPU_CSR_COND1, |
| 103 | FPU_CSR_COND2, |
| 104 | FPU_CSR_COND3, |
| 105 | FPU_CSR_COND4, |
| 106 | FPU_CSR_COND5, |
| 107 | FPU_CSR_COND6, |
| 108 | FPU_CSR_COND7 |
| 109 | }; |
| 110 | #endif |
| 111 | |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 112 | /* (microMIPS) Convert 16-bit register encoding to 32-bit register encoding. */ |
| 113 | static const unsigned int reg16to32map[8] = {16, 17, 2, 3, 4, 5, 6, 7}; |
| 114 | |
| 115 | /* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */ |
| 116 | static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0}; |
| 117 | static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0}; |
| 118 | static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0}; |
| 119 | static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0}; |
| 120 | |
| 121 | /* |
| 122 | * This functions translates a 32-bit microMIPS instruction |
| 123 | * into a 32-bit MIPS32 instruction. Returns 0 on success |
| 124 | * and SIGILL otherwise. |
| 125 | */ |
| 126 | static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr) |
| 127 | { |
| 128 | union mips_instruction insn = *insn_ptr; |
| 129 | union mips_instruction mips32_insn = insn; |
| 130 | int func, fmt, op; |
| 131 | |
| 132 | switch (insn.mm_i_format.opcode) { |
| 133 | case mm_ldc132_op: |
| 134 | mips32_insn.mm_i_format.opcode = ldc1_op; |
| 135 | mips32_insn.mm_i_format.rt = insn.mm_i_format.rs; |
| 136 | mips32_insn.mm_i_format.rs = insn.mm_i_format.rt; |
| 137 | break; |
| 138 | case mm_lwc132_op: |
| 139 | mips32_insn.mm_i_format.opcode = lwc1_op; |
| 140 | mips32_insn.mm_i_format.rt = insn.mm_i_format.rs; |
| 141 | mips32_insn.mm_i_format.rs = insn.mm_i_format.rt; |
| 142 | break; |
| 143 | case mm_sdc132_op: |
| 144 | mips32_insn.mm_i_format.opcode = sdc1_op; |
| 145 | mips32_insn.mm_i_format.rt = insn.mm_i_format.rs; |
| 146 | mips32_insn.mm_i_format.rs = insn.mm_i_format.rt; |
| 147 | break; |
| 148 | case mm_swc132_op: |
| 149 | mips32_insn.mm_i_format.opcode = swc1_op; |
| 150 | mips32_insn.mm_i_format.rt = insn.mm_i_format.rs; |
| 151 | mips32_insn.mm_i_format.rs = insn.mm_i_format.rt; |
| 152 | break; |
| 153 | case mm_pool32i_op: |
| 154 | /* NOTE: offset is << by 1 if in microMIPS mode. */ |
| 155 | if ((insn.mm_i_format.rt == mm_bc1f_op) || |
| 156 | (insn.mm_i_format.rt == mm_bc1t_op)) { |
| 157 | mips32_insn.fb_format.opcode = cop1_op; |
| 158 | mips32_insn.fb_format.bc = bc_op; |
| 159 | mips32_insn.fb_format.flag = |
| 160 | (insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0; |
| 161 | } else |
| 162 | return SIGILL; |
| 163 | break; |
| 164 | case mm_pool32f_op: |
| 165 | switch (insn.mm_fp0_format.func) { |
| 166 | case mm_32f_01_op: |
| 167 | case mm_32f_11_op: |
| 168 | case mm_32f_02_op: |
| 169 | case mm_32f_12_op: |
| 170 | case mm_32f_41_op: |
| 171 | case mm_32f_51_op: |
| 172 | case mm_32f_42_op: |
| 173 | case mm_32f_52_op: |
| 174 | op = insn.mm_fp0_format.func; |
| 175 | if (op == mm_32f_01_op) |
| 176 | func = madd_s_op; |
| 177 | else if (op == mm_32f_11_op) |
| 178 | func = madd_d_op; |
| 179 | else if (op == mm_32f_02_op) |
| 180 | func = nmadd_s_op; |
| 181 | else if (op == mm_32f_12_op) |
| 182 | func = nmadd_d_op; |
| 183 | else if (op == mm_32f_41_op) |
| 184 | func = msub_s_op; |
| 185 | else if (op == mm_32f_51_op) |
| 186 | func = msub_d_op; |
| 187 | else if (op == mm_32f_42_op) |
| 188 | func = nmsub_s_op; |
| 189 | else |
| 190 | func = nmsub_d_op; |
| 191 | mips32_insn.fp6_format.opcode = cop1x_op; |
| 192 | mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr; |
| 193 | mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft; |
| 194 | mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs; |
| 195 | mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd; |
| 196 | mips32_insn.fp6_format.func = func; |
| 197 | break; |
| 198 | case mm_32f_10_op: |
| 199 | func = -1; /* Invalid */ |
| 200 | op = insn.mm_fp5_format.op & 0x7; |
| 201 | if (op == mm_ldxc1_op) |
| 202 | func = ldxc1_op; |
| 203 | else if (op == mm_sdxc1_op) |
| 204 | func = sdxc1_op; |
| 205 | else if (op == mm_lwxc1_op) |
| 206 | func = lwxc1_op; |
| 207 | else if (op == mm_swxc1_op) |
| 208 | func = swxc1_op; |
| 209 | |
| 210 | if (func != -1) { |
| 211 | mips32_insn.r_format.opcode = cop1x_op; |
| 212 | mips32_insn.r_format.rs = |
| 213 | insn.mm_fp5_format.base; |
| 214 | mips32_insn.r_format.rt = |
| 215 | insn.mm_fp5_format.index; |
| 216 | mips32_insn.r_format.rd = 0; |
| 217 | mips32_insn.r_format.re = insn.mm_fp5_format.fd; |
| 218 | mips32_insn.r_format.func = func; |
| 219 | } else |
| 220 | return SIGILL; |
| 221 | break; |
| 222 | case mm_32f_40_op: |
| 223 | op = -1; /* Invalid */ |
| 224 | if (insn.mm_fp2_format.op == mm_fmovt_op) |
| 225 | op = 1; |
| 226 | else if (insn.mm_fp2_format.op == mm_fmovf_op) |
| 227 | op = 0; |
| 228 | if (op != -1) { |
| 229 | mips32_insn.fp0_format.opcode = cop1_op; |
| 230 | mips32_insn.fp0_format.fmt = |
| 231 | sdps_format[insn.mm_fp2_format.fmt]; |
| 232 | mips32_insn.fp0_format.ft = |
| 233 | (insn.mm_fp2_format.cc<<2) + op; |
| 234 | mips32_insn.fp0_format.fs = |
| 235 | insn.mm_fp2_format.fs; |
| 236 | mips32_insn.fp0_format.fd = |
| 237 | insn.mm_fp2_format.fd; |
| 238 | mips32_insn.fp0_format.func = fmovc_op; |
| 239 | } else |
| 240 | return SIGILL; |
| 241 | break; |
| 242 | case mm_32f_60_op: |
| 243 | func = -1; /* Invalid */ |
| 244 | if (insn.mm_fp0_format.op == mm_fadd_op) |
| 245 | func = fadd_op; |
| 246 | else if (insn.mm_fp0_format.op == mm_fsub_op) |
| 247 | func = fsub_op; |
| 248 | else if (insn.mm_fp0_format.op == mm_fmul_op) |
| 249 | func = fmul_op; |
| 250 | else if (insn.mm_fp0_format.op == mm_fdiv_op) |
| 251 | func = fdiv_op; |
| 252 | if (func != -1) { |
| 253 | mips32_insn.fp0_format.opcode = cop1_op; |
| 254 | mips32_insn.fp0_format.fmt = |
| 255 | sdps_format[insn.mm_fp0_format.fmt]; |
| 256 | mips32_insn.fp0_format.ft = |
| 257 | insn.mm_fp0_format.ft; |
| 258 | mips32_insn.fp0_format.fs = |
| 259 | insn.mm_fp0_format.fs; |
| 260 | mips32_insn.fp0_format.fd = |
| 261 | insn.mm_fp0_format.fd; |
| 262 | mips32_insn.fp0_format.func = func; |
| 263 | } else |
| 264 | return SIGILL; |
| 265 | break; |
| 266 | case mm_32f_70_op: |
| 267 | func = -1; /* Invalid */ |
| 268 | if (insn.mm_fp0_format.op == mm_fmovn_op) |
| 269 | func = fmovn_op; |
| 270 | else if (insn.mm_fp0_format.op == mm_fmovz_op) |
| 271 | func = fmovz_op; |
| 272 | if (func != -1) { |
| 273 | mips32_insn.fp0_format.opcode = cop1_op; |
| 274 | mips32_insn.fp0_format.fmt = |
| 275 | sdps_format[insn.mm_fp0_format.fmt]; |
| 276 | mips32_insn.fp0_format.ft = |
| 277 | insn.mm_fp0_format.ft; |
| 278 | mips32_insn.fp0_format.fs = |
| 279 | insn.mm_fp0_format.fs; |
| 280 | mips32_insn.fp0_format.fd = |
| 281 | insn.mm_fp0_format.fd; |
| 282 | mips32_insn.fp0_format.func = func; |
| 283 | } else |
| 284 | return SIGILL; |
| 285 | break; |
| 286 | case mm_32f_73_op: /* POOL32FXF */ |
| 287 | switch (insn.mm_fp1_format.op) { |
| 288 | case mm_movf0_op: |
| 289 | case mm_movf1_op: |
| 290 | case mm_movt0_op: |
| 291 | case mm_movt1_op: |
| 292 | if ((insn.mm_fp1_format.op & 0x7f) == |
| 293 | mm_movf0_op) |
| 294 | op = 0; |
| 295 | else |
| 296 | op = 1; |
| 297 | mips32_insn.r_format.opcode = spec_op; |
| 298 | mips32_insn.r_format.rs = insn.mm_fp4_format.fs; |
| 299 | mips32_insn.r_format.rt = |
| 300 | (insn.mm_fp4_format.cc << 2) + op; |
| 301 | mips32_insn.r_format.rd = insn.mm_fp4_format.rt; |
| 302 | mips32_insn.r_format.re = 0; |
| 303 | mips32_insn.r_format.func = movc_op; |
| 304 | break; |
| 305 | case mm_fcvtd0_op: |
| 306 | case mm_fcvtd1_op: |
| 307 | case mm_fcvts0_op: |
| 308 | case mm_fcvts1_op: |
| 309 | if ((insn.mm_fp1_format.op & 0x7f) == |
| 310 | mm_fcvtd0_op) { |
| 311 | func = fcvtd_op; |
| 312 | fmt = swl_format[insn.mm_fp3_format.fmt]; |
| 313 | } else { |
| 314 | func = fcvts_op; |
| 315 | fmt = dwl_format[insn.mm_fp3_format.fmt]; |
| 316 | } |
| 317 | mips32_insn.fp0_format.opcode = cop1_op; |
| 318 | mips32_insn.fp0_format.fmt = fmt; |
| 319 | mips32_insn.fp0_format.ft = 0; |
| 320 | mips32_insn.fp0_format.fs = |
| 321 | insn.mm_fp3_format.fs; |
| 322 | mips32_insn.fp0_format.fd = |
| 323 | insn.mm_fp3_format.rt; |
| 324 | mips32_insn.fp0_format.func = func; |
| 325 | break; |
| 326 | case mm_fmov0_op: |
| 327 | case mm_fmov1_op: |
| 328 | case mm_fabs0_op: |
| 329 | case mm_fabs1_op: |
| 330 | case mm_fneg0_op: |
| 331 | case mm_fneg1_op: |
| 332 | if ((insn.mm_fp1_format.op & 0x7f) == |
| 333 | mm_fmov0_op) |
| 334 | func = fmov_op; |
| 335 | else if ((insn.mm_fp1_format.op & 0x7f) == |
| 336 | mm_fabs0_op) |
| 337 | func = fabs_op; |
| 338 | else |
| 339 | func = fneg_op; |
| 340 | mips32_insn.fp0_format.opcode = cop1_op; |
| 341 | mips32_insn.fp0_format.fmt = |
| 342 | sdps_format[insn.mm_fp3_format.fmt]; |
| 343 | mips32_insn.fp0_format.ft = 0; |
| 344 | mips32_insn.fp0_format.fs = |
| 345 | insn.mm_fp3_format.fs; |
| 346 | mips32_insn.fp0_format.fd = |
| 347 | insn.mm_fp3_format.rt; |
| 348 | mips32_insn.fp0_format.func = func; |
| 349 | break; |
| 350 | case mm_ffloorl_op: |
| 351 | case mm_ffloorw_op: |
| 352 | case mm_fceill_op: |
| 353 | case mm_fceilw_op: |
| 354 | case mm_ftruncl_op: |
| 355 | case mm_ftruncw_op: |
| 356 | case mm_froundl_op: |
| 357 | case mm_froundw_op: |
| 358 | case mm_fcvtl_op: |
| 359 | case mm_fcvtw_op: |
| 360 | if (insn.mm_fp1_format.op == mm_ffloorl_op) |
| 361 | func = ffloorl_op; |
| 362 | else if (insn.mm_fp1_format.op == mm_ffloorw_op) |
| 363 | func = ffloor_op; |
| 364 | else if (insn.mm_fp1_format.op == mm_fceill_op) |
| 365 | func = fceill_op; |
| 366 | else if (insn.mm_fp1_format.op == mm_fceilw_op) |
| 367 | func = fceil_op; |
| 368 | else if (insn.mm_fp1_format.op == mm_ftruncl_op) |
| 369 | func = ftruncl_op; |
| 370 | else if (insn.mm_fp1_format.op == mm_ftruncw_op) |
| 371 | func = ftrunc_op; |
| 372 | else if (insn.mm_fp1_format.op == mm_froundl_op) |
| 373 | func = froundl_op; |
| 374 | else if (insn.mm_fp1_format.op == mm_froundw_op) |
| 375 | func = fround_op; |
| 376 | else if (insn.mm_fp1_format.op == mm_fcvtl_op) |
| 377 | func = fcvtl_op; |
| 378 | else |
| 379 | func = fcvtw_op; |
| 380 | mips32_insn.fp0_format.opcode = cop1_op; |
| 381 | mips32_insn.fp0_format.fmt = |
| 382 | sd_format[insn.mm_fp1_format.fmt]; |
| 383 | mips32_insn.fp0_format.ft = 0; |
| 384 | mips32_insn.fp0_format.fs = |
| 385 | insn.mm_fp1_format.fs; |
| 386 | mips32_insn.fp0_format.fd = |
| 387 | insn.mm_fp1_format.rt; |
| 388 | mips32_insn.fp0_format.func = func; |
| 389 | break; |
| 390 | case mm_frsqrt_op: |
| 391 | case mm_fsqrt_op: |
| 392 | case mm_frecip_op: |
| 393 | if (insn.mm_fp1_format.op == mm_frsqrt_op) |
| 394 | func = frsqrt_op; |
| 395 | else if (insn.mm_fp1_format.op == mm_fsqrt_op) |
| 396 | func = fsqrt_op; |
| 397 | else |
| 398 | func = frecip_op; |
| 399 | mips32_insn.fp0_format.opcode = cop1_op; |
| 400 | mips32_insn.fp0_format.fmt = |
| 401 | sdps_format[insn.mm_fp1_format.fmt]; |
| 402 | mips32_insn.fp0_format.ft = 0; |
| 403 | mips32_insn.fp0_format.fs = |
| 404 | insn.mm_fp1_format.fs; |
| 405 | mips32_insn.fp0_format.fd = |
| 406 | insn.mm_fp1_format.rt; |
| 407 | mips32_insn.fp0_format.func = func; |
| 408 | break; |
| 409 | case mm_mfc1_op: |
| 410 | case mm_mtc1_op: |
| 411 | case mm_cfc1_op: |
| 412 | case mm_ctc1_op: |
Steven J. Hill | 9355e59 | 2013-11-07 12:48:29 +0000 | [diff] [blame] | 413 | case mm_mfhc1_op: |
| 414 | case mm_mthc1_op: |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 415 | if (insn.mm_fp1_format.op == mm_mfc1_op) |
| 416 | op = mfc_op; |
| 417 | else if (insn.mm_fp1_format.op == mm_mtc1_op) |
| 418 | op = mtc_op; |
| 419 | else if (insn.mm_fp1_format.op == mm_cfc1_op) |
| 420 | op = cfc_op; |
Steven J. Hill | 9355e59 | 2013-11-07 12:48:29 +0000 | [diff] [blame] | 421 | else if (insn.mm_fp1_format.op == mm_ctc1_op) |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 422 | op = ctc_op; |
Steven J. Hill | 9355e59 | 2013-11-07 12:48:29 +0000 | [diff] [blame] | 423 | else if (insn.mm_fp1_format.op == mm_mfhc1_op) |
| 424 | op = mfhc_op; |
| 425 | else |
| 426 | op = mthc_op; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 427 | mips32_insn.fp1_format.opcode = cop1_op; |
| 428 | mips32_insn.fp1_format.op = op; |
| 429 | mips32_insn.fp1_format.rt = |
| 430 | insn.mm_fp1_format.rt; |
| 431 | mips32_insn.fp1_format.fs = |
| 432 | insn.mm_fp1_format.fs; |
| 433 | mips32_insn.fp1_format.fd = 0; |
| 434 | mips32_insn.fp1_format.func = 0; |
| 435 | break; |
| 436 | default: |
| 437 | return SIGILL; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 438 | } |
| 439 | break; |
| 440 | case mm_32f_74_op: /* c.cond.fmt */ |
| 441 | mips32_insn.fp0_format.opcode = cop1_op; |
| 442 | mips32_insn.fp0_format.fmt = |
| 443 | sdps_format[insn.mm_fp4_format.fmt]; |
| 444 | mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt; |
| 445 | mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs; |
| 446 | mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2; |
| 447 | mips32_insn.fp0_format.func = |
| 448 | insn.mm_fp4_format.cond | MM_MIPS32_COND_FC; |
| 449 | break; |
| 450 | default: |
| 451 | return SIGILL; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 452 | } |
| 453 | break; |
| 454 | default: |
| 455 | return SIGILL; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | *insn_ptr = mips32_insn; |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | int mm_isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn, |
| 463 | unsigned long *contpc) |
| 464 | { |
| 465 | union mips_instruction insn = (union mips_instruction)dec_insn.insn; |
| 466 | int bc_false = 0; |
| 467 | unsigned int fcr31; |
| 468 | unsigned int bit; |
| 469 | |
David Daney | fe6d290 | 2013-05-24 20:54:09 +0000 | [diff] [blame] | 470 | if (!cpu_has_mmips) |
| 471 | return 0; |
| 472 | |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 473 | switch (insn.mm_i_format.opcode) { |
| 474 | case mm_pool32a_op: |
| 475 | if ((insn.mm_i_format.simmediate & MM_POOL32A_MINOR_MASK) == |
| 476 | mm_pool32axf_op) { |
| 477 | switch (insn.mm_i_format.simmediate >> |
| 478 | MM_POOL32A_MINOR_SHIFT) { |
| 479 | case mm_jalr_op: |
| 480 | case mm_jalrhb_op: |
| 481 | case mm_jalrs_op: |
| 482 | case mm_jalrshb_op: |
| 483 | if (insn.mm_i_format.rt != 0) /* Not mm_jr */ |
| 484 | regs->regs[insn.mm_i_format.rt] = |
| 485 | regs->cp0_epc + |
| 486 | dec_insn.pc_inc + |
| 487 | dec_insn.next_pc_inc; |
| 488 | *contpc = regs->regs[insn.mm_i_format.rs]; |
| 489 | return 1; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | break; |
| 493 | case mm_pool32i_op: |
| 494 | switch (insn.mm_i_format.rt) { |
| 495 | case mm_bltzals_op: |
| 496 | case mm_bltzal_op: |
| 497 | regs->regs[31] = regs->cp0_epc + |
| 498 | dec_insn.pc_inc + |
| 499 | dec_insn.next_pc_inc; |
| 500 | /* Fall through */ |
| 501 | case mm_bltz_op: |
| 502 | if ((long)regs->regs[insn.mm_i_format.rs] < 0) |
| 503 | *contpc = regs->cp0_epc + |
| 504 | dec_insn.pc_inc + |
| 505 | (insn.mm_i_format.simmediate << 1); |
| 506 | else |
| 507 | *contpc = regs->cp0_epc + |
| 508 | dec_insn.pc_inc + |
| 509 | dec_insn.next_pc_inc; |
| 510 | return 1; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 511 | case mm_bgezals_op: |
| 512 | case mm_bgezal_op: |
| 513 | regs->regs[31] = regs->cp0_epc + |
| 514 | dec_insn.pc_inc + |
| 515 | dec_insn.next_pc_inc; |
| 516 | /* Fall through */ |
| 517 | case mm_bgez_op: |
| 518 | if ((long)regs->regs[insn.mm_i_format.rs] >= 0) |
| 519 | *contpc = regs->cp0_epc + |
| 520 | dec_insn.pc_inc + |
| 521 | (insn.mm_i_format.simmediate << 1); |
| 522 | else |
| 523 | *contpc = regs->cp0_epc + |
| 524 | dec_insn.pc_inc + |
| 525 | dec_insn.next_pc_inc; |
| 526 | return 1; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 527 | case mm_blez_op: |
| 528 | if ((long)regs->regs[insn.mm_i_format.rs] <= 0) |
| 529 | *contpc = regs->cp0_epc + |
| 530 | dec_insn.pc_inc + |
| 531 | (insn.mm_i_format.simmediate << 1); |
| 532 | else |
| 533 | *contpc = regs->cp0_epc + |
| 534 | dec_insn.pc_inc + |
| 535 | dec_insn.next_pc_inc; |
| 536 | return 1; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 537 | case mm_bgtz_op: |
| 538 | if ((long)regs->regs[insn.mm_i_format.rs] <= 0) |
| 539 | *contpc = regs->cp0_epc + |
| 540 | dec_insn.pc_inc + |
| 541 | (insn.mm_i_format.simmediate << 1); |
| 542 | else |
| 543 | *contpc = regs->cp0_epc + |
| 544 | dec_insn.pc_inc + |
| 545 | dec_insn.next_pc_inc; |
| 546 | return 1; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 547 | case mm_bc2f_op: |
| 548 | case mm_bc1f_op: |
| 549 | bc_false = 1; |
| 550 | /* Fall through */ |
| 551 | case mm_bc2t_op: |
| 552 | case mm_bc1t_op: |
| 553 | preempt_disable(); |
| 554 | if (is_fpu_owner()) |
| 555 | asm volatile("cfc1\t%0,$31" : "=r" (fcr31)); |
| 556 | else |
| 557 | fcr31 = current->thread.fpu.fcr31; |
| 558 | preempt_enable(); |
| 559 | |
| 560 | if (bc_false) |
| 561 | fcr31 = ~fcr31; |
| 562 | |
| 563 | bit = (insn.mm_i_format.rs >> 2); |
| 564 | bit += (bit != 0); |
| 565 | bit += 23; |
| 566 | if (fcr31 & (1 << bit)) |
| 567 | *contpc = regs->cp0_epc + |
| 568 | dec_insn.pc_inc + |
| 569 | (insn.mm_i_format.simmediate << 1); |
| 570 | else |
| 571 | *contpc = regs->cp0_epc + |
| 572 | dec_insn.pc_inc + dec_insn.next_pc_inc; |
| 573 | return 1; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 574 | } |
| 575 | break; |
| 576 | case mm_pool16c_op: |
| 577 | switch (insn.mm_i_format.rt) { |
| 578 | case mm_jalr16_op: |
| 579 | case mm_jalrs16_op: |
| 580 | regs->regs[31] = regs->cp0_epc + |
| 581 | dec_insn.pc_inc + dec_insn.next_pc_inc; |
| 582 | /* Fall through */ |
| 583 | case mm_jr16_op: |
| 584 | *contpc = regs->regs[insn.mm_i_format.rs]; |
| 585 | return 1; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 586 | } |
| 587 | break; |
| 588 | case mm_beqz16_op: |
| 589 | if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] == 0) |
| 590 | *contpc = regs->cp0_epc + |
| 591 | dec_insn.pc_inc + |
| 592 | (insn.mm_b1_format.simmediate << 1); |
| 593 | else |
| 594 | *contpc = regs->cp0_epc + |
| 595 | dec_insn.pc_inc + dec_insn.next_pc_inc; |
| 596 | return 1; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 597 | case mm_bnez16_op: |
| 598 | if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] != 0) |
| 599 | *contpc = regs->cp0_epc + |
| 600 | dec_insn.pc_inc + |
| 601 | (insn.mm_b1_format.simmediate << 1); |
| 602 | else |
| 603 | *contpc = regs->cp0_epc + |
| 604 | dec_insn.pc_inc + dec_insn.next_pc_inc; |
| 605 | return 1; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 606 | case mm_b16_op: |
| 607 | *contpc = regs->cp0_epc + dec_insn.pc_inc + |
| 608 | (insn.mm_b0_format.simmediate << 1); |
| 609 | return 1; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 610 | case mm_beq32_op: |
| 611 | if (regs->regs[insn.mm_i_format.rs] == |
| 612 | regs->regs[insn.mm_i_format.rt]) |
| 613 | *contpc = regs->cp0_epc + |
| 614 | dec_insn.pc_inc + |
| 615 | (insn.mm_i_format.simmediate << 1); |
| 616 | else |
| 617 | *contpc = regs->cp0_epc + |
| 618 | dec_insn.pc_inc + |
| 619 | dec_insn.next_pc_inc; |
| 620 | return 1; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 621 | case mm_bne32_op: |
| 622 | if (regs->regs[insn.mm_i_format.rs] != |
| 623 | regs->regs[insn.mm_i_format.rt]) |
| 624 | *contpc = regs->cp0_epc + |
| 625 | dec_insn.pc_inc + |
| 626 | (insn.mm_i_format.simmediate << 1); |
| 627 | else |
| 628 | *contpc = regs->cp0_epc + |
| 629 | dec_insn.pc_inc + dec_insn.next_pc_inc; |
| 630 | return 1; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 631 | case mm_jalx32_op: |
| 632 | regs->regs[31] = regs->cp0_epc + |
| 633 | dec_insn.pc_inc + dec_insn.next_pc_inc; |
| 634 | *contpc = regs->cp0_epc + dec_insn.pc_inc; |
| 635 | *contpc >>= 28; |
| 636 | *contpc <<= 28; |
| 637 | *contpc |= (insn.j_format.target << 2); |
| 638 | return 1; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 639 | case mm_jals32_op: |
| 640 | case mm_jal32_op: |
| 641 | regs->regs[31] = regs->cp0_epc + |
| 642 | dec_insn.pc_inc + dec_insn.next_pc_inc; |
| 643 | /* Fall through */ |
| 644 | case mm_j32_op: |
| 645 | *contpc = regs->cp0_epc + dec_insn.pc_inc; |
| 646 | *contpc >>= 27; |
| 647 | *contpc <<= 27; |
| 648 | *contpc |= (insn.j_format.target << 1); |
| 649 | set_isa16_mode(*contpc); |
| 650 | return 1; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 651 | } |
| 652 | return 0; |
| 653 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | |
| 655 | /* |
| 656 | * Redundant with logic already in kernel/branch.c, |
| 657 | * embedded in compute_return_epc. At some point, |
| 658 | * a single subroutine should be used across both |
| 659 | * modules. |
| 660 | */ |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 661 | static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn, |
| 662 | unsigned long *contpc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | { |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 664 | union mips_instruction insn = (union mips_instruction)dec_insn.insn; |
| 665 | unsigned int fcr31; |
| 666 | unsigned int bit = 0; |
| 667 | |
| 668 | switch (insn.i_format.opcode) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | case spec_op: |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 670 | switch (insn.r_format.func) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | case jalr_op: |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 672 | regs->regs[insn.r_format.rd] = |
| 673 | regs->cp0_epc + dec_insn.pc_inc + |
| 674 | dec_insn.next_pc_inc; |
| 675 | /* Fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | case jr_op: |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 677 | *contpc = regs->regs[insn.r_format.rs]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | return 1; |
| 679 | } |
| 680 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | case bcond_op: |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 682 | switch (insn.i_format.rt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | case bltzal_op: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | case bltzall_op: |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 685 | regs->regs[31] = regs->cp0_epc + |
| 686 | dec_insn.pc_inc + |
| 687 | dec_insn.next_pc_inc; |
| 688 | /* Fall through */ |
| 689 | case bltz_op: |
| 690 | case bltzl_op: |
| 691 | if ((long)regs->regs[insn.i_format.rs] < 0) |
| 692 | *contpc = regs->cp0_epc + |
| 693 | dec_insn.pc_inc + |
| 694 | (insn.i_format.simmediate << 2); |
| 695 | else |
| 696 | *contpc = regs->cp0_epc + |
| 697 | dec_insn.pc_inc + |
| 698 | dec_insn.next_pc_inc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | return 1; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 700 | case bgezal_op: |
| 701 | case bgezall_op: |
| 702 | regs->regs[31] = regs->cp0_epc + |
| 703 | dec_insn.pc_inc + |
| 704 | dec_insn.next_pc_inc; |
| 705 | /* Fall through */ |
| 706 | case bgez_op: |
| 707 | case bgezl_op: |
| 708 | if ((long)regs->regs[insn.i_format.rs] >= 0) |
| 709 | *contpc = regs->cp0_epc + |
| 710 | dec_insn.pc_inc + |
| 711 | (insn.i_format.simmediate << 2); |
| 712 | else |
| 713 | *contpc = regs->cp0_epc + |
| 714 | dec_insn.pc_inc + |
| 715 | dec_insn.next_pc_inc; |
| 716 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | } |
| 718 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | case jalx_op: |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 720 | set_isa16_mode(bit); |
| 721 | case jal_op: |
| 722 | regs->regs[31] = regs->cp0_epc + |
| 723 | dec_insn.pc_inc + |
| 724 | dec_insn.next_pc_inc; |
| 725 | /* Fall through */ |
| 726 | case j_op: |
| 727 | *contpc = regs->cp0_epc + dec_insn.pc_inc; |
| 728 | *contpc >>= 28; |
| 729 | *contpc <<= 28; |
| 730 | *contpc |= (insn.j_format.target << 2); |
| 731 | /* Set microMIPS mode bit: XOR for jalx. */ |
| 732 | *contpc ^= bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | return 1; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 734 | case beq_op: |
| 735 | case beql_op: |
| 736 | if (regs->regs[insn.i_format.rs] == |
| 737 | regs->regs[insn.i_format.rt]) |
| 738 | *contpc = regs->cp0_epc + |
| 739 | dec_insn.pc_inc + |
| 740 | (insn.i_format.simmediate << 2); |
| 741 | else |
| 742 | *contpc = regs->cp0_epc + |
| 743 | dec_insn.pc_inc + |
| 744 | dec_insn.next_pc_inc; |
| 745 | return 1; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 746 | case bne_op: |
| 747 | case bnel_op: |
| 748 | if (regs->regs[insn.i_format.rs] != |
| 749 | regs->regs[insn.i_format.rt]) |
| 750 | *contpc = regs->cp0_epc + |
| 751 | dec_insn.pc_inc + |
| 752 | (insn.i_format.simmediate << 2); |
| 753 | else |
| 754 | *contpc = regs->cp0_epc + |
| 755 | dec_insn.pc_inc + |
| 756 | dec_insn.next_pc_inc; |
| 757 | return 1; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 758 | case blez_op: |
| 759 | case blezl_op: |
| 760 | if ((long)regs->regs[insn.i_format.rs] <= 0) |
| 761 | *contpc = regs->cp0_epc + |
| 762 | dec_insn.pc_inc + |
| 763 | (insn.i_format.simmediate << 2); |
| 764 | else |
| 765 | *contpc = regs->cp0_epc + |
| 766 | dec_insn.pc_inc + |
| 767 | dec_insn.next_pc_inc; |
| 768 | return 1; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 769 | case bgtz_op: |
| 770 | case bgtzl_op: |
| 771 | if ((long)regs->regs[insn.i_format.rs] > 0) |
| 772 | *contpc = regs->cp0_epc + |
| 773 | dec_insn.pc_inc + |
| 774 | (insn.i_format.simmediate << 2); |
| 775 | else |
| 776 | *contpc = regs->cp0_epc + |
| 777 | dec_insn.pc_inc + |
| 778 | dec_insn.next_pc_inc; |
| 779 | return 1; |
David Daney | c26d421 | 2013-08-19 12:10:34 -0700 | [diff] [blame] | 780 | #ifdef CONFIG_CPU_CAVIUM_OCTEON |
| 781 | case lwc2_op: /* This is bbit0 on Octeon */ |
| 782 | if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0) |
| 783 | *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2); |
| 784 | else |
| 785 | *contpc = regs->cp0_epc + 8; |
| 786 | return 1; |
| 787 | case ldc2_op: /* This is bbit032 on Octeon */ |
| 788 | if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0) |
| 789 | *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2); |
| 790 | else |
| 791 | *contpc = regs->cp0_epc + 8; |
| 792 | return 1; |
| 793 | case swc2_op: /* This is bbit1 on Octeon */ |
| 794 | if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) |
| 795 | *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2); |
| 796 | else |
| 797 | *contpc = regs->cp0_epc + 8; |
| 798 | return 1; |
| 799 | case sdc2_op: /* This is bbit132 on Octeon */ |
| 800 | if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) |
| 801 | *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2); |
| 802 | else |
| 803 | *contpc = regs->cp0_epc + 8; |
| 804 | return 1; |
| 805 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | case cop0_op: |
| 807 | case cop1_op: |
| 808 | case cop2_op: |
| 809 | case cop1x_op: |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 810 | if (insn.i_format.rs == bc_op) { |
| 811 | preempt_disable(); |
| 812 | if (is_fpu_owner()) |
| 813 | asm volatile("cfc1\t%0,$31" : "=r" (fcr31)); |
| 814 | else |
| 815 | fcr31 = current->thread.fpu.fcr31; |
| 816 | preempt_enable(); |
| 817 | |
| 818 | bit = (insn.i_format.rt >> 2); |
| 819 | bit += (bit != 0); |
| 820 | bit += 23; |
| 821 | switch (insn.i_format.rt & 3) { |
| 822 | case 0: /* bc1f */ |
| 823 | case 2: /* bc1fl */ |
| 824 | if (~fcr31 & (1 << bit)) |
| 825 | *contpc = regs->cp0_epc + |
| 826 | dec_insn.pc_inc + |
| 827 | (insn.i_format.simmediate << 2); |
| 828 | else |
| 829 | *contpc = regs->cp0_epc + |
| 830 | dec_insn.pc_inc + |
| 831 | dec_insn.next_pc_inc; |
| 832 | return 1; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 833 | case 1: /* bc1t */ |
| 834 | case 3: /* bc1tl */ |
| 835 | if (fcr31 & (1 << bit)) |
| 836 | *contpc = regs->cp0_epc + |
| 837 | dec_insn.pc_inc + |
| 838 | (insn.i_format.simmediate << 2); |
| 839 | else |
| 840 | *contpc = regs->cp0_epc + |
| 841 | dec_insn.pc_inc + |
| 842 | dec_insn.next_pc_inc; |
| 843 | return 1; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 844 | } |
| 845 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | break; |
| 847 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | /* |
| 852 | * In the Linux kernel, we support selection of FPR format on the |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 853 | * basis of the Status.FR bit. If an FPU is not present, the FR bit |
David Daney | da0bac3 | 2009-11-02 11:33:46 -0800 | [diff] [blame] | 854 | * is hardwired to zero, which would imply a 32-bit FPU even for |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 855 | * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS. |
Ralf Baechle | 51d943f | 2012-08-15 19:42:19 +0200 | [diff] [blame] | 856 | * FPU emu is slow and bulky and optimizing this function offers fairly |
| 857 | * sizeable benefits so we try to be clever and make this function return |
| 858 | * a constant whenever possible, that is on 64-bit kernels without O32 |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 859 | * compatibility enabled and on 32-bit without 64-bit FPU support. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | */ |
David Daney | da0bac3 | 2009-11-02 11:33:46 -0800 | [diff] [blame] | 861 | static inline int cop1_64bit(struct pt_regs *xcp) |
| 862 | { |
Ralf Baechle | 51d943f | 2012-08-15 19:42:19 +0200 | [diff] [blame] | 863 | #if defined(CONFIG_64BIT) && !defined(CONFIG_MIPS32_O32) |
| 864 | return 1; |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 865 | #elif defined(CONFIG_32BIT) && !defined(CONFIG_MIPS_O32_FP64_SUPPORT) |
David Daney | da0bac3 | 2009-11-02 11:33:46 -0800 | [diff] [blame] | 866 | return 0; |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 867 | #else |
| 868 | return !test_thread_flag(TIF_32BIT_FPREGS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | #endif |
David Daney | da0bac3 | 2009-11-02 11:33:46 -0800 | [diff] [blame] | 870 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | |
Ralf Baechle | 47fa0c0 | 2014-04-16 11:00:12 +0200 | [diff] [blame^] | 872 | #define SIFROMREG(si, x) \ |
| 873 | do { \ |
Paul Burton | bbd426f | 2014-02-13 11:26:41 +0000 | [diff] [blame] | 874 | if (cop1_64bit(xcp)) \ |
| 875 | (si) = get_fpr32(&ctx->fpr[x], 0); \ |
| 876 | else \ |
| 877 | (si) = get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \ |
| 878 | } while (0) |
David Daney | da0bac3 | 2009-11-02 11:33:46 -0800 | [diff] [blame] | 879 | |
Ralf Baechle | 47fa0c0 | 2014-04-16 11:00:12 +0200 | [diff] [blame^] | 880 | #define SITOREG(si, x) \ |
| 881 | do { \ |
Paul Burton | ef1c47a | 2014-01-27 17:14:47 +0000 | [diff] [blame] | 882 | if (cop1_64bit(xcp)) { \ |
| 883 | unsigned i; \ |
Paul Burton | bbd426f | 2014-02-13 11:26:41 +0000 | [diff] [blame] | 884 | set_fpr32(&ctx->fpr[x], 0, si); \ |
Paul Burton | ef1c47a | 2014-01-27 17:14:47 +0000 | [diff] [blame] | 885 | for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \ |
| 886 | set_fpr32(&ctx->fpr[x], i, 0); \ |
| 887 | } else { \ |
Paul Burton | bbd426f | 2014-02-13 11:26:41 +0000 | [diff] [blame] | 888 | set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \ |
Paul Burton | ef1c47a | 2014-01-27 17:14:47 +0000 | [diff] [blame] | 889 | } \ |
Paul Burton | bbd426f | 2014-02-13 11:26:41 +0000 | [diff] [blame] | 890 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | |
Paul Burton | bbd426f | 2014-02-13 11:26:41 +0000 | [diff] [blame] | 892 | #define SIFROMHREG(si, x) ((si) = get_fpr32(&ctx->fpr[x], 1)) |
Paul Burton | ef1c47a | 2014-01-27 17:14:47 +0000 | [diff] [blame] | 893 | |
Ralf Baechle | 47fa0c0 | 2014-04-16 11:00:12 +0200 | [diff] [blame^] | 894 | #define SITOHREG(si, x) \ |
| 895 | do { \ |
Paul Burton | ef1c47a | 2014-01-27 17:14:47 +0000 | [diff] [blame] | 896 | unsigned i; \ |
| 897 | set_fpr32(&ctx->fpr[x], 1, si); \ |
| 898 | for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \ |
| 899 | set_fpr32(&ctx->fpr[x], i, 0); \ |
| 900 | } while (0) |
Leonid Yegoshin | 1ac94400 | 2013-11-07 12:48:28 +0000 | [diff] [blame] | 901 | |
Ralf Baechle | 47fa0c0 | 2014-04-16 11:00:12 +0200 | [diff] [blame^] | 902 | #define DIFROMREG(di, x) \ |
Paul Burton | bbd426f | 2014-02-13 11:26:41 +0000 | [diff] [blame] | 903 | ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0)) |
| 904 | |
Ralf Baechle | 47fa0c0 | 2014-04-16 11:00:12 +0200 | [diff] [blame^] | 905 | #define DITOREG(di, x) \ |
| 906 | do { \ |
Paul Burton | ef1c47a | 2014-01-27 17:14:47 +0000 | [diff] [blame] | 907 | unsigned fpr, i; \ |
| 908 | fpr = (x) & ~(cop1_64bit(xcp) == 0); \ |
| 909 | set_fpr64(&ctx->fpr[fpr], 0, di); \ |
| 910 | for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \ |
| 911 | set_fpr64(&ctx->fpr[fpr], i, 0); \ |
| 912 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 914 | #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x) |
| 915 | #define SPTOREG(sp, x) SITOREG((sp).bits, x) |
| 916 | #define DPFROMREG(dp, x) DIFROMREG((dp).bits, x) |
| 917 | #define DPTOREG(dp, x) DITOREG((dp).bits, x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | |
| 919 | /* |
| 920 | * Emulate the single floating point instruction pointed at by EPC. |
| 921 | * Two instructions if the instruction is in a branch delay slot. |
| 922 | */ |
| 923 | |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 924 | static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx, |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 925 | struct mm_decoded_insn dec_insn, void *__user *fault_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | { |
| 927 | mips_instruction ir; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 928 | unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | unsigned int cond; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 930 | int pc_inc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | |
| 932 | /* XXX NEC Vr54xx bug workaround */ |
Ralf Baechle | e7e9cae | 2014-04-16 01:59:03 +0200 | [diff] [blame] | 933 | if (delay_slot(xcp)) { |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 934 | if (dec_insn.micro_mips_mode) { |
| 935 | if (!mm_isBranchInstr(xcp, dec_insn, &contpc)) |
Ralf Baechle | e7e9cae | 2014-04-16 01:59:03 +0200 | [diff] [blame] | 936 | clear_delay_slot(xcp); |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 937 | } else { |
| 938 | if (!isBranchInstr(xcp, dec_insn, &contpc)) |
Ralf Baechle | e7e9cae | 2014-04-16 01:59:03 +0200 | [diff] [blame] | 939 | clear_delay_slot(xcp); |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 940 | } |
| 941 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | |
Ralf Baechle | e7e9cae | 2014-04-16 01:59:03 +0200 | [diff] [blame] | 943 | if (delay_slot(xcp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | /* |
| 945 | * The instruction to be emulated is in a branch delay slot |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 946 | * which means that we have to emulate the branch instruction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | * BEFORE we do the cop1 instruction. |
| 948 | * |
| 949 | * This branch could be a COP1 branch, but in that case we |
| 950 | * would have had a trap for that instruction, and would not |
| 951 | * come through this route. |
| 952 | * |
| 953 | * Linux MIPS branch emulator operates on context, updating the |
| 954 | * cp0_epc. |
| 955 | */ |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 956 | ir = dec_insn.next_insn; /* process delay slot instr */ |
| 957 | pc_inc = dec_insn.next_pc_inc; |
Ralf Baechle | 333d1f6 | 2005-02-28 17:55:57 +0000 | [diff] [blame] | 958 | } else { |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 959 | ir = dec_insn.insn; /* process current instr */ |
| 960 | pc_inc = dec_insn.pc_inc; |
| 961 | } |
| 962 | |
| 963 | /* |
| 964 | * Since microMIPS FPU instructios are a subset of MIPS32 FPU |
| 965 | * instructions, we want to convert microMIPS FPU instructions |
| 966 | * into MIPS32 instructions so that we could reuse all of the |
| 967 | * FPU emulation code. |
| 968 | * |
| 969 | * NOTE: We cannot do this for branch instructions since they |
| 970 | * are not a subset. Example: Cannot emulate a 16-bit |
| 971 | * aligned target address with a MIPS32 instruction. |
| 972 | */ |
| 973 | if (dec_insn.micro_mips_mode) { |
| 974 | /* |
| 975 | * If next instruction is a 16-bit instruction, then it |
| 976 | * it cannot be a FPU instruction. This could happen |
| 977 | * since we can be called for non-FPU instructions. |
| 978 | */ |
| 979 | if ((pc_inc == 2) || |
| 980 | (microMIPS32_to_MIPS32((union mips_instruction *)&ir) |
| 981 | == SIGILL)) |
| 982 | return SIGILL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | emul: |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 986 | perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0); |
David Daney | b6ee75e | 2009-11-05 11:34:26 -0800 | [diff] [blame] | 987 | MIPS_FPU_EMU_INC_STATS(emulated); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | switch (MIPSInst_OPCODE(ir)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | case ldc1_op:{ |
Ralf Baechle | 3fccc01 | 2005-10-23 13:58:21 +0100 | [diff] [blame] | 990 | u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | MIPSInst_SIMM(ir)); |
| 992 | u64 val; |
| 993 | |
David Daney | b6ee75e | 2009-11-05 11:34:26 -0800 | [diff] [blame] | 994 | MIPS_FPU_EMU_INC_STATS(loads); |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 995 | |
| 996 | if (!access_ok(VERIFY_READ, va, sizeof(u64))) { |
David Daney | b6ee75e | 2009-11-05 11:34:26 -0800 | [diff] [blame] | 997 | MIPS_FPU_EMU_INC_STATS(errors); |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 998 | *fault_addr = va; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | return SIGBUS; |
| 1000 | } |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1001 | if (__get_user(val, va)) { |
| 1002 | MIPS_FPU_EMU_INC_STATS(errors); |
| 1003 | *fault_addr = va; |
| 1004 | return SIGSEGV; |
| 1005 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | DITOREG(val, MIPSInst_RT(ir)); |
| 1007 | break; |
| 1008 | } |
| 1009 | |
| 1010 | case sdc1_op:{ |
Ralf Baechle | 3fccc01 | 2005-10-23 13:58:21 +0100 | [diff] [blame] | 1011 | u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | MIPSInst_SIMM(ir)); |
| 1013 | u64 val; |
| 1014 | |
David Daney | b6ee75e | 2009-11-05 11:34:26 -0800 | [diff] [blame] | 1015 | MIPS_FPU_EMU_INC_STATS(stores); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | DIFROMREG(val, MIPSInst_RT(ir)); |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1017 | if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) { |
David Daney | b6ee75e | 2009-11-05 11:34:26 -0800 | [diff] [blame] | 1018 | MIPS_FPU_EMU_INC_STATS(errors); |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1019 | *fault_addr = va; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | return SIGBUS; |
| 1021 | } |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1022 | if (__put_user(val, va)) { |
| 1023 | MIPS_FPU_EMU_INC_STATS(errors); |
| 1024 | *fault_addr = va; |
| 1025 | return SIGSEGV; |
| 1026 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | break; |
| 1028 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | |
| 1030 | case lwc1_op:{ |
Ralf Baechle | 3fccc01 | 2005-10-23 13:58:21 +0100 | [diff] [blame] | 1031 | u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | MIPSInst_SIMM(ir)); |
| 1033 | u32 val; |
| 1034 | |
David Daney | b6ee75e | 2009-11-05 11:34:26 -0800 | [diff] [blame] | 1035 | MIPS_FPU_EMU_INC_STATS(loads); |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1036 | if (!access_ok(VERIFY_READ, va, sizeof(u32))) { |
David Daney | b6ee75e | 2009-11-05 11:34:26 -0800 | [diff] [blame] | 1037 | MIPS_FPU_EMU_INC_STATS(errors); |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1038 | *fault_addr = va; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | return SIGBUS; |
| 1040 | } |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1041 | if (__get_user(val, va)) { |
| 1042 | MIPS_FPU_EMU_INC_STATS(errors); |
| 1043 | *fault_addr = va; |
| 1044 | return SIGSEGV; |
| 1045 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | SITOREG(val, MIPSInst_RT(ir)); |
| 1047 | break; |
| 1048 | } |
| 1049 | |
| 1050 | case swc1_op:{ |
Ralf Baechle | 3fccc01 | 2005-10-23 13:58:21 +0100 | [diff] [blame] | 1051 | u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | MIPSInst_SIMM(ir)); |
| 1053 | u32 val; |
| 1054 | |
David Daney | b6ee75e | 2009-11-05 11:34:26 -0800 | [diff] [blame] | 1055 | MIPS_FPU_EMU_INC_STATS(stores); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | SIFROMREG(val, MIPSInst_RT(ir)); |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1057 | if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) { |
David Daney | b6ee75e | 2009-11-05 11:34:26 -0800 | [diff] [blame] | 1058 | MIPS_FPU_EMU_INC_STATS(errors); |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1059 | *fault_addr = va; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | return SIGBUS; |
| 1061 | } |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1062 | if (__put_user(val, va)) { |
| 1063 | MIPS_FPU_EMU_INC_STATS(errors); |
| 1064 | *fault_addr = va; |
| 1065 | return SIGSEGV; |
| 1066 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | break; |
| 1068 | } |
| 1069 | |
| 1070 | case cop1_op: |
| 1071 | switch (MIPSInst_RS(ir)) { |
| 1072 | |
Ralf Baechle | 4b724ef | 2005-10-23 15:05:47 +0100 | [diff] [blame] | 1073 | #if defined(__mips64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | case dmfc_op: |
| 1075 | /* copregister fs -> gpr[rt] */ |
| 1076 | if (MIPSInst_RT(ir) != 0) { |
| 1077 | DIFROMREG(xcp->regs[MIPSInst_RT(ir)], |
| 1078 | MIPSInst_RD(ir)); |
| 1079 | } |
| 1080 | break; |
| 1081 | |
| 1082 | case dmtc_op: |
| 1083 | /* copregister fs <- rt */ |
| 1084 | DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir)); |
| 1085 | break; |
| 1086 | #endif |
| 1087 | |
Leonid Yegoshin | 1ac94400 | 2013-11-07 12:48:28 +0000 | [diff] [blame] | 1088 | case mfhc_op: |
| 1089 | if (!cpu_has_mips_r2) |
| 1090 | goto sigill; |
| 1091 | |
| 1092 | /* copregister rd -> gpr[rt] */ |
| 1093 | if (MIPSInst_RT(ir) != 0) { |
| 1094 | SIFROMHREG(xcp->regs[MIPSInst_RT(ir)], |
| 1095 | MIPSInst_RD(ir)); |
| 1096 | } |
| 1097 | break; |
| 1098 | |
| 1099 | case mthc_op: |
| 1100 | if (!cpu_has_mips_r2) |
| 1101 | goto sigill; |
| 1102 | |
| 1103 | /* copregister rd <- gpr[rt] */ |
| 1104 | SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir)); |
| 1105 | break; |
| 1106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | case mfc_op: |
| 1108 | /* copregister rd -> gpr[rt] */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | if (MIPSInst_RT(ir) != 0) { |
| 1110 | SIFROMREG(xcp->regs[MIPSInst_RT(ir)], |
| 1111 | MIPSInst_RD(ir)); |
| 1112 | } |
| 1113 | break; |
| 1114 | |
| 1115 | case mtc_op: |
| 1116 | /* copregister rd <- rt */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir)); |
| 1118 | break; |
| 1119 | |
| 1120 | case cfc_op:{ |
| 1121 | /* cop control register rd -> gpr[rt] */ |
| 1122 | u32 value; |
| 1123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | if (MIPSInst_RD(ir) == FPCREG_CSR) { |
| 1125 | value = ctx->fcr31; |
Shane McDonald | 3f13553 | 2010-05-07 00:02:09 -0600 | [diff] [blame] | 1126 | value = (value & ~FPU_CSR_RM) | |
| 1127 | mips_rm[modeindex(value)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | #ifdef CSRTRACE |
| 1129 | printk("%p gpr[%d]<-csr=%08x\n", |
Ralf Baechle | 333d1f6 | 2005-02-28 17:55:57 +0000 | [diff] [blame] | 1130 | (void *) (xcp->cp0_epc), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | MIPSInst_RT(ir), value); |
| 1132 | #endif |
| 1133 | } |
| 1134 | else if (MIPSInst_RD(ir) == FPCREG_RID) |
| 1135 | value = 0; |
| 1136 | else |
| 1137 | value = 0; |
| 1138 | if (MIPSInst_RT(ir)) |
| 1139 | xcp->regs[MIPSInst_RT(ir)] = value; |
| 1140 | break; |
| 1141 | } |
| 1142 | |
| 1143 | case ctc_op:{ |
| 1144 | /* copregister rd <- rt */ |
| 1145 | u32 value; |
| 1146 | |
| 1147 | if (MIPSInst_RT(ir) == 0) |
| 1148 | value = 0; |
| 1149 | else |
| 1150 | value = xcp->regs[MIPSInst_RT(ir)]; |
| 1151 | |
| 1152 | /* we only have one writable control reg |
| 1153 | */ |
| 1154 | if (MIPSInst_RD(ir) == FPCREG_CSR) { |
| 1155 | #ifdef CSRTRACE |
| 1156 | printk("%p gpr[%d]->csr=%08x\n", |
Ralf Baechle | 333d1f6 | 2005-02-28 17:55:57 +0000 | [diff] [blame] | 1157 | (void *) (xcp->cp0_epc), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | MIPSInst_RT(ir), value); |
| 1159 | #endif |
Shane McDonald | 95e8f63 | 2010-05-06 23:26:57 -0600 | [diff] [blame] | 1160 | |
| 1161 | /* |
| 1162 | * Don't write reserved bits, |
| 1163 | * and convert to ieee library modes |
| 1164 | */ |
| 1165 | ctx->fcr31 = (value & |
| 1166 | ~(FPU_CSR_RSVD | FPU_CSR_RM)) | |
| 1167 | ieee_rm[modeindex(value)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | } |
| 1169 | if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) { |
| 1170 | return SIGFPE; |
| 1171 | } |
| 1172 | break; |
| 1173 | } |
| 1174 | |
| 1175 | case bc_op:{ |
| 1176 | int likely = 0; |
| 1177 | |
Ralf Baechle | e7e9cae | 2014-04-16 01:59:03 +0200 | [diff] [blame] | 1178 | if (delay_slot(xcp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | return SIGILL; |
| 1180 | |
| 1181 | #if __mips >= 4 |
| 1182 | cond = ctx->fcr31 & fpucondbit[MIPSInst_RT(ir) >> 2]; |
| 1183 | #else |
| 1184 | cond = ctx->fcr31 & FPU_CSR_COND; |
| 1185 | #endif |
| 1186 | switch (MIPSInst_RT(ir) & 3) { |
| 1187 | case bcfl_op: |
| 1188 | likely = 1; |
| 1189 | case bcf_op: |
| 1190 | cond = !cond; |
| 1191 | break; |
| 1192 | case bctl_op: |
| 1193 | likely = 1; |
| 1194 | case bct_op: |
| 1195 | break; |
| 1196 | default: |
| 1197 | /* thats an illegal instruction */ |
| 1198 | return SIGILL; |
| 1199 | } |
| 1200 | |
Ralf Baechle | e7e9cae | 2014-04-16 01:59:03 +0200 | [diff] [blame] | 1201 | set_delay_slot(xcp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | if (cond) { |
| 1203 | /* branch taken: emulate dslot |
| 1204 | * instruction |
| 1205 | */ |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 1206 | xcp->cp0_epc += dec_insn.pc_inc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 1208 | contpc = MIPSInst_SIMM(ir); |
| 1209 | ir = dec_insn.next_insn; |
| 1210 | if (dec_insn.micro_mips_mode) { |
| 1211 | contpc = (xcp->cp0_epc + (contpc << 1)); |
| 1212 | |
| 1213 | /* If 16-bit instruction, not FPU. */ |
| 1214 | if ((dec_insn.next_pc_inc == 2) || |
| 1215 | (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) { |
| 1216 | |
| 1217 | /* |
| 1218 | * Since this instruction will |
| 1219 | * be put on the stack with |
| 1220 | * 32-bit words, get around |
| 1221 | * this problem by putting a |
| 1222 | * NOP16 as the second one. |
| 1223 | */ |
| 1224 | if (dec_insn.next_pc_inc == 2) |
| 1225 | ir = (ir & (~0xffff)) | MM_NOP16; |
| 1226 | |
| 1227 | /* |
| 1228 | * Single step the non-CP1 |
| 1229 | * instruction in the dslot. |
| 1230 | */ |
| 1231 | return mips_dsemul(xcp, ir, contpc); |
| 1232 | } |
| 1233 | } else |
| 1234 | contpc = (xcp->cp0_epc + (contpc << 2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | |
| 1236 | switch (MIPSInst_OPCODE(ir)) { |
| 1237 | case lwc1_op: |
| 1238 | case swc1_op: |
Ralf Baechle | 4b724ef | 2005-10-23 15:05:47 +0100 | [diff] [blame] | 1239 | #if (__mips >= 2 || defined(__mips64)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | case ldc1_op: |
| 1241 | case sdc1_op: |
| 1242 | #endif |
| 1243 | case cop1_op: |
| 1244 | #if __mips >= 4 && __mips != 32 |
| 1245 | case cop1x_op: |
| 1246 | #endif |
| 1247 | /* its one of ours */ |
| 1248 | goto emul; |
| 1249 | #if __mips >= 4 |
| 1250 | case spec_op: |
| 1251 | if (MIPSInst_FUNC(ir) == movc_op) |
| 1252 | goto emul; |
| 1253 | break; |
| 1254 | #endif |
| 1255 | } |
| 1256 | |
| 1257 | /* |
| 1258 | * Single step the non-cp1 |
| 1259 | * instruction in the dslot |
| 1260 | */ |
Atsushi Nemoto | e70dfc1 | 2007-07-13 23:02:29 +0900 | [diff] [blame] | 1261 | return mips_dsemul(xcp, ir, contpc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | } |
| 1263 | else { |
| 1264 | /* branch not taken */ |
| 1265 | if (likely) { |
| 1266 | /* |
| 1267 | * branch likely nullifies |
| 1268 | * dslot if not taken |
| 1269 | */ |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 1270 | xcp->cp0_epc += dec_insn.pc_inc; |
| 1271 | contpc += dec_insn.pc_inc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | /* |
| 1273 | * else continue & execute |
| 1274 | * dslot as normal insn |
| 1275 | */ |
| 1276 | } |
| 1277 | } |
| 1278 | break; |
| 1279 | } |
| 1280 | |
| 1281 | default: |
| 1282 | if (!(MIPSInst_RS(ir) & 0x10)) |
| 1283 | return SIGILL; |
| 1284 | { |
| 1285 | int sig; |
| 1286 | |
| 1287 | /* a real fpu computation instruction */ |
| 1288 | if ((sig = fpu_emu(xcp, ctx, ir))) |
| 1289 | return sig; |
| 1290 | } |
| 1291 | } |
| 1292 | break; |
| 1293 | |
| 1294 | #if __mips >= 4 && __mips != 32 |
| 1295 | case cop1x_op:{ |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1296 | int sig = fpux_emu(xcp, ctx, ir, fault_addr); |
| 1297 | if (sig) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | return sig; |
| 1299 | break; |
| 1300 | } |
| 1301 | #endif |
| 1302 | |
| 1303 | #if __mips >= 4 |
| 1304 | case spec_op: |
| 1305 | if (MIPSInst_FUNC(ir) != movc_op) |
| 1306 | return SIGILL; |
| 1307 | cond = fpucondbit[MIPSInst_RT(ir) >> 2]; |
| 1308 | if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0)) |
| 1309 | xcp->regs[MIPSInst_RD(ir)] = |
| 1310 | xcp->regs[MIPSInst_RS(ir)]; |
| 1311 | break; |
| 1312 | #endif |
| 1313 | |
| 1314 | default: |
Leonid Yegoshin | 1ac94400 | 2013-11-07 12:48:28 +0000 | [diff] [blame] | 1315 | sigill: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | return SIGILL; |
| 1317 | } |
| 1318 | |
| 1319 | /* we did it !! */ |
Atsushi Nemoto | e70dfc1 | 2007-07-13 23:02:29 +0900 | [diff] [blame] | 1320 | xcp->cp0_epc = contpc; |
Ralf Baechle | e7e9cae | 2014-04-16 01:59:03 +0200 | [diff] [blame] | 1321 | clear_delay_slot(xcp); |
Ralf Baechle | 333d1f6 | 2005-02-28 17:55:57 +0000 | [diff] [blame] | 1322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | return 0; |
| 1324 | } |
| 1325 | |
| 1326 | /* |
| 1327 | * Conversion table from MIPS compare ops 48-63 |
| 1328 | * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig); |
| 1329 | */ |
| 1330 | static const unsigned char cmptab[8] = { |
| 1331 | 0, /* cmp_0 (sig) cmp_sf */ |
| 1332 | IEEE754_CUN, /* cmp_un (sig) cmp_ngle */ |
| 1333 | IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */ |
| 1334 | IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */ |
| 1335 | IEEE754_CLT, /* cmp_olt (sig) cmp_lt */ |
| 1336 | IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */ |
| 1337 | IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */ |
| 1338 | IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */ |
| 1339 | }; |
| 1340 | |
| 1341 | |
| 1342 | #if __mips >= 4 && __mips != 32 |
| 1343 | |
| 1344 | /* |
| 1345 | * Additional MIPS4 instructions |
| 1346 | */ |
| 1347 | |
Ralf Baechle | 47fa0c0 | 2014-04-16 11:00:12 +0200 | [diff] [blame^] | 1348 | #define DEF3OP(name, p, f1, f2, f3) \ |
| 1349 | static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \ |
| 1350 | union ieee754##p s, union ieee754##p t) \ |
| 1351 | { \ |
| 1352 | struct _ieee754_csr ieee754_csr_save; \ |
| 1353 | s = f1(s, t); \ |
| 1354 | ieee754_csr_save = ieee754_csr; \ |
| 1355 | s = f2(s, r); \ |
| 1356 | ieee754_csr_save.cx |= ieee754_csr.cx; \ |
| 1357 | ieee754_csr_save.sx |= ieee754_csr.sx; \ |
| 1358 | s = f3(s); \ |
| 1359 | ieee754_csr.cx |= ieee754_csr_save.cx; \ |
| 1360 | ieee754_csr.sx |= ieee754_csr_save.sx; \ |
| 1361 | return s; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | } |
| 1363 | |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1364 | static union ieee754dp fpemu_dp_recip(union ieee754dp d) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | { |
| 1366 | return ieee754dp_div(ieee754dp_one(0), d); |
| 1367 | } |
| 1368 | |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1369 | static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | { |
| 1371 | return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d)); |
| 1372 | } |
| 1373 | |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1374 | static union ieee754sp fpemu_sp_recip(union ieee754sp s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | { |
| 1376 | return ieee754sp_div(ieee754sp_one(0), s); |
| 1377 | } |
| 1378 | |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1379 | static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | { |
| 1381 | return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s)); |
| 1382 | } |
| 1383 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 1384 | DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, ); |
| 1385 | DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg); |
| 1387 | DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg); |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 1388 | DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, ); |
| 1389 | DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg); |
| 1391 | DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg); |
| 1392 | |
Atsushi Nemoto | eae8907 | 2006-05-16 01:26:03 +0900 | [diff] [blame] | 1393 | static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx, |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1394 | mips_instruction ir, void *__user *fault_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | { |
| 1396 | unsigned rcsr = 0; /* resulting csr */ |
| 1397 | |
David Daney | b6ee75e | 2009-11-05 11:34:26 -0800 | [diff] [blame] | 1398 | MIPS_FPU_EMU_INC_STATS(cp1xops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | |
| 1400 | switch (MIPSInst_FMA_FFMT(ir)) { |
| 1401 | case s_fmt:{ /* 0 */ |
| 1402 | |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1403 | union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp); |
| 1404 | union ieee754sp fd, fr, fs, ft; |
Ralf Baechle | 3fccc01 | 2005-10-23 13:58:21 +0100 | [diff] [blame] | 1405 | u32 __user *va; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | u32 val; |
| 1407 | |
| 1408 | switch (MIPSInst_FUNC(ir)) { |
| 1409 | case lwxc1_op: |
Ralf Baechle | 3fccc01 | 2005-10-23 13:58:21 +0100 | [diff] [blame] | 1410 | va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | xcp->regs[MIPSInst_FT(ir)]); |
| 1412 | |
David Daney | b6ee75e | 2009-11-05 11:34:26 -0800 | [diff] [blame] | 1413 | MIPS_FPU_EMU_INC_STATS(loads); |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1414 | if (!access_ok(VERIFY_READ, va, sizeof(u32))) { |
David Daney | b6ee75e | 2009-11-05 11:34:26 -0800 | [diff] [blame] | 1415 | MIPS_FPU_EMU_INC_STATS(errors); |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1416 | *fault_addr = va; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | return SIGBUS; |
| 1418 | } |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1419 | if (__get_user(val, va)) { |
| 1420 | MIPS_FPU_EMU_INC_STATS(errors); |
| 1421 | *fault_addr = va; |
| 1422 | return SIGSEGV; |
| 1423 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | SITOREG(val, MIPSInst_FD(ir)); |
| 1425 | break; |
| 1426 | |
| 1427 | case swxc1_op: |
Ralf Baechle | 3fccc01 | 2005-10-23 13:58:21 +0100 | [diff] [blame] | 1428 | va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | xcp->regs[MIPSInst_FT(ir)]); |
| 1430 | |
David Daney | b6ee75e | 2009-11-05 11:34:26 -0800 | [diff] [blame] | 1431 | MIPS_FPU_EMU_INC_STATS(stores); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | |
| 1433 | SIFROMREG(val, MIPSInst_FS(ir)); |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1434 | if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) { |
| 1435 | MIPS_FPU_EMU_INC_STATS(errors); |
| 1436 | *fault_addr = va; |
| 1437 | return SIGBUS; |
| 1438 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | if (put_user(val, va)) { |
David Daney | b6ee75e | 2009-11-05 11:34:26 -0800 | [diff] [blame] | 1440 | MIPS_FPU_EMU_INC_STATS(errors); |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1441 | *fault_addr = va; |
| 1442 | return SIGSEGV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | } |
| 1444 | break; |
| 1445 | |
| 1446 | case madd_s_op: |
| 1447 | handler = fpemu_sp_madd; |
| 1448 | goto scoptop; |
| 1449 | case msub_s_op: |
| 1450 | handler = fpemu_sp_msub; |
| 1451 | goto scoptop; |
| 1452 | case nmadd_s_op: |
| 1453 | handler = fpemu_sp_nmadd; |
| 1454 | goto scoptop; |
| 1455 | case nmsub_s_op: |
| 1456 | handler = fpemu_sp_nmsub; |
| 1457 | goto scoptop; |
| 1458 | |
| 1459 | scoptop: |
| 1460 | SPFROMREG(fr, MIPSInst_FR(ir)); |
| 1461 | SPFROMREG(fs, MIPSInst_FS(ir)); |
| 1462 | SPFROMREG(ft, MIPSInst_FT(ir)); |
| 1463 | fd = (*handler) (fr, fs, ft); |
| 1464 | SPTOREG(fd, MIPSInst_FD(ir)); |
| 1465 | |
| 1466 | copcsr: |
| 1467 | if (ieee754_cxtest(IEEE754_INEXACT)) |
| 1468 | rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S; |
| 1469 | if (ieee754_cxtest(IEEE754_UNDERFLOW)) |
| 1470 | rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S; |
| 1471 | if (ieee754_cxtest(IEEE754_OVERFLOW)) |
| 1472 | rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S; |
| 1473 | if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) |
| 1474 | rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S; |
| 1475 | |
| 1476 | ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) { |
| 1478 | /*printk ("SIGFPE: fpu csr = %08x\n", |
| 1479 | ctx->fcr31); */ |
| 1480 | return SIGFPE; |
| 1481 | } |
| 1482 | |
| 1483 | break; |
| 1484 | |
| 1485 | default: |
| 1486 | return SIGILL; |
| 1487 | } |
| 1488 | break; |
| 1489 | } |
| 1490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | case d_fmt:{ /* 1 */ |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1492 | union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp); |
| 1493 | union ieee754dp fd, fr, fs, ft; |
Ralf Baechle | 3fccc01 | 2005-10-23 13:58:21 +0100 | [diff] [blame] | 1494 | u64 __user *va; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | u64 val; |
| 1496 | |
| 1497 | switch (MIPSInst_FUNC(ir)) { |
| 1498 | case ldxc1_op: |
Ralf Baechle | 3fccc01 | 2005-10-23 13:58:21 +0100 | [diff] [blame] | 1499 | va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | xcp->regs[MIPSInst_FT(ir)]); |
| 1501 | |
David Daney | b6ee75e | 2009-11-05 11:34:26 -0800 | [diff] [blame] | 1502 | MIPS_FPU_EMU_INC_STATS(loads); |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1503 | if (!access_ok(VERIFY_READ, va, sizeof(u64))) { |
David Daney | b6ee75e | 2009-11-05 11:34:26 -0800 | [diff] [blame] | 1504 | MIPS_FPU_EMU_INC_STATS(errors); |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1505 | *fault_addr = va; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | return SIGBUS; |
| 1507 | } |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1508 | if (__get_user(val, va)) { |
| 1509 | MIPS_FPU_EMU_INC_STATS(errors); |
| 1510 | *fault_addr = va; |
| 1511 | return SIGSEGV; |
| 1512 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | DITOREG(val, MIPSInst_FD(ir)); |
| 1514 | break; |
| 1515 | |
| 1516 | case sdxc1_op: |
Ralf Baechle | 3fccc01 | 2005-10-23 13:58:21 +0100 | [diff] [blame] | 1517 | va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | xcp->regs[MIPSInst_FT(ir)]); |
| 1519 | |
David Daney | b6ee75e | 2009-11-05 11:34:26 -0800 | [diff] [blame] | 1520 | MIPS_FPU_EMU_INC_STATS(stores); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | DIFROMREG(val, MIPSInst_FS(ir)); |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1522 | if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) { |
David Daney | b6ee75e | 2009-11-05 11:34:26 -0800 | [diff] [blame] | 1523 | MIPS_FPU_EMU_INC_STATS(errors); |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1524 | *fault_addr = va; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | return SIGBUS; |
| 1526 | } |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 1527 | if (__put_user(val, va)) { |
| 1528 | MIPS_FPU_EMU_INC_STATS(errors); |
| 1529 | *fault_addr = va; |
| 1530 | return SIGSEGV; |
| 1531 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | break; |
| 1533 | |
| 1534 | case madd_d_op: |
| 1535 | handler = fpemu_dp_madd; |
| 1536 | goto dcoptop; |
| 1537 | case msub_d_op: |
| 1538 | handler = fpemu_dp_msub; |
| 1539 | goto dcoptop; |
| 1540 | case nmadd_d_op: |
| 1541 | handler = fpemu_dp_nmadd; |
| 1542 | goto dcoptop; |
| 1543 | case nmsub_d_op: |
| 1544 | handler = fpemu_dp_nmsub; |
| 1545 | goto dcoptop; |
| 1546 | |
| 1547 | dcoptop: |
| 1548 | DPFROMREG(fr, MIPSInst_FR(ir)); |
| 1549 | DPFROMREG(fs, MIPSInst_FS(ir)); |
| 1550 | DPFROMREG(ft, MIPSInst_FT(ir)); |
| 1551 | fd = (*handler) (fr, fs, ft); |
| 1552 | DPTOREG(fd, MIPSInst_FD(ir)); |
| 1553 | goto copcsr; |
| 1554 | |
| 1555 | default: |
| 1556 | return SIGILL; |
| 1557 | } |
| 1558 | break; |
| 1559 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | |
Deng-Cheng Zhu | 51061b8 | 2014-03-06 17:05:27 -0800 | [diff] [blame] | 1561 | case 0x3: |
| 1562 | if (MIPSInst_FUNC(ir) != pfetch_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | return SIGILL; |
Deng-Cheng Zhu | 51061b8 | 2014-03-06 17:05:27 -0800 | [diff] [blame] | 1564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | /* ignore prefx operation */ |
| 1566 | break; |
| 1567 | |
| 1568 | default: |
| 1569 | return SIGILL; |
| 1570 | } |
| 1571 | |
| 1572 | return 0; |
| 1573 | } |
| 1574 | #endif |
| 1575 | |
| 1576 | |
| 1577 | |
| 1578 | /* |
| 1579 | * Emulate a single COP1 arithmetic instruction. |
| 1580 | */ |
Atsushi Nemoto | eae8907 | 2006-05-16 01:26:03 +0900 | [diff] [blame] | 1581 | static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | mips_instruction ir) |
| 1583 | { |
| 1584 | int rfmt; /* resulting format */ |
| 1585 | unsigned rcsr = 0; /* resulting csr */ |
| 1586 | unsigned cond; |
| 1587 | union { |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1588 | union ieee754dp d; |
| 1589 | union ieee754sp s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | int w; |
Yoichi Yuasa | 766160c | 2005-09-03 15:56:22 -0700 | [diff] [blame] | 1591 | #ifdef __mips64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | s64 l; |
| 1593 | #endif |
| 1594 | } rv; /* resulting value */ |
| 1595 | |
David Daney | b6ee75e | 2009-11-05 11:34:26 -0800 | [diff] [blame] | 1596 | MIPS_FPU_EMU_INC_STATS(cp1ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) { |
| 1598 | case s_fmt:{ /* 0 */ |
| 1599 | union { |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1600 | union ieee754sp(*b) (union ieee754sp, union ieee754sp); |
| 1601 | union ieee754sp(*u) (union ieee754sp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | } handler; |
| 1603 | |
| 1604 | switch (MIPSInst_FUNC(ir)) { |
| 1605 | /* binary ops */ |
| 1606 | case fadd_op: |
| 1607 | handler.b = ieee754sp_add; |
| 1608 | goto scopbop; |
| 1609 | case fsub_op: |
| 1610 | handler.b = ieee754sp_sub; |
| 1611 | goto scopbop; |
| 1612 | case fmul_op: |
| 1613 | handler.b = ieee754sp_mul; |
| 1614 | goto scopbop; |
| 1615 | case fdiv_op: |
| 1616 | handler.b = ieee754sp_div; |
| 1617 | goto scopbop; |
| 1618 | |
| 1619 | /* unary ops */ |
Ralf Baechle | 587cb98 | 2005-09-15 08:52:34 +0000 | [diff] [blame] | 1620 | #if __mips >= 2 || defined(__mips64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | case fsqrt_op: |
| 1622 | handler.u = ieee754sp_sqrt; |
| 1623 | goto scopuop; |
| 1624 | #endif |
| 1625 | #if __mips >= 4 && __mips != 32 |
| 1626 | case frsqrt_op: |
| 1627 | handler.u = fpemu_sp_rsqrt; |
| 1628 | goto scopuop; |
| 1629 | case frecip_op: |
| 1630 | handler.u = fpemu_sp_recip; |
| 1631 | goto scopuop; |
| 1632 | #endif |
| 1633 | #if __mips >= 4 |
| 1634 | case fmovc_op: |
| 1635 | cond = fpucondbit[MIPSInst_FT(ir) >> 2]; |
| 1636 | if (((ctx->fcr31 & cond) != 0) != |
| 1637 | ((MIPSInst_FT(ir) & 1) != 0)) |
| 1638 | return 0; |
| 1639 | SPFROMREG(rv.s, MIPSInst_FS(ir)); |
| 1640 | break; |
| 1641 | case fmovz_op: |
| 1642 | if (xcp->regs[MIPSInst_FT(ir)] != 0) |
| 1643 | return 0; |
| 1644 | SPFROMREG(rv.s, MIPSInst_FS(ir)); |
| 1645 | break; |
| 1646 | case fmovn_op: |
| 1647 | if (xcp->regs[MIPSInst_FT(ir)] == 0) |
| 1648 | return 0; |
| 1649 | SPFROMREG(rv.s, MIPSInst_FS(ir)); |
| 1650 | break; |
| 1651 | #endif |
| 1652 | case fabs_op: |
| 1653 | handler.u = ieee754sp_abs; |
| 1654 | goto scopuop; |
| 1655 | case fneg_op: |
| 1656 | handler.u = ieee754sp_neg; |
| 1657 | goto scopuop; |
| 1658 | case fmov_op: |
| 1659 | /* an easy one */ |
| 1660 | SPFROMREG(rv.s, MIPSInst_FS(ir)); |
| 1661 | goto copcsr; |
| 1662 | |
| 1663 | /* binary op on handler */ |
| 1664 | scopbop: |
| 1665 | { |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1666 | union ieee754sp fs, ft; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | |
| 1668 | SPFROMREG(fs, MIPSInst_FS(ir)); |
| 1669 | SPFROMREG(ft, MIPSInst_FT(ir)); |
| 1670 | |
| 1671 | rv.s = (*handler.b) (fs, ft); |
| 1672 | goto copcsr; |
| 1673 | } |
| 1674 | scopuop: |
| 1675 | { |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1676 | union ieee754sp fs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | |
| 1678 | SPFROMREG(fs, MIPSInst_FS(ir)); |
| 1679 | rv.s = (*handler.u) (fs); |
| 1680 | goto copcsr; |
| 1681 | } |
| 1682 | copcsr: |
| 1683 | if (ieee754_cxtest(IEEE754_INEXACT)) |
| 1684 | rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S; |
| 1685 | if (ieee754_cxtest(IEEE754_UNDERFLOW)) |
| 1686 | rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S; |
| 1687 | if (ieee754_cxtest(IEEE754_OVERFLOW)) |
| 1688 | rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S; |
| 1689 | if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) |
| 1690 | rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S; |
| 1691 | if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) |
| 1692 | rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S; |
| 1693 | break; |
| 1694 | |
| 1695 | /* unary conv ops */ |
| 1696 | case fcvts_op: |
| 1697 | return SIGILL; /* not defined */ |
| 1698 | case fcvtd_op:{ |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1699 | union ieee754sp fs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | |
| 1701 | SPFROMREG(fs, MIPSInst_FS(ir)); |
| 1702 | rv.d = ieee754dp_fsp(fs); |
| 1703 | rfmt = d_fmt; |
| 1704 | goto copcsr; |
| 1705 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | case fcvtw_op:{ |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1707 | union ieee754sp fs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | |
| 1709 | SPFROMREG(fs, MIPSInst_FS(ir)); |
| 1710 | rv.w = ieee754sp_tint(fs); |
| 1711 | rfmt = w_fmt; |
| 1712 | goto copcsr; |
| 1713 | } |
| 1714 | |
Ralf Baechle | 587cb98 | 2005-09-15 08:52:34 +0000 | [diff] [blame] | 1715 | #if __mips >= 2 || defined(__mips64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | case fround_op: |
| 1717 | case ftrunc_op: |
| 1718 | case fceil_op: |
| 1719 | case ffloor_op:{ |
| 1720 | unsigned int oldrm = ieee754_csr.rm; |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1721 | union ieee754sp fs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | |
| 1723 | SPFROMREG(fs, MIPSInst_FS(ir)); |
Shane McDonald | 3f13553 | 2010-05-07 00:02:09 -0600 | [diff] [blame] | 1724 | ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | rv.w = ieee754sp_tint(fs); |
| 1726 | ieee754_csr.rm = oldrm; |
| 1727 | rfmt = w_fmt; |
| 1728 | goto copcsr; |
| 1729 | } |
| 1730 | #endif /* __mips >= 2 */ |
| 1731 | |
Ralf Baechle | 4b724ef | 2005-10-23 15:05:47 +0100 | [diff] [blame] | 1732 | #if defined(__mips64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | case fcvtl_op:{ |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1734 | union ieee754sp fs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | |
| 1736 | SPFROMREG(fs, MIPSInst_FS(ir)); |
| 1737 | rv.l = ieee754sp_tlong(fs); |
| 1738 | rfmt = l_fmt; |
| 1739 | goto copcsr; |
| 1740 | } |
| 1741 | |
| 1742 | case froundl_op: |
| 1743 | case ftruncl_op: |
| 1744 | case fceill_op: |
| 1745 | case ffloorl_op:{ |
| 1746 | unsigned int oldrm = ieee754_csr.rm; |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1747 | union ieee754sp fs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | |
| 1749 | SPFROMREG(fs, MIPSInst_FS(ir)); |
Shane McDonald | 3f13553 | 2010-05-07 00:02:09 -0600 | [diff] [blame] | 1750 | ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | rv.l = ieee754sp_tlong(fs); |
| 1752 | ieee754_csr.rm = oldrm; |
| 1753 | rfmt = l_fmt; |
| 1754 | goto copcsr; |
| 1755 | } |
Ralf Baechle | 4b724ef | 2005-10-23 15:05:47 +0100 | [diff] [blame] | 1756 | #endif /* defined(__mips64) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | |
| 1758 | default: |
| 1759 | if (MIPSInst_FUNC(ir) >= fcmp_op) { |
| 1760 | unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op; |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1761 | union ieee754sp fs, ft; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | |
| 1763 | SPFROMREG(fs, MIPSInst_FS(ir)); |
| 1764 | SPFROMREG(ft, MIPSInst_FT(ir)); |
| 1765 | rv.w = ieee754sp_cmp(fs, ft, |
| 1766 | cmptab[cmpop & 0x7], cmpop & 0x8); |
| 1767 | rfmt = -1; |
| 1768 | if ((cmpop & 0x8) && ieee754_cxtest |
| 1769 | (IEEE754_INVALID_OPERATION)) |
| 1770 | rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S; |
| 1771 | else |
| 1772 | goto copcsr; |
| 1773 | |
| 1774 | } |
| 1775 | else { |
| 1776 | return SIGILL; |
| 1777 | } |
| 1778 | break; |
| 1779 | } |
| 1780 | break; |
| 1781 | } |
| 1782 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | case d_fmt:{ |
| 1784 | union { |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1785 | union ieee754dp(*b) (union ieee754dp, union ieee754dp); |
| 1786 | union ieee754dp(*u) (union ieee754dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | } handler; |
| 1788 | |
| 1789 | switch (MIPSInst_FUNC(ir)) { |
| 1790 | /* binary ops */ |
| 1791 | case fadd_op: |
| 1792 | handler.b = ieee754dp_add; |
| 1793 | goto dcopbop; |
| 1794 | case fsub_op: |
| 1795 | handler.b = ieee754dp_sub; |
| 1796 | goto dcopbop; |
| 1797 | case fmul_op: |
| 1798 | handler.b = ieee754dp_mul; |
| 1799 | goto dcopbop; |
| 1800 | case fdiv_op: |
| 1801 | handler.b = ieee754dp_div; |
| 1802 | goto dcopbop; |
| 1803 | |
| 1804 | /* unary ops */ |
Ralf Baechle | 587cb98 | 2005-09-15 08:52:34 +0000 | [diff] [blame] | 1805 | #if __mips >= 2 || defined(__mips64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | case fsqrt_op: |
| 1807 | handler.u = ieee754dp_sqrt; |
| 1808 | goto dcopuop; |
| 1809 | #endif |
| 1810 | #if __mips >= 4 && __mips != 32 |
| 1811 | case frsqrt_op: |
| 1812 | handler.u = fpemu_dp_rsqrt; |
| 1813 | goto dcopuop; |
| 1814 | case frecip_op: |
| 1815 | handler.u = fpemu_dp_recip; |
| 1816 | goto dcopuop; |
| 1817 | #endif |
| 1818 | #if __mips >= 4 |
| 1819 | case fmovc_op: |
| 1820 | cond = fpucondbit[MIPSInst_FT(ir) >> 2]; |
| 1821 | if (((ctx->fcr31 & cond) != 0) != |
| 1822 | ((MIPSInst_FT(ir) & 1) != 0)) |
| 1823 | return 0; |
| 1824 | DPFROMREG(rv.d, MIPSInst_FS(ir)); |
| 1825 | break; |
| 1826 | case fmovz_op: |
| 1827 | if (xcp->regs[MIPSInst_FT(ir)] != 0) |
| 1828 | return 0; |
| 1829 | DPFROMREG(rv.d, MIPSInst_FS(ir)); |
| 1830 | break; |
| 1831 | case fmovn_op: |
| 1832 | if (xcp->regs[MIPSInst_FT(ir)] == 0) |
| 1833 | return 0; |
| 1834 | DPFROMREG(rv.d, MIPSInst_FS(ir)); |
| 1835 | break; |
| 1836 | #endif |
| 1837 | case fabs_op: |
| 1838 | handler.u = ieee754dp_abs; |
| 1839 | goto dcopuop; |
| 1840 | |
| 1841 | case fneg_op: |
| 1842 | handler.u = ieee754dp_neg; |
| 1843 | goto dcopuop; |
| 1844 | |
| 1845 | case fmov_op: |
| 1846 | /* an easy one */ |
| 1847 | DPFROMREG(rv.d, MIPSInst_FS(ir)); |
| 1848 | goto copcsr; |
| 1849 | |
| 1850 | /* binary op on handler */ |
| 1851 | dcopbop:{ |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1852 | union ieee754dp fs, ft; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | |
| 1854 | DPFROMREG(fs, MIPSInst_FS(ir)); |
| 1855 | DPFROMREG(ft, MIPSInst_FT(ir)); |
| 1856 | |
| 1857 | rv.d = (*handler.b) (fs, ft); |
| 1858 | goto copcsr; |
| 1859 | } |
| 1860 | dcopuop:{ |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1861 | union ieee754dp fs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | |
| 1863 | DPFROMREG(fs, MIPSInst_FS(ir)); |
| 1864 | rv.d = (*handler.u) (fs); |
| 1865 | goto copcsr; |
| 1866 | } |
| 1867 | |
| 1868 | /* unary conv ops */ |
| 1869 | case fcvts_op:{ |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1870 | union ieee754dp fs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | |
| 1872 | DPFROMREG(fs, MIPSInst_FS(ir)); |
| 1873 | rv.s = ieee754sp_fdp(fs); |
| 1874 | rfmt = s_fmt; |
| 1875 | goto copcsr; |
| 1876 | } |
| 1877 | case fcvtd_op: |
| 1878 | return SIGILL; /* not defined */ |
| 1879 | |
| 1880 | case fcvtw_op:{ |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1881 | union ieee754dp fs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | |
| 1883 | DPFROMREG(fs, MIPSInst_FS(ir)); |
| 1884 | rv.w = ieee754dp_tint(fs); /* wrong */ |
| 1885 | rfmt = w_fmt; |
| 1886 | goto copcsr; |
| 1887 | } |
| 1888 | |
Ralf Baechle | 587cb98 | 2005-09-15 08:52:34 +0000 | [diff] [blame] | 1889 | #if __mips >= 2 || defined(__mips64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | case fround_op: |
| 1891 | case ftrunc_op: |
| 1892 | case fceil_op: |
| 1893 | case ffloor_op:{ |
| 1894 | unsigned int oldrm = ieee754_csr.rm; |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1895 | union ieee754dp fs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | |
| 1897 | DPFROMREG(fs, MIPSInst_FS(ir)); |
Shane McDonald | 3f13553 | 2010-05-07 00:02:09 -0600 | [diff] [blame] | 1898 | ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | rv.w = ieee754dp_tint(fs); |
| 1900 | ieee754_csr.rm = oldrm; |
| 1901 | rfmt = w_fmt; |
| 1902 | goto copcsr; |
| 1903 | } |
| 1904 | #endif |
| 1905 | |
Ralf Baechle | 4b724ef | 2005-10-23 15:05:47 +0100 | [diff] [blame] | 1906 | #if defined(__mips64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | case fcvtl_op:{ |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1908 | union ieee754dp fs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | |
| 1910 | DPFROMREG(fs, MIPSInst_FS(ir)); |
| 1911 | rv.l = ieee754dp_tlong(fs); |
| 1912 | rfmt = l_fmt; |
| 1913 | goto copcsr; |
| 1914 | } |
| 1915 | |
| 1916 | case froundl_op: |
| 1917 | case ftruncl_op: |
| 1918 | case fceill_op: |
| 1919 | case ffloorl_op:{ |
| 1920 | unsigned int oldrm = ieee754_csr.rm; |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1921 | union ieee754dp fs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | |
| 1923 | DPFROMREG(fs, MIPSInst_FS(ir)); |
Shane McDonald | 3f13553 | 2010-05-07 00:02:09 -0600 | [diff] [blame] | 1924 | ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | rv.l = ieee754dp_tlong(fs); |
| 1926 | ieee754_csr.rm = oldrm; |
| 1927 | rfmt = l_fmt; |
| 1928 | goto copcsr; |
| 1929 | } |
Ralf Baechle | 4b724ef | 2005-10-23 15:05:47 +0100 | [diff] [blame] | 1930 | #endif /* __mips >= 3 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | |
| 1932 | default: |
| 1933 | if (MIPSInst_FUNC(ir) >= fcmp_op) { |
| 1934 | unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op; |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1935 | union ieee754dp fs, ft; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | |
| 1937 | DPFROMREG(fs, MIPSInst_FS(ir)); |
| 1938 | DPFROMREG(ft, MIPSInst_FT(ir)); |
| 1939 | rv.w = ieee754dp_cmp(fs, ft, |
| 1940 | cmptab[cmpop & 0x7], cmpop & 0x8); |
| 1941 | rfmt = -1; |
| 1942 | if ((cmpop & 0x8) |
| 1943 | && |
| 1944 | ieee754_cxtest |
| 1945 | (IEEE754_INVALID_OPERATION)) |
| 1946 | rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S; |
| 1947 | else |
| 1948 | goto copcsr; |
| 1949 | |
| 1950 | } |
| 1951 | else { |
| 1952 | return SIGILL; |
| 1953 | } |
| 1954 | break; |
| 1955 | } |
| 1956 | break; |
| 1957 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | |
| 1959 | case w_fmt:{ |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 1960 | union ieee754sp fs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | |
| 1962 | switch (MIPSInst_FUNC(ir)) { |
| 1963 | case fcvts_op: |
| 1964 | /* convert word to single precision real */ |
| 1965 | SPFROMREG(fs, MIPSInst_FS(ir)); |
| 1966 | rv.s = ieee754sp_fint(fs.bits); |
| 1967 | rfmt = s_fmt; |
| 1968 | goto copcsr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | case fcvtd_op: |
| 1970 | /* convert word to double precision real */ |
| 1971 | SPFROMREG(fs, MIPSInst_FS(ir)); |
| 1972 | rv.d = ieee754dp_fint(fs.bits); |
| 1973 | rfmt = d_fmt; |
| 1974 | goto copcsr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | default: |
| 1976 | return SIGILL; |
| 1977 | } |
| 1978 | break; |
| 1979 | } |
| 1980 | |
Ralf Baechle | 4b724ef | 2005-10-23 15:05:47 +0100 | [diff] [blame] | 1981 | #if defined(__mips64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | case l_fmt:{ |
Paul Burton | bbd426f | 2014-02-13 11:26:41 +0000 | [diff] [blame] | 1983 | u64 bits; |
| 1984 | DIFROMREG(bits, MIPSInst_FS(ir)); |
| 1985 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | switch (MIPSInst_FUNC(ir)) { |
| 1987 | case fcvts_op: |
| 1988 | /* convert long to single precision real */ |
Paul Burton | bbd426f | 2014-02-13 11:26:41 +0000 | [diff] [blame] | 1989 | rv.s = ieee754sp_flong(bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | rfmt = s_fmt; |
| 1991 | goto copcsr; |
| 1992 | case fcvtd_op: |
| 1993 | /* convert long to double precision real */ |
Paul Burton | bbd426f | 2014-02-13 11:26:41 +0000 | [diff] [blame] | 1994 | rv.d = ieee754dp_flong(bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | rfmt = d_fmt; |
| 1996 | goto copcsr; |
| 1997 | default: |
| 1998 | return SIGILL; |
| 1999 | } |
| 2000 | break; |
| 2001 | } |
| 2002 | #endif |
| 2003 | |
| 2004 | default: |
| 2005 | return SIGILL; |
| 2006 | } |
| 2007 | |
| 2008 | /* |
| 2009 | * Update the fpu CSR register for this operation. |
| 2010 | * If an exception is required, generate a tidy SIGFPE exception, |
| 2011 | * without updating the result register. |
| 2012 | * Note: cause exception bits do not accumulate, they are rewritten |
| 2013 | * for each op; only the flag/sticky bits accumulate. |
| 2014 | */ |
| 2015 | ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr; |
| 2016 | if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) { |
| 2017 | /*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */ |
| 2018 | return SIGFPE; |
| 2019 | } |
| 2020 | |
| 2021 | /* |
| 2022 | * Now we can safely write the result back to the register file. |
| 2023 | */ |
| 2024 | switch (rfmt) { |
| 2025 | case -1:{ |
| 2026 | #if __mips >= 4 |
| 2027 | cond = fpucondbit[MIPSInst_FD(ir) >> 2]; |
| 2028 | #else |
| 2029 | cond = FPU_CSR_COND; |
| 2030 | #endif |
| 2031 | if (rv.w) |
| 2032 | ctx->fcr31 |= cond; |
| 2033 | else |
| 2034 | ctx->fcr31 &= ~cond; |
| 2035 | break; |
| 2036 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | case d_fmt: |
| 2038 | DPTOREG(rv.d, MIPSInst_FD(ir)); |
| 2039 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | case s_fmt: |
| 2041 | SPTOREG(rv.s, MIPSInst_FD(ir)); |
| 2042 | break; |
| 2043 | case w_fmt: |
| 2044 | SITOREG(rv.w, MIPSInst_FD(ir)); |
| 2045 | break; |
Ralf Baechle | 4b724ef | 2005-10-23 15:05:47 +0100 | [diff] [blame] | 2046 | #if defined(__mips64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | case l_fmt: |
| 2048 | DITOREG(rv.l, MIPSInst_FD(ir)); |
| 2049 | break; |
| 2050 | #endif |
| 2051 | default: |
| 2052 | return SIGILL; |
| 2053 | } |
| 2054 | |
| 2055 | return 0; |
| 2056 | } |
| 2057 | |
Atsushi Nemoto | e04582b | 2006-10-09 00:10:01 +0900 | [diff] [blame] | 2058 | int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx, |
David Daney | 515b029 | 2010-10-21 16:32:26 -0700 | [diff] [blame] | 2059 | int has_fpu, void *__user *fault_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | { |
Ralf Baechle | 333d1f6 | 2005-02-28 17:55:57 +0000 | [diff] [blame] | 2061 | unsigned long oldepc, prevepc; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 2062 | struct mm_decoded_insn dec_insn; |
| 2063 | u16 instr[4]; |
| 2064 | u16 *instr_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | int sig = 0; |
| 2066 | |
| 2067 | oldepc = xcp->cp0_epc; |
| 2068 | do { |
| 2069 | prevepc = xcp->cp0_epc; |
| 2070 | |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 2071 | if (get_isa16_mode(prevepc) && cpu_has_mmips) { |
| 2072 | /* |
| 2073 | * Get next 2 microMIPS instructions and convert them |
| 2074 | * into 32-bit instructions. |
| 2075 | */ |
| 2076 | if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) || |
| 2077 | (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) || |
| 2078 | (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) || |
| 2079 | (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) { |
| 2080 | MIPS_FPU_EMU_INC_STATS(errors); |
| 2081 | return SIGBUS; |
| 2082 | } |
| 2083 | instr_ptr = instr; |
| 2084 | |
| 2085 | /* Get first instruction. */ |
| 2086 | if (mm_insn_16bit(*instr_ptr)) { |
| 2087 | /* Duplicate the half-word. */ |
| 2088 | dec_insn.insn = (*instr_ptr << 16) | |
| 2089 | (*instr_ptr); |
| 2090 | /* 16-bit instruction. */ |
| 2091 | dec_insn.pc_inc = 2; |
| 2092 | instr_ptr += 1; |
| 2093 | } else { |
| 2094 | dec_insn.insn = (*instr_ptr << 16) | |
| 2095 | *(instr_ptr+1); |
| 2096 | /* 32-bit instruction. */ |
| 2097 | dec_insn.pc_inc = 4; |
| 2098 | instr_ptr += 2; |
| 2099 | } |
| 2100 | /* Get second instruction. */ |
| 2101 | if (mm_insn_16bit(*instr_ptr)) { |
| 2102 | /* Duplicate the half-word. */ |
| 2103 | dec_insn.next_insn = (*instr_ptr << 16) | |
| 2104 | (*instr_ptr); |
| 2105 | /* 16-bit instruction. */ |
| 2106 | dec_insn.next_pc_inc = 2; |
| 2107 | } else { |
| 2108 | dec_insn.next_insn = (*instr_ptr << 16) | |
| 2109 | *(instr_ptr+1); |
| 2110 | /* 32-bit instruction. */ |
| 2111 | dec_insn.next_pc_inc = 4; |
| 2112 | } |
| 2113 | dec_insn.micro_mips_mode = 1; |
| 2114 | } else { |
| 2115 | if ((get_user(dec_insn.insn, |
| 2116 | (mips_instruction __user *) xcp->cp0_epc)) || |
| 2117 | (get_user(dec_insn.next_insn, |
| 2118 | (mips_instruction __user *)(xcp->cp0_epc+4)))) { |
| 2119 | MIPS_FPU_EMU_INC_STATS(errors); |
| 2120 | return SIGBUS; |
| 2121 | } |
| 2122 | dec_insn.pc_inc = 4; |
| 2123 | dec_insn.next_pc_inc = 4; |
| 2124 | dec_insn.micro_mips_mode = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | } |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 2126 | |
| 2127 | if ((dec_insn.insn == 0) || |
| 2128 | ((dec_insn.pc_inc == 2) && |
| 2129 | ((dec_insn.insn & 0xffff) == MM_NOP16))) |
| 2130 | xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | else { |
Ralf Baechle | cd21dfc | 2005-04-28 13:39:10 +0000 | [diff] [blame] | 2132 | /* |
| 2133 | * The 'ieee754_csr' is an alias of |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 2134 | * ctx->fcr31. No need to copy ctx->fcr31 to |
| 2135 | * ieee754_csr. But ieee754_csr.rm is ieee |
Ralf Baechle | cd21dfc | 2005-04-28 13:39:10 +0000 | [diff] [blame] | 2136 | * library modes. (not mips rounding mode) |
| 2137 | */ |
| 2138 | /* convert to ieee library modes */ |
| 2139 | ieee754_csr.rm = ieee_rm[ieee754_csr.rm]; |
Leonid Yegoshin | 102cedc | 2013-03-25 12:09:02 -0500 | [diff] [blame] | 2140 | sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr); |
Ralf Baechle | cd21dfc | 2005-04-28 13:39:10 +0000 | [diff] [blame] | 2141 | /* revert to mips rounding mode */ |
| 2142 | ieee754_csr.rm = mips_rm[ieee754_csr.rm]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | } |
| 2144 | |
Atsushi Nemoto | e04582b | 2006-10-09 00:10:01 +0900 | [diff] [blame] | 2145 | if (has_fpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | break; |
| 2147 | if (sig) |
| 2148 | break; |
| 2149 | |
| 2150 | cond_resched(); |
| 2151 | } while (xcp->cp0_epc > prevepc); |
| 2152 | |
| 2153 | /* SIGILL indicates a non-fpu instruction */ |
| 2154 | if (sig == SIGILL && xcp->cp0_epc != oldepc) |
| 2155 | /* but if epc has advanced, then ignore it */ |
| 2156 | sig = 0; |
| 2157 | |
| 2158 | return sig; |
| 2159 | } |