kprobes.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Kernel Probes (KProbes)
  4. *
  5. * Copyright IBM Corp. 2002, 2006
  6. *
  7. * s390 port, used ppc64 as template. Mike Grundy <grundym@us.ibm.com>
  8. */
  9. #define pr_fmt(fmt) "kprobes: " fmt
  10. #include <linux/kprobes.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/preempt.h>
  13. #include <linux/stop_machine.h>
  14. #include <linux/kdebug.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/extable.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/hardirq.h>
  20. #include <linux/ftrace.h>
  21. #include <linux/execmem.h>
  22. #include <asm/text-patching.h>
  23. #include <asm/set_memory.h>
  24. #include <asm/sections.h>
  25. #include <asm/dis.h>
  26. #include "entry.h"
  27. DEFINE_PER_CPU(struct kprobe *, current_kprobe);
  28. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  29. struct kretprobe_blackpoint kretprobe_blacklist[] = { };
  30. void *alloc_insn_page(void)
  31. {
  32. void *page;
  33. page = execmem_alloc(EXECMEM_KPROBES, PAGE_SIZE);
  34. if (!page)
  35. return NULL;
  36. set_memory_rox((unsigned long)page, 1);
  37. return page;
  38. }
  39. static void copy_instruction(struct kprobe *p)
  40. {
  41. kprobe_opcode_t insn[MAX_INSN_SIZE];
  42. s64 disp, new_disp;
  43. u64 addr, new_addr;
  44. unsigned int len;
  45. len = insn_length(*p->addr >> 8);
  46. memcpy(&insn, p->addr, len);
  47. p->opcode = insn[0];
  48. if (probe_is_insn_relative_long(&insn[0])) {
  49. /*
  50. * For pc-relative instructions in RIL-b or RIL-c format patch
  51. * the RI2 displacement field. The insn slot for the to be
  52. * patched instruction is within the same 4GB area like the
  53. * original instruction. Therefore the new displacement will
  54. * always fit.
  55. */
  56. disp = *(s32 *)&insn[1];
  57. addr = (u64)(unsigned long)p->addr;
  58. new_addr = (u64)(unsigned long)p->ainsn.insn;
  59. new_disp = ((addr + (disp * 2)) - new_addr) / 2;
  60. *(s32 *)&insn[1] = new_disp;
  61. }
  62. s390_kernel_write(p->ainsn.insn, &insn, len);
  63. }
  64. NOKPROBE_SYMBOL(copy_instruction);
  65. /* Check if paddr is at an instruction boundary */
  66. static bool can_probe(unsigned long paddr)
  67. {
  68. unsigned long addr, offset = 0;
  69. kprobe_opcode_t insn;
  70. struct kprobe *kp;
  71. if (paddr & 0x01)
  72. return false;
  73. if (!kallsyms_lookup_size_offset(paddr, NULL, &offset))
  74. return false;
  75. /* Decode instructions */
  76. addr = paddr - offset;
  77. while (addr < paddr) {
  78. if (copy_from_kernel_nofault(&insn, (void *)addr, sizeof(insn)))
  79. return false;
  80. if (insn >> 8 == 0) {
  81. if (insn != BREAKPOINT_INSTRUCTION) {
  82. /*
  83. * Note that QEMU inserts opcode 0x0000 to implement
  84. * software breakpoints for guests. Since the size of
  85. * the original instruction is unknown, stop following
  86. * instructions and prevent setting a kprobe.
  87. */
  88. return false;
  89. }
  90. /*
  91. * Check if the instruction has been modified by another
  92. * kprobe, in which case the original instruction is
  93. * decoded.
  94. */
  95. kp = get_kprobe((void *)addr);
  96. if (!kp) {
  97. /* not a kprobe */
  98. return false;
  99. }
  100. insn = kp->opcode;
  101. }
  102. addr += insn_length(insn >> 8);
  103. }
  104. return addr == paddr;
  105. }
  106. int arch_prepare_kprobe(struct kprobe *p)
  107. {
  108. if (!can_probe((unsigned long)p->addr))
  109. return -EINVAL;
  110. /* Make sure the probe isn't going on a difficult instruction */
  111. if (probe_is_prohibited_opcode(p->addr))
  112. return -EINVAL;
  113. p->ainsn.insn = get_insn_slot();
  114. if (!p->ainsn.insn)
  115. return -ENOMEM;
  116. copy_instruction(p);
  117. return 0;
  118. }
  119. NOKPROBE_SYMBOL(arch_prepare_kprobe);
  120. struct swap_insn_args {
  121. struct kprobe *p;
  122. unsigned int arm_kprobe : 1;
  123. };
  124. static int swap_instruction(void *data)
  125. {
  126. struct swap_insn_args *args = data;
  127. struct kprobe *p = args->p;
  128. u16 opc;
  129. opc = args->arm_kprobe ? BREAKPOINT_INSTRUCTION : p->opcode;
  130. s390_kernel_write(p->addr, &opc, sizeof(opc));
  131. return 0;
  132. }
  133. NOKPROBE_SYMBOL(swap_instruction);
  134. void arch_arm_kprobe(struct kprobe *p)
  135. {
  136. struct swap_insn_args args = {.p = p, .arm_kprobe = 1};
  137. if (MACHINE_HAS_SEQ_INSN) {
  138. swap_instruction(&args);
  139. text_poke_sync();
  140. } else {
  141. stop_machine_cpuslocked(swap_instruction, &args, NULL);
  142. }
  143. }
  144. NOKPROBE_SYMBOL(arch_arm_kprobe);
  145. void arch_disarm_kprobe(struct kprobe *p)
  146. {
  147. struct swap_insn_args args = {.p = p, .arm_kprobe = 0};
  148. if (MACHINE_HAS_SEQ_INSN) {
  149. swap_instruction(&args);
  150. text_poke_sync();
  151. } else {
  152. stop_machine_cpuslocked(swap_instruction, &args, NULL);
  153. }
  154. }
  155. NOKPROBE_SYMBOL(arch_disarm_kprobe);
  156. void arch_remove_kprobe(struct kprobe *p)
  157. {
  158. if (!p->ainsn.insn)
  159. return;
  160. free_insn_slot(p->ainsn.insn, 0);
  161. p->ainsn.insn = NULL;
  162. }
  163. NOKPROBE_SYMBOL(arch_remove_kprobe);
  164. static void enable_singlestep(struct kprobe_ctlblk *kcb,
  165. struct pt_regs *regs,
  166. unsigned long ip)
  167. {
  168. union {
  169. struct ctlreg regs[3];
  170. struct {
  171. struct ctlreg control;
  172. struct ctlreg start;
  173. struct ctlreg end;
  174. };
  175. } per_kprobe;
  176. /* Set up the PER control registers %cr9-%cr11 */
  177. per_kprobe.control.val = PER_EVENT_IFETCH;
  178. per_kprobe.start.val = ip;
  179. per_kprobe.end.val = ip;
  180. /* Save control regs and psw mask */
  181. __local_ctl_store(9, 11, kcb->kprobe_saved_ctl);
  182. kcb->kprobe_saved_imask = regs->psw.mask &
  183. (PSW_MASK_PER | PSW_MASK_IO | PSW_MASK_EXT);
  184. /* Set PER control regs, turns on single step for the given address */
  185. __local_ctl_load(9, 11, per_kprobe.regs);
  186. regs->psw.mask |= PSW_MASK_PER;
  187. regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT);
  188. regs->psw.addr = ip;
  189. }
  190. NOKPROBE_SYMBOL(enable_singlestep);
  191. static void disable_singlestep(struct kprobe_ctlblk *kcb,
  192. struct pt_regs *regs,
  193. unsigned long ip)
  194. {
  195. /* Restore control regs and psw mask, set new psw address */
  196. __local_ctl_load(9, 11, kcb->kprobe_saved_ctl);
  197. regs->psw.mask &= ~PSW_MASK_PER;
  198. regs->psw.mask |= kcb->kprobe_saved_imask;
  199. regs->psw.addr = ip;
  200. }
  201. NOKPROBE_SYMBOL(disable_singlestep);
  202. /*
  203. * Activate a kprobe by storing its pointer to current_kprobe. The
  204. * previous kprobe is stored in kcb->prev_kprobe. A stack of up to
  205. * two kprobes can be active, see KPROBE_REENTER.
  206. */
  207. static void push_kprobe(struct kprobe_ctlblk *kcb, struct kprobe *p)
  208. {
  209. kcb->prev_kprobe.kp = __this_cpu_read(current_kprobe);
  210. kcb->prev_kprobe.status = kcb->kprobe_status;
  211. __this_cpu_write(current_kprobe, p);
  212. }
  213. NOKPROBE_SYMBOL(push_kprobe);
  214. /*
  215. * Deactivate a kprobe by backing up to the previous state. If the
  216. * current state is KPROBE_REENTER prev_kprobe.kp will be non-NULL,
  217. * for any other state prev_kprobe.kp will be NULL.
  218. */
  219. static void pop_kprobe(struct kprobe_ctlblk *kcb)
  220. {
  221. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  222. kcb->kprobe_status = kcb->prev_kprobe.status;
  223. kcb->prev_kprobe.kp = NULL;
  224. }
  225. NOKPROBE_SYMBOL(pop_kprobe);
  226. static void kprobe_reenter_check(struct kprobe_ctlblk *kcb, struct kprobe *p)
  227. {
  228. switch (kcb->kprobe_status) {
  229. case KPROBE_HIT_SSDONE:
  230. case KPROBE_HIT_ACTIVE:
  231. kprobes_inc_nmissed_count(p);
  232. break;
  233. case KPROBE_HIT_SS:
  234. case KPROBE_REENTER:
  235. default:
  236. /*
  237. * A kprobe on the code path to single step an instruction
  238. * is a BUG. The code path resides in the .kprobes.text
  239. * section and is executed with interrupts disabled.
  240. */
  241. pr_err("Failed to recover from reentered kprobes.\n");
  242. dump_kprobe(p);
  243. BUG();
  244. }
  245. }
  246. NOKPROBE_SYMBOL(kprobe_reenter_check);
  247. static int kprobe_handler(struct pt_regs *regs)
  248. {
  249. struct kprobe_ctlblk *kcb;
  250. struct kprobe *p;
  251. /*
  252. * We want to disable preemption for the entire duration of kprobe
  253. * processing. That includes the calls to the pre/post handlers
  254. * and single stepping the kprobe instruction.
  255. */
  256. preempt_disable();
  257. kcb = get_kprobe_ctlblk();
  258. p = get_kprobe((void *)(regs->psw.addr - 2));
  259. if (p) {
  260. if (kprobe_running()) {
  261. /*
  262. * We have hit a kprobe while another is still
  263. * active. This can happen in the pre and post
  264. * handler. Single step the instruction of the
  265. * new probe but do not call any handler function
  266. * of this secondary kprobe.
  267. * push_kprobe and pop_kprobe saves and restores
  268. * the currently active kprobe.
  269. */
  270. kprobe_reenter_check(kcb, p);
  271. push_kprobe(kcb, p);
  272. kcb->kprobe_status = KPROBE_REENTER;
  273. } else {
  274. /*
  275. * If we have no pre-handler or it returned 0, we
  276. * continue with single stepping. If we have a
  277. * pre-handler and it returned non-zero, it prepped
  278. * for changing execution path, so get out doing
  279. * nothing more here.
  280. */
  281. push_kprobe(kcb, p);
  282. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  283. if (p->pre_handler && p->pre_handler(p, regs)) {
  284. pop_kprobe(kcb);
  285. preempt_enable_no_resched();
  286. return 1;
  287. }
  288. kcb->kprobe_status = KPROBE_HIT_SS;
  289. }
  290. enable_singlestep(kcb, regs, (unsigned long) p->ainsn.insn);
  291. return 1;
  292. } /* else:
  293. * No kprobe at this address and no active kprobe. The trap has
  294. * not been caused by a kprobe breakpoint. The race of breakpoint
  295. * vs. kprobe remove does not exist because on s390 as we use
  296. * stop_machine to arm/disarm the breakpoints.
  297. */
  298. preempt_enable_no_resched();
  299. return 0;
  300. }
  301. NOKPROBE_SYMBOL(kprobe_handler);
  302. /*
  303. * Called after single-stepping. p->addr is the address of the
  304. * instruction whose first byte has been replaced by the "breakpoint"
  305. * instruction. To avoid the SMP problems that can occur when we
  306. * temporarily put back the original opcode to single-step, we
  307. * single-stepped a copy of the instruction. The address of this
  308. * copy is p->ainsn.insn.
  309. */
  310. static void resume_execution(struct kprobe *p, struct pt_regs *regs)
  311. {
  312. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  313. unsigned long ip = regs->psw.addr;
  314. int fixup = probe_get_fixup_type(p->ainsn.insn);
  315. if (fixup & FIXUP_PSW_NORMAL)
  316. ip += (unsigned long) p->addr - (unsigned long) p->ainsn.insn;
  317. if (fixup & FIXUP_BRANCH_NOT_TAKEN) {
  318. int ilen = insn_length(p->ainsn.insn[0] >> 8);
  319. if (ip - (unsigned long) p->ainsn.insn == ilen)
  320. ip = (unsigned long) p->addr + ilen;
  321. }
  322. if (fixup & FIXUP_RETURN_REGISTER) {
  323. int reg = (p->ainsn.insn[0] & 0xf0) >> 4;
  324. regs->gprs[reg] += (unsigned long) p->addr -
  325. (unsigned long) p->ainsn.insn;
  326. }
  327. disable_singlestep(kcb, regs, ip);
  328. }
  329. NOKPROBE_SYMBOL(resume_execution);
  330. static int post_kprobe_handler(struct pt_regs *regs)
  331. {
  332. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  333. struct kprobe *p = kprobe_running();
  334. if (!p)
  335. return 0;
  336. resume_execution(p, regs);
  337. if (kcb->kprobe_status != KPROBE_REENTER && p->post_handler) {
  338. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  339. p->post_handler(p, regs, 0);
  340. }
  341. pop_kprobe(kcb);
  342. preempt_enable_no_resched();
  343. /*
  344. * if somebody else is singlestepping across a probe point, psw mask
  345. * will have PER set, in which case, continue the remaining processing
  346. * of do_single_step, as if this is not a probe hit.
  347. */
  348. if (regs->psw.mask & PSW_MASK_PER)
  349. return 0;
  350. return 1;
  351. }
  352. NOKPROBE_SYMBOL(post_kprobe_handler);
  353. static int kprobe_trap_handler(struct pt_regs *regs, int trapnr)
  354. {
  355. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  356. struct kprobe *p = kprobe_running();
  357. switch(kcb->kprobe_status) {
  358. case KPROBE_HIT_SS:
  359. case KPROBE_REENTER:
  360. /*
  361. * We are here because the instruction being single
  362. * stepped caused a page fault. We reset the current
  363. * kprobe and the nip points back to the probe address
  364. * and allow the page fault handler to continue as a
  365. * normal page fault.
  366. */
  367. disable_singlestep(kcb, regs, (unsigned long) p->addr);
  368. pop_kprobe(kcb);
  369. preempt_enable_no_resched();
  370. break;
  371. case KPROBE_HIT_ACTIVE:
  372. case KPROBE_HIT_SSDONE:
  373. /*
  374. * In case the user-specified fault handler returned
  375. * zero, try to fix up.
  376. */
  377. if (fixup_exception(regs))
  378. return 1;
  379. /*
  380. * fixup_exception() could not handle it,
  381. * Let do_page_fault() fix it.
  382. */
  383. break;
  384. default:
  385. break;
  386. }
  387. return 0;
  388. }
  389. NOKPROBE_SYMBOL(kprobe_trap_handler);
  390. int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  391. {
  392. int ret;
  393. if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
  394. local_irq_disable();
  395. ret = kprobe_trap_handler(regs, trapnr);
  396. if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
  397. local_irq_restore(regs->psw.mask & ~PSW_MASK_PER);
  398. return ret;
  399. }
  400. NOKPROBE_SYMBOL(kprobe_fault_handler);
  401. /*
  402. * Wrapper routine to for handling exceptions.
  403. */
  404. int kprobe_exceptions_notify(struct notifier_block *self,
  405. unsigned long val, void *data)
  406. {
  407. struct die_args *args = (struct die_args *) data;
  408. struct pt_regs *regs = args->regs;
  409. int ret = NOTIFY_DONE;
  410. if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
  411. local_irq_disable();
  412. switch (val) {
  413. case DIE_BPT:
  414. if (kprobe_handler(regs))
  415. ret = NOTIFY_STOP;
  416. break;
  417. case DIE_SSTEP:
  418. if (post_kprobe_handler(regs))
  419. ret = NOTIFY_STOP;
  420. break;
  421. case DIE_TRAP:
  422. if (!preemptible() && kprobe_running() &&
  423. kprobe_trap_handler(regs, args->trapnr))
  424. ret = NOTIFY_STOP;
  425. break;
  426. default:
  427. break;
  428. }
  429. if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
  430. local_irq_restore(regs->psw.mask & ~PSW_MASK_PER);
  431. return ret;
  432. }
  433. NOKPROBE_SYMBOL(kprobe_exceptions_notify);
  434. int __init arch_init_kprobes(void)
  435. {
  436. return 0;
  437. }
  438. int __init arch_populate_kprobe_blacklist(void)
  439. {
  440. return kprobe_add_area_blacklist((unsigned long)__irqentry_text_start,
  441. (unsigned long)__irqentry_text_end);
  442. }
  443. int arch_trampoline_kprobe(struct kprobe *p)
  444. {
  445. return 0;
  446. }
  447. NOKPROBE_SYMBOL(arch_trampoline_kprobe);