Commit 0dc036c9 authored by Ananth N Mavinakayanahalli's avatar Ananth N Mavinakayanahalli Committed by Linus Torvalds

[PATCH] Kprobes: Track kprobe on a per_cpu basis - ppc64 changes

PPC64 changes to track kprobe execution on a per-cpu basis.  We now track the
kprobe state machine independently on each cpu using an arch specific kprobe
control block.
Signed-off-by: default avatarAnanth N Mavinakayanahalli <ananth@in.ibm.com>
Signed-off-by: default avatarAnil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 8a5c4dc5
...@@ -37,12 +37,8 @@ ...@@ -37,12 +37,8 @@
#include <asm/sstep.h> #include <asm/sstep.h>
static DECLARE_MUTEX(kprobe_mutex); static DECLARE_MUTEX(kprobe_mutex);
DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
static struct kprobe *current_kprobe; DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
static unsigned long kprobe_status, kprobe_saved_msr;
static struct kprobe *kprobe_prev;
static unsigned long kprobe_status_prev, kprobe_saved_msr_prev;
static struct pt_regs jprobe_saved_regs;
int __kprobes arch_prepare_kprobe(struct kprobe *p) int __kprobes arch_prepare_kprobe(struct kprobe *p)
{ {
...@@ -108,18 +104,25 @@ static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs) ...@@ -108,18 +104,25 @@ static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
regs->nip = (unsigned long)p->ainsn.insn; regs->nip = (unsigned long)p->ainsn.insn;
} }
static inline void save_previous_kprobe(void) static inline void save_previous_kprobe(struct kprobe_ctlblk *kcb)
{
kcb->prev_kprobe.kp = kprobe_running();
kcb->prev_kprobe.status = kcb->kprobe_status;
kcb->prev_kprobe.saved_msr = kcb->kprobe_saved_msr;
}
static inline void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
{ {
kprobe_prev = current_kprobe; __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
kprobe_status_prev = kprobe_status; kcb->kprobe_status = kcb->prev_kprobe.status;
kprobe_saved_msr_prev = kprobe_saved_msr; kcb->kprobe_saved_msr = kcb->prev_kprobe.saved_msr;
} }
static inline void restore_previous_kprobe(void) static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
struct kprobe_ctlblk *kcb)
{ {
current_kprobe = kprobe_prev; __get_cpu_var(current_kprobe) = p;
kprobe_status = kprobe_status_prev; kcb->kprobe_saved_msr = regs->msr;
kprobe_saved_msr = kprobe_saved_msr_prev;
} }
void __kprobes arch_prepare_kretprobe(struct kretprobe *rp, void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
...@@ -145,6 +148,7 @@ static inline int kprobe_handler(struct pt_regs *regs) ...@@ -145,6 +148,7 @@ static inline int kprobe_handler(struct pt_regs *regs)
struct kprobe *p; struct kprobe *p;
int ret = 0; int ret = 0;
unsigned int *addr = (unsigned int *)regs->nip; unsigned int *addr = (unsigned int *)regs->nip;
struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
/* Check we're not actually recursing */ /* Check we're not actually recursing */
if (kprobe_running()) { if (kprobe_running()) {
...@@ -153,10 +157,10 @@ static inline int kprobe_handler(struct pt_regs *regs) ...@@ -153,10 +157,10 @@ static inline int kprobe_handler(struct pt_regs *regs)
p = get_kprobe(addr); p = get_kprobe(addr);
if (p) { if (p) {
kprobe_opcode_t insn = *p->ainsn.insn; kprobe_opcode_t insn = *p->ainsn.insn;
if (kprobe_status == KPROBE_HIT_SS && if (kcb->kprobe_status == KPROBE_HIT_SS &&
is_trap(insn)) { is_trap(insn)) {
regs->msr &= ~MSR_SE; regs->msr &= ~MSR_SE;
regs->msr |= kprobe_saved_msr; regs->msr |= kcb->kprobe_saved_msr;
unlock_kprobes(); unlock_kprobes();
goto no_kprobe; goto no_kprobe;
} }
...@@ -166,15 +170,15 @@ static inline int kprobe_handler(struct pt_regs *regs) ...@@ -166,15 +170,15 @@ static inline int kprobe_handler(struct pt_regs *regs)
* just single step on the instruction of the new probe * just single step on the instruction of the new probe
* without calling any user handlers. * without calling any user handlers.
*/ */
save_previous_kprobe(); save_previous_kprobe(kcb);
current_kprobe = p; set_current_kprobe(p, regs, kcb);
kprobe_saved_msr = regs->msr; kcb->kprobe_saved_msr = regs->msr;
p->nmissed++; p->nmissed++;
prepare_singlestep(p, regs); prepare_singlestep(p, regs);
kprobe_status = KPROBE_REENTER; kcb->kprobe_status = KPROBE_REENTER;
return 1; return 1;
} else { } else {
p = current_kprobe; p = __get_cpu_var(current_kprobe);
if (p->break_handler && p->break_handler(p, regs)) { if (p->break_handler && p->break_handler(p, regs)) {
goto ss_probe; goto ss_probe;
} }
...@@ -214,16 +218,15 @@ static inline int kprobe_handler(struct pt_regs *regs) ...@@ -214,16 +218,15 @@ static inline int kprobe_handler(struct pt_regs *regs)
* in post_kprobe_handler(). * in post_kprobe_handler().
*/ */
preempt_disable(); preempt_disable();
kprobe_status = KPROBE_HIT_ACTIVE; kcb->kprobe_status = KPROBE_HIT_ACTIVE;
current_kprobe = p; set_current_kprobe(p, regs, kcb);
kprobe_saved_msr = regs->msr;
if (p->pre_handler && p->pre_handler(p, regs)) if (p->pre_handler && p->pre_handler(p, regs))
/* handler has already set things up, so skip ss setup */ /* handler has already set things up, so skip ss setup */
return 1; return 1;
ss_probe: ss_probe:
prepare_singlestep(p, regs); prepare_singlestep(p, regs);
kprobe_status = KPROBE_HIT_SS; kcb->kprobe_status = KPROBE_HIT_SS;
return 1; return 1;
no_kprobe: no_kprobe:
...@@ -292,6 +295,7 @@ int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs) ...@@ -292,6 +295,7 @@ int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address)); BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
regs->nip = orig_ret_address; regs->nip = orig_ret_address;
reset_current_kprobe();
unlock_kprobes(); unlock_kprobes();
preempt_enable_no_resched(); preempt_enable_no_resched();
...@@ -324,22 +328,26 @@ static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs) ...@@ -324,22 +328,26 @@ static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
static inline int post_kprobe_handler(struct pt_regs *regs) static inline int post_kprobe_handler(struct pt_regs *regs)
{ {
if (!kprobe_running()) struct kprobe *cur = kprobe_running();
struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
if (!cur)
return 0; return 0;
if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) { if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
kprobe_status = KPROBE_HIT_SSDONE; kcb->kprobe_status = KPROBE_HIT_SSDONE;
current_kprobe->post_handler(current_kprobe, regs, 0); cur->post_handler(cur, regs, 0);
} }
resume_execution(current_kprobe, regs); resume_execution(cur, regs);
regs->msr |= kprobe_saved_msr; regs->msr |= kcb->kprobe_saved_msr;
/*Restore back the original saved kprobes variables and continue. */ /*Restore back the original saved kprobes variables and continue. */
if (kprobe_status == KPROBE_REENTER) { if (kcb->kprobe_status == KPROBE_REENTER) {
restore_previous_kprobe(); restore_previous_kprobe(kcb);
goto out; goto out;
} }
reset_current_kprobe();
unlock_kprobes(); unlock_kprobes();
out: out:
preempt_enable_no_resched(); preempt_enable_no_resched();
...@@ -358,15 +366,18 @@ out: ...@@ -358,15 +366,18 @@ out:
/* Interrupts disabled, kprobe_lock held. */ /* Interrupts disabled, kprobe_lock held. */
static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr) static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
{ {
if (current_kprobe->fault_handler struct kprobe *cur = kprobe_running();
&& current_kprobe->fault_handler(current_kprobe, regs, trapnr)) struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
return 1; return 1;
if (kprobe_status & KPROBE_HIT_SS) { if (kcb->kprobe_status & KPROBE_HIT_SS) {
resume_execution(current_kprobe, regs); resume_execution(cur, regs);
regs->msr &= ~MSR_SE; regs->msr &= ~MSR_SE;
regs->msr |= kprobe_saved_msr; regs->msr |= kcb->kprobe_saved_msr;
reset_current_kprobe();
unlock_kprobes(); unlock_kprobes();
preempt_enable_no_resched(); preempt_enable_no_resched();
} }
...@@ -411,8 +422,9 @@ int __kprobes kprobe_exceptions_notify(struct notifier_block *self, ...@@ -411,8 +422,9 @@ int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
{ {
struct jprobe *jp = container_of(p, struct jprobe, kp); struct jprobe *jp = container_of(p, struct jprobe, kp);
struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
memcpy(&jprobe_saved_regs, regs, sizeof(struct pt_regs)); memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs));
/* setup return addr to the jprobe handler routine */ /* setup return addr to the jprobe handler routine */
regs->nip = (unsigned long)(((func_descr_t *)jp->entry)->entry); regs->nip = (unsigned long)(((func_descr_t *)jp->entry)->entry);
...@@ -432,12 +444,14 @@ void __kprobes jprobe_return_end(void) ...@@ -432,12 +444,14 @@ void __kprobes jprobe_return_end(void)
int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
{ {
struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
/* /*
* FIXME - we should ideally be validating that we got here 'cos * FIXME - we should ideally be validating that we got here 'cos
* of the "trap" in jprobe_return() above, before restoring the * of the "trap" in jprobe_return() above, before restoring the
* saved regs... * saved regs...
*/ */
memcpy(regs, &jprobe_saved_regs, sizeof(struct pt_regs)); memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs));
return 1; return 1;
} }
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
*/ */
#include <linux/types.h> #include <linux/types.h>
#include <linux/ptrace.h> #include <linux/ptrace.h>
#include <linux/percpu.h>
struct pt_regs; struct pt_regs;
...@@ -53,6 +54,20 @@ struct arch_specific_insn { ...@@ -53,6 +54,20 @@ struct arch_specific_insn {
kprobe_opcode_t *insn; kprobe_opcode_t *insn;
}; };
struct prev_kprobe {
struct kprobe *kp;
unsigned long status;
unsigned long saved_msr;
};
/* per-cpu kprobe control block */
struct kprobe_ctlblk {
unsigned long kprobe_status;
unsigned long kprobe_saved_msr;
struct pt_regs jprobe_saved_regs;
struct prev_kprobe prev_kprobe;
};
#ifdef CONFIG_KPROBES #ifdef CONFIG_KPROBES
extern int kprobe_exceptions_notify(struct notifier_block *self, extern int kprobe_exceptions_notify(struct notifier_block *self,
unsigned long val, void *data); unsigned long val, void *data);
......
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