blob: cd5d2cc5d1579c09b920810060da269084ccb7a1 [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>
Neil Leeder1baed2f2011-10-25 17:58:14 -040030#include <asm/mmu_writeable.h>
Abhishek Sagar24ba6132007-06-11 22:20:10 +000031
Abhishek Sagar24ba6132007-06-11 22:20:10 +000032#define MIN_STACK_SIZE(addr) \
33 min((unsigned long)MAX_STACK_SIZE, \
34 (unsigned long)current_thread_info() + THREAD_START_SP - (addr))
35
36#define flush_insns(addr, cnt) \
37 flush_icache_range((unsigned long)(addr), \
38 (unsigned long)(addr) + \
39 sizeof(kprobe_opcode_t) * (cnt))
40
41/* Used as a marker in ARM_pc to note when we're in a jprobe. */
42#define JPROBE_MAGIC_ADDR 0xffffffff
43
44DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
45DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
46
47
48int __kprobes arch_prepare_kprobe(struct kprobe *p)
49{
50 kprobe_opcode_t insn;
51 kprobe_opcode_t tmp_insn[MAX_INSN_SIZE];
52 unsigned long addr = (unsigned long)p->addr;
53 int is;
54
Nicolas Pitre785d3cd2007-12-03 15:27:56 -050055 if (addr & 0x3 || in_exception_text(addr))
Abhishek Sagar24ba6132007-06-11 22:20:10 +000056 return -EINVAL;
57
58 insn = *p->addr;
59 p->opcode = insn;
60 p->ainsn.insn = tmp_insn;
61
62 switch (arm_kprobe_decode_insn(insn, &p->ainsn)) {
63 case INSN_REJECTED: /* not supported */
64 return -EINVAL;
65
66 case INSN_GOOD: /* instruction uses slot */
67 p->ainsn.insn = get_insn_slot();
68 if (!p->ainsn.insn)
69 return -ENOMEM;
70 for (is = 0; is < MAX_INSN_SIZE; ++is)
71 p->ainsn.insn[is] = tmp_insn[is];
Nicolas Pitre8f79ff02008-04-23 18:44:15 -040072 flush_insns(p->ainsn.insn, MAX_INSN_SIZE);
Abhishek Sagar24ba6132007-06-11 22:20:10 +000073 break;
74
75 case INSN_GOOD_NO_SLOT: /* instruction doesn't need insn slot */
76 p->ainsn.insn = NULL;
77 break;
78 }
79
80 return 0;
81}
82
83void __kprobes arch_arm_kprobe(struct kprobe *p)
84{
Neil Leeder1baed2f2011-10-25 17:58:14 -040085 unsigned long flags;
86
87 mem_text_writeable_spinlock(&flags);
88 mem_text_address_writeable((unsigned long)p->addr);
Abhishek Sagar24ba6132007-06-11 22:20:10 +000089 *p->addr = KPROBE_BREAKPOINT_INSTRUCTION;
90 flush_insns(p->addr, 1);
Neil Leeder1baed2f2011-10-25 17:58:14 -040091 mem_text_address_restore();
92 mem_text_writeable_spinunlock(&flags);
Abhishek Sagar24ba6132007-06-11 22:20:10 +000093}
94
Frederic Riss2003b7a2009-09-21 08:43:30 +010095/*
96 * The actual disarming is done here on each CPU and synchronized using
97 * stop_machine. This synchronization is necessary on SMP to avoid removing
98 * a probe between the moment the 'Undefined Instruction' exception is raised
99 * and the moment the exception handler reads the faulting instruction from
100 * memory.
101 */
102int __kprobes __arch_disarm_kprobe(void *p)
103{
Neil Leeder1baed2f2011-10-25 17:58:14 -0400104 unsigned long flags;
Frederic Riss2003b7a2009-09-21 08:43:30 +0100105 struct kprobe *kp = p;
Neil Leeder1baed2f2011-10-25 17:58:14 -0400106 mem_text_writeable_spinlock(&flags);
107 mem_text_address_writeable((unsigned long)kp->addr);
Frederic Riss2003b7a2009-09-21 08:43:30 +0100108 *kp->addr = kp->opcode;
109 flush_insns(kp->addr, 1);
Neil Leeder1baed2f2011-10-25 17:58:14 -0400110 mem_text_address_restore();
111 mem_text_writeable_spinunlock(&flags);
Frederic Riss2003b7a2009-09-21 08:43:30 +0100112 return 0;
113}
114
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000115void __kprobes arch_disarm_kprobe(struct kprobe *p)
116{
Frederic Riss2003b7a2009-09-21 08:43:30 +0100117 stop_machine(__arch_disarm_kprobe, p, &cpu_online_map);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000118}
119
120void __kprobes arch_remove_kprobe(struct kprobe *p)
121{
122 if (p->ainsn.insn) {
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000123 free_insn_slot(p->ainsn.insn, 0);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000124 p->ainsn.insn = NULL;
125 }
126}
127
128static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
129{
130 kcb->prev_kprobe.kp = kprobe_running();
131 kcb->prev_kprobe.status = kcb->kprobe_status;
132}
133
134static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
135{
136 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
137 kcb->kprobe_status = kcb->prev_kprobe.status;
138}
139
140static void __kprobes set_current_kprobe(struct kprobe *p)
141{
142 __get_cpu_var(current_kprobe) = p;
143}
144
145static void __kprobes singlestep(struct kprobe *p, struct pt_regs *regs,
146 struct kprobe_ctlblk *kcb)
147{
148 regs->ARM_pc += 4;
Jon Medhurst073090c2011-04-06 11:17:09 +0100149 if (p->ainsn.insn_check_cc(regs->ARM_cpsr))
150 p->ainsn.insn_handler(p, regs);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000151}
152
153/*
154 * Called with IRQs disabled. IRQs must remain disabled from that point
155 * all the way until processing this kprobe is complete. The current
156 * kprobes implementation cannot process more than one nested level of
157 * kprobe, and that level is reserved for user kprobe handlers, so we can't
158 * risk encountering a new kprobe in an interrupt handler.
159 */
160void __kprobes kprobe_handler(struct pt_regs *regs)
161{
162 struct kprobe *p, *cur;
163 struct kprobe_ctlblk *kcb;
164 kprobe_opcode_t *addr = (kprobe_opcode_t *)regs->ARM_pc;
165
166 kcb = get_kprobe_ctlblk();
167 cur = kprobe_running();
168 p = get_kprobe(addr);
169
170 if (p) {
171 if (cur) {
172 /* Kprobe is pending, so we're recursing. */
173 switch (kcb->kprobe_status) {
174 case KPROBE_HIT_ACTIVE:
175 case KPROBE_HIT_SSDONE:
176 /* A pre- or post-handler probe got us here. */
177 kprobes_inc_nmissed_count(p);
178 save_previous_kprobe(kcb);
179 set_current_kprobe(p);
180 kcb->kprobe_status = KPROBE_REENTER;
181 singlestep(p, regs, kcb);
182 restore_previous_kprobe(kcb);
183 break;
184 default:
185 /* impossible cases */
186 BUG();
187 }
188 } else {
189 set_current_kprobe(p);
190 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
191
192 /*
193 * If we have no pre-handler or it returned 0, we
194 * continue with normal processing. If we have a
195 * pre-handler and it returned non-zero, it prepped
196 * for calling the break_handler below on re-entry,
197 * so get out doing nothing more here.
198 */
199 if (!p->pre_handler || !p->pre_handler(p, regs)) {
200 kcb->kprobe_status = KPROBE_HIT_SS;
201 singlestep(p, regs, kcb);
202 if (p->post_handler) {
203 kcb->kprobe_status = KPROBE_HIT_SSDONE;
204 p->post_handler(p, regs, 0);
205 }
206 reset_current_kprobe();
207 }
208 }
209 } else if (cur) {
210 /* We probably hit a jprobe. Call its break handler. */
211 if (cur->break_handler && cur->break_handler(cur, regs)) {
212 kcb->kprobe_status = KPROBE_HIT_SS;
213 singlestep(cur, regs, kcb);
214 if (cur->post_handler) {
215 kcb->kprobe_status = KPROBE_HIT_SSDONE;
216 cur->post_handler(cur, regs, 0);
217 }
218 }
219 reset_current_kprobe();
220 } else {
221 /*
222 * The probe was removed and a race is in progress.
223 * There is nothing we can do about it. Let's restart
224 * the instruction. By the time we can restart, the
225 * real instruction will be there.
226 */
227 }
228}
229
Nicolas Pitre3305a602008-08-19 04:15:23 +0100230static int __kprobes kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000231{
Nicolas Pitre3305a602008-08-19 04:15:23 +0100232 unsigned long flags;
233 local_irq_save(flags);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000234 kprobe_handler(regs);
Nicolas Pitre3305a602008-08-19 04:15:23 +0100235 local_irq_restore(flags);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000236 return 0;
237}
238
239int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
240{
241 struct kprobe *cur = kprobe_running();
242 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
243
244 switch (kcb->kprobe_status) {
245 case KPROBE_HIT_SS:
246 case KPROBE_REENTER:
247 /*
248 * We are here because the instruction being single
249 * stepped caused a page fault. We reset the current
250 * kprobe and the PC to point back to the probe address
251 * and allow the page fault handler to continue as a
252 * normal page fault.
253 */
254 regs->ARM_pc = (long)cur->addr;
255 if (kcb->kprobe_status == KPROBE_REENTER) {
256 restore_previous_kprobe(kcb);
257 } else {
258 reset_current_kprobe();
259 }
260 break;
261
262 case KPROBE_HIT_ACTIVE:
263 case KPROBE_HIT_SSDONE:
264 /*
265 * We increment the nmissed count for accounting,
266 * we can also use npre/npostfault count for accounting
267 * these specific fault cases.
268 */
269 kprobes_inc_nmissed_count(cur);
270
271 /*
272 * We come here because instructions in the pre/post
273 * handler caused the page_fault, this could happen
274 * if handler tries to access user space by
275 * copy_from_user(), get_user() etc. Let the
276 * user-specified handler try to fix it.
277 */
278 if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
279 return 1;
280 break;
281
282 default:
283 break;
284 }
285
286 return 0;
287}
288
289int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
290 unsigned long val, void *data)
291{
292 /*
293 * notify_die() is currently never called on ARM,
294 * so this callback is currently empty.
295 */
296 return NOTIFY_DONE;
297}
298
299/*
300 * When a retprobed function returns, trampoline_handler() is called,
301 * calling the kretprobe's handler. We construct a struct pt_regs to
302 * give a view of registers r0-r11 to the user return-handler. This is
303 * not a complete pt_regs structure, but that should be plenty sufficient
304 * for kretprobe handlers which should normally be interested in r0 only
305 * anyway.
306 */
Abhishek Sagare0773412008-05-31 14:24:02 +0530307void __naked __kprobes kretprobe_trampoline(void)
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000308{
309 __asm__ __volatile__ (
310 "stmdb sp!, {r0 - r11} \n\t"
311 "mov r0, sp \n\t"
312 "bl trampoline_handler \n\t"
313 "mov lr, r0 \n\t"
314 "ldmia sp!, {r0 - r11} \n\t"
315 "mov pc, lr \n\t"
316 : : : "memory");
317}
318
319/* Called from kretprobe_trampoline */
320static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
321{
322 struct kretprobe_instance *ri = NULL;
323 struct hlist_head *head, empty_rp;
324 struct hlist_node *node, *tmp;
325 unsigned long flags, orig_ret_address = 0;
326 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
327
328 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700329 kretprobe_hash_lock(current, &head, &flags);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000330
331 /*
332 * It is possible to have multiple instances associated with a given
333 * task either because multiple functions in the call path have
334 * a return probe installed on them, and/or more than one return
335 * probe was registered for a target function.
336 *
337 * We can handle this because:
338 * - instances are always inserted at the head of the list
339 * - when multiple return probes are registered for the same
340 * function, the first instance's ret_addr will point to the
341 * real return address, and all the rest will point to
342 * kretprobe_trampoline
343 */
344 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
345 if (ri->task != current)
346 /* another task is sharing our hash bucket */
347 continue;
348
349 if (ri->rp && ri->rp->handler) {
350 __get_cpu_var(current_kprobe) = &ri->rp->kp;
351 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
352 ri->rp->handler(ri, regs);
353 __get_cpu_var(current_kprobe) = NULL;
354 }
355
356 orig_ret_address = (unsigned long)ri->ret_addr;
357 recycle_rp_inst(ri, &empty_rp);
358
359 if (orig_ret_address != trampoline_address)
360 /*
361 * This is the real return address. Any other
362 * instances associated with this task are for
363 * other calls deeper on the call stack
364 */
365 break;
366 }
367
368 kretprobe_assert(ri, orig_ret_address, trampoline_address);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700369 kretprobe_hash_unlock(current, &flags);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000370
371 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
372 hlist_del(&ri->hlist);
373 kfree(ri);
374 }
375
376 return (void *)orig_ret_address;
377}
378
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000379void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
380 struct pt_regs *regs)
381{
382 ri->ret_addr = (kprobe_opcode_t *)regs->ARM_lr;
383
384 /* Replace the return addr with trampoline addr. */
385 regs->ARM_lr = (unsigned long)&kretprobe_trampoline;
386}
387
388int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
389{
390 struct jprobe *jp = container_of(p, struct jprobe, kp);
391 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
392 long sp_addr = regs->ARM_sp;
393
394 kcb->jprobe_saved_regs = *regs;
395 memcpy(kcb->jprobes_stack, (void *)sp_addr, MIN_STACK_SIZE(sp_addr));
396 regs->ARM_pc = (long)jp->entry;
397 regs->ARM_cpsr |= PSR_I_BIT;
398 preempt_disable();
399 return 1;
400}
401
402void __kprobes jprobe_return(void)
403{
404 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
405
406 __asm__ __volatile__ (
407 /*
408 * Setup an empty pt_regs. Fill SP and PC fields as
409 * they're needed by longjmp_break_handler.
Mika Westerberg782a0fd2010-03-29 06:59:16 +0100410 *
411 * We allocate some slack between the original SP and start of
412 * our fabricated regs. To be precise we want to have worst case
413 * covered which is STMFD with all 16 regs so we allocate 2 *
414 * sizeof(struct_pt_regs)).
415 *
416 * This is to prevent any simulated instruction from writing
417 * over the regs when they are accessing the stack.
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000418 */
419 "sub sp, %0, %1 \n\t"
420 "ldr r0, ="__stringify(JPROBE_MAGIC_ADDR)"\n\t"
421 "str %0, [sp, %2] \n\t"
422 "str r0, [sp, %3] \n\t"
423 "mov r0, sp \n\t"
424 "bl kprobe_handler \n\t"
425
426 /*
427 * Return to the context saved by setjmp_pre_handler
428 * and restored by longjmp_break_handler.
429 */
430 "ldr r0, [sp, %4] \n\t"
431 "msr cpsr_cxsf, r0 \n\t"
432 "ldmia sp, {r0 - pc} \n\t"
433 :
434 : "r" (kcb->jprobe_saved_regs.ARM_sp),
Mika Westerberg782a0fd2010-03-29 06:59:16 +0100435 "I" (sizeof(struct pt_regs) * 2),
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000436 "J" (offsetof(struct pt_regs, ARM_sp)),
437 "J" (offsetof(struct pt_regs, ARM_pc)),
438 "J" (offsetof(struct pt_regs, ARM_cpsr))
439 : "memory", "cc");
440}
441
442int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
443{
444 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
445 long stack_addr = kcb->jprobe_saved_regs.ARM_sp;
446 long orig_sp = regs->ARM_sp;
447 struct jprobe *jp = container_of(p, struct jprobe, kp);
448
449 if (regs->ARM_pc == JPROBE_MAGIC_ADDR) {
450 if (orig_sp != stack_addr) {
451 struct pt_regs *saved_regs =
452 (struct pt_regs *)kcb->jprobe_saved_regs.ARM_sp;
453 printk("current sp %lx does not match saved sp %lx\n",
454 orig_sp, stack_addr);
455 printk("Saved registers for jprobe %p\n", jp);
456 show_regs(saved_regs);
457 printk("Current registers\n");
458 show_regs(regs);
459 BUG();
460 }
461 *regs = kcb->jprobe_saved_regs;
462 memcpy((void *)stack_addr, kcb->jprobes_stack,
463 MIN_STACK_SIZE(stack_addr));
464 preempt_enable_no_resched();
465 return 1;
466 }
467 return 0;
468}
469
Nicolas Pitreb24061f2008-03-04 21:56:21 +0100470int __kprobes arch_trampoline_kprobe(struct kprobe *p)
471{
472 return 0;
473}
474
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000475static struct undef_hook kprobes_break_hook = {
476 .instr_mask = 0xffffffff,
477 .instr_val = KPROBE_BREAKPOINT_INSTRUCTION,
478 .cpsr_mask = MODE_MASK,
479 .cpsr_val = SVC_MODE,
480 .fn = kprobe_trap_handler,
481};
482
483int __init arch_init_kprobes()
484{
485 arm_kprobe_decode_init();
486 register_undef_hook(&kprobes_break_hook);
487 return 0;
488}