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>
27 #include <linux/kernel.h>
28 #include <asm/cacheflush.h>
30 #include <asm/facility.h>
31 #include <asm/nospec-branch.h>
32 #include <asm/set_memory.h>
36 u32 seen
; /* Flags to remember seen eBPF instructions */
37 u32 seen_reg
[16]; /* Array to remember which registers are used */
38 u32
*addrs
; /* Array with relative instruction addresses */
39 u8
*prg_buf
; /* Start of program */
40 int size
; /* Size of program and literal pool */
41 int size_prg
; /* Size of program */
42 int prg
; /* Current position in program */
43 int lit32_start
; /* Start of 32-bit literal pool */
44 int lit32
; /* Current position in 32-bit literal pool */
45 int lit64_start
; /* Start of 64-bit literal pool */
46 int lit64
; /* Current position in 64-bit literal pool */
47 int base_ip
; /* Base address for literal pool */
48 int exit_ip
; /* Address of exit */
49 int r1_thunk_ip
; /* Address of expoline thunk for 'br %r1' */
50 int r14_thunk_ip
; /* Address of expoline thunk for 'br %r14' */
51 int tail_call_start
; /* Tail call start offset */
52 int labels
[1]; /* Labels for local jumps */
55 #define SEEN_MEM BIT(0) /* use mem[] for temporary storage */
56 #define SEEN_LITERAL BIT(1) /* code uses literals */
57 #define SEEN_FUNC BIT(2) /* calls C functions */
58 #define SEEN_TAIL_CALL BIT(3) /* code uses tail calls */
59 #define SEEN_STACK (SEEN_FUNC | SEEN_MEM)
64 #define REG_W0 (MAX_BPF_JIT_REG + 0) /* Work register 1 (even) */
65 #define REG_W1 (MAX_BPF_JIT_REG + 1) /* Work register 2 (odd) */
66 #define REG_L (MAX_BPF_JIT_REG + 2) /* Literal pool register */
67 #define REG_15 (MAX_BPF_JIT_REG + 3) /* Register 15 */
68 #define REG_0 REG_W0 /* Register 0 */
69 #define REG_1 REG_W1 /* Register 1 */
70 #define REG_2 BPF_REG_1 /* Register 2 */
71 #define REG_14 BPF_REG_0 /* Register 14 */
74 * Mapping of BPF registers to s390 registers
76 static const int reg2hex
[] = {
79 /* Function parameters */
85 /* Call saved registers */
90 /* BPF stack pointer */
92 /* Register for blinding */
94 /* Work registers for s390x backend */
101 static inline u32
reg(u32 dst_reg
, u32 src_reg
)
103 return reg2hex
[dst_reg
] << 4 | reg2hex
[src_reg
];
106 static inline u32
reg_high(u32 reg
)
108 return reg2hex
[reg
] << 4;
111 static inline void reg_set_seen(struct bpf_jit
*jit
, u32 b1
)
113 u32 r1
= reg2hex
[b1
];
115 if (!jit
->seen_reg
[r1
] && r1
>= 6 && r1
<= 15)
116 jit
->seen_reg
[r1
] = 1;
119 #define REG_SET_SEEN(b1) \
121 reg_set_seen(jit, b1); \
124 #define REG_SEEN(b1) jit->seen_reg[reg2hex[(b1)]]
127 * EMIT macros for code generation
133 *(u16 *) (jit->prg_buf + jit->prg) = (op); \
137 #define EMIT2(op, b1, b2) \
139 _EMIT2((op) | reg(b1, b2)); \
147 *(u32 *) (jit->prg_buf + jit->prg) = (op); \
151 #define EMIT4(op, b1, b2) \
153 _EMIT4((op) | reg(b1, b2)); \
158 #define EMIT4_RRF(op, b1, b2, b3) \
160 _EMIT4((op) | reg_high(b3) << 8 | reg(b1, b2)); \
166 #define _EMIT4_DISP(op, disp) \
168 unsigned int __disp = (disp) & 0xfff; \
169 _EMIT4((op) | __disp); \
172 #define EMIT4_DISP(op, b1, b2, disp) \
174 _EMIT4_DISP((op) | reg_high(b1) << 16 | \
175 reg_high(b2) << 8, (disp)); \
180 #define EMIT4_IMM(op, b1, imm) \
182 unsigned int __imm = (imm) & 0xffff; \
183 _EMIT4((op) | reg_high(b1) << 16 | __imm); \
187 #define EMIT4_PCREL(op, pcrel) \
189 long __pcrel = ((pcrel) >> 1) & 0xffff; \
190 _EMIT4((op) | __pcrel); \
193 #define EMIT4_PCREL_RIC(op, mask, target) \
195 int __rel = ((target) - jit->prg) / 2; \
196 _EMIT4((op) | (mask) << 20 | (__rel & 0xffff)); \
199 #define _EMIT6(op1, op2) \
201 if (jit->prg_buf) { \
202 *(u32 *) (jit->prg_buf + jit->prg) = (op1); \
203 *(u16 *) (jit->prg_buf + jit->prg + 4) = (op2); \
208 #define _EMIT6_DISP(op1, op2, disp) \
210 unsigned int __disp = (disp) & 0xfff; \
211 _EMIT6((op1) | __disp, op2); \
214 #define _EMIT6_DISP_LH(op1, op2, disp) \
216 u32 _disp = (u32) (disp); \
217 unsigned int __disp_h = _disp & 0xff000; \
218 unsigned int __disp_l = _disp & 0x00fff; \
219 _EMIT6((op1) | __disp_l, (op2) | __disp_h >> 4); \
222 #define EMIT6_DISP_LH(op1, op2, b1, b2, b3, disp) \
224 _EMIT6_DISP_LH((op1) | reg(b1, b2) << 16 | \
225 reg_high(b3) << 8, op2, disp); \
231 #define EMIT6_PCREL_LABEL(op1, op2, b1, b2, label, mask) \
233 int rel = (jit->labels[label] - jit->prg) >> 1; \
234 _EMIT6((op1) | reg(b1, b2) << 16 | (rel & 0xffff), \
235 (op2) | (mask) << 12); \
240 #define EMIT6_PCREL_IMM_LABEL(op1, op2, b1, imm, label, mask) \
242 int rel = (jit->labels[label] - jit->prg) >> 1; \
243 _EMIT6((op1) | (reg_high(b1) | (mask)) << 16 | \
244 (rel & 0xffff), (op2) | ((imm) & 0xff) << 8); \
246 BUILD_BUG_ON(((unsigned long) (imm)) > 0xff); \
249 #define EMIT6_PCREL(op1, op2, b1, b2, i, off, mask) \
251 /* Branch instruction needs 6 bytes */ \
252 int rel = (addrs[(i) + (off) + 1] - (addrs[(i) + 1] - 6)) / 2;\
253 _EMIT6((op1) | reg(b1, b2) << 16 | (rel & 0xffff), (op2) | (mask));\
258 #define EMIT6_PCREL_RILB(op, b, target) \
260 unsigned int rel = (int)((target) - jit->prg) / 2; \
261 _EMIT6((op) | reg_high(b) << 16 | rel >> 16, rel & 0xffff);\
265 #define EMIT6_PCREL_RIL(op, target) \
267 unsigned int rel = (int)((target) - jit->prg) / 2; \
268 _EMIT6((op) | rel >> 16, rel & 0xffff); \
271 #define EMIT6_PCREL_RILC(op, mask, target) \
273 EMIT6_PCREL_RIL((op) | (mask) << 20, (target)); \
276 #define _EMIT6_IMM(op, imm) \
278 unsigned int __imm = (imm); \
279 _EMIT6((op) | (__imm >> 16), __imm & 0xffff); \
282 #define EMIT6_IMM(op, b1, imm) \
284 _EMIT6_IMM((op) | reg_high(b1) << 16, imm); \
288 #define _EMIT_CONST_U32(val) \
293 *(u32 *)(jit->prg_buf + jit->lit32) = (u32)(val);\
298 #define EMIT_CONST_U32(val) \
300 jit->seen |= SEEN_LITERAL; \
301 _EMIT_CONST_U32(val) - jit->base_ip; \
304 #define _EMIT_CONST_U64(val) \
309 *(u64 *)(jit->prg_buf + jit->lit64) = (u64)(val);\
314 #define EMIT_CONST_U64(val) \
316 jit->seen |= SEEN_LITERAL; \
317 _EMIT_CONST_U64(val) - jit->base_ip; \
320 #define EMIT_ZERO(b1) \
322 if (!fp->aux->verifier_zext) { \
323 /* llgfr %dst,%dst (zero extend to 64 bit) */ \
324 EMIT4(0xb9160000, b1, b1); \
330 * Return whether this is the first pass. The first pass is special, since we
331 * don't know any sizes yet, and thus must be conservative.
333 static bool is_first_pass(struct bpf_jit
*jit
)
335 return jit
->size
== 0;
339 * Return whether this is the code generation pass. The code generation pass is
340 * special, since we should change as little as possible.
342 static bool is_codegen_pass(struct bpf_jit
*jit
)
348 * Return whether "rel" can be encoded as a short PC-relative offset
350 static bool is_valid_rel(int rel
)
352 return rel
>= -65536 && rel
<= 65534;
356 * Return whether "off" can be reached using a short PC-relative offset
358 static bool can_use_rel(struct bpf_jit
*jit
, int off
)
360 return is_valid_rel(off
- jit
->prg
);
364 * Return whether given displacement can be encoded using
365 * Long-Displacement Facility
367 static bool is_valid_ldisp(int disp
)
369 return disp
>= -524288 && disp
<= 524287;
373 * Return whether the next 32-bit literal pool entry can be referenced using
374 * Long-Displacement Facility
376 static bool can_use_ldisp_for_lit32(struct bpf_jit
*jit
)
378 return is_valid_ldisp(jit
->lit32
- jit
->base_ip
);
382 * Return whether the next 64-bit literal pool entry can be referenced using
383 * Long-Displacement Facility
385 static bool can_use_ldisp_for_lit64(struct bpf_jit
*jit
)
387 return is_valid_ldisp(jit
->lit64
- jit
->base_ip
);
391 * Fill whole space with illegal instructions
393 static void jit_fill_hole(void *area
, unsigned int size
)
395 memset(area
, 0, size
);
399 * Save registers from "rs" (register start) to "re" (register end) on stack
401 static void save_regs(struct bpf_jit
*jit
, u32 rs
, u32 re
)
403 u32 off
= STK_OFF_R6
+ (rs
- 6) * 8;
406 /* stg %rs,off(%r15) */
407 _EMIT6(0xe300f000 | rs
<< 20 | off
, 0x0024);
409 /* stmg %rs,%re,off(%r15) */
410 _EMIT6_DISP(0xeb00f000 | rs
<< 20 | re
<< 16, 0x0024, off
);
414 * Restore registers from "rs" (register start) to "re" (register end) on stack
416 static void restore_regs(struct bpf_jit
*jit
, u32 rs
, u32 re
, u32 stack_depth
)
418 u32 off
= STK_OFF_R6
+ (rs
- 6) * 8;
420 if (jit
->seen
& SEEN_STACK
)
421 off
+= STK_OFF
+ stack_depth
;
424 /* lg %rs,off(%r15) */
425 _EMIT6(0xe300f000 | rs
<< 20 | off
, 0x0004);
427 /* lmg %rs,%re,off(%r15) */
428 _EMIT6_DISP(0xeb00f000 | rs
<< 20 | re
<< 16, 0x0004, off
);
432 * Return first seen register (from start)
434 static int get_start(struct bpf_jit
*jit
, int start
)
438 for (i
= start
; i
<= 15; i
++) {
439 if (jit
->seen_reg
[i
])
446 * Return last seen register (from start) (gap >= 2)
448 static int get_end(struct bpf_jit
*jit
, int start
)
452 for (i
= start
; i
< 15; i
++) {
453 if (!jit
->seen_reg
[i
] && !jit
->seen_reg
[i
+ 1])
456 return jit
->seen_reg
[15] ? 15 : 14;
460 #define REGS_RESTORE 0
462 * Save and restore clobbered registers (6-15) on stack.
463 * We save/restore registers in chunks with gap >= 2 registers.
465 static void save_restore_regs(struct bpf_jit
*jit
, int op
, u32 stack_depth
)
467 const int last
= 15, save_restore_size
= 6;
470 if (is_first_pass(jit
)) {
472 * We don't know yet which registers are used. Reserve space
475 jit
->prg
+= (last
- re
+ 1) * save_restore_size
;
480 rs
= get_start(jit
, re
);
483 re
= get_end(jit
, rs
+ 1);
485 save_regs(jit
, rs
, re
);
487 restore_regs(jit
, rs
, re
, stack_depth
);
489 } while (re
<= last
);
493 * Emit function prologue
495 * Save registers and create stack frame if necessary.
496 * See stack frame layout desription in "bpf_jit.h"!
498 static void bpf_jit_prologue(struct bpf_jit
*jit
, u32 stack_depth
)
500 if (jit
->seen
& SEEN_TAIL_CALL
) {
501 /* xc STK_OFF_TCCNT(4,%r15),STK_OFF_TCCNT(%r15) */
502 _EMIT6(0xd703f000 | STK_OFF_TCCNT
, 0xf000 | STK_OFF_TCCNT
);
504 /* j tail_call_start: NOP if no tail calls are used */
505 EMIT4_PCREL(0xa7f40000, 6);
508 /* Tail calls have to skip above initialization */
509 jit
->tail_call_start
= jit
->prg
;
511 save_restore_regs(jit
, REGS_SAVE
, stack_depth
);
512 /* Setup literal pool */
513 if (is_first_pass(jit
) || (jit
->seen
& SEEN_LITERAL
)) {
514 if (!is_first_pass(jit
) &&
515 is_valid_ldisp(jit
->size
- (jit
->prg
+ 2))) {
517 EMIT2(0x0d00, REG_L
, REG_0
);
518 jit
->base_ip
= jit
->prg
;
520 /* larl %l,lit32_start */
521 EMIT6_PCREL_RILB(0xc0000000, REG_L
, jit
->lit32_start
);
522 jit
->base_ip
= jit
->lit32_start
;
525 /* Setup stack and backchain */
526 if (is_first_pass(jit
) || (jit
->seen
& SEEN_STACK
)) {
527 if (is_first_pass(jit
) || (jit
->seen
& SEEN_FUNC
))
528 /* lgr %w1,%r15 (backchain) */
529 EMIT4(0xb9040000, REG_W1
, REG_15
);
530 /* la %bfp,STK_160_UNUSED(%r15) (BPF frame pointer) */
531 EMIT4_DISP(0x41000000, BPF_REG_FP
, REG_15
, STK_160_UNUSED
);
532 /* aghi %r15,-STK_OFF */
533 EMIT4_IMM(0xa70b0000, REG_15
, -(STK_OFF
+ stack_depth
));
534 if (is_first_pass(jit
) || (jit
->seen
& SEEN_FUNC
))
535 /* stg %w1,152(%r15) (backchain) */
536 EMIT6_DISP_LH(0xe3000000, 0x0024, REG_W1
, REG_0
,
544 static void bpf_jit_epilogue(struct bpf_jit
*jit
, u32 stack_depth
)
546 jit
->exit_ip
= jit
->prg
;
547 /* Load exit code: lgr %r2,%b0 */
548 EMIT4(0xb9040000, REG_2
, BPF_REG_0
);
549 /* Restore registers */
550 save_restore_regs(jit
, REGS_RESTORE
, stack_depth
);
551 if (__is_defined(CC_USING_EXPOLINE
) && !nospec_disable
) {
552 jit
->r14_thunk_ip
= jit
->prg
;
553 /* Generate __s390_indirect_jump_r14 thunk */
554 if (test_facility(35)) {
556 EMIT6_PCREL_RIL(0xc6000000, jit
->prg
+ 10);
559 EMIT6_PCREL_RILB(0xc0000000, REG_1
, jit
->prg
+ 14);
561 EMIT4_DISP(0x44000000, REG_0
, REG_1
, 0);
564 EMIT4_PCREL(0xa7f40000, 0);
569 if (__is_defined(CC_USING_EXPOLINE
) && !nospec_disable
&&
570 (is_first_pass(jit
) || (jit
->seen
& SEEN_FUNC
))) {
571 jit
->r1_thunk_ip
= jit
->prg
;
572 /* Generate __s390_indirect_jump_r1 thunk */
573 if (test_facility(35)) {
575 EMIT6_PCREL_RIL(0xc6000000, jit
->prg
+ 10);
577 EMIT4_PCREL(0xa7f40000, 0);
581 /* ex 0,S390_lowcore.br_r1_tampoline */
582 EMIT4_DISP(0x44000000, REG_0
, REG_0
,
583 offsetof(struct lowcore
, br_r1_trampoline
));
585 EMIT4_PCREL(0xa7f40000, 0);
591 * Compile one eBPF instruction into s390x code
593 * NOTE: Use noinline because for gcov (-fprofile-arcs) gcc allocates a lot of
594 * stack space for the large switch statement.
596 static noinline
int bpf_jit_insn(struct bpf_jit
*jit
, struct bpf_prog
*fp
,
597 int i
, bool extra_pass
, u32 stack_depth
)
599 struct bpf_insn
*insn
= &fp
->insnsi
[i
];
600 u32 dst_reg
= insn
->dst_reg
;
601 u32 src_reg
= insn
->src_reg
;
602 int last
, insn_count
= 1;
603 u32
*addrs
= jit
->addrs
;
608 switch (insn
->code
) {
612 case BPF_ALU
| BPF_MOV
| BPF_X
: /* dst = (u32) src */
613 /* llgfr %dst,%src */
614 EMIT4(0xb9160000, dst_reg
, src_reg
);
615 if (insn_is_zext(&insn
[1]))
618 case BPF_ALU64
| BPF_MOV
| BPF_X
: /* dst = src */
620 EMIT4(0xb9040000, dst_reg
, src_reg
);
622 case BPF_ALU
| BPF_MOV
| BPF_K
: /* dst = (u32) imm */
624 EMIT6_IMM(0xc00f0000, dst_reg
, imm
);
625 if (insn_is_zext(&insn
[1]))
628 case BPF_ALU64
| BPF_MOV
| BPF_K
: /* dst = imm */
630 EMIT6_IMM(0xc0010000, dst_reg
, imm
);
635 case BPF_LD
| BPF_IMM
| BPF_DW
: /* dst = (u64) imm */
637 /* 16 byte instruction that uses two 'struct bpf_insn' */
640 imm64
= (u64
)(u32
) insn
[0].imm
| ((u64
)(u32
) insn
[1].imm
) << 32;
642 EMIT6_PCREL_RILB(0xc4080000, dst_reg
, _EMIT_CONST_U64(imm64
));
649 case BPF_ALU
| BPF_ADD
| BPF_X
: /* dst = (u32) dst + (u32) src */
651 EMIT2(0x1a00, dst_reg
, src_reg
);
654 case BPF_ALU64
| BPF_ADD
| BPF_X
: /* dst = dst + src */
656 EMIT4(0xb9080000, dst_reg
, src_reg
);
658 case BPF_ALU
| BPF_ADD
| BPF_K
: /* dst = (u32) dst + (u32) imm */
662 EMIT6_IMM(0xc20b0000, dst_reg
, imm
);
665 case BPF_ALU64
| BPF_ADD
| BPF_K
: /* dst = dst + imm */
669 EMIT6_IMM(0xc2080000, dst_reg
, imm
);
674 case BPF_ALU
| BPF_SUB
| BPF_X
: /* dst = (u32) dst - (u32) src */
676 EMIT2(0x1b00, dst_reg
, src_reg
);
679 case BPF_ALU64
| BPF_SUB
| BPF_X
: /* dst = dst - src */
681 EMIT4(0xb9090000, dst_reg
, src_reg
);
683 case BPF_ALU
| BPF_SUB
| BPF_K
: /* dst = (u32) dst - (u32) imm */
687 EMIT6_IMM(0xc20b0000, dst_reg
, -imm
);
690 case BPF_ALU64
| BPF_SUB
| BPF_K
: /* dst = dst - imm */
694 EMIT6_IMM(0xc2080000, dst_reg
, -imm
);
699 case BPF_ALU
| BPF_MUL
| BPF_X
: /* dst = (u32) dst * (u32) src */
701 EMIT4(0xb2520000, dst_reg
, src_reg
);
704 case BPF_ALU64
| BPF_MUL
| BPF_X
: /* dst = dst * src */
706 EMIT4(0xb90c0000, dst_reg
, src_reg
);
708 case BPF_ALU
| BPF_MUL
| BPF_K
: /* dst = (u32) dst * (u32) imm */
712 EMIT6_IMM(0xc2010000, dst_reg
, imm
);
715 case BPF_ALU64
| BPF_MUL
| BPF_K
: /* dst = dst * imm */
719 EMIT6_IMM(0xc2000000, dst_reg
, imm
);
724 case BPF_ALU
| BPF_DIV
| BPF_X
: /* dst = (u32) dst / (u32) src */
725 case BPF_ALU
| BPF_MOD
| BPF_X
: /* dst = (u32) dst % (u32) src */
727 int rc_reg
= BPF_OP(insn
->code
) == BPF_DIV
? REG_W1
: REG_W0
;
730 EMIT4_IMM(0xa7080000, REG_W0
, 0);
732 EMIT2(0x1800, REG_W1
, dst_reg
);
734 EMIT4(0xb9970000, REG_W0
, src_reg
);
736 EMIT4(0xb9160000, dst_reg
, rc_reg
);
737 if (insn_is_zext(&insn
[1]))
741 case BPF_ALU64
| BPF_DIV
| BPF_X
: /* dst = dst / src */
742 case BPF_ALU64
| BPF_MOD
| BPF_X
: /* dst = dst % src */
744 int rc_reg
= BPF_OP(insn
->code
) == BPF_DIV
? REG_W1
: REG_W0
;
747 EMIT4_IMM(0xa7090000, REG_W0
, 0);
749 EMIT4(0xb9040000, REG_W1
, dst_reg
);
751 EMIT4(0xb9870000, REG_W0
, src_reg
);
753 EMIT4(0xb9040000, dst_reg
, rc_reg
);
756 case BPF_ALU
| BPF_DIV
| BPF_K
: /* dst = (u32) dst / (u32) imm */
757 case BPF_ALU
| BPF_MOD
| BPF_K
: /* dst = (u32) dst % (u32) imm */
759 int rc_reg
= BPF_OP(insn
->code
) == BPF_DIV
? REG_W1
: REG_W0
;
762 if (BPF_OP(insn
->code
) == BPF_MOD
)
764 EMIT4_IMM(0xa7090000, dst_reg
, 0);
768 EMIT4_IMM(0xa7080000, REG_W0
, 0);
770 EMIT2(0x1800, REG_W1
, dst_reg
);
771 if (!is_first_pass(jit
) && can_use_ldisp_for_lit32(jit
)) {
772 /* dl %w0,<d(imm)>(%l) */
773 EMIT6_DISP_LH(0xe3000000, 0x0097, REG_W0
, REG_0
, REG_L
,
774 EMIT_CONST_U32(imm
));
777 EMIT6_PCREL_RILB(0xc40c0000, dst_reg
,
778 _EMIT_CONST_U32(imm
));
779 jit
->seen
|= SEEN_LITERAL
;
781 EMIT4(0xb9970000, REG_W0
, dst_reg
);
784 EMIT4(0xb9160000, dst_reg
, rc_reg
);
785 if (insn_is_zext(&insn
[1]))
789 case BPF_ALU64
| BPF_DIV
| BPF_K
: /* dst = dst / imm */
790 case BPF_ALU64
| BPF_MOD
| BPF_K
: /* dst = dst % imm */
792 int rc_reg
= BPF_OP(insn
->code
) == BPF_DIV
? REG_W1
: REG_W0
;
795 if (BPF_OP(insn
->code
) == BPF_MOD
)
797 EMIT4_IMM(0xa7090000, dst_reg
, 0);
801 EMIT4_IMM(0xa7090000, REG_W0
, 0);
803 EMIT4(0xb9040000, REG_W1
, dst_reg
);
804 if (!is_first_pass(jit
) && can_use_ldisp_for_lit64(jit
)) {
805 /* dlg %w0,<d(imm)>(%l) */
806 EMIT6_DISP_LH(0xe3000000, 0x0087, REG_W0
, REG_0
, REG_L
,
807 EMIT_CONST_U64(imm
));
810 EMIT6_PCREL_RILB(0xc4080000, dst_reg
,
811 _EMIT_CONST_U64(imm
));
812 jit
->seen
|= SEEN_LITERAL
;
814 EMIT4(0xb9870000, REG_W0
, dst_reg
);
817 EMIT4(0xb9040000, dst_reg
, rc_reg
);
823 case BPF_ALU
| BPF_AND
| BPF_X
: /* dst = (u32) dst & (u32) src */
825 EMIT2(0x1400, dst_reg
, src_reg
);
828 case BPF_ALU64
| BPF_AND
| BPF_X
: /* dst = dst & src */
830 EMIT4(0xb9800000, dst_reg
, src_reg
);
832 case BPF_ALU
| BPF_AND
| BPF_K
: /* dst = (u32) dst & (u32) imm */
834 EMIT6_IMM(0xc00b0000, dst_reg
, imm
);
837 case BPF_ALU64
| BPF_AND
| BPF_K
: /* dst = dst & imm */
838 if (!is_first_pass(jit
) && can_use_ldisp_for_lit64(jit
)) {
839 /* ng %dst,<d(imm)>(%l) */
840 EMIT6_DISP_LH(0xe3000000, 0x0080,
841 dst_reg
, REG_0
, REG_L
,
842 EMIT_CONST_U64(imm
));
845 EMIT6_PCREL_RILB(0xc4080000, REG_W0
,
846 _EMIT_CONST_U64(imm
));
847 jit
->seen
|= SEEN_LITERAL
;
849 EMIT4(0xb9800000, dst_reg
, REG_W0
);
855 case BPF_ALU
| BPF_OR
| BPF_X
: /* dst = (u32) dst | (u32) src */
857 EMIT2(0x1600, dst_reg
, src_reg
);
860 case BPF_ALU64
| BPF_OR
| BPF_X
: /* dst = dst | src */
862 EMIT4(0xb9810000, dst_reg
, src_reg
);
864 case BPF_ALU
| BPF_OR
| BPF_K
: /* dst = (u32) dst | (u32) imm */
866 EMIT6_IMM(0xc00d0000, dst_reg
, imm
);
869 case BPF_ALU64
| BPF_OR
| BPF_K
: /* dst = dst | imm */
870 if (!is_first_pass(jit
) && can_use_ldisp_for_lit64(jit
)) {
871 /* og %dst,<d(imm)>(%l) */
872 EMIT6_DISP_LH(0xe3000000, 0x0081,
873 dst_reg
, REG_0
, REG_L
,
874 EMIT_CONST_U64(imm
));
877 EMIT6_PCREL_RILB(0xc4080000, REG_W0
,
878 _EMIT_CONST_U64(imm
));
879 jit
->seen
|= SEEN_LITERAL
;
881 EMIT4(0xb9810000, dst_reg
, REG_W0
);
887 case BPF_ALU
| BPF_XOR
| BPF_X
: /* dst = (u32) dst ^ (u32) src */
889 EMIT2(0x1700, dst_reg
, src_reg
);
892 case BPF_ALU64
| BPF_XOR
| BPF_X
: /* dst = dst ^ src */
894 EMIT4(0xb9820000, dst_reg
, src_reg
);
896 case BPF_ALU
| BPF_XOR
| BPF_K
: /* dst = (u32) dst ^ (u32) imm */
900 EMIT6_IMM(0xc0070000, dst_reg
, imm
);
903 case BPF_ALU64
| BPF_XOR
| BPF_K
: /* dst = dst ^ imm */
904 if (!is_first_pass(jit
) && can_use_ldisp_for_lit64(jit
)) {
905 /* xg %dst,<d(imm)>(%l) */
906 EMIT6_DISP_LH(0xe3000000, 0x0082,
907 dst_reg
, REG_0
, REG_L
,
908 EMIT_CONST_U64(imm
));
911 EMIT6_PCREL_RILB(0xc4080000, REG_W0
,
912 _EMIT_CONST_U64(imm
));
913 jit
->seen
|= SEEN_LITERAL
;
915 EMIT4(0xb9820000, dst_reg
, REG_W0
);
921 case BPF_ALU
| BPF_LSH
| BPF_X
: /* dst = (u32) dst << (u32) src */
922 /* sll %dst,0(%src) */
923 EMIT4_DISP(0x89000000, dst_reg
, src_reg
, 0);
926 case BPF_ALU64
| BPF_LSH
| BPF_X
: /* dst = dst << src */
927 /* sllg %dst,%dst,0(%src) */
928 EMIT6_DISP_LH(0xeb000000, 0x000d, dst_reg
, dst_reg
, src_reg
, 0);
930 case BPF_ALU
| BPF_LSH
| BPF_K
: /* dst = (u32) dst << (u32) imm */
933 /* sll %dst,imm(%r0) */
934 EMIT4_DISP(0x89000000, dst_reg
, REG_0
, imm
);
937 case BPF_ALU64
| BPF_LSH
| BPF_K
: /* dst = dst << imm */
940 /* sllg %dst,%dst,imm(%r0) */
941 EMIT6_DISP_LH(0xeb000000, 0x000d, dst_reg
, dst_reg
, REG_0
, imm
);
946 case BPF_ALU
| BPF_RSH
| BPF_X
: /* dst = (u32) dst >> (u32) src */
947 /* srl %dst,0(%src) */
948 EMIT4_DISP(0x88000000, dst_reg
, src_reg
, 0);
951 case BPF_ALU64
| BPF_RSH
| BPF_X
: /* dst = dst >> src */
952 /* srlg %dst,%dst,0(%src) */
953 EMIT6_DISP_LH(0xeb000000, 0x000c, dst_reg
, dst_reg
, src_reg
, 0);
955 case BPF_ALU
| BPF_RSH
| BPF_K
: /* dst = (u32) dst >> (u32) imm */
958 /* srl %dst,imm(%r0) */
959 EMIT4_DISP(0x88000000, dst_reg
, REG_0
, imm
);
962 case BPF_ALU64
| BPF_RSH
| BPF_K
: /* dst = dst >> imm */
965 /* srlg %dst,%dst,imm(%r0) */
966 EMIT6_DISP_LH(0xeb000000, 0x000c, dst_reg
, dst_reg
, REG_0
, imm
);
971 case BPF_ALU
| BPF_ARSH
| BPF_X
: /* ((s32) dst) >>= src */
972 /* sra %dst,%dst,0(%src) */
973 EMIT4_DISP(0x8a000000, dst_reg
, src_reg
, 0);
976 case BPF_ALU64
| BPF_ARSH
| BPF_X
: /* ((s64) dst) >>= src */
977 /* srag %dst,%dst,0(%src) */
978 EMIT6_DISP_LH(0xeb000000, 0x000a, dst_reg
, dst_reg
, src_reg
, 0);
980 case BPF_ALU
| BPF_ARSH
| BPF_K
: /* ((s32) dst >> imm */
983 /* sra %dst,imm(%r0) */
984 EMIT4_DISP(0x8a000000, dst_reg
, REG_0
, imm
);
987 case BPF_ALU64
| BPF_ARSH
| BPF_K
: /* ((s64) dst) >>= imm */
990 /* srag %dst,%dst,imm(%r0) */
991 EMIT6_DISP_LH(0xeb000000, 0x000a, dst_reg
, dst_reg
, REG_0
, imm
);
996 case BPF_ALU
| BPF_NEG
: /* dst = (u32) -dst */
998 EMIT2(0x1300, dst_reg
, dst_reg
);
1001 case BPF_ALU64
| BPF_NEG
: /* dst = -dst */
1002 /* lcgr %dst,%dst */
1003 EMIT4(0xb9030000, dst_reg
, dst_reg
);
1008 case BPF_ALU
| BPF_END
| BPF_FROM_BE
:
1009 /* s390 is big endian, therefore only clear high order bytes */
1011 case 16: /* dst = (u16) cpu_to_be16(dst) */
1012 /* llghr %dst,%dst */
1013 EMIT4(0xb9850000, dst_reg
, dst_reg
);
1014 if (insn_is_zext(&insn
[1]))
1017 case 32: /* dst = (u32) cpu_to_be32(dst) */
1018 if (!fp
->aux
->verifier_zext
)
1019 /* llgfr %dst,%dst */
1020 EMIT4(0xb9160000, dst_reg
, dst_reg
);
1022 case 64: /* dst = (u64) cpu_to_be64(dst) */
1026 case BPF_ALU
| BPF_END
| BPF_FROM_LE
:
1028 case 16: /* dst = (u16) cpu_to_le16(dst) */
1029 /* lrvr %dst,%dst */
1030 EMIT4(0xb91f0000, dst_reg
, dst_reg
);
1031 /* srl %dst,16(%r0) */
1032 EMIT4_DISP(0x88000000, dst_reg
, REG_0
, 16);
1033 /* llghr %dst,%dst */
1034 EMIT4(0xb9850000, dst_reg
, dst_reg
);
1035 if (insn_is_zext(&insn
[1]))
1038 case 32: /* dst = (u32) cpu_to_le32(dst) */
1039 /* lrvr %dst,%dst */
1040 EMIT4(0xb91f0000, dst_reg
, dst_reg
);
1041 if (!fp
->aux
->verifier_zext
)
1042 /* llgfr %dst,%dst */
1043 EMIT4(0xb9160000, dst_reg
, dst_reg
);
1045 case 64: /* dst = (u64) cpu_to_le64(dst) */
1046 /* lrvgr %dst,%dst */
1047 EMIT4(0xb90f0000, dst_reg
, dst_reg
);
1054 case BPF_STX
| BPF_MEM
| BPF_B
: /* *(u8 *)(dst + off) = src_reg */
1055 /* stcy %src,off(%dst) */
1056 EMIT6_DISP_LH(0xe3000000, 0x0072, src_reg
, dst_reg
, REG_0
, off
);
1057 jit
->seen
|= SEEN_MEM
;
1059 case BPF_STX
| BPF_MEM
| BPF_H
: /* (u16 *)(dst + off) = src */
1060 /* sthy %src,off(%dst) */
1061 EMIT6_DISP_LH(0xe3000000, 0x0070, src_reg
, dst_reg
, REG_0
, off
);
1062 jit
->seen
|= SEEN_MEM
;
1064 case BPF_STX
| BPF_MEM
| BPF_W
: /* *(u32 *)(dst + off) = src */
1065 /* sty %src,off(%dst) */
1066 EMIT6_DISP_LH(0xe3000000, 0x0050, src_reg
, dst_reg
, REG_0
, off
);
1067 jit
->seen
|= SEEN_MEM
;
1069 case BPF_STX
| BPF_MEM
| BPF_DW
: /* (u64 *)(dst + off) = src */
1070 /* stg %src,off(%dst) */
1071 EMIT6_DISP_LH(0xe3000000, 0x0024, src_reg
, dst_reg
, REG_0
, off
);
1072 jit
->seen
|= SEEN_MEM
;
1074 case BPF_ST
| BPF_MEM
| BPF_B
: /* *(u8 *)(dst + off) = imm */
1076 EMIT4_IMM(0xa7080000, REG_W0
, (u8
) imm
);
1077 /* stcy %w0,off(dst) */
1078 EMIT6_DISP_LH(0xe3000000, 0x0072, REG_W0
, dst_reg
, REG_0
, off
);
1079 jit
->seen
|= SEEN_MEM
;
1081 case BPF_ST
| BPF_MEM
| BPF_H
: /* (u16 *)(dst + off) = imm */
1083 EMIT4_IMM(0xa7080000, REG_W0
, (u16
) imm
);
1084 /* sthy %w0,off(dst) */
1085 EMIT6_DISP_LH(0xe3000000, 0x0070, REG_W0
, dst_reg
, REG_0
, off
);
1086 jit
->seen
|= SEEN_MEM
;
1088 case BPF_ST
| BPF_MEM
| BPF_W
: /* *(u32 *)(dst + off) = imm */
1090 EMIT6_IMM(0xc00f0000, REG_W0
, (u32
) imm
);
1091 /* sty %w0,off(%dst) */
1092 EMIT6_DISP_LH(0xe3000000, 0x0050, REG_W0
, dst_reg
, REG_0
, off
);
1093 jit
->seen
|= SEEN_MEM
;
1095 case BPF_ST
| BPF_MEM
| BPF_DW
: /* *(u64 *)(dst + off) = imm */
1097 EMIT6_IMM(0xc0010000, REG_W0
, imm
);
1098 /* stg %w0,off(%dst) */
1099 EMIT6_DISP_LH(0xe3000000, 0x0024, REG_W0
, dst_reg
, REG_0
, off
);
1100 jit
->seen
|= SEEN_MEM
;
1103 * BPF_STX XADD (atomic_add)
1105 case BPF_STX
| BPF_XADD
| BPF_W
: /* *(u32 *)(dst + off) += src */
1106 /* laal %w0,%src,off(%dst) */
1107 EMIT6_DISP_LH(0xeb000000, 0x00fa, REG_W0
, src_reg
,
1109 jit
->seen
|= SEEN_MEM
;
1111 case BPF_STX
| BPF_XADD
| BPF_DW
: /* *(u64 *)(dst + off) += src */
1112 /* laalg %w0,%src,off(%dst) */
1113 EMIT6_DISP_LH(0xeb000000, 0x00ea, REG_W0
, src_reg
,
1115 jit
->seen
|= SEEN_MEM
;
1120 case BPF_LDX
| BPF_MEM
| BPF_B
: /* dst = *(u8 *)(ul) (src + off) */
1121 /* llgc %dst,0(off,%src) */
1122 EMIT6_DISP_LH(0xe3000000, 0x0090, dst_reg
, src_reg
, REG_0
, off
);
1123 jit
->seen
|= SEEN_MEM
;
1124 if (insn_is_zext(&insn
[1]))
1127 case BPF_LDX
| BPF_MEM
| BPF_H
: /* dst = *(u16 *)(ul) (src + off) */
1128 /* llgh %dst,0(off,%src) */
1129 EMIT6_DISP_LH(0xe3000000, 0x0091, dst_reg
, src_reg
, REG_0
, off
);
1130 jit
->seen
|= SEEN_MEM
;
1131 if (insn_is_zext(&insn
[1]))
1134 case BPF_LDX
| BPF_MEM
| BPF_W
: /* dst = *(u32 *)(ul) (src + off) */
1135 /* llgf %dst,off(%src) */
1136 jit
->seen
|= SEEN_MEM
;
1137 EMIT6_DISP_LH(0xe3000000, 0x0016, dst_reg
, src_reg
, REG_0
, off
);
1138 if (insn_is_zext(&insn
[1]))
1141 case BPF_LDX
| BPF_MEM
| BPF_DW
: /* dst = *(u64 *)(ul) (src + off) */
1142 /* lg %dst,0(off,%src) */
1143 jit
->seen
|= SEEN_MEM
;
1144 EMIT6_DISP_LH(0xe3000000, 0x0004, dst_reg
, src_reg
, REG_0
, off
);
1149 case BPF_JMP
| BPF_CALL
:
1152 bool func_addr_fixed
;
1155 ret
= bpf_jit_get_func_addr(fp
, insn
, extra_pass
,
1156 &func
, &func_addr_fixed
);
1160 REG_SET_SEEN(BPF_REG_5
);
1161 jit
->seen
|= SEEN_FUNC
;
1163 EMIT6_PCREL_RILB(0xc4080000, REG_W1
, _EMIT_CONST_U64(func
));
1164 if (__is_defined(CC_USING_EXPOLINE
) && !nospec_disable
) {
1165 /* brasl %r14,__s390_indirect_jump_r1 */
1166 EMIT6_PCREL_RILB(0xc0050000, REG_14
, jit
->r1_thunk_ip
);
1169 EMIT2(0x0d00, REG_14
, REG_W1
);
1171 /* lgr %b0,%r2: load return value into %b0 */
1172 EMIT4(0xb9040000, BPF_REG_0
, REG_2
);
1175 case BPF_JMP
| BPF_TAIL_CALL
:
1178 * B1: pointer to ctx
1179 * B2: pointer to bpf_array
1180 * B3: index in bpf_array
1182 jit
->seen
|= SEEN_TAIL_CALL
;
1185 * if (index >= array->map.max_entries)
1189 /* llgf %w1,map.max_entries(%b2) */
1190 EMIT6_DISP_LH(0xe3000000, 0x0016, REG_W1
, REG_0
, BPF_REG_2
,
1191 offsetof(struct bpf_array
, map
.max_entries
));
1192 /* if ((u32)%b3 >= (u32)%w1) goto out; */
1193 if (!is_first_pass(jit
) && can_use_rel(jit
, jit
->labels
[0])) {
1194 /* clrj %b3,%w1,0xa,label0 */
1195 EMIT6_PCREL_LABEL(0xec000000, 0x0077, BPF_REG_3
,
1199 EMIT2(0x1500, BPF_REG_3
, REG_W1
);
1200 /* brcl 0xa,label0 */
1201 EMIT6_PCREL_RILC(0xc0040000, 0xa, jit
->labels
[0]);
1205 * if (tail_call_cnt++ > MAX_TAIL_CALL_CNT)
1209 if (jit
->seen
& SEEN_STACK
)
1210 off
= STK_OFF_TCCNT
+ STK_OFF
+ stack_depth
;
1212 off
= STK_OFF_TCCNT
;
1214 EMIT4_IMM(0xa7080000, REG_W0
, 1);
1215 /* laal %w1,%w0,off(%r15) */
1216 EMIT6_DISP_LH(0xeb000000, 0x00fa, REG_W1
, REG_W0
, REG_15
, off
);
1217 if (!is_first_pass(jit
) && can_use_rel(jit
, jit
->labels
[0])) {
1218 /* clij %w1,MAX_TAIL_CALL_CNT,0x2,label0 */
1219 EMIT6_PCREL_IMM_LABEL(0xec000000, 0x007f, REG_W1
,
1220 MAX_TAIL_CALL_CNT
, 0, 0x2);
1222 /* clfi %w1,MAX_TAIL_CALL_CNT */
1223 EMIT6_IMM(0xc20f0000, REG_W1
, MAX_TAIL_CALL_CNT
);
1224 /* brcl 0x2,label0 */
1225 EMIT6_PCREL_RILC(0xc0040000, 0x2, jit
->labels
[0]);
1229 * prog = array->ptrs[index];
1234 /* llgfr %r1,%b3: %r1 = (u32) index */
1235 EMIT4(0xb9160000, REG_1
, BPF_REG_3
);
1236 /* sllg %r1,%r1,3: %r1 *= 8 */
1237 EMIT6_DISP_LH(0xeb000000, 0x000d, REG_1
, REG_1
, REG_0
, 3);
1238 /* ltg %r1,prog(%b2,%r1) */
1239 EMIT6_DISP_LH(0xe3000000, 0x0002, REG_1
, BPF_REG_2
,
1240 REG_1
, offsetof(struct bpf_array
, ptrs
));
1241 if (!is_first_pass(jit
) && can_use_rel(jit
, jit
->labels
[0])) {
1242 /* brc 0x8,label0 */
1243 EMIT4_PCREL_RIC(0xa7040000, 0x8, jit
->labels
[0]);
1245 /* brcl 0x8,label0 */
1246 EMIT6_PCREL_RILC(0xc0040000, 0x8, jit
->labels
[0]);
1250 * Restore registers before calling function
1252 save_restore_regs(jit
, REGS_RESTORE
, stack_depth
);
1255 * goto *(prog->bpf_func + tail_call_start);
1258 /* lg %r1,bpf_func(%r1) */
1259 EMIT6_DISP_LH(0xe3000000, 0x0004, REG_1
, REG_1
, REG_0
,
1260 offsetof(struct bpf_prog
, bpf_func
));
1261 /* bc 0xf,tail_call_start(%r1) */
1262 _EMIT4(0x47f01000 + jit
->tail_call_start
);
1264 jit
->labels
[0] = jit
->prg
;
1266 case BPF_JMP
| BPF_EXIT
: /* return b0 */
1267 last
= (i
== fp
->len
- 1) ? 1 : 0;
1271 EMIT4_PCREL(0xa7f40000, jit
->exit_ip
- jit
->prg
);
1274 * Branch relative (number of skipped instructions) to offset on
1277 * Condition code to mask mapping:
1279 * CC | Description | Mask
1280 * ------------------------------
1281 * 0 | Operands equal | 8
1282 * 1 | First operand low | 4
1283 * 2 | First operand high | 2
1286 * For s390x relative branches: ip = ip + off_bytes
1287 * For BPF relative branches: insn = insn + off_insns + 1
1289 * For example for s390x with offset 0 we jump to the branch
1290 * instruction itself (loop) and for BPF with offset 0 we
1291 * branch to the instruction behind the branch.
1293 case BPF_JMP
| BPF_JA
: /* if (true) */
1294 mask
= 0xf000; /* j */
1296 case BPF_JMP
| BPF_JSGT
| BPF_K
: /* ((s64) dst > (s64) imm) */
1297 case BPF_JMP32
| BPF_JSGT
| BPF_K
: /* ((s32) dst > (s32) imm) */
1298 mask
= 0x2000; /* jh */
1300 case BPF_JMP
| BPF_JSLT
| BPF_K
: /* ((s64) dst < (s64) imm) */
1301 case BPF_JMP32
| BPF_JSLT
| BPF_K
: /* ((s32) dst < (s32) imm) */
1302 mask
= 0x4000; /* jl */
1304 case BPF_JMP
| BPF_JSGE
| BPF_K
: /* ((s64) dst >= (s64) imm) */
1305 case BPF_JMP32
| BPF_JSGE
| BPF_K
: /* ((s32) dst >= (s32) imm) */
1306 mask
= 0xa000; /* jhe */
1308 case BPF_JMP
| BPF_JSLE
| BPF_K
: /* ((s64) dst <= (s64) imm) */
1309 case BPF_JMP32
| BPF_JSLE
| BPF_K
: /* ((s32) dst <= (s32) imm) */
1310 mask
= 0xc000; /* jle */
1312 case BPF_JMP
| BPF_JGT
| BPF_K
: /* (dst_reg > imm) */
1313 case BPF_JMP32
| BPF_JGT
| BPF_K
: /* ((u32) dst_reg > (u32) imm) */
1314 mask
= 0x2000; /* jh */
1316 case BPF_JMP
| BPF_JLT
| BPF_K
: /* (dst_reg < imm) */
1317 case BPF_JMP32
| BPF_JLT
| BPF_K
: /* ((u32) dst_reg < (u32) imm) */
1318 mask
= 0x4000; /* jl */
1320 case BPF_JMP
| BPF_JGE
| BPF_K
: /* (dst_reg >= imm) */
1321 case BPF_JMP32
| BPF_JGE
| BPF_K
: /* ((u32) dst_reg >= (u32) imm) */
1322 mask
= 0xa000; /* jhe */
1324 case BPF_JMP
| BPF_JLE
| BPF_K
: /* (dst_reg <= imm) */
1325 case BPF_JMP32
| BPF_JLE
| BPF_K
: /* ((u32) dst_reg <= (u32) imm) */
1326 mask
= 0xc000; /* jle */
1328 case BPF_JMP
| BPF_JNE
| BPF_K
: /* (dst_reg != imm) */
1329 case BPF_JMP32
| BPF_JNE
| BPF_K
: /* ((u32) dst_reg != (u32) imm) */
1330 mask
= 0x7000; /* jne */
1332 case BPF_JMP
| BPF_JEQ
| BPF_K
: /* (dst_reg == imm) */
1333 case BPF_JMP32
| BPF_JEQ
| BPF_K
: /* ((u32) dst_reg == (u32) imm) */
1334 mask
= 0x8000; /* je */
1336 case BPF_JMP
| BPF_JSET
| BPF_K
: /* (dst_reg & imm) */
1337 case BPF_JMP32
| BPF_JSET
| BPF_K
: /* ((u32) dst_reg & (u32) imm) */
1338 mask
= 0x7000; /* jnz */
1339 if (BPF_CLASS(insn
->code
) == BPF_JMP32
) {
1340 /* llilf %w1,imm (load zero extend imm) */
1341 EMIT6_IMM(0xc00f0000, REG_W1
, imm
);
1343 EMIT2(0x1400, REG_W1
, dst_reg
);
1345 /* lgfi %w1,imm (load sign extend imm) */
1346 EMIT6_IMM(0xc0010000, REG_W1
, imm
);
1348 EMIT4(0xb9800000, REG_W1
, dst_reg
);
1352 case BPF_JMP
| BPF_JSGT
| BPF_X
: /* ((s64) dst > (s64) src) */
1353 case BPF_JMP32
| BPF_JSGT
| BPF_X
: /* ((s32) dst > (s32) src) */
1354 mask
= 0x2000; /* jh */
1356 case BPF_JMP
| BPF_JSLT
| BPF_X
: /* ((s64) dst < (s64) src) */
1357 case BPF_JMP32
| BPF_JSLT
| BPF_X
: /* ((s32) dst < (s32) src) */
1358 mask
= 0x4000; /* jl */
1360 case BPF_JMP
| BPF_JSGE
| BPF_X
: /* ((s64) dst >= (s64) src) */
1361 case BPF_JMP32
| BPF_JSGE
| BPF_X
: /* ((s32) dst >= (s32) src) */
1362 mask
= 0xa000; /* jhe */
1364 case BPF_JMP
| BPF_JSLE
| BPF_X
: /* ((s64) dst <= (s64) src) */
1365 case BPF_JMP32
| BPF_JSLE
| BPF_X
: /* ((s32) dst <= (s32) src) */
1366 mask
= 0xc000; /* jle */
1368 case BPF_JMP
| BPF_JGT
| BPF_X
: /* (dst > src) */
1369 case BPF_JMP32
| BPF_JGT
| BPF_X
: /* ((u32) dst > (u32) src) */
1370 mask
= 0x2000; /* jh */
1372 case BPF_JMP
| BPF_JLT
| BPF_X
: /* (dst < src) */
1373 case BPF_JMP32
| BPF_JLT
| BPF_X
: /* ((u32) dst < (u32) src) */
1374 mask
= 0x4000; /* jl */
1376 case BPF_JMP
| BPF_JGE
| BPF_X
: /* (dst >= src) */
1377 case BPF_JMP32
| BPF_JGE
| BPF_X
: /* ((u32) dst >= (u32) src) */
1378 mask
= 0xa000; /* jhe */
1380 case BPF_JMP
| BPF_JLE
| BPF_X
: /* (dst <= src) */
1381 case BPF_JMP32
| BPF_JLE
| BPF_X
: /* ((u32) dst <= (u32) src) */
1382 mask
= 0xc000; /* jle */
1384 case BPF_JMP
| BPF_JNE
| BPF_X
: /* (dst != src) */
1385 case BPF_JMP32
| BPF_JNE
| BPF_X
: /* ((u32) dst != (u32) src) */
1386 mask
= 0x7000; /* jne */
1388 case BPF_JMP
| BPF_JEQ
| BPF_X
: /* (dst == src) */
1389 case BPF_JMP32
| BPF_JEQ
| BPF_X
: /* ((u32) dst == (u32) src) */
1390 mask
= 0x8000; /* je */
1392 case BPF_JMP
| BPF_JSET
| BPF_X
: /* (dst & src) */
1393 case BPF_JMP32
| BPF_JSET
| BPF_X
: /* ((u32) dst & (u32) src) */
1395 bool is_jmp32
= BPF_CLASS(insn
->code
) == BPF_JMP32
;
1397 mask
= 0x7000; /* jnz */
1398 /* nrk or ngrk %w1,%dst,%src */
1399 EMIT4_RRF((is_jmp32
? 0xb9f40000 : 0xb9e40000),
1400 REG_W1
, dst_reg
, src_reg
);
1403 is_jmp32
= BPF_CLASS(insn
->code
) == BPF_JMP32
;
1404 /* cfi or cgfi %dst,imm */
1405 EMIT6_IMM(is_jmp32
? 0xc20d0000 : 0xc20c0000,
1407 if (!is_first_pass(jit
) &&
1408 can_use_rel(jit
, addrs
[i
+ off
+ 1])) {
1410 EMIT4_PCREL_RIC(0xa7040000,
1411 mask
>> 12, addrs
[i
+ off
+ 1]);
1414 EMIT6_PCREL_RILC(0xc0040000,
1415 mask
>> 12, addrs
[i
+ off
+ 1]);
1419 is_jmp32
= BPF_CLASS(insn
->code
) == BPF_JMP32
;
1420 /* clfi or clgfi %dst,imm */
1421 EMIT6_IMM(is_jmp32
? 0xc20f0000 : 0xc20e0000,
1423 if (!is_first_pass(jit
) &&
1424 can_use_rel(jit
, addrs
[i
+ off
+ 1])) {
1426 EMIT4_PCREL_RIC(0xa7040000,
1427 mask
>> 12, addrs
[i
+ off
+ 1]);
1430 EMIT6_PCREL_RILC(0xc0040000,
1431 mask
>> 12, addrs
[i
+ off
+ 1]);
1435 is_jmp32
= BPF_CLASS(insn
->code
) == BPF_JMP32
;
1436 if (!is_first_pass(jit
) &&
1437 can_use_rel(jit
, addrs
[i
+ off
+ 1])) {
1438 /* crj or cgrj %dst,%src,mask,off */
1439 EMIT6_PCREL(0xec000000, (is_jmp32
? 0x0076 : 0x0064),
1440 dst_reg
, src_reg
, i
, off
, mask
);
1442 /* cr or cgr %dst,%src */
1444 EMIT2(0x1900, dst_reg
, src_reg
);
1446 EMIT4(0xb9200000, dst_reg
, src_reg
);
1448 EMIT6_PCREL_RILC(0xc0040000,
1449 mask
>> 12, addrs
[i
+ off
+ 1]);
1453 is_jmp32
= BPF_CLASS(insn
->code
) == BPF_JMP32
;
1454 if (!is_first_pass(jit
) &&
1455 can_use_rel(jit
, addrs
[i
+ off
+ 1])) {
1456 /* clrj or clgrj %dst,%src,mask,off */
1457 EMIT6_PCREL(0xec000000, (is_jmp32
? 0x0077 : 0x0065),
1458 dst_reg
, src_reg
, i
, off
, mask
);
1460 /* clr or clgr %dst,%src */
1462 EMIT2(0x1500, dst_reg
, src_reg
);
1464 EMIT4(0xb9210000, dst_reg
, src_reg
);
1466 EMIT6_PCREL_RILC(0xc0040000,
1467 mask
>> 12, addrs
[i
+ off
+ 1]);
1471 if (!is_first_pass(jit
) &&
1472 can_use_rel(jit
, addrs
[i
+ off
+ 1])) {
1474 EMIT4_PCREL_RIC(0xa7040000,
1475 mask
>> 12, addrs
[i
+ off
+ 1]);
1478 EMIT6_PCREL_RILC(0xc0040000,
1479 mask
>> 12, addrs
[i
+ off
+ 1]);
1483 default: /* too complex, give up */
1484 pr_err("Unknown opcode %02x\n", insn
->code
);
1491 * Return whether new i-th instruction address does not violate any invariant
1493 static bool bpf_is_new_addr_sane(struct bpf_jit
*jit
, int i
)
1495 /* On the first pass anything goes */
1496 if (is_first_pass(jit
))
1499 /* The codegen pass must not change anything */
1500 if (is_codegen_pass(jit
))
1501 return jit
->addrs
[i
] == jit
->prg
;
1503 /* Passes in between must not increase code size */
1504 return jit
->addrs
[i
] >= jit
->prg
;
1508 * Update the address of i-th instruction
1510 static int bpf_set_addr(struct bpf_jit
*jit
, int i
)
1512 if (!bpf_is_new_addr_sane(jit
, i
))
1514 jit
->addrs
[i
] = jit
->prg
;
1519 * Compile eBPF program into s390x code
1521 static int bpf_jit_prog(struct bpf_jit
*jit
, struct bpf_prog
*fp
,
1522 bool extra_pass
, u32 stack_depth
)
1524 int i
, insn_count
, lit32_size
, lit64_size
;
1526 jit
->lit32
= jit
->lit32_start
;
1527 jit
->lit64
= jit
->lit64_start
;
1530 bpf_jit_prologue(jit
, stack_depth
);
1531 if (bpf_set_addr(jit
, 0) < 0)
1533 for (i
= 0; i
< fp
->len
; i
+= insn_count
) {
1534 insn_count
= bpf_jit_insn(jit
, fp
, i
, extra_pass
, stack_depth
);
1537 /* Next instruction address */
1538 if (bpf_set_addr(jit
, i
+ insn_count
) < 0)
1541 bpf_jit_epilogue(jit
, stack_depth
);
1543 lit32_size
= jit
->lit32
- jit
->lit32_start
;
1544 lit64_size
= jit
->lit64
- jit
->lit64_start
;
1545 jit
->lit32_start
= jit
->prg
;
1547 jit
->lit32_start
= ALIGN(jit
->lit32_start
, 4);
1548 jit
->lit64_start
= jit
->lit32_start
+ lit32_size
;
1550 jit
->lit64_start
= ALIGN(jit
->lit64_start
, 8);
1551 jit
->size
= jit
->lit64_start
+ lit64_size
;
1552 jit
->size_prg
= jit
->prg
;
1556 bool bpf_jit_needs_zext(void)
1561 struct s390_jit_data
{
1562 struct bpf_binary_header
*header
;
1568 * Compile eBPF program "fp"
1570 struct bpf_prog
*bpf_int_jit_compile(struct bpf_prog
*fp
)
1572 u32 stack_depth
= round_up(fp
->aux
->stack_depth
, 8);
1573 struct bpf_prog
*tmp
, *orig_fp
= fp
;
1574 struct bpf_binary_header
*header
;
1575 struct s390_jit_data
*jit_data
;
1576 bool tmp_blinded
= false;
1577 bool extra_pass
= false;
1581 if (!fp
->jit_requested
)
1584 tmp
= bpf_jit_blind_constants(fp
);
1586 * If blinding was requested and we failed during blinding,
1587 * we must fall back to the interpreter.
1596 jit_data
= fp
->aux
->jit_data
;
1598 jit_data
= kzalloc(sizeof(*jit_data
), GFP_KERNEL
);
1603 fp
->aux
->jit_data
= jit_data
;
1605 if (jit_data
->ctx
.addrs
) {
1606 jit
= jit_data
->ctx
;
1607 header
= jit_data
->header
;
1609 pass
= jit_data
->pass
+ 1;
1613 memset(&jit
, 0, sizeof(jit
));
1614 jit
.addrs
= kvcalloc(fp
->len
+ 1, sizeof(*jit
.addrs
), GFP_KERNEL
);
1615 if (jit
.addrs
== NULL
) {
1620 * Three initial passes:
1621 * - 1/2: Determine clobbered registers
1622 * - 3: Calculate program size and addrs arrray
1624 for (pass
= 1; pass
<= 3; pass
++) {
1625 if (bpf_jit_prog(&jit
, fp
, extra_pass
, stack_depth
)) {
1631 * Final pass: Allocate and generate program
1633 header
= bpf_jit_binary_alloc(jit
.size
, &jit
.prg_buf
, 8, jit_fill_hole
);
1639 if (bpf_jit_prog(&jit
, fp
, extra_pass
, stack_depth
)) {
1640 bpf_jit_binary_free(header
);
1644 if (bpf_jit_enable
> 1) {
1645 bpf_jit_dump(fp
->len
, jit
.size
, pass
, jit
.prg_buf
);
1646 print_fn_code(jit
.prg_buf
, jit
.size_prg
);
1648 if (!fp
->is_func
|| extra_pass
) {
1649 bpf_jit_binary_lock_ro(header
);
1651 jit_data
->header
= header
;
1652 jit_data
->ctx
= jit
;
1653 jit_data
->pass
= pass
;
1655 fp
->bpf_func
= (void *) jit
.prg_buf
;
1657 fp
->jited_len
= jit
.size
;
1659 if (!fp
->is_func
|| extra_pass
) {
1660 bpf_prog_fill_jited_linfo(fp
, jit
.addrs
+ 1);
1664 fp
->aux
->jit_data
= NULL
;
1668 bpf_jit_prog_release_other(fp
, fp
== orig_fp
?