ftrace_32.S 5.1 KB

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