core.c 18 KB

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