2 * cp1emu.c: a MIPS coprocessor 1 (FPU) instruction emulator
4 * MIPS floating point support
5 * Copyright (C) 1994-2000 Algorithmics Ltd.
7 * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
8 * Copyright (C) 2000 MIPS Technologies, Inc.
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.
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
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 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
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.
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.
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!
36 #include <linux/sched.h>
37 #include <linux/debugfs.h>
38 #include <linux/kconfig.h>
39 #include <linux/percpu-defs.h>
40 #include <linux/perf_event.h>
42 #include <asm/branch.h>
44 #include <asm/ptrace.h>
45 #include <asm/signal.h>
46 #include <asm/uaccess.h>
48 #include <asm/processor.h>
49 #include <asm/fpu_emulator.h>
51 #include <asm/mips-r2-to-r6-emul.h>
55 /* Function which emulates a floating point instruction. */
57 static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
60 static int fpux_emu(struct pt_regs *,
61 struct mips_fpu_struct *, mips_instruction, void *__user *);
63 /* Control registers */
65 #define FPCREG_RID 0 /* $0 = revision id */
66 #define FPCREG_CSR 31 /* $31 = csr */
68 /* convert condition code register number to csr bit */
69 const unsigned int fpucondbit[8] = {
80 /* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
81 static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
82 static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
83 static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
84 static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
87 * This functions translates a 32-bit microMIPS instruction
88 * into a 32-bit MIPS32 instruction. Returns 0 on success
89 * and SIGILL otherwise.
91 static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
93 union mips_instruction insn = *insn_ptr;
94 union mips_instruction mips32_insn = insn;
97 switch (insn.mm_i_format.opcode) {
99 mips32_insn.mm_i_format.opcode = ldc1_op;
100 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
101 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
104 mips32_insn.mm_i_format.opcode = lwc1_op;
105 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
106 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
109 mips32_insn.mm_i_format.opcode = sdc1_op;
110 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
111 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
114 mips32_insn.mm_i_format.opcode = swc1_op;
115 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
116 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
119 /* NOTE: offset is << by 1 if in microMIPS mode. */
120 if ((insn.mm_i_format.rt == mm_bc1f_op) ||
121 (insn.mm_i_format.rt == mm_bc1t_op)) {
122 mips32_insn.fb_format.opcode = cop1_op;
123 mips32_insn.fb_format.bc = bc_op;
124 mips32_insn.fb_format.flag =
125 (insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
130 switch (insn.mm_fp0_format.func) {
139 op = insn.mm_fp0_format.func;
140 if (op == mm_32f_01_op)
142 else if (op == mm_32f_11_op)
144 else if (op == mm_32f_02_op)
146 else if (op == mm_32f_12_op)
148 else if (op == mm_32f_41_op)
150 else if (op == mm_32f_51_op)
152 else if (op == mm_32f_42_op)
156 mips32_insn.fp6_format.opcode = cop1x_op;
157 mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
158 mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
159 mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
160 mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
161 mips32_insn.fp6_format.func = func;
164 func = -1; /* Invalid */
165 op = insn.mm_fp5_format.op & 0x7;
166 if (op == mm_ldxc1_op)
168 else if (op == mm_sdxc1_op)
170 else if (op == mm_lwxc1_op)
172 else if (op == mm_swxc1_op)
176 mips32_insn.r_format.opcode = cop1x_op;
177 mips32_insn.r_format.rs =
178 insn.mm_fp5_format.base;
179 mips32_insn.r_format.rt =
180 insn.mm_fp5_format.index;
181 mips32_insn.r_format.rd = 0;
182 mips32_insn.r_format.re = insn.mm_fp5_format.fd;
183 mips32_insn.r_format.func = func;
188 op = -1; /* Invalid */
189 if (insn.mm_fp2_format.op == mm_fmovt_op)
191 else if (insn.mm_fp2_format.op == mm_fmovf_op)
194 mips32_insn.fp0_format.opcode = cop1_op;
195 mips32_insn.fp0_format.fmt =
196 sdps_format[insn.mm_fp2_format.fmt];
197 mips32_insn.fp0_format.ft =
198 (insn.mm_fp2_format.cc<<2) + op;
199 mips32_insn.fp0_format.fs =
200 insn.mm_fp2_format.fs;
201 mips32_insn.fp0_format.fd =
202 insn.mm_fp2_format.fd;
203 mips32_insn.fp0_format.func = fmovc_op;
208 func = -1; /* Invalid */
209 if (insn.mm_fp0_format.op == mm_fadd_op)
211 else if (insn.mm_fp0_format.op == mm_fsub_op)
213 else if (insn.mm_fp0_format.op == mm_fmul_op)
215 else if (insn.mm_fp0_format.op == mm_fdiv_op)
218 mips32_insn.fp0_format.opcode = cop1_op;
219 mips32_insn.fp0_format.fmt =
220 sdps_format[insn.mm_fp0_format.fmt];
221 mips32_insn.fp0_format.ft =
222 insn.mm_fp0_format.ft;
223 mips32_insn.fp0_format.fs =
224 insn.mm_fp0_format.fs;
225 mips32_insn.fp0_format.fd =
226 insn.mm_fp0_format.fd;
227 mips32_insn.fp0_format.func = func;
232 func = -1; /* Invalid */
233 if (insn.mm_fp0_format.op == mm_fmovn_op)
235 else if (insn.mm_fp0_format.op == mm_fmovz_op)
238 mips32_insn.fp0_format.opcode = cop1_op;
239 mips32_insn.fp0_format.fmt =
240 sdps_format[insn.mm_fp0_format.fmt];
241 mips32_insn.fp0_format.ft =
242 insn.mm_fp0_format.ft;
243 mips32_insn.fp0_format.fs =
244 insn.mm_fp0_format.fs;
245 mips32_insn.fp0_format.fd =
246 insn.mm_fp0_format.fd;
247 mips32_insn.fp0_format.func = func;
251 case mm_32f_73_op: /* POOL32FXF */
252 switch (insn.mm_fp1_format.op) {
257 if ((insn.mm_fp1_format.op & 0x7f) ==
262 mips32_insn.r_format.opcode = spec_op;
263 mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
264 mips32_insn.r_format.rt =
265 (insn.mm_fp4_format.cc << 2) + op;
266 mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
267 mips32_insn.r_format.re = 0;
268 mips32_insn.r_format.func = movc_op;
274 if ((insn.mm_fp1_format.op & 0x7f) ==
277 fmt = swl_format[insn.mm_fp3_format.fmt];
280 fmt = dwl_format[insn.mm_fp3_format.fmt];
282 mips32_insn.fp0_format.opcode = cop1_op;
283 mips32_insn.fp0_format.fmt = fmt;
284 mips32_insn.fp0_format.ft = 0;
285 mips32_insn.fp0_format.fs =
286 insn.mm_fp3_format.fs;
287 mips32_insn.fp0_format.fd =
288 insn.mm_fp3_format.rt;
289 mips32_insn.fp0_format.func = func;
297 if ((insn.mm_fp1_format.op & 0x7f) ==
300 else if ((insn.mm_fp1_format.op & 0x7f) ==
305 mips32_insn.fp0_format.opcode = cop1_op;
306 mips32_insn.fp0_format.fmt =
307 sdps_format[insn.mm_fp3_format.fmt];
308 mips32_insn.fp0_format.ft = 0;
309 mips32_insn.fp0_format.fs =
310 insn.mm_fp3_format.fs;
311 mips32_insn.fp0_format.fd =
312 insn.mm_fp3_format.rt;
313 mips32_insn.fp0_format.func = func;
325 if (insn.mm_fp1_format.op == mm_ffloorl_op)
327 else if (insn.mm_fp1_format.op == mm_ffloorw_op)
329 else if (insn.mm_fp1_format.op == mm_fceill_op)
331 else if (insn.mm_fp1_format.op == mm_fceilw_op)
333 else if (insn.mm_fp1_format.op == mm_ftruncl_op)
335 else if (insn.mm_fp1_format.op == mm_ftruncw_op)
337 else if (insn.mm_fp1_format.op == mm_froundl_op)
339 else if (insn.mm_fp1_format.op == mm_froundw_op)
341 else if (insn.mm_fp1_format.op == mm_fcvtl_op)
345 mips32_insn.fp0_format.opcode = cop1_op;
346 mips32_insn.fp0_format.fmt =
347 sd_format[insn.mm_fp1_format.fmt];
348 mips32_insn.fp0_format.ft = 0;
349 mips32_insn.fp0_format.fs =
350 insn.mm_fp1_format.fs;
351 mips32_insn.fp0_format.fd =
352 insn.mm_fp1_format.rt;
353 mips32_insn.fp0_format.func = func;
358 if (insn.mm_fp1_format.op == mm_frsqrt_op)
360 else if (insn.mm_fp1_format.op == mm_fsqrt_op)
364 mips32_insn.fp0_format.opcode = cop1_op;
365 mips32_insn.fp0_format.fmt =
366 sdps_format[insn.mm_fp1_format.fmt];
367 mips32_insn.fp0_format.ft = 0;
368 mips32_insn.fp0_format.fs =
369 insn.mm_fp1_format.fs;
370 mips32_insn.fp0_format.fd =
371 insn.mm_fp1_format.rt;
372 mips32_insn.fp0_format.func = func;
380 if (insn.mm_fp1_format.op == mm_mfc1_op)
382 else if (insn.mm_fp1_format.op == mm_mtc1_op)
384 else if (insn.mm_fp1_format.op == mm_cfc1_op)
386 else if (insn.mm_fp1_format.op == mm_ctc1_op)
388 else if (insn.mm_fp1_format.op == mm_mfhc1_op)
392 mips32_insn.fp1_format.opcode = cop1_op;
393 mips32_insn.fp1_format.op = op;
394 mips32_insn.fp1_format.rt =
395 insn.mm_fp1_format.rt;
396 mips32_insn.fp1_format.fs =
397 insn.mm_fp1_format.fs;
398 mips32_insn.fp1_format.fd = 0;
399 mips32_insn.fp1_format.func = 0;
405 case mm_32f_74_op: /* c.cond.fmt */
406 mips32_insn.fp0_format.opcode = cop1_op;
407 mips32_insn.fp0_format.fmt =
408 sdps_format[insn.mm_fp4_format.fmt];
409 mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
410 mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
411 mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
412 mips32_insn.fp0_format.func =
413 insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
423 *insn_ptr = mips32_insn;
428 * Redundant with logic already in kernel/branch.c,
429 * embedded in compute_return_epc. At some point,
430 * a single subroutine should be used across both
433 static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
434 unsigned long *contpc)
436 union mips_instruction insn = (union mips_instruction)dec_insn.insn;
438 unsigned int bit = 0;
440 switch (insn.i_format.opcode) {
442 switch (insn.r_format.func) {
444 regs->regs[insn.r_format.rd] =
445 regs->cp0_epc + dec_insn.pc_inc +
446 dec_insn.next_pc_inc;
449 /* For R6, JR already emulated in jalr_op */
450 if (NO_R6EMU && insn.r_format.opcode == jr_op)
452 *contpc = regs->regs[insn.r_format.rs];
457 switch (insn.i_format.rt) {
460 if (NO_R6EMU && (insn.i_format.rs ||
461 insn.i_format.rt == bltzall_op))
464 regs->regs[31] = regs->cp0_epc +
466 dec_insn.next_pc_inc;
472 if ((long)regs->regs[insn.i_format.rs] < 0)
473 *contpc = regs->cp0_epc +
475 (insn.i_format.simmediate << 2);
477 *contpc = regs->cp0_epc +
479 dec_insn.next_pc_inc;
483 if (NO_R6EMU && (insn.i_format.rs ||
484 insn.i_format.rt == bgezall_op))
487 regs->regs[31] = regs->cp0_epc +
489 dec_insn.next_pc_inc;
495 if ((long)regs->regs[insn.i_format.rs] >= 0)
496 *contpc = regs->cp0_epc +
498 (insn.i_format.simmediate << 2);
500 *contpc = regs->cp0_epc +
502 dec_insn.next_pc_inc;
509 regs->regs[31] = regs->cp0_epc +
511 dec_insn.next_pc_inc;
514 *contpc = regs->cp0_epc + dec_insn.pc_inc;
517 *contpc |= (insn.j_format.target << 2);
518 /* Set microMIPS mode bit: XOR for jalx. */
525 if (regs->regs[insn.i_format.rs] ==
526 regs->regs[insn.i_format.rt])
527 *contpc = regs->cp0_epc +
529 (insn.i_format.simmediate << 2);
531 *contpc = regs->cp0_epc +
533 dec_insn.next_pc_inc;
539 if (regs->regs[insn.i_format.rs] !=
540 regs->regs[insn.i_format.rt])
541 *contpc = regs->cp0_epc +
543 (insn.i_format.simmediate << 2);
545 *contpc = regs->cp0_epc +
547 dec_insn.next_pc_inc;
555 * Compact branches for R6 for the
556 * blez and blezl opcodes.
557 * BLEZ | rs = 0 | rt != 0 == BLEZALC
558 * BLEZ | rs = rt != 0 == BGEZALC
559 * BLEZ | rs != 0 | rt != 0 == BGEUC
560 * BLEZL | rs = 0 | rt != 0 == BLEZC
561 * BLEZL | rs = rt != 0 == BGEZC
562 * BLEZL | rs != 0 | rt != 0 == BGEC
564 * For real BLEZ{,L}, rt is always 0.
566 if (cpu_has_mips_r6 && insn.i_format.rt) {
567 if ((insn.i_format.opcode == blez_op) &&
568 ((!insn.i_format.rs && insn.i_format.rt) ||
569 (insn.i_format.rs == insn.i_format.rt)))
570 regs->regs[31] = regs->cp0_epc +
572 *contpc = regs->cp0_epc + dec_insn.pc_inc +
573 dec_insn.next_pc_inc;
577 if ((long)regs->regs[insn.i_format.rs] <= 0)
578 *contpc = regs->cp0_epc +
580 (insn.i_format.simmediate << 2);
582 *contpc = regs->cp0_epc +
584 dec_insn.next_pc_inc;
591 * Compact branches for R6 for the
592 * bgtz and bgtzl opcodes.
593 * BGTZ | rs = 0 | rt != 0 == BGTZALC
594 * BGTZ | rs = rt != 0 == BLTZALC
595 * BGTZ | rs != 0 | rt != 0 == BLTUC
596 * BGTZL | rs = 0 | rt != 0 == BGTZC
597 * BGTZL | rs = rt != 0 == BLTZC
598 * BGTZL | rs != 0 | rt != 0 == BLTC
600 * *ZALC varint for BGTZ &&& rt != 0
601 * For real GTZ{,L}, rt is always 0.
603 if (cpu_has_mips_r6 && insn.i_format.rt) {
604 if ((insn.i_format.opcode == blez_op) &&
605 ((!insn.i_format.rs && insn.i_format.rt) ||
606 (insn.i_format.rs == insn.i_format.rt)))
607 regs->regs[31] = regs->cp0_epc +
609 *contpc = regs->cp0_epc + dec_insn.pc_inc +
610 dec_insn.next_pc_inc;
615 if ((long)regs->regs[insn.i_format.rs] > 0)
616 *contpc = regs->cp0_epc +
618 (insn.i_format.simmediate << 2);
620 *contpc = regs->cp0_epc +
622 dec_insn.next_pc_inc;
626 if (!cpu_has_mips_r6)
628 if (insn.i_format.rt && !insn.i_format.rs)
629 regs->regs[31] = regs->cp0_epc + 4;
630 *contpc = regs->cp0_epc + dec_insn.pc_inc +
631 dec_insn.next_pc_inc;
634 #ifdef CONFIG_CPU_CAVIUM_OCTEON
635 case lwc2_op: /* This is bbit0 on Octeon */
636 if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
637 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
639 *contpc = regs->cp0_epc + 8;
641 case ldc2_op: /* This is bbit032 on Octeon */
642 if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
643 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
645 *contpc = regs->cp0_epc + 8;
647 case swc2_op: /* This is bbit1 on Octeon */
648 if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
649 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
651 *contpc = regs->cp0_epc + 8;
653 case sdc2_op: /* This is bbit132 on Octeon */
654 if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
655 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
657 *contpc = regs->cp0_epc + 8;
662 * Only valid for MIPS R6 but we can still end up
663 * here from a broken userland so just tell emulator
664 * this is not a branch and let it break later on.
666 if (!cpu_has_mips_r6)
668 *contpc = regs->cp0_epc + dec_insn.pc_inc +
669 dec_insn.next_pc_inc;
673 if (!cpu_has_mips_r6)
675 regs->regs[31] = regs->cp0_epc + 4;
676 *contpc = regs->cp0_epc + dec_insn.pc_inc +
677 dec_insn.next_pc_inc;
681 if (!cpu_has_mips_r6)
683 *contpc = regs->cp0_epc + dec_insn.pc_inc +
684 dec_insn.next_pc_inc;
688 if (!cpu_has_mips_r6)
690 if (!insn.i_format.rs)
691 regs->regs[31] = regs->cp0_epc + 4;
692 *contpc = regs->cp0_epc + dec_insn.pc_inc +
693 dec_insn.next_pc_inc;
699 /* Need to check for R6 bc1nez and bc1eqz branches */
700 if (cpu_has_mips_r6 &&
701 ((insn.i_format.rs == bc1eqz_op) ||
702 (insn.i_format.rs == bc1nez_op))) {
704 switch (insn.i_format.rs) {
706 if (get_fpr32(¤t->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1)
710 if (!(get_fpr32(¤t->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1))
715 *contpc = regs->cp0_epc +
717 (insn.i_format.simmediate << 2);
719 *contpc = regs->cp0_epc +
721 dec_insn.next_pc_inc;
725 /* R2/R6 compatible cop1 instruction. Fall through */
728 if (insn.i_format.rs == bc_op) {
731 fcr31 = read_32bit_cp1_register(CP1_STATUS);
733 fcr31 = current->thread.fpu.fcr31;
736 bit = (insn.i_format.rt >> 2);
739 switch (insn.i_format.rt & 3) {
742 if (~fcr31 & (1 << bit))
743 *contpc = regs->cp0_epc +
745 (insn.i_format.simmediate << 2);
747 *contpc = regs->cp0_epc +
749 dec_insn.next_pc_inc;
753 if (fcr31 & (1 << bit))
754 *contpc = regs->cp0_epc +
756 (insn.i_format.simmediate << 2);
758 *contpc = regs->cp0_epc +
760 dec_insn.next_pc_inc;
770 * In the Linux kernel, we support selection of FPR format on the
771 * basis of the Status.FR bit. If an FPU is not present, the FR bit
772 * is hardwired to zero, which would imply a 32-bit FPU even for
773 * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
774 * FPU emu is slow and bulky and optimizing this function offers fairly
775 * sizeable benefits so we try to be clever and make this function return
776 * a constant whenever possible, that is on 64-bit kernels without O32
777 * compatibility enabled and on 32-bit without 64-bit FPU support.
779 static inline int cop1_64bit(struct pt_regs *xcp)
781 if (config_enabled(CONFIG_64BIT) && !config_enabled(CONFIG_MIPS32_O32))
783 else if (config_enabled(CONFIG_32BIT) &&
784 !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
787 return !test_thread_flag(TIF_32BIT_FPREGS);
790 static inline bool hybrid_fprs(void)
792 return test_thread_flag(TIF_HYBRID_FPREGS);
795 #define SIFROMREG(si, x) \
797 if (cop1_64bit(xcp) && !hybrid_fprs()) \
798 (si) = (int)get_fpr32(&ctx->fpr[x], 0); \
800 (si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
803 #define SITOREG(si, x) \
805 if (cop1_64bit(xcp) && !hybrid_fprs()) { \
807 set_fpr32(&ctx->fpr[x], 0, si); \
808 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
809 set_fpr32(&ctx->fpr[x], i, 0); \
811 set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
815 #define SIFROMHREG(si, x) ((si) = (int)get_fpr32(&ctx->fpr[x], 1))
817 #define SITOHREG(si, x) \
820 set_fpr32(&ctx->fpr[x], 1, si); \
821 for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
822 set_fpr32(&ctx->fpr[x], i, 0); \
825 #define DIFROMREG(di, x) \
826 ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
828 #define DITOREG(di, x) \
831 fpr = (x) & ~(cop1_64bit(xcp) == 0); \
832 set_fpr64(&ctx->fpr[fpr], 0, di); \
833 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
834 set_fpr64(&ctx->fpr[fpr], i, 0); \
837 #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
838 #define SPTOREG(sp, x) SITOREG((sp).bits, x)
839 #define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
840 #define DPTOREG(dp, x) DITOREG((dp).bits, x)
843 * Emulate a CFC1 instruction.
845 static inline void cop1_cfc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
850 if (MIPSInst_RD(ir) == FPCREG_CSR) {
852 pr_debug("%p gpr[%d]<-csr=%08x\n",
853 (void *)xcp->cp0_epc,
854 MIPSInst_RT(ir), value);
855 } else if (MIPSInst_RD(ir) == FPCREG_RID)
860 xcp->regs[MIPSInst_RT(ir)] = value;
864 * Emulate a CTC1 instruction.
866 static inline void cop1_ctc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
871 if (MIPSInst_RT(ir) == 0)
874 value = xcp->regs[MIPSInst_RT(ir)];
876 /* we only have one writable control reg
878 if (MIPSInst_RD(ir) == FPCREG_CSR) {
879 pr_debug("%p gpr[%d]->csr=%08x\n",
880 (void *)xcp->cp0_epc,
881 MIPSInst_RT(ir), value);
883 /* Don't write reserved bits. */
884 ctx->fcr31 = value & ~FPU_CSR_RSVD;
889 * Emulate the single floating point instruction pointed at by EPC.
890 * Two instructions if the instruction is in a branch delay slot.
893 static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
894 struct mm_decoded_insn dec_insn, void *__user *fault_addr)
896 unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
897 unsigned int cond, cbit;
907 * These are giving gcc a gentle hint about what to expect in
908 * dec_inst in order to do better optimization.
910 if (!cpu_has_mmips && dec_insn.micro_mips_mode)
913 /* XXX NEC Vr54xx bug workaround */
914 if (delay_slot(xcp)) {
915 if (dec_insn.micro_mips_mode) {
916 if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
917 clear_delay_slot(xcp);
919 if (!isBranchInstr(xcp, dec_insn, &contpc))
920 clear_delay_slot(xcp);
924 if (delay_slot(xcp)) {
926 * The instruction to be emulated is in a branch delay slot
927 * which means that we have to emulate the branch instruction
928 * BEFORE we do the cop1 instruction.
930 * This branch could be a COP1 branch, but in that case we
931 * would have had a trap for that instruction, and would not
932 * come through this route.
934 * Linux MIPS branch emulator operates on context, updating the
937 ir = dec_insn.next_insn; /* process delay slot instr */
938 pc_inc = dec_insn.next_pc_inc;
940 ir = dec_insn.insn; /* process current instr */
941 pc_inc = dec_insn.pc_inc;
945 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
946 * instructions, we want to convert microMIPS FPU instructions
947 * into MIPS32 instructions so that we could reuse all of the
948 * FPU emulation code.
950 * NOTE: We cannot do this for branch instructions since they
951 * are not a subset. Example: Cannot emulate a 16-bit
952 * aligned target address with a MIPS32 instruction.
954 if (dec_insn.micro_mips_mode) {
956 * If next instruction is a 16-bit instruction, then it
957 * it cannot be a FPU instruction. This could happen
958 * since we can be called for non-FPU instructions.
961 (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
967 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
968 MIPS_FPU_EMU_INC_STATS(emulated);
969 switch (MIPSInst_OPCODE(ir)) {
971 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
973 MIPS_FPU_EMU_INC_STATS(loads);
975 if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
976 MIPS_FPU_EMU_INC_STATS(errors);
980 if (__get_user(dval, dva)) {
981 MIPS_FPU_EMU_INC_STATS(errors);
985 DITOREG(dval, MIPSInst_RT(ir));
989 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
991 MIPS_FPU_EMU_INC_STATS(stores);
992 DIFROMREG(dval, MIPSInst_RT(ir));
993 if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
994 MIPS_FPU_EMU_INC_STATS(errors);
998 if (__put_user(dval, dva)) {
999 MIPS_FPU_EMU_INC_STATS(errors);
1006 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1008 MIPS_FPU_EMU_INC_STATS(loads);
1009 if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
1010 MIPS_FPU_EMU_INC_STATS(errors);
1014 if (__get_user(wval, wva)) {
1015 MIPS_FPU_EMU_INC_STATS(errors);
1019 SITOREG(wval, MIPSInst_RT(ir));
1023 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1025 MIPS_FPU_EMU_INC_STATS(stores);
1026 SIFROMREG(wval, MIPSInst_RT(ir));
1027 if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
1028 MIPS_FPU_EMU_INC_STATS(errors);
1032 if (__put_user(wval, wva)) {
1033 MIPS_FPU_EMU_INC_STATS(errors);
1040 switch (MIPSInst_RS(ir)) {
1042 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1045 /* copregister fs -> gpr[rt] */
1046 if (MIPSInst_RT(ir) != 0) {
1047 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1053 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1056 /* copregister fs <- rt */
1057 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1061 if (!cpu_has_mips_r2)
1064 /* copregister rd -> gpr[rt] */
1065 if (MIPSInst_RT(ir) != 0) {
1066 SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
1072 if (!cpu_has_mips_r2)
1075 /* copregister rd <- gpr[rt] */
1076 SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1080 /* copregister rd -> gpr[rt] */
1081 if (MIPSInst_RT(ir) != 0) {
1082 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1088 /* copregister rd <- rt */
1089 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1093 /* cop control register rd -> gpr[rt] */
1094 cop1_cfc(xcp, ctx, ir);
1098 /* copregister rd <- rt */
1099 cop1_ctc(xcp, ctx, ir);
1100 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1106 if (delay_slot(xcp))
1109 if (cpu_has_mips_4_5_r)
1110 cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
1112 cbit = FPU_CSR_COND;
1113 cond = ctx->fcr31 & cbit;
1116 switch (MIPSInst_RT(ir) & 3) {
1118 if (cpu_has_mips_2_3_4_5_r)
1125 if (cpu_has_mips_2_3_4_5_r)
1132 set_delay_slot(xcp);
1135 * Branch taken: emulate dslot instruction
1140 * Remember EPC at the branch to point back
1141 * at so that any delay-slot instruction
1142 * signal is not silently ignored.
1144 bcpc = xcp->cp0_epc;
1145 xcp->cp0_epc += dec_insn.pc_inc;
1147 contpc = MIPSInst_SIMM(ir);
1148 ir = dec_insn.next_insn;
1149 if (dec_insn.micro_mips_mode) {
1150 contpc = (xcp->cp0_epc + (contpc << 1));
1152 /* If 16-bit instruction, not FPU. */
1153 if ((dec_insn.next_pc_inc == 2) ||
1154 (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1157 * Since this instruction will
1158 * be put on the stack with
1159 * 32-bit words, get around
1160 * this problem by putting a
1161 * NOP16 as the second one.
1163 if (dec_insn.next_pc_inc == 2)
1164 ir = (ir & (~0xffff)) | MM_NOP16;
1167 * Single step the non-CP1
1168 * instruction in the dslot.
1170 sig = mips_dsemul(xcp, ir,
1173 xcp->cp0_epc = bcpc;
1175 * SIGILL forces out of
1176 * the emulation loop.
1178 return sig ? sig : SIGILL;
1181 contpc = (xcp->cp0_epc + (contpc << 2));
1183 switch (MIPSInst_OPCODE(ir)) {
1190 if (cpu_has_mips_2_3_4_5_r)
1199 if (cpu_has_mips_4_5_64_r2_r6)
1200 /* its one of ours */
1206 switch (MIPSInst_FUNC(ir)) {
1208 if (cpu_has_mips_4_5_r)
1216 xcp->cp0_epc = bcpc;
1221 * Single step the non-cp1
1222 * instruction in the dslot
1224 sig = mips_dsemul(xcp, ir, contpc);
1226 xcp->cp0_epc = bcpc;
1227 /* SIGILL forces out of the emulation loop. */
1228 return sig ? sig : SIGILL;
1229 } else if (likely) { /* branch not taken */
1231 * branch likely nullifies
1232 * dslot if not taken
1234 xcp->cp0_epc += dec_insn.pc_inc;
1235 contpc += dec_insn.pc_inc;
1237 * else continue & execute
1238 * dslot as normal insn
1244 if (!(MIPSInst_RS(ir) & 0x10))
1247 /* a real fpu computation instruction */
1248 if ((sig = fpu_emu(xcp, ctx, ir)))
1254 if (!cpu_has_mips_4_5_64_r2_r6)
1257 sig = fpux_emu(xcp, ctx, ir, fault_addr);
1263 if (!cpu_has_mips_4_5_r)
1266 if (MIPSInst_FUNC(ir) != movc_op)
1268 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
1269 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
1270 xcp->regs[MIPSInst_RD(ir)] =
1271 xcp->regs[MIPSInst_RS(ir)];
1279 xcp->cp0_epc = contpc;
1280 clear_delay_slot(xcp);
1286 * Conversion table from MIPS compare ops 48-63
1287 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
1289 static const unsigned char cmptab[8] = {
1290 0, /* cmp_0 (sig) cmp_sf */
1291 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
1292 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
1293 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
1294 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
1295 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
1296 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
1297 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
1302 * Additional MIPS4 instructions
1305 #define DEF3OP(name, p, f1, f2, f3) \
1306 static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \
1307 union ieee754##p s, union ieee754##p t) \
1309 struct _ieee754_csr ieee754_csr_save; \
1311 ieee754_csr_save = ieee754_csr; \
1313 ieee754_csr_save.cx |= ieee754_csr.cx; \
1314 ieee754_csr_save.sx |= ieee754_csr.sx; \
1316 ieee754_csr.cx |= ieee754_csr_save.cx; \
1317 ieee754_csr.sx |= ieee754_csr_save.sx; \
1321 static union ieee754dp fpemu_dp_recip(union ieee754dp d)
1323 return ieee754dp_div(ieee754dp_one(0), d);
1326 static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
1328 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
1331 static union ieee754sp fpemu_sp_recip(union ieee754sp s)
1333 return ieee754sp_div(ieee754sp_one(0), s);
1336 static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
1338 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
1341 DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
1342 DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
1343 DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
1344 DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
1345 DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
1346 DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
1347 DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
1348 DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
1350 static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1351 mips_instruction ir, void *__user *fault_addr)
1353 unsigned rcsr = 0; /* resulting csr */
1355 MIPS_FPU_EMU_INC_STATS(cp1xops);
1357 switch (MIPSInst_FMA_FFMT(ir)) {
1358 case s_fmt:{ /* 0 */
1360 union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
1361 union ieee754sp fd, fr, fs, ft;
1365 switch (MIPSInst_FUNC(ir)) {
1367 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1368 xcp->regs[MIPSInst_FT(ir)]);
1370 MIPS_FPU_EMU_INC_STATS(loads);
1371 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
1372 MIPS_FPU_EMU_INC_STATS(errors);
1376 if (__get_user(val, va)) {
1377 MIPS_FPU_EMU_INC_STATS(errors);
1381 SITOREG(val, MIPSInst_FD(ir));
1385 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1386 xcp->regs[MIPSInst_FT(ir)]);
1388 MIPS_FPU_EMU_INC_STATS(stores);
1390 SIFROMREG(val, MIPSInst_FS(ir));
1391 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1392 MIPS_FPU_EMU_INC_STATS(errors);
1396 if (put_user(val, va)) {
1397 MIPS_FPU_EMU_INC_STATS(errors);
1404 handler = fpemu_sp_madd;
1407 handler = fpemu_sp_msub;
1410 handler = fpemu_sp_nmadd;
1413 handler = fpemu_sp_nmsub;
1417 SPFROMREG(fr, MIPSInst_FR(ir));
1418 SPFROMREG(fs, MIPSInst_FS(ir));
1419 SPFROMREG(ft, MIPSInst_FT(ir));
1420 fd = (*handler) (fr, fs, ft);
1421 SPTOREG(fd, MIPSInst_FD(ir));
1424 if (ieee754_cxtest(IEEE754_INEXACT)) {
1425 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
1426 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1428 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1429 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
1430 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1432 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1433 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
1434 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1436 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1437 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
1438 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1441 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1442 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1443 /*printk ("SIGFPE: FPU csr = %08x\n",
1456 case d_fmt:{ /* 1 */
1457 union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
1458 union ieee754dp fd, fr, fs, ft;
1462 switch (MIPSInst_FUNC(ir)) {
1464 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1465 xcp->regs[MIPSInst_FT(ir)]);
1467 MIPS_FPU_EMU_INC_STATS(loads);
1468 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
1469 MIPS_FPU_EMU_INC_STATS(errors);
1473 if (__get_user(val, va)) {
1474 MIPS_FPU_EMU_INC_STATS(errors);
1478 DITOREG(val, MIPSInst_FD(ir));
1482 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1483 xcp->regs[MIPSInst_FT(ir)]);
1485 MIPS_FPU_EMU_INC_STATS(stores);
1486 DIFROMREG(val, MIPSInst_FS(ir));
1487 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
1488 MIPS_FPU_EMU_INC_STATS(errors);
1492 if (__put_user(val, va)) {
1493 MIPS_FPU_EMU_INC_STATS(errors);
1500 handler = fpemu_dp_madd;
1503 handler = fpemu_dp_msub;
1506 handler = fpemu_dp_nmadd;
1509 handler = fpemu_dp_nmsub;
1513 DPFROMREG(fr, MIPSInst_FR(ir));
1514 DPFROMREG(fs, MIPSInst_FS(ir));
1515 DPFROMREG(ft, MIPSInst_FT(ir));
1516 fd = (*handler) (fr, fs, ft);
1517 DPTOREG(fd, MIPSInst_FD(ir));
1527 if (MIPSInst_FUNC(ir) != pfetch_op)
1530 /* ignore prefx operation */
1543 * Emulate a single COP1 arithmetic instruction.
1545 static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1546 mips_instruction ir)
1548 int rfmt; /* resulting format */
1549 unsigned rcsr = 0; /* resulting csr */
1558 } rv; /* resulting value */
1561 MIPS_FPU_EMU_INC_STATS(cp1ops);
1562 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
1563 case s_fmt: { /* 0 */
1565 union ieee754sp(*b) (union ieee754sp, union ieee754sp);
1566 union ieee754sp(*u) (union ieee754sp);
1568 union ieee754sp fs, ft;
1570 switch (MIPSInst_FUNC(ir)) {
1573 handler.b = ieee754sp_add;
1576 handler.b = ieee754sp_sub;
1579 handler.b = ieee754sp_mul;
1582 handler.b = ieee754sp_div;
1587 if (!cpu_has_mips_2_3_4_5_r)
1590 handler.u = ieee754sp_sqrt;
1594 * Note that on some MIPS IV implementations such as the
1595 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1596 * achieve full IEEE-754 accuracy - however this emulator does.
1599 if (!cpu_has_mips_4_5_64_r2_r6)
1602 handler.u = fpemu_sp_rsqrt;
1606 if (!cpu_has_mips_4_5_64_r2_r6)
1609 handler.u = fpemu_sp_recip;
1613 if (!cpu_has_mips_4_5_r)
1616 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1617 if (((ctx->fcr31 & cond) != 0) !=
1618 ((MIPSInst_FT(ir) & 1) != 0))
1620 SPFROMREG(rv.s, MIPSInst_FS(ir));
1624 if (!cpu_has_mips_4_5_r)
1627 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1629 SPFROMREG(rv.s, MIPSInst_FS(ir));
1633 if (!cpu_has_mips_4_5_r)
1636 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1638 SPFROMREG(rv.s, MIPSInst_FS(ir));
1642 handler.u = ieee754sp_abs;
1646 handler.u = ieee754sp_neg;
1651 SPFROMREG(rv.s, MIPSInst_FS(ir));
1654 /* binary op on handler */
1656 SPFROMREG(fs, MIPSInst_FS(ir));
1657 SPFROMREG(ft, MIPSInst_FT(ir));
1659 rv.s = (*handler.b) (fs, ft);
1662 SPFROMREG(fs, MIPSInst_FS(ir));
1663 rv.s = (*handler.u) (fs);
1666 if (ieee754_cxtest(IEEE754_INEXACT)) {
1667 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
1668 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1670 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1671 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
1672 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1674 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1675 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
1676 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1678 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1679 MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
1680 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
1682 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1683 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
1684 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1688 /* unary conv ops */
1690 return SIGILL; /* not defined */
1693 SPFROMREG(fs, MIPSInst_FS(ir));
1694 rv.d = ieee754dp_fsp(fs);
1699 SPFROMREG(fs, MIPSInst_FS(ir));
1700 rv.w = ieee754sp_tint(fs);
1708 if (!cpu_has_mips_2_3_4_5_r)
1711 oldrm = ieee754_csr.rm;
1712 SPFROMREG(fs, MIPSInst_FS(ir));
1713 ieee754_csr.rm = MIPSInst_FUNC(ir);
1714 rv.w = ieee754sp_tint(fs);
1715 ieee754_csr.rm = oldrm;
1720 if (!cpu_has_mips_3_4_5_64_r2_r6)
1723 SPFROMREG(fs, MIPSInst_FS(ir));
1724 rv.l = ieee754sp_tlong(fs);
1732 if (!cpu_has_mips_3_4_5_64_r2_r6)
1735 oldrm = ieee754_csr.rm;
1736 SPFROMREG(fs, MIPSInst_FS(ir));
1737 ieee754_csr.rm = MIPSInst_FUNC(ir);
1738 rv.l = ieee754sp_tlong(fs);
1739 ieee754_csr.rm = oldrm;
1744 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1745 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1746 union ieee754sp fs, ft;
1748 SPFROMREG(fs, MIPSInst_FS(ir));
1749 SPFROMREG(ft, MIPSInst_FT(ir));
1750 rv.w = ieee754sp_cmp(fs, ft,
1751 cmptab[cmpop & 0x7], cmpop & 0x8);
1753 if ((cmpop & 0x8) && ieee754_cxtest
1754 (IEEE754_INVALID_OPERATION))
1755 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1767 union ieee754dp fs, ft;
1769 union ieee754dp(*b) (union ieee754dp, union ieee754dp);
1770 union ieee754dp(*u) (union ieee754dp);
1773 switch (MIPSInst_FUNC(ir)) {
1776 handler.b = ieee754dp_add;
1779 handler.b = ieee754dp_sub;
1782 handler.b = ieee754dp_mul;
1785 handler.b = ieee754dp_div;
1790 if (!cpu_has_mips_2_3_4_5_r)
1793 handler.u = ieee754dp_sqrt;
1796 * Note that on some MIPS IV implementations such as the
1797 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1798 * achieve full IEEE-754 accuracy - however this emulator does.
1801 if (!cpu_has_mips_4_5_64_r2_r6)
1804 handler.u = fpemu_dp_rsqrt;
1807 if (!cpu_has_mips_4_5_64_r2_r6)
1810 handler.u = fpemu_dp_recip;
1813 if (!cpu_has_mips_4_5_r)
1816 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1817 if (((ctx->fcr31 & cond) != 0) !=
1818 ((MIPSInst_FT(ir) & 1) != 0))
1820 DPFROMREG(rv.d, MIPSInst_FS(ir));
1823 if (!cpu_has_mips_4_5_r)
1826 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1828 DPFROMREG(rv.d, MIPSInst_FS(ir));
1831 if (!cpu_has_mips_4_5_r)
1834 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1836 DPFROMREG(rv.d, MIPSInst_FS(ir));
1839 handler.u = ieee754dp_abs;
1843 handler.u = ieee754dp_neg;
1848 DPFROMREG(rv.d, MIPSInst_FS(ir));
1851 /* binary op on handler */
1853 DPFROMREG(fs, MIPSInst_FS(ir));
1854 DPFROMREG(ft, MIPSInst_FT(ir));
1856 rv.d = (*handler.b) (fs, ft);
1859 DPFROMREG(fs, MIPSInst_FS(ir));
1860 rv.d = (*handler.u) (fs);
1867 DPFROMREG(fs, MIPSInst_FS(ir));
1868 rv.s = ieee754sp_fdp(fs);
1873 return SIGILL; /* not defined */
1876 DPFROMREG(fs, MIPSInst_FS(ir));
1877 rv.w = ieee754dp_tint(fs); /* wrong */
1885 if (!cpu_has_mips_2_3_4_5_r)
1888 oldrm = ieee754_csr.rm;
1889 DPFROMREG(fs, MIPSInst_FS(ir));
1890 ieee754_csr.rm = MIPSInst_FUNC(ir);
1891 rv.w = ieee754dp_tint(fs);
1892 ieee754_csr.rm = oldrm;
1897 if (!cpu_has_mips_3_4_5_64_r2_r6)
1900 DPFROMREG(fs, MIPSInst_FS(ir));
1901 rv.l = ieee754dp_tlong(fs);
1909 if (!cpu_has_mips_3_4_5_64_r2_r6)
1912 oldrm = ieee754_csr.rm;
1913 DPFROMREG(fs, MIPSInst_FS(ir));
1914 ieee754_csr.rm = MIPSInst_FUNC(ir);
1915 rv.l = ieee754dp_tlong(fs);
1916 ieee754_csr.rm = oldrm;
1921 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1922 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1923 union ieee754dp fs, ft;
1925 DPFROMREG(fs, MIPSInst_FS(ir));
1926 DPFROMREG(ft, MIPSInst_FT(ir));
1927 rv.w = ieee754dp_cmp(fs, ft,
1928 cmptab[cmpop & 0x7], cmpop & 0x8);
1933 (IEEE754_INVALID_OPERATION))
1934 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1947 switch (MIPSInst_FUNC(ir)) {
1949 /* convert word to single precision real */
1950 SPFROMREG(fs, MIPSInst_FS(ir));
1951 rv.s = ieee754sp_fint(fs.bits);
1955 /* convert word to double precision real */
1956 SPFROMREG(fs, MIPSInst_FS(ir));
1957 rv.d = ieee754dp_fint(fs.bits);
1968 if (!cpu_has_mips_3_4_5_64_r2_r6)
1971 DIFROMREG(bits, MIPSInst_FS(ir));
1973 switch (MIPSInst_FUNC(ir)) {
1975 /* convert long to single precision real */
1976 rv.s = ieee754sp_flong(bits);
1980 /* convert long to double precision real */
1981 rv.d = ieee754dp_flong(bits);
1994 * Update the fpu CSR register for this operation.
1995 * If an exception is required, generate a tidy SIGFPE exception,
1996 * without updating the result register.
1997 * Note: cause exception bits do not accumulate, they are rewritten
1998 * for each op; only the flag/sticky bits accumulate.
2000 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
2001 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
2002 /*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
2007 * Now we can safely write the result back to the register file.
2012 if (cpu_has_mips_4_5_r)
2013 cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
2015 cbit = FPU_CSR_COND;
2019 ctx->fcr31 &= ~cbit;
2023 DPTOREG(rv.d, MIPSInst_FD(ir));
2026 SPTOREG(rv.s, MIPSInst_FD(ir));
2029 SITOREG(rv.w, MIPSInst_FD(ir));
2032 if (!cpu_has_mips_3_4_5_64_r2_r6)
2035 DITOREG(rv.l, MIPSInst_FD(ir));
2044 int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
2045 int has_fpu, void *__user *fault_addr)
2047 unsigned long oldepc, prevepc;
2048 struct mm_decoded_insn dec_insn;
2053 oldepc = xcp->cp0_epc;
2055 prevepc = xcp->cp0_epc;
2057 if (get_isa16_mode(prevepc) && cpu_has_mmips) {
2059 * Get next 2 microMIPS instructions and convert them
2060 * into 32-bit instructions.
2062 if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2063 (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2064 (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2065 (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2066 MIPS_FPU_EMU_INC_STATS(errors);
2071 /* Get first instruction. */
2072 if (mm_insn_16bit(*instr_ptr)) {
2073 /* Duplicate the half-word. */
2074 dec_insn.insn = (*instr_ptr << 16) |
2076 /* 16-bit instruction. */
2077 dec_insn.pc_inc = 2;
2080 dec_insn.insn = (*instr_ptr << 16) |
2082 /* 32-bit instruction. */
2083 dec_insn.pc_inc = 4;
2086 /* Get second instruction. */
2087 if (mm_insn_16bit(*instr_ptr)) {
2088 /* Duplicate the half-word. */
2089 dec_insn.next_insn = (*instr_ptr << 16) |
2091 /* 16-bit instruction. */
2092 dec_insn.next_pc_inc = 2;
2094 dec_insn.next_insn = (*instr_ptr << 16) |
2096 /* 32-bit instruction. */
2097 dec_insn.next_pc_inc = 4;
2099 dec_insn.micro_mips_mode = 1;
2101 if ((get_user(dec_insn.insn,
2102 (mips_instruction __user *) xcp->cp0_epc)) ||
2103 (get_user(dec_insn.next_insn,
2104 (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2105 MIPS_FPU_EMU_INC_STATS(errors);
2108 dec_insn.pc_inc = 4;
2109 dec_insn.next_pc_inc = 4;
2110 dec_insn.micro_mips_mode = 0;
2113 if ((dec_insn.insn == 0) ||
2114 ((dec_insn.pc_inc == 2) &&
2115 ((dec_insn.insn & 0xffff) == MM_NOP16)))
2116 xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
2119 * The 'ieee754_csr' is an alias of ctx->fcr31.
2120 * No need to copy ctx->fcr31 to ieee754_csr.
2122 sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
2131 } while (xcp->cp0_epc > prevepc);
2133 /* SIGILL indicates a non-fpu instruction */
2134 if (sig == SIGILL && xcp->cp0_epc != oldepc)
2135 /* but if EPC has advanced, then ignore it */