ftrace.c 6.9 KB

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