ftrace.c 9.4 KB

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