ftrace.c 5.5 KB

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