ftrace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Copyright (C) 2008 Matt Fleming <matt@console-pimps.org>
  3. * Copyright (C) 2008 Paul Mundt <lethal@linux-sh.org>
  4. *
  5. * Code for replacing ftrace calls with jumps.
  6. *
  7. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  8. *
  9. * Thanks goes to Ingo Molnar, for suggesting the idea.
  10. * Mathieu Desnoyers, for suggesting postponing the modifications.
  11. * Arjan van de Ven, for keeping me straight, and explaining to me
  12. * the dangers of modifying code on the run.
  13. */
  14. #include <linux/uaccess.h>
  15. #include <linux/ftrace.h>
  16. #include <linux/string.h>
  17. #include <linux/init.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <asm/ftrace.h>
  21. #include <asm/cacheflush.h>
  22. #include <asm/unistd.h>
  23. #include <trace/syscall.h>
  24. #ifdef CONFIG_DYNAMIC_FTRACE
  25. static unsigned char ftrace_replaced_code[MCOUNT_INSN_SIZE];
  26. static unsigned char ftrace_nop[4];
  27. /*
  28. * If we're trying to nop out a call to a function, we instead
  29. * place a call to the address after the memory table.
  30. *
  31. * 8c011060 <a>:
  32. * 8c011060: 02 d1 mov.l 8c01106c <a+0xc>,r1
  33. * 8c011062: 22 4f sts.l pr,@-r15
  34. * 8c011064: 02 c7 mova 8c011070 <a+0x10>,r0
  35. * 8c011066: 2b 41 jmp @r1
  36. * 8c011068: 2a 40 lds r0,pr
  37. * 8c01106a: 09 00 nop
  38. * 8c01106c: 68 24 .word 0x2468 <--- ip
  39. * 8c01106e: 1d 8c .word 0x8c1d
  40. * 8c011070: 26 4f lds.l @r15+,pr <--- ip + MCOUNT_INSN_SIZE
  41. *
  42. * We write 0x8c011070 to 0x8c01106c so that on entry to a() we branch
  43. * past the _mcount call and continue executing code like normal.
  44. */
  45. static unsigned char *ftrace_nop_replace(unsigned long ip)
  46. {
  47. __raw_writel(ip + MCOUNT_INSN_SIZE, ftrace_nop);
  48. return ftrace_nop;
  49. }
  50. static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
  51. {
  52. /* Place the address in the memory table. */
  53. __raw_writel(addr, ftrace_replaced_code);
  54. /*
  55. * No locking needed, this must be called via kstop_machine
  56. * which in essence is like running on a uniprocessor machine.
  57. */
  58. return ftrace_replaced_code;
  59. }
  60. /*
  61. * Modifying code must take extra care. On an SMP machine, if
  62. * the code being modified is also being executed on another CPU
  63. * that CPU will have undefined results and possibly take a GPF.
  64. * We use kstop_machine to stop other CPUS from exectuing code.
  65. * But this does not stop NMIs from happening. We still need
  66. * to protect against that. We separate out the modification of
  67. * the code to take care of this.
  68. *
  69. * Two buffers are added: An IP buffer and a "code" buffer.
  70. *
  71. * 1) Put the instruction pointer into the IP buffer
  72. * and the new code into the "code" buffer.
  73. * 2) Wait for any running NMIs to finish and set a flag that says
  74. * we are modifying code, it is done in an atomic operation.
  75. * 3) Write the code
  76. * 4) clear the flag.
  77. * 5) Wait for any running NMIs to finish.
  78. *
  79. * If an NMI is executed, the first thing it does is to call
  80. * "ftrace_nmi_enter". This will check if the flag is set to write
  81. * and if it is, it will write what is in the IP and "code" buffers.
  82. *
  83. * The trick is, it does not matter if everyone is writing the same
  84. * content to the code location. Also, if a CPU is executing code
  85. * it is OK to write to that code location if the contents being written
  86. * are the same as what exists.
  87. */
  88. #define MOD_CODE_WRITE_FLAG (1 << 31) /* set when NMI should do the write */
  89. static atomic_t nmi_running = ATOMIC_INIT(0);
  90. static int mod_code_status; /* holds return value of text write */
  91. static void *mod_code_ip; /* holds the IP to write to */
  92. static void *mod_code_newcode; /* holds the text to write to the IP */
  93. static unsigned nmi_wait_count;
  94. static atomic_t nmi_update_count = ATOMIC_INIT(0);
  95. int ftrace_arch_read_dyn_info(char *buf, int size)
  96. {
  97. int r;
  98. r = snprintf(buf, size, "%u %u",
  99. nmi_wait_count,
  100. atomic_read(&nmi_update_count));
  101. return r;
  102. }
  103. static void clear_mod_flag(void)
  104. {
  105. int old = atomic_read(&nmi_running);
  106. for (;;) {
  107. int new = old & ~MOD_CODE_WRITE_FLAG;
  108. if (old == new)
  109. break;
  110. old = atomic_cmpxchg(&nmi_running, old, new);
  111. }
  112. }
  113. static void ftrace_mod_code(void)
  114. {
  115. /*
  116. * Yes, more than one CPU process can be writing to mod_code_status.
  117. * (and the code itself)
  118. * But if one were to fail, then they all should, and if one were
  119. * to succeed, then they all should.
  120. */
  121. mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode,
  122. MCOUNT_INSN_SIZE);
  123. /* if we fail, then kill any new writers */
  124. if (mod_code_status)
  125. clear_mod_flag();
  126. }
  127. void ftrace_nmi_enter(void)
  128. {
  129. if (atomic_inc_return(&nmi_running) & MOD_CODE_WRITE_FLAG) {
  130. smp_rmb();
  131. ftrace_mod_code();
  132. atomic_inc(&nmi_update_count);
  133. }
  134. /* Must have previous changes seen before executions */
  135. smp_mb();
  136. }
  137. void ftrace_nmi_exit(void)
  138. {
  139. /* Finish all executions before clearing nmi_running */
  140. smp_mb();
  141. atomic_dec(&nmi_running);
  142. }
  143. static void wait_for_nmi_and_set_mod_flag(void)
  144. {
  145. if (!atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG))
  146. return;
  147. do {
  148. cpu_relax();
  149. } while (atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG));
  150. nmi_wait_count++;
  151. }
  152. static void wait_for_nmi(void)
  153. {
  154. if (!atomic_read(&nmi_running))
  155. return;
  156. do {
  157. cpu_relax();
  158. } while (atomic_read(&nmi_running));
  159. nmi_wait_count++;
  160. }
  161. static int
  162. do_ftrace_mod_code(unsigned long ip, void *new_code)
  163. {
  164. mod_code_ip = (void *)ip;
  165. mod_code_newcode = new_code;
  166. /* The buffers need to be visible before we let NMIs write them */
  167. smp_mb();
  168. wait_for_nmi_and_set_mod_flag();
  169. /* Make sure all running NMIs have finished before we write the code */
  170. smp_mb();
  171. ftrace_mod_code();
  172. /* Make sure the write happens before clearing the bit */
  173. smp_mb();
  174. clear_mod_flag();
  175. wait_for_nmi();
  176. return mod_code_status;
  177. }
  178. static int ftrace_modify_code(unsigned long ip, unsigned char *old_code,
  179. unsigned char *new_code)
  180. {
  181. unsigned char replaced[MCOUNT_INSN_SIZE];
  182. /*
  183. * Note: Due to modules and __init, code can
  184. * disappear and change, we need to protect against faulting
  185. * as well as code changing. We do this by using the
  186. * probe_kernel_* functions.
  187. *
  188. * No real locking needed, this code is run through
  189. * kstop_machine, or before SMP starts.
  190. */
  191. /* read the text we want to modify */
  192. if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
  193. return -EFAULT;
  194. /* Make sure it is what we expect it to be */
  195. if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
  196. return -EINVAL;
  197. /* replace the text with the new text */
  198. if (do_ftrace_mod_code(ip, new_code))
  199. return -EPERM;
  200. flush_icache_range(ip, ip + MCOUNT_INSN_SIZE);
  201. return 0;
  202. }
  203. int ftrace_update_ftrace_func(ftrace_func_t func)
  204. {
  205. unsigned long ip = (unsigned long)(&ftrace_call) + MCOUNT_INSN_OFFSET;
  206. unsigned char old[MCOUNT_INSN_SIZE], *new;
  207. memcpy(old, (unsigned char *)ip, MCOUNT_INSN_SIZE);
  208. new = ftrace_call_replace(ip, (unsigned long)func);
  209. return ftrace_modify_code(ip, old, new);
  210. }
  211. int ftrace_make_nop(struct module *mod,
  212. struct dyn_ftrace *rec, unsigned long addr)
  213. {
  214. unsigned char *new, *old;
  215. unsigned long ip = rec->ip;
  216. old = ftrace_call_replace(ip, addr);
  217. new = ftrace_nop_replace(ip);
  218. return ftrace_modify_code(rec->ip, old, new);
  219. }
  220. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  221. {
  222. unsigned char *new, *old;
  223. unsigned long ip = rec->ip;
  224. old = ftrace_nop_replace(ip);
  225. new = ftrace_call_replace(ip, addr);
  226. return ftrace_modify_code(rec->ip, old, new);
  227. }
  228. int __init ftrace_dyn_arch_init(void)
  229. {
  230. return 0;
  231. }
  232. #endif /* CONFIG_DYNAMIC_FTRACE */
  233. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  234. #ifdef CONFIG_DYNAMIC_FTRACE
  235. extern void ftrace_graph_call(void);
  236. static int ftrace_mod(unsigned long ip, unsigned long old_addr,
  237. unsigned long new_addr)
  238. {
  239. unsigned char code[MCOUNT_INSN_SIZE];
  240. if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
  241. return -EFAULT;
  242. if (old_addr != __raw_readl((unsigned long *)code))
  243. return -EINVAL;
  244. __raw_writel(new_addr, ip);
  245. return 0;
  246. }
  247. int ftrace_enable_ftrace_graph_caller(void)
  248. {
  249. unsigned long ip, old_addr, new_addr;
  250. ip = (unsigned long)(&ftrace_graph_call) + GRAPH_INSN_OFFSET;
  251. old_addr = (unsigned long)(&skip_trace);
  252. new_addr = (unsigned long)(&ftrace_graph_caller);
  253. return ftrace_mod(ip, old_addr, new_addr);
  254. }
  255. int ftrace_disable_ftrace_graph_caller(void)
  256. {
  257. unsigned long ip, old_addr, new_addr;
  258. ip = (unsigned long)(&ftrace_graph_call) + GRAPH_INSN_OFFSET;
  259. old_addr = (unsigned long)(&ftrace_graph_caller);
  260. new_addr = (unsigned long)(&skip_trace);
  261. return ftrace_mod(ip, old_addr, new_addr);
  262. }
  263. #endif /* CONFIG_DYNAMIC_FTRACE */
  264. /*
  265. * Hook the return address and push it in the stack of return addrs
  266. * in the current thread info.
  267. *
  268. * This is the main routine for the function graph tracer. The function
  269. * graph tracer essentially works like this:
  270. *
  271. * parent is the stack address containing self_addr's return address.
  272. * We pull the real return address out of parent and store it in
  273. * current's ret_stack. Then, we replace the return address on the stack
  274. * with the address of return_to_handler. self_addr is the function that
  275. * called mcount.
  276. *
  277. * When self_addr returns, it will jump to return_to_handler which calls
  278. * ftrace_return_to_handler. ftrace_return_to_handler will pull the real
  279. * return address off of current's ret_stack and jump to it.
  280. */
  281. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
  282. {
  283. unsigned long old;
  284. int faulted, err;
  285. struct ftrace_graph_ent trace;
  286. unsigned long return_hooker = (unsigned long)&return_to_handler;
  287. if (unlikely(ftrace_graph_is_dead()))
  288. return;
  289. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  290. return;
  291. /*
  292. * Protect against fault, even if it shouldn't
  293. * happen. This tool is too much intrusive to
  294. * ignore such a protection.
  295. */
  296. __asm__ __volatile__(
  297. "1: \n\t"
  298. "mov.l @%2, %0 \n\t"
  299. "2: \n\t"
  300. "mov.l %3, @%2 \n\t"
  301. "mov #0, %1 \n\t"
  302. "3: \n\t"
  303. ".section .fixup, \"ax\" \n\t"
  304. "4: \n\t"
  305. "mov.l 5f, %0 \n\t"
  306. "jmp @%0 \n\t"
  307. " mov #1, %1 \n\t"
  308. ".balign 4 \n\t"
  309. "5: .long 3b \n\t"
  310. ".previous \n\t"
  311. ".section __ex_table,\"a\" \n\t"
  312. ".long 1b, 4b \n\t"
  313. ".long 2b, 4b \n\t"
  314. ".previous \n\t"
  315. : "=&r" (old), "=r" (faulted)
  316. : "r" (parent), "r" (return_hooker)
  317. );
  318. if (unlikely(faulted)) {
  319. ftrace_graph_stop();
  320. WARN_ON(1);
  321. return;
  322. }
  323. err = ftrace_push_return_trace(old, self_addr, &trace.depth, 0);
  324. if (err == -EBUSY) {
  325. __raw_writel(old, parent);
  326. return;
  327. }
  328. trace.func = self_addr;
  329. /* Only trace if the calling function expects to */
  330. if (!ftrace_graph_entry(&trace)) {
  331. current->curr_ret_stack--;
  332. __raw_writel(old, parent);
  333. }
  334. }
  335. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */