blob: 0df2d6d57c04036b753df962e5e2787ded45425f [file] [log] [blame]
Abhishek Sagar24ba6132007-06-11 22:20:10 +00001/*
2 * arch/arm/kernel/kprobes.c
3 *
4 * Kprobes on ARM
5 *
6 * Abhishek Sagar <sagar.abhishek@gmail.com>
7 * Copyright (C) 2006, 2007 Motorola Inc.
8 *
9 * Nicolas Pitre <nico@marvell.com>
10 * Copyright (C) 2007 Marvell Ltd.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 */
21
22#include <linux/kernel.h>
23#include <linux/kprobes.h>
24#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Frederic Riss2003b7a2009-09-21 08:43:30 +010026#include <linux/stop_machine.h>
Abhishek Sagar24ba6132007-06-11 22:20:10 +000027#include <linux/stringify.h>
28#include <asm/traps.h>
29#include <asm/cacheflush.h>
30
Jon Medhurst221bf152011-04-20 10:52:38 +010031#include "kprobes.h"
32
Abhishek Sagar24ba6132007-06-11 22:20:10 +000033#define MIN_STACK_SIZE(addr) \
34 min((unsigned long)MAX_STACK_SIZE, \
35 (unsigned long)current_thread_info() + THREAD_START_SP - (addr))
36
37#define flush_insns(addr, cnt) \
38 flush_icache_range((unsigned long)(addr), \
39 (unsigned long)(addr) + \
40 sizeof(kprobe_opcode_t) * (cnt))
41
42/* Used as a marker in ARM_pc to note when we're in a jprobe. */
43#define JPROBE_MAGIC_ADDR 0xffffffff
44
45DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
46DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
47
48
49int __kprobes arch_prepare_kprobe(struct kprobe *p)
50{
51 kprobe_opcode_t insn;
52 kprobe_opcode_t tmp_insn[MAX_INSN_SIZE];
53 unsigned long addr = (unsigned long)p->addr;
Jon Medhurst24371702011-04-19 17:56:58 +010054 kprobe_decode_insn_t *decode_insn;
Abhishek Sagar24ba6132007-06-11 22:20:10 +000055 int is;
56
Jon Medhurst24371702011-04-19 17:56:58 +010057 if (in_exception_text(addr))
Abhishek Sagar24ba6132007-06-11 22:20:10 +000058 return -EINVAL;
59
Jon Medhurst24371702011-04-19 17:56:58 +010060#ifdef CONFIG_THUMB2_KERNEL
61 addr &= ~1; /* Bit 0 would normally be set to indicate Thumb code */
62 insn = ((u16 *)addr)[0];
63 if (is_wide_instruction(insn)) {
64 insn <<= 16;
65 insn |= ((u16 *)addr)[1];
66 decode_insn = thumb32_kprobe_decode_insn;
67 } else
68 decode_insn = thumb16_kprobe_decode_insn;
69#else /* !CONFIG_THUMB2_KERNEL */
70 if (addr & 0x3)
71 return -EINVAL;
Abhishek Sagar24ba6132007-06-11 22:20:10 +000072 insn = *p->addr;
Jon Medhurst24371702011-04-19 17:56:58 +010073 decode_insn = arm_kprobe_decode_insn;
74#endif
75
Abhishek Sagar24ba6132007-06-11 22:20:10 +000076 p->opcode = insn;
77 p->ainsn.insn = tmp_insn;
78
Jon Medhurst24371702011-04-19 17:56:58 +010079 switch ((*decode_insn)(insn, &p->ainsn)) {
Abhishek Sagar24ba6132007-06-11 22:20:10 +000080 case INSN_REJECTED: /* not supported */
81 return -EINVAL;
82
83 case INSN_GOOD: /* instruction uses slot */
84 p->ainsn.insn = get_insn_slot();
85 if (!p->ainsn.insn)
86 return -ENOMEM;
87 for (is = 0; is < MAX_INSN_SIZE; ++is)
88 p->ainsn.insn[is] = tmp_insn[is];
Nicolas Pitre8f79ff02008-04-23 18:44:15 -040089 flush_insns(p->ainsn.insn, MAX_INSN_SIZE);
Abhishek Sagar24ba6132007-06-11 22:20:10 +000090 break;
91
92 case INSN_GOOD_NO_SLOT: /* instruction doesn't need insn slot */
93 p->ainsn.insn = NULL;
94 break;
95 }
96
97 return 0;
98}
99
100void __kprobes arch_arm_kprobe(struct kprobe *p)
101{
102 *p->addr = KPROBE_BREAKPOINT_INSTRUCTION;
103 flush_insns(p->addr, 1);
104}
105
Frederic Riss2003b7a2009-09-21 08:43:30 +0100106/*
107 * The actual disarming is done here on each CPU and synchronized using
108 * stop_machine. This synchronization is necessary on SMP to avoid removing
109 * a probe between the moment the 'Undefined Instruction' exception is raised
110 * and the moment the exception handler reads the faulting instruction from
111 * memory.
112 */
113int __kprobes __arch_disarm_kprobe(void *p)
114{
115 struct kprobe *kp = p;
116 *kp->addr = kp->opcode;
117 flush_insns(kp->addr, 1);
118 return 0;
119}
120
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000121void __kprobes arch_disarm_kprobe(struct kprobe *p)
122{
Frederic Riss2003b7a2009-09-21 08:43:30 +0100123 stop_machine(__arch_disarm_kprobe, p, &cpu_online_map);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000124}
125
126void __kprobes arch_remove_kprobe(struct kprobe *p)
127{
128 if (p->ainsn.insn) {
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000129 free_insn_slot(p->ainsn.insn, 0);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000130 p->ainsn.insn = NULL;
131 }
132}
133
134static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
135{
136 kcb->prev_kprobe.kp = kprobe_running();
137 kcb->prev_kprobe.status = kcb->kprobe_status;
138}
139
140static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
141{
142 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
143 kcb->kprobe_status = kcb->prev_kprobe.status;
144}
145
146static void __kprobes set_current_kprobe(struct kprobe *p)
147{
148 __get_cpu_var(current_kprobe) = p;
149}
150
151static void __kprobes singlestep(struct kprobe *p, struct pt_regs *regs,
152 struct kprobe_ctlblk *kcb)
153{
154 regs->ARM_pc += 4;
Jon Medhurst073090c2011-04-06 11:17:09 +0100155 if (p->ainsn.insn_check_cc(regs->ARM_cpsr))
156 p->ainsn.insn_handler(p, regs);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000157}
158
159/*
160 * Called with IRQs disabled. IRQs must remain disabled from that point
161 * all the way until processing this kprobe is complete. The current
162 * kprobes implementation cannot process more than one nested level of
163 * kprobe, and that level is reserved for user kprobe handlers, so we can't
164 * risk encountering a new kprobe in an interrupt handler.
165 */
166void __kprobes kprobe_handler(struct pt_regs *regs)
167{
168 struct kprobe *p, *cur;
169 struct kprobe_ctlblk *kcb;
170 kprobe_opcode_t *addr = (kprobe_opcode_t *)regs->ARM_pc;
171
172 kcb = get_kprobe_ctlblk();
173 cur = kprobe_running();
174 p = get_kprobe(addr);
175
176 if (p) {
177 if (cur) {
178 /* Kprobe is pending, so we're recursing. */
179 switch (kcb->kprobe_status) {
180 case KPROBE_HIT_ACTIVE:
181 case KPROBE_HIT_SSDONE:
182 /* A pre- or post-handler probe got us here. */
183 kprobes_inc_nmissed_count(p);
184 save_previous_kprobe(kcb);
185 set_current_kprobe(p);
186 kcb->kprobe_status = KPROBE_REENTER;
187 singlestep(p, regs, kcb);
188 restore_previous_kprobe(kcb);
189 break;
190 default:
191 /* impossible cases */
192 BUG();
193 }
194 } else {
195 set_current_kprobe(p);
196 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
197
198 /*
199 * If we have no pre-handler or it returned 0, we
200 * continue with normal processing. If we have a
201 * pre-handler and it returned non-zero, it prepped
202 * for calling the break_handler below on re-entry,
203 * so get out doing nothing more here.
204 */
205 if (!p->pre_handler || !p->pre_handler(p, regs)) {
206 kcb->kprobe_status = KPROBE_HIT_SS;
207 singlestep(p, regs, kcb);
208 if (p->post_handler) {
209 kcb->kprobe_status = KPROBE_HIT_SSDONE;
210 p->post_handler(p, regs, 0);
211 }
212 reset_current_kprobe();
213 }
214 }
215 } else if (cur) {
216 /* We probably hit a jprobe. Call its break handler. */
217 if (cur->break_handler && cur->break_handler(cur, regs)) {
218 kcb->kprobe_status = KPROBE_HIT_SS;
219 singlestep(cur, regs, kcb);
220 if (cur->post_handler) {
221 kcb->kprobe_status = KPROBE_HIT_SSDONE;
222 cur->post_handler(cur, regs, 0);
223 }
224 }
225 reset_current_kprobe();
226 } else {
227 /*
228 * The probe was removed and a race is in progress.
229 * There is nothing we can do about it. Let's restart
230 * the instruction. By the time we can restart, the
231 * real instruction will be there.
232 */
233 }
234}
235
Nicolas Pitre3305a602008-08-19 04:15:23 +0100236static int __kprobes kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000237{
Nicolas Pitre3305a602008-08-19 04:15:23 +0100238 unsigned long flags;
239 local_irq_save(flags);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000240 kprobe_handler(regs);
Nicolas Pitre3305a602008-08-19 04:15:23 +0100241 local_irq_restore(flags);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000242 return 0;
243}
244
245int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
246{
247 struct kprobe *cur = kprobe_running();
248 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
249
250 switch (kcb->kprobe_status) {
251 case KPROBE_HIT_SS:
252 case KPROBE_REENTER:
253 /*
254 * We are here because the instruction being single
255 * stepped caused a page fault. We reset the current
256 * kprobe and the PC to point back to the probe address
257 * and allow the page fault handler to continue as a
258 * normal page fault.
259 */
260 regs->ARM_pc = (long)cur->addr;
261 if (kcb->kprobe_status == KPROBE_REENTER) {
262 restore_previous_kprobe(kcb);
263 } else {
264 reset_current_kprobe();
265 }
266 break;
267
268 case KPROBE_HIT_ACTIVE:
269 case KPROBE_HIT_SSDONE:
270 /*
271 * We increment the nmissed count for accounting,
272 * we can also use npre/npostfault count for accounting
273 * these specific fault cases.
274 */
275 kprobes_inc_nmissed_count(cur);
276
277 /*
278 * We come here because instructions in the pre/post
279 * handler caused the page_fault, this could happen
280 * if handler tries to access user space by
281 * copy_from_user(), get_user() etc. Let the
282 * user-specified handler try to fix it.
283 */
284 if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
285 return 1;
286 break;
287
288 default:
289 break;
290 }
291
292 return 0;
293}
294
295int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
296 unsigned long val, void *data)
297{
298 /*
299 * notify_die() is currently never called on ARM,
300 * so this callback is currently empty.
301 */
302 return NOTIFY_DONE;
303}
304
305/*
306 * When a retprobed function returns, trampoline_handler() is called,
307 * calling the kretprobe's handler. We construct a struct pt_regs to
308 * give a view of registers r0-r11 to the user return-handler. This is
309 * not a complete pt_regs structure, but that should be plenty sufficient
310 * for kretprobe handlers which should normally be interested in r0 only
311 * anyway.
312 */
Abhishek Sagare0773412008-05-31 14:24:02 +0530313void __naked __kprobes kretprobe_trampoline(void)
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000314{
315 __asm__ __volatile__ (
316 "stmdb sp!, {r0 - r11} \n\t"
317 "mov r0, sp \n\t"
318 "bl trampoline_handler \n\t"
319 "mov lr, r0 \n\t"
320 "ldmia sp!, {r0 - r11} \n\t"
Jon Medhurstde419842011-06-14 13:08:04 +0100321#ifdef CONFIG_THUMB2_KERNEL
322 "bx lr \n\t"
323#else
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000324 "mov pc, lr \n\t"
Jon Medhurstde419842011-06-14 13:08:04 +0100325#endif
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000326 : : : "memory");
327}
328
329/* Called from kretprobe_trampoline */
330static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
331{
332 struct kretprobe_instance *ri = NULL;
333 struct hlist_head *head, empty_rp;
334 struct hlist_node *node, *tmp;
335 unsigned long flags, orig_ret_address = 0;
336 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
337
338 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700339 kretprobe_hash_lock(current, &head, &flags);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000340
341 /*
342 * It is possible to have multiple instances associated with a given
343 * task either because multiple functions in the call path have
344 * a return probe installed on them, and/or more than one return
345 * probe was registered for a target function.
346 *
347 * We can handle this because:
348 * - instances are always inserted at the head of the list
349 * - when multiple return probes are registered for the same
350 * function, the first instance's ret_addr will point to the
351 * real return address, and all the rest will point to
352 * kretprobe_trampoline
353 */
354 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
355 if (ri->task != current)
356 /* another task is sharing our hash bucket */
357 continue;
358
359 if (ri->rp && ri->rp->handler) {
360 __get_cpu_var(current_kprobe) = &ri->rp->kp;
361 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
362 ri->rp->handler(ri, regs);
363 __get_cpu_var(current_kprobe) = NULL;
364 }
365
366 orig_ret_address = (unsigned long)ri->ret_addr;
367 recycle_rp_inst(ri, &empty_rp);
368
369 if (orig_ret_address != trampoline_address)
370 /*
371 * This is the real return address. Any other
372 * instances associated with this task are for
373 * other calls deeper on the call stack
374 */
375 break;
376 }
377
378 kretprobe_assert(ri, orig_ret_address, trampoline_address);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700379 kretprobe_hash_unlock(current, &flags);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000380
381 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
382 hlist_del(&ri->hlist);
383 kfree(ri);
384 }
385
386 return (void *)orig_ret_address;
387}
388
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000389void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
390 struct pt_regs *regs)
391{
392 ri->ret_addr = (kprobe_opcode_t *)regs->ARM_lr;
393
394 /* Replace the return addr with trampoline addr. */
395 regs->ARM_lr = (unsigned long)&kretprobe_trampoline;
396}
397
398int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
399{
400 struct jprobe *jp = container_of(p, struct jprobe, kp);
401 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
402 long sp_addr = regs->ARM_sp;
Jon Medhurstde419842011-06-14 13:08:04 +0100403 long cpsr;
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000404
405 kcb->jprobe_saved_regs = *regs;
406 memcpy(kcb->jprobes_stack, (void *)sp_addr, MIN_STACK_SIZE(sp_addr));
407 regs->ARM_pc = (long)jp->entry;
Jon Medhurstde419842011-06-14 13:08:04 +0100408
409 cpsr = regs->ARM_cpsr | PSR_I_BIT;
410#ifdef CONFIG_THUMB2_KERNEL
411 /* Set correct Thumb state in cpsr */
412 if (regs->ARM_pc & 1)
413 cpsr |= PSR_T_BIT;
414 else
415 cpsr &= ~PSR_T_BIT;
416#endif
417 regs->ARM_cpsr = cpsr;
418
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000419 preempt_disable();
420 return 1;
421}
422
423void __kprobes jprobe_return(void)
424{
425 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
426
427 __asm__ __volatile__ (
428 /*
429 * Setup an empty pt_regs. Fill SP and PC fields as
430 * they're needed by longjmp_break_handler.
Mika Westerberg782a0fd2010-03-29 06:59:16 +0100431 *
432 * We allocate some slack between the original SP and start of
433 * our fabricated regs. To be precise we want to have worst case
434 * covered which is STMFD with all 16 regs so we allocate 2 *
435 * sizeof(struct_pt_regs)).
436 *
437 * This is to prevent any simulated instruction from writing
438 * over the regs when they are accessing the stack.
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000439 */
Jon Medhurstde419842011-06-14 13:08:04 +0100440#ifdef CONFIG_THUMB2_KERNEL
441 "sub r0, %0, %1 \n\t"
442 "mov sp, r0 \n\t"
443#else
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000444 "sub sp, %0, %1 \n\t"
Jon Medhurstde419842011-06-14 13:08:04 +0100445#endif
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000446 "ldr r0, ="__stringify(JPROBE_MAGIC_ADDR)"\n\t"
447 "str %0, [sp, %2] \n\t"
448 "str r0, [sp, %3] \n\t"
449 "mov r0, sp \n\t"
450 "bl kprobe_handler \n\t"
451
452 /*
453 * Return to the context saved by setjmp_pre_handler
454 * and restored by longjmp_break_handler.
455 */
Jon Medhurstde419842011-06-14 13:08:04 +0100456#ifdef CONFIG_THUMB2_KERNEL
457 "ldr lr, [sp, %2] \n\t" /* lr = saved sp */
458 "ldrd r0, r1, [sp, %5] \n\t" /* r0,r1 = saved lr,pc */
459 "ldr r2, [sp, %4] \n\t" /* r2 = saved psr */
460 "stmdb lr!, {r0, r1, r2} \n\t" /* push saved lr and */
461 /* rfe context */
462 "ldmia sp, {r0 - r12} \n\t"
463 "mov sp, lr \n\t"
464 "ldr lr, [sp], #4 \n\t"
465 "rfeia sp! \n\t"
466#else
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000467 "ldr r0, [sp, %4] \n\t"
468 "msr cpsr_cxsf, r0 \n\t"
469 "ldmia sp, {r0 - pc} \n\t"
Jon Medhurstde419842011-06-14 13:08:04 +0100470#endif
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000471 :
472 : "r" (kcb->jprobe_saved_regs.ARM_sp),
Mika Westerberg782a0fd2010-03-29 06:59:16 +0100473 "I" (sizeof(struct pt_regs) * 2),
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000474 "J" (offsetof(struct pt_regs, ARM_sp)),
475 "J" (offsetof(struct pt_regs, ARM_pc)),
Jon Medhurstde419842011-06-14 13:08:04 +0100476 "J" (offsetof(struct pt_regs, ARM_cpsr)),
477 "J" (offsetof(struct pt_regs, ARM_lr))
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000478 : "memory", "cc");
479}
480
481int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
482{
483 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
484 long stack_addr = kcb->jprobe_saved_regs.ARM_sp;
485 long orig_sp = regs->ARM_sp;
486 struct jprobe *jp = container_of(p, struct jprobe, kp);
487
488 if (regs->ARM_pc == JPROBE_MAGIC_ADDR) {
489 if (orig_sp != stack_addr) {
490 struct pt_regs *saved_regs =
491 (struct pt_regs *)kcb->jprobe_saved_regs.ARM_sp;
492 printk("current sp %lx does not match saved sp %lx\n",
493 orig_sp, stack_addr);
494 printk("Saved registers for jprobe %p\n", jp);
495 show_regs(saved_regs);
496 printk("Current registers\n");
497 show_regs(regs);
498 BUG();
499 }
500 *regs = kcb->jprobe_saved_regs;
501 memcpy((void *)stack_addr, kcb->jprobes_stack,
502 MIN_STACK_SIZE(stack_addr));
503 preempt_enable_no_resched();
504 return 1;
505 }
506 return 0;
507}
508
Nicolas Pitreb24061f2008-03-04 21:56:21 +0100509int __kprobes arch_trampoline_kprobe(struct kprobe *p)
510{
511 return 0;
512}
513
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000514static struct undef_hook kprobes_break_hook = {
515 .instr_mask = 0xffffffff,
516 .instr_val = KPROBE_BREAKPOINT_INSTRUCTION,
517 .cpsr_mask = MODE_MASK,
518 .cpsr_val = SVC_MODE,
519 .fn = kprobe_trap_handler,
520};
521
522int __init arch_init_kprobes()
523{
524 arm_kprobe_decode_init();
525 register_undef_hook(&kprobes_break_hook);
526 return 0;
527}