ftrace.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Dynamic function tracer architecture backend.
  3. *
  4. * Copyright IBM Corp. 2009,2014
  5. *
  6. * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,
  7. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  8. */
  9. #include <linux/moduleloader.h>
  10. #include <linux/hardirq.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/ftrace.h>
  13. #include <linux/kernel.h>
  14. #include <linux/types.h>
  15. #include <linux/kprobes.h>
  16. #include <trace/syscall.h>
  17. #include <asm/asm-offsets.h>
  18. #include <asm/cacheflush.h>
  19. #include <asm/set_memory.h>
  20. #include "entry.h"
  21. /*
  22. * The mcount code looks like this:
  23. * stg %r14,8(%r15) # offset 0
  24. * larl %r1,<&counter> # offset 6
  25. * brasl %r14,_mcount # offset 12
  26. * lg %r14,8(%r15) # offset 18
  27. * Total length is 24 bytes. Only the first instruction will be patched
  28. * by ftrace_make_call / ftrace_make_nop.
  29. * The enabled ftrace code block looks like this:
  30. * > brasl %r0,ftrace_caller # offset 0
  31. * larl %r1,<&counter> # offset 6
  32. * brasl %r14,_mcount # offset 12
  33. * lg %r14,8(%r15) # offset 18
  34. * The ftrace function gets called with a non-standard C function call ABI
  35. * where r0 contains the return address. It is also expected that the called
  36. * function only clobbers r0 and r1, but restores r2-r15.
  37. * For module code we can't directly jump to ftrace caller, but need a
  38. * trampoline (ftrace_plt), which clobbers also r1.
  39. * The return point of the ftrace function has offset 24, so execution
  40. * continues behind the mcount block.
  41. * The disabled ftrace code block looks like this:
  42. * > jg .+24 # offset 0
  43. * larl %r1,<&counter> # offset 6
  44. * brasl %r14,_mcount # offset 12
  45. * lg %r14,8(%r15) # offset 18
  46. * The jg instruction branches to offset 24 to skip as many instructions
  47. * as possible.
  48. * In case we use gcc's hotpatch feature the original and also the disabled
  49. * function prologue contains only a single six byte instruction and looks
  50. * like this:
  51. * > brcl 0,0 # offset 0
  52. * To enable ftrace the code gets patched like above and afterwards looks
  53. * like this:
  54. * > brasl %r0,ftrace_caller # offset 0
  55. */
  56. unsigned long ftrace_plt;
  57. static inline void ftrace_generate_orig_insn(struct ftrace_insn *insn)
  58. {
  59. #ifdef CC_USING_HOTPATCH
  60. /* brcl 0,0 */
  61. insn->opc = 0xc004;
  62. insn->disp = 0;
  63. #else
  64. /* stg r14,8(r15) */
  65. insn->opc = 0xe3e0;
  66. insn->disp = 0xf0080024;
  67. #endif
  68. }
  69. static inline int is_kprobe_on_ftrace(struct ftrace_insn *insn)
  70. {
  71. #ifdef CONFIG_KPROBES
  72. if (insn->opc == BREAKPOINT_INSTRUCTION)
  73. return 1;
  74. #endif
  75. return 0;
  76. }
  77. static inline void ftrace_generate_kprobe_nop_insn(struct ftrace_insn *insn)
  78. {
  79. #ifdef CONFIG_KPROBES
  80. insn->opc = BREAKPOINT_INSTRUCTION;
  81. insn->disp = KPROBE_ON_FTRACE_NOP;
  82. #endif
  83. }
  84. static inline void ftrace_generate_kprobe_call_insn(struct ftrace_insn *insn)
  85. {
  86. #ifdef CONFIG_KPROBES
  87. insn->opc = BREAKPOINT_INSTRUCTION;
  88. insn->disp = KPROBE_ON_FTRACE_CALL;
  89. #endif
  90. }
  91. int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
  92. unsigned long addr)
  93. {
  94. return 0;
  95. }
  96. int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
  97. unsigned long addr)
  98. {
  99. struct ftrace_insn orig, new, old;
  100. if (probe_kernel_read(&old, (void *) rec->ip, sizeof(old)))
  101. return -EFAULT;
  102. if (addr == MCOUNT_ADDR) {
  103. /* Initial code replacement */
  104. ftrace_generate_orig_insn(&orig);
  105. ftrace_generate_nop_insn(&new);
  106. } else if (is_kprobe_on_ftrace(&old)) {
  107. /*
  108. * If we find a breakpoint instruction, a kprobe has been
  109. * placed at the beginning of the function. We write the
  110. * constant KPROBE_ON_FTRACE_NOP into the remaining four
  111. * bytes of the original instruction so that the kprobes
  112. * handler can execute a nop, if it reaches this breakpoint.
  113. */
  114. ftrace_generate_kprobe_call_insn(&orig);
  115. ftrace_generate_kprobe_nop_insn(&new);
  116. } else {
  117. /* Replace ftrace call with a nop. */
  118. ftrace_generate_call_insn(&orig, rec->ip);
  119. ftrace_generate_nop_insn(&new);
  120. }
  121. /* Verify that the to be replaced code matches what we expect. */
  122. if (memcmp(&orig, &old, sizeof(old)))
  123. return -EINVAL;
  124. s390_kernel_write((void *) rec->ip, &new, sizeof(new));
  125. return 0;
  126. }
  127. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  128. {
  129. struct ftrace_insn orig, new, old;
  130. if (probe_kernel_read(&old, (void *) rec->ip, sizeof(old)))
  131. return -EFAULT;
  132. if (is_kprobe_on_ftrace(&old)) {
  133. /*
  134. * If we find a breakpoint instruction, a kprobe has been
  135. * placed at the beginning of the function. We write the
  136. * constant KPROBE_ON_FTRACE_CALL into the remaining four
  137. * bytes of the original instruction so that the kprobes
  138. * handler can execute a brasl if it reaches this breakpoint.
  139. */
  140. ftrace_generate_kprobe_nop_insn(&orig);
  141. ftrace_generate_kprobe_call_insn(&new);
  142. } else {
  143. /* Replace nop with an ftrace call. */
  144. ftrace_generate_nop_insn(&orig);
  145. ftrace_generate_call_insn(&new, rec->ip);
  146. }
  147. /* Verify that the to be replaced code matches what we expect. */
  148. if (memcmp(&orig, &old, sizeof(old)))
  149. return -EINVAL;
  150. s390_kernel_write((void *) rec->ip, &new, sizeof(new));
  151. return 0;
  152. }
  153. int ftrace_update_ftrace_func(ftrace_func_t func)
  154. {
  155. return 0;
  156. }
  157. int __init ftrace_dyn_arch_init(void)
  158. {
  159. return 0;
  160. }
  161. #ifdef CONFIG_MODULES
  162. static int __init ftrace_plt_init(void)
  163. {
  164. unsigned int *ip;
  165. ftrace_plt = (unsigned long) module_alloc(PAGE_SIZE);
  166. if (!ftrace_plt)
  167. panic("cannot allocate ftrace plt\n");
  168. ip = (unsigned int *) ftrace_plt;
  169. ip[0] = 0x0d10e310; /* basr 1,0; lg 1,10(1); br 1 */
  170. ip[1] = 0x100a0004;
  171. ip[2] = 0x07f10000;
  172. ip[3] = FTRACE_ADDR >> 32;
  173. ip[4] = FTRACE_ADDR & 0xffffffff;
  174. set_memory_ro(ftrace_plt, 1);
  175. return 0;
  176. }
  177. device_initcall(ftrace_plt_init);
  178. #endif /* CONFIG_MODULES */
  179. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  180. /*
  181. * Hook the return address and push it in the stack of return addresses
  182. * in current thread info.
  183. */
  184. unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip)
  185. {
  186. struct ftrace_graph_ent trace;
  187. if (unlikely(ftrace_graph_is_dead()))
  188. goto out;
  189. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  190. goto out;
  191. ip -= MCOUNT_INSN_SIZE;
  192. trace.func = ip;
  193. trace.depth = current->curr_ret_stack + 1;
  194. /* Only trace if the calling function expects to. */
  195. if (!ftrace_graph_entry(&trace))
  196. goto out;
  197. if (ftrace_push_return_trace(parent, ip, &trace.depth, 0,
  198. NULL) == -EBUSY)
  199. goto out;
  200. parent = (unsigned long) return_to_handler;
  201. out:
  202. return parent;
  203. }
  204. NOKPROBE_SYMBOL(prepare_ftrace_return);
  205. /*
  206. * Patch the kernel code at ftrace_graph_caller location. The instruction
  207. * there is branch relative on condition. To enable the ftrace graph code
  208. * block, we simply patch the mask field of the instruction to zero and
  209. * turn the instruction into a nop.
  210. * To disable the ftrace graph code the mask field will be patched to
  211. * all ones, which turns the instruction into an unconditional branch.
  212. */
  213. int ftrace_enable_ftrace_graph_caller(void)
  214. {
  215. u8 op = 0x04; /* set mask field to zero */
  216. s390_kernel_write(__va(ftrace_graph_caller)+1, &op, sizeof(op));
  217. return 0;
  218. }
  219. int ftrace_disable_ftrace_graph_caller(void)
  220. {
  221. u8 op = 0xf4; /* set mask field to all ones */
  222. s390_kernel_write(__va(ftrace_graph_caller)+1, &op, sizeof(op));
  223. return 0;
  224. }
  225. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */