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 regs->regs[31] = regs->cp0_epc +
464 dec_insn.next_pc_inc;
468 if ((long)regs->regs[insn.i_format.rs] < 0)
469 *contpc = regs->cp0_epc +
471 (insn.i_format.simmediate << 2);
473 *contpc = regs->cp0_epc +
475 dec_insn.next_pc_inc;
479 regs->regs[31] = regs->cp0_epc +
481 dec_insn.next_pc_inc;
485 if ((long)regs->regs[insn.i_format.rs] >= 0)
486 *contpc = regs->cp0_epc +
488 (insn.i_format.simmediate << 2);
490 *contpc = regs->cp0_epc +
492 dec_insn.next_pc_inc;
499 regs->regs[31] = regs->cp0_epc +
501 dec_insn.next_pc_inc;
504 *contpc = regs->cp0_epc + dec_insn.pc_inc;
507 *contpc |= (insn.j_format.target << 2);
508 /* Set microMIPS mode bit: XOR for jalx. */
513 if (regs->regs[insn.i_format.rs] ==
514 regs->regs[insn.i_format.rt])
515 *contpc = regs->cp0_epc +
517 (insn.i_format.simmediate << 2);
519 *contpc = regs->cp0_epc +
521 dec_insn.next_pc_inc;
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 ((long)regs->regs[insn.i_format.rs] <= 0)
538 *contpc = regs->cp0_epc +
540 (insn.i_format.simmediate << 2);
542 *contpc = regs->cp0_epc +
544 dec_insn.next_pc_inc;
548 if ((long)regs->regs[insn.i_format.rs] > 0)
549 *contpc = regs->cp0_epc +
551 (insn.i_format.simmediate << 2);
553 *contpc = regs->cp0_epc +
555 dec_insn.next_pc_inc;
557 #ifdef CONFIG_CPU_CAVIUM_OCTEON
558 case lwc2_op: /* This is bbit0 on Octeon */
559 if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
560 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
562 *contpc = regs->cp0_epc + 8;
564 case ldc2_op: /* This is bbit032 on Octeon */
565 if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
566 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
568 *contpc = regs->cp0_epc + 8;
570 case swc2_op: /* This is bbit1 on Octeon */
571 if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
572 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
574 *contpc = regs->cp0_epc + 8;
576 case sdc2_op: /* This is bbit132 on Octeon */
577 if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
578 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
580 *contpc = regs->cp0_epc + 8;
587 if (insn.i_format.rs == bc_op) {
590 fcr31 = read_32bit_cp1_register(CP1_STATUS);
592 fcr31 = current->thread.fpu.fcr31;
595 bit = (insn.i_format.rt >> 2);
598 switch (insn.i_format.rt & 3) {
601 if (~fcr31 & (1 << bit))
602 *contpc = regs->cp0_epc +
604 (insn.i_format.simmediate << 2);
606 *contpc = regs->cp0_epc +
608 dec_insn.next_pc_inc;
612 if (fcr31 & (1 << bit))
613 *contpc = regs->cp0_epc +
615 (insn.i_format.simmediate << 2);
617 *contpc = regs->cp0_epc +
619 dec_insn.next_pc_inc;
629 * In the Linux kernel, we support selection of FPR format on the
630 * basis of the Status.FR bit. If an FPU is not present, the FR bit
631 * is hardwired to zero, which would imply a 32-bit FPU even for
632 * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
633 * FPU emu is slow and bulky and optimizing this function offers fairly
634 * sizeable benefits so we try to be clever and make this function return
635 * a constant whenever possible, that is on 64-bit kernels without O32
636 * compatibility enabled and on 32-bit without 64-bit FPU support.
638 static inline int cop1_64bit(struct pt_regs *xcp)
640 if (config_enabled(CONFIG_64BIT) && !config_enabled(CONFIG_MIPS32_O32))
642 else if (config_enabled(CONFIG_32BIT) &&
643 !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
646 return !test_thread_flag(TIF_32BIT_FPREGS);
649 static inline bool hybrid_fprs(void)
651 return test_thread_flag(TIF_HYBRID_FPREGS);
654 #define SIFROMREG(si, x) \
656 if (cop1_64bit(xcp) && !hybrid_fprs()) \
657 (si) = (int)get_fpr32(&ctx->fpr[x], 0); \
659 (si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
662 #define SITOREG(si, x) \
664 if (cop1_64bit(xcp) && !hybrid_fprs()) { \
666 set_fpr32(&ctx->fpr[x], 0, si); \
667 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
668 set_fpr32(&ctx->fpr[x], i, 0); \
670 set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
674 #define SIFROMHREG(si, x) ((si) = (int)get_fpr32(&ctx->fpr[x], 1))
676 #define SITOHREG(si, x) \
679 set_fpr32(&ctx->fpr[x], 1, si); \
680 for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
681 set_fpr32(&ctx->fpr[x], i, 0); \
684 #define DIFROMREG(di, x) \
685 ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
687 #define DITOREG(di, x) \
690 fpr = (x) & ~(cop1_64bit(xcp) == 0); \
691 set_fpr64(&ctx->fpr[fpr], 0, di); \
692 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
693 set_fpr64(&ctx->fpr[fpr], i, 0); \
696 #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
697 #define SPTOREG(sp, x) SITOREG((sp).bits, x)
698 #define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
699 #define DPTOREG(dp, x) DITOREG((dp).bits, x)
702 * Emulate the single floating point instruction pointed at by EPC.
703 * Two instructions if the instruction is in a branch delay slot.
706 static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
707 struct mm_decoded_insn dec_insn, void *__user *fault_addr)
709 unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
710 unsigned int cond, cbit;
721 * These are giving gcc a gentle hint about what to expect in
722 * dec_inst in order to do better optimization.
724 if (!cpu_has_mmips && dec_insn.micro_mips_mode)
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) | modeindex(value);
911 pr_debug("%p gpr[%d]<-csr=%08x\n",
912 (void *) (xcp->cp0_epc),
913 MIPSInst_RT(ir), value);
915 else if (MIPSInst_RD(ir) == FPCREG_RID)
920 xcp->regs[MIPSInst_RT(ir)] = value;
924 /* copregister rd <- rt */
925 if (MIPSInst_RT(ir) == 0)
928 value = xcp->regs[MIPSInst_RT(ir)];
930 /* we only have one writable control reg
932 if (MIPSInst_RD(ir) == FPCREG_CSR) {
933 pr_debug("%p gpr[%d]->csr=%08x\n",
934 (void *) (xcp->cp0_epc),
935 MIPSInst_RT(ir), value);
938 * Don't write reserved bits,
939 * and convert to ieee library modes
941 ctx->fcr31 = (value & ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
944 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
953 if (cpu_has_mips_4_5_r)
954 cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
957 cond = ctx->fcr31 & cbit;
960 switch (MIPSInst_RT(ir) & 3) {
971 /* thats an illegal instruction */
978 * Branch taken: emulate dslot instruction
980 xcp->cp0_epc += dec_insn.pc_inc;
982 contpc = MIPSInst_SIMM(ir);
983 ir = dec_insn.next_insn;
984 if (dec_insn.micro_mips_mode) {
985 contpc = (xcp->cp0_epc + (contpc << 1));
987 /* If 16-bit instruction, not FPU. */
988 if ((dec_insn.next_pc_inc == 2) ||
989 (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
992 * Since this instruction will
993 * be put on the stack with
994 * 32-bit words, get around
995 * this problem by putting a
996 * NOP16 as the second one.
998 if (dec_insn.next_pc_inc == 2)
999 ir = (ir & (~0xffff)) | MM_NOP16;
1002 * Single step the non-CP1
1003 * instruction in the dslot.
1005 return mips_dsemul(xcp, ir, contpc);
1008 contpc = (xcp->cp0_epc + (contpc << 2));
1010 switch (MIPSInst_OPCODE(ir)) {
1019 if (cpu_has_mips_2_3_4_5 ||
1030 if (cpu_has_mips_4_5 || cpu_has_mips64 || cpu_has_mips32r2)
1031 /* its one of ours */
1037 if (!cpu_has_mips_4_5_r)
1040 if (MIPSInst_FUNC(ir) == movc_op)
1046 * Single step the non-cp1
1047 * instruction in the dslot
1049 return mips_dsemul(xcp, ir, contpc);
1050 } else if (likely) { /* branch not taken */
1052 * branch likely nullifies
1053 * dslot if not taken
1055 xcp->cp0_epc += dec_insn.pc_inc;
1056 contpc += dec_insn.pc_inc;
1058 * else continue & execute
1059 * dslot as normal insn
1065 if (!(MIPSInst_RS(ir) & 0x10))
1068 /* a real fpu computation instruction */
1069 if ((sig = fpu_emu(xcp, ctx, ir)))
1075 if (!cpu_has_mips_4_5 && !cpu_has_mips64 && !cpu_has_mips32r2)
1078 sig = fpux_emu(xcp, ctx, ir, fault_addr);
1084 if (!cpu_has_mips_4_5_r)
1087 if (MIPSInst_FUNC(ir) != movc_op)
1089 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
1090 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
1091 xcp->regs[MIPSInst_RD(ir)] =
1092 xcp->regs[MIPSInst_RS(ir)];
1100 xcp->cp0_epc = contpc;
1101 clear_delay_slot(xcp);
1107 * Conversion table from MIPS compare ops 48-63
1108 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
1110 static const unsigned char cmptab[8] = {
1111 0, /* cmp_0 (sig) cmp_sf */
1112 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
1113 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
1114 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
1115 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
1116 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
1117 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
1118 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
1123 * Additional MIPS4 instructions
1126 #define DEF3OP(name, p, f1, f2, f3) \
1127 static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \
1128 union ieee754##p s, union ieee754##p t) \
1130 struct _ieee754_csr ieee754_csr_save; \
1132 ieee754_csr_save = ieee754_csr; \
1134 ieee754_csr_save.cx |= ieee754_csr.cx; \
1135 ieee754_csr_save.sx |= ieee754_csr.sx; \
1137 ieee754_csr.cx |= ieee754_csr_save.cx; \
1138 ieee754_csr.sx |= ieee754_csr_save.sx; \
1142 static union ieee754dp fpemu_dp_recip(union ieee754dp d)
1144 return ieee754dp_div(ieee754dp_one(0), d);
1147 static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
1149 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
1152 static union ieee754sp fpemu_sp_recip(union ieee754sp s)
1154 return ieee754sp_div(ieee754sp_one(0), s);
1157 static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
1159 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
1162 DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
1163 DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
1164 DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
1165 DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
1166 DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
1167 DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
1168 DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
1169 DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
1171 static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1172 mips_instruction ir, void *__user *fault_addr)
1174 unsigned rcsr = 0; /* resulting csr */
1176 MIPS_FPU_EMU_INC_STATS(cp1xops);
1178 switch (MIPSInst_FMA_FFMT(ir)) {
1179 case s_fmt:{ /* 0 */
1181 union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
1182 union ieee754sp fd, fr, fs, ft;
1186 switch (MIPSInst_FUNC(ir)) {
1188 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1189 xcp->regs[MIPSInst_FT(ir)]);
1191 MIPS_FPU_EMU_INC_STATS(loads);
1192 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
1193 MIPS_FPU_EMU_INC_STATS(errors);
1197 if (__get_user(val, va)) {
1198 MIPS_FPU_EMU_INC_STATS(errors);
1202 SITOREG(val, MIPSInst_FD(ir));
1206 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1207 xcp->regs[MIPSInst_FT(ir)]);
1209 MIPS_FPU_EMU_INC_STATS(stores);
1211 SIFROMREG(val, MIPSInst_FS(ir));
1212 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1213 MIPS_FPU_EMU_INC_STATS(errors);
1217 if (put_user(val, va)) {
1218 MIPS_FPU_EMU_INC_STATS(errors);
1225 handler = fpemu_sp_madd;
1228 handler = fpemu_sp_msub;
1231 handler = fpemu_sp_nmadd;
1234 handler = fpemu_sp_nmsub;
1238 SPFROMREG(fr, MIPSInst_FR(ir));
1239 SPFROMREG(fs, MIPSInst_FS(ir));
1240 SPFROMREG(ft, MIPSInst_FT(ir));
1241 fd = (*handler) (fr, fs, ft);
1242 SPTOREG(fd, MIPSInst_FD(ir));
1245 if (ieee754_cxtest(IEEE754_INEXACT)) {
1246 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
1247 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1249 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1250 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
1251 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1253 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1254 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
1255 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1257 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1258 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
1259 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1262 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1263 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1264 /*printk ("SIGFPE: FPU csr = %08x\n",
1277 case d_fmt:{ /* 1 */
1278 union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
1279 union ieee754dp fd, fr, fs, ft;
1283 switch (MIPSInst_FUNC(ir)) {
1285 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1286 xcp->regs[MIPSInst_FT(ir)]);
1288 MIPS_FPU_EMU_INC_STATS(loads);
1289 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
1290 MIPS_FPU_EMU_INC_STATS(errors);
1294 if (__get_user(val, va)) {
1295 MIPS_FPU_EMU_INC_STATS(errors);
1299 DITOREG(val, MIPSInst_FD(ir));
1303 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1304 xcp->regs[MIPSInst_FT(ir)]);
1306 MIPS_FPU_EMU_INC_STATS(stores);
1307 DIFROMREG(val, MIPSInst_FS(ir));
1308 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
1309 MIPS_FPU_EMU_INC_STATS(errors);
1313 if (__put_user(val, va)) {
1314 MIPS_FPU_EMU_INC_STATS(errors);
1321 handler = fpemu_dp_madd;
1324 handler = fpemu_dp_msub;
1327 handler = fpemu_dp_nmadd;
1330 handler = fpemu_dp_nmsub;
1334 DPFROMREG(fr, MIPSInst_FR(ir));
1335 DPFROMREG(fs, MIPSInst_FS(ir));
1336 DPFROMREG(ft, MIPSInst_FT(ir));
1337 fd = (*handler) (fr, fs, ft);
1338 DPTOREG(fd, MIPSInst_FD(ir));
1348 if (MIPSInst_FUNC(ir) != pfetch_op)
1351 /* ignore prefx operation */
1364 * Emulate a single COP1 arithmetic instruction.
1366 static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1367 mips_instruction ir)
1369 int rfmt; /* resulting format */
1370 unsigned rcsr = 0; /* resulting csr */
1379 } rv; /* resulting value */
1382 MIPS_FPU_EMU_INC_STATS(cp1ops);
1383 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
1384 case s_fmt: { /* 0 */
1386 union ieee754sp(*b) (union ieee754sp, union ieee754sp);
1387 union ieee754sp(*u) (union ieee754sp);
1389 union ieee754sp fs, ft;
1391 switch (MIPSInst_FUNC(ir)) {
1394 handler.b = ieee754sp_add;
1397 handler.b = ieee754sp_sub;
1400 handler.b = ieee754sp_mul;
1403 handler.b = ieee754sp_div;
1408 if (!cpu_has_mips_4_5_r)
1411 handler.u = ieee754sp_sqrt;
1415 * Note that on some MIPS IV implementations such as the
1416 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1417 * achieve full IEEE-754 accuracy - however this emulator does.
1420 if (!cpu_has_mips_4_5_r2)
1423 handler.u = fpemu_sp_rsqrt;
1427 if (!cpu_has_mips_4_5_r2)
1430 handler.u = fpemu_sp_recip;
1434 if (!cpu_has_mips_4_5_r)
1437 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1438 if (((ctx->fcr31 & cond) != 0) !=
1439 ((MIPSInst_FT(ir) & 1) != 0))
1441 SPFROMREG(rv.s, MIPSInst_FS(ir));
1445 if (!cpu_has_mips_4_5_r)
1448 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1450 SPFROMREG(rv.s, MIPSInst_FS(ir));
1454 if (!cpu_has_mips_4_5_r)
1457 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1459 SPFROMREG(rv.s, MIPSInst_FS(ir));
1463 handler.u = ieee754sp_abs;
1467 handler.u = ieee754sp_neg;
1472 SPFROMREG(rv.s, MIPSInst_FS(ir));
1475 /* binary op on handler */
1477 SPFROMREG(fs, MIPSInst_FS(ir));
1478 SPFROMREG(ft, MIPSInst_FT(ir));
1480 rv.s = (*handler.b) (fs, ft);
1483 SPFROMREG(fs, MIPSInst_FS(ir));
1484 rv.s = (*handler.u) (fs);
1487 if (ieee754_cxtest(IEEE754_INEXACT)) {
1488 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
1489 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1491 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1492 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
1493 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1495 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1496 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
1497 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1499 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1500 MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
1501 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
1503 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1504 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
1505 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1509 /* unary conv ops */
1511 return SIGILL; /* not defined */
1514 SPFROMREG(fs, MIPSInst_FS(ir));
1515 rv.d = ieee754dp_fsp(fs);
1520 SPFROMREG(fs, MIPSInst_FS(ir));
1521 rv.w = ieee754sp_tint(fs);
1529 if (!cpu_has_mips_2_3_4_5 && !cpu_has_mips64)
1532 oldrm = ieee754_csr.rm;
1533 SPFROMREG(fs, MIPSInst_FS(ir));
1534 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
1535 rv.w = ieee754sp_tint(fs);
1536 ieee754_csr.rm = oldrm;
1541 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1544 SPFROMREG(fs, MIPSInst_FS(ir));
1545 rv.l = ieee754sp_tlong(fs);
1553 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1556 oldrm = ieee754_csr.rm;
1557 SPFROMREG(fs, MIPSInst_FS(ir));
1558 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
1559 rv.l = ieee754sp_tlong(fs);
1560 ieee754_csr.rm = oldrm;
1565 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1566 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1567 union ieee754sp fs, ft;
1569 SPFROMREG(fs, MIPSInst_FS(ir));
1570 SPFROMREG(ft, MIPSInst_FT(ir));
1571 rv.w = ieee754sp_cmp(fs, ft,
1572 cmptab[cmpop & 0x7], cmpop & 0x8);
1574 if ((cmpop & 0x8) && ieee754_cxtest
1575 (IEEE754_INVALID_OPERATION))
1576 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1588 union ieee754dp fs, ft;
1590 union ieee754dp(*b) (union ieee754dp, union ieee754dp);
1591 union ieee754dp(*u) (union ieee754dp);
1594 switch (MIPSInst_FUNC(ir)) {
1597 handler.b = ieee754dp_add;
1600 handler.b = ieee754dp_sub;
1603 handler.b = ieee754dp_mul;
1606 handler.b = ieee754dp_div;
1611 if (!cpu_has_mips_2_3_4_5_r)
1614 handler.u = ieee754dp_sqrt;
1617 * Note that on some MIPS IV implementations such as the
1618 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1619 * achieve full IEEE-754 accuracy - however this emulator does.
1622 if (!cpu_has_mips_4_5_r2)
1625 handler.u = fpemu_dp_rsqrt;
1628 if (!cpu_has_mips_4_5_r2)
1631 handler.u = fpemu_dp_recip;
1634 if (!cpu_has_mips_4_5_r)
1637 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1638 if (((ctx->fcr31 & cond) != 0) !=
1639 ((MIPSInst_FT(ir) & 1) != 0))
1641 DPFROMREG(rv.d, MIPSInst_FS(ir));
1644 if (!cpu_has_mips_4_5_r)
1647 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1649 DPFROMREG(rv.d, MIPSInst_FS(ir));
1652 if (!cpu_has_mips_4_5_r)
1655 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1657 DPFROMREG(rv.d, MIPSInst_FS(ir));
1660 handler.u = ieee754dp_abs;
1664 handler.u = ieee754dp_neg;
1669 DPFROMREG(rv.d, MIPSInst_FS(ir));
1672 /* binary op on handler */
1674 DPFROMREG(fs, MIPSInst_FS(ir));
1675 DPFROMREG(ft, MIPSInst_FT(ir));
1677 rv.d = (*handler.b) (fs, ft);
1680 DPFROMREG(fs, MIPSInst_FS(ir));
1681 rv.d = (*handler.u) (fs);
1688 DPFROMREG(fs, MIPSInst_FS(ir));
1689 rv.s = ieee754sp_fdp(fs);
1694 return SIGILL; /* not defined */
1697 DPFROMREG(fs, MIPSInst_FS(ir));
1698 rv.w = ieee754dp_tint(fs); /* wrong */
1706 if (!cpu_has_mips_2_3_4_5_r)
1709 oldrm = ieee754_csr.rm;
1710 DPFROMREG(fs, MIPSInst_FS(ir));
1711 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
1712 rv.w = ieee754dp_tint(fs);
1713 ieee754_csr.rm = oldrm;
1718 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1721 DPFROMREG(fs, MIPSInst_FS(ir));
1722 rv.l = ieee754dp_tlong(fs);
1730 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1733 oldrm = ieee754_csr.rm;
1734 DPFROMREG(fs, MIPSInst_FS(ir));
1735 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
1736 rv.l = ieee754dp_tlong(fs);
1737 ieee754_csr.rm = oldrm;
1742 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1743 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1744 union ieee754dp fs, ft;
1746 DPFROMREG(fs, MIPSInst_FS(ir));
1747 DPFROMREG(ft, MIPSInst_FT(ir));
1748 rv.w = ieee754dp_cmp(fs, ft,
1749 cmptab[cmpop & 0x7], cmpop & 0x8);
1754 (IEEE754_INVALID_OPERATION))
1755 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1768 switch (MIPSInst_FUNC(ir)) {
1770 /* convert word to single precision real */
1771 SPFROMREG(fs, MIPSInst_FS(ir));
1772 rv.s = ieee754sp_fint(fs.bits);
1776 /* convert word to double precision real */
1777 SPFROMREG(fs, MIPSInst_FS(ir));
1778 rv.d = ieee754dp_fint(fs.bits);
1789 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1792 DIFROMREG(bits, MIPSInst_FS(ir));
1794 switch (MIPSInst_FUNC(ir)) {
1796 /* convert long to single precision real */
1797 rv.s = ieee754sp_flong(bits);
1801 /* convert long to double precision real */
1802 rv.d = ieee754dp_flong(bits);
1815 * Update the fpu CSR register for this operation.
1816 * If an exception is required, generate a tidy SIGFPE exception,
1817 * without updating the result register.
1818 * Note: cause exception bits do not accumulate, they are rewritten
1819 * for each op; only the flag/sticky bits accumulate.
1821 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1822 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1823 /*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
1828 * Now we can safely write the result back to the register file.
1833 if (cpu_has_mips_4_5_r)
1834 cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
1836 cbit = FPU_CSR_COND;
1840 ctx->fcr31 &= ~cbit;
1844 DPTOREG(rv.d, MIPSInst_FD(ir));
1847 SPTOREG(rv.s, MIPSInst_FD(ir));
1850 SITOREG(rv.w, MIPSInst_FD(ir));
1853 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1856 DITOREG(rv.l, MIPSInst_FD(ir));
1865 int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1866 int has_fpu, void *__user *fault_addr)
1868 unsigned long oldepc, prevepc;
1869 struct mm_decoded_insn dec_insn;
1874 oldepc = xcp->cp0_epc;
1876 prevepc = xcp->cp0_epc;
1878 if (get_isa16_mode(prevepc) && cpu_has_mmips) {
1880 * Get next 2 microMIPS instructions and convert them
1881 * into 32-bit instructions.
1883 if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
1884 (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
1885 (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
1886 (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
1887 MIPS_FPU_EMU_INC_STATS(errors);
1892 /* Get first instruction. */
1893 if (mm_insn_16bit(*instr_ptr)) {
1894 /* Duplicate the half-word. */
1895 dec_insn.insn = (*instr_ptr << 16) |
1897 /* 16-bit instruction. */
1898 dec_insn.pc_inc = 2;
1901 dec_insn.insn = (*instr_ptr << 16) |
1903 /* 32-bit instruction. */
1904 dec_insn.pc_inc = 4;
1907 /* Get second instruction. */
1908 if (mm_insn_16bit(*instr_ptr)) {
1909 /* Duplicate the half-word. */
1910 dec_insn.next_insn = (*instr_ptr << 16) |
1912 /* 16-bit instruction. */
1913 dec_insn.next_pc_inc = 2;
1915 dec_insn.next_insn = (*instr_ptr << 16) |
1917 /* 32-bit instruction. */
1918 dec_insn.next_pc_inc = 4;
1920 dec_insn.micro_mips_mode = 1;
1922 if ((get_user(dec_insn.insn,
1923 (mips_instruction __user *) xcp->cp0_epc)) ||
1924 (get_user(dec_insn.next_insn,
1925 (mips_instruction __user *)(xcp->cp0_epc+4)))) {
1926 MIPS_FPU_EMU_INC_STATS(errors);
1929 dec_insn.pc_inc = 4;
1930 dec_insn.next_pc_inc = 4;
1931 dec_insn.micro_mips_mode = 0;
1934 if ((dec_insn.insn == 0) ||
1935 ((dec_insn.pc_inc == 2) &&
1936 ((dec_insn.insn & 0xffff) == MM_NOP16)))
1937 xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
1940 * The 'ieee754_csr' is an alias of
1941 * ctx->fcr31. No need to copy ctx->fcr31 to
1942 * ieee754_csr. But ieee754_csr.rm is ieee
1943 * library modes. (not mips rounding mode)
1945 sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
1954 } while (xcp->cp0_epc > prevepc);
1956 /* SIGILL indicates a non-fpu instruction */
1957 if (sig == SIGILL && xcp->cp0_epc != oldepc)
1958 /* but if EPC has advanced, then ignore it */