ftrace.c 6.8 KB

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