target/riscv: Smepmp: Return error when access permission not allowed in PMP
PATCH 1/1 target/riscv: Smepmp: Return error when access permission not allowed in PMP
On an address match, skip checking for default permissions and return error based on access defined in PMP configuration.
--- target/riscv/pmp.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
target/riscv/pmp.c +3 -2
@@ -435,8 +435,8 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,* defined with PMP must be used. We shouldn't fallback on * finding default privileges. */ - ret = true; -break ; + ret = ((privs & *allowed_privs) == privs ? true : false); + goto _address_matched; } }
@@ -445,6 +445,7 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr, ret =pmp_hart_has_privs_default (env, privs, allowed_privs, mode); } + _address_matched:return ret; }
Signature · 1 line
2.34.1
Quoted 14 lines · Himanshu Chauhan
On an address match, skip checking for default permissions and return error based on access defined in PMP configuration.
Fixes 90b1fafce06 ("target/riscv: Smepmp: Skip applying default rules when address matches")Signed-off-by Himanshu Chauhan <[email protected]>--- target/riscv/pmp.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)target/riscv/pmp.c +0
-0
target/riscv/pmp.c bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr, · line 439
Quoted 7 lines
target/riscv/pmp.c +2
-2@@ -435,8 +435,8 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,* defined with PMP must be used. We shouldn't fallback on * finding default privileges. */ - ret = true; -break ; + ret = ((privs & *allowed_privs) == privs ? true : false); + goto _address_matched;
goto seems unnecessary. We can directly return (privs & *allowed_privs) == privs here.
And then we can directly return pmp_hart_has_privs_default(env, privs, allowed_privs, mode) after this loop.
Regards,
Weiwei Li
Quoted 12 lines
target/riscv/pmp.c +1
-0} }@@ -445,6 +445,7 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr, ret =pmp_hart_has_privs_default (env, privs, allowed_privs, mode); } + _address_matched:return ret; }
PATCH v2 1/1 target/riscv: Smepmp: Return error when access permission not allowed in PMP
In reply to Himanshu ChauhanOn an address match, skip checking for default permissions and return error based on access defined in PMP configuration.
v2 Changes:
o Removed goto to return in place when address matches
o Call pmp_hart_has_privs_default at the end of the loop
--- target/riscv/pmp.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
target/riscv/pmp.c +2 -8
@@ -291,7 +291,6 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr, pmp_priv_t *allowed_privs, target_ulong mode) { int i = 0; -bool ret = false;int pmp_size =0 ;target_ulong s =0 ;target_ulong e =0 ;
@@ -435,17 +434,12 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,* defined with PMP must be used. We shouldn't fallback on * finding default privileges. */ - ret = true; -break ; +return (privs & *allowed_privs) == privs ? true : false; } }/* No rule matched */ -if (!ret) { - ret =pmp_hart_has_privs_default (env, privs, allowed_privs, mode); - } - -return ret; +return pmp_hart_has_privs_default (env, privs, allowed_privs, mode); }/*
Signature · 1 line
2.34.1
Quoted 26 lines · Himanshu Chauhan
On an address match, skip checking for default permissions and return error based on access defined in PMP configuration.
v2 Changes:
o Removed goto to return in place when address matches
o Callpmp_hart_has_privs_defaultat the end of the loopFixes 90b1fafce06 ("target/riscv: Smepmp: Skip applying default rules when address matches")Signed-off-by Himanshu Chauhan <[email protected]>--- target/riscv/pmp.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)target/riscv/pmp.c +0
-1@@ -291,7 +291,6 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr, pmp_priv_t *allowed_privs, target_ulong mode) { int i = 0; -bool ret = false;int pmp_size =0 ;target_ulong s =0 ;target_ulong e =0 ;
target/riscv/pmp.c bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr, · line 437
Quoted 6 lines
target/riscv/pmp.c +1
-2@@ -435,17 +434,12 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,* defined with PMP must be used. We shouldn't fallback on * finding default privileges. */ - ret = true; -break ; +return (privs & *allowed_privs) == privs ? true : false;
This conditional assignment is unnecessary.
+ return (privs & *allowed_privs) == privs;
Otherwise, Reviewed-by: Weiwei Li <[email protected]>
Weiwei Li
Quoted 13 lines
target/riscv/pmp.c +1
-5} }/* No rule matched */ -if (!ret) { - ret =pmp_hart_has_privs_default (env, privs, allowed_privs, mode); - } - -return ret; +return pmp_hart_has_privs_default (env, privs, allowed_privs, mode); }/*
Quoted 1 line · Himanshu Chauhan
+ ret = ((privs &
*allowed_privs) == privs ? true : false);
Never convert bool to bool in this way.
r~
PATCH v3 1/1 target/riscv: Smepmp: Return error when access permission not allowed in PMP
In reply to Himanshu ChauhanOn an address match, skip checking for default permissions and return error based on access defined in PMP configuration.
v3 Changes:
o Removed explicit return of boolean value from comparision of priv/allowed_priv
v2 Changes:
o Removed goto to return in place when address matches
o Call pmp_hart_has_privs_default at the end of the loop
--- target/riscv/pmp.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
target/riscv/pmp.c +2 -8
@@ -291,7 +291,6 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr, pmp_priv_t *allowed_privs, target_ulong mode) { int i = 0; -bool ret = false;int pmp_size =0 ;target_ulong s =0 ;target_ulong e =0 ;
@@ -435,17 +434,12 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,* defined with PMP must be used. We shouldn't fallback on * finding default privileges. */ - ret = true; -break ; +return (privs & *allowed_privs) == privs; } }/* No rule matched */ -if (!ret) { - ret =pmp_hart_has_privs_default (env, privs, allowed_privs, mode); - } - -return ret; +return pmp_hart_has_privs_default (env, privs, allowed_privs, mode); }/*
Signature · 1 line
2.34.1
Quoted 7 lines · Himanshu Chauhan
On an address match, skip checking for default permissions and return error based on access defined in PMP configuration.
v3 Changes:
o Removed explicit return of boolean value from comparision of priv/allowed_priv
Quoted 7 lines
v2 Changes:
o Removed goto to return in place when address matches
o Callpmp_hart_has_privs_defaultat the end of the loopFixes 90b1fafce06 ("target/riscv: Smepmp: Skip applying default rules when address matches")Signed-off-by Himanshu Chauhan <[email protected]>---
Weiwei Li
Quoted 35 lines
target/riscv/pmp.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)target/riscv/pmp.c +2
-8@@ -291,7 +291,6 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr, pmp_priv_t *allowed_privs, target_ulong mode) { int i = 0; -bool ret = false;int pmp_size =0 ;target_ulong s =0 ;target_ulong e =0 ;@@ -435,17 +434,12 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,* defined with PMP must be used. We shouldn't fallback on * finding default privileges. */ - ret = true; -break ; +return (privs & *allowed_privs) == privs; } }/* No rule matched */ -if (!ret) { - ret =pmp_hart_has_privs_default (env, privs, allowed_privs, mode); - } - -return ret; +return pmp_hart_has_privs_default (env, privs, allowed_privs, mode); }/*
Quoted 7 lines · Himanshu Chauhan
On an address match, skip checking for default permissions and return error based on access defined in PMP configuration.
v3 Changes:
o Removed explicit return of boolean value from comparision of priv/allowed_priv
Quoted 7 lines
v2 Changes:
o Removed goto to return in place when address matches
o Callpmp_hart_has_privs_defaultat the end of the loopFixes 90b1fafce06 ("target/riscv: Smepmp: Skip applying default rules when address matches")Signed-off-by Himanshu Chauhan <[email protected]>---
Quoted 35 lines
target/riscv/pmp.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)target/riscv/pmp.c +2
-8@@ -291,7 +291,6 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr, pmp_priv_t *allowed_privs, target_ulong mode) { int i = 0; -bool ret = false;int pmp_size =0 ;target_ulong s =0 ;target_ulong e =0 ;@@ -435,17 +434,12 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,* defined with PMP must be used. We shouldn't fallback on * finding default privileges. */ - ret = true; -break ; +return (privs & *allowed_privs) == privs; } }/* No rule matched */ -if (!ret) { - ret =pmp_hart_has_privs_default (env, privs, allowed_privs, mode); - } - -return ret; +return pmp_hart_has_privs_default (env, privs, allowed_privs, mode); }/*
Alistair Francis
AppliedQuoted 8 lines · Himanshu Chauhan
On an address match, skip checking for default permissions and return error based on access defined in PMP configuration.
v3 Changes:
o Removed explicit return of boolean value from comparision of priv/allowed_priv
Quoted 6 lines
v2 Changes:
o Removed goto to return in place when address matches
o Callpmp_hart_has_privs_defaultat the end of the loopFixes 90b1fafce06 ("target/riscv: Smepmp: Skip applying default rules when address matches")Signed-off-by Himanshu Chauhan <[email protected]>
Thanks!
Applied to riscv-to-apply.next
Alistair
Quoted 40 lines
--- target/riscv/pmp.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)target/riscv/pmp.c +2
-9@@ -291,7 +291,6 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr, pmp_priv_t *allowed_privs, target_ulong mode) { int i = 0; -bool ret = false;int pmp_size =0 ;target_ulong s =0 ;target_ulong e =0 ;@@ -435,17 +434,12 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,* defined with PMP must be used. We shouldn't fallback on * finding default privileges. */ - ret = true; -break ; +return (privs & *allowed_privs) == privs; } }/* No rule matched */ -if (!ret) { - ret =pmp_hart_has_privs_default (env, privs, allowed_privs, mode); - } - -return ret; +return pmp_hart_has_privs_default (env, privs, allowed_privs, mode); }/* -- 2.34.1