On Fri, Oct 17, 2025 at 09:29:22PM +0530, Anup Patel wrote:
> All uses of kvm_riscv_vcpu_sbi_forward() also updates retdata->uexit so
> to further reduce code duplication move retdata->uexit assignment to
> kvm_riscv_vcpu_sbi_forward() and convert it into SBI extension handler.
> 
> Signed-off-by: Anup Patel <[email protected]>
> ---
>  arch/riscv/include/asm/kvm_vcpu_sbi.h |  4 +++-
>  arch/riscv/kvm/vcpu_sbi.c             |  6 +++++-
>  arch/riscv/kvm/vcpu_sbi_base.c        | 20 +++-----------------
>  arch/riscv/kvm/vcpu_sbi_replace.c     | 27 +--------------------------
>  arch/riscv/kvm/vcpu_sbi_system.c      |  4 +---
>  arch/riscv/kvm/vcpu_sbi_v01.c         |  3 +--
>  6 files changed, 14 insertions(+), 50 deletions(-)
> 
> diff --git a/arch/riscv/include/asm/kvm_vcpu_sbi.h 
> b/arch/riscv/include/asm/kvm_vcpu_sbi.h
> index 3497489e04db..446f4a8eb3cd 100644
> --- a/arch/riscv/include/asm/kvm_vcpu_sbi.h
> +++ b/arch/riscv/include/asm/kvm_vcpu_sbi.h
> @@ -69,7 +69,9 @@ struct kvm_vcpu_sbi_extension {
>                            unsigned long reg_size, const void *reg_val);
>  };
>  
> -void kvm_riscv_vcpu_sbi_forward(struct kvm_vcpu *vcpu, struct kvm_run *run);
> +int kvm_riscv_vcpu_sbi_forward_handler(struct kvm_vcpu *vcpu,
> +                                    struct kvm_run *run,
> +                                    struct kvm_vcpu_sbi_return *retdata);
>  void kvm_riscv_vcpu_sbi_system_reset(struct kvm_vcpu *vcpu,
>                                    struct kvm_run *run,
>                                    u32 type, u64 flags);
> diff --git a/arch/riscv/kvm/vcpu_sbi.c b/arch/riscv/kvm/vcpu_sbi.c
> index 1b13623380e1..fd4106c276d8 100644
> --- a/arch/riscv/kvm/vcpu_sbi.c
> +++ b/arch/riscv/kvm/vcpu_sbi.c
> @@ -120,7 +120,9 @@ static bool riscv_vcpu_supports_sbi_ext(struct kvm_vcpu 
> *vcpu, int idx)
>       return sext && scontext->ext_status[sext->ext_idx] != 
> KVM_RISCV_SBI_EXT_STATUS_UNAVAILABLE;
>  }
>  
> -void kvm_riscv_vcpu_sbi_forward(struct kvm_vcpu *vcpu, struct kvm_run *run)
> +int kvm_riscv_vcpu_sbi_forward_handler(struct kvm_vcpu *vcpu,
> +                                    struct kvm_run *run,
> +                                    struct kvm_vcpu_sbi_return *retdata)
>  {
>       struct kvm_cpu_context *cp = &vcpu->arch.guest_context;
>  
> @@ -137,6 +139,8 @@ void kvm_riscv_vcpu_sbi_forward(struct kvm_vcpu *vcpu, 
> struct kvm_run *run)
>       run->riscv_sbi.args[5] = cp->a5;
>       run->riscv_sbi.ret[0] = SBI_ERR_NOT_SUPPORTED;
>       run->riscv_sbi.ret[1] = 0;
> +     retdata->uexit = true;
> +     return 0;
>  }
>  
>  void kvm_riscv_vcpu_sbi_system_reset(struct kvm_vcpu *vcpu,
> diff --git a/arch/riscv/kvm/vcpu_sbi_base.c b/arch/riscv/kvm/vcpu_sbi_base.c
> index 5bc570b984f4..ca489f2dfbdf 100644
> --- a/arch/riscv/kvm/vcpu_sbi_base.c
> +++ b/arch/riscv/kvm/vcpu_sbi_base.c
> @@ -41,8 +41,7 @@ static int kvm_sbi_ext_base_handler(struct kvm_vcpu *vcpu, 
> struct kvm_run *run,
>                        * For experimental/vendor extensions
>                        * forward it to the userspace
>                        */
> -                     kvm_riscv_vcpu_sbi_forward(vcpu, run);
> -                     retdata->uexit = true;
> +                     return kvm_riscv_vcpu_sbi_forward_handler(vcpu, run, 
> retdata);
>               } else {
>                       sbi_ext = kvm_vcpu_sbi_find_ext(vcpu, cp->a0);
>                       *out_val = sbi_ext && sbi_ext->probe ?
> @@ -72,27 +71,14 @@ const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_base = {
>       .handler = kvm_sbi_ext_base_handler,
>  };
>  
> -static int kvm_sbi_ext_forward_handler(struct kvm_vcpu *vcpu,
> -                                    struct kvm_run *run,
> -                                    struct kvm_vcpu_sbi_return *retdata)
> -{
> -     /*
> -      * Both SBI experimental and vendor extensions are
> -      * unconditionally forwarded to userspace.
> -      */
> -     kvm_riscv_vcpu_sbi_forward(vcpu, run);
> -     retdata->uexit = true;
> -     return 0;
> -}
> -
>  const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_experimental = {
>       .extid_start = SBI_EXT_EXPERIMENTAL_START,
>       .extid_end = SBI_EXT_EXPERIMENTAL_END,
> -     .handler = kvm_sbi_ext_forward_handler,
> +     .handler = kvm_riscv_vcpu_sbi_forward_handler,
>  };
>  
>  const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_vendor = {
>       .extid_start = SBI_EXT_VENDOR_START,
>       .extid_end = SBI_EXT_VENDOR_END,
> -     .handler = kvm_sbi_ext_forward_handler,
> +     .handler = kvm_riscv_vcpu_sbi_forward_handler,
>  };
> diff --git a/arch/riscv/kvm/vcpu_sbi_replace.c 
> b/arch/riscv/kvm/vcpu_sbi_replace.c
> index b490ed1428a6..2c456e26f6ca 100644
> --- a/arch/riscv/kvm/vcpu_sbi_replace.c
> +++ b/arch/riscv/kvm/vcpu_sbi_replace.c
> @@ -186,34 +186,9 @@ const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_srst = {
>       .handler = kvm_sbi_ext_srst_handler,
>  };
>  
> -static int kvm_sbi_ext_dbcn_handler(struct kvm_vcpu *vcpu,
> -                                 struct kvm_run *run,
> -                                 struct kvm_vcpu_sbi_return *retdata)
> -{
> -     struct kvm_cpu_context *cp = &vcpu->arch.guest_context;
> -     unsigned long funcid = cp->a6;
> -
> -     switch (funcid) {
> -     case SBI_EXT_DBCN_CONSOLE_WRITE:
> -     case SBI_EXT_DBCN_CONSOLE_READ:
> -     case SBI_EXT_DBCN_CONSOLE_WRITE_BYTE:
> -             /*
> -              * The SBI debug console functions are unconditionally
> -              * forwarded to the userspace.
> -              */
> -             kvm_riscv_vcpu_sbi_forward(vcpu, run);
> -             retdata->uexit = true;
> -             break;
> -     default:
> -             retdata->err_val = SBI_ERR_NOT_SUPPORTED;
> -     }
> -
> -     return 0;
> -}
> -
>  const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_dbcn = {
>       .extid_start = SBI_EXT_DBCN,
>       .extid_end = SBI_EXT_DBCN,
>       .default_disabled = true,
> -     .handler = kvm_sbi_ext_dbcn_handler,
> +     .handler = kvm_riscv_vcpu_sbi_forward_handler,
>  };
> diff --git a/arch/riscv/kvm/vcpu_sbi_system.c 
> b/arch/riscv/kvm/vcpu_sbi_system.c
> index 359be90b0fc5..c6f7e609ac79 100644
> --- a/arch/riscv/kvm/vcpu_sbi_system.c
> +++ b/arch/riscv/kvm/vcpu_sbi_system.c
> @@ -47,9 +47,7 @@ static int kvm_sbi_ext_susp_handler(struct kvm_vcpu *vcpu, 
> struct kvm_run *run,
>               kvm_riscv_vcpu_sbi_request_reset(vcpu, cp->a1, cp->a2);
>  
>               /* userspace provides the suspend implementation */
> -             kvm_riscv_vcpu_sbi_forward(vcpu, run);
> -             retdata->uexit = true;
> -             break;
> +             return kvm_riscv_vcpu_sbi_forward_handler(vcpu, run, retdata);
>       default:
>               retdata->err_val = SBI_ERR_NOT_SUPPORTED;
>               break;
> diff --git a/arch/riscv/kvm/vcpu_sbi_v01.c b/arch/riscv/kvm/vcpu_sbi_v01.c
> index 368dfddd23d9..188d5ea5b3b8 100644
> --- a/arch/riscv/kvm/vcpu_sbi_v01.c
> +++ b/arch/riscv/kvm/vcpu_sbi_v01.c
> @@ -32,8 +32,7 @@ static int kvm_sbi_ext_v01_handler(struct kvm_vcpu *vcpu, 
> struct kvm_run *run,
>                * The CONSOLE_GETCHAR/CONSOLE_PUTCHAR SBI calls cannot be
>                * handled in kernel so we forward these to user-space
>                */
> -             kvm_riscv_vcpu_sbi_forward(vcpu, run);
> -             retdata->uexit = true;
> +             ret = kvm_riscv_vcpu_sbi_forward_handler(vcpu, run, retdata);
>               break;
>       case SBI_EXT_0_1_SET_TIMER:
>  #if __riscv_xlen == 32
> -- 
> 2.43.0
>

Reviewed-by: Andrew Jones <[email protected]>

Reply via email to