RISC-V: Add support for the Zicsr extension
PATCH 1/1 RISC-V: Add support for the Zicsr extension
The various CSR instructions have been split out of the base ISA as part of the ratification process. This patch adds a Zicsr argument, which disables all the CSR instructions.
--- target/riscv/cpu.c | 1 + target/riscv/cpu.h | 1 + target/riscv/csr.c | 5 +++++ 3 files changed, 7 insertions(+)
target/riscv/cpu.c +1 -0
@@ -442,6 +442,7 @@ static Property riscv_cpu_properties[] = {DEFINE_PROP_BOOL ("u" ,RISCVCPU , cfg.ext_u, true),DEFINE_PROP_BOOL ("Counters" ,RISCVCPU , cfg.ext_counters, true),DEFINE_PROP_BOOL ("Zifencei" ,RISCVCPU , cfg.ext_ifencei, true), +DEFINE_PROP_BOOL ("Zicsr" ,RISCVCPU , cfg.ext_icsr, true),DEFINE_PROP_STRING ("priv_spec" ,RISCVCPU , cfg.priv_spec),DEFINE_PROP_BOOL ("mmu" ,RISCVCPU , cfg.mmu, true), DEFINE_PROP_BOOL("pmp", RISCVCPU, cfg.pmp, true),
target/riscv/cpu.h +1 -0
@@ -224,6 +224,7 @@ typedef struct RISCVCPU {bool ext_u;bool ext_counters;bool ext_ifencei; +bool ext_icsr;char *priv_spec;char *user_spec;
target/riscv/csr.c +5 -0
@@ -793,6 +793,7 @@ int riscv_csrrw(CPURISCVState *env, int csrno, target_ulong *ret_value, {int ret;target_ulong old_value; +RISCVCPU *cpu =env_archcpu (env);/* check privileges and return -1 if check fails */ #if !defined(CONFIG_USER_ONLY )
@@ -803,6 +804,10 @@ int riscv_csrrw(CPURISCVState *env, int csrno, target_ulong *ret_value, }#endif +/* ensure the CSR extension is enabled. */ +if (!cpu->cfg.ext_icsr) +return -1 ; +/* check predicate */ if (!csr_ops[csrno].predicate || csr_ops[csrno].predicate (env, csrno) <0 ) {return -1 ;
Signature · 1 line
2.21.0
Quoted 49 lines · Palmer Dabbelt
The various CSR instructions have been split out of the base ISA as part of the ratification process. This patch adds a Zicsr argument, which disables all the CSR instructions.
Signed-off-by Palmer Dabbelt <[email protected]>--- target/riscv/cpu.c | 1 + target/riscv/cpu.h | 1 + target/riscv/csr.c | 5 +++++ 3 files changed, 7 insertions(+)target/riscv/cpu.c +1
-0@@ -442,6 +442,7 @@ static Property riscv_cpu_properties[] = {DEFINE_PROP_BOOL ("u" ,RISCVCPU , cfg.ext_u, true),DEFINE_PROP_BOOL ("Counters" ,RISCVCPU , cfg.ext_counters, true),DEFINE_PROP_BOOL ("Zifencei" ,RISCVCPU , cfg.ext_ifencei, true), +DEFINE_PROP_BOOL ("Zicsr" ,RISCVCPU , cfg.ext_icsr, true),DEFINE_PROP_STRING ("priv_spec" ,RISCVCPU , cfg.priv_spec),DEFINE_PROP_BOOL ("mmu" ,RISCVCPU , cfg.mmu, true), DEFINE_PROP_BOOL("pmp", RISCVCPU, cfg.pmp, true),target/riscv/cpu.h +1
-0@@ -224,6 +224,7 @@ typedef struct RISCVCPU {bool ext_u;bool ext_counters;bool ext_ifencei; +bool ext_icsr;char *priv_spec;char *user_spec;target/riscv/csr.c +1
-0@@ -793,6 +793,7 @@ int riscv_csrrw(CPURISCVState *env, int csrno, target_ulong *ret_value, {int ret;target_ulong old_value; +RISCVCPU *cpu =env_archcpu (env);/* check privileges and return -1 if check fails */ #if !defined(CONFIG_USER_ONLY )
target/riscv/csr.c int riscv_csrrw(CPURISCVState *env, int csrno, target_ulong *ret_value, · line 809
Quoted 6 lines
target/riscv/csr.c +3
-0@@ -803,6 +804,10 @@ int riscv_csrrw(CPURISCVState *env, int csrno, target_ulong *ret_value, }#endif +/* ensure the CSR extension is enabled. */ +if (!cpu->cfg.ext_icsr) +return -1 ;
QEMU style include curly braces around a single line if. Plus I think it makes it less error prone. Can you add braces?
After that:
Alistair
Quoted 8 lines
target/riscv/csr.c +1
-1+/* check predicate */ if (!csr_ops[csrno].predicate || csr_ops[csrno].predicate (env, csrno) <0 ) {return -1 ; --2.21.0
Quoted 60 lines · On Tue, 25 Jun 2019 08:20:55 PDT (-0700), [email protected] wrote:
Quoted 49 lines · Palmer Dabbelt
The various CSR instructions have been split out of the base ISA as part of the ratification process. This patch adds a Zicsr argument, which disables all the CSR instructions.
Signed-off-by Palmer Dabbelt <[email protected]>--- target/riscv/cpu.c | 1 + target/riscv/cpu.h | 1 + target/riscv/csr.c | 5 +++++ 3 files changed, 7 insertions(+)target/riscv/cpu.c +1
-0@@ -442,6 +442,7 @@ static Property riscv_cpu_properties[] = {DEFINE_PROP_BOOL ("u" ,RISCVCPU , cfg.ext_u, true),DEFINE_PROP_BOOL ("Counters" ,RISCVCPU , cfg.ext_counters, true),DEFINE_PROP_BOOL ("Zifencei" ,RISCVCPU , cfg.ext_ifencei, true), +DEFINE_PROP_BOOL ("Zicsr" ,RISCVCPU , cfg.ext_icsr, true),DEFINE_PROP_STRING ("priv_spec" ,RISCVCPU , cfg.priv_spec),DEFINE_PROP_BOOL ("mmu" ,RISCVCPU , cfg.mmu, true), DEFINE_PROP_BOOL("pmp", RISCVCPU, cfg.pmp, true),target/riscv/cpu.h +1
-0@@ -224,6 +224,7 @@ typedef struct RISCVCPU {bool ext_u;bool ext_counters;bool ext_ifencei; +bool ext_icsr;char *priv_spec;char *user_spec;target/riscv/csr.c +1
-0@@ -793,6 +793,7 @@ int riscv_csrrw(CPURISCVState *env, int csrno, target_ulong *ret_value, {int ret;target_ulong old_value; +RISCVCPU *cpu =env_archcpu (env);/* check privileges and return -1 if check fails */ #if !defined(CONFIG_USER_ONLY )target/riscv/csr.c int riscv_csrrw(CPURISCVState *env, int csrno, target_ulong *ret_value, · line 809
Quoted 6 lines
target/riscv/csr.c +3
-0@@ -803,6 +804,10 @@ int riscv_csrrw(CPURISCVState *env, int csrno, target_ulong *ret_value, }#endif +/* ensure the CSR extension is enabled. */ +if (!cpu->cfg.ext_icsr) +return -1 ;QEMU style include curly braces around a single line if. Plus I think it makes it less error prone. Can you add braces?
Quoted 3 lines
After that:
Reviewed-by Alistair Francis <[email protected]>
Thanks.
Quoted 11 lines
Alistair
Quoted 8 lines
target/riscv/csr.c +1
-1+/* check predicate */ if (!csr_ops[csrno].predicate || csr_ops[csrno].predicate (env, csrno) <0 ) {return -1 ; --2.21.0