[PATCH] kprobes: arch_remove_kprobe
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / powerpc / kernel / kprobes.c
CommitLineData
1da177e4
LT
1/*
2 * Kernel Probes (KProbes)
3 * arch/ppc64/kernel/kprobes.c
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 * Copyright (C) IBM Corporation, 2002, 2004
20 *
21 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
22 * Probes initial implementation ( includes contributions from
23 * Rusty Russell).
24 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
25 * interface to access function arguments.
26 * 2004-Nov Ananth N Mavinakayanahalli <ananth@in.ibm.com> kprobes port
27 * for PPC64
28 */
29
30#include <linux/config.h>
31#include <linux/kprobes.h>
32#include <linux/ptrace.h>
1da177e4 33#include <linux/preempt.h>
7e1048b1 34#include <asm/cacheflush.h>
1da177e4
LT
35#include <asm/kdebug.h>
36#include <asm/sstep.h>
37
0dc036c9
AM
38DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
39DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
1da177e4 40
bb144a85 41int __kprobes arch_prepare_kprobe(struct kprobe *p)
1da177e4 42{
63224d1e 43 int ret = 0;
1da177e4
LT
44 kprobe_opcode_t insn = *p->addr;
45
63224d1e
AM
46 if ((unsigned long)p->addr & 0x03) {
47 printk("Attempt to register kprobe at an unaligned address\n");
48 ret = -EINVAL;
49 } else if (IS_MTMSRD(insn) || IS_RFID(insn)) {
50 printk("Cannot register a kprobe on rfid or mtmsrd\n");
51 ret = -EINVAL;
52 }
9ec4b1f3
AM
53
54 /* insn must be on a special executable page on ppc64 */
55 if (!ret) {
2d8ab6ad 56 p->ainsn.insn = get_insn_slot();
9ec4b1f3
AM
57 if (!p->ainsn.insn)
58 ret = -ENOMEM;
59 }
1da177e4 60
49a2a1b8
AK
61 if (!ret) {
62 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
63 p->opcode = *p->addr;
64 }
65
66 return ret;
1da177e4
LT
67}
68
bb144a85 69void __kprobes arch_arm_kprobe(struct kprobe *p)
1da177e4 70{
7e1048b1
RL
71 *p->addr = BREAKPOINT_INSTRUCTION;
72 flush_icache_range((unsigned long) p->addr,
73 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
1da177e4
LT
74}
75
bb144a85 76void __kprobes arch_disarm_kprobe(struct kprobe *p)
1da177e4
LT
77{
78 *p->addr = p->opcode;
7e1048b1
RL
79 flush_icache_range((unsigned long) p->addr,
80 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
81}
82
e597c298 83void __kprobes arch_remove_kprobe(struct kprobe *p, struct semaphore *s)
7e1048b1 84{
e597c298 85 down(s);
2d8ab6ad 86 free_insn_slot(p->ainsn.insn);
e597c298 87 up(s);
1da177e4
LT
88}
89
90static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
91{
9ec4b1f3
AM
92 kprobe_opcode_t insn = *p->ainsn.insn;
93
1da177e4 94 regs->msr |= MSR_SE;
9ec4b1f3
AM
95
96 /* single step inline if it is a trap variant */
deac66ae 97 if (is_trap(insn))
1da177e4
LT
98 regs->nip = (unsigned long)p->addr;
99 else
9ec4b1f3 100 regs->nip = (unsigned long)p->ainsn.insn;
1da177e4
LT
101}
102
0dc036c9
AM
103static inline void save_previous_kprobe(struct kprobe_ctlblk *kcb)
104{
105 kcb->prev_kprobe.kp = kprobe_running();
106 kcb->prev_kprobe.status = kcb->kprobe_status;
107 kcb->prev_kprobe.saved_msr = kcb->kprobe_saved_msr;
108}
109
110static inline void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
42cc2060 111{
0dc036c9
AM
112 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
113 kcb->kprobe_status = kcb->prev_kprobe.status;
114 kcb->kprobe_saved_msr = kcb->prev_kprobe.saved_msr;
42cc2060
PP
115}
116
0dc036c9
AM
117static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
118 struct kprobe_ctlblk *kcb)
42cc2060 119{
0dc036c9
AM
120 __get_cpu_var(current_kprobe) = p;
121 kcb->kprobe_saved_msr = regs->msr;
42cc2060
PP
122}
123
991a51d8 124/* Called with kretprobe_lock held */
bb144a85
PP
125void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
126 struct pt_regs *regs)
97f7943d
RL
127{
128 struct kretprobe_instance *ri;
129
130 if ((ri = get_free_rp_inst(rp)) != NULL) {
131 ri->rp = rp;
132 ri->task = current;
133 ri->ret_addr = (kprobe_opcode_t *)regs->link;
134
135 /* Replace the return addr with trampoline addr */
136 regs->link = (unsigned long)kretprobe_trampoline;
137 add_rp_inst(ri);
138 } else {
139 rp->nmissed++;
140 }
141}
142
1da177e4
LT
143static inline int kprobe_handler(struct pt_regs *regs)
144{
145 struct kprobe *p;
146 int ret = 0;
147 unsigned int *addr = (unsigned int *)regs->nip;
d217d545
AM
148 struct kprobe_ctlblk *kcb;
149
150 /*
151 * We don't want to be preempted for the entire
152 * duration of kprobe processing
153 */
154 preempt_disable();
155 kcb = get_kprobe_ctlblk();
1da177e4
LT
156
157 /* Check we're not actually recursing */
158 if (kprobe_running()) {
1da177e4
LT
159 p = get_kprobe(addr);
160 if (p) {
deac66ae 161 kprobe_opcode_t insn = *p->ainsn.insn;
0dc036c9 162 if (kcb->kprobe_status == KPROBE_HIT_SS &&
deac66ae 163 is_trap(insn)) {
1da177e4 164 regs->msr &= ~MSR_SE;
0dc036c9 165 regs->msr |= kcb->kprobe_saved_msr;
1da177e4
LT
166 goto no_kprobe;
167 }
42cc2060
PP
168 /* We have reentered the kprobe_handler(), since
169 * another probe was hit while within the handler.
170 * We here save the original kprobes variables and
171 * just single step on the instruction of the new probe
172 * without calling any user handlers.
173 */
0dc036c9
AM
174 save_previous_kprobe(kcb);
175 set_current_kprobe(p, regs, kcb);
176 kcb->kprobe_saved_msr = regs->msr;
bf8d5c52 177 kprobes_inc_nmissed_count(p);
42cc2060 178 prepare_singlestep(p, regs);
0dc036c9 179 kcb->kprobe_status = KPROBE_REENTER;
42cc2060 180 return 1;
1da177e4 181 } else {
0dc036c9 182 p = __get_cpu_var(current_kprobe);
1da177e4
LT
183 if (p->break_handler && p->break_handler(p, regs)) {
184 goto ss_probe;
185 }
186 }
1da177e4
LT
187 goto no_kprobe;
188 }
189
1da177e4
LT
190 p = get_kprobe(addr);
191 if (!p) {
1da177e4
LT
192 if (*addr != BREAKPOINT_INSTRUCTION) {
193 /*
194 * PowerPC has multiple variants of the "trap"
195 * instruction. If the current instruction is a
196 * trap variant, it could belong to someone else
197 */
198 kprobe_opcode_t cur_insn = *addr;
deac66ae 199 if (is_trap(cur_insn))
1da177e4
LT
200 goto no_kprobe;
201 /*
202 * The breakpoint instruction was removed right
203 * after we hit it. Another cpu has removed
204 * either a probepoint or a debugger breakpoint
205 * at this address. In either case, no further
206 * handling of this interrupt is appropriate.
207 */
208 ret = 1;
209 }
210 /* Not one of ours: let kernel handle it */
211 goto no_kprobe;
212 }
213
0dc036c9
AM
214 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
215 set_current_kprobe(p, regs, kcb);
1da177e4
LT
216 if (p->pre_handler && p->pre_handler(p, regs))
217 /* handler has already set things up, so skip ss setup */
218 return 1;
219
220ss_probe:
221 prepare_singlestep(p, regs);
0dc036c9 222 kcb->kprobe_status = KPROBE_HIT_SS;
1da177e4
LT
223 return 1;
224
225no_kprobe:
d217d545 226 preempt_enable_no_resched();
1da177e4
LT
227 return ret;
228}
229
97f7943d
RL
230/*
231 * Function return probe trampoline:
232 * - init_kprobes() establishes a probepoint here
233 * - When the probed function returns, this probe
234 * causes the handlers to fire
235 */
236void kretprobe_trampoline_holder(void)
237{
238 asm volatile(".global kretprobe_trampoline\n"
239 "kretprobe_trampoline:\n"
240 "nop\n");
241}
242
243/*
244 * Called when the probe at kretprobe trampoline is hit
245 */
bb144a85 246int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
97f7943d
RL
247{
248 struct kretprobe_instance *ri = NULL;
249 struct hlist_head *head;
250 struct hlist_node *node, *tmp;
991a51d8 251 unsigned long flags, orig_ret_address = 0;
97f7943d
RL
252 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
253
991a51d8 254 spin_lock_irqsave(&kretprobe_lock, flags);
97f7943d
RL
255 head = kretprobe_inst_table_head(current);
256
257 /*
258 * It is possible to have multiple instances associated with a given
259 * task either because an multiple functions in the call path
260 * have a return probe installed on them, and/or more then one return
261 * return probe was registered for a target function.
262 *
263 * We can handle this because:
264 * - instances are always inserted at the head of the list
265 * - when multiple return probes are registered for the same
266 * function, the first instance's ret_addr will point to the
267 * real return address, and all the rest will point to
268 * kretprobe_trampoline
269 */
270 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
271 if (ri->task != current)
272 /* another task is sharing our hash bucket */
273 continue;
274
275 if (ri->rp && ri->rp->handler)
276 ri->rp->handler(ri, regs);
277
278 orig_ret_address = (unsigned long)ri->ret_addr;
279 recycle_rp_inst(ri);
280
281 if (orig_ret_address != trampoline_address)
282 /*
283 * This is the real return address. Any other
284 * instances associated with this task are for
285 * other calls deeper on the call stack
286 */
287 break;
288 }
289
290 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
291 regs->nip = orig_ret_address;
292
0dc036c9 293 reset_current_kprobe();
991a51d8 294 spin_unlock_irqrestore(&kretprobe_lock, flags);
66ff2d06 295 preempt_enable_no_resched();
97f7943d
RL
296
297 /*
298 * By returning a non-zero value, we are telling
d217d545
AM
299 * kprobe_handler() that we don't want the post_handler
300 * to run (and have re-enabled preemption)
97f7943d
RL
301 */
302 return 1;
303}
304
1da177e4
LT
305/*
306 * Called after single-stepping. p->addr is the address of the
307 * instruction whose first byte has been replaced by the "breakpoint"
308 * instruction. To avoid the SMP problems that can occur when we
309 * temporarily put back the original opcode to single-step, we
310 * single-stepped a copy of the instruction. The address of this
311 * copy is p->ainsn.insn.
312 */
bb144a85 313static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
1da177e4
LT
314{
315 int ret;
9ec4b1f3 316 unsigned int insn = *p->ainsn.insn;
1da177e4
LT
317
318 regs->nip = (unsigned long)p->addr;
9ec4b1f3 319 ret = emulate_step(regs, insn);
1da177e4
LT
320 if (ret == 0)
321 regs->nip = (unsigned long)p->addr + 4;
1da177e4
LT
322}
323
324static inline int post_kprobe_handler(struct pt_regs *regs)
325{
0dc036c9
AM
326 struct kprobe *cur = kprobe_running();
327 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
328
329 if (!cur)
1da177e4
LT
330 return 0;
331
0dc036c9
AM
332 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
333 kcb->kprobe_status = KPROBE_HIT_SSDONE;
334 cur->post_handler(cur, regs, 0);
42cc2060 335 }
1da177e4 336
0dc036c9
AM
337 resume_execution(cur, regs);
338 regs->msr |= kcb->kprobe_saved_msr;
1da177e4 339
42cc2060 340 /*Restore back the original saved kprobes variables and continue. */
0dc036c9
AM
341 if (kcb->kprobe_status == KPROBE_REENTER) {
342 restore_previous_kprobe(kcb);
42cc2060
PP
343 goto out;
344 }
0dc036c9 345 reset_current_kprobe();
42cc2060 346out:
1da177e4
LT
347 preempt_enable_no_resched();
348
349 /*
350 * if somebody else is singlestepping across a probe point, msr
351 * will have SE set, in which case, continue the remaining processing
352 * of do_debug, as if this is not a probe hit.
353 */
354 if (regs->msr & MSR_SE)
355 return 0;
356
357 return 1;
358}
359
1da177e4
LT
360static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
361{
0dc036c9
AM
362 struct kprobe *cur = kprobe_running();
363 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
364
365 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
1da177e4
LT
366 return 1;
367
0dc036c9
AM
368 if (kcb->kprobe_status & KPROBE_HIT_SS) {
369 resume_execution(cur, regs);
f829fd23 370 regs->msr &= ~MSR_SE;
0dc036c9 371 regs->msr |= kcb->kprobe_saved_msr;
1da177e4 372
0dc036c9 373 reset_current_kprobe();
1da177e4
LT
374 preempt_enable_no_resched();
375 }
376 return 0;
377}
378
379/*
380 * Wrapper routine to for handling exceptions.
381 */
bb144a85
PP
382int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
383 unsigned long val, void *data)
1da177e4
LT
384{
385 struct die_args *args = (struct die_args *)data;
386 int ret = NOTIFY_DONE;
387
1da177e4 388 switch (val) {
1da177e4
LT
389 case DIE_BPT:
390 if (kprobe_handler(args->regs))
391 ret = NOTIFY_STOP;
392 break;
393 case DIE_SSTEP:
394 if (post_kprobe_handler(args->regs))
395 ret = NOTIFY_STOP;
396 break;
1da177e4 397 case DIE_PAGE_FAULT:
d217d545
AM
398 /* kprobe_running() needs smp_processor_id() */
399 preempt_disable();
1da177e4
LT
400 if (kprobe_running() &&
401 kprobe_fault_handler(args->regs, args->trapnr))
402 ret = NOTIFY_STOP;
d217d545 403 preempt_enable();
1da177e4
LT
404 break;
405 default:
406 break;
407 }
1da177e4
LT
408 return ret;
409}
410
bb144a85 411int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
1da177e4
LT
412{
413 struct jprobe *jp = container_of(p, struct jprobe, kp);
0dc036c9 414 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1da177e4 415
0dc036c9 416 memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs));
1da177e4
LT
417
418 /* setup return addr to the jprobe handler routine */
419 regs->nip = (unsigned long)(((func_descr_t *)jp->entry)->entry);
420 regs->gpr[2] = (unsigned long)(((func_descr_t *)jp->entry)->toc);
421
422 return 1;
423}
424
bb144a85 425void __kprobes jprobe_return(void)
1da177e4
LT
426{
427 asm volatile("trap" ::: "memory");
428}
429
bb144a85 430void __kprobes jprobe_return_end(void)
1da177e4
LT
431{
432};
433
bb144a85 434int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
1da177e4 435{
0dc036c9
AM
436 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
437
1da177e4
LT
438 /*
439 * FIXME - we should ideally be validating that we got here 'cos
440 * of the "trap" in jprobe_return() above, before restoring the
441 * saved regs...
442 */
0dc036c9 443 memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs));
d217d545 444 preempt_enable_no_resched();
1da177e4
LT
445 return 1;
446}
97f7943d
RL
447
448static struct kprobe trampoline_p = {
449 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
450 .pre_handler = trampoline_probe_handler
451};
452
6772926b 453int __init arch_init_kprobes(void)
97f7943d
RL
454{
455 return register_kprobe(&trampoline_p);
456}