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 * 59 Temple Place - Suite 330, Boston MA 02111-1307, 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/module.h>
38 #include <linux/debugfs.h>
39 #include <linux/perf_event.h>
42 #include <asm/bootinfo.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>
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_struct *,
65 #if __mips >= 4 && __mips != 32
66 static int fpux_emu(struct pt_regs *,
67 struct mips_fpu_struct *, mips_instruction, void *__user *);
70 /* Further private data for which no space exists in mips_fpu_struct */
72 #ifdef CONFIG_DEBUG_FS
73 DEFINE_PER_CPU(struct mips_fpu_emulator_stats, fpuemustats);
76 /* Control registers */
78 #define FPCREG_RID 0 /* $0 = revision id */
79 #define FPCREG_CSR 31 /* $31 = csr */
81 /* Determine rounding mode from the RM bits of the FCSR */
82 #define modeindex(v) ((v) & FPU_CSR_RM)
84 /* Convert Mips rounding mode (0..3) to IEEE library modes. */
85 static const unsigned char ieee_rm[4] = {
86 [FPU_CSR_RN] = IEEE754_RN,
87 [FPU_CSR_RZ] = IEEE754_RZ,
88 [FPU_CSR_RU] = IEEE754_RU,
89 [FPU_CSR_RD] = IEEE754_RD,
91 /* Convert IEEE library modes to Mips rounding mode (0..3). */
92 static const unsigned char mips_rm[4] = {
93 [IEEE754_RN] = FPU_CSR_RN,
94 [IEEE754_RZ] = FPU_CSR_RZ,
95 [IEEE754_RD] = FPU_CSR_RD,
96 [IEEE754_RU] = FPU_CSR_RU,
100 /* convert condition code register number to csr bit */
101 static const unsigned int fpucondbit[8] = {
115 * Redundant with logic already in kernel/branch.c,
116 * embedded in compute_return_epc. At some point,
117 * a single subroutine should be used across both
120 static int isBranchInstr(mips_instruction * i)
122 switch (MIPSInst_OPCODE(*i)) {
124 switch (MIPSInst_FUNC(*i)) {
132 switch (MIPSInst_RT(*i)) {
162 if (MIPSInst_RS(*i) == bc_op)
171 * In the Linux kernel, we support selection of FPR format on the
172 * basis of the Status.FR bit. If an FPU is not present, the FR bit
173 * is hardwired to zero, which would imply a 32-bit FPU even for
174 * 64-bit CPUs. For 64-bit kernels with no FPU we use TIF_32BIT_REGS
175 * as a proxy for the FR bit so that a 64-bit FPU is emulated. In any
176 * case, for a 32-bit kernel which uses the O32 MIPS ABI, only the
177 * even FPRs are used (Status.FR = 0).
179 static inline int cop1_64bit(struct pt_regs *xcp)
182 return xcp->cp0_status & ST0_FR;
184 return !test_thread_flag(TIF_32BIT_REGS);
190 #define SIFROMREG(si, x) ((si) = cop1_64bit(xcp) || !(x & 1) ? \
191 (int)ctx->fpr[x] : (int)(ctx->fpr[x & ~1] >> 32))
193 #define SITOREG(si, x) (ctx->fpr[x & ~(cop1_64bit(xcp) == 0)] = \
194 cop1_64bit(xcp) || !(x & 1) ? \
195 ctx->fpr[x & ~1] >> 32 << 32 | (u32)(si) : \
196 ctx->fpr[x & ~1] << 32 >> 32 | (u64)(si) << 32)
198 #define DIFROMREG(di, x) ((di) = ctx->fpr[x & ~(cop1_64bit(xcp) == 0)])
199 #define DITOREG(di, x) (ctx->fpr[x & ~(cop1_64bit(xcp) == 0)] = (di))
201 #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
202 #define SPTOREG(sp, x) SITOREG((sp).bits, x)
203 #define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
204 #define DPTOREG(dp, x) DITOREG((dp).bits, x)
207 * Emulate the single floating point instruction pointed at by EPC.
208 * Two instructions if the instruction is in a branch delay slot.
211 static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
212 void *__user *fault_addr)
215 unsigned long emulpc, contpc;
218 if (!access_ok(VERIFY_READ, xcp->cp0_epc, sizeof(mips_instruction))) {
219 MIPS_FPU_EMU_INC_STATS(errors);
220 *fault_addr = (mips_instruction __user *)xcp->cp0_epc;
223 if (__get_user(ir, (mips_instruction __user *) xcp->cp0_epc)) {
224 MIPS_FPU_EMU_INC_STATS(errors);
225 *fault_addr = (mips_instruction __user *)xcp->cp0_epc;
229 /* XXX NEC Vr54xx bug workaround */
230 if ((xcp->cp0_cause & CAUSEF_BD) && !isBranchInstr(&ir))
231 xcp->cp0_cause &= ~CAUSEF_BD;
233 if (xcp->cp0_cause & CAUSEF_BD) {
235 * The instruction to be emulated is in a branch delay slot
236 * which means that we have to emulate the branch instruction
237 * BEFORE we do the cop1 instruction.
239 * This branch could be a COP1 branch, but in that case we
240 * would have had a trap for that instruction, and would not
241 * come through this route.
243 * Linux MIPS branch emulator operates on context, updating the
246 emulpc = xcp->cp0_epc + 4; /* Snapshot emulation target */
248 if (__compute_return_epc(xcp)) {
250 printk("failed to emulate branch at %p\n",
251 (void *) (xcp->cp0_epc));
255 if (!access_ok(VERIFY_READ, emulpc, sizeof(mips_instruction))) {
256 MIPS_FPU_EMU_INC_STATS(errors);
257 *fault_addr = (mips_instruction __user *)emulpc;
260 if (__get_user(ir, (mips_instruction __user *) emulpc)) {
261 MIPS_FPU_EMU_INC_STATS(errors);
262 *fault_addr = (mips_instruction __user *)emulpc;
265 /* __compute_return_epc() will have updated cp0_epc */
266 contpc = xcp->cp0_epc;
267 /* In order not to confuse ptrace() et al, tweak context */
268 xcp->cp0_epc = emulpc - 4;
270 emulpc = xcp->cp0_epc;
271 contpc = xcp->cp0_epc + 4;
275 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
277 MIPS_FPU_EMU_INC_STATS(emulated);
278 switch (MIPSInst_OPCODE(ir)) {
280 u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
284 MIPS_FPU_EMU_INC_STATS(loads);
286 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
287 MIPS_FPU_EMU_INC_STATS(errors);
291 if (__get_user(val, va)) {
292 MIPS_FPU_EMU_INC_STATS(errors);
296 DITOREG(val, MIPSInst_RT(ir));
301 u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
305 MIPS_FPU_EMU_INC_STATS(stores);
306 DIFROMREG(val, MIPSInst_RT(ir));
307 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
308 MIPS_FPU_EMU_INC_STATS(errors);
312 if (__put_user(val, va)) {
313 MIPS_FPU_EMU_INC_STATS(errors);
321 u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
325 MIPS_FPU_EMU_INC_STATS(loads);
326 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
327 MIPS_FPU_EMU_INC_STATS(errors);
331 if (__get_user(val, va)) {
332 MIPS_FPU_EMU_INC_STATS(errors);
336 SITOREG(val, MIPSInst_RT(ir));
341 u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
345 MIPS_FPU_EMU_INC_STATS(stores);
346 SIFROMREG(val, MIPSInst_RT(ir));
347 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
348 MIPS_FPU_EMU_INC_STATS(errors);
352 if (__put_user(val, va)) {
353 MIPS_FPU_EMU_INC_STATS(errors);
361 switch (MIPSInst_RS(ir)) {
363 #if defined(__mips64)
365 /* copregister fs -> gpr[rt] */
366 if (MIPSInst_RT(ir) != 0) {
367 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
373 /* copregister fs <- rt */
374 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
379 /* copregister rd -> gpr[rt] */
380 if (MIPSInst_RT(ir) != 0) {
381 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
387 /* copregister rd <- rt */
388 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
392 /* cop control register rd -> gpr[rt] */
395 if (MIPSInst_RD(ir) == FPCREG_CSR) {
397 value = (value & ~FPU_CSR_RM) |
398 mips_rm[modeindex(value)];
400 printk("%p gpr[%d]<-csr=%08x\n",
401 (void *) (xcp->cp0_epc),
402 MIPSInst_RT(ir), value);
405 else if (MIPSInst_RD(ir) == FPCREG_RID)
410 xcp->regs[MIPSInst_RT(ir)] = value;
415 /* copregister rd <- rt */
418 if (MIPSInst_RT(ir) == 0)
421 value = xcp->regs[MIPSInst_RT(ir)];
423 /* we only have one writable control reg
425 if (MIPSInst_RD(ir) == FPCREG_CSR) {
427 printk("%p gpr[%d]->csr=%08x\n",
428 (void *) (xcp->cp0_epc),
429 MIPSInst_RT(ir), value);
433 * Don't write reserved bits,
434 * and convert to ieee library modes
436 ctx->fcr31 = (value &
437 ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
438 ieee_rm[modeindex(value)];
440 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
449 if (xcp->cp0_cause & CAUSEF_BD)
453 cond = ctx->fcr31 & fpucondbit[MIPSInst_RT(ir) >> 2];
455 cond = ctx->fcr31 & FPU_CSR_COND;
457 switch (MIPSInst_RT(ir) & 3) {
468 /* thats an illegal instruction */
472 xcp->cp0_cause |= CAUSEF_BD;
474 /* branch taken: emulate dslot
478 contpc = (xcp->cp0_epc +
479 (MIPSInst_SIMM(ir) << 2));
481 if (!access_ok(VERIFY_READ, xcp->cp0_epc,
482 sizeof(mips_instruction))) {
483 MIPS_FPU_EMU_INC_STATS(errors);
484 *fault_addr = (mips_instruction __user *)xcp->cp0_epc;
488 (mips_instruction __user *) xcp->cp0_epc)) {
489 MIPS_FPU_EMU_INC_STATS(errors);
490 *fault_addr = (mips_instruction __user *)xcp->cp0_epc;
494 switch (MIPSInst_OPCODE(ir)) {
497 #if (__mips >= 2 || defined(__mips64))
502 #if __mips >= 4 && __mips != 32
505 /* its one of ours */
509 if (MIPSInst_FUNC(ir) == movc_op)
516 * Single step the non-cp1
517 * instruction in the dslot
519 return mips_dsemul(xcp, ir, contpc);
522 /* branch not taken */
525 * branch likely nullifies
531 * else continue & execute
532 * dslot as normal insn
540 if (!(MIPSInst_RS(ir) & 0x10))
545 /* a real fpu computation instruction */
546 if ((sig = fpu_emu(xcp, ctx, ir)))
552 #if __mips >= 4 && __mips != 32
554 int sig = fpux_emu(xcp, ctx, ir, fault_addr);
563 if (MIPSInst_FUNC(ir) != movc_op)
565 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
566 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
567 xcp->regs[MIPSInst_RD(ir)] =
568 xcp->regs[MIPSInst_RS(ir)];
577 xcp->cp0_epc = contpc;
578 xcp->cp0_cause &= ~CAUSEF_BD;
584 * Conversion table from MIPS compare ops 48-63
585 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
587 static const unsigned char cmptab[8] = {
588 0, /* cmp_0 (sig) cmp_sf */
589 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
590 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
591 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
592 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
593 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
594 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
595 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
599 #if __mips >= 4 && __mips != 32
602 * Additional MIPS4 instructions
605 #define DEF3OP(name, p, f1, f2, f3) \
606 static ieee754##p fpemu_##p##_##name(ieee754##p r, ieee754##p s, \
609 struct _ieee754_csr ieee754_csr_save; \
611 ieee754_csr_save = ieee754_csr; \
613 ieee754_csr_save.cx |= ieee754_csr.cx; \
614 ieee754_csr_save.sx |= ieee754_csr.sx; \
616 ieee754_csr.cx |= ieee754_csr_save.cx; \
617 ieee754_csr.sx |= ieee754_csr_save.sx; \
621 static ieee754dp fpemu_dp_recip(ieee754dp d)
623 return ieee754dp_div(ieee754dp_one(0), d);
626 static ieee754dp fpemu_dp_rsqrt(ieee754dp d)
628 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
631 static ieee754sp fpemu_sp_recip(ieee754sp s)
633 return ieee754sp_div(ieee754sp_one(0), s);
636 static ieee754sp fpemu_sp_rsqrt(ieee754sp s)
638 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
641 DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
642 DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
643 DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
644 DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
645 DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
646 DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
647 DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
648 DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
650 static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
651 mips_instruction ir, void *__user *fault_addr)
653 unsigned rcsr = 0; /* resulting csr */
655 MIPS_FPU_EMU_INC_STATS(cp1xops);
657 switch (MIPSInst_FMA_FFMT(ir)) {
660 ieee754sp(*handler) (ieee754sp, ieee754sp, ieee754sp);
661 ieee754sp fd, fr, fs, ft;
665 switch (MIPSInst_FUNC(ir)) {
667 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
668 xcp->regs[MIPSInst_FT(ir)]);
670 MIPS_FPU_EMU_INC_STATS(loads);
671 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
672 MIPS_FPU_EMU_INC_STATS(errors);
676 if (__get_user(val, va)) {
677 MIPS_FPU_EMU_INC_STATS(errors);
681 SITOREG(val, MIPSInst_FD(ir));
685 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
686 xcp->regs[MIPSInst_FT(ir)]);
688 MIPS_FPU_EMU_INC_STATS(stores);
690 SIFROMREG(val, MIPSInst_FS(ir));
691 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
692 MIPS_FPU_EMU_INC_STATS(errors);
696 if (put_user(val, va)) {
697 MIPS_FPU_EMU_INC_STATS(errors);
704 handler = fpemu_sp_madd;
707 handler = fpemu_sp_msub;
710 handler = fpemu_sp_nmadd;
713 handler = fpemu_sp_nmsub;
717 SPFROMREG(fr, MIPSInst_FR(ir));
718 SPFROMREG(fs, MIPSInst_FS(ir));
719 SPFROMREG(ft, MIPSInst_FT(ir));
720 fd = (*handler) (fr, fs, ft);
721 SPTOREG(fd, MIPSInst_FD(ir));
724 if (ieee754_cxtest(IEEE754_INEXACT))
725 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
726 if (ieee754_cxtest(IEEE754_UNDERFLOW))
727 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
728 if (ieee754_cxtest(IEEE754_OVERFLOW))
729 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
730 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
731 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
733 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
734 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
735 /*printk ("SIGFPE: fpu csr = %08x\n",
749 ieee754dp(*handler) (ieee754dp, ieee754dp, ieee754dp);
750 ieee754dp fd, fr, fs, ft;
754 switch (MIPSInst_FUNC(ir)) {
756 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
757 xcp->regs[MIPSInst_FT(ir)]);
759 MIPS_FPU_EMU_INC_STATS(loads);
760 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
761 MIPS_FPU_EMU_INC_STATS(errors);
765 if (__get_user(val, va)) {
766 MIPS_FPU_EMU_INC_STATS(errors);
770 DITOREG(val, MIPSInst_FD(ir));
774 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
775 xcp->regs[MIPSInst_FT(ir)]);
777 MIPS_FPU_EMU_INC_STATS(stores);
778 DIFROMREG(val, MIPSInst_FS(ir));
779 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
780 MIPS_FPU_EMU_INC_STATS(errors);
784 if (__put_user(val, va)) {
785 MIPS_FPU_EMU_INC_STATS(errors);
792 handler = fpemu_dp_madd;
795 handler = fpemu_dp_msub;
798 handler = fpemu_dp_nmadd;
801 handler = fpemu_dp_nmsub;
805 DPFROMREG(fr, MIPSInst_FR(ir));
806 DPFROMREG(fs, MIPSInst_FS(ir));
807 DPFROMREG(ft, MIPSInst_FT(ir));
808 fd = (*handler) (fr, fs, ft);
809 DPTOREG(fd, MIPSInst_FD(ir));
819 if (MIPSInst_FUNC(ir) != pfetch_op) {
822 /* ignore prefx operation */
836 * Emulate a single COP1 arithmetic instruction.
838 static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
841 int rfmt; /* resulting format */
842 unsigned rcsr = 0; /* resulting csr */
851 } rv; /* resulting value */
853 MIPS_FPU_EMU_INC_STATS(cp1ops);
854 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
857 ieee754sp(*b) (ieee754sp, ieee754sp);
858 ieee754sp(*u) (ieee754sp);
861 switch (MIPSInst_FUNC(ir)) {
864 handler.b = ieee754sp_add;
867 handler.b = ieee754sp_sub;
870 handler.b = ieee754sp_mul;
873 handler.b = ieee754sp_div;
877 #if __mips >= 2 || defined(__mips64)
879 handler.u = ieee754sp_sqrt;
882 #if __mips >= 4 && __mips != 32
884 handler.u = fpemu_sp_rsqrt;
887 handler.u = fpemu_sp_recip;
892 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
893 if (((ctx->fcr31 & cond) != 0) !=
894 ((MIPSInst_FT(ir) & 1) != 0))
896 SPFROMREG(rv.s, MIPSInst_FS(ir));
899 if (xcp->regs[MIPSInst_FT(ir)] != 0)
901 SPFROMREG(rv.s, MIPSInst_FS(ir));
904 if (xcp->regs[MIPSInst_FT(ir)] == 0)
906 SPFROMREG(rv.s, MIPSInst_FS(ir));
910 handler.u = ieee754sp_abs;
913 handler.u = ieee754sp_neg;
917 SPFROMREG(rv.s, MIPSInst_FS(ir));
920 /* binary op on handler */
925 SPFROMREG(fs, MIPSInst_FS(ir));
926 SPFROMREG(ft, MIPSInst_FT(ir));
928 rv.s = (*handler.b) (fs, ft);
935 SPFROMREG(fs, MIPSInst_FS(ir));
936 rv.s = (*handler.u) (fs);
940 if (ieee754_cxtest(IEEE754_INEXACT))
941 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
942 if (ieee754_cxtest(IEEE754_UNDERFLOW))
943 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
944 if (ieee754_cxtest(IEEE754_OVERFLOW))
945 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
946 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE))
947 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
948 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
949 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
954 return SIGILL; /* not defined */
958 SPFROMREG(fs, MIPSInst_FS(ir));
959 rv.d = ieee754dp_fsp(fs);
966 SPFROMREG(fs, MIPSInst_FS(ir));
967 rv.w = ieee754sp_tint(fs);
972 #if __mips >= 2 || defined(__mips64)
977 unsigned int oldrm = ieee754_csr.rm;
980 SPFROMREG(fs, MIPSInst_FS(ir));
981 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
982 rv.w = ieee754sp_tint(fs);
983 ieee754_csr.rm = oldrm;
987 #endif /* __mips >= 2 */
989 #if defined(__mips64)
993 SPFROMREG(fs, MIPSInst_FS(ir));
994 rv.l = ieee754sp_tlong(fs);
1003 unsigned int oldrm = ieee754_csr.rm;
1006 SPFROMREG(fs, MIPSInst_FS(ir));
1007 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
1008 rv.l = ieee754sp_tlong(fs);
1009 ieee754_csr.rm = oldrm;
1013 #endif /* defined(__mips64) */
1016 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1017 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1020 SPFROMREG(fs, MIPSInst_FS(ir));
1021 SPFROMREG(ft, MIPSInst_FT(ir));
1022 rv.w = ieee754sp_cmp(fs, ft,
1023 cmptab[cmpop & 0x7], cmpop & 0x8);
1025 if ((cmpop & 0x8) && ieee754_cxtest
1026 (IEEE754_INVALID_OPERATION))
1027 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1042 ieee754dp(*b) (ieee754dp, ieee754dp);
1043 ieee754dp(*u) (ieee754dp);
1046 switch (MIPSInst_FUNC(ir)) {
1049 handler.b = ieee754dp_add;
1052 handler.b = ieee754dp_sub;
1055 handler.b = ieee754dp_mul;
1058 handler.b = ieee754dp_div;
1062 #if __mips >= 2 || defined(__mips64)
1064 handler.u = ieee754dp_sqrt;
1067 #if __mips >= 4 && __mips != 32
1069 handler.u = fpemu_dp_rsqrt;
1072 handler.u = fpemu_dp_recip;
1077 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1078 if (((ctx->fcr31 & cond) != 0) !=
1079 ((MIPSInst_FT(ir) & 1) != 0))
1081 DPFROMREG(rv.d, MIPSInst_FS(ir));
1084 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1086 DPFROMREG(rv.d, MIPSInst_FS(ir));
1089 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1091 DPFROMREG(rv.d, MIPSInst_FS(ir));
1095 handler.u = ieee754dp_abs;
1099 handler.u = ieee754dp_neg;
1104 DPFROMREG(rv.d, MIPSInst_FS(ir));
1107 /* binary op on handler */
1111 DPFROMREG(fs, MIPSInst_FS(ir));
1112 DPFROMREG(ft, MIPSInst_FT(ir));
1114 rv.d = (*handler.b) (fs, ft);
1120 DPFROMREG(fs, MIPSInst_FS(ir));
1121 rv.d = (*handler.u) (fs);
1125 /* unary conv ops */
1129 DPFROMREG(fs, MIPSInst_FS(ir));
1130 rv.s = ieee754sp_fdp(fs);
1135 return SIGILL; /* not defined */
1140 DPFROMREG(fs, MIPSInst_FS(ir));
1141 rv.w = ieee754dp_tint(fs); /* wrong */
1146 #if __mips >= 2 || defined(__mips64)
1151 unsigned int oldrm = ieee754_csr.rm;
1154 DPFROMREG(fs, MIPSInst_FS(ir));
1155 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
1156 rv.w = ieee754dp_tint(fs);
1157 ieee754_csr.rm = oldrm;
1163 #if defined(__mips64)
1167 DPFROMREG(fs, MIPSInst_FS(ir));
1168 rv.l = ieee754dp_tlong(fs);
1177 unsigned int oldrm = ieee754_csr.rm;
1180 DPFROMREG(fs, MIPSInst_FS(ir));
1181 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
1182 rv.l = ieee754dp_tlong(fs);
1183 ieee754_csr.rm = oldrm;
1187 #endif /* __mips >= 3 */
1190 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1191 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1194 DPFROMREG(fs, MIPSInst_FS(ir));
1195 DPFROMREG(ft, MIPSInst_FT(ir));
1196 rv.w = ieee754dp_cmp(fs, ft,
1197 cmptab[cmpop & 0x7], cmpop & 0x8);
1202 (IEEE754_INVALID_OPERATION))
1203 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1219 switch (MIPSInst_FUNC(ir)) {
1221 /* convert word to single precision real */
1222 SPFROMREG(fs, MIPSInst_FS(ir));
1223 rv.s = ieee754sp_fint(fs.bits);
1227 /* convert word to double precision real */
1228 SPFROMREG(fs, MIPSInst_FS(ir));
1229 rv.d = ieee754dp_fint(fs.bits);
1238 #if defined(__mips64)
1240 switch (MIPSInst_FUNC(ir)) {
1242 /* convert long to single precision real */
1243 rv.s = ieee754sp_flong(ctx->fpr[MIPSInst_FS(ir)]);
1247 /* convert long to double precision real */
1248 rv.d = ieee754dp_flong(ctx->fpr[MIPSInst_FS(ir)]);
1263 * Update the fpu CSR register for this operation.
1264 * If an exception is required, generate a tidy SIGFPE exception,
1265 * without updating the result register.
1266 * Note: cause exception bits do not accumulate, they are rewritten
1267 * for each op; only the flag/sticky bits accumulate.
1269 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1270 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1271 /*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */
1276 * Now we can safely write the result back to the register file.
1281 cond = fpucondbit[MIPSInst_FD(ir) >> 2];
1283 cond = FPU_CSR_COND;
1288 ctx->fcr31 &= ~cond;
1292 DPTOREG(rv.d, MIPSInst_FD(ir));
1295 SPTOREG(rv.s, MIPSInst_FD(ir));
1298 SITOREG(rv.w, MIPSInst_FD(ir));
1300 #if defined(__mips64)
1302 DITOREG(rv.l, MIPSInst_FD(ir));
1312 int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1313 int has_fpu, void *__user *fault_addr)
1315 unsigned long oldepc, prevepc;
1316 mips_instruction insn;
1319 oldepc = xcp->cp0_epc;
1321 prevepc = xcp->cp0_epc;
1323 if (!access_ok(VERIFY_READ, xcp->cp0_epc, sizeof(mips_instruction))) {
1324 MIPS_FPU_EMU_INC_STATS(errors);
1325 *fault_addr = (mips_instruction __user *)xcp->cp0_epc;
1328 if (__get_user(insn, (mips_instruction __user *) xcp->cp0_epc)) {
1329 MIPS_FPU_EMU_INC_STATS(errors);
1330 *fault_addr = (mips_instruction __user *)xcp->cp0_epc;
1334 xcp->cp0_epc += 4; /* skip nops */
1337 * The 'ieee754_csr' is an alias of
1338 * ctx->fcr31. No need to copy ctx->fcr31 to
1339 * ieee754_csr. But ieee754_csr.rm is ieee
1340 * library modes. (not mips rounding mode)
1342 /* convert to ieee library modes */
1343 ieee754_csr.rm = ieee_rm[ieee754_csr.rm];
1344 sig = cop1Emulate(xcp, ctx, fault_addr);
1345 /* revert to mips rounding mode */
1346 ieee754_csr.rm = mips_rm[ieee754_csr.rm];
1355 } while (xcp->cp0_epc > prevepc);
1357 /* SIGILL indicates a non-fpu instruction */
1358 if (sig == SIGILL && xcp->cp0_epc != oldepc)
1359 /* but if epc has advanced, then ignore it */
1365 #ifdef CONFIG_DEBUG_FS
1367 static int fpuemu_stat_get(void *data, u64 *val)
1370 unsigned long sum = 0;
1371 for_each_online_cpu(cpu) {
1372 struct mips_fpu_emulator_stats *ps;
1374 ps = &per_cpu(fpuemustats, cpu);
1375 pv = (void *)ps + (unsigned long)data;
1376 sum += local_read(pv);
1381 DEFINE_SIMPLE_ATTRIBUTE(fops_fpuemu_stat, fpuemu_stat_get, NULL, "%llu\n");
1383 extern struct dentry *mips_debugfs_dir;
1384 static int __init debugfs_fpuemu(void)
1386 struct dentry *d, *dir;
1388 if (!mips_debugfs_dir)
1390 dir = debugfs_create_dir("fpuemustats", mips_debugfs_dir);
1394 #define FPU_STAT_CREATE(M) \
1396 d = debugfs_create_file(#M , S_IRUGO, dir, \
1397 (void *)offsetof(struct mips_fpu_emulator_stats, M), \
1398 &fops_fpuemu_stat); \
1403 FPU_STAT_CREATE(emulated);
1404 FPU_STAT_CREATE(loads);
1405 FPU_STAT_CREATE(stores);
1406 FPU_STAT_CREATE(cp1ops);
1407 FPU_STAT_CREATE(cp1xops);
1408 FPU_STAT_CREATE(errors);
1412 __initcall(debugfs_fpuemu);