blob: 972a7e23737aaf62cde9d43bee08fd7bc0a87b75 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * cp1emu.c: a MIPS coprocessor 1 (fpu) instruction emulator
3 *
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.,
21 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * A complete emulator for MIPS coprocessor 1 instructions. This is
24 * required for #float(switch) or #float(trap), where it catches all
25 * COP1 instructions via the "CoProcessor Unusable" exception.
26 *
27 * More surprisingly it is also required for #float(ieee), to help out
28 * the hardware fpu at the boundaries of the IEEE-754 representation
29 * (denormalised values, infinities, underflow, etc). It is made
30 * quite nasty because emulation of some non-COP1 instructions is
31 * required, e.g. in branch delay slots.
32 *
33 * Note if you know that you won't have an fpu, then you'll get much
34 * better performance by compiling with -msoft-float!
35 */
36#include <linux/sched.h>
Atsushi 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
48#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/fpu_emulator.h>
Leonid Yegoshin102cedc2013-03-25 12:09:02 -050050#include <asm/fpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#include "ieee754.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* Function which emulates a floating point instruction. */
55
Atsushi Nemotoeae89072006-05-16 01:26:03 +090056static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 mips_instruction);
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static int fpux_emu(struct pt_regs *,
David Daney515b0292010-10-21 16:32:26 -070060 struct mips_fpu_struct *, mips_instruction, void *__user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/* Control registers */
63
64#define FPCREG_RID 0 /* $0 = revision id */
65#define FPCREG_CSR 31 /* $31 = csr */
66
Shane McDonald95e8f632010-05-06 23:26:57 -060067/* Determine rounding mode from the RM bits of the FCSR */
68#define modeindex(v) ((v) & FPU_CSR_RM)
69
Leonid Yegoshin102cedc2013-03-25 12:09:02 -050070/* microMIPS bitfields */
71#define MM_POOL32A_MINOR_MASK 0x3f
72#define MM_POOL32A_MINOR_SHIFT 0x6
73#define MM_MIPS32_COND_FC 0x30
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/* Convert Mips rounding mode (0..3) to IEEE library modes. */
76static const unsigned char ieee_rm[4] = {
Ralf Baechlecd21dfc2005-04-28 13:39:10 +000077 [FPU_CSR_RN] = IEEE754_RN,
78 [FPU_CSR_RZ] = IEEE754_RZ,
79 [FPU_CSR_RU] = IEEE754_RU,
80 [FPU_CSR_RD] = IEEE754_RD,
81};
82/* Convert IEEE library modes to Mips rounding mode (0..3). */
83static const unsigned char mips_rm[4] = {
84 [IEEE754_RN] = FPU_CSR_RN,
85 [IEEE754_RZ] = FPU_CSR_RZ,
86 [IEEE754_RD] = FPU_CSR_RD,
87 [IEEE754_RU] = FPU_CSR_RU,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* convert condition code register number to csr bit */
91static const unsigned int fpucondbit[8] = {
92 FPU_CSR_COND0,
93 FPU_CSR_COND1,
94 FPU_CSR_COND2,
95 FPU_CSR_COND3,
96 FPU_CSR_COND4,
97 FPU_CSR_COND5,
98 FPU_CSR_COND6,
99 FPU_CSR_COND7
100};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500102/* (microMIPS) Convert 16-bit register encoding to 32-bit register encoding. */
103static const unsigned int reg16to32map[8] = {16, 17, 2, 3, 4, 5, 6, 7};
104
105/* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
106static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
107static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
108static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
109static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
110
111/*
112 * This functions translates a 32-bit microMIPS instruction
113 * into a 32-bit MIPS32 instruction. Returns 0 on success
114 * and SIGILL otherwise.
115 */
116static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
117{
118 union mips_instruction insn = *insn_ptr;
119 union mips_instruction mips32_insn = insn;
120 int func, fmt, op;
121
122 switch (insn.mm_i_format.opcode) {
123 case mm_ldc132_op:
124 mips32_insn.mm_i_format.opcode = ldc1_op;
125 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
126 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
127 break;
128 case mm_lwc132_op:
129 mips32_insn.mm_i_format.opcode = lwc1_op;
130 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
131 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
132 break;
133 case mm_sdc132_op:
134 mips32_insn.mm_i_format.opcode = sdc1_op;
135 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
136 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
137 break;
138 case mm_swc132_op:
139 mips32_insn.mm_i_format.opcode = swc1_op;
140 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
141 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
142 break;
143 case mm_pool32i_op:
144 /* NOTE: offset is << by 1 if in microMIPS mode. */
145 if ((insn.mm_i_format.rt == mm_bc1f_op) ||
146 (insn.mm_i_format.rt == mm_bc1t_op)) {
147 mips32_insn.fb_format.opcode = cop1_op;
148 mips32_insn.fb_format.bc = bc_op;
149 mips32_insn.fb_format.flag =
150 (insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
151 } else
152 return SIGILL;
153 break;
154 case mm_pool32f_op:
155 switch (insn.mm_fp0_format.func) {
156 case mm_32f_01_op:
157 case mm_32f_11_op:
158 case mm_32f_02_op:
159 case mm_32f_12_op:
160 case mm_32f_41_op:
161 case mm_32f_51_op:
162 case mm_32f_42_op:
163 case mm_32f_52_op:
164 op = insn.mm_fp0_format.func;
165 if (op == mm_32f_01_op)
166 func = madd_s_op;
167 else if (op == mm_32f_11_op)
168 func = madd_d_op;
169 else if (op == mm_32f_02_op)
170 func = nmadd_s_op;
171 else if (op == mm_32f_12_op)
172 func = nmadd_d_op;
173 else if (op == mm_32f_41_op)
174 func = msub_s_op;
175 else if (op == mm_32f_51_op)
176 func = msub_d_op;
177 else if (op == mm_32f_42_op)
178 func = nmsub_s_op;
179 else
180 func = nmsub_d_op;
181 mips32_insn.fp6_format.opcode = cop1x_op;
182 mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
183 mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
184 mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
185 mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
186 mips32_insn.fp6_format.func = func;
187 break;
188 case mm_32f_10_op:
189 func = -1; /* Invalid */
190 op = insn.mm_fp5_format.op & 0x7;
191 if (op == mm_ldxc1_op)
192 func = ldxc1_op;
193 else if (op == mm_sdxc1_op)
194 func = sdxc1_op;
195 else if (op == mm_lwxc1_op)
196 func = lwxc1_op;
197 else if (op == mm_swxc1_op)
198 func = swxc1_op;
199
200 if (func != -1) {
201 mips32_insn.r_format.opcode = cop1x_op;
202 mips32_insn.r_format.rs =
203 insn.mm_fp5_format.base;
204 mips32_insn.r_format.rt =
205 insn.mm_fp5_format.index;
206 mips32_insn.r_format.rd = 0;
207 mips32_insn.r_format.re = insn.mm_fp5_format.fd;
208 mips32_insn.r_format.func = func;
209 } else
210 return SIGILL;
211 break;
212 case mm_32f_40_op:
213 op = -1; /* Invalid */
214 if (insn.mm_fp2_format.op == mm_fmovt_op)
215 op = 1;
216 else if (insn.mm_fp2_format.op == mm_fmovf_op)
217 op = 0;
218 if (op != -1) {
219 mips32_insn.fp0_format.opcode = cop1_op;
220 mips32_insn.fp0_format.fmt =
221 sdps_format[insn.mm_fp2_format.fmt];
222 mips32_insn.fp0_format.ft =
223 (insn.mm_fp2_format.cc<<2) + op;
224 mips32_insn.fp0_format.fs =
225 insn.mm_fp2_format.fs;
226 mips32_insn.fp0_format.fd =
227 insn.mm_fp2_format.fd;
228 mips32_insn.fp0_format.func = fmovc_op;
229 } else
230 return SIGILL;
231 break;
232 case mm_32f_60_op:
233 func = -1; /* Invalid */
234 if (insn.mm_fp0_format.op == mm_fadd_op)
235 func = fadd_op;
236 else if (insn.mm_fp0_format.op == mm_fsub_op)
237 func = fsub_op;
238 else if (insn.mm_fp0_format.op == mm_fmul_op)
239 func = fmul_op;
240 else if (insn.mm_fp0_format.op == mm_fdiv_op)
241 func = fdiv_op;
242 if (func != -1) {
243 mips32_insn.fp0_format.opcode = cop1_op;
244 mips32_insn.fp0_format.fmt =
245 sdps_format[insn.mm_fp0_format.fmt];
246 mips32_insn.fp0_format.ft =
247 insn.mm_fp0_format.ft;
248 mips32_insn.fp0_format.fs =
249 insn.mm_fp0_format.fs;
250 mips32_insn.fp0_format.fd =
251 insn.mm_fp0_format.fd;
252 mips32_insn.fp0_format.func = func;
253 } else
254 return SIGILL;
255 break;
256 case mm_32f_70_op:
257 func = -1; /* Invalid */
258 if (insn.mm_fp0_format.op == mm_fmovn_op)
259 func = fmovn_op;
260 else if (insn.mm_fp0_format.op == mm_fmovz_op)
261 func = fmovz_op;
262 if (func != -1) {
263 mips32_insn.fp0_format.opcode = cop1_op;
264 mips32_insn.fp0_format.fmt =
265 sdps_format[insn.mm_fp0_format.fmt];
266 mips32_insn.fp0_format.ft =
267 insn.mm_fp0_format.ft;
268 mips32_insn.fp0_format.fs =
269 insn.mm_fp0_format.fs;
270 mips32_insn.fp0_format.fd =
271 insn.mm_fp0_format.fd;
272 mips32_insn.fp0_format.func = func;
273 } else
274 return SIGILL;
275 break;
276 case mm_32f_73_op: /* POOL32FXF */
277 switch (insn.mm_fp1_format.op) {
278 case mm_movf0_op:
279 case mm_movf1_op:
280 case mm_movt0_op:
281 case mm_movt1_op:
282 if ((insn.mm_fp1_format.op & 0x7f) ==
283 mm_movf0_op)
284 op = 0;
285 else
286 op = 1;
287 mips32_insn.r_format.opcode = spec_op;
288 mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
289 mips32_insn.r_format.rt =
290 (insn.mm_fp4_format.cc << 2) + op;
291 mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
292 mips32_insn.r_format.re = 0;
293 mips32_insn.r_format.func = movc_op;
294 break;
295 case mm_fcvtd0_op:
296 case mm_fcvtd1_op:
297 case mm_fcvts0_op:
298 case mm_fcvts1_op:
299 if ((insn.mm_fp1_format.op & 0x7f) ==
300 mm_fcvtd0_op) {
301 func = fcvtd_op;
302 fmt = swl_format[insn.mm_fp3_format.fmt];
303 } else {
304 func = fcvts_op;
305 fmt = dwl_format[insn.mm_fp3_format.fmt];
306 }
307 mips32_insn.fp0_format.opcode = cop1_op;
308 mips32_insn.fp0_format.fmt = fmt;
309 mips32_insn.fp0_format.ft = 0;
310 mips32_insn.fp0_format.fs =
311 insn.mm_fp3_format.fs;
312 mips32_insn.fp0_format.fd =
313 insn.mm_fp3_format.rt;
314 mips32_insn.fp0_format.func = func;
315 break;
316 case mm_fmov0_op:
317 case mm_fmov1_op:
318 case mm_fabs0_op:
319 case mm_fabs1_op:
320 case mm_fneg0_op:
321 case mm_fneg1_op:
322 if ((insn.mm_fp1_format.op & 0x7f) ==
323 mm_fmov0_op)
324 func = fmov_op;
325 else if ((insn.mm_fp1_format.op & 0x7f) ==
326 mm_fabs0_op)
327 func = fabs_op;
328 else
329 func = fneg_op;
330 mips32_insn.fp0_format.opcode = cop1_op;
331 mips32_insn.fp0_format.fmt =
332 sdps_format[insn.mm_fp3_format.fmt];
333 mips32_insn.fp0_format.ft = 0;
334 mips32_insn.fp0_format.fs =
335 insn.mm_fp3_format.fs;
336 mips32_insn.fp0_format.fd =
337 insn.mm_fp3_format.rt;
338 mips32_insn.fp0_format.func = func;
339 break;
340 case mm_ffloorl_op:
341 case mm_ffloorw_op:
342 case mm_fceill_op:
343 case mm_fceilw_op:
344 case mm_ftruncl_op:
345 case mm_ftruncw_op:
346 case mm_froundl_op:
347 case mm_froundw_op:
348 case mm_fcvtl_op:
349 case mm_fcvtw_op:
350 if (insn.mm_fp1_format.op == mm_ffloorl_op)
351 func = ffloorl_op;
352 else if (insn.mm_fp1_format.op == mm_ffloorw_op)
353 func = ffloor_op;
354 else if (insn.mm_fp1_format.op == mm_fceill_op)
355 func = fceill_op;
356 else if (insn.mm_fp1_format.op == mm_fceilw_op)
357 func = fceil_op;
358 else if (insn.mm_fp1_format.op == mm_ftruncl_op)
359 func = ftruncl_op;
360 else if (insn.mm_fp1_format.op == mm_ftruncw_op)
361 func = ftrunc_op;
362 else if (insn.mm_fp1_format.op == mm_froundl_op)
363 func = froundl_op;
364 else if (insn.mm_fp1_format.op == mm_froundw_op)
365 func = fround_op;
366 else if (insn.mm_fp1_format.op == mm_fcvtl_op)
367 func = fcvtl_op;
368 else
369 func = fcvtw_op;
370 mips32_insn.fp0_format.opcode = cop1_op;
371 mips32_insn.fp0_format.fmt =
372 sd_format[insn.mm_fp1_format.fmt];
373 mips32_insn.fp0_format.ft = 0;
374 mips32_insn.fp0_format.fs =
375 insn.mm_fp1_format.fs;
376 mips32_insn.fp0_format.fd =
377 insn.mm_fp1_format.rt;
378 mips32_insn.fp0_format.func = func;
379 break;
380 case mm_frsqrt_op:
381 case mm_fsqrt_op:
382 case mm_frecip_op:
383 if (insn.mm_fp1_format.op == mm_frsqrt_op)
384 func = frsqrt_op;
385 else if (insn.mm_fp1_format.op == mm_fsqrt_op)
386 func = fsqrt_op;
387 else
388 func = frecip_op;
389 mips32_insn.fp0_format.opcode = cop1_op;
390 mips32_insn.fp0_format.fmt =
391 sdps_format[insn.mm_fp1_format.fmt];
392 mips32_insn.fp0_format.ft = 0;
393 mips32_insn.fp0_format.fs =
394 insn.mm_fp1_format.fs;
395 mips32_insn.fp0_format.fd =
396 insn.mm_fp1_format.rt;
397 mips32_insn.fp0_format.func = func;
398 break;
399 case mm_mfc1_op:
400 case mm_mtc1_op:
401 case mm_cfc1_op:
402 case mm_ctc1_op:
Steven J. Hill9355e592013-11-07 12:48:29 +0000403 case mm_mfhc1_op:
404 case mm_mthc1_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500405 if (insn.mm_fp1_format.op == mm_mfc1_op)
406 op = mfc_op;
407 else if (insn.mm_fp1_format.op == mm_mtc1_op)
408 op = mtc_op;
409 else if (insn.mm_fp1_format.op == mm_cfc1_op)
410 op = cfc_op;
Steven J. Hill9355e592013-11-07 12:48:29 +0000411 else if (insn.mm_fp1_format.op == mm_ctc1_op)
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500412 op = ctc_op;
Steven J. Hill9355e592013-11-07 12:48:29 +0000413 else if (insn.mm_fp1_format.op == mm_mfhc1_op)
414 op = mfhc_op;
415 else
416 op = mthc_op;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500417 mips32_insn.fp1_format.opcode = cop1_op;
418 mips32_insn.fp1_format.op = op;
419 mips32_insn.fp1_format.rt =
420 insn.mm_fp1_format.rt;
421 mips32_insn.fp1_format.fs =
422 insn.mm_fp1_format.fs;
423 mips32_insn.fp1_format.fd = 0;
424 mips32_insn.fp1_format.func = 0;
425 break;
426 default:
427 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500428 }
429 break;
430 case mm_32f_74_op: /* c.cond.fmt */
431 mips32_insn.fp0_format.opcode = cop1_op;
432 mips32_insn.fp0_format.fmt =
433 sdps_format[insn.mm_fp4_format.fmt];
434 mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
435 mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
436 mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
437 mips32_insn.fp0_format.func =
438 insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
439 break;
440 default:
441 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500442 }
443 break;
444 default:
445 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500446 }
447
448 *insn_ptr = mips32_insn;
449 return 0;
450}
451
452int mm_isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
453 unsigned long *contpc)
454{
455 union mips_instruction insn = (union mips_instruction)dec_insn.insn;
456 int bc_false = 0;
457 unsigned int fcr31;
458 unsigned int bit;
459
David Daneyfe6d2902013-05-24 20:54:09 +0000460 if (!cpu_has_mmips)
461 return 0;
462
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500463 switch (insn.mm_i_format.opcode) {
464 case mm_pool32a_op:
465 if ((insn.mm_i_format.simmediate & MM_POOL32A_MINOR_MASK) ==
466 mm_pool32axf_op) {
467 switch (insn.mm_i_format.simmediate >>
468 MM_POOL32A_MINOR_SHIFT) {
469 case mm_jalr_op:
470 case mm_jalrhb_op:
471 case mm_jalrs_op:
472 case mm_jalrshb_op:
473 if (insn.mm_i_format.rt != 0) /* Not mm_jr */
474 regs->regs[insn.mm_i_format.rt] =
475 regs->cp0_epc +
476 dec_insn.pc_inc +
477 dec_insn.next_pc_inc;
478 *contpc = regs->regs[insn.mm_i_format.rs];
479 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500480 }
481 }
482 break;
483 case mm_pool32i_op:
484 switch (insn.mm_i_format.rt) {
485 case mm_bltzals_op:
486 case mm_bltzal_op:
487 regs->regs[31] = regs->cp0_epc +
488 dec_insn.pc_inc +
489 dec_insn.next_pc_inc;
490 /* Fall through */
491 case mm_bltz_op:
492 if ((long)regs->regs[insn.mm_i_format.rs] < 0)
493 *contpc = regs->cp0_epc +
494 dec_insn.pc_inc +
495 (insn.mm_i_format.simmediate << 1);
496 else
497 *contpc = regs->cp0_epc +
498 dec_insn.pc_inc +
499 dec_insn.next_pc_inc;
500 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500501 case mm_bgezals_op:
502 case mm_bgezal_op:
503 regs->regs[31] = regs->cp0_epc +
504 dec_insn.pc_inc +
505 dec_insn.next_pc_inc;
506 /* Fall through */
507 case mm_bgez_op:
508 if ((long)regs->regs[insn.mm_i_format.rs] >= 0)
509 *contpc = regs->cp0_epc +
510 dec_insn.pc_inc +
511 (insn.mm_i_format.simmediate << 1);
512 else
513 *contpc = regs->cp0_epc +
514 dec_insn.pc_inc +
515 dec_insn.next_pc_inc;
516 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500517 case mm_blez_op:
518 if ((long)regs->regs[insn.mm_i_format.rs] <= 0)
519 *contpc = regs->cp0_epc +
520 dec_insn.pc_inc +
521 (insn.mm_i_format.simmediate << 1);
522 else
523 *contpc = regs->cp0_epc +
524 dec_insn.pc_inc +
525 dec_insn.next_pc_inc;
526 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500527 case mm_bgtz_op:
528 if ((long)regs->regs[insn.mm_i_format.rs] <= 0)
529 *contpc = regs->cp0_epc +
530 dec_insn.pc_inc +
531 (insn.mm_i_format.simmediate << 1);
532 else
533 *contpc = regs->cp0_epc +
534 dec_insn.pc_inc +
535 dec_insn.next_pc_inc;
536 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500537 case mm_bc2f_op:
538 case mm_bc1f_op:
539 bc_false = 1;
540 /* Fall through */
541 case mm_bc2t_op:
542 case mm_bc1t_op:
543 preempt_disable();
544 if (is_fpu_owner())
545 asm volatile("cfc1\t%0,$31" : "=r" (fcr31));
546 else
547 fcr31 = current->thread.fpu.fcr31;
548 preempt_enable();
549
550 if (bc_false)
551 fcr31 = ~fcr31;
552
553 bit = (insn.mm_i_format.rs >> 2);
554 bit += (bit != 0);
555 bit += 23;
556 if (fcr31 & (1 << bit))
557 *contpc = regs->cp0_epc +
558 dec_insn.pc_inc +
559 (insn.mm_i_format.simmediate << 1);
560 else
561 *contpc = regs->cp0_epc +
562 dec_insn.pc_inc + dec_insn.next_pc_inc;
563 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500564 }
565 break;
566 case mm_pool16c_op:
567 switch (insn.mm_i_format.rt) {
568 case mm_jalr16_op:
569 case mm_jalrs16_op:
570 regs->regs[31] = regs->cp0_epc +
571 dec_insn.pc_inc + dec_insn.next_pc_inc;
572 /* Fall through */
573 case mm_jr16_op:
574 *contpc = regs->regs[insn.mm_i_format.rs];
575 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500576 }
577 break;
578 case mm_beqz16_op:
579 if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] == 0)
580 *contpc = regs->cp0_epc +
581 dec_insn.pc_inc +
582 (insn.mm_b1_format.simmediate << 1);
583 else
584 *contpc = regs->cp0_epc +
585 dec_insn.pc_inc + dec_insn.next_pc_inc;
586 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500587 case mm_bnez16_op:
588 if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] != 0)
589 *contpc = regs->cp0_epc +
590 dec_insn.pc_inc +
591 (insn.mm_b1_format.simmediate << 1);
592 else
593 *contpc = regs->cp0_epc +
594 dec_insn.pc_inc + dec_insn.next_pc_inc;
595 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500596 case mm_b16_op:
597 *contpc = regs->cp0_epc + dec_insn.pc_inc +
598 (insn.mm_b0_format.simmediate << 1);
599 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500600 case mm_beq32_op:
601 if (regs->regs[insn.mm_i_format.rs] ==
602 regs->regs[insn.mm_i_format.rt])
603 *contpc = regs->cp0_epc +
604 dec_insn.pc_inc +
605 (insn.mm_i_format.simmediate << 1);
606 else
607 *contpc = regs->cp0_epc +
608 dec_insn.pc_inc +
609 dec_insn.next_pc_inc;
610 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500611 case mm_bne32_op:
612 if (regs->regs[insn.mm_i_format.rs] !=
613 regs->regs[insn.mm_i_format.rt])
614 *contpc = regs->cp0_epc +
615 dec_insn.pc_inc +
616 (insn.mm_i_format.simmediate << 1);
617 else
618 *contpc = regs->cp0_epc +
619 dec_insn.pc_inc + dec_insn.next_pc_inc;
620 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500621 case mm_jalx32_op:
622 regs->regs[31] = regs->cp0_epc +
623 dec_insn.pc_inc + dec_insn.next_pc_inc;
624 *contpc = regs->cp0_epc + dec_insn.pc_inc;
625 *contpc >>= 28;
626 *contpc <<= 28;
627 *contpc |= (insn.j_format.target << 2);
628 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500629 case mm_jals32_op:
630 case mm_jal32_op:
631 regs->regs[31] = regs->cp0_epc +
632 dec_insn.pc_inc + dec_insn.next_pc_inc;
633 /* Fall through */
634 case mm_j32_op:
635 *contpc = regs->cp0_epc + dec_insn.pc_inc;
636 *contpc >>= 27;
637 *contpc <<= 27;
638 *contpc |= (insn.j_format.target << 1);
639 set_isa16_mode(*contpc);
640 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500641 }
642 return 0;
643}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645/*
646 * Redundant with logic already in kernel/branch.c,
647 * embedded in compute_return_epc. At some point,
648 * a single subroutine should be used across both
649 * modules.
650 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500651static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
652 unsigned long *contpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500654 union mips_instruction insn = (union mips_instruction)dec_insn.insn;
655 unsigned int fcr31;
656 unsigned int bit = 0;
657
658 switch (insn.i_format.opcode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 case spec_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500660 switch (insn.r_format.func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 case jalr_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500662 regs->regs[insn.r_format.rd] =
663 regs->cp0_epc + dec_insn.pc_inc +
664 dec_insn.next_pc_inc;
665 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 case jr_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500667 *contpc = regs->regs[insn.r_format.rs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return 1;
669 }
670 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 case bcond_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500672 switch (insn.i_format.rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 case bltzal_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 case bltzall_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500675 regs->regs[31] = regs->cp0_epc +
676 dec_insn.pc_inc +
677 dec_insn.next_pc_inc;
678 /* Fall through */
679 case bltz_op:
680 case bltzl_op:
681 if ((long)regs->regs[insn.i_format.rs] < 0)
682 *contpc = regs->cp0_epc +
683 dec_insn.pc_inc +
684 (insn.i_format.simmediate << 2);
685 else
686 *contpc = regs->cp0_epc +
687 dec_insn.pc_inc +
688 dec_insn.next_pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500690 case bgezal_op:
691 case bgezall_op:
692 regs->regs[31] = regs->cp0_epc +
693 dec_insn.pc_inc +
694 dec_insn.next_pc_inc;
695 /* Fall through */
696 case bgez_op:
697 case bgezl_op:
698 if ((long)regs->regs[insn.i_format.rs] >= 0)
699 *contpc = regs->cp0_epc +
700 dec_insn.pc_inc +
701 (insn.i_format.simmediate << 2);
702 else
703 *contpc = regs->cp0_epc +
704 dec_insn.pc_inc +
705 dec_insn.next_pc_inc;
706 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 case jalx_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500710 set_isa16_mode(bit);
711 case jal_op:
712 regs->regs[31] = regs->cp0_epc +
713 dec_insn.pc_inc +
714 dec_insn.next_pc_inc;
715 /* Fall through */
716 case j_op:
717 *contpc = regs->cp0_epc + dec_insn.pc_inc;
718 *contpc >>= 28;
719 *contpc <<= 28;
720 *contpc |= (insn.j_format.target << 2);
721 /* Set microMIPS mode bit: XOR for jalx. */
722 *contpc ^= bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500724 case beq_op:
725 case beql_op:
726 if (regs->regs[insn.i_format.rs] ==
727 regs->regs[insn.i_format.rt])
728 *contpc = regs->cp0_epc +
729 dec_insn.pc_inc +
730 (insn.i_format.simmediate << 2);
731 else
732 *contpc = regs->cp0_epc +
733 dec_insn.pc_inc +
734 dec_insn.next_pc_inc;
735 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500736 case bne_op:
737 case bnel_op:
738 if (regs->regs[insn.i_format.rs] !=
739 regs->regs[insn.i_format.rt])
740 *contpc = regs->cp0_epc +
741 dec_insn.pc_inc +
742 (insn.i_format.simmediate << 2);
743 else
744 *contpc = regs->cp0_epc +
745 dec_insn.pc_inc +
746 dec_insn.next_pc_inc;
747 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500748 case blez_op:
749 case blezl_op:
750 if ((long)regs->regs[insn.i_format.rs] <= 0)
751 *contpc = regs->cp0_epc +
752 dec_insn.pc_inc +
753 (insn.i_format.simmediate << 2);
754 else
755 *contpc = regs->cp0_epc +
756 dec_insn.pc_inc +
757 dec_insn.next_pc_inc;
758 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500759 case bgtz_op:
760 case bgtzl_op:
761 if ((long)regs->regs[insn.i_format.rs] > 0)
762 *contpc = regs->cp0_epc +
763 dec_insn.pc_inc +
764 (insn.i_format.simmediate << 2);
765 else
766 *contpc = regs->cp0_epc +
767 dec_insn.pc_inc +
768 dec_insn.next_pc_inc;
769 return 1;
David Daneyc26d4212013-08-19 12:10:34 -0700770#ifdef CONFIG_CPU_CAVIUM_OCTEON
771 case lwc2_op: /* This is bbit0 on Octeon */
772 if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
773 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
774 else
775 *contpc = regs->cp0_epc + 8;
776 return 1;
777 case ldc2_op: /* This is bbit032 on Octeon */
778 if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
779 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
780 else
781 *contpc = regs->cp0_epc + 8;
782 return 1;
783 case swc2_op: /* This is bbit1 on Octeon */
784 if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
785 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
786 else
787 *contpc = regs->cp0_epc + 8;
788 return 1;
789 case sdc2_op: /* This is bbit132 on Octeon */
790 if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
791 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
792 else
793 *contpc = regs->cp0_epc + 8;
794 return 1;
795#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 case cop0_op:
797 case cop1_op:
798 case cop2_op:
799 case cop1x_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500800 if (insn.i_format.rs == bc_op) {
801 preempt_disable();
802 if (is_fpu_owner())
803 asm volatile("cfc1\t%0,$31" : "=r" (fcr31));
804 else
805 fcr31 = current->thread.fpu.fcr31;
806 preempt_enable();
807
808 bit = (insn.i_format.rt >> 2);
809 bit += (bit != 0);
810 bit += 23;
811 switch (insn.i_format.rt & 3) {
812 case 0: /* bc1f */
813 case 2: /* bc1fl */
814 if (~fcr31 & (1 << bit))
815 *contpc = regs->cp0_epc +
816 dec_insn.pc_inc +
817 (insn.i_format.simmediate << 2);
818 else
819 *contpc = regs->cp0_epc +
820 dec_insn.pc_inc +
821 dec_insn.next_pc_inc;
822 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500823 case 1: /* bc1t */
824 case 3: /* bc1tl */
825 if (fcr31 & (1 << bit))
826 *contpc = regs->cp0_epc +
827 dec_insn.pc_inc +
828 (insn.i_format.simmediate << 2);
829 else
830 *contpc = regs->cp0_epc +
831 dec_insn.pc_inc +
832 dec_insn.next_pc_inc;
833 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500834 }
835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 break;
837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return 0;
839}
840
841/*
842 * In the Linux kernel, we support selection of FPR format on the
Ralf Baechle70342282013-01-22 12:59:30 +0100843 * basis of the Status.FR bit. If an FPU is not present, the FR bit
David Daneyda0bac32009-11-02 11:33:46 -0800844 * is hardwired to zero, which would imply a 32-bit FPU even for
Paul Burton597ce172013-11-22 13:12:07 +0000845 * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
Ralf Baechle51d943f2012-08-15 19:42:19 +0200846 * FPU emu is slow and bulky and optimizing this function offers fairly
847 * sizeable benefits so we try to be clever and make this function return
848 * a constant whenever possible, that is on 64-bit kernels without O32
Paul Burton597ce172013-11-22 13:12:07 +0000849 * compatibility enabled and on 32-bit without 64-bit FPU support.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 */
David Daneyda0bac32009-11-02 11:33:46 -0800851static inline int cop1_64bit(struct pt_regs *xcp)
852{
Ralf Baechle08a07902014-04-19 13:11:37 +0200853 if (config_enabled(CONFIG_64BIT) && !config_enabled(CONFIG_MIPS32_O32))
854 return 1;
855 else if (config_enabled(CONFIG_32BIT) &&
856 !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
857 return 0;
858
Paul Burton597ce172013-11-22 13:12:07 +0000859 return !test_thread_flag(TIF_32BIT_FPREGS);
David Daneyda0bac32009-11-02 11:33:46 -0800860}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200862#define SIFROMREG(si, x) \
863do { \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000864 if (cop1_64bit(xcp)) \
865 (si) = get_fpr32(&ctx->fpr[x], 0); \
866 else \
867 (si) = get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
868} while (0)
David Daneyda0bac32009-11-02 11:33:46 -0800869
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200870#define SITOREG(si, x) \
871do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000872 if (cop1_64bit(xcp)) { \
873 unsigned i; \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000874 set_fpr32(&ctx->fpr[x], 0, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000875 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
876 set_fpr32(&ctx->fpr[x], i, 0); \
877 } else { \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000878 set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000879 } \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000880} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Paul Burtonbbd426f2014-02-13 11:26:41 +0000882#define SIFROMHREG(si, x) ((si) = get_fpr32(&ctx->fpr[x], 1))
Paul Burtonef1c47a2014-01-27 17:14:47 +0000883
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200884#define SITOHREG(si, x) \
885do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000886 unsigned i; \
887 set_fpr32(&ctx->fpr[x], 1, si); \
888 for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
889 set_fpr32(&ctx->fpr[x], i, 0); \
890} while (0)
Leonid Yegoshin1ac944002013-11-07 12:48:28 +0000891
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200892#define DIFROMREG(di, x) \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000893 ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
894
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200895#define DITOREG(di, x) \
896do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000897 unsigned fpr, i; \
898 fpr = (x) & ~(cop1_64bit(xcp) == 0); \
899 set_fpr64(&ctx->fpr[fpr], 0, di); \
900 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
901 set_fpr64(&ctx->fpr[fpr], i, 0); \
902} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Ralf Baechle21a151d2007-10-11 23:46:15 +0100904#define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
905#define SPTOREG(sp, x) SITOREG((sp).bits, x)
906#define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
907#define DPTOREG(dp, x) DITOREG((dp).bits, x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909/*
910 * Emulate the single floating point instruction pointed at by EPC.
911 * Two instructions if the instruction is in a branch delay slot.
912 */
913
David Daney515b0292010-10-21 16:32:26 -0700914static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500915 struct mm_decoded_insn dec_insn, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 mips_instruction ir;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500918 unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 unsigned int cond;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500920 int pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 /* XXX NEC Vr54xx bug workaround */
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200923 if (delay_slot(xcp)) {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500924 if (dec_insn.micro_mips_mode) {
925 if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200926 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500927 } else {
928 if (!isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200929 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500930 }
931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200933 if (delay_slot(xcp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 /*
935 * The instruction to be emulated is in a branch delay slot
Ralf Baechle70342282013-01-22 12:59:30 +0100936 * which means that we have to emulate the branch instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 * BEFORE we do the cop1 instruction.
938 *
939 * This branch could be a COP1 branch, but in that case we
940 * would have had a trap for that instruction, and would not
941 * come through this route.
942 *
943 * Linux MIPS branch emulator operates on context, updating the
944 * cp0_epc.
945 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500946 ir = dec_insn.next_insn; /* process delay slot instr */
947 pc_inc = dec_insn.next_pc_inc;
Ralf Baechle333d1f62005-02-28 17:55:57 +0000948 } else {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500949 ir = dec_insn.insn; /* process current instr */
950 pc_inc = dec_insn.pc_inc;
951 }
952
953 /*
954 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
955 * instructions, we want to convert microMIPS FPU instructions
956 * into MIPS32 instructions so that we could reuse all of the
957 * FPU emulation code.
958 *
959 * NOTE: We cannot do this for branch instructions since they
960 * are not a subset. Example: Cannot emulate a 16-bit
961 * aligned target address with a MIPS32 instruction.
962 */
963 if (dec_insn.micro_mips_mode) {
964 /*
965 * If next instruction is a 16-bit instruction, then it
966 * it cannot be a FPU instruction. This could happen
967 * since we can be called for non-FPU instructions.
968 */
969 if ((pc_inc == 2) ||
970 (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
971 == SIGILL))
972 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974
975 emul:
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200976 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
David Daneyb6ee75e2009-11-05 11:34:26 -0800977 MIPS_FPU_EMU_INC_STATS(emulated);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 switch (MIPSInst_OPCODE(ir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 case ldc1_op:{
Ralf Baechle3fccc012005-10-23 13:58:21 +0100980 u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 MIPSInst_SIMM(ir));
982 u64 val;
983
David Daneyb6ee75e2009-11-05 11:34:26 -0800984 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -0700985
986 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800987 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -0700988 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return SIGBUS;
990 }
David Daney515b0292010-10-21 16:32:26 -0700991 if (__get_user(val, va)) {
992 MIPS_FPU_EMU_INC_STATS(errors);
993 *fault_addr = va;
994 return SIGSEGV;
995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 DITOREG(val, MIPSInst_RT(ir));
997 break;
998 }
999
1000 case sdc1_op:{
Ralf Baechle3fccc012005-10-23 13:58:21 +01001001 u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 MIPSInst_SIMM(ir));
1003 u64 val;
1004
David Daneyb6ee75e2009-11-05 11:34:26 -08001005 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 DIFROMREG(val, MIPSInst_RT(ir));
David Daney515b0292010-10-21 16:32:26 -07001007 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001008 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001009 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return SIGBUS;
1011 }
David Daney515b0292010-10-21 16:32:26 -07001012 if (__put_user(val, va)) {
1013 MIPS_FPU_EMU_INC_STATS(errors);
1014 *fault_addr = va;
1015 return SIGSEGV;
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 break;
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 case lwc1_op:{
Ralf Baechle3fccc012005-10-23 13:58:21 +01001021 u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 MIPSInst_SIMM(ir));
1023 u32 val;
1024
David Daneyb6ee75e2009-11-05 11:34:26 -08001025 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001026 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001027 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001028 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return SIGBUS;
1030 }
David Daney515b0292010-10-21 16:32:26 -07001031 if (__get_user(val, va)) {
1032 MIPS_FPU_EMU_INC_STATS(errors);
1033 *fault_addr = va;
1034 return SIGSEGV;
1035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 SITOREG(val, MIPSInst_RT(ir));
1037 break;
1038 }
1039
1040 case swc1_op:{
Ralf Baechle3fccc012005-10-23 13:58:21 +01001041 u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 MIPSInst_SIMM(ir));
1043 u32 val;
1044
David Daneyb6ee75e2009-11-05 11:34:26 -08001045 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 SIFROMREG(val, MIPSInst_RT(ir));
David Daney515b0292010-10-21 16:32:26 -07001047 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001048 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001049 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return SIGBUS;
1051 }
David Daney515b0292010-10-21 16:32:26 -07001052 if (__put_user(val, va)) {
1053 MIPS_FPU_EMU_INC_STATS(errors);
1054 *fault_addr = va;
1055 return SIGSEGV;
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 break;
1058 }
1059
1060 case cop1_op:
1061 switch (MIPSInst_RS(ir)) {
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 case dmfc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001064 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1065 return SIGILL;
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /* copregister fs -> gpr[rt] */
1068 if (MIPSInst_RT(ir) != 0) {
1069 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1070 MIPSInst_RD(ir));
1071 }
1072 break;
1073
1074 case dmtc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001075 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1076 return SIGILL;
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 /* copregister fs <- rt */
1079 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1080 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001082 case mfhc_op:
1083 if (!cpu_has_mips_r2)
1084 goto sigill;
1085
1086 /* copregister rd -> gpr[rt] */
1087 if (MIPSInst_RT(ir) != 0) {
1088 SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
1089 MIPSInst_RD(ir));
1090 }
1091 break;
1092
1093 case mthc_op:
1094 if (!cpu_has_mips_r2)
1095 goto sigill;
1096
1097 /* copregister rd <- gpr[rt] */
1098 SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1099 break;
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 case mfc_op:
1102 /* copregister rd -> gpr[rt] */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (MIPSInst_RT(ir) != 0) {
1104 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1105 MIPSInst_RD(ir));
1106 }
1107 break;
1108
1109 case mtc_op:
1110 /* copregister rd <- rt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1112 break;
1113
1114 case cfc_op:{
1115 /* cop control register rd -> gpr[rt] */
1116 u32 value;
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (MIPSInst_RD(ir) == FPCREG_CSR) {
1119 value = ctx->fcr31;
Shane McDonald3f135532010-05-07 00:02:09 -06001120 value = (value & ~FPU_CSR_RM) |
1121 mips_rm[modeindex(value)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122#ifdef CSRTRACE
1123 printk("%p gpr[%d]<-csr=%08x\n",
Ralf Baechle333d1f62005-02-28 17:55:57 +00001124 (void *) (xcp->cp0_epc),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 MIPSInst_RT(ir), value);
1126#endif
1127 }
1128 else if (MIPSInst_RD(ir) == FPCREG_RID)
1129 value = 0;
1130 else
1131 value = 0;
1132 if (MIPSInst_RT(ir))
1133 xcp->regs[MIPSInst_RT(ir)] = value;
1134 break;
1135 }
1136
1137 case ctc_op:{
1138 /* copregister rd <- rt */
1139 u32 value;
1140
1141 if (MIPSInst_RT(ir) == 0)
1142 value = 0;
1143 else
1144 value = xcp->regs[MIPSInst_RT(ir)];
1145
1146 /* we only have one writable control reg
1147 */
1148 if (MIPSInst_RD(ir) == FPCREG_CSR) {
1149#ifdef CSRTRACE
1150 printk("%p gpr[%d]->csr=%08x\n",
Ralf Baechle333d1f62005-02-28 17:55:57 +00001151 (void *) (xcp->cp0_epc),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 MIPSInst_RT(ir), value);
1153#endif
Shane McDonald95e8f632010-05-06 23:26:57 -06001154
1155 /*
1156 * Don't write reserved bits,
1157 * and convert to ieee library modes
1158 */
1159 ctx->fcr31 = (value &
1160 ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
1161 ieee_rm[modeindex(value)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1164 return SIGFPE;
1165 }
1166 break;
1167 }
1168
1169 case bc_op:{
Ralf Baechle08a07902014-04-19 13:11:37 +02001170 unsigned int cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 int likely = 0;
1172
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001173 if (delay_slot(xcp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return SIGILL;
1175
Ralf Baechle08a07902014-04-19 13:11:37 +02001176 if (cpu_has_mips_4_5_r)
1177 cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
1178 else
1179 cbit = FPU_CSR_COND;
1180 cond = ctx->fcr31 & cbit;
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 switch (MIPSInst_RT(ir) & 3) {
1183 case bcfl_op:
1184 likely = 1;
1185 case bcf_op:
1186 cond = !cond;
1187 break;
1188 case bctl_op:
1189 likely = 1;
1190 case bct_op:
1191 break;
1192 default:
1193 /* thats an illegal instruction */
1194 return SIGILL;
1195 }
1196
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001197 set_delay_slot(xcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (cond) {
1199 /* branch taken: emulate dslot
1200 * instruction
1201 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001202 xcp->cp0_epc += dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001204 contpc = MIPSInst_SIMM(ir);
1205 ir = dec_insn.next_insn;
1206 if (dec_insn.micro_mips_mode) {
1207 contpc = (xcp->cp0_epc + (contpc << 1));
1208
1209 /* If 16-bit instruction, not FPU. */
1210 if ((dec_insn.next_pc_inc == 2) ||
1211 (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1212
1213 /*
1214 * Since this instruction will
1215 * be put on the stack with
1216 * 32-bit words, get around
1217 * this problem by putting a
1218 * NOP16 as the second one.
1219 */
1220 if (dec_insn.next_pc_inc == 2)
1221 ir = (ir & (~0xffff)) | MM_NOP16;
1222
1223 /*
1224 * Single step the non-CP1
1225 * instruction in the dslot.
1226 */
1227 return mips_dsemul(xcp, ir, contpc);
1228 }
1229 } else
1230 contpc = (xcp->cp0_epc + (contpc << 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 switch (MIPSInst_OPCODE(ir)) {
1233 case lwc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001234 goto emul;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 case swc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001236 goto emul;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 case ldc1_op:
1238 case sdc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001239 if (cpu_has_mips_2_3_4_5 ||
1240 cpu_has_mips64)
1241 goto emul;
1242
1243 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 goto emul;
Ralf Baechle08a07902014-04-19 13:11:37 +02001245 case cop1_op:
1246 goto emul;
1247 case cop1x_op:
1248 if (cpu_has_mips_4_5 || cpu_has_mips64)
1249 /* its one of ours */
1250 goto emul;
1251
1252 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 case spec_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001254 if (!cpu_has_mips_4_5_r)
1255 return SIGILL;
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (MIPSInst_FUNC(ir) == movc_op)
1258 goto emul;
1259 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261
1262 /*
1263 * Single step the non-cp1
1264 * instruction in the dslot
1265 */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +09001266 return mips_dsemul(xcp, ir, contpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268 else {
1269 /* branch not taken */
1270 if (likely) {
1271 /*
1272 * branch likely nullifies
1273 * dslot if not taken
1274 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001275 xcp->cp0_epc += dec_insn.pc_inc;
1276 contpc += dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /*
1278 * else continue & execute
1279 * dslot as normal insn
1280 */
1281 }
1282 }
1283 break;
1284 }
1285
1286 default:
1287 if (!(MIPSInst_RS(ir) & 0x10))
1288 return SIGILL;
1289 {
1290 int sig;
1291
1292 /* a real fpu computation instruction */
1293 if ((sig = fpu_emu(xcp, ctx, ir)))
1294 return sig;
1295 }
1296 }
1297 break;
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 case cop1x_op:{
Ralf Baechle08a07902014-04-19 13:11:37 +02001300 int sig;
1301
1302 if (!cpu_has_mips_4_5 && !cpu_has_mips64)
1303 return SIGILL;
1304
1305 sig = fpux_emu(xcp, ctx, ir, fault_addr);
David Daney515b0292010-10-21 16:32:26 -07001306 if (sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return sig;
1308 break;
1309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 case spec_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001312 if (!cpu_has_mips_4_5_r)
1313 return SIGILL;
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (MIPSInst_FUNC(ir) != movc_op)
1316 return SIGILL;
1317 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
1318 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
1319 xcp->regs[MIPSInst_RD(ir)] =
1320 xcp->regs[MIPSInst_RS(ir)];
1321 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 default:
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001323sigill:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return SIGILL;
1325 }
1326
1327 /* we did it !! */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +09001328 xcp->cp0_epc = contpc;
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001329 clear_delay_slot(xcp);
Ralf Baechle333d1f62005-02-28 17:55:57 +00001330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return 0;
1332}
1333
1334/*
1335 * Conversion table from MIPS compare ops 48-63
1336 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
1337 */
1338static const unsigned char cmptab[8] = {
1339 0, /* cmp_0 (sig) cmp_sf */
1340 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
1341 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
1342 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
1343 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
1344 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
1345 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
1346 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
1347};
1348
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350/*
1351 * Additional MIPS4 instructions
1352 */
1353
Ralf Baechle47fa0c02014-04-16 11:00:12 +02001354#define DEF3OP(name, p, f1, f2, f3) \
1355static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \
1356 union ieee754##p s, union ieee754##p t) \
1357{ \
1358 struct _ieee754_csr ieee754_csr_save; \
1359 s = f1(s, t); \
1360 ieee754_csr_save = ieee754_csr; \
1361 s = f2(s, r); \
1362 ieee754_csr_save.cx |= ieee754_csr.cx; \
1363 ieee754_csr_save.sx |= ieee754_csr.sx; \
1364 s = f3(s); \
1365 ieee754_csr.cx |= ieee754_csr_save.cx; \
1366 ieee754_csr.sx |= ieee754_csr_save.sx; \
1367 return s; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001370static union ieee754dp fpemu_dp_recip(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 return ieee754dp_div(ieee754dp_one(0), d);
1373}
1374
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001375static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
1378}
1379
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001380static union ieee754sp fpemu_sp_recip(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 return ieee754sp_div(ieee754sp_one(0), s);
1383}
1384
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001385static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
1387 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
1388}
1389
Ralf Baechle21a151d2007-10-11 23:46:15 +01001390DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
1391DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
1393DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
Ralf Baechle21a151d2007-10-11 23:46:15 +01001394DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
1395DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
1397DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
1398
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001399static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07001400 mips_instruction ir, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 unsigned rcsr = 0; /* resulting csr */
1403
David Daneyb6ee75e2009-11-05 11:34:26 -08001404 MIPS_FPU_EMU_INC_STATS(cp1xops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 switch (MIPSInst_FMA_FFMT(ir)) {
1407 case s_fmt:{ /* 0 */
1408
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001409 union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
1410 union ieee754sp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001411 u32 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 u32 val;
1413
1414 switch (MIPSInst_FUNC(ir)) {
1415 case lwxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001416 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 xcp->regs[MIPSInst_FT(ir)]);
1418
David Daneyb6ee75e2009-11-05 11:34:26 -08001419 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001420 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001421 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001422 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return SIGBUS;
1424 }
David Daney515b0292010-10-21 16:32:26 -07001425 if (__get_user(val, va)) {
1426 MIPS_FPU_EMU_INC_STATS(errors);
1427 *fault_addr = va;
1428 return SIGSEGV;
1429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 SITOREG(val, MIPSInst_FD(ir));
1431 break;
1432
1433 case swxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001434 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 xcp->regs[MIPSInst_FT(ir)]);
1436
David Daneyb6ee75e2009-11-05 11:34:26 -08001437 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 SIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001440 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1441 MIPS_FPU_EMU_INC_STATS(errors);
1442 *fault_addr = va;
1443 return SIGBUS;
1444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (put_user(val, va)) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001446 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001447 *fault_addr = va;
1448 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450 break;
1451
1452 case madd_s_op:
1453 handler = fpemu_sp_madd;
1454 goto scoptop;
1455 case msub_s_op:
1456 handler = fpemu_sp_msub;
1457 goto scoptop;
1458 case nmadd_s_op:
1459 handler = fpemu_sp_nmadd;
1460 goto scoptop;
1461 case nmsub_s_op:
1462 handler = fpemu_sp_nmsub;
1463 goto scoptop;
1464
1465 scoptop:
1466 SPFROMREG(fr, MIPSInst_FR(ir));
1467 SPFROMREG(fs, MIPSInst_FS(ir));
1468 SPFROMREG(ft, MIPSInst_FT(ir));
1469 fd = (*handler) (fr, fs, ft);
1470 SPTOREG(fd, MIPSInst_FD(ir));
1471
1472 copcsr:
1473 if (ieee754_cxtest(IEEE754_INEXACT))
1474 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1475 if (ieee754_cxtest(IEEE754_UNDERFLOW))
1476 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1477 if (ieee754_cxtest(IEEE754_OVERFLOW))
1478 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1479 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
1480 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1481
1482 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1484 /*printk ("SIGFPE: fpu csr = %08x\n",
1485 ctx->fcr31); */
1486 return SIGFPE;
1487 }
1488
1489 break;
1490
1491 default:
1492 return SIGILL;
1493 }
1494 break;
1495 }
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 case d_fmt:{ /* 1 */
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001498 union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
1499 union ieee754dp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001500 u64 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 u64 val;
1502
1503 switch (MIPSInst_FUNC(ir)) {
1504 case ldxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001505 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 xcp->regs[MIPSInst_FT(ir)]);
1507
David Daneyb6ee75e2009-11-05 11:34:26 -08001508 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001509 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001510 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001511 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return SIGBUS;
1513 }
David Daney515b0292010-10-21 16:32:26 -07001514 if (__get_user(val, va)) {
1515 MIPS_FPU_EMU_INC_STATS(errors);
1516 *fault_addr = va;
1517 return SIGSEGV;
1518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 DITOREG(val, MIPSInst_FD(ir));
1520 break;
1521
1522 case sdxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001523 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 xcp->regs[MIPSInst_FT(ir)]);
1525
David Daneyb6ee75e2009-11-05 11:34:26 -08001526 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 DIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001528 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001529 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001530 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return SIGBUS;
1532 }
David Daney515b0292010-10-21 16:32:26 -07001533 if (__put_user(val, va)) {
1534 MIPS_FPU_EMU_INC_STATS(errors);
1535 *fault_addr = va;
1536 return SIGSEGV;
1537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 break;
1539
1540 case madd_d_op:
1541 handler = fpemu_dp_madd;
1542 goto dcoptop;
1543 case msub_d_op:
1544 handler = fpemu_dp_msub;
1545 goto dcoptop;
1546 case nmadd_d_op:
1547 handler = fpemu_dp_nmadd;
1548 goto dcoptop;
1549 case nmsub_d_op:
1550 handler = fpemu_dp_nmsub;
1551 goto dcoptop;
1552
1553 dcoptop:
1554 DPFROMREG(fr, MIPSInst_FR(ir));
1555 DPFROMREG(fs, MIPSInst_FS(ir));
1556 DPFROMREG(ft, MIPSInst_FT(ir));
1557 fd = (*handler) (fr, fs, ft);
1558 DPTOREG(fd, MIPSInst_FD(ir));
1559 goto copcsr;
1560
1561 default:
1562 return SIGILL;
1563 }
1564 break;
1565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001567 case 0x3:
1568 if (MIPSInst_FUNC(ir) != pfetch_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return SIGILL;
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 /* ignore prefx operation */
1572 break;
1573
1574 default:
1575 return SIGILL;
1576 }
1577
1578 return 0;
1579}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581
1582
1583/*
1584 * Emulate a single COP1 arithmetic instruction.
1585 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001586static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 mips_instruction ir)
1588{
1589 int rfmt; /* resulting format */
1590 unsigned rcsr = 0; /* resulting csr */
1591 unsigned cond;
1592 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001593 union ieee754dp d;
1594 union ieee754sp s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 int w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 s64 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 } rv; /* resulting value */
1598
David Daneyb6ee75e2009-11-05 11:34:26 -08001599 MIPS_FPU_EMU_INC_STATS(cp1ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
1601 case s_fmt:{ /* 0 */
1602 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001603 union ieee754sp(*b) (union ieee754sp, union ieee754sp);
1604 union ieee754sp(*u) (union ieee754sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 } handler;
1606
1607 switch (MIPSInst_FUNC(ir)) {
1608 /* binary ops */
1609 case fadd_op:
1610 handler.b = ieee754sp_add;
1611 goto scopbop;
1612 case fsub_op:
1613 handler.b = ieee754sp_sub;
1614 goto scopbop;
1615 case fmul_op:
1616 handler.b = ieee754sp_mul;
1617 goto scopbop;
1618 case fdiv_op:
1619 handler.b = ieee754sp_div;
1620 goto scopbop;
1621
1622 /* unary ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 case fsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001624 if (!cpu_has_mips_4_5_r)
1625 return SIGILL;
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 handler.u = ieee754sp_sqrt;
1628 goto scopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02001629 /*
1630 * Note that on some MIPS IV implementations such as the
1631 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1632 * achieve full IEEE-754 accuracy - however this emulator does.
1633 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 case frsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001635 if (!cpu_has_mips_4_5_r2)
1636 return SIGILL;
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 handler.u = fpemu_sp_rsqrt;
1639 goto scopuop;
1640 case frecip_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001641 if (!cpu_has_mips_4_5_r2)
1642 return SIGILL;
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 handler.u = fpemu_sp_recip;
1645 goto scopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02001646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001648 if (!cpu_has_mips_4_5_r)
1649 return SIGILL;
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1652 if (((ctx->fcr31 & cond) != 0) !=
1653 ((MIPSInst_FT(ir) & 1) != 0))
1654 return 0;
1655 SPFROMREG(rv.s, MIPSInst_FS(ir));
1656 break;
1657 case fmovz_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001658 if (!cpu_has_mips_4_5_r)
1659 return SIGILL;
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1662 return 0;
1663 SPFROMREG(rv.s, MIPSInst_FS(ir));
1664 break;
1665 case fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001666 if (!cpu_has_mips_4_5_r)
1667 return SIGILL;
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1670 return 0;
1671 SPFROMREG(rv.s, MIPSInst_FS(ir));
1672 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 case fabs_op:
1674 handler.u = ieee754sp_abs;
1675 goto scopuop;
1676 case fneg_op:
1677 handler.u = ieee754sp_neg;
1678 goto scopuop;
1679 case fmov_op:
1680 /* an easy one */
1681 SPFROMREG(rv.s, MIPSInst_FS(ir));
1682 goto copcsr;
1683
1684 /* binary op on handler */
1685 scopbop:
1686 {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001687 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 SPFROMREG(fs, MIPSInst_FS(ir));
1690 SPFROMREG(ft, MIPSInst_FT(ir));
1691
1692 rv.s = (*handler.b) (fs, ft);
1693 goto copcsr;
1694 }
1695 scopuop:
1696 {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001697 union ieee754sp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 SPFROMREG(fs, MIPSInst_FS(ir));
1700 rv.s = (*handler.u) (fs);
1701 goto copcsr;
1702 }
1703 copcsr:
1704 if (ieee754_cxtest(IEEE754_INEXACT))
1705 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1706 if (ieee754_cxtest(IEEE754_UNDERFLOW))
1707 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1708 if (ieee754_cxtest(IEEE754_OVERFLOW))
1709 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1710 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE))
1711 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
1712 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
1713 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1714 break;
1715
1716 /* unary conv ops */
1717 case fcvts_op:
1718 return SIGILL; /* not defined */
1719 case fcvtd_op:{
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001720 union ieee754sp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 SPFROMREG(fs, MIPSInst_FS(ir));
1723 rv.d = ieee754dp_fsp(fs);
1724 rfmt = d_fmt;
1725 goto copcsr;
1726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 case fcvtw_op:{
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001728 union ieee754sp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 SPFROMREG(fs, MIPSInst_FS(ir));
1731 rv.w = ieee754sp_tint(fs);
1732 rfmt = w_fmt;
1733 goto copcsr;
1734 }
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 case fround_op:
1737 case ftrunc_op:
1738 case fceil_op:
1739 case ffloor_op:{
1740 unsigned int oldrm = ieee754_csr.rm;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001741 union ieee754sp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Ralf Baechle08a07902014-04-19 13:11:37 +02001743 if (!cpu_has_mips_2_3_4_5 && !cpu_has_mips64)
1744 return SIGILL;
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 SPFROMREG(fs, MIPSInst_FS(ir));
Shane McDonald3f135532010-05-07 00:02:09 -06001747 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 rv.w = ieee754sp_tint(fs);
1749 ieee754_csr.rm = oldrm;
1750 rfmt = w_fmt;
1751 goto copcsr;
1752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 case fcvtl_op:{
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001755 union ieee754sp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Ralf Baechle08a07902014-04-19 13:11:37 +02001757 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1758 return SIGILL;
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 SPFROMREG(fs, MIPSInst_FS(ir));
1761 rv.l = ieee754sp_tlong(fs);
1762 rfmt = l_fmt;
1763 goto copcsr;
1764 }
1765
1766 case froundl_op:
1767 case ftruncl_op:
1768 case fceill_op:
1769 case ffloorl_op:{
1770 unsigned int oldrm = ieee754_csr.rm;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001771 union ieee754sp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Ralf Baechle08a07902014-04-19 13:11:37 +02001773 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1774 return SIGILL;
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 SPFROMREG(fs, MIPSInst_FS(ir));
Shane McDonald3f135532010-05-07 00:02:09 -06001777 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 rv.l = ieee754sp_tlong(fs);
1779 ieee754_csr.rm = oldrm;
1780 rfmt = l_fmt;
1781 goto copcsr;
1782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 default:
1785 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1786 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001787 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 SPFROMREG(fs, MIPSInst_FS(ir));
1790 SPFROMREG(ft, MIPSInst_FT(ir));
1791 rv.w = ieee754sp_cmp(fs, ft,
1792 cmptab[cmpop & 0x7], cmpop & 0x8);
1793 rfmt = -1;
1794 if ((cmpop & 0x8) && ieee754_cxtest
1795 (IEEE754_INVALID_OPERATION))
1796 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1797 else
1798 goto copcsr;
1799
1800 }
1801 else {
1802 return SIGILL;
1803 }
1804 break;
1805 }
1806 break;
1807 }
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 case d_fmt:{
1810 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001811 union ieee754dp(*b) (union ieee754dp, union ieee754dp);
1812 union ieee754dp(*u) (union ieee754dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 } handler;
1814
1815 switch (MIPSInst_FUNC(ir)) {
1816 /* binary ops */
1817 case fadd_op:
1818 handler.b = ieee754dp_add;
1819 goto dcopbop;
1820 case fsub_op:
1821 handler.b = ieee754dp_sub;
1822 goto dcopbop;
1823 case fmul_op:
1824 handler.b = ieee754dp_mul;
1825 goto dcopbop;
1826 case fdiv_op:
1827 handler.b = ieee754dp_div;
1828 goto dcopbop;
1829
1830 /* unary ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 case fsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001832 if (!cpu_has_mips_2_3_4_5_r)
1833 return SIGILL;
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 handler.u = ieee754dp_sqrt;
1836 goto dcopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02001837 /*
1838 * Note that on some MIPS IV implementations such as the
1839 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1840 * achieve full IEEE-754 accuracy - however this emulator does.
1841 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 case frsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001843 if (!cpu_has_mips_4_5_r2)
1844 return SIGILL;
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 handler.u = fpemu_dp_rsqrt;
1847 goto dcopuop;
1848 case frecip_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001849 if (!cpu_has_mips_4_5_r2)
1850 return SIGILL;
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 handler.u = fpemu_dp_recip;
1853 goto dcopuop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001855 if (!cpu_has_mips_4_5_r)
1856 return SIGILL;
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1859 if (((ctx->fcr31 & cond) != 0) !=
1860 ((MIPSInst_FT(ir) & 1) != 0))
1861 return 0;
1862 DPFROMREG(rv.d, MIPSInst_FS(ir));
1863 break;
1864 case fmovz_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001865 if (!cpu_has_mips_4_5_r)
1866 return SIGILL;
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1869 return 0;
1870 DPFROMREG(rv.d, MIPSInst_FS(ir));
1871 break;
1872 case fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001873 if (!cpu_has_mips_4_5_r)
1874 return SIGILL;
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1877 return 0;
1878 DPFROMREG(rv.d, MIPSInst_FS(ir));
1879 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 case fabs_op:
1881 handler.u = ieee754dp_abs;
1882 goto dcopuop;
1883
1884 case fneg_op:
1885 handler.u = ieee754dp_neg;
1886 goto dcopuop;
1887
1888 case fmov_op:
1889 /* an easy one */
1890 DPFROMREG(rv.d, MIPSInst_FS(ir));
1891 goto copcsr;
1892
1893 /* binary op on handler */
1894 dcopbop:{
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001895 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897 DPFROMREG(fs, MIPSInst_FS(ir));
1898 DPFROMREG(ft, MIPSInst_FT(ir));
1899
1900 rv.d = (*handler.b) (fs, ft);
1901 goto copcsr;
1902 }
1903 dcopuop:{
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001904 union ieee754dp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 DPFROMREG(fs, MIPSInst_FS(ir));
1907 rv.d = (*handler.u) (fs);
1908 goto copcsr;
1909 }
1910
1911 /* unary conv ops */
1912 case fcvts_op:{
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001913 union ieee754dp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 DPFROMREG(fs, MIPSInst_FS(ir));
1916 rv.s = ieee754sp_fdp(fs);
1917 rfmt = s_fmt;
1918 goto copcsr;
1919 }
1920 case fcvtd_op:
1921 return SIGILL; /* not defined */
1922
1923 case fcvtw_op:{
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001924 union ieee754dp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 DPFROMREG(fs, MIPSInst_FS(ir));
1927 rv.w = ieee754dp_tint(fs); /* wrong */
1928 rfmt = w_fmt;
1929 goto copcsr;
1930 }
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 case fround_op:
1933 case ftrunc_op:
1934 case fceil_op:
1935 case ffloor_op:{
1936 unsigned int oldrm = ieee754_csr.rm;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001937 union ieee754dp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Ralf Baechle08a07902014-04-19 13:11:37 +02001939 if (!cpu_has_mips_2_3_4_5_r)
1940 return SIGILL;
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 DPFROMREG(fs, MIPSInst_FS(ir));
Shane McDonald3f135532010-05-07 00:02:09 -06001943 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 rv.w = ieee754dp_tint(fs);
1945 ieee754_csr.rm = oldrm;
1946 rfmt = w_fmt;
1947 goto copcsr;
1948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 case fcvtl_op:{
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001951 union ieee754dp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Ralf Baechle08a07902014-04-19 13:11:37 +02001953 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1954 return SIGILL;
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 DPFROMREG(fs, MIPSInst_FS(ir));
1957 rv.l = ieee754dp_tlong(fs);
1958 rfmt = l_fmt;
1959 goto copcsr;
1960 }
1961
1962 case froundl_op:
1963 case ftruncl_op:
1964 case fceill_op:
1965 case ffloorl_op:{
1966 unsigned int oldrm = ieee754_csr.rm;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001967 union ieee754dp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Ralf Baechle08a07902014-04-19 13:11:37 +02001969 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1970 return SIGILL;
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 DPFROMREG(fs, MIPSInst_FS(ir));
Shane McDonald3f135532010-05-07 00:02:09 -06001973 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 rv.l = ieee754dp_tlong(fs);
1975 ieee754_csr.rm = oldrm;
1976 rfmt = l_fmt;
1977 goto copcsr;
1978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 default:
1981 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1982 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001983 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 DPFROMREG(fs, MIPSInst_FS(ir));
1986 DPFROMREG(ft, MIPSInst_FT(ir));
1987 rv.w = ieee754dp_cmp(fs, ft,
1988 cmptab[cmpop & 0x7], cmpop & 0x8);
1989 rfmt = -1;
1990 if ((cmpop & 0x8)
1991 &&
1992 ieee754_cxtest
1993 (IEEE754_INVALID_OPERATION))
1994 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1995 else
1996 goto copcsr;
1997
1998 }
1999 else {
2000 return SIGILL;
2001 }
2002 break;
2003 }
2004 break;
2005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 case w_fmt:{
Ralf Baechle2209bcb2014-04-16 01:31:11 +02002008 union ieee754sp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 switch (MIPSInst_FUNC(ir)) {
2011 case fcvts_op:
2012 /* convert word to single precision real */
2013 SPFROMREG(fs, MIPSInst_FS(ir));
2014 rv.s = ieee754sp_fint(fs.bits);
2015 rfmt = s_fmt;
2016 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 case fcvtd_op:
2018 /* convert word to double precision real */
2019 SPFROMREG(fs, MIPSInst_FS(ir));
2020 rv.d = ieee754dp_fint(fs.bits);
2021 rfmt = d_fmt;
2022 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 default:
2024 return SIGILL;
2025 }
2026 break;
2027 }
2028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 case l_fmt:{
Paul Burtonbbd426f2014-02-13 11:26:41 +00002030 u64 bits;
Ralf Baechle08a07902014-04-19 13:11:37 +02002031
2032 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
2033 return SIGILL;
2034
Paul Burtonbbd426f2014-02-13 11:26:41 +00002035 DIFROMREG(bits, MIPSInst_FS(ir));
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 switch (MIPSInst_FUNC(ir)) {
2038 case fcvts_op:
2039 /* convert long to single precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00002040 rv.s = ieee754sp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 rfmt = s_fmt;
2042 goto copcsr;
2043 case fcvtd_op:
2044 /* convert long to double precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00002045 rv.d = ieee754dp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 rfmt = d_fmt;
2047 goto copcsr;
2048 default:
2049 return SIGILL;
2050 }
2051 break;
2052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054 default:
2055 return SIGILL;
2056 }
2057
2058 /*
2059 * Update the fpu CSR register for this operation.
2060 * If an exception is required, generate a tidy SIGFPE exception,
2061 * without updating the result register.
2062 * Note: cause exception bits do not accumulate, they are rewritten
2063 * for each op; only the flag/sticky bits accumulate.
2064 */
2065 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
2066 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
2067 /*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */
2068 return SIGFPE;
2069 }
2070
2071 /*
2072 * Now we can safely write the result back to the register file.
2073 */
2074 switch (rfmt) {
Ralf Baechle08a07902014-04-19 13:11:37 +02002075 unsigned int cbit;
2076 case -1:
2077
2078 if (cpu_has_mips_4_5_r)
2079 cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 else
Ralf Baechle08a07902014-04-19 13:11:37 +02002081 cbit = FPU_CSR_COND;
2082 if (rv.w)
2083 ctx->fcr31 |= cbit;
2084 else
2085 ctx->fcr31 &= ~cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 break;
Ralf Baechle08a07902014-04-19 13:11:37 +02002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 case d_fmt:
2089 DPTOREG(rv.d, MIPSInst_FD(ir));
2090 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 case s_fmt:
2092 SPTOREG(rv.s, MIPSInst_FD(ir));
2093 break;
2094 case w_fmt:
2095 SITOREG(rv.w, MIPSInst_FD(ir));
2096 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 case l_fmt:
Ralf Baechle08a07902014-04-19 13:11:37 +02002098 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
2099 return SIGILL;
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 DITOREG(rv.l, MIPSInst_FD(ir));
2102 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 default:
2104 return SIGILL;
2105 }
2106
2107 return 0;
2108}
2109
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09002110int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07002111 int has_fpu, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
Ralf Baechle333d1f62005-02-28 17:55:57 +00002113 unsigned long oldepc, prevepc;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002114 struct mm_decoded_insn dec_insn;
2115 u16 instr[4];
2116 u16 *instr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 int sig = 0;
2118
2119 oldepc = xcp->cp0_epc;
2120 do {
2121 prevepc = xcp->cp0_epc;
2122
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002123 if (get_isa16_mode(prevepc) && cpu_has_mmips) {
2124 /*
2125 * Get next 2 microMIPS instructions and convert them
2126 * into 32-bit instructions.
2127 */
2128 if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2129 (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2130 (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2131 (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2132 MIPS_FPU_EMU_INC_STATS(errors);
2133 return SIGBUS;
2134 }
2135 instr_ptr = instr;
2136
2137 /* Get first instruction. */
2138 if (mm_insn_16bit(*instr_ptr)) {
2139 /* Duplicate the half-word. */
2140 dec_insn.insn = (*instr_ptr << 16) |
2141 (*instr_ptr);
2142 /* 16-bit instruction. */
2143 dec_insn.pc_inc = 2;
2144 instr_ptr += 1;
2145 } else {
2146 dec_insn.insn = (*instr_ptr << 16) |
2147 *(instr_ptr+1);
2148 /* 32-bit instruction. */
2149 dec_insn.pc_inc = 4;
2150 instr_ptr += 2;
2151 }
2152 /* Get second instruction. */
2153 if (mm_insn_16bit(*instr_ptr)) {
2154 /* Duplicate the half-word. */
2155 dec_insn.next_insn = (*instr_ptr << 16) |
2156 (*instr_ptr);
2157 /* 16-bit instruction. */
2158 dec_insn.next_pc_inc = 2;
2159 } else {
2160 dec_insn.next_insn = (*instr_ptr << 16) |
2161 *(instr_ptr+1);
2162 /* 32-bit instruction. */
2163 dec_insn.next_pc_inc = 4;
2164 }
2165 dec_insn.micro_mips_mode = 1;
2166 } else {
2167 if ((get_user(dec_insn.insn,
2168 (mips_instruction __user *) xcp->cp0_epc)) ||
2169 (get_user(dec_insn.next_insn,
2170 (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2171 MIPS_FPU_EMU_INC_STATS(errors);
2172 return SIGBUS;
2173 }
2174 dec_insn.pc_inc = 4;
2175 dec_insn.next_pc_inc = 4;
2176 dec_insn.micro_mips_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 }
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002178
2179 if ((dec_insn.insn == 0) ||
2180 ((dec_insn.pc_inc == 2) &&
2181 ((dec_insn.insn & 0xffff) == MM_NOP16)))
2182 xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 else {
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002184 /*
2185 * The 'ieee754_csr' is an alias of
Ralf Baechle70342282013-01-22 12:59:30 +01002186 * ctx->fcr31. No need to copy ctx->fcr31 to
2187 * ieee754_csr. But ieee754_csr.rm is ieee
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002188 * library modes. (not mips rounding mode)
2189 */
2190 /* convert to ieee library modes */
2191 ieee754_csr.rm = ieee_rm[ieee754_csr.rm];
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002192 sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002193 /* revert to mips rounding mode */
2194 ieee754_csr.rm = mips_rm[ieee754_csr.rm];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 }
2196
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09002197 if (has_fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 break;
2199 if (sig)
2200 break;
2201
2202 cond_resched();
2203 } while (xcp->cp0_epc > prevepc);
2204
2205 /* SIGILL indicates a non-fpu instruction */
2206 if (sig == SIGILL && xcp->cp0_epc != oldepc)
2207 /* but if epc has advanced, then ignore it */
2208 sig = 0;
2209
2210 return sig;
2211}