1 // SPDX-License-Identifier: GPL-2.0
3 * BPF Jit compiler for s390.
5 * Minimum build requirements:
7 * - HAVE_MARCH_Z196_FEATURES: laal, laalg
8 * - HAVE_MARCH_Z10_FEATURES: msfi, cgrj, clgrj
9 * - HAVE_MARCH_Z9_109_FEATURES: alfi, llilf, clfi, oilf, nilf
13 * Copyright IBM Corp. 2012,2015
15 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
16 * Michael Holzheu <holzheu@linux.vnet.ibm.com>
19 #define KMSG_COMPONENT "bpf_jit"
20 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
22 #include <linux/netdevice.h>
23 #include <linux/filter.h>
24 #include <linux/init.h>
25 #include <linux/bpf.h>
26 #include <asm/cacheflush.h>
28 #include <asm/facility.h>
29 #include <asm/nospec-branch.h>
30 #include <asm/set_memory.h>
34 u32 seen
; /* Flags to remember seen eBPF instructions */
35 u32 seen_reg
[16]; /* Array to remember which registers are used */
36 u32
*addrs
; /* Array with relative instruction addresses */
37 u8
*prg_buf
; /* Start of program */
38 int size
; /* Size of program and literal pool */
39 int size_prg
; /* Size of program */
40 int prg
; /* Current position in program */
41 int lit_start
; /* Start of literal pool */
42 int lit
; /* Current position in literal pool */
43 int base_ip
; /* Base address for literal pool */
44 int ret0_ip
; /* Address of return 0 */
45 int exit_ip
; /* Address of exit */
46 int r1_thunk_ip
; /* Address of expoline thunk for 'br %r1' */
47 int r14_thunk_ip
; /* Address of expoline thunk for 'br %r14' */
48 int tail_call_start
; /* Tail call start offset */
49 int labels
[1]; /* Labels for local jumps */
52 #define BPF_SIZE_MAX 0xffff /* Max size for program (16 bit branches) */
54 #define SEEN_MEM (1 << 0) /* use mem[] for temporary storage */
55 #define SEEN_RET0 (1 << 1) /* ret0_ip points to a valid return 0 */
56 #define SEEN_LITERAL (1 << 2) /* code uses literals */
57 #define SEEN_FUNC (1 << 3) /* calls C functions */
58 #define SEEN_TAIL_CALL (1 << 4) /* code uses tail calls */
59 #define SEEN_REG_AX (1 << 5) /* code uses constant blinding */
60 #define SEEN_STACK (SEEN_FUNC | SEEN_MEM)
65 #define REG_W0 (MAX_BPF_JIT_REG + 0) /* Work register 1 (even) */
66 #define REG_W1 (MAX_BPF_JIT_REG + 1) /* Work register 2 (odd) */
67 #define REG_L (MAX_BPF_JIT_REG + 2) /* Literal pool register */
68 #define REG_15 (MAX_BPF_JIT_REG + 3) /* Register 15 */
69 #define REG_0 REG_W0 /* Register 0 */
70 #define REG_1 REG_W1 /* Register 1 */
71 #define REG_2 BPF_REG_1 /* Register 2 */
72 #define REG_14 BPF_REG_0 /* Register 14 */
75 * Mapping of BPF registers to s390 registers
77 static const int reg2hex
[] = {
80 /* Function parameters */
86 /* Call saved registers */
91 /* BPF stack pointer */
93 /* Register for blinding */
95 /* Work registers for s390x backend */
102 static inline u32
reg(u32 dst_reg
, u32 src_reg
)
104 return reg2hex
[dst_reg
] << 4 | reg2hex
[src_reg
];
107 static inline u32
reg_high(u32 reg
)
109 return reg2hex
[reg
] << 4;
112 static inline void reg_set_seen(struct bpf_jit
*jit
, u32 b1
)
114 u32 r1
= reg2hex
[b1
];
116 if (!jit
->seen_reg
[r1
] && r1
>= 6 && r1
<= 15)
117 jit
->seen_reg
[r1
] = 1;
120 #define REG_SET_SEEN(b1) \
122 reg_set_seen(jit, b1); \
125 #define REG_SEEN(b1) jit->seen_reg[reg2hex[(b1)]]
128 * EMIT macros for code generation
134 *(u16 *) (jit->prg_buf + jit->prg) = op; \
138 #define EMIT2(op, b1, b2) \
140 _EMIT2(op | reg(b1, b2)); \
148 *(u32 *) (jit->prg_buf + jit->prg) = op; \
152 #define EMIT4(op, b1, b2) \
154 _EMIT4(op | reg(b1, b2)); \
159 #define EMIT4_RRF(op, b1, b2, b3) \
161 _EMIT4(op | reg_high(b3) << 8 | reg(b1, b2)); \
167 #define _EMIT4_DISP(op, disp) \
169 unsigned int __disp = (disp) & 0xfff; \
170 _EMIT4(op | __disp); \
173 #define EMIT4_DISP(op, b1, b2, disp) \
175 _EMIT4_DISP(op | reg_high(b1) << 16 | \
176 reg_high(b2) << 8, disp); \
181 #define EMIT4_IMM(op, b1, imm) \
183 unsigned int __imm = (imm) & 0xffff; \
184 _EMIT4(op | reg_high(b1) << 16 | __imm); \
188 #define EMIT4_PCREL(op, pcrel) \
190 long __pcrel = ((pcrel) >> 1) & 0xffff; \
191 _EMIT4(op | __pcrel); \
194 #define _EMIT6(op1, op2) \
196 if (jit->prg_buf) { \
197 *(u32 *) (jit->prg_buf + jit->prg) = op1; \
198 *(u16 *) (jit->prg_buf + jit->prg + 4) = op2; \
203 #define _EMIT6_DISP(op1, op2, disp) \
205 unsigned int __disp = (disp) & 0xfff; \
206 _EMIT6(op1 | __disp, op2); \
209 #define _EMIT6_DISP_LH(op1, op2, disp) \
211 u32 _disp = (u32) disp; \
212 unsigned int __disp_h = _disp & 0xff000; \
213 unsigned int __disp_l = _disp & 0x00fff; \
214 _EMIT6(op1 | __disp_l, op2 | __disp_h >> 4); \
217 #define EMIT6_DISP_LH(op1, op2, b1, b2, b3, disp) \
219 _EMIT6_DISP_LH(op1 | reg(b1, b2) << 16 | \
220 reg_high(b3) << 8, op2, disp); \
226 #define EMIT6_PCREL_LABEL(op1, op2, b1, b2, label, mask) \
228 int rel = (jit->labels[label] - jit->prg) >> 1; \
229 _EMIT6(op1 | reg(b1, b2) << 16 | (rel & 0xffff), \
235 #define EMIT6_PCREL_IMM_LABEL(op1, op2, b1, imm, label, mask) \
237 int rel = (jit->labels[label] - jit->prg) >> 1; \
238 _EMIT6(op1 | (reg_high(b1) | mask) << 16 | \
239 (rel & 0xffff), op2 | (imm & 0xff) << 8); \
241 BUILD_BUG_ON(((unsigned long) imm) > 0xff); \
244 #define EMIT6_PCREL(op1, op2, b1, b2, i, off, mask) \
246 /* Branch instruction needs 6 bytes */ \
247 int rel = (addrs[i + off + 1] - (addrs[i + 1] - 6)) / 2;\
248 _EMIT6(op1 | reg(b1, b2) << 16 | (rel & 0xffff), op2 | mask); \
253 #define EMIT6_PCREL_RILB(op, b, target) \
255 int rel = (target - jit->prg) / 2; \
256 _EMIT6(op | reg_high(b) << 16 | rel >> 16, rel & 0xffff); \
260 #define EMIT6_PCREL_RIL(op, target) \
262 int rel = (target - jit->prg) / 2; \
263 _EMIT6(op | rel >> 16, rel & 0xffff); \
266 #define _EMIT6_IMM(op, imm) \
268 unsigned int __imm = (imm); \
269 _EMIT6(op | (__imm >> 16), __imm & 0xffff); \
272 #define EMIT6_IMM(op, b1, imm) \
274 _EMIT6_IMM(op | reg_high(b1) << 16, imm); \
278 #define EMIT_CONST_U32(val) \
281 ret = jit->lit - jit->base_ip; \
282 jit->seen |= SEEN_LITERAL; \
284 *(u32 *) (jit->prg_buf + jit->lit) = (u32) val; \
289 #define EMIT_CONST_U64(val) \
292 ret = jit->lit - jit->base_ip; \
293 jit->seen |= SEEN_LITERAL; \
295 *(u64 *) (jit->prg_buf + jit->lit) = (u64) val; \
300 #define EMIT_ZERO(b1) \
302 /* llgfr %dst,%dst (zero extend to 64 bit) */ \
303 EMIT4(0xb9160000, b1, b1); \
308 * Fill whole space with illegal instructions
310 static void jit_fill_hole(void *area
, unsigned int size
)
312 memset(area
, 0, size
);
316 * Save registers from "rs" (register start) to "re" (register end) on stack
318 static void save_regs(struct bpf_jit
*jit
, u32 rs
, u32 re
)
320 u32 off
= STK_OFF_R6
+ (rs
- 6) * 8;
323 /* stg %rs,off(%r15) */
324 _EMIT6(0xe300f000 | rs
<< 20 | off
, 0x0024);
326 /* stmg %rs,%re,off(%r15) */
327 _EMIT6_DISP(0xeb00f000 | rs
<< 20 | re
<< 16, 0x0024, off
);
331 * Restore registers from "rs" (register start) to "re" (register end) on stack
333 static void restore_regs(struct bpf_jit
*jit
, u32 rs
, u32 re
, u32 stack_depth
)
335 u32 off
= STK_OFF_R6
+ (rs
- 6) * 8;
337 if (jit
->seen
& SEEN_STACK
)
338 off
+= STK_OFF
+ stack_depth
;
341 /* lg %rs,off(%r15) */
342 _EMIT6(0xe300f000 | rs
<< 20 | off
, 0x0004);
344 /* lmg %rs,%re,off(%r15) */
345 _EMIT6_DISP(0xeb00f000 | rs
<< 20 | re
<< 16, 0x0004, off
);
349 * Return first seen register (from start)
351 static int get_start(struct bpf_jit
*jit
, int start
)
355 for (i
= start
; i
<= 15; i
++) {
356 if (jit
->seen_reg
[i
])
363 * Return last seen register (from start) (gap >= 2)
365 static int get_end(struct bpf_jit
*jit
, int start
)
369 for (i
= start
; i
< 15; i
++) {
370 if (!jit
->seen_reg
[i
] && !jit
->seen_reg
[i
+ 1])
373 return jit
->seen_reg
[15] ? 15 : 14;
377 #define REGS_RESTORE 0
379 * Save and restore clobbered registers (6-15) on stack.
380 * We save/restore registers in chunks with gap >= 2 registers.
382 static void save_restore_regs(struct bpf_jit
*jit
, int op
, u32 stack_depth
)
388 rs
= get_start(jit
, re
);
391 re
= get_end(jit
, rs
+ 1);
393 save_regs(jit
, rs
, re
);
395 restore_regs(jit
, rs
, re
, stack_depth
);
401 * Emit function prologue
403 * Save registers and create stack frame if necessary.
404 * See stack frame layout desription in "bpf_jit.h"!
406 static void bpf_jit_prologue(struct bpf_jit
*jit
, u32 stack_depth
)
408 if (jit
->seen
& SEEN_TAIL_CALL
) {
409 /* xc STK_OFF_TCCNT(4,%r15),STK_OFF_TCCNT(%r15) */
410 _EMIT6(0xd703f000 | STK_OFF_TCCNT
, 0xf000 | STK_OFF_TCCNT
);
412 /* j tail_call_start: NOP if no tail calls are used */
413 EMIT4_PCREL(0xa7f40000, 6);
416 /* Tail calls have to skip above initialization */
417 jit
->tail_call_start
= jit
->prg
;
419 save_restore_regs(jit
, REGS_SAVE
, stack_depth
);
420 /* Setup literal pool */
421 if (jit
->seen
& SEEN_LITERAL
) {
423 EMIT2(0x0d00, REG_L
, REG_0
);
424 jit
->base_ip
= jit
->prg
;
426 /* Setup stack and backchain */
427 if (jit
->seen
& SEEN_STACK
) {
428 if (jit
->seen
& SEEN_FUNC
)
429 /* lgr %w1,%r15 (backchain) */
430 EMIT4(0xb9040000, REG_W1
, REG_15
);
431 /* la %bfp,STK_160_UNUSED(%r15) (BPF frame pointer) */
432 EMIT4_DISP(0x41000000, BPF_REG_FP
, REG_15
, STK_160_UNUSED
);
433 /* aghi %r15,-STK_OFF */
434 EMIT4_IMM(0xa70b0000, REG_15
, -(STK_OFF
+ stack_depth
));
435 if (jit
->seen
& SEEN_FUNC
)
436 /* stg %w1,152(%r15) (backchain) */
437 EMIT6_DISP_LH(0xe3000000, 0x0024, REG_W1
, REG_0
,
445 static void bpf_jit_epilogue(struct bpf_jit
*jit
, u32 stack_depth
)
448 if (jit
->seen
& SEEN_RET0
) {
449 jit
->ret0_ip
= jit
->prg
;
451 EMIT4_IMM(0xa7090000, BPF_REG_0
, 0);
453 jit
->exit_ip
= jit
->prg
;
454 /* Load exit code: lgr %r2,%b0 */
455 EMIT4(0xb9040000, REG_2
, BPF_REG_0
);
456 /* Restore registers */
457 save_restore_regs(jit
, REGS_RESTORE
, stack_depth
);
458 if (IS_ENABLED(CC_USING_EXPOLINE
) && !nospec_disable
) {
459 jit
->r14_thunk_ip
= jit
->prg
;
460 /* Generate __s390_indirect_jump_r14 thunk */
461 if (test_facility(35)) {
463 EMIT6_PCREL_RIL(0xc6000000, jit
->prg
+ 10);
466 EMIT6_PCREL_RILB(0xc0000000, REG_1
, jit
->prg
+ 14);
468 EMIT4_DISP(0x44000000, REG_0
, REG_1
, 0);
471 EMIT4_PCREL(0xa7f40000, 0);
476 if (IS_ENABLED(CC_USING_EXPOLINE
) && !nospec_disable
&&
477 (jit
->seen
& SEEN_FUNC
)) {
478 jit
->r1_thunk_ip
= jit
->prg
;
479 /* Generate __s390_indirect_jump_r1 thunk */
480 if (test_facility(35)) {
482 EMIT6_PCREL_RIL(0xc6000000, jit
->prg
+ 10);
484 EMIT4_PCREL(0xa7f40000, 0);
488 /* ex 0,S390_lowcore.br_r1_tampoline */
489 EMIT4_DISP(0x44000000, REG_0
, REG_0
,
490 offsetof(struct lowcore
, br_r1_trampoline
));
492 EMIT4_PCREL(0xa7f40000, 0);
498 * Compile one eBPF instruction into s390x code
500 * NOTE: Use noinline because for gcov (-fprofile-arcs) gcc allocates a lot of
501 * stack space for the large switch statement.
503 static noinline
int bpf_jit_insn(struct bpf_jit
*jit
, struct bpf_prog
*fp
, int i
)
505 struct bpf_insn
*insn
= &fp
->insnsi
[i
];
506 int jmp_off
, last
, insn_count
= 1;
507 u32 dst_reg
= insn
->dst_reg
;
508 u32 src_reg
= insn
->src_reg
;
509 u32
*addrs
= jit
->addrs
;
514 if (dst_reg
== BPF_REG_AX
|| src_reg
== BPF_REG_AX
)
515 jit
->seen
|= SEEN_REG_AX
;
516 switch (insn
->code
) {
520 case BPF_ALU
| BPF_MOV
| BPF_X
: /* dst = (u32) src */
521 /* llgfr %dst,%src */
522 EMIT4(0xb9160000, dst_reg
, src_reg
);
524 case BPF_ALU64
| BPF_MOV
| BPF_X
: /* dst = src */
526 EMIT4(0xb9040000, dst_reg
, src_reg
);
528 case BPF_ALU
| BPF_MOV
| BPF_K
: /* dst = (u32) imm */
530 EMIT6_IMM(0xc00f0000, dst_reg
, imm
);
532 case BPF_ALU64
| BPF_MOV
| BPF_K
: /* dst = imm */
534 EMIT6_IMM(0xc0010000, dst_reg
, imm
);
539 case BPF_LD
| BPF_IMM
| BPF_DW
: /* dst = (u64) imm */
541 /* 16 byte instruction that uses two 'struct bpf_insn' */
544 imm64
= (u64
)(u32
) insn
[0].imm
| ((u64
)(u32
) insn
[1].imm
) << 32;
545 /* lg %dst,<d(imm)>(%l) */
546 EMIT6_DISP_LH(0xe3000000, 0x0004, dst_reg
, REG_0
, REG_L
,
547 EMIT_CONST_U64(imm64
));
554 case BPF_ALU
| BPF_ADD
| BPF_X
: /* dst = (u32) dst + (u32) src */
556 EMIT2(0x1a00, dst_reg
, src_reg
);
559 case BPF_ALU64
| BPF_ADD
| BPF_X
: /* dst = dst + src */
561 EMIT4(0xb9080000, dst_reg
, src_reg
);
563 case BPF_ALU
| BPF_ADD
| BPF_K
: /* dst = (u32) dst + (u32) imm */
567 EMIT6_IMM(0xc20b0000, dst_reg
, imm
);
570 case BPF_ALU64
| BPF_ADD
| BPF_K
: /* dst = dst + imm */
574 EMIT6_IMM(0xc2080000, dst_reg
, imm
);
579 case BPF_ALU
| BPF_SUB
| BPF_X
: /* dst = (u32) dst - (u32) src */
581 EMIT2(0x1b00, dst_reg
, src_reg
);
584 case BPF_ALU64
| BPF_SUB
| BPF_X
: /* dst = dst - src */
586 EMIT4(0xb9090000, dst_reg
, src_reg
);
588 case BPF_ALU
| BPF_SUB
| BPF_K
: /* dst = (u32) dst - (u32) imm */
592 EMIT6_IMM(0xc20b0000, dst_reg
, -imm
);
595 case BPF_ALU64
| BPF_SUB
| BPF_K
: /* dst = dst - imm */
599 EMIT6_IMM(0xc2080000, dst_reg
, -imm
);
604 case BPF_ALU
| BPF_MUL
| BPF_X
: /* dst = (u32) dst * (u32) src */
606 EMIT4(0xb2520000, dst_reg
, src_reg
);
609 case BPF_ALU64
| BPF_MUL
| BPF_X
: /* dst = dst * src */
611 EMIT4(0xb90c0000, dst_reg
, src_reg
);
613 case BPF_ALU
| BPF_MUL
| BPF_K
: /* dst = (u32) dst * (u32) imm */
617 EMIT6_IMM(0xc2010000, dst_reg
, imm
);
620 case BPF_ALU64
| BPF_MUL
| BPF_K
: /* dst = dst * imm */
624 EMIT6_IMM(0xc2000000, dst_reg
, imm
);
629 case BPF_ALU
| BPF_DIV
| BPF_X
: /* dst = (u32) dst / (u32) src */
630 case BPF_ALU
| BPF_MOD
| BPF_X
: /* dst = (u32) dst % (u32) src */
632 int rc_reg
= BPF_OP(insn
->code
) == BPF_DIV
? REG_W1
: REG_W0
;
635 EMIT4_IMM(0xa7080000, REG_W0
, 0);
637 EMIT2(0x1800, REG_W1
, dst_reg
);
639 EMIT4(0xb9970000, REG_W0
, src_reg
);
641 EMIT4(0xb9160000, dst_reg
, rc_reg
);
644 case BPF_ALU64
| BPF_DIV
| BPF_X
: /* dst = dst / src */
645 case BPF_ALU64
| BPF_MOD
| BPF_X
: /* dst = dst % src */
647 int rc_reg
= BPF_OP(insn
->code
) == BPF_DIV
? REG_W1
: REG_W0
;
650 EMIT4_IMM(0xa7090000, REG_W0
, 0);
652 EMIT4(0xb9040000, REG_W1
, dst_reg
);
654 EMIT4(0xb9870000, REG_W0
, src_reg
);
656 EMIT4(0xb9040000, dst_reg
, rc_reg
);
659 case BPF_ALU
| BPF_DIV
| BPF_K
: /* dst = (u32) dst / (u32) imm */
660 case BPF_ALU
| BPF_MOD
| BPF_K
: /* dst = (u32) dst % (u32) imm */
662 int rc_reg
= BPF_OP(insn
->code
) == BPF_DIV
? REG_W1
: REG_W0
;
665 if (BPF_OP(insn
->code
) == BPF_MOD
)
667 EMIT4_IMM(0xa7090000, dst_reg
, 0);
671 EMIT4_IMM(0xa7080000, REG_W0
, 0);
673 EMIT2(0x1800, REG_W1
, dst_reg
);
674 /* dl %w0,<d(imm)>(%l) */
675 EMIT6_DISP_LH(0xe3000000, 0x0097, REG_W0
, REG_0
, REG_L
,
676 EMIT_CONST_U32(imm
));
678 EMIT4(0xb9160000, dst_reg
, rc_reg
);
681 case BPF_ALU64
| BPF_DIV
| BPF_K
: /* dst = dst / imm */
682 case BPF_ALU64
| BPF_MOD
| BPF_K
: /* dst = dst % imm */
684 int rc_reg
= BPF_OP(insn
->code
) == BPF_DIV
? REG_W1
: REG_W0
;
687 if (BPF_OP(insn
->code
) == BPF_MOD
)
689 EMIT4_IMM(0xa7090000, dst_reg
, 0);
693 EMIT4_IMM(0xa7090000, REG_W0
, 0);
695 EMIT4(0xb9040000, REG_W1
, dst_reg
);
696 /* dlg %w0,<d(imm)>(%l) */
697 EMIT6_DISP_LH(0xe3000000, 0x0087, REG_W0
, REG_0
, REG_L
,
698 EMIT_CONST_U64(imm
));
700 EMIT4(0xb9040000, dst_reg
, rc_reg
);
706 case BPF_ALU
| BPF_AND
| BPF_X
: /* dst = (u32) dst & (u32) src */
708 EMIT2(0x1400, dst_reg
, src_reg
);
711 case BPF_ALU64
| BPF_AND
| BPF_X
: /* dst = dst & src */
713 EMIT4(0xb9800000, dst_reg
, src_reg
);
715 case BPF_ALU
| BPF_AND
| BPF_K
: /* dst = (u32) dst & (u32) imm */
717 EMIT6_IMM(0xc00b0000, dst_reg
, imm
);
720 case BPF_ALU64
| BPF_AND
| BPF_K
: /* dst = dst & imm */
721 /* ng %dst,<d(imm)>(%l) */
722 EMIT6_DISP_LH(0xe3000000, 0x0080, dst_reg
, REG_0
, REG_L
,
723 EMIT_CONST_U64(imm
));
728 case BPF_ALU
| BPF_OR
| BPF_X
: /* dst = (u32) dst | (u32) src */
730 EMIT2(0x1600, dst_reg
, src_reg
);
733 case BPF_ALU64
| BPF_OR
| BPF_X
: /* dst = dst | src */
735 EMIT4(0xb9810000, dst_reg
, src_reg
);
737 case BPF_ALU
| BPF_OR
| BPF_K
: /* dst = (u32) dst | (u32) imm */
739 EMIT6_IMM(0xc00d0000, dst_reg
, imm
);
742 case BPF_ALU64
| BPF_OR
| BPF_K
: /* dst = dst | imm */
743 /* og %dst,<d(imm)>(%l) */
744 EMIT6_DISP_LH(0xe3000000, 0x0081, dst_reg
, REG_0
, REG_L
,
745 EMIT_CONST_U64(imm
));
750 case BPF_ALU
| BPF_XOR
| BPF_X
: /* dst = (u32) dst ^ (u32) src */
752 EMIT2(0x1700, dst_reg
, src_reg
);
755 case BPF_ALU64
| BPF_XOR
| BPF_X
: /* dst = dst ^ src */
757 EMIT4(0xb9820000, dst_reg
, src_reg
);
759 case BPF_ALU
| BPF_XOR
| BPF_K
: /* dst = (u32) dst ^ (u32) imm */
763 EMIT6_IMM(0xc0070000, dst_reg
, imm
);
766 case BPF_ALU64
| BPF_XOR
| BPF_K
: /* dst = dst ^ imm */
767 /* xg %dst,<d(imm)>(%l) */
768 EMIT6_DISP_LH(0xe3000000, 0x0082, dst_reg
, REG_0
, REG_L
,
769 EMIT_CONST_U64(imm
));
774 case BPF_ALU
| BPF_LSH
| BPF_X
: /* dst = (u32) dst << (u32) src */
775 /* sll %dst,0(%src) */
776 EMIT4_DISP(0x89000000, dst_reg
, src_reg
, 0);
779 case BPF_ALU64
| BPF_LSH
| BPF_X
: /* dst = dst << src */
780 /* sllg %dst,%dst,0(%src) */
781 EMIT6_DISP_LH(0xeb000000, 0x000d, dst_reg
, dst_reg
, src_reg
, 0);
783 case BPF_ALU
| BPF_LSH
| BPF_K
: /* dst = (u32) dst << (u32) imm */
786 /* sll %dst,imm(%r0) */
787 EMIT4_DISP(0x89000000, dst_reg
, REG_0
, imm
);
790 case BPF_ALU64
| BPF_LSH
| BPF_K
: /* dst = dst << imm */
793 /* sllg %dst,%dst,imm(%r0) */
794 EMIT6_DISP_LH(0xeb000000, 0x000d, dst_reg
, dst_reg
, REG_0
, imm
);
799 case BPF_ALU
| BPF_RSH
| BPF_X
: /* dst = (u32) dst >> (u32) src */
800 /* srl %dst,0(%src) */
801 EMIT4_DISP(0x88000000, dst_reg
, src_reg
, 0);
804 case BPF_ALU64
| BPF_RSH
| BPF_X
: /* dst = dst >> src */
805 /* srlg %dst,%dst,0(%src) */
806 EMIT6_DISP_LH(0xeb000000, 0x000c, dst_reg
, dst_reg
, src_reg
, 0);
808 case BPF_ALU
| BPF_RSH
| BPF_K
: /* dst = (u32) dst >> (u32) imm */
811 /* srl %dst,imm(%r0) */
812 EMIT4_DISP(0x88000000, dst_reg
, REG_0
, imm
);
815 case BPF_ALU64
| BPF_RSH
| BPF_K
: /* dst = dst >> imm */
818 /* srlg %dst,%dst,imm(%r0) */
819 EMIT6_DISP_LH(0xeb000000, 0x000c, dst_reg
, dst_reg
, REG_0
, imm
);
824 case BPF_ALU64
| BPF_ARSH
| BPF_X
: /* ((s64) dst) >>= src */
825 /* srag %dst,%dst,0(%src) */
826 EMIT6_DISP_LH(0xeb000000, 0x000a, dst_reg
, dst_reg
, src_reg
, 0);
828 case BPF_ALU64
| BPF_ARSH
| BPF_K
: /* ((s64) dst) >>= imm */
831 /* srag %dst,%dst,imm(%r0) */
832 EMIT6_DISP_LH(0xeb000000, 0x000a, dst_reg
, dst_reg
, REG_0
, imm
);
837 case BPF_ALU
| BPF_NEG
: /* dst = (u32) -dst */
839 EMIT2(0x1300, dst_reg
, dst_reg
);
842 case BPF_ALU64
| BPF_NEG
: /* dst = -dst */
844 EMIT4(0xb9130000, dst_reg
, dst_reg
);
849 case BPF_ALU
| BPF_END
| BPF_FROM_BE
:
850 /* s390 is big endian, therefore only clear high order bytes */
852 case 16: /* dst = (u16) cpu_to_be16(dst) */
853 /* llghr %dst,%dst */
854 EMIT4(0xb9850000, dst_reg
, dst_reg
);
856 case 32: /* dst = (u32) cpu_to_be32(dst) */
857 /* llgfr %dst,%dst */
858 EMIT4(0xb9160000, dst_reg
, dst_reg
);
860 case 64: /* dst = (u64) cpu_to_be64(dst) */
864 case BPF_ALU
| BPF_END
| BPF_FROM_LE
:
866 case 16: /* dst = (u16) cpu_to_le16(dst) */
868 EMIT4(0xb91f0000, dst_reg
, dst_reg
);
869 /* srl %dst,16(%r0) */
870 EMIT4_DISP(0x88000000, dst_reg
, REG_0
, 16);
871 /* llghr %dst,%dst */
872 EMIT4(0xb9850000, dst_reg
, dst_reg
);
874 case 32: /* dst = (u32) cpu_to_le32(dst) */
876 EMIT4(0xb91f0000, dst_reg
, dst_reg
);
877 /* llgfr %dst,%dst */
878 EMIT4(0xb9160000, dst_reg
, dst_reg
);
880 case 64: /* dst = (u64) cpu_to_le64(dst) */
881 /* lrvgr %dst,%dst */
882 EMIT4(0xb90f0000, dst_reg
, dst_reg
);
889 case BPF_STX
| BPF_MEM
| BPF_B
: /* *(u8 *)(dst + off) = src_reg */
890 /* stcy %src,off(%dst) */
891 EMIT6_DISP_LH(0xe3000000, 0x0072, src_reg
, dst_reg
, REG_0
, off
);
892 jit
->seen
|= SEEN_MEM
;
894 case BPF_STX
| BPF_MEM
| BPF_H
: /* (u16 *)(dst + off) = src */
895 /* sthy %src,off(%dst) */
896 EMIT6_DISP_LH(0xe3000000, 0x0070, src_reg
, dst_reg
, REG_0
, off
);
897 jit
->seen
|= SEEN_MEM
;
899 case BPF_STX
| BPF_MEM
| BPF_W
: /* *(u32 *)(dst + off) = src */
900 /* sty %src,off(%dst) */
901 EMIT6_DISP_LH(0xe3000000, 0x0050, src_reg
, dst_reg
, REG_0
, off
);
902 jit
->seen
|= SEEN_MEM
;
904 case BPF_STX
| BPF_MEM
| BPF_DW
: /* (u64 *)(dst + off) = src */
905 /* stg %src,off(%dst) */
906 EMIT6_DISP_LH(0xe3000000, 0x0024, src_reg
, dst_reg
, REG_0
, off
);
907 jit
->seen
|= SEEN_MEM
;
909 case BPF_ST
| BPF_MEM
| BPF_B
: /* *(u8 *)(dst + off) = imm */
911 EMIT4_IMM(0xa7080000, REG_W0
, (u8
) imm
);
912 /* stcy %w0,off(dst) */
913 EMIT6_DISP_LH(0xe3000000, 0x0072, REG_W0
, dst_reg
, REG_0
, off
);
914 jit
->seen
|= SEEN_MEM
;
916 case BPF_ST
| BPF_MEM
| BPF_H
: /* (u16 *)(dst + off) = imm */
918 EMIT4_IMM(0xa7080000, REG_W0
, (u16
) imm
);
919 /* sthy %w0,off(dst) */
920 EMIT6_DISP_LH(0xe3000000, 0x0070, REG_W0
, dst_reg
, REG_0
, off
);
921 jit
->seen
|= SEEN_MEM
;
923 case BPF_ST
| BPF_MEM
| BPF_W
: /* *(u32 *)(dst + off) = imm */
925 EMIT6_IMM(0xc00f0000, REG_W0
, (u32
) imm
);
926 /* sty %w0,off(%dst) */
927 EMIT6_DISP_LH(0xe3000000, 0x0050, REG_W0
, dst_reg
, REG_0
, off
);
928 jit
->seen
|= SEEN_MEM
;
930 case BPF_ST
| BPF_MEM
| BPF_DW
: /* *(u64 *)(dst + off) = imm */
932 EMIT6_IMM(0xc0010000, REG_W0
, imm
);
933 /* stg %w0,off(%dst) */
934 EMIT6_DISP_LH(0xe3000000, 0x0024, REG_W0
, dst_reg
, REG_0
, off
);
935 jit
->seen
|= SEEN_MEM
;
938 * BPF_STX XADD (atomic_add)
940 case BPF_STX
| BPF_XADD
| BPF_W
: /* *(u32 *)(dst + off) += src */
941 /* laal %w0,%src,off(%dst) */
942 EMIT6_DISP_LH(0xeb000000, 0x00fa, REG_W0
, src_reg
,
944 jit
->seen
|= SEEN_MEM
;
946 case BPF_STX
| BPF_XADD
| BPF_DW
: /* *(u64 *)(dst + off) += src */
947 /* laalg %w0,%src,off(%dst) */
948 EMIT6_DISP_LH(0xeb000000, 0x00ea, REG_W0
, src_reg
,
950 jit
->seen
|= SEEN_MEM
;
955 case BPF_LDX
| BPF_MEM
| BPF_B
: /* dst = *(u8 *)(ul) (src + off) */
956 /* llgc %dst,0(off,%src) */
957 EMIT6_DISP_LH(0xe3000000, 0x0090, dst_reg
, src_reg
, REG_0
, off
);
958 jit
->seen
|= SEEN_MEM
;
960 case BPF_LDX
| BPF_MEM
| BPF_H
: /* dst = *(u16 *)(ul) (src + off) */
961 /* llgh %dst,0(off,%src) */
962 EMIT6_DISP_LH(0xe3000000, 0x0091, dst_reg
, src_reg
, REG_0
, off
);
963 jit
->seen
|= SEEN_MEM
;
965 case BPF_LDX
| BPF_MEM
| BPF_W
: /* dst = *(u32 *)(ul) (src + off) */
966 /* llgf %dst,off(%src) */
967 jit
->seen
|= SEEN_MEM
;
968 EMIT6_DISP_LH(0xe3000000, 0x0016, dst_reg
, src_reg
, REG_0
, off
);
970 case BPF_LDX
| BPF_MEM
| BPF_DW
: /* dst = *(u64 *)(ul) (src + off) */
971 /* lg %dst,0(off,%src) */
972 jit
->seen
|= SEEN_MEM
;
973 EMIT6_DISP_LH(0xe3000000, 0x0004, dst_reg
, src_reg
, REG_0
, off
);
978 case BPF_JMP
| BPF_CALL
:
981 * b0 = (__bpf_call_base + imm)(b1, b2, b3, b4, b5)
983 const u64 func
= (u64
)__bpf_call_base
+ imm
;
985 REG_SET_SEEN(BPF_REG_5
);
986 jit
->seen
|= SEEN_FUNC
;
987 /* lg %w1,<d(imm)>(%l) */
988 EMIT6_DISP_LH(0xe3000000, 0x0004, REG_W1
, REG_0
, REG_L
,
989 EMIT_CONST_U64(func
));
990 if (IS_ENABLED(CC_USING_EXPOLINE
) && !nospec_disable
) {
991 /* brasl %r14,__s390_indirect_jump_r1 */
992 EMIT6_PCREL_RILB(0xc0050000, REG_14
, jit
->r1_thunk_ip
);
995 EMIT2(0x0d00, REG_14
, REG_W1
);
997 /* lgr %b0,%r2: load return value into %b0 */
998 EMIT4(0xb9040000, BPF_REG_0
, REG_2
);
1001 case BPF_JMP
| BPF_TAIL_CALL
:
1004 * B1: pointer to ctx
1005 * B2: pointer to bpf_array
1006 * B3: index in bpf_array
1008 jit
->seen
|= SEEN_TAIL_CALL
;
1011 * if (index >= array->map.max_entries)
1015 /* llgf %w1,map.max_entries(%b2) */
1016 EMIT6_DISP_LH(0xe3000000, 0x0016, REG_W1
, REG_0
, BPF_REG_2
,
1017 offsetof(struct bpf_array
, map
.max_entries
));
1018 /* clgrj %b3,%w1,0xa,label0: if %b3 >= %w1 goto out */
1019 EMIT6_PCREL_LABEL(0xec000000, 0x0065, BPF_REG_3
,
1023 * if (tail_call_cnt++ > MAX_TAIL_CALL_CNT)
1027 if (jit
->seen
& SEEN_STACK
)
1028 off
= STK_OFF_TCCNT
+ STK_OFF
+ fp
->aux
->stack_depth
;
1030 off
= STK_OFF_TCCNT
;
1032 EMIT4_IMM(0xa7080000, REG_W0
, 1);
1033 /* laal %w1,%w0,off(%r15) */
1034 EMIT6_DISP_LH(0xeb000000, 0x00fa, REG_W1
, REG_W0
, REG_15
, off
);
1035 /* clij %w1,MAX_TAIL_CALL_CNT,0x2,label0 */
1036 EMIT6_PCREL_IMM_LABEL(0xec000000, 0x007f, REG_W1
,
1037 MAX_TAIL_CALL_CNT
, 0, 0x2);
1040 * prog = array->ptrs[index];
1045 /* sllg %r1,%b3,3: %r1 = index * 8 */
1046 EMIT6_DISP_LH(0xeb000000, 0x000d, REG_1
, BPF_REG_3
, REG_0
, 3);
1047 /* lg %r1,prog(%b2,%r1) */
1048 EMIT6_DISP_LH(0xe3000000, 0x0004, REG_1
, BPF_REG_2
,
1049 REG_1
, offsetof(struct bpf_array
, ptrs
));
1050 /* clgij %r1,0,0x8,label0 */
1051 EMIT6_PCREL_IMM_LABEL(0xec000000, 0x007d, REG_1
, 0, 0, 0x8);
1054 * Restore registers before calling function
1056 save_restore_regs(jit
, REGS_RESTORE
, fp
->aux
->stack_depth
);
1059 * goto *(prog->bpf_func + tail_call_start);
1062 /* lg %r1,bpf_func(%r1) */
1063 EMIT6_DISP_LH(0xe3000000, 0x0004, REG_1
, REG_1
, REG_0
,
1064 offsetof(struct bpf_prog
, bpf_func
));
1065 /* bc 0xf,tail_call_start(%r1) */
1066 _EMIT4(0x47f01000 + jit
->tail_call_start
);
1068 jit
->labels
[0] = jit
->prg
;
1070 case BPF_JMP
| BPF_EXIT
: /* return b0 */
1071 last
= (i
== fp
->len
- 1) ? 1 : 0;
1072 if (last
&& !(jit
->seen
& SEEN_RET0
))
1075 EMIT4_PCREL(0xa7f40000, jit
->exit_ip
- jit
->prg
);
1078 * Branch relative (number of skipped instructions) to offset on
1081 * Condition code to mask mapping:
1083 * CC | Description | Mask
1084 * ------------------------------
1085 * 0 | Operands equal | 8
1086 * 1 | First operand low | 4
1087 * 2 | First operand high | 2
1090 * For s390x relative branches: ip = ip + off_bytes
1091 * For BPF relative branches: insn = insn + off_insns + 1
1093 * For example for s390x with offset 0 we jump to the branch
1094 * instruction itself (loop) and for BPF with offset 0 we
1095 * branch to the instruction behind the branch.
1097 case BPF_JMP
| BPF_JA
: /* if (true) */
1098 mask
= 0xf000; /* j */
1100 case BPF_JMP
| BPF_JSGT
| BPF_K
: /* ((s64) dst > (s64) imm) */
1101 mask
= 0x2000; /* jh */
1103 case BPF_JMP
| BPF_JSLT
| BPF_K
: /* ((s64) dst < (s64) imm) */
1104 mask
= 0x4000; /* jl */
1106 case BPF_JMP
| BPF_JSGE
| BPF_K
: /* ((s64) dst >= (s64) imm) */
1107 mask
= 0xa000; /* jhe */
1109 case BPF_JMP
| BPF_JSLE
| BPF_K
: /* ((s64) dst <= (s64) imm) */
1110 mask
= 0xc000; /* jle */
1112 case BPF_JMP
| BPF_JGT
| BPF_K
: /* (dst_reg > imm) */
1113 mask
= 0x2000; /* jh */
1115 case BPF_JMP
| BPF_JLT
| BPF_K
: /* (dst_reg < imm) */
1116 mask
= 0x4000; /* jl */
1118 case BPF_JMP
| BPF_JGE
| BPF_K
: /* (dst_reg >= imm) */
1119 mask
= 0xa000; /* jhe */
1121 case BPF_JMP
| BPF_JLE
| BPF_K
: /* (dst_reg <= imm) */
1122 mask
= 0xc000; /* jle */
1124 case BPF_JMP
| BPF_JNE
| BPF_K
: /* (dst_reg != imm) */
1125 mask
= 0x7000; /* jne */
1127 case BPF_JMP
| BPF_JEQ
| BPF_K
: /* (dst_reg == imm) */
1128 mask
= 0x8000; /* je */
1130 case BPF_JMP
| BPF_JSET
| BPF_K
: /* (dst_reg & imm) */
1131 mask
= 0x7000; /* jnz */
1132 /* lgfi %w1,imm (load sign extend imm) */
1133 EMIT6_IMM(0xc0010000, REG_W1
, imm
);
1135 EMIT4(0xb9800000, REG_W1
, dst_reg
);
1138 case BPF_JMP
| BPF_JSGT
| BPF_X
: /* ((s64) dst > (s64) src) */
1139 mask
= 0x2000; /* jh */
1141 case BPF_JMP
| BPF_JSLT
| BPF_X
: /* ((s64) dst < (s64) src) */
1142 mask
= 0x4000; /* jl */
1144 case BPF_JMP
| BPF_JSGE
| BPF_X
: /* ((s64) dst >= (s64) src) */
1145 mask
= 0xa000; /* jhe */
1147 case BPF_JMP
| BPF_JSLE
| BPF_X
: /* ((s64) dst <= (s64) src) */
1148 mask
= 0xc000; /* jle */
1150 case BPF_JMP
| BPF_JGT
| BPF_X
: /* (dst > src) */
1151 mask
= 0x2000; /* jh */
1153 case BPF_JMP
| BPF_JLT
| BPF_X
: /* (dst < src) */
1154 mask
= 0x4000; /* jl */
1156 case BPF_JMP
| BPF_JGE
| BPF_X
: /* (dst >= src) */
1157 mask
= 0xa000; /* jhe */
1159 case BPF_JMP
| BPF_JLE
| BPF_X
: /* (dst <= src) */
1160 mask
= 0xc000; /* jle */
1162 case BPF_JMP
| BPF_JNE
| BPF_X
: /* (dst != src) */
1163 mask
= 0x7000; /* jne */
1165 case BPF_JMP
| BPF_JEQ
| BPF_X
: /* (dst == src) */
1166 mask
= 0x8000; /* je */
1168 case BPF_JMP
| BPF_JSET
| BPF_X
: /* (dst & src) */
1169 mask
= 0x7000; /* jnz */
1170 /* ngrk %w1,%dst,%src */
1171 EMIT4_RRF(0xb9e40000, REG_W1
, dst_reg
, src_reg
);
1174 /* lgfi %w1,imm (load sign extend imm) */
1175 EMIT6_IMM(0xc0010000, REG_W1
, imm
);
1176 /* cgrj %dst,%w1,mask,off */
1177 EMIT6_PCREL(0xec000000, 0x0064, dst_reg
, REG_W1
, i
, off
, mask
);
1180 /* lgfi %w1,imm (load sign extend imm) */
1181 EMIT6_IMM(0xc0010000, REG_W1
, imm
);
1182 /* clgrj %dst,%w1,mask,off */
1183 EMIT6_PCREL(0xec000000, 0x0065, dst_reg
, REG_W1
, i
, off
, mask
);
1186 /* cgrj %dst,%src,mask,off */
1187 EMIT6_PCREL(0xec000000, 0x0064, dst_reg
, src_reg
, i
, off
, mask
);
1190 /* clgrj %dst,%src,mask,off */
1191 EMIT6_PCREL(0xec000000, 0x0065, dst_reg
, src_reg
, i
, off
, mask
);
1194 /* brc mask,jmp_off (branch instruction needs 4 bytes) */
1195 jmp_off
= addrs
[i
+ off
+ 1] - (addrs
[i
+ 1] - 4);
1196 EMIT4_PCREL(0xa7040000 | mask
<< 8, jmp_off
);
1198 default: /* too complex, give up */
1199 pr_err("Unknown opcode %02x\n", insn
->code
);
1206 * Compile eBPF program into s390x code
1208 static int bpf_jit_prog(struct bpf_jit
*jit
, struct bpf_prog
*fp
)
1212 jit
->lit
= jit
->lit_start
;
1215 bpf_jit_prologue(jit
, fp
->aux
->stack_depth
);
1216 for (i
= 0; i
< fp
->len
; i
+= insn_count
) {
1217 insn_count
= bpf_jit_insn(jit
, fp
, i
);
1220 /* Next instruction address */
1221 jit
->addrs
[i
+ insn_count
] = jit
->prg
;
1223 bpf_jit_epilogue(jit
, fp
->aux
->stack_depth
);
1225 jit
->lit_start
= jit
->prg
;
1226 jit
->size
= jit
->lit
;
1227 jit
->size_prg
= jit
->prg
;
1232 * Compile eBPF program "fp"
1234 struct bpf_prog
*bpf_int_jit_compile(struct bpf_prog
*fp
)
1236 struct bpf_prog
*tmp
, *orig_fp
= fp
;
1237 struct bpf_binary_header
*header
;
1238 bool tmp_blinded
= false;
1242 if (!fp
->jit_requested
)
1245 tmp
= bpf_jit_blind_constants(fp
);
1247 * If blinding was requested and we failed during blinding,
1248 * we must fall back to the interpreter.
1257 memset(&jit
, 0, sizeof(jit
));
1258 jit
.addrs
= kcalloc(fp
->len
+ 1, sizeof(*jit
.addrs
), GFP_KERNEL
);
1259 if (jit
.addrs
== NULL
) {
1264 * Three initial passes:
1265 * - 1/2: Determine clobbered registers
1266 * - 3: Calculate program size and addrs arrray
1268 for (pass
= 1; pass
<= 3; pass
++) {
1269 if (bpf_jit_prog(&jit
, fp
)) {
1275 * Final pass: Allocate and generate program
1277 if (jit
.size
>= BPF_SIZE_MAX
) {
1281 header
= bpf_jit_binary_alloc(jit
.size
, &jit
.prg_buf
, 2, jit_fill_hole
);
1286 if (bpf_jit_prog(&jit
, fp
)) {
1287 bpf_jit_binary_free(header
);
1291 if (bpf_jit_enable
> 1) {
1292 bpf_jit_dump(fp
->len
, jit
.size
, pass
, jit
.prg_buf
);
1293 print_fn_code(jit
.prg_buf
, jit
.size_prg
);
1295 bpf_jit_binary_lock_ro(header
);
1296 fp
->bpf_func
= (void *) jit
.prg_buf
;
1298 fp
->jited_len
= jit
.size
;
1303 bpf_jit_prog_release_other(fp
, fp
== orig_fp
?