static_call.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_STATIC_CALL_H
  3. #define _ASM_STATIC_CALL_H
  4. #include <asm/text-patching.h>
  5. /*
  6. * For CONFIG_HAVE_STATIC_CALL_INLINE, this is a temporary trampoline which
  7. * uses the current value of the key->func pointer to do an indirect jump to
  8. * the function. This trampoline is only used during boot, before the call
  9. * sites get patched by static_call_update(). The name of this trampoline has
  10. * a magical aspect: objtool uses it to find static call sites so it can create
  11. * the .static_call_sites section.
  12. *
  13. * For CONFIG_HAVE_STATIC_CALL, this is a permanent trampoline which
  14. * does a direct jump to the function. The direct jump gets patched by
  15. * static_call_update().
  16. *
  17. * Having the trampoline in a special section forces GCC to emit a JMP.d32 when
  18. * it does tail-call optimization on the call; since you cannot compute the
  19. * relative displacement across sections.
  20. */
  21. /*
  22. * The trampoline is 8 bytes and of the general form:
  23. *
  24. * jmp.d32 \func
  25. * ud1 %esp, %ecx
  26. *
  27. * That trailing #UD provides both a speculation stop and serves as a unique
  28. * 3 byte signature identifying static call trampolines. Also see tramp_ud[]
  29. * and __static_call_fixup().
  30. */
  31. #define __ARCH_DEFINE_STATIC_CALL_TRAMP(name, insns) \
  32. asm(".pushsection .static_call.text, \"ax\" \n" \
  33. ".align 4 \n" \
  34. ".globl " STATIC_CALL_TRAMP_STR(name) " \n" \
  35. STATIC_CALL_TRAMP_STR(name) ": \n" \
  36. ANNOTATE_NOENDBR \
  37. insns " \n" \
  38. ".byte 0x0f, 0xb9, 0xcc \n" \
  39. ".type " STATIC_CALL_TRAMP_STR(name) ", @function \n" \
  40. ".size " STATIC_CALL_TRAMP_STR(name) ", . - " STATIC_CALL_TRAMP_STR(name) " \n" \
  41. ".popsection \n")
  42. #define ARCH_DEFINE_STATIC_CALL_TRAMP(name, func) \
  43. __ARCH_DEFINE_STATIC_CALL_TRAMP(name, ".byte 0xe9; .long " #func " - (. + 4)")
  44. #ifdef CONFIG_RETHUNK
  45. #define ARCH_DEFINE_STATIC_CALL_NULL_TRAMP(name) \
  46. __ARCH_DEFINE_STATIC_CALL_TRAMP(name, "jmp __x86_return_thunk")
  47. #else
  48. #define ARCH_DEFINE_STATIC_CALL_NULL_TRAMP(name) \
  49. __ARCH_DEFINE_STATIC_CALL_TRAMP(name, "ret; int3; nop; nop; nop")
  50. #endif
  51. #define ARCH_DEFINE_STATIC_CALL_RET0_TRAMP(name) \
  52. ARCH_DEFINE_STATIC_CALL_TRAMP(name, __static_call_return0)
  53. #define ARCH_ADD_TRAMP_KEY(name) \
  54. asm(".pushsection .static_call_tramp_key, \"a\" \n" \
  55. ".long " STATIC_CALL_TRAMP_STR(name) " - . \n" \
  56. ".long " STATIC_CALL_KEY_STR(name) " - . \n" \
  57. ".popsection \n")
  58. extern bool __static_call_fixup(void *tramp, u8 op, void *dest);
  59. #endif /* _ASM_STATIC_CALL_H */