kprobes.c 14 KB

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