blob: a1f06ad017595d479c06d1a4908e1a845833458d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002 * cp1emu.c: a MIPS coprocessor 1 (FPU) instruction emulator
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * MIPS floating point support
5 * Copyright (C) 1994-2000 Algorithmics Ltd.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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 Baechle3f7cac42014-04-26 01:49:14 +020021 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 *
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 Baechle3f7cac42014-04-26 01:49:14 +020028 * the hardware FPU at the boundaries of the IEEE-754 representation
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * (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 Baechle3f7cac42014-04-26 01:49:14 +020033 * Note if you know that you won't have an FPU, then you'll get much
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * better performance by compiling with -msoft-float!
35 */
36#include <linux/sched.h>
Atsushi Nemoto83fd38c2007-07-07 23:21:49 +090037#include <linux/debugfs.h>
Ralf Baechle85c51c52014-04-16 02:46:11 +020038#include <linux/percpu-defs.h>
Deng-Cheng Zhu7f788d22010-10-12 19:37:21 +080039#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Ralf Baechlecd8ee342014-04-16 02:09:53 +020041#include <asm/branch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/inst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/ptrace.h>
44#include <asm/signal.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080045#include <linux/uaccess.h>
Ralf Baechlecd8ee342014-04-16 02:09:53 +020046
Maciej W. Rozyckif6843622015-04-03 23:27:26 +010047#include <asm/cpu-info.h>
Ralf Baechlecd8ee342014-04-16 02:09:53 +020048#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/fpu_emulator.h>
Leonid Yegoshin102cedc2013-03-25 12:09:02 -050050#include <asm/fpu.h>
Leonid Yegoshinb0a668f2014-12-03 15:47:03 +000051#include <asm/mips-r2-to-r6-emul.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#include "ieee754.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/* Function which emulates a floating point instruction. */
56
Atsushi Nemotoeae89072006-05-16 01:26:03 +090057static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 mips_instruction);
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static int fpux_emu(struct pt_regs *,
Paul Burton445a58c2017-08-23 11:17:51 -070061 struct mips_fpu_struct *, mips_instruction, void __user **);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/* Control registers */
64
65#define FPCREG_RID 0 /* $0 = revision id */
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +010066#define FPCREG_FCCR 25 /* $25 = fccr */
67#define FPCREG_FEXR 26 /* $26 = fexr */
68#define FPCREG_FENR 28 /* $28 = fenr */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define FPCREG_CSR 31 /* $31 = csr */
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* convert condition code register number to csr bit */
Leonid Yegoshinb0a668f2014-12-03 15:47:03 +000072const unsigned int fpucondbit[8] = {
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +010073 FPU_CSR_COND,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 FPU_CSR_COND1,
75 FPU_CSR_COND2,
76 FPU_CSR_COND3,
77 FPU_CSR_COND4,
78 FPU_CSR_COND5,
79 FPU_CSR_COND6,
80 FPU_CSR_COND7
81};
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Leonid Yegoshin102cedc2013-03-25 12:09:02 -050083/* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
84static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
85static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
86static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
87static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
88
89/*
90 * This functions translates a 32-bit microMIPS instruction
91 * into a 32-bit MIPS32 instruction. Returns 0 on success
92 * and SIGILL otherwise.
93 */
94static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
95{
96 union mips_instruction insn = *insn_ptr;
97 union mips_instruction mips32_insn = insn;
98 int func, fmt, op;
99
100 switch (insn.mm_i_format.opcode) {
101 case mm_ldc132_op:
102 mips32_insn.mm_i_format.opcode = ldc1_op;
103 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
104 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
105 break;
106 case mm_lwc132_op:
107 mips32_insn.mm_i_format.opcode = lwc1_op;
108 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
109 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
110 break;
111 case mm_sdc132_op:
112 mips32_insn.mm_i_format.opcode = sdc1_op;
113 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
114 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
115 break;
116 case mm_swc132_op:
117 mips32_insn.mm_i_format.opcode = swc1_op;
118 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
119 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
120 break;
121 case mm_pool32i_op:
122 /* NOTE: offset is << by 1 if in microMIPS mode. */
123 if ((insn.mm_i_format.rt == mm_bc1f_op) ||
124 (insn.mm_i_format.rt == mm_bc1t_op)) {
125 mips32_insn.fb_format.opcode = cop1_op;
126 mips32_insn.fb_format.bc = bc_op;
127 mips32_insn.fb_format.flag =
128 (insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
129 } else
130 return SIGILL;
131 break;
132 case mm_pool32f_op:
133 switch (insn.mm_fp0_format.func) {
134 case mm_32f_01_op:
135 case mm_32f_11_op:
136 case mm_32f_02_op:
137 case mm_32f_12_op:
138 case mm_32f_41_op:
139 case mm_32f_51_op:
140 case mm_32f_42_op:
141 case mm_32f_52_op:
142 op = insn.mm_fp0_format.func;
143 if (op == mm_32f_01_op)
144 func = madd_s_op;
145 else if (op == mm_32f_11_op)
146 func = madd_d_op;
147 else if (op == mm_32f_02_op)
148 func = nmadd_s_op;
149 else if (op == mm_32f_12_op)
150 func = nmadd_d_op;
151 else if (op == mm_32f_41_op)
152 func = msub_s_op;
153 else if (op == mm_32f_51_op)
154 func = msub_d_op;
155 else if (op == mm_32f_42_op)
156 func = nmsub_s_op;
157 else
158 func = nmsub_d_op;
159 mips32_insn.fp6_format.opcode = cop1x_op;
160 mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
161 mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
162 mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
163 mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
164 mips32_insn.fp6_format.func = func;
165 break;
166 case mm_32f_10_op:
167 func = -1; /* Invalid */
168 op = insn.mm_fp5_format.op & 0x7;
169 if (op == mm_ldxc1_op)
170 func = ldxc1_op;
171 else if (op == mm_sdxc1_op)
172 func = sdxc1_op;
173 else if (op == mm_lwxc1_op)
174 func = lwxc1_op;
175 else if (op == mm_swxc1_op)
176 func = swxc1_op;
177
178 if (func != -1) {
179 mips32_insn.r_format.opcode = cop1x_op;
180 mips32_insn.r_format.rs =
181 insn.mm_fp5_format.base;
182 mips32_insn.r_format.rt =
183 insn.mm_fp5_format.index;
184 mips32_insn.r_format.rd = 0;
185 mips32_insn.r_format.re = insn.mm_fp5_format.fd;
186 mips32_insn.r_format.func = func;
187 } else
188 return SIGILL;
189 break;
190 case mm_32f_40_op:
191 op = -1; /* Invalid */
192 if (insn.mm_fp2_format.op == mm_fmovt_op)
193 op = 1;
194 else if (insn.mm_fp2_format.op == mm_fmovf_op)
195 op = 0;
196 if (op != -1) {
197 mips32_insn.fp0_format.opcode = cop1_op;
198 mips32_insn.fp0_format.fmt =
199 sdps_format[insn.mm_fp2_format.fmt];
200 mips32_insn.fp0_format.ft =
201 (insn.mm_fp2_format.cc<<2) + op;
202 mips32_insn.fp0_format.fs =
203 insn.mm_fp2_format.fs;
204 mips32_insn.fp0_format.fd =
205 insn.mm_fp2_format.fd;
206 mips32_insn.fp0_format.func = fmovc_op;
207 } else
208 return SIGILL;
209 break;
210 case mm_32f_60_op:
211 func = -1; /* Invalid */
212 if (insn.mm_fp0_format.op == mm_fadd_op)
213 func = fadd_op;
214 else if (insn.mm_fp0_format.op == mm_fsub_op)
215 func = fsub_op;
216 else if (insn.mm_fp0_format.op == mm_fmul_op)
217 func = fmul_op;
218 else if (insn.mm_fp0_format.op == mm_fdiv_op)
219 func = fdiv_op;
220 if (func != -1) {
221 mips32_insn.fp0_format.opcode = cop1_op;
222 mips32_insn.fp0_format.fmt =
223 sdps_format[insn.mm_fp0_format.fmt];
224 mips32_insn.fp0_format.ft =
225 insn.mm_fp0_format.ft;
226 mips32_insn.fp0_format.fs =
227 insn.mm_fp0_format.fs;
228 mips32_insn.fp0_format.fd =
229 insn.mm_fp0_format.fd;
230 mips32_insn.fp0_format.func = func;
231 } else
232 return SIGILL;
233 break;
234 case mm_32f_70_op:
235 func = -1; /* Invalid */
236 if (insn.mm_fp0_format.op == mm_fmovn_op)
237 func = fmovn_op;
238 else if (insn.mm_fp0_format.op == mm_fmovz_op)
239 func = fmovz_op;
240 if (func != -1) {
241 mips32_insn.fp0_format.opcode = cop1_op;
242 mips32_insn.fp0_format.fmt =
243 sdps_format[insn.mm_fp0_format.fmt];
244 mips32_insn.fp0_format.ft =
245 insn.mm_fp0_format.ft;
246 mips32_insn.fp0_format.fs =
247 insn.mm_fp0_format.fs;
248 mips32_insn.fp0_format.fd =
249 insn.mm_fp0_format.fd;
250 mips32_insn.fp0_format.func = func;
251 } else
252 return SIGILL;
253 break;
254 case mm_32f_73_op: /* POOL32FXF */
255 switch (insn.mm_fp1_format.op) {
256 case mm_movf0_op:
257 case mm_movf1_op:
258 case mm_movt0_op:
259 case mm_movt1_op:
260 if ((insn.mm_fp1_format.op & 0x7f) ==
261 mm_movf0_op)
262 op = 0;
263 else
264 op = 1;
265 mips32_insn.r_format.opcode = spec_op;
266 mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
267 mips32_insn.r_format.rt =
268 (insn.mm_fp4_format.cc << 2) + op;
269 mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
270 mips32_insn.r_format.re = 0;
271 mips32_insn.r_format.func = movc_op;
272 break;
273 case mm_fcvtd0_op:
274 case mm_fcvtd1_op:
275 case mm_fcvts0_op:
276 case mm_fcvts1_op:
277 if ((insn.mm_fp1_format.op & 0x7f) ==
278 mm_fcvtd0_op) {
279 func = fcvtd_op;
280 fmt = swl_format[insn.mm_fp3_format.fmt];
281 } else {
282 func = fcvts_op;
283 fmt = dwl_format[insn.mm_fp3_format.fmt];
284 }
285 mips32_insn.fp0_format.opcode = cop1_op;
286 mips32_insn.fp0_format.fmt = fmt;
287 mips32_insn.fp0_format.ft = 0;
288 mips32_insn.fp0_format.fs =
289 insn.mm_fp3_format.fs;
290 mips32_insn.fp0_format.fd =
291 insn.mm_fp3_format.rt;
292 mips32_insn.fp0_format.func = func;
293 break;
294 case mm_fmov0_op:
295 case mm_fmov1_op:
296 case mm_fabs0_op:
297 case mm_fabs1_op:
298 case mm_fneg0_op:
299 case mm_fneg1_op:
300 if ((insn.mm_fp1_format.op & 0x7f) ==
301 mm_fmov0_op)
302 func = fmov_op;
303 else if ((insn.mm_fp1_format.op & 0x7f) ==
304 mm_fabs0_op)
305 func = fabs_op;
306 else
307 func = fneg_op;
308 mips32_insn.fp0_format.opcode = cop1_op;
309 mips32_insn.fp0_format.fmt =
310 sdps_format[insn.mm_fp3_format.fmt];
311 mips32_insn.fp0_format.ft = 0;
312 mips32_insn.fp0_format.fs =
313 insn.mm_fp3_format.fs;
314 mips32_insn.fp0_format.fd =
315 insn.mm_fp3_format.rt;
316 mips32_insn.fp0_format.func = func;
317 break;
318 case mm_ffloorl_op:
319 case mm_ffloorw_op:
320 case mm_fceill_op:
321 case mm_fceilw_op:
322 case mm_ftruncl_op:
323 case mm_ftruncw_op:
324 case mm_froundl_op:
325 case mm_froundw_op:
326 case mm_fcvtl_op:
327 case mm_fcvtw_op:
328 if (insn.mm_fp1_format.op == mm_ffloorl_op)
329 func = ffloorl_op;
330 else if (insn.mm_fp1_format.op == mm_ffloorw_op)
331 func = ffloor_op;
332 else if (insn.mm_fp1_format.op == mm_fceill_op)
333 func = fceill_op;
334 else if (insn.mm_fp1_format.op == mm_fceilw_op)
335 func = fceil_op;
336 else if (insn.mm_fp1_format.op == mm_ftruncl_op)
337 func = ftruncl_op;
338 else if (insn.mm_fp1_format.op == mm_ftruncw_op)
339 func = ftrunc_op;
340 else if (insn.mm_fp1_format.op == mm_froundl_op)
341 func = froundl_op;
342 else if (insn.mm_fp1_format.op == mm_froundw_op)
343 func = fround_op;
344 else if (insn.mm_fp1_format.op == mm_fcvtl_op)
345 func = fcvtl_op;
346 else
347 func = fcvtw_op;
348 mips32_insn.fp0_format.opcode = cop1_op;
349 mips32_insn.fp0_format.fmt =
350 sd_format[insn.mm_fp1_format.fmt];
351 mips32_insn.fp0_format.ft = 0;
352 mips32_insn.fp0_format.fs =
353 insn.mm_fp1_format.fs;
354 mips32_insn.fp0_format.fd =
355 insn.mm_fp1_format.rt;
356 mips32_insn.fp0_format.func = func;
357 break;
358 case mm_frsqrt_op:
359 case mm_fsqrt_op:
360 case mm_frecip_op:
361 if (insn.mm_fp1_format.op == mm_frsqrt_op)
362 func = frsqrt_op;
363 else if (insn.mm_fp1_format.op == mm_fsqrt_op)
364 func = fsqrt_op;
365 else
366 func = frecip_op;
367 mips32_insn.fp0_format.opcode = cop1_op;
368 mips32_insn.fp0_format.fmt =
369 sdps_format[insn.mm_fp1_format.fmt];
370 mips32_insn.fp0_format.ft = 0;
371 mips32_insn.fp0_format.fs =
372 insn.mm_fp1_format.fs;
373 mips32_insn.fp0_format.fd =
374 insn.mm_fp1_format.rt;
375 mips32_insn.fp0_format.func = func;
376 break;
377 case mm_mfc1_op:
378 case mm_mtc1_op:
379 case mm_cfc1_op:
380 case mm_ctc1_op:
Steven J. Hill9355e592013-11-07 12:48:29 +0000381 case mm_mfhc1_op:
382 case mm_mthc1_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500383 if (insn.mm_fp1_format.op == mm_mfc1_op)
384 op = mfc_op;
385 else if (insn.mm_fp1_format.op == mm_mtc1_op)
386 op = mtc_op;
387 else if (insn.mm_fp1_format.op == mm_cfc1_op)
388 op = cfc_op;
Steven J. Hill9355e592013-11-07 12:48:29 +0000389 else if (insn.mm_fp1_format.op == mm_ctc1_op)
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500390 op = ctc_op;
Steven J. Hill9355e592013-11-07 12:48:29 +0000391 else if (insn.mm_fp1_format.op == mm_mfhc1_op)
392 op = mfhc_op;
393 else
394 op = mthc_op;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500395 mips32_insn.fp1_format.opcode = cop1_op;
396 mips32_insn.fp1_format.op = op;
397 mips32_insn.fp1_format.rt =
398 insn.mm_fp1_format.rt;
399 mips32_insn.fp1_format.fs =
400 insn.mm_fp1_format.fs;
401 mips32_insn.fp1_format.fd = 0;
402 mips32_insn.fp1_format.func = 0;
403 break;
404 default:
405 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500406 }
407 break;
408 case mm_32f_74_op: /* c.cond.fmt */
409 mips32_insn.fp0_format.opcode = cop1_op;
410 mips32_insn.fp0_format.fmt =
411 sdps_format[insn.mm_fp4_format.fmt];
412 mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
413 mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
414 mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
415 mips32_insn.fp0_format.func =
416 insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
417 break;
418 default:
419 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500420 }
421 break;
422 default:
423 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500424 }
425
426 *insn_ptr = mips32_insn;
427 return 0;
428}
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430/*
431 * Redundant with logic already in kernel/branch.c,
432 * embedded in compute_return_epc. At some point,
433 * a single subroutine should be used across both
434 * modules.
435 */
Paul Burton432c6ba2016-07-08 11:06:19 +0100436int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
437 unsigned long *contpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500439 union mips_instruction insn = (union mips_instruction)dec_insn.insn;
440 unsigned int fcr31;
441 unsigned int bit = 0;
Douglas Leung8bcd84a2017-03-13 16:36:37 +0100442 unsigned int bit0;
443 union fpureg *fpr;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500444
445 switch (insn.i_format.opcode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 case spec_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500447 switch (insn.r_format.func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 case jalr_op:
Paul Burtonab4a92e2016-04-21 14:04:55 +0100449 if (insn.r_format.rd != 0) {
450 regs->regs[insn.r_format.rd] =
451 regs->cp0_epc + dec_insn.pc_inc +
452 dec_insn.next_pc_inc;
453 }
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500454 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 case jr_op:
Markos Chandras5f9f41c2014-11-25 15:54:14 +0000456 /* For R6, JR already emulated in jalr_op */
Markos Chandras143fefc2015-06-24 09:52:01 +0100457 if (NO_R6EMU && insn.r_format.func == jr_op)
Markos Chandras5f9f41c2014-11-25 15:54:14 +0000458 break;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500459 *contpc = regs->regs[insn.r_format.rs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return 1;
461 }
462 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 case bcond_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500464 switch (insn.i_format.rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 case bltzal_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 case bltzall_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000467 if (NO_R6EMU && (insn.i_format.rs ||
468 insn.i_format.rt == bltzall_op))
469 break;
470
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500471 regs->regs[31] = regs->cp0_epc +
472 dec_insn.pc_inc +
473 dec_insn.next_pc_inc;
474 /* Fall through */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500475 case bltzl_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000476 if (NO_R6EMU)
477 break;
478 case bltz_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500479 if ((long)regs->regs[insn.i_format.rs] < 0)
480 *contpc = regs->cp0_epc +
481 dec_insn.pc_inc +
482 (insn.i_format.simmediate << 2);
483 else
484 *contpc = regs->cp0_epc +
485 dec_insn.pc_inc +
486 dec_insn.next_pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500488 case bgezal_op:
489 case bgezall_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000490 if (NO_R6EMU && (insn.i_format.rs ||
491 insn.i_format.rt == bgezall_op))
492 break;
493
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500494 regs->regs[31] = regs->cp0_epc +
495 dec_insn.pc_inc +
496 dec_insn.next_pc_inc;
497 /* Fall through */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500498 case bgezl_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000499 if (NO_R6EMU)
500 break;
501 case bgez_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500502 if ((long)regs->regs[insn.i_format.rs] >= 0)
503 *contpc = regs->cp0_epc +
504 dec_insn.pc_inc +
505 (insn.i_format.simmediate << 2);
506 else
507 *contpc = regs->cp0_epc +
508 dec_insn.pc_inc +
509 dec_insn.next_pc_inc;
510 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 case jalx_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500514 set_isa16_mode(bit);
515 case jal_op:
516 regs->regs[31] = regs->cp0_epc +
517 dec_insn.pc_inc +
518 dec_insn.next_pc_inc;
519 /* Fall through */
520 case j_op:
521 *contpc = regs->cp0_epc + dec_insn.pc_inc;
522 *contpc >>= 28;
523 *contpc <<= 28;
524 *contpc |= (insn.j_format.target << 2);
525 /* Set microMIPS mode bit: XOR for jalx. */
526 *contpc ^= bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500528 case beql_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000529 if (NO_R6EMU)
530 break;
531 case beq_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500532 if (regs->regs[insn.i_format.rs] ==
533 regs->regs[insn.i_format.rt])
534 *contpc = regs->cp0_epc +
535 dec_insn.pc_inc +
536 (insn.i_format.simmediate << 2);
537 else
538 *contpc = regs->cp0_epc +
539 dec_insn.pc_inc +
540 dec_insn.next_pc_inc;
541 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500542 case bnel_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000543 if (NO_R6EMU)
544 break;
545 case bne_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500546 if (regs->regs[insn.i_format.rs] !=
547 regs->regs[insn.i_format.rt])
548 *contpc = regs->cp0_epc +
549 dec_insn.pc_inc +
550 (insn.i_format.simmediate << 2);
551 else
552 *contpc = regs->cp0_epc +
553 dec_insn.pc_inc +
554 dec_insn.next_pc_inc;
555 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500556 case blezl_op:
Markos Chandrase9d92d22015-06-24 09:52:00 +0100557 if (!insn.i_format.rt && NO_R6EMU)
Markos Chandras319824e2014-11-25 16:02:23 +0000558 break;
559 case blez_op:
Markos Chandrasa8ff66f2014-11-26 12:57:54 +0000560
561 /*
562 * Compact branches for R6 for the
563 * blez and blezl opcodes.
564 * BLEZ | rs = 0 | rt != 0 == BLEZALC
565 * BLEZ | rs = rt != 0 == BGEZALC
566 * BLEZ | rs != 0 | rt != 0 == BGEUC
567 * BLEZL | rs = 0 | rt != 0 == BLEZC
568 * BLEZL | rs = rt != 0 == BGEZC
569 * BLEZL | rs != 0 | rt != 0 == BGEC
570 *
571 * For real BLEZ{,L}, rt is always 0.
572 */
573 if (cpu_has_mips_r6 && insn.i_format.rt) {
574 if ((insn.i_format.opcode == blez_op) &&
575 ((!insn.i_format.rs && insn.i_format.rt) ||
576 (insn.i_format.rs == insn.i_format.rt)))
577 regs->regs[31] = regs->cp0_epc +
578 dec_insn.pc_inc;
579 *contpc = regs->cp0_epc + dec_insn.pc_inc +
580 dec_insn.next_pc_inc;
581
582 return 1;
583 }
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500584 if ((long)regs->regs[insn.i_format.rs] <= 0)
585 *contpc = regs->cp0_epc +
586 dec_insn.pc_inc +
587 (insn.i_format.simmediate << 2);
588 else
589 *contpc = regs->cp0_epc +
590 dec_insn.pc_inc +
591 dec_insn.next_pc_inc;
592 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500593 case bgtzl_op:
Markos Chandrase9d92d22015-06-24 09:52:00 +0100594 if (!insn.i_format.rt && NO_R6EMU)
Markos Chandras319824e2014-11-25 16:02:23 +0000595 break;
596 case bgtz_op:
Markos Chandrasf1b44062014-11-26 13:05:09 +0000597 /*
598 * Compact branches for R6 for the
599 * bgtz and bgtzl opcodes.
600 * BGTZ | rs = 0 | rt != 0 == BGTZALC
601 * BGTZ | rs = rt != 0 == BLTZALC
602 * BGTZ | rs != 0 | rt != 0 == BLTUC
603 * BGTZL | rs = 0 | rt != 0 == BGTZC
604 * BGTZL | rs = rt != 0 == BLTZC
605 * BGTZL | rs != 0 | rt != 0 == BLTC
606 *
607 * *ZALC varint for BGTZ &&& rt != 0
608 * For real GTZ{,L}, rt is always 0.
609 */
610 if (cpu_has_mips_r6 && insn.i_format.rt) {
611 if ((insn.i_format.opcode == blez_op) &&
612 ((!insn.i_format.rs && insn.i_format.rt) ||
613 (insn.i_format.rs == insn.i_format.rt)))
614 regs->regs[31] = regs->cp0_epc +
615 dec_insn.pc_inc;
616 *contpc = regs->cp0_epc + dec_insn.pc_inc +
617 dec_insn.next_pc_inc;
618
619 return 1;
620 }
621
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500622 if ((long)regs->regs[insn.i_format.rs] > 0)
623 *contpc = regs->cp0_epc +
624 dec_insn.pc_inc +
625 (insn.i_format.simmediate << 2);
626 else
627 *contpc = regs->cp0_epc +
628 dec_insn.pc_inc +
629 dec_insn.next_pc_inc;
630 return 1;
Paul Burton1b492602016-07-04 19:35:08 +0100631 case pop10_op:
632 case pop30_op:
Markos Chandrasc893ce32014-11-26 14:08:52 +0000633 if (!cpu_has_mips_r6)
634 break;
635 if (insn.i_format.rt && !insn.i_format.rs)
636 regs->regs[31] = regs->cp0_epc + 4;
637 *contpc = regs->cp0_epc + dec_insn.pc_inc +
638 dec_insn.next_pc_inc;
639
640 return 1;
David Daneyc26d4212013-08-19 12:10:34 -0700641#ifdef CONFIG_CPU_CAVIUM_OCTEON
642 case lwc2_op: /* This is bbit0 on Octeon */
643 if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 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 ldc2_op: /* This is bbit032 on Octeon */
649 if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
650 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
651 else
652 *contpc = regs->cp0_epc + 8;
653 return 1;
654 case swc2_op: /* This is bbit1 on Octeon */
655 if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
656 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
657 else
658 *contpc = regs->cp0_epc + 8;
659 return 1;
660 case sdc2_op: /* This is bbit132 on Octeon */
661 if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
662 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
663 else
664 *contpc = regs->cp0_epc + 8;
665 return 1;
Markos Chandras8467ca02014-11-26 13:56:51 +0000666#else
667 case bc6_op:
668 /*
669 * Only valid for MIPS R6 but we can still end up
670 * here from a broken userland so just tell emulator
671 * this is not a branch and let it break later on.
672 */
673 if (!cpu_has_mips_r6)
674 break;
675 *contpc = regs->cp0_epc + dec_insn.pc_inc +
676 dec_insn.next_pc_inc;
677
678 return 1;
Markos Chandras84fef632014-11-26 15:43:11 +0000679 case balc6_op:
680 if (!cpu_has_mips_r6)
681 break;
682 regs->regs[31] = regs->cp0_epc + 4;
683 *contpc = regs->cp0_epc + dec_insn.pc_inc +
684 dec_insn.next_pc_inc;
685
686 return 1;
Paul Burton1c66b792016-07-04 19:35:07 +0100687 case pop66_op:
Markos Chandras69b9a2f2014-11-27 09:32:25 +0000688 if (!cpu_has_mips_r6)
689 break;
690 *contpc = regs->cp0_epc + dec_insn.pc_inc +
691 dec_insn.next_pc_inc;
692
693 return 1;
Paul Burton1c66b792016-07-04 19:35:07 +0100694 case pop76_op:
Markos Chandras28d6f932015-01-08 11:55:20 +0000695 if (!cpu_has_mips_r6)
696 break;
697 if (!insn.i_format.rs)
698 regs->regs[31] = regs->cp0_epc + 4;
699 *contpc = regs->cp0_epc + dec_insn.pc_inc +
700 dec_insn.next_pc_inc;
701
702 return 1;
David Daneyc26d4212013-08-19 12:10:34 -0700703#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 case cop0_op:
705 case cop1_op:
Markos Chandrasc8a34582014-11-26 10:10:18 +0000706 /* Need to check for R6 bc1nez and bc1eqz branches */
707 if (cpu_has_mips_r6 &&
708 ((insn.i_format.rs == bc1eqz_op) ||
709 (insn.i_format.rs == bc1nez_op))) {
710 bit = 0;
Douglas Leung8bcd84a2017-03-13 16:36:37 +0100711 fpr = &current->thread.fpu.fpr[insn.i_format.rt];
712 bit0 = get_fpr32(fpr, 0) & 0x1;
Markos Chandrasc8a34582014-11-26 10:10:18 +0000713 switch (insn.i_format.rs) {
714 case bc1eqz_op:
Douglas Leung8bcd84a2017-03-13 16:36:37 +0100715 bit = bit0 == 0;
Markos Chandrasc8a34582014-11-26 10:10:18 +0000716 break;
717 case bc1nez_op:
Douglas Leung8bcd84a2017-03-13 16:36:37 +0100718 bit = bit0 != 0;
Markos Chandrasc8a34582014-11-26 10:10:18 +0000719 break;
720 }
721 if (bit)
722 *contpc = regs->cp0_epc +
723 dec_insn.pc_inc +
724 (insn.i_format.simmediate << 2);
725 else
726 *contpc = regs->cp0_epc +
727 dec_insn.pc_inc +
728 dec_insn.next_pc_inc;
729
730 return 1;
731 }
732 /* R2/R6 compatible cop1 instruction. Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 case cop2_op:
734 case cop1x_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500735 if (insn.i_format.rs == bc_op) {
736 preempt_disable();
737 if (is_fpu_owner())
Manuel Lauss842dfc12014-11-07 14:13:54 +0100738 fcr31 = read_32bit_cp1_register(CP1_STATUS);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500739 else
740 fcr31 = current->thread.fpu.fcr31;
741 preempt_enable();
742
743 bit = (insn.i_format.rt >> 2);
744 bit += (bit != 0);
745 bit += 23;
746 switch (insn.i_format.rt & 3) {
747 case 0: /* bc1f */
748 case 2: /* bc1fl */
749 if (~fcr31 & (1 << bit))
750 *contpc = regs->cp0_epc +
751 dec_insn.pc_inc +
752 (insn.i_format.simmediate << 2);
753 else
754 *contpc = regs->cp0_epc +
755 dec_insn.pc_inc +
756 dec_insn.next_pc_inc;
757 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500758 case 1: /* bc1t */
759 case 3: /* bc1tl */
760 if (fcr31 & (1 << bit))
761 *contpc = regs->cp0_epc +
762 dec_insn.pc_inc +
763 (insn.i_format.simmediate << 2);
764 else
765 *contpc = regs->cp0_epc +
766 dec_insn.pc_inc +
767 dec_insn.next_pc_inc;
768 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500769 }
770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 break;
772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return 0;
774}
775
776/*
777 * In the Linux kernel, we support selection of FPR format on the
Ralf Baechle70342282013-01-22 12:59:30 +0100778 * basis of the Status.FR bit. If an FPU is not present, the FR bit
David Daneyda0bac32009-11-02 11:33:46 -0800779 * is hardwired to zero, which would imply a 32-bit FPU even for
Paul Burton597ce172013-11-22 13:12:07 +0000780 * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
Ralf Baechle51d943f2012-08-15 19:42:19 +0200781 * FPU emu is slow and bulky and optimizing this function offers fairly
782 * sizeable benefits so we try to be clever and make this function return
783 * a constant whenever possible, that is on 64-bit kernels without O32
Paul Burton597ce172013-11-22 13:12:07 +0000784 * compatibility enabled and on 32-bit without 64-bit FPU support.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 */
David Daneyda0bac32009-11-02 11:33:46 -0800786static inline int cop1_64bit(struct pt_regs *xcp)
787{
Masahiro Yamada97f26452016-08-03 13:45:50 -0700788 if (IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_MIPS32_O32))
Ralf Baechle08a07902014-04-19 13:11:37 +0200789 return 1;
Masahiro Yamada97f26452016-08-03 13:45:50 -0700790 else if (IS_ENABLED(CONFIG_32BIT) &&
791 !IS_ENABLED(CONFIG_MIPS_O32_FP64_SUPPORT))
Ralf Baechle08a07902014-04-19 13:11:37 +0200792 return 0;
793
Paul Burton597ce172013-11-22 13:12:07 +0000794 return !test_thread_flag(TIF_32BIT_FPREGS);
David Daneyda0bac32009-11-02 11:33:46 -0800795}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Paul Burton4227a2d2014-09-11 08:30:20 +0100797static inline bool hybrid_fprs(void)
798{
799 return test_thread_flag(TIF_HYBRID_FPREGS);
800}
801
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200802#define SIFROMREG(si, x) \
803do { \
Paul Burton4227a2d2014-09-11 08:30:20 +0100804 if (cop1_64bit(xcp) && !hybrid_fprs()) \
Paul Burtonc8c0da62014-09-24 10:45:37 +0100805 (si) = (int)get_fpr32(&ctx->fpr[x], 0); \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000806 else \
Paul Burtonc8c0da62014-09-24 10:45:37 +0100807 (si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000808} while (0)
David Daneyda0bac32009-11-02 11:33:46 -0800809
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200810#define SITOREG(si, x) \
811do { \
Paul Burton4227a2d2014-09-11 08:30:20 +0100812 if (cop1_64bit(xcp) && !hybrid_fprs()) { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000813 unsigned i; \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000814 set_fpr32(&ctx->fpr[x], 0, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000815 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
816 set_fpr32(&ctx->fpr[x], i, 0); \
817 } else { \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000818 set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000819 } \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000820} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Paul Burtonc8c0da62014-09-24 10:45:37 +0100822#define SIFROMHREG(si, x) ((si) = (int)get_fpr32(&ctx->fpr[x], 1))
Paul Burtonef1c47a2014-01-27 17:14:47 +0000823
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200824#define SITOHREG(si, x) \
825do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000826 unsigned i; \
827 set_fpr32(&ctx->fpr[x], 1, si); \
828 for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
829 set_fpr32(&ctx->fpr[x], i, 0); \
830} while (0)
Leonid Yegoshin1ac944002013-11-07 12:48:28 +0000831
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200832#define DIFROMREG(di, x) \
Manuel Lauss8535f2b2017-08-14 12:21:48 +0200833 ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) ^ 1)], 0))
Paul Burtonbbd426f2014-02-13 11:26:41 +0000834
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200835#define DITOREG(di, x) \
836do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000837 unsigned fpr, i; \
Manuel Lauss8535f2b2017-08-14 12:21:48 +0200838 fpr = (x) & ~(cop1_64bit(xcp) ^ 1); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000839 set_fpr64(&ctx->fpr[fpr], 0, di); \
840 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
841 set_fpr64(&ctx->fpr[fpr], i, 0); \
842} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Ralf Baechle21a151d2007-10-11 23:46:15 +0100844#define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
845#define SPTOREG(sp, x) SITOREG((sp).bits, x)
846#define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
847#define DPTOREG(dp, x) DITOREG((dp).bits, x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849/*
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100850 * Emulate a CFC1 instruction.
851 */
852static inline void cop1_cfc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
853 mips_instruction ir)
854{
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100855 u32 fcr31 = ctx->fcr31;
856 u32 value = 0;
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100857
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100858 switch (MIPSInst_RD(ir)) {
859 case FPCREG_CSR:
860 value = fcr31;
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100861 pr_debug("%p gpr[%d]<-csr=%08x\n",
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100862 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
863 break;
864
865 case FPCREG_FENR:
866 if (!cpu_has_mips_r)
867 break;
868 value = (fcr31 >> (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
869 MIPS_FENR_FS;
870 value |= fcr31 & (FPU_CSR_ALL_E | FPU_CSR_RM);
871 pr_debug("%p gpr[%d]<-enr=%08x\n",
872 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
873 break;
874
875 case FPCREG_FEXR:
876 if (!cpu_has_mips_r)
877 break;
878 value = fcr31 & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
879 pr_debug("%p gpr[%d]<-exr=%08x\n",
880 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
881 break;
882
883 case FPCREG_FCCR:
884 if (!cpu_has_mips_r)
885 break;
886 value = (fcr31 >> (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
887 MIPS_FCCR_COND0;
888 value |= (fcr31 >> (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
889 (MIPS_FCCR_CONDX & ~MIPS_FCCR_COND0);
890 pr_debug("%p gpr[%d]<-ccr=%08x\n",
891 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
892 break;
893
894 case FPCREG_RID:
Maciej W. Rozycki03dce592015-05-12 15:20:57 +0100895 value = boot_cpu_data.fpu_id;
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100896 break;
897
898 default:
899 break;
900 }
901
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100902 if (MIPSInst_RT(ir))
903 xcp->regs[MIPSInst_RT(ir)] = value;
904}
905
906/*
907 * Emulate a CTC1 instruction.
908 */
909static inline void cop1_ctc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
910 mips_instruction ir)
911{
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100912 u32 fcr31 = ctx->fcr31;
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100913 u32 value;
Maciej W. Rozycki9b266162015-04-03 23:27:48 +0100914 u32 mask;
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100915
916 if (MIPSInst_RT(ir) == 0)
917 value = 0;
918 else
919 value = xcp->regs[MIPSInst_RT(ir)];
920
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100921 switch (MIPSInst_RD(ir)) {
922 case FPCREG_CSR:
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100923 pr_debug("%p gpr[%d]->csr=%08x\n",
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100924 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100925
Maciej W. Rozycki9b266162015-04-03 23:27:48 +0100926 /* Preserve read-only bits. */
Maciej W. Rozycki03dce592015-05-12 15:20:57 +0100927 mask = boot_cpu_data.fpu_msk31;
Maciej W. Rozycki9b266162015-04-03 23:27:48 +0100928 fcr31 = (value & ~mask) | (fcr31 & mask);
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100929 break;
930
931 case FPCREG_FENR:
932 if (!cpu_has_mips_r)
933 break;
934 pr_debug("%p gpr[%d]->enr=%08x\n",
935 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
936 fcr31 &= ~(FPU_CSR_FS | FPU_CSR_ALL_E | FPU_CSR_RM);
937 fcr31 |= (value << (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
938 FPU_CSR_FS;
939 fcr31 |= value & (FPU_CSR_ALL_E | FPU_CSR_RM);
940 break;
941
942 case FPCREG_FEXR:
943 if (!cpu_has_mips_r)
944 break;
945 pr_debug("%p gpr[%d]->exr=%08x\n",
946 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
947 fcr31 &= ~(FPU_CSR_ALL_X | FPU_CSR_ALL_S);
948 fcr31 |= value & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
949 break;
950
951 case FPCREG_FCCR:
952 if (!cpu_has_mips_r)
953 break;
954 pr_debug("%p gpr[%d]->ccr=%08x\n",
955 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
956 fcr31 &= ~(FPU_CSR_CONDX | FPU_CSR_COND);
957 fcr31 |= (value << (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
958 FPU_CSR_COND;
959 fcr31 |= (value << (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
960 FPU_CSR_CONDX;
961 break;
962
963 default:
964 break;
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100965 }
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100966
967 ctx->fcr31 = fcr31;
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100968}
969
970/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 * Emulate the single floating point instruction pointed at by EPC.
972 * Two instructions if the instruction is in a branch delay slot.
973 */
974
David Daney515b0292010-10-21 16:32:26 -0700975static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Paul Burton445a58c2017-08-23 11:17:51 -0700976 struct mm_decoded_insn dec_insn, void __user **fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500978 unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
Paul Burton93583e12016-04-21 14:04:45 +0100979 unsigned int cond, cbit, bit0;
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200980 mips_instruction ir;
981 int likely, pc_inc;
Paul Burton93583e12016-04-21 14:04:45 +0100982 union fpureg *fpr;
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200983 u32 __user *wva;
984 u64 __user *dva;
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200985 u32 wval;
986 u64 dval;
987 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Ralf Baechle70e4c232014-04-30 11:09:44 +0200989 /*
990 * These are giving gcc a gentle hint about what to expect in
991 * dec_inst in order to do better optimization.
992 */
993 if (!cpu_has_mmips && dec_insn.micro_mips_mode)
994 unreachable();
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 /* XXX NEC Vr54xx bug workaround */
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200997 if (delay_slot(xcp)) {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500998 if (dec_insn.micro_mips_mode) {
999 if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001000 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001001 } else {
1002 if (!isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001003 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001004 }
1005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001007 if (delay_slot(xcp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /*
1009 * The instruction to be emulated is in a branch delay slot
Ralf Baechle70342282013-01-22 12:59:30 +01001010 * which means that we have to emulate the branch instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 * BEFORE we do the cop1 instruction.
1012 *
1013 * This branch could be a COP1 branch, but in that case we
1014 * would have had a trap for that instruction, and would not
1015 * come through this route.
1016 *
1017 * Linux MIPS branch emulator operates on context, updating the
1018 * cp0_epc.
1019 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001020 ir = dec_insn.next_insn; /* process delay slot instr */
1021 pc_inc = dec_insn.next_pc_inc;
Ralf Baechle333d1f62005-02-28 17:55:57 +00001022 } else {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001023 ir = dec_insn.insn; /* process current instr */
1024 pc_inc = dec_insn.pc_inc;
1025 }
1026
1027 /*
1028 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
1029 * instructions, we want to convert microMIPS FPU instructions
1030 * into MIPS32 instructions so that we could reuse all of the
1031 * FPU emulation code.
1032 *
1033 * NOTE: We cannot do this for branch instructions since they
1034 * are not a subset. Example: Cannot emulate a 16-bit
1035 * aligned target address with a MIPS32 instruction.
1036 */
1037 if (dec_insn.micro_mips_mode) {
1038 /*
1039 * If next instruction is a 16-bit instruction, then it
1040 * it cannot be a FPU instruction. This could happen
1041 * since we can be called for non-FPU instructions.
1042 */
1043 if ((pc_inc == 2) ||
1044 (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
1045 == SIGILL))
1046 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001049emul:
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001050 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
David Daneyb6ee75e2009-11-05 11:34:26 -08001051 MIPS_FPU_EMU_INC_STATS(emulated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 switch (MIPSInst_OPCODE(ir)) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001053 case ldc1_op:
1054 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1055 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -08001056 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001057
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001058 if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001059 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001060 *fault_addr = dva;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return SIGBUS;
1062 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001063 if (__get_user(dval, dva)) {
David Daney515b0292010-10-21 16:32:26 -07001064 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001065 *fault_addr = dva;
David Daney515b0292010-10-21 16:32:26 -07001066 return SIGSEGV;
1067 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001068 DITOREG(dval, MIPSInst_RT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001071 case sdc1_op:
1072 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1073 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -08001074 MIPS_FPU_EMU_INC_STATS(stores);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001075 DIFROMREG(dval, MIPSInst_RT(ir));
1076 if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001077 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001078 *fault_addr = dva;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return SIGBUS;
1080 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001081 if (__put_user(dval, dva)) {
David Daney515b0292010-10-21 16:32:26 -07001082 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001083 *fault_addr = dva;
David Daney515b0292010-10-21 16:32:26 -07001084 return SIGSEGV;
1085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001088 case lwc1_op:
1089 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1090 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -08001091 MIPS_FPU_EMU_INC_STATS(loads);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001092 if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001093 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001094 *fault_addr = wva;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return SIGBUS;
1096 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001097 if (__get_user(wval, wva)) {
David Daney515b0292010-10-21 16:32:26 -07001098 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001099 *fault_addr = wva;
David Daney515b0292010-10-21 16:32:26 -07001100 return SIGSEGV;
1101 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001102 SITOREG(wval, MIPSInst_RT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001105 case swc1_op:
1106 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1107 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -08001108 MIPS_FPU_EMU_INC_STATS(stores);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001109 SIFROMREG(wval, MIPSInst_RT(ir));
1110 if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001111 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001112 *fault_addr = wva;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return SIGBUS;
1114 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001115 if (__put_user(wval, wva)) {
David Daney515b0292010-10-21 16:32:26 -07001116 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001117 *fault_addr = wva;
David Daney515b0292010-10-21 16:32:26 -07001118 return SIGSEGV;
1119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 case cop1_op:
1123 switch (MIPSInst_RS(ir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 case dmfc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001125 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1126 return SIGILL;
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 /* copregister fs -> gpr[rt] */
1129 if (MIPSInst_RT(ir) != 0) {
1130 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1131 MIPSInst_RD(ir));
1132 }
1133 break;
1134
1135 case dmtc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001136 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1137 return SIGILL;
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 /* copregister fs <- rt */
1140 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1141 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001143 case mfhc_op:
Markos Chandrase8f80cc2015-07-17 10:36:03 +01001144 if (!cpu_has_mips_r2_r6)
Maciej W. Rozycki70f743d2017-06-16 00:16:15 +01001145 return SIGILL;
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001146
1147 /* copregister rd -> gpr[rt] */
1148 if (MIPSInst_RT(ir) != 0) {
1149 SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
1150 MIPSInst_RD(ir));
1151 }
1152 break;
1153
1154 case mthc_op:
Markos Chandrase8f80cc2015-07-17 10:36:03 +01001155 if (!cpu_has_mips_r2_r6)
Maciej W. Rozycki70f743d2017-06-16 00:16:15 +01001156 return SIGILL;
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001157
1158 /* copregister rd <- gpr[rt] */
1159 SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1160 break;
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 case mfc_op:
1163 /* copregister rd -> gpr[rt] */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (MIPSInst_RT(ir) != 0) {
1165 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1166 MIPSInst_RD(ir));
1167 }
1168 break;
1169
1170 case mtc_op:
1171 /* copregister rd <- rt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1173 break;
1174
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001175 case cfc_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 /* cop control register rd -> gpr[rt] */
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +01001177 cop1_cfc(xcp, ctx, ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001180 case ctc_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /* copregister rd <- rt */
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +01001182 cop1_ctc(xcp, ctx, ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1184 return SIGFPE;
1185 }
1186 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Markos Chandrasc909ca72015-07-17 10:38:32 +01001188 case bc1eqz_op:
1189 case bc1nez_op:
1190 if (!cpu_has_mips_r6 || delay_slot(xcp))
1191 return SIGILL;
1192
1193 cond = likely = 0;
Paul Burton93583e12016-04-21 14:04:45 +01001194 fpr = &current->thread.fpu.fpr[MIPSInst_RT(ir)];
1195 bit0 = get_fpr32(fpr, 0) & 0x1;
Markos Chandrasc909ca72015-07-17 10:38:32 +01001196 switch (MIPSInst_RS(ir)) {
1197 case bc1eqz_op:
Paul Burton93583e12016-04-21 14:04:45 +01001198 cond = bit0 == 0;
Markos Chandrasc909ca72015-07-17 10:38:32 +01001199 break;
1200 case bc1nez_op:
Paul Burton93583e12016-04-21 14:04:45 +01001201 cond = bit0 != 0;
Markos Chandrasc909ca72015-07-17 10:38:32 +01001202 break;
1203 }
1204 goto branch_common;
1205
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001206 case bc_op:
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001207 if (delay_slot(xcp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return SIGILL;
1209
Ralf Baechle08a07902014-04-19 13:11:37 +02001210 if (cpu_has_mips_4_5_r)
1211 cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
1212 else
1213 cbit = FPU_CSR_COND;
1214 cond = ctx->fcr31 & cbit;
1215
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001216 likely = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 switch (MIPSInst_RT(ir) & 3) {
1218 case bcfl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001219 if (cpu_has_mips_2_3_4_5_r)
1220 likely = 1;
1221 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 case bcf_op:
1223 cond = !cond;
1224 break;
1225 case bctl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001226 if (cpu_has_mips_2_3_4_5_r)
1227 likely = 1;
1228 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 case bct_op:
1230 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
Markos Chandrasc909ca72015-07-17 10:38:32 +01001232branch_common:
Aleksandar Markovicae5f3f52017-08-21 14:24:50 +02001233 MIPS_FPU_EMU_INC_STATS(branches);
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001234 set_delay_slot(xcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (cond) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001236 /*
1237 * Branch taken: emulate dslot instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 */
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001239 unsigned long bcpc;
1240
1241 /*
1242 * Remember EPC at the branch to point back
1243 * at so that any delay-slot instruction
1244 * signal is not silently ignored.
1245 */
1246 bcpc = xcp->cp0_epc;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001247 xcp->cp0_epc += dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001249 contpc = MIPSInst_SIMM(ir);
1250 ir = dec_insn.next_insn;
1251 if (dec_insn.micro_mips_mode) {
1252 contpc = (xcp->cp0_epc + (contpc << 1));
1253
1254 /* If 16-bit instruction, not FPU. */
1255 if ((dec_insn.next_pc_inc == 2) ||
1256 (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1257
1258 /*
1259 * Since this instruction will
1260 * be put on the stack with
1261 * 32-bit words, get around
1262 * this problem by putting a
1263 * NOP16 as the second one.
1264 */
1265 if (dec_insn.next_pc_inc == 2)
1266 ir = (ir & (~0xffff)) | MM_NOP16;
1267
1268 /*
1269 * Single step the non-CP1
1270 * instruction in the dslot.
1271 */
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001272 sig = mips_dsemul(xcp, ir,
Paul Burton432c6ba2016-07-08 11:06:19 +01001273 bcpc, contpc);
Maciej W. Rozyckie4553572016-01-22 05:20:26 +00001274 if (sig < 0)
1275 break;
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001276 if (sig)
1277 xcp->cp0_epc = bcpc;
1278 /*
1279 * SIGILL forces out of
1280 * the emulation loop.
1281 */
1282 return sig ? sig : SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001283 }
1284 } else
1285 contpc = (xcp->cp0_epc + (contpc << 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 switch (MIPSInst_OPCODE(ir)) {
1288 case lwc1_op:
1289 case swc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001290 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 case ldc1_op:
1293 case sdc1_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001294 if (cpu_has_mips_2_3_4_5_r)
Ralf Baechle08a07902014-04-19 13:11:37 +02001295 goto emul;
1296
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001297 goto bc_sigill;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001298
Ralf Baechle08a07902014-04-19 13:11:37 +02001299 case cop1_op:
1300 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001301
Ralf Baechle08a07902014-04-19 13:11:37 +02001302 case cop1x_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001303 if (cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001304 /* its one of ours */
1305 goto emul;
1306
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001307 goto bc_sigill;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 case spec_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001310 switch (MIPSInst_FUNC(ir)) {
1311 case movc_op:
1312 if (cpu_has_mips_4_5_r)
1313 goto emul;
Ralf Baechle08a07902014-04-19 13:11:37 +02001314
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001315 goto bc_sigill;
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 break;
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001318
1319 bc_sigill:
1320 xcp->cp0_epc = bcpc;
1321 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
1323
1324 /*
1325 * Single step the non-cp1
1326 * instruction in the dslot
1327 */
Paul Burton432c6ba2016-07-08 11:06:19 +01001328 sig = mips_dsemul(xcp, ir, bcpc, contpc);
Maciej W. Rozyckie4553572016-01-22 05:20:26 +00001329 if (sig < 0)
1330 break;
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001331 if (sig)
1332 xcp->cp0_epc = bcpc;
1333 /* SIGILL forces out of the emulation loop. */
1334 return sig ? sig : SIGILL;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001335 } else if (likely) { /* branch not taken */
Maciej W. Rozycki5d77cf22015-04-03 23:24:24 +01001336 /*
1337 * branch likely nullifies
1338 * dslot if not taken
1339 */
1340 xcp->cp0_epc += dec_insn.pc_inc;
1341 contpc += dec_insn.pc_inc;
1342 /*
1343 * else continue & execute
1344 * dslot as normal insn
1345 */
1346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 default:
1350 if (!(MIPSInst_RS(ir) & 0x10))
1351 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001353 /* a real fpu computation instruction */
1354 if ((sig = fpu_emu(xcp, ctx, ir)))
1355 return sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
1357 break;
1358
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001359 case cop1x_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001360 if (!cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001361 return SIGILL;
1362
1363 sig = fpux_emu(xcp, ctx, ir, fault_addr);
David Daney515b0292010-10-21 16:32:26 -07001364 if (sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return sig;
1366 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 case spec_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001369 if (!cpu_has_mips_4_5_r)
1370 return SIGILL;
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (MIPSInst_FUNC(ir) != movc_op)
1373 return SIGILL;
1374 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
1375 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
1376 xcp->regs[MIPSInst_RD(ir)] =
1377 xcp->regs[MIPSInst_RS(ir)];
1378 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 default:
1380 return SIGILL;
1381 }
1382
1383 /* we did it !! */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +09001384 xcp->cp0_epc = contpc;
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001385 clear_delay_slot(xcp);
Ralf Baechle333d1f62005-02-28 17:55:57 +00001386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return 0;
1388}
1389
1390/*
1391 * Conversion table from MIPS compare ops 48-63
1392 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
1393 */
1394static const unsigned char cmptab[8] = {
1395 0, /* cmp_0 (sig) cmp_sf */
1396 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
1397 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
1398 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
1399 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
1400 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
1401 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
1402 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
1403};
1404
Markos Chandrasf8c3c672015-08-13 09:56:28 +02001405static const unsigned char negative_cmptab[8] = {
1406 0, /* Reserved */
1407 IEEE754_CLT | IEEE754_CGT | IEEE754_CEQ,
1408 IEEE754_CLT | IEEE754_CGT | IEEE754_CUN,
1409 IEEE754_CLT | IEEE754_CGT,
1410 /* Reserved */
1411};
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414/*
1415 * Additional MIPS4 instructions
1416 */
1417
Ralf Baechle47fa0c02014-04-16 11:00:12 +02001418#define DEF3OP(name, p, f1, f2, f3) \
1419static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \
1420 union ieee754##p s, union ieee754##p t) \
1421{ \
1422 struct _ieee754_csr ieee754_csr_save; \
1423 s = f1(s, t); \
1424 ieee754_csr_save = ieee754_csr; \
1425 s = f2(s, r); \
1426 ieee754_csr_save.cx |= ieee754_csr.cx; \
1427 ieee754_csr_save.sx |= ieee754_csr.sx; \
1428 s = f3(s); \
1429 ieee754_csr.cx |= ieee754_csr_save.cx; \
1430 ieee754_csr.sx |= ieee754_csr_save.sx; \
1431 return s; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
1433
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001434static union ieee754dp fpemu_dp_recip(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
1436 return ieee754dp_div(ieee754dp_one(0), d);
1437}
1438
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001439static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
1442}
1443
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001444static union ieee754sp fpemu_sp_recip(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 return ieee754sp_div(ieee754sp_one(0), s);
1447}
1448
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001449static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
1452}
1453
Ralf Baechle21a151d2007-10-11 23:46:15 +01001454DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
1455DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
1457DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
Ralf Baechle21a151d2007-10-11 23:46:15 +01001458DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
1459DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
1461DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
1462
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001463static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Paul Burton445a58c2017-08-23 11:17:51 -07001464 mips_instruction ir, void __user **fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 unsigned rcsr = 0; /* resulting csr */
1467
David Daneyb6ee75e2009-11-05 11:34:26 -08001468 MIPS_FPU_EMU_INC_STATS(cp1xops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 switch (MIPSInst_FMA_FFMT(ir)) {
1471 case s_fmt:{ /* 0 */
1472
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001473 union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
1474 union ieee754sp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001475 u32 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 u32 val;
1477
1478 switch (MIPSInst_FUNC(ir)) {
1479 case lwxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001480 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 xcp->regs[MIPSInst_FT(ir)]);
1482
David Daneyb6ee75e2009-11-05 11:34:26 -08001483 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001484 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001485 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001486 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return SIGBUS;
1488 }
David Daney515b0292010-10-21 16:32:26 -07001489 if (__get_user(val, va)) {
1490 MIPS_FPU_EMU_INC_STATS(errors);
1491 *fault_addr = va;
1492 return SIGSEGV;
1493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 SITOREG(val, MIPSInst_FD(ir));
1495 break;
1496
1497 case swxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001498 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 xcp->regs[MIPSInst_FT(ir)]);
1500
David Daneyb6ee75e2009-11-05 11:34:26 -08001501 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 SIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001504 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1505 MIPS_FPU_EMU_INC_STATS(errors);
1506 *fault_addr = va;
1507 return SIGBUS;
1508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (put_user(val, va)) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001510 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001511 *fault_addr = va;
1512 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
1514 break;
1515
1516 case madd_s_op:
1517 handler = fpemu_sp_madd;
1518 goto scoptop;
1519 case msub_s_op:
1520 handler = fpemu_sp_msub;
1521 goto scoptop;
1522 case nmadd_s_op:
1523 handler = fpemu_sp_nmadd;
1524 goto scoptop;
1525 case nmsub_s_op:
1526 handler = fpemu_sp_nmsub;
1527 goto scoptop;
1528
1529 scoptop:
1530 SPFROMREG(fr, MIPSInst_FR(ir));
1531 SPFROMREG(fs, MIPSInst_FS(ir));
1532 SPFROMREG(ft, MIPSInst_FT(ir));
1533 fd = (*handler) (fr, fs, ft);
1534 SPTOREG(fd, MIPSInst_FD(ir));
1535
1536 copcsr:
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001537 if (ieee754_cxtest(IEEE754_INEXACT)) {
1538 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001540 }
1541 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1542 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001544 }
1545 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1546 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001548 }
1549 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1550 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001556 /*printk ("SIGFPE: FPU csr = %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 ctx->fcr31); */
1558 return SIGFPE;
1559 }
1560
1561 break;
1562
1563 default:
1564 return SIGILL;
1565 }
1566 break;
1567 }
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 case d_fmt:{ /* 1 */
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001570 union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
1571 union ieee754dp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001572 u64 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 u64 val;
1574
1575 switch (MIPSInst_FUNC(ir)) {
1576 case ldxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001577 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 xcp->regs[MIPSInst_FT(ir)]);
1579
David Daneyb6ee75e2009-11-05 11:34:26 -08001580 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001581 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001582 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001583 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 return SIGBUS;
1585 }
David Daney515b0292010-10-21 16:32:26 -07001586 if (__get_user(val, va)) {
1587 MIPS_FPU_EMU_INC_STATS(errors);
1588 *fault_addr = va;
1589 return SIGSEGV;
1590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 DITOREG(val, MIPSInst_FD(ir));
1592 break;
1593
1594 case sdxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001595 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 xcp->regs[MIPSInst_FT(ir)]);
1597
David Daneyb6ee75e2009-11-05 11:34:26 -08001598 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 DIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001600 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001601 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001602 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return SIGBUS;
1604 }
David Daney515b0292010-10-21 16:32:26 -07001605 if (__put_user(val, va)) {
1606 MIPS_FPU_EMU_INC_STATS(errors);
1607 *fault_addr = va;
1608 return SIGSEGV;
1609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 break;
1611
1612 case madd_d_op:
1613 handler = fpemu_dp_madd;
1614 goto dcoptop;
1615 case msub_d_op:
1616 handler = fpemu_dp_msub;
1617 goto dcoptop;
1618 case nmadd_d_op:
1619 handler = fpemu_dp_nmadd;
1620 goto dcoptop;
1621 case nmsub_d_op:
1622 handler = fpemu_dp_nmsub;
1623 goto dcoptop;
1624
1625 dcoptop:
1626 DPFROMREG(fr, MIPSInst_FR(ir));
1627 DPFROMREG(fs, MIPSInst_FS(ir));
1628 DPFROMREG(ft, MIPSInst_FT(ir));
1629 fd = (*handler) (fr, fs, ft);
1630 DPTOREG(fd, MIPSInst_FD(ir));
1631 goto copcsr;
1632
1633 default:
1634 return SIGILL;
1635 }
1636 break;
1637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001639 case 0x3:
1640 if (MIPSInst_FUNC(ir) != pfetch_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return SIGILL;
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 /* ignore prefx operation */
1644 break;
1645
1646 default:
1647 return SIGILL;
1648 }
1649
1650 return 0;
1651}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653
1654
1655/*
1656 * Emulate a single COP1 arithmetic instruction.
1657 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001658static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 mips_instruction ir)
1660{
1661 int rfmt; /* resulting format */
1662 unsigned rcsr = 0; /* resulting csr */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001663 unsigned int oldrm;
1664 unsigned int cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 unsigned cond;
1666 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001667 union ieee754dp d;
1668 union ieee754sp s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 int w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 s64 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 } rv; /* resulting value */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001672 u64 bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
David Daneyb6ee75e2009-11-05 11:34:26 -08001674 MIPS_FPU_EMU_INC_STATS(cp1ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001676 case s_fmt: { /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001678 union ieee754sp(*b) (union ieee754sp, union ieee754sp);
1679 union ieee754sp(*u) (union ieee754sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 } handler;
Paul Burton4b820d92016-04-21 14:04:48 +01001681 union ieee754sp fd, fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 switch (MIPSInst_FUNC(ir)) {
1684 /* binary ops */
1685 case fadd_op:
1686 handler.b = ieee754sp_add;
1687 goto scopbop;
1688 case fsub_op:
1689 handler.b = ieee754sp_sub;
1690 goto scopbop;
1691 case fmul_op:
1692 handler.b = ieee754sp_mul;
1693 goto scopbop;
1694 case fdiv_op:
1695 handler.b = ieee754sp_div;
1696 goto scopbop;
1697
1698 /* unary ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 case fsqrt_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001700 if (!cpu_has_mips_2_3_4_5_r)
Ralf Baechle08a07902014-04-19 13:11:37 +02001701 return SIGILL;
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 handler.u = ieee754sp_sqrt;
1704 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001705
Ralf Baechle08a07902014-04-19 13:11:37 +02001706 /*
1707 * Note that on some MIPS IV implementations such as the
1708 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1709 * achieve full IEEE-754 accuracy - however this emulator does.
1710 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 case frsqrt_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001712 if (!cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001713 return SIGILL;
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 handler.u = fpemu_sp_rsqrt;
1716 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 case frecip_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001719 if (!cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001720 return SIGILL;
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 handler.u = fpemu_sp_recip;
1723 goto scopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02001724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001726 if (!cpu_has_mips_4_5_r)
1727 return SIGILL;
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1730 if (((ctx->fcr31 & cond) != 0) !=
1731 ((MIPSInst_FT(ir) & 1) != 0))
1732 return 0;
1733 SPFROMREG(rv.s, MIPSInst_FS(ir));
1734 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 case fmovz_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001737 if (!cpu_has_mips_4_5_r)
1738 return SIGILL;
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1741 return 0;
1742 SPFROMREG(rv.s, MIPSInst_FS(ir));
1743 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 case fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001746 if (!cpu_has_mips_4_5_r)
1747 return SIGILL;
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1750 return 0;
1751 SPFROMREG(rv.s, MIPSInst_FS(ir));
1752 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001753
Markos Chandras67613f02015-08-13 09:56:29 +02001754 case fseleqz_op:
1755 if (!cpu_has_mips_r6)
1756 return SIGILL;
1757
1758 SPFROMREG(rv.s, MIPSInst_FT(ir));
1759 if (rv.w & 0x1)
1760 rv.w = 0;
1761 else
1762 SPFROMREG(rv.s, MIPSInst_FS(ir));
1763 break;
1764
Markos Chandras130fe352015-08-13 09:56:30 +02001765 case fselnez_op:
1766 if (!cpu_has_mips_r6)
1767 return SIGILL;
1768
1769 SPFROMREG(rv.s, MIPSInst_FT(ir));
1770 if (rv.w & 0x1)
1771 SPFROMREG(rv.s, MIPSInst_FS(ir));
1772 else
1773 rv.w = 0;
1774 break;
1775
Markos Chandrase24c3be2015-08-13 09:56:31 +02001776 case fmaddf_op: {
1777 union ieee754sp ft, fs, fd;
1778
1779 if (!cpu_has_mips_r6)
1780 return SIGILL;
1781
1782 SPFROMREG(ft, MIPSInst_FT(ir));
1783 SPFROMREG(fs, MIPSInst_FS(ir));
1784 SPFROMREG(fd, MIPSInst_FD(ir));
1785 rv.s = ieee754sp_maddf(fd, fs, ft);
1786 break;
1787 }
1788
Markos Chandras83d43302015-08-13 09:56:32 +02001789 case fmsubf_op: {
1790 union ieee754sp ft, fs, fd;
1791
1792 if (!cpu_has_mips_r6)
1793 return SIGILL;
1794
1795 SPFROMREG(ft, MIPSInst_FT(ir));
1796 SPFROMREG(fs, MIPSInst_FS(ir));
1797 SPFROMREG(fd, MIPSInst_FD(ir));
1798 rv.s = ieee754sp_msubf(fd, fs, ft);
1799 break;
1800 }
1801
Markos Chandras400bd2e2015-08-13 09:56:33 +02001802 case frint_op: {
1803 union ieee754sp fs;
1804
1805 if (!cpu_has_mips_r6)
1806 return SIGILL;
1807
1808 SPFROMREG(fs, MIPSInst_FS(ir));
Aleksandar Markovic3ec404d2017-08-21 14:24:48 +02001809 rv.s = ieee754sp_rint(fs);
Markos Chandras400bd2e2015-08-13 09:56:33 +02001810 goto copcsr;
1811 }
1812
Markos Chandras38db37b2015-08-13 09:56:34 +02001813 case fclass_op: {
1814 union ieee754sp fs;
1815
1816 if (!cpu_has_mips_r6)
1817 return SIGILL;
1818
1819 SPFROMREG(fs, MIPSInst_FS(ir));
1820 rv.w = ieee754sp_2008class(fs);
1821 rfmt = w_fmt;
1822 break;
1823 }
1824
Markos Chandras4e9561b2015-08-13 09:56:35 +02001825 case fmin_op: {
1826 union ieee754sp fs, ft;
1827
1828 if (!cpu_has_mips_r6)
1829 return SIGILL;
1830
1831 SPFROMREG(ft, MIPSInst_FT(ir));
1832 SPFROMREG(fs, MIPSInst_FS(ir));
1833 rv.s = ieee754sp_fmin(fs, ft);
1834 break;
1835 }
1836
1837 case fmina_op: {
1838 union ieee754sp fs, ft;
1839
1840 if (!cpu_has_mips_r6)
1841 return SIGILL;
1842
1843 SPFROMREG(ft, MIPSInst_FT(ir));
1844 SPFROMREG(fs, MIPSInst_FS(ir));
1845 rv.s = ieee754sp_fmina(fs, ft);
1846 break;
1847 }
1848
Markos Chandrasa79f5f92015-08-13 09:56:36 +02001849 case fmax_op: {
1850 union ieee754sp fs, ft;
1851
1852 if (!cpu_has_mips_r6)
1853 return SIGILL;
1854
1855 SPFROMREG(ft, MIPSInst_FT(ir));
1856 SPFROMREG(fs, MIPSInst_FS(ir));
1857 rv.s = ieee754sp_fmax(fs, ft);
1858 break;
1859 }
1860
1861 case fmaxa_op: {
1862 union ieee754sp fs, ft;
1863
1864 if (!cpu_has_mips_r6)
1865 return SIGILL;
1866
1867 SPFROMREG(ft, MIPSInst_FT(ir));
1868 SPFROMREG(fs, MIPSInst_FS(ir));
1869 rv.s = ieee754sp_fmaxa(fs, ft);
1870 break;
1871 }
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 case fabs_op:
1874 handler.u = ieee754sp_abs;
1875 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 case fneg_op:
1878 handler.u = ieee754sp_neg;
1879 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 case fmov_op:
1882 /* an easy one */
1883 SPFROMREG(rv.s, MIPSInst_FS(ir));
1884 goto copcsr;
1885
1886 /* binary op on handler */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001887scopbop:
1888 SPFROMREG(fs, MIPSInst_FS(ir));
1889 SPFROMREG(ft, MIPSInst_FT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001891 rv.s = (*handler.b) (fs, ft);
1892 goto copcsr;
1893scopuop:
1894 SPFROMREG(fs, MIPSInst_FS(ir));
1895 rv.s = (*handler.u) (fs);
1896 goto copcsr;
1897copcsr:
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001898 if (ieee754_cxtest(IEEE754_INEXACT)) {
1899 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001901 }
1902 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1903 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001905 }
1906 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1907 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001909 }
1910 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1911 MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001913 }
1914 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1915 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 break;
1919
1920 /* unary conv ops */
1921 case fcvts_op:
1922 return SIGILL; /* not defined */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001924 case fcvtd_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 SPFROMREG(fs, MIPSInst_FS(ir));
1926 rv.d = ieee754dp_fsp(fs);
1927 rfmt = d_fmt;
1928 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001930 case fcvtw_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 SPFROMREG(fs, MIPSInst_FS(ir));
1932 rv.w = ieee754sp_tint(fs);
1933 rfmt = w_fmt;
1934 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 case fround_op:
1937 case ftrunc_op:
1938 case fceil_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001939 case ffloor_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001940 if (!cpu_has_mips_2_3_4_5_r)
Ralf Baechle08a07902014-04-19 13:11:37 +02001941 return SIGILL;
1942
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001943 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 SPFROMREG(fs, MIPSInst_FS(ir));
Maciej W. Rozycki2cfcf8a2015-04-03 23:24:56 +01001945 ieee754_csr.rm = MIPSInst_FUNC(ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 rv.w = ieee754sp_tint(fs);
1947 ieee754_csr.rm = oldrm;
1948 rfmt = w_fmt;
1949 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Paul Burton4b820d92016-04-21 14:04:48 +01001951 case fsel_op:
1952 if (!cpu_has_mips_r6)
1953 return SIGILL;
1954
1955 SPFROMREG(fd, MIPSInst_FD(ir));
1956 if (fd.bits & 0x1)
1957 SPFROMREG(rv.s, MIPSInst_FT(ir));
1958 else
1959 SPFROMREG(rv.s, MIPSInst_FS(ir));
1960 break;
1961
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001962 case fcvtl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001963 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001964 return SIGILL;
1965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 SPFROMREG(fs, MIPSInst_FS(ir));
1967 rv.l = ieee754sp_tlong(fs);
1968 rfmt = l_fmt;
1969 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 case froundl_op:
1972 case ftruncl_op:
1973 case fceill_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001974 case ffloorl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001975 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001976 return SIGILL;
1977
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001978 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 SPFROMREG(fs, MIPSInst_FS(ir));
Maciej W. Rozycki2cfcf8a2015-04-03 23:24:56 +01001980 ieee754_csr.rm = MIPSInst_FUNC(ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 rv.l = ieee754sp_tlong(fs);
1982 ieee754_csr.rm = oldrm;
1983 rfmt = l_fmt;
1984 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 default:
Markos Chandrasf8c3c672015-08-13 09:56:28 +02001987 if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001989 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991 SPFROMREG(fs, MIPSInst_FS(ir));
1992 SPFROMREG(ft, MIPSInst_FT(ir));
1993 rv.w = ieee754sp_cmp(fs, ft,
1994 cmptab[cmpop & 0x7], cmpop & 0x8);
1995 rfmt = -1;
1996 if ((cmpop & 0x8) && ieee754_cxtest
1997 (IEEE754_INVALID_OPERATION))
1998 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1999 else
2000 goto copcsr;
2001
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002002 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 break;
2005 }
2006 break;
2007 }
2008
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002009 case d_fmt: {
Paul Burton4b820d92016-04-21 14:04:48 +01002010 union ieee754dp fd, fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02002012 union ieee754dp(*b) (union ieee754dp, union ieee754dp);
2013 union ieee754dp(*u) (union ieee754dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 } handler;
2015
2016 switch (MIPSInst_FUNC(ir)) {
2017 /* binary ops */
2018 case fadd_op:
2019 handler.b = ieee754dp_add;
2020 goto dcopbop;
2021 case fsub_op:
2022 handler.b = ieee754dp_sub;
2023 goto dcopbop;
2024 case fmul_op:
2025 handler.b = ieee754dp_mul;
2026 goto dcopbop;
2027 case fdiv_op:
2028 handler.b = ieee754dp_div;
2029 goto dcopbop;
2030
2031 /* unary ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 case fsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02002033 if (!cpu_has_mips_2_3_4_5_r)
2034 return SIGILL;
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 handler.u = ieee754dp_sqrt;
2037 goto dcopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02002038 /*
2039 * Note that on some MIPS IV implementations such as the
2040 * R5000 and R8000 the FSQRT and FRECIP instructions do not
2041 * achieve full IEEE-754 accuracy - however this emulator does.
2042 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 case frsqrt_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01002044 if (!cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02002045 return SIGILL;
2046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 handler.u = fpemu_dp_rsqrt;
2048 goto dcopuop;
2049 case frecip_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01002050 if (!cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02002051 return SIGILL;
2052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 handler.u = fpemu_dp_recip;
2054 goto dcopuop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02002056 if (!cpu_has_mips_4_5_r)
2057 return SIGILL;
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
2060 if (((ctx->fcr31 & cond) != 0) !=
2061 ((MIPSInst_FT(ir) & 1) != 0))
2062 return 0;
2063 DPFROMREG(rv.d, MIPSInst_FS(ir));
2064 break;
2065 case fmovz_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02002066 if (!cpu_has_mips_4_5_r)
2067 return SIGILL;
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (xcp->regs[MIPSInst_FT(ir)] != 0)
2070 return 0;
2071 DPFROMREG(rv.d, MIPSInst_FS(ir));
2072 break;
2073 case fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02002074 if (!cpu_has_mips_4_5_r)
2075 return SIGILL;
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 if (xcp->regs[MIPSInst_FT(ir)] == 0)
2078 return 0;
2079 DPFROMREG(rv.d, MIPSInst_FS(ir));
2080 break;
Markos Chandras67613f02015-08-13 09:56:29 +02002081
2082 case fseleqz_op:
2083 if (!cpu_has_mips_r6)
2084 return SIGILL;
2085
2086 DPFROMREG(rv.d, MIPSInst_FT(ir));
2087 if (rv.l & 0x1)
2088 rv.l = 0;
2089 else
2090 DPFROMREG(rv.d, MIPSInst_FS(ir));
2091 break;
2092
Markos Chandras130fe352015-08-13 09:56:30 +02002093 case fselnez_op:
2094 if (!cpu_has_mips_r6)
2095 return SIGILL;
2096
2097 DPFROMREG(rv.d, MIPSInst_FT(ir));
2098 if (rv.l & 0x1)
2099 DPFROMREG(rv.d, MIPSInst_FS(ir));
2100 else
2101 rv.l = 0;
2102 break;
2103
Markos Chandrase24c3be2015-08-13 09:56:31 +02002104 case fmaddf_op: {
2105 union ieee754dp ft, fs, fd;
2106
2107 if (!cpu_has_mips_r6)
2108 return SIGILL;
2109
2110 DPFROMREG(ft, MIPSInst_FT(ir));
2111 DPFROMREG(fs, MIPSInst_FS(ir));
2112 DPFROMREG(fd, MIPSInst_FD(ir));
2113 rv.d = ieee754dp_maddf(fd, fs, ft);
2114 break;
2115 }
2116
Markos Chandras83d43302015-08-13 09:56:32 +02002117 case fmsubf_op: {
2118 union ieee754dp ft, fs, fd;
2119
2120 if (!cpu_has_mips_r6)
2121 return SIGILL;
2122
2123 DPFROMREG(ft, MIPSInst_FT(ir));
2124 DPFROMREG(fs, MIPSInst_FS(ir));
2125 DPFROMREG(fd, MIPSInst_FD(ir));
2126 rv.d = ieee754dp_msubf(fd, fs, ft);
2127 break;
2128 }
2129
Markos Chandras400bd2e2015-08-13 09:56:33 +02002130 case frint_op: {
2131 union ieee754dp fs;
2132
2133 if (!cpu_has_mips_r6)
2134 return SIGILL;
2135
2136 DPFROMREG(fs, MIPSInst_FS(ir));
Aleksandar Markovic3ec404d2017-08-21 14:24:48 +02002137 rv.d = ieee754dp_rint(fs);
Markos Chandras400bd2e2015-08-13 09:56:33 +02002138 goto copcsr;
2139 }
2140
Markos Chandras38db37b2015-08-13 09:56:34 +02002141 case fclass_op: {
2142 union ieee754dp fs;
2143
2144 if (!cpu_has_mips_r6)
2145 return SIGILL;
2146
2147 DPFROMREG(fs, MIPSInst_FS(ir));
Aleksandar Markovice1231dd2017-08-21 14:24:49 +02002148 rv.l = ieee754dp_2008class(fs);
2149 rfmt = l_fmt;
Markos Chandras38db37b2015-08-13 09:56:34 +02002150 break;
2151 }
2152
Markos Chandras4e9561b2015-08-13 09:56:35 +02002153 case fmin_op: {
2154 union ieee754dp fs, ft;
2155
2156 if (!cpu_has_mips_r6)
2157 return SIGILL;
2158
2159 DPFROMREG(ft, MIPSInst_FT(ir));
2160 DPFROMREG(fs, MIPSInst_FS(ir));
2161 rv.d = ieee754dp_fmin(fs, ft);
2162 break;
2163 }
2164
2165 case fmina_op: {
2166 union ieee754dp fs, ft;
2167
2168 if (!cpu_has_mips_r6)
2169 return SIGILL;
2170
2171 DPFROMREG(ft, MIPSInst_FT(ir));
2172 DPFROMREG(fs, MIPSInst_FS(ir));
2173 rv.d = ieee754dp_fmina(fs, ft);
2174 break;
2175 }
2176
Markos Chandrasa79f5f92015-08-13 09:56:36 +02002177 case fmax_op: {
2178 union ieee754dp fs, ft;
2179
2180 if (!cpu_has_mips_r6)
2181 return SIGILL;
2182
2183 DPFROMREG(ft, MIPSInst_FT(ir));
2184 DPFROMREG(fs, MIPSInst_FS(ir));
2185 rv.d = ieee754dp_fmax(fs, ft);
2186 break;
2187 }
2188
2189 case fmaxa_op: {
2190 union ieee754dp fs, ft;
2191
2192 if (!cpu_has_mips_r6)
2193 return SIGILL;
2194
2195 DPFROMREG(ft, MIPSInst_FT(ir));
2196 DPFROMREG(fs, MIPSInst_FS(ir));
2197 rv.d = ieee754dp_fmaxa(fs, ft);
2198 break;
2199 }
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 case fabs_op:
2202 handler.u = ieee754dp_abs;
2203 goto dcopuop;
2204
2205 case fneg_op:
2206 handler.u = ieee754dp_neg;
2207 goto dcopuop;
2208
2209 case fmov_op:
2210 /* an easy one */
2211 DPFROMREG(rv.d, MIPSInst_FS(ir));
2212 goto copcsr;
2213
2214 /* binary op on handler */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002215dcopbop:
2216 DPFROMREG(fs, MIPSInst_FS(ir));
2217 DPFROMREG(ft, MIPSInst_FT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002219 rv.d = (*handler.b) (fs, ft);
2220 goto copcsr;
2221dcopuop:
2222 DPFROMREG(fs, MIPSInst_FS(ir));
2223 rv.d = (*handler.u) (fs);
2224 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002226 /*
2227 * unary conv ops
2228 */
2229 case fcvts_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 DPFROMREG(fs, MIPSInst_FS(ir));
2231 rv.s = ieee754sp_fdp(fs);
2232 rfmt = s_fmt;
2233 goto copcsr;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 case fcvtd_op:
2236 return SIGILL; /* not defined */
2237
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002238 case fcvtw_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 DPFROMREG(fs, MIPSInst_FS(ir));
2240 rv.w = ieee754dp_tint(fs); /* wrong */
2241 rfmt = w_fmt;
2242 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 case fround_op:
2245 case ftrunc_op:
2246 case fceil_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002247 case ffloor_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02002248 if (!cpu_has_mips_2_3_4_5_r)
2249 return SIGILL;
2250
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002251 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 DPFROMREG(fs, MIPSInst_FS(ir));
Maciej W. Rozycki2cfcf8a2015-04-03 23:24:56 +01002253 ieee754_csr.rm = MIPSInst_FUNC(ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 rv.w = ieee754dp_tint(fs);
2255 ieee754_csr.rm = oldrm;
2256 rfmt = w_fmt;
2257 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Paul Burton4b820d92016-04-21 14:04:48 +01002259 case fsel_op:
2260 if (!cpu_has_mips_r6)
2261 return SIGILL;
2262
2263 DPFROMREG(fd, MIPSInst_FD(ir));
2264 if (fd.bits & 0x1)
2265 DPFROMREG(rv.d, MIPSInst_FT(ir));
2266 else
2267 DPFROMREG(rv.d, MIPSInst_FS(ir));
2268 break;
2269
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002270 case fcvtl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01002271 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02002272 return SIGILL;
2273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 DPFROMREG(fs, MIPSInst_FS(ir));
2275 rv.l = ieee754dp_tlong(fs);
2276 rfmt = l_fmt;
2277 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 case froundl_op:
2280 case ftruncl_op:
2281 case fceill_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002282 case ffloorl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01002283 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02002284 return SIGILL;
2285
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002286 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 DPFROMREG(fs, MIPSInst_FS(ir));
Maciej W. Rozycki2cfcf8a2015-04-03 23:24:56 +01002288 ieee754_csr.rm = MIPSInst_FUNC(ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 rv.l = ieee754dp_tlong(fs);
2290 ieee754_csr.rm = oldrm;
2291 rfmt = l_fmt;
2292 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 default:
Markos Chandrasf8c3c672015-08-13 09:56:28 +02002295 if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02002297 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
2299 DPFROMREG(fs, MIPSInst_FS(ir));
2300 DPFROMREG(ft, MIPSInst_FT(ir));
2301 rv.w = ieee754dp_cmp(fs, ft,
2302 cmptab[cmpop & 0x7], cmpop & 0x8);
2303 rfmt = -1;
2304 if ((cmpop & 0x8)
2305 &&
2306 ieee754_cxtest
2307 (IEEE754_INVALID_OPERATION))
2308 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2309 else
2310 goto copcsr;
2311
2312 }
2313 else {
2314 return SIGILL;
2315 }
2316 break;
2317 }
2318 break;
Markos Chandrasbbdd8142015-07-16 14:06:45 +01002319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Markos Chandrasbbdd8142015-07-16 14:06:45 +01002321 case w_fmt: {
2322 union ieee754dp fs;
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 switch (MIPSInst_FUNC(ir)) {
2325 case fcvts_op:
2326 /* convert word to single precision real */
2327 SPFROMREG(fs, MIPSInst_FS(ir));
2328 rv.s = ieee754sp_fint(fs.bits);
2329 rfmt = s_fmt;
2330 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 case fcvtd_op:
2332 /* convert word to double precision real */
2333 SPFROMREG(fs, MIPSInst_FS(ir));
2334 rv.d = ieee754dp_fint(fs.bits);
2335 rfmt = d_fmt;
2336 goto copcsr;
Markos Chandrasf8c3c672015-08-13 09:56:28 +02002337 default: {
2338 /* Emulating the new CMP.condn.fmt R6 instruction */
2339#define CMPOP_MASK 0x7
2340#define SIGN_BIT (0x1 << 3)
2341#define PREDICATE_BIT (0x1 << 4)
2342
2343 int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
2344 int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
2345 union ieee754sp fs, ft;
2346
2347 /* This is an R6 only instruction */
2348 if (!cpu_has_mips_r6 ||
2349 (MIPSInst_FUNC(ir) & 0x20))
2350 return SIGILL;
2351
2352 /* fmt is w_fmt for single precision so fix it */
2353 rfmt = s_fmt;
2354 /* default to false */
2355 rv.w = 0;
2356
2357 /* CMP.condn.S */
2358 SPFROMREG(fs, MIPSInst_FS(ir));
2359 SPFROMREG(ft, MIPSInst_FT(ir));
2360
2361 /* positive predicates */
2362 if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2363 if (ieee754sp_cmp(fs, ft, cmptab[cmpop],
2364 sig))
2365 rv.w = -1; /* true, all 1s */
2366 if ((sig) &&
2367 ieee754_cxtest(IEEE754_INVALID_OPERATION))
2368 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2369 else
2370 goto copcsr;
2371 } else {
2372 /* negative predicates */
2373 switch (cmpop) {
2374 case 1:
2375 case 2:
2376 case 3:
2377 if (ieee754sp_cmp(fs, ft,
2378 negative_cmptab[cmpop],
2379 sig))
2380 rv.w = -1; /* true, all 1s */
2381 if (sig &&
2382 ieee754_cxtest(IEEE754_INVALID_OPERATION))
2383 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2384 else
2385 goto copcsr;
2386 break;
2387 default:
2388 /* Reserved R6 ops */
2389 pr_err("Reserved MIPS R6 CMP.condn.S operation\n");
2390 return SIGILL;
2391 }
2392 }
2393 break;
2394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 }
Aleksandar Markovic1ff85602017-08-21 14:24:47 +02002396 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
2398
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002399 case l_fmt:
Ralf Baechle08a07902014-04-19 13:11:37 +02002400
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01002401 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02002402 return SIGILL;
2403
Paul Burtonbbd426f2014-02-13 11:26:41 +00002404 DIFROMREG(bits, MIPSInst_FS(ir));
2405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 switch (MIPSInst_FUNC(ir)) {
2407 case fcvts_op:
2408 /* convert long to single precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00002409 rv.s = ieee754sp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 rfmt = s_fmt;
2411 goto copcsr;
2412 case fcvtd_op:
2413 /* convert long to double precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00002414 rv.d = ieee754dp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 rfmt = d_fmt;
2416 goto copcsr;
Markos Chandrasf8c3c672015-08-13 09:56:28 +02002417 default: {
2418 /* Emulating the new CMP.condn.fmt R6 instruction */
2419 int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
2420 int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
2421 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Markos Chandrasf8c3c672015-08-13 09:56:28 +02002423 if (!cpu_has_mips_r6 ||
2424 (MIPSInst_FUNC(ir) & 0x20))
2425 return SIGILL;
2426
2427 /* fmt is l_fmt for double precision so fix it */
2428 rfmt = d_fmt;
2429 /* default to false */
2430 rv.l = 0;
2431
2432 /* CMP.condn.D */
2433 DPFROMREG(fs, MIPSInst_FS(ir));
2434 DPFROMREG(ft, MIPSInst_FT(ir));
2435
2436 /* positive predicates */
2437 if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2438 if (ieee754dp_cmp(fs, ft,
2439 cmptab[cmpop], sig))
2440 rv.l = -1LL; /* true, all 1s */
2441 if (sig &&
2442 ieee754_cxtest(IEEE754_INVALID_OPERATION))
2443 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2444 else
2445 goto copcsr;
2446 } else {
2447 /* negative predicates */
2448 switch (cmpop) {
2449 case 1:
2450 case 2:
2451 case 3:
2452 if (ieee754dp_cmp(fs, ft,
2453 negative_cmptab[cmpop],
2454 sig))
2455 rv.l = -1LL; /* true, all 1s */
2456 if (sig &&
2457 ieee754_cxtest(IEEE754_INVALID_OPERATION))
2458 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2459 else
2460 goto copcsr;
2461 break;
2462 default:
2463 /* Reserved R6 ops */
2464 pr_err("Reserved MIPS R6 CMP.condn.D operation\n");
2465 return SIGILL;
2466 }
2467 }
2468 break;
2469 }
2470 }
Aleksandar Markovic1ff85602017-08-21 14:24:47 +02002471 break;
2472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 default:
2474 return SIGILL;
2475 }
2476
2477 /*
2478 * Update the fpu CSR register for this operation.
2479 * If an exception is required, generate a tidy SIGFPE exception,
2480 * without updating the result register.
2481 * Note: cause exception bits do not accumulate, they are rewritten
2482 * for each op; only the flag/sticky bits accumulate.
2483 */
2484 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
2485 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002486 /*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 return SIGFPE;
2488 }
2489
2490 /*
2491 * Now we can safely write the result back to the register file.
2492 */
2493 switch (rfmt) {
Ralf Baechle08a07902014-04-19 13:11:37 +02002494 case -1:
2495
2496 if (cpu_has_mips_4_5_r)
Rob Kendrickc3b9b942014-07-23 10:03:58 +01002497 cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 else
Ralf Baechle08a07902014-04-19 13:11:37 +02002499 cbit = FPU_CSR_COND;
2500 if (rv.w)
2501 ctx->fcr31 |= cbit;
2502 else
2503 ctx->fcr31 &= ~cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 break;
Ralf Baechle08a07902014-04-19 13:11:37 +02002505
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 case d_fmt:
2507 DPTOREG(rv.d, MIPSInst_FD(ir));
2508 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 case s_fmt:
2510 SPTOREG(rv.s, MIPSInst_FD(ir));
2511 break;
2512 case w_fmt:
2513 SITOREG(rv.w, MIPSInst_FD(ir));
2514 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 case l_fmt:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01002516 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02002517 return SIGILL;
2518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 DITOREG(rv.l, MIPSInst_FD(ir));
2520 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 default:
2522 return SIGILL;
2523 }
2524
2525 return 0;
2526}
2527
Maciej W. Rozycki13769eba2017-06-16 00:05:08 +01002528/*
2529 * Emulate FPU instructions.
2530 *
2531 * If we use FPU hardware, then we have been typically called to handle
2532 * an unimplemented operation, such as where an operand is a NaN or
2533 * denormalized. In that case exit the emulation loop after a single
2534 * iteration so as to let hardware execute any subsequent instructions.
2535 *
2536 * If we have no FPU hardware or it has been disabled, then continue
2537 * emulating floating-point instructions until one of these conditions
2538 * has occurred:
2539 *
2540 * - a non-FPU instruction has been encountered,
2541 *
2542 * - an attempt to emulate has ended with a signal,
2543 *
2544 * - the ISA mode has been switched.
2545 *
2546 * We need to terminate the emulation loop if we got switched to the
2547 * MIPS16 mode, whether supported or not, so that we do not attempt
2548 * to emulate a MIPS16 instruction as a regular MIPS FPU instruction.
2549 * Similarly if we got switched to the microMIPS mode and only the
2550 * regular MIPS mode is supported, so that we do not attempt to emulate
2551 * a microMIPS instruction as a regular MIPS FPU instruction. Or if
2552 * we got switched to the regular MIPS mode and only the microMIPS mode
2553 * is supported, so that we do not attempt to emulate a regular MIPS
2554 * instruction that should cause an Address Error exception instead.
2555 * For simplicity we always terminate upon an ISA mode switch.
2556 */
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09002557int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Paul Burton445a58c2017-08-23 11:17:51 -07002558 int has_fpu, void __user **fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
Ralf Baechle333d1f62005-02-28 17:55:57 +00002560 unsigned long oldepc, prevepc;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002561 struct mm_decoded_insn dec_insn;
2562 u16 instr[4];
2563 u16 *instr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 int sig = 0;
2565
2566 oldepc = xcp->cp0_epc;
2567 do {
2568 prevepc = xcp->cp0_epc;
2569
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002570 if (get_isa16_mode(prevepc) && cpu_has_mmips) {
2571 /*
2572 * Get next 2 microMIPS instructions and convert them
2573 * into 32-bit instructions.
2574 */
2575 if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2576 (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2577 (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2578 (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2579 MIPS_FPU_EMU_INC_STATS(errors);
2580 return SIGBUS;
2581 }
2582 instr_ptr = instr;
2583
2584 /* Get first instruction. */
2585 if (mm_insn_16bit(*instr_ptr)) {
2586 /* Duplicate the half-word. */
2587 dec_insn.insn = (*instr_ptr << 16) |
2588 (*instr_ptr);
2589 /* 16-bit instruction. */
2590 dec_insn.pc_inc = 2;
2591 instr_ptr += 1;
2592 } else {
2593 dec_insn.insn = (*instr_ptr << 16) |
2594 *(instr_ptr+1);
2595 /* 32-bit instruction. */
2596 dec_insn.pc_inc = 4;
2597 instr_ptr += 2;
2598 }
2599 /* Get second instruction. */
2600 if (mm_insn_16bit(*instr_ptr)) {
2601 /* Duplicate the half-word. */
2602 dec_insn.next_insn = (*instr_ptr << 16) |
2603 (*instr_ptr);
2604 /* 16-bit instruction. */
2605 dec_insn.next_pc_inc = 2;
2606 } else {
2607 dec_insn.next_insn = (*instr_ptr << 16) |
2608 *(instr_ptr+1);
2609 /* 32-bit instruction. */
2610 dec_insn.next_pc_inc = 4;
2611 }
2612 dec_insn.micro_mips_mode = 1;
2613 } else {
2614 if ((get_user(dec_insn.insn,
2615 (mips_instruction __user *) xcp->cp0_epc)) ||
2616 (get_user(dec_insn.next_insn,
2617 (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2618 MIPS_FPU_EMU_INC_STATS(errors);
2619 return SIGBUS;
2620 }
2621 dec_insn.pc_inc = 4;
2622 dec_insn.next_pc_inc = 4;
2623 dec_insn.micro_mips_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 }
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002625
2626 if ((dec_insn.insn == 0) ||
2627 ((dec_insn.pc_inc == 2) &&
2628 ((dec_insn.insn & 0xffff) == MM_NOP16)))
2629 xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 else {
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002631 /*
Maciej W. Rozycki2cfcf8a2015-04-03 23:24:56 +01002632 * The 'ieee754_csr' is an alias of ctx->fcr31.
2633 * No need to copy ctx->fcr31 to ieee754_csr.
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002634 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002635 sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 }
2637
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09002638 if (has_fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 break;
2640 if (sig)
2641 break;
Maciej W. Rozycki13769eba2017-06-16 00:05:08 +01002642 /*
2643 * We have to check for the ISA bit explicitly here,
2644 * because `get_isa16_mode' may return 0 if support
2645 * for code compression has been globally disabled,
2646 * or otherwise we may produce the wrong signal or
2647 * even proceed successfully where we must not.
2648 */
2649 if ((xcp->cp0_epc ^ prevepc) & 0x1)
2650 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
2652 cond_resched();
2653 } while (xcp->cp0_epc > prevepc);
2654
2655 /* SIGILL indicates a non-fpu instruction */
2656 if (sig == SIGILL && xcp->cp0_epc != oldepc)
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002657 /* but if EPC has advanced, then ignore it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 sig = 0;
2659
2660 return sig;
2661}