ftrace.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Dynamic function tracing support.
  3. *
  4. * Copyright (C) 2008 Abhishek Sagar <sagar.abhishek@gmail.com>
  5. * Copyright (C) 2010 Rabin Vincent <rabin@rab.in>
  6. *
  7. * For licencing details, see COPYING.
  8. *
  9. * Defines low-level handling of mcount calls when the kernel
  10. * is compiled with the -pg flag. When using dynamic ftrace, the
  11. * mcount call-sites get patched with NOP till they are enabled.
  12. * All code mutation routines here are called under stop_machine().
  13. */
  14. #include <linux/ftrace.h>
  15. #include <linux/uaccess.h>
  16. #include <asm/cacheflush.h>
  17. #include <asm/ftrace.h>
  18. #ifdef CONFIG_THUMB2_KERNEL
  19. #define NOP 0xeb04f85d /* pop.w {lr} */
  20. #else
  21. #define NOP 0xe8bd4000 /* pop {lr} */
  22. #endif
  23. #ifdef CONFIG_OLD_MCOUNT
  24. #define OLD_MCOUNT_ADDR ((unsigned long) mcount)
  25. #define OLD_FTRACE_ADDR ((unsigned long) ftrace_caller_old)
  26. #define OLD_NOP 0xe1a00000 /* mov r0, r0 */
  27. static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
  28. {
  29. return rec->arch.old_mcount ? OLD_NOP : NOP;
  30. }
  31. static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr)
  32. {
  33. if (!rec->arch.old_mcount)
  34. return addr;
  35. if (addr == MCOUNT_ADDR)
  36. addr = OLD_MCOUNT_ADDR;
  37. else if (addr == FTRACE_ADDR)
  38. addr = OLD_FTRACE_ADDR;
  39. return addr;
  40. }
  41. #else
  42. static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
  43. {
  44. return NOP;
  45. }
  46. static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr)
  47. {
  48. return addr;
  49. }
  50. #endif
  51. /* construct a branch (BL) instruction to addr */
  52. #ifdef CONFIG_THUMB2_KERNEL
  53. static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr)
  54. {
  55. unsigned long s, j1, j2, i1, i2, imm10, imm11;
  56. unsigned long first, second;
  57. long offset;
  58. offset = (long)addr - (long)(pc + 4);
  59. if (offset < -16777216 || offset > 16777214) {
  60. WARN_ON_ONCE(1);
  61. return 0;
  62. }
  63. s = (offset >> 24) & 0x1;
  64. i1 = (offset >> 23) & 0x1;
  65. i2 = (offset >> 22) & 0x1;
  66. imm10 = (offset >> 12) & 0x3ff;
  67. imm11 = (offset >> 1) & 0x7ff;
  68. j1 = (!i1) ^ s;
  69. j2 = (!i2) ^ s;
  70. first = 0xf000 | (s << 10) | imm10;
  71. second = 0xd000 | (j1 << 13) | (j2 << 11) | imm11;
  72. return (second << 16) | first;
  73. }
  74. #else
  75. static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr)
  76. {
  77. long offset;
  78. offset = (long)addr - (long)(pc + 8);
  79. if (unlikely(offset < -33554432 || offset > 33554428)) {
  80. /* Can't generate branches that far (from ARM ARM). Ftrace
  81. * doesn't generate branches outside of kernel text.
  82. */
  83. WARN_ON_ONCE(1);
  84. return 0;
  85. }
  86. offset = (offset >> 2) & 0x00ffffff;
  87. return 0xeb000000 | offset;
  88. }
  89. #endif
  90. static int ftrace_modify_code(unsigned long pc, unsigned long old,
  91. unsigned long new)
  92. {
  93. unsigned long replaced;
  94. if (probe_kernel_read(&replaced, (void *)pc, MCOUNT_INSN_SIZE))
  95. return -EFAULT;
  96. if (replaced != old)
  97. return -EINVAL;
  98. if (probe_kernel_write((void *)pc, &new, MCOUNT_INSN_SIZE))
  99. return -EPERM;
  100. flush_icache_range(pc, pc + MCOUNT_INSN_SIZE);
  101. return 0;
  102. }
  103. int ftrace_update_ftrace_func(ftrace_func_t func)
  104. {
  105. unsigned long pc, old;
  106. unsigned long new;
  107. int ret;
  108. pc = (unsigned long)&ftrace_call;
  109. memcpy(&old, &ftrace_call, MCOUNT_INSN_SIZE);
  110. new = ftrace_call_replace(pc, (unsigned long)func);
  111. ret = ftrace_modify_code(pc, old, new);
  112. #ifdef CONFIG_OLD_MCOUNT
  113. if (!ret) {
  114. pc = (unsigned long)&ftrace_call_old;
  115. memcpy(&old, &ftrace_call_old, MCOUNT_INSN_SIZE);
  116. new = ftrace_call_replace(pc, (unsigned long)func);
  117. ret = ftrace_modify_code(pc, old, new);
  118. }
  119. #endif
  120. return ret;
  121. }
  122. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  123. {
  124. unsigned long new, old;
  125. unsigned long ip = rec->ip;
  126. old = ftrace_nop_replace(rec);
  127. new = ftrace_call_replace(ip, adjust_address(rec, addr));
  128. return ftrace_modify_code(rec->ip, old, new);
  129. }
  130. int ftrace_make_nop(struct module *mod,
  131. struct dyn_ftrace *rec, unsigned long addr)
  132. {
  133. unsigned long ip = rec->ip;
  134. unsigned long old;
  135. unsigned long new;
  136. int ret;
  137. old = ftrace_call_replace(ip, adjust_address(rec, addr));
  138. new = ftrace_nop_replace(rec);
  139. ret = ftrace_modify_code(ip, old, new);
  140. #ifdef CONFIG_OLD_MCOUNT
  141. if (ret == -EINVAL && addr == MCOUNT_ADDR) {
  142. rec->arch.old_mcount = true;
  143. old = ftrace_call_replace(ip, adjust_address(rec, addr));
  144. new = ftrace_nop_replace(rec);
  145. ret = ftrace_modify_code(ip, old, new);
  146. }
  147. #endif
  148. return ret;
  149. }
  150. int __init ftrace_dyn_arch_init(void *data)
  151. {
  152. *(unsigned long *)data = 0;
  153. return 0;
  154. }