// SPDX-License-Identifier: GPL-2.0 /* * BPF JIT compiler for PA-RISC (32-bit) * * Copyright (c) 2023 Helge Deller <deller@gmx.de> * * The code is based on the BPF JIT compiler for RV64 by Björn Töpel and * the BPF JIT compiler for 32-bit ARM by Shubham Bansal and Mircea Gherzan.
*/
staticconst s8 regmap[][2] = { /* Return value from in-kernel function, and exit value from eBPF. */
[BPF_REG_0] = {HPPA_REG_RET0, HPPA_REG_RET1}, /* HI/LOW */
/* Arguments from eBPF program to in-kernel function. */
[BPF_REG_1] = {HPPA_R(3), HPPA_R(4)},
[BPF_REG_2] = {HPPA_R(5), HPPA_R(6)},
[BPF_REG_3] = {HPPA_R(7), HPPA_R(8)},
[BPF_REG_4] = {HPPA_R(9), HPPA_R(10)},
[BPF_REG_5] = {HPPA_R(11), HPPA_R(12)},
[BPF_REG_6] = {HPPA_R(13), HPPA_R(14)},
[BPF_REG_7] = {HPPA_R(15), HPPA_R(16)}, /* * Callee-saved registers that in-kernel function will preserve. * Stored on the stack.
*/
[BPF_REG_8] = {STACK_OFFSET(BPF_R8_HI), STACK_OFFSET(BPF_R8_LO)},
[BPF_REG_9] = {STACK_OFFSET(BPF_R9_HI), STACK_OFFSET(BPF_R9_LO)},
/* Read-only frame pointer to access BPF stack. Not needed. */
[BPF_REG_FP] = {STACK_OFFSET(BPF_FP_HI), STACK_OFFSET(BPF_FP_LO)},
/* Temporary register for blinding constants. Stored on the stack. */
[BPF_REG_AX] = {STACK_OFFSET(BPF_AX_HI), STACK_OFFSET(BPF_AX_LO)}, /* * Temporary registers used by the JIT to operate on registers stored * on the stack. Save t0 and t1 to be used as temporaries in generated * code.
*/
[TMP_REG_1] = {HPPA_REG_T3, HPPA_REG_T2},
[TMP_REG_2] = {HPPA_REG_T5, HPPA_REG_T4},
/* temporary space for BPF_R0 during libgcc and millicode calls */
[TMP_REG_R0] = {STACK_OFFSET(BPF_R0_TEMP_HI), STACK_OFFSET(BPF_R0_TEMP_LO)},
};
if (is_tail_call) { /* * goto *(t0 + 4); * Skips first instruction of prologue which initializes tail * call counter. Assumes t0 contains address of target program, * see emit_bpf_tail_call.
*/
emit(hppa_ldo(1 * HPPA_INSN_SIZE, HPPA_REG_T0, HPPA_REG_T0), ctx);
emit(hppa_bv(HPPA_REG_ZERO, HPPA_REG_T0, EXEC_NEXT_INSTR), ctx); /* in delay slot: */
emit(hppa_copy(HPPA_REG_TCC, HPPA_REG_TCC_IN_INIT), ctx);
return;
}
/* load epilogue function pointer and jump to it. */ /* exit point is either directly below, or the outest TCC exit function */
emit(EXIT_PTR_LOAD(HPPA_REG_RP), ctx);
emit(EXIT_PTR_JUMP(HPPA_REG_RP, NOP_NEXT_INSTR), ctx);
/* NOTE: we are 32-bit and big-endian, so return lower 32-bit value */
emit_hppa_copy(lo(r0), HPPA_REG_RET0, ctx);
/* Restore callee-saved registers. */ for (i = 3; i <= 18; i++) { if (OPTIMIZE_HPPA && !REG_WAS_SEEN(ctx, HPPA_R(i))) continue;
emit(hppa_ldw(-REG_SIZE * (8 + (i-3)), HPPA_REG_SP, HPPA_R(i)), ctx);
}
/* load original return pointer (stored by outest TCC function) */
emit(hppa_ldw(-0x14, HPPA_REG_SP, HPPA_REG_RP), ctx);
emit(hppa_bv(HPPA_REG_ZERO, HPPA_REG_RP, EXEC_NEXT_INSTR), ctx); /* in delay slot: */
emit(hppa_ldw(-0x04, HPPA_REG_SP, HPPA_REG_SP), ctx);
}
/* Note: allocate 2 instructions for jumps if force_far is set. */ if (relative_bits_ok(paoff - HPPA_BRANCH_DISPLACEMENT, 17)) { /* use BL,short branch followed by nop() */
emit(hppa_bl(paoff - HPPA_BRANCH_DISPLACEMENT, HPPA_REG_ZERO), ctx); if (force_far)
emit(hppa_nop(), ctx); return;
}
/* * NO_JUMP skips over the rest of the instructions and the * emit_jump, meaning the BPF branch is not taken. * JUMP skips directly to the emit_jump, meaning * the BPF branch is taken. * * The fallthrough case results in the BPF branch being taken.
*/ #define NO_JUMP(idx) (2 + (idx) - 1) #define JUMP(idx) (0 + (idx) - 1)
e = ctx->ninsns; /* Adjust for extra insns. */
paoff -= (e - s);
emit_jump(paoff, true, ctx); return 0;
}
staticint emit_bcc(u8 op, u8 rd, u8 rs, int paoff, struct hppa_jit_context *ctx)
{ int e, s; bool far = false; int off;
if (op == BPF_JSET) { /* * BPF_JSET is a special case: it has no inverse so we always * treat it as a far branch.
*/
emit(hppa_and(rd, rs, HPPA_REG_T0), ctx);
paoff -= 1; /* reduce offset due to hppa_and() above */
rd = HPPA_REG_T0;
rs = HPPA_REG_ZERO;
op = BPF_JNE;
}
s = ctx->ninsns;
if (!relative_bits_ok(paoff - HPPA_BRANCH_DISPLACEMENT, 12)) {
op = invert_bpf_cond(op);
far = true;
}
/* * For a far branch, the condition is negated and we jump over the * branch itself, and the three instructions from emit_jump. * For a near branch, just use paoff.
*/
off = far ? (HPPA_BRANCH_DISPLACEMENT - 1) : paoff - HPPA_BRANCH_DISPLACEMENT;
switch (op) { /* IF (dst COND src) JUMP off */ case BPF_JEQ:
emit(hppa_beq(rd, rs, off), ctx); break; case BPF_JGT:
emit(hppa_bgtu(rd, rs, off), ctx); break; case BPF_JLT:
emit(hppa_bltu(rd, rs, off), ctx); break; case BPF_JGE:
emit(hppa_bgeu(rd, rs, off), ctx); break; case BPF_JLE:
emit(hppa_bleu(rd, rs, off), ctx); break; case BPF_JNE:
emit(hppa_bne(rd, rs, off), ctx); break; case BPF_JSGT:
emit(hppa_bgt(rd, rs, off), ctx); break; case BPF_JSLT:
emit(hppa_blt(rd, rs, off), ctx); break; case BPF_JSGE:
emit(hppa_bge(rd, rs, off), ctx); break; case BPF_JSLE:
emit(hppa_ble(rd, rs, off), ctx); break; default:
WARN_ON(1);
}
if (far) {
e = ctx->ninsns; /* Adjust for extra insns. */
paoff -= (e - s);
emit_jump(paoff, true, ctx);
} return 0;
}
/* backup TCC */ if (REG_WAS_SEEN(ctx, HPPA_REG_TCC))
emit(hppa_copy(HPPA_REG_TCC, HPPA_REG_TCC_SAVED), ctx);
/* * Use ldil() to load absolute address. Don't use emit_imm as the * number of emitted instructions should not depend on the value of * addr.
*/
emit(hppa_ldil(addr, HPPA_REG_R31), ctx);
emit(hppa_be_l(im11(addr) >> 2, HPPA_REG_R31, EXEC_NEXT_INSTR), ctx); /* set return address in delay slot */
emit_hppa_copy(HPPA_REG_R31, HPPA_REG_RP, ctx);
/* restore TCC */ if (REG_WAS_SEEN(ctx, HPPA_REG_TCC))
emit(hppa_copy(HPPA_REG_TCC_SAVED, HPPA_REG_TCC), ctx);
/* need to calculate address since offset does not fit in 14 bits? */ if (relative_bits_ok(off, 14))
srcreg = lo(rs); else { /* need to use R1 here, since addil puts result into R1 */
srcreg = HPPA_REG_R1;
emit(hppa_addil(off, lo(rs)), ctx);
off = im11(off);
}
/* LDX: dst = *(size *)(src + off) */ switch (size) { case BPF_B:
emit(hppa_ldb(off + 0, srcreg, lo(rd)), ctx); if (!ctx->prog->aux->verifier_zext)
emit_hppa_copy(HPPA_REG_ZERO, hi(rd), ctx); break; case BPF_H:
emit(hppa_ldh(off + 0, srcreg, lo(rd)), ctx); if (!ctx->prog->aux->verifier_zext)
emit_hppa_copy(HPPA_REG_ZERO, hi(rd), ctx); break; case BPF_W:
emit(hppa_ldw(off + 0, srcreg, lo(rd)), ctx); if (!ctx->prog->aux->verifier_zext)
emit_hppa_copy(HPPA_REG_ZERO, hi(rd), ctx); break; case BPF_DW:
emit(hppa_ldw(off + 0, srcreg, hi(rd)), ctx);
emit(hppa_ldw(off + 4, srcreg, lo(rd)), ctx); break;
}
/* need to calculate address since offset does not fit in 14 bits? */ if (relative_bits_ok(off, 14))
dstreg = lo(rd); else { /* need to use R1 here, since addil puts result into R1 */
dstreg = HPPA_REG_R1;
emit(hppa_addil(off, lo(rd)), ctx);
off = im11(off);
}
/* ST: *(size *)(dst + off) = imm */ switch (size) { case BPF_B:
emit(hppa_stb(lo(rs), off + 0, dstreg), ctx); break; case BPF_H:
emit(hppa_sth(lo(rs), off + 0, dstreg), ctx); break; case BPF_W:
emit(hppa_stw(lo(rs), off + 0, dstreg), ctx); break; case BPF_DW:
emit(hppa_stw(hi(rs), off + 0, dstreg), ctx);
emit(hppa_stw(lo(rs), off + 4, dstreg), ctx); break;
}
case BPF_ALU64 | BPF_MOV | BPF_K: case BPF_ALU64 | BPF_AND | BPF_K: case BPF_ALU64 | BPF_OR | BPF_K: case BPF_ALU64 | BPF_XOR | BPF_K: case BPF_ALU64 | BPF_LSH | BPF_K: case BPF_ALU64 | BPF_RSH | BPF_K: case BPF_ALU64 | BPF_ARSH | BPF_K:
emit_alu_i64(dst, imm, ctx, BPF_OP(code)); break;
case BPF_ALU | BPF_MOV | BPF_X: if (imm == 1) { /* Special mov32 for zext. */
emit_zext64(dst, ctx); break;
}
fallthrough; /* dst = dst OP src */ case BPF_ALU | BPF_ADD | BPF_X: case BPF_ALU | BPF_SUB | BPF_X: case BPF_ALU | BPF_AND | BPF_X: case BPF_ALU | BPF_OR | BPF_X: case BPF_ALU | BPF_XOR | BPF_X:
case BPF_ALU | BPF_MUL | BPF_X: case BPF_ALU | BPF_MUL | BPF_K:
case BPF_ALU | BPF_DIV | BPF_X: case BPF_ALU | BPF_DIV | BPF_K:
case BPF_ALU | BPF_MOD | BPF_X: case BPF_ALU | BPF_MOD | BPF_K:
case BPF_ALU | BPF_LSH | BPF_X: case BPF_ALU | BPF_RSH | BPF_X: case BPF_ALU | BPF_ARSH | BPF_X: if (BPF_SRC(code) == BPF_K) {
emit_imm32(tmp2, imm, ctx);
src = tmp2;
}
emit_alu_r32(dst, src, ctx, BPF_OP(code)); break;
/* dst = dst OP imm */ case BPF_ALU | BPF_MOV | BPF_K: case BPF_ALU | BPF_ADD | BPF_K: case BPF_ALU | BPF_SUB | BPF_K: case BPF_ALU | BPF_AND | BPF_K: case BPF_ALU | BPF_OR | BPF_K: case BPF_ALU | BPF_XOR | BPF_K: case BPF_ALU | BPF_LSH | BPF_K: case BPF_ALU | BPF_RSH | BPF_K: case BPF_ALU | BPF_ARSH | BPF_K: /* * mul,div,mod are handled in the BPF_X case.
*/
emit_alu_i32(dst, imm, ctx, BPF_OP(code)); break;
/* dst = -dst */ case BPF_ALU | BPF_NEG: /* * src is ignored---choose tmp2 as a dummy register since it * is not on the stack.
*/
emit_alu_r32(dst, tmp2, ctx, BPF_OP(code)); break;
void bpf_jit_build_prologue(struct hppa_jit_context *ctx)
{ const s8 *tmp = regmap[TMP_REG_1]; const s8 *dst, *reg; int stack_adjust = 0; int i; unsignedlong addr; int bpf_stack_adjust;
/* * stack on hppa grows up, so if tail calls are used we need to * allocate the maximum stack size
*/ if (REG_ALL_SEEN(ctx))
bpf_stack_adjust = MAX_BPF_STACK; else
bpf_stack_adjust = ctx->prog->aux->stack_depth;
bpf_stack_adjust = round_up(bpf_stack_adjust, STACK_ALIGN);
/* make space for callee-saved registers. */
stack_adjust += NR_SAVED_REGISTERS * REG_SIZE; /* make space for BPF registers on stack. */
stack_adjust += BPF_JIT_SCRATCH_REGS * REG_SIZE; /* make space for BPF stack. */
stack_adjust += bpf_stack_adjust; /* round up for stack alignment. */
stack_adjust = round_up(stack_adjust, STACK_ALIGN);
/* * The first instruction sets the tail-call-counter (TCC) register. * This instruction is skipped by tail calls. * Use a temporary register instead of a caller-saved register initially.
*/
emit(hppa_ldi(MAX_TAIL_CALL_CNT, HPPA_REG_TCC_IN_INIT), ctx);
/* * skip all initializations when called as BPF TAIL call.
*/
emit(hppa_ldi(MAX_TAIL_CALL_CNT, HPPA_REG_R1), ctx);
emit(hppa_bne(HPPA_REG_TCC_IN_INIT, HPPA_REG_R1, ctx->prologue_len - 2 - HPPA_BRANCH_DISPLACEMENT), ctx);
/* save callee-save registers. */ for (i = 3; i <= 18; i++) { if (OPTIMIZE_HPPA && !REG_WAS_SEEN(ctx, HPPA_R(i))) continue;
emit(hppa_stw(HPPA_R(i), -REG_SIZE * (8 + (i-3)), HPPA_REG_SP), ctx); // stw ri,-save_area(sp)
}
/* * now really set the tail call counter (TCC) register.
*/ if (REG_WAS_SEEN(ctx, HPPA_REG_TCC))
emit(hppa_ldi(MAX_TAIL_CALL_CNT, HPPA_REG_TCC), ctx);
/* * save epilogue function pointer for outer TCC call chain. * The main TCC call stores the final RP on stack.
*/
addr = (uintptr_t) &ctx->insns[ctx->epilogue_offset]; /* skip first two instructions of exit function, which jump to exit */
addr += 2 * HPPA_INSN_SIZE;
emit(hppa_ldil(addr, HPPA_REG_T2), ctx);
emit(hppa_ldo(im11(addr), HPPA_REG_T2, HPPA_REG_T2), ctx);
emit(EXIT_PTR_STORE(HPPA_REG_T2), ctx);
/* load R1 & R2 from registers, R3-R5 from stack. */ /* use HPPA_REG_R1 which holds the old stack value */
dst = regmap[BPF_REG_5];
reg = bpf_get_reg64_ref(dst, tmp, false, ctx); if (REG_WAS_SEEN(ctx, lo(reg)) | REG_WAS_SEEN(ctx, hi(reg))) { if (REG_WAS_SEEN(ctx, hi(reg)))
emit(hppa_ldw(-0x48, HPPA_REG_R1, hi(reg)), ctx); if (REG_WAS_SEEN(ctx, lo(reg)))
emit(hppa_ldw(-0x44, HPPA_REG_R1, lo(reg)), ctx);
bpf_put_reg64(dst, tmp, ctx);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.