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 *) 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 *) 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)) {
259 #ifndef SINGLE_ONLY_FPU
261 u64 *va = (void *) (xcp->regs[MIPSInst_RS(ir)] +
266 if (get_user(val, va)) {
267 fpuemustats.errors++;
270 DITOREG(val, MIPSInst_RT(ir));
275 u64 *va = (void *) (xcp->regs[MIPSInst_RS(ir)] +
279 fpuemustats.stores++;
280 DIFROMREG(val, MIPSInst_RT(ir));
281 if (put_user(val, va)) {
282 fpuemustats.errors++;
290 u32 *va = (void *) (xcp->regs[MIPSInst_RS(ir)] +
295 if (get_user(val, va)) {
296 fpuemustats.errors++;
299 #ifdef SINGLE_ONLY_FPU
300 if (MIPSInst_RT(ir) & 1) {
301 /* illegal register in single-float mode */
305 SITOREG(val, MIPSInst_RT(ir));
310 u32 *va = (void *) (xcp->regs[MIPSInst_RS(ir)] +
314 fpuemustats.stores++;
315 #ifdef SINGLE_ONLY_FPU
316 if (MIPSInst_RT(ir) & 1) {
317 /* illegal register in single-float mode */
321 SIFROMREG(val, MIPSInst_RT(ir));
322 if (put_user(val, va)) {
323 fpuemustats.errors++;
330 switch (MIPSInst_RS(ir)) {
332 #if defined(__mips64) && !defined(SINGLE_ONLY_FPU)
334 /* copregister fs -> gpr[rt] */
335 if (MIPSInst_RT(ir) != 0) {
336 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
342 /* copregister fs <- rt */
343 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
348 /* copregister rd -> gpr[rt] */
349 #ifdef SINGLE_ONLY_FPU
350 if (MIPSInst_RD(ir) & 1) {
351 /* illegal register in single-float mode */
355 if (MIPSInst_RT(ir) != 0) {
356 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
362 /* copregister rd <- rt */
363 #ifdef SINGLE_ONLY_FPU
364 if (MIPSInst_RD(ir) & 1) {
365 /* illegal register in single-float mode */
369 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
373 /* cop control register rd -> gpr[rt] */
376 if (ir == CP1UNDEF) {
377 return do_dsemulret(xcp);
379 if (MIPSInst_RD(ir) == FPCREG_CSR) {
381 value = (value & ~0x3) | mips_rm[value & 0x3];
383 printk("%p gpr[%d]<-csr=%08x\n",
384 (void *) (xcp->cp0_epc),
385 MIPSInst_RT(ir), value);
388 else if (MIPSInst_RD(ir) == FPCREG_RID)
393 xcp->regs[MIPSInst_RT(ir)] = value;
398 /* copregister rd <- rt */
401 if (MIPSInst_RT(ir) == 0)
404 value = xcp->regs[MIPSInst_RT(ir)];
406 /* we only have one writable control reg
408 if (MIPSInst_RD(ir) == FPCREG_CSR) {
410 printk("%p gpr[%d]->csr=%08x\n",
411 (void *) (xcp->cp0_epc),
412 MIPSInst_RT(ir), value);
414 value &= (FPU_CSR_FLUSH | FPU_CSR_ALL_E | FPU_CSR_ALL_S | 0x03);
415 ctx->fcr31 &= ~(FPU_CSR_FLUSH | FPU_CSR_ALL_E | FPU_CSR_ALL_S | 0x03);
416 /* convert to ieee library modes */
417 ctx->fcr31 |= (value & ~0x3) | ieee_rm[value & 0x3];
419 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
428 if (xcp->cp0_cause & CAUSEF_BD)
432 cond = ctx->fcr31 & fpucondbit[MIPSInst_RT(ir) >> 2];
434 cond = ctx->fcr31 & FPU_CSR_COND;
436 switch (MIPSInst_RT(ir) & 3) {
447 /* thats an illegal instruction */
451 xcp->cp0_cause |= CAUSEF_BD;
453 /* branch taken: emulate dslot
459 (MIPSInst_SIMM(ir) << 2));
461 if (get_user(ir, (mips_instruction *)
462 (void *) xcp->cp0_epc)) {
463 fpuemustats.errors++;
467 switch (MIPSInst_OPCODE(ir)) {
470 #if (__mips >= 2 || defined(__mips64)) && !defined(SINGLE_ONLY_FPU)
475 #if __mips >= 4 && __mips != 32
478 /* its one of ours */
482 if (MIPSInst_FUNC(ir) == movc_op)
489 * Single step the non-cp1
490 * instruction in the dslot
492 return mips_dsemul(xcp, ir, (unsigned long) contpc);
495 /* branch not taken */
498 * branch likely nullifies
504 * else continue & execute
505 * dslot as normal insn
513 if (!(MIPSInst_RS(ir) & 0x10))
518 /* a real fpu computation instruction */
519 if ((sig = fpu_emu(xcp, ctx, ir)))
525 #if __mips >= 4 && __mips != 32
529 if ((sig = fpux_emu(xcp, ctx, ir)))
537 if (MIPSInst_FUNC(ir) != movc_op)
539 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
540 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
541 xcp->regs[MIPSInst_RD(ir)] =
542 xcp->regs[MIPSInst_RS(ir)];
551 xcp->cp0_epc = (unsigned long) contpc;
552 xcp->cp0_cause &= ~CAUSEF_BD;
558 * Conversion table from MIPS compare ops 48-63
559 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
561 static const unsigned char cmptab[8] = {
562 0, /* cmp_0 (sig) cmp_sf */
563 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
564 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
565 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
566 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
567 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
568 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
569 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
573 #if __mips >= 4 && __mips != 32
576 * Additional MIPS4 instructions
579 #define DEF3OP(name, p, f1, f2, f3) \
580 static ieee754##p fpemu_##p##_##name (ieee754##p r, ieee754##p s, \
583 struct _ieee754_csr ieee754_csr_save; \
585 ieee754_csr_save = ieee754_csr; \
587 ieee754_csr_save.cx |= ieee754_csr.cx; \
588 ieee754_csr_save.sx |= ieee754_csr.sx; \
590 ieee754_csr.cx |= ieee754_csr_save.cx; \
591 ieee754_csr.sx |= ieee754_csr_save.sx; \
595 static ieee754dp fpemu_dp_recip(ieee754dp d)
597 return ieee754dp_div(ieee754dp_one(0), d);
600 static ieee754dp fpemu_dp_rsqrt(ieee754dp d)
602 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
605 static ieee754sp fpemu_sp_recip(ieee754sp s)
607 return ieee754sp_div(ieee754sp_one(0), s);
610 static ieee754sp fpemu_sp_rsqrt(ieee754sp s)
612 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
615 DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add,);
616 DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub,);
617 DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
618 DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
619 DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add,);
620 DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub,);
621 DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
622 DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
624 static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_soft_struct *ctx,
627 unsigned rcsr = 0; /* resulting csr */
629 fpuemustats.cp1xops++;
631 switch (MIPSInst_FMA_FFMT(ir)) {
634 ieee754sp(*handler) (ieee754sp, ieee754sp, ieee754sp);
635 ieee754sp fd, fr, fs, ft;
639 switch (MIPSInst_FUNC(ir)) {
641 va = (void *) (xcp->regs[MIPSInst_FR(ir)] +
642 xcp->regs[MIPSInst_FT(ir)]);
645 if (get_user(val, va)) {
646 fpuemustats.errors++;
649 #ifdef SINGLE_ONLY_FPU
650 if (MIPSInst_FD(ir) & 1) {
651 /* illegal register in single-float
657 SITOREG(val, MIPSInst_FD(ir));
661 va = (void *) (xcp->regs[MIPSInst_FR(ir)] +
662 xcp->regs[MIPSInst_FT(ir)]);
664 fpuemustats.stores++;
665 #ifdef SINGLE_ONLY_FPU
666 if (MIPSInst_FS(ir) & 1) {
667 /* illegal register in single-float
674 SIFROMREG(val, MIPSInst_FS(ir));
675 if (put_user(val, va)) {
676 fpuemustats.errors++;
682 handler = fpemu_sp_madd;
685 handler = fpemu_sp_msub;
688 handler = fpemu_sp_nmadd;
691 handler = fpemu_sp_nmsub;
695 SPFROMREG(fr, MIPSInst_FR(ir));
696 SPFROMREG(fs, MIPSInst_FS(ir));
697 SPFROMREG(ft, MIPSInst_FT(ir));
698 fd = (*handler) (fr, fs, ft);
699 SPTOREG(fd, MIPSInst_FD(ir));
702 if (ieee754_cxtest(IEEE754_INEXACT))
703 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
704 if (ieee754_cxtest(IEEE754_UNDERFLOW))
705 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
706 if (ieee754_cxtest(IEEE754_OVERFLOW))
707 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
708 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
709 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
711 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
712 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
713 /*printk ("SIGFPE: fpu csr = %08x\n",
726 #ifndef SINGLE_ONLY_FPU
728 ieee754dp(*handler) (ieee754dp, ieee754dp, ieee754dp);
729 ieee754dp fd, fr, fs, ft;
733 switch (MIPSInst_FUNC(ir)) {
735 va = (void *) (xcp->regs[MIPSInst_FR(ir)] +
736 xcp->regs[MIPSInst_FT(ir)]);
739 if (get_user(val, va)) {
740 fpuemustats.errors++;
743 DITOREG(val, MIPSInst_FD(ir));
747 va = (void *) (xcp->regs[MIPSInst_FR(ir)] +
748 xcp->regs[MIPSInst_FT(ir)]);
750 fpuemustats.stores++;
751 DIFROMREG(val, MIPSInst_FS(ir));
752 if (put_user(val, va)) {
753 fpuemustats.errors++;
759 handler = fpemu_dp_madd;
762 handler = fpemu_dp_msub;
765 handler = fpemu_dp_nmadd;
768 handler = fpemu_dp_nmsub;
772 DPFROMREG(fr, MIPSInst_FR(ir));
773 DPFROMREG(fs, MIPSInst_FS(ir));
774 DPFROMREG(ft, MIPSInst_FT(ir));
775 fd = (*handler) (fr, fs, ft);
776 DPTOREG(fd, MIPSInst_FD(ir));
787 if (MIPSInst_FUNC(ir) != pfetch_op) {
790 /* ignore prefx operation */
804 * Emulate a single COP1 arithmetic instruction.
806 static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_soft_struct *ctx,
809 int rfmt; /* resulting format */
810 unsigned rcsr = 0; /* resulting csr */
819 } rv; /* resulting value */
821 fpuemustats.cp1ops++;
822 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
825 ieee754sp(*b) (ieee754sp, ieee754sp);
826 ieee754sp(*u) (ieee754sp);
829 switch (MIPSInst_FUNC(ir)) {
832 handler.b = ieee754sp_add;
835 handler.b = ieee754sp_sub;
838 handler.b = ieee754sp_mul;
841 handler.b = ieee754sp_div;
845 #if __mips >= 2 || defined(__mips64)
847 handler.u = ieee754sp_sqrt;
850 #if __mips >= 4 && __mips != 32
852 handler.u = fpemu_sp_rsqrt;
855 handler.u = fpemu_sp_recip;
860 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
861 if (((ctx->fcr31 & cond) != 0) !=
862 ((MIPSInst_FT(ir) & 1) != 0))
864 SPFROMREG(rv.s, MIPSInst_FS(ir));
867 if (xcp->regs[MIPSInst_FT(ir)] != 0)
869 SPFROMREG(rv.s, MIPSInst_FS(ir));
872 if (xcp->regs[MIPSInst_FT(ir)] == 0)
874 SPFROMREG(rv.s, MIPSInst_FS(ir));
878 handler.u = ieee754sp_abs;
881 handler.u = ieee754sp_neg;
885 SPFROMREG(rv.s, MIPSInst_FS(ir));
888 /* binary op on handler */
893 SPFROMREG(fs, MIPSInst_FS(ir));
894 SPFROMREG(ft, MIPSInst_FT(ir));
896 rv.s = (*handler.b) (fs, ft);
903 SPFROMREG(fs, MIPSInst_FS(ir));
904 rv.s = (*handler.u) (fs);
908 if (ieee754_cxtest(IEEE754_INEXACT))
909 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
910 if (ieee754_cxtest(IEEE754_UNDERFLOW))
911 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
912 if (ieee754_cxtest(IEEE754_OVERFLOW))
913 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
914 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE))
915 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
916 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
917 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
922 return SIGILL; /* not defined */
924 #ifdef SINGLE_ONLY_FPU
925 return SIGILL; /* not defined */
929 SPFROMREG(fs, MIPSInst_FS(ir));
930 rv.d = ieee754dp_fsp(fs);
938 SPFROMREG(fs, MIPSInst_FS(ir));
939 rv.w = ieee754sp_tint(fs);
944 #if __mips >= 2 || defined(__mips64)
949 unsigned int oldrm = ieee754_csr.rm;
952 SPFROMREG(fs, MIPSInst_FS(ir));
953 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
954 rv.w = ieee754sp_tint(fs);
955 ieee754_csr.rm = oldrm;
959 #endif /* __mips >= 2 */
961 #if defined(__mips64) && !defined(SINGLE_ONLY_FPU)
965 SPFROMREG(fs, MIPSInst_FS(ir));
966 rv.l = ieee754sp_tlong(fs);
975 unsigned int oldrm = ieee754_csr.rm;
978 SPFROMREG(fs, MIPSInst_FS(ir));
979 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
980 rv.l = ieee754sp_tlong(fs);
981 ieee754_csr.rm = oldrm;
985 #endif /* defined(__mips64) && !fpu(single) */
988 if (MIPSInst_FUNC(ir) >= fcmp_op) {
989 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
992 SPFROMREG(fs, MIPSInst_FS(ir));
993 SPFROMREG(ft, MIPSInst_FT(ir));
994 rv.w = ieee754sp_cmp(fs, ft,
995 cmptab[cmpop & 0x7], cmpop & 0x8);
997 if ((cmpop & 0x8) && ieee754_cxtest
998 (IEEE754_INVALID_OPERATION))
999 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1012 #ifndef SINGLE_ONLY_FPU
1015 ieee754dp(*b) (ieee754dp, ieee754dp);
1016 ieee754dp(*u) (ieee754dp);
1019 switch (MIPSInst_FUNC(ir)) {
1022 handler.b = ieee754dp_add;
1025 handler.b = ieee754dp_sub;
1028 handler.b = ieee754dp_mul;
1031 handler.b = ieee754dp_div;
1035 #if __mips >= 2 || defined(__mips64)
1037 handler.u = ieee754dp_sqrt;
1040 #if __mips >= 4 && __mips != 32
1042 handler.u = fpemu_dp_rsqrt;
1045 handler.u = fpemu_dp_recip;
1050 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1051 if (((ctx->fcr31 & cond) != 0) !=
1052 ((MIPSInst_FT(ir) & 1) != 0))
1054 DPFROMREG(rv.d, MIPSInst_FS(ir));
1057 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1059 DPFROMREG(rv.d, MIPSInst_FS(ir));
1062 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1064 DPFROMREG(rv.d, MIPSInst_FS(ir));
1068 handler.u = ieee754dp_abs;
1072 handler.u = ieee754dp_neg;
1077 DPFROMREG(rv.d, MIPSInst_FS(ir));
1080 /* binary op on handler */
1084 DPFROMREG(fs, MIPSInst_FS(ir));
1085 DPFROMREG(ft, MIPSInst_FT(ir));
1087 rv.d = (*handler.b) (fs, ft);
1093 DPFROMREG(fs, MIPSInst_FS(ir));
1094 rv.d = (*handler.u) (fs);
1098 /* unary conv ops */
1102 DPFROMREG(fs, MIPSInst_FS(ir));
1103 rv.s = ieee754sp_fdp(fs);
1108 return SIGILL; /* not defined */
1113 DPFROMREG(fs, MIPSInst_FS(ir));
1114 rv.w = ieee754dp_tint(fs); /* wrong */
1119 #if __mips >= 2 || defined(__mips64)
1124 unsigned int oldrm = ieee754_csr.rm;
1127 DPFROMREG(fs, MIPSInst_FS(ir));
1128 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
1129 rv.w = ieee754dp_tint(fs);
1130 ieee754_csr.rm = oldrm;
1136 #if defined(__mips64) && !defined(SINGLE_ONLY_FPU)
1140 DPFROMREG(fs, MIPSInst_FS(ir));
1141 rv.l = ieee754dp_tlong(fs);
1150 unsigned int oldrm = ieee754_csr.rm;
1153 DPFROMREG(fs, MIPSInst_FS(ir));
1154 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
1155 rv.l = ieee754dp_tlong(fs);
1156 ieee754_csr.rm = oldrm;
1160 #endif /* __mips >= 3 && !fpu(single) */
1163 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1164 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1167 DPFROMREG(fs, MIPSInst_FS(ir));
1168 DPFROMREG(ft, MIPSInst_FT(ir));
1169 rv.w = ieee754dp_cmp(fs, ft,
1170 cmptab[cmpop & 0x7], cmpop & 0x8);
1175 (IEEE754_INVALID_OPERATION))
1176 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1188 #endif /* ifndef SINGLE_ONLY_FPU */
1193 switch (MIPSInst_FUNC(ir)) {
1195 /* convert word to single precision real */
1196 SPFROMREG(fs, MIPSInst_FS(ir));
1197 rv.s = ieee754sp_fint(fs.bits);
1200 #ifndef SINGLE_ONLY_FPU
1202 /* convert word to double precision real */
1203 SPFROMREG(fs, MIPSInst_FS(ir));
1204 rv.d = ieee754dp_fint(fs.bits);
1214 #if defined(__mips64) && !defined(SINGLE_ONLY_FPU)
1216 switch (MIPSInst_FUNC(ir)) {
1218 /* convert long to single precision real */
1219 rv.s = ieee754sp_flong(ctx->fpr[MIPSInst_FS(ir)]);
1223 /* convert long to double precision real */
1224 rv.d = ieee754dp_flong(ctx->fpr[MIPSInst_FS(ir)]);
1239 * Update the fpu CSR register for this operation.
1240 * If an exception is required, generate a tidy SIGFPE exception,
1241 * without updating the result register.
1242 * Note: cause exception bits do not accumulate, they are rewritten
1243 * for each op; only the flag/sticky bits accumulate.
1245 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1246 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1247 /*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */
1252 * Now we can safely write the result back to the register file.
1257 cond = fpucondbit[MIPSInst_FD(ir) >> 2];
1259 cond = FPU_CSR_COND;
1264 ctx->fcr31 &= ~cond;
1267 #ifndef SINGLE_ONLY_FPU
1269 DPTOREG(rv.d, MIPSInst_FD(ir));
1273 SPTOREG(rv.s, MIPSInst_FD(ir));
1276 SITOREG(rv.w, MIPSInst_FD(ir));
1278 #if defined(__mips64) && !defined(SINGLE_ONLY_FPU)
1280 DITOREG(rv.l, MIPSInst_FD(ir));
1290 int fpu_emulator_cop1Handler(struct pt_regs *xcp,
1291 struct mips_fpu_soft_struct *ctx)
1293 unsigned long oldepc, prevepc;
1294 mips_instruction insn;
1297 oldepc = xcp->cp0_epc;
1299 prevepc = xcp->cp0_epc;
1301 if (get_user(insn, (mips_instruction *) xcp->cp0_epc)) {
1302 fpuemustats.errors++;
1306 xcp->cp0_epc += 4; /* skip nops */
1309 * The 'ieee754_csr' is an alias of
1310 * ctx->fcr31. No need to copy ctx->fcr31 to
1311 * ieee754_csr. But ieee754_csr.rm is ieee
1312 * library modes. (not mips rounding mode)
1314 /* convert to ieee library modes */
1315 ieee754_csr.rm = ieee_rm[ieee754_csr.rm];
1316 sig = cop1Emulate(xcp, ctx);
1317 /* revert to mips rounding mode */
1318 ieee754_csr.rm = mips_rm[ieee754_csr.rm];
1327 } while (xcp->cp0_epc > prevepc);
1329 /* SIGILL indicates a non-fpu instruction */
1330 if (sig == SIGILL && xcp->cp0_epc != oldepc)
1331 /* but if epc has advanced, then ignore it */