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>
38 #include <linux/debugfs.h>
41 #include <asm/bootinfo.h>
42 #include <asm/processor.h>
43 #include <asm/ptrace.h>
44 #include <asm/signal.h>
45 #include <asm/mipsregs.h>
46 #include <asm/fpu_emulator.h>
47 #include <asm/uaccess.h>
48 #include <asm/branch.h>
52 /* Strap kernel emulator for full MIPS IV emulation */
59 /* Function which emulates a floating point instruction. */
61 static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
64 #if __mips >= 4 && __mips != 32
65 static int fpux_emu(struct pt_regs *,
66 struct mips_fpu_struct *, mips_instruction);
69 /* Further private data for which no space exists in mips_fpu_struct */
71 struct mips_fpu_emulator_stats fpuemustats;
73 /* Control registers */
75 #define FPCREG_RID 0 /* $0 = revision id */
76 #define FPCREG_CSR 31 /* $31 = csr */
78 /* Convert Mips rounding mode (0..3) to IEEE library modes. */
79 static const unsigned char ieee_rm[4] = {
80 [FPU_CSR_RN] = IEEE754_RN,
81 [FPU_CSR_RZ] = IEEE754_RZ,
82 [FPU_CSR_RU] = IEEE754_RU,
83 [FPU_CSR_RD] = IEEE754_RD,
85 /* Convert IEEE library modes to Mips rounding mode (0..3). */
86 static const unsigned char mips_rm[4] = {
87 [IEEE754_RN] = FPU_CSR_RN,
88 [IEEE754_RZ] = FPU_CSR_RZ,
89 [IEEE754_RD] = FPU_CSR_RD,
90 [IEEE754_RU] = FPU_CSR_RU,
94 /* convert condition code register number to csr bit */
95 static const unsigned int fpucondbit[8] = {
109 * Redundant with logic already in kernel/branch.c,
110 * embedded in compute_return_epc. At some point,
111 * a single subroutine should be used across both
114 static int isBranchInstr(mips_instruction * i)
116 switch (MIPSInst_OPCODE(*i)) {
118 switch (MIPSInst_FUNC(*i)) {
126 switch (MIPSInst_RT(*i)) {
156 if (MIPSInst_RS(*i) == bc_op)
165 * In the Linux kernel, we support selection of FPR format on the
166 * basis of the Status.FR bit. This does imply that, if a full 32
167 * FPRs are desired, there needs to be a flip-flop that can be written
168 * to one at that bit position. In any case, O32 MIPS ABI uses
169 * only the even FPRs (Status.FR = 0).
172 #define CP0_STATUS_FR_SUPPORT
174 #ifdef CP0_STATUS_FR_SUPPORT
175 #define FR_BIT ST0_FR
180 #define SIFROMREG(si, x) ((si) = \
181 (xcp->cp0_status & FR_BIT) || !(x & 1) ? \
183 (int)(ctx->fpr[x & ~1] >> 32 ))
184 #define SITOREG(si, x) (ctx->fpr[x & ~((xcp->cp0_status & FR_BIT) == 0)] = \
185 (xcp->cp0_status & FR_BIT) || !(x & 1) ? \
186 ctx->fpr[x & ~1] >> 32 << 32 | (u32)(si) : \
187 ctx->fpr[x & ~1] << 32 >> 32 | (u64)(si) << 32)
189 #define DIFROMREG(di, x) ((di) = \
190 ctx->fpr[x & ~((xcp->cp0_status & FR_BIT) == 0)])
191 #define DITOREG(di, x) (ctx->fpr[x & ~((xcp->cp0_status & FR_BIT) == 0)] \
194 #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
195 #define SPTOREG(sp, x) SITOREG((sp).bits, x)
196 #define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
197 #define DPTOREG(dp, x) DITOREG((dp).bits, x)
200 * Emulate the single floating point instruction pointed at by EPC.
201 * Two instructions if the instruction is in a branch delay slot.
204 static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx)
207 unsigned long emulpc, contpc;
210 if (get_user(ir, (mips_instruction __user *) xcp->cp0_epc)) {
211 fpuemustats.errors++;
215 /* XXX NEC Vr54xx bug workaround */
216 if ((xcp->cp0_cause & CAUSEF_BD) && !isBranchInstr(&ir))
217 xcp->cp0_cause &= ~CAUSEF_BD;
219 if (xcp->cp0_cause & CAUSEF_BD) {
221 * The instruction to be emulated is in a branch delay slot
222 * which means that we have to emulate the branch instruction
223 * BEFORE we do the cop1 instruction.
225 * This branch could be a COP1 branch, but in that case we
226 * would have had a trap for that instruction, and would not
227 * come through this route.
229 * Linux MIPS branch emulator operates on context, updating the
232 emulpc = xcp->cp0_epc + 4; /* Snapshot emulation target */
234 if (__compute_return_epc(xcp)) {
236 printk("failed to emulate branch at %p\n",
237 (void *) (xcp->cp0_epc));
241 if (get_user(ir, (mips_instruction __user *) emulpc)) {
242 fpuemustats.errors++;
245 /* __compute_return_epc() will have updated cp0_epc */
246 contpc = xcp->cp0_epc;
247 /* In order not to confuse ptrace() et al, tweak context */
248 xcp->cp0_epc = emulpc - 4;
250 emulpc = xcp->cp0_epc;
251 contpc = xcp->cp0_epc + 4;
255 fpuemustats.emulated++;
256 switch (MIPSInst_OPCODE(ir)) {
258 u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
263 if (get_user(val, va)) {
264 fpuemustats.errors++;
267 DITOREG(val, MIPSInst_RT(ir));
272 u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
276 fpuemustats.stores++;
277 DIFROMREG(val, MIPSInst_RT(ir));
278 if (put_user(val, va)) {
279 fpuemustats.errors++;
286 u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
291 if (get_user(val, va)) {
292 fpuemustats.errors++;
295 SITOREG(val, MIPSInst_RT(ir));
300 u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
304 fpuemustats.stores++;
305 SIFROMREG(val, MIPSInst_RT(ir));
306 if (put_user(val, va)) {
307 fpuemustats.errors++;
314 switch (MIPSInst_RS(ir)) {
316 #if defined(__mips64)
318 /* copregister fs -> gpr[rt] */
319 if (MIPSInst_RT(ir) != 0) {
320 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
326 /* copregister fs <- rt */
327 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
332 /* copregister rd -> gpr[rt] */
333 if (MIPSInst_RT(ir) != 0) {
334 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
340 /* copregister rd <- rt */
341 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
345 /* cop control register rd -> gpr[rt] */
348 if (MIPSInst_RD(ir) == FPCREG_CSR) {
350 value = (value & ~0x3) | mips_rm[value & 0x3];
352 printk("%p gpr[%d]<-csr=%08x\n",
353 (void *) (xcp->cp0_epc),
354 MIPSInst_RT(ir), value);
357 else if (MIPSInst_RD(ir) == FPCREG_RID)
362 xcp->regs[MIPSInst_RT(ir)] = value;
367 /* copregister rd <- rt */
370 if (MIPSInst_RT(ir) == 0)
373 value = xcp->regs[MIPSInst_RT(ir)];
375 /* we only have one writable control reg
377 if (MIPSInst_RD(ir) == FPCREG_CSR) {
379 printk("%p gpr[%d]->csr=%08x\n",
380 (void *) (xcp->cp0_epc),
381 MIPSInst_RT(ir), value);
383 value &= (FPU_CSR_FLUSH | FPU_CSR_ALL_E | FPU_CSR_ALL_S | 0x03);
384 ctx->fcr31 &= ~(FPU_CSR_FLUSH | FPU_CSR_ALL_E | FPU_CSR_ALL_S | 0x03);
385 /* convert to ieee library modes */
386 ctx->fcr31 |= (value & ~0x3) | ieee_rm[value & 0x3];
388 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
397 if (xcp->cp0_cause & CAUSEF_BD)
401 cond = ctx->fcr31 & fpucondbit[MIPSInst_RT(ir) >> 2];
403 cond = ctx->fcr31 & FPU_CSR_COND;
405 switch (MIPSInst_RT(ir) & 3) {
416 /* thats an illegal instruction */
420 xcp->cp0_cause |= CAUSEF_BD;
422 /* branch taken: emulate dslot
426 contpc = (xcp->cp0_epc +
427 (MIPSInst_SIMM(ir) << 2));
430 (mips_instruction __user *) xcp->cp0_epc)) {
431 fpuemustats.errors++;
435 switch (MIPSInst_OPCODE(ir)) {
438 #if (__mips >= 2 || defined(__mips64))
443 #if __mips >= 4 && __mips != 32
446 /* its one of ours */
450 if (MIPSInst_FUNC(ir) == movc_op)
457 * Single step the non-cp1
458 * instruction in the dslot
460 return mips_dsemul(xcp, ir, contpc);
463 /* branch not taken */
466 * branch likely nullifies
472 * else continue & execute
473 * dslot as normal insn
481 if (!(MIPSInst_RS(ir) & 0x10))
486 /* a real fpu computation instruction */
487 if ((sig = fpu_emu(xcp, ctx, ir)))
493 #if __mips >= 4 && __mips != 32
497 if ((sig = fpux_emu(xcp, ctx, ir)))
505 if (MIPSInst_FUNC(ir) != movc_op)
507 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
508 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
509 xcp->regs[MIPSInst_RD(ir)] =
510 xcp->regs[MIPSInst_RS(ir)];
519 xcp->cp0_epc = contpc;
520 xcp->cp0_cause &= ~CAUSEF_BD;
526 * Conversion table from MIPS compare ops 48-63
527 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
529 static const unsigned char cmptab[8] = {
530 0, /* cmp_0 (sig) cmp_sf */
531 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
532 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
533 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
534 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
535 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
536 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
537 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
541 #if __mips >= 4 && __mips != 32
544 * Additional MIPS4 instructions
547 #define DEF3OP(name, p, f1, f2, f3) \
548 static ieee754##p fpemu_##p##_##name(ieee754##p r, ieee754##p s, \
551 struct _ieee754_csr ieee754_csr_save; \
553 ieee754_csr_save = ieee754_csr; \
555 ieee754_csr_save.cx |= ieee754_csr.cx; \
556 ieee754_csr_save.sx |= ieee754_csr.sx; \
558 ieee754_csr.cx |= ieee754_csr_save.cx; \
559 ieee754_csr.sx |= ieee754_csr_save.sx; \
563 static ieee754dp fpemu_dp_recip(ieee754dp d)
565 return ieee754dp_div(ieee754dp_one(0), d);
568 static ieee754dp fpemu_dp_rsqrt(ieee754dp d)
570 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
573 static ieee754sp fpemu_sp_recip(ieee754sp s)
575 return ieee754sp_div(ieee754sp_one(0), s);
578 static ieee754sp fpemu_sp_rsqrt(ieee754sp s)
580 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
583 DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
584 DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
585 DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
586 DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
587 DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
588 DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
589 DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
590 DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
592 static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
595 unsigned rcsr = 0; /* resulting csr */
597 fpuemustats.cp1xops++;
599 switch (MIPSInst_FMA_FFMT(ir)) {
602 ieee754sp(*handler) (ieee754sp, ieee754sp, ieee754sp);
603 ieee754sp fd, fr, fs, ft;
607 switch (MIPSInst_FUNC(ir)) {
609 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
610 xcp->regs[MIPSInst_FT(ir)]);
613 if (get_user(val, va)) {
614 fpuemustats.errors++;
617 SITOREG(val, MIPSInst_FD(ir));
621 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
622 xcp->regs[MIPSInst_FT(ir)]);
624 fpuemustats.stores++;
626 SIFROMREG(val, MIPSInst_FS(ir));
627 if (put_user(val, va)) {
628 fpuemustats.errors++;
634 handler = fpemu_sp_madd;
637 handler = fpemu_sp_msub;
640 handler = fpemu_sp_nmadd;
643 handler = fpemu_sp_nmsub;
647 SPFROMREG(fr, MIPSInst_FR(ir));
648 SPFROMREG(fs, MIPSInst_FS(ir));
649 SPFROMREG(ft, MIPSInst_FT(ir));
650 fd = (*handler) (fr, fs, ft);
651 SPTOREG(fd, MIPSInst_FD(ir));
654 if (ieee754_cxtest(IEEE754_INEXACT))
655 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
656 if (ieee754_cxtest(IEEE754_UNDERFLOW))
657 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
658 if (ieee754_cxtest(IEEE754_OVERFLOW))
659 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
660 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
661 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
663 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
664 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
665 /*printk ("SIGFPE: fpu csr = %08x\n",
679 ieee754dp(*handler) (ieee754dp, ieee754dp, ieee754dp);
680 ieee754dp fd, fr, fs, ft;
684 switch (MIPSInst_FUNC(ir)) {
686 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
687 xcp->regs[MIPSInst_FT(ir)]);
690 if (get_user(val, va)) {
691 fpuemustats.errors++;
694 DITOREG(val, MIPSInst_FD(ir));
698 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
699 xcp->regs[MIPSInst_FT(ir)]);
701 fpuemustats.stores++;
702 DIFROMREG(val, MIPSInst_FS(ir));
703 if (put_user(val, va)) {
704 fpuemustats.errors++;
710 handler = fpemu_dp_madd;
713 handler = fpemu_dp_msub;
716 handler = fpemu_dp_nmadd;
719 handler = fpemu_dp_nmsub;
723 DPFROMREG(fr, MIPSInst_FR(ir));
724 DPFROMREG(fs, MIPSInst_FS(ir));
725 DPFROMREG(ft, MIPSInst_FT(ir));
726 fd = (*handler) (fr, fs, ft);
727 DPTOREG(fd, MIPSInst_FD(ir));
737 if (MIPSInst_FUNC(ir) != pfetch_op) {
740 /* ignore prefx operation */
754 * Emulate a single COP1 arithmetic instruction.
756 static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
759 int rfmt; /* resulting format */
760 unsigned rcsr = 0; /* resulting csr */
769 } rv; /* resulting value */
771 fpuemustats.cp1ops++;
772 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
775 ieee754sp(*b) (ieee754sp, ieee754sp);
776 ieee754sp(*u) (ieee754sp);
779 switch (MIPSInst_FUNC(ir)) {
782 handler.b = ieee754sp_add;
785 handler.b = ieee754sp_sub;
788 handler.b = ieee754sp_mul;
791 handler.b = ieee754sp_div;
795 #if __mips >= 2 || defined(__mips64)
797 handler.u = ieee754sp_sqrt;
800 #if __mips >= 4 && __mips != 32
802 handler.u = fpemu_sp_rsqrt;
805 handler.u = fpemu_sp_recip;
810 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
811 if (((ctx->fcr31 & cond) != 0) !=
812 ((MIPSInst_FT(ir) & 1) != 0))
814 SPFROMREG(rv.s, MIPSInst_FS(ir));
817 if (xcp->regs[MIPSInst_FT(ir)] != 0)
819 SPFROMREG(rv.s, MIPSInst_FS(ir));
822 if (xcp->regs[MIPSInst_FT(ir)] == 0)
824 SPFROMREG(rv.s, MIPSInst_FS(ir));
828 handler.u = ieee754sp_abs;
831 handler.u = ieee754sp_neg;
835 SPFROMREG(rv.s, MIPSInst_FS(ir));
838 /* binary op on handler */
843 SPFROMREG(fs, MIPSInst_FS(ir));
844 SPFROMREG(ft, MIPSInst_FT(ir));
846 rv.s = (*handler.b) (fs, ft);
853 SPFROMREG(fs, MIPSInst_FS(ir));
854 rv.s = (*handler.u) (fs);
858 if (ieee754_cxtest(IEEE754_INEXACT))
859 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
860 if (ieee754_cxtest(IEEE754_UNDERFLOW))
861 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
862 if (ieee754_cxtest(IEEE754_OVERFLOW))
863 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
864 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE))
865 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
866 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
867 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
872 return SIGILL; /* not defined */
876 SPFROMREG(fs, MIPSInst_FS(ir));
877 rv.d = ieee754dp_fsp(fs);
884 SPFROMREG(fs, MIPSInst_FS(ir));
885 rv.w = ieee754sp_tint(fs);
890 #if __mips >= 2 || defined(__mips64)
895 unsigned int oldrm = ieee754_csr.rm;
898 SPFROMREG(fs, MIPSInst_FS(ir));
899 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
900 rv.w = ieee754sp_tint(fs);
901 ieee754_csr.rm = oldrm;
905 #endif /* __mips >= 2 */
907 #if defined(__mips64)
911 SPFROMREG(fs, MIPSInst_FS(ir));
912 rv.l = ieee754sp_tlong(fs);
921 unsigned int oldrm = ieee754_csr.rm;
924 SPFROMREG(fs, MIPSInst_FS(ir));
925 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
926 rv.l = ieee754sp_tlong(fs);
927 ieee754_csr.rm = oldrm;
931 #endif /* defined(__mips64) */
934 if (MIPSInst_FUNC(ir) >= fcmp_op) {
935 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
938 SPFROMREG(fs, MIPSInst_FS(ir));
939 SPFROMREG(ft, MIPSInst_FT(ir));
940 rv.w = ieee754sp_cmp(fs, ft,
941 cmptab[cmpop & 0x7], cmpop & 0x8);
943 if ((cmpop & 0x8) && ieee754_cxtest
944 (IEEE754_INVALID_OPERATION))
945 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
960 ieee754dp(*b) (ieee754dp, ieee754dp);
961 ieee754dp(*u) (ieee754dp);
964 switch (MIPSInst_FUNC(ir)) {
967 handler.b = ieee754dp_add;
970 handler.b = ieee754dp_sub;
973 handler.b = ieee754dp_mul;
976 handler.b = ieee754dp_div;
980 #if __mips >= 2 || defined(__mips64)
982 handler.u = ieee754dp_sqrt;
985 #if __mips >= 4 && __mips != 32
987 handler.u = fpemu_dp_rsqrt;
990 handler.u = fpemu_dp_recip;
995 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
996 if (((ctx->fcr31 & cond) != 0) !=
997 ((MIPSInst_FT(ir) & 1) != 0))
999 DPFROMREG(rv.d, MIPSInst_FS(ir));
1002 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1004 DPFROMREG(rv.d, MIPSInst_FS(ir));
1007 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1009 DPFROMREG(rv.d, MIPSInst_FS(ir));
1013 handler.u = ieee754dp_abs;
1017 handler.u = ieee754dp_neg;
1022 DPFROMREG(rv.d, MIPSInst_FS(ir));
1025 /* binary op on handler */
1029 DPFROMREG(fs, MIPSInst_FS(ir));
1030 DPFROMREG(ft, MIPSInst_FT(ir));
1032 rv.d = (*handler.b) (fs, ft);
1038 DPFROMREG(fs, MIPSInst_FS(ir));
1039 rv.d = (*handler.u) (fs);
1043 /* unary conv ops */
1047 DPFROMREG(fs, MIPSInst_FS(ir));
1048 rv.s = ieee754sp_fdp(fs);
1053 return SIGILL; /* not defined */
1058 DPFROMREG(fs, MIPSInst_FS(ir));
1059 rv.w = ieee754dp_tint(fs); /* wrong */
1064 #if __mips >= 2 || defined(__mips64)
1069 unsigned int oldrm = ieee754_csr.rm;
1072 DPFROMREG(fs, MIPSInst_FS(ir));
1073 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
1074 rv.w = ieee754dp_tint(fs);
1075 ieee754_csr.rm = oldrm;
1081 #if defined(__mips64)
1085 DPFROMREG(fs, MIPSInst_FS(ir));
1086 rv.l = ieee754dp_tlong(fs);
1095 unsigned int oldrm = ieee754_csr.rm;
1098 DPFROMREG(fs, MIPSInst_FS(ir));
1099 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
1100 rv.l = ieee754dp_tlong(fs);
1101 ieee754_csr.rm = oldrm;
1105 #endif /* __mips >= 3 */
1108 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1109 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1112 DPFROMREG(fs, MIPSInst_FS(ir));
1113 DPFROMREG(ft, MIPSInst_FT(ir));
1114 rv.w = ieee754dp_cmp(fs, ft,
1115 cmptab[cmpop & 0x7], cmpop & 0x8);
1120 (IEEE754_INVALID_OPERATION))
1121 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1137 switch (MIPSInst_FUNC(ir)) {
1139 /* convert word to single precision real */
1140 SPFROMREG(fs, MIPSInst_FS(ir));
1141 rv.s = ieee754sp_fint(fs.bits);
1145 /* convert word to double precision real */
1146 SPFROMREG(fs, MIPSInst_FS(ir));
1147 rv.d = ieee754dp_fint(fs.bits);
1156 #if defined(__mips64)
1158 switch (MIPSInst_FUNC(ir)) {
1160 /* convert long to single precision real */
1161 rv.s = ieee754sp_flong(ctx->fpr[MIPSInst_FS(ir)]);
1165 /* convert long to double precision real */
1166 rv.d = ieee754dp_flong(ctx->fpr[MIPSInst_FS(ir)]);
1181 * Update the fpu CSR register for this operation.
1182 * If an exception is required, generate a tidy SIGFPE exception,
1183 * without updating the result register.
1184 * Note: cause exception bits do not accumulate, they are rewritten
1185 * for each op; only the flag/sticky bits accumulate.
1187 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1188 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1189 /*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */
1194 * Now we can safely write the result back to the register file.
1199 cond = fpucondbit[MIPSInst_FD(ir) >> 2];
1201 cond = FPU_CSR_COND;
1206 ctx->fcr31 &= ~cond;
1210 DPTOREG(rv.d, MIPSInst_FD(ir));
1213 SPTOREG(rv.s, MIPSInst_FD(ir));
1216 SITOREG(rv.w, MIPSInst_FD(ir));
1218 #if defined(__mips64)
1220 DITOREG(rv.l, MIPSInst_FD(ir));
1230 int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1233 unsigned long oldepc, prevepc;
1234 mips_instruction insn;
1237 oldepc = xcp->cp0_epc;
1239 prevepc = xcp->cp0_epc;
1241 if (get_user(insn, (mips_instruction __user *) xcp->cp0_epc)) {
1242 fpuemustats.errors++;
1246 xcp->cp0_epc += 4; /* skip nops */
1249 * The 'ieee754_csr' is an alias of
1250 * ctx->fcr31. No need to copy ctx->fcr31 to
1251 * ieee754_csr. But ieee754_csr.rm is ieee
1252 * library modes. (not mips rounding mode)
1254 /* convert to ieee library modes */
1255 ieee754_csr.rm = ieee_rm[ieee754_csr.rm];
1256 sig = cop1Emulate(xcp, ctx);
1257 /* revert to mips rounding mode */
1258 ieee754_csr.rm = mips_rm[ieee754_csr.rm];
1267 } while (xcp->cp0_epc > prevepc);
1269 /* SIGILL indicates a non-fpu instruction */
1270 if (sig == SIGILL && xcp->cp0_epc != oldepc)
1271 /* but if epc has advanced, then ignore it */
1277 #ifdef CONFIG_DEBUG_FS
1278 extern struct dentry *mips_debugfs_dir;
1279 static int __init debugfs_fpuemu(void)
1281 struct dentry *d, *dir;
1286 } vars[] __initdata = {
1287 { "emulated", &fpuemustats.emulated },
1288 { "loads", &fpuemustats.loads },
1289 { "stores", &fpuemustats.stores },
1290 { "cp1ops", &fpuemustats.cp1ops },
1291 { "cp1xops", &fpuemustats.cp1xops },
1292 { "errors", &fpuemustats.errors },
1295 if (!mips_debugfs_dir)
1297 dir = debugfs_create_dir("fpuemustats", mips_debugfs_dir);
1300 for (i = 0; i < ARRAY_SIZE(vars); i++) {
1301 d = debugfs_create_u32(vars[i].name, S_IRUGO, dir, vars[i].v);
1307 __initcall(debugfs_fpuemu);