bpf_jit.h 8.5 KB

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