|
| 1 | +/* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | +/* |
| 3 | + * The interface that a back-end should provide to bpf_jit_core.c. |
| 4 | + * |
| 5 | + * Copyright (c) 2024 Synopsys Inc. |
| 6 | + * Author: Shahab Vahedi <[email protected]> |
| 7 | + */ |
| 8 | + |
| 9 | +#ifndef _ARC_BPF_JIT_H |
| 10 | +#define _ARC_BPF_JIT_H |
| 11 | + |
| 12 | +#include <linux/bpf.h> |
| 13 | +#include <linux/filter.h> |
| 14 | + |
| 15 | +/* Print debug info and assert. */ |
| 16 | +//#define ARC_BPF_JIT_DEBUG |
| 17 | + |
| 18 | +/* Determine the address type of the target. */ |
| 19 | +#ifdef CONFIG_ISA_ARCV2 |
| 20 | +#define ARC_ADDR u32 |
| 21 | +#endif |
| 22 | + |
| 23 | +/* |
| 24 | + * For the translation of some BPF instructions, a temporary register |
| 25 | + * might be needed for some interim data. |
| 26 | + */ |
| 27 | +#define JIT_REG_TMP MAX_BPF_JIT_REG |
| 28 | + |
| 29 | +/************* Globals that have effects on code generation ***********/ |
| 30 | +/* |
| 31 | + * If "emit" is true, the instructions are actually generated. Else, the |
| 32 | + * generation part will be skipped and only the length of instruction is |
| 33 | + * returned by the responsible functions. |
| 34 | + */ |
| 35 | +extern bool emit; |
| 36 | + |
| 37 | +/* An indicator if zero-extend must be done for the 32-bit operations. */ |
| 38 | +extern bool zext_thyself; |
| 39 | + |
| 40 | +/************** Functions that the back-end must provide **************/ |
| 41 | +/* Extension for 32-bit operations. */ |
| 42 | +extern inline u8 zext(u8 *buf, u8 rd); |
| 43 | +/***** Moves *****/ |
| 44 | +extern u8 mov_r32(u8 *buf, u8 rd, u8 rs, u8 sign_ext); |
| 45 | +extern u8 mov_r32_i32(u8 *buf, u8 reg, s32 imm); |
| 46 | +extern u8 mov_r64(u8 *buf, u8 rd, u8 rs, u8 sign_ext); |
| 47 | +extern u8 mov_r64_i32(u8 *buf, u8 reg, s32 imm); |
| 48 | +extern u8 mov_r64_i64(u8 *buf, u8 reg, u32 lo, u32 hi); |
| 49 | +/***** Loads and stores *****/ |
| 50 | +extern u8 load_r(u8 *buf, u8 rd, u8 rs, s16 off, u8 size, bool sign_ext); |
| 51 | +extern u8 store_r(u8 *buf, u8 rd, u8 rs, s16 off, u8 size); |
| 52 | +extern u8 store_i(u8 *buf, s32 imm, u8 rd, s16 off, u8 size); |
| 53 | +/***** Addition *****/ |
| 54 | +extern u8 add_r32(u8 *buf, u8 rd, u8 rs); |
| 55 | +extern u8 add_r32_i32(u8 *buf, u8 rd, s32 imm); |
| 56 | +extern u8 add_r64(u8 *buf, u8 rd, u8 rs); |
| 57 | +extern u8 add_r64_i32(u8 *buf, u8 rd, s32 imm); |
| 58 | +/***** Subtraction *****/ |
| 59 | +extern u8 sub_r32(u8 *buf, u8 rd, u8 rs); |
| 60 | +extern u8 sub_r32_i32(u8 *buf, u8 rd, s32 imm); |
| 61 | +extern u8 sub_r64(u8 *buf, u8 rd, u8 rs); |
| 62 | +extern u8 sub_r64_i32(u8 *buf, u8 rd, s32 imm); |
| 63 | +/***** Multiplication *****/ |
| 64 | +extern u8 mul_r32(u8 *buf, u8 rd, u8 rs); |
| 65 | +extern u8 mul_r32_i32(u8 *buf, u8 rd, s32 imm); |
| 66 | +extern u8 mul_r64(u8 *buf, u8 rd, u8 rs); |
| 67 | +extern u8 mul_r64_i32(u8 *buf, u8 rd, s32 imm); |
| 68 | +/***** Division *****/ |
| 69 | +extern u8 div_r32(u8 *buf, u8 rd, u8 rs, bool sign_ext); |
| 70 | +extern u8 div_r32_i32(u8 *buf, u8 rd, s32 imm, bool sign_ext); |
| 71 | +/***** Remainder *****/ |
| 72 | +extern u8 mod_r32(u8 *buf, u8 rd, u8 rs, bool sign_ext); |
| 73 | +extern u8 mod_r32_i32(u8 *buf, u8 rd, s32 imm, bool sign_ext); |
| 74 | +/***** Bitwise AND *****/ |
| 75 | +extern u8 and_r32(u8 *buf, u8 rd, u8 rs); |
| 76 | +extern u8 and_r32_i32(u8 *buf, u8 rd, s32 imm); |
| 77 | +extern u8 and_r64(u8 *buf, u8 rd, u8 rs); |
| 78 | +extern u8 and_r64_i32(u8 *buf, u8 rd, s32 imm); |
| 79 | +/***** Bitwise OR *****/ |
| 80 | +extern u8 or_r32(u8 *buf, u8 rd, u8 rs); |
| 81 | +extern u8 or_r32_i32(u8 *buf, u8 rd, s32 imm); |
| 82 | +extern u8 or_r64(u8 *buf, u8 rd, u8 rs); |
| 83 | +extern u8 or_r64_i32(u8 *buf, u8 rd, s32 imm); |
| 84 | +/***** Bitwise XOR *****/ |
| 85 | +extern u8 xor_r32(u8 *buf, u8 rd, u8 rs); |
| 86 | +extern u8 xor_r32_i32(u8 *buf, u8 rd, s32 imm); |
| 87 | +extern u8 xor_r64(u8 *buf, u8 rd, u8 rs); |
| 88 | +extern u8 xor_r64_i32(u8 *buf, u8 rd, s32 imm); |
| 89 | +/***** Bitwise Negate *****/ |
| 90 | +extern u8 neg_r32(u8 *buf, u8 r); |
| 91 | +extern u8 neg_r64(u8 *buf, u8 r); |
| 92 | +/***** Bitwise left shift *****/ |
| 93 | +extern u8 lsh_r32(u8 *buf, u8 rd, u8 rs); |
| 94 | +extern u8 lsh_r32_i32(u8 *buf, u8 rd, u8 imm); |
| 95 | +extern u8 lsh_r64(u8 *buf, u8 rd, u8 rs); |
| 96 | +extern u8 lsh_r64_i32(u8 *buf, u8 rd, s32 imm); |
| 97 | +/***** Bitwise right shift (logical) *****/ |
| 98 | +extern u8 rsh_r32(u8 *buf, u8 rd, u8 rs); |
| 99 | +extern u8 rsh_r32_i32(u8 *buf, u8 rd, u8 imm); |
| 100 | +extern u8 rsh_r64(u8 *buf, u8 rd, u8 rs); |
| 101 | +extern u8 rsh_r64_i32(u8 *buf, u8 rd, s32 imm); |
| 102 | +/***** Bitwise right shift (arithmetic) *****/ |
| 103 | +extern u8 arsh_r32(u8 *buf, u8 rd, u8 rs); |
| 104 | +extern u8 arsh_r32_i32(u8 *buf, u8 rd, u8 imm); |
| 105 | +extern u8 arsh_r64(u8 *buf, u8 rd, u8 rs); |
| 106 | +extern u8 arsh_r64_i32(u8 *buf, u8 rd, s32 imm); |
| 107 | +/***** Frame related *****/ |
| 108 | +extern u32 mask_for_used_regs(u8 bpf_reg, bool is_call); |
| 109 | +extern u8 arc_prologue(u8 *buf, u32 usage, u16 frame_size); |
| 110 | +extern u8 arc_epilogue(u8 *buf, u32 usage, u16 frame_size); |
| 111 | +/***** Jumps *****/ |
| 112 | +/* |
| 113 | + * Different sorts of conditions (ARC enum as opposed to BPF_*). |
| 114 | + * |
| 115 | + * Do not change the order of enums here. ARC_CC_SLE+1 is used |
| 116 | + * to determine the number of JCCs. |
| 117 | + */ |
| 118 | +enum ARC_CC { |
| 119 | + ARC_CC_UGT = 0, /* unsigned > */ |
| 120 | + ARC_CC_UGE, /* unsigned >= */ |
| 121 | + ARC_CC_ULT, /* unsigned < */ |
| 122 | + ARC_CC_ULE, /* unsigned <= */ |
| 123 | + ARC_CC_SGT, /* signed > */ |
| 124 | + ARC_CC_SGE, /* signed >= */ |
| 125 | + ARC_CC_SLT, /* signed < */ |
| 126 | + ARC_CC_SLE, /* signed <= */ |
| 127 | + ARC_CC_AL, /* always */ |
| 128 | + ARC_CC_EQ, /* == */ |
| 129 | + ARC_CC_NE, /* != */ |
| 130 | + ARC_CC_SET, /* test */ |
| 131 | + ARC_CC_LAST |
| 132 | +}; |
| 133 | +/* |
| 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 are |
| 140 | + * merely offsets in the JIT stream and not absolute addresses. One |
| 141 | + * can look at them as addresses if the JIT code would start from |
| 142 | + * address 0x0000_0000. Nonetheless, since the buffer address for the |
| 143 | + * JIT is on a word-aligned address, this works and actually makes |
| 144 | + * things simpler (offsets are in the range of u32 which is more than |
| 145 | + * enough). |
| 146 | + */ |
| 147 | +extern bool check_jmp_32(u32 curr_off, u32 targ_off, u8 cond); |
| 148 | +extern bool check_jmp_64(u32 curr_off, u32 targ_off, u8 cond); |
| 149 | +extern u8 gen_jmp_32(u8 *buf, u8 rd, u8 rs, u8 cond, u32 c_off, u32 t_off); |
| 150 | +extern u8 gen_jmp_64(u8 *buf, u8 rd, u8 rs, u8 cond, u32 c_off, u32 t_off); |
| 151 | +/***** Miscellaneous *****/ |
| 152 | +extern u8 gen_func_call(u8 *buf, ARC_ADDR func_addr, bool external_func); |
| 153 | +extern u8 arc_to_bpf_return(u8 *buf); |
| 154 | +/* |
| 155 | + * Perform byte swaps on "rd" based on the "size". If "force" is |
| 156 | + * set to "true", do it unconditionally. Otherwise, consider the |
| 157 | + * desired "endian"ness and the host endianness. |
| 158 | + */ |
| 159 | +extern u8 gen_swap(u8 *buf, u8 rd, u8 size, u8 endian, bool force); |
| 160 | + |
| 161 | +#endif /* _ARC_BPF_JIT_H */ |
0 commit comments