ftrace.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/ftrace.h>
  14. #include <linux/percpu.h>
  15. #include <linux/init.h>
  16. #include <linux/list.h>
  17. #include <asm/alternative.h>
  18. #include <asm/ftrace.h>
  19. /* Long is fine, even if it is only 4 bytes ;-) */
  20. static long *ftrace_nop;
  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 notrace ftrace_calc_offset(long ip, long addr)
  29. {
  30. return (int)(addr - ip);
  31. }
  32. notrace unsigned char *ftrace_nop_replace(void)
  33. {
  34. return (char *)ftrace_nop;
  35. }
  36. notrace 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. notrace int
  48. ftrace_modify_code(unsigned long ip, unsigned char *old_code,
  49. unsigned char *new_code)
  50. {
  51. unsigned replaced;
  52. unsigned old = *(unsigned *)old_code; /* 4 bytes */
  53. unsigned new = *(unsigned *)new_code; /* 4 bytes */
  54. unsigned char newch = new_code[4];
  55. int faulted = 0;
  56. /*
  57. * Note: Due to modules and __init, code can
  58. * disappear and change, we need to protect against faulting
  59. * as well as code changing.
  60. *
  61. * No real locking needed, this code is run through
  62. * kstop_machine.
  63. */
  64. asm volatile (
  65. "1: lock\n"
  66. " cmpxchg %3, (%2)\n"
  67. " jnz 2f\n"
  68. " movb %b4, 4(%2)\n"
  69. "2:\n"
  70. ".section .fixup, \"ax\"\n"
  71. "3: movl $1, %0\n"
  72. " jmp 2b\n"
  73. ".previous\n"
  74. _ASM_EXTABLE(1b, 3b)
  75. : "=r"(faulted), "=a"(replaced)
  76. : "r"(ip), "r"(new), "c"(newch),
  77. "0"(faulted), "a"(old)
  78. : "memory");
  79. sync_core();
  80. if (replaced != old && replaced != new)
  81. faulted = 2;
  82. return faulted;
  83. }
  84. notrace int ftrace_update_ftrace_func(ftrace_func_t func)
  85. {
  86. unsigned long ip = (unsigned long)(&ftrace_call);
  87. unsigned char old[MCOUNT_INSN_SIZE], *new;
  88. int ret;
  89. memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
  90. new = ftrace_call_replace(ip, (unsigned long)func);
  91. ret = ftrace_modify_code(ip, old, new);
  92. return ret;
  93. }
  94. notrace int ftrace_mcount_set(unsigned long *data)
  95. {
  96. unsigned long ip = (long)(&mcount_call);
  97. unsigned long *addr = data;
  98. unsigned char old[MCOUNT_INSN_SIZE], *new;
  99. /*
  100. * Replace the mcount stub with a pointer to the
  101. * ip recorder function.
  102. */
  103. memcpy(old, &mcount_call, MCOUNT_INSN_SIZE);
  104. new = ftrace_call_replace(ip, *addr);
  105. *addr = ftrace_modify_code(ip, old, new);
  106. return 0;
  107. }
  108. int __init ftrace_dyn_arch_init(void *data)
  109. {
  110. const unsigned char *const *noptable = find_nop_table();
  111. /* This is running in kstop_machine */
  112. ftrace_mcount_set(data);
  113. ftrace_nop = (unsigned long *)noptable[MCOUNT_INSN_SIZE];
  114. return 0;
  115. }