ftrace.c 5.1 KB

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