Add S-mode checks for delegation-related CSRs
v3 cover Add S-mode checks for delegation-related CSRs
Patch 1 adds a predicate to restrict access to "medeleg, mideleg, and medelegh" to harts that support S-mode.
Patch 2 adds a privilege check for the "midelegh" CSR, which is defined by the AIA extension and only valid when Smaia is supported. This is enforced via an updated predicate in aia_smode32.
Change log:
V3:
* Add cover letter
Jay Chang (2):
target/riscv: Restrict mideleg/medeleg/medelegh access to S-mode harts target/riscv: Restrict midelegh access to S-mode harts
target/riscv/csr.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
Signature · 1 line
2.48.1
PATCH v3 1/2 target/riscv: Restrict mideleg/medeleg/medelegh access to S-mode harts
RISC-V Privileged Spec states:
"In harts with S-mode, the medeleg and mideleg registers must exist, and setting a bit in medeleg or mideleg will delegate the corresponding trap , when occurring in S-mode or U-mode, to the S-mode trap handler. In harts without S-mode, the medeleg and mideleg registers should not exist."
Add smode predicate to ensure these CSRs are only accessible when S-mode is supported.
--- target/riscv/csr.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
target/riscv/csr.c +3 -3
@@ -5862,8 +5862,8 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { NULL, read_mstatus_i128 }, [CSR_MISA] = { "misa", any, read_misa, write_misa, NULL, read_misa_i128 }, - [CSR_MIDELEG] = { "mideleg", any, NULL, NULL, rmw_mideleg }, - [CSR_MEDELEG] = { "medeleg", any, read_medeleg, write_medeleg }, + [CSR_MIDELEG] = { "mideleg", smode, NULL, NULL, rmw_mideleg }, + [CSR_MEDELEG] = { "medeleg", smode, read_medeleg, write_medeleg }, [CSR_MIE] = { "mie", any, NULL, NULL, rmw_mie }, [CSR_MTVEC] = { "mtvec", any, read_mtvec, write_mtvec }, [CSR_MCOUNTEREN] = { "mcounteren", umode, read_mcounteren,
@@ -5871,7 +5871,7 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MSTATUSH] = { "mstatush", any32, read_mstatush, write_mstatush }, - [CSR_MEDELEGH] = { "medelegh", any32, read_zero, write_ignore, + [CSR_MEDELEGH] = { "medelegh", smode32, read_zero, write_ignore, .min_priv_ver = PRIV_VERSION_1_13_0 }, [CSR_HEDELEGH] = { "hedelegh", hmode32, read_hedelegh, write_hedelegh, .min_priv_ver = PRIV_VERSION_1_13_0 },
Signature · 1 line
2.48.1
Re: [PATCH v3 1/2] target/riscv: Restrict mideleg/medeleg/medelegh access to S-mode harts
Quoted 33 lines · Jay Chang
RISC-V Privileged Spec states:
"In harts with S-mode, the medeleg and mideleg registers must exist, and setting a bit in medeleg or mideleg will delegate the corresponding trap , when occurring in S-mode or U-mode, to the S-mode trap handler. In harts without S-mode, the medeleg and mideleg registers should not exist."Add smode predicate to ensure these CSRs are only accessible when S-mode is supported.
Reviewed-by Frank Chang <[email protected]>Reviewed-by Alistair Francis <[email protected]>Signed-off-by Jay Chang <[email protected]>--- target/riscv/csr.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)target/riscv/csr.c +2
-2@@ -5862,8 +5862,8 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { NULL, read_mstatus_i128 }, [CSR_MISA] = { "misa", any, read_misa, write_misa, NULL, read_misa_i128 }, - [CSR_MIDELEG] = { "mideleg", any, NULL, NULL, rmw_mideleg }, - [CSR_MEDELEG] = { "medeleg", any, read_medeleg, write_medeleg }, + [CSR_MIDELEG] = { "mideleg", smode, NULL, NULL, rmw_mideleg }, + [CSR_MEDELEG] = { "medeleg", smode, read_medeleg, write_medeleg }, [CSR_MIE] = { "mie", any, NULL, NULL, rmw_mie }, [CSR_MTVEC] = { "mtvec", any, read_mtvec, write_mtvec }, [CSR_MCOUNTEREN] = { "mcounteren", umode, read_mcounteren,
target/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 5877
Quoted 8 lines
target/riscv/csr.c +1
-1@@ -5871,7 +5871,7 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MSTATUSH] = { "mstatush", any32, read_mstatush, write_mstatush }, - [CSR_MEDELEGH] = { "medelegh", any32, read_zero, write_ignore, + [CSR_MEDELEGH] = { "medelegh", smode32, read_zero, write_ignore, .min_priv_ver = PRIV_VERSION_1_13_0 }, [CSR_HEDELEGH] = { "hedelegh", hmode32, read_hedelegh, write_hedelegh, .min_priv_ver = PRIV_VERSION_1_13_0 },
Thanks,
Nutty
PATCH v3 2/2 target/riscv: Restrict midelegh access to S-mode harts
In reply to Jay ChangRISC-V AIA Spec states:
"For a machine-level environment, extension Smaia encompasses all added CSRs and all modifications to interrupt response behavior that the AIA specifies for a hart, over all privilege levels. For a supervisor-level environment, extension Ssaia is essentially the same as Smaia except excluding the machine-level CSRs and behavior not directly visible to supervisor level."
Since midelegh is an AIA machine-mode CSR, add Smaia extension check in aia_smode32 predicate.
--- target/riscv/csr.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
target/riscv/csr.c +5 -2
@@ -374,8 +374,11 @@ static RISCVException aia_smode(CPURISCVState *env, int csrno)static RISCVException aia_smode32 (CPURISCVState *env,int csrno) {int ret; +int csr_priv =get_field (csrno,0x300 ); -if (!riscv_cpu_cfg (env)->ext_ssaia) { +if (csr_priv ==PRV_M && !riscv_cpu_cfg (env)->ext_smaia) { +return RISCV_EXCP_ILLEGAL_INST ; + }else if (!riscv_cpu_cfg (env)->ext_ssaia) {return RISCV_EXCP_ILLEGAL_INST ; }
@@ -5911,7 +5914,7 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MVIP] = { "mvip", aia_any, NULL, NULL, rmw_mvip }, /* Machine-Level High-Half CSRs (AIA) */ - [CSR_MIDELEGH] = { "midelegh", aia_any32, NULL, NULL, rmw_midelegh }, + [CSR_MIDELEGH] = { "midelegh", aia_smode32, NULL, NULL, rmw_midelegh }, [CSR_MIEH] = { "mieh", aia_any32, NULL, NULL, rmw_mieh }, [CSR_MVIENH] = { "mvienh", aia_any32, NULL, NULL, rmw_mvienh }, [CSR_MVIPH] = { "mviph", aia_any32, NULL, NULL, rmw_mviph },
Signature · 1 line
2.48.1
Re: [PATCH v3 2/2] target/riscv: Restrict midelegh access to S-mode harts
Quoted 23 lines · Jay Chang
RISC-V AIA Spec states:
"For a machine-level environment, extension Smaia encompasses all added CSRs and all modifications to interrupt response behavior that the AIA specifies for a hart, over all privilege levels. For a supervisor-level environment, extension Ssaia is essentially the same as Smaia except excluding the machine-level CSRs and behavior not directly visible to supervisor level."Since midelegh is an AIA machine-mode CSR, add Smaia extension check in
aia_smode32predicate.Reviewed-by Frank Chang <[email protected]>Reviewed-by Alistair Francis <[email protected]>Signed-off-by Jay Chang <[email protected]>--- target/riscv/csr.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)target/riscv/csr.c +0
-0
target/riscv/csr.c static RISCVException aia_smode(CPURISCVState *env, int csrno) · line 381
Quoted 9 lines
target/riscv/csr.c +4
-1@@ -374,8 +374,11 @@ static RISCVException aia_smode(CPURISCVState *env, int csrno)static RISCVException aia_smode32 (CPURISCVState *env,int csrno) {int ret; +int csr_priv =get_field (csrno,0x300 ); -if (!riscv_cpu_cfg (env)->ext_ssaia) { +if (csr_priv ==PRV_M && !riscv_cpu_cfg (env)->ext_smaia) { +return RISCV_EXCP_ILLEGAL_INST ; + }else if (!riscv_cpu_cfg (env)->ext_ssaia) {
+ if ((csr_priv == PRV_M && !riscv_cpu_cfg(env)->ext_smaia) ||
+ (!riscv_cpu_cfg(env)->ext_ssaia)) {
Would the above code be better ?
Otherwise,
Thanks,
Nutty
Quoted 12 lines
target/riscv/csr.c +1
-1return RISCV_EXCP_ILLEGAL_INST ; }@@ -5911,7 +5914,7 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MVIP] = { "mvip", aia_any, NULL, NULL, rmw_mvip }, /* Machine-Level High-Half CSRs (AIA) */ - [CSR_MIDELEGH] = { "midelegh", aia_any32, NULL, NULL, rmw_midelegh }, + [CSR_MIDELEGH] = { "midelegh", aia_smode32, NULL, NULL, rmw_midelegh }, [CSR_MIEH] = { "mieh", aia_any32, NULL, NULL, rmw_mieh }, [CSR_MVIENH] = { "mvienh", aia_any32, NULL, NULL, rmw_mvienh }, [CSR_MVIPH] = { "mviph", aia_any32, NULL, NULL, rmw_mviph },
Re: [PATCH v3 2/2] target/riscv: Restrict midelegh access to S-mode harts
I intended to separate the S-mode and M-mode handling. Do you think this change could improve performance?
Thanks,
Jay Chang
Quoted 63 lines · Nutty Liu
target/riscv/csr.c static RISCVException aia_smode(CPURISCVState *env,
Quoted 23 lines · Jay Chang
RISC-V AIA Spec states:
"For a machine-level environment, extension Smaia encompasses all added CSRs and all modifications to interrupt response behavior that the AIA specifies for a hart, over all privilege levels. For a supervisor-level environment, extension Ssaia is essentially the same as Smaia except excluding the machine-level CSRs and behavior not directly visible to supervisor level."Since midelegh is an AIA machine-mode CSR, add Smaia extension check in
aia_smode32predicate.Reviewed-by Frank Chang <[email protected]>Reviewed-by Alistair Francis <[email protected]>Signed-off-by Jay Chang <[email protected]>--- target/riscv/csr.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)target/riscv/csr.c +0
-0@@ -374,8 +374,11 @@ static RISCVException aia_smode(CPURISCVState *env,int csrno)
Quoted 9 lines
target/riscv/csr.c +4
-1static RISCVException aia_smode32 (CPURISCVState *env,int csrno) {int ret; +int csr_priv =get_field (csrno,0x300 ); -if (!riscv_cpu_cfg (env)->ext_ssaia) { +if (csr_priv ==PRV_M && !riscv_cpu_cfg (env)->ext_smaia) { +return RISCV_EXCP_ILLEGAL_INST ; + }else if (!riscv_cpu_cfg (env)->ext_ssaia) {(quoted diff) +2
-0+ if ((csr_priv == PRV_M && !riscv_cpu_cfg(env)->ext_smaia) || + (!riscv_cpu_cfg(env)->ext_ssaia)) {Would the above code be better ?
Otherwise,Reviewed-by Nutty Liu<[email protected]>Thanks,
Nuttytarget/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 5914
Quoted 8 lines
target/riscv/csr.c +0
-1return RISCV_EXCP_ILLEGAL_INST ; }@@ -5911,7 +5914,7 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MVIP] = { "mvip", aia_any, NULL, NULL, rmw_mvip }, /* Machine-Level High-Half CSRs (AIA) */ - [CSR_MIDELEGH] = { "midelegh", aia_any32, NULL, NULL, rmw_midelegh},
target/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 5917
Quoted 1 line
target/riscv/csr.c +1
-0+ [CSR_MIDELEGH] = { "midelegh", aia_smode32, NULL, NULL,
rmw_midelegh},target/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 5918
Quoted 1 line
target/riscv/csr.c +0
-0[CSR_MIEH] = { "mieh", aia_any32, NULL, NULL, rmw_mieh},
target/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 5919
Quoted 1 line
target/riscv/csr.c +0
-0[CSR_MVIENH] = { "mvienh", aia_any32, NULL, NULL, rmw_mvienh},
target/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 5920
Quoted 1 line
target/riscv/csr.c +0
-0[CSR_MVIPH] = { "mviph", aia_any32, NULL, NULL, rmw_mviph},
Alistair Francis
AppliedQuoted 8 lines · Jay Chang
Patch 1 adds a predicate to restrict access to "medeleg, mideleg, and medelegh" to harts that support S-mode.
Patch 2 adds a privilege check for the "midelegh" CSR, which is defined by the AIA extension and only valid when Smaia is supported. This is enforced via an updated predicate in
aia_smode32.
Quoted 7 lines
Change log:
V3:
* Add cover letterJay Chang (2):
target/riscv: Restrict mideleg/medeleg/medelegh access to S-mode harts target/riscv: Restrict midelegh access to S-mode harts
Thanks!
Applied to riscv-to-apply.next
Alistair
Quoted 8 lines
target/riscv/csr.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)--
2.48.1