@@ -473,90 +473,24 @@ static inline void emit_kcfi(u32 hash, struct rv_jit_context *ctx)
473473 emit (hash , ctx );
474474}
475475
476- static int emit_load_8 (bool sign_ext , u8 rd , s32 off , u8 rs , struct rv_jit_context * ctx )
476+ static void emit_ldx_insn (u8 rd , s16 off , u8 rs , u8 size , bool sign_ext ,
477+ struct rv_jit_context * ctx )
477478{
478- int insns_start ;
479-
480- if (is_12b_int (off )) {
481- insns_start = ctx -> ninsns ;
482- if (sign_ext )
483- emit (rv_lb (rd , off , rs ), ctx );
484- else
485- emit (rv_lbu (rd , off , rs ), ctx );
486- return ctx -> ninsns - insns_start ;
487- }
488-
489- emit_imm (RV_REG_T1 , off , ctx );
490- emit_add (RV_REG_T1 , RV_REG_T1 , rs , ctx );
491- insns_start = ctx -> ninsns ;
492- if (sign_ext )
493- emit (rv_lb (rd , 0 , RV_REG_T1 ), ctx );
494- else
495- emit (rv_lbu (rd , 0 , RV_REG_T1 ), ctx );
496- return ctx -> ninsns - insns_start ;
497- }
498-
499- static int emit_load_16 (bool sign_ext , u8 rd , s32 off , u8 rs , struct rv_jit_context * ctx )
500- {
501- int insns_start ;
502-
503- if (is_12b_int (off )) {
504- insns_start = ctx -> ninsns ;
505- if (sign_ext )
506- emit (rv_lh (rd , off , rs ), ctx );
507- else
508- emit (rv_lhu (rd , off , rs ), ctx );
509- return ctx -> ninsns - insns_start ;
510- }
511-
512- emit_imm (RV_REG_T1 , off , ctx );
513- emit_add (RV_REG_T1 , RV_REG_T1 , rs , ctx );
514- insns_start = ctx -> ninsns ;
515- if (sign_ext )
516- emit (rv_lh (rd , 0 , RV_REG_T1 ), ctx );
517- else
518- emit (rv_lhu (rd , 0 , RV_REG_T1 ), ctx );
519- return ctx -> ninsns - insns_start ;
520- }
521-
522- static int emit_load_32 (bool sign_ext , u8 rd , s32 off , u8 rs , struct rv_jit_context * ctx )
523- {
524- int insns_start ;
525-
526- if (is_12b_int (off )) {
527- insns_start = ctx -> ninsns ;
528- if (sign_ext )
529- emit (rv_lw (rd , off , rs ), ctx );
530- else
531- emit (rv_lwu (rd , off , rs ), ctx );
532- return ctx -> ninsns - insns_start ;
533- }
534-
535- emit_imm (RV_REG_T1 , off , ctx );
536- emit_add (RV_REG_T1 , RV_REG_T1 , rs , ctx );
537- insns_start = ctx -> ninsns ;
538- if (sign_ext )
539- emit (rv_lw (rd , 0 , RV_REG_T1 ), ctx );
540- else
541- emit (rv_lwu (rd , 0 , RV_REG_T1 ), ctx );
542- return ctx -> ninsns - insns_start ;
543- }
544-
545- static int emit_load_64 (bool sign_ext , u8 rd , s32 off , u8 rs , struct rv_jit_context * ctx )
546- {
547- int insns_start ;
548-
549- if (is_12b_int (off )) {
550- insns_start = ctx -> ninsns ;
479+ switch (size ) {
480+ case BPF_B :
481+ emit (sign_ext ? rv_lb (rd , off , rs ) : rv_lbu (rd , off , rs ), ctx );
482+ break ;
483+ case BPF_H :
484+ emit (sign_ext ? rv_lh (rd , off , rs ) : rv_lhu (rd , off , rs ), ctx );
485+ break ;
486+ case BPF_W :
487+ emit (sign_ext ? rv_lw (rd , off , rs ) : rv_lwu (rd , off , rs ), ctx );
488+ break ;
489+ case BPF_DW :
551490 emit_ld (rd , off , rs , ctx );
552- return ctx -> ninsns - insns_start ;
491+ break ;
553492 }
554493
555- emit_imm (RV_REG_T1 , off , ctx );
556- emit_add (RV_REG_T1 , RV_REG_T1 , rs , ctx );
557- insns_start = ctx -> ninsns ;
558- emit_ld (rd , 0 , RV_REG_T1 , ctx );
559- return ctx -> ninsns - insns_start ;
560494}
561495
562496static void emit_stx_insn (u8 rd , s16 off , u8 rs , u8 size , struct rv_jit_context * ctx )
@@ -577,6 +511,24 @@ static void emit_stx_insn(u8 rd, s16 off, u8 rs, u8 size, struct rv_jit_context
577511 }
578512}
579513
514+ static int emit_ldx (u8 rd , s16 off , u8 rs , u8 size , bool sign_ext ,
515+ struct rv_jit_context * ctx )
516+ {
517+ int insns_start ;
518+
519+ if (is_12b_int (off )) {
520+ insns_start = ctx -> ninsns ;
521+ emit_ldx_insn (rd , off , rs , size , sign_ext , ctx );
522+ return ctx -> ninsns - insns_start ;
523+ }
524+
525+ emit_imm (RV_REG_T1 , off , ctx );
526+ emit_add (RV_REG_T1 , RV_REG_T1 , rs , ctx );
527+ insns_start = ctx -> ninsns ;
528+ emit_ldx_insn (rd , 0 , RV_REG_T1 , size , sign_ext , ctx );
529+ return ctx -> ninsns - insns_start ;
530+ }
531+
580532static int emit_st (u8 rd , s16 off , s32 imm , u8 size , struct rv_jit_context * ctx )
581533{
582534 int insns_start ;
@@ -622,20 +574,7 @@ static int emit_atomic_ld_st(u8 rd, u8 rs, const struct bpf_insn *insn,
622574 switch (imm ) {
623575 /* dst_reg = load_acquire(src_reg + off16) */
624576 case BPF_LOAD_ACQ :
625- switch (BPF_SIZE (code )) {
626- case BPF_B :
627- emit_load_8 (false, rd , off , rs , ctx );
628- break ;
629- case BPF_H :
630- emit_load_16 (false, rd , off , rs , ctx );
631- break ;
632- case BPF_W :
633- emit_load_32 (false, rd , off , rs , ctx );
634- break ;
635- case BPF_DW :
636- emit_load_64 (false, rd , off , rs , ctx );
637- break ;
638- }
577+ emit_ldx (rd , off , rs , BPF_SIZE (code ), false, ctx );
639578 emit_fence_r_rw (ctx );
640579
641580 /* If our next insn is a redundant zext, return 1 to tell
@@ -1859,20 +1798,7 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
18591798 rs = RV_REG_T2 ;
18601799 }
18611800
1862- switch (BPF_SIZE (code )) {
1863- case BPF_B :
1864- insn_len = emit_load_8 (sign_ext , rd , off , rs , ctx );
1865- break ;
1866- case BPF_H :
1867- insn_len = emit_load_16 (sign_ext , rd , off , rs , ctx );
1868- break ;
1869- case BPF_W :
1870- insn_len = emit_load_32 (sign_ext , rd , off , rs , ctx );
1871- break ;
1872- case BPF_DW :
1873- insn_len = emit_load_64 (sign_ext , rd , off , rs , ctx );
1874- break ;
1875- }
1801+ insn_len = emit_ldx (rd , off , rs , BPF_SIZE (code ), sign_ext , ctx );
18761802
18771803 ret = add_exception_handler (insn , ctx , rd , insn_len );
18781804 if (ret )
@@ -1882,6 +1808,7 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
18821808 return 1 ;
18831809 break ;
18841810 }
1811+
18851812 /* speculation barrier */
18861813 case BPF_ST | BPF_NOSPEC :
18871814 break ;
0 commit comments