ftrace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Code for replacing ftrace calls with jumps.
  4. *
  5. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  6. * Copyright (C) 2009, 2010 DSLab, Lanzhou University, China
  7. * Author: Wu Zhangjin <wuzhangjin@gmail.com>
  8. *
  9. * Thanks goes to Steven Rostedt for writing the original x86 version.
  10. */
  11. #include <linux/uaccess.h>
  12. #include <linux/init.h>
  13. #include <linux/ftrace.h>
  14. #include <linux/syscalls.h>
  15. #include <asm/asm.h>
  16. #include <asm/asm-offsets.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/syscall.h>
  19. #include <asm/uasm.h>
  20. #include <asm/unistd.h>
  21. #include <asm-generic/sections.h>
  22. #if defined(KBUILD_MCOUNT_RA_ADDRESS) && defined(CONFIG_32BIT)
  23. #define MCOUNT_OFFSET_INSNS 5
  24. #else
  25. #define MCOUNT_OFFSET_INSNS 4
  26. #endif
  27. #ifdef CONFIG_DYNAMIC_FTRACE
  28. /* Arch override because MIPS doesn't need to run this from stop_machine() */
  29. void arch_ftrace_update_code(int command)
  30. {
  31. ftrace_modify_all_code(command);
  32. }
  33. #endif
  34. #ifdef CONFIG_DYNAMIC_FTRACE
  35. #define JAL 0x0c000000 /* jump & link: ip --> ra, jump to target */
  36. #define ADDR_MASK 0x03ffffff /* op_code|addr : 31...26|25 ....0 */
  37. #define JUMP_RANGE_MASK ((1UL << 28) - 1)
  38. #define INSN_NOP 0x00000000 /* nop */
  39. #define INSN_JAL(addr) \
  40. ((unsigned int)(JAL | (((addr) >> 2) & ADDR_MASK)))
  41. static unsigned int insn_jal_ftrace_caller __read_mostly;
  42. static unsigned int insn_la_mcount[2] __read_mostly;
  43. static unsigned int insn_j_ftrace_graph_caller __maybe_unused __read_mostly;
  44. static inline void ftrace_dyn_arch_init_insns(void)
  45. {
  46. u32 *buf;
  47. unsigned int v1;
  48. /* la v1, _mcount */
  49. v1 = 3;
  50. buf = (u32 *)&insn_la_mcount[0];
  51. UASM_i_LA(&buf, v1, MCOUNT_ADDR);
  52. /* jal (ftrace_caller + 8), jump over the first two instruction */
  53. buf = (u32 *)&insn_jal_ftrace_caller;
  54. uasm_i_jal(&buf, (FTRACE_ADDR + 8) & JUMP_RANGE_MASK);
  55. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  56. /* j ftrace_graph_caller */
  57. buf = (u32 *)&insn_j_ftrace_graph_caller;
  58. uasm_i_j(&buf, (unsigned long)ftrace_graph_caller & JUMP_RANGE_MASK);
  59. #endif
  60. }
  61. static int ftrace_modify_code(unsigned long ip, unsigned int new_code)
  62. {
  63. int faulted;
  64. mm_segment_t old_fs;
  65. /* *(unsigned int *)ip = new_code; */
  66. safe_store_code(new_code, ip, faulted);
  67. if (unlikely(faulted))
  68. return -EFAULT;
  69. old_fs = get_fs();
  70. set_fs(get_ds());
  71. flush_icache_range(ip, ip + 8);
  72. set_fs(old_fs);
  73. return 0;
  74. }
  75. #ifndef CONFIG_64BIT
  76. static int ftrace_modify_code_2(unsigned long ip, unsigned int new_code1,
  77. unsigned int new_code2)
  78. {
  79. int faulted;
  80. mm_segment_t old_fs;
  81. safe_store_code(new_code1, ip, faulted);
  82. if (unlikely(faulted))
  83. return -EFAULT;
  84. ip += 4;
  85. safe_store_code(new_code2, ip, faulted);
  86. if (unlikely(faulted))
  87. return -EFAULT;
  88. ip -= 4;
  89. old_fs = get_fs();
  90. set_fs(get_ds());
  91. flush_icache_range(ip, ip + 8);
  92. set_fs(old_fs);
  93. return 0;
  94. }
  95. static int ftrace_modify_code_2r(unsigned long ip, unsigned int new_code1,
  96. unsigned int new_code2)
  97. {
  98. int faulted;
  99. mm_segment_t old_fs;
  100. ip += 4;
  101. safe_store_code(new_code2, ip, faulted);
  102. if (unlikely(faulted))
  103. return -EFAULT;
  104. ip -= 4;
  105. safe_store_code(new_code1, ip, faulted);
  106. if (unlikely(faulted))
  107. return -EFAULT;
  108. old_fs = get_fs();
  109. set_fs(get_ds());
  110. flush_icache_range(ip, ip + 8);
  111. set_fs(old_fs);
  112. return 0;
  113. }
  114. #endif
  115. /*
  116. * The details about the calling site of mcount on MIPS
  117. *
  118. * 1. For kernel:
  119. *
  120. * move at, ra
  121. * jal _mcount --> nop
  122. * sub sp, sp, 8 --> nop (CONFIG_32BIT)
  123. *
  124. * 2. For modules:
  125. *
  126. * 2.1 For KBUILD_MCOUNT_RA_ADDRESS and CONFIG_32BIT
  127. *
  128. * lui v1, hi_16bit_of_mcount --> b 1f (0x10000005)
  129. * addiu v1, v1, low_16bit_of_mcount --> nop (CONFIG_32BIT)
  130. * move at, ra
  131. * move $12, ra_address
  132. * jalr v1
  133. * sub sp, sp, 8
  134. * 1: offset = 5 instructions
  135. * 2.2 For the Other situations
  136. *
  137. * lui v1, hi_16bit_of_mcount --> b 1f (0x10000004)
  138. * addiu v1, v1, low_16bit_of_mcount --> nop (CONFIG_32BIT)
  139. * move at, ra
  140. * jalr v1
  141. * nop | move $12, ra_address | sub sp, sp, 8
  142. * 1: offset = 4 instructions
  143. */
  144. #define INSN_B_1F (0x10000000 | MCOUNT_OFFSET_INSNS)
  145. int ftrace_make_nop(struct module *mod,
  146. struct dyn_ftrace *rec, unsigned long addr)
  147. {
  148. unsigned int new;
  149. unsigned long ip = rec->ip;
  150. /*
  151. * If ip is in kernel space, no long call, otherwise, long call is
  152. * needed.
  153. */
  154. new = core_kernel_text(ip) ? INSN_NOP : INSN_B_1F;
  155. #ifdef CONFIG_64BIT
  156. return ftrace_modify_code(ip, new);
  157. #else
  158. /*
  159. * On 32 bit MIPS platforms, gcc adds a stack adjust
  160. * instruction in the delay slot after the branch to
  161. * mcount and expects mcount to restore the sp on return.
  162. * This is based on a legacy API and does nothing but
  163. * waste instructions so it's being removed at runtime.
  164. */
  165. return ftrace_modify_code_2(ip, new, INSN_NOP);
  166. #endif
  167. }
  168. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  169. {
  170. unsigned int new;
  171. unsigned long ip = rec->ip;
  172. new = core_kernel_text(ip) ? insn_jal_ftrace_caller : insn_la_mcount[0];
  173. #ifdef CONFIG_64BIT
  174. return ftrace_modify_code(ip, new);
  175. #else
  176. return ftrace_modify_code_2r(ip, new, core_kernel_text(ip) ?
  177. INSN_NOP : insn_la_mcount[1]);
  178. #endif
  179. }
  180. #define FTRACE_CALL_IP ((unsigned long)(&ftrace_call))
  181. int ftrace_update_ftrace_func(ftrace_func_t func)
  182. {
  183. unsigned int new;
  184. new = INSN_JAL((unsigned long)func);
  185. return ftrace_modify_code(FTRACE_CALL_IP, new);
  186. }
  187. int __init ftrace_dyn_arch_init(void)
  188. {
  189. /* Encode the instructions when booting */
  190. ftrace_dyn_arch_init_insns();
  191. /* Remove "b ftrace_stub" to ensure ftrace_caller() is executed */
  192. ftrace_modify_code(MCOUNT_ADDR, INSN_NOP);
  193. return 0;
  194. }
  195. #endif /* CONFIG_DYNAMIC_FTRACE */
  196. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  197. #ifdef CONFIG_DYNAMIC_FTRACE
  198. extern void ftrace_graph_call(void);
  199. #define FTRACE_GRAPH_CALL_IP ((unsigned long)(&ftrace_graph_call))
  200. int ftrace_enable_ftrace_graph_caller(void)
  201. {
  202. return ftrace_modify_code(FTRACE_GRAPH_CALL_IP,
  203. insn_j_ftrace_graph_caller);
  204. }
  205. int ftrace_disable_ftrace_graph_caller(void)
  206. {
  207. return ftrace_modify_code(FTRACE_GRAPH_CALL_IP, INSN_NOP);
  208. }
  209. #endif /* CONFIG_DYNAMIC_FTRACE */
  210. #ifndef KBUILD_MCOUNT_RA_ADDRESS
  211. #define S_RA_SP (0xafbf << 16) /* s{d,w} ra, offset(sp) */
  212. #define S_R_SP (0xafb0 << 16) /* s{d,w} R, offset(sp) */
  213. #define OFFSET_MASK 0xffff /* stack offset range: 0 ~ PT_SIZE */
  214. unsigned long ftrace_get_parent_ra_addr(unsigned long self_ra, unsigned long
  215. old_parent_ra, unsigned long parent_ra_addr, unsigned long fp)
  216. {
  217. unsigned long sp, ip, tmp;
  218. unsigned int code;
  219. int faulted;
  220. /*
  221. * For module, move the ip from the return address after the
  222. * instruction "lui v1, hi_16bit_of_mcount"(offset is 24), but for
  223. * kernel, move after the instruction "move ra, at"(offset is 16)
  224. */
  225. ip = self_ra - (core_kernel_text(self_ra) ? 16 : 24);
  226. /*
  227. * search the text until finding the non-store instruction or "s{d,w}
  228. * ra, offset(sp)" instruction
  229. */
  230. do {
  231. /* get the code at "ip": code = *(unsigned int *)ip; */
  232. safe_load_code(code, ip, faulted);
  233. if (unlikely(faulted))
  234. return 0;
  235. /*
  236. * If we hit the non-store instruction before finding where the
  237. * ra is stored, then this is a leaf function and it does not
  238. * store the ra on the stack
  239. */
  240. if ((code & S_R_SP) != S_R_SP)
  241. return parent_ra_addr;
  242. /* Move to the next instruction */
  243. ip -= 4;
  244. } while ((code & S_RA_SP) != S_RA_SP);
  245. sp = fp + (code & OFFSET_MASK);
  246. /* tmp = *(unsigned long *)sp; */
  247. safe_load_stack(tmp, sp, faulted);
  248. if (unlikely(faulted))
  249. return 0;
  250. if (tmp == old_parent_ra)
  251. return sp;
  252. return 0;
  253. }
  254. #endif /* !KBUILD_MCOUNT_RA_ADDRESS */
  255. /*
  256. * Hook the return address and push it in the stack of return addrs
  257. * in current thread info.
  258. */
  259. void prepare_ftrace_return(unsigned long *parent_ra_addr, unsigned long self_ra,
  260. unsigned long fp)
  261. {
  262. unsigned long old_parent_ra;
  263. struct ftrace_graph_ent trace;
  264. unsigned long return_hooker = (unsigned long)
  265. &return_to_handler;
  266. int faulted, insns;
  267. if (unlikely(ftrace_graph_is_dead()))
  268. return;
  269. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  270. return;
  271. /*
  272. * "parent_ra_addr" is the stack address saved the return address of
  273. * the caller of _mcount.
  274. *
  275. * if the gcc < 4.5, a leaf function does not save the return address
  276. * in the stack address, so, we "emulate" one in _mcount's stack space,
  277. * and hijack it directly, but for a non-leaf function, it save the
  278. * return address to the its own stack space, we can not hijack it
  279. * directly, but need to find the real stack address,
  280. * ftrace_get_parent_addr() does it!
  281. *
  282. * if gcc>= 4.5, with the new -mmcount-ra-address option, for a
  283. * non-leaf function, the location of the return address will be saved
  284. * to $12 for us, and for a leaf function, only put a zero into $12. we
  285. * do it in ftrace_graph_caller of mcount.S.
  286. */
  287. /* old_parent_ra = *parent_ra_addr; */
  288. safe_load_stack(old_parent_ra, parent_ra_addr, faulted);
  289. if (unlikely(faulted))
  290. goto out;
  291. #ifndef KBUILD_MCOUNT_RA_ADDRESS
  292. parent_ra_addr = (unsigned long *)ftrace_get_parent_ra_addr(self_ra,
  293. old_parent_ra, (unsigned long)parent_ra_addr, fp);
  294. /*
  295. * If fails when getting the stack address of the non-leaf function's
  296. * ra, stop function graph tracer and return
  297. */
  298. if (parent_ra_addr == NULL)
  299. goto out;
  300. #endif
  301. /* *parent_ra_addr = return_hooker; */
  302. safe_store_stack(return_hooker, parent_ra_addr, faulted);
  303. if (unlikely(faulted))
  304. goto out;
  305. if (ftrace_push_return_trace(old_parent_ra, self_ra, &trace.depth, fp,
  306. NULL) == -EBUSY) {
  307. *parent_ra_addr = old_parent_ra;
  308. return;
  309. }
  310. /*
  311. * Get the recorded ip of the current mcount calling site in the
  312. * __mcount_loc section, which will be used to filter the function
  313. * entries configured through the tracing/set_graph_function interface.
  314. */
  315. insns = core_kernel_text(self_ra) ? 2 : MCOUNT_OFFSET_INSNS + 1;
  316. trace.func = self_ra - (MCOUNT_INSN_SIZE * insns);
  317. /* Only trace if the calling function expects to */
  318. if (!ftrace_graph_entry(&trace)) {
  319. current->curr_ret_stack--;
  320. *parent_ra_addr = old_parent_ra;
  321. }
  322. return;
  323. out:
  324. ftrace_graph_stop();
  325. WARN_ON(1);
  326. }
  327. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  328. #ifdef CONFIG_FTRACE_SYSCALLS
  329. #ifdef CONFIG_32BIT
  330. unsigned long __init arch_syscall_addr(int nr)
  331. {
  332. return (unsigned long)sys_call_table[nr - __NR_O32_Linux];
  333. }
  334. #endif
  335. #ifdef CONFIG_64BIT
  336. unsigned long __init arch_syscall_addr(int nr)
  337. {
  338. #ifdef CONFIG_MIPS32_N32
  339. if (nr >= __NR_N32_Linux && nr <= __NR_N32_Linux + __NR_N32_Linux_syscalls)
  340. return (unsigned long)sysn32_call_table[nr - __NR_N32_Linux];
  341. #endif
  342. if (nr >= __NR_64_Linux && nr <= __NR_64_Linux + __NR_64_Linux_syscalls)
  343. return (unsigned long)sys_call_table[nr - __NR_64_Linux];
  344. #ifdef CONFIG_MIPS32_O32
  345. if (nr >= __NR_O32_Linux && nr <= __NR_O32_Linux + __NR_O32_Linux_syscalls)
  346. return (unsigned long)sys32_call_table[nr - __NR_O32_Linux];
  347. #endif
  348. return (unsigned long) &sys_ni_syscall;
  349. }
  350. #endif
  351. #endif /* CONFIG_FTRACE_SYSCALLS */