Commit d8d4e3ae authored by Maneesh Soni's avatar Maneesh Soni Committed by Ralf Baechle

MIPS Kprobes: Refactor branch emulation

This patch refactors MIPS branch emulation code so as to allow skipping
delay slot instruction in case of branch likely instructions when branch is
not taken. This is useful for keeping the code common for use cases like
kprobes where one would like to handle the branch instructions keeping the
delay slot instuction also in picture for branch likely instructions. Also
allow emulation when instruction to be decoded is not at pt_regs->cp0_epc
as in case of kprobes where pt_regs->cp0_epc points to the breakpoint
instruction.

The patch also exports the function for modules.
Signed-off-by: default avatarManeesh Soni <manesoni@cisco.com>
Signed-off-by: default avatarVictor Kamensky <kamensky@cisco.com>
Cc: David Daney <david.daney@cavium.com>
Cc: ananth@in.ibm.com
Cc: linux-kernel@vger.kernel.org
Cc: linux-mips@linux-mips.org
Patchwork: https://patchwork.linux-mips.org/patch/2913/Signed-off-by: default avatarRalf Baechle <ralf@linux-mips.org>
parent 9233c1ee
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#define _ASM_BRANCH_H #define _ASM_BRANCH_H
#include <asm/ptrace.h> #include <asm/ptrace.h>
#include <asm/inst.h>
static inline int delay_slot(struct pt_regs *regs) static inline int delay_slot(struct pt_regs *regs)
{ {
...@@ -23,7 +24,11 @@ static inline unsigned long exception_epc(struct pt_regs *regs) ...@@ -23,7 +24,11 @@ static inline unsigned long exception_epc(struct pt_regs *regs)
return regs->cp0_epc + 4; return regs->cp0_epc + 4;
} }
#define BRANCH_LIKELY_TAKEN 0x0001
extern int __compute_return_epc(struct pt_regs *regs); extern int __compute_return_epc(struct pt_regs *regs);
extern int __compute_return_epc_for_insn(struct pt_regs *regs,
union mips_instruction insn);
static inline int compute_return_epc(struct pt_regs *regs) static inline int compute_return_epc(struct pt_regs *regs)
{ {
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/signal.h> #include <linux/signal.h>
#include <linux/module.h>
#include <asm/branch.h> #include <asm/branch.h>
#include <asm/cpu.h> #include <asm/cpu.h>
#include <asm/cpu-features.h> #include <asm/cpu-features.h>
...@@ -17,28 +18,22 @@ ...@@ -17,28 +18,22 @@
#include <asm/ptrace.h> #include <asm/ptrace.h>
#include <asm/uaccess.h> #include <asm/uaccess.h>
/* /**
* Compute the return address and do emulate branch simulation, if required. * __compute_return_epc_for_insn - Computes the return address and do emulate
* branch simulation, if required.
*
* @regs: Pointer to pt_regs
* @insn: branch instruction to decode
* @returns: -EFAULT on error and forces SIGBUS, and on success
* returns 0 or BRANCH_LIKELY_TAKEN as appropriate after
* evaluating the branch.
*/ */
int __compute_return_epc(struct pt_regs *regs) int __compute_return_epc_for_insn(struct pt_regs *regs,
union mips_instruction insn)
{ {
unsigned int __user *addr;
unsigned int bit, fcr31, dspcontrol; unsigned int bit, fcr31, dspcontrol;
long epc; long epc = regs->cp0_epc;
union mips_instruction insn; int ret = 0;
epc = regs->cp0_epc;
if (epc & 3)
goto unaligned;
/*
* Read the instruction
*/
addr = (unsigned int __user *) epc;
if (__get_user(insn.word, addr)) {
force_sig(SIGSEGV, current);
return -EFAULT;
}
switch (insn.i_format.opcode) { switch (insn.i_format.opcode) {
/* /*
...@@ -64,18 +59,22 @@ int __compute_return_epc(struct pt_regs *regs) ...@@ -64,18 +59,22 @@ int __compute_return_epc(struct pt_regs *regs)
switch (insn.i_format.rt) { switch (insn.i_format.rt) {
case bltz_op: case bltz_op:
case bltzl_op: case bltzl_op:
if ((long)regs->regs[insn.i_format.rs] < 0) if ((long)regs->regs[insn.i_format.rs] < 0) {
epc = epc + 4 + (insn.i_format.simmediate << 2); epc = epc + 4 + (insn.i_format.simmediate << 2);
else if (insn.i_format.rt == bltzl_op)
ret = BRANCH_LIKELY_TAKEN;
} else
epc += 8; epc += 8;
regs->cp0_epc = epc; regs->cp0_epc = epc;
break; break;
case bgez_op: case bgez_op:
case bgezl_op: case bgezl_op:
if ((long)regs->regs[insn.i_format.rs] >= 0) if ((long)regs->regs[insn.i_format.rs] >= 0) {
epc = epc + 4 + (insn.i_format.simmediate << 2); epc = epc + 4 + (insn.i_format.simmediate << 2);
else if (insn.i_format.rt == bgezl_op)
ret = BRANCH_LIKELY_TAKEN;
} else
epc += 8; epc += 8;
regs->cp0_epc = epc; regs->cp0_epc = epc;
break; break;
...@@ -83,9 +82,11 @@ int __compute_return_epc(struct pt_regs *regs) ...@@ -83,9 +82,11 @@ int __compute_return_epc(struct pt_regs *regs)
case bltzal_op: case bltzal_op:
case bltzall_op: case bltzall_op:
regs->regs[31] = epc + 8; regs->regs[31] = epc + 8;
if ((long)regs->regs[insn.i_format.rs] < 0) if ((long)regs->regs[insn.i_format.rs] < 0) {
epc = epc + 4 + (insn.i_format.simmediate << 2); epc = epc + 4 + (insn.i_format.simmediate << 2);
else if (insn.i_format.rt == bltzall_op)
ret = BRANCH_LIKELY_TAKEN;
} else
epc += 8; epc += 8;
regs->cp0_epc = epc; regs->cp0_epc = epc;
break; break;
...@@ -93,12 +94,15 @@ int __compute_return_epc(struct pt_regs *regs) ...@@ -93,12 +94,15 @@ int __compute_return_epc(struct pt_regs *regs)
case bgezal_op: case bgezal_op:
case bgezall_op: case bgezall_op:
regs->regs[31] = epc + 8; regs->regs[31] = epc + 8;
if ((long)regs->regs[insn.i_format.rs] >= 0) if ((long)regs->regs[insn.i_format.rs] >= 0) {
epc = epc + 4 + (insn.i_format.simmediate << 2); epc = epc + 4 + (insn.i_format.simmediate << 2);
else if (insn.i_format.rt == bgezall_op)
ret = BRANCH_LIKELY_TAKEN;
} else
epc += 8; epc += 8;
regs->cp0_epc = epc; regs->cp0_epc = epc;
break; break;
case bposge32_op: case bposge32_op:
if (!cpu_has_dsp) if (!cpu_has_dsp)
goto sigill; goto sigill;
...@@ -133,9 +137,11 @@ int __compute_return_epc(struct pt_regs *regs) ...@@ -133,9 +137,11 @@ int __compute_return_epc(struct pt_regs *regs)
case beq_op: case beq_op:
case beql_op: case beql_op:
if (regs->regs[insn.i_format.rs] == if (regs->regs[insn.i_format.rs] ==
regs->regs[insn.i_format.rt]) regs->regs[insn.i_format.rt]) {
epc = epc + 4 + (insn.i_format.simmediate << 2); epc = epc + 4 + (insn.i_format.simmediate << 2);
else if (insn.i_format.rt == beql_op)
ret = BRANCH_LIKELY_TAKEN;
} else
epc += 8; epc += 8;
regs->cp0_epc = epc; regs->cp0_epc = epc;
break; break;
...@@ -143,9 +149,11 @@ int __compute_return_epc(struct pt_regs *regs) ...@@ -143,9 +149,11 @@ int __compute_return_epc(struct pt_regs *regs)
case bne_op: case bne_op:
case bnel_op: case bnel_op:
if (regs->regs[insn.i_format.rs] != if (regs->regs[insn.i_format.rs] !=
regs->regs[insn.i_format.rt]) regs->regs[insn.i_format.rt]) {
epc = epc + 4 + (insn.i_format.simmediate << 2); epc = epc + 4 + (insn.i_format.simmediate << 2);
else if (insn.i_format.rt == bnel_op)
ret = BRANCH_LIKELY_TAKEN;
} else
epc += 8; epc += 8;
regs->cp0_epc = epc; regs->cp0_epc = epc;
break; break;
...@@ -153,9 +161,11 @@ int __compute_return_epc(struct pt_regs *regs) ...@@ -153,9 +161,11 @@ int __compute_return_epc(struct pt_regs *regs)
case blez_op: /* not really i_format */ case blez_op: /* not really i_format */
case blezl_op: case blezl_op:
/* rt field assumed to be zero */ /* rt field assumed to be zero */
if ((long)regs->regs[insn.i_format.rs] <= 0) if ((long)regs->regs[insn.i_format.rs] <= 0) {
epc = epc + 4 + (insn.i_format.simmediate << 2); epc = epc + 4 + (insn.i_format.simmediate << 2);
else if (insn.i_format.rt == bnel_op)
ret = BRANCH_LIKELY_TAKEN;
} else
epc += 8; epc += 8;
regs->cp0_epc = epc; regs->cp0_epc = epc;
break; break;
...@@ -163,9 +173,11 @@ int __compute_return_epc(struct pt_regs *regs) ...@@ -163,9 +173,11 @@ int __compute_return_epc(struct pt_regs *regs)
case bgtz_op: case bgtz_op:
case bgtzl_op: case bgtzl_op:
/* rt field assumed to be zero */ /* rt field assumed to be zero */
if ((long)regs->regs[insn.i_format.rs] > 0) if ((long)regs->regs[insn.i_format.rs] > 0) {
epc = epc + 4 + (insn.i_format.simmediate << 2); epc = epc + 4 + (insn.i_format.simmediate << 2);
else if (insn.i_format.rt == bnel_op)
ret = BRANCH_LIKELY_TAKEN;
} else
epc += 8; epc += 8;
regs->cp0_epc = epc; regs->cp0_epc = epc;
break; break;
...@@ -187,18 +199,22 @@ int __compute_return_epc(struct pt_regs *regs) ...@@ -187,18 +199,22 @@ int __compute_return_epc(struct pt_regs *regs)
switch (insn.i_format.rt & 3) { switch (insn.i_format.rt & 3) {
case 0: /* bc1f */ case 0: /* bc1f */
case 2: /* bc1fl */ case 2: /* bc1fl */
if (~fcr31 & (1 << bit)) if (~fcr31 & (1 << bit)) {
epc = epc + 4 + (insn.i_format.simmediate << 2); epc = epc + 4 + (insn.i_format.simmediate << 2);
else if (insn.i_format.rt == 2)
ret = BRANCH_LIKELY_TAKEN;
} else
epc += 8; epc += 8;
regs->cp0_epc = epc; regs->cp0_epc = epc;
break; break;
case 1: /* bc1t */ case 1: /* bc1t */
case 3: /* bc1tl */ case 3: /* bc1tl */
if (fcr31 & (1 << bit)) if (fcr31 & (1 << bit)) {
epc = epc + 4 + (insn.i_format.simmediate << 2); epc = epc + 4 + (insn.i_format.simmediate << 2);
else if (insn.i_format.rt == 3)
ret = BRANCH_LIKELY_TAKEN;
} else
epc += 8; epc += 8;
regs->cp0_epc = epc; regs->cp0_epc = epc;
break; break;
...@@ -239,15 +255,39 @@ int __compute_return_epc(struct pt_regs *regs) ...@@ -239,15 +255,39 @@ int __compute_return_epc(struct pt_regs *regs)
#endif #endif
} }
return 0; return ret;
unaligned: sigill:
printk("%s: unaligned epc - sending SIGBUS.\n", current->comm); printk("%s: DSP branch but not DSP ASE - sending SIGBUS.\n", current->comm);
force_sig(SIGBUS, current); force_sig(SIGBUS, current);
return -EFAULT; return -EFAULT;
}
EXPORT_SYMBOL_GPL(__compute_return_epc_for_insn);
sigill: int __compute_return_epc(struct pt_regs *regs)
printk("%s: DSP branch but not DSP ASE - sending SIGBUS.\n", current->comm); {
unsigned int __user *addr;
long epc;
union mips_instruction insn;
epc = regs->cp0_epc;
if (epc & 3)
goto unaligned;
/*
* Read the instruction
*/
addr = (unsigned int __user *) epc;
if (__get_user(insn.word, addr)) {
force_sig(SIGSEGV, current);
return -EFAULT;
}
return __compute_return_epc_for_insn(regs, insn);
unaligned:
printk("%s: unaligned epc - sending SIGBUS.\n", current->comm);
force_sig(SIGBUS, current); force_sig(SIGBUS, current);
return -EFAULT; return -EFAULT;
} }
...@@ -245,7 +245,7 @@ static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx, ...@@ -245,7 +245,7 @@ static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
*/ */
emulpc = xcp->cp0_epc + 4; /* Snapshot emulation target */ emulpc = xcp->cp0_epc + 4; /* Snapshot emulation target */
if (__compute_return_epc(xcp)) { if (__compute_return_epc(xcp) < 0) {
#ifdef CP1DBG #ifdef CP1DBG
printk("failed to emulate branch at %p\n", printk("failed to emulate branch at %p\n",
(void *) (xcp->cp0_epc)); (void *) (xcp->cp0_epc));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment