// SPDX-License-Identifier: GPL-2.01/*2* The ARCv2 backend of Just-In-Time compiler for eBPF bytecode.3*4* Copyright (c) 2024 Synopsys Inc.5* Author: Shahab Vahedi <[email protected]>6*/7#include <linux/bug.h>8#include "bpf_jit.h"910/* ARC core registers. */11enum {12ARC_R_0, ARC_R_1, ARC_R_2, ARC_R_3, ARC_R_4, ARC_R_5,13ARC_R_6, ARC_R_7, ARC_R_8, ARC_R_9, ARC_R_10, ARC_R_11,14ARC_R_12, ARC_R_13, ARC_R_14, ARC_R_15, ARC_R_16, ARC_R_17,15ARC_R_18, ARC_R_19, ARC_R_20, ARC_R_21, ARC_R_22, ARC_R_23,16ARC_R_24, ARC_R_25, ARC_R_26, ARC_R_FP, ARC_R_SP, ARC_R_ILINK,17ARC_R_30, ARC_R_BLINK,18/*19* Having ARC_R_IMM encoded as source register means there is an20* immediate that must be interpreted from the next 4 bytes. If21* encoded as the destination register though, it implies that the22* output of the operation is not assigned to any register. The23* latter is helpful if we only care about updating the CPU status24* flags.25*/26ARC_R_IMM = 6227};2829/*30* Remarks about the rationale behind the chosen mapping:31*32* - BPF_REG_{1,2,3,4} are the argument registers and must be mapped to33* argument registers in ARCv2 ABI: r0-r7. The r7 registers is the last34* argument register in the ABI. Therefore BPF_REG_5, as the fifth35* argument, must be pushed onto the stack. This is a must for calling36* in-kernel functions.37*38* - In ARCv2 ABI, the return value is in r0 for 32-bit results and (r1,r0)39* for 64-bit results. However, because they're already used for BPF_REG_1,40* the next available scratch registers, r8 and r9, are the best candidates41* for BPF_REG_0. After a "call" to a(n) (in-kernel) function, the result42* is "mov"ed to these registers. At a BPF_EXIT, their value is "mov"ed to43* (r1,r0).44* It is worth mentioning that scratch registers are the best choice for45* BPF_REG_0, because it is very popular in BPF instruction encoding.46*47* - JIT_REG_TMP is an artifact needed to translate some BPF instructions.48* Its life span is one single BPF instruction. Since during the49* analyze_reg_usage(), it is not known if temporary registers are used,50* it is mapped to ARC's scratch registers: r10 and r11. Therefore, they51* don't matter in analysing phase and don't need saving. This temporary52* register is added as yet another index in the bpf2arc array, so it will53* unfold like the rest of registers during the code generation process.54*55* - Mapping of callee-saved BPF registers, BPF_REG_{6,7,8,9}, starts from56* (r15,r14) register pair. The (r13,r12) is not a good choice, because57* in ARCv2 ABI, r12 is not a callee-saved register and this can cause58* problem when calling an in-kernel function. Theoretically, the mapping59* could start from (r14,r13), but it is not a conventional ARCv2 register60* pair. To have a future proof design, I opted for this arrangement.61* If/when we decide to add ARCv2 instructions that do use register pairs,62* the mapping, hopefully, doesn't need to be revisited.63*/64static const u8 bpf2arc[][2] = {65/* Return value from in-kernel function, and exit value from eBPF */66[BPF_REG_0] = {ARC_R_8, ARC_R_9},67/* Arguments from eBPF program to in-kernel function */68[BPF_REG_1] = {ARC_R_0, ARC_R_1},69[BPF_REG_2] = {ARC_R_2, ARC_R_3},70[BPF_REG_3] = {ARC_R_4, ARC_R_5},71[BPF_REG_4] = {ARC_R_6, ARC_R_7},72/* Remaining arguments, to be passed on the stack per 32-bit ABI */73[BPF_REG_5] = {ARC_R_22, ARC_R_23},74/* Callee-saved registers that in-kernel function will preserve */75[BPF_REG_6] = {ARC_R_14, ARC_R_15},76[BPF_REG_7] = {ARC_R_16, ARC_R_17},77[BPF_REG_8] = {ARC_R_18, ARC_R_19},78[BPF_REG_9] = {ARC_R_20, ARC_R_21},79/* Read-only frame pointer to access the eBPF stack. 32-bit only. */80[BPF_REG_FP] = {ARC_R_FP, },81/* Register for blinding constants */82[BPF_REG_AX] = {ARC_R_24, ARC_R_25},83/* Temporary registers for internal use */84[JIT_REG_TMP] = {ARC_R_10, ARC_R_11}85};8687#define ARC_CALLEE_SAVED_REG_FIRST ARC_R_1388#define ARC_CALLEE_SAVED_REG_LAST ARC_R_258990#define REG_LO(r) (bpf2arc[(r)][0])91#define REG_HI(r) (bpf2arc[(r)][1])9293/*94* To comply with ARCv2 ABI, BPF's arg5 must be put on stack. After which,95* the stack needs to be restored by ARG5_SIZE.96*/97#define ARG5_SIZE 89899/* Instruction lengths in bytes. */100enum {101INSN_len_normal = 4, /* Normal instructions length. */102INSN_len_imm = 4 /* Length of an extra 32-bit immediate. */103};104105/* ZZ defines the size of operation in encodings that it is used. */106enum {107ZZ_1_byte = 1,108ZZ_2_byte = 2,109ZZ_4_byte = 0,110ZZ_8_byte = 3111};112113/*114* AA is mostly about address write back mode. It determines if the115* address in question should be updated before usage or after:116* addr += offset; data = *addr;117* data = *addr; addr += offset;118*119* In "scaling" mode, the effective address will become the sum120* of "address" + "index"*"size". The "size" is specified by the121* "ZZ" field. There is no write back when AA is set for scaling:122* data = *(addr + offset<<zz)123*/124enum {125AA_none = 0,126AA_pre = 1, /* in assembly known as "a/aw". */127AA_post = 2, /* in assembly known as "ab". */128AA_scale = 3 /* in assembly known as "as". */129};130131/* X flag determines the mode of extension. */132enum {133X_zero = 0,134X_sign = 1135};136137/* Condition codes. */138enum {139CC_always = 0, /* condition is true all the time */140CC_equal = 1, /* if status32.z flag is set */141CC_unequal = 2, /* if status32.z flag is clear */142CC_positive = 3, /* if status32.n flag is clear */143CC_negative = 4, /* if status32.n flag is set */144CC_less_u = 5, /* less than (unsigned) */145CC_less_eq_u = 14, /* less than or equal (unsigned) */146CC_great_eq_u = 6, /* greater than or equal (unsigned) */147CC_great_u = 13, /* greater than (unsigned) */148CC_less_s = 11, /* less than (signed) */149CC_less_eq_s = 12, /* less than or equal (signed) */150CC_great_eq_s = 10, /* greater than or equal (signed) */151CC_great_s = 9 /* greater than (signed) */152};153154#define IN_U6_RANGE(x) ((x) <= (0x40 - 1) && (x) >= 0)155#define IN_S9_RANGE(x) ((x) <= (0x100 - 1) && (x) >= -0x100)156#define IN_S12_RANGE(x) ((x) <= (0x800 - 1) && (x) >= -0x800)157#define IN_S21_RANGE(x) ((x) <= (0x100000 - 1) && (x) >= -0x100000)158#define IN_S25_RANGE(x) ((x) <= (0x1000000 - 1) && (x) >= -0x1000000)159160/* Operands in most of the encodings. */161#define OP_A(x) ((x) & 0x03f)162#define OP_B(x) ((((x) & 0x07) << 24) | (((x) & 0x38) << 9))163#define OP_C(x) (((x) & 0x03f) << 6)164#define OP_IMM (OP_C(ARC_R_IMM))165#define COND(x) (OP_A((x) & 31))166#define FLAG(x) (((x) & 1) << 15)167168/*169* The 4-byte encoding of "mov b,c":170*171* 0010_0bbb 0000_1010 0BBB_cccc cc00_0000172*173* b: BBBbbb destination register174* c: cccccc source register175*/176#define OPC_MOV 0x200a0000177178/*179* The 4-byte encoding of "mov b,s12" (used for moving small immediates):180*181* 0010_0bbb 1000_1010 0BBB_ssss ssSS_SSSS182*183* b: BBBbbb destination register184* s: SSSSSSssssss source immediate (signed)185*/186#define OPC_MOVI 0x208a0000187#define MOVI_S12(x) ((((x) & 0xfc0) >> 6) | (((x) & 0x3f) << 6))188189/*190* The 4-byte encoding of "mov[.qq] b,u6", used for conditional191* moving of even smaller immediates:192*193* 0010_0bbb 1100_1010 0BBB_cccc cciq_qqqq194*195* qq: qqqqq condition code196* i: If set, c is considered a 6-bit immediate, else a reg.197*198* b: BBBbbb destination register199* c: cccccc source200*/201#define OPC_MOV_CC 0x20ca0000202#define MOV_CC_I BIT(5)203#define OPC_MOVU_CC (OPC_MOV_CC | MOV_CC_I)204205/*206* The 4-byte encoding of "sexb b,c" (8-bit sign extension):207*208* 0010_0bbb 0010_1111 0BBB_cccc cc00_0101209*210* b: BBBbbb destination register211* c: cccccc source register212*/213#define OPC_SEXB 0x202f0005214215/*216* The 4-byte encoding of "sexh b,c" (16-bit sign extension):217*218* 0010_0bbb 0010_1111 0BBB_cccc cc00_0110219*220* b: BBBbbb destination register221* c: cccccc source register222*/223#define OPC_SEXH 0x202f0006224225/*226* The 4-byte encoding of "ld[zz][.x][.aa] c,[b,s9]":227*228* 0001_0bbb ssss_ssss SBBB_0aaz zxcc_cccc229*230* zz: size mode231* aa: address write back mode232* x: extension mode233*234* s9: S_ssss_ssss 9-bit signed number235* b: BBBbbb source reg for address236* c: cccccc destination register237*/238#define OPC_LOAD 0x10000000239#define LOAD_X(x) ((x) << 6)240#define LOAD_ZZ(x) ((x) << 7)241#define LOAD_AA(x) ((x) << 9)242#define LOAD_S9(x) ((((x) & 0x0ff) << 16) | (((x) & 0x100) << 7))243#define LOAD_C(x) ((x) & 0x03f)244/* Unsigned and signed loads. */245#define OPC_LDU (OPC_LOAD | LOAD_X(X_zero))246#define OPC_LDS (OPC_LOAD | LOAD_X(X_sign))247/* 32-bit load. */248#define OPC_LD32 (OPC_LDU | LOAD_ZZ(ZZ_4_byte))249/* "pop reg" is merely a "ld.ab reg,[sp,4]". */250#define OPC_POP \251(OPC_LD32 | LOAD_AA(AA_post) | LOAD_S9(4) | OP_B(ARC_R_SP))252253/*254* The 4-byte encoding of "st[zz][.aa] c,[b,s9]":255*256* 0001_1bbb ssss_ssss SBBB_cccc cc0a_azz0257*258* zz: zz size mode259* aa: aa address write back mode260*261* s9: S_ssss_ssss 9-bit signed number262* b: BBBbbb source reg for address263* c: cccccc source reg to be stored264*/265#define OPC_STORE 0x18000000266#define STORE_ZZ(x) ((x) << 1)267#define STORE_AA(x) ((x) << 3)268#define STORE_S9(x) ((((x) & 0x0ff) << 16) | (((x) & 0x100) << 7))269/* 32-bit store. */270#define OPC_ST32 (OPC_STORE | STORE_ZZ(ZZ_4_byte))271/* "push reg" is merely a "st.aw reg,[sp,-4]". */272#define OPC_PUSH \273(OPC_ST32 | STORE_AA(AA_pre) | STORE_S9(-4) | OP_B(ARC_R_SP))274275/*276* The 4-byte encoding of "add a,b,c":277*278* 0010_0bbb 0i00_0000 fBBB_cccc ccaa_aaaa279*280* f: indicates if flags (carry, etc.) should be updated281* i: If set, c is considered a 6-bit immediate, else a reg.282*283* a: aaaaaa result284* b: BBBbbb the 1st input operand285* c: cccccc the 2nd input operand286*/287#define OPC_ADD 0x20000000288/* Addition with updating the pertinent flags in "status32" register. */289#define OPC_ADDF (OPC_ADD | FLAG(1))290#define ADDI BIT(22)291#define ADDI_U6(x) OP_C(x)292#define OPC_ADDI (OPC_ADD | ADDI)293#define OPC_ADDIF (OPC_ADDI | FLAG(1))294#define OPC_ADD_I (OPC_ADD | OP_IMM)295296/*297* The 4-byte encoding of "adc a,b,c" (addition with carry):298*299* 0010_0bbb 0i00_0001 0BBB_cccc ccaa_aaaa300*301* i: if set, c is considered a 6-bit immediate, else a reg.302*303* a: aaaaaa result304* b: BBBbbb the 1st input operand305* c: cccccc the 2nd input operand306*/307#define OPC_ADC 0x20010000308#define ADCI BIT(22)309#define ADCI_U6(x) OP_C(x)310#define OPC_ADCI (OPC_ADC | ADCI)311312/*313* The 4-byte encoding of "sub a,b,c":314*315* 0010_0bbb 0i00_0010 fBBB_cccc ccaa_aaaa316*317* f: indicates if flags (carry, etc.) should be updated318* i: if set, c is considered a 6-bit immediate, else a reg.319*320* a: aaaaaa result321* b: BBBbbb the 1st input operand322* c: cccccc the 2nd input operand323*/324#define OPC_SUB 0x20020000325/* Subtraction with updating the pertinent flags in "status32" register. */326#define OPC_SUBF (OPC_SUB | FLAG(1))327#define SUBI BIT(22)328#define SUBI_U6(x) OP_C(x)329#define OPC_SUBI (OPC_SUB | SUBI)330#define OPC_SUB_I (OPC_SUB | OP_IMM)331332/*333* The 4-byte encoding of "sbc a,b,c" (subtraction with carry):334*335* 0010_0bbb 0000_0011 fBBB_cccc ccaa_aaaa336*337* f: indicates if flags (carry, etc.) should be updated338*339* a: aaaaaa result340* b: BBBbbb the 1st input operand341* c: cccccc the 2nd input operand342*/343#define OPC_SBC 0x20030000344345/*346* The 4-byte encoding of "cmp[.qq] b,c":347*348* 0010_0bbb 1100_1100 1BBB_cccc cc0q_qqqq349*350* qq: qqqqq condition code351*352* b: BBBbbb the 1st operand353* c: cccccc the 2nd operand354*/355#define OPC_CMP 0x20cc8000356357/*358* The 4-byte encoding of "neg a,b":359*360* 0010_0bbb 0100_1110 0BBB_0000 00aa_aaaa361*362* a: aaaaaa result363* b: BBBbbb input364*/365#define OPC_NEG 0x204e0000366367/*368* The 4-byte encoding of "mpy a,b,c".369* mpy is the signed 32-bit multiplication with the lower 32-bit370* of the product as the result.371*372* 0010_0bbb 0001_1010 0BBB_cccc ccaa_aaaa373*374* a: aaaaaa result375* b: BBBbbb the 1st input operand376* c: cccccc the 2nd input operand377*/378#define OPC_MPY 0x201a0000379#define OPC_MPYI (OPC_MPY | OP_IMM)380381/*382* The 4-byte encoding of "mpydu a,b,c".383* mpydu is the unsigned 32-bit multiplication with the lower 32-bit of384* the product in register "a" and the higher 32-bit in register "a+1".385*386* 0010_1bbb 0001_1001 0BBB_cccc ccaa_aaaa387*388* a: aaaaaa 64-bit result in registers (R_a+1,R_a)389* b: BBBbbb the 1st input operand390* c: cccccc the 2nd input operand391*/392#define OPC_MPYDU 0x28190000393#define OPC_MPYDUI (OPC_MPYDU | OP_IMM)394395/*396* The 4-byte encoding of "divu a,b,c" (unsigned division):397*398* 0010_1bbb 0000_0101 0BBB_cccc ccaa_aaaa399*400* a: aaaaaa result (quotient)401* b: BBBbbb the 1st input operand402* c: cccccc the 2nd input operand (divisor)403*/404#define OPC_DIVU 0x28050000405#define OPC_DIVUI (OPC_DIVU | OP_IMM)406407/*408* The 4-byte encoding of "div a,b,c" (signed division):409*410* 0010_1bbb 0000_0100 0BBB_cccc ccaa_aaaa411*412* a: aaaaaa result (quotient)413* b: BBBbbb the 1st input operand414* c: cccccc the 2nd input operand (divisor)415*/416#define OPC_DIVS 0x28040000417#define OPC_DIVSI (OPC_DIVS | OP_IMM)418419/*420* The 4-byte encoding of "remu a,b,c" (unsigned remainder):421*422* 0010_1bbb 0000_1001 0BBB_cccc ccaa_aaaa423*424* a: aaaaaa result (remainder)425* b: BBBbbb the 1st input operand426* c: cccccc the 2nd input operand (divisor)427*/428#define OPC_REMU 0x28090000429#define OPC_REMUI (OPC_REMU | OP_IMM)430431/*432* The 4-byte encoding of "rem a,b,c" (signed remainder):433*434* 0010_1bbb 0000_1000 0BBB_cccc ccaa_aaaa435*436* a: aaaaaa result (remainder)437* b: BBBbbb the 1st input operand438* c: cccccc the 2nd input operand (divisor)439*/440#define OPC_REMS 0x28080000441#define OPC_REMSI (OPC_REMS | OP_IMM)442443/*444* The 4-byte encoding of "and a,b,c":445*446* 0010_0bbb 0000_0100 fBBB_cccc ccaa_aaaa447*448* f: indicates if zero and negative flags should be updated449*450* a: aaaaaa result451* b: BBBbbb the 1st input operand452* c: cccccc the 2nd input operand453*/454#define OPC_AND 0x20040000455#define OPC_ANDI (OPC_AND | OP_IMM)456457/*458* The 4-byte encoding of "tst[.qq] b,c".459* Checks if the two input operands have any bit set at the same460* position.461*462* 0010_0bbb 1100_1011 1BBB_cccc cc0q_qqqq463*464* qq: qqqqq condition code465*466* b: BBBbbb the 1st input operand467* c: cccccc the 2nd input operand468*/469#define OPC_TST 0x20cb8000470471/*472* The 4-byte encoding of "or a,b,c":473*474* 0010_0bbb 0000_0101 0BBB_cccc ccaa_aaaa475*476* a: aaaaaa result477* b: BBBbbb the 1st input operand478* c: cccccc the 2nd input operand479*/480#define OPC_OR 0x20050000481#define OPC_ORI (OPC_OR | OP_IMM)482483/*484* The 4-byte encoding of "xor a,b,c":485*486* 0010_0bbb 0000_0111 0BBB_cccc ccaa_aaaa487*488* a: aaaaaa result489* b: BBBbbb the 1st input operand490* c: cccccc the 2nd input operand491*/492#define OPC_XOR 0x20070000493#define OPC_XORI (OPC_XOR | OP_IMM)494495/*496* The 4-byte encoding of "not b,c":497*498* 0010_0bbb 0010_1111 0BBB_cccc cc00_1010499*500* b: BBBbbb result501* c: cccccc input502*/503#define OPC_NOT 0x202f000a504505/*506* The 4-byte encoding of "btst b,u6":507*508* 0010_0bbb 0101_0001 1BBB_uuuu uu00_0000509*510* b: BBBbbb input number to check511* u6: uuuuuu 6-bit unsigned number specifying bit position to check512*/513#define OPC_BTSTU6 0x20518000514#define BTST_U6(x) (OP_C((x) & 63))515516/*517* The 4-byte encoding of "asl[.qq] b,b,c" (arithmetic shift left):518*519* 0010_1bbb 0i00_0000 0BBB_cccc ccaa_aaaa520*521* i: if set, c is considered a 5-bit immediate, else a reg.522*523* b: BBBbbb result and the first operand (number to be shifted)524* c: cccccc amount to be shifted525*/526#define OPC_ASL 0x28000000527#define ASL_I BIT(22)528#define ASLI_U6(x) OP_C((x) & 31)529#define OPC_ASLI (OPC_ASL | ASL_I)530531/*532* The 4-byte encoding of "asr a,b,c" (arithmetic shift right):533*534* 0010_1bbb 0i00_0010 0BBB_cccc ccaa_aaaa535*536* i: if set, c is considered a 6-bit immediate, else a reg.537*538* a: aaaaaa result539* b: BBBbbb first input: number to be shifted540* c: cccccc second input: amount to be shifted541*/542#define OPC_ASR 0x28020000543#define ASR_I ASL_I544#define ASRI_U6(x) ASLI_U6(x)545#define OPC_ASRI (OPC_ASR | ASR_I)546547/*548* The 4-byte encoding of "lsr a,b,c" (logical shift right):549*550* 0010_1bbb 0i00_0001 0BBB_cccc ccaa_aaaa551*552* i: if set, c is considered a 6-bit immediate, else a reg.553*554* a: aaaaaa result555* b: BBBbbb first input: number to be shifted556* c: cccccc second input: amount to be shifted557*/558#define OPC_LSR 0x28010000559#define LSR_I ASL_I560#define LSRI_U6(x) ASLI_U6(x)561#define OPC_LSRI (OPC_LSR | LSR_I)562563/*564* The 4-byte encoding of "swape b,c":565*566* 0010_1bbb 0010_1111 0bbb_cccc cc00_1001567*568* b: BBBbbb destination register569* c: cccccc source register570*/571#define OPC_SWAPE 0x282f0009572573/*574* Encoding for jump to an address in register:575* j reg_c576*577* 0010_0000 1110_0000 0000_cccc cc00_0000578*579* c: cccccc register holding the destination address580*/581#define OPC_JMP 0x20e00000582/* Jump to "branch-and-link" register, which effectively is a "return". */583#define OPC_J_BLINK (OPC_JMP | OP_C(ARC_R_BLINK))584585/*586* Encoding for jump-and-link to an address in register:587* jl reg_c588*589* 0010_0000 0010_0010 0000_cccc cc00_0000590*591* c: cccccc register holding the destination address592*/593#define OPC_JL 0x20220000594595/*596* Encoding for (conditional) branch to an offset from the current location597* that is word aligned: (PC & 0xffff_fffc) + s21598* B[qq] s21599*600* 0000_0sss ssss_sss0 SSSS_SSSS SS0q_qqqq601*602* qq: qqqqq condition code603* s21: SSSS SSSS_SSss ssss_ssss The displacement (21-bit signed)604*605* The displacement is supposed to be 16-bit (2-byte) aligned. Therefore,606* it should be a multiple of 2. Hence, there is an implied '0' bit at its607* LSB: S_SSSS SSSS_Ssss ssss_sss0608*/609#define OPC_BCC 0x00000000610#define BCC_S21(d) ((((d) & 0x7fe) << 16) | (((d) & 0x1ff800) >> 5))611612/*613* Encoding for unconditional branch to an offset from the current location614* that is word aligned: (PC & 0xffff_fffc) + s25615* B s25616*617* 0000_0sss ssss_sss1 SSSS_SSSS SS00_TTTT618*619* s25: TTTT SSSS SSSS_SSss ssss_ssss The displacement (25-bit signed)620*621* The displacement is supposed to be 16-bit (2-byte) aligned. Therefore,622* it should be a multiple of 2. Hence, there is an implied '0' bit at its623* LSB: T TTTS_SSSS SSSS_Ssss ssss_sss0624*/625#define OPC_B 0x00010000626#define B_S25(d) ((((d) & 0x1e00000) >> 21) | BCC_S21(d))627628static inline void emit_2_bytes(u8 *buf, u16 bytes)629{630*((u16 *)buf) = bytes;631}632633static inline void emit_4_bytes(u8 *buf, u32 bytes)634{635emit_2_bytes(buf, bytes >> 16);636emit_2_bytes(buf + 2, bytes & 0xffff);637}638639static inline u8 bpf_to_arc_size(u8 size)640{641switch (size) {642case BPF_B:643return ZZ_1_byte;644case BPF_H:645return ZZ_2_byte;646case BPF_W:647return ZZ_4_byte;648case BPF_DW:649return ZZ_8_byte;650default:651return ZZ_4_byte;652}653}654655/************** Encoders (Deal with ARC regs) ************/656657/* Move an immediate to register with a 4-byte instruction. */658static u8 arc_movi_r(u8 *buf, u8 reg, s16 imm)659{660const u32 insn = OPC_MOVI | OP_B(reg) | MOVI_S12(imm);661662if (buf)663emit_4_bytes(buf, insn);664return INSN_len_normal;665}666667/* rd <- rs */668static u8 arc_mov_r(u8 *buf, u8 rd, u8 rs)669{670const u32 insn = OPC_MOV | OP_B(rd) | OP_C(rs);671672if (buf)673emit_4_bytes(buf, insn);674return INSN_len_normal;675}676677/* The emitted code may have different sizes based on "imm". */678static u8 arc_mov_i(u8 *buf, u8 rd, s32 imm)679{680const u32 insn = OPC_MOV | OP_B(rd) | OP_IMM;681682if (IN_S12_RANGE(imm))683return arc_movi_r(buf, rd, imm);684685if (buf) {686emit_4_bytes(buf, insn);687emit_4_bytes(buf + INSN_len_normal, imm);688}689return INSN_len_normal + INSN_len_imm;690}691692/* The emitted code will always have the same size (8). */693static u8 arc_mov_i_fixed(u8 *buf, u8 rd, s32 imm)694{695const u32 insn = OPC_MOV | OP_B(rd) | OP_IMM;696697if (buf) {698emit_4_bytes(buf, insn);699emit_4_bytes(buf + INSN_len_normal, imm);700}701return INSN_len_normal + INSN_len_imm;702}703704/* Conditional move. */705static u8 arc_mov_cc_r(u8 *buf, u8 cc, u8 rd, u8 rs)706{707const u32 insn = OPC_MOV_CC | OP_B(rd) | OP_C(rs) | COND(cc);708709if (buf)710emit_4_bytes(buf, insn);711return INSN_len_normal;712}713714/* Conditional move of a small immediate to rd. */715static u8 arc_movu_cc_r(u8 *buf, u8 cc, u8 rd, u8 imm)716{717const u32 insn = OPC_MOVU_CC | OP_B(rd) | OP_C(imm) | COND(cc);718719if (buf)720emit_4_bytes(buf, insn);721return INSN_len_normal;722}723724/* Sign extension from a byte. */725static u8 arc_sexb_r(u8 *buf, u8 rd, u8 rs)726{727const u32 insn = OPC_SEXB | OP_B(rd) | OP_C(rs);728729if (buf)730emit_4_bytes(buf, insn);731return INSN_len_normal;732}733734/* Sign extension from two bytes. */735static u8 arc_sexh_r(u8 *buf, u8 rd, u8 rs)736{737const u32 insn = OPC_SEXH | OP_B(rd) | OP_C(rs);738739if (buf)740emit_4_bytes(buf, insn);741return INSN_len_normal;742}743744/* st reg, [reg_mem, off] */745static u8 arc_st_r(u8 *buf, u8 reg, u8 reg_mem, s16 off, u8 zz)746{747const u32 insn = OPC_STORE | STORE_ZZ(zz) | OP_C(reg) |748OP_B(reg_mem) | STORE_S9(off);749750if (buf)751emit_4_bytes(buf, insn);752return INSN_len_normal;753}754755/* st.aw reg, [sp, -4] */756static u8 arc_push_r(u8 *buf, u8 reg)757{758const u32 insn = OPC_PUSH | OP_C(reg);759760if (buf)761emit_4_bytes(buf, insn);762return INSN_len_normal;763}764765/* ld reg, [reg_mem, off] (unsigned) */766static u8 arc_ld_r(u8 *buf, u8 reg, u8 reg_mem, s16 off, u8 zz)767{768const u32 insn = OPC_LDU | LOAD_ZZ(zz) | LOAD_C(reg) |769OP_B(reg_mem) | LOAD_S9(off);770771if (buf)772emit_4_bytes(buf, insn);773return INSN_len_normal;774}775776/* ld.x reg, [reg_mem, off] (sign extend) */777static u8 arc_ldx_r(u8 *buf, u8 reg, u8 reg_mem, s16 off, u8 zz)778{779const u32 insn = OPC_LDS | LOAD_ZZ(zz) | LOAD_C(reg) |780OP_B(reg_mem) | LOAD_S9(off);781782if (buf)783emit_4_bytes(buf, insn);784return INSN_len_normal;785}786787/* ld.ab reg,[sp,4] */788static u8 arc_pop_r(u8 *buf, u8 reg)789{790const u32 insn = OPC_POP | LOAD_C(reg);791792if (buf)793emit_4_bytes(buf, insn);794return INSN_len_normal;795}796797/* add Ra,Ra,Rc */798static u8 arc_add_r(u8 *buf, u8 ra, u8 rc)799{800const u32 insn = OPC_ADD | OP_A(ra) | OP_B(ra) | OP_C(rc);801802if (buf)803emit_4_bytes(buf, insn);804return INSN_len_normal;805}806807/* add.f Ra,Ra,Rc */808static u8 arc_addf_r(u8 *buf, u8 ra, u8 rc)809{810const u32 insn = OPC_ADDF | OP_A(ra) | OP_B(ra) | OP_C(rc);811812if (buf)813emit_4_bytes(buf, insn);814return INSN_len_normal;815}816817/* add.f Ra,Ra,u6 */818static u8 arc_addif_r(u8 *buf, u8 ra, u8 u6)819{820const u32 insn = OPC_ADDIF | OP_A(ra) | OP_B(ra) | ADDI_U6(u6);821822if (buf)823emit_4_bytes(buf, insn);824return INSN_len_normal;825}826827/* add Ra,Ra,u6 */828static u8 arc_addi_r(u8 *buf, u8 ra, u8 u6)829{830const u32 insn = OPC_ADDI | OP_A(ra) | OP_B(ra) | ADDI_U6(u6);831832if (buf)833emit_4_bytes(buf, insn);834return INSN_len_normal;835}836837/* add Ra,Rb,imm */838static u8 arc_add_i(u8 *buf, u8 ra, u8 rb, s32 imm)839{840const u32 insn = OPC_ADD_I | OP_A(ra) | OP_B(rb);841842if (buf) {843emit_4_bytes(buf, insn);844emit_4_bytes(buf + INSN_len_normal, imm);845}846return INSN_len_normal + INSN_len_imm;847}848849/* adc Ra,Ra,Rc */850static u8 arc_adc_r(u8 *buf, u8 ra, u8 rc)851{852const u32 insn = OPC_ADC | OP_A(ra) | OP_B(ra) | OP_C(rc);853854if (buf)855emit_4_bytes(buf, insn);856return INSN_len_normal;857}858859/* adc Ra,Ra,u6 */860static u8 arc_adci_r(u8 *buf, u8 ra, u8 u6)861{862const u32 insn = OPC_ADCI | OP_A(ra) | OP_B(ra) | ADCI_U6(u6);863864if (buf)865emit_4_bytes(buf, insn);866return INSN_len_normal;867}868869/* sub Ra,Ra,Rc */870static u8 arc_sub_r(u8 *buf, u8 ra, u8 rc)871{872const u32 insn = OPC_SUB | OP_A(ra) | OP_B(ra) | OP_C(rc);873874if (buf)875emit_4_bytes(buf, insn);876return INSN_len_normal;877}878879/* sub.f Ra,Ra,Rc */880static u8 arc_subf_r(u8 *buf, u8 ra, u8 rc)881{882const u32 insn = OPC_SUBF | OP_A(ra) | OP_B(ra) | OP_C(rc);883884if (buf)885emit_4_bytes(buf, insn);886return INSN_len_normal;887}888889/* sub Ra,Ra,u6 */890static u8 arc_subi_r(u8 *buf, u8 ra, u8 u6)891{892const u32 insn = OPC_SUBI | OP_A(ra) | OP_B(ra) | SUBI_U6(u6);893894if (buf)895emit_4_bytes(buf, insn);896return INSN_len_normal;897}898899/* sub Ra,Ra,imm */900static u8 arc_sub_i(u8 *buf, u8 ra, s32 imm)901{902const u32 insn = OPC_SUB_I | OP_A(ra) | OP_B(ra);903904if (buf) {905emit_4_bytes(buf, insn);906emit_4_bytes(buf + INSN_len_normal, imm);907}908return INSN_len_normal + INSN_len_imm;909}910911/* sbc Ra,Ra,Rc */912static u8 arc_sbc_r(u8 *buf, u8 ra, u8 rc)913{914const u32 insn = OPC_SBC | OP_A(ra) | OP_B(ra) | OP_C(rc);915916if (buf)917emit_4_bytes(buf, insn);918return INSN_len_normal;919}920921/* cmp Rb,Rc */922static u8 arc_cmp_r(u8 *buf, u8 rb, u8 rc)923{924const u32 insn = OPC_CMP | OP_B(rb) | OP_C(rc);925926if (buf)927emit_4_bytes(buf, insn);928return INSN_len_normal;929}930931/*932* cmp.z Rb,Rc933*934* This "cmp.z" variant of compare instruction is used on lower935* 32-bits of register pairs after "cmp"ing their upper parts. If the936* upper parts are equal (z), then this one will proceed to check the937* rest.938*/939static u8 arc_cmpz_r(u8 *buf, u8 rb, u8 rc)940{941const u32 insn = OPC_CMP | OP_B(rb) | OP_C(rc) | CC_equal;942943if (buf)944emit_4_bytes(buf, insn);945return INSN_len_normal;946}947948/* neg Ra,Rb */949static u8 arc_neg_r(u8 *buf, u8 ra, u8 rb)950{951const u32 insn = OPC_NEG | OP_A(ra) | OP_B(rb);952953if (buf)954emit_4_bytes(buf, insn);955return INSN_len_normal;956}957958/* mpy Ra,Rb,Rc */959static u8 arc_mpy_r(u8 *buf, u8 ra, u8 rb, u8 rc)960{961const u32 insn = OPC_MPY | OP_A(ra) | OP_B(rb) | OP_C(rc);962963if (buf)964emit_4_bytes(buf, insn);965return INSN_len_normal;966}967968/* mpy Ra,Rb,imm */969static u8 arc_mpy_i(u8 *buf, u8 ra, u8 rb, s32 imm)970{971const u32 insn = OPC_MPYI | OP_A(ra) | OP_B(rb);972973if (buf) {974emit_4_bytes(buf, insn);975emit_4_bytes(buf + INSN_len_normal, imm);976}977return INSN_len_normal + INSN_len_imm;978}979980/* mpydu Ra,Ra,Rc */981static u8 arc_mpydu_r(u8 *buf, u8 ra, u8 rc)982{983const u32 insn = OPC_MPYDU | OP_A(ra) | OP_B(ra) | OP_C(rc);984985if (buf)986emit_4_bytes(buf, insn);987return INSN_len_normal;988}989990/* mpydu Ra,Ra,imm */991static u8 arc_mpydu_i(u8 *buf, u8 ra, s32 imm)992{993const u32 insn = OPC_MPYDUI | OP_A(ra) | OP_B(ra);994995if (buf) {996emit_4_bytes(buf, insn);997emit_4_bytes(buf + INSN_len_normal, imm);998}999return INSN_len_normal + INSN_len_imm;1000}10011002/* divu Rd,Rd,Rs */1003static u8 arc_divu_r(u8 *buf, u8 rd, u8 rs)1004{1005const u32 insn = OPC_DIVU | OP_A(rd) | OP_B(rd) | OP_C(rs);10061007if (buf)1008emit_4_bytes(buf, insn);1009return INSN_len_normal;1010}10111012/* divu Rd,Rd,imm */1013static u8 arc_divu_i(u8 *buf, u8 rd, s32 imm)1014{1015const u32 insn = OPC_DIVUI | OP_A(rd) | OP_B(rd);10161017if (buf) {1018emit_4_bytes(buf, insn);1019emit_4_bytes(buf + INSN_len_normal, imm);1020}1021return INSN_len_normal + INSN_len_imm;1022}10231024/* div Rd,Rd,Rs */1025static u8 arc_divs_r(u8 *buf, u8 rd, u8 rs)1026{1027const u32 insn = OPC_DIVS | OP_A(rd) | OP_B(rd) | OP_C(rs);10281029if (buf)1030emit_4_bytes(buf, insn);1031return INSN_len_normal;1032}10331034/* div Rd,Rd,imm */1035static u8 arc_divs_i(u8 *buf, u8 rd, s32 imm)1036{1037const u32 insn = OPC_DIVSI | OP_A(rd) | OP_B(rd);10381039if (buf) {1040emit_4_bytes(buf, insn);1041emit_4_bytes(buf + INSN_len_normal, imm);1042}1043return INSN_len_normal + INSN_len_imm;1044}10451046/* remu Rd,Rd,Rs */1047static u8 arc_remu_r(u8 *buf, u8 rd, u8 rs)1048{1049const u32 insn = OPC_REMU | OP_A(rd) | OP_B(rd) | OP_C(rs);10501051if (buf)1052emit_4_bytes(buf, insn);1053return INSN_len_normal;1054}10551056/* remu Rd,Rd,imm */1057static u8 arc_remu_i(u8 *buf, u8 rd, s32 imm)1058{1059const u32 insn = OPC_REMUI | OP_A(rd) | OP_B(rd);10601061if (buf) {1062emit_4_bytes(buf, insn);1063emit_4_bytes(buf + INSN_len_normal, imm);1064}1065return INSN_len_normal + INSN_len_imm;1066}10671068/* rem Rd,Rd,Rs */1069static u8 arc_rems_r(u8 *buf, u8 rd, u8 rs)1070{1071const u32 insn = OPC_REMS | OP_A(rd) | OP_B(rd) | OP_C(rs);10721073if (buf)1074emit_4_bytes(buf, insn);1075return INSN_len_normal;1076}10771078/* rem Rd,Rd,imm */1079static u8 arc_rems_i(u8 *buf, u8 rd, s32 imm)1080{1081const u32 insn = OPC_REMSI | OP_A(rd) | OP_B(rd);10821083if (buf) {1084emit_4_bytes(buf, insn);1085emit_4_bytes(buf + INSN_len_normal, imm);1086}1087return INSN_len_normal + INSN_len_imm;1088}10891090/* and Rd,Rd,Rs */1091static u8 arc_and_r(u8 *buf, u8 rd, u8 rs)1092{1093const u32 insn = OPC_AND | OP_A(rd) | OP_B(rd) | OP_C(rs);10941095if (buf)1096emit_4_bytes(buf, insn);1097return INSN_len_normal;1098}10991100/* and Rd,Rd,limm */1101static u8 arc_and_i(u8 *buf, u8 rd, s32 imm)1102{1103const u32 insn = OPC_ANDI | OP_A(rd) | OP_B(rd);11041105if (buf) {1106emit_4_bytes(buf, insn);1107emit_4_bytes(buf + INSN_len_normal, imm);1108}1109return INSN_len_normal + INSN_len_imm;1110}11111112/* tst Rd,Rs */1113static u8 arc_tst_r(u8 *buf, u8 rd, u8 rs)1114{1115const u32 insn = OPC_TST | OP_B(rd) | OP_C(rs);11161117if (buf)1118emit_4_bytes(buf, insn);1119return INSN_len_normal;1120}11211122/*1123* This particular version, "tst.z ...", is meant to be used after a1124* "tst" on the low 32-bit of register pairs. If that "tst" is not1125* zero, then we don't need to test the upper 32-bits lest it sets1126* the zero flag.1127*/1128static u8 arc_tstz_r(u8 *buf, u8 rd, u8 rs)1129{1130const u32 insn = OPC_TST | OP_B(rd) | OP_C(rs) | CC_equal;11311132if (buf)1133emit_4_bytes(buf, insn);1134return INSN_len_normal;1135}11361137static u8 arc_or_r(u8 *buf, u8 rd, u8 rs1, u8 rs2)1138{1139const u32 insn = OPC_OR | OP_A(rd) | OP_B(rs1) | OP_C(rs2);11401141if (buf)1142emit_4_bytes(buf, insn);1143return INSN_len_normal;1144}11451146static u8 arc_or_i(u8 *buf, u8 rd, s32 imm)1147{1148const u32 insn = OPC_ORI | OP_A(rd) | OP_B(rd);11491150if (buf) {1151emit_4_bytes(buf, insn);1152emit_4_bytes(buf + INSN_len_normal, imm);1153}1154return INSN_len_normal + INSN_len_imm;1155}11561157static u8 arc_xor_r(u8 *buf, u8 rd, u8 rs)1158{1159const u32 insn = OPC_XOR | OP_A(rd) | OP_B(rd) | OP_C(rs);11601161if (buf)1162emit_4_bytes(buf, insn);1163return INSN_len_normal;1164}11651166static u8 arc_xor_i(u8 *buf, u8 rd, s32 imm)1167{1168const u32 insn = OPC_XORI | OP_A(rd) | OP_B(rd);11691170if (buf) {1171emit_4_bytes(buf, insn);1172emit_4_bytes(buf + INSN_len_normal, imm);1173}1174return INSN_len_normal + INSN_len_imm;1175}11761177static u8 arc_not_r(u8 *buf, u8 rd, u8 rs)1178{1179const u32 insn = OPC_NOT | OP_B(rd) | OP_C(rs);11801181if (buf)1182emit_4_bytes(buf, insn);1183return INSN_len_normal;1184}11851186static u8 arc_btst_i(u8 *buf, u8 rs, u8 imm)1187{1188const u32 insn = OPC_BTSTU6 | OP_B(rs) | BTST_U6(imm);11891190if (buf)1191emit_4_bytes(buf, insn);1192return INSN_len_normal;1193}11941195static u8 arc_asl_r(u8 *buf, u8 rd, u8 rs1, u8 rs2)1196{1197const u32 insn = OPC_ASL | OP_A(rd) | OP_B(rs1) | OP_C(rs2);11981199if (buf)1200emit_4_bytes(buf, insn);1201return INSN_len_normal;1202}12031204static u8 arc_asli_r(u8 *buf, u8 rd, u8 rs, u8 imm)1205{1206const u32 insn = OPC_ASLI | OP_A(rd) | OP_B(rs) | ASLI_U6(imm);12071208if (buf)1209emit_4_bytes(buf, insn);1210return INSN_len_normal;1211}12121213static u8 arc_asr_r(u8 *buf, u8 rd, u8 rs1, u8 rs2)1214{1215const u32 insn = OPC_ASR | OP_A(rd) | OP_B(rs1) | OP_C(rs2);12161217if (buf)1218emit_4_bytes(buf, insn);1219return INSN_len_normal;1220}12211222static u8 arc_asri_r(u8 *buf, u8 rd, u8 rs, u8 imm)1223{1224const u32 insn = OPC_ASRI | OP_A(rd) | OP_B(rs) | ASRI_U6(imm);12251226if (buf)1227emit_4_bytes(buf, insn);1228return INSN_len_normal;1229}12301231static u8 arc_lsr_r(u8 *buf, u8 rd, u8 rs1, u8 rs2)1232{1233const u32 insn = OPC_LSR | OP_A(rd) | OP_B(rs1) | OP_C(rs2);12341235if (buf)1236emit_4_bytes(buf, insn);1237return INSN_len_normal;1238}12391240static u8 arc_lsri_r(u8 *buf, u8 rd, u8 rs, u8 imm)1241{1242const u32 insn = OPC_LSRI | OP_A(rd) | OP_B(rs) | LSRI_U6(imm);12431244if (buf)1245emit_4_bytes(buf, insn);1246return INSN_len_normal;1247}12481249static u8 arc_swape_r(u8 *buf, u8 r)1250{1251const u32 insn = OPC_SWAPE | OP_B(r) | OP_C(r);12521253if (buf)1254emit_4_bytes(buf, insn);1255return INSN_len_normal;1256}12571258static u8 arc_jmp_return(u8 *buf)1259{1260if (buf)1261emit_4_bytes(buf, OPC_J_BLINK);1262return INSN_len_normal;1263}12641265static u8 arc_jl(u8 *buf, u8 reg)1266{1267const u32 insn = OPC_JL | OP_C(reg);12681269if (buf)1270emit_4_bytes(buf, insn);1271return INSN_len_normal;1272}12731274/*1275* Conditional jump to an address that is max 21 bits away (signed).1276*1277* b<cc> s211278*/1279static u8 arc_bcc(u8 *buf, u8 cc, int offset)1280{1281const u32 insn = OPC_BCC | BCC_S21(offset) | COND(cc);12821283if (buf)1284emit_4_bytes(buf, insn);1285return INSN_len_normal;1286}12871288/*1289* Unconditional jump to an address that is max 25 bits away (signed).1290*1291* b s251292*/1293static u8 arc_b(u8 *buf, s32 offset)1294{1295const u32 insn = OPC_B | B_S25(offset);12961297if (buf)1298emit_4_bytes(buf, insn);1299return INSN_len_normal;1300}13011302/************* Packers (Deal with BPF_REGs) **************/13031304u8 zext(u8 *buf, u8 rd)1305{1306if (rd != BPF_REG_FP)1307return arc_movi_r(buf, REG_HI(rd), 0);1308else1309return 0;1310}13111312u8 mov_r32(u8 *buf, u8 rd, u8 rs, u8 sign_ext)1313{1314u8 len = 0;13151316if (sign_ext) {1317if (sign_ext == 8)1318len = arc_sexb_r(buf, REG_LO(rd), REG_LO(rs));1319else if (sign_ext == 16)1320len = arc_sexh_r(buf, REG_LO(rd), REG_LO(rs));1321else if (sign_ext == 32 && rd != rs)1322len = arc_mov_r(buf, REG_LO(rd), REG_LO(rs));13231324return len;1325}13261327/* Unsigned move. */13281329if (rd != rs)1330len = arc_mov_r(buf, REG_LO(rd), REG_LO(rs));13311332return len;1333}13341335u8 mov_r32_i32(u8 *buf, u8 reg, s32 imm)1336{1337return arc_mov_i(buf, REG_LO(reg), imm);1338}13391340u8 mov_r64(u8 *buf, u8 rd, u8 rs, u8 sign_ext)1341{1342u8 len = 0;13431344if (sign_ext) {1345/* First handle the low 32-bit part. */1346len = mov_r32(buf, rd, rs, sign_ext);13471348/* Now propagate the sign bit of LO to HI. */1349if (sign_ext == 8 || sign_ext == 16 || sign_ext == 32) {1350len += arc_asri_r(BUF(buf, len),1351REG_HI(rd), REG_LO(rd), 31);1352}13531354return len;1355}13561357/* Unsigned move. */13581359if (rd == rs)1360return 0;13611362len = arc_mov_r(buf, REG_LO(rd), REG_LO(rs));13631364if (rs != BPF_REG_FP)1365len += arc_mov_r(BUF(buf, len), REG_HI(rd), REG_HI(rs));1366/* BPF_REG_FP is mapped to 32-bit "fp" register. */1367else1368len += arc_movi_r(BUF(buf, len), REG_HI(rd), 0);13691370return len;1371}13721373/* Sign extend the 32-bit immediate into 64-bit register pair. */1374u8 mov_r64_i32(u8 *buf, u8 reg, s32 imm)1375{1376u8 len = 0;13771378len = arc_mov_i(buf, REG_LO(reg), imm);13791380/* BPF_REG_FP is mapped to 32-bit "fp" register. */1381if (reg != BPF_REG_FP) {1382if (imm >= 0)1383len += arc_movi_r(BUF(buf, len), REG_HI(reg), 0);1384else1385len += arc_movi_r(BUF(buf, len), REG_HI(reg), -1);1386}13871388return len;1389}13901391/*1392* This is merely used for translation of "LD R, IMM64" instructions1393* of the BPF. These sort of instructions are sometimes used for1394* relocations. If during the normal pass, the relocation value is1395* not known, the BPF instruction may look something like:1396*1397* LD R <- 0x0000_0001_0000_00011398*1399* Which will nicely translate to two 4-byte ARC instructions:1400*1401* mov R_lo, 1 # imm is small enough to be s121402* mov R_hi, 1 # same1403*1404* However, during the extra pass, the IMM64 will have changed1405* to the resolved address and looks something like:1406*1407* LD R <- 0x0000_0000_1234_56781408*1409* Now, the translated code will require 12 bytes:1410*1411* mov R_lo, 0x12345678 # this is an 8-byte instruction1412* mov R_hi, 0 # still 4 bytes1413*1414* Which in practice will result in overwriting the following1415* instruction. To avoid such cases, we will always emit codes1416* with fixed sizes.1417*/1418u8 mov_r64_i64(u8 *buf, u8 reg, u32 lo, u32 hi)1419{1420u8 len;14211422len = arc_mov_i_fixed(buf, REG_LO(reg), lo);1423len += arc_mov_i_fixed(BUF(buf, len), REG_HI(reg), hi);14241425return len;1426}14271428/*1429* If the "off"set is too big (doesn't encode as S9) for:1430*1431* {ld,st} r, [rm, off]1432*1433* Then emit:1434*1435* add r10, REG_LO(rm), off1436*1437* and make sure that r10 becomes the effective address:1438*1439* {ld,st} r, [r10, 0]1440*/1441static u8 adjust_mem_access(u8 *buf, s16 *off, u8 size,1442u8 rm, u8 *arc_reg_mem)1443{1444u8 len = 0;1445*arc_reg_mem = REG_LO(rm);14461447if (!IN_S9_RANGE(*off) ||1448(size == BPF_DW && !IN_S9_RANGE(*off + 4))) {1449len += arc_add_i(BUF(buf, len),1450REG_LO(JIT_REG_TMP), REG_LO(rm), (u32)(*off));1451*arc_reg_mem = REG_LO(JIT_REG_TMP);1452*off = 0;1453}14541455return len;1456}14571458/* store rs, [rd, off] */1459u8 store_r(u8 *buf, u8 rs, u8 rd, s16 off, u8 size)1460{1461u8 len, arc_reg_mem;14621463len = adjust_mem_access(buf, &off, size, rd, &arc_reg_mem);14641465if (size == BPF_DW) {1466len += arc_st_r(BUF(buf, len), REG_LO(rs), arc_reg_mem,1467off, ZZ_4_byte);1468len += arc_st_r(BUF(buf, len), REG_HI(rs), arc_reg_mem,1469off + 4, ZZ_4_byte);1470} else {1471u8 zz = bpf_to_arc_size(size);14721473len += arc_st_r(BUF(buf, len), REG_LO(rs), arc_reg_mem,1474off, zz);1475}14761477return len;1478}14791480/*1481* For {8,16,32}-bit stores:1482* mov r21, imm1483* st r21, [...]1484* For 64-bit stores:1485* mov r21, imm1486* st r21, [...]1487* mov r21, {0,-1}1488* st r21, [...+4]1489*/1490u8 store_i(u8 *buf, s32 imm, u8 rd, s16 off, u8 size)1491{1492u8 len, arc_reg_mem;1493/* REG_LO(JIT_REG_TMP) might be used by "adjust_mem_access()". */1494const u8 arc_rs = REG_HI(JIT_REG_TMP);14951496len = adjust_mem_access(buf, &off, size, rd, &arc_reg_mem);14971498if (size == BPF_DW) {1499len += arc_mov_i(BUF(buf, len), arc_rs, imm);1500len += arc_st_r(BUF(buf, len), arc_rs, arc_reg_mem,1501off, ZZ_4_byte);1502imm = (imm >= 0 ? 0 : -1);1503len += arc_mov_i(BUF(buf, len), arc_rs, imm);1504len += arc_st_r(BUF(buf, len), arc_rs, arc_reg_mem,1505off + 4, ZZ_4_byte);1506} else {1507u8 zz = bpf_to_arc_size(size);15081509len += arc_mov_i(BUF(buf, len), arc_rs, imm);1510len += arc_st_r(BUF(buf, len), arc_rs, arc_reg_mem, off, zz);1511}15121513return len;1514}15151516/*1517* For the calling convention of a little endian machine, the LO part1518* must be on top of the stack.1519*/1520static u8 push_r64(u8 *buf, u8 reg)1521{1522u8 len = 0;15231524#ifdef __LITTLE_ENDIAN1525/* BPF_REG_FP is mapped to 32-bit "fp" register. */1526if (reg != BPF_REG_FP)1527len += arc_push_r(BUF(buf, len), REG_HI(reg));1528len += arc_push_r(BUF(buf, len), REG_LO(reg));1529#else1530len += arc_push_r(BUF(buf, len), REG_LO(reg));1531if (reg != BPF_REG_FP)1532len += arc_push_r(BUF(buf, len), REG_HI(reg));1533#endif15341535return len;1536}15371538/* load rd, [rs, off] */1539u8 load_r(u8 *buf, u8 rd, u8 rs, s16 off, u8 size, bool sign_ext)1540{1541u8 len, arc_reg_mem;15421543len = adjust_mem_access(buf, &off, size, rs, &arc_reg_mem);15441545if (size == BPF_B || size == BPF_H || size == BPF_W) {1546const u8 zz = bpf_to_arc_size(size);15471548/* Use LD.X only if the data size is less than 32-bit. */1549if (sign_ext && (zz == ZZ_1_byte || zz == ZZ_2_byte)) {1550len += arc_ldx_r(BUF(buf, len), REG_LO(rd),1551arc_reg_mem, off, zz);1552} else {1553len += arc_ld_r(BUF(buf, len), REG_LO(rd),1554arc_reg_mem, off, zz);1555}15561557if (sign_ext) {1558/* Propagate the sign bit to the higher reg. */1559len += arc_asri_r(BUF(buf, len),1560REG_HI(rd), REG_LO(rd), 31);1561} else {1562len += arc_movi_r(BUF(buf, len), REG_HI(rd), 0);1563}1564} else if (size == BPF_DW) {1565/*1566* We are about to issue 2 consecutive loads:1567*1568* ld rx, [rb, off+0]1569* ld ry, [rb, off+4]1570*1571* If "rx" and "rb" are the same registers, then the order1572* should change to guarantee that "rb" remains intact1573* during these 2 operations:1574*1575* ld ry, [rb, off+4]1576* ld rx, [rb, off+0]1577*/1578if (REG_LO(rd) != arc_reg_mem) {1579len += arc_ld_r(BUF(buf, len), REG_LO(rd), arc_reg_mem,1580off, ZZ_4_byte);1581len += arc_ld_r(BUF(buf, len), REG_HI(rd), arc_reg_mem,1582off + 4, ZZ_4_byte);1583} else {1584len += arc_ld_r(BUF(buf, len), REG_HI(rd), arc_reg_mem,1585off + 4, ZZ_4_byte);1586len += arc_ld_r(BUF(buf, len), REG_LO(rd), arc_reg_mem,1587off, ZZ_4_byte);1588}1589}15901591return len;1592}15931594u8 add_r32(u8 *buf, u8 rd, u8 rs)1595{1596return arc_add_r(buf, REG_LO(rd), REG_LO(rs));1597}15981599u8 add_r32_i32(u8 *buf, u8 rd, s32 imm)1600{1601if (IN_U6_RANGE(imm))1602return arc_addi_r(buf, REG_LO(rd), imm);1603else1604return arc_add_i(buf, REG_LO(rd), REG_LO(rd), imm);1605}16061607u8 add_r64(u8 *buf, u8 rd, u8 rs)1608{1609u8 len;16101611len = arc_addf_r(buf, REG_LO(rd), REG_LO(rs));1612len += arc_adc_r(BUF(buf, len), REG_HI(rd), REG_HI(rs));1613return len;1614}16151616u8 add_r64_i32(u8 *buf, u8 rd, s32 imm)1617{1618u8 len;16191620if (IN_U6_RANGE(imm)) {1621len = arc_addif_r(buf, REG_LO(rd), imm);1622len += arc_adci_r(BUF(buf, len), REG_HI(rd), 0);1623} else {1624len = mov_r64_i32(buf, JIT_REG_TMP, imm);1625len += add_r64(BUF(buf, len), rd, JIT_REG_TMP);1626}1627return len;1628}16291630u8 sub_r32(u8 *buf, u8 rd, u8 rs)1631{1632return arc_sub_r(buf, REG_LO(rd), REG_LO(rs));1633}16341635u8 sub_r32_i32(u8 *buf, u8 rd, s32 imm)1636{1637if (IN_U6_RANGE(imm))1638return arc_subi_r(buf, REG_LO(rd), imm);1639else1640return arc_sub_i(buf, REG_LO(rd), imm);1641}16421643u8 sub_r64(u8 *buf, u8 rd, u8 rs)1644{1645u8 len;16461647len = arc_subf_r(buf, REG_LO(rd), REG_LO(rs));1648len += arc_sbc_r(BUF(buf, len), REG_HI(rd), REG_HI(rs));1649return len;1650}16511652u8 sub_r64_i32(u8 *buf, u8 rd, s32 imm)1653{1654u8 len;16551656len = mov_r64_i32(buf, JIT_REG_TMP, imm);1657len += sub_r64(BUF(buf, len), rd, JIT_REG_TMP);1658return len;1659}16601661static u8 cmp_r32(u8 *buf, u8 rd, u8 rs)1662{1663return arc_cmp_r(buf, REG_LO(rd), REG_LO(rs));1664}16651666u8 neg_r32(u8 *buf, u8 r)1667{1668return arc_neg_r(buf, REG_LO(r), REG_LO(r));1669}16701671/* In a two's complement system, -r is (~r + 1). */1672u8 neg_r64(u8 *buf, u8 r)1673{1674u8 len;16751676len = arc_not_r(buf, REG_LO(r), REG_LO(r));1677len += arc_not_r(BUF(buf, len), REG_HI(r), REG_HI(r));1678len += add_r64_i32(BUF(buf, len), r, 1);1679return len;1680}16811682u8 mul_r32(u8 *buf, u8 rd, u8 rs)1683{1684return arc_mpy_r(buf, REG_LO(rd), REG_LO(rd), REG_LO(rs));1685}16861687u8 mul_r32_i32(u8 *buf, u8 rd, s32 imm)1688{1689return arc_mpy_i(buf, REG_LO(rd), REG_LO(rd), imm);1690}16911692/*1693* MUL B, C1694* --------1695* mpy t0, B_hi, C_lo1696* mpy t1, B_lo, C_hi1697* mpydu B_lo, B_lo, C_lo1698* add B_hi, B_hi, t01699* add B_hi, B_hi, t11700*/1701u8 mul_r64(u8 *buf, u8 rd, u8 rs)1702{1703const u8 t0 = REG_LO(JIT_REG_TMP);1704const u8 t1 = REG_HI(JIT_REG_TMP);1705const u8 C_lo = REG_LO(rs);1706const u8 C_hi = REG_HI(rs);1707const u8 B_lo = REG_LO(rd);1708const u8 B_hi = REG_HI(rd);1709u8 len;17101711len = arc_mpy_r(buf, t0, B_hi, C_lo);1712len += arc_mpy_r(BUF(buf, len), t1, B_lo, C_hi);1713len += arc_mpydu_r(BUF(buf, len), B_lo, C_lo);1714len += arc_add_r(BUF(buf, len), B_hi, t0);1715len += arc_add_r(BUF(buf, len), B_hi, t1);17161717return len;1718}17191720/*1721* MUL B, imm1722* ----------1723*1724* To get a 64-bit result from a signed 64x32 multiplication:1725*1726* B_hi B_lo *1727* sign imm1728* -----------------------------1729* HI(B_lo*imm) LO(B_lo*imm) +1730* B_hi*imm +1731* B_lo*sign1732* -----------------------------1733* res_hi res_lo1734*1735* mpy t1, B_lo, sign(imm)1736* mpy t0, B_hi, imm1737* mpydu B_lo, B_lo, imm1738* add B_hi, B_hi, t01739* add B_hi, B_hi, t11740*1741* Note: We can't use signed double multiplication, "mpyd", instead of an1742* unsigned version, "mpydu", and then get rid of the sign adjustments1743* calculated in "t1". The signed multiplication, "mpyd", will consider1744* both operands, "B_lo" and "imm", as signed inputs. However, for this1745* 64x32 multiplication, "B_lo" must be treated as an unsigned number.1746*/1747u8 mul_r64_i32(u8 *buf, u8 rd, s32 imm)1748{1749const u8 t0 = REG_LO(JIT_REG_TMP);1750const u8 t1 = REG_HI(JIT_REG_TMP);1751const u8 B_lo = REG_LO(rd);1752const u8 B_hi = REG_HI(rd);1753u8 len = 0;17541755if (imm == 1)1756return 0;17571758/* Is the sign-extension of the immediate "-1"? */1759if (imm < 0)1760len += arc_neg_r(BUF(buf, len), t1, B_lo);17611762len += arc_mpy_i(BUF(buf, len), t0, B_hi, imm);1763len += arc_mpydu_i(BUF(buf, len), B_lo, imm);1764len += arc_add_r(BUF(buf, len), B_hi, t0);17651766/* Add the "sign*B_lo" part, if necessary. */1767if (imm < 0)1768len += arc_add_r(BUF(buf, len), B_hi, t1);17691770return len;1771}17721773u8 div_r32(u8 *buf, u8 rd, u8 rs, bool sign_ext)1774{1775if (sign_ext)1776return arc_divs_r(buf, REG_LO(rd), REG_LO(rs));1777else1778return arc_divu_r(buf, REG_LO(rd), REG_LO(rs));1779}17801781u8 div_r32_i32(u8 *buf, u8 rd, s32 imm, bool sign_ext)1782{1783if (imm == 0)1784return 0;17851786if (sign_ext)1787return arc_divs_i(buf, REG_LO(rd), imm);1788else1789return arc_divu_i(buf, REG_LO(rd), imm);1790}17911792u8 mod_r32(u8 *buf, u8 rd, u8 rs, bool sign_ext)1793{1794if (sign_ext)1795return arc_rems_r(buf, REG_LO(rd), REG_LO(rs));1796else1797return arc_remu_r(buf, REG_LO(rd), REG_LO(rs));1798}17991800u8 mod_r32_i32(u8 *buf, u8 rd, s32 imm, bool sign_ext)1801{1802if (imm == 0)1803return 0;18041805if (sign_ext)1806return arc_rems_i(buf, REG_LO(rd), imm);1807else1808return arc_remu_i(buf, REG_LO(rd), imm);1809}18101811u8 and_r32(u8 *buf, u8 rd, u8 rs)1812{1813return arc_and_r(buf, REG_LO(rd), REG_LO(rs));1814}18151816u8 and_r32_i32(u8 *buf, u8 rd, s32 imm)1817{1818return arc_and_i(buf, REG_LO(rd), imm);1819}18201821u8 and_r64(u8 *buf, u8 rd, u8 rs)1822{1823u8 len;18241825len = arc_and_r(buf, REG_LO(rd), REG_LO(rs));1826len += arc_and_r(BUF(buf, len), REG_HI(rd), REG_HI(rs));1827return len;1828}18291830u8 and_r64_i32(u8 *buf, u8 rd, s32 imm)1831{1832u8 len;18331834len = mov_r64_i32(buf, JIT_REG_TMP, imm);1835len += and_r64(BUF(buf, len), rd, JIT_REG_TMP);1836return len;1837}18381839static u8 tst_r32(u8 *buf, u8 rd, u8 rs)1840{1841return arc_tst_r(buf, REG_LO(rd), REG_LO(rs));1842}18431844u8 or_r32(u8 *buf, u8 rd, u8 rs)1845{1846return arc_or_r(buf, REG_LO(rd), REG_LO(rd), REG_LO(rs));1847}18481849u8 or_r32_i32(u8 *buf, u8 rd, s32 imm)1850{1851return arc_or_i(buf, REG_LO(rd), imm);1852}18531854u8 or_r64(u8 *buf, u8 rd, u8 rs)1855{1856u8 len;18571858len = arc_or_r(buf, REG_LO(rd), REG_LO(rd), REG_LO(rs));1859len += arc_or_r(BUF(buf, len), REG_HI(rd), REG_HI(rd), REG_HI(rs));1860return len;1861}18621863u8 or_r64_i32(u8 *buf, u8 rd, s32 imm)1864{1865u8 len;18661867len = mov_r64_i32(buf, JIT_REG_TMP, imm);1868len += or_r64(BUF(buf, len), rd, JIT_REG_TMP);1869return len;1870}18711872u8 xor_r32(u8 *buf, u8 rd, u8 rs)1873{1874return arc_xor_r(buf, REG_LO(rd), REG_LO(rs));1875}18761877u8 xor_r32_i32(u8 *buf, u8 rd, s32 imm)1878{1879return arc_xor_i(buf, REG_LO(rd), imm);1880}18811882u8 xor_r64(u8 *buf, u8 rd, u8 rs)1883{1884u8 len;18851886len = arc_xor_r(buf, REG_LO(rd), REG_LO(rs));1887len += arc_xor_r(BUF(buf, len), REG_HI(rd), REG_HI(rs));1888return len;1889}18901891u8 xor_r64_i32(u8 *buf, u8 rd, s32 imm)1892{1893u8 len;18941895len = mov_r64_i32(buf, JIT_REG_TMP, imm);1896len += xor_r64(BUF(buf, len), rd, JIT_REG_TMP);1897return len;1898}18991900/* "asl a,b,c" --> "a = (b << (c & 31))". */1901u8 lsh_r32(u8 *buf, u8 rd, u8 rs)1902{1903return arc_asl_r(buf, REG_LO(rd), REG_LO(rd), REG_LO(rs));1904}19051906u8 lsh_r32_i32(u8 *buf, u8 rd, u8 imm)1907{1908return arc_asli_r(buf, REG_LO(rd), REG_LO(rd), imm);1909}19101911/*1912* algorithm1913* ---------1914* if (n <= 32)1915* to_hi = lo >> (32-n) # (32-n) is the negate of "n" in a 5-bit width.1916* lo <<= n1917* hi <<= n1918* hi |= to_hi1919* else1920* hi = lo << (n-32)1921* lo = 01922*1923* assembly translation for "LSH B, C"1924* (heavily influenced by ARC gcc)1925* -----------------------------------1926* not t0, C_lo # The first 3 lines are almost the same as:1927* lsr t1, B_lo, 1 # neg t0, C_lo1928* lsr t1, t1, t0 # lsr t1, B_lo, t0 --> t1 is "to_hi"1929* mov t0, C_lo* # with one important difference. In "neg"1930* asl B_lo, B_lo, t0 # version, when C_lo=0, t1 becomes B_lo while1931* asl B_hi, B_hi, t0 # it should be 0. The "not" approach instead,1932* or B_hi, B_hi, t1 # "shift"s t1 once and 31 times, practically1933* btst t0, 5 # setting it to 0 when C_lo=0.1934* mov.ne B_hi, B_lo**1935* mov.ne B_lo, 01936*1937* *The "mov t0, C_lo" is necessary to cover the cases that C is the same1938* register as B.1939*1940* **ARC performs a shift in this manner: B <<= (C & 31)1941* For 32<=n<64, "n-32" and "n&31" are the same. Therefore, "B << n" and1942* "B << (n-32)" yield the same results. e.g. the results of "B << 35" and1943* "B << 3" are the same.1944*1945* The behaviour is undefined for n >= 64.1946*/1947u8 lsh_r64(u8 *buf, u8 rd, u8 rs)1948{1949const u8 t0 = REG_LO(JIT_REG_TMP);1950const u8 t1 = REG_HI(JIT_REG_TMP);1951const u8 C_lo = REG_LO(rs);1952const u8 B_lo = REG_LO(rd);1953const u8 B_hi = REG_HI(rd);1954u8 len;19551956len = arc_not_r(buf, t0, C_lo);1957len += arc_lsri_r(BUF(buf, len), t1, B_lo, 1);1958len += arc_lsr_r(BUF(buf, len), t1, t1, t0);1959len += arc_mov_r(BUF(buf, len), t0, C_lo);1960len += arc_asl_r(BUF(buf, len), B_lo, B_lo, t0);1961len += arc_asl_r(BUF(buf, len), B_hi, B_hi, t0);1962len += arc_or_r(BUF(buf, len), B_hi, B_hi, t1);1963len += arc_btst_i(BUF(buf, len), t0, 5);1964len += arc_mov_cc_r(BUF(buf, len), CC_unequal, B_hi, B_lo);1965len += arc_movu_cc_r(BUF(buf, len), CC_unequal, B_lo, 0);19661967return len;1968}19691970/*1971* if (n < 32)1972* to_hi = B_lo >> 32-n # extract upper n bits1973* lo <<= n1974* hi <<=n1975* hi |= to_hi1976* else if (n < 64)1977* hi = lo << n-321978* lo = 01979*/1980u8 lsh_r64_i32(u8 *buf, u8 rd, s32 imm)1981{1982const u8 t0 = REG_LO(JIT_REG_TMP);1983const u8 B_lo = REG_LO(rd);1984const u8 B_hi = REG_HI(rd);1985const u8 n = (u8)imm;1986u8 len = 0;19871988if (n == 0) {1989return 0;1990} else if (n <= 31) {1991len = arc_lsri_r(buf, t0, B_lo, 32 - n);1992len += arc_asli_r(BUF(buf, len), B_lo, B_lo, n);1993len += arc_asli_r(BUF(buf, len), B_hi, B_hi, n);1994len += arc_or_r(BUF(buf, len), B_hi, B_hi, t0);1995} else if (n <= 63) {1996len = arc_asli_r(buf, B_hi, B_lo, n - 32);1997len += arc_movi_r(BUF(buf, len), B_lo, 0);1998}1999/* n >= 64 is undefined behaviour. */20002001return len;2002}20032004/* "lsr a,b,c" --> "a = (b >> (c & 31))". */2005u8 rsh_r32(u8 *buf, u8 rd, u8 rs)2006{2007return arc_lsr_r(buf, REG_LO(rd), REG_LO(rd), REG_LO(rs));2008}20092010u8 rsh_r32_i32(u8 *buf, u8 rd, u8 imm)2011{2012return arc_lsri_r(buf, REG_LO(rd), REG_LO(rd), imm);2013}20142015/*2016* For better commentary, see lsh_r64().2017*2018* algorithm2019* ---------2020* if (n <= 32)2021* to_lo = hi << (32-n)2022* hi >>= n2023* lo >>= n2024* lo |= to_lo2025* else2026* lo = hi >> (n-32)2027* hi = 02028*2029* RSH B,C2030* ----------2031* not t0, C_lo2032* asl t1, B_hi, 12033* asl t1, t1, t02034* mov t0, C_lo2035* lsr B_hi, B_hi, t02036* lsr B_lo, B_lo, t02037* or B_lo, B_lo, t12038* btst t0, 52039* mov.ne B_lo, B_hi2040* mov.ne B_hi, 02041*/2042u8 rsh_r64(u8 *buf, u8 rd, u8 rs)2043{2044const u8 t0 = REG_LO(JIT_REG_TMP);2045const u8 t1 = REG_HI(JIT_REG_TMP);2046const u8 C_lo = REG_LO(rs);2047const u8 B_lo = REG_LO(rd);2048const u8 B_hi = REG_HI(rd);2049u8 len;20502051len = arc_not_r(buf, t0, C_lo);2052len += arc_asli_r(BUF(buf, len), t1, B_hi, 1);2053len += arc_asl_r(BUF(buf, len), t1, t1, t0);2054len += arc_mov_r(BUF(buf, len), t0, C_lo);2055len += arc_lsr_r(BUF(buf, len), B_hi, B_hi, t0);2056len += arc_lsr_r(BUF(buf, len), B_lo, B_lo, t0);2057len += arc_or_r(BUF(buf, len), B_lo, B_lo, t1);2058len += arc_btst_i(BUF(buf, len), t0, 5);2059len += arc_mov_cc_r(BUF(buf, len), CC_unequal, B_lo, B_hi);2060len += arc_movu_cc_r(BUF(buf, len), CC_unequal, B_hi, 0);20612062return len;2063}20642065/*2066* if (n < 32)2067* to_lo = B_lo << 32-n # extract lower n bits, right-padded with 32-n 0s2068* lo >>=n2069* hi >>=n2070* hi |= to_lo2071* else if (n < 64)2072* lo = hi >> n-322073* hi = 02074*/2075u8 rsh_r64_i32(u8 *buf, u8 rd, s32 imm)2076{2077const u8 t0 = REG_LO(JIT_REG_TMP);2078const u8 B_lo = REG_LO(rd);2079const u8 B_hi = REG_HI(rd);2080const u8 n = (u8)imm;2081u8 len = 0;20822083if (n == 0) {2084return 0;2085} else if (n <= 31) {2086len = arc_asli_r(buf, t0, B_hi, 32 - n);2087len += arc_lsri_r(BUF(buf, len), B_lo, B_lo, n);2088len += arc_lsri_r(BUF(buf, len), B_hi, B_hi, n);2089len += arc_or_r(BUF(buf, len), B_lo, B_lo, t0);2090} else if (n <= 63) {2091len = arc_lsri_r(buf, B_lo, B_hi, n - 32);2092len += arc_movi_r(BUF(buf, len), B_hi, 0);2093}2094/* n >= 64 is undefined behaviour. */20952096return len;2097}20982099/* "asr a,b,c" --> "a = (b s>> (c & 31))". */2100u8 arsh_r32(u8 *buf, u8 rd, u8 rs)2101{2102return arc_asr_r(buf, REG_LO(rd), REG_LO(rd), REG_LO(rs));2103}21042105u8 arsh_r32_i32(u8 *buf, u8 rd, u8 imm)2106{2107return arc_asri_r(buf, REG_LO(rd), REG_LO(rd), imm);2108}21092110/*2111* For comparison, see rsh_r64().2112*2113* algorithm2114* ---------2115* if (n <= 32)2116* to_lo = hi << (32-n)2117* hi s>>= n2118* lo >>= n2119* lo |= to_lo2120* else2121* hi_sign = hi s>>312122* lo = hi s>> (n-32)2123* hi = hi_sign2124*2125* ARSH B,C2126* ----------2127* not t0, C_lo2128* asl t1, B_hi, 12129* asl t1, t1, t02130* mov t0, C_lo2131* asr B_hi, B_hi, t02132* lsr B_lo, B_lo, t02133* or B_lo, B_lo, t12134* btst t0, 52135* asr t0, B_hi, 31 # now, t0 = 0 or -1 based on B_hi's sign2136* mov.ne B_lo, B_hi2137* mov.ne B_hi, t02138*/2139u8 arsh_r64(u8 *buf, u8 rd, u8 rs)2140{2141const u8 t0 = REG_LO(JIT_REG_TMP);2142const u8 t1 = REG_HI(JIT_REG_TMP);2143const u8 C_lo = REG_LO(rs);2144const u8 B_lo = REG_LO(rd);2145const u8 B_hi = REG_HI(rd);2146u8 len;21472148len = arc_not_r(buf, t0, C_lo);2149len += arc_asli_r(BUF(buf, len), t1, B_hi, 1);2150len += arc_asl_r(BUF(buf, len), t1, t1, t0);2151len += arc_mov_r(BUF(buf, len), t0, C_lo);2152len += arc_asr_r(BUF(buf, len), B_hi, B_hi, t0);2153len += arc_lsr_r(BUF(buf, len), B_lo, B_lo, t0);2154len += arc_or_r(BUF(buf, len), B_lo, B_lo, t1);2155len += arc_btst_i(BUF(buf, len), t0, 5);2156len += arc_asri_r(BUF(buf, len), t0, B_hi, 31);2157len += arc_mov_cc_r(BUF(buf, len), CC_unequal, B_lo, B_hi);2158len += arc_mov_cc_r(BUF(buf, len), CC_unequal, B_hi, t0);21592160return len;2161}21622163/*2164* if (n < 32)2165* to_lo = lo << 32-n # extract lower n bits, right-padded with 32-n 0s2166* lo >>=n2167* hi s>>=n2168* hi |= to_lo2169* else if (n < 64)2170* lo = hi s>> n-322171* hi = (lo[msb] ? -1 : 0)2172*/2173u8 arsh_r64_i32(u8 *buf, u8 rd, s32 imm)2174{2175const u8 t0 = REG_LO(JIT_REG_TMP);2176const u8 B_lo = REG_LO(rd);2177const u8 B_hi = REG_HI(rd);2178const u8 n = (u8)imm;2179u8 len = 0;21802181if (n == 0) {2182return 0;2183} else if (n <= 31) {2184len = arc_asli_r(buf, t0, B_hi, 32 - n);2185len += arc_lsri_r(BUF(buf, len), B_lo, B_lo, n);2186len += arc_asri_r(BUF(buf, len), B_hi, B_hi, n);2187len += arc_or_r(BUF(buf, len), B_lo, B_lo, t0);2188} else if (n <= 63) {2189len = arc_asri_r(buf, B_lo, B_hi, n - 32);2190len += arc_movi_r(BUF(buf, len), B_hi, -1);2191len += arc_btst_i(BUF(buf, len), B_lo, 31);2192len += arc_movu_cc_r(BUF(buf, len), CC_equal, B_hi, 0);2193}2194/* n >= 64 is undefined behaviour. */21952196return len;2197}21982199u8 gen_swap(u8 *buf, u8 rd, u8 size, u8 endian, bool force, bool do_zext)2200{2201u8 len = 0;2202#ifdef __BIG_ENDIAN2203const u8 host_endian = BPF_FROM_BE;2204#else2205const u8 host_endian = BPF_FROM_LE;2206#endif2207if (host_endian != endian || force) {2208switch (size) {2209case 16:2210/*2211* r = B4B3_B2B1 << 16 --> r = B2B1_00002212* then, swape(r) would become the desired 0000_B1B22213*/2214len = arc_asli_r(buf, REG_LO(rd), REG_LO(rd), 16);2215fallthrough;2216case 32:2217len += arc_swape_r(BUF(buf, len), REG_LO(rd));2218if (do_zext)2219len += zext(BUF(buf, len), rd);2220break;2221case 64:2222/*2223* swap "hi" and "lo":2224* hi ^= lo;2225* lo ^= hi;2226* hi ^= lo;2227* and then swap the bytes in "hi" and "lo".2228*/2229len = arc_xor_r(buf, REG_HI(rd), REG_LO(rd));2230len += arc_xor_r(BUF(buf, len), REG_LO(rd), REG_HI(rd));2231len += arc_xor_r(BUF(buf, len), REG_HI(rd), REG_LO(rd));2232len += arc_swape_r(BUF(buf, len), REG_LO(rd));2233len += arc_swape_r(BUF(buf, len), REG_HI(rd));2234break;2235default:2236/* The caller must have handled this. */2237break;2238}2239} else {2240/*2241* If the same endianness, there's not much to do other2242* than zeroing out the upper bytes based on the "size".2243*/2244switch (size) {2245case 16:2246len = arc_and_i(buf, REG_LO(rd), 0xffff);2247fallthrough;2248case 32:2249if (do_zext)2250len += zext(BUF(buf, len), rd);2251break;2252case 64:2253break;2254default:2255/* The caller must have handled this. */2256break;2257}2258}22592260return len;2261}22622263/*2264* To create a frame, all that is needed is:2265*2266* push fp2267* mov fp, sp2268* sub sp, <frame_size>2269*2270* "push fp" is taken care of separately while saving the clobbered registers.2271* All that remains is copying SP value to FP and shrinking SP's address space2272* for any possible function call to come.2273*/2274static inline u8 frame_create(u8 *buf, u16 size)2275{2276u8 len;22772278len = arc_mov_r(buf, ARC_R_FP, ARC_R_SP);2279if (IN_U6_RANGE(size))2280len += arc_subi_r(BUF(buf, len), ARC_R_SP, size);2281else2282len += arc_sub_i(BUF(buf, len), ARC_R_SP, size);2283return len;2284}22852286/*2287* mov sp, fp2288*2289* The value of SP upon entering was copied to FP.2290*/2291static inline u8 frame_restore(u8 *buf)2292{2293return arc_mov_r(buf, ARC_R_SP, ARC_R_FP);2294}22952296/*2297* Going from a JITed code to the native caller:2298*2299* mov ARC_ABI_RET_lo, BPF_REG_0_lo # r0 <- r82300* mov ARC_ABI_RET_hi, BPF_REG_0_hi # r1 <- r92301*/2302static u8 bpf_to_arc_return(u8 *buf)2303{2304u8 len;23052306len = arc_mov_r(buf, ARC_R_0, REG_LO(BPF_REG_0));2307len += arc_mov_r(BUF(buf, len), ARC_R_1, REG_HI(BPF_REG_0));2308return len;2309}23102311/*2312* Coming back from an external (in-kernel) function to the JITed code:2313*2314* mov ARC_ABI_RET_lo, BPF_REG_0_lo # r8 <- r02315* mov ARC_ABI_RET_hi, BPF_REG_0_hi # r9 <- r12316*/2317u8 arc_to_bpf_return(u8 *buf)2318{2319u8 len;23202321len = arc_mov_r(buf, REG_LO(BPF_REG_0), ARC_R_0);2322len += arc_mov_r(BUF(buf, len), REG_HI(BPF_REG_0), ARC_R_1);2323return len;2324}23252326/*2327* This translation leads to:2328*2329* mov r10, addr # always an 8-byte instruction2330* jl [r10]2331*2332* The length of the "mov" must be fixed (8), otherwise it may diverge2333* during the normal and extra passes:2334*2335* normal pass extra pass2336*2337* 180: mov r10,0 | 180: mov r10,0x700578d82338* 184: jl [r10] | 188: jl [r10]2339* 188: add.f r16,r16,0x1 | 18c: adc r17,r17,02340* 18c: adc r17,r17,0 |2341*2342* In the above example, the change from "r10 <- 0" to "r10 <- 0x700578d8"2343* has led to an increase in the length of the "mov" instruction.2344* Inadvertently, that caused the loss of the "add.f" instruction.2345*/2346static u8 jump_and_link(u8 *buf, u32 addr)2347{2348u8 len;23492350len = arc_mov_i_fixed(buf, REG_LO(JIT_REG_TMP), addr);2351len += arc_jl(BUF(buf, len), REG_LO(JIT_REG_TMP));2352return len;2353}23542355/*2356* This function determines which ARC registers must be saved and restored.2357* It does so by looking into:2358*2359* "bpf_reg": The clobbered (destination) BPF register2360* "is_call": Indicator if the current instruction is a call2361*2362* When a register of interest is clobbered, its corresponding bit position2363* in return value, "usage", is set to true.2364*/2365u32 mask_for_used_regs(u8 bpf_reg, bool is_call)2366{2367u32 usage = 0;23682369/* BPF registers that must be saved. */2370if (bpf_reg >= BPF_REG_6 && bpf_reg <= BPF_REG_9) {2371usage |= BIT(REG_LO(bpf_reg));2372usage |= BIT(REG_HI(bpf_reg));2373/*2374* Using the frame pointer register implies that it should2375* be saved and reinitialised with the current frame data.2376*/2377} else if (bpf_reg == BPF_REG_FP) {2378usage |= BIT(REG_LO(BPF_REG_FP));2379/* Could there be some ARC registers that must to be saved? */2380} else {2381if (REG_LO(bpf_reg) >= ARC_CALLEE_SAVED_REG_FIRST &&2382REG_LO(bpf_reg) <= ARC_CALLEE_SAVED_REG_LAST)2383usage |= BIT(REG_LO(bpf_reg));23842385if (REG_HI(bpf_reg) >= ARC_CALLEE_SAVED_REG_FIRST &&2386REG_HI(bpf_reg) <= ARC_CALLEE_SAVED_REG_LAST)2387usage |= BIT(REG_HI(bpf_reg));2388}23892390/* A "call" indicates that ARC's "blink" reg must be saved. */2391usage |= is_call ? BIT(ARC_R_BLINK) : 0;23922393return usage;2394}23952396/*2397* push blink # if blink is marked as clobbered2398* push r[0-n] # if r[i] is marked as clobbered2399* push fp # if fp is marked as clobbered2400* mov fp, sp # if frame_size > 0 (clobbers fp)2401* sub sp, <frame_size> # same as above2402*/2403u8 arc_prologue(u8 *buf, u32 usage, u16 frame_size)2404{2405u8 len = 0;2406u32 gp_regs = 0;24072408/* Deal with blink first. */2409if (usage & BIT(ARC_R_BLINK))2410len += arc_push_r(BUF(buf, len), ARC_R_BLINK);24112412gp_regs = usage & ~(BIT(ARC_R_BLINK) | BIT(ARC_R_FP));2413while (gp_regs) {2414u8 reg = __builtin_ffs(gp_regs) - 1;24152416len += arc_push_r(BUF(buf, len), reg);2417gp_regs &= ~BIT(reg);2418}24192420/* Deal with fp last. */2421if ((usage & BIT(ARC_R_FP)) || frame_size > 0)2422len += arc_push_r(BUF(buf, len), ARC_R_FP);24232424if (frame_size > 0)2425len += frame_create(BUF(buf, len), frame_size);24262427#ifdef ARC_BPF_JIT_DEBUG2428if ((usage & BIT(ARC_R_FP)) && frame_size == 0) {2429pr_err("FP is being saved while there is no frame.");2430BUG();2431}2432#endif24332434return len;2435}24362437/*2438* mov sp, fp # if frame_size > 02439* pop fp # if fp is marked as clobbered2440* pop r[n-0] # if r[i] is marked as clobbered2441* pop blink # if blink is marked as clobbered2442* mov r0, r8 # always: ABI_return <- BPF_return2443* mov r1, r9 # continuation of above2444* j [blink] # always2445*2446* "fp being marked as clobbered" and "frame_size > 0" are the two sides of2447* the same coin.2448*/2449u8 arc_epilogue(u8 *buf, u32 usage, u16 frame_size)2450{2451u32 len = 0;2452u32 gp_regs = 0;24532454#ifdef ARC_BPF_JIT_DEBUG2455if ((usage & BIT(ARC_R_FP)) && frame_size == 0) {2456pr_err("FP is being saved while there is no frame.");2457BUG();2458}2459#endif24602461if (frame_size > 0)2462len += frame_restore(BUF(buf, len));24632464/* Deal with fp first. */2465if ((usage & BIT(ARC_R_FP)) || frame_size > 0)2466len += arc_pop_r(BUF(buf, len), ARC_R_FP);24672468gp_regs = usage & ~(BIT(ARC_R_BLINK) | BIT(ARC_R_FP));2469while (gp_regs) {2470/* "usage" is 32-bit, each bit indicating an ARC register. */2471u8 reg = 31 - __builtin_clz(gp_regs);24722473len += arc_pop_r(BUF(buf, len), reg);2474gp_regs &= ~BIT(reg);2475}24762477/* Deal with blink last. */2478if (usage & BIT(ARC_R_BLINK))2479len += arc_pop_r(BUF(buf, len), ARC_R_BLINK);24802481/* Wrap up the return value and jump back to the caller. */2482len += bpf_to_arc_return(BUF(buf, len));2483len += arc_jmp_return(BUF(buf, len));24842485return len;2486}24872488/*2489* For details on the algorithm, see the comments of "gen_jcc_64()".2490*2491* This data structure is holding information for jump translations.2492*2493* jit_off: How many bytes into the current JIT address, "b"ranch insn. occurs2494* cond: The condition that the ARC branch instruction must use2495*2496* e.g.:2497*2498* BPF_JGE R1, R0, @target2499* ------------------------2500* |2501* v2502* 0x1000: cmp r3, r1 # 0x1000 is the JIT address for "BPF_JGE ..." insn2503* 0x1004: bhi @target # first jump (branch higher)2504* 0x1008: blo @end # second jump acting as a skip (end is 0x1014)2505* 0x100C: cmp r2, r0 # the lower 32 bits are evaluated2506* 0x1010: bhs @target # third jump (branch higher or same)2507* 0x1014: ...2508*2509* The jit_off(set) of the "bhi" is 4 bytes.2510* The cond(ition) for the "bhi" is "CC_great_u".2511*2512* The jit_off(set) is necessary for calculating the exact displacement2513* to the "target" address:2514*2515* jit_address + jit_off(set) - @target2516* 0x1000 + 4 - @target2517*/2518#define JCC64_NR_OF_JMPS 3 /* Number of jumps in jcc64 template. */2519#define JCC64_INSNS_TO_END 3 /* Number of insn. inclusive the 2nd jmp to end. */2520#define JCC64_SKIP_JMP 1 /* Index of the "skip" jump to "end". */2521static const struct {2522/*2523* "jit_off" is common between all "jmp[]" and is coupled with2524* "cond" of each "jmp[]" instance. e.g.:2525*2526* arcv2_64_jccs.jit_off[1]2527* arcv2_64_jccs.jmp[ARC_CC_UGT].cond[1]2528*2529* Are indicating that the second jump in JITed code of "UGT"2530* is at offset "jit_off[1]" while its condition is "cond[1]".2531*/2532u8 jit_off[JCC64_NR_OF_JMPS];25332534struct {2535u8 cond[JCC64_NR_OF_JMPS];2536} jmp[ARC_CC_SLE + 1];2537} arcv2_64_jccs = {2538.jit_off = {2539INSN_len_normal * 1,2540INSN_len_normal * 2,2541INSN_len_normal * 42542},2543/*2544* cmp rd_hi, rs_hi2545* bhi @target # 1: u>2546* blo @end # 2: u<2547* cmp rd_lo, rs_lo2548* bhi @target # 3: u>2549* end:2550*/2551.jmp[ARC_CC_UGT] = {2552.cond = {CC_great_u, CC_less_u, CC_great_u}2553},2554/*2555* cmp rd_hi, rs_hi2556* bhi @target # 1: u>2557* blo @end # 2: u<2558* cmp rd_lo, rs_lo2559* bhs @target # 3: u>=2560* end:2561*/2562.jmp[ARC_CC_UGE] = {2563.cond = {CC_great_u, CC_less_u, CC_great_eq_u}2564},2565/*2566* cmp rd_hi, rs_hi2567* blo @target # 1: u<2568* bhi @end # 2: u>2569* cmp rd_lo, rs_lo2570* blo @target # 3: u<2571* end:2572*/2573.jmp[ARC_CC_ULT] = {2574.cond = {CC_less_u, CC_great_u, CC_less_u}2575},2576/*2577* cmp rd_hi, rs_hi2578* blo @target # 1: u<2579* bhi @end # 2: u>2580* cmp rd_lo, rs_lo2581* bls @target # 3: u<=2582* end:2583*/2584.jmp[ARC_CC_ULE] = {2585.cond = {CC_less_u, CC_great_u, CC_less_eq_u}2586},2587/*2588* cmp rd_hi, rs_hi2589* bgt @target # 1: s>2590* blt @end # 2: s<2591* cmp rd_lo, rs_lo2592* bhi @target # 3: u>2593* end:2594*/2595.jmp[ARC_CC_SGT] = {2596.cond = {CC_great_s, CC_less_s, CC_great_u}2597},2598/*2599* cmp rd_hi, rs_hi2600* bgt @target # 1: s>2601* blt @end # 2: s<2602* cmp rd_lo, rs_lo2603* bhs @target # 3: u>=2604* end:2605*/2606.jmp[ARC_CC_SGE] = {2607.cond = {CC_great_s, CC_less_s, CC_great_eq_u}2608},2609/*2610* cmp rd_hi, rs_hi2611* blt @target # 1: s<2612* bgt @end # 2: s>2613* cmp rd_lo, rs_lo2614* blo @target # 3: u<2615* end:2616*/2617.jmp[ARC_CC_SLT] = {2618.cond = {CC_less_s, CC_great_s, CC_less_u}2619},2620/*2621* cmp rd_hi, rs_hi2622* blt @target # 1: s<2623* bgt @end # 2: s>2624* cmp rd_lo, rs_lo2625* bls @target # 3: u<=2626* end:2627*/2628.jmp[ARC_CC_SLE] = {2629.cond = {CC_less_s, CC_great_s, CC_less_eq_u}2630}2631};26322633/*2634* The displacement (offset) for ARC's "b"ranch instruction is the distance2635* from the aligned version of _current_ instruction (PCL) to the target2636* instruction:2637*2638* DISP = TARGET - PCL # PCL is the word aligned PC2639*/2640static inline s32 get_displacement(u32 curr_off, u32 targ_off)2641{2642return (s32)(targ_off - (curr_off & ~3L));2643}26442645/*2646* "disp"lacement should be:2647*2648* 1. 16-bit aligned.2649* 2. fit in S25, because no "condition code" is supposed to be encoded.2650*/2651static inline bool is_valid_far_disp(s32 disp)2652{2653return (!(disp & 1) && IN_S25_RANGE(disp));2654}26552656/*2657* "disp"lacement should be:2658*2659* 1. 16-bit aligned.2660* 2. fit in S21, because "condition code" is supposed to be encoded too.2661*/2662static inline bool is_valid_near_disp(s32 disp)2663{2664return (!(disp & 1) && IN_S21_RANGE(disp));2665}26662667/*2668* cmp rd_hi, rs_hi2669* cmp.z rd_lo, rs_lo2670* b{eq,ne} @target2671* | |2672* | `--> "eq" param is false (JNE)2673* `-----> "eq" param is true (JEQ)2674*/2675static int gen_j_eq_64(u8 *buf, u8 rd, u8 rs, bool eq,2676u32 curr_off, u32 targ_off)2677{2678s32 disp;2679u8 len = 0;26802681len += arc_cmp_r(BUF(buf, len), REG_HI(rd), REG_HI(rs));2682len += arc_cmpz_r(BUF(buf, len), REG_LO(rd), REG_LO(rs));2683disp = get_displacement(curr_off + len, targ_off);2684len += arc_bcc(BUF(buf, len), eq ? CC_equal : CC_unequal, disp);26852686return len;2687}26882689/*2690* tst rd_hi, rs_hi2691* tst.z rd_lo, rs_lo2692* bne @target2693*/2694static u8 gen_jset_64(u8 *buf, u8 rd, u8 rs, u32 curr_off, u32 targ_off)2695{2696u8 len = 0;2697s32 disp;26982699len += arc_tst_r(BUF(buf, len), REG_HI(rd), REG_HI(rs));2700len += arc_tstz_r(BUF(buf, len), REG_LO(rd), REG_LO(rs));2701disp = get_displacement(curr_off + len, targ_off);2702len += arc_bcc(BUF(buf, len), CC_unequal, disp);27032704return len;2705}27062707/*2708* Verify if all the jumps for a JITed jcc64 operation are valid,2709* by consulting the data stored at "arcv2_64_jccs".2710*/2711static bool check_jcc_64(u32 curr_off, u32 targ_off, u8 cond)2712{2713size_t i;27142715if (cond >= ARC_CC_LAST)2716return false;27172718for (i = 0; i < JCC64_NR_OF_JMPS; i++) {2719u32 from, to;27202721from = curr_off + arcv2_64_jccs.jit_off[i];2722/* for the 2nd jump, we jump to the end of block. */2723if (i != JCC64_SKIP_JMP)2724to = targ_off;2725else2726to = from + (JCC64_INSNS_TO_END * INSN_len_normal);2727/* There is a "cc" in the instruction, so a "near" jump. */2728if (!is_valid_near_disp(get_displacement(from, to)))2729return false;2730}27312732return true;2733}27342735/* Can the jump from "curr_off" to "targ_off" actually happen? */2736bool check_jmp_64(u32 curr_off, u32 targ_off, u8 cond)2737{2738s32 disp;27392740switch (cond) {2741case ARC_CC_UGT:2742case ARC_CC_UGE:2743case ARC_CC_ULT:2744case ARC_CC_ULE:2745case ARC_CC_SGT:2746case ARC_CC_SGE:2747case ARC_CC_SLT:2748case ARC_CC_SLE:2749return check_jcc_64(curr_off, targ_off, cond);2750case ARC_CC_EQ:2751case ARC_CC_NE:2752case ARC_CC_SET:2753/*2754* The "jump" for the JITed BPF_J{SET,EQ,NE} is actually the2755* 3rd instruction. See comments of "gen_j{set,_eq}_64()".2756*/2757curr_off += 2 * INSN_len_normal;2758disp = get_displacement(curr_off, targ_off);2759/* There is a "cc" field in the issued instruction. */2760return is_valid_near_disp(disp);2761case ARC_CC_AL:2762disp = get_displacement(curr_off, targ_off);2763return is_valid_far_disp(disp);2764default:2765return false;2766}2767}27682769/*2770* The template for the 64-bit jumps with the following BPF conditions2771*2772* u< u<= u> u>= s< s<= s> s>=2773*2774* Looks like below:2775*2776* cmp rd_hi, rs_hi2777* b<c1> @target2778* b<c2> @end2779* cmp rd_lo, rs_lo # if execution reaches here, r{d,s}_hi are equal2780* b<c3> @target2781* end:2782*2783* "c1" is the condition that JIT is handling minus the equality part.2784* For instance if we have to translate an "unsigned greater or equal",2785* then "c1" will be "unsigned greater". We won't know about equality2786* until all 64-bits of data (higeher and lower registers) are processed.2787*2788* "c2" is the counter logic of "c1". For instance, if "c1" is originated2789* from "s>", then "c2" would be "s<". Notice that equality doesn't play2790* a role here either, because the lower 32 bits are not processed yet.2791*2792* "c3" is the unsigned version of "c1", no matter if the BPF condition2793* was signed or unsigned. An unsigned version is necessary, because the2794* MSB of the lower 32 bits does not reflect a sign in the whole 64-bit2795* scheme. Otherwise, 64-bit comparisons like2796* (0x0000_0000,0x8000_0000) s>= (0x0000_0000,0x0000_0000)2797* would yield an incorrect result. Finally, if there is an equality2798* check in the BPF condition, it will be reflected in "c3".2799*2800* You can find all the instances of this template where the2801* "arcv2_64_jccs" is getting initialised.2802*/2803static u8 gen_jcc_64(u8 *buf, u8 rd, u8 rs, u8 cond,2804u32 curr_off, u32 targ_off)2805{2806s32 disp;2807u32 end_off;2808const u8 *cc = arcv2_64_jccs.jmp[cond].cond;2809u8 len = 0;28102811/* cmp rd_hi, rs_hi */2812len += arc_cmp_r(buf, REG_HI(rd), REG_HI(rs));28132814/* b<c1> @target */2815disp = get_displacement(curr_off + len, targ_off);2816len += arc_bcc(BUF(buf, len), cc[0], disp);28172818/* b<c2> @end */2819end_off = curr_off + len + (JCC64_INSNS_TO_END * INSN_len_normal);2820disp = get_displacement(curr_off + len, end_off);2821len += arc_bcc(BUF(buf, len), cc[1], disp);28222823/* cmp rd_lo, rs_lo */2824len += arc_cmp_r(BUF(buf, len), REG_LO(rd), REG_LO(rs));28252826/* b<c3> @target */2827disp = get_displacement(curr_off + len, targ_off);2828len += arc_bcc(BUF(buf, len), cc[2], disp);28292830return len;2831}28322833/*2834* This function only applies the necessary logic to make the proper2835* translations. All the sanity checks must have already been done2836* by calling the check_jmp_64().2837*/2838u8 gen_jmp_64(u8 *buf, u8 rd, u8 rs, u8 cond, u32 curr_off, u32 targ_off)2839{2840u8 len = 0;2841bool eq = false;2842s32 disp;28432844switch (cond) {2845case ARC_CC_AL:2846disp = get_displacement(curr_off, targ_off);2847len = arc_b(buf, disp);2848break;2849case ARC_CC_UGT:2850case ARC_CC_UGE:2851case ARC_CC_ULT:2852case ARC_CC_ULE:2853case ARC_CC_SGT:2854case ARC_CC_SGE:2855case ARC_CC_SLT:2856case ARC_CC_SLE:2857len = gen_jcc_64(buf, rd, rs, cond, curr_off, targ_off);2858break;2859case ARC_CC_EQ:2860eq = true;2861fallthrough;2862case ARC_CC_NE:2863len = gen_j_eq_64(buf, rd, rs, eq, curr_off, targ_off);2864break;2865case ARC_CC_SET:2866len = gen_jset_64(buf, rd, rs, curr_off, targ_off);2867break;2868default:2869#ifdef ARC_BPF_JIT_DEBUG2870pr_err("64-bit jump condition is not known.");2871BUG();2872#endif2873}2874return len;2875}28762877/*2878* The condition codes to use when generating JIT instructions2879* for 32-bit jumps.2880*2881* The "ARC_CC_AL" index is not really used by the code, but it2882* is here for the sake of completeness.2883*2884* The "ARC_CC_SET" becomes "CC_unequal" because of the "tst"2885* instruction that precedes the conditional branch.2886*/2887static const u8 arcv2_32_jmps[ARC_CC_LAST] = {2888[ARC_CC_UGT] = CC_great_u,2889[ARC_CC_UGE] = CC_great_eq_u,2890[ARC_CC_ULT] = CC_less_u,2891[ARC_CC_ULE] = CC_less_eq_u,2892[ARC_CC_SGT] = CC_great_s,2893[ARC_CC_SGE] = CC_great_eq_s,2894[ARC_CC_SLT] = CC_less_s,2895[ARC_CC_SLE] = CC_less_eq_s,2896[ARC_CC_AL] = CC_always,2897[ARC_CC_EQ] = CC_equal,2898[ARC_CC_NE] = CC_unequal,2899[ARC_CC_SET] = CC_unequal2900};29012902/* Can the jump from "curr_off" to "targ_off" actually happen? */2903bool check_jmp_32(u32 curr_off, u32 targ_off, u8 cond)2904{2905u8 addendum;2906s32 disp;29072908if (cond >= ARC_CC_LAST)2909return false;29102911/*2912* The unconditional jump happens immediately, while the rest2913* are either preceded by a "cmp" or "tst" instruction.2914*/2915addendum = (cond == ARC_CC_AL) ? 0 : INSN_len_normal;2916disp = get_displacement(curr_off + addendum, targ_off);29172918if (cond == ARC_CC_AL)2919return is_valid_far_disp(disp);2920else2921return is_valid_near_disp(disp);2922}29232924/*2925* The JITed code for 32-bit (conditional) branches:2926*2927* ARC_CC_AL @target2928* b @jit_targ_addr2929*2930* ARC_CC_SET rd, rs, @target2931* tst rd, rs2932* bnz @jit_targ_addr2933*2934* ARC_CC_xx rd, rs, @target2935* cmp rd, rs2936* b<cc> @jit_targ_addr # cc = arcv2_32_jmps[xx]2937*/2938u8 gen_jmp_32(u8 *buf, u8 rd, u8 rs, u8 cond, u32 curr_off, u32 targ_off)2939{2940s32 disp;2941u8 len = 0;29422943/*2944* Although this must have already been checked by "check_jmp_32()",2945* we're not going to risk accessing "arcv2_32_jmps" array without2946* the boundary check.2947*/2948if (cond >= ARC_CC_LAST) {2949#ifdef ARC_BPF_JIT_DEBUG2950pr_err("32-bit jump condition is not known.");2951BUG();2952#endif2953return 0;2954}29552956/* If there is a "condition", issue the "cmp" or "tst" first. */2957if (cond != ARC_CC_AL) {2958if (cond == ARC_CC_SET)2959len = tst_r32(buf, rd, rs);2960else2961len = cmp_r32(buf, rd, rs);2962/*2963* The issued instruction affects the "disp"lacement as2964* it alters the "curr_off" by its "len"gth. The "curr_off"2965* should always point to the jump instruction.2966*/2967disp = get_displacement(curr_off + len, targ_off);2968len += arc_bcc(BUF(buf, len), arcv2_32_jmps[cond], disp);2969} else {2970/* The straight forward unconditional jump. */2971disp = get_displacement(curr_off, targ_off);2972len = arc_b(buf, disp);2973}29742975return len;2976}29772978/*2979* Generate code for functions calls. There can be two types of calls:2980*2981* - Calling another BPF function2982* - Calling an in-kernel function which is compiled by ARC gcc2983*2984* In the later case, we must comply to ARCv2 ABI and handle arguments2985* and return values accordingly.2986*/2987u8 gen_func_call(u8 *buf, ARC_ADDR func_addr, bool external_func)2988{2989u8 len = 0;29902991/*2992* In case of an in-kernel function call, always push the 5th2993* argument onto the stack, because that's where the ABI dictates2994* it should be found. If the callee doesn't really use it, no harm2995* is done. The stack is readjusted either way after the call.2996*/2997if (external_func)2998len += push_r64(BUF(buf, len), BPF_REG_5);29993000len += jump_and_link(BUF(buf, len), func_addr);30013002if (external_func)3003len += arc_add_i(BUF(buf, len), ARC_R_SP, ARC_R_SP, ARG5_SIZE);30043005return len;3006}300730083009