#include <linux/math64.h>
#include <linux/errno.h>
#include <linux/filter.h>
#include <linux/bpf.h>
#include <asm/cpu-features.h>
#include <asm/isa-rev.h>
#include <asm/uasm.h>
#include "bpf_jit_comp.h"
#undef MIPS_R_A4
#undef MIPS_R_A5
#undef MIPS_R_A6
#undef MIPS_R_A7
#define MIPS_STACK_ALIGNMENT 8
#define JIT_RESERVED_STACK 16
#define JIT_REG_TMP MAX_BPF_JIT_REG
#ifdef __BIG_ENDIAN
#define JIT_TCALL_SKIP 12
#else
#define JIT_TCALL_SKIP 8
#endif
#define JIT_RETURN_REGS \
(BIT(MIPS_R_V0) | \
BIT(MIPS_R_V1))
#define JIT_ARG_REGS \
(BIT(MIPS_R_A0) | \
BIT(MIPS_R_A1) | \
BIT(MIPS_R_A2) | \
BIT(MIPS_R_A3))
#define JIT_STACK_REGS \
(BIT(MIPS_R_T0) | \
BIT(MIPS_R_T1) | \
BIT(MIPS_R_T2) | \
BIT(MIPS_R_T3) | \
BIT(MIPS_R_T4) | \
BIT(MIPS_R_T5))
#define JIT_CALLER_REGS \
(JIT_RETURN_REGS | \
JIT_ARG_REGS | \
JIT_STACK_REGS)
#define JIT_CALLEE_REGS \
(BIT(MIPS_R_S0) | \
BIT(MIPS_R_S1) | \
BIT(MIPS_R_S2) | \
BIT(MIPS_R_S3) | \
BIT(MIPS_R_S4) | \
BIT(MIPS_R_S5) | \
BIT(MIPS_R_S6) | \
BIT(MIPS_R_S7) | \
BIT(MIPS_R_GP) | \
BIT(MIPS_R_FP) | \
BIT(MIPS_R_RA))
static const u8 bpf2mips32[][2] = {
[BPF_REG_0] = {MIPS_R_V1, MIPS_R_V0},
[BPF_REG_1] = {MIPS_R_A1, MIPS_R_A0},
[BPF_REG_2] = {MIPS_R_A3, MIPS_R_A2},
[BPF_REG_3] = {MIPS_R_T1, MIPS_R_T0},
[BPF_REG_4] = {MIPS_R_T3, MIPS_R_T2},
[BPF_REG_5] = {MIPS_R_T5, MIPS_R_T4},
[BPF_REG_6] = {MIPS_R_S1, MIPS_R_S0},
[BPF_REG_7] = {MIPS_R_S3, MIPS_R_S2},
[BPF_REG_8] = {MIPS_R_S5, MIPS_R_S4},
[BPF_REG_9] = {MIPS_R_S7, MIPS_R_S6},
#ifdef __BIG_ENDIAN
[BPF_REG_FP] = {MIPS_R_FP, MIPS_R_ZERO},
#else
[BPF_REG_FP] = {MIPS_R_ZERO, MIPS_R_FP},
#endif
[BPF_REG_AX] = {MIPS_R_GP, MIPS_R_AT},
[JIT_REG_TMP] = {MIPS_R_T7, MIPS_R_T6},
};
static inline u8 lo(const u8 reg[])
{
#ifdef __BIG_ENDIAN
return reg[0];
#else
return reg[1];
#endif
}
static inline u8 hi(const u8 reg[])
{
#ifdef __BIG_ENDIAN
return reg[1];
#else
return reg[0];
#endif
}
static void clobber_reg64(struct jit_context *ctx, const u8 reg[])
{
clobber_reg(ctx, reg[0]);
clobber_reg(ctx, reg[1]);
}
static void emit_mov_se_i64(struct jit_context *ctx, const u8 dst[], s32 imm)
{
emit_mov_i(ctx, lo(dst), imm);
if (imm < 0)
emit(ctx, addiu, hi(dst), MIPS_R_ZERO, -1);
else
emit(ctx, move, hi(dst), MIPS_R_ZERO);
clobber_reg64(ctx, dst);
}
static void emit_zext_ver(struct jit_context *ctx, const u8 dst[])
{
if (!ctx->program->aux->verifier_zext) {
emit(ctx, move, hi(dst), MIPS_R_ZERO);
clobber_reg(ctx, hi(dst));
}
}
static void emit_load_delay(struct jit_context *ctx)
{
if (!cpu_has_mips_2_3_4_5_r)
emit(ctx, nop);
}
static void emit_alu_i64(struct jit_context *ctx,
const u8 dst[], s32 imm, u8 op)
{
u8 src = MIPS_R_T6;
if (imm > S32_MIN && imm < 0)
switch (op) {
case BPF_ADD:
op = BPF_SUB;
imm = -imm;
break;
case BPF_SUB:
op = BPF_ADD;
imm = -imm;
break;
}
emit_mov_i(ctx, src, imm);
switch (op) {
case BPF_ADD:
emit(ctx, addu, lo(dst), lo(dst), src);
emit(ctx, sltu, MIPS_R_T9, lo(dst), src);
emit(ctx, addu, hi(dst), hi(dst), MIPS_R_T9);
if (imm < 0)
emit(ctx, addiu, hi(dst), hi(dst), -1);
break;
case BPF_SUB:
emit(ctx, sltu, MIPS_R_T9, lo(dst), src);
emit(ctx, subu, lo(dst), lo(dst), src);
emit(ctx, subu, hi(dst), hi(dst), MIPS_R_T9);
if (imm < 0)
emit(ctx, addiu, hi(dst), hi(dst), 1);
break;
case BPF_OR:
emit(ctx, or, lo(dst), lo(dst), src);
if (imm < 0)
emit(ctx, addiu, hi(dst), MIPS_R_ZERO, -1);
break;
case BPF_AND:
emit(ctx, and, lo(dst), lo(dst), src);
if (imm >= 0)
emit(ctx, move, hi(dst), MIPS_R_ZERO);
break;
case BPF_XOR:
emit(ctx, xor, lo(dst), lo(dst), src);
if (imm < 0) {
emit(ctx, subu, hi(dst), MIPS_R_ZERO, hi(dst));
emit(ctx, addiu, hi(dst), hi(dst), -1);
}
break;
}
clobber_reg64(ctx, dst);
}
static void emit_alu_r64(struct jit_context *ctx,
const u8 dst[], const u8 src[], u8 op)
{
switch (BPF_OP(op)) {
case BPF_ADD:
if (src == dst) {
emit(ctx, srl, MIPS_R_T9, lo(dst), 31);
emit(ctx, addu, lo(dst), lo(dst), lo(dst));
} else {
emit(ctx, addu, lo(dst), lo(dst), lo(src));
emit(ctx, sltu, MIPS_R_T9, lo(dst), lo(src));
}
emit(ctx, addu, hi(dst), hi(dst), hi(src));
emit(ctx, addu, hi(dst), hi(dst), MIPS_R_T9);
break;
case BPF_SUB:
emit(ctx, sltu, MIPS_R_T9, lo(dst), lo(src));
emit(ctx, subu, lo(dst), lo(dst), lo(src));
emit(ctx, subu, hi(dst), hi(dst), hi(src));
emit(ctx, subu, hi(dst), hi(dst), MIPS_R_T9);
break;
case BPF_OR:
emit(ctx, or, lo(dst), lo(dst), lo(src));
emit(ctx, or, hi(dst), hi(dst), hi(src));
break;
case BPF_AND:
emit(ctx, and, lo(dst), lo(dst), lo(src));
emit(ctx, and, hi(dst), hi(dst), hi(src));
break;
case BPF_XOR:
emit(ctx, xor, lo(dst), lo(dst), lo(src));
emit(ctx, xor, hi(dst), hi(dst), hi(src));
break;
}
clobber_reg64(ctx, dst);
}
static void emit_neg_i64(struct jit_context *ctx, const u8 dst[])
{
emit(ctx, sltu, MIPS_R_T9, MIPS_R_ZERO, lo(dst));
emit(ctx, subu, lo(dst), MIPS_R_ZERO, lo(dst));
emit(ctx, subu, hi(dst), MIPS_R_ZERO, hi(dst));
emit(ctx, subu, hi(dst), hi(dst), MIPS_R_T9);
clobber_reg64(ctx, dst);
}
static void emit_shift_i64(struct jit_context *ctx,
const u8 dst[], u32 imm, u8 op)
{
switch (BPF_OP(op)) {
case BPF_LSH:
if (imm < 32) {
emit(ctx, srl, MIPS_R_T9, lo(dst), 32 - imm);
emit(ctx, sll, lo(dst), lo(dst), imm);
emit(ctx, sll, hi(dst), hi(dst), imm);
emit(ctx, or, hi(dst), hi(dst), MIPS_R_T9);
} else {
emit(ctx, sll, hi(dst), lo(dst), imm - 32);
emit(ctx, move, lo(dst), MIPS_R_ZERO);
}
break;
case BPF_RSH:
if (imm < 32) {
emit(ctx, sll, MIPS_R_T9, hi(dst), 32 - imm);
emit(ctx, srl, lo(dst), lo(dst), imm);
emit(ctx, srl, hi(dst), hi(dst), imm);
emit(ctx, or, lo(dst), lo(dst), MIPS_R_T9);
} else {
emit(ctx, srl, lo(dst), hi(dst), imm - 32);
emit(ctx, move, hi(dst), MIPS_R_ZERO);
}
break;
case BPF_ARSH:
if (imm < 32) {
emit(ctx, sll, MIPS_R_T9, hi(dst), 32 - imm);
emit(ctx, srl, lo(dst), lo(dst), imm);
emit(ctx, sra, hi(dst), hi(dst), imm);
emit(ctx, or, lo(dst), lo(dst), MIPS_R_T9);
} else {
emit(ctx, sra, lo(dst), hi(dst), imm - 32);
emit(ctx, sra, hi(dst), hi(dst), 31);
}
break;
}
clobber_reg64(ctx, dst);
}
static void emit_shift_r64(struct jit_context *ctx,
const u8 dst[], u8 src, u8 op)
{
u8 t1 = MIPS_R_T8;
u8 t2 = MIPS_R_T9;
emit(ctx, andi, t1, src, 32);
emit(ctx, beqz, t1, 16);
emit(ctx, nor, t2, src, MIPS_R_ZERO);
switch (BPF_OP(op)) {
case BPF_LSH:
emit(ctx, sllv, hi(dst), lo(dst), src);
emit(ctx, move, lo(dst), MIPS_R_ZERO);
emit(ctx, b, 20);
emit(ctx, srl, t1, lo(dst), 1);
emit(ctx, srlv, t1, t1, t2);
emit(ctx, sllv, lo(dst), lo(dst), src);
emit(ctx, sllv, hi(dst), hi(dst), src);
emit(ctx, or, hi(dst), hi(dst), t1);
break;
case BPF_RSH:
emit(ctx, srlv, lo(dst), hi(dst), src);
emit(ctx, move, hi(dst), MIPS_R_ZERO);
emit(ctx, b, 20);
emit(ctx, sll, t1, hi(dst), 1);
emit(ctx, sllv, t1, t1, t2);
emit(ctx, srlv, lo(dst), lo(dst), src);
emit(ctx, srlv, hi(dst), hi(dst), src);
emit(ctx, or, lo(dst), lo(dst), t1);
break;
case BPF_ARSH:
emit(ctx, srav, lo(dst), hi(dst), src);
emit(ctx, sra, hi(dst), hi(dst), 31);
emit(ctx, b, 20);
emit(ctx, sll, t1, hi(dst), 1);
emit(ctx, sllv, t1, t1, t2);
emit(ctx, srlv, lo(dst), lo(dst), src);
emit(ctx, srav, hi(dst), hi(dst), src);
emit(ctx, or, lo(dst), lo(dst), t1);
break;
}
clobber_reg64(ctx, dst);
}
static void emit_mul_i64(struct jit_context *ctx, const u8 dst[], s32 imm)
{
u8 src = MIPS_R_T6;
u8 tmp = MIPS_R_T9;
switch (imm) {
case 1:
break;
case -1:
emit_neg_i64(ctx, dst);
break;
case 0:
emit_mov_r(ctx, lo(dst), MIPS_R_ZERO);
emit_mov_r(ctx, hi(dst), MIPS_R_ZERO);
break;
default:
emit_mov_i(ctx, src, imm);
if (cpu_has_mips32r1 || cpu_has_mips32r6) {
emit(ctx, mul, hi(dst), hi(dst), src);
} else {
emit(ctx, multu, hi(dst), src);
emit(ctx, mflo, hi(dst));
}
if (imm < 0)
emit(ctx, subu, hi(dst), hi(dst), lo(dst));
if (cpu_has_mips32r6) {
emit(ctx, muhu, tmp, lo(dst), src);
emit(ctx, mulu, lo(dst), lo(dst), src);
} else {
emit(ctx, multu, lo(dst), src);
emit(ctx, mflo, lo(dst));
emit(ctx, mfhi, tmp);
}
emit(ctx, addu, hi(dst), hi(dst), tmp);
clobber_reg64(ctx, dst);
break;
}
}
static void emit_mul_r64(struct jit_context *ctx,
const u8 dst[], const u8 src[])
{
u8 acc = MIPS_R_T8;
u8 tmp = MIPS_R_T9;
if (cpu_has_mips32r1 || cpu_has_mips32r6) {
emit(ctx, mul, acc, hi(dst), lo(src));
} else {
emit(ctx, multu, hi(dst), lo(src));
emit(ctx, mflo, acc);
}
if (cpu_has_mips32r1 || cpu_has_mips32r6) {
emit(ctx, mul, tmp, lo(dst), hi(src));
} else {
emit(ctx, multu, lo(dst), hi(src));
emit(ctx, mflo, tmp);
}
emit(ctx, addu, acc, acc, tmp);
if (cpu_has_mips32r6) {
emit(ctx, muhu, tmp, lo(dst), lo(src));
emit(ctx, mulu, lo(dst), lo(dst), lo(src));
} else {
emit(ctx, multu, lo(dst), lo(src));
emit(ctx, mflo, lo(dst));
emit(ctx, mfhi, tmp);
}
emit(ctx, addu, hi(dst), acc, tmp);
clobber_reg64(ctx, dst);
}
static u64 jit_mod64(u64 a, u64 b)
{
u64 rem;
div64_u64_rem(a, b, &rem);
return rem;
}
static void emit_divmod_r64(struct jit_context *ctx,
const u8 dst[], const u8 src[], u8 op)
{
const u8 *r0 = bpf2mips32[BPF_REG_0];
const u8 *r1 = bpf2mips32[BPF_REG_1];
const u8 *r2 = bpf2mips32[BPF_REG_2];
int exclude, k;
u32 addr = 0;
push_regs(ctx, ctx->clobbered & JIT_CALLER_REGS,
0, JIT_RESERVED_STACK);
for (k = 0; k < 2; k++) {
emit(ctx, move, MIPS_R_T9, src[k]);
emit(ctx, move, r1[k], dst[k]);
emit(ctx, move, r2[k], MIPS_R_T9);
}
switch (BPF_OP(op)) {
case BPF_DIV:
addr = (u32)&div64_u64;
break;
case BPF_MOD:
addr = (u32)&jit_mod64;
break;
}
emit_mov_i(ctx, MIPS_R_T9, addr);
emit(ctx, jalr, MIPS_R_RA, MIPS_R_T9);
emit(ctx, nop);
emit(ctx, move, dst[0], r0[0]);
emit(ctx, move, dst[1], r0[1]);
exclude = BIT(lo(dst)) | BIT(hi(dst));
pop_regs(ctx, ctx->clobbered & JIT_CALLER_REGS,
exclude, JIT_RESERVED_STACK);
emit_load_delay(ctx);
clobber_reg64(ctx, dst);
clobber_reg(ctx, MIPS_R_V0);
clobber_reg(ctx, MIPS_R_V1);
clobber_reg(ctx, MIPS_R_RA);
}
static void emit_swap8_r(struct jit_context *ctx, u8 dst, u8 src, u8 mask)
{
u8 tmp = MIPS_R_T9;
emit(ctx, and, tmp, src, mask);
emit(ctx, sll, tmp, tmp, 8);
emit(ctx, srl, dst, src, 8);
emit(ctx, and, dst, dst, mask);
emit(ctx, or, dst, dst, tmp);
}
static void emit_swap16_r(struct jit_context *ctx, u8 dst, u8 src)
{
u8 tmp = MIPS_R_T9;
emit(ctx, sll, tmp, src, 16);
emit(ctx, srl, dst, src, 16);
emit(ctx, or, dst, dst, tmp);
}
static void emit_bswap_r64(struct jit_context *ctx, const u8 dst[], u32 width)
{
u8 tmp = MIPS_R_T8;
switch (width) {
case 64:
if (cpu_has_mips32r2 || cpu_has_mips32r6) {
emit(ctx, rotr, tmp, hi(dst), 16);
emit(ctx, rotr, hi(dst), lo(dst), 16);
emit(ctx, wsbh, lo(dst), tmp);
emit(ctx, wsbh, hi(dst), hi(dst));
} else {
emit_swap16_r(ctx, tmp, lo(dst));
emit_swap16_r(ctx, lo(dst), hi(dst));
emit(ctx, move, hi(dst), tmp);
emit(ctx, lui, tmp, 0xff);
emit(ctx, ori, tmp, tmp, 0xff);
emit_swap8_r(ctx, lo(dst), lo(dst), tmp);
emit_swap8_r(ctx, hi(dst), hi(dst), tmp);
}
break;
case 32:
case 16:
emit_bswap_r(ctx, lo(dst), width);
emit(ctx, move, hi(dst), MIPS_R_ZERO);
break;
}
clobber_reg64(ctx, dst);
}
static void emit_trunc_r64(struct jit_context *ctx, const u8 dst[], u32 width)
{
switch (width) {
case 64:
break;
case 32:
emit(ctx, move, hi(dst), MIPS_R_ZERO);
clobber_reg(ctx, hi(dst));
break;
case 16:
emit(ctx, move, hi(dst), MIPS_R_ZERO);
emit(ctx, andi, lo(dst), lo(dst), 0xffff);
clobber_reg64(ctx, dst);
break;
}
}
static void emit_ldx(struct jit_context *ctx,
const u8 dst[], u8 src, s16 off, u8 size)
{
switch (size) {
case BPF_B:
emit(ctx, lbu, lo(dst), off, src);
emit(ctx, move, hi(dst), MIPS_R_ZERO);
break;
case BPF_H:
emit(ctx, lhu, lo(dst), off, src);
emit(ctx, move, hi(dst), MIPS_R_ZERO);
break;
case BPF_W:
emit(ctx, lw, lo(dst), off, src);
emit(ctx, move, hi(dst), MIPS_R_ZERO);
break;
case BPF_DW:
if (dst[1] == src) {
emit(ctx, lw, dst[0], off + 4, src);
emit(ctx, lw, dst[1], off, src);
} else {
emit(ctx, lw, dst[1], off, src);
emit(ctx, lw, dst[0], off + 4, src);
}
emit_load_delay(ctx);
break;
}
clobber_reg64(ctx, dst);
}
static void emit_stx(struct jit_context *ctx,
const u8 dst, const u8 src[], s16 off, u8 size)
{
switch (size) {
case BPF_B:
emit(ctx, sb, lo(src), off, dst);
break;
case BPF_H:
emit(ctx, sh, lo(src), off, dst);
break;
case BPF_W:
emit(ctx, sw, lo(src), off, dst);
break;
case BPF_DW:
emit(ctx, sw, src[1], off, dst);
emit(ctx, sw, src[0], off + 4, dst);
break;
}
}
static void emit_atomic_r32(struct jit_context *ctx,
u8 dst, u8 src, s16 off, u8 code)
{
u32 exclude = 0;
u32 addr = 0;
push_regs(ctx, ctx->clobbered & JIT_CALLER_REGS,
0, JIT_RESERVED_STACK);
emit(ctx, move, MIPS_R_T9, dst);
if (code == BPF_XCHG) {
emit(ctx, move, MIPS_R_A1, src);
emit(ctx, addiu, MIPS_R_A0, MIPS_R_T9, off);
} else {
emit(ctx, move, MIPS_R_A0, src);
emit(ctx, addiu, MIPS_R_A1, MIPS_R_T9, off);
}
switch (code) {
case BPF_ADD:
addr = (u32)&atomic_add;
break;
case BPF_ADD | BPF_FETCH:
addr = (u32)&atomic_fetch_add;
break;
case BPF_SUB:
addr = (u32)&atomic_sub;
break;
case BPF_SUB | BPF_FETCH:
addr = (u32)&atomic_fetch_sub;
break;
case BPF_OR:
addr = (u32)&atomic_or;
break;
case BPF_OR | BPF_FETCH:
addr = (u32)&atomic_fetch_or;
break;
case BPF_AND:
addr = (u32)&atomic_and;
break;
case BPF_AND | BPF_FETCH:
addr = (u32)&atomic_fetch_and;
break;
case BPF_XOR:
addr = (u32)&atomic_xor;
break;
case BPF_XOR | BPF_FETCH:
addr = (u32)&atomic_fetch_xor;
break;
case BPF_XCHG:
addr = (u32)&atomic_xchg;
break;
}
emit_mov_i(ctx, MIPS_R_T9, addr);
emit(ctx, jalr, MIPS_R_RA, MIPS_R_T9);
emit(ctx, nop);
if (code & BPF_FETCH) {
emit(ctx, move, src, MIPS_R_V0);
exclude = BIT(src);
clobber_reg(ctx, src);
}
pop_regs(ctx, ctx->clobbered & JIT_CALLER_REGS,
exclude, JIT_RESERVED_STACK);
emit_load_delay(ctx);
clobber_reg(ctx, MIPS_R_RA);
}
static s64 jit_xchg64(s64 a, atomic64_t *v)
{
return atomic64_xchg(v, a);
}
static void emit_atomic_r64(struct jit_context *ctx,
u8 dst, const u8 src[], s16 off, u8 code)
{
const u8 *r0 = bpf2mips32[BPF_REG_0];
const u8 *r1 = bpf2mips32[BPF_REG_1];
u32 exclude = 0;
u32 addr = 0;
push_regs(ctx, ctx->clobbered & JIT_CALLER_REGS,
0, JIT_RESERVED_STACK);
emit(ctx, move, MIPS_R_T9, dst);
emit(ctx, move, r1[0], src[0]);
emit(ctx, move, r1[1], src[1]);
emit(ctx, addiu, MIPS_R_A2, MIPS_R_T9, off);
switch (code) {
case BPF_ADD:
addr = (u32)&atomic64_add;
break;
case BPF_ADD | BPF_FETCH:
addr = (u32)&atomic64_fetch_add;
break;
case BPF_SUB:
addr = (u32)&atomic64_sub;
break;
case BPF_SUB | BPF_FETCH:
addr = (u32)&atomic64_fetch_sub;
break;
case BPF_OR:
addr = (u32)&atomic64_or;
break;
case BPF_OR | BPF_FETCH:
addr = (u32)&atomic64_fetch_or;
break;
case BPF_AND:
addr = (u32)&atomic64_and;
break;
case BPF_AND | BPF_FETCH:
addr = (u32)&atomic64_fetch_and;
break;
case BPF_XOR:
addr = (u32)&atomic64_xor;
break;
case BPF_XOR | BPF_FETCH:
addr = (u32)&atomic64_fetch_xor;
break;
case BPF_XCHG:
addr = (u32)&jit_xchg64;
break;
}
emit_mov_i(ctx, MIPS_R_T9, addr);
emit(ctx, jalr, MIPS_R_RA, MIPS_R_T9);
emit(ctx, nop);
if (code & BPF_FETCH) {
emit(ctx, move, lo(src), lo(r0));
emit(ctx, move, hi(src), hi(r0));
exclude = BIT(src[0]) | BIT(src[1]);
clobber_reg64(ctx, src);
}
pop_regs(ctx, ctx->clobbered & JIT_CALLER_REGS,
exclude, JIT_RESERVED_STACK);
emit_load_delay(ctx);
clobber_reg(ctx, MIPS_R_RA);
}
static void emit_cmpxchg_r32(struct jit_context *ctx, u8 dst, u8 src, s16 off)
{
const u8 *r0 = bpf2mips32[BPF_REG_0];
push_regs(ctx, ctx->clobbered & JIT_CALLER_REGS,
JIT_RETURN_REGS, JIT_RESERVED_STACK + 2 * sizeof(u32));
emit(ctx, addiu, MIPS_R_T9, dst, off);
emit(ctx, move, MIPS_R_T8, src);
emit(ctx, move, MIPS_R_A1, lo(r0));
emit(ctx, move, MIPS_R_A0, MIPS_R_T9);
emit(ctx, move, MIPS_R_A2, MIPS_R_T8);
emit_mov_i(ctx, MIPS_R_T9, (u32)&atomic_cmpxchg);
emit(ctx, jalr, MIPS_R_RA, MIPS_R_T9);
emit(ctx, nop);
#ifdef __BIG_ENDIAN
emit(ctx, move, lo(r0), MIPS_R_V0);
#endif
pop_regs(ctx, ctx->clobbered & JIT_CALLER_REGS,
JIT_RETURN_REGS, JIT_RESERVED_STACK + 2 * sizeof(u32));
emit_load_delay(ctx);
clobber_reg(ctx, MIPS_R_V0);
clobber_reg(ctx, MIPS_R_V1);
clobber_reg(ctx, MIPS_R_RA);
}
static void emit_cmpxchg_r64(struct jit_context *ctx,
u8 dst, const u8 src[], s16 off)
{
const u8 *r0 = bpf2mips32[BPF_REG_0];
const u8 *r2 = bpf2mips32[BPF_REG_2];
push_regs(ctx, ctx->clobbered & JIT_CALLER_REGS,
JIT_RETURN_REGS, JIT_RESERVED_STACK + 2 * sizeof(u32));
push_regs(ctx, BIT(src[0]) | BIT(src[1]), 0, JIT_RESERVED_STACK);
emit(ctx, addiu, MIPS_R_T9, dst, off);
emit(ctx, move, r2[0], r0[0]);
emit(ctx, move, r2[1], r0[1]);
emit(ctx, move, MIPS_R_A0, MIPS_R_T9);
emit_mov_i(ctx, MIPS_R_T9, (u32)&atomic64_cmpxchg);
emit(ctx, jalr, MIPS_R_RA, MIPS_R_T9);
emit(ctx, nop);
pop_regs(ctx, ctx->clobbered & JIT_CALLER_REGS,
JIT_RETURN_REGS, JIT_RESERVED_STACK + 2 * sizeof(u32));
emit_load_delay(ctx);
clobber_reg(ctx, MIPS_R_V0);
clobber_reg(ctx, MIPS_R_V1);
clobber_reg(ctx, MIPS_R_RA);
}
static void emit_movz_r(struct jit_context *ctx, u8 rd, u8 rs, u8 rt)
{
if (cpu_has_mips_2) {
emit(ctx, movz, rd, rs, rt);
} else if (cpu_has_mips32r6) {
if (rs != MIPS_R_ZERO)
emit(ctx, seleqz, rs, rs, rt);
emit(ctx, selnez, rd, rd, rt);
if (rs != MIPS_R_ZERO)
emit(ctx, or, rd, rd, rs);
} else {
emit(ctx, bnez, rt, 8);
emit(ctx, nop);
emit(ctx, or, rd, rs, MIPS_R_ZERO);
}
clobber_reg(ctx, rd);
clobber_reg(ctx, rs);
}
static void emit_movn_r(struct jit_context *ctx, u8 rd, u8 rs, u8 rt)
{
if (cpu_has_mips_2) {
emit(ctx, movn, rd, rs, rt);
} else if (cpu_has_mips32r6) {
if (rs != MIPS_R_ZERO)
emit(ctx, selnez, rs, rs, rt);
emit(ctx, seleqz, rd, rd, rt);
if (rs != MIPS_R_ZERO)
emit(ctx, or, rd, rd, rs);
} else {
emit(ctx, beqz, rt, 8);
emit(ctx, nop);
emit(ctx, or, rd, rs, MIPS_R_ZERO);
}
clobber_reg(ctx, rd);
clobber_reg(ctx, rs);
}
static void emit_sltiu_r64(struct jit_context *ctx, u8 rd,
const u8 rs[], s64 imm)
{
u8 tmp = MIPS_R_T9;
if (imm < 0) {
emit_mov_i(ctx, rd, imm);
emit(ctx, sltu, rd, lo(rs), rd);
emit(ctx, sltiu, tmp, hi(rs), -1);
emit(ctx, or, rd, rd, tmp);
} else {
if (imm > 0x7fff) {
emit_mov_i(ctx, rd, (s32)imm);
emit(ctx, sltu, rd, lo(rs), rd);
} else {
emit(ctx, sltiu, rd, lo(rs), imm);
}
emit_movn_r(ctx, rd, MIPS_R_ZERO, hi(rs));
}
}
static void emit_sltu_r64(struct jit_context *ctx, u8 rd,
const u8 rs[], const u8 rt[])
{
u8 tmp = MIPS_R_T9;
emit(ctx, sltu, rd, lo(rs), lo(rt));
emit(ctx, subu, tmp, hi(rs), hi(rt));
emit_movn_r(ctx, rd, MIPS_R_ZERO, tmp);
emit(ctx, sltu, tmp, hi(rs), hi(rt));
emit(ctx, or, rd, rd, tmp);
}
static void emit_slti_r64(struct jit_context *ctx, u8 rd,
const u8 rs[], s64 imm)
{
u8 t1 = MIPS_R_T8;
u8 t2 = MIPS_R_T9;
u8 cmp;
emit_mov_i(ctx, rd, (s32)imm);
emit(ctx, sltu, t1, lo(rs), rd);
emit(ctx, sltu, t2, rd, lo(rs));
emit(ctx, srl, rd, hi(rs), 31);
if (imm < 0)
emit_movz_r(ctx, t1, t2, rd);
else
emit_movn_r(ctx, t1, t2, rd);
if (imm < 0) {
emit(ctx, addiu, rd, hi(rs), 1);
cmp = rd;
} else {
cmp = hi(rs);
}
emit_movn_r(ctx, t1, MIPS_R_ZERO, cmp);
emit(ctx, slti, rd, hi(rs), imm < 0 ? -1 : 0);
emit(ctx, or, rd, rd, t1);
}
static void emit_slt_r64(struct jit_context *ctx, u8 rd,
const u8 rs[], const u8 rt[])
{
u8 t1 = MIPS_R_T7;
u8 t2 = MIPS_R_T8;
u8 t3 = MIPS_R_T9;
emit(ctx, sltu, t1, lo(rs), lo(rt));
emit(ctx, sltu, t2, lo(rt), lo(rs));
emit(ctx, xor, t3, hi(rs), hi(rt));
emit(ctx, srl, rd, t3, 31);
emit_movn_r(ctx, t1, t2, rd);
emit_movn_r(ctx, t1, MIPS_R_ZERO, t3);
emit(ctx, slt, rd, hi(rs), hi(rt));
emit(ctx, or, rd, rd, t1);
}
static void emit_jmp_i64(struct jit_context *ctx,
const u8 dst[], s32 imm, s32 off, u8 op)
{
u8 tmp = MIPS_R_T6;
switch (op) {
case JIT_JNOP:
break;
case BPF_JEQ:
case BPF_JNE:
if (imm >= -0x7fff && imm <= 0x8000) {
emit(ctx, addiu, tmp, lo(dst), -imm);
} else if ((u32)imm <= 0xffff) {
emit(ctx, xori, tmp, lo(dst), imm);
} else {
emit_mov_i(ctx, tmp, imm);
emit(ctx, xor, tmp, lo(dst), tmp);
}
if (imm < 0) {
emit(ctx, addu, MIPS_R_T9, hi(dst), 1);
emit(ctx, or, tmp, tmp, MIPS_R_T9);
} else {
emit(ctx, or, tmp, tmp, hi(dst));
}
if (op == BPF_JEQ)
emit(ctx, beqz, tmp, off);
else
emit(ctx, bnez, tmp, off);
break;
case BPF_JSET:
case JIT_JNSET:
if ((u32)imm <= 0xffff) {
emit(ctx, andi, tmp, lo(dst), imm);
} else {
emit_mov_i(ctx, tmp, imm);
emit(ctx, and, tmp, lo(dst), tmp);
}
if (imm < 0)
emit(ctx, or, tmp, tmp, hi(dst));
if (op == BPF_JSET)
emit(ctx, bnez, tmp, off);
else
emit(ctx, beqz, tmp, off);
break;
case BPF_JGT:
emit_sltiu_r64(ctx, tmp, dst, (s64)imm + 1);
emit(ctx, beqz, tmp, off);
break;
case BPF_JGE:
emit_sltiu_r64(ctx, tmp, dst, imm);
emit(ctx, beqz, tmp, off);
break;
case BPF_JLT:
emit_sltiu_r64(ctx, tmp, dst, imm);
emit(ctx, bnez, tmp, off);
break;
case BPF_JLE:
emit_sltiu_r64(ctx, tmp, dst, (s64)imm + 1);
emit(ctx, bnez, tmp, off);
break;
case BPF_JSGT:
emit_slti_r64(ctx, tmp, dst, (s64)imm + 1);
emit(ctx, beqz, tmp, off);
break;
case BPF_JSGE:
emit_slti_r64(ctx, tmp, dst, imm);
emit(ctx, beqz, tmp, off);
break;
case BPF_JSLT:
emit_slti_r64(ctx, tmp, dst, imm);
emit(ctx, bnez, tmp, off);
break;
case BPF_JSLE:
emit_slti_r64(ctx, tmp, dst, (s64)imm + 1);
emit(ctx, bnez, tmp, off);
break;
}
}
static void emit_jmp_r64(struct jit_context *ctx,
const u8 dst[], const u8 src[], s32 off, u8 op)
{
u8 t1 = MIPS_R_T6;
u8 t2 = MIPS_R_T7;
switch (op) {
case JIT_JNOP:
break;
case BPF_JEQ:
case BPF_JNE:
emit(ctx, subu, t1, lo(dst), lo(src));
emit(ctx, subu, t2, hi(dst), hi(src));
emit(ctx, or, t1, t1, t2);
if (op == BPF_JEQ)
emit(ctx, beqz, t1, off);
else
emit(ctx, bnez, t1, off);
break;
case BPF_JSET:
case JIT_JNSET:
emit(ctx, and, t1, lo(dst), lo(src));
emit(ctx, and, t2, hi(dst), hi(src));
emit(ctx, or, t1, t1, t2);
if (op == BPF_JSET)
emit(ctx, bnez, t1, off);
else
emit(ctx, beqz, t1, off);
break;
case BPF_JGT:
emit_sltu_r64(ctx, t1, src, dst);
emit(ctx, bnez, t1, off);
break;
case BPF_JGE:
emit_sltu_r64(ctx, t1, dst, src);
emit(ctx, beqz, t1, off);
break;
case BPF_JLT:
emit_sltu_r64(ctx, t1, dst, src);
emit(ctx, bnez, t1, off);
break;
case BPF_JLE:
emit_sltu_r64(ctx, t1, src, dst);
emit(ctx, beqz, t1, off);
break;
case BPF_JSGT:
emit_slt_r64(ctx, t1, src, dst);
emit(ctx, bnez, t1, off);
break;
case BPF_JSGE:
emit_slt_r64(ctx, t1, dst, src);
emit(ctx, beqz, t1, off);
break;
case BPF_JSLT:
emit_slt_r64(ctx, t1, dst, src);
emit(ctx, bnez, t1, off);
break;
case BPF_JSLE:
emit_slt_r64(ctx, t1, src, dst);
emit(ctx, beqz, t1, off);
break;
}
}
static int emit_call(struct jit_context *ctx, const struct bpf_insn *insn)
{
bool fixed;
u64 addr;
if (bpf_jit_get_func_addr(ctx->program, insn, false,
&addr, &fixed) < 0)
return -1;
if (!fixed)
return -1;
push_regs(ctx, JIT_STACK_REGS, 0, JIT_RESERVED_STACK);
emit_mov_i(ctx, MIPS_R_T9, addr);
emit(ctx, jalr, MIPS_R_RA, MIPS_R_T9);
emit(ctx, nop);
clobber_reg(ctx, MIPS_R_RA);
clobber_reg(ctx, MIPS_R_V0);
clobber_reg(ctx, MIPS_R_V1);
return 0;
}
static int emit_tail_call(struct jit_context *ctx)
{
u8 ary = lo(bpf2mips32[BPF_REG_2]);
u8 ind = lo(bpf2mips32[BPF_REG_3]);
u8 t1 = MIPS_R_T8;
u8 t2 = MIPS_R_T9;
int off;
off = offsetof(struct bpf_array, map.max_entries);
if (off > 0x7fff)
return -1;
emit(ctx, lw, t1, off, ary);
emit_load_delay(ctx);
emit(ctx, sltu, t1, ind, t1);
emit(ctx, beqz, t1, get_offset(ctx, 1));
emit(ctx, lw, t2, ctx->stack_size, MIPS_R_SP);
emit_load_delay(ctx);
emit(ctx, blez, t2, get_offset(ctx, 1));
emit(ctx, addiu, t2, t2, -1);
emit(ctx, sw, t2, ctx->stack_size, MIPS_R_SP);
off = offsetof(struct bpf_array, ptrs);
if (off > 0x7fff)
return -1;
emit(ctx, sll, t1, ind, 2);
emit(ctx, addu, t1, t1, ary);
emit(ctx, lw, t2, off, t1);
emit_load_delay(ctx);
emit(ctx, beqz, t2, get_offset(ctx, 1));
emit(ctx, nop);
off = offsetof(struct bpf_prog, bpf_func);
if (off > 0x7fff)
return -1;
emit(ctx, lw, t1, off, t2);
emit_load_delay(ctx);
emit(ctx, addiu, t1, t1, JIT_TCALL_SKIP);
build_epilogue(ctx, t1);
return 0;
}
void build_prologue(struct jit_context *ctx)
{
const u8 *r1 = bpf2mips32[BPF_REG_1];
const u8 *fp = bpf2mips32[BPF_REG_FP];
int stack, saved, locals, reserved;
BUILD_BUG_ON(MAX_TAIL_CALL_CNT > 0xffff);
emit(ctx, ori, MIPS_R_T9, MIPS_R_ZERO, MAX_TAIL_CALL_CNT);
emit(ctx, sw, MIPS_R_T9, 0, MIPS_R_SP);
#ifdef __BIG_ENDIAN
emit(ctx, move, lo(r1), MIPS_R_A0);
#endif
emit(ctx, move, hi(r1), MIPS_R_ZERO);
if (ctx->accessed & BIT(BPF_REG_FP))
clobber_reg64(ctx, fp);
saved = hweight32(ctx->clobbered & JIT_CALLEE_REGS) * sizeof(u32);
saved = ALIGN(saved, MIPS_STACK_ALIGNMENT);
locals = ALIGN(ctx->program->aux->stack_depth, MIPS_STACK_ALIGNMENT);
reserved = ctx->stack_used;
stack = ALIGN(saved + locals + reserved, MIPS_STACK_ALIGNMENT);
emit(ctx, addiu, MIPS_R_SP, MIPS_R_SP, -stack);
push_regs(ctx, ctx->clobbered & JIT_CALLEE_REGS, 0, stack - saved);
if (ctx->accessed & BIT(BPF_REG_FP))
emit(ctx, addiu, lo(fp), MIPS_R_SP, stack - saved);
ctx->saved_size = saved;
ctx->stack_size = stack;
}
void build_epilogue(struct jit_context *ctx, int dest_reg)
{
pop_regs(ctx, ctx->clobbered & JIT_CALLEE_REGS, 0,
ctx->stack_size - ctx->saved_size);
#ifdef __BIG_ENDIAN
emit(ctx, move, MIPS_R_V0, MIPS_R_V1);
#endif
emit(ctx, jr, dest_reg);
emit(ctx, addiu, MIPS_R_SP, MIPS_R_SP, ctx->stack_size);
}
int build_insn(const struct bpf_insn *insn, struct jit_context *ctx)
{
const u8 *dst = bpf2mips32[insn->dst_reg];
const u8 *src = bpf2mips32[insn->src_reg];
const u8 *res = bpf2mips32[BPF_REG_0];
const u8 *tmp = bpf2mips32[JIT_REG_TMP];
u8 code = insn->code;
s16 off = insn->off;
s32 imm = insn->imm;
s32 val, rel;
u8 alu, jmp;
switch (code) {
case BPF_ALU | BPF_MOV | BPF_K:
emit_mov_i(ctx, lo(dst), imm);
emit_zext_ver(ctx, dst);
break;
case BPF_ALU | BPF_MOV | BPF_X:
if (imm == 1) {
emit_mov_i(ctx, hi(dst), 0);
} else {
emit_mov_r(ctx, lo(dst), lo(src));
emit_zext_ver(ctx, dst);
}
break;
case BPF_ALU | BPF_NEG:
emit_alu_i(ctx, lo(dst), 0, BPF_NEG);
emit_zext_ver(ctx, dst);
break;
case BPF_ALU | BPF_OR | BPF_K:
case BPF_ALU | BPF_AND | 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:
case BPF_ALU | BPF_ADD | BPF_K:
case BPF_ALU | BPF_SUB | BPF_K:
case BPF_ALU | BPF_MUL | BPF_K:
case BPF_ALU | BPF_DIV | BPF_K:
case BPF_ALU | BPF_MOD | BPF_K:
if (!valid_alu_i(BPF_OP(code), imm)) {
emit_mov_i(ctx, MIPS_R_T6, imm);
emit_alu_r(ctx, lo(dst), MIPS_R_T6, BPF_OP(code));
} else if (rewrite_alu_i(BPF_OP(code), imm, &alu, &val)) {
emit_alu_i(ctx, lo(dst), val, alu);
}
emit_zext_ver(ctx, dst);
break;
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_LSH | BPF_X:
case BPF_ALU | BPF_RSH | BPF_X:
case BPF_ALU | BPF_ARSH | BPF_X:
case BPF_ALU | BPF_ADD | BPF_X:
case BPF_ALU | BPF_SUB | BPF_X:
case BPF_ALU | BPF_MUL | BPF_X:
case BPF_ALU | BPF_DIV | BPF_X:
case BPF_ALU | BPF_MOD | BPF_X:
emit_alu_r(ctx, lo(dst), lo(src), BPF_OP(code));
emit_zext_ver(ctx, dst);
break;
case BPF_ALU64 | BPF_MOV | BPF_K:
emit_mov_se_i64(ctx, dst, imm);
break;
case BPF_ALU64 | BPF_MOV | BPF_X:
emit_mov_r(ctx, lo(dst), lo(src));
emit_mov_r(ctx, hi(dst), hi(src));
break;
case BPF_ALU64 | BPF_NEG:
emit_neg_i64(ctx, dst);
break;
case BPF_ALU64 | BPF_AND | BPF_K:
emit_alu_i64(ctx, dst, imm, BPF_OP(code));
break;
case BPF_ALU64 | BPF_OR | BPF_K:
case BPF_ALU64 | BPF_XOR | BPF_K:
case BPF_ALU64 | BPF_ADD | BPF_K:
case BPF_ALU64 | BPF_SUB | BPF_K:
if (imm)
emit_alu_i64(ctx, dst, imm, BPF_OP(code));
break;
case BPF_ALU64 | BPF_LSH | BPF_K:
case BPF_ALU64 | BPF_RSH | BPF_K:
case BPF_ALU64 | BPF_ARSH | BPF_K:
if (imm)
emit_shift_i64(ctx, dst, imm, BPF_OP(code));
break;
case BPF_ALU64 | BPF_MUL | BPF_K:
emit_mul_i64(ctx, dst, imm);
break;
case BPF_ALU64 | BPF_DIV | BPF_K:
case BPF_ALU64 | BPF_MOD | BPF_K:
emit_mov_se_i64(ctx, tmp, imm);
emit_divmod_r64(ctx, dst, tmp, BPF_OP(code));
break;
case BPF_ALU64 | BPF_AND | BPF_X:
case BPF_ALU64 | BPF_OR | BPF_X:
case BPF_ALU64 | BPF_XOR | BPF_X:
case BPF_ALU64 | BPF_ADD | BPF_X:
case BPF_ALU64 | BPF_SUB | BPF_X:
emit_alu_r64(ctx, dst, src, BPF_OP(code));
break;
case BPF_ALU64 | BPF_LSH | BPF_X:
case BPF_ALU64 | BPF_RSH | BPF_X:
case BPF_ALU64 | BPF_ARSH | BPF_X:
emit_shift_r64(ctx, dst, lo(src), BPF_OP(code));
break;
case BPF_ALU64 | BPF_MUL | BPF_X:
emit_mul_r64(ctx, dst, src);
break;
case BPF_ALU64 | BPF_DIV | BPF_X:
case BPF_ALU64 | BPF_MOD | BPF_X:
emit_divmod_r64(ctx, dst, src, BPF_OP(code));
break;
case BPF_ALU | BPF_END | BPF_FROM_LE:
case BPF_ALU | BPF_END | BPF_FROM_BE:
if (BPF_SRC(code) ==
#ifdef __BIG_ENDIAN
BPF_FROM_LE
#else
BPF_FROM_BE
#endif
)
emit_bswap_r64(ctx, dst, imm);
else
emit_trunc_r64(ctx, dst, imm);
break;
case BPF_LD | BPF_IMM | BPF_DW:
emit_mov_i(ctx, lo(dst), imm);
emit_mov_i(ctx, hi(dst), insn[1].imm);
return 1;
case BPF_LDX | BPF_MEM | BPF_W:
case BPF_LDX | BPF_MEM | BPF_H:
case BPF_LDX | BPF_MEM | BPF_B:
case BPF_LDX | BPF_MEM | BPF_DW:
emit_ldx(ctx, dst, lo(src), off, BPF_SIZE(code));
break;
case BPF_ST | BPF_MEM | BPF_W:
case BPF_ST | BPF_MEM | BPF_H:
case BPF_ST | BPF_MEM | BPF_B:
case BPF_ST | BPF_MEM | BPF_DW:
switch (BPF_SIZE(code)) {
case BPF_DW:
emit_mov_se_i64(ctx, tmp, imm);
break;
case BPF_W:
case BPF_H:
case BPF_B:
emit_mov_i(ctx, lo(tmp), imm);
break;
}
emit_stx(ctx, lo(dst), tmp, off, BPF_SIZE(code));
break;
case BPF_STX | BPF_MEM | BPF_W:
case BPF_STX | BPF_MEM | BPF_H:
case BPF_STX | BPF_MEM | BPF_B:
case BPF_STX | BPF_MEM | BPF_DW:
emit_stx(ctx, lo(dst), src, off, BPF_SIZE(code));
break;
case BPF_ST | BPF_NOSPEC:
break;
case BPF_STX | BPF_ATOMIC | BPF_W:
switch (imm) {
case BPF_ADD:
case BPF_ADD | BPF_FETCH:
case BPF_AND:
case BPF_AND | BPF_FETCH:
case BPF_OR:
case BPF_OR | BPF_FETCH:
case BPF_XOR:
case BPF_XOR | BPF_FETCH:
case BPF_XCHG:
if (cpu_has_llsc)
emit_atomic_r(ctx, lo(dst), lo(src), off, imm);
else
emit_atomic_r32(ctx, lo(dst), lo(src),
off, imm);
if (imm & BPF_FETCH)
emit_zext_ver(ctx, src);
break;
case BPF_CMPXCHG:
if (cpu_has_llsc)
emit_cmpxchg_r(ctx, lo(dst), lo(src),
lo(res), off);
else
emit_cmpxchg_r32(ctx, lo(dst), lo(src), off);
break;
default:
goto notyet;
}
break;
case BPF_STX | BPF_ATOMIC | BPF_DW:
switch (imm) {
case BPF_ADD:
case BPF_ADD | BPF_FETCH:
case BPF_AND:
case BPF_AND | BPF_FETCH:
case BPF_OR:
case BPF_OR | BPF_FETCH:
case BPF_XOR:
case BPF_XOR | BPF_FETCH:
case BPF_XCHG:
emit_atomic_r64(ctx, lo(dst), src, off, imm);
break;
case BPF_CMPXCHG:
emit_cmpxchg_r64(ctx, lo(dst), src, off);
break;
default:
goto notyet;
}
break;
case BPF_JMP32 | BPF_JEQ | BPF_X:
case BPF_JMP32 | BPF_JNE | BPF_X:
case BPF_JMP32 | BPF_JSET | BPF_X:
case BPF_JMP32 | BPF_JGT | BPF_X:
case BPF_JMP32 | BPF_JGE | BPF_X:
case BPF_JMP32 | BPF_JLT | BPF_X:
case BPF_JMP32 | BPF_JLE | BPF_X:
case BPF_JMP32 | BPF_JSGT | BPF_X:
case BPF_JMP32 | BPF_JSGE | BPF_X:
case BPF_JMP32 | BPF_JSLT | BPF_X:
case BPF_JMP32 | BPF_JSLE | BPF_X:
if (off == 0)
break;
setup_jmp_r(ctx, dst == src, BPF_OP(code), off, &jmp, &rel);
emit_jmp_r(ctx, lo(dst), lo(src), rel, jmp);
if (finish_jmp(ctx, jmp, off) < 0)
goto toofar;
break;
case BPF_JMP32 | BPF_JEQ | BPF_K:
case BPF_JMP32 | BPF_JNE | BPF_K:
case BPF_JMP32 | BPF_JSET | BPF_K:
case BPF_JMP32 | BPF_JGT | BPF_K:
case BPF_JMP32 | BPF_JGE | BPF_K:
case BPF_JMP32 | BPF_JLT | BPF_K:
case BPF_JMP32 | BPF_JLE | BPF_K:
case BPF_JMP32 | BPF_JSGT | BPF_K:
case BPF_JMP32 | BPF_JSGE | BPF_K:
case BPF_JMP32 | BPF_JSLT | BPF_K:
case BPF_JMP32 | BPF_JSLE | BPF_K:
if (off == 0)
break;
setup_jmp_i(ctx, imm, 32, BPF_OP(code), off, &jmp, &rel);
if (valid_jmp_i(jmp, imm)) {
emit_jmp_i(ctx, lo(dst), imm, rel, jmp);
} else {
emit_mov_i(ctx, MIPS_R_T6, imm);
emit_jmp_r(ctx, lo(dst), MIPS_R_T6, rel, jmp);
}
if (finish_jmp(ctx, jmp, off) < 0)
goto toofar;
break;
case BPF_JMP | BPF_JEQ | BPF_X:
case BPF_JMP | BPF_JNE | BPF_X:
case BPF_JMP | BPF_JSET | BPF_X:
case BPF_JMP | BPF_JGT | BPF_X:
case BPF_JMP | BPF_JGE | BPF_X:
case BPF_JMP | BPF_JLT | BPF_X:
case BPF_JMP | BPF_JLE | BPF_X:
case BPF_JMP | BPF_JSGT | BPF_X:
case BPF_JMP | BPF_JSGE | BPF_X:
case BPF_JMP | BPF_JSLT | BPF_X:
case BPF_JMP | BPF_JSLE | BPF_X:
if (off == 0)
break;
setup_jmp_r(ctx, dst == src, BPF_OP(code), off, &jmp, &rel);
emit_jmp_r64(ctx, dst, src, rel, jmp);
if (finish_jmp(ctx, jmp, off) < 0)
goto toofar;
break;
case BPF_JMP | BPF_JEQ | BPF_K:
case BPF_JMP | BPF_JNE | BPF_K:
case BPF_JMP | BPF_JSET | BPF_K:
case BPF_JMP | BPF_JGT | BPF_K:
case BPF_JMP | BPF_JGE | BPF_K:
case BPF_JMP | BPF_JLT | BPF_K:
case BPF_JMP | BPF_JLE | BPF_K:
case BPF_JMP | BPF_JSGT | BPF_K:
case BPF_JMP | BPF_JSGE | BPF_K:
case BPF_JMP | BPF_JSLT | BPF_K:
case BPF_JMP | BPF_JSLE | BPF_K:
if (off == 0)
break;
setup_jmp_i(ctx, imm, 64, BPF_OP(code), off, &jmp, &rel);
emit_jmp_i64(ctx, dst, imm, rel, jmp);
if (finish_jmp(ctx, jmp, off) < 0)
goto toofar;
break;
case BPF_JMP | BPF_JA:
if (off == 0)
break;
if (emit_ja(ctx, off) < 0)
goto toofar;
break;
case BPF_JMP | BPF_TAIL_CALL:
if (emit_tail_call(ctx) < 0)
goto invalid;
break;
case BPF_JMP | BPF_CALL:
if (emit_call(ctx, insn) < 0)
goto invalid;
break;
case BPF_JMP | BPF_EXIT:
if (ctx->bpf_index == ctx->program->len - 1)
break;
if (emit_exit(ctx) < 0)
goto toofar;
break;
default:
invalid:
pr_err_once("unknown opcode %02x\n", code);
return -EINVAL;
notyet:
pr_info_once("*** NOT YET: opcode %02x ***\n", code);
return -EFAULT;
toofar:
pr_info_once("*** TOO FAR: jump at %u opcode %02x ***\n",
ctx->bpf_index, code);
return -E2BIG;
}
return 0;
}