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>
33 int bpf_jit_enable __read_mostly
;
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 lit_start
; /* Start of literal pool */
44 int lit
; /* Current position in literal pool */
45 int base_ip
; /* Base address for literal pool */
46 int ret0_ip
; /* Address of return 0 */
47 int exit_ip
; /* Address of exit */
48 int r1_thunk_ip
; /* Address of expoline thunk for 'br %r1' */
49 int r14_thunk_ip
; /* Address of expoline thunk for 'br %r14' */
50 int tail_call_start
; /* Tail call start offset */
51 int labels
[1]; /* Labels for local jumps */
54 #define BPF_SIZE_MAX 0xffff /* Max size for program (16 bit branches) */
56 #define SEEN_SKB 1 /* skb access */
57 #define SEEN_MEM 2 /* use mem[] for temporary storage */
58 #define SEEN_RET0 4 /* ret0_ip points to a valid return 0 */
59 #define SEEN_LITERAL 8 /* code uses literals */
60 #define SEEN_FUNC 16 /* calls C functions */
61 #define SEEN_TAIL_CALL 32 /* code uses tail calls */
62 #define SEEN_REG_AX 64 /* code uses constant blinding */
63 #define SEEN_STACK (SEEN_FUNC | SEEN_MEM | SEEN_SKB)
68 #define REG_W0 (MAX_BPF_JIT_REG + 0) /* Work register 1 (even) */
69 #define REG_W1 (MAX_BPF_JIT_REG + 1) /* Work register 2 (odd) */
70 #define REG_SKB_DATA (MAX_BPF_JIT_REG + 2) /* SKB data register */
71 #define REG_L (MAX_BPF_JIT_REG + 3) /* Literal pool register */
72 #define REG_15 (MAX_BPF_JIT_REG + 4) /* Register 15 */
73 #define REG_0 REG_W0 /* Register 0 */
74 #define REG_1 REG_W1 /* Register 1 */
75 #define REG_2 BPF_REG_1 /* Register 2 */
76 #define REG_14 BPF_REG_0 /* Register 14 */
79 * Mapping of BPF registers to s390 registers
81 static const int reg2hex
[] = {
84 /* Function parameters */
90 /* Call saved registers */
95 /* BPF stack pointer */
97 /* Register for blinding (shared with REG_SKB_DATA) */
99 /* SKB data pointer */
101 /* Work registers for s390x backend */
108 static inline u32
reg(u32 dst_reg
, u32 src_reg
)
110 return reg2hex
[dst_reg
] << 4 | reg2hex
[src_reg
];
113 static inline u32
reg_high(u32 reg
)
115 return reg2hex
[reg
] << 4;
118 static inline void reg_set_seen(struct bpf_jit
*jit
, u32 b1
)
120 u32 r1
= reg2hex
[b1
];
122 if (!jit
->seen_reg
[r1
] && r1
>= 6 && r1
<= 15)
123 jit
->seen_reg
[r1
] = 1;
126 #define REG_SET_SEEN(b1) \
128 reg_set_seen(jit, b1); \
131 #define REG_SEEN(b1) jit->seen_reg[reg2hex[(b1)]]
134 * EMIT macros for code generation
140 *(u16 *) (jit->prg_buf + jit->prg) = op; \
144 #define EMIT2(op, b1, b2) \
146 _EMIT2(op | reg(b1, b2)); \
154 *(u32 *) (jit->prg_buf + jit->prg) = op; \
158 #define EMIT4(op, b1, b2) \
160 _EMIT4(op | reg(b1, b2)); \
165 #define EMIT4_RRF(op, b1, b2, b3) \
167 _EMIT4(op | reg_high(b3) << 8 | reg(b1, b2)); \
173 #define _EMIT4_DISP(op, disp) \
175 unsigned int __disp = (disp) & 0xfff; \
176 _EMIT4(op | __disp); \
179 #define EMIT4_DISP(op, b1, b2, disp) \
181 _EMIT4_DISP(op | reg_high(b1) << 16 | \
182 reg_high(b2) << 8, disp); \
187 #define EMIT4_IMM(op, b1, imm) \
189 unsigned int __imm = (imm) & 0xffff; \
190 _EMIT4(op | reg_high(b1) << 16 | __imm); \
194 #define EMIT4_PCREL(op, pcrel) \
196 long __pcrel = ((pcrel) >> 1) & 0xffff; \
197 _EMIT4(op | __pcrel); \
200 #define _EMIT6(op1, op2) \
202 if (jit->prg_buf) { \
203 *(u32 *) (jit->prg_buf + jit->prg) = op1; \
204 *(u16 *) (jit->prg_buf + jit->prg + 4) = op2; \
209 #define _EMIT6_DISP(op1, op2, disp) \
211 unsigned int __disp = (disp) & 0xfff; \
212 _EMIT6(op1 | __disp, op2); \
215 #define _EMIT6_DISP_LH(op1, op2, disp) \
217 u32 _disp = (u32) disp; \
218 unsigned int __disp_h = _disp & 0xff000; \
219 unsigned int __disp_l = _disp & 0x00fff; \
220 _EMIT6(op1 | __disp_l, op2 | __disp_h >> 4); \
223 #define EMIT6_DISP_LH(op1, op2, b1, b2, b3, disp) \
225 _EMIT6_DISP_LH(op1 | reg(b1, b2) << 16 | \
226 reg_high(b3) << 8, op2, disp); \
232 #define EMIT6_PCREL_LABEL(op1, op2, b1, b2, label, mask) \
234 int rel = (jit->labels[label] - jit->prg) >> 1; \
235 _EMIT6(op1 | reg(b1, b2) << 16 | (rel & 0xffff), \
241 #define EMIT6_PCREL_IMM_LABEL(op1, op2, b1, imm, label, mask) \
243 int rel = (jit->labels[label] - jit->prg) >> 1; \
244 _EMIT6(op1 | (reg_high(b1) | mask) << 16 | \
245 (rel & 0xffff), op2 | (imm & 0xff) << 8); \
247 BUILD_BUG_ON(((unsigned long) imm) > 0xff); \
250 #define EMIT6_PCREL(op1, op2, b1, b2, i, off, mask) \
252 /* Branch instruction needs 6 bytes */ \
253 int rel = (addrs[i + off + 1] - (addrs[i + 1] - 6)) / 2;\
254 _EMIT6(op1 | reg(b1, b2) << 16 | (rel & 0xffff), op2 | mask); \
259 #define EMIT6_PCREL_RILB(op, b, target) \
261 int rel = (target - jit->prg) / 2; \
262 _EMIT6(op | reg_high(b) << 16 | rel >> 16, rel & 0xffff); \
266 #define EMIT6_PCREL_RIL(op, target) \
268 int rel = (target - jit->prg) / 2; \
269 _EMIT6(op | rel >> 16, rel & 0xffff); \
272 #define _EMIT6_IMM(op, imm) \
274 unsigned int __imm = (imm); \
275 _EMIT6(op | (__imm >> 16), __imm & 0xffff); \
278 #define EMIT6_IMM(op, b1, imm) \
280 _EMIT6_IMM(op | reg_high(b1) << 16, imm); \
284 #define EMIT_CONST_U32(val) \
287 ret = jit->lit - jit->base_ip; \
288 jit->seen |= SEEN_LITERAL; \
290 *(u32 *) (jit->prg_buf + jit->lit) = (u32) val; \
295 #define EMIT_CONST_U64(val) \
298 ret = jit->lit - jit->base_ip; \
299 jit->seen |= SEEN_LITERAL; \
301 *(u64 *) (jit->prg_buf + jit->lit) = (u64) val; \
306 #define EMIT_ZERO(b1) \
308 /* llgfr %dst,%dst (zero extend to 64 bit) */ \
309 EMIT4(0xb9160000, b1, b1); \
314 * Fill whole space with illegal instructions
316 static void jit_fill_hole(void *area
, unsigned int size
)
318 memset(area
, 0, size
);
322 * Save registers from "rs" (register start) to "re" (register end) on stack
324 static void save_regs(struct bpf_jit
*jit
, u32 rs
, u32 re
)
326 u32 off
= STK_OFF_R6
+ (rs
- 6) * 8;
329 /* stg %rs,off(%r15) */
330 _EMIT6(0xe300f000 | rs
<< 20 | off
, 0x0024);
332 /* stmg %rs,%re,off(%r15) */
333 _EMIT6_DISP(0xeb00f000 | rs
<< 20 | re
<< 16, 0x0024, off
);
337 * Restore registers from "rs" (register start) to "re" (register end) on stack
339 static void restore_regs(struct bpf_jit
*jit
, u32 rs
, u32 re
)
341 u32 off
= STK_OFF_R6
+ (rs
- 6) * 8;
343 if (jit
->seen
& SEEN_STACK
)
347 /* lg %rs,off(%r15) */
348 _EMIT6(0xe300f000 | rs
<< 20 | off
, 0x0004);
350 /* lmg %rs,%re,off(%r15) */
351 _EMIT6_DISP(0xeb00f000 | rs
<< 20 | re
<< 16, 0x0004, off
);
355 * Return first seen register (from start)
357 static int get_start(struct bpf_jit
*jit
, int start
)
361 for (i
= start
; i
<= 15; i
++) {
362 if (jit
->seen_reg
[i
])
369 * Return last seen register (from start) (gap >= 2)
371 static int get_end(struct bpf_jit
*jit
, int start
)
375 for (i
= start
; i
< 15; i
++) {
376 if (!jit
->seen_reg
[i
] && !jit
->seen_reg
[i
+ 1])
379 return jit
->seen_reg
[15] ? 15 : 14;
383 #define REGS_RESTORE 0
385 * Save and restore clobbered registers (6-15) on stack.
386 * We save/restore registers in chunks with gap >= 2 registers.
388 static void save_restore_regs(struct bpf_jit
*jit
, int op
)
394 rs
= get_start(jit
, re
);
397 re
= get_end(jit
, rs
+ 1);
399 save_regs(jit
, rs
, re
);
401 restore_regs(jit
, rs
, re
);
407 * For SKB access %b1 contains the SKB pointer. For "bpf_jit.S"
408 * we store the SKB header length on the stack and the SKB data
409 * pointer in REG_SKB_DATA if BPF_REG_AX is not used.
411 static void emit_load_skb_data_hlen(struct bpf_jit
*jit
)
413 /* Header length: llgf %w1,<len>(%b1) */
414 EMIT6_DISP_LH(0xe3000000, 0x0016, REG_W1
, REG_0
, BPF_REG_1
,
415 offsetof(struct sk_buff
, len
));
416 /* s %w1,<data_len>(%b1) */
417 EMIT4_DISP(0x5b000000, REG_W1
, BPF_REG_1
,
418 offsetof(struct sk_buff
, data_len
));
419 /* stg %w1,ST_OFF_HLEN(%r0,%r15) */
420 EMIT6_DISP_LH(0xe3000000, 0x0024, REG_W1
, REG_0
, REG_15
, STK_OFF_HLEN
);
421 if (!(jit
->seen
& SEEN_REG_AX
))
422 /* lg %skb_data,data_off(%b1) */
423 EMIT6_DISP_LH(0xe3000000, 0x0004, REG_SKB_DATA
, REG_0
,
424 BPF_REG_1
, offsetof(struct sk_buff
, data
));
428 * Emit function prologue
430 * Save registers and create stack frame if necessary.
431 * See stack frame layout desription in "bpf_jit.h"!
433 static void bpf_jit_prologue(struct bpf_jit
*jit
)
435 if (jit
->seen
& SEEN_TAIL_CALL
) {
436 /* xc STK_OFF_TCCNT(4,%r15),STK_OFF_TCCNT(%r15) */
437 _EMIT6(0xd703f000 | STK_OFF_TCCNT
, 0xf000 | STK_OFF_TCCNT
);
439 /* j tail_call_start: NOP if no tail calls are used */
440 EMIT4_PCREL(0xa7f40000, 6);
443 /* Tail calls have to skip above initialization */
444 jit
->tail_call_start
= jit
->prg
;
446 save_restore_regs(jit
, REGS_SAVE
);
447 /* Setup literal pool */
448 if (jit
->seen
& SEEN_LITERAL
) {
450 EMIT2(0x0d00, REG_L
, REG_0
);
451 jit
->base_ip
= jit
->prg
;
453 /* Setup stack and backchain */
454 if (jit
->seen
& SEEN_STACK
) {
455 if (jit
->seen
& SEEN_FUNC
)
456 /* lgr %w1,%r15 (backchain) */
457 EMIT4(0xb9040000, REG_W1
, REG_15
);
458 /* la %bfp,STK_160_UNUSED(%r15) (BPF frame pointer) */
459 EMIT4_DISP(0x41000000, BPF_REG_FP
, REG_15
, STK_160_UNUSED
);
460 /* aghi %r15,-STK_OFF */
461 EMIT4_IMM(0xa70b0000, REG_15
, -STK_OFF
);
462 if (jit
->seen
& SEEN_FUNC
)
463 /* stg %w1,152(%r15) (backchain) */
464 EMIT6_DISP_LH(0xe3000000, 0x0024, REG_W1
, REG_0
,
467 if (jit
->seen
& SEEN_SKB
) {
468 emit_load_skb_data_hlen(jit
);
469 /* stg %b1,ST_OFF_SKBP(%r0,%r15) */
470 EMIT6_DISP_LH(0xe3000000, 0x0024, BPF_REG_1
, REG_0
, REG_15
,
478 static void bpf_jit_epilogue(struct bpf_jit
*jit
)
481 if (jit
->seen
& SEEN_RET0
) {
482 jit
->ret0_ip
= jit
->prg
;
484 EMIT4_IMM(0xa7090000, BPF_REG_0
, 0);
486 jit
->exit_ip
= jit
->prg
;
487 /* Load exit code: lgr %r2,%b0 */
488 EMIT4(0xb9040000, REG_2
, BPF_REG_0
);
489 /* Restore registers */
490 save_restore_regs(jit
, REGS_RESTORE
);
491 if (IS_ENABLED(CC_USING_EXPOLINE
) && !nospec_disable
) {
492 jit
->r14_thunk_ip
= jit
->prg
;
493 /* Generate __s390_indirect_jump_r14 thunk */
494 if (test_facility(35)) {
496 EMIT6_PCREL_RIL(0xc6000000, jit
->prg
+ 10);
499 EMIT6_PCREL_RILB(0xc0000000, REG_1
, jit
->prg
+ 14);
501 EMIT4_DISP(0x44000000, REG_0
, REG_1
, 0);
504 EMIT4_PCREL(0xa7f40000, 0);
509 if (IS_ENABLED(CC_USING_EXPOLINE
) && !nospec_disable
&&
510 (jit
->seen
& SEEN_FUNC
)) {
511 jit
->r1_thunk_ip
= jit
->prg
;
512 /* Generate __s390_indirect_jump_r1 thunk */
513 if (test_facility(35)) {
515 EMIT6_PCREL_RIL(0xc6000000, jit
->prg
+ 10);
517 EMIT4_PCREL(0xa7f40000, 0);
521 /* ex 0,S390_lowcore.br_r1_tampoline */
522 EMIT4_DISP(0x44000000, REG_0
, REG_0
,
523 offsetof(struct lowcore
, br_r1_trampoline
));
525 EMIT4_PCREL(0xa7f40000, 0);
531 * Compile one eBPF instruction into s390x code
533 * NOTE: Use noinline because for gcov (-fprofile-arcs) gcc allocates a lot of
534 * stack space for the large switch statement.
536 static noinline
int bpf_jit_insn(struct bpf_jit
*jit
, struct bpf_prog
*fp
, int i
)
538 struct bpf_insn
*insn
= &fp
->insnsi
[i
];
539 int jmp_off
, last
, insn_count
= 1;
540 unsigned int func_addr
, mask
;
541 u32 dst_reg
= insn
->dst_reg
;
542 u32 src_reg
= insn
->src_reg
;
543 u32
*addrs
= jit
->addrs
;
547 if (dst_reg
== BPF_REG_AX
|| src_reg
== BPF_REG_AX
)
548 jit
->seen
|= SEEN_REG_AX
;
549 switch (insn
->code
) {
553 case BPF_ALU
| BPF_MOV
| BPF_X
: /* dst = (u32) src */
554 /* llgfr %dst,%src */
555 EMIT4(0xb9160000, dst_reg
, src_reg
);
557 case BPF_ALU64
| BPF_MOV
| BPF_X
: /* dst = src */
559 EMIT4(0xb9040000, dst_reg
, src_reg
);
561 case BPF_ALU
| BPF_MOV
| BPF_K
: /* dst = (u32) imm */
563 EMIT6_IMM(0xc00f0000, dst_reg
, imm
);
565 case BPF_ALU64
| BPF_MOV
| BPF_K
: /* dst = imm */
567 EMIT6_IMM(0xc0010000, dst_reg
, imm
);
572 case BPF_LD
| BPF_IMM
| BPF_DW
: /* dst = (u64) imm */
574 /* 16 byte instruction that uses two 'struct bpf_insn' */
577 imm64
= (u64
)(u32
) insn
[0].imm
| ((u64
)(u32
) insn
[1].imm
) << 32;
578 /* lg %dst,<d(imm)>(%l) */
579 EMIT6_DISP_LH(0xe3000000, 0x0004, dst_reg
, REG_0
, REG_L
,
580 EMIT_CONST_U64(imm64
));
587 case BPF_ALU
| BPF_ADD
| BPF_X
: /* dst = (u32) dst + (u32) src */
589 EMIT2(0x1a00, dst_reg
, src_reg
);
592 case BPF_ALU64
| BPF_ADD
| BPF_X
: /* dst = dst + src */
594 EMIT4(0xb9080000, dst_reg
, src_reg
);
596 case BPF_ALU
| BPF_ADD
| BPF_K
: /* dst = (u32) dst + (u32) imm */
600 EMIT6_IMM(0xc20b0000, dst_reg
, imm
);
603 case BPF_ALU64
| BPF_ADD
| BPF_K
: /* dst = dst + imm */
607 EMIT6_IMM(0xc2080000, dst_reg
, imm
);
612 case BPF_ALU
| BPF_SUB
| BPF_X
: /* dst = (u32) dst - (u32) src */
614 EMIT2(0x1b00, dst_reg
, src_reg
);
617 case BPF_ALU64
| BPF_SUB
| BPF_X
: /* dst = dst - src */
619 EMIT4(0xb9090000, dst_reg
, src_reg
);
621 case BPF_ALU
| BPF_SUB
| BPF_K
: /* dst = (u32) dst - (u32) imm */
625 EMIT6_IMM(0xc20b0000, dst_reg
, -imm
);
628 case BPF_ALU64
| BPF_SUB
| BPF_K
: /* dst = dst - imm */
632 EMIT6_IMM(0xc2080000, dst_reg
, -imm
);
637 case BPF_ALU
| BPF_MUL
| BPF_X
: /* dst = (u32) dst * (u32) src */
639 EMIT4(0xb2520000, dst_reg
, src_reg
);
642 case BPF_ALU64
| BPF_MUL
| BPF_X
: /* dst = dst * src */
644 EMIT4(0xb90c0000, dst_reg
, src_reg
);
646 case BPF_ALU
| BPF_MUL
| BPF_K
: /* dst = (u32) dst * (u32) imm */
650 EMIT6_IMM(0xc2010000, dst_reg
, imm
);
653 case BPF_ALU64
| BPF_MUL
| BPF_K
: /* dst = dst * imm */
657 EMIT6_IMM(0xc2000000, dst_reg
, imm
);
662 case BPF_ALU
| BPF_DIV
| BPF_X
: /* dst = (u32) dst / (u32) src */
663 case BPF_ALU
| BPF_MOD
| BPF_X
: /* dst = (u32) dst % (u32) src */
665 int rc_reg
= BPF_OP(insn
->code
) == BPF_DIV
? REG_W1
: REG_W0
;
667 jit
->seen
|= SEEN_RET0
;
668 /* ltr %src,%src (if src == 0 goto fail) */
669 EMIT2(0x1200, src_reg
, src_reg
);
671 EMIT4_PCREL(0xa7840000, jit
->ret0_ip
- jit
->prg
);
673 EMIT4_IMM(0xa7080000, REG_W0
, 0);
675 EMIT2(0x1800, REG_W1
, dst_reg
);
677 EMIT4(0xb9970000, REG_W0
, src_reg
);
679 EMIT4(0xb9160000, dst_reg
, rc_reg
);
682 case BPF_ALU64
| BPF_DIV
| BPF_X
: /* dst = dst / src */
683 case BPF_ALU64
| BPF_MOD
| BPF_X
: /* dst = dst % src */
685 int rc_reg
= BPF_OP(insn
->code
) == BPF_DIV
? REG_W1
: REG_W0
;
687 jit
->seen
|= SEEN_RET0
;
688 /* ltgr %src,%src (if src == 0 goto fail) */
689 EMIT4(0xb9020000, src_reg
, src_reg
);
691 EMIT4_PCREL(0xa7840000, jit
->ret0_ip
- jit
->prg
);
693 EMIT4_IMM(0xa7090000, REG_W0
, 0);
695 EMIT4(0xb9040000, REG_W1
, dst_reg
);
697 EMIT4(0xb9870000, REG_W0
, src_reg
);
699 EMIT4(0xb9040000, dst_reg
, rc_reg
);
702 case BPF_ALU
| BPF_DIV
| BPF_K
: /* dst = (u32) dst / (u32) imm */
703 case BPF_ALU
| BPF_MOD
| BPF_K
: /* dst = (u32) dst % (u32) imm */
705 int rc_reg
= BPF_OP(insn
->code
) == BPF_DIV
? REG_W1
: REG_W0
;
708 if (BPF_OP(insn
->code
) == BPF_MOD
)
710 EMIT4_IMM(0xa7090000, dst_reg
, 0);
714 EMIT4_IMM(0xa7080000, REG_W0
, 0);
716 EMIT2(0x1800, REG_W1
, dst_reg
);
717 /* dl %w0,<d(imm)>(%l) */
718 EMIT6_DISP_LH(0xe3000000, 0x0097, REG_W0
, REG_0
, REG_L
,
719 EMIT_CONST_U32(imm
));
721 EMIT4(0xb9160000, dst_reg
, rc_reg
);
724 case BPF_ALU64
| BPF_DIV
| BPF_K
: /* dst = dst / imm */
725 case BPF_ALU64
| BPF_MOD
| BPF_K
: /* dst = dst % imm */
727 int rc_reg
= BPF_OP(insn
->code
) == BPF_DIV
? REG_W1
: REG_W0
;
730 if (BPF_OP(insn
->code
) == BPF_MOD
)
732 EMIT4_IMM(0xa7090000, dst_reg
, 0);
736 EMIT4_IMM(0xa7090000, REG_W0
, 0);
738 EMIT4(0xb9040000, REG_W1
, dst_reg
);
739 /* dlg %w0,<d(imm)>(%l) */
740 EMIT6_DISP_LH(0xe3000000, 0x0087, REG_W0
, REG_0
, REG_L
,
741 EMIT_CONST_U64(imm
));
743 EMIT4(0xb9040000, dst_reg
, rc_reg
);
749 case BPF_ALU
| BPF_AND
| BPF_X
: /* dst = (u32) dst & (u32) src */
751 EMIT2(0x1400, dst_reg
, src_reg
);
754 case BPF_ALU64
| BPF_AND
| BPF_X
: /* dst = dst & src */
756 EMIT4(0xb9800000, dst_reg
, src_reg
);
758 case BPF_ALU
| BPF_AND
| BPF_K
: /* dst = (u32) dst & (u32) imm */
760 EMIT6_IMM(0xc00b0000, dst_reg
, imm
);
763 case BPF_ALU64
| BPF_AND
| BPF_K
: /* dst = dst & imm */
764 /* ng %dst,<d(imm)>(%l) */
765 EMIT6_DISP_LH(0xe3000000, 0x0080, dst_reg
, REG_0
, REG_L
,
766 EMIT_CONST_U64(imm
));
771 case BPF_ALU
| BPF_OR
| BPF_X
: /* dst = (u32) dst | (u32) src */
773 EMIT2(0x1600, dst_reg
, src_reg
);
776 case BPF_ALU64
| BPF_OR
| BPF_X
: /* dst = dst | src */
778 EMIT4(0xb9810000, dst_reg
, src_reg
);
780 case BPF_ALU
| BPF_OR
| BPF_K
: /* dst = (u32) dst | (u32) imm */
782 EMIT6_IMM(0xc00d0000, dst_reg
, imm
);
785 case BPF_ALU64
| BPF_OR
| BPF_K
: /* dst = dst | imm */
786 /* og %dst,<d(imm)>(%l) */
787 EMIT6_DISP_LH(0xe3000000, 0x0081, dst_reg
, REG_0
, REG_L
,
788 EMIT_CONST_U64(imm
));
793 case BPF_ALU
| BPF_XOR
| BPF_X
: /* dst = (u32) dst ^ (u32) src */
795 EMIT2(0x1700, dst_reg
, src_reg
);
798 case BPF_ALU64
| BPF_XOR
| BPF_X
: /* dst = dst ^ src */
800 EMIT4(0xb9820000, dst_reg
, src_reg
);
802 case BPF_ALU
| BPF_XOR
| BPF_K
: /* dst = (u32) dst ^ (u32) imm */
806 EMIT6_IMM(0xc0070000, dst_reg
, imm
);
809 case BPF_ALU64
| BPF_XOR
| BPF_K
: /* dst = dst ^ imm */
810 /* xg %dst,<d(imm)>(%l) */
811 EMIT6_DISP_LH(0xe3000000, 0x0082, dst_reg
, REG_0
, REG_L
,
812 EMIT_CONST_U64(imm
));
817 case BPF_ALU
| BPF_LSH
| BPF_X
: /* dst = (u32) dst << (u32) src */
818 /* sll %dst,0(%src) */
819 EMIT4_DISP(0x89000000, dst_reg
, src_reg
, 0);
822 case BPF_ALU64
| BPF_LSH
| BPF_X
: /* dst = dst << src */
823 /* sllg %dst,%dst,0(%src) */
824 EMIT6_DISP_LH(0xeb000000, 0x000d, dst_reg
, dst_reg
, src_reg
, 0);
826 case BPF_ALU
| BPF_LSH
| BPF_K
: /* dst = (u32) dst << (u32) imm */
829 /* sll %dst,imm(%r0) */
830 EMIT4_DISP(0x89000000, dst_reg
, REG_0
, imm
);
833 case BPF_ALU64
| BPF_LSH
| BPF_K
: /* dst = dst << imm */
836 /* sllg %dst,%dst,imm(%r0) */
837 EMIT6_DISP_LH(0xeb000000, 0x000d, dst_reg
, dst_reg
, REG_0
, imm
);
842 case BPF_ALU
| BPF_RSH
| BPF_X
: /* dst = (u32) dst >> (u32) src */
843 /* srl %dst,0(%src) */
844 EMIT4_DISP(0x88000000, dst_reg
, src_reg
, 0);
847 case BPF_ALU64
| BPF_RSH
| BPF_X
: /* dst = dst >> src */
848 /* srlg %dst,%dst,0(%src) */
849 EMIT6_DISP_LH(0xeb000000, 0x000c, dst_reg
, dst_reg
, src_reg
, 0);
851 case BPF_ALU
| BPF_RSH
| BPF_K
: /* dst = (u32) dst >> (u32) imm */
854 /* srl %dst,imm(%r0) */
855 EMIT4_DISP(0x88000000, dst_reg
, REG_0
, imm
);
858 case BPF_ALU64
| BPF_RSH
| BPF_K
: /* dst = dst >> imm */
861 /* srlg %dst,%dst,imm(%r0) */
862 EMIT6_DISP_LH(0xeb000000, 0x000c, dst_reg
, dst_reg
, REG_0
, imm
);
867 case BPF_ALU64
| BPF_ARSH
| BPF_X
: /* ((s64) dst) >>= src */
868 /* srag %dst,%dst,0(%src) */
869 EMIT6_DISP_LH(0xeb000000, 0x000a, dst_reg
, dst_reg
, src_reg
, 0);
871 case BPF_ALU64
| BPF_ARSH
| BPF_K
: /* ((s64) dst) >>= imm */
874 /* srag %dst,%dst,imm(%r0) */
875 EMIT6_DISP_LH(0xeb000000, 0x000a, dst_reg
, dst_reg
, REG_0
, imm
);
880 case BPF_ALU
| BPF_NEG
: /* dst = (u32) -dst */
882 EMIT2(0x1300, dst_reg
, dst_reg
);
885 case BPF_ALU64
| BPF_NEG
: /* dst = -dst */
887 EMIT4(0xb9130000, dst_reg
, dst_reg
);
892 case BPF_ALU
| BPF_END
| BPF_FROM_BE
:
893 /* s390 is big endian, therefore only clear high order bytes */
895 case 16: /* dst = (u16) cpu_to_be16(dst) */
896 /* llghr %dst,%dst */
897 EMIT4(0xb9850000, dst_reg
, dst_reg
);
899 case 32: /* dst = (u32) cpu_to_be32(dst) */
900 /* llgfr %dst,%dst */
901 EMIT4(0xb9160000, dst_reg
, dst_reg
);
903 case 64: /* dst = (u64) cpu_to_be64(dst) */
907 case BPF_ALU
| BPF_END
| BPF_FROM_LE
:
909 case 16: /* dst = (u16) cpu_to_le16(dst) */
911 EMIT4(0xb91f0000, dst_reg
, dst_reg
);
912 /* srl %dst,16(%r0) */
913 EMIT4_DISP(0x88000000, dst_reg
, REG_0
, 16);
914 /* llghr %dst,%dst */
915 EMIT4(0xb9850000, dst_reg
, dst_reg
);
917 case 32: /* dst = (u32) cpu_to_le32(dst) */
919 EMIT4(0xb91f0000, dst_reg
, dst_reg
);
920 /* llgfr %dst,%dst */
921 EMIT4(0xb9160000, dst_reg
, dst_reg
);
923 case 64: /* dst = (u64) cpu_to_le64(dst) */
924 /* lrvgr %dst,%dst */
925 EMIT4(0xb90f0000, dst_reg
, dst_reg
);
932 case BPF_STX
| BPF_MEM
| BPF_B
: /* *(u8 *)(dst + off) = src_reg */
933 /* stcy %src,off(%dst) */
934 EMIT6_DISP_LH(0xe3000000, 0x0072, src_reg
, dst_reg
, REG_0
, off
);
935 jit
->seen
|= SEEN_MEM
;
937 case BPF_STX
| BPF_MEM
| BPF_H
: /* (u16 *)(dst + off) = src */
938 /* sthy %src,off(%dst) */
939 EMIT6_DISP_LH(0xe3000000, 0x0070, src_reg
, dst_reg
, REG_0
, off
);
940 jit
->seen
|= SEEN_MEM
;
942 case BPF_STX
| BPF_MEM
| BPF_W
: /* *(u32 *)(dst + off) = src */
943 /* sty %src,off(%dst) */
944 EMIT6_DISP_LH(0xe3000000, 0x0050, src_reg
, dst_reg
, REG_0
, off
);
945 jit
->seen
|= SEEN_MEM
;
947 case BPF_STX
| BPF_MEM
| BPF_DW
: /* (u64 *)(dst + off) = src */
948 /* stg %src,off(%dst) */
949 EMIT6_DISP_LH(0xe3000000, 0x0024, src_reg
, dst_reg
, REG_0
, off
);
950 jit
->seen
|= SEEN_MEM
;
952 case BPF_ST
| BPF_MEM
| BPF_B
: /* *(u8 *)(dst + off) = imm */
954 EMIT4_IMM(0xa7080000, REG_W0
, (u8
) imm
);
955 /* stcy %w0,off(dst) */
956 EMIT6_DISP_LH(0xe3000000, 0x0072, REG_W0
, dst_reg
, REG_0
, off
);
957 jit
->seen
|= SEEN_MEM
;
959 case BPF_ST
| BPF_MEM
| BPF_H
: /* (u16 *)(dst + off) = imm */
961 EMIT4_IMM(0xa7080000, REG_W0
, (u16
) imm
);
962 /* sthy %w0,off(dst) */
963 EMIT6_DISP_LH(0xe3000000, 0x0070, REG_W0
, dst_reg
, REG_0
, off
);
964 jit
->seen
|= SEEN_MEM
;
966 case BPF_ST
| BPF_MEM
| BPF_W
: /* *(u32 *)(dst + off) = imm */
968 EMIT6_IMM(0xc00f0000, REG_W0
, (u32
) imm
);
969 /* sty %w0,off(%dst) */
970 EMIT6_DISP_LH(0xe3000000, 0x0050, REG_W0
, dst_reg
, REG_0
, off
);
971 jit
->seen
|= SEEN_MEM
;
973 case BPF_ST
| BPF_MEM
| BPF_DW
: /* *(u64 *)(dst + off) = imm */
975 EMIT6_IMM(0xc0010000, REG_W0
, imm
);
976 /* stg %w0,off(%dst) */
977 EMIT6_DISP_LH(0xe3000000, 0x0024, REG_W0
, dst_reg
, REG_0
, off
);
978 jit
->seen
|= SEEN_MEM
;
981 * BPF_STX XADD (atomic_add)
983 case BPF_STX
| BPF_XADD
| BPF_W
: /* *(u32 *)(dst + off) += src */
984 /* laal %w0,%src,off(%dst) */
985 EMIT6_DISP_LH(0xeb000000, 0x00fa, REG_W0
, src_reg
,
987 jit
->seen
|= SEEN_MEM
;
989 case BPF_STX
| BPF_XADD
| BPF_DW
: /* *(u64 *)(dst + off) += src */
990 /* laalg %w0,%src,off(%dst) */
991 EMIT6_DISP_LH(0xeb000000, 0x00ea, REG_W0
, src_reg
,
993 jit
->seen
|= SEEN_MEM
;
998 case BPF_LDX
| BPF_MEM
| BPF_B
: /* dst = *(u8 *)(ul) (src + off) */
999 /* llgc %dst,0(off,%src) */
1000 EMIT6_DISP_LH(0xe3000000, 0x0090, dst_reg
, src_reg
, REG_0
, off
);
1001 jit
->seen
|= SEEN_MEM
;
1003 case BPF_LDX
| BPF_MEM
| BPF_H
: /* dst = *(u16 *)(ul) (src + off) */
1004 /* llgh %dst,0(off,%src) */
1005 EMIT6_DISP_LH(0xe3000000, 0x0091, dst_reg
, src_reg
, REG_0
, off
);
1006 jit
->seen
|= SEEN_MEM
;
1008 case BPF_LDX
| BPF_MEM
| BPF_W
: /* dst = *(u32 *)(ul) (src + off) */
1009 /* llgf %dst,off(%src) */
1010 jit
->seen
|= SEEN_MEM
;
1011 EMIT6_DISP_LH(0xe3000000, 0x0016, dst_reg
, src_reg
, REG_0
, off
);
1013 case BPF_LDX
| BPF_MEM
| BPF_DW
: /* dst = *(u64 *)(ul) (src + off) */
1014 /* lg %dst,0(off,%src) */
1015 jit
->seen
|= SEEN_MEM
;
1016 EMIT6_DISP_LH(0xe3000000, 0x0004, dst_reg
, src_reg
, REG_0
, off
);
1021 case BPF_JMP
| BPF_CALL
:
1024 * b0 = (__bpf_call_base + imm)(b1, b2, b3, b4, b5)
1026 const u64 func
= (u64
)__bpf_call_base
+ imm
;
1028 REG_SET_SEEN(BPF_REG_5
);
1029 jit
->seen
|= SEEN_FUNC
;
1030 /* lg %w1,<d(imm)>(%l) */
1031 EMIT6_DISP_LH(0xe3000000, 0x0004, REG_W1
, REG_0
, REG_L
,
1032 EMIT_CONST_U64(func
));
1033 if (IS_ENABLED(CC_USING_EXPOLINE
) && !nospec_disable
) {
1034 /* brasl %r14,__s390_indirect_jump_r1 */
1035 EMIT6_PCREL_RILB(0xc0050000, REG_14
, jit
->r1_thunk_ip
);
1038 EMIT2(0x0d00, REG_14
, REG_W1
);
1040 /* lgr %b0,%r2: load return value into %b0 */
1041 EMIT4(0xb9040000, BPF_REG_0
, REG_2
);
1042 if ((jit
->seen
& SEEN_SKB
) &&
1043 bpf_helper_changes_pkt_data((void *)func
)) {
1044 /* lg %b1,ST_OFF_SKBP(%r15) */
1045 EMIT6_DISP_LH(0xe3000000, 0x0004, BPF_REG_1
, REG_0
,
1046 REG_15
, STK_OFF_SKBP
);
1047 emit_load_skb_data_hlen(jit
);
1051 case BPF_JMP
| BPF_TAIL_CALL
:
1054 * B1: pointer to ctx
1055 * B2: pointer to bpf_array
1056 * B3: index in bpf_array
1058 jit
->seen
|= SEEN_TAIL_CALL
;
1061 * if (index >= array->map.max_entries)
1065 /* llgf %w1,map.max_entries(%b2) */
1066 EMIT6_DISP_LH(0xe3000000, 0x0016, REG_W1
, REG_0
, BPF_REG_2
,
1067 offsetof(struct bpf_array
, map
.max_entries
));
1068 /* clgrj %b3,%w1,0xa,label0: if %b3 >= %w1 goto out */
1069 EMIT6_PCREL_LABEL(0xec000000, 0x0065, BPF_REG_3
,
1073 * if (tail_call_cnt++ > MAX_TAIL_CALL_CNT)
1077 if (jit
->seen
& SEEN_STACK
)
1078 off
= STK_OFF_TCCNT
+ STK_OFF
;
1080 off
= STK_OFF_TCCNT
;
1082 EMIT4_IMM(0xa7080000, REG_W0
, 1);
1083 /* laal %w1,%w0,off(%r15) */
1084 EMIT6_DISP_LH(0xeb000000, 0x00fa, REG_W1
, REG_W0
, REG_15
, off
);
1085 /* clij %w1,MAX_TAIL_CALL_CNT,0x2,label0 */
1086 EMIT6_PCREL_IMM_LABEL(0xec000000, 0x007f, REG_W1
,
1087 MAX_TAIL_CALL_CNT
, 0, 0x2);
1090 * prog = array->ptrs[index];
1095 /* sllg %r1,%b3,3: %r1 = index * 8 */
1096 EMIT6_DISP_LH(0xeb000000, 0x000d, REG_1
, BPF_REG_3
, REG_0
, 3);
1097 /* lg %r1,prog(%b2,%r1) */
1098 EMIT6_DISP_LH(0xe3000000, 0x0004, REG_1
, BPF_REG_2
,
1099 REG_1
, offsetof(struct bpf_array
, ptrs
));
1100 /* clgij %r1,0,0x8,label0 */
1101 EMIT6_PCREL_IMM_LABEL(0xec000000, 0x007d, REG_1
, 0, 0, 0x8);
1104 * Restore registers before calling function
1106 save_restore_regs(jit
, REGS_RESTORE
);
1109 * goto *(prog->bpf_func + tail_call_start);
1112 /* lg %r1,bpf_func(%r1) */
1113 EMIT6_DISP_LH(0xe3000000, 0x0004, REG_1
, REG_1
, REG_0
,
1114 offsetof(struct bpf_prog
, bpf_func
));
1115 /* bc 0xf,tail_call_start(%r1) */
1116 _EMIT4(0x47f01000 + jit
->tail_call_start
);
1118 jit
->labels
[0] = jit
->prg
;
1120 case BPF_JMP
| BPF_EXIT
: /* return b0 */
1121 last
= (i
== fp
->len
- 1) ? 1 : 0;
1122 if (last
&& !(jit
->seen
& SEEN_RET0
))
1125 EMIT4_PCREL(0xa7f40000, jit
->exit_ip
- jit
->prg
);
1128 * Branch relative (number of skipped instructions) to offset on
1131 * Condition code to mask mapping:
1133 * CC | Description | Mask
1134 * ------------------------------
1135 * 0 | Operands equal | 8
1136 * 1 | First operand low | 4
1137 * 2 | First operand high | 2
1140 * For s390x relative branches: ip = ip + off_bytes
1141 * For BPF relative branches: insn = insn + off_insns + 1
1143 * For example for s390x with offset 0 we jump to the branch
1144 * instruction itself (loop) and for BPF with offset 0 we
1145 * branch to the instruction behind the branch.
1147 case BPF_JMP
| BPF_JA
: /* if (true) */
1148 mask
= 0xf000; /* j */
1150 case BPF_JMP
| BPF_JSGT
| BPF_K
: /* ((s64) dst > (s64) imm) */
1151 mask
= 0x2000; /* jh */
1153 case BPF_JMP
| BPF_JSLT
| BPF_K
: /* ((s64) dst < (s64) imm) */
1154 mask
= 0x4000; /* jl */
1156 case BPF_JMP
| BPF_JSGE
| BPF_K
: /* ((s64) dst >= (s64) imm) */
1157 mask
= 0xa000; /* jhe */
1159 case BPF_JMP
| BPF_JSLE
| BPF_K
: /* ((s64) dst <= (s64) imm) */
1160 mask
= 0xc000; /* jle */
1162 case BPF_JMP
| BPF_JGT
| BPF_K
: /* (dst_reg > imm) */
1163 mask
= 0x2000; /* jh */
1165 case BPF_JMP
| BPF_JLT
| BPF_K
: /* (dst_reg < imm) */
1166 mask
= 0x4000; /* jl */
1168 case BPF_JMP
| BPF_JGE
| BPF_K
: /* (dst_reg >= imm) */
1169 mask
= 0xa000; /* jhe */
1171 case BPF_JMP
| BPF_JLE
| BPF_K
: /* (dst_reg <= imm) */
1172 mask
= 0xc000; /* jle */
1174 case BPF_JMP
| BPF_JNE
| BPF_K
: /* (dst_reg != imm) */
1175 mask
= 0x7000; /* jne */
1177 case BPF_JMP
| BPF_JEQ
| BPF_K
: /* (dst_reg == imm) */
1178 mask
= 0x8000; /* je */
1180 case BPF_JMP
| BPF_JSET
| BPF_K
: /* (dst_reg & imm) */
1181 mask
= 0x7000; /* jnz */
1182 /* lgfi %w1,imm (load sign extend imm) */
1183 EMIT6_IMM(0xc0010000, REG_W1
, imm
);
1185 EMIT4(0xb9800000, REG_W1
, dst_reg
);
1188 case BPF_JMP
| BPF_JSGT
| BPF_X
: /* ((s64) dst > (s64) src) */
1189 mask
= 0x2000; /* jh */
1191 case BPF_JMP
| BPF_JSLT
| BPF_X
: /* ((s64) dst < (s64) src) */
1192 mask
= 0x4000; /* jl */
1194 case BPF_JMP
| BPF_JSGE
| BPF_X
: /* ((s64) dst >= (s64) src) */
1195 mask
= 0xa000; /* jhe */
1197 case BPF_JMP
| BPF_JSLE
| BPF_X
: /* ((s64) dst <= (s64) src) */
1198 mask
= 0xc000; /* jle */
1200 case BPF_JMP
| BPF_JGT
| BPF_X
: /* (dst > src) */
1201 mask
= 0x2000; /* jh */
1203 case BPF_JMP
| BPF_JLT
| BPF_X
: /* (dst < src) */
1204 mask
= 0x4000; /* jl */
1206 case BPF_JMP
| BPF_JGE
| BPF_X
: /* (dst >= src) */
1207 mask
= 0xa000; /* jhe */
1209 case BPF_JMP
| BPF_JLE
| BPF_X
: /* (dst <= src) */
1210 mask
= 0xc000; /* jle */
1212 case BPF_JMP
| BPF_JNE
| BPF_X
: /* (dst != src) */
1213 mask
= 0x7000; /* jne */
1215 case BPF_JMP
| BPF_JEQ
| BPF_X
: /* (dst == src) */
1216 mask
= 0x8000; /* je */
1218 case BPF_JMP
| BPF_JSET
| BPF_X
: /* (dst & src) */
1219 mask
= 0x7000; /* jnz */
1220 /* ngrk %w1,%dst,%src */
1221 EMIT4_RRF(0xb9e40000, REG_W1
, dst_reg
, src_reg
);
1224 /* lgfi %w1,imm (load sign extend imm) */
1225 EMIT6_IMM(0xc0010000, REG_W1
, imm
);
1226 /* cgrj %dst,%w1,mask,off */
1227 EMIT6_PCREL(0xec000000, 0x0064, dst_reg
, REG_W1
, i
, off
, mask
);
1230 /* lgfi %w1,imm (load sign extend imm) */
1231 EMIT6_IMM(0xc0010000, REG_W1
, imm
);
1232 /* clgrj %dst,%w1,mask,off */
1233 EMIT6_PCREL(0xec000000, 0x0065, dst_reg
, REG_W1
, i
, off
, mask
);
1236 /* cgrj %dst,%src,mask,off */
1237 EMIT6_PCREL(0xec000000, 0x0064, dst_reg
, src_reg
, i
, off
, mask
);
1240 /* clgrj %dst,%src,mask,off */
1241 EMIT6_PCREL(0xec000000, 0x0065, dst_reg
, src_reg
, i
, off
, mask
);
1244 /* brc mask,jmp_off (branch instruction needs 4 bytes) */
1245 jmp_off
= addrs
[i
+ off
+ 1] - (addrs
[i
+ 1] - 4);
1246 EMIT4_PCREL(0xa7040000 | mask
<< 8, jmp_off
);
1251 case BPF_LD
| BPF_ABS
| BPF_B
: /* b0 = *(u8 *) (skb->data+imm) */
1252 case BPF_LD
| BPF_IND
| BPF_B
: /* b0 = *(u8 *) (skb->data+imm+src) */
1253 if ((BPF_MODE(insn
->code
) == BPF_ABS
) && (imm
>= 0))
1254 func_addr
= __pa(sk_load_byte_pos
);
1256 func_addr
= __pa(sk_load_byte
);
1258 case BPF_LD
| BPF_ABS
| BPF_H
: /* b0 = *(u16 *) (skb->data+imm) */
1259 case BPF_LD
| BPF_IND
| BPF_H
: /* b0 = *(u16 *) (skb->data+imm+src) */
1260 if ((BPF_MODE(insn
->code
) == BPF_ABS
) && (imm
>= 0))
1261 func_addr
= __pa(sk_load_half_pos
);
1263 func_addr
= __pa(sk_load_half
);
1265 case BPF_LD
| BPF_ABS
| BPF_W
: /* b0 = *(u32 *) (skb->data+imm) */
1266 case BPF_LD
| BPF_IND
| BPF_W
: /* b0 = *(u32 *) (skb->data+imm+src) */
1267 if ((BPF_MODE(insn
->code
) == BPF_ABS
) && (imm
>= 0))
1268 func_addr
= __pa(sk_load_word_pos
);
1270 func_addr
= __pa(sk_load_word
);
1273 jit
->seen
|= SEEN_SKB
| SEEN_RET0
| SEEN_FUNC
;
1274 REG_SET_SEEN(REG_14
); /* Return address of possible func call */
1278 * BPF_REG_6 (R7) : skb pointer
1279 * REG_SKB_DATA (R12): skb data pointer (if no BPF_REG_AX)
1282 * BPF_REG_2 (R3) : offset of byte(s) to fetch in skb
1283 * BPF_REG_5 (R6) : return address
1286 * BPF_REG_0 (R14): data read from skb
1288 * Scratch registers (BPF_REG_1-5)
1291 /* Call function: llilf %w1,func_addr */
1292 EMIT6_IMM(0xc00f0000, REG_W1
, func_addr
);
1294 /* Offset: lgfi %b2,imm */
1295 EMIT6_IMM(0xc0010000, BPF_REG_2
, imm
);
1296 if (BPF_MODE(insn
->code
) == BPF_IND
)
1297 /* agfr %b2,%src (%src is s32 here) */
1298 EMIT4(0xb9180000, BPF_REG_2
, src_reg
);
1300 /* Reload REG_SKB_DATA if BPF_REG_AX is used */
1301 if (jit
->seen
& SEEN_REG_AX
)
1302 /* lg %skb_data,data_off(%b6) */
1303 EMIT6_DISP_LH(0xe3000000, 0x0004, REG_SKB_DATA
, REG_0
,
1304 BPF_REG_6
, offsetof(struct sk_buff
, data
));
1305 /* basr %b5,%w1 (%b5 is call saved) */
1306 EMIT2(0x0d00, BPF_REG_5
, REG_W1
);
1309 * Note: For fast access we jump directly after the
1310 * jnz instruction from bpf_jit.S
1313 EMIT4_PCREL(0xa7740000, jit
->ret0_ip
- jit
->prg
);
1315 default: /* too complex, give up */
1316 pr_err("Unknown opcode %02x\n", insn
->code
);
1323 * Compile eBPF program into s390x code
1325 static int bpf_jit_prog(struct bpf_jit
*jit
, struct bpf_prog
*fp
)
1329 jit
->lit
= jit
->lit_start
;
1332 bpf_jit_prologue(jit
);
1333 for (i
= 0; i
< fp
->len
; i
+= insn_count
) {
1334 insn_count
= bpf_jit_insn(jit
, fp
, i
);
1337 /* Next instruction address */
1338 jit
->addrs
[i
+ insn_count
] = jit
->prg
;
1340 bpf_jit_epilogue(jit
);
1342 jit
->lit_start
= jit
->prg
;
1343 jit
->size
= jit
->lit
;
1344 jit
->size_prg
= jit
->prg
;
1349 * Compile eBPF program "fp"
1351 struct bpf_prog
*bpf_int_jit_compile(struct bpf_prog
*fp
)
1353 struct bpf_prog
*tmp
, *orig_fp
= fp
;
1354 struct bpf_binary_header
*header
;
1355 bool tmp_blinded
= false;
1359 if (!bpf_jit_enable
)
1362 tmp
= bpf_jit_blind_constants(fp
);
1364 * If blinding was requested and we failed during blinding,
1365 * we must fall back to the interpreter.
1374 memset(&jit
, 0, sizeof(jit
));
1375 jit
.addrs
= kcalloc(fp
->len
+ 1, sizeof(*jit
.addrs
), GFP_KERNEL
);
1376 if (jit
.addrs
== NULL
) {
1381 * Three initial passes:
1382 * - 1/2: Determine clobbered registers
1383 * - 3: Calculate program size and addrs arrray
1385 for (pass
= 1; pass
<= 3; pass
++) {
1386 if (bpf_jit_prog(&jit
, fp
)) {
1392 * Final pass: Allocate and generate program
1394 if (jit
.size
>= BPF_SIZE_MAX
) {
1398 header
= bpf_jit_binary_alloc(jit
.size
, &jit
.prg_buf
, 2, jit_fill_hole
);
1403 if (bpf_jit_prog(&jit
, fp
)) {
1404 bpf_jit_binary_free(header
);
1408 if (bpf_jit_enable
> 1) {
1409 bpf_jit_dump(fp
->len
, jit
.size
, pass
, jit
.prg_buf
);
1410 print_fn_code(jit
.prg_buf
, jit
.size_prg
);
1412 bpf_jit_binary_lock_ro(header
);
1413 fp
->bpf_func
= (void *) jit
.prg_buf
;
1415 fp
->jited_len
= jit
.size
;
1420 bpf_jit_prog_release_other(fp
, fp
== orig_fp
?