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 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 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/kconfig.h>
39 #include <linux/percpu-defs.h>
40 #include <linux/perf_event.h>
42 #include <asm/branch.h>
44 #include <asm/ptrace.h>
45 #include <asm/signal.h>
46 #include <asm/uaccess.h>
48 #include <asm/cpu-info.h>
49 #include <asm/processor.h>
50 #include <asm/fpu_emulator.h>
52 #include <asm/mips-r2-to-r6-emul.h>
56 /* Function which emulates a floating point instruction. */
58 static int fpu_emu(struct pt_regs
*, struct mips_fpu_struct
*,
61 static int fpux_emu(struct pt_regs
*,
62 struct mips_fpu_struct
*, mips_instruction
, void *__user
*);
64 /* Control registers */
66 #define FPCREG_RID 0 /* $0 = revision id */
67 #define FPCREG_FCCR 25 /* $25 = fccr */
68 #define FPCREG_FEXR 26 /* $26 = fexr */
69 #define FPCREG_FENR 28 /* $28 = fenr */
70 #define FPCREG_CSR 31 /* $31 = csr */
72 /* convert condition code register number to csr bit */
73 const unsigned int fpucondbit
[8] = {
84 /* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
85 static const int sd_format
[] = {16, 17, 0, 0, 0, 0, 0, 0};
86 static const int sdps_format
[] = {16, 17, 22, 0, 0, 0, 0, 0};
87 static const int dwl_format
[] = {17, 20, 21, 0, 0, 0, 0, 0};
88 static const int swl_format
[] = {16, 20, 21, 0, 0, 0, 0, 0};
91 * This functions translates a 32-bit microMIPS instruction
92 * into a 32-bit MIPS32 instruction. Returns 0 on success
93 * and SIGILL otherwise.
95 static int microMIPS32_to_MIPS32(union mips_instruction
*insn_ptr
)
97 union mips_instruction insn
= *insn_ptr
;
98 union mips_instruction mips32_insn
= insn
;
101 switch (insn
.mm_i_format
.opcode
) {
103 mips32_insn
.mm_i_format
.opcode
= ldc1_op
;
104 mips32_insn
.mm_i_format
.rt
= insn
.mm_i_format
.rs
;
105 mips32_insn
.mm_i_format
.rs
= insn
.mm_i_format
.rt
;
108 mips32_insn
.mm_i_format
.opcode
= lwc1_op
;
109 mips32_insn
.mm_i_format
.rt
= insn
.mm_i_format
.rs
;
110 mips32_insn
.mm_i_format
.rs
= insn
.mm_i_format
.rt
;
113 mips32_insn
.mm_i_format
.opcode
= sdc1_op
;
114 mips32_insn
.mm_i_format
.rt
= insn
.mm_i_format
.rs
;
115 mips32_insn
.mm_i_format
.rs
= insn
.mm_i_format
.rt
;
118 mips32_insn
.mm_i_format
.opcode
= swc1_op
;
119 mips32_insn
.mm_i_format
.rt
= insn
.mm_i_format
.rs
;
120 mips32_insn
.mm_i_format
.rs
= insn
.mm_i_format
.rt
;
123 /* NOTE: offset is << by 1 if in microMIPS mode. */
124 if ((insn
.mm_i_format
.rt
== mm_bc1f_op
) ||
125 (insn
.mm_i_format
.rt
== mm_bc1t_op
)) {
126 mips32_insn
.fb_format
.opcode
= cop1_op
;
127 mips32_insn
.fb_format
.bc
= bc_op
;
128 mips32_insn
.fb_format
.flag
=
129 (insn
.mm_i_format
.rt
== mm_bc1t_op
) ? 1 : 0;
134 switch (insn
.mm_fp0_format
.func
) {
143 op
= insn
.mm_fp0_format
.func
;
144 if (op
== mm_32f_01_op
)
146 else if (op
== mm_32f_11_op
)
148 else if (op
== mm_32f_02_op
)
150 else if (op
== mm_32f_12_op
)
152 else if (op
== mm_32f_41_op
)
154 else if (op
== mm_32f_51_op
)
156 else if (op
== mm_32f_42_op
)
160 mips32_insn
.fp6_format
.opcode
= cop1x_op
;
161 mips32_insn
.fp6_format
.fr
= insn
.mm_fp6_format
.fr
;
162 mips32_insn
.fp6_format
.ft
= insn
.mm_fp6_format
.ft
;
163 mips32_insn
.fp6_format
.fs
= insn
.mm_fp6_format
.fs
;
164 mips32_insn
.fp6_format
.fd
= insn
.mm_fp6_format
.fd
;
165 mips32_insn
.fp6_format
.func
= func
;
168 func
= -1; /* Invalid */
169 op
= insn
.mm_fp5_format
.op
& 0x7;
170 if (op
== mm_ldxc1_op
)
172 else if (op
== mm_sdxc1_op
)
174 else if (op
== mm_lwxc1_op
)
176 else if (op
== mm_swxc1_op
)
180 mips32_insn
.r_format
.opcode
= cop1x_op
;
181 mips32_insn
.r_format
.rs
=
182 insn
.mm_fp5_format
.base
;
183 mips32_insn
.r_format
.rt
=
184 insn
.mm_fp5_format
.index
;
185 mips32_insn
.r_format
.rd
= 0;
186 mips32_insn
.r_format
.re
= insn
.mm_fp5_format
.fd
;
187 mips32_insn
.r_format
.func
= func
;
192 op
= -1; /* Invalid */
193 if (insn
.mm_fp2_format
.op
== mm_fmovt_op
)
195 else if (insn
.mm_fp2_format
.op
== mm_fmovf_op
)
198 mips32_insn
.fp0_format
.opcode
= cop1_op
;
199 mips32_insn
.fp0_format
.fmt
=
200 sdps_format
[insn
.mm_fp2_format
.fmt
];
201 mips32_insn
.fp0_format
.ft
=
202 (insn
.mm_fp2_format
.cc
<<2) + op
;
203 mips32_insn
.fp0_format
.fs
=
204 insn
.mm_fp2_format
.fs
;
205 mips32_insn
.fp0_format
.fd
=
206 insn
.mm_fp2_format
.fd
;
207 mips32_insn
.fp0_format
.func
= fmovc_op
;
212 func
= -1; /* Invalid */
213 if (insn
.mm_fp0_format
.op
== mm_fadd_op
)
215 else if (insn
.mm_fp0_format
.op
== mm_fsub_op
)
217 else if (insn
.mm_fp0_format
.op
== mm_fmul_op
)
219 else if (insn
.mm_fp0_format
.op
== mm_fdiv_op
)
222 mips32_insn
.fp0_format
.opcode
= cop1_op
;
223 mips32_insn
.fp0_format
.fmt
=
224 sdps_format
[insn
.mm_fp0_format
.fmt
];
225 mips32_insn
.fp0_format
.ft
=
226 insn
.mm_fp0_format
.ft
;
227 mips32_insn
.fp0_format
.fs
=
228 insn
.mm_fp0_format
.fs
;
229 mips32_insn
.fp0_format
.fd
=
230 insn
.mm_fp0_format
.fd
;
231 mips32_insn
.fp0_format
.func
= func
;
236 func
= -1; /* Invalid */
237 if (insn
.mm_fp0_format
.op
== mm_fmovn_op
)
239 else if (insn
.mm_fp0_format
.op
== mm_fmovz_op
)
242 mips32_insn
.fp0_format
.opcode
= cop1_op
;
243 mips32_insn
.fp0_format
.fmt
=
244 sdps_format
[insn
.mm_fp0_format
.fmt
];
245 mips32_insn
.fp0_format
.ft
=
246 insn
.mm_fp0_format
.ft
;
247 mips32_insn
.fp0_format
.fs
=
248 insn
.mm_fp0_format
.fs
;
249 mips32_insn
.fp0_format
.fd
=
250 insn
.mm_fp0_format
.fd
;
251 mips32_insn
.fp0_format
.func
= func
;
255 case mm_32f_73_op
: /* POOL32FXF */
256 switch (insn
.mm_fp1_format
.op
) {
261 if ((insn
.mm_fp1_format
.op
& 0x7f) ==
266 mips32_insn
.r_format
.opcode
= spec_op
;
267 mips32_insn
.r_format
.rs
= insn
.mm_fp4_format
.fs
;
268 mips32_insn
.r_format
.rt
=
269 (insn
.mm_fp4_format
.cc
<< 2) + op
;
270 mips32_insn
.r_format
.rd
= insn
.mm_fp4_format
.rt
;
271 mips32_insn
.r_format
.re
= 0;
272 mips32_insn
.r_format
.func
= movc_op
;
278 if ((insn
.mm_fp1_format
.op
& 0x7f) ==
281 fmt
= swl_format
[insn
.mm_fp3_format
.fmt
];
284 fmt
= dwl_format
[insn
.mm_fp3_format
.fmt
];
286 mips32_insn
.fp0_format
.opcode
= cop1_op
;
287 mips32_insn
.fp0_format
.fmt
= fmt
;
288 mips32_insn
.fp0_format
.ft
= 0;
289 mips32_insn
.fp0_format
.fs
=
290 insn
.mm_fp3_format
.fs
;
291 mips32_insn
.fp0_format
.fd
=
292 insn
.mm_fp3_format
.rt
;
293 mips32_insn
.fp0_format
.func
= func
;
301 if ((insn
.mm_fp1_format
.op
& 0x7f) ==
304 else if ((insn
.mm_fp1_format
.op
& 0x7f) ==
309 mips32_insn
.fp0_format
.opcode
= cop1_op
;
310 mips32_insn
.fp0_format
.fmt
=
311 sdps_format
[insn
.mm_fp3_format
.fmt
];
312 mips32_insn
.fp0_format
.ft
= 0;
313 mips32_insn
.fp0_format
.fs
=
314 insn
.mm_fp3_format
.fs
;
315 mips32_insn
.fp0_format
.fd
=
316 insn
.mm_fp3_format
.rt
;
317 mips32_insn
.fp0_format
.func
= func
;
329 if (insn
.mm_fp1_format
.op
== mm_ffloorl_op
)
331 else if (insn
.mm_fp1_format
.op
== mm_ffloorw_op
)
333 else if (insn
.mm_fp1_format
.op
== mm_fceill_op
)
335 else if (insn
.mm_fp1_format
.op
== mm_fceilw_op
)
337 else if (insn
.mm_fp1_format
.op
== mm_ftruncl_op
)
339 else if (insn
.mm_fp1_format
.op
== mm_ftruncw_op
)
341 else if (insn
.mm_fp1_format
.op
== mm_froundl_op
)
343 else if (insn
.mm_fp1_format
.op
== mm_froundw_op
)
345 else if (insn
.mm_fp1_format
.op
== mm_fcvtl_op
)
349 mips32_insn
.fp0_format
.opcode
= cop1_op
;
350 mips32_insn
.fp0_format
.fmt
=
351 sd_format
[insn
.mm_fp1_format
.fmt
];
352 mips32_insn
.fp0_format
.ft
= 0;
353 mips32_insn
.fp0_format
.fs
=
354 insn
.mm_fp1_format
.fs
;
355 mips32_insn
.fp0_format
.fd
=
356 insn
.mm_fp1_format
.rt
;
357 mips32_insn
.fp0_format
.func
= func
;
362 if (insn
.mm_fp1_format
.op
== mm_frsqrt_op
)
364 else if (insn
.mm_fp1_format
.op
== mm_fsqrt_op
)
368 mips32_insn
.fp0_format
.opcode
= cop1_op
;
369 mips32_insn
.fp0_format
.fmt
=
370 sdps_format
[insn
.mm_fp1_format
.fmt
];
371 mips32_insn
.fp0_format
.ft
= 0;
372 mips32_insn
.fp0_format
.fs
=
373 insn
.mm_fp1_format
.fs
;
374 mips32_insn
.fp0_format
.fd
=
375 insn
.mm_fp1_format
.rt
;
376 mips32_insn
.fp0_format
.func
= func
;
384 if (insn
.mm_fp1_format
.op
== mm_mfc1_op
)
386 else if (insn
.mm_fp1_format
.op
== mm_mtc1_op
)
388 else if (insn
.mm_fp1_format
.op
== mm_cfc1_op
)
390 else if (insn
.mm_fp1_format
.op
== mm_ctc1_op
)
392 else if (insn
.mm_fp1_format
.op
== mm_mfhc1_op
)
396 mips32_insn
.fp1_format
.opcode
= cop1_op
;
397 mips32_insn
.fp1_format
.op
= op
;
398 mips32_insn
.fp1_format
.rt
=
399 insn
.mm_fp1_format
.rt
;
400 mips32_insn
.fp1_format
.fs
=
401 insn
.mm_fp1_format
.fs
;
402 mips32_insn
.fp1_format
.fd
= 0;
403 mips32_insn
.fp1_format
.func
= 0;
409 case mm_32f_74_op
: /* c.cond.fmt */
410 mips32_insn
.fp0_format
.opcode
= cop1_op
;
411 mips32_insn
.fp0_format
.fmt
=
412 sdps_format
[insn
.mm_fp4_format
.fmt
];
413 mips32_insn
.fp0_format
.ft
= insn
.mm_fp4_format
.rt
;
414 mips32_insn
.fp0_format
.fs
= insn
.mm_fp4_format
.fs
;
415 mips32_insn
.fp0_format
.fd
= insn
.mm_fp4_format
.cc
<< 2;
416 mips32_insn
.fp0_format
.func
=
417 insn
.mm_fp4_format
.cond
| MM_MIPS32_COND_FC
;
427 *insn_ptr
= mips32_insn
;
432 * Redundant with logic already in kernel/branch.c,
433 * embedded in compute_return_epc. At some point,
434 * a single subroutine should be used across both
437 static int isBranchInstr(struct pt_regs
*regs
, struct mm_decoded_insn dec_insn
,
438 unsigned long *contpc
)
440 union mips_instruction insn
= (union mips_instruction
)dec_insn
.insn
;
442 unsigned int bit
= 0;
444 switch (insn
.i_format
.opcode
) {
446 switch (insn
.r_format
.func
) {
448 regs
->regs
[insn
.r_format
.rd
] =
449 regs
->cp0_epc
+ dec_insn
.pc_inc
+
450 dec_insn
.next_pc_inc
;
453 /* For R6, JR already emulated in jalr_op */
454 if (NO_R6EMU
&& insn
.r_format
.func
== jr_op
)
456 *contpc
= regs
->regs
[insn
.r_format
.rs
];
461 switch (insn
.i_format
.rt
) {
464 if (NO_R6EMU
&& (insn
.i_format
.rs
||
465 insn
.i_format
.rt
== bltzall_op
))
468 regs
->regs
[31] = regs
->cp0_epc
+
470 dec_insn
.next_pc_inc
;
476 if ((long)regs
->regs
[insn
.i_format
.rs
] < 0)
477 *contpc
= regs
->cp0_epc
+
479 (insn
.i_format
.simmediate
<< 2);
481 *contpc
= regs
->cp0_epc
+
483 dec_insn
.next_pc_inc
;
487 if (NO_R6EMU
&& (insn
.i_format
.rs
||
488 insn
.i_format
.rt
== bgezall_op
))
491 regs
->regs
[31] = regs
->cp0_epc
+
493 dec_insn
.next_pc_inc
;
499 if ((long)regs
->regs
[insn
.i_format
.rs
] >= 0)
500 *contpc
= regs
->cp0_epc
+
502 (insn
.i_format
.simmediate
<< 2);
504 *contpc
= regs
->cp0_epc
+
506 dec_insn
.next_pc_inc
;
513 regs
->regs
[31] = regs
->cp0_epc
+
515 dec_insn
.next_pc_inc
;
518 *contpc
= regs
->cp0_epc
+ dec_insn
.pc_inc
;
521 *contpc
|= (insn
.j_format
.target
<< 2);
522 /* Set microMIPS mode bit: XOR for jalx. */
529 if (regs
->regs
[insn
.i_format
.rs
] ==
530 regs
->regs
[insn
.i_format
.rt
])
531 *contpc
= regs
->cp0_epc
+
533 (insn
.i_format
.simmediate
<< 2);
535 *contpc
= regs
->cp0_epc
+
537 dec_insn
.next_pc_inc
;
543 if (regs
->regs
[insn
.i_format
.rs
] !=
544 regs
->regs
[insn
.i_format
.rt
])
545 *contpc
= regs
->cp0_epc
+
547 (insn
.i_format
.simmediate
<< 2);
549 *contpc
= regs
->cp0_epc
+
551 dec_insn
.next_pc_inc
;
554 if (!insn
.i_format
.rt
&& NO_R6EMU
)
559 * Compact branches for R6 for the
560 * blez and blezl opcodes.
561 * BLEZ | rs = 0 | rt != 0 == BLEZALC
562 * BLEZ | rs = rt != 0 == BGEZALC
563 * BLEZ | rs != 0 | rt != 0 == BGEUC
564 * BLEZL | rs = 0 | rt != 0 == BLEZC
565 * BLEZL | rs = rt != 0 == BGEZC
566 * BLEZL | rs != 0 | rt != 0 == BGEC
568 * For real BLEZ{,L}, rt is always 0.
570 if (cpu_has_mips_r6
&& insn
.i_format
.rt
) {
571 if ((insn
.i_format
.opcode
== blez_op
) &&
572 ((!insn
.i_format
.rs
&& insn
.i_format
.rt
) ||
573 (insn
.i_format
.rs
== insn
.i_format
.rt
)))
574 regs
->regs
[31] = regs
->cp0_epc
+
576 *contpc
= regs
->cp0_epc
+ dec_insn
.pc_inc
+
577 dec_insn
.next_pc_inc
;
581 if ((long)regs
->regs
[insn
.i_format
.rs
] <= 0)
582 *contpc
= regs
->cp0_epc
+
584 (insn
.i_format
.simmediate
<< 2);
586 *contpc
= regs
->cp0_epc
+
588 dec_insn
.next_pc_inc
;
591 if (!insn
.i_format
.rt
&& NO_R6EMU
)
595 * Compact branches for R6 for the
596 * bgtz and bgtzl opcodes.
597 * BGTZ | rs = 0 | rt != 0 == BGTZALC
598 * BGTZ | rs = rt != 0 == BLTZALC
599 * BGTZ | rs != 0 | rt != 0 == BLTUC
600 * BGTZL | rs = 0 | rt != 0 == BGTZC
601 * BGTZL | rs = rt != 0 == BLTZC
602 * BGTZL | rs != 0 | rt != 0 == BLTC
604 * *ZALC varint for BGTZ &&& rt != 0
605 * For real GTZ{,L}, rt is always 0.
607 if (cpu_has_mips_r6
&& insn
.i_format
.rt
) {
608 if ((insn
.i_format
.opcode
== blez_op
) &&
609 ((!insn
.i_format
.rs
&& insn
.i_format
.rt
) ||
610 (insn
.i_format
.rs
== insn
.i_format
.rt
)))
611 regs
->regs
[31] = regs
->cp0_epc
+
613 *contpc
= regs
->cp0_epc
+ dec_insn
.pc_inc
+
614 dec_insn
.next_pc_inc
;
619 if ((long)regs
->regs
[insn
.i_format
.rs
] > 0)
620 *contpc
= regs
->cp0_epc
+
622 (insn
.i_format
.simmediate
<< 2);
624 *contpc
= regs
->cp0_epc
+
626 dec_insn
.next_pc_inc
;
630 if (!cpu_has_mips_r6
)
632 if (insn
.i_format
.rt
&& !insn
.i_format
.rs
)
633 regs
->regs
[31] = regs
->cp0_epc
+ 4;
634 *contpc
= regs
->cp0_epc
+ dec_insn
.pc_inc
+
635 dec_insn
.next_pc_inc
;
638 #ifdef CONFIG_CPU_CAVIUM_OCTEON
639 case lwc2_op
: /* This is bbit0 on Octeon */
640 if ((regs
->regs
[insn
.i_format
.rs
] & (1ull<<insn
.i_format
.rt
)) == 0)
641 *contpc
= regs
->cp0_epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
643 *contpc
= regs
->cp0_epc
+ 8;
645 case ldc2_op
: /* This is bbit032 on Octeon */
646 if ((regs
->regs
[insn
.i_format
.rs
] & (1ull<<(insn
.i_format
.rt
+ 32))) == 0)
647 *contpc
= regs
->cp0_epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
649 *contpc
= regs
->cp0_epc
+ 8;
651 case swc2_op
: /* This is bbit1 on Octeon */
652 if (regs
->regs
[insn
.i_format
.rs
] & (1ull<<insn
.i_format
.rt
))
653 *contpc
= regs
->cp0_epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
655 *contpc
= regs
->cp0_epc
+ 8;
657 case sdc2_op
: /* This is bbit132 on Octeon */
658 if (regs
->regs
[insn
.i_format
.rs
] & (1ull<<(insn
.i_format
.rt
+ 32)))
659 *contpc
= regs
->cp0_epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
661 *contpc
= regs
->cp0_epc
+ 8;
666 * Only valid for MIPS R6 but we can still end up
667 * here from a broken userland so just tell emulator
668 * this is not a branch and let it break later on.
670 if (!cpu_has_mips_r6
)
672 *contpc
= regs
->cp0_epc
+ dec_insn
.pc_inc
+
673 dec_insn
.next_pc_inc
;
677 if (!cpu_has_mips_r6
)
679 regs
->regs
[31] = regs
->cp0_epc
+ 4;
680 *contpc
= regs
->cp0_epc
+ dec_insn
.pc_inc
+
681 dec_insn
.next_pc_inc
;
685 if (!cpu_has_mips_r6
)
687 *contpc
= regs
->cp0_epc
+ dec_insn
.pc_inc
+
688 dec_insn
.next_pc_inc
;
692 if (!cpu_has_mips_r6
)
694 if (!insn
.i_format
.rs
)
695 regs
->regs
[31] = regs
->cp0_epc
+ 4;
696 *contpc
= regs
->cp0_epc
+ dec_insn
.pc_inc
+
697 dec_insn
.next_pc_inc
;
703 /* Need to check for R6 bc1nez and bc1eqz branches */
704 if (cpu_has_mips_r6
&&
705 ((insn
.i_format
.rs
== bc1eqz_op
) ||
706 (insn
.i_format
.rs
== bc1nez_op
))) {
708 switch (insn
.i_format
.rs
) {
710 if (get_fpr32(¤t
->thread
.fpu
.fpr
[insn
.i_format
.rt
], 0) & 0x1)
714 if (!(get_fpr32(¤t
->thread
.fpu
.fpr
[insn
.i_format
.rt
], 0) & 0x1))
719 *contpc
= regs
->cp0_epc
+
721 (insn
.i_format
.simmediate
<< 2);
723 *contpc
= regs
->cp0_epc
+
725 dec_insn
.next_pc_inc
;
729 /* R2/R6 compatible cop1 instruction. Fall through */
732 if (insn
.i_format
.rs
== bc_op
) {
735 fcr31
= read_32bit_cp1_register(CP1_STATUS
);
737 fcr31
= current
->thread
.fpu
.fcr31
;
740 bit
= (insn
.i_format
.rt
>> 2);
743 switch (insn
.i_format
.rt
& 3) {
746 if (~fcr31
& (1 << bit
))
747 *contpc
= regs
->cp0_epc
+
749 (insn
.i_format
.simmediate
<< 2);
751 *contpc
= regs
->cp0_epc
+
753 dec_insn
.next_pc_inc
;
757 if (fcr31
& (1 << bit
))
758 *contpc
= regs
->cp0_epc
+
760 (insn
.i_format
.simmediate
<< 2);
762 *contpc
= regs
->cp0_epc
+
764 dec_insn
.next_pc_inc
;
774 * In the Linux kernel, we support selection of FPR format on the
775 * basis of the Status.FR bit. If an FPU is not present, the FR bit
776 * is hardwired to zero, which would imply a 32-bit FPU even for
777 * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
778 * FPU emu is slow and bulky and optimizing this function offers fairly
779 * sizeable benefits so we try to be clever and make this function return
780 * a constant whenever possible, that is on 64-bit kernels without O32
781 * compatibility enabled and on 32-bit without 64-bit FPU support.
783 static inline int cop1_64bit(struct pt_regs
*xcp
)
785 if (config_enabled(CONFIG_64BIT
) && !config_enabled(CONFIG_MIPS32_O32
))
787 else if (config_enabled(CONFIG_32BIT
) &&
788 !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT
))
791 return !test_thread_flag(TIF_32BIT_FPREGS
);
794 static inline bool hybrid_fprs(void)
796 return test_thread_flag(TIF_HYBRID_FPREGS
);
799 #define SIFROMREG(si, x) \
801 if (cop1_64bit(xcp) && !hybrid_fprs()) \
802 (si) = (int)get_fpr32(&ctx->fpr[x], 0); \
804 (si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
807 #define SITOREG(si, x) \
809 if (cop1_64bit(xcp) && !hybrid_fprs()) { \
811 set_fpr32(&ctx->fpr[x], 0, si); \
812 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
813 set_fpr32(&ctx->fpr[x], i, 0); \
815 set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
819 #define SIFROMHREG(si, x) ((si) = (int)get_fpr32(&ctx->fpr[x], 1))
821 #define SITOHREG(si, x) \
824 set_fpr32(&ctx->fpr[x], 1, si); \
825 for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
826 set_fpr32(&ctx->fpr[x], i, 0); \
829 #define DIFROMREG(di, x) \
830 ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
832 #define DITOREG(di, x) \
835 fpr = (x) & ~(cop1_64bit(xcp) == 0); \
836 set_fpr64(&ctx->fpr[fpr], 0, di); \
837 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
838 set_fpr64(&ctx->fpr[fpr], i, 0); \
841 #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
842 #define SPTOREG(sp, x) SITOREG((sp).bits, x)
843 #define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
844 #define DPTOREG(dp, x) DITOREG((dp).bits, x)
847 * Emulate a CFC1 instruction.
849 static inline void cop1_cfc(struct pt_regs
*xcp
, struct mips_fpu_struct
*ctx
,
852 u32 fcr31
= ctx
->fcr31
;
855 switch (MIPSInst_RD(ir
)) {
858 pr_debug("%p gpr[%d]<-csr=%08x\n",
859 (void *)xcp
->cp0_epc
, MIPSInst_RT(ir
), value
);
865 value
= (fcr31
>> (FPU_CSR_FS_S
- MIPS_FENR_FS_S
)) &
867 value
|= fcr31
& (FPU_CSR_ALL_E
| FPU_CSR_RM
);
868 pr_debug("%p gpr[%d]<-enr=%08x\n",
869 (void *)xcp
->cp0_epc
, MIPSInst_RT(ir
), value
);
875 value
= fcr31
& (FPU_CSR_ALL_X
| FPU_CSR_ALL_S
);
876 pr_debug("%p gpr[%d]<-exr=%08x\n",
877 (void *)xcp
->cp0_epc
, MIPSInst_RT(ir
), value
);
883 value
= (fcr31
>> (FPU_CSR_COND_S
- MIPS_FCCR_COND0_S
)) &
885 value
|= (fcr31
>> (FPU_CSR_COND1_S
- MIPS_FCCR_COND1_S
)) &
886 (MIPS_FCCR_CONDX
& ~MIPS_FCCR_COND0
);
887 pr_debug("%p gpr[%d]<-ccr=%08x\n",
888 (void *)xcp
->cp0_epc
, MIPSInst_RT(ir
), value
);
892 value
= boot_cpu_data
.fpu_id
;
900 xcp
->regs
[MIPSInst_RT(ir
)] = value
;
904 * Emulate a CTC1 instruction.
906 static inline void cop1_ctc(struct pt_regs
*xcp
, struct mips_fpu_struct
*ctx
,
909 u32 fcr31
= ctx
->fcr31
;
913 if (MIPSInst_RT(ir
) == 0)
916 value
= xcp
->regs
[MIPSInst_RT(ir
)];
918 switch (MIPSInst_RD(ir
)) {
920 pr_debug("%p gpr[%d]->csr=%08x\n",
921 (void *)xcp
->cp0_epc
, MIPSInst_RT(ir
), value
);
923 /* Preserve read-only bits. */
924 mask
= boot_cpu_data
.fpu_msk31
;
925 fcr31
= (value
& ~mask
) | (fcr31
& mask
);
931 pr_debug("%p gpr[%d]->enr=%08x\n",
932 (void *)xcp
->cp0_epc
, MIPSInst_RT(ir
), value
);
933 fcr31
&= ~(FPU_CSR_FS
| FPU_CSR_ALL_E
| FPU_CSR_RM
);
934 fcr31
|= (value
<< (FPU_CSR_FS_S
- MIPS_FENR_FS_S
)) &
936 fcr31
|= value
& (FPU_CSR_ALL_E
| FPU_CSR_RM
);
942 pr_debug("%p gpr[%d]->exr=%08x\n",
943 (void *)xcp
->cp0_epc
, MIPSInst_RT(ir
), value
);
944 fcr31
&= ~(FPU_CSR_ALL_X
| FPU_CSR_ALL_S
);
945 fcr31
|= value
& (FPU_CSR_ALL_X
| FPU_CSR_ALL_S
);
951 pr_debug("%p gpr[%d]->ccr=%08x\n",
952 (void *)xcp
->cp0_epc
, MIPSInst_RT(ir
), value
);
953 fcr31
&= ~(FPU_CSR_CONDX
| FPU_CSR_COND
);
954 fcr31
|= (value
<< (FPU_CSR_COND_S
- MIPS_FCCR_COND0_S
)) &
956 fcr31
|= (value
<< (FPU_CSR_COND1_S
- MIPS_FCCR_COND1_S
)) &
968 * Emulate the single floating point instruction pointed at by EPC.
969 * Two instructions if the instruction is in a branch delay slot.
972 static int cop1Emulate(struct pt_regs
*xcp
, struct mips_fpu_struct
*ctx
,
973 struct mm_decoded_insn dec_insn
, void *__user
*fault_addr
)
975 unsigned long contpc
= xcp
->cp0_epc
+ dec_insn
.pc_inc
;
976 unsigned int cond
, cbit
, bit0
;
987 * These are giving gcc a gentle hint about what to expect in
988 * dec_inst in order to do better optimization.
990 if (!cpu_has_mmips
&& dec_insn
.micro_mips_mode
)
993 /* XXX NEC Vr54xx bug workaround */
994 if (delay_slot(xcp
)) {
995 if (dec_insn
.micro_mips_mode
) {
996 if (!mm_isBranchInstr(xcp
, dec_insn
, &contpc
))
997 clear_delay_slot(xcp
);
999 if (!isBranchInstr(xcp
, dec_insn
, &contpc
))
1000 clear_delay_slot(xcp
);
1004 if (delay_slot(xcp
)) {
1006 * The instruction to be emulated is in a branch delay slot
1007 * which means that we have to emulate the branch instruction
1008 * BEFORE we do the cop1 instruction.
1010 * This branch could be a COP1 branch, but in that case we
1011 * would have had a trap for that instruction, and would not
1012 * come through this route.
1014 * Linux MIPS branch emulator operates on context, updating the
1017 ir
= dec_insn
.next_insn
; /* process delay slot instr */
1018 pc_inc
= dec_insn
.next_pc_inc
;
1020 ir
= dec_insn
.insn
; /* process current instr */
1021 pc_inc
= dec_insn
.pc_inc
;
1025 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
1026 * instructions, we want to convert microMIPS FPU instructions
1027 * into MIPS32 instructions so that we could reuse all of the
1028 * FPU emulation code.
1030 * NOTE: We cannot do this for branch instructions since they
1031 * are not a subset. Example: Cannot emulate a 16-bit
1032 * aligned target address with a MIPS32 instruction.
1034 if (dec_insn
.micro_mips_mode
) {
1036 * If next instruction is a 16-bit instruction, then it
1037 * it cannot be a FPU instruction. This could happen
1038 * since we can be called for non-FPU instructions.
1040 if ((pc_inc
== 2) ||
1041 (microMIPS32_to_MIPS32((union mips_instruction
*)&ir
)
1047 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS
, 1, xcp
, 0);
1048 MIPS_FPU_EMU_INC_STATS(emulated
);
1049 switch (MIPSInst_OPCODE(ir
)) {
1051 dva
= (u64 __user
*) (xcp
->regs
[MIPSInst_RS(ir
)] +
1053 MIPS_FPU_EMU_INC_STATS(loads
);
1055 if (!access_ok(VERIFY_READ
, dva
, sizeof(u64
))) {
1056 MIPS_FPU_EMU_INC_STATS(errors
);
1060 if (__get_user(dval
, dva
)) {
1061 MIPS_FPU_EMU_INC_STATS(errors
);
1065 DITOREG(dval
, MIPSInst_RT(ir
));
1069 dva
= (u64 __user
*) (xcp
->regs
[MIPSInst_RS(ir
)] +
1071 MIPS_FPU_EMU_INC_STATS(stores
);
1072 DIFROMREG(dval
, MIPSInst_RT(ir
));
1073 if (!access_ok(VERIFY_WRITE
, dva
, sizeof(u64
))) {
1074 MIPS_FPU_EMU_INC_STATS(errors
);
1078 if (__put_user(dval
, dva
)) {
1079 MIPS_FPU_EMU_INC_STATS(errors
);
1086 wva
= (u32 __user
*) (xcp
->regs
[MIPSInst_RS(ir
)] +
1088 MIPS_FPU_EMU_INC_STATS(loads
);
1089 if (!access_ok(VERIFY_READ
, wva
, sizeof(u32
))) {
1090 MIPS_FPU_EMU_INC_STATS(errors
);
1094 if (__get_user(wval
, wva
)) {
1095 MIPS_FPU_EMU_INC_STATS(errors
);
1099 SITOREG(wval
, MIPSInst_RT(ir
));
1103 wva
= (u32 __user
*) (xcp
->regs
[MIPSInst_RS(ir
)] +
1105 MIPS_FPU_EMU_INC_STATS(stores
);
1106 SIFROMREG(wval
, MIPSInst_RT(ir
));
1107 if (!access_ok(VERIFY_WRITE
, wva
, sizeof(u32
))) {
1108 MIPS_FPU_EMU_INC_STATS(errors
);
1112 if (__put_user(wval
, wva
)) {
1113 MIPS_FPU_EMU_INC_STATS(errors
);
1120 switch (MIPSInst_RS(ir
)) {
1122 if (!cpu_has_mips_3_4_5
&& !cpu_has_mips64
)
1125 /* copregister fs -> gpr[rt] */
1126 if (MIPSInst_RT(ir
) != 0) {
1127 DIFROMREG(xcp
->regs
[MIPSInst_RT(ir
)],
1133 if (!cpu_has_mips_3_4_5
&& !cpu_has_mips64
)
1136 /* copregister fs <- rt */
1137 DITOREG(xcp
->regs
[MIPSInst_RT(ir
)], MIPSInst_RD(ir
));
1141 if (!cpu_has_mips_r2_r6
)
1144 /* copregister rd -> gpr[rt] */
1145 if (MIPSInst_RT(ir
) != 0) {
1146 SIFROMHREG(xcp
->regs
[MIPSInst_RT(ir
)],
1152 if (!cpu_has_mips_r2_r6
)
1155 /* copregister rd <- gpr[rt] */
1156 SITOHREG(xcp
->regs
[MIPSInst_RT(ir
)], MIPSInst_RD(ir
));
1160 /* copregister rd -> gpr[rt] */
1161 if (MIPSInst_RT(ir
) != 0) {
1162 SIFROMREG(xcp
->regs
[MIPSInst_RT(ir
)],
1168 /* copregister rd <- rt */
1169 SITOREG(xcp
->regs
[MIPSInst_RT(ir
)], MIPSInst_RD(ir
));
1173 /* cop control register rd -> gpr[rt] */
1174 cop1_cfc(xcp
, ctx
, ir
);
1178 /* copregister rd <- rt */
1179 cop1_ctc(xcp
, ctx
, ir
);
1180 if ((ctx
->fcr31
>> 5) & ctx
->fcr31
& FPU_CSR_ALL_E
) {
1187 if (!cpu_has_mips_r6
|| delay_slot(xcp
))
1191 fpr
= ¤t
->thread
.fpu
.fpr
[MIPSInst_RT(ir
)];
1192 bit0
= get_fpr32(fpr
, 0) & 0x1;
1193 switch (MIPSInst_RS(ir
)) {
1204 if (delay_slot(xcp
))
1207 if (cpu_has_mips_4_5_r
)
1208 cbit
= fpucondbit
[MIPSInst_RT(ir
) >> 2];
1210 cbit
= FPU_CSR_COND
;
1211 cond
= ctx
->fcr31
& cbit
;
1214 switch (MIPSInst_RT(ir
) & 3) {
1216 if (cpu_has_mips_2_3_4_5_r
)
1223 if (cpu_has_mips_2_3_4_5_r
)
1230 set_delay_slot(xcp
);
1233 * Branch taken: emulate dslot instruction
1238 * Remember EPC at the branch to point back
1239 * at so that any delay-slot instruction
1240 * signal is not silently ignored.
1242 bcpc
= xcp
->cp0_epc
;
1243 xcp
->cp0_epc
+= dec_insn
.pc_inc
;
1245 contpc
= MIPSInst_SIMM(ir
);
1246 ir
= dec_insn
.next_insn
;
1247 if (dec_insn
.micro_mips_mode
) {
1248 contpc
= (xcp
->cp0_epc
+ (contpc
<< 1));
1250 /* If 16-bit instruction, not FPU. */
1251 if ((dec_insn
.next_pc_inc
== 2) ||
1252 (microMIPS32_to_MIPS32((union mips_instruction
*)&ir
) == SIGILL
)) {
1255 * Since this instruction will
1256 * be put on the stack with
1257 * 32-bit words, get around
1258 * this problem by putting a
1259 * NOP16 as the second one.
1261 if (dec_insn
.next_pc_inc
== 2)
1262 ir
= (ir
& (~0xffff)) | MM_NOP16
;
1265 * Single step the non-CP1
1266 * instruction in the dslot.
1268 sig
= mips_dsemul(xcp
, ir
,
1273 xcp
->cp0_epc
= bcpc
;
1275 * SIGILL forces out of
1276 * the emulation loop.
1278 return sig
? sig
: SIGILL
;
1281 contpc
= (xcp
->cp0_epc
+ (contpc
<< 2));
1283 switch (MIPSInst_OPCODE(ir
)) {
1290 if (cpu_has_mips_2_3_4_5_r
)
1299 if (cpu_has_mips_4_5_64_r2_r6
)
1300 /* its one of ours */
1306 switch (MIPSInst_FUNC(ir
)) {
1308 if (cpu_has_mips_4_5_r
)
1316 xcp
->cp0_epc
= bcpc
;
1321 * Single step the non-cp1
1322 * instruction in the dslot
1324 sig
= mips_dsemul(xcp
, ir
, contpc
);
1328 xcp
->cp0_epc
= bcpc
;
1329 /* SIGILL forces out of the emulation loop. */
1330 return sig
? sig
: SIGILL
;
1331 } else if (likely
) { /* branch not taken */
1333 * branch likely nullifies
1334 * dslot if not taken
1336 xcp
->cp0_epc
+= dec_insn
.pc_inc
;
1337 contpc
+= dec_insn
.pc_inc
;
1339 * else continue & execute
1340 * dslot as normal insn
1346 if (!(MIPSInst_RS(ir
) & 0x10))
1349 /* a real fpu computation instruction */
1350 if ((sig
= fpu_emu(xcp
, ctx
, ir
)))
1356 if (!cpu_has_mips_4_5_64_r2_r6
)
1359 sig
= fpux_emu(xcp
, ctx
, ir
, fault_addr
);
1365 if (!cpu_has_mips_4_5_r
)
1368 if (MIPSInst_FUNC(ir
) != movc_op
)
1370 cond
= fpucondbit
[MIPSInst_RT(ir
) >> 2];
1371 if (((ctx
->fcr31
& cond
) != 0) == ((MIPSInst_RT(ir
) & 1) != 0))
1372 xcp
->regs
[MIPSInst_RD(ir
)] =
1373 xcp
->regs
[MIPSInst_RS(ir
)];
1381 xcp
->cp0_epc
= contpc
;
1382 clear_delay_slot(xcp
);
1388 * Conversion table from MIPS compare ops 48-63
1389 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
1391 static const unsigned char cmptab
[8] = {
1392 0, /* cmp_0 (sig) cmp_sf */
1393 IEEE754_CUN
, /* cmp_un (sig) cmp_ngle */
1394 IEEE754_CEQ
, /* cmp_eq (sig) cmp_seq */
1395 IEEE754_CEQ
| IEEE754_CUN
, /* cmp_ueq (sig) cmp_ngl */
1396 IEEE754_CLT
, /* cmp_olt (sig) cmp_lt */
1397 IEEE754_CLT
| IEEE754_CUN
, /* cmp_ult (sig) cmp_nge */
1398 IEEE754_CLT
| IEEE754_CEQ
, /* cmp_ole (sig) cmp_le */
1399 IEEE754_CLT
| IEEE754_CEQ
| IEEE754_CUN
, /* cmp_ule (sig) cmp_ngt */
1402 static const unsigned char negative_cmptab
[8] = {
1404 IEEE754_CLT
| IEEE754_CGT
| IEEE754_CEQ
,
1405 IEEE754_CLT
| IEEE754_CGT
| IEEE754_CUN
,
1406 IEEE754_CLT
| IEEE754_CGT
,
1412 * Additional MIPS4 instructions
1415 #define DEF3OP(name, p, f1, f2, f3) \
1416 static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \
1417 union ieee754##p s, union ieee754##p t) \
1419 struct _ieee754_csr ieee754_csr_save; \
1421 ieee754_csr_save = ieee754_csr; \
1423 ieee754_csr_save.cx |= ieee754_csr.cx; \
1424 ieee754_csr_save.sx |= ieee754_csr.sx; \
1426 ieee754_csr.cx |= ieee754_csr_save.cx; \
1427 ieee754_csr.sx |= ieee754_csr_save.sx; \
1431 static union ieee754dp
fpemu_dp_recip(union ieee754dp d
)
1433 return ieee754dp_div(ieee754dp_one(0), d
);
1436 static union ieee754dp
fpemu_dp_rsqrt(union ieee754dp d
)
1438 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d
));
1441 static union ieee754sp
fpemu_sp_recip(union ieee754sp s
)
1443 return ieee754sp_div(ieee754sp_one(0), s
);
1446 static union ieee754sp
fpemu_sp_rsqrt(union ieee754sp s
)
1448 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s
));
1451 DEF3OP(madd
, sp
, ieee754sp_mul
, ieee754sp_add
, );
1452 DEF3OP(msub
, sp
, ieee754sp_mul
, ieee754sp_sub
, );
1453 DEF3OP(nmadd
, sp
, ieee754sp_mul
, ieee754sp_add
, ieee754sp_neg
);
1454 DEF3OP(nmsub
, sp
, ieee754sp_mul
, ieee754sp_sub
, ieee754sp_neg
);
1455 DEF3OP(madd
, dp
, ieee754dp_mul
, ieee754dp_add
, );
1456 DEF3OP(msub
, dp
, ieee754dp_mul
, ieee754dp_sub
, );
1457 DEF3OP(nmadd
, dp
, ieee754dp_mul
, ieee754dp_add
, ieee754dp_neg
);
1458 DEF3OP(nmsub
, dp
, ieee754dp_mul
, ieee754dp_sub
, ieee754dp_neg
);
1460 static int fpux_emu(struct pt_regs
*xcp
, struct mips_fpu_struct
*ctx
,
1461 mips_instruction ir
, void *__user
*fault_addr
)
1463 unsigned rcsr
= 0; /* resulting csr */
1465 MIPS_FPU_EMU_INC_STATS(cp1xops
);
1467 switch (MIPSInst_FMA_FFMT(ir
)) {
1468 case s_fmt
:{ /* 0 */
1470 union ieee754sp(*handler
) (union ieee754sp
, union ieee754sp
, union ieee754sp
);
1471 union ieee754sp fd
, fr
, fs
, ft
;
1475 switch (MIPSInst_FUNC(ir
)) {
1477 va
= (void __user
*) (xcp
->regs
[MIPSInst_FR(ir
)] +
1478 xcp
->regs
[MIPSInst_FT(ir
)]);
1480 MIPS_FPU_EMU_INC_STATS(loads
);
1481 if (!access_ok(VERIFY_READ
, va
, sizeof(u32
))) {
1482 MIPS_FPU_EMU_INC_STATS(errors
);
1486 if (__get_user(val
, va
)) {
1487 MIPS_FPU_EMU_INC_STATS(errors
);
1491 SITOREG(val
, MIPSInst_FD(ir
));
1495 va
= (void __user
*) (xcp
->regs
[MIPSInst_FR(ir
)] +
1496 xcp
->regs
[MIPSInst_FT(ir
)]);
1498 MIPS_FPU_EMU_INC_STATS(stores
);
1500 SIFROMREG(val
, MIPSInst_FS(ir
));
1501 if (!access_ok(VERIFY_WRITE
, va
, sizeof(u32
))) {
1502 MIPS_FPU_EMU_INC_STATS(errors
);
1506 if (put_user(val
, va
)) {
1507 MIPS_FPU_EMU_INC_STATS(errors
);
1514 handler
= fpemu_sp_madd
;
1517 handler
= fpemu_sp_msub
;
1520 handler
= fpemu_sp_nmadd
;
1523 handler
= fpemu_sp_nmsub
;
1527 SPFROMREG(fr
, MIPSInst_FR(ir
));
1528 SPFROMREG(fs
, MIPSInst_FS(ir
));
1529 SPFROMREG(ft
, MIPSInst_FT(ir
));
1530 fd
= (*handler
) (fr
, fs
, ft
);
1531 SPTOREG(fd
, MIPSInst_FD(ir
));
1534 if (ieee754_cxtest(IEEE754_INEXACT
)) {
1535 MIPS_FPU_EMU_INC_STATS(ieee754_inexact
);
1536 rcsr
|= FPU_CSR_INE_X
| FPU_CSR_INE_S
;
1538 if (ieee754_cxtest(IEEE754_UNDERFLOW
)) {
1539 MIPS_FPU_EMU_INC_STATS(ieee754_underflow
);
1540 rcsr
|= FPU_CSR_UDF_X
| FPU_CSR_UDF_S
;
1542 if (ieee754_cxtest(IEEE754_OVERFLOW
)) {
1543 MIPS_FPU_EMU_INC_STATS(ieee754_overflow
);
1544 rcsr
|= FPU_CSR_OVF_X
| FPU_CSR_OVF_S
;
1546 if (ieee754_cxtest(IEEE754_INVALID_OPERATION
)) {
1547 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop
);
1548 rcsr
|= FPU_CSR_INV_X
| FPU_CSR_INV_S
;
1551 ctx
->fcr31
= (ctx
->fcr31
& ~FPU_CSR_ALL_X
) | rcsr
;
1552 if ((ctx
->fcr31
>> 5) & ctx
->fcr31
& FPU_CSR_ALL_E
) {
1553 /*printk ("SIGFPE: FPU csr = %08x\n",
1566 case d_fmt
:{ /* 1 */
1567 union ieee754dp(*handler
) (union ieee754dp
, union ieee754dp
, union ieee754dp
);
1568 union ieee754dp fd
, fr
, fs
, ft
;
1572 switch (MIPSInst_FUNC(ir
)) {
1574 va
= (void __user
*) (xcp
->regs
[MIPSInst_FR(ir
)] +
1575 xcp
->regs
[MIPSInst_FT(ir
)]);
1577 MIPS_FPU_EMU_INC_STATS(loads
);
1578 if (!access_ok(VERIFY_READ
, va
, sizeof(u64
))) {
1579 MIPS_FPU_EMU_INC_STATS(errors
);
1583 if (__get_user(val
, va
)) {
1584 MIPS_FPU_EMU_INC_STATS(errors
);
1588 DITOREG(val
, MIPSInst_FD(ir
));
1592 va
= (void __user
*) (xcp
->regs
[MIPSInst_FR(ir
)] +
1593 xcp
->regs
[MIPSInst_FT(ir
)]);
1595 MIPS_FPU_EMU_INC_STATS(stores
);
1596 DIFROMREG(val
, MIPSInst_FS(ir
));
1597 if (!access_ok(VERIFY_WRITE
, va
, sizeof(u64
))) {
1598 MIPS_FPU_EMU_INC_STATS(errors
);
1602 if (__put_user(val
, va
)) {
1603 MIPS_FPU_EMU_INC_STATS(errors
);
1610 handler
= fpemu_dp_madd
;
1613 handler
= fpemu_dp_msub
;
1616 handler
= fpemu_dp_nmadd
;
1619 handler
= fpemu_dp_nmsub
;
1623 DPFROMREG(fr
, MIPSInst_FR(ir
));
1624 DPFROMREG(fs
, MIPSInst_FS(ir
));
1625 DPFROMREG(ft
, MIPSInst_FT(ir
));
1626 fd
= (*handler
) (fr
, fs
, ft
);
1627 DPTOREG(fd
, MIPSInst_FD(ir
));
1637 if (MIPSInst_FUNC(ir
) != pfetch_op
)
1640 /* ignore prefx operation */
1653 * Emulate a single COP1 arithmetic instruction.
1655 static int fpu_emu(struct pt_regs
*xcp
, struct mips_fpu_struct
*ctx
,
1656 mips_instruction ir
)
1658 int rfmt
; /* resulting format */
1659 unsigned rcsr
= 0; /* resulting csr */
1668 } rv
; /* resulting value */
1671 MIPS_FPU_EMU_INC_STATS(cp1ops
);
1672 switch (rfmt
= (MIPSInst_FFMT(ir
) & 0xf)) {
1673 case s_fmt
: { /* 0 */
1675 union ieee754sp(*b
) (union ieee754sp
, union ieee754sp
);
1676 union ieee754sp(*u
) (union ieee754sp
);
1678 union ieee754sp fs
, ft
;
1680 switch (MIPSInst_FUNC(ir
)) {
1683 handler
.b
= ieee754sp_add
;
1686 handler
.b
= ieee754sp_sub
;
1689 handler
.b
= ieee754sp_mul
;
1692 handler
.b
= ieee754sp_div
;
1697 if (!cpu_has_mips_2_3_4_5_r
)
1700 handler
.u
= ieee754sp_sqrt
;
1704 * Note that on some MIPS IV implementations such as the
1705 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1706 * achieve full IEEE-754 accuracy - however this emulator does.
1709 if (!cpu_has_mips_4_5_64_r2_r6
)
1712 handler
.u
= fpemu_sp_rsqrt
;
1716 if (!cpu_has_mips_4_5_64_r2_r6
)
1719 handler
.u
= fpemu_sp_recip
;
1723 if (!cpu_has_mips_4_5_r
)
1726 cond
= fpucondbit
[MIPSInst_FT(ir
) >> 2];
1727 if (((ctx
->fcr31
& cond
) != 0) !=
1728 ((MIPSInst_FT(ir
) & 1) != 0))
1730 SPFROMREG(rv
.s
, MIPSInst_FS(ir
));
1734 if (!cpu_has_mips_4_5_r
)
1737 if (xcp
->regs
[MIPSInst_FT(ir
)] != 0)
1739 SPFROMREG(rv
.s
, MIPSInst_FS(ir
));
1743 if (!cpu_has_mips_4_5_r
)
1746 if (xcp
->regs
[MIPSInst_FT(ir
)] == 0)
1748 SPFROMREG(rv
.s
, MIPSInst_FS(ir
));
1752 if (!cpu_has_mips_r6
)
1755 SPFROMREG(rv
.s
, MIPSInst_FT(ir
));
1759 SPFROMREG(rv
.s
, MIPSInst_FS(ir
));
1763 if (!cpu_has_mips_r6
)
1766 SPFROMREG(rv
.s
, MIPSInst_FT(ir
));
1768 SPFROMREG(rv
.s
, MIPSInst_FS(ir
));
1774 union ieee754sp ft
, fs
, fd
;
1776 if (!cpu_has_mips_r6
)
1779 SPFROMREG(ft
, MIPSInst_FT(ir
));
1780 SPFROMREG(fs
, MIPSInst_FS(ir
));
1781 SPFROMREG(fd
, MIPSInst_FD(ir
));
1782 rv
.s
= ieee754sp_maddf(fd
, fs
, ft
);
1787 union ieee754sp ft
, fs
, fd
;
1789 if (!cpu_has_mips_r6
)
1792 SPFROMREG(ft
, MIPSInst_FT(ir
));
1793 SPFROMREG(fs
, MIPSInst_FS(ir
));
1794 SPFROMREG(fd
, MIPSInst_FD(ir
));
1795 rv
.s
= ieee754sp_msubf(fd
, fs
, ft
);
1802 if (!cpu_has_mips_r6
)
1805 SPFROMREG(fs
, MIPSInst_FS(ir
));
1806 rv
.l
= ieee754sp_tlong(fs
);
1807 rv
.s
= ieee754sp_flong(rv
.l
);
1814 if (!cpu_has_mips_r6
)
1817 SPFROMREG(fs
, MIPSInst_FS(ir
));
1818 rv
.w
= ieee754sp_2008class(fs
);
1824 union ieee754sp fs
, ft
;
1826 if (!cpu_has_mips_r6
)
1829 SPFROMREG(ft
, MIPSInst_FT(ir
));
1830 SPFROMREG(fs
, MIPSInst_FS(ir
));
1831 rv
.s
= ieee754sp_fmin(fs
, ft
);
1836 union ieee754sp fs
, ft
;
1838 if (!cpu_has_mips_r6
)
1841 SPFROMREG(ft
, MIPSInst_FT(ir
));
1842 SPFROMREG(fs
, MIPSInst_FS(ir
));
1843 rv
.s
= ieee754sp_fmina(fs
, ft
);
1848 union ieee754sp fs
, ft
;
1850 if (!cpu_has_mips_r6
)
1853 SPFROMREG(ft
, MIPSInst_FT(ir
));
1854 SPFROMREG(fs
, MIPSInst_FS(ir
));
1855 rv
.s
= ieee754sp_fmax(fs
, ft
);
1860 union ieee754sp fs
, ft
;
1862 if (!cpu_has_mips_r6
)
1865 SPFROMREG(ft
, MIPSInst_FT(ir
));
1866 SPFROMREG(fs
, MIPSInst_FS(ir
));
1867 rv
.s
= ieee754sp_fmaxa(fs
, ft
);
1872 handler
.u
= ieee754sp_abs
;
1876 handler
.u
= ieee754sp_neg
;
1881 SPFROMREG(rv
.s
, MIPSInst_FS(ir
));
1884 /* binary op on handler */
1886 SPFROMREG(fs
, MIPSInst_FS(ir
));
1887 SPFROMREG(ft
, MIPSInst_FT(ir
));
1889 rv
.s
= (*handler
.b
) (fs
, ft
);
1892 SPFROMREG(fs
, MIPSInst_FS(ir
));
1893 rv
.s
= (*handler
.u
) (fs
);
1896 if (ieee754_cxtest(IEEE754_INEXACT
)) {
1897 MIPS_FPU_EMU_INC_STATS(ieee754_inexact
);
1898 rcsr
|= FPU_CSR_INE_X
| FPU_CSR_INE_S
;
1900 if (ieee754_cxtest(IEEE754_UNDERFLOW
)) {
1901 MIPS_FPU_EMU_INC_STATS(ieee754_underflow
);
1902 rcsr
|= FPU_CSR_UDF_X
| FPU_CSR_UDF_S
;
1904 if (ieee754_cxtest(IEEE754_OVERFLOW
)) {
1905 MIPS_FPU_EMU_INC_STATS(ieee754_overflow
);
1906 rcsr
|= FPU_CSR_OVF_X
| FPU_CSR_OVF_S
;
1908 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE
)) {
1909 MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv
);
1910 rcsr
|= FPU_CSR_DIV_X
| FPU_CSR_DIV_S
;
1912 if (ieee754_cxtest(IEEE754_INVALID_OPERATION
)) {
1913 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop
);
1914 rcsr
|= FPU_CSR_INV_X
| FPU_CSR_INV_S
;
1918 /* unary conv ops */
1920 return SIGILL
; /* not defined */
1923 SPFROMREG(fs
, MIPSInst_FS(ir
));
1924 rv
.d
= ieee754dp_fsp(fs
);
1929 SPFROMREG(fs
, MIPSInst_FS(ir
));
1930 rv
.w
= ieee754sp_tint(fs
);
1938 if (!cpu_has_mips_2_3_4_5_r
)
1941 oldrm
= ieee754_csr
.rm
;
1942 SPFROMREG(fs
, MIPSInst_FS(ir
));
1943 ieee754_csr
.rm
= MIPSInst_FUNC(ir
);
1944 rv
.w
= ieee754sp_tint(fs
);
1945 ieee754_csr
.rm
= oldrm
;
1950 if (!cpu_has_mips_3_4_5_64_r2_r6
)
1953 SPFROMREG(fs
, MIPSInst_FS(ir
));
1954 rv
.l
= ieee754sp_tlong(fs
);
1962 if (!cpu_has_mips_3_4_5_64_r2_r6
)
1965 oldrm
= ieee754_csr
.rm
;
1966 SPFROMREG(fs
, MIPSInst_FS(ir
));
1967 ieee754_csr
.rm
= MIPSInst_FUNC(ir
);
1968 rv
.l
= ieee754sp_tlong(fs
);
1969 ieee754_csr
.rm
= oldrm
;
1974 if (!NO_R6EMU
&& MIPSInst_FUNC(ir
) >= fcmp_op
) {
1975 unsigned cmpop
= MIPSInst_FUNC(ir
) - fcmp_op
;
1976 union ieee754sp fs
, ft
;
1978 SPFROMREG(fs
, MIPSInst_FS(ir
));
1979 SPFROMREG(ft
, MIPSInst_FT(ir
));
1980 rv
.w
= ieee754sp_cmp(fs
, ft
,
1981 cmptab
[cmpop
& 0x7], cmpop
& 0x8);
1983 if ((cmpop
& 0x8) && ieee754_cxtest
1984 (IEEE754_INVALID_OPERATION
))
1985 rcsr
= FPU_CSR_INV_X
| FPU_CSR_INV_S
;
1997 union ieee754dp fs
, ft
;
1999 union ieee754dp(*b
) (union ieee754dp
, union ieee754dp
);
2000 union ieee754dp(*u
) (union ieee754dp
);
2003 switch (MIPSInst_FUNC(ir
)) {
2006 handler
.b
= ieee754dp_add
;
2009 handler
.b
= ieee754dp_sub
;
2012 handler
.b
= ieee754dp_mul
;
2015 handler
.b
= ieee754dp_div
;
2020 if (!cpu_has_mips_2_3_4_5_r
)
2023 handler
.u
= ieee754dp_sqrt
;
2026 * Note that on some MIPS IV implementations such as the
2027 * R5000 and R8000 the FSQRT and FRECIP instructions do not
2028 * achieve full IEEE-754 accuracy - however this emulator does.
2031 if (!cpu_has_mips_4_5_64_r2_r6
)
2034 handler
.u
= fpemu_dp_rsqrt
;
2037 if (!cpu_has_mips_4_5_64_r2_r6
)
2040 handler
.u
= fpemu_dp_recip
;
2043 if (!cpu_has_mips_4_5_r
)
2046 cond
= fpucondbit
[MIPSInst_FT(ir
) >> 2];
2047 if (((ctx
->fcr31
& cond
) != 0) !=
2048 ((MIPSInst_FT(ir
) & 1) != 0))
2050 DPFROMREG(rv
.d
, MIPSInst_FS(ir
));
2053 if (!cpu_has_mips_4_5_r
)
2056 if (xcp
->regs
[MIPSInst_FT(ir
)] != 0)
2058 DPFROMREG(rv
.d
, MIPSInst_FS(ir
));
2061 if (!cpu_has_mips_4_5_r
)
2064 if (xcp
->regs
[MIPSInst_FT(ir
)] == 0)
2066 DPFROMREG(rv
.d
, MIPSInst_FS(ir
));
2070 if (!cpu_has_mips_r6
)
2073 DPFROMREG(rv
.d
, MIPSInst_FT(ir
));
2077 DPFROMREG(rv
.d
, MIPSInst_FS(ir
));
2081 if (!cpu_has_mips_r6
)
2084 DPFROMREG(rv
.d
, MIPSInst_FT(ir
));
2086 DPFROMREG(rv
.d
, MIPSInst_FS(ir
));
2092 union ieee754dp ft
, fs
, fd
;
2094 if (!cpu_has_mips_r6
)
2097 DPFROMREG(ft
, MIPSInst_FT(ir
));
2098 DPFROMREG(fs
, MIPSInst_FS(ir
));
2099 DPFROMREG(fd
, MIPSInst_FD(ir
));
2100 rv
.d
= ieee754dp_maddf(fd
, fs
, ft
);
2105 union ieee754dp ft
, fs
, fd
;
2107 if (!cpu_has_mips_r6
)
2110 DPFROMREG(ft
, MIPSInst_FT(ir
));
2111 DPFROMREG(fs
, MIPSInst_FS(ir
));
2112 DPFROMREG(fd
, MIPSInst_FD(ir
));
2113 rv
.d
= ieee754dp_msubf(fd
, fs
, ft
);
2120 if (!cpu_has_mips_r6
)
2123 DPFROMREG(fs
, MIPSInst_FS(ir
));
2124 rv
.l
= ieee754dp_tlong(fs
);
2125 rv
.d
= ieee754dp_flong(rv
.l
);
2132 if (!cpu_has_mips_r6
)
2135 DPFROMREG(fs
, MIPSInst_FS(ir
));
2136 rv
.w
= ieee754dp_2008class(fs
);
2142 union ieee754dp fs
, ft
;
2144 if (!cpu_has_mips_r6
)
2147 DPFROMREG(ft
, MIPSInst_FT(ir
));
2148 DPFROMREG(fs
, MIPSInst_FS(ir
));
2149 rv
.d
= ieee754dp_fmin(fs
, ft
);
2154 union ieee754dp fs
, ft
;
2156 if (!cpu_has_mips_r6
)
2159 DPFROMREG(ft
, MIPSInst_FT(ir
));
2160 DPFROMREG(fs
, MIPSInst_FS(ir
));
2161 rv
.d
= ieee754dp_fmina(fs
, ft
);
2166 union ieee754dp fs
, ft
;
2168 if (!cpu_has_mips_r6
)
2171 DPFROMREG(ft
, MIPSInst_FT(ir
));
2172 DPFROMREG(fs
, MIPSInst_FS(ir
));
2173 rv
.d
= ieee754dp_fmax(fs
, ft
);
2178 union ieee754dp fs
, ft
;
2180 if (!cpu_has_mips_r6
)
2183 DPFROMREG(ft
, MIPSInst_FT(ir
));
2184 DPFROMREG(fs
, MIPSInst_FS(ir
));
2185 rv
.d
= ieee754dp_fmaxa(fs
, ft
);
2190 handler
.u
= ieee754dp_abs
;
2194 handler
.u
= ieee754dp_neg
;
2199 DPFROMREG(rv
.d
, MIPSInst_FS(ir
));
2202 /* binary op on handler */
2204 DPFROMREG(fs
, MIPSInst_FS(ir
));
2205 DPFROMREG(ft
, MIPSInst_FT(ir
));
2207 rv
.d
= (*handler
.b
) (fs
, ft
);
2210 DPFROMREG(fs
, MIPSInst_FS(ir
));
2211 rv
.d
= (*handler
.u
) (fs
);
2218 DPFROMREG(fs
, MIPSInst_FS(ir
));
2219 rv
.s
= ieee754sp_fdp(fs
);
2224 return SIGILL
; /* not defined */
2227 DPFROMREG(fs
, MIPSInst_FS(ir
));
2228 rv
.w
= ieee754dp_tint(fs
); /* wrong */
2236 if (!cpu_has_mips_2_3_4_5_r
)
2239 oldrm
= ieee754_csr
.rm
;
2240 DPFROMREG(fs
, MIPSInst_FS(ir
));
2241 ieee754_csr
.rm
= MIPSInst_FUNC(ir
);
2242 rv
.w
= ieee754dp_tint(fs
);
2243 ieee754_csr
.rm
= oldrm
;
2248 if (!cpu_has_mips_3_4_5_64_r2_r6
)
2251 DPFROMREG(fs
, MIPSInst_FS(ir
));
2252 rv
.l
= ieee754dp_tlong(fs
);
2260 if (!cpu_has_mips_3_4_5_64_r2_r6
)
2263 oldrm
= ieee754_csr
.rm
;
2264 DPFROMREG(fs
, MIPSInst_FS(ir
));
2265 ieee754_csr
.rm
= MIPSInst_FUNC(ir
);
2266 rv
.l
= ieee754dp_tlong(fs
);
2267 ieee754_csr
.rm
= oldrm
;
2272 if (!NO_R6EMU
&& MIPSInst_FUNC(ir
) >= fcmp_op
) {
2273 unsigned cmpop
= MIPSInst_FUNC(ir
) - fcmp_op
;
2274 union ieee754dp fs
, ft
;
2276 DPFROMREG(fs
, MIPSInst_FS(ir
));
2277 DPFROMREG(ft
, MIPSInst_FT(ir
));
2278 rv
.w
= ieee754dp_cmp(fs
, ft
,
2279 cmptab
[cmpop
& 0x7], cmpop
& 0x8);
2284 (IEEE754_INVALID_OPERATION
))
2285 rcsr
= FPU_CSR_INV_X
| FPU_CSR_INV_S
;
2301 switch (MIPSInst_FUNC(ir
)) {
2303 /* convert word to single precision real */
2304 SPFROMREG(fs
, MIPSInst_FS(ir
));
2305 rv
.s
= ieee754sp_fint(fs
.bits
);
2309 /* convert word to double precision real */
2310 SPFROMREG(fs
, MIPSInst_FS(ir
));
2311 rv
.d
= ieee754dp_fint(fs
.bits
);
2315 /* Emulating the new CMP.condn.fmt R6 instruction */
2316 #define CMPOP_MASK 0x7
2317 #define SIGN_BIT (0x1 << 3)
2318 #define PREDICATE_BIT (0x1 << 4)
2320 int cmpop
= MIPSInst_FUNC(ir
) & CMPOP_MASK
;
2321 int sig
= MIPSInst_FUNC(ir
) & SIGN_BIT
;
2322 union ieee754sp fs
, ft
;
2324 /* This is an R6 only instruction */
2325 if (!cpu_has_mips_r6
||
2326 (MIPSInst_FUNC(ir
) & 0x20))
2329 /* fmt is w_fmt for single precision so fix it */
2331 /* default to false */
2335 SPFROMREG(fs
, MIPSInst_FS(ir
));
2336 SPFROMREG(ft
, MIPSInst_FT(ir
));
2338 /* positive predicates */
2339 if (!(MIPSInst_FUNC(ir
) & PREDICATE_BIT
)) {
2340 if (ieee754sp_cmp(fs
, ft
, cmptab
[cmpop
],
2342 rv
.w
= -1; /* true, all 1s */
2344 ieee754_cxtest(IEEE754_INVALID_OPERATION
))
2345 rcsr
= FPU_CSR_INV_X
| FPU_CSR_INV_S
;
2349 /* negative predicates */
2354 if (ieee754sp_cmp(fs
, ft
,
2355 negative_cmptab
[cmpop
],
2357 rv
.w
= -1; /* true, all 1s */
2359 ieee754_cxtest(IEEE754_INVALID_OPERATION
))
2360 rcsr
= FPU_CSR_INV_X
| FPU_CSR_INV_S
;
2365 /* Reserved R6 ops */
2366 pr_err("Reserved MIPS R6 CMP.condn.S operation\n");
2377 if (!cpu_has_mips_3_4_5_64_r2_r6
)
2380 DIFROMREG(bits
, MIPSInst_FS(ir
));
2382 switch (MIPSInst_FUNC(ir
)) {
2384 /* convert long to single precision real */
2385 rv
.s
= ieee754sp_flong(bits
);
2389 /* convert long to double precision real */
2390 rv
.d
= ieee754dp_flong(bits
);
2394 /* Emulating the new CMP.condn.fmt R6 instruction */
2395 int cmpop
= MIPSInst_FUNC(ir
) & CMPOP_MASK
;
2396 int sig
= MIPSInst_FUNC(ir
) & SIGN_BIT
;
2397 union ieee754dp fs
, ft
;
2399 if (!cpu_has_mips_r6
||
2400 (MIPSInst_FUNC(ir
) & 0x20))
2403 /* fmt is l_fmt for double precision so fix it */
2405 /* default to false */
2409 DPFROMREG(fs
, MIPSInst_FS(ir
));
2410 DPFROMREG(ft
, MIPSInst_FT(ir
));
2412 /* positive predicates */
2413 if (!(MIPSInst_FUNC(ir
) & PREDICATE_BIT
)) {
2414 if (ieee754dp_cmp(fs
, ft
,
2415 cmptab
[cmpop
], sig
))
2416 rv
.l
= -1LL; /* true, all 1s */
2418 ieee754_cxtest(IEEE754_INVALID_OPERATION
))
2419 rcsr
= FPU_CSR_INV_X
| FPU_CSR_INV_S
;
2423 /* negative predicates */
2428 if (ieee754dp_cmp(fs
, ft
,
2429 negative_cmptab
[cmpop
],
2431 rv
.l
= -1LL; /* true, all 1s */
2433 ieee754_cxtest(IEEE754_INVALID_OPERATION
))
2434 rcsr
= FPU_CSR_INV_X
| FPU_CSR_INV_S
;
2439 /* Reserved R6 ops */
2440 pr_err("Reserved MIPS R6 CMP.condn.D operation\n");
2452 * Update the fpu CSR register for this operation.
2453 * If an exception is required, generate a tidy SIGFPE exception,
2454 * without updating the result register.
2455 * Note: cause exception bits do not accumulate, they are rewritten
2456 * for each op; only the flag/sticky bits accumulate.
2458 ctx
->fcr31
= (ctx
->fcr31
& ~FPU_CSR_ALL_X
) | rcsr
;
2459 if ((ctx
->fcr31
>> 5) & ctx
->fcr31
& FPU_CSR_ALL_E
) {
2460 /*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
2465 * Now we can safely write the result back to the register file.
2470 if (cpu_has_mips_4_5_r
)
2471 cbit
= fpucondbit
[MIPSInst_FD(ir
) >> 2];
2473 cbit
= FPU_CSR_COND
;
2477 ctx
->fcr31
&= ~cbit
;
2481 DPTOREG(rv
.d
, MIPSInst_FD(ir
));
2484 SPTOREG(rv
.s
, MIPSInst_FD(ir
));
2487 SITOREG(rv
.w
, MIPSInst_FD(ir
));
2490 if (!cpu_has_mips_3_4_5_64_r2_r6
)
2493 DITOREG(rv
.l
, MIPSInst_FD(ir
));
2502 int fpu_emulator_cop1Handler(struct pt_regs
*xcp
, struct mips_fpu_struct
*ctx
,
2503 int has_fpu
, void *__user
*fault_addr
)
2505 unsigned long oldepc
, prevepc
;
2506 struct mm_decoded_insn dec_insn
;
2511 oldepc
= xcp
->cp0_epc
;
2513 prevepc
= xcp
->cp0_epc
;
2515 if (get_isa16_mode(prevepc
) && cpu_has_mmips
) {
2517 * Get next 2 microMIPS instructions and convert them
2518 * into 32-bit instructions.
2520 if ((get_user(instr
[0], (u16 __user
*)msk_isa16_mode(xcp
->cp0_epc
))) ||
2521 (get_user(instr
[1], (u16 __user
*)msk_isa16_mode(xcp
->cp0_epc
+ 2))) ||
2522 (get_user(instr
[2], (u16 __user
*)msk_isa16_mode(xcp
->cp0_epc
+ 4))) ||
2523 (get_user(instr
[3], (u16 __user
*)msk_isa16_mode(xcp
->cp0_epc
+ 6)))) {
2524 MIPS_FPU_EMU_INC_STATS(errors
);
2529 /* Get first instruction. */
2530 if (mm_insn_16bit(*instr_ptr
)) {
2531 /* Duplicate the half-word. */
2532 dec_insn
.insn
= (*instr_ptr
<< 16) |
2534 /* 16-bit instruction. */
2535 dec_insn
.pc_inc
= 2;
2538 dec_insn
.insn
= (*instr_ptr
<< 16) |
2540 /* 32-bit instruction. */
2541 dec_insn
.pc_inc
= 4;
2544 /* Get second instruction. */
2545 if (mm_insn_16bit(*instr_ptr
)) {
2546 /* Duplicate the half-word. */
2547 dec_insn
.next_insn
= (*instr_ptr
<< 16) |
2549 /* 16-bit instruction. */
2550 dec_insn
.next_pc_inc
= 2;
2552 dec_insn
.next_insn
= (*instr_ptr
<< 16) |
2554 /* 32-bit instruction. */
2555 dec_insn
.next_pc_inc
= 4;
2557 dec_insn
.micro_mips_mode
= 1;
2559 if ((get_user(dec_insn
.insn
,
2560 (mips_instruction __user
*) xcp
->cp0_epc
)) ||
2561 (get_user(dec_insn
.next_insn
,
2562 (mips_instruction __user
*)(xcp
->cp0_epc
+4)))) {
2563 MIPS_FPU_EMU_INC_STATS(errors
);
2566 dec_insn
.pc_inc
= 4;
2567 dec_insn
.next_pc_inc
= 4;
2568 dec_insn
.micro_mips_mode
= 0;
2571 if ((dec_insn
.insn
== 0) ||
2572 ((dec_insn
.pc_inc
== 2) &&
2573 ((dec_insn
.insn
& 0xffff) == MM_NOP16
)))
2574 xcp
->cp0_epc
+= dec_insn
.pc_inc
; /* Skip NOPs */
2577 * The 'ieee754_csr' is an alias of ctx->fcr31.
2578 * No need to copy ctx->fcr31 to ieee754_csr.
2580 sig
= cop1Emulate(xcp
, ctx
, dec_insn
, fault_addr
);
2589 } while (xcp
->cp0_epc
> prevepc
);
2591 /* SIGILL indicates a non-fpu instruction */
2592 if (sig
== SIGILL
&& xcp
->cp0_epc
!= oldepc
)
2593 /* but if EPC has advanced, then ignore it */