ftrace.c 10 KB

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