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