ftrace.c 9.4 KB

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