kprobes.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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. #include <linux/kernel.h>
  22. #include <linux/kprobes.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/stop_machine.h>
  26. #include <linux/stringify.h>
  27. #include <asm/traps.h>
  28. #include <asm/cacheflush.h>
  29. #include "kprobes.h"
  30. #define MIN_STACK_SIZE(addr) \
  31. min((unsigned long)MAX_STACK_SIZE, \
  32. (unsigned long)current_thread_info() + THREAD_START_SP - (addr))
  33. #define flush_insns(addr, size) \
  34. flush_icache_range((unsigned long)(addr), \
  35. (unsigned long)(addr) + \
  36. (size))
  37. /* Used as a marker in ARM_pc to note when we're in a jprobe. */
  38. #define JPROBE_MAGIC_ADDR 0xffffffff
  39. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  40. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  41. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  42. {
  43. kprobe_opcode_t insn;
  44. kprobe_opcode_t tmp_insn[MAX_INSN_SIZE];
  45. unsigned long addr = (unsigned long)p->addr;
  46. bool thumb;
  47. kprobe_decode_insn_t *decode_insn;
  48. int is;
  49. if (in_exception_text(addr))
  50. return -EINVAL;
  51. #ifdef CONFIG_THUMB2_KERNEL
  52. thumb = true;
  53. addr &= ~1; /* Bit 0 would normally be set to indicate Thumb code */
  54. insn = ((u16 *)addr)[0];
  55. if (is_wide_instruction(insn)) {
  56. insn <<= 16;
  57. insn |= ((u16 *)addr)[1];
  58. decode_insn = thumb32_kprobe_decode_insn;
  59. } else
  60. decode_insn = thumb16_kprobe_decode_insn;
  61. #else /* !CONFIG_THUMB2_KERNEL */
  62. thumb = false;
  63. if (addr & 0x3)
  64. return -EINVAL;
  65. insn = *p->addr;
  66. decode_insn = arm_kprobe_decode_insn;
  67. #endif
  68. p->opcode = insn;
  69. p->ainsn.insn = tmp_insn;
  70. switch ((*decode_insn)(insn, &p->ainsn)) {
  71. case INSN_REJECTED: /* not supported */
  72. return -EINVAL;
  73. case INSN_GOOD: /* instruction uses slot */
  74. p->ainsn.insn = get_insn_slot();
  75. if (!p->ainsn.insn)
  76. return -ENOMEM;
  77. for (is = 0; is < MAX_INSN_SIZE; ++is)
  78. p->ainsn.insn[is] = tmp_insn[is];
  79. flush_insns(p->ainsn.insn,
  80. sizeof(p->ainsn.insn[0]) * MAX_INSN_SIZE);
  81. p->ainsn.insn_fn = (kprobe_insn_fn_t *)
  82. ((uintptr_t)p->ainsn.insn | thumb);
  83. break;
  84. case INSN_GOOD_NO_SLOT: /* instruction doesn't need insn slot */
  85. p->ainsn.insn = NULL;
  86. break;
  87. }
  88. return 0;
  89. }
  90. #ifdef CONFIG_THUMB2_KERNEL
  91. /*
  92. * For a 32-bit Thumb breakpoint spanning two memory words we need to take
  93. * special precautions to insert the breakpoint atomically, especially on SMP
  94. * systems. This is achieved by calling this arming function using stop_machine.
  95. */
  96. static int __kprobes set_t32_breakpoint(void *addr)
  97. {
  98. ((u16 *)addr)[0] = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION >> 16;
  99. ((u16 *)addr)[1] = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION & 0xffff;
  100. flush_insns(addr, 2*sizeof(u16));
  101. return 0;
  102. }
  103. void __kprobes arch_arm_kprobe(struct kprobe *p)
  104. {
  105. uintptr_t addr = (uintptr_t)p->addr & ~1; /* Remove any Thumb flag */
  106. if (!is_wide_instruction(p->opcode)) {
  107. *(u16 *)addr = KPROBE_THUMB16_BREAKPOINT_INSTRUCTION;
  108. flush_insns(addr, sizeof(u16));
  109. } else if (addr & 2) {
  110. /* A 32-bit instruction spanning two words needs special care */
  111. stop_machine(set_t32_breakpoint, (void *)addr, &cpu_online_map);
  112. } else {
  113. /* Word aligned 32-bit instruction can be written atomically */
  114. u32 bkp = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION;
  115. #ifndef __ARMEB__ /* Swap halfwords for little-endian */
  116. bkp = (bkp >> 16) | (bkp << 16);
  117. #endif
  118. *(u32 *)addr = bkp;
  119. flush_insns(addr, sizeof(u32));
  120. }
  121. }
  122. #else /* !CONFIG_THUMB2_KERNEL */
  123. void __kprobes arch_arm_kprobe(struct kprobe *p)
  124. {
  125. kprobe_opcode_t insn = p->opcode;
  126. kprobe_opcode_t brkp = KPROBE_ARM_BREAKPOINT_INSTRUCTION;
  127. if (insn >= 0xe0000000)
  128. brkp |= 0xe0000000; /* Unconditional instruction */
  129. else
  130. brkp |= insn & 0xf0000000; /* Copy condition from insn */
  131. *p->addr = brkp;
  132. flush_insns(p->addr, sizeof(p->addr[0]));
  133. }
  134. #endif /* !CONFIG_THUMB2_KERNEL */
  135. /*
  136. * The actual disarming is done here on each CPU and synchronized using
  137. * stop_machine. This synchronization is necessary on SMP to avoid removing
  138. * a probe between the moment the 'Undefined Instruction' exception is raised
  139. * and the moment the exception handler reads the faulting instruction from
  140. * memory. It is also needed to atomically set the two half-words of a 32-bit
  141. * Thumb breakpoint.
  142. */
  143. int __kprobes __arch_disarm_kprobe(void *p)
  144. {
  145. struct kprobe *kp = p;
  146. #ifdef CONFIG_THUMB2_KERNEL
  147. u16 *addr = (u16 *)((uintptr_t)kp->addr & ~1);
  148. kprobe_opcode_t insn = kp->opcode;
  149. unsigned int len;
  150. if (is_wide_instruction(insn)) {
  151. ((u16 *)addr)[0] = insn>>16;
  152. ((u16 *)addr)[1] = insn;
  153. len = 2*sizeof(u16);
  154. } else {
  155. ((u16 *)addr)[0] = insn;
  156. len = sizeof(u16);
  157. }
  158. flush_insns(addr, len);
  159. #else /* !CONFIG_THUMB2_KERNEL */
  160. *kp->addr = kp->opcode;
  161. flush_insns(kp->addr, sizeof(kp->addr[0]));
  162. #endif
  163. return 0;
  164. }
  165. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  166. {
  167. stop_machine(__arch_disarm_kprobe, p, &cpu_online_map);
  168. }
  169. void __kprobes arch_remove_kprobe(struct kprobe *p)
  170. {
  171. if (p->ainsn.insn) {
  172. free_insn_slot(p->ainsn.insn, 0);
  173. p->ainsn.insn = NULL;
  174. }
  175. }
  176. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  177. {
  178. kcb->prev_kprobe.kp = kprobe_running();
  179. kcb->prev_kprobe.status = kcb->kprobe_status;
  180. }
  181. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  182. {
  183. __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
  184. kcb->kprobe_status = kcb->prev_kprobe.status;
  185. }
  186. static void __kprobes set_current_kprobe(struct kprobe *p)
  187. {
  188. __get_cpu_var(current_kprobe) = p;
  189. }
  190. static void __kprobes
  191. singlestep_skip(struct kprobe *p, struct pt_regs *regs)
  192. {
  193. #ifdef CONFIG_THUMB2_KERNEL
  194. regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
  195. if (is_wide_instruction(p->opcode))
  196. regs->ARM_pc += 4;
  197. else
  198. regs->ARM_pc += 2;
  199. #else
  200. regs->ARM_pc += 4;
  201. #endif
  202. }
  203. static inline void __kprobes
  204. singlestep(struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
  205. {
  206. p->ainsn.insn_singlestep(p, regs);
  207. }
  208. /*
  209. * Called with IRQs disabled. IRQs must remain disabled from that point
  210. * all the way until processing this kprobe is complete. The current
  211. * kprobes implementation cannot process more than one nested level of
  212. * kprobe, and that level is reserved for user kprobe handlers, so we can't
  213. * risk encountering a new kprobe in an interrupt handler.
  214. */
  215. void __kprobes kprobe_handler(struct pt_regs *regs)
  216. {
  217. struct kprobe *p, *cur;
  218. struct kprobe_ctlblk *kcb;
  219. kcb = get_kprobe_ctlblk();
  220. cur = kprobe_running();
  221. #ifdef CONFIG_THUMB2_KERNEL
  222. /*
  223. * First look for a probe which was registered using an address with
  224. * bit 0 set, this is the usual situation for pointers to Thumb code.
  225. * If not found, fallback to looking for one with bit 0 clear.
  226. */
  227. p = get_kprobe((kprobe_opcode_t *)(regs->ARM_pc | 1));
  228. if (!p)
  229. p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
  230. #else /* ! CONFIG_THUMB2_KERNEL */
  231. p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
  232. #endif
  233. if (p) {
  234. if (cur) {
  235. /* Kprobe is pending, so we're recursing. */
  236. switch (kcb->kprobe_status) {
  237. case KPROBE_HIT_ACTIVE:
  238. case KPROBE_HIT_SSDONE:
  239. /* A pre- or post-handler probe got us here. */
  240. kprobes_inc_nmissed_count(p);
  241. save_previous_kprobe(kcb);
  242. set_current_kprobe(p);
  243. kcb->kprobe_status = KPROBE_REENTER;
  244. singlestep(p, regs, kcb);
  245. restore_previous_kprobe(kcb);
  246. break;
  247. default:
  248. /* impossible cases */
  249. BUG();
  250. }
  251. } else if (p->ainsn.insn_check_cc(regs->ARM_cpsr)) {
  252. /* Probe hit and conditional execution check ok. */
  253. set_current_kprobe(p);
  254. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  255. /*
  256. * If we have no pre-handler or it returned 0, we
  257. * continue with normal processing. If we have a
  258. * pre-handler and it returned non-zero, it prepped
  259. * for calling the break_handler below on re-entry,
  260. * so get out doing nothing more here.
  261. */
  262. if (!p->pre_handler || !p->pre_handler(p, regs)) {
  263. kcb->kprobe_status = KPROBE_HIT_SS;
  264. singlestep(p, regs, kcb);
  265. if (p->post_handler) {
  266. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  267. p->post_handler(p, regs, 0);
  268. }
  269. reset_current_kprobe();
  270. }
  271. } else {
  272. /*
  273. * Probe hit but conditional execution check failed,
  274. * so just skip the instruction and continue as if
  275. * nothing had happened.
  276. */
  277. singlestep_skip(p, regs);
  278. }
  279. } else if (cur) {
  280. /* We probably hit a jprobe. Call its break handler. */
  281. if (cur->break_handler && cur->break_handler(cur, regs)) {
  282. kcb->kprobe_status = KPROBE_HIT_SS;
  283. singlestep(cur, regs, kcb);
  284. if (cur->post_handler) {
  285. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  286. cur->post_handler(cur, regs, 0);
  287. }
  288. }
  289. reset_current_kprobe();
  290. } else {
  291. /*
  292. * The probe was removed and a race is in progress.
  293. * There is nothing we can do about it. Let's restart
  294. * the instruction. By the time we can restart, the
  295. * real instruction will be there.
  296. */
  297. }
  298. }
  299. static int __kprobes kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
  300. {
  301. unsigned long flags;
  302. local_irq_save(flags);
  303. kprobe_handler(regs);
  304. local_irq_restore(flags);
  305. return 0;
  306. }
  307. int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
  308. {
  309. struct kprobe *cur = kprobe_running();
  310. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  311. switch (kcb->kprobe_status) {
  312. case KPROBE_HIT_SS:
  313. case KPROBE_REENTER:
  314. /*
  315. * We are here because the instruction being single
  316. * stepped caused a page fault. We reset the current
  317. * kprobe and the PC to point back to the probe address
  318. * and allow the page fault handler to continue as a
  319. * normal page fault.
  320. */
  321. regs->ARM_pc = (long)cur->addr;
  322. if (kcb->kprobe_status == KPROBE_REENTER) {
  323. restore_previous_kprobe(kcb);
  324. } else {
  325. reset_current_kprobe();
  326. }
  327. break;
  328. case KPROBE_HIT_ACTIVE:
  329. case KPROBE_HIT_SSDONE:
  330. /*
  331. * We increment the nmissed count for accounting,
  332. * we can also use npre/npostfault count for accounting
  333. * these specific fault cases.
  334. */
  335. kprobes_inc_nmissed_count(cur);
  336. /*
  337. * We come here because instructions in the pre/post
  338. * handler caused the page_fault, this could happen
  339. * if handler tries to access user space by
  340. * copy_from_user(), get_user() etc. Let the
  341. * user-specified handler try to fix it.
  342. */
  343. if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
  344. return 1;
  345. break;
  346. default:
  347. break;
  348. }
  349. return 0;
  350. }
  351. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  352. unsigned long val, void *data)
  353. {
  354. /*
  355. * notify_die() is currently never called on ARM,
  356. * so this callback is currently empty.
  357. */
  358. return NOTIFY_DONE;
  359. }
  360. /*
  361. * When a retprobed function returns, trampoline_handler() is called,
  362. * calling the kretprobe's handler. We construct a struct pt_regs to
  363. * give a view of registers r0-r11 to the user return-handler. This is
  364. * not a complete pt_regs structure, but that should be plenty sufficient
  365. * for kretprobe handlers which should normally be interested in r0 only
  366. * anyway.
  367. */
  368. void __naked __kprobes kretprobe_trampoline(void)
  369. {
  370. __asm__ __volatile__ (
  371. "stmdb sp!, {r0 - r11} \n\t"
  372. "mov r0, sp \n\t"
  373. "bl trampoline_handler \n\t"
  374. "mov lr, r0 \n\t"
  375. "ldmia sp!, {r0 - r11} \n\t"
  376. #ifdef CONFIG_THUMB2_KERNEL
  377. "bx lr \n\t"
  378. #else
  379. "mov pc, lr \n\t"
  380. #endif
  381. : : : "memory");
  382. }
  383. /* Called from kretprobe_trampoline */
  384. static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
  385. {
  386. struct kretprobe_instance *ri = NULL;
  387. struct hlist_head *head, empty_rp;
  388. struct hlist_node *node, *tmp;
  389. unsigned long flags, orig_ret_address = 0;
  390. unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
  391. INIT_HLIST_HEAD(&empty_rp);
  392. kretprobe_hash_lock(current, &head, &flags);
  393. /*
  394. * It is possible to have multiple instances associated with a given
  395. * task either because multiple functions in the call path have
  396. * a return probe installed on them, and/or more than one return
  397. * probe was registered for a target function.
  398. *
  399. * We can handle this because:
  400. * - instances are always inserted at the head of the list
  401. * - when multiple return probes are registered for the same
  402. * function, the first instance's ret_addr will point to the
  403. * real return address, and all the rest will point to
  404. * kretprobe_trampoline
  405. */
  406. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  407. if (ri->task != current)
  408. /* another task is sharing our hash bucket */
  409. continue;
  410. if (ri->rp && ri->rp->handler) {
  411. __get_cpu_var(current_kprobe) = &ri->rp->kp;
  412. get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
  413. ri->rp->handler(ri, regs);
  414. __get_cpu_var(current_kprobe) = NULL;
  415. }
  416. orig_ret_address = (unsigned long)ri->ret_addr;
  417. recycle_rp_inst(ri, &empty_rp);
  418. if (orig_ret_address != trampoline_address)
  419. /*
  420. * This is the real return address. Any other
  421. * instances associated with this task are for
  422. * other calls deeper on the call stack
  423. */
  424. break;
  425. }
  426. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  427. kretprobe_hash_unlock(current, &flags);
  428. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  429. hlist_del(&ri->hlist);
  430. kfree(ri);
  431. }
  432. return (void *)orig_ret_address;
  433. }
  434. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  435. struct pt_regs *regs)
  436. {
  437. ri->ret_addr = (kprobe_opcode_t *)regs->ARM_lr;
  438. /* Replace the return addr with trampoline addr. */
  439. regs->ARM_lr = (unsigned long)&kretprobe_trampoline;
  440. }
  441. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  442. {
  443. struct jprobe *jp = container_of(p, struct jprobe, kp);
  444. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  445. long sp_addr = regs->ARM_sp;
  446. long cpsr;
  447. kcb->jprobe_saved_regs = *regs;
  448. memcpy(kcb->jprobes_stack, (void *)sp_addr, MIN_STACK_SIZE(sp_addr));
  449. regs->ARM_pc = (long)jp->entry;
  450. cpsr = regs->ARM_cpsr | PSR_I_BIT;
  451. #ifdef CONFIG_THUMB2_KERNEL
  452. /* Set correct Thumb state in cpsr */
  453. if (regs->ARM_pc & 1)
  454. cpsr |= PSR_T_BIT;
  455. else
  456. cpsr &= ~PSR_T_BIT;
  457. #endif
  458. regs->ARM_cpsr = cpsr;
  459. preempt_disable();
  460. return 1;
  461. }
  462. void __kprobes jprobe_return(void)
  463. {
  464. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  465. __asm__ __volatile__ (
  466. /*
  467. * Setup an empty pt_regs. Fill SP and PC fields as
  468. * they're needed by longjmp_break_handler.
  469. *
  470. * We allocate some slack between the original SP and start of
  471. * our fabricated regs. To be precise we want to have worst case
  472. * covered which is STMFD with all 16 regs so we allocate 2 *
  473. * sizeof(struct_pt_regs)).
  474. *
  475. * This is to prevent any simulated instruction from writing
  476. * over the regs when they are accessing the stack.
  477. */
  478. #ifdef CONFIG_THUMB2_KERNEL
  479. "sub r0, %0, %1 \n\t"
  480. "mov sp, r0 \n\t"
  481. #else
  482. "sub sp, %0, %1 \n\t"
  483. #endif
  484. "ldr r0, ="__stringify(JPROBE_MAGIC_ADDR)"\n\t"
  485. "str %0, [sp, %2] \n\t"
  486. "str r0, [sp, %3] \n\t"
  487. "mov r0, sp \n\t"
  488. "bl kprobe_handler \n\t"
  489. /*
  490. * Return to the context saved by setjmp_pre_handler
  491. * and restored by longjmp_break_handler.
  492. */
  493. #ifdef CONFIG_THUMB2_KERNEL
  494. "ldr lr, [sp, %2] \n\t" /* lr = saved sp */
  495. "ldrd r0, r1, [sp, %5] \n\t" /* r0,r1 = saved lr,pc */
  496. "ldr r2, [sp, %4] \n\t" /* r2 = saved psr */
  497. "stmdb lr!, {r0, r1, r2} \n\t" /* push saved lr and */
  498. /* rfe context */
  499. "ldmia sp, {r0 - r12} \n\t"
  500. "mov sp, lr \n\t"
  501. "ldr lr, [sp], #4 \n\t"
  502. "rfeia sp! \n\t"
  503. #else
  504. "ldr r0, [sp, %4] \n\t"
  505. "msr cpsr_cxsf, r0 \n\t"
  506. "ldmia sp, {r0 - pc} \n\t"
  507. #endif
  508. :
  509. : "r" (kcb->jprobe_saved_regs.ARM_sp),
  510. "I" (sizeof(struct pt_regs) * 2),
  511. "J" (offsetof(struct pt_regs, ARM_sp)),
  512. "J" (offsetof(struct pt_regs, ARM_pc)),
  513. "J" (offsetof(struct pt_regs, ARM_cpsr)),
  514. "J" (offsetof(struct pt_regs, ARM_lr))
  515. : "memory", "cc");
  516. }
  517. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  518. {
  519. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  520. long stack_addr = kcb->jprobe_saved_regs.ARM_sp;
  521. long orig_sp = regs->ARM_sp;
  522. struct jprobe *jp = container_of(p, struct jprobe, kp);
  523. if (regs->ARM_pc == JPROBE_MAGIC_ADDR) {
  524. if (orig_sp != stack_addr) {
  525. struct pt_regs *saved_regs =
  526. (struct pt_regs *)kcb->jprobe_saved_regs.ARM_sp;
  527. printk("current sp %lx does not match saved sp %lx\n",
  528. orig_sp, stack_addr);
  529. printk("Saved registers for jprobe %p\n", jp);
  530. show_regs(saved_regs);
  531. printk("Current registers\n");
  532. show_regs(regs);
  533. BUG();
  534. }
  535. *regs = kcb->jprobe_saved_regs;
  536. memcpy((void *)stack_addr, kcb->jprobes_stack,
  537. MIN_STACK_SIZE(stack_addr));
  538. preempt_enable_no_resched();
  539. return 1;
  540. }
  541. return 0;
  542. }
  543. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  544. {
  545. return 0;
  546. }
  547. #ifdef CONFIG_THUMB2_KERNEL
  548. static struct undef_hook kprobes_thumb16_break_hook = {
  549. .instr_mask = 0xffff,
  550. .instr_val = KPROBE_THUMB16_BREAKPOINT_INSTRUCTION,
  551. .cpsr_mask = MODE_MASK,
  552. .cpsr_val = SVC_MODE,
  553. .fn = kprobe_trap_handler,
  554. };
  555. static struct undef_hook kprobes_thumb32_break_hook = {
  556. .instr_mask = 0xffffffff,
  557. .instr_val = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION,
  558. .cpsr_mask = MODE_MASK,
  559. .cpsr_val = SVC_MODE,
  560. .fn = kprobe_trap_handler,
  561. };
  562. #else /* !CONFIG_THUMB2_KERNEL */
  563. static struct undef_hook kprobes_arm_break_hook = {
  564. .instr_mask = 0x0fffffff,
  565. .instr_val = KPROBE_ARM_BREAKPOINT_INSTRUCTION,
  566. .cpsr_mask = MODE_MASK,
  567. .cpsr_val = SVC_MODE,
  568. .fn = kprobe_trap_handler,
  569. };
  570. #endif /* !CONFIG_THUMB2_KERNEL */
  571. int __init arch_init_kprobes()
  572. {
  573. arm_kprobe_decode_init();
  574. #ifdef CONFIG_THUMB2_KERNEL
  575. register_undef_hook(&kprobes_thumb16_break_hook);
  576. register_undef_hook(&kprobes_thumb32_break_hook);
  577. #else
  578. register_undef_hook(&kprobes_arm_break_hook);
  579. #endif
  580. return 0;
  581. }