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 MIPS rounding mode (0..3) to IEEE library modes. */
71 static const unsigned char ieee_rm[4] = {
72 [FPU_CSR_RN] = IEEE754_RN,
73 [FPU_CSR_RZ] = IEEE754_RZ,
74 [FPU_CSR_RU] = IEEE754_RU,
75 [FPU_CSR_RD] = IEEE754_RD,
77 /* Convert IEEE library modes to MIPS rounding mode (0..3). */
78 static const unsigned char mips_rm[4] = {
79 [IEEE754_RN] = FPU_CSR_RN,
80 [IEEE754_RZ] = FPU_CSR_RZ,
81 [IEEE754_RD] = FPU_CSR_RD,
82 [IEEE754_RU] = FPU_CSR_RU,
85 /* convert condition code register number to csr bit */
86 static const unsigned int fpucondbit[8] = {
97 /* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
98 static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
99 static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
100 static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
101 static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
104 * This functions translates a 32-bit microMIPS instruction
105 * into a 32-bit MIPS32 instruction. Returns 0 on success
106 * and SIGILL otherwise.
108 static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
110 union mips_instruction insn = *insn_ptr;
111 union mips_instruction mips32_insn = insn;
114 switch (insn.mm_i_format.opcode) {
116 mips32_insn.mm_i_format.opcode = ldc1_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 mips32_insn.mm_i_format.opcode = lwc1_op;
122 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
123 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
126 mips32_insn.mm_i_format.opcode = sdc1_op;
127 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
128 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
131 mips32_insn.mm_i_format.opcode = swc1_op;
132 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
133 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
136 /* NOTE: offset is << by 1 if in microMIPS mode. */
137 if ((insn.mm_i_format.rt == mm_bc1f_op) ||
138 (insn.mm_i_format.rt == mm_bc1t_op)) {
139 mips32_insn.fb_format.opcode = cop1_op;
140 mips32_insn.fb_format.bc = bc_op;
141 mips32_insn.fb_format.flag =
142 (insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
147 switch (insn.mm_fp0_format.func) {
156 op = insn.mm_fp0_format.func;
157 if (op == mm_32f_01_op)
159 else if (op == mm_32f_11_op)
161 else if (op == mm_32f_02_op)
163 else if (op == mm_32f_12_op)
165 else if (op == mm_32f_41_op)
167 else if (op == mm_32f_51_op)
169 else if (op == mm_32f_42_op)
173 mips32_insn.fp6_format.opcode = cop1x_op;
174 mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
175 mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
176 mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
177 mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
178 mips32_insn.fp6_format.func = func;
181 func = -1; /* Invalid */
182 op = insn.mm_fp5_format.op & 0x7;
183 if (op == mm_ldxc1_op)
185 else if (op == mm_sdxc1_op)
187 else if (op == mm_lwxc1_op)
189 else if (op == mm_swxc1_op)
193 mips32_insn.r_format.opcode = cop1x_op;
194 mips32_insn.r_format.rs =
195 insn.mm_fp5_format.base;
196 mips32_insn.r_format.rt =
197 insn.mm_fp5_format.index;
198 mips32_insn.r_format.rd = 0;
199 mips32_insn.r_format.re = insn.mm_fp5_format.fd;
200 mips32_insn.r_format.func = func;
205 op = -1; /* Invalid */
206 if (insn.mm_fp2_format.op == mm_fmovt_op)
208 else if (insn.mm_fp2_format.op == mm_fmovf_op)
211 mips32_insn.fp0_format.opcode = cop1_op;
212 mips32_insn.fp0_format.fmt =
213 sdps_format[insn.mm_fp2_format.fmt];
214 mips32_insn.fp0_format.ft =
215 (insn.mm_fp2_format.cc<<2) + op;
216 mips32_insn.fp0_format.fs =
217 insn.mm_fp2_format.fs;
218 mips32_insn.fp0_format.fd =
219 insn.mm_fp2_format.fd;
220 mips32_insn.fp0_format.func = fmovc_op;
225 func = -1; /* Invalid */
226 if (insn.mm_fp0_format.op == mm_fadd_op)
228 else if (insn.mm_fp0_format.op == mm_fsub_op)
230 else if (insn.mm_fp0_format.op == mm_fmul_op)
232 else if (insn.mm_fp0_format.op == mm_fdiv_op)
235 mips32_insn.fp0_format.opcode = cop1_op;
236 mips32_insn.fp0_format.fmt =
237 sdps_format[insn.mm_fp0_format.fmt];
238 mips32_insn.fp0_format.ft =
239 insn.mm_fp0_format.ft;
240 mips32_insn.fp0_format.fs =
241 insn.mm_fp0_format.fs;
242 mips32_insn.fp0_format.fd =
243 insn.mm_fp0_format.fd;
244 mips32_insn.fp0_format.func = func;
249 func = -1; /* Invalid */
250 if (insn.mm_fp0_format.op == mm_fmovn_op)
252 else if (insn.mm_fp0_format.op == mm_fmovz_op)
255 mips32_insn.fp0_format.opcode = cop1_op;
256 mips32_insn.fp0_format.fmt =
257 sdps_format[insn.mm_fp0_format.fmt];
258 mips32_insn.fp0_format.ft =
259 insn.mm_fp0_format.ft;
260 mips32_insn.fp0_format.fs =
261 insn.mm_fp0_format.fs;
262 mips32_insn.fp0_format.fd =
263 insn.mm_fp0_format.fd;
264 mips32_insn.fp0_format.func = func;
268 case mm_32f_73_op: /* POOL32FXF */
269 switch (insn.mm_fp1_format.op) {
274 if ((insn.mm_fp1_format.op & 0x7f) ==
279 mips32_insn.r_format.opcode = spec_op;
280 mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
281 mips32_insn.r_format.rt =
282 (insn.mm_fp4_format.cc << 2) + op;
283 mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
284 mips32_insn.r_format.re = 0;
285 mips32_insn.r_format.func = movc_op;
291 if ((insn.mm_fp1_format.op & 0x7f) ==
294 fmt = swl_format[insn.mm_fp3_format.fmt];
297 fmt = dwl_format[insn.mm_fp3_format.fmt];
299 mips32_insn.fp0_format.opcode = cop1_op;
300 mips32_insn.fp0_format.fmt = fmt;
301 mips32_insn.fp0_format.ft = 0;
302 mips32_insn.fp0_format.fs =
303 insn.mm_fp3_format.fs;
304 mips32_insn.fp0_format.fd =
305 insn.mm_fp3_format.rt;
306 mips32_insn.fp0_format.func = func;
314 if ((insn.mm_fp1_format.op & 0x7f) ==
317 else if ((insn.mm_fp1_format.op & 0x7f) ==
322 mips32_insn.fp0_format.opcode = cop1_op;
323 mips32_insn.fp0_format.fmt =
324 sdps_format[insn.mm_fp3_format.fmt];
325 mips32_insn.fp0_format.ft = 0;
326 mips32_insn.fp0_format.fs =
327 insn.mm_fp3_format.fs;
328 mips32_insn.fp0_format.fd =
329 insn.mm_fp3_format.rt;
330 mips32_insn.fp0_format.func = func;
342 if (insn.mm_fp1_format.op == mm_ffloorl_op)
344 else if (insn.mm_fp1_format.op == mm_ffloorw_op)
346 else if (insn.mm_fp1_format.op == mm_fceill_op)
348 else if (insn.mm_fp1_format.op == mm_fceilw_op)
350 else if (insn.mm_fp1_format.op == mm_ftruncl_op)
352 else if (insn.mm_fp1_format.op == mm_ftruncw_op)
354 else if (insn.mm_fp1_format.op == mm_froundl_op)
356 else if (insn.mm_fp1_format.op == mm_froundw_op)
358 else if (insn.mm_fp1_format.op == mm_fcvtl_op)
362 mips32_insn.fp0_format.opcode = cop1_op;
363 mips32_insn.fp0_format.fmt =
364 sd_format[insn.mm_fp1_format.fmt];
365 mips32_insn.fp0_format.ft = 0;
366 mips32_insn.fp0_format.fs =
367 insn.mm_fp1_format.fs;
368 mips32_insn.fp0_format.fd =
369 insn.mm_fp1_format.rt;
370 mips32_insn.fp0_format.func = func;
375 if (insn.mm_fp1_format.op == mm_frsqrt_op)
377 else if (insn.mm_fp1_format.op == mm_fsqrt_op)
381 mips32_insn.fp0_format.opcode = cop1_op;
382 mips32_insn.fp0_format.fmt =
383 sdps_format[insn.mm_fp1_format.fmt];
384 mips32_insn.fp0_format.ft = 0;
385 mips32_insn.fp0_format.fs =
386 insn.mm_fp1_format.fs;
387 mips32_insn.fp0_format.fd =
388 insn.mm_fp1_format.rt;
389 mips32_insn.fp0_format.func = func;
397 if (insn.mm_fp1_format.op == mm_mfc1_op)
399 else if (insn.mm_fp1_format.op == mm_mtc1_op)
401 else if (insn.mm_fp1_format.op == mm_cfc1_op)
403 else if (insn.mm_fp1_format.op == mm_ctc1_op)
405 else if (insn.mm_fp1_format.op == mm_mfhc1_op)
409 mips32_insn.fp1_format.opcode = cop1_op;
410 mips32_insn.fp1_format.op = op;
411 mips32_insn.fp1_format.rt =
412 insn.mm_fp1_format.rt;
413 mips32_insn.fp1_format.fs =
414 insn.mm_fp1_format.fs;
415 mips32_insn.fp1_format.fd = 0;
416 mips32_insn.fp1_format.func = 0;
422 case mm_32f_74_op: /* c.cond.fmt */
423 mips32_insn.fp0_format.opcode = cop1_op;
424 mips32_insn.fp0_format.fmt =
425 sdps_format[insn.mm_fp4_format.fmt];
426 mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
427 mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
428 mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
429 mips32_insn.fp0_format.func =
430 insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
440 *insn_ptr = mips32_insn;
445 * Redundant with logic already in kernel/branch.c,
446 * embedded in compute_return_epc. At some point,
447 * a single subroutine should be used across both
450 static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
451 unsigned long *contpc)
453 union mips_instruction insn = (union mips_instruction)dec_insn.insn;
455 unsigned int bit = 0;
457 switch (insn.i_format.opcode) {
459 switch (insn.r_format.func) {
461 regs->regs[insn.r_format.rd] =
462 regs->cp0_epc + dec_insn.pc_inc +
463 dec_insn.next_pc_inc;
466 *contpc = regs->regs[insn.r_format.rs];
471 switch (insn.i_format.rt) {
474 regs->regs[31] = regs->cp0_epc +
476 dec_insn.next_pc_inc;
480 if ((long)regs->regs[insn.i_format.rs] < 0)
481 *contpc = regs->cp0_epc +
483 (insn.i_format.simmediate << 2);
485 *contpc = regs->cp0_epc +
487 dec_insn.next_pc_inc;
491 regs->regs[31] = regs->cp0_epc +
493 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. */
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;
537 if (regs->regs[insn.i_format.rs] !=
538 regs->regs[insn.i_format.rt])
539 *contpc = regs->cp0_epc +
541 (insn.i_format.simmediate << 2);
543 *contpc = regs->cp0_epc +
545 dec_insn.next_pc_inc;
549 if ((long)regs->regs[insn.i_format.rs] <= 0)
550 *contpc = regs->cp0_epc +
552 (insn.i_format.simmediate << 2);
554 *contpc = regs->cp0_epc +
556 dec_insn.next_pc_inc;
560 if ((long)regs->regs[insn.i_format.rs] > 0)
561 *contpc = regs->cp0_epc +
563 (insn.i_format.simmediate << 2);
565 *contpc = regs->cp0_epc +
567 dec_insn.next_pc_inc;
569 #ifdef CONFIG_CPU_CAVIUM_OCTEON
570 case lwc2_op: /* This is bbit0 on Octeon */
571 if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
572 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
574 *contpc = regs->cp0_epc + 8;
576 case ldc2_op: /* This is bbit032 on Octeon */
577 if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
578 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
580 *contpc = regs->cp0_epc + 8;
582 case swc2_op: /* This is bbit1 on Octeon */
583 if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
584 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
586 *contpc = regs->cp0_epc + 8;
588 case sdc2_op: /* This is bbit132 on Octeon */
589 if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
590 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
592 *contpc = regs->cp0_epc + 8;
599 if (insn.i_format.rs == bc_op) {
602 asm volatile("cfc1\t%0,$31" : "=r" (fcr31));
604 fcr31 = current->thread.fpu.fcr31;
607 bit = (insn.i_format.rt >> 2);
610 switch (insn.i_format.rt & 3) {
613 if (~fcr31 & (1 << bit))
614 *contpc = regs->cp0_epc +
616 (insn.i_format.simmediate << 2);
618 *contpc = regs->cp0_epc +
620 dec_insn.next_pc_inc;
624 if (fcr31 & (1 << bit))
625 *contpc = regs->cp0_epc +
627 (insn.i_format.simmediate << 2);
629 *contpc = regs->cp0_epc +
631 dec_insn.next_pc_inc;
641 * In the Linux kernel, we support selection of FPR format on the
642 * basis of the Status.FR bit. If an FPU is not present, the FR bit
643 * is hardwired to zero, which would imply a 32-bit FPU even for
644 * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
645 * FPU emu is slow and bulky and optimizing this function offers fairly
646 * sizeable benefits so we try to be clever and make this function return
647 * a constant whenever possible, that is on 64-bit kernels without O32
648 * compatibility enabled and on 32-bit without 64-bit FPU support.
650 static inline int cop1_64bit(struct pt_regs *xcp)
652 if (config_enabled(CONFIG_64BIT) && !config_enabled(CONFIG_MIPS32_O32))
654 else if (config_enabled(CONFIG_32BIT) &&
655 !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
658 return !test_thread_flag(TIF_32BIT_FPREGS);
661 #define SIFROMREG(si, x) \
663 if (cop1_64bit(xcp)) \
664 (si) = get_fpr32(&ctx->fpr[x], 0); \
666 (si) = get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
669 #define SITOREG(si, x) \
671 if (cop1_64bit(xcp)) { \
673 set_fpr32(&ctx->fpr[x], 0, si); \
674 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
675 set_fpr32(&ctx->fpr[x], i, 0); \
677 set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
681 #define SIFROMHREG(si, x) ((si) = get_fpr32(&ctx->fpr[x], 1))
683 #define SITOHREG(si, x) \
686 set_fpr32(&ctx->fpr[x], 1, si); \
687 for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
688 set_fpr32(&ctx->fpr[x], i, 0); \
691 #define DIFROMREG(di, x) \
692 ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
694 #define DITOREG(di, x) \
697 fpr = (x) & ~(cop1_64bit(xcp) == 0); \
698 set_fpr64(&ctx->fpr[fpr], 0, di); \
699 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
700 set_fpr64(&ctx->fpr[fpr], i, 0); \
703 #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
704 #define SPTOREG(sp, x) SITOREG((sp).bits, x)
705 #define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
706 #define DPTOREG(dp, x) DITOREG((dp).bits, x)
709 * Emulate the single floating point instruction pointed at by EPC.
710 * Two instructions if the instruction is in a branch delay slot.
713 static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
714 struct mm_decoded_insn dec_insn, void *__user *fault_addr)
716 unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
717 unsigned int cond, cbit;
727 /* XXX NEC Vr54xx bug workaround */
728 if (delay_slot(xcp)) {
729 if (dec_insn.micro_mips_mode) {
730 if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
731 clear_delay_slot(xcp);
733 if (!isBranchInstr(xcp, dec_insn, &contpc))
734 clear_delay_slot(xcp);
738 if (delay_slot(xcp)) {
740 * The instruction to be emulated is in a branch delay slot
741 * which means that we have to emulate the branch instruction
742 * BEFORE we do the cop1 instruction.
744 * This branch could be a COP1 branch, but in that case we
745 * would have had a trap for that instruction, and would not
746 * come through this route.
748 * Linux MIPS branch emulator operates on context, updating the
751 ir = dec_insn.next_insn; /* process delay slot instr */
752 pc_inc = dec_insn.next_pc_inc;
754 ir = dec_insn.insn; /* process current instr */
755 pc_inc = dec_insn.pc_inc;
759 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
760 * instructions, we want to convert microMIPS FPU instructions
761 * into MIPS32 instructions so that we could reuse all of the
762 * FPU emulation code.
764 * NOTE: We cannot do this for branch instructions since they
765 * are not a subset. Example: Cannot emulate a 16-bit
766 * aligned target address with a MIPS32 instruction.
768 if (dec_insn.micro_mips_mode) {
770 * If next instruction is a 16-bit instruction, then it
771 * it cannot be a FPU instruction. This could happen
772 * since we can be called for non-FPU instructions.
775 (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
781 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
782 MIPS_FPU_EMU_INC_STATS(emulated);
783 switch (MIPSInst_OPCODE(ir)) {
785 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
787 MIPS_FPU_EMU_INC_STATS(loads);
789 if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
790 MIPS_FPU_EMU_INC_STATS(errors);
794 if (__get_user(dval, dva)) {
795 MIPS_FPU_EMU_INC_STATS(errors);
799 DITOREG(dval, MIPSInst_RT(ir));
803 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
805 MIPS_FPU_EMU_INC_STATS(stores);
806 DIFROMREG(dval, MIPSInst_RT(ir));
807 if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
808 MIPS_FPU_EMU_INC_STATS(errors);
812 if (__put_user(dval, dva)) {
813 MIPS_FPU_EMU_INC_STATS(errors);
820 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
822 MIPS_FPU_EMU_INC_STATS(loads);
823 if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
824 MIPS_FPU_EMU_INC_STATS(errors);
828 if (__get_user(wval, wva)) {
829 MIPS_FPU_EMU_INC_STATS(errors);
833 SITOREG(wval, MIPSInst_RT(ir));
837 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
839 MIPS_FPU_EMU_INC_STATS(stores);
840 SIFROMREG(wval, MIPSInst_RT(ir));
841 if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
842 MIPS_FPU_EMU_INC_STATS(errors);
846 if (__put_user(wval, wva)) {
847 MIPS_FPU_EMU_INC_STATS(errors);
854 switch (MIPSInst_RS(ir)) {
856 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
859 /* copregister fs -> gpr[rt] */
860 if (MIPSInst_RT(ir) != 0) {
861 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
867 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
870 /* copregister fs <- rt */
871 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
875 if (!cpu_has_mips_r2)
878 /* copregister rd -> gpr[rt] */
879 if (MIPSInst_RT(ir) != 0) {
880 SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
886 if (!cpu_has_mips_r2)
889 /* copregister rd <- gpr[rt] */
890 SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
894 /* copregister rd -> gpr[rt] */
895 if (MIPSInst_RT(ir) != 0) {
896 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
902 /* copregister rd <- rt */
903 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
907 /* cop control register rd -> gpr[rt] */
908 if (MIPSInst_RD(ir) == FPCREG_CSR) {
910 value = (value & ~FPU_CSR_RM) |
911 mips_rm[modeindex(value)];
912 pr_debug("%p gpr[%d]<-csr=%08x\n",
913 (void *) (xcp->cp0_epc),
914 MIPSInst_RT(ir), value);
916 else if (MIPSInst_RD(ir) == FPCREG_RID)
921 xcp->regs[MIPSInst_RT(ir)] = value;
925 /* copregister rd <- rt */
926 if (MIPSInst_RT(ir) == 0)
929 value = xcp->regs[MIPSInst_RT(ir)];
931 /* we only have one writable control reg
933 if (MIPSInst_RD(ir) == FPCREG_CSR) {
934 pr_debug("%p gpr[%d]->csr=%08x\n",
935 (void *) (xcp->cp0_epc),
936 MIPSInst_RT(ir), value);
939 * Don't write reserved bits,
940 * and convert to ieee library modes
942 ctx->fcr31 = (value &
943 ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
944 ieee_rm[modeindex(value)];
946 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
955 if (cpu_has_mips_4_5_r)
956 cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
959 cond = ctx->fcr31 & cbit;
962 switch (MIPSInst_RT(ir) & 3) {
973 /* thats an illegal instruction */
980 * Branch taken: emulate dslot instruction
982 xcp->cp0_epc += dec_insn.pc_inc;
984 contpc = MIPSInst_SIMM(ir);
985 ir = dec_insn.next_insn;
986 if (dec_insn.micro_mips_mode) {
987 contpc = (xcp->cp0_epc + (contpc << 1));
989 /* If 16-bit instruction, not FPU. */
990 if ((dec_insn.next_pc_inc == 2) ||
991 (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
994 * Since this instruction will
995 * be put on the stack with
996 * 32-bit words, get around
997 * this problem by putting a
998 * NOP16 as the second one.
1000 if (dec_insn.next_pc_inc == 2)
1001 ir = (ir & (~0xffff)) | MM_NOP16;
1004 * Single step the non-CP1
1005 * instruction in the dslot.
1007 return mips_dsemul(xcp, ir, contpc);
1010 contpc = (xcp->cp0_epc + (contpc << 2));
1012 switch (MIPSInst_OPCODE(ir)) {
1021 if (cpu_has_mips_2_3_4_5 ||
1032 if (cpu_has_mips_4_5 || cpu_has_mips64)
1033 /* its one of ours */
1039 if (!cpu_has_mips_4_5_r)
1042 if (MIPSInst_FUNC(ir) == movc_op)
1048 * Single step the non-cp1
1049 * instruction in the dslot
1051 return mips_dsemul(xcp, ir, contpc);
1052 } else if (likely) { /* branch not taken */
1054 * branch likely nullifies
1055 * dslot if not taken
1057 xcp->cp0_epc += dec_insn.pc_inc;
1058 contpc += dec_insn.pc_inc;
1060 * else continue & execute
1061 * dslot as normal insn
1067 if (!(MIPSInst_RS(ir) & 0x10))
1070 /* a real fpu computation instruction */
1071 if ((sig = fpu_emu(xcp, ctx, ir)))
1077 if (!cpu_has_mips_4_5 && !cpu_has_mips64)
1080 sig = fpux_emu(xcp, ctx, ir, fault_addr);
1086 if (!cpu_has_mips_4_5_r)
1089 if (MIPSInst_FUNC(ir) != movc_op)
1091 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
1092 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
1093 xcp->regs[MIPSInst_RD(ir)] =
1094 xcp->regs[MIPSInst_RS(ir)];
1102 xcp->cp0_epc = contpc;
1103 clear_delay_slot(xcp);
1109 * Conversion table from MIPS compare ops 48-63
1110 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
1112 static const unsigned char cmptab[8] = {
1113 0, /* cmp_0 (sig) cmp_sf */
1114 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
1115 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
1116 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
1117 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
1118 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
1119 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
1120 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
1125 * Additional MIPS4 instructions
1128 #define DEF3OP(name, p, f1, f2, f3) \
1129 static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \
1130 union ieee754##p s, union ieee754##p t) \
1132 struct _ieee754_csr ieee754_csr_save; \
1134 ieee754_csr_save = ieee754_csr; \
1136 ieee754_csr_save.cx |= ieee754_csr.cx; \
1137 ieee754_csr_save.sx |= ieee754_csr.sx; \
1139 ieee754_csr.cx |= ieee754_csr_save.cx; \
1140 ieee754_csr.sx |= ieee754_csr_save.sx; \
1144 static union ieee754dp fpemu_dp_recip(union ieee754dp d)
1146 return ieee754dp_div(ieee754dp_one(0), d);
1149 static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
1151 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
1154 static union ieee754sp fpemu_sp_recip(union ieee754sp s)
1156 return ieee754sp_div(ieee754sp_one(0), s);
1159 static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
1161 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
1164 DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
1165 DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
1166 DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
1167 DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
1168 DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
1169 DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
1170 DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
1171 DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
1173 static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1174 mips_instruction ir, void *__user *fault_addr)
1176 unsigned rcsr = 0; /* resulting csr */
1178 MIPS_FPU_EMU_INC_STATS(cp1xops);
1180 switch (MIPSInst_FMA_FFMT(ir)) {
1181 case s_fmt:{ /* 0 */
1183 union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
1184 union ieee754sp fd, fr, fs, ft;
1188 switch (MIPSInst_FUNC(ir)) {
1190 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1191 xcp->regs[MIPSInst_FT(ir)]);
1193 MIPS_FPU_EMU_INC_STATS(loads);
1194 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
1195 MIPS_FPU_EMU_INC_STATS(errors);
1199 if (__get_user(val, va)) {
1200 MIPS_FPU_EMU_INC_STATS(errors);
1204 SITOREG(val, MIPSInst_FD(ir));
1208 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1209 xcp->regs[MIPSInst_FT(ir)]);
1211 MIPS_FPU_EMU_INC_STATS(stores);
1213 SIFROMREG(val, MIPSInst_FS(ir));
1214 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1215 MIPS_FPU_EMU_INC_STATS(errors);
1219 if (put_user(val, va)) {
1220 MIPS_FPU_EMU_INC_STATS(errors);
1227 handler = fpemu_sp_madd;
1230 handler = fpemu_sp_msub;
1233 handler = fpemu_sp_nmadd;
1236 handler = fpemu_sp_nmsub;
1240 SPFROMREG(fr, MIPSInst_FR(ir));
1241 SPFROMREG(fs, MIPSInst_FS(ir));
1242 SPFROMREG(ft, MIPSInst_FT(ir));
1243 fd = (*handler) (fr, fs, ft);
1244 SPTOREG(fd, MIPSInst_FD(ir));
1247 if (ieee754_cxtest(IEEE754_INEXACT))
1248 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1249 if (ieee754_cxtest(IEEE754_UNDERFLOW))
1250 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1251 if (ieee754_cxtest(IEEE754_OVERFLOW))
1252 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1253 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
1254 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1256 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1257 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1258 /*printk ("SIGFPE: FPU csr = %08x\n",
1271 case d_fmt:{ /* 1 */
1272 union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
1273 union ieee754dp fd, fr, fs, ft;
1277 switch (MIPSInst_FUNC(ir)) {
1279 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1280 xcp->regs[MIPSInst_FT(ir)]);
1282 MIPS_FPU_EMU_INC_STATS(loads);
1283 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
1284 MIPS_FPU_EMU_INC_STATS(errors);
1288 if (__get_user(val, va)) {
1289 MIPS_FPU_EMU_INC_STATS(errors);
1293 DITOREG(val, MIPSInst_FD(ir));
1297 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1298 xcp->regs[MIPSInst_FT(ir)]);
1300 MIPS_FPU_EMU_INC_STATS(stores);
1301 DIFROMREG(val, MIPSInst_FS(ir));
1302 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
1303 MIPS_FPU_EMU_INC_STATS(errors);
1307 if (__put_user(val, va)) {
1308 MIPS_FPU_EMU_INC_STATS(errors);
1315 handler = fpemu_dp_madd;
1318 handler = fpemu_dp_msub;
1321 handler = fpemu_dp_nmadd;
1324 handler = fpemu_dp_nmsub;
1328 DPFROMREG(fr, MIPSInst_FR(ir));
1329 DPFROMREG(fs, MIPSInst_FS(ir));
1330 DPFROMREG(ft, MIPSInst_FT(ir));
1331 fd = (*handler) (fr, fs, ft);
1332 DPTOREG(fd, MIPSInst_FD(ir));
1342 if (MIPSInst_FUNC(ir) != pfetch_op)
1345 /* ignore prefx operation */
1358 * Emulate a single COP1 arithmetic instruction.
1360 static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1361 mips_instruction ir)
1363 int rfmt; /* resulting format */
1364 unsigned rcsr = 0; /* resulting csr */
1373 } rv; /* resulting value */
1376 MIPS_FPU_EMU_INC_STATS(cp1ops);
1377 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
1378 case s_fmt: { /* 0 */
1380 union ieee754sp(*b) (union ieee754sp, union ieee754sp);
1381 union ieee754sp(*u) (union ieee754sp);
1383 union ieee754sp fs, ft;
1385 switch (MIPSInst_FUNC(ir)) {
1388 handler.b = ieee754sp_add;
1391 handler.b = ieee754sp_sub;
1394 handler.b = ieee754sp_mul;
1397 handler.b = ieee754sp_div;
1402 if (!cpu_has_mips_4_5_r)
1405 handler.u = ieee754sp_sqrt;
1409 * Note that on some MIPS IV implementations such as the
1410 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1411 * achieve full IEEE-754 accuracy - however this emulator does.
1414 if (!cpu_has_mips_4_5_r2)
1417 handler.u = fpemu_sp_rsqrt;
1421 if (!cpu_has_mips_4_5_r2)
1424 handler.u = fpemu_sp_recip;
1428 if (!cpu_has_mips_4_5_r)
1431 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1432 if (((ctx->fcr31 & cond) != 0) !=
1433 ((MIPSInst_FT(ir) & 1) != 0))
1435 SPFROMREG(rv.s, MIPSInst_FS(ir));
1439 if (!cpu_has_mips_4_5_r)
1442 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1444 SPFROMREG(rv.s, MIPSInst_FS(ir));
1448 if (!cpu_has_mips_4_5_r)
1451 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1453 SPFROMREG(rv.s, MIPSInst_FS(ir));
1457 handler.u = ieee754sp_abs;
1461 handler.u = ieee754sp_neg;
1466 SPFROMREG(rv.s, MIPSInst_FS(ir));
1469 /* binary op on handler */
1471 SPFROMREG(fs, MIPSInst_FS(ir));
1472 SPFROMREG(ft, MIPSInst_FT(ir));
1474 rv.s = (*handler.b) (fs, ft);
1477 SPFROMREG(fs, MIPSInst_FS(ir));
1478 rv.s = (*handler.u) (fs);
1481 if (ieee754_cxtest(IEEE754_INEXACT))
1482 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1483 if (ieee754_cxtest(IEEE754_UNDERFLOW))
1484 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1485 if (ieee754_cxtest(IEEE754_OVERFLOW))
1486 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1487 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE))
1488 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
1489 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
1490 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1493 /* unary conv ops */
1495 return SIGILL; /* not defined */
1498 SPFROMREG(fs, MIPSInst_FS(ir));
1499 rv.d = ieee754dp_fsp(fs);
1504 SPFROMREG(fs, MIPSInst_FS(ir));
1505 rv.w = ieee754sp_tint(fs);
1513 if (!cpu_has_mips_2_3_4_5 && !cpu_has_mips64)
1516 oldrm = ieee754_csr.rm;
1517 SPFROMREG(fs, MIPSInst_FS(ir));
1518 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
1519 rv.w = ieee754sp_tint(fs);
1520 ieee754_csr.rm = oldrm;
1525 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1528 SPFROMREG(fs, MIPSInst_FS(ir));
1529 rv.l = ieee754sp_tlong(fs);
1537 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1540 oldrm = ieee754_csr.rm;
1541 SPFROMREG(fs, MIPSInst_FS(ir));
1542 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
1543 rv.l = ieee754sp_tlong(fs);
1544 ieee754_csr.rm = oldrm;
1549 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1550 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1551 union ieee754sp fs, ft;
1553 SPFROMREG(fs, MIPSInst_FS(ir));
1554 SPFROMREG(ft, MIPSInst_FT(ir));
1555 rv.w = ieee754sp_cmp(fs, ft,
1556 cmptab[cmpop & 0x7], cmpop & 0x8);
1558 if ((cmpop & 0x8) && ieee754_cxtest
1559 (IEEE754_INVALID_OPERATION))
1560 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1572 union ieee754dp fs, ft;
1574 union ieee754dp(*b) (union ieee754dp, union ieee754dp);
1575 union ieee754dp(*u) (union ieee754dp);
1578 switch (MIPSInst_FUNC(ir)) {
1581 handler.b = ieee754dp_add;
1584 handler.b = ieee754dp_sub;
1587 handler.b = ieee754dp_mul;
1590 handler.b = ieee754dp_div;
1595 if (!cpu_has_mips_2_3_4_5_r)
1598 handler.u = ieee754dp_sqrt;
1601 * Note that on some MIPS IV implementations such as the
1602 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1603 * achieve full IEEE-754 accuracy - however this emulator does.
1606 if (!cpu_has_mips_4_5_r2)
1609 handler.u = fpemu_dp_rsqrt;
1612 if (!cpu_has_mips_4_5_r2)
1615 handler.u = fpemu_dp_recip;
1618 if (!cpu_has_mips_4_5_r)
1621 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1622 if (((ctx->fcr31 & cond) != 0) !=
1623 ((MIPSInst_FT(ir) & 1) != 0))
1625 DPFROMREG(rv.d, MIPSInst_FS(ir));
1628 if (!cpu_has_mips_4_5_r)
1631 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1633 DPFROMREG(rv.d, MIPSInst_FS(ir));
1636 if (!cpu_has_mips_4_5_r)
1639 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1641 DPFROMREG(rv.d, MIPSInst_FS(ir));
1644 handler.u = ieee754dp_abs;
1648 handler.u = ieee754dp_neg;
1653 DPFROMREG(rv.d, MIPSInst_FS(ir));
1656 /* binary op on handler */
1658 DPFROMREG(fs, MIPSInst_FS(ir));
1659 DPFROMREG(ft, MIPSInst_FT(ir));
1661 rv.d = (*handler.b) (fs, ft);
1664 DPFROMREG(fs, MIPSInst_FS(ir));
1665 rv.d = (*handler.u) (fs);
1672 DPFROMREG(fs, MIPSInst_FS(ir));
1673 rv.s = ieee754sp_fdp(fs);
1678 return SIGILL; /* not defined */
1681 DPFROMREG(fs, MIPSInst_FS(ir));
1682 rv.w = ieee754dp_tint(fs); /* wrong */
1690 if (!cpu_has_mips_2_3_4_5_r)
1693 oldrm = ieee754_csr.rm;
1694 DPFROMREG(fs, MIPSInst_FS(ir));
1695 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
1696 rv.w = ieee754dp_tint(fs);
1697 ieee754_csr.rm = oldrm;
1702 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1705 DPFROMREG(fs, MIPSInst_FS(ir));
1706 rv.l = ieee754dp_tlong(fs);
1714 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1717 oldrm = ieee754_csr.rm;
1718 DPFROMREG(fs, MIPSInst_FS(ir));
1719 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
1720 rv.l = ieee754dp_tlong(fs);
1721 ieee754_csr.rm = oldrm;
1726 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1727 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1728 union ieee754dp fs, ft;
1730 DPFROMREG(fs, MIPSInst_FS(ir));
1731 DPFROMREG(ft, MIPSInst_FT(ir));
1732 rv.w = ieee754dp_cmp(fs, ft,
1733 cmptab[cmpop & 0x7], cmpop & 0x8);
1738 (IEEE754_INVALID_OPERATION))
1739 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1752 switch (MIPSInst_FUNC(ir)) {
1754 /* convert word to single precision real */
1755 SPFROMREG(fs, MIPSInst_FS(ir));
1756 rv.s = ieee754sp_fint(fs.bits);
1760 /* convert word to double precision real */
1761 SPFROMREG(fs, MIPSInst_FS(ir));
1762 rv.d = ieee754dp_fint(fs.bits);
1773 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1776 DIFROMREG(bits, MIPSInst_FS(ir));
1778 switch (MIPSInst_FUNC(ir)) {
1780 /* convert long to single precision real */
1781 rv.s = ieee754sp_flong(bits);
1785 /* convert long to double precision real */
1786 rv.d = ieee754dp_flong(bits);
1799 * Update the fpu CSR register for this operation.
1800 * If an exception is required, generate a tidy SIGFPE exception,
1801 * without updating the result register.
1802 * Note: cause exception bits do not accumulate, they are rewritten
1803 * for each op; only the flag/sticky bits accumulate.
1805 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1806 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1807 /*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
1812 * Now we can safely write the result back to the register file.
1817 if (cpu_has_mips_4_5_r)
1818 cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
1820 cbit = FPU_CSR_COND;
1824 ctx->fcr31 &= ~cbit;
1828 DPTOREG(rv.d, MIPSInst_FD(ir));
1831 SPTOREG(rv.s, MIPSInst_FD(ir));
1834 SITOREG(rv.w, MIPSInst_FD(ir));
1837 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1840 DITOREG(rv.l, MIPSInst_FD(ir));
1849 int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1850 int has_fpu, void *__user *fault_addr)
1852 unsigned long oldepc, prevepc;
1853 struct mm_decoded_insn dec_insn;
1858 oldepc = xcp->cp0_epc;
1860 prevepc = xcp->cp0_epc;
1862 if (get_isa16_mode(prevepc) && cpu_has_mmips) {
1864 * Get next 2 microMIPS instructions and convert them
1865 * into 32-bit instructions.
1867 if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
1868 (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
1869 (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
1870 (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
1871 MIPS_FPU_EMU_INC_STATS(errors);
1876 /* Get first instruction. */
1877 if (mm_insn_16bit(*instr_ptr)) {
1878 /* Duplicate the half-word. */
1879 dec_insn.insn = (*instr_ptr << 16) |
1881 /* 16-bit instruction. */
1882 dec_insn.pc_inc = 2;
1885 dec_insn.insn = (*instr_ptr << 16) |
1887 /* 32-bit instruction. */
1888 dec_insn.pc_inc = 4;
1891 /* Get second instruction. */
1892 if (mm_insn_16bit(*instr_ptr)) {
1893 /* Duplicate the half-word. */
1894 dec_insn.next_insn = (*instr_ptr << 16) |
1896 /* 16-bit instruction. */
1897 dec_insn.next_pc_inc = 2;
1899 dec_insn.next_insn = (*instr_ptr << 16) |
1901 /* 32-bit instruction. */
1902 dec_insn.next_pc_inc = 4;
1904 dec_insn.micro_mips_mode = 1;
1906 if ((get_user(dec_insn.insn,
1907 (mips_instruction __user *) xcp->cp0_epc)) ||
1908 (get_user(dec_insn.next_insn,
1909 (mips_instruction __user *)(xcp->cp0_epc+4)))) {
1910 MIPS_FPU_EMU_INC_STATS(errors);
1913 dec_insn.pc_inc = 4;
1914 dec_insn.next_pc_inc = 4;
1915 dec_insn.micro_mips_mode = 0;
1918 if ((dec_insn.insn == 0) ||
1919 ((dec_insn.pc_inc == 2) &&
1920 ((dec_insn.insn & 0xffff) == MM_NOP16)))
1921 xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
1924 * The 'ieee754_csr' is an alias of
1925 * ctx->fcr31. No need to copy ctx->fcr31 to
1926 * ieee754_csr. But ieee754_csr.rm is ieee
1927 * library modes. (not mips rounding mode)
1929 /* convert to ieee library modes */
1930 ieee754_csr.rm = ieee_rm[ieee754_csr.rm];
1931 sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
1932 /* revert to mips rounding mode */
1933 ieee754_csr.rm = mips_rm[ieee754_csr.rm];
1942 } while (xcp->cp0_epc > prevepc);
1944 /* SIGILL indicates a non-fpu instruction */
1945 if (sig == SIGILL && xcp->cp0_epc != oldepc)
1946 /* but if EPC has advanced, then ignore it */