2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 1996, 97, 2000, 2001 by Ralf Baechle
7 * Copyright (C) 2001 MIPS Technologies, Inc.
9 #include <linux/kernel.h>
10 #include <linux/sched/signal.h>
11 #include <linux/signal.h>
12 #include <linux/export.h>
13 #include <asm/branch.h>
15 #include <asm/cpu-features.h>
17 #include <asm/fpu_emulator.h>
19 #include <asm/mips-r2-to-r6-emul.h>
20 #include <asm/ptrace.h>
21 #include <linux/uaccess.h>
24 * Calculate and return exception PC in case of branch delay slot
25 * for microMIPS and MIPS16e. It does not clear the ISA mode bit.
27 int __isa_exception_epc(struct pt_regs
*regs
)
30 long epc
= regs
->cp0_epc
;
32 /* Calculate exception PC in branch delay slot. */
33 if (__get_user(inst
, (u16 __user
*) msk_isa16_mode(epc
))) {
34 /* This should never happen because delay slot was checked. */
39 union mips16e_instruction inst_mips16e
;
41 inst_mips16e
.full
= inst
;
42 if (inst_mips16e
.ri
.opcode
== MIPS16e_jal_op
)
46 } else if (mm_insn_16bit(inst
))
54 /* (microMIPS) Convert 16-bit register encoding to 32-bit register encoding. */
55 static const unsigned int reg16to32map
[8] = {16, 17, 2, 3, 4, 5, 6, 7};
57 int __mm_isBranchInstr(struct pt_regs
*regs
, struct mm_decoded_insn dec_insn
,
58 unsigned long *contpc
)
60 union mips_instruction insn
= (union mips_instruction
)dec_insn
.insn
;
61 int __maybe_unused bc_false
= 0;
66 switch (insn
.mm_i_format
.opcode
) {
68 if ((insn
.mm_i_format
.simmediate
& MM_POOL32A_MINOR_MASK
) ==
70 switch (insn
.mm_i_format
.simmediate
>>
71 MM_POOL32A_MINOR_SHIFT
) {
76 if (insn
.mm_i_format
.rt
!= 0) /* Not mm_jr */
77 regs
->regs
[insn
.mm_i_format
.rt
] =
81 *contpc
= regs
->regs
[insn
.mm_i_format
.rs
];
87 switch (insn
.mm_i_format
.rt
) {
90 regs
->regs
[31] = regs
->cp0_epc
+
95 if ((long)regs
->regs
[insn
.mm_i_format
.rs
] < 0)
96 *contpc
= regs
->cp0_epc
+
98 (insn
.mm_i_format
.simmediate
<< 1);
100 *contpc
= regs
->cp0_epc
+
102 dec_insn
.next_pc_inc
;
106 regs
->regs
[31] = regs
->cp0_epc
+
108 dec_insn
.next_pc_inc
;
111 if ((long)regs
->regs
[insn
.mm_i_format
.rs
] >= 0)
112 *contpc
= regs
->cp0_epc
+
114 (insn
.mm_i_format
.simmediate
<< 1);
116 *contpc
= regs
->cp0_epc
+
118 dec_insn
.next_pc_inc
;
121 if ((long)regs
->regs
[insn
.mm_i_format
.rs
] <= 0)
122 *contpc
= regs
->cp0_epc
+
124 (insn
.mm_i_format
.simmediate
<< 1);
126 *contpc
= regs
->cp0_epc
+
128 dec_insn
.next_pc_inc
;
131 if ((long)regs
->regs
[insn
.mm_i_format
.rs
] <= 0)
132 *contpc
= regs
->cp0_epc
+
134 (insn
.mm_i_format
.simmediate
<< 1);
136 *contpc
= regs
->cp0_epc
+
138 dec_insn
.next_pc_inc
;
140 #ifdef CONFIG_MIPS_FP_SUPPORT
152 fcr31
= read_32bit_cp1_register(CP1_STATUS
);
154 fcr31
= current
->thread
.fpu
.fcr31
;
160 bit
= (insn
.mm_i_format
.rs
>> 2);
163 if (fcr31
& (1 << bit
))
164 *contpc
= regs
->cp0_epc
+
166 (insn
.mm_i_format
.simmediate
<< 1);
168 *contpc
= regs
->cp0_epc
+
169 dec_insn
.pc_inc
+ dec_insn
.next_pc_inc
;
172 #endif /* CONFIG_MIPS_FP_SUPPORT */
176 switch (insn
.mm_i_format
.rt
) {
179 regs
->regs
[31] = regs
->cp0_epc
+
180 dec_insn
.pc_inc
+ dec_insn
.next_pc_inc
;
183 *contpc
= regs
->regs
[insn
.mm_i_format
.rs
];
188 if ((long)regs
->regs
[reg16to32map
[insn
.mm_b1_format
.rs
]] == 0)
189 *contpc
= regs
->cp0_epc
+
191 (insn
.mm_b1_format
.simmediate
<< 1);
193 *contpc
= regs
->cp0_epc
+
194 dec_insn
.pc_inc
+ dec_insn
.next_pc_inc
;
197 if ((long)regs
->regs
[reg16to32map
[insn
.mm_b1_format
.rs
]] != 0)
198 *contpc
= regs
->cp0_epc
+
200 (insn
.mm_b1_format
.simmediate
<< 1);
202 *contpc
= regs
->cp0_epc
+
203 dec_insn
.pc_inc
+ dec_insn
.next_pc_inc
;
206 *contpc
= regs
->cp0_epc
+ dec_insn
.pc_inc
+
207 (insn
.mm_b0_format
.simmediate
<< 1);
210 if (regs
->regs
[insn
.mm_i_format
.rs
] ==
211 regs
->regs
[insn
.mm_i_format
.rt
])
212 *contpc
= regs
->cp0_epc
+
214 (insn
.mm_i_format
.simmediate
<< 1);
216 *contpc
= regs
->cp0_epc
+
218 dec_insn
.next_pc_inc
;
221 if (regs
->regs
[insn
.mm_i_format
.rs
] !=
222 regs
->regs
[insn
.mm_i_format
.rt
])
223 *contpc
= regs
->cp0_epc
+
225 (insn
.mm_i_format
.simmediate
<< 1);
227 *contpc
= regs
->cp0_epc
+
228 dec_insn
.pc_inc
+ dec_insn
.next_pc_inc
;
231 regs
->regs
[31] = regs
->cp0_epc
+
232 dec_insn
.pc_inc
+ dec_insn
.next_pc_inc
;
233 *contpc
= regs
->cp0_epc
+ dec_insn
.pc_inc
;
236 *contpc
|= (insn
.j_format
.target
<< 2);
240 regs
->regs
[31] = regs
->cp0_epc
+
241 dec_insn
.pc_inc
+ dec_insn
.next_pc_inc
;
244 *contpc
= regs
->cp0_epc
+ dec_insn
.pc_inc
;
247 *contpc
|= (insn
.j_format
.target
<< 1);
248 set_isa16_mode(*contpc
);
255 * Compute return address and emulate branch in microMIPS mode after an
256 * exception only. It does not handle compact branches/jumps and cannot
257 * be used in interrupt context. (Compact branches/jumps do not cause
260 int __microMIPS_compute_return_epc(struct pt_regs
*regs
)
265 unsigned long contpc
;
266 struct mm_decoded_insn mminsn
= { 0 };
268 mminsn
.micro_mips_mode
= 1;
270 /* This load never faults. */
271 pc16
= (unsigned short __user
*)msk_isa16_mode(regs
->cp0_epc
);
272 __get_user(halfword
, pc16
);
274 contpc
= regs
->cp0_epc
+ 2;
275 word
= ((unsigned int)halfword
<< 16);
278 if (!mm_insn_16bit(halfword
)) {
279 __get_user(halfword
, pc16
);
281 contpc
= regs
->cp0_epc
+ 4;
287 if (get_user(halfword
, pc16
))
289 mminsn
.next_pc_inc
= 2;
290 word
= ((unsigned int)halfword
<< 16);
292 if (!mm_insn_16bit(halfword
)) {
294 if (get_user(halfword
, pc16
))
296 mminsn
.next_pc_inc
= 4;
299 mminsn
.next_insn
= word
;
301 mm_isBranchInstr(regs
, mminsn
, &contpc
);
303 regs
->cp0_epc
= contpc
;
313 * Compute return address and emulate branch in MIPS16e mode after an
314 * exception only. It does not handle compact branches/jumps and cannot
315 * be used in interrupt context. (Compact branches/jumps do not cause
318 int __MIPS16e_compute_return_epc(struct pt_regs
*regs
)
321 union mips16e_instruction inst
;
328 /* Read the instruction. */
329 addr
= (u16 __user
*)msk_isa16_mode(epc
);
330 if (__get_user(inst
.full
, addr
)) {
335 switch (inst
.ri
.opcode
) {
336 case MIPS16e_extend_op
:
341 * JAL and JALX in MIPS16e mode
345 if (__get_user(inst2
, addr
)) {
349 fullinst
= ((unsigned)inst
.full
<< 16) | inst2
;
350 regs
->regs
[31] = epc
+ 6;
355 * JAL:5 X:1 TARGET[20-16]:5 TARGET[25:21]:5 TARGET[15:0]:16
357 * ......TARGET[15:0].................TARGET[20:16]...........
358 * ......TARGET[25:21]
361 ((fullinst
& 0xffff) << 2) | ((fullinst
& 0x3e00000) >> 3) |
362 ((fullinst
& 0x1f0000) << 7);
364 set_isa16_mode(epc
); /* Set ISA mode bit. */
372 if (inst
.rr
.func
== MIPS16e_jr_func
) {
375 regs
->cp0_epc
= regs
->regs
[31];
378 regs
->regs
[reg16to32
[inst
.rr
.rx
]];
382 regs
->regs
[31] = epc
+ 2;
384 regs
->regs
[31] = epc
+ 4;
392 * All other cases have no branch delay slot and are 16-bits.
393 * Branches do not cause an exception.
401 * __compute_return_epc_for_insn - Computes the return address and do emulate
402 * branch simulation, if required.
404 * @regs: Pointer to pt_regs
405 * @insn: branch instruction to decode
406 * Return: -EFAULT on error and forces SIGILL, and on success
407 * returns 0 or BRANCH_LIKELY_TAKEN as appropriate after
408 * evaluating the branch.
410 * MIPS R6 Compact branches and forbidden slots:
411 * Compact branches do not throw exceptions because they do
412 * not have delay slots. The forbidden slot instruction ($PC+4)
413 * is only executed if the branch was not taken. Otherwise the
414 * forbidden slot is skipped entirely. This means that the
415 * only possible reason to be here because of a MIPS R6 compact
416 * branch instruction is that the forbidden slot has thrown one.
417 * In that case the branch was not taken, so the EPC can be safely
420 int __compute_return_epc_for_insn(struct pt_regs
*regs
,
421 union mips_instruction insn
)
423 long epc
= regs
->cp0_epc
;
424 unsigned int dspcontrol
;
427 switch (insn
.i_format
.opcode
) {
429 * jr and jalr are in r_format format.
432 switch (insn
.r_format
.func
) {
434 regs
->regs
[insn
.r_format
.rd
] = epc
+ 8;
437 if (NO_R6EMU
&& insn
.r_format
.func
== jr_op
)
439 regs
->cp0_epc
= regs
->regs
[insn
.r_format
.rs
];
445 * This group contains:
446 * bltz_op, bgez_op, bltzl_op, bgezl_op,
447 * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
450 switch (insn
.i_format
.rt
) {
456 if ((long)regs
->regs
[insn
.i_format
.rs
] < 0) {
457 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
458 if (insn
.i_format
.rt
== bltzl_op
)
459 ret
= BRANCH_LIKELY_TAKEN
;
470 if ((long)regs
->regs
[insn
.i_format
.rs
] >= 0) {
471 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
472 if (insn
.i_format
.rt
== bgezl_op
)
473 ret
= BRANCH_LIKELY_TAKEN
;
481 if (NO_R6EMU
&& (insn
.i_format
.rs
||
482 insn
.i_format
.rt
== bltzall_op
))
484 regs
->regs
[31] = epc
+ 8;
486 * OK we are here either because we hit a NAL
487 * instruction or because we are emulating an
488 * old bltzal{,l} one. Let's figure out what the
491 if (!insn
.i_format
.rs
) {
493 * NAL or BLTZAL with rs == 0
494 * Doesn't matter if we are R6 or not. The
498 (insn
.i_format
.simmediate
<< 2);
501 /* Now do the real thing for non-R6 BLTZAL{,L} */
502 if ((long)regs
->regs
[insn
.i_format
.rs
] < 0) {
503 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
504 if (insn
.i_format
.rt
== bltzall_op
)
505 ret
= BRANCH_LIKELY_TAKEN
;
513 if (NO_R6EMU
&& (insn
.i_format
.rs
||
514 insn
.i_format
.rt
== bgezall_op
))
516 regs
->regs
[31] = epc
+ 8;
518 * OK we are here either because we hit a BAL
519 * instruction or because we are emulating an
520 * old bgezal{,l} one. Let's figure out what the
523 if (!insn
.i_format
.rs
) {
525 * BAL or BGEZAL with rs == 0
526 * Doesn't matter if we are R6 or not. The
530 (insn
.i_format
.simmediate
<< 2);
533 /* Now do the real thing for non-R6 BGEZAL{,L} */
534 if ((long)regs
->regs
[insn
.i_format
.rs
] >= 0) {
535 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
536 if (insn
.i_format
.rt
== bgezall_op
)
537 ret
= BRANCH_LIKELY_TAKEN
;
547 dspcontrol
= rddsp(0x01);
549 if (dspcontrol
>= 32) {
550 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
559 * These are unconditional and in j_format.
563 regs
->regs
[31] = regs
->cp0_epc
+ 8;
569 epc
|= (insn
.j_format
.target
<< 2);
571 if (insn
.i_format
.opcode
== jalx_op
)
572 set_isa16_mode(regs
->cp0_epc
);
576 * These are conditional and in i_format.
583 if (regs
->regs
[insn
.i_format
.rs
] ==
584 regs
->regs
[insn
.i_format
.rt
]) {
585 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
586 if (insn
.i_format
.opcode
== beql_op
)
587 ret
= BRANCH_LIKELY_TAKEN
;
598 if (regs
->regs
[insn
.i_format
.rs
] !=
599 regs
->regs
[insn
.i_format
.rt
]) {
600 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
601 if (insn
.i_format
.opcode
== bnel_op
)
602 ret
= BRANCH_LIKELY_TAKEN
;
608 case blezl_op
: /* not really i_format */
609 if (!insn
.i_format
.rt
&& NO_R6EMU
)
614 * Compact branches for R6 for the
615 * blez and blezl opcodes.
616 * BLEZ | rs = 0 | rt != 0 == BLEZALC
617 * BLEZ | rs = rt != 0 == BGEZALC
618 * BLEZ | rs != 0 | rt != 0 == BGEUC
619 * BLEZL | rs = 0 | rt != 0 == BLEZC
620 * BLEZL | rs = rt != 0 == BGEZC
621 * BLEZL | rs != 0 | rt != 0 == BGEC
623 * For real BLEZ{,L}, rt is always 0.
626 if (cpu_has_mips_r6
&& insn
.i_format
.rt
) {
627 if ((insn
.i_format
.opcode
== blez_op
) &&
628 ((!insn
.i_format
.rs
&& insn
.i_format
.rt
) ||
629 (insn
.i_format
.rs
== insn
.i_format
.rt
)))
630 regs
->regs
[31] = epc
+ 4;
634 /* rt field assumed to be zero */
635 if ((long)regs
->regs
[insn
.i_format
.rs
] <= 0) {
636 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
637 if (insn
.i_format
.opcode
== blezl_op
)
638 ret
= BRANCH_LIKELY_TAKEN
;
645 if (!insn
.i_format
.rt
&& NO_R6EMU
)
650 * Compact branches for R6 for the
651 * bgtz and bgtzl opcodes.
652 * BGTZ | rs = 0 | rt != 0 == BGTZALC
653 * BGTZ | rs = rt != 0 == BLTZALC
654 * BGTZ | rs != 0 | rt != 0 == BLTUC
655 * BGTZL | rs = 0 | rt != 0 == BGTZC
656 * BGTZL | rs = rt != 0 == BLTZC
657 * BGTZL | rs != 0 | rt != 0 == BLTC
659 * *ZALC varint for BGTZ &&& rt != 0
660 * For real GTZ{,L}, rt is always 0.
662 if (cpu_has_mips_r6
&& insn
.i_format
.rt
) {
663 if ((insn
.i_format
.opcode
== blez_op
) &&
664 ((!insn
.i_format
.rs
&& insn
.i_format
.rt
) ||
665 (insn
.i_format
.rs
== insn
.i_format
.rt
)))
666 regs
->regs
[31] = epc
+ 4;
671 /* rt field assumed to be zero */
672 if ((long)regs
->regs
[insn
.i_format
.rs
] > 0) {
673 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
674 if (insn
.i_format
.opcode
== bgtzl_op
)
675 ret
= BRANCH_LIKELY_TAKEN
;
681 #ifdef CONFIG_MIPS_FP_SUPPORT
683 * And now the FPA/cp1 branch instructions.
686 unsigned int bit
, fcr31
, reg
;
688 if (cpu_has_mips_r6
&&
689 ((insn
.i_format
.rs
== bc1eqz_op
) ||
690 (insn
.i_format
.rs
== bc1nez_op
))) {
691 if (!init_fp_ctx(current
))
693 reg
= insn
.i_format
.rt
;
694 bit
= get_fpr32(¤t
->thread
.fpu
.fpr
[reg
], 0) & 0x1;
695 if (insn
.i_format
.rs
== bc1eqz_op
)
700 (insn
.i_format
.simmediate
<< 2);
710 fcr31
= read_32bit_cp1_register(CP1_STATUS
);
712 fcr31
= current
->thread
.fpu
.fcr31
;
715 bit
= (insn
.i_format
.rt
>> 2);
718 switch (insn
.i_format
.rt
& 3) {
721 if (~fcr31
& (1 << bit
)) {
723 (insn
.i_format
.simmediate
<< 2);
724 if (insn
.i_format
.rt
== 2)
725 ret
= BRANCH_LIKELY_TAKEN
;
733 if (fcr31
& (1 << bit
)) {
735 (insn
.i_format
.simmediate
<< 2);
736 if (insn
.i_format
.rt
== 3)
737 ret
= BRANCH_LIKELY_TAKEN
;
746 #endif /* CONFIG_MIPS_FP_SUPPORT */
748 #ifdef CONFIG_CPU_CAVIUM_OCTEON
749 case lwc2_op
: /* This is bbit0 on Octeon */
750 if ((regs
->regs
[insn
.i_format
.rs
] & (1ull<<insn
.i_format
.rt
))
752 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
757 case ldc2_op
: /* This is bbit032 on Octeon */
758 if ((regs
->regs
[insn
.i_format
.rs
] &
759 (1ull<<(insn
.i_format
.rt
+32))) == 0)
760 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
765 case swc2_op
: /* This is bbit1 on Octeon */
766 if (regs
->regs
[insn
.i_format
.rs
] & (1ull<<insn
.i_format
.rt
))
767 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
772 case sdc2_op
: /* This is bbit132 on Octeon */
773 if (regs
->regs
[insn
.i_format
.rs
] &
774 (1ull<<(insn
.i_format
.rt
+32)))
775 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
782 /* Only valid for MIPS R6 */
783 if (!cpu_has_mips_r6
)
788 if (!cpu_has_mips_r6
)
790 /* Compact branch: BALC */
791 regs
->regs
[31] = epc
+ 4;
792 epc
+= 4 + (insn
.i_format
.simmediate
<< 2);
796 if (!cpu_has_mips_r6
)
798 /* Compact branch: BEQZC || JIC */
802 if (!cpu_has_mips_r6
)
804 /* Compact branch: BNEZC || JIALC */
805 if (!insn
.i_format
.rs
) {
806 /* JIALC: set $31/ra */
807 regs
->regs
[31] = epc
+ 4;
814 /* Only valid for MIPS R6 */
815 if (!cpu_has_mips_r6
)
819 * bovc, beqc, beqzalc, bnvc, bnec, bnezlac
821 if (insn
.i_format
.rt
&& !insn
.i_format
.rs
)
822 regs
->regs
[31] = epc
+ 4;
830 pr_debug("%s: DSP branch but not DSP ASE - sending SIGILL.\n",
835 pr_debug("%s: R2 branch but r2-to-r6 emulator is not present - sending SIGILL.\n",
840 pr_debug("%s: R6 branch but no MIPSr6 ISA support - sending SIGILL.\n",
845 EXPORT_SYMBOL_GPL(__compute_return_epc_for_insn
);
847 int __compute_return_epc(struct pt_regs
*regs
)
849 unsigned int __user
*addr
;
851 union mips_instruction insn
;
858 * Read the instruction
860 addr
= (unsigned int __user
*) epc
;
861 if (__get_user(insn
.word
, addr
)) {
866 return __compute_return_epc_for_insn(regs
, insn
);
869 printk("%s: unaligned epc - sending SIGBUS.\n", current
->comm
);
874 #if (defined CONFIG_KPROBES) || (defined CONFIG_UPROBES)
876 int __insn_is_compact_branch(union mips_instruction insn
)
878 if (!cpu_has_mips_r6
)
881 switch (insn
.i_format
.opcode
) {
887 * blez[l] and bgtz[l] opcodes with non-zero rt
888 * are MIPS R6 compact branches
890 if (insn
.i_format
.rt
)
904 EXPORT_SYMBOL_GPL(__insn_is_compact_branch
);
906 #endif /* CONFIG_KPROBES || CONFIG_UPROBES */