ftrace.c 8.5 KB

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