kprobes.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Kernel Probes (KProbes)
  4. *
  5. * Copyright (C) IBM Corporation, 2002, 2004
  6. *
  7. * 2002-Oct Created by Vamsi Krishna S <[email protected]> Kernel
  8. * Probes initial implementation ( includes contributions from
  9. * Rusty Russell).
  10. * 2004-July Suparna Bhattacharya <[email protected]> added jumper probes
  11. * interface to access function arguments.
  12. * 2004-Nov Ananth N Mavinakayanahalli <[email protected]> kprobes port
  13. * for PPC64
  14. */
  15. #include <linux/kprobes.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/preempt.h>
  18. #include <linux/extable.h>
  19. #include <linux/kdebug.h>
  20. #include <linux/slab.h>
  21. #include <linux/moduleloader.h>
  22. #include <asm/code-patching.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/sstep.h>
  25. #include <asm/sections.h>
  26. #include <asm/inst.h>
  27. #include <asm/set_memory.h>
  28. #include <linux/uaccess.h>
  29. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  30. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  31. struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};
  32. bool arch_within_kprobe_blacklist(unsigned long addr)
  33. {
  34. return (addr >= (unsigned long)__kprobes_text_start &&
  35. addr < (unsigned long)__kprobes_text_end) ||
  36. (addr >= (unsigned long)_stext &&
  37. addr < (unsigned long)__head_end);
  38. }
  39. kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset)
  40. {
  41. kprobe_opcode_t *addr = NULL;
  42. #ifdef CONFIG_PPC64_ELF_ABI_V2
  43. /* PPC64 ABIv2 needs local entry point */
  44. addr = (kprobe_opcode_t *)kallsyms_lookup_name(name);
  45. if (addr && !offset) {
  46. #ifdef CONFIG_KPROBES_ON_FTRACE
  47. unsigned long faddr;
  48. /*
  49. * Per livepatch.h, ftrace location is always within the first
  50. * 16 bytes of a function on powerpc with -mprofile-kernel.
  51. */
  52. faddr = ftrace_location_range((unsigned long)addr,
  53. (unsigned long)addr + 16);
  54. if (faddr)
  55. addr = (kprobe_opcode_t *)faddr;
  56. else
  57. #endif
  58. addr = (kprobe_opcode_t *)ppc_function_entry(addr);
  59. }
  60. #elif defined(CONFIG_PPC64_ELF_ABI_V1)
  61. /*
  62. * 64bit powerpc ABIv1 uses function descriptors:
  63. * - Check for the dot variant of the symbol first.
  64. * - If that fails, try looking up the symbol provided.
  65. *
  66. * This ensures we always get to the actual symbol and not
  67. * the descriptor.
  68. *
  69. * Also handle <module:symbol> format.
  70. */
  71. char dot_name[MODULE_NAME_LEN + 1 + KSYM_NAME_LEN];
  72. bool dot_appended = false;
  73. const char *c;
  74. ssize_t ret = 0;
  75. int len = 0;
  76. if ((c = strnchr(name, MODULE_NAME_LEN, ':')) != NULL) {
  77. c++;
  78. len = c - name;
  79. memcpy(dot_name, name, len);
  80. } else
  81. c = name;
  82. if (*c != '\0' && *c != '.') {
  83. dot_name[len++] = '.';
  84. dot_appended = true;
  85. }
  86. ret = strscpy(dot_name + len, c, KSYM_NAME_LEN);
  87. if (ret > 0)
  88. addr = (kprobe_opcode_t *)kallsyms_lookup_name(dot_name);
  89. /* Fallback to the original non-dot symbol lookup */
  90. if (!addr && dot_appended)
  91. addr = (kprobe_opcode_t *)kallsyms_lookup_name(name);
  92. #else
  93. addr = (kprobe_opcode_t *)kallsyms_lookup_name(name);
  94. #endif
  95. return addr;
  96. }
  97. static bool arch_kprobe_on_func_entry(unsigned long offset)
  98. {
  99. #ifdef CONFIG_PPC64_ELF_ABI_V2
  100. #ifdef CONFIG_KPROBES_ON_FTRACE
  101. return offset <= 16;
  102. #else
  103. return offset <= 8;
  104. #endif
  105. #else
  106. return !offset;
  107. #endif
  108. }
  109. /* XXX try and fold the magic of kprobe_lookup_name() in this */
  110. kprobe_opcode_t *arch_adjust_kprobe_addr(unsigned long addr, unsigned long offset,
  111. bool *on_func_entry)
  112. {
  113. *on_func_entry = arch_kprobe_on_func_entry(offset);
  114. return (kprobe_opcode_t *)(addr + offset);
  115. }
  116. void *alloc_insn_page(void)
  117. {
  118. void *page;
  119. page = module_alloc(PAGE_SIZE);
  120. if (!page)
  121. return NULL;
  122. if (strict_module_rwx_enabled()) {
  123. set_memory_ro((unsigned long)page, 1);
  124. set_memory_x((unsigned long)page, 1);
  125. }
  126. return page;
  127. }
  128. int arch_prepare_kprobe(struct kprobe *p)
  129. {
  130. int ret = 0;
  131. struct kprobe *prev;
  132. ppc_inst_t insn = ppc_inst_read(p->addr);
  133. if ((unsigned long)p->addr & 0x03) {
  134. printk("Attempt to register kprobe at an unaligned address\n");
  135. ret = -EINVAL;
  136. } else if (!can_single_step(ppc_inst_val(insn))) {
  137. printk("Cannot register a kprobe on instructions that can't be single stepped\n");
  138. ret = -EINVAL;
  139. } else if ((unsigned long)p->addr & ~PAGE_MASK &&
  140. ppc_inst_prefixed(ppc_inst_read(p->addr - 1))) {
  141. printk("Cannot register a kprobe on the second word of prefixed instruction\n");
  142. ret = -EINVAL;
  143. }
  144. preempt_disable();
  145. prev = get_kprobe(p->addr - 1);
  146. preempt_enable_no_resched();
  147. /*
  148. * When prev is a ftrace-based kprobe, we don't have an insn, and it
  149. * doesn't probe for prefixed instruction.
  150. */
  151. if (prev && !kprobe_ftrace(prev) &&
  152. ppc_inst_prefixed(ppc_inst_read(prev->ainsn.insn))) {
  153. printk("Cannot register a kprobe on the second word of prefixed instruction\n");
  154. ret = -EINVAL;
  155. }
  156. /* insn must be on a special executable page on ppc64. This is
  157. * not explicitly required on ppc32 (right now), but it doesn't hurt */
  158. if (!ret) {
  159. p->ainsn.insn = get_insn_slot();
  160. if (!p->ainsn.insn)
  161. ret = -ENOMEM;
  162. }
  163. if (!ret) {
  164. patch_instruction(p->ainsn.insn, insn);
  165. p->opcode = ppc_inst_val(insn);
  166. }
  167. p->ainsn.boostable = 0;
  168. return ret;
  169. }
  170. NOKPROBE_SYMBOL(arch_prepare_kprobe);
  171. void arch_arm_kprobe(struct kprobe *p)
  172. {
  173. WARN_ON_ONCE(patch_instruction(p->addr, ppc_inst(BREAKPOINT_INSTRUCTION)));
  174. }
  175. NOKPROBE_SYMBOL(arch_arm_kprobe);
  176. void arch_disarm_kprobe(struct kprobe *p)
  177. {
  178. WARN_ON_ONCE(patch_instruction(p->addr, ppc_inst(p->opcode)));
  179. }
  180. NOKPROBE_SYMBOL(arch_disarm_kprobe);
  181. void arch_remove_kprobe(struct kprobe *p)
  182. {
  183. if (p->ainsn.insn) {
  184. free_insn_slot(p->ainsn.insn, 0);
  185. p->ainsn.insn = NULL;
  186. }
  187. }
  188. NOKPROBE_SYMBOL(arch_remove_kprobe);
  189. static nokprobe_inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  190. {
  191. enable_single_step(regs);
  192. /*
  193. * On powerpc we should single step on the original
  194. * instruction even if the probed insn is a trap
  195. * variant as values in regs could play a part in
  196. * if the trap is taken or not
  197. */
  198. regs_set_return_ip(regs, (unsigned long)p->ainsn.insn);
  199. }
  200. static nokprobe_inline void save_previous_kprobe(struct kprobe_ctlblk *kcb)
  201. {
  202. kcb->prev_kprobe.kp = kprobe_running();
  203. kcb->prev_kprobe.status = kcb->kprobe_status;
  204. kcb->prev_kprobe.saved_msr = kcb->kprobe_saved_msr;
  205. }
  206. static nokprobe_inline void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  207. {
  208. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  209. kcb->kprobe_status = kcb->prev_kprobe.status;
  210. kcb->kprobe_saved_msr = kcb->prev_kprobe.saved_msr;
  211. }
  212. static nokprobe_inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  213. struct kprobe_ctlblk *kcb)
  214. {
  215. __this_cpu_write(current_kprobe, p);
  216. kcb->kprobe_saved_msr = regs->msr;
  217. }
  218. void arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs)
  219. {
  220. ri->ret_addr = (kprobe_opcode_t *)regs->link;
  221. ri->fp = NULL;
  222. /* Replace the return addr with trampoline addr */
  223. regs->link = (unsigned long)__kretprobe_trampoline;
  224. }
  225. NOKPROBE_SYMBOL(arch_prepare_kretprobe);
  226. static int try_to_emulate(struct kprobe *p, struct pt_regs *regs)
  227. {
  228. int ret;
  229. ppc_inst_t insn = ppc_inst_read(p->ainsn.insn);
  230. /* regs->nip is also adjusted if emulate_step returns 1 */
  231. ret = emulate_step(regs, insn);
  232. if (ret > 0) {
  233. /*
  234. * Once this instruction has been boosted
  235. * successfully, set the boostable flag
  236. */
  237. if (unlikely(p->ainsn.boostable == 0))
  238. p->ainsn.boostable = 1;
  239. } else if (ret < 0) {
  240. /*
  241. * We don't allow kprobes on mtmsr(d)/rfi(d), etc.
  242. * So, we should never get here... but, its still
  243. * good to catch them, just in case...
  244. */
  245. printk("Can't step on instruction %08lx\n", ppc_inst_as_ulong(insn));
  246. BUG();
  247. } else {
  248. /*
  249. * If we haven't previously emulated this instruction, then it
  250. * can't be boosted. Note it down so we don't try to do so again.
  251. *
  252. * If, however, we had emulated this instruction in the past,
  253. * then this is just an error with the current run (for
  254. * instance, exceptions due to a load/store). We return 0 so
  255. * that this is now single-stepped, but continue to try
  256. * emulating it in subsequent probe hits.
  257. */
  258. if (unlikely(p->ainsn.boostable != 1))
  259. p->ainsn.boostable = -1;
  260. }
  261. return ret;
  262. }
  263. NOKPROBE_SYMBOL(try_to_emulate);
  264. int kprobe_handler(struct pt_regs *regs)
  265. {
  266. struct kprobe *p;
  267. int ret = 0;
  268. unsigned int *addr = (unsigned int *)regs->nip;
  269. struct kprobe_ctlblk *kcb;
  270. if (user_mode(regs))
  271. return 0;
  272. if (!IS_ENABLED(CONFIG_BOOKE) &&
  273. (!(regs->msr & MSR_IR) || !(regs->msr & MSR_DR)))
  274. return 0;
  275. /*
  276. * We don't want to be preempted for the entire
  277. * duration of kprobe processing
  278. */
  279. preempt_disable();
  280. kcb = get_kprobe_ctlblk();
  281. p = get_kprobe(addr);
  282. if (!p) {
  283. unsigned int instr;
  284. if (get_kernel_nofault(instr, addr))
  285. goto no_kprobe;
  286. if (instr != BREAKPOINT_INSTRUCTION) {
  287. /*
  288. * PowerPC has multiple variants of the "trap"
  289. * instruction. If the current instruction is a
  290. * trap variant, it could belong to someone else
  291. */
  292. if (is_trap(instr))
  293. goto no_kprobe;
  294. /*
  295. * The breakpoint instruction was removed right
  296. * after we hit it. Another cpu has removed
  297. * either a probepoint or a debugger breakpoint
  298. * at this address. In either case, no further
  299. * handling of this interrupt is appropriate.
  300. */
  301. ret = 1;
  302. }
  303. /* Not one of ours: let kernel handle it */
  304. goto no_kprobe;
  305. }
  306. /* Check we're not actually recursing */
  307. if (kprobe_running()) {
  308. kprobe_opcode_t insn = *p->ainsn.insn;
  309. if (kcb->kprobe_status == KPROBE_HIT_SS && is_trap(insn)) {
  310. /* Turn off 'trace' bits */
  311. regs_set_return_msr(regs,
  312. (regs->msr & ~MSR_SINGLESTEP) |
  313. kcb->kprobe_saved_msr);
  314. goto no_kprobe;
  315. }
  316. /*
  317. * We have reentered the kprobe_handler(), since another probe
  318. * was hit while within the handler. We here save the original
  319. * kprobes variables and just single step on the instruction of
  320. * the new probe without calling any user handlers.
  321. */
  322. save_previous_kprobe(kcb);
  323. set_current_kprobe(p, regs, kcb);
  324. kprobes_inc_nmissed_count(p);
  325. kcb->kprobe_status = KPROBE_REENTER;
  326. if (p->ainsn.boostable >= 0) {
  327. ret = try_to_emulate(p, regs);
  328. if (ret > 0) {
  329. restore_previous_kprobe(kcb);
  330. preempt_enable_no_resched();
  331. return 1;
  332. }
  333. }
  334. prepare_singlestep(p, regs);
  335. return 1;
  336. }
  337. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  338. set_current_kprobe(p, regs, kcb);
  339. if (p->pre_handler && p->pre_handler(p, regs)) {
  340. /* handler changed execution path, so skip ss setup */
  341. reset_current_kprobe();
  342. preempt_enable_no_resched();
  343. return 1;
  344. }
  345. if (p->ainsn.boostable >= 0) {
  346. ret = try_to_emulate(p, regs);
  347. if (ret > 0) {
  348. if (p->post_handler)
  349. p->post_handler(p, regs, 0);
  350. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  351. reset_current_kprobe();
  352. preempt_enable_no_resched();
  353. return 1;
  354. }
  355. }
  356. prepare_singlestep(p, regs);
  357. kcb->kprobe_status = KPROBE_HIT_SS;
  358. return 1;
  359. no_kprobe:
  360. preempt_enable_no_resched();
  361. return ret;
  362. }
  363. NOKPROBE_SYMBOL(kprobe_handler);
  364. /*
  365. * Function return probe trampoline:
  366. * - init_kprobes() establishes a probepoint here
  367. * - When the probed function returns, this probe
  368. * causes the handlers to fire
  369. */
  370. asm(".global __kretprobe_trampoline\n"
  371. ".type __kretprobe_trampoline, @function\n"
  372. "__kretprobe_trampoline:\n"
  373. "nop\n"
  374. "blr\n"
  375. ".size __kretprobe_trampoline, .-__kretprobe_trampoline\n");
  376. /*
  377. * Called when the probe at kretprobe trampoline is hit
  378. */
  379. static int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  380. {
  381. unsigned long orig_ret_address;
  382. orig_ret_address = __kretprobe_trampoline_handler(regs, NULL);
  383. /*
  384. * We get here through one of two paths:
  385. * 1. by taking a trap -> kprobe_handler() -> here
  386. * 2. by optprobe branch -> optimized_callback() -> opt_pre_handler() -> here
  387. *
  388. * When going back through (1), we need regs->nip to be setup properly
  389. * as it is used to determine the return address from the trap.
  390. * For (2), since nip is not honoured with optprobes, we instead setup
  391. * the link register properly so that the subsequent 'blr' in
  392. * __kretprobe_trampoline jumps back to the right instruction.
  393. *
  394. * For nip, we should set the address to the previous instruction since
  395. * we end up emulating it in kprobe_handler(), which increments the nip
  396. * again.
  397. */
  398. regs_set_return_ip(regs, orig_ret_address - 4);
  399. regs->link = orig_ret_address;
  400. return 0;
  401. }
  402. NOKPROBE_SYMBOL(trampoline_probe_handler);
  403. /*
  404. * Called after single-stepping. p->addr is the address of the
  405. * instruction whose first byte has been replaced by the "breakpoint"
  406. * instruction. To avoid the SMP problems that can occur when we
  407. * temporarily put back the original opcode to single-step, we
  408. * single-stepped a copy of the instruction. The address of this
  409. * copy is p->ainsn.insn.
  410. */
  411. int kprobe_post_handler(struct pt_regs *regs)
  412. {
  413. int len;
  414. struct kprobe *cur = kprobe_running();
  415. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  416. if (!cur || user_mode(regs))
  417. return 0;
  418. len = ppc_inst_len(ppc_inst_read(cur->ainsn.insn));
  419. /* make sure we got here for instruction we have a kprobe on */
  420. if (((unsigned long)cur->ainsn.insn + len) != regs->nip)
  421. return 0;
  422. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  423. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  424. cur->post_handler(cur, regs, 0);
  425. }
  426. /* Adjust nip to after the single-stepped instruction */
  427. regs_set_return_ip(regs, (unsigned long)cur->addr + len);
  428. regs_set_return_msr(regs, regs->msr | kcb->kprobe_saved_msr);
  429. /*Restore back the original saved kprobes variables and continue. */
  430. if (kcb->kprobe_status == KPROBE_REENTER) {
  431. restore_previous_kprobe(kcb);
  432. goto out;
  433. }
  434. reset_current_kprobe();
  435. out:
  436. preempt_enable_no_resched();
  437. /*
  438. * if somebody else is singlestepping across a probe point, msr
  439. * will have DE/SE set, in which case, continue the remaining processing
  440. * of do_debug, as if this is not a probe hit.
  441. */
  442. if (regs->msr & MSR_SINGLESTEP)
  443. return 0;
  444. return 1;
  445. }
  446. NOKPROBE_SYMBOL(kprobe_post_handler);
  447. int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  448. {
  449. struct kprobe *cur = kprobe_running();
  450. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  451. const struct exception_table_entry *entry;
  452. switch(kcb->kprobe_status) {
  453. case KPROBE_HIT_SS:
  454. case KPROBE_REENTER:
  455. /*
  456. * We are here because the instruction being single
  457. * stepped caused a page fault. We reset the current
  458. * kprobe and the nip points back to the probe address
  459. * and allow the page fault handler to continue as a
  460. * normal page fault.
  461. */
  462. regs_set_return_ip(regs, (unsigned long)cur->addr);
  463. /* Turn off 'trace' bits */
  464. regs_set_return_msr(regs,
  465. (regs->msr & ~MSR_SINGLESTEP) |
  466. kcb->kprobe_saved_msr);
  467. if (kcb->kprobe_status == KPROBE_REENTER)
  468. restore_previous_kprobe(kcb);
  469. else
  470. reset_current_kprobe();
  471. preempt_enable_no_resched();
  472. break;
  473. case KPROBE_HIT_ACTIVE:
  474. case KPROBE_HIT_SSDONE:
  475. /*
  476. * In case the user-specified fault handler returned
  477. * zero, try to fix up.
  478. */
  479. if ((entry = search_exception_tables(regs->nip)) != NULL) {
  480. regs_set_return_ip(regs, extable_fixup(entry));
  481. return 1;
  482. }
  483. /*
  484. * fixup_exception() could not handle it,
  485. * Let do_page_fault() fix it.
  486. */
  487. break;
  488. default:
  489. break;
  490. }
  491. return 0;
  492. }
  493. NOKPROBE_SYMBOL(kprobe_fault_handler);
  494. static struct kprobe trampoline_p = {
  495. .addr = (kprobe_opcode_t *) &__kretprobe_trampoline,
  496. .pre_handler = trampoline_probe_handler
  497. };
  498. int __init arch_init_kprobes(void)
  499. {
  500. return register_kprobe(&trampoline_p);
  501. }
  502. int arch_trampoline_kprobe(struct kprobe *p)
  503. {
  504. if (p->addr == (kprobe_opcode_t *)&__kretprobe_trampoline)
  505. return 1;
  506. return 0;
  507. }
  508. NOKPROBE_SYMBOL(arch_trampoline_kprobe);