2 * cp1emu.c: a MIPS coprocessor 1 (fpu) instruction emulator
4 * MIPS floating point support
5 * Copyright (C) 1994-2000 Algorithmics Ltd.
6 * http://www.algor.co.uk
8 * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
9 * Copyright (C) 2000 MIPS Technologies, Inc.
11 * This program is free software; you can distribute it and/or modify it
12 * under the terms of the GNU General Public License (Version 2) as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
24 * A complete emulator for MIPS coprocessor 1 instructions. This is
25 * required for #float(switch) or #float(trap), where it catches all
26 * COP1 instructions via the "CoProcessor Unusable" exception.
28 * More surprisingly it is also required for #float(ieee), to help out
29 * the hardware fpu at the boundaries of the IEEE-754 representation
30 * (denormalised values, infinities, underflow, etc). It is made
31 * quite nasty because emulation of some non-COP1 instructions is
32 * required, e.g. in branch delay slots.
34 * Note if you know that you won't have an fpu, then you'll get much
35 * better performance by compiling with -msoft-float!
37 #include <linux/sched.h>
40 #include <asm/bootinfo.h>
42 #include <asm/cpu-features.h>
43 #include <asm/processor.h>
44 #include <asm/ptrace.h>
45 #include <asm/signal.h>
46 #include <asm/mipsregs.h>
47 #include <asm/fpu_emulator.h>
48 #include <asm/uaccess.h>
49 #include <asm/branch.h>
54 /* Strap kernel emulator for full MIPS IV emulation */
61 /* Function which emulates a floating point instruction. */
63 static int fpu_emu(struct pt_regs *, struct mips_fpu_soft_struct *,
66 #if __mips >= 4 && __mips != 32
67 static int fpux_emu(struct pt_regs *,
68 struct mips_fpu_soft_struct *, mips_instruction);
71 /* Further private data for which no space exists in mips_fpu_soft_struct */
73 struct mips_fpu_emulator_stats fpuemustats;
75 /* Control registers */
77 #define FPCREG_RID 0 /* $0 = revision id */
78 #define FPCREG_CSR 31 /* $31 = csr */
80 /* Convert Mips rounding mode (0..3) to IEEE library modes. */
81 static const unsigned char ieee_rm[4] = {
82 [FPU_CSR_RN] = IEEE754_RN,
83 [FPU_CSR_RZ] = IEEE754_RZ,
84 [FPU_CSR_RU] = IEEE754_RU,
85 [FPU_CSR_RD] = IEEE754_RD,
87 /* Convert IEEE library modes to Mips rounding mode (0..3). */
88 static const unsigned char mips_rm[4] = {
89 [IEEE754_RN] = FPU_CSR_RN,
90 [IEEE754_RZ] = FPU_CSR_RZ,
91 [IEEE754_RD] = FPU_CSR_RD,
92 [IEEE754_RU] = FPU_CSR_RU,
96 /* convert condition code register number to csr bit */
97 static const unsigned int fpucondbit[8] = {
111 * Redundant with logic already in kernel/branch.c,
112 * embedded in compute_return_epc. At some point,
113 * a single subroutine should be used across both
116 static int isBranchInstr(mips_instruction * i)
118 switch (MIPSInst_OPCODE(*i)) {
120 switch (MIPSInst_FUNC(*i)) {
128 switch (MIPSInst_RT(*i)) {
158 if (MIPSInst_RS(*i) == bc_op)
167 * In the Linux kernel, we support selection of FPR format on the
168 * basis of the Status.FR bit. This does imply that, if a full 32
169 * FPRs are desired, there needs to be a flip-flop that can be written
170 * to one at that bit position. In any case, O32 MIPS ABI uses
171 * only the even FPRs (Status.FR = 0).
174 #define CP0_STATUS_FR_SUPPORT
176 #ifdef CP0_STATUS_FR_SUPPORT
177 #define FR_BIT ST0_FR
182 #define SIFROMREG(si,x) ((si) = \
183 (xcp->cp0_status & FR_BIT) || !(x & 1) ? \
185 (int)(ctx->fpr[x & ~1] >> 32 ))
186 #define SITOREG(si,x) (ctx->fpr[x & ~((xcp->cp0_status & FR_BIT) == 0)] = \
187 (xcp->cp0_status & FR_BIT) || !(x & 1) ? \
188 ctx->fpr[x & ~1] >> 32 << 32 | (u32)(si) : \
189 ctx->fpr[x & ~1] << 32 >> 32 | (u64)(si) << 32)
191 #define DIFROMREG(di,x) ((di) = \
192 ctx->fpr[x & ~((xcp->cp0_status & FR_BIT) == 0)])
193 #define DITOREG(di,x) (ctx->fpr[x & ~((xcp->cp0_status & FR_BIT) == 0)] \
196 #define SPFROMREG(sp,x) SIFROMREG((sp).bits,x)
197 #define SPTOREG(sp,x) SITOREG((sp).bits,x)
198 #define DPFROMREG(dp,x) DIFROMREG((dp).bits,x)
199 #define DPTOREG(dp,x) DITOREG((dp).bits,x)
202 * Emulate the single floating point instruction pointed at by EPC.
203 * Two instructions if the instruction is in a branch delay slot.
206 static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_soft_struct *ctx)
209 void * emulpc, *contpc;
212 if (get_user(ir, (mips_instruction __user *) xcp->cp0_epc)) {
213 fpuemustats.errors++;
217 /* XXX NEC Vr54xx bug workaround */
218 if ((xcp->cp0_cause & CAUSEF_BD) && !isBranchInstr(&ir))
219 xcp->cp0_cause &= ~CAUSEF_BD;
221 if (xcp->cp0_cause & CAUSEF_BD) {
223 * The instruction to be emulated is in a branch delay slot
224 * which means that we have to emulate the branch instruction
225 * BEFORE we do the cop1 instruction.
227 * This branch could be a COP1 branch, but in that case we
228 * would have had a trap for that instruction, and would not
229 * come through this route.
231 * Linux MIPS branch emulator operates on context, updating the
234 emulpc = (void *) (xcp->cp0_epc + 4); /* Snapshot emulation target */
236 if (__compute_return_epc(xcp)) {
238 printk("failed to emulate branch at %p\n",
239 (void *) (xcp->cp0_epc));
243 if (get_user(ir, (mips_instruction __user *) emulpc)) {
244 fpuemustats.errors++;
247 /* __compute_return_epc() will have updated cp0_epc */
248 contpc = (void *) xcp->cp0_epc;
249 /* In order not to confuse ptrace() et al, tweak context */
250 xcp->cp0_epc = (unsigned long) emulpc - 4;
252 emulpc = (void *) xcp->cp0_epc;
253 contpc = (void *) (xcp->cp0_epc + 4);
257 fpuemustats.emulated++;
258 switch (MIPSInst_OPCODE(ir)) {
260 u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
265 if (get_user(val, va)) {
266 fpuemustats.errors++;
269 DITOREG(val, MIPSInst_RT(ir));
274 u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
278 fpuemustats.stores++;
279 DIFROMREG(val, MIPSInst_RT(ir));
280 if (put_user(val, va)) {
281 fpuemustats.errors++;
288 u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
293 if (get_user(val, va)) {
294 fpuemustats.errors++;
297 SITOREG(val, MIPSInst_RT(ir));
302 u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
306 fpuemustats.stores++;
307 SIFROMREG(val, MIPSInst_RT(ir));
308 if (put_user(val, va)) {
309 fpuemustats.errors++;
316 switch (MIPSInst_RS(ir)) {
318 #if defined(__mips64)
320 /* copregister fs -> gpr[rt] */
321 if (MIPSInst_RT(ir) != 0) {
322 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
328 /* copregister fs <- rt */
329 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
334 /* copregister rd -> gpr[rt] */
335 if (MIPSInst_RT(ir) != 0) {
336 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
342 /* copregister rd <- rt */
343 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
347 /* cop control register rd -> gpr[rt] */
350 if (ir == CP1UNDEF) {
351 return do_dsemulret(xcp);
353 if (MIPSInst_RD(ir) == FPCREG_CSR) {
355 value = (value & ~0x3) | mips_rm[value & 0x3];
357 printk("%p gpr[%d]<-csr=%08x\n",
358 (void *) (xcp->cp0_epc),
359 MIPSInst_RT(ir), value);
362 else if (MIPSInst_RD(ir) == FPCREG_RID)
367 xcp->regs[MIPSInst_RT(ir)] = value;
372 /* copregister rd <- rt */
375 if (MIPSInst_RT(ir) == 0)
378 value = xcp->regs[MIPSInst_RT(ir)];
380 /* we only have one writable control reg
382 if (MIPSInst_RD(ir) == FPCREG_CSR) {
384 printk("%p gpr[%d]->csr=%08x\n",
385 (void *) (xcp->cp0_epc),
386 MIPSInst_RT(ir), value);
388 value &= (FPU_CSR_FLUSH | FPU_CSR_ALL_E | FPU_CSR_ALL_S | 0x03);
389 ctx->fcr31 &= ~(FPU_CSR_FLUSH | FPU_CSR_ALL_E | FPU_CSR_ALL_S | 0x03);
390 /* convert to ieee library modes */
391 ctx->fcr31 |= (value & ~0x3) | ieee_rm[value & 0x3];
393 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
402 if (xcp->cp0_cause & CAUSEF_BD)
406 cond = ctx->fcr31 & fpucondbit[MIPSInst_RT(ir) >> 2];
408 cond = ctx->fcr31 & FPU_CSR_COND;
410 switch (MIPSInst_RT(ir) & 3) {
421 /* thats an illegal instruction */
425 xcp->cp0_cause |= CAUSEF_BD;
427 /* branch taken: emulate dslot
433 (MIPSInst_SIMM(ir) << 2));
436 (mips_instruction __user *) xcp->cp0_epc)) {
437 fpuemustats.errors++;
441 switch (MIPSInst_OPCODE(ir)) {
444 #if (__mips >= 2 || defined(__mips64))
449 #if __mips >= 4 && __mips != 32
452 /* its one of ours */
456 if (MIPSInst_FUNC(ir) == movc_op)
463 * Single step the non-cp1
464 * instruction in the dslot
466 return mips_dsemul(xcp, ir, (unsigned long) contpc);
469 /* branch not taken */
472 * branch likely nullifies
478 * else continue & execute
479 * dslot as normal insn
487 if (!(MIPSInst_RS(ir) & 0x10))
492 /* a real fpu computation instruction */
493 if ((sig = fpu_emu(xcp, ctx, ir)))
499 #if __mips >= 4 && __mips != 32
503 if ((sig = fpux_emu(xcp, ctx, ir)))
511 if (MIPSInst_FUNC(ir) != movc_op)
513 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
514 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
515 xcp->regs[MIPSInst_RD(ir)] =
516 xcp->regs[MIPSInst_RS(ir)];
525 xcp->cp0_epc = (unsigned long) contpc;
526 xcp->cp0_cause &= ~CAUSEF_BD;
532 * Conversion table from MIPS compare ops 48-63
533 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
535 static const unsigned char cmptab[8] = {
536 0, /* cmp_0 (sig) cmp_sf */
537 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
538 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
539 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
540 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
541 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
542 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
543 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
547 #if __mips >= 4 && __mips != 32
550 * Additional MIPS4 instructions
553 #define DEF3OP(name, p, f1, f2, f3) \
554 static ieee754##p fpemu_##p##_##name (ieee754##p r, ieee754##p s, \
557 struct _ieee754_csr ieee754_csr_save; \
559 ieee754_csr_save = ieee754_csr; \
561 ieee754_csr_save.cx |= ieee754_csr.cx; \
562 ieee754_csr_save.sx |= ieee754_csr.sx; \
564 ieee754_csr.cx |= ieee754_csr_save.cx; \
565 ieee754_csr.sx |= ieee754_csr_save.sx; \
569 static ieee754dp fpemu_dp_recip(ieee754dp d)
571 return ieee754dp_div(ieee754dp_one(0), d);
574 static ieee754dp fpemu_dp_rsqrt(ieee754dp d)
576 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
579 static ieee754sp fpemu_sp_recip(ieee754sp s)
581 return ieee754sp_div(ieee754sp_one(0), s);
584 static ieee754sp fpemu_sp_rsqrt(ieee754sp s)
586 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
589 DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add,);
590 DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub,);
591 DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
592 DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
593 DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add,);
594 DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub,);
595 DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
596 DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
598 static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_soft_struct *ctx,
601 unsigned rcsr = 0; /* resulting csr */
603 fpuemustats.cp1xops++;
605 switch (MIPSInst_FMA_FFMT(ir)) {
608 ieee754sp(*handler) (ieee754sp, ieee754sp, ieee754sp);
609 ieee754sp fd, fr, fs, ft;
613 switch (MIPSInst_FUNC(ir)) {
615 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
616 xcp->regs[MIPSInst_FT(ir)]);
619 if (get_user(val, va)) {
620 fpuemustats.errors++;
623 SITOREG(val, MIPSInst_FD(ir));
627 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
628 xcp->regs[MIPSInst_FT(ir)]);
630 fpuemustats.stores++;
632 SIFROMREG(val, MIPSInst_FS(ir));
633 if (put_user(val, va)) {
634 fpuemustats.errors++;
640 handler = fpemu_sp_madd;
643 handler = fpemu_sp_msub;
646 handler = fpemu_sp_nmadd;
649 handler = fpemu_sp_nmsub;
653 SPFROMREG(fr, MIPSInst_FR(ir));
654 SPFROMREG(fs, MIPSInst_FS(ir));
655 SPFROMREG(ft, MIPSInst_FT(ir));
656 fd = (*handler) (fr, fs, ft);
657 SPTOREG(fd, MIPSInst_FD(ir));
660 if (ieee754_cxtest(IEEE754_INEXACT))
661 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
662 if (ieee754_cxtest(IEEE754_UNDERFLOW))
663 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
664 if (ieee754_cxtest(IEEE754_OVERFLOW))
665 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
666 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
667 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
669 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
670 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
671 /*printk ("SIGFPE: fpu csr = %08x\n",
685 ieee754dp(*handler) (ieee754dp, ieee754dp, ieee754dp);
686 ieee754dp fd, fr, fs, ft;
690 switch (MIPSInst_FUNC(ir)) {
692 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
693 xcp->regs[MIPSInst_FT(ir)]);
696 if (get_user(val, va)) {
697 fpuemustats.errors++;
700 DITOREG(val, MIPSInst_FD(ir));
704 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
705 xcp->regs[MIPSInst_FT(ir)]);
707 fpuemustats.stores++;
708 DIFROMREG(val, MIPSInst_FS(ir));
709 if (put_user(val, va)) {
710 fpuemustats.errors++;
716 handler = fpemu_dp_madd;
719 handler = fpemu_dp_msub;
722 handler = fpemu_dp_nmadd;
725 handler = fpemu_dp_nmsub;
729 DPFROMREG(fr, MIPSInst_FR(ir));
730 DPFROMREG(fs, MIPSInst_FS(ir));
731 DPFROMREG(ft, MIPSInst_FT(ir));
732 fd = (*handler) (fr, fs, ft);
733 DPTOREG(fd, MIPSInst_FD(ir));
743 if (MIPSInst_FUNC(ir) != pfetch_op) {
746 /* ignore prefx operation */
760 * Emulate a single COP1 arithmetic instruction.
762 static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_soft_struct *ctx,
765 int rfmt; /* resulting format */
766 unsigned rcsr = 0; /* resulting csr */
775 } rv; /* resulting value */
777 fpuemustats.cp1ops++;
778 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
781 ieee754sp(*b) (ieee754sp, ieee754sp);
782 ieee754sp(*u) (ieee754sp);
785 switch (MIPSInst_FUNC(ir)) {
788 handler.b = ieee754sp_add;
791 handler.b = ieee754sp_sub;
794 handler.b = ieee754sp_mul;
797 handler.b = ieee754sp_div;
801 #if __mips >= 2 || defined(__mips64)
803 handler.u = ieee754sp_sqrt;
806 #if __mips >= 4 && __mips != 32
808 handler.u = fpemu_sp_rsqrt;
811 handler.u = fpemu_sp_recip;
816 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
817 if (((ctx->fcr31 & cond) != 0) !=
818 ((MIPSInst_FT(ir) & 1) != 0))
820 SPFROMREG(rv.s, MIPSInst_FS(ir));
823 if (xcp->regs[MIPSInst_FT(ir)] != 0)
825 SPFROMREG(rv.s, MIPSInst_FS(ir));
828 if (xcp->regs[MIPSInst_FT(ir)] == 0)
830 SPFROMREG(rv.s, MIPSInst_FS(ir));
834 handler.u = ieee754sp_abs;
837 handler.u = ieee754sp_neg;
841 SPFROMREG(rv.s, MIPSInst_FS(ir));
844 /* binary op on handler */
849 SPFROMREG(fs, MIPSInst_FS(ir));
850 SPFROMREG(ft, MIPSInst_FT(ir));
852 rv.s = (*handler.b) (fs, ft);
859 SPFROMREG(fs, MIPSInst_FS(ir));
860 rv.s = (*handler.u) (fs);
864 if (ieee754_cxtest(IEEE754_INEXACT))
865 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
866 if (ieee754_cxtest(IEEE754_UNDERFLOW))
867 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
868 if (ieee754_cxtest(IEEE754_OVERFLOW))
869 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
870 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE))
871 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
872 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
873 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
878 return SIGILL; /* not defined */
882 SPFROMREG(fs, MIPSInst_FS(ir));
883 rv.d = ieee754dp_fsp(fs);
890 SPFROMREG(fs, MIPSInst_FS(ir));
891 rv.w = ieee754sp_tint(fs);
896 #if __mips >= 2 || defined(__mips64)
901 unsigned int oldrm = ieee754_csr.rm;
904 SPFROMREG(fs, MIPSInst_FS(ir));
905 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
906 rv.w = ieee754sp_tint(fs);
907 ieee754_csr.rm = oldrm;
911 #endif /* __mips >= 2 */
913 #if defined(__mips64)
917 SPFROMREG(fs, MIPSInst_FS(ir));
918 rv.l = ieee754sp_tlong(fs);
927 unsigned int oldrm = ieee754_csr.rm;
930 SPFROMREG(fs, MIPSInst_FS(ir));
931 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
932 rv.l = ieee754sp_tlong(fs);
933 ieee754_csr.rm = oldrm;
937 #endif /* defined(__mips64) */
940 if (MIPSInst_FUNC(ir) >= fcmp_op) {
941 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
944 SPFROMREG(fs, MIPSInst_FS(ir));
945 SPFROMREG(ft, MIPSInst_FT(ir));
946 rv.w = ieee754sp_cmp(fs, ft,
947 cmptab[cmpop & 0x7], cmpop & 0x8);
949 if ((cmpop & 0x8) && ieee754_cxtest
950 (IEEE754_INVALID_OPERATION))
951 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
966 ieee754dp(*b) (ieee754dp, ieee754dp);
967 ieee754dp(*u) (ieee754dp);
970 switch (MIPSInst_FUNC(ir)) {
973 handler.b = ieee754dp_add;
976 handler.b = ieee754dp_sub;
979 handler.b = ieee754dp_mul;
982 handler.b = ieee754dp_div;
986 #if __mips >= 2 || defined(__mips64)
988 handler.u = ieee754dp_sqrt;
991 #if __mips >= 4 && __mips != 32
993 handler.u = fpemu_dp_rsqrt;
996 handler.u = fpemu_dp_recip;
1001 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1002 if (((ctx->fcr31 & cond) != 0) !=
1003 ((MIPSInst_FT(ir) & 1) != 0))
1005 DPFROMREG(rv.d, MIPSInst_FS(ir));
1008 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1010 DPFROMREG(rv.d, MIPSInst_FS(ir));
1013 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1015 DPFROMREG(rv.d, MIPSInst_FS(ir));
1019 handler.u = ieee754dp_abs;
1023 handler.u = ieee754dp_neg;
1028 DPFROMREG(rv.d, MIPSInst_FS(ir));
1031 /* binary op on handler */
1035 DPFROMREG(fs, MIPSInst_FS(ir));
1036 DPFROMREG(ft, MIPSInst_FT(ir));
1038 rv.d = (*handler.b) (fs, ft);
1044 DPFROMREG(fs, MIPSInst_FS(ir));
1045 rv.d = (*handler.u) (fs);
1049 /* unary conv ops */
1053 DPFROMREG(fs, MIPSInst_FS(ir));
1054 rv.s = ieee754sp_fdp(fs);
1059 return SIGILL; /* not defined */
1064 DPFROMREG(fs, MIPSInst_FS(ir));
1065 rv.w = ieee754dp_tint(fs); /* wrong */
1070 #if __mips >= 2 || defined(__mips64)
1075 unsigned int oldrm = ieee754_csr.rm;
1078 DPFROMREG(fs, MIPSInst_FS(ir));
1079 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
1080 rv.w = ieee754dp_tint(fs);
1081 ieee754_csr.rm = oldrm;
1087 #if defined(__mips64)
1091 DPFROMREG(fs, MIPSInst_FS(ir));
1092 rv.l = ieee754dp_tlong(fs);
1101 unsigned int oldrm = ieee754_csr.rm;
1104 DPFROMREG(fs, MIPSInst_FS(ir));
1105 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
1106 rv.l = ieee754dp_tlong(fs);
1107 ieee754_csr.rm = oldrm;
1111 #endif /* __mips >= 3 */
1114 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1115 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1118 DPFROMREG(fs, MIPSInst_FS(ir));
1119 DPFROMREG(ft, MIPSInst_FT(ir));
1120 rv.w = ieee754dp_cmp(fs, ft,
1121 cmptab[cmpop & 0x7], cmpop & 0x8);
1126 (IEEE754_INVALID_OPERATION))
1127 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1143 switch (MIPSInst_FUNC(ir)) {
1145 /* convert word to single precision real */
1146 SPFROMREG(fs, MIPSInst_FS(ir));
1147 rv.s = ieee754sp_fint(fs.bits);
1151 /* convert word to double precision real */
1152 SPFROMREG(fs, MIPSInst_FS(ir));
1153 rv.d = ieee754dp_fint(fs.bits);
1162 #if defined(__mips64)
1164 switch (MIPSInst_FUNC(ir)) {
1166 /* convert long to single precision real */
1167 rv.s = ieee754sp_flong(ctx->fpr[MIPSInst_FS(ir)]);
1171 /* convert long to double precision real */
1172 rv.d = ieee754dp_flong(ctx->fpr[MIPSInst_FS(ir)]);
1187 * Update the fpu CSR register for this operation.
1188 * If an exception is required, generate a tidy SIGFPE exception,
1189 * without updating the result register.
1190 * Note: cause exception bits do not accumulate, they are rewritten
1191 * for each op; only the flag/sticky bits accumulate.
1193 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1194 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1195 /*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */
1200 * Now we can safely write the result back to the register file.
1205 cond = fpucondbit[MIPSInst_FD(ir) >> 2];
1207 cond = FPU_CSR_COND;
1212 ctx->fcr31 &= ~cond;
1216 DPTOREG(rv.d, MIPSInst_FD(ir));
1219 SPTOREG(rv.s, MIPSInst_FD(ir));
1222 SITOREG(rv.w, MIPSInst_FD(ir));
1224 #if defined(__mips64)
1226 DITOREG(rv.l, MIPSInst_FD(ir));
1236 int fpu_emulator_cop1Handler(struct pt_regs *xcp,
1237 struct mips_fpu_soft_struct *ctx)
1239 unsigned long oldepc, prevepc;
1240 mips_instruction insn;
1243 oldepc = xcp->cp0_epc;
1245 prevepc = xcp->cp0_epc;
1247 if (get_user(insn, (mips_instruction __user *) xcp->cp0_epc)) {
1248 fpuemustats.errors++;
1252 xcp->cp0_epc += 4; /* skip nops */
1255 * The 'ieee754_csr' is an alias of
1256 * ctx->fcr31. No need to copy ctx->fcr31 to
1257 * ieee754_csr. But ieee754_csr.rm is ieee
1258 * library modes. (not mips rounding mode)
1260 /* convert to ieee library modes */
1261 ieee754_csr.rm = ieee_rm[ieee754_csr.rm];
1262 sig = cop1Emulate(xcp, ctx);
1263 /* revert to mips rounding mode */
1264 ieee754_csr.rm = mips_rm[ieee754_csr.rm];
1273 } while (xcp->cp0_epc > prevepc);
1275 /* SIGILL indicates a non-fpu instruction */
1276 if (sig == SIGILL && xcp->cp0_epc != oldepc)
1277 /* but if epc has advanced, then ignore it */