vendor_hooks.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Note: we intentionally omit include file ifdef protection
  4. * This is due to the way trace events work. If a file includes two
  5. * trace event headers under one "CREATE_TRACE_POINTS" the first include
  6. * will override the DECLARE_RESTRICTED_HOOK and break the second include.
  7. */
  8. #ifndef __GENKSYMS__
  9. #include <linux/tracepoint.h>
  10. #endif
  11. #if defined(CONFIG_TRACEPOINTS) && defined(CONFIG_ANDROID_VENDOR_HOOKS)
  12. #define DECLARE_HOOK DECLARE_TRACE
  13. int android_rvh_probe_register(struct tracepoint *tp, void *probe, void *data);
  14. #ifdef TRACE_HEADER_MULTI_READ
  15. #define DEFINE_HOOK_FN(_name, _reg, _unreg, proto, args) \
  16. static const char __tpstrtab_##_name[] \
  17. __section("__tracepoints_strings") = #_name; \
  18. extern struct static_call_key STATIC_CALL_KEY(tp_func_##_name); \
  19. int __traceiter_##_name(void *__data, proto); \
  20. struct tracepoint __tracepoint_##_name __used \
  21. __section("__tracepoints") = { \
  22. .name = __tpstrtab_##_name, \
  23. .key = STATIC_KEY_INIT_FALSE, \
  24. .static_call_key = &STATIC_CALL_KEY(tp_func_##_name), \
  25. .static_call_tramp = STATIC_CALL_TRAMP_ADDR(tp_func_##_name), \
  26. .iterator = &__traceiter_##_name, \
  27. .regfunc = _reg, \
  28. .unregfunc = _unreg, \
  29. .funcs = NULL }; \
  30. __TRACEPOINT_ENTRY(_name); \
  31. int __traceiter_##_name(void *__data, proto) \
  32. { \
  33. struct tracepoint_func *it_func_ptr; \
  34. void *it_func; \
  35. \
  36. it_func_ptr = (&__tracepoint_##_name)->funcs; \
  37. it_func = (it_func_ptr)->func; \
  38. do { \
  39. __data = (it_func_ptr)->data; \
  40. ((void(*)(void *, proto))(it_func))(__data, args); \
  41. it_func = READ_ONCE((++it_func_ptr)->func); \
  42. } while (it_func); \
  43. return 0; \
  44. } \
  45. DEFINE_STATIC_CALL(tp_func_##_name, __traceiter_##_name);
  46. #undef DECLARE_RESTRICTED_HOOK
  47. #define DECLARE_RESTRICTED_HOOK(name, proto, args, cond) \
  48. DEFINE_HOOK_FN(name, NULL, NULL, PARAMS(proto), PARAMS(args))
  49. /* prevent additional recursion */
  50. #undef TRACE_HEADER_MULTI_READ
  51. #else /* TRACE_HEADER_MULTI_READ */
  52. #ifdef CONFIG_HAVE_STATIC_CALL
  53. #define __DO_RESTRICTED_HOOK_CALL(name, args) \
  54. do { \
  55. struct tracepoint_func *it_func_ptr; \
  56. void *__data; \
  57. it_func_ptr = (&__tracepoint_##name)->funcs; \
  58. if (it_func_ptr) { \
  59. __data = (it_func_ptr)->data; \
  60. static_call(tp_func_##name)(__data, args); \
  61. } \
  62. } while (0)
  63. #else
  64. #define __DO_RESTRICTED_HOOK_CALL(name, args) __traceiter_##name(NULL, args)
  65. #endif
  66. #define DO_RESTRICTED_HOOK(name, args, cond) \
  67. do { \
  68. if (!(cond)) \
  69. return; \
  70. \
  71. __DO_RESTRICTED_HOOK_CALL(name, TP_ARGS(args)); \
  72. } while (0)
  73. #define __DECLARE_RESTRICTED_HOOK(name, proto, args, cond, data_proto) \
  74. extern int __traceiter_##name(data_proto); \
  75. DECLARE_STATIC_CALL(tp_func_##name, __traceiter_##name); \
  76. extern struct tracepoint __tracepoint_##name; \
  77. static inline void trace_##name(proto) \
  78. { \
  79. if (static_key_false(&__tracepoint_##name.key)) \
  80. DO_RESTRICTED_HOOK(name, \
  81. TP_ARGS(args), \
  82. TP_CONDITION(cond)); \
  83. } \
  84. static inline bool \
  85. trace_##name##_enabled(void) \
  86. { \
  87. return static_key_false(&__tracepoint_##name.key); \
  88. } \
  89. static inline int \
  90. register_trace_##name(void (*probe)(data_proto), void *data) \
  91. { \
  92. return android_rvh_probe_register(&__tracepoint_##name, \
  93. (void *)probe, data); \
  94. } \
  95. /* vendor hooks cannot be unregistered */ \
  96. #undef DECLARE_RESTRICTED_HOOK
  97. #define DECLARE_RESTRICTED_HOOK(name, proto, args, cond) \
  98. __DECLARE_RESTRICTED_HOOK(name, PARAMS(proto), PARAMS(args), \
  99. cond, \
  100. PARAMS(void *__data, proto))
  101. #endif /* TRACE_HEADER_MULTI_READ */
  102. #else /* !CONFIG_TRACEPOINTS || !CONFIG_ANDROID_VENDOR_HOOKS */
  103. /* suppress trace hooks */
  104. #define DECLARE_HOOK DECLARE_EVENT_NOP
  105. #define DECLARE_RESTRICTED_HOOK(name, proto, args, cond) \
  106. DECLARE_EVENT_NOP(name, PARAMS(proto), PARAMS(args))
  107. #endif