relo_core.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
  2. /* Copyright (c) 2019 Facebook */
  3. #ifndef __RELO_CORE_H
  4. #define __RELO_CORE_H
  5. #include <linux/bpf.h>
  6. struct bpf_core_cand {
  7. const struct btf *btf;
  8. __u32 id;
  9. };
  10. /* dynamically sized list of type IDs and its associated struct btf */
  11. struct bpf_core_cand_list {
  12. struct bpf_core_cand *cands;
  13. int len;
  14. };
  15. #define BPF_CORE_SPEC_MAX_LEN 64
  16. /* represents BPF CO-RE field or array element accessor */
  17. struct bpf_core_accessor {
  18. __u32 type_id; /* struct/union type or array element type */
  19. __u32 idx; /* field index or array index */
  20. const char *name; /* field name or NULL for array accessor */
  21. };
  22. struct bpf_core_spec {
  23. const struct btf *btf;
  24. /* high-level spec: named fields and array indices only */
  25. struct bpf_core_accessor spec[BPF_CORE_SPEC_MAX_LEN];
  26. /* original unresolved (no skip_mods_or_typedefs) root type ID */
  27. __u32 root_type_id;
  28. /* CO-RE relocation kind */
  29. enum bpf_core_relo_kind relo_kind;
  30. /* high-level spec length */
  31. int len;
  32. /* raw, low-level spec: 1-to-1 with accessor spec string */
  33. int raw_spec[BPF_CORE_SPEC_MAX_LEN];
  34. /* raw spec length */
  35. int raw_len;
  36. /* field bit offset represented by spec */
  37. __u32 bit_offset;
  38. };
  39. struct bpf_core_relo_res {
  40. /* expected value in the instruction, unless validate == false */
  41. __u64 orig_val;
  42. /* new value that needs to be patched up to */
  43. __u64 new_val;
  44. /* relocation unsuccessful, poison instruction, but don't fail load */
  45. bool poison;
  46. /* some relocations can't be validated against orig_val */
  47. bool validate;
  48. /* for field byte offset relocations or the forms:
  49. * *(T *)(rX + <off>) = rY
  50. * rX = *(T *)(rY + <off>),
  51. * we remember original and resolved field size to adjust direct
  52. * memory loads of pointers and integers; this is necessary for 32-bit
  53. * host kernel architectures, but also allows to automatically
  54. * relocate fields that were resized from, e.g., u32 to u64, etc.
  55. */
  56. bool fail_memsz_adjust;
  57. __u32 orig_sz;
  58. __u32 orig_type_id;
  59. __u32 new_sz;
  60. __u32 new_type_id;
  61. };
  62. int __bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
  63. const struct btf *targ_btf, __u32 targ_id, int level);
  64. int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
  65. const struct btf *targ_btf, __u32 targ_id);
  66. int __bpf_core_types_match(const struct btf *local_btf, __u32 local_id, const struct btf *targ_btf,
  67. __u32 targ_id, bool behind_ptr, int level);
  68. int bpf_core_types_match(const struct btf *local_btf, __u32 local_id, const struct btf *targ_btf,
  69. __u32 targ_id);
  70. size_t bpf_core_essential_name_len(const char *name);
  71. int bpf_core_calc_relo_insn(const char *prog_name,
  72. const struct bpf_core_relo *relo, int relo_idx,
  73. const struct btf *local_btf,
  74. struct bpf_core_cand_list *cands,
  75. struct bpf_core_spec *specs_scratch,
  76. struct bpf_core_relo_res *targ_res);
  77. int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
  78. int insn_idx, const struct bpf_core_relo *relo,
  79. int relo_idx, const struct bpf_core_relo_res *res);
  80. int bpf_core_parse_spec(const char *prog_name, const struct btf *btf,
  81. const struct bpf_core_relo *relo,
  82. struct bpf_core_spec *spec);
  83. int bpf_core_format_spec(char *buf, size_t buf_sz, const struct bpf_core_spec *spec);
  84. #endif