ftrace_32.S 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (C) 2017 Steven Rostedt, VMware Inc.
  3. */
  4. #include <linux/linkage.h>
  5. #include <asm/page_types.h>
  6. #include <asm/segment.h>
  7. #include <asm/export.h>
  8. #include <asm/ftrace.h>
  9. #ifdef CC_USING_FENTRY
  10. # define function_hook __fentry__
  11. EXPORT_SYMBOL(__fentry__)
  12. #else
  13. # define function_hook mcount
  14. EXPORT_SYMBOL(mcount)
  15. #endif
  16. #ifdef CONFIG_DYNAMIC_FTRACE
  17. /* mcount uses a frame pointer even if CONFIG_FRAME_POINTER is not set */
  18. #if !defined(CC_USING_FENTRY) || defined(CONFIG_FRAME_POINTER)
  19. # define USING_FRAME_POINTER
  20. #endif
  21. #ifdef USING_FRAME_POINTER
  22. # define MCOUNT_FRAME 1 /* using frame = true */
  23. #else
  24. # define MCOUNT_FRAME 0 /* using frame = false */
  25. #endif
  26. ENTRY(function_hook)
  27. ret
  28. END(function_hook)
  29. ENTRY(ftrace_caller)
  30. #ifdef USING_FRAME_POINTER
  31. # ifdef CC_USING_FENTRY
  32. /*
  33. * Frame pointers are of ip followed by bp.
  34. * Since fentry is an immediate jump, we are left with
  35. * parent-ip, function-ip. We need to add a frame with
  36. * parent-ip followed by ebp.
  37. */
  38. pushl 4(%esp) /* parent ip */
  39. pushl %ebp
  40. movl %esp, %ebp
  41. pushl 2*4(%esp) /* function ip */
  42. # endif
  43. /* For mcount, the function ip is directly above */
  44. pushl %ebp
  45. movl %esp, %ebp
  46. #endif
  47. pushl %eax
  48. pushl %ecx
  49. pushl %edx
  50. pushl $0 /* Pass NULL as regs pointer */
  51. #ifdef USING_FRAME_POINTER
  52. /* Load parent ebp into edx */
  53. movl 4*4(%esp), %edx
  54. #else
  55. /* There's no frame pointer, load the appropriate stack addr instead */
  56. lea 4*4(%esp), %edx
  57. #endif
  58. movl (MCOUNT_FRAME+4)*4(%esp), %eax /* load the rip */
  59. /* Get the parent ip */
  60. movl 4(%edx), %edx /* edx has ebp */
  61. movl function_trace_op, %ecx
  62. subl $MCOUNT_INSN_SIZE, %eax
  63. .globl ftrace_call
  64. ftrace_call:
  65. call ftrace_stub
  66. addl $4, %esp /* skip NULL pointer */
  67. popl %edx
  68. popl %ecx
  69. popl %eax
  70. #ifdef USING_FRAME_POINTER
  71. popl %ebp
  72. # ifdef CC_USING_FENTRY
  73. addl $4,%esp /* skip function ip */
  74. popl %ebp /* this is the orig bp */
  75. addl $4, %esp /* skip parent ip */
  76. # endif
  77. #endif
  78. .Lftrace_ret:
  79. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  80. .globl ftrace_graph_call
  81. ftrace_graph_call:
  82. jmp ftrace_stub
  83. #endif
  84. /* This is weak to keep gas from relaxing the jumps */
  85. WEAK(ftrace_stub)
  86. ret
  87. END(ftrace_caller)
  88. ENTRY(ftrace_regs_caller)
  89. /*
  90. * i386 does not save SS and ESP when coming from kernel.
  91. * Instead, to get sp, &regs->sp is used (see ptrace.h).
  92. * Unfortunately, that means eflags must be at the same location
  93. * as the current return ip is. We move the return ip into the
  94. * regs->ip location, and move flags into the return ip location.
  95. */
  96. pushl $__KERNEL_CS
  97. pushl 4(%esp) /* Save the return ip */
  98. pushl $0 /* Load 0 into orig_ax */
  99. pushl %gs
  100. pushl %fs
  101. pushl %es
  102. pushl %ds
  103. pushl %eax
  104. /* Get flags and place them into the return ip slot */
  105. pushf
  106. popl %eax
  107. movl %eax, 8*4(%esp)
  108. pushl %ebp
  109. pushl %edi
  110. pushl %esi
  111. pushl %edx
  112. pushl %ecx
  113. pushl %ebx
  114. movl 12*4(%esp), %eax /* Load ip (1st parameter) */
  115. subl $MCOUNT_INSN_SIZE, %eax /* Adjust ip */
  116. #ifdef CC_USING_FENTRY
  117. movl 15*4(%esp), %edx /* Load parent ip (2nd parameter) */
  118. #else
  119. movl 0x4(%ebp), %edx /* Load parent ip (2nd parameter) */
  120. #endif
  121. movl function_trace_op, %ecx /* Save ftrace_pos in 3rd parameter */
  122. pushl %esp /* Save pt_regs as 4th parameter */
  123. GLOBAL(ftrace_regs_call)
  124. call ftrace_stub
  125. addl $4, %esp /* Skip pt_regs */
  126. /* restore flags */
  127. push 14*4(%esp)
  128. popf
  129. /* Move return ip back to its original location */
  130. movl 12*4(%esp), %eax
  131. movl %eax, 14*4(%esp)
  132. popl %ebx
  133. popl %ecx
  134. popl %edx
  135. popl %esi
  136. popl %edi
  137. popl %ebp
  138. popl %eax
  139. popl %ds
  140. popl %es
  141. popl %fs
  142. popl %gs
  143. /* use lea to not affect flags */
  144. lea 3*4(%esp), %esp /* Skip orig_ax, ip and cs */
  145. jmp .Lftrace_ret
  146. #else /* ! CONFIG_DYNAMIC_FTRACE */
  147. ENTRY(function_hook)
  148. cmpl $__PAGE_OFFSET, %esp
  149. jb ftrace_stub /* Paging not enabled yet? */
  150. cmpl $ftrace_stub, ftrace_trace_function
  151. jnz .Ltrace
  152. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  153. cmpl $ftrace_stub, ftrace_graph_return
  154. jnz ftrace_graph_caller
  155. cmpl $ftrace_graph_entry_stub, ftrace_graph_entry
  156. jnz ftrace_graph_caller
  157. #endif
  158. .globl ftrace_stub
  159. ftrace_stub:
  160. ret
  161. /* taken from glibc */
  162. .Ltrace:
  163. pushl %eax
  164. pushl %ecx
  165. pushl %edx
  166. movl 0xc(%esp), %eax
  167. movl 0x4(%ebp), %edx
  168. subl $MCOUNT_INSN_SIZE, %eax
  169. call *ftrace_trace_function
  170. popl %edx
  171. popl %ecx
  172. popl %eax
  173. jmp ftrace_stub
  174. END(function_hook)
  175. #endif /* CONFIG_DYNAMIC_FTRACE */
  176. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  177. ENTRY(ftrace_graph_caller)
  178. pushl %eax
  179. pushl %ecx
  180. pushl %edx
  181. movl 3*4(%esp), %eax
  182. /* Even with frame pointers, fentry doesn't have one here */
  183. #ifdef CC_USING_FENTRY
  184. lea 4*4(%esp), %edx
  185. movl $0, %ecx
  186. #else
  187. lea 0x4(%ebp), %edx
  188. movl (%ebp), %ecx
  189. #endif
  190. subl $MCOUNT_INSN_SIZE, %eax
  191. call prepare_ftrace_return
  192. popl %edx
  193. popl %ecx
  194. popl %eax
  195. ret
  196. END(ftrace_graph_caller)
  197. .globl return_to_handler
  198. return_to_handler:
  199. pushl %eax
  200. pushl %edx
  201. #ifdef CC_USING_FENTRY
  202. movl $0, %eax
  203. #else
  204. movl %ebp, %eax
  205. #endif
  206. call ftrace_return_to_handler
  207. movl %eax, %ecx
  208. popl %edx
  209. popl %eax
  210. jmp *%ecx
  211. #endif