18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _ASM_STATIC_CALL_H
38c2ecf20Sopenharmony_ci#define _ASM_STATIC_CALL_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <asm/text-patching.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci/*
88c2ecf20Sopenharmony_ci * For CONFIG_HAVE_STATIC_CALL_INLINE, this is a temporary trampoline which
98c2ecf20Sopenharmony_ci * uses the current value of the key->func pointer to do an indirect jump to
108c2ecf20Sopenharmony_ci * the function.  This trampoline is only used during boot, before the call
118c2ecf20Sopenharmony_ci * sites get patched by static_call_update().  The name of this trampoline has
128c2ecf20Sopenharmony_ci * a magical aspect: objtool uses it to find static call sites so it can create
138c2ecf20Sopenharmony_ci * the .static_call_sites section.
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * For CONFIG_HAVE_STATIC_CALL, this is a permanent trampoline which
168c2ecf20Sopenharmony_ci * does a direct jump to the function.  The direct jump gets patched by
178c2ecf20Sopenharmony_ci * static_call_update().
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * Having the trampoline in a special section forces GCC to emit a JMP.d32 when
208c2ecf20Sopenharmony_ci * it does tail-call optimization on the call; since you cannot compute the
218c2ecf20Sopenharmony_ci * relative displacement across sections.
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/*
258c2ecf20Sopenharmony_ci * The trampoline is 8 bytes and of the general form:
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci *   jmp.d32 \func
288c2ecf20Sopenharmony_ci *   ud1 %esp, %ecx
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci * That trailing #UD provides both a speculation stop and serves as a unique
318c2ecf20Sopenharmony_ci * 3 byte signature identifying static call trampolines. Also see tramp_ud[]
328c2ecf20Sopenharmony_ci * and __static_call_fixup().
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci#define __ARCH_DEFINE_STATIC_CALL_TRAMP(name, insns)			\
358c2ecf20Sopenharmony_ci	asm(".pushsection .static_call.text, \"ax\"		\n"	\
368c2ecf20Sopenharmony_ci	    ".align 4						\n"	\
378c2ecf20Sopenharmony_ci	    ".globl " STATIC_CALL_TRAMP_STR(name) "		\n"	\
388c2ecf20Sopenharmony_ci	    STATIC_CALL_TRAMP_STR(name) ":			\n"	\
398c2ecf20Sopenharmony_ci	    insns "						\n"	\
408c2ecf20Sopenharmony_ci	    ".type " STATIC_CALL_TRAMP_STR(name) ", @function	\n"	\
418c2ecf20Sopenharmony_ci	    ".size " STATIC_CALL_TRAMP_STR(name) ", . - " STATIC_CALL_TRAMP_STR(name) " \n" \
428c2ecf20Sopenharmony_ci	    ".popsection					\n")
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define ARCH_DEFINE_STATIC_CALL_TRAMP(name, func)			\
458c2ecf20Sopenharmony_ci	__ARCH_DEFINE_STATIC_CALL_TRAMP(name, ".byte 0xe9; .long " #func " - (. + 4)")
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#ifdef CONFIG_RETHUNK
488c2ecf20Sopenharmony_ci#define ARCH_DEFINE_STATIC_CALL_NULL_TRAMP(name)			\
498c2ecf20Sopenharmony_ci	__ARCH_DEFINE_STATIC_CALL_TRAMP(name, "jmp __x86_return_thunk")
508c2ecf20Sopenharmony_ci#else
518c2ecf20Sopenharmony_ci#define ARCH_DEFINE_STATIC_CALL_NULL_TRAMP(name)			\
528c2ecf20Sopenharmony_ci	__ARCH_DEFINE_STATIC_CALL_TRAMP(name, "ret; int3; nop; nop; nop")
538c2ecf20Sopenharmony_ci#endif
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#define ARCH_ADD_TRAMP_KEY(name)					\
578c2ecf20Sopenharmony_ci	asm(".pushsection .static_call_tramp_key, \"a\"		\n"	\
588c2ecf20Sopenharmony_ci	    ".long " STATIC_CALL_TRAMP_STR(name) " - .		\n"	\
598c2ecf20Sopenharmony_ci	    ".long " STATIC_CALL_KEY_STR(name) " - .		\n"	\
608c2ecf20Sopenharmony_ci	    ".popsection					\n")
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ciextern bool __static_call_fixup(void *tramp, u8 op, void *dest);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#endif /* _ASM_STATIC_CALL_H */
65