ftrace.c 4.4 KB

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