ftrace.c 11 KB

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