1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * BPF JIT compiler for LoongArch
5 * Copyright (C) 2022 Loongson Technology Corporation Limited
7 #include <linux/bitfield.h>
9 #include <linux/filter.h>
10 #include <asm/cacheflush.h>
14 const struct bpf_prog *prog;
17 unsigned int epilogue_offset;
20 union loongarch_instruction *image;
25 struct bpf_binary_header *header;
30 #define emit_insn(ctx, func, ...) \
32 if (ctx->image != NULL) { \
33 union loongarch_instruction *insn = &ctx->image[ctx->idx]; \
34 emit_##func(insn, ##__VA_ARGS__); \
39 #define is_signed_imm12(val) signed_imm_check(val, 12)
40 #define is_signed_imm14(val) signed_imm_check(val, 14)
41 #define is_signed_imm16(val) signed_imm_check(val, 16)
42 #define is_signed_imm26(val) signed_imm_check(val, 26)
43 #define is_signed_imm32(val) signed_imm_check(val, 32)
44 #define is_signed_imm52(val) signed_imm_check(val, 52)
45 #define is_unsigned_imm12(val) unsigned_imm_check(val, 12)
47 static inline int bpf2la_offset(int bpf_insn, int off, const struct jit_ctx *ctx)
49 /* BPF JMP offset is relative to the next instruction */
52 * Whereas LoongArch branch instructions encode the offset
53 * from the branch itself, so we must subtract 1 from the
56 return (ctx->offset[bpf_insn + off] - (ctx->offset[bpf_insn] - 1));
59 static inline int epilogue_offset(const struct jit_ctx *ctx)
62 int to = ctx->epilogue_offset;
67 /* Zero-extend 32 bits into 64 bits */
68 static inline void emit_zext_32(struct jit_ctx *ctx, enum loongarch_gpr reg, bool is32)
73 emit_insn(ctx, lu32id, reg, 0);
76 /* Signed-extend 32 bits into 64 bits */
77 static inline void emit_sext_32(struct jit_ctx *ctx, enum loongarch_gpr reg, bool is32)
82 emit_insn(ctx, addiw, reg, reg, 0);
85 static inline void move_addr(struct jit_ctx *ctx, enum loongarch_gpr rd, u64 addr)
87 u64 imm_11_0, imm_31_12, imm_51_32, imm_63_52;
89 /* lu12iw rd, imm_31_12 */
90 imm_31_12 = (addr >> 12) & 0xfffff;
91 emit_insn(ctx, lu12iw, rd, imm_31_12);
93 /* ori rd, rd, imm_11_0 */
94 imm_11_0 = addr & 0xfff;
95 emit_insn(ctx, ori, rd, rd, imm_11_0);
97 /* lu32id rd, imm_51_32 */
98 imm_51_32 = (addr >> 32) & 0xfffff;
99 emit_insn(ctx, lu32id, rd, imm_51_32);
101 /* lu52id rd, rd, imm_63_52 */
102 imm_63_52 = (addr >> 52) & 0xfff;
103 emit_insn(ctx, lu52id, rd, rd, imm_63_52);
106 static inline void move_imm(struct jit_ctx *ctx, enum loongarch_gpr rd, long imm, bool is32)
108 long imm_11_0, imm_31_12, imm_51_32, imm_63_52, imm_51_0, imm_51_31;
110 /* or rd, $zero, $zero */
112 emit_insn(ctx, or, rd, LOONGARCH_GPR_ZERO, LOONGARCH_GPR_ZERO);
116 /* addiw rd, $zero, imm_11_0 */
117 if (is_signed_imm12(imm)) {
118 emit_insn(ctx, addiw, rd, LOONGARCH_GPR_ZERO, imm);
122 /* ori rd, $zero, imm_11_0 */
123 if (is_unsigned_imm12(imm)) {
124 emit_insn(ctx, ori, rd, LOONGARCH_GPR_ZERO, imm);
128 /* lu52id rd, $zero, imm_63_52 */
129 imm_63_52 = (imm >> 52) & 0xfff;
130 imm_51_0 = imm & 0xfffffffffffff;
131 if (imm_63_52 != 0 && imm_51_0 == 0) {
132 emit_insn(ctx, lu52id, rd, LOONGARCH_GPR_ZERO, imm_63_52);
136 /* lu12iw rd, imm_31_12 */
137 imm_31_12 = (imm >> 12) & 0xfffff;
138 emit_insn(ctx, lu12iw, rd, imm_31_12);
140 /* ori rd, rd, imm_11_0 */
141 imm_11_0 = imm & 0xfff;
143 emit_insn(ctx, ori, rd, rd, imm_11_0);
145 if (!is_signed_imm32(imm)) {
148 * If bit[51:31] is all 0 or all 1,
149 * it means bit[51:32] is sign extended by lu12iw,
150 * no need to call lu32id to do a new filled operation.
152 imm_51_31 = (imm >> 31) & 0x1fffff;
153 if (imm_51_31 != 0 || imm_51_31 != 0x1fffff) {
154 /* lu32id rd, imm_51_32 */
155 imm_51_32 = (imm >> 32) & 0xfffff;
156 emit_insn(ctx, lu32id, rd, imm_51_32);
160 /* lu52id rd, rd, imm_63_52 */
161 if (!is_signed_imm52(imm))
162 emit_insn(ctx, lu52id, rd, rd, imm_63_52);
166 emit_zext_32(ctx, rd, is32);
169 static inline void move_reg(struct jit_ctx *ctx, enum loongarch_gpr rd,
170 enum loongarch_gpr rj)
172 emit_insn(ctx, or, rd, rj, LOONGARCH_GPR_ZERO);
175 static inline int invert_jmp_cond(u8 cond)
203 static inline void cond_jmp_offset(struct jit_ctx *ctx, u8 cond, enum loongarch_gpr rj,
204 enum loongarch_gpr rd, int jmp_offset)
208 /* PC += jmp_offset if rj == rd */
209 emit_insn(ctx, beq, rj, rd, jmp_offset);
213 /* PC += jmp_offset if rj != rd */
214 emit_insn(ctx, bne, rj, rd, jmp_offset);
217 /* PC += jmp_offset if rj > rd (unsigned) */
218 emit_insn(ctx, bltu, rd, rj, jmp_offset);
221 /* PC += jmp_offset if rj < rd (unsigned) */
222 emit_insn(ctx, bltu, rj, rd, jmp_offset);
225 /* PC += jmp_offset if rj >= rd (unsigned) */
226 emit_insn(ctx, bgeu, rj, rd, jmp_offset);
229 /* PC += jmp_offset if rj <= rd (unsigned) */
230 emit_insn(ctx, bgeu, rd, rj, jmp_offset);
233 /* PC += jmp_offset if rj > rd (signed) */
234 emit_insn(ctx, blt, rd, rj, jmp_offset);
237 /* PC += jmp_offset if rj < rd (signed) */
238 emit_insn(ctx, blt, rj, rd, jmp_offset);
241 /* PC += jmp_offset if rj >= rd (signed) */
242 emit_insn(ctx, bge, rj, rd, jmp_offset);
245 /* PC += jmp_offset if rj <= rd (signed) */
246 emit_insn(ctx, bge, rd, rj, jmp_offset);
251 static inline void cond_jmp_offs26(struct jit_ctx *ctx, u8 cond, enum loongarch_gpr rj,
252 enum loongarch_gpr rd, int jmp_offset)
254 cond = invert_jmp_cond(cond);
255 cond_jmp_offset(ctx, cond, rj, rd, 2);
256 emit_insn(ctx, b, jmp_offset);
259 static inline void uncond_jmp_offs26(struct jit_ctx *ctx, int jmp_offset)
261 emit_insn(ctx, b, jmp_offset);
264 static inline int emit_cond_jmp(struct jit_ctx *ctx, u8 cond, enum loongarch_gpr rj,
265 enum loongarch_gpr rd, int jmp_offset)
268 * A large PC-relative jump offset may overflow the immediate field of
269 * the native conditional branch instruction, triggering a conversion
270 * to use an absolute jump instead, this jump sequence is particularly
271 * nasty. For now, use cond_jmp_offs26() directly to keep it simple.
272 * In the future, maybe we can add support for far branching, the branch
273 * relaxation requires more than two passes to converge, the code seems
274 * too complex to understand, not quite sure whether it is necessary and
275 * worth the extra pain. Anyway, just leave it as it is to enhance code
278 if (is_signed_imm26(jmp_offset)) {
279 cond_jmp_offs26(ctx, cond, rj, rd, jmp_offset);
286 static inline int emit_uncond_jmp(struct jit_ctx *ctx, int jmp_offset)
288 if (is_signed_imm26(jmp_offset)) {
289 uncond_jmp_offs26(ctx, jmp_offset);
296 static inline int emit_tailcall_jmp(struct jit_ctx *ctx, u8 cond, enum loongarch_gpr rj,
297 enum loongarch_gpr rd, int jmp_offset)
299 if (is_signed_imm16(jmp_offset)) {
300 cond_jmp_offset(ctx, cond, rj, rd, jmp_offset);