2 * cp1emu.c: a MIPS coprocessor 1 (fpu) instruction emulator
4 * MIPS floating point support
5 * Copyright (C) 1994-2000 Algorithmics Ltd. All rights reserved.
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/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>
53 /* Strap kernel emulator for full MIPS IV emulation */
60 /* Function which emulates a floating point instruction. */
62 static int fpu_emu(struct pt_regs *, struct mips_fpu_soft_struct *,
65 #if __mips >= 4 && __mips != 32
66 static int fpux_emu(struct pt_regs *,
67 struct mips_fpu_soft_struct *, mips_instruction);
70 /* Further private data for which no space exists in mips_fpu_soft_struct */
72 struct mips_fpu_emulator_private fpuemuprivate;
74 /* Control registers */
76 #define FPCREG_RID 0 /* $0 = revision id */
77 #define FPCREG_CSR 31 /* $31 = csr */
79 /* Convert Mips rounding mode (0..3) to IEEE library modes. */
80 static const unsigned char ieee_rm[4] = {
81 IEEE754_RN, IEEE754_RZ, IEEE754_RU, IEEE754_RD
85 /* convert condition code register number to csr bit */
86 static const unsigned int fpucondbit[8] = {
100 * Redundant with logic already in kernel/branch.c,
101 * embedded in compute_return_epc. At some point,
102 * a single subroutine should be used across both
105 static int isBranchInstr(mips_instruction * i)
107 switch (MIPSInst_OPCODE(*i)) {
109 switch (MIPSInst_FUNC(*i)) {
117 switch (MIPSInst_RT(*i)) {
147 if (MIPSInst_RS(*i) == bc_op)
156 * In the Linux kernel, we support selection of FPR format on the
157 * basis of the Status.FR bit. This does imply that, if a full 32
158 * FPRs are desired, there needs to be a flip-flop that can be written
159 * to one at that bit position. In any case, O32 MIPS ABI uses
160 * only the even FPRs (Status.FR = 0).
163 #define CP0_STATUS_FR_SUPPORT
165 #ifdef CP0_STATUS_FR_SUPPORT
166 #define FR_BIT ST0_FR
171 #define SIFROMREG(si,x) ((si) = \
172 (xcp->cp0_status & FR_BIT) || !(x & 1) ? \
174 (int)(ctx->fpr[x & ~1] >> 32 ))
175 #define SITOREG(si,x) (ctx->fpr[x & ~((xcp->cp0_status & FR_BIT) == 0)] = \
176 (xcp->cp0_status & FR_BIT) || !(x & 1) ? \
177 ctx->fpr[x & ~1] >> 32 << 32 | (u32)(si) : \
178 ctx->fpr[x & ~1] << 32 >> 32 | (u64)(si) << 32)
180 #define DIFROMREG(di,x) ((di) = \
181 ctx->fpr[x & ~((xcp->cp0_status & FR_BIT) == 0)])
182 #define DITOREG(di,x) (ctx->fpr[x & ~((xcp->cp0_status & FR_BIT) == 0)] \
185 #define SPFROMREG(sp,x) SIFROMREG((sp).bits,x)
186 #define SPTOREG(sp,x) SITOREG((sp).bits,x)
187 #define DPFROMREG(dp,x) DIFROMREG((dp).bits,x)
188 #define DPTOREG(dp,x) DITOREG((dp).bits,x)
191 * Emulate the single floating point instruction pointed at by EPC.
192 * Two instructions if the instruction is in a branch delay slot.
195 static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_soft_struct *ctx)
198 vaddr_t emulpc, contpc;
201 if (get_user(ir, (mips_instruction *) xcp->cp0_epc)) {
202 fpuemuprivate.stats.errors++;
206 /* XXX NEC Vr54xx bug workaround */
207 if ((xcp->cp0_cause & CAUSEF_BD) && !isBranchInstr(&ir))
208 xcp->cp0_cause &= ~CAUSEF_BD;
210 if (xcp->cp0_cause & CAUSEF_BD) {
212 * The instruction to be emulated is in a branch delay slot
213 * which means that we have to emulate the branch instruction
214 * BEFORE we do the cop1 instruction.
216 * This branch could be a COP1 branch, but in that case we
217 * would have had a trap for that instruction, and would not
218 * come through this route.
220 * Linux MIPS branch emulator operates on context, updating the
223 emulpc = REG_TO_VA(xcp->cp0_epc + 4); /* Snapshot emulation target */
225 if (__compute_return_epc(xcp)) {
227 printk("failed to emulate branch at %p\n",
228 REG_TO_VA(xcp->cp0_epc));
232 if (get_user(ir, (mips_instruction *) emulpc)) {
233 fpuemuprivate.stats.errors++;
236 /* __computer_return_epc() will have updated cp0_epc */
237 contpc = REG_TO_VA xcp->cp0_epc;
238 /* In order not to confuse ptrace() et al, tweak context */
239 xcp->cp0_epc = VA_TO_REG emulpc - 4;
242 emulpc = REG_TO_VA xcp->cp0_epc;
243 contpc = REG_TO_VA(xcp->cp0_epc + 4);
247 fpuemuprivate.stats.emulated++;
248 switch (MIPSInst_OPCODE(ir)) {
249 #ifndef SINGLE_ONLY_FPU
251 u64 *va = REG_TO_VA(xcp->regs[MIPSInst_RS(ir)] +
255 fpuemuprivate.stats.loads++;
256 if (get_user(val, va)) {
257 fpuemuprivate.stats.errors++;
260 DITOREG(val, MIPSInst_RT(ir));
265 u64 *va = REG_TO_VA(xcp->regs[MIPSInst_RS(ir)] +
269 fpuemuprivate.stats.stores++;
270 DIFROMREG(val, MIPSInst_RT(ir));
271 if (put_user(val, va)) {
272 fpuemuprivate.stats.errors++;
280 u32 *va = REG_TO_VA(xcp->regs[MIPSInst_RS(ir)] +
284 fpuemuprivate.stats.loads++;
285 if (get_user(val, va)) {
286 fpuemuprivate.stats.errors++;
289 #ifdef SINGLE_ONLY_FPU
290 if (MIPSInst_RT(ir) & 1) {
291 /* illegal register in single-float mode */
295 SITOREG(val, MIPSInst_RT(ir));
300 u32 *va = REG_TO_VA(xcp->regs[MIPSInst_RS(ir)] +
304 fpuemuprivate.stats.stores++;
305 #ifdef SINGLE_ONLY_FPU
306 if (MIPSInst_RT(ir) & 1) {
307 /* illegal register in single-float mode */
311 SIFROMREG(val, MIPSInst_RT(ir));
312 if (put_user(val, va)) {
313 fpuemuprivate.stats.errors++;
320 switch (MIPSInst_RS(ir)) {
322 #if __mips64 && !defined(SINGLE_ONLY_FPU)
324 /* copregister fs -> gpr[rt] */
325 if (MIPSInst_RT(ir) != 0) {
326 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
332 /* copregister fs <- rt */
333 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
338 /* copregister rd -> gpr[rt] */
339 #ifdef SINGLE_ONLY_FPU
340 if (MIPSInst_RD(ir) & 1) {
341 /* illegal register in single-float mode */
345 if (MIPSInst_RT(ir) != 0) {
346 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
352 /* copregister rd <- rt */
353 #ifdef SINGLE_ONLY_FPU
354 if (MIPSInst_RD(ir) & 1) {
355 /* illegal register in single-float mode */
359 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
363 /* cop control register rd -> gpr[rt] */
366 if (ir == CP1UNDEF) {
367 return do_dsemulret(xcp);
369 if (MIPSInst_RD(ir) == FPCREG_CSR) {
372 printk("%p gpr[%d]<-csr=%08x\n",
373 REG_TO_VA(xcp->cp0_epc),
374 MIPSInst_RT(ir), value);
377 else if (MIPSInst_RD(ir) == FPCREG_RID)
382 xcp->regs[MIPSInst_RT(ir)] = value;
387 /* copregister rd <- rt */
390 if (MIPSInst_RT(ir) == 0)
393 value = xcp->regs[MIPSInst_RT(ir)];
395 /* we only have one writable control reg
397 if (MIPSInst_RD(ir) == FPCREG_CSR) {
399 printk("%p gpr[%d]->csr=%08x\n",
400 REG_TO_VA(xcp->cp0_epc),
401 MIPSInst_RT(ir), value);
404 /* copy new rounding mode and
405 flush bit to ieee library state! */
406 ieee754_csr.nod = (ctx->fcr31 & 0x1000000) != 0;
407 ieee754_csr.rm = ieee_rm[value & 0x3];
409 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
418 if (xcp->cp0_cause & CAUSEF_BD)
422 cond = ctx->fcr31 & fpucondbit[MIPSInst_RT(ir) >> 2];
424 cond = ctx->fcr31 & FPU_CSR_COND;
426 switch (MIPSInst_RT(ir) & 3) {
437 /* thats an illegal instruction */
441 xcp->cp0_cause |= CAUSEF_BD;
443 /* branch taken: emulate dslot
449 (MIPSInst_SIMM(ir) << 2));
451 if (get_user(ir, (mips_instruction *)
452 REG_TO_VA xcp->cp0_epc)) {
453 fpuemuprivate.stats.errors++;
457 switch (MIPSInst_OPCODE(ir)) {
460 #if (__mips >= 2 || __mips64) && !defined(SINGLE_ONLY_FPU)
465 #if __mips >= 4 && __mips != 32
468 /* its one of ours */
472 if (MIPSInst_FUNC(ir) == movc_op)
479 * Single step the non-cp1
480 * instruction in the dslot
482 return mips_dsemul(xcp, ir, VA_TO_REG contpc);
485 /* branch not taken */
488 * branch likely nullifies
494 * else continue & execute
495 * dslot as normal insn
503 if (!(MIPSInst_RS(ir) & 0x10))
508 /* a real fpu computation instruction */
509 if ((sig = fpu_emu(xcp, ctx, ir)))
515 #if __mips >= 4 && __mips != 32
519 if ((sig = fpux_emu(xcp, ctx, ir)))
527 if (MIPSInst_FUNC(ir) != movc_op)
529 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
530 if (((ctx->fcr31 & cond) != 0) != ((MIPSInst_RT(ir) & 1) != 0))
532 xcp->regs[MIPSInst_RD(ir)] = xcp->regs[MIPSInst_RS(ir)];
541 xcp->cp0_epc = VA_TO_REG(contpc);
542 xcp->cp0_cause &= ~CAUSEF_BD;
547 * Conversion table from MIPS compare ops 48-63
548 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
550 static const unsigned char cmptab[8] = {
551 0, /* cmp_0 (sig) cmp_sf */
552 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
553 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
554 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
555 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
556 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
557 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
558 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
562 #if __mips >= 4 && __mips != 32
565 * Additional MIPS4 instructions
568 #define DEF3OP(name, p, f1, f2, f3) \
569 static ieee754##p fpemu_##p##_##name (ieee754##p r, ieee754##p s, \
572 struct ieee754_csr ieee754_csr_save; \
574 ieee754_csr_save = ieee754_csr; \
576 ieee754_csr_save.cx |= ieee754_csr.cx; \
577 ieee754_csr_save.sx |= ieee754_csr.sx; \
579 ieee754_csr.cx |= ieee754_csr_save.cx; \
580 ieee754_csr.sx |= ieee754_csr_save.sx; \
584 static ieee754dp fpemu_dp_recip(ieee754dp d)
586 return ieee754dp_div(ieee754dp_one(0), d);
589 static ieee754dp fpemu_dp_rsqrt(ieee754dp d)
591 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
594 static ieee754sp fpemu_sp_recip(ieee754sp s)
596 return ieee754sp_div(ieee754sp_one(0), s);
599 static ieee754sp fpemu_sp_rsqrt(ieee754sp s)
601 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
604 DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add,);
605 DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub,);
606 DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
607 DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
608 DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add,);
609 DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub,);
610 DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
611 DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
613 static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_soft_struct *ctx,
616 unsigned rcsr = 0; /* resulting csr */
618 fpuemuprivate.stats.cp1xops++;
620 switch (MIPSInst_FMA_FFMT(ir)) {
623 ieee754sp(*handler) (ieee754sp, ieee754sp, ieee754sp);
624 ieee754sp fd, fr, fs, ft;
628 switch (MIPSInst_FUNC(ir)) {
630 va = REG_TO_VA(xcp->regs[MIPSInst_FR(ir)] +
631 xcp->regs[MIPSInst_FT(ir)]);
633 fpuemuprivate.stats.loads++;
634 if (get_user(val, va)) {
635 fpuemuprivate.stats.errors++;
638 #ifdef SINGLE_ONLY_FPU
639 if (MIPSInst_FD(ir) & 1) {
640 /* illegal register in single-float
646 SITOREG(val, MIPSInst_FD(ir));
650 va = REG_TO_VA(xcp->regs[MIPSInst_FR(ir)] +
651 xcp->regs[MIPSInst_FT(ir)]);
653 fpuemuprivate.stats.stores++;
654 #ifdef SINGLE_ONLY_FPU
655 if (MIPSInst_FS(ir) & 1) {
656 /* illegal register in single-float
663 SIFROMREG(val, MIPSInst_FS(ir));
664 if (put_user(val, va)) {
665 fpuemuprivate.stats.errors++;
671 handler = fpemu_sp_madd;
674 handler = fpemu_sp_msub;
677 handler = fpemu_sp_nmadd;
680 handler = fpemu_sp_nmsub;
684 SPFROMREG(fr, MIPSInst_FR(ir));
685 SPFROMREG(fs, MIPSInst_FS(ir));
686 SPFROMREG(ft, MIPSInst_FT(ir));
687 fd = (*handler) (fr, fs, ft);
688 SPTOREG(fd, MIPSInst_FD(ir));
691 if (ieee754_cxtest(IEEE754_INEXACT))
692 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
693 if (ieee754_cxtest(IEEE754_UNDERFLOW))
694 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
695 if (ieee754_cxtest(IEEE754_OVERFLOW))
696 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
697 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
698 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
700 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
702 ctx->fcr31 |= 0x1000000;
703 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
704 /*printk ("SIGFPE: fpu csr = %08x\n",
717 #ifndef SINGLE_ONLY_FPU
719 ieee754dp(*handler) (ieee754dp, ieee754dp, ieee754dp);
720 ieee754dp fd, fr, fs, ft;
724 switch (MIPSInst_FUNC(ir)) {
726 va = REG_TO_VA(xcp->regs[MIPSInst_FR(ir)] +
727 xcp->regs[MIPSInst_FT(ir)]);
729 fpuemuprivate.stats.loads++;
730 if (get_user(val, va)) {
731 fpuemuprivate.stats.errors++;
734 DITOREG(val, MIPSInst_FD(ir));
738 va = REG_TO_VA(xcp->regs[MIPSInst_FR(ir)] +
739 xcp->regs[MIPSInst_FT(ir)]);
741 fpuemuprivate.stats.stores++;
742 DIFROMREG(val, MIPSInst_FS(ir));
743 if (put_user(val, va)) {
744 fpuemuprivate.stats.errors++;
750 handler = fpemu_dp_madd;
753 handler = fpemu_dp_msub;
756 handler = fpemu_dp_nmadd;
759 handler = fpemu_dp_nmsub;
763 DPFROMREG(fr, MIPSInst_FR(ir));
764 DPFROMREG(fs, MIPSInst_FS(ir));
765 DPFROMREG(ft, MIPSInst_FT(ir));
766 fd = (*handler) (fr, fs, ft);
767 DPTOREG(fd, MIPSInst_FD(ir));
778 if (MIPSInst_FUNC(ir) != pfetch_op) {
781 /* ignore prefx operation */
795 * Emulate a single COP1 arithmetic instruction.
797 static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_soft_struct *ctx,
800 int rfmt; /* resulting format */
801 unsigned rcsr = 0; /* resulting csr */
810 } rv; /* resulting value */
812 fpuemuprivate.stats.cp1ops++;
813 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
816 ieee754sp(*b) (ieee754sp, ieee754sp);
817 ieee754sp(*u) (ieee754sp);
820 switch (MIPSInst_FUNC(ir)) {
823 handler.b = ieee754sp_add;
826 handler.b = ieee754sp_sub;
829 handler.b = ieee754sp_mul;
832 handler.b = ieee754sp_div;
836 #if __mips >= 2 || __mips64
838 handler.u = ieee754sp_sqrt;
841 #if __mips >= 4 && __mips != 32
843 handler.u = fpemu_sp_rsqrt;
846 handler.u = fpemu_sp_recip;
851 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
852 if (((ctx->fcr31 & cond) != 0) !=
853 ((MIPSInst_FT(ir) & 1) != 0))
855 SPFROMREG(rv.s, MIPSInst_FS(ir));
858 if (xcp->regs[MIPSInst_FT(ir)] != 0)
860 SPFROMREG(rv.s, MIPSInst_FS(ir));
863 if (xcp->regs[MIPSInst_FT(ir)] == 0)
865 SPFROMREG(rv.s, MIPSInst_FS(ir));
869 handler.u = ieee754sp_abs;
872 handler.u = ieee754sp_neg;
876 SPFROMREG(rv.s, MIPSInst_FS(ir));
879 /* binary op on handler */
884 SPFROMREG(fs, MIPSInst_FS(ir));
885 SPFROMREG(ft, MIPSInst_FT(ir));
887 rv.s = (*handler.b) (fs, ft);
894 SPFROMREG(fs, MIPSInst_FS(ir));
895 rv.s = (*handler.u) (fs);
899 if (ieee754_cxtest(IEEE754_INEXACT))
900 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
901 if (ieee754_cxtest(IEEE754_UNDERFLOW))
902 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
903 if (ieee754_cxtest(IEEE754_OVERFLOW))
904 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
905 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE))
906 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
907 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
908 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
913 return SIGILL; /* not defined */
915 #ifdef SINGLE_ONLY_FPU
916 return SIGILL; /* not defined */
920 SPFROMREG(fs, MIPSInst_FS(ir));
921 rv.d = ieee754dp_fsp(fs);
929 SPFROMREG(fs, MIPSInst_FS(ir));
930 rv.w = ieee754sp_tint(fs);
935 #if __mips >= 2 || __mips64
940 unsigned int oldrm = ieee754_csr.rm;
943 SPFROMREG(fs, MIPSInst_FS(ir));
944 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
945 rv.w = ieee754sp_tint(fs);
946 ieee754_csr.rm = oldrm;
950 #endif /* __mips >= 2 */
952 #if __mips64 && !defined(SINGLE_ONLY_FPU)
956 SPFROMREG(fs, MIPSInst_FS(ir));
957 rv.l = ieee754sp_tlong(fs);
966 unsigned int oldrm = ieee754_csr.rm;
969 SPFROMREG(fs, MIPSInst_FS(ir));
970 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
971 rv.l = ieee754sp_tlong(fs);
972 ieee754_csr.rm = oldrm;
976 #endif /* __mips64 && !fpu(single) */
979 if (MIPSInst_FUNC(ir) >= fcmp_op) {
980 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
983 SPFROMREG(fs, MIPSInst_FS(ir));
984 SPFROMREG(ft, MIPSInst_FT(ir));
985 rv.w = ieee754sp_cmp(fs, ft,
986 cmptab[cmpop & 0x7], cmpop & 0x8);
988 if ((cmpop & 0x8) && ieee754_cxtest
989 (IEEE754_INVALID_OPERATION))
990 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1003 #ifndef SINGLE_ONLY_FPU
1006 ieee754dp(*b) (ieee754dp, ieee754dp);
1007 ieee754dp(*u) (ieee754dp);
1010 switch (MIPSInst_FUNC(ir)) {
1013 handler.b = ieee754dp_add;
1016 handler.b = ieee754dp_sub;
1019 handler.b = ieee754dp_mul;
1022 handler.b = ieee754dp_div;
1026 #if __mips >= 2 || __mips64
1028 handler.u = ieee754dp_sqrt;
1031 #if __mips >= 4 && __mips != 32
1033 handler.u = fpemu_dp_rsqrt;
1036 handler.u = fpemu_dp_recip;
1041 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1042 if (((ctx->fcr31 & cond) != 0) !=
1043 ((MIPSInst_FT(ir) & 1) != 0))
1045 DPFROMREG(rv.d, MIPSInst_FS(ir));
1048 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1050 DPFROMREG(rv.d, MIPSInst_FS(ir));
1053 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1055 DPFROMREG(rv.d, MIPSInst_FS(ir));
1059 handler.u = ieee754dp_abs;
1063 handler.u = ieee754dp_neg;
1068 DPFROMREG(rv.d, MIPSInst_FS(ir));
1071 /* binary op on handler */
1075 DPFROMREG(fs, MIPSInst_FS(ir));
1076 DPFROMREG(ft, MIPSInst_FT(ir));
1078 rv.d = (*handler.b) (fs, ft);
1084 DPFROMREG(fs, MIPSInst_FS(ir));
1085 rv.d = (*handler.u) (fs);
1089 /* unary conv ops */
1093 DPFROMREG(fs, MIPSInst_FS(ir));
1094 rv.s = ieee754sp_fdp(fs);
1099 return SIGILL; /* not defined */
1104 DPFROMREG(fs, MIPSInst_FS(ir));
1105 rv.w = ieee754dp_tint(fs); /* wrong */
1110 #if __mips >= 2 || __mips64
1115 unsigned int oldrm = ieee754_csr.rm;
1118 DPFROMREG(fs, MIPSInst_FS(ir));
1119 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
1120 rv.w = ieee754dp_tint(fs);
1121 ieee754_csr.rm = oldrm;
1127 #if __mips64 && !defined(SINGLE_ONLY_FPU)
1131 DPFROMREG(fs, MIPSInst_FS(ir));
1132 rv.l = ieee754dp_tlong(fs);
1141 unsigned int oldrm = ieee754_csr.rm;
1144 DPFROMREG(fs, MIPSInst_FS(ir));
1145 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
1146 rv.l = ieee754dp_tlong(fs);
1147 ieee754_csr.rm = oldrm;
1151 #endif /* __mips >= 3 && !fpu(single) */
1154 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1155 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1158 DPFROMREG(fs, MIPSInst_FS(ir));
1159 DPFROMREG(ft, MIPSInst_FT(ir));
1160 rv.w = ieee754dp_cmp(fs, ft,
1161 cmptab[cmpop & 0x7], cmpop & 0x8);
1166 (IEEE754_INVALID_OPERATION))
1167 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1179 #endif /* ifndef SINGLE_ONLY_FPU */
1184 switch (MIPSInst_FUNC(ir)) {
1186 /* convert word to single precision real */
1187 SPFROMREG(fs, MIPSInst_FS(ir));
1188 rv.s = ieee754sp_fint(fs.bits);
1191 #ifndef SINGLE_ONLY_FPU
1193 /* convert word to double precision real */
1194 SPFROMREG(fs, MIPSInst_FS(ir));
1195 rv.d = ieee754dp_fint(fs.bits);
1205 #if __mips64 && !defined(SINGLE_ONLY_FPU)
1207 switch (MIPSInst_FUNC(ir)) {
1209 /* convert long to single precision real */
1210 rv.s = ieee754sp_flong(ctx->fpr[MIPSInst_FS(ir)]);
1214 /* convert long to double precision real */
1215 rv.d = ieee754dp_flong(ctx->fpr[MIPSInst_FS(ir)]);
1230 * Update the fpu CSR register for this operation.
1231 * If an exception is required, generate a tidy SIGFPE exception,
1232 * without updating the result register.
1233 * Note: cause exception bits do not accumulate, they are rewritten
1234 * for each op; only the flag/sticky bits accumulate.
1236 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1237 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1238 /*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */
1243 * Now we can safely write the result back to the register file.
1248 cond = fpucondbit[MIPSInst_FD(ir) >> 2];
1250 cond = FPU_CSR_COND;
1255 ctx->fcr31 &= ~cond;
1258 #ifndef SINGLE_ONLY_FPU
1260 DPTOREG(rv.d, MIPSInst_FD(ir));
1264 SPTOREG(rv.s, MIPSInst_FD(ir));
1267 SITOREG(rv.w, MIPSInst_FD(ir));
1269 #if __mips64 && !defined(SINGLE_ONLY_FPU)
1271 DITOREG(rv.l, MIPSInst_FD(ir));
1281 int fpu_emulator_cop1Handler(int xcptno, struct pt_regs *xcp,
1282 struct mips_fpu_soft_struct *ctx)
1284 gpreg_t oldepc, prevepc;
1285 mips_instruction insn;
1288 oldepc = xcp->cp0_epc;
1290 prevepc = xcp->cp0_epc;
1292 if (get_user(insn, (mips_instruction *) xcp->cp0_epc)) {
1293 fpuemuprivate.stats.errors++;
1297 xcp->cp0_epc += 4; /* skip nops */
1299 /* Update ieee754_csr. Only relevant if we have a
1301 ieee754_csr.nod = (ctx->fcr31 & 0x1000000) != 0;
1302 ieee754_csr.rm = ieee_rm[ctx->fcr31 & 0x3];
1303 ieee754_csr.cx = (ctx->fcr31 >> 12) & 0x1f;
1304 sig = cop1Emulate(xcp, ctx);
1313 } while (xcp->cp0_epc > prevepc);
1315 /* SIGILL indicates a non-fpu instruction */
1316 if (sig == SIGILL && xcp->cp0_epc != oldepc)
1317 /* but if epc has advanced, then ignore it */