[PATCH] target/riscv: fix vs() to return proper error code

frank.chang@sifive.com posted 1 patch 4 years, 8 months ago
Test checkpatch passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20210223065935.20208-1-frank.chang@sifive.com
Maintainers: Alistair Francis <Alistair.Francis@wdc.com>, Sagar Karandikar <sagark@eecs.berkeley.edu>, Bastian Koppelmann <kbastian@mail.uni-paderborn.de>, Palmer Dabbelt <palmer@dabbelt.com>
target/riscv/csr.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] target/riscv: fix vs() to return proper error code
Posted by frank.chang@sifive.com 4 years, 8 months ago
From: Frank Chang <frank.chang@sifive.com>

vs() should return -RISCV_EXCP_ILLEGAL_INST instead of -1 if rvv feature
is not enabled.

If -1 is returned, exception will be raised and cs->exception_index will
be set to the negative return value. The exception will then be treated
as an instruction access fault instead of illegal instruction fault.

Signed-off-by: Frank Chang <frank.chang@sifive.com>
---
 target/riscv/csr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/target/riscv/csr.c b/target/riscv/csr.c
index fd2e6363f39..d2ae73e4a08 100644
--- a/target/riscv/csr.c
+++ b/target/riscv/csr.c
@@ -54,7 +54,7 @@ static int vs(CPURISCVState *env, int csrno)
     if (env->misa & RVV) {
         return 0;
     }
-    return -1;
+    return -RISCV_EXCP_ILLEGAL_INST;
 }
 
 static int ctr(CPURISCVState *env, int csrno)
-- 
2.17.1


Re: [PATCH] target/riscv: fix vs() to return proper error code
Posted by Richard Henderson 4 years, 8 months ago
On 2/22/21 10:59 PM, frank.chang@sifive.com wrote:
> From: Frank Chang <frank.chang@sifive.com>
> 
> vs() should return -RISCV_EXCP_ILLEGAL_INST instead of -1 if rvv feature
> is not enabled.
> 
> If -1 is returned, exception will be raised and cs->exception_index will
> be set to the negative return value. The exception will then be treated
> as an instruction access fault instead of illegal instruction fault.

It does seem an unfortunate interface; -1 seems so tempting, but does not by
itself mean anything.

I wonder if we should dispense with the whole "negative number" thing and
simply return an exception value.  Then for bonus points put all of the
RISCV_EXCP_* values in an enumeration, and return that type from these
functions so that it's perfectly clear what the interface really is.

That said,

> @@ -54,7 +54,7 @@ static int vs(CPURISCVState *env, int csrno)
>      if (env->misa & RVV) {
>          return 0;
>      }
> -    return -1;
> +    return -RISCV_EXCP_ILLEGAL_INST;

this fixes the immediate bug, so
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>


r~

Re: [PATCH] target/riscv: fix vs() to return proper error code
Posted by Alistair Francis 4 years, 8 months ago
On Tue, Feb 23, 2021 at 1:46 PM Richard Henderson
<richard.henderson@linaro.org> wrote:
>
> On 2/22/21 10:59 PM, frank.chang@sifive.com wrote:
> > From: Frank Chang <frank.chang@sifive.com>
> >
> > vs() should return -RISCV_EXCP_ILLEGAL_INST instead of -1 if rvv feature
> > is not enabled.
> >
> > If -1 is returned, exception will be raised and cs->exception_index will
> > be set to the negative return value. The exception will then be treated
> > as an instruction access fault instead of illegal instruction fault.
>
> It does seem an unfortunate interface; -1 seems so tempting, but does not by
> itself mean anything.
>
> I wonder if we should dispense with the whole "negative number" thing and
> simply return an exception value.  Then for bonus points put all of the
> RISCV_EXCP_* values in an enumeration, and return that type from these
> functions so that it's perfectly clear what the interface really is.

Good idea!

Reviewed-by: Alistair Francis <alistair.francis@wdc.com>

Alistair

>
> That said,
>
> > @@ -54,7 +54,7 @@ static int vs(CPURISCVState *env, int csrno)
> >      if (env->misa & RVV) {
> >          return 0;
> >      }
> > -    return -1;
> > +    return -RISCV_EXCP_ILLEGAL_INST;
>
> this fixes the immediate bug, so
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
>
>
> r~
>