blob: b6e9a1cc1c55a50da958bd99f472f4e93d92eb44 [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
Jon Medhurstaceb4872011-04-19 17:18:35 +010037#define flush_insns(addr, size) \
Abhishek Sagar24ba6132007-06-11 22:20:10 +000038 flush_icache_range((unsigned long)(addr), \
39 (unsigned long)(addr) + \
Jon Medhurstaceb4872011-04-19 17:18:35 +010040 (size))
Abhishek Sagar24ba6132007-06-11 22:20:10 +000041
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];
Jon Medhurstaceb4872011-04-19 17:18:35 +010089 flush_insns(p->ainsn.insn,
90 sizeof(p->ainsn.insn[0]) * MAX_INSN_SIZE);
Abhishek Sagar24ba6132007-06-11 22:20:10 +000091 break;
92
93 case INSN_GOOD_NO_SLOT: /* instruction doesn't need insn slot */
94 p->ainsn.insn = NULL;
95 break;
96 }
97
98 return 0;
99}
100
Jon Medhurstaceb4872011-04-19 17:18:35 +0100101#ifdef CONFIG_THUMB2_KERNEL
102
103/*
104 * For a 32-bit Thumb breakpoint spanning two memory words we need to take
105 * special precautions to insert the breakpoint atomically, especially on SMP
106 * systems. This is achieved by calling this arming function using stop_machine.
107 */
108static int __kprobes set_t32_breakpoint(void *addr)
109{
110 ((u16 *)addr)[0] = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION >> 16;
111 ((u16 *)addr)[1] = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION & 0xffff;
112 flush_insns(addr, 2*sizeof(u16));
113 return 0;
114}
115
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000116void __kprobes arch_arm_kprobe(struct kprobe *p)
117{
Jon Medhurstaceb4872011-04-19 17:18:35 +0100118 uintptr_t addr = (uintptr_t)p->addr & ~1; /* Remove any Thumb flag */
119
120 if (!is_wide_instruction(p->opcode)) {
121 *(u16 *)addr = KPROBE_THUMB16_BREAKPOINT_INSTRUCTION;
122 flush_insns(addr, sizeof(u16));
123 } else if (addr & 2) {
124 /* A 32-bit instruction spanning two words needs special care */
125 stop_machine(set_t32_breakpoint, (void *)addr, &cpu_online_map);
126 } else {
127 /* Word aligned 32-bit instruction can be written atomically */
128 u32 bkp = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION;
129#ifndef __ARMEB__ /* Swap halfwords for little-endian */
130 bkp = (bkp >> 16) | (bkp << 16);
131#endif
132 *(u32 *)addr = bkp;
133 flush_insns(addr, sizeof(u32));
134 }
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000135}
136
Jon Medhurstaceb4872011-04-19 17:18:35 +0100137#else /* !CONFIG_THUMB2_KERNEL */
138
139void __kprobes arch_arm_kprobe(struct kprobe *p)
140{
141 *p->addr = KPROBE_ARM_BREAKPOINT_INSTRUCTION;
142 flush_insns(p->addr, sizeof(p->addr[0]));
143}
144
145#endif /* !CONFIG_THUMB2_KERNEL */
146
Frederic Riss2003b7a2009-09-21 08:43:30 +0100147/*
148 * The actual disarming is done here on each CPU and synchronized using
149 * stop_machine. This synchronization is necessary on SMP to avoid removing
150 * a probe between the moment the 'Undefined Instruction' exception is raised
151 * and the moment the exception handler reads the faulting instruction from
Jon Medhurstaceb4872011-04-19 17:18:35 +0100152 * memory. It is also needed to atomically set the two half-words of a 32-bit
153 * Thumb breakpoint.
Frederic Riss2003b7a2009-09-21 08:43:30 +0100154 */
155int __kprobes __arch_disarm_kprobe(void *p)
156{
157 struct kprobe *kp = p;
Jon Medhurstaceb4872011-04-19 17:18:35 +0100158#ifdef CONFIG_THUMB2_KERNEL
159 u16 *addr = (u16 *)((uintptr_t)kp->addr & ~1);
160 kprobe_opcode_t insn = kp->opcode;
161 unsigned int len;
162
163 if (is_wide_instruction(insn)) {
164 ((u16 *)addr)[0] = insn>>16;
165 ((u16 *)addr)[1] = insn;
166 len = 2*sizeof(u16);
167 } else {
168 ((u16 *)addr)[0] = insn;
169 len = sizeof(u16);
170 }
171 flush_insns(addr, len);
172
173#else /* !CONFIG_THUMB2_KERNEL */
Frederic Riss2003b7a2009-09-21 08:43:30 +0100174 *kp->addr = kp->opcode;
Jon Medhurstaceb4872011-04-19 17:18:35 +0100175 flush_insns(kp->addr, sizeof(kp->addr[0]));
176#endif
Frederic Riss2003b7a2009-09-21 08:43:30 +0100177 return 0;
178}
179
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000180void __kprobes arch_disarm_kprobe(struct kprobe *p)
181{
Frederic Riss2003b7a2009-09-21 08:43:30 +0100182 stop_machine(__arch_disarm_kprobe, p, &cpu_online_map);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000183}
184
185void __kprobes arch_remove_kprobe(struct kprobe *p)
186{
187 if (p->ainsn.insn) {
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000188 free_insn_slot(p->ainsn.insn, 0);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000189 p->ainsn.insn = NULL;
190 }
191}
192
193static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
194{
195 kcb->prev_kprobe.kp = kprobe_running();
196 kcb->prev_kprobe.status = kcb->kprobe_status;
197}
198
199static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
200{
201 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
202 kcb->kprobe_status = kcb->prev_kprobe.status;
203}
204
205static void __kprobes set_current_kprobe(struct kprobe *p)
206{
207 __get_cpu_var(current_kprobe) = p;
208}
209
Jon Medhurst3cca6c22011-06-16 15:54:00 +0100210static void __kprobes
211singlestep_skip(struct kprobe *p, struct pt_regs *regs)
212{
213#ifdef CONFIG_THUMB2_KERNEL
214 regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
215 if (is_wide_instruction(p->opcode))
216 regs->ARM_pc += 4;
217 else
218 regs->ARM_pc += 2;
219#else
220 regs->ARM_pc += 4;
221#endif
222}
223
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000224static void __kprobes singlestep(struct kprobe *p, struct pt_regs *regs,
225 struct kprobe_ctlblk *kcb)
226{
227 regs->ARM_pc += 4;
Jon Medhurst073090c2011-04-06 11:17:09 +0100228 if (p->ainsn.insn_check_cc(regs->ARM_cpsr))
229 p->ainsn.insn_handler(p, regs);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000230}
231
232/*
233 * Called with IRQs disabled. IRQs must remain disabled from that point
234 * all the way until processing this kprobe is complete. The current
235 * kprobes implementation cannot process more than one nested level of
236 * kprobe, and that level is reserved for user kprobe handlers, so we can't
237 * risk encountering a new kprobe in an interrupt handler.
238 */
239void __kprobes kprobe_handler(struct pt_regs *regs)
240{
241 struct kprobe *p, *cur;
242 struct kprobe_ctlblk *kcb;
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000243
244 kcb = get_kprobe_ctlblk();
245 cur = kprobe_running();
Jon Medhurstaceb4872011-04-19 17:18:35 +0100246
247#ifdef CONFIG_THUMB2_KERNEL
248 /*
249 * First look for a probe which was registered using an address with
250 * bit 0 set, this is the usual situation for pointers to Thumb code.
251 * If not found, fallback to looking for one with bit 0 clear.
252 */
253 p = get_kprobe((kprobe_opcode_t *)(regs->ARM_pc | 1));
254 if (!p)
255 p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
256
257#else /* ! CONFIG_THUMB2_KERNEL */
258 p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
259#endif
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000260
261 if (p) {
262 if (cur) {
263 /* Kprobe is pending, so we're recursing. */
264 switch (kcb->kprobe_status) {
265 case KPROBE_HIT_ACTIVE:
266 case KPROBE_HIT_SSDONE:
267 /* A pre- or post-handler probe got us here. */
268 kprobes_inc_nmissed_count(p);
269 save_previous_kprobe(kcb);
270 set_current_kprobe(p);
271 kcb->kprobe_status = KPROBE_REENTER;
272 singlestep(p, regs, kcb);
273 restore_previous_kprobe(kcb);
274 break;
275 default:
276 /* impossible cases */
277 BUG();
278 }
Jon Medhurst3cca6c22011-06-16 15:54:00 +0100279 } else if (p->ainsn.insn_check_cc(regs->ARM_cpsr)) {
280 /* Probe hit and conditional execution check ok. */
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000281 set_current_kprobe(p);
282 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
283
284 /*
285 * If we have no pre-handler or it returned 0, we
286 * continue with normal processing. If we have a
287 * pre-handler and it returned non-zero, it prepped
288 * for calling the break_handler below on re-entry,
289 * so get out doing nothing more here.
290 */
291 if (!p->pre_handler || !p->pre_handler(p, regs)) {
292 kcb->kprobe_status = KPROBE_HIT_SS;
293 singlestep(p, regs, kcb);
294 if (p->post_handler) {
295 kcb->kprobe_status = KPROBE_HIT_SSDONE;
296 p->post_handler(p, regs, 0);
297 }
298 reset_current_kprobe();
299 }
Jon Medhurst3cca6c22011-06-16 15:54:00 +0100300 } else {
301 /*
302 * Probe hit but conditional execution check failed,
303 * so just skip the instruction and continue as if
304 * nothing had happened.
305 */
306 singlestep_skip(p, regs);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000307 }
308 } else if (cur) {
309 /* We probably hit a jprobe. Call its break handler. */
310 if (cur->break_handler && cur->break_handler(cur, regs)) {
311 kcb->kprobe_status = KPROBE_HIT_SS;
312 singlestep(cur, regs, kcb);
313 if (cur->post_handler) {
314 kcb->kprobe_status = KPROBE_HIT_SSDONE;
315 cur->post_handler(cur, regs, 0);
316 }
317 }
318 reset_current_kprobe();
319 } else {
320 /*
321 * The probe was removed and a race is in progress.
322 * There is nothing we can do about it. Let's restart
323 * the instruction. By the time we can restart, the
324 * real instruction will be there.
325 */
326 }
327}
328
Nicolas Pitre3305a602008-08-19 04:15:23 +0100329static int __kprobes kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000330{
Nicolas Pitre3305a602008-08-19 04:15:23 +0100331 unsigned long flags;
332 local_irq_save(flags);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000333 kprobe_handler(regs);
Nicolas Pitre3305a602008-08-19 04:15:23 +0100334 local_irq_restore(flags);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000335 return 0;
336}
337
338int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
339{
340 struct kprobe *cur = kprobe_running();
341 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
342
343 switch (kcb->kprobe_status) {
344 case KPROBE_HIT_SS:
345 case KPROBE_REENTER:
346 /*
347 * We are here because the instruction being single
348 * stepped caused a page fault. We reset the current
349 * kprobe and the PC to point back to the probe address
350 * and allow the page fault handler to continue as a
351 * normal page fault.
352 */
353 regs->ARM_pc = (long)cur->addr;
354 if (kcb->kprobe_status == KPROBE_REENTER) {
355 restore_previous_kprobe(kcb);
356 } else {
357 reset_current_kprobe();
358 }
359 break;
360
361 case KPROBE_HIT_ACTIVE:
362 case KPROBE_HIT_SSDONE:
363 /*
364 * We increment the nmissed count for accounting,
365 * we can also use npre/npostfault count for accounting
366 * these specific fault cases.
367 */
368 kprobes_inc_nmissed_count(cur);
369
370 /*
371 * We come here because instructions in the pre/post
372 * handler caused the page_fault, this could happen
373 * if handler tries to access user space by
374 * copy_from_user(), get_user() etc. Let the
375 * user-specified handler try to fix it.
376 */
377 if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
378 return 1;
379 break;
380
381 default:
382 break;
383 }
384
385 return 0;
386}
387
388int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
389 unsigned long val, void *data)
390{
391 /*
392 * notify_die() is currently never called on ARM,
393 * so this callback is currently empty.
394 */
395 return NOTIFY_DONE;
396}
397
398/*
399 * When a retprobed function returns, trampoline_handler() is called,
400 * calling the kretprobe's handler. We construct a struct pt_regs to
401 * give a view of registers r0-r11 to the user return-handler. This is
402 * not a complete pt_regs structure, but that should be plenty sufficient
403 * for kretprobe handlers which should normally be interested in r0 only
404 * anyway.
405 */
Abhishek Sagare0773412008-05-31 14:24:02 +0530406void __naked __kprobes kretprobe_trampoline(void)
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000407{
408 __asm__ __volatile__ (
409 "stmdb sp!, {r0 - r11} \n\t"
410 "mov r0, sp \n\t"
411 "bl trampoline_handler \n\t"
412 "mov lr, r0 \n\t"
413 "ldmia sp!, {r0 - r11} \n\t"
Jon Medhurstde419842011-06-14 13:08:04 +0100414#ifdef CONFIG_THUMB2_KERNEL
415 "bx lr \n\t"
416#else
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000417 "mov pc, lr \n\t"
Jon Medhurstde419842011-06-14 13:08:04 +0100418#endif
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000419 : : : "memory");
420}
421
422/* Called from kretprobe_trampoline */
423static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
424{
425 struct kretprobe_instance *ri = NULL;
426 struct hlist_head *head, empty_rp;
427 struct hlist_node *node, *tmp;
428 unsigned long flags, orig_ret_address = 0;
429 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
430
431 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700432 kretprobe_hash_lock(current, &head, &flags);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000433
434 /*
435 * It is possible to have multiple instances associated with a given
436 * task either because multiple functions in the call path have
437 * a return probe installed on them, and/or more than one return
438 * probe was registered for a target function.
439 *
440 * We can handle this because:
441 * - instances are always inserted at the head of the list
442 * - when multiple return probes are registered for the same
443 * function, the first instance's ret_addr will point to the
444 * real return address, and all the rest will point to
445 * kretprobe_trampoline
446 */
447 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
448 if (ri->task != current)
449 /* another task is sharing our hash bucket */
450 continue;
451
452 if (ri->rp && ri->rp->handler) {
453 __get_cpu_var(current_kprobe) = &ri->rp->kp;
454 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
455 ri->rp->handler(ri, regs);
456 __get_cpu_var(current_kprobe) = NULL;
457 }
458
459 orig_ret_address = (unsigned long)ri->ret_addr;
460 recycle_rp_inst(ri, &empty_rp);
461
462 if (orig_ret_address != trampoline_address)
463 /*
464 * This is the real return address. Any other
465 * instances associated with this task are for
466 * other calls deeper on the call stack
467 */
468 break;
469 }
470
471 kretprobe_assert(ri, orig_ret_address, trampoline_address);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700472 kretprobe_hash_unlock(current, &flags);
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000473
474 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
475 hlist_del(&ri->hlist);
476 kfree(ri);
477 }
478
479 return (void *)orig_ret_address;
480}
481
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000482void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
483 struct pt_regs *regs)
484{
485 ri->ret_addr = (kprobe_opcode_t *)regs->ARM_lr;
486
487 /* Replace the return addr with trampoline addr. */
488 regs->ARM_lr = (unsigned long)&kretprobe_trampoline;
489}
490
491int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
492{
493 struct jprobe *jp = container_of(p, struct jprobe, kp);
494 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
495 long sp_addr = regs->ARM_sp;
Jon Medhurstde419842011-06-14 13:08:04 +0100496 long cpsr;
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000497
498 kcb->jprobe_saved_regs = *regs;
499 memcpy(kcb->jprobes_stack, (void *)sp_addr, MIN_STACK_SIZE(sp_addr));
500 regs->ARM_pc = (long)jp->entry;
Jon Medhurstde419842011-06-14 13:08:04 +0100501
502 cpsr = regs->ARM_cpsr | PSR_I_BIT;
503#ifdef CONFIG_THUMB2_KERNEL
504 /* Set correct Thumb state in cpsr */
505 if (regs->ARM_pc & 1)
506 cpsr |= PSR_T_BIT;
507 else
508 cpsr &= ~PSR_T_BIT;
509#endif
510 regs->ARM_cpsr = cpsr;
511
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000512 preempt_disable();
513 return 1;
514}
515
516void __kprobes jprobe_return(void)
517{
518 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
519
520 __asm__ __volatile__ (
521 /*
522 * Setup an empty pt_regs. Fill SP and PC fields as
523 * they're needed by longjmp_break_handler.
Mika Westerberg782a0fd2010-03-29 06:59:16 +0100524 *
525 * We allocate some slack between the original SP and start of
526 * our fabricated regs. To be precise we want to have worst case
527 * covered which is STMFD with all 16 regs so we allocate 2 *
528 * sizeof(struct_pt_regs)).
529 *
530 * This is to prevent any simulated instruction from writing
531 * over the regs when they are accessing the stack.
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000532 */
Jon Medhurstde419842011-06-14 13:08:04 +0100533#ifdef CONFIG_THUMB2_KERNEL
534 "sub r0, %0, %1 \n\t"
535 "mov sp, r0 \n\t"
536#else
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000537 "sub sp, %0, %1 \n\t"
Jon Medhurstde419842011-06-14 13:08:04 +0100538#endif
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000539 "ldr r0, ="__stringify(JPROBE_MAGIC_ADDR)"\n\t"
540 "str %0, [sp, %2] \n\t"
541 "str r0, [sp, %3] \n\t"
542 "mov r0, sp \n\t"
543 "bl kprobe_handler \n\t"
544
545 /*
546 * Return to the context saved by setjmp_pre_handler
547 * and restored by longjmp_break_handler.
548 */
Jon Medhurstde419842011-06-14 13:08:04 +0100549#ifdef CONFIG_THUMB2_KERNEL
550 "ldr lr, [sp, %2] \n\t" /* lr = saved sp */
551 "ldrd r0, r1, [sp, %5] \n\t" /* r0,r1 = saved lr,pc */
552 "ldr r2, [sp, %4] \n\t" /* r2 = saved psr */
553 "stmdb lr!, {r0, r1, r2} \n\t" /* push saved lr and */
554 /* rfe context */
555 "ldmia sp, {r0 - r12} \n\t"
556 "mov sp, lr \n\t"
557 "ldr lr, [sp], #4 \n\t"
558 "rfeia sp! \n\t"
559#else
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000560 "ldr r0, [sp, %4] \n\t"
561 "msr cpsr_cxsf, r0 \n\t"
562 "ldmia sp, {r0 - pc} \n\t"
Jon Medhurstde419842011-06-14 13:08:04 +0100563#endif
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000564 :
565 : "r" (kcb->jprobe_saved_regs.ARM_sp),
Mika Westerberg782a0fd2010-03-29 06:59:16 +0100566 "I" (sizeof(struct pt_regs) * 2),
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000567 "J" (offsetof(struct pt_regs, ARM_sp)),
568 "J" (offsetof(struct pt_regs, ARM_pc)),
Jon Medhurstde419842011-06-14 13:08:04 +0100569 "J" (offsetof(struct pt_regs, ARM_cpsr)),
570 "J" (offsetof(struct pt_regs, ARM_lr))
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000571 : "memory", "cc");
572}
573
574int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
575{
576 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
577 long stack_addr = kcb->jprobe_saved_regs.ARM_sp;
578 long orig_sp = regs->ARM_sp;
579 struct jprobe *jp = container_of(p, struct jprobe, kp);
580
581 if (regs->ARM_pc == JPROBE_MAGIC_ADDR) {
582 if (orig_sp != stack_addr) {
583 struct pt_regs *saved_regs =
584 (struct pt_regs *)kcb->jprobe_saved_regs.ARM_sp;
585 printk("current sp %lx does not match saved sp %lx\n",
586 orig_sp, stack_addr);
587 printk("Saved registers for jprobe %p\n", jp);
588 show_regs(saved_regs);
589 printk("Current registers\n");
590 show_regs(regs);
591 BUG();
592 }
593 *regs = kcb->jprobe_saved_regs;
594 memcpy((void *)stack_addr, kcb->jprobes_stack,
595 MIN_STACK_SIZE(stack_addr));
596 preempt_enable_no_resched();
597 return 1;
598 }
599 return 0;
600}
601
Nicolas Pitreb24061f2008-03-04 21:56:21 +0100602int __kprobes arch_trampoline_kprobe(struct kprobe *p)
603{
604 return 0;
605}
606
Jon Medhurstaceb4872011-04-19 17:18:35 +0100607#ifdef CONFIG_THUMB2_KERNEL
608
609static struct undef_hook kprobes_thumb16_break_hook = {
610 .instr_mask = 0xffff,
611 .instr_val = KPROBE_THUMB16_BREAKPOINT_INSTRUCTION,
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000612 .cpsr_mask = MODE_MASK,
613 .cpsr_val = SVC_MODE,
614 .fn = kprobe_trap_handler,
615};
616
Jon Medhurstaceb4872011-04-19 17:18:35 +0100617static struct undef_hook kprobes_thumb32_break_hook = {
618 .instr_mask = 0xffffffff,
619 .instr_val = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION,
620 .cpsr_mask = MODE_MASK,
621 .cpsr_val = SVC_MODE,
622 .fn = kprobe_trap_handler,
623};
624
625#else /* !CONFIG_THUMB2_KERNEL */
626
627static struct undef_hook kprobes_arm_break_hook = {
628 .instr_mask = 0xffffffff,
629 .instr_val = KPROBE_ARM_BREAKPOINT_INSTRUCTION,
630 .cpsr_mask = MODE_MASK,
631 .cpsr_val = SVC_MODE,
632 .fn = kprobe_trap_handler,
633};
634
635#endif /* !CONFIG_THUMB2_KERNEL */
636
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000637int __init arch_init_kprobes()
638{
639 arm_kprobe_decode_init();
Jon Medhurstaceb4872011-04-19 17:18:35 +0100640#ifdef CONFIG_THUMB2_KERNEL
641 register_undef_hook(&kprobes_thumb16_break_hook);
642 register_undef_hook(&kprobes_thumb32_break_hook);
643#else
644 register_undef_hook(&kprobes_arm_break_hook);
645#endif
Abhishek Sagar24ba6132007-06-11 22:20:10 +0000646 return 0;
647}