/* SPDX-License-Identifier: GPL-2.0 */1/*2* Copyright (C) 2017 Steven Rostedt, VMware Inc.3*/45#include <linux/export.h>6#include <linux/linkage.h>7#include <asm/page_types.h>8#include <asm/segment.h>9#include <asm/ftrace.h>10#include <asm/nospec-branch.h>11#include <asm/frame.h>12#include <asm/asm-offsets.h>1314#ifdef CONFIG_FRAME_POINTER15# define MCOUNT_FRAME 1 /* using frame = true */16#else17# define MCOUNT_FRAME 0 /* using frame = false */18#endif1920SYM_FUNC_START(__fentry__)21RET22SYM_FUNC_END(__fentry__)23EXPORT_SYMBOL(__fentry__)2425SYM_CODE_START(ftrace_caller)2627#ifdef CONFIG_FRAME_POINTER28/*29* Frame pointers are of ip followed by bp.30* Since fentry is an immediate jump, we are left with31* parent-ip, function-ip. We need to add a frame with32* parent-ip followed by ebp.33*/34pushl 4(%esp) /* parent ip */35pushl %ebp36movl %esp, %ebp37pushl 2*4(%esp) /* function ip */3839/* For mcount, the function ip is directly above */40pushl %ebp41movl %esp, %ebp42#endif43pushl %eax44pushl %ecx45pushl %edx46pushl $0 /* Pass NULL as regs pointer */4748#ifdef CONFIG_FRAME_POINTER49/* Load parent ebp into edx */50movl 4*4(%esp), %edx51#else52/* There's no frame pointer, load the appropriate stack addr instead */53lea 4*4(%esp), %edx54#endif5556movl (MCOUNT_FRAME+4)*4(%esp), %eax /* load the rip */57/* Get the parent ip */58movl 4(%edx), %edx /* edx has ebp */5960movl function_trace_op, %ecx61subl $MCOUNT_INSN_SIZE, %eax6263.globl ftrace_call64ftrace_call:65call ftrace_stub6667addl $4, %esp /* skip NULL pointer */68popl %edx69popl %ecx70popl %eax71#ifdef CONFIG_FRAME_POINTER72popl %ebp73addl $4,%esp /* skip function ip */74popl %ebp /* this is the orig bp */75addl $4, %esp /* skip parent ip */76#endif77.Lftrace_ret:78#ifdef CONFIG_FUNCTION_GRAPH_TRACER79.globl ftrace_graph_call80ftrace_graph_call:81jmp ftrace_stub82#endif8384/* This is weak to keep gas from relaxing the jumps */85SYM_INNER_LABEL_ALIGN(ftrace_stub, SYM_L_WEAK)86RET87SYM_CODE_END(ftrace_caller)8889SYM_CODE_START(ftrace_regs_caller)90/*91* We're here from an mcount/fentry CALL, and the stack frame looks like:92*93* <previous context>94* RET-IP95*96* The purpose of this function is to call out in an emulated INT397* environment with a stack frame like:98*99* <previous context>100* gap / RET-IP101* gap102* gap103* gap104* pt_regs105*106* We do _NOT_ restore: ss, flags, cs, gs, fs, es, ds107*/108subl $3*4, %esp # RET-IP + 3 gaps109pushl %ss # ss110pushl %esp # points at ss111addl $5*4, (%esp) # make it point at <previous context>112pushfl # flags113pushl $__KERNEL_CS # cs114pushl 7*4(%esp) # ip <- RET-IP115pushl $0 # orig_eax116117pushl %gs118pushl %fs119pushl %es120pushl %ds121122pushl %eax123pushl %ebp124pushl %edi125pushl %esi126pushl %edx127pushl %ecx128pushl %ebx129130ENCODE_FRAME_POINTER131132movl PT_EIP(%esp), %eax # 1st argument: IP133subl $MCOUNT_INSN_SIZE, %eax134movl 21*4(%esp), %edx # 2nd argument: parent ip135movl function_trace_op, %ecx # 3rd argument: ftrace_pos136pushl %esp # 4th argument: pt_regs137138SYM_INNER_LABEL(ftrace_regs_call, SYM_L_GLOBAL)139call ftrace_stub140141addl $4, %esp # skip 4th argument142143/* place IP below the new SP */144movl PT_OLDESP(%esp), %eax145movl PT_EIP(%esp), %ecx146movl %ecx, -4(%eax)147148/* place EAX below that */149movl PT_EAX(%esp), %ecx150movl %ecx, -8(%eax)151152popl %ebx153popl %ecx154popl %edx155popl %esi156popl %edi157popl %ebp158159lea -8(%eax), %esp160popl %eax161162jmp .Lftrace_ret163SYM_CODE_END(ftrace_regs_caller)164165SYM_FUNC_START(ftrace_stub_direct_tramp)166CALL_DEPTH_ACCOUNT167RET168SYM_FUNC_END(ftrace_stub_direct_tramp)169170#ifdef CONFIG_FUNCTION_GRAPH_TRACER171SYM_CODE_START(ftrace_graph_caller)172pushl %eax173pushl %ecx174pushl %edx175movl 3*4(%esp), %eax176/* Even with frame pointers, fentry doesn't have one here */177lea 4*4(%esp), %edx178movl $0, %ecx179subl $MCOUNT_INSN_SIZE, %eax180call prepare_ftrace_return181popl %edx182popl %ecx183popl %eax184RET185SYM_CODE_END(ftrace_graph_caller)186187.globl return_to_handler188return_to_handler:189subl $(PTREGS_SIZE), %esp190movl $0, PT_EBP(%esp)191movl %edx, PT_EDX(%esp)192movl %eax, PT_EAX(%esp)193movl %esp, %eax194call ftrace_return_to_handler195movl %eax, %ecx196movl PT_EAX(%esp), %eax197movl PT_EDX(%esp), %edx198addl $(PTREGS_SIZE), %esp199JMP_NOSPEC ecx200#endif201202203