Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ |
| 2 | #ifndef __BPF_TRACING_H__ |
| 3 | #define __BPF_TRACING_H__ |
| 4 | |
Andrii Nakryiko | 6f5d467 | 2022-07-14 00:07:53 -0700 | [diff] [blame] | 5 | #include <bpf/bpf_helpers.h> |
| 6 | |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 7 | /* Scan the ARCH passed in from ARCH env variable (see Makefile) */ |
| 8 | #if defined(__TARGET_ARCH_x86) |
| 9 | #define bpf_target_x86 |
| 10 | #define bpf_target_defined |
| 11 | #elif defined(__TARGET_ARCH_s390) |
| 12 | #define bpf_target_s390 |
| 13 | #define bpf_target_defined |
| 14 | #elif defined(__TARGET_ARCH_arm) |
| 15 | #define bpf_target_arm |
| 16 | #define bpf_target_defined |
| 17 | #elif defined(__TARGET_ARCH_arm64) |
| 18 | #define bpf_target_arm64 |
| 19 | #define bpf_target_defined |
| 20 | #elif defined(__TARGET_ARCH_mips) |
| 21 | #define bpf_target_mips |
| 22 | #define bpf_target_defined |
| 23 | #elif defined(__TARGET_ARCH_powerpc) |
| 24 | #define bpf_target_powerpc |
| 25 | #define bpf_target_defined |
| 26 | #elif defined(__TARGET_ARCH_sparc) |
| 27 | #define bpf_target_sparc |
| 28 | #define bpf_target_defined |
Björn Töpel | 589fed4 | 2021-10-28 18:10:56 +0200 | [diff] [blame] | 29 | #elif defined(__TARGET_ARCH_riscv) |
| 30 | #define bpf_target_riscv |
| 31 | #define bpf_target_defined |
Vladimir Isaev | 0738599 | 2022-04-09 01:44:42 +0300 | [diff] [blame] | 32 | #elif defined(__TARGET_ARCH_arc) |
| 33 | #define bpf_target_arc |
| 34 | #define bpf_target_defined |
Hengqi Chen | 0088392 | 2022-12-31 18:07:57 +0800 | [diff] [blame] | 35 | #elif defined(__TARGET_ARCH_loongarch) |
| 36 | #define bpf_target_loongarch |
| 37 | #define bpf_target_defined |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 38 | #else |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 39 | |
| 40 | /* Fall back to what the compiler says */ |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 41 | #if defined(__x86_64__) |
| 42 | #define bpf_target_x86 |
Lorenz Bauer | 4a638d5 | 2021-06-16 09:36:35 +0100 | [diff] [blame] | 43 | #define bpf_target_defined |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 44 | #elif defined(__s390__) |
| 45 | #define bpf_target_s390 |
Lorenz Bauer | 4a638d5 | 2021-06-16 09:36:35 +0100 | [diff] [blame] | 46 | #define bpf_target_defined |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 47 | #elif defined(__arm__) |
| 48 | #define bpf_target_arm |
Lorenz Bauer | 4a638d5 | 2021-06-16 09:36:35 +0100 | [diff] [blame] | 49 | #define bpf_target_defined |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 50 | #elif defined(__aarch64__) |
| 51 | #define bpf_target_arm64 |
Lorenz Bauer | 4a638d5 | 2021-06-16 09:36:35 +0100 | [diff] [blame] | 52 | #define bpf_target_defined |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 53 | #elif defined(__mips__) |
| 54 | #define bpf_target_mips |
Lorenz Bauer | 4a638d5 | 2021-06-16 09:36:35 +0100 | [diff] [blame] | 55 | #define bpf_target_defined |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 56 | #elif defined(__powerpc__) |
| 57 | #define bpf_target_powerpc |
Lorenz Bauer | 4a638d5 | 2021-06-16 09:36:35 +0100 | [diff] [blame] | 58 | #define bpf_target_defined |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 59 | #elif defined(__sparc__) |
| 60 | #define bpf_target_sparc |
Lorenz Bauer | 4a638d5 | 2021-06-16 09:36:35 +0100 | [diff] [blame] | 61 | #define bpf_target_defined |
Björn Töpel | 589fed4 | 2021-10-28 18:10:56 +0200 | [diff] [blame] | 62 | #elif defined(__riscv) && __riscv_xlen == 64 |
| 63 | #define bpf_target_riscv |
| 64 | #define bpf_target_defined |
Vladimir Isaev | 0738599 | 2022-04-09 01:44:42 +0300 | [diff] [blame] | 65 | #elif defined(__arc__) |
| 66 | #define bpf_target_arc |
| 67 | #define bpf_target_defined |
Hengqi Chen | 0088392 | 2022-12-31 18:07:57 +0800 | [diff] [blame] | 68 | #elif defined(__loongarch__) |
| 69 | #define bpf_target_loongarch |
| 70 | #define bpf_target_defined |
Lorenz Bauer | 4a638d5 | 2021-06-16 09:36:35 +0100 | [diff] [blame] | 71 | #endif /* no compiler target */ |
| 72 | |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 73 | #endif |
Lorenz Bauer | 4a638d5 | 2021-06-16 09:36:35 +0100 | [diff] [blame] | 74 | |
| 75 | #ifndef __BPF_TARGET_MISSING |
| 76 | #define __BPF_TARGET_MISSING "GCC error \"Must specify a BPF target arch via __TARGET_ARCH_xxx\"" |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 77 | #endif |
| 78 | |
| 79 | #if defined(bpf_target_x86) |
| 80 | |
Andrii Nakryiko | 0132903 | 2023-01-20 12:08:51 -0800 | [diff] [blame] | 81 | /* |
| 82 | * https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI |
| 83 | */ |
| 84 | |
Andrii Nakryiko | fd56e00 | 2020-02-29 15:11:10 -0800 | [diff] [blame] | 85 | #if defined(__KERNEL__) || defined(__VMLINUX_H__) |
Andrii Nakryiko | b8ebce8 | 2020-03-13 10:23:35 -0700 | [diff] [blame] | 86 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 87 | #define __PT_PARM1_REG di |
| 88 | #define __PT_PARM2_REG si |
| 89 | #define __PT_PARM3_REG dx |
| 90 | #define __PT_PARM4_REG cx |
| 91 | #define __PT_PARM5_REG r8 |
Andrii Nakryiko | 0132903 | 2023-01-20 12:08:51 -0800 | [diff] [blame] | 92 | #define __PT_PARM6_REG r9 |
Andrii Nakryiko | d21fbce | 2023-01-20 12:09:02 -0800 | [diff] [blame] | 93 | /* |
| 94 | * Syscall uses r10 for PARM4. See arch/x86/entry/entry_64.S:entry_SYSCALL_64 |
| 95 | * comments in Linux sources. And refer to syscall(2) manpage. |
| 96 | */ |
| 97 | #define __PT_PARM1_SYSCALL_REG __PT_PARM1_REG |
| 98 | #define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG |
| 99 | #define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG |
| 100 | #define __PT_PARM4_SYSCALL_REG r10 |
| 101 | #define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG |
| 102 | #define __PT_PARM6_SYSCALL_REG __PT_PARM6_REG |
| 103 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 104 | #define __PT_RET_REG sp |
| 105 | #define __PT_FP_REG bp |
| 106 | #define __PT_RC_REG ax |
| 107 | #define __PT_SP_REG sp |
| 108 | #define __PT_IP_REG ip |
Andrii Nakryiko | b8ebce8 | 2020-03-13 10:23:35 -0700 | [diff] [blame] | 109 | |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 110 | #else |
Andrii Nakryiko | b8ebce8 | 2020-03-13 10:23:35 -0700 | [diff] [blame] | 111 | |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 112 | #ifdef __i386__ |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 113 | |
Andrii Nakryiko | 3c59623 | 2023-01-20 12:08:50 -0800 | [diff] [blame] | 114 | /* i386 kernel is built with -mregparm=3 */ |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 115 | #define __PT_PARM1_REG eax |
| 116 | #define __PT_PARM2_REG edx |
| 117 | #define __PT_PARM3_REG ecx |
Andrii Nakryiko | ff00f9c | 2023-01-20 12:09:03 -0800 | [diff] [blame] | 118 | /* i386 syscall ABI is very different, refer to syscall(2) manpage */ |
| 119 | #define __PT_PARM1_SYSCALL_REG ebx |
| 120 | #define __PT_PARM2_SYSCALL_REG ecx |
| 121 | #define __PT_PARM3_SYSCALL_REG edx |
| 122 | #define __PT_PARM4_SYSCALL_REG esi |
| 123 | #define __PT_PARM5_SYSCALL_REG edi |
| 124 | #define __PT_PARM6_SYSCALL_REG ebp |
| 125 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 126 | #define __PT_RET_REG esp |
| 127 | #define __PT_FP_REG ebp |
| 128 | #define __PT_RC_REG eax |
| 129 | #define __PT_SP_REG esp |
| 130 | #define __PT_IP_REG eip |
Andrii Nakryiko | b8ebce8 | 2020-03-13 10:23:35 -0700 | [diff] [blame] | 131 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 132 | #else /* __i386__ */ |
Andrii Nakryiko | b8ebce8 | 2020-03-13 10:23:35 -0700 | [diff] [blame] | 133 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 134 | #define __PT_PARM1_REG rdi |
| 135 | #define __PT_PARM2_REG rsi |
| 136 | #define __PT_PARM3_REG rdx |
| 137 | #define __PT_PARM4_REG rcx |
| 138 | #define __PT_PARM5_REG r8 |
Andrii Nakryiko | 0132903 | 2023-01-20 12:08:51 -0800 | [diff] [blame] | 139 | #define __PT_PARM6_REG r9 |
Andrii Nakryiko | d21fbce | 2023-01-20 12:09:02 -0800 | [diff] [blame] | 140 | |
| 141 | #define __PT_PARM1_SYSCALL_REG __PT_PARM1_REG |
| 142 | #define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG |
| 143 | #define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG |
| 144 | #define __PT_PARM4_SYSCALL_REG r10 |
| 145 | #define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG |
| 146 | #define __PT_PARM6_SYSCALL_REG __PT_PARM6_REG |
| 147 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 148 | #define __PT_RET_REG rsp |
| 149 | #define __PT_FP_REG rbp |
| 150 | #define __PT_RC_REG rax |
| 151 | #define __PT_SP_REG rsp |
| 152 | #define __PT_IP_REG rip |
Andrii Nakryiko | b8ebce8 | 2020-03-13 10:23:35 -0700 | [diff] [blame] | 153 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 154 | #endif /* __i386__ */ |
Andrii Nakryiko | b8ebce8 | 2020-03-13 10:23:35 -0700 | [diff] [blame] | 155 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 156 | #endif /* __KERNEL__ || __VMLINUX_H__ */ |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 157 | |
| 158 | #elif defined(bpf_target_s390) |
| 159 | |
Andrii Nakryiko | e82b96a | 2023-01-20 12:09:04 -0800 | [diff] [blame] | 160 | /* |
| 161 | * https://github.com/IBM/s390x-abi/releases/download/v1.6/lzsabi_s390x.pdf |
| 162 | */ |
| 163 | |
Ilya Leoshkevich | 1f22a6f | 2022-02-09 03:17:45 +0100 | [diff] [blame] | 164 | struct pt_regs___s390 { |
| 165 | unsigned long orig_gpr2; |
| 166 | }; |
| 167 | |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 168 | /* s390 provides user_pt_regs instead of struct pt_regs to userspace */ |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 169 | #define __PT_REGS_CAST(x) ((const user_pt_regs *)(x)) |
| 170 | #define __PT_PARM1_REG gprs[2] |
| 171 | #define __PT_PARM2_REG gprs[3] |
| 172 | #define __PT_PARM3_REG gprs[4] |
| 173 | #define __PT_PARM4_REG gprs[5] |
| 174 | #define __PT_PARM5_REG gprs[6] |
Andrii Nakryiko | e82b96a | 2023-01-20 12:09:04 -0800 | [diff] [blame] | 175 | |
| 176 | #define __PT_PARM1_SYSCALL_REG orig_gpr2 |
| 177 | #define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG |
| 178 | #define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG |
| 179 | #define __PT_PARM4_SYSCALL_REG __PT_PARM4_REG |
| 180 | #define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG |
| 181 | #define __PT_PARM6_SYSCALL_REG gprs[7] |
| 182 | #define PT_REGS_PARM1_SYSCALL(x) PT_REGS_PARM1_CORE_SYSCALL(x) |
| 183 | #define PT_REGS_PARM1_CORE_SYSCALL(x) \ |
| 184 | BPF_CORE_READ((const struct pt_regs___s390 *)(x), __PT_PARM1_SYSCALL_REG) |
| 185 | |
Daniel T. Lee | 7244eb6 | 2022-12-24 16:15:27 +0900 | [diff] [blame] | 186 | #define __PT_RET_REG gprs[14] |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 187 | #define __PT_FP_REG gprs[11] /* Works only with CONFIG_FRAME_POINTER */ |
| 188 | #define __PT_RC_REG gprs[2] |
| 189 | #define __PT_SP_REG gprs[15] |
| 190 | #define __PT_IP_REG psw.addr |
Andrii Nakryiko | b8ebce8 | 2020-03-13 10:23:35 -0700 | [diff] [blame] | 191 | |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 192 | #elif defined(bpf_target_arm) |
| 193 | |
Andrii Nakryiko | 1dac40a | 2023-01-20 12:08:52 -0800 | [diff] [blame] | 194 | /* |
| 195 | * https://github.com/ARM-software/abi-aa/blob/main/aapcs32/aapcs32.rst#machine-registers |
| 196 | */ |
| 197 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 198 | #define __PT_PARM1_REG uregs[0] |
| 199 | #define __PT_PARM2_REG uregs[1] |
| 200 | #define __PT_PARM3_REG uregs[2] |
| 201 | #define __PT_PARM4_REG uregs[3] |
Andrii Nakryiko | 3a95c42 | 2023-01-20 12:09:05 -0800 | [diff] [blame] | 202 | |
| 203 | #define __PT_PARM1_SYSCALL_REG __PT_PARM1_REG |
| 204 | #define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG |
| 205 | #define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG |
| 206 | #define __PT_PARM4_SYSCALL_REG __PT_PARM4_REG |
Puranjay Mohan | 06943ae | 2023-02-23 09:53:46 +0000 | [diff] [blame] | 207 | #define __PT_PARM5_SYSCALL_REG uregs[4] |
Andrii Nakryiko | 3a95c42 | 2023-01-20 12:09:05 -0800 | [diff] [blame] | 208 | #define __PT_PARM6_SYSCALL_REG uregs[5] |
| 209 | #define __PT_PARM7_SYSCALL_REG uregs[6] |
| 210 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 211 | #define __PT_RET_REG uregs[14] |
| 212 | #define __PT_FP_REG uregs[11] /* Works only with CONFIG_FRAME_POINTER */ |
| 213 | #define __PT_RC_REG uregs[0] |
| 214 | #define __PT_SP_REG uregs[13] |
| 215 | #define __PT_IP_REG uregs[12] |
Andrii Nakryiko | b8ebce8 | 2020-03-13 10:23:35 -0700 | [diff] [blame] | 216 | |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 217 | #elif defined(bpf_target_arm64) |
| 218 | |
Andrii Nakryiko | 1dac40a | 2023-01-20 12:08:52 -0800 | [diff] [blame] | 219 | /* |
| 220 | * https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#machine-registers |
| 221 | */ |
| 222 | |
Ilya Leoshkevich | fbca4a2 | 2022-02-09 03:17:44 +0100 | [diff] [blame] | 223 | struct pt_regs___arm64 { |
| 224 | unsigned long orig_x0; |
| 225 | }; |
| 226 | |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 227 | /* arm64 provides struct user_pt_regs instead of struct pt_regs to userspace */ |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 228 | #define __PT_REGS_CAST(x) ((const struct user_pt_regs *)(x)) |
| 229 | #define __PT_PARM1_REG regs[0] |
| 230 | #define __PT_PARM2_REG regs[1] |
| 231 | #define __PT_PARM3_REG regs[2] |
| 232 | #define __PT_PARM4_REG regs[3] |
| 233 | #define __PT_PARM5_REG regs[4] |
Andrii Nakryiko | 1dac40a | 2023-01-20 12:08:52 -0800 | [diff] [blame] | 234 | #define __PT_PARM6_REG regs[5] |
| 235 | #define __PT_PARM7_REG regs[6] |
| 236 | #define __PT_PARM8_REG regs[7] |
Andrii Nakryiko | 3488ea0 | 2023-01-20 12:09:06 -0800 | [diff] [blame] | 237 | |
| 238 | #define __PT_PARM1_SYSCALL_REG orig_x0 |
| 239 | #define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG |
| 240 | #define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG |
| 241 | #define __PT_PARM4_SYSCALL_REG __PT_PARM4_REG |
| 242 | #define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG |
| 243 | #define __PT_PARM6_SYSCALL_REG __PT_PARM6_REG |
| 244 | #define PT_REGS_PARM1_SYSCALL(x) PT_REGS_PARM1_CORE_SYSCALL(x) |
| 245 | #define PT_REGS_PARM1_CORE_SYSCALL(x) \ |
| 246 | BPF_CORE_READ((const struct pt_regs___arm64 *)(x), __PT_PARM1_SYSCALL_REG) |
| 247 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 248 | #define __PT_RET_REG regs[30] |
| 249 | #define __PT_FP_REG regs[29] /* Works only with CONFIG_FRAME_POINTER */ |
| 250 | #define __PT_RC_REG regs[0] |
| 251 | #define __PT_SP_REG sp |
| 252 | #define __PT_IP_REG pc |
Andrii Nakryiko | b8ebce8 | 2020-03-13 10:23:35 -0700 | [diff] [blame] | 253 | |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 254 | #elif defined(bpf_target_mips) |
| 255 | |
Andrii Nakryiko | 1222445 | 2023-01-20 12:08:53 -0800 | [diff] [blame] | 256 | /* |
| 257 | * N64 ABI is assumed right now. |
| 258 | * https://en.wikipedia.org/wiki/MIPS_architecture#Calling_conventions |
| 259 | */ |
| 260 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 261 | #define __PT_PARM1_REG regs[4] |
| 262 | #define __PT_PARM2_REG regs[5] |
| 263 | #define __PT_PARM3_REG regs[6] |
| 264 | #define __PT_PARM4_REG regs[7] |
| 265 | #define __PT_PARM5_REG regs[8] |
Andrii Nakryiko | 1222445 | 2023-01-20 12:08:53 -0800 | [diff] [blame] | 266 | #define __PT_PARM6_REG regs[9] |
| 267 | #define __PT_PARM7_REG regs[10] |
| 268 | #define __PT_PARM8_REG regs[11] |
Andrii Nakryiko | cfd0bbe | 2023-01-20 12:09:07 -0800 | [diff] [blame] | 269 | |
| 270 | #define __PT_PARM1_SYSCALL_REG __PT_PARM1_REG |
| 271 | #define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG |
| 272 | #define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG |
| 273 | #define __PT_PARM4_SYSCALL_REG __PT_PARM4_REG |
| 274 | #define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG /* only N32/N64 */ |
| 275 | #define __PT_PARM6_SYSCALL_REG __PT_PARM6_REG /* only N32/N64 */ |
| 276 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 277 | #define __PT_RET_REG regs[31] |
| 278 | #define __PT_FP_REG regs[30] /* Works only with CONFIG_FRAME_POINTER */ |
| 279 | #define __PT_RC_REG regs[2] |
| 280 | #define __PT_SP_REG regs[29] |
| 281 | #define __PT_IP_REG cp0_epc |
Andrii Nakryiko | b8ebce8 | 2020-03-13 10:23:35 -0700 | [diff] [blame] | 282 | |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 283 | #elif defined(bpf_target_powerpc) |
| 284 | |
Andrii Nakryiko | 2eb2be3 | 2023-01-20 12:08:54 -0800 | [diff] [blame] | 285 | /* |
| 286 | * http://refspecs.linux-foundation.org/elf/elfspec_ppc.pdf (page 3-14, |
| 287 | * section "Function Calling Sequence") |
| 288 | */ |
| 289 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 290 | #define __PT_PARM1_REG gpr[3] |
| 291 | #define __PT_PARM2_REG gpr[4] |
| 292 | #define __PT_PARM3_REG gpr[5] |
| 293 | #define __PT_PARM4_REG gpr[6] |
| 294 | #define __PT_PARM5_REG gpr[7] |
Andrii Nakryiko | 2eb2be3 | 2023-01-20 12:08:54 -0800 | [diff] [blame] | 295 | #define __PT_PARM6_REG gpr[8] |
| 296 | #define __PT_PARM7_REG gpr[9] |
| 297 | #define __PT_PARM8_REG gpr[10] |
Andrii Nakryiko | c1cc01a | 2023-01-20 12:09:08 -0800 | [diff] [blame] | 298 | |
| 299 | /* powerpc does not select ARCH_HAS_SYSCALL_WRAPPER. */ |
| 300 | #define PT_REGS_SYSCALL_REGS(ctx) ctx |
| 301 | #define __PT_PARM1_SYSCALL_REG orig_gpr3 |
| 302 | #define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG |
| 303 | #define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG |
| 304 | #define __PT_PARM4_SYSCALL_REG __PT_PARM4_REG |
| 305 | #define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG |
| 306 | #define __PT_PARM6_SYSCALL_REG __PT_PARM6_REG |
| 307 | #if !defined(__arch64__) |
| 308 | #define __PT_PARM7_SYSCALL_REG __PT_PARM7_REG /* only powerpc (not powerpc64) */ |
| 309 | #endif |
| 310 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 311 | #define __PT_RET_REG regs[31] |
| 312 | #define __PT_FP_REG __unsupported__ |
| 313 | #define __PT_RC_REG gpr[3] |
| 314 | #define __PT_SP_REG sp |
| 315 | #define __PT_IP_REG nip |
Andrii Nakryiko | b8ebce8 | 2020-03-13 10:23:35 -0700 | [diff] [blame] | 316 | |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 317 | #elif defined(bpf_target_sparc) |
| 318 | |
Andrii Nakryiko | 7f60f5d | 2023-01-20 12:08:55 -0800 | [diff] [blame] | 319 | /* |
| 320 | * https://en.wikipedia.org/wiki/Calling_convention#SPARC |
| 321 | */ |
| 322 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 323 | #define __PT_PARM1_REG u_regs[UREG_I0] |
| 324 | #define __PT_PARM2_REG u_regs[UREG_I1] |
| 325 | #define __PT_PARM3_REG u_regs[UREG_I2] |
| 326 | #define __PT_PARM4_REG u_regs[UREG_I3] |
| 327 | #define __PT_PARM5_REG u_regs[UREG_I4] |
Andrii Nakryiko | 7f60f5d | 2023-01-20 12:08:55 -0800 | [diff] [blame] | 328 | #define __PT_PARM6_REG u_regs[UREG_I5] |
Andrii Nakryiko | 377c15b | 2023-01-20 12:09:09 -0800 | [diff] [blame] | 329 | |
| 330 | #define __PT_PARM1_SYSCALL_REG __PT_PARM1_REG |
| 331 | #define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG |
| 332 | #define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG |
| 333 | #define __PT_PARM4_SYSCALL_REG __PT_PARM4_REG |
| 334 | #define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG |
| 335 | #define __PT_PARM6_SYSCALL_REG __PT_PARM6_REG |
| 336 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 337 | #define __PT_RET_REG u_regs[UREG_I7] |
| 338 | #define __PT_FP_REG __unsupported__ |
| 339 | #define __PT_RC_REG u_regs[UREG_I0] |
| 340 | #define __PT_SP_REG u_regs[UREG_FP] |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 341 | /* Should this also be a bpf_target check for the sparc case? */ |
| 342 | #if defined(__arch64__) |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 343 | #define __PT_IP_REG tpc |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 344 | #else |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 345 | #define __PT_IP_REG pc |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 346 | #endif |
| 347 | |
Björn Töpel | 589fed4 | 2021-10-28 18:10:56 +0200 | [diff] [blame] | 348 | #elif defined(bpf_target_riscv) |
| 349 | |
Andrii Nakryiko | b13ed8c | 2023-01-20 12:08:56 -0800 | [diff] [blame] | 350 | /* |
| 351 | * https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc#risc-v-calling-conventions |
| 352 | */ |
| 353 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 354 | #define __PT_REGS_CAST(x) ((const struct user_regs_struct *)(x)) |
| 355 | #define __PT_PARM1_REG a0 |
| 356 | #define __PT_PARM2_REG a1 |
| 357 | #define __PT_PARM3_REG a2 |
| 358 | #define __PT_PARM4_REG a3 |
| 359 | #define __PT_PARM5_REG a4 |
Andrii Nakryiko | b13ed8c | 2023-01-20 12:08:56 -0800 | [diff] [blame] | 360 | #define __PT_PARM6_REG a5 |
| 361 | #define __PT_PARM7_REG a6 |
| 362 | #define __PT_PARM8_REG a7 |
Andrii Nakryiko | a042621 | 2023-01-20 12:09:10 -0800 | [diff] [blame] | 363 | |
| 364 | /* riscv does not select ARCH_HAS_SYSCALL_WRAPPER. */ |
| 365 | #define PT_REGS_SYSCALL_REGS(ctx) ctx |
| 366 | #define __PT_PARM1_SYSCALL_REG __PT_PARM1_REG |
| 367 | #define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG |
| 368 | #define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG |
| 369 | #define __PT_PARM4_SYSCALL_REG __PT_PARM4_REG |
| 370 | #define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG |
| 371 | #define __PT_PARM6_SYSCALL_REG __PT_PARM6_REG |
| 372 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 373 | #define __PT_RET_REG ra |
Ilya Leoshkevich | 5c10115 | 2022-02-09 03:17:40 +0100 | [diff] [blame] | 374 | #define __PT_FP_REG s0 |
Yixun Lan | 935dc35 | 2022-07-06 22:02:04 +0800 | [diff] [blame] | 375 | #define __PT_RC_REG a0 |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 376 | #define __PT_SP_REG sp |
Ilya Leoshkevich | 5c10115 | 2022-02-09 03:17:40 +0100 | [diff] [blame] | 377 | #define __PT_IP_REG pc |
Björn Töpel | 589fed4 | 2021-10-28 18:10:56 +0200 | [diff] [blame] | 378 | |
Vladimir Isaev | 0738599 | 2022-04-09 01:44:42 +0300 | [diff] [blame] | 379 | #elif defined(bpf_target_arc) |
| 380 | |
Andrii Nakryiko | 0ac08656 | 2023-01-20 12:08:57 -0800 | [diff] [blame] | 381 | /* |
| 382 | * Section "Function Calling Sequence" (page 24): |
| 383 | * https://raw.githubusercontent.com/wiki/foss-for-synopsys-dwc-arc-processors/toolchain/files/ARCv2_ABI.pdf |
| 384 | */ |
| 385 | |
Vladimir Isaev | 0738599 | 2022-04-09 01:44:42 +0300 | [diff] [blame] | 386 | /* arc provides struct user_pt_regs instead of struct pt_regs to userspace */ |
| 387 | #define __PT_REGS_CAST(x) ((const struct user_regs_struct *)(x)) |
| 388 | #define __PT_PARM1_REG scratch.r0 |
| 389 | #define __PT_PARM2_REG scratch.r1 |
| 390 | #define __PT_PARM3_REG scratch.r2 |
| 391 | #define __PT_PARM4_REG scratch.r3 |
| 392 | #define __PT_PARM5_REG scratch.r4 |
Andrii Nakryiko | 0ac08656 | 2023-01-20 12:08:57 -0800 | [diff] [blame] | 393 | #define __PT_PARM6_REG scratch.r5 |
| 394 | #define __PT_PARM7_REG scratch.r6 |
| 395 | #define __PT_PARM8_REG scratch.r7 |
Andrii Nakryiko | 2cf8027 | 2023-01-20 12:09:11 -0800 | [diff] [blame] | 396 | |
| 397 | /* arc does not select ARCH_HAS_SYSCALL_WRAPPER. */ |
| 398 | #define PT_REGS_SYSCALL_REGS(ctx) ctx |
| 399 | #define __PT_PARM1_SYSCALL_REG __PT_PARM1_REG |
| 400 | #define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG |
| 401 | #define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG |
| 402 | #define __PT_PARM4_SYSCALL_REG __PT_PARM4_REG |
| 403 | #define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG |
| 404 | #define __PT_PARM6_SYSCALL_REG __PT_PARM6_REG |
| 405 | |
Vladimir Isaev | 0738599 | 2022-04-09 01:44:42 +0300 | [diff] [blame] | 406 | #define __PT_RET_REG scratch.blink |
Andrii Nakryiko | 0ac08656 | 2023-01-20 12:08:57 -0800 | [diff] [blame] | 407 | #define __PT_FP_REG scratch.fp |
Vladimir Isaev | 0738599 | 2022-04-09 01:44:42 +0300 | [diff] [blame] | 408 | #define __PT_RC_REG scratch.r0 |
| 409 | #define __PT_SP_REG scratch.sp |
| 410 | #define __PT_IP_REG scratch.ret |
Vladimir Isaev | 0738599 | 2022-04-09 01:44:42 +0300 | [diff] [blame] | 411 | |
Hengqi Chen | 0088392 | 2022-12-31 18:07:57 +0800 | [diff] [blame] | 412 | #elif defined(bpf_target_loongarch) |
| 413 | |
Andrii Nakryiko | 55ff00d | 2023-01-20 12:08:58 -0800 | [diff] [blame] | 414 | /* |
| 415 | * https://docs.kernel.org/loongarch/introduction.html |
| 416 | * https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html |
| 417 | */ |
Hengqi Chen | 0088392 | 2022-12-31 18:07:57 +0800 | [diff] [blame] | 418 | |
Tiezhu Yang | 29c66ad | 2023-02-24 18:36:53 +0800 | [diff] [blame] | 419 | /* loongarch provides struct user_pt_regs instead of struct pt_regs to userspace */ |
| 420 | #define __PT_REGS_CAST(x) ((const struct user_pt_regs *)(x)) |
Hengqi Chen | 0088392 | 2022-12-31 18:07:57 +0800 | [diff] [blame] | 421 | #define __PT_PARM1_REG regs[4] |
| 422 | #define __PT_PARM2_REG regs[5] |
| 423 | #define __PT_PARM3_REG regs[6] |
| 424 | #define __PT_PARM4_REG regs[7] |
| 425 | #define __PT_PARM5_REG regs[8] |
Andrii Nakryiko | 55ff00d | 2023-01-20 12:08:58 -0800 | [diff] [blame] | 426 | #define __PT_PARM6_REG regs[9] |
| 427 | #define __PT_PARM7_REG regs[10] |
| 428 | #define __PT_PARM8_REG regs[11] |
Andrii Nakryiko | 12a299f | 2023-01-20 12:09:12 -0800 | [diff] [blame] | 429 | |
| 430 | /* loongarch does not select ARCH_HAS_SYSCALL_WRAPPER. */ |
| 431 | #define PT_REGS_SYSCALL_REGS(ctx) ctx |
| 432 | #define __PT_PARM1_SYSCALL_REG __PT_PARM1_REG |
| 433 | #define __PT_PARM2_SYSCALL_REG __PT_PARM2_REG |
| 434 | #define __PT_PARM3_SYSCALL_REG __PT_PARM3_REG |
| 435 | #define __PT_PARM4_SYSCALL_REG __PT_PARM4_REG |
| 436 | #define __PT_PARM5_SYSCALL_REG __PT_PARM5_REG |
| 437 | #define __PT_PARM6_SYSCALL_REG __PT_PARM6_REG |
| 438 | |
Hengqi Chen | 0088392 | 2022-12-31 18:07:57 +0800 | [diff] [blame] | 439 | #define __PT_RET_REG regs[1] |
| 440 | #define __PT_FP_REG regs[22] |
| 441 | #define __PT_RC_REG regs[4] |
| 442 | #define __PT_SP_REG regs[3] |
| 443 | #define __PT_IP_REG csr_era |
Hengqi Chen | 0088392 | 2022-12-31 18:07:57 +0800 | [diff] [blame] | 444 | |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 445 | #endif |
| 446 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 447 | #if defined(bpf_target_defined) |
| 448 | |
| 449 | struct pt_regs; |
| 450 | |
Andrii Nakryiko | 3c59623 | 2023-01-20 12:08:50 -0800 | [diff] [blame] | 451 | /* allow some architectures to override `struct pt_regs` */ |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 452 | #ifndef __PT_REGS_CAST |
| 453 | #define __PT_REGS_CAST(x) (x) |
| 454 | #endif |
| 455 | |
Andrii Nakryiko | 3c59623 | 2023-01-20 12:08:50 -0800 | [diff] [blame] | 456 | /* |
| 457 | * Different architectures support different number of arguments passed |
| 458 | * through registers. i386 supports just 3, some arches support up to 8. |
| 459 | */ |
| 460 | #ifndef __PT_PARM4_REG |
| 461 | #define __PT_PARM4_REG __unsupported__ |
| 462 | #endif |
| 463 | #ifndef __PT_PARM5_REG |
| 464 | #define __PT_PARM5_REG __unsupported__ |
| 465 | #endif |
| 466 | #ifndef __PT_PARM6_REG |
| 467 | #define __PT_PARM6_REG __unsupported__ |
| 468 | #endif |
| 469 | #ifndef __PT_PARM7_REG |
| 470 | #define __PT_PARM7_REG __unsupported__ |
| 471 | #endif |
| 472 | #ifndef __PT_PARM8_REG |
| 473 | #define __PT_PARM8_REG __unsupported__ |
| 474 | #endif |
Andrii Nakryiko | 8ccabee | 2023-01-20 12:09:01 -0800 | [diff] [blame] | 475 | /* |
| 476 | * Similarly, syscall-specific conventions might differ between function call |
| 477 | * conventions within each architecutre. All supported architectures pass |
| 478 | * either 6 or 7 syscall arguments in registers. |
| 479 | * |
| 480 | * See syscall(2) manpage for succinct table with information on each arch. |
| 481 | */ |
Andrii Nakryiko | 8ccabee | 2023-01-20 12:09:01 -0800 | [diff] [blame] | 482 | #ifndef __PT_PARM7_SYSCALL_REG |
| 483 | #define __PT_PARM7_SYSCALL_REG __unsupported__ |
| 484 | #endif |
Andrii Nakryiko | 3c59623 | 2023-01-20 12:08:50 -0800 | [diff] [blame] | 485 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 486 | #define PT_REGS_PARM1(x) (__PT_REGS_CAST(x)->__PT_PARM1_REG) |
| 487 | #define PT_REGS_PARM2(x) (__PT_REGS_CAST(x)->__PT_PARM2_REG) |
| 488 | #define PT_REGS_PARM3(x) (__PT_REGS_CAST(x)->__PT_PARM3_REG) |
| 489 | #define PT_REGS_PARM4(x) (__PT_REGS_CAST(x)->__PT_PARM4_REG) |
| 490 | #define PT_REGS_PARM5(x) (__PT_REGS_CAST(x)->__PT_PARM5_REG) |
Andrii Nakryiko | 3c59623 | 2023-01-20 12:08:50 -0800 | [diff] [blame] | 491 | #define PT_REGS_PARM6(x) (__PT_REGS_CAST(x)->__PT_PARM6_REG) |
| 492 | #define PT_REGS_PARM7(x) (__PT_REGS_CAST(x)->__PT_PARM7_REG) |
| 493 | #define PT_REGS_PARM8(x) (__PT_REGS_CAST(x)->__PT_PARM8_REG) |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 494 | #define PT_REGS_RET(x) (__PT_REGS_CAST(x)->__PT_RET_REG) |
| 495 | #define PT_REGS_FP(x) (__PT_REGS_CAST(x)->__PT_FP_REG) |
| 496 | #define PT_REGS_RC(x) (__PT_REGS_CAST(x)->__PT_RC_REG) |
| 497 | #define PT_REGS_SP(x) (__PT_REGS_CAST(x)->__PT_SP_REG) |
| 498 | #define PT_REGS_IP(x) (__PT_REGS_CAST(x)->__PT_IP_REG) |
| 499 | |
| 500 | #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM1_REG) |
| 501 | #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM2_REG) |
| 502 | #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM3_REG) |
| 503 | #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM4_REG) |
| 504 | #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM5_REG) |
Andrii Nakryiko | 3c59623 | 2023-01-20 12:08:50 -0800 | [diff] [blame] | 505 | #define PT_REGS_PARM6_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM6_REG) |
| 506 | #define PT_REGS_PARM7_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM7_REG) |
| 507 | #define PT_REGS_PARM8_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM8_REG) |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 508 | #define PT_REGS_RET_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_RET_REG) |
| 509 | #define PT_REGS_FP_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_FP_REG) |
| 510 | #define PT_REGS_RC_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_RC_REG) |
| 511 | #define PT_REGS_SP_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_SP_REG) |
| 512 | #define PT_REGS_IP_CORE(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_IP_REG) |
| 513 | |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 514 | #if defined(bpf_target_powerpc) |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 515 | |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 516 | #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; }) |
| 517 | #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 518 | |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 519 | #elif defined(bpf_target_sparc) |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 520 | |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 521 | #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = PT_REGS_RET(ctx); }) |
| 522 | #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 523 | |
| 524 | #else |
| 525 | |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 526 | #define BPF_KPROBE_READ_RET_IP(ip, ctx) \ |
Andrii Nakryiko | 70785cf | 2020-08-18 14:33:55 -0700 | [diff] [blame] | 527 | ({ bpf_probe_read_kernel(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); }) |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 528 | #define BPF_KRETPROBE_READ_RET_IP(ip, ctx) \ |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 529 | ({ bpf_probe_read_kernel(&(ip), sizeof(ip), (void *)(PT_REGS_FP(ctx) + sizeof(ip))); }) |
| 530 | |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 531 | #endif |
| 532 | |
Ilya Leoshkevich | 60d16c5 | 2022-02-09 03:17:43 +0100 | [diff] [blame] | 533 | #ifndef PT_REGS_PARM1_SYSCALL |
Andrii Nakryiko | 8ccabee | 2023-01-20 12:09:01 -0800 | [diff] [blame] | 534 | #define PT_REGS_PARM1_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM1_SYSCALL_REG) |
| 535 | #define PT_REGS_PARM1_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM1_SYSCALL_REG) |
Ilya Leoshkevich | 60d16c5 | 2022-02-09 03:17:43 +0100 | [diff] [blame] | 536 | #endif |
Andrii Nakryiko | 8ccabee | 2023-01-20 12:09:01 -0800 | [diff] [blame] | 537 | #ifndef PT_REGS_PARM2_SYSCALL |
| 538 | #define PT_REGS_PARM2_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM2_SYSCALL_REG) |
| 539 | #define PT_REGS_PARM2_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM2_SYSCALL_REG) |
| 540 | #endif |
| 541 | #ifndef PT_REGS_PARM3_SYSCALL |
| 542 | #define PT_REGS_PARM3_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM3_SYSCALL_REG) |
| 543 | #define PT_REGS_PARM3_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM3_SYSCALL_REG) |
| 544 | #endif |
Ilya Leoshkevich | 60d16c5 | 2022-02-09 03:17:43 +0100 | [diff] [blame] | 545 | #ifndef PT_REGS_PARM4_SYSCALL |
Andrii Nakryiko | 8ccabee | 2023-01-20 12:09:01 -0800 | [diff] [blame] | 546 | #define PT_REGS_PARM4_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM4_SYSCALL_REG) |
| 547 | #define PT_REGS_PARM4_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM4_SYSCALL_REG) |
Kenta Tada | d084df3 | 2022-01-24 23:16:21 +0900 | [diff] [blame] | 548 | #endif |
Andrii Nakryiko | 8ccabee | 2023-01-20 12:09:01 -0800 | [diff] [blame] | 549 | #ifndef PT_REGS_PARM5_SYSCALL |
| 550 | #define PT_REGS_PARM5_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM5_SYSCALL_REG) |
| 551 | #define PT_REGS_PARM5_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM5_SYSCALL_REG) |
Ilya Leoshkevich | 60d16c5 | 2022-02-09 03:17:43 +0100 | [diff] [blame] | 552 | #endif |
Andrii Nakryiko | 8ccabee | 2023-01-20 12:09:01 -0800 | [diff] [blame] | 553 | #ifndef PT_REGS_PARM6_SYSCALL |
| 554 | #define PT_REGS_PARM6_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM6_SYSCALL_REG) |
| 555 | #define PT_REGS_PARM6_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM6_SYSCALL_REG) |
Kenta Tada | d084df3 | 2022-01-24 23:16:21 +0900 | [diff] [blame] | 556 | #endif |
Andrii Nakryiko | 8ccabee | 2023-01-20 12:09:01 -0800 | [diff] [blame] | 557 | #ifndef PT_REGS_PARM7_SYSCALL |
| 558 | #define PT_REGS_PARM7_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM7_SYSCALL_REG) |
| 559 | #define PT_REGS_PARM7_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM7_SYSCALL_REG) |
| 560 | #endif |
Kenta Tada | d084df3 | 2022-01-24 23:16:21 +0900 | [diff] [blame] | 561 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 562 | #else /* defined(bpf_target_defined) */ |
Lorenz Bauer | 4a638d5 | 2021-06-16 09:36:35 +0100 | [diff] [blame] | 563 | |
| 564 | #define PT_REGS_PARM1(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 565 | #define PT_REGS_PARM2(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 566 | #define PT_REGS_PARM3(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 567 | #define PT_REGS_PARM4(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 568 | #define PT_REGS_PARM5(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
Andrii Nakryiko | 3c59623 | 2023-01-20 12:08:50 -0800 | [diff] [blame] | 569 | #define PT_REGS_PARM6(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 570 | #define PT_REGS_PARM7(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 571 | #define PT_REGS_PARM8(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
Lorenz Bauer | 4a638d5 | 2021-06-16 09:36:35 +0100 | [diff] [blame] | 572 | #define PT_REGS_RET(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 573 | #define PT_REGS_FP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 574 | #define PT_REGS_RC(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 575 | #define PT_REGS_SP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 576 | #define PT_REGS_IP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 577 | |
| 578 | #define PT_REGS_PARM1_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 579 | #define PT_REGS_PARM2_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 580 | #define PT_REGS_PARM3_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 581 | #define PT_REGS_PARM4_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 582 | #define PT_REGS_PARM5_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
Andrii Nakryiko | 3c59623 | 2023-01-20 12:08:50 -0800 | [diff] [blame] | 583 | #define PT_REGS_PARM6_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 584 | #define PT_REGS_PARM7_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 585 | #define PT_REGS_PARM8_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
Lorenz Bauer | 4a638d5 | 2021-06-16 09:36:35 +0100 | [diff] [blame] | 586 | #define PT_REGS_RET_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 587 | #define PT_REGS_FP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 588 | #define PT_REGS_RC_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 589 | #define PT_REGS_SP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 590 | #define PT_REGS_IP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 591 | |
| 592 | #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 593 | #define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 594 | |
Kenta Tada | d084df3 | 2022-01-24 23:16:21 +0900 | [diff] [blame] | 595 | #define PT_REGS_PARM1_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 596 | #define PT_REGS_PARM2_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 597 | #define PT_REGS_PARM3_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 598 | #define PT_REGS_PARM4_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 599 | #define PT_REGS_PARM5_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
Andrii Nakryiko | 8ccabee | 2023-01-20 12:09:01 -0800 | [diff] [blame] | 600 | #define PT_REGS_PARM6_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 601 | #define PT_REGS_PARM7_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
Kenta Tada | d084df3 | 2022-01-24 23:16:21 +0900 | [diff] [blame] | 602 | |
| 603 | #define PT_REGS_PARM1_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 604 | #define PT_REGS_PARM2_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 605 | #define PT_REGS_PARM3_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 606 | #define PT_REGS_PARM4_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 607 | #define PT_REGS_PARM5_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
Andrii Nakryiko | 8ccabee | 2023-01-20 12:09:01 -0800 | [diff] [blame] | 608 | #define PT_REGS_PARM6_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
| 609 | #define PT_REGS_PARM7_CORE_SYSCALL(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) |
Kenta Tada | d084df3 | 2022-01-24 23:16:21 +0900 | [diff] [blame] | 610 | |
Andrii Nakryiko | 3cc31d7 | 2021-12-22 13:39:23 -0800 | [diff] [blame] | 611 | #endif /* defined(bpf_target_defined) */ |
Lorenz Bauer | 4a638d5 | 2021-06-16 09:36:35 +0100 | [diff] [blame] | 612 | |
Ilya Leoshkevich | c5a1ffa | 2022-02-09 03:17:37 +0100 | [diff] [blame] | 613 | /* |
| 614 | * When invoked from a syscall handler kprobe, returns a pointer to a |
| 615 | * struct pt_regs containing syscall arguments and suitable for passing to |
| 616 | * PT_REGS_PARMn_SYSCALL() and PT_REGS_PARMn_CORE_SYSCALL(). |
| 617 | */ |
| 618 | #ifndef PT_REGS_SYSCALL_REGS |
| 619 | /* By default, assume that the arch selects ARCH_HAS_SYSCALL_WRAPPER. */ |
| 620 | #define PT_REGS_SYSCALL_REGS(ctx) ((struct pt_regs *)PT_REGS_PARM1(ctx)) |
| 621 | #endif |
| 622 | |
Florent Revest | d6a6a55 | 2021-05-26 18:46:43 +0200 | [diff] [blame] | 623 | #ifndef ___bpf_concat |
Andrii Nakryiko | df8ff35 | 2020-02-29 15:11:12 -0800 | [diff] [blame] | 624 | #define ___bpf_concat(a, b) a ## b |
Florent Revest | d6a6a55 | 2021-05-26 18:46:43 +0200 | [diff] [blame] | 625 | #endif |
| 626 | #ifndef ___bpf_apply |
Andrii Nakryiko | df8ff35 | 2020-02-29 15:11:12 -0800 | [diff] [blame] | 627 | #define ___bpf_apply(fn, n) ___bpf_concat(fn, n) |
Florent Revest | d6a6a55 | 2021-05-26 18:46:43 +0200 | [diff] [blame] | 628 | #endif |
| 629 | #ifndef ___bpf_nth |
Andrii Nakryiko | df8ff35 | 2020-02-29 15:11:12 -0800 | [diff] [blame] | 630 | #define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N |
Florent Revest | d6a6a55 | 2021-05-26 18:46:43 +0200 | [diff] [blame] | 631 | #endif |
| 632 | #ifndef ___bpf_narg |
Andrii Nakryiko | f60edf5 | 2021-12-22 13:39:24 -0800 | [diff] [blame] | 633 | #define ___bpf_narg(...) ___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) |
Florent Revest | d6a6a55 | 2021-05-26 18:46:43 +0200 | [diff] [blame] | 634 | #endif |
Andrii Nakryiko | df8ff35 | 2020-02-29 15:11:12 -0800 | [diff] [blame] | 635 | |
Andrii Nakryiko | f60edf5 | 2021-12-22 13:39:24 -0800 | [diff] [blame] | 636 | #define ___bpf_ctx_cast0() ctx |
| 637 | #define ___bpf_ctx_cast1(x) ___bpf_ctx_cast0(), (void *)ctx[0] |
| 638 | #define ___bpf_ctx_cast2(x, args...) ___bpf_ctx_cast1(args), (void *)ctx[1] |
| 639 | #define ___bpf_ctx_cast3(x, args...) ___bpf_ctx_cast2(args), (void *)ctx[2] |
| 640 | #define ___bpf_ctx_cast4(x, args...) ___bpf_ctx_cast3(args), (void *)ctx[3] |
| 641 | #define ___bpf_ctx_cast5(x, args...) ___bpf_ctx_cast4(args), (void *)ctx[4] |
| 642 | #define ___bpf_ctx_cast6(x, args...) ___bpf_ctx_cast5(args), (void *)ctx[5] |
| 643 | #define ___bpf_ctx_cast7(x, args...) ___bpf_ctx_cast6(args), (void *)ctx[6] |
| 644 | #define ___bpf_ctx_cast8(x, args...) ___bpf_ctx_cast7(args), (void *)ctx[7] |
| 645 | #define ___bpf_ctx_cast9(x, args...) ___bpf_ctx_cast8(args), (void *)ctx[8] |
Andrii Nakryiko | df8ff35 | 2020-02-29 15:11:12 -0800 | [diff] [blame] | 646 | #define ___bpf_ctx_cast10(x, args...) ___bpf_ctx_cast9(args), (void *)ctx[9] |
| 647 | #define ___bpf_ctx_cast11(x, args...) ___bpf_ctx_cast10(args), (void *)ctx[10] |
| 648 | #define ___bpf_ctx_cast12(x, args...) ___bpf_ctx_cast11(args), (void *)ctx[11] |
Andrii Nakryiko | f60edf5 | 2021-12-22 13:39:24 -0800 | [diff] [blame] | 649 | #define ___bpf_ctx_cast(args...) ___bpf_apply(___bpf_ctx_cast, ___bpf_narg(args))(args) |
Andrii Nakryiko | df8ff35 | 2020-02-29 15:11:12 -0800 | [diff] [blame] | 650 | |
| 651 | /* |
| 652 | * BPF_PROG is a convenience wrapper for generic tp_btf/fentry/fexit and |
| 653 | * similar kinds of BPF programs, that accept input arguments as a single |
| 654 | * pointer to untyped u64 array, where each u64 can actually be a typed |
| 655 | * pointer or integer of different size. Instead of requring user to write |
| 656 | * manual casts and work with array elements by index, BPF_PROG macro |
| 657 | * allows user to declare a list of named and typed input arguments in the |
| 658 | * same syntax as for normal C function. All the casting is hidden and |
| 659 | * performed transparently, while user code can just assume working with |
| 660 | * function arguments of specified type and name. |
| 661 | * |
| 662 | * Original raw context argument is preserved as well as 'ctx' argument. |
| 663 | * This is useful when using BPF helpers that expect original context |
| 664 | * as one of the parameters (e.g., for bpf_perf_event_output()). |
| 665 | */ |
| 666 | #define BPF_PROG(name, args...) \ |
| 667 | name(unsigned long long *ctx); \ |
James Hilliard | d25f40f | 2022-08-03 09:14:03 -0600 | [diff] [blame] | 668 | static __always_inline typeof(name(0)) \ |
Andrii Nakryiko | df8ff35 | 2020-02-29 15:11:12 -0800 | [diff] [blame] | 669 | ____##name(unsigned long long *ctx, ##args); \ |
| 670 | typeof(name(0)) name(unsigned long long *ctx) \ |
| 671 | { \ |
| 672 | _Pragma("GCC diagnostic push") \ |
| 673 | _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ |
| 674 | return ____##name(___bpf_ctx_cast(args)); \ |
| 675 | _Pragma("GCC diagnostic pop") \ |
| 676 | } \ |
James Hilliard | d25f40f | 2022-08-03 09:14:03 -0600 | [diff] [blame] | 677 | static __always_inline typeof(name(0)) \ |
Andrii Nakryiko | df8ff35 | 2020-02-29 15:11:12 -0800 | [diff] [blame] | 678 | ____##name(unsigned long long *ctx, ##args) |
| 679 | |
Yonghong Song | 9f2f5d7 | 2022-09-09 19:52:14 -0700 | [diff] [blame] | 680 | #ifndef ___bpf_nth2 |
| 681 | #define ___bpf_nth2(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, \ |
| 682 | _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, N, ...) N |
Yonghong Song | 34586d2 | 2022-08-31 08:27:07 -0700 | [diff] [blame] | 683 | #endif |
Yonghong Song | 9f2f5d7 | 2022-09-09 19:52:14 -0700 | [diff] [blame] | 684 | #ifndef ___bpf_narg2 |
| 685 | #define ___bpf_narg2(...) \ |
| 686 | ___bpf_nth2(_, ##__VA_ARGS__, 12, 12, 11, 11, 10, 10, 9, 9, 8, 8, 7, 7, \ |
| 687 | 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0) |
Yonghong Song | 34586d2 | 2022-08-31 08:27:07 -0700 | [diff] [blame] | 688 | #endif |
| 689 | |
Yonghong Song | 9f2f5d7 | 2022-09-09 19:52:14 -0700 | [diff] [blame] | 690 | #define ___bpf_treg_cnt(t) \ |
| 691 | __builtin_choose_expr(sizeof(t) == 1, 1, \ |
| 692 | __builtin_choose_expr(sizeof(t) == 2, 1, \ |
| 693 | __builtin_choose_expr(sizeof(t) == 4, 1, \ |
| 694 | __builtin_choose_expr(sizeof(t) == 8, 1, \ |
| 695 | __builtin_choose_expr(sizeof(t) == 16, 2, \ |
Yonghong Song | 34586d2 | 2022-08-31 08:27:07 -0700 | [diff] [blame] | 696 | (void)0))))) |
| 697 | |
Yonghong Song | 9f2f5d7 | 2022-09-09 19:52:14 -0700 | [diff] [blame] | 698 | #define ___bpf_reg_cnt0() (0) |
| 699 | #define ___bpf_reg_cnt1(t, x) (___bpf_reg_cnt0() + ___bpf_treg_cnt(t)) |
| 700 | #define ___bpf_reg_cnt2(t, x, args...) (___bpf_reg_cnt1(args) + ___bpf_treg_cnt(t)) |
| 701 | #define ___bpf_reg_cnt3(t, x, args...) (___bpf_reg_cnt2(args) + ___bpf_treg_cnt(t)) |
| 702 | #define ___bpf_reg_cnt4(t, x, args...) (___bpf_reg_cnt3(args) + ___bpf_treg_cnt(t)) |
| 703 | #define ___bpf_reg_cnt5(t, x, args...) (___bpf_reg_cnt4(args) + ___bpf_treg_cnt(t)) |
| 704 | #define ___bpf_reg_cnt6(t, x, args...) (___bpf_reg_cnt5(args) + ___bpf_treg_cnt(t)) |
| 705 | #define ___bpf_reg_cnt7(t, x, args...) (___bpf_reg_cnt6(args) + ___bpf_treg_cnt(t)) |
| 706 | #define ___bpf_reg_cnt8(t, x, args...) (___bpf_reg_cnt7(args) + ___bpf_treg_cnt(t)) |
| 707 | #define ___bpf_reg_cnt9(t, x, args...) (___bpf_reg_cnt8(args) + ___bpf_treg_cnt(t)) |
| 708 | #define ___bpf_reg_cnt10(t, x, args...) (___bpf_reg_cnt9(args) + ___bpf_treg_cnt(t)) |
| 709 | #define ___bpf_reg_cnt11(t, x, args...) (___bpf_reg_cnt10(args) + ___bpf_treg_cnt(t)) |
| 710 | #define ___bpf_reg_cnt12(t, x, args...) (___bpf_reg_cnt11(args) + ___bpf_treg_cnt(t)) |
| 711 | #define ___bpf_reg_cnt(args...) ___bpf_apply(___bpf_reg_cnt, ___bpf_narg2(args))(args) |
Yonghong Song | 34586d2 | 2022-08-31 08:27:07 -0700 | [diff] [blame] | 712 | |
Yonghong Song | 9f2f5d7 | 2022-09-09 19:52:14 -0700 | [diff] [blame] | 713 | #define ___bpf_union_arg(t, x, n) \ |
| 714 | __builtin_choose_expr(sizeof(t) == 1, ({ union { __u8 z[1]; t x; } ___t = { .z = {ctx[n]}}; ___t.x; }), \ |
| 715 | __builtin_choose_expr(sizeof(t) == 2, ({ union { __u16 z[1]; t x; } ___t = { .z = {ctx[n]} }; ___t.x; }), \ |
| 716 | __builtin_choose_expr(sizeof(t) == 4, ({ union { __u32 z[1]; t x; } ___t = { .z = {ctx[n]} }; ___t.x; }), \ |
| 717 | __builtin_choose_expr(sizeof(t) == 8, ({ union { __u64 z[1]; t x; } ___t = {.z = {ctx[n]} }; ___t.x; }), \ |
| 718 | __builtin_choose_expr(sizeof(t) == 16, ({ union { __u64 z[2]; t x; } ___t = {.z = {ctx[n], ctx[n + 1]} }; ___t.x; }), \ |
| 719 | (void)0))))) |
| 720 | |
| 721 | #define ___bpf_ctx_arg0(n, args...) |
| 722 | #define ___bpf_ctx_arg1(n, t, x) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt1(t, x)) |
| 723 | #define ___bpf_ctx_arg2(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt2(t, x, args)) ___bpf_ctx_arg1(n, args) |
| 724 | #define ___bpf_ctx_arg3(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt3(t, x, args)) ___bpf_ctx_arg2(n, args) |
| 725 | #define ___bpf_ctx_arg4(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt4(t, x, args)) ___bpf_ctx_arg3(n, args) |
| 726 | #define ___bpf_ctx_arg5(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt5(t, x, args)) ___bpf_ctx_arg4(n, args) |
| 727 | #define ___bpf_ctx_arg6(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt6(t, x, args)) ___bpf_ctx_arg5(n, args) |
| 728 | #define ___bpf_ctx_arg7(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt7(t, x, args)) ___bpf_ctx_arg6(n, args) |
| 729 | #define ___bpf_ctx_arg8(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt8(t, x, args)) ___bpf_ctx_arg7(n, args) |
| 730 | #define ___bpf_ctx_arg9(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt9(t, x, args)) ___bpf_ctx_arg8(n, args) |
| 731 | #define ___bpf_ctx_arg10(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt10(t, x, args)) ___bpf_ctx_arg9(n, args) |
| 732 | #define ___bpf_ctx_arg11(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt11(t, x, args)) ___bpf_ctx_arg10(n, args) |
| 733 | #define ___bpf_ctx_arg12(n, t, x, args...) , ___bpf_union_arg(t, x, n - ___bpf_reg_cnt12(t, x, args)) ___bpf_ctx_arg11(n, args) |
| 734 | #define ___bpf_ctx_arg(args...) ___bpf_apply(___bpf_ctx_arg, ___bpf_narg2(args))(___bpf_reg_cnt(args), args) |
| 735 | |
| 736 | #define ___bpf_ctx_decl0() |
| 737 | #define ___bpf_ctx_decl1(t, x) , t x |
| 738 | #define ___bpf_ctx_decl2(t, x, args...) , t x ___bpf_ctx_decl1(args) |
| 739 | #define ___bpf_ctx_decl3(t, x, args...) , t x ___bpf_ctx_decl2(args) |
| 740 | #define ___bpf_ctx_decl4(t, x, args...) , t x ___bpf_ctx_decl3(args) |
| 741 | #define ___bpf_ctx_decl5(t, x, args...) , t x ___bpf_ctx_decl4(args) |
| 742 | #define ___bpf_ctx_decl6(t, x, args...) , t x ___bpf_ctx_decl5(args) |
| 743 | #define ___bpf_ctx_decl7(t, x, args...) , t x ___bpf_ctx_decl6(args) |
| 744 | #define ___bpf_ctx_decl8(t, x, args...) , t x ___bpf_ctx_decl7(args) |
| 745 | #define ___bpf_ctx_decl9(t, x, args...) , t x ___bpf_ctx_decl8(args) |
| 746 | #define ___bpf_ctx_decl10(t, x, args...) , t x ___bpf_ctx_decl9(args) |
| 747 | #define ___bpf_ctx_decl11(t, x, args...) , t x ___bpf_ctx_decl10(args) |
| 748 | #define ___bpf_ctx_decl12(t, x, args...) , t x ___bpf_ctx_decl11(args) |
| 749 | #define ___bpf_ctx_decl(args...) ___bpf_apply(___bpf_ctx_decl, ___bpf_narg2(args))(args) |
Yonghong Song | 34586d2 | 2022-08-31 08:27:07 -0700 | [diff] [blame] | 750 | |
| 751 | /* |
Yonghong Song | 9f2f5d7 | 2022-09-09 19:52:14 -0700 | [diff] [blame] | 752 | * BPF_PROG2 is an enhanced version of BPF_PROG in order to handle struct |
| 753 | * arguments. Since each struct argument might take one or two u64 values |
| 754 | * in the trampoline stack, argument type size is needed to place proper number |
| 755 | * of u64 values for each argument. Therefore, BPF_PROG2 has different |
| 756 | * syntax from BPF_PROG. For example, for the following BPF_PROG syntax: |
| 757 | * |
| 758 | * int BPF_PROG(test2, int a, int b) { ... } |
| 759 | * |
| 760 | * the corresponding BPF_PROG2 syntax is: |
| 761 | * |
| 762 | * int BPF_PROG2(test2, int, a, int, b) { ... } |
| 763 | * |
| 764 | * where type and the corresponding argument name are separated by comma. |
| 765 | * |
| 766 | * Use BPF_PROG2 macro if one of the arguments might be a struct/union larger |
| 767 | * than 8 bytes: |
| 768 | * |
| 769 | * int BPF_PROG2(test_struct_arg, struct bpf_testmod_struct_arg_1, a, int, b, |
| 770 | * int, c, int, d, struct bpf_testmod_struct_arg_2, e, int, ret) |
| 771 | * { |
| 772 | * // access a, b, c, d, e, and ret directly |
| 773 | * ... |
| 774 | * } |
Yonghong Song | 34586d2 | 2022-08-31 08:27:07 -0700 | [diff] [blame] | 775 | */ |
| 776 | #define BPF_PROG2(name, args...) \ |
| 777 | name(unsigned long long *ctx); \ |
| 778 | static __always_inline typeof(name(0)) \ |
Yonghong Song | 9f2f5d7 | 2022-09-09 19:52:14 -0700 | [diff] [blame] | 779 | ____##name(unsigned long long *ctx ___bpf_ctx_decl(args)); \ |
Yonghong Song | 34586d2 | 2022-08-31 08:27:07 -0700 | [diff] [blame] | 780 | typeof(name(0)) name(unsigned long long *ctx) \ |
| 781 | { \ |
Yonghong Song | 9f2f5d7 | 2022-09-09 19:52:14 -0700 | [diff] [blame] | 782 | return ____##name(ctx ___bpf_ctx_arg(args)); \ |
Yonghong Song | 34586d2 | 2022-08-31 08:27:07 -0700 | [diff] [blame] | 783 | } \ |
| 784 | static __always_inline typeof(name(0)) \ |
Yonghong Song | 9f2f5d7 | 2022-09-09 19:52:14 -0700 | [diff] [blame] | 785 | ____##name(unsigned long long *ctx ___bpf_ctx_decl(args)) |
Yonghong Song | 34586d2 | 2022-08-31 08:27:07 -0700 | [diff] [blame] | 786 | |
Andrii Nakryiko | df8ff35 | 2020-02-29 15:11:12 -0800 | [diff] [blame] | 787 | struct pt_regs; |
| 788 | |
Andrii Nakryiko | f60edf5 | 2021-12-22 13:39:24 -0800 | [diff] [blame] | 789 | #define ___bpf_kprobe_args0() ctx |
| 790 | #define ___bpf_kprobe_args1(x) ___bpf_kprobe_args0(), (void *)PT_REGS_PARM1(ctx) |
| 791 | #define ___bpf_kprobe_args2(x, args...) ___bpf_kprobe_args1(args), (void *)PT_REGS_PARM2(ctx) |
| 792 | #define ___bpf_kprobe_args3(x, args...) ___bpf_kprobe_args2(args), (void *)PT_REGS_PARM3(ctx) |
| 793 | #define ___bpf_kprobe_args4(x, args...) ___bpf_kprobe_args3(args), (void *)PT_REGS_PARM4(ctx) |
| 794 | #define ___bpf_kprobe_args5(x, args...) ___bpf_kprobe_args4(args), (void *)PT_REGS_PARM5(ctx) |
Andrii Nakryiko | 3c59623 | 2023-01-20 12:08:50 -0800 | [diff] [blame] | 795 | #define ___bpf_kprobe_args6(x, args...) ___bpf_kprobe_args5(args), (void *)PT_REGS_PARM6(ctx) |
| 796 | #define ___bpf_kprobe_args7(x, args...) ___bpf_kprobe_args6(args), (void *)PT_REGS_PARM7(ctx) |
| 797 | #define ___bpf_kprobe_args8(x, args...) ___bpf_kprobe_args7(args), (void *)PT_REGS_PARM8(ctx) |
Andrii Nakryiko | f60edf5 | 2021-12-22 13:39:24 -0800 | [diff] [blame] | 798 | #define ___bpf_kprobe_args(args...) ___bpf_apply(___bpf_kprobe_args, ___bpf_narg(args))(args) |
Andrii Nakryiko | df8ff35 | 2020-02-29 15:11:12 -0800 | [diff] [blame] | 799 | |
| 800 | /* |
| 801 | * BPF_KPROBE serves the same purpose for kprobes as BPF_PROG for |
| 802 | * tp_btf/fentry/fexit BPF programs. It hides the underlying platform-specific |
| 803 | * low-level way of getting kprobe input arguments from struct pt_regs, and |
| 804 | * provides a familiar typed and named function arguments syntax and |
| 805 | * semantics of accessing kprobe input paremeters. |
| 806 | * |
| 807 | * Original struct pt_regs* context is preserved as 'ctx' argument. This might |
| 808 | * be necessary when using BPF helpers like bpf_perf_event_output(). |
| 809 | */ |
| 810 | #define BPF_KPROBE(name, args...) \ |
| 811 | name(struct pt_regs *ctx); \ |
James Hilliard | d25f40f | 2022-08-03 09:14:03 -0600 | [diff] [blame] | 812 | static __always_inline typeof(name(0)) \ |
Andrii Nakryiko | df8ff35 | 2020-02-29 15:11:12 -0800 | [diff] [blame] | 813 | ____##name(struct pt_regs *ctx, ##args); \ |
| 814 | typeof(name(0)) name(struct pt_regs *ctx) \ |
| 815 | { \ |
| 816 | _Pragma("GCC diagnostic push") \ |
| 817 | _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ |
| 818 | return ____##name(___bpf_kprobe_args(args)); \ |
| 819 | _Pragma("GCC diagnostic pop") \ |
| 820 | } \ |
James Hilliard | d25f40f | 2022-08-03 09:14:03 -0600 | [diff] [blame] | 821 | static __always_inline typeof(name(0)) \ |
Andrii Nakryiko | df8ff35 | 2020-02-29 15:11:12 -0800 | [diff] [blame] | 822 | ____##name(struct pt_regs *ctx, ##args) |
| 823 | |
Andrii Nakryiko | f60edf5 | 2021-12-22 13:39:24 -0800 | [diff] [blame] | 824 | #define ___bpf_kretprobe_args0() ctx |
| 825 | #define ___bpf_kretprobe_args1(x) ___bpf_kretprobe_args0(), (void *)PT_REGS_RC(ctx) |
| 826 | #define ___bpf_kretprobe_args(args...) ___bpf_apply(___bpf_kretprobe_args, ___bpf_narg(args))(args) |
Andrii Nakryiko | df8ff35 | 2020-02-29 15:11:12 -0800 | [diff] [blame] | 827 | |
| 828 | /* |
| 829 | * BPF_KRETPROBE is similar to BPF_KPROBE, except, it only provides optional |
| 830 | * return value (in addition to `struct pt_regs *ctx`), but no input |
| 831 | * arguments, because they will be clobbered by the time probed function |
| 832 | * returns. |
| 833 | */ |
| 834 | #define BPF_KRETPROBE(name, args...) \ |
| 835 | name(struct pt_regs *ctx); \ |
James Hilliard | d25f40f | 2022-08-03 09:14:03 -0600 | [diff] [blame] | 836 | static __always_inline typeof(name(0)) \ |
Andrii Nakryiko | df8ff35 | 2020-02-29 15:11:12 -0800 | [diff] [blame] | 837 | ____##name(struct pt_regs *ctx, ##args); \ |
| 838 | typeof(name(0)) name(struct pt_regs *ctx) \ |
| 839 | { \ |
| 840 | _Pragma("GCC diagnostic push") \ |
| 841 | _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ |
| 842 | return ____##name(___bpf_kretprobe_args(args)); \ |
| 843 | _Pragma("GCC diagnostic pop") \ |
| 844 | } \ |
| 845 | static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args) |
| 846 | |
Andrii Nakryiko | 6f5d467 | 2022-07-14 00:07:53 -0700 | [diff] [blame] | 847 | /* If kernel has CONFIG_ARCH_HAS_SYSCALL_WRAPPER, read pt_regs directly */ |
Hengqi Chen | 816ae10 | 2022-02-07 22:31:33 +0800 | [diff] [blame] | 848 | #define ___bpf_syscall_args0() ctx |
Andrii Nakryiko | 6f5d467 | 2022-07-14 00:07:53 -0700 | [diff] [blame] | 849 | #define ___bpf_syscall_args1(x) ___bpf_syscall_args0(), (void *)PT_REGS_PARM1_SYSCALL(regs) |
| 850 | #define ___bpf_syscall_args2(x, args...) ___bpf_syscall_args1(args), (void *)PT_REGS_PARM2_SYSCALL(regs) |
| 851 | #define ___bpf_syscall_args3(x, args...) ___bpf_syscall_args2(args), (void *)PT_REGS_PARM3_SYSCALL(regs) |
| 852 | #define ___bpf_syscall_args4(x, args...) ___bpf_syscall_args3(args), (void *)PT_REGS_PARM4_SYSCALL(regs) |
| 853 | #define ___bpf_syscall_args5(x, args...) ___bpf_syscall_args4(args), (void *)PT_REGS_PARM5_SYSCALL(regs) |
Andrii Nakryiko | 8ccabee | 2023-01-20 12:09:01 -0800 | [diff] [blame] | 854 | #define ___bpf_syscall_args6(x, args...) ___bpf_syscall_args5(args), (void *)PT_REGS_PARM6_SYSCALL(regs) |
| 855 | #define ___bpf_syscall_args7(x, args...) ___bpf_syscall_args6(args), (void *)PT_REGS_PARM7_SYSCALL(regs) |
Hengqi Chen | 816ae10 | 2022-02-07 22:31:33 +0800 | [diff] [blame] | 856 | #define ___bpf_syscall_args(args...) ___bpf_apply(___bpf_syscall_args, ___bpf_narg(args))(args) |
| 857 | |
Andrii Nakryiko | 6f5d467 | 2022-07-14 00:07:53 -0700 | [diff] [blame] | 858 | /* If kernel doesn't have CONFIG_ARCH_HAS_SYSCALL_WRAPPER, we have to BPF_CORE_READ from pt_regs */ |
| 859 | #define ___bpf_syswrap_args0() ctx |
| 860 | #define ___bpf_syswrap_args1(x) ___bpf_syswrap_args0(), (void *)PT_REGS_PARM1_CORE_SYSCALL(regs) |
| 861 | #define ___bpf_syswrap_args2(x, args...) ___bpf_syswrap_args1(args), (void *)PT_REGS_PARM2_CORE_SYSCALL(regs) |
| 862 | #define ___bpf_syswrap_args3(x, args...) ___bpf_syswrap_args2(args), (void *)PT_REGS_PARM3_CORE_SYSCALL(regs) |
| 863 | #define ___bpf_syswrap_args4(x, args...) ___bpf_syswrap_args3(args), (void *)PT_REGS_PARM4_CORE_SYSCALL(regs) |
| 864 | #define ___bpf_syswrap_args5(x, args...) ___bpf_syswrap_args4(args), (void *)PT_REGS_PARM5_CORE_SYSCALL(regs) |
Andrii Nakryiko | 8ccabee | 2023-01-20 12:09:01 -0800 | [diff] [blame] | 865 | #define ___bpf_syswrap_args6(x, args...) ___bpf_syswrap_args5(args), (void *)PT_REGS_PARM6_CORE_SYSCALL(regs) |
| 866 | #define ___bpf_syswrap_args7(x, args...) ___bpf_syswrap_args6(args), (void *)PT_REGS_PARM7_CORE_SYSCALL(regs) |
Andrii Nakryiko | 6f5d467 | 2022-07-14 00:07:53 -0700 | [diff] [blame] | 867 | #define ___bpf_syswrap_args(args...) ___bpf_apply(___bpf_syswrap_args, ___bpf_narg(args))(args) |
| 868 | |
Hengqi Chen | 816ae10 | 2022-02-07 22:31:33 +0800 | [diff] [blame] | 869 | /* |
Andrii Nakryiko | 6f5d467 | 2022-07-14 00:07:53 -0700 | [diff] [blame] | 870 | * BPF_KSYSCALL is a variant of BPF_KPROBE, which is intended for |
Hengqi Chen | 816ae10 | 2022-02-07 22:31:33 +0800 | [diff] [blame] | 871 | * tracing syscall functions, like __x64_sys_close. It hides the underlying |
| 872 | * platform-specific low-level way of getting syscall input arguments from |
| 873 | * struct pt_regs, and provides a familiar typed and named function arguments |
| 874 | * syntax and semantics of accessing syscall input parameters. |
| 875 | * |
Andrii Nakryiko | 6f5d467 | 2022-07-14 00:07:53 -0700 | [diff] [blame] | 876 | * Original struct pt_regs * context is preserved as 'ctx' argument. This might |
Hengqi Chen | 816ae10 | 2022-02-07 22:31:33 +0800 | [diff] [blame] | 877 | * be necessary when using BPF helpers like bpf_perf_event_output(). |
| 878 | * |
Ilya Leoshkevich | 2d369b4 | 2022-07-26 15:40:07 +0200 | [diff] [blame] | 879 | * At the moment BPF_KSYSCALL does not transparently handle all the calling |
| 880 | * convention quirks for the following syscalls: |
| 881 | * |
| 882 | * - mmap(): __ARCH_WANT_SYS_OLD_MMAP. |
| 883 | * - clone(): CONFIG_CLONE_BACKWARDS, CONFIG_CLONE_BACKWARDS2 and |
| 884 | * CONFIG_CLONE_BACKWARDS3. |
| 885 | * - socket-related syscalls: __ARCH_WANT_SYS_SOCKETCALL. |
| 886 | * - compat syscalls. |
| 887 | * |
| 888 | * This may or may not change in the future. User needs to take extra measures |
| 889 | * to handle such quirks explicitly, if necessary. |
Andrii Nakryiko | 6f5d467 | 2022-07-14 00:07:53 -0700 | [diff] [blame] | 890 | * |
| 891 | * This macro relies on BPF CO-RE support and virtual __kconfig externs. |
Hengqi Chen | 816ae10 | 2022-02-07 22:31:33 +0800 | [diff] [blame] | 892 | */ |
Andrii Nakryiko | 6f5d467 | 2022-07-14 00:07:53 -0700 | [diff] [blame] | 893 | #define BPF_KSYSCALL(name, args...) \ |
Hengqi Chen | 816ae10 | 2022-02-07 22:31:33 +0800 | [diff] [blame] | 894 | name(struct pt_regs *ctx); \ |
Andrii Nakryiko | 6f5d467 | 2022-07-14 00:07:53 -0700 | [diff] [blame] | 895 | extern _Bool LINUX_HAS_SYSCALL_WRAPPER __kconfig; \ |
James Hilliard | d25f40f | 2022-08-03 09:14:03 -0600 | [diff] [blame] | 896 | static __always_inline typeof(name(0)) \ |
Hengqi Chen | 816ae10 | 2022-02-07 22:31:33 +0800 | [diff] [blame] | 897 | ____##name(struct pt_regs *ctx, ##args); \ |
| 898 | typeof(name(0)) name(struct pt_regs *ctx) \ |
| 899 | { \ |
Andrii Nakryiko | 6f5d467 | 2022-07-14 00:07:53 -0700 | [diff] [blame] | 900 | struct pt_regs *regs = LINUX_HAS_SYSCALL_WRAPPER \ |
| 901 | ? (struct pt_regs *)PT_REGS_PARM1(ctx) \ |
| 902 | : ctx; \ |
Hengqi Chen | 816ae10 | 2022-02-07 22:31:33 +0800 | [diff] [blame] | 903 | _Pragma("GCC diagnostic push") \ |
| 904 | _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ |
Andrii Nakryiko | 6f5d467 | 2022-07-14 00:07:53 -0700 | [diff] [blame] | 905 | if (LINUX_HAS_SYSCALL_WRAPPER) \ |
| 906 | return ____##name(___bpf_syswrap_args(args)); \ |
| 907 | else \ |
| 908 | return ____##name(___bpf_syscall_args(args)); \ |
Hengqi Chen | 816ae10 | 2022-02-07 22:31:33 +0800 | [diff] [blame] | 909 | _Pragma("GCC diagnostic pop") \ |
| 910 | } \ |
James Hilliard | d25f40f | 2022-08-03 09:14:03 -0600 | [diff] [blame] | 911 | static __always_inline typeof(name(0)) \ |
Hengqi Chen | 816ae10 | 2022-02-07 22:31:33 +0800 | [diff] [blame] | 912 | ____##name(struct pt_regs *ctx, ##args) |
| 913 | |
Andrii Nakryiko | 6f5d467 | 2022-07-14 00:07:53 -0700 | [diff] [blame] | 914 | #define BPF_KPROBE_SYSCALL BPF_KSYSCALL |
| 915 | |
Andrii Nakryiko | ac4afd6 | 2023-01-20 12:08:59 -0800 | [diff] [blame] | 916 | /* BPF_UPROBE and BPF_URETPROBE are identical to BPF_KPROBE and BPF_KRETPROBE, |
| 917 | * but are named way less confusingly for SEC("uprobe") and SEC("uretprobe") |
| 918 | * use cases. |
| 919 | */ |
| 920 | #define BPF_UPROBE(name, args...) BPF_KPROBE(name, ##args) |
| 921 | #define BPF_URETPROBE(name, args...) BPF_KRETPROBE(name, ##args) |
| 922 | |
Andrii Nakryiko | 3ac4dbe | 2019-10-08 10:59:39 -0700 | [diff] [blame] | 923 | #endif |