ftrace.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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, true);
  54. return ftrace_modify_code(pc, 0, new, false);
  55. }
  56. /*
  57. * Turn on the call to ftrace_caller() in instrumented function
  58. */
  59. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  60. {
  61. unsigned long pc = rec->ip;
  62. u32 old, new;
  63. old = aarch64_insn_gen_nop();
  64. new = aarch64_insn_gen_branch_imm(pc, addr, true);
  65. return ftrace_modify_code(pc, old, new, true);
  66. }
  67. /*
  68. * Turn off the call to ftrace_caller() in instrumented function
  69. */
  70. int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
  71. unsigned long addr)
  72. {
  73. unsigned long pc = rec->ip;
  74. u32 old, new;
  75. old = aarch64_insn_gen_branch_imm(pc, addr, true);
  76. new = aarch64_insn_gen_nop();
  77. return ftrace_modify_code(pc, old, new, true);
  78. }
  79. int __init ftrace_dyn_arch_init(void)
  80. {
  81. return 0;
  82. }
  83. #endif /* CONFIG_DYNAMIC_FTRACE */
  84. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  85. /*
  86. * function_graph tracer expects ftrace_return_to_handler() to be called
  87. * on the way back to parent. For this purpose, this function is called
  88. * in _mcount() or ftrace_caller() to replace return address (*parent) on
  89. * the call stack to return_to_handler.
  90. *
  91. * Note that @frame_pointer is used only for sanity check later.
  92. */
  93. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
  94. unsigned long frame_pointer)
  95. {
  96. unsigned long return_hooker = (unsigned long)&return_to_handler;
  97. unsigned long old;
  98. struct ftrace_graph_ent trace;
  99. int err;
  100. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  101. return;
  102. /*
  103. * Note:
  104. * No protection against faulting at *parent, which may be seen
  105. * on other archs. It's unlikely on AArch64.
  106. */
  107. old = *parent;
  108. *parent = return_hooker;
  109. trace.func = self_addr;
  110. trace.depth = current->curr_ret_stack + 1;
  111. /* Only trace if the calling function expects to */
  112. if (!ftrace_graph_entry(&trace)) {
  113. *parent = old;
  114. return;
  115. }
  116. err = ftrace_push_return_trace(old, self_addr, &trace.depth,
  117. frame_pointer);
  118. if (err == -EBUSY) {
  119. *parent = old;
  120. return;
  121. }
  122. }
  123. #ifdef CONFIG_DYNAMIC_FTRACE
  124. /*
  125. * Turn on/off the call to ftrace_graph_caller() in ftrace_caller()
  126. * depending on @enable.
  127. */
  128. static int ftrace_modify_graph_caller(bool enable)
  129. {
  130. unsigned long pc = (unsigned long)&ftrace_graph_call;
  131. u32 branch, nop;
  132. branch = aarch64_insn_gen_branch_imm(pc,
  133. (unsigned long)ftrace_graph_caller, false);
  134. nop = aarch64_insn_gen_nop();
  135. if (enable)
  136. return ftrace_modify_code(pc, nop, branch, true);
  137. else
  138. return ftrace_modify_code(pc, branch, nop, true);
  139. }
  140. int ftrace_enable_ftrace_graph_caller(void)
  141. {
  142. return ftrace_modify_graph_caller(true);
  143. }
  144. int ftrace_disable_ftrace_graph_caller(void)
  145. {
  146. return ftrace_modify_graph_caller(false);
  147. }
  148. #endif /* CONFIG_DYNAMIC_FTRACE */
  149. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */