diff mbox series

KVM: riscv: selftests: get-reg-list print_reg should never fail

Message ID 20230920143713.6896-2-ajones@ventanamicro.com (mailing list archive)
State Handled Elsewhere
Headers show
Series KVM: riscv: selftests: get-reg-list print_reg should never fail | expand

Checks

Context Check Description
conchuod/cover_letter success Single patches do not need cover letters
conchuod/tree_selection success Guessed tree name to be for-next at HEAD 0bb80ecc33a8
conchuod/fixes_present success Fixes tag not required for -next series
conchuod/maintainers_pattern success MAINTAINERS pattern errors before the patch: 5 and now 5
conchuod/verify_signedoff success Signed-off-by tag matches author and committer
conchuod/kdoc success Errors and warnings before: 0 this patch: 0
conchuod/build_rv64_clang_allmodconfig success Errors and warnings before: 12 this patch: 12
conchuod/module_param success Was 0 now: 0
conchuod/build_rv64_gcc_allmodconfig success Errors and warnings before: 13 this patch: 13
conchuod/build_rv32_defconfig success Build OK
conchuod/dtb_warn_rv64 success Errors and warnings before: 29 this patch: 29
conchuod/header_inline success No static functions without inline keyword in header files
conchuod/checkpatch warning CHECK: Alignment should match open parenthesis
conchuod/build_rv64_nommu_k210_defconfig success Build OK
conchuod/verify_fixes success No Fixes tag
conchuod/build_rv64_nommu_virt_defconfig success Build OK

Commit Message

Andrew Jones Sept. 20, 2023, 2:37 p.m. UTC
When outputting the "new" register list we want to print all of the
new registers, decoding as much as possible of each of them. Also, we
don't want to assert while listing registers with '--list'. We output
"/* UNKNOWN */" after each new register (which we were already doing
for some), which should be enough.

Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
---
 .../selftests/kvm/riscv/get-reg-list.c        | 93 +++++++++----------
 1 file changed, 42 insertions(+), 51 deletions(-)

Comments

