ftrace.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/init.h>
  17. #include <linux/list.h>
  18. #include <asm/ftrace.h>
  19. #include <asm/nops.h>
  20. /* Long is fine, even if it is only 4 bytes ;-) */
  21. static unsigned long *ftrace_nop;
  22. union ftrace_code_union {
  23. char code[MCOUNT_INSN_SIZE];
  24. struct {
  25. char e8;
  26. int offset;
  27. } __attribute__((packed));
  28. };
  29. static int notrace ftrace_calc_offset(long ip, long addr)
  30. {
  31. return (int)(addr - ip);
  32. }
  33. notrace unsigned char *ftrace_nop_replace(void)
  34. {
  35. return (char *)ftrace_nop;
  36. }
  37. notrace unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
  38. {
  39. static union ftrace_code_union calc;
  40. calc.e8 = 0xe8;
  41. calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
  42. /*
  43. * No locking needed, this must be called via kstop_machine
  44. * which in essence is like running on a uniprocessor machine.
  45. */
  46. return calc.code;
  47. }
  48. notrace int
  49. ftrace_modify_code(unsigned long ip, unsigned char *old_code,
  50. unsigned char *new_code)
  51. {
  52. unsigned char replaced[MCOUNT_INSN_SIZE];
  53. /*
  54. * Note: Due to modules and __init, code can
  55. * disappear and change, we need to protect against faulting
  56. * as well as code changing.
  57. *
  58. * No real locking needed, this code is run through
  59. * kstop_machine, or before SMP starts.
  60. */
  61. if (__copy_from_user_inatomic(replaced, (char __user *)ip, MCOUNT_INSN_SIZE))
  62. return 1;
  63. if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
  64. return 2;
  65. WARN_ON_ONCE(__copy_to_user_inatomic((char __user *)ip, new_code,
  66. MCOUNT_INSN_SIZE));
  67. sync_core();
  68. return 0;
  69. }
  70. notrace int ftrace_update_ftrace_func(ftrace_func_t func)
  71. {
  72. unsigned long ip = (unsigned long)(&ftrace_call);
  73. unsigned char old[MCOUNT_INSN_SIZE], *new;
  74. int ret;
  75. memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
  76. new = ftrace_call_replace(ip, (unsigned long)func);
  77. ret = ftrace_modify_code(ip, old, new);
  78. return ret;
  79. }
  80. notrace int ftrace_mcount_set(unsigned long *data)
  81. {
  82. /* mcount is initialized as a nop */
  83. *data = 0;
  84. return 0;
  85. }
  86. int __init ftrace_dyn_arch_init(void *data)
  87. {
  88. extern const unsigned char ftrace_test_p6nop[];
  89. extern const unsigned char ftrace_test_nop5[];
  90. extern const unsigned char ftrace_test_jmp[];
  91. int faulted = 0;
  92. /*
  93. * There is no good nop for all x86 archs.
  94. * We will default to using the P6_NOP5, but first we
  95. * will test to make sure that the nop will actually
  96. * work on this CPU. If it faults, we will then
  97. * go to a lesser efficient 5 byte nop. If that fails
  98. * we then just use a jmp as our nop. This isn't the most
  99. * efficient nop, but we can not use a multi part nop
  100. * since we would then risk being preempted in the middle
  101. * of that nop, and if we enabled tracing then, it might
  102. * cause a system crash.
  103. *
  104. * TODO: check the cpuid to determine the best nop.
  105. */
  106. asm volatile (
  107. "jmp ftrace_test_jmp\n"
  108. /* This code needs to stay around */
  109. ".section .text, \"ax\"\n"
  110. "ftrace_test_jmp:"
  111. "jmp ftrace_test_p6nop\n"
  112. "nop\n"
  113. "nop\n"
  114. "nop\n" /* 2 byte jmp + 3 bytes */
  115. "ftrace_test_p6nop:"
  116. P6_NOP5
  117. "jmp 1f\n"
  118. "ftrace_test_nop5:"
  119. ".byte 0x66,0x66,0x66,0x66,0x90\n"
  120. "jmp 1f\n"
  121. ".previous\n"
  122. "1:"
  123. ".section .fixup, \"ax\"\n"
  124. "2: movl $1, %0\n"
  125. " jmp ftrace_test_nop5\n"
  126. "3: movl $2, %0\n"
  127. " jmp 1b\n"
  128. ".previous\n"
  129. _ASM_EXTABLE(ftrace_test_p6nop, 2b)
  130. _ASM_EXTABLE(ftrace_test_nop5, 3b)
  131. : "=r"(faulted) : "0" (faulted));
  132. switch (faulted) {
  133. case 0:
  134. pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
  135. ftrace_nop = (unsigned long *)ftrace_test_p6nop;
  136. break;
  137. case 1:
  138. pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
  139. ftrace_nop = (unsigned long *)ftrace_test_nop5;
  140. break;
  141. case 2:
  142. pr_info("ftrace: converting mcount calls to jmp . + 5\n");
  143. ftrace_nop = (unsigned long *)ftrace_test_jmp;
  144. break;
  145. }
  146. /* The return code is retured via data */
  147. *(unsigned long *)data = 0;
  148. return 0;
  149. }