Lines Matching defs:ctx
41 static u8 bpf_to_rv_reg(int bpf_reg, struct rv_jit_context *ctx)
52 __set_bit(reg, &ctx->flags);
57 static bool seen_reg(int reg, struct rv_jit_context *ctx)
67 return test_bit(reg, &ctx->flags);
72 static void mark_fp(struct rv_jit_context *ctx)
74 __set_bit(RV_CTX_F_SEEN_S5, &ctx->flags);
77 static void mark_call(struct rv_jit_context *ctx)
79 __set_bit(RV_CTX_F_SEEN_CALL, &ctx->flags);
82 static bool seen_call(struct rv_jit_context *ctx)
84 return test_bit(RV_CTX_F_SEEN_CALL, &ctx->flags);
87 static void mark_tail_call(struct rv_jit_context *ctx)
89 __set_bit(RV_CTX_F_SEEN_TAIL_CALL, &ctx->flags);
92 static bool seen_tail_call(struct rv_jit_context *ctx)
94 return test_bit(RV_CTX_F_SEEN_TAIL_CALL, &ctx->flags);
97 static u8 rv_tail_call_reg(struct rv_jit_context *ctx)
99 mark_tail_call(ctx);
101 if (seen_call(ctx)) {
102 __set_bit(RV_CTX_F_SEEN_S6, &ctx->flags);
123 static void emit_imm(u8 rd, s64 val, struct rv_jit_context *ctx)
144 emit_lui(rd, upper, ctx);
147 emit_li(rd, lower, ctx);
151 emit_addiw(rd, rd, lower, ctx);
159 emit_imm(rd, upper, ctx);
161 emit_slli(rd, rd, shift, ctx);
163 emit_addi(rd, rd, lower, ctx);
166 static void __build_epilogue(bool is_tail_call, struct rv_jit_context *ctx)
168 int stack_adjust = ctx->stack_size, store_offset = stack_adjust - 8;
170 if (seen_reg(RV_REG_RA, ctx)) {
171 emit_ld(RV_REG_RA, store_offset, RV_REG_SP, ctx);
174 emit_ld(RV_REG_FP, store_offset, RV_REG_SP, ctx);
176 if (seen_reg(RV_REG_S1, ctx)) {
177 emit_ld(RV_REG_S1, store_offset, RV_REG_SP, ctx);
180 if (seen_reg(RV_REG_S2, ctx)) {
181 emit_ld(RV_REG_S2, store_offset, RV_REG_SP, ctx);
184 if (seen_reg(RV_REG_S3, ctx)) {
185 emit_ld(RV_REG_S3, store_offset, RV_REG_SP, ctx);
188 if (seen_reg(RV_REG_S4, ctx)) {
189 emit_ld(RV_REG_S4, store_offset, RV_REG_SP, ctx);
192 if (seen_reg(RV_REG_S5, ctx)) {
193 emit_ld(RV_REG_S5, store_offset, RV_REG_SP, ctx);
196 if (seen_reg(RV_REG_S6, ctx)) {
197 emit_ld(RV_REG_S6, store_offset, RV_REG_SP, ctx);
201 emit_addi(RV_REG_SP, RV_REG_SP, stack_adjust, ctx);
204 emit_addiw(RV_REG_A0, RV_REG_A5, 0, ctx);
207 ctx);
211 struct rv_jit_context *ctx)
215 emit(rv_beq(rd, rs, rvoff >> 1), ctx);
218 emit(rv_bltu(rs, rd, rvoff >> 1), ctx);
221 emit(rv_bltu(rd, rs, rvoff >> 1), ctx);
224 emit(rv_bgeu(rd, rs, rvoff >> 1), ctx);
227 emit(rv_bgeu(rs, rd, rvoff >> 1), ctx);
230 emit(rv_bne(rd, rs, rvoff >> 1), ctx);
233 emit(rv_blt(rs, rd, rvoff >> 1), ctx);
236 emit(rv_blt(rd, rs, rvoff >> 1), ctx);
239 emit(rv_bge(rd, rs, rvoff >> 1), ctx);
242 emit(rv_bge(rs, rd, rvoff >> 1), ctx);
247 struct rv_jit_context *ctx)
252 emit_bcc(cond, rd, rs, rvoff, ctx);
269 emit_bcc(cond, rd, rs, 8, ctx);
270 emit(rv_jal(RV_REG_ZERO, rvoff >> 1), ctx);
280 emit_bcc(cond, rd, rs, 12, ctx);
281 emit(rv_auipc(RV_REG_T1, upper), ctx);
282 emit(rv_jalr(RV_REG_ZERO, RV_REG_T1, lower), ctx);
285 static void emit_zext_32(u8 reg, struct rv_jit_context *ctx)
287 emit_slli(reg, reg, 32, ctx);
288 emit_srli(reg, reg, 32, ctx);
291 static int emit_bpf_tail_call(int insn, struct rv_jit_context *ctx)
293 int tc_ninsn, off, start_insn = ctx->ninsns;
294 u8 tcc = rv_tail_call_reg(ctx);
296 /* a0: &ctx
303 tc_ninsn = insn ? ctx->offset[insn] - ctx->offset[insn - 1] :
304 ctx->offset[0];
305 emit_zext_32(RV_REG_A2, ctx);
310 emit(rv_lwu(RV_REG_T1, off, RV_REG_A1), ctx);
311 off = ninsns_rvoff(tc_ninsn - (ctx->ninsns - start_insn));
312 emit_branch(BPF_JGE, RV_REG_A2, RV_REG_T1, off, ctx);
317 emit_addi(RV_REG_T1, tcc, -1, ctx);
318 off = ninsns_rvoff(tc_ninsn - (ctx->ninsns - start_insn));
319 emit_branch(BPF_JSLT, tcc, RV_REG_ZERO, off, ctx);
325 emit_slli(RV_REG_T2, RV_REG_A2, 3, ctx);
326 emit_add(RV_REG_T2, RV_REG_T2, RV_REG_A1, ctx);
330 emit_ld(RV_REG_T2, off, RV_REG_T2, ctx);
331 off = ninsns_rvoff(tc_ninsn - (ctx->ninsns - start_insn));
332 emit_branch(BPF_JEQ, RV_REG_T2, RV_REG_ZERO, off, ctx);
338 emit_ld(RV_REG_T3, off, RV_REG_T2, ctx);
339 emit_mv(RV_REG_TCC, RV_REG_T1, ctx);
340 __build_epilogue(true, ctx);
345 struct rv_jit_context *ctx)
356 *rd = bpf_to_rv_reg(insn->dst_reg, ctx);
362 *rs = bpf_to_rv_reg(insn->src_reg, ctx);
365 static void emit_zext_32_rd_rs(u8 *rd, u8 *rs, struct rv_jit_context *ctx)
367 emit_mv(RV_REG_T2, *rd, ctx);
368 emit_zext_32(RV_REG_T2, ctx);
369 emit_mv(RV_REG_T1, *rs, ctx);
370 emit_zext_32(RV_REG_T1, ctx);
375 static void emit_sext_32_rd_rs(u8 *rd, u8 *rs, struct rv_jit_context *ctx)
377 emit_addiw(RV_REG_T2, *rd, 0, ctx);
378 emit_addiw(RV_REG_T1, *rs, 0, ctx);
383 static void emit_zext_32_rd_t1(u8 *rd, struct rv_jit_context *ctx)
385 emit_mv(RV_REG_T2, *rd, ctx);
386 emit_zext_32(RV_REG_T2, ctx);
387 emit_zext_32(RV_REG_T1, ctx);
391 static void emit_sext_32_rd(u8 *rd, struct rv_jit_context *ctx)
393 emit_addiw(RV_REG_T2, *rd, 0, ctx);
398 struct rv_jit_context *ctx)
403 emit(rv_jal(rd, rvoff >> 1), ctx);
408 emit(rv_auipc(RV_REG_T1, upper), ctx);
409 emit(rv_jalr(rd, RV_REG_T1, lower), ctx);
423 static int emit_call(u64 addr, bool fixed_addr, struct rv_jit_context *ctx)
428 if (addr && ctx->insns) {
429 ip = (u64)(long)(ctx->insns + ctx->ninsns);
433 return emit_jump_and_link(RV_REG_RA, off, fixed_addr, ctx);
436 int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
441 int s, e, rvoff, ret, i = insn - ctx->prog->insnsi;
442 struct bpf_prog_aux *aux = ctx->prog->aux;
447 init_regs(&rd, &rs, insn, ctx);
455 emit_zext_32(rd, ctx);
458 emit_mv(rd, rs, ctx);
460 emit_zext_32(rd, ctx);
466 emit_add(rd, rd, rs, ctx);
468 emit_zext_32(rd, ctx);
473 emit_sub(rd, rd, rs, ctx);
475 emit_subw(rd, rd, rs, ctx);
478 emit_zext_32(rd, ctx);
482 emit_and(rd, rd, rs, ctx);
484 emit_zext_32(rd, ctx);
488 emit_or(rd, rd, rs, ctx);
490 emit_zext_32(rd, ctx);
494 emit_xor(rd, rd, rs, ctx);
496 emit_zext_32(rd, ctx);
500 emit(is64 ? rv_mul(rd, rd, rs) : rv_mulw(rd, rd, rs), ctx);
502 emit_zext_32(rd, ctx);
506 emit(is64 ? rv_divu(rd, rd, rs) : rv_divuw(rd, rd, rs), ctx);
508 emit_zext_32(rd, ctx);
512 emit(is64 ? rv_remu(rd, rd, rs) : rv_remuw(rd, rd, rs), ctx);
514 emit_zext_32(rd, ctx);
518 emit(is64 ? rv_sll(rd, rd, rs) : rv_sllw(rd, rd, rs), ctx);
520 emit_zext_32(rd, ctx);
524 emit(is64 ? rv_srl(rd, rd, rs) : rv_srlw(rd, rd, rs), ctx);
526 emit_zext_32(rd, ctx);
530 emit(is64 ? rv_sra(rd, rd, rs) : rv_sraw(rd, rd, rs), ctx);
532 emit_zext_32(rd, ctx);
538 emit_sub(rd, RV_REG_ZERO, rd, ctx);
540 emit_zext_32(rd, ctx);
547 emit_slli(rd, rd, 48, ctx);
548 emit_srli(rd, rd, 48, ctx);
552 emit_zext_32(rd, ctx);
561 emit_li(RV_REG_T2, 0, ctx);
563 emit_andi(RV_REG_T1, rd, 0xff, ctx);
564 emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
565 emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
566 emit_srli(rd, rd, 8, ctx);
570 emit_andi(RV_REG_T1, rd, 0xff, ctx);
571 emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
572 emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
573 emit_srli(rd, rd, 8, ctx);
575 emit_andi(RV_REG_T1, rd, 0xff, ctx);
576 emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
577 emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
578 emit_srli(rd, rd, 8, ctx);
582 emit_andi(RV_REG_T1, rd, 0xff, ctx);
583 emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
584 emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
585 emit_srli(rd, rd, 8, ctx);
587 emit_andi(RV_REG_T1, rd, 0xff, ctx);
588 emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
589 emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
590 emit_srli(rd, rd, 8, ctx);
592 emit_andi(RV_REG_T1, rd, 0xff, ctx);
593 emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
594 emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
595 emit_srli(rd, rd, 8, ctx);
597 emit_andi(RV_REG_T1, rd, 0xff, ctx);
598 emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
599 emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
600 emit_srli(rd, rd, 8, ctx);
602 emit_andi(RV_REG_T1, rd, 0xff, ctx);
603 emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
605 emit_mv(rd, RV_REG_T2, ctx);
611 emit_imm(rd, imm, ctx);
613 emit_zext_32(rd, ctx);
620 emit_addi(rd, rd, imm, ctx);
622 emit_imm(RV_REG_T1, imm, ctx);
623 emit_add(rd, rd, RV_REG_T1, ctx);
626 emit_zext_32(rd, ctx);
631 emit_addi(rd, rd, -imm, ctx);
633 emit_imm(RV_REG_T1, imm, ctx);
634 emit_sub(rd, rd, RV_REG_T1, ctx);
637 emit_zext_32(rd, ctx);
642 emit_andi(rd, rd, imm, ctx);
644 emit_imm(RV_REG_T1, imm, ctx);
645 emit_and(rd, rd, RV_REG_T1, ctx);
648 emit_zext_32(rd, ctx);
653 emit(rv_ori(rd, rd, imm), ctx);
655 emit_imm(RV_REG_T1, imm, ctx);
656 emit_or(rd, rd, RV_REG_T1, ctx);
659 emit_zext_32(rd, ctx);
664 emit(rv_xori(rd, rd, imm), ctx);
666 emit_imm(RV_REG_T1, imm, ctx);
667 emit_xor(rd, rd, RV_REG_T1, ctx);
670 emit_zext_32(rd, ctx);
674 emit_imm(RV_REG_T1, imm, ctx);
676 rv_mulw(rd, rd, RV_REG_T1), ctx);
678 emit_zext_32(rd, ctx);
682 emit_imm(RV_REG_T1, imm, ctx);
684 rv_divuw(rd, rd, RV_REG_T1), ctx);
686 emit_zext_32(rd, ctx);
690 emit_imm(RV_REG_T1, imm, ctx);
692 rv_remuw(rd, rd, RV_REG_T1), ctx);
694 emit_zext_32(rd, ctx);
698 emit_slli(rd, rd, imm, ctx);
701 emit_zext_32(rd, ctx);
706 emit_srli(rd, rd, imm, ctx);
708 emit(rv_srliw(rd, rd, imm), ctx);
711 emit_zext_32(rd, ctx);
716 emit_srai(rd, rd, imm, ctx);
718 emit(rv_sraiw(rd, rd, imm), ctx);
721 emit_zext_32(rd, ctx);
726 rvoff = rv_offset(i, off, ctx);
727 ret = emit_jump_and_link(RV_REG_ZERO, rvoff, true, ctx);
755 rvoff = rv_offset(i, off, ctx);
757 s = ctx->ninsns;
759 emit_sext_32_rd_rs(&rd, &rs, ctx);
761 emit_zext_32_rd_rs(&rd, &rs, ctx);
762 e = ctx->ninsns;
771 emit_and(RV_REG_T1, rd, rs, ctx);
773 ctx);
775 emit_branch(BPF_OP(code), rd, rs, rvoff, ctx);
800 rvoff = rv_offset(i, off, ctx);
801 s = ctx->ninsns;
803 emit_imm(RV_REG_T1, imm, ctx);
811 emit_sext_32_rd(&rd, ctx);
813 emit_zext_32_rd_t1(&rd, ctx);
815 e = ctx->ninsns;
819 emit_branch(BPF_OP(code), rd, rs, rvoff, ctx);
824 rvoff = rv_offset(i, off, ctx);
825 s = ctx->ninsns;
827 emit_andi(RV_REG_T1, rd, imm, ctx);
829 emit_imm(RV_REG_T1, imm, ctx);
830 emit_and(RV_REG_T1, rd, RV_REG_T1, ctx);
837 emit_addiw(RV_REG_T1, RV_REG_T1, 0, ctx);
838 e = ctx->ninsns;
840 emit_branch(BPF_JNE, RV_REG_T1, RV_REG_ZERO, rvoff, ctx);
849 mark_call(ctx);
850 ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass,
855 ret = emit_call(addr, fixed_addr, ctx);
860 emit_mv(bpf_to_rv_reg(BPF_REG_0, ctx), RV_REG_A0, ctx);
865 if (emit_bpf_tail_call(i, ctx))
871 if (i == ctx->prog->len - 1)
874 rvoff = epilogue_offset(ctx);
875 ret = emit_jump_and_link(RV_REG_ZERO, rvoff, true, ctx);
887 emit_imm(rd, imm64, ctx);
894 emit(rv_lbu(rd, off, rs), ctx);
898 emit_imm(RV_REG_T1, off, ctx);
899 emit_add(RV_REG_T1, RV_REG_T1, rs, ctx);
900 emit(rv_lbu(rd, 0, RV_REG_T1), ctx);
906 emit(rv_lhu(rd, off, rs), ctx);
910 emit_imm(RV_REG_T1, off, ctx);
911 emit_add(RV_REG_T1, RV_REG_T1, rs, ctx);
912 emit(rv_lhu(rd, 0, RV_REG_T1), ctx);
918 emit(rv_lwu(rd, off, rs), ctx);
922 emit_imm(RV_REG_T1, off, ctx);
923 emit_add(RV_REG_T1, RV_REG_T1, rs, ctx);
924 emit(rv_lwu(rd, 0, RV_REG_T1), ctx);
930 emit_ld(rd, off, rs, ctx);
934 emit_imm(RV_REG_T1, off, ctx);
935 emit_add(RV_REG_T1, RV_REG_T1, rs, ctx);
936 emit_ld(rd, 0, RV_REG_T1, ctx);
945 emit_imm(RV_REG_T1, imm, ctx);
947 emit(rv_sb(rd, off, RV_REG_T1), ctx);
951 emit_imm(RV_REG_T2, off, ctx);
952 emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
953 emit(rv_sb(RV_REG_T2, 0, RV_REG_T1), ctx);
957 emit_imm(RV_REG_T1, imm, ctx);
959 emit(rv_sh(rd, off, RV_REG_T1), ctx);
963 emit_imm(RV_REG_T2, off, ctx);
964 emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
965 emit(rv_sh(RV_REG_T2, 0, RV_REG_T1), ctx);
968 emit_imm(RV_REG_T1, imm, ctx);
970 emit_sw(rd, off, RV_REG_T1, ctx);
974 emit_imm(RV_REG_T2, off, ctx);
975 emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
976 emit_sw(RV_REG_T2, 0, RV_REG_T1, ctx);
979 emit_imm(RV_REG_T1, imm, ctx);
981 emit_sd(rd, off, RV_REG_T1, ctx);
985 emit_imm(RV_REG_T2, off, ctx);
986 emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
987 emit_sd(RV_REG_T2, 0, RV_REG_T1, ctx);
993 emit(rv_sb(rd, off, rs), ctx);
997 emit_imm(RV_REG_T1, off, ctx);
998 emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
999 emit(rv_sb(RV_REG_T1, 0, rs), ctx);
1003 emit(rv_sh(rd, off, rs), ctx);
1007 emit_imm(RV_REG_T1, off, ctx);
1008 emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
1009 emit(rv_sh(RV_REG_T1, 0, rs), ctx);
1013 emit_sw(rd, off, rs, ctx);
1017 emit_imm(RV_REG_T1, off, ctx);
1018 emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
1019 emit_sw(RV_REG_T1, 0, rs, ctx);
1023 emit_sd(rd, off, rs, ctx);
1027 emit_imm(RV_REG_T1, off, ctx);
1028 emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
1029 emit_sd(RV_REG_T1, 0, rs, ctx);
1037 emit_addi(RV_REG_T1, rd, off, ctx);
1039 emit_imm(RV_REG_T1, off, ctx);
1040 emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
1048 rv_amoadd_d(RV_REG_ZERO, rs, rd, 0, 0), ctx);
1058 void bpf_jit_build_prologue(struct rv_jit_context *ctx)
1062 bpf_stack_adjust = round_up(ctx->prog->aux->stack_depth, 16);
1064 mark_fp(ctx);
1066 if (seen_reg(RV_REG_RA, ctx))
1069 if (seen_reg(RV_REG_S1, ctx))
1071 if (seen_reg(RV_REG_S2, ctx))
1073 if (seen_reg(RV_REG_S3, ctx))
1075 if (seen_reg(RV_REG_S4, ctx))
1077 if (seen_reg(RV_REG_S5, ctx))
1079 if (seen_reg(RV_REG_S6, ctx))
1091 emit(rv_addi(RV_REG_TCC, RV_REG_ZERO, MAX_TAIL_CALL_CNT), ctx);
1093 emit_addi(RV_REG_SP, RV_REG_SP, -stack_adjust, ctx);
1095 if (seen_reg(RV_REG_RA, ctx)) {
1096 emit_sd(RV_REG_SP, store_offset, RV_REG_RA, ctx);
1099 emit_sd(RV_REG_SP, store_offset, RV_REG_FP, ctx);
1101 if (seen_reg(RV_REG_S1, ctx)) {
1102 emit_sd(RV_REG_SP, store_offset, RV_REG_S1, ctx);
1105 if (seen_reg(RV_REG_S2, ctx)) {
1106 emit_sd(RV_REG_SP, store_offset, RV_REG_S2, ctx);
1109 if (seen_reg(RV_REG_S3, ctx)) {
1110 emit_sd(RV_REG_SP, store_offset, RV_REG_S3, ctx);
1113 if (seen_reg(RV_REG_S4, ctx)) {
1114 emit_sd(RV_REG_SP, store_offset, RV_REG_S4, ctx);
1117 if (seen_reg(RV_REG_S5, ctx)) {
1118 emit_sd(RV_REG_SP, store_offset, RV_REG_S5, ctx);
1121 if (seen_reg(RV_REG_S6, ctx)) {
1122 emit_sd(RV_REG_SP, store_offset, RV_REG_S6, ctx);
1126 emit_addi(RV_REG_FP, RV_REG_SP, stack_adjust, ctx);
1129 emit_addi(RV_REG_S5, RV_REG_SP, bpf_stack_adjust, ctx);
1134 if (seen_tail_call(ctx) && seen_call(ctx))
1135 emit_mv(RV_REG_TCC_SAVED, RV_REG_TCC, ctx);
1137 ctx->stack_size = stack_adjust;
1140 void bpf_jit_build_epilogue(struct rv_jit_context *ctx)
1142 __build_epilogue(false, ctx);