ftrace.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. /*
  18. * If the Instruction Pointer is in module space (0xc0000000), return true;
  19. * otherwise, it is in kernel space (0x80000000), return false.
  20. *
  21. * FIXME: This will not work when the kernel space and module space are the
  22. * same. If they are the same, we need to modify scripts/recordmcount.pl,
  23. * ftrace_make_nop/call() and the other related parts to ensure the
  24. * enabling/disabling of the calling site to _mcount is right for both kernel
  25. * and module.
  26. */
  27. static inline int in_module(unsigned long ip)
  28. {
  29. return ip & 0x40000000;
  30. }
  31. #ifdef CONFIG_DYNAMIC_FTRACE
  32. #define JAL 0x0c000000 /* jump & link: ip --> ra, jump to target */
  33. #define ADDR_MASK 0x03ffffff /* op_code|addr : 31...26|25 ....0 */
  34. #define INSN_B_1F_4 0x10000004 /* b 1f; offset = 4 */
  35. #define INSN_B_1F_5 0x10000005 /* b 1f; offset = 5 */
  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_lui_v1_hi16_mcount __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. /* lui v1, hi16_mcount */
  47. v1 = 3;
  48. buf = (u32 *)&insn_lui_v1_hi16_mcount;
  49. UASM_i_LA_mostly(&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));
  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);
  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. 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. * We have compiled module with -mlong-calls, but compiled the kernel
  76. * without it, we need to cope with them respectively.
  77. */
  78. if (in_module(ip)) {
  79. #if defined(KBUILD_MCOUNT_RA_ADDRESS) && defined(CONFIG_32BIT)
  80. /*
  81. * lui v1, hi_16bit_of_mcount --> b 1f (0x10000005)
  82. * addiu v1, v1, low_16bit_of_mcount
  83. * move at, ra
  84. * move $12, ra_address
  85. * jalr v1
  86. * sub sp, sp, 8
  87. * 1: offset = 5 instructions
  88. */
  89. new = INSN_B_1F_5;
  90. #else
  91. /*
  92. * lui v1, hi_16bit_of_mcount --> b 1f (0x10000004)
  93. * addiu v1, v1, low_16bit_of_mcount
  94. * move at, ra
  95. * jalr v1
  96. * nop | move $12, ra_address | sub sp, sp, 8
  97. * 1: offset = 4 instructions
  98. */
  99. new = INSN_B_1F_4;
  100. #endif
  101. } else {
  102. /*
  103. * move at, ra
  104. * jal _mcount --> nop
  105. */
  106. new = INSN_NOP;
  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. /* ip, module: 0xc0000000, kernel: 0x80000000 */
  115. new = in_module(ip) ? insn_lui_v1_hi16_mcount : insn_jal_ftrace_caller;
  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_addr(unsigned long self_addr,
  155. unsigned long parent,
  156. unsigned long parent_addr,
  157. unsigned long fp)
  158. {
  159. unsigned long sp, ip, ra;
  160. unsigned int code;
  161. int faulted;
  162. /*
  163. * For module, move the ip from calling site of mcount to the
  164. * instruction "lui v1, hi_16bit_of_mcount"(offset is 20), but for
  165. * kernel, move to the instruction "move ra, at"(offset is 12)
  166. */
  167. ip = self_addr - (in_module(self_addr) ? 20 : 12);
  168. /*
  169. * search the text until finding the non-store instruction or "s{d,w}
  170. * ra, offset(sp)" instruction
  171. */
  172. do {
  173. ip -= 4;
  174. /* get the code at "ip": code = *(unsigned int *)ip; */
  175. safe_load_code(code, ip, faulted);
  176. if (unlikely(faulted))
  177. return 0;
  178. /*
  179. * If we hit the non-store instruction before finding where the
  180. * ra is stored, then this is a leaf function and it does not
  181. * store the ra on the stack
  182. */
  183. if ((code & S_R_SP) != S_R_SP)
  184. return parent_addr;
  185. } while (((code & S_RA_SP) != S_RA_SP));
  186. sp = fp + (code & OFFSET_MASK);
  187. /* ra = *(unsigned long *)sp; */
  188. safe_load_stack(ra, sp, faulted);
  189. if (unlikely(faulted))
  190. return 0;
  191. if (ra == parent)
  192. return sp;
  193. return 0;
  194. }
  195. #endif /* !KBUILD_MCOUNT_RA_ADDRESS */
  196. /*
  197. * Hook the return address and push it in the stack of return addrs
  198. * in current thread info.
  199. */
  200. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
  201. unsigned long fp)
  202. {
  203. unsigned long old;
  204. struct ftrace_graph_ent trace;
  205. unsigned long return_hooker = (unsigned long)
  206. &return_to_handler;
  207. int faulted;
  208. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  209. return;
  210. /*
  211. * "parent" is the stack address saved the return address of the caller
  212. * of _mcount.
  213. *
  214. * if the gcc < 4.5, a leaf function does not save the return address
  215. * in the stack address, so, we "emulate" one in _mcount's stack space,
  216. * and hijack it directly, but for a non-leaf function, it save the
  217. * return address to the its own stack space, we can not hijack it
  218. * directly, but need to find the real stack address,
  219. * ftrace_get_parent_addr() does it!
  220. *
  221. * if gcc>= 4.5, with the new -mmcount-ra-address option, for a
  222. * non-leaf function, the location of the return address will be saved
  223. * to $12 for us, and for a leaf function, only put a zero into $12. we
  224. * do it in ftrace_graph_caller of mcount.S.
  225. */
  226. /* old = *parent; */
  227. safe_load_stack(old, parent, faulted);
  228. if (unlikely(faulted))
  229. goto out;
  230. #ifndef KBUILD_MCOUNT_RA_ADDRESS
  231. parent = (unsigned long *)ftrace_get_parent_addr(self_addr, old,
  232. (unsigned long)parent, fp);
  233. /*
  234. * If fails when getting the stack address of the non-leaf function's
  235. * ra, stop function graph tracer and return
  236. */
  237. if (parent == 0)
  238. goto out;
  239. #endif
  240. /* *parent = return_hooker; */
  241. safe_store_stack(return_hooker, parent, faulted);
  242. if (unlikely(faulted))
  243. goto out;
  244. if (ftrace_push_return_trace(old, self_addr, &trace.depth, fp) ==
  245. -EBUSY) {
  246. *parent = old;
  247. return;
  248. }
  249. trace.func = self_addr;
  250. /* Only trace if the calling function expects to */
  251. if (!ftrace_graph_entry(&trace)) {
  252. current->curr_ret_stack--;
  253. *parent = old;
  254. }
  255. return;
  256. out:
  257. ftrace_graph_stop();
  258. WARN_ON(1);
  259. }
  260. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */