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