@@ -473,90 +473,24 @@ static inline void emit_kcfi(u32 hash, struct rv_jit_context *ctx)
473
473
emit (hash , ctx );
474
474
}
475
475
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 )
477
478
{
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 :
551
490
emit_ld (rd , off , rs , ctx );
552
- return ctx -> ninsns - insns_start ;
491
+ break ;
553
492
}
554
493
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 ;
560
494
}
561
495
562
496
static 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
577
511
}
578
512
}
579
513
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
+
580
532
static int emit_st (u8 rd , s16 off , s32 imm , u8 size , struct rv_jit_context * ctx )
581
533
{
582
534
int insns_start ;
@@ -622,20 +574,7 @@ static int emit_atomic_ld_st(u8 rd, u8 rs, const struct bpf_insn *insn,
622
574
switch (imm ) {
623
575
/* dst_reg = load_acquire(src_reg + off16) */
624
576
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 );
639
578
emit_fence_r_rw (ctx );
640
579
641
580
/* 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,
1859
1798
rs = RV_REG_T2 ;
1860
1799
}
1861
1800
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 );
1876
1802
1877
1803
ret = add_exception_handler (insn , ctx , rd , insn_len );
1878
1804
if (ret )
@@ -1882,6 +1808,7 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
1882
1808
return 1 ;
1883
1809
break ;
1884
1810
}
1811
+
1885
1812
/* speculation barrier */
1886
1813
case BPF_ST | BPF_NOSPEC :
1887
1814
break ;
0 commit comments