qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] target/riscv: fix csr check for cycle{h}, instret{h}, time{h}, hpmcounter3-31{h}
@ 2022-08-17  7:36 Weiwei Li
  2022-08-17  8:20 ` Andrew Jones
  0 siblings, 1 reply; 2+ messages in thread
From: Weiwei Li @ 2022-08-17  7:36 UTC (permalink / raw)
  To: palmer, alistair.francis, bin.meng, qemu-riscv, qemu-devel
  Cc: wangjunqiang, lazyparser, Weiwei Li

- modify check for mcounteren to work in all less-privilege mode
- modify check for scounteren to work only when S mode is enabled
- distinguish the exception type raised by check for scounteren between U
and VU mode

Signed-off-by: Weiwei Li <liweiwei@iscas.ac.cn>
Signed-off-by: Junqiang Wang <wangjunqiang@iscas.ac.cn>
---
v2:
 - Rebase on patches v13 for "Improve PMU support"

 target/riscv/csr.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
 
diff --git a/target/riscv/csr.c b/target/riscv/csr.c
index 2dcd4e5b2d..a6bf2ff964 100644
--- a/target/riscv/csr.c
+++ b/target/riscv/csr.c
@@ -98,17 +98,22 @@ static RISCVException ctr(CPURISCVState *env, int csrno)
 
 skip_ext_pmu_check:
 
-    if (((env->priv == PRV_S) && (!get_field(env->mcounteren, ctr_mask))) ||
-        ((env->priv == PRV_U) && (!get_field(env->scounteren, ctr_mask)))) {
+    if ((env->priv < PRV_M) && (!get_field(env->mcounteren, ctr_mask))) {
         return RISCV_EXCP_ILLEGAL_INST;
     }
 
     if (riscv_cpu_virt_enabled(env)) {
-        if (!get_field(env->hcounteren, ctr_mask) &&
-            get_field(env->mcounteren, ctr_mask)) {
+        if (!get_field(env->hcounteren, ctr_mask) ||
+            ((env->priv == PRV_U) && !get_field(env->scounteren, ctr_mask))) {
             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
         }
     }
+
+    if (riscv_has_ext(env, RVS) && (env->priv == PRV_U) &&
+        !get_field(env->scounteren, ctr_mask)) {
+        return RISCV_EXCP_ILLEGAL_INST;
+    }
+
 #endif
     return RISCV_EXCP_NONE;
 }
-- 
2.17.1



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] target/riscv: fix csr check for cycle{h}, instret{h},  time{h}, hpmcounter3-31{h}
  2022-08-17  7:36 [PATCH v2] target/riscv: fix csr check for cycle{h}, instret{h}, time{h}, hpmcounter3-31{h} Weiwei Li
@ 2022-08-17  8:20 ` Andrew Jones
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Jones @ 2022-08-17  8:20 UTC (permalink / raw)
  To: Weiwei Li
  Cc: palmer, alistair.francis, bin.meng, qemu-riscv, qemu-devel,
	wangjunqiang, lazyparser

On Wed, Aug 17, 2022 at 03:36:35PM +0800, Weiwei Li wrote:
> - modify check for mcounteren to work in all less-privilege mode
> - modify check for scounteren to work only when S mode is enabled
> - distinguish the exception type raised by check for scounteren between U
> and VU mode
> 
> Signed-off-by: Weiwei Li <liweiwei@iscas.ac.cn>
> Signed-off-by: Junqiang Wang <wangjunqiang@iscas.ac.cn>
> ---
> v2:
>  - Rebase on patches v13 for "Improve PMU support"
> 
>  target/riscv/csr.c | 13 +++++++++----
>  1 file changed, 9 insertions(+), 4 deletions(-)
>  
> diff --git a/target/riscv/csr.c b/target/riscv/csr.c
> index 2dcd4e5b2d..a6bf2ff964 100644
> --- a/target/riscv/csr.c
> +++ b/target/riscv/csr.c
> @@ -98,17 +98,22 @@ static RISCVException ctr(CPURISCVState *env, int csrno)
>  
>  skip_ext_pmu_check:
>  
> -    if (((env->priv == PRV_S) && (!get_field(env->mcounteren, ctr_mask))) ||
> -        ((env->priv == PRV_U) && (!get_field(env->scounteren, ctr_mask)))) {
> +    if ((env->priv < PRV_M) && (!get_field(env->mcounteren, ctr_mask))) {

I'd drop the unnecessary ()'s on both sides of the &&

>          return RISCV_EXCP_ILLEGAL_INST;
>      }
>  
>      if (riscv_cpu_virt_enabled(env)) {
> -        if (!get_field(env->hcounteren, ctr_mask) &&
> -            get_field(env->mcounteren, ctr_mask)) {
> +        if (!get_field(env->hcounteren, ctr_mask) ||
> +            ((env->priv == PRV_U) && !get_field(env->scounteren, ctr_mask))) {

() around env->priv == PRV_U can be dropped too

>              return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
>          }
>      }
> +
> +    if (riscv_has_ext(env, RVS) && (env->priv == PRV_U) &&

And here. Hmm, it seems a lot of env->priv checks in this file have
unnecessary ()'s. Oh well, I like lisp.

Thanks,
drew

> +        !get_field(env->scounteren, ctr_mask)) {
> +        return RISCV_EXCP_ILLEGAL_INST;
> +    }
> +
>  #endif
>      return RISCV_EXCP_NONE;
>  }
> -- 
> 2.17.1
> 
> 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-08-17  8:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-08-17  7:36 [PATCH v2] target/riscv: fix csr check for cycle{h}, instret{h}, time{h}, hpmcounter3-31{h} Weiwei Li
2022-08-17  8:20 ` Andrew Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).