ftrace.c 11 KB

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