blob: 99977c3bf10a341c08d85b0125bc570605989522 [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 Baechle08a07902014-04-19 13:11:37 +020038#include <linux/kconfig.h>
Ralf Baechle85c51c52014-04-16 02:46:11 +020039#include <linux/percpu-defs.h>
Deng-Cheng Zhu7f788d22010-10-12 19:37:21 +080040#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Ralf Baechlecd8ee342014-04-16 02:09:53 +020042#include <asm/branch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/inst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/ptrace.h>
45#include <asm/signal.h>
Ralf Baechlecd8ee342014-04-16 02:09:53 +020046#include <asm/uaccess.h>
47
Maciej W. Rozyckif6843622015-04-03 23:27:26 +010048#include <asm/cpu-info.h>
Ralf Baechlecd8ee342014-04-16 02:09:53 +020049#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/fpu_emulator.h>
Leonid Yegoshin102cedc2013-03-25 12:09:02 -050051#include <asm/fpu.h>
Leonid Yegoshinb0a668f2014-12-03 15:47:03 +000052#include <asm/mips-r2-to-r6-emul.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include "ieee754.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/* Function which emulates a floating point instruction. */
57
Atsushi Nemotoeae89072006-05-16 01:26:03 +090058static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 mips_instruction);
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static int fpux_emu(struct pt_regs *,
David Daney515b0292010-10-21 16:32:26 -070062 struct mips_fpu_struct *, mips_instruction, void *__user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/* Control registers */
65
66#define FPCREG_RID 0 /* $0 = revision id */
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +010067#define FPCREG_FCCR 25 /* $25 = fccr */
68#define FPCREG_FEXR 26 /* $26 = fexr */
69#define FPCREG_FENR 28 /* $28 = fenr */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define FPCREG_CSR 31 /* $31 = csr */
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* convert condition code register number to csr bit */
Leonid Yegoshinb0a668f2014-12-03 15:47:03 +000073const unsigned int fpucondbit[8] = {
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +010074 FPU_CSR_COND,
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 FPU_CSR_COND1,
76 FPU_CSR_COND2,
77 FPU_CSR_COND3,
78 FPU_CSR_COND4,
79 FPU_CSR_COND5,
80 FPU_CSR_COND6,
81 FPU_CSR_COND7
82};
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Leonid Yegoshin102cedc2013-03-25 12:09:02 -050084/* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
85static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
86static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
87static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
88static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
89
90/*
91 * This functions translates a 32-bit microMIPS instruction
92 * into a 32-bit MIPS32 instruction. Returns 0 on success
93 * and SIGILL otherwise.
94 */
95static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
96{
97 union mips_instruction insn = *insn_ptr;
98 union mips_instruction mips32_insn = insn;
99 int func, fmt, op;
100
101 switch (insn.mm_i_format.opcode) {
102 case mm_ldc132_op:
103 mips32_insn.mm_i_format.opcode = ldc1_op;
104 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
105 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
106 break;
107 case mm_lwc132_op:
108 mips32_insn.mm_i_format.opcode = lwc1_op;
109 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
110 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
111 break;
112 case mm_sdc132_op:
113 mips32_insn.mm_i_format.opcode = sdc1_op;
114 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
115 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
116 break;
117 case mm_swc132_op:
118 mips32_insn.mm_i_format.opcode = swc1_op;
119 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
120 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
121 break;
122 case mm_pool32i_op:
123 /* NOTE: offset is << by 1 if in microMIPS mode. */
124 if ((insn.mm_i_format.rt == mm_bc1f_op) ||
125 (insn.mm_i_format.rt == mm_bc1t_op)) {
126 mips32_insn.fb_format.opcode = cop1_op;
127 mips32_insn.fb_format.bc = bc_op;
128 mips32_insn.fb_format.flag =
129 (insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
130 } else
131 return SIGILL;
132 break;
133 case mm_pool32f_op:
134 switch (insn.mm_fp0_format.func) {
135 case mm_32f_01_op:
136 case mm_32f_11_op:
137 case mm_32f_02_op:
138 case mm_32f_12_op:
139 case mm_32f_41_op:
140 case mm_32f_51_op:
141 case mm_32f_42_op:
142 case mm_32f_52_op:
143 op = insn.mm_fp0_format.func;
144 if (op == mm_32f_01_op)
145 func = madd_s_op;
146 else if (op == mm_32f_11_op)
147 func = madd_d_op;
148 else if (op == mm_32f_02_op)
149 func = nmadd_s_op;
150 else if (op == mm_32f_12_op)
151 func = nmadd_d_op;
152 else if (op == mm_32f_41_op)
153 func = msub_s_op;
154 else if (op == mm_32f_51_op)
155 func = msub_d_op;
156 else if (op == mm_32f_42_op)
157 func = nmsub_s_op;
158 else
159 func = nmsub_d_op;
160 mips32_insn.fp6_format.opcode = cop1x_op;
161 mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
162 mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
163 mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
164 mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
165 mips32_insn.fp6_format.func = func;
166 break;
167 case mm_32f_10_op:
168 func = -1; /* Invalid */
169 op = insn.mm_fp5_format.op & 0x7;
170 if (op == mm_ldxc1_op)
171 func = ldxc1_op;
172 else if (op == mm_sdxc1_op)
173 func = sdxc1_op;
174 else if (op == mm_lwxc1_op)
175 func = lwxc1_op;
176 else if (op == mm_swxc1_op)
177 func = swxc1_op;
178
179 if (func != -1) {
180 mips32_insn.r_format.opcode = cop1x_op;
181 mips32_insn.r_format.rs =
182 insn.mm_fp5_format.base;
183 mips32_insn.r_format.rt =
184 insn.mm_fp5_format.index;
185 mips32_insn.r_format.rd = 0;
186 mips32_insn.r_format.re = insn.mm_fp5_format.fd;
187 mips32_insn.r_format.func = func;
188 } else
189 return SIGILL;
190 break;
191 case mm_32f_40_op:
192 op = -1; /* Invalid */
193 if (insn.mm_fp2_format.op == mm_fmovt_op)
194 op = 1;
195 else if (insn.mm_fp2_format.op == mm_fmovf_op)
196 op = 0;
197 if (op != -1) {
198 mips32_insn.fp0_format.opcode = cop1_op;
199 mips32_insn.fp0_format.fmt =
200 sdps_format[insn.mm_fp2_format.fmt];
201 mips32_insn.fp0_format.ft =
202 (insn.mm_fp2_format.cc<<2) + op;
203 mips32_insn.fp0_format.fs =
204 insn.mm_fp2_format.fs;
205 mips32_insn.fp0_format.fd =
206 insn.mm_fp2_format.fd;
207 mips32_insn.fp0_format.func = fmovc_op;
208 } else
209 return SIGILL;
210 break;
211 case mm_32f_60_op:
212 func = -1; /* Invalid */
213 if (insn.mm_fp0_format.op == mm_fadd_op)
214 func = fadd_op;
215 else if (insn.mm_fp0_format.op == mm_fsub_op)
216 func = fsub_op;
217 else if (insn.mm_fp0_format.op == mm_fmul_op)
218 func = fmul_op;
219 else if (insn.mm_fp0_format.op == mm_fdiv_op)
220 func = fdiv_op;
221 if (func != -1) {
222 mips32_insn.fp0_format.opcode = cop1_op;
223 mips32_insn.fp0_format.fmt =
224 sdps_format[insn.mm_fp0_format.fmt];
225 mips32_insn.fp0_format.ft =
226 insn.mm_fp0_format.ft;
227 mips32_insn.fp0_format.fs =
228 insn.mm_fp0_format.fs;
229 mips32_insn.fp0_format.fd =
230 insn.mm_fp0_format.fd;
231 mips32_insn.fp0_format.func = func;
232 } else
233 return SIGILL;
234 break;
235 case mm_32f_70_op:
236 func = -1; /* Invalid */
237 if (insn.mm_fp0_format.op == mm_fmovn_op)
238 func = fmovn_op;
239 else if (insn.mm_fp0_format.op == mm_fmovz_op)
240 func = fmovz_op;
241 if (func != -1) {
242 mips32_insn.fp0_format.opcode = cop1_op;
243 mips32_insn.fp0_format.fmt =
244 sdps_format[insn.mm_fp0_format.fmt];
245 mips32_insn.fp0_format.ft =
246 insn.mm_fp0_format.ft;
247 mips32_insn.fp0_format.fs =
248 insn.mm_fp0_format.fs;
249 mips32_insn.fp0_format.fd =
250 insn.mm_fp0_format.fd;
251 mips32_insn.fp0_format.func = func;
252 } else
253 return SIGILL;
254 break;
255 case mm_32f_73_op: /* POOL32FXF */
256 switch (insn.mm_fp1_format.op) {
257 case mm_movf0_op:
258 case mm_movf1_op:
259 case mm_movt0_op:
260 case mm_movt1_op:
261 if ((insn.mm_fp1_format.op & 0x7f) ==
262 mm_movf0_op)
263 op = 0;
264 else
265 op = 1;
266 mips32_insn.r_format.opcode = spec_op;
267 mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
268 mips32_insn.r_format.rt =
269 (insn.mm_fp4_format.cc << 2) + op;
270 mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
271 mips32_insn.r_format.re = 0;
272 mips32_insn.r_format.func = movc_op;
273 break;
274 case mm_fcvtd0_op:
275 case mm_fcvtd1_op:
276 case mm_fcvts0_op:
277 case mm_fcvts1_op:
278 if ((insn.mm_fp1_format.op & 0x7f) ==
279 mm_fcvtd0_op) {
280 func = fcvtd_op;
281 fmt = swl_format[insn.mm_fp3_format.fmt];
282 } else {
283 func = fcvts_op;
284 fmt = dwl_format[insn.mm_fp3_format.fmt];
285 }
286 mips32_insn.fp0_format.opcode = cop1_op;
287 mips32_insn.fp0_format.fmt = fmt;
288 mips32_insn.fp0_format.ft = 0;
289 mips32_insn.fp0_format.fs =
290 insn.mm_fp3_format.fs;
291 mips32_insn.fp0_format.fd =
292 insn.mm_fp3_format.rt;
293 mips32_insn.fp0_format.func = func;
294 break;
295 case mm_fmov0_op:
296 case mm_fmov1_op:
297 case mm_fabs0_op:
298 case mm_fabs1_op:
299 case mm_fneg0_op:
300 case mm_fneg1_op:
301 if ((insn.mm_fp1_format.op & 0x7f) ==
302 mm_fmov0_op)
303 func = fmov_op;
304 else if ((insn.mm_fp1_format.op & 0x7f) ==
305 mm_fabs0_op)
306 func = fabs_op;
307 else
308 func = fneg_op;
309 mips32_insn.fp0_format.opcode = cop1_op;
310 mips32_insn.fp0_format.fmt =
311 sdps_format[insn.mm_fp3_format.fmt];
312 mips32_insn.fp0_format.ft = 0;
313 mips32_insn.fp0_format.fs =
314 insn.mm_fp3_format.fs;
315 mips32_insn.fp0_format.fd =
316 insn.mm_fp3_format.rt;
317 mips32_insn.fp0_format.func = func;
318 break;
319 case mm_ffloorl_op:
320 case mm_ffloorw_op:
321 case mm_fceill_op:
322 case mm_fceilw_op:
323 case mm_ftruncl_op:
324 case mm_ftruncw_op:
325 case mm_froundl_op:
326 case mm_froundw_op:
327 case mm_fcvtl_op:
328 case mm_fcvtw_op:
329 if (insn.mm_fp1_format.op == mm_ffloorl_op)
330 func = ffloorl_op;
331 else if (insn.mm_fp1_format.op == mm_ffloorw_op)
332 func = ffloor_op;
333 else if (insn.mm_fp1_format.op == mm_fceill_op)
334 func = fceill_op;
335 else if (insn.mm_fp1_format.op == mm_fceilw_op)
336 func = fceil_op;
337 else if (insn.mm_fp1_format.op == mm_ftruncl_op)
338 func = ftruncl_op;
339 else if (insn.mm_fp1_format.op == mm_ftruncw_op)
340 func = ftrunc_op;
341 else if (insn.mm_fp1_format.op == mm_froundl_op)
342 func = froundl_op;
343 else if (insn.mm_fp1_format.op == mm_froundw_op)
344 func = fround_op;
345 else if (insn.mm_fp1_format.op == mm_fcvtl_op)
346 func = fcvtl_op;
347 else
348 func = fcvtw_op;
349 mips32_insn.fp0_format.opcode = cop1_op;
350 mips32_insn.fp0_format.fmt =
351 sd_format[insn.mm_fp1_format.fmt];
352 mips32_insn.fp0_format.ft = 0;
353 mips32_insn.fp0_format.fs =
354 insn.mm_fp1_format.fs;
355 mips32_insn.fp0_format.fd =
356 insn.mm_fp1_format.rt;
357 mips32_insn.fp0_format.func = func;
358 break;
359 case mm_frsqrt_op:
360 case mm_fsqrt_op:
361 case mm_frecip_op:
362 if (insn.mm_fp1_format.op == mm_frsqrt_op)
363 func = frsqrt_op;
364 else if (insn.mm_fp1_format.op == mm_fsqrt_op)
365 func = fsqrt_op;
366 else
367 func = frecip_op;
368 mips32_insn.fp0_format.opcode = cop1_op;
369 mips32_insn.fp0_format.fmt =
370 sdps_format[insn.mm_fp1_format.fmt];
371 mips32_insn.fp0_format.ft = 0;
372 mips32_insn.fp0_format.fs =
373 insn.mm_fp1_format.fs;
374 mips32_insn.fp0_format.fd =
375 insn.mm_fp1_format.rt;
376 mips32_insn.fp0_format.func = func;
377 break;
378 case mm_mfc1_op:
379 case mm_mtc1_op:
380 case mm_cfc1_op:
381 case mm_ctc1_op:
Steven J. Hill9355e592013-11-07 12:48:29 +0000382 case mm_mfhc1_op:
383 case mm_mthc1_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500384 if (insn.mm_fp1_format.op == mm_mfc1_op)
385 op = mfc_op;
386 else if (insn.mm_fp1_format.op == mm_mtc1_op)
387 op = mtc_op;
388 else if (insn.mm_fp1_format.op == mm_cfc1_op)
389 op = cfc_op;
Steven J. Hill9355e592013-11-07 12:48:29 +0000390 else if (insn.mm_fp1_format.op == mm_ctc1_op)
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500391 op = ctc_op;
Steven J. Hill9355e592013-11-07 12:48:29 +0000392 else if (insn.mm_fp1_format.op == mm_mfhc1_op)
393 op = mfhc_op;
394 else
395 op = mthc_op;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500396 mips32_insn.fp1_format.opcode = cop1_op;
397 mips32_insn.fp1_format.op = op;
398 mips32_insn.fp1_format.rt =
399 insn.mm_fp1_format.rt;
400 mips32_insn.fp1_format.fs =
401 insn.mm_fp1_format.fs;
402 mips32_insn.fp1_format.fd = 0;
403 mips32_insn.fp1_format.func = 0;
404 break;
405 default:
406 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500407 }
408 break;
409 case mm_32f_74_op: /* c.cond.fmt */
410 mips32_insn.fp0_format.opcode = cop1_op;
411 mips32_insn.fp0_format.fmt =
412 sdps_format[insn.mm_fp4_format.fmt];
413 mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
414 mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
415 mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
416 mips32_insn.fp0_format.func =
417 insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
418 break;
419 default:
420 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500421 }
422 break;
423 default:
424 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500425 }
426
427 *insn_ptr = mips32_insn;
428 return 0;
429}
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/*
432 * Redundant with logic already in kernel/branch.c,
433 * embedded in compute_return_epc. At some point,
434 * a single subroutine should be used across both
435 * modules.
436 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500437static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
438 unsigned long *contpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500440 union mips_instruction insn = (union mips_instruction)dec_insn.insn;
441 unsigned int fcr31;
442 unsigned int bit = 0;
443
444 switch (insn.i_format.opcode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 case spec_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500446 switch (insn.r_format.func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 case jalr_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500448 regs->regs[insn.r_format.rd] =
449 regs->cp0_epc + dec_insn.pc_inc +
450 dec_insn.next_pc_inc;
451 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 case jr_op:
Markos Chandras5f9f41c2014-11-25 15:54:14 +0000453 /* For R6, JR already emulated in jalr_op */
Markos Chandras143fefc2015-06-24 09:52:01 +0100454 if (NO_R6EMU && insn.r_format.func == jr_op)
Markos Chandras5f9f41c2014-11-25 15:54:14 +0000455 break;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500456 *contpc = regs->regs[insn.r_format.rs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 1;
458 }
459 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 case bcond_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500461 switch (insn.i_format.rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 case bltzal_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 case bltzall_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000464 if (NO_R6EMU && (insn.i_format.rs ||
465 insn.i_format.rt == bltzall_op))
466 break;
467
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500468 regs->regs[31] = regs->cp0_epc +
469 dec_insn.pc_inc +
470 dec_insn.next_pc_inc;
471 /* Fall through */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500472 case bltzl_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000473 if (NO_R6EMU)
474 break;
475 case bltz_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500476 if ((long)regs->regs[insn.i_format.rs] < 0)
477 *contpc = regs->cp0_epc +
478 dec_insn.pc_inc +
479 (insn.i_format.simmediate << 2);
480 else
481 *contpc = regs->cp0_epc +
482 dec_insn.pc_inc +
483 dec_insn.next_pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500485 case bgezal_op:
486 case bgezall_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000487 if (NO_R6EMU && (insn.i_format.rs ||
488 insn.i_format.rt == bgezall_op))
489 break;
490
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500491 regs->regs[31] = regs->cp0_epc +
492 dec_insn.pc_inc +
493 dec_insn.next_pc_inc;
494 /* Fall through */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500495 case bgezl_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000496 if (NO_R6EMU)
497 break;
498 case bgez_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500499 if ((long)regs->regs[insn.i_format.rs] >= 0)
500 *contpc = regs->cp0_epc +
501 dec_insn.pc_inc +
502 (insn.i_format.simmediate << 2);
503 else
504 *contpc = regs->cp0_epc +
505 dec_insn.pc_inc +
506 dec_insn.next_pc_inc;
507 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 case jalx_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500511 set_isa16_mode(bit);
512 case jal_op:
513 regs->regs[31] = regs->cp0_epc +
514 dec_insn.pc_inc +
515 dec_insn.next_pc_inc;
516 /* Fall through */
517 case j_op:
518 *contpc = regs->cp0_epc + dec_insn.pc_inc;
519 *contpc >>= 28;
520 *contpc <<= 28;
521 *contpc |= (insn.j_format.target << 2);
522 /* Set microMIPS mode bit: XOR for jalx. */
523 *contpc ^= bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500525 case beql_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000526 if (NO_R6EMU)
527 break;
528 case beq_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500529 if (regs->regs[insn.i_format.rs] ==
530 regs->regs[insn.i_format.rt])
531 *contpc = regs->cp0_epc +
532 dec_insn.pc_inc +
533 (insn.i_format.simmediate << 2);
534 else
535 *contpc = regs->cp0_epc +
536 dec_insn.pc_inc +
537 dec_insn.next_pc_inc;
538 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500539 case bnel_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000540 if (NO_R6EMU)
541 break;
542 case bne_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500543 if (regs->regs[insn.i_format.rs] !=
544 regs->regs[insn.i_format.rt])
545 *contpc = regs->cp0_epc +
546 dec_insn.pc_inc +
547 (insn.i_format.simmediate << 2);
548 else
549 *contpc = regs->cp0_epc +
550 dec_insn.pc_inc +
551 dec_insn.next_pc_inc;
552 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500553 case blezl_op:
Markos Chandrase9d92d22015-06-24 09:52:00 +0100554 if (!insn.i_format.rt && NO_R6EMU)
Markos Chandras319824e2014-11-25 16:02:23 +0000555 break;
556 case blez_op:
Markos Chandrasa8ff66f2014-11-26 12:57:54 +0000557
558 /*
559 * Compact branches for R6 for the
560 * blez and blezl opcodes.
561 * BLEZ | rs = 0 | rt != 0 == BLEZALC
562 * BLEZ | rs = rt != 0 == BGEZALC
563 * BLEZ | rs != 0 | rt != 0 == BGEUC
564 * BLEZL | rs = 0 | rt != 0 == BLEZC
565 * BLEZL | rs = rt != 0 == BGEZC
566 * BLEZL | rs != 0 | rt != 0 == BGEC
567 *
568 * For real BLEZ{,L}, rt is always 0.
569 */
570 if (cpu_has_mips_r6 && insn.i_format.rt) {
571 if ((insn.i_format.opcode == blez_op) &&
572 ((!insn.i_format.rs && insn.i_format.rt) ||
573 (insn.i_format.rs == insn.i_format.rt)))
574 regs->regs[31] = regs->cp0_epc +
575 dec_insn.pc_inc;
576 *contpc = regs->cp0_epc + dec_insn.pc_inc +
577 dec_insn.next_pc_inc;
578
579 return 1;
580 }
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500581 if ((long)regs->regs[insn.i_format.rs] <= 0)
582 *contpc = regs->cp0_epc +
583 dec_insn.pc_inc +
584 (insn.i_format.simmediate << 2);
585 else
586 *contpc = regs->cp0_epc +
587 dec_insn.pc_inc +
588 dec_insn.next_pc_inc;
589 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500590 case bgtzl_op:
Markos Chandrase9d92d22015-06-24 09:52:00 +0100591 if (!insn.i_format.rt && NO_R6EMU)
Markos Chandras319824e2014-11-25 16:02:23 +0000592 break;
593 case bgtz_op:
Markos Chandrasf1b44062014-11-26 13:05:09 +0000594 /*
595 * Compact branches for R6 for the
596 * bgtz and bgtzl opcodes.
597 * BGTZ | rs = 0 | rt != 0 == BGTZALC
598 * BGTZ | rs = rt != 0 == BLTZALC
599 * BGTZ | rs != 0 | rt != 0 == BLTUC
600 * BGTZL | rs = 0 | rt != 0 == BGTZC
601 * BGTZL | rs = rt != 0 == BLTZC
602 * BGTZL | rs != 0 | rt != 0 == BLTC
603 *
604 * *ZALC varint for BGTZ &&& rt != 0
605 * For real GTZ{,L}, rt is always 0.
606 */
607 if (cpu_has_mips_r6 && insn.i_format.rt) {
608 if ((insn.i_format.opcode == blez_op) &&
609 ((!insn.i_format.rs && insn.i_format.rt) ||
610 (insn.i_format.rs == insn.i_format.rt)))
611 regs->regs[31] = regs->cp0_epc +
612 dec_insn.pc_inc;
613 *contpc = regs->cp0_epc + dec_insn.pc_inc +
614 dec_insn.next_pc_inc;
615
616 return 1;
617 }
618
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500619 if ((long)regs->regs[insn.i_format.rs] > 0)
620 *contpc = regs->cp0_epc +
621 dec_insn.pc_inc +
622 (insn.i_format.simmediate << 2);
623 else
624 *contpc = regs->cp0_epc +
625 dec_insn.pc_inc +
626 dec_insn.next_pc_inc;
627 return 1;
Markos Chandrasc893ce32014-11-26 14:08:52 +0000628 case cbcond0_op:
Markos Chandras10d962d2014-11-26 15:03:54 +0000629 case cbcond1_op:
Markos Chandrasc893ce32014-11-26 14:08:52 +0000630 if (!cpu_has_mips_r6)
631 break;
632 if (insn.i_format.rt && !insn.i_format.rs)
633 regs->regs[31] = regs->cp0_epc + 4;
634 *contpc = regs->cp0_epc + dec_insn.pc_inc +
635 dec_insn.next_pc_inc;
636
637 return 1;
David Daneyc26d4212013-08-19 12:10:34 -0700638#ifdef CONFIG_CPU_CAVIUM_OCTEON
639 case lwc2_op: /* This is bbit0 on Octeon */
640 if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
641 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
642 else
643 *contpc = regs->cp0_epc + 8;
644 return 1;
645 case ldc2_op: /* This is bbit032 on Octeon */
646 if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
647 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
648 else
649 *contpc = regs->cp0_epc + 8;
650 return 1;
651 case swc2_op: /* This is bbit1 on Octeon */
652 if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
653 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
654 else
655 *contpc = regs->cp0_epc + 8;
656 return 1;
657 case sdc2_op: /* This is bbit132 on Octeon */
658 if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
659 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
660 else
661 *contpc = regs->cp0_epc + 8;
662 return 1;
Markos Chandras8467ca02014-11-26 13:56:51 +0000663#else
664 case bc6_op:
665 /*
666 * Only valid for MIPS R6 but we can still end up
667 * here from a broken userland so just tell emulator
668 * this is not a branch and let it break later on.
669 */
670 if (!cpu_has_mips_r6)
671 break;
672 *contpc = regs->cp0_epc + dec_insn.pc_inc +
673 dec_insn.next_pc_inc;
674
675 return 1;
Markos Chandras84fef632014-11-26 15:43:11 +0000676 case balc6_op:
677 if (!cpu_has_mips_r6)
678 break;
679 regs->regs[31] = regs->cp0_epc + 4;
680 *contpc = regs->cp0_epc + dec_insn.pc_inc +
681 dec_insn.next_pc_inc;
682
683 return 1;
Markos Chandras69b9a2f2014-11-27 09:32:25 +0000684 case beqzcjic_op:
685 if (!cpu_has_mips_r6)
686 break;
687 *contpc = regs->cp0_epc + dec_insn.pc_inc +
688 dec_insn.next_pc_inc;
689
690 return 1;
Markos Chandras28d6f932015-01-08 11:55:20 +0000691 case bnezcjialc_op:
692 if (!cpu_has_mips_r6)
693 break;
694 if (!insn.i_format.rs)
695 regs->regs[31] = regs->cp0_epc + 4;
696 *contpc = regs->cp0_epc + dec_insn.pc_inc +
697 dec_insn.next_pc_inc;
698
699 return 1;
David Daneyc26d4212013-08-19 12:10:34 -0700700#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 case cop0_op:
702 case cop1_op:
Markos Chandrasc8a34582014-11-26 10:10:18 +0000703 /* Need to check for R6 bc1nez and bc1eqz branches */
704 if (cpu_has_mips_r6 &&
705 ((insn.i_format.rs == bc1eqz_op) ||
706 (insn.i_format.rs == bc1nez_op))) {
707 bit = 0;
708 switch (insn.i_format.rs) {
709 case bc1eqz_op:
710 if (get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1)
711 bit = 1;
712 break;
713 case bc1nez_op:
714 if (!(get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1))
715 bit = 1;
716 break;
717 }
718 if (bit)
719 *contpc = regs->cp0_epc +
720 dec_insn.pc_inc +
721 (insn.i_format.simmediate << 2);
722 else
723 *contpc = regs->cp0_epc +
724 dec_insn.pc_inc +
725 dec_insn.next_pc_inc;
726
727 return 1;
728 }
729 /* R2/R6 compatible cop1 instruction. Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 case cop2_op:
731 case cop1x_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500732 if (insn.i_format.rs == bc_op) {
733 preempt_disable();
734 if (is_fpu_owner())
Manuel Lauss842dfc12014-11-07 14:13:54 +0100735 fcr31 = read_32bit_cp1_register(CP1_STATUS);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500736 else
737 fcr31 = current->thread.fpu.fcr31;
738 preempt_enable();
739
740 bit = (insn.i_format.rt >> 2);
741 bit += (bit != 0);
742 bit += 23;
743 switch (insn.i_format.rt & 3) {
744 case 0: /* bc1f */
745 case 2: /* bc1fl */
746 if (~fcr31 & (1 << bit))
747 *contpc = regs->cp0_epc +
748 dec_insn.pc_inc +
749 (insn.i_format.simmediate << 2);
750 else
751 *contpc = regs->cp0_epc +
752 dec_insn.pc_inc +
753 dec_insn.next_pc_inc;
754 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500755 case 1: /* bc1t */
756 case 3: /* bc1tl */
757 if (fcr31 & (1 << bit))
758 *contpc = regs->cp0_epc +
759 dec_insn.pc_inc +
760 (insn.i_format.simmediate << 2);
761 else
762 *contpc = regs->cp0_epc +
763 dec_insn.pc_inc +
764 dec_insn.next_pc_inc;
765 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500766 }
767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 break;
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return 0;
771}
772
773/*
774 * In the Linux kernel, we support selection of FPR format on the
Ralf Baechle70342282013-01-22 12:59:30 +0100775 * basis of the Status.FR bit. If an FPU is not present, the FR bit
David Daneyda0bac32009-11-02 11:33:46 -0800776 * is hardwired to zero, which would imply a 32-bit FPU even for
Paul Burton597ce172013-11-22 13:12:07 +0000777 * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
Ralf Baechle51d943f2012-08-15 19:42:19 +0200778 * FPU emu is slow and bulky and optimizing this function offers fairly
779 * sizeable benefits so we try to be clever and make this function return
780 * a constant whenever possible, that is on 64-bit kernels without O32
Paul Burton597ce172013-11-22 13:12:07 +0000781 * compatibility enabled and on 32-bit without 64-bit FPU support.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 */
David Daneyda0bac32009-11-02 11:33:46 -0800783static inline int cop1_64bit(struct pt_regs *xcp)
784{
Ralf Baechle08a07902014-04-19 13:11:37 +0200785 if (config_enabled(CONFIG_64BIT) && !config_enabled(CONFIG_MIPS32_O32))
786 return 1;
787 else if (config_enabled(CONFIG_32BIT) &&
788 !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
789 return 0;
790
Paul Burton597ce172013-11-22 13:12:07 +0000791 return !test_thread_flag(TIF_32BIT_FPREGS);
David Daneyda0bac32009-11-02 11:33:46 -0800792}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Paul Burton4227a2d2014-09-11 08:30:20 +0100794static inline bool hybrid_fprs(void)
795{
796 return test_thread_flag(TIF_HYBRID_FPREGS);
797}
798
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200799#define SIFROMREG(si, x) \
800do { \
Paul Burton4227a2d2014-09-11 08:30:20 +0100801 if (cop1_64bit(xcp) && !hybrid_fprs()) \
Paul Burtonc8c0da62014-09-24 10:45:37 +0100802 (si) = (int)get_fpr32(&ctx->fpr[x], 0); \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000803 else \
Paul Burtonc8c0da62014-09-24 10:45:37 +0100804 (si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000805} while (0)
David Daneyda0bac32009-11-02 11:33:46 -0800806
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200807#define SITOREG(si, x) \
808do { \
Paul Burton4227a2d2014-09-11 08:30:20 +0100809 if (cop1_64bit(xcp) && !hybrid_fprs()) { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000810 unsigned i; \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000811 set_fpr32(&ctx->fpr[x], 0, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000812 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
813 set_fpr32(&ctx->fpr[x], i, 0); \
814 } else { \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000815 set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000816 } \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000817} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Paul Burtonc8c0da62014-09-24 10:45:37 +0100819#define SIFROMHREG(si, x) ((si) = (int)get_fpr32(&ctx->fpr[x], 1))
Paul Burtonef1c47a2014-01-27 17:14:47 +0000820
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200821#define SITOHREG(si, x) \
822do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000823 unsigned i; \
824 set_fpr32(&ctx->fpr[x], 1, si); \
825 for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
826 set_fpr32(&ctx->fpr[x], i, 0); \
827} while (0)
Leonid Yegoshin1ac944002013-11-07 12:48:28 +0000828
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200829#define DIFROMREG(di, x) \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000830 ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
831
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200832#define DITOREG(di, x) \
833do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000834 unsigned fpr, i; \
835 fpr = (x) & ~(cop1_64bit(xcp) == 0); \
836 set_fpr64(&ctx->fpr[fpr], 0, di); \
837 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
838 set_fpr64(&ctx->fpr[fpr], i, 0); \
839} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Ralf Baechle21a151d2007-10-11 23:46:15 +0100841#define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
842#define SPTOREG(sp, x) SITOREG((sp).bits, x)
843#define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
844#define DPTOREG(dp, x) DITOREG((dp).bits, x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846/*
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100847 * Emulate a CFC1 instruction.
848 */
849static inline void cop1_cfc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
850 mips_instruction ir)
851{
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100852 u32 fcr31 = ctx->fcr31;
853 u32 value = 0;
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100854
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100855 switch (MIPSInst_RD(ir)) {
856 case FPCREG_CSR:
857 value = fcr31;
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100858 pr_debug("%p gpr[%d]<-csr=%08x\n",
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100859 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
860 break;
861
862 case FPCREG_FENR:
863 if (!cpu_has_mips_r)
864 break;
865 value = (fcr31 >> (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
866 MIPS_FENR_FS;
867 value |= fcr31 & (FPU_CSR_ALL_E | FPU_CSR_RM);
868 pr_debug("%p gpr[%d]<-enr=%08x\n",
869 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
870 break;
871
872 case FPCREG_FEXR:
873 if (!cpu_has_mips_r)
874 break;
875 value = fcr31 & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
876 pr_debug("%p gpr[%d]<-exr=%08x\n",
877 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
878 break;
879
880 case FPCREG_FCCR:
881 if (!cpu_has_mips_r)
882 break;
883 value = (fcr31 >> (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
884 MIPS_FCCR_COND0;
885 value |= (fcr31 >> (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
886 (MIPS_FCCR_CONDX & ~MIPS_FCCR_COND0);
887 pr_debug("%p gpr[%d]<-ccr=%08x\n",
888 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
889 break;
890
891 case FPCREG_RID:
Maciej W. Rozycki03dce592015-05-12 15:20:57 +0100892 value = boot_cpu_data.fpu_id;
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100893 break;
894
895 default:
896 break;
897 }
898
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100899 if (MIPSInst_RT(ir))
900 xcp->regs[MIPSInst_RT(ir)] = value;
901}
902
903/*
904 * Emulate a CTC1 instruction.
905 */
906static inline void cop1_ctc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
907 mips_instruction ir)
908{
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100909 u32 fcr31 = ctx->fcr31;
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100910 u32 value;
Maciej W. Rozycki9b266162015-04-03 23:27:48 +0100911 u32 mask;
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100912
913 if (MIPSInst_RT(ir) == 0)
914 value = 0;
915 else
916 value = xcp->regs[MIPSInst_RT(ir)];
917
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100918 switch (MIPSInst_RD(ir)) {
919 case FPCREG_CSR:
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100920 pr_debug("%p gpr[%d]->csr=%08x\n",
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100921 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100922
Maciej W. Rozycki9b266162015-04-03 23:27:48 +0100923 /* Preserve read-only bits. */
Maciej W. Rozycki03dce592015-05-12 15:20:57 +0100924 mask = boot_cpu_data.fpu_msk31;
Maciej W. Rozycki9b266162015-04-03 23:27:48 +0100925 fcr31 = (value & ~mask) | (fcr31 & mask);
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100926 break;
927
928 case FPCREG_FENR:
929 if (!cpu_has_mips_r)
930 break;
931 pr_debug("%p gpr[%d]->enr=%08x\n",
932 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
933 fcr31 &= ~(FPU_CSR_FS | FPU_CSR_ALL_E | FPU_CSR_RM);
934 fcr31 |= (value << (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
935 FPU_CSR_FS;
936 fcr31 |= value & (FPU_CSR_ALL_E | FPU_CSR_RM);
937 break;
938
939 case FPCREG_FEXR:
940 if (!cpu_has_mips_r)
941 break;
942 pr_debug("%p gpr[%d]->exr=%08x\n",
943 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
944 fcr31 &= ~(FPU_CSR_ALL_X | FPU_CSR_ALL_S);
945 fcr31 |= value & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
946 break;
947
948 case FPCREG_FCCR:
949 if (!cpu_has_mips_r)
950 break;
951 pr_debug("%p gpr[%d]->ccr=%08x\n",
952 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
953 fcr31 &= ~(FPU_CSR_CONDX | FPU_CSR_COND);
954 fcr31 |= (value << (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
955 FPU_CSR_COND;
956 fcr31 |= (value << (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
957 FPU_CSR_CONDX;
958 break;
959
960 default:
961 break;
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100962 }
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100963
964 ctx->fcr31 = fcr31;
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100965}
966
967/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 * Emulate the single floating point instruction pointed at by EPC.
969 * Two instructions if the instruction is in a branch delay slot.
970 */
971
David Daney515b0292010-10-21 16:32:26 -0700972static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500973 struct mm_decoded_insn dec_insn, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500975 unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
Paul Burton93583e12016-04-21 14:04:45 +0100976 unsigned int cond, cbit, bit0;
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200977 mips_instruction ir;
978 int likely, pc_inc;
Paul Burton93583e12016-04-21 14:04:45 +0100979 union fpureg *fpr;
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200980 u32 __user *wva;
981 u64 __user *dva;
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200982 u32 wval;
983 u64 dval;
984 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Ralf Baechle70e4c232014-04-30 11:09:44 +0200986 /*
987 * These are giving gcc a gentle hint about what to expect in
988 * dec_inst in order to do better optimization.
989 */
990 if (!cpu_has_mmips && dec_insn.micro_mips_mode)
991 unreachable();
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 /* XXX NEC Vr54xx bug workaround */
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200994 if (delay_slot(xcp)) {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500995 if (dec_insn.micro_mips_mode) {
996 if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200997 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500998 } else {
999 if (!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 }
1002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001004 if (delay_slot(xcp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 /*
1006 * The instruction to be emulated is in a branch delay slot
Ralf Baechle70342282013-01-22 12:59:30 +01001007 * which means that we have to emulate the branch instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 * BEFORE we do the cop1 instruction.
1009 *
1010 * This branch could be a COP1 branch, but in that case we
1011 * would have had a trap for that instruction, and would not
1012 * come through this route.
1013 *
1014 * Linux MIPS branch emulator operates on context, updating the
1015 * cp0_epc.
1016 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001017 ir = dec_insn.next_insn; /* process delay slot instr */
1018 pc_inc = dec_insn.next_pc_inc;
Ralf Baechle333d1f62005-02-28 17:55:57 +00001019 } else {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001020 ir = dec_insn.insn; /* process current instr */
1021 pc_inc = dec_insn.pc_inc;
1022 }
1023
1024 /*
1025 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
1026 * instructions, we want to convert microMIPS FPU instructions
1027 * into MIPS32 instructions so that we could reuse all of the
1028 * FPU emulation code.
1029 *
1030 * NOTE: We cannot do this for branch instructions since they
1031 * are not a subset. Example: Cannot emulate a 16-bit
1032 * aligned target address with a MIPS32 instruction.
1033 */
1034 if (dec_insn.micro_mips_mode) {
1035 /*
1036 * If next instruction is a 16-bit instruction, then it
1037 * it cannot be a FPU instruction. This could happen
1038 * since we can be called for non-FPU instructions.
1039 */
1040 if ((pc_inc == 2) ||
1041 (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
1042 == SIGILL))
1043 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001046emul:
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001047 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
David Daneyb6ee75e2009-11-05 11:34:26 -08001048 MIPS_FPU_EMU_INC_STATS(emulated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 switch (MIPSInst_OPCODE(ir)) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001050 case ldc1_op:
1051 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1052 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -08001053 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001054
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001055 if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001056 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001057 *fault_addr = dva;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return SIGBUS;
1059 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001060 if (__get_user(dval, dva)) {
David Daney515b0292010-10-21 16:32:26 -07001061 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001062 *fault_addr = dva;
David Daney515b0292010-10-21 16:32:26 -07001063 return SIGSEGV;
1064 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001065 DITOREG(dval, MIPSInst_RT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001068 case sdc1_op:
1069 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1070 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -08001071 MIPS_FPU_EMU_INC_STATS(stores);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001072 DIFROMREG(dval, MIPSInst_RT(ir));
1073 if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001074 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001075 *fault_addr = dva;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return SIGBUS;
1077 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001078 if (__put_user(dval, dva)) {
David Daney515b0292010-10-21 16:32:26 -07001079 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001080 *fault_addr = dva;
David Daney515b0292010-10-21 16:32:26 -07001081 return SIGSEGV;
1082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001085 case lwc1_op:
1086 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1087 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -08001088 MIPS_FPU_EMU_INC_STATS(loads);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001089 if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001090 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001091 *fault_addr = wva;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return SIGBUS;
1093 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001094 if (__get_user(wval, wva)) {
David Daney515b0292010-10-21 16:32:26 -07001095 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001096 *fault_addr = wva;
David Daney515b0292010-10-21 16:32:26 -07001097 return SIGSEGV;
1098 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001099 SITOREG(wval, MIPSInst_RT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001102 case swc1_op:
1103 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1104 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -08001105 MIPS_FPU_EMU_INC_STATS(stores);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001106 SIFROMREG(wval, MIPSInst_RT(ir));
1107 if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001108 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001109 *fault_addr = wva;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return SIGBUS;
1111 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001112 if (__put_user(wval, wva)) {
David Daney515b0292010-10-21 16:32:26 -07001113 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001114 *fault_addr = wva;
David Daney515b0292010-10-21 16:32:26 -07001115 return SIGSEGV;
1116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 case cop1_op:
1120 switch (MIPSInst_RS(ir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 case dmfc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001122 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1123 return SIGILL;
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 /* copregister fs -> gpr[rt] */
1126 if (MIPSInst_RT(ir) != 0) {
1127 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1128 MIPSInst_RD(ir));
1129 }
1130 break;
1131
1132 case dmtc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001133 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1134 return SIGILL;
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 /* copregister fs <- rt */
1137 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1138 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001140 case mfhc_op:
Markos Chandrase8f80cc2015-07-17 10:36:03 +01001141 if (!cpu_has_mips_r2_r6)
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001142 goto sigill;
1143
1144 /* copregister rd -> gpr[rt] */
1145 if (MIPSInst_RT(ir) != 0) {
1146 SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
1147 MIPSInst_RD(ir));
1148 }
1149 break;
1150
1151 case mthc_op:
Markos Chandrase8f80cc2015-07-17 10:36:03 +01001152 if (!cpu_has_mips_r2_r6)
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001153 goto sigill;
1154
1155 /* copregister rd <- gpr[rt] */
1156 SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1157 break;
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 case mfc_op:
1160 /* copregister rd -> gpr[rt] */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (MIPSInst_RT(ir) != 0) {
1162 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1163 MIPSInst_RD(ir));
1164 }
1165 break;
1166
1167 case mtc_op:
1168 /* copregister rd <- rt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1170 break;
1171
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001172 case cfc_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /* cop control register rd -> gpr[rt] */
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +01001174 cop1_cfc(xcp, ctx, ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001177 case ctc_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 /* copregister rd <- rt */
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +01001179 cop1_ctc(xcp, ctx, ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1181 return SIGFPE;
1182 }
1183 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Markos Chandrasc909ca72015-07-17 10:38:32 +01001185 case bc1eqz_op:
1186 case bc1nez_op:
1187 if (!cpu_has_mips_r6 || delay_slot(xcp))
1188 return SIGILL;
1189
1190 cond = likely = 0;
Paul Burton93583e12016-04-21 14:04:45 +01001191 fpr = &current->thread.fpu.fpr[MIPSInst_RT(ir)];
1192 bit0 = get_fpr32(fpr, 0) & 0x1;
Markos Chandrasc909ca72015-07-17 10:38:32 +01001193 switch (MIPSInst_RS(ir)) {
1194 case bc1eqz_op:
Paul Burton93583e12016-04-21 14:04:45 +01001195 cond = bit0 == 0;
Markos Chandrasc909ca72015-07-17 10:38:32 +01001196 break;
1197 case bc1nez_op:
Paul Burton93583e12016-04-21 14:04:45 +01001198 cond = bit0 != 0;
Markos Chandrasc909ca72015-07-17 10:38:32 +01001199 break;
1200 }
1201 goto branch_common;
1202
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001203 case bc_op:
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001204 if (delay_slot(xcp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return SIGILL;
1206
Ralf Baechle08a07902014-04-19 13:11:37 +02001207 if (cpu_has_mips_4_5_r)
1208 cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
1209 else
1210 cbit = FPU_CSR_COND;
1211 cond = ctx->fcr31 & cbit;
1212
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001213 likely = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 switch (MIPSInst_RT(ir) & 3) {
1215 case bcfl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001216 if (cpu_has_mips_2_3_4_5_r)
1217 likely = 1;
1218 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 case bcf_op:
1220 cond = !cond;
1221 break;
1222 case bctl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001223 if (cpu_has_mips_2_3_4_5_r)
1224 likely = 1;
1225 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 case bct_op:
1227 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
Markos Chandrasc909ca72015-07-17 10:38:32 +01001229branch_common:
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001230 set_delay_slot(xcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (cond) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001232 /*
1233 * Branch taken: emulate dslot instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 */
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001235 unsigned long bcpc;
1236
1237 /*
1238 * Remember EPC at the branch to point back
1239 * at so that any delay-slot instruction
1240 * signal is not silently ignored.
1241 */
1242 bcpc = xcp->cp0_epc;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001243 xcp->cp0_epc += dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001245 contpc = MIPSInst_SIMM(ir);
1246 ir = dec_insn.next_insn;
1247 if (dec_insn.micro_mips_mode) {
1248 contpc = (xcp->cp0_epc + (contpc << 1));
1249
1250 /* If 16-bit instruction, not FPU. */
1251 if ((dec_insn.next_pc_inc == 2) ||
1252 (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1253
1254 /*
1255 * Since this instruction will
1256 * be put on the stack with
1257 * 32-bit words, get around
1258 * this problem by putting a
1259 * NOP16 as the second one.
1260 */
1261 if (dec_insn.next_pc_inc == 2)
1262 ir = (ir & (~0xffff)) | MM_NOP16;
1263
1264 /*
1265 * Single step the non-CP1
1266 * instruction in the dslot.
1267 */
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001268 sig = mips_dsemul(xcp, ir,
1269 contpc);
Maciej W. Rozyckie4553572016-01-22 05:20:26 +00001270 if (sig < 0)
1271 break;
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001272 if (sig)
1273 xcp->cp0_epc = bcpc;
1274 /*
1275 * SIGILL forces out of
1276 * the emulation loop.
1277 */
1278 return sig ? sig : SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001279 }
1280 } else
1281 contpc = (xcp->cp0_epc + (contpc << 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 switch (MIPSInst_OPCODE(ir)) {
1284 case lwc1_op:
1285 case swc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001286 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 case ldc1_op:
1289 case sdc1_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001290 if (cpu_has_mips_2_3_4_5_r)
Ralf Baechle08a07902014-04-19 13:11:37 +02001291 goto emul;
1292
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001293 goto bc_sigill;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001294
Ralf Baechle08a07902014-04-19 13:11:37 +02001295 case cop1_op:
1296 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001297
Ralf Baechle08a07902014-04-19 13:11:37 +02001298 case cop1x_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001299 if (cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001300 /* its one of ours */
1301 goto emul;
1302
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001303 goto bc_sigill;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 case spec_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001306 switch (MIPSInst_FUNC(ir)) {
1307 case movc_op:
1308 if (cpu_has_mips_4_5_r)
1309 goto emul;
Ralf Baechle08a07902014-04-19 13:11:37 +02001310
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001311 goto bc_sigill;
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 break;
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001314
1315 bc_sigill:
1316 xcp->cp0_epc = bcpc;
1317 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319
1320 /*
1321 * Single step the non-cp1
1322 * instruction in the dslot
1323 */
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001324 sig = mips_dsemul(xcp, ir, contpc);
Maciej W. Rozyckie4553572016-01-22 05:20:26 +00001325 if (sig < 0)
1326 break;
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001327 if (sig)
1328 xcp->cp0_epc = bcpc;
1329 /* SIGILL forces out of the emulation loop. */
1330 return sig ? sig : SIGILL;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001331 } else if (likely) { /* branch not taken */
Maciej W. Rozycki5d77cf22015-04-03 23:24:24 +01001332 /*
1333 * branch likely nullifies
1334 * dslot if not taken
1335 */
1336 xcp->cp0_epc += dec_insn.pc_inc;
1337 contpc += dec_insn.pc_inc;
1338 /*
1339 * else continue & execute
1340 * dslot as normal insn
1341 */
1342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 default:
1346 if (!(MIPSInst_RS(ir) & 0x10))
1347 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001349 /* a real fpu computation instruction */
1350 if ((sig = fpu_emu(xcp, ctx, ir)))
1351 return sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
1353 break;
1354
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001355 case cop1x_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001356 if (!cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001357 return SIGILL;
1358
1359 sig = fpux_emu(xcp, ctx, ir, fault_addr);
David Daney515b0292010-10-21 16:32:26 -07001360 if (sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return sig;
1362 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 case spec_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001365 if (!cpu_has_mips_4_5_r)
1366 return SIGILL;
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (MIPSInst_FUNC(ir) != movc_op)
1369 return SIGILL;
1370 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
1371 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
1372 xcp->regs[MIPSInst_RD(ir)] =
1373 xcp->regs[MIPSInst_RS(ir)];
1374 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 default:
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001376sigill:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return SIGILL;
1378 }
1379
1380 /* we did it !! */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +09001381 xcp->cp0_epc = contpc;
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001382 clear_delay_slot(xcp);
Ralf Baechle333d1f62005-02-28 17:55:57 +00001383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return 0;
1385}
1386
1387/*
1388 * Conversion table from MIPS compare ops 48-63
1389 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
1390 */
1391static const unsigned char cmptab[8] = {
1392 0, /* cmp_0 (sig) cmp_sf */
1393 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
1394 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
1395 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
1396 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
1397 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
1398 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
1399 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
1400};
1401
Markos Chandrasf8c3c672015-08-13 09:56:28 +02001402static const unsigned char negative_cmptab[8] = {
1403 0, /* Reserved */
1404 IEEE754_CLT | IEEE754_CGT | IEEE754_CEQ,
1405 IEEE754_CLT | IEEE754_CGT | IEEE754_CUN,
1406 IEEE754_CLT | IEEE754_CGT,
1407 /* Reserved */
1408};
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411/*
1412 * Additional MIPS4 instructions
1413 */
1414
Ralf Baechle47fa0c02014-04-16 11:00:12 +02001415#define DEF3OP(name, p, f1, f2, f3) \
1416static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \
1417 union ieee754##p s, union ieee754##p t) \
1418{ \
1419 struct _ieee754_csr ieee754_csr_save; \
1420 s = f1(s, t); \
1421 ieee754_csr_save = ieee754_csr; \
1422 s = f2(s, r); \
1423 ieee754_csr_save.cx |= ieee754_csr.cx; \
1424 ieee754_csr_save.sx |= ieee754_csr.sx; \
1425 s = f3(s); \
1426 ieee754_csr.cx |= ieee754_csr_save.cx; \
1427 ieee754_csr.sx |= ieee754_csr_save.sx; \
1428 return s; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001431static union ieee754dp fpemu_dp_recip(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
1433 return ieee754dp_div(ieee754dp_one(0), d);
1434}
1435
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001436static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
1438 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
1439}
1440
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001441static union ieee754sp fpemu_sp_recip(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 return ieee754sp_div(ieee754sp_one(0), s);
1444}
1445
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001446static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
1448 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
1449}
1450
Ralf Baechle21a151d2007-10-11 23:46:15 +01001451DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
1452DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
1454DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
Ralf Baechle21a151d2007-10-11 23:46:15 +01001455DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
1456DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
1458DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
1459
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001460static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07001461 mips_instruction ir, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
1463 unsigned rcsr = 0; /* resulting csr */
1464
David Daneyb6ee75e2009-11-05 11:34:26 -08001465 MIPS_FPU_EMU_INC_STATS(cp1xops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 switch (MIPSInst_FMA_FFMT(ir)) {
1468 case s_fmt:{ /* 0 */
1469
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001470 union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
1471 union ieee754sp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001472 u32 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 u32 val;
1474
1475 switch (MIPSInst_FUNC(ir)) {
1476 case lwxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001477 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 xcp->regs[MIPSInst_FT(ir)]);
1479
David Daneyb6ee75e2009-11-05 11:34:26 -08001480 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001481 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001482 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001483 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 return SIGBUS;
1485 }
David Daney515b0292010-10-21 16:32:26 -07001486 if (__get_user(val, va)) {
1487 MIPS_FPU_EMU_INC_STATS(errors);
1488 *fault_addr = va;
1489 return SIGSEGV;
1490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 SITOREG(val, MIPSInst_FD(ir));
1492 break;
1493
1494 case swxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001495 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 xcp->regs[MIPSInst_FT(ir)]);
1497
David Daneyb6ee75e2009-11-05 11:34:26 -08001498 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 SIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001501 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1502 MIPS_FPU_EMU_INC_STATS(errors);
1503 *fault_addr = va;
1504 return SIGBUS;
1505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (put_user(val, va)) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001507 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001508 *fault_addr = va;
1509 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
1511 break;
1512
1513 case madd_s_op:
1514 handler = fpemu_sp_madd;
1515 goto scoptop;
1516 case msub_s_op:
1517 handler = fpemu_sp_msub;
1518 goto scoptop;
1519 case nmadd_s_op:
1520 handler = fpemu_sp_nmadd;
1521 goto scoptop;
1522 case nmsub_s_op:
1523 handler = fpemu_sp_nmsub;
1524 goto scoptop;
1525
1526 scoptop:
1527 SPFROMREG(fr, MIPSInst_FR(ir));
1528 SPFROMREG(fs, MIPSInst_FS(ir));
1529 SPFROMREG(ft, MIPSInst_FT(ir));
1530 fd = (*handler) (fr, fs, ft);
1531 SPTOREG(fd, MIPSInst_FD(ir));
1532
1533 copcsr:
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001534 if (ieee754_cxtest(IEEE754_INEXACT)) {
1535 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001537 }
1538 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1539 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001541 }
1542 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1543 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001545 }
1546 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1547 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001553 /*printk ("SIGFPE: FPU csr = %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 ctx->fcr31); */
1555 return SIGFPE;
1556 }
1557
1558 break;
1559
1560 default:
1561 return SIGILL;
1562 }
1563 break;
1564 }
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 case d_fmt:{ /* 1 */
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001567 union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
1568 union ieee754dp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001569 u64 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 u64 val;
1571
1572 switch (MIPSInst_FUNC(ir)) {
1573 case ldxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001574 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 xcp->regs[MIPSInst_FT(ir)]);
1576
David Daneyb6ee75e2009-11-05 11:34:26 -08001577 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001578 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001579 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001580 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return SIGBUS;
1582 }
David Daney515b0292010-10-21 16:32:26 -07001583 if (__get_user(val, va)) {
1584 MIPS_FPU_EMU_INC_STATS(errors);
1585 *fault_addr = va;
1586 return SIGSEGV;
1587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 DITOREG(val, MIPSInst_FD(ir));
1589 break;
1590
1591 case sdxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001592 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 xcp->regs[MIPSInst_FT(ir)]);
1594
David Daneyb6ee75e2009-11-05 11:34:26 -08001595 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 DIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001597 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001598 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001599 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 return SIGBUS;
1601 }
David Daney515b0292010-10-21 16:32:26 -07001602 if (__put_user(val, va)) {
1603 MIPS_FPU_EMU_INC_STATS(errors);
1604 *fault_addr = va;
1605 return SIGSEGV;
1606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 break;
1608
1609 case madd_d_op:
1610 handler = fpemu_dp_madd;
1611 goto dcoptop;
1612 case msub_d_op:
1613 handler = fpemu_dp_msub;
1614 goto dcoptop;
1615 case nmadd_d_op:
1616 handler = fpemu_dp_nmadd;
1617 goto dcoptop;
1618 case nmsub_d_op:
1619 handler = fpemu_dp_nmsub;
1620 goto dcoptop;
1621
1622 dcoptop:
1623 DPFROMREG(fr, MIPSInst_FR(ir));
1624 DPFROMREG(fs, MIPSInst_FS(ir));
1625 DPFROMREG(ft, MIPSInst_FT(ir));
1626 fd = (*handler) (fr, fs, ft);
1627 DPTOREG(fd, MIPSInst_FD(ir));
1628 goto copcsr;
1629
1630 default:
1631 return SIGILL;
1632 }
1633 break;
1634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001636 case 0x3:
1637 if (MIPSInst_FUNC(ir) != pfetch_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return SIGILL;
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 /* ignore prefx operation */
1641 break;
1642
1643 default:
1644 return SIGILL;
1645 }
1646
1647 return 0;
1648}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650
1651
1652/*
1653 * Emulate a single COP1 arithmetic instruction.
1654 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001655static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 mips_instruction ir)
1657{
1658 int rfmt; /* resulting format */
1659 unsigned rcsr = 0; /* resulting csr */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001660 unsigned int oldrm;
1661 unsigned int cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 unsigned cond;
1663 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001664 union ieee754dp d;
1665 union ieee754sp s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 int w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 s64 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 } rv; /* resulting value */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001669 u64 bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
David Daneyb6ee75e2009-11-05 11:34:26 -08001671 MIPS_FPU_EMU_INC_STATS(cp1ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001673 case s_fmt: { /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001675 union ieee754sp(*b) (union ieee754sp, union ieee754sp);
1676 union ieee754sp(*u) (union ieee754sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 } handler;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001678 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 switch (MIPSInst_FUNC(ir)) {
1681 /* binary ops */
1682 case fadd_op:
1683 handler.b = ieee754sp_add;
1684 goto scopbop;
1685 case fsub_op:
1686 handler.b = ieee754sp_sub;
1687 goto scopbop;
1688 case fmul_op:
1689 handler.b = ieee754sp_mul;
1690 goto scopbop;
1691 case fdiv_op:
1692 handler.b = ieee754sp_div;
1693 goto scopbop;
1694
1695 /* unary ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 case fsqrt_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001697 if (!cpu_has_mips_2_3_4_5_r)
Ralf Baechle08a07902014-04-19 13:11:37 +02001698 return SIGILL;
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 handler.u = ieee754sp_sqrt;
1701 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001702
Ralf Baechle08a07902014-04-19 13:11:37 +02001703 /*
1704 * Note that on some MIPS IV implementations such as the
1705 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1706 * achieve full IEEE-754 accuracy - however this emulator does.
1707 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 case frsqrt_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001709 if (!cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001710 return SIGILL;
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 handler.u = fpemu_sp_rsqrt;
1713 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 case frecip_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001716 if (!cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001717 return SIGILL;
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 handler.u = fpemu_sp_recip;
1720 goto scopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02001721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001723 if (!cpu_has_mips_4_5_r)
1724 return SIGILL;
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1727 if (((ctx->fcr31 & cond) != 0) !=
1728 ((MIPSInst_FT(ir) & 1) != 0))
1729 return 0;
1730 SPFROMREG(rv.s, MIPSInst_FS(ir));
1731 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 case fmovz_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001734 if (!cpu_has_mips_4_5_r)
1735 return SIGILL;
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1738 return 0;
1739 SPFROMREG(rv.s, MIPSInst_FS(ir));
1740 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 case fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001743 if (!cpu_has_mips_4_5_r)
1744 return SIGILL;
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1747 return 0;
1748 SPFROMREG(rv.s, MIPSInst_FS(ir));
1749 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001750
Markos Chandras67613f02015-08-13 09:56:29 +02001751 case fseleqz_op:
1752 if (!cpu_has_mips_r6)
1753 return SIGILL;
1754
1755 SPFROMREG(rv.s, MIPSInst_FT(ir));
1756 if (rv.w & 0x1)
1757 rv.w = 0;
1758 else
1759 SPFROMREG(rv.s, MIPSInst_FS(ir));
1760 break;
1761
Markos Chandras130fe352015-08-13 09:56:30 +02001762 case fselnez_op:
1763 if (!cpu_has_mips_r6)
1764 return SIGILL;
1765
1766 SPFROMREG(rv.s, MIPSInst_FT(ir));
1767 if (rv.w & 0x1)
1768 SPFROMREG(rv.s, MIPSInst_FS(ir));
1769 else
1770 rv.w = 0;
1771 break;
1772
Markos Chandrase24c3be2015-08-13 09:56:31 +02001773 case fmaddf_op: {
1774 union ieee754sp ft, fs, fd;
1775
1776 if (!cpu_has_mips_r6)
1777 return SIGILL;
1778
1779 SPFROMREG(ft, MIPSInst_FT(ir));
1780 SPFROMREG(fs, MIPSInst_FS(ir));
1781 SPFROMREG(fd, MIPSInst_FD(ir));
1782 rv.s = ieee754sp_maddf(fd, fs, ft);
1783 break;
1784 }
1785
Markos Chandras83d43302015-08-13 09:56:32 +02001786 case fmsubf_op: {
1787 union ieee754sp ft, fs, fd;
1788
1789 if (!cpu_has_mips_r6)
1790 return SIGILL;
1791
1792 SPFROMREG(ft, MIPSInst_FT(ir));
1793 SPFROMREG(fs, MIPSInst_FS(ir));
1794 SPFROMREG(fd, MIPSInst_FD(ir));
1795 rv.s = ieee754sp_msubf(fd, fs, ft);
1796 break;
1797 }
1798
Markos Chandras400bd2e2015-08-13 09:56:33 +02001799 case frint_op: {
1800 union ieee754sp fs;
1801
1802 if (!cpu_has_mips_r6)
1803 return SIGILL;
1804
1805 SPFROMREG(fs, MIPSInst_FS(ir));
1806 rv.l = ieee754sp_tlong(fs);
1807 rv.s = ieee754sp_flong(rv.l);
1808 goto copcsr;
1809 }
1810
Markos Chandras38db37b2015-08-13 09:56:34 +02001811 case fclass_op: {
1812 union ieee754sp fs;
1813
1814 if (!cpu_has_mips_r6)
1815 return SIGILL;
1816
1817 SPFROMREG(fs, MIPSInst_FS(ir));
1818 rv.w = ieee754sp_2008class(fs);
1819 rfmt = w_fmt;
1820 break;
1821 }
1822
Markos Chandras4e9561b2015-08-13 09:56:35 +02001823 case fmin_op: {
1824 union ieee754sp fs, ft;
1825
1826 if (!cpu_has_mips_r6)
1827 return SIGILL;
1828
1829 SPFROMREG(ft, MIPSInst_FT(ir));
1830 SPFROMREG(fs, MIPSInst_FS(ir));
1831 rv.s = ieee754sp_fmin(fs, ft);
1832 break;
1833 }
1834
1835 case fmina_op: {
1836 union ieee754sp fs, ft;
1837
1838 if (!cpu_has_mips_r6)
1839 return SIGILL;
1840
1841 SPFROMREG(ft, MIPSInst_FT(ir));
1842 SPFROMREG(fs, MIPSInst_FS(ir));
1843 rv.s = ieee754sp_fmina(fs, ft);
1844 break;
1845 }
1846
Markos Chandrasa79f5f92015-08-13 09:56:36 +02001847 case fmax_op: {
1848 union ieee754sp fs, ft;
1849
1850 if (!cpu_has_mips_r6)
1851 return SIGILL;
1852
1853 SPFROMREG(ft, MIPSInst_FT(ir));
1854 SPFROMREG(fs, MIPSInst_FS(ir));
1855 rv.s = ieee754sp_fmax(fs, ft);
1856 break;
1857 }
1858
1859 case fmaxa_op: {
1860 union ieee754sp fs, ft;
1861
1862 if (!cpu_has_mips_r6)
1863 return SIGILL;
1864
1865 SPFROMREG(ft, MIPSInst_FT(ir));
1866 SPFROMREG(fs, MIPSInst_FS(ir));
1867 rv.s = ieee754sp_fmaxa(fs, ft);
1868 break;
1869 }
1870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 case fabs_op:
1872 handler.u = ieee754sp_abs;
1873 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 case fneg_op:
1876 handler.u = ieee754sp_neg;
1877 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 case fmov_op:
1880 /* an easy one */
1881 SPFROMREG(rv.s, MIPSInst_FS(ir));
1882 goto copcsr;
1883
1884 /* binary op on handler */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001885scopbop:
1886 SPFROMREG(fs, MIPSInst_FS(ir));
1887 SPFROMREG(ft, MIPSInst_FT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001889 rv.s = (*handler.b) (fs, ft);
1890 goto copcsr;
1891scopuop:
1892 SPFROMREG(fs, MIPSInst_FS(ir));
1893 rv.s = (*handler.u) (fs);
1894 goto copcsr;
1895copcsr:
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001896 if (ieee754_cxtest(IEEE754_INEXACT)) {
1897 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001899 }
1900 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1901 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001903 }
1904 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1905 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001907 }
1908 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1909 MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001911 }
1912 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1913 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 break;
1917
1918 /* unary conv ops */
1919 case fcvts_op:
1920 return SIGILL; /* not defined */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001922 case fcvtd_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 SPFROMREG(fs, MIPSInst_FS(ir));
1924 rv.d = ieee754dp_fsp(fs);
1925 rfmt = d_fmt;
1926 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001928 case fcvtw_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 SPFROMREG(fs, MIPSInst_FS(ir));
1930 rv.w = ieee754sp_tint(fs);
1931 rfmt = w_fmt;
1932 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 case fround_op:
1935 case ftrunc_op:
1936 case fceil_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001937 case ffloor_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001938 if (!cpu_has_mips_2_3_4_5_r)
Ralf Baechle08a07902014-04-19 13:11:37 +02001939 return SIGILL;
1940
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001941 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 SPFROMREG(fs, MIPSInst_FS(ir));
Maciej W. Rozycki2cfcf8a2015-04-03 23:24:56 +01001943 ieee754_csr.rm = MIPSInst_FUNC(ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 rv.w = ieee754sp_tint(fs);
1945 ieee754_csr.rm = oldrm;
1946 rfmt = w_fmt;
1947 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001949 case fcvtl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001950 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001951 return SIGILL;
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 SPFROMREG(fs, MIPSInst_FS(ir));
1954 rv.l = ieee754sp_tlong(fs);
1955 rfmt = l_fmt;
1956 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 case froundl_op:
1959 case ftruncl_op:
1960 case fceill_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001961 case ffloorl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001962 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001963 return SIGILL;
1964
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001965 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 SPFROMREG(fs, MIPSInst_FS(ir));
Maciej W. Rozycki2cfcf8a2015-04-03 23:24:56 +01001967 ieee754_csr.rm = MIPSInst_FUNC(ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 rv.l = ieee754sp_tlong(fs);
1969 ieee754_csr.rm = oldrm;
1970 rfmt = l_fmt;
1971 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 default:
Markos Chandrasf8c3c672015-08-13 09:56:28 +02001974 if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001976 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 SPFROMREG(fs, MIPSInst_FS(ir));
1979 SPFROMREG(ft, MIPSInst_FT(ir));
1980 rv.w = ieee754sp_cmp(fs, ft,
1981 cmptab[cmpop & 0x7], cmpop & 0x8);
1982 rfmt = -1;
1983 if ((cmpop & 0x8) && ieee754_cxtest
1984 (IEEE754_INVALID_OPERATION))
1985 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1986 else
1987 goto copcsr;
1988
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001989 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 break;
1992 }
1993 break;
1994 }
1995
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001996 case d_fmt: {
1997 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001999 union ieee754dp(*b) (union ieee754dp, union ieee754dp);
2000 union ieee754dp(*u) (union ieee754dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 } handler;
2002
2003 switch (MIPSInst_FUNC(ir)) {
2004 /* binary ops */
2005 case fadd_op:
2006 handler.b = ieee754dp_add;
2007 goto dcopbop;
2008 case fsub_op:
2009 handler.b = ieee754dp_sub;
2010 goto dcopbop;
2011 case fmul_op:
2012 handler.b = ieee754dp_mul;
2013 goto dcopbop;
2014 case fdiv_op:
2015 handler.b = ieee754dp_div;
2016 goto dcopbop;
2017
2018 /* unary ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 case fsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02002020 if (!cpu_has_mips_2_3_4_5_r)
2021 return SIGILL;
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 handler.u = ieee754dp_sqrt;
2024 goto dcopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02002025 /*
2026 * Note that on some MIPS IV implementations such as the
2027 * R5000 and R8000 the FSQRT and FRECIP instructions do not
2028 * achieve full IEEE-754 accuracy - however this emulator does.
2029 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 case frsqrt_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01002031 if (!cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02002032 return SIGILL;
2033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 handler.u = fpemu_dp_rsqrt;
2035 goto dcopuop;
2036 case frecip_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01002037 if (!cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02002038 return SIGILL;
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 handler.u = fpemu_dp_recip;
2041 goto dcopuop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02002043 if (!cpu_has_mips_4_5_r)
2044 return SIGILL;
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
2047 if (((ctx->fcr31 & cond) != 0) !=
2048 ((MIPSInst_FT(ir) & 1) != 0))
2049 return 0;
2050 DPFROMREG(rv.d, MIPSInst_FS(ir));
2051 break;
2052 case fmovz_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02002053 if (!cpu_has_mips_4_5_r)
2054 return SIGILL;
2055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (xcp->regs[MIPSInst_FT(ir)] != 0)
2057 return 0;
2058 DPFROMREG(rv.d, MIPSInst_FS(ir));
2059 break;
2060 case fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02002061 if (!cpu_has_mips_4_5_r)
2062 return SIGILL;
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (xcp->regs[MIPSInst_FT(ir)] == 0)
2065 return 0;
2066 DPFROMREG(rv.d, MIPSInst_FS(ir));
2067 break;
Markos Chandras67613f02015-08-13 09:56:29 +02002068
2069 case fseleqz_op:
2070 if (!cpu_has_mips_r6)
2071 return SIGILL;
2072
2073 DPFROMREG(rv.d, MIPSInst_FT(ir));
2074 if (rv.l & 0x1)
2075 rv.l = 0;
2076 else
2077 DPFROMREG(rv.d, MIPSInst_FS(ir));
2078 break;
2079
Markos Chandras130fe352015-08-13 09:56:30 +02002080 case fselnez_op:
2081 if (!cpu_has_mips_r6)
2082 return SIGILL;
2083
2084 DPFROMREG(rv.d, MIPSInst_FT(ir));
2085 if (rv.l & 0x1)
2086 DPFROMREG(rv.d, MIPSInst_FS(ir));
2087 else
2088 rv.l = 0;
2089 break;
2090
Markos Chandrase24c3be2015-08-13 09:56:31 +02002091 case fmaddf_op: {
2092 union ieee754dp ft, fs, fd;
2093
2094 if (!cpu_has_mips_r6)
2095 return SIGILL;
2096
2097 DPFROMREG(ft, MIPSInst_FT(ir));
2098 DPFROMREG(fs, MIPSInst_FS(ir));
2099 DPFROMREG(fd, MIPSInst_FD(ir));
2100 rv.d = ieee754dp_maddf(fd, fs, ft);
2101 break;
2102 }
2103
Markos Chandras83d43302015-08-13 09:56:32 +02002104 case fmsubf_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_msubf(fd, fs, ft);
2114 break;
2115 }
2116
Markos Chandras400bd2e2015-08-13 09:56:33 +02002117 case frint_op: {
2118 union ieee754dp fs;
2119
2120 if (!cpu_has_mips_r6)
2121 return SIGILL;
2122
2123 DPFROMREG(fs, MIPSInst_FS(ir));
2124 rv.l = ieee754dp_tlong(fs);
2125 rv.d = ieee754dp_flong(rv.l);
2126 goto copcsr;
2127 }
2128
Markos Chandras38db37b2015-08-13 09:56:34 +02002129 case fclass_op: {
2130 union ieee754dp fs;
2131
2132 if (!cpu_has_mips_r6)
2133 return SIGILL;
2134
2135 DPFROMREG(fs, MIPSInst_FS(ir));
2136 rv.w = ieee754dp_2008class(fs);
2137 rfmt = w_fmt;
2138 break;
2139 }
2140
Markos Chandras4e9561b2015-08-13 09:56:35 +02002141 case fmin_op: {
2142 union ieee754dp fs, ft;
2143
2144 if (!cpu_has_mips_r6)
2145 return SIGILL;
2146
2147 DPFROMREG(ft, MIPSInst_FT(ir));
2148 DPFROMREG(fs, MIPSInst_FS(ir));
2149 rv.d = ieee754dp_fmin(fs, ft);
2150 break;
2151 }
2152
2153 case fmina_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_fmina(fs, ft);
2162 break;
2163 }
2164
Markos Chandrasa79f5f92015-08-13 09:56:36 +02002165 case fmax_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_fmax(fs, ft);
2174 break;
2175 }
2176
2177 case fmaxa_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_fmaxa(fs, ft);
2186 break;
2187 }
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 case fabs_op:
2190 handler.u = ieee754dp_abs;
2191 goto dcopuop;
2192
2193 case fneg_op:
2194 handler.u = ieee754dp_neg;
2195 goto dcopuop;
2196
2197 case fmov_op:
2198 /* an easy one */
2199 DPFROMREG(rv.d, MIPSInst_FS(ir));
2200 goto copcsr;
2201
2202 /* binary op on handler */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002203dcopbop:
2204 DPFROMREG(fs, MIPSInst_FS(ir));
2205 DPFROMREG(ft, MIPSInst_FT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002207 rv.d = (*handler.b) (fs, ft);
2208 goto copcsr;
2209dcopuop:
2210 DPFROMREG(fs, MIPSInst_FS(ir));
2211 rv.d = (*handler.u) (fs);
2212 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002214 /*
2215 * unary conv ops
2216 */
2217 case fcvts_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 DPFROMREG(fs, MIPSInst_FS(ir));
2219 rv.s = ieee754sp_fdp(fs);
2220 rfmt = s_fmt;
2221 goto copcsr;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 case fcvtd_op:
2224 return SIGILL; /* not defined */
2225
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002226 case fcvtw_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 DPFROMREG(fs, MIPSInst_FS(ir));
2228 rv.w = ieee754dp_tint(fs); /* wrong */
2229 rfmt = w_fmt;
2230 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 case fround_op:
2233 case ftrunc_op:
2234 case fceil_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002235 case ffloor_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02002236 if (!cpu_has_mips_2_3_4_5_r)
2237 return SIGILL;
2238
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002239 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 DPFROMREG(fs, MIPSInst_FS(ir));
Maciej W. Rozycki2cfcf8a2015-04-03 23:24:56 +01002241 ieee754_csr.rm = MIPSInst_FUNC(ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 rv.w = ieee754dp_tint(fs);
2243 ieee754_csr.rm = oldrm;
2244 rfmt = w_fmt;
2245 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002247 case fcvtl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01002248 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02002249 return SIGILL;
2250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 DPFROMREG(fs, MIPSInst_FS(ir));
2252 rv.l = ieee754dp_tlong(fs);
2253 rfmt = l_fmt;
2254 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 case froundl_op:
2257 case ftruncl_op:
2258 case fceill_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002259 case ffloorl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01002260 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02002261 return SIGILL;
2262
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002263 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 DPFROMREG(fs, MIPSInst_FS(ir));
Maciej W. Rozycki2cfcf8a2015-04-03 23:24:56 +01002265 ieee754_csr.rm = MIPSInst_FUNC(ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 rv.l = ieee754dp_tlong(fs);
2267 ieee754_csr.rm = oldrm;
2268 rfmt = l_fmt;
2269 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271 default:
Markos Chandrasf8c3c672015-08-13 09:56:28 +02002272 if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02002274 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
2276 DPFROMREG(fs, MIPSInst_FS(ir));
2277 DPFROMREG(ft, MIPSInst_FT(ir));
2278 rv.w = ieee754dp_cmp(fs, ft,
2279 cmptab[cmpop & 0x7], cmpop & 0x8);
2280 rfmt = -1;
2281 if ((cmpop & 0x8)
2282 &&
2283 ieee754_cxtest
2284 (IEEE754_INVALID_OPERATION))
2285 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2286 else
2287 goto copcsr;
2288
2289 }
2290 else {
2291 return SIGILL;
2292 }
2293 break;
2294 }
2295 break;
Markos Chandrasbbdd8142015-07-16 14:06:45 +01002296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Markos Chandrasbbdd8142015-07-16 14:06:45 +01002298 case w_fmt: {
2299 union ieee754dp fs;
2300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 switch (MIPSInst_FUNC(ir)) {
2302 case fcvts_op:
2303 /* convert word to single precision real */
2304 SPFROMREG(fs, MIPSInst_FS(ir));
2305 rv.s = ieee754sp_fint(fs.bits);
2306 rfmt = s_fmt;
2307 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 case fcvtd_op:
2309 /* convert word to double precision real */
2310 SPFROMREG(fs, MIPSInst_FS(ir));
2311 rv.d = ieee754dp_fint(fs.bits);
2312 rfmt = d_fmt;
2313 goto copcsr;
Markos Chandrasf8c3c672015-08-13 09:56:28 +02002314 default: {
2315 /* Emulating the new CMP.condn.fmt R6 instruction */
2316#define CMPOP_MASK 0x7
2317#define SIGN_BIT (0x1 << 3)
2318#define PREDICATE_BIT (0x1 << 4)
2319
2320 int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
2321 int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
2322 union ieee754sp fs, ft;
2323
2324 /* This is an R6 only instruction */
2325 if (!cpu_has_mips_r6 ||
2326 (MIPSInst_FUNC(ir) & 0x20))
2327 return SIGILL;
2328
2329 /* fmt is w_fmt for single precision so fix it */
2330 rfmt = s_fmt;
2331 /* default to false */
2332 rv.w = 0;
2333
2334 /* CMP.condn.S */
2335 SPFROMREG(fs, MIPSInst_FS(ir));
2336 SPFROMREG(ft, MIPSInst_FT(ir));
2337
2338 /* positive predicates */
2339 if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2340 if (ieee754sp_cmp(fs, ft, cmptab[cmpop],
2341 sig))
2342 rv.w = -1; /* true, all 1s */
2343 if ((sig) &&
2344 ieee754_cxtest(IEEE754_INVALID_OPERATION))
2345 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2346 else
2347 goto copcsr;
2348 } else {
2349 /* negative predicates */
2350 switch (cmpop) {
2351 case 1:
2352 case 2:
2353 case 3:
2354 if (ieee754sp_cmp(fs, ft,
2355 negative_cmptab[cmpop],
2356 sig))
2357 rv.w = -1; /* true, all 1s */
2358 if (sig &&
2359 ieee754_cxtest(IEEE754_INVALID_OPERATION))
2360 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2361 else
2362 goto copcsr;
2363 break;
2364 default:
2365 /* Reserved R6 ops */
2366 pr_err("Reserved MIPS R6 CMP.condn.S operation\n");
2367 return SIGILL;
2368 }
2369 }
2370 break;
2371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
2374
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002375 case l_fmt:
Ralf Baechle08a07902014-04-19 13:11:37 +02002376
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01002377 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02002378 return SIGILL;
2379
Paul Burtonbbd426f2014-02-13 11:26:41 +00002380 DIFROMREG(bits, MIPSInst_FS(ir));
2381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 switch (MIPSInst_FUNC(ir)) {
2383 case fcvts_op:
2384 /* convert long to single precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00002385 rv.s = ieee754sp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 rfmt = s_fmt;
2387 goto copcsr;
2388 case fcvtd_op:
2389 /* convert long to double precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00002390 rv.d = ieee754dp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 rfmt = d_fmt;
2392 goto copcsr;
Markos Chandrasf8c3c672015-08-13 09:56:28 +02002393 default: {
2394 /* Emulating the new CMP.condn.fmt R6 instruction */
2395 int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
2396 int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
2397 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Markos Chandrasf8c3c672015-08-13 09:56:28 +02002399 if (!cpu_has_mips_r6 ||
2400 (MIPSInst_FUNC(ir) & 0x20))
2401 return SIGILL;
2402
2403 /* fmt is l_fmt for double precision so fix it */
2404 rfmt = d_fmt;
2405 /* default to false */
2406 rv.l = 0;
2407
2408 /* CMP.condn.D */
2409 DPFROMREG(fs, MIPSInst_FS(ir));
2410 DPFROMREG(ft, MIPSInst_FT(ir));
2411
2412 /* positive predicates */
2413 if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2414 if (ieee754dp_cmp(fs, ft,
2415 cmptab[cmpop], sig))
2416 rv.l = -1LL; /* true, all 1s */
2417 if (sig &&
2418 ieee754_cxtest(IEEE754_INVALID_OPERATION))
2419 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2420 else
2421 goto copcsr;
2422 } else {
2423 /* negative predicates */
2424 switch (cmpop) {
2425 case 1:
2426 case 2:
2427 case 3:
2428 if (ieee754dp_cmp(fs, ft,
2429 negative_cmptab[cmpop],
2430 sig))
2431 rv.l = -1LL; /* true, all 1s */
2432 if (sig &&
2433 ieee754_cxtest(IEEE754_INVALID_OPERATION))
2434 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2435 else
2436 goto copcsr;
2437 break;
2438 default:
2439 /* Reserved R6 ops */
2440 pr_err("Reserved MIPS R6 CMP.condn.D operation\n");
2441 return SIGILL;
2442 }
2443 }
2444 break;
2445 }
2446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 default:
2448 return SIGILL;
2449 }
2450
2451 /*
2452 * Update the fpu CSR register for this operation.
2453 * If an exception is required, generate a tidy SIGFPE exception,
2454 * without updating the result register.
2455 * Note: cause exception bits do not accumulate, they are rewritten
2456 * for each op; only the flag/sticky bits accumulate.
2457 */
2458 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
2459 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002460 /*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 return SIGFPE;
2462 }
2463
2464 /*
2465 * Now we can safely write the result back to the register file.
2466 */
2467 switch (rfmt) {
Ralf Baechle08a07902014-04-19 13:11:37 +02002468 case -1:
2469
2470 if (cpu_has_mips_4_5_r)
Rob Kendrickc3b9b942014-07-23 10:03:58 +01002471 cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 else
Ralf Baechle08a07902014-04-19 13:11:37 +02002473 cbit = FPU_CSR_COND;
2474 if (rv.w)
2475 ctx->fcr31 |= cbit;
2476 else
2477 ctx->fcr31 &= ~cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 break;
Ralf Baechle08a07902014-04-19 13:11:37 +02002479
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 case d_fmt:
2481 DPTOREG(rv.d, MIPSInst_FD(ir));
2482 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 case s_fmt:
2484 SPTOREG(rv.s, MIPSInst_FD(ir));
2485 break;
2486 case w_fmt:
2487 SITOREG(rv.w, MIPSInst_FD(ir));
2488 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 case l_fmt:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01002490 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02002491 return SIGILL;
2492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 DITOREG(rv.l, MIPSInst_FD(ir));
2494 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 default:
2496 return SIGILL;
2497 }
2498
2499 return 0;
2500}
2501
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09002502int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07002503 int has_fpu, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
Ralf Baechle333d1f62005-02-28 17:55:57 +00002505 unsigned long oldepc, prevepc;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002506 struct mm_decoded_insn dec_insn;
2507 u16 instr[4];
2508 u16 *instr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 int sig = 0;
2510
2511 oldepc = xcp->cp0_epc;
2512 do {
2513 prevepc = xcp->cp0_epc;
2514
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002515 if (get_isa16_mode(prevepc) && cpu_has_mmips) {
2516 /*
2517 * Get next 2 microMIPS instructions and convert them
2518 * into 32-bit instructions.
2519 */
2520 if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2521 (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2522 (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2523 (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2524 MIPS_FPU_EMU_INC_STATS(errors);
2525 return SIGBUS;
2526 }
2527 instr_ptr = instr;
2528
2529 /* Get first instruction. */
2530 if (mm_insn_16bit(*instr_ptr)) {
2531 /* Duplicate the half-word. */
2532 dec_insn.insn = (*instr_ptr << 16) |
2533 (*instr_ptr);
2534 /* 16-bit instruction. */
2535 dec_insn.pc_inc = 2;
2536 instr_ptr += 1;
2537 } else {
2538 dec_insn.insn = (*instr_ptr << 16) |
2539 *(instr_ptr+1);
2540 /* 32-bit instruction. */
2541 dec_insn.pc_inc = 4;
2542 instr_ptr += 2;
2543 }
2544 /* Get second instruction. */
2545 if (mm_insn_16bit(*instr_ptr)) {
2546 /* Duplicate the half-word. */
2547 dec_insn.next_insn = (*instr_ptr << 16) |
2548 (*instr_ptr);
2549 /* 16-bit instruction. */
2550 dec_insn.next_pc_inc = 2;
2551 } else {
2552 dec_insn.next_insn = (*instr_ptr << 16) |
2553 *(instr_ptr+1);
2554 /* 32-bit instruction. */
2555 dec_insn.next_pc_inc = 4;
2556 }
2557 dec_insn.micro_mips_mode = 1;
2558 } else {
2559 if ((get_user(dec_insn.insn,
2560 (mips_instruction __user *) xcp->cp0_epc)) ||
2561 (get_user(dec_insn.next_insn,
2562 (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2563 MIPS_FPU_EMU_INC_STATS(errors);
2564 return SIGBUS;
2565 }
2566 dec_insn.pc_inc = 4;
2567 dec_insn.next_pc_inc = 4;
2568 dec_insn.micro_mips_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 }
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002570
2571 if ((dec_insn.insn == 0) ||
2572 ((dec_insn.pc_inc == 2) &&
2573 ((dec_insn.insn & 0xffff) == MM_NOP16)))
2574 xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 else {
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002576 /*
Maciej W. Rozycki2cfcf8a2015-04-03 23:24:56 +01002577 * The 'ieee754_csr' is an alias of ctx->fcr31.
2578 * No need to copy ctx->fcr31 to ieee754_csr.
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002579 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002580 sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 }
2582
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09002583 if (has_fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 break;
2585 if (sig)
2586 break;
2587
2588 cond_resched();
2589 } while (xcp->cp0_epc > prevepc);
2590
2591 /* SIGILL indicates a non-fpu instruction */
2592 if (sig == SIGILL && xcp->cp0_epc != oldepc)
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002593 /* but if EPC has advanced, then ignore it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 sig = 0;
2595
2596 return sig;
2597}