ftrace.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. We do this by using the
  57. * __copy_*_user functions.
  58. *
  59. * No real locking needed, this code is run through
  60. * kstop_machine, or before SMP starts.
  61. */
  62. /* read the text we want to modify */
  63. if (__copy_from_user_inatomic(replaced, (char __user *)ip,
  64. MCOUNT_INSN_SIZE))
  65. return -EFAULT;
  66. /* Make sure it is what we expect it to be */
  67. if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
  68. return -EINVAL;
  69. /* replace the text with the new text */
  70. if (__copy_to_user_inatomic((char __user *)ip, new_code,
  71. MCOUNT_INSN_SIZE))
  72. return -EPERM;
  73. sync_core();
  74. return 0;
  75. }
  76. notrace int ftrace_update_ftrace_func(ftrace_func_t func)
  77. {
  78. unsigned long ip = (unsigned long)(&ftrace_call);
  79. unsigned char old[MCOUNT_INSN_SIZE], *new;
  80. int ret;
  81. memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
  82. new = ftrace_call_replace(ip, (unsigned long)func);
  83. ret = ftrace_modify_code(ip, old, new);
  84. return ret;
  85. }
  86. notrace int ftrace_mcount_set(unsigned long *data)
  87. {
  88. /* mcount is initialized as a nop */
  89. *data = 0;
  90. return 0;
  91. }
  92. int __init ftrace_dyn_arch_init(void *data)
  93. {
  94. extern const unsigned char ftrace_test_p6nop[];
  95. extern const unsigned char ftrace_test_nop5[];
  96. extern const unsigned char ftrace_test_jmp[];
  97. int faulted = 0;
  98. /*
  99. * There is no good nop for all x86 archs.
  100. * We will default to using the P6_NOP5, but first we
  101. * will test to make sure that the nop will actually
  102. * work on this CPU. If it faults, we will then
  103. * go to a lesser efficient 5 byte nop. If that fails
  104. * we then just use a jmp as our nop. This isn't the most
  105. * efficient nop, but we can not use a multi part nop
  106. * since we would then risk being preempted in the middle
  107. * of that nop, and if we enabled tracing then, it might
  108. * cause a system crash.
  109. *
  110. * TODO: check the cpuid to determine the best nop.
  111. */
  112. asm volatile (
  113. "jmp ftrace_test_jmp\n"
  114. /* This code needs to stay around */
  115. ".section .text, \"ax\"\n"
  116. "ftrace_test_jmp:"
  117. "jmp ftrace_test_p6nop\n"
  118. "nop\n"
  119. "nop\n"
  120. "nop\n" /* 2 byte jmp + 3 bytes */
  121. "ftrace_test_p6nop:"
  122. P6_NOP5
  123. "jmp 1f\n"
  124. "ftrace_test_nop5:"
  125. ".byte 0x66,0x66,0x66,0x66,0x90\n"
  126. "jmp 1f\n"
  127. ".previous\n"
  128. "1:"
  129. ".section .fixup, \"ax\"\n"
  130. "2: movl $1, %0\n"
  131. " jmp ftrace_test_nop5\n"
  132. "3: movl $2, %0\n"
  133. " jmp 1b\n"
  134. ".previous\n"
  135. _ASM_EXTABLE(ftrace_test_p6nop, 2b)
  136. _ASM_EXTABLE(ftrace_test_nop5, 3b)
  137. : "=r"(faulted) : "0" (faulted));
  138. switch (faulted) {
  139. case 0:
  140. pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
  141. ftrace_nop = (unsigned long *)ftrace_test_p6nop;
  142. break;
  143. case 1:
  144. pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
  145. ftrace_nop = (unsigned long *)ftrace_test_nop5;
  146. break;
  147. case 2:
  148. pr_info("ftrace: converting mcount calls to jmp . + 5\n");
  149. ftrace_nop = (unsigned long *)ftrace_test_jmp;
  150. break;
  151. }
  152. /* The return code is retured via data */
  153. *(unsigned long *)data = 0;
  154. return 0;
  155. }