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>
54 /* Function which emulates a floating point instruction. */
56 static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
59 static int fpux_emu(struct pt_regs *,
60 struct mips_fpu_struct *, mips_instruction, void *__user *);
62 /* Control registers */
64 #define FPCREG_RID 0 /* $0 = revision id */
65 #define FPCREG_CSR 31 /* $31 = csr */
67 /* Determine rounding mode from the RM bits of the FCSR */
68 #define modeindex(v) ((v) & FPU_CSR_RM)
70 /* convert condition code register number to csr bit */
71 static const unsigned int fpucondbit[8] = {
82 /* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
83 static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
84 static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
85 static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
86 static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
89 * This functions translates a 32-bit microMIPS instruction
90 * into a 32-bit MIPS32 instruction. Returns 0 on success
91 * and SIGILL otherwise.
93 static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
95 union mips_instruction insn = *insn_ptr;
96 union mips_instruction mips32_insn = insn;
99 switch (insn.mm_i_format.opcode) {
101 mips32_insn.mm_i_format.opcode = ldc1_op;
102 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
103 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
106 mips32_insn.mm_i_format.opcode = lwc1_op;
107 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
108 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
111 mips32_insn.mm_i_format.opcode = sdc1_op;
112 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
113 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
116 mips32_insn.mm_i_format.opcode = swc1_op;
117 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
118 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
121 /* NOTE: offset is << by 1 if in microMIPS mode. */
122 if ((insn.mm_i_format.rt == mm_bc1f_op) ||
123 (insn.mm_i_format.rt == mm_bc1t_op)) {
124 mips32_insn.fb_format.opcode = cop1_op;
125 mips32_insn.fb_format.bc = bc_op;
126 mips32_insn.fb_format.flag =
127 (insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
132 switch (insn.mm_fp0_format.func) {
141 op = insn.mm_fp0_format.func;
142 if (op == mm_32f_01_op)
144 else if (op == mm_32f_11_op)
146 else if (op == mm_32f_02_op)
148 else if (op == mm_32f_12_op)
150 else if (op == mm_32f_41_op)
152 else if (op == mm_32f_51_op)
154 else if (op == mm_32f_42_op)
158 mips32_insn.fp6_format.opcode = cop1x_op;
159 mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
160 mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
161 mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
162 mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
163 mips32_insn.fp6_format.func = func;
166 func = -1; /* Invalid */
167 op = insn.mm_fp5_format.op & 0x7;
168 if (op == mm_ldxc1_op)
170 else if (op == mm_sdxc1_op)
172 else if (op == mm_lwxc1_op)
174 else if (op == mm_swxc1_op)
178 mips32_insn.r_format.opcode = cop1x_op;
179 mips32_insn.r_format.rs =
180 insn.mm_fp5_format.base;
181 mips32_insn.r_format.rt =
182 insn.mm_fp5_format.index;
183 mips32_insn.r_format.rd = 0;
184 mips32_insn.r_format.re = insn.mm_fp5_format.fd;
185 mips32_insn.r_format.func = func;
190 op = -1; /* Invalid */
191 if (insn.mm_fp2_format.op == mm_fmovt_op)
193 else if (insn.mm_fp2_format.op == mm_fmovf_op)
196 mips32_insn.fp0_format.opcode = cop1_op;
197 mips32_insn.fp0_format.fmt =
198 sdps_format[insn.mm_fp2_format.fmt];
199 mips32_insn.fp0_format.ft =
200 (insn.mm_fp2_format.cc<<2) + op;
201 mips32_insn.fp0_format.fs =
202 insn.mm_fp2_format.fs;
203 mips32_insn.fp0_format.fd =
204 insn.mm_fp2_format.fd;
205 mips32_insn.fp0_format.func = fmovc_op;
210 func = -1; /* Invalid */
211 if (insn.mm_fp0_format.op == mm_fadd_op)
213 else if (insn.mm_fp0_format.op == mm_fsub_op)
215 else if (insn.mm_fp0_format.op == mm_fmul_op)
217 else if (insn.mm_fp0_format.op == mm_fdiv_op)
220 mips32_insn.fp0_format.opcode = cop1_op;
221 mips32_insn.fp0_format.fmt =
222 sdps_format[insn.mm_fp0_format.fmt];
223 mips32_insn.fp0_format.ft =
224 insn.mm_fp0_format.ft;
225 mips32_insn.fp0_format.fs =
226 insn.mm_fp0_format.fs;
227 mips32_insn.fp0_format.fd =
228 insn.mm_fp0_format.fd;
229 mips32_insn.fp0_format.func = func;
234 func = -1; /* Invalid */
235 if (insn.mm_fp0_format.op == mm_fmovn_op)
237 else if (insn.mm_fp0_format.op == mm_fmovz_op)
240 mips32_insn.fp0_format.opcode = cop1_op;
241 mips32_insn.fp0_format.fmt =
242 sdps_format[insn.mm_fp0_format.fmt];
243 mips32_insn.fp0_format.ft =
244 insn.mm_fp0_format.ft;
245 mips32_insn.fp0_format.fs =
246 insn.mm_fp0_format.fs;
247 mips32_insn.fp0_format.fd =
248 insn.mm_fp0_format.fd;
249 mips32_insn.fp0_format.func = func;
253 case mm_32f_73_op: /* POOL32FXF */
254 switch (insn.mm_fp1_format.op) {
259 if ((insn.mm_fp1_format.op & 0x7f) ==
264 mips32_insn.r_format.opcode = spec_op;
265 mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
266 mips32_insn.r_format.rt =
267 (insn.mm_fp4_format.cc << 2) + op;
268 mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
269 mips32_insn.r_format.re = 0;
270 mips32_insn.r_format.func = movc_op;
276 if ((insn.mm_fp1_format.op & 0x7f) ==
279 fmt = swl_format[insn.mm_fp3_format.fmt];
282 fmt = dwl_format[insn.mm_fp3_format.fmt];
284 mips32_insn.fp0_format.opcode = cop1_op;
285 mips32_insn.fp0_format.fmt = fmt;
286 mips32_insn.fp0_format.ft = 0;
287 mips32_insn.fp0_format.fs =
288 insn.mm_fp3_format.fs;
289 mips32_insn.fp0_format.fd =
290 insn.mm_fp3_format.rt;
291 mips32_insn.fp0_format.func = func;
299 if ((insn.mm_fp1_format.op & 0x7f) ==
302 else if ((insn.mm_fp1_format.op & 0x7f) ==
307 mips32_insn.fp0_format.opcode = cop1_op;
308 mips32_insn.fp0_format.fmt =
309 sdps_format[insn.mm_fp3_format.fmt];
310 mips32_insn.fp0_format.ft = 0;
311 mips32_insn.fp0_format.fs =
312 insn.mm_fp3_format.fs;
313 mips32_insn.fp0_format.fd =
314 insn.mm_fp3_format.rt;
315 mips32_insn.fp0_format.func = func;
327 if (insn.mm_fp1_format.op == mm_ffloorl_op)
329 else if (insn.mm_fp1_format.op == mm_ffloorw_op)
331 else if (insn.mm_fp1_format.op == mm_fceill_op)
333 else if (insn.mm_fp1_format.op == mm_fceilw_op)
335 else if (insn.mm_fp1_format.op == mm_ftruncl_op)
337 else if (insn.mm_fp1_format.op == mm_ftruncw_op)
339 else if (insn.mm_fp1_format.op == mm_froundl_op)
341 else if (insn.mm_fp1_format.op == mm_froundw_op)
343 else if (insn.mm_fp1_format.op == mm_fcvtl_op)
347 mips32_insn.fp0_format.opcode = cop1_op;
348 mips32_insn.fp0_format.fmt =
349 sd_format[insn.mm_fp1_format.fmt];
350 mips32_insn.fp0_format.ft = 0;
351 mips32_insn.fp0_format.fs =
352 insn.mm_fp1_format.fs;
353 mips32_insn.fp0_format.fd =
354 insn.mm_fp1_format.rt;
355 mips32_insn.fp0_format.func = func;
360 if (insn.mm_fp1_format.op == mm_frsqrt_op)
362 else if (insn.mm_fp1_format.op == mm_fsqrt_op)
366 mips32_insn.fp0_format.opcode = cop1_op;
367 mips32_insn.fp0_format.fmt =
368 sdps_format[insn.mm_fp1_format.fmt];
369 mips32_insn.fp0_format.ft = 0;
370 mips32_insn.fp0_format.fs =
371 insn.mm_fp1_format.fs;
372 mips32_insn.fp0_format.fd =
373 insn.mm_fp1_format.rt;
374 mips32_insn.fp0_format.func = func;
382 if (insn.mm_fp1_format.op == mm_mfc1_op)
384 else if (insn.mm_fp1_format.op == mm_mtc1_op)
386 else if (insn.mm_fp1_format.op == mm_cfc1_op)
388 else if (insn.mm_fp1_format.op == mm_ctc1_op)
390 else if (insn.mm_fp1_format.op == mm_mfhc1_op)
394 mips32_insn.fp1_format.opcode = cop1_op;
395 mips32_insn.fp1_format.op = op;
396 mips32_insn.fp1_format.rt =
397 insn.mm_fp1_format.rt;
398 mips32_insn.fp1_format.fs =
399 insn.mm_fp1_format.fs;
400 mips32_insn.fp1_format.fd = 0;
401 mips32_insn.fp1_format.func = 0;
407 case mm_32f_74_op: /* c.cond.fmt */
408 mips32_insn.fp0_format.opcode = cop1_op;
409 mips32_insn.fp0_format.fmt =
410 sdps_format[insn.mm_fp4_format.fmt];
411 mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
412 mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
413 mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
414 mips32_insn.fp0_format.func =
415 insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
425 *insn_ptr = mips32_insn;
430 * Redundant with logic already in kernel/branch.c,
431 * embedded in compute_return_epc. At some point,
432 * a single subroutine should be used across both
435 static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
436 unsigned long *contpc)
438 union mips_instruction insn = (union mips_instruction)dec_insn.insn;
440 unsigned int bit = 0;
442 switch (insn.i_format.opcode) {
444 switch (insn.r_format.func) {
446 regs->regs[insn.r_format.rd] =
447 regs->cp0_epc + dec_insn.pc_inc +
448 dec_insn.next_pc_inc;
451 /* For R6, JR already emulated in jalr_op */
452 if (NO_R6EMU && insn.r_format.opcode == jr_op)
454 *contpc = regs->regs[insn.r_format.rs];
459 switch (insn.i_format.rt) {
462 if (NO_R6EMU && (insn.i_format.rs ||
463 insn.i_format.rt == bltzall_op))
466 regs->regs[31] = regs->cp0_epc +
468 dec_insn.next_pc_inc;
474 if ((long)regs->regs[insn.i_format.rs] < 0)
475 *contpc = regs->cp0_epc +
477 (insn.i_format.simmediate << 2);
479 *contpc = regs->cp0_epc +
481 dec_insn.next_pc_inc;
485 if (NO_R6EMU && (insn.i_format.rs ||
486 insn.i_format.rt == bgezall_op))
489 regs->regs[31] = regs->cp0_epc +
491 dec_insn.next_pc_inc;
497 if ((long)regs->regs[insn.i_format.rs] >= 0)
498 *contpc = regs->cp0_epc +
500 (insn.i_format.simmediate << 2);
502 *contpc = regs->cp0_epc +
504 dec_insn.next_pc_inc;
511 regs->regs[31] = regs->cp0_epc +
513 dec_insn.next_pc_inc;
516 *contpc = regs->cp0_epc + dec_insn.pc_inc;
519 *contpc |= (insn.j_format.target << 2);
520 /* Set microMIPS mode bit: XOR for jalx. */
527 if (regs->regs[insn.i_format.rs] ==
528 regs->regs[insn.i_format.rt])
529 *contpc = regs->cp0_epc +
531 (insn.i_format.simmediate << 2);
533 *contpc = regs->cp0_epc +
535 dec_insn.next_pc_inc;
541 if (regs->regs[insn.i_format.rs] !=
542 regs->regs[insn.i_format.rt])
543 *contpc = regs->cp0_epc +
545 (insn.i_format.simmediate << 2);
547 *contpc = regs->cp0_epc +
549 dec_insn.next_pc_inc;
557 * Compact branches for R6 for the
558 * blez and blezl opcodes.
559 * BLEZ | rs = 0 | rt != 0 == BLEZALC
560 * BLEZ | rs = rt != 0 == BGEZALC
561 * BLEZ | rs != 0 | rt != 0 == BGEUC
562 * BLEZL | rs = 0 | rt != 0 == BLEZC
563 * BLEZL | rs = rt != 0 == BGEZC
564 * BLEZL | rs != 0 | rt != 0 == BGEC
566 * For real BLEZ{,L}, rt is always 0.
568 if (cpu_has_mips_r6 && insn.i_format.rt) {
569 if ((insn.i_format.opcode == blez_op) &&
570 ((!insn.i_format.rs && insn.i_format.rt) ||
571 (insn.i_format.rs == insn.i_format.rt)))
572 regs->regs[31] = regs->cp0_epc +
574 *contpc = regs->cp0_epc + dec_insn.pc_inc +
575 dec_insn.next_pc_inc;
579 if ((long)regs->regs[insn.i_format.rs] <= 0)
580 *contpc = regs->cp0_epc +
582 (insn.i_format.simmediate << 2);
584 *contpc = regs->cp0_epc +
586 dec_insn.next_pc_inc;
593 * Compact branches for R6 for the
594 * bgtz and bgtzl opcodes.
595 * BGTZ | rs = 0 | rt != 0 == BGTZALC
596 * BGTZ | rs = rt != 0 == BLTZALC
597 * BGTZ | rs != 0 | rt != 0 == BLTUC
598 * BGTZL | rs = 0 | rt != 0 == BGTZC
599 * BGTZL | rs = rt != 0 == BLTZC
600 * BGTZL | rs != 0 | rt != 0 == BLTC
602 * *ZALC varint for BGTZ &&& rt != 0
603 * For real GTZ{,L}, rt is always 0.
605 if (cpu_has_mips_r6 && insn.i_format.rt) {
606 if ((insn.i_format.opcode == blez_op) &&
607 ((!insn.i_format.rs && insn.i_format.rt) ||
608 (insn.i_format.rs == insn.i_format.rt)))
609 regs->regs[31] = regs->cp0_epc +
611 *contpc = regs->cp0_epc + dec_insn.pc_inc +
612 dec_insn.next_pc_inc;
617 if ((long)regs->regs[insn.i_format.rs] > 0)
618 *contpc = regs->cp0_epc +
620 (insn.i_format.simmediate << 2);
622 *contpc = regs->cp0_epc +
624 dec_insn.next_pc_inc;
628 if (!cpu_has_mips_r6)
630 if (insn.i_format.rt && !insn.i_format.rs)
631 regs->regs[31] = regs->cp0_epc + 4;
632 *contpc = regs->cp0_epc + dec_insn.pc_inc +
633 dec_insn.next_pc_inc;
636 #ifdef CONFIG_CPU_CAVIUM_OCTEON
637 case lwc2_op: /* This is bbit0 on Octeon */
638 if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
639 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
641 *contpc = regs->cp0_epc + 8;
643 case ldc2_op: /* This is bbit032 on Octeon */
644 if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
645 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
647 *contpc = regs->cp0_epc + 8;
649 case swc2_op: /* This is bbit1 on Octeon */
650 if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
651 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
653 *contpc = regs->cp0_epc + 8;
655 case sdc2_op: /* This is bbit132 on Octeon */
656 if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
657 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
659 *contpc = regs->cp0_epc + 8;
664 * Only valid for MIPS R6 but we can still end up
665 * here from a broken userland so just tell emulator
666 * this is not a branch and let it break later on.
668 if (!cpu_has_mips_r6)
670 *contpc = regs->cp0_epc + dec_insn.pc_inc +
671 dec_insn.next_pc_inc;
675 if (!cpu_has_mips_r6)
677 regs->regs[31] = regs->cp0_epc + 4;
678 *contpc = regs->cp0_epc + dec_insn.pc_inc +
679 dec_insn.next_pc_inc;
685 /* Need to check for R6 bc1nez and bc1eqz branches */
686 if (cpu_has_mips_r6 &&
687 ((insn.i_format.rs == bc1eqz_op) ||
688 (insn.i_format.rs == bc1nez_op))) {
690 switch (insn.i_format.rs) {
692 if (get_fpr32(¤t->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1)
696 if (!(get_fpr32(¤t->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1))
701 *contpc = regs->cp0_epc +
703 (insn.i_format.simmediate << 2);
705 *contpc = regs->cp0_epc +
707 dec_insn.next_pc_inc;
711 /* R2/R6 compatible cop1 instruction. Fall through */
714 if (insn.i_format.rs == bc_op) {
717 fcr31 = read_32bit_cp1_register(CP1_STATUS);
719 fcr31 = current->thread.fpu.fcr31;
722 bit = (insn.i_format.rt >> 2);
725 switch (insn.i_format.rt & 3) {
728 if (~fcr31 & (1 << bit))
729 *contpc = regs->cp0_epc +
731 (insn.i_format.simmediate << 2);
733 *contpc = regs->cp0_epc +
735 dec_insn.next_pc_inc;
739 if (fcr31 & (1 << bit))
740 *contpc = regs->cp0_epc +
742 (insn.i_format.simmediate << 2);
744 *contpc = regs->cp0_epc +
746 dec_insn.next_pc_inc;
756 * In the Linux kernel, we support selection of FPR format on the
757 * basis of the Status.FR bit. If an FPU is not present, the FR bit
758 * is hardwired to zero, which would imply a 32-bit FPU even for
759 * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
760 * FPU emu is slow and bulky and optimizing this function offers fairly
761 * sizeable benefits so we try to be clever and make this function return
762 * a constant whenever possible, that is on 64-bit kernels without O32
763 * compatibility enabled and on 32-bit without 64-bit FPU support.
765 static inline int cop1_64bit(struct pt_regs *xcp)
767 if (config_enabled(CONFIG_64BIT) && !config_enabled(CONFIG_MIPS32_O32))
769 else if (config_enabled(CONFIG_32BIT) &&
770 !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
773 return !test_thread_flag(TIF_32BIT_FPREGS);
776 static inline bool hybrid_fprs(void)
778 return test_thread_flag(TIF_HYBRID_FPREGS);
781 #define SIFROMREG(si, x) \
783 if (cop1_64bit(xcp) && !hybrid_fprs()) \
784 (si) = (int)get_fpr32(&ctx->fpr[x], 0); \
786 (si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
789 #define SITOREG(si, x) \
791 if (cop1_64bit(xcp) && !hybrid_fprs()) { \
793 set_fpr32(&ctx->fpr[x], 0, si); \
794 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
795 set_fpr32(&ctx->fpr[x], i, 0); \
797 set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
801 #define SIFROMHREG(si, x) ((si) = (int)get_fpr32(&ctx->fpr[x], 1))
803 #define SITOHREG(si, x) \
806 set_fpr32(&ctx->fpr[x], 1, si); \
807 for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
808 set_fpr32(&ctx->fpr[x], i, 0); \
811 #define DIFROMREG(di, x) \
812 ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
814 #define DITOREG(di, x) \
817 fpr = (x) & ~(cop1_64bit(xcp) == 0); \
818 set_fpr64(&ctx->fpr[fpr], 0, di); \
819 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
820 set_fpr64(&ctx->fpr[fpr], i, 0); \
823 #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
824 #define SPTOREG(sp, x) SITOREG((sp).bits, x)
825 #define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
826 #define DPTOREG(dp, x) DITOREG((dp).bits, x)
829 * Emulate the single floating point instruction pointed at by EPC.
830 * Two instructions if the instruction is in a branch delay slot.
833 static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
834 struct mm_decoded_insn dec_insn, void *__user *fault_addr)
836 unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
837 unsigned int cond, cbit;
848 * These are giving gcc a gentle hint about what to expect in
849 * dec_inst in order to do better optimization.
851 if (!cpu_has_mmips && dec_insn.micro_mips_mode)
854 /* XXX NEC Vr54xx bug workaround */
855 if (delay_slot(xcp)) {
856 if (dec_insn.micro_mips_mode) {
857 if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
858 clear_delay_slot(xcp);
860 if (!isBranchInstr(xcp, dec_insn, &contpc))
861 clear_delay_slot(xcp);
865 if (delay_slot(xcp)) {
867 * The instruction to be emulated is in a branch delay slot
868 * which means that we have to emulate the branch instruction
869 * BEFORE we do the cop1 instruction.
871 * This branch could be a COP1 branch, but in that case we
872 * would have had a trap for that instruction, and would not
873 * come through this route.
875 * Linux MIPS branch emulator operates on context, updating the
878 ir = dec_insn.next_insn; /* process delay slot instr */
879 pc_inc = dec_insn.next_pc_inc;
881 ir = dec_insn.insn; /* process current instr */
882 pc_inc = dec_insn.pc_inc;
886 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
887 * instructions, we want to convert microMIPS FPU instructions
888 * into MIPS32 instructions so that we could reuse all of the
889 * FPU emulation code.
891 * NOTE: We cannot do this for branch instructions since they
892 * are not a subset. Example: Cannot emulate a 16-bit
893 * aligned target address with a MIPS32 instruction.
895 if (dec_insn.micro_mips_mode) {
897 * If next instruction is a 16-bit instruction, then it
898 * it cannot be a FPU instruction. This could happen
899 * since we can be called for non-FPU instructions.
902 (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
908 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
909 MIPS_FPU_EMU_INC_STATS(emulated);
910 switch (MIPSInst_OPCODE(ir)) {
912 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
914 MIPS_FPU_EMU_INC_STATS(loads);
916 if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
917 MIPS_FPU_EMU_INC_STATS(errors);
921 if (__get_user(dval, dva)) {
922 MIPS_FPU_EMU_INC_STATS(errors);
926 DITOREG(dval, MIPSInst_RT(ir));
930 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
932 MIPS_FPU_EMU_INC_STATS(stores);
933 DIFROMREG(dval, MIPSInst_RT(ir));
934 if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
935 MIPS_FPU_EMU_INC_STATS(errors);
939 if (__put_user(dval, dva)) {
940 MIPS_FPU_EMU_INC_STATS(errors);
947 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
949 MIPS_FPU_EMU_INC_STATS(loads);
950 if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
951 MIPS_FPU_EMU_INC_STATS(errors);
955 if (__get_user(wval, wva)) {
956 MIPS_FPU_EMU_INC_STATS(errors);
960 SITOREG(wval, MIPSInst_RT(ir));
964 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
966 MIPS_FPU_EMU_INC_STATS(stores);
967 SIFROMREG(wval, MIPSInst_RT(ir));
968 if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
969 MIPS_FPU_EMU_INC_STATS(errors);
973 if (__put_user(wval, wva)) {
974 MIPS_FPU_EMU_INC_STATS(errors);
981 switch (MIPSInst_RS(ir)) {
983 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
986 /* copregister fs -> gpr[rt] */
987 if (MIPSInst_RT(ir) != 0) {
988 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
994 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
997 /* copregister fs <- rt */
998 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1002 if (!cpu_has_mips_r2)
1005 /* copregister rd -> gpr[rt] */
1006 if (MIPSInst_RT(ir) != 0) {
1007 SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
1013 if (!cpu_has_mips_r2)
1016 /* copregister rd <- gpr[rt] */
1017 SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1021 /* copregister rd -> gpr[rt] */
1022 if (MIPSInst_RT(ir) != 0) {
1023 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1029 /* copregister rd <- rt */
1030 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1034 /* cop control register rd -> gpr[rt] */
1035 if (MIPSInst_RD(ir) == FPCREG_CSR) {
1037 value = (value & ~FPU_CSR_RM) | modeindex(value);
1038 pr_debug("%p gpr[%d]<-csr=%08x\n",
1039 (void *) (xcp->cp0_epc),
1040 MIPSInst_RT(ir), value);
1042 else if (MIPSInst_RD(ir) == FPCREG_RID)
1046 if (MIPSInst_RT(ir))
1047 xcp->regs[MIPSInst_RT(ir)] = value;
1051 /* copregister rd <- rt */
1052 if (MIPSInst_RT(ir) == 0)
1055 value = xcp->regs[MIPSInst_RT(ir)];
1057 /* we only have one writable control reg
1059 if (MIPSInst_RD(ir) == FPCREG_CSR) {
1060 pr_debug("%p gpr[%d]->csr=%08x\n",
1061 (void *) (xcp->cp0_epc),
1062 MIPSInst_RT(ir), value);
1065 * Don't write reserved bits,
1066 * and convert to ieee library modes
1068 ctx->fcr31 = (value & ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
1071 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1077 if (delay_slot(xcp))
1080 if (cpu_has_mips_4_5_r)
1081 cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
1083 cbit = FPU_CSR_COND;
1084 cond = ctx->fcr31 & cbit;
1087 switch (MIPSInst_RT(ir) & 3) {
1098 /* thats an illegal instruction */
1102 set_delay_slot(xcp);
1105 * Branch taken: emulate dslot instruction
1107 xcp->cp0_epc += dec_insn.pc_inc;
1109 contpc = MIPSInst_SIMM(ir);
1110 ir = dec_insn.next_insn;
1111 if (dec_insn.micro_mips_mode) {
1112 contpc = (xcp->cp0_epc + (contpc << 1));
1114 /* If 16-bit instruction, not FPU. */
1115 if ((dec_insn.next_pc_inc == 2) ||
1116 (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1119 * Since this instruction will
1120 * be put on the stack with
1121 * 32-bit words, get around
1122 * this problem by putting a
1123 * NOP16 as the second one.
1125 if (dec_insn.next_pc_inc == 2)
1126 ir = (ir & (~0xffff)) | MM_NOP16;
1129 * Single step the non-CP1
1130 * instruction in the dslot.
1132 return mips_dsemul(xcp, ir, contpc);
1135 contpc = (xcp->cp0_epc + (contpc << 2));
1137 switch (MIPSInst_OPCODE(ir)) {
1146 if (cpu_has_mips_2_3_4_5 ||
1157 if (cpu_has_mips_4_5 || cpu_has_mips64 || cpu_has_mips32r2)
1158 /* its one of ours */
1164 if (!cpu_has_mips_4_5_r)
1167 if (MIPSInst_FUNC(ir) == movc_op)
1173 * Single step the non-cp1
1174 * instruction in the dslot
1176 return mips_dsemul(xcp, ir, contpc);
1177 } else if (likely) { /* branch not taken */
1179 * branch likely nullifies
1180 * dslot if not taken
1182 xcp->cp0_epc += dec_insn.pc_inc;
1183 contpc += dec_insn.pc_inc;
1185 * else continue & execute
1186 * dslot as normal insn
1192 if (!(MIPSInst_RS(ir) & 0x10))
1195 /* a real fpu computation instruction */
1196 if ((sig = fpu_emu(xcp, ctx, ir)))
1202 if (!cpu_has_mips_4_5 && !cpu_has_mips64 && !cpu_has_mips32r2)
1205 sig = fpux_emu(xcp, ctx, ir, fault_addr);
1211 if (!cpu_has_mips_4_5_r)
1214 if (MIPSInst_FUNC(ir) != movc_op)
1216 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
1217 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
1218 xcp->regs[MIPSInst_RD(ir)] =
1219 xcp->regs[MIPSInst_RS(ir)];
1227 xcp->cp0_epc = contpc;
1228 clear_delay_slot(xcp);
1234 * Conversion table from MIPS compare ops 48-63
1235 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
1237 static const unsigned char cmptab[8] = {
1238 0, /* cmp_0 (sig) cmp_sf */
1239 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
1240 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
1241 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
1242 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
1243 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
1244 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
1245 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
1250 * Additional MIPS4 instructions
1253 #define DEF3OP(name, p, f1, f2, f3) \
1254 static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \
1255 union ieee754##p s, union ieee754##p t) \
1257 struct _ieee754_csr ieee754_csr_save; \
1259 ieee754_csr_save = ieee754_csr; \
1261 ieee754_csr_save.cx |= ieee754_csr.cx; \
1262 ieee754_csr_save.sx |= ieee754_csr.sx; \
1264 ieee754_csr.cx |= ieee754_csr_save.cx; \
1265 ieee754_csr.sx |= ieee754_csr_save.sx; \
1269 static union ieee754dp fpemu_dp_recip(union ieee754dp d)
1271 return ieee754dp_div(ieee754dp_one(0), d);
1274 static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
1276 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
1279 static union ieee754sp fpemu_sp_recip(union ieee754sp s)
1281 return ieee754sp_div(ieee754sp_one(0), s);
1284 static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
1286 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
1289 DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
1290 DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
1291 DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
1292 DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
1293 DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
1294 DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
1295 DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
1296 DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
1298 static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1299 mips_instruction ir, void *__user *fault_addr)
1301 unsigned rcsr = 0; /* resulting csr */
1303 MIPS_FPU_EMU_INC_STATS(cp1xops);
1305 switch (MIPSInst_FMA_FFMT(ir)) {
1306 case s_fmt:{ /* 0 */
1308 union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
1309 union ieee754sp fd, fr, fs, ft;
1313 switch (MIPSInst_FUNC(ir)) {
1315 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1316 xcp->regs[MIPSInst_FT(ir)]);
1318 MIPS_FPU_EMU_INC_STATS(loads);
1319 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
1320 MIPS_FPU_EMU_INC_STATS(errors);
1324 if (__get_user(val, va)) {
1325 MIPS_FPU_EMU_INC_STATS(errors);
1329 SITOREG(val, MIPSInst_FD(ir));
1333 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1334 xcp->regs[MIPSInst_FT(ir)]);
1336 MIPS_FPU_EMU_INC_STATS(stores);
1338 SIFROMREG(val, MIPSInst_FS(ir));
1339 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1340 MIPS_FPU_EMU_INC_STATS(errors);
1344 if (put_user(val, va)) {
1345 MIPS_FPU_EMU_INC_STATS(errors);
1352 handler = fpemu_sp_madd;
1355 handler = fpemu_sp_msub;
1358 handler = fpemu_sp_nmadd;
1361 handler = fpemu_sp_nmsub;
1365 SPFROMREG(fr, MIPSInst_FR(ir));
1366 SPFROMREG(fs, MIPSInst_FS(ir));
1367 SPFROMREG(ft, MIPSInst_FT(ir));
1368 fd = (*handler) (fr, fs, ft);
1369 SPTOREG(fd, MIPSInst_FD(ir));
1372 if (ieee754_cxtest(IEEE754_INEXACT)) {
1373 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
1374 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1376 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1377 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
1378 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1380 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1381 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
1382 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1384 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1385 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
1386 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1389 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1390 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1391 /*printk ("SIGFPE: FPU csr = %08x\n",
1404 case d_fmt:{ /* 1 */
1405 union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
1406 union ieee754dp fd, fr, fs, ft;
1410 switch (MIPSInst_FUNC(ir)) {
1412 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1413 xcp->regs[MIPSInst_FT(ir)]);
1415 MIPS_FPU_EMU_INC_STATS(loads);
1416 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
1417 MIPS_FPU_EMU_INC_STATS(errors);
1421 if (__get_user(val, va)) {
1422 MIPS_FPU_EMU_INC_STATS(errors);
1426 DITOREG(val, MIPSInst_FD(ir));
1430 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1431 xcp->regs[MIPSInst_FT(ir)]);
1433 MIPS_FPU_EMU_INC_STATS(stores);
1434 DIFROMREG(val, MIPSInst_FS(ir));
1435 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
1436 MIPS_FPU_EMU_INC_STATS(errors);
1440 if (__put_user(val, va)) {
1441 MIPS_FPU_EMU_INC_STATS(errors);
1448 handler = fpemu_dp_madd;
1451 handler = fpemu_dp_msub;
1454 handler = fpemu_dp_nmadd;
1457 handler = fpemu_dp_nmsub;
1461 DPFROMREG(fr, MIPSInst_FR(ir));
1462 DPFROMREG(fs, MIPSInst_FS(ir));
1463 DPFROMREG(ft, MIPSInst_FT(ir));
1464 fd = (*handler) (fr, fs, ft);
1465 DPTOREG(fd, MIPSInst_FD(ir));
1475 if (MIPSInst_FUNC(ir) != pfetch_op)
1478 /* ignore prefx operation */
1491 * Emulate a single COP1 arithmetic instruction.
1493 static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1494 mips_instruction ir)
1496 int rfmt; /* resulting format */
1497 unsigned rcsr = 0; /* resulting csr */
1506 } rv; /* resulting value */
1509 MIPS_FPU_EMU_INC_STATS(cp1ops);
1510 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
1511 case s_fmt: { /* 0 */
1513 union ieee754sp(*b) (union ieee754sp, union ieee754sp);
1514 union ieee754sp(*u) (union ieee754sp);
1516 union ieee754sp fs, ft;
1518 switch (MIPSInst_FUNC(ir)) {
1521 handler.b = ieee754sp_add;
1524 handler.b = ieee754sp_sub;
1527 handler.b = ieee754sp_mul;
1530 handler.b = ieee754sp_div;
1535 if (!cpu_has_mips_4_5_r)
1538 handler.u = ieee754sp_sqrt;
1542 * Note that on some MIPS IV implementations such as the
1543 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1544 * achieve full IEEE-754 accuracy - however this emulator does.
1547 if (!cpu_has_mips_4_5_r2)
1550 handler.u = fpemu_sp_rsqrt;
1554 if (!cpu_has_mips_4_5_r2)
1557 handler.u = fpemu_sp_recip;
1561 if (!cpu_has_mips_4_5_r)
1564 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1565 if (((ctx->fcr31 & cond) != 0) !=
1566 ((MIPSInst_FT(ir) & 1) != 0))
1568 SPFROMREG(rv.s, MIPSInst_FS(ir));
1572 if (!cpu_has_mips_4_5_r)
1575 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1577 SPFROMREG(rv.s, MIPSInst_FS(ir));
1581 if (!cpu_has_mips_4_5_r)
1584 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1586 SPFROMREG(rv.s, MIPSInst_FS(ir));
1590 handler.u = ieee754sp_abs;
1594 handler.u = ieee754sp_neg;
1599 SPFROMREG(rv.s, MIPSInst_FS(ir));
1602 /* binary op on handler */
1604 SPFROMREG(fs, MIPSInst_FS(ir));
1605 SPFROMREG(ft, MIPSInst_FT(ir));
1607 rv.s = (*handler.b) (fs, ft);
1610 SPFROMREG(fs, MIPSInst_FS(ir));
1611 rv.s = (*handler.u) (fs);
1614 if (ieee754_cxtest(IEEE754_INEXACT)) {
1615 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
1616 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1618 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1619 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
1620 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1622 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1623 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
1624 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1626 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1627 MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
1628 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
1630 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1631 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
1632 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1636 /* unary conv ops */
1638 return SIGILL; /* not defined */
1641 SPFROMREG(fs, MIPSInst_FS(ir));
1642 rv.d = ieee754dp_fsp(fs);
1647 SPFROMREG(fs, MIPSInst_FS(ir));
1648 rv.w = ieee754sp_tint(fs);
1656 if (!cpu_has_mips_2_3_4_5 && !cpu_has_mips64)
1659 oldrm = ieee754_csr.rm;
1660 SPFROMREG(fs, MIPSInst_FS(ir));
1661 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
1662 rv.w = ieee754sp_tint(fs);
1663 ieee754_csr.rm = oldrm;
1668 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1671 SPFROMREG(fs, MIPSInst_FS(ir));
1672 rv.l = ieee754sp_tlong(fs);
1680 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1683 oldrm = ieee754_csr.rm;
1684 SPFROMREG(fs, MIPSInst_FS(ir));
1685 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
1686 rv.l = ieee754sp_tlong(fs);
1687 ieee754_csr.rm = oldrm;
1692 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1693 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1694 union ieee754sp fs, ft;
1696 SPFROMREG(fs, MIPSInst_FS(ir));
1697 SPFROMREG(ft, MIPSInst_FT(ir));
1698 rv.w = ieee754sp_cmp(fs, ft,
1699 cmptab[cmpop & 0x7], cmpop & 0x8);
1701 if ((cmpop & 0x8) && ieee754_cxtest
1702 (IEEE754_INVALID_OPERATION))
1703 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1715 union ieee754dp fs, ft;
1717 union ieee754dp(*b) (union ieee754dp, union ieee754dp);
1718 union ieee754dp(*u) (union ieee754dp);
1721 switch (MIPSInst_FUNC(ir)) {
1724 handler.b = ieee754dp_add;
1727 handler.b = ieee754dp_sub;
1730 handler.b = ieee754dp_mul;
1733 handler.b = ieee754dp_div;
1738 if (!cpu_has_mips_2_3_4_5_r)
1741 handler.u = ieee754dp_sqrt;
1744 * Note that on some MIPS IV implementations such as the
1745 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1746 * achieve full IEEE-754 accuracy - however this emulator does.
1749 if (!cpu_has_mips_4_5_r2)
1752 handler.u = fpemu_dp_rsqrt;
1755 if (!cpu_has_mips_4_5_r2)
1758 handler.u = fpemu_dp_recip;
1761 if (!cpu_has_mips_4_5_r)
1764 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1765 if (((ctx->fcr31 & cond) != 0) !=
1766 ((MIPSInst_FT(ir) & 1) != 0))
1768 DPFROMREG(rv.d, MIPSInst_FS(ir));
1771 if (!cpu_has_mips_4_5_r)
1774 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1776 DPFROMREG(rv.d, MIPSInst_FS(ir));
1779 if (!cpu_has_mips_4_5_r)
1782 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1784 DPFROMREG(rv.d, MIPSInst_FS(ir));
1787 handler.u = ieee754dp_abs;
1791 handler.u = ieee754dp_neg;
1796 DPFROMREG(rv.d, MIPSInst_FS(ir));
1799 /* binary op on handler */
1801 DPFROMREG(fs, MIPSInst_FS(ir));
1802 DPFROMREG(ft, MIPSInst_FT(ir));
1804 rv.d = (*handler.b) (fs, ft);
1807 DPFROMREG(fs, MIPSInst_FS(ir));
1808 rv.d = (*handler.u) (fs);
1815 DPFROMREG(fs, MIPSInst_FS(ir));
1816 rv.s = ieee754sp_fdp(fs);
1821 return SIGILL; /* not defined */
1824 DPFROMREG(fs, MIPSInst_FS(ir));
1825 rv.w = ieee754dp_tint(fs); /* wrong */
1833 if (!cpu_has_mips_2_3_4_5_r)
1836 oldrm = ieee754_csr.rm;
1837 DPFROMREG(fs, MIPSInst_FS(ir));
1838 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
1839 rv.w = ieee754dp_tint(fs);
1840 ieee754_csr.rm = oldrm;
1845 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1848 DPFROMREG(fs, MIPSInst_FS(ir));
1849 rv.l = ieee754dp_tlong(fs);
1857 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1860 oldrm = ieee754_csr.rm;
1861 DPFROMREG(fs, MIPSInst_FS(ir));
1862 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
1863 rv.l = ieee754dp_tlong(fs);
1864 ieee754_csr.rm = oldrm;
1869 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1870 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1871 union ieee754dp fs, ft;
1873 DPFROMREG(fs, MIPSInst_FS(ir));
1874 DPFROMREG(ft, MIPSInst_FT(ir));
1875 rv.w = ieee754dp_cmp(fs, ft,
1876 cmptab[cmpop & 0x7], cmpop & 0x8);
1881 (IEEE754_INVALID_OPERATION))
1882 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1895 switch (MIPSInst_FUNC(ir)) {
1897 /* convert word to single precision real */
1898 SPFROMREG(fs, MIPSInst_FS(ir));
1899 rv.s = ieee754sp_fint(fs.bits);
1903 /* convert word to double precision real */
1904 SPFROMREG(fs, MIPSInst_FS(ir));
1905 rv.d = ieee754dp_fint(fs.bits);
1916 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1919 DIFROMREG(bits, MIPSInst_FS(ir));
1921 switch (MIPSInst_FUNC(ir)) {
1923 /* convert long to single precision real */
1924 rv.s = ieee754sp_flong(bits);
1928 /* convert long to double precision real */
1929 rv.d = ieee754dp_flong(bits);
1942 * Update the fpu CSR register for this operation.
1943 * If an exception is required, generate a tidy SIGFPE exception,
1944 * without updating the result register.
1945 * Note: cause exception bits do not accumulate, they are rewritten
1946 * for each op; only the flag/sticky bits accumulate.
1948 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1949 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1950 /*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
1955 * Now we can safely write the result back to the register file.
1960 if (cpu_has_mips_4_5_r)
1961 cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
1963 cbit = FPU_CSR_COND;
1967 ctx->fcr31 &= ~cbit;
1971 DPTOREG(rv.d, MIPSInst_FD(ir));
1974 SPTOREG(rv.s, MIPSInst_FD(ir));
1977 SITOREG(rv.w, MIPSInst_FD(ir));
1980 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1983 DITOREG(rv.l, MIPSInst_FD(ir));
1992 int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1993 int has_fpu, void *__user *fault_addr)
1995 unsigned long oldepc, prevepc;
1996 struct mm_decoded_insn dec_insn;
2001 oldepc = xcp->cp0_epc;
2003 prevepc = xcp->cp0_epc;
2005 if (get_isa16_mode(prevepc) && cpu_has_mmips) {
2007 * Get next 2 microMIPS instructions and convert them
2008 * into 32-bit instructions.
2010 if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2011 (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2012 (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2013 (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2014 MIPS_FPU_EMU_INC_STATS(errors);
2019 /* Get first instruction. */
2020 if (mm_insn_16bit(*instr_ptr)) {
2021 /* Duplicate the half-word. */
2022 dec_insn.insn = (*instr_ptr << 16) |
2024 /* 16-bit instruction. */
2025 dec_insn.pc_inc = 2;
2028 dec_insn.insn = (*instr_ptr << 16) |
2030 /* 32-bit instruction. */
2031 dec_insn.pc_inc = 4;
2034 /* Get second instruction. */
2035 if (mm_insn_16bit(*instr_ptr)) {
2036 /* Duplicate the half-word. */
2037 dec_insn.next_insn = (*instr_ptr << 16) |
2039 /* 16-bit instruction. */
2040 dec_insn.next_pc_inc = 2;
2042 dec_insn.next_insn = (*instr_ptr << 16) |
2044 /* 32-bit instruction. */
2045 dec_insn.next_pc_inc = 4;
2047 dec_insn.micro_mips_mode = 1;
2049 if ((get_user(dec_insn.insn,
2050 (mips_instruction __user *) xcp->cp0_epc)) ||
2051 (get_user(dec_insn.next_insn,
2052 (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2053 MIPS_FPU_EMU_INC_STATS(errors);
2056 dec_insn.pc_inc = 4;
2057 dec_insn.next_pc_inc = 4;
2058 dec_insn.micro_mips_mode = 0;
2061 if ((dec_insn.insn == 0) ||
2062 ((dec_insn.pc_inc == 2) &&
2063 ((dec_insn.insn & 0xffff) == MM_NOP16)))
2064 xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
2067 * The 'ieee754_csr' is an alias of
2068 * ctx->fcr31. No need to copy ctx->fcr31 to
2069 * ieee754_csr. But ieee754_csr.rm is ieee
2070 * library modes. (not mips rounding mode)
2072 sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
2081 } while (xcp->cp0_epc > prevepc);
2083 /* SIGILL indicates a non-fpu instruction */
2084 if (sig == SIGILL && xcp->cp0_epc != oldepc)
2085 /* but if EPC has advanced, then ignore it */