/* SPDX-License-Identifier: GPL-2.0 */1/*2* The interface that a back-end should provide to bpf_jit_core.c.3*4* Copyright (c) 2024 Synopsys Inc.5* Author: Shahab Vahedi <[email protected]>6*/78#ifndef _ARC_BPF_JIT_H9#define _ARC_BPF_JIT_H1011#include <linux/bpf.h>12#include <linux/filter.h>1314/* Print debug info and assert. */15//#define ARC_BPF_JIT_DEBUG1617/* Determine the address type of the target. */18#ifdef CONFIG_ISA_ARCV219#define ARC_ADDR u3220#endif2122/*23* For the translation of some BPF instructions, a temporary register24* might be needed for some interim data.25*/26#define JIT_REG_TMP MAX_BPF_JIT_REG2728/*29* Buffer access: If buffer "b" is not NULL, advance by "n" bytes.30*31* This macro must be used in any place that potentially requires a32* "buf + len". This way, we make sure that the "buf" argument for33* the underlying "arc_*(buf, ...)" ends up as NULL instead of something34* like "0+4" or "0+8", etc. Those "arc_*()" functions check their "buf"35* value to decide if instructions should be emitted or not.36*/37#define BUF(b, n) (((b) != NULL) ? ((b) + (n)) : (b))3839/************** Functions that the back-end must provide **************/40/* Extension for 32-bit operations. */41u8 zext(u8 *buf, u8 rd);42/***** Moves *****/43u8 mov_r32(u8 *buf, u8 rd, u8 rs, u8 sign_ext);44u8 mov_r32_i32(u8 *buf, u8 reg, s32 imm);45u8 mov_r64(u8 *buf, u8 rd, u8 rs, u8 sign_ext);46u8 mov_r64_i32(u8 *buf, u8 reg, s32 imm);47u8 mov_r64_i64(u8 *buf, u8 reg, u32 lo, u32 hi);48/***** Loads and stores *****/49u8 load_r(u8 *buf, u8 rd, u8 rs, s16 off, u8 size, bool sign_ext);50u8 store_r(u8 *buf, u8 rd, u8 rs, s16 off, u8 size);51u8 store_i(u8 *buf, s32 imm, u8 rd, s16 off, u8 size);52/***** Addition *****/53u8 add_r32(u8 *buf, u8 rd, u8 rs);54u8 add_r32_i32(u8 *buf, u8 rd, s32 imm);55u8 add_r64(u8 *buf, u8 rd, u8 rs);56u8 add_r64_i32(u8 *buf, u8 rd, s32 imm);57/***** Subtraction *****/58u8 sub_r32(u8 *buf, u8 rd, u8 rs);59u8 sub_r32_i32(u8 *buf, u8 rd, s32 imm);60u8 sub_r64(u8 *buf, u8 rd, u8 rs);61u8 sub_r64_i32(u8 *buf, u8 rd, s32 imm);62/***** Multiplication *****/63u8 mul_r32(u8 *buf, u8 rd, u8 rs);64u8 mul_r32_i32(u8 *buf, u8 rd, s32 imm);65u8 mul_r64(u8 *buf, u8 rd, u8 rs);66u8 mul_r64_i32(u8 *buf, u8 rd, s32 imm);67/***** Division *****/68u8 div_r32(u8 *buf, u8 rd, u8 rs, bool sign_ext);69u8 div_r32_i32(u8 *buf, u8 rd, s32 imm, bool sign_ext);70/***** Remainder *****/71u8 mod_r32(u8 *buf, u8 rd, u8 rs, bool sign_ext);72u8 mod_r32_i32(u8 *buf, u8 rd, s32 imm, bool sign_ext);73/***** Bitwise AND *****/74u8 and_r32(u8 *buf, u8 rd, u8 rs);75u8 and_r32_i32(u8 *buf, u8 rd, s32 imm);76u8 and_r64(u8 *buf, u8 rd, u8 rs);77u8 and_r64_i32(u8 *buf, u8 rd, s32 imm);78/***** Bitwise OR *****/79u8 or_r32(u8 *buf, u8 rd, u8 rs);80u8 or_r32_i32(u8 *buf, u8 rd, s32 imm);81u8 or_r64(u8 *buf, u8 rd, u8 rs);82u8 or_r64_i32(u8 *buf, u8 rd, s32 imm);83/***** Bitwise XOR *****/84u8 xor_r32(u8 *buf, u8 rd, u8 rs);85u8 xor_r32_i32(u8 *buf, u8 rd, s32 imm);86u8 xor_r64(u8 *buf, u8 rd, u8 rs);87u8 xor_r64_i32(u8 *buf, u8 rd, s32 imm);88/***** Bitwise Negate *****/89u8 neg_r32(u8 *buf, u8 r);90u8 neg_r64(u8 *buf, u8 r);91/***** Bitwise left shift *****/92u8 lsh_r32(u8 *buf, u8 rd, u8 rs);93u8 lsh_r32_i32(u8 *buf, u8 rd, u8 imm);94u8 lsh_r64(u8 *buf, u8 rd, u8 rs);95u8 lsh_r64_i32(u8 *buf, u8 rd, s32 imm);96/***** Bitwise right shift (logical) *****/97u8 rsh_r32(u8 *buf, u8 rd, u8 rs);98u8 rsh_r32_i32(u8 *buf, u8 rd, u8 imm);99u8 rsh_r64(u8 *buf, u8 rd, u8 rs);100u8 rsh_r64_i32(u8 *buf, u8 rd, s32 imm);101/***** Bitwise right shift (arithmetic) *****/102u8 arsh_r32(u8 *buf, u8 rd, u8 rs);103u8 arsh_r32_i32(u8 *buf, u8 rd, u8 imm);104u8 arsh_r64(u8 *buf, u8 rd, u8 rs);105u8 arsh_r64_i32(u8 *buf, u8 rd, s32 imm);106/***** Frame related *****/107u32 mask_for_used_regs(u8 bpf_reg, bool is_call);108u8 arc_prologue(u8 *buf, u32 usage, u16 frame_size);109u8 arc_epilogue(u8 *buf, u32 usage, u16 frame_size);110/***** Jumps *****/111/*112* Different sorts of conditions (ARC enum as opposed to BPF_*).113*114* Do not change the order of enums here. ARC_CC_SLE+1 is used115* to determine the number of JCCs.116*/117enum ARC_CC {118ARC_CC_UGT = 0, /* unsigned > */119ARC_CC_UGE, /* unsigned >= */120ARC_CC_ULT, /* unsigned < */121ARC_CC_ULE, /* unsigned <= */122ARC_CC_SGT, /* signed > */123ARC_CC_SGE, /* signed >= */124ARC_CC_SLT, /* signed < */125ARC_CC_SLE, /* signed <= */126ARC_CC_AL, /* always */127ARC_CC_EQ, /* == */128ARC_CC_NE, /* != */129ARC_CC_SET, /* test */130ARC_CC_LAST131};132133/*134* A few notes:135*136* - check_jmp_*() are prerequisites before calling the gen_jmp_*().137* They return "true" if the jump is possible and "false" otherwise.138*139* - The notion of "*_off" is to emphasize that these parameters are140* merely offsets in the JIT stream and not absolute addresses. One141* can look at them as addresses if the JIT code would start from142* address 0x0000_0000. Nonetheless, since the buffer address for the143* JIT is on a word-aligned address, this works and actually makes144* things simpler (offsets are in the range of u32 which is more than145* enough).146*/147bool check_jmp_32(u32 curr_off, u32 targ_off, u8 cond);148bool check_jmp_64(u32 curr_off, u32 targ_off, u8 cond);149u8 gen_jmp_32(u8 *buf, u8 rd, u8 rs, u8 cond, u32 c_off, u32 t_off);150u8 gen_jmp_64(u8 *buf, u8 rd, u8 rs, u8 cond, u32 c_off, u32 t_off);151/***** Miscellaneous *****/152u8 gen_func_call(u8 *buf, ARC_ADDR func_addr, bool external_func);153u8 arc_to_bpf_return(u8 *buf);154/*155* - Perform byte swaps on "rd" based on the "size".156* - If "force" is set, do it unconditionally. Otherwise, consider the157* desired "endian"ness and the host endianness.158* - For data "size"s up to 32 bits, perform a zero-extension if asked159* by the "do_zext" boolean.160*/161u8 gen_swap(u8 *buf, u8 rd, u8 size, u8 endian, bool force, bool do_zext);162163#endif /* _ARC_BPF_JIT_H */164165166