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/module.h>
39 #include <linux/debugfs.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
);
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 /* Convert Mips rounding mode (0..3) to IEEE library modes. */
82 static const unsigned char ieee_rm
[4] = {
83 [FPU_CSR_RN
] = IEEE754_RN
,
84 [FPU_CSR_RZ
] = IEEE754_RZ
,
85 [FPU_CSR_RU
] = IEEE754_RU
,
86 [FPU_CSR_RD
] = IEEE754_RD
,
88 /* Convert IEEE library modes to Mips rounding mode (0..3). */
89 static const unsigned char mips_rm
[4] = {
90 [IEEE754_RN
] = FPU_CSR_RN
,
91 [IEEE754_RZ
] = FPU_CSR_RZ
,
92 [IEEE754_RD
] = FPU_CSR_RD
,
93 [IEEE754_RU
] = FPU_CSR_RU
,
97 /* convert condition code register number to csr bit */
98 static const unsigned int fpucondbit
[8] = {
112 * Redundant with logic already in kernel/branch.c,
113 * embedded in compute_return_epc. At some point,
114 * a single subroutine should be used across both
117 static int isBranchInstr(mips_instruction
* i
)
119 switch (MIPSInst_OPCODE(*i
)) {
121 switch (MIPSInst_FUNC(*i
)) {
129 switch (MIPSInst_RT(*i
)) {
159 if (MIPSInst_RS(*i
) == bc_op
)
168 * In the Linux kernel, we support selection of FPR format on the
169 * basis of the Status.FR bit. If an FPU is not present, the FR bit
170 * is hardwired to zero, which would imply a 32-bit FPU even for
171 * 64-bit CPUs. For 64-bit kernels with no FPU we use TIF_32BIT_REGS
172 * as a proxy for the FR bit so that a 64-bit FPU is emulated. In any
173 * case, for a 32-bit kernel which uses the O32 MIPS ABI, only the
174 * even FPRs are used (Status.FR = 0).
176 static inline int cop1_64bit(struct pt_regs
*xcp
)
179 return xcp
->cp0_status
& ST0_FR
;
181 return !test_thread_flag(TIF_32BIT_REGS
);
187 #define SIFROMREG(si, x) ((si) = cop1_64bit(xcp) || !(x & 1) ? \
188 (int)ctx->fpr[x] : (int)(ctx->fpr[x & ~1] >> 32))
190 #define SITOREG(si, x) (ctx->fpr[x & ~(cop1_64bit(xcp) == 0)] = \
191 cop1_64bit(xcp) || !(x & 1) ? \
192 ctx->fpr[x & ~1] >> 32 << 32 | (u32)(si) : \
193 ctx->fpr[x & ~1] << 32 >> 32 | (u64)(si) << 32)
195 #define DIFROMREG(di, x) ((di) = ctx->fpr[x & ~(cop1_64bit(xcp) == 0)])
196 #define DITOREG(di, x) (ctx->fpr[x & ~(cop1_64bit(xcp) == 0)] = (di))
198 #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
199 #define SPTOREG(sp, x) SITOREG((sp).bits, x)
200 #define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
201 #define DPTOREG(dp, x) DITOREG((dp).bits, x)
204 * Emulate the single floating point instruction pointed at by EPC.
205 * Two instructions if the instruction is in a branch delay slot.
208 static int cop1Emulate(struct pt_regs
*xcp
, struct mips_fpu_struct
*ctx
)
211 unsigned long emulpc
, contpc
;
214 if (get_user(ir
, (mips_instruction __user
*) xcp
->cp0_epc
)) {
215 MIPS_FPU_EMU_INC_STATS(errors
);
219 /* XXX NEC Vr54xx bug workaround */
220 if ((xcp
->cp0_cause
& CAUSEF_BD
) && !isBranchInstr(&ir
))
221 xcp
->cp0_cause
&= ~CAUSEF_BD
;
223 if (xcp
->cp0_cause
& CAUSEF_BD
) {
225 * The instruction to be emulated is in a branch delay slot
226 * which means that we have to emulate the branch instruction
227 * BEFORE we do the cop1 instruction.
229 * This branch could be a COP1 branch, but in that case we
230 * would have had a trap for that instruction, and would not
231 * come through this route.
233 * Linux MIPS branch emulator operates on context, updating the
236 emulpc
= xcp
->cp0_epc
+ 4; /* Snapshot emulation target */
238 if (__compute_return_epc(xcp
)) {
240 printk("failed to emulate branch at %p\n",
241 (void *) (xcp
->cp0_epc
));
245 if (get_user(ir
, (mips_instruction __user
*) emulpc
)) {
246 MIPS_FPU_EMU_INC_STATS(errors
);
249 /* __compute_return_epc() will have updated cp0_epc */
250 contpc
= xcp
->cp0_epc
;
251 /* In order not to confuse ptrace() et al, tweak context */
252 xcp
->cp0_epc
= emulpc
- 4;
254 emulpc
= xcp
->cp0_epc
;
255 contpc
= xcp
->cp0_epc
+ 4;
259 MIPS_FPU_EMU_INC_STATS(emulated
);
260 switch (MIPSInst_OPCODE(ir
)) {
262 u64 __user
*va
= (u64 __user
*) (xcp
->regs
[MIPSInst_RS(ir
)] +
266 MIPS_FPU_EMU_INC_STATS(loads
);
267 if (get_user(val
, va
)) {
268 MIPS_FPU_EMU_INC_STATS(errors
);
271 DITOREG(val
, MIPSInst_RT(ir
));
276 u64 __user
*va
= (u64 __user
*) (xcp
->regs
[MIPSInst_RS(ir
)] +
280 MIPS_FPU_EMU_INC_STATS(stores
);
281 DIFROMREG(val
, MIPSInst_RT(ir
));
282 if (put_user(val
, va
)) {
283 MIPS_FPU_EMU_INC_STATS(errors
);
290 u32 __user
*va
= (u32 __user
*) (xcp
->regs
[MIPSInst_RS(ir
)] +
294 MIPS_FPU_EMU_INC_STATS(loads
);
295 if (get_user(val
, va
)) {
296 MIPS_FPU_EMU_INC_STATS(errors
);
299 SITOREG(val
, MIPSInst_RT(ir
));
304 u32 __user
*va
= (u32 __user
*) (xcp
->regs
[MIPSInst_RS(ir
)] +
308 MIPS_FPU_EMU_INC_STATS(stores
);
309 SIFROMREG(val
, MIPSInst_RT(ir
));
310 if (put_user(val
, va
)) {
311 MIPS_FPU_EMU_INC_STATS(errors
);
318 switch (MIPSInst_RS(ir
)) {
320 #if defined(__mips64)
322 /* copregister fs -> gpr[rt] */
323 if (MIPSInst_RT(ir
) != 0) {
324 DIFROMREG(xcp
->regs
[MIPSInst_RT(ir
)],
330 /* copregister fs <- rt */
331 DITOREG(xcp
->regs
[MIPSInst_RT(ir
)], MIPSInst_RD(ir
));
336 /* copregister rd -> gpr[rt] */
337 if (MIPSInst_RT(ir
) != 0) {
338 SIFROMREG(xcp
->regs
[MIPSInst_RT(ir
)],
344 /* copregister rd <- rt */
345 SITOREG(xcp
->regs
[MIPSInst_RT(ir
)], MIPSInst_RD(ir
));
349 /* cop control register rd -> gpr[rt] */
352 if (MIPSInst_RD(ir
) == FPCREG_CSR
) {
354 value
= (value
& ~0x3) | mips_rm
[value
& 0x3];
356 printk("%p gpr[%d]<-csr=%08x\n",
357 (void *) (xcp
->cp0_epc
),
358 MIPSInst_RT(ir
), value
);
361 else if (MIPSInst_RD(ir
) == FPCREG_RID
)
366 xcp
->regs
[MIPSInst_RT(ir
)] = value
;
371 /* copregister rd <- rt */
374 if (MIPSInst_RT(ir
) == 0)
377 value
= xcp
->regs
[MIPSInst_RT(ir
)];
379 /* we only have one writable control reg
381 if (MIPSInst_RD(ir
) == FPCREG_CSR
) {
383 printk("%p gpr[%d]->csr=%08x\n",
384 (void *) (xcp
->cp0_epc
),
385 MIPSInst_RT(ir
), value
);
387 value
&= (FPU_CSR_FLUSH
| FPU_CSR_ALL_E
| FPU_CSR_ALL_S
| 0x03);
388 ctx
->fcr31
&= ~(FPU_CSR_FLUSH
| FPU_CSR_ALL_E
| FPU_CSR_ALL_S
| 0x03);
389 /* convert to ieee library modes */
390 ctx
->fcr31
|= (value
& ~0x3) | ieee_rm
[value
& 0x3];
392 if ((ctx
->fcr31
>> 5) & ctx
->fcr31
& FPU_CSR_ALL_E
) {
401 if (xcp
->cp0_cause
& CAUSEF_BD
)
405 cond
= ctx
->fcr31
& fpucondbit
[MIPSInst_RT(ir
) >> 2];
407 cond
= ctx
->fcr31
& FPU_CSR_COND
;
409 switch (MIPSInst_RT(ir
) & 3) {
420 /* thats an illegal instruction */
424 xcp
->cp0_cause
|= CAUSEF_BD
;
426 /* branch taken: emulate dslot
430 contpc
= (xcp
->cp0_epc
+
431 (MIPSInst_SIMM(ir
) << 2));
434 (mips_instruction __user
*) xcp
->cp0_epc
)) {
435 MIPS_FPU_EMU_INC_STATS(errors
);
439 switch (MIPSInst_OPCODE(ir
)) {
442 #if (__mips >= 2 || defined(__mips64))
447 #if __mips >= 4 && __mips != 32
450 /* its one of ours */
454 if (MIPSInst_FUNC(ir
) == movc_op
)
461 * Single step the non-cp1
462 * instruction in the dslot
464 return mips_dsemul(xcp
, ir
, contpc
);
467 /* branch not taken */
470 * branch likely nullifies
476 * else continue & execute
477 * dslot as normal insn
485 if (!(MIPSInst_RS(ir
) & 0x10))
490 /* a real fpu computation instruction */
491 if ((sig
= fpu_emu(xcp
, ctx
, ir
)))
497 #if __mips >= 4 && __mips != 32
501 if ((sig
= fpux_emu(xcp
, ctx
, ir
)))
509 if (MIPSInst_FUNC(ir
) != movc_op
)
511 cond
= fpucondbit
[MIPSInst_RT(ir
) >> 2];
512 if (((ctx
->fcr31
& cond
) != 0) == ((MIPSInst_RT(ir
) & 1) != 0))
513 xcp
->regs
[MIPSInst_RD(ir
)] =
514 xcp
->regs
[MIPSInst_RS(ir
)];
523 xcp
->cp0_epc
= contpc
;
524 xcp
->cp0_cause
&= ~CAUSEF_BD
;
530 * Conversion table from MIPS compare ops 48-63
531 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
533 static const unsigned char cmptab
[8] = {
534 0, /* cmp_0 (sig) cmp_sf */
535 IEEE754_CUN
, /* cmp_un (sig) cmp_ngle */
536 IEEE754_CEQ
, /* cmp_eq (sig) cmp_seq */
537 IEEE754_CEQ
| IEEE754_CUN
, /* cmp_ueq (sig) cmp_ngl */
538 IEEE754_CLT
, /* cmp_olt (sig) cmp_lt */
539 IEEE754_CLT
| IEEE754_CUN
, /* cmp_ult (sig) cmp_nge */
540 IEEE754_CLT
| IEEE754_CEQ
, /* cmp_ole (sig) cmp_le */
541 IEEE754_CLT
| IEEE754_CEQ
| IEEE754_CUN
, /* cmp_ule (sig) cmp_ngt */
545 #if __mips >= 4 && __mips != 32
548 * Additional MIPS4 instructions
551 #define DEF3OP(name, p, f1, f2, f3) \
552 static ieee754##p fpemu_##p##_##name(ieee754##p r, ieee754##p s, \
555 struct _ieee754_csr ieee754_csr_save; \
557 ieee754_csr_save = ieee754_csr; \
559 ieee754_csr_save.cx |= ieee754_csr.cx; \
560 ieee754_csr_save.sx |= ieee754_csr.sx; \
562 ieee754_csr.cx |= ieee754_csr_save.cx; \
563 ieee754_csr.sx |= ieee754_csr_save.sx; \
567 static ieee754dp
fpemu_dp_recip(ieee754dp d
)
569 return ieee754dp_div(ieee754dp_one(0), d
);
572 static ieee754dp
fpemu_dp_rsqrt(ieee754dp d
)
574 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d
));
577 static ieee754sp
fpemu_sp_recip(ieee754sp s
)
579 return ieee754sp_div(ieee754sp_one(0), s
);
582 static ieee754sp
fpemu_sp_rsqrt(ieee754sp s
)
584 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s
));
587 DEF3OP(madd
, sp
, ieee754sp_mul
, ieee754sp_add
, );
588 DEF3OP(msub
, sp
, ieee754sp_mul
, ieee754sp_sub
, );
589 DEF3OP(nmadd
, sp
, ieee754sp_mul
, ieee754sp_add
, ieee754sp_neg
);
590 DEF3OP(nmsub
, sp
, ieee754sp_mul
, ieee754sp_sub
, ieee754sp_neg
);
591 DEF3OP(madd
, dp
, ieee754dp_mul
, ieee754dp_add
, );
592 DEF3OP(msub
, dp
, ieee754dp_mul
, ieee754dp_sub
, );
593 DEF3OP(nmadd
, dp
, ieee754dp_mul
, ieee754dp_add
, ieee754dp_neg
);
594 DEF3OP(nmsub
, dp
, ieee754dp_mul
, ieee754dp_sub
, ieee754dp_neg
);
596 static int fpux_emu(struct pt_regs
*xcp
, struct mips_fpu_struct
*ctx
,
599 unsigned rcsr
= 0; /* resulting csr */
601 MIPS_FPU_EMU_INC_STATS(cp1xops
);
603 switch (MIPSInst_FMA_FFMT(ir
)) {
606 ieee754sp(*handler
) (ieee754sp
, ieee754sp
, ieee754sp
);
607 ieee754sp fd
, fr
, fs
, ft
;
611 switch (MIPSInst_FUNC(ir
)) {
613 va
= (void __user
*) (xcp
->regs
[MIPSInst_FR(ir
)] +
614 xcp
->regs
[MIPSInst_FT(ir
)]);
616 MIPS_FPU_EMU_INC_STATS(loads
);
617 if (get_user(val
, va
)) {
618 MIPS_FPU_EMU_INC_STATS(errors
);
621 SITOREG(val
, MIPSInst_FD(ir
));
625 va
= (void __user
*) (xcp
->regs
[MIPSInst_FR(ir
)] +
626 xcp
->regs
[MIPSInst_FT(ir
)]);
628 MIPS_FPU_EMU_INC_STATS(stores
);
630 SIFROMREG(val
, MIPSInst_FS(ir
));
631 if (put_user(val
, va
)) {
632 MIPS_FPU_EMU_INC_STATS(errors
);
638 handler
= fpemu_sp_madd
;
641 handler
= fpemu_sp_msub
;
644 handler
= fpemu_sp_nmadd
;
647 handler
= fpemu_sp_nmsub
;
651 SPFROMREG(fr
, MIPSInst_FR(ir
));
652 SPFROMREG(fs
, MIPSInst_FS(ir
));
653 SPFROMREG(ft
, MIPSInst_FT(ir
));
654 fd
= (*handler
) (fr
, fs
, ft
);
655 SPTOREG(fd
, MIPSInst_FD(ir
));
658 if (ieee754_cxtest(IEEE754_INEXACT
))
659 rcsr
|= FPU_CSR_INE_X
| FPU_CSR_INE_S
;
660 if (ieee754_cxtest(IEEE754_UNDERFLOW
))
661 rcsr
|= FPU_CSR_UDF_X
| FPU_CSR_UDF_S
;
662 if (ieee754_cxtest(IEEE754_OVERFLOW
))
663 rcsr
|= FPU_CSR_OVF_X
| FPU_CSR_OVF_S
;
664 if (ieee754_cxtest(IEEE754_INVALID_OPERATION
))
665 rcsr
|= FPU_CSR_INV_X
| FPU_CSR_INV_S
;
667 ctx
->fcr31
= (ctx
->fcr31
& ~FPU_CSR_ALL_X
) | rcsr
;
668 if ((ctx
->fcr31
>> 5) & ctx
->fcr31
& FPU_CSR_ALL_E
) {
669 /*printk ("SIGFPE: fpu csr = %08x\n",
683 ieee754dp(*handler
) (ieee754dp
, ieee754dp
, ieee754dp
);
684 ieee754dp fd
, fr
, fs
, ft
;
688 switch (MIPSInst_FUNC(ir
)) {
690 va
= (void __user
*) (xcp
->regs
[MIPSInst_FR(ir
)] +
691 xcp
->regs
[MIPSInst_FT(ir
)]);
693 MIPS_FPU_EMU_INC_STATS(loads
);
694 if (get_user(val
, va
)) {
695 MIPS_FPU_EMU_INC_STATS(errors
);
698 DITOREG(val
, MIPSInst_FD(ir
));
702 va
= (void __user
*) (xcp
->regs
[MIPSInst_FR(ir
)] +
703 xcp
->regs
[MIPSInst_FT(ir
)]);
705 MIPS_FPU_EMU_INC_STATS(stores
);
706 DIFROMREG(val
, MIPSInst_FS(ir
));
707 if (put_user(val
, va
)) {
708 MIPS_FPU_EMU_INC_STATS(errors
);
714 handler
= fpemu_dp_madd
;
717 handler
= fpemu_dp_msub
;
720 handler
= fpemu_dp_nmadd
;
723 handler
= fpemu_dp_nmsub
;
727 DPFROMREG(fr
, MIPSInst_FR(ir
));
728 DPFROMREG(fs
, MIPSInst_FS(ir
));
729 DPFROMREG(ft
, MIPSInst_FT(ir
));
730 fd
= (*handler
) (fr
, fs
, ft
);
731 DPTOREG(fd
, MIPSInst_FD(ir
));
741 if (MIPSInst_FUNC(ir
) != pfetch_op
) {
744 /* ignore prefx operation */
758 * Emulate a single COP1 arithmetic instruction.
760 static int fpu_emu(struct pt_regs
*xcp
, struct mips_fpu_struct
*ctx
,
763 int rfmt
; /* resulting format */
764 unsigned rcsr
= 0; /* resulting csr */
773 } rv
; /* resulting value */
775 MIPS_FPU_EMU_INC_STATS(cp1ops
);
776 switch (rfmt
= (MIPSInst_FFMT(ir
) & 0xf)) {
779 ieee754sp(*b
) (ieee754sp
, ieee754sp
);
780 ieee754sp(*u
) (ieee754sp
);
783 switch (MIPSInst_FUNC(ir
)) {
786 handler
.b
= ieee754sp_add
;
789 handler
.b
= ieee754sp_sub
;
792 handler
.b
= ieee754sp_mul
;
795 handler
.b
= ieee754sp_div
;
799 #if __mips >= 2 || defined(__mips64)
801 handler
.u
= ieee754sp_sqrt
;
804 #if __mips >= 4 && __mips != 32
806 handler
.u
= fpemu_sp_rsqrt
;
809 handler
.u
= fpemu_sp_recip
;
814 cond
= fpucondbit
[MIPSInst_FT(ir
) >> 2];
815 if (((ctx
->fcr31
& cond
) != 0) !=
816 ((MIPSInst_FT(ir
) & 1) != 0))
818 SPFROMREG(rv
.s
, MIPSInst_FS(ir
));
821 if (xcp
->regs
[MIPSInst_FT(ir
)] != 0)
823 SPFROMREG(rv
.s
, MIPSInst_FS(ir
));
826 if (xcp
->regs
[MIPSInst_FT(ir
)] == 0)
828 SPFROMREG(rv
.s
, MIPSInst_FS(ir
));
832 handler
.u
= ieee754sp_abs
;
835 handler
.u
= ieee754sp_neg
;
839 SPFROMREG(rv
.s
, MIPSInst_FS(ir
));
842 /* binary op on handler */
847 SPFROMREG(fs
, MIPSInst_FS(ir
));
848 SPFROMREG(ft
, MIPSInst_FT(ir
));
850 rv
.s
= (*handler
.b
) (fs
, ft
);
857 SPFROMREG(fs
, MIPSInst_FS(ir
));
858 rv
.s
= (*handler
.u
) (fs
);
862 if (ieee754_cxtest(IEEE754_INEXACT
))
863 rcsr
|= FPU_CSR_INE_X
| FPU_CSR_INE_S
;
864 if (ieee754_cxtest(IEEE754_UNDERFLOW
))
865 rcsr
|= FPU_CSR_UDF_X
| FPU_CSR_UDF_S
;
866 if (ieee754_cxtest(IEEE754_OVERFLOW
))
867 rcsr
|= FPU_CSR_OVF_X
| FPU_CSR_OVF_S
;
868 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE
))
869 rcsr
|= FPU_CSR_DIV_X
| FPU_CSR_DIV_S
;
870 if (ieee754_cxtest(IEEE754_INVALID_OPERATION
))
871 rcsr
|= FPU_CSR_INV_X
| FPU_CSR_INV_S
;
876 return SIGILL
; /* not defined */
880 SPFROMREG(fs
, MIPSInst_FS(ir
));
881 rv
.d
= ieee754dp_fsp(fs
);
888 SPFROMREG(fs
, MIPSInst_FS(ir
));
889 rv
.w
= ieee754sp_tint(fs
);
894 #if __mips >= 2 || defined(__mips64)
899 unsigned int oldrm
= ieee754_csr
.rm
;
902 SPFROMREG(fs
, MIPSInst_FS(ir
));
903 ieee754_csr
.rm
= ieee_rm
[MIPSInst_FUNC(ir
) & 0x3];
904 rv
.w
= ieee754sp_tint(fs
);
905 ieee754_csr
.rm
= oldrm
;
909 #endif /* __mips >= 2 */
911 #if defined(__mips64)
915 SPFROMREG(fs
, MIPSInst_FS(ir
));
916 rv
.l
= ieee754sp_tlong(fs
);
925 unsigned int oldrm
= ieee754_csr
.rm
;
928 SPFROMREG(fs
, MIPSInst_FS(ir
));
929 ieee754_csr
.rm
= ieee_rm
[MIPSInst_FUNC(ir
) & 0x3];
930 rv
.l
= ieee754sp_tlong(fs
);
931 ieee754_csr
.rm
= oldrm
;
935 #endif /* defined(__mips64) */
938 if (MIPSInst_FUNC(ir
) >= fcmp_op
) {
939 unsigned cmpop
= MIPSInst_FUNC(ir
) - fcmp_op
;
942 SPFROMREG(fs
, MIPSInst_FS(ir
));
943 SPFROMREG(ft
, MIPSInst_FT(ir
));
944 rv
.w
= ieee754sp_cmp(fs
, ft
,
945 cmptab
[cmpop
& 0x7], cmpop
& 0x8);
947 if ((cmpop
& 0x8) && ieee754_cxtest
948 (IEEE754_INVALID_OPERATION
))
949 rcsr
= FPU_CSR_INV_X
| FPU_CSR_INV_S
;
964 ieee754dp(*b
) (ieee754dp
, ieee754dp
);
965 ieee754dp(*u
) (ieee754dp
);
968 switch (MIPSInst_FUNC(ir
)) {
971 handler
.b
= ieee754dp_add
;
974 handler
.b
= ieee754dp_sub
;
977 handler
.b
= ieee754dp_mul
;
980 handler
.b
= ieee754dp_div
;
984 #if __mips >= 2 || defined(__mips64)
986 handler
.u
= ieee754dp_sqrt
;
989 #if __mips >= 4 && __mips != 32
991 handler
.u
= fpemu_dp_rsqrt
;
994 handler
.u
= fpemu_dp_recip
;
999 cond
= fpucondbit
[MIPSInst_FT(ir
) >> 2];
1000 if (((ctx
->fcr31
& cond
) != 0) !=
1001 ((MIPSInst_FT(ir
) & 1) != 0))
1003 DPFROMREG(rv
.d
, MIPSInst_FS(ir
));
1006 if (xcp
->regs
[MIPSInst_FT(ir
)] != 0)
1008 DPFROMREG(rv
.d
, MIPSInst_FS(ir
));
1011 if (xcp
->regs
[MIPSInst_FT(ir
)] == 0)
1013 DPFROMREG(rv
.d
, MIPSInst_FS(ir
));
1017 handler
.u
= ieee754dp_abs
;
1021 handler
.u
= ieee754dp_neg
;
1026 DPFROMREG(rv
.d
, MIPSInst_FS(ir
));
1029 /* binary op on handler */
1033 DPFROMREG(fs
, MIPSInst_FS(ir
));
1034 DPFROMREG(ft
, MIPSInst_FT(ir
));
1036 rv
.d
= (*handler
.b
) (fs
, ft
);
1042 DPFROMREG(fs
, MIPSInst_FS(ir
));
1043 rv
.d
= (*handler
.u
) (fs
);
1047 /* unary conv ops */
1051 DPFROMREG(fs
, MIPSInst_FS(ir
));
1052 rv
.s
= ieee754sp_fdp(fs
);
1057 return SIGILL
; /* not defined */
1062 DPFROMREG(fs
, MIPSInst_FS(ir
));
1063 rv
.w
= ieee754dp_tint(fs
); /* wrong */
1068 #if __mips >= 2 || defined(__mips64)
1073 unsigned int oldrm
= ieee754_csr
.rm
;
1076 DPFROMREG(fs
, MIPSInst_FS(ir
));
1077 ieee754_csr
.rm
= ieee_rm
[MIPSInst_FUNC(ir
) & 0x3];
1078 rv
.w
= ieee754dp_tint(fs
);
1079 ieee754_csr
.rm
= oldrm
;
1085 #if defined(__mips64)
1089 DPFROMREG(fs
, MIPSInst_FS(ir
));
1090 rv
.l
= ieee754dp_tlong(fs
);
1099 unsigned int oldrm
= ieee754_csr
.rm
;
1102 DPFROMREG(fs
, MIPSInst_FS(ir
));
1103 ieee754_csr
.rm
= ieee_rm
[MIPSInst_FUNC(ir
) & 0x3];
1104 rv
.l
= ieee754dp_tlong(fs
);
1105 ieee754_csr
.rm
= oldrm
;
1109 #endif /* __mips >= 3 */
1112 if (MIPSInst_FUNC(ir
) >= fcmp_op
) {
1113 unsigned cmpop
= MIPSInst_FUNC(ir
) - fcmp_op
;
1116 DPFROMREG(fs
, MIPSInst_FS(ir
));
1117 DPFROMREG(ft
, MIPSInst_FT(ir
));
1118 rv
.w
= ieee754dp_cmp(fs
, ft
,
1119 cmptab
[cmpop
& 0x7], cmpop
& 0x8);
1124 (IEEE754_INVALID_OPERATION
))
1125 rcsr
= FPU_CSR_INV_X
| FPU_CSR_INV_S
;
1141 switch (MIPSInst_FUNC(ir
)) {
1143 /* convert word to single precision real */
1144 SPFROMREG(fs
, MIPSInst_FS(ir
));
1145 rv
.s
= ieee754sp_fint(fs
.bits
);
1149 /* convert word to double precision real */
1150 SPFROMREG(fs
, MIPSInst_FS(ir
));
1151 rv
.d
= ieee754dp_fint(fs
.bits
);
1160 #if defined(__mips64)
1162 switch (MIPSInst_FUNC(ir
)) {
1164 /* convert long to single precision real */
1165 rv
.s
= ieee754sp_flong(ctx
->fpr
[MIPSInst_FS(ir
)]);
1169 /* convert long to double precision real */
1170 rv
.d
= ieee754dp_flong(ctx
->fpr
[MIPSInst_FS(ir
)]);
1185 * Update the fpu CSR register for this operation.
1186 * If an exception is required, generate a tidy SIGFPE exception,
1187 * without updating the result register.
1188 * Note: cause exception bits do not accumulate, they are rewritten
1189 * for each op; only the flag/sticky bits accumulate.
1191 ctx
->fcr31
= (ctx
->fcr31
& ~FPU_CSR_ALL_X
) | rcsr
;
1192 if ((ctx
->fcr31
>> 5) & ctx
->fcr31
& FPU_CSR_ALL_E
) {
1193 /*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */
1198 * Now we can safely write the result back to the register file.
1203 cond
= fpucondbit
[MIPSInst_FD(ir
) >> 2];
1205 cond
= FPU_CSR_COND
;
1210 ctx
->fcr31
&= ~cond
;
1214 DPTOREG(rv
.d
, MIPSInst_FD(ir
));
1217 SPTOREG(rv
.s
, MIPSInst_FD(ir
));
1220 SITOREG(rv
.w
, MIPSInst_FD(ir
));
1222 #if defined(__mips64)
1224 DITOREG(rv
.l
, MIPSInst_FD(ir
));
1234 int fpu_emulator_cop1Handler(struct pt_regs
*xcp
, struct mips_fpu_struct
*ctx
,
1237 unsigned long oldepc
, prevepc
;
1238 mips_instruction insn
;
1241 oldepc
= xcp
->cp0_epc
;
1243 prevepc
= xcp
->cp0_epc
;
1245 if (get_user(insn
, (mips_instruction __user
*) xcp
->cp0_epc
)) {
1246 MIPS_FPU_EMU_INC_STATS(errors
);
1250 xcp
->cp0_epc
+= 4; /* skip nops */
1253 * The 'ieee754_csr' is an alias of
1254 * ctx->fcr31. No need to copy ctx->fcr31 to
1255 * ieee754_csr. But ieee754_csr.rm is ieee
1256 * library modes. (not mips rounding mode)
1258 /* convert to ieee library modes */
1259 ieee754_csr
.rm
= ieee_rm
[ieee754_csr
.rm
];
1260 sig
= cop1Emulate(xcp
, ctx
);
1261 /* revert to mips rounding mode */
1262 ieee754_csr
.rm
= mips_rm
[ieee754_csr
.rm
];
1271 } while (xcp
->cp0_epc
> prevepc
);
1273 /* SIGILL indicates a non-fpu instruction */
1274 if (sig
== SIGILL
&& xcp
->cp0_epc
!= oldepc
)
1275 /* but if epc has advanced, then ignore it */
1281 #ifdef CONFIG_DEBUG_FS
1283 static int fpuemu_stat_get(void *data
, u64
*val
)
1286 unsigned long sum
= 0;
1287 for_each_online_cpu(cpu
) {
1288 struct mips_fpu_emulator_stats
*ps
;
1290 ps
= &per_cpu(fpuemustats
, cpu
);
1291 pv
= (void *)ps
+ (unsigned long)data
;
1292 sum
+= local_read(pv
);
1297 DEFINE_SIMPLE_ATTRIBUTE(fops_fpuemu_stat
, fpuemu_stat_get
, NULL
, "%llu\n");
1299 extern struct dentry
*mips_debugfs_dir
;
1300 static int __init
debugfs_fpuemu(void)
1302 struct dentry
*d
, *dir
;
1304 if (!mips_debugfs_dir
)
1306 dir
= debugfs_create_dir("fpuemustats", mips_debugfs_dir
);
1310 #define FPU_STAT_CREATE(M) \
1312 d = debugfs_create_file(#M , S_IRUGO, dir, \
1313 (void *)offsetof(struct mips_fpu_emulator_stats, M), \
1314 &fops_fpuemu_stat); \
1319 FPU_STAT_CREATE(emulated
);
1320 FPU_STAT_CREATE(loads
);
1321 FPU_STAT_CREATE(stores
);
1322 FPU_STAT_CREATE(cp1ops
);
1323 FPU_STAT_CREATE(cp1xops
);
1324 FPU_STAT_CREATE(errors
);
1328 __initcall(debugfs_fpuemu
);