mcount_64.S 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * linux/arch/x86_64/mcount_64.S
  3. *
  4. * Copyright (C) 2014 Steven Rostedt, Red Hat Inc
  5. */
  6. #include <linux/linkage.h>
  7. #include <asm/ptrace.h>
  8. #include <asm/ftrace.h>
  9. .code64
  10. .section .entry.text, "ax"
  11. #ifdef CONFIG_FUNCTION_TRACER
  12. #ifdef CC_USING_FENTRY
  13. # define function_hook __fentry__
  14. #else
  15. # define function_hook mcount
  16. #endif
  17. #ifdef CONFIG_DYNAMIC_FTRACE
  18. ENTRY(function_hook)
  19. retq
  20. END(function_hook)
  21. /* skip is set if stack has been adjusted */
  22. .macro ftrace_caller_setup skip=0
  23. MCOUNT_SAVE_FRAME \skip
  24. /* Load the ftrace_ops into the 3rd parameter */
  25. movq function_trace_op(%rip), %rdx
  26. /* Load ip into the first parameter */
  27. movq RIP(%rsp), %rdi
  28. subq $MCOUNT_INSN_SIZE, %rdi
  29. /* Load the parent_ip into the second parameter */
  30. #ifdef CC_USING_FENTRY
  31. movq SS+16(%rsp), %rsi
  32. #else
  33. movq 8(%rbp), %rsi
  34. #endif
  35. .endm
  36. ENTRY(ftrace_caller)
  37. ftrace_caller_setup
  38. /* regs go into 4th parameter (but make it NULL) */
  39. movq $0, %rcx
  40. GLOBAL(ftrace_call)
  41. call ftrace_stub
  42. MCOUNT_RESTORE_FRAME
  43. ftrace_return:
  44. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  45. GLOBAL(ftrace_graph_call)
  46. jmp ftrace_stub
  47. #endif
  48. GLOBAL(ftrace_stub)
  49. retq
  50. END(ftrace_caller)
  51. ENTRY(ftrace_regs_caller)
  52. /* Save the current flags before compare (in SS location)*/
  53. pushfq
  54. /* skip=8 to skip flags saved in SS */
  55. ftrace_caller_setup 8
  56. /* Save the rest of pt_regs */
  57. movq %r15, R15(%rsp)
  58. movq %r14, R14(%rsp)
  59. movq %r13, R13(%rsp)
  60. movq %r12, R12(%rsp)
  61. movq %r11, R11(%rsp)
  62. movq %r10, R10(%rsp)
  63. movq %rbp, RBP(%rsp)
  64. movq %rbx, RBX(%rsp)
  65. /* Copy saved flags */
  66. movq SS(%rsp), %rcx
  67. movq %rcx, EFLAGS(%rsp)
  68. /* Kernel segments */
  69. movq $__KERNEL_DS, %rcx
  70. movq %rcx, SS(%rsp)
  71. movq $__KERNEL_CS, %rcx
  72. movq %rcx, CS(%rsp)
  73. /* Stack - skipping return address */
  74. leaq SS+16(%rsp), %rcx
  75. movq %rcx, RSP(%rsp)
  76. /* regs go into 4th parameter */
  77. leaq (%rsp), %rcx
  78. GLOBAL(ftrace_regs_call)
  79. call ftrace_stub
  80. /* Copy flags back to SS, to restore them */
  81. movq EFLAGS(%rsp), %rax
  82. movq %rax, SS(%rsp)
  83. /* Handlers can change the RIP */
  84. movq RIP(%rsp), %rax
  85. movq %rax, SS+8(%rsp)
  86. /* restore the rest of pt_regs */
  87. movq R15(%rsp), %r15
  88. movq R14(%rsp), %r14
  89. movq R13(%rsp), %r13
  90. movq R12(%rsp), %r12
  91. movq R10(%rsp), %r10
  92. movq RBP(%rsp), %rbp
  93. movq RBX(%rsp), %rbx
  94. /* skip=8 to skip flags saved in SS */
  95. MCOUNT_RESTORE_FRAME 8
  96. /* Restore flags */
  97. popfq
  98. jmp ftrace_return
  99. popfq
  100. jmp ftrace_stub
  101. END(ftrace_regs_caller)
  102. #else /* ! CONFIG_DYNAMIC_FTRACE */
  103. ENTRY(function_hook)
  104. cmpq $ftrace_stub, ftrace_trace_function
  105. jnz trace
  106. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  107. cmpq $ftrace_stub, ftrace_graph_return
  108. jnz ftrace_graph_caller
  109. cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
  110. jnz ftrace_graph_caller
  111. #endif
  112. GLOBAL(ftrace_stub)
  113. retq
  114. trace:
  115. MCOUNT_SAVE_FRAME
  116. movq RIP(%rsp), %rdi
  117. #ifdef CC_USING_FENTRY
  118. movq SS+16(%rsp), %rsi
  119. #else
  120. movq 8(%rbp), %rsi
  121. #endif
  122. subq $MCOUNT_INSN_SIZE, %rdi
  123. call *ftrace_trace_function
  124. MCOUNT_RESTORE_FRAME
  125. jmp ftrace_stub
  126. END(function_hook)
  127. #endif /* CONFIG_DYNAMIC_FTRACE */
  128. #endif /* CONFIG_FUNCTION_TRACER */
  129. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  130. ENTRY(ftrace_graph_caller)
  131. MCOUNT_SAVE_FRAME
  132. #ifdef CC_USING_FENTRY
  133. leaq SS+16(%rsp), %rdi
  134. movq $0, %rdx /* No framepointers needed */
  135. #else
  136. leaq 8(%rbp), %rdi
  137. movq (%rbp), %rdx
  138. #endif
  139. movq RIP(%rsp), %rsi
  140. subq $MCOUNT_INSN_SIZE, %rsi
  141. call prepare_ftrace_return
  142. MCOUNT_RESTORE_FRAME
  143. retq
  144. END(ftrace_graph_caller)
  145. GLOBAL(return_to_handler)
  146. subq $24, %rsp
  147. /* Save the return values */
  148. movq %rax, (%rsp)
  149. movq %rdx, 8(%rsp)
  150. movq %rbp, %rdi
  151. call ftrace_return_to_handler
  152. movq %rax, %rdi
  153. movq 8(%rsp), %rdx
  154. movq (%rsp), %rax
  155. addq $24, %rsp
  156. jmp *%rdi
  157. #endif