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