ftrace.c 10 KB

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