xref: /openbmc/linux/tools/lib/bpf/bpf_tracing.h (revision dc6a81c3)
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 #ifndef __BPF_TRACING_H__
3 #define __BPF_TRACING_H__
4 
5 /* Scan the ARCH passed in from ARCH env variable (see Makefile) */
6 #if defined(__TARGET_ARCH_x86)
7 	#define bpf_target_x86
8 	#define bpf_target_defined
9 #elif defined(__TARGET_ARCH_s390)
10 	#define bpf_target_s390
11 	#define bpf_target_defined
12 #elif defined(__TARGET_ARCH_arm)
13 	#define bpf_target_arm
14 	#define bpf_target_defined
15 #elif defined(__TARGET_ARCH_arm64)
16 	#define bpf_target_arm64
17 	#define bpf_target_defined
18 #elif defined(__TARGET_ARCH_mips)
19 	#define bpf_target_mips
20 	#define bpf_target_defined
21 #elif defined(__TARGET_ARCH_powerpc)
22 	#define bpf_target_powerpc
23 	#define bpf_target_defined
24 #elif defined(__TARGET_ARCH_sparc)
25 	#define bpf_target_sparc
26 	#define bpf_target_defined
27 #else
28 	#undef bpf_target_defined
29 #endif
30 
31 /* Fall back to what the compiler says */
32 #ifndef bpf_target_defined
33 #if defined(__x86_64__)
34 	#define bpf_target_x86
35 #elif defined(__s390__)
36 	#define bpf_target_s390
37 #elif defined(__arm__)
38 	#define bpf_target_arm
39 #elif defined(__aarch64__)
40 	#define bpf_target_arm64
41 #elif defined(__mips__)
42 	#define bpf_target_mips
43 #elif defined(__powerpc__)
44 	#define bpf_target_powerpc
45 #elif defined(__sparc__)
46 	#define bpf_target_sparc
47 #endif
48 #endif
49 
50 #if defined(bpf_target_x86)
51 
52 #ifdef __KERNEL__
53 #define PT_REGS_PARM1(x) ((x)->di)
54 #define PT_REGS_PARM2(x) ((x)->si)
55 #define PT_REGS_PARM3(x) ((x)->dx)
56 #define PT_REGS_PARM4(x) ((x)->cx)
57 #define PT_REGS_PARM5(x) ((x)->r8)
58 #define PT_REGS_RET(x) ((x)->sp)
59 #define PT_REGS_FP(x) ((x)->bp)
60 #define PT_REGS_RC(x) ((x)->ax)
61 #define PT_REGS_SP(x) ((x)->sp)
62 #define PT_REGS_IP(x) ((x)->ip)
63 #else
64 #ifdef __i386__
65 /* i386 kernel is built with -mregparm=3 */
66 #define PT_REGS_PARM1(x) ((x)->eax)
67 #define PT_REGS_PARM2(x) ((x)->edx)
68 #define PT_REGS_PARM3(x) ((x)->ecx)
69 #define PT_REGS_PARM4(x) 0
70 #define PT_REGS_PARM5(x) 0
71 #define PT_REGS_RET(x) ((x)->esp)
72 #define PT_REGS_FP(x) ((x)->ebp)
73 #define PT_REGS_RC(x) ((x)->eax)
74 #define PT_REGS_SP(x) ((x)->esp)
75 #define PT_REGS_IP(x) ((x)->eip)
76 #else
77 #define PT_REGS_PARM1(x) ((x)->rdi)
78 #define PT_REGS_PARM2(x) ((x)->rsi)
79 #define PT_REGS_PARM3(x) ((x)->rdx)
80 #define PT_REGS_PARM4(x) ((x)->rcx)
81 #define PT_REGS_PARM5(x) ((x)->r8)
82 #define PT_REGS_RET(x) ((x)->rsp)
83 #define PT_REGS_FP(x) ((x)->rbp)
84 #define PT_REGS_RC(x) ((x)->rax)
85 #define PT_REGS_SP(x) ((x)->rsp)
86 #define PT_REGS_IP(x) ((x)->rip)
87 #endif
88 #endif
89 
90 #elif defined(bpf_target_s390)
91 
92 /* s390 provides user_pt_regs instead of struct pt_regs to userspace */
93 struct pt_regs;
94 #define PT_REGS_S390 const volatile user_pt_regs
95 #define PT_REGS_PARM1(x) (((PT_REGS_S390 *)(x))->gprs[2])
96 #define PT_REGS_PARM2(x) (((PT_REGS_S390 *)(x))->gprs[3])
97 #define PT_REGS_PARM3(x) (((PT_REGS_S390 *)(x))->gprs[4])
98 #define PT_REGS_PARM4(x) (((PT_REGS_S390 *)(x))->gprs[5])
99 #define PT_REGS_PARM5(x) (((PT_REGS_S390 *)(x))->gprs[6])
100 #define PT_REGS_RET(x) (((PT_REGS_S390 *)(x))->gprs[14])
101 /* Works only with CONFIG_FRAME_POINTER */
102 #define PT_REGS_FP(x) (((PT_REGS_S390 *)(x))->gprs[11])
103 #define PT_REGS_RC(x) (((PT_REGS_S390 *)(x))->gprs[2])
104 #define PT_REGS_SP(x) (((PT_REGS_S390 *)(x))->gprs[15])
105 #define PT_REGS_IP(x) (((PT_REGS_S390 *)(x))->psw.addr)
106 
107 #elif defined(bpf_target_arm)
108 
109 #define PT_REGS_PARM1(x) ((x)->uregs[0])
110 #define PT_REGS_PARM2(x) ((x)->uregs[1])
111 #define PT_REGS_PARM3(x) ((x)->uregs[2])
112 #define PT_REGS_PARM4(x) ((x)->uregs[3])
113 #define PT_REGS_PARM5(x) ((x)->uregs[4])
114 #define PT_REGS_RET(x) ((x)->uregs[14])
115 #define PT_REGS_FP(x) ((x)->uregs[11]) /* Works only with CONFIG_FRAME_POINTER */
116 #define PT_REGS_RC(x) ((x)->uregs[0])
117 #define PT_REGS_SP(x) ((x)->uregs[13])
118 #define PT_REGS_IP(x) ((x)->uregs[12])
119 
120 #elif defined(bpf_target_arm64)
121 
122 /* arm64 provides struct user_pt_regs instead of struct pt_regs to userspace */
123 struct pt_regs;
124 #define PT_REGS_ARM64 const volatile struct user_pt_regs
125 #define PT_REGS_PARM1(x) (((PT_REGS_ARM64 *)(x))->regs[0])
126 #define PT_REGS_PARM2(x) (((PT_REGS_ARM64 *)(x))->regs[1])
127 #define PT_REGS_PARM3(x) (((PT_REGS_ARM64 *)(x))->regs[2])
128 #define PT_REGS_PARM4(x) (((PT_REGS_ARM64 *)(x))->regs[3])
129 #define PT_REGS_PARM5(x) (((PT_REGS_ARM64 *)(x))->regs[4])
130 #define PT_REGS_RET(x) (((PT_REGS_ARM64 *)(x))->regs[30])
131 /* Works only with CONFIG_FRAME_POINTER */
132 #define PT_REGS_FP(x) (((PT_REGS_ARM64 *)(x))->regs[29])
133 #define PT_REGS_RC(x) (((PT_REGS_ARM64 *)(x))->regs[0])
134 #define PT_REGS_SP(x) (((PT_REGS_ARM64 *)(x))->sp)
135 #define PT_REGS_IP(x) (((PT_REGS_ARM64 *)(x))->pc)
136 
137 #elif defined(bpf_target_mips)
138 
139 #define PT_REGS_PARM1(x) ((x)->regs[4])
140 #define PT_REGS_PARM2(x) ((x)->regs[5])
141 #define PT_REGS_PARM3(x) ((x)->regs[6])
142 #define PT_REGS_PARM4(x) ((x)->regs[7])
143 #define PT_REGS_PARM5(x) ((x)->regs[8])
144 #define PT_REGS_RET(x) ((x)->regs[31])
145 #define PT_REGS_FP(x) ((x)->regs[30]) /* Works only with CONFIG_FRAME_POINTER */
146 #define PT_REGS_RC(x) ((x)->regs[1])
147 #define PT_REGS_SP(x) ((x)->regs[29])
148 #define PT_REGS_IP(x) ((x)->cp0_epc)
149 
150 #elif defined(bpf_target_powerpc)
151 
152 #define PT_REGS_PARM1(x) ((x)->gpr[3])
153 #define PT_REGS_PARM2(x) ((x)->gpr[4])
154 #define PT_REGS_PARM3(x) ((x)->gpr[5])
155 #define PT_REGS_PARM4(x) ((x)->gpr[6])
156 #define PT_REGS_PARM5(x) ((x)->gpr[7])
157 #define PT_REGS_RC(x) ((x)->gpr[3])
158 #define PT_REGS_SP(x) ((x)->sp)
159 #define PT_REGS_IP(x) ((x)->nip)
160 
161 #elif defined(bpf_target_sparc)
162 
163 #define PT_REGS_PARM1(x) ((x)->u_regs[UREG_I0])
164 #define PT_REGS_PARM2(x) ((x)->u_regs[UREG_I1])
165 #define PT_REGS_PARM3(x) ((x)->u_regs[UREG_I2])
166 #define PT_REGS_PARM4(x) ((x)->u_regs[UREG_I3])
167 #define PT_REGS_PARM5(x) ((x)->u_regs[UREG_I4])
168 #define PT_REGS_RET(x) ((x)->u_regs[UREG_I7])
169 #define PT_REGS_RC(x) ((x)->u_regs[UREG_I0])
170 #define PT_REGS_SP(x) ((x)->u_regs[UREG_FP])
171 
172 /* Should this also be a bpf_target check for the sparc case? */
173 #if defined(__arch64__)
174 #define PT_REGS_IP(x) ((x)->tpc)
175 #else
176 #define PT_REGS_IP(x) ((x)->pc)
177 #endif
178 
179 #endif
180 
181 #if defined(bpf_target_powerpc)
182 #define BPF_KPROBE_READ_RET_IP(ip, ctx)		({ (ip) = (ctx)->link; })
183 #define BPF_KRETPROBE_READ_RET_IP		BPF_KPROBE_READ_RET_IP
184 #elif defined(bpf_target_sparc)
185 #define BPF_KPROBE_READ_RET_IP(ip, ctx)		({ (ip) = PT_REGS_RET(ctx); })
186 #define BPF_KRETPROBE_READ_RET_IP		BPF_KPROBE_READ_RET_IP
187 #else
188 #define BPF_KPROBE_READ_RET_IP(ip, ctx)					    \
189 	({ bpf_probe_read(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); })
190 #define BPF_KRETPROBE_READ_RET_IP(ip, ctx)				    \
191 	({ bpf_probe_read(&(ip), sizeof(ip),				    \
192 			  (void *)(PT_REGS_FP(ctx) + sizeof(ip))); })
193 #endif
194 
195 #endif
196