blob: 73a473c5bdd6d6cab3f23b4655107fb646a19c9c [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>
17#include <linux/kconfig.h>
18#include <linux/moduleloader.h>
19#include <linux/netdevice.h>
20#include <linux/string.h>
21#include <linux/slab.h>
22#include <linux/types.h>
23#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
31 *
Markos Chandrasc6610de2014-04-08 12:47:14 +010032 * s3 BPF register A
33 * s4 BPF register X
34 * s5 *skb
35 * s6 *scratch memory
36 *
37 * On entry (*bpf_func)(*skb, *filter)
38 * a0 = MIPS_R_A0 = skb;
39 * a1 = MIPS_R_A1 = filter;
40 *
41 * Stack
42 * ...
43 * M[15]
44 * M[14]
45 * M[13]
46 * ...
47 * M[0] <-- r_M
48 * saved reg k-1
49 * saved reg k-2
50 * ...
51 * saved reg 0 <-- r_sp
52 * <no argument area>
53 *
54 * Packet layout
55 *
56 * <--------------------- len ------------------------>
57 * <--skb-len(r_skb_hl)-->< ----- skb->data_len ------>
58 * ----------------------------------------------------
59 * | skb->data |
60 * ----------------------------------------------------
61 */
62
63#define RSIZE (sizeof(unsigned long))
64#define ptr typeof(unsigned long)
65
66/* ABI specific return values */
67#ifdef CONFIG_32BIT /* O32 */
68#ifdef CONFIG_CPU_LITTLE_ENDIAN
69#define r_err MIPS_R_V1
70#define r_val MIPS_R_V0
71#else /* CONFIG_CPU_LITTLE_ENDIAN */
72#define r_err MIPS_R_V0
73#define r_val MIPS_R_V1
74#endif
75#else /* N64 */
76#define r_err MIPS_R_V0
77#define r_val MIPS_R_V0
78#endif
79
80#define r_ret MIPS_R_V0
81
82/*
83 * Use 2 scratch registers to avoid pipeline interlocks.
84 * There is no overhead during epilogue and prologue since
85 * any of the $s0-$s6 registers will only be preserved if
86 * they are going to actually be used.
87 */
Markos Chandrasc6610de2014-04-08 12:47:14 +010088#define r_off MIPS_R_S2
89#define r_A MIPS_R_S3
90#define r_X MIPS_R_S4
91#define r_skb MIPS_R_S5
92#define r_M MIPS_R_S6
Markos Chandrasad152bd2015-06-04 11:56:11 +010093#define r_s0 MIPS_R_T4 /* scratch reg 1 */
94#define r_s1 MIPS_R_T5 /* scratch reg 2 */
Markos Chandrasc6610de2014-04-08 12:47:14 +010095#define r_tmp_imm MIPS_R_T6 /* No need to preserve this */
96#define r_tmp MIPS_R_T7 /* No need to preserve this */
97#define r_zero MIPS_R_ZERO
98#define r_sp MIPS_R_SP
99#define r_ra MIPS_R_RA
100
101#define SCRATCH_OFF(k) (4 * (k))
102
103/* JIT flags */
104#define SEEN_CALL (1 << BPF_MEMWORDS)
105#define SEEN_SREG_SFT (BPF_MEMWORDS + 1)
106#define SEEN_SREG_BASE (1 << SEEN_SREG_SFT)
107#define SEEN_SREG(x) (SEEN_SREG_BASE << (x))
Markos Chandrasc6610de2014-04-08 12:47:14 +0100108#define SEEN_OFF SEEN_SREG(2)
109#define SEEN_A SEEN_SREG(3)
110#define SEEN_X SEEN_SREG(4)
111#define SEEN_SKB SEEN_SREG(5)
112#define SEEN_MEM SEEN_SREG(6)
113
114/* Arguments used by JIT */
115#define ARGS_USED_BY_JIT 2 /* only applicable to 64-bit */
116
Markos Chandrasc6610de2014-04-08 12:47:14 +0100117#define SBIT(x) (1 << (x)) /* Signed version of BIT() */
118
119/**
120 * struct jit_ctx - JIT context
121 * @skf: The sk_filter
122 * @prologue_bytes: Number of bytes for prologue
123 * @idx: Instruction index
124 * @flags: JIT flags
125 * @offsets: Instruction offsets
126 * @target: Memory location for the compiled filter
127 */
128struct jit_ctx {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700129 const struct bpf_prog *skf;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100130 unsigned int prologue_bytes;
131 u32 idx;
132 u32 flags;
133 u32 *offsets;
134 u32 *target;
135};
136
137
138static inline int optimize_div(u32 *k)
139{
140 /* power of 2 divides can be implemented with right shift */
141 if (!(*k & (*k-1))) {
142 *k = ilog2(*k);
143 return 1;
144 }
145
146 return 0;
147}
148
Markos Chandras95782bf2014-06-25 09:37:21 +0100149static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx);
150
Markos Chandrasc6610de2014-04-08 12:47:14 +0100151/* Simply emit the instruction if the JIT memory space has been allocated */
152#define emit_instr(ctx, func, ...) \
153do { \
154 if ((ctx)->target != NULL) { \
155 u32 *p = &(ctx)->target[ctx->idx]; \
156 uasm_i_##func(&p, ##__VA_ARGS__); \
157 } \
158 (ctx)->idx++; \
159} while (0)
160
Markos Chandras39bcb792014-07-23 10:00:09 +0100161/*
162 * Similar to emit_instr but it must be used when we need to emit
163 * 32-bit or 64-bit instructions
164 */
165#define emit_long_instr(ctx, func, ...) \
166do { \
167 if ((ctx)->target != NULL) { \
168 u32 *p = &(ctx)->target[ctx->idx]; \
169 UASM_i_##func(&p, ##__VA_ARGS__); \
170 } \
171 (ctx)->idx++; \
172} while (0)
173
Markos Chandrasc6610de2014-04-08 12:47:14 +0100174/* Determine if immediate is within the 16-bit signed range */
175static inline bool is_range16(s32 imm)
176{
Markos Chandras10c4d612014-06-23 10:38:55 +0100177 return !(imm >= SBIT(15) || imm < -SBIT(15));
Markos Chandrasc6610de2014-04-08 12:47:14 +0100178}
179
180static inline void emit_addu(unsigned int dst, unsigned int src1,
181 unsigned int src2, struct jit_ctx *ctx)
182{
183 emit_instr(ctx, addu, dst, src1, src2);
184}
185
186static inline void emit_nop(struct jit_ctx *ctx)
187{
188 emit_instr(ctx, nop);
189}
190
191/* Load a u32 immediate to a register */
192static inline void emit_load_imm(unsigned int dst, u32 imm, struct jit_ctx *ctx)
193{
194 if (ctx->target != NULL) {
195 /* addiu can only handle s16 */
Markos Chandras10c4d612014-06-23 10:38:55 +0100196 if (!is_range16(imm)) {
Markos Chandrasc6610de2014-04-08 12:47:14 +0100197 u32 *p = &ctx->target[ctx->idx];
198 uasm_i_lui(&p, r_tmp_imm, (s32)imm >> 16);
199 p = &ctx->target[ctx->idx + 1];
200 uasm_i_ori(&p, dst, r_tmp_imm, imm & 0xffff);
201 } else {
202 u32 *p = &ctx->target[ctx->idx];
203 uasm_i_addiu(&p, dst, r_zero, imm);
204 }
205 }
206 ctx->idx++;
207
Markos Chandras10c4d612014-06-23 10:38:55 +0100208 if (!is_range16(imm))
Markos Chandrasc6610de2014-04-08 12:47:14 +0100209 ctx->idx++;
210}
211
212static inline void emit_or(unsigned int dst, unsigned int src1,
213 unsigned int src2, struct jit_ctx *ctx)
214{
215 emit_instr(ctx, or, dst, src1, src2);
216}
217
218static inline void emit_ori(unsigned int dst, unsigned src, u32 imm,
219 struct jit_ctx *ctx)
220{
221 if (imm >= BIT(16)) {
222 emit_load_imm(r_tmp, imm, ctx);
223 emit_or(dst, src, r_tmp, ctx);
224 } else {
225 emit_instr(ctx, ori, dst, src, imm);
226 }
227}
228
Markos Chandrasc6610de2014-04-08 12:47:14 +0100229static inline void emit_daddiu(unsigned int dst, unsigned int src,
230 int imm, struct jit_ctx *ctx)
231{
232 /*
233 * Only used for stack, so the imm is relatively small
234 * and it fits in 15-bits
235 */
236 emit_instr(ctx, daddiu, dst, src, imm);
237}
238
239static inline void emit_addiu(unsigned int dst, unsigned int src,
240 u32 imm, struct jit_ctx *ctx)
241{
Markos Chandras10c4d612014-06-23 10:38:55 +0100242 if (!is_range16(imm)) {
Markos Chandrasc6610de2014-04-08 12:47:14 +0100243 emit_load_imm(r_tmp, imm, ctx);
244 emit_addu(dst, r_tmp, src, ctx);
245 } else {
246 emit_instr(ctx, addiu, dst, src, imm);
247 }
248}
249
250static inline void emit_and(unsigned int dst, unsigned int src1,
251 unsigned int src2, struct jit_ctx *ctx)
252{
253 emit_instr(ctx, and, dst, src1, src2);
254}
255
256static inline void emit_andi(unsigned int dst, unsigned int src,
257 u32 imm, struct jit_ctx *ctx)
258{
259 /* If imm does not fit in u16 then load it to register */
260 if (imm >= BIT(16)) {
261 emit_load_imm(r_tmp, imm, ctx);
262 emit_and(dst, src, r_tmp, ctx);
263 } else {
264 emit_instr(ctx, andi, dst, src, imm);
265 }
266}
267
268static inline void emit_xor(unsigned int dst, unsigned int src1,
269 unsigned int src2, struct jit_ctx *ctx)
270{
271 emit_instr(ctx, xor, dst, src1, src2);
272}
273
274static inline void emit_xori(ptr dst, ptr src, u32 imm, struct jit_ctx *ctx)
275{
276 /* If imm does not fit in u16 then load it to register */
277 if (imm >= BIT(16)) {
278 emit_load_imm(r_tmp, imm, ctx);
279 emit_xor(dst, src, r_tmp, ctx);
280 } else {
281 emit_instr(ctx, xori, dst, src, imm);
282 }
283}
284
285static inline void emit_stack_offset(int offset, struct jit_ctx *ctx)
286{
Markos Chandras39bcb792014-07-23 10:00:09 +0100287 emit_long_instr(ctx, ADDIU, r_sp, r_sp, offset);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100288}
289
290static inline void emit_subu(unsigned int dst, unsigned int src1,
291 unsigned int src2, struct jit_ctx *ctx)
292{
293 emit_instr(ctx, subu, dst, src1, src2);
294}
295
296static inline void emit_neg(unsigned int reg, struct jit_ctx *ctx)
297{
298 emit_subu(reg, r_zero, reg, ctx);
299}
300
301static inline void emit_sllv(unsigned int dst, unsigned int src,
302 unsigned int sa, struct jit_ctx *ctx)
303{
304 emit_instr(ctx, sllv, dst, src, sa);
305}
306
307static inline void emit_sll(unsigned int dst, unsigned int src,
308 unsigned int sa, struct jit_ctx *ctx)
309{
310 /* sa is 5-bits long */
Markos Chandras95782bf2014-06-25 09:37:21 +0100311 if (sa >= BIT(5))
312 /* Shifting >= 32 results in zero */
313 emit_jit_reg_move(dst, r_zero, ctx);
314 else
315 emit_instr(ctx, sll, dst, src, sa);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100316}
317
318static inline void emit_srlv(unsigned int dst, unsigned int src,
319 unsigned int sa, struct jit_ctx *ctx)
320{
321 emit_instr(ctx, srlv, dst, src, sa);
322}
323
324static inline void emit_srl(unsigned int dst, unsigned int src,
325 unsigned int sa, struct jit_ctx *ctx)
326{
327 /* sa is 5-bits long */
Markos Chandras95782bf2014-06-25 09:37:21 +0100328 if (sa >= BIT(5))
329 /* Shifting >= 32 results in zero */
330 emit_jit_reg_move(dst, r_zero, ctx);
331 else
332 emit_instr(ctx, srl, dst, src, sa);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100333}
334
Markos Chandras55393ee2014-06-23 10:38:48 +0100335static inline void emit_slt(unsigned int dst, unsigned int src1,
336 unsigned int src2, struct jit_ctx *ctx)
337{
338 emit_instr(ctx, slt, dst, src1, src2);
339}
340
Markos Chandrasc6610de2014-04-08 12:47:14 +0100341static inline void emit_sltu(unsigned int dst, unsigned int src1,
342 unsigned int src2, struct jit_ctx *ctx)
343{
344 emit_instr(ctx, sltu, dst, src1, src2);
345}
346
347static inline void emit_sltiu(unsigned dst, unsigned int src,
348 unsigned int imm, struct jit_ctx *ctx)
349{
350 /* 16 bit immediate */
Markos Chandras10c4d612014-06-23 10:38:55 +0100351 if (!is_range16((s32)imm)) {
Markos Chandrasc6610de2014-04-08 12:47:14 +0100352 emit_load_imm(r_tmp, imm, ctx);
353 emit_sltu(dst, src, r_tmp, ctx);
354 } else {
355 emit_instr(ctx, sltiu, dst, src, imm);
356 }
357
358}
359
360/* Store register on the stack */
361static inline void emit_store_stack_reg(ptr reg, ptr base,
362 unsigned int offset,
363 struct jit_ctx *ctx)
364{
Markos Chandras39bcb792014-07-23 10:00:09 +0100365 emit_long_instr(ctx, SW, reg, offset, base);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100366}
367
368static inline void emit_store(ptr reg, ptr base, unsigned int offset,
369 struct jit_ctx *ctx)
370{
371 emit_instr(ctx, sw, reg, offset, base);
372}
373
374static inline void emit_load_stack_reg(ptr reg, ptr base,
375 unsigned int offset,
376 struct jit_ctx *ctx)
377{
Markos Chandras39bcb792014-07-23 10:00:09 +0100378 emit_long_instr(ctx, LW, reg, offset, base);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100379}
380
381static inline void emit_load(unsigned int reg, unsigned int base,
382 unsigned int offset, struct jit_ctx *ctx)
383{
384 emit_instr(ctx, lw, reg, offset, base);
385}
386
387static inline void emit_load_byte(unsigned int reg, unsigned int base,
388 unsigned int offset, struct jit_ctx *ctx)
389{
390 emit_instr(ctx, lb, reg, offset, base);
391}
392
393static inline void emit_half_load(unsigned int reg, unsigned int base,
394 unsigned int offset, struct jit_ctx *ctx)
395{
396 emit_instr(ctx, lh, reg, offset, base);
397}
398
399static inline void emit_mul(unsigned int dst, unsigned int src1,
400 unsigned int src2, struct jit_ctx *ctx)
401{
402 emit_instr(ctx, mul, dst, src1, src2);
403}
404
405static inline void emit_div(unsigned int dst, unsigned int src,
406 struct jit_ctx *ctx)
407{
408 if (ctx->target != NULL) {
409 u32 *p = &ctx->target[ctx->idx];
410 uasm_i_divu(&p, dst, src);
411 p = &ctx->target[ctx->idx + 1];
Markos Chandras35a8e162014-06-23 10:38:47 +0100412 uasm_i_mflo(&p, dst);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100413 }
414 ctx->idx += 2; /* 2 insts */
415}
416
417static inline void emit_mod(unsigned int dst, unsigned int src,
418 struct jit_ctx *ctx)
419{
420 if (ctx->target != NULL) {
421 u32 *p = &ctx->target[ctx->idx];
422 uasm_i_divu(&p, dst, src);
423 p = &ctx->target[ctx->idx + 1];
Denis Kirjanov2e464772014-12-01 12:57:02 +0300424 uasm_i_mfhi(&p, dst);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100425 }
426 ctx->idx += 2; /* 2 insts */
427}
428
429static inline void emit_dsll(unsigned int dst, unsigned int src,
430 unsigned int sa, struct jit_ctx *ctx)
431{
432 emit_instr(ctx, dsll, dst, src, sa);
433}
434
435static inline void emit_dsrl32(unsigned int dst, unsigned int src,
436 unsigned int sa, struct jit_ctx *ctx)
437{
438 emit_instr(ctx, dsrl32, dst, src, sa);
439}
440
441static inline void emit_wsbh(unsigned int dst, unsigned int src,
442 struct jit_ctx *ctx)
443{
444 emit_instr(ctx, wsbh, dst, src);
445}
446
Markos Chandrasb6a14a92014-06-25 09:39:38 +0100447/* load pointer to register */
448static inline void emit_load_ptr(unsigned int dst, unsigned int src,
449 int imm, struct jit_ctx *ctx)
450{
451 /* src contains the base addr of the 32/64-pointer */
Markos Chandras39bcb792014-07-23 10:00:09 +0100452 emit_long_instr(ctx, LW, dst, imm, src);
Markos Chandrasb6a14a92014-06-25 09:39:38 +0100453}
454
Markos Chandrasc6610de2014-04-08 12:47:14 +0100455/* load a function pointer to register */
456static inline void emit_load_func(unsigned int reg, ptr imm,
457 struct jit_ctx *ctx)
458{
459 if (config_enabled(CONFIG_64BIT)) {
460 /* At this point imm is always 64-bit */
461 emit_load_imm(r_tmp, (u64)imm >> 32, ctx);
462 emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */
463 emit_ori(r_tmp, r_tmp_imm, (imm >> 16) & 0xffff, ctx);
464 emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */
465 emit_ori(reg, r_tmp_imm, imm & 0xffff, ctx);
466 } else {
467 emit_load_imm(reg, imm, ctx);
468 }
469}
470
471/* Move to real MIPS register */
472static inline void emit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
473{
Markos Chandras39bcb792014-07-23 10:00:09 +0100474 emit_long_instr(ctx, ADDU, dst, src, r_zero);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100475}
476
477/* Move to JIT (32-bit) register */
478static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
479{
480 emit_addu(dst, src, r_zero, ctx);
481}
482
483/* Compute the immediate value for PC-relative branches. */
484static inline u32 b_imm(unsigned int tgt, struct jit_ctx *ctx)
485{
486 if (ctx->target == NULL)
487 return 0;
488
489 /*
490 * We want a pc-relative branch. We only do forward branches
491 * so tgt is always after pc. tgt is the instruction offset
492 * we want to jump to.
493
494 * Branch on MIPS:
495 * I: target_offset <- sign_extend(offset)
496 * I+1: PC += target_offset (delay slot)
497 *
498 * ctx->idx currently points to the branch instruction
499 * but the offset is added to the delay slot so we need
500 * to subtract 4.
501 */
502 return ctx->offsets[tgt] -
503 (ctx->idx * 4 - ctx->prologue_bytes) - 4;
504}
505
506static inline void emit_bcond(int cond, unsigned int reg1, unsigned int reg2,
507 unsigned int imm, struct jit_ctx *ctx)
508{
509 if (ctx->target != NULL) {
510 u32 *p = &ctx->target[ctx->idx];
511
512 switch (cond) {
513 case MIPS_COND_EQ:
514 uasm_i_beq(&p, reg1, reg2, imm);
515 break;
516 case MIPS_COND_NE:
517 uasm_i_bne(&p, reg1, reg2, imm);
518 break;
519 case MIPS_COND_ALL:
520 uasm_i_b(&p, imm);
521 break;
522 default:
523 pr_warn("%s: Unhandled branch conditional: %d\n",
524 __func__, cond);
525 }
526 }
527 ctx->idx++;
528}
529
530static inline void emit_b(unsigned int imm, struct jit_ctx *ctx)
531{
532 emit_bcond(MIPS_COND_ALL, r_zero, r_zero, imm, ctx);
533}
534
535static inline void emit_jalr(unsigned int link, unsigned int reg,
536 struct jit_ctx *ctx)
537{
538 emit_instr(ctx, jalr, link, reg);
539}
540
541static inline void emit_jr(unsigned int reg, struct jit_ctx *ctx)
542{
543 emit_instr(ctx, jr, reg);
544}
545
546static inline u16 align_sp(unsigned int num)
547{
548 /* Double word alignment for 32-bit, quadword for 64-bit */
549 unsigned int align = config_enabled(CONFIG_64BIT) ? 16 : 8;
550 num = (num + (align - 1)) & -align;
551 return num;
552}
553
Markos Chandrasc6610de2014-04-08 12:47:14 +0100554static bool is_load_to_a(u16 inst)
555{
556 switch (inst) {
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200557 case BPF_LD | BPF_W | BPF_LEN:
558 case BPF_LD | BPF_W | BPF_ABS:
559 case BPF_LD | BPF_H | BPF_ABS:
560 case BPF_LD | BPF_B | BPF_ABS:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100561 return true;
562 default:
563 return false;
564 }
565}
566
567static void save_bpf_jit_regs(struct jit_ctx *ctx, unsigned offset)
568{
569 int i = 0, real_off = 0;
570 u32 sflags, tmp_flags;
571
572 /* Adjust the stack pointer */
573 emit_stack_offset(-align_sp(offset), ctx);
574
575 if (ctx->flags & SEEN_CALL) {
576 /* Argument save area */
577 if (config_enabled(CONFIG_64BIT))
578 /* Bottom of current frame */
579 real_off = align_sp(offset) - RSIZE;
580 else
581 /* Top of previous frame */
582 real_off = align_sp(offset) + RSIZE;
583 emit_store_stack_reg(MIPS_R_A0, r_sp, real_off, ctx);
584 emit_store_stack_reg(MIPS_R_A1, r_sp, real_off + RSIZE, ctx);
585
586 real_off = 0;
587 }
588
589 tmp_flags = sflags = ctx->flags >> SEEN_SREG_SFT;
590 /* sflags is essentially a bitmap */
591 while (tmp_flags) {
592 if ((sflags >> i) & 0x1) {
593 emit_store_stack_reg(MIPS_R_S0 + i, r_sp, real_off,
594 ctx);
595 real_off += RSIZE;
596 }
597 i++;
598 tmp_flags >>= 1;
599 }
600
601 /* save return address */
602 if (ctx->flags & SEEN_CALL) {
603 emit_store_stack_reg(r_ra, r_sp, real_off, ctx);
604 real_off += RSIZE;
605 }
606
607 /* Setup r_M leaving the alignment gap if necessary */
608 if (ctx->flags & SEEN_MEM) {
609 if (real_off % (RSIZE * 2))
610 real_off += RSIZE;
Markos Chandras39bcb792014-07-23 10:00:09 +0100611 emit_long_instr(ctx, ADDIU, r_M, r_sp, real_off);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100612 }
613}
614
615static void restore_bpf_jit_regs(struct jit_ctx *ctx,
616 unsigned int offset)
617{
618 int i, real_off = 0;
619 u32 sflags, tmp_flags;
620
621 if (ctx->flags & SEEN_CALL) {
622 if (config_enabled(CONFIG_64BIT))
623 /* Bottom of current frame */
624 real_off = align_sp(offset) - RSIZE;
625 else
626 /* Top of previous frame */
627 real_off = align_sp(offset) + RSIZE;
628 emit_load_stack_reg(MIPS_R_A0, r_sp, real_off, ctx);
629 emit_load_stack_reg(MIPS_R_A1, r_sp, real_off + RSIZE, ctx);
630
631 real_off = 0;
632 }
633
634 tmp_flags = sflags = ctx->flags >> SEEN_SREG_SFT;
635 /* sflags is a bitmap */
636 i = 0;
637 while (tmp_flags) {
638 if ((sflags >> i) & 0x1) {
639 emit_load_stack_reg(MIPS_R_S0 + i, r_sp, real_off,
640 ctx);
641 real_off += RSIZE;
642 }
643 i++;
644 tmp_flags >>= 1;
645 }
646
647 /* restore return address */
648 if (ctx->flags & SEEN_CALL)
649 emit_load_stack_reg(r_ra, r_sp, real_off, ctx);
650
651 /* Restore the sp and discard the scrach memory */
652 emit_stack_offset(align_sp(offset), ctx);
653}
654
655static unsigned int get_stack_depth(struct jit_ctx *ctx)
656{
657 int sp_off = 0;
658
659
660 /* How may s* regs do we need to preserved? */
661 sp_off += hweight32(ctx->flags >> SEEN_SREG_SFT) * RSIZE;
662
663 if (ctx->flags & SEEN_MEM)
664 sp_off += 4 * BPF_MEMWORDS; /* BPF_MEMWORDS are 32-bit */
665
666 if (ctx->flags & SEEN_CALL)
667 /*
668 * The JIT code make calls to external functions using 2
669 * arguments. Therefore, for o32 we don't need to allocate
670 * space because we don't care if the argumetns are lost
671 * across calls. We do need however to preserve incoming
672 * arguments but the space is already allocated for us by
673 * the caller. On the other hand, for n64, we need to allocate
674 * this space ourselves. We need to preserve $ra as well.
675 */
676 sp_off += config_enabled(CONFIG_64BIT) ?
677 (ARGS_USED_BY_JIT + 1) * RSIZE : RSIZE;
678
Markos Chandras8833bc32015-06-04 11:56:13 +0100679 return sp_off;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100680}
681
682static void build_prologue(struct jit_ctx *ctx)
683{
684 u16 first_inst = ctx->skf->insns[0].code;
685 int sp_off;
686
687 /* Calculate the total offset for the stack pointer */
688 sp_off = get_stack_depth(ctx);
689 save_bpf_jit_regs(ctx, sp_off);
690
691 if (ctx->flags & SEEN_SKB)
692 emit_reg_move(r_skb, MIPS_R_A0, ctx);
693
Markos Chandrase5bb48b2014-06-23 10:38:56 +0100694 if (ctx->flags & SEEN_X)
Markos Chandrasc6610de2014-04-08 12:47:14 +0100695 emit_jit_reg_move(r_X, r_zero, ctx);
696
697 /* Do not leak kernel data to userspace */
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200698 if ((first_inst != (BPF_RET | BPF_K)) && !(is_load_to_a(first_inst)))
Markos Chandrasc6610de2014-04-08 12:47:14 +0100699 emit_jit_reg_move(r_A, r_zero, ctx);
700}
701
702static void build_epilogue(struct jit_ctx *ctx)
703{
704 unsigned int sp_off;
705
706 /* Calculate the total offset for the stack pointer */
707
708 sp_off = get_stack_depth(ctx);
709 restore_bpf_jit_regs(ctx, sp_off);
710
711 /* Return */
712 emit_jr(r_ra, ctx);
713 emit_nop(ctx);
714}
715
716static u64 jit_get_skb_b(struct sk_buff *skb, unsigned offset)
717{
718 u8 ret;
719 int err;
720
721 err = skb_copy_bits(skb, offset, &ret, 1);
722
723 return (u64)err << 32 | ret;
724}
725
726static u64 jit_get_skb_h(struct sk_buff *skb, unsigned offset)
727{
728 u16 ret;
729 int err;
730
731 err = skb_copy_bits(skb, offset, &ret, 2);
732
733 return (u64)err << 32 | ntohs(ret);
734}
735
736static u64 jit_get_skb_w(struct sk_buff *skb, unsigned offset)
737{
738 u32 ret;
739 int err;
740
741 err = skb_copy_bits(skb, offset, &ret, 4);
742
743 return (u64)err << 32 | ntohl(ret);
744}
745
Markos Chandrasc6610de2014-04-08 12:47:14 +0100746static int build_body(struct jit_ctx *ctx)
747{
748 void *load_func[] = {jit_get_skb_b, jit_get_skb_h, jit_get_skb_w};
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700749 const struct bpf_prog *prog = ctx->skf;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100750 const struct sock_filter *inst;
751 unsigned int i, off, load_order, condt;
752 u32 k, b_off __maybe_unused;
753
754 for (i = 0; i < prog->len; i++) {
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200755 u16 code;
756
Markos Chandrasc6610de2014-04-08 12:47:14 +0100757 inst = &(prog->insns[i]);
758 pr_debug("%s: code->0x%02x, jt->0x%x, jf->0x%x, k->0x%x\n",
759 __func__, inst->code, inst->jt, inst->jf, inst->k);
760 k = inst->k;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200761 code = bpf_anc_helper(inst);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100762
763 if (ctx->target == NULL)
764 ctx->offsets[i] = ctx->idx * 4;
765
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200766 switch (code) {
767 case BPF_LD | BPF_IMM:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100768 /* A <- k ==> li r_A, k */
769 ctx->flags |= SEEN_A;
770 emit_load_imm(r_A, k, ctx);
771 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200772 case BPF_LD | BPF_W | BPF_LEN:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100773 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
774 /* A <- len ==> lw r_A, offset(skb) */
775 ctx->flags |= SEEN_SKB | SEEN_A;
776 off = offsetof(struct sk_buff, len);
777 emit_load(r_A, r_skb, off, ctx);
778 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200779 case BPF_LD | BPF_MEM:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100780 /* A <- M[k] ==> lw r_A, offset(M) */
781 ctx->flags |= SEEN_MEM | SEEN_A;
782 emit_load(r_A, r_M, SCRATCH_OFF(k), ctx);
783 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200784 case BPF_LD | BPF_W | BPF_ABS:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100785 /* A <- P[k:4] */
786 load_order = 2;
787 goto load;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200788 case BPF_LD | BPF_H | BPF_ABS:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100789 /* A <- P[k:2] */
790 load_order = 1;
791 goto load;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200792 case BPF_LD | BPF_B | BPF_ABS:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100793 /* A <- P[k:1] */
794 load_order = 0;
795load:
Markos Chandras55393ee2014-06-23 10:38:48 +0100796 /* the interpreter will deal with the negative K */
797 if ((int)k < 0)
798 return -ENOTSUPP;
799
Markos Chandrasc6610de2014-04-08 12:47:14 +0100800 emit_load_imm(r_off, k, ctx);
801load_common:
Markos Chandras55393ee2014-06-23 10:38:48 +0100802 /*
803 * We may got here from the indirect loads so
804 * return if offset is negative.
805 */
806 emit_slt(r_s0, r_off, r_zero, ctx);
807 emit_bcond(MIPS_COND_NE, r_s0, r_zero,
808 b_imm(prog->len, ctx), ctx);
809 emit_reg_move(r_ret, r_zero, ctx);
810
Markos Chandrasad152bd2015-06-04 11:56:11 +0100811 ctx->flags |= SEEN_CALL | SEEN_OFF |
Markos Chandrasc6610de2014-04-08 12:47:14 +0100812 SEEN_SKB | SEEN_A;
813
814 emit_load_func(r_s0, (ptr)load_func[load_order],
815 ctx);
816 emit_reg_move(MIPS_R_A0, r_skb, ctx);
817 emit_jalr(MIPS_R_RA, r_s0, ctx);
818 /* Load second argument to delay slot */
819 emit_reg_move(MIPS_R_A1, r_off, ctx);
820 /* Check the error value */
821 if (config_enabled(CONFIG_64BIT)) {
822 /* Get error code from the top 32-bits */
823 emit_dsrl32(r_s0, r_val, 0, ctx);
824 /* Branch to 3 instructions ahead */
825 emit_bcond(MIPS_COND_NE, r_s0, r_zero, 3 << 2,
826 ctx);
827 } else {
828 /* Branch to 3 instructions ahead */
829 emit_bcond(MIPS_COND_NE, r_err, r_zero, 3 << 2,
830 ctx);
831 }
832 emit_nop(ctx);
833 /* We are good */
834 emit_b(b_imm(i + 1, ctx), ctx);
835 emit_jit_reg_move(r_A, r_val, ctx);
836 /* Return with error */
837 emit_b(b_imm(prog->len, ctx), ctx);
838 emit_reg_move(r_ret, r_zero, ctx);
839 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200840 case BPF_LD | BPF_W | BPF_IND:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100841 /* A <- P[X + k:4] */
842 load_order = 2;
843 goto load_ind;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200844 case BPF_LD | BPF_H | BPF_IND:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100845 /* A <- P[X + k:2] */
846 load_order = 1;
847 goto load_ind;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200848 case BPF_LD | BPF_B | BPF_IND:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100849 /* A <- P[X + k:1] */
850 load_order = 0;
851load_ind:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100852 ctx->flags |= SEEN_OFF | SEEN_X;
853 emit_addiu(r_off, r_X, k, ctx);
854 goto load_common;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200855 case BPF_LDX | BPF_IMM:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100856 /* X <- k */
857 ctx->flags |= SEEN_X;
858 emit_load_imm(r_X, k, ctx);
859 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200860 case BPF_LDX | BPF_MEM:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100861 /* X <- M[k] */
862 ctx->flags |= SEEN_X | SEEN_MEM;
863 emit_load(r_X, r_M, SCRATCH_OFF(k), ctx);
864 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200865 case BPF_LDX | BPF_W | BPF_LEN:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100866 /* X <- len */
867 ctx->flags |= SEEN_X | SEEN_SKB;
868 off = offsetof(struct sk_buff, len);
869 emit_load(r_X, r_skb, off, ctx);
870 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200871 case BPF_LDX | BPF_B | BPF_MSH:
Markos Chandras55393ee2014-06-23 10:38:48 +0100872 /* the interpreter will deal with the negative K */
873 if ((int)k < 0)
874 return -ENOTSUPP;
875
Markos Chandrasc6610de2014-04-08 12:47:14 +0100876 /* X <- 4 * (P[k:1] & 0xf) */
Markos Chandrasad152bd2015-06-04 11:56:11 +0100877 ctx->flags |= SEEN_X | SEEN_CALL | SEEN_SKB;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100878 /* Load offset to a1 */
879 emit_load_func(r_s0, (ptr)jit_get_skb_b, ctx);
880 /*
881 * This may emit two instructions so it may not fit
882 * in the delay slot. So use a0 in the delay slot.
883 */
884 emit_load_imm(MIPS_R_A1, k, ctx);
885 emit_jalr(MIPS_R_RA, r_s0, ctx);
886 emit_reg_move(MIPS_R_A0, r_skb, ctx); /* delay slot */
887 /* Check the error value */
888 if (config_enabled(CONFIG_64BIT)) {
889 /* Top 32-bits of $v0 on 64-bit */
890 emit_dsrl32(r_s0, r_val, 0, ctx);
891 emit_bcond(MIPS_COND_NE, r_s0, r_zero,
892 3 << 2, ctx);
893 } else {
894 emit_bcond(MIPS_COND_NE, r_err, r_zero,
895 3 << 2, ctx);
896 }
897 /* No need for delay slot */
898 /* We are good */
899 /* X <- P[1:K] & 0xf */
900 emit_andi(r_X, r_val, 0xf, ctx);
901 /* X << 2 */
902 emit_b(b_imm(i + 1, ctx), ctx);
903 emit_sll(r_X, r_X, 2, ctx); /* delay slot */
904 /* Return with error */
905 emit_b(b_imm(prog->len, ctx), ctx);
906 emit_load_imm(r_ret, 0, ctx); /* delay slot */
907 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200908 case BPF_ST:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100909 /* M[k] <- A */
910 ctx->flags |= SEEN_MEM | SEEN_A;
911 emit_store(r_A, r_M, SCRATCH_OFF(k), ctx);
912 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200913 case BPF_STX:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100914 /* M[k] <- X */
915 ctx->flags |= SEEN_MEM | SEEN_X;
916 emit_store(r_X, r_M, SCRATCH_OFF(k), ctx);
917 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200918 case BPF_ALU | BPF_ADD | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100919 /* A += K */
920 ctx->flags |= SEEN_A;
921 emit_addiu(r_A, r_A, k, ctx);
922 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200923 case BPF_ALU | BPF_ADD | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100924 /* A += X */
925 ctx->flags |= SEEN_A | SEEN_X;
926 emit_addu(r_A, r_A, r_X, ctx);
927 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200928 case BPF_ALU | BPF_SUB | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100929 /* A -= K */
930 ctx->flags |= SEEN_A;
931 emit_addiu(r_A, r_A, -k, ctx);
932 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200933 case BPF_ALU | BPF_SUB | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100934 /* A -= X */
935 ctx->flags |= SEEN_A | SEEN_X;
936 emit_subu(r_A, r_A, r_X, ctx);
937 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200938 case BPF_ALU | BPF_MUL | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100939 /* A *= K */
940 /* Load K to scratch register before MUL */
Markos Chandrasad152bd2015-06-04 11:56:11 +0100941 ctx->flags |= SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100942 emit_load_imm(r_s0, k, ctx);
943 emit_mul(r_A, r_A, r_s0, ctx);
944 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200945 case BPF_ALU | BPF_MUL | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100946 /* A *= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +0100947 ctx->flags |= SEEN_A | SEEN_X;
948 emit_mul(r_A, r_A, r_X, ctx);
949 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200950 case BPF_ALU | BPF_DIV | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100951 /* A /= k */
952 if (k == 1)
953 break;
954 if (optimize_div(&k)) {
955 ctx->flags |= SEEN_A;
956 emit_srl(r_A, r_A, k, ctx);
957 break;
958 }
Markos Chandrasad152bd2015-06-04 11:56:11 +0100959 ctx->flags |= SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100960 emit_load_imm(r_s0, k, ctx);
961 emit_div(r_A, r_s0, ctx);
962 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200963 case BPF_ALU | BPF_MOD | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100964 /* A %= k */
Denis Kirjanov2e464772014-12-01 12:57:02 +0300965 if (k == 1) {
Markos Chandrasc6610de2014-04-08 12:47:14 +0100966 ctx->flags |= SEEN_A;
967 emit_jit_reg_move(r_A, r_zero, ctx);
968 } else {
Markos Chandrasad152bd2015-06-04 11:56:11 +0100969 ctx->flags |= SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100970 emit_load_imm(r_s0, k, ctx);
971 emit_mod(r_A, r_s0, ctx);
972 }
973 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200974 case BPF_ALU | BPF_DIV | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100975 /* A /= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +0100976 ctx->flags |= SEEN_X | SEEN_A;
977 /* Check if r_X is zero */
978 emit_bcond(MIPS_COND_EQ, r_X, r_zero,
979 b_imm(prog->len, ctx), ctx);
980 emit_load_imm(r_val, 0, ctx); /* delay slot */
981 emit_div(r_A, r_X, ctx);
982 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200983 case BPF_ALU | BPF_MOD | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100984 /* A %= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +0100985 ctx->flags |= SEEN_X | SEEN_A;
986 /* Check if r_X is zero */
987 emit_bcond(MIPS_COND_EQ, r_X, r_zero,
988 b_imm(prog->len, ctx), ctx);
989 emit_load_imm(r_val, 0, ctx); /* delay slot */
990 emit_mod(r_A, r_X, ctx);
991 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200992 case BPF_ALU | BPF_OR | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100993 /* A |= K */
994 ctx->flags |= SEEN_A;
995 emit_ori(r_A, r_A, k, ctx);
996 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200997 case BPF_ALU | BPF_OR | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100998 /* A |= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +0100999 ctx->flags |= SEEN_A;
1000 emit_ori(r_A, r_A, r_X, ctx);
1001 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001002 case BPF_ALU | BPF_XOR | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001003 /* A ^= k */
1004 ctx->flags |= SEEN_A;
1005 emit_xori(r_A, r_A, k, ctx);
1006 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001007 case BPF_ANC | SKF_AD_ALU_XOR_X:
1008 case BPF_ALU | BPF_XOR | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001009 /* A ^= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +01001010 ctx->flags |= SEEN_A;
1011 emit_xor(r_A, r_A, r_X, ctx);
1012 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001013 case BPF_ALU | BPF_AND | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001014 /* A &= K */
1015 ctx->flags |= SEEN_A;
1016 emit_andi(r_A, r_A, k, ctx);
1017 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001018 case BPF_ALU | BPF_AND | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001019 /* A &= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +01001020 ctx->flags |= SEEN_A | SEEN_X;
1021 emit_and(r_A, r_A, r_X, ctx);
1022 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001023 case BPF_ALU | BPF_LSH | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001024 /* A <<= K */
1025 ctx->flags |= SEEN_A;
1026 emit_sll(r_A, r_A, k, ctx);
1027 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001028 case BPF_ALU | BPF_LSH | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001029 /* A <<= X */
1030 ctx->flags |= SEEN_A | SEEN_X;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001031 emit_sllv(r_A, r_A, r_X, ctx);
1032 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001033 case BPF_ALU | BPF_RSH | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001034 /* A >>= K */
1035 ctx->flags |= SEEN_A;
1036 emit_srl(r_A, r_A, k, ctx);
1037 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001038 case BPF_ALU | BPF_RSH | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001039 ctx->flags |= SEEN_A | SEEN_X;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001040 emit_srlv(r_A, r_A, r_X, ctx);
1041 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001042 case BPF_ALU | BPF_NEG:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001043 /* A = -A */
1044 ctx->flags |= SEEN_A;
1045 emit_neg(r_A, ctx);
1046 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001047 case BPF_JMP | BPF_JA:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001048 /* pc += K */
1049 emit_b(b_imm(i + k + 1, ctx), ctx);
1050 emit_nop(ctx);
1051 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001052 case BPF_JMP | BPF_JEQ | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001053 /* pc += ( A == K ) ? pc->jt : pc->jf */
1054 condt = MIPS_COND_EQ | MIPS_COND_K;
1055 goto jmp_cmp;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001056 case BPF_JMP | BPF_JEQ | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001057 ctx->flags |= SEEN_X;
1058 /* pc += ( A == X ) ? pc->jt : pc->jf */
1059 condt = MIPS_COND_EQ | MIPS_COND_X;
1060 goto jmp_cmp;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001061 case BPF_JMP | BPF_JGE | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001062 /* pc += ( A >= K ) ? pc->jt : pc->jf */
1063 condt = MIPS_COND_GE | MIPS_COND_K;
1064 goto jmp_cmp;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001065 case BPF_JMP | BPF_JGE | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001066 ctx->flags |= SEEN_X;
1067 /* pc += ( A >= X ) ? pc->jt : pc->jf */
1068 condt = MIPS_COND_GE | MIPS_COND_X;
1069 goto jmp_cmp;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001070 case BPF_JMP | BPF_JGT | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001071 /* pc += ( A > K ) ? pc->jt : pc->jf */
1072 condt = MIPS_COND_GT | MIPS_COND_K;
1073 goto jmp_cmp;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001074 case BPF_JMP | BPF_JGT | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001075 ctx->flags |= SEEN_X;
1076 /* pc += ( A > X ) ? pc->jt : pc->jf */
1077 condt = MIPS_COND_GT | MIPS_COND_X;
1078jmp_cmp:
1079 /* Greater or Equal */
1080 if ((condt & MIPS_COND_GE) ||
1081 (condt & MIPS_COND_GT)) {
1082 if (condt & MIPS_COND_K) { /* K */
Markos Chandrasad152bd2015-06-04 11:56:11 +01001083 ctx->flags |= SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001084 emit_sltiu(r_s0, r_A, k, ctx);
1085 } else { /* X */
Markos Chandrasad152bd2015-06-04 11:56:11 +01001086 ctx->flags |= SEEN_A |
Markos Chandrasc6610de2014-04-08 12:47:14 +01001087 SEEN_X;
1088 emit_sltu(r_s0, r_A, r_X, ctx);
1089 }
1090 /* A < (K|X) ? r_scrach = 1 */
1091 b_off = b_imm(i + inst->jf + 1, ctx);
Markos Chandras1ab24a42014-06-23 10:38:51 +01001092 emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off,
Markos Chandrasc6610de2014-04-08 12:47:14 +01001093 ctx);
1094 emit_nop(ctx);
1095 /* A > (K|X) ? scratch = 0 */
1096 if (condt & MIPS_COND_GT) {
1097 /* Checking for equality */
Markos Chandrasad152bd2015-06-04 11:56:11 +01001098 ctx->flags |= SEEN_A | SEEN_X;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001099 if (condt & MIPS_COND_K)
1100 emit_load_imm(r_s0, k, ctx);
1101 else
1102 emit_jit_reg_move(r_s0, r_X,
1103 ctx);
1104 b_off = b_imm(i + inst->jf + 1, ctx);
1105 emit_bcond(MIPS_COND_EQ, r_A, r_s0,
1106 b_off, ctx);
1107 emit_nop(ctx);
1108 /* Finally, A > K|X */
1109 b_off = b_imm(i + inst->jt + 1, ctx);
1110 emit_b(b_off, ctx);
1111 emit_nop(ctx);
1112 } else {
1113 /* A >= (K|X) so jump */
1114 b_off = b_imm(i + inst->jt + 1, ctx);
1115 emit_b(b_off, ctx);
1116 emit_nop(ctx);
1117 }
1118 } else {
1119 /* A == K|X */
1120 if (condt & MIPS_COND_K) { /* K */
Markos Chandrasad152bd2015-06-04 11:56:11 +01001121 ctx->flags |= SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001122 emit_load_imm(r_s0, k, ctx);
1123 /* jump true */
1124 b_off = b_imm(i + inst->jt + 1, ctx);
1125 emit_bcond(MIPS_COND_EQ, r_A, r_s0,
1126 b_off, ctx);
1127 emit_nop(ctx);
1128 /* jump false */
1129 b_off = b_imm(i + inst->jf + 1,
1130 ctx);
1131 emit_bcond(MIPS_COND_NE, r_A, r_s0,
1132 b_off, ctx);
1133 emit_nop(ctx);
1134 } else { /* X */
1135 /* jump true */
1136 ctx->flags |= SEEN_A | SEEN_X;
1137 b_off = b_imm(i + inst->jt + 1,
1138 ctx);
1139 emit_bcond(MIPS_COND_EQ, r_A, r_X,
1140 b_off, ctx);
1141 emit_nop(ctx);
1142 /* jump false */
1143 b_off = b_imm(i + inst->jf + 1, ctx);
1144 emit_bcond(MIPS_COND_NE, r_A, r_X,
1145 b_off, ctx);
1146 emit_nop(ctx);
1147 }
1148 }
1149 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001150 case BPF_JMP | BPF_JSET | BPF_K:
Markos Chandrasad152bd2015-06-04 11:56:11 +01001151 ctx->flags |= SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001152 /* pc += (A & K) ? pc -> jt : pc -> jf */
1153 emit_load_imm(r_s1, k, ctx);
1154 emit_and(r_s0, r_A, r_s1, ctx);
1155 /* jump true */
1156 b_off = b_imm(i + inst->jt + 1, ctx);
1157 emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
1158 emit_nop(ctx);
1159 /* jump false */
1160 b_off = b_imm(i + inst->jf + 1, ctx);
1161 emit_b(b_off, ctx);
1162 emit_nop(ctx);
1163 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001164 case BPF_JMP | BPF_JSET | BPF_X:
Markos Chandrasad152bd2015-06-04 11:56:11 +01001165 ctx->flags |= SEEN_X | SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001166 /* pc += (A & X) ? pc -> jt : pc -> jf */
1167 emit_and(r_s0, r_A, r_X, ctx);
1168 /* jump true */
1169 b_off = b_imm(i + inst->jt + 1, ctx);
1170 emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
1171 emit_nop(ctx);
1172 /* jump false */
1173 b_off = b_imm(i + inst->jf + 1, ctx);
1174 emit_b(b_off, ctx);
1175 emit_nop(ctx);
1176 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001177 case BPF_RET | BPF_A:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001178 ctx->flags |= SEEN_A;
1179 if (i != prog->len - 1)
1180 /*
1181 * If this is not the last instruction
1182 * then jump to the epilogue
1183 */
1184 emit_b(b_imm(prog->len, ctx), ctx);
1185 emit_reg_move(r_ret, r_A, ctx); /* delay slot */
1186 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001187 case BPF_RET | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001188 /*
1189 * It can emit two instructions so it does not fit on
1190 * the delay slot.
1191 */
1192 emit_load_imm(r_ret, k, ctx);
1193 if (i != prog->len - 1) {
1194 /*
1195 * If this is not the last instruction
1196 * then jump to the epilogue
1197 */
1198 emit_b(b_imm(prog->len, ctx), ctx);
1199 emit_nop(ctx);
1200 }
1201 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001202 case BPF_MISC | BPF_TAX:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001203 /* X = A */
1204 ctx->flags |= SEEN_X | SEEN_A;
1205 emit_jit_reg_move(r_X, r_A, ctx);
1206 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001207 case BPF_MISC | BPF_TXA:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001208 /* A = X */
1209 ctx->flags |= SEEN_A | SEEN_X;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001210 emit_jit_reg_move(r_A, r_X, ctx);
1211 break;
1212 /* AUX */
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001213 case BPF_ANC | SKF_AD_PROTOCOL:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001214 /* A = ntohs(skb->protocol */
1215 ctx->flags |= SEEN_SKB | SEEN_OFF | SEEN_A;
1216 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1217 protocol) != 2);
1218 off = offsetof(struct sk_buff, protocol);
1219 emit_half_load(r_A, r_skb, off, ctx);
1220#ifdef CONFIG_CPU_LITTLE_ENDIAN
1221 /* This needs little endian fixup */
Chen Jie3c09bae2014-08-15 16:56:58 +08001222 if (cpu_has_wsbh) {
Ralf Baechleb4f16c92014-06-03 13:05:55 +02001223 /* R2 and later have the wsbh instruction */
1224 emit_wsbh(r_A, r_A, ctx);
1225 } else {
Markos Chandrasc6610de2014-04-08 12:47:14 +01001226 /* Get first byte */
1227 emit_andi(r_tmp_imm, r_A, 0xff, ctx);
1228 /* Shift it */
1229 emit_sll(r_tmp, r_tmp_imm, 8, ctx);
1230 /* Get second byte */
1231 emit_srl(r_tmp_imm, r_A, 8, ctx);
1232 emit_andi(r_tmp_imm, r_tmp_imm, 0xff, ctx);
1233 /* Put everyting together in r_A */
1234 emit_or(r_A, r_tmp, r_tmp_imm, ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +01001235 }
1236#endif
1237 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001238 case BPF_ANC | SKF_AD_CPU:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001239 ctx->flags |= SEEN_A | SEEN_OFF;
1240 /* A = current_thread_info()->cpu */
1241 BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info,
1242 cpu) != 4);
1243 off = offsetof(struct thread_info, cpu);
1244 /* $28/gp points to the thread_info struct */
1245 emit_load(r_A, 28, off, ctx);
1246 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001247 case BPF_ANC | SKF_AD_IFINDEX:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001248 /* A = skb->dev->ifindex */
Markos Chandrasad152bd2015-06-04 11:56:11 +01001249 ctx->flags |= SEEN_SKB | SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001250 off = offsetof(struct sk_buff, dev);
Markos Chandrasb6a14a92014-06-25 09:39:38 +01001251 /* Load *dev pointer */
1252 emit_load_ptr(r_s0, r_skb, off, ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +01001253 /* error (0) in the delay slot */
1254 emit_bcond(MIPS_COND_EQ, r_s0, r_zero,
1255 b_imm(prog->len, ctx), ctx);
1256 emit_reg_move(r_ret, r_zero, ctx);
1257 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device,
1258 ifindex) != 4);
1259 off = offsetof(struct net_device, ifindex);
1260 emit_load(r_A, r_s0, off, ctx);
1261 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001262 case BPF_ANC | SKF_AD_MARK:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001263 ctx->flags |= SEEN_SKB | SEEN_A;
1264 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
1265 off = offsetof(struct sk_buff, mark);
1266 emit_load(r_A, r_skb, off, ctx);
1267 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001268 case BPF_ANC | SKF_AD_RXHASH:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001269 ctx->flags |= SEEN_SKB | SEEN_A;
1270 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
1271 off = offsetof(struct sk_buff, hash);
1272 emit_load(r_A, r_skb, off, ctx);
1273 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001274 case BPF_ANC | SKF_AD_VLAN_TAG:
1275 case BPF_ANC | SKF_AD_VLAN_TAG_PRESENT:
Markos Chandrasad152bd2015-06-04 11:56:11 +01001276 ctx->flags |= SEEN_SKB | SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001277 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1278 vlan_tci) != 2);
1279 off = offsetof(struct sk_buff, vlan_tci);
1280 emit_half_load(r_s0, r_skb, off, ctx);
Markos Chandras91a41d72014-06-23 10:38:53 +01001281 if (code == (BPF_ANC | SKF_AD_VLAN_TAG)) {
Markos Chandras6e86c592014-06-23 10:38:52 +01001282 emit_andi(r_A, r_s0, (u16)~VLAN_TAG_PRESENT, ctx);
Markos Chandras91a41d72014-06-23 10:38:53 +01001283 } else {
Markos Chandras9ee16062014-06-23 10:38:49 +01001284 emit_andi(r_A, r_s0, VLAN_TAG_PRESENT, ctx);
Markos Chandras91a41d72014-06-23 10:38:53 +01001285 /* return 1 if present */
1286 emit_sltu(r_A, r_zero, r_A, ctx);
1287 }
Markos Chandrasc6610de2014-04-08 12:47:14 +01001288 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001289 case BPF_ANC | SKF_AD_PKTTYPE:
Markos Chandras9eebfe42014-06-23 10:38:50 +01001290 ctx->flags |= SEEN_SKB;
1291
Hannes Frederic Sowa233577a2014-09-12 14:04:43 +02001292 emit_load_byte(r_tmp, r_skb, PKT_TYPE_OFFSET(), ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +01001293 /* Keep only the last 3 bits */
1294 emit_andi(r_A, r_tmp, PKT_TYPE_MAX, ctx);
Markos Chandrasb4fe0ec2014-06-23 10:38:58 +01001295#ifdef __BIG_ENDIAN_BITFIELD
1296 /* Get the actual packet type to the lower 3 bits */
1297 emit_srl(r_A, r_A, 5, ctx);
1298#endif
Markos Chandrasc6610de2014-04-08 12:47:14 +01001299 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001300 case BPF_ANC | SKF_AD_QUEUE:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001301 ctx->flags |= SEEN_SKB | SEEN_A;
1302 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1303 queue_mapping) != 2);
1304 BUILD_BUG_ON(offsetof(struct sk_buff,
1305 queue_mapping) > 0xff);
1306 off = offsetof(struct sk_buff, queue_mapping);
1307 emit_half_load(r_A, r_skb, off, ctx);
1308 break;
1309 default:
Markos Chandras78b95b62014-06-23 10:38:54 +01001310 pr_debug("%s: Unhandled opcode: 0x%02x\n", __FILE__,
1311 inst->code);
Markos Chandrasc6610de2014-04-08 12:47:14 +01001312 return -1;
1313 }
1314 }
1315
1316 /* compute offsets only during the first pass */
1317 if (ctx->target == NULL)
1318 ctx->offsets[i] = ctx->idx * 4;
1319
1320 return 0;
1321}
1322
1323int bpf_jit_enable __read_mostly;
1324
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001325void bpf_jit_compile(struct bpf_prog *fp)
Markos Chandrasc6610de2014-04-08 12:47:14 +01001326{
1327 struct jit_ctx ctx;
1328 unsigned int alloc_size, tmp_idx;
1329
1330 if (!bpf_jit_enable)
1331 return;
1332
1333 memset(&ctx, 0, sizeof(ctx));
1334
1335 ctx.offsets = kcalloc(fp->len, sizeof(*ctx.offsets), GFP_KERNEL);
1336 if (ctx.offsets == NULL)
1337 return;
1338
1339 ctx.skf = fp;
1340
1341 if (build_body(&ctx))
1342 goto out;
1343
1344 tmp_idx = ctx.idx;
1345 build_prologue(&ctx);
1346 ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
1347 /* just to complete the ctx.idx count */
1348 build_epilogue(&ctx);
1349
1350 alloc_size = 4 * ctx.idx;
1351 ctx.target = module_alloc(alloc_size);
1352 if (ctx.target == NULL)
1353 goto out;
1354
1355 /* Clean it */
1356 memset(ctx.target, 0, alloc_size);
1357
1358 ctx.idx = 0;
1359
1360 /* Generate the actual JIT code */
1361 build_prologue(&ctx);
1362 build_body(&ctx);
1363 build_epilogue(&ctx);
1364
1365 /* Update the icache */
1366 flush_icache_range((ptr)ctx.target, (ptr)(ctx.target + ctx.idx));
1367
1368 if (bpf_jit_enable > 1)
1369 /* Dump JIT code */
1370 bpf_jit_dump(fp->len, alloc_size, 2, ctx.target);
1371
1372 fp->bpf_func = (void *)ctx.target;
Daniel Borkmann286aad32014-09-08 08:04:49 +02001373 fp->jited = true;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001374
1375out:
1376 kfree(ctx.offsets);
1377}
1378
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001379void bpf_jit_free(struct bpf_prog *fp)
Markos Chandrasc6610de2014-04-08 12:47:14 +01001380{
1381 if (fp->jited)
Rusty Russellbe1f2212015-01-20 09:07:05 +10301382 module_memfree(fp->bpf_func);
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001383
1384 bpf_prog_unlock_free(fp);
Markos Chandrasc6610de2014-04-08 12:47:14 +01001385}