ftrace.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. * We are paranoid about modifying text, as if a bug were to happen, it
  29. * could cause us to read or write to someplace that could cause harm.
  30. * Carefully read and modify the code with aarch64_insn_*() which uses
  31. * probe_kernel_*(), and make sure what we read is what we expected it
  32. * to be before modifying it.
  33. */
  34. if (validate) {
  35. if (aarch64_insn_read((void *)pc, &replaced))
  36. return -EFAULT;
  37. if (replaced != old)
  38. return -EINVAL;
  39. }
  40. if (aarch64_insn_patch_text_nosync((void *)pc, new))
  41. return -EPERM;
  42. return 0;
  43. }
  44. /*
  45. * Replace tracer function in ftrace_caller()
  46. */
  47. int ftrace_update_ftrace_func(ftrace_func_t func)
  48. {
  49. unsigned long pc;
  50. u32 new;
  51. pc = (unsigned long)&ftrace_call;
  52. new = aarch64_insn_gen_branch_imm(pc, (unsigned long)func,
  53. AARCH64_INSN_BRANCH_LINK);
  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, AARCH64_INSN_BRANCH_LINK);
  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, AARCH64_INSN_BRANCH_LINK);
  76. new = aarch64_insn_gen_nop();
  77. return ftrace_modify_code(pc, old, new, true);
  78. }
  79. void arch_ftrace_update_code(int command)
  80. {
  81. ftrace_modify_all_code(command);
  82. }
  83. int __init ftrace_dyn_arch_init(void)
  84. {
  85. return 0;
  86. }
  87. #endif /* CONFIG_DYNAMIC_FTRACE */
  88. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  89. /*
  90. * function_graph tracer expects ftrace_return_to_handler() to be called
  91. * on the way back to parent. For this purpose, this function is called
  92. * in _mcount() or ftrace_caller() to replace return address (*parent) on
  93. * the call stack to return_to_handler.
  94. *
  95. * Note that @frame_pointer is used only for sanity check later.
  96. */
  97. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
  98. unsigned long frame_pointer)
  99. {
  100. unsigned long return_hooker = (unsigned long)&return_to_handler;
  101. unsigned long old;
  102. struct ftrace_graph_ent trace;
  103. int err;
  104. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  105. return;
  106. /*
  107. * Note:
  108. * No protection against faulting at *parent, which may be seen
  109. * on other archs. It's unlikely on AArch64.
  110. */
  111. old = *parent;
  112. trace.func = self_addr;
  113. trace.depth = current->curr_ret_stack + 1;
  114. /* Only trace if the calling function expects to */
  115. if (!ftrace_graph_entry(&trace))
  116. return;
  117. err = ftrace_push_return_trace(old, self_addr, &trace.depth,
  118. frame_pointer, NULL);
  119. if (err == -EBUSY)
  120. return;
  121. else
  122. *parent = return_hooker;
  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 */