core.c 18 KB

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