ftrace_32.S 5.0 KB

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