#include <linux/limits.h>
#include <linux/bitops.h>
#include <linux/errno.h>
#include <linux/filter.h>
#include <linux/bpf.h>
#include <linux/slab.h>
#include <asm/bitops.h>
#include <asm/cacheflush.h>
#include <asm/cpu-features.h>
#include <asm/isa-rev.h>
#include <asm/uasm.h>
#include "bpf_jit_comp.h"
#define CONVERTED(desc) ((desc) & JIT_DESC_CONVERT)
#define INDEX(desc) ((desc) & ~JIT_DESC_CONVERT)
int push_regs(struct jit_context *ctx, u32 mask, u32 excl, int depth)
{
int reg;
for (reg = 0; reg < BITS_PER_BYTE * sizeof(mask); reg++)
if (mask & BIT(reg)) {
if ((excl & BIT(reg)) == 0) {
if (sizeof(long) == 4)
emit(ctx, sw, reg, depth, MIPS_R_SP);
else
emit(ctx, sd, reg, depth, MIPS_R_SP);
}
depth += sizeof(long);
}
ctx->stack_used = max((int)ctx->stack_used, depth);
return depth;
}
int pop_regs(struct jit_context *ctx, u32 mask, u32 excl, int depth)
{
int reg;
for (reg = 0; reg < BITS_PER_BYTE * sizeof(mask); reg++)
if (mask & BIT(reg)) {
if ((excl & BIT(reg)) == 0) {
if (sizeof(long) == 4)
emit(ctx, lw, reg, depth, MIPS_R_SP);
else
emit(ctx, ld, reg, depth, MIPS_R_SP);
}
depth += sizeof(long);
}
return depth;
}
int get_target(struct jit_context *ctx, u32 loc)
{
u32 index = INDEX(ctx->descriptors[loc]);
unsigned long pc = (unsigned long)&ctx->target[ctx->jit_index];
unsigned long addr = (unsigned long)&ctx->target[index];
if (!ctx->target)
return 0;
if ((addr ^ pc) & ~MIPS_JMP_MASK)
return -1;
return addr & MIPS_JMP_MASK;
}
int get_offset(const struct jit_context *ctx, int off)
{
return (INDEX(ctx->descriptors[ctx->bpf_index + off]) -
ctx->jit_index - 1) * sizeof(u32);
}
void emit_mov_i(struct jit_context *ctx, u8 dst, s32 imm)
{
if (imm >= -0x8000 && imm <= 0x7fff) {
emit(ctx, addiu, dst, MIPS_R_ZERO, imm);
} else {
emit(ctx, lui, dst, (s16)((u32)imm >> 16));
emit(ctx, ori, dst, dst, (u16)(imm & 0xffff));
}
clobber_reg(ctx, dst);
}
void emit_mov_r(struct jit_context *ctx, u8 dst, u8 src)
{
emit(ctx, ori, dst, src, 0);
clobber_reg(ctx, dst);
}
bool valid_alu_i(u8 op, s32 imm)
{
switch (BPF_OP(op)) {
case BPF_NEG:
case BPF_LSH:
case BPF_RSH:
case BPF_ARSH:
return true;
case BPF_ADD:
if (IS_ENABLED(CONFIG_CPU_DADDI_WORKAROUNDS))
return false;
return imm >= -0x8000 && imm <= 0x7fff;
case BPF_SUB:
if (IS_ENABLED(CONFIG_CPU_DADDI_WORKAROUNDS))
return false;
return imm >= -0x7fff && imm <= 0x8000;
case BPF_AND:
case BPF_OR:
case BPF_XOR:
return imm >= 0 && imm <= 0xffff;
case BPF_MUL:
return imm == 0 || (imm > 0 && is_power_of_2(imm));
case BPF_DIV:
case BPF_MOD:
return (u32)imm <= 0x10000 && is_power_of_2((u32)imm);
}
return false;
}
bool rewrite_alu_i(u8 op, s32 imm, u8 *alu, s32 *val)
{
bool act = true;
switch (BPF_OP(op)) {
case BPF_LSH:
case BPF_RSH:
case BPF_ARSH:
case BPF_ADD:
case BPF_SUB:
case BPF_OR:
case BPF_XOR:
act = imm != 0;
break;
case BPF_MUL:
if (imm == 1) {
act = false;
} else if (imm == 0) {
op = BPF_AND;
} else {
op = BPF_LSH;
imm = ilog2(abs(imm));
}
break;
case BPF_DIV:
if (imm == 1) {
act = false;
} else {
op = BPF_RSH;
imm = ilog2(imm);
}
break;
case BPF_MOD:
op = BPF_AND;
imm--;
break;
}
*alu = op;
*val = imm;
return act;
}
void emit_alu_i(struct jit_context *ctx, u8 dst, s32 imm, u8 op)
{
switch (BPF_OP(op)) {
case BPF_NEG:
emit(ctx, subu, dst, MIPS_R_ZERO, dst);
break;
case BPF_AND:
emit(ctx, andi, dst, dst, (u16)imm);
break;
case BPF_OR:
emit(ctx, ori, dst, dst, (u16)imm);
break;
case BPF_XOR:
emit(ctx, xori, dst, dst, (u16)imm);
break;
case BPF_LSH:
emit(ctx, sll, dst, dst, imm);
break;
case BPF_RSH:
emit(ctx, srl, dst, dst, imm);
break;
case BPF_ARSH:
emit(ctx, sra, dst, dst, imm);
break;
case BPF_ADD:
emit(ctx, addiu, dst, dst, imm);
break;
case BPF_SUB:
emit(ctx, addiu, dst, dst, -imm);
break;
}
clobber_reg(ctx, dst);
}
void emit_alu_r(struct jit_context *ctx, u8 dst, u8 src, u8 op)
{
switch (BPF_OP(op)) {
case BPF_AND:
emit(ctx, and, dst, dst, src);
break;
case BPF_OR:
emit(ctx, or, dst, dst, src);
break;
case BPF_XOR:
emit(ctx, xor, dst, dst, src);
break;
case BPF_LSH:
emit(ctx, sllv, dst, dst, src);
break;
case BPF_RSH:
emit(ctx, srlv, dst, dst, src);
break;
case BPF_ARSH:
emit(ctx, srav, dst, dst, src);
break;
case BPF_ADD:
emit(ctx, addu, dst, dst, src);
break;
case BPF_SUB:
emit(ctx, subu, dst, dst, src);
break;
case BPF_MUL:
if (cpu_has_mips32r1 || cpu_has_mips32r6) {
emit(ctx, mul, dst, dst, src);
} else {
emit(ctx, multu, dst, src);
emit(ctx, mflo, dst);
}
break;
case BPF_DIV:
if (cpu_has_mips32r6) {
emit(ctx, divu_r6, dst, dst, src);
} else {
emit(ctx, divu, dst, src);
emit(ctx, mflo, dst);
}
break;
case BPF_MOD:
if (cpu_has_mips32r6) {
emit(ctx, modu, dst, dst, src);
} else {
emit(ctx, divu, dst, src);
emit(ctx, mfhi, dst);
}
break;
}
clobber_reg(ctx, dst);
}
void emit_atomic_r(struct jit_context *ctx, u8 dst, u8 src, s16 off, u8 code)
{
LLSC_sync(ctx);
emit(ctx, ll, MIPS_R_T9, off, dst);
switch (code) {
case BPF_ADD:
case BPF_ADD | BPF_FETCH:
emit(ctx, addu, MIPS_R_T8, MIPS_R_T9, src);
break;
case BPF_AND:
case BPF_AND | BPF_FETCH:
emit(ctx, and, MIPS_R_T8, MIPS_R_T9, src);
break;
case BPF_OR:
case BPF_OR | BPF_FETCH:
emit(ctx, or, MIPS_R_T8, MIPS_R_T9, src);
break;
case BPF_XOR:
case BPF_XOR | BPF_FETCH:
emit(ctx, xor, MIPS_R_T8, MIPS_R_T9, src);
break;
case BPF_XCHG:
emit(ctx, move, MIPS_R_T8, src);
break;
}
emit(ctx, sc, MIPS_R_T8, off, dst);
emit(ctx, LLSC_beqz, MIPS_R_T8, -16 - LLSC_offset);
emit(ctx, nop);
if (code & BPF_FETCH) {
emit(ctx, move, src, MIPS_R_T9);
clobber_reg(ctx, src);
}
}
void emit_cmpxchg_r(struct jit_context *ctx, u8 dst, u8 src, u8 res, s16 off)
{
LLSC_sync(ctx);
emit(ctx, ll, MIPS_R_T9, off, dst);
emit(ctx, bne, MIPS_R_T9, res, 12);
emit(ctx, move, MIPS_R_T8, src);
emit(ctx, sc, MIPS_R_T8, off, dst);
emit(ctx, LLSC_beqz, MIPS_R_T8, -20 - LLSC_offset);
emit(ctx, move, res, MIPS_R_T9);
clobber_reg(ctx, res);
}
void emit_bswap_r(struct jit_context *ctx, u8 dst, u32 width)
{
u8 tmp = MIPS_R_T8;
u8 msk = MIPS_R_T9;
switch (width) {
case 32:
if (cpu_has_mips32r2 || cpu_has_mips32r6) {
emit(ctx, wsbh, dst, dst);
emit(ctx, rotr, dst, dst, 16);
} else {
emit(ctx, sll, tmp, dst, 16);
emit(ctx, srl, dst, dst, 16);
emit(ctx, or, dst, dst, tmp);
emit(ctx, lui, msk, 0xff);
emit(ctx, ori, msk, msk, 0xff);
emit(ctx, and, tmp, dst, msk);
emit(ctx, sll, tmp, tmp, 8);
emit(ctx, srl, dst, dst, 8);
emit(ctx, and, dst, dst, msk);
emit(ctx, or, dst, dst, tmp);
}
break;
case 16:
if (cpu_has_mips32r2 || cpu_has_mips32r6) {
emit(ctx, wsbh, dst, dst);
emit(ctx, andi, dst, dst, 0xffff);
} else {
emit(ctx, andi, tmp, dst, 0xff00);
emit(ctx, srl, tmp, tmp, 8);
emit(ctx, andi, dst, dst, 0x00ff);
emit(ctx, sll, dst, dst, 8);
emit(ctx, or, dst, dst, tmp);
}
break;
}
clobber_reg(ctx, dst);
}
bool valid_jmp_i(u8 op, s32 imm)
{
switch (op) {
case JIT_JNOP:
return true;
case BPF_JEQ:
case BPF_JNE:
return false;
case BPF_JSET:
case JIT_JNSET:
return imm >= 0 && imm <= 0xffff;
case BPF_JGE:
case BPF_JLT:
case BPF_JSGE:
case BPF_JSLT:
return imm >= -0x8000 && imm <= 0x7fff;
case BPF_JGT:
case BPF_JLE:
case BPF_JSGT:
case BPF_JSLE:
return imm >= -0x8001 && imm <= 0x7ffe;
}
return false;
}
static u8 invert_jmp(u8 op)
{
switch (op) {
case BPF_JA: return JIT_JNOP;
case BPF_JEQ: return BPF_JNE;
case BPF_JNE: return BPF_JEQ;
case BPF_JSET: return JIT_JNSET;
case BPF_JGT: return BPF_JLE;
case BPF_JGE: return BPF_JLT;
case BPF_JLT: return BPF_JGE;
case BPF_JLE: return BPF_JGT;
case BPF_JSGT: return BPF_JSLE;
case BPF_JSGE: return BPF_JSLT;
case BPF_JSLT: return BPF_JSGE;
case BPF_JSLE: return BPF_JSGT;
}
return 0;
}
static void setup_jmp(struct jit_context *ctx, u8 bpf_op,
s16 bpf_off, u8 *jit_op, s32 *jit_off)
{
u32 *descp = &ctx->descriptors[ctx->bpf_index];
int op = bpf_op;
int offset = 0;
if (INDEX(*descp) == 0)
goto done;
if (bpf_op == JIT_JNOP)
goto done;
if (bpf_op == BPF_JA)
*descp |= JIT_DESC_CONVERT;
if (!CONVERTED(*descp)) {
int target = ctx->bpf_index + bpf_off + 1;
int origin = ctx->bpf_index + 1;
offset = (INDEX(ctx->descriptors[target]) -
INDEX(ctx->descriptors[origin]) + 1) * sizeof(u32);
}
if (CONVERTED(*descp) || offset < -0x20000 || offset > 0x1ffff) {
offset = 3 * sizeof(u32);
op = invert_jmp(bpf_op);
ctx->changes += !CONVERTED(*descp);
*descp |= JIT_DESC_CONVERT;
}
done:
*jit_off = offset;
*jit_op = op;
}
void setup_jmp_i(struct jit_context *ctx, s32 imm, u8 width,
u8 bpf_op, s16 bpf_off, u8 *jit_op, s32 *jit_off)
{
bool always = false;
bool never = false;
switch (bpf_op) {
case BPF_JEQ:
case BPF_JNE:
break;
case BPF_JSET:
case BPF_JLT:
never = imm == 0;
break;
case BPF_JGE:
always = imm == 0;
break;
case BPF_JGT:
never = (u32)imm == U32_MAX;
break;
case BPF_JLE:
always = (u32)imm == U32_MAX;
break;
case BPF_JSGT:
never = imm == S32_MAX && width == 32;
break;
case BPF_JSGE:
always = imm == S32_MIN && width == 32;
break;
case BPF_JSLT:
never = imm == S32_MIN && width == 32;
break;
case BPF_JSLE:
always = imm == S32_MAX && width == 32;
break;
}
if (never)
bpf_op = JIT_JNOP;
if (always)
bpf_op = BPF_JA;
setup_jmp(ctx, bpf_op, bpf_off, jit_op, jit_off);
}
void setup_jmp_r(struct jit_context *ctx, bool same_reg,
u8 bpf_op, s16 bpf_off, u8 *jit_op, s32 *jit_off)
{
switch (bpf_op) {
case BPF_JSET:
break;
case BPF_JEQ:
case BPF_JGE:
case BPF_JLE:
case BPF_JSGE:
case BPF_JSLE:
if (same_reg)
bpf_op = BPF_JA;
break;
case BPF_JNE:
case BPF_JLT:
case BPF_JGT:
case BPF_JSGT:
case BPF_JSLT:
if (same_reg)
bpf_op = JIT_JNOP;
break;
}
setup_jmp(ctx, bpf_op, bpf_off, jit_op, jit_off);
}
int finish_jmp(struct jit_context *ctx, u8 jit_op, s16 bpf_off)
{
if (jit_op != JIT_JNOP)
emit(ctx, nop);
if (CONVERTED(ctx->descriptors[ctx->bpf_index])) {
int target = get_target(ctx, ctx->bpf_index + bpf_off + 1);
if (target < 0)
return -1;
emit(ctx, j, target);
emit(ctx, nop);
}
return 0;
}
void emit_jmp_i(struct jit_context *ctx, u8 dst, s32 imm, s32 off, u8 op)
{
switch (op) {
case JIT_JNOP:
break;
case BPF_JSET:
emit(ctx, andi, MIPS_R_T9, dst, (u16)imm);
emit(ctx, bnez, MIPS_R_T9, off);
break;
case JIT_JNSET:
emit(ctx, andi, MIPS_R_T9, dst, (u16)imm);
emit(ctx, beqz, MIPS_R_T9, off);
break;
case BPF_JGT:
emit(ctx, sltiu, MIPS_R_T9, dst, imm + 1);
emit(ctx, beqz, MIPS_R_T9, off);
break;
case BPF_JGE:
emit(ctx, sltiu, MIPS_R_T9, dst, imm);
emit(ctx, beqz, MIPS_R_T9, off);
break;
case BPF_JLT:
emit(ctx, sltiu, MIPS_R_T9, dst, imm);
emit(ctx, bnez, MIPS_R_T9, off);
break;
case BPF_JLE:
emit(ctx, sltiu, MIPS_R_T9, dst, imm + 1);
emit(ctx, bnez, MIPS_R_T9, off);
break;
case BPF_JSGT:
emit(ctx, slti, MIPS_R_T9, dst, imm + 1);
emit(ctx, beqz, MIPS_R_T9, off);
break;
case BPF_JSGE:
emit(ctx, slti, MIPS_R_T9, dst, imm);
emit(ctx, beqz, MIPS_R_T9, off);
break;
case BPF_JSLT:
emit(ctx, slti, MIPS_R_T9, dst, imm);
emit(ctx, bnez, MIPS_R_T9, off);
break;
case BPF_JSLE:
emit(ctx, slti, MIPS_R_T9, dst, imm + 1);
emit(ctx, bnez, MIPS_R_T9, off);
break;
}
}
void emit_jmp_r(struct jit_context *ctx, u8 dst, u8 src, s32 off, u8 op)
{
switch (op) {
case JIT_JNOP:
break;
case BPF_JEQ:
emit(ctx, beq, dst, src, off);
break;
case BPF_JNE:
emit(ctx, bne, dst, src, off);
break;
case BPF_JSET:
emit(ctx, and, MIPS_R_T9, dst, src);
emit(ctx, bnez, MIPS_R_T9, off);
break;
case JIT_JNSET:
emit(ctx, and, MIPS_R_T9, dst, src);
emit(ctx, beqz, MIPS_R_T9, off);
break;
case BPF_JGT:
emit(ctx, sltu, MIPS_R_T9, src, dst);
emit(ctx, bnez, MIPS_R_T9, off);
break;
case BPF_JGE:
emit(ctx, sltu, MIPS_R_T9, dst, src);
emit(ctx, beqz, MIPS_R_T9, off);
break;
case BPF_JLT:
emit(ctx, sltu, MIPS_R_T9, dst, src);
emit(ctx, bnez, MIPS_R_T9, off);
break;
case BPF_JLE:
emit(ctx, sltu, MIPS_R_T9, src, dst);
emit(ctx, beqz, MIPS_R_T9, off);
break;
case BPF_JSGT:
emit(ctx, slt, MIPS_R_T9, src, dst);
emit(ctx, bnez, MIPS_R_T9, off);
break;
case BPF_JSGE:
emit(ctx, slt, MIPS_R_T9, dst, src);
emit(ctx, beqz, MIPS_R_T9, off);
break;
case BPF_JSLT:
emit(ctx, slt, MIPS_R_T9, dst, src);
emit(ctx, bnez, MIPS_R_T9, off);
break;
case BPF_JSLE:
emit(ctx, slt, MIPS_R_T9, src, dst);
emit(ctx, beqz, MIPS_R_T9, off);
break;
}
}
int emit_ja(struct jit_context *ctx, s16 off)
{
int target = get_target(ctx, ctx->bpf_index + off + 1);
if (target < 0)
return -1;
emit(ctx, j, target);
emit(ctx, nop);
return 0;
}
int emit_exit(struct jit_context *ctx)
{
int target = get_target(ctx, ctx->program->len);
if (target < 0)
return -1;
emit(ctx, j, target);
emit(ctx, nop);
return 0;
}
static int build_body(struct jit_context *ctx)
{
const struct bpf_prog *prog = ctx->program;
unsigned int i;
ctx->stack_used = 0;
for (i = 0; i < prog->len; i++) {
const struct bpf_insn *insn = &prog->insnsi[i];
u32 *descp = &ctx->descriptors[i];
int ret;
access_reg(ctx, insn->src_reg);
access_reg(ctx, insn->dst_reg);
ctx->bpf_index = i;
if (ctx->target == NULL) {
ctx->changes += INDEX(*descp) != ctx->jit_index;
*descp &= JIT_DESC_CONVERT;
*descp |= ctx->jit_index;
}
ret = build_insn(insn, ctx);
if (ret < 0)
return ret;
if (ret > 0) {
i++;
if (ctx->target == NULL)
descp[1] = ctx->jit_index;
}
}
ctx->descriptors[prog->len] = ctx->jit_index;
return 0;
}
static void set_convert_flag(struct jit_context *ctx, bool enable)
{
const struct bpf_prog *prog = ctx->program;
u32 flag = enable ? JIT_DESC_CONVERT : 0;
unsigned int i;
for (i = 0; i <= prog->len; i++)
ctx->descriptors[i] = INDEX(ctx->descriptors[i]) | flag;
}
static void jit_fill_hole(void *area, unsigned int size)
{
u32 *p;
for (p = area; size >= sizeof(u32); size -= sizeof(u32))
uasm_i_break(&p, BRK_BUG);
}
bool bpf_jit_needs_zext(void)
{
return true;
}
struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
{
struct bpf_prog *tmp, *orig_prog = prog;
struct bpf_binary_header *header = NULL;
struct jit_context ctx;
bool tmp_blinded = false;
unsigned int tmp_idx;
unsigned int image_size;
u8 *image_ptr;
int tries;
if (!prog->jit_requested)
return orig_prog;
tmp = bpf_jit_blind_constants(prog);
if (IS_ERR(tmp))
return orig_prog;
if (tmp != prog) {
tmp_blinded = true;
prog = tmp;
}
memset(&ctx, 0, sizeof(ctx));
ctx.program = prog;
ctx.descriptors = kcalloc(prog->len + 1, sizeof(*ctx.descriptors),
GFP_KERNEL);
if (ctx.descriptors == NULL)
goto out_err;
if (build_body(&ctx) < 0)
goto out_err;
ctx.jit_index = 0;
build_prologue(&ctx);
tmp_idx = ctx.jit_index;
tries = JIT_MAX_ITERATIONS;
do {
ctx.jit_index = tmp_idx;
ctx.changes = 0;
if (tries == 2)
set_convert_flag(&ctx, true);
if (build_body(&ctx) < 0)
goto out_err;
} while (ctx.changes > 0 && --tries > 0);
if (WARN_ONCE(ctx.changes > 0, "JIT offsets failed to converge"))
goto out_err;
build_epilogue(&ctx, MIPS_R_RA);
image_size = sizeof(u32) * ctx.jit_index;
header = bpf_jit_binary_alloc(image_size, &image_ptr,
sizeof(u32), jit_fill_hole);
if (header == NULL)
goto out_err;
ctx.target = (u32 *)image_ptr;
ctx.jit_index = 0;
build_prologue(&ctx);
if (build_body(&ctx) < 0)
goto out_err;
build_epilogue(&ctx, MIPS_R_RA);
set_convert_flag(&ctx, false);
bpf_prog_fill_jited_linfo(prog, &ctx.descriptors[1]);
if (bpf_jit_binary_lock_ro(header))
goto out_err;
flush_icache_range((unsigned long)header,
(unsigned long)&ctx.target[ctx.jit_index]);
if (bpf_jit_enable > 1)
bpf_jit_dump(prog->len, image_size, 2, ctx.target);
prog->bpf_func = (void *)ctx.target;
prog->jited = 1;
prog->jited_len = image_size;
out:
if (tmp_blinded)
bpf_jit_prog_release_other(prog, prog == orig_prog ?
tmp : orig_prog);
kfree(ctx.descriptors);
return prog;
out_err:
prog = orig_prog;
if (header)
bpf_jit_binary_free(header);
goto out;
}