ftrace.c 6.9 KB

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