ftrace.c 8.4 KB

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