Xu, Haibo1 Sept. 20, 2023, 2:59 p.m. UTC | #1
> -----Original Message-----
> From: Andrew Jones <ajones@ventanamicro.com>
> Sent: Wednesday, September 20, 2023 10:37 PM
> To: kvm-riscv@lists.infradead.org; linux-riscv@lists.infradead.org
> Cc: anup@brainfault.org; atishp@atishpatra.org; Xu, Haibo1
> <haibo1.xu@intel.com>; paul.walmsley@sifive.com; palmer@dabbelt.com;
> aou@eecs.berkeley.edu
> Subject: [PATCH] KVM: riscv: selftests: get-reg-list print_reg should never fail
> 
> When outputting the "new" register list we want to print all of the new
> registers, decoding as much as possible of each of them. Also, we don't want
> to assert while listing registers with '--list'. We output
> "/* UNKNOWN */" after each new register (which we were already doing for
> some), which should be enough.
> 
> Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
> ---
>  .../selftests/kvm/riscv/get-reg-list.c        | 93 +++++++++----------
>  1 file changed, 42 insertions(+), 51 deletions(-)
> 
> diff --git a/tools/testing/selftests/kvm/riscv/get-reg-list.c
> b/tools/testing/selftests/kvm/riscv/get-reg-list.c
> index 85907c86b835..054706538b9e 100644
> --- a/tools/testing/selftests/kvm/riscv/get-reg-list.c
> +++ b/tools/testing/selftests/kvm/riscv/get-reg-list.c
> @@ -112,11 +112,13 @@ void finalize_vcpu(struct kvm_vcpu *vcpu, struct
> vcpu_reg_list *c)
>  	}
>  }
> 
> -static const char *config_id_to_str(__u64 id)
> +static const char *config_id_to_str(const char *prefix, __u64 id)
>  {
>  	/* reg_off is the offset into struct kvm_riscv_config */
>  	__u64 reg_off = id & ~(REG_MASK | KVM_REG_RISCV_CONFIG);
> 
> +	assert((id & KVM_REG_RISCV_TYPE_MASK) ==
> KVM_REG_RISCV_CONFIG);
> +

I think we can skip this kind of assert test since the function was reached by:

switch (id & KVM_REG_RISCV_TYPE_MASK) {
     case KVM_REG_RISCV_CONFIG:

>  	switch (reg_off) {
>  	case KVM_REG_RISCV_CONFIG_REG(isa):
>  		return "KVM_REG_RISCV_CONFIG_REG(isa)"; @@ -134,11 +136,7
> @@ static const char *config_id_to_str(__u64 id)
	break;
>  	default:
Andrew Jones Sept. 20, 2023, 3:28 p.m. UTC | #2
On Wed, Sep 20, 2023 at 02:59:07PM +0000, Xu, Haibo1 wrote:
> > -----Original Message-----
> > From: Andrew Jones <ajones@ventanamicro.com>
> > Sent: Wednesday, September 20, 2023 10:37 PM
> > To: kvm-riscv@lists.infradead.org; linux-riscv@lists.infradead.org
> > Cc: anup@brainfault.org; atishp@atishpatra.org; Xu, Haibo1
> > <haibo1.xu@intel.com>; paul.walmsley@sifive.com; palmer@dabbelt.com;
> > aou@eecs.berkeley.edu
> > Subject: [PATCH] KVM: riscv: selftests: get-reg-list print_reg should never fail
> > 
> > When outputting the "new" register list we want to print all of the new
> > registers, decoding as much as possible of each of them. Also, we don't want
> > to assert while listing registers with '--list'. We output
> > "/* UNKNOWN */" after each new register (which we were already doing for
> > some), which should be enough.
> > 
> > Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
> > ---
> >  .../selftests/kvm/riscv/get-reg-list.c        | 93 +++++++++----------
> >  1 file changed, 42 insertions(+), 51 deletions(-)
> > 
> > diff --git a/tools/testing/selftests/kvm/riscv/get-reg-list.c
> > b/tools/testing/selftests/kvm/riscv/get-reg-list.c
> > index 85907c86b835..054706538b9e 100644
> > --- a/tools/testing/selftests/kvm/riscv/get-reg-list.c
> > +++ b/tools/testing/selftests/kvm/riscv/get-reg-list.c
> > @@ -112,11 +112,13 @@ void finalize_vcpu(struct kvm_vcpu *vcpu, struct
> > vcpu_reg_list *c)
> >  	}
> >  }
> > 
> > -static const char *config_id_to_str(__u64 id)
> > +static const char *config_id_to_str(const char *prefix, __u64 id)
> >  {
> >  	/* reg_off is the offset into struct kvm_riscv_config */
> >  	__u64 reg_off = id & ~(REG_MASK | KVM_REG_RISCV_CONFIG);
> > 
> > +	assert((id & KVM_REG_RISCV_TYPE_MASK) ==
> > KVM_REG_RISCV_CONFIG);
> > +
> 
> I think we can skip this kind of assert test since the function was reached by:
> 
> switch (id & KVM_REG_RISCV_TYPE_MASK) {
>      case KVM_REG_RISCV_CONFIG:

The assert is to ensure developers don't call it from anywhere else, where
they may not have already confirmed the register type. And, that's why
it's an 'assert' instead of a 'TEST_ASSERT'. It's for developers, not test
results.

Thanks,
drew
Xu, Haibo1 Sept. 20, 2023, 3:41 p.m. UTC | #3
> -----Original Message-----
> From: Andrew Jones <ajones@ventanamicro.com>
> Sent: Wednesday, September 20, 2023 11:28 PM
> To: Xu, Haibo1 <haibo1.xu@intel.com>
> Cc: kvm-riscv@lists.infradead.org; linux-riscv@lists.infradead.org;
> anup@brainfault.org; atishp@atishpatra.org; paul.walmsley@sifive.com;
> palmer@dabbelt.com; aou@eecs.berkeley.edu
> Subject: Re: [PATCH] KVM: riscv: selftests: get-reg-list print_reg should never
> fail
> 
> On Wed, Sep 20, 2023 at 02:59:07PM +0000, Xu, Haibo1 wrote:
> > > -----Original Message-----
> > > From: Andrew Jones <ajones@ventanamicro.com>
> > > Sent: Wednesday, September 20, 2023 10:37 PM
> > > To: kvm-riscv@lists.infradead.org; linux-riscv@lists.infradead.org
> > > Cc: anup@brainfault.org; atishp@atishpatra.org; Xu, Haibo1
> > > <haibo1.xu@intel.com>; paul.walmsley@sifive.com; palmer@dabbelt.com;
> > > aou@eecs.berkeley.edu
> > > Subject: [PATCH] KVM: riscv: selftests: get-reg-list print_reg
> > > should never fail
> > >
> > > When outputting the "new" register list we want to print all of the
> > > new registers, decoding as much as possible of each of them. Also,
> > > we don't want to assert while listing registers with '--list'. We
> > > output
> > > "/* UNKNOWN */" after each new register (which we were already doing
> > > for some), which should be enough.
> > >
> > > Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
> > > ---
> > >  .../selftests/kvm/riscv/get-reg-list.c        | 93 +++++++++----------
> > >  1 file changed, 42 insertions(+), 51 deletions(-)
> > >
> > > diff --git a/tools/testing/selftests/kvm/riscv/get-reg-list.c
> > > b/tools/testing/selftests/kvm/riscv/get-reg-list.c
> > > index 85907c86b835..054706538b9e 100644
> > > --- a/tools/testing/selftests/kvm/riscv/get-reg-list.c
> > > +++ b/tools/testing/selftests/kvm/riscv/get-reg-list.c
> > > @@ -112,11 +112,13 @@ void finalize_vcpu(struct kvm_vcpu *vcpu,
> > > struct vcpu_reg_list *c)
> > >  	}
> > >  }
> > >
> > > -static const char *config_id_to_str(__u64 id)
> > > +static const char *config_id_to_str(const char *prefix, __u64 id)
> > >  {
> > >  	/* reg_off is the offset into struct kvm_riscv_config */
> > >  	__u64 reg_off = id & ~(REG_MASK | KVM_REG_RISCV_CONFIG);
> > >
> > > +	assert((id & KVM_REG_RISCV_TYPE_MASK) ==
> > > KVM_REG_RISCV_CONFIG);
> > > +
> >
> > I think we can skip this kind of assert test since the function was reached by:
> >
> > switch (id & KVM_REG_RISCV_TYPE_MASK) {
> >      case KVM_REG_RISCV_CONFIG:
> 
> The assert is to ensure developers don't call it from anywhere else, where they
> may not have already confirmed the register type. And, that's why it's an
> 'assert' instead of a 'TEST_ASSERT'. It's for developers, not test results.
> 

Yes, it's helpful to avoid abusing this kind of APIs.
 
Reviewed-by: Haibo Xu <haibo1.xu@intel.com>

> Thanks,
> drew
Anup Patel Oct. 10, 2023, 5:59 a.m. UTC | #4
On Wed, Sep 20, 2023 at 8:07 PM Andrew Jones <ajones@ventanamicro.com> wrote:
>
> When outputting the "new" register list we want to print all of the
> new registers, decoding as much as possible of each of them. Also, we
> don't want to assert while listing registers with '--list'. We output
> "/* UNKNOWN */" after each new register (which we were already doing
> for some), which should be enough.
>
> Signed-off-by: Andrew Jones <ajones@ventanamicro.com>

Queued this patch for Linux-6.7

Thanks,
Anup

> ---
>  .../selftests/kvm/riscv/get-reg-list.c        | 93 +++++++++----------
>  1 file changed, 42 insertions(+), 51 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/riscv/get-reg-list.c b/tools/testing/selftests/kvm/riscv/get-reg-list.c
> index 85907c86b835..054706538b9e 100644
> --- a/tools/testing/selftests/kvm/riscv/get-reg-list.c
> +++ b/tools/testing/selftests/kvm/riscv/get-reg-list.c
> @@ -112,11 +112,13 @@ void finalize_vcpu(struct kvm_vcpu *vcpu, struct vcpu_reg_list *c)
>         }
>  }
>
> -static const char *config_id_to_str(__u64 id)
> +static const char *config_id_to_str(const char *prefix, __u64 id)
>  {
>         /* reg_off is the offset into struct kvm_riscv_config */
>         __u64 reg_off = id & ~(REG_MASK | KVM_REG_RISCV_CONFIG);
>
> +       assert((id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CONFIG);
> +
>         switch (reg_off) {
>         case KVM_REG_RISCV_CONFIG_REG(isa):
>                 return "KVM_REG_RISCV_CONFIG_REG(isa)";
> @@ -134,11 +136,7 @@ static const char *config_id_to_str(__u64 id)
>                 return "KVM_REG_RISCV_CONFIG_REG(satp_mode)";
>         }
>
> -       /*
> -        * Config regs would grow regularly with new pseudo reg added, so
> -        * just show raw id to indicate a new pseudo config reg.
> -        */
> -       return strdup_printf("KVM_REG_RISCV_CONFIG_REG(%lld) /* UNKNOWN */", reg_off);
> +       return strdup_printf("%lld /* UNKNOWN */", reg_off);
>  }
>
>  static const char *core_id_to_str(const char *prefix, __u64 id)
> @@ -146,6 +144,8 @@ static const char *core_id_to_str(const char *prefix, __u64 id)
>         /* reg_off is the offset into struct kvm_riscv_core */
>         __u64 reg_off = id & ~(REG_MASK | KVM_REG_RISCV_CORE);
>
> +       assert((id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CORE);
> +
>         switch (reg_off) {
>         case KVM_REG_RISCV_CORE_REG(regs.pc):
>                 return "KVM_REG_RISCV_CORE_REG(regs.pc)";
> @@ -176,8 +176,7 @@ static const char *core_id_to_str(const char *prefix, __u64 id)
>                 return "KVM_REG_RISCV_CORE_REG(mode)";
>         }
>
> -       TEST_FAIL("%s: Unknown core reg id: 0x%llx", prefix, id);
> -       return NULL;
> +       return strdup_printf("%lld /* UNKNOWN */", reg_off);
>  }
>
>  #define RISCV_CSR_GENERAL(csr) \
> @@ -211,8 +210,7 @@ static const char *general_csr_id_to_str(__u64 reg_off)
>                 return RISCV_CSR_GENERAL(scounteren);
>         }
>
> -       TEST_FAIL("Unknown general csr reg: 0x%llx", reg_off);
> -       return NULL;
> +       return strdup_printf("KVM_REG_RISCV_CSR_GENERAL | %lld /* UNKNOWN */", reg_off);
>  }
>
>  static const char *aia_csr_id_to_str(__u64 reg_off)
> @@ -235,8 +233,7 @@ static const char *aia_csr_id_to_str(__u64 reg_off)
>                 return RISCV_CSR_AIA(iprio2h);
>         }
>
> -       TEST_FAIL("Unknown aia csr reg: 0x%llx", reg_off);
> -       return NULL;
> +       return strdup_printf("KVM_REG_RISCV_CSR_AIA | %lld /* UNKNOWN */", reg_off);
>  }
>
>  static const char *csr_id_to_str(const char *prefix, __u64 id)
> @@ -244,6 +241,8 @@ static const char *csr_id_to_str(const char *prefix, __u64 id)
>         __u64 reg_off = id & ~(REG_MASK | KVM_REG_RISCV_CSR);
>         __u64 reg_subtype = reg_off & KVM_REG_RISCV_SUBTYPE_MASK;
>
> +       assert((id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CSR);
> +
>         reg_off &= ~KVM_REG_RISCV_SUBTYPE_MASK;
>
>         switch (reg_subtype) {
> @@ -253,8 +252,7 @@ static const char *csr_id_to_str(const char *prefix, __u64 id)
>                 return aia_csr_id_to_str(reg_off);
>         }
>
> -       TEST_FAIL("%s: Unknown csr subtype: 0x%llx", prefix, reg_subtype);
> -       return NULL;
> +       return strdup_printf("%lld | %lld /* UNKNOWN */", reg_subtype, reg_off);
>  }
>
>  static const char *timer_id_to_str(const char *prefix, __u64 id)
> @@ -262,6 +260,8 @@ static const char *timer_id_to_str(const char *prefix, __u64 id)
>         /* reg_off is the offset into struct kvm_riscv_timer */
>         __u64 reg_off = id & ~(REG_MASK | KVM_REG_RISCV_TIMER);
>
> +       assert((id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_TIMER);
> +
>         switch (reg_off) {
>         case KVM_REG_RISCV_TIMER_REG(frequency):
>                 return "KVM_REG_RISCV_TIMER_REG(frequency)";
> @@ -273,8 +273,7 @@ static const char *timer_id_to_str(const char *prefix, __u64 id)
>                 return "KVM_REG_RISCV_TIMER_REG(state)";
>         }
>
> -       TEST_FAIL("%s: Unknown timer reg id: 0x%llx", prefix, id);
> -       return NULL;
> +       return strdup_printf("%lld /* UNKNOWN */", reg_off);
>  }
>
>  static const char *fp_f_id_to_str(const char *prefix, __u64 id)
> @@ -282,6 +281,8 @@ static const char *fp_f_id_to_str(const char *prefix, __u64 id)
>         /* reg_off is the offset into struct __riscv_f_ext_state */
>         __u64 reg_off = id & ~(REG_MASK | KVM_REG_RISCV_FP_F);
>
> +       assert((id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_FP_F);
> +
>         switch (reg_off) {
>         case KVM_REG_RISCV_FP_F_REG(f[0]) ...
>              KVM_REG_RISCV_FP_F_REG(f[31]):
> @@ -290,8 +291,7 @@ static const char *fp_f_id_to_str(const char *prefix, __u64 id)
>                 return "KVM_REG_RISCV_FP_F_REG(fcsr)";
>         }
>
> -       TEST_FAIL("%s: Unknown fp_f reg id: 0x%llx", prefix, id);
> -       return NULL;
> +       return strdup_printf("%lld /* UNKNOWN */", reg_off);
>  }
>
>  static const char *fp_d_id_to_str(const char *prefix, __u64 id)
> @@ -299,6 +299,8 @@ static const char *fp_d_id_to_str(const char *prefix, __u64 id)
>         /* reg_off is the offset into struct __riscv_d_ext_state */
>         __u64 reg_off = id & ~(REG_MASK | KVM_REG_RISCV_FP_D);
>
> +       assert((id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_FP_D);
> +
>         switch (reg_off) {
>         case KVM_REG_RISCV_FP_D_REG(f[0]) ...
>              KVM_REG_RISCV_FP_D_REG(f[31]):
> @@ -307,15 +309,16 @@ static const char *fp_d_id_to_str(const char *prefix, __u64 id)
>                 return "KVM_REG_RISCV_FP_D_REG(fcsr)";
>         }
>
> -       TEST_FAIL("%s: Unknown fp_d reg id: 0x%llx", prefix, id);
> -       return NULL;
> +       return strdup_printf("%lld /* UNKNOWN */", reg_off);
>  }
>
> -static const char *isa_ext_id_to_str(__u64 id)
> +static const char *isa_ext_id_to_str(const char *prefix, __u64 id)
>  {
>         /* reg_off is the offset into unsigned long kvm_isa_ext_arr[] */
>         __u64 reg_off = id & ~(REG_MASK | KVM_REG_RISCV_ISA_EXT);
>
> +       assert((id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_ISA_EXT);
> +
>         static const char * const kvm_isa_ext_reg_name[] = {
>                 "KVM_RISCV_ISA_EXT_A",
>                 "KVM_RISCV_ISA_EXT_C",
> @@ -342,13 +345,8 @@ static const char *isa_ext_id_to_str(__u64 id)
>                 "KVM_RISCV_ISA_EXT_ZIHPM",
>         };
>
> -       if (reg_off >= ARRAY_SIZE(kvm_isa_ext_reg_name)) {
> -               /*
> -                * isa_ext regs would grow regularly with new isa extension added, so
> -                * just show "reg" to indicate a new extension.
> -                */
> +       if (reg_off >= ARRAY_SIZE(kvm_isa_ext_reg_name))
>                 return strdup_printf("%lld /* UNKNOWN */", reg_off);
> -       }
>
>         return kvm_isa_ext_reg_name[reg_off];
>  }
> @@ -368,35 +366,27 @@ static const char *sbi_ext_single_id_to_str(__u64 reg_off)
>                 "KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_VENDOR",
>         };
>
> -       if (reg_off >= ARRAY_SIZE(kvm_sbi_ext_reg_name)) {
> -               /*
> -                * sbi_ext regs would grow regularly with new sbi extension added, so
> -                * just show "reg" to indicate a new extension.
> -                */
> +       if (reg_off >= ARRAY_SIZE(kvm_sbi_ext_reg_name))
>                 return strdup_printf("KVM_REG_RISCV_SBI_SINGLE | %lld /* UNKNOWN */", reg_off);
> -       }
>
>         return kvm_sbi_ext_reg_name[reg_off];
>  }
>
>  static const char *sbi_ext_multi_id_to_str(__u64 reg_subtype, __u64 reg_off)
>  {
> -       if (reg_off > KVM_REG_RISCV_SBI_MULTI_REG_LAST) {
> -               /*
> -                * sbi_ext regs would grow regularly with new sbi extension added, so
> -                * just show "reg" to indicate a new extension.
> -                */
> -               return strdup_printf("%lld /* UNKNOWN */", reg_off);
> -       }
> +       const char *unknown = "";
> +
> +       if (reg_off > KVM_REG_RISCV_SBI_MULTI_REG_LAST)
> +               unknown = " /* UNKNOWN */";
>
>         switch (reg_subtype) {
>         case KVM_REG_RISCV_SBI_MULTI_EN:
> -               return strdup_printf("KVM_REG_RISCV_SBI_MULTI_EN | %lld", reg_off);
> +               return strdup_printf("KVM_REG_RISCV_SBI_MULTI_EN | %lld%s", reg_off, unknown);
>         case KVM_REG_RISCV_SBI_MULTI_DIS:
> -               return strdup_printf("KVM_REG_RISCV_SBI_MULTI_DIS | %lld", reg_off);
> +               return strdup_printf("KVM_REG_RISCV_SBI_MULTI_DIS | %lld%s", reg_off, unknown);
>         }
>
> -       return NULL;
> +       return strdup_printf("%lld | %lld /* UNKNOWN */", reg_subtype, reg_off);
>  }
>
>  static const char *sbi_ext_id_to_str(const char *prefix, __u64 id)
> @@ -404,6 +394,8 @@ static const char *sbi_ext_id_to_str(const char *prefix, __u64 id)
>         __u64 reg_off = id & ~(REG_MASK | KVM_REG_RISCV_SBI_EXT);
>         __u64 reg_subtype = reg_off & KVM_REG_RISCV_SUBTYPE_MASK;
>
> +       assert((id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_SBI_EXT);
> +
>         reg_off &= ~KVM_REG_RISCV_SUBTYPE_MASK;
>
>         switch (reg_subtype) {
> @@ -414,8 +406,7 @@ static const char *sbi_ext_id_to_str(const char *prefix, __u64 id)
>                 return sbi_ext_multi_id_to_str(reg_subtype, reg_off);
>         }
>
> -       TEST_FAIL("%s: Unknown sbi ext subtype: 0x%llx", prefix, reg_subtype);
> -       return NULL;
> +       return strdup_printf("%lld | %lld /* UNKNOWN */", reg_subtype, reg_off);
>  }
>
>  void print_reg(const char *prefix, __u64 id)
> @@ -436,14 +427,14 @@ void print_reg(const char *prefix, __u64 id)
>                 reg_size = "KVM_REG_SIZE_U128";
>                 break;
>         default:
> -               TEST_FAIL("%s: Unexpected reg size: 0x%llx in reg id: 0x%llx",
> -                         prefix, (id & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT, id);
> +               printf("\tKVM_REG_RISCV | (%lld << KVM_REG_SIZE_SHIFT) | 0x%llx /* UNKNOWN */,",
> +                      (id & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT, id & REG_MASK);
>         }
>
>         switch (id & KVM_REG_RISCV_TYPE_MASK) {
>         case KVM_REG_RISCV_CONFIG:
>                 printf("\tKVM_REG_RISCV | %s | KVM_REG_RISCV_CONFIG | %s,\n",
> -                               reg_size, config_id_to_str(id));
> +                               reg_size, config_id_to_str(prefix, id));
>                 break;
>         case KVM_REG_RISCV_CORE:
>                 printf("\tKVM_REG_RISCV | %s | KVM_REG_RISCV_CORE | %s,\n",
> @@ -467,15 +458,15 @@ void print_reg(const char *prefix, __u64 id)
>                 break;
>         case KVM_REG_RISCV_ISA_EXT:
>                 printf("\tKVM_REG_RISCV | %s | KVM_REG_RISCV_ISA_EXT | %s,\n",
> -                               reg_size, isa_ext_id_to_str(id));
> +                               reg_size, isa_ext_id_to_str(prefix, id));
>                 break;
>         case KVM_REG_RISCV_SBI_EXT:
>                 printf("\tKVM_REG_RISCV | %s | KVM_REG_RISCV_SBI_EXT | %s,\n",
>                                 reg_size, sbi_ext_id_to_str(prefix, id));
>                 break;
>         default:
> -               TEST_FAIL("%s: Unexpected reg type: 0x%llx in reg id: 0x%llx", prefix,
> -                               (id & KVM_REG_RISCV_TYPE_MASK) >> KVM_REG_RISCV_TYPE_SHIFT, id);
> +               printf("\tKVM_REG_RISCV | %s | 0x%llx /* UNKNOWN */,",
> +                               reg_size, id & REG_MASK);
>         }
>  }
>
> --
> 2.41.0
>
diff mbox series

Patch

diff --git a/tools/testing/selftests/kvm/riscv/get-reg-list.c b/tools/testing/selftests/kvm/riscv/get-reg-list.c
index 85907c86b835..054706538b9e 100644
--- a/tools/testing/selftests/kvm/riscv/get-reg-list.c
+++ b/tools/testing/selftests/kvm/riscv/get-reg-list.c
@@ -112,11 +112,13 @@  void finalize_vcpu(struct kvm_vcpu *vcpu, struct vcpu_reg_list *c)
 	}
 }
 
-static const char *config_id_to_str(__u64 id)
+static const char *config_id_to_str(const char *prefix, __u64 id)
 {
 	/* reg_off is the offset into struct kvm_riscv_config */
 	__u64 reg_off = id & ~(REG_MASK | KVM_REG_RISCV_CONFIG);
 
+	assert((id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CONFIG);
+
 	switch (reg_off) {
 	case KVM_REG_RISCV_CONFIG_REG(isa):
 		return "KVM_REG_RISCV_CONFIG_REG(isa)";
@@ -134,11 +136,7 @@  static const char *config_id_to_str(__u64 id)
 		return "KVM_REG_RISCV_CONFIG_REG(satp_mode)";
 	}
 
-	/*
-	 * Config regs would grow regularly with new pseudo reg added, so
-	 * just show raw id to indicate a new pseudo config reg.
-	 */
-	return strdup_printf("KVM_REG_RISCV_CONFIG_REG(%lld) /* UNKNOWN */", reg_off);
+	return strdup_printf("%lld /* UNKNOWN */", reg_off);
 }
 
 static const char *core_id_to_str(const char *prefix, __u64 id)
@@ -146,6 +144,8 @@  static const char *core_id_to_str(const char *prefix, __u64 id)
 	/* reg_off is the offset into struct kvm_riscv_core */
 	__u64 reg_off = id & ~(REG_MASK | KVM_REG_RISCV_CORE);
 
+	assert((id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CORE);
+
 	switch (reg_off) {
 	case KVM_REG_RISCV_CORE_REG(regs.pc):
 		return "KVM_REG_RISCV_CORE_REG(regs.pc)";
@@ -176,8 +176,7 @@  static const char *core_id_to_str(const char *prefix, __u64 id)
 		return "KVM_REG_RISCV_CORE_REG(mode)";
 	}
 
-	TEST_FAIL("%s: Unknown core reg id: 0x%llx", prefix, id);
-	return NULL;
+	return strdup_printf("%lld /* UNKNOWN */", reg_off);
 }
 
 #define RISCV_CSR_GENERAL(csr) \
@@ -211,8 +210,7 @@  static const char *general_csr_id_to_str(__u64 reg_off)
 		return RISCV_CSR_GENERAL(scounteren);
 	}
 
-	TEST_FAIL("Unknown general csr reg: 0x%llx", reg_off);
-	return NULL;
+	return strdup_printf("KVM_REG_RISCV_CSR_GENERAL | %lld /* UNKNOWN */", reg_off);
 }
 
 static const char *aia_csr_id_to_str(__u64 reg_off)
@@ -235,8 +233,7 @@  static const char *aia_csr_id_to_str(__u64 reg_off)
 		return RISCV_CSR_AIA(iprio2h);
 	}
 
-	TEST_FAIL("Unknown aia csr reg: 0x%llx", reg_off);
-	return NULL;
+	return strdup_printf("KVM_REG_RISCV_CSR_AIA | %lld /* UNKNOWN */", reg_off);
 }
 
 static const char *csr_id_to_str(const char *prefix, __u64 id)
@@ -244,6 +241,8 @@  static const char *csr_id_to_str(const char *prefix, __u64 id)
 	__u64 reg_off = id & ~(REG_MASK | KVM_REG_RISCV_CSR);
 	__u64 reg_subtype = reg_off & KVM_REG_RISCV_SUBTYPE_MASK;
 
+	assert((id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CSR);
+
 	reg_off &= ~KVM_REG_RISCV_SUBTYPE_MASK;
 
 	switch (reg_subtype) {
@@ -253,8 +252,7 @@  static const char *csr_id_to_str(const char *prefix, __u64 id)
 		return aia_csr_id_to_str(reg_off);
 	}
 
-	TEST_FAIL("%s: Unknown csr subtype: 0x%llx", prefix, reg_subtype);
-	return NULL;
+	return strdup_printf("%lld | %lld /* UNKNOWN */", reg_subtype, reg_off);
 }
 
 static const char *timer_id_to_str(const char *prefix, __u64 id)
@@ -262,6 +260,8 @@  static const char *timer_id_to_str(const char *prefix, __u64 id)
 	/* reg_off is the offset into struct kvm_riscv_timer */
 	__u64 reg_off = id & ~(REG_MASK | KVM_REG_RISCV_TIMER);
 
+	assert((id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_TIMER);
+
 	switch (reg_off) {
 	case KVM_REG_RISCV_TIMER_REG(frequency):
 		return "KVM_REG_RISCV_TIMER_REG(frequency)";
@@ -273,8 +273,7 @@  static const char *timer_id_to_str(const char *prefix, __u64 id)
 		return "KVM_REG_RISCV_TIMER_REG(state)";
 	}
 
-	TEST_FAIL("%s: Unknown timer reg id: 0x%llx", prefix, id);
-	return NULL;
+	return strdup_printf("%lld /* UNKNOWN */", reg_off);
 }
 
 static const char *fp_f_id_to_str(const char *prefix, __u64 id)
@@ -282,6 +281,8 @@  static const char *fp_f_id_to_str(const char *prefix, __u64 id)
 	/* reg_off is the offset into struct __riscv_f_ext_state */
 	__u64 reg_off = id & ~(REG_MASK | KVM_REG_RISCV_FP_F);
 
+	assert((id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_FP_F);
+
 	switch (reg_off) {
 	case KVM_REG_RISCV_FP_F_REG(f[0]) ...
 	     KVM_REG_RISCV_FP_F_REG(f[31]):
@@ -290,8 +291,7 @@  static const char *fp_f_id_to_str(const char *prefix, __u64 id)
 		return "KVM_REG_RISCV_FP_F_REG(fcsr)";
 	}
 
-	TEST_FAIL("%s: Unknown fp_f reg id: 0x%llx", prefix, id);
-	return NULL;
+	return strdup_printf("%lld /* UNKNOWN */", reg_off);
 }
 
 static const char *fp_d_id_to_str(const char *prefix, __u64 id)
@@ -299,6 +299,8 @@  static const char *fp_d_id_to_str(const char *prefix, __u64 id)
 	/* reg_off is the offset into struct __riscv_d_ext_state */
 	__u64 reg_off = id & ~(REG_MASK | KVM_REG_RISCV_FP_D);
 
+	assert((id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_FP_D);
+
 	switch (reg_off) {
 	case KVM_REG_RISCV_FP_D_REG(f[0]) ...
 	     KVM_REG_RISCV_FP_D_REG(f[31]):
@@ -307,15 +309,16 @@  static const char *fp_d_id_to_str(const char *prefix, __u64 id)
 		return "KVM_REG_RISCV_FP_D_REG(fcsr)";
 	}
 
-	TEST_FAIL("%s: Unknown fp_d reg id: 0x%llx", prefix, id);
-	return NULL;
+	return strdup_printf("%lld /* UNKNOWN */", reg_off);
 }
 
-static const char *isa_ext_id_to_str(__u64 id)
+static const char *isa_ext_id_to_str(const char *prefix, __u64 id)
 {
 	/* reg_off is the offset into unsigned long kvm_isa_ext_arr[] */
 	__u64 reg_off = id & ~(REG_MASK | KVM_REG_RISCV_ISA_EXT);
 
+	assert((id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_ISA_EXT);
+
 	static const char * const kvm_isa_ext_reg_name[] = {
 		"KVM_RISCV_ISA_EXT_A",
 		"KVM_RISCV_ISA_EXT_C",
@@ -342,13 +345,8 @@  static const char *isa_ext_id_to_str(__u64 id)
 		"KVM_RISCV_ISA_EXT_ZIHPM",
 	};
 
-	if (reg_off >= ARRAY_SIZE(kvm_isa_ext_reg_name)) {
-		/*
-		 * isa_ext regs would grow regularly with new isa extension added, so
-		 * just show "reg" to indicate a new extension.
-		 */
+	if (reg_off >= ARRAY_SIZE(kvm_isa_ext_reg_name))
 		return strdup_printf("%lld /* UNKNOWN */", reg_off);
-	}
 
 	return kvm_isa_ext_reg_name[reg_off];
 }
@@ -368,35 +366,27 @@  static const char *sbi_ext_single_id_to_str(__u64 reg_off)
 		"KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_VENDOR",
 	};
 
-	if (reg_off >= ARRAY_SIZE(kvm_sbi_ext_reg_name)) {
-		/*
-		 * sbi_ext regs would grow regularly with new sbi extension added, so
-		 * just show "reg" to indicate a new extension.
-		 */
+	if (reg_off >= ARRAY_SIZE(kvm_sbi_ext_reg_name))
 		return strdup_printf("KVM_REG_RISCV_SBI_SINGLE | %lld /* UNKNOWN */", reg_off);
-	}
 
 	return kvm_sbi_ext_reg_name[reg_off];
 }
 
 static const char *sbi_ext_multi_id_to_str(__u64 reg_subtype, __u64 reg_off)
 {
-	if (reg_off > KVM_REG_RISCV_SBI_MULTI_REG_LAST) {
-		/*
-		 * sbi_ext regs would grow regularly with new sbi extension added, so
-		 * just show "reg" to indicate a new extension.
-		 */
-		return strdup_printf("%lld /* UNKNOWN */", reg_off);
-	}
+	const char *unknown = "";
+
+	if (reg_off > KVM_REG_RISCV_SBI_MULTI_REG_LAST)
+		unknown = " /* UNKNOWN */";
 
 	switch (reg_subtype) {
 	case KVM_REG_RISCV_SBI_MULTI_EN:
-		return strdup_printf("KVM_REG_RISCV_SBI_MULTI_EN | %lld", reg_off);
+		return strdup_printf("KVM_REG_RISCV_SBI_MULTI_EN | %lld%s", reg_off, unknown);
 	case KVM_REG_RISCV_SBI_MULTI_DIS:
-		return strdup_printf("KVM_REG_RISCV_SBI_MULTI_DIS | %lld", reg_off);
+		return strdup_printf("KVM_REG_RISCV_SBI_MULTI_DIS | %lld%s", reg_off, unknown);
 	}
 
-	return NULL;
+	return strdup_printf("%lld | %lld /* UNKNOWN */", reg_subtype, reg_off);
 }
 
 static const char *sbi_ext_id_to_str(const char *prefix, __u64 id)
