bpf_tracing.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
  2. #ifndef __BPF_TRACING_H__
  3. #define __BPF_TRACING_H__
  4. /* Scan the ARCH passed in from ARCH env variable (see Makefile) */
  5. #if defined(__TARGET_ARCH_x86)
  6. #define bpf_target_x86
  7. #define bpf_target_defined
  8. #elif defined(__TARGET_ARCH_s390)
  9. #define bpf_target_s390
  10. #define bpf_target_defined
  11. #elif defined(__TARGET_ARCH_arm)
  12. #define bpf_target_arm
  13. #define bpf_target_defined
  14. #elif defined(__TARGET_ARCH_arm64)
  15. #define bpf_target_arm64
  16. #define bpf_target_defined
  17. #elif defined(__TARGET_ARCH_mips)
  18. #define bpf_target_mips
  19. #define bpf_target_defined
  20. #elif defined(__TARGET_ARCH_powerpc)
  21. #define bpf_target_powerpc
  22. #define bpf_target_defined
  23. #elif defined(__TARGET_ARCH_sparc)
  24. #define bpf_target_sparc
  25. #define bpf_target_defined
  26. #elif defined(__TARGET_ARCH_riscv)
  27. #define bpf_target_riscv
  28. #define bpf_target_defined
  29. #else
  30. /* Fall back to what the compiler says */
  31. #if defined(__x86_64__)
  32. #define bpf_target_x86
  33. #define bpf_target_defined
  34. #elif defined(__s390__)
  35. #define bpf_target_s390
  36. #define bpf_target_defined
  37. #elif defined(__arm__)
  38. #define bpf_target_arm
  39. #define bpf_target_defined
  40. #elif defined(__aarch64__)
  41. #define bpf_target_arm64
  42. #define bpf_target_defined
  43. #elif defined(__mips__)
  44. #define bpf_target_mips
  45. #define bpf_target_defined
  46. #elif defined(__powerpc__)
  47. #define bpf_target_powerpc
  48. #define bpf_target_defined
  49. #elif defined(__sparc__)
  50. #define bpf_target_sparc
  51. #define bpf_target_defined
  52. #elif defined(__riscv) && __riscv_xlen == 64
  53. #define bpf_target_riscv
  54. #define bpf_target_defined
  55. #endif /* no compiler target */
  56. #endif
  57. #ifndef __BPF_TARGET_MISSING
  58. #define __BPF_TARGET_MISSING "GCC error \"Must specify a BPF target arch via __TARGET_ARCH_xxx\""
  59. #endif
  60. #if defined(bpf_target_x86)
  61. #if defined(__KERNEL__) || defined(__VMLINUX_H__)
  62. #define PT_REGS_PARM1(x) ((x)->di)
  63. #define PT_REGS_PARM2(x) ((x)->si)
  64. #define PT_REGS_PARM3(x) ((x)->dx)
  65. #define PT_REGS_PARM4(x) ((x)->cx)
  66. #define PT_REGS_PARM5(x) ((x)->r8)
  67. #define PT_REGS_RET(x) ((x)->sp)
  68. #define PT_REGS_FP(x) ((x)->bp)
  69. #define PT_REGS_RC(x) ((x)->ax)
  70. #define PT_REGS_SP(x) ((x)->sp)
  71. #define PT_REGS_IP(x) ((x)->ip)
  72. #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((x), di)
  73. #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((x), si)
  74. #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((x), dx)
  75. #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((x), cx)
  76. #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((x), r8)
  77. #define PT_REGS_RET_CORE(x) BPF_CORE_READ((x), sp)
  78. #define PT_REGS_FP_CORE(x) BPF_CORE_READ((x), bp)
  79. #define PT_REGS_RC_CORE(x) BPF_CORE_READ((x), ax)
  80. #define PT_REGS_SP_CORE(x) BPF_CORE_READ((x), sp)
  81. #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), ip)
  82. #else
  83. #ifdef __i386__
  84. /* i386 kernel is built with -mregparm=3 */
  85. #define PT_REGS_PARM1(x) ((x)->eax)
  86. #define PT_REGS_PARM2(x) ((x)->edx)
  87. #define PT_REGS_PARM3(x) ((x)->ecx)
  88. #define PT_REGS_PARM4(x) 0
  89. #define PT_REGS_PARM5(x) 0
  90. #define PT_REGS_RET(x) ((x)->esp)
  91. #define PT_REGS_FP(x) ((x)->ebp)
  92. #define PT_REGS_RC(x) ((x)->eax)
  93. #define PT_REGS_SP(x) ((x)->esp)
  94. #define PT_REGS_IP(x) ((x)->eip)
  95. #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((x), eax)
  96. #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((x), edx)
  97. #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((x), ecx)
  98. #define PT_REGS_PARM4_CORE(x) 0
  99. #define PT_REGS_PARM5_CORE(x) 0
  100. #define PT_REGS_RET_CORE(x) BPF_CORE_READ((x), esp)
  101. #define PT_REGS_FP_CORE(x) BPF_CORE_READ((x), ebp)
  102. #define PT_REGS_RC_CORE(x) BPF_CORE_READ((x), eax)
  103. #define PT_REGS_SP_CORE(x) BPF_CORE_READ((x), esp)
  104. #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), eip)
  105. #else
  106. #define PT_REGS_PARM1(x) ((x)->rdi)
  107. #define PT_REGS_PARM2(x) ((x)->rsi)
  108. #define PT_REGS_PARM3(x) ((x)->rdx)
  109. #define PT_REGS_PARM4(x) ((x)->rcx)
  110. #define PT_REGS_PARM5(x) ((x)->r8)
  111. #define PT_REGS_RET(x) ((x)->rsp)
  112. #define PT_REGS_FP(x) ((x)->rbp)
  113. #define PT_REGS_RC(x) ((x)->rax)
  114. #define PT_REGS_SP(x) ((x)->rsp)
  115. #define PT_REGS_IP(x) ((x)->rip)
  116. #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((x), rdi)
  117. #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((x), rsi)
  118. #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((x), rdx)
  119. #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((x), rcx)
  120. #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((x), r8)
  121. #define PT_REGS_RET_CORE(x) BPF_CORE_READ((x), rsp)
  122. #define PT_REGS_FP_CORE(x) BPF_CORE_READ((x), rbp)
  123. #define PT_REGS_RC_CORE(x) BPF_CORE_READ((x), rax)
  124. #define PT_REGS_SP_CORE(x) BPF_CORE_READ((x), rsp)
  125. #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), rip)
  126. #endif
  127. #endif
  128. #elif defined(bpf_target_s390)
  129. /* s390 provides user_pt_regs instead of struct pt_regs to userspace */
  130. struct pt_regs;
  131. #define PT_REGS_S390 const volatile user_pt_regs
  132. #define PT_REGS_PARM1(x) (((PT_REGS_S390 *)(x))->gprs[2])
  133. #define PT_REGS_PARM2(x) (((PT_REGS_S390 *)(x))->gprs[3])
  134. #define PT_REGS_PARM3(x) (((PT_REGS_S390 *)(x))->gprs[4])
  135. #define PT_REGS_PARM4(x) (((PT_REGS_S390 *)(x))->gprs[5])
  136. #define PT_REGS_PARM5(x) (((PT_REGS_S390 *)(x))->gprs[6])
  137. #define PT_REGS_RET(x) (((PT_REGS_S390 *)(x))->gprs[14])
  138. /* Works only with CONFIG_FRAME_POINTER */
  139. #define PT_REGS_FP(x) (((PT_REGS_S390 *)(x))->gprs[11])
  140. #define PT_REGS_RC(x) (((PT_REGS_S390 *)(x))->gprs[2])
  141. #define PT_REGS_SP(x) (((PT_REGS_S390 *)(x))->gprs[15])
  142. #define PT_REGS_IP(x) (((PT_REGS_S390 *)(x))->psw.addr)
  143. #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[2])
  144. #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[3])
  145. #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[4])
  146. #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[5])
  147. #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[6])
  148. #define PT_REGS_RET_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[14])
  149. #define PT_REGS_FP_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[11])
  150. #define PT_REGS_RC_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[2])
  151. #define PT_REGS_SP_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[15])
  152. #define PT_REGS_IP_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), psw.addr)
  153. #elif defined(bpf_target_arm)
  154. #define PT_REGS_PARM1(x) ((x)->uregs[0])
  155. #define PT_REGS_PARM2(x) ((x)->uregs[1])
  156. #define PT_REGS_PARM3(x) ((x)->uregs[2])
  157. #define PT_REGS_PARM4(x) ((x)->uregs[3])
  158. #define PT_REGS_PARM5(x) ((x)->uregs[4])
  159. #define PT_REGS_RET(x) ((x)->uregs[14])
  160. #define PT_REGS_FP(x) ((x)->uregs[11]) /* Works only with CONFIG_FRAME_POINTER */
  161. #define PT_REGS_RC(x) ((x)->uregs[0])
  162. #define PT_REGS_SP(x) ((x)->uregs[13])
  163. #define PT_REGS_IP(x) ((x)->uregs[12])
  164. #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((x), uregs[0])
  165. #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((x), uregs[1])
  166. #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((x), uregs[2])
  167. #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((x), uregs[3])
  168. #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((x), uregs[4])
  169. #define PT_REGS_RET_CORE(x) BPF_CORE_READ((x), uregs[14])
  170. #define PT_REGS_FP_CORE(x) BPF_CORE_READ((x), uregs[11])
  171. #define PT_REGS_RC_CORE(x) BPF_CORE_READ((x), uregs[0])
  172. #define PT_REGS_SP_CORE(x) BPF_CORE_READ((x), uregs[13])
  173. #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), uregs[12])
  174. #elif defined(bpf_target_arm64)
  175. /* arm64 provides struct user_pt_regs instead of struct pt_regs to userspace */
  176. struct pt_regs;
  177. #define PT_REGS_ARM64 const volatile struct user_pt_regs
  178. #define PT_REGS_PARM1(x) (((PT_REGS_ARM64 *)(x))->regs[0])
  179. #define PT_REGS_PARM2(x) (((PT_REGS_ARM64 *)(x))->regs[1])
  180. #define PT_REGS_PARM3(x) (((PT_REGS_ARM64 *)(x))->regs[2])
  181. #define PT_REGS_PARM4(x) (((PT_REGS_ARM64 *)(x))->regs[3])
  182. #define PT_REGS_PARM5(x) (((PT_REGS_ARM64 *)(x))->regs[4])
  183. #define PT_REGS_RET(x) (((PT_REGS_ARM64 *)(x))->regs[30])
  184. /* Works only with CONFIG_FRAME_POINTER */
  185. #define PT_REGS_FP(x) (((PT_REGS_ARM64 *)(x))->regs[29])
  186. #define PT_REGS_RC(x) (((PT_REGS_ARM64 *)(x))->regs[0])
  187. #define PT_REGS_SP(x) (((PT_REGS_ARM64 *)(x))->sp)
  188. #define PT_REGS_IP(x) (((PT_REGS_ARM64 *)(x))->pc)
  189. #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[0])
  190. #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[1])
  191. #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[2])
  192. #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[3])
  193. #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[4])
  194. #define PT_REGS_RET_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[30])
  195. #define PT_REGS_FP_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[29])
  196. #define PT_REGS_RC_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[0])
  197. #define PT_REGS_SP_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), sp)
  198. #define PT_REGS_IP_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), pc)
  199. #elif defined(bpf_target_mips)
  200. #define PT_REGS_PARM1(x) ((x)->regs[4])
  201. #define PT_REGS_PARM2(x) ((x)->regs[5])
  202. #define PT_REGS_PARM3(x) ((x)->regs[6])
  203. #define PT_REGS_PARM4(x) ((x)->regs[7])
  204. #define PT_REGS_PARM5(x) ((x)->regs[8])
  205. #define PT_REGS_RET(x) ((x)->regs[31])
  206. #define PT_REGS_FP(x) ((x)->regs[30]) /* Works only with CONFIG_FRAME_POINTER */
  207. #define PT_REGS_RC(x) ((x)->regs[2])
  208. #define PT_REGS_SP(x) ((x)->regs[29])
  209. #define PT_REGS_IP(x) ((x)->cp0_epc)
  210. #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((x), regs[4])
  211. #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((x), regs[5])
  212. #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((x), regs[6])
  213. #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((x), regs[7])
  214. #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((x), regs[8])
  215. #define PT_REGS_RET_CORE(x) BPF_CORE_READ((x), regs[31])
  216. #define PT_REGS_FP_CORE(x) BPF_CORE_READ((x), regs[30])
  217. #define PT_REGS_RC_CORE(x) BPF_CORE_READ((x), regs[2])
  218. #define PT_REGS_SP_CORE(x) BPF_CORE_READ((x), regs[29])
  219. #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), cp0_epc)
  220. #elif defined(bpf_target_powerpc)
  221. #define PT_REGS_PARM1(x) ((x)->gpr[3])
  222. #define PT_REGS_PARM2(x) ((x)->gpr[4])
  223. #define PT_REGS_PARM3(x) ((x)->gpr[5])
  224. #define PT_REGS_PARM4(x) ((x)->gpr[6])
  225. #define PT_REGS_PARM5(x) ((x)->gpr[7])
  226. #define PT_REGS_RC(x) ((x)->gpr[3])
  227. #define PT_REGS_SP(x) ((x)->sp)
  228. #define PT_REGS_IP(x) ((x)->nip)
  229. #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((x), gpr[3])
  230. #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((x), gpr[4])
  231. #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((x), gpr[5])
  232. #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((x), gpr[6])
  233. #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((x), gpr[7])
  234. #define PT_REGS_RC_CORE(x) BPF_CORE_READ((x), gpr[3])
  235. #define PT_REGS_SP_CORE(x) BPF_CORE_READ((x), sp)
  236. #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), nip)
  237. #elif defined(bpf_target_sparc)
  238. #define PT_REGS_PARM1(x) ((x)->u_regs[UREG_I0])
  239. #define PT_REGS_PARM2(x) ((x)->u_regs[UREG_I1])
  240. #define PT_REGS_PARM3(x) ((x)->u_regs[UREG_I2])
  241. #define PT_REGS_PARM4(x) ((x)->u_regs[UREG_I3])
  242. #define PT_REGS_PARM5(x) ((x)->u_regs[UREG_I4])
  243. #define PT_REGS_RET(x) ((x)->u_regs[UREG_I7])
  244. #define PT_REGS_RC(x) ((x)->u_regs[UREG_I0])
  245. #define PT_REGS_SP(x) ((x)->u_regs[UREG_FP])
  246. #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((x), u_regs[UREG_I0])
  247. #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((x), u_regs[UREG_I1])
  248. #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((x), u_regs[UREG_I2])
  249. #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((x), u_regs[UREG_I3])
  250. #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((x), u_regs[UREG_I4])
  251. #define PT_REGS_RET_CORE(x) BPF_CORE_READ((x), u_regs[UREG_I7])
  252. #define PT_REGS_RC_CORE(x) BPF_CORE_READ((x), u_regs[UREG_I0])
  253. #define PT_REGS_SP_CORE(x) BPF_CORE_READ((x), u_regs[UREG_FP])
  254. /* Should this also be a bpf_target check for the sparc case? */
  255. #if defined(__arch64__)
  256. #define PT_REGS_IP(x) ((x)->tpc)
  257. #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), tpc)
  258. #else
  259. #define PT_REGS_IP(x) ((x)->pc)
  260. #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), pc)
  261. #endif
  262. #elif defined(bpf_target_riscv)
  263. struct pt_regs;
  264. #define PT_REGS_RV const volatile struct user_regs_struct
  265. #define PT_REGS_PARM1(x) (((PT_REGS_RV *)(x))->a0)
  266. #define PT_REGS_PARM2(x) (((PT_REGS_RV *)(x))->a1)
  267. #define PT_REGS_PARM3(x) (((PT_REGS_RV *)(x))->a2)
  268. #define PT_REGS_PARM4(x) (((PT_REGS_RV *)(x))->a3)
  269. #define PT_REGS_PARM5(x) (((PT_REGS_RV *)(x))->a4)
  270. #define PT_REGS_RET(x) (((PT_REGS_RV *)(x))->ra)
  271. #define PT_REGS_FP(x) (((PT_REGS_RV *)(x))->s5)
  272. #define PT_REGS_RC(x) (((PT_REGS_RV *)(x))->a5)
  273. #define PT_REGS_SP(x) (((PT_REGS_RV *)(x))->sp)
  274. #define PT_REGS_IP(x) (((PT_REGS_RV *)(x))->epc)
  275. #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), a0)
  276. #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), a1)
  277. #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), a2)
  278. #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), a3)
  279. #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), a4)
  280. #define PT_REGS_RET_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), ra)
  281. #define PT_REGS_FP_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), fp)
  282. #define PT_REGS_RC_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), a5)
  283. #define PT_REGS_SP_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), sp)
  284. #define PT_REGS_IP_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), epc)
  285. #endif
  286. #if defined(bpf_target_powerpc)
  287. #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; })
  288. #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP
  289. #elif defined(bpf_target_sparc)
  290. #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = PT_REGS_RET(ctx); })
  291. #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP
  292. #elif defined(bpf_target_defined)
  293. #define BPF_KPROBE_READ_RET_IP(ip, ctx) \
  294. ({ bpf_probe_read_kernel(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); })
  295. #define BPF_KRETPROBE_READ_RET_IP(ip, ctx) \
  296. ({ bpf_probe_read_kernel(&(ip), sizeof(ip), \
  297. (void *)(PT_REGS_FP(ctx) + sizeof(ip))); })
  298. #endif
  299. #if !defined(bpf_target_defined)
  300. #define PT_REGS_PARM1(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  301. #define PT_REGS_PARM2(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  302. #define PT_REGS_PARM3(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  303. #define PT_REGS_PARM4(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  304. #define PT_REGS_PARM5(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  305. #define PT_REGS_RET(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  306. #define PT_REGS_FP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  307. #define PT_REGS_RC(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  308. #define PT_REGS_SP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  309. #define PT_REGS_IP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  310. #define PT_REGS_PARM1_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  311. #define PT_REGS_PARM2_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  312. #define PT_REGS_PARM3_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  313. #define PT_REGS_PARM4_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  314. #define PT_REGS_PARM5_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  315. #define PT_REGS_RET_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  316. #define PT_REGS_FP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  317. #define PT_REGS_RC_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  318. #define PT_REGS_SP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  319. #define PT_REGS_IP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  320. #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  321. #define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ _Pragma(__BPF_TARGET_MISSING); 0l; })
  322. #endif /* !defined(bpf_target_defined) */
  323. #ifndef ___bpf_concat
  324. #define ___bpf_concat(a, b) a ## b
  325. #endif
  326. #ifndef ___bpf_apply
  327. #define ___bpf_apply(fn, n) ___bpf_concat(fn, n)
  328. #endif
  329. #ifndef ___bpf_nth
  330. #define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N
  331. #endif
  332. #ifndef ___bpf_narg
  333. #define ___bpf_narg(...) \
  334. ___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
  335. #endif
  336. #define ___bpf_ctx_cast0() ctx
  337. #define ___bpf_ctx_cast1(x) ___bpf_ctx_cast0(), (void *)ctx[0]
  338. #define ___bpf_ctx_cast2(x, args...) ___bpf_ctx_cast1(args), (void *)ctx[1]
  339. #define ___bpf_ctx_cast3(x, args...) ___bpf_ctx_cast2(args), (void *)ctx[2]
  340. #define ___bpf_ctx_cast4(x, args...) ___bpf_ctx_cast3(args), (void *)ctx[3]
  341. #define ___bpf_ctx_cast5(x, args...) ___bpf_ctx_cast4(args), (void *)ctx[4]
  342. #define ___bpf_ctx_cast6(x, args...) ___bpf_ctx_cast5(args), (void *)ctx[5]
  343. #define ___bpf_ctx_cast7(x, args...) ___bpf_ctx_cast6(args), (void *)ctx[6]
  344. #define ___bpf_ctx_cast8(x, args...) ___bpf_ctx_cast7(args), (void *)ctx[7]
  345. #define ___bpf_ctx_cast9(x, args...) ___bpf_ctx_cast8(args), (void *)ctx[8]
  346. #define ___bpf_ctx_cast10(x, args...) ___bpf_ctx_cast9(args), (void *)ctx[9]
  347. #define ___bpf_ctx_cast11(x, args...) ___bpf_ctx_cast10(args), (void *)ctx[10]
  348. #define ___bpf_ctx_cast12(x, args...) ___bpf_ctx_cast11(args), (void *)ctx[11]
  349. #define ___bpf_ctx_cast(args...) \
  350. ___bpf_apply(___bpf_ctx_cast, ___bpf_narg(args))(args)
  351. /*
  352. * BPF_PROG is a convenience wrapper for generic tp_btf/fentry/fexit and
  353. * similar kinds of BPF programs, that accept input arguments as a single
  354. * pointer to untyped u64 array, where each u64 can actually be a typed
  355. * pointer or integer of different size. Instead of requring user to write
  356. * manual casts and work with array elements by index, BPF_PROG macro
  357. * allows user to declare a list of named and typed input arguments in the
  358. * same syntax as for normal C function. All the casting is hidden and
  359. * performed transparently, while user code can just assume working with
  360. * function arguments of specified type and name.
  361. *
  362. * Original raw context argument is preserved as well as 'ctx' argument.
  363. * This is useful when using BPF helpers that expect original context
  364. * as one of the parameters (e.g., for bpf_perf_event_output()).
  365. */
  366. #define BPF_PROG(name, args...) \
  367. name(unsigned long long *ctx); \
  368. static __attribute__((always_inline)) typeof(name(0)) \
  369. ____##name(unsigned long long *ctx, ##args); \
  370. typeof(name(0)) name(unsigned long long *ctx) \
  371. { \
  372. _Pragma("GCC diagnostic push") \
  373. _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
  374. return ____##name(___bpf_ctx_cast(args)); \
  375. _Pragma("GCC diagnostic pop") \
  376. } \
  377. static __attribute__((always_inline)) typeof(name(0)) \
  378. ____##name(unsigned long long *ctx, ##args)
  379. struct pt_regs;
  380. #define ___bpf_kprobe_args0() ctx
  381. #define ___bpf_kprobe_args1(x) \
  382. ___bpf_kprobe_args0(), (void *)PT_REGS_PARM1(ctx)
  383. #define ___bpf_kprobe_args2(x, args...) \
  384. ___bpf_kprobe_args1(args), (void *)PT_REGS_PARM2(ctx)
  385. #define ___bpf_kprobe_args3(x, args...) \
  386. ___bpf_kprobe_args2(args), (void *)PT_REGS_PARM3(ctx)
  387. #define ___bpf_kprobe_args4(x, args...) \
  388. ___bpf_kprobe_args3(args), (void *)PT_REGS_PARM4(ctx)
  389. #define ___bpf_kprobe_args5(x, args...) \
  390. ___bpf_kprobe_args4(args), (void *)PT_REGS_PARM5(ctx)
  391. #define ___bpf_kprobe_args(args...) \
  392. ___bpf_apply(___bpf_kprobe_args, ___bpf_narg(args))(args)
  393. /*
  394. * BPF_KPROBE serves the same purpose for kprobes as BPF_PROG for
  395. * tp_btf/fentry/fexit BPF programs. It hides the underlying platform-specific
  396. * low-level way of getting kprobe input arguments from struct pt_regs, and
  397. * provides a familiar typed and named function arguments syntax and
  398. * semantics of accessing kprobe input paremeters.
  399. *
  400. * Original struct pt_regs* context is preserved as 'ctx' argument. This might
  401. * be necessary when using BPF helpers like bpf_perf_event_output().
  402. */
  403. #define BPF_KPROBE(name, args...) \
  404. name(struct pt_regs *ctx); \
  405. static __attribute__((always_inline)) typeof(name(0)) \
  406. ____##name(struct pt_regs *ctx, ##args); \
  407. typeof(name(0)) name(struct pt_regs *ctx) \
  408. { \
  409. _Pragma("GCC diagnostic push") \
  410. _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
  411. return ____##name(___bpf_kprobe_args(args)); \
  412. _Pragma("GCC diagnostic pop") \
  413. } \
  414. static __attribute__((always_inline)) typeof(name(0)) \
  415. ____##name(struct pt_regs *ctx, ##args)
  416. #define ___bpf_kretprobe_args0() ctx
  417. #define ___bpf_kretprobe_args1(x) \
  418. ___bpf_kretprobe_args0(), (void *)PT_REGS_RC(ctx)
  419. #define ___bpf_kretprobe_args(args...) \
  420. ___bpf_apply(___bpf_kretprobe_args, ___bpf_narg(args))(args)
  421. /*
  422. * BPF_KRETPROBE is similar to BPF_KPROBE, except, it only provides optional
  423. * return value (in addition to `struct pt_regs *ctx`), but no input
  424. * arguments, because they will be clobbered by the time probed function
  425. * returns.
  426. */
  427. #define BPF_KRETPROBE(name, args...) \
  428. name(struct pt_regs *ctx); \
  429. static __attribute__((always_inline)) typeof(name(0)) \
  430. ____##name(struct pt_regs *ctx, ##args); \
  431. typeof(name(0)) name(struct pt_regs *ctx) \
  432. { \
  433. _Pragma("GCC diagnostic push") \
  434. _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
  435. return ____##name(___bpf_kretprobe_args(args)); \
  436. _Pragma("GCC diagnostic pop") \
  437. } \
  438. static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args)
  439. #endif