bpf_tracing.h 37 KB

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