ftrace.c 9.8 KB

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