btf.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (c) 2018 Facebook */
  3. #ifndef _LINUX_BTF_H
  4. #define _LINUX_BTF_H 1
  5. #include <linux/types.h>
  6. #include <uapi/linux/btf.h>
  7. #include <uapi/linux/bpf.h>
  8. #define BTF_TYPE_EMIT(type) ((void)(type *)0)
  9. struct btf;
  10. struct btf_member;
  11. struct btf_type;
  12. union bpf_attr;
  13. struct btf_show;
  14. extern const struct file_operations btf_fops;
  15. void btf_put(struct btf *btf);
  16. int btf_new_fd(const union bpf_attr *attr);
  17. struct btf *btf_get_by_fd(int fd);
  18. int btf_get_info_by_fd(const struct btf *btf,
  19. const union bpf_attr *attr,
  20. union bpf_attr __user *uattr);
  21. /* Figure out the size of a type_id. If type_id is a modifier
  22. * (e.g. const), it will be resolved to find out the type with size.
  23. *
  24. * For example:
  25. * In describing "const void *", type_id is "const" and "const"
  26. * refers to "void *". The return type will be "void *".
  27. *
  28. * If type_id is a simple "int", then return type will be "int".
  29. *
  30. * @btf: struct btf object
  31. * @type_id: Find out the size of type_id. The type_id of the return
  32. * type is set to *type_id.
  33. * @ret_size: It can be NULL. If not NULL, the size of the return
  34. * type is set to *ret_size.
  35. * Return: The btf_type (resolved to another type with size info if needed).
  36. * NULL is returned if type_id itself does not have size info
  37. * (e.g. void) or it cannot be resolved to another type that
  38. * has size info.
  39. * *type_id and *ret_size will not be changed in the
  40. * NULL return case.
  41. */
  42. const struct btf_type *btf_type_id_size(const struct btf *btf,
  43. u32 *type_id,
  44. u32 *ret_size);
  45. /*
  46. * Options to control show behaviour.
  47. * - BTF_SHOW_COMPACT: no formatting around type information
  48. * - BTF_SHOW_NONAME: no struct/union member names/types
  49. * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values;
  50. * equivalent to %px.
  51. * - BTF_SHOW_ZERO: show zero-valued struct/union members; they
  52. * are not displayed by default
  53. * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read
  54. * data before displaying it.
  55. */
  56. #define BTF_SHOW_COMPACT BTF_F_COMPACT
  57. #define BTF_SHOW_NONAME BTF_F_NONAME
  58. #define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW
  59. #define BTF_SHOW_ZERO BTF_F_ZERO
  60. #define BTF_SHOW_UNSAFE (1ULL << 4)
  61. void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
  62. struct seq_file *m);
  63. int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
  64. struct seq_file *m, u64 flags);
  65. /*
  66. * Copy len bytes of string representation of obj of BTF type_id into buf.
  67. *
  68. * @btf: struct btf object
  69. * @type_id: type id of type obj points to
  70. * @obj: pointer to typed data
  71. * @buf: buffer to write to
  72. * @len: maximum length to write to buf
  73. * @flags: show options (see above)
  74. *
  75. * Return: length that would have been/was copied as per snprintf, or
  76. * negative error.
  77. */
  78. int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
  79. char *buf, int len, u64 flags);
  80. int btf_get_fd_by_id(u32 id);
  81. u32 btf_id(const struct btf *btf);
  82. bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
  83. const struct btf_member *m,
  84. u32 expected_offset, u32 expected_size);
  85. int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
  86. bool btf_type_is_void(const struct btf_type *t);
  87. s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
  88. const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
  89. u32 id, u32 *res_id);
  90. const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
  91. u32 id, u32 *res_id);
  92. const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
  93. u32 id, u32 *res_id);
  94. const struct btf_type *
  95. btf_resolve_size(const struct btf *btf, const struct btf_type *type,
  96. u32 *type_size);
  97. #define for_each_member(i, struct_type, member) \
  98. for (i = 0, member = btf_type_member(struct_type); \
  99. i < btf_type_vlen(struct_type); \
  100. i++, member++)
  101. #define for_each_vsi(i, datasec_type, member) \
  102. for (i = 0, member = btf_type_var_secinfo(datasec_type); \
  103. i < btf_type_vlen(datasec_type); \
  104. i++, member++)
  105. static inline bool btf_type_is_ptr(const struct btf_type *t)
  106. {
  107. return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
  108. }
  109. static inline bool btf_type_is_int(const struct btf_type *t)
  110. {
  111. return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
  112. }
  113. static inline bool btf_type_is_small_int(const struct btf_type *t)
  114. {
  115. return btf_type_is_int(t) && t->size <= sizeof(u64);
  116. }
  117. static inline bool btf_type_is_enum(const struct btf_type *t)
  118. {
  119. return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
  120. }
  121. static inline bool btf_type_is_typedef(const struct btf_type *t)
  122. {
  123. return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
  124. }
  125. static inline bool btf_type_is_func(const struct btf_type *t)
  126. {
  127. return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
  128. }
  129. static inline bool btf_type_is_func_proto(const struct btf_type *t)
  130. {
  131. return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
  132. }
  133. static inline bool btf_type_is_var(const struct btf_type *t)
  134. {
  135. return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
  136. }
  137. /* union is only a special case of struct:
  138. * all its offsetof(member) == 0
  139. */
  140. static inline bool btf_type_is_struct(const struct btf_type *t)
  141. {
  142. u8 kind = BTF_INFO_KIND(t->info);
  143. return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
  144. }
  145. static inline u16 btf_type_vlen(const struct btf_type *t)
  146. {
  147. return BTF_INFO_VLEN(t->info);
  148. }
  149. static inline u16 btf_func_linkage(const struct btf_type *t)
  150. {
  151. return BTF_INFO_VLEN(t->info);
  152. }
  153. static inline bool btf_type_kflag(const struct btf_type *t)
  154. {
  155. return BTF_INFO_KFLAG(t->info);
  156. }
  157. static inline u32 btf_member_bit_offset(const struct btf_type *struct_type,
  158. const struct btf_member *member)
  159. {
  160. return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
  161. : member->offset;
  162. }
  163. static inline u32 btf_member_bitfield_size(const struct btf_type *struct_type,
  164. const struct btf_member *member)
  165. {
  166. return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
  167. : 0;
  168. }
  169. static inline const struct btf_member *btf_type_member(const struct btf_type *t)
  170. {
  171. return (const struct btf_member *)(t + 1);
  172. }
  173. static inline const struct btf_var_secinfo *btf_type_var_secinfo(
  174. const struct btf_type *t)
  175. {
  176. return (const struct btf_var_secinfo *)(t + 1);
  177. }
  178. #ifdef CONFIG_BPF_SYSCALL
  179. const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
  180. const char *btf_name_by_offset(const struct btf *btf, u32 offset);
  181. struct btf *btf_parse_vmlinux(void);
  182. struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
  183. #else
  184. static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
  185. u32 type_id)
  186. {
  187. return NULL;
  188. }
  189. static inline const char *btf_name_by_offset(const struct btf *btf,
  190. u32 offset)
  191. {
  192. return NULL;
  193. }
  194. #endif
  195. #endif