kprobes.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 <vamsi_krishna@in.ibm.com> Kernel
  8. * Probes initial implementation ( includes contributions from
  9. * Rusty Russell).
  10. * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
  11. * interface to access function arguments.
  12. * 2004-Nov Ananth N Mavinakayanahalli <ananth@in.ibm.com> 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/set_memory.h>
  22. #include <linux/execmem.h>
  23. #include <asm/code-patching.h>
  24. #include <asm/cacheflush.h>
  25. #include <asm/sstep.h>
  26. #include <asm/sections.h>
  27. #include <asm/inst.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. int arch_prepare_kprobe(struct kprobe *p)
  117. {
  118. int ret = 0;
  119. struct kprobe *prev;
  120. ppc_inst_t insn = ppc_inst_read(p->addr);
  121. if ((unsigned long)p->addr & 0x03) {
  122. printk("Attempt to register kprobe at an unaligned address\n");
  123. ret = -EINVAL;
  124. } else if (!can_single_step(ppc_inst_val(insn))) {
  125. printk("Cannot register a kprobe on instructions that can't be single stepped\n");
  126. ret = -EINVAL;
  127. } else if ((unsigned long)p->addr & ~PAGE_MASK &&
  128. ppc_inst_prefixed(ppc_inst_read(p->addr - 1))) {
  129. printk("Cannot register a kprobe on the second word of prefixed instruction\n");
  130. ret = -EINVAL;
  131. }
  132. prev = get_kprobe(p->addr - 1);
  133. /*
  134. * When prev is a ftrace-based kprobe, we don't have an insn, and it
  135. * doesn't probe for prefixed instruction.
  136. */
  137. if (prev && !kprobe_ftrace(prev) &&
  138. ppc_inst_prefixed(ppc_inst_read(prev->ainsn.insn))) {
  139. printk("Cannot register a kprobe on the second word of prefixed instruction\n");
  140. ret = -EINVAL;
  141. }
  142. /* insn must be on a special executable page on ppc64. This is
  143. * not explicitly required on ppc32 (right now), but it doesn't hurt */
  144. if (!ret) {
  145. p->ainsn.insn = get_insn_slot();
  146. if (!p->ainsn.insn)
  147. ret = -ENOMEM;
  148. }
  149. if (!ret) {
  150. patch_instruction(p->ainsn.insn, insn);
  151. p->opcode = ppc_inst_val(insn);
  152. }
  153. p->ainsn.boostable = 0;
  154. return ret;
  155. }
  156. NOKPROBE_SYMBOL(arch_prepare_kprobe);
  157. void arch_arm_kprobe(struct kprobe *p)
  158. {
  159. WARN_ON_ONCE(patch_instruction(p->addr, ppc_inst(BREAKPOINT_INSTRUCTION)));
  160. }
  161. NOKPROBE_SYMBOL(arch_arm_kprobe);
  162. void arch_disarm_kprobe(struct kprobe *p)
  163. {
  164. WARN_ON_ONCE(patch_instruction(p->addr, ppc_inst(p->opcode)));
  165. }
  166. NOKPROBE_SYMBOL(arch_disarm_kprobe);
  167. void 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. NOKPROBE_SYMBOL(arch_remove_kprobe);
  175. static nokprobe_inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  176. {
  177. enable_single_step(regs);
  178. /*
  179. * On powerpc we should single step on the original
  180. * instruction even if the probed insn is a trap
  181. * variant as values in regs could play a part in
  182. * if the trap is taken or not
  183. */
  184. regs_set_return_ip(regs, (unsigned long)p->ainsn.insn);
  185. }
  186. static nokprobe_inline void save_previous_kprobe(struct kprobe_ctlblk *kcb)
  187. {
  188. kcb->prev_kprobe.kp = kprobe_running();
  189. kcb->prev_kprobe.status = kcb->kprobe_status;
  190. kcb->prev_kprobe.saved_msr = kcb->kprobe_saved_msr;
  191. }
  192. static nokprobe_inline void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  193. {
  194. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  195. kcb->kprobe_status = kcb->prev_kprobe.status;
  196. kcb->kprobe_saved_msr = kcb->prev_kprobe.saved_msr;
  197. }
  198. static nokprobe_inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  199. struct kprobe_ctlblk *kcb)
  200. {
  201. __this_cpu_write(current_kprobe, p);
  202. kcb->kprobe_saved_msr = regs->msr;
  203. }
  204. static int try_to_emulate(struct kprobe *p, struct pt_regs *regs)
  205. {
  206. int ret;
  207. ppc_inst_t insn = ppc_inst_read(p->ainsn.insn);
  208. /* regs->nip is also adjusted if emulate_step returns 1 */
  209. ret = emulate_step(regs, insn);
  210. if (ret > 0) {
  211. /*
  212. * Once this instruction has been boosted
  213. * successfully, set the boostable flag
  214. */
  215. if (unlikely(p->ainsn.boostable == 0))
  216. p->ainsn.boostable = 1;
  217. } else if (ret < 0) {
  218. /*
  219. * We don't allow kprobes on mtmsr(d)/rfi(d), etc.
  220. * So, we should never get here... but, its still
  221. * good to catch them, just in case...
  222. */
  223. printk("Can't step on instruction %08lx\n", ppc_inst_as_ulong(insn));
  224. BUG();
  225. } else {
  226. /*
  227. * If we haven't previously emulated this instruction, then it
  228. * can't be boosted. Note it down so we don't try to do so again.
  229. *
  230. * If, however, we had emulated this instruction in the past,
  231. * then this is just an error with the current run (for
  232. * instance, exceptions due to a load/store). We return 0 so
  233. * that this is now single-stepped, but continue to try
  234. * emulating it in subsequent probe hits.
  235. */
  236. if (unlikely(p->ainsn.boostable != 1))
  237. p->ainsn.boostable = -1;
  238. }
  239. return ret;
  240. }
  241. NOKPROBE_SYMBOL(try_to_emulate);
  242. int kprobe_handler(struct pt_regs *regs)
  243. {
  244. struct kprobe *p;
  245. int ret = 0;
  246. unsigned int *addr = (unsigned int *)regs->nip;
  247. struct kprobe_ctlblk *kcb;
  248. if (user_mode(regs))
  249. return 0;
  250. if (!IS_ENABLED(CONFIG_BOOKE) &&
  251. (!(regs->msr & MSR_IR) || !(regs->msr & MSR_DR)))
  252. return 0;
  253. /*
  254. * We don't want to be preempted for the entire
  255. * duration of kprobe processing
  256. */
  257. preempt_disable();
  258. kcb = get_kprobe_ctlblk();
  259. p = get_kprobe(addr);
  260. if (!p) {
  261. unsigned int instr;
  262. if (get_kernel_nofault(instr, addr))
  263. goto no_kprobe;
  264. if (instr != BREAKPOINT_INSTRUCTION) {
  265. /*
  266. * PowerPC has multiple variants of the "trap"
  267. * instruction. If the current instruction is a
  268. * trap variant, it could belong to someone else
  269. */
  270. if (is_trap(instr))
  271. goto no_kprobe;
  272. /*
  273. * The breakpoint instruction was removed right
  274. * after we hit it. Another cpu has removed
  275. * either a probepoint or a debugger breakpoint
  276. * at this address. In either case, no further
  277. * handling of this interrupt is appropriate.
  278. */
  279. ret = 1;
  280. }
  281. /* Not one of ours: let kernel handle it */
  282. goto no_kprobe;
  283. }
  284. /* Check we're not actually recursing */
  285. if (kprobe_running()) {
  286. kprobe_opcode_t insn = *p->ainsn.insn;
  287. if (kcb->kprobe_status == KPROBE_HIT_SS && is_trap(insn)) {
  288. /* Turn off 'trace' bits */
  289. regs_set_return_msr(regs,
  290. (regs->msr & ~MSR_SINGLESTEP) |
  291. kcb->kprobe_saved_msr);
  292. goto no_kprobe;
  293. }
  294. /*
  295. * We have reentered the kprobe_handler(), since another probe
  296. * was hit while within the handler. We here save the original
  297. * kprobes variables and just single step on the instruction of
  298. * the new probe without calling any user handlers.
  299. */
  300. save_previous_kprobe(kcb);
  301. set_current_kprobe(p, regs, kcb);
  302. kprobes_inc_nmissed_count(p);
  303. kcb->kprobe_status = KPROBE_REENTER;
  304. if (p->ainsn.boostable >= 0) {
  305. ret = try_to_emulate(p, regs);
  306. if (ret > 0) {
  307. restore_previous_kprobe(kcb);
  308. preempt_enable();
  309. return 1;
  310. }
  311. }
  312. prepare_singlestep(p, regs);
  313. return 1;
  314. }
  315. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  316. set_current_kprobe(p, regs, kcb);
  317. if (p->pre_handler && p->pre_handler(p, regs)) {
  318. /* handler changed execution path, so skip ss setup */
  319. reset_current_kprobe();
  320. preempt_enable();
  321. return 1;
  322. }
  323. if (p->ainsn.boostable >= 0) {
  324. ret = try_to_emulate(p, regs);
  325. if (ret > 0) {
  326. if (p->post_handler)
  327. p->post_handler(p, regs, 0);
  328. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  329. reset_current_kprobe();
  330. preempt_enable();
  331. return 1;
  332. }
  333. }
  334. prepare_singlestep(p, regs);
  335. kcb->kprobe_status = KPROBE_HIT_SS;
  336. return 1;
  337. no_kprobe:
  338. preempt_enable();
  339. return ret;
  340. }
  341. NOKPROBE_SYMBOL(kprobe_handler);
  342. /*
  343. * Called after single-stepping. p->addr is the address of the
  344. * instruction whose first byte has been replaced by the "breakpoint"
  345. * instruction. To avoid the SMP problems that can occur when we
  346. * temporarily put back the original opcode to single-step, we
  347. * single-stepped a copy of the instruction. The address of this
  348. * copy is p->ainsn.insn.
  349. */
  350. int kprobe_post_handler(struct pt_regs *regs)
  351. {
  352. int len;
  353. struct kprobe *cur = kprobe_running();
  354. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  355. if (!cur || user_mode(regs))
  356. return 0;
  357. len = ppc_inst_len(ppc_inst_read(cur->ainsn.insn));
  358. /* make sure we got here for instruction we have a kprobe on */
  359. if (((unsigned long)cur->ainsn.insn + len) != regs->nip)
  360. return 0;
  361. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  362. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  363. cur->post_handler(cur, regs, 0);
  364. }
  365. /* Adjust nip to after the single-stepped instruction */
  366. regs_set_return_ip(regs, (unsigned long)cur->addr + len);
  367. regs_set_return_msr(regs, regs->msr | kcb->kprobe_saved_msr);
  368. /*Restore back the original saved kprobes variables and continue. */
  369. if (kcb->kprobe_status == KPROBE_REENTER) {
  370. restore_previous_kprobe(kcb);
  371. goto out;
  372. }
  373. reset_current_kprobe();
  374. out:
  375. preempt_enable();
  376. /*
  377. * if somebody else is singlestepping across a probe point, msr
  378. * will have DE/SE set, in which case, continue the remaining processing
  379. * of do_debug, as if this is not a probe hit.
  380. */
  381. if (regs->msr & MSR_SINGLESTEP)
  382. return 0;
  383. return 1;
  384. }
  385. NOKPROBE_SYMBOL(kprobe_post_handler);
  386. int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  387. {
  388. struct kprobe *cur = kprobe_running();
  389. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  390. const struct exception_table_entry *entry;
  391. switch(kcb->kprobe_status) {
  392. case KPROBE_HIT_SS:
  393. case KPROBE_REENTER:
  394. /*
  395. * We are here because the instruction being single
  396. * stepped caused a page fault. We reset the current
  397. * kprobe and the nip points back to the probe address
  398. * and allow the page fault handler to continue as a
  399. * normal page fault.
  400. */
  401. regs_set_return_ip(regs, (unsigned long)cur->addr);
  402. /* Turn off 'trace' bits */
  403. regs_set_return_msr(regs,
  404. (regs->msr & ~MSR_SINGLESTEP) |
  405. kcb->kprobe_saved_msr);
  406. if (kcb->kprobe_status == KPROBE_REENTER)
  407. restore_previous_kprobe(kcb);
  408. else
  409. reset_current_kprobe();
  410. preempt_enable();
  411. break;
  412. case KPROBE_HIT_ACTIVE:
  413. case KPROBE_HIT_SSDONE:
  414. /*
  415. * In case the user-specified fault handler returned
  416. * zero, try to fix up.
  417. */
  418. if ((entry = search_exception_tables(regs->nip)) != NULL) {
  419. regs_set_return_ip(regs, extable_fixup(entry));
  420. return 1;
  421. }
  422. /*
  423. * fixup_exception() could not handle it,
  424. * Let do_page_fault() fix it.
  425. */
  426. break;
  427. default:
  428. break;
  429. }
  430. return 0;
  431. }
  432. NOKPROBE_SYMBOL(kprobe_fault_handler);
  433. int arch_trampoline_kprobe(struct kprobe *p)
  434. {
  435. if (p->addr == (kprobe_opcode_t *)&arch_rethook_trampoline)
  436. return 1;
  437. return 0;
  438. }
  439. NOKPROBE_SYMBOL(arch_trampoline_kprobe);