ftrace.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #ifdef CONFIG_DYNAMIC_FTRACE
  19. #define JAL 0x0c000000 /* jump & link: ip --> ra, jump to target */
  20. #define ADDR_MASK 0x03ffffff /* op_code|addr : 31...26|25 ....0 */
  21. #define INSN_B_1F_4 0x10000004 /* b 1f; offset = 4 */
  22. #define INSN_B_1F_5 0x10000005 /* b 1f; offset = 5 */
  23. #define INSN_NOP 0x00000000 /* nop */
  24. #define INSN_JAL(addr) \
  25. ((unsigned int)(JAL | (((addr) >> 2) & ADDR_MASK)))
  26. static unsigned int insn_jal_ftrace_caller __read_mostly;
  27. static unsigned int insn_lui_v1_hi16_mcount __read_mostly;
  28. static unsigned int insn_j_ftrace_graph_caller __maybe_unused __read_mostly;
  29. static inline void ftrace_dyn_arch_init_insns(void)
  30. {
  31. u32 *buf;
  32. unsigned int v1;
  33. /* lui v1, hi16_mcount */
  34. v1 = 3;
  35. buf = (u32 *)&insn_lui_v1_hi16_mcount;
  36. UASM_i_LA_mostly(&buf, v1, MCOUNT_ADDR);
  37. /* jal (ftrace_caller + 8), jump over the first two instruction */
  38. buf = (u32 *)&insn_jal_ftrace_caller;
  39. uasm_i_jal(&buf, (FTRACE_ADDR + 8));
  40. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  41. /* j ftrace_graph_caller */
  42. buf = (u32 *)&insn_j_ftrace_graph_caller;
  43. uasm_i_j(&buf, (unsigned long)ftrace_graph_caller);
  44. #endif
  45. }
  46. /*
  47. * Check if the address is in kernel space
  48. *
  49. * Clone core_kernel_text() from kernel/extable.c, but doesn't call
  50. * init_kernel_text() for Ftrace doesn't trace functions in init sections.
  51. */
  52. static inline int in_kernel_space(unsigned long ip)
  53. {
  54. if (ip >= (unsigned long)_stext &&
  55. ip <= (unsigned long)_etext)
  56. return 1;
  57. return 0;
  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. int ftrace_make_nop(struct module *mod,
  70. struct dyn_ftrace *rec, unsigned long addr)
  71. {
  72. unsigned int new;
  73. unsigned long ip = rec->ip;
  74. /*
  75. * If ip is in kernel space, no long call, otherwise, long call is
  76. * needed.
  77. */
  78. if (in_kernel_space(ip)) {
  79. /*
  80. * move at, ra
  81. * jal _mcount --> nop
  82. */
  83. new = INSN_NOP;
  84. } else {
  85. #if defined(KBUILD_MCOUNT_RA_ADDRESS) && defined(CONFIG_32BIT)
  86. /*
  87. * lui v1, hi_16bit_of_mcount --> b 1f (0x10000005)
  88. * addiu v1, v1, low_16bit_of_mcount
  89. * move at, ra
  90. * move $12, ra_address
  91. * jalr v1
  92. * sub sp, sp, 8
  93. * 1: offset = 5 instructions
  94. */
  95. new = INSN_B_1F_5;
  96. #else
  97. /*
  98. * lui v1, hi_16bit_of_mcount --> b 1f (0x10000004)
  99. * addiu v1, v1, low_16bit_of_mcount
  100. * move at, ra
  101. * jalr v1
  102. * nop | move $12, ra_address | sub sp, sp, 8
  103. * 1: offset = 4 instructions
  104. */
  105. new = INSN_B_1F_4;
  106. #endif
  107. }
  108. return ftrace_modify_code(ip, new);
  109. }
  110. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  111. {
  112. unsigned int new;
  113. unsigned long ip = rec->ip;
  114. new = in_kernel_space(ip) ? insn_jal_ftrace_caller :
  115. insn_lui_v1_hi16_mcount;
  116. return ftrace_modify_code(ip, new);
  117. }
  118. #define FTRACE_CALL_IP ((unsigned long)(&ftrace_call))
  119. int ftrace_update_ftrace_func(ftrace_func_t func)
  120. {
  121. unsigned int new;
  122. new = INSN_JAL((unsigned long)func);
  123. return ftrace_modify_code(FTRACE_CALL_IP, new);
  124. }
  125. int __init ftrace_dyn_arch_init(void *data)
  126. {
  127. /* Encode the instructions when booting */
  128. ftrace_dyn_arch_init_insns();
  129. /* Remove "b ftrace_stub" to ensure ftrace_caller() is executed */
  130. ftrace_modify_code(MCOUNT_ADDR, INSN_NOP);
  131. /* The return code is retured via data */
  132. *(unsigned long *)data = 0;
  133. return 0;
  134. }
  135. #endif /* CONFIG_DYNAMIC_FTRACE */
  136. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  137. #ifdef CONFIG_DYNAMIC_FTRACE
  138. extern void ftrace_graph_call(void);
  139. #define FTRACE_GRAPH_CALL_IP ((unsigned long)(&ftrace_graph_call))
  140. int ftrace_enable_ftrace_graph_caller(void)
  141. {
  142. return ftrace_modify_code(FTRACE_GRAPH_CALL_IP,
  143. insn_j_ftrace_graph_caller);
  144. }
  145. int ftrace_disable_ftrace_graph_caller(void)
  146. {
  147. return ftrace_modify_code(FTRACE_GRAPH_CALL_IP, INSN_NOP);
  148. }
  149. #endif /* CONFIG_DYNAMIC_FTRACE */
  150. #ifndef KBUILD_MCOUNT_RA_ADDRESS
  151. #define S_RA_SP (0xafbf << 16) /* s{d,w} ra, offset(sp) */
  152. #define S_R_SP (0xafb0 << 16) /* s{d,w} R, offset(sp) */
  153. #define OFFSET_MASK 0xffff /* stack offset range: 0 ~ PT_SIZE */
  154. unsigned long ftrace_get_parent_ra_addr(unsigned long self_ra, unsigned long
  155. old_parent_ra, unsigned long parent_ra_addr, unsigned long fp)
  156. {
  157. unsigned long sp, ip, tmp;
  158. unsigned int code;
  159. int faulted;
  160. /*
  161. * For module, move the ip from the return address after the
  162. * instruction "lui v1, hi_16bit_of_mcount"(offset is 24), but for
  163. * kernel, move after the instruction "move ra, at"(offset is 16)
  164. */
  165. ip = self_ra - (in_kernel_space(self_ra) ? 16 : 24);
  166. /*
  167. * search the text until finding the non-store instruction or "s{d,w}
  168. * ra, offset(sp)" instruction
  169. */
  170. do {
  171. /* get the code at "ip": code = *(unsigned int *)ip; */
  172. safe_load_code(code, ip, faulted);
  173. if (unlikely(faulted))
  174. return 0;
  175. /*
  176. * If we hit the non-store instruction before finding where the
  177. * ra is stored, then this is a leaf function and it does not
  178. * store the ra on the stack
  179. */
  180. if ((code & S_R_SP) != S_R_SP)
  181. return parent_ra_addr;
  182. /* Move to the next instruction */
  183. ip -= 4;
  184. } while ((code & S_RA_SP) != S_RA_SP);
  185. sp = fp + (code & OFFSET_MASK);
  186. /* tmp = *(unsigned long *)sp; */
  187. safe_load_stack(tmp, sp, faulted);
  188. if (unlikely(faulted))
  189. return 0;
  190. if (tmp == old_parent_ra)
  191. return sp;
  192. return 0;
  193. }
  194. #endif /* !KBUILD_MCOUNT_RA_ADDRESS */
  195. /*
  196. * Hook the return address and push it in the stack of return addrs
  197. * in current thread info.
  198. */
  199. void prepare_ftrace_return(unsigned long *parent_ra_addr, unsigned long self_ra,
  200. unsigned long fp)
  201. {
  202. unsigned long old_parent_ra;
  203. struct ftrace_graph_ent trace;
  204. unsigned long return_hooker = (unsigned long)
  205. &return_to_handler;
  206. int faulted;
  207. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  208. return;
  209. /*
  210. * "parent_ra_addr" is the stack address saved the return address of
  211. * the caller of _mcount.
  212. *
  213. * if the gcc < 4.5, a leaf function does not save the return address
  214. * in the stack address, so, we "emulate" one in _mcount's stack space,
  215. * and hijack it directly, but for a non-leaf function, it save the
  216. * return address to the its own stack space, we can not hijack it
  217. * directly, but need to find the real stack address,
  218. * ftrace_get_parent_addr() does it!
  219. *
  220. * if gcc>= 4.5, with the new -mmcount-ra-address option, for a
  221. * non-leaf function, the location of the return address will be saved
  222. * to $12 for us, and for a leaf function, only put a zero into $12. we
  223. * do it in ftrace_graph_caller of mcount.S.
  224. */
  225. /* old_parent_ra = *parent_ra_addr; */
  226. safe_load_stack(old_parent_ra, parent_ra_addr, faulted);
  227. if (unlikely(faulted))
  228. goto out;
  229. #ifndef KBUILD_MCOUNT_RA_ADDRESS
  230. parent_ra_addr = (unsigned long *)ftrace_get_parent_ra_addr(self_ra,
  231. old_parent_ra, (unsigned long)parent_ra_addr, fp);
  232. /*
  233. * If fails when getting the stack address of the non-leaf function's
  234. * ra, stop function graph tracer and return
  235. */
  236. if (parent_ra_addr == 0)
  237. goto out;
  238. #endif
  239. /* *parent_ra_addr = return_hooker; */
  240. safe_store_stack(return_hooker, parent_ra_addr, faulted);
  241. if (unlikely(faulted))
  242. goto out;
  243. if (ftrace_push_return_trace(old_parent_ra, self_ra, &trace.depth, fp)
  244. == -EBUSY) {
  245. *parent_ra_addr = old_parent_ra;
  246. return;
  247. }
  248. trace.func = self_ra;
  249. /* Only trace if the calling function expects to */
  250. if (!ftrace_graph_entry(&trace)) {
  251. current->curr_ret_stack--;
  252. *parent_ra_addr = old_parent_ra;
  253. }
  254. return;
  255. out:
  256. ftrace_graph_stop();
  257. WARN_ON(1);
  258. }
  259. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */