1 // SPDX-License-Identifier: GPL-2.0
3 * The ARCv2 backend of Just-In-Time compiler for eBPF bytecode.
5 * Copyright (c) 2024 Synopsys Inc.
11 /* ARC core registers. */
13 ARC_R_0, ARC_R_1, ARC_R_2, ARC_R_3, ARC_R_4, ARC_R_5,
14 ARC_R_6, ARC_R_7, ARC_R_8, ARC_R_9, ARC_R_10, ARC_R_11,
15 ARC_R_12, ARC_R_13, ARC_R_14, ARC_R_15, ARC_R_16, ARC_R_17,
16 ARC_R_18, ARC_R_19, ARC_R_20, ARC_R_21, ARC_R_22, ARC_R_23,
17 ARC_R_24, ARC_R_25, ARC_R_26, ARC_R_FP, ARC_R_SP, ARC_R_ILINK,
18 ARC_R_30, ARC_R_BLINK,
20 * Having ARC_R_IMM encoded as source register means there is an
21 * immediate that must be interpreted from the next 4 bytes. If
22 * encoded as the destination register though, it implies that the
23 * output of the operation is not assigned to any register. The
24 * latter is helpful if we only care about updating the CPU status
31 * Remarks about the rationale behind the chosen mapping:
33 * - BPF_REG_{1,2,3,4} are the argument registers and must be mapped to
34 * argument registers in ARCv2 ABI: r0-r7. The r7 registers is the last
35 * argument register in the ABI. Therefore BPF_REG_5, as the fifth
36 * argument, must be pushed onto the stack. This is a must for calling
37 * in-kernel functions.
39 * - In ARCv2 ABI, the return value is in r0 for 32-bit results and (r1,r0)
40 * for 64-bit results. However, because they're already used for BPF_REG_1,
41 * the next available scratch registers, r8 and r9, are the best candidates
42 * for BPF_REG_0. After a "call" to a(n) (in-kernel) function, the result
43 * is "mov"ed to these registers. At a BPF_EXIT, their value is "mov"ed to
45 * It is worth mentioning that scratch registers are the best choice for
46 * BPF_REG_0, because it is very popular in BPF instruction encoding.
48 * - JIT_REG_TMP is an artifact needed to translate some BPF instructions.
49 * Its life span is one single BPF instruction. Since during the
50 * analyze_reg_usage(), it is not known if temporary registers are used,
51 * it is mapped to ARC's scratch registers: r10 and r11. Therefore, they
52 * don't matter in analysing phase and don't need saving. This temporary
53 * register is added as yet another index in the bpf2arc array, so it will
54 * unfold like the rest of registers during the code generation process.
56 * - Mapping of callee-saved BPF registers, BPF_REG_{6,7,8,9}, starts from
57 * (r15,r14) register pair. The (r13,r12) is not a good choice, because
58 * in ARCv2 ABI, r12 is not a callee-saved register and this can cause
59 * problem when calling an in-kernel function. Theoretically, the mapping
60 * could start from (r14,r13), but it is not a conventional ARCv2 register
61 * pair. To have a future proof design, I opted for this arrangement.
62 * If/when we decide to add ARCv2 instructions that do use register pairs,
63 * the mapping, hopefully, doesn't need to be revisited.
65 const u8 bpf2arc[][2] = {
66 /* Return value from in-kernel function, and exit value from eBPF */
67 [BPF_REG_0] = {ARC_R_8, ARC_R_9},
68 /* Arguments from eBPF program to in-kernel function */
69 [BPF_REG_1] = {ARC_R_0, ARC_R_1},
70 [BPF_REG_2] = {ARC_R_2, ARC_R_3},
71 [BPF_REG_3] = {ARC_R_4, ARC_R_5},
72 [BPF_REG_4] = {ARC_R_6, ARC_R_7},
73 /* Remaining arguments, to be passed on the stack per 32-bit ABI */
74 [BPF_REG_5] = {ARC_R_22, ARC_R_23},
75 /* Callee-saved registers that in-kernel function will preserve */
76 [BPF_REG_6] = {ARC_R_14, ARC_R_15},
77 [BPF_REG_7] = {ARC_R_16, ARC_R_17},
78 [BPF_REG_8] = {ARC_R_18, ARC_R_19},
79 [BPF_REG_9] = {ARC_R_20, ARC_R_21},
80 /* Read-only frame pointer to access the eBPF stack. 32-bit only. */
81 [BPF_REG_FP] = {ARC_R_FP, },
82 /* Register for blinding constants */
83 [BPF_REG_AX] = {ARC_R_24, ARC_R_25},
84 /* Temporary registers for internal use */
85 [JIT_REG_TMP] = {ARC_R_10, ARC_R_11}
88 #define ARC_CALLEE_SAVED_REG_FIRST ARC_R_13
89 #define ARC_CALLEE_SAVED_REG_LAST ARC_R_25
91 #define REG_LO(r) (bpf2arc[(r)][0])
92 #define REG_HI(r) (bpf2arc[(r)][1])
95 * To comply with ARCv2 ABI, BPF's arg5 must be put on stack. After which,
96 * the stack needs to be restored by ARG5_SIZE.
100 /* Instruction lengths in bytes. */
102 INSN_len_normal = 4, /* Normal instructions length. */
103 INSN_len_imm = 4 /* Length of an extra 32-bit immediate. */
106 /* ZZ defines the size of operation in encodings that it is used. */
115 * AA is mostly about address write back mode. It determines if the
116 * address in question should be updated before usage or after:
117 * addr += offset; data = *addr;
118 * data = *addr; addr += offset;
120 * In "scaling" mode, the effective address will become the sum
121 * of "address" + "index"*"size". The "size" is specified by the
122 * "ZZ" field. There is no write back when AA is set for scaling:
123 * data = *(addr + offset<<zz)
127 AA_pre = 1, /* in assembly known as "a/aw". */
128 AA_post = 2, /* in assembly known as "ab". */
129 AA_scale = 3 /* in assembly known as "as". */
132 /* X flag determines the mode of extension. */
138 /* Condition codes. */
140 CC_always = 0, /* condition is true all the time */
141 CC_equal = 1, /* if status32.z flag is set */
142 CC_unequal = 2, /* if status32.z flag is clear */
143 CC_positive = 3, /* if status32.n flag is clear */
144 CC_negative = 4, /* if status32.n flag is set */
145 CC_less_u = 5, /* less than (unsigned) */
146 CC_less_eq_u = 14, /* less than or equal (unsigned) */
147 CC_great_eq_u = 6, /* greater than or equal (unsigned) */
148 CC_great_u = 13, /* greater than (unsigned) */
149 CC_less_s = 11, /* less than (signed) */
150 CC_less_eq_s = 12, /* less than or equal (signed) */
151 CC_great_eq_s = 10, /* greater than or equal (signed) */
152 CC_great_s = 9 /* greater than (signed) */
155 #define IN_U6_RANGE(x) ((x) <= (0x40 - 1) && (x) >= 0)
156 #define IN_S9_RANGE(x) ((x) <= (0x100 - 1) && (x) >= -0x100)
157 #define IN_S12_RANGE(x) ((x) <= (0x800 - 1) && (x) >= -0x800)
158 #define IN_S21_RANGE(x) ((x) <= (0x100000 - 1) && (x) >= -0x100000)
159 #define IN_S25_RANGE(x) ((x) <= (0x1000000 - 1) && (x) >= -0x1000000)
161 /* Operands in most of the encodings. */
162 #define OP_A(x) ((x) & 0x03f)
163 #define OP_B(x) ((((x) & 0x07) << 24) | (((x) & 0x38) << 9))
164 #define OP_C(x) (((x) & 0x03f) << 6)
165 #define OP_IMM (OP_C(ARC_R_IMM))
166 #define COND(x) (OP_A((x) & 31))
167 #define FLAG(x) (((x) & 1) << 15)
170 * The 4-byte encoding of "mov b,c":
172 * 0010_0bbb 0000_1010 0BBB_cccc cc00_0000
174 * b: BBBbbb destination register
175 * c: cccccc source register
177 #define OPC_MOV 0x200a0000
180 * The 4-byte encoding of "mov b,s12" (used for moving small immediates):
182 * 0010_0bbb 1000_1010 0BBB_ssss ssSS_SSSS
184 * b: BBBbbb destination register
185 * s: SSSSSSssssss source immediate (signed)
187 #define OPC_MOVI 0x208a0000
188 #define MOVI_S12(x) ((((x) & 0xfc0) >> 6) | (((x) & 0x3f) << 6))
191 * The 4-byte encoding of "mov[.qq] b,u6", used for conditional
192 * moving of even smaller immediates:
194 * 0010_0bbb 1100_1010 0BBB_cccc cciq_qqqq
196 * qq: qqqqq condition code
197 * i: If set, c is considered a 6-bit immediate, else a reg.
199 * b: BBBbbb destination register
202 #define OPC_MOV_CC 0x20ca0000
203 #define MOV_CC_I BIT(5)
204 #define OPC_MOVU_CC (OPC_MOV_CC | MOV_CC_I)
207 * The 4-byte encoding of "sexb b,c" (8-bit sign extension):
209 * 0010_0bbb 0010_1111 0BBB_cccc cc00_0101
211 * b: BBBbbb destination register
212 * c: cccccc source register
214 #define OPC_SEXB 0x202f0005
217 * The 4-byte encoding of "sexh b,c" (16-bit sign extension):
219 * 0010_0bbb 0010_1111 0BBB_cccc cc00_0110
221 * b: BBBbbb destination register
222 * c: cccccc source register
224 #define OPC_SEXH 0x202f0006
227 * The 4-byte encoding of "ld[zz][.x][.aa] c,[b,s9]":
229 * 0001_0bbb ssss_ssss SBBB_0aaz zxcc_cccc
232 * aa: address write back mode
235 * s9: S_ssss_ssss 9-bit signed number
236 * b: BBBbbb source reg for address
237 * c: cccccc destination register
239 #define OPC_LOAD 0x10000000
240 #define LOAD_X(x) ((x) << 6)
241 #define LOAD_ZZ(x) ((x) << 7)
242 #define LOAD_AA(x) ((x) << 9)
243 #define LOAD_S9(x) ((((x) & 0x0ff) << 16) | (((x) & 0x100) << 7))
244 #define LOAD_C(x) ((x) & 0x03f)
245 /* Unsigned and signed loads. */
246 #define OPC_LDU (OPC_LOAD | LOAD_X(X_zero))
247 #define OPC_LDS (OPC_LOAD | LOAD_X(X_sign))
249 #define OPC_LD32 (OPC_LDU | LOAD_ZZ(ZZ_4_byte))
250 /* "pop reg" is merely a "ld.ab reg,[sp,4]". */
252 (OPC_LD32 | LOAD_AA(AA_post) | LOAD_S9(4) | OP_B(ARC_R_SP))
255 * The 4-byte encoding of "st[zz][.aa] c,[b,s9]":
257 * 0001_1bbb ssss_ssss SBBB_cccc cc0a_azz0
260 * aa: aa address write back mode
262 * s9: S_ssss_ssss 9-bit signed number
263 * b: BBBbbb source reg for address
264 * c: cccccc source reg to be stored
266 #define OPC_STORE 0x18000000
267 #define STORE_ZZ(x) ((x) << 1)
268 #define STORE_AA(x) ((x) << 3)
269 #define STORE_S9(x) ((((x) & 0x0ff) << 16) | (((x) & 0x100) << 7))
271 #define OPC_ST32 (OPC_STORE | STORE_ZZ(ZZ_4_byte))
272 /* "push reg" is merely a "st.aw reg,[sp,-4]". */
274 (OPC_ST32 | STORE_AA(AA_pre) | STORE_S9(-4) | OP_B(ARC_R_SP))
277 * The 4-byte encoding of "add a,b,c":
279 * 0010_0bbb 0i00_0000 fBBB_cccc ccaa_aaaa
281 * f: indicates if flags (carry, etc.) should be updated
282 * i: If set, c is considered a 6-bit immediate, else a reg.
285 * b: BBBbbb the 1st input operand
286 * c: cccccc the 2nd input operand
288 #define OPC_ADD 0x20000000
289 /* Addition with updating the pertinent flags in "status32" register. */
290 #define OPC_ADDF (OPC_ADD | FLAG(1))
292 #define ADDI_U6(x) OP_C(x)
293 #define OPC_ADDI (OPC_ADD | ADDI)
294 #define OPC_ADDIF (OPC_ADDI | FLAG(1))
295 #define OPC_ADD_I (OPC_ADD | OP_IMM)
298 * The 4-byte encoding of "adc a,b,c" (addition with carry):
300 * 0010_0bbb 0i00_0001 0BBB_cccc ccaa_aaaa
302 * i: if set, c is considered a 6-bit immediate, else a reg.
305 * b: BBBbbb the 1st input operand
306 * c: cccccc the 2nd input operand
308 #define OPC_ADC 0x20010000
310 #define ADCI_U6(x) OP_C(x)
311 #define OPC_ADCI (OPC_ADC | ADCI)
314 * The 4-byte encoding of "sub a,b,c":
316 * 0010_0bbb 0i00_0010 fBBB_cccc ccaa_aaaa
318 * f: indicates if flags (carry, etc.) should be updated
319 * i: if set, c is considered a 6-bit immediate, else a reg.
322 * b: BBBbbb the 1st input operand
323 * c: cccccc the 2nd input operand
325 #define OPC_SUB 0x20020000
326 /* Subtraction with updating the pertinent flags in "status32" register. */
327 #define OPC_SUBF (OPC_SUB | FLAG(1))
329 #define SUBI_U6(x) OP_C(x)
330 #define OPC_SUBI (OPC_SUB | SUBI)
331 #define OPC_SUB_I (OPC_SUB | OP_IMM)
334 * The 4-byte encoding of "sbc a,b,c" (subtraction with carry):
336 * 0010_0bbb 0000_0011 fBBB_cccc ccaa_aaaa
338 * f: indicates if flags (carry, etc.) should be updated
341 * b: BBBbbb the 1st input operand
342 * c: cccccc the 2nd input operand
344 #define OPC_SBC 0x20030000
347 * The 4-byte encoding of "cmp[.qq] b,c":
349 * 0010_0bbb 1100_1100 1BBB_cccc cc0q_qqqq
351 * qq: qqqqq condition code
353 * b: BBBbbb the 1st operand
354 * c: cccccc the 2nd operand
356 #define OPC_CMP 0x20cc8000
359 * The 4-byte encoding of "neg a,b":
361 * 0010_0bbb 0100_1110 0BBB_0000 00aa_aaaa
366 #define OPC_NEG 0x204e0000
369 * The 4-byte encoding of "mpy a,b,c".
370 * mpy is the signed 32-bit multiplication with the lower 32-bit
371 * of the product as the result.
373 * 0010_0bbb 0001_1010 0BBB_cccc ccaa_aaaa
376 * b: BBBbbb the 1st input operand
377 * c: cccccc the 2nd input operand
379 #define OPC_MPY 0x201a0000
380 #define OPC_MPYI (OPC_MPY | OP_IMM)
383 * The 4-byte encoding of "mpydu a,b,c".
384 * mpydu is the unsigned 32-bit multiplication with the lower 32-bit of
385 * the product in register "a" and the higher 32-bit in register "a+1".
387 * 0010_1bbb 0001_1001 0BBB_cccc ccaa_aaaa
389 * a: aaaaaa 64-bit result in registers (R_a+1,R_a)
390 * b: BBBbbb the 1st input operand
391 * c: cccccc the 2nd input operand
393 #define OPC_MPYDU 0x28190000
394 #define OPC_MPYDUI (OPC_MPYDU | OP_IMM)
397 * The 4-byte encoding of "divu a,b,c" (unsigned division):
399 * 0010_1bbb 0000_0101 0BBB_cccc ccaa_aaaa
401 * a: aaaaaa result (quotient)
402 * b: BBBbbb the 1st input operand
403 * c: cccccc the 2nd input operand (divisor)
405 #define OPC_DIVU 0x28050000
406 #define OPC_DIVUI (OPC_DIVU | OP_IMM)
409 * The 4-byte encoding of "div a,b,c" (signed division):
411 * 0010_1bbb 0000_0100 0BBB_cccc ccaa_aaaa
413 * a: aaaaaa result (quotient)
414 * b: BBBbbb the 1st input operand
415 * c: cccccc the 2nd input operand (divisor)
417 #define OPC_DIVS 0x28040000
418 #define OPC_DIVSI (OPC_DIVS | OP_IMM)
421 * The 4-byte encoding of "remu a,b,c" (unsigned remainder):
423 * 0010_1bbb 0000_1001 0BBB_cccc ccaa_aaaa
425 * a: aaaaaa result (remainder)
426 * b: BBBbbb the 1st input operand
427 * c: cccccc the 2nd input operand (divisor)
429 #define OPC_REMU 0x28090000
430 #define OPC_REMUI (OPC_REMU | OP_IMM)
433 * The 4-byte encoding of "rem a,b,c" (signed remainder):
435 * 0010_1bbb 0000_1000 0BBB_cccc ccaa_aaaa
437 * a: aaaaaa result (remainder)
438 * b: BBBbbb the 1st input operand
439 * c: cccccc the 2nd input operand (divisor)
441 #define OPC_REMS 0x28080000
442 #define OPC_REMSI (OPC_REMS | OP_IMM)
445 * The 4-byte encoding of "and a,b,c":
447 * 0010_0bbb 0000_0100 fBBB_cccc ccaa_aaaa
449 * f: indicates if zero and negative flags should be updated
452 * b: BBBbbb the 1st input operand
453 * c: cccccc the 2nd input operand
455 #define OPC_AND 0x20040000
456 #define OPC_ANDI (OPC_AND | OP_IMM)
459 * The 4-byte encoding of "tst[.qq] b,c".
460 * Checks if the two input operands have any bit set at the same
463 * 0010_0bbb 1100_1011 1BBB_cccc cc0q_qqqq
465 * qq: qqqqq condition code
467 * b: BBBbbb the 1st input operand
468 * c: cccccc the 2nd input operand
470 #define OPC_TST 0x20cb8000
473 * The 4-byte encoding of "or a,b,c":
475 * 0010_0bbb 0000_0101 0BBB_cccc ccaa_aaaa
478 * b: BBBbbb the 1st input operand
479 * c: cccccc the 2nd input operand
481 #define OPC_OR 0x20050000
482 #define OPC_ORI (OPC_OR | OP_IMM)
485 * The 4-byte encoding of "xor a,b,c":
487 * 0010_0bbb 0000_0111 0BBB_cccc ccaa_aaaa
490 * b: BBBbbb the 1st input operand
491 * c: cccccc the 2nd input operand
493 #define OPC_XOR 0x20070000
494 #define OPC_XORI (OPC_XOR | OP_IMM)
497 * The 4-byte encoding of "not b,c":
499 * 0010_0bbb 0010_1111 0BBB_cccc cc00_1010
504 #define OPC_NOT 0x202f000a
507 * The 4-byte encoding of "btst b,u6":
509 * 0010_0bbb 0101_0001 1BBB_uuuu uu00_0000
511 * b: BBBbbb input number to check
512 * u6: uuuuuu 6-bit unsigned number specifying bit position to check
514 #define OPC_BTSTU6 0x20518000
515 #define BTST_U6(x) (OP_C((x) & 63))
518 * The 4-byte encoding of "asl[.qq] b,b,c" (arithmetic shift left):
520 * 0010_1bbb 0i00_0000 0BBB_cccc ccaa_aaaa
522 * i: if set, c is considered a 5-bit immediate, else a reg.
524 * b: BBBbbb result and the first operand (number to be shifted)
525 * c: cccccc amount to be shifted
527 #define OPC_ASL 0x28000000
528 #define ASL_I BIT(22)
529 #define ASLI_U6(x) OP_C((x) & 31)
530 #define OPC_ASLI (OPC_ASL | ASL_I)
533 * The 4-byte encoding of "asr a,b,c" (arithmetic shift right):
535 * 0010_1bbb 0i00_0010 0BBB_cccc ccaa_aaaa
537 * i: if set, c is considered a 6-bit immediate, else a reg.
540 * b: BBBbbb first input: number to be shifted
541 * c: cccccc second input: amount to be shifted
543 #define OPC_ASR 0x28020000
545 #define ASRI_U6(x) ASLI_U6(x)
546 #define OPC_ASRI (OPC_ASR | ASR_I)
549 * The 4-byte encoding of "lsr a,b,c" (logical shift right):
551 * 0010_1bbb 0i00_0001 0BBB_cccc ccaa_aaaa
553 * i: if set, c is considered a 6-bit immediate, else a reg.
556 * b: BBBbbb first input: number to be shifted
557 * c: cccccc second input: amount to be shifted
559 #define OPC_LSR 0x28010000
561 #define LSRI_U6(x) ASLI_U6(x)
562 #define OPC_LSRI (OPC_LSR | LSR_I)
565 * The 4-byte encoding of "swape b,c":
567 * 0010_1bbb 0010_1111 0bbb_cccc cc00_1001
569 * b: BBBbbb destination register
570 * c: cccccc source register
572 #define OPC_SWAPE 0x282f0009
575 * Encoding for jump to an address in register:
578 * 0010_0000 1110_0000 0000_cccc cc00_0000
580 * c: cccccc register holding the destination address
582 #define OPC_JMP 0x20e00000
583 /* Jump to "branch-and-link" register, which effectively is a "return". */
584 #define OPC_J_BLINK (OPC_JMP | OP_C(ARC_R_BLINK))
587 * Encoding for jump-and-link to an address in register:
590 * 0010_0000 0010_0010 0000_cccc cc00_0000
592 * c: cccccc register holding the destination address
594 #define OPC_JL 0x20220000
597 * Encoding for (conditional) branch to an offset from the current location
598 * that is word aligned: (PC & 0xffff_fffc) + s21
601 * 0000_0sss ssss_sss0 SSSS_SSSS SS0q_qqqq
603 * qq: qqqqq condition code
604 * s21: SSSS SSSS_SSss ssss_ssss The displacement (21-bit signed)
606 * The displacement is supposed to be 16-bit (2-byte) aligned. Therefore,
607 * it should be a multiple of 2. Hence, there is an implied '0' bit at its
608 * LSB: S_SSSS SSSS_Ssss ssss_sss0
610 #define OPC_BCC 0x00000000
611 #define BCC_S21(d) ((((d) & 0x7fe) << 16) | (((d) & 0x1ff800) >> 5))
614 * Encoding for unconditional branch to an offset from the current location
615 * that is word aligned: (PC & 0xffff_fffc) + s25
618 * 0000_0sss ssss_sss1 SSSS_SSSS SS00_TTTT
620 * s25: TTTT SSSS SSSS_SSss ssss_ssss The displacement (25-bit signed)
622 * The displacement is supposed to be 16-bit (2-byte) aligned. Therefore,
623 * it should be a multiple of 2. Hence, there is an implied '0' bit at its
624 * LSB: T TTTS_SSSS SSSS_Ssss ssss_sss0
626 #define OPC_B 0x00010000
627 #define B_S25(d) ((((d) & 0x1e00000) >> 21) | BCC_S21(d))
629 static inline void emit_2_bytes(u8 *buf, u16 bytes)
631 *((u16 *)buf) = bytes;
634 static inline void emit_4_bytes(u8 *buf, u32 bytes)
636 emit_2_bytes(buf, bytes >> 16);
637 emit_2_bytes(buf + 2, bytes & 0xffff);
640 static inline u8 bpf_to_arc_size(u8 size)
656 /************** Encoders (Deal with ARC regs) ************/
658 /* Move an immediate to register with a 4-byte instruction. */
659 static u8 arc_movi_r(u8 *buf, u8 reg, s16 imm)
661 const u32 insn = OPC_MOVI | OP_B(reg) | MOVI_S12(imm);
664 emit_4_bytes(buf, insn);
665 return INSN_len_normal;
669 static u8 arc_mov_r(u8 *buf, u8 rd, u8 rs)
671 const u32 insn = OPC_MOV | OP_B(rd) | OP_C(rs);
674 emit_4_bytes(buf, insn);
675 return INSN_len_normal;
678 /* The emitted code may have different sizes based on "imm". */
679 static u8 arc_mov_i(u8 *buf, u8 rd, s32 imm)
681 const u32 insn = OPC_MOV | OP_B(rd) | OP_IMM;
683 if (IN_S12_RANGE(imm))
684 return arc_movi_r(buf, rd, imm);
687 emit_4_bytes(buf, insn);
688 emit_4_bytes(buf + INSN_len_normal, imm);
690 return INSN_len_normal + INSN_len_imm;
693 /* The emitted code will always have the same size (8). */
694 static u8 arc_mov_i_fixed(u8 *buf, u8 rd, s32 imm)
696 const u32 insn = OPC_MOV | OP_B(rd) | OP_IMM;
699 emit_4_bytes(buf, insn);
700 emit_4_bytes(buf + INSN_len_normal, imm);
702 return INSN_len_normal + INSN_len_imm;
705 /* Conditional move. */
706 static u8 arc_mov_cc_r(u8 *buf, u8 cc, u8 rd, u8 rs)
708 const u32 insn = OPC_MOV_CC | OP_B(rd) | OP_C(rs) | COND(cc);
711 emit_4_bytes(buf, insn);
712 return INSN_len_normal;
715 /* Conditional move of a small immediate to rd. */
716 static u8 arc_movu_cc_r(u8 *buf, u8 cc, u8 rd, u8 imm)
718 const u32 insn = OPC_MOVU_CC | OP_B(rd) | OP_C(imm) | COND(cc);
721 emit_4_bytes(buf, insn);
722 return INSN_len_normal;
725 /* Sign extension from a byte. */
726 static u8 arc_sexb_r(u8 *buf, u8 rd, u8 rs)
728 const u32 insn = OPC_SEXB | OP_B(rd) | OP_C(rs);
731 emit_4_bytes(buf, insn);
732 return INSN_len_normal;
735 /* Sign extension from two bytes. */
736 static u8 arc_sexh_r(u8 *buf, u8 rd, u8 rs)
738 const u32 insn = OPC_SEXH | OP_B(rd) | OP_C(rs);
741 emit_4_bytes(buf, insn);
742 return INSN_len_normal;
745 /* st reg, [reg_mem, off] */
746 static u8 arc_st_r(u8 *buf, u8 reg, u8 reg_mem, s16 off, u8 zz)
748 const u32 insn = OPC_STORE | STORE_ZZ(zz) | OP_C(reg) |
749 OP_B(reg_mem) | STORE_S9(off);
752 emit_4_bytes(buf, insn);
753 return INSN_len_normal;
756 /* st.aw reg, [sp, -4] */
757 static u8 arc_push_r(u8 *buf, u8 reg)
759 const u32 insn = OPC_PUSH | OP_C(reg);
762 emit_4_bytes(buf, insn);
763 return INSN_len_normal;
766 /* ld reg, [reg_mem, off] (unsigned) */
767 static u8 arc_ld_r(u8 *buf, u8 reg, u8 reg_mem, s16 off, u8 zz)
769 const u32 insn = OPC_LDU | LOAD_ZZ(zz) | LOAD_C(reg) |
770 OP_B(reg_mem) | LOAD_S9(off);
773 emit_4_bytes(buf, insn);
774 return INSN_len_normal;
777 /* ld.x reg, [reg_mem, off] (sign extend) */
778 static u8 arc_ldx_r(u8 *buf, u8 reg, u8 reg_mem, s16 off, u8 zz)
780 const u32 insn = OPC_LDS | LOAD_ZZ(zz) | LOAD_C(reg) |
781 OP_B(reg_mem) | LOAD_S9(off);
784 emit_4_bytes(buf, insn);
785 return INSN_len_normal;
788 /* ld.ab reg,[sp,4] */
789 static u8 arc_pop_r(u8 *buf, u8 reg)
791 const u32 insn = OPC_POP | LOAD_C(reg);
794 emit_4_bytes(buf, insn);
795 return INSN_len_normal;
799 static u8 arc_add_r(u8 *buf, u8 ra, u8 rc)
801 const u32 insn = OPC_ADD | OP_A(ra) | OP_B(ra) | OP_C(rc);
804 emit_4_bytes(buf, insn);
805 return INSN_len_normal;
809 static u8 arc_addf_r(u8 *buf, u8 ra, u8 rc)
811 const u32 insn = OPC_ADDF | OP_A(ra) | OP_B(ra) | OP_C(rc);
814 emit_4_bytes(buf, insn);
815 return INSN_len_normal;
819 static u8 arc_addif_r(u8 *buf, u8 ra, u8 u6)
821 const u32 insn = OPC_ADDIF | OP_A(ra) | OP_B(ra) | ADDI_U6(u6);
824 emit_4_bytes(buf, insn);
825 return INSN_len_normal;
829 static u8 arc_addi_r(u8 *buf, u8 ra, u8 u6)
831 const u32 insn = OPC_ADDI | OP_A(ra) | OP_B(ra) | ADDI_U6(u6);
834 emit_4_bytes(buf, insn);
835 return INSN_len_normal;
839 static u8 arc_add_i(u8 *buf, u8 ra, u8 rb, s32 imm)
841 const u32 insn = OPC_ADD_I | OP_A(ra) | OP_B(rb);
844 emit_4_bytes(buf, insn);
845 emit_4_bytes(buf + INSN_len_normal, imm);
847 return INSN_len_normal + INSN_len_imm;
851 static u8 arc_adc_r(u8 *buf, u8 ra, u8 rc)
853 const u32 insn = OPC_ADC | OP_A(ra) | OP_B(ra) | OP_C(rc);
856 emit_4_bytes(buf, insn);
857 return INSN_len_normal;
861 static u8 arc_adci_r(u8 *buf, u8 ra, u8 u6)
863 const u32 insn = OPC_ADCI | OP_A(ra) | OP_B(ra) | ADCI_U6(u6);
866 emit_4_bytes(buf, insn);
867 return INSN_len_normal;
871 static u8 arc_sub_r(u8 *buf, u8 ra, u8 rc)
873 const u32 insn = OPC_SUB | OP_A(ra) | OP_B(ra) | OP_C(rc);
876 emit_4_bytes(buf, insn);
877 return INSN_len_normal;
881 static u8 arc_subf_r(u8 *buf, u8 ra, u8 rc)
883 const u32 insn = OPC_SUBF | OP_A(ra) | OP_B(ra) | OP_C(rc);
886 emit_4_bytes(buf, insn);
887 return INSN_len_normal;
891 static u8 arc_subi_r(u8 *buf, u8 ra, u8 u6)
893 const u32 insn = OPC_SUBI | OP_A(ra) | OP_B(ra) | SUBI_U6(u6);
896 emit_4_bytes(buf, insn);
897 return INSN_len_normal;
901 static u8 arc_sub_i(u8 *buf, u8 ra, s32 imm)
903 const u32 insn = OPC_SUB_I | OP_A(ra) | OP_B(ra);
906 emit_4_bytes(buf, insn);
907 emit_4_bytes(buf + INSN_len_normal, imm);
909 return INSN_len_normal + INSN_len_imm;
913 static u8 arc_sbc_r(u8 *buf, u8 ra, u8 rc)
915 const u32 insn = OPC_SBC | OP_A(ra) | OP_B(ra) | OP_C(rc);
918 emit_4_bytes(buf, insn);
919 return INSN_len_normal;
923 static u8 arc_cmp_r(u8 *buf, u8 rb, u8 rc)
925 const u32 insn = OPC_CMP | OP_B(rb) | OP_C(rc);
928 emit_4_bytes(buf, insn);
929 return INSN_len_normal;
935 * This "cmp.z" variant of compare instruction is used on lower
936 * 32-bits of register pairs after "cmp"ing their upper parts. If the
937 * upper parts are equal (z), then this one will proceed to check the
940 static u8 arc_cmpz_r(u8 *buf, u8 rb, u8 rc)
942 const u32 insn = OPC_CMP | OP_B(rb) | OP_C(rc) | CC_equal;
945 emit_4_bytes(buf, insn);
946 return INSN_len_normal;
950 static u8 arc_neg_r(u8 *buf, u8 ra, u8 rb)
952 const u32 insn = OPC_NEG | OP_A(ra) | OP_B(rb);
955 emit_4_bytes(buf, insn);
956 return INSN_len_normal;
960 static u8 arc_mpy_r(u8 *buf, u8 ra, u8 rb, u8 rc)
962 const u32 insn = OPC_MPY | OP_A(ra) | OP_B(rb) | OP_C(rc);
965 emit_4_bytes(buf, insn);
966 return INSN_len_normal;
970 static u8 arc_mpy_i(u8 *buf, u8 ra, u8 rb, s32 imm)
972 const u32 insn = OPC_MPYI | OP_A(ra) | OP_B(rb);
975 emit_4_bytes(buf, insn);
976 emit_4_bytes(buf + INSN_len_normal, imm);
978 return INSN_len_normal + INSN_len_imm;
982 static u8 arc_mpydu_r(u8 *buf, u8 ra, u8 rc)
984 const u32 insn = OPC_MPYDU | OP_A(ra) | OP_B(ra) | OP_C(rc);
987 emit_4_bytes(buf, insn);
988 return INSN_len_normal;
991 /* mpydu Ra,Ra,imm */
992 static u8 arc_mpydu_i(u8 *buf, u8 ra, s32 imm)
994 const u32 insn = OPC_MPYDUI | OP_A(ra) | OP_B(ra);
997 emit_4_bytes(buf, insn);
998 emit_4_bytes(buf + INSN_len_normal, imm);
1000 return INSN_len_normal + INSN_len_imm;
1004 static u8 arc_divu_r(u8 *buf, u8 rd, u8 rs)
1006 const u32 insn = OPC_DIVU | OP_A(rd) | OP_B(rd) | OP_C(rs);
1009 emit_4_bytes(buf, insn);
1010 return INSN_len_normal;
1013 /* divu Rd,Rd,imm */
1014 static u8 arc_divu_i(u8 *buf, u8 rd, s32 imm)
1016 const u32 insn = OPC_DIVUI | OP_A(rd) | OP_B(rd);
1019 emit_4_bytes(buf, insn);
1020 emit_4_bytes(buf + INSN_len_normal, imm);
1022 return INSN_len_normal + INSN_len_imm;
1026 static u8 arc_divs_r(u8 *buf, u8 rd, u8 rs)
1028 const u32 insn = OPC_DIVS | OP_A(rd) | OP_B(rd) | OP_C(rs);
1031 emit_4_bytes(buf, insn);
1032 return INSN_len_normal;
1036 static u8 arc_divs_i(u8 *buf, u8 rd, s32 imm)
1038 const u32 insn = OPC_DIVSI | OP_A(rd) | OP_B(rd);
1041 emit_4_bytes(buf, insn);
1042 emit_4_bytes(buf + INSN_len_normal, imm);
1044 return INSN_len_normal + INSN_len_imm;
1048 static u8 arc_remu_r(u8 *buf, u8 rd, u8 rs)
1050 const u32 insn = OPC_REMU | OP_A(rd) | OP_B(rd) | OP_C(rs);
1053 emit_4_bytes(buf, insn);
1054 return INSN_len_normal;
1057 /* remu Rd,Rd,imm */
1058 static u8 arc_remu_i(u8 *buf, u8 rd, s32 imm)
1060 const u32 insn = OPC_REMUI | OP_A(rd) | OP_B(rd);
1063 emit_4_bytes(buf, insn);
1064 emit_4_bytes(buf + INSN_len_normal, imm);
1066 return INSN_len_normal + INSN_len_imm;
1070 static u8 arc_rems_r(u8 *buf, u8 rd, u8 rs)
1072 const u32 insn = OPC_REMS | OP_A(rd) | OP_B(rd) | OP_C(rs);
1075 emit_4_bytes(buf, insn);
1076 return INSN_len_normal;
1080 static u8 arc_rems_i(u8 *buf, u8 rd, s32 imm)
1082 const u32 insn = OPC_REMSI | OP_A(rd) | OP_B(rd);
1085 emit_4_bytes(buf, insn);
1086 emit_4_bytes(buf + INSN_len_normal, imm);
1088 return INSN_len_normal + INSN_len_imm;
1092 static u8 arc_and_r(u8 *buf, u8 rd, u8 rs)
1094 const u32 insn = OPC_AND | OP_A(rd) | OP_B(rd) | OP_C(rs);
1097 emit_4_bytes(buf, insn);
1098 return INSN_len_normal;
1101 /* and Rd,Rd,limm */
1102 static u8 arc_and_i(u8 *buf, u8 rd, s32 imm)
1104 const u32 insn = OPC_ANDI | OP_A(rd) | OP_B(rd);
1107 emit_4_bytes(buf, insn);
1108 emit_4_bytes(buf + INSN_len_normal, imm);
1110 return INSN_len_normal + INSN_len_imm;
1114 static u8 arc_tst_r(u8 *buf, u8 rd, u8 rs)
1116 const u32 insn = OPC_TST | OP_B(rd) | OP_C(rs);
1119 emit_4_bytes(buf, insn);
1120 return INSN_len_normal;
1124 * This particular version, "tst.z ...", is meant to be used after a
1125 * "tst" on the low 32-bit of register pairs. If that "tst" is not
1126 * zero, then we don't need to test the upper 32-bits lest it sets
1129 static u8 arc_tstz_r(u8 *buf, u8 rd, u8 rs)
1131 const u32 insn = OPC_TST | OP_B(rd) | OP_C(rs) | CC_equal;
1134 emit_4_bytes(buf, insn);
1135 return INSN_len_normal;
1138 static u8 arc_or_r(u8 *buf, u8 rd, u8 rs1, u8 rs2)
1140 const u32 insn = OPC_OR | OP_A(rd) | OP_B(rs1) | OP_C(rs2);
1143 emit_4_bytes(buf, insn);
1144 return INSN_len_normal;
1147 static u8 arc_or_i(u8 *buf, u8 rd, s32 imm)
1149 const u32 insn = OPC_ORI | OP_A(rd) | OP_B(rd);
1152 emit_4_bytes(buf, insn);
1153 emit_4_bytes(buf + INSN_len_normal, imm);
1155 return INSN_len_normal + INSN_len_imm;
1158 static u8 arc_xor_r(u8 *buf, u8 rd, u8 rs)
1160 const u32 insn = OPC_XOR | OP_A(rd) | OP_B(rd) | OP_C(rs);
1163 emit_4_bytes(buf, insn);
1164 return INSN_len_normal;
1167 static u8 arc_xor_i(u8 *buf, u8 rd, s32 imm)
1169 const u32 insn = OPC_XORI | OP_A(rd) | OP_B(rd);
1172 emit_4_bytes(buf, insn);
1173 emit_4_bytes(buf + INSN_len_normal, imm);
1175 return INSN_len_normal + INSN_len_imm;
1178 static u8 arc_not_r(u8 *buf, u8 rd, u8 rs)
1180 const u32 insn = OPC_NOT | OP_B(rd) | OP_C(rs);
1183 emit_4_bytes(buf, insn);
1184 return INSN_len_normal;
1187 static u8 arc_btst_i(u8 *buf, u8 rs, u8 imm)
1189 const u32 insn = OPC_BTSTU6 | OP_B(rs) | BTST_U6(imm);
1192 emit_4_bytes(buf, insn);
1193 return INSN_len_normal;
1196 static u8 arc_asl_r(u8 *buf, u8 rd, u8 rs1, u8 rs2)
1198 const u32 insn = OPC_ASL | OP_A(rd) | OP_B(rs1) | OP_C(rs2);
1201 emit_4_bytes(buf, insn);
1202 return INSN_len_normal;
1205 static u8 arc_asli_r(u8 *buf, u8 rd, u8 rs, u8 imm)
1207 const u32 insn = OPC_ASLI | OP_A(rd) | OP_B(rs) | ASLI_U6(imm);
1210 emit_4_bytes(buf, insn);
1211 return INSN_len_normal;
1214 static u8 arc_asr_r(u8 *buf, u8 rd, u8 rs1, u8 rs2)
1216 const u32 insn = OPC_ASR | OP_A(rd) | OP_B(rs1) | OP_C(rs2);
1219 emit_4_bytes(buf, insn);
1220 return INSN_len_normal;
1223 static u8 arc_asri_r(u8 *buf, u8 rd, u8 rs, u8 imm)
1225 const u32 insn = OPC_ASRI | OP_A(rd) | OP_B(rs) | ASRI_U6(imm);
1228 emit_4_bytes(buf, insn);
1229 return INSN_len_normal;
1232 static u8 arc_lsr_r(u8 *buf, u8 rd, u8 rs1, u8 rs2)
1234 const u32 insn = OPC_LSR | OP_A(rd) | OP_B(rs1) | OP_C(rs2);
1237 emit_4_bytes(buf, insn);
1238 return INSN_len_normal;
1241 static u8 arc_lsri_r(u8 *buf, u8 rd, u8 rs, u8 imm)
1243 const u32 insn = OPC_LSRI | OP_A(rd) | OP_B(rs) | LSRI_U6(imm);
1246 emit_4_bytes(buf, insn);
1247 return INSN_len_normal;
1250 static u8 arc_swape_r(u8 *buf, u8 r)
1252 const u32 insn = OPC_SWAPE | OP_B(r) | OP_C(r);
1255 emit_4_bytes(buf, insn);
1256 return INSN_len_normal;
1259 static u8 arc_jmp_return(u8 *buf)
1262 emit_4_bytes(buf, OPC_J_BLINK);
1263 return INSN_len_normal;
1266 static u8 arc_jl(u8 *buf, u8 reg)
1268 const u32 insn = OPC_JL | OP_C(reg);
1271 emit_4_bytes(buf, insn);
1272 return INSN_len_normal;
1276 * Conditional jump to an address that is max 21 bits away (signed).
1280 static u8 arc_bcc(u8 *buf, u8 cc, int offset)
1282 const u32 insn = OPC_BCC | BCC_S21(offset) | COND(cc);
1285 emit_4_bytes(buf, insn);
1286 return INSN_len_normal;
1290 * Unconditional jump to an address that is max 25 bits away (signed).
1294 static u8 arc_b(u8 *buf, s32 offset)
1296 const u32 insn = OPC_B | B_S25(offset);
1299 emit_4_bytes(buf, insn);
1300 return INSN_len_normal;
1303 /************* Packers (Deal with BPF_REGs) **************/
1305 inline u8 zext(u8 *buf, u8 rd)
1307 if (rd != BPF_REG_FP)
1308 return arc_movi_r(buf, REG_HI(rd), 0);
1313 u8 mov_r32(u8 *buf, u8 rd, u8 rs, u8 sign_ext)
1319 len = arc_sexb_r(buf, REG_LO(rd), REG_LO(rs));
1320 else if (sign_ext == 16)
1321 len = arc_sexh_r(buf, REG_LO(rd), REG_LO(rs));
1322 else if (sign_ext == 32 && rd != rs)
1323 len = arc_mov_r(buf, REG_LO(rd), REG_LO(rs));
1328 /* Unsigned move. */
1331 len = arc_mov_r(buf, REG_LO(rd), REG_LO(rs));
1336 u8 mov_r32_i32(u8 *buf, u8 reg, s32 imm)
1338 return arc_mov_i(buf, REG_LO(reg), imm);
1341 u8 mov_r64(u8 *buf, u8 rd, u8 rs, u8 sign_ext)
1346 /* First handle the low 32-bit part. */
1347 len = mov_r32(buf, rd, rs, sign_ext);
1349 /* Now propagate the sign bit of LO to HI. */
1350 if (sign_ext == 8 || sign_ext == 16 || sign_ext == 32) {
1351 len += arc_asri_r(BUF(buf, len),
1352 REG_HI(rd), REG_LO(rd), 31);
1358 /* Unsigned move. */
1363 len = arc_mov_r(buf, REG_LO(rd), REG_LO(rs));
1365 if (rs != BPF_REG_FP)
1366 len += arc_mov_r(BUF(buf, len), REG_HI(rd), REG_HI(rs));
1367 /* BPF_REG_FP is mapped to 32-bit "fp" register. */
1369 len += arc_movi_r(BUF(buf, len), REG_HI(rd), 0);
1374 /* Sign extend the 32-bit immediate into 64-bit register pair. */
1375 u8 mov_r64_i32(u8 *buf, u8 reg, s32 imm)
1379 len = arc_mov_i(buf, REG_LO(reg), imm);
1381 /* BPF_REG_FP is mapped to 32-bit "fp" register. */
1382 if (reg != BPF_REG_FP) {
1384 len += arc_movi_r(BUF(buf, len), REG_HI(reg), 0);
1386 len += arc_movi_r(BUF(buf, len), REG_HI(reg), -1);
1393 * This is merely used for translation of "LD R, IMM64" instructions
1394 * of the BPF. These sort of instructions are sometimes used for
1395 * relocations. If during the normal pass, the relocation value is
1396 * not known, the BPF instruction may look something like:
1398 * LD R <- 0x0000_0001_0000_0001
1400 * Which will nicely translate to two 4-byte ARC instructions:
1402 * mov R_lo, 1 # imm is small enough to be s12
1403 * mov R_hi, 1 # same
1405 * However, during the extra pass, the IMM64 will have changed
1406 * to the resolved address and looks something like:
1408 * LD R <- 0x0000_0000_1234_5678
1410 * Now, the translated code will require 12 bytes:
1412 * mov R_lo, 0x12345678 # this is an 8-byte instruction
1413 * mov R_hi, 0 # still 4 bytes
1415 * Which in practice will result in overwriting the following
1416 * instruction. To avoid such cases, we will always emit codes
1419 u8 mov_r64_i64(u8 *buf, u8 reg, u32 lo, u32 hi)
1423 len = arc_mov_i_fixed(buf, REG_LO(reg), lo);
1424 len += arc_mov_i_fixed(BUF(buf, len), REG_HI(reg), hi);
1430 * If the "off"set is too big (doesn't encode as S9) for:
1432 * {ld,st} r, [rm, off]
1436 * add r10, REG_LO(rm), off
1438 * and make sure that r10 becomes the effective address:
1440 * {ld,st} r, [r10, 0]
1442 static u8 adjust_mem_access(u8 *buf, s16 *off, u8 size,
1443 u8 rm, u8 *arc_reg_mem)
1446 *arc_reg_mem = REG_LO(rm);
1448 if (!IN_S9_RANGE(*off) ||
1449 (size == BPF_DW && !IN_S9_RANGE(*off + 4))) {
1450 len += arc_add_i(BUF(buf, len),
1451 REG_LO(JIT_REG_TMP), REG_LO(rm), (u32)(*off));
1452 *arc_reg_mem = REG_LO(JIT_REG_TMP);
1459 /* store rs, [rd, off] */
1460 u8 store_r(u8 *buf, u8 rs, u8 rd, s16 off, u8 size)
1462 u8 len, arc_reg_mem;
1464 len = adjust_mem_access(buf, &off, size, rd, &arc_reg_mem);
1466 if (size == BPF_DW) {
1467 len += arc_st_r(BUF(buf, len), REG_LO(rs), arc_reg_mem,
1469 len += arc_st_r(BUF(buf, len), REG_HI(rs), arc_reg_mem,
1470 off + 4, ZZ_4_byte);
1472 u8 zz = bpf_to_arc_size(size);
1474 len += arc_st_r(BUF(buf, len), REG_LO(rs), arc_reg_mem,
1482 * For {8,16,32}-bit stores:
1485 * For 64-bit stores:
1491 u8 store_i(u8 *buf, s32 imm, u8 rd, s16 off, u8 size)
1493 u8 len, arc_reg_mem;
1494 /* REG_LO(JIT_REG_TMP) might be used by "adjust_mem_access()". */
1495 const u8 arc_rs = REG_HI(JIT_REG_TMP);
1497 len = adjust_mem_access(buf, &off, size, rd, &arc_reg_mem);
1499 if (size == BPF_DW) {
1500 len += arc_mov_i(BUF(buf, len), arc_rs, imm);
1501 len += arc_st_r(BUF(buf, len), arc_rs, arc_reg_mem,
1503 imm = (imm >= 0 ? 0 : -1);
1504 len += arc_mov_i(BUF(buf, len), arc_rs, imm);
1505 len += arc_st_r(BUF(buf, len), arc_rs, arc_reg_mem,
1506 off + 4, ZZ_4_byte);
1508 u8 zz = bpf_to_arc_size(size);
1510 len += arc_mov_i(BUF(buf, len), arc_rs, imm);
1511 len += arc_st_r(BUF(buf, len), arc_rs, arc_reg_mem, off, zz);
1518 * For the calling convention of a little endian machine, the LO part
1519 * must be on top of the stack.
1521 static u8 push_r64(u8 *buf, u8 reg)
1525 #ifdef __LITTLE_ENDIAN
1526 /* BPF_REG_FP is mapped to 32-bit "fp" register. */
1527 if (reg != BPF_REG_FP)
1528 len += arc_push_r(BUF(buf, len), REG_HI(reg));
1529 len += arc_push_r(BUF(buf, len), REG_LO(reg));
1531 len += arc_push_r(BUF(buf, len), REG_LO(reg));
1532 if (reg != BPF_REG_FP)
1533 len += arc_push_r(BUF(buf, len), REG_HI(reg));
1539 /* load rd, [rs, off] */
1540 u8 load_r(u8 *buf, u8 rd, u8 rs, s16 off, u8 size, bool sign_ext)
1542 u8 len, arc_reg_mem;
1544 len = adjust_mem_access(buf, &off, size, rs, &arc_reg_mem);
1546 if (size == BPF_B || size == BPF_H || size == BPF_W) {
1547 const u8 zz = bpf_to_arc_size(size);
1549 /* Use LD.X only if the data size is less than 32-bit. */
1550 if (sign_ext && (zz == ZZ_1_byte || zz == ZZ_2_byte)) {
1551 len += arc_ldx_r(BUF(buf, len), REG_LO(rd),
1552 arc_reg_mem, off, zz);
1554 len += arc_ld_r(BUF(buf, len), REG_LO(rd),
1555 arc_reg_mem, off, zz);
1559 /* Propagate the sign bit to the higher reg. */
1560 len += arc_asri_r(BUF(buf, len),
1561 REG_HI(rd), REG_LO(rd), 31);
1563 len += arc_movi_r(BUF(buf, len), REG_HI(rd), 0);
1565 } else if (size == BPF_DW) {
1567 * We are about to issue 2 consecutive loads:
1569 * ld rx, [rb, off+0]
1570 * ld ry, [rb, off+4]
1572 * If "rx" and "rb" are the same registers, then the order
1573 * should change to guarantee that "rb" remains intact
1574 * during these 2 operations:
1576 * ld ry, [rb, off+4]
1577 * ld rx, [rb, off+0]
1579 if (REG_LO(rd) != arc_reg_mem) {
1580 len += arc_ld_r(BUF(buf, len), REG_LO(rd), arc_reg_mem,
1582 len += arc_ld_r(BUF(buf, len), REG_HI(rd), arc_reg_mem,
1583 off + 4, ZZ_4_byte);
1585 len += arc_ld_r(BUF(buf, len), REG_HI(rd), arc_reg_mem,
1586 off + 4, ZZ_4_byte);
1587 len += arc_ld_r(BUF(buf, len), REG_LO(rd), arc_reg_mem,
1595 u8 add_r32(u8 *buf, u8 rd, u8 rs)
1597 return arc_add_r(buf, REG_LO(rd), REG_LO(rs));
1600 u8 add_r32_i32(u8 *buf, u8 rd, s32 imm)
1602 if (IN_U6_RANGE(imm))
1603 return arc_addi_r(buf, REG_LO(rd), imm);
1605 return arc_add_i(buf, REG_LO(rd), REG_LO(rd), imm);
1608 u8 add_r64(u8 *buf, u8 rd, u8 rs)
1612 len = arc_addf_r(buf, REG_LO(rd), REG_LO(rs));
1613 len += arc_adc_r(BUF(buf, len), REG_HI(rd), REG_HI(rs));
1617 u8 add_r64_i32(u8 *buf, u8 rd, s32 imm)
1621 if (IN_U6_RANGE(imm)) {
1622 len = arc_addif_r(buf, REG_LO(rd), imm);
1623 len += arc_adci_r(BUF(buf, len), REG_HI(rd), 0);
1625 len = mov_r64_i32(buf, JIT_REG_TMP, imm);
1626 len += add_r64(BUF(buf, len), rd, JIT_REG_TMP);
1631 u8 sub_r32(u8 *buf, u8 rd, u8 rs)
1633 return arc_sub_r(buf, REG_LO(rd), REG_LO(rs));
1636 u8 sub_r32_i32(u8 *buf, u8 rd, s32 imm)
1638 if (IN_U6_RANGE(imm))
1639 return arc_subi_r(buf, REG_LO(rd), imm);
1641 return arc_sub_i(buf, REG_LO(rd), imm);
1644 u8 sub_r64(u8 *buf, u8 rd, u8 rs)
1648 len = arc_subf_r(buf, REG_LO(rd), REG_LO(rs));
1649 len += arc_sbc_r(BUF(buf, len), REG_HI(rd), REG_HI(rs));
1653 u8 sub_r64_i32(u8 *buf, u8 rd, s32 imm)
1657 len = mov_r64_i32(buf, JIT_REG_TMP, imm);
1658 len += sub_r64(BUF(buf, len), rd, JIT_REG_TMP);
1662 static u8 cmp_r32(u8 *buf, u8 rd, u8 rs)
1664 return arc_cmp_r(buf, REG_LO(rd), REG_LO(rs));
1667 u8 neg_r32(u8 *buf, u8 r)
1669 return arc_neg_r(buf, REG_LO(r), REG_LO(r));
1672 /* In a two's complement system, -r is (~r + 1). */
1673 u8 neg_r64(u8 *buf, u8 r)
1677 len = arc_not_r(buf, REG_LO(r), REG_LO(r));
1678 len += arc_not_r(BUF(buf, len), REG_HI(r), REG_HI(r));
1679 len += add_r64_i32(BUF(buf, len), r, 1);
1683 u8 mul_r32(u8 *buf, u8 rd, u8 rs)
1685 return arc_mpy_r(buf, REG_LO(rd), REG_LO(rd), REG_LO(rs));
1688 u8 mul_r32_i32(u8 *buf, u8 rd, s32 imm)
1690 return arc_mpy_i(buf, REG_LO(rd), REG_LO(rd), imm);
1696 * mpy t0, B_hi, C_lo
1697 * mpy t1, B_lo, C_hi
1698 * mpydu B_lo, B_lo, C_lo
1699 * add B_hi, B_hi, t0
1700 * add B_hi, B_hi, t1
1702 u8 mul_r64(u8 *buf, u8 rd, u8 rs)
1704 const u8 t0 = REG_LO(JIT_REG_TMP);
1705 const u8 t1 = REG_HI(JIT_REG_TMP);
1706 const u8 C_lo = REG_LO(rs);
1707 const u8 C_hi = REG_HI(rs);
1708 const u8 B_lo = REG_LO(rd);
1709 const u8 B_hi = REG_HI(rd);
1712 len = arc_mpy_r(buf, t0, B_hi, C_lo);
1713 len += arc_mpy_r(BUF(buf, len), t1, B_lo, C_hi);
1714 len += arc_mpydu_r(BUF(buf, len), B_lo, C_lo);
1715 len += arc_add_r(BUF(buf, len), B_hi, t0);
1716 len += arc_add_r(BUF(buf, len), B_hi, t1);
1725 * To get a 64-bit result from a signed 64x32 multiplication:
1729 * -----------------------------
1730 * HI(B_lo*imm) LO(B_lo*imm) +
1733 * -----------------------------
1736 * mpy t1, B_lo, sign(imm)
1738 * mpydu B_lo, B_lo, imm
1739 * add B_hi, B_hi, t0
1740 * add B_hi, B_hi, t1
1742 * Note: We can't use signed double multiplication, "mpyd", instead of an
1743 * unsigned version, "mpydu", and then get rid of the sign adjustments
1744 * calculated in "t1". The signed multiplication, "mpyd", will consider
1745 * both operands, "B_lo" and "imm", as signed inputs. However, for this
1746 * 64x32 multiplication, "B_lo" must be treated as an unsigned number.
1748 u8 mul_r64_i32(u8 *buf, u8 rd, s32 imm)
1750 const u8 t0 = REG_LO(JIT_REG_TMP);
1751 const u8 t1 = REG_HI(JIT_REG_TMP);
1752 const u8 B_lo = REG_LO(rd);
1753 const u8 B_hi = REG_HI(rd);
1759 /* Is the sign-extension of the immediate "-1"? */
1761 len += arc_neg_r(BUF(buf, len), t1, B_lo);
1763 len += arc_mpy_i(BUF(buf, len), t0, B_hi, imm);
1764 len += arc_mpydu_i(BUF(buf, len), B_lo, imm);
1765 len += arc_add_r(BUF(buf, len), B_hi, t0);
1767 /* Add the "sign*B_lo" part, if necessary. */
1769 len += arc_add_r(BUF(buf, len), B_hi, t1);
1774 u8 div_r32(u8 *buf, u8 rd, u8 rs, bool sign_ext)
1777 return arc_divs_r(buf, REG_LO(rd), REG_LO(rs));
1779 return arc_divu_r(buf, REG_LO(rd), REG_LO(rs));
1782 u8 div_r32_i32(u8 *buf, u8 rd, s32 imm, bool sign_ext)
1788 return arc_divs_i(buf, REG_LO(rd), imm);
1790 return arc_divu_i(buf, REG_LO(rd), imm);
1793 u8 mod_r32(u8 *buf, u8 rd, u8 rs, bool sign_ext)
1796 return arc_rems_r(buf, REG_LO(rd), REG_LO(rs));
1798 return arc_remu_r(buf, REG_LO(rd), REG_LO(rs));
1801 u8 mod_r32_i32(u8 *buf, u8 rd, s32 imm, bool sign_ext)
1807 return arc_rems_i(buf, REG_LO(rd), imm);
1809 return arc_remu_i(buf, REG_LO(rd), imm);
1812 u8 and_r32(u8 *buf, u8 rd, u8 rs)
1814 return arc_and_r(buf, REG_LO(rd), REG_LO(rs));
1817 u8 and_r32_i32(u8 *buf, u8 rd, s32 imm)
1819 return arc_and_i(buf, REG_LO(rd), imm);
1822 u8 and_r64(u8 *buf, u8 rd, u8 rs)
1826 len = arc_and_r(buf, REG_LO(rd), REG_LO(rs));
1827 len += arc_and_r(BUF(buf, len), REG_HI(rd), REG_HI(rs));
1831 u8 and_r64_i32(u8 *buf, u8 rd, s32 imm)
1835 len = mov_r64_i32(buf, JIT_REG_TMP, imm);
1836 len += and_r64(BUF(buf, len), rd, JIT_REG_TMP);
1840 static u8 tst_r32(u8 *buf, u8 rd, u8 rs)
1842 return arc_tst_r(buf, REG_LO(rd), REG_LO(rs));
1845 u8 or_r32(u8 *buf, u8 rd, u8 rs)
1847 return arc_or_r(buf, REG_LO(rd), REG_LO(rd), REG_LO(rs));
1850 u8 or_r32_i32(u8 *buf, u8 rd, s32 imm)
1852 return arc_or_i(buf, REG_LO(rd), imm);
1855 u8 or_r64(u8 *buf, u8 rd, u8 rs)
1859 len = arc_or_r(buf, REG_LO(rd), REG_LO(rd), REG_LO(rs));
1860 len += arc_or_r(BUF(buf, len), REG_HI(rd), REG_HI(rd), REG_HI(rs));
1864 u8 or_r64_i32(u8 *buf, u8 rd, s32 imm)
1868 len = mov_r64_i32(buf, JIT_REG_TMP, imm);
1869 len += or_r64(BUF(buf, len), rd, JIT_REG_TMP);
1873 u8 xor_r32(u8 *buf, u8 rd, u8 rs)
1875 return arc_xor_r(buf, REG_LO(rd), REG_LO(rs));
1878 u8 xor_r32_i32(u8 *buf, u8 rd, s32 imm)
1880 return arc_xor_i(buf, REG_LO(rd), imm);
1883 u8 xor_r64(u8 *buf, u8 rd, u8 rs)
1887 len = arc_xor_r(buf, REG_LO(rd), REG_LO(rs));
1888 len += arc_xor_r(BUF(buf, len), REG_HI(rd), REG_HI(rs));
1892 u8 xor_r64_i32(u8 *buf, u8 rd, s32 imm)
1896 len = mov_r64_i32(buf, JIT_REG_TMP, imm);
1897 len += xor_r64(BUF(buf, len), rd, JIT_REG_TMP);
1901 /* "asl a,b,c" --> "a = (b << (c & 31))". */
1902 u8 lsh_r32(u8 *buf, u8 rd, u8 rs)
1904 return arc_asl_r(buf, REG_LO(rd), REG_LO(rd), REG_LO(rs));
1907 u8 lsh_r32_i32(u8 *buf, u8 rd, u8 imm)
1909 return arc_asli_r(buf, REG_LO(rd), REG_LO(rd), imm);
1916 * to_hi = lo >> (32-n) # (32-n) is the negate of "n" in a 5-bit width.
1924 * assembly translation for "LSH B, C"
1925 * (heavily influenced by ARC gcc)
1926 * -----------------------------------
1927 * not t0, C_lo # The first 3 lines are almost the same as:
1928 * lsr t1, B_lo, 1 # neg t0, C_lo
1929 * lsr t1, t1, t0 # lsr t1, B_lo, t0 --> t1 is "to_hi"
1930 * mov t0, C_lo* # with one important difference. In "neg"
1931 * asl B_lo, B_lo, t0 # version, when C_lo=0, t1 becomes B_lo while
1932 * asl B_hi, B_hi, t0 # it should be 0. The "not" approach instead,
1933 * or B_hi, B_hi, t1 # "shift"s t1 once and 31 times, practically
1934 * btst t0, 5 # setting it to 0 when C_lo=0.
1935 * mov.ne B_hi, B_lo**
1938 * *The "mov t0, C_lo" is necessary to cover the cases that C is the same
1941 * **ARC performs a shift in this manner: B <<= (C & 31)
1942 * For 32<=n<64, "n-32" and "n&31" are the same. Therefore, "B << n" and
1943 * "B << (n-32)" yield the same results. e.g. the results of "B << 35" and
1944 * "B << 3" are the same.
1946 * The behaviour is undefined for n >= 64.
1948 u8 lsh_r64(u8 *buf, u8 rd, u8 rs)
1950 const u8 t0 = REG_LO(JIT_REG_TMP);
1951 const u8 t1 = REG_HI(JIT_REG_TMP);
1952 const u8 C_lo = REG_LO(rs);
1953 const u8 B_lo = REG_LO(rd);
1954 const u8 B_hi = REG_HI(rd);
1957 len = arc_not_r(buf, t0, C_lo);
1958 len += arc_lsri_r(BUF(buf, len), t1, B_lo, 1);
1959 len += arc_lsr_r(BUF(buf, len), t1, t1, t0);
1960 len += arc_mov_r(BUF(buf, len), t0, C_lo);
1961 len += arc_asl_r(BUF(buf, len), B_lo, B_lo, t0);
1962 len += arc_asl_r(BUF(buf, len), B_hi, B_hi, t0);
1963 len += arc_or_r(BUF(buf, len), B_hi, B_hi, t1);
1964 len += arc_btst_i(BUF(buf, len), t0, 5);
1965 len += arc_mov_cc_r(BUF(buf, len), CC_unequal, B_hi, B_lo);
1966 len += arc_movu_cc_r(BUF(buf, len), CC_unequal, B_lo, 0);
1973 * to_hi = B_lo >> 32-n # extract upper n bits
1981 u8 lsh_r64_i32(u8 *buf, u8 rd, s32 imm)
1983 const u8 t0 = REG_LO(JIT_REG_TMP);
1984 const u8 B_lo = REG_LO(rd);
1985 const u8 B_hi = REG_HI(rd);
1986 const u8 n = (u8)imm;
1991 } else if (n <= 31) {
1992 len = arc_lsri_r(buf, t0, B_lo, 32 - n);
1993 len += arc_asli_r(BUF(buf, len), B_lo, B_lo, n);
1994 len += arc_asli_r(BUF(buf, len), B_hi, B_hi, n);
1995 len += arc_or_r(BUF(buf, len), B_hi, B_hi, t0);
1996 } else if (n <= 63) {
1997 len = arc_asli_r(buf, B_hi, B_lo, n - 32);
1998 len += arc_movi_r(BUF(buf, len), B_lo, 0);
2000 /* n >= 64 is undefined behaviour. */
2005 /* "lsr a,b,c" --> "a = (b >> (c & 31))". */
2006 u8 rsh_r32(u8 *buf, u8 rd, u8 rs)
2008 return arc_lsr_r(buf, REG_LO(rd), REG_LO(rd), REG_LO(rs));
2011 u8 rsh_r32_i32(u8 *buf, u8 rd, u8 imm)
2013 return arc_lsri_r(buf, REG_LO(rd), REG_LO(rd), imm);
2017 * For better commentary, see lsh_r64().
2022 * to_lo = hi << (32-n)
2036 * lsr B_hi, B_hi, t0
2037 * lsr B_lo, B_lo, t0
2043 u8 rsh_r64(u8 *buf, u8 rd, u8 rs)
2045 const u8 t0 = REG_LO(JIT_REG_TMP);
2046 const u8 t1 = REG_HI(JIT_REG_TMP);
2047 const u8 C_lo = REG_LO(rs);
2048 const u8 B_lo = REG_LO(rd);
2049 const u8 B_hi = REG_HI(rd);
2052 len = arc_not_r(buf, t0, C_lo);
2053 len += arc_asli_r(BUF(buf, len), t1, B_hi, 1);
2054 len += arc_asl_r(BUF(buf, len), t1, t1, t0);
2055 len += arc_mov_r(BUF(buf, len), t0, C_lo);
2056 len += arc_lsr_r(BUF(buf, len), B_hi, B_hi, t0);
2057 len += arc_lsr_r(BUF(buf, len), B_lo, B_lo, t0);
2058 len += arc_or_r(BUF(buf, len), B_lo, B_lo, t1);
2059 len += arc_btst_i(BUF(buf, len), t0, 5);
2060 len += arc_mov_cc_r(BUF(buf, len), CC_unequal, B_lo, B_hi);
2061 len += arc_movu_cc_r(BUF(buf, len), CC_unequal, B_hi, 0);
2068 * to_lo = B_lo << 32-n # extract lower n bits, right-padded with 32-n 0s
2076 u8 rsh_r64_i32(u8 *buf, u8 rd, s32 imm)
2078 const u8 t0 = REG_LO(JIT_REG_TMP);
2079 const u8 B_lo = REG_LO(rd);
2080 const u8 B_hi = REG_HI(rd);
2081 const u8 n = (u8)imm;
2086 } else if (n <= 31) {
2087 len = arc_asli_r(buf, t0, B_hi, 32 - n);
2088 len += arc_lsri_r(BUF(buf, len), B_lo, B_lo, n);
2089 len += arc_lsri_r(BUF(buf, len), B_hi, B_hi, n);
2090 len += arc_or_r(BUF(buf, len), B_lo, B_lo, t0);
2091 } else if (n <= 63) {
2092 len = arc_lsri_r(buf, B_lo, B_hi, n - 32);
2093 len += arc_movi_r(BUF(buf, len), B_hi, 0);
2095 /* n >= 64 is undefined behaviour. */
2100 /* "asr a,b,c" --> "a = (b s>> (c & 31))". */
2101 u8 arsh_r32(u8 *buf, u8 rd, u8 rs)
2103 return arc_asr_r(buf, REG_LO(rd), REG_LO(rd), REG_LO(rs));
2106 u8 arsh_r32_i32(u8 *buf, u8 rd, u8 imm)
2108 return arc_asri_r(buf, REG_LO(rd), REG_LO(rd), imm);
2112 * For comparison, see rsh_r64().
2117 * to_lo = hi << (32-n)
2122 * hi_sign = hi s>>31
2123 * lo = hi s>> (n-32)
2132 * asr B_hi, B_hi, t0
2133 * lsr B_lo, B_lo, t0
2136 * asr t0, B_hi, 31 # now, t0 = 0 or -1 based on B_hi's sign
2140 u8 arsh_r64(u8 *buf, u8 rd, u8 rs)
2142 const u8 t0 = REG_LO(JIT_REG_TMP);
2143 const u8 t1 = REG_HI(JIT_REG_TMP);
2144 const u8 C_lo = REG_LO(rs);
2145 const u8 B_lo = REG_LO(rd);
2146 const u8 B_hi = REG_HI(rd);
2149 len = arc_not_r(buf, t0, C_lo);
2150 len += arc_asli_r(BUF(buf, len), t1, B_hi, 1);
2151 len += arc_asl_r(BUF(buf, len), t1, t1, t0);
2152 len += arc_mov_r(BUF(buf, len), t0, C_lo);
2153 len += arc_asr_r(BUF(buf, len), B_hi, B_hi, t0);
2154 len += arc_lsr_r(BUF(buf, len), B_lo, B_lo, t0);
2155 len += arc_or_r(BUF(buf, len), B_lo, B_lo, t1);
2156 len += arc_btst_i(BUF(buf, len), t0, 5);
2157 len += arc_asri_r(BUF(buf, len), t0, B_hi, 31);
2158 len += arc_mov_cc_r(BUF(buf, len), CC_unequal, B_lo, B_hi);
2159 len += arc_mov_cc_r(BUF(buf, len), CC_unequal, B_hi, t0);
2166 * to_lo = lo << 32-n # extract lower n bits, right-padded with 32-n 0s
2172 * hi = (lo[msb] ? -1 : 0)
2174 u8 arsh_r64_i32(u8 *buf, u8 rd, s32 imm)
2176 const u8 t0 = REG_LO(JIT_REG_TMP);
2177 const u8 B_lo = REG_LO(rd);
2178 const u8 B_hi = REG_HI(rd);
2179 const u8 n = (u8)imm;
2184 } else if (n <= 31) {
2185 len = arc_asli_r(buf, t0, B_hi, 32 - n);
2186 len += arc_lsri_r(BUF(buf, len), B_lo, B_lo, n);
2187 len += arc_asri_r(BUF(buf, len), B_hi, B_hi, n);
2188 len += arc_or_r(BUF(buf, len), B_lo, B_lo, t0);
2189 } else if (n <= 63) {
2190 len = arc_asri_r(buf, B_lo, B_hi, n - 32);
2191 len += arc_movi_r(BUF(buf, len), B_hi, -1);
2192 len += arc_btst_i(BUF(buf, len), B_lo, 31);
2193 len += arc_movu_cc_r(BUF(buf, len), CC_equal, B_hi, 0);
2195 /* n >= 64 is undefined behaviour. */
2200 u8 gen_swap(u8 *buf, u8 rd, u8 size, u8 endian, bool force, bool do_zext)
2204 const u8 host_endian = BPF_FROM_BE;
2206 const u8 host_endian = BPF_FROM_LE;
2208 if (host_endian != endian || force) {
2212 * r = B4B3_B2B1 << 16 --> r = B2B1_0000
2213 * then, swape(r) would become the desired 0000_B1B2
2215 len = arc_asli_r(buf, REG_LO(rd), REG_LO(rd), 16);
2218 len += arc_swape_r(BUF(buf, len), REG_LO(rd));
2220 len += zext(BUF(buf, len), rd);
2224 * swap "hi" and "lo":
2228 * and then swap the bytes in "hi" and "lo".
2230 len = arc_xor_r(buf, REG_HI(rd), REG_LO(rd));
2231 len += arc_xor_r(BUF(buf, len), REG_LO(rd), REG_HI(rd));
2232 len += arc_xor_r(BUF(buf, len), REG_HI(rd), REG_LO(rd));
2233 len += arc_swape_r(BUF(buf, len), REG_LO(rd));
2234 len += arc_swape_r(BUF(buf, len), REG_HI(rd));
2237 /* The caller must have handled this. */
2241 * If the same endianness, there's not much to do other
2242 * than zeroing out the upper bytes based on the "size".
2246 len = arc_and_i(buf, REG_LO(rd), 0xffff);
2250 len += zext(BUF(buf, len), rd);
2255 /* The caller must have handled this. */
2263 * To create a frame, all that is needed is:
2267 * sub sp, <frame_size>
2269 * "push fp" is taken care of separately while saving the clobbered registers.
2270 * All that remains is copying SP value to FP and shrinking SP's address space
2271 * for any possible function call to come.
2273 static inline u8 frame_create(u8 *buf, u16 size)
2277 len = arc_mov_r(buf, ARC_R_FP, ARC_R_SP);
2278 if (IN_U6_RANGE(size))
2279 len += arc_subi_r(BUF(buf, len), ARC_R_SP, size);
2281 len += arc_sub_i(BUF(buf, len), ARC_R_SP, size);
2288 * The value of SP upon entering was copied to FP.
2290 static inline u8 frame_restore(u8 *buf)
2292 return arc_mov_r(buf, ARC_R_SP, ARC_R_FP);
2296 * Going from a JITed code to the native caller:
2298 * mov ARC_ABI_RET_lo, BPF_REG_0_lo # r0 <- r8
2299 * mov ARC_ABI_RET_hi, BPF_REG_0_hi # r1 <- r9
2301 static u8 bpf_to_arc_return(u8 *buf)
2305 len = arc_mov_r(buf, ARC_R_0, REG_LO(BPF_REG_0));
2306 len += arc_mov_r(BUF(buf, len), ARC_R_1, REG_HI(BPF_REG_0));
2311 * Coming back from an external (in-kernel) function to the JITed code:
2313 * mov ARC_ABI_RET_lo, BPF_REG_0_lo # r8 <- r0
2314 * mov ARC_ABI_RET_hi, BPF_REG_0_hi # r9 <- r1
2316 u8 arc_to_bpf_return(u8 *buf)
2320 len = arc_mov_r(buf, REG_LO(BPF_REG_0), ARC_R_0);
2321 len += arc_mov_r(BUF(buf, len), REG_HI(BPF_REG_0), ARC_R_1);
2326 * This translation leads to:
2328 * mov r10, addr # always an 8-byte instruction
2331 * The length of the "mov" must be fixed (8), otherwise it may diverge
2332 * during the normal and extra passes:
2334 * normal pass extra pass
2336 * 180: mov r10,0 | 180: mov r10,0x700578d8
2337 * 184: jl [r10] | 188: jl [r10]
2338 * 188: add.f r16,r16,0x1 | 18c: adc r17,r17,0
2339 * 18c: adc r17,r17,0 |
2341 * In the above example, the change from "r10 <- 0" to "r10 <- 0x700578d8"
2342 * has led to an increase in the length of the "mov" instruction.
2343 * Inadvertently, that caused the loss of the "add.f" instruction.
2345 static u8 jump_and_link(u8 *buf, u32 addr)
2349 len = arc_mov_i_fixed(buf, REG_LO(JIT_REG_TMP), addr);
2350 len += arc_jl(BUF(buf, len), REG_LO(JIT_REG_TMP));
2355 * This function determines which ARC registers must be saved and restored.
2356 * It does so by looking into:
2358 * "bpf_reg": The clobbered (destination) BPF register
2359 * "is_call": Indicator if the current instruction is a call
2361 * When a register of interest is clobbered, its corresponding bit position
2362 * in return value, "usage", is set to true.
2364 u32 mask_for_used_regs(u8 bpf_reg, bool is_call)
2368 /* BPF registers that must be saved. */
2369 if (bpf_reg >= BPF_REG_6 && bpf_reg <= BPF_REG_9) {
2370 usage |= BIT(REG_LO(bpf_reg));
2371 usage |= BIT(REG_HI(bpf_reg));
2373 * Using the frame pointer register implies that it should
2374 * be saved and reinitialised with the current frame data.
2376 } else if (bpf_reg == BPF_REG_FP) {
2377 usage |= BIT(REG_LO(BPF_REG_FP));
2378 /* Could there be some ARC registers that must to be saved? */
2380 if (REG_LO(bpf_reg) >= ARC_CALLEE_SAVED_REG_FIRST &&
2381 REG_LO(bpf_reg) <= ARC_CALLEE_SAVED_REG_LAST)
2382 usage |= BIT(REG_LO(bpf_reg));
2384 if (REG_HI(bpf_reg) >= ARC_CALLEE_SAVED_REG_FIRST &&
2385 REG_HI(bpf_reg) <= ARC_CALLEE_SAVED_REG_LAST)
2386 usage |= BIT(REG_HI(bpf_reg));
2389 /* A "call" indicates that ARC's "blink" reg must be saved. */
2390 usage |= is_call ? BIT(ARC_R_BLINK) : 0;
2396 * push blink # if blink is marked as clobbered
2397 * push r[0-n] # if r[i] is marked as clobbered
2398 * push fp # if fp is marked as clobbered
2399 * mov fp, sp # if frame_size > 0 (clobbers fp)
2400 * sub sp, <frame_size> # same as above
2402 u8 arc_prologue(u8 *buf, u32 usage, u16 frame_size)
2407 /* Deal with blink first. */
2408 if (usage & BIT(ARC_R_BLINK))
2409 len += arc_push_r(BUF(buf, len), ARC_R_BLINK);
2411 gp_regs = usage & ~(BIT(ARC_R_BLINK) | BIT(ARC_R_FP));
2413 u8 reg = __builtin_ffs(gp_regs) - 1;
2415 len += arc_push_r(BUF(buf, len), reg);
2416 gp_regs &= ~BIT(reg);
2419 /* Deal with fp last. */
2420 if ((usage & BIT(ARC_R_FP)) || frame_size > 0)
2421 len += arc_push_r(BUF(buf, len), ARC_R_FP);
2424 len += frame_create(BUF(buf, len), frame_size);
2426 #ifdef ARC_BPF_JIT_DEBUG
2427 if ((usage & BIT(ARC_R_FP)) && frame_size == 0) {
2428 pr_err("FP is being saved while there is no frame.");
2437 * mov sp, fp # if frame_size > 0
2438 * pop fp # if fp is marked as clobbered
2439 * pop r[n-0] # if r[i] is marked as clobbered
2440 * pop blink # if blink is marked as clobbered
2441 * mov r0, r8 # always: ABI_return <- BPF_return
2442 * mov r1, r9 # continuation of above
2443 * j [blink] # always
2445 * "fp being marked as clobbered" and "frame_size > 0" are the two sides of
2448 u8 arc_epilogue(u8 *buf, u32 usage, u16 frame_size)
2453 #ifdef ARC_BPF_JIT_DEBUG
2454 if ((usage & BIT(ARC_R_FP)) && frame_size == 0) {
2455 pr_err("FP is being saved while there is no frame.");
2461 len += frame_restore(BUF(buf, len));
2463 /* Deal with fp first. */
2464 if ((usage & BIT(ARC_R_FP)) || frame_size > 0)
2465 len += arc_pop_r(BUF(buf, len), ARC_R_FP);
2467 gp_regs = usage & ~(BIT(ARC_R_BLINK) | BIT(ARC_R_FP));
2469 /* "usage" is 32-bit, each bit indicating an ARC register. */
2470 u8 reg = 31 - __builtin_clz(gp_regs);
2472 len += arc_pop_r(BUF(buf, len), reg);
2473 gp_regs &= ~BIT(reg);
2476 /* Deal with blink last. */
2477 if (usage & BIT(ARC_R_BLINK))
2478 len += arc_pop_r(BUF(buf, len), ARC_R_BLINK);
2480 /* Wrap up the return value and jump back to the caller. */
2481 len += bpf_to_arc_return(BUF(buf, len));
2482 len += arc_jmp_return(BUF(buf, len));
2488 * For details on the algorithm, see the comments of "gen_jcc_64()".
2490 * This data structure is holding information for jump translations.
2492 * jit_off: How many bytes into the current JIT address, "b"ranch insn. occurs
2493 * cond: The condition that the ARC branch instruction must use
2497 * BPF_JGE R1, R0, @target
2498 * ------------------------
2501 * 0x1000: cmp r3, r1 # 0x1000 is the JIT address for "BPF_JGE ..." insn
2502 * 0x1004: bhi @target # first jump (branch higher)
2503 * 0x1008: blo @end # second jump acting as a skip (end is 0x1014)
2504 * 0x100C: cmp r2, r0 # the lower 32 bits are evaluated
2505 * 0x1010: bhs @target # third jump (branch higher or same)
2508 * The jit_off(set) of the "bhi" is 4 bytes.
2509 * The cond(ition) for the "bhi" is "CC_great_u".
2511 * The jit_off(set) is necessary for calculating the exact displacement
2512 * to the "target" address:
2514 * jit_address + jit_off(set) - @target
2515 * 0x1000 + 4 - @target
2517 #define JCC64_NR_OF_JMPS 3 /* Number of jumps in jcc64 template. */
2518 #define JCC64_INSNS_TO_END 3 /* Number of insn. inclusive the 2nd jmp to end. */
2519 #define JCC64_SKIP_JMP 1 /* Index of the "skip" jump to "end". */
2522 * "jit_off" is common between all "jmp[]" and is coupled with
2523 * "cond" of each "jmp[]" instance. e.g.:
2525 * arcv2_64_jccs.jit_off[1]
2526 * arcv2_64_jccs.jmp[ARC_CC_UGT].cond[1]
2528 * Are indicating that the second jump in JITed code of "UGT"
2529 * is at offset "jit_off[1]" while its condition is "cond[1]".
2531 u8 jit_off[JCC64_NR_OF_JMPS];
2534 u8 cond[JCC64_NR_OF_JMPS];
2535 } jmp[ARC_CC_SLE + 1];
2538 INSN_len_normal * 1,
2539 INSN_len_normal * 2,
2544 * bhi @target # 1: u>
2547 * bhi @target # 3: u>
2550 .jmp[ARC_CC_UGT] = {
2551 .cond = {CC_great_u, CC_less_u, CC_great_u}
2555 * bhi @target # 1: u>
2558 * bhs @target # 3: u>=
2561 .jmp[ARC_CC_UGE] = {
2562 .cond = {CC_great_u, CC_less_u, CC_great_eq_u}
2566 * blo @target # 1: u<
2569 * blo @target # 3: u<
2572 .jmp[ARC_CC_ULT] = {
2573 .cond = {CC_less_u, CC_great_u, CC_less_u}
2577 * blo @target # 1: u<
2580 * bls @target # 3: u<=
2583 .jmp[ARC_CC_ULE] = {
2584 .cond = {CC_less_u, CC_great_u, CC_less_eq_u}
2588 * bgt @target # 1: s>
2591 * bhi @target # 3: u>
2594 .jmp[ARC_CC_SGT] = {
2595 .cond = {CC_great_s, CC_less_s, CC_great_u}
2599 * bgt @target # 1: s>
2602 * bhs @target # 3: u>=
2605 .jmp[ARC_CC_SGE] = {
2606 .cond = {CC_great_s, CC_less_s, CC_great_eq_u}
2610 * blt @target # 1: s<
2613 * blo @target # 3: u<
2616 .jmp[ARC_CC_SLT] = {
2617 .cond = {CC_less_s, CC_great_s, CC_less_u}
2621 * blt @target # 1: s<
2624 * bls @target # 3: u<=
2627 .jmp[ARC_CC_SLE] = {
2628 .cond = {CC_less_s, CC_great_s, CC_less_eq_u}
2633 * The displacement (offset) for ARC's "b"ranch instruction is the distance
2634 * from the aligned version of _current_ instruction (PCL) to the target
2637 * DISP = TARGET - PCL # PCL is the word aligned PC
2639 static inline s32 get_displacement(u32 curr_off, u32 targ_off)
2641 return (s32)(targ_off - (curr_off & ~3L));
2645 * "disp"lacement should be:
2647 * 1. 16-bit aligned.
2648 * 2. fit in S25, because no "condition code" is supposed to be encoded.
2650 static inline bool is_valid_far_disp(s32 disp)
2652 return (!(disp & 1) && IN_S25_RANGE(disp));
2656 * "disp"lacement should be:
2658 * 1. 16-bit aligned.
2659 * 2. fit in S21, because "condition code" is supposed to be encoded too.
2661 static inline bool is_valid_near_disp(s32 disp)
2663 return (!(disp & 1) && IN_S21_RANGE(disp));
2668 * cmp.z rd_lo, rs_lo
2671 * | `--> "eq" param is false (JNE)
2672 * `-----> "eq" param is true (JEQ)
2674 static int gen_j_eq_64(u8 *buf, u8 rd, u8 rs, bool eq,
2675 u32 curr_off, u32 targ_off)
2680 len += arc_cmp_r(BUF(buf, len), REG_HI(rd), REG_HI(rs));
2681 len += arc_cmpz_r(BUF(buf, len), REG_LO(rd), REG_LO(rs));
2682 disp = get_displacement(curr_off + len, targ_off);
2683 len += arc_bcc(BUF(buf, len), eq ? CC_equal : CC_unequal, disp);
2690 * tst.z rd_lo, rs_lo
2693 static u8 gen_jset_64(u8 *buf, u8 rd, u8 rs, u32 curr_off, u32 targ_off)
2698 len += arc_tst_r(BUF(buf, len), REG_HI(rd), REG_HI(rs));
2699 len += arc_tstz_r(BUF(buf, len), REG_LO(rd), REG_LO(rs));
2700 disp = get_displacement(curr_off + len, targ_off);
2701 len += arc_bcc(BUF(buf, len), CC_unequal, disp);
2707 * Verify if all the jumps for a JITed jcc64 operation are valid,
2708 * by consulting the data stored at "arcv2_64_jccs".
2710 static bool check_jcc_64(u32 curr_off, u32 targ_off, u8 cond)
2714 if (cond >= ARC_CC_LAST)
2717 for (i = 0; i < JCC64_NR_OF_JMPS; i++) {
2720 from = curr_off + arcv2_64_jccs.jit_off[i];
2721 /* for the 2nd jump, we jump to the end of block. */
2722 if (i != JCC64_SKIP_JMP)
2725 to = from + (JCC64_INSNS_TO_END * INSN_len_normal);
2726 /* There is a "cc" in the instruction, so a "near" jump. */
2727 if (!is_valid_near_disp(get_displacement(from, to)))
2734 /* Can the jump from "curr_off" to "targ_off" actually happen? */
2735 bool check_jmp_64(u32 curr_off, u32 targ_off, u8 cond)
2748 return check_jcc_64(curr_off, targ_off, cond);
2753 * The "jump" for the JITed BPF_J{SET,EQ,NE} is actually the
2754 * 3rd instruction. See comments of "gen_j{set,_eq}_64()".
2756 curr_off += 2 * INSN_len_normal;
2757 disp = get_displacement(curr_off, targ_off);
2758 /* There is a "cc" field in the issued instruction. */
2759 return is_valid_near_disp(disp);
2761 disp = get_displacement(curr_off, targ_off);
2762 return is_valid_far_disp(disp);
2769 * The template for the 64-bit jumps with the following BPF conditions
2771 * u< u<= u> u>= s< s<= s> s>=
2778 * cmp rd_lo, rs_lo # if execution reaches here, r{d,s}_hi are equal
2782 * "c1" is the condition that JIT is handling minus the equality part.
2783 * For instance if we have to translate an "unsigned greater or equal",
2784 * then "c1" will be "unsigned greater". We won't know about equality
2785 * until all 64-bits of data (higeher and lower registers) are processed.
2787 * "c2" is the counter logic of "c1". For instance, if "c1" is originated
2788 * from "s>", then "c2" would be "s<". Notice that equality doesn't play
2789 * a role here either, because the lower 32 bits are not processed yet.
2791 * "c3" is the unsigned version of "c1", no matter if the BPF condition
2792 * was signed or unsigned. An unsigned version is necessary, because the
2793 * MSB of the lower 32 bits does not reflect a sign in the whole 64-bit
2794 * scheme. Otherwise, 64-bit comparisons like
2795 * (0x0000_0000,0x8000_0000) s>= (0x0000_0000,0x0000_0000)
2796 * would yield an incorrect result. Finally, if there is an equality
2797 * check in the BPF condition, it will be reflected in "c3".
2799 * You can find all the instances of this template where the
2800 * "arcv2_64_jccs" is getting initialised.
2802 static u8 gen_jcc_64(u8 *buf, u8 rd, u8 rs, u8 cond,
2803 u32 curr_off, u32 targ_off)
2807 const u8 *cc = arcv2_64_jccs.jmp[cond].cond;
2810 /* cmp rd_hi, rs_hi */
2811 len += arc_cmp_r(buf, REG_HI(rd), REG_HI(rs));
2814 disp = get_displacement(curr_off + len, targ_off);
2815 len += arc_bcc(BUF(buf, len), cc[0], disp);
2818 end_off = curr_off + len + (JCC64_INSNS_TO_END * INSN_len_normal);
2819 disp = get_displacement(curr_off + len, end_off);
2820 len += arc_bcc(BUF(buf, len), cc[1], disp);
2822 /* cmp rd_lo, rs_lo */
2823 len += arc_cmp_r(BUF(buf, len), REG_LO(rd), REG_LO(rs));
2826 disp = get_displacement(curr_off + len, targ_off);
2827 len += arc_bcc(BUF(buf, len), cc[2], disp);
2833 * This function only applies the necessary logic to make the proper
2834 * translations. All the sanity checks must have already been done
2835 * by calling the check_jmp_64().
2837 u8 gen_jmp_64(u8 *buf, u8 rd, u8 rs, u8 cond, u32 curr_off, u32 targ_off)
2845 disp = get_displacement(curr_off, targ_off);
2846 len = arc_b(buf, disp);
2856 len = gen_jcc_64(buf, rd, rs, cond, curr_off, targ_off);
2862 len = gen_j_eq_64(buf, rd, rs, eq, curr_off, targ_off);
2865 len = gen_jset_64(buf, rd, rs, curr_off, targ_off);
2868 #ifdef ARC_BPF_JIT_DEBUG
2869 pr_err("64-bit jump condition is not known.");
2877 * The condition codes to use when generating JIT instructions
2880 * The "ARC_CC_AL" index is not really used by the code, but it
2881 * is here for the sake of completeness.
2883 * The "ARC_CC_SET" becomes "CC_unequal" because of the "tst"
2884 * instruction that precedes the conditional branch.
2886 const u8 arcv2_32_jmps[ARC_CC_LAST] = {
2887 [ARC_CC_UGT] = CC_great_u,
2888 [ARC_CC_UGE] = CC_great_eq_u,
2889 [ARC_CC_ULT] = CC_less_u,
2890 [ARC_CC_ULE] = CC_less_eq_u,
2891 [ARC_CC_SGT] = CC_great_s,
2892 [ARC_CC_SGE] = CC_great_eq_s,
2893 [ARC_CC_SLT] = CC_less_s,
2894 [ARC_CC_SLE] = CC_less_eq_s,
2895 [ARC_CC_AL] = CC_always,
2896 [ARC_CC_EQ] = CC_equal,
2897 [ARC_CC_NE] = CC_unequal,
2898 [ARC_CC_SET] = CC_unequal
2901 /* Can the jump from "curr_off" to "targ_off" actually happen? */
2902 bool check_jmp_32(u32 curr_off, u32 targ_off, u8 cond)
2907 if (cond >= ARC_CC_LAST)
2911 * The unconditional jump happens immediately, while the rest
2912 * are either preceded by a "cmp" or "tst" instruction.
2914 addendum = (cond == ARC_CC_AL) ? 0 : INSN_len_normal;
2915 disp = get_displacement(curr_off + addendum, targ_off);
2918 return is_valid_far_disp(disp);
2920 return is_valid_near_disp(disp);
2924 * The JITed code for 32-bit (conditional) branches:
2929 * ARC_CC_SET rd, rs, @target
2931 * bnz @jit_targ_addr
2933 * ARC_CC_xx rd, rs, @target
2935 * b<cc> @jit_targ_addr # cc = arcv2_32_jmps[xx]
2937 u8 gen_jmp_32(u8 *buf, u8 rd, u8 rs, u8 cond, u32 curr_off, u32 targ_off)
2943 * Although this must have already been checked by "check_jmp_32()",
2944 * we're not going to risk accessing "arcv2_32_jmps" array without
2945 * the boundary check.
2947 if (cond >= ARC_CC_LAST) {
2948 #ifdef ARC_BPF_JIT_DEBUG
2949 pr_err("32-bit jump condition is not known.");
2955 /* If there is a "condition", issue the "cmp" or "tst" first. */
2956 if (cond != ARC_CC_AL) {
2957 if (cond == ARC_CC_SET)
2958 len = tst_r32(buf, rd, rs);
2960 len = cmp_r32(buf, rd, rs);
2962 * The issued instruction affects the "disp"lacement as
2963 * it alters the "curr_off" by its "len"gth. The "curr_off"
2964 * should always point to the jump instruction.
2966 disp = get_displacement(curr_off + len, targ_off);
2967 len += arc_bcc(BUF(buf, len), arcv2_32_jmps[cond], disp);
2969 /* The straight forward unconditional jump. */
2970 disp = get_displacement(curr_off, targ_off);
2971 len = arc_b(buf, disp);
2978 * Generate code for functions calls. There can be two types of calls:
2980 * - Calling another BPF function
2981 * - Calling an in-kernel function which is compiled by ARC gcc
2983 * In the later case, we must comply to ARCv2 ABI and handle arguments
2984 * and return values accordingly.
2986 u8 gen_func_call(u8 *buf, ARC_ADDR func_addr, bool external_func)
2991 * In case of an in-kernel function call, always push the 5th
2992 * argument onto the stack, because that's where the ABI dictates
2993 * it should be found. If the callee doesn't really use it, no harm
2994 * is done. The stack is readjusted either way after the call.
2997 len += push_r64(BUF(buf, len), BPF_REG_5);
2999 len += jump_and_link(BUF(buf, len), func_addr);
3002 len += arc_add_i(BUF(buf, len), ARC_R_SP, ARC_R_SP, ARG5_SIZE);