@@ -404,6 +394,8 @@  static const char *sbi_ext_id_to_str(const char *prefix, __u64 id)
 	__u64 reg_off = id & ~(REG_MASK | KVM_REG_RISCV_SBI_EXT);
 	__u64 reg_subtype = reg_off & KVM_REG_RISCV_SUBTYPE_MASK;
 
+	assert((id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_SBI_EXT);
+
 	reg_off &= ~KVM_REG_RISCV_SUBTYPE_MASK;
 
 	switch (reg_subtype) {
@@ -414,8 +406,7 @@  static const char *sbi_ext_id_to_str(const char *prefix, __u64 id)
 		return sbi_ext_multi_id_to_str(reg_subtype, reg_off);
 	}
 
-	TEST_FAIL("%s: Unknown sbi ext subtype: 0x%llx", prefix, reg_subtype);
-	return NULL;
+	return strdup_printf("%lld | %lld /* UNKNOWN */", reg_subtype, reg_off);
 }
 
 void print_reg(const char *prefix, __u64 id)
@@ -436,14 +427,14 @@  void print_reg(const char *prefix, __u64 id)
 		reg_size = "KVM_REG_SIZE_U128";
 		break;
 	default:
-		TEST_FAIL("%s: Unexpected reg size: 0x%llx in reg id: 0x%llx",
-			  prefix, (id & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT, id);
+		printf("\tKVM_REG_RISCV | (%lld << KVM_REG_SIZE_SHIFT) | 0x%llx /* UNKNOWN */,",
+		       (id & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT, id & REG_MASK);
 	}
 
 	switch (id & KVM_REG_RISCV_TYPE_MASK) {
 	case KVM_REG_RISCV_CONFIG:
 		printf("\tKVM_REG_RISCV | %s | KVM_REG_RISCV_CONFIG | %s,\n",
-				reg_size, config_id_to_str(id));
+				reg_size, config_id_to_str(prefix, id));
 		break;
 	case KVM_REG_RISCV_CORE:
 		printf("\tKVM_REG_RISCV | %s | KVM_REG_RISCV_CORE | %s,\n",
@@ -467,15 +458,15 @@  void print_reg(const char *prefix, __u64 id)
 		break;
 	case KVM_REG_RISCV_ISA_EXT:
 		printf("\tKVM_REG_RISCV | %s | KVM_REG_RISCV_ISA_EXT | %s,\n",
-				reg_size, isa_ext_id_to_str(id));
+				reg_size, isa_ext_id_to_str(prefix, id));
 		break;
 	case KVM_REG_RISCV_SBI_EXT:
 		printf("\tKVM_REG_RISCV | %s | KVM_REG_RISCV_SBI_EXT | %s,\n",
 				reg_size, sbi_ext_id_to_str(prefix, id));
 		break;
 	default:
-		TEST_FAIL("%s: Unexpected reg type: 0x%llx in reg id: 0x%llx", prefix,
-				(id & KVM_REG_RISCV_TYPE_MASK) >> KVM_REG_RISCV_TYPE_SHIFT, id);
+		printf("\tKVM_REG_RISCV | %s | 0x%llx /* UNKNOWN */,",
+				reg_size, id & REG_MASK);
 	}
 }