bpf_jit.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * BPF JIT compiler for LoongArch
  4. *
  5. * Copyright (C) 2022 Loongson Technology Corporation Limited
  6. */
  7. #include <linux/bpf.h>
  8. #include <linux/filter.h>
  9. #include <asm/cacheflush.h>
  10. #include <asm/inst.h>
  11. struct jit_ctx {
  12. const struct bpf_prog *prog;
  13. unsigned int idx;
  14. unsigned int flags;
  15. unsigned int epilogue_offset;
  16. u32 *offset;
  17. union loongarch_instruction *image;
  18. u32 stack_size;
  19. };
  20. struct jit_data {
  21. struct bpf_binary_header *header;
  22. u8 *image;
  23. struct jit_ctx ctx;
  24. };
  25. #define emit_insn(ctx, func, ...) \
  26. do { \
  27. if (ctx->image != NULL) { \
  28. union loongarch_instruction *insn = &ctx->image[ctx->idx]; \
  29. emit_##func(insn, ##__VA_ARGS__); \
  30. } \
  31. ctx->idx++; \
  32. } while (0)
  33. #define is_signed_imm12(val) signed_imm_check(val, 12)
  34. #define is_signed_imm14(val) signed_imm_check(val, 14)
  35. #define is_signed_imm16(val) signed_imm_check(val, 16)
  36. #define is_signed_imm26(val) signed_imm_check(val, 26)
  37. #define is_signed_imm32(val) signed_imm_check(val, 32)
  38. #define is_signed_imm52(val) signed_imm_check(val, 52)
  39. #define is_unsigned_imm12(val) unsigned_imm_check(val, 12)
  40. static inline int bpf2la_offset(int bpf_insn, int off, const struct jit_ctx *ctx)
  41. {
  42. /* BPF JMP offset is relative to the next instruction */
  43. bpf_insn++;
  44. /*
  45. * Whereas LoongArch branch instructions encode the offset
  46. * from the branch itself, so we must subtract 1 from the
  47. * instruction offset.
  48. */
  49. return (ctx->offset[bpf_insn + off] - (ctx->offset[bpf_insn] - 1));
  50. }
  51. static inline int epilogue_offset(const struct jit_ctx *ctx)
  52. {
  53. int from = ctx->idx;
  54. int to = ctx->epilogue_offset;
  55. return (to - from);
  56. }
  57. /* Zero-extend 32 bits into 64 bits */
  58. static inline void emit_zext_32(struct jit_ctx *ctx, enum loongarch_gpr reg, bool is32)
  59. {
  60. if (!is32)
  61. return;
  62. emit_insn(ctx, lu32id, reg, 0);
  63. }
  64. /* Signed-extend 32 bits into 64 bits */
  65. static inline void emit_sext_32(struct jit_ctx *ctx, enum loongarch_gpr reg, bool is32)
  66. {
  67. if (!is32)
  68. return;
  69. emit_insn(ctx, addiw, reg, reg, 0);
  70. }
  71. static inline void move_addr(struct jit_ctx *ctx, enum loongarch_gpr rd, u64 addr)
  72. {
  73. u64 imm_11_0, imm_31_12, imm_51_32, imm_63_52;
  74. /* lu12iw rd, imm_31_12 */
  75. imm_31_12 = (addr >> 12) & 0xfffff;
  76. emit_insn(ctx, lu12iw, rd, imm_31_12);
  77. /* ori rd, rd, imm_11_0 */
  78. imm_11_0 = addr & 0xfff;
  79. emit_insn(ctx, ori, rd, rd, imm_11_0);
  80. /* lu32id rd, imm_51_32 */
  81. imm_51_32 = (addr >> 32) & 0xfffff;
  82. emit_insn(ctx, lu32id, rd, imm_51_32);
  83. /* lu52id rd, rd, imm_63_52 */
  84. imm_63_52 = (addr >> 52) & 0xfff;
  85. emit_insn(ctx, lu52id, rd, rd, imm_63_52);
  86. }
  87. static inline void move_imm(struct jit_ctx *ctx, enum loongarch_gpr rd, long imm, bool is32)
  88. {
  89. long imm_11_0, imm_31_12, imm_51_32, imm_63_52, imm_51_0, imm_51_31;
  90. /* or rd, $zero, $zero */
  91. if (imm == 0) {
  92. emit_insn(ctx, or, rd, LOONGARCH_GPR_ZERO, LOONGARCH_GPR_ZERO);
  93. return;
  94. }
  95. /* addiw rd, $zero, imm_11_0 */
  96. if (is_signed_imm12(imm)) {
  97. emit_insn(ctx, addiw, rd, LOONGARCH_GPR_ZERO, imm);
  98. goto zext;
  99. }
  100. /* ori rd, $zero, imm_11_0 */
  101. if (is_unsigned_imm12(imm)) {
  102. emit_insn(ctx, ori, rd, LOONGARCH_GPR_ZERO, imm);
  103. goto zext;
  104. }
  105. /* lu52id rd, $zero, imm_63_52 */
  106. imm_63_52 = (imm >> 52) & 0xfff;
  107. imm_51_0 = imm & 0xfffffffffffff;
  108. if (imm_63_52 != 0 && imm_51_0 == 0) {
  109. emit_insn(ctx, lu52id, rd, LOONGARCH_GPR_ZERO, imm_63_52);
  110. return;
  111. }
  112. /* lu12iw rd, imm_31_12 */
  113. imm_31_12 = (imm >> 12) & 0xfffff;
  114. emit_insn(ctx, lu12iw, rd, imm_31_12);
  115. /* ori rd, rd, imm_11_0 */
  116. imm_11_0 = imm & 0xfff;
  117. if (imm_11_0 != 0)
  118. emit_insn(ctx, ori, rd, rd, imm_11_0);
  119. if (!is_signed_imm32(imm)) {
  120. if (imm_51_0 != 0) {
  121. /*
  122. * If bit[51:31] is all 0 or all 1,
  123. * it means bit[51:32] is sign extended by lu12iw,
  124. * no need to call lu32id to do a new filled operation.
  125. */
  126. imm_51_31 = (imm >> 31) & 0x1fffff;
  127. if (imm_51_31 != 0 && imm_51_31 != 0x1fffff) {
  128. /* lu32id rd, imm_51_32 */
  129. imm_51_32 = (imm >> 32) & 0xfffff;
  130. emit_insn(ctx, lu32id, rd, imm_51_32);
  131. }
  132. }
  133. /* lu52id rd, rd, imm_63_52 */
  134. if (!is_signed_imm52(imm))
  135. emit_insn(ctx, lu52id, rd, rd, imm_63_52);
  136. }
  137. zext:
  138. emit_zext_32(ctx, rd, is32);
  139. }
  140. static inline void move_reg(struct jit_ctx *ctx, enum loongarch_gpr rd,
  141. enum loongarch_gpr rj)
  142. {
  143. emit_insn(ctx, or, rd, rj, LOONGARCH_GPR_ZERO);
  144. }
  145. static inline int invert_jmp_cond(u8 cond)
  146. {
  147. switch (cond) {
  148. case BPF_JEQ:
  149. return BPF_JNE;
  150. case BPF_JNE:
  151. case BPF_JSET:
  152. return BPF_JEQ;
  153. case BPF_JGT:
  154. return BPF_JLE;
  155. case BPF_JGE:
  156. return BPF_JLT;
  157. case BPF_JLT:
  158. return BPF_JGE;
  159. case BPF_JLE:
  160. return BPF_JGT;
  161. case BPF_JSGT:
  162. return BPF_JSLE;
  163. case BPF_JSGE:
  164. return BPF_JSLT;
  165. case BPF_JSLT:
  166. return BPF_JSGE;
  167. case BPF_JSLE:
  168. return BPF_JSGT;
  169. }
  170. return -1;
  171. }
  172. static inline void cond_jmp_offset(struct jit_ctx *ctx, u8 cond, enum loongarch_gpr rj,
  173. enum loongarch_gpr rd, int jmp_offset)
  174. {
  175. switch (cond) {
  176. case BPF_JEQ:
  177. /* PC += jmp_offset if rj == rd */
  178. emit_insn(ctx, beq, rj, rd, jmp_offset);
  179. return;
  180. case BPF_JNE:
  181. case BPF_JSET:
  182. /* PC += jmp_offset if rj != rd */
  183. emit_insn(ctx, bne, rj, rd, jmp_offset);
  184. return;
  185. case BPF_JGT:
  186. /* PC += jmp_offset if rj > rd (unsigned) */
  187. emit_insn(ctx, bltu, rd, rj, jmp_offset);
  188. return;
  189. case BPF_JLT:
  190. /* PC += jmp_offset if rj < rd (unsigned) */
  191. emit_insn(ctx, bltu, rj, rd, jmp_offset);
  192. return;
  193. case BPF_JGE:
  194. /* PC += jmp_offset if rj >= rd (unsigned) */
  195. emit_insn(ctx, bgeu, rj, rd, jmp_offset);
  196. return;
  197. case BPF_JLE:
  198. /* PC += jmp_offset if rj <= rd (unsigned) */
  199. emit_insn(ctx, bgeu, rd, rj, jmp_offset);
  200. return;
  201. case BPF_JSGT:
  202. /* PC += jmp_offset if rj > rd (signed) */
  203. emit_insn(ctx, blt, rd, rj, jmp_offset);
  204. return;
  205. case BPF_JSLT:
  206. /* PC += jmp_offset if rj < rd (signed) */
  207. emit_insn(ctx, blt, rj, rd, jmp_offset);
  208. return;
  209. case BPF_JSGE:
  210. /* PC += jmp_offset if rj >= rd (signed) */
  211. emit_insn(ctx, bge, rj, rd, jmp_offset);
  212. return;
  213. case BPF_JSLE:
  214. /* PC += jmp_offset if rj <= rd (signed) */
  215. emit_insn(ctx, bge, rd, rj, jmp_offset);
  216. return;
  217. }
  218. }
  219. static inline void cond_jmp_offs26(struct jit_ctx *ctx, u8 cond, enum loongarch_gpr rj,
  220. enum loongarch_gpr rd, int jmp_offset)
  221. {
  222. cond = invert_jmp_cond(cond);
  223. cond_jmp_offset(ctx, cond, rj, rd, 2);
  224. emit_insn(ctx, b, jmp_offset);
  225. }
  226. static inline void uncond_jmp_offs26(struct jit_ctx *ctx, int jmp_offset)
  227. {
  228. emit_insn(ctx, b, jmp_offset);
  229. }
  230. static inline int emit_cond_jmp(struct jit_ctx *ctx, u8 cond, enum loongarch_gpr rj,
  231. enum loongarch_gpr rd, int jmp_offset)
  232. {
  233. /*
  234. * A large PC-relative jump offset may overflow the immediate field of
  235. * the native conditional branch instruction, triggering a conversion
  236. * to use an absolute jump instead, this jump sequence is particularly
  237. * nasty. For now, use cond_jmp_offs26() directly to keep it simple.
  238. * In the future, maybe we can add support for far branching, the branch
  239. * relaxation requires more than two passes to converge, the code seems
  240. * too complex to understand, not quite sure whether it is necessary and
  241. * worth the extra pain. Anyway, just leave it as it is to enhance code
  242. * readability now.
  243. */
  244. if (is_signed_imm26(jmp_offset)) {
  245. cond_jmp_offs26(ctx, cond, rj, rd, jmp_offset);
  246. return 0;
  247. }
  248. return -EINVAL;
  249. }
  250. static inline int emit_uncond_jmp(struct jit_ctx *ctx, int jmp_offset)
  251. {
  252. if (is_signed_imm26(jmp_offset)) {
  253. uncond_jmp_offs26(ctx, jmp_offset);
  254. return 0;
  255. }
  256. return -EINVAL;
  257. }
  258. static inline int emit_tailcall_jmp(struct jit_ctx *ctx, u8 cond, enum loongarch_gpr rj,
  259. enum loongarch_gpr rd, int jmp_offset)
  260. {
  261. if (is_signed_imm16(jmp_offset)) {
  262. cond_jmp_offset(ctx, cond, rj, rd, jmp_offset);
  263. return 0;
  264. }
  265. return -EINVAL;
  266. }