ftrace.c 10 KB

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