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>
23 #include "probes-common.h"
26 * Calculate and return exception PC in case of branch delay slot
27 * for microMIPS and MIPS16e. It does not clear the ISA mode bit.
29 int __isa_exception_epc(struct pt_regs
*regs
)
32 long epc
= regs
->cp0_epc
;
34 /* Calculate exception PC in branch delay slot. */
35 if (__get_user(inst
, (u16 __user
*) msk_isa16_mode(epc
))) {
36 /* This should never happen because delay slot was checked. */
41 union mips16e_instruction inst_mips16e
;
43 inst_mips16e
.full
= inst
;
44 if (inst_mips16e
.ri
.opcode
== MIPS16e_jal_op
)
48 } else if (mm_insn_16bit(inst
))
56 /* (microMIPS) Convert 16-bit register encoding to 32-bit register encoding. */
57 static const unsigned int reg16to32map
[8] = {16, 17, 2, 3, 4, 5, 6, 7};
59 int __mm_isBranchInstr(struct pt_regs
*regs
, struct mm_decoded_insn dec_insn
,
60 unsigned long *contpc
)
62 union mips_instruction insn
= (union mips_instruction
)dec_insn
.insn
;
63 int __maybe_unused bc_false
= 0;
68 switch (insn
.mm_i_format
.opcode
) {
70 if ((insn
.mm_i_format
.simmediate
& MM_POOL32A_MINOR_MASK
) ==
72 switch (insn
.mm_i_format
.simmediate
>>
73 MM_POOL32A_MINOR_SHIFT
) {
78 if (insn
.mm_i_format
.rt
!= 0) /* Not mm_jr */
79 regs
->regs
[insn
.mm_i_format
.rt
] =
83 *contpc
= regs
->regs
[insn
.mm_i_format
.rs
];
89 switch (insn
.mm_i_format
.rt
) {
92 regs
->regs
[31] = regs
->cp0_epc
+
97 if ((long)regs
->regs
[insn
.mm_i_format
.rs
] < 0)
98 *contpc
= regs
->cp0_epc
+
100 (insn
.mm_i_format
.simmediate
<< 1);
102 *contpc
= regs
->cp0_epc
+
104 dec_insn
.next_pc_inc
;
108 regs
->regs
[31] = regs
->cp0_epc
+
110 dec_insn
.next_pc_inc
;
113 if ((long)regs
->regs
[insn
.mm_i_format
.rs
] >= 0)
114 *contpc
= regs
->cp0_epc
+
116 (insn
.mm_i_format
.simmediate
<< 1);
118 *contpc
= regs
->cp0_epc
+
120 dec_insn
.next_pc_inc
;
123 if ((long)regs
->regs
[insn
.mm_i_format
.rs
] <= 0)
124 *contpc
= regs
->cp0_epc
+
126 (insn
.mm_i_format
.simmediate
<< 1);
128 *contpc
= regs
->cp0_epc
+
130 dec_insn
.next_pc_inc
;
133 if ((long)regs
->regs
[insn
.mm_i_format
.rs
] <= 0)
134 *contpc
= regs
->cp0_epc
+
136 (insn
.mm_i_format
.simmediate
<< 1);
138 *contpc
= regs
->cp0_epc
+
140 dec_insn
.next_pc_inc
;
142 #ifdef CONFIG_MIPS_FP_SUPPORT
154 fcr31
= read_32bit_cp1_register(CP1_STATUS
);
156 fcr31
= current
->thread
.fpu
.fcr31
;
162 bit
= (insn
.mm_i_format
.rs
>> 2);
165 if (fcr31
& (1 << bit
))
166 *contpc
= regs
->cp0_epc
+
168 (insn
.mm_i_format
.simmediate
<< 1);
170 *contpc
= regs
->cp0_epc
+
171 dec_insn
.pc_inc
+ dec_insn
.next_pc_inc
;
174 #endif /* CONFIG_MIPS_FP_SUPPORT */
178 switch (insn
.mm_i_format
.rt
) {
181 regs
->regs
[31] = regs
->cp0_epc
+
182 dec_insn
.pc_inc
+ dec_insn
.next_pc_inc
;
185 *contpc
= regs
->regs
[insn
.mm_i_format
.rs
];
190 if ((long)regs
->regs
[reg16to32map
[insn
.mm_b1_format
.rs
]] == 0)
191 *contpc
= regs
->cp0_epc
+
193 (insn
.mm_b1_format
.simmediate
<< 1);
195 *contpc
= regs
->cp0_epc
+
196 dec_insn
.pc_inc
+ dec_insn
.next_pc_inc
;
199 if ((long)regs
->regs
[reg16to32map
[insn
.mm_b1_format
.rs
]] != 0)
200 *contpc
= regs
->cp0_epc
+
202 (insn
.mm_b1_format
.simmediate
<< 1);
204 *contpc
= regs
->cp0_epc
+
205 dec_insn
.pc_inc
+ dec_insn
.next_pc_inc
;
208 *contpc
= regs
->cp0_epc
+ dec_insn
.pc_inc
+
209 (insn
.mm_b0_format
.simmediate
<< 1);
212 if (regs
->regs
[insn
.mm_i_format
.rs
] ==
213 regs
->regs
[insn
.mm_i_format
.rt
])
214 *contpc
= regs
->cp0_epc
+
216 (insn
.mm_i_format
.simmediate
<< 1);
218 *contpc
= regs
->cp0_epc
+
220 dec_insn
.next_pc_inc
;
223 if (regs
->regs
[insn
.mm_i_format
.rs
] !=
224 regs
->regs
[insn
.mm_i_format
.rt
])
225 *contpc
= regs
->cp0_epc
+
227 (insn
.mm_i_format
.simmediate
<< 1);
229 *contpc
= regs
->cp0_epc
+
230 dec_insn
.pc_inc
+ dec_insn
.next_pc_inc
;
233 regs
->regs
[31] = regs
->cp0_epc
+
234 dec_insn
.pc_inc
+ dec_insn
.next_pc_inc
;
235 *contpc
= regs
->cp0_epc
+ dec_insn
.pc_inc
;
238 *contpc
|= (insn
.j_format
.target
<< 2);
242 regs
->regs
[31] = regs
->cp0_epc
+
243 dec_insn
.pc_inc
+ dec_insn
.next_pc_inc
;
246 *contpc
= regs
->cp0_epc
+ dec_insn
.pc_inc
;
249 *contpc
|= (insn
.j_format
.target
<< 1);
250 set_isa16_mode(*contpc
);
257 * Compute return address and emulate branch in microMIPS mode after an
258 * exception only. It does not handle compact branches/jumps and cannot
259 * be used in interrupt context. (Compact branches/jumps do not cause
262 int __microMIPS_compute_return_epc(struct pt_regs
*regs
)
267 unsigned long contpc
;
268 struct mm_decoded_insn mminsn
= { 0 };
270 mminsn
.micro_mips_mode
= 1;
272 /* This load never faults. */
273 pc16
= (unsigned short __user
*)msk_isa16_mode(regs
->cp0_epc
);
274 __get_user(halfword
, pc16
);
276 contpc
= regs
->cp0_epc
+ 2;
277 word
= ((unsigned int)halfword
<< 16);
280 if (!mm_insn_16bit(halfword
)) {
281 __get_user(halfword
, pc16
);
283 contpc
= regs
->cp0_epc
+ 4;
289 if (get_user(halfword
, pc16
))
291 mminsn
.next_pc_inc
= 2;
292 word
= ((unsigned int)halfword
<< 16);
294 if (!mm_insn_16bit(halfword
)) {
296 if (get_user(halfword
, pc16
))
298 mminsn
.next_pc_inc
= 4;
301 mminsn
.next_insn
= word
;
303 mm_isBranchInstr(regs
, mminsn
, &contpc
);
305 regs
->cp0_epc
= contpc
;
315 * Compute return address and emulate branch in MIPS16e mode after an
316 * exception only. It does not handle compact branches/jumps and cannot
317 * be used in interrupt context. (Compact branches/jumps do not cause
320 int __MIPS16e_compute_return_epc(struct pt_regs
*regs
)
323 union mips16e_instruction inst
;
330 /* Read the instruction. */
331 addr
= (u16 __user
*)msk_isa16_mode(epc
);
332 if (__get_user(inst
.full
, addr
)) {
337 switch (inst
.ri
.opcode
) {
338 case MIPS16e_extend_op
:
343 * JAL and JALX in MIPS16e mode
347 if (__get_user(inst2
, addr
)) {
351 fullinst
= ((unsigned)inst
.full
<< 16) | inst2
;
352 regs
->regs
[31] = epc
+ 6;
357 * JAL:5 X:1 TARGET[20-16]:5 TARGET[25:21]:5 TARGET[15:0]:16
359 * ......TARGET[15:0].................TARGET[20:16]...........
360 * ......TARGET[25:21]
363 ((fullinst
& 0xffff) << 2) | ((fullinst
& 0x3e00000) >> 3) |
364 ((fullinst
& 0x1f0000) << 7);
366 set_isa16_mode(epc
); /* Set ISA mode bit. */
374 if (inst
.rr
.func
== MIPS16e_jr_func
) {
377 regs
->cp0_epc
= regs
->regs
[31];
380 regs
->regs
[reg16to32
[inst
.rr
.rx
]];
384 regs
->regs
[31] = epc
+ 2;
386 regs
->regs
[31] = epc
+ 4;
394 * All other cases have no branch delay slot and are 16-bits.
395 * Branches do not cause an exception.
403 * __compute_return_epc_for_insn - Computes the return address and do emulate
404 * branch simulation, if required.
406 * @regs: Pointer to pt_regs
407 * @insn: branch instruction to decode
408 * Return: -EFAULT on error and forces SIGILL, and on success
409 * returns 0 or BRANCH_LIKELY_TAKEN as appropriate after
410 * evaluating the branch.
412 * MIPS R6 Compact branches and forbidden slots:
413 * Compact branches do not throw exceptions because they do
414 * not have delay slots. The forbidden slot instruction ($PC+4)
415 * is only executed if the branch was not taken. Otherwise the
416 * forbidden slot is skipped entirely. This means that the
417 * only possible reason to be here because of a MIPS R6 compact
418 * branch instruction is that the forbidden slot has thrown one.
419 * In that case the branch was not taken, so the EPC can be safely
422 int __compute_return_epc_for_insn(struct pt_regs
*regs
,
423 union mips_instruction insn
)
425 long epc
= regs
->cp0_epc
;
426 unsigned int dspcontrol
;
429 switch (insn
.i_format
.opcode
) {
431 * jr and jalr are in r_format format.
434 switch (insn
.r_format
.func
) {
436 regs
->regs
[insn
.r_format
.rd
] = epc
+ 8;
439 if (NO_R6EMU
&& insn
.r_format
.func
== jr_op
)
441 regs
->cp0_epc
= regs
->regs
[insn
.r_format
.rs
];
447 * This group contains:
448 * bltz_op, bgez_op, bltzl_op, bgezl_op,
449 * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
452 switch (insn
.i_format
.rt
) {
458 if ((long)regs
->regs
[insn
.i_format
.rs
] < 0) {
459 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
460 if (insn
.i_format
.rt
== bltzl_op
)
461 ret
= BRANCH_LIKELY_TAKEN
;
472 if ((long)regs
->regs
[insn
.i_format
.rs
] >= 0) {
473 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
474 if (insn
.i_format
.rt
== bgezl_op
)
475 ret
= BRANCH_LIKELY_TAKEN
;
483 if (NO_R6EMU
&& (insn
.i_format
.rs
||
484 insn
.i_format
.rt
== bltzall_op
))
486 regs
->regs
[31] = epc
+ 8;
488 * OK we are here either because we hit a NAL
489 * instruction or because we are emulating an
490 * old bltzal{,l} one. Let's figure out what the
493 if (!insn
.i_format
.rs
) {
495 * NAL or BLTZAL with rs == 0
496 * Doesn't matter if we are R6 or not. The
500 (insn
.i_format
.simmediate
<< 2);
503 /* Now do the real thing for non-R6 BLTZAL{,L} */
504 if ((long)regs
->regs
[insn
.i_format
.rs
] < 0) {
505 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
506 if (insn
.i_format
.rt
== bltzall_op
)
507 ret
= BRANCH_LIKELY_TAKEN
;
515 if (NO_R6EMU
&& (insn
.i_format
.rs
||
516 insn
.i_format
.rt
== bgezall_op
))
518 regs
->regs
[31] = epc
+ 8;
520 * OK we are here either because we hit a BAL
521 * instruction or because we are emulating an
522 * old bgezal{,l} one. Let's figure out what the
525 if (!insn
.i_format
.rs
) {
527 * BAL or BGEZAL with rs == 0
528 * Doesn't matter if we are R6 or not. The
532 (insn
.i_format
.simmediate
<< 2);
535 /* Now do the real thing for non-R6 BGEZAL{,L} */
536 if ((long)regs
->regs
[insn
.i_format
.rs
] >= 0) {
537 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
538 if (insn
.i_format
.rt
== bgezall_op
)
539 ret
= BRANCH_LIKELY_TAKEN
;
549 dspcontrol
= rddsp(0x01);
551 if (dspcontrol
>= 32) {
552 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
561 * These are unconditional and in j_format.
565 regs
->regs
[31] = regs
->cp0_epc
+ 8;
571 epc
|= (insn
.j_format
.target
<< 2);
573 if (insn
.i_format
.opcode
== jalx_op
)
574 set_isa16_mode(regs
->cp0_epc
);
578 * These are conditional and in i_format.
585 if (regs
->regs
[insn
.i_format
.rs
] ==
586 regs
->regs
[insn
.i_format
.rt
]) {
587 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
588 if (insn
.i_format
.opcode
== beql_op
)
589 ret
= BRANCH_LIKELY_TAKEN
;
600 if (regs
->regs
[insn
.i_format
.rs
] !=
601 regs
->regs
[insn
.i_format
.rt
]) {
602 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
603 if (insn
.i_format
.opcode
== bnel_op
)
604 ret
= BRANCH_LIKELY_TAKEN
;
610 case blezl_op
: /* not really i_format */
611 if (!insn
.i_format
.rt
&& NO_R6EMU
)
616 * Compact branches for R6 for the
617 * blez and blezl opcodes.
618 * BLEZ | rs = 0 | rt != 0 == BLEZALC
619 * BLEZ | rs = rt != 0 == BGEZALC
620 * BLEZ | rs != 0 | rt != 0 == BGEUC
621 * BLEZL | rs = 0 | rt != 0 == BLEZC
622 * BLEZL | rs = rt != 0 == BGEZC
623 * BLEZL | rs != 0 | rt != 0 == BGEC
625 * For real BLEZ{,L}, rt is always 0.
628 if (cpu_has_mips_r6
&& insn
.i_format
.rt
) {
629 if ((insn
.i_format
.opcode
== blez_op
) &&
630 ((!insn
.i_format
.rs
&& insn
.i_format
.rt
) ||
631 (insn
.i_format
.rs
== insn
.i_format
.rt
)))
632 regs
->regs
[31] = epc
+ 4;
636 /* rt field assumed to be zero */
637 if ((long)regs
->regs
[insn
.i_format
.rs
] <= 0) {
638 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
639 if (insn
.i_format
.opcode
== blezl_op
)
640 ret
= BRANCH_LIKELY_TAKEN
;
647 if (!insn
.i_format
.rt
&& NO_R6EMU
)
652 * Compact branches for R6 for the
653 * bgtz and bgtzl opcodes.
654 * BGTZ | rs = 0 | rt != 0 == BGTZALC
655 * BGTZ | rs = rt != 0 == BLTZALC
656 * BGTZ | rs != 0 | rt != 0 == BLTUC
657 * BGTZL | rs = 0 | rt != 0 == BGTZC
658 * BGTZL | rs = rt != 0 == BLTZC
659 * BGTZL | rs != 0 | rt != 0 == BLTC
661 * *ZALC varint for BGTZ &&& rt != 0
662 * For real GTZ{,L}, rt is always 0.
664 if (cpu_has_mips_r6
&& insn
.i_format
.rt
) {
665 if ((insn
.i_format
.opcode
== blez_op
) &&
666 ((!insn
.i_format
.rs
&& insn
.i_format
.rt
) ||
667 (insn
.i_format
.rs
== insn
.i_format
.rt
)))
668 regs
->regs
[31] = epc
+ 4;
673 /* rt field assumed to be zero */
674 if ((long)regs
->regs
[insn
.i_format
.rs
] > 0) {
675 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
676 if (insn
.i_format
.opcode
== bgtzl_op
)
677 ret
= BRANCH_LIKELY_TAKEN
;
683 #ifdef CONFIG_MIPS_FP_SUPPORT
685 * And now the FPA/cp1 branch instructions.
688 unsigned int bit
, fcr31
, reg
;
690 if (cpu_has_mips_r6
&&
691 ((insn
.i_format
.rs
== bc1eqz_op
) ||
692 (insn
.i_format
.rs
== bc1nez_op
))) {
693 if (!init_fp_ctx(current
))
695 reg
= insn
.i_format
.rt
;
696 bit
= get_fpr32(¤t
->thread
.fpu
.fpr
[reg
], 0) & 0x1;
697 if (insn
.i_format
.rs
== bc1eqz_op
)
702 (insn
.i_format
.simmediate
<< 2);
712 fcr31
= read_32bit_cp1_register(CP1_STATUS
);
714 fcr31
= current
->thread
.fpu
.fcr31
;
717 bit
= (insn
.i_format
.rt
>> 2);
720 switch (insn
.i_format
.rt
& 3) {
723 if (~fcr31
& (1 << bit
)) {
725 (insn
.i_format
.simmediate
<< 2);
726 if (insn
.i_format
.rt
== 2)
727 ret
= BRANCH_LIKELY_TAKEN
;
735 if (fcr31
& (1 << bit
)) {
737 (insn
.i_format
.simmediate
<< 2);
738 if (insn
.i_format
.rt
== 3)
739 ret
= BRANCH_LIKELY_TAKEN
;
748 #endif /* CONFIG_MIPS_FP_SUPPORT */
750 #ifdef CONFIG_CPU_CAVIUM_OCTEON
751 case lwc2_op
: /* This is bbit0 on Octeon */
752 if ((regs
->regs
[insn
.i_format
.rs
] & (1ull<<insn
.i_format
.rt
))
754 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
759 case ldc2_op
: /* This is bbit032 on Octeon */
760 if ((regs
->regs
[insn
.i_format
.rs
] &
761 (1ull<<(insn
.i_format
.rt
+32))) == 0)
762 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
767 case swc2_op
: /* This is bbit1 on Octeon */
768 if (regs
->regs
[insn
.i_format
.rs
] & (1ull<<insn
.i_format
.rt
))
769 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
774 case sdc2_op
: /* This is bbit132 on Octeon */
775 if (regs
->regs
[insn
.i_format
.rs
] &
776 (1ull<<(insn
.i_format
.rt
+32)))
777 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
784 /* Only valid for MIPS R6 */
785 if (!cpu_has_mips_r6
)
790 if (!cpu_has_mips_r6
)
792 /* Compact branch: BALC */
793 regs
->regs
[31] = epc
+ 4;
794 epc
+= 4 + (insn
.i_format
.simmediate
<< 2);
798 if (!cpu_has_mips_r6
)
800 /* Compact branch: BEQZC || JIC */
804 if (!cpu_has_mips_r6
)
806 /* Compact branch: BNEZC || JIALC */
807 if (!insn
.i_format
.rs
) {
808 /* JIALC: set $31/ra */
809 regs
->regs
[31] = epc
+ 4;
816 /* Only valid for MIPS R6 */
817 if (!cpu_has_mips_r6
)
821 * bovc, beqc, beqzalc, bnvc, bnec, bnezlac
823 if (insn
.i_format
.rt
&& !insn
.i_format
.rs
)
824 regs
->regs
[31] = epc
+ 4;
832 pr_debug("%s: DSP branch but not DSP ASE - sending SIGILL.\n",
837 pr_debug("%s: R2 branch but r2-to-r6 emulator is not present - sending SIGILL.\n",
842 pr_debug("%s: R6 branch but no MIPSr6 ISA support - sending SIGILL.\n",
847 EXPORT_SYMBOL_GPL(__compute_return_epc_for_insn
);
849 int __compute_return_epc(struct pt_regs
*regs
)
851 unsigned int __user
*addr
;
853 union mips_instruction insn
;
860 * Read the instruction
862 addr
= (unsigned int __user
*) epc
;
863 if (__get_user(insn
.word
, addr
)) {
868 return __compute_return_epc_for_insn(regs
, insn
);
871 printk("%s: unaligned epc - sending SIGBUS.\n", current
->comm
);
876 #if (defined CONFIG_KPROBES) || (defined CONFIG_UPROBES)
878 int __insn_is_compact_branch(union mips_instruction insn
)
880 if (!cpu_has_mips_r6
)
883 switch (insn
.i_format
.opcode
) {
889 * blez[l] and bgtz[l] opcodes with non-zero rt
890 * are MIPS R6 compact branches
892 if (insn
.i_format
.rt
)
906 EXPORT_SYMBOL_GPL(__insn_is_compact_branch
);
908 #endif /* CONFIG_KPROBES || CONFIG_UPROBES */