bpf_jit.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * The interface that a back-end should provide to bpf_jit_core.c.
  4. *
  5. * Copyright (c) 2024 Synopsys Inc.
  6. * Author: Shahab Vahedi <shahab@synopsys.com>
  7. */
  8. #ifndef _ARC_BPF_JIT_H
  9. #define _ARC_BPF_JIT_H
  10. #include <linux/bpf.h>
  11. #include <linux/filter.h>
  12. /* Print debug info and assert. */
  13. //#define ARC_BPF_JIT_DEBUG
  14. /* Determine the address type of the target. */
  15. #ifdef CONFIG_ISA_ARCV2
  16. #define ARC_ADDR u32
  17. #endif
  18. /*
  19. * For the translation of some BPF instructions, a temporary register
  20. * might be needed for some interim data.
  21. */
  22. #define JIT_REG_TMP MAX_BPF_JIT_REG
  23. /*
  24. * Buffer access: If buffer "b" is not NULL, advance by "n" bytes.
  25. *
  26. * This macro must be used in any place that potentially requires a
  27. * "buf + len". This way, we make sure that the "buf" argument for
  28. * the underlying "arc_*(buf, ...)" ends up as NULL instead of something
  29. * like "0+4" or "0+8", etc. Those "arc_*()" functions check their "buf"
  30. * value to decide if instructions should be emitted or not.
  31. */
  32. #define BUF(b, n) (((b) != NULL) ? ((b) + (n)) : (b))
  33. /************** Functions that the back-end must provide **************/
  34. /* Extension for 32-bit operations. */
  35. u8 zext(u8 *buf, u8 rd);
  36. /***** Moves *****/
  37. u8 mov_r32(u8 *buf, u8 rd, u8 rs, u8 sign_ext);
  38. u8 mov_r32_i32(u8 *buf, u8 reg, s32 imm);
  39. u8 mov_r64(u8 *buf, u8 rd, u8 rs, u8 sign_ext);
  40. u8 mov_r64_i32(u8 *buf, u8 reg, s32 imm);
  41. u8 mov_r64_i64(u8 *buf, u8 reg, u32 lo, u32 hi);
  42. /***** Loads and stores *****/
  43. u8 load_r(u8 *buf, u8 rd, u8 rs, s16 off, u8 size, bool sign_ext);
  44. u8 store_r(u8 *buf, u8 rd, u8 rs, s16 off, u8 size);
  45. u8 store_i(u8 *buf, s32 imm, u8 rd, s16 off, u8 size);
  46. /***** Addition *****/
  47. u8 add_r32(u8 *buf, u8 rd, u8 rs);
  48. u8 add_r32_i32(u8 *buf, u8 rd, s32 imm);
  49. u8 add_r64(u8 *buf, u8 rd, u8 rs);
  50. u8 add_r64_i32(u8 *buf, u8 rd, s32 imm);
  51. /***** Subtraction *****/
  52. u8 sub_r32(u8 *buf, u8 rd, u8 rs);
  53. u8 sub_r32_i32(u8 *buf, u8 rd, s32 imm);
  54. u8 sub_r64(u8 *buf, u8 rd, u8 rs);
  55. u8 sub_r64_i32(u8 *buf, u8 rd, s32 imm);
  56. /***** Multiplication *****/
  57. u8 mul_r32(u8 *buf, u8 rd, u8 rs);
  58. u8 mul_r32_i32(u8 *buf, u8 rd, s32 imm);
  59. u8 mul_r64(u8 *buf, u8 rd, u8 rs);
  60. u8 mul_r64_i32(u8 *buf, u8 rd, s32 imm);
  61. /***** Division *****/
  62. u8 div_r32(u8 *buf, u8 rd, u8 rs, bool sign_ext);
  63. u8 div_r32_i32(u8 *buf, u8 rd, s32 imm, bool sign_ext);
  64. /***** Remainder *****/
  65. u8 mod_r32(u8 *buf, u8 rd, u8 rs, bool sign_ext);
  66. u8 mod_r32_i32(u8 *buf, u8 rd, s32 imm, bool sign_ext);
  67. /***** Bitwise AND *****/
  68. u8 and_r32(u8 *buf, u8 rd, u8 rs);
  69. u8 and_r32_i32(u8 *buf, u8 rd, s32 imm);
  70. u8 and_r64(u8 *buf, u8 rd, u8 rs);
  71. u8 and_r64_i32(u8 *buf, u8 rd, s32 imm);
  72. /***** Bitwise OR *****/
  73. u8 or_r32(u8 *buf, u8 rd, u8 rs);
  74. u8 or_r32_i32(u8 *buf, u8 rd, s32 imm);
  75. u8 or_r64(u8 *buf, u8 rd, u8 rs);
  76. u8 or_r64_i32(u8 *buf, u8 rd, s32 imm);
  77. /***** Bitwise XOR *****/
  78. u8 xor_r32(u8 *buf, u8 rd, u8 rs);
  79. u8 xor_r32_i32(u8 *buf, u8 rd, s32 imm);
  80. u8 xor_r64(u8 *buf, u8 rd, u8 rs);
  81. u8 xor_r64_i32(u8 *buf, u8 rd, s32 imm);
  82. /***** Bitwise Negate *****/
  83. u8 neg_r32(u8 *buf, u8 r);
  84. u8 neg_r64(u8 *buf, u8 r);
  85. /***** Bitwise left shift *****/
  86. u8 lsh_r32(u8 *buf, u8 rd, u8 rs);
  87. u8 lsh_r32_i32(u8 *buf, u8 rd, u8 imm);
  88. u8 lsh_r64(u8 *buf, u8 rd, u8 rs);
  89. u8 lsh_r64_i32(u8 *buf, u8 rd, s32 imm);
  90. /***** Bitwise right shift (logical) *****/
  91. u8 rsh_r32(u8 *buf, u8 rd, u8 rs);
  92. u8 rsh_r32_i32(u8 *buf, u8 rd, u8 imm);
  93. u8 rsh_r64(u8 *buf, u8 rd, u8 rs);
  94. u8 rsh_r64_i32(u8 *buf, u8 rd, s32 imm);
  95. /***** Bitwise right shift (arithmetic) *****/
  96. u8 arsh_r32(u8 *buf, u8 rd, u8 rs);
  97. u8 arsh_r32_i32(u8 *buf, u8 rd, u8 imm);
  98. u8 arsh_r64(u8 *buf, u8 rd, u8 rs);
  99. u8 arsh_r64_i32(u8 *buf, u8 rd, s32 imm);
  100. /***** Frame related *****/
  101. u32 mask_for_used_regs(u8 bpf_reg, bool is_call);
  102. u8 arc_prologue(u8 *buf, u32 usage, u16 frame_size);
  103. u8 arc_epilogue(u8 *buf, u32 usage, u16 frame_size);
  104. /***** Jumps *****/
  105. /*
  106. * Different sorts of conditions (ARC enum as opposed to BPF_*).
  107. *
  108. * Do not change the order of enums here. ARC_CC_SLE+1 is used
  109. * to determine the number of JCCs.
  110. */
  111. enum ARC_CC {
  112. ARC_CC_UGT = 0, /* unsigned > */
  113. ARC_CC_UGE, /* unsigned >= */
  114. ARC_CC_ULT, /* unsigned < */
  115. ARC_CC_ULE, /* unsigned <= */
  116. ARC_CC_SGT, /* signed > */
  117. ARC_CC_SGE, /* signed >= */
  118. ARC_CC_SLT, /* signed < */
  119. ARC_CC_SLE, /* signed <= */
  120. ARC_CC_AL, /* always */
  121. ARC_CC_EQ, /* == */
  122. ARC_CC_NE, /* != */
  123. ARC_CC_SET, /* test */
  124. ARC_CC_LAST
  125. };
  126. /*
  127. * A few notes:
  128. *
  129. * - check_jmp_*() are prerequisites before calling the gen_jmp_*().
  130. * They return "true" if the jump is possible and "false" otherwise.
  131. *
  132. * - The notion of "*_off" is to emphasize that these parameters are
  133. * merely offsets in the JIT stream and not absolute addresses. One
  134. * can look at them as addresses if the JIT code would start from
  135. * address 0x0000_0000. Nonetheless, since the buffer address for the
  136. * JIT is on a word-aligned address, this works and actually makes
  137. * things simpler (offsets are in the range of u32 which is more than
  138. * enough).
  139. */
  140. bool check_jmp_32(u32 curr_off, u32 targ_off, u8 cond);
  141. bool check_jmp_64(u32 curr_off, u32 targ_off, u8 cond);
  142. u8 gen_jmp_32(u8 *buf, u8 rd, u8 rs, u8 cond, u32 c_off, u32 t_off);
  143. u8 gen_jmp_64(u8 *buf, u8 rd, u8 rs, u8 cond, u32 c_off, u32 t_off);
  144. /***** Miscellaneous *****/
  145. u8 gen_func_call(u8 *buf, ARC_ADDR func_addr, bool external_func);
  146. u8 arc_to_bpf_return(u8 *buf);
  147. /*
  148. * - Perform byte swaps on "rd" based on the "size".
  149. * - If "force" is set, do it unconditionally. Otherwise, consider the
  150. * desired "endian"ness and the host endianness.
  151. * - For data "size"s up to 32 bits, perform a zero-extension if asked
  152. * by the "do_zext" boolean.
  153. */
  154. u8 gen_swap(u8 *buf, u8 rd, u8 size, u8 endian, bool force, bool do_zext);
  155. #endif /* _ARC_BPF_JIT_H */