/* SPDX-License-Identifier: GPL-2.0-only */1/*2* Just-In-Time compiler for eBPF bytecode on 32-bit and 64-bit MIPS.3*4* Copyright (c) 2021 Anyfi Networks AB.5* Author: Johan Almbladh <[email protected]>6*7* Based on code and ideas from8* Copyright (c) 2017 Cavium, Inc.9* Copyright (c) 2017 Shubham Bansal <[email protected]>10* Copyright (c) 2011 Mircea Gherzan <[email protected]>11*/1213#ifndef _BPF_JIT_COMP_H14#define _BPF_JIT_COMP_H1516/* MIPS registers */17#define MIPS_R_ZERO 0 /* Const zero */18#define MIPS_R_AT 1 /* Asm temp */19#define MIPS_R_V0 2 /* Result */20#define MIPS_R_V1 3 /* Result */21#define MIPS_R_A0 4 /* Argument */22#define MIPS_R_A1 5 /* Argument */23#define MIPS_R_A2 6 /* Argument */24#define MIPS_R_A3 7 /* Argument */25#define MIPS_R_A4 8 /* Arg (n64) */26#define MIPS_R_A5 9 /* Arg (n64) */27#define MIPS_R_A6 10 /* Arg (n64) */28#define MIPS_R_A7 11 /* Arg (n64) */29#define MIPS_R_T0 8 /* Temp (o32) */30#define MIPS_R_T1 9 /* Temp (o32) */31#define MIPS_R_T2 10 /* Temp (o32) */32#define MIPS_R_T3 11 /* Temp (o32) */33#define MIPS_R_T4 12 /* Temporary */34#define MIPS_R_T5 13 /* Temporary */35#define MIPS_R_T6 14 /* Temporary */36#define MIPS_R_T7 15 /* Temporary */37#define MIPS_R_S0 16 /* Saved */38#define MIPS_R_S1 17 /* Saved */39#define MIPS_R_S2 18 /* Saved */40#define MIPS_R_S3 19 /* Saved */41#define MIPS_R_S4 20 /* Saved */42#define MIPS_R_S5 21 /* Saved */43#define MIPS_R_S6 22 /* Saved */44#define MIPS_R_S7 23 /* Saved */45#define MIPS_R_T8 24 /* Temporary */46#define MIPS_R_T9 25 /* Temporary */47/* MIPS_R_K0 26 Reserved */48/* MIPS_R_K1 27 Reserved */49#define MIPS_R_GP 28 /* Global ptr */50#define MIPS_R_SP 29 /* Stack ptr */51#define MIPS_R_FP 30 /* Frame ptr */52#define MIPS_R_RA 31 /* Return */5354/*55* Jump address mask for immediate jumps. The four most significant bits56* must be equal to PC.57*/58#define MIPS_JMP_MASK 0x0fffffffUL5960/* Maximum number of iterations in offset table computation */61#define JIT_MAX_ITERATIONS 86263/*64* Jump pseudo-instructions used internally65* for branch conversion and branch optimization.66*/67#define JIT_JNSET 0xe068#define JIT_JNOP 0xf06970/* Descriptor flag for PC-relative branch conversion */71#define JIT_DESC_CONVERT BIT(31)7273/* JIT context for an eBPF program */74struct jit_context {75struct bpf_prog *program; /* The eBPF program being JITed */76u32 *descriptors; /* eBPF to JITed CPU insn descriptors */77u32 *target; /* JITed code buffer */78u32 bpf_index; /* Index of current BPF program insn */79u32 jit_index; /* Index of current JIT target insn */80u32 changes; /* Number of PC-relative branch conv */81u32 accessed; /* Bit mask of read eBPF registers */82u32 clobbered; /* Bit mask of modified CPU registers */83u32 stack_size; /* Total allocated stack size in bytes */84u32 saved_size; /* Size of callee-saved registers */85u32 stack_used; /* Stack size used for function calls */86};8788/* Emit the instruction if the JIT memory space has been allocated */89#define __emit(ctx, func, ...) \90do { \91if ((ctx)->target != NULL) { \92u32 *p = &(ctx)->target[ctx->jit_index]; \93uasm_i_##func(&p, ##__VA_ARGS__); \94} \95(ctx)->jit_index++; \96} while (0)97#define emit(...) __emit(__VA_ARGS__)9899/* Workaround for R10000 ll/sc errata */100#ifdef CONFIG_WAR_R10000_LLSC101#define LLSC_beqz beqzl102#else103#define LLSC_beqz beqz104#endif105106/* Workaround for Loongson-3 ll/sc errata */107#ifdef CONFIG_CPU_LOONGSON3_WORKAROUNDS108#define LLSC_sync(ctx) emit(ctx, sync, 0)109#define LLSC_offset 4110#else111#define LLSC_sync(ctx)112#define LLSC_offset 0113#endif114115/* Workaround for Loongson-2F jump errata */116#ifdef CONFIG_CPU_JUMP_WORKAROUNDS117#define JALR_MASK 0xffffffffcfffffffULL118#else119#define JALR_MASK (~0ULL)120#endif121122/*123* Mark a BPF register as accessed, it needs to be124* initialized by the program if expected, e.g. FP.125*/126static inline void access_reg(struct jit_context *ctx, u8 reg)127{128ctx->accessed |= BIT(reg);129}130131/*132* Mark a CPU register as clobbered, it needs to be133* saved/restored by the program if callee-saved.134*/135static inline void clobber_reg(struct jit_context *ctx, u8 reg)136{137ctx->clobbered |= BIT(reg);138}139140/*141* Push registers on the stack, starting at a given depth from the stack142* pointer and increasing. The next depth to be written is returned.143*/144int push_regs(struct jit_context *ctx, u32 mask, u32 excl, int depth);145146/*147* Pop registers from the stack, starting at a given depth from the stack148* pointer and increasing. The next depth to be read is returned.149*/150int pop_regs(struct jit_context *ctx, u32 mask, u32 excl, int depth);151152/* Compute the 28-bit jump target address from a BPF program location */153int get_target(struct jit_context *ctx, u32 loc);154155/* Compute the PC-relative offset to relative BPF program offset */156int get_offset(const struct jit_context *ctx, int off);157158/* dst = imm (32-bit) */159void emit_mov_i(struct jit_context *ctx, u8 dst, s32 imm);160161/* dst = src (32-bit) */162void emit_mov_r(struct jit_context *ctx, u8 dst, u8 src);163164/* Validate ALU/ALU64 immediate range */165bool valid_alu_i(u8 op, s32 imm);166167/* Rewrite ALU/ALU64 immediate operation */168bool rewrite_alu_i(u8 op, s32 imm, u8 *alu, s32 *val);169170/* ALU immediate operation (32-bit) */171void emit_alu_i(struct jit_context *ctx, u8 dst, s32 imm, u8 op);172173/* ALU register operation (32-bit) */174void emit_alu_r(struct jit_context *ctx, u8 dst, u8 src, u8 op);175176/* Atomic read-modify-write (32-bit) */177void emit_atomic_r(struct jit_context *ctx, u8 dst, u8 src, s16 off, u8 code);178179/* Atomic compare-and-exchange (32-bit) */180void emit_cmpxchg_r(struct jit_context *ctx, u8 dst, u8 src, u8 res, s16 off);181182/* Swap bytes and truncate a register word or half word */183void emit_bswap_r(struct jit_context *ctx, u8 dst, u32 width);184185/* Validate JMP/JMP32 immediate range */186bool valid_jmp_i(u8 op, s32 imm);187188/* Prepare a PC-relative jump operation with immediate conditional */189void setup_jmp_i(struct jit_context *ctx, s32 imm, u8 width,190u8 bpf_op, s16 bpf_off, u8 *jit_op, s32 *jit_off);191192/* Prepare a PC-relative jump operation with register conditional */193void setup_jmp_r(struct jit_context *ctx, bool same_reg,194u8 bpf_op, s16 bpf_off, u8 *jit_op, s32 *jit_off);195196/* Finish a PC-relative jump operation */197int finish_jmp(struct jit_context *ctx, u8 jit_op, s16 bpf_off);198199/* Conditional JMP/JMP32 immediate */200void emit_jmp_i(struct jit_context *ctx, u8 dst, s32 imm, s32 off, u8 op);201202/* Conditional JMP/JMP32 register */203void emit_jmp_r(struct jit_context *ctx, u8 dst, u8 src, s32 off, u8 op);204205/* Jump always */206int emit_ja(struct jit_context *ctx, s16 off);207208/* Jump to epilogue */209int emit_exit(struct jit_context *ctx);210211/*212* Build program prologue to set up the stack and registers.213* This function is implemented separately for 32-bit and 64-bit JITs.214*/215void build_prologue(struct jit_context *ctx);216217/*218* Build the program epilogue to restore the stack and registers.219* This function is implemented separately for 32-bit and 64-bit JITs.220*/221void build_epilogue(struct jit_context *ctx, int dest_reg);222223/*224* Convert an eBPF instruction to native instruction, i.e225* JITs an eBPF instruction.226* Returns :227* 0 - Successfully JITed an 8-byte eBPF instruction228* >0 - Successfully JITed a 16-byte eBPF instruction229* <0 - Failed to JIT.230* This function is implemented separately for 32-bit and 64-bit JITs.231*/232int build_insn(const struct bpf_insn *insn, struct jit_context *ctx);233234#endif /* _BPF_JIT_COMP_H */235236237