ftrace.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * arch/arm64/kernel/ftrace.c
  4. *
  5. * Copyright (C) 2013 Linaro Limited
  6. * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
  7. */
  8. #include <linux/ftrace.h>
  9. #include <linux/module.h>
  10. #include <linux/swab.h>
  11. #include <linux/uaccess.h>
  12. #include <asm/cacheflush.h>
  13. #include <asm/debug-monitors.h>
  14. #include <asm/ftrace.h>
  15. #include <asm/insn.h>
  16. #include <asm/patching.h>
  17. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_ARGS
  18. struct fregs_offset {
  19. const char *name;
  20. int offset;
  21. };
  22. #define FREGS_OFFSET(n, field) \
  23. { \
  24. .name = n, \
  25. .offset = offsetof(struct ftrace_regs, field), \
  26. }
  27. static const struct fregs_offset fregs_offsets[] = {
  28. FREGS_OFFSET("x0", regs[0]),
  29. FREGS_OFFSET("x1", regs[1]),
  30. FREGS_OFFSET("x2", regs[2]),
  31. FREGS_OFFSET("x3", regs[3]),
  32. FREGS_OFFSET("x4", regs[4]),
  33. FREGS_OFFSET("x5", regs[5]),
  34. FREGS_OFFSET("x6", regs[6]),
  35. FREGS_OFFSET("x7", regs[7]),
  36. FREGS_OFFSET("x8", regs[8]),
  37. FREGS_OFFSET("x29", fp),
  38. FREGS_OFFSET("x30", lr),
  39. FREGS_OFFSET("lr", lr),
  40. FREGS_OFFSET("sp", sp),
  41. FREGS_OFFSET("pc", pc),
  42. };
  43. int ftrace_regs_query_register_offset(const char *name)
  44. {
  45. for (int i = 0; i < ARRAY_SIZE(fregs_offsets); i++) {
  46. const struct fregs_offset *roff = &fregs_offsets[i];
  47. if (!strcmp(roff->name, name))
  48. return roff->offset;
  49. }
  50. return -EINVAL;
  51. }
  52. #endif
  53. unsigned long ftrace_call_adjust(unsigned long addr)
  54. {
  55. /*
  56. * When using mcount, addr is the address of the mcount call
  57. * instruction, and no adjustment is necessary.
  58. */
  59. if (!IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_ARGS))
  60. return addr;
  61. /*
  62. * When using patchable-function-entry without pre-function NOPS, addr
  63. * is the address of the first NOP after the function entry point.
  64. *
  65. * The compiler has either generated:
  66. *
  67. * addr+00: func: NOP // To be patched to MOV X9, LR
  68. * addr+04: NOP // To be patched to BL <caller>
  69. *
  70. * Or:
  71. *
  72. * addr-04: BTI C
  73. * addr+00: func: NOP // To be patched to MOV X9, LR
  74. * addr+04: NOP // To be patched to BL <caller>
  75. *
  76. * We must adjust addr to the address of the NOP which will be patched
  77. * to `BL <caller>`, which is at `addr + 4` bytes in either case.
  78. *
  79. */
  80. if (!IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS))
  81. return addr + AARCH64_INSN_SIZE;
  82. /*
  83. * When using patchable-function-entry with pre-function NOPs, addr is
  84. * the address of the first pre-function NOP.
  85. *
  86. * Starting from an 8-byte aligned base, the compiler has either
  87. * generated:
  88. *
  89. * addr+00: NOP // Literal (first 32 bits)
  90. * addr+04: NOP // Literal (last 32 bits)
  91. * addr+08: func: NOP // To be patched to MOV X9, LR
  92. * addr+12: NOP // To be patched to BL <caller>
  93. *
  94. * Or:
  95. *
  96. * addr+00: NOP // Literal (first 32 bits)
  97. * addr+04: NOP // Literal (last 32 bits)
  98. * addr+08: func: BTI C
  99. * addr+12: NOP // To be patched to MOV X9, LR
  100. * addr+16: NOP // To be patched to BL <caller>
  101. *
  102. * We must adjust addr to the address of the NOP which will be patched
  103. * to `BL <caller>`, which is at either addr+12 or addr+16 depending on
  104. * whether there is a BTI.
  105. */
  106. if (!IS_ALIGNED(addr, sizeof(unsigned long))) {
  107. WARN_RATELIMIT(1, "Misaligned patch-site %pS\n",
  108. (void *)(addr + 8));
  109. return 0;
  110. }
  111. /* Skip the NOPs placed before the function entry point */
  112. addr += 2 * AARCH64_INSN_SIZE;
  113. /* Skip any BTI */
  114. if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL)) {
  115. u32 insn = le32_to_cpu(*(__le32 *)addr);
  116. if (aarch64_insn_is_bti(insn)) {
  117. addr += AARCH64_INSN_SIZE;
  118. } else if (insn != aarch64_insn_gen_nop()) {
  119. WARN_RATELIMIT(1, "unexpected insn in patch-site %pS: 0x%08x\n",
  120. (void *)addr, insn);
  121. }
  122. }
  123. /* Skip the first NOP after function entry */
  124. addr += AARCH64_INSN_SIZE;
  125. return addr;
  126. }
  127. /*
  128. * Replace a single instruction, which may be a branch or NOP.
  129. * If @validate == true, a replaced instruction is checked against 'old'.
  130. */
  131. static int ftrace_modify_code(unsigned long pc, u32 old, u32 new,
  132. bool validate)
  133. {
  134. u32 replaced;
  135. /*
  136. * Note:
  137. * We are paranoid about modifying text, as if a bug were to happen, it
  138. * could cause us to read or write to someplace that could cause harm.
  139. * Carefully read and modify the code with aarch64_insn_*() which uses
  140. * probe_kernel_*(), and make sure what we read is what we expected it
  141. * to be before modifying it.
  142. */
  143. if (validate) {
  144. if (aarch64_insn_read((void *)pc, &replaced))
  145. return -EFAULT;
  146. if (replaced != old)
  147. return -EINVAL;
  148. }
  149. if (aarch64_insn_patch_text_nosync((void *)pc, new))
  150. return -EPERM;
  151. return 0;
  152. }
  153. /*
  154. * Replace tracer function in ftrace_caller()
  155. */
  156. int ftrace_update_ftrace_func(ftrace_func_t func)
  157. {
  158. unsigned long pc;
  159. u32 new;
  160. /*
  161. * When using CALL_OPS, the function to call is associated with the
  162. * call site, and we don't have a global function pointer to update.
  163. */
  164. if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS))
  165. return 0;
  166. pc = (unsigned long)ftrace_call;
  167. new = aarch64_insn_gen_branch_imm(pc, (unsigned long)func,
  168. AARCH64_INSN_BRANCH_LINK);
  169. return ftrace_modify_code(pc, 0, new, false);
  170. }
  171. static struct plt_entry *get_ftrace_plt(struct module *mod, unsigned long addr)
  172. {
  173. #ifdef CONFIG_MODULES
  174. struct plt_entry *plt = NULL;
  175. if (within_module_mem_type(addr, mod, MOD_INIT_TEXT))
  176. plt = mod->arch.init_ftrace_trampolines;
  177. else if (within_module_mem_type(addr, mod, MOD_TEXT))
  178. plt = mod->arch.ftrace_trampolines;
  179. else
  180. return NULL;
  181. return &plt[FTRACE_PLT_IDX];
  182. #else
  183. return NULL;
  184. #endif
  185. }
  186. static bool reachable_by_bl(unsigned long addr, unsigned long pc)
  187. {
  188. long offset = (long)addr - (long)pc;
  189. return offset >= -SZ_128M && offset < SZ_128M;
  190. }
  191. /*
  192. * Find the address the callsite must branch to in order to reach '*addr'.
  193. *
  194. * Due to the limited range of 'BL' instructions, modules may be placed too far
  195. * away to branch directly and must use a PLT.
  196. *
  197. * Returns true when '*addr' contains a reachable target address, or has been
  198. * modified to contain a PLT address. Returns false otherwise.
  199. */
  200. static bool ftrace_find_callable_addr(struct dyn_ftrace *rec,
  201. struct module *mod,
  202. unsigned long *addr)
  203. {
  204. unsigned long pc = rec->ip;
  205. struct plt_entry *plt;
  206. /*
  207. * If a custom trampoline is unreachable, rely on the ftrace_caller
  208. * trampoline which knows how to indirectly reach that trampoline
  209. * through ops->direct_call.
  210. */
  211. if (*addr != FTRACE_ADDR && !reachable_by_bl(*addr, pc))
  212. *addr = FTRACE_ADDR;
  213. /*
  214. * When the target is within range of the 'BL' instruction, use 'addr'
  215. * as-is and branch to that directly.
  216. */
  217. if (reachable_by_bl(*addr, pc))
  218. return true;
  219. /*
  220. * When the target is outside of the range of a 'BL' instruction, we
  221. * must use a PLT to reach it. We can only place PLTs for modules, and
  222. * only when module PLT support is built-in.
  223. */
  224. if (!IS_ENABLED(CONFIG_MODULES))
  225. return false;
  226. /*
  227. * 'mod' is only set at module load time, but if we end up
  228. * dealing with an out-of-range condition, we can assume it
  229. * is due to a module being loaded far away from the kernel.
  230. *
  231. * NOTE: __module_text_address() must be called with preemption
  232. * disabled, but we can rely on ftrace_lock to ensure that 'mod'
  233. * retains its validity throughout the remainder of this code.
  234. */
  235. if (!mod) {
  236. preempt_disable();
  237. mod = __module_text_address(pc);
  238. preempt_enable();
  239. }
  240. if (WARN_ON(!mod))
  241. return false;
  242. plt = get_ftrace_plt(mod, pc);
  243. if (!plt) {
  244. pr_err("ftrace: no module PLT for %ps\n", (void *)*addr);
  245. return false;
  246. }
  247. *addr = (unsigned long)plt;
  248. return true;
  249. }
  250. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS
  251. static const struct ftrace_ops *arm64_rec_get_ops(struct dyn_ftrace *rec)
  252. {
  253. const struct ftrace_ops *ops = NULL;
  254. if (rec->flags & FTRACE_FL_CALL_OPS_EN) {
  255. ops = ftrace_find_unique_ops(rec);
  256. WARN_ON_ONCE(!ops);
  257. }
  258. if (!ops)
  259. ops = &ftrace_list_ops;
  260. return ops;
  261. }
  262. static int ftrace_rec_set_ops(const struct dyn_ftrace *rec,
  263. const struct ftrace_ops *ops)
  264. {
  265. unsigned long literal = ALIGN_DOWN(rec->ip - 12, 8);
  266. return aarch64_insn_write_literal_u64((void *)literal,
  267. (unsigned long)ops);
  268. }
  269. static int ftrace_rec_set_nop_ops(struct dyn_ftrace *rec)
  270. {
  271. return ftrace_rec_set_ops(rec, &ftrace_nop_ops);
  272. }
  273. static int ftrace_rec_update_ops(struct dyn_ftrace *rec)
  274. {
  275. return ftrace_rec_set_ops(rec, arm64_rec_get_ops(rec));
  276. }
  277. #else
  278. static int ftrace_rec_set_nop_ops(struct dyn_ftrace *rec) { return 0; }
  279. static int ftrace_rec_update_ops(struct dyn_ftrace *rec) { return 0; }
  280. #endif
  281. /*
  282. * Turn on the call to ftrace_caller() in instrumented function
  283. */
  284. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  285. {
  286. unsigned long pc = rec->ip;
  287. u32 old, new;
  288. int ret;
  289. ret = ftrace_rec_update_ops(rec);
  290. if (ret)
  291. return ret;
  292. if (!ftrace_find_callable_addr(rec, NULL, &addr))
  293. return -EINVAL;
  294. old = aarch64_insn_gen_nop();
  295. new = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
  296. return ftrace_modify_code(pc, old, new, true);
  297. }
  298. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS
  299. int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
  300. unsigned long addr)
  301. {
  302. unsigned long pc = rec->ip;
  303. u32 old, new;
  304. int ret;
  305. ret = ftrace_rec_set_ops(rec, arm64_rec_get_ops(rec));
  306. if (ret)
  307. return ret;
  308. if (!ftrace_find_callable_addr(rec, NULL, &old_addr))
  309. return -EINVAL;
  310. if (!ftrace_find_callable_addr(rec, NULL, &addr))
  311. return -EINVAL;
  312. old = aarch64_insn_gen_branch_imm(pc, old_addr,
  313. AARCH64_INSN_BRANCH_LINK);
  314. new = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
  315. return ftrace_modify_code(pc, old, new, true);
  316. }
  317. #endif
  318. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_ARGS
  319. /*
  320. * The compiler has inserted two NOPs before the regular function prologue.
  321. * All instrumented functions follow the AAPCS, so x0-x8 and x19-x30 are live,
  322. * and x9-x18 are free for our use.
  323. *
  324. * At runtime we want to be able to swing a single NOP <-> BL to enable or
  325. * disable the ftrace call. The BL requires us to save the original LR value,
  326. * so here we insert a <MOV X9, LR> over the first NOP so the instructions
  327. * before the regular prologue are:
  328. *
  329. * | Compiled | Disabled | Enabled |
  330. * +----------+------------+------------+
  331. * | NOP | MOV X9, LR | MOV X9, LR |
  332. * | NOP | NOP | BL <entry> |
  333. *
  334. * The LR value will be recovered by ftrace_caller, and restored into LR
  335. * before returning to the regular function prologue. When a function is not
  336. * being traced, the MOV is not harmful given x9 is not live per the AAPCS.
  337. *
  338. * Note: ftrace_process_locs() has pre-adjusted rec->ip to be the address of
  339. * the BL.
  340. */
  341. int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
  342. {
  343. unsigned long pc = rec->ip - AARCH64_INSN_SIZE;
  344. u32 old, new;
  345. int ret;
  346. ret = ftrace_rec_set_nop_ops(rec);
  347. if (ret)
  348. return ret;
  349. old = aarch64_insn_gen_nop();
  350. new = aarch64_insn_gen_move_reg(AARCH64_INSN_REG_9,
  351. AARCH64_INSN_REG_LR,
  352. AARCH64_INSN_VARIANT_64BIT);
  353. return ftrace_modify_code(pc, old, new, true);
  354. }
  355. #endif
  356. /*
  357. * Turn off the call to ftrace_caller() in instrumented function
  358. */
  359. int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
  360. unsigned long addr)
  361. {
  362. unsigned long pc = rec->ip;
  363. u32 old = 0, new;
  364. int ret;
  365. new = aarch64_insn_gen_nop();
  366. ret = ftrace_rec_set_nop_ops(rec);
  367. if (ret)
  368. return ret;
  369. /*
  370. * When using mcount, callsites in modules may have been initalized to
  371. * call an arbitrary module PLT (which redirects to the _mcount stub)
  372. * rather than the ftrace PLT we'll use at runtime (which redirects to
  373. * the ftrace trampoline). We can ignore the old PLT when initializing
  374. * the callsite.
  375. *
  376. * Note: 'mod' is only set at module load time.
  377. */
  378. if (!IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_ARGS) && mod)
  379. return aarch64_insn_patch_text_nosync((void *)pc, new);
  380. if (!ftrace_find_callable_addr(rec, mod, &addr))
  381. return -EINVAL;
  382. old = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
  383. return ftrace_modify_code(pc, old, new, true);
  384. }
  385. void arch_ftrace_update_code(int command)
  386. {
  387. command |= FTRACE_MAY_SLEEP;
  388. ftrace_modify_all_code(command);
  389. }
  390. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  391. /*
  392. * function_graph tracer expects ftrace_return_to_handler() to be called
  393. * on the way back to parent. For this purpose, this function is called
  394. * in _mcount() or ftrace_caller() to replace return address (*parent) on
  395. * the call stack to return_to_handler.
  396. */
  397. void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent,
  398. unsigned long frame_pointer)
  399. {
  400. unsigned long return_hooker = (unsigned long)&return_to_handler;
  401. unsigned long old;
  402. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  403. return;
  404. /*
  405. * Note:
  406. * No protection against faulting at *parent, which may be seen
  407. * on other archs. It's unlikely on AArch64.
  408. */
  409. old = *parent;
  410. if (!function_graph_enter(old, self_addr, frame_pointer,
  411. (void *)frame_pointer)) {
  412. *parent = return_hooker;
  413. }
  414. }
  415. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_ARGS
  416. void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,
  417. struct ftrace_ops *op, struct ftrace_regs *fregs)
  418. {
  419. prepare_ftrace_return(ip, &fregs->lr, fregs->fp);
  420. }
  421. #else
  422. /*
  423. * Turn on/off the call to ftrace_graph_caller() in ftrace_caller()
  424. * depending on @enable.
  425. */
  426. static int ftrace_modify_graph_caller(bool enable)
  427. {
  428. unsigned long pc = (unsigned long)&ftrace_graph_call;
  429. u32 branch, nop;
  430. branch = aarch64_insn_gen_branch_imm(pc,
  431. (unsigned long)ftrace_graph_caller,
  432. AARCH64_INSN_BRANCH_NOLINK);
  433. nop = aarch64_insn_gen_nop();
  434. if (enable)
  435. return ftrace_modify_code(pc, nop, branch, true);
  436. else
  437. return ftrace_modify_code(pc, branch, nop, true);
  438. }
  439. int ftrace_enable_ftrace_graph_caller(void)
  440. {
  441. return ftrace_modify_graph_caller(true);
  442. }
  443. int ftrace_disable_ftrace_graph_caller(void)
  444. {
  445. return ftrace_modify_graph_caller(false);
  446. }
  447. #endif /* CONFIG_DYNAMIC_FTRACE_WITH_ARGS */
  448. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */