ftrace.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Code for replacing ftrace calls with jumps.
  3. *
  4. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  5. * Copyright (C) 2009 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/cacheflush.h>
  14. #include <asm/asm.h>
  15. #include <asm/asm-offsets.h>
  16. #ifdef CONFIG_DYNAMIC_FTRACE
  17. #define JAL 0x0c000000 /* jump & link: ip --> ra, jump to target */
  18. #define ADDR_MASK 0x03ffffff /* op_code|addr : 31...26|25 ....0 */
  19. #define jump_insn_encode(op_code, addr) \
  20. ((unsigned int)((op_code) | (((addr) >> 2) & ADDR_MASK)))
  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) jump_insn_encode(JAL, addr)
  25. static int ftrace_modify_code(unsigned long ip, unsigned int new_code)
  26. {
  27. int faulted;
  28. /* *(unsigned int *)ip = new_code; */
  29. safe_store_code(new_code, ip, faulted);
  30. if (unlikely(faulted))
  31. return -EFAULT;
  32. flush_icache_range(ip, ip + 8);
  33. return 0;
  34. }
  35. static int lui_v1;
  36. static int jal_mcount;
  37. int ftrace_make_nop(struct module *mod,
  38. struct dyn_ftrace *rec, unsigned long addr)
  39. {
  40. unsigned int new;
  41. int faulted;
  42. unsigned long ip = rec->ip;
  43. /* We have compiled module with -mlong-calls, but compiled the kernel
  44. * without it, we need to cope with them respectively. */
  45. if (ip & 0x40000000) {
  46. /* record it for ftrace_make_call */
  47. if (lui_v1 == 0) {
  48. /* lui_v1 = *(unsigned int *)ip; */
  49. safe_load_code(lui_v1, ip, faulted);
  50. if (unlikely(faulted))
  51. return -EFAULT;
  52. }
  53. #if defined(KBUILD_MCOUNT_RA_ADDRESS) && defined(CONFIG_32BIT)
  54. /* lui v1, hi_16bit_of_mcount --> b 1f (0x10000005)
  55. * addiu v1, v1, low_16bit_of_mcount
  56. * move at, ra
  57. * move $12, ra_address
  58. * jalr v1
  59. * sub sp, sp, 8
  60. * 1: offset = 5 instructions
  61. */
  62. new = INSN_B_1F_5;
  63. #else
  64. /* lui v1, hi_16bit_of_mcount --> b 1f (0x10000004)
  65. * addiu v1, v1, low_16bit_of_mcount
  66. * move at, ra
  67. * jalr v1
  68. * nop | move $12, ra_address | sub sp, sp, 8
  69. * 1: offset = 4 instructions
  70. */
  71. new = INSN_B_1F_4;
  72. #endif
  73. } else {
  74. /* record/calculate it for ftrace_make_call */
  75. if (jal_mcount == 0) {
  76. /* We can record it directly like this:
  77. * jal_mcount = *(unsigned int *)ip;
  78. * Herein, jump over the first two nop instructions */
  79. jal_mcount = INSN_JAL(MCOUNT_ADDR + 8);
  80. }
  81. /* move at, ra
  82. * jalr v1 --> nop
  83. */
  84. new = INSN_NOP;
  85. }
  86. return ftrace_modify_code(ip, new);
  87. }
  88. static int modified; /* initialized as 0 by default */
  89. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  90. {
  91. unsigned int new;
  92. unsigned long ip = rec->ip;
  93. /* We just need to remove the "b ftrace_stub" at the fist time! */
  94. if (modified == 0) {
  95. modified = 1;
  96. ftrace_modify_code(addr, INSN_NOP);
  97. }
  98. /* ip, module: 0xc0000000, kernel: 0x80000000 */
  99. new = (ip & 0x40000000) ? lui_v1 : jal_mcount;
  100. return ftrace_modify_code(ip, new);
  101. }
  102. #define FTRACE_CALL_IP ((unsigned long)(&ftrace_call))
  103. int ftrace_update_ftrace_func(ftrace_func_t func)
  104. {
  105. unsigned int new;
  106. new = INSN_JAL((unsigned long)func);
  107. return ftrace_modify_code(FTRACE_CALL_IP, new);
  108. }
  109. int __init ftrace_dyn_arch_init(void *data)
  110. {
  111. /* The return code is retured via data */
  112. *(unsigned long *)data = 0;
  113. return 0;
  114. }
  115. #endif /* CONFIG_DYNAMIC_FTRACE */
  116. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  117. #ifdef CONFIG_DYNAMIC_FTRACE
  118. extern void ftrace_graph_call(void);
  119. #define JMP 0x08000000 /* jump to target directly */
  120. #define CALL_FTRACE_GRAPH_CALLER \
  121. jump_insn_encode(JMP, (unsigned long)(&ftrace_graph_caller))
  122. #define FTRACE_GRAPH_CALL_IP ((unsigned long)(&ftrace_graph_call))
  123. int ftrace_enable_ftrace_graph_caller(void)
  124. {
  125. return ftrace_modify_code(FTRACE_GRAPH_CALL_IP,
  126. CALL_FTRACE_GRAPH_CALLER);
  127. }
  128. int ftrace_disable_ftrace_graph_caller(void)
  129. {
  130. return ftrace_modify_code(FTRACE_GRAPH_CALL_IP, INSN_NOP);
  131. }
  132. #endif /* !CONFIG_DYNAMIC_FTRACE */
  133. #ifndef KBUILD_MCOUNT_RA_ADDRESS
  134. #define S_RA_SP (0xafbf << 16) /* s{d,w} ra, offset(sp) */
  135. #define S_R_SP (0xafb0 << 16) /* s{d,w} R, offset(sp) */
  136. #define OFFSET_MASK 0xffff /* stack offset range: 0 ~ PT_SIZE */
  137. unsigned long ftrace_get_parent_addr(unsigned long self_addr,
  138. unsigned long parent,
  139. unsigned long parent_addr,
  140. unsigned long fp)
  141. {
  142. unsigned long sp, ip, ra;
  143. unsigned int code;
  144. int faulted;
  145. /* in module or kernel? */
  146. if (self_addr & 0x40000000) {
  147. /* module: move to the instruction "lui v1, HI_16BIT_OF_MCOUNT" */
  148. ip = self_addr - 20;
  149. } else {
  150. /* kernel: move to the instruction "move ra, at" */
  151. ip = self_addr - 12;
  152. }
  153. /* search the text until finding the non-store instruction or "s{d,w}
  154. * ra, offset(sp)" instruction */
  155. do {
  156. ip -= 4;
  157. /* get the code at "ip": code = *(unsigned int *)ip; */
  158. safe_load_code(code, ip, faulted);
  159. if (unlikely(faulted))
  160. return 0;
  161. /* If we hit the non-store instruction before finding where the
  162. * ra is stored, then this is a leaf function and it does not
  163. * store the ra on the stack. */
  164. if ((code & S_R_SP) != S_R_SP)
  165. return parent_addr;
  166. } while (((code & S_RA_SP) != S_RA_SP));
  167. sp = fp + (code & OFFSET_MASK);
  168. /* ra = *(unsigned long *)sp; */
  169. safe_load_stack(ra, sp, faulted);
  170. if (unlikely(faulted))
  171. return 0;
  172. if (ra == parent)
  173. return sp;
  174. return 0;
  175. }
  176. #endif
  177. /*
  178. * Hook the return address and push it in the stack of return addrs
  179. * in current thread info.
  180. */
  181. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
  182. unsigned long fp)
  183. {
  184. unsigned long old;
  185. struct ftrace_graph_ent trace;
  186. unsigned long return_hooker = (unsigned long)
  187. &return_to_handler;
  188. int faulted;
  189. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  190. return;
  191. /* "parent" is the stack address saved the return address of the caller
  192. * of _mcount.
  193. *
  194. * if the gcc < 4.5, a leaf function does not save the return address
  195. * in the stack address, so, we "emulate" one in _mcount's stack space,
  196. * and hijack it directly, but for a non-leaf function, it save the
  197. * return address to the its own stack space, we can not hijack it
  198. * directly, but need to find the real stack address,
  199. * ftrace_get_parent_addr() does it!
  200. *
  201. * if gcc>= 4.5, with the new -mmcount-ra-address option, for a
  202. * non-leaf function, the location of the return address will be saved
  203. * to $12 for us, and for a leaf function, only put a zero into $12. we
  204. * do it in ftrace_graph_caller of mcount.S.
  205. */
  206. /* old = *parent; */
  207. safe_load_stack(old, parent, faulted);
  208. if (unlikely(faulted))
  209. goto out;
  210. #ifndef KBUILD_MCOUNT_RA_ADDRESS
  211. parent = (unsigned long *)ftrace_get_parent_addr(self_addr, old,
  212. (unsigned long)parent,
  213. fp);
  214. /* If fails when getting the stack address of the non-leaf function's
  215. * ra, stop function graph tracer and return */
  216. if (parent == 0)
  217. goto out;
  218. #endif
  219. /* *parent = return_hooker; */
  220. safe_store_stack(return_hooker, parent, faulted);
  221. if (unlikely(faulted))
  222. goto out;
  223. if (ftrace_push_return_trace(old, self_addr, &trace.depth, fp) ==
  224. -EBUSY) {
  225. *parent = old;
  226. return;
  227. }
  228. trace.func = self_addr;
  229. /* Only trace if the calling function expects to */
  230. if (!ftrace_graph_entry(&trace)) {
  231. current->curr_ret_stack--;
  232. *parent = old;
  233. }
  234. return;
  235. out:
  236. ftrace_graph_stop();
  237. WARN_ON(1);
  238. }
  239. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */