static_call_types.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _STATIC_CALL_TYPES_H
  3. #define _STATIC_CALL_TYPES_H
  4. #include <linux/types.h>
  5. #include <linux/stringify.h>
  6. #include <linux/compiler.h>
  7. #define STATIC_CALL_KEY_PREFIX __SCK__
  8. #define STATIC_CALL_KEY_PREFIX_STR __stringify(STATIC_CALL_KEY_PREFIX)
  9. #define STATIC_CALL_KEY_PREFIX_LEN (sizeof(STATIC_CALL_KEY_PREFIX_STR) - 1)
  10. #define STATIC_CALL_KEY(name) __PASTE(STATIC_CALL_KEY_PREFIX, name)
  11. #define STATIC_CALL_KEY_STR(name) __stringify(STATIC_CALL_KEY(name))
  12. #define STATIC_CALL_TRAMP_PREFIX __SCT__
  13. #define STATIC_CALL_TRAMP_PREFIX_STR __stringify(STATIC_CALL_TRAMP_PREFIX)
  14. #define STATIC_CALL_TRAMP_PREFIX_LEN (sizeof(STATIC_CALL_TRAMP_PREFIX_STR) - 1)
  15. #define STATIC_CALL_TRAMP(name) __PASTE(STATIC_CALL_TRAMP_PREFIX, name)
  16. #define STATIC_CALL_TRAMP_STR(name) __stringify(STATIC_CALL_TRAMP(name))
  17. /*
  18. * Flags in the low bits of static_call_site::key.
  19. */
  20. #define STATIC_CALL_SITE_TAIL 1UL /* tail call */
  21. #define STATIC_CALL_SITE_INIT 2UL /* init section */
  22. #define STATIC_CALL_SITE_FLAGS 3UL
  23. /*
  24. * The static call site table needs to be created by external tooling (objtool
  25. * or a compiler plugin).
  26. */
  27. struct static_call_site {
  28. s32 addr;
  29. s32 key;
  30. };
  31. #define DECLARE_STATIC_CALL(name, func) \
  32. extern struct static_call_key STATIC_CALL_KEY(name); \
  33. extern typeof(func) STATIC_CALL_TRAMP(name);
  34. #ifdef CONFIG_HAVE_STATIC_CALL
  35. #define __raw_static_call(name) (&STATIC_CALL_TRAMP(name))
  36. #ifdef CONFIG_HAVE_STATIC_CALL_INLINE
  37. /*
  38. * __ADDRESSABLE() is used to ensure the key symbol doesn't get stripped from
  39. * the symbol table so that objtool can reference it when it generates the
  40. * .static_call_sites section.
  41. */
  42. #define __STATIC_CALL_ADDRESSABLE(name) \
  43. __ADDRESSABLE(STATIC_CALL_KEY(name))
  44. #define __static_call(name) \
  45. ({ \
  46. __STATIC_CALL_ADDRESSABLE(name); \
  47. __raw_static_call(name); \
  48. })
  49. #else /* !CONFIG_HAVE_STATIC_CALL_INLINE */
  50. #define __STATIC_CALL_ADDRESSABLE(name)
  51. #define __static_call(name) __raw_static_call(name)
  52. #endif /* CONFIG_HAVE_STATIC_CALL_INLINE */
  53. #ifdef MODULE
  54. #define __STATIC_CALL_MOD_ADDRESSABLE(name)
  55. #define static_call_mod(name) __raw_static_call(name)
  56. #else
  57. #define __STATIC_CALL_MOD_ADDRESSABLE(name) __STATIC_CALL_ADDRESSABLE(name)
  58. #define static_call_mod(name) __static_call(name)
  59. #endif
  60. #define static_call(name) __static_call(name)
  61. #else
  62. #define static_call(name) \
  63. ((typeof(STATIC_CALL_TRAMP(name))*)(STATIC_CALL_KEY(name).func))
  64. #endif /* CONFIG_HAVE_STATIC_CALL */
  65. #endif /* _STATIC_CALL_TYPES_H */