ftrace.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Dynamic function tracer architecture backend.
  3. *
  4. * Copyright IBM Corp. 2009
  5. *
  6. * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,
  7. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  8. */
  9. #include <linux/hardirq.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/ftrace.h>
  12. #include <linux/kernel.h>
  13. #include <linux/types.h>
  14. #include <linux/kprobes.h>
  15. #include <trace/syscall.h>
  16. #include <asm/asm-offsets.h>
  17. #ifdef CONFIG_64BIT
  18. #define MCOUNT_OFFSET_RET 12
  19. #else
  20. #define MCOUNT_OFFSET_RET 22
  21. #endif
  22. #ifdef CONFIG_DYNAMIC_FTRACE
  23. void ftrace_disable_code(void);
  24. void ftrace_enable_insn(void);
  25. #ifdef CONFIG_64BIT
  26. /*
  27. * The 64-bit mcount code looks like this:
  28. * stg %r14,8(%r15) # offset 0
  29. * > larl %r1,<&counter> # offset 6
  30. * > brasl %r14,_mcount # offset 12
  31. * lg %r14,8(%r15) # offset 18
  32. * Total length is 24 bytes. The middle two instructions of the mcount
  33. * block get overwritten by ftrace_make_nop / ftrace_make_call.
  34. * The 64-bit enabled ftrace code block looks like this:
  35. * stg %r14,8(%r15) # offset 0
  36. * > lg %r1,__LC_FTRACE_FUNC # offset 6
  37. * > lgr %r0,%r0 # offset 12
  38. * > basr %r14,%r1 # offset 16
  39. * lg %r14,8(%15) # offset 18
  40. * The return points of the mcount/ftrace function have the same offset 18.
  41. * The 64-bit disable ftrace code block looks like this:
  42. * stg %r14,8(%r15) # offset 0
  43. * > jg .+18 # offset 6
  44. * > lgr %r0,%r0 # offset 12
  45. * > basr %r14,%r1 # offset 16
  46. * lg %r14,8(%15) # offset 18
  47. * The jg instruction branches to offset 24 to skip as many instructions
  48. * as possible.
  49. */
  50. asm(
  51. " .align 4\n"
  52. "ftrace_disable_code:\n"
  53. " jg 0f\n"
  54. " lgr %r0,%r0\n"
  55. " basr %r14,%r1\n"
  56. "0:\n"
  57. " .align 4\n"
  58. "ftrace_enable_insn:\n"
  59. " lg %r1,"__stringify(__LC_FTRACE_FUNC)"\n");
  60. #define FTRACE_INSN_SIZE 6
  61. #else /* CONFIG_64BIT */
  62. /*
  63. * The 31-bit mcount code looks like this:
  64. * st %r14,4(%r15) # offset 0
  65. * > bras %r1,0f # offset 4
  66. * > .long _mcount # offset 8
  67. * > .long <&counter> # offset 12
  68. * > 0: l %r14,0(%r1) # offset 16
  69. * > l %r1,4(%r1) # offset 20
  70. * basr %r14,%r14 # offset 24
  71. * l %r14,4(%r15) # offset 26
  72. * Total length is 30 bytes. The twenty bytes starting from offset 4
  73. * to offset 24 get overwritten by ftrace_make_nop / ftrace_make_call.
  74. * The 31-bit enabled ftrace code block looks like this:
  75. * st %r14,4(%r15) # offset 0
  76. * > l %r14,__LC_FTRACE_FUNC # offset 4
  77. * > j 0f # offset 8
  78. * > .fill 12,1,0x07 # offset 12
  79. * 0: basr %r14,%r14 # offset 24
  80. * l %r14,4(%r14) # offset 26
  81. * The return points of the mcount/ftrace function have the same offset 26.
  82. * The 31-bit disabled ftrace code block looks like this:
  83. * st %r14,4(%r15) # offset 0
  84. * > j .+26 # offset 4
  85. * > j 0f # offset 8
  86. * > .fill 12,1,0x07 # offset 12
  87. * 0: basr %r14,%r14 # offset 24
  88. * l %r14,4(%r14) # offset 26
  89. * The j instruction branches to offset 30 to skip as many instructions
  90. * as possible.
  91. */
  92. asm(
  93. " .align 4\n"
  94. "ftrace_disable_code:\n"
  95. " j 1f\n"
  96. " j 0f\n"
  97. " .fill 12,1,0x07\n"
  98. "0: basr %r14,%r14\n"
  99. "1:\n"
  100. " .align 4\n"
  101. "ftrace_enable_insn:\n"
  102. " l %r14,"__stringify(__LC_FTRACE_FUNC)"\n");
  103. #define FTRACE_INSN_SIZE 4
  104. #endif /* CONFIG_64BIT */
  105. int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
  106. unsigned long addr)
  107. {
  108. if (probe_kernel_write((void *) rec->ip, ftrace_disable_code,
  109. MCOUNT_INSN_SIZE))
  110. return -EPERM;
  111. return 0;
  112. }
  113. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  114. {
  115. if (probe_kernel_write((void *) rec->ip, ftrace_enable_insn,
  116. FTRACE_INSN_SIZE))
  117. return -EPERM;
  118. return 0;
  119. }
  120. int ftrace_update_ftrace_func(ftrace_func_t func)
  121. {
  122. return 0;
  123. }
  124. int __init ftrace_dyn_arch_init(void *data)
  125. {
  126. *(unsigned long *) data = 0;
  127. return 0;
  128. }
  129. #endif /* CONFIG_DYNAMIC_FTRACE */
  130. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  131. /*
  132. * Hook the return address and push it in the stack of return addresses
  133. * in current thread info.
  134. */
  135. unsigned long __kprobes prepare_ftrace_return(unsigned long parent,
  136. unsigned long ip)
  137. {
  138. struct ftrace_graph_ent trace;
  139. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  140. goto out;
  141. if (ftrace_push_return_trace(parent, ip, &trace.depth, 0) == -EBUSY)
  142. goto out;
  143. trace.func = (ip & PSW_ADDR_INSN) - MCOUNT_OFFSET_RET;
  144. /* Only trace if the calling function expects to. */
  145. if (!ftrace_graph_entry(&trace)) {
  146. current->curr_ret_stack--;
  147. goto out;
  148. }
  149. parent = (unsigned long) return_to_handler;
  150. out:
  151. return parent;
  152. }
  153. #ifdef CONFIG_DYNAMIC_FTRACE
  154. /*
  155. * Patch the kernel code at ftrace_graph_caller location. The instruction
  156. * there is branch relative and save to prepare_ftrace_return. To disable
  157. * the call to prepare_ftrace_return we patch the bras offset to point
  158. * directly after the instructions. To enable the call we calculate
  159. * the original offset to prepare_ftrace_return and put it back.
  160. */
  161. int ftrace_enable_ftrace_graph_caller(void)
  162. {
  163. unsigned short offset;
  164. offset = ((void *) prepare_ftrace_return -
  165. (void *) ftrace_graph_caller) / 2;
  166. return probe_kernel_write(ftrace_graph_caller + 2,
  167. &offset, sizeof(offset));
  168. }
  169. int ftrace_disable_ftrace_graph_caller(void)
  170. {
  171. static unsigned short offset = 0x0002;
  172. return probe_kernel_write(ftrace_graph_caller + 2,
  173. &offset, sizeof(offset));
  174. }
  175. #endif /* CONFIG_DYNAMIC_FTRACE */
  176. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */