bpf_jit.h 7.6 KB

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