blob: 44b925005dd37c78d8c85a814bef8ed69c927522 [file] [log] [blame]
Markos Chandrasc6610de2014-04-08 12:47:14 +01001/*
2 * Just-In-Time compiler for BPF filters on MIPS
3 *
4 * Copyright (c) 2014 Imagination Technologies Ltd.
5 * Author: Markos Chandras <markos.chandras@imgtec.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; version 2 of the License.
10 */
11
12#include <linux/bitops.h>
13#include <linux/compiler.h>
14#include <linux/errno.h>
15#include <linux/filter.h>
16#include <linux/if_vlan.h>
Markos Chandrasc6610de2014-04-08 12:47:14 +010017#include <linux/moduleloader.h>
18#include <linux/netdevice.h>
19#include <linux/string.h>
20#include <linux/slab.h>
21#include <linux/types.h>
Markos Chandras5e0c5612015-06-04 11:56:12 +010022#include <asm/asm.h>
Markos Chandrasc6610de2014-04-08 12:47:14 +010023#include <asm/bitops.h>
24#include <asm/cacheflush.h>
25#include <asm/cpu-features.h>
26#include <asm/uasm.h>
27
28#include "bpf_jit.h"
29
30/* ABI
Markos Chandras266a88e2015-06-04 11:56:16 +010031 * r_skb_hl SKB header length
32 * r_data SKB data pointer
33 * r_off Offset
Markos Chandrasbeaf70b2015-06-04 11:56:15 +010034 * r_A BPF register A
35 * r_X BPF register X
36 * r_skb *skb
37 * r_M *scratch memory
Markos Chandras266a88e2015-06-04 11:56:16 +010038 * r_skb_len SKB length
Markos Chandrasc6610de2014-04-08 12:47:14 +010039 *
40 * On entry (*bpf_func)(*skb, *filter)
41 * a0 = MIPS_R_A0 = skb;
42 * a1 = MIPS_R_A1 = filter;
43 *
44 * Stack
45 * ...
46 * M[15]
47 * M[14]
48 * M[13]
49 * ...
50 * M[0] <-- r_M
51 * saved reg k-1
52 * saved reg k-2
53 * ...
54 * saved reg 0 <-- r_sp
55 * <no argument area>
56 *
57 * Packet layout
58 *
59 * <--------------------- len ------------------------>
60 * <--skb-len(r_skb_hl)-->< ----- skb->data_len ------>
61 * ----------------------------------------------------
62 * | skb->data |
63 * ----------------------------------------------------
64 */
65
Markos Chandrasc6610de2014-04-08 12:47:14 +010066#define ptr typeof(unsigned long)
67
Markos Chandrasc6610de2014-04-08 12:47:14 +010068#define SCRATCH_OFF(k) (4 * (k))
69
70/* JIT flags */
71#define SEEN_CALL (1 << BPF_MEMWORDS)
72#define SEEN_SREG_SFT (BPF_MEMWORDS + 1)
73#define SEEN_SREG_BASE (1 << SEEN_SREG_SFT)
74#define SEEN_SREG(x) (SEEN_SREG_BASE << (x))
Markos Chandrasc6610de2014-04-08 12:47:14 +010075#define SEEN_OFF SEEN_SREG(2)
76#define SEEN_A SEEN_SREG(3)
77#define SEEN_X SEEN_SREG(4)
78#define SEEN_SKB SEEN_SREG(5)
79#define SEEN_MEM SEEN_SREG(6)
Markos Chandras266a88e2015-06-04 11:56:16 +010080/* SEEN_SK_DATA also implies skb_hl an skb_len */
81#define SEEN_SKB_DATA (SEEN_SREG(7) | SEEN_SREG(1) | SEEN_SREG(0))
Markos Chandrasc6610de2014-04-08 12:47:14 +010082
83/* Arguments used by JIT */
84#define ARGS_USED_BY_JIT 2 /* only applicable to 64-bit */
85
Markos Chandrasc6610de2014-04-08 12:47:14 +010086#define SBIT(x) (1 << (x)) /* Signed version of BIT() */
87
88/**
89 * struct jit_ctx - JIT context
90 * @skf: The sk_filter
91 * @prologue_bytes: Number of bytes for prologue
92 * @idx: Instruction index
93 * @flags: JIT flags
94 * @offsets: Instruction offsets
95 * @target: Memory location for the compiled filter
96 */
97struct jit_ctx {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -070098 const struct bpf_prog *skf;
Markos Chandrasc6610de2014-04-08 12:47:14 +010099 unsigned int prologue_bytes;
100 u32 idx;
101 u32 flags;
102 u32 *offsets;
103 u32 *target;
104};
105
106
107static inline int optimize_div(u32 *k)
108{
109 /* power of 2 divides can be implemented with right shift */
110 if (!(*k & (*k-1))) {
111 *k = ilog2(*k);
112 return 1;
113 }
114
115 return 0;
116}
117
Markos Chandras95782bf2014-06-25 09:37:21 +0100118static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx);
119
Markos Chandrasc6610de2014-04-08 12:47:14 +0100120/* Simply emit the instruction if the JIT memory space has been allocated */
121#define emit_instr(ctx, func, ...) \
122do { \
123 if ((ctx)->target != NULL) { \
124 u32 *p = &(ctx)->target[ctx->idx]; \
125 uasm_i_##func(&p, ##__VA_ARGS__); \
126 } \
127 (ctx)->idx++; \
128} while (0)
129
Markos Chandras39bcb792014-07-23 10:00:09 +0100130/*
131 * Similar to emit_instr but it must be used when we need to emit
132 * 32-bit or 64-bit instructions
133 */
134#define emit_long_instr(ctx, func, ...) \
135do { \
136 if ((ctx)->target != NULL) { \
137 u32 *p = &(ctx)->target[ctx->idx]; \
138 UASM_i_##func(&p, ##__VA_ARGS__); \
139 } \
140 (ctx)->idx++; \
141} while (0)
142
Markos Chandrasc6610de2014-04-08 12:47:14 +0100143/* Determine if immediate is within the 16-bit signed range */
144static inline bool is_range16(s32 imm)
145{
Markos Chandras10c4d612014-06-23 10:38:55 +0100146 return !(imm >= SBIT(15) || imm < -SBIT(15));
Markos Chandrasc6610de2014-04-08 12:47:14 +0100147}
148
149static inline void emit_addu(unsigned int dst, unsigned int src1,
150 unsigned int src2, struct jit_ctx *ctx)
151{
152 emit_instr(ctx, addu, dst, src1, src2);
153}
154
155static inline void emit_nop(struct jit_ctx *ctx)
156{
157 emit_instr(ctx, nop);
158}
159
160/* Load a u32 immediate to a register */
161static inline void emit_load_imm(unsigned int dst, u32 imm, struct jit_ctx *ctx)
162{
163 if (ctx->target != NULL) {
164 /* addiu can only handle s16 */
Markos Chandras10c4d612014-06-23 10:38:55 +0100165 if (!is_range16(imm)) {
Markos Chandrasc6610de2014-04-08 12:47:14 +0100166 u32 *p = &ctx->target[ctx->idx];
167 uasm_i_lui(&p, r_tmp_imm, (s32)imm >> 16);
168 p = &ctx->target[ctx->idx + 1];
169 uasm_i_ori(&p, dst, r_tmp_imm, imm & 0xffff);
170 } else {
171 u32 *p = &ctx->target[ctx->idx];
172 uasm_i_addiu(&p, dst, r_zero, imm);
173 }
174 }
175 ctx->idx++;
176
Markos Chandras10c4d612014-06-23 10:38:55 +0100177 if (!is_range16(imm))
Markos Chandrasc6610de2014-04-08 12:47:14 +0100178 ctx->idx++;
179}
180
181static inline void emit_or(unsigned int dst, unsigned int src1,
182 unsigned int src2, struct jit_ctx *ctx)
183{
184 emit_instr(ctx, or, dst, src1, src2);
185}
186
187static inline void emit_ori(unsigned int dst, unsigned src, u32 imm,
188 struct jit_ctx *ctx)
189{
190 if (imm >= BIT(16)) {
191 emit_load_imm(r_tmp, imm, ctx);
192 emit_or(dst, src, r_tmp, ctx);
193 } else {
194 emit_instr(ctx, ori, dst, src, imm);
195 }
196}
197
Markos Chandrasc6610de2014-04-08 12:47:14 +0100198static inline void emit_daddiu(unsigned int dst, unsigned int src,
199 int imm, struct jit_ctx *ctx)
200{
201 /*
202 * Only used for stack, so the imm is relatively small
203 * and it fits in 15-bits
204 */
205 emit_instr(ctx, daddiu, dst, src, imm);
206}
207
208static inline void emit_addiu(unsigned int dst, unsigned int src,
209 u32 imm, struct jit_ctx *ctx)
210{
Markos Chandras10c4d612014-06-23 10:38:55 +0100211 if (!is_range16(imm)) {
Markos Chandrasc6610de2014-04-08 12:47:14 +0100212 emit_load_imm(r_tmp, imm, ctx);
213 emit_addu(dst, r_tmp, src, ctx);
214 } else {
215 emit_instr(ctx, addiu, dst, src, imm);
216 }
217}
218
219static inline void emit_and(unsigned int dst, unsigned int src1,
220 unsigned int src2, struct jit_ctx *ctx)
221{
222 emit_instr(ctx, and, dst, src1, src2);
223}
224
225static inline void emit_andi(unsigned int dst, unsigned int src,
226 u32 imm, struct jit_ctx *ctx)
227{
228 /* If imm does not fit in u16 then load it to register */
229 if (imm >= BIT(16)) {
230 emit_load_imm(r_tmp, imm, ctx);
231 emit_and(dst, src, r_tmp, ctx);
232 } else {
233 emit_instr(ctx, andi, dst, src, imm);
234 }
235}
236
237static inline void emit_xor(unsigned int dst, unsigned int src1,
238 unsigned int src2, struct jit_ctx *ctx)
239{
240 emit_instr(ctx, xor, dst, src1, src2);
241}
242
243static inline void emit_xori(ptr dst, ptr src, u32 imm, struct jit_ctx *ctx)
244{
245 /* If imm does not fit in u16 then load it to register */
246 if (imm >= BIT(16)) {
247 emit_load_imm(r_tmp, imm, ctx);
248 emit_xor(dst, src, r_tmp, ctx);
249 } else {
250 emit_instr(ctx, xori, dst, src, imm);
251 }
252}
253
254static inline void emit_stack_offset(int offset, struct jit_ctx *ctx)
255{
Markos Chandras39bcb792014-07-23 10:00:09 +0100256 emit_long_instr(ctx, ADDIU, r_sp, r_sp, offset);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100257}
258
259static inline void emit_subu(unsigned int dst, unsigned int src1,
260 unsigned int src2, struct jit_ctx *ctx)
261{
262 emit_instr(ctx, subu, dst, src1, src2);
263}
264
265static inline void emit_neg(unsigned int reg, struct jit_ctx *ctx)
266{
267 emit_subu(reg, r_zero, reg, ctx);
268}
269
270static inline void emit_sllv(unsigned int dst, unsigned int src,
271 unsigned int sa, struct jit_ctx *ctx)
272{
273 emit_instr(ctx, sllv, dst, src, sa);
274}
275
276static inline void emit_sll(unsigned int dst, unsigned int src,
277 unsigned int sa, struct jit_ctx *ctx)
278{
279 /* sa is 5-bits long */
Markos Chandras95782bf2014-06-25 09:37:21 +0100280 if (sa >= BIT(5))
281 /* Shifting >= 32 results in zero */
282 emit_jit_reg_move(dst, r_zero, ctx);
283 else
284 emit_instr(ctx, sll, dst, src, sa);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100285}
286
287static inline void emit_srlv(unsigned int dst, unsigned int src,
288 unsigned int sa, struct jit_ctx *ctx)
289{
290 emit_instr(ctx, srlv, dst, src, sa);
291}
292
293static inline void emit_srl(unsigned int dst, unsigned int src,
294 unsigned int sa, struct jit_ctx *ctx)
295{
296 /* sa is 5-bits long */
Markos Chandras95782bf2014-06-25 09:37:21 +0100297 if (sa >= BIT(5))
298 /* Shifting >= 32 results in zero */
299 emit_jit_reg_move(dst, r_zero, ctx);
300 else
301 emit_instr(ctx, srl, dst, src, sa);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100302}
303
Markos Chandras55393ee2014-06-23 10:38:48 +0100304static inline void emit_slt(unsigned int dst, unsigned int src1,
305 unsigned int src2, struct jit_ctx *ctx)
306{
307 emit_instr(ctx, slt, dst, src1, src2);
308}
309
Markos Chandrasc6610de2014-04-08 12:47:14 +0100310static inline void emit_sltu(unsigned int dst, unsigned int src1,
311 unsigned int src2, struct jit_ctx *ctx)
312{
313 emit_instr(ctx, sltu, dst, src1, src2);
314}
315
316static inline void emit_sltiu(unsigned dst, unsigned int src,
317 unsigned int imm, struct jit_ctx *ctx)
318{
319 /* 16 bit immediate */
Markos Chandras10c4d612014-06-23 10:38:55 +0100320 if (!is_range16((s32)imm)) {
Markos Chandrasc6610de2014-04-08 12:47:14 +0100321 emit_load_imm(r_tmp, imm, ctx);
322 emit_sltu(dst, src, r_tmp, ctx);
323 } else {
324 emit_instr(ctx, sltiu, dst, src, imm);
325 }
326
327}
328
329/* Store register on the stack */
330static inline void emit_store_stack_reg(ptr reg, ptr base,
331 unsigned int offset,
332 struct jit_ctx *ctx)
333{
Markos Chandras39bcb792014-07-23 10:00:09 +0100334 emit_long_instr(ctx, SW, reg, offset, base);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100335}
336
337static inline void emit_store(ptr reg, ptr base, unsigned int offset,
338 struct jit_ctx *ctx)
339{
340 emit_instr(ctx, sw, reg, offset, base);
341}
342
343static inline void emit_load_stack_reg(ptr reg, ptr base,
344 unsigned int offset,
345 struct jit_ctx *ctx)
346{
Markos Chandras39bcb792014-07-23 10:00:09 +0100347 emit_long_instr(ctx, LW, reg, offset, base);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100348}
349
350static inline void emit_load(unsigned int reg, unsigned int base,
351 unsigned int offset, struct jit_ctx *ctx)
352{
353 emit_instr(ctx, lw, reg, offset, base);
354}
355
356static inline void emit_load_byte(unsigned int reg, unsigned int base,
357 unsigned int offset, struct jit_ctx *ctx)
358{
359 emit_instr(ctx, lb, reg, offset, base);
360}
361
362static inline void emit_half_load(unsigned int reg, unsigned int base,
363 unsigned int offset, struct jit_ctx *ctx)
364{
365 emit_instr(ctx, lh, reg, offset, base);
366}
367
David Daney4ad70152017-03-14 14:21:41 -0700368static inline void emit_half_load_unsigned(unsigned int reg, unsigned int base,
369 unsigned int offset, struct jit_ctx *ctx)
370{
371 emit_instr(ctx, lhu, reg, offset, base);
372}
373
Markos Chandrasc6610de2014-04-08 12:47:14 +0100374static inline void emit_mul(unsigned int dst, unsigned int src1,
375 unsigned int src2, struct jit_ctx *ctx)
376{
377 emit_instr(ctx, mul, dst, src1, src2);
378}
379
380static inline void emit_div(unsigned int dst, unsigned int src,
381 struct jit_ctx *ctx)
382{
383 if (ctx->target != NULL) {
384 u32 *p = &ctx->target[ctx->idx];
385 uasm_i_divu(&p, dst, src);
386 p = &ctx->target[ctx->idx + 1];
Markos Chandras35a8e162014-06-23 10:38:47 +0100387 uasm_i_mflo(&p, dst);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100388 }
389 ctx->idx += 2; /* 2 insts */
390}
391
392static inline void emit_mod(unsigned int dst, unsigned int src,
393 struct jit_ctx *ctx)
394{
395 if (ctx->target != NULL) {
396 u32 *p = &ctx->target[ctx->idx];
397 uasm_i_divu(&p, dst, src);
398 p = &ctx->target[ctx->idx + 1];
Denis Kirjanov2e464772014-12-01 12:57:02 +0300399 uasm_i_mfhi(&p, dst);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100400 }
401 ctx->idx += 2; /* 2 insts */
402}
403
404static inline void emit_dsll(unsigned int dst, unsigned int src,
405 unsigned int sa, struct jit_ctx *ctx)
406{
407 emit_instr(ctx, dsll, dst, src, sa);
408}
409
410static inline void emit_dsrl32(unsigned int dst, unsigned int src,
411 unsigned int sa, struct jit_ctx *ctx)
412{
413 emit_instr(ctx, dsrl32, dst, src, sa);
414}
415
416static inline void emit_wsbh(unsigned int dst, unsigned int src,
417 struct jit_ctx *ctx)
418{
419 emit_instr(ctx, wsbh, dst, src);
420}
421
Markos Chandrasb6a14a92014-06-25 09:39:38 +0100422/* load pointer to register */
423static inline void emit_load_ptr(unsigned int dst, unsigned int src,
424 int imm, struct jit_ctx *ctx)
425{
426 /* src contains the base addr of the 32/64-pointer */
Markos Chandras39bcb792014-07-23 10:00:09 +0100427 emit_long_instr(ctx, LW, dst, imm, src);
Markos Chandrasb6a14a92014-06-25 09:39:38 +0100428}
429
Markos Chandrasc6610de2014-04-08 12:47:14 +0100430/* load a function pointer to register */
431static inline void emit_load_func(unsigned int reg, ptr imm,
432 struct jit_ctx *ctx)
433{
Masahiro Yamada97f26452016-08-03 13:45:50 -0700434 if (IS_ENABLED(CONFIG_64BIT)) {
Markos Chandrasc6610de2014-04-08 12:47:14 +0100435 /* At this point imm is always 64-bit */
436 emit_load_imm(r_tmp, (u64)imm >> 32, ctx);
437 emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */
438 emit_ori(r_tmp, r_tmp_imm, (imm >> 16) & 0xffff, ctx);
439 emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */
440 emit_ori(reg, r_tmp_imm, imm & 0xffff, ctx);
441 } else {
442 emit_load_imm(reg, imm, ctx);
443 }
444}
445
446/* Move to real MIPS register */
447static inline void emit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
448{
Markos Chandras39bcb792014-07-23 10:00:09 +0100449 emit_long_instr(ctx, ADDU, dst, src, r_zero);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100450}
451
452/* Move to JIT (32-bit) register */
453static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
454{
455 emit_addu(dst, src, r_zero, ctx);
456}
457
458/* Compute the immediate value for PC-relative branches. */
459static inline u32 b_imm(unsigned int tgt, struct jit_ctx *ctx)
460{
461 if (ctx->target == NULL)
462 return 0;
463
464 /*
465 * We want a pc-relative branch. We only do forward branches
466 * so tgt is always after pc. tgt is the instruction offset
467 * we want to jump to.
468
469 * Branch on MIPS:
470 * I: target_offset <- sign_extend(offset)
471 * I+1: PC += target_offset (delay slot)
472 *
473 * ctx->idx currently points to the branch instruction
474 * but the offset is added to the delay slot so we need
475 * to subtract 4.
476 */
477 return ctx->offsets[tgt] -
478 (ctx->idx * 4 - ctx->prologue_bytes) - 4;
479}
480
481static inline void emit_bcond(int cond, unsigned int reg1, unsigned int reg2,
482 unsigned int imm, struct jit_ctx *ctx)
483{
484 if (ctx->target != NULL) {
485 u32 *p = &ctx->target[ctx->idx];
486
487 switch (cond) {
488 case MIPS_COND_EQ:
489 uasm_i_beq(&p, reg1, reg2, imm);
490 break;
491 case MIPS_COND_NE:
492 uasm_i_bne(&p, reg1, reg2, imm);
493 break;
494 case MIPS_COND_ALL:
495 uasm_i_b(&p, imm);
496 break;
497 default:
498 pr_warn("%s: Unhandled branch conditional: %d\n",
499 __func__, cond);
500 }
501 }
502 ctx->idx++;
503}
504
505static inline void emit_b(unsigned int imm, struct jit_ctx *ctx)
506{
507 emit_bcond(MIPS_COND_ALL, r_zero, r_zero, imm, ctx);
508}
509
510static inline void emit_jalr(unsigned int link, unsigned int reg,
511 struct jit_ctx *ctx)
512{
513 emit_instr(ctx, jalr, link, reg);
514}
515
516static inline void emit_jr(unsigned int reg, struct jit_ctx *ctx)
517{
518 emit_instr(ctx, jr, reg);
519}
520
521static inline u16 align_sp(unsigned int num)
522{
523 /* Double word alignment for 32-bit, quadword for 64-bit */
Masahiro Yamada97f26452016-08-03 13:45:50 -0700524 unsigned int align = IS_ENABLED(CONFIG_64BIT) ? 16 : 8;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100525 num = (num + (align - 1)) & -align;
526 return num;
527}
528
Markos Chandrasc6610de2014-04-08 12:47:14 +0100529static void save_bpf_jit_regs(struct jit_ctx *ctx, unsigned offset)
530{
531 int i = 0, real_off = 0;
532 u32 sflags, tmp_flags;
533
534 /* Adjust the stack pointer */
David Daney1ef09102017-03-14 14:21:43 -0700535 if (offset)
536 emit_stack_offset(-align_sp(offset), ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100537
Markos Chandrasc6610de2014-04-08 12:47:14 +0100538 tmp_flags = sflags = ctx->flags >> SEEN_SREG_SFT;
539 /* sflags is essentially a bitmap */
540 while (tmp_flags) {
541 if ((sflags >> i) & 0x1) {
542 emit_store_stack_reg(MIPS_R_S0 + i, r_sp, real_off,
543 ctx);
Markos Chandras5e0c5612015-06-04 11:56:12 +0100544 real_off += SZREG;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100545 }
546 i++;
547 tmp_flags >>= 1;
548 }
549
550 /* save return address */
551 if (ctx->flags & SEEN_CALL) {
552 emit_store_stack_reg(r_ra, r_sp, real_off, ctx);
Markos Chandras5e0c5612015-06-04 11:56:12 +0100553 real_off += SZREG;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100554 }
555
556 /* Setup r_M leaving the alignment gap if necessary */
557 if (ctx->flags & SEEN_MEM) {
Markos Chandras5e0c5612015-06-04 11:56:12 +0100558 if (real_off % (SZREG * 2))
559 real_off += SZREG;
Markos Chandras39bcb792014-07-23 10:00:09 +0100560 emit_long_instr(ctx, ADDIU, r_M, r_sp, real_off);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100561 }
562}
563
564static void restore_bpf_jit_regs(struct jit_ctx *ctx,
565 unsigned int offset)
566{
567 int i, real_off = 0;
568 u32 sflags, tmp_flags;
569
Markos Chandrasc6610de2014-04-08 12:47:14 +0100570 tmp_flags = sflags = ctx->flags >> SEEN_SREG_SFT;
571 /* sflags is a bitmap */
572 i = 0;
573 while (tmp_flags) {
574 if ((sflags >> i) & 0x1) {
575 emit_load_stack_reg(MIPS_R_S0 + i, r_sp, real_off,
576 ctx);
Markos Chandras5e0c5612015-06-04 11:56:12 +0100577 real_off += SZREG;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100578 }
579 i++;
580 tmp_flags >>= 1;
581 }
582
583 /* restore return address */
584 if (ctx->flags & SEEN_CALL)
585 emit_load_stack_reg(r_ra, r_sp, real_off, ctx);
586
587 /* Restore the sp and discard the scrach memory */
David Daney1ef09102017-03-14 14:21:43 -0700588 if (offset)
589 emit_stack_offset(align_sp(offset), ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100590}
591
592static unsigned int get_stack_depth(struct jit_ctx *ctx)
593{
594 int sp_off = 0;
595
596
597 /* How may s* regs do we need to preserved? */
Markos Chandras5e0c5612015-06-04 11:56:12 +0100598 sp_off += hweight32(ctx->flags >> SEEN_SREG_SFT) * SZREG;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100599
600 if (ctx->flags & SEEN_MEM)
601 sp_off += 4 * BPF_MEMWORDS; /* BPF_MEMWORDS are 32-bit */
602
603 if (ctx->flags & SEEN_CALL)
Markos Chandras266a88e2015-06-04 11:56:16 +0100604 sp_off += SZREG; /* Space for our ra register */
Markos Chandrasc6610de2014-04-08 12:47:14 +0100605
Markos Chandras8833bc32015-06-04 11:56:13 +0100606 return sp_off;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100607}
608
609static void build_prologue(struct jit_ctx *ctx)
610{
Markos Chandrasc6610de2014-04-08 12:47:14 +0100611 int sp_off;
612
613 /* Calculate the total offset for the stack pointer */
614 sp_off = get_stack_depth(ctx);
615 save_bpf_jit_regs(ctx, sp_off);
616
617 if (ctx->flags & SEEN_SKB)
618 emit_reg_move(r_skb, MIPS_R_A0, ctx);
619
Markos Chandras266a88e2015-06-04 11:56:16 +0100620 if (ctx->flags & SEEN_SKB_DATA) {
621 /* Load packet length */
622 emit_load(r_skb_len, r_skb, offsetof(struct sk_buff, len),
623 ctx);
624 emit_load(r_tmp, r_skb, offsetof(struct sk_buff, data_len),
625 ctx);
626 /* Load the data pointer */
627 emit_load_ptr(r_skb_data, r_skb,
628 offsetof(struct sk_buff, data), ctx);
629 /* Load the header length */
630 emit_subu(r_skb_hl, r_skb_len, r_tmp, ctx);
631 }
632
Markos Chandrase5bb48b2014-06-23 10:38:56 +0100633 if (ctx->flags & SEEN_X)
Markos Chandrasc6610de2014-04-08 12:47:14 +0100634 emit_jit_reg_move(r_X, r_zero, ctx);
635
David Daney1ef09102017-03-14 14:21:43 -0700636 /*
637 * Do not leak kernel data to userspace, we only need to clear
638 * r_A if it is ever used. In fact if it is never used, we
639 * will not save/restore it, so clearing it in this case would
640 * corrupt the state of the caller.
641 */
642 if (bpf_needs_clear_a(&ctx->skf->insns[0]) &&
643 (ctx->flags & SEEN_A))
Markos Chandrasc6610de2014-04-08 12:47:14 +0100644 emit_jit_reg_move(r_A, r_zero, ctx);
645}
646
647static void build_epilogue(struct jit_ctx *ctx)
648{
649 unsigned int sp_off;
650
651 /* Calculate the total offset for the stack pointer */
652
653 sp_off = get_stack_depth(ctx);
654 restore_bpf_jit_regs(ctx, sp_off);
655
656 /* Return */
657 emit_jr(r_ra, ctx);
658 emit_nop(ctx);
659}
660
Markos Chandras266a88e2015-06-04 11:56:16 +0100661#define CHOOSE_LOAD_FUNC(K, func) \
662 ((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative : func) : \
663 func##_positive)
Markos Chandrasc6610de2014-04-08 12:47:14 +0100664
Markos Chandrasc6610de2014-04-08 12:47:14 +0100665static int build_body(struct jit_ctx *ctx)
666{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700667 const struct bpf_prog *prog = ctx->skf;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100668 const struct sock_filter *inst;
Markos Chandras266a88e2015-06-04 11:56:16 +0100669 unsigned int i, off, condt;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100670 u32 k, b_off __maybe_unused;
Markos Chandras266a88e2015-06-04 11:56:16 +0100671 u8 (*sk_load_func)(unsigned long *skb, int offset);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100672
673 for (i = 0; i < prog->len; i++) {
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200674 u16 code;
675
Markos Chandrasc6610de2014-04-08 12:47:14 +0100676 inst = &(prog->insns[i]);
677 pr_debug("%s: code->0x%02x, jt->0x%x, jf->0x%x, k->0x%x\n",
678 __func__, inst->code, inst->jt, inst->jf, inst->k);
679 k = inst->k;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200680 code = bpf_anc_helper(inst);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100681
682 if (ctx->target == NULL)
683 ctx->offsets[i] = ctx->idx * 4;
684
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200685 switch (code) {
686 case BPF_LD | BPF_IMM:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100687 /* A <- k ==> li r_A, k */
688 ctx->flags |= SEEN_A;
689 emit_load_imm(r_A, k, ctx);
690 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200691 case BPF_LD | BPF_W | BPF_LEN:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100692 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
693 /* A <- len ==> lw r_A, offset(skb) */
694 ctx->flags |= SEEN_SKB | SEEN_A;
695 off = offsetof(struct sk_buff, len);
696 emit_load(r_A, r_skb, off, ctx);
697 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200698 case BPF_LD | BPF_MEM:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100699 /* A <- M[k] ==> lw r_A, offset(M) */
700 ctx->flags |= SEEN_MEM | SEEN_A;
701 emit_load(r_A, r_M, SCRATCH_OFF(k), ctx);
702 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200703 case BPF_LD | BPF_W | BPF_ABS:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100704 /* A <- P[k:4] */
Markos Chandras266a88e2015-06-04 11:56:16 +0100705 sk_load_func = CHOOSE_LOAD_FUNC(k, sk_load_word);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100706 goto load;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200707 case BPF_LD | BPF_H | BPF_ABS:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100708 /* A <- P[k:2] */
Markos Chandras266a88e2015-06-04 11:56:16 +0100709 sk_load_func = CHOOSE_LOAD_FUNC(k, sk_load_half);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100710 goto load;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200711 case BPF_LD | BPF_B | BPF_ABS:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100712 /* A <- P[k:1] */
Markos Chandras266a88e2015-06-04 11:56:16 +0100713 sk_load_func = CHOOSE_LOAD_FUNC(k, sk_load_byte);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100714load:
715 emit_load_imm(r_off, k, ctx);
716load_common:
Markos Chandrasad152bd2015-06-04 11:56:11 +0100717 ctx->flags |= SEEN_CALL | SEEN_OFF |
Markos Chandras266a88e2015-06-04 11:56:16 +0100718 SEEN_SKB | SEEN_A | SEEN_SKB_DATA;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100719
Markos Chandras266a88e2015-06-04 11:56:16 +0100720 emit_load_func(r_s0, (ptr)sk_load_func, ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100721 emit_reg_move(MIPS_R_A0, r_skb, ctx);
722 emit_jalr(MIPS_R_RA, r_s0, ctx);
723 /* Load second argument to delay slot */
724 emit_reg_move(MIPS_R_A1, r_off, ctx);
725 /* Check the error value */
Markos Chandras266a88e2015-06-04 11:56:16 +0100726 emit_bcond(MIPS_COND_EQ, r_ret, 0, b_imm(i + 1, ctx),
727 ctx);
728 /* Load return register on DS for failures */
729 emit_reg_move(r_ret, r_zero, ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100730 /* Return with error */
731 emit_b(b_imm(prog->len, ctx), ctx);
Markos Chandras266a88e2015-06-04 11:56:16 +0100732 emit_nop(ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100733 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200734 case BPF_LD | BPF_W | BPF_IND:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100735 /* A <- P[X + k:4] */
Markos Chandras266a88e2015-06-04 11:56:16 +0100736 sk_load_func = sk_load_word;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100737 goto load_ind;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200738 case BPF_LD | BPF_H | BPF_IND:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100739 /* A <- P[X + k:2] */
Markos Chandras266a88e2015-06-04 11:56:16 +0100740 sk_load_func = sk_load_half;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100741 goto load_ind;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200742 case BPF_LD | BPF_B | BPF_IND:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100743 /* A <- P[X + k:1] */
Markos Chandras266a88e2015-06-04 11:56:16 +0100744 sk_load_func = sk_load_byte;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100745load_ind:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100746 ctx->flags |= SEEN_OFF | SEEN_X;
747 emit_addiu(r_off, r_X, k, ctx);
748 goto load_common;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200749 case BPF_LDX | BPF_IMM:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100750 /* X <- k */
751 ctx->flags |= SEEN_X;
752 emit_load_imm(r_X, k, ctx);
753 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200754 case BPF_LDX | BPF_MEM:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100755 /* X <- M[k] */
756 ctx->flags |= SEEN_X | SEEN_MEM;
757 emit_load(r_X, r_M, SCRATCH_OFF(k), ctx);
758 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200759 case BPF_LDX | BPF_W | BPF_LEN:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100760 /* X <- len */
761 ctx->flags |= SEEN_X | SEEN_SKB;
762 off = offsetof(struct sk_buff, len);
763 emit_load(r_X, r_skb, off, ctx);
764 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200765 case BPF_LDX | BPF_B | BPF_MSH:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100766 /* X <- 4 * (P[k:1] & 0xf) */
Markos Chandrasad152bd2015-06-04 11:56:11 +0100767 ctx->flags |= SEEN_X | SEEN_CALL | SEEN_SKB;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100768 /* Load offset to a1 */
Markos Chandras266a88e2015-06-04 11:56:16 +0100769 emit_load_func(r_s0, (ptr)sk_load_byte, ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100770 /*
771 * This may emit two instructions so it may not fit
772 * in the delay slot. So use a0 in the delay slot.
773 */
774 emit_load_imm(MIPS_R_A1, k, ctx);
775 emit_jalr(MIPS_R_RA, r_s0, ctx);
776 emit_reg_move(MIPS_R_A0, r_skb, ctx); /* delay slot */
777 /* Check the error value */
Markos Chandras266a88e2015-06-04 11:56:16 +0100778 emit_bcond(MIPS_COND_NE, r_ret, 0,
779 b_imm(prog->len, ctx), ctx);
780 emit_reg_move(r_ret, r_zero, ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100781 /* We are good */
782 /* X <- P[1:K] & 0xf */
Markos Chandras266a88e2015-06-04 11:56:16 +0100783 emit_andi(r_X, r_A, 0xf, ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100784 /* X << 2 */
785 emit_b(b_imm(i + 1, ctx), ctx);
786 emit_sll(r_X, r_X, 2, ctx); /* delay slot */
Markos Chandrasc6610de2014-04-08 12:47:14 +0100787 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200788 case BPF_ST:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100789 /* M[k] <- A */
790 ctx->flags |= SEEN_MEM | SEEN_A;
791 emit_store(r_A, r_M, SCRATCH_OFF(k), ctx);
792 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200793 case BPF_STX:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100794 /* M[k] <- X */
795 ctx->flags |= SEEN_MEM | SEEN_X;
796 emit_store(r_X, r_M, SCRATCH_OFF(k), ctx);
797 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200798 case BPF_ALU | BPF_ADD | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100799 /* A += K */
800 ctx->flags |= SEEN_A;
801 emit_addiu(r_A, r_A, k, ctx);
802 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200803 case BPF_ALU | BPF_ADD | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100804 /* A += X */
805 ctx->flags |= SEEN_A | SEEN_X;
806 emit_addu(r_A, r_A, r_X, ctx);
807 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200808 case BPF_ALU | BPF_SUB | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100809 /* A -= K */
810 ctx->flags |= SEEN_A;
811 emit_addiu(r_A, r_A, -k, ctx);
812 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200813 case BPF_ALU | BPF_SUB | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100814 /* A -= X */
815 ctx->flags |= SEEN_A | SEEN_X;
816 emit_subu(r_A, r_A, r_X, ctx);
817 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200818 case BPF_ALU | BPF_MUL | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100819 /* A *= K */
820 /* Load K to scratch register before MUL */
Markos Chandrasad152bd2015-06-04 11:56:11 +0100821 ctx->flags |= SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100822 emit_load_imm(r_s0, k, ctx);
823 emit_mul(r_A, r_A, r_s0, ctx);
824 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200825 case BPF_ALU | BPF_MUL | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100826 /* A *= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +0100827 ctx->flags |= SEEN_A | SEEN_X;
828 emit_mul(r_A, r_A, r_X, ctx);
829 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200830 case BPF_ALU | BPF_DIV | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100831 /* A /= k */
832 if (k == 1)
833 break;
834 if (optimize_div(&k)) {
835 ctx->flags |= SEEN_A;
836 emit_srl(r_A, r_A, k, ctx);
837 break;
838 }
Markos Chandrasad152bd2015-06-04 11:56:11 +0100839 ctx->flags |= SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100840 emit_load_imm(r_s0, k, ctx);
841 emit_div(r_A, r_s0, ctx);
842 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200843 case BPF_ALU | BPF_MOD | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100844 /* A %= k */
Denis Kirjanov2e464772014-12-01 12:57:02 +0300845 if (k == 1) {
Markos Chandrasc6610de2014-04-08 12:47:14 +0100846 ctx->flags |= SEEN_A;
847 emit_jit_reg_move(r_A, r_zero, ctx);
848 } else {
Markos Chandrasad152bd2015-06-04 11:56:11 +0100849 ctx->flags |= SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100850 emit_load_imm(r_s0, k, ctx);
851 emit_mod(r_A, r_s0, ctx);
852 }
853 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200854 case BPF_ALU | BPF_DIV | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100855 /* A /= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +0100856 ctx->flags |= SEEN_X | SEEN_A;
857 /* Check if r_X is zero */
858 emit_bcond(MIPS_COND_EQ, r_X, r_zero,
859 b_imm(prog->len, ctx), ctx);
Markos Chandras266a88e2015-06-04 11:56:16 +0100860 emit_load_imm(r_ret, 0, ctx); /* delay slot */
Markos Chandrasc6610de2014-04-08 12:47:14 +0100861 emit_div(r_A, r_X, ctx);
862 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200863 case BPF_ALU | BPF_MOD | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100864 /* A %= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +0100865 ctx->flags |= SEEN_X | SEEN_A;
866 /* Check if r_X is zero */
867 emit_bcond(MIPS_COND_EQ, r_X, r_zero,
868 b_imm(prog->len, ctx), ctx);
Markos Chandras266a88e2015-06-04 11:56:16 +0100869 emit_load_imm(r_ret, 0, ctx); /* delay slot */
Markos Chandrasc6610de2014-04-08 12:47:14 +0100870 emit_mod(r_A, r_X, ctx);
871 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200872 case BPF_ALU | BPF_OR | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100873 /* A |= K */
874 ctx->flags |= SEEN_A;
875 emit_ori(r_A, r_A, k, ctx);
876 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200877 case BPF_ALU | BPF_OR | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100878 /* A |= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +0100879 ctx->flags |= SEEN_A;
880 emit_ori(r_A, r_A, r_X, ctx);
881 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200882 case BPF_ALU | BPF_XOR | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100883 /* A ^= k */
884 ctx->flags |= SEEN_A;
885 emit_xori(r_A, r_A, k, ctx);
886 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200887 case BPF_ANC | SKF_AD_ALU_XOR_X:
888 case BPF_ALU | BPF_XOR | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100889 /* A ^= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +0100890 ctx->flags |= SEEN_A;
891 emit_xor(r_A, r_A, r_X, ctx);
892 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200893 case BPF_ALU | BPF_AND | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100894 /* A &= K */
895 ctx->flags |= SEEN_A;
896 emit_andi(r_A, r_A, k, ctx);
897 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200898 case BPF_ALU | BPF_AND | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100899 /* A &= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +0100900 ctx->flags |= SEEN_A | SEEN_X;
901 emit_and(r_A, r_A, r_X, ctx);
902 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200903 case BPF_ALU | BPF_LSH | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100904 /* A <<= K */
905 ctx->flags |= SEEN_A;
906 emit_sll(r_A, r_A, k, ctx);
907 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200908 case BPF_ALU | BPF_LSH | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100909 /* A <<= X */
910 ctx->flags |= SEEN_A | SEEN_X;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100911 emit_sllv(r_A, r_A, r_X, ctx);
912 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200913 case BPF_ALU | BPF_RSH | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100914 /* A >>= K */
915 ctx->flags |= SEEN_A;
916 emit_srl(r_A, r_A, k, ctx);
917 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200918 case BPF_ALU | BPF_RSH | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100919 ctx->flags |= SEEN_A | SEEN_X;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100920 emit_srlv(r_A, r_A, r_X, ctx);
921 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200922 case BPF_ALU | BPF_NEG:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100923 /* A = -A */
924 ctx->flags |= SEEN_A;
925 emit_neg(r_A, ctx);
926 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200927 case BPF_JMP | BPF_JA:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100928 /* pc += K */
929 emit_b(b_imm(i + k + 1, ctx), ctx);
930 emit_nop(ctx);
931 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200932 case BPF_JMP | BPF_JEQ | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100933 /* pc += ( A == K ) ? pc->jt : pc->jf */
934 condt = MIPS_COND_EQ | MIPS_COND_K;
935 goto jmp_cmp;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200936 case BPF_JMP | BPF_JEQ | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100937 ctx->flags |= SEEN_X;
938 /* pc += ( A == X ) ? pc->jt : pc->jf */
939 condt = MIPS_COND_EQ | MIPS_COND_X;
940 goto jmp_cmp;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200941 case BPF_JMP | BPF_JGE | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100942 /* pc += ( A >= K ) ? pc->jt : pc->jf */
943 condt = MIPS_COND_GE | MIPS_COND_K;
944 goto jmp_cmp;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200945 case BPF_JMP | BPF_JGE | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100946 ctx->flags |= SEEN_X;
947 /* pc += ( A >= X ) ? pc->jt : pc->jf */
948 condt = MIPS_COND_GE | MIPS_COND_X;
949 goto jmp_cmp;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200950 case BPF_JMP | BPF_JGT | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100951 /* pc += ( A > K ) ? pc->jt : pc->jf */
952 condt = MIPS_COND_GT | MIPS_COND_K;
953 goto jmp_cmp;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200954 case BPF_JMP | BPF_JGT | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100955 ctx->flags |= SEEN_X;
956 /* pc += ( A > X ) ? pc->jt : pc->jf */
957 condt = MIPS_COND_GT | MIPS_COND_X;
958jmp_cmp:
959 /* Greater or Equal */
960 if ((condt & MIPS_COND_GE) ||
961 (condt & MIPS_COND_GT)) {
962 if (condt & MIPS_COND_K) { /* K */
Markos Chandrasad152bd2015-06-04 11:56:11 +0100963 ctx->flags |= SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100964 emit_sltiu(r_s0, r_A, k, ctx);
965 } else { /* X */
Markos Chandrasad152bd2015-06-04 11:56:11 +0100966 ctx->flags |= SEEN_A |
Markos Chandrasc6610de2014-04-08 12:47:14 +0100967 SEEN_X;
968 emit_sltu(r_s0, r_A, r_X, ctx);
969 }
970 /* A < (K|X) ? r_scrach = 1 */
971 b_off = b_imm(i + inst->jf + 1, ctx);
Markos Chandras1ab24a42014-06-23 10:38:51 +0100972 emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off,
Markos Chandrasc6610de2014-04-08 12:47:14 +0100973 ctx);
974 emit_nop(ctx);
975 /* A > (K|X) ? scratch = 0 */
976 if (condt & MIPS_COND_GT) {
977 /* Checking for equality */
Markos Chandrasad152bd2015-06-04 11:56:11 +0100978 ctx->flags |= SEEN_A | SEEN_X;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100979 if (condt & MIPS_COND_K)
980 emit_load_imm(r_s0, k, ctx);
981 else
982 emit_jit_reg_move(r_s0, r_X,
983 ctx);
984 b_off = b_imm(i + inst->jf + 1, ctx);
985 emit_bcond(MIPS_COND_EQ, r_A, r_s0,
986 b_off, ctx);
987 emit_nop(ctx);
988 /* Finally, A > K|X */
989 b_off = b_imm(i + inst->jt + 1, ctx);
990 emit_b(b_off, ctx);
991 emit_nop(ctx);
992 } else {
993 /* A >= (K|X) so jump */
994 b_off = b_imm(i + inst->jt + 1, ctx);
995 emit_b(b_off, ctx);
996 emit_nop(ctx);
997 }
998 } else {
999 /* A == K|X */
1000 if (condt & MIPS_COND_K) { /* K */
Markos Chandrasad152bd2015-06-04 11:56:11 +01001001 ctx->flags |= SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001002 emit_load_imm(r_s0, k, ctx);
1003 /* jump true */
1004 b_off = b_imm(i + inst->jt + 1, ctx);
1005 emit_bcond(MIPS_COND_EQ, r_A, r_s0,
1006 b_off, ctx);
1007 emit_nop(ctx);
1008 /* jump false */
1009 b_off = b_imm(i + inst->jf + 1,
1010 ctx);
1011 emit_bcond(MIPS_COND_NE, r_A, r_s0,
1012 b_off, ctx);
1013 emit_nop(ctx);
1014 } else { /* X */
1015 /* jump true */
1016 ctx->flags |= SEEN_A | SEEN_X;
1017 b_off = b_imm(i + inst->jt + 1,
1018 ctx);
1019 emit_bcond(MIPS_COND_EQ, r_A, r_X,
1020 b_off, ctx);
1021 emit_nop(ctx);
1022 /* jump false */
1023 b_off = b_imm(i + inst->jf + 1, ctx);
1024 emit_bcond(MIPS_COND_NE, r_A, r_X,
1025 b_off, ctx);
1026 emit_nop(ctx);
1027 }
1028 }
1029 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001030 case BPF_JMP | BPF_JSET | BPF_K:
Markos Chandrasad152bd2015-06-04 11:56:11 +01001031 ctx->flags |= SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001032 /* pc += (A & K) ? pc -> jt : pc -> jf */
1033 emit_load_imm(r_s1, k, ctx);
1034 emit_and(r_s0, r_A, r_s1, ctx);
1035 /* jump true */
1036 b_off = b_imm(i + inst->jt + 1, ctx);
1037 emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
1038 emit_nop(ctx);
1039 /* jump false */
1040 b_off = b_imm(i + inst->jf + 1, ctx);
1041 emit_b(b_off, ctx);
1042 emit_nop(ctx);
1043 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001044 case BPF_JMP | BPF_JSET | BPF_X:
Markos Chandrasad152bd2015-06-04 11:56:11 +01001045 ctx->flags |= SEEN_X | SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001046 /* pc += (A & X) ? pc -> jt : pc -> jf */
1047 emit_and(r_s0, r_A, r_X, ctx);
1048 /* jump true */
1049 b_off = b_imm(i + inst->jt + 1, ctx);
1050 emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
1051 emit_nop(ctx);
1052 /* jump false */
1053 b_off = b_imm(i + inst->jf + 1, ctx);
1054 emit_b(b_off, ctx);
1055 emit_nop(ctx);
1056 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001057 case BPF_RET | BPF_A:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001058 ctx->flags |= SEEN_A;
1059 if (i != prog->len - 1)
1060 /*
1061 * If this is not the last instruction
1062 * then jump to the epilogue
1063 */
1064 emit_b(b_imm(prog->len, ctx), ctx);
1065 emit_reg_move(r_ret, r_A, ctx); /* delay slot */
1066 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001067 case BPF_RET | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001068 /*
1069 * It can emit two instructions so it does not fit on
1070 * the delay slot.
1071 */
1072 emit_load_imm(r_ret, k, ctx);
1073 if (i != prog->len - 1) {
1074 /*
1075 * If this is not the last instruction
1076 * then jump to the epilogue
1077 */
1078 emit_b(b_imm(prog->len, ctx), ctx);
1079 emit_nop(ctx);
1080 }
1081 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001082 case BPF_MISC | BPF_TAX:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001083 /* X = A */
1084 ctx->flags |= SEEN_X | SEEN_A;
1085 emit_jit_reg_move(r_X, r_A, ctx);
1086 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001087 case BPF_MISC | BPF_TXA:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001088 /* A = X */
1089 ctx->flags |= SEEN_A | SEEN_X;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001090 emit_jit_reg_move(r_A, r_X, ctx);
1091 break;
1092 /* AUX */
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001093 case BPF_ANC | SKF_AD_PROTOCOL:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001094 /* A = ntohs(skb->protocol */
1095 ctx->flags |= SEEN_SKB | SEEN_OFF | SEEN_A;
1096 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1097 protocol) != 2);
1098 off = offsetof(struct sk_buff, protocol);
1099 emit_half_load(r_A, r_skb, off, ctx);
1100#ifdef CONFIG_CPU_LITTLE_ENDIAN
1101 /* This needs little endian fixup */
Chen Jie3c09bae2014-08-15 16:56:58 +08001102 if (cpu_has_wsbh) {
Ralf Baechleb4f16c92014-06-03 13:05:55 +02001103 /* R2 and later have the wsbh instruction */
1104 emit_wsbh(r_A, r_A, ctx);
1105 } else {
Markos Chandrasc6610de2014-04-08 12:47:14 +01001106 /* Get first byte */
1107 emit_andi(r_tmp_imm, r_A, 0xff, ctx);
1108 /* Shift it */
1109 emit_sll(r_tmp, r_tmp_imm, 8, ctx);
1110 /* Get second byte */
1111 emit_srl(r_tmp_imm, r_A, 8, ctx);
1112 emit_andi(r_tmp_imm, r_tmp_imm, 0xff, ctx);
1113 /* Put everyting together in r_A */
1114 emit_or(r_A, r_tmp, r_tmp_imm, ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +01001115 }
1116#endif
1117 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001118 case BPF_ANC | SKF_AD_CPU:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001119 ctx->flags |= SEEN_A | SEEN_OFF;
1120 /* A = current_thread_info()->cpu */
1121 BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info,
1122 cpu) != 4);
1123 off = offsetof(struct thread_info, cpu);
1124 /* $28/gp points to the thread_info struct */
1125 emit_load(r_A, 28, off, ctx);
1126 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001127 case BPF_ANC | SKF_AD_IFINDEX:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001128 /* A = skb->dev->ifindex */
David Daney4ad70152017-03-14 14:21:41 -07001129 case BPF_ANC | SKF_AD_HATYPE:
1130 /* A = skb->dev->type */
Markos Chandrasad152bd2015-06-04 11:56:11 +01001131 ctx->flags |= SEEN_SKB | SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001132 off = offsetof(struct sk_buff, dev);
Markos Chandrasb6a14a92014-06-25 09:39:38 +01001133 /* Load *dev pointer */
1134 emit_load_ptr(r_s0, r_skb, off, ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +01001135 /* error (0) in the delay slot */
1136 emit_bcond(MIPS_COND_EQ, r_s0, r_zero,
1137 b_imm(prog->len, ctx), ctx);
1138 emit_reg_move(r_ret, r_zero, ctx);
David Daney4ad70152017-03-14 14:21:41 -07001139 if (code == (BPF_ANC | SKF_AD_IFINDEX)) {
1140 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
1141 off = offsetof(struct net_device, ifindex);
1142 emit_load(r_A, r_s0, off, ctx);
1143 } else { /* (code == (BPF_ANC | SKF_AD_HATYPE) */
1144 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
1145 off = offsetof(struct net_device, type);
1146 emit_half_load_unsigned(r_A, r_s0, off, ctx);
1147 }
Markos Chandrasc6610de2014-04-08 12:47:14 +01001148 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001149 case BPF_ANC | SKF_AD_MARK:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001150 ctx->flags |= SEEN_SKB | SEEN_A;
1151 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
1152 off = offsetof(struct sk_buff, mark);
1153 emit_load(r_A, r_skb, off, ctx);
1154 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001155 case BPF_ANC | SKF_AD_RXHASH:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001156 ctx->flags |= SEEN_SKB | SEEN_A;
1157 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
1158 off = offsetof(struct sk_buff, hash);
1159 emit_load(r_A, r_skb, off, ctx);
1160 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001161 case BPF_ANC | SKF_AD_VLAN_TAG:
1162 case BPF_ANC | SKF_AD_VLAN_TAG_PRESENT:
Markos Chandrasad152bd2015-06-04 11:56:11 +01001163 ctx->flags |= SEEN_SKB | SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001164 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1165 vlan_tci) != 2);
1166 off = offsetof(struct sk_buff, vlan_tci);
David Daney64b2dd32017-03-14 14:21:42 -07001167 emit_half_load_unsigned(r_s0, r_skb, off, ctx);
Markos Chandras91a41d72014-06-23 10:38:53 +01001168 if (code == (BPF_ANC | SKF_AD_VLAN_TAG)) {
Markos Chandras6e86c592014-06-23 10:38:52 +01001169 emit_andi(r_A, r_s0, (u16)~VLAN_TAG_PRESENT, ctx);
Markos Chandras91a41d72014-06-23 10:38:53 +01001170 } else {
Markos Chandras9ee16062014-06-23 10:38:49 +01001171 emit_andi(r_A, r_s0, VLAN_TAG_PRESENT, ctx);
Markos Chandras91a41d72014-06-23 10:38:53 +01001172 /* return 1 if present */
1173 emit_sltu(r_A, r_zero, r_A, ctx);
1174 }
Markos Chandrasc6610de2014-04-08 12:47:14 +01001175 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001176 case BPF_ANC | SKF_AD_PKTTYPE:
Markos Chandras9eebfe42014-06-23 10:38:50 +01001177 ctx->flags |= SEEN_SKB;
1178
Hannes Frederic Sowa233577a2014-09-12 14:04:43 +02001179 emit_load_byte(r_tmp, r_skb, PKT_TYPE_OFFSET(), ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +01001180 /* Keep only the last 3 bits */
1181 emit_andi(r_A, r_tmp, PKT_TYPE_MAX, ctx);
Markos Chandrasb4fe0ec2014-06-23 10:38:58 +01001182#ifdef __BIG_ENDIAN_BITFIELD
1183 /* Get the actual packet type to the lower 3 bits */
1184 emit_srl(r_A, r_A, 5, ctx);
1185#endif
Markos Chandrasc6610de2014-04-08 12:47:14 +01001186 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001187 case BPF_ANC | SKF_AD_QUEUE:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001188 ctx->flags |= SEEN_SKB | SEEN_A;
1189 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1190 queue_mapping) != 2);
1191 BUILD_BUG_ON(offsetof(struct sk_buff,
1192 queue_mapping) > 0xff);
1193 off = offsetof(struct sk_buff, queue_mapping);
David Daney64b2dd32017-03-14 14:21:42 -07001194 emit_half_load_unsigned(r_A, r_skb, off, ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +01001195 break;
1196 default:
Markos Chandras78b95b62014-06-23 10:38:54 +01001197 pr_debug("%s: Unhandled opcode: 0x%02x\n", __FILE__,
1198 inst->code);
Markos Chandrasc6610de2014-04-08 12:47:14 +01001199 return -1;
1200 }
1201 }
1202
1203 /* compute offsets only during the first pass */
1204 if (ctx->target == NULL)
1205 ctx->offsets[i] = ctx->idx * 4;
1206
1207 return 0;
1208}
1209
1210int bpf_jit_enable __read_mostly;
1211
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001212void bpf_jit_compile(struct bpf_prog *fp)
Markos Chandrasc6610de2014-04-08 12:47:14 +01001213{
1214 struct jit_ctx ctx;
1215 unsigned int alloc_size, tmp_idx;
1216
1217 if (!bpf_jit_enable)
1218 return;
1219
1220 memset(&ctx, 0, sizeof(ctx));
1221
Daniel Borkmannb4e76f7e2016-07-14 13:57:55 +02001222 ctx.offsets = kcalloc(fp->len + 1, sizeof(*ctx.offsets), GFP_KERNEL);
Markos Chandrasc6610de2014-04-08 12:47:14 +01001223 if (ctx.offsets == NULL)
1224 return;
1225
1226 ctx.skf = fp;
1227
1228 if (build_body(&ctx))
1229 goto out;
1230
1231 tmp_idx = ctx.idx;
1232 build_prologue(&ctx);
1233 ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
1234 /* just to complete the ctx.idx count */
1235 build_epilogue(&ctx);
1236
1237 alloc_size = 4 * ctx.idx;
1238 ctx.target = module_alloc(alloc_size);
1239 if (ctx.target == NULL)
1240 goto out;
1241
1242 /* Clean it */
1243 memset(ctx.target, 0, alloc_size);
1244
1245 ctx.idx = 0;
1246
1247 /* Generate the actual JIT code */
1248 build_prologue(&ctx);
1249 build_body(&ctx);
1250 build_epilogue(&ctx);
1251
1252 /* Update the icache */
1253 flush_icache_range((ptr)ctx.target, (ptr)(ctx.target + ctx.idx));
1254
1255 if (bpf_jit_enable > 1)
1256 /* Dump JIT code */
1257 bpf_jit_dump(fp->len, alloc_size, 2, ctx.target);
1258
1259 fp->bpf_func = (void *)ctx.target;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001260 fp->jited = 1;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001261
1262out:
1263 kfree(ctx.offsets);
1264}
1265
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001266void bpf_jit_free(struct bpf_prog *fp)
Markos Chandrasc6610de2014-04-08 12:47:14 +01001267{
1268 if (fp->jited)
Rusty Russellbe1f2212015-01-20 09:07:05 +10301269 module_memfree(fp->bpf_func);
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001270
1271 bpf_prog_unlock_free(fp);
Markos Chandrasc6610de2014-04-08 12:47:14 +01001272}