ftrace.c 4.1 KB

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