ftrace.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Dynamic function tracer architecture backend.
  4. *
  5. * Copyright IBM Corp. 2009,2014
  6. *
  7. * Author(s): Martin Schwidefsky <[email protected]>
  8. */
  9. #include <linux/moduleloader.h>
  10. #include <linux/hardirq.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/ftrace.h>
  13. #include <linux/kernel.h>
  14. #include <linux/types.h>
  15. #include <linux/kprobes.h>
  16. #include <trace/syscall.h>
  17. #include <asm/asm-offsets.h>
  18. #include <asm/text-patching.h>
  19. #include <asm/cacheflush.h>
  20. #include <asm/ftrace.lds.h>
  21. #include <asm/nospec-branch.h>
  22. #include <asm/set_memory.h>
  23. #include "entry.h"
  24. #include "ftrace.h"
  25. /*
  26. * To generate function prologue either gcc's hotpatch feature (since gcc 4.8)
  27. * or a combination of -pg -mrecord-mcount -mnop-mcount -mfentry flags
  28. * (since gcc 9 / clang 10) is used.
  29. * In both cases the original and also the disabled function prologue contains
  30. * only a single six byte instruction and looks like this:
  31. * > brcl 0,0 # offset 0
  32. * To enable ftrace the code gets patched like above and afterwards looks
  33. * like this:
  34. * > brasl %r0,ftrace_caller # offset 0
  35. *
  36. * The instruction will be patched by ftrace_make_call / ftrace_make_nop.
  37. * The ftrace function gets called with a non-standard C function call ABI
  38. * where r0 contains the return address. It is also expected that the called
  39. * function only clobbers r0 and r1, but restores r2-r15.
  40. * For module code we can't directly jump to ftrace caller, but need a
  41. * trampoline (ftrace_plt), which clobbers also r1.
  42. */
  43. void *ftrace_func __read_mostly = ftrace_stub;
  44. struct ftrace_insn {
  45. u16 opc;
  46. s32 disp;
  47. } __packed;
  48. asm(
  49. " .align 16\n"
  50. "ftrace_shared_hotpatch_trampoline_br:\n"
  51. " lmg %r0,%r1,2(%r1)\n"
  52. " br %r1\n"
  53. "ftrace_shared_hotpatch_trampoline_br_end:\n"
  54. );
  55. #ifdef CONFIG_EXPOLINE
  56. asm(
  57. " .align 16\n"
  58. "ftrace_shared_hotpatch_trampoline_exrl:\n"
  59. " lmg %r0,%r1,2(%r1)\n"
  60. " exrl %r0,0f\n"
  61. " j .\n"
  62. "0: br %r1\n"
  63. "ftrace_shared_hotpatch_trampoline_exrl_end:\n"
  64. );
  65. #endif /* CONFIG_EXPOLINE */
  66. #ifdef CONFIG_MODULES
  67. static char *ftrace_plt;
  68. #endif /* CONFIG_MODULES */
  69. static const char *ftrace_shared_hotpatch_trampoline(const char **end)
  70. {
  71. const char *tstart, *tend;
  72. tstart = ftrace_shared_hotpatch_trampoline_br;
  73. tend = ftrace_shared_hotpatch_trampoline_br_end;
  74. #ifdef CONFIG_EXPOLINE
  75. if (!nospec_disable) {
  76. tstart = ftrace_shared_hotpatch_trampoline_exrl;
  77. tend = ftrace_shared_hotpatch_trampoline_exrl_end;
  78. }
  79. #endif /* CONFIG_EXPOLINE */
  80. if (end)
  81. *end = tend;
  82. return tstart;
  83. }
  84. bool ftrace_need_init_nop(void)
  85. {
  86. return true;
  87. }
  88. int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
  89. {
  90. static struct ftrace_hotpatch_trampoline *next_vmlinux_trampoline =
  91. __ftrace_hotpatch_trampolines_start;
  92. static const char orig[6] = { 0xc0, 0x04, 0x00, 0x00, 0x00, 0x00 };
  93. static struct ftrace_hotpatch_trampoline *trampoline;
  94. struct ftrace_hotpatch_trampoline **next_trampoline;
  95. struct ftrace_hotpatch_trampoline *trampolines_end;
  96. struct ftrace_hotpatch_trampoline tmp;
  97. struct ftrace_insn *insn;
  98. const char *shared;
  99. s32 disp;
  100. BUILD_BUG_ON(sizeof(struct ftrace_hotpatch_trampoline) !=
  101. SIZEOF_FTRACE_HOTPATCH_TRAMPOLINE);
  102. next_trampoline = &next_vmlinux_trampoline;
  103. trampolines_end = __ftrace_hotpatch_trampolines_end;
  104. shared = ftrace_shared_hotpatch_trampoline(NULL);
  105. #ifdef CONFIG_MODULES
  106. if (mod) {
  107. next_trampoline = &mod->arch.next_trampoline;
  108. trampolines_end = mod->arch.trampolines_end;
  109. shared = ftrace_plt;
  110. }
  111. #endif
  112. if (WARN_ON_ONCE(*next_trampoline >= trampolines_end))
  113. return -ENOMEM;
  114. trampoline = (*next_trampoline)++;
  115. /* Check for the compiler-generated fentry nop (brcl 0, .). */
  116. if (WARN_ON_ONCE(memcmp((const void *)rec->ip, &orig, sizeof(orig))))
  117. return -EINVAL;
  118. /* Generate the trampoline. */
  119. tmp.brasl_opc = 0xc015; /* brasl %r1, shared */
  120. tmp.brasl_disp = (shared - (const char *)&trampoline->brasl_opc) / 2;
  121. tmp.interceptor = FTRACE_ADDR;
  122. tmp.rest_of_intercepted_function = rec->ip + sizeof(struct ftrace_insn);
  123. s390_kernel_write(trampoline, &tmp, sizeof(tmp));
  124. /* Generate a jump to the trampoline. */
  125. disp = ((char *)trampoline - (char *)rec->ip) / 2;
  126. insn = (struct ftrace_insn *)rec->ip;
  127. s390_kernel_write(&insn->disp, &disp, sizeof(disp));
  128. return 0;
  129. }
  130. static struct ftrace_hotpatch_trampoline *ftrace_get_trampoline(struct dyn_ftrace *rec)
  131. {
  132. struct ftrace_hotpatch_trampoline *trampoline;
  133. struct ftrace_insn insn;
  134. s64 disp;
  135. u16 opc;
  136. if (copy_from_kernel_nofault(&insn, (void *)rec->ip, sizeof(insn)))
  137. return ERR_PTR(-EFAULT);
  138. disp = (s64)insn.disp * 2;
  139. trampoline = (void *)(rec->ip + disp);
  140. if (get_kernel_nofault(opc, &trampoline->brasl_opc))
  141. return ERR_PTR(-EFAULT);
  142. if (opc != 0xc015)
  143. return ERR_PTR(-EINVAL);
  144. return trampoline;
  145. }
  146. int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
  147. unsigned long addr)
  148. {
  149. struct ftrace_hotpatch_trampoline *trampoline;
  150. u64 old;
  151. trampoline = ftrace_get_trampoline(rec);
  152. if (IS_ERR(trampoline))
  153. return PTR_ERR(trampoline);
  154. if (get_kernel_nofault(old, &trampoline->interceptor))
  155. return -EFAULT;
  156. if (old != old_addr)
  157. return -EINVAL;
  158. s390_kernel_write(&trampoline->interceptor, &addr, sizeof(addr));
  159. return 0;
  160. }
  161. static int ftrace_patch_branch_mask(void *addr, u16 expected, bool enable)
  162. {
  163. u16 old;
  164. u8 op;
  165. if (get_kernel_nofault(old, addr))
  166. return -EFAULT;
  167. if (old != expected)
  168. return -EINVAL;
  169. /* set mask field to all ones or zeroes */
  170. op = enable ? 0xf4 : 0x04;
  171. s390_kernel_write((char *)addr + 1, &op, sizeof(op));
  172. return 0;
  173. }
  174. int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
  175. unsigned long addr)
  176. {
  177. /* Expect brcl 0xf,... */
  178. return ftrace_patch_branch_mask((void *)rec->ip, 0xc0f4, false);
  179. }
  180. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  181. {
  182. struct ftrace_hotpatch_trampoline *trampoline;
  183. trampoline = ftrace_get_trampoline(rec);
  184. if (IS_ERR(trampoline))
  185. return PTR_ERR(trampoline);
  186. s390_kernel_write(&trampoline->interceptor, &addr, sizeof(addr));
  187. /* Expect brcl 0x0,... */
  188. return ftrace_patch_branch_mask((void *)rec->ip, 0xc004, true);
  189. }
  190. int ftrace_update_ftrace_func(ftrace_func_t func)
  191. {
  192. ftrace_func = func;
  193. return 0;
  194. }
  195. void arch_ftrace_update_code(int command)
  196. {
  197. ftrace_modify_all_code(command);
  198. }
  199. void ftrace_arch_code_modify_post_process(void)
  200. {
  201. /*
  202. * Flush any pre-fetched instructions on all
  203. * CPUs to make the new code visible.
  204. */
  205. text_poke_sync_lock();
  206. }
  207. #ifdef CONFIG_MODULES
  208. static int __init ftrace_plt_init(void)
  209. {
  210. const char *start, *end;
  211. ftrace_plt = module_alloc(PAGE_SIZE);
  212. if (!ftrace_plt)
  213. panic("cannot allocate ftrace plt\n");
  214. start = ftrace_shared_hotpatch_trampoline(&end);
  215. memcpy(ftrace_plt, start, end - start);
  216. set_memory_ro((unsigned long)ftrace_plt, 1);
  217. return 0;
  218. }
  219. device_initcall(ftrace_plt_init);
  220. #endif /* CONFIG_MODULES */
  221. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  222. /*
  223. * Hook the return address and push it in the stack of return addresses
  224. * in current thread info.
  225. */
  226. unsigned long prepare_ftrace_return(unsigned long ra, unsigned long sp,
  227. unsigned long ip)
  228. {
  229. if (unlikely(ftrace_graph_is_dead()))
  230. goto out;
  231. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  232. goto out;
  233. ip -= MCOUNT_INSN_SIZE;
  234. if (!function_graph_enter(ra, ip, 0, (void *) sp))
  235. ra = (unsigned long) return_to_handler;
  236. out:
  237. return ra;
  238. }
  239. NOKPROBE_SYMBOL(prepare_ftrace_return);
  240. /*
  241. * Patch the kernel code at ftrace_graph_caller location. The instruction
  242. * there is branch relative on condition. To enable the ftrace graph code
  243. * block, we simply patch the mask field of the instruction to zero and
  244. * turn the instruction into a nop.
  245. * To disable the ftrace graph code the mask field will be patched to
  246. * all ones, which turns the instruction into an unconditional branch.
  247. */
  248. int ftrace_enable_ftrace_graph_caller(void)
  249. {
  250. int rc;
  251. /* Expect brc 0xf,... */
  252. rc = ftrace_patch_branch_mask(ftrace_graph_caller, 0xa7f4, false);
  253. if (rc)
  254. return rc;
  255. text_poke_sync_lock();
  256. return 0;
  257. }
  258. int ftrace_disable_ftrace_graph_caller(void)
  259. {
  260. int rc;
  261. /* Expect brc 0x0,... */
  262. rc = ftrace_patch_branch_mask(ftrace_graph_caller, 0xa704, true);
  263. if (rc)
  264. return rc;
  265. text_poke_sync_lock();
  266. return 0;
  267. }
  268. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  269. #ifdef CONFIG_KPROBES_ON_FTRACE
  270. void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
  271. struct ftrace_ops *ops, struct ftrace_regs *fregs)
  272. {
  273. struct kprobe_ctlblk *kcb;
  274. struct pt_regs *regs;
  275. struct kprobe *p;
  276. int bit;
  277. bit = ftrace_test_recursion_trylock(ip, parent_ip);
  278. if (bit < 0)
  279. return;
  280. regs = ftrace_get_regs(fregs);
  281. p = get_kprobe((kprobe_opcode_t *)ip);
  282. if (!regs || unlikely(!p) || kprobe_disabled(p))
  283. goto out;
  284. if (kprobe_running()) {
  285. kprobes_inc_nmissed_count(p);
  286. goto out;
  287. }
  288. __this_cpu_write(current_kprobe, p);
  289. kcb = get_kprobe_ctlblk();
  290. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  291. instruction_pointer_set(regs, ip);
  292. if (!p->pre_handler || !p->pre_handler(p, regs)) {
  293. instruction_pointer_set(regs, ip + MCOUNT_INSN_SIZE);
  294. if (unlikely(p->post_handler)) {
  295. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  296. p->post_handler(p, regs, 0);
  297. }
  298. }
  299. __this_cpu_write(current_kprobe, NULL);
  300. out:
  301. ftrace_test_recursion_unlock(bit);
  302. }
  303. NOKPROBE_SYMBOL(kprobe_ftrace_handler);
  304. int arch_prepare_kprobe_ftrace(struct kprobe *p)
  305. {
  306. p->ainsn.insn = NULL;
  307. return 0;
  308. }
  309. #endif