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/debugfs.h>
38 #include <linux/perf_event.h>
40 #include <asm/branch.h>
42 #include <asm/ptrace.h>
43 #include <asm/signal.h>
44 #include <asm/uaccess.h>
46 #include <asm/processor.h>
47 #include <asm/fpu_emulator.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, void *__user *);
69 /* Further private data for which no space exists in mips_fpu_struct */
71 #ifdef CONFIG_DEBUG_FS
72 DEFINE_PER_CPU(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 /* Determine rounding mode from the RM bits of the FCSR */
81 #define modeindex(v) ((v) & FPU_CSR_RM)
83 /* microMIPS bitfields */
84 #define MM_POOL32A_MINOR_MASK 0x3f
85 #define MM_POOL32A_MINOR_SHIFT 0x6
86 #define MM_MIPS32_COND_FC 0x30
88 /* Convert Mips rounding mode (0..3) to IEEE library modes. */
89 static const unsigned char ieee_rm[4] = {
90 [FPU_CSR_RN] = IEEE754_RN,
91 [FPU_CSR_RZ] = IEEE754_RZ,
92 [FPU_CSR_RU] = IEEE754_RU,
93 [FPU_CSR_RD] = IEEE754_RD,
95 /* Convert IEEE library modes to Mips rounding mode (0..3). */
96 static const unsigned char mips_rm[4] = {
97 [IEEE754_RN] = FPU_CSR_RN,
98 [IEEE754_RZ] = FPU_CSR_RZ,
99 [IEEE754_RD] = FPU_CSR_RD,
100 [IEEE754_RU] = FPU_CSR_RU,
104 /* convert condition code register number to csr bit */
105 static const unsigned int fpucondbit[8] = {
117 /* (microMIPS) Convert 16-bit register encoding to 32-bit register encoding. */
118 static const unsigned int reg16to32map[8] = {16, 17, 2, 3, 4, 5, 6, 7};
120 /* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
121 static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
122 static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
123 static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
124 static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
127 * This functions translates a 32-bit microMIPS instruction
128 * into a 32-bit MIPS32 instruction. Returns 0 on success
129 * and SIGILL otherwise.
131 static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
133 union mips_instruction insn = *insn_ptr;
134 union mips_instruction mips32_insn = insn;
137 switch (insn.mm_i_format.opcode) {
139 mips32_insn.mm_i_format.opcode = ldc1_op;
140 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
141 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
144 mips32_insn.mm_i_format.opcode = lwc1_op;
145 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
146 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
149 mips32_insn.mm_i_format.opcode = sdc1_op;
150 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
151 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
154 mips32_insn.mm_i_format.opcode = swc1_op;
155 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
156 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
159 /* NOTE: offset is << by 1 if in microMIPS mode. */
160 if ((insn.mm_i_format.rt == mm_bc1f_op) ||
161 (insn.mm_i_format.rt == mm_bc1t_op)) {
162 mips32_insn.fb_format.opcode = cop1_op;
163 mips32_insn.fb_format.bc = bc_op;
164 mips32_insn.fb_format.flag =
165 (insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
170 switch (insn.mm_fp0_format.func) {
179 op = insn.mm_fp0_format.func;
180 if (op == mm_32f_01_op)
182 else if (op == mm_32f_11_op)
184 else if (op == mm_32f_02_op)
186 else if (op == mm_32f_12_op)
188 else if (op == mm_32f_41_op)
190 else if (op == mm_32f_51_op)
192 else if (op == mm_32f_42_op)
196 mips32_insn.fp6_format.opcode = cop1x_op;
197 mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
198 mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
199 mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
200 mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
201 mips32_insn.fp6_format.func = func;
204 func = -1; /* Invalid */
205 op = insn.mm_fp5_format.op & 0x7;
206 if (op == mm_ldxc1_op)
208 else if (op == mm_sdxc1_op)
210 else if (op == mm_lwxc1_op)
212 else if (op == mm_swxc1_op)
216 mips32_insn.r_format.opcode = cop1x_op;
217 mips32_insn.r_format.rs =
218 insn.mm_fp5_format.base;
219 mips32_insn.r_format.rt =
220 insn.mm_fp5_format.index;
221 mips32_insn.r_format.rd = 0;
222 mips32_insn.r_format.re = insn.mm_fp5_format.fd;
223 mips32_insn.r_format.func = func;
228 op = -1; /* Invalid */
229 if (insn.mm_fp2_format.op == mm_fmovt_op)
231 else if (insn.mm_fp2_format.op == mm_fmovf_op)
234 mips32_insn.fp0_format.opcode = cop1_op;
235 mips32_insn.fp0_format.fmt =
236 sdps_format[insn.mm_fp2_format.fmt];
237 mips32_insn.fp0_format.ft =
238 (insn.mm_fp2_format.cc<<2) + op;
239 mips32_insn.fp0_format.fs =
240 insn.mm_fp2_format.fs;
241 mips32_insn.fp0_format.fd =
242 insn.mm_fp2_format.fd;
243 mips32_insn.fp0_format.func = fmovc_op;
248 func = -1; /* Invalid */
249 if (insn.mm_fp0_format.op == mm_fadd_op)
251 else if (insn.mm_fp0_format.op == mm_fsub_op)
253 else if (insn.mm_fp0_format.op == mm_fmul_op)
255 else if (insn.mm_fp0_format.op == mm_fdiv_op)
258 mips32_insn.fp0_format.opcode = cop1_op;
259 mips32_insn.fp0_format.fmt =
260 sdps_format[insn.mm_fp0_format.fmt];
261 mips32_insn.fp0_format.ft =
262 insn.mm_fp0_format.ft;
263 mips32_insn.fp0_format.fs =
264 insn.mm_fp0_format.fs;
265 mips32_insn.fp0_format.fd =
266 insn.mm_fp0_format.fd;
267 mips32_insn.fp0_format.func = func;
272 func = -1; /* Invalid */
273 if (insn.mm_fp0_format.op == mm_fmovn_op)
275 else if (insn.mm_fp0_format.op == mm_fmovz_op)
278 mips32_insn.fp0_format.opcode = cop1_op;
279 mips32_insn.fp0_format.fmt =
280 sdps_format[insn.mm_fp0_format.fmt];
281 mips32_insn.fp0_format.ft =
282 insn.mm_fp0_format.ft;
283 mips32_insn.fp0_format.fs =
284 insn.mm_fp0_format.fs;
285 mips32_insn.fp0_format.fd =
286 insn.mm_fp0_format.fd;
287 mips32_insn.fp0_format.func = func;
291 case mm_32f_73_op: /* POOL32FXF */
292 switch (insn.mm_fp1_format.op) {
297 if ((insn.mm_fp1_format.op & 0x7f) ==
302 mips32_insn.r_format.opcode = spec_op;
303 mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
304 mips32_insn.r_format.rt =
305 (insn.mm_fp4_format.cc << 2) + op;
306 mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
307 mips32_insn.r_format.re = 0;
308 mips32_insn.r_format.func = movc_op;
314 if ((insn.mm_fp1_format.op & 0x7f) ==
317 fmt = swl_format[insn.mm_fp3_format.fmt];
320 fmt = dwl_format[insn.mm_fp3_format.fmt];
322 mips32_insn.fp0_format.opcode = cop1_op;
323 mips32_insn.fp0_format.fmt = fmt;
324 mips32_insn.fp0_format.ft = 0;
325 mips32_insn.fp0_format.fs =
326 insn.mm_fp3_format.fs;
327 mips32_insn.fp0_format.fd =
328 insn.mm_fp3_format.rt;
329 mips32_insn.fp0_format.func = func;
337 if ((insn.mm_fp1_format.op & 0x7f) ==
340 else if ((insn.mm_fp1_format.op & 0x7f) ==
345 mips32_insn.fp0_format.opcode = cop1_op;
346 mips32_insn.fp0_format.fmt =
347 sdps_format[insn.mm_fp3_format.fmt];
348 mips32_insn.fp0_format.ft = 0;
349 mips32_insn.fp0_format.fs =
350 insn.mm_fp3_format.fs;
351 mips32_insn.fp0_format.fd =
352 insn.mm_fp3_format.rt;
353 mips32_insn.fp0_format.func = func;
365 if (insn.mm_fp1_format.op == mm_ffloorl_op)
367 else if (insn.mm_fp1_format.op == mm_ffloorw_op)
369 else if (insn.mm_fp1_format.op == mm_fceill_op)
371 else if (insn.mm_fp1_format.op == mm_fceilw_op)
373 else if (insn.mm_fp1_format.op == mm_ftruncl_op)
375 else if (insn.mm_fp1_format.op == mm_ftruncw_op)
377 else if (insn.mm_fp1_format.op == mm_froundl_op)
379 else if (insn.mm_fp1_format.op == mm_froundw_op)
381 else if (insn.mm_fp1_format.op == mm_fcvtl_op)
385 mips32_insn.fp0_format.opcode = cop1_op;
386 mips32_insn.fp0_format.fmt =
387 sd_format[insn.mm_fp1_format.fmt];
388 mips32_insn.fp0_format.ft = 0;
389 mips32_insn.fp0_format.fs =
390 insn.mm_fp1_format.fs;
391 mips32_insn.fp0_format.fd =
392 insn.mm_fp1_format.rt;
393 mips32_insn.fp0_format.func = func;
398 if (insn.mm_fp1_format.op == mm_frsqrt_op)
400 else if (insn.mm_fp1_format.op == mm_fsqrt_op)
404 mips32_insn.fp0_format.opcode = cop1_op;
405 mips32_insn.fp0_format.fmt =
406 sdps_format[insn.mm_fp1_format.fmt];
407 mips32_insn.fp0_format.ft = 0;
408 mips32_insn.fp0_format.fs =
409 insn.mm_fp1_format.fs;
410 mips32_insn.fp0_format.fd =
411 insn.mm_fp1_format.rt;
412 mips32_insn.fp0_format.func = func;
420 if (insn.mm_fp1_format.op == mm_mfc1_op)
422 else if (insn.mm_fp1_format.op == mm_mtc1_op)
424 else if (insn.mm_fp1_format.op == mm_cfc1_op)
426 else if (insn.mm_fp1_format.op == mm_ctc1_op)
428 else if (insn.mm_fp1_format.op == mm_mfhc1_op)
432 mips32_insn.fp1_format.opcode = cop1_op;
433 mips32_insn.fp1_format.op = op;
434 mips32_insn.fp1_format.rt =
435 insn.mm_fp1_format.rt;
436 mips32_insn.fp1_format.fs =
437 insn.mm_fp1_format.fs;
438 mips32_insn.fp1_format.fd = 0;
439 mips32_insn.fp1_format.func = 0;
445 case mm_32f_74_op: /* c.cond.fmt */
446 mips32_insn.fp0_format.opcode = cop1_op;
447 mips32_insn.fp0_format.fmt =
448 sdps_format[insn.mm_fp4_format.fmt];
449 mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
450 mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
451 mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
452 mips32_insn.fp0_format.func =
453 insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
463 *insn_ptr = mips32_insn;
467 int mm_isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
468 unsigned long *contpc)
470 union mips_instruction insn = (union mips_instruction)dec_insn.insn;
478 switch (insn.mm_i_format.opcode) {
480 if ((insn.mm_i_format.simmediate & MM_POOL32A_MINOR_MASK) ==
482 switch (insn.mm_i_format.simmediate >>
483 MM_POOL32A_MINOR_SHIFT) {
488 if (insn.mm_i_format.rt != 0) /* Not mm_jr */
489 regs->regs[insn.mm_i_format.rt] =
492 dec_insn.next_pc_inc;
493 *contpc = regs->regs[insn.mm_i_format.rs];
499 switch (insn.mm_i_format.rt) {
502 regs->regs[31] = regs->cp0_epc +
504 dec_insn.next_pc_inc;
507 if ((long)regs->regs[insn.mm_i_format.rs] < 0)
508 *contpc = regs->cp0_epc +
510 (insn.mm_i_format.simmediate << 1);
512 *contpc = regs->cp0_epc +
514 dec_insn.next_pc_inc;
518 regs->regs[31] = regs->cp0_epc +
520 dec_insn.next_pc_inc;
523 if ((long)regs->regs[insn.mm_i_format.rs] >= 0)
524 *contpc = regs->cp0_epc +
526 (insn.mm_i_format.simmediate << 1);
528 *contpc = regs->cp0_epc +
530 dec_insn.next_pc_inc;
533 if ((long)regs->regs[insn.mm_i_format.rs] <= 0)
534 *contpc = regs->cp0_epc +
536 (insn.mm_i_format.simmediate << 1);
538 *contpc = regs->cp0_epc +
540 dec_insn.next_pc_inc;
543 if ((long)regs->regs[insn.mm_i_format.rs] <= 0)
544 *contpc = regs->cp0_epc +
546 (insn.mm_i_format.simmediate << 1);
548 *contpc = regs->cp0_epc +
550 dec_insn.next_pc_inc;
560 asm volatile("cfc1\t%0,$31" : "=r" (fcr31));
562 fcr31 = current->thread.fpu.fcr31;
568 bit = (insn.mm_i_format.rs >> 2);
571 if (fcr31 & (1 << bit))
572 *contpc = regs->cp0_epc +
574 (insn.mm_i_format.simmediate << 1);
576 *contpc = regs->cp0_epc +
577 dec_insn.pc_inc + dec_insn.next_pc_inc;
582 switch (insn.mm_i_format.rt) {
585 regs->regs[31] = regs->cp0_epc +
586 dec_insn.pc_inc + dec_insn.next_pc_inc;
589 *contpc = regs->regs[insn.mm_i_format.rs];
594 if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] == 0)
595 *contpc = regs->cp0_epc +
597 (insn.mm_b1_format.simmediate << 1);
599 *contpc = regs->cp0_epc +
600 dec_insn.pc_inc + dec_insn.next_pc_inc;
603 if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] != 0)
604 *contpc = regs->cp0_epc +
606 (insn.mm_b1_format.simmediate << 1);
608 *contpc = regs->cp0_epc +
609 dec_insn.pc_inc + dec_insn.next_pc_inc;
612 *contpc = regs->cp0_epc + dec_insn.pc_inc +
613 (insn.mm_b0_format.simmediate << 1);
616 if (regs->regs[insn.mm_i_format.rs] ==
617 regs->regs[insn.mm_i_format.rt])
618 *contpc = regs->cp0_epc +
620 (insn.mm_i_format.simmediate << 1);
622 *contpc = regs->cp0_epc +
624 dec_insn.next_pc_inc;
627 if (regs->regs[insn.mm_i_format.rs] !=
628 regs->regs[insn.mm_i_format.rt])
629 *contpc = regs->cp0_epc +
631 (insn.mm_i_format.simmediate << 1);
633 *contpc = regs->cp0_epc +
634 dec_insn.pc_inc + dec_insn.next_pc_inc;
637 regs->regs[31] = regs->cp0_epc +
638 dec_insn.pc_inc + dec_insn.next_pc_inc;
639 *contpc = regs->cp0_epc + dec_insn.pc_inc;
642 *contpc |= (insn.j_format.target << 2);
646 regs->regs[31] = regs->cp0_epc +
647 dec_insn.pc_inc + dec_insn.next_pc_inc;
650 *contpc = regs->cp0_epc + dec_insn.pc_inc;
653 *contpc |= (insn.j_format.target << 1);
654 set_isa16_mode(*contpc);
661 * Redundant with logic already in kernel/branch.c,
662 * embedded in compute_return_epc. At some point,
663 * a single subroutine should be used across both
666 static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
667 unsigned long *contpc)
669 union mips_instruction insn = (union mips_instruction)dec_insn.insn;
671 unsigned int bit = 0;
673 switch (insn.i_format.opcode) {
675 switch (insn.r_format.func) {
677 regs->regs[insn.r_format.rd] =
678 regs->cp0_epc + dec_insn.pc_inc +
679 dec_insn.next_pc_inc;
682 *contpc = regs->regs[insn.r_format.rs];
687 switch (insn.i_format.rt) {
690 regs->regs[31] = regs->cp0_epc +
692 dec_insn.next_pc_inc;
696 if ((long)regs->regs[insn.i_format.rs] < 0)
697 *contpc = regs->cp0_epc +
699 (insn.i_format.simmediate << 2);
701 *contpc = regs->cp0_epc +
703 dec_insn.next_pc_inc;
707 regs->regs[31] = regs->cp0_epc +
709 dec_insn.next_pc_inc;
713 if ((long)regs->regs[insn.i_format.rs] >= 0)
714 *contpc = regs->cp0_epc +
716 (insn.i_format.simmediate << 2);
718 *contpc = regs->cp0_epc +
720 dec_insn.next_pc_inc;
727 regs->regs[31] = regs->cp0_epc +
729 dec_insn.next_pc_inc;
732 *contpc = regs->cp0_epc + dec_insn.pc_inc;
735 *contpc |= (insn.j_format.target << 2);
736 /* Set microMIPS mode bit: XOR for jalx. */
741 if (regs->regs[insn.i_format.rs] ==
742 regs->regs[insn.i_format.rt])
743 *contpc = regs->cp0_epc +
745 (insn.i_format.simmediate << 2);
747 *contpc = regs->cp0_epc +
749 dec_insn.next_pc_inc;
753 if (regs->regs[insn.i_format.rs] !=
754 regs->regs[insn.i_format.rt])
755 *contpc = regs->cp0_epc +
757 (insn.i_format.simmediate << 2);
759 *contpc = regs->cp0_epc +
761 dec_insn.next_pc_inc;
765 if ((long)regs->regs[insn.i_format.rs] <= 0)
766 *contpc = regs->cp0_epc +
768 (insn.i_format.simmediate << 2);
770 *contpc = regs->cp0_epc +
772 dec_insn.next_pc_inc;
776 if ((long)regs->regs[insn.i_format.rs] > 0)
777 *contpc = regs->cp0_epc +
779 (insn.i_format.simmediate << 2);
781 *contpc = regs->cp0_epc +
783 dec_insn.next_pc_inc;
785 #ifdef CONFIG_CPU_CAVIUM_OCTEON
786 case lwc2_op: /* This is bbit0 on Octeon */
787 if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
788 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
790 *contpc = regs->cp0_epc + 8;
792 case ldc2_op: /* This is bbit032 on Octeon */
793 if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
794 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
796 *contpc = regs->cp0_epc + 8;
798 case swc2_op: /* This is bbit1 on Octeon */
799 if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
800 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
802 *contpc = regs->cp0_epc + 8;
804 case sdc2_op: /* This is bbit132 on Octeon */
805 if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
806 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
808 *contpc = regs->cp0_epc + 8;
815 if (insn.i_format.rs == bc_op) {
818 asm volatile("cfc1\t%0,$31" : "=r" (fcr31));
820 fcr31 = current->thread.fpu.fcr31;
823 bit = (insn.i_format.rt >> 2);
826 switch (insn.i_format.rt & 3) {
829 if (~fcr31 & (1 << bit))
830 *contpc = regs->cp0_epc +
832 (insn.i_format.simmediate << 2);
834 *contpc = regs->cp0_epc +
836 dec_insn.next_pc_inc;
840 if (fcr31 & (1 << bit))
841 *contpc = regs->cp0_epc +
843 (insn.i_format.simmediate << 2);
845 *contpc = regs->cp0_epc +
847 dec_insn.next_pc_inc;
857 * In the Linux kernel, we support selection of FPR format on the
858 * basis of the Status.FR bit. If an FPU is not present, the FR bit
859 * is hardwired to zero, which would imply a 32-bit FPU even for
860 * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
861 * FPU emu is slow and bulky and optimizing this function offers fairly
862 * sizeable benefits so we try to be clever and make this function return
863 * a constant whenever possible, that is on 64-bit kernels without O32
864 * compatibility enabled and on 32-bit without 64-bit FPU support.
866 static inline int cop1_64bit(struct pt_regs *xcp)
868 #if defined(CONFIG_64BIT) && !defined(CONFIG_MIPS32_O32)
870 #elif defined(CONFIG_32BIT) && !defined(CONFIG_MIPS_O32_FP64_SUPPORT)
873 return !test_thread_flag(TIF_32BIT_FPREGS);
877 #define SIFROMREG(si, x) do { \
878 if (cop1_64bit(xcp)) \
879 (si) = get_fpr32(&ctx->fpr[x], 0); \
881 (si) = get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
884 #define SITOREG(si, x) do { \
885 if (cop1_64bit(xcp)) { \
887 set_fpr32(&ctx->fpr[x], 0, si); \
888 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
889 set_fpr32(&ctx->fpr[x], i, 0); \
891 set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
895 #define SIFROMHREG(si, x) ((si) = get_fpr32(&ctx->fpr[x], 1))
897 #define SITOHREG(si, x) do { \
899 set_fpr32(&ctx->fpr[x], 1, si); \
900 for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
901 set_fpr32(&ctx->fpr[x], i, 0); \
904 #define DIFROMREG(di, x) \
905 ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
907 #define DITOREG(di, x) do { \
909 fpr = (x) & ~(cop1_64bit(xcp) == 0); \
910 set_fpr64(&ctx->fpr[fpr], 0, di); \
911 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
912 set_fpr64(&ctx->fpr[fpr], i, 0); \
915 #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
916 #define SPTOREG(sp, x) SITOREG((sp).bits, x)
917 #define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
918 #define DPTOREG(dp, x) DITOREG((dp).bits, x)
921 * Emulate the single floating point instruction pointed at by EPC.
922 * Two instructions if the instruction is in a branch delay slot.
925 static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
926 struct mm_decoded_insn dec_insn, void *__user *fault_addr)
929 unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
933 /* XXX NEC Vr54xx bug workaround */
934 if (delay_slot(xcp)) {
935 if (dec_insn.micro_mips_mode) {
936 if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
937 clear_delay_slot(xcp);
939 if (!isBranchInstr(xcp, dec_insn, &contpc))
940 clear_delay_slot(xcp);
944 if (delay_slot(xcp)) {
946 * The instruction to be emulated is in a branch delay slot
947 * which means that we have to emulate the branch instruction
948 * BEFORE we do the cop1 instruction.
950 * This branch could be a COP1 branch, but in that case we
951 * would have had a trap for that instruction, and would not
952 * come through this route.
954 * Linux MIPS branch emulator operates on context, updating the
957 ir = dec_insn.next_insn; /* process delay slot instr */
958 pc_inc = dec_insn.next_pc_inc;
960 ir = dec_insn.insn; /* process current instr */
961 pc_inc = dec_insn.pc_inc;
965 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
966 * instructions, we want to convert microMIPS FPU instructions
967 * into MIPS32 instructions so that we could reuse all of the
968 * FPU emulation code.
970 * NOTE: We cannot do this for branch instructions since they
971 * are not a subset. Example: Cannot emulate a 16-bit
972 * aligned target address with a MIPS32 instruction.
974 if (dec_insn.micro_mips_mode) {
976 * If next instruction is a 16-bit instruction, then it
977 * it cannot be a FPU instruction. This could happen
978 * since we can be called for non-FPU instructions.
981 (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
987 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
988 MIPS_FPU_EMU_INC_STATS(emulated);
989 switch (MIPSInst_OPCODE(ir)) {
991 u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
995 MIPS_FPU_EMU_INC_STATS(loads);
997 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
998 MIPS_FPU_EMU_INC_STATS(errors);
1002 if (__get_user(val, va)) {
1003 MIPS_FPU_EMU_INC_STATS(errors);
1007 DITOREG(val, MIPSInst_RT(ir));
1012 u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1016 MIPS_FPU_EMU_INC_STATS(stores);
1017 DIFROMREG(val, MIPSInst_RT(ir));
1018 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
1019 MIPS_FPU_EMU_INC_STATS(errors);
1023 if (__put_user(val, va)) {
1024 MIPS_FPU_EMU_INC_STATS(errors);
1032 u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1036 MIPS_FPU_EMU_INC_STATS(loads);
1037 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
1038 MIPS_FPU_EMU_INC_STATS(errors);
1042 if (__get_user(val, va)) {
1043 MIPS_FPU_EMU_INC_STATS(errors);
1047 SITOREG(val, MIPSInst_RT(ir));
1052 u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1056 MIPS_FPU_EMU_INC_STATS(stores);
1057 SIFROMREG(val, MIPSInst_RT(ir));
1058 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1059 MIPS_FPU_EMU_INC_STATS(errors);
1063 if (__put_user(val, va)) {
1064 MIPS_FPU_EMU_INC_STATS(errors);
1072 switch (MIPSInst_RS(ir)) {
1074 #if defined(__mips64)
1076 /* copregister fs -> gpr[rt] */
1077 if (MIPSInst_RT(ir) != 0) {
1078 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1084 /* copregister fs <- rt */
1085 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1090 if (!cpu_has_mips_r2)
1093 /* copregister rd -> gpr[rt] */
1094 if (MIPSInst_RT(ir) != 0) {
1095 SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
1101 if (!cpu_has_mips_r2)
1104 /* copregister rd <- gpr[rt] */
1105 SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1109 /* copregister rd -> gpr[rt] */
1110 if (MIPSInst_RT(ir) != 0) {
1111 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1117 /* copregister rd <- rt */
1118 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1122 /* cop control register rd -> gpr[rt] */
1125 if (MIPSInst_RD(ir) == FPCREG_CSR) {
1127 value = (value & ~FPU_CSR_RM) |
1128 mips_rm[modeindex(value)];
1130 printk("%p gpr[%d]<-csr=%08x\n",
1131 (void *) (xcp->cp0_epc),
1132 MIPSInst_RT(ir), value);
1135 else if (MIPSInst_RD(ir) == FPCREG_RID)
1139 if (MIPSInst_RT(ir))
1140 xcp->regs[MIPSInst_RT(ir)] = value;
1145 /* copregister rd <- rt */
1148 if (MIPSInst_RT(ir) == 0)
1151 value = xcp->regs[MIPSInst_RT(ir)];
1153 /* we only have one writable control reg
1155 if (MIPSInst_RD(ir) == FPCREG_CSR) {
1157 printk("%p gpr[%d]->csr=%08x\n",
1158 (void *) (xcp->cp0_epc),
1159 MIPSInst_RT(ir), value);
1163 * Don't write reserved bits,
1164 * and convert to ieee library modes
1166 ctx->fcr31 = (value &
1167 ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
1168 ieee_rm[modeindex(value)];
1170 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1179 if (delay_slot(xcp))
1183 cond = ctx->fcr31 & fpucondbit[MIPSInst_RT(ir) >> 2];
1185 cond = ctx->fcr31 & FPU_CSR_COND;
1187 switch (MIPSInst_RT(ir) & 3) {
1198 /* thats an illegal instruction */
1202 set_delay_slot(xcp);
1204 /* branch taken: emulate dslot
1207 xcp->cp0_epc += dec_insn.pc_inc;
1209 contpc = MIPSInst_SIMM(ir);
1210 ir = dec_insn.next_insn;
1211 if (dec_insn.micro_mips_mode) {
1212 contpc = (xcp->cp0_epc + (contpc << 1));
1214 /* If 16-bit instruction, not FPU. */
1215 if ((dec_insn.next_pc_inc == 2) ||
1216 (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1219 * Since this instruction will
1220 * be put on the stack with
1221 * 32-bit words, get around
1222 * this problem by putting a
1223 * NOP16 as the second one.
1225 if (dec_insn.next_pc_inc == 2)
1226 ir = (ir & (~0xffff)) | MM_NOP16;
1229 * Single step the non-CP1
1230 * instruction in the dslot.
1232 return mips_dsemul(xcp, ir, contpc);
1235 contpc = (xcp->cp0_epc + (contpc << 2));
1237 switch (MIPSInst_OPCODE(ir)) {
1240 #if (__mips >= 2 || defined(__mips64))
1245 #if __mips >= 4 && __mips != 32
1248 /* its one of ours */
1252 if (MIPSInst_FUNC(ir) == movc_op)
1259 * Single step the non-cp1
1260 * instruction in the dslot
1262 return mips_dsemul(xcp, ir, contpc);
1265 /* branch not taken */
1268 * branch likely nullifies
1269 * dslot if not taken
1271 xcp->cp0_epc += dec_insn.pc_inc;
1272 contpc += dec_insn.pc_inc;
1274 * else continue & execute
1275 * dslot as normal insn
1283 if (!(MIPSInst_RS(ir) & 0x10))
1288 /* a real fpu computation instruction */
1289 if ((sig = fpu_emu(xcp, ctx, ir)))
1295 #if __mips >= 4 && __mips != 32
1297 int sig = fpux_emu(xcp, ctx, ir, fault_addr);
1306 if (MIPSInst_FUNC(ir) != movc_op)
1308 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
1309 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
1310 xcp->regs[MIPSInst_RD(ir)] =
1311 xcp->regs[MIPSInst_RS(ir)];
1321 xcp->cp0_epc = contpc;
1322 clear_delay_slot(xcp);
1328 * Conversion table from MIPS compare ops 48-63
1329 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
1331 static const unsigned char cmptab[8] = {
1332 0, /* cmp_0 (sig) cmp_sf */
1333 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
1334 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
1335 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
1336 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
1337 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
1338 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
1339 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
1343 #if __mips >= 4 && __mips != 32
1346 * Additional MIPS4 instructions
1349 #define DEF3OP(name, p, f1, f2, f3) \
1350 static union ieee754##p fpemu_##p##_##name(union ieee754##p r, union ieee754##p s, \
1351 union ieee754##p t) \
1353 struct _ieee754_csr ieee754_csr_save; \
1355 ieee754_csr_save = ieee754_csr; \
1357 ieee754_csr_save.cx |= ieee754_csr.cx; \
1358 ieee754_csr_save.sx |= ieee754_csr.sx; \
1360 ieee754_csr.cx |= ieee754_csr_save.cx; \
1361 ieee754_csr.sx |= ieee754_csr_save.sx; \
1365 static union ieee754dp fpemu_dp_recip(union ieee754dp d)
1367 return ieee754dp_div(ieee754dp_one(0), d);
1370 static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
1372 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
1375 static union ieee754sp fpemu_sp_recip(union ieee754sp s)
1377 return ieee754sp_div(ieee754sp_one(0), s);
1380 static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
1382 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
1385 DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
1386 DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
1387 DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
1388 DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
1389 DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
1390 DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
1391 DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
1392 DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
1394 static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1395 mips_instruction ir, void *__user *fault_addr)
1397 unsigned rcsr = 0; /* resulting csr */
1399 MIPS_FPU_EMU_INC_STATS(cp1xops);
1401 switch (MIPSInst_FMA_FFMT(ir)) {
1402 case s_fmt:{ /* 0 */
1404 union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
1405 union ieee754sp fd, fr, fs, ft;
1409 switch (MIPSInst_FUNC(ir)) {
1411 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1412 xcp->regs[MIPSInst_FT(ir)]);
1414 MIPS_FPU_EMU_INC_STATS(loads);
1415 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
1416 MIPS_FPU_EMU_INC_STATS(errors);
1420 if (__get_user(val, va)) {
1421 MIPS_FPU_EMU_INC_STATS(errors);
1425 SITOREG(val, MIPSInst_FD(ir));
1429 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1430 xcp->regs[MIPSInst_FT(ir)]);
1432 MIPS_FPU_EMU_INC_STATS(stores);
1434 SIFROMREG(val, MIPSInst_FS(ir));
1435 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1436 MIPS_FPU_EMU_INC_STATS(errors);
1440 if (put_user(val, va)) {
1441 MIPS_FPU_EMU_INC_STATS(errors);
1448 handler = fpemu_sp_madd;
1451 handler = fpemu_sp_msub;
1454 handler = fpemu_sp_nmadd;
1457 handler = fpemu_sp_nmsub;
1461 SPFROMREG(fr, MIPSInst_FR(ir));
1462 SPFROMREG(fs, MIPSInst_FS(ir));
1463 SPFROMREG(ft, MIPSInst_FT(ir));
1464 fd = (*handler) (fr, fs, ft);
1465 SPTOREG(fd, MIPSInst_FD(ir));
1468 if (ieee754_cxtest(IEEE754_INEXACT))
1469 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1470 if (ieee754_cxtest(IEEE754_UNDERFLOW))
1471 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1472 if (ieee754_cxtest(IEEE754_OVERFLOW))
1473 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1474 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
1475 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1477 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1478 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1479 /*printk ("SIGFPE: fpu csr = %08x\n",
1492 case d_fmt:{ /* 1 */
1493 union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
1494 union ieee754dp fd, fr, fs, ft;
1498 switch (MIPSInst_FUNC(ir)) {
1500 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1501 xcp->regs[MIPSInst_FT(ir)]);
1503 MIPS_FPU_EMU_INC_STATS(loads);
1504 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
1505 MIPS_FPU_EMU_INC_STATS(errors);
1509 if (__get_user(val, va)) {
1510 MIPS_FPU_EMU_INC_STATS(errors);
1514 DITOREG(val, MIPSInst_FD(ir));
1518 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1519 xcp->regs[MIPSInst_FT(ir)]);
1521 MIPS_FPU_EMU_INC_STATS(stores);
1522 DIFROMREG(val, MIPSInst_FS(ir));
1523 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
1524 MIPS_FPU_EMU_INC_STATS(errors);
1528 if (__put_user(val, va)) {
1529 MIPS_FPU_EMU_INC_STATS(errors);
1536 handler = fpemu_dp_madd;
1539 handler = fpemu_dp_msub;
1542 handler = fpemu_dp_nmadd;
1545 handler = fpemu_dp_nmsub;
1549 DPFROMREG(fr, MIPSInst_FR(ir));
1550 DPFROMREG(fs, MIPSInst_FS(ir));
1551 DPFROMREG(ft, MIPSInst_FT(ir));
1552 fd = (*handler) (fr, fs, ft);
1553 DPTOREG(fd, MIPSInst_FD(ir));
1563 if (MIPSInst_FUNC(ir) != pfetch_op)
1566 /* ignore prefx operation */
1580 * Emulate a single COP1 arithmetic instruction.
1582 static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1583 mips_instruction ir)
1585 int rfmt; /* resulting format */
1586 unsigned rcsr = 0; /* resulting csr */
1595 } rv; /* resulting value */
1597 MIPS_FPU_EMU_INC_STATS(cp1ops);
1598 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
1599 case s_fmt:{ /* 0 */
1601 union ieee754sp(*b) (union ieee754sp, union ieee754sp);
1602 union ieee754sp(*u) (union ieee754sp);
1605 switch (MIPSInst_FUNC(ir)) {
1608 handler.b = ieee754sp_add;
1611 handler.b = ieee754sp_sub;
1614 handler.b = ieee754sp_mul;
1617 handler.b = ieee754sp_div;
1621 #if __mips >= 2 || defined(__mips64)
1623 handler.u = ieee754sp_sqrt;
1626 #if __mips >= 4 && __mips != 32
1628 handler.u = fpemu_sp_rsqrt;
1631 handler.u = fpemu_sp_recip;
1636 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1637 if (((ctx->fcr31 & cond) != 0) !=
1638 ((MIPSInst_FT(ir) & 1) != 0))
1640 SPFROMREG(rv.s, MIPSInst_FS(ir));
1643 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1645 SPFROMREG(rv.s, MIPSInst_FS(ir));
1648 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1650 SPFROMREG(rv.s, MIPSInst_FS(ir));
1654 handler.u = ieee754sp_abs;
1657 handler.u = ieee754sp_neg;
1661 SPFROMREG(rv.s, MIPSInst_FS(ir));
1664 /* binary op on handler */
1667 union ieee754sp fs, ft;
1669 SPFROMREG(fs, MIPSInst_FS(ir));
1670 SPFROMREG(ft, MIPSInst_FT(ir));
1672 rv.s = (*handler.b) (fs, ft);
1679 SPFROMREG(fs, MIPSInst_FS(ir));
1680 rv.s = (*handler.u) (fs);
1684 if (ieee754_cxtest(IEEE754_INEXACT))
1685 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1686 if (ieee754_cxtest(IEEE754_UNDERFLOW))
1687 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1688 if (ieee754_cxtest(IEEE754_OVERFLOW))
1689 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1690 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE))
1691 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
1692 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
1693 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1696 /* unary conv ops */
1698 return SIGILL; /* not defined */
1702 SPFROMREG(fs, MIPSInst_FS(ir));
1703 rv.d = ieee754dp_fsp(fs);
1710 SPFROMREG(fs, MIPSInst_FS(ir));
1711 rv.w = ieee754sp_tint(fs);
1716 #if __mips >= 2 || defined(__mips64)
1721 unsigned int oldrm = ieee754_csr.rm;
1724 SPFROMREG(fs, MIPSInst_FS(ir));
1725 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
1726 rv.w = ieee754sp_tint(fs);
1727 ieee754_csr.rm = oldrm;
1731 #endif /* __mips >= 2 */
1733 #if defined(__mips64)
1737 SPFROMREG(fs, MIPSInst_FS(ir));
1738 rv.l = ieee754sp_tlong(fs);
1747 unsigned int oldrm = ieee754_csr.rm;
1750 SPFROMREG(fs, MIPSInst_FS(ir));
1751 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
1752 rv.l = ieee754sp_tlong(fs);
1753 ieee754_csr.rm = oldrm;
1757 #endif /* defined(__mips64) */
1760 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1761 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1762 union ieee754sp fs, ft;
1764 SPFROMREG(fs, MIPSInst_FS(ir));
1765 SPFROMREG(ft, MIPSInst_FT(ir));
1766 rv.w = ieee754sp_cmp(fs, ft,
1767 cmptab[cmpop & 0x7], cmpop & 0x8);
1769 if ((cmpop & 0x8) && ieee754_cxtest
1770 (IEEE754_INVALID_OPERATION))
1771 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1786 union ieee754dp(*b) (union ieee754dp, union ieee754dp);
1787 union ieee754dp(*u) (union ieee754dp);
1790 switch (MIPSInst_FUNC(ir)) {
1793 handler.b = ieee754dp_add;
1796 handler.b = ieee754dp_sub;
1799 handler.b = ieee754dp_mul;
1802 handler.b = ieee754dp_div;
1806 #if __mips >= 2 || defined(__mips64)
1808 handler.u = ieee754dp_sqrt;
1811 #if __mips >= 4 && __mips != 32
1813 handler.u = fpemu_dp_rsqrt;
1816 handler.u = fpemu_dp_recip;
1821 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1822 if (((ctx->fcr31 & cond) != 0) !=
1823 ((MIPSInst_FT(ir) & 1) != 0))
1825 DPFROMREG(rv.d, MIPSInst_FS(ir));
1828 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1830 DPFROMREG(rv.d, MIPSInst_FS(ir));
1833 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1835 DPFROMREG(rv.d, MIPSInst_FS(ir));
1839 handler.u = ieee754dp_abs;
1843 handler.u = ieee754dp_neg;
1848 DPFROMREG(rv.d, MIPSInst_FS(ir));
1851 /* binary op on handler */
1853 union ieee754dp fs, ft;
1855 DPFROMREG(fs, MIPSInst_FS(ir));
1856 DPFROMREG(ft, MIPSInst_FT(ir));
1858 rv.d = (*handler.b) (fs, ft);
1864 DPFROMREG(fs, MIPSInst_FS(ir));
1865 rv.d = (*handler.u) (fs);
1869 /* unary conv ops */
1873 DPFROMREG(fs, MIPSInst_FS(ir));
1874 rv.s = ieee754sp_fdp(fs);
1879 return SIGILL; /* not defined */
1884 DPFROMREG(fs, MIPSInst_FS(ir));
1885 rv.w = ieee754dp_tint(fs); /* wrong */
1890 #if __mips >= 2 || defined(__mips64)
1895 unsigned int oldrm = ieee754_csr.rm;
1898 DPFROMREG(fs, MIPSInst_FS(ir));
1899 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
1900 rv.w = ieee754dp_tint(fs);
1901 ieee754_csr.rm = oldrm;
1907 #if defined(__mips64)
1911 DPFROMREG(fs, MIPSInst_FS(ir));
1912 rv.l = ieee754dp_tlong(fs);
1921 unsigned int oldrm = ieee754_csr.rm;
1924 DPFROMREG(fs, MIPSInst_FS(ir));
1925 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
1926 rv.l = ieee754dp_tlong(fs);
1927 ieee754_csr.rm = oldrm;
1931 #endif /* __mips >= 3 */
1934 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1935 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1936 union ieee754dp fs, ft;
1938 DPFROMREG(fs, MIPSInst_FS(ir));
1939 DPFROMREG(ft, MIPSInst_FT(ir));
1940 rv.w = ieee754dp_cmp(fs, ft,
1941 cmptab[cmpop & 0x7], cmpop & 0x8);
1946 (IEEE754_INVALID_OPERATION))
1947 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1963 switch (MIPSInst_FUNC(ir)) {
1965 /* convert word to single precision real */
1966 SPFROMREG(fs, MIPSInst_FS(ir));
1967 rv.s = ieee754sp_fint(fs.bits);
1971 /* convert word to double precision real */
1972 SPFROMREG(fs, MIPSInst_FS(ir));
1973 rv.d = ieee754dp_fint(fs.bits);
1982 #if defined(__mips64)
1985 DIFROMREG(bits, MIPSInst_FS(ir));
1987 switch (MIPSInst_FUNC(ir)) {
1989 /* convert long to single precision real */
1990 rv.s = ieee754sp_flong(bits);
1994 /* convert long to double precision real */
1995 rv.d = ieee754dp_flong(bits);
2010 * Update the fpu CSR register for this operation.
2011 * If an exception is required, generate a tidy SIGFPE exception,
2012 * without updating the result register.
2013 * Note: cause exception bits do not accumulate, they are rewritten
2014 * for each op; only the flag/sticky bits accumulate.
2016 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
2017 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
2018 /*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */
2023 * Now we can safely write the result back to the register file.
2028 cond = fpucondbit[MIPSInst_FD(ir) >> 2];
2030 cond = FPU_CSR_COND;
2035 ctx->fcr31 &= ~cond;
2039 DPTOREG(rv.d, MIPSInst_FD(ir));
2042 SPTOREG(rv.s, MIPSInst_FD(ir));
2045 SITOREG(rv.w, MIPSInst_FD(ir));
2047 #if defined(__mips64)
2049 DITOREG(rv.l, MIPSInst_FD(ir));
2059 int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
2060 int has_fpu, void *__user *fault_addr)
2062 unsigned long oldepc, prevepc;
2063 struct mm_decoded_insn dec_insn;
2068 oldepc = xcp->cp0_epc;
2070 prevepc = xcp->cp0_epc;
2072 if (get_isa16_mode(prevepc) && cpu_has_mmips) {
2074 * Get next 2 microMIPS instructions and convert them
2075 * into 32-bit instructions.
2077 if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2078 (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2079 (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2080 (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2081 MIPS_FPU_EMU_INC_STATS(errors);
2086 /* Get first instruction. */
2087 if (mm_insn_16bit(*instr_ptr)) {
2088 /* Duplicate the half-word. */
2089 dec_insn.insn = (*instr_ptr << 16) |
2091 /* 16-bit instruction. */
2092 dec_insn.pc_inc = 2;
2095 dec_insn.insn = (*instr_ptr << 16) |
2097 /* 32-bit instruction. */
2098 dec_insn.pc_inc = 4;
2101 /* Get second instruction. */
2102 if (mm_insn_16bit(*instr_ptr)) {
2103 /* Duplicate the half-word. */
2104 dec_insn.next_insn = (*instr_ptr << 16) |
2106 /* 16-bit instruction. */
2107 dec_insn.next_pc_inc = 2;
2109 dec_insn.next_insn = (*instr_ptr << 16) |
2111 /* 32-bit instruction. */
2112 dec_insn.next_pc_inc = 4;
2114 dec_insn.micro_mips_mode = 1;
2116 if ((get_user(dec_insn.insn,
2117 (mips_instruction __user *) xcp->cp0_epc)) ||
2118 (get_user(dec_insn.next_insn,
2119 (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2120 MIPS_FPU_EMU_INC_STATS(errors);
2123 dec_insn.pc_inc = 4;
2124 dec_insn.next_pc_inc = 4;
2125 dec_insn.micro_mips_mode = 0;
2128 if ((dec_insn.insn == 0) ||
2129 ((dec_insn.pc_inc == 2) &&
2130 ((dec_insn.insn & 0xffff) == MM_NOP16)))
2131 xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
2134 * The 'ieee754_csr' is an alias of
2135 * ctx->fcr31. No need to copy ctx->fcr31 to
2136 * ieee754_csr. But ieee754_csr.rm is ieee
2137 * library modes. (not mips rounding mode)
2139 /* convert to ieee library modes */
2140 ieee754_csr.rm = ieee_rm[ieee754_csr.rm];
2141 sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
2142 /* revert to mips rounding mode */
2143 ieee754_csr.rm = mips_rm[ieee754_csr.rm];
2152 } while (xcp->cp0_epc > prevepc);
2154 /* SIGILL indicates a non-fpu instruction */
2155 if (sig == SIGILL && xcp->cp0_epc != oldepc)
2156 /* but if epc has advanced, then ignore it */
2162 #ifdef CONFIG_DEBUG_FS
2164 static int fpuemu_stat_get(void *data, u64 *val)
2167 unsigned long sum = 0;
2168 for_each_online_cpu(cpu) {
2169 struct mips_fpu_emulator_stats *ps;
2171 ps = &per_cpu(fpuemustats, cpu);
2172 pv = (void *)ps + (unsigned long)data;
2173 sum += local_read(pv);
2178 DEFINE_SIMPLE_ATTRIBUTE(fops_fpuemu_stat, fpuemu_stat_get, NULL, "%llu\n");
2180 extern struct dentry *mips_debugfs_dir;
2181 static int __init debugfs_fpuemu(void)
2183 struct dentry *d, *dir;
2185 if (!mips_debugfs_dir)
2187 dir = debugfs_create_dir("fpuemustats", mips_debugfs_dir);
2191 #define FPU_STAT_CREATE(M) \
2193 d = debugfs_create_file(#M , S_IRUGO, dir, \
2194 (void *)offsetof(struct mips_fpu_emulator_stats, M), \
2195 &fops_fpuemu_stat); \
2200 FPU_STAT_CREATE(emulated);
2201 FPU_STAT_CREATE(loads);
2202 FPU_STAT_CREATE(stores);
2203 FPU_STAT_CREATE(cp1ops);
2204 FPU_STAT_CREATE(cp1xops);
2205 FPU_STAT_CREATE(errors);
2209 __initcall(debugfs_fpuemu);