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