ftrace.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
  57. unsigned long addr)
  58. {
  59. return 0;
  60. }
  61. int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
  62. unsigned long addr)
  63. {
  64. struct ftrace_insn orig, new, old;
  65. if (probe_kernel_read(&old, (void *) rec->ip, sizeof(old)))
  66. return -EFAULT;
  67. if (addr == MCOUNT_ADDR) {
  68. /* Initial code replacement */
  69. #ifdef CC_USING_HOTPATCH
  70. /* We expect to see brcl 0,0 */
  71. ftrace_generate_nop_insn(&orig);
  72. #else
  73. /* We expect to see stg r14,8(r15) */
  74. orig.opc = 0xe3e0;
  75. orig.disp = 0xf0080024;
  76. #endif
  77. ftrace_generate_nop_insn(&new);
  78. } else if (old.opc == BREAKPOINT_INSTRUCTION) {
  79. /*
  80. * If we find a breakpoint instruction, a kprobe has been
  81. * placed at the beginning of the function. We write the
  82. * constant KPROBE_ON_FTRACE_NOP into the remaining four
  83. * bytes of the original instruction so that the kprobes
  84. * handler can execute a nop, if it reaches this breakpoint.
  85. */
  86. new.opc = orig.opc = BREAKPOINT_INSTRUCTION;
  87. orig.disp = KPROBE_ON_FTRACE_CALL;
  88. new.disp = KPROBE_ON_FTRACE_NOP;
  89. } else {
  90. /* Replace ftrace call with a nop. */
  91. ftrace_generate_call_insn(&orig, rec->ip);
  92. ftrace_generate_nop_insn(&new);
  93. }
  94. /* Verify that the to be replaced code matches what we expect. */
  95. if (memcmp(&orig, &old, sizeof(old)))
  96. return -EINVAL;
  97. if (probe_kernel_write((void *) rec->ip, &new, sizeof(new)))
  98. return -EPERM;
  99. return 0;
  100. }
  101. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  102. {
  103. struct ftrace_insn orig, new, old;
  104. if (probe_kernel_read(&old, (void *) rec->ip, sizeof(old)))
  105. return -EFAULT;
  106. if (old.opc == BREAKPOINT_INSTRUCTION) {
  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_CALL into the remaining four
  111. * bytes of the original instruction so that the kprobes
  112. * handler can execute a brasl if it reaches this breakpoint.
  113. */
  114. new.opc = orig.opc = BREAKPOINT_INSTRUCTION;
  115. orig.disp = KPROBE_ON_FTRACE_NOP;
  116. new.disp = KPROBE_ON_FTRACE_CALL;
  117. } else {
  118. /* Replace nop with an ftrace call. */
  119. ftrace_generate_nop_insn(&orig);
  120. ftrace_generate_call_insn(&new, rec->ip);
  121. }
  122. /* Verify that the to be replaced code matches what we expect. */
  123. if (memcmp(&orig, &old, sizeof(old)))
  124. return -EINVAL;
  125. if (probe_kernel_write((void *) rec->ip, &new, sizeof(new)))
  126. return -EPERM;
  127. return 0;
  128. }
  129. int ftrace_update_ftrace_func(ftrace_func_t func)
  130. {
  131. return 0;
  132. }
  133. int __init ftrace_dyn_arch_init(void)
  134. {
  135. return 0;
  136. }
  137. static int __init ftrace_plt_init(void)
  138. {
  139. unsigned int *ip;
  140. ftrace_plt = (unsigned long) module_alloc(PAGE_SIZE);
  141. if (!ftrace_plt)
  142. panic("cannot allocate ftrace plt\n");
  143. ip = (unsigned int *) ftrace_plt;
  144. ip[0] = 0x0d10e310; /* basr 1,0; lg 1,10(1); br 1 */
  145. ip[1] = 0x100a0004;
  146. ip[2] = 0x07f10000;
  147. ip[3] = FTRACE_ADDR >> 32;
  148. ip[4] = FTRACE_ADDR & 0xffffffff;
  149. set_memory_ro(ftrace_plt, 1);
  150. return 0;
  151. }
  152. device_initcall(ftrace_plt_init);
  153. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  154. /*
  155. * Hook the return address and push it in the stack of return addresses
  156. * in current thread info.
  157. */
  158. unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip)
  159. {
  160. struct ftrace_graph_ent trace;
  161. if (unlikely(ftrace_graph_is_dead()))
  162. goto out;
  163. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  164. goto out;
  165. ip = (ip & PSW_ADDR_INSN) - MCOUNT_INSN_SIZE;
  166. trace.func = ip;
  167. trace.depth = current->curr_ret_stack + 1;
  168. /* Only trace if the calling function expects to. */
  169. if (!ftrace_graph_entry(&trace))
  170. goto out;
  171. if (ftrace_push_return_trace(parent, ip, &trace.depth, 0) == -EBUSY)
  172. goto out;
  173. parent = (unsigned long) return_to_handler;
  174. out:
  175. return parent;
  176. }
  177. NOKPROBE_SYMBOL(prepare_ftrace_return);
  178. /*
  179. * Patch the kernel code at ftrace_graph_caller location. The instruction
  180. * there is branch relative on condition. To enable the ftrace graph code
  181. * block, we simply patch the mask field of the instruction to zero and
  182. * turn the instruction into a nop.
  183. * To disable the ftrace graph code the mask field will be patched to
  184. * all ones, which turns the instruction into an unconditional branch.
  185. */
  186. int ftrace_enable_ftrace_graph_caller(void)
  187. {
  188. u8 op = 0x04; /* set mask field to zero */
  189. return probe_kernel_write(__va(ftrace_graph_caller)+1, &op, sizeof(op));
  190. }
  191. int ftrace_disable_ftrace_graph_caller(void)
  192. {
  193. u8 op = 0xf4; /* set mask field to all ones */
  194. return probe_kernel_write(__va(ftrace_graph_caller)+1, &op, sizeof(op));
  195. }
  196. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */