1  /* SPDX-License-Identifier: GPL-2.0 */
2  
3  #undef TRACE_SYSTEM_VAR
4  
5  #ifdef CONFIG_BPF_EVENTS
6  
7  #include "stages/stage6_event_callback.h"
8  
9  #undef __perf_count
10  #define __perf_count(c)	(c)
11  
12  #undef __perf_task
13  #define __perf_task(t)	(t)
14  
15  #include <linux/args.h>
16  
17  /* cast any integer, pointer, or small struct to u64 */
18  #define UINTTYPE(size) \
19  	__typeof__(__builtin_choose_expr(size == 1,  (u8)1, \
20  		   __builtin_choose_expr(size == 2, (u16)2, \
21  		   __builtin_choose_expr(size == 4, (u32)3, \
22  		   __builtin_choose_expr(size == 8, (u64)4, \
23  					 (void)5)))))
24  #define __CAST_TO_U64(x) ({ \
25  	typeof(x) __src = (x); \
26  	UINTTYPE(sizeof(x)) __dst; \
27  	memcpy(&__dst, &__src, sizeof(__dst)); \
28  	(u64)__dst; })
29  
30  #define __CAST1(a,...) __CAST_TO_U64(a)
31  #define __CAST2(a,...) __CAST_TO_U64(a), __CAST1(__VA_ARGS__)
32  #define __CAST3(a,...) __CAST_TO_U64(a), __CAST2(__VA_ARGS__)
33  #define __CAST4(a,...) __CAST_TO_U64(a), __CAST3(__VA_ARGS__)
34  #define __CAST5(a,...) __CAST_TO_U64(a), __CAST4(__VA_ARGS__)
35  #define __CAST6(a,...) __CAST_TO_U64(a), __CAST5(__VA_ARGS__)
36  #define __CAST7(a,...) __CAST_TO_U64(a), __CAST6(__VA_ARGS__)
37  #define __CAST8(a,...) __CAST_TO_U64(a), __CAST7(__VA_ARGS__)
38  #define __CAST9(a,...) __CAST_TO_U64(a), __CAST8(__VA_ARGS__)
39  #define __CAST10(a,...) __CAST_TO_U64(a), __CAST9(__VA_ARGS__)
40  #define __CAST11(a,...) __CAST_TO_U64(a), __CAST10(__VA_ARGS__)
41  #define __CAST12(a,...) __CAST_TO_U64(a), __CAST11(__VA_ARGS__)
42  /* tracepoints with more than 12 arguments will hit build error */
43  #define CAST_TO_U64(...) CONCATENATE(__CAST, COUNT_ARGS(__VA_ARGS__))(__VA_ARGS__)
44  
45  #define __BPF_DECLARE_TRACE(call, proto, args)				\
46  static notrace void							\
47  __bpf_trace_##call(void *__data, proto)					\
48  {									\
49  	CONCATENATE(bpf_trace_run, COUNT_ARGS(args))(__data, CAST_TO_U64(args));	\
50  }
51  
52  #undef DECLARE_EVENT_CLASS
53  #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)	\
54  	__BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args))
55  
56  /*
57   * This part is compiled out, it is only here as a build time check
58   * to make sure that if the tracepoint handling changes, the
59   * bpf probe will fail to compile unless it too is updated.
60   */
61  #define __DEFINE_EVENT(template, call, proto, args, size)		\
62  static inline void bpf_test_probe_##call(void)				\
63  {									\
64  	check_trace_callback_type_##call(__bpf_trace_##template);	\
65  }									\
66  typedef void (*btf_trace_##call)(void *__data, proto);			\
67  static union {								\
68  	struct bpf_raw_event_map event;					\
69  	btf_trace_##call handler;					\
70  } __bpf_trace_tp_map_##call __used					\
71  __section("__bpf_raw_tp_map") = {					\
72  	.event = {							\
73  		.tp		= &__tracepoint_##call,			\
74  		.bpf_func	= __bpf_trace_##template,		\
75  		.num_args	= COUNT_ARGS(args),			\
76  		.writable_size	= size,					\
77  	},								\
78  };
79  
80  #define FIRST(x, ...) x
81  
82  #define __CHECK_WRITABLE_BUF_SIZE(call, proto, args, size)		\
83  static inline void bpf_test_buffer_##call(void)				\
84  {									\
85  	/* BUILD_BUG_ON() is ignored if the code is completely eliminated, but \
86  	 * BUILD_BUG_ON_ZERO() uses a different mechanism that is not	\
87  	 * dead-code-eliminated.					\
88  	 */								\
89  	FIRST(proto);							\
90  	(void)BUILD_BUG_ON_ZERO(size != sizeof(*FIRST(args)));		\
91  }
92  
93  #undef DEFINE_EVENT_WRITABLE
94  #define DEFINE_EVENT_WRITABLE(template, call, proto, args, size) \
95  	__CHECK_WRITABLE_BUF_SIZE(call, PARAMS(proto), PARAMS(args), size) \
96  	__DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), size)
97  
98  #undef DEFINE_EVENT
99  #define DEFINE_EVENT(template, call, proto, args)			\
100  	__DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), 0)
101  
102  #undef DEFINE_EVENT_PRINT
103  #define DEFINE_EVENT_PRINT(template, name, proto, args, print)	\
104  	DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
105  
106  #undef DECLARE_TRACE
107  #define DECLARE_TRACE(call, proto, args)				\
108  	__BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args))		\
109  	__DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), 0)
110  
111  #undef DECLARE_TRACE_WRITABLE
112  #define DECLARE_TRACE_WRITABLE(call, proto, args, size) \
113  	__CHECK_WRITABLE_BUF_SIZE(call, PARAMS(proto), PARAMS(args), size) \
114  	__BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args)) \
115  	__DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), size)
116  
117  #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
118  
119  #undef DECLARE_TRACE_WRITABLE
120  #undef DEFINE_EVENT_WRITABLE
121  #undef __CHECK_WRITABLE_BUF_SIZE
122  #undef __DEFINE_EVENT
123  #undef FIRST
124  
125  #endif /* CONFIG_BPF_EVENTS */
126