blob: f0db4f8310b28c6ccb98fc83f8b17502e2fe5a7b [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>
Markos Chandras5e0c5612015-06-04 11:56:12 +010023#include <asm/asm.h>
Markos Chandrasc6610de2014-04-08 12:47:14 +010024#include <asm/bitops.h>
25#include <asm/cacheflush.h>
26#include <asm/cpu-features.h>
27#include <asm/uasm.h>
28
29#include "bpf_jit.h"
30
31/* ABI
32 *
Markos Chandrasbeaf70b2015-06-04 11:56:15 +010033 * r_A BPF register A
34 * r_X BPF register X
35 * r_skb *skb
36 * r_M *scratch memory
Markos Chandrasc6610de2014-04-08 12:47:14 +010037 *
38 * On entry (*bpf_func)(*skb, *filter)
39 * a0 = MIPS_R_A0 = skb;
40 * a1 = MIPS_R_A1 = filter;
41 *
42 * Stack
43 * ...
44 * M[15]
45 * M[14]
46 * M[13]
47 * ...
48 * M[0] <-- r_M
49 * saved reg k-1
50 * saved reg k-2
51 * ...
52 * saved reg 0 <-- r_sp
53 * <no argument area>
54 *
55 * Packet layout
56 *
57 * <--------------------- len ------------------------>
58 * <--skb-len(r_skb_hl)-->< ----- skb->data_len ------>
59 * ----------------------------------------------------
60 * | skb->data |
61 * ----------------------------------------------------
62 */
63
Markos Chandrasc6610de2014-04-08 12:47:14 +010064#define ptr typeof(unsigned long)
65
Markos Chandrasc6610de2014-04-08 12:47:14 +010066#define SCRATCH_OFF(k) (4 * (k))
67
68/* JIT flags */
69#define SEEN_CALL (1 << BPF_MEMWORDS)
70#define SEEN_SREG_SFT (BPF_MEMWORDS + 1)
71#define SEEN_SREG_BASE (1 << SEEN_SREG_SFT)
72#define SEEN_SREG(x) (SEEN_SREG_BASE << (x))
Markos Chandrasc6610de2014-04-08 12:47:14 +010073#define SEEN_OFF SEEN_SREG(2)
74#define SEEN_A SEEN_SREG(3)
75#define SEEN_X SEEN_SREG(4)
76#define SEEN_SKB SEEN_SREG(5)
77#define SEEN_MEM SEEN_SREG(6)
78
79/* Arguments used by JIT */
80#define ARGS_USED_BY_JIT 2 /* only applicable to 64-bit */
81
Markos Chandrasc6610de2014-04-08 12:47:14 +010082#define SBIT(x) (1 << (x)) /* Signed version of BIT() */
83
84/**
85 * struct jit_ctx - JIT context
86 * @skf: The sk_filter
87 * @prologue_bytes: Number of bytes for prologue
88 * @idx: Instruction index
89 * @flags: JIT flags
90 * @offsets: Instruction offsets
91 * @target: Memory location for the compiled filter
92 */
93struct jit_ctx {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -070094 const struct bpf_prog *skf;
Markos Chandrasc6610de2014-04-08 12:47:14 +010095 unsigned int prologue_bytes;
96 u32 idx;
97 u32 flags;
98 u32 *offsets;
99 u32 *target;
100};
101
102
103static inline int optimize_div(u32 *k)
104{
105 /* power of 2 divides can be implemented with right shift */
106 if (!(*k & (*k-1))) {
107 *k = ilog2(*k);
108 return 1;
109 }
110
111 return 0;
112}
113
Markos Chandras95782bf2014-06-25 09:37:21 +0100114static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx);
115
Markos Chandrasc6610de2014-04-08 12:47:14 +0100116/* Simply emit the instruction if the JIT memory space has been allocated */
117#define emit_instr(ctx, func, ...) \
118do { \
119 if ((ctx)->target != NULL) { \
120 u32 *p = &(ctx)->target[ctx->idx]; \
121 uasm_i_##func(&p, ##__VA_ARGS__); \
122 } \
123 (ctx)->idx++; \
124} while (0)
125
Markos Chandras39bcb792014-07-23 10:00:09 +0100126/*
127 * Similar to emit_instr but it must be used when we need to emit
128 * 32-bit or 64-bit instructions
129 */
130#define emit_long_instr(ctx, func, ...) \
131do { \
132 if ((ctx)->target != NULL) { \
133 u32 *p = &(ctx)->target[ctx->idx]; \
134 UASM_i_##func(&p, ##__VA_ARGS__); \
135 } \
136 (ctx)->idx++; \
137} while (0)
138
Markos Chandrasc6610de2014-04-08 12:47:14 +0100139/* Determine if immediate is within the 16-bit signed range */
140static inline bool is_range16(s32 imm)
141{
Markos Chandras10c4d612014-06-23 10:38:55 +0100142 return !(imm >= SBIT(15) || imm < -SBIT(15));
Markos Chandrasc6610de2014-04-08 12:47:14 +0100143}
144
145static inline void emit_addu(unsigned int dst, unsigned int src1,
146 unsigned int src2, struct jit_ctx *ctx)
147{
148 emit_instr(ctx, addu, dst, src1, src2);
149}
150
151static inline void emit_nop(struct jit_ctx *ctx)
152{
153 emit_instr(ctx, nop);
154}
155
156/* Load a u32 immediate to a register */
157static inline void emit_load_imm(unsigned int dst, u32 imm, struct jit_ctx *ctx)
158{
159 if (ctx->target != NULL) {
160 /* addiu can only handle s16 */
Markos Chandras10c4d612014-06-23 10:38:55 +0100161 if (!is_range16(imm)) {
Markos Chandrasc6610de2014-04-08 12:47:14 +0100162 u32 *p = &ctx->target[ctx->idx];
163 uasm_i_lui(&p, r_tmp_imm, (s32)imm >> 16);
164 p = &ctx->target[ctx->idx + 1];
165 uasm_i_ori(&p, dst, r_tmp_imm, imm & 0xffff);
166 } else {
167 u32 *p = &ctx->target[ctx->idx];
168 uasm_i_addiu(&p, dst, r_zero, imm);
169 }
170 }
171 ctx->idx++;
172
Markos Chandras10c4d612014-06-23 10:38:55 +0100173 if (!is_range16(imm))
Markos Chandrasc6610de2014-04-08 12:47:14 +0100174 ctx->idx++;
175}
176
177static inline void emit_or(unsigned int dst, unsigned int src1,
178 unsigned int src2, struct jit_ctx *ctx)
179{
180 emit_instr(ctx, or, dst, src1, src2);
181}
182
183static inline void emit_ori(unsigned int dst, unsigned src, u32 imm,
184 struct jit_ctx *ctx)
185{
186 if (imm >= BIT(16)) {
187 emit_load_imm(r_tmp, imm, ctx);
188 emit_or(dst, src, r_tmp, ctx);
189 } else {
190 emit_instr(ctx, ori, dst, src, imm);
191 }
192}
193
Markos Chandrasc6610de2014-04-08 12:47:14 +0100194static inline void emit_daddiu(unsigned int dst, unsigned int src,
195 int imm, struct jit_ctx *ctx)
196{
197 /*
198 * Only used for stack, so the imm is relatively small
199 * and it fits in 15-bits
200 */
201 emit_instr(ctx, daddiu, dst, src, imm);
202}
203
204static inline void emit_addiu(unsigned int dst, unsigned int src,
205 u32 imm, struct jit_ctx *ctx)
206{
Markos Chandras10c4d612014-06-23 10:38:55 +0100207 if (!is_range16(imm)) {
Markos Chandrasc6610de2014-04-08 12:47:14 +0100208 emit_load_imm(r_tmp, imm, ctx);
209 emit_addu(dst, r_tmp, src, ctx);
210 } else {
211 emit_instr(ctx, addiu, dst, src, imm);
212 }
213}
214
215static inline void emit_and(unsigned int dst, unsigned int src1,
216 unsigned int src2, struct jit_ctx *ctx)
217{
218 emit_instr(ctx, and, dst, src1, src2);
219}
220
221static inline void emit_andi(unsigned int dst, unsigned int src,
222 u32 imm, struct jit_ctx *ctx)
223{
224 /* If imm does not fit in u16 then load it to register */
225 if (imm >= BIT(16)) {
226 emit_load_imm(r_tmp, imm, ctx);
227 emit_and(dst, src, r_tmp, ctx);
228 } else {
229 emit_instr(ctx, andi, dst, src, imm);
230 }
231}
232
233static inline void emit_xor(unsigned int dst, unsigned int src1,
234 unsigned int src2, struct jit_ctx *ctx)
235{
236 emit_instr(ctx, xor, dst, src1, src2);
237}
238
239static inline void emit_xori(ptr dst, ptr src, u32 imm, struct jit_ctx *ctx)
240{
241 /* If imm does not fit in u16 then load it to register */
242 if (imm >= BIT(16)) {
243 emit_load_imm(r_tmp, imm, ctx);
244 emit_xor(dst, src, r_tmp, ctx);
245 } else {
246 emit_instr(ctx, xori, dst, src, imm);
247 }
248}
249
250static inline void emit_stack_offset(int offset, struct jit_ctx *ctx)
251{
Markos Chandras39bcb792014-07-23 10:00:09 +0100252 emit_long_instr(ctx, ADDIU, r_sp, r_sp, offset);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100253}
254
255static inline void emit_subu(unsigned int dst, unsigned int src1,
256 unsigned int src2, struct jit_ctx *ctx)
257{
258 emit_instr(ctx, subu, dst, src1, src2);
259}
260
261static inline void emit_neg(unsigned int reg, struct jit_ctx *ctx)
262{
263 emit_subu(reg, r_zero, reg, ctx);
264}
265
266static inline void emit_sllv(unsigned int dst, unsigned int src,
267 unsigned int sa, struct jit_ctx *ctx)
268{
269 emit_instr(ctx, sllv, dst, src, sa);
270}
271
272static inline void emit_sll(unsigned int dst, unsigned int src,
273 unsigned int sa, struct jit_ctx *ctx)
274{
275 /* sa is 5-bits long */
Markos Chandras95782bf2014-06-25 09:37:21 +0100276 if (sa >= BIT(5))
277 /* Shifting >= 32 results in zero */
278 emit_jit_reg_move(dst, r_zero, ctx);
279 else
280 emit_instr(ctx, sll, dst, src, sa);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100281}
282
283static inline void emit_srlv(unsigned int dst, unsigned int src,
284 unsigned int sa, struct jit_ctx *ctx)
285{
286 emit_instr(ctx, srlv, dst, src, sa);
287}
288
289static inline void emit_srl(unsigned int dst, unsigned int src,
290 unsigned int sa, struct jit_ctx *ctx)
291{
292 /* sa is 5-bits long */
Markos Chandras95782bf2014-06-25 09:37:21 +0100293 if (sa >= BIT(5))
294 /* Shifting >= 32 results in zero */
295 emit_jit_reg_move(dst, r_zero, ctx);
296 else
297 emit_instr(ctx, srl, dst, src, sa);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100298}
299
Markos Chandras55393ee2014-06-23 10:38:48 +0100300static inline void emit_slt(unsigned int dst, unsigned int src1,
301 unsigned int src2, struct jit_ctx *ctx)
302{
303 emit_instr(ctx, slt, dst, src1, src2);
304}
305
Markos Chandrasc6610de2014-04-08 12:47:14 +0100306static inline void emit_sltu(unsigned int dst, unsigned int src1,
307 unsigned int src2, struct jit_ctx *ctx)
308{
309 emit_instr(ctx, sltu, dst, src1, src2);
310}
311
312static inline void emit_sltiu(unsigned dst, unsigned int src,
313 unsigned int imm, struct jit_ctx *ctx)
314{
315 /* 16 bit immediate */
Markos Chandras10c4d612014-06-23 10:38:55 +0100316 if (!is_range16((s32)imm)) {
Markos Chandrasc6610de2014-04-08 12:47:14 +0100317 emit_load_imm(r_tmp, imm, ctx);
318 emit_sltu(dst, src, r_tmp, ctx);
319 } else {
320 emit_instr(ctx, sltiu, dst, src, imm);
321 }
322
323}
324
325/* Store register on the stack */
326static inline void emit_store_stack_reg(ptr reg, ptr base,
327 unsigned int offset,
328 struct jit_ctx *ctx)
329{
Markos Chandras39bcb792014-07-23 10:00:09 +0100330 emit_long_instr(ctx, SW, reg, offset, base);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100331}
332
333static inline void emit_store(ptr reg, ptr base, unsigned int offset,
334 struct jit_ctx *ctx)
335{
336 emit_instr(ctx, sw, reg, offset, base);
337}
338
339static inline void emit_load_stack_reg(ptr reg, ptr base,
340 unsigned int offset,
341 struct jit_ctx *ctx)
342{
Markos Chandras39bcb792014-07-23 10:00:09 +0100343 emit_long_instr(ctx, LW, reg, offset, base);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100344}
345
346static inline void emit_load(unsigned int reg, unsigned int base,
347 unsigned int offset, struct jit_ctx *ctx)
348{
349 emit_instr(ctx, lw, reg, offset, base);
350}
351
352static inline void emit_load_byte(unsigned int reg, unsigned int base,
353 unsigned int offset, struct jit_ctx *ctx)
354{
355 emit_instr(ctx, lb, reg, offset, base);
356}
357
358static inline void emit_half_load(unsigned int reg, unsigned int base,
359 unsigned int offset, struct jit_ctx *ctx)
360{
361 emit_instr(ctx, lh, reg, offset, base);
362}
363
364static inline void emit_mul(unsigned int dst, unsigned int src1,
365 unsigned int src2, struct jit_ctx *ctx)
366{
367 emit_instr(ctx, mul, dst, src1, src2);
368}
369
370static inline void emit_div(unsigned int dst, unsigned int src,
371 struct jit_ctx *ctx)
372{
373 if (ctx->target != NULL) {
374 u32 *p = &ctx->target[ctx->idx];
375 uasm_i_divu(&p, dst, src);
376 p = &ctx->target[ctx->idx + 1];
Markos Chandras35a8e162014-06-23 10:38:47 +0100377 uasm_i_mflo(&p, dst);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100378 }
379 ctx->idx += 2; /* 2 insts */
380}
381
382static inline void emit_mod(unsigned int dst, unsigned int src,
383 struct jit_ctx *ctx)
384{
385 if (ctx->target != NULL) {
386 u32 *p = &ctx->target[ctx->idx];
387 uasm_i_divu(&p, dst, src);
388 p = &ctx->target[ctx->idx + 1];
Denis Kirjanov2e464772014-12-01 12:57:02 +0300389 uasm_i_mfhi(&p, dst);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100390 }
391 ctx->idx += 2; /* 2 insts */
392}
393
394static inline void emit_dsll(unsigned int dst, unsigned int src,
395 unsigned int sa, struct jit_ctx *ctx)
396{
397 emit_instr(ctx, dsll, dst, src, sa);
398}
399
400static inline void emit_dsrl32(unsigned int dst, unsigned int src,
401 unsigned int sa, struct jit_ctx *ctx)
402{
403 emit_instr(ctx, dsrl32, dst, src, sa);
404}
405
406static inline void emit_wsbh(unsigned int dst, unsigned int src,
407 struct jit_ctx *ctx)
408{
409 emit_instr(ctx, wsbh, dst, src);
410}
411
Markos Chandrasb6a14a92014-06-25 09:39:38 +0100412/* load pointer to register */
413static inline void emit_load_ptr(unsigned int dst, unsigned int src,
414 int imm, struct jit_ctx *ctx)
415{
416 /* src contains the base addr of the 32/64-pointer */
Markos Chandras39bcb792014-07-23 10:00:09 +0100417 emit_long_instr(ctx, LW, dst, imm, src);
Markos Chandrasb6a14a92014-06-25 09:39:38 +0100418}
419
Markos Chandrasc6610de2014-04-08 12:47:14 +0100420/* load a function pointer to register */
421static inline void emit_load_func(unsigned int reg, ptr imm,
422 struct jit_ctx *ctx)
423{
424 if (config_enabled(CONFIG_64BIT)) {
425 /* At this point imm is always 64-bit */
426 emit_load_imm(r_tmp, (u64)imm >> 32, ctx);
427 emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */
428 emit_ori(r_tmp, r_tmp_imm, (imm >> 16) & 0xffff, ctx);
429 emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */
430 emit_ori(reg, r_tmp_imm, imm & 0xffff, ctx);
431 } else {
432 emit_load_imm(reg, imm, ctx);
433 }
434}
435
436/* Move to real MIPS register */
437static inline void emit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
438{
Markos Chandras39bcb792014-07-23 10:00:09 +0100439 emit_long_instr(ctx, ADDU, dst, src, r_zero);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100440}
441
442/* Move to JIT (32-bit) register */
443static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
444{
445 emit_addu(dst, src, r_zero, ctx);
446}
447
448/* Compute the immediate value for PC-relative branches. */
449static inline u32 b_imm(unsigned int tgt, struct jit_ctx *ctx)
450{
451 if (ctx->target == NULL)
452 return 0;
453
454 /*
455 * We want a pc-relative branch. We only do forward branches
456 * so tgt is always after pc. tgt is the instruction offset
457 * we want to jump to.
458
459 * Branch on MIPS:
460 * I: target_offset <- sign_extend(offset)
461 * I+1: PC += target_offset (delay slot)
462 *
463 * ctx->idx currently points to the branch instruction
464 * but the offset is added to the delay slot so we need
465 * to subtract 4.
466 */
467 return ctx->offsets[tgt] -
468 (ctx->idx * 4 - ctx->prologue_bytes) - 4;
469}
470
471static inline void emit_bcond(int cond, unsigned int reg1, unsigned int reg2,
472 unsigned int imm, struct jit_ctx *ctx)
473{
474 if (ctx->target != NULL) {
475 u32 *p = &ctx->target[ctx->idx];
476
477 switch (cond) {
478 case MIPS_COND_EQ:
479 uasm_i_beq(&p, reg1, reg2, imm);
480 break;
481 case MIPS_COND_NE:
482 uasm_i_bne(&p, reg1, reg2, imm);
483 break;
484 case MIPS_COND_ALL:
485 uasm_i_b(&p, imm);
486 break;
487 default:
488 pr_warn("%s: Unhandled branch conditional: %d\n",
489 __func__, cond);
490 }
491 }
492 ctx->idx++;
493}
494
495static inline void emit_b(unsigned int imm, struct jit_ctx *ctx)
496{
497 emit_bcond(MIPS_COND_ALL, r_zero, r_zero, imm, ctx);
498}
499
500static inline void emit_jalr(unsigned int link, unsigned int reg,
501 struct jit_ctx *ctx)
502{
503 emit_instr(ctx, jalr, link, reg);
504}
505
506static inline void emit_jr(unsigned int reg, struct jit_ctx *ctx)
507{
508 emit_instr(ctx, jr, reg);
509}
510
511static inline u16 align_sp(unsigned int num)
512{
513 /* Double word alignment for 32-bit, quadword for 64-bit */
514 unsigned int align = config_enabled(CONFIG_64BIT) ? 16 : 8;
515 num = (num + (align - 1)) & -align;
516 return num;
517}
518
Markos Chandrasc6610de2014-04-08 12:47:14 +0100519static bool is_load_to_a(u16 inst)
520{
521 switch (inst) {
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200522 case BPF_LD | BPF_W | BPF_LEN:
523 case BPF_LD | BPF_W | BPF_ABS:
524 case BPF_LD | BPF_H | BPF_ABS:
525 case BPF_LD | BPF_B | BPF_ABS:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100526 return true;
527 default:
528 return false;
529 }
530}
531
532static void save_bpf_jit_regs(struct jit_ctx *ctx, unsigned offset)
533{
534 int i = 0, real_off = 0;
535 u32 sflags, tmp_flags;
536
537 /* Adjust the stack pointer */
538 emit_stack_offset(-align_sp(offset), ctx);
539
540 if (ctx->flags & SEEN_CALL) {
541 /* Argument save area */
542 if (config_enabled(CONFIG_64BIT))
543 /* Bottom of current frame */
Markos Chandras5e0c5612015-06-04 11:56:12 +0100544 real_off = align_sp(offset) - SZREG;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100545 else
546 /* Top of previous frame */
Markos Chandras5e0c5612015-06-04 11:56:12 +0100547 real_off = align_sp(offset) + SZREG;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100548 emit_store_stack_reg(MIPS_R_A0, r_sp, real_off, ctx);
Markos Chandras5e0c5612015-06-04 11:56:12 +0100549 emit_store_stack_reg(MIPS_R_A1, r_sp, real_off + SZREG, ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100550
551 real_off = 0;
552 }
553
554 tmp_flags = sflags = ctx->flags >> SEEN_SREG_SFT;
555 /* sflags is essentially a bitmap */
556 while (tmp_flags) {
557 if ((sflags >> i) & 0x1) {
558 emit_store_stack_reg(MIPS_R_S0 + i, r_sp, real_off,
559 ctx);
Markos Chandras5e0c5612015-06-04 11:56:12 +0100560 real_off += SZREG;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100561 }
562 i++;
563 tmp_flags >>= 1;
564 }
565
566 /* save return address */
567 if (ctx->flags & SEEN_CALL) {
568 emit_store_stack_reg(r_ra, r_sp, real_off, ctx);
Markos Chandras5e0c5612015-06-04 11:56:12 +0100569 real_off += SZREG;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100570 }
571
572 /* Setup r_M leaving the alignment gap if necessary */
573 if (ctx->flags & SEEN_MEM) {
Markos Chandras5e0c5612015-06-04 11:56:12 +0100574 if (real_off % (SZREG * 2))
575 real_off += SZREG;
Markos Chandras39bcb792014-07-23 10:00:09 +0100576 emit_long_instr(ctx, ADDIU, r_M, r_sp, real_off);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100577 }
578}
579
580static void restore_bpf_jit_regs(struct jit_ctx *ctx,
581 unsigned int offset)
582{
583 int i, real_off = 0;
584 u32 sflags, tmp_flags;
585
586 if (ctx->flags & SEEN_CALL) {
587 if (config_enabled(CONFIG_64BIT))
588 /* Bottom of current frame */
Markos Chandras5e0c5612015-06-04 11:56:12 +0100589 real_off = align_sp(offset) - SZREG;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100590 else
591 /* Top of previous frame */
Markos Chandras5e0c5612015-06-04 11:56:12 +0100592 real_off = align_sp(offset) + SZREG;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100593 emit_load_stack_reg(MIPS_R_A0, r_sp, real_off, ctx);
Markos Chandras5e0c5612015-06-04 11:56:12 +0100594 emit_load_stack_reg(MIPS_R_A1, r_sp, real_off + SZREG, ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100595
596 real_off = 0;
597 }
598
599 tmp_flags = sflags = ctx->flags >> SEEN_SREG_SFT;
600 /* sflags is a bitmap */
601 i = 0;
602 while (tmp_flags) {
603 if ((sflags >> i) & 0x1) {
604 emit_load_stack_reg(MIPS_R_S0 + i, r_sp, real_off,
605 ctx);
Markos Chandras5e0c5612015-06-04 11:56:12 +0100606 real_off += SZREG;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100607 }
608 i++;
609 tmp_flags >>= 1;
610 }
611
612 /* restore return address */
613 if (ctx->flags & SEEN_CALL)
614 emit_load_stack_reg(r_ra, r_sp, real_off, ctx);
615
616 /* Restore the sp and discard the scrach memory */
617 emit_stack_offset(align_sp(offset), ctx);
618}
619
620static unsigned int get_stack_depth(struct jit_ctx *ctx)
621{
622 int sp_off = 0;
623
624
625 /* How may s* regs do we need to preserved? */
Markos Chandras5e0c5612015-06-04 11:56:12 +0100626 sp_off += hweight32(ctx->flags >> SEEN_SREG_SFT) * SZREG;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100627
628 if (ctx->flags & SEEN_MEM)
629 sp_off += 4 * BPF_MEMWORDS; /* BPF_MEMWORDS are 32-bit */
630
631 if (ctx->flags & SEEN_CALL)
632 /*
633 * The JIT code make calls to external functions using 2
634 * arguments. Therefore, for o32 we don't need to allocate
635 * space because we don't care if the argumetns are lost
636 * across calls. We do need however to preserve incoming
637 * arguments but the space is already allocated for us by
638 * the caller. On the other hand, for n64, we need to allocate
639 * this space ourselves. We need to preserve $ra as well.
640 */
641 sp_off += config_enabled(CONFIG_64BIT) ?
Markos Chandras5e0c5612015-06-04 11:56:12 +0100642 (ARGS_USED_BY_JIT + 1) * SZREG : SZREG;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100643
Markos Chandras8833bc32015-06-04 11:56:13 +0100644 return sp_off;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100645}
646
647static void build_prologue(struct jit_ctx *ctx)
648{
649 u16 first_inst = ctx->skf->insns[0].code;
650 int sp_off;
651
652 /* Calculate the total offset for the stack pointer */
653 sp_off = get_stack_depth(ctx);
654 save_bpf_jit_regs(ctx, sp_off);
655
656 if (ctx->flags & SEEN_SKB)
657 emit_reg_move(r_skb, MIPS_R_A0, ctx);
658
Markos Chandrase5bb48b2014-06-23 10:38:56 +0100659 if (ctx->flags & SEEN_X)
Markos Chandrasc6610de2014-04-08 12:47:14 +0100660 emit_jit_reg_move(r_X, r_zero, ctx);
661
662 /* Do not leak kernel data to userspace */
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200663 if ((first_inst != (BPF_RET | BPF_K)) && !(is_load_to_a(first_inst)))
Markos Chandrasc6610de2014-04-08 12:47:14 +0100664 emit_jit_reg_move(r_A, r_zero, ctx);
665}
666
667static void build_epilogue(struct jit_ctx *ctx)
668{
669 unsigned int sp_off;
670
671 /* Calculate the total offset for the stack pointer */
672
673 sp_off = get_stack_depth(ctx);
674 restore_bpf_jit_regs(ctx, sp_off);
675
676 /* Return */
677 emit_jr(r_ra, ctx);
678 emit_nop(ctx);
679}
680
681static u64 jit_get_skb_b(struct sk_buff *skb, unsigned offset)
682{
683 u8 ret;
684 int err;
685
686 err = skb_copy_bits(skb, offset, &ret, 1);
687
688 return (u64)err << 32 | ret;
689}
690
691static u64 jit_get_skb_h(struct sk_buff *skb, unsigned offset)
692{
693 u16 ret;
694 int err;
695
696 err = skb_copy_bits(skb, offset, &ret, 2);
697
698 return (u64)err << 32 | ntohs(ret);
699}
700
701static u64 jit_get_skb_w(struct sk_buff *skb, unsigned offset)
702{
703 u32 ret;
704 int err;
705
706 err = skb_copy_bits(skb, offset, &ret, 4);
707
708 return (u64)err << 32 | ntohl(ret);
709}
710
Markos Chandrasc6610de2014-04-08 12:47:14 +0100711static int build_body(struct jit_ctx *ctx)
712{
713 void *load_func[] = {jit_get_skb_b, jit_get_skb_h, jit_get_skb_w};
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700714 const struct bpf_prog *prog = ctx->skf;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100715 const struct sock_filter *inst;
716 unsigned int i, off, load_order, condt;
717 u32 k, b_off __maybe_unused;
718
719 for (i = 0; i < prog->len; i++) {
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200720 u16 code;
721
Markos Chandrasc6610de2014-04-08 12:47:14 +0100722 inst = &(prog->insns[i]);
723 pr_debug("%s: code->0x%02x, jt->0x%x, jf->0x%x, k->0x%x\n",
724 __func__, inst->code, inst->jt, inst->jf, inst->k);
725 k = inst->k;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200726 code = bpf_anc_helper(inst);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100727
728 if (ctx->target == NULL)
729 ctx->offsets[i] = ctx->idx * 4;
730
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200731 switch (code) {
732 case BPF_LD | BPF_IMM:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100733 /* A <- k ==> li r_A, k */
734 ctx->flags |= SEEN_A;
735 emit_load_imm(r_A, k, ctx);
736 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200737 case BPF_LD | BPF_W | BPF_LEN:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100738 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
739 /* A <- len ==> lw r_A, offset(skb) */
740 ctx->flags |= SEEN_SKB | SEEN_A;
741 off = offsetof(struct sk_buff, len);
742 emit_load(r_A, r_skb, off, ctx);
743 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200744 case BPF_LD | BPF_MEM:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100745 /* A <- M[k] ==> lw r_A, offset(M) */
746 ctx->flags |= SEEN_MEM | SEEN_A;
747 emit_load(r_A, r_M, SCRATCH_OFF(k), ctx);
748 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200749 case BPF_LD | BPF_W | BPF_ABS:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100750 /* A <- P[k:4] */
751 load_order = 2;
752 goto load;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200753 case BPF_LD | BPF_H | BPF_ABS:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100754 /* A <- P[k:2] */
755 load_order = 1;
756 goto load;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200757 case BPF_LD | BPF_B | BPF_ABS:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100758 /* A <- P[k:1] */
759 load_order = 0;
760load:
Markos Chandras55393ee2014-06-23 10:38:48 +0100761 /* the interpreter will deal with the negative K */
762 if ((int)k < 0)
763 return -ENOTSUPP;
764
Markos Chandrasc6610de2014-04-08 12:47:14 +0100765 emit_load_imm(r_off, k, ctx);
766load_common:
Markos Chandras55393ee2014-06-23 10:38:48 +0100767 /*
768 * We may got here from the indirect loads so
769 * return if offset is negative.
770 */
771 emit_slt(r_s0, r_off, r_zero, ctx);
772 emit_bcond(MIPS_COND_NE, r_s0, r_zero,
773 b_imm(prog->len, ctx), ctx);
774 emit_reg_move(r_ret, r_zero, ctx);
775
Markos Chandrasad152bd2015-06-04 11:56:11 +0100776 ctx->flags |= SEEN_CALL | SEEN_OFF |
Markos Chandrasc6610de2014-04-08 12:47:14 +0100777 SEEN_SKB | SEEN_A;
778
779 emit_load_func(r_s0, (ptr)load_func[load_order],
780 ctx);
781 emit_reg_move(MIPS_R_A0, r_skb, ctx);
782 emit_jalr(MIPS_R_RA, r_s0, ctx);
783 /* Load second argument to delay slot */
784 emit_reg_move(MIPS_R_A1, r_off, ctx);
785 /* Check the error value */
786 if (config_enabled(CONFIG_64BIT)) {
787 /* Get error code from the top 32-bits */
788 emit_dsrl32(r_s0, r_val, 0, ctx);
789 /* Branch to 3 instructions ahead */
790 emit_bcond(MIPS_COND_NE, r_s0, r_zero, 3 << 2,
791 ctx);
792 } else {
793 /* Branch to 3 instructions ahead */
794 emit_bcond(MIPS_COND_NE, r_err, r_zero, 3 << 2,
795 ctx);
796 }
797 emit_nop(ctx);
798 /* We are good */
799 emit_b(b_imm(i + 1, ctx), ctx);
800 emit_jit_reg_move(r_A, r_val, ctx);
801 /* Return with error */
802 emit_b(b_imm(prog->len, ctx), ctx);
803 emit_reg_move(r_ret, r_zero, ctx);
804 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200805 case BPF_LD | BPF_W | BPF_IND:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100806 /* A <- P[X + k:4] */
807 load_order = 2;
808 goto load_ind;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200809 case BPF_LD | BPF_H | BPF_IND:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100810 /* A <- P[X + k:2] */
811 load_order = 1;
812 goto load_ind;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200813 case BPF_LD | BPF_B | BPF_IND:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100814 /* A <- P[X + k:1] */
815 load_order = 0;
816load_ind:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100817 ctx->flags |= SEEN_OFF | SEEN_X;
818 emit_addiu(r_off, r_X, k, ctx);
819 goto load_common;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200820 case BPF_LDX | BPF_IMM:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100821 /* X <- k */
822 ctx->flags |= SEEN_X;
823 emit_load_imm(r_X, k, ctx);
824 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200825 case BPF_LDX | BPF_MEM:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100826 /* X <- M[k] */
827 ctx->flags |= SEEN_X | SEEN_MEM;
828 emit_load(r_X, r_M, SCRATCH_OFF(k), ctx);
829 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200830 case BPF_LDX | BPF_W | BPF_LEN:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100831 /* X <- len */
832 ctx->flags |= SEEN_X | SEEN_SKB;
833 off = offsetof(struct sk_buff, len);
834 emit_load(r_X, r_skb, off, ctx);
835 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200836 case BPF_LDX | BPF_B | BPF_MSH:
Markos Chandras55393ee2014-06-23 10:38:48 +0100837 /* the interpreter will deal with the negative K */
838 if ((int)k < 0)
839 return -ENOTSUPP;
840
Markos Chandrasc6610de2014-04-08 12:47:14 +0100841 /* X <- 4 * (P[k:1] & 0xf) */
Markos Chandrasad152bd2015-06-04 11:56:11 +0100842 ctx->flags |= SEEN_X | SEEN_CALL | SEEN_SKB;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100843 /* Load offset to a1 */
844 emit_load_func(r_s0, (ptr)jit_get_skb_b, ctx);
845 /*
846 * This may emit two instructions so it may not fit
847 * in the delay slot. So use a0 in the delay slot.
848 */
849 emit_load_imm(MIPS_R_A1, k, ctx);
850 emit_jalr(MIPS_R_RA, r_s0, ctx);
851 emit_reg_move(MIPS_R_A0, r_skb, ctx); /* delay slot */
852 /* Check the error value */
853 if (config_enabled(CONFIG_64BIT)) {
854 /* Top 32-bits of $v0 on 64-bit */
855 emit_dsrl32(r_s0, r_val, 0, ctx);
856 emit_bcond(MIPS_COND_NE, r_s0, r_zero,
857 3 << 2, ctx);
858 } else {
859 emit_bcond(MIPS_COND_NE, r_err, r_zero,
860 3 << 2, ctx);
861 }
862 /* No need for delay slot */
863 /* We are good */
864 /* X <- P[1:K] & 0xf */
865 emit_andi(r_X, r_val, 0xf, ctx);
866 /* X << 2 */
867 emit_b(b_imm(i + 1, ctx), ctx);
868 emit_sll(r_X, r_X, 2, ctx); /* delay slot */
869 /* Return with error */
870 emit_b(b_imm(prog->len, ctx), ctx);
871 emit_load_imm(r_ret, 0, ctx); /* delay slot */
872 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200873 case BPF_ST:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100874 /* M[k] <- A */
875 ctx->flags |= SEEN_MEM | SEEN_A;
876 emit_store(r_A, r_M, SCRATCH_OFF(k), ctx);
877 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200878 case BPF_STX:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100879 /* M[k] <- X */
880 ctx->flags |= SEEN_MEM | SEEN_X;
881 emit_store(r_X, r_M, SCRATCH_OFF(k), ctx);
882 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200883 case BPF_ALU | BPF_ADD | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100884 /* A += K */
885 ctx->flags |= SEEN_A;
886 emit_addiu(r_A, r_A, k, ctx);
887 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200888 case BPF_ALU | BPF_ADD | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100889 /* A += X */
890 ctx->flags |= SEEN_A | SEEN_X;
891 emit_addu(r_A, r_A, r_X, ctx);
892 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200893 case BPF_ALU | BPF_SUB | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100894 /* A -= K */
895 ctx->flags |= SEEN_A;
896 emit_addiu(r_A, r_A, -k, ctx);
897 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200898 case BPF_ALU | BPF_SUB | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100899 /* A -= X */
900 ctx->flags |= SEEN_A | SEEN_X;
901 emit_subu(r_A, r_A, r_X, ctx);
902 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200903 case BPF_ALU | BPF_MUL | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100904 /* A *= K */
905 /* Load K to scratch register before MUL */
Markos Chandrasad152bd2015-06-04 11:56:11 +0100906 ctx->flags |= SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100907 emit_load_imm(r_s0, k, ctx);
908 emit_mul(r_A, r_A, r_s0, ctx);
909 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200910 case BPF_ALU | BPF_MUL | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100911 /* A *= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +0100912 ctx->flags |= SEEN_A | SEEN_X;
913 emit_mul(r_A, r_A, r_X, ctx);
914 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200915 case BPF_ALU | BPF_DIV | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100916 /* A /= k */
917 if (k == 1)
918 break;
919 if (optimize_div(&k)) {
920 ctx->flags |= SEEN_A;
921 emit_srl(r_A, r_A, k, ctx);
922 break;
923 }
Markos Chandrasad152bd2015-06-04 11:56:11 +0100924 ctx->flags |= SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100925 emit_load_imm(r_s0, k, ctx);
926 emit_div(r_A, r_s0, ctx);
927 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200928 case BPF_ALU | BPF_MOD | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100929 /* A %= k */
Denis Kirjanov2e464772014-12-01 12:57:02 +0300930 if (k == 1) {
Markos Chandrasc6610de2014-04-08 12:47:14 +0100931 ctx->flags |= SEEN_A;
932 emit_jit_reg_move(r_A, r_zero, ctx);
933 } else {
Markos Chandrasad152bd2015-06-04 11:56:11 +0100934 ctx->flags |= SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100935 emit_load_imm(r_s0, k, ctx);
936 emit_mod(r_A, r_s0, ctx);
937 }
938 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200939 case BPF_ALU | BPF_DIV | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100940 /* A /= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +0100941 ctx->flags |= SEEN_X | SEEN_A;
942 /* Check if r_X is zero */
943 emit_bcond(MIPS_COND_EQ, r_X, r_zero,
944 b_imm(prog->len, ctx), ctx);
945 emit_load_imm(r_val, 0, ctx); /* delay slot */
946 emit_div(r_A, r_X, ctx);
947 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200948 case BPF_ALU | BPF_MOD | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100949 /* A %= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +0100950 ctx->flags |= SEEN_X | SEEN_A;
951 /* Check if r_X is zero */
952 emit_bcond(MIPS_COND_EQ, r_X, r_zero,
953 b_imm(prog->len, ctx), ctx);
954 emit_load_imm(r_val, 0, ctx); /* delay slot */
955 emit_mod(r_A, r_X, ctx);
956 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200957 case BPF_ALU | BPF_OR | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100958 /* A |= K */
959 ctx->flags |= SEEN_A;
960 emit_ori(r_A, r_A, k, ctx);
961 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200962 case BPF_ALU | BPF_OR | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100963 /* A |= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +0100964 ctx->flags |= SEEN_A;
965 emit_ori(r_A, r_A, r_X, ctx);
966 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200967 case BPF_ALU | BPF_XOR | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100968 /* A ^= k */
969 ctx->flags |= SEEN_A;
970 emit_xori(r_A, r_A, k, ctx);
971 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200972 case BPF_ANC | SKF_AD_ALU_XOR_X:
973 case BPF_ALU | BPF_XOR | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100974 /* A ^= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +0100975 ctx->flags |= SEEN_A;
976 emit_xor(r_A, r_A, r_X, ctx);
977 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200978 case BPF_ALU | BPF_AND | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100979 /* A &= K */
980 ctx->flags |= SEEN_A;
981 emit_andi(r_A, r_A, k, ctx);
982 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200983 case BPF_ALU | BPF_AND | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100984 /* A &= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +0100985 ctx->flags |= SEEN_A | SEEN_X;
986 emit_and(r_A, r_A, r_X, ctx);
987 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200988 case BPF_ALU | BPF_LSH | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100989 /* A <<= K */
990 ctx->flags |= SEEN_A;
991 emit_sll(r_A, r_A, k, ctx);
992 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200993 case BPF_ALU | BPF_LSH | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100994 /* A <<= X */
995 ctx->flags |= SEEN_A | SEEN_X;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100996 emit_sllv(r_A, r_A, r_X, ctx);
997 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200998 case BPF_ALU | BPF_RSH | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100999 /* A >>= K */
1000 ctx->flags |= SEEN_A;
1001 emit_srl(r_A, r_A, k, ctx);
1002 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001003 case BPF_ALU | BPF_RSH | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001004 ctx->flags |= SEEN_A | SEEN_X;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001005 emit_srlv(r_A, r_A, r_X, ctx);
1006 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001007 case BPF_ALU | BPF_NEG:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001008 /* A = -A */
1009 ctx->flags |= SEEN_A;
1010 emit_neg(r_A, ctx);
1011 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001012 case BPF_JMP | BPF_JA:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001013 /* pc += K */
1014 emit_b(b_imm(i + k + 1, ctx), ctx);
1015 emit_nop(ctx);
1016 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001017 case BPF_JMP | BPF_JEQ | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001018 /* pc += ( A == K ) ? pc->jt : pc->jf */
1019 condt = MIPS_COND_EQ | MIPS_COND_K;
1020 goto jmp_cmp;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001021 case BPF_JMP | BPF_JEQ | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001022 ctx->flags |= SEEN_X;
1023 /* pc += ( A == X ) ? pc->jt : pc->jf */
1024 condt = MIPS_COND_EQ | MIPS_COND_X;
1025 goto jmp_cmp;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001026 case BPF_JMP | BPF_JGE | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001027 /* pc += ( A >= K ) ? pc->jt : pc->jf */
1028 condt = MIPS_COND_GE | MIPS_COND_K;
1029 goto jmp_cmp;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001030 case BPF_JMP | BPF_JGE | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001031 ctx->flags |= SEEN_X;
1032 /* pc += ( A >= X ) ? pc->jt : pc->jf */
1033 condt = MIPS_COND_GE | MIPS_COND_X;
1034 goto jmp_cmp;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001035 case BPF_JMP | BPF_JGT | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001036 /* pc += ( A > K ) ? pc->jt : pc->jf */
1037 condt = MIPS_COND_GT | MIPS_COND_K;
1038 goto jmp_cmp;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001039 case BPF_JMP | BPF_JGT | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001040 ctx->flags |= SEEN_X;
1041 /* pc += ( A > X ) ? pc->jt : pc->jf */
1042 condt = MIPS_COND_GT | MIPS_COND_X;
1043jmp_cmp:
1044 /* Greater or Equal */
1045 if ((condt & MIPS_COND_GE) ||
1046 (condt & MIPS_COND_GT)) {
1047 if (condt & MIPS_COND_K) { /* K */
Markos Chandrasad152bd2015-06-04 11:56:11 +01001048 ctx->flags |= SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001049 emit_sltiu(r_s0, r_A, k, ctx);
1050 } else { /* X */
Markos Chandrasad152bd2015-06-04 11:56:11 +01001051 ctx->flags |= SEEN_A |
Markos Chandrasc6610de2014-04-08 12:47:14 +01001052 SEEN_X;
1053 emit_sltu(r_s0, r_A, r_X, ctx);
1054 }
1055 /* A < (K|X) ? r_scrach = 1 */
1056 b_off = b_imm(i + inst->jf + 1, ctx);
Markos Chandras1ab24a42014-06-23 10:38:51 +01001057 emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off,
Markos Chandrasc6610de2014-04-08 12:47:14 +01001058 ctx);
1059 emit_nop(ctx);
1060 /* A > (K|X) ? scratch = 0 */
1061 if (condt & MIPS_COND_GT) {
1062 /* Checking for equality */
Markos Chandrasad152bd2015-06-04 11:56:11 +01001063 ctx->flags |= SEEN_A | SEEN_X;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001064 if (condt & MIPS_COND_K)
1065 emit_load_imm(r_s0, k, ctx);
1066 else
1067 emit_jit_reg_move(r_s0, r_X,
1068 ctx);
1069 b_off = b_imm(i + inst->jf + 1, ctx);
1070 emit_bcond(MIPS_COND_EQ, r_A, r_s0,
1071 b_off, ctx);
1072 emit_nop(ctx);
1073 /* Finally, A > K|X */
1074 b_off = b_imm(i + inst->jt + 1, ctx);
1075 emit_b(b_off, ctx);
1076 emit_nop(ctx);
1077 } else {
1078 /* A >= (K|X) so jump */
1079 b_off = b_imm(i + inst->jt + 1, ctx);
1080 emit_b(b_off, ctx);
1081 emit_nop(ctx);
1082 }
1083 } else {
1084 /* A == K|X */
1085 if (condt & MIPS_COND_K) { /* K */
Markos Chandrasad152bd2015-06-04 11:56:11 +01001086 ctx->flags |= SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001087 emit_load_imm(r_s0, k, ctx);
1088 /* jump true */
1089 b_off = b_imm(i + inst->jt + 1, ctx);
1090 emit_bcond(MIPS_COND_EQ, r_A, r_s0,
1091 b_off, ctx);
1092 emit_nop(ctx);
1093 /* jump false */
1094 b_off = b_imm(i + inst->jf + 1,
1095 ctx);
1096 emit_bcond(MIPS_COND_NE, r_A, r_s0,
1097 b_off, ctx);
1098 emit_nop(ctx);
1099 } else { /* X */
1100 /* jump true */
1101 ctx->flags |= SEEN_A | SEEN_X;
1102 b_off = b_imm(i + inst->jt + 1,
1103 ctx);
1104 emit_bcond(MIPS_COND_EQ, r_A, r_X,
1105 b_off, ctx);
1106 emit_nop(ctx);
1107 /* jump false */
1108 b_off = b_imm(i + inst->jf + 1, ctx);
1109 emit_bcond(MIPS_COND_NE, r_A, r_X,
1110 b_off, ctx);
1111 emit_nop(ctx);
1112 }
1113 }
1114 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001115 case BPF_JMP | BPF_JSET | BPF_K:
Markos Chandrasad152bd2015-06-04 11:56:11 +01001116 ctx->flags |= SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001117 /* pc += (A & K) ? pc -> jt : pc -> jf */
1118 emit_load_imm(r_s1, k, ctx);
1119 emit_and(r_s0, r_A, r_s1, ctx);
1120 /* jump true */
1121 b_off = b_imm(i + inst->jt + 1, ctx);
1122 emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
1123 emit_nop(ctx);
1124 /* jump false */
1125 b_off = b_imm(i + inst->jf + 1, ctx);
1126 emit_b(b_off, ctx);
1127 emit_nop(ctx);
1128 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001129 case BPF_JMP | BPF_JSET | BPF_X:
Markos Chandrasad152bd2015-06-04 11:56:11 +01001130 ctx->flags |= SEEN_X | SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001131 /* pc += (A & X) ? pc -> jt : pc -> jf */
1132 emit_and(r_s0, r_A, r_X, ctx);
1133 /* jump true */
1134 b_off = b_imm(i + inst->jt + 1, ctx);
1135 emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
1136 emit_nop(ctx);
1137 /* jump false */
1138 b_off = b_imm(i + inst->jf + 1, ctx);
1139 emit_b(b_off, ctx);
1140 emit_nop(ctx);
1141 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001142 case BPF_RET | BPF_A:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001143 ctx->flags |= SEEN_A;
1144 if (i != prog->len - 1)
1145 /*
1146 * If this is not the last instruction
1147 * then jump to the epilogue
1148 */
1149 emit_b(b_imm(prog->len, ctx), ctx);
1150 emit_reg_move(r_ret, r_A, ctx); /* delay slot */
1151 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001152 case BPF_RET | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001153 /*
1154 * It can emit two instructions so it does not fit on
1155 * the delay slot.
1156 */
1157 emit_load_imm(r_ret, k, ctx);
1158 if (i != prog->len - 1) {
1159 /*
1160 * If this is not the last instruction
1161 * then jump to the epilogue
1162 */
1163 emit_b(b_imm(prog->len, ctx), ctx);
1164 emit_nop(ctx);
1165 }
1166 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001167 case BPF_MISC | BPF_TAX:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001168 /* X = A */
1169 ctx->flags |= SEEN_X | SEEN_A;
1170 emit_jit_reg_move(r_X, r_A, ctx);
1171 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001172 case BPF_MISC | BPF_TXA:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001173 /* A = X */
1174 ctx->flags |= SEEN_A | SEEN_X;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001175 emit_jit_reg_move(r_A, r_X, ctx);
1176 break;
1177 /* AUX */
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001178 case BPF_ANC | SKF_AD_PROTOCOL:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001179 /* A = ntohs(skb->protocol */
1180 ctx->flags |= SEEN_SKB | SEEN_OFF | SEEN_A;
1181 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1182 protocol) != 2);
1183 off = offsetof(struct sk_buff, protocol);
1184 emit_half_load(r_A, r_skb, off, ctx);
1185#ifdef CONFIG_CPU_LITTLE_ENDIAN
1186 /* This needs little endian fixup */
Chen Jie3c09bae2014-08-15 16:56:58 +08001187 if (cpu_has_wsbh) {
Ralf Baechleb4f16c92014-06-03 13:05:55 +02001188 /* R2 and later have the wsbh instruction */
1189 emit_wsbh(r_A, r_A, ctx);
1190 } else {
Markos Chandrasc6610de2014-04-08 12:47:14 +01001191 /* Get first byte */
1192 emit_andi(r_tmp_imm, r_A, 0xff, ctx);
1193 /* Shift it */
1194 emit_sll(r_tmp, r_tmp_imm, 8, ctx);
1195 /* Get second byte */
1196 emit_srl(r_tmp_imm, r_A, 8, ctx);
1197 emit_andi(r_tmp_imm, r_tmp_imm, 0xff, ctx);
1198 /* Put everyting together in r_A */
1199 emit_or(r_A, r_tmp, r_tmp_imm, ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +01001200 }
1201#endif
1202 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001203 case BPF_ANC | SKF_AD_CPU:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001204 ctx->flags |= SEEN_A | SEEN_OFF;
1205 /* A = current_thread_info()->cpu */
1206 BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info,
1207 cpu) != 4);
1208 off = offsetof(struct thread_info, cpu);
1209 /* $28/gp points to the thread_info struct */
1210 emit_load(r_A, 28, off, ctx);
1211 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001212 case BPF_ANC | SKF_AD_IFINDEX:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001213 /* A = skb->dev->ifindex */
Markos Chandrasad152bd2015-06-04 11:56:11 +01001214 ctx->flags |= SEEN_SKB | SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001215 off = offsetof(struct sk_buff, dev);
Markos Chandrasb6a14a92014-06-25 09:39:38 +01001216 /* Load *dev pointer */
1217 emit_load_ptr(r_s0, r_skb, off, ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +01001218 /* error (0) in the delay slot */
1219 emit_bcond(MIPS_COND_EQ, r_s0, r_zero,
1220 b_imm(prog->len, ctx), ctx);
1221 emit_reg_move(r_ret, r_zero, ctx);
1222 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device,
1223 ifindex) != 4);
1224 off = offsetof(struct net_device, ifindex);
1225 emit_load(r_A, r_s0, off, ctx);
1226 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001227 case BPF_ANC | SKF_AD_MARK:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001228 ctx->flags |= SEEN_SKB | SEEN_A;
1229 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
1230 off = offsetof(struct sk_buff, mark);
1231 emit_load(r_A, r_skb, off, ctx);
1232 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001233 case BPF_ANC | SKF_AD_RXHASH:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001234 ctx->flags |= SEEN_SKB | SEEN_A;
1235 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
1236 off = offsetof(struct sk_buff, hash);
1237 emit_load(r_A, r_skb, off, ctx);
1238 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001239 case BPF_ANC | SKF_AD_VLAN_TAG:
1240 case BPF_ANC | SKF_AD_VLAN_TAG_PRESENT:
Markos Chandrasad152bd2015-06-04 11:56:11 +01001241 ctx->flags |= SEEN_SKB | SEEN_A;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001242 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1243 vlan_tci) != 2);
1244 off = offsetof(struct sk_buff, vlan_tci);
1245 emit_half_load(r_s0, r_skb, off, ctx);
Markos Chandras91a41d72014-06-23 10:38:53 +01001246 if (code == (BPF_ANC | SKF_AD_VLAN_TAG)) {
Markos Chandras6e86c592014-06-23 10:38:52 +01001247 emit_andi(r_A, r_s0, (u16)~VLAN_TAG_PRESENT, ctx);
Markos Chandras91a41d72014-06-23 10:38:53 +01001248 } else {
Markos Chandras9ee16062014-06-23 10:38:49 +01001249 emit_andi(r_A, r_s0, VLAN_TAG_PRESENT, ctx);
Markos Chandras91a41d72014-06-23 10:38:53 +01001250 /* return 1 if present */
1251 emit_sltu(r_A, r_zero, r_A, ctx);
1252 }
Markos Chandrasc6610de2014-04-08 12:47:14 +01001253 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001254 case BPF_ANC | SKF_AD_PKTTYPE:
Markos Chandras9eebfe42014-06-23 10:38:50 +01001255 ctx->flags |= SEEN_SKB;
1256
Hannes Frederic Sowa233577a2014-09-12 14:04:43 +02001257 emit_load_byte(r_tmp, r_skb, PKT_TYPE_OFFSET(), ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +01001258 /* Keep only the last 3 bits */
1259 emit_andi(r_A, r_tmp, PKT_TYPE_MAX, ctx);
Markos Chandrasb4fe0ec2014-06-23 10:38:58 +01001260#ifdef __BIG_ENDIAN_BITFIELD
1261 /* Get the actual packet type to the lower 3 bits */
1262 emit_srl(r_A, r_A, 5, ctx);
1263#endif
Markos Chandrasc6610de2014-04-08 12:47:14 +01001264 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001265 case BPF_ANC | SKF_AD_QUEUE:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001266 ctx->flags |= SEEN_SKB | SEEN_A;
1267 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1268 queue_mapping) != 2);
1269 BUILD_BUG_ON(offsetof(struct sk_buff,
1270 queue_mapping) > 0xff);
1271 off = offsetof(struct sk_buff, queue_mapping);
1272 emit_half_load(r_A, r_skb, off, ctx);
1273 break;
1274 default:
Markos Chandras78b95b62014-06-23 10:38:54 +01001275 pr_debug("%s: Unhandled opcode: 0x%02x\n", __FILE__,
1276 inst->code);
Markos Chandrasc6610de2014-04-08 12:47:14 +01001277 return -1;
1278 }
1279 }
1280
1281 /* compute offsets only during the first pass */
1282 if (ctx->target == NULL)
1283 ctx->offsets[i] = ctx->idx * 4;
1284
1285 return 0;
1286}
1287
1288int bpf_jit_enable __read_mostly;
1289
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001290void bpf_jit_compile(struct bpf_prog *fp)
Markos Chandrasc6610de2014-04-08 12:47:14 +01001291{
1292 struct jit_ctx ctx;
1293 unsigned int alloc_size, tmp_idx;
1294
1295 if (!bpf_jit_enable)
1296 return;
1297
1298 memset(&ctx, 0, sizeof(ctx));
1299
1300 ctx.offsets = kcalloc(fp->len, sizeof(*ctx.offsets), GFP_KERNEL);
1301 if (ctx.offsets == NULL)
1302 return;
1303
1304 ctx.skf = fp;
1305
1306 if (build_body(&ctx))
1307 goto out;
1308
1309 tmp_idx = ctx.idx;
1310 build_prologue(&ctx);
1311 ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
1312 /* just to complete the ctx.idx count */
1313 build_epilogue(&ctx);
1314
1315 alloc_size = 4 * ctx.idx;
1316 ctx.target = module_alloc(alloc_size);
1317 if (ctx.target == NULL)
1318 goto out;
1319
1320 /* Clean it */
1321 memset(ctx.target, 0, alloc_size);
1322
1323 ctx.idx = 0;
1324
1325 /* Generate the actual JIT code */
1326 build_prologue(&ctx);
1327 build_body(&ctx);
1328 build_epilogue(&ctx);
1329
1330 /* Update the icache */
1331 flush_icache_range((ptr)ctx.target, (ptr)(ctx.target + ctx.idx));
1332
1333 if (bpf_jit_enable > 1)
1334 /* Dump JIT code */
1335 bpf_jit_dump(fp->len, alloc_size, 2, ctx.target);
1336
1337 fp->bpf_func = (void *)ctx.target;
Daniel Borkmann286aad32014-09-08 08:04:49 +02001338 fp->jited = true;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001339
1340out:
1341 kfree(ctx.offsets);
1342}
1343
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001344void bpf_jit_free(struct bpf_prog *fp)
Markos Chandrasc6610de2014-04-08 12:47:14 +01001345{
1346 if (fp->jited)
Rusty Russellbe1f2212015-01-20 09:07:05 +10301347 module_memfree(fp->bpf_func);
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001348
1349 bpf_prog_unlock_free(fp);
Markos Chandrasc6610de2014-04-08 12:47:14 +01001350}