2 * BPF Jit compiler for s390.
4 * Minimum build requirements:
6 * - HAVE_MARCH_Z196_FEATURES: laal, laalg
7 * - HAVE_MARCH_Z10_FEATURES: msfi, cgrj, clgrj
8 * - HAVE_MARCH_Z9_109_FEATURES: alfi, llilf, clfi, oilf, nilf
12 * Copyright IBM Corp. 2012,2015
14 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
15 * Michael Holzheu <holzheu@linux.vnet.ibm.com>
18 #define KMSG_COMPONENT "bpf_jit"
19 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
21 #include <linux/netdevice.h>
22 #include <linux/filter.h>
23 #include <linux/init.h>
24 #include <linux/bpf.h>
25 #include <asm/cacheflush.h>
29 int bpf_jit_enable __read_mostly
;
32 u32 seen
; /* Flags to remember seen eBPF instructions */
33 u32 seen_reg
[16]; /* Array to remember which registers are used */
34 u32
*addrs
; /* Array with relative instruction addresses */
35 u8
*prg_buf
; /* Start of program */
36 int size
; /* Size of program and literal pool */
37 int size_prg
; /* Size of program */
38 int prg
; /* Current position in program */
39 int lit_start
; /* Start of literal pool */
40 int lit
; /* Current position in literal pool */
41 int base_ip
; /* Base address for literal pool */
42 int ret0_ip
; /* Address of return 0 */
43 int exit_ip
; /* Address of exit */
44 int tail_call_start
; /* Tail call start offset */
45 int labels
[1]; /* Labels for local jumps */
48 #define BPF_SIZE_MAX 0x7ffff /* Max size for program (20 bit signed displ) */
50 #define SEEN_SKB 1 /* skb access */
51 #define SEEN_MEM 2 /* use mem[] for temporary storage */
52 #define SEEN_RET0 4 /* ret0_ip points to a valid return 0 */
53 #define SEEN_LITERAL 8 /* code uses literals */
54 #define SEEN_FUNC 16 /* calls C functions */
55 #define SEEN_TAIL_CALL 32 /* code uses tail calls */
56 #define SEEN_SKB_CHANGE 64 /* code changes skb data */
57 #define SEEN_STACK (SEEN_FUNC | SEEN_MEM | SEEN_SKB)
62 #define REG_W0 (__MAX_BPF_REG+0) /* Work register 1 (even) */
63 #define REG_W1 (__MAX_BPF_REG+1) /* Work register 2 (odd) */
64 #define REG_SKB_DATA (__MAX_BPF_REG+2) /* SKB data register */
65 #define REG_L (__MAX_BPF_REG+3) /* Literal pool register */
66 #define REG_15 (__MAX_BPF_REG+4) /* Register 15 */
67 #define REG_0 REG_W0 /* Register 0 */
68 #define REG_1 REG_W1 /* Register 1 */
69 #define REG_2 BPF_REG_1 /* Register 2 */
70 #define REG_14 BPF_REG_0 /* Register 14 */
73 * Mapping of BPF registers to s390 registers
75 static const int reg2hex
[] = {
78 /* Function parameters */
84 /* Call saved registers */
89 /* BPF stack pointer */
91 /* SKB data pointer */
93 /* Work registers for s390x backend */
100 static inline u32
reg(u32 dst_reg
, u32 src_reg
)
102 return reg2hex
[dst_reg
] << 4 | reg2hex
[src_reg
];
105 static inline u32
reg_high(u32 reg
)
107 return reg2hex
[reg
] << 4;
110 static inline void reg_set_seen(struct bpf_jit
*jit
, u32 b1
)
112 u32 r1
= reg2hex
[b1
];
114 if (!jit
->seen_reg
[r1
] && r1
>= 6 && r1
<= 15)
115 jit
->seen_reg
[r1
] = 1;
118 #define REG_SET_SEEN(b1) \
120 reg_set_seen(jit, b1); \
123 #define REG_SEEN(b1) jit->seen_reg[reg2hex[(b1)]]
126 * EMIT macros for code generation
132 *(u16 *) (jit->prg_buf + jit->prg) = op; \
136 #define EMIT2(op, b1, b2) \
138 _EMIT2(op | reg(b1, b2)); \
146 *(u32 *) (jit->prg_buf + jit->prg) = op; \
150 #define EMIT4(op, b1, b2) \
152 _EMIT4(op | reg(b1, b2)); \
157 #define EMIT4_RRF(op, b1, b2, b3) \
159 _EMIT4(op | reg_high(b3) << 8 | reg(b1, b2)); \
165 #define _EMIT4_DISP(op, disp) \
167 unsigned int __disp = (disp) & 0xfff; \
168 _EMIT4(op | __disp); \
171 #define EMIT4_DISP(op, b1, b2, disp) \
173 _EMIT4_DISP(op | reg_high(b1) << 16 | \
174 reg_high(b2) << 8, disp); \
179 #define EMIT4_IMM(op, b1, imm) \
181 unsigned int __imm = (imm) & 0xffff; \
182 _EMIT4(op | reg_high(b1) << 16 | __imm); \
186 #define EMIT4_PCREL(op, pcrel) \
188 long __pcrel = ((pcrel) >> 1) & 0xffff; \
189 _EMIT4(op | __pcrel); \
192 #define _EMIT6(op1, op2) \
194 if (jit->prg_buf) { \
195 *(u32 *) (jit->prg_buf + jit->prg) = op1; \
196 *(u16 *) (jit->prg_buf + jit->prg + 4) = op2; \
201 #define _EMIT6_DISP(op1, op2, disp) \
203 unsigned int __disp = (disp) & 0xfff; \
204 _EMIT6(op1 | __disp, op2); \
207 #define _EMIT6_DISP_LH(op1, op2, disp) \
209 u32 _disp = (u32) disp; \
210 unsigned int __disp_h = _disp & 0xff000; \
211 unsigned int __disp_l = _disp & 0x00fff; \
212 _EMIT6(op1 | __disp_l, op2 | __disp_h >> 4); \
215 #define EMIT6_DISP_LH(op1, op2, b1, b2, b3, disp) \
217 _EMIT6_DISP_LH(op1 | reg(b1, b2) << 16 | \
218 reg_high(b3) << 8, op2, disp); \
224 #define EMIT6_PCREL_LABEL(op1, op2, b1, b2, label, mask) \
226 int rel = (jit->labels[label] - jit->prg) >> 1; \
227 _EMIT6(op1 | reg(b1, b2) << 16 | (rel & 0xffff), \
233 #define EMIT6_PCREL_IMM_LABEL(op1, op2, b1, imm, label, mask) \
235 int rel = (jit->labels[label] - jit->prg) >> 1; \
236 _EMIT6(op1 | (reg_high(b1) | mask) << 16 | \
237 (rel & 0xffff), op2 | (imm & 0xff) << 8); \
239 BUILD_BUG_ON(((unsigned long) imm) > 0xff); \
242 #define EMIT6_PCREL(op1, op2, b1, b2, i, off, mask) \
244 /* Branch instruction needs 6 bytes */ \
245 int rel = (addrs[i + off + 1] - (addrs[i + 1] - 6)) / 2;\
246 _EMIT6(op1 | reg(b1, b2) << 16 | (rel & 0xffff), op2 | mask); \
251 #define _EMIT6_IMM(op, imm) \
253 unsigned int __imm = (imm); \
254 _EMIT6(op | (__imm >> 16), __imm & 0xffff); \
257 #define EMIT6_IMM(op, b1, imm) \
259 _EMIT6_IMM(op | reg_high(b1) << 16, imm); \
263 #define EMIT_CONST_U32(val) \
266 ret = jit->lit - jit->base_ip; \
267 jit->seen |= SEEN_LITERAL; \
269 *(u32 *) (jit->prg_buf + jit->lit) = (u32) val; \
274 #define EMIT_CONST_U64(val) \
277 ret = jit->lit - jit->base_ip; \
278 jit->seen |= SEEN_LITERAL; \
280 *(u64 *) (jit->prg_buf + jit->lit) = (u64) val; \
285 #define EMIT_ZERO(b1) \
287 /* llgfr %dst,%dst (zero extend to 64 bit) */ \
288 EMIT4(0xb9160000, b1, b1); \
293 * Fill whole space with illegal instructions
295 static void jit_fill_hole(void *area
, unsigned int size
)
297 memset(area
, 0, size
);
301 * Save registers from "rs" (register start) to "re" (register end) on stack
303 static void save_regs(struct bpf_jit
*jit
, u32 rs
, u32 re
)
305 u32 off
= STK_OFF_R6
+ (rs
- 6) * 8;
308 /* stg %rs,off(%r15) */
309 _EMIT6(0xe300f000 | rs
<< 20 | off
, 0x0024);
311 /* stmg %rs,%re,off(%r15) */
312 _EMIT6_DISP(0xeb00f000 | rs
<< 20 | re
<< 16, 0x0024, off
);
316 * Restore registers from "rs" (register start) to "re" (register end) on stack
318 static void restore_regs(struct bpf_jit
*jit
, u32 rs
, u32 re
)
320 u32 off
= STK_OFF_R6
+ (rs
- 6) * 8;
322 if (jit
->seen
& SEEN_STACK
)
326 /* lg %rs,off(%r15) */
327 _EMIT6(0xe300f000 | rs
<< 20 | off
, 0x0004);
329 /* lmg %rs,%re,off(%r15) */
330 _EMIT6_DISP(0xeb00f000 | rs
<< 20 | re
<< 16, 0x0004, off
);
334 * Return first seen register (from start)
336 static int get_start(struct bpf_jit
*jit
, int start
)
340 for (i
= start
; i
<= 15; i
++) {
341 if (jit
->seen_reg
[i
])
348 * Return last seen register (from start) (gap >= 2)
350 static int get_end(struct bpf_jit
*jit
, int start
)
354 for (i
= start
; i
< 15; i
++) {
355 if (!jit
->seen_reg
[i
] && !jit
->seen_reg
[i
+ 1])
358 return jit
->seen_reg
[15] ? 15 : 14;
362 #define REGS_RESTORE 0
364 * Save and restore clobbered registers (6-15) on stack.
365 * We save/restore registers in chunks with gap >= 2 registers.
367 static void save_restore_regs(struct bpf_jit
*jit
, int op
)
373 rs
= get_start(jit
, re
);
376 re
= get_end(jit
, rs
+ 1);
378 save_regs(jit
, rs
, re
);
380 restore_regs(jit
, rs
, re
);
386 * For SKB access %b1 contains the SKB pointer. For "bpf_jit.S"
387 * we store the SKB header length on the stack and the SKB data
388 * pointer in REG_SKB_DATA.
390 static void emit_load_skb_data_hlen(struct bpf_jit
*jit
)
392 /* Header length: llgf %w1,<len>(%b1) */
393 EMIT6_DISP_LH(0xe3000000, 0x0016, REG_W1
, REG_0
, BPF_REG_1
,
394 offsetof(struct sk_buff
, len
));
395 /* s %w1,<data_len>(%b1) */
396 EMIT4_DISP(0x5b000000, REG_W1
, BPF_REG_1
,
397 offsetof(struct sk_buff
, data_len
));
398 /* stg %w1,ST_OFF_HLEN(%r0,%r15) */
399 EMIT6_DISP_LH(0xe3000000, 0x0024, REG_W1
, REG_0
, REG_15
, STK_OFF_HLEN
);
400 /* lg %skb_data,data_off(%b1) */
401 EMIT6_DISP_LH(0xe3000000, 0x0004, REG_SKB_DATA
, REG_0
,
402 BPF_REG_1
, offsetof(struct sk_buff
, data
));
406 * Emit function prologue
408 * Save registers and create stack frame if necessary.
409 * See stack frame layout desription in "bpf_jit.h"!
411 static void bpf_jit_prologue(struct bpf_jit
*jit
, bool is_classic
)
413 if (jit
->seen
& SEEN_TAIL_CALL
) {
414 /* xc STK_OFF_TCCNT(4,%r15),STK_OFF_TCCNT(%r15) */
415 _EMIT6(0xd703f000 | STK_OFF_TCCNT
, 0xf000 | STK_OFF_TCCNT
);
417 /* j tail_call_start: NOP if no tail calls are used */
418 EMIT4_PCREL(0xa7f40000, 6);
421 /* Tail calls have to skip above initialization */
422 jit
->tail_call_start
= jit
->prg
;
424 save_restore_regs(jit
, REGS_SAVE
);
425 /* Setup literal pool */
426 if (jit
->seen
& SEEN_LITERAL
) {
428 EMIT2(0x0d00, REG_L
, REG_0
);
429 jit
->base_ip
= jit
->prg
;
431 /* Setup stack and backchain */
432 if (jit
->seen
& SEEN_STACK
) {
433 if (jit
->seen
& SEEN_FUNC
)
434 /* lgr %w1,%r15 (backchain) */
435 EMIT4(0xb9040000, REG_W1
, REG_15
);
436 /* la %bfp,STK_160_UNUSED(%r15) (BPF frame pointer) */
437 EMIT4_DISP(0x41000000, BPF_REG_FP
, REG_15
, STK_160_UNUSED
);
438 /* aghi %r15,-STK_OFF */
439 EMIT4_IMM(0xa70b0000, REG_15
, -STK_OFF
);
440 if (jit
->seen
& SEEN_FUNC
)
441 /* stg %w1,152(%r15) (backchain) */
442 EMIT6_DISP_LH(0xe3000000, 0x0024, REG_W1
, REG_0
,
445 if (jit
->seen
& SEEN_SKB
)
446 emit_load_skb_data_hlen(jit
);
447 if (jit
->seen
& SEEN_SKB_CHANGE
)
448 /* stg %b1,ST_OFF_SKBP(%r0,%r15) */
449 EMIT6_DISP_LH(0xe3000000, 0x0024, REG_W1
, REG_0
, REG_15
,
451 /* Clear A (%b0) and X (%b7) registers for converted BPF programs */
453 if (REG_SEEN(BPF_REG_A
))
455 EMIT4_IMM(0xa7090000, BPF_REG_A
, 0);
456 if (REG_SEEN(BPF_REG_X
))
458 EMIT4_IMM(0xa7090000, BPF_REG_X
, 0);
465 static void bpf_jit_epilogue(struct bpf_jit
*jit
)
468 if (jit
->seen
& SEEN_RET0
) {
469 jit
->ret0_ip
= jit
->prg
;
471 EMIT4_IMM(0xa7090000, BPF_REG_0
, 0);
473 jit
->exit_ip
= jit
->prg
;
474 /* Load exit code: lgr %r2,%b0 */
475 EMIT4(0xb9040000, REG_2
, BPF_REG_0
);
476 /* Restore registers */
477 save_restore_regs(jit
, REGS_RESTORE
);
483 * Compile one eBPF instruction into s390x code
485 * NOTE: Use noinline because for gcov (-fprofile-arcs) gcc allocates a lot of
486 * stack space for the large switch statement.
488 static noinline
int bpf_jit_insn(struct bpf_jit
*jit
, struct bpf_prog
*fp
, int i
)
490 struct bpf_insn
*insn
= &fp
->insnsi
[i
];
491 int jmp_off
, last
, insn_count
= 1;
492 unsigned int func_addr
, mask
;
493 u32 dst_reg
= insn
->dst_reg
;
494 u32 src_reg
= insn
->src_reg
;
495 u32
*addrs
= jit
->addrs
;
499 switch (insn
->code
) {
503 case BPF_ALU
| BPF_MOV
| BPF_X
: /* dst = (u32) src */
504 /* llgfr %dst,%src */
505 EMIT4(0xb9160000, dst_reg
, src_reg
);
507 case BPF_ALU64
| BPF_MOV
| BPF_X
: /* dst = src */
509 EMIT4(0xb9040000, dst_reg
, src_reg
);
511 case BPF_ALU
| BPF_MOV
| BPF_K
: /* dst = (u32) imm */
513 EMIT6_IMM(0xc00f0000, dst_reg
, imm
);
515 case BPF_ALU64
| BPF_MOV
| BPF_K
: /* dst = imm */
517 EMIT6_IMM(0xc0010000, dst_reg
, imm
);
522 case BPF_LD
| BPF_IMM
| BPF_DW
: /* dst = (u64) imm */
524 /* 16 byte instruction that uses two 'struct bpf_insn' */
527 imm64
= (u64
)(u32
) insn
[0].imm
| ((u64
)(u32
) insn
[1].imm
) << 32;
528 /* lg %dst,<d(imm)>(%l) */
529 EMIT6_DISP_LH(0xe3000000, 0x0004, dst_reg
, REG_0
, REG_L
,
530 EMIT_CONST_U64(imm64
));
537 case BPF_ALU
| BPF_ADD
| BPF_X
: /* dst = (u32) dst + (u32) src */
539 EMIT2(0x1a00, dst_reg
, src_reg
);
542 case BPF_ALU64
| BPF_ADD
| BPF_X
: /* dst = dst + src */
544 EMIT4(0xb9080000, dst_reg
, src_reg
);
546 case BPF_ALU
| BPF_ADD
| BPF_K
: /* dst = (u32) dst + (u32) imm */
550 EMIT6_IMM(0xc20b0000, dst_reg
, imm
);
553 case BPF_ALU64
| BPF_ADD
| BPF_K
: /* dst = dst + imm */
557 EMIT6_IMM(0xc2080000, dst_reg
, imm
);
562 case BPF_ALU
| BPF_SUB
| BPF_X
: /* dst = (u32) dst - (u32) src */
564 EMIT2(0x1b00, dst_reg
, src_reg
);
567 case BPF_ALU64
| BPF_SUB
| BPF_X
: /* dst = dst - src */
569 EMIT4(0xb9090000, dst_reg
, src_reg
);
571 case BPF_ALU
| BPF_SUB
| BPF_K
: /* dst = (u32) dst - (u32) imm */
575 EMIT6_IMM(0xc20b0000, dst_reg
, -imm
);
578 case BPF_ALU64
| BPF_SUB
| BPF_K
: /* dst = dst - imm */
582 EMIT6_IMM(0xc2080000, dst_reg
, -imm
);
587 case BPF_ALU
| BPF_MUL
| BPF_X
: /* dst = (u32) dst * (u32) src */
589 EMIT4(0xb2520000, dst_reg
, src_reg
);
592 case BPF_ALU64
| BPF_MUL
| BPF_X
: /* dst = dst * src */
594 EMIT4(0xb90c0000, dst_reg
, src_reg
);
596 case BPF_ALU
| BPF_MUL
| BPF_K
: /* dst = (u32) dst * (u32) imm */
600 EMIT6_IMM(0xc2010000, dst_reg
, imm
);
603 case BPF_ALU64
| BPF_MUL
| BPF_K
: /* dst = dst * imm */
607 EMIT6_IMM(0xc2000000, dst_reg
, imm
);
612 case BPF_ALU
| BPF_DIV
| BPF_X
: /* dst = (u32) dst / (u32) src */
613 case BPF_ALU
| BPF_MOD
| BPF_X
: /* dst = (u32) dst % (u32) src */
615 int rc_reg
= BPF_OP(insn
->code
) == BPF_DIV
? REG_W1
: REG_W0
;
617 jit
->seen
|= SEEN_RET0
;
618 /* ltr %src,%src (if src == 0 goto fail) */
619 EMIT2(0x1200, src_reg
, src_reg
);
621 EMIT4_PCREL(0xa7840000, jit
->ret0_ip
- jit
->prg
);
623 EMIT4_IMM(0xa7080000, REG_W0
, 0);
625 EMIT2(0x1800, REG_W1
, dst_reg
);
627 EMIT4(0xb9970000, REG_W0
, src_reg
);
629 EMIT4(0xb9160000, dst_reg
, rc_reg
);
632 case BPF_ALU64
| BPF_DIV
| BPF_X
: /* dst = dst / src */
633 case BPF_ALU64
| BPF_MOD
| BPF_X
: /* dst = dst % src */
635 int rc_reg
= BPF_OP(insn
->code
) == BPF_DIV
? REG_W1
: REG_W0
;
637 jit
->seen
|= SEEN_RET0
;
638 /* ltgr %src,%src (if src == 0 goto fail) */
639 EMIT4(0xb9020000, src_reg
, src_reg
);
641 EMIT4_PCREL(0xa7840000, jit
->ret0_ip
- jit
->prg
);
643 EMIT4_IMM(0xa7090000, REG_W0
, 0);
645 EMIT4(0xb9040000, REG_W1
, dst_reg
);
647 EMIT4(0xb9870000, REG_W0
, src_reg
);
649 EMIT4(0xb9040000, dst_reg
, rc_reg
);
652 case BPF_ALU
| BPF_DIV
| BPF_K
: /* dst = (u32) dst / (u32) imm */
653 case BPF_ALU
| BPF_MOD
| BPF_K
: /* dst = (u32) dst % (u32) imm */
655 int rc_reg
= BPF_OP(insn
->code
) == BPF_DIV
? REG_W1
: REG_W0
;
658 if (BPF_OP(insn
->code
) == BPF_MOD
)
660 EMIT4_IMM(0xa7090000, dst_reg
, 0);
664 EMIT4_IMM(0xa7080000, REG_W0
, 0);
666 EMIT2(0x1800, REG_W1
, dst_reg
);
667 /* dl %w0,<d(imm)>(%l) */
668 EMIT6_DISP_LH(0xe3000000, 0x0097, REG_W0
, REG_0
, REG_L
,
669 EMIT_CONST_U32(imm
));
671 EMIT4(0xb9160000, dst_reg
, rc_reg
);
674 case BPF_ALU64
| BPF_DIV
| BPF_K
: /* dst = dst / imm */
675 case BPF_ALU64
| BPF_MOD
| BPF_K
: /* dst = dst % imm */
677 int rc_reg
= BPF_OP(insn
->code
) == BPF_DIV
? REG_W1
: REG_W0
;
680 if (BPF_OP(insn
->code
) == BPF_MOD
)
682 EMIT4_IMM(0xa7090000, dst_reg
, 0);
686 EMIT4_IMM(0xa7090000, REG_W0
, 0);
688 EMIT4(0xb9040000, REG_W1
, dst_reg
);
689 /* dlg %w0,<d(imm)>(%l) */
690 EMIT6_DISP_LH(0xe3000000, 0x0087, REG_W0
, REG_0
, REG_L
,
691 EMIT_CONST_U64(imm
));
693 EMIT4(0xb9040000, dst_reg
, rc_reg
);
699 case BPF_ALU
| BPF_AND
| BPF_X
: /* dst = (u32) dst & (u32) src */
701 EMIT2(0x1400, dst_reg
, src_reg
);
704 case BPF_ALU64
| BPF_AND
| BPF_X
: /* dst = dst & src */
706 EMIT4(0xb9800000, dst_reg
, src_reg
);
708 case BPF_ALU
| BPF_AND
| BPF_K
: /* dst = (u32) dst & (u32) imm */
710 EMIT6_IMM(0xc00b0000, dst_reg
, imm
);
713 case BPF_ALU64
| BPF_AND
| BPF_K
: /* dst = dst & imm */
714 /* ng %dst,<d(imm)>(%l) */
715 EMIT6_DISP_LH(0xe3000000, 0x0080, dst_reg
, REG_0
, REG_L
,
716 EMIT_CONST_U64(imm
));
721 case BPF_ALU
| BPF_OR
| BPF_X
: /* dst = (u32) dst | (u32) src */
723 EMIT2(0x1600, dst_reg
, src_reg
);
726 case BPF_ALU64
| BPF_OR
| BPF_X
: /* dst = dst | src */
728 EMIT4(0xb9810000, dst_reg
, src_reg
);
730 case BPF_ALU
| BPF_OR
| BPF_K
: /* dst = (u32) dst | (u32) imm */
732 EMIT6_IMM(0xc00d0000, dst_reg
, imm
);
735 case BPF_ALU64
| BPF_OR
| BPF_K
: /* dst = dst | imm */
736 /* og %dst,<d(imm)>(%l) */
737 EMIT6_DISP_LH(0xe3000000, 0x0081, dst_reg
, REG_0
, REG_L
,
738 EMIT_CONST_U64(imm
));
743 case BPF_ALU
| BPF_XOR
| BPF_X
: /* dst = (u32) dst ^ (u32) src */
745 EMIT2(0x1700, dst_reg
, src_reg
);
748 case BPF_ALU64
| BPF_XOR
| BPF_X
: /* dst = dst ^ src */
750 EMIT4(0xb9820000, dst_reg
, src_reg
);
752 case BPF_ALU
| BPF_XOR
| BPF_K
: /* dst = (u32) dst ^ (u32) imm */
756 EMIT6_IMM(0xc0070000, dst_reg
, imm
);
759 case BPF_ALU64
| BPF_XOR
| BPF_K
: /* dst = dst ^ imm */
760 /* xg %dst,<d(imm)>(%l) */
761 EMIT6_DISP_LH(0xe3000000, 0x0082, dst_reg
, REG_0
, REG_L
,
762 EMIT_CONST_U64(imm
));
767 case BPF_ALU
| BPF_LSH
| BPF_X
: /* dst = (u32) dst << (u32) src */
768 /* sll %dst,0(%src) */
769 EMIT4_DISP(0x89000000, dst_reg
, src_reg
, 0);
772 case BPF_ALU64
| BPF_LSH
| BPF_X
: /* dst = dst << src */
773 /* sllg %dst,%dst,0(%src) */
774 EMIT6_DISP_LH(0xeb000000, 0x000d, dst_reg
, dst_reg
, src_reg
, 0);
776 case BPF_ALU
| BPF_LSH
| BPF_K
: /* dst = (u32) dst << (u32) imm */
779 /* sll %dst,imm(%r0) */
780 EMIT4_DISP(0x89000000, dst_reg
, REG_0
, imm
);
783 case BPF_ALU64
| BPF_LSH
| BPF_K
: /* dst = dst << imm */
786 /* sllg %dst,%dst,imm(%r0) */
787 EMIT6_DISP_LH(0xeb000000, 0x000d, dst_reg
, dst_reg
, REG_0
, imm
);
792 case BPF_ALU
| BPF_RSH
| BPF_X
: /* dst = (u32) dst >> (u32) src */
793 /* srl %dst,0(%src) */
794 EMIT4_DISP(0x88000000, dst_reg
, src_reg
, 0);
797 case BPF_ALU64
| BPF_RSH
| BPF_X
: /* dst = dst >> src */
798 /* srlg %dst,%dst,0(%src) */
799 EMIT6_DISP_LH(0xeb000000, 0x000c, dst_reg
, dst_reg
, src_reg
, 0);
801 case BPF_ALU
| BPF_RSH
| BPF_K
: /* dst = (u32) dst >> (u32) imm */
804 /* srl %dst,imm(%r0) */
805 EMIT4_DISP(0x88000000, dst_reg
, REG_0
, imm
);
808 case BPF_ALU64
| BPF_RSH
| BPF_K
: /* dst = dst >> imm */
811 /* srlg %dst,%dst,imm(%r0) */
812 EMIT6_DISP_LH(0xeb000000, 0x000c, dst_reg
, dst_reg
, REG_0
, imm
);
817 case BPF_ALU64
| BPF_ARSH
| BPF_X
: /* ((s64) dst) >>= src */
818 /* srag %dst,%dst,0(%src) */
819 EMIT6_DISP_LH(0xeb000000, 0x000a, dst_reg
, dst_reg
, src_reg
, 0);
821 case BPF_ALU64
| BPF_ARSH
| BPF_K
: /* ((s64) dst) >>= imm */
824 /* srag %dst,%dst,imm(%r0) */
825 EMIT6_DISP_LH(0xeb000000, 0x000a, dst_reg
, dst_reg
, REG_0
, imm
);
830 case BPF_ALU
| BPF_NEG
: /* dst = (u32) -dst */
832 EMIT2(0x1300, dst_reg
, dst_reg
);
835 case BPF_ALU64
| BPF_NEG
: /* dst = -dst */
837 EMIT4(0xb9130000, dst_reg
, dst_reg
);
842 case BPF_ALU
| BPF_END
| BPF_FROM_BE
:
843 /* s390 is big endian, therefore only clear high order bytes */
845 case 16: /* dst = (u16) cpu_to_be16(dst) */
846 /* llghr %dst,%dst */
847 EMIT4(0xb9850000, dst_reg
, dst_reg
);
849 case 32: /* dst = (u32) cpu_to_be32(dst) */
850 /* llgfr %dst,%dst */
851 EMIT4(0xb9160000, dst_reg
, dst_reg
);
853 case 64: /* dst = (u64) cpu_to_be64(dst) */
857 case BPF_ALU
| BPF_END
| BPF_FROM_LE
:
859 case 16: /* dst = (u16) cpu_to_le16(dst) */
861 EMIT4(0xb91f0000, dst_reg
, dst_reg
);
862 /* srl %dst,16(%r0) */
863 EMIT4_DISP(0x88000000, dst_reg
, REG_0
, 16);
864 /* llghr %dst,%dst */
865 EMIT4(0xb9850000, dst_reg
, dst_reg
);
867 case 32: /* dst = (u32) cpu_to_le32(dst) */
869 EMIT4(0xb91f0000, dst_reg
, dst_reg
);
870 /* llgfr %dst,%dst */
871 EMIT4(0xb9160000, dst_reg
, dst_reg
);
873 case 64: /* dst = (u64) cpu_to_le64(dst) */
874 /* lrvgr %dst,%dst */
875 EMIT4(0xb90f0000, dst_reg
, dst_reg
);
882 case BPF_STX
| BPF_MEM
| BPF_B
: /* *(u8 *)(dst + off) = src_reg */
883 /* stcy %src,off(%dst) */
884 EMIT6_DISP_LH(0xe3000000, 0x0072, src_reg
, dst_reg
, REG_0
, off
);
885 jit
->seen
|= SEEN_MEM
;
887 case BPF_STX
| BPF_MEM
| BPF_H
: /* (u16 *)(dst + off) = src */
888 /* sthy %src,off(%dst) */
889 EMIT6_DISP_LH(0xe3000000, 0x0070, src_reg
, dst_reg
, REG_0
, off
);
890 jit
->seen
|= SEEN_MEM
;
892 case BPF_STX
| BPF_MEM
| BPF_W
: /* *(u32 *)(dst + off) = src */
893 /* sty %src,off(%dst) */
894 EMIT6_DISP_LH(0xe3000000, 0x0050, src_reg
, dst_reg
, REG_0
, off
);
895 jit
->seen
|= SEEN_MEM
;
897 case BPF_STX
| BPF_MEM
| BPF_DW
: /* (u64 *)(dst + off) = src */
898 /* stg %src,off(%dst) */
899 EMIT6_DISP_LH(0xe3000000, 0x0024, src_reg
, dst_reg
, REG_0
, off
);
900 jit
->seen
|= SEEN_MEM
;
902 case BPF_ST
| BPF_MEM
| BPF_B
: /* *(u8 *)(dst + off) = imm */
904 EMIT4_IMM(0xa7080000, REG_W0
, (u8
) imm
);
905 /* stcy %w0,off(dst) */
906 EMIT6_DISP_LH(0xe3000000, 0x0072, REG_W0
, dst_reg
, REG_0
, off
);
907 jit
->seen
|= SEEN_MEM
;
909 case BPF_ST
| BPF_MEM
| BPF_H
: /* (u16 *)(dst + off) = imm */
911 EMIT4_IMM(0xa7080000, REG_W0
, (u16
) imm
);
912 /* sthy %w0,off(dst) */
913 EMIT6_DISP_LH(0xe3000000, 0x0070, REG_W0
, dst_reg
, REG_0
, off
);
914 jit
->seen
|= SEEN_MEM
;
916 case BPF_ST
| BPF_MEM
| BPF_W
: /* *(u32 *)(dst + off) = imm */
918 EMIT6_IMM(0xc00f0000, REG_W0
, (u32
) imm
);
919 /* sty %w0,off(%dst) */
920 EMIT6_DISP_LH(0xe3000000, 0x0050, REG_W0
, dst_reg
, REG_0
, off
);
921 jit
->seen
|= SEEN_MEM
;
923 case BPF_ST
| BPF_MEM
| BPF_DW
: /* *(u64 *)(dst + off) = imm */
925 EMIT6_IMM(0xc0010000, REG_W0
, imm
);
926 /* stg %w0,off(%dst) */
927 EMIT6_DISP_LH(0xe3000000, 0x0024, REG_W0
, dst_reg
, REG_0
, off
);
928 jit
->seen
|= SEEN_MEM
;
931 * BPF_STX XADD (atomic_add)
933 case BPF_STX
| BPF_XADD
| BPF_W
: /* *(u32 *)(dst + off) += src */
934 /* laal %w0,%src,off(%dst) */
935 EMIT6_DISP_LH(0xeb000000, 0x00fa, REG_W0
, src_reg
,
937 jit
->seen
|= SEEN_MEM
;
939 case BPF_STX
| BPF_XADD
| BPF_DW
: /* *(u64 *)(dst + off) += src */
940 /* laalg %w0,%src,off(%dst) */
941 EMIT6_DISP_LH(0xeb000000, 0x00ea, REG_W0
, src_reg
,
943 jit
->seen
|= SEEN_MEM
;
948 case BPF_LDX
| BPF_MEM
| BPF_B
: /* dst = *(u8 *)(ul) (src + off) */
949 /* llgc %dst,0(off,%src) */
950 EMIT6_DISP_LH(0xe3000000, 0x0090, dst_reg
, src_reg
, REG_0
, off
);
951 jit
->seen
|= SEEN_MEM
;
953 case BPF_LDX
| BPF_MEM
| BPF_H
: /* dst = *(u16 *)(ul) (src + off) */
954 /* llgh %dst,0(off,%src) */
955 EMIT6_DISP_LH(0xe3000000, 0x0091, dst_reg
, src_reg
, REG_0
, off
);
956 jit
->seen
|= SEEN_MEM
;
958 case BPF_LDX
| BPF_MEM
| BPF_W
: /* dst = *(u32 *)(ul) (src + off) */
959 /* llgf %dst,off(%src) */
960 jit
->seen
|= SEEN_MEM
;
961 EMIT6_DISP_LH(0xe3000000, 0x0016, dst_reg
, src_reg
, REG_0
, off
);
963 case BPF_LDX
| BPF_MEM
| BPF_DW
: /* dst = *(u64 *)(ul) (src + off) */
964 /* lg %dst,0(off,%src) */
965 jit
->seen
|= SEEN_MEM
;
966 EMIT6_DISP_LH(0xe3000000, 0x0004, dst_reg
, src_reg
, REG_0
, off
);
971 case BPF_JMP
| BPF_CALL
:
974 * b0 = (__bpf_call_base + imm)(b1, b2, b3, b4, b5)
976 const u64 func
= (u64
)__bpf_call_base
+ imm
;
978 REG_SET_SEEN(BPF_REG_5
);
979 jit
->seen
|= SEEN_FUNC
;
980 /* lg %w1,<d(imm)>(%l) */
981 EMIT6_DISP_LH(0xe3000000, 0x0004, REG_W1
, REG_0
, REG_L
,
982 EMIT_CONST_U64(func
));
984 EMIT2(0x0d00, REG_14
, REG_W1
);
985 /* lgr %b0,%r2: load return value into %b0 */
986 EMIT4(0xb9040000, BPF_REG_0
, REG_2
);
987 if (bpf_helper_changes_skb_data((void *)func
)) {
988 jit
->seen
|= SEEN_SKB_CHANGE
;
989 /* lg %b1,ST_OFF_SKBP(%r15) */
990 EMIT6_DISP_LH(0xe3000000, 0x0004, BPF_REG_1
, REG_0
,
991 REG_15
, STK_OFF_SKBP
);
992 emit_load_skb_data_hlen(jit
);
996 case BPF_JMP
| BPF_CALL
| BPF_X
:
1000 * B2: pointer to bpf_array
1001 * B3: index in bpf_array
1003 jit
->seen
|= SEEN_TAIL_CALL
;
1006 * if (index >= array->map.max_entries)
1010 /* llgf %w1,map.max_entries(%b2) */
1011 EMIT6_DISP_LH(0xe3000000, 0x0016, REG_W1
, REG_0
, BPF_REG_2
,
1012 offsetof(struct bpf_array
, map
.max_entries
));
1013 /* clgrj %b3,%w1,0xa,label0: if %b3 >= %w1 goto out */
1014 EMIT6_PCREL_LABEL(0xec000000, 0x0065, BPF_REG_3
,
1018 * if (tail_call_cnt++ > MAX_TAIL_CALL_CNT)
1022 if (jit
->seen
& SEEN_STACK
)
1023 off
= STK_OFF_TCCNT
+ STK_OFF
;
1025 off
= STK_OFF_TCCNT
;
1027 EMIT4_IMM(0xa7080000, REG_W0
, 1);
1028 /* laal %w1,%w0,off(%r15) */
1029 EMIT6_DISP_LH(0xeb000000, 0x00fa, REG_W1
, REG_W0
, REG_15
, off
);
1030 /* clij %w1,MAX_TAIL_CALL_CNT,0x2,label0 */
1031 EMIT6_PCREL_IMM_LABEL(0xec000000, 0x007f, REG_W1
,
1032 MAX_TAIL_CALL_CNT
, 0, 0x2);
1035 * prog = array->ptrs[index];
1040 /* sllg %r1,%b3,3: %r1 = index * 8 */
1041 EMIT6_DISP_LH(0xeb000000, 0x000d, REG_1
, BPF_REG_3
, REG_0
, 3);
1042 /* lg %r1,prog(%b2,%r1) */
1043 EMIT6_DISP_LH(0xe3000000, 0x0004, REG_1
, BPF_REG_2
,
1044 REG_1
, offsetof(struct bpf_array
, ptrs
));
1045 /* clgij %r1,0,0x8,label0 */
1046 EMIT6_PCREL_IMM_LABEL(0xec000000, 0x007d, REG_1
, 0, 0, 0x8);
1049 * Restore registers before calling function
1051 save_restore_regs(jit
, REGS_RESTORE
);
1054 * goto *(prog->bpf_func + tail_call_start);
1057 /* lg %r1,bpf_func(%r1) */
1058 EMIT6_DISP_LH(0xe3000000, 0x0004, REG_1
, REG_1
, REG_0
,
1059 offsetof(struct bpf_prog
, bpf_func
));
1060 /* bc 0xf,tail_call_start(%r1) */
1061 _EMIT4(0x47f01000 + jit
->tail_call_start
);
1063 jit
->labels
[0] = jit
->prg
;
1065 case BPF_JMP
| BPF_EXIT
: /* return b0 */
1066 last
= (i
== fp
->len
- 1) ? 1 : 0;
1067 if (last
&& !(jit
->seen
& SEEN_RET0
))
1070 EMIT4_PCREL(0xa7f40000, jit
->exit_ip
- jit
->prg
);
1073 * Branch relative (number of skipped instructions) to offset on
1076 * Condition code to mask mapping:
1078 * CC | Description | Mask
1079 * ------------------------------
1080 * 0 | Operands equal | 8
1081 * 1 | First operand low | 4
1082 * 2 | First operand high | 2
1085 * For s390x relative branches: ip = ip + off_bytes
1086 * For BPF relative branches: insn = insn + off_insns + 1
1088 * For example for s390x with offset 0 we jump to the branch
1089 * instruction itself (loop) and for BPF with offset 0 we
1090 * branch to the instruction behind the branch.
1092 case BPF_JMP
| BPF_JA
: /* if (true) */
1093 mask
= 0xf000; /* j */
1095 case BPF_JMP
| BPF_JSGT
| BPF_K
: /* ((s64) dst > (s64) imm) */
1096 mask
= 0x2000; /* jh */
1098 case BPF_JMP
| BPF_JSGE
| BPF_K
: /* ((s64) dst >= (s64) imm) */
1099 mask
= 0xa000; /* jhe */
1101 case BPF_JMP
| BPF_JGT
| BPF_K
: /* (dst_reg > imm) */
1102 mask
= 0x2000; /* jh */
1104 case BPF_JMP
| BPF_JGE
| BPF_K
: /* (dst_reg >= imm) */
1105 mask
= 0xa000; /* jhe */
1107 case BPF_JMP
| BPF_JNE
| BPF_K
: /* (dst_reg != imm) */
1108 mask
= 0x7000; /* jne */
1110 case BPF_JMP
| BPF_JEQ
| BPF_K
: /* (dst_reg == imm) */
1111 mask
= 0x8000; /* je */
1113 case BPF_JMP
| BPF_JSET
| BPF_K
: /* (dst_reg & imm) */
1114 mask
= 0x7000; /* jnz */
1115 /* lgfi %w1,imm (load sign extend imm) */
1116 EMIT6_IMM(0xc0010000, REG_W1
, imm
);
1118 EMIT4(0xb9800000, REG_W1
, dst_reg
);
1121 case BPF_JMP
| BPF_JSGT
| BPF_X
: /* ((s64) dst > (s64) src) */
1122 mask
= 0x2000; /* jh */
1124 case BPF_JMP
| BPF_JSGE
| BPF_X
: /* ((s64) dst >= (s64) src) */
1125 mask
= 0xa000; /* jhe */
1127 case BPF_JMP
| BPF_JGT
| BPF_X
: /* (dst > src) */
1128 mask
= 0x2000; /* jh */
1130 case BPF_JMP
| BPF_JGE
| BPF_X
: /* (dst >= src) */
1131 mask
= 0xa000; /* jhe */
1133 case BPF_JMP
| BPF_JNE
| BPF_X
: /* (dst != src) */
1134 mask
= 0x7000; /* jne */
1136 case BPF_JMP
| BPF_JEQ
| BPF_X
: /* (dst == src) */
1137 mask
= 0x8000; /* je */
1139 case BPF_JMP
| BPF_JSET
| BPF_X
: /* (dst & src) */
1140 mask
= 0x7000; /* jnz */
1141 /* ngrk %w1,%dst,%src */
1142 EMIT4_RRF(0xb9e40000, REG_W1
, dst_reg
, src_reg
);
1145 /* lgfi %w1,imm (load sign extend imm) */
1146 EMIT6_IMM(0xc0010000, REG_W1
, imm
);
1147 /* cgrj %dst,%w1,mask,off */
1148 EMIT6_PCREL(0xec000000, 0x0064, dst_reg
, REG_W1
, i
, off
, mask
);
1151 /* lgfi %w1,imm (load sign extend imm) */
1152 EMIT6_IMM(0xc0010000, REG_W1
, imm
);
1153 /* clgrj %dst,%w1,mask,off */
1154 EMIT6_PCREL(0xec000000, 0x0065, dst_reg
, REG_W1
, i
, off
, mask
);
1157 /* cgrj %dst,%src,mask,off */
1158 EMIT6_PCREL(0xec000000, 0x0064, dst_reg
, src_reg
, i
, off
, mask
);
1161 /* clgrj %dst,%src,mask,off */
1162 EMIT6_PCREL(0xec000000, 0x0065, dst_reg
, src_reg
, i
, off
, mask
);
1165 /* brc mask,jmp_off (branch instruction needs 4 bytes) */
1166 jmp_off
= addrs
[i
+ off
+ 1] - (addrs
[i
+ 1] - 4);
1167 EMIT4_PCREL(0xa7040000 | mask
<< 8, jmp_off
);
1172 case BPF_LD
| BPF_ABS
| BPF_B
: /* b0 = *(u8 *) (skb->data+imm) */
1173 case BPF_LD
| BPF_IND
| BPF_B
: /* b0 = *(u8 *) (skb->data+imm+src) */
1174 if ((BPF_MODE(insn
->code
) == BPF_ABS
) && (imm
>= 0))
1175 func_addr
= __pa(sk_load_byte_pos
);
1177 func_addr
= __pa(sk_load_byte
);
1179 case BPF_LD
| BPF_ABS
| BPF_H
: /* b0 = *(u16 *) (skb->data+imm) */
1180 case BPF_LD
| BPF_IND
| BPF_H
: /* b0 = *(u16 *) (skb->data+imm+src) */
1181 if ((BPF_MODE(insn
->code
) == BPF_ABS
) && (imm
>= 0))
1182 func_addr
= __pa(sk_load_half_pos
);
1184 func_addr
= __pa(sk_load_half
);
1186 case BPF_LD
| BPF_ABS
| BPF_W
: /* b0 = *(u32 *) (skb->data+imm) */
1187 case BPF_LD
| BPF_IND
| BPF_W
: /* b0 = *(u32 *) (skb->data+imm+src) */
1188 if ((BPF_MODE(insn
->code
) == BPF_ABS
) && (imm
>= 0))
1189 func_addr
= __pa(sk_load_word_pos
);
1191 func_addr
= __pa(sk_load_word
);
1194 jit
->seen
|= SEEN_SKB
| SEEN_RET0
| SEEN_FUNC
;
1195 REG_SET_SEEN(REG_14
); /* Return address of possible func call */
1199 * BPF_REG_6 (R7) : skb pointer
1200 * REG_SKB_DATA (R12): skb data pointer
1203 * BPF_REG_2 (R3) : offset of byte(s) to fetch in skb
1204 * BPF_REG_5 (R6) : return address
1207 * BPF_REG_0 (R14): data read from skb
1209 * Scratch registers (BPF_REG_1-5)
1212 /* Call function: llilf %w1,func_addr */
1213 EMIT6_IMM(0xc00f0000, REG_W1
, func_addr
);
1215 /* Offset: lgfi %b2,imm */
1216 EMIT6_IMM(0xc0010000, BPF_REG_2
, imm
);
1217 if (BPF_MODE(insn
->code
) == BPF_IND
)
1218 /* agfr %b2,%src (%src is s32 here) */
1219 EMIT4(0xb9180000, BPF_REG_2
, src_reg
);
1221 /* basr %b5,%w1 (%b5 is call saved) */
1222 EMIT2(0x0d00, BPF_REG_5
, REG_W1
);
1225 * Note: For fast access we jump directly after the
1226 * jnz instruction from bpf_jit.S
1229 EMIT4_PCREL(0xa7740000, jit
->ret0_ip
- jit
->prg
);
1231 default: /* too complex, give up */
1232 pr_err("Unknown opcode %02x\n", insn
->code
);
1239 * Compile eBPF program into s390x code
1241 static int bpf_jit_prog(struct bpf_jit
*jit
, struct bpf_prog
*fp
)
1245 jit
->lit
= jit
->lit_start
;
1248 bpf_jit_prologue(jit
, bpf_prog_was_classic(fp
));
1249 for (i
= 0; i
< fp
->len
; i
+= insn_count
) {
1250 insn_count
= bpf_jit_insn(jit
, fp
, i
);
1253 jit
->addrs
[i
+ 1] = jit
->prg
; /* Next instruction address */
1255 bpf_jit_epilogue(jit
);
1257 jit
->lit_start
= jit
->prg
;
1258 jit
->size
= jit
->lit
;
1259 jit
->size_prg
= jit
->prg
;
1264 * Classic BPF function stub. BPF programs will be converted into
1265 * eBPF and then bpf_int_jit_compile() will be called.
1267 void bpf_jit_compile(struct bpf_prog
*fp
)
1272 * Compile eBPF program "fp"
1274 void bpf_int_jit_compile(struct bpf_prog
*fp
)
1276 struct bpf_binary_header
*header
;
1280 if (!bpf_jit_enable
)
1282 memset(&jit
, 0, sizeof(jit
));
1283 jit
.addrs
= kcalloc(fp
->len
+ 1, sizeof(*jit
.addrs
), GFP_KERNEL
);
1284 if (jit
.addrs
== NULL
)
1287 * Three initial passes:
1288 * - 1/2: Determine clobbered registers
1289 * - 3: Calculate program size and addrs arrray
1291 for (pass
= 1; pass
<= 3; pass
++) {
1292 if (bpf_jit_prog(&jit
, fp
))
1296 * Final pass: Allocate and generate program
1298 if (jit
.size
>= BPF_SIZE_MAX
)
1300 header
= bpf_jit_binary_alloc(jit
.size
, &jit
.prg_buf
, 2, jit_fill_hole
);
1303 if (bpf_jit_prog(&jit
, fp
))
1305 if (bpf_jit_enable
> 1) {
1306 bpf_jit_dump(fp
->len
, jit
.size
, pass
, jit
.prg_buf
);
1308 print_fn_code(jit
.prg_buf
, jit
.size_prg
);
1311 set_memory_ro((unsigned long)header
, header
->pages
);
1312 fp
->bpf_func
= (void *) jit
.prg_buf
;
1322 void bpf_jit_free(struct bpf_prog
*fp
)
1324 unsigned long addr
= (unsigned long)fp
->bpf_func
& PAGE_MASK
;
1325 struct bpf_binary_header
*header
= (void *)addr
;
1330 set_memory_rw(addr
, header
->pages
);
1331 bpf_jit_binary_free(header
);
1334 bpf_prog_unlock_free(fp
);