ftrace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * Code for replacing ftrace calls with jumps.
  3. *
  4. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  5. *
  6. * Thanks goes to Ingo Molnar, for suggesting the idea.
  7. * Mathieu Desnoyers, for suggesting postponing the modifications.
  8. * Arjan van de Ven, for keeping me straight, and explaining to me
  9. * the dangers of modifying code on the run.
  10. */
  11. #include <linux/spinlock.h>
  12. #include <linux/hardirq.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/ftrace.h>
  15. #include <linux/percpu.h>
  16. #include <linux/sched.h>
  17. #include <linux/init.h>
  18. #include <linux/list.h>
  19. #include <asm/ftrace.h>
  20. #include <linux/ftrace.h>
  21. #include <asm/nops.h>
  22. #include <asm/nmi.h>
  23. #ifdef CONFIG_FUNCTION_RET_TRACER
  24. /*
  25. * These functions are picked from those used on
  26. * this page for dynamic ftrace. They have been
  27. * simplified to ignore all traces in NMI context.
  28. */
  29. static atomic_t in_nmi;
  30. void ftrace_nmi_enter(void)
  31. {
  32. atomic_inc(&in_nmi);
  33. }
  34. void ftrace_nmi_exit(void)
  35. {
  36. atomic_dec(&in_nmi);
  37. }
  38. /* Add a function return address to the trace stack on thread info.*/
  39. static int push_return_trace(unsigned long ret, unsigned long long time,
  40. unsigned long func)
  41. {
  42. int index;
  43. struct thread_info *ti = current_thread_info();
  44. /* The return trace stack is full */
  45. if (ti->curr_ret_stack == FTRACE_RET_STACK_SIZE - 1)
  46. return -EBUSY;
  47. index = ++ti->curr_ret_stack;
  48. ti->ret_stack[index].ret = ret;
  49. ti->ret_stack[index].func = func;
  50. ti->ret_stack[index].calltime = time;
  51. return 0;
  52. }
  53. /* Retrieve a function return address to the trace stack on thread info.*/
  54. static void pop_return_trace(unsigned long *ret, unsigned long long *time,
  55. unsigned long *func)
  56. {
  57. int index;
  58. struct thread_info *ti = current_thread_info();
  59. index = ti->curr_ret_stack;
  60. *ret = ti->ret_stack[index].ret;
  61. *func = ti->ret_stack[index].func;
  62. *time = ti->ret_stack[index].calltime;
  63. ti->curr_ret_stack--;
  64. }
  65. /*
  66. * Send the trace to the ring-buffer.
  67. * @return the original return address.
  68. */
  69. unsigned long ftrace_return_to_handler(void)
  70. {
  71. struct ftrace_retfunc trace;
  72. pop_return_trace(&trace.ret, &trace.calltime, &trace.func);
  73. trace.rettime = cpu_clock(raw_smp_processor_id());
  74. ftrace_function_return(&trace);
  75. return trace.ret;
  76. }
  77. /*
  78. * Hook the return address and push it in the stack of return addrs
  79. * in current thread info.
  80. */
  81. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
  82. {
  83. unsigned long old;
  84. unsigned long long calltime;
  85. int faulted;
  86. unsigned long return_hooker = (unsigned long)
  87. &return_to_handler;
  88. /* Nmi's are currently unsupported */
  89. if (atomic_read(&in_nmi))
  90. return;
  91. /*
  92. * Protect against fault, even if it shouldn't
  93. * happen. This tool is too much intrusive to
  94. * ignore such a protection.
  95. */
  96. asm volatile(
  97. "1: movl (%[parent_old]), %[old]\n"
  98. "2: movl %[return_hooker], (%[parent_replaced])\n"
  99. " movl $0, %[faulted]\n"
  100. ".section .fixup, \"ax\"\n"
  101. "3: movl $1, %[faulted]\n"
  102. ".previous\n"
  103. ".section __ex_table, \"a\"\n"
  104. " .long 1b, 3b\n"
  105. " .long 2b, 3b\n"
  106. ".previous\n"
  107. : [parent_replaced] "=r" (parent), [old] "=r" (old),
  108. [faulted] "=r" (faulted)
  109. : [parent_old] "0" (parent), [return_hooker] "r" (return_hooker)
  110. : "memory"
  111. );
  112. if (WARN_ON(faulted)) {
  113. unregister_ftrace_return();
  114. return;
  115. }
  116. if (WARN_ON(!__kernel_text_address(old))) {
  117. unregister_ftrace_return();
  118. *parent = old;
  119. return;
  120. }
  121. calltime = cpu_clock(raw_smp_processor_id());
  122. if (push_return_trace(old, calltime, self_addr) == -EBUSY)
  123. *parent = old;
  124. }
  125. #endif
  126. #ifdef CONFIG_DYNAMIC_FTRACE
  127. union ftrace_code_union {
  128. char code[MCOUNT_INSN_SIZE];
  129. struct {
  130. char e8;
  131. int offset;
  132. } __attribute__((packed));
  133. };
  134. static int ftrace_calc_offset(long ip, long addr)
  135. {
  136. return (int)(addr - ip);
  137. }
  138. static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
  139. {
  140. static union ftrace_code_union calc;
  141. calc.e8 = 0xe8;
  142. calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
  143. /*
  144. * No locking needed, this must be called via kstop_machine
  145. * which in essence is like running on a uniprocessor machine.
  146. */
  147. return calc.code;
  148. }
  149. /*
  150. * Modifying code must take extra care. On an SMP machine, if
  151. * the code being modified is also being executed on another CPU
  152. * that CPU will have undefined results and possibly take a GPF.
  153. * We use kstop_machine to stop other CPUS from exectuing code.
  154. * But this does not stop NMIs from happening. We still need
  155. * to protect against that. We separate out the modification of
  156. * the code to take care of this.
  157. *
  158. * Two buffers are added: An IP buffer and a "code" buffer.
  159. *
  160. * 1) Put the instruction pointer into the IP buffer
  161. * and the new code into the "code" buffer.
  162. * 2) Set a flag that says we are modifying code
  163. * 3) Wait for any running NMIs to finish.
  164. * 4) Write the code
  165. * 5) clear the flag.
  166. * 6) Wait for any running NMIs to finish.
  167. *
  168. * If an NMI is executed, the first thing it does is to call
  169. * "ftrace_nmi_enter". This will check if the flag is set to write
  170. * and if it is, it will write what is in the IP and "code" buffers.
  171. *
  172. * The trick is, it does not matter if everyone is writing the same
  173. * content to the code location. Also, if a CPU is executing code
  174. * it is OK to write to that code location if the contents being written
  175. * are the same as what exists.
  176. */
  177. static atomic_t in_nmi = ATOMIC_INIT(0);
  178. static int mod_code_status; /* holds return value of text write */
  179. static int mod_code_write; /* set when NMI should do the write */
  180. static void *mod_code_ip; /* holds the IP to write to */
  181. static void *mod_code_newcode; /* holds the text to write to the IP */
  182. static unsigned nmi_wait_count;
  183. static atomic_t nmi_update_count = ATOMIC_INIT(0);
  184. int ftrace_arch_read_dyn_info(char *buf, int size)
  185. {
  186. int r;
  187. r = snprintf(buf, size, "%u %u",
  188. nmi_wait_count,
  189. atomic_read(&nmi_update_count));
  190. return r;
  191. }
  192. static void ftrace_mod_code(void)
  193. {
  194. /*
  195. * Yes, more than one CPU process can be writing to mod_code_status.
  196. * (and the code itself)
  197. * But if one were to fail, then they all should, and if one were
  198. * to succeed, then they all should.
  199. */
  200. mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode,
  201. MCOUNT_INSN_SIZE);
  202. }
  203. void ftrace_nmi_enter(void)
  204. {
  205. atomic_inc(&in_nmi);
  206. /* Must have in_nmi seen before reading write flag */
  207. smp_mb();
  208. if (mod_code_write) {
  209. ftrace_mod_code();
  210. atomic_inc(&nmi_update_count);
  211. }
  212. }
  213. void ftrace_nmi_exit(void)
  214. {
  215. /* Finish all executions before clearing in_nmi */
  216. smp_wmb();
  217. atomic_dec(&in_nmi);
  218. }
  219. static void wait_for_nmi(void)
  220. {
  221. int waited = 0;
  222. while (atomic_read(&in_nmi)) {
  223. waited = 1;
  224. cpu_relax();
  225. }
  226. if (waited)
  227. nmi_wait_count++;
  228. }
  229. static int
  230. do_ftrace_mod_code(unsigned long ip, void *new_code)
  231. {
  232. mod_code_ip = (void *)ip;
  233. mod_code_newcode = new_code;
  234. /* The buffers need to be visible before we let NMIs write them */
  235. smp_wmb();
  236. mod_code_write = 1;
  237. /* Make sure write bit is visible before we wait on NMIs */
  238. smp_mb();
  239. wait_for_nmi();
  240. /* Make sure all running NMIs have finished before we write the code */
  241. smp_mb();
  242. ftrace_mod_code();
  243. /* Make sure the write happens before clearing the bit */
  244. smp_wmb();
  245. mod_code_write = 0;
  246. /* make sure NMIs see the cleared bit */
  247. smp_mb();
  248. wait_for_nmi();
  249. return mod_code_status;
  250. }
  251. static unsigned char ftrace_nop[MCOUNT_INSN_SIZE];
  252. static unsigned char *ftrace_nop_replace(void)
  253. {
  254. return ftrace_nop;
  255. }
  256. static int
  257. ftrace_modify_code(unsigned long ip, unsigned char *old_code,
  258. unsigned char *new_code)
  259. {
  260. unsigned char replaced[MCOUNT_INSN_SIZE];
  261. /*
  262. * Note: Due to modules and __init, code can
  263. * disappear and change, we need to protect against faulting
  264. * as well as code changing. We do this by using the
  265. * probe_kernel_* functions.
  266. *
  267. * No real locking needed, this code is run through
  268. * kstop_machine, or before SMP starts.
  269. */
  270. /* read the text we want to modify */
  271. if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
  272. return -EFAULT;
  273. /* Make sure it is what we expect it to be */
  274. if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
  275. return -EINVAL;
  276. /* replace the text with the new text */
  277. if (do_ftrace_mod_code(ip, new_code))
  278. return -EPERM;
  279. sync_core();
  280. return 0;
  281. }
  282. int ftrace_make_nop(struct module *mod,
  283. struct dyn_ftrace *rec, unsigned long addr)
  284. {
  285. unsigned char *new, *old;
  286. unsigned long ip = rec->ip;
  287. old = ftrace_call_replace(ip, addr);
  288. new = ftrace_nop_replace();
  289. return ftrace_modify_code(rec->ip, old, new);
  290. }
  291. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  292. {
  293. unsigned char *new, *old;
  294. unsigned long ip = rec->ip;
  295. old = ftrace_nop_replace();
  296. new = ftrace_call_replace(ip, addr);
  297. return ftrace_modify_code(rec->ip, old, new);
  298. }
  299. int ftrace_update_ftrace_func(ftrace_func_t func)
  300. {
  301. unsigned long ip = (unsigned long)(&ftrace_call);
  302. unsigned char old[MCOUNT_INSN_SIZE], *new;
  303. int ret;
  304. memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
  305. new = ftrace_call_replace(ip, (unsigned long)func);
  306. ret = ftrace_modify_code(ip, old, new);
  307. return ret;
  308. }
  309. int __init ftrace_dyn_arch_init(void *data)
  310. {
  311. extern const unsigned char ftrace_test_p6nop[];
  312. extern const unsigned char ftrace_test_nop5[];
  313. extern const unsigned char ftrace_test_jmp[];
  314. int faulted = 0;
  315. /*
  316. * There is no good nop for all x86 archs.
  317. * We will default to using the P6_NOP5, but first we
  318. * will test to make sure that the nop will actually
  319. * work on this CPU. If it faults, we will then
  320. * go to a lesser efficient 5 byte nop. If that fails
  321. * we then just use a jmp as our nop. This isn't the most
  322. * efficient nop, but we can not use a multi part nop
  323. * since we would then risk being preempted in the middle
  324. * of that nop, and if we enabled tracing then, it might
  325. * cause a system crash.
  326. *
  327. * TODO: check the cpuid to determine the best nop.
  328. */
  329. asm volatile (
  330. "ftrace_test_jmp:"
  331. "jmp ftrace_test_p6nop\n"
  332. "nop\n"
  333. "nop\n"
  334. "nop\n" /* 2 byte jmp + 3 bytes */
  335. "ftrace_test_p6nop:"
  336. P6_NOP5
  337. "jmp 1f\n"
  338. "ftrace_test_nop5:"
  339. ".byte 0x66,0x66,0x66,0x66,0x90\n"
  340. "1:"
  341. ".section .fixup, \"ax\"\n"
  342. "2: movl $1, %0\n"
  343. " jmp ftrace_test_nop5\n"
  344. "3: movl $2, %0\n"
  345. " jmp 1b\n"
  346. ".previous\n"
  347. _ASM_EXTABLE(ftrace_test_p6nop, 2b)
  348. _ASM_EXTABLE(ftrace_test_nop5, 3b)
  349. : "=r"(faulted) : "0" (faulted));
  350. switch (faulted) {
  351. case 0:
  352. pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
  353. memcpy(ftrace_nop, ftrace_test_p6nop, MCOUNT_INSN_SIZE);
  354. break;
  355. case 1:
  356. pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
  357. memcpy(ftrace_nop, ftrace_test_nop5, MCOUNT_INSN_SIZE);
  358. break;
  359. case 2:
  360. pr_info("ftrace: converting mcount calls to jmp . + 5\n");
  361. memcpy(ftrace_nop, ftrace_test_jmp, MCOUNT_INSN_SIZE);
  362. break;
  363. }
  364. /* The return code is retured via data */
  365. *(unsigned long *)data = 0;
  366. return 0;
  367. }
  368. #endif