ftrace.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * arch/arm64/kernel/ftrace.c
  3. *
  4. * Copyright (C) 2013 Linaro Limited
  5. * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/ftrace.h>
  12. #include <linux/swab.h>
  13. #include <linux/uaccess.h>
  14. #include <asm/cacheflush.h>
  15. #include <asm/ftrace.h>
  16. #include <asm/insn.h>
  17. #ifdef CONFIG_DYNAMIC_FTRACE
  18. /*
  19. * Replace a single instruction, which may be a branch or NOP.
  20. * If @validate == true, a replaced instruction is checked against 'old'.
  21. */
  22. static int ftrace_modify_code(unsigned long pc, u32 old, u32 new,
  23. bool validate)
  24. {
  25. u32 replaced;
  26. /*
  27. * Note:
  28. * Due to modules and __init, code can disappear and change,
  29. * we need to protect against faulting as well as code changing.
  30. * We do this by aarch64_insn_*() which use the probe_kernel_*().
  31. *
  32. * No lock is held here because all the modifications are run
  33. * through stop_machine().
  34. */
  35. if (validate) {
  36. if (aarch64_insn_read((void *)pc, &replaced))
  37. return -EFAULT;
  38. if (replaced != old)
  39. return -EINVAL;
  40. }
  41. if (aarch64_insn_patch_text_nosync((void *)pc, new))
  42. return -EPERM;
  43. return 0;
  44. }
  45. /*
  46. * Replace tracer function in ftrace_caller()
  47. */
  48. int ftrace_update_ftrace_func(ftrace_func_t func)
  49. {
  50. unsigned long pc;
  51. u32 new;
  52. pc = (unsigned long)&ftrace_call;
  53. new = aarch64_insn_gen_branch_imm(pc, (unsigned long)func,
  54. AARCH64_INSN_BRANCH_LINK);
  55. return ftrace_modify_code(pc, 0, new, false);
  56. }
  57. /*
  58. * Turn on the call to ftrace_caller() in instrumented function
  59. */
  60. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  61. {
  62. unsigned long pc = rec->ip;
  63. u32 old, new;
  64. old = aarch64_insn_gen_nop();
  65. new = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
  66. return ftrace_modify_code(pc, old, new, true);
  67. }
  68. /*
  69. * Turn off the call to ftrace_caller() in instrumented function
  70. */
  71. int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
  72. unsigned long addr)
  73. {
  74. unsigned long pc = rec->ip;
  75. u32 old, new;
  76. old = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
  77. new = aarch64_insn_gen_nop();
  78. return ftrace_modify_code(pc, old, new, true);
  79. }
  80. int __init ftrace_dyn_arch_init(void)
  81. {
  82. return 0;
  83. }
  84. #endif /* CONFIG_DYNAMIC_FTRACE */
  85. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  86. /*
  87. * function_graph tracer expects ftrace_return_to_handler() to be called
  88. * on the way back to parent. For this purpose, this function is called
  89. * in _mcount() or ftrace_caller() to replace return address (*parent) on
  90. * the call stack to return_to_handler.
  91. *
  92. * Note that @frame_pointer is used only for sanity check later.
  93. */
  94. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
  95. unsigned long frame_pointer)
  96. {
  97. unsigned long return_hooker = (unsigned long)&return_to_handler;
  98. unsigned long old;
  99. struct ftrace_graph_ent trace;
  100. int err;
  101. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  102. return;
  103. /*
  104. * Note:
  105. * No protection against faulting at *parent, which may be seen
  106. * on other archs. It's unlikely on AArch64.
  107. */
  108. old = *parent;
  109. *parent = return_hooker;
  110. trace.func = self_addr;
  111. trace.depth = current->curr_ret_stack + 1;
  112. /* Only trace if the calling function expects to */
  113. if (!ftrace_graph_entry(&trace)) {
  114. *parent = old;
  115. return;
  116. }
  117. err = ftrace_push_return_trace(old, self_addr, &trace.depth,
  118. frame_pointer);
  119. if (err == -EBUSY) {
  120. *parent = old;
  121. return;
  122. }
  123. }
  124. #ifdef CONFIG_DYNAMIC_FTRACE
  125. /*
  126. * Turn on/off the call to ftrace_graph_caller() in ftrace_caller()
  127. * depending on @enable.
  128. */
  129. static int ftrace_modify_graph_caller(bool enable)
  130. {
  131. unsigned long pc = (unsigned long)&ftrace_graph_call;
  132. u32 branch, nop;
  133. branch = aarch64_insn_gen_branch_imm(pc,
  134. (unsigned long)ftrace_graph_caller,
  135. AARCH64_INSN_BRANCH_NOLINK);
  136. nop = aarch64_insn_gen_nop();
  137. if (enable)
  138. return ftrace_modify_code(pc, nop, branch, true);
  139. else
  140. return ftrace_modify_code(pc, branch, nop, true);
  141. }
  142. int ftrace_enable_ftrace_graph_caller(void)
  143. {
  144. return ftrace_modify_graph_caller(true);
  145. }
  146. int ftrace_disable_ftrace_graph_caller(void)
  147. {
  148. return ftrace_modify_graph_caller(false);
  149. }
  150. #endif /* CONFIG_DYNAMIC_FTRACE */
  151. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */