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