ftrace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 arch_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 arch_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:
  184. * We are paranoid about modifying text, as if a bug was to happen, it
  185. * could cause us to read or write to someplace that could cause harm.
  186. * Carefully read and modify the code with probe_kernel_*(), and make
  187. * sure what we read is what we expected it to be before modifying it.
  188. */
  189. /* read the text we want to modify */
  190. if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
  191. return -EFAULT;
  192. /* Make sure it is what we expect it to be */
  193. if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
  194. return -EINVAL;
  195. /* replace the text with the new text */
  196. if (do_ftrace_mod_code(ip, new_code))
  197. return -EPERM;
  198. flush_icache_range(ip, ip + MCOUNT_INSN_SIZE);
  199. return 0;
  200. }
  201. int ftrace_update_ftrace_func(ftrace_func_t func)
  202. {
  203. unsigned long ip = (unsigned long)(&ftrace_call) + MCOUNT_INSN_OFFSET;
  204. unsigned char old[MCOUNT_INSN_SIZE], *new;
  205. memcpy(old, (unsigned char *)ip, MCOUNT_INSN_SIZE);
  206. new = ftrace_call_replace(ip, (unsigned long)func);
  207. return ftrace_modify_code(ip, old, new);
  208. }
  209. int ftrace_make_nop(struct module *mod,
  210. struct dyn_ftrace *rec, unsigned long addr)
  211. {
  212. unsigned char *new, *old;
  213. unsigned long ip = rec->ip;
  214. old = ftrace_call_replace(ip, addr);
  215. new = ftrace_nop_replace(ip);
  216. return ftrace_modify_code(rec->ip, old, new);
  217. }
  218. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  219. {
  220. unsigned char *new, *old;
  221. unsigned long ip = rec->ip;
  222. old = ftrace_nop_replace(ip);
  223. new = ftrace_call_replace(ip, addr);
  224. return ftrace_modify_code(rec->ip, old, new);
  225. }
  226. int __init ftrace_dyn_arch_init(void)
  227. {
  228. return 0;
  229. }
  230. #endif /* CONFIG_DYNAMIC_FTRACE */
  231. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  232. #ifdef CONFIG_DYNAMIC_FTRACE
  233. extern void ftrace_graph_call(void);
  234. static int ftrace_mod(unsigned long ip, unsigned long old_addr,
  235. unsigned long new_addr)
  236. {
  237. unsigned char code[MCOUNT_INSN_SIZE];
  238. if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
  239. return -EFAULT;
  240. if (old_addr != __raw_readl((unsigned long *)code))
  241. return -EINVAL;
  242. __raw_writel(new_addr, ip);
  243. return 0;
  244. }
  245. int ftrace_enable_ftrace_graph_caller(void)
  246. {
  247. unsigned long ip, old_addr, new_addr;
  248. ip = (unsigned long)(&ftrace_graph_call) + GRAPH_INSN_OFFSET;
  249. old_addr = (unsigned long)(&skip_trace);
  250. new_addr = (unsigned long)(&ftrace_graph_caller);
  251. return ftrace_mod(ip, old_addr, new_addr);
  252. }
  253. int ftrace_disable_ftrace_graph_caller(void)
  254. {
  255. unsigned long ip, old_addr, new_addr;
  256. ip = (unsigned long)(&ftrace_graph_call) + GRAPH_INSN_OFFSET;
  257. old_addr = (unsigned long)(&ftrace_graph_caller);
  258. new_addr = (unsigned long)(&skip_trace);
  259. return ftrace_mod(ip, old_addr, new_addr);
  260. }
  261. #endif /* CONFIG_DYNAMIC_FTRACE */
  262. /*
  263. * Hook the return address and push it in the stack of return addrs
  264. * in the current thread info.
  265. *
  266. * This is the main routine for the function graph tracer. The function
  267. * graph tracer essentially works like this:
  268. *
  269. * parent is the stack address containing self_addr's return address.
  270. * We pull the real return address out of parent and store it in
  271. * current's ret_stack. Then, we replace the return address on the stack
  272. * with the address of return_to_handler. self_addr is the function that
  273. * called mcount.
  274. *
  275. * When self_addr returns, it will jump to return_to_handler which calls
  276. * ftrace_return_to_handler. ftrace_return_to_handler will pull the real
  277. * return address off of current's ret_stack and jump to it.
  278. */
  279. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
  280. {
  281. unsigned long old;
  282. int faulted, err;
  283. struct ftrace_graph_ent trace;
  284. unsigned long return_hooker = (unsigned long)&return_to_handler;
  285. if (unlikely(ftrace_graph_is_dead()))
  286. return;
  287. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  288. return;
  289. /*
  290. * Protect against fault, even if it shouldn't
  291. * happen. This tool is too much intrusive to
  292. * ignore such a protection.
  293. */
  294. __asm__ __volatile__(
  295. "1: \n\t"
  296. "mov.l @%2, %0 \n\t"
  297. "2: \n\t"
  298. "mov.l %3, @%2 \n\t"
  299. "mov #0, %1 \n\t"
  300. "3: \n\t"
  301. ".section .fixup, \"ax\" \n\t"
  302. "4: \n\t"
  303. "mov.l 5f, %0 \n\t"
  304. "jmp @%0 \n\t"
  305. " mov #1, %1 \n\t"
  306. ".balign 4 \n\t"
  307. "5: .long 3b \n\t"
  308. ".previous \n\t"
  309. ".section __ex_table,\"a\" \n\t"
  310. ".long 1b, 4b \n\t"
  311. ".long 2b, 4b \n\t"
  312. ".previous \n\t"
  313. : "=&r" (old), "=r" (faulted)
  314. : "r" (parent), "r" (return_hooker)
  315. );
  316. if (unlikely(faulted)) {
  317. ftrace_graph_stop();
  318. WARN_ON(1);
  319. return;
  320. }
  321. err = ftrace_push_return_trace(old, self_addr, &trace.depth, 0, NULL);
  322. if (err == -EBUSY) {
  323. __raw_writel(old, parent);
  324. return;
  325. }
  326. trace.func = self_addr;
  327. /* Only trace if the calling function expects to */
  328. if (!ftrace_graph_entry(&trace)) {
  329. current->curr_ret_stack--;
  330. __raw_writel(old, parent);
  331. }
  332. }
  333. